ExAgent.Continuation.Recovery (ExAgent v3.0.0)

View Source

Supervised recovery for trusted Store namespaces.

Stability — Stable.

Add a worker to the host's supervision tree to scan one page of recovery candidates per interval. The Store indexes uncertain runs, claim horizons and timer deferrals. The worker revokes expired claims using ExAgent.Continuation.recover/3. Every mutation goes through the host's continuation authorizer and the journal CAS.

Without a custom policy the worker uses ExAgent.Continuation.Recovery.Policy.default/1. For custom recovery use ExAgent.Continuation.Recovery.Policy.new/2. Its decision function receives ExAgent.Continuation.run_details/0 for a recovered, uncertain run. In multi-namespace mode the details also contain :namespace. It may return one action per visit:

  • :leave keeps the run in the recovery inbox.
  • :cancel closes a run with only unconfirmed model effects.
  • {:reconcile_model, effect_id, evidence, opts} records confirmed evidence. Supply the trusted :agent and :continuation binding or pass :catalog to resolve both.
  • {:retry_effect, effect_id, opts} authorizes a linked attempt for a model or declared read. A model also requires the agent binding, accept_duplicate_risk: true and a key as documented by ExAgent.Continuation.retry_effect/4.
  • {:reconcile_tool, effect_id, tool} invokes the declared read-only lookup for an uncertain write.

Model and tool attempts dispatch through the host's usual resume path. Set :on_ready to deliver the exact reference of a recovered ready revision to that path. It also delivers a committed initial Frame3 agent request that has yet to acquire its first claim. Give the policy a new version when its decisions change. Keep decisions, authorizers and delivery callbacks bounded.

At a timer's :wake_at, Recovery authorizes :wake_timer and commits a local :not_executed result. The Store checks the timestamp and compares the revision and deferral binding. If the run's lifetime has expired, Recovery uses the ordinary :expire authorization and transition.

Summary

Functions

Returns a specification to start this module under a supervisor.

Start a worker. Requires :store, :actor and :authorize. Give it a scoped Store, or an unscoped Store and :namespaces as a list of trusted namespace strings or a zero-arity function returning that list. The worker rotates one page per namespace and refreshes the list after each rotation.

Sweep the next page immediately and return its counts. This shares the worker's cursor with scheduled sweeps. :applied counts fresh recovery mutations, including committed timer wakeups. With :on_ready, :woken counts acknowledged deliveries. A timer that commits and triggers delivery counts in both :applied and the delivery outcome (:woken, :left or :rejected). A replay counts only as :replayed. Invalid PostgreSQL lease rows count as rejected and appear under :issues with their physical keys and reasons. Other candidates in the page are still processed. The caller's :timeout defaults to 5,000 ms. A timeout after dispatch returns {:error, :result_unconfirmed}. The sweep may still finish.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Start a worker. Requires :store, :actor and :authorize. Give it a scoped Store, or an unscoped Store and :namespaces as a list of trusted namespace strings or a zero-arity function returning that list. The worker rotates one page per namespace and refreshes the list after each rotation.

By default the worker uses Policy.default/0. Supply a value from Policy.default/1 as :policy to configure the built-in defaults. Supply Policy.new/2 as :policy for a custom decision function.

The optional unary :on_ready receives a map with :namespace, :kind, :definition and the exact :continuation reference. It runs after :authorize accepts :resume for the ready revision. Return :ok after durably scheduling that reference, :leave to leave it to an operator, or {:error, reason} if delivery could not be confirmed. Keep delivery idempotent by reference. A later pass can deliver the same ready revision again, including after a worker restart.

When an elapsed timer makes a run ready, the sweep with a fresh CAS acknowledgement can attempt delivery. Later passes can authorize and deliver that same ready revision from its timer receipt.

Optional keys are the local atom :name, :interval_ms (default 5,000) and :page_size (1–100, default 50). The first page runs after the interval. Supported Stores are supervised ETS and PostgreSQL. Unified migration 1 installs the PostgreSQL recovery and timer indexes and the follow-up table. Backfill run headers for imported 2.x C7 journals before starting Recovery. A sweep can succeed while omitting imported runs that lack both projections. See ExAgent.Store.Postgres.rebuild_run_headers/1.

Enable candidate :follow_ups with [conversations: MyApp.Conversations] to scan one page of due follow-ups per sweep. The authorizer receives :deliver_follow_up with a target containing :namespace, conversation :id, :follow_up_id and UTC millisecond :retry_at. The optional unary :run_options receives that target and supplies trusted options for the new turn.

sweep(worker, opts \\ [])

@spec sweep(GenServer.server(), keyword()) ::
  {:ok,
   %{
     :applied => non_neg_integer(),
     :replayed => non_neg_integer(),
     :left => non_neg_integer(),
     :rejected => non_neg_integer(),
     optional(:woken) => non_neg_integer(),
     optional(:follow_ups) =>
       %{
         submitted: non_neg_integer(),
         deferred: non_neg_integer(),
         rejected: non_neg_integer()
       }
       | {:error, term()},
     optional(:issues) => [
       %{key: String.t(), reason: :orphan_lease | :unexpected_lease}
     ]
   }}
  | {:error, term()}

Sweep the next page immediately and return its counts. This shares the worker's cursor with scheduled sweeps. :applied counts fresh recovery mutations, including committed timer wakeups. With :on_ready, :woken counts acknowledged deliveries. A timer that commits and triggers delivery counts in both :applied and the delivery outcome (:woken, :left or :rejected). A replay counts only as :replayed. Invalid PostgreSQL lease rows count as rejected and appear under :issues with their physical keys and reasons. Other candidates in the page are still processed. The caller's :timeout defaults to 5,000 ms. A timeout after dispatch returns {:error, :result_unconfirmed}. The sweep may still finish.

With :follow_ups, successful recovery scans also include the candidate field :follow_ups with the counts of :submitted, :deferred and :rejected deliveries, or the follow-up scan error. Submission can precede the durable turn start.