ExAgent.Session.Snapshot (ExAgent v1.1.0)

Copy Markdown View Source

A serializable checkpoint of an ExAgent.Session's coordination state.

Carries the serializable parts of a session: the app-defined shared_state, the participant roster (ids + kinds — never the live refs), the turn-policy module and its state, the current participant, and the status.

Just like ExAgent.Server.Snapshot, it round-trips through strict JSON so nothing opaque (pids, secrets, closures, the live agent refs) can land in the store. The live participant refs come from the app on restart.

The shared_state portability rule

shared_state must be JSON-encodable (plain maps/lists/scalars, or a struct with @derive [Jason.Encoder] whose fields are themselves JSON-safe — avoid tuples, which Jason turns into arrays that don't round-trip). Jason.encode! raises rather than persisting junk, exactly like Server.Snapshot does for metadata.

Summary

Functions

Decode JSON back into a snapshot, reconstructing the policy struct.

Build a snapshot from a Session.State, stripping non-serializable refs.

Strict JSON encode. Raises on non-encodable values (pids, closures, …).

Types

t()

@type t() :: %ExAgent.Session.Snapshot{
  current: term() | nil,
  metadata: map(),
  participants: [%{id: term(), kind: atom()}],
  policy_mod: module(),
  policy_state: term(),
  saved_at: String.t() | nil,
  seq: non_neg_integer(),
  session_id: String.t(),
  shared_state: term(),
  status: atom(),
  version: pos_integer()
}

Functions

deserialize(binary)

@spec deserialize(binary()) :: {:ok, t()} | {:error, term()}

Decode JSON back into a snapshot, reconstructing the policy struct.

new(s)

Build a snapshot from a Session.State, stripping non-serializable refs.

serialize(snap)

@spec serialize(t()) :: binary()

Strict JSON encode. Raises on non-encodable values (pids, closures, …).