Stay durable without a workflow engine

Three panels. A wizard gets the answer from @todoist and the laptop marks the run done. Then a messenger arrives with a late result and the laptop turns it away: Run’s over. Then the messenger brings the same write again and the laptop refuses: It may already be made. Not twice.

A question asked to @todoist in a thread starts a run. The model reads the thread, searches two channels, and is writing its answer when a deploy replaces the task. The model’s work so far lived in that process. What should happen next?

The textbook answer is a durable workflow engine such as Temporal: record each step, and on another worker, pick the run up where it stopped. Agent Core has no workflow engine and no queue. The run fails, the asker is told, and they ask again.

What the article argues

Anthropic’s How we built our multi-agent research system makes the case for durability. Its agents run for a long time and keep state across many tool calls, so “restarts are expensive and frustrating for users.” Anthropic built systems that resume from where the agent was when the error occurred, added retry logic and regular checkpoints, and ships new versions with rainbow deployments, keeping old and new running while traffic shifts.

The model writes the answer, code delivers it took the article’s split between model and code for delivery.

What the first release decided

A teammate’s target architecture for Agent Core sums itself up as “Pi reasons. Temporal remembers. Gateways enforce.” (Pi is the library that runs our model loop.) In that design, each run is a Temporal workflow that can wait for an approval, survive a worker restart, and resume. It was the target, and when I wrote this, none of the Temporal part was built.

The first-release spec narrowed it. It excludes Temporal and SQS, and “automatic recovery of interrupted reasoning”. Its section on interruption gives the reason in one line: “every V1 run is user-triggered and the user is present.” A research agent that restarts loses its work. A @todoist run answers a question the asker is waiting for, and asking again costs one message.

The repository’s guide for agents turns that into rules. “Preserve honest failure on interrupted reasoning. Do not add automatic reasoning recovery.” “Reject and ignore late model or tool work.” Adding Temporal or SQS needs an approved scope change.

Writes were treated differently from the start. Losing reasoning costs a retry. Doing a write twice, or saying it happened when it didn’t, can’t be undone by asking again.

What MySQL holds instead

Agent Core is one NestJS service with its operational state in MySQL. That state can’t continue a run. It holds enough to end each piece of work honestly:

  • Runs. A run moves to completed, failed or cancelled in one transaction that succeeds in full or changes nothing. A product that sends the same trigger again gets the stored run back, in whatever state it is.
  • Replies. The post ID is saved before the first attempt to post, so a retry can’t post twice.
  • Writes the model makes. Each is recorded as possible before it is sent, and becomes none only when the product proves nothing was sent, and made when it confirms the change.
  • Approved writes. The proposal is stored with its arguments and an ID chosen at proposal time. The approval runs it once.
MySQL holds run status written in one transaction, a service lease with a sweep of interrupted work, reply IDs and write records, and stored approvals. A workflow engine would add resuming a run mid-reasoning, a run that waits for an approval, and timers.

A finished run stays finished

When a run ends, by completion, a cancel or the deadline, its process may still have model and tool calls in flight. They must not change the result. A teammate wrote the service test for this. Its fake model returns a completed run, then a moment later sends one more text chunk and calls a tool (trimmed, from the test, not a run):

await options.on_event?.({ type: 'assistant.delta', text: 'Visible before completion.' })
setTimeout(async () => {
    await options.on_event?.({ type: 'assistant.delta', text: ' Forbidden late delta.' })
    await options.tools[0].execute({}, { tool_call_id: 'late-tool-call', ... })
}, 10)
return completedPiRunResult(options.run_id, 'The stored final output.')

The assertions:

expect(response.text).toContain('Visible before completion.')
expect(response.text).not.toContain('Forbidden late delta.')
expect(lateToolRejected).toBe(true)
expect(readCallCount).toBe(0)        // the product was never called
expect(loaded.body.current_run).toMatchObject({
    status: 'completed', final_output: 'The stored final output.',
})

Another test races a cancel against a completion being saved: the cancel answers completed. When I moved every tool call into one ToolCallGate, the gate kept that check on both sides of the product call. A tool that returns after the run ended is dropped, not admitted.

When the process goes away

Only one task runs @todoist at a time. It holds a MySQL lock, the run-service lease, on one database connection. Another task asking to start a run gets a retryable 503 service_unavailable. Teammates built the lease and what happens when a task takes it: every run the previous process left created or running becomes failed with run_interrupted, and a reply it left pending becomes uncertain, because the post may have landed.

A deploy is an orderly stop, and the task still holds the asker’s access while it shuts down. I used that to post the failure sentence, as the delivery post shows. A crash or a lost lease still ends silent: the process that held the access is gone.

The lease has its own edge. The database closes a connection left idle long enough, and with it the lease. I made the log say whether that stopped anything. Two of its service tests (trimmed, from the tests):

lease lost, nothing in progress  → warn  { active_runs: 0, pending_replies: 0 }
                                   next run takes the lease again → assistant.completed
lease lost during a model call   → error { active_runs: 1 }
                                   the run ends → run.error { code: "run_interrupted" }

Counting active runs alone would have hidden a real interruption: a reply is written after its run leaves the active set, and losing the lease stops it. I added no keepalive. Every run start checks the lease and takes it again, so an idle loss costs nothing.

One process holding every run also means one bug can end them all. At first, a single MySQL error while adding the 👀 at the start of a run went unhandled. Node exited, and every active run ended as run_interrupted.

A write is never sent twice on a guess

The model proposes, the asker approves, code writes covers approvals: an ID fixed at proposal time, one approval that runs the write, an unclear answer that ends as write_outcome_unknown and is never retried, and a write cut off by a stop that ends as unknown on the next approval.

The writes the model makes during a run follow the same rule, and a bug showed where it leaked. The Todoist tools package retried a write that got a 502, and a gateway can answer 502 after the change was saved. I made it send the write once. What the tool did with one write that got a 502, before and after:

add_tasks, Todoist answers 502
before: 3 POSTs, then "Todoist answered 502."
after:  1 POST, then "The Todoist change may or may not have been saved. Do not try it again."

That result still relied on the model reading “Do not try it again”, so I moved the rule into the tool-call gate: the same write, after a result that may have written, is not sent again in the same run. Move the rule into the tool shows the tests.

What it costs

  1. A deploy fails every run in flight. The model’s reading is thrown away, and the asker has to ask again. Posting the failure sentence on a deploy didn’t cover the questions refused before they start, during the deploy overlap.
  2. One task runs everything. The lease keeps the sweep safe, and it also means no second task shares the load.
  3. Recovery is lazy. The sweep runs when the next run start takes the lease. Until then, an interrupted run still reads running.
  4. Unknown stays unknown. Nothing looks up an uncertain reply. Someone opens the conversation and looks for the post ID.
  5. Products carry the retries. An approval left executing closes only when Comms repeats it. Feedback votes are retried by Comms too. Agent Core starts no background job.
  6. A run can’t wait. A run that needs an approval ends with a proposal, and the model never sees the approved write’s outcome.

When I made approvals run the write inside the request, I wrote down why: a background job would need its own lease, and a way to get the asker’s access after the request ended. That is the less visible cost of durability: work that resumes later needs access that is still valid later, and a run’s access is short-lived and never stored.

When we’d need Temporal

The first-release spec names its own triggers. “Runs must recover automatically or continue without a user present” leads to an outbox and SQS, “then Temporal if workflow complexity warrants it”. Exact recovery of a half-streamed answer leads to a durable event stream and checkpoints.

When I wrote this, I thought the first trigger was getting closer. Agent Core already accepted runs that Automations started with no post asking, such as a daily check-in, and when one is interrupted, nobody is there to ask again. The other features that would want an engine are the ones the list above leaves out: an approval that resumes the run with its outcome, an approval that expires and lets the run go on without it, and a run that survives a deploy.

What I’d tell someone doing the same

  1. Decide what an interrupted run costs the user before choosing durability. If they’re present and the run is short, an honest failure and a retry can be the right answer.
  2. Make the end of a run one transaction, and reject everything after it. Without resume, late output is the bug to guard against.
  3. Spend durability on writes, not reasoning. An ID fixed before the first attempt, a possible record before the send, and “unknown” as a final state.
  4. Enforce “don’t retry” in code. A sentence in the tool result is a request, not a guarantee.
  5. Write down the triggers that would change the decision. Ours are in the spec, and one of them was getting closer when I wrote this.