ExAgent. Session. TurnPolicy behaviour
(ExAgent v2.1.0)
View Source
Decides the order in which session participants take turns.
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.
Callbacks
init/1— build the policy state from options.optscarries`: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— whetherparticipant_idmay act right now (thepolicy tracks the current actor).participant_joined/2/participant_left/2— keep the policy's roster insync as participants come and go.
Built-in implementations
ExAgent.Session.TurnPolicy.RoundRobin— insertion order, cycling forever.ExAgent.Session.TurnPolicy.Initiative— an explicit:order, cycling forever.ExAgent.Session.TurnPolicy.SupervisorPolicy— a supervisor alternates with workers.
Summary
Callbacks
Override the current actor without advancing the scheduling cursor.
Opt-in JSON data codec for persisted custom policies.
Types
@type context() :: %{ shared_state: term(), participants: [ExAgent.Session.Participant.t()] }
@type id() :: term()
@type state() :: struct()
Callbacks
Override the current actor without advancing the scheduling cursor.
@callback participant_joined(state(), ExAgent.Session.Participant.t()) :: state()
@callback restore_snapshot(pos_integer(), term(), context()) :: {:ok, state()} | {:error, term()}
@callback snapshot(state()) :: {:ok, pos_integer(), term()} | {:error, term()}
Opt-in JSON data codec for persisted custom policies.