From 86751d7aea2491bee156f80053624c4f54259f98 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 3 Nov 2026 14:46:23 +0800 Subject: [PATCH 1/5] fix(storage): keep the ref floor out of the replay leg's byRef (#5589) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a Responses/codex native-compaction rebase (#2592/F5) pins the session's ref high-water mark, byRaw carries the synthetic "bili:ref-floor" entry with no byRef counterpart (documented invariant). The refs ledger persists every byRaw entry as a row, and LegacyViewBuilder inverted every non-BLOCKED row into byRef — including the floor row — minting a phantom mNNNNN→floor entry the live map never has. Since #4685 made the parity digest order-insensitive, that content delta was the last divergent dimension left: DIGEST DRIFT [diverged: state; state: messageRefs] on every sampled verify of any session that ever pinned a floor, watermark drop + full-tail re-ingest each time, escalating to frozen JSON envelope writes after three samples — permanently, because the floor never leaves byRaw while the drift can never be clean. In shadow mode the JSON envelope is the primary durable record, so restarting after a freeze resumes from a stale snapshot. Codex Desktop sessions pin the floor on every native compaction (rebase-type boundary), which matches the field report: v0.1.212, long-running Codex conversation, persistent drift. The replay reconstruction now skips the floor entry exactly like BLOCKED: byRaw keeps the row (the cursor high-water must survive full-mode boot); byRef stays the strict inverse of real messages only. REF_FLOOR_RAW_ID moved to state-hydration.ts so the storage seam can consume it without importing the session layer; session.ts re-exports it for existing importers. Tests: tests/issue5589-ref-floor-parity.test.ts — red run reproduces the exact field log line byte-for-byte; negative gate proves a genuine divergence around the floor number still drifts. Adjacent suites 50/50. --- src/session.ts | 12 +- src/state-hydration.ts | 12 ++ src/storage/ingest.ts | 13 +- tests/issue5589-ref-floor-parity.test.ts | 155 +++++++++++++++++++++++ 4 files changed, 180 insertions(+), 12 deletions(-) create mode 100644 tests/issue5589-ref-floor-parity.test.ts diff --git a/src/session.ts b/src/session.ts index dc5f83ac88..53f82621fc 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1739,13 +1739,11 @@ export function preCompactionArchiveOf(session: Session): PreCompactionArchive { // byRaw/byRef are pruned to liveRawIds (stops the #390 additive leak). // nextIndex is left alone so a freed ref slot is never re-allocated onto a // retained tail's live tag. -/** Synthetic byRaw key pinning a session's ref high-water mark. Its value is - * the highest ref this session must never re-allocate (refs the parent used - * before a fork/resume, or numbers freed by an archive prune); the kernel - * cursor (highestUsedIndex+1) then starts above it. Never a real message id - * (those are SHA-256 hex), so it matches no incoming message and has no - * byRef entry. */ -export const REF_FLOOR_RAW_ID = "bili:ref-floor"; +// #5589: defined in state-hydration.js so the storage rebuild seam can skip +// the floor row without importing the session layer; re-exported for the +// existing importers (fork-adoption, tests). +export { REF_FLOOR_RAW_ID } from "./state-hydration.js"; +import { REF_FLOOR_RAW_ID } from "./state-hydration.js"; /** Raise `session`'s ref floor to at least `index` (no-op when its map * already reaches that high). */ diff --git a/src/state-hydration.ts b/src/state-hydration.ts index d2cd70bb4e..486ec4772f 100644 --- a/src/state-hydration.ts +++ b/src/state-hydration.ts @@ -81,3 +81,15 @@ export function mergeState(parsed: CompressionState): CompressionState { */ export const COMPRESSION_STATE_FIELDS: readonly (keyof CompressionState)[] = Object.keys(mergeState(createInitialState())) as (keyof CompressionState)[]; + +/** Synthetic byRaw key pinning a session's ref high-water mark (#2592/F5). Its + * value is the highest ref this session must never re-allocate (refs the + * parent used before a fork/resume, or numbers freed by an archive prune); + * the kernel cursor (highestUsedIndex+1) then starts above it. Never a real + * message id (those are SHA-256 hex), so it matches no incoming message and + * has NO byRef entry — consumers that invert the ref ledger back into byRef + * must skip it exactly like "BLOCKED" (#5589: inverting the floor row minted + * a phantom entry the live map never has → permanent DIGEST DRIFT). Lives + * here, not in session.ts, so the storage rebuild seam can consume it without + * importing the session layer. */ +export const REF_FLOOR_RAW_ID = "bili:ref-floor"; diff --git a/src/storage/ingest.ts b/src/storage/ingest.ts index bc9b3e2f6a..e5b01d8126 100644 --- a/src/storage/ingest.ts +++ b/src/storage/ingest.ts @@ -36,7 +36,7 @@ import { splitMediaContent, identityOfStoredContent } from "./identity.js"; * (src/state-hydration.ts), not hand-kept: the hand list had already drifted * (#4192). The loader rebuilds the state object in exactly this order. */ -import { COMPRESSION_STATE_FIELDS as MERGE_STATE_FIELDS } from "../state-hydration.js"; +import { COMPRESSION_STATE_FIELDS as MERGE_STATE_FIELDS, REF_FLOOR_RAW_ID } from "../state-hydration.js"; /** Fields owned by dedicated tables/columns — never inside kstate_json. */ const TABLE_OWNED_FIELDS = new Set(["blocks", "messageRefs", "deadRefs", "nextBlockId", "nextRunId"]); @@ -600,11 +600,14 @@ export class LegacyViewBuilder { const byRef: Record = {}; for (const r of refs) { byRaw[r.rawId] = r.ref; - // "BLOCKED" is a byRaw-only token: the in-memory map keeps the - // inverse projection strictly mNNNNN→rawId. Admitting it into byRef + // "BLOCKED" and the ref floor (#2592/F5, REF_FLOOR_RAW_ID) are + // byRaw-only tokens: the in-memory map keeps the inverse + // projection strictly mNNNNN→rawId. Admitting either into byRef // mints a key the persisted state never had (#2671 soak: sub-agent - // sessions with protected messages drifted on every save). - if (r.ref !== "BLOCKED" && byRef[r.ref] === undefined) byRef[r.ref] = r.rawId; + // sessions with protected messages drifted on every save; #5589: + // the floor row drifted every Codex native-compaction session on + // every sampled verify → watermark drop + envelope freeze). + if (r.ref !== "BLOCKED" && r.rawId !== REF_FLOOR_RAW_ID && byRef[r.ref] === undefined) byRef[r.ref] = r.rawId; } const state = this.state; diff --git a/tests/issue5589-ref-floor-parity.test.ts b/tests/issue5589-ref-floor-parity.test.ts new file mode 100644 index 0000000000..76da02d7e4 --- /dev/null +++ b/tests/issue5589-ref-floor-parity.test.ts @@ -0,0 +1,155 @@ +/** + * #5589: the synthetic ref-floor pin must not break unified shadow parity. + * + * After a Responses/codex native-compaction rebase (#2592/F5), byRaw carries + * the synthetic key "bili:ref-floor" → mNNNNN with NO byRef counterpart + * (documented invariant — the floor only exists to hold the kernel cursor + * high). The refs ledger persists EVERY byRaw entry, and the replay leg used + * to invert every non-BLOCKED row into byRef — including the floor row — + * minting a phantom mNNNNN→"bili:ref-floor" entry the live map never has. + * Post-#4685 key-sorting removed the order false positive, so this content + * difference was the last divergent dimension left: DIGEST DRIFT [diverged: + * state; state: messageRefs] on every sampled verify of any session that ever + * pinned a floor, watermark drop + full-tail re-ingest each time, escalating + * to envelope freeze after three samples — permanently, because the floor + * never leaves byRaw. Field evidence: v0.1.212, Codex Desktop app-server + * 0.166.0, long-running conversation. + */ +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 { UnifiedStore } from "../src/storage/store.ts"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; +import { loadLegacyView, planIngest, type LegacySessionLike } from "../src/storage/ingest.ts"; +import { REF_FLOOR_RAW_ID } from "../src/state-hydration.ts"; +import type { CoreMessage } from "acp-kernel"; + +process.env.BILI_PERSIST_ZSTD = "0"; +const hermetic = fs.mkdtempSync(path.join(os.tmpdir(), "bili-ref-floor-hermetic-")); +process.env.BILI_CONFIG_FILE = path.join(hermetic, "no-config.json"); +process.env.BILI_STORAGE_UNIFIED = "legacy"; + +function tmpdir(prefix: string): string { + return fs.mkdtempSync(path.join(os.tmpdir(), prefix)); +} + +function rmBestEffort(p: string): void { + try { + fs.rmSync(p, { recursive: true, force: true }); + } catch { + // best effort: a locked sqlite file must not fail the assertion path + } +} + +function msg(i: number): CoreMessage { + return { id: `h_msg${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: `hello ${i}` } as CoreMessage; +} + +// Above canonicalize's > 2020-09-01 mask threshold so createdAt / lastSeen +// collapse to __TS__ on BOTH digest legs. +const T_CREATED = 1_700_000_000_000; +const T_SEEN = 1_700_000_000_500; + +interface StateShape { + blocks: unknown[]; + messageRefs: { byRaw: Record; byRef: Record }; + nextBlockId: number; + nextRunId: number; +} + +function mkLegacy(id: string, state: StateShape): LegacySessionLike { + return { + id, + createdAt: T_CREATED, + lastSeen: T_SEEN, + meta: { protocol: "anthropic" }, + stats: { requests: 1 }, + metadata: {}, + state: state as unknown as LegacySessionLike["state"], + blockContents: new Map(), + lastMessages: [msg(0), msg(1)], + lastMessagesFolded: false, + }; +} + +interface StatsView { + ingests: number; + deltaIngests: number; + verifySamples: number; + verifyOk: number; + verifyDrift: number; +} + +function openShadow(dir: string): UnifiedShadow { + UnifiedStore.open(path.join(dir, "index.db")); + const shadow = UnifiedShadow.maybeOpen(dir, { BILI_STORAGE_UNIFIED: "shadow" }, (r) => r as LegacySessionLike); + assert.ok(shadow, "shadow mode sidecar must open"); + return shadow; +} + +function stats(shadow: UnifiedShadow): StatsView { + return (shadow as unknown as { stats: StatsView }).stats; +} + +// A session right after a native-compaction rebase: one live message plus the +// floor pinning the pre-compaction high-water mark (byRaw-only, no byRef). +const FLOOR_PINNED_STATE: StateShape = { + blocks: [], + messageRefs: { + byRaw: { "raw-a": "m00001", [REF_FLOOR_RAW_ID]: "m00042" }, + byRef: { m00001: "raw-a" }, + }, + nextBlockId: 1, + nextRunId: 1, +}; + +test("#5589: a floor-pinned session must not trip the parity digest", () => { + const dir = tmpdir("bili-5589-floor-"); + try { + const shadow = openShadow(dir); + shadow.record(mkLegacy("floor-pin", FLOOR_PINNED_STATE)); + const s = stats(shadow); + assert.equal(s.verifySamples, 1, "first save must run the sampled verify"); + assert.equal(s.verifyDrift, 0, "the synthetic floor row must not manufacture a byRef entry (pre-fix: 1)"); + assert.equal(s.verifyOk, 1, "the sampled verify must pass"); + } finally { + rmBestEffort(dir); + } +}); + +test("#5589: the replay leg keeps the floor in byRaw without minting a byRef entry", () => { + const replayed = loadLegacyView(planIngest(mkLegacy("floor-shape", FLOOR_PINNED_STATE)) as never); + // Unchecked cast: loadLegacyView types state as Record + // (the view is store-shaped, not kernel-typed); this fixture owns it. + const replayedState = replayed.state as unknown as StateShape; + assert.equal(replayedState.messageRefs.byRaw[REF_FLOOR_RAW_ID], "m00042", "the cursor pin must survive the round-trip (full-mode boot relies on it)"); + assert.equal(replayedState.messageRefs.byRef["m00042"], undefined, "no phantom mNNNNN→floor entry may be minted"); + assert.deepEqual(replayedState.messageRefs.byRef, { m00001: "raw-a" }); +}); + +test("#5589: a genuine divergence around the floor number still drifts", () => { + const dir = tmpdir("bili-5589-content-"); + try { + const shadow = openShadow(dir); + // Live leg claims m00042 → raw-c while byRaw pins the floor at the same + // number: the rows rebuild byRef without raw-c's claim, so a real fork + // around the floor number must STILL be reported (the #4323 heal path + // depends on this direction). + shadow.record(mkLegacy("floor-content", { + blocks: [], + messageRefs: { + byRaw: { "raw-a": "m00001", [REF_FLOOR_RAW_ID]: "m00042" }, + byRef: { m00001: "raw-a", m00042: "raw-c" }, + }, + nextBlockId: 1, + nextRunId: 1, + })); + const s = stats(shadow); + assert.equal(s.verifySamples, 1, "first save must run the sampled verify"); + assert.equal(s.verifyDrift, 1, "a divergent ref mapping around the floor number must still be reported"); + } finally { + rmBestEffort(dir); + } +}); From 81b36c87c3e09fe30405b24378c6c4ad82ec60ce Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 3 Nov 2026 17:03:38 +0800 Subject: [PATCH 2/5] fix: correct the REF_FLOOR_RAW_ID re-export importer note (#5589 review) --- src/session.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.ts b/src/session.ts index 53f82621fc..2a35a553a2 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1741,7 +1741,7 @@ export function preCompactionArchiveOf(session: Session): PreCompactionArchive { // retained tail's live tag. // #5589: defined in state-hydration.js so the storage rebuild seam can skip // the floor row without importing the session layer; re-exported for the -// existing importers (fork-adoption, tests). +// existing test importers. export { REF_FLOOR_RAW_ID } from "./state-hydration.js"; import { REF_FLOOR_RAW_ID } from "./state-hydration.js"; From 6b0d37baecee8476b26e4f28befa019c1b8a8ffb Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 20:23:06 +0800 Subject: [PATCH 3/5] fix(storage): reset the orphan journal on authoritative clears (#5589 facet C) A native-compaction rebase clears blockContents in place, but the store kept whatever the sessions.orphans_json journal already held (merge semantics): pre-clear entries became ghosts that the sampled verifier flagged as [diverged: blockContents] on EVERY verify - a permanent drift the escalation could never heal (field evidence S6 on 0.1.212). Per-save replacement: null resets the journal, a populated set replaces it. The old retention protected a production-unreachable state (a thinned plain map) - LazyBlockContents re-materializes on iteration or throws, so a save only ever sees the full set or a genuine clear; the pinned #4216 expectation for that case is updated accordingly. The mysql upsert now carries orphans_json (same latent class: it never wrote the journal column at all). Hermetic repro tests/issue5589-orphan-journal-ghost.test.ts reproduces the byte-identical field log signature pre-fix, plus a negative control. --- src/storage/mysql.ts | 8 +- src/storage/store.ts | 58 ++++----- tests/issue5589-orphan-journal-ghost.test.ts | 125 +++++++++++++++++++ tests/storage-orphan-representable.test.ts | 23 +++- 4 files changed, 172 insertions(+), 42 deletions(-) create mode 100644 tests/issue5589-orphan-journal-ghost.test.ts diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index 91e0928476..c589d67e36 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -676,8 +676,8 @@ export class MysqlBackend implements StorageBackend { await c.query( `INSERT INTO sessions (session_id, parent_session_id, fork_point_seq, protocol, label, title, host_title, namespace, origin, status, created_at, updated_at, next_block_id, next_run_id, - meta_json, stats_json, metadata_json, kstate_json, tail_json, ccr_json, last_ingested_seq) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + meta_json, stats_json, metadata_json, kstate_json, tail_json, ccr_json, last_ingested_seq, orphans_json) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE fork_point_seq = IF(parent_session_id IS NULL, VALUES(fork_point_seq), fork_point_seq), parent_session_id = COALESCE(parent_session_id, VALUES(parent_session_id)), @@ -697,7 +697,8 @@ export class MysqlBackend implements StorageBackend { kstate_json = VALUES(kstate_json), tail_json = VALUES(tail_json), ccr_json = VALUES(ccr_json), - last_ingested_seq = VALUES(last_ingested_seq)`, + last_ingested_seq = VALUES(last_ingested_seq), + orphans_json = VALUES(orphans_json)`, [ s.sessionId, s.parentSessionId ?? null, @@ -720,6 +721,7 @@ export class MysqlBackend implements StorageBackend { s.tailJson, s.ccrJson ?? null, watermark, + s.orphansJson ?? null, ], ); await c.query( diff --git a/src/storage/store.ts b/src/storage/store.ts index ba6aed922f..e8c232ce65 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -308,9 +308,10 @@ export interface IngestSessionInput { /** Serialized orphan map (Record) — blockContents * entries whose block is absent from state.blocks (#4216/#4242: the reap * shape is REPRESENTABLE; rides the same transaction so the session row, - * blocks and tail keep advancing). MERGE semantics on write: existing - * journal entries survive, because LazyBlockContents means a live - * session's in-memory map may not carry entries trimmed earlier. */ + * blocks and tail keep advancing). REPLACE semantics on write (#5589-C): + * finishPlan iterates the full live map (LazyBlockContents re-materializes + * on iteration), so this set is authoritative — an empty set resets the + * journal after an authoritative clear (native-compaction rebase). */ orphansJson?: string | null; } @@ -753,38 +754,26 @@ export class UnifiedStore { } /** refcount-aware release; the CAS body itself never leaves. */ - /** #4242: merge the incoming orphan map over the journal's current - * contents. Incoming entries win on key collision (same id, refreshed - * view); existing entries the live session no longer carries (trimmed - * from memory by LazyBlockContents) survive. */ - private mergeOrphansJson(sessionId: string, incomingJson: string | null): string | null { - if (incomingJson === null) { - // No orphans in this save — keep whatever the journal already - // holds (a previous reap's trimmed-away contents must survive). - const row = this.db - .prepare("SELECT orphans_json FROM sessions WHERE session_id = ?") - .get(sessionId) as { orphans_json: string | null } | undefined; - return row?.orphans_json ?? null; - } - let merged: Record = {}; - try { - const existing = this.db - .prepare("SELECT orphans_json FROM sessions WHERE session_id = ?") - .get(sessionId) as { orphans_json: string | null } | undefined; - if (existing?.orphans_json) { - const parsed = JSON.parse(existing.orphans_json) as Record; - if (parsed && typeof parsed === "object") merged = parsed; - } - } catch { - // corrupt journal → start fresh (loadLegacyView skips bad entries too) - } + /** #4242/#5589-C: the plan's orphan set is AUTHORITATIVE — finishPlan + * iterates the full live map (LazyBlockContents re-materializes on + * iteration), so the journal REPLACES instead of merging. Merge + * semantics latched pre-clear ghosts forever: after a native-compaction + * rebase (resetSessionCompression clears blockContents) the live side + * can never carry the old ids again, yet every sampled verify flagged + * them ([diverged: blockContents]) until escalation froze the envelope + * with no save path able to clear the journal (field evidence S6, + * v0.1.212 Codex Desktop). */ + private writeOrphansJson(incomingJson: string | null): string | null { + if (incomingJson === null) return null; try { const incoming = JSON.parse(incomingJson) as Record; - if (incoming && typeof incoming === "object") merged = { ...merged, ...incoming }; + if (incoming && typeof incoming === "object" && !Array.isArray(incoming)) { + return Object.keys(incoming).length > 0 ? JSON.stringify(incoming) : null; + } } catch { - // corrupt incoming → keep existing only + // corrupt authoritative input → treat as an empty set } - return Object.keys(merged).length > 0 ? JSON.stringify(merged) : null; + return null; } private releaseContent(hash: string | null): void { @@ -1010,10 +999,9 @@ export class UnifiedStore { tailJson: s.tailJson, ccrJson: s.ccrJson, lastIngestedSeq: watermark, - // #4216/#4242: orphan journal rides the normal upsert with - // MERGE semantics (existing entries survive — LazyBlockContents - // means the live map may not carry trimmed entries). - orphansJson: this.mergeOrphansJson(s.sessionId, s.orphansJson ?? null), + // #4216/#4242/#5589-C: the plan carries the exact current + // orphan set; the upsert replaces the journal wholesale. + orphansJson: this.writeOrphansJson(s.orphansJson ?? null), }); // Identity index (#2960 R2.1): claim (namespace, conversation_id) diff --git a/tests/issue5589-orphan-journal-ghost.test.ts b/tests/issue5589-orphan-journal-ghost.test.ts new file mode 100644 index 0000000000..3062ef6e77 --- /dev/null +++ b/tests/issue5589-orphan-journal-ghost.test.ts @@ -0,0 +1,125 @@ +/** + * #5589 (facet C): an authoritative blockContents clear must not leave stale + * orphan-journal ghosts that latch permanent DIGEST DRIFT. + * + * The orphan journal (#4216/#4242) rides every save so reaped block contents + * stay decompress-resolvable. The store-side merge kept existing journal + * entries the incoming plan no longer carried — sound while the live map can + * re-materialize them, but after a native-compaction REBASE + * (resetSessionCompression, #2592/F5) the live map is authoritatively CLEARED + * and can never carry them again. The ghosts then fail the verifier's + * orphan-vs-live comparison on every sampled verify ([diverged: + * blockContents]), escalate after three samples, and freeze the JSON envelope + * forever — nothing in the save path can ever clear the journal again. Field + * evidence: v0.1.212, Codex Desktop (BILI_CODEX_COMPACT=intercept), session S6 + * — 4× blockContents,state drifts, escalation, 0× "drift resolved". + */ +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 { UnifiedStore } from "../src/storage/store.ts"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; +import type { LegacySessionLike } from "../src/storage/ingest.ts"; +import type { CoreMessage } from "acp-kernel"; + +process.env.BILI_PERSIST_ZSTD = "0"; +const hermetic = fs.mkdtempSync(path.join(os.tmpdir(), "bili-orphan-ghost-hermetic-")); +process.env.BILI_CONFIG_FILE = path.join(hermetic, "no-config.json"); +process.env.BILI_STORAGE_UNIFIED = "legacy"; + +function tmpdir(prefix: string): string { + return fs.mkdtempSync(path.join(os.tmpdir(), prefix)); +} + +function rmBestEffort(p: string): void { + try { + fs.rmSync(p, { recursive: true, force: true }); + } catch { + // best effort: a locked sqlite file must not fail the assertion path + } +} + +function msg(i: number): CoreMessage { + return { id: `h_msg${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: `hello ${i}` } as CoreMessage; +} + +const T_CREATED = 1_700_000_000_000; +const T_SEEN = 1_700_000_000_500; + +type OrphanMap = Map; + +function mkLegacy(id: string, blockContents: OrphanMap): LegacySessionLike { + return { + id, + createdAt: T_CREATED, + lastSeen: T_SEEN, + meta: { protocol: "anthropic" }, + stats: { requests: 1 }, + metadata: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, nextBlockId: 1, nextRunId: 1 } as unknown as LegacySessionLike["state"], + blockContents: blockContents as unknown as LegacySessionLike["blockContents"], + lastMessages: [msg(0), msg(1)], + lastMessagesFolded: false, + }; +} + +interface StatsView { + ingests: number; + deltaIngests: number; + verifySamples: number; + verifyOk: number; + verifyDrift: number; +} + +function openShadow(dir: string): UnifiedShadow { + UnifiedStore.open(path.join(dir, "index.db")); + const shadow = UnifiedShadow.maybeOpen(dir, { BILI_STORAGE_UNIFIED: "shadow" }, (r) => r as LegacySessionLike); + assert.ok(shadow, "shadow mode sidecar must open"); + return shadow; +} + +function stats(shadow: UnifiedShadow): StatsView { + return (shadow as unknown as { stats: StatsView }).stats; +} + +function reapShape(): OrphanMap { + return new Map([["blk-1", { one: null, full: { text: "T1", count: 2 } }]]); +} + +test("#5589-C: an authoritative clear must not latch a ghost-journal drift", () => { + const dir = tmpdir("bili-5589-ghost-"); + try { + const shadow = openShadow(dir); + // Generation 1: the reap shape — block gone from state.blocks, content + // retained; the journal covers it, so the first sampled verify is clean. + shadow.record(mkLegacy("ghost", reapShape())); + let s = stats(shadow); + assert.equal(s.verifySamples, 1, "first save must run the sampled verify"); + assert.equal(s.verifyDrift, 0, "reap shape with journal coverage must be clean (#4216/#4242)"); + // Generation 2: a native-compaction rebase cleared blockContents. The + // 21st save carries the second sampled verify. + for (let i = 0; i < 20; i++) shadow.record(mkLegacy("ghost", new Map())); + s = stats(shadow); + assert.equal(s.verifySamples, 2, "the 21st save must run the second sampled verify"); + assert.equal(s.verifyDrift, 0, "an authoritative clear must reset the journal, not leave ghosts behind (pre-fix: 1)"); + } finally { + rmBestEffort(dir); + } +}); + +test("#5589-C: persisted orphan contents stay covered across saves", () => { + const dir = tmpdir("bili-5589-ghost-keep-"); + try { + const shadow = openShadow(dir); + // No clear: the orphan content stays in the live map across saves, so + // every plan carries it and the journal must keep covering it. + for (let i = 0; i < 21; i++) shadow.record(mkLegacy("ghost-keep", reapShape())); + const s = stats(shadow); + assert.equal(s.verifySamples, 2, "both sampled verifies must run"); + assert.equal(s.verifyDrift, 0, "replacement semantics must not drop entries the live side still carries"); + } finally { + rmBestEffort(dir); + } +}); diff --git a/tests/storage-orphan-representable.test.ts b/tests/storage-orphan-representable.test.ts index 4d2cc9a443..db4dfd06b4 100644 --- a/tests/storage-orphan-representable.test.ts +++ b/tests/storage-orphan-representable.test.ts @@ -90,18 +90,33 @@ test("#4216: post-reap saves keep advancing (no freeze) and cold loads are fresh assert.ok(view.blockContents.has("b3"), "orphan original survives for decompress"); assert.equal((view.blockContents.get("b3") as { full: { text: string } }).full.text, "b3 orphaned original summary"); - // 4. journal merge: a later save (still carrying the orphan) is stable, - // and a save WITHOUT it (LazyBlockContents trimmed) keeps the journal. + // 4. journal is authoritative per save (#5589-C): a later save still + // carrying the orphan is stable; an authoritative EMPTY set (a + // native-compaction rebase cleared blockContents) resets it. The + // pre-#5589-C expectation here was "a trimmed live map keeps the + // journal" — unreachable in production: LazyBlockContents + // re-materializes on iteration (or throws refusing the thinned + // view), so a save only ever sees the full set or a genuine clear. + // Holding onto pre-clear ghosts latched a permanent [diverged: + // blockContents] drift + envelope freeze (field evidence S6). store.ingestSession(planIngest(legacyLike("sess", 900, true))); + let view2 = loadLegacyView({ + session: store.getSessionRow("sess")!, + messages: store.getMessages("sess"), + blocks: store.getBlocks("sess"), + deadRefs: store.getDeadRefs("sess"), + refs: store.getRefs("sess"), + }); + assert.ok(view2.blockContents.has("b3"), "orphan survives while the live side still carries it"); store.ingestSession(planIngest({ ...legacyLike("sess", 950, false), blockContents: new Map() } as LegacySessionLike)); - const view2 = loadLegacyView({ + view2 = loadLegacyView({ session: store.getSessionRow("sess")!, messages: store.getMessages("sess"), blocks: store.getBlocks("sess"), deadRefs: store.getDeadRefs("sess"), refs: store.getRefs("sess"), }); - assert.ok(view2.blockContents.has("b3"), "trimmed-away journal entry survives the merge"); + assert.ok(!view2.blockContents.has("b3"), "an authoritative clear resets the journal (pre-fix: ghost survived forever)"); store.close(); } finally { From 2aeac3f204da3aab63b95ed742a48b4947ca0f3a Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 22:24:39 +0800 Subject: [PATCH 4/5] fix: correct the orphan-journal write-semantics comments after #5589 facet C --- src/storage/ingest.ts | 7 +++++-- src/storage/shadow.ts | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/storage/ingest.ts b/src/storage/ingest.ts index 66522d3bf6..5dd3e874ec 100644 --- a/src/storage/ingest.ts +++ b/src/storage/ingest.ts @@ -432,8 +432,11 @@ function finishPlan( // #4216/#4242: the orphan journal — blockContents entries whose block is // gone from state.blocks (#2695 reap keeps the contents for decompress). - // Rides every plan; the store merges it into sessions.orphans_json in the - // same transaction as the rest of the session. This is what makes the + // Rides every plan; the store records it authoritatively in + // sessions.orphans_json — REPLACE semantics (#5589-C): each save stores + // the live set verbatim, so an authoritative clear resets the journal + // (the pre-fix merge kept ghosts forever and froze the envelope). Same + // transaction as the rest of the session. This is what makes the // reap shape REPRESENTABLE, so record() no longer skips the ingest (the // freeze in #4216: sessions stuck at hours-old rows). const knownBlockIds = new Set(session.state.blocks.map((b) => b.blockId)); diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index 6b77bf7ffc..a7c5d0832e 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -458,7 +458,8 @@ export class UnifiedShadow { // #4216/#4242: the reap shape (blockContents without their // state.blocks entries) is now REPRESENTABLE — planIngest // journals the orphan contents into sessions.orphans_json and - // the store merges it in the same transaction, while the blocks + // the store records it authoritatively in the same transaction + // (#5589-C: replacement, not merge), while the blocks // stale-sweep drops the reaped rows. The old early-return here // froze every subsequent save for such sessions (rows stuck at // hours-old state, #4216). Normal ingest for everyone. From a5b269b63b8a2e88c9b6c78031af2d43115c784c Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 17 Nov 2026 23:36:30 +0800 Subject: [PATCH 5/5] fix(storage): name the diverging members inside object-typed drift fields (#5589) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sampled DIGEST DRIFT diagnosis (#5677) names the divergent top-level fields of the first divergent lastMessages slot, but a fixed-size serialization difference INSIDE a sidecar field (e.g. rawResponsesItem — the v0.1.215 Codex CLI shape: same id/role/text, +26 canonical bytes in rawResponsesItem) still leaves the exact wire member unknown. Drill one level: for each divergent top-level field whose value is a plain object on both legs, name the inner members whose canonicalized values disagree. Wire schema names only — values never reach the log line (same privacy contract as the #5677 field names). Bounded: at most 5 inner members per field, sorted. Fields absent on one leg are not drilled (the top-level name already says "the sidecar appeared/disappeared"); arrays are not drilled (index-level diffs are a different shape). The line prefix stays byte-identical for existing greps — the nested segment is additive: ...; fields: rawResponsesItem; rawResponsesItem: summary Wired into both call sites (the header-level describeViewDivergence and the per-row compareMessages that produces the sampled verify line). Diagnostic only — no behavior, wire, storage, or config change. --- src/storage/shadow.ts | 47 +++++++++++++++++--- tests/issue5677-drift-field-diag.test.ts | 55 ++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 5 deletions(-) diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index bc23f6205e..7eeeb3c97b 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -1535,7 +1535,7 @@ interface DriftDiagnostics { * converges the tables it owns; the sub-key names the actual residue. */ stateKeys?: string[]; /** First divergent lastMessages slot (or the length-mismatch index). */ - tailSlot?: { index: number; expect: DriftSlotFingerprint; got: DriftSlotFingerprint; /** #5677: top-level field names whose values disagree (names only). */ fields?: string[] }; + tailSlot?: { index: number; expect: DriftSlotFingerprint; got: DriftSlotFingerprint; /** #5677: top-level field names whose values disagree (names only). */ fields?: string[]; /** #5589: for object-typed divergent fields, the inner keys that disagree (wire schema names only, capped). */ nestedFields?: Record }; } /** Compare the replay by component, retaining only message fingerprints across batches. */ @@ -1658,9 +1658,10 @@ class BatchedViewVerification { this.changed(key); if (key === "lastMessages" && expected && !this.tailSlot) { const fields = diffFieldNames(expected[wantIndex], rec.raw); + const nestedFields = diffNestedFieldKeys(expected[wantIndex], rec.raw, fields); this.tailSlot = fields === undefined ? { index, expect: want.slot, got: rec.sig.slot } - : { index, expect: want.slot, got: rec.sig.slot, fields }; + : { index, expect: want.slot, got: rec.sig.slot, fields, ...(nestedFields ? { nestedFields } : {}) }; } } index++; @@ -1743,6 +1744,39 @@ function diffFieldNames(a: unknown, b: unknown): string[] | undefined { return out.length > 0 ? out : undefined; } +/** #5589: one level deeper than diffFieldNames — for each divergent top-level + * field whose value is a PLAIN OBJECT on both legs, name the inner keys whose + * canonicalized values disagree. Wire schema names only — values never leave + * this function (same privacy contract as the #5677 field names). Bounded: + * at most MAX_DRIFT_NESTED_KEYS inner keys per field, sorted. Fields absent on + * one leg are NOT drilled (the top-level name already says "the sidecar + * appeared/disappeared"); arrays are not drilled (index-level diffs are a + * different shape). undefined when no field qualifies. */ +const MAX_DRIFT_NESTED_KEYS = 5; +function diffNestedFieldKeys(a: unknown, b: unknown, fields: string[] | undefined): Record | undefined { + if (!fields || !a || typeof a !== "object" || Array.isArray(a) || !b || typeof b !== "object" || Array.isArray(b)) return undefined; + const ao = a as Record; + const bo = b as Record; + const ser = (v: unknown): string | null => (v === undefined ? null : JSON.stringify(canonicalize(v))); + const out: Record = {}; + for (const f of fields) { + const av = ao[f]; + const bv = bo[f]; + if (av === null || typeof av !== "object" || Array.isArray(av) || bv === null || typeof bv !== "object" || Array.isArray(bv)) continue; + const aobj = av as Record; + const bobj = bv as Record; + const inner: string[] = []; + for (const k of [...new Set([...Object.keys(aobj), ...Object.keys(bobj)])].sort()) { + if (ser(aobj[k]) !== ser(bobj[k])) { + inner.push(k); + if (inner.length >= MAX_DRIFT_NESTED_KEYS) break; + } + } + if (inner.length > 0) out[f] = inner; + } + return Object.keys(out).length > 0 ? out : undefined; +} + // #3919: which dimensions disagree when the sampled verify detects drift — // the digest is a single hash and leaves classification to guesswork. Pure; // runs only on detected drift (rare). Fingerprints carry id/role/length/ @@ -1795,9 +1829,10 @@ export function describeViewDivergence(expectView: unknown, gotView: unknown): D const ea = index < et.length ? et[index] : null; const ga = index < gt.length ? gt[index] : null; const fields = diffFieldNames(ea, ga); + const nestedFields = diffNestedFieldKeys(ea, ga, fields); diagnostics.tailSlot = fields === undefined ? { index, expect: fp(ea), got: fp(ga) } - : { index, expect: fp(ea), got: fp(ga), fields }; + : { index, expect: fp(ea), got: fp(ga), fields, ...(nestedFields ? { nestedFields } : {}) }; } } return diagnostics; @@ -1808,8 +1843,10 @@ export function formatDriftDiagnostics(d: DriftDiagnostics): string { const parts = [`diverged: ${d.keys.length > 0 ? d.keys.join(",") : ""}`]; if (d.stateKeys) parts.push(`state: ${d.stateKeys.join(",")}`); if (d.tailSlot) { - const { index, expect: ex, got: go, fields } = d.tailSlot; - parts.push(`tail slot ${index}: expect ${ex.id}/${ex.role} len=${ex.length} sha=${ex.sha8}, got ${go.id}/${go.role} len=${go.length} sha=${go.sha8}${fields ? `; fields: ${fields.join(",")}` : ""}`); + const { index, expect: ex, got: go, fields, nestedFields } = d.tailSlot; + let suffix = fields ? `; fields: ${fields.join(",")}` : ""; + if (nestedFields) suffix += `; ${Object.entries(nestedFields).map(([f, ks]) => `${f}: ${ks.join(",")}`).join("; ")}`; + parts.push(`tail slot ${index}: expect ${ex.id}/${ex.role} len=${ex.length} sha=${ex.sha8}, got ${go.id}/${go.role} len=${go.length} sha=${go.sha8}${suffix}`); } return " [" + parts.join("; ") + "]"; } diff --git a/tests/issue5677-drift-field-diag.test.ts b/tests/issue5677-drift-field-diag.test.ts index 66e4ffd0fd..e5f2742265 100644 --- a/tests/issue5677-drift-field-diag.test.ts +++ b/tests/issue5677-drift-field-diag.test.ts @@ -162,6 +162,61 @@ test("diagnosis: the divergent slot's differing top-level fields are named, valu assert.equal(d5.tailSlot?.fields, undefined); }); +test("diagnosis: object-typed divergent fields drill one level into their inner keys (#5589)", () => { + // The v0.1.215 Codex CLI shape: same id/role/text on both legs, a fixed + // (+26 canonical bytes) difference INSIDE rawResponsesItem — the top-level + // diff names the sidecar but not which wire key moved. + const base = [msg(0), msg(1)]; + const item = (extra: Record = {}) => ({ type: "message", status: "completed", ...extra }); + const expectView = { lastMessages: [{ ...base[0], rawResponsesItem: item() }, base[1]] }; + const withItem = (extra: Record) => ({ lastMessages: [{ ...base[0], rawResponsesItem: item(extra) }, base[1]] }); + + // One inner key added on the stored copy: named; values stay out. + const d = describeViewDivergence(expectView, withItem({ summary: "x".repeat(26) })); + assert.deepEqual(d.keys, ["lastMessages"]); + assert.equal(d.tailSlot?.index, 0); + assert.deepEqual(d.tailSlot?.fields, ["rawResponsesItem"]); + assert.deepEqual(d.tailSlot?.nestedFields, { rawResponsesItem: ["summary"] }); + const line = formatDriftDiagnostics(d); + assert.ok(line.includes("fields: rawResponsesItem; rawResponsesItem: summary"), `nested segment present: ${line}`); + assert.ok(!line.includes("x".repeat(26)), "inner values never reach the line"); + + // A VALUE change on a shared inner key names that key. + const d2 = describeViewDivergence(expectView, withItem({ status: "incomplete" })); + assert.deepEqual(d2.tailSlot?.nestedFields, { rawResponsesItem: ["status"] }); + + // Multiple inner disagreements: all named, sorted. + const d3 = describeViewDivergence(expectView, withItem({ zeta: 1, alpha: 2 })); + assert.deepEqual(d3.tailSlot?.nestedFields, { rawResponsesItem: ["alpha", "zeta"] }); + + // The whole field present on one leg only: the top-level name IS the + // diagnosis — no drill. + const d4 = describeViewDivergence( + { lastMessages: [{ ...base[0] }, base[1]] }, + { lastMessages: [{ ...base[0], rawResponsesItem: item() }, base[1]] }, + ); + assert.deepEqual(d4.tailSlot?.fields, ["rawResponsesItem"]); + assert.equal(d4.tailSlot?.nestedFields, undefined, "no drill when the field is absent on one leg"); + + // Bounded: >5 disagreeing inner keys report the first 5 (sorted). + const d5 = describeViewDivergence(expectView, withItem({ k1: 1, k2: 2, k3: 3, k4: 4, k5: 5, k6: 6, k7: 7 })); + assert.deepEqual(d5.tailSlot?.nestedFields, { rawResponsesItem: ["k1", "k2", "k3", "k4", "k5"] }); + + // Array-typed divergent fields are named but not drilled. + const d6 = describeViewDivergence( + { lastMessages: [{ ...base[0], rawOpenaiContentParts: [{ type: "output_text", text: "a" }] }, base[1]] }, + { lastMessages: [{ ...base[0], rawOpenaiContentParts: [{ type: "output_text", text: "b" }] }, base[1]] }, + ); + assert.deepEqual(d6.tailSlot?.fields, ["rawOpenaiContentParts"]); + assert.equal(d6.tailSlot?.nestedFields, undefined, "arrays are not drilled"); + + // A non-object divergent field: named, no drill segment (no empty suffix). + const d7 = describeViewDivergence(expectView, { lastMessages: [{ ...base[0], rawResponsesItem: item(), ts: 123 }, base[1]] }); + assert.deepEqual(d7.tailSlot?.fields, ["ts"]); + assert.equal(d7.tailSlot?.nestedFields, undefined, "non-object fields are not drilled"); + assert.ok(!formatDriftDiagnostics(d7).includes("ts: "), `no empty drill segment: ${formatDriftDiagnostics(d7)}`); +}); + test("latch gate: a drift-latched session rides the full path until a clean sample re-admits it", () => { const dir = tmpdir("bili-5677-latch-"); try {