# `Spectre.GAM.Domain.Recovery.Policy`
[🔗](https://github.com/elchemista/spectre/blob/0.4.0/lib/spectre/gam/domain/recovery/policy.ex#L1)

Optional host checks and incident handling around verified Domain recovery.

Configure `recovery_policy: {MyPolicy, options}` when starting a Domain.
`check/2` receives only its identity and the recovered head, after canonical,
semantic, Genesis and payload checks. A nil head means no ledger was found:
the host can reject bootstrap of a Domain which must already exist.

The host may compare this information against an independently retained
witness. A hash chain alone cannot detect a coherently rewritten history or
rollback. Checking an older witness against a newer head requires verifying
their relationship; merely comparing revision numbers is not sufficient.
This callback does not make the host or database tamper-proof.

Optional `failed/2` receives startup failures and fatal runtime incidents.
Enqueue application-owned quarantine, backup recovery or operator review
there. Its result cannot authorize work, replace a projection or suppress the
original error. After repair, restart the Domain and run verification again.
Never blindly retry an external effect whose Outcome remains ambiguous.

Callbacks run in isolated, monitored processes with `recovery_timeout`
(default 5 seconds). Do not perform a long repair in the callback: arrange it
in the application's supervision tree. Notifications are best effort, may be
repeated on restart, and are not a durable incident log. Callback failure or
timeout cannot turn a failed verification into success.

Managed GAM also supports optional `recover/2`: return `:wait` (the default),
`{:retry, milliseconds}` or `:memory`. Retry re-verifies the existing Domain;
memory starts a distinct RAM Domain, never replacing facts in the old one.
The host chooses whether losing continuity is acceptable. See the Domain
storage guide for AgentServer checkpoint and consent handling.

This is a recovery boundary, not continuous storage monitoring. A running
Domain uses its verified prefix and checks new suffixes; it does not reread
all historical bytes before every decision. Independent audits and external
witness publication remain host responsibilities.

# `config`

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

# `context`

```elixir
@type context() :: %{domain_ref: String.t(), head: head() | nil}
```

# `head`

```elixir
@type head() :: %{revision: non_neg_integer(), head_digest: String.t()}
```

# `incident`

```elixir
@type incident() :: %{
  domain_ref: String.t(),
  phase: :startup | :runtime,
  reason: term(),
  last_verified: head() | nil
}
```

# `check`

```elixir
@callback check(
  context(),
  keyword()
) :: :ok | {:error, term()}
```

# `failed`
*optional* 

```elixir
@callback failed(
  incident(),
  keyword()
) :: term()
```

# `recover`
*optional* 

```elixir
@callback recover(
  incident(),
  keyword()
) :: :wait | :memory | {:retry, non_neg_integer()}
```

# `notify_failed`

```elixir
@spec notify_failed(config(), incident(), pos_integer()) :: :ok
```

Dispatches a bounded incident notification without delaying a failing Domain.

# `recover`

Optional host lifecycle decision; it never replaces or authorizes GAM facts.

---

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