← All Labs Lab 06 / 28 Crash mid-turn, resume cleanly
Lab 06 · Durable agents · Learned

Crash mid-turn, resume cleanly

After SIGKILL mid-turn, reopening habitat.sqlite keeps every committed entry and resume() finishes the turn without re-running a finished tool call.

Learned Needs: tool-calling Smoke: pnpm exec tsx examples/06-durable-crash-resume/run.ts --smoke Run: pnpm exec tsx examples/06-durable-crash-resume/run.ts Source: examples/06-durable-crash-resume

A child worker opens a DurableHabitat, commits one turn, then starts a second turn that runs a write tool and hangs while waiting for the model. The parent kills it with SIGKILL, reopens the same habitat.sqlite, and calls resume(). The lab checks what survived and that the write did not run again.

Run it

zsh
pnpm exec tsx examples/06-durable-crash-resume/run.ts            # narrated, offline faux model
pnpm exec tsx examples/06-durable-crash-resume/run.ts --smoke    # PASS/FAIL lines, exit 0 when all pass
MYCEL_URL=https://mycel.thefocus.ai pnpm exec tsx examples/06-durable-crash-resume/run.ts --live --model mycel:z-ai/glm-5.3-flash

The worker always uses the faux model so the stall point is exact. With --live, only the resumed generation calls the live model. Add --keep to keep the temp root. The parent waits up to 120 s for the worker to reach mid-turn; on a loaded machine, raise it with LAB06_MIDTURN_TIMEOUT_MS.

Walkthrough

  1. Look at the worker's committed entries. The last one is the write tool result.
  2. After SIGKILL, the parent overwrites crash-marker.txt. If the tool ran again, this edit would be lost.
  3. inspect() on the reopened file lists one unfinished task: pi.generation. No tool task is listed.
  4. After resume(), the old entries are marked ✓ and a new assistant answer is marked +.
  5. no-rerun checks there is still exactly one tool result and the marker still holds the parent's edit.

Files

  • run.ts — parent drill and the --worker child process.
  • presentation.html — the Lab 06 deck: the building blocks, how the smoke proves no re-run, the real run output, and what we learned.
  • PROMPT.md — prompt to rebuild this lab.
  • FINDINGS.md — what we learned.

Prompt

Coding-Agent Prompt · Ready to Paste
Build a standalone lab under `examples/06-durable-crash-resume/` that tests one thing: after SIGKILL mid-turn, a SQLite-backed `DurableHabitat` (`@umwelten/habitat` on `@earendil-works/pi-durable`) reopens with every committed entry intact, and `harness.resume()` finishes the turn without re-running a tool call that already committed its result.

Read these files first to match exact types and invariants:
- `docs/specs/orchestration/durable-habitats.md`
- `packages/habitat/src/durable/durable-habitat.ts`
- `packages/habitat/src/durable/durable-habitat.test.ts`

Requirements:
1. One file, `examples/06-durable-crash-resume/run.ts`, runnable via `pnpm exec tsx examples/06-durable-crash-resume/run.ts`. Do not import from other example folders.
2. Default to an offline `fauxProvider` from `@earendil-works/pi-ai` (a self-replenishing `FauxResponseFactory`). Support `--live --model <provider:model>` via `resolvePiModel(parseModelSpecString(spec))` from `@umwelten/pi`. Tool-call turns must pass `{ stopReason: "toolUse" }` to `fauxAssistantMessage`.
3. In a `mkdtemp` root, create `owned-app/`. The script forks itself (`child_process.fork` with `--worker <root>`). The worker opens `DurableHabitat` (`workDir`, `env: "local"`, `ownedRepoRoot`), commits a warm-up turn with `ask()`, then `submit()`s a turn where the faux model calls `write` on `owned-app/crash-marker.txt`. After the tool result, the faux model never answers (return a never-resolving promise) and the worker sends its committed entries to the parent over IPC.
4. The parent sends SIGKILL, waits for the `exit` event (one writer per `habitat.sqlite`), then overwrites `crash-marker.txt` so a re-run of the tool would be visible.
5. The parent reopens the same `workDir`, prints `harness.inspect()` tasks, calls `harness.resume()`, waits for idle, and prints entries marking pre-crash vs new. In `--live` mode, `configure({ model })` the root conversation so the resumed generation uses the live model; the worker always uses faux.
6. Checks, each printed as `PASS <name>: ...` or `FAIL <name>: ...`: sigkill (exit signal is SIGKILL), inspect (only a generation task is unfinished), preserved (every pre-crash entry id and kind still present), resumed (a new assistant entry exists), no-rerun (exactly one tool result, marker still holds the parent's edit).
7. `--smoke` prints only the PASS/FAIL lines and exits 0 only if all pass; without it, narrate each step. `--keep` keeps the temp root.
8. Strict TypeScript, zero `any`. Clean up the temp root and kill a still-running worker in `finally`. Verify with `pnpm exec tsc --noEmit -p examples/tsconfig.json` and `--smoke`.

Also write `README.md` (frontmatter: title, track, status, tests, needs, run, smoke, deck), `PROMPT.md`, and `FINDINGS.md`.
Works in Pi, Claude Code, Codex & Antigravity Offline mock first → --live opt-in

What we learned

The answer

After SIGKILL mid-generation, every committed entry survived. On reopen, inspect() showed one unfinished task, pi.generation, as pending. resume() produced the answer. The write tool call had already committed its result, so it was not in the unfinished list and did not run again: the parent's post-crash edit to crash-marker.txt was still there, and there was still exactly one tool result.

Gotchas

  • One writer per habitat.sqlite. The parent opens the file only after the worker's exit event.
  • inspect() lists only unfinished tasks. Its state type has no terminal kind. A finished tool shows up as absent, not as terminal.
  • The worker must stay alive until killed. A setInterval keeps the event loop busy while the faux model hangs.
  • Live resume needs configure({ model }). The worker stored the faux model on the conversation. To have the live model finish the turn, the parent sets the live model on root before resume().
  • Live models copy the transcript's style. On a live run, GLM answered "(faux) Wrote crash-marker.txt, then: ...", copying the faux warm-up answer. The check still passes; it only needs a new assistant entry.
  • --live through Mycel needs MYCEL_URL. It defaults to localhost:7438. Use MYCEL_URL=https://mycel.thefocus.ai.

Not tested

  • A kill during a tool's execution. There the tool may run twice, so it needs its own idempotency.
  • A kill during a commit itself.

Changes from the torture box

  • The crash drill now stands alone and has its own SQLite file under habitat/. There is no Mounted repo, REPL, or other drill.
  • New no-rerun evidence: the parent edits the marker after the crash and checks the edit survives. The old drill only checked the marker existed.
  • With --live, the resumed generation now uses the live model. The old drill always resumed on faux.

Smoke run

Visual Target Wireframe ASCII Layout
PASS sigkill: worker exited via SIGKILL
PASS inspect: unfinished tasks [pi.generation]
PASS preserved: 6 pre-crash entries still present
PASS resumed: new answer: assistant: Tool results: write:OK
PASS no-rerun: write tool results=1, marker still holds the post-crash edit=true

Live run (--smoke --live --model mycel:z-ai/glm-5.3-flash): all five PASS.