05 | Snapshots, Recovery and Patch Verification
Sunil Tiwari · Oct 29, 2026 · 13 min read
Expanded from Part 5 of OpenCode Architecture by Vinoth Govindarajan, The Agent Stack.
Vinoth's post, "OpenCode Architecture - Part 5: Snapshots, Recovery, and Patch Verification" (19 October 2026), closes the series with a question most agent demos skip: when an agent stops in the middle of a task, what state did it leave behind, and which of those changes can recovery actually undo? He answers it with an experiment, not opinion. He runs the real OpenCode v1.18.30 binary on Windows x64 with a scripted provider (no model, no API calls), pauses a tool at a precise moment, and then stops the work three different ways.
This note walks through his five sections in order, adds the matching OpenCode source (the processor, revert and snapshot modules at the commit he pinned), and draws each mechanism.
Architecture map canvas: the canvas in this project (Canvas or Split view), the whole mechanism on one board.

The picture to keep in mind: recovery has two inputs. The shadow git repository holds old file contents. The patch parts in the session say which files belong to which step. Revert uses both, and they are written at different moments.
One request, three files
The task is deliberately small. A function in labels.mjs should trim leading and trailing spaces from a label but keep the spaces inside it. A Node script then uses that function to generate catalogue.json, and writes catalogue.manifest.json holding the SHA-256 of the JSON bytes. OpenCode edits the source, then runs the generator through its shell tool (the built-in bash tool, which on Windows runs PowerShell).
The interesting part is the pause. The lab's helper writes the catalogue, signals readiness outside the snapshot workspace, and then waits before writing the manifest. The harness then independently confirms three things: the catalogue changed on disk, the stored tool record says running, and the old catalogue bytes really exist in a saved git tree. At that instant:
| File | State at the pause |
labels.mjs | Fixed (the edit step already finished) |
catalogue.json | New, correct values |
catalogue.manifest.json | Old checksum, now stale |
Vinoth is careful to say this timing was engineered for the experiment. It is not evidence that agents often stop exactly here. The point is that a multi-file write with a derived file (a checksum, a lockfile, a generated index) always has a window where the files disagree, and any interruption inside that window leaves a state that is neither "before" nor "after".
Why this is a good test: a single-file edit can only be old or new. Two files written in sequence can be in a third state. A checksum file makes that third state detectable, because the six acceptance tests include one that compares the manifest with the actual catalogue bytes.

A small detail from the source explains why the source fix is safe in every case: SessionProcessor.create calls snapshot.track() before the LLM stream starts. The comment in processor.ts says the AI SDK may run tools before it emits the start-step event, so capturing inside the event handler "can be too late". When step-finish arrives, the processor calls snapshot.patch(ctx.snapshot) and stores a patch part listing the changed files. That is what makes the first step (the edit) durable before the generator even starts.
The same unfinished work, three outcomes
From the identical paused state, Vinoth releases the work three ways.
- Normal completion. The helper is released, writes the matching manifest, the step finishes and all six of six acceptance tests pass.
- Graceful abort. The harness calls OpenCode's session cancellation route (
POST /session/:id/abort). After a restart, reverting the session's message restores the exact baseline, because the recovery records list bothlabels.mjsandcatalogue.json. "Graceful" means OpenCode's own application-level abort, not a gentle signal to the child process. - Forced stop. The harness kills the OpenCode server process instead of calling the API. The generator survives the first observation window and the harness stops it separately. After restart the records select only
labels.mjs.
His companion lab README gives the full grid, reproduced here:
| Observation | Normal release | Graceful abort | Active server stop |
| Bytes before intervention | Fixed source, new catalogue, old manifest | Same | Same |
| Catalogue path recorded before intervention | No | No | No |
| Old catalogue bytes found in a snapshot before intervention | Yes | Yes | Yes |
| Task acceptance before first revert | 6 pass | 5 pass, manifest mismatch | 5 pass, manifest mismatch |
| Catalogue path recorded after intervention/restart | Yes | Yes | No |
| First revert restores exact baseline | Yes | Yes | No, catalogue stays new |
| Unrevert restores pre-undo bytes | Yes, complete task | Yes, incomplete task | Yes, incomplete task |
The code explains the middle column. In processor.ts, the stream is wrapped in Effect.onInterrupt(...) and Effect.ensuring(cleanup()). The abort route calls SessionPrompt.cancel, which calls SessionRunState.cancel, which interrupts the running fiber. The interrupt triggers cleanup(), and cleanup() does three things that matter here:
- if a step snapshot is still open (
ctx.snapshot), it callssnapshot.patchand saves a patch part.patchrunsgit addon the worktree first, so it picks up whatever is on disk now, includingcatalogue.json; - it waits up to 250 ms for each in-flight tool call to settle;
- it rewrites any tool part still open to
status: "error",error: "Tool execution aborted",metadata.interrupted: true.
A killed server runs none of that. There is no finally for kill -9 (or taskkill /F). So the only patch part on record is the one saved at the end of the edit step.

A common misconception is that the graceful abort is "safe" and the forced stop is "unsafe". Both left the same partial bytes on disk. The difference is only in what was written down about them before the process went away.
Why the snapshot was not enough
This is the heart of the post. Revert needs two separate inputs:
- a snapshot: a saved git tree with earlier file contents;
- a patch record: a list of files that belong to a step, plus the hash of the tree to restore them from. Vinoth stresses it is not a set of reverse edits.
They are written at different times. The snapshot is taken at the start of a step. The patch record is written at step-finish, or in cleanup() when a request is finalised. A file can change on disk between those two moments and never be recorded. In both interrupted cases the old catalogue.json existed in a snapshot. Only the forced-stop revert skipped it, because no record selected it.
Here is how that works in snapshot/index.ts:
- Where snapshots live. A separate git directory per project and worktree:
<data>/snapshot/<project id>/<hash of worktree>, driven with--git-dirand--work-tree, so your real.gitis never touched. It is configured for large trees (feature.manyFiles,index.version 4, untracked cache) and garbage collected withgc --prune=7.days. track()stages the worktree and runsgit write-tree. The tree hash is the snapshot. No commit is created.patch(hash)stages again and runsgit diff --cached --name-only <hash>, dropping ignored files. It returns{ hash, files }.revert(patches)walks patches in order and, for each file the first time it is seen, runsgit checkout <hash> -- <file>. If the file did not exist in that tree, it deletes it. Adjacent files from the same tree are batched (up to 100, skipping paths that nest inside each other).restore(hash)is the whole-tree version:git read-tree <hash>thengit checkout-index -a -f.
And in session/revert.ts, SessionRevert.revert does this:
// simplified from packages/opencode/src/session/revert.ts
yield* state.assertNotBusy(sessionID) // refuse while a run is active
// collect every "patch" part after the revert point
rev.snapshot = session.revert?.snapshot ?? (yield* snap.track()) // save "now" for unrevert
if (session.revert?.snapshot) yield* snap.restore(session.revert.snapshot)
yield* snap.revert(patches) // only files named in patches
rev.diff = yield* snap.diff(rev.snapshot)Read that last call carefully: revert only touches files named in patch parts. A file written by a step that never reached a patch part is invisible to it, however many old copies sit in the shadow repo.

The dotted edge is the forced-stop case in one line: the bytes were saved, but nothing pointed at them.
A worked way to think about it: the snapshot is a library, the patch record is the checkout slip. Revert only fetches books that have a slip. Vinoth's wording for the lesson is that file writes, snapshot capture, record updates and restoration "happen at separate points", so a request can stop after a write but before the write is recorded.
A stopped request is not necessarily a stopped process
Two observations here, one per stop mode.
Graceful case. The abort route returned true, but an OS-level check still found the Node generator running. It exited a little later on its own. The handler at the pinned commit is literally:
const abort = Effect.fn("SessionHttpApi.abort")(function* (ctx) {
yield* promptSvc.cancel(ctx.params.sessionID)
return true
})true means "the cancel request was accepted", not "every child process is gone". Cancellation interrupts the fiber; the shell tool's process cleanup then runs through the spawner, which on Windows uses taskkill /pid <pid> /T /F and elsewhere signals the process group with SIGTERM, then SIGKILL after a timeout. That takes real time, and the lab measured a window where the API had answered and the generator was still alive. The stored tool part later read error with Tool execution aborted.
Forced case. The server and the PowerShell process it had spawned exited, but the Node grandchild kept running until the harness stopped it (exit code 75, the fixture's own cleanup path). After restart, the stored tool part still said running, even though the generator was dead and the live session status list had no entry for the session. That stale running is exactly what you would expect from the code: the only thing that rewrites open tool parts to error is cleanup(), which a killed server never ran.
Vinoth's rule: check the actual process before acting on files it might still modify. Neither an API acknowledgement nor a stored status is proof.

The misconception to avoid: "status is stored in a database, so it must be true." A database row is only as current as the last process that wrote it.
Restored is not the same as finished
The last section is about reading results honestly.
The original baseline and the forced-stop revert both passed 3 of 6 tests. Same total, different failures:
| State | Source | catalogue.json | Manifest | Tests |
| Original baseline | Buggy | Old (wrong labels) | Matches old JSON | 3 / 6 |
| Forced stop, after revert | Buggy again | New (right labels) | Stale, no longer matches | 3 / 6 |
| Normal completion | Fixed | New | Matches | 6 / 6 |
In the baseline the JSON had the wrong label values but its checksum agreed. After the forced-stop revert the JSON had the intended values, the old checksum no longer agreed, and the source was back to its original wrong behaviour. The six tests cover interior spaces, outer whitespace, all-whitespace input, empty input, intended catalogue values and manifest agreement, so "which three failed" tells you which state you are in. Compare file contents and failing test names, not totals.
Unrevert has its own trap. SessionRevert.unrevert simply calls snap.restore(session.revert.snapshot), the tree captured just before the revert. For the interrupted cases that means the fixed source, the new catalogue and the stale manifest come back exactly. It does not finish the task or rerun the generator. Unrevert is faithful, not helpful.

He mentions one repair he did not perform: keep the fix, confirm the old writer is gone, then regenerate the catalogue and manifest. He is explicit that this does not justify blindly retrying tools. His closing point is that four things must be reconciled before recovery can be trusted: saved contents, recorded restoration scope, live processes, and the actual files.
What to steal
The post does not have a "What I would steal" heading; its takeaway is the five checks before an interrupted request continues. As a checklist for any agent that edits files:
- Writers. Confirm every process that could still change files has actually exited (by PID or handle), not just that an abort call returned.
- Effects. Diff the disk against the last snapshot to find every changed file, including ones no record mentions.
- Coverage. Before reverting, list exactly which paths and which saved versions the revert will restore, and compare with the effects list.
- Target. Decide on purpose: back to baseline, keep partial work, or finish the original request.
- Acceptance. After recovery, check the files and the named checks against that target. Never trust a pass count alone.
- Take the step snapshot before the model stream starts, as OpenCode does, because tools may run before the step event arrives.
- Record changed files in a
finally-style path on abort, and accept that a hard kill skips it. Treat a storedrunningafter restart as "unknown", not "running". - Keep snapshots in a separate git dir (
--git-dir+--work-tree) so recovery never touches the user's own repository history.
Quick self-check
- Why did the graceful abort revert restore
catalogue.jsonwhile the forced stop did not, given the old bytes were saved in both cases? - What does
POST /session/:id/abortreturningtrueactually guarantee? - After a forced stop and restart, the tool part says
running. What should you conclude? - Baseline and forced-stop revert both scored 3/6. Why is that not the same state?
- What does unrevert restore, and what does it not do?
- Why does
SessionProcessor.createcallsnapshot.track()before the stream starts?
Answers:
- Revert only restores files listed in patch parts. On abort,
cleanup()ransnapshot.patch, which staged the disk and recorded the catalogue. A killed server never rancleanup(), so only the edit step's patch (labels.mjs) existed. - Only that the cancel was accepted and the session fiber was interrupted. Child processes may still be running for a while.
- Nothing about the process. The row is stale because the cleanup that marks tool parts as errors never ran. Check the OS.
- Different tests failed: the baseline had wrong labels with a matching checksum; the reverted state had right labels with a stale checksum and buggy source.
- The exact tree captured just before the revert, including partial work. It does not rerun tools or complete the task.
- The AI SDK can execute tools before emitting the step-start event, so a snapshot taken in that handler could already include the tool's writes.
Source and credit
- Vinoth Govindarajan, "OpenCode Architecture - Part 5: Snapshots, Recovery, and Patch Verification", The Agent Stack, 19 October 2026.
- Companion lab (all numbers and the outcome grid above): the-agent-stack-labs / 05-interrupted-recovery.
- Vinoth on LinkedIn: https://www.linkedin.com/in/vinothgovindarajan
- OpenCode source at the commit he pinned (anomalyco/opencode, formerly sst/opencode; identical in release v1.19.5):
- session/processor.ts: initial
track(), step-finish patch,cleanup(), interrupt handling - session/revert.ts:
revert,unrevert,cleanup - snapshot/index.ts:
track,patch,restore,revert,diff - handlers/session.ts: the abort route
- core/src/cross-spawn-spawner.ts: Windows
taskkill /T /Fand SIGTERM-then-SIGKILL
- session/processor.ts: initial
- Code note: on OpenCode's
devbranch, revert is being rewritten inpackages/core/src/session/revert.ts(stage/clear/commit, files taken from each assistant message'ssnapshot.startandsnapshot.files, restored withSnapshot.restore({ files })). The two-input idea is the same; the function names will differ from this note once that ships.