* test(session): cover SQLite persistence and document the provider * test(session): align coverage with the latest SQLite implementation * test(session): adapt storage coverage to the current SQLite backend * feat(session)!: pass fork source through agent creation * refactor: prepare forks privately and append child segment events * refactor: choose fork sharing in SQLite creation transaction * fix(session): preserve caller-prepared fork seeds * feat(session): flush fixed fork prefix before child creation * fix(session): persist fork sources and await parent drain * chore: remove test and documentation changes from fork PR * refactor(agent-loop): reuse agent fork source type * fix(session): retain SQLite handle and JSONL errors after rebase * perf(session): create forks from the caller's history without reloading the parent The fork source carries the complete child history from seq 0. A live parent's log supplies the shared prefix; a cold parent's prefix is used as persistence returned it to the caller. The factory no longer reopens the parent, and the JSONL backend copies the supplied history instead of decoding the parent log again. The SQLite backend drains a live parent writer before sharing its persisted prefix, so the factory no longer flushes the parent or tracks owner disposal during that wait; its fork creation serializes child events without the read-side validation and reports publication failures through the shared wrapper. * refactor(session-query): report the persisted prefix length on live observations A live observation reports its event count as the persisted prefix, so the fork command's boundary fallback only covers observations from other producers. * test(session): exclude the SQLite POSIX lease from Windows coverage The SQLite copy of the session write lease executes its POSIX face only off Windows, matching the JSONL exclusion. * test(session): report legacy opener failures on stderr The two-process suite asserts only the error name; the message and cause now reach the inherited stderr so a CI failure identifies the SQLite error. * refactor(agent): keep seed as the constructor input and narrow the fork source CreateAgentOptions.seed stays the only constructor history; source names the parent and cut a seeded fork shares. The factory stores the constructed log with that source at creation, so no fork-only seed selection remains in the loop. * test(session): complete fork regressions and documentation * test(session): propagate current SQLite fixtures through fork regressions * test(session): describe fork creation from the caller's history The fork source now carries the complete child history and the factory no longer reopens or flushes the parent, so the fake factories, source expectations, and cancellation cases follow that flow: a sharing backend drains the parent writer itself, JSONL copies the supplied history, and observations always report the persisted prefix. Documentation, the upgrade guide, and the generated catalogs describe the same behavior. * test(session): cover fork creation without a factory flush The in-process fork provider's cases no longer wait for a parent flush: the live log supplies the completed prefix and cancellation is exercised while child storage is pending. The Remote fork command's boundary fallback is covered by an observation that omits its persisted count. * test(session): drop redundant observation assertions in the fork fallback case * test(session): describe fork creation with seed beside a parent source The agent factory keeps seed as the constructor history and source names only the parent and cut, so the fakes, source expectations, and documentation follow that split; the case where a live parent replaced the supplied prefix no longer applies. * refactor(session): name the fork parent option parentSource `CreateAgentOptions.source` and `SessionPersistenceCreateOptions.source` become `parentSource`: the field names the parent Session a seeded fork shares its prefix with, and the shorter name read as a generic origin. * refactor(session-query): report persistedEventCount on cold observations only A live observation holds the Session's own events, none of them read-time repairs, so its stored prefix is its whole log and the field said nothing; only a cold read appends interrupted-turn closers in memory and needs to tell the fork cut where the stored prefix ends.
description, kind
| description | kind |
|---|---|
| The durable session-storage seam for users and maintainers choosing a persistence backend, resuming sessions, or building a backend against the shared service contract. | package-reference |
@deepseek-ai/dsh-session-persistence
English | 中文
Summary
This package lets applications persist and resume session event logs through a backend-independent API. Readers can create, open, inspect, list, append to, read, flush, and close stored sessions while preserving contiguous append-only history. A completed flush is the durability barrier; readers never receive torn tails or invalid records, and only one writer per session is allowed within a backend instance. Use the shipped JSONL backend for one compressed log per session, or implement another backend with the same observable guarantees.
Table of Contents
- Use this package
- Understand the implementation
- Further Exploration
- Model Experience
- Known Limitations and Deferred Work
- Dev Note
Use this package
Mount one persistence backend to make sessions durable. The backend registers itself as ctx.sessionPersistence and routes every published session's live events into that session's active write handle; agent-loop — the production publication point for sessions — acquires each session's write handle before publication, so nothing else in the composition changes.
Choosing a backend
Shipped profiles use the JSONL backend: one append-only .jsonl.zstd log per session. The separate SQLite provider saves logical events in a database and imports existing JSONL histories on first open. Both providers expose the same service and handle methods. A third-party backend may implement the service directly; the backend contract below is what it must honor.
What the service provides
With a backend mounted, five service methods address stored sessions:
const handle = await ctx.sessionPersistence.create(header) // store a new session, take write ownership
const handle = await ctx.sessionPersistence.open(id, 'write') // claim single-writer ownership of an existing session
const reader = await ctx.sessionPersistence.open(id, 'read') // observe without ownership
const snap = await ctx.sessionPersistence.stat(id) // header + revision (+ eventCount / sizeBytes) without a log read
const all = await ctx.sessionPersistence.list() // one snapshot per visible stored session
await ctx.sessionPersistence.flush() // backend-wide durability barrier over every active write handle
Service-level flush() drains every active write handle's routed events and materializes its session, exactly as each handle's own flush would; failures aggregate per session as an AggregateError without abandoning the sweep, and a handle closed mid-sweep counts as flushed because close itself drains durably.
create(header, { source, inheritedEventCount }) durably stores the complete fork history before returning the writer. source.events runs from seq 0: the parent's stored prefix through the exclusive source.endSeq, then the child's events; when header.isSeeded is true, they contain the inherited marker at inheritedEventCount. A backend that shares the prefix requires the parent to exist with at least endSeq persisted events and writes only the remainder; a copying backend writes all of them. Later parent appends do not change the child. See SessionPersistenceCreateOptions for the source fields and creation failures.
Every log read and write flows through the returned SessionHandle; there are no id-addressed append or load methods. handle.read(offset?, length?) returns { eventState, events }: the outer slice belongs to the caller, while eventState distinguishes an exclusively detached event graph from a shared-frozen graph that may also reside in a backend cache. The producer establishes this state and slices preserve it even when empty. Both states are safe to adopt without copying; a consumer that needs mutable events clones them first. Reads never include a torn tail, repeated reads on one handle never observe an older state than a prior read, and a write handle reads its own successful appends. handle.append(events) appends a contiguous batch whose first seq equals the stored next-seq; persistence is best-effort on resolution — the batch is accepted, ordered, and visible to reads on this backend instance, and only a resolved flush promises it survives a crash (the shipped JSONL backend happens to persist each batch immediately). handle.flush() is the durability barrier and also materializes an empty created session so it becomes durably listable. handle.close() is idempotent and uncancellable: a read handle frees local resources; a write handle completes pending durability and releases write ownership. Once an append or flush resolves, reads started afterwards on the same backend instance — on any handle, or through stat/list — observe at least that prefix.
Ownership and visibility
create and open(id, 'write') take in-process single-writer ownership: a second write open while an owner is active rejects with SessionAlreadyOwnedError, create on an occupied id rejects with SessionAlreadyExistsError, and a mutation on a read handle rejects with SessionReadOnlyError — one handle type, runtime refusal. Any operation on a closed handle rejects with SessionHandleClosedError, and SessionOwnershipLostError marks a write handle whose ownership is permanently gone (close it and reopen). A created session is observable in this process from the moment create resolves, while the backend may defer physical materialization until the first append or flush; other processes see only materialized sessions, and a session that never materialized before a crash never existed.
The live write path and shutdown drain
The backend owns the live write path: it installs the session listeners once and routes every published session's events by id to that session's active write handle — session/event copies into a bounded internal batching window, session/flush is the immediate durability and error-observation barrier, and session/disposed runs the final drain and closes the handle. A published session without an active write handle persists nothing. A background write failure retains its events in order, pauses the automatic path, and is logged; the next explicit flush retries and rejects loudly. close() itself drains the routed buffer through the still-open storage before releasing ownership, so backend teardown's close sweep keeps application shutdown lossless even though root-fiber disposal runs fibers' disposers concurrently.
Resuming and crash recovery
Persistence returns the physically valid log; semantic repair belongs to the reader. A session that crashed mid-turn keeps its open final turn — a single turn can be large, and those events were durably appended before the crash; only the incomplete fragment of a never-acknowledged torn tail is discarded — complete records recovered from it are durably rewritten by the write path before the handle's first new append. Resume (agent-loop) reads the stored log through its write handle, computes interruptedTurnClosers — synthetic tool/result errors, any open step/end, and turn/end {interrupted} — and appends them through the same handle as an ordinary batch. Read-only observers (session-query) balance an interrupted cold log with the same closers in memory only.
Failures and recovery
For versioned artifacts, stat and list expose formatStatus as current or migration-required. The separate header remains current logical metadata. This status describes storage, not an in-memory preparation or migration task; providers without this information omit it. A materialized snapshot also carries location with the backend-resolved selected per-Session artifact path when the backend keeps one; this observational metadata supports diagnostics and export, is not persisted Session content, and is omitted for an unmaterialized pending Session.
A stored log the current build cannot faithfully interpret is refused with a direction-aware error, never misread. SessionHandle exposes only current logical records identified by SESSION_FORMAT_VERSION; a provider must convert any supported historical storage before returning a handle, and the shipped JSONL provider migrates supported historical generations through its static catalog. A newer format instructs the operator to upgrade the harness. An event type unknown to this build refuses unless its envelope marks it ignorable, and committed-prefix corruption rejects as SessionPersistenceCorruptionError.
Understand the implementation
Implementation internals — click to expand
This section explains how the seam realizes durable storage and how backends plug in; the observable contract is covered in Use this package and the generated Cordis API.
Design concept
The package is a seam, not a backend framework: it exports the abstract SessionPersistence service, the SessionHandle contract, the stable error classes consumers catch, the pure stored-record validation helpers (storage-contract), and the branded revision — nothing else. Each provider owns its complete storage runtime (handle class, mutation ordering, single-writer bookkeeping, live-event routing, teardown), and two shared test suites — runPersistenceContract and runLiveWritePathContract under tests/ — pin the observable behavior every provider must agree on. Deliberate consequence: providers may resemble each other where their storage happens to be similar, but no implementation machinery crosses the package boundary.
The invariants every backend honors
- Append-only, contiguous
seq. Committed events are never rewritten;append's firstseqmust equal the stored next-seq, and a gap rejects. - A torn physical tail never reaches a reader. It belongs to an append that never resolved; the write path truncates it durably before its first new append.
- Lossless JSON data. Batches and headers pass the shared one-pass validate-and-snapshot boundary (
materializeAppendBatch/materializeCreateHeader); non-serializable payloads reject at the call site. - Durability.
appendpersists best-effort;flush— per handle or service-wide — is the barrier that promises storage and also materializes an empty session. - Fail-closed reads.
validateStoredEventsrefuses unknown event vocabulary and retired pre-release shapes;assertVersionrefuses foreign format versions. - Single writer per backend instance. The provider's in-process claim is taken at
create/open('write')and released at handle close.
Source map
| File | Role |
|---|---|
src/index.ts |
Plugin entry: the abstract SessionPersistence service and re-exported seam vocabulary |
src/handle.ts |
The SessionHandle contract: read/append/flush/close semantics and freshness rules |
src/storage-contract.ts |
Shared validation: version gate, fail-closed vocabulary, batch materialization, contiguity |
src/errors.ts |
Stable handle/ownership failures and format refusals |
src/revision.ts |
The branded opaque revision token |
The write path at a glance
Each session/event for the writer's session copies into that handle's internal buffer. The first pending event starts a fixed batching window; later events join without resetting its deadline. Expiry drains the pending prefix through the handle's mutation chain; events admitted during a drain coalesce into the next chained batch, in order. session/flush cancels the wait and drains through quiescence, then runs handle.flush(), so the loop uses it as the ordering and error-observation checkpoint before the next turn. A rejected background drain retains its events and pauses the automatic timer; explicit flush, writer close, or backend teardown retries immediately and rejects loudly. Constructor seed events never emit session/event, so a seed appended through the handle before publication is never re-enqueued.
Stored-record validation
The seam's shared helpers validate current logical records identified by SESSION_FORMAT_VERSION, and appends write only the current format (rationale). Historical decoding and immutable successor publication belong inside each provider before it returns a handle. Every backend runs storage-contract validation on handle reads and write-open priming, refusing an unknown event type as SessionFormatUnsupportedError and a malformed current record as SessionPersistenceCorruptionError, with the raw-log SessionLocation attached when the backend keeps one artifact per session.
Further Exploration
Read these pages when the package-level contract is not enough. They move from the shared durability model to the shipped backends and the decision evidence.
- Session persistence subsystem — the full service contract, handle semantics, flush checkpoint, crash recovery, and generated Cordis API.
- Handle-based persistence Agent Note — the seam design and its ownership model.
- JSONL persistence backend — the shipped per-session-file backend.
- Session checkpoint policy — the plugin that flushes through
session/flushat semantic boundaries. - Session package map — adjacent persistence, projection, title, and telemetry packages.
Model Experience
Resumed conversation history
What the model sees
The seam adds no prompt or schema. Resume restores stored surface events as message history; stored request headers reconstruct earlier calls, while the new loop composes the current system prompt, tools, and session prefix for its next request. Crash repair marks an assistant request without a durable call as TOOL_NOT_STARTED; a durable call without a result becomes TOOL_OUTCOME_UNKNOWN, whose text lets the model retry read-only or idempotent work but directs it to verify side effects or ask the user instead of retrying blindly.
Token effect
Zero tokens during ordinary persistence. Resume restores retained history cost and pays the current request envelope normally; each repaired call adds the quoted retained error text.
KV Cache effect
Persistence does not mutate live request prefixes. A resumed loop can reuse provider cache only when its reconstructed history, current envelope, and model route match; crash-repair results append without rewriting earlier history.
Known Limitations and Deferred Work
These limits define where the seam's guarantees stop. They are current package constraints, not a task backlog.
- The seam guarantees write ownership only within one backend instance — cross-process exclusion is provider-specific. The shipped JSONL provider adds a kernel-backed lease across instances and processes; another provider must document an equivalent guarantee or require deployments to prevent concurrent writers.
- A backend plugin reload under live sessions fails their writers loudly — a reloaded backend cannot serve handles the old instance issued; writes fail until the sessions restart, and nothing silently re-adopts the logs.
- Only handle-acquired sessions persist —
ctx.sessions.create+session/flushalone stores nothing; agent-loop is the production acquisition point, and tests seed storage throughcreate/append/close. - No deletion or retention API — pruning stored sessions is out-of-band backend maintenance.
list()is unpaginated and unfiltered — it returns every stored session's snapshot; fine for local stores, unindexed at scale.- Synthetic closers are the only crash story — resume appends
interruptedTurnClosersthrough the write handle; there is no partial-turn resume that continues an interrupted turn instead of closing it.
Dev Note
Working context for maintainers — click to expand
None.