Skip to content
Source on GitHub (opens in a new tab)

The reducer (core/loop) folds a session’s log into a State. This document describes that state’s lifecycle: the few facts that say what the session is doing, the rules between them, and how the tests check that the reducer keeps them. It describes the reducer as it is; the durability and transitions contracts say what it promises.

The lifecycle is a set of independent facts, each a small finite machine read from the state’s fields (lifecycleOf in core/loop/lifecycle_test.go).

Fact States Read from
Session unstarted, started, deleted no configuration yet; Deleted
Run none, admitting, active RunID; hooks due or pending before the run starts
Ending none, held, asked Ending: held while Stop hooks decide, or asked of the driver
Interrupt none, interrupting Interrupt: its reason and the outcome it ends with
Blocked no, yes Blocked
Budget normal, due, sent the ledger’s wrap-up
Gates a set of: hooks, compact-gate:checked, compact-gate:blocked, classifying, switch-checked, compact-requested Due, CompactGate, Classifying, SwitchChecked, CompactRequest
Suspension no, yes Suspended
Move none, prepared-host, prepared-tool, committed-host, committed-tool Move, by whether a tool call made it
Restore none, rewind, rewind+restore, restore an unfinished Rewind, FileRestore or both
Materialization materializing, releasing, owned, each no or yes Materializing, Releasing, the inputs and publications held
Effect per pending effect: pending, running, waiting, retryable its attempt and whether it is open (E2)
Background processes, subagents, timers, sources, each no or yes anything running, unfinished, live or not ended

An effect is pending before its first attempt, running while an attempt is open, waiting while an input request’s attempt is open, and retryable between attempts.

The facts are independent where the reducer keeps them so. Interrupt and Blocked are separate: an unanswerable question blocks the run, and a later interrupt doesn’t clear it. Suspension isn’t tied to a waiting request: an answer can resolve one without resuming the session.

A fact changes only when an event folds, and the tests observe it at fold boundaries, labelled by the event that changed it. A fold can pass through states it doesn’t expose: RunFinished can start the next run from queued input, so active → active is a change, and an interrupt replaces an asked ending and may ask for another in the same fold.

Each scalar fact changes only by the edges listed here (edges in core/loop/edges_test.go), each labelled by the kind of event whose fold makes it; a command is labelled by its command kind and an effect start by its effect kind. The tests check every fold of every test, fixture and generated session against the list, so a change the reducer makes that isn’t listed fails them: it is either a bug or an edge to add here. Each gate is listed as a fact of its own (hooks-due, compact-gate, classifying, switch-checked, compact-requested). The pending effects are a set whose members move as E2 says, which rule I14 checks effect by effect.

A fold that passes through a state and leaves it, such as a run ending and the next starting from queued input, shows no change of that fact, so it lists none.

Fact From Event To
session started command_accepted:delete_session deleted
session unstarted session_started started
run active run_finished admitting
run active run_finished none
run admitting effect_cancelled none
run admitting effect_finished active
run admitting effect_finished none
run none command_accepted:background_notice active
run none command_accepted:background_notice admitting
run none command_accepted:compact active
run none command_accepted:compact admitting
run none command_accepted:invoke_skill active
run none command_accepted:invoke_skill admitting
run none command_accepted:occurrence active
run none command_accepted:user_message active
run none command_accepted:user_message admitting
run none run_finished active
run none workspace_move_handed_off active
ending asked command_accepted:clear_goal none
ending asked command_accepted:set_goal none
ending asked run_finished none
ending held command_accepted:interrupt asked
ending held effect_cancelled asked
ending held effect_finished asked
ending none command_accepted:interrupt asked
ending none command_accepted:user_message asked
ending none effect_cancelled asked
ending none effect_finished asked
ending none effect_finished held
ending none quota_exceeded asked
ending none timer_fired asked
ending none workspace_move_prepared asked
interrupt interrupting run_finished none
interrupt none command_accepted:background_calls interrupting
interrupt none command_accepted:interrupt interrupting
interrupt none effect_cancelled interrupting
interrupt none effect_finished interrupting
interrupt none quota_exceeded interrupting
interrupt none timer_fired interrupting
interrupt none workspace_move_prepared interrupting
blocked no effect_finished yes
blocked yes run_finished no
budget due effect_finished sent
budget due effect_reconciled sent
budget due run_finished normal
budget normal effect_finished due
budget normal effect_finished sent
budget sent run_finished normal
suspended no suspended yes
suspended yes resumed no
move committed-host workspace_move_handed_off none
move committed-host workspace_move_prepared prepared-host
move committed-tool workspace_move_handed_off none
move none workspace_move_prepared prepared-host
move none workspace_move_prepared prepared-tool
move prepared-host workspace_move_committed committed-host
move prepared-tool workspace_move_committed committed-tool
restore none file_restore_requested restore
restore none rewind_requested rewind
restore restore file_restore_finished none
restore rewind file_restore_requested rewind+restore
restore rewind rewind_finished none
restore rewind+restore file_restore_finished rewind
materializing no skill_materialized yes
materializing yes effect_finished no
releasing no materialization_released yes
releasing yes effect_finished no
owned no effect_started:materialize yes
owned yes effect_finished no
processes no process_started yes
processes yes evidence_recorded no
processes yes process_ended no
subagents no child_started yes
subagents yes child_finished no
timers no command_accepted:user_message yes
timers no effect_started:await_input yes
timers no run_finished yes
timers no workspace_move_handed_off yes
timers yes command_accepted:answer_input no
timers yes command_accepted:interrupt no
timers yes effect_cancelled no
timers yes effect_finished no
timers yes timer_fired no
timers yes workspace_move_prepared no
sources no command_accepted:create_source yes
sources no command_accepted:schedule yes
sources yes command_accepted:end_source no
sources yes command_accepted:occurrence no
sources yes command_accepted:schedule no
hooks-due no command_accepted:background_notice yes
hooks-due no command_accepted:compact yes
hooks-due no command_accepted:invoke_skill yes
hooks-due no command_accepted:user_message yes
hooks-due no effect_finished yes
hooks-due yes command_accepted:interrupt no
hooks-due yes effect_cancelled no
hooks-due yes effect_finished no
compact-gate none effect_finished blocked
compact-gate none effect_finished checked
classifying no action_classified yes
classifying yes effect_finished no
switch-checked no effect_finished yes
switch-checked yes effect_finished no
compact-requested no command_accepted:compact yes
compact-requested yes command_accepted:compact no
compact-requested yes command_accepted:user_message no
compact-requested yes effect_finished no
compact-requested yes effect_reconciled no
compact-requested yes run_finished no

The rules between the facts hold for different reasons, so they are listed apart. Each has an ID the tests name; rules() in core/loop/lifecycle_test.go states each as code.

These hold in every state Step reaches from any log it accepts. A log that would break one is refused, or can’t be written: no event leads there.

  • I1. An ending is held or asked only in a run, or for an interrupt that ended a run before it started (an interrupt applies while hooks admit a run, so it can ask for an interrupted ending of run 0).
  • I2. Stop hooks hold an ending only in an active run.
  • I3. An interrupt is under way only in a run, or with the ending it asked for.
  • I4. A blocked run is active and asks the model nothing more: the turn’s other calls still run, and the run ends as blocked once they have results.
  • I5. A wrap-up is due or sent only in an active run.
  • I6. Model and tool calls are pending only in an active run.
  • I7. Hooks gate only a run that is admitting or active.
  • I8. A skill materializes only in an active run.
  • I9. A release runs only between runs, for materialized files the session owns.
  • I10. A rewind or file restore runs only in an idle session that isn’t moving: no run, no pending effect, not suspended, and no move from its preparation to its handoff. A move doesn’t start while either is under way, since both write files at the root the move is leaving.
  • I11. A tool’s move happens in its run; a host’s committed move waits for no run and no running effect.
  • I12. A deleted session has no run, and an unstarted one nothing at all.
  • I13. The fields that live for a run are zero between runs (see lifetimes).
  • I14. A pending effect is asked for pending, starts (pending or retryable to running or waiting) only by its own EffectStarted, and closes a running attempt (to retryable) only by its own AttemptEnded. It may leave the pending effects by its terminal event, or be dropped when its run or timer goes.
  • I15. A request waiting for its answer ends by its answer, a cancel or its expiry. Step refuses an attempt end for it: nothing of it is in flight to fail or retry.
  • I16. Only a tool call ends as reconciled, with its outcome unknown (R4). Every other effect found lost is retried, so Step refuses an EffectReconciled for one: settling a Stop hook, a model call or a classification that way would end the run with an error where recovery would have tried again.

These hold for every log a driver that follows the driver contract writes, at its append boundaries. Step accepts logs that break them, so a host’s own driver could.

  • D1. No timer’s attempt is open or retryable at an append boundary: the driver appends a timer’s start, registration and finish as one batch (T3). Recovery finishes a timer a driver left open anyway (R4).
  • D2. Between a move’s preparation and its handoff no effect starts but the moving call’s PostToolUse hooks (transitions).
  • D3. The records a fold owes are appended in the batch that owes them, so none is owed at an append boundary.

The contracts promise more than either list above: an effect’s attempts and its single terminal event (E2, E3), the interrupt’s ending (S2), recovery’s termination first (R3) and the move’s phases (transitions). Where a promise rests on a rule above, the rule names it.

Before an event takes effect, its fold checks that the session’s lifecycle allows it. Each such check is a named rule (core/loop/admission.go), called where the handler made the check, so whether a malformed event or a disallowed state is refused first is unchanged. A rule that doesn’t hold refuses the event with loop.ErrInconsistent, except the two that ignore it: an interrupt while nothing runs, and an answer to a request already settled, are accepted, recorded and do nothing. Some rules apply to only some events of their kind, chosen by the event’s own fields; the matrix says which.

The checks of an event’s own fields, of the effect or attempt it names, and of the configuration are not admission rules: they say whether the event is well formed or follows from the log, whatever the lifecycle. core/loop/testdata/guards.txt classes every refusal of the reducer this way.

Rule Requires If not
commands-after-restore no file restore is under way refused: close interrupted file restore before accepting commands
commands-after-rewind no rewind is under way refused: close the rewind under way before accepting commands
config-not-moving no workspace move is under way refused: configuration changed during a workspace move
config-idle no effect is pending and no ending asked for refused: configuration changed during a run
delete-idle no run, pending effect, asked ending or queued input refused: cannot delete an active session
delete-no-processes no background process runs refused: cannot delete a session with background processes
requirements-idle no run, pending effect, asked ending, suspension or queued input refused: requirement changes require an idle session
continue-finished the child’s last run ended and nothing else is under way refused: child is not finished
interrupt-busy an effect is pending or an ending asked for folded, does nothing
answer-open the request it answers is still waiting folded, does nothing
start-not-interrupted no interrupt is under way refused: effect starts after the run was interrupted
run-ending the run asked for its ending and nothing is pending refused: run finished while it wasn’t ending
refusal-alone nothing but the refused model call is pending refused: refused reservation while other work is pending
resume-suspended the session is suspended refused: resumed a session that wasn’t suspended
materialize-in-run a run is active, not interrupted and not ending refused: materialize requires a recorded bundle in an active run
release-idle ownership is tracked, and no run, ending, pending effect or queued input refused: cleanup requires idle authoritative ownership
release-no-processes no background process runs refused: cleanup while a background process runs
release-no-children no subagent is unfinished refused: cleanup while a child runs
rewind-idle idle, with no queued input or request, and no rewind or file restore under way refused: rewind requires an idle session
rewind-after-restore the rewind’s own file restore, if it has one, has finished refused: rewind finished before its file restore
restore-idle no run, suspension, pending effect or file restore under way refused: file restore requires an idle session
restore-not-moving no workspace move is under way, from its preparation to its handoff refused: files restored during workspace move
checkpoint-drop-idle no run is active refused: checkpoints dropped during a run
one-move no other move is prepared and not yet committed refused: move is already prepared
move-quiet the session is idle, or for a tool’s move its run waits on nothing but the handoff refused: move commits only once the session is idle
Event Rules
action_classified none
attempt_ended none
attempt_recorded none
background_requested none
child_continued none
child_finished none
child_started none
command_accepted commands-after-restore, commands-after-rewind
completion_checked none
config_changed config-not-moving, config-idle
continuation_used none
cwd_changed none
effect_cancelled none
effect_finished none
effect_reconciled none
effect_started start-not-interrupted (except a materialization cleanup between runs)
evidence_recorded none
execution_traced none
file_changed none
file_observed none
file_restore_applied none
file_restore_finished none
file_restore_requested restore-idle, restore-not-moving
file_versions_dropped checkpoint-drop-idle (only a drop of every checkpoint)
file_versions_saved none
hook_decided none
instructions_loaded none
materialization_release_requested release-idle, release-no-processes, release-no-children
materialization_released none
materialize_requested materialize-in-run
memory_loaded none
permission_decided none
policy_amended none
policy_decided none
process_ended none
process_started none
quota_exceeded none
record_emitted none
report_recorded none
requirement_declared none
requirement_retired none
resumed resume-suspended
rewind_finished rewind-after-restore
rewind_requested rewind-idle, restore-not-moving
run_finished run-ending, refusal-alone (only a run ended by a refused reservation)
run_started none
session_forked none
session_started none
shortcut_flagged none
skill_loaded none
skill_materialized none
skills_indexed none
steer_outcome none
steer_withdrawn none
steers_taken none
structured_output_accepted none
structured_output_invalidated none
structured_output_rejected none
suspended none
timer_fired none
timer_set none
todos_updated none
turn_diff none
wake_withdrawn none
warning none
workspace_baseline none
workspace_fingerprinted none
workspace_move_committed move-quiet
workspace_move_handed_off none
workspace_move_prepared one-move
workspace_moved none
command answer_input answer-open (ignored otherwise)
command background_calls none
command background_notice none
command clear_goal none
command compact none
command continue_child continue-finished
command create_source none
command declare_requirement requirements-idle
command delete_session delete-idle, delete-no-processes
command end_source none
command host_notice none
command inject_context none
command interrupt interrupt-busy (ignored otherwise)
command invoke_skill none
command occurrence none
command retire_requirement requirements-idle
command retry_denied none
command schedule none
command set_goal none
command set_model none
command set_output none
command set_permission_mode none
command set_policy none
command set_rules none
command set_tags none
command set_title none
command set_verification_work requirements-idle
command steer none
command user_message none
command withdraw_steer none

Every field of the folded state has a lifetime (core/loop/lifetime_test.go), which a field added without one fails:

  • session: what the session said, decided, spent and owns, kept for its whole life;
  • run: zero between runs (I13), such as the run’s ID, its turns, its ending and its gates;
  • batch: the records a fold owes, gone once the driver appends them (D3);
  • until a named event: values that outlive the run that set them, such as queued messages (until the run that takes them starts), pending effects (timers, cleanup and requests end on their own), a requested compaction (until the model call that makes it) and a suspension (until Resumed).

This is separate from the rewind classification, which says what a rewind takes back.

The state is stored as the lifecycle, the configuration, the sequence bookkeeping, the conversation and the session’s own record, plus parts that each group the fields of one concern: Hooks, Compaction, Materialization, Background, Checkpoints, Approvals, Completion, Worktree, Turn and Settings, with the Ledger beside them. Each part has its own clone, which State.Clone calls. A JSON checkpoint of the state carries the number of this layout, checkpoint_shape, raised whenever a field moves, is renamed or changes type; a checkpoint of another layout, or of a session of another format generation, is refused with ErrIncompatible rather than decoded with the moved fields empty or folded onto, and the session is folded again from its log.

Of the lifecycle’s facts, the ending, the interrupt and the block are each stored as one value: an ending held by Stop hooks and one asked of the driver never coexist, so Ending is one of the two or none, its JSON naming its kind; an interrupt’s reason and outcome are set and cleared together; a blocked run’s questions exist only with its block. The others stay as they were. A run is its RunID, which only the run’s start and finish set; admitting isn’t stored, since it is exactly the hooks due or running before a run starts, and storing it would repeat them. A move is one Move whose Committed flag the driver reads. A rewind and a file restore keep their results once finished, for their accessors, so their state is whether each has finished.

The table lists every field: the part that holds it, how long its value lives (Lifetimes above), what a rewind does with it (rewind), and whether Clone copies it. A field holding maps, slices or pointers is copied, or says why sharing it is safe; TestCloneIsolatesEveryField changes each copied field’s containers in place after a clone and requires the clone to be as it was.

Part Field Lives Rewind Clone
— Interaction the run run value
— FileRestore the session session copied
— Tags the session session copied
— Deleted the session session value
— Move the session session shared: a move’s phase changes by replacing the Move
— Decisions the batch run copied
— Instructions the session conversation copied
— SkillIndex the session session copied
— Memory the session session value
— Notes the session conversation copied
— Title the session session value
— Blocked the run run copied
— Suspended until Resumed run value
— SuspendedAt until the next Suspended run shared: a time’s location is never changed
— Records the session session value
— RunRecords the run run value
— Trace the session session copied
— Todos the session conversation copied
— Plan the session conversation copied
— Skills the session conversation copied
— SkillText the session conversation copied
— Last the session session copied
— Commands the session session copied
— RunID the run run value
— Session the session session value
— Seq the session session value
— Config the session session copied
— History the session conversation copied
— Rewind the session session copied
— Pending until the effect’s terminal event: timers, cleanup and requests outlive a run run copied
— Interrupt the run run copied
— Ending the run run copied
— Usage the session session shared: the run’s usage is replaced by a sum that carries no breakdown
— Ledger the session session copied
Approvals Requests the session session copied
Approvals Settled the session session copied
Approvals Classified the session session copied
Approvals Classifying the run run copied
Approvals Breaker the session session copied
Approvals ReviewRevision the session reset value
Approvals Denials the session conversation copied
Approvals ReviewTrail the session conversation copied
Approvals ReviewNotes the session conversation copied
Approvals Granted the session session copied
Approvals PolicyRevision the session session value
Approvals TabSites the session session copied
Approvals Answers the session conversation copied
Approvals Said the session conversation copied
Background Processes the session session copied
Background Subagents the session session copied
Background Sources the session session copied
Background Timers the session session copied
Background Notices the run run copied
Background Carries until the ending of the open provider attempt run copied
Background Seen the session session copied
Checkpoints CheckpointVersions the session session copied
Checkpoints CheckpointPasses the session session copied
Checkpoints StartingPoint the session session value
Checkpoints RunPoint the run run value
Checkpoints CheckpointCalls the run run copied
Checkpoints Points the session conversation copied
Compaction Measured the session reset copied
Compaction LastInput the session session value
Compaction Uncached the session session value
Compaction Cleared the session session value
Compaction Requested until the next model call run value
Compaction RequestedRevision until the next model call run value
Compaction ContextRevision the session reset value
Compaction Compacted until the next model turn run value
Compaction Compactions the session session value
Compaction SummaryUsers the session conversation copied
Compaction SummaryOmitted the session conversation value
Compaction Native the session conversation copied
Compaction CompactRequest until the model call that makes the compaction run shared: a compaction request is replaced, never changed
Compaction CompactOnly the run run value
Completion Goal the session conversation value
Completion GoalPending until a model turn that sees the goal change run value
Completion Requirements the session conversation copied
Completion VerificationWork the session conversation copied
Completion VerificationWorks the session conversation copied
Completion RequirementEvidence the session conversation copied
Completion ScopedEvidence the session session copied
Completion Evidence the session session copied
Completion EvidenceRuns the session session copied
Completion PassedRuns the session session copied
Completion Tainted the session session copied
Completion Continuations the run run value
Completion Constrained the run run value
Completion ContinuedAt the run run value
Completion Failures the run run copied
Completion Stuck the run run value
Completion Flags the run run copied
Completion Truncations the run run value
Completion TaskAttempts the run run copied
Completion Cut until the next model turn run value
Completion Claim the run run copied
Completion Reported the run run value
Completion LastReport the session session copied
Completion Output the session session copied
Completion OutputFailures the run run value
Completion OutputExhausted the run run value
Completion OutputRejected the run run value
Completion OutputErrors the run run copied
Completion NextOutput the session session copied
Completion RunOutput the run run copied
Hooks HookRuns until the decision of the gate the hooks run for run copied
Hooks Due the run run copied
Hooks StopBlocks the run run value
Hooks CompactGate the run run value
Hooks BatchHooked the run run value
Hooks SwitchChecked until the model call that applies the checked switch run value
Hooks PromptsBlocked the run run copied
Materialization Releasing the session session copied
Materialization MaterializationInputs the session session copied
Materialization MaterializationTracking the session session value
Materialization Materialized the session session copied
Materialization Materializing the run run copied
Settings ModelChanges until the model call that applies them run copied
Settings ModeChanges the run run copied
Settings RuleChanges the run run copied
Settings PolicyChanges the run run copied
Settings PlanFrom the session session value
Settings ModelMode the session conversation value
Settings ContextMode the session conversation value
Turn Calls the run run copied
Turn Queued until the start of the run that takes them run copied
Turn QueuedBy until the start of the run that takes the queued items run copied
Turn WakeTriggers until the run ending or withdrawal of all wakes before the run begins run copied
Turn Steering until the start of the run its steering messages wait for run value
Turn SteerOwed until the record of the steer’s outcome run copied
Turn Advance the run run value
Turn Moving the run run copied
Turn Turns the run run value
Turn Text the session session value
Turn ActiveSkill the run run value
Turn QueuedSkill the run run value
Worktree Cwd the session session value
Worktree Workspace the session session shared: the workspace baseline is replaced, never changed
Worktree Files the session reset copied
Worktree RecentFiles the session conversation copied
Worktree Changes the run run copied
Worktree Generation the session reset value
  • testStep, which the reducer’s tests fold through, checks the enforced rules and the listed changes after every event; a state a test built by hand that breaks a rule already isn’t blamed on the fold.
  • The seeded walks (walk_test.go) propose events as a driver would and check every rule and listed change, the driver’s rules at each append boundary.
  • TestCensus folds every fixture and walk, checks the rules (the driver’s for the logs a driver wrote), and writes core/loop/testdata/census.txt: each fact’s states, its changes by event, the combinations of the control facts, and the refusals. Every state, named overlap and listed change of the model must be witnessed by the corpus or carry a written reason it can’t be.
  • TestStepAndReplayAgree (integration tag) folds the same corpus with Step and with Replay and compares their states and outputs after every append. Nothing compares the reducer with an earlier commit: a change of behaviour lands with its own test, and the corpus is recaptured.

A run triggered only by withdrawn wakes can remain parked while the session has no writer. On the next execution the driver records an interruption before dispatch. An inbox poke can start that execution. A suspended run still waits for its recorded input. See process stops.