Files
deepseek-harness/packages/session/session-persistence/src/index.ts
T
Kexin Liu e50e24de40 feat(session): 等父前缀持久化后创建共享 fork (#8392)
* 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.
2026-11-17 17:28:59 +08:00

237 lines
10 KiB
TypeScript

/**
* Durable session-persistence Service Definition (`ctx.sessionPersistence`). Backends store
* {@link SessionEvent}s as the event-sourced log and carry non-replayable
* {@link SessionHeader} metadata separately; callers address one stored
* session through a {@link SessionHandle} obtained from `create`/`open`.
* @module @deepseek-ai/dsh-session-persistence
*/
import { Context, Service } from '@deepseek-ai/cordis'
import type { SessionEvent, SessionHeader, SessionId, SessionLogOffset } from '@deepseek-ai/dsh-session'
import type { SessionHandle, SessionAccess } from './handle.ts'
import type { SessionLocation } from './errors.ts'
import type { SessionPersistenceRevision } from './revision.ts'
// Re-export the metadata vocabulary so Consumers import it from the Service Definition.
export type { SessionHeader } from '@deepseek-ai/dsh-session'
export { SessionPersistenceRevision } from './revision.ts'
export type {
SessionAccess,
SessionHandle,
SessionHandleAppendOptions,
SessionHandleFlushOptions,
SessionHandleReadOptions,
SessionHandleReadResult,
} from './handle.ts'
export {
SessionAlreadyExistsError,
SessionAlreadyOwnedError,
SessionFormatUnsupportedError,
SessionHandleClosedError,
SessionOwnershipLostError,
SessionPersistenceCorruptionError,
SessionPersistenceNotFoundError,
SessionReadOnlyError,
sessionFormatVersionRefusal,
} from './errors.ts'
export type { SessionLocation } from './errors.ts'
export {
assertContiguous,
assertStoredId,
assertVersion,
materializeAppendBatch,
materializeCreateHeader,
validateStoredEvents,
} from './storage-contract.ts'
/**
* Lightweight stored-session observation returned by {@link SessionPersistence.stat}
* and {@link SessionPersistence.list} without reading the full event log.
*/
export interface SessionPersistenceSnapshot {
/** Detached metadata for one stored session. */
readonly header: SessionHeader
/** Header-only format classification; absent without versioned artifact metadata. */
readonly formatStatus?: 'current' | 'migration-required'
/** Opaque change token; see {@link SessionPersistence.stat}. */
readonly revision: SessionPersistenceRevision
/** Logical event count, when the backend can provide it cheaply from metadata; otherwise absent. */
readonly eventCount?: number
/** Physical artifact byte size, when the backend can provide it cheaply (JSONL); otherwise absent. */
readonly sizeBytes?: number
/**
* Backend-resolved location of the selected materialized per-Session
* artifact, for diagnostics and export. Absent when the backend has no
* materialized per-Session file, including an unmaterialized pending Session.
*/
readonly location?: SessionLocation
}
/** Options for {@link SessionPersistence.create}. */
export interface SessionPersistenceCreateOptions {
/** Optional cancellation observed before backend work starts. */
readonly signal?: AbortSignal
/**
* Exact fork-inherited prefix length. Required when `header.isSeeded` is
* true and must be omitted (or `0`) otherwise; the backend refuses a
* mismatch at create.
*/
readonly inheritedEventCount?: SessionLogOffset
/**
* Fork history durably stored before creation resolves; omission creates an
* empty stored session. A backend that shares the parent's stored prefix
* requires the parent to exist with at least `endSeq` persisted events and
* writes only the events from `endSeq`; a copying backend writes all of
* them. When `header.isSeeded` is true, the events include an inherited
* `session/end-seed` marker at `inheritedEventCount`.
*/
readonly parentSource?: {
/** Parent Session identity. */
readonly sessionId: SessionId
/** Exclusive end of the parent prefix within `events`. */
readonly endSeq: SessionLogOffset
/** Complete child history from seq 0: the parent's stored prefix through `endSeq`, then the child's events. */
readonly events: readonly SessionEvent[]
}
}
/**
* Logical Session header paired with its exact inherited cut for body-bearing
* storage operations. `isSeeded` marks fork lineage on the header; the
* numeric cut travels beside it, never inside the replayable event log.
*/
export interface SessionStorageMetadata {
/** Validated immutable Session header. */
readonly meta: SessionHeader
/** Number of leading events inherited from the Session's fork parent. */
readonly inheritedEventCount: SessionLogOffset
}
/** Immutable logical session read: storage metadata plus the complete validated event log. */
export interface SessionInspection extends SessionStorageMetadata {
/** Contiguous validated events from seq 0. */
readonly events: readonly SessionEvent[]
}
/** Options for {@link SessionPersistence.open}. */
export interface SessionPersistenceOpenOptions {
/** Optional cancellation observed before backend work starts. */
readonly signal?: AbortSignal
}
/** Options for {@link SessionPersistence.stat}. */
export interface SessionPersistenceStatOptions {
/** Optional cancellation for backend metadata reads. */
readonly signal?: AbortSignal
}
/** Options for {@link SessionPersistence.list}. */
export interface SessionPersistenceListOptions {
/** Optional cancellation for backend listing work. */
readonly signal?: AbortSignal
}
declare module '@deepseek-ai/cordis' {
interface Context {
sessionPersistence: SessionPersistence
}
}
/**
* Durable append-only session storage addressed through per-session handles.
*
* Storage semantics shared by every backend: events are contiguous from seq 0
* and never rewritten; a torn physical tail is never returned to a reader and
* is truncated by the write path before its first append; reads validate
* current-format records only and refuse unknown vocabulary fail-closed.
* `append` persists best-effort; `flush` — per handle or service-wide — is
* the durability barrier.
*
* Visibility: a created session is observable through `stat`/`list`/`open`
* in this process from the moment `create` resolves, even while a backend
* defers physical materialization (a pure optimization); other processes see
* the session only once it materializes, and a session that never
* materialized before a crash never existed. `SessionHandle.flush` forces
* materialization.
*
* Freshness: once an `append` or `flush` resolves, reads started afterwards
* on this backend instance observe at least that prefix.
*/
export abstract class SessionPersistence extends Service {
/** Process-local instance identity, stable through Context proxies and distinct after service replacement. */
readonly identity: symbol = Symbol('sessionPersistence')
constructor(ctx: Context) {
super(ctx, 'sessionPersistence')
}
/**
* Create a new stored session and take its write ownership.
* With `parentSource`, store the complete fork history before resolving; a
* backend may share the parent's stored prefix or copy it. A failed creation
* releases acquired write ownership; already committed history remains stored.
* @param header - the immutable header (id, version, cwd, lineage) to store.
* @param options - cancellation, inherited-prefix count, and optional fork source.
* @returns a `write` handle owned by the caller; close it to release ownership.
* @throws {SessionAlreadyExistsError} when the id already exists.
* @throws {SessionPersistenceNotFoundError} when a sharing backend cannot find the fork parent.
* @throws {HarnessError} with `SESSION_FORK_PREFIX_NOT_PERSISTED` when a sharing backend finds the parent prefix not durable.
*/
abstract create(header: SessionHeader, options?: SessionPersistenceCreateOptions): Promise<SessionHandle>
/**
* Open an existing stored session.
*
* Returned `read` handles retain no write ownership and work while another
* handle (or process) holds write ownership. A first legacy read open may
* acquire ownership temporarily to import history into the selected backend.
* `write` atomically claims single-writer ownership;
* an existing active owner rejects.
* @param id - the stored session to open.
* @param access - `read` or `write`.
* @param options - optional cancellation.
* @returns the open handle.
* @throws {SessionPersistenceNotFoundError} when the session does not exist.
* @throws {SessionAlreadyOwnedError} for `write` when ownership is taken.
*/
abstract open(id: SessionId, access: SessionAccess, options?: SessionPersistenceOpenOptions): Promise<SessionHandle>
/**
* Flush every active write handle owned by this service instance in one
* durability barrier: each handle's routed live events drain durably and
* its session materializes, exactly as that handle's own
* `SessionHandle.flush` would. Read handles buffer nothing and are
* untouched. A handle closed concurrently counts as flushed — close itself
* drains durably.
* @returns resolution once every write handle active at the call has flushed.
* @throws {AggregateError} naming each session whose flush failed; the
* remaining handles still flush.
*/
abstract flush(): Promise<void>
/**
* Observe one stored session without reading its event log or taking
* ownership.
*
* The snapshot's `revision` is an opaque change token comparable only
* against revisions from the same service instance and session id: equal
* revisions may be treated as an unchanged log; unequal revisions promise
* nothing. Write-ownership churn does not change a revision. It exists for
* derived read-model caches keyed off `stat`/`list`; it plays no part in
* open, read, or resume.
* @param id - the stored session to observe.
* @param options - optional cancellation.
* @returns the snapshot, or `undefined` when the session does not exist.
*/
abstract stat(id: SessionId, options?: SessionPersistenceStatOptions): Promise<SessionPersistenceSnapshot | undefined>
/**
* List every stored session visible to this process, in no promised order.
* @param options - optional cancellation.
* @returns one snapshot per stored session.
*/
abstract list(options?: SessionPersistenceListOptions): Promise<readonly SessionPersistenceSnapshot[]>
}
export default SessionPersistence