Recovery and retries
What survives a daemon crash, what does not, and how a viewer continues an agent's work safely.
What is durable
With home, the supervisor uses a file store under it:
- one append-only JSONL event log and one atomic JSON record per session (0600);
writer.lock, created exclusively by the one process that owns the home;identity.json(the daemon key), and, once served,token,peers.jsonanddaemon.sock.
Every event is on disk before any viewer sees it. A torn final line after a crash is cut on the next open; a complete final event that lost only its newline is kept. Temp files left by an interrupted atomic write are removed at open. A torn or unreadable line in the middle of a log loses that event only: reads skip it and log a warning. A write that fails part way (a full disk) is cut back to the last complete batch before the next append.
After a crash
Sessions that were running when the daemon died become failed with error.code: "daemon.interrupted_by_restart", ending with an error event and a terminal status event. Their outcome is
unknown, so the daemon never re-runs them. Exited sessions are untouched.
The new daemon refuses a home whose writer.lock is still there (daemon.store_locked). The lock is never stolen
by guessing from a PID. Remove it only after proving the old process is gone.
// The process dies: its sockets go away, the supervisor is never closed, writer.lock stays behind.
paired.close();
await first.server.close();
const port = Number(new URL(first.server.directUrl ?? "").port);
// The new daemon refuses a home with a writer.lock (daemon.store_locked). The operator removes it only after
// proving the old process is gone.
await fs.rm(path.join(home, "writer.lock"));
clock.advance(60_000);The daemon keeps its identity and its peers, so paired viewers reconnect without pairing again, and replay the old
session's log with session.subscribe as before.
const second = await startDaemon(port);
const client = await connect(daemonId, viewer); // same identity.json, same peers.json: no re-pairing
const { sessions } = await client.list();
const old = sessions.find((s) => s.id === live.id);
assert.ok(old);
console.log(`after | ${old.id} ${old.status} ${old.error?.code}`);
for await (const event of client.session(old.id).events({ afterSeq: old.last_seq - 2 })) {
const detail = event.type === "status" ? event.status : event.type === "error" ? event.error.code : "";
console.log(`after | ${event.seq} ${event.type} ${detail}`);
}Continuing the agent
To continue an agent's conversation, start a new session with resume set to the old session's
agent_session_id (runners with capabilities.resume; claude-code has it). Pass an idempotency_key derived
from the old session, so a start retried after a lost result returns the same session instead of forking the
conversation twice.
const start = {
runner: "claude-code",
cwd: project,
prompt: "Fix it and run the test again.",
resume: old.agent_session_id, // the Claude session to continue
idempotency_key: `resume:${old.id}`, // a retried start returns the same session instead of forking it
};
const resumed = await client.startSession(start);
const retried = await client.startSession(start); // e.g. after the first result was lost with the channel
console.log(`resume | ${resumed.id}; retry returned ${retried.id === resumed.id ? "the same session" : retried.id}`);
for await (const event of resumed.events()) {
if (event.type === "message") console.log(`resume | ${event.role}: ${event.content}`);
if (event.type === "exit") console.log(`resume | exit ${event.exit_code}`);
}The transcript:
before | ses_00000000000000000000000001 is waiting_input
after | ses_00000000000000000000000001 failed daemon.interrupted_by_restart
after | 8 error daemon.interrupted_by_restart
after | 9 status failed
resume | ses_00000000000000000000000002; retry returned the same session
resume | user: Fix it and run the test again.
resume | assistant: Fixed parse.ts. The test passes.
resume | exit 0Retrying requests
A viewer that loses its channel cannot know whether a request ran. The rules:
| Request | Safe to repeat? |
|---|---|
session.start with idempotency_key | Yes. Same key and same intent return the same session; the same key with any other field changed fails with daemon.idempotency_conflict |
session.start without a key | No. It starts another session |
approval.respond | Yes, with every field the same; see Approvals |
session.input | No. Check the session's events first |
session.stop, session.interrupt, reads | Yes |
peers.revoke | Yes. Revoking an unknown peer returns {}. If saving peers.json failed, the key is already refused and its channels closed, and a retry writes the file again before it returns {} |
The intent compared for idempotency_key is the runner, cwd, prompt, name, model, env, tenant and the
runner options (resume included).