# `Spectre.Instance.CheckpointStore`
[🔗](https://github.com/elchemista/spectre/blob/0.4.0/lib/spectre/instance/checkpoint_store.ex#L1)

Durable adapter boundary for the complete canonical Agent checkpoint.

Stores should implement `compare_and_swap/5`. The expected revision is the
last checkpoint acknowledged by the adapter and the new revision is embedded
in the encoded checkpoint as well. An adapter that cannot determine whether
a write committed must return `{:error, {:ambiguous, reason}}`; Spectre will
not retry that write automatically.

# `config`

```elixir
@type config() :: module() | {module(), keyword()} | false | nil
```

# `compare_and_swap`
*optional* 

```elixir
@callback compare_and_swap(
  Spectre.Instance.Ref.t(),
  String.t(),
  non_neg_integer(),
  non_neg_integer(),
  keyword()
) :: :ok | {:ok, term()} | {:error, term()}
```

# `erase`
*optional* 

```elixir
@callback erase(Spectre.Instance.Ref.t(), Spectre.Instance.Erasure.Request.t(), keyword()) ::
  {:ok, :erased | :already_erased} | {:error, term()}
```

# `erasure_status`
*optional* 

```elixir
@callback erasure_status(
  Spectre.Instance.Ref.t(),
  keyword()
) :: :not_erased | {:ok, Spectre.Instance.Erasure.Status.t()} | {:error, term()}
```

# `load`
*optional* 

```elixir
@callback load(
  Spectre.Instance.Ref.t(),
  keyword()
) :: :not_found | {:ok, String.t() | map()} | {:error, term()}
```

# `migrate_instance_key`
*optional* 

```elixir
@callback migrate_instance_key(
  Spectre.Instance.Ref.t(),
  Spectre.Instance.Ref.t(),
  String.t() | map(),
  String.t(),
  keyword()
) :: :ok | {:ok, :moved | :aliased} | {:error, term()}
```

# `erase`

```elixir
@spec erase(
  {module(), keyword()},
  Spectre.Instance.Ref.t(),
  Spectre.Instance.Erasure.Request.t(),
  keyword()
) :: {:ok, :erased | :already_erased} | {:error, term()}
```

Atomically removes or marks absent one exact checkpoint identity.

An adapter must leave a private marker that makes subsequent `load/3`
return `:not_found`, rejects stale CAS writes, and remains observable through
`erasure_status/3`.

# `erasure_capability`

```elixir
@spec erasure_capability(nil | {module(), keyword()}) :: :ok | {:error, term()}
```

Returns whether a normalized store implements the complete erasure boundary.

Both mutation and status read-back are required: core never reports success
from a delete callback that it cannot verify independently.

# `erasure_status`

```elixir
@spec erasure_status({module(), keyword()}, Spectre.Instance.Ref.t(), keyword()) ::
  :not_erased | {:ok, Spectre.Instance.Erasure.Status.t()} | {:error, term()}
```

Reads and validates the privacy-safe projection of an erasure marker.

# `load`

```elixir
@spec load(nil | {module(), keyword()}, Spectre.Instance.Ref.t(), keyword()) ::
  :not_found | {:ok, String.t() | map()} | {:error, term()}
```

Loads the checkpoint for `ref` from a normalized store configuration.

Returns `:not_found` when checkpointing is disabled or the adapter does not
export `load/2`. Adapter exceptions and throws are captured and returned as
`{:error, term()}`; invalid adapter replies are rejected as well.

# `migrate_instance_key`

```elixir
@spec migrate_instance_key(
  {module(), keyword()},
  Spectre.Instance.Ref.t(),
  Spectre.Instance.Ref.t(),
  String.t() | map(),
  String.t(),
  keyword()
) :: :ok | {:error, term()}
```

Atomically migrates a validated legacy Instance key to its stable key.

The adapter receives both the exact legacy checkpoint observed by core and
the migrated schema-2 checkpoint it must expose under `stable_ref`. It must
reject an existing divergent target and either move the source or leave a
controlled alias. Core verifies the target byte-for-byte after success.

# `normalize`

```elixir
@spec normalize(config()) :: {:ok, nil | {module(), keyword()}} | {:error, term()}
```

Normalizes a user-facing store configuration into `nil` (checkpointing
disabled) or a canonical `{module, opts}` tuple.

Returns `{:error, {:invalid_checkpoint_store, value}}` for anything else.

# `persist`

```elixir
@spec persist(
  {module(), keyword()},
  Spectre.Instance.Ref.t(),
  String.t(),
  non_neg_integer(),
  non_neg_integer(),
  keyword()
) :: :ok | {:error, term()}
```

Persists an encoded checkpoint through the adapter's `compare_and_swap/5`.

`expected` is the last acknowledged revision and `revision` the one being
written. Adapter exceptions, throws, and invalid replies are mapped to
`{:error, {:ambiguous, reason}}` because the write may still have committed.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
