From fcac9e30bc993728ee7d71a692d7e1641d935396 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 11 Oct 2026 19:28:00 +0800 Subject: [PATCH] fix: survive a corrupt unified index by degrading to legacy-only (#2771); pin the storage engine's restart/rollback/CAS contracts --- src/storage/shadow.ts | 15 +- tests/golden-storage-drive.ts | 42 ++- tests/storage-engine-proofs.test.ts | 451 ++++++++++++++++++++++++++++ 3 files changed, 505 insertions(+), 3 deletions(-) create mode 100644 tests/storage-engine-proofs.test.ts diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index 562786d2da..dfb6cbbe2f 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -64,7 +64,20 @@ export class UnifiedShadow { ): UnifiedShadow | null { const mode = resolveStorageMode(env, fileUnified); if (mode === "legacy") return null; - const store = UnifiedStore.open(`${dir}/index.db`); + let store: UnifiedStore; + try { + store = UnifiedStore.open(`${dir}/index.db`); + } catch (err) { + // #2771: a corrupt/unreadable index.db must degrade to legacy-only + // (same shape as the encryption opt-out), never brick SessionStore + // construction. No auto-delete: the index may hold pinned checkpoint + // blobs (#2671 Phase 2) — destructive recovery is operator policy. + loggerLog( + "warn", + `[storage] unified sidecar failed to open ${dir}/index.db (${String(err)}) — staying legacy-only; delete the file to reset the index`, + ); + return null; + } const shadow = new UnifiedShadow(mode, store, legacyLoad ?? (() => null)); loggerLog( "info", diff --git a/tests/golden-storage-drive.ts b/tests/golden-storage-drive.ts index 26b186ab60..991b18d8f8 100644 --- a/tests/golden-storage-drive.ts +++ b/tests/golden-storage-drive.ts @@ -367,12 +367,27 @@ function goldenProxyOptions(upstreamPort: number, ctx: number): ProxyOptions { }; } -/** Drive one wire and produce the golden record. */ -export async function driveWire(wire: Wire, sessionId = `golden-${wire}`, opts: { dumpStateTo?: string } = {}): Promise { +/** Drive one wire and produce the golden record. + * + * opts.storageUnified ("shadow"|"full") flips BILI_STORAGE_UNIFIED for the + * whole drive (storage-engine proof leg: the per-turn outbound digests and + * the final state digest must equal the legacy baseline's — storage mode is + * invisible on the wire). opts.reloadAfterEdit additionally simulates a + * process restart right after the mid-history edit: flush + close the + * store, reset the session pool, optionally drop the legacy file + * ("dropFile" — the unified index must be self-sufficient), then continue + * the conversation through a fresh SessionStore. */ +export async function driveWire(wire: Wire, sessionId = `golden-${wire}`, opts: { + dumpStateTo?: string; + storageUnified?: "shadow" | "full"; + reloadAfterEdit?: "keepFile" | "dropFile"; +} = {}): Promise { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), `golden-${wire}-`)); const sessionsDir = path.join(tmp, "sessions"); const prevXdg = process.env.XDG_STATE_HOME; + const prevUnified = process.env.BILI_STORAGE_UNIFIED; process.env.XDG_STATE_HOME = tmp; + if (opts.storageUnified) process.env.BILI_STORAGE_UNIFIED = opts.storageUnified; delete process.env.ACP_DUMP_BODY; const hist: Item[] = []; const ops = makeOps(wire, hist); @@ -442,6 +457,27 @@ export async function driveWire(wire: Wire, sessionId = `golden-${wire}`, opts: const editedAt = bodies.length - 1; const editedBody = bodies[editedAt]!; if (!editedBody.includes(EDIT_SENTINEL)) throw new Error(`${wire}: edited original did not re-enter the wire`); + if (opts.reloadAfterEdit) { + // Storage-engine proof leg (#2690 read path): restart the process's + // storage mid-conversation. flushAllSessions mirrors the graceful + // SIGTERM path; _resetSessionsForTest drops the resident pool so + // the next request re-hydrates via loadSync (unified-first in full + // mode). "dropFile" goes one step further: with the legacy file + // gone the load can ONLY come from index.db — if that leg still + // produces identical wire bytes, the index is self-sufficient. + await flushAllSessions(); + _resetSessionsForTest(); + store!.cancelAll(); + store!.closeQuietly(); + if (opts.reloadAfterEdit === "dropFile") { + for (const f of fs.readdirSync(sessionsDir, { recursive: true })) { + const fp = path.join(sessionsDir, f as string); + if (fs.statSync(fp).isFile() && (f as string).endsWith(".json") && !fs.existsSync(fp + ".tmp")) fs.rmSync(fp, { force: true }); + } + } + store = new SessionStore({ dir: sessionsDir, debounceMs: 1, enabled: true }); + _setStoreForTest(store); + } // Post-edit growth: 2 trailing turns. Also load-bearing for the // STATE digest: the persist layer does not reliably write the final // turn's mutations on their own (missing final markDirty — #2671 @@ -518,6 +554,8 @@ export async function driveWire(wire: Wire, sessionId = `golden-${wire}`, opts: _resetSessionsForTest(); if (prevXdg === undefined) delete process.env.XDG_STATE_HOME; else process.env.XDG_STATE_HOME = prevXdg; + if (prevUnified === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = prevUnified; rmrf(tmp); } } diff --git a/tests/storage-engine-proofs.test.ts b/tests/storage-engine-proofs.test.ts new file mode 100644 index 0000000000..883d3cc919 --- /dev/null +++ b/tests/storage-engine-proofs.test.ts @@ -0,0 +1,451 @@ +/** + * Storage-engine proofs (#2690 coverage audit, #2671). + * + * The parity core shipped with corpus digests (storage-unified.test.ts) — + * STATE-level equality between the legacy files and the unified index. This + * suite pins the seams that audit found unproven: + * + * WIRE PARITY — BILI_STORAGE_UNIFIED=full must be invisible on the + * wire: per-turn outbound digests, fold landmarks and + * the final state digest equal the COMMITTED storage + * render goldens, both with a mid-conversation restart + * and with the legacy file DELETED at the restart (the + * index alone carries the session — the migration's + * end-state promise). + * STALE REJECT — a multi-instance stale write that the guarded build + * rejects must never reach index.db; the converged disk + * record is what gets ingested. + * IMPORT GUARD — a hand-aged legacy file (older savedAt than the + * index watermark) must not re-import over newer index + * state on load. + * ROLLBACK — unset the env after running full → the legacy file + * loads with the exact digest. + * SELF-DISABLE — 3 consecutive sidecar failures disable it; reads flip + * back to legacy; the legacy file stays readable. + * CORRUPT INDEX — a corrupt index.db degrades the store to legacy-only + * instead of bricking construction (#2771). + * CAS REFCOUNT — shared content blobs survive deleteSession of one + * session and are collected only at refcount 0. + * + * The corrupt-index test is the regression for #2771; the rest are contract + * pins (proof-only, no product change). + */ + +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 { SessionStore } from "../src/persist.ts"; +import type { Session } from "../src/session.ts"; +import { UnifiedStore } from "../src/storage/store.ts"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; +import { planIngest, loadLegacyView, toLegacyLike, type LegacySessionLike } from "../src/storage/ingest.ts"; +import { canonicalize, sha256 } from "./golden-canonical.ts"; +import { driveWire, WIRES, type Wire } from "./golden-storage-drive.ts"; +import { rmrf } from "./tmp-rm.ts"; + +process.env.BILI_PERSIST_ZSTD = "0"; + +// Same hermeticity contract as the corpus suites: a developer machine with +// storage.unified in its real config must not leak a tier into these tests. +process.env.BILI_CONFIG_FILE = path.join( + fs.mkdtempSync(path.join(os.tmpdir(), "bili-engproof-hermetic-")), + "no-config.json", +); +delete process.env.BILI_STORAGE_UNIFIED; + +const CORPUS_ROOT = path.resolve(import.meta.dirname, "golden/storage-corpus"); +const RENDER_ROOT = path.resolve(import.meta.dirname, "golden/storage-render"); + +function tmpdir(prefix: string): string { + return fs.mkdtempSync(path.join(os.tmpdir(), prefix)); +} + +/** Loads the "folded" corpus case into a live Session object. */ +function loadFolded(dir: string): Session { + fs.cpSync(path.join(CORPUS_ROOT, "folded"), dir, { recursive: true }); + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + const session = store.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + store.cancelAll(); + assert.ok(session, "folded corpus case failed to load"); + return session; +} + +function recordDigest(record: Record): string { + return sha256(JSON.stringify(canonicalize({ + id: record["id"], + meta: record["meta"], + stats: record["stats"], + metadata: record["metadata"], + state: record["state"], + blockContents: Object.entries((record["blockContents"] ?? {}) as Record), + lastMessages: record["messages"], + lastMessagesFolded: record["messagesFolded"] === true, + pluginSnapshot: record["pluginSnapshot"], + contentStore: record["forkContentStore"], + }))); +} + +function viewDigest(view: ReturnType): string { + return sha256(JSON.stringify(canonicalize({ + id: view.id, + meta: view.meta, + stats: view.stats, + metadata: view.metadata, + state: view.state, + blockContents: [...view.blockContents.entries()], + lastMessages: view.lastMessages, + lastMessagesFolded: view.lastMessagesFolded, + pluginSnapshot: view.pluginSnapshot, + contentStore: view.contentStore, + }))); +} + +/** Finds the persisted envelope payload for `id` under `root`. */ +function findWrittenRecord(root: string, id: string): Record | null { + const hits: string[] = []; + const walk = (d: string): void => { + for (const f of fs.readdirSync(d, { withFileTypes: true })) { + const fp = path.join(d, f.name); + if (f.isDirectory()) walk(fp); + else if (f.name.endsWith(".json") && f.name !== "index.db") hits.push(fp); + } + }; + walk(root); + for (const hit of hits) { + const envelope = JSON.parse(fs.readFileSync(hit, "utf8")) as Record; + const payload = (envelope["payload"] ?? envelope) as Record; + if (payload["id"] === id) return payload; + } + return null; +} + +function unifiedRows(store: UnifiedStore, id: string): ReturnType { + const rows = { + session: store.getSessionRow(id)!, + messages: store.getMessages(id), + blocks: store.getBlocks(id), + deadRefs: store.getDeadRefs(id), + refs: store.getRefs(id), + }; + assert.ok(rows.session, `session row missing: ${id}`); + return loadLegacyView(rows); +} + +// --------------------------------------------------------------------------- +// WIRE PARITY: full mode + mid-conversation restart == committed goldens. +// --------------------------------------------------------------------------- + +for (const wire of WIRES) { + test(`storage engine: full-mode restart is invisible on the ${wire} wire (== legacy restart, wire == committed goldens)`, { timeout: 240_000 }, async () => { + const golden = JSON.parse(fs.readFileSync(path.join(RENDER_ROOT, `${wire}.json`), "utf8")) as { + turns: Array<{ i: number; sha: string }>; + foldAt: number; editedAt: number; summaryFrom: number; + stateDigest: string; + stateShape: Record; + }; + // The restart oracle: a legacy-only process restart. One caveat is + // restart-INHERENT and not a storage property: the body-stability seam + // slots (cacheLedger line bs/bl/be pairing, #2131) live in memory, so + // exactly the first post-restart ledger line loses its bs fields — + // identically in both legs. Everything else must be byte-equal. + const legacy = await driveWire(wire as Wire, `golden-${wire}`, { reloadAfterEdit: "keepFile" }); + for (const leg of ["keepFile", "dropFile"] as const) { + const record = await driveWire(wire as Wire, `golden-${wire}`, { storageUnified: "full", reloadAfterEdit: leg }); + assert.equal(record.turns.length, legacy.turns.length, `${wire}/${leg}: body count drifted vs legacy restart`); + for (let i = 0; i < legacy.turns.length; i++) { + assert.equal(record.turns[i]!.sha, legacy.turns[i]!.sha, `${wire}/${leg}: outbound body #${i} digest drift vs legacy restart`); + assert.equal(record.turns[i]!.sha, golden.turns[i]!.sha, `${wire}/${leg}: outbound body #${i} digest drift vs committed goldens (restart must not change the wire)`); + } + assert.equal(record.foldAt, legacy.foldAt, `${wire}/${leg}: fold landmark drifted`); + assert.equal(record.editedAt, legacy.editedAt, `${wire}/${leg}: edit landmark drifted`); + assert.equal(record.summaryFrom, legacy.summaryFrom, `${wire}/${leg}: summary carrier landmark drifted`); + assert.equal(record.stateDigest, legacy.stateDigest, `${wire}/${leg}: persisted state digest drift vs legacy restart`); + assert.deepEqual(record.stateShape, legacy.stateShape, `${wire}/${leg}: state shape drift`); + assert.deepEqual(record.sectionDigests, legacy.sectionDigests, `${wire}/${leg}: state section digests drift`); + assert.deepEqual(record.stateKeyDigests, legacy.stateKeyDigests, `${wire}/${leg}: state key digests drift`); + } + }); +} + +// --------------------------------------------------------------------------- +// STALE REJECT: a rejected (stale) multi-writer build never reaches index.db. +// --------------------------------------------------------------------------- + +test("storage engine: stale-rejected multi-writer build never ingests; the converged disk record does", async () => { + const dir = tmpdir("bili-engproof-stale-"); + const prev = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "shadow"; + try { + const storeA = new SessionStore({ dir, debounceMs: 1, enabled: true }); + const session = loadFolded(path.join(dir, "seed")); + const blocksAtWrite = session.state.blocks.length; + + // Instance A commits the newer lineage (requests bumped). + session.stats.requests = 7; + await storeA.writeNow(session); + storeA.closeQuietly(); + + // Instance B holds the OLDER in-memory lineage and tries to commit. + const storeB = new SessionStore({ dir, debounceMs: 1, enabled: true }); + session.stats.requests = 4; // stale: disk already has 7 + session.state.blocks = session.state.blocks.slice(0, Math.max(0, blocksAtWrite - 1)); // older fold state + await storeB.writeNow(session); + storeB.closeQuietly(); + + const probe = UnifiedStore.open(path.join(dir, "index.db")); + try { + const view = unifiedRows(probe, "corpus-folded"); + const stats = view.stats as { requests?: number }; + assert.equal(stats.requests, 7, "index ingested the STALE rejected build (must be the converged disk record)"); + assert.equal((view.state as { blocks: unknown[] }).blocks.length, blocksAtWrite, "index fold state regressed to the stale lineage"); + + // The index view must equal the record on disk (the winner). + const disk = findWrittenRecord(dir, "corpus-folded"); + assert.ok(disk, "disk record not found"); + assert.equal(viewDigest(view), recordDigest(disk), "index view digest != winning disk record"); + } finally { + probe.close(); + } + } finally { + if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = prev; + rmrf(dir); + } +}); + +// --------------------------------------------------------------------------- +// IMPORT GUARD: an older legacy file must not re-import over newer index state. +// --------------------------------------------------------------------------- + +test("storage engine: load-side import guard skips a hand-aged legacy file (index watermark wins)", async () => { + const dir = tmpdir("bili-engproof-guard-"); + const prev = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "full"; + try { + const session = loadFolded(path.join(dir, "seed")); + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + session.stats.requests = 9; + await store.writeNow(session); + store.closeQuietly(); + + // Hand-age the LEGACY file: older savedAt, older fold state. + const disk = findWrittenRecord(dir, "corpus-folded"); + assert.ok(disk, "disk record not found"); + const aged = JSON.parse(JSON.stringify(disk)) as Record; + const payload = (aged["payload"] ?? aged) as Record; + payload["savedAt"] = (payload["savedAt"] as number) - 60_000; + (payload["state"] as Record)["blocks"] = []; + const target = path.join(dir, "anthropic"); + let wrote = false; + for (const f of fs.readdirSync(target, { recursive: true, withFileTypes: false }) as unknown as string[]) { + const fp = path.join(target, f); + if (fs.statSync(fp).isFile() && f.endsWith(".json")) { + const envelope = JSON.parse(fs.readFileSync(fp, "utf8")) as Record; + const inner = (envelope["payload"] ?? envelope) as Record; + if (inner["id"] === "corpus-folded") { + fs.writeFileSync(fp, JSON.stringify({ ...envelope, payload })); + wrote = true; + } + } + } + assert.ok(wrote, "aged legacy file not found to rewrite"); + + // Reload in full mode: the index watermark is newer → no re-import. + const store2 = new SessionStore({ dir, debounceMs: 1, enabled: true }); + const reloaded = store2.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + store2.cancelAll(); + assert.ok(reloaded, "full-mode reload returned null"); + const blocks = (toLegacyLike(reloaded).state as { blocks: unknown[] }).blocks; + assert.ok(blocks.length > 0, "stale legacy file re-imported over the newer index state (import guard broken)"); + } finally { + if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = prev; + rmrf(dir); + } +}); + +// --------------------------------------------------------------------------- +// ROLLBACK: full → unset env → legacy file authoritative, digest exact. +// --------------------------------------------------------------------------- + +test("storage engine: rollback contract — unset the env and the legacy read path serves the identical state", async () => { + const dir = tmpdir("bili-engproof-rollback-"); + const prev = process.env.BILI_STORAGE_UNIFIED; + try { + process.env.BILI_STORAGE_UNIFIED = "full"; + const session = loadFolded(path.join(dir, "seed")); + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + await store.writeNow(session); + store.closeQuietly(); + assert.ok(fs.existsSync(path.join(dir, "index.db")), "full mode did not create index.db"); + + // Read through BOTH paths post-write and require byte-equality of the + // served state — rollback must change the read path, never the bytes. + // (The load-path digest is the stable oracle: an in-memory session vs + // a loaded one differs benignly by the storageMode stamp + one-time + // tail normalization, identically under both paths.) + const storeFull = new SessionStore({ dir, debounceMs: 1, enabled: true }); + const viaFull = storeFull.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + storeFull.cancelAll(); + assert.ok(viaFull, "full-mode reload returned null"); + const digestFull = viewDigest(toLegacyLike(viaFull)); + + delete process.env.BILI_STORAGE_UNIFIED; + const store2 = new SessionStore({ dir, debounceMs: 1, enabled: true }); + const legacy = store2.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + store2.cancelAll(); + assert.ok(legacy, "legacy-only reload returned null after rollback"); + assert.equal(viewDigest(toLegacyLike(legacy)), digestFull, "rollback: legacy-only load digest drift vs full-mode load"); + const disk = findWrittenRecord(dir, "corpus-folded"); + assert.ok(disk, "disk record not found"); + assert.equal(viewDigest(toLegacyLike(legacy)), recordDigest(disk), "rollback: legacy load digest != disk record"); + } finally { + if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = prev; + rmrf(dir); + } +}); + +// --------------------------------------------------------------------------- +// SELF-DISABLE: 3 consecutive failures flip reads back to legacy-only. +// --------------------------------------------------------------------------- + +test("storage engine: sidecar self-disables after 3 consecutive failures (reads flip legacy)", () => { + const dir = tmpdir("bili-engproof-disable-"); + try { + const shadow = UnifiedShadow.maybeOpen(dir, { BILI_STORAGE_UNIFIED: "full" }, () => { + throw new Error("synthetic legacy-load failure"); + }); + assert.ok(shadow, "sidecar did not open"); + assert.equal(shadow.readsUnified, true, "fresh full sidecar must read unified"); + shadow.record({ some: "record" }); + shadow.record({ some: "record" }); + assert.equal(shadow.readsUnified, true, "2 failures must not disable yet"); + shadow.record({ some: "record" }); + assert.equal(shadow.stats.disabled, true, "3 consecutive failures must disable the sidecar"); + assert.equal(shadow.readsUnified, false, "disabled sidecar must not read unified"); + assert.equal(shadow.viewFor("anything"), null, "disabled sidecar must miss reads"); + shadow.close(); + + // The legacy file is unaffected through a plain SessionStore. + const legacy = loadFolded(path.join(dir, "seed")); + assert.ok(legacy, "legacy load broken while the sidecar is disabled"); + } finally { + rmrf(dir); + } +}); + +// --------------------------------------------------------------------------- +// CORRUPT INDEX (#2771): construction degrades to legacy-only. +// --------------------------------------------------------------------------- + +test("storage engine: corrupt index.db degrades to legacy-only instead of bricking construction (#2771)", async () => { + const dir = tmpdir("bili-engproof-corrupt-"); + const prev = process.env.BILI_STORAGE_UNIFIED; + try { + process.env.BILI_STORAGE_UNIFIED = "full"; + const session = loadFolded(path.join(dir, "seed")); + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + await store.writeNow(session); + store.closeQuietly(); + + // The clean read through full mode — the digest the corrupt-index + // fallback must reproduce exactly from the legacy file. + const clean = new SessionStore({ dir, debounceMs: 1, enabled: true }); + const viaFull = clean.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + clean.cancelAll(); + assert.ok(viaFull, "full-mode read returned null"); + const digestClean = viewDigest(toLegacyLike(viaFull)); + + // Corrupt the index header (crashed checkpoint / stray write). + const fd = fs.openSync(path.join(dir, "index.db"), "r+"); + fs.writeSync(fd, Buffer.alloc(512, 0x5a), 0, 512, 0); + fs.closeSync(fd); + + // THE #2771 regression: construction must survive, reads must fall + // back to the legacy file, and the digest must be exact. + const store2 = new SessionStore({ dir, debounceMs: 1, enabled: true }); + const reloaded = store2.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + store2.cancelAll(); + assert.ok(reloaded, "legacy fallback returned null over a corrupt index"); + assert.equal(viewDigest(toLegacyLike(reloaded)), digestClean, "corrupt-index fallback digest drift vs clean full-mode read"); + // Writes must keep working (legacy path) — no throw, file refreshed. + await store2.writeNow(reloaded); + store2.closeQuietly(); + } finally { + if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = prev; + rmrf(dir); + } +}); + +// --------------------------------------------------------------------------- +// CAS REFCOUNT: shared blobs survive one session's deletion. +// --------------------------------------------------------------------------- + +test("storage engine: CAS refcounts conserve shared content across deleteSession", () => { + const dir = tmpdir("bili-engproof-refcount-"); + try { + const store = UnifiedStore.open(path.join(dir, "index.db")); + try { + const mkSession = (id: string): LegacySessionLike => ({ + id, + createdAt: 1_000, + lastSeen: 2_000, + meta: { protocol: "openai" }, + stats: { requests: 1 }, + metadata: {}, + state: { + nextBlockId: 2, + nextRunId: 1, + blocks: [{ + blockId: "b1", + runId: "r1", + tier: 1, + summary: "SHARED-SUMMARY-BODY", + directMessageIds: ["raw1", "raw2"], + effectiveMessageIds: ["raw1", "raw2"], + directBlockIds: [], + compressedTokens: 100, + createdAt: 1_500, + survivedCount: 2, + generation: "young", + active: true, + }], + messageRefs: { byRaw: {}, byRef: {} }, + }, + blockContents: new Map([["b1", { one: null, full: { text: "SHARED-SUMMARY-BODY", count: 2 } }]]), + lastMessages: [], + lastMessagesFolded: false, + pluginSnapshot: undefined, + contentStore: undefined, + }); + store.ingestSession(planIngest(mkSession("sess-one"))); + store.ingestSession(planIngest(mkSession("sess-two"))); + let report = store.contentDedupReport(); + assert.equal(report.rows, 1, "identical summaries must intern to ONE CAS row"); + assert.equal(report.refcounted, 2, "two sessions must hold one refcount each"); + + store.deleteSession("sess-one"); + report = store.contentDedupReport(); + assert.equal(report.rows, 1, "shared blob must survive one holder's deletion"); + assert.equal(report.refcounted, 1, "refcount must drop to the surviving holder"); + + store.deleteSession("sess-two"); + report = store.contentDedupReport(); + assert.equal(report.rows, 0, "blob at refcount 0 must be collected"); + assert.equal(store.getSessionRow("sess-one"), null, "session rows must be gone"); + } finally { + store.close(); + } + } finally { + rmrf(dir); + } +}); + +// Engine note: these legs run on the process-default engine; the corpus +// suites (storage-unified.test.ts) cover BOTH engines at the state level, +// and the wire legs here assert against the same committed goldens the +// legacy baseline produced.