Documentation
¶
Overview ¶
Package agentstate is the automaton of a conversation's agent: where it is (wire.AgentState) and what one input — a hook, a transcript entry, a look at the screen, the process under the pane, the launch, a command — makes of it.
Next is a pure function over the Rules table: the same state and input give the same state and effects, nothing is read or written. The conversation's actor feeds it and carries the effects out (events, the queue, the gates). unknown is "not checked yet": nothing here treats it as idle.
Index ¶
Constants ¶
const LaunchAliveFor wire.Ms = 10 * 60_000
LaunchAliveFor is how long a launch whose agent CLI is up in the pane may take before it fails: a loaded machine starts Claude in minutes (its MCP servers, plugins and session hooks), and a slow start is not a failed one.
Variables ¶
var InputKinds = []InputKind{LaunchRequested, LaunchStep, LaunchReady, LaunchFailed, Tick, SessionStart, PromptSubmit, PermissionPrompt, PreCompact, PostCompact, Stop, StopFailure, TurnOpened, QuestionAsked, PlanProposed, GateResolved, ScreenBusy, Quiet, Interrupted, Shell, Gone, Restart}
InputKinds is every InputKind, in the order of the table.
var Rules = []Rule{ {In: LaunchRequested, From: notWorking, To: wire.StateStarting}, {In: LaunchStep, From: kinds(wire.StateStarting), To: wire.StateStarting}, {In: LaunchReady, From: kinds(wire.StateStarting, wire.StateUnknown, wire.StateStopped, wire.StateLaunchFailed), To: wire.StateIdle, Effects: []EffectKind{WakeQueue}}, {In: LaunchReady, From: kinds(wire.StateWorking), Guard: turnEnded, To: wire.StateIdle, Effects: []EffectKind{WakeQueue}}, {In: LaunchFailed, From: kinds(wire.StateStarting), To: wire.StateLaunchFailed, Effects: []EffectKind{PauseQueue}}, {In: Tick, From: kinds(wire.StateStarting), Guard: launchOver, To: wire.StateLaunchFailed, Effects: []EffectKind{PauseQueue}}, {In: SessionStart, From: kinds(wire.StateStarting, wire.StateUnknown, wire.StateStopped, wire.StateLaunchFailed), To: wire.StateIdle, Effects: []EffectKind{WakeQueue}}, {In: SessionStart, From: kinds(wire.StateIdle, wire.StateReady), To: wire.StateIdle}, {In: SessionStart, From: kinds(wire.StateWorking), Guard: compacting, To: wire.StateWorking, Effects: []EffectKind{CompactionFinished}}, {In: PromptSubmit, From: kinds(wire.StateWorking), Effects: []EffectKind{PromptSubmitted}}, {In: PromptSubmit, From: waiting, To: wire.StateWorking, Effects: []EffectKind{PromptSubmitted, CloseGate}}, {In: PromptSubmit, From: append(kinds(wire.StateStarting), runningIdle...), To: wire.StateWorking, Effects: []EffectKind{PromptSubmitted}}, {In: TurnOpened, From: kinds(wire.StateWorking), Guard: newTurn, To: wire.StateWorking, Effects: []EffectKind{TurnStarted}}, {In: TurnOpened, From: runningIdle, To: wire.StateWorking, Effects: []EffectKind{TurnStarted}}, {In: QuestionAsked, From: kinds(wire.StateWorking, wire.StateUnknown), To: wire.StateWaitingQuestion, Effects: []EffectKind{AskQuestion}}, {In: PermissionPrompt, From: kinds(wire.StateWorking, wire.StateUnknown, wire.StateIdle, wire.StateReady), To: wire.StateWaitingPermission, Effects: []EffectKind{RequestPermission}}, {In: PermissionPrompt, From: kinds(wire.StateStarting, wire.StateStopped, wire.StateLaunchFailed), Guard: alive, To: wire.StateWaitingPermission, Effects: []EffectKind{RequestPermission}}, {In: PlanProposed, From: kinds(wire.StateWorking, wire.StateUnknown), To: wire.StateWaitingPermission, Effects: []EffectKind{ProposePlan}}, {In: GateResolved, From: waiting, To: wire.StateWorking, Effects: []EffectKind{GateMovedOn}}, {In: PreCompact, From: kinds(wire.StateWorking), To: wire.StateWorking, Effects: []EffectKind{CompactionStarted}}, {In: PreCompact, From: runningIdle, To: wire.StateWorking, Effects: []EffectKind{CompactionStarted}}, {In: PostCompact, From: kinds(wire.StateWorking), To: wire.StateWorking, Effects: []EffectKind{CompactionFinished}}, {In: Stop, From: kinds(wire.StateWorking), To: wire.StateReady, Effects: []EffectKind{TurnFinished, WakeQueue, NotifyPerson}}, {In: Stop, From: waiting, To: wire.StateReady, Effects: []EffectKind{CloseGate, TurnFinished, WakeQueue, NotifyPerson}}, {In: StopFailure, From: live, To: wire.StateReady, Effects: []EffectKind{CloseGate, TurnFailed, WakeQueue, NotifyPerson}}, {In: Interrupted, From: live, Guard: pauses, To: wire.StateIdle, Effects: []EffectKind{CloseGate, TurnInterrupted, PauseQueue}}, {In: Interrupted, From: live, To: wire.StateIdle, Effects: []EffectKind{CloseGate, TurnInterrupted, WakeQueue}}, {In: ScreenBusy, From: kinds(wire.StateUnknown, wire.StateStopped), To: wire.StateWorking}, {In: Quiet, From: kinds(wire.StateWorking), Guard: turnEnded, To: wire.StateReady, Effects: []EffectKind{TurnFinished, WakeQueue}}, {In: Quiet, From: kinds(wire.StateWorking), Guard: turnOpen, To: wire.StateIdle, Effects: []EffectKind{TurnFinished, WakeQueue}}, {In: Quiet, From: kinds(wire.StateUnknown), Guard: turnEnded, To: wire.StateReady, Effects: []EffectKind{WakeQueue}}, {In: Quiet, From: kinds(wire.StateUnknown), Guard: turnOpen, To: wire.StateIdle, Effects: []EffectKind{WakeQueue}}, {In: Shell, From: live, To: wire.StateStopped, Effects: []EffectKind{CloseGate, TurnFailed, WakeQueue}}, {In: Shell, From: kinds(wire.StateIdle, wire.StateReady, wire.StateUnknown, wire.StateStopped), To: wire.StateStopped, Effects: []EffectKind{WakeQueue}}, {In: Shell, From: kinds(wire.StateGone), Guard: tmuxLost, To: wire.StateStopped, Effects: []EffectKind{WakeQueue}}, {In: Gone, From: live, To: wire.StateGone, Effects: []EffectKind{CloseGate, TurnFailed, PauseQueue}}, {In: Gone, From: notGone, To: wire.StateGone, Effects: []EffectKind{PauseQueue}}, {In: Restart, To: wire.StateUnknown}, }
Rules is the table of §4.2 of the spec. Where it was silent the rows say so in their comment.
Functions ¶
This section is empty.
Types ¶
type Effect ¶
type Effect struct {
K EffectKind
ID string
Turn wire.TurnFailureCode // turn-failed
Detail string // turn-failed: what the agent said about it
Pause wire.PauseReason // pause-queue
Reason wire.GateCloseReason // gate-closed
}
Effect is one thing to do; ID is the gate or the turn it is about, Code and Reason its why.
func Next ¶
func Next(st wire.AgentState, in Input) (wire.AgentState, []Effect)
Next is the state after in and what to do about it. An input no row takes changes nothing.
type EffectKind ¶
type EffectKind string
EffectKind names what the actor must do after a transition.
const ( WakeQueue EffectKind = "wake-queue" PauseQueue EffectKind = "pause-queue" PromptSubmitted EffectKind = "prompt-submitted" // the message in flight is accepted (in-input → accepted) TurnStarted EffectKind = "turn-started" TurnFinished EffectKind = "turn-finished" TurnFailed EffectKind = "turn-failed" TurnInterrupted EffectKind = "turn-interrupted" AskQuestion EffectKind = "question-asked" RequestPermission EffectKind = "permission-requested" ProposePlan EffectKind = "plan-proposed" GateMovedOn EffectKind = "gate-moved-on" // answered when we answered it, else closed{agent-moved-on} CloseGate EffectKind = "gate-closed" CompactionStarted EffectKind = "compaction-started" CompactionFinished EffectKind = "compaction-finished" NotifyPerson EffectKind = "notify" )
type Input ¶
type Input struct {
K InputKind
At wire.Ms
Step wire.LaunchStep // launch-step
Failure wire.LaunchFailure // launch-failed
TurnID string // turn-opened
GateID string // question-asked, permission-prompt, plan-proposed
TurnFailure wire.TurnFailureCode // stop-failure
TurnDetail string // stop-failure: what the agent said about it, "" nothing
TurnEnded bool // quiet: the transcript ends with the turn's end
Resumable bool // shell: the agent has a session to resume
GoneReason wire.GoneReason // gone
PauseQueue bool // interrupted
Alive bool // launch tick or startup dialog: the agent CLI is in the pane's foreground
}
Input is one thing that happened, at At; the other fields belong to some kinds only.
type InputKind ¶
type InputKind string
InputKind names what happened.
const ( // The launch (internal/launch, prompts.LaunchPhase). LaunchRequested InputKind = "launch-requested" LaunchStep InputKind = "launch-step" LaunchReady InputKind = "launch-ready" LaunchFailed InputKind = "launch-failed" // Tick is the clock: a starting agent past its deadline has failed to launch. Tick InputKind = "tick" // Hooks (agents/claude/hook.go, agents/codex/hook.go). SessionStart InputKind = "session-start" PromptSubmit InputKind = "prompt-submit" PermissionPrompt InputKind = "permission-prompt" // notification(permission_prompt), Codex PermissionRequest PreCompact InputKind = "pre-compact" PostCompact InputKind = "post-compact" Stop InputKind = "stop" StopFailure InputKind = "stop-failure" // The transcript and the screen. TurnOpened InputKind = "turn-opened" // a user entry that opens a turn QuestionAsked InputKind = "question-asked" // ask-user-question without a result, or the question dialog PlanProposed InputKind = "plan-proposed" // ExitPlanMode, or the plan menu GateResolved InputKind = "gate-resolved" // the gate's tool result, an entry after it, or its menu gone ScreenBusy InputKind = "screen-busy" // a probe of an unchecked agent sees its spinner Quiet InputKind = "quiet" // long silence and an input line without a spinner (decideStuck) // Commands and processes. Interrupted InputKind = "interrupted" // our Escape went in Shell InputKind = "shell" // a shell, not the agent, is in the pane's foreground Gone InputKind = "gone" // no tmux session, or the tab closed Restart InputKind = "restart" // the runtime started )
type Rule ¶
type Rule struct {
In InputKind
From []wire.AgentStateKind
Guard func(st wire.AgentState, in Input) bool
To wire.AgentStateKind
Effects []EffectKind
}
Rule is one row of the table: an input taken in one of From (nil: any state) where Guard holds goes To ("" stays in the same kind, its fields kept) and asks for Effects. The first row that matches wins; no row, no change.