diff --git a/reference/architecture.md b/reference/architecture.md index f02e4afefa..477af01d9e 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -304,6 +304,7 @@ billion-context/ │ ├── remote-notices.ts # Remote announcement feed watcher: fail-open polling, last-known-good snapshot (#5548) │ ├── request-membership.ts # Settle-side membership probe: is this usage report ours (#4805) │ ├── resolver-wedge.ts # Persistent local DNS resolver wedge detector + alarm (#5221) +│ ├── responses-system-sections.ts # Single source of truth for the Responses-wire developer-section join: steady prepare + round-2 rebuild share one separator (#6922) │ ├── responses-tool-output.ts # Responses tool-output image parts: projection + patch helpers │ ├── rollout.ts # Feature rollout: per-install uuid, basis-point buckets, nested cohorts (#3062) │ ├── runaway-guard.ts # Intrinsic stream terminator for runaway tag/codepoint enumeration (#2346) diff --git a/src/decompress-shared.ts b/src/decompress-shared.ts index f5eabd5603..a1561634df 100644 --- a/src/decompress-shared.ts +++ b/src/decompress-shared.ts @@ -22,7 +22,7 @@ import { existsSync, mkdirSync, unlinkSync, writeFileSync } from "node:fs"; import { dirname, join, resolve as resolvePath } from "node:path"; import { tmpdir } from "node:os"; import { fileURLToPath } from "node:url"; -import { markDirty, preCompactionArchiveOf, peekSession, findSessionByCanonicalId, type Session } from "./session.js"; +import { blockContentSnapshot, markDirty, preCompactionArchiveOf, peekSession, findSessionByCanonicalId, type Session } from "./session.js"; import { getStore } from "./persist.js"; import { ccrEnabled, contentStoreOf } from "./store.js"; import { safePrefix } from "./text-safe.js"; @@ -218,7 +218,7 @@ export function resolveDecompress( } const full = args.full === true || args.full === "true"; - const cached = ctx.session.blockContents.get(blockId); + const cached = blockContentSnapshot(ctx.session.blockContents, block); let body: string; let count: number; if (cached) { @@ -591,7 +591,7 @@ function resolveDerivedDecompress( return toolFail(`[decompress FAILED: range restore (startId/endId) of derived-parent blocks is not supported — decompress "${blockId}" without range args (#1333)]`); } const full = args.full === true || args.full === "true"; - const cached = anc.blockContents.get(blockId); + const cached = blockContentSnapshot(anc.blockContents, block); const view = cached ? (full ? cached.full : (cached.one ?? cached.full)) : undefined; const body = view?.text || block.summary; const count = view?.count ?? 0; diff --git a/src/export.ts b/src/export.ts index 14a47aecf5..26c17f678d 100644 --- a/src/export.ts +++ b/src/export.ts @@ -1,7 +1,7 @@ import { existsSync, mkdirSync, writeFileSync } from "node:fs"; import path from "node:path"; import { indexToRef, matchSession as matchSessionKernel, parseStoredPlaceholder, refForRaw, renderHandoff as renderHandoffKernel, renderMessage, type CompressionBlock, type MessageContentStore, type CoreMessage } from "acp-kernel"; -import type { Session } from "./session.js"; +import { blockContentSnapshot, type Session } from "./session.js"; import { SessionStore } from "./persist.js"; import { SqliteBackend, backendSelectionNotice, listBackendNames, openBackend, resolveBackendName, resolveMysqlDsn, selectedBackendRaw, type StorageBackend } from "./storage/backend.js"; import { fileStorageBackend, fileStorageMysqlDsn } from "./knobs.js"; @@ -64,7 +64,7 @@ function blockLedgerLine(s: Session, b: CompressionBlock): string { } } } - const orig = s.blockContents.get(b.blockId)?.full.text.length; + const orig = blockContentSnapshot(s.blockContents, b)?.full.text.length; const status = !b.active ? "consumed" : b.retired ? "retired" : "active"; return `messages ${range} · original ${orig !== undefined ? `${orig} chars` : "not cached"} · summary ${b.summary.length} chars · ${status}`; } @@ -196,7 +196,7 @@ export function renderHandoff(s: Session, full: boolean): string { lines.push(""); lines.push(b.summary.trim()); lines.push(""); - const content = s.blockContents.get(b.blockId); + const content = blockContentSnapshot(s.blockContents, b); if (full && content) { lines.push(`### Original messages (${content.full.count})`); lines.push(""); @@ -242,7 +242,7 @@ function blockSummariesSection(s: Session, includeOriginals: boolean, priorDoc: lines.push(summary); } lines.push(""); - const content = includeOriginals ? s.blockContents.get(b.blockId) : undefined; + const content = includeOriginals ? blockContentSnapshot(s.blockContents, b) : undefined; if (content) { lines.push(`#### Original messages (${content.full.count})`); lines.push(""); @@ -392,7 +392,7 @@ export function renderSessionJsonlFromSession(s: Session, full: boolean, content lines.push(JSON.stringify({ type: "message", seq: i, rawId: m.id, role: m.role, message: out })); } for (const [i, b] of s.state.blocks.entries()) { - const orig = s.blockContents.get(b.blockId)?.full.text.length; + const orig = blockContentSnapshot(s.blockContents, b)?.full.text.length; lines.push( JSON.stringify({ type: "block", diff --git a/src/external-summary-compress.ts b/src/external-summary-compress.ts index 004032b072..652fcbac0d 100644 --- a/src/external-summary-compress.ts +++ b/src/external-summary-compress.ts @@ -1,4 +1,5 @@ import { collectBlockContent } from "acp-kernel"; +import { blockContentSnapshot } from "./session.js"; import { buildCompressSystemPrompt, parseCompressInput, type ParsedRange } from "./compress-tool.js"; import { messageTimestampSections } from "./external-summary-surface.js"; import { configuredSummaryPlan } from "./external-summary-runtime.js"; @@ -84,7 +85,7 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx const collected = collectBlockContent(state, block, source, { full: false }).text; if (collected.trim()) return collected; // An inline-restored block may no longer have originals in the wire view. - const cached = ctx.session.blockContents.get(block.blockId); + const cached = blockContentSnapshot(ctx.session.blockContents, block); return cached?.one?.text ?? cached?.full?.text ?? ""; }).join("\n\n"); if (!content.trim()) continue; diff --git a/src/session.ts b/src/session.ts index f518aa9ca0..380fcd30d4 100644 --- a/src/session.ts +++ b/src/session.ts @@ -29,6 +29,22 @@ export type BlockContent = { full: BlockView; }; +/** The compression-time snapshot of a block, or undefined when there is none. + * The unified index stores a placeholder for blocks whose legacy record had no + * snapshot ({ one: null, full: { text: block.summary, count: 0 } }, see + * completeBlockContents in src/storage/ingest.ts) so every block row has a view; + * replay serves it back. It is not a snapshot: readers must treat it like a + * missing entry (decompress collects from the live messages, the web view says + * "no snapshot") or a migrated session returns the summary as the block's content. */ +export function blockContentSnapshot( + contents: { get(blockId: string): BlockContent | undefined }, + block: { blockId: string; summary?: string }, +): BlockContent | undefined { + const content = contents.get(block.blockId); + if (content !== undefined && content.one === null && content.full.count === 0 && content.full.text === block.summary) return undefined; + return content; +} + /** #3050 R3 ②b: block-contents map that folds away to (almost) nothing once * a save has captured it, and re-materializes from the store on the first * access. Idle sessions stop carrying every folded block's originals; the diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index 19933d6dc0..b53d233994 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -1,5 +1,5 @@ import { defaultCountTokens, orderedRefPair, type CompressionBlock } from "acp-kernel"; -import { listSessions, displayContextBest, type ContextBest, type Session, type BlockContent } from "../session.js"; +import { blockContentSnapshot, listSessions, displayContextBest, type ContextBest, type Session, type BlockContent } from "../session.js"; import { conflictClientOf, conflictEventsOf } from "../conflict-watch.js"; import { isDisplayOnlyConflictDetail, isSiblingConflictDetail } from "../thirdparty-scan.js"; import { fileNameMatchesId, getStore, isValidRecord, _resetStoreForTest, relPathFor } from "../persist.js"; @@ -1729,7 +1729,7 @@ function snapshotRefIndexFor(session: Session, block: CompressionBlock): Map; const SUMMARY_MARKER = "[Compressed conversation section]"; const NUDGE_MARKER = "Compressible ranges ("; -// Indices verified against the kernel's nudge gate with nudgeGrowthTokens=2000 -// and ~9KiB filler turns. Pre-#4521 (master) the injections landed on -// requests {7,8,9}; since #4521 the armed-but-silent status line rides an -// ephemeral carrier on the gated turns and its shown-marker feeds the nudge -// gate back, so the schedule shifts to {4,6,8,9} — the fold/proxy-tool-round -// mechanics under test are unchanged, only this probe's anchor re-calibrates -// to the next nudge-carrying request pair (6 → tool round, 7 → fold round). -const PROXY_TOOL_AT = 6; // upstream request index whose response = acp_status call (round 1) -const FOLD_AT = 7; // upstream request index whose response = compress call (round 2) -// FOLD_AT+1 = post-fold refreshed re-request (round 3); FOLD_AT+2 = next steady client turn. +// The fold turn is materialized SCHEDULE-INDEPENDENTLY: the fake upstream +// arms on the first request carrying the ephemeral nudge (with >=11 tagged +// refs to fold) and folds on the re-request that follows the acp_status +// round. A hardcoded request-index anchor broke every time the nudge gate's +// token schedule shifted — #4521's status-line feedback ({7,8,9} → {4,6,8,9}), +// #6928's status-line removal, and #4313's timestamp-rendering default flip +// (the suite preload pin BILI_MESSAGE_TIMESTAMPS="0" changes the wire shape +// and hence the schedule: OFF {4,13} vs ON {3,5,11,14} — direct local runs +// without the preload diverge from CI). The probe also pins +// compress.messageTimestamps:false so its wire shape is hermetic regardless +// of how the suite is launched. const TOTAL_TURNS = 13; const FILLER = (seed: number, kb: number): string => { @@ -120,6 +121,13 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl try { const captured: Array<{ url: string; body: string }> = []; + // Object (not bare lets) so the outer-scope reads aren't narrowed to + // the initial "idle" — the mutations happen inside the request callback. + const fold = { + state: "idle" as "idle" | "armed" | "folded", + armIdx: -1, // upstream request index that carried the nudge (acp_status round) + foldIdx: -1, // upstream request index that carried the compress call + }; upstream = http.createServer((req, res) => { const chunks: Buffer[] = []; req.on("data", (c: Buffer) => chunks.push(c)); @@ -127,15 +135,21 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl const idx = captured.length; const body = Buffer.concat(chunks).toString("utf8"); captured.push({ url: req.url ?? "", body }); - if (idx === PROXY_TOOL_AT) { - // Round 1 of the fold turn: model asks for proxy status — the - // loop executes it locally and re-requests (round 2). + const input = (JSON.parse(body) as { input?: Item[] }).input ?? []; + if (fold.state === "idle" && hasNudge(input) && refsOf(input).length >= 11) { + // Round 1 of the fold turn: the first nudge-carrying request + // with enough refs — model asks for proxy status, the loop + // executes it locally and re-requests (round 2). + fold.state = "armed"; + fold.armIdx = idx; functionCallSse(res, `resp_${idx}`, "acp_status", "{}", `call_st_${idx}`); return; } - if (idx === FOLD_AT) { - const prev = (JSON.parse(captured[idx - 1]!.body) as { input?: Item[] }).input ?? []; - const uniq = refsOf(prev); + if (fold.state === "armed") { + // Round 2: the re-request after the acp_status round — fold. + fold.state = "folded"; + fold.foldIdx = idx; + const uniq = refsOf(input); assert.ok(uniq.length >= 11, `fold round: expected >=11 tagged refs, saw ${uniq.length}`); const args = JSON.stringify({ content: [ { startId: uniq[3]!, endId: uniq[5]!, topic: "F1A", summary: `F1A summary: ${FILLER(1, 1)}` }, @@ -144,10 +158,9 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl functionCallSse(res, `resp_${idx}`, "compress", args, `call_cmp_${idx}`); return; } - const parsed = JSON.parse(body) as { input?: Item[] }; let label = "0"; - for (let i = (parsed.input ?? []).length - 1; i >= 0; i--) { - const it = parsed.input![i]!; + for (let i = input.length - 1; i >= 0; i--) { + const it = input[i]!; if (it.type === "message" && it.role === "user" && typeof it.content === "string") { const m = (it.content as string).match(/Turn (\d+):/); if (m) { label = m[1]!; break; } @@ -169,7 +182,7 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 200_000 } } } }, modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000), - compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 2000 }, + compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 2000, messageTimestamps: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: true, @@ -223,17 +236,18 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl } const inputs = captured.map((c) => (JSON.parse(c.body) as { input?: Item[] }).input ?? []); - assert.ok(inputs.length >= FOLD_AT + 3, `expected >= ${FOLD_AT + 3} upstream requests, got ${inputs.length}`); + assert.ok(fold.state === "folded", `scenario miss: the nudge never fired on a request with >=11 refs — probe cannot materialize the #2929 trigger; adjust nudgeGrowthTokens/filler`); + assert.ok(inputs.length >= fold.foldIdx + 3, `expected >= ${fold.foldIdx + 3} upstream requests, got ${inputs.length}`); // (0) Scenario materialization: the nudge must have been injected into - // the PRE-FOLD original request (production shape: nudge armed -> model + // the arming (pre-fold) request (production shape: nudge armed -> model // calls proxy tool -> compress). Without it this probe does not cover // the #2929 trigger. - assert.ok(hasNudge(inputs[PROXY_TOOL_AT]!), `scenario miss: no ephemeral nudge in the pre-fold request (inputs[${PROXY_TOOL_AT}]) — probe does not cover the production trigger; adjust nudgeGrowthTokens/filler`); - console.log(`probe: nudge confirmed in pre-fold request; total upstream requests=${inputs.length}`); + assert.ok(hasNudge(inputs[fold.armIdx]!), `scenario miss: no ephemeral nudge in the arming request (inputs[${fold.armIdx}]) — probe does not cover the production trigger`); + console.log(`probe: nudge at inputs[${fold.armIdx}], fold at inputs[${fold.foldIdx}]; total upstream requests=${inputs.length}`); // (1) The post-fold re-request carries the in-place summary(s). - const rPost = inputs[FOLD_AT + 1]!; + const rPost = inputs[fold.foldIdx + 1]!; const postSums = summaryIndices(rPost); assert.ok(postSums.length >= 1, "post-fold re-request must carry the in-place summary"); @@ -241,7 +255,7 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl // the post-fold re-request up to and including the summary anchor. In // production this is where the chain breaks (next turn hits only the // instructions baseline). - const next = inputs[FOLD_AT + 2]!; + const next = inputs[fold.foldIdx + 2]!; const nextSums = summaryIndices(next); assert.ok(nextSums.length >= 1, `next steady turn lost the summary entirely (summaries: ${nextSums.length}) — fold did not survive to the client turn`); assert.equal(nextSums[0], postSums[0], `summary anchor drifted: re-request item[${postSums[0]}] vs next turn item[${nextSums[0]}]`); diff --git a/tests/migrate-block-placeholder.test.ts b/tests/migrate-block-placeholder.test.ts new file mode 100644 index 0000000000..ed444ce73c --- /dev/null +++ b/tests/migrate-block-placeholder.test.ts @@ -0,0 +1,76 @@ +// Migration: a legacy session may carry folded blocks with no blockContents snapshot +// (pruned or never cached). planIngest stores a placeholder for them +// ({ one: null, full: { text: block.summary, count: 0 } }, completeBlockContents) and +// the index replay serves it back as if it were a compression-time snapshot. decompress, +// the web block view and export must treat that placeholder as "no snapshot" — exactly +// like the legacy JSON load — instead of returning the block's own summary as content. +import test from "node:test"; +import assert from "node:assert/strict"; + +process.env.BILI_PERSIST = "0"; + +import { createCore, defaultConfig, type Config, type CoreMessage } from "acp-kernel"; +import { applyCompressSettings } from "../src/compress-settings.ts"; +import { storeEffectiveCcr } from "../src/store.ts"; +import { getSession, blockContentSnapshot } from "../src/session.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges } from "../src/stream.ts"; +import { resolveDecompress } from "../src/decompress-shared.ts"; +import { completeBlockContents } from "../src/storage/ingest.ts"; + +const pad = (n: number): string => String(n).padStart(5, "0"); + +function fold() { + const core = createCore(); + const config = applyCompressSettings(defaultConfig(200_000), 200_000, { ccr: { enabled: true, minToolTokens: 50 } }) as Config; + const session = getSession(`mig-ph-${Math.random().toString(36).slice(2)}`); + storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); + const raw: CoreMessage[] = []; + for (let i = 0; i < 20; i++) { + raw.push({ + id: `h_${i}`, + role: i % 2 === 0 ? "user" : "assistant", + contentType: "text", + text: `\x3cacp tokens="2K" type="text"\x3em${pad(i + 1)}\x3c/acp\x3e\nHistorical detail ${i}. ${"x".repeat(1000)}`, + }); + } + const turn = core.processTurn({ messages: raw, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + const ctx = { core, config, messages: turn.messages, session, log: () => {} }; + applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00007", summary: "Early history: messages 1-7 covered initial setup.", topic: "Early history" }] }), ctx); + const block = [...session.state.blocks].find((b) => b.active); + assert.ok(block, "a block was created"); + return { ctx, session, block }; +} + +test("migration: decompress of a block whose snapshot is the index placeholder restores the originals, not the summary", () => { + const { ctx, session, block } = fold(); + const legacyLoad = resolveDecompress({ blockId: block.blockId }, ctx).text; + assert.match(legacyLoad, /Historical detail 0\./, "cached snapshot restores originals"); + + // Legacy JSON without a snapshot for this block (pruned) → collected from the live messages. + session.blockContents = new Map(); + const noSnapshot = resolveDecompress({ blockId: block.blockId }, ctx).text; + assert.match(noSnapshot, /Historical detail 0\./); + + // Same session after the unified import + index replay: the placeholder row comes back. + session.blockContents = completeBlockContents(new Map(), session.state.blocks); + assert.equal(session.blockContents.get(block.blockId)?.full.text, block.summary, "replay serves the placeholder"); + const migrated = resolveDecompress({ blockId: block.blockId }, ctx).text; + assert.equal(migrated, noSnapshot, "migrated decompress must equal the legacy no-snapshot decompress"); +}); + +test("migration: blockContentSnapshot ignores only the placeholder shape", () => { + const { session, block } = fold(); + const real = session.blockContents.get(block.blockId); + assert.ok(real); + assert.equal(blockContentSnapshot(session.blockContents, block), real); + const placeholder = completeBlockContents(new Map(), session.state.blocks); + assert.equal(blockContentSnapshot(placeholder, block), undefined); + const emptySummary = new Map([[block.blockId, { one: null, full: { text: "different", count: 0 } }]]); + assert.ok(blockContentSnapshot(emptySummary, block), "a real zero-count snapshot with other text is kept"); + const nonEmptyCount = new Map([[block.blockId, { one: null, full: { text: block.summary, count: 5 } }]]); + assert.ok(blockContentSnapshot(nonEmptyCount, block), "a snapshot with the summary text but real items is kept"); + const oneView = new Map([[block.blockId, { one: { text: "x", count: 1 }, full: { text: block.summary, count: 0 } }]]); + assert.ok(blockContentSnapshot(oneView, block), "a snapshot with a real one-view is kept"); +}); diff --git a/tests/storage-block-content-placeholder.test.ts b/tests/storage-block-content-placeholder.test.ts new file mode 100644 index 0000000000..ccde370c0e --- /dev/null +++ b/tests/storage-block-content-placeholder.test.ts @@ -0,0 +1,89 @@ +// Repro on master: a legacy session with a folded block but no blockContents +// snapshot. After SQLite import the index replays the completeBlockContents +// placeholder ({ one: null, full: { text: summary, count: 0 } }) and readers +// treat it as the compression-time snapshot. +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"; + +process.env.BILI_PERSIST_ZSTD = "0"; +const hermetic = fs.mkdtempSync(path.join(os.tmpdir(), "bili-ph-repro-")); +process.env.BILI_CONFIG_FILE = path.join(hermetic, "no-config.json"); +process.env.BILI_STORAGE_UNIFIED = "legacy"; + +import { createCore, defaultConfig, type Config, type CoreMessage } from "acp-kernel"; +import { applyCompressSettings } from "../src/compress-settings.ts"; +import { storeEffectiveCcr } from "../src/store.ts"; +import { getSession, type Session } from "../src/session.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges } from "../src/stream.ts"; +import { resolveDecompress } from "../src/decompress-shared.ts"; +import { SessionStore } from "../src/persist.ts"; + +const pad = (n: number): string => String(n).padStart(5, "0"); + +function fold() { + const core = createCore(); + const config = applyCompressSettings(defaultConfig(200_000), 200_000, { ccr: { enabled: true, minToolTokens: 50 } }) as Config; + const session = getSession(`ph-repro-${Math.random().toString(36).slice(2)}`); + session.meta.protocol = "anthropic"; + session.meta.upstreamOrigin = "http://127.0.0.1:8199"; + storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); + const raw: CoreMessage[] = []; + for (let i = 0; i < 20; i++) { + raw.push({ + id: `h_${i}`, + role: i % 2 === 0 ? "user" : "assistant", + contentType: "text", + text: `\x3cacp tokens="2K" type="text"\x3em${pad(i + 1)}\x3c/acp\x3e\nHistorical detail ${i}. ${"x".repeat(1000)}`, + }); + } + const turn = core.processTurn({ messages: raw, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + const ctx = { core, config, messages: turn.messages, session, log: () => {} }; + applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00007", summary: "Early history: messages 1-7 covered initial setup.", topic: "Early history" }] }), ctx); + const block = [...session.state.blocks].find((b) => b.active); + assert.ok(block, "a block was created"); + return { ctx, session, block }; +} + +function reload(dir: string, mode: string, session: Session): Session { + process.env.BILI_STORAGE_UNIFIED = mode; + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + try { + const loaded = store.loadSync(session.id, session.meta); + assert.ok(loaded, `${mode}: loadSync returned null`); + return loaded; + } finally { + store.cancelAll(); + store.closeQuietly(); + } +} + +test("legacy session without a block snapshot: SQLite load must decompress like the JSON load", async () => { + const dir = path.join(hermetic, "store"); + const { ctx, session, block } = fold(); + // Legacy record without a snapshot for the folded block (pruned / never cached). + session.blockContents = new Map(); + process.env.BILI_STORAGE_UNIFIED = "legacy"; + const writer = new SessionStore({ dir, debounceMs: 1, enabled: true }); + await writer.writeNow(session); + writer.cancelAll(); + writer.closeQuietly(); + + const legacy = reload(dir, "legacy", session); + const legacyText = resolveDecompress({ blockId: block.blockId }, { ...ctx, session: legacy }).text; + + reload(dir, "full", session); // organic import + const migrated = reload(dir, "full", session); // served from the index + // Pin the precondition: the index replay must serve the placeholder shape — + // without it this test would stop exercising the bug and still pass. + assert.equal(legacy.blockContents.get(block.blockId), undefined, "legacy load has no snapshot for the block"); + assert.deepEqual(migrated.blockContents.get(block.blockId), { one: null, full: { text: block.summary, count: 0 } }, "index replay serves the completeBlockContents placeholder"); + const migratedText = resolveDecompress({ blockId: block.blockId }, { ...ctx, session: migrated }).text; + assert.match(legacyText, /Historical detail 0\./, "legacy JSON load restores originals from the live messages"); + assert.equal(migratedText, legacyText, "SQLite load must not serve the block summary as its content"); +}); +