A crash can leave a durable log whose final turn never closed. The old
behavior truncated everything after the last turn/end as a "crash tail".
But a single turn can be HUGE in a long-horizon task (many steps, large
tool output), so truncating it silently destroys real, durably-written
work — truncating a turn is wrong.
New crash recovery (ADR 0018): load() PRESERVES the interrupted turn's
events and CLOSES the orphaned turn by durably appending synthetic
boundary events — a step/end if a step was open, then a turn/end carrying
the new merge-extensible TurnEndReason {kind:'interrupted'}. load()
returns the balanced log, so a resumed session is immediately usable. Only
a never-fully-written TORN tail fragment is discarded; corruption in the
committed region is still unloadable.
- dsh-session: TurnEndReason {kind:'interrupted'} + shared
interruptedTurnClosers() repair helper.
- JSONL backend: scanLog preserves the longest contiguous prefix
(including a partial final turn); loadCore truncates a torn fragment and
durably writes the closers, returning the balanced log.
- runPersistenceContract gains a crash-recovery test (both backends + mock).
- Docs: ADR 0018/0017, architecture.md, package READMEs.
Also (review #33): RFC 013 records the "move event vocabulary to Zod"
question (merge-extensible maps → runtime schema registry) + blast radius;
deferred, not done here.
6.4 KiB
ADR 0018: Session persistence as an abstract service over the existing SessionEvent
Status: accepted (2026-06-15)
Context
Sessions lived only in memory. The example session-jsonl.ts plugin (duplicated byte-for-byte in both examples) was write-only telemetry: it buffered session/event and appended JSON lines, with no read/replay path, no crash-safety (no fsync, no atomic write, a fire-and-forget dispose drain), no listing, and no format versioning. Nothing could rehydrate a past session from disk into a live agent, so durable resume ("continue yesterday's task"), durable forking, and the ACP session/load method (RFC 010) were all impossible.
The event-sourced model makes the append-only log the single source of truth and derives LLM history from it. Persistence had to stay faithful to that: persist the existing SessionEvent directly, with no parallel "persisted message" type that the log is converted to and from. The backend also had to be swappable — a file store now, a database store later — behind one interface.
Decision
Persistence is an abstract capability seam (ADR 0009, the dsh-bash template), not loop or core logic:
- Interface (
dsh-session-persistence,ctx.sessionPersistence) — an abstractSessionPersistenceservice:create/append/load/list/has/delete/update. Its persisted unit IS the existingSessionEvent({ type, seq, time, data }), reused verbatim — no conversion type. - Implementation (
dsh-session-persistence-jsonl) — an append-only JSONL log per session (aSessionHeaderline then oneSessionEventper line, verbatim includingassistant/chunk) plus an atomic.summary.jsonsidecar for the mutableSessionSummary.
Key choices recorded here because they are durable, contested, and surprising:
- The canonical durable log persists every
SessionEventverbatim, includingassistant/chunk.deriveMessages()skips chunks, and a chunk-filtered rollout (Codex'spolicy.rs) is tempting — butseq = log.lengthand the load-validationevents[i].seq === irequire a contiguous log; filtering chunks out would leave holes and break both the contract and resume. A chunk-filtered projection is possible later as a derived view with its own renumbering, but it is NOT the canonical log. - Append-only; a crashed turn is closed, never truncated. Committed events — those at or below a flushed
turn/end— are never rewritten. The loop only flushes atturn/end, so a crash can leave a durable log whose final turn never closed: real, fully-written events sit after the lastturn/end. A single turn can be huge in a long-horizon task (many steps, large tool output spanning a long autonomous run), so discarding the interrupted turn would silently destroy a large amount of real work — truncating a turn is wrong. Instead, on reloadloadPRESERVES those events and CLOSES the orphaned turn by durably appending the minimal synthetic boundary events: astep/endif a step was still open, then aturn/endcarrying the merge-extensible{ kind: 'interrupted' }reason (a marker that records the turn was cut short by a crash, not completed by the model — no loop ever emits it).loadreturns the balanced log, so a resumed session is immediately usable. The ONLY thing discarded is a never-fully-written torn tail fragment — a final record whose bytes (JSONL) or row were never completely flushed; that fragment is not a valid event and is dropped before the synthetic closers are written. A parse error orseqgap in the COMMITTED region (at or before the last realturn/end) is genuine corruption and makes the session unloadable. - File backend canonical, DB backend a drop-in.
SessionEventmaps 1:1 onto a row(session_id, seq, type, time, data)—appendis INSERT (in a transaction asserting the contiguous-seq contract),loadis SELECT … ORDER BY seq. A futuredsh-session-persistence-sqliteis aSessionPersistencesubclass with no interface change (opencode runs this exact shape on SQLite/WAL). - Metadata is out-of-log. Format version, cwd, and lineage are storage concerns, not replayable conversation state, so they live in a
SessionMeta(SessionHeader & SessionSummary) owned bydsh-sessionand attached to aSessionvia a new readonlysession.header— never inSessionEventMap, never reachingderiveMessages(). The alternative (a merge-extensiblesession/metaevent as log line 0) was rejected: an in-log event would ride along with a seeded/forked session for free, but metadata is not replayable state, so the explicit out-of-log header seam is the cleaner cost. loadreturns a resumable event log, not just bytes.load(sessionId)yields theSessionMetaplus the committedSessionEvent[](through the last completeturn/end), shaped so a caller can reconstruct a live session with the loaded events as seed (solastTurnNumber/deriveMessagescontinue) on the SAME session id. The agent-facing create/resume factory that consumes this is a separate seam (a follow-up onctx.agents); the persistence layer deliberately stops at theloadprimitive and does NOT reach into the loop. The agent-loop does NOT hard-injectsessionPersistence(that would pend non-persistent demos forever), so any resume path built on this rejects with a clear error when the backend is absent.
Format versioning: the header carries a version; load rejects an unknown version (no v1 migration). Stated honestly: append-only + flush is robust to partial trailing writes (tolerated on load) but not to fsync-less power loss mid-line; a DB/WAL backend is the stronger option later.
Consequences
Two new packages and the metadata seam in dsh-session (session.header, the create(id?, options?) signature). Bought: durable resume/fork, a read/replay path, crash tolerance, and the foundation RFC 010's session/load needs — all over the existing event-sourced log, with the backend swappable behind one interface. The reusable runPersistenceContract suite holds every backend to the same append-only / contiguous-seq / lazy-materialization / serializability semantics. This completes ADR 0003's deferred "real persistence backend" and resolves its TODO(review) on the event vocabulary: persisting the log freezes its shape, and the assistant/chunk fidelity question is answered above (persist verbatim).