ExAgent. Session
(ExAgent v3.0.0)
View Source
Single-writer coordination of application state and a participant roster.
Stability — Stable.
TurnPolicy uses struct state and controls scheduling/admission. Session enforces membership and lifecycle. A turn computes one complete coordination transition, then checkpoints it once. Change functions should calculate state, not perform external effects expecting transactional rollback.
With Store, successful mutation replies follow save confirmation. Checkpoint
errors preserve the new state in memory; until checkpoint/2 saves it, every
mutation returns {:error, :checkpoint_pending} and no change function runs.
Restore loads validated conversation data, not processes or arbitrary
executable continuations. JSON changes atom keys to strings and does not
redact secret content. health/1 reports :persistence with the same shape
as ExAgent.Server.
Errors keep one meaning each: {:error, :not_a_participant} for an id outside
the roster, {:error, :not_your_turn} for a member the policy does not allow
to act now, and {:error, :continuation_pending} while a bound continuation
waits for complete_turn/5 or reconcile_turn/4; health/1 names it in
:continuation_blocked.
Reads return {:ok, value} or {:error, :unavailable} on owner loss or
timeout (default 5000 ms). Continuation queries also preserve Store errors.
Every mutation accepts :timeout as its last argument's only option (5000 ms;
:infinity for checkpoint/2, complete_turn/5 and reconcile_turn/4).
{:error, :unavailable} means no process had that pid or name, so nothing was
sent. {:error, :result_unconfirmed} means the call was sent and exited without
a reply: check the state and revision before repeating it. Process links still
deliver exit signals to linked callers.
Summary
Functions
Retry only the unconfirmed save, without invoking a change function.
Returns :ok once nothing is pending or {:error, %ExAgent.CheckpointError{}}.
Child specification with id {:exagent_session, session_id} (or the :name),
restarted only after an abnormal exit like ExAgent.Server. A missing
:session_id is generated once so that supervisor restarts restore the same
session.
Complete a bound terminal run once per lifetime/run using pure host state calculation.
Query a participant's explicitly bound atomic continuation; no agent execution.
Returns {:error, :unavailable} if the Session does not reply.
Read the current participant ID, or nil if there is no current turn.
Hand the turn to participant id, bypassing the policy's next choice while
preserving its scheduling cursor; after id acts, the policy's order resumes
from there. The policy may refuse through its optional handoff/3 callback.
Emits :session_turn_changed with via: :handoff.
Report the session's identity, status, persistence and continuation state.
Read the roster of participants.
Read the current shared state.
Acknowledge a bound local diagnostic explicitly, without running a callback or advancing the turn.
Start the turn sequence. Returns {:ok, :done} when the policy has no turn to
give, and {:error, :no_participants} without any transition on an empty roster.
Start a Session with shared_state, policy, participants, session_id, pubsub,
metadata, optional trusted application namespace and Store. Namespace must
match any Store.scoped/2 descriptor. Live participant refs are supplied by
the app within the intended namespace; persisted data never resolves processes.
Read the current lifecycle status.
Functions
@spec chat( GenServer.server(), ExAgent.Session.TurnPolicy.id(), ExAgent.prompt(), keyword() ) :: {:ok, ExAgent.Result.t()} | {:error, term()}
Run a volatile agent turn for the scheduled participant. Set its :ref
to an ExAgent definition. Each handoff destination must identify a roster
agent with the same definition in its :ref. The turn policy may refuse a
transfer through handoff/3.
Returns the result of ExAgent.run/3, with the specialist's answer after a
transfer. On a transfer, the current participant changes before the specialist
runs. Call end_turn/3 separately to advance the policy. Other mutations
during the call return {:error, :turn_in_progress}.
Takes the options of ExAgent.run/3 and a :timeout for waiting for the
reply (default 5000 ms). Use :deadline to bound execution. After a lost
reply, the result is unconfirmed. Supply any conversation history as
:message_history. This operation requires a Session without a Store or
continuation bindings.
@spec checkpoint(GenServer.server(), keyword()) :: :ok | {:error, term()}
Retry only the unconfirmed save, without invoking a change function.
Returns :ok once nothing is pending or {:error, %ExAgent.CheckpointError{}}.
Child specification with id {:exagent_session, session_id} (or the :name),
restarted only after an abnormal exit like ExAgent.Server. A missing
:session_id is generated once so that supervisor restarts restore the same
session.
@spec close(GenServer.server(), keyword()) :: :ok | {:error, term()}
@spec complete_turn( GenServer.server(), integer() | String.t(), map(), (term() -> term()), keyword() ) :: {:ok, term(), ExAgent.Session.TurnPolicy.id() | :done} | {:error, term()}
Complete a bound terminal run once per lifetime/run using pure host state calculation.
@spec continuation(GenServer.server(), ExAgent.Session.TurnPolicy.id(), keyword()) :: {:ok, map()} | {:error, term()}
Query a participant's explicitly bound atomic continuation; no agent execution.
Returns {:error, :unavailable} if the Session does not reply.
@spec current(GenServer.server(), keyword()) :: {:ok, term()} | {:error, :unavailable}
Read the current participant ID, or nil if there is no current turn.
@spec end_turn(GenServer.server(), ExAgent.Session.TurnPolicy.id(), keyword()) :: {:ok, ExAgent.Session.TurnPolicy.id() | :done} | {:error, term()}
@spec handoff(GenServer.server(), ExAgent.Session.TurnPolicy.id(), keyword()) :: {:ok, ExAgent.Session.TurnPolicy.id()} | {:error, term()}
Hand the turn to participant id, bypassing the policy's next choice while
preserving its scheduling cursor; after id acts, the policy's order resumes
from there. The policy may refuse through its optional handoff/3 callback.
Emits :session_turn_changed with via: :handoff.
Returns {:ok, id} or {:error, reason}, for example {:not_running, status},
:not_a_participant or :unsupported_handoff. :timeout bounds the call
(5000 ms by default).
@spec health(GenServer.server(), keyword()) :: {:ok, map()} | {:error, :unavailable}
Report the session's identity, status, persistence and continuation state.
Use :session_id and :namespace with ExAgent.Event.session_topic/2.
The :emitter_id identifies the current process and changes on restart.
Reading health also checks the session's bound continuations.
:continuation_blocked is nil or %{participant_id: id, reason: reason},
where reason is :completion_required, :unavailable, :identity_changed,
:result_not_final or :changed_during_callback.
@spec join(GenServer.server(), ExAgent.Session.Participant.t() | keyword(), keyword()) :: :ok | {:error, term()}
@spec leave(GenServer.server(), ExAgent.Session.TurnPolicy.id(), keyword()) :: :ok | {:error, term()}
@spec participants(GenServer.server(), keyword()) :: {:ok, [ExAgent.Session.Participant.t()]} | {:error, :unavailable}
Read the roster of participants.
@spec pause(GenServer.server(), keyword()) :: :ok | {:error, term()}
@spec read_state(GenServer.server(), keyword()) :: {:ok, term()} | {:error, :unavailable}
Read the current shared state.
@spec reconcile_turn(GenServer.server(), integer() | String.t(), map(), keyword()) :: :ok | {:error, term()}
Acknowledge a bound local diagnostic explicitly, without running a callback or advancing the turn.
@spec resume(GenServer.server(), keyword()) :: :ok | {:error, term()}
@spec start(GenServer.server(), keyword()) :: {:ok, ExAgent.Session.TurnPolicy.id() | :done} | {:error, term()}
Start the turn sequence. Returns {:ok, :done} when the policy has no turn to
give, and {:error, :no_participants} without any transition on an empty roster.
Start a Session with shared_state, policy, participants, session_id, pubsub,
metadata, optional trusted application namespace and Store. Namespace must
match any Store.scoped/2 descriptor. Live participant refs are supplied by
the app within the intended namespace; persisted data never resolves processes.
Option values are validated before starting the process. A malformed list,
an unknown or duplicate key or an invalid value raises ArgumentError naming
the key, never its value. With a Store, participant IDs must be integers or
valid UTF-8 strings and metadata must encode as a JSON object.
shared_state_codec: MyCodec optionally encodes and restores application
structs through ExAgent.Session.StateCodec, after snapshot validation.
Store errors and invalid or incompatible snapshots return
{:error, {:restore_failed, reason}} rather than starting empty.
On restore, the saved roster determines membership. Startup participants
supply live refs and metadata for matching IDs of the same kind. An absent
ID is ignored unless it has a configured continuation binding, which makes
startup fail with {:restore_failed, :snapshot_roster_mismatch}. Add members
after restore with join/3. A policy that fails to initialize a new session
returns its error unwrapped, such as {:error, {:policy_exception, module}}.
@spec status(GenServer.server(), keyword()) :: {:ok, atom()} | {:error, :unavailable}
Read the current lifecycle status.
@spec take_turn( GenServer.server(), ExAgent.Session.TurnPolicy.id(), (term() -> term()), keyword() ) :: {:ok, term(), ExAgent.Session.TurnPolicy.id() | :done} | {:error, term()}
@spec update_state( GenServer.server(), ExAgent.Session.TurnPolicy.id(), (term() -> term()), keyword() ) :: {:ok, term()} | {:error, term()}