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

The loop is a small cycle that repeats until a run is done: decide what to do next, do it, record it. zelk keeps the deciding and the doing in two separate parts, and everything on this page follows from that split. It starts with one turn of the cycle and ends with the states a session moves through.

The reducer, loop.Step, is a pure function. It takes the session’s state and one event from its log and returns the new state. The state includes the effects it wants done next: call the model, run these tools, ask a person. The reducer does no I/O, reads no clock and draws no random number, so the same log always folds to the same state and the same pending effects.

The driver does what the reducer asks. For each effect it checks that it still holds the lease, appends that the effect started, does it, and appends how it ended, with the results, in one batch. Then it folds what it appended and looks at the new state. It keeps nothing of its own: whatever it does next follows from the log.

The split pays off three ways.

  • Replay. Folding a log needs no model, no tools and no workspace, so a session can be read back, inspected or rewound exactly as it happened.
  • Recovery. The state lists every effect that started and hasn’t ended. After a crash, that list is all the next process has to settle (Durability and recovery).
  • Testing. The reducer takes events and returns state, so its rules are tested without a provider or a disk. The driver is tested with fakes standing in for both.

A host that needs another way to run effects can write its own driver from the public pieces and keep the same guarantees.

A turn is one model call and the tool calls it asks for.

  1. The request. The reducer builds the model’s request from the session as folded: the system prompt and tools first, then project context, then the conversation, with any messages that arrived since the last call (Context and compaction).
  2. The model call. The reducer asks for it. The driver records that the attempt started, calls the provider, and records the reply and its usage. A call that fails in a way worth retrying is tried again, each attempt recorded.
  3. The decisions. If the reply asks for tool calls, the reducer decides each one in the order the model gave them, against the permission rules: allowed, sent to a person, or refused. A refused call gets a result that says so, and the model reads it on its next call (Permissions).
  4. The waves. The reducer asks for the allowed calls in waves. A run of read-only calls shares one wave. Any other call waits for the calls before it and runs alone, and so does a call that needs a person’s approval. The driver records the intent of a whole wave together, runs its calls in parallel (up to ten at a time), and records each result as that call ends.
  5. The next call. Once every call has its result, the results join the conversation in the order the model asked for them, whichever ended first, and the reducer asks for the next model call. When a reply asks for no tool, the turn ends and the run goes to its ending (Runs and completion).

A call counts as read-only only if its tool says so and zelk trusts the claim, which it does for built-in tools. A tool from an MCP server or a client never counts, and neither does a shell command: they run alone. A write never races a read of the same file, and a crash while a slow call runs loses none of the calls that finished beside it, because each result is recorded as it ends.

  • A turn with tool calls: The fold decides the waves; the driver runs them. Read and Grep are read-only, so the fold asks for both at once and they run in parallel. Edit changes files, so it waits for them and runs alone. Grep ends first, but the results reach the model in the order it asked for the calls.
  • A steer mid-run: A steer joins the run’s next model call. It arrives while the command runs and changes nothing yet: the fold only holds it. When the results are in, the next model call takes it, and the run can’t end before a model call has.
  • A mode change waits: A mode change waits until no tool call is running. The first edit was decided and started under accept-edits, so it finishes under it. Once it has ended the fold applies plan mode, and the second edit, not yet decided, is decided under plan mode: refused.

A session isn’t in one state. The reducer keeps a few independent facts, and a session is in one state of each at once:

  • the session: unstarted, started or deleted;
  • the run: none, admitting or active (admitting while hooks decide whether a prompt may start a run);
  • the ending: none, held while Stop hooks decide, or asked of the driver;
  • the interrupt: none, or under way;
  • the suspension: whether the host let go of the process while a request waits;
  • each pending effect: pending, running, waiting or retryable.

The contract lists more, such as the budget and background work. A fact changes only when an event folds, and the contract lists every change each one may make.

  • Each fact is a rail of states with one bead on the state the session is in:Session (unstarted, started, deleted); Run (none, admitting, active); Ending (none, held, asked); Interrupt (none, interrupting); Suspension (no, yes); Effect (pending, running, waiting, retryable).
  • A run: A run is a stretch of work between two idle moments. One message starts it, effects come and go while it is active, and the ending is asked for and then recorded.
  • Hooks first: A run can be admitting before it is active. While hooks decide whether the prompt may start a run, it is admitting. A hook that blocks it ends the run before the model is called.
  • An interrupt: An interrupt asks for an ending, and the run settles first. The effect in flight is ended, never abandoned, so the log shows how each effect ended before the run does.
  • Waiting for a person: Waiting for a person is a state of an effect, not of the run. The run stays active. Suspension is a separate fact: an answer can arrive without resuming the session, and a session can suspend with the request still waiting.
Each rail is one fact and its states; the bead is where the session is. An event slides the beads of the facts it changes and leaves the others still, so a session can be in any combination the facts allow.

In plain words, the states that matter for what a session accepts:

The session is It accepts It refuses or ignores
idle a message, which starts a run; a compaction; a rewind; deleting it an interrupt, which is recorded and does nothing; a steer, which is rejected because no run is going
active a steer; a message, which waits for the next model call; a permission-mode, rule or model change, which waits its turn; an interrupt; an answer deleting it, rewinding it, restoring files and changing its configuration, until the run ends
ending an interrupt a steer, which is rejected because the run is ending
interrupting the ends of the effects in flight any new effect
waiting for a person, inside an active run an answer, or the request’s expiry, whichever folds first a second answer to the same request, which is recorded and changes nothing
suspended a resume, which the next execution records before anything else a resume of a session that isn’t suspended

A rule that refuses an event refuses it with loop.ErrInconsistent. Two ignore it instead: an interrupt while nothing runs, and an answer to a request already settled. The contract names every rule.

Everything that comes from outside the loop is a command. It is appended to the log first, and the reducer folds it like any other event.

A message while idle starts a run. The reducer queues the message and asks for a model call.

A message mid-run is a steer. In the terminal UI, what you send while a run goes is a steer, and it shows as queued. In Go it is Execution.Steer. The reducer holds it, and the next model call takes it. The run can’t end before a model call has seen it. A steer can be taken back with Execution.Withdraw until a call takes it. If no run is going when it is sent, Go returns ErrNoRun and the terminal UI sends the text as a message instead. If the run ends before the steer folds, the steer is rejected, and the log says so. A plain UserMessage sent mid-run isn’t a steer: it waits in the queue, joins the next model call, and doesn’t hold the run open. If the run ends first, it starts the next one.

An interrupt is Esc in the terminal UI and Execution.Interrupt in Go. The reducer asks for an interrupted ending at once and drops the effects that never started. The driver cancels the ones in flight and records how each ended. Then the run ends interrupted. An interrupt tells you only that the intent is in the log; the effects may still be winding down.

A mode change is Shift+Tab in the terminal UI and SetPermissionMode in Go. It is accepted at once and applied once no tool call is running or waiting on a hook, so every call is decided and run under one policy. Calls of the turn not yet decided are decided under the new mode.

Budgets, deadlines and waiting for a person

Section titled “Budgets, deadlines and waiting for a person”

Limits are part of the folded state, so they hold across crashes. A turn limit (WithMaxTurns, -max-turns) ends it max_turns. A token budget (WithBudget) charges every attempt: a hard limit refuses a call that would cross it, and a soft limit allows one wrap-up turn. Either ends the run max_budget. A wall-time limit (WithMaxTime) is a deadline fixed when the run starts, so the time a crashed process spent down counts. See Limits for how to set them.

An approval or a question is an effect like any other. The reducer asks for it and the driver records that it went out, and then it waits: nothing is in flight and no goroutine is busy. The host answers while the process runs, or lets the session suspend and answers later. A host that can’t ask has the call refused, or the run ends blocked. Approvals covers the three ways.

A subagent is a child session that a tool call starts. The parent’s Task call reserves the child’s share of the budget and records the child’s start with its result. The child then runs its own loop over its own log, and the parent records how it ended when a call collects it. The parent’s log holds the call and those two records that link the logs, not the child’s events. Subagents follows a child from the call to its report, and Extending zelk shows where a subagent, a hook and an MCP server plug into the loop.