refactor(session-persistence): extract a shared write coordinator

The JSONL and SQLite backends were byte-identical (or same-algorithm) for ALL
of their write-path orchestration — the four maps (states/buffers/chains/inits),
installWritePath, initFor, onCreated's four adoption cases, flush, drain,
serialize, adopt/adoptLivePrefix, assertVersion, and the create/append/load/
has/delete skeletons. Only the storage primitives (write bytes vs INSERT rows)
differed, so every fix landed twice.

Extract that orchestration into a PersistenceCoordinator in the seam package.
Each backend composes one (new PersistenceCoordinator(ctx, this)), implements a
small PersistenceBackend hook interface (loadStored, loadLive, appendBatch,
commitRepair, deleteStored, list, optional close), and delegates its six public
service methods to it. Composition, not inheritance — a backend exposes only the
hooks, can't reach the coordinator's private state, and the public
SessionPersistence API is unchanged so a third-party backend may still implement
it directly.

The crash-repair torn-tail token is OPAQUE: the coordinator computes the
synthetic closers (it owns interruptedTurnClosers) but only tests
`tornMarker !== undefined` and round-trips it to commitRepair, never inspecting
it (JSONL = byte offset, SQLite = seq). loadStored vs loadLive stay distinct so
HMR adoption is cwd-scoped (a same-id log at a different cwd is a collision, not
a resume). appendBatch carries meta so lazy-materialize + first-batch commit
atomically (no separate materialize hook).

Tests: the duplicated orchestration tests (adoption, HMR, collision,
dispose-drain, crash-tail) move into one runCoordinatorContract suite run once
per backend (memory + jsonl + sqlite) via hook fixtures; per-backend specs keep
only storage mechanics. A through-coordinator torn-tail test per real backend
keeps the commitRepair-with-marker branch covered under the 100% gate.

Net -112 lines (the dedup outweighs the new coordinator + shared suite); 100%
coverage; backends shrank ~1200 lines of duplicated churn. Migrates the
write-coordinator RFC proposed -> implemented.
This commit is contained in:
Tianyi Cui
2026-06-20 03:47:28 +08:00
parent 31af23b4fe
commit ab02e9acec
13 changed files with 1879 additions and 1991 deletions

View File

@@ -1,19 +1,18 @@
/**
* SQLite durable session-persistence backend (`@deepseek-ai/dsh-session-persistence-sqlite`).
*
* A SECOND {@link SessionPersistence} implementation, built to validate that
* the abstract seam + the shared `runPersistenceContract` suite are genuinely
* A SECOND {@link SessionPersistence} implementation, built to validate that the
* abstract seam + the shared `runPersistenceContract` suite are genuinely
* backend-agnostic: the same append-only / contiguous-seq / lazy-materialization
* / interrupted-turn-close-on-load semantics the JSONL backend expresses over
* file bytes, expressed here over `node:sqlite` rows. Each `SessionEvent` maps
* 1:1 onto a row `(session_id, seq, type, time, data)`; `append` is an INSERT
* inside a transaction that asserts the contiguous-seq contract.
* 1:1 onto a row `(session_id, seq, type, time, data)`.
*
* Like the JSONL backend it is also the write-path plugin: it installs the
* `session/event` → buffer → `session/flush` drain, persists a fork's seed once
* on `session/created`, keeps a per-session write cursor so a resumed session
* never re-appends stored events, and seeds existing live sessions on apply
* (HMR does not replay `session/created`).
* Like the JSONL backend it supplies ONLY the storage primitives (the
* {@link PersistenceBackend} hooks below — INSERT/DELETE/SELECT inside
* transactions); all the write-path orchestration lives in the backend-agnostic
* {@link PersistenceCoordinator} this class composes. The six public
* {@link SessionPersistence} methods delegate to the coordinator.
*
* @module @deepseek-ai/dsh-session-persistence-sqlite
*/
@@ -24,9 +23,9 @@ import { DatabaseSync } from 'node:sqlite'
import { mkdir } from 'node:fs/promises'
import { dirname, resolve } from 'node:path'
import {
SessionPersistence, assertSerializable, seedCoversPrefix,
SessionPersistence, PersistenceCoordinator,
type PersistenceBackend, type StoredPrefix,
} from '@deepseek-ai/dsh-session-persistence'
import { interruptedTurnClosers } from '@deepseek-ai/dsh-session'
import type { Session, SessionEvent, SessionId, SessionHeader } from '@deepseek-ai/dsh-session'
import {
openDatabase, rowToMeta, scanRows, type EventRow, type SessionRow,
@@ -44,55 +43,36 @@ export interface Config {
path: string
}
/** Backend bookkeeping for a session id (NOT the live Session object). */
interface SessionState {
meta: SessionHeader
/** Next seq to write — equals the number of committed events. */
cursor: number
/** Whether the session has at least one persisted event (materialized). */
materialized: boolean
/** The live Session that owns this state (collision detection); see onCreated. */
owner?: Session
}
async function settledErrors(promises: Iterable<Promise<unknown>>): Promise<unknown[]> {
const settled = await Promise.allSettled([...promises])
const errors: unknown[] = []
for (const result of settled) {
if (result.status === 'rejected') errors.push(result.reason)
}
return errors
}
/**
* The SQLite persistence backend. Load as a plugin; it registers as
* `ctx.sessionPersistence` and installs the write-path listeners.
* `ctx.sessionPersistence` and (via the coordinator) installs the write-path
* listeners. Its torn-tail marker is the seq to delete from.
*/
export class SessionPersistenceSqlite extends SessionPersistence {
export class SessionPersistenceSqlite extends SessionPersistence implements PersistenceBackend<number> {
static inject = ['sessions']
static Config: z<Config> = z.object({
path: z.string().required(),
})
/**
* Backend label for the coordinator's dispose diagnostics. Intentionally
* shadows cordis `Service.name` (set to `'sessionPersistence'` by the base);
* see the JSONL backend for why this does not affect service resolution.
*/
override readonly name = 'session-persistence-sqlite'
private db!: DatabaseSync
private ready: Promise<void>
/** Backend bookkeeping keyed by session id (NOT the live Session object). */
private states = new Map<string, SessionState>()
/** Write-behind buffers keyed by the live Session (write path). */
private buffers = new Map<Session, SessionEvent[]>()
/** Per-session serialization chain (keyed by session id). */
private chains = new Map<string, Promise<unknown>>()
/** Per-session init promise (onCreated), keyed by the LIVE Session object. */
private inits = new Map<Session, Promise<void>>()
private coordinator: PersistenceCoordinator<number>
constructor(ctx: Context, public config: Config) {
super(ctx)
// Open the database asynchronously (the parent directory may need creating);
// every backend op awaits `ready` first. Opening synchronously in the ctor
// would force a sync mkdir and block plugin apply.
// every hook awaits `ready` first. Opening synchronously would force a sync
// mkdir and block plugin apply.
this.ready = this.openDb(config.path)
this.installWritePath()
this.coordinator = new PersistenceCoordinator<number>(this.ctx, this)
}
private async openDb(path: string): Promise<void> {
@@ -105,229 +85,156 @@ export class SessionPersistenceSqlite extends SessionPersistence {
}
}
// --- SessionPersistence backend surface (all serialized per session id) ---
// --- SessionPersistence service surface (delegated to the coordinator) ---
create(meta: SessionHeader): Promise<void> {
const snapshot: SessionHeader = { ...meta }
return this.serialize(snapshot.id, () => this.createCore(snapshot))
return this.coordinator.create(meta)
}
private async createCore(meta: SessionHeader): Promise<void> {
append(id: SessionId, events: readonly SessionEvent[]): Promise<void> {
return this.coordinator.append(id, events)
}
load(id: SessionId): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
return this.coordinator.load(id)
}
has(id: SessionId): Promise<boolean> {
return this.coordinator.has(id)
}
delete(id: SessionId): Promise<void> {
return this.coordinator.delete(id)
}
// `list` is BOTH the public service method and the PersistenceBackend hook —
// one method (the SELECT below). The coordinator adds no orchestration for
// listing, so routing it through the coordinator would just recurse. Defined
// once, in the "PersistenceBackend hooks" section.
/**
* The per-session init promises, exposed for white-box tests that await a
* specific session's onCreated (there is no public API to await one init).
*/
get inits(): Map<Session, Promise<void>> {
return this.coordinator.inits
}
// --- PersistenceBackend hooks (the SQLite storage primitives) ---
/** Read a stored prefix by id (ids are globally unique — no scope to scan). */
loadStored(id: SessionId): Promise<StoredPrefix<number> | undefined> {
return this.readPrefix(id)
}
/** Read a stored prefix; `cwd` is ignored (the id is globally unique in SQLite). */
loadLive(id: SessionId, _cwd: string | undefined): Promise<StoredPrefix<number> | undefined> {
return this.readPrefix(id)
}
/**
* Read a session's row + ordered events into a {@link StoredPrefix}. The
* torn-tail marker is the seq from which a never-committed tail must be deleted
* (`scanRows` already returns it as `number | undefined`).
*/
private async readPrefix(id: SessionId): Promise<StoredPrefix<number> | undefined> {
await this.ready
if (this.states.has(meta.id)) {
throw new Error(`session "${meta.id}" already exists in this backend`)
}
if (this.rowFor(meta.id) !== undefined) {
throw new Error(`session "${meta.id}" already has a persisted row; load/resume it instead of creating`)
}
// Lazy: record intent in memory only. No row until the first append, so an
// abandoned (never-appended) session leaves nothing behind and stays absent
// from has()/list().
this.states.set(meta.id, { meta, cursor: 0, materialized: false })
const row = this.rowFor(id)
if (row === undefined) return undefined
const meta = rowToMeta(row)
const eventRows = this.db
.prepare('SELECT seq, type, time, data FROM events WHERE session_id = ? ORDER BY seq')
.all(id) as unknown as EventRow[]
const { preserved, tornFrom } = scanRows(eventRows)
return { meta, events: preserved, ...tornFrom !== undefined ? { tornMarker: tornFrom } : {} }
}
// `async` so the synchronous validate/clone below reject (not throw) per the
// Promise<void> contract — callers use `await expect(...).rejects`.
async append(id: SessionId, events: readonly SessionEvent[]): Promise<void> {
// Validate serializability BEFORE cloning so a bad event surfaces the typed
// "non-JSON-serializable" error rather than an opaque DataCloneError from
// structuredClone. Then deep-snapshot the batch HERE, before the op waits
// behind the per-session chain: a caller that passes a live array (e.g.
// session.events) and mutates it — OR mutates an event inside it — before
// the op runs would otherwise have those changes persisted, or advance the
// cursor past what was written. The clone is taken at call time (before the
// first await), matching the JSONL backend.
assertSerializable(events)
const batch = events.map(e => structuredClone(e))
return this.serialize(id, () => this.appendCore(id, batch))
}
private async appendCore(id: SessionId, events: readonly SessionEvent[]): Promise<void> {
/**
* Durably append a batch in ONE transaction: materialize the sessions row (if
* lazy) and INSERT every event, or roll back entirely. The transaction is the
* atomicity + durability boundary, so a mid-batch failure (a UNIQUE violation
* on a duplicated seq) leaves the stored log untouched.
*/
async appendBatch(meta: SessionHeader, events: readonly SessionEvent[], isMaterialized: boolean): Promise<void> {
await this.ready
if (events.length === 0) return
let state = this.states.get(id)
if (state === undefined) state = await this.adopt(id)
// Contiguity contract: each event's seq must continue the stored log.
for (const [i, event] of events.entries()) {
if (event.seq !== state.cursor + i) {
throw new Error(`append seq mismatch for "${id}": expected ${state.cursor + i} at index ${i}, got ${event.seq}`)
}
}
// The transaction is the durability + atomicity boundary: materialize the
// sessions row (if lazy) and INSERT every event, or roll back entirely. A
// BEGIN/COMMIT around the batch means a mid-batch failure (a UNIQUE
// violation on a duplicated seq from a concurrent writer) leaves the stored
// log untouched, so the cursor stays truthful and a retry is clean. (A crash
// tail is already gone: load() physically deletes the torn fragment and
// durably closes the interrupted turn before returning, so by the time any
// append runs the stored log is balanced and contiguous.)
const insertEvent = this.db.prepare(
'INSERT INTO events (session_id, seq, type, time, data) VALUES (?, ?, ?, ?, ?)',
)
this.db.exec('BEGIN')
try {
if (!state.materialized) this.writeRow(state.meta)
if (!isMaterialized) this.writeRow(meta)
for (const event of events) {
insertEvent.run(id, event.seq, event.type, event.time, JSON.stringify(event.data))
insertEvent.run(meta.id, event.seq, event.type, event.time, JSON.stringify(event.data))
}
this.db.exec('COMMIT')
} catch (error) {
this.db.exec('ROLLBACK')
throw error
}
state.materialized = true
state.cursor += events.length
}
load(id: SessionId): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
return this.serialize(id, () => this.loadCore(id))
}
private async loadCore(id: SessionId): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
/**
* Make a crash repair durable in ONE transaction: DELETE the torn tail (from
* `tornMarker`) and INSERT the synthetic `closers`. After COMMIT the stored rows
* == the balanced log.
*/
async commitRepair(meta: SessionHeader, tornMarker: number | undefined, closers: readonly SessionEvent[]): Promise<void> {
await this.ready
const row = this.rowFor(id)
if (row === undefined) throw new Error(`session "${id}" not found`)
const meta = rowToMeta(row)
this.assertVersion(meta)
// Read every stored row ordered by seq, then scan for the preserved prefix:
// the longest seq-contiguous, parseable run, INCLUDING the real events of an
// interrupted final turn after the last turn/end (a turn can be huge — they
// are never truncated). scanRows works off the seq+type COLUMNS for the
// last-turn/end boundary, so a malformed `data` in a torn tail row is
// discarded (not unloadable); only a parse error / seq gap in the COMMITTED
// region (at or before the last turn/end) throws (genuine corruption).
const eventRows = this.db
.prepare('SELECT seq, type, time, data FROM events WHERE session_id = ? ORDER BY seq')
.all(id) as unknown as EventRow[]
const { preserved, tornFrom } = scanRows(eventRows)
// Crash-recovery (mutating load, same as the JSONL backend): if the log ended
// mid-turn, close it DURING load so disk, the returned log, and the cursor all
// agree — both append routes then continue with no special-casing. Synthesize
// the boundary events (a step/end if a step was open, then a
// turn/end {kind:'interrupted'}); the interrupted turn's real events are
// preserved, never truncated (the session-persistence RFC).
const closers = interruptedTurnClosers(preserved)
const balanced = [...preserved, ...closers]
// Physically repair the stored log inside one transaction: DELETE the torn
// tail fragment (if any), then INSERT the synthetic closers. After COMMIT the
// stored rows == balanced, so the cursor is truthful and the next append
// continues cleanly with no deferred repair. The metadata row stays as-is
// even when preserved.length === 0 (an all-tail crash): the session WAS
// materialized by the partial append, so has()/list() still report it — the
// same as the JSONL backend, whose file likewise survives a first append that
// never reached turn/end.
if (tornFrom !== undefined || closers.length > 0) {
this.db.exec('BEGIN')
try {
if (tornFrom !== undefined) {
this.db.prepare('DELETE FROM events WHERE session_id = ? AND seq >= ?').run(id, tornFrom)
}
if (closers.length > 0) {
const insertEvent = this.db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, ?, ?, ?, ?)')
for (const event of closers) {
insertEvent.run(id, event.seq, event.type, event.time, JSON.stringify(event.data))
}
}
this.db.exec('COMMIT')
} catch (error) {
// The DELETE+INSERT cannot collide (a row at a closer's seq is preserved
// or deleted as torn first); this rolls back a DB-level failure (disk
// full, etc.), unreachable in test.
/* v8 ignore start */
this.db.exec('ROLLBACK')
throw error
/* v8 ignore stop */
this.db.exec('BEGIN')
try {
if (tornMarker !== undefined) {
this.db.prepare('DELETE FROM events WHERE session_id = ? AND seq >= ?').run(meta.id, tornMarker)
}
if (closers.length > 0) {
const insertEvent = this.db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, ?, ?, ?, ?)')
for (const event of closers) {
insertEvent.run(meta.id, event.seq, event.type, event.time, JSON.stringify(event.data))
}
}
this.db.exec('COMMIT')
} catch (error) {
// The DELETE+INSERT cannot collide (a row at a closer's seq is preserved or
// deleted as torn first); this rolls back a DB-level failure (disk full,
// etc.), unreachable in test.
/* v8 ignore start */
this.db.exec('ROLLBACK')
throw error
/* v8 ignore stop */
}
// Record state at the balanced length. The state keeps its OWN copy of the
// meta; the returned value is separate so a consumer mutating loaded.meta
// cannot corrupt the backend's row metadata.
this.states.set(id, {
meta: { ...meta },
cursor: balanced.length,
materialized: true,
})
return { meta, events: balanced }
}
private async adoptLiveStoredPrefix(session: Session, seed: readonly SessionEvent[]): Promise<void> {
/** Remove a session's row (ON DELETE CASCADE drops its events). */
async deleteStored(id: SessionId): Promise<void> {
await this.ready
const row = this.rowFor(session.header.id)
/* v8 ignore next -- caller checked row existence */
if (row === undefined) throw new Error(`session "${session.header.id}" not found`)
const meta = rowToMeta(row)
this.assertVersion(meta)
const rows = this.db
.prepare('SELECT seq, type, time, data FROM events WHERE session_id = ? ORDER BY seq')
.all(session.header.id) as unknown as EventRow[]
const { preserved, tornFrom } = scanRows(rows)
if (!seedCoversPrefix(seed, preserved)) {
throw new Error(`session "${session.header.id}" already has a persisted log that does not match this live session (id collision)`)
}
if (tornFrom !== undefined) {
this.db.prepare('DELETE FROM events WHERE session_id = ? AND seq >= ?').run(session.header.id, tornFrom)
}
this.states.set(session.header.id, {
meta: { ...meta },
cursor: preserved.length,
materialized: true,
owner: session,
})
const suffix = seed.slice(preserved.length)
if (suffix.length > 0) await this.appendCore(session.header.id, suffix)
this.db.prepare('DELETE FROM sessions WHERE id = ?').run(id)
}
/** List all materialized sessions' metadata (every row is a materialized session). */
async list(): Promise<SessionHeader[]> {
await this.ready
// Every metadata row is a materialized session: the row is written only by
// the first append (a created-but-never-appended session has no row), so
// listing all rows is exactly the materialized set.
const rows = this.db
.prepare('SELECT * FROM sessions')
.all() as unknown as SessionRow[]
return rows.map(rowToMeta)
}
async has(id: SessionId): Promise<boolean> {
/** Close the database handle (awaited by the coordinator's dispose, post-drain). */
async close(): Promise<void> {
await this.ready
const state = this.states.get(id)
if (state?.materialized) return true
// A metadata row exists iff the session was materialized by a first append.
return this.rowFor(id) !== undefined
}
delete(id: SessionId): Promise<void> {
return this.serialize(id, () => this.deleteCore(id))
}
private async deleteCore(id: SessionId): Promise<void> {
await this.ready
// ON DELETE CASCADE drops the session's events with its row.
this.db.prepare('DELETE FROM sessions WHERE id = ?').run(id)
this.states.delete(id)
this.db.close()
}
// --- row helpers ---
/** Fetch a session's row, or undefined if absent. */
private rowFor(id: SessionId): SessionRow | undefined {
const row = this.db.prepare('SELECT * FROM sessions WHERE id = ?').get(id) as unknown as SessionRow | undefined
return row
return this.db.prepare('SELECT * FROM sessions WHERE id = ?').get(id) as unknown as SessionRow | undefined
}
/**
* Insert-or-replace a session's metadata row. The only caller is the first
* materializing `append`, so writing the row IS the materialization (its
* existence is the signal `has`/`list` read); a never-appended session has no
* row at all.
* materializing `appendBatch`, so writing the row IS the materialization (its
* existence is the signal `has`/`list` read).
*/
private writeRow(meta: SessionHeader): void {
this.db.prepare(`
@@ -346,190 +253,6 @@ export class SessionPersistenceSqlite extends SessionPersistence {
meta.parentSession ?? null,
)
}
/** Build a state for a session present in the DB but not yet in memory. */
private async adopt(id: SessionId): Promise<SessionState> {
await this.loadCore(id) // sets the state; load (serialized) would deadlock
const state = this.states.get(id)
/* v8 ignore next -- loadCore always sets the state for the id */
if (!state) throw new Error(`failed to adopt session "${id}"`)
return state
}
private assertVersion(meta: SessionHeader): void {
if (meta.version !== 1) {
throw new Error(`unsupported session format version ${meta.version} for "${meta.id}" (only v1 is supported)`)
}
}
/**
* Run `op` after any in-flight operation for the same session id, so writes
* for one session never interleave. Errors do not poison the chain. NOTE:
* serialized public methods must NOT call each other (deadlock); they call
* the unserialized `*Core` helpers instead.
*/
private serialize<T>(id: SessionId, op: () => Promise<T>): Promise<T> {
const prior = this.chains.get(id) ?? Promise.resolve()
const next = prior.then(op, op)
this.chains.set(id, next.then(() => undefined, () => undefined))
return next
}
// --- write path (session/event → flush drain) ---
private installWritePath(): void {
const ctx = this.ctx
ctx.on('session/created', (session) => { void this.initFor(session) })
// Snapshot + buffer every event (the live object is mutable; clone so a
// later in-place mutation cannot rewrite a buffered event). Serializability
// is guaranteed at the source (Session.append), so structuredClone is safe.
ctx.on('session/event', (session, event) => {
let buffer = this.buffers.get(session)
if (!buffer) this.buffers.set(session, buffer = [])
buffer.push(structuredClone(event))
})
ctx.on('session/flush', session => this.flush(session))
// Dispose must reach quiescence: await every init + final drain, then close
// the database, BEFORE returning, so no write lands after teardown.
ctx.effect(() => async () => {
let disposeError: unknown
try {
const errors = [
...await settledErrors(this.inits.values()),
...await settledErrors([...this.buffers.keys()].map(s => this.flush(s))),
...await settledErrors(this.chains.values()),
]
if (errors.length > 0) {
throw new AggregateError(errors, 'session-persistence-sqlite dispose failed')
}
} catch (error: unknown) {
disposeError = error
throw error
} finally {
try {
await this.ready
this.db.close()
} catch (error: unknown) {
/* v8 ignore next -- open/close failure racing disposal is a defensive teardown edge */
if (disposeError === undefined) throw error
// Opening/closing the database can only add teardown context here; keep
// the already-captured init/flush/chain AggregateError as the primary
// disposal failure instead of masking it from callers.
}
}
}, 'session-persistence-sqlite write path')
// HMR: a hot reload does not replay session/created, so seed existing live
// sessions (mirrors dsh-invariants and the JSONL backend).
for (const session of ctx.sessions.list()) void this.initFor(session)
}
/** Start (once) the async init for a session and remember its promise. */
private initFor(session: Session): Promise<void> {
const existing = this.inits.get(session)
if (existing) return existing
const seed = session.events.map(e => structuredClone(e))
const p = this.onCreated(session, seed)
p.catch(() => { /* observed by flush/dispose via the stored promise */ })
this.inits.set(session, p)
return p
}
/**
* On session/created: sync the backend's state to a live Session. Cases
* mirror the JSONL backend:
* 1. Already tracked → no-op (or claim ownerless state if the seed matches).
* 2. A row EXISTS and is a seq-aligned PREFIX of the live events → adopt
* (HMR/resume), persisting any live suffix beyond the stored prefix.
* 3. A row EXISTS but is NOT a prefix → reject (id collision).
* 4. No row → a genuinely new session: register meta (lazy) + persist seed.
*/
private async onCreated(session: Session, seed: readonly SessionEvent[]): Promise<void> {
await this.ready
const id = session.header.id
const tracked = this.states.get(id)
if (tracked !== undefined) {
/* v8 ignore next -- initFor dedupes per session object; same-object re-entry can't occur */
if (tracked.owner === session) return
if (tracked.owner === undefined) {
// Ownerless state from a public create()/load(). The first live session
// claims it ONLY if its seed reproduces the persisted prefix.
if (!await this.seedMatchesPersisted(id, seed, tracked.cursor)) {
throw new Error(`session "${id}" is already persisted with ${tracked.cursor} event(s) that do not match this live session (id collision)`)
}
tracked.owner = session
const suffix = seed.slice(tracked.cursor)
if (suffix.length > 0) await this.append(id, suffix)
return
}
// Owned by a DIFFERENT live session. Reclaim ONLY a truly-abandoned id
// (never materialized, no pending buffer); else it is a real collision.
const ownerBuffer = this.buffers.get(tracked.owner)
if (!tracked.materialized && !ownerBuffer?.length) {
this.states.delete(id)
} else {
throw new Error(`session "${id}" is already bound to a different live session in this backend (id collision)`)
}
}
const row = this.rowFor(id)
if (row !== undefined) {
// Adopt a LIVE prefix without crash-repairing an open turn as interrupted;
// HMR may still append the real completion from the live Session.
await this.serialize(id, () => this.adoptLiveStoredPrefix(session, seed))
return
}
// case 4: a genuinely new session.
const meta: SessionHeader = { ...session.header }
await this.create(meta)
const created = this.states.get(id)
/* v8 ignore next -- create() always sets the state for the id */
if (created !== undefined) created.owner = session
if (seed.length > 0) await this.append(id, seed)
}
/** The preserved events for a session id (torn tail excluded, turn NOT yet closed). */
private eventsFor(id: SessionId): SessionEvent[] {
const rows = this.db
.prepare('SELECT seq, type, time, data FROM events WHERE session_id = ? ORDER BY seq')
.all(id) as unknown as EventRow[]
// Scan on seq+type columns, parsing `data` only for the preserved prefix (a
// malformed torn tail must not throw here — same as loadCore). Returns the
// preserved events WITHOUT the synthetic closers, so a collision check
// compares a live seed against the real on-disk events, mirroring the JSONL
// backend's scanLog use in onCreated.
return scanRows(rows).preserved
}
/** Whether a live session's seed reproduces the first `cursor` stored events. */
private async seedMatchesPersisted(id: SessionId, seed: readonly SessionEvent[], cursor: number): Promise<boolean> {
await this.ready
if (cursor === 0) return true
return seedCoversPrefix(seed, this.eventsFor(id).slice(0, cursor))
}
private async flush(session: Session): Promise<void> {
await this.inits.get(session)
await this.serialize(session.header.id, () => this.drain(session))
}
/** Drain a session's write buffer to the database. Caller serializes per id. */
private async drain(session: Session): Promise<void> {
const buffer = this.buffers.get(session)
if (!buffer?.length) return
const batch = buffer.slice()
const state = this.states.get(session.header.id)
/* v8 ignore next -- state is always set by the awaited init before flush */
const cursor = state?.cursor ?? 0
const fresh = batch.filter(e => e.seq >= cursor)
if (fresh.length > 0) await this.appendCore(session.header.id, fresh)
buffer.splice(0, batch.length)
}
}
export default SessionPersistenceSqlite

View File

@@ -3,11 +3,12 @@ import { Context } from 'cordis'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
import type { Session, SessionEvent, SessionHeader } from '@deepseek-ai/dsh-session'
import SessionStore from '@deepseek-ai/dsh-session'
import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
import SessionPersistenceSqlite, { SCHEMA_VERSION } from '@deepseek-ai/dsh-session-persistence-sqlite'
import { openDatabase, scanRows, type EventRow } from '../src/schema.ts'
import { runPersistenceContract, meta, oneTurnLog } from '../../session-persistence/tests/contract.ts'
import { runCoordinatorContract, type CoordinatorFixture } from '../../session-persistence/tests/coordinator-contract.ts'
const dirs: string[] = []
afterEach(async () => { for (const d of dirs.splice(0)) await rm(d, { recursive: true, force: true }) })
@@ -38,6 +39,31 @@ runPersistenceContract('sqlite', async () => {
}
})
// Run the shared coordinator orchestration suite against the real SQLite backend.
// A FILE-backed db (not :memory:) is the shared storage scope so two mounted
// instances see the same rows (HMR/reload). `corruptTail` INSERTs a row past the
// committed seq whose `data` is invalid JSON — a never-committed torn tail that
// drives the coordinator's commitRepair-with-tornMarker branch over real db rows.
runCoordinatorContract('sqlite', async (): Promise<CoordinatorFixture> => {
const dir = await mkdtemp(join(tmpdir(), 'dsh-sqlite-coord-'))
const path = join(dir, 'sessions.db')
return {
mount: async ctx => ctx.plugin(SessionPersistenceSqlite, { path }),
corruptTail: async (id) => {
// A row past the committed region whose `data` does not parse: scanRows
// bounds the preserved prefix at it and returns its seq as tornFrom, which
// the backend surfaces to the coordinator as the tornMarker to delete from.
const db = openDatabase(path)
const next = (db.prepare('SELECT COALESCE(MAX(seq), -1) + 1 AS n FROM events WHERE session_id = ?')
.get(id) as { n: number }).n
db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, ?, ?, ?, ?)')
.run(id, next, 'assistant/chunk', 99, '{not valid json')
db.close()
},
cleanup: async () => { await rm(dir, { recursive: true, force: true }) },
}
})
describe('scanRows', () => {
// scanRows works off EventRows (data is a JSON string column); build them from
// SessionEvents so the unit tests read in terms of the event vocabulary.
@@ -108,35 +134,6 @@ describe('scanRows', () => {
})
})
describe('SessionPersistenceSqlite: HMR adoption', () => {
it('does not crash-repair an active open turn as interrupted', async () => {
const path = await freshDbPath()
const ctx = new Context()
await ctx.plugin(SessionStore)
const first = await ctx.plugin(SessionPersistenceSqlite, { path })
const session = ctx.sessions.create('hmr-open', { meta: { cwd: '/hmr' } })
session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
session.append('step/start', { turn: 1, step: 1 })
await ctx.parallel('session/flush', session)
await first.dispose()
const db = openDatabase(path)
db.prepare('INSERT INTO events (session_id, seq, type, time, data) VALUES (?, ?, ?, ?, ?)')
.run('hmr-open', 2, 'step/end', 2, '{"torn":')
db.close()
const second = await ctx.plugin(SessionPersistenceSqlite, { path })
session.append('step/end', { turn: 1, step: 1 })
session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
await ctx.parallel('session/flush', session)
const loaded = await ctx.sessionPersistence.load(SessionId('hmr-open'))
expect(loaded.events.map(e => e.type)).toEqual(['turn/start', 'step/start', 'step/end', 'turn/end'])
expect(loaded.events.at(-1)).toMatchObject({ type: 'turn/end', data: { reason: { kind: 'completed' } } })
await second.dispose()
await ctx.fiber.dispose()
})
})
describe('SessionPersistenceSqlite: durability and crash semantics', () => {
it('an interrupted turn (rows after the last turn/end) is PRESERVED and closed during load', async () => {
const path = await freshDbPath()
@@ -251,31 +248,6 @@ describe('SessionPersistenceSqlite: durability and crash semantics', () => {
expect(() => openDatabase(olderPath)).toThrow(/incompatible with this build/)
})
it('append snapshots the batch: mutating an event after the call does not corrupt the persisted copy', async () => {
const ctx = new Context()
await ctx.plugin(SessionStore)
const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
const m = meta('snapshot')
await ctx.sessionPersistence.create(m)
const batch: SessionEvent[] = [
{ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
{ type: 'user/message', seq: 1, time: 2, data: { content: [{ type: 'text', text: 'original' }], source: { kind: 'user' } } },
{ type: 'turn/end', seq: 2, time: 3, data: { turn: 1, reason: { kind: 'completed' } } },
]
const p = ctx.sessionPersistence.append(m.id, batch)
// Mutate the live array AND an event's data AFTER the call but before it
// drains behind the per-session chain. The snapshot taken at call time must
// shield the persisted copy.
;(batch[1]!.data as { content: { type: 'text'; text: string }[] }).content[0]!.text = 'HACKED'
batch.push({ type: 'user/message', seq: 3, time: 4, data: { content: [{ type: 'text', text: 'injected' }], source: { kind: 'user' } } })
await p
const loaded = await ctx.sessionPersistence.load(m.id)
expect(loaded.events).toHaveLength(3) // the pushed event was not persisted
const um = loaded.events[1]
expect(um?.type === 'user/message' && (um.data.content[0] as { text: string }).text).toBe('original')
await fiber.dispose()
})
it('a corrupt-JSON row in the uncommitted tail is discarded on load, not unloadable', async () => {
const path = await freshDbPath()
const m = meta('corrupt-tail')
@@ -344,183 +316,12 @@ describe('SessionPersistenceSqlite: durability and crash semantics', () => {
await fiber2.dispose()
})
it('rejects an unknown format version on load', async () => {
const path = await freshDbPath()
// Materialize a row with version 2 directly via the real schema.
const db = openDatabase(path)
db.prepare('INSERT INTO sessions (id, version, created_at) VALUES (?, ?, ?)')
.run('v2', 2, 1)
db.close()
const ctx = new Context()
await ctx.plugin(SessionStore)
const fiber = await ctx.plugin(SessionPersistenceSqlite, { path })
await expect(ctx.sessionPersistence.load(SessionId('v2'))).rejects.toThrow(/version 2/)
await fiber.dispose()
})
it('create rejects a duplicate id (in memory and on a persisted row)', async () => {
const path = await freshDbPath()
const m = meta('dup')
const ctx = new Context()
await ctx.plugin(SessionStore)
const fiber = await ctx.plugin(SessionPersistenceSqlite, { path })
await ctx.sessionPersistence.create(m)
// Same in-memory state.
await expect(ctx.sessionPersistence.create(m)).rejects.toThrow(/already exists/)
await ctx.sessionPersistence.append(m.id, oneTurnLog())
await fiber.dispose()
// A fresh instance over the same file sees the persisted row.
const ctx2 = new Context()
await ctx2.plugin(SessionStore)
const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
await expect(ctx2.sessionPersistence.create(m)).rejects.toThrow(/already has a persisted row/)
await fiber2.dispose()
})
it('exposes the schema version constant', () => {
expect(SCHEMA_VERSION).toBe(2)
})
})
describe('SessionPersistenceSqlite: write path (session/event → flush)', () => {
function send(session: Session, events: SessionEvent[]): void {
for (const e of events) session.append(e.type, e.data)
}
it('persists a turn appended through the live session on flush', async () => {
const ctx = new Context()
await ctx.plugin(SessionStore)
const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
const session = ctx.sessions.create('w1')
send(session, oneTurnLog())
await ctx.parallel('session/flush', session)
const loaded = await ctx.sessionPersistence.load(SessionId('w1'))
expect(loaded.events.map(e => e.type)).toEqual(oneTurnLog().map(e => e.type))
await fiber.dispose()
})
it('a resumed session does not re-append its seed', async () => {
const path = await freshDbPath()
// Run 1: persist a full turn through the live session.
const ctx1 = new Context()
await ctx1.plugin(SessionStore)
const fiber1 = await ctx1.plugin(SessionPersistenceSqlite, { path })
const s1 = ctx1.sessions.create('resume')
for (const e of oneTurnLog()) s1.append(e.type, e.data)
await ctx1.parallel('session/flush', s1)
await fiber1.dispose()
// Run 2: reconstruct the live session from the loaded log (seed), then add a
// second turn. The seed must NOT be re-appended (no UNIQUE collision), and
// the second turn continues the seq.
const ctx2 = new Context()
await ctx2.plugin(SessionStore)
const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
const { events } = await ctx2.sessionPersistence.load(SessionId('resume'))
const s2 = ctx2.sessions.create('resume', { seed: events })
s2.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
s2.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
await ctx2.parallel('session/flush', s2)
const reloaded = await ctx2.sessionPersistence.load(SessionId('resume'))
expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
await fiber2.dispose()
})
it('HMR: applying the plugin seeds existing live sessions', async () => {
const ctx = new Context()
await ctx.plugin(SessionStore)
const session = ctx.sessions.create('hmr')
for (const e of oneTurnLog()) session.append(e.type, e.data)
// Plugin applied AFTER the session already has events.
const fiber = await ctx.plugin(SessionPersistenceSqlite, { path: ':memory:' })
await ctx.parallel('session/flush', session)
expect(await ctx.sessionPersistence.has(SessionId('hmr'))).toBe(true)
await fiber.dispose()
})
it('dispose drains a pending buffer before closing the database', async () => {
const path = await freshDbPath()
const ctx = new Context()
await ctx.plugin(SessionStore)
const fiber = await ctx.plugin(SessionPersistenceSqlite, { path })
const session = ctx.sessions.create('drain')
for (const e of oneTurnLog()) session.append(e.type, e.data)
// No explicit flush — dispose must drain the buffer.
await fiber.dispose()
const ctx2 = new Context()
await ctx2.plugin(SessionStore)
const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
expect(await ctx2.sessionPersistence.has(SessionId('drain'))).toBe(true)
await fiber2.dispose()
})
it('rejects a different live session colliding on a persisted id', async () => {
const path = await freshDbPath()
const ctx1 = new Context()
await ctx1.plugin(SessionStore)
const fiber1 = await ctx1.plugin(SessionPersistenceSqlite, { path })
const s1 = ctx1.sessions.create('collide')
for (const e of oneTurnLog()) s1.append(e.type, e.data)
await ctx1.parallel('session/flush', s1)
await fiber1.dispose()
// A fresh, unrelated session reusing the id (no seed) must be rejected.
const ctx2 = new Context()
await ctx2.plugin(SessionStore)
const fiber2 = await ctx2.plugin(SessionPersistenceSqlite, { path })
const s2 = ctx2.sessions.create('collide')
s2.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
await expect(ctx2.parallel('session/flush', s2)).rejects.toThrow(/id collision/)
await fiber2.dispose()
})
})
describe('SessionPersistenceSqlite: edge cases', () => {
it('append of an empty batch is a no-op', async () => {
const { ctx, dispose } = await backend()
const m = meta('empty-batch')
await ctx.sessionPersistence.create(m)
await ctx.sessionPersistence.append(m.id, [])
expect(await ctx.sessionPersistence.has(m.id)).toBe(false) // still lazy
await dispose()
})
it('load rejects a missing session', async () => {
const { ctx, dispose } = await backend()
await expect(ctx.sessionPersistence.load(SessionId('nope'))).rejects.toThrow(/not found/)
await dispose()
})
it('delete of a non-existent session is a no-op', async () => {
const { ctx, dispose } = await backend()
await ctx.sessionPersistence.delete(SessionId('ghost'))
expect(await ctx.sessionPersistence.has(SessionId('ghost'))).toBe(false)
await dispose()
})
it('append adopts a session that exists only in the DB (fresh instance)', async () => {
const path = await freshDbPath()
const m = meta('adopt-append')
const b1 = await backend(path)
await b1.ctx.sessionPersistence.create(m)
await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
await b1.dispose()
// A fresh instance appends a second turn WITHOUT a prior create/load: append
// must adopt the on-disk row (cursor = stored length) and continue the seq.
const b2 = await backend(path)
await b2.ctx.sessionPersistence.append(m.id, [
{ type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
{ type: 'turn/end', seq: 7, time: 8, data: { turn: 2, reason: { kind: 'completed' } } },
])
const loaded = await b2.ctx.sessionPersistence.load(m.id)
expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
await b2.dispose()
})
it('append rolls back and rethrows when an event INSERT fails inside the transaction', async () => {
const path = await freshDbPath()
const m = meta('rollback-insert')
@@ -549,190 +350,6 @@ describe('SessionPersistenceSqlite: edge cases', () => {
await b2.dispose()
})
it('round-trips a header with parentSession (fork lineage)', async () => {
const { ctx, dispose } = await backend()
const m: SessionHeader = { ...meta('child'), parentSession: SessionId('parent') }
await ctx.sessionPersistence.create(m)
await ctx.sessionPersistence.append(m.id, oneTurnLog())
const loaded = await ctx.sessionPersistence.load(m.id)
expect(loaded.meta.parentSession).toBe(SessionId('parent'))
await dispose()
})
it('a fresh live session reusing a previously-loaded id is rejected (ownerless guard)', async () => {
const path = await freshDbPath()
const m = meta('ownerless')
const b1 = await backend(path)
await b1.ctx.sessionPersistence.create(m)
await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
await b1.dispose()
const b2 = await backend(path)
// load() leaves ownerless state with cursor 6.
await b2.ctx.sessionPersistence.load(m.id)
// A fresh, unrelated live session reusing the id has a shorter/non-matching
// seed → its onCreated must reject rather than graft onto the loaded prefix.
const s = b2.ctx.sessions.create('ownerless')
s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
await expect(b2.ctx.parallel('session/flush', s)).rejects.toThrow(/id collision/)
await b2.dispose()
})
it('a live session whose seed matches the loaded prefix claims ownerless state and persists the suffix', async () => {
const path = await freshDbPath()
const m = meta('claim')
const b1 = await backend(path)
await b1.ctx.sessionPersistence.create(m)
await b1.ctx.sessionPersistence.append(m.id, oneTurnLog())
await b1.dispose()
const b2 = await backend(path)
const { events } = await b2.ctx.sessionPersistence.load(m.id) // ownerless, cursor 6
// A live session seeded with the loaded log PLUS a new turn claims the state
// and persists only the suffix.
const s = b2.ctx.sessions.create('claim', { seed: [
...events,
{ type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } } },
{ type: 'turn/end', seq: 7, time: 8, data: { turn: 2, reason: { kind: 'completed' } } },
] })
await b2.ctx.parallel('session/flush', s)
const loaded = await b2.ctx.sessionPersistence.load(m.id)
expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
await b2.dispose()
})
it('an abandoned lazy session (never materialized) releases its id for reuse', async () => {
const { ctx, dispose } = await backend()
const inits = (ctx.sessionPersistence as unknown as { inits: Map<Session, Promise<void>> }).inits
let first!: Session
const firstFiber = await ctx.plugin(Object.assign((inner: Context) => {
first = inner.sessions.create('reuse')
}, { inject: ['sessions'] }))
await inits.get(first) // let the lazy create register the state
await firstFiber.dispose() // disposed before any append → never materialized
let reuse!: Session
await ctx.plugin(Object.assign((inner: Context) => {
reuse = inner.sessions.create('reuse')
}, { inject: ['sessions'] }))
await expect(inits.get(reuse)).resolves.toBeUndefined()
reuse.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
reuse.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
await ctx.parallel('session/flush', reuse)
expect(await ctx.sessionPersistence.has(SessionId('reuse'))).toBe(true)
await dispose()
})
it('does NOT reclaim an id whose abandoned owner still has buffered (unflushed) events', async () => {
const { ctx, dispose } = await backend()
const inits = (ctx.sessionPersistence as unknown as { inits: Map<Session, Promise<void>> }).inits
let first!: Session
const firstFiber = await ctx.plugin(Object.assign((inner: Context) => {
first = inner.sessions.create('buffered')
}, { inject: ['sessions'] }))
await inits.get(first)
// Append a turn but do NOT flush — events sit in the write-behind buffer.
first.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
first.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
await firstFiber.dispose() // disposed before flush; not materialized, buffer pending
let reuse!: Session
await ctx.plugin(Object.assign((inner: Context) => {
reuse = inner.sessions.create('buffered')
}, { inject: ['sessions'] }))
await expect(inits.get(reuse)).rejects.toThrow(/already bound to a different live session/)
await dispose()
})
it('initFor is idempotent: re-emitting session/created does not re-initialize', async () => {
const { ctx, dispose } = await backend()
const session = ctx.sessions.create('idem')
ctx.emit('session/created', session) // second create event for the same object
session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
await ctx.parallel('session/flush', session)
expect(await ctx.sessionPersistence.has(SessionId('idem'))).toBe(true)
await dispose()
})
it('a live session claims cursor-0 ownerless state created via the public API and persists its seed', async () => {
const { ctx, dispose } = await backend()
// create() registers ownerless state with cursor 0 (no events yet).
await ctx.sessionPersistence.create(meta('cursor0'))
// A live session reusing that id, seeded with a turn, claims the ownerless
// state (cursor 0 trivially matches any seed) and persists the whole seed.
const s = ctx.sessions.create('cursor0', { seed: [
{ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
{ type: 'turn/end', seq: 1, time: 2, data: { turn: 1, reason: { kind: 'completed' } } },
] })
await ctx.parallel('session/flush', s)
const loaded = await ctx.sessionPersistence.load(SessionId('cursor0'))
expect(loaded.events.map(e => e.seq)).toEqual([0, 1])
await dispose()
})
it('HMR: reloading the backend adopts a still-live, already-materialized session', async () => {
const path = await freshDbPath()
const ctx = new Context()
await ctx.plugin(SessionStore)
// The session lives in its OWN fiber so it survives the backend reload.
let session!: Session
await ctx.plugin(Object.assign((inner: Context) => {
session = inner.sessions.create('hmr-adopt')
}, { inject: ['sessions'] }))
// Backend instance 1 materializes the session on disk.
const backend1 = await ctx.plugin(SessionPersistenceSqlite, { path })
session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } })
session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
await ctx.parallel('session/flush', session)
// Hot-reload: dispose instance 1, plug in instance 2 over the SAME file
// while the session stays live. Instance 2 has an empty states map but the
// row is materialized on disk and is a prefix of the live events — it must
// ADOPT (not reject), and a second turn then persists.
await backend1.dispose()
await ctx.plugin(SessionPersistenceSqlite, { path })
session.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
session.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
await expect(ctx.parallel('session/flush', session)).resolves.not.toThrow()
const loaded = await ctx.sessionPersistence.load(SessionId('hmr-adopt'))
expect(loaded.events.filter(e => e.type === 'turn/start')).toHaveLength(2)
await ctx.fiber.dispose()
})
it('HMR: adoption persists the live SUFFIX that was ahead of the on-disk prefix', async () => {
const path = await freshDbPath()
const ctx = new Context()
await ctx.plugin(SessionStore)
let session!: Session
await ctx.plugin(Object.assign((inner: Context) => {
session = inner.sessions.create('hmr-suffix')
}, { inject: ['sessions'] }))
const backend1 = await ctx.plugin(SessionPersistenceSqlite, { path })
session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
await ctx.parallel('session/flush', session)
// Append turn 2 to the LIVE session, then dispose instance 1 WITHOUT
// flushing turn 2: it is now ONLY in the live session's events.
await backend1.dispose()
session.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
session.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
// Instance 2 adopts the on-disk prefix (turn 1) and MUST persist the live
// suffix (turn 2) carried in the session's events.
await ctx.plugin(SessionPersistenceSqlite, { path })
await ctx.parallel('session/flush', session)
const loaded = await ctx.sessionPersistence.load(SessionId('hmr-suffix'))
expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3])
expect(loaded.events.filter(e => e.type === 'turn/start')).toHaveLength(2)
await ctx.fiber.dispose()
})
it('HMR: a DIFFERENT session colliding with a materialized on-disk id is rejected', async () => {
const path = await freshDbPath()
// Instance 1 materializes a session and disposes.