diff --git a/src/persist.ts b/src/persist.ts index 3aead97047..c12d36ca6d 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -1438,6 +1438,25 @@ export class SessionStore { if (this.shadow.driftedSessions.has(id)) { const driftedEnvelope = this.loadEnvelope(id, meta); if (driftedEnvelope) { + // #4323: the latch historically served the envelope + // unconditionally on the assumption it was the fresher copy + // — true for one-off external churn, FALSE under a + // multi-writer ping-pong (#394): the last file write may be + // an OLDER generation than the index (measured incident: + // envelope 12 active blocks vs index 16 — serving the file + // put ~240K folded tokens back into context). Fold progress + // is the freshness proxy owner-ruled for this class: + // strictly more active blocks wins; a tie or an unreadable + // other side keeps the historical envelope preference. + const envActive = countActiveBlocks(driftedEnvelope.payload); + const idxActive = this.shadow.activeBlockCount(id); + if (idxActive !== null && idxActive > envActive) { + const unified = this.loadUnified(id); + if (unified) { + this.log("warn", `[persist] serving ${id} from the UNIFIED store despite the drift latch: legacy envelope holds ${envActive} active block(s), the index holds ${idxActive} — taking the fuller fold coverage instead of the older generation (#4323/#394)`); + return this.afterLoad(id, unified); + } + } const drifted = buildSession(driftedEnvelope.payload); if (hasNegativePersistedTokens(driftedEnvelope.payload)) { this.scheduleSave(drifted); @@ -2263,7 +2282,7 @@ export class SessionStore { * bytes on disk and the ownership fast path stayed live. */ scheduleSave(session: Session): void { if (!this.saveContentStore(session)) return; - if (this.writerOffActive()) { + if (this.writerOffActive() || this.envelopeFrozen(session.id)) { // Phase 3 (#2960): index-only persistence — the builder closure // runs the sidecar ingest (raw tail + live CCR) and the stale-disk // guard; no envelope write is armed, so no debounce is needed. @@ -2348,7 +2367,7 @@ export class SessionStore { * either. Do NOT regress this by eager-snapshotting the record here. */ async writeNow(session: Session): Promise { if (!this.saveContentStore(session)) throw new Error(`Originals were not saved for ${session.id}`); - if (this.writerOffActive()) { + if (this.writerOffActive() || this.envelopeFrozen(session.id)) { // #3133: drop the armed microtask (token mismatch below) — this // sync build subsumes it; later mutations re-arm on their own. this.writerOffPending.delete(session.id); @@ -2428,7 +2447,7 @@ export class SessionStore { * failure for a never-persisted session or it is lost permanently). */ flushSync(session: Session): boolean { if (!this.saveContentStore(session)) return false; - if (this.writerOffActive()) { + if (this.writerOffActive() || this.envelopeFrozen(session.id)) { // #3133: same subsume rule as writeNow. this.writerOffPending.delete(session.id); try { @@ -2549,6 +2568,14 @@ export class SessionStore { return this.jsonWriterOff && this.shadow !== undefined && !this.shadow.stats.disabled; } + /** #4323: per-session envelope freeze (escalated structural drift, see + * UnifiedShadow.envelopeFrozen) — the save routes index-only exactly + * like writerOff, so a multi-writer ping-pong (#394) can no longer + * churn the file copy from this process. */ + private envelopeFrozen(sessionId: string): boolean { + return this.shadow?.envelopeFrozen(sessionId) ?? false; + } + /** Cancel all pending writes without flushing (e.g. for tests). */ cancelAll(): void { for (const timer of this.saveTimers.values()) clearTimeout(timer); @@ -2792,6 +2819,15 @@ function hasNegativePersistedTokens(parsed: PersistedSession): boolean { return (typeof last === "number" && last < 0) || (typeof ctx === "number" && ctx < 0); } +/** #4323: active fold blocks a persisted record carries — the freshness + * proxy for drift-latched source selection (loadSync). Defensive: malformed + * files must count as zero, never throw into the load path. */ +function countActiveBlocks(payload: PersistedSession): number { + const blocks = payload.state?.blocks; + if (!Array.isArray(blocks)) return 0; + return blocks.filter((b) => !!b && b.active === true).length; +} + /** #2671 Phase 1.5: adapter that lets buildSession (with its full forward- * compat pass, incl. mergeState) rebuild a Session from a unified-store * view. `messagesFolded` is only ever set when `messages` is present — an diff --git a/src/storage/runtime-status.ts b/src/storage/runtime-status.ts index ae170f3d54..c67290321b 100644 --- a/src/storage/runtime-status.ts +++ b/src/storage/runtime-status.ts @@ -175,6 +175,8 @@ interface StorageRuntimeStatus { integrity: { ok: boolean; checkedAt: number; durationMs: number; detail?: string } | null; secondProcess: { pid: number; at: number } | null; backup: { lastPath: string; lastAt: number; lastBytes: number } | null; + /** #4323: sessions whose JSON-envelope writes this process froze after repeated non-converging drift samples. */ + frozenEnvelopeSessions: number; } function getStorageRuntimeStatus(): StorageRuntimeStatus { @@ -215,6 +217,7 @@ function getStorageRuntimeStatus(): StorageRuntimeStatus { integrity: state.integrity, secondProcess: state.secondProcess, backup: state.backup, + frozenEnvelopeSessions: sidecar?.frozenEnvelopeIds.size ?? 0, }; } @@ -238,6 +241,8 @@ interface StorageStatusWire { integrity: { ok: boolean; checkedAt: number; durationMs: number; detail?: string } | null; secondProcess: { pid: number; at: number } | null; backup: { lastPath: string; lastAt: number; lastBytes: number } | null; + /** #4323: envelope-write freeze count on the wire. */ + frozenEnvelopeSessions: number; } export function storageStatusWire(): StorageStatusWire { @@ -258,6 +263,7 @@ export function storageStatusWire(): StorageStatusWire { integrity: st.integrity, secondProcess: st.secondProcess, backup: st.backup, + frozenEnvelopeSessions: st.frozenEnvelopeSessions, }; } diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index 977f0aebb9..8712291834 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -43,6 +43,16 @@ export const MAX_CONSECUTIVE_FAILURES = 3; * session always verifies (cold-path full check), then every N-th. */ export const VERIFY_EVERY_INGESTS = 20; +/** #4323: consecutive sampled drifts required before escalating. A one-off + * drift is transient external churn (the watermark clear below heals it); + * N in a row proves the views are STRUCTURALLY divergent — the multi-writer + * signature (#394), where nothing this process does can converge them. + * Escalation = loud WARN with both sides' fold counts + freeze of THIS + * process's JSON envelope writes for the session (persist.ts consults + * envelopeFrozen on every save route); the unified store keeps receiving + * every save, and the first clean sample re-admits the session. */ +export const DRIFT_ESCALATE_STREAK = 3; + /** #3823: on win32 "unable to open database file" (SQLITE_CANTOPEN) is almost * always an OS-level refusal — antivirus/Defender holding the dir, OneDrive * placeholder files under the profile dir, a path past MAX_PATH, or a @@ -106,6 +116,14 @@ export class UnifiedShadow { * until a save verifies clean again; surfaced on /__bili/status wire as * driftedSessions so drift is observable, not just logged. */ readonly driftedSessions = new Set(); + /** #4323: per-session consecutive sampled-drift count (resets on a clean + * sample). Per-process only, same as ingestCounts. */ + readonly driftStreaks = new Map(); + /** #4323: sessions whose drift escalated to DRIFT_ESCALATE_STREAK — this + * process stops writing their JSON envelopes (persist.ts routes their + * saves index-only) so a multi-writer ping-pong (#394) can no longer + * churn the file copy; the first clean sample re-admits. */ + readonly frozenEnvelopeIds = new Set(); /** #3931 step 3: the sqlite fast lane. Null when the selected backend * is a network backend (mysql) — then every sync facade method below * returns its miss value (null / [] / no-op) and callers fall back to @@ -364,6 +382,29 @@ export class UnifiedShadow { // to distinguish from one-off external churn. const repeat = this.driftedSessions.has(legacy.id); this.driftedSessions.add(legacy.id); + // #4323: count CONSECUTIVE sampled drifts — membership alone + // caps at "repeat yes/no" and cannot distinguish 3+ samples. + // N in a row proves structural divergence (multi-writer + // #394): escalate ONCE at the threshold with a loud WARN + // naming both sides' fold counts, then freeze THIS + // process's envelope writes for the session so the ping- + // pong stops churning the file copy (persist.ts consults + // envelopeFrozen on every save route; the unified store + // keeps receiving every save either way). + const streak = (this.driftStreaks.get(legacy.id) ?? 0) + 1; + this.driftStreaks.set(legacy.id, streak); + if (streak === DRIFT_ESCALATE_STREAK) { + this.frozenEnvelopeIds.add(legacy.id); + let sides = ""; + try { + const envBlocks = expectView.state.blocks.filter((b) => b.active).length; + const idxBlocks = (gotView.state.blocks as { active?: boolean }[] | null | undefined)?.filter((b) => b.active === true).length ?? 0; + sides = ` [envelope ${envBlocks} active block(s) vs index ${idxBlocks}]`; + } catch { + // counts must never break the heal path + } + loggerLog("warn", `[storage] DIGEST DRIFT ESCALATED for ${legacy.id}: ${streak} consecutive sampled drifts — the views have stopped converging${sides}. This usually means MULTIPLE bili instances share one sessions dir (#394 anti-pattern; second-instance detection: #4177). Freezing THIS process's JSON envelope writes for the session until a clean sample re-admits it — the unified store keeps receiving every save (#4323)`); + } // Panels consume {disabled, lastError} — make the sampled // degradation visible there, not just in the counter/log. this.stats.lastError = `digest drift for ${legacy.id}: unified replay != legacy load view`; @@ -394,6 +435,12 @@ export class UnifiedShadow { // index-first reads (the watermark re-verify path above // proved the views converged again). this.driftedSessions.delete(legacy.id); + // #4323: convergence also ends the streak and unfreezes + // this process's envelope writes for the session. + this.driftStreaks.delete(legacy.id); + if (this.frozenEnvelopeIds.delete(legacy.id)) { + loggerLog("info", `[storage] drift resolved for ${legacy.id}: clean sample re-admits the session — envelope writes resume (#4323)`); + } } } this.consecutiveFailures = 0; @@ -474,6 +521,26 @@ export class UnifiedShadow { } } + /** #4323: true once this session's drift escalated and no clean sample + * has since re-admitted it. Sqlite lanes only — network backends keep + * the envelope as their sync read path, where freezing would strand + * the session. persist.ts consults this on every save route. */ + envelopeFrozen(id: string): boolean { + return this.sync !== null && this.frozenEnvelopeIds.has(id); + } + + /** #4323: active block rows the unified store holds for `id` — null when + * the sqlite fast lane is unavailable; callers must not guess from null. */ + activeBlockCount(id: string): number | null { + if (this.sync === null) return null; + if (this.stats.disabled) return null; + try { + return this.sync.getBlocks(id).filter((b) => b.active).length; + } catch { + return null; + } + } + /** Organic import (full mode): ingest a legacy record the index has not * seen so future loads serve from the unified store. Never throws. */ importRecord(record: unknown): LegacySessionLike | null { @@ -501,6 +568,8 @@ export class UnifiedShadow { remove(id: string): void { if (this.sync === null) return; this.ingestCounts.delete(id); + this.driftStreaks.delete(id); + this.frozenEnvelopeIds.delete(id); if (this.stats.disabled) return; try { this.sync.deleteSession(id); diff --git a/tests/issue4323-drift-escalation.test.ts b/tests/issue4323-drift-escalation.test.ts new file mode 100644 index 0000000000..43692d052e --- /dev/null +++ b/tests/issue4323-drift-escalation.test.ts @@ -0,0 +1,376 @@ +/** + * #4323: structural digest drift must escalate to a loud alarm plus an + * envelope-write freeze, and a drift-latched cold load must take the side + * with the fuller fold coverage. + * + * Measured incident: several bili instances sharing one sessions dir (#394 + * anti-pattern) ping-ponged the JSON envelope and the SQLite index into + * permanent divergence; every restart/eviction cold load hit the drift latch + * and served the STALE envelope (12 active blocks where the index held 16) — + * putting ~240K already-folded tokens back into context. Two gaps pinned here: + * + * A. the sampler logged each non-converging sample but never escalated: N + * consecutive structural drifts looked like one-off external churn. Now + * DRIFT_ESCALATE_STREAK samples fire ONE loud WARN naming both sides' + * fold counts and freeze THIS process's JSON envelope writes for the + * session (persist.ts routes its saves index-only); a clean sample + * re-admits and resumes file writes. + * B. the latch served the legacy envelope unconditionally ("last file write + * is fresher" — false under multi-writer ping-pong). Now the unified + * view wins when it strictly carries MORE active blocks; a tie or an + * unreadable other side keeps the historical envelope preference (the + * negative gate is pinned too: unlatched loads stay unified-first). + */ + +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 { openSqliteDatabase } from "../src/storage/driver.ts"; +import type { LegacySessionLike } from "../src/storage/ingest.ts"; +import { setLogCapture } from "../src/logger.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetStorageRuntimeStatusForTest, storageStatusWire } from "../src/storage/runtime-status.ts"; +import type { Session } from "../src/session.ts"; +import type { CompressionBlock, CoreMessage } from "acp-kernel"; + +process.env.BILI_PERSIST_ZSTD = "0"; +// Same hermeticity pin as the other storage suites: a developer machine with +// storage.unified in its real config must not leak a tier into these tests. +const HERMETIC = fs.mkdtempSync(path.join(os.tmpdir(), "bili-4323-hermetic-")); +process.env.BILI_CONFIG_FILE = path.join(HERMETIC, "no-config.json"); +delete process.env.BILI_STORAGE_UNIFIED; + +const PREV_UNIFIED = process.env.BILI_STORAGE_UNIFIED; + +type Captured = { level: string; msg: string }; + +function captureLogs(): { captured: Captured[]; stop: () => void } { + const captured: Captured[] = []; + setLogCapture((level, msg) => captured.push({ level, msg })); + return { captured, stop: () => setLogCapture(null) }; +} + +function rmBestEffort(p: string): void { + try { + fs.rmSync(p, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); + } catch { + /* leaked temp dir on a Windows runner is harmless */ + } +} + +function tmpDir(tag: string): string { + return fs.mkdtempSync(path.join(HERMETIC, `${tag}-`)); +} + +function settle(ms = 300): Promise { + return new Promise((r) => setTimeout(r, ms)); +} + +// --------------------------------------------------------------------------- +// A. Escalation + freeze at the shadow level (harness replicated from #3919) +// --------------------------------------------------------------------------- + +function msg(i: number, text = `hello ${i}`): CoreMessage { + return { id: `h_msg${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text } as CoreMessage; +} + +function mkLegacy(id: string, lastMessages: LegacySessionLike["lastMessages"]): LegacySessionLike { + return { + id, + createdAt: 1_000, + lastSeen: 2_000, + meta: { protocol: "anthropic" }, + stats: { requests: 1 }, + metadata: {}, + state: { + blocks: [], + messageRefs: { byRaw: {}, byRef: {} }, + nextBlockId: 1, + nextRunId: 1, + }, + blockContents: new Map(), + lastMessages, + lastMessagesFolded: false, + }; +} + +interface StatsView { + verifyOk: number; + verifyDrift: number; +} + +function stats(shadow: UnifiedShadow): StatsView { + return (shadow as unknown as { stats: StatsView }).stats; +} + +function openShadow(dir: string): { shadow: UnifiedShadow; store: UnifiedStore } { + const store = 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, store }; +} + +/** Swap the CAS bytes under a session's first message row — the tamper shape + * that only the sampled read-back can catch (replicated from #3919). */ +function tamperFirstRowBytes(dir: string, sessionId: string): string { + const conn = openSqliteDatabase(path.join(dir, "index.db")).db; + try { + const row = conn.prepare("SELECT content_hash FROM messages WHERE session_id = ? ORDER BY seq LIMIT 1").get(sessionId) as { content_hash: string } | undefined; + assert.ok(row, "row to tamper must exist"); + conn + .prepare("UPDATE content SET bytes = ? WHERE hash = ?") + .run(Buffer.from(JSON.stringify({ id: "tampered", role: "user", contentType: "text", text: "tampered" }), "utf8"), row.content_hash); + return row.content_hash; + } finally { + conn.close(); + } +} + +test("#4323 A: three consecutive sampled drifts escalate once and freeze envelope writes; a clean sample re-admits", () => { + const dir = tmpDir("escalate"); + try { + const { shadow, store } = openShadow(dir); + try { + const ids = "drift-esc"; + let k = 5; + shadow.record(mkLegacy(ids, Array.from({ length: k }, (_, i) => msg(i)))); + const hash = tamperFirstRowBytes(dir, ids); + + const { captured, stop } = captureLogs(); + try { + // Sample n=21: first drift — not yet structural. + for (let i = 0; i < 20; i++) { + k += 1; + shadow.record(mkLegacy(ids, Array.from({ length: k }, (_, j) => msg(j)))); + } + assert.equal(stats(shadow).verifyDrift, 1); + assert.equal(shadow.envelopeFrozen(ids), false, "one drift sample must not freeze"); + + // Sample n=41 over the SAME unhealed tamper: still below threshold. + for (let i = 0; i < 20; i++) { + k += 1; + shadow.record(mkLegacy(ids, Array.from({ length: k }, (_, j) => msg(j)))); + } + assert.equal(stats(shadow).verifyDrift, 2); + assert.equal(shadow.envelopeFrozen(ids), false, "two drift samples must not freeze yet"); + + // Sample n=61: third consecutive — structural, escalate + freeze. + for (let i = 0; i < 20; i++) { + k += 1; + shadow.record(mkLegacy(ids, Array.from({ length: k }, (_, j) => msg(j)))); + } + assert.equal(stats(shadow).verifyDrift, 3); + assert.equal(shadow.envelopeFrozen(ids), true, "three consecutive sampled drifts freeze the envelope"); + + // Repair the CAS bytes, then sample n=81: clean re-admission. + const conn = openSqliteDatabase(path.join(dir, "index.db")).db; + try { + conn.prepare("UPDATE content SET bytes = ? WHERE hash = ?").run(Buffer.from(JSON.stringify(msg(0)), "utf8"), hash); + } finally { + conn.close(); + } + for (let i = 0; i < 20; i++) { + k += 1; + shadow.record(mkLegacy(ids, Array.from({ length: k }, (_, j) => msg(j)))); + } + assert.equal(stats(shadow).verifyDrift, 3, "no further drift after the repair"); + assert.equal(stats(shadow).verifyOk, 2, "pre-tamper sample plus the post-repair sample pass"); + assert.equal(shadow.envelopeFrozen(ids), false, "a clean sample re-admits and unfreezes"); + } finally { + stop(); + } + + const escalated = captured.filter((e) => e.level === "warn" && e.msg.includes(`DIGEST DRIFT ESCALATED for ${ids}`)); + assert.equal(escalated.length, 1, "exactly ONE escalation alarm for the whole streak"); + assert.ok(escalated[0].msg.includes("active block(s) vs index"), `alarm names both sides' fold counts: ${escalated[0].msg}`); + assert.ok(escalated[0].msg.includes("#394"), "alarm names the multi-writer anti-pattern"); + const resolved = captured.filter((e) => e.level === "info" && e.msg.includes(`drift resolved for ${ids}`)); + assert.equal(resolved.length, 1, "re-admission is logged"); + const driftErrors = captured.filter((e) => e.level === "error" && e.msg.startsWith(`[storage] unified shadow DIGEST DRIFT for ${ids}:`)); + assert.equal(driftErrors.length, 3, "each non-converging sample still logs its error line"); + } finally { + shadow.close(); + store.close(); + } + } finally { + rmBestEffort(dir); + } +}); + +// --------------------------------------------------------------------------- +// B. Drift-latched cold load takes the fuller fold coverage (full mode) +// --------------------------------------------------------------------------- + +type ShadowLike = { + record(r: unknown): void; + stats: { verifyDrift: number }; + driftedSessions?: Set; + frozenEnvelopeIds?: Set; +}; + +function makeBlock(blockId: string): CompressionBlock { + return { + blockId, + runId: "r0", + tier: 1, + summary: "", + directMessageIds: [], + effectiveMessageIds: [], + directBlockIds: [], + compressedTokens: 0, + createdAt: Date.now(), + survivedCount: 0, + generation: "young", + active: true, + }; +} + +function makeSession(id: string, requests: number, blocks: CompressionBlock[]): Session { + return { + id, + createdAt: Date.now(), + meta: {}, + metadata: {}, + stats: { requests }, + state: { blocks, messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {}, nextBlockId: blocks.length + 1 }, + blockContents: new Map(), + lastMessages: [], + lastMessagesFolded: false, + pluginSnapshot: undefined, + contentStore: undefined, + } as unknown as Session; +} + +interface FullCase { + tag: string; + latch: boolean; + genA: { requests: number; blocks: CompressionBlock[] }; + genB: { requests: number; blocks: CompressionBlock[] }; +} + +async function runLatchedLoad(c: FullCase): Promise<{ reloaded: Session | null; captured: Captured[] }> { + _resetStorageRuntimeStatusForTest(); + process.env.BILI_STORAGE_UNIFIED = "full"; + const dir = tmpDir(c.tag); + try { + const store = new SessionStore({ enabled: true, dir, debounceMs: 0 }); + const { captured, stop } = captureLogs(); + try { + const id = `${c.tag}-session`; + const genA = makeSession(id, c.genA.requests, c.genA.blocks); + store.scheduleSave(genA); + await settle(); + + const sh = (store as unknown as { shadow?: ShadowLike }).shadow; + assert.ok(sh, "full mode opens a sidecar"); + if (!sh.driftedSessions) sh.driftedSessions = new Set(); + if (!sh.frozenEnvelopeIds) sh.frozenEnvelopeIds = new Set(); + if (c.latch) sh.driftedSessions.add(id); + + // Commit ONLY the envelope to generation B (stub the sidecar ingest + // for one save — same recipe as storage-full-flip #3818 ④): the + // index now holds genA, the file holds genB. + const realRecord = sh.record.bind(sh); + sh.record = () => {}; + const genB = makeSession(id, c.genB.requests, c.genB.blocks); + assert.ok(store.flushSync(genB), "gen-B envelope committed"); + sh.record = realRecord; + + const reloaded = store.loadSync(id); + stop(); + store.closeQuietly(); + return { reloaded, captured }; + } finally { + stop(); + rmBestEffort(dir); + } + } finally { + delete process.env.BILI_STORAGE_UNIFIED; + } +} + +test("#4323 B1: latched load serves the INDEX when it strictly has more active blocks", async () => { + const { reloaded, captured } = await runLatchedLoad({ + tag: "idx-rich", latch: true, + genA: { requests: 5, blocks: [makeBlock("bA1"), makeBlock("bA2")] }, + genB: { requests: 99, blocks: [] }, + }); + assert.ok(reloaded, "session loads"); + assert.equal((reloaded.state.blocks as CompressionBlock[]).length, 2, "the fuller (index) fold coverage wins"); + assert.equal((reloaded.stats as { requests?: number }).requests, 5, "served the index generation, not the newer-but-thinner envelope"); + const warn = captured.find((e) => e.level === "warn" && e.msg.includes("despite the drift latch")); + assert.ok(warn, "source switch is logged loudly"); + assert.ok(warn?.msg.includes("legacy envelope holds 0 active block(s), the index holds 2"), `counts named in the log: ${warn?.msg}`); +}); + +test("#4323 B2: latched load KEEPS the envelope when it has the fuller coverage (historical preference)", async () => { + const { reloaded, captured } = await runLatchedLoad({ + tag: "env-rich", latch: true, + genA: { requests: 5, blocks: [] }, + genB: { requests: 99, blocks: [makeBlock("bB1"), makeBlock("bB2")] }, + }); + assert.ok(reloaded, "session loads"); + assert.equal((reloaded.state.blocks as CompressionBlock[]).length, 2, "envelope keeps the richer fold coverage"); + assert.equal((reloaded.stats as { requests?: number }).requests, 99, "served the envelope generation"); + assert.ok(!captured.some((e) => e.msg.includes("despite the drift latch")), "no source-switch alarm when the envelope stays authoritative"); + assert.ok(captured.some((e) => e.msg.includes("serving") && e.msg.includes("legacy envelope: last sampled digest drifted")), "historical fallback line intact"); +}); + +test("#4323 B3: latched load with EQUAL active blocks keeps the historical envelope preference", async () => { + const { reloaded } = await runLatchedLoad({ + tag: "tie", latch: true, + genA: { requests: 5, blocks: [makeBlock("bTA")] }, + genB: { requests: 99, blocks: [makeBlock("bTB")] }, + }); + assert.ok(reloaded, "session loads"); + assert.equal((reloaded.stats as { requests?: number }).requests, 99, "tie goes to the envelope (unchanged behavior)"); + assert.deepEqual((reloaded.state.blocks as CompressionBlock[]).map((b) => b.blockId), ["bTB"], "envelope block served"); +}); + +test("#4323 B4 negative gate: WITHOUT the latch the load stays unified-first regardless of counts", async () => { + const { reloaded, captured } = await runLatchedLoad({ + tag: "no-latch", latch: false, + genA: { requests: 5, blocks: [] }, + genB: { requests: 99, blocks: [makeBlock("bN1"), makeBlock("bN2")] }, + }); + assert.ok(reloaded, "session loads"); + assert.equal((reloaded.stats as { requests?: number }).requests, 5, "unlatched full-mode loads serve the index (unified-first default)"); + assert.ok(!captured.some((e) => e.msg.includes("despite the drift latch")), "selection logic must not fire outside the latch"); + assert.ok(!captured.some((e) => e.msg.includes("last sampled digest drifted")), "no drift-fallback line outside the latch"); +}); + +// --------------------------------------------------------------------------- +// C. The freeze is observable on the status wire +// --------------------------------------------------------------------------- + +test("#4323 C: frozenEnvelopeSessions rides the /__bili/status wire", async () => { + _resetStorageRuntimeStatusForTest(); + process.env.BILI_STORAGE_UNIFIED = "full"; + const dir = tmpDir("wire"); + try { + const store = new SessionStore({ enabled: true, dir, debounceMs: 0 }); + const sh = (store as unknown as { shadow?: ShadowLike }).shadow; + assert.ok(sh, "full mode opens a sidecar"); + if (!sh.frozenEnvelopeIds) sh.frozenEnvelopeIds = new Set(); + assert.equal(storageStatusWire().frozenEnvelopeSessions, 0, "no freezes on a fresh store"); + sh.frozenEnvelopeIds.add("wire-session"); + assert.equal(storageStatusWire().frozenEnvelopeSessions, 1, "an escalated session is visible on the wire"); + sh.frozenEnvelopeIds.delete("wire-session"); + assert.equal(storageStatusWire().frozenEnvelopeSessions, 0, "re-admission clears the counter"); + store.closeQuietly(); + rmBestEffort(dir); + } finally { + delete process.env.BILI_STORAGE_UNIFIED; + _setStoreForTest(new SessionStore({ enabled: false, dir: tmpDir("wire-out") })); + } +}); + +test.after(() => { + if (PREV_UNIFIED === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = PREV_UNIFIED; + rmBestEffort(HERMETIC); +});