Write-ahead logging for long-running agents
What an agent runtime should persist before it acknowledges a turn, edits files or dispatches a background job, and how recovery compares that intent with what actually happened.

On this page
Send a prompt to a coding agent and the endpoint returns success immediately, while the turn it started may run for twenty minutes across many model and tool calls inside a process that may restart at any point. Running a long-lived workflow inside an ordinary request handler works fine right up until a deploy lands mid-turn. Saving the transcript only at completion discards the work, and re-running the turn blindly repeats side effects or overwrites files someone has since changed.
Write-ahead logging handles this with one adjustment for agents: record intent durably before acknowledging or acting, then recover by comparing that intent against the live transcript, filesystem, or machine instead of replaying the log. I applied this across two runtimes. One is a Rust agent SDK I wrote for Ferrite, my application platform, with its loop and recovery classification adapted from pi, an open-source agent toolkit, and Flue, a TypeScript agent framework built on it. The other is the TypeScript runtime for a production coding agent that edits a workspace and runs commands in E2B hosted sandboxes.
Acknowledging a turn
Before acknowledging a turn, the Ferrite SDK writes a submission record keyed by the client’s idempotency key, holding a request hash, a pending status, and the request itself. A reused key with a different hash fails as a conflict; a settled one returns its stored outcome without calling the model again. As the loop runs, each message commits on completion, guarded by a compare-and-swap on the conversation’s head. A storage failure stops the loop so memory never advances past durable state.
Suppose the process crashes right after persisting a file-write tool call, before its result is recorded. Model providers reject unpaired tool calls, and nothing in storage can establish whether the write reached disk, so recovery reads the transcript and appends a synthetic error result noting the interruption. Pending turns then re-run on start-up against that repaired history, up to ten attempts. Because recovery is at-least-once and stops at the runtime’s own store, a tool that sends email still needs an idempotency key at the far end, since no local log can unsend it.
Editing a batch of files
A crash partway through a multi-file edit in the coding agent leaves the workspace half-applied. Before any write hits disk, the file writer preflights a batch of up to 16 paths and flushes a receipt storing each path’s expected preimage digest and intended postimage digest. If that flush fails, nothing is written. Each file is written to a staging file in the same directory and replaced via POSIX rename, giving per-file atomicity, not a multi-file transaction.
If the process dies after the first file is renamed into place, before the batch is marked committed or the second file is touched, recovery hashes the live files and compares them against the recorded digests. The first file already matches its intended digest and is marked done without rewriting; the second still holds its expected bytes and is written once. Any file matching neither digest fails as a conflict and stays untouched, never auto-repaired: from a digest alone, a colleague’s intervening fix and corruption look the same.
Dispatching background jobs
Long builds and test runs in the coding agent’s E2B sandboxes execute asynchronously so the model does not burn turns polling for exit status. Before dispatching a background command, the runtime persists the owning turn ID and the sandbox incarnation, so a late result from a replaced machine cannot pass as current.
Completions arrive at least once under a stable ID and stay pending until acknowledged, while the turn stays open as long as results are owed. Acknowledging a notice twice is a no-op, so redelivery after a crash is harmless. In a scripted integration test, the model launches the job and stops after two calls; the third fires only when the completion notice arrives. On restart, recovery attaches only to the recorded sandbox incarnation to inspect the job and emit a single terminal notice. If that machine is gone, it preserves the pending turn record and fails closed instead of provisioning a replacement and silently abandoning the original command. Across all three boundaries, crash recovery is exercised in restart tests against persisted state rather than by killing live processes.
Appending mutations instead of files
Write-ahead persistence only stays practical if flushing every step is cheap. In the coding agent runtime, the session journal had been a file on a versioned network filesystem that stored a full copy of the file on every append. On one long turn, that write amplification burned about a gigabyte a minute just to record incremental progress. The journal is now an append-only mutation list in SQLite, tying flush cost to the size of each change, not the size of the history. On restart, those mutations replay through the same validation path that accepted them live, so recovery cannot reconstruct a state that live execution would have rejected.
Chris Chabot, August 2026