ExAgent.Session.TurnPolicy behaviour (ExAgent v3.0.0)

View Source

Decides the order in which session participants take turns.

Stability — Stable.

A turn policy is a small, stateful module that sequences participants. The ExAgent.Session owns no scheduling logic of its own — it delegates "who acts next" and "may this participant act now" to the policy, so the same Session can drive a round-robin chat, an initiative-ordered combat, or a supervisor that alternates with workers.

Policy state is a struct whose module implements this behaviour; arbitrary map or tuple states are not dispatched. Handoff uses an explicit callback, or keeps state unchanged when the destination is already admitted by can_act?/3. Custom persistence requires the opt-in versioned data codec callbacks; stored bytes never select a module to load or execute.

init/1 receives the initial participants in the order supplied by the host. Later callbacks receive the current roster in context.participants. Its enumeration order is unspecified. Keep any scheduling order and cursor in the policy state.

Session can call can_act?/3 to admit the current actor and again to validate the next actor before saving the turn. Treat it as a repeatable query. Advance the cursor in the state returned by next_participant/2.

Callbacks

  • init/1 — build the policy state from options. opts carries
                        `:participants` (the initial list) plus any
                        policy-specific keys (e.g. `:order` for initiative).
  • next_participant/2 — return {:ok, id, new_state} for the next actor,
                        or `{:done, new_state}` when the sequence is
                        exhausted. `context` carries `%{shared_state: _, participants: _}`
                        for policies that need it.
  • can_act?/3 — whether participant_id may act right now (the
                        policy tracks the current actor).
  • participant_joined/2 / participant_left/2 — keep the policy's roster in
                        sync as participants come and go.

Built-in implementations

Summary

Types

context()

@type context() :: %{
  shared_state: term(),
  participants: [ExAgent.Session.Participant.t()]
}

id()

@type id() :: term()

state()

@type state() :: struct()

Callbacks

can_act?(state, participant_id, context)

@callback can_act?(state(), participant_id :: id(), context()) :: boolean()

handoff(state, id, context)

(optional)
@callback handoff(state(), id(), context()) :: {:ok, state()} | {:error, term()}

Override the current actor without advancing the scheduling cursor.

init(opts)

@callback init(opts :: keyword()) :: state()

next_participant(state, context)

@callback next_participant(state(), context()) :: {:ok, id(), state()} | {:done, state()}

participant_joined(state, t)

(optional)
@callback participant_joined(state(), ExAgent.Session.Participant.t()) :: state()

participant_left(state, id)

(optional)
@callback participant_left(state(), id()) :: state()

restore_snapshot(pos_integer, term, context)

(optional)
@callback restore_snapshot(pos_integer(), term(), context()) ::
  {:ok, state()} | {:error, term()}

snapshot(state)

(optional)
@callback snapshot(state()) :: {:ok, pos_integer(), term()} | {:error, term()}

Opt-in JSON data codec for persisted custom policies.

Functions

can_act?(state, id, ctx)

@spec can_act?(state(), id(), context()) :: boolean()

handoff(state, id, ctx)

init(mod, opts)

@spec init(module(), keyword()) :: state()

next_participant(state, ctx)

@spec next_participant(state(), context()) :: {:ok, id(), state()} | {:done, state()}

participant_joined(state, participant)

@spec participant_joined(state(), ExAgent.Session.Participant.t()) :: state()

participant_left(state, id)

@spec participant_left(state(), id()) :: state()