* refactor(session): move Session log validation into a module Move header, seed-event envelope, and inherited-cut validation out of the Session constructor into validation.ts without changing check order or error text. Export needsInheritedSeedMarker for the fork marker rule the constructor applies. * feat(session): validate a Session log without constructing a Session validateSessionLog composes the header, seed-event, surface, and inherited-cut checks of Session construction in the same order. Omitting eventState matches Session.create; an event state matches Session.fromRestore. * refactor: validate stored Session logs with validateSessionLog The chat importer, session-query readSession, the format catalog, and JSONL generation verification constructed a discarded Session only to validate a log. They now call validateSessionLog with the same mode, inherited count, and message interpreters. * refactor(session): extract buildSessionHeader Move new-Session header construction out of SessionStore.prepare into the exported buildSessionHeader and name its input SessionHeaderMeta. Automatic ids and duplicate checks stay in prepare. * refactor(session): move header and fork error helpers out of the session index Move buildSessionHeader into validation.ts and SessionForkError with SessionForkErrorCode into fork.ts so later modules can import them without importing index.ts. index.ts re-exports both under the same names. Complete the JSDoc of the exported validation helpers. * refactor(session): extract checkSessionLog from the Session constructor Move header validation, seed acceptance with surface admission, and the inherited-cut check into checkSessionLog in validation.ts, keeping the constructor's order and errors. The Session constructor and validateSessionLog call it. * test(session): cover Session log validation and construction helpers Cover validateSessionLog against Session.create and Session.fromRestore in every event state, buildSessionHeader, needsInheritedSeedMarker, Session.readEvents range semantics, and the fork subpath export of SessionForkError. * docs(session): document Session log validation Document validateSessionLog, buildSessionHeader, needsInheritedSeedMarker, Session.readEvents, and the SessionForkError subpath export in the package README. Align the Session and CreateSessionOptions type blocks, add SessionHeaderMeta, regenerate the Cordis, event graph, and persistence catalogs, and update the plugin entry, unknown-cast, and package dependency registries. --------- Co-authored-by: imccyu <[email protected]>
description, kind
| description | kind |
|---|---|
| The event-sourced session log and in-memory store for users and maintainers building, inspecting, or extending the durable record behind every agent interaction. | package-reference |
@deepseek-ai/dsh-session
English | 中文
Summary
dsh-session records every model-visible fact in an append-only session log and derives model history from that record. Consumers can inspect, replay, fork, and flush sessions while preserving historical events; compaction hides superseded entries from the active conversation without deleting them. Sessions remain in memory unless a persistence backend is added, and durability checkpoints wait for configured backends. Choose this package wherever an agent needs a reconstructable session record; it does not call models.
Table of Contents
- Use this package
- Understand the implementation
- Further Exploration
- Model Experience
- Known Limitations and Deferred Work
- Dev Note
Use this package
Mount dsh-session wherever a session must exist. It creates and holds event-sourced Session instances in memory; durable storage is layered on by a persistence plugin that subscribes to the session/event feed.
Create and inspect sessions
SessionId is a type exported by the package root and /types. Apply it to an admitted string with brandString<SessionId>(value) from dsh-brand; branding does not validate or generate an id.
ctx.sessions.create() builds a live session bound to the calling fiber; get(id) and list() find sessions, and fork() creates an independent session from a stable prefix of a live one. It copies history, cwd, isSeeded, and the inherited event count without recording a source relationship. parentSession records a subagent’s direct creator; historical ordinary forks may retain their source there, so classification uses origin.
Independent Session.create() and Session.fromRestore() instances accept events and plugin records without sending session/event. A SessionStore takes ownership through prepare() or enter(); writes then require store membership and a completed synchronous session/created announcement. Creation listeners are read-only and see all existing history; entering an independent instance does not replay that history as session/event. Failed announcement or removal leaves writes disabled. Removal emits session/disposed only if session/created began, including a failed announcement; an entered but never announced instance emits neither notification.
const session = ctx.sessions.create(sessionId, { meta: { cwd: '/workspace' } })
ctx.sessions.get(sessionId) // the live session
ctx.sessions.list() // every live session, in creation order
Validate a log and build a header
validateSessionLog(header, events?, inheritedEventCount?, eventState?, projections?) runs every check that constructing a Session performs, in the same order and with the same errors, without constructing one. Without eventState it matches Session.create(), which copies the borrowed events and header before validating them; with detached or shared-frozen it matches Session.fromRestore(), which validates and deeply freezes the header in place and leaves the events uncopied. The Session identity comes from header.id, and no in-memory inherited marker is appended.
buildSessionHeader(id, meta?) builds the current-format header that SessionStore.prepare() uses from the caller-supplied SessionHeaderMeta fields; createdAt defaults to the current time and isSeeded to false. The result is neither validated nor frozen. needsInheritedSeedMarker(header, events, inheritedEventCount) reports whether a seeded log lacks session/end-seed { inherited: true } at its cut, the condition under which Session construction appends that marker to a borrowed seed; an earlier ancestor marker does not count.
Append and derive
session.append(type, data, opts?) commits one typed event. It rejects read-only instances before reading payload fields, snapshots and freezes valid JSON, then rechecks write permission before committing and notifying observers. session.deriveMessages() projects the log into the Message[] the model sees, incrementally and cached:
session.append('user/message', { role: 'user', content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } },
{ surfaceOp: 'append' })
session.deriveMessages() // the derived model history
Surface events (system/message, developer/message, user/message, assistant/message, tool/result) require surfaceOp in both typed events and append input. A replacement uses exactly { op: 'replace', startSeq, endSeq }, with inclusive SessionSeq endpoints in current surface order. An Assistant message embeds its exact compact provider stream and forbids sourceEventSeqs. Known log-only events forbid both metadata fields and never produce a message.
developer/message stores tool-addition names and a headerSeq reference to an earlier request/header. The reference is required exactly when additions are present, and each name must identify exactly one complete schema in that header. Removals name the tool without a header reference. Historical headers remain available across restart, fork, and surface replacement; the current registry and latest header do not select an earlier addition's definition. sourceEventSeqs continues to describe derivation and replaced nodes.
Plugins declare content-changing events with @messageProjection and register a pure definition through ctx.sessions.registerMessageProjection(). Session calls the definition before accepting an event and caches its immutable message updates. Missing definitions reject append and restore; unloading a used definition also blocks cached reads. Detached constructors and foldSurface(events, projections) require explicit definitions. Reconstructors pass the fold's projectedMessages to deriveEventMessage(); live instance methods apply the same projections automatically. Plugin-owned message projections defines ownership and offline assembly.
Append, seed/restore, and event adoption/snapshot reject any header.system and exactly empty optional request-header fields (tools: [], adapterDefaults: {}) instead of normalizing input. Tool-result data.error is allowed only when the first-class message has isError === true; failure identity remains optional. Rejected appends do not change the log, derived state, or event feed. Adoption validates event-local metadata but not referenced history or replacement membership.
system/message stores model-visible system text, including source-attributed admissions and the instructions rebuilt on the next model attempt after compaction. Optional source metadata identifies canonical section text separately from source-associated notices; adapters format the recorded blocks for provider requests. The system-prompt package owns per-Session comparison and prompt assembly. Empty-content system nodes project to no message. The surface fold protects a system/message at node 0 from replacements except a system event replacing exactly that node; later system nodes may be shadowed by compaction.
Read the log
session.seq reads the current log length without materializing an array, and session.eventAt(seq) reads one accepted, deeply frozen event by sequence number. session.snapshotEvents(fromSeq?, toSeqExclusive?) materializes a frozen, stable snapshot of a half-open range; a complete current snapshot is cached until the next append. eventAt(), snapshotEvents(), and ownEvents() are deprecated: existing logic may remain unmigrated for now, but new production calls are prohibited. Repository test files may use these three readers under their scoped lint allowance (policy). Callers that only need a length use seq.
Session log positions use two numeric types. SessionSeq identifies an existing event or inclusive event watermark; SessionLogOffset identifies a gap, prefix length, or read boundary and may equal the event count. SessionSeqCursor adds the -1 “no event yet” value, while OptionalSessionSeq uses null when absence is data. The constructors validate non-negative safe integers, and the brands disappear at runtime, so durable JSON and wire values remain ordinary numbers.
Write experimental plugin records
appendPluginRecord(session, type, data) appends one plugin record for a package under packages/experimental/; the verify-plugin-record-callers check rejects any other production caller in this repository. Plugins outside this repository must not call it either, although no repository check can inspect them.
Declare each record in PluginRecordMap through @deepseek-ai/dsh-session/types, with a description and an explicit payload type annotation in the owning experimental package's src/. Names use plugin:<owner>/<record>, where owner is the package name after removing the legacy @deepseek-ai/dsh-experimental- prefix when present, otherwise @deepseek-ai/dsh-; for example, plugin:pi-extensions/entry. TypeScript checks each name and payload against its declaration; the writer's runtime JSON snapshot rejects values that cannot be preserved losslessly. Put extension-defined or other dynamic names inside the payload of a declared record.
The experimental persistence catalog lists current plugin-record declarations with their owners, descriptions, payload type annotations, and source files. These declarations remain separate from SessionEventMap, expanded persistence schemas, released type history, and KNOWN_SESSION_EVENT_TYPES. Records carry ignorable: true: a build that does not recognize one retains and skips it on read. Records never enter the model-visible surface. Resume and fork carry them with the rest of the log; Session format migration keeps them on a best-effort basis.
pluginRecordOf(event) returns an event as a record, or undefined for any other event; a ctx.sessionProjections unit that passes each event to it rebuilds plugin state on resume. Its data remains unknown even when the current map declares the name. The owner validates it before use because earlier builds can write different payloads, and the V3-to-V4 format edge can rename an unknown ignorable V3 event into the same plugin: namespace. Removing or renaming a declaration does not discard stored records. The ignorable events decision owns the compatibility policy.
Fork a session
ctx.sessions.fork(source, boundary?, childSessionId?) copies an exact inclusive event prefix (default: the last event) from a live source. buildForkSeed in dsh-session/fork places an inherited marker after the copied events, adds missing error tool results only for the open step, and closes that step and turn with a forked reason. Closed steps and turns remain unchanged, including historical missing results. The marker and closers belong to the child; inheritedEventCount counts only the copied prefix.
A rejected fork throws SessionForkError with a SessionForkErrorCode; both are exported from the package root.
The logical SessionHeader.isSeeded field reports whether fork history exists without exposing a positional integer. Session.inheritedEventCount retains the exact checked SessionLogOffset; ownEvents() returns events at and after that cut, and isOwnSeq(seq) accepts only an existing child-owned position. A low-level seeded constructor must supply an explicit seed and inheritedEventCount because the constructor seed can contain child-owned setup events after the inherited prefix.
session.append(type, data, ...opts) appends one event and returns that event. session.append(inputs) accepts a readonly array of positional [type, data, ...opts] tuples (SessionEventInput) and returns the committed events in input order; an empty array appends nothing. Array admission validates every candidate and synchronous dispatch before changing the log or message surface, so observers read the complete committed group. Persistence listeners retain their own durability rules; array admission alone does not guarantee crash-atomic storage.
Flush durable state
ctx.sessions.flush(session) dispatches the awaited durability checkpoint: every persistence listener flushes and the call settles after all of them. A producer that needs an immediate durability barrier awaits it instead of assuming the write-behind drained.
Understand the implementation
Implementation internals — click to expand
This section explains how the package realizes the behavior above; the observable contract is covered in Use this package.
Design concept
The package is built on event sourcing: a Session is an append-only log of typed SessionEvents, and everything else — model history, transcripts, telemetry, titles, persistence — derives from that stream. The surface is a derived projection: an incremental manager validates append candidates, advances the ordered view from committed events, and tracks replaceGeneration for positional replacements and contentGeneration for replacements and plugin-owned message changes. Model-visible means logged: anything that reaches a model request must be reconstructable from the log. Each model attempt that reaches settlement commits one event: assistant/message carries the assembled model-visible message plus its compact timed stream, while assistant/attempt retains a failed, retried, cancelled, or stream-error attempt without adding model history. A hard process loss before settlement leaves no durable attempt stream.
Request headers
request/header stores a full canonical snapshot of the non-history request envelope with reason initial, resume, change, or series. An explicit message-series start, a surface replacement, or a plugin-owned message change writes a series snapshot when the envelope is unchanged; a simultaneous change uses startsSeries: true. Same-series steps, retries, and ordinary later turns inherit the latest snapshot. adapterDefaults distinguishes values resolved by the adapter from explicit settings, and foldRequestHeader() selects the latest snapshot. This self-contained record supports partial-window rendering and exact reconstruction at the cost of growth per message series; the reconstructable-requests Agent Note owns the detail.
Source map
| File | Role |
|---|---|
src/index.ts |
Plugin entry: SessionStore service, store lifecycle, fork, flush |
src/validation.ts |
Session log validation shared by Session construction and validateSessionLog; header construction |
src/types.ts |
SessionEventMap, SessionEvent, UserMessage, SessionHeader, TurnEndReasonMap |
src/surface.ts |
Ordered surface projection, replacement validation, deriveEventMessage |
src/request-header.ts |
request/header folding and reconstruction |
dsh-util-values |
Shared lossless JSON validation and detached snapshots |
src/repair.ts |
Shared tool-result recovery for failed steps, interrupted logs, and fork seeds |
Append validation
Every append uses the shared iterative snapshotJsonValue() pass, which reads, validates, and copies each nested value once, so a stateful getter cannot supply one value to validation and another to storage. Non-lossless-JSON payloads (BigInt, cycles, sparse arrays, -0, exotic prototypes) are rejected at the append site, before any backend flush. The append path constructs each SessionSeq; surface events additionally validate marker shape, cited source-event sequences, and complete shadowed-node coverage for replacements.
Shared recovery
ToolCallRecovery tracks unanswered requests from committed events without retaining event history. AgentLoop observes live steps; crash recovery and fork-seed construction replay their prefixes through openTurnClosers. Live failures and crash recovery use interrupted-result wording by default; fork construction passes the fork cause to select its distinct retry guidance. The caller appends recovery results before closing the step (reference).
Derived history
deriveMessages() caches deep-frozen projections and returns a fresh array per call. The surface event types (system/message, developer/message, user/message, assistant/message, tool/result) supply their recorded message identities and content; empty-content system and developer nodes project to no message. Plugin-owned projections change derived content without mutating recorded messages. Replacements and projection decisions invalidate the cache. Embedded Assistant streams and assistant/attempt events remain replay and diagnostic data only. Ordinary prompts and injected context project without automatic type- or source-specific wrappers. Producers own any framing in their recorded content.
surface.systemNodes exposes current system sequences and whether their logged content arrays are empty, in surface order. Each read scans the current surface nodes, so the list reflects actual replaced positions; citations outside a replaced range do not remove nodes.
The request header
The loop logs a full canonical request/header snapshot (call config, adapter defaults, assembled tool schemas — the rendered system prompt is a system/message surface node, not header state) at each loop-instance boundary and on change; foldRequestHeader(events) reconstructs it by selecting the latest snapshot, making every conversation request a pure function of the log. Route metadata (request/context) is separate logged state appended only when the provider, model, capacity, or systemPromptUpdate mode differs; it records the actual prepared call's mode after prompt and user admission, rather than supplying that admission decision.
Further Exploration
The package-level contract is enough for most consumers; read these when you need the surrounding domain.
- Session subsystem — the full event vocabulary, surface types, and generated service API.
- Persistence subsystem — how backends make this log durable.
- Core subsystem — the loop that writes and derives from sessions.
- Generated persistence catalog — every session event with its payload and declaration site.
- Core group map — how the core packages compose.
Model Experience
Derived message history
What the model sees
The model receives the complete messages from system/message, developer/message, user/message, assistant/message, and tool/result surface entries with logged projections applied, the system prompt first. Identities, roles, sources, and unmodified blocks retain their original values; projections never mint identities. Direct prompts and injected context remain separate user/message events whose sources preserve their attribution. Embedded streams, assistant/attempt, boundaries, and other log-only facts add no message.
Token effect
Appended surface entries are resent on later steps. A replace surface operation removes the shadowed entries from future inputs without deleting their raw log records.
KV Cache effect
Appended surface entries preserve reusable prefixes. A replace operation invalidates reuse from the first shadowed message even though the underlying event log stays append-only.
Tool-result recovery and fork results
What the model sees
If recovery finds an assistant tool request with no durable tool/call, its synthetic TOOL_NOT_STARTED result says The tool call was interrupted before the Harness recorded it as started. Retry it if it is still needed. If a durable tool/call has no result, its TOOL_OUTCOME_UNKNOWN result says The tool call was interrupted after it was recorded, but no result was durably recorded. Its outcome is unknown. Decide whether to retry from the tool semantics: retry only if the operation is read-only or idempotent; if it may have side effects, first verify external state or ask the user. Do not retry blindly. Fork-generated results describe only the inherited records: the parent may have started or completed a call after the selected event. TOOL_NOT_STARTED means the prefix contains no start record; TOOL_OUTCOME_UNKNOWN means it contains a start but no result. Both tell the model to retry only read-only or idempotent operations without further checks; operations with side effects require external verification or user input. See the fork reference.
Token effect
Zero tokens in an intact session. Each repaired call adds its retained risk-specific error text to later requests.
KV Cache effect
Append-only; newly visible content follows the reusable request prefix and does not invalidate existing KV-cache entries.
Logged request header
What the model sees
The session reconstructs active tool schemas and call config from request headers; provider-specific deferred and retained declarations are derived at dispatch. toolHistory() caches a stateful fold over committed headers and developer messages, consuming only unseen events on subsequent reads. It resolves additions through their historical headerSeq, returns immutable snapshots, and restarts declarations on an explicit request series or a changed retained definition. Restoring a retained name with an unchanged definition continues the series; the recorded addition re-offers it. Both loop requests and compaction calls use this state. Headers lacking matching update records use complete active declarations. Same-name schema changes are allowed and invalidate prefix reuse even on in-history routes; registration-time rejection against historical tools is deferred (rationale). The system prompt remains part of deriveMessages(); header events add no message or prompt copy.
Token effect
Zero duplicate tokens from logging. The system nodes and schemas still incur their normal per-request cost.
KV Cache effect
Logging causes no invalidation, and exact reconstruction preserves request-prefix identity. A later header with changed config or schemas may invalidate reuse from its first difference; a prompt change that replaces surface node 0 invalidates reuse from the first token, while an in-history append keeps the prefix through the cached history reusable.
Known Limitations and Deferred Work
These limits define when the session store needs special care. They are current package constraints, not a task backlog.
- Live-source Store API — persisted-but-unloaded sources use the Host observation path. Fork closes only the open tail and does not repair missing results in previously closed steps.
SESSION_FORMAT_VERSIONnames the current logical representation — the current reader rejects retiredheader.systemand validatessystem/messagepayloads and protected-head rewrites. Historical headers and events belong to adjacent format packages; the adjacent migration chain converts supported history before constructingSession, and the write-open path publishes only the current-format successor. Equal-version unknown events require the envelope's explicitignorablemarker, which does not promise safe structural migration (mechanism). Reader compatibility determines whether a change needs a version increase.TurnEndReasonMapomits the ACP-namedrefusal/max_turn_requestsvariants — producer-gated: they land when an adapter or the loop first emits them.- No session tree beyond fork — a pi-style entry tree over branched sessions is deferred unless a consumer needs more than boundary-based forking.
Dev Note
Working context for maintainers — click to expand
None.