diff --git a/src/storage/ingest.ts b/src/storage/ingest.ts index dc5ac1483b..ca27096ae9 100644 --- a/src/storage/ingest.ts +++ b/src/storage/ingest.ts @@ -28,7 +28,7 @@ import type { CoreMessage, CompressionBlock } from "../../kernel/src/types.js"; import type { BlockView } from "../session.js"; import { refToIndex } from "../../kernel/src/refs.js"; -import { sha256Hex, type BlockRow, type CcrEntryRow, type IngestSessionInput, type MessageRow, type RefRow, type SessionRow } from "./store.js"; +import { sha256Hex, type BlockRow, type CcrEntryRow, type DeltaRegistration, type IngestSessionInput, type MessageRow, type RefRow, type SeqState, type SessionRow } from "./store.js"; /** Field order of mergeState() output (src/persist.ts) — the loader must * rebuild the state object in exactly this order. Keep in sync. */ @@ -80,6 +80,11 @@ interface IngestPlan extends IngestSessionInput { /** Resolved log seqs for pluginSnapshot entries (null when absent). */ pluginSnapshotSeqs: number[] | null; tailSeqs: number[] | null; + /** #3133 incremental mode: identities this plan allocated fresh seqs for + * (plan.messages, with their ref tokens). The caller commits them to the + * per-session ledger AFTER ingestSession's transaction succeeds — the + * ledger must never know about rows that did not land. */ + newRegs?: DeltaRegistration[]; } /** Adapts any Session-shaped object (persist loadSync output) to the @@ -117,27 +122,79 @@ export function toLegacyLike(s: { /** Builds rows + pointer sequences. Pure: no database touched, so tests can * assert the mapping itself. seq assignment: first-seen over the ordered - * union (pluginSnapshot first — frozen raw prefix — then fresh tail ids). */ -export function planIngest(session: LegacySessionLike): IngestPlan { + * union (pluginSnapshot first — frozen raw prefix — then fresh tail ids). + * + * #3133 incremental mode (`seqState` bound): the per-session ledger claims + * known messages WITHOUT serializing them, so a steady-state save costs O(new + * messages) instead of O(full log) — the wire entry points re-parse the whole + * history into fresh objects every request, so object identity never survives + * across saves and only content-keyed claims work. Claim ladder (each rung + * fails soft to the next): + * 1. ref-token claim — the token occurs exactly ONCE in this save's union + * (split tool-call cores share a token across distinct content parts, so + * a repeated token is ambiguous by construction) AND the ledger holds a + * sole-identity claim for it whose msgId matches — zero serialization; + * 2. known-by-bytes — stringify+hash, but the identity is already in the + * ledger (token invalidated or absent): claim its seq, emit no row; + * 3. new identity — allocate the next absolute seq, emit a row, register + * the pending registration for post-commit ledger update. + * A token whose claimed msgId no longer matches is de-ambiguated PERMANENTLY + * (variants counter) — shared sentinels like "BLOCKED" change holders over + * time, and a stale claim would replay another message's bytes under this + * one's pointer until the sampled digest verify notices. Full mode + * (no `seqState`) is byte-identical to the pre-existing behavior: relative + * seqs, every identity serialized, no registrations. */ +export function planIngest(session: LegacySessionLike, seqState?: SeqState): IngestPlan { const byRawRef = session.state.messageRefs.byRaw; + const st = seqState; + const delta = st !== undefined; + + // Pass 0 (incremental only): ref-token occurrence counts over the ordered + // union — cheap string pass, no serialization. A token seen more than once + // in THIS save cannot be claimed (split cores / sentinel reuse). + const tokenCounts = new Map(); + if (delta) { + for (const m of [...(session.pluginSnapshot ?? []), ...(session.lastMessages ?? [])]) { + const t = byRawRef[m.id]; + if (t !== undefined) tokenCounts.set(t, (tokenCounts.get(t) ?? 0) + 1); + } + } // Pass 1: ordered union of identities (msg_id + content hash). - // #3133: no cross-save memo — the wire entry points replace the message - // array with freshly parsed objects on every request, so an object- - // identity WeakMap never hits on the production save path (see the - // issue for the content/seq watermark design that actually cuts cost). - const log: { msgId: string; content: string; hash: string; role: string; kind: string; ref: string | null }[] = []; + // seq travels with the entry: in full mode it equals the log index, but a + // delta plan logs ONLY the new rows, whose seqs are absolute cursor values. + const log: { msgId: string; content: string; hash: string; role: string; kind: string; ref: string | null; seq: number }[] = []; + const regs: DeltaRegistration[] = []; const seqByIdentity = new Map(); const intern = (m: CoreMessage): number => { + if (delta && st) { + const t = byRawRef[m.id]; + if (t !== undefined && tokenCounts.get(t) === 1 && (st.tokenVariants.get(t) ?? 0) <= 1) { + const claim = st.byRef.get(t); + if (claim !== undefined) { + if (claim.msgId === m.id) return claim.seq; + st.tokenVariants.set(t, (st.tokenVariants.get(t) ?? 1) + 1); + st.byRef.delete(t); + } + } + } const content = JSON.stringify(m); const parsed = JSON.parse(content) as { role: string; contentType?: string }; const entry = { msgId: m.id, content, hash: sha256Hex(content), role: parsed.role, kind: parsed.contentType ?? "message" }; const key = `${m.id}\u0000${entry.hash}`; const existing = seqByIdentity.get(key); if (existing !== undefined) return existing; - const seq = log.length; + if (delta && st) { + const known = st.byIdentity.get(key); + if (known !== undefined) { + seqByIdentity.set(key, known); + return known; + } + } + const seq = delta && st ? st.nextSeq++ : log.length; seqByIdentity.set(key, seq); - log.push({ msgId: m.id, content: entry.content, hash: entry.hash, role: entry.role, kind: entry.kind, ref: byRawRef[m.id] ?? null }); + log.push({ msgId: m.id, content: entry.content, hash: entry.hash, role: entry.role, kind: entry.kind, ref: byRawRef[m.id] ?? null, seq }); + if (delta) regs.push({ msgId: m.id, hash: entry.hash, seq, token: byRawRef[m.id] }); return seq; }; const pluginSnapshotSeqs = session.pluginSnapshot ? session.pluginSnapshot.map(intern) : null; @@ -152,8 +209,8 @@ export function planIngest(session: LegacySessionLike): IngestPlan { refNum: refToIndex(ref), })); - const messages: MessageRow[] = log.map((e, seq) => ({ - seq, + const messages: MessageRow[] = log.map((e) => ({ + seq: e.seq, msgId: e.msgId, ref: e.ref, refNum: e.ref === null ? null : refToIndex(e.ref), @@ -236,7 +293,18 @@ export function planIngest(session: LegacySessionLike): IngestPlan { ccrJson: session.contentStore ? JSON.stringify(session.contentStore) : null, }; - return { session: sessionRow, messages, blocks, deadRefs, refs, ccrEntries, pluginSnapshotSeqs, tailSeqs }; + return { + session: sessionRow, + messages, + blocks, + deadRefs, + refs, + ccrEntries, + pluginSnapshotSeqs, + tailSeqs, + delta, + ...(delta ? { newRegs: regs } : {}), + }; } /** Rebuilds the legacy load view from unified rows alone. */ diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index 19250f93b6..1e1ed2164a 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -49,6 +49,10 @@ interface ShadowStats { verifySamples: number; verifyOk: number; verifyDrift: number; + /** #3133: ingests that ran through the incremental (delta) path — + * observability for the steady-state cost claim, same semantics as + * verifySamples (counts RUNS, not sessions). */ + deltaSaves: number; imports: number; disabled: boolean; lastError: string | null; @@ -56,13 +60,20 @@ interface ShadowStats { export class UnifiedShadow { readonly mode: StorageMode; - readonly stats: ShadowStats = { ingests: 0, verifySamples: 0, verifyOk: 0, verifyDrift: 0, imports: 0, disabled: false, lastError: null }; + readonly stats: ShadowStats = { ingests: 0, verifySamples: 0, verifyOk: 0, verifyDrift: 0, deltaSaves: 0, imports: 0, disabled: false, lastError: null }; private readonly store: UnifiedStore; private readonly legacyLoad: (record: unknown) => LegacySessionLike | null; private consecutiveFailures = 0; /** #3133: per-session ingest counters (verify sampling). Per-process * only — nothing persisted, no schema change. */ private readonly ingestCounts = new Map(); + /** #3133 incremental mode: one-shot "skip the ledger" flags. Set after a + * sampled digest drift or a delta PK-conflict heal; the NEXT record for + * that session re-seeds a fresh ledger from the DB (full replay when the + * conflict path already dropped it) instead of trusting a possibly-stale + * claim map. Consumed on read — a session that never saves again keeps + * no state beyond the set entry. */ + private readonly forceFullNext = new Set(); private constructor(mode: StorageMode, store: UnifiedStore, legacyLoad: (record: unknown) => LegacySessionLike | null) { this.mode = mode; @@ -142,8 +153,35 @@ export class UnifiedShadow { // stay empty for every plain compressed session. Both digest legs // flow through the same override. if (liveCcr) legacy = { ...legacy, contentStore: liveCcr }; - const plan = planIngest(legacy); - this.store.ingestSession(plan); + // #3133 incremental ingest: bind the per-session seq ledger so + // known messages are claimed by token/identity instead of being + // re-serialized every save. A pending forceFull flag (drift or a + // previous conflict heal) skips the ledger for exactly this save — + // it is consumed on read, so the save after it resumes delta mode + // off a freshly seeded ledger. + const forceFull = this.forceFullNext.delete(legacy.id); + const st = forceFull ? undefined : this.store.seqStateFor(legacy.id) ?? undefined; + let plan = planIngest(legacy, st); + try { + this.store.ingestSession(plan); + } catch (err) { + // The only self-healable failure: a delta row collided with an + // externally written (session_id, seq). Drop the ledger, re-seed + // from the DB (which absorbs the foreign rows), and replay. Any + // other error propagates to the catch below (consecutive-failure + // accounting unchanged). + if (!(plan.delta && isConstraintError(err))) throw err; + loggerLog("warn", `[storage] unified shadow ${legacy.id}: delta ingest PK conflict — reseeding seq ledger and re-planning`); + this.store.dropSeqState(legacy.id); + this.forceFullNext.add(legacy.id); + plan = planIngest(legacy, this.store.seqStateFor(legacy.id) ?? undefined); + this.store.ingestSession(plan); + } + if (plan.delta) { + // Ledger commits only what the transaction actually wrote. + this.store.commitDelta(legacy.id, plan.newRegs ?? []); + this.stats.deltaSaves++; + } this.stats.ingests++; // #3133 sampled verification: ingest integrity is already guarded // by the SQLite write path; the read-back digest is a migration @@ -170,6 +208,12 @@ export class UnifiedShadow { // Panels consume {disabled, lastError} — make the sampled // degradation visible there, not just in the counter/log. this.stats.lastError = `digest drift for ${legacy.id}: unified replay != legacy load view`; + // Drift means some claim may have pinned a stale identity — + // drop the ledger so the next save replays fully (and the + // one after resumes delta off a fresh seed), instead of + // continuing to serve possibly-wrong bytes under old seqs. + this.store.dropSeqState(legacy.id); + this.forceFullNext.add(legacy.id); loggerLog("error", `[storage] unified shadow DIGEST DRIFT for ${legacy.id}: unified replay != legacy load view — migration parity broken, report this`); } else { this.stats.verifyOk++; @@ -230,6 +274,10 @@ export class UnifiedShadow { /** GC parity: remove the session's rows when the legacy files are deleted. */ remove(id: string): void { this.ingestCounts.delete(id); + // A recreated session id must re-seed from scratch — reusing the + // deleted session's ledger would alias its old seqs. + this.store.dropSeqState(id); + this.forceFullNext.delete(id); if (this.stats.disabled) return; try { this.store.deleteSession(id); @@ -384,3 +432,26 @@ export class UnifiedShadow { function shadowDigest(view: unknown): string { return sha256(JSON.stringify(canonicalize(view))); } + +/** #3133 incremental mode: row-exists constraint violations are the only + * delta-ingest failure class that is self-healable (a seq stolen by an + * external writer, not corruption) — record() re-seeds and replays instead of + * counting a sidecar failure. The engines report them differently: + * better-sqlite3 carries a string `code` (SQLITE_CONSTRAINT_PRIMARYKEY / + * _UNIQUE); node:sqlite carries a numeric extended result code in `errcode` + * (base 19 = SQLITE_CONSTRAINT | sub-class << 8; a composite PK reports as + * UNIQUE, sub 6). Other constraint subclasses (FK/CHECK/NOTNULL) indicate a + * real bug and must propagate to the consecutive-failure accounting. */ +function isConstraintError(err: unknown): boolean { + const e = err as { code?: unknown; errcode?: unknown } | null; + if (!e) return false; + // better-sqlite3 surfaces the sqlite result code as a string. node:sqlite + // ALSO sets .code — to a generic ERR_SQLITE_ERROR — so a string code only + // decides when it is itself a SQLITE_* code; otherwise the extended + // result code in errcode decides. + if (typeof e.code === "string" && e.code.startsWith("SQLITE_")) { + return e.code === "SQLITE_CONSTRAINT_PRIMARYKEY" || e.code === "SQLITE_CONSTRAINT_UNIQUE"; + } + if (typeof e.errcode === "number") return e.errcode === (19 | (5 << 8)) || e.errcode === (19 | (6 << 8)); + return false; +} diff --git a/src/storage/store.ts b/src/storage/store.ts index 4f04faf1ef..2c6cc3e227 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -167,11 +167,59 @@ export interface IngestSessionInput { deadRefs: { ord: number; ref: string; refNum: number; recordedAt: number }[]; refs: RefRow[]; ccrEntries: CcrEntryRow[]; - /** Plan-space pointer sequences (indices into `messages`), if the caller - * has them. ingestSession rewrites both the rows and these pointers to - * absolute seqs before insert — see remapPlanSeqs. */ + /** Plan-space pointer sequences (indices into `messages`) in FULL mode; + * ABSOLUTE seqs in delta mode. ingestSession rewrites both the rows and + * these pointers to absolute seqs before insert — see remapPlanSeqs. */ pluginSnapshotSeqs?: number[] | null; tailSeqs?: number[] | null; + /** #3133 incremental ingest: `messages` holds ONLY newly-interned rows, + * already at their absolute seqs (the planner held the per-session seq + * ledger), and tailJson pointers are absolute too. The remap pass is + * skipped and the message inserts go STRICT: a PK clash means an + * external writer moved the seq space — the transaction rolls back and + * the caller re-plans authoritatively (never a silent renumber). */ + delta?: boolean; +} + +/** #3133: one pending message registration from a delta plan — applied to + * the session's SeqState only after the ingest transaction commits. */ +export interface DeltaRegistration { + msgId: string; + hash: string; + seq: number; + /** Ref token (byRaw value) that named the message, if any. */ + token: string | undefined; +} + +/** #3133: the single identity a ref token still names exclusively. Carries + * the msgId because a SHARED sentinel token (the "BLOCKED" value used for + * dedup-blocked refs) can legally name DIFFERENT messages at different + * times: m1/BLOCKED folded out of the tail, later m2/BLOCKED arrives alone + * in the union — a bare seq claim would replay m1's bytes under m2's + * pointer. The msgId equality check turns that case into a permanent + * de-ambiguation instead of a silent mis-claim. */ +interface RefClaim { + seq: number; + msgId: string; +} + +/** #3133: per-session absolute-seq ledger, lazily seeded from the messages + * table. Lets the planner claim known messages without re-serializing them + * (see planIngest's delta tier) and allocate continuation seqs for new + * ones. In-memory only — a process restart re-seeds from the DB. */ +export interface SeqState { + /** Next allocatable seq (max existing + 1). Gaps are legal. */ + nextSeq: number; + /** `${msgId}\u0000${contentHash}` -> absolute seq. */ + byIdentity: Map; + /** Ref token -> exclusive claim; present ONLY while the token names + * exactly one identity (tokenVariants absent-or-1). Split tool-call + * cores share an id/token with different content — those tokens stay + * invalidated, as do sentinel-reused tokens (see RefClaim). */ + byRef: Map; + /** Distinct-identity count observed under a token; >=2 = permanently + * ambiguous (monotonic — once ambiguous, always serialized). */ + tokenVariants: Map; } /** Extension-namespace guards (#3069): keys/namespaces are non-empty @@ -179,6 +227,8 @@ export interface IngestSessionInput { * namespace by convention like "r3:" / "web:" / "lane:"), hostile by * accident is what we guard, not hostile by design. */ const EXT_KEY_MAX = 256; +/** #3133: bound on cached per-session seq ledgers (recency-ordered). */ +const SEQ_STATE_CAP = 128; const EXT_VALUE_MAX_BYTES = 4 * 1024 * 1024; function validateExtKey(value: string, what: string): void { @@ -214,6 +264,10 @@ export class UnifiedStore { /** Where the engine came from (#2833): builtin / host / self-downloaded — logged at enable so field diagnosis can tell which acquisition path a lane ran on. */ readonly engineSource: SqliteEngineSource; private readonly db: SqliteDatabase; + /** #3133 per-session seq ledgers (see SeqState). Bounded: a long-lived + * daemon serving many sessions must not grow without limit; evicted + * sessions simply re-seed from the DB on their next delta save. */ + private readonly seqStates = new Map(); private constructor(db: SqliteDatabase, engine: SqliteEngineName, engineSource: SqliteEngineSource) { this.db = db; @@ -268,50 +322,137 @@ export class UnifiedStore { this.db.prepare("UPDATE content SET refcount = MAX(refcount - 1, 0) WHERE hash = ?").run(hash); } + /** #3133: this session's absolute-seq ledger, lazily seeded from the + * messages table (metadata only — no CAS reads). Returns null when the + * seed fails so the caller falls back to an authoritative full plan. */ + seqStateFor(sessionId: string): SeqState | null { + const hit = this.seqStates.get(sessionId); + if (hit) { + this.seqStates.delete(sessionId); + this.seqStates.set(sessionId, hit); + return hit; + } + try { + const rows = this.db + .prepare("SELECT seq, msg_id, ref, content_hash FROM messages WHERE session_id = ?") + .all(sessionId) as { seq: number; msg_id: string; ref: string | null; content_hash: string }[]; + const st: SeqState = { nextSeq: 0, byIdentity: new Map(), byRef: new Map(), tokenVariants: new Map() }; + const soleIdent = new Map(); + for (const r of rows) { + const ident = `${r.msg_id}\u0000${r.content_hash}`; + st.byIdentity.set(ident, r.seq); + if (r.seq + 1 > st.nextSeq) st.nextSeq = r.seq + 1; + if (r.ref !== null && r.ref !== "") { + const prev = soleIdent.get(r.ref); + if (prev === undefined) soleIdent.set(r.ref, ident); + else if (prev !== ident) { + soleIdent.set(r.ref, "\u0000"); + st.tokenVariants.set(r.ref, 2); + } + } + } + for (const [token, ident] of soleIdent) { + if (ident === "\u0000") continue; + const seq = st.byIdentity.get(ident); + if (seq !== undefined) { + const msgId = ident.slice(0, ident.indexOf("\u0000")); + st.byRef.set(token, { seq, msgId }); + } + } + if (this.seqStates.size >= SEQ_STATE_CAP) { + const oldest = this.seqStates.keys().next().value; + if (oldest !== undefined && oldest !== sessionId) this.seqStates.delete(oldest); + } + this.seqStates.set(sessionId, st); + return st; + } catch { + return null; + } + } + + /** #3133: fold a committed delta plan's new registrations into the + * ledger. Called ONLY after the ingest transaction succeeded — a rolled- + * back plan must not advance the cursor or claim tokens. */ + commitDelta(sessionId: string, regs: DeltaRegistration[]): void { + const st = this.seqStates.get(sessionId); + if (!st || regs.length === 0) return; + for (const r of regs) { + st.byIdentity.set(`${r.msgId}\u0000${r.hash}`, r.seq); + if (r.token === undefined) continue; + const prev = st.byRef.get(r.token); + if (prev !== undefined && prev.seq === r.seq && prev.msgId === r.msgId) continue; + if (prev === undefined) { + const v = st.tokenVariants.get(r.token) ?? 0; + if (v <= 1) st.byRef.set(r.token, { seq: r.seq, msgId: r.msgId }); + else st.tokenVariants.set(r.token, v + 1); + } else { + // A second identity under the same token (sentinel reuse or a + // split core committed late): permanently ambiguous. + st.tokenVariants.set(r.token, (st.tokenVariants.get(r.token) ?? 1) + 1); + st.byRef.delete(r.token); + } + } + } + + /** #3133: drop the ledger — next save re-seeds from the DB (after drift + * detection, constraint-conflict healing, session removal). */ + dropSeqState(sessionId: string): void { + this.seqStates.delete(sessionId); + } + ingestSession(input: IngestSessionInput): void { const s = input.session; this.db.transaction(() => { - // Absolute-seq remap (#2671 soak finding): planIngest assigns seq - // RELATIVE to the current snapshot's log (pluginSnapshot first, - // then tail). For a folded session whose tail slides every save - // (and no pluginSnapshot to pin the prefix), plan seq 0 re-bases - // to a DIFFERENT message each save — while messages is keyed - // (session_id, seq) with INSERT OR IGNORE, so the FIRST row wins - // forever and tail pointers resolve to stale turns. Remap to - // stable absolute seqs: same identity (msgId + content hash) - // keeps its old seq; new identities continue after max(seq). - const existing = this.db - .prepare("SELECT seq, msg_id, content_hash FROM messages WHERE session_id = ?") - .all(s.sessionId) as { seq: number; msg_id: string; content_hash: string }[]; - const seqByIdentity = new Map(); - let nextSeq = 0; - for (const row of existing) { - seqByIdentity.set(`${row.msg_id}\u0000${row.content_hash}`, row.seq); - if (row.seq + 1 > nextSeq) nextSeq = row.seq + 1; - } - const planToAbs = new Map(); - const messages = input.messages.map((m) => { - // #3133: plan rows carry the intern-pass hash; only direct - // callers without a planner pay the sha256 here. - const hash = m.contentHash ?? sha256Hex(m.content); - const key = `${m.msgId}\u0000${hash}`; - let abs = seqByIdentity.get(key); - if (abs === undefined) { - abs = nextSeq++; - seqByIdentity.set(key, abs); + let messages: MessageRow[]; + if (input.delta) { + // #3133 delta plan: rows are NEW identities only, already at + // their absolute seqs (the planner held the ledger), and + // tailJson pointers are absolute too — nothing to remap. + messages = input.messages; + } else { + // Absolute-seq remap (#2671 soak finding): planIngest assigns + // seq RELATIVE to the current snapshot's log (pluginSnapshot + // first, then tail). For a folded session whose tail slides + // every save (and no pluginSnapshot to pin the prefix), plan + // seq 0 re-bases to a DIFFERENT message each save — while + // messages is keyed (session_id, seq) with INSERT OR IGNORE, + // so the FIRST row wins forever and tail pointers resolve to + // stale turns. Remap to stable absolute seqs: same identity + // (msgId + content hash) keeps its old seq; new identities + // continue after max(seq). + const existing = this.db + .prepare("SELECT seq, msg_id, content_hash FROM messages WHERE session_id = ?") + .all(s.sessionId) as { seq: number; msg_id: string; content_hash: string }[]; + const seqByIdentity = new Map(); + let nextSeq = 0; + for (const row of existing) { + seqByIdentity.set(`${row.msg_id}\u0000${row.content_hash}`, row.seq); + if (row.seq + 1 > nextSeq) nextSeq = row.seq + 1; } - planToAbs.set(m.seq, abs); - return { ...m, seq: abs }; - }); - const remapSeqs = (seqs: number[] | null | undefined): number[] | null | undefined => - seqs === null || seqs === undefined ? seqs : seqs.map((p) => planToAbs.get(p) ?? p); - const pluginSnapshotSeqs = remapSeqs(input.pluginSnapshotSeqs); - const tailSeqs = remapSeqs(input.tailSeqs); - s.tailJson = JSON.stringify({ - ...(JSON.parse(s.tailJson) as Record), - ...(pluginSnapshotSeqs !== undefined ? { pluginSnapshotSeqs } : {}), - ...(tailSeqs !== undefined ? { tailSeqs } : {}), - }); + const planToAbs = new Map(); + messages = input.messages.map((m) => { + // #3133: plan rows carry the intern-pass hash; only direct + // callers without a planner pay the sha256 here. + const hash = m.contentHash ?? sha256Hex(m.content); + const key = `${m.msgId}\u0000${hash}`; + let abs = seqByIdentity.get(key); + if (abs === undefined) { + abs = nextSeq++; + seqByIdentity.set(key, abs); + } + planToAbs.set(m.seq, abs); + return { ...m, seq: abs }; + }); + const remapSeqs = (seqs: number[] | null | undefined): number[] | null | undefined => + seqs === null || seqs === undefined ? seqs : seqs.map((p) => planToAbs.get(p) ?? p); + const pluginSnapshotSeqs = remapSeqs(input.pluginSnapshotSeqs); + const tailSeqs = remapSeqs(input.tailSeqs); + s.tailJson = JSON.stringify({ + ...(JSON.parse(s.tailJson) as Record), + ...(pluginSnapshotSeqs !== undefined ? { pluginSnapshotSeqs } : {}), + ...(tailSeqs !== undefined ? { tailSeqs } : {}), + }); + } this.db .prepare( `INSERT INTO sessions (session_id, parent_session_id, fork_point_seq, protocol, label, title, host_title, namespace, origin, @@ -375,6 +516,18 @@ export class UnifiedStore { for (const m of messages) { const hash = this.putContent(m.content, m.contentHash); + if (input.delta) { + // Delta rows are NEW identities at ledger-allocated seqs: + // a clash means an external writer moved the seq space out + // from under us — throw so the transaction rolls back and + // the caller re-plans authoritatively (never renumber). + this.db + .prepare( + "INSERT INTO messages (session_id, seq, msg_id, ref, ref_num, role, kind, content_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + ) + .run(s.sessionId, m.seq, m.msgId, m.ref, m.refNum, m.role, m.kind, hash); + continue; + } const res = this.db .prepare( "INSERT OR IGNORE INTO messages (session_id, seq, msg_id, ref, ref_num, role, kind, content_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", @@ -506,6 +659,8 @@ export class UnifiedStore { * refcount — replica GC protection, #2671 Phase 2); unpinned ones are * swept here together with the session. */ deleteSession(sessionId: string): void { + // #3133: the seq ledger is derived state — it dies with the session. + this.seqStates.delete(sessionId); this.db.transaction(() => { const refs: string[] = []; for (const m of this.db diff --git a/tests/storage-incremental-ingest.test.ts b/tests/storage-incremental-ingest.test.ts new file mode 100644 index 0000000000..12d73da80c --- /dev/null +++ b/tests/storage-incremental-ingest.test.ts @@ -0,0 +1,388 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { createRequire } from "node:module"; +import { SessionStore } from "../src/persist.ts"; +import { createSession, type Session } from "../src/session.ts"; +import { planIngest, toLegacyLike } from "../src/storage/ingest.ts"; +import { UnifiedStore } from "../src/storage/store.ts"; +import { openSqliteDatabase, setPreferredSqliteEngineForTests, type SqliteEngineName } from "../src/storage/driver.ts"; +import type { CoreMessage } from "acp-kernel"; + +/** + * #3133 completion: the per-session seq ledger that makes steady-state shadow + * saves cost O(new messages) instead of O(full log). Every wire entry point + * hands the save path freshly parsed objects per request, so cross-save + * identity must come from the ledger (ref-token / content-identity claims off + * the SQLite rows), not from object references. These regressions pin: + * 1. churned-ref steady state — a save whose whole tail arrived as new + * objects allocates ZERO ledger seqs (every message claimed); appended + * turns allocate exactly their count. + * 2. pipeline equivalence — full (remap) and delta (ledger) ingests of the + * same sessions write byte-identical tables. + * 3. PK-conflict heal — an externally written row stealing the next seq + * forces a re-seed + replay inside the save; the sidecar stays enabled. + * 4. drift recovery — a sampled digest drift drops the ledger; the next + * save replays fully, the one after resumes delta. + * 5. restart re-seed — a fresh process seeds the ledger from the DB and + * claims everything already stored. + */ + +process.env.BILI_PERSIST_ZSTD = "0"; +// Same hermeticity pin as the other storage suites: pin BOTH tiers so every +// leg controls its mode explicitly (absent now defaults to shadow). +const hermetic = fs.mkdtempSync(path.join(os.tmpdir(), "bili-incr-hermetic-")); +process.env.BILI_CONFIG_FILE = path.join(hermetic, "no-config.json"); +process.env.BILI_STORAGE_UNIFIED = "legacy"; + +const CORPUS_ROOT = path.resolve(import.meta.dirname, "golden/storage-corpus"); + +function loadCorpusCase(name: string, id: string, protocol: string): Session { + const seed = fs.mkdtempSync(path.join(os.tmpdir(), "bili-incr-seed-")); + try { + fs.cpSync(path.join(CORPUS_ROOT, name), path.join(seed, name), { recursive: true }); + const loader = new SessionStore({ dir: path.join(seed, name), debounceMs: 1, enabled: true }); + const loaded = loader.loadSync(id, { protocol, upstreamOrigin: "http://127.0.0.1:8199" }); + loader.cancelAll(); + loader.closeQuietly(); + assert.ok(loaded, `${name}: corpus loadSync returned null`); + assert.ok((loaded.lastMessages?.length ?? 0) > 0, `${name}: corpus case must carry a non-empty tail`); + return loaded; + } finally { + fs.rmSync(seed, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); + } +} + +/** Windows: an ephemeral stray handle must not fail CI — best-effort delete + * with bounded retries, swallowed on persistent EBUSY/EPERM. */ +function rmBestEffort(p: string): void { + try { + fs.rmSync(p, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); + } catch { + /* leaked temp dir on a Windows runner is harmless */ + } +} + +interface ShadowStatsView { + ingests: number; + verifySamples: number; + verifyOk: number; + verifyDrift: number; + deltaSaves: number; + disabled: boolean; +} + +function shadowOf(store: SessionStore): { stats: ShadowStatsView; store: UnifiedStore } { + const shadow = (store as unknown as { shadow?: { stats: ShadowStatsView; store: UnifiedStore } }).shadow; + assert.ok(shadow, "sidecar must be open in shadow mode"); + return shadow; +} + +/** Ledger allocation cursor: advances exactly when a message had to be + * serialized and given a new seq — the measurable unit of save cost. */ +function allocCursor(store: SessionStore, id: string): number { + const ustore = shadowOf(store).store; + const st = ustore.seqStateFor(id); + assert.ok(st, "ledger must exist after a save"); + return st.nextSeq; +} + +/** Simulate the production cycle: the next request re-parses the wire body + * into an all-new object graph — fresh identities for every message. */ +function churn(session: Session): void { + const t = session.lastMessages; + assert.ok(t, "tail must be present"); + session.lastMessages = t.map((m) => ({ ...m })); +} + +function pad6(n: number): string { + return String(n).padStart(6, "0"); +} + +function synthSession(id: string, n: number): Session { + const session = createSession(id, { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + const msgs: CoreMessage[] = []; + const byRaw = session.state.messageRefs.byRaw; + const byRef = session.state.messageRefs.byRef; + for (let i = 0; i < n; i++) { + const m: CoreMessage = { id: `syn-${pad6(i)}`, role: i % 2 ? "assistant" : "user", contentType: "text", text: `synthetic message ${i} ${"x".repeat(160)}` }; + // Keep both ref maps consistent — real sessions maintain them as a + // pair (assignRefs), and the digest legs compare them independently. + const token = `s${pad6(i)}`; + byRaw[m.id] = token; + byRef[token] = m.id; + msgs.push(m); + } + session.lastMessages = msgs; + return session; +} + +function appendTurns(session: Session, k: number, base: number): void { + const t = session.lastMessages; + assert.ok(t, "tail must be present"); + const byRaw = session.state.messageRefs.byRaw; + const byRef = session.state.messageRefs.byRef; + for (let i = 0; i < k; i++) { + const m: CoreMessage = { id: `syn-${pad6(base + i)}`, role: "user", contentType: "text", text: `appended turn ${base + i} ${"y".repeat(120)}` }; + const token = `s${pad6(base + i)}`; + byRaw[m.id] = token; + byRef[token] = m.id; + t.push(m); + } +} + +test("incremental tier: churned refs allocate nothing; appends allocate exactly their count", async () => { + const prevMode = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "shadow"; + const N = 120; + const session = synthSession("incr-t1", N); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-incr-t1-")); + try { + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + try { + await store.writeNow(session); + const stats = shadowOf(store).stats; + assert.equal(allocCursor(store, session.id), N, "cold save allocates the full log once"); + assert.equal(stats.deltaSaves, 1, "first save runs the delta path off an empty ledger"); + + // Production cycle: the next save arrives with FRESH objects for + // the entire tail — every message must be claimed, none allocated. + churn(session); + await store.writeNow(session); + assert.equal(allocCursor(store, session.id), N, "a fully churned tail must claim every message via the ledger"); + + appendTurns(session, 3, N); + churn(session); + await store.writeNow(session); + assert.equal(allocCursor(store, session.id), N + 3, "appended turns allocate exactly their count"); + + churn(session); + await store.writeNow(session); + assert.equal(allocCursor(store, session.id), N + 3, "the following save claims the appended turns too"); + + // Drive to ingest n=21: the sampled verifications (n=1, n=21) must + // pass on views rebuilt by the delta pipeline. + let guard = 0; + while (shadowOf(store).stats.ingests < 21 && guard++ < 60) { + churn(session); + await store.writeNow(session); + } + const s = shadowOf(store).stats; + assert.equal(s.ingests, 21, "every writeNow must ingest exactly once"); + assert.equal(s.verifySamples, 2, "verify must run on ingest 1 and 21 only"); + assert.equal(s.verifyOk, 2, "both sampled verifications must pass"); + assert.equal(s.verifyDrift, 0); + assert.equal(s.disabled, false); + } finally { + store.cancelAll(); + store.closeQuietly(); + } + } finally { + process.env.BILI_STORAGE_UNIFIED = prevMode; + rmBestEffort(dir); + } +}); + +test("pipeline equivalence: full (remap) and delta (ledger) ingests write identical tables", () => { + const session = loadCorpusCase("folded", "corpus-folded", "anthropic"); + const dirX = fs.mkdtempSync(path.join(os.tmpdir(), "bili-incr-x-")); + const dirY = fs.mkdtempSync(path.join(os.tmpdir(), "bili-incr-y-")); + try { + const x = UnifiedStore.open(path.join(dirX, "index.db")); + const y = UnifiedStore.open(path.join(dirY, "index.db")); + try { + const round = (legacy: ReturnType): void => { + x.ingestSession(planIngest(legacy)); + const stY = y.seqStateFor(session.id); + assert.ok(stY, "ledger must seed for the delta leg"); + const py = planIngest(legacy, stY); + assert.equal(py.delta, true, "delta leg must run in delta mode"); + y.ingestSession(py); + y.commitDelta(session.id, py.newRegs ?? []); + }; + round(toLegacyLike(session)); + appendTurns(session, 2, session.lastMessages?.length ?? 0); + round(toLegacyLike(session)); + + assert.deepEqual(y.getMessages(session.id), x.getMessages(session.id), "message tables must match byte for byte"); + assert.deepEqual(y.getBlocks(session.id), x.getBlocks(session.id), "block tables must match"); + assert.deepEqual(y.getRefs(session.id), x.getRefs(session.id), "ref ledgers must match"); + assert.deepEqual(y.getDeadRefs(session.id), x.getDeadRefs(session.id), "dead-ref ledgers must match"); + assert.deepEqual(y.getSessionRow(session.id), x.getSessionRow(session.id), "session rows (incl. tailJson) must match"); + } finally { + x.close(); + y.close(); + } + } finally { + rmBestEffort(dirX); + rmBestEffort(dirY); + } +}); + +// The heal path must recognize constraint violations under BOTH engines — +// they report them differently (string code vs numeric errcode), and a missed +// classification turns a healable collision into a sidecar failure. +const requireMod = createRequire(import.meta.url); +function betterSqliteAvailable(): boolean { + try { + requireMod.resolve("better-sqlite3"); + return true; + } catch { + return false; + } +} + +function runHealCase(engine: SqliteEngineName): Promise { + return (async () => { + const prevMode = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "shadow"; + setPreferredSqliteEngineForTests(engine); + const N = 40; + const session = synthSession(`incr-t3-${engine}`, N); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-incr-t3-")); + try { + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + try { + await store.writeNow(session); + assert.equal(allocCursor(store, session.id), N, "cold save allocates seqs 0..N-1"); + + // An external writer inserts a row at the exact seq the ledger + // would hand out next. + const conn = openSqliteDatabase(path.join(dir, "index.db")).db; + try { + // The external writer references REAL content — reuse an + // existing CAS hash so the content_hash FK holds. + const hash = conn.prepare("SELECT content_hash FROM messages WHERE session_id = ? LIMIT 1").get(session.id) as { content_hash: string }; + assert.ok(hash.content_hash, "a content hash to reuse must exist"); + conn.prepare( + "INSERT INTO messages (session_id, seq, msg_id, ref, ref_num, role, kind, content_hash) VALUES (?, ?, 'ghost', NULL, NULL, 'user', 'message', ?)" + ).run(session.id, N, hash.content_hash); + } finally { + conn.close(); + } + + // The next save allocates at the stolen seq → strict INSERT + // conflicts → the save must heal internally and NOT throw. + appendTurns(session, 1, N); + churn(session); + await store.writeNow(session); + assert.equal(allocCursor(store, session.id), N + 2, "heal re-seeds past the foreign row and lands the new message at N+1"); + assert.equal(shadowOf(store).stats.disabled, false, "a healed conflict must not disable the sidecar"); + + const conn2 = openSqliteDatabase(path.join(dir, "index.db")).db; + let ghosts = 0; + try { + const rows = conn2.prepare("SELECT seq FROM messages WHERE session_id = ? AND msg_id = 'ghost'").all(session.id) as { seq: number }[]; + ghosts = rows.length; + assert.deepEqual(rows.map((r) => r.seq), [N], "the foreign row survives untouched"); + } finally { + conn2.close(); + } + assert.equal(ghosts, 1); + + // Steady state resumes after the heal... + churn(session); + await store.writeNow(session); + assert.equal(allocCursor(store, session.id), N + 2, "post-heal churned save claims everything again"); + } finally { + store.cancelAll(); + store.closeQuietly(); + } + } finally { + setPreferredSqliteEngineForTests(null); + process.env.BILI_STORAGE_UNIFIED = prevMode; + rmBestEffort(dir); + } + })(); +} + +test("PK-conflict heal [node:sqlite]: a foreign row stealing the next seq forces reseed + replay", () => runHealCase("node:sqlite")); +test("PK-conflict heal [better-sqlite3]: same contract under the string-code engine", { skip: !betterSqliteAvailable() && "better-sqlite3 not installed (optional native dependency)" }, () => runHealCase("better-sqlite3")); + +test("drift recovery: a sampled drift drops the ledger; next save replays fully, the one after resumes delta", async () => { + const prevMode = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "shadow"; + const session = loadCorpusCase("folded", "corpus-folded", "anthropic"); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-incr-t4-")); + try { + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + try { + await store.writeNow(session); // n=1: sampled, ok + const conn = openSqliteDatabase(path.join(dir, "index.db")).db; + try { + const row = conn + .prepare("SELECT content_hash FROM messages WHERE session_id = ? ORDER BY seq LIMIT 1") + .get(session.id) as { content_hash: string } | undefined; + assert.ok(row, "messages row to tamper must exist"); + // Valid message JSON (a throw would be a failure, not drift). + conn + .prepare("UPDATE content SET bytes = ? WHERE hash = ?") + .run(Buffer.from(JSON.stringify({ id: "tampered", role: "user", contentType: "text", text: "tampered" }), "utf8"), row.content_hash); + } finally { + conn.close(); + } + for (let i = 0; i < 20; i++) await store.writeNow(session); // n=2..21 + let stats = shadowOf(store).stats; + assert.equal(stats.verifySamples, 2); + assert.equal(stats.verifyDrift, 1, "the sampled save at n=21 must catch the CAS drift"); + const deltaBeforeReplay = stats.deltaSaves; + + // n=22: the drift dropped the ledger and armed a forced-full save. + churn(session); + await store.writeNow(session); + stats = shadowOf(store).stats; + assert.equal(stats.deltaSaves, deltaBeforeReplay, "the post-drift save must replay fully (no delta save)"); + assert.equal(stats.disabled, false); + + // n=23: the ledger re-seeds from the DB and delta mode resumes. + churn(session); + await store.writeNow(session); + const cursorAfterResume = allocCursor(store, session.id); + stats = shadowOf(store).stats; + assert.equal(stats.deltaSaves, deltaBeforeReplay + 1, "the save after the forced replay must resume delta mode"); + const cursorAfterSteady = allocCursor(store, session.id); + assert.equal(cursorAfterSteady, cursorAfterResume, "and it must claim everything again"); + } finally { + store.cancelAll(); + store.closeQuietly(); + } + } finally { + process.env.BILI_STORAGE_UNIFIED = prevMode; + rmBestEffort(dir); + } +}); + +test("restart re-seed: a fresh store seeds the ledger from the DB and claims everything stored", async () => { + const prevMode = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "shadow"; + const N = 80; + const session = synthSession("incr-t5", N); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-incr-t5-")); + try { + const store1 = new SessionStore({ dir, debounceMs: 1, enabled: true }); + try { + await store1.writeNow(session); + assert.equal(allocCursor(store1, session.id), N); + } finally { + store1.cancelAll(); + store1.closeQuietly(); + } + + appendTurns(session, 2, N); + churn(session); + const store2 = new SessionStore({ dir, debounceMs: 1, enabled: true }); + try { + await store2.writeNow(session); + assert.equal(allocCursor(store2, session.id), N + 2, "restarted ledger must claim the N stored messages and allocate only the 2 new ones"); + assert.equal(shadowOf(store2).stats.verifyOk, 1, "the restarted first save's sampled verify must pass"); + } finally { + store2.cancelAll(); + store2.closeQuietly(); + } + } finally { + process.env.BILI_STORAGE_UNIFIED = prevMode; + rmBestEffort(dir); + } +});