Pauses and restarts
Why a paid model call is never repeated, whichever worker resumes the ticket.
A harness may pause a turn through the ledger (a budget threshold or a repeated tool call). The engine writes the pause ahead, and resumes the harness from its last token, so a paid model call is never repeated. The same token lets a turn continue after a worker dies. The harness saves it after every model reply and every tool result, so a crash repeats at most the call that was in flight.
The broker
The broker provides the queue, the per-ticket lock, the events across processes and the cancel
signals. The afe-redis plugin implements it on Redis.
- a Stream with a consumer group for the queue.
SET NX PXwith owner-checked renewal for the lock.- pub/sub for the notices and the cancels.
- short-lived keys for the worker heartbeats.
The broker never carries an event's content. A worker records the event in the store and only pings the broker. Every process reads the gap back from the store by sequence number, so no event is lost or duplicated.
State and events
The engine stores the events of a pause or an end before it stores the new state. A reader that
sees paused_human, needs_human, done, failed or cancelled already has the events that led
to it. Poll the state first, then read the events: the state is never ahead of its own history.
When a worker dies
A worker that dies loses its lock (it expires), and its unacked job is redelivered to another worker. That worker continues the ticket from its last checkpoint. A node whose result was written ahead, but whose checkpoint was not saved, is not called again.
A worker that still runs keeps its job: it renews the lock and tells the broker the job is still being worked on. Another worker therefore leaves it alone, however long the ticket takes. A renewal that fails, or a broker that cannot confirm one, counts as a lost lock: the run stops and the job is redelivered.
An error the worker does not recognise (a store, a clone, a sandbox provider) is logged with the ticket id and the error class. That text goes through the same redaction as every other. The job is left unacknowledged and its lock released, so it is redelivered like the job of a dead worker.
A job delivered three times without being finished puts the ticket in needs_human, with the
reason "gave up after 3 attempts" and the retry, take over and close actions. retry queues
it again with the attempt count reset. At startup, every worker also queues a recovery job for
every running ticket that no lock holds. A ticket with no configuration revision, or one started
by an earlier engine, is skipped with a warning in the log that names it.
A broker outage does not stop a worker. Its claim loop, heartbeat and cancel subscription log the
error class once and retry, so the process stays live. /readyz reports broker until Redis is
back. A cancel sent during the outage still stops the ticket at its next node boundary.
Only the current owner writes
Each time a worker takes a ticket, the store gives it a new ownership number. Every write the run makes (state, checkpoint, events, artifacts, pending requests) carries that number, and the store refuses it once a newer owner exists. A worker that was slow, not dead, therefore cannot undo what the new owner did. Its job is left for redelivery and never acknowledged, and the log names the ticket.
A resume belongs to its request
A resume job carries the id of the request it answers. The engine reads the ticket's current
request at execution time, and applies the answer only when the id matches. A stale answer is a
no-op, so a duplicate resume never skips a later pause.
A request that offers actions needs an explicit answer among those offered. A resume with no
answer cannot pass it. Each ticket_resumed event records the request id.
Cancel versus pause
ticket.cancel stops the harness turn in progress at once, on whichever worker runs it, and in
afe run --local. The ticket becomes cancelled, the artifacts and ledger rows written so far
are kept, and one ticket_cancelled is emitted.
The cancel is final. It stores ticket_cancelled, sets the state and takes a new ownership number
in one transaction. A late write from the stopped worker is refused, and a late resume finds a
closed ticket. A second cancel, or a cancel of a ticket that is done or failed, writes nothing.
ticket.pause still stops at the next node boundary. A turn interrupted and later resumed would
otherwise pay its model call twice.
Cleanup after a cancel
A cancel also removes the ticket's sandbox objects and stored workspace bundles. In queue mode the
server publishes a cleanup job, so whichever worker claims it removes them, even if no process
ever opened that workspace. The objects are found by the ticket's stored key (id and uid),
never by a process cache. An object of another ticket with the same visible id is left alone.
A failed removal records a cleanup_failed event with the error class and a redacted message, waits
(1, 2, 4, 8, then 16 seconds) and tries again. After the last attempt the ticket's
terminal marker is left to the sandbox reaper. A successful removal records cleanup_done. The
ticket stays cancelled throughout, and ticket.get reports cleanup: "pending" while the last
cleanup event is cleanup_failed.
Crash recovery
A worker resuming a running ticket continues it from its last checkpoint. The workers also
queue a recovery job for every running ticket no lock holds at startup. A ticket with no
configuration revision, or one started by an earlier engine, is skipped with a warning in the log
that names it. Tickets that wait for a person (paused_budget, paused_human, needs_human)
stay as they are, with their pending request readable.
A turn that crashed with pauses already answered replays them in order, and resumes the harness from the last token. No paid model call is repeated.
A node that finished inside a lane or a map branch is not called again either. The engine names its run after the task's checkpoint namespace, so after a crash it reuses the result written ahead, as at the top level. This holds for an agent, a script, a human node and a plugin node type. A decision already taken is not asked a second time.
afe run --local never recovers other tickets.
Memory held per ticket
The engine keeps a workspace, a key, a compiled graph and a few markers for each ticket it works on. It counts the operations running on a ticket, and when the last one ends it frees every one of those entries. A cancel or a pause that leaves a run still alive frees them only when that run ends. The memory of a long-lived worker follows the tickets in progress, not the tickets it has ever seen.
Troubleshooting
A ticket sits in needs_human with the reason "gave up after 3 attempts".
Three different workers claimed the job and none finished it, usually because the node itself
crashes or hangs. Fix the underlying cause first, then retry to queue it again with a fresh
attempt count, or take_over to run the node by hand.
/readyz reports broker as unhealthy
The broker is unreachable. Workers stay live and keep retrying, so no work is lost, but new
tickets cannot be enqueued and cancels are delayed until Redis answers again. Check Redis itself,
not the worker.