From ac0f405b0b06d048ac2f4776d051e4aea96c0846 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sun, 1 Nov 2026 16:43:14 +0800 Subject: [PATCH 1/2] test: async drift-latch convergence regression (#5415) Hermetic async-backend fixture mirroring issue5408-frozen-recovery B1: a drift-latched folded save sweeps the foreign row out of the index (positive gate) while an unlatched folded save leaves it untouched (negative gate). Driven through the real enqueueAsyncRecord path and verified to fail without the fix. --- tests/issue5415-async-drift-latch.test.ts | 203 ++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 tests/issue5415-async-drift-latch.test.ts diff --git a/tests/issue5415-async-drift-latch.test.ts b/tests/issue5415-async-drift-latch.test.ts new file mode 100644 index 0000000000..8d8247cae3 --- /dev/null +++ b/tests/issue5415-async-drift-latch.test.ts @@ -0,0 +1,203 @@ +/** + * #5415: async storage backend — the drift latch never releases once engaged. + * + * #5408/#5409 gave the SYNC sidecar latched-sweep convergence: while drift- + * latched the full re-ingest sweeps foreign rows out of the index even without + * a raw tail (the envelope is what load routing serves under the latch, so the + * index must converge to it — persist.ts lazySource serves the FILE for a + * latched id on BOTH backends). The ASYNC backend twin (enqueueAsyncRecord) + * kept `{ completeLog: hasRawTail }`, so a folded-resident save (no raw tail) + * never swept here: foreign rows rode every replay, each sampled verify kept + * drifting, the watermark was cleared every time, and the latch could never + * release. + * + * Async-backend mirror of B1 in tests/issue5408-frozen-recovery.test.ts, driven + * through the REAL async path (UnifiedShadow.fromBackend + SqliteBackend -> + * record() -> enqueueAsyncRecord's serial queue): + * + * A. POSITIVE GATE. A drift-latched folded save runs the authoritative-set + * sweep and retires the foreign row out of the index. + * B. NEGATIVE GATE. An UNLATCHED folded save leaves foreign rows untouched + * (no raw tail, no latch -> no sweep authority over the table). + */ + +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 { SqliteBackend } from "../src/storage/backend.ts"; +import { openSqliteDatabase } from "../src/storage/driver.ts"; +import type { LegacySessionLike } from "../src/storage/ingest.ts"; +import type { CoreMessage } from "acp-kernel"; + +process.env.BILI_PERSIST_ZSTD = "0"; +// Same hermeticity pin as the #5408 suite: 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-5415-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; + +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 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 { + ingests: number; + verifyOk: number; + verifyDrift: number; +} + +function stats(shadow: UnifiedShadow): StatsView { + return (shadow as unknown as { stats: StatsView }).stats; +} + +/** Build an ASYNC-mode shadow (sync fast lane null) over a real SQLite store — + * record() routes through enqueueAsyncRecord's serial queue exactly like the + * mysql backend, but stays queryable via plain SQLite. */ +function openAsyncShadow(dir: string): { shadow: UnifiedShadow; store: UnifiedStore } { + const store = UnifiedStore.open(path.join(dir, "index.db")); + const shadow = UnifiedShadow.fromBackend("shadow", new SqliteBackend(store), (r) => r as LegacySessionLike); + return { shadow, store }; +} + +/** Count the session's live (non-retired) message rows. */ +function countMessages(dir: string, sessionId: string): number | null { + const conn = openSqliteDatabase(path.join(dir, "index.db")).db; + try { + const row = conn.prepare("SELECT COUNT(*) AS n FROM messages WHERE session_id = ? AND retired_at IS NULL").get(sessionId) as { n: number } | undefined; + return row ? row.n : null; + } finally { + conn.close(); + } +} + +/** Insert a foreign message row (identity absent from any plan this test + * produces) — the residue shape a second writer or a lost rewind leaves. */ +function insertForeignRow(dir: string, sessionId: string): void { + 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, "base content row must exist"); + conn + .prepare("INSERT INTO messages (session_id, seq, msg_id, role, kind, content_hash, identity) VALUES (?, ?, ?, ?, ?, ?, ?)") + .run(sessionId, 99_999, "foreign-msg", "user", "message", row.content_hash, "foreign-identity"); + } finally { + conn.close(); + } +} + +function foreignRowRetired(dir: string, sessionId: string): boolean | null { + const conn = openSqliteDatabase(path.join(dir, "index.db")).db; + try { + const row = conn.prepare("SELECT retired_at FROM messages WHERE session_id = ? AND msg_id = 'foreign-msg'").get(sessionId) as { retired_at: number | null } | undefined; + return row ? row.retired_at !== null : null; + } finally { + conn.close(); + } +} + +function latchDrift(shadow: UnifiedShadow, id: string): void { + const sh = shadow as unknown as { driftedSessions?: Set }; + if (!sh.driftedSessions) sh.driftedSessions = new Set(); + sh.driftedSessions.add(id); +} + +/** Poll an async-queue effect to completion (bounded) instead of a fixed sleep. */ +async function waitFor(fn: () => boolean, what: string, timeoutMs = 5000): Promise { + const deadline = Date.now() + timeoutMs; + for (;;) { + if (fn()) return; + if (Date.now() > deadline) throw new Error(`timed out waiting for ${what}`); + await new Promise((r) => setTimeout(r, 10)); + } +} + +test("#5415 A: a drift-latched folded save sweeps the foreign row out of the async index", async () => { + const dir = tmpDir("a-latched"); + try { + const { shadow, store } = openAsyncShadow(dir); + try { + const ids = "a-latched"; + shadow.record(mkLegacy(ids, Array.from({ length: 5 }, (_, i) => msg(i)))); + await waitFor(() => (countMessages(dir, ids) ?? 0) >= 5, "first ingest to land 5 rows"); + insertForeignRow(dir, ids); + assert.equal(foreignRowRetired(dir, ids), false, "foreign row present before the latched save"); + + latchDrift(shadow, ids); + shadow.record({ ...mkLegacy(ids, []), lastMessagesFolded: true }); + + await waitFor(() => foreignRowRetired(dir, ids) === true, "latched folded save to run the authoritative-set sweep"); + assert.equal(foreignRowRetired(dir, ids), true, "latched folded save retires the foreign row on the async path too"); + } finally { + store.close(); + } + } finally { + rmBestEffort(dir); + } +}); + +test("#5415 B negative gate: an UNLATCHED folded save leaves foreign rows untouched on the async path", async () => { + const dir = tmpDir("b-control"); + try { + const { shadow, store } = openAsyncShadow(dir); + try { + const ids = "b-control"; + shadow.record(mkLegacy(ids, Array.from({ length: 5 }, (_, i) => msg(i)))); + await waitFor(() => (countMessages(dir, ids) ?? 0) >= 5, "first ingest to land 5 rows"); + insertForeignRow(dir, ids); + + // No latch: historical behavior — a folded save (no raw tail) does + // not claim authoritative-set authority over the table. + shadow.record({ ...mkLegacy(ids, []), lastMessagesFolded: true }); + await waitFor(() => stats(shadow).ingests >= 2, "folded save to process"); + + assert.equal(foreignRowRetired(dir, ids), false, "unlatched folded save must not sweep (no raw tail, no latch)"); + } finally { + store.close(); + } + } finally { + rmBestEffort(dir); + } +}); + +test.after(() => { + if (PREV_UNIFIED === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = PREV_UNIFIED; +}); From dc6d5a4e96a0b92f4539a232dac0cbb96f0bfe1c Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 11 Nov 2026 13:17:47 +0800 Subject: [PATCH 2/2] test: consolidate async drift-latch regression into the #5412 suite with deterministic gates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #5421 added tests/issue5415-async-drift-latch.test.ts as a regression pin for the async drift-latch fix that already landed in #5420. On this repo, #5415 is an unrelated closed issue (0.1.210 nudge cadence); the drift-latch bug is #5412, which #5420 already pins with tests/issue5412-async-drift-latch-parity.test.ts — same two gates, same scenario. This keeps ONE canonical suite named after the public issue: - delete the duplicate tests/issue5415-async-drift-latch.test.ts; - adopt its strictly stronger waiting discipline into the canonical file: bounded polling (waitFor) plus an ingests>=2 barrier replace fixed settle(300ms) sleeps, which could false-red gate A on loaded runners and pass gate B vacuously if the folded save had not been processed yet; - cleanup now uses closeAsync() — close() is a no-op in async mode and leaked the sqlite handle (windows EBUSY on temp-dir teardown); - the BILI_STORAGE_UNIFIED restore now captures the previous value before deleting it (the PR variant captured after, making the restore dead code). Verified locally: both gates green at this head; reverting the #5420 fix line (src/storage/shadow.ts enqueueAsyncRecord) turns gate A red while gate B stays green, so the consolidated suite carries the same teeth. --- ...issue5412-async-drift-latch-parity.test.ts | 86 ++++++-- tests/issue5415-async-drift-latch.test.ts | 203 ------------------ 2 files changed, 67 insertions(+), 222 deletions(-) delete mode 100644 tests/issue5415-async-drift-latch.test.ts diff --git a/tests/issue5412-async-drift-latch-parity.test.ts b/tests/issue5412-async-drift-latch-parity.test.ts index 5dd8e04846..5b98cf741f 100644 --- a/tests/issue5412-async-drift-latch-parity.test.ts +++ b/tests/issue5412-async-drift-latch-parity.test.ts @@ -6,14 +6,30 @@ * `completeLog = hasRawTail || driftedSessions.has(id)` so a latched save * claims authoritative-set authority and its stale sweep retires residue * rows even without a raw tail. The async port - * (`UnifiedShadow.enqueueAsyncRecord`) was not updated: under the same - * latch it kept ingesting `{ completeLog: hasRawTail }`, i.e. false — every - * latched save re-took the incomplete view, the next sample drifted again, - * and a session frozen via the #4323 escalation could never converge back - * to clean on an async backend (mysql etc.). Positive gate pinned here: - * latched folded save → foreign row retired. Negative gate: an UNLATCHED - * folded save leaves foreign rows untouched (no raw tail, no latch) — the - * historical behavior must not leak into async either way. + * (`UnifiedShadow.enqueueAsyncRecord`) originally kept + * `{ completeLog: hasRawTail }`: under the latch that is always false, so + * every latched save re-took the incomplete view, each sampled verify kept + * drifting, the watermark was cleared every time, and a session frozen via + * the #4323 escalation could never release its latch on an async backend + * (mysql etc.). Fixed in #5420 by mirroring the sync condition verbatim. + * + * Gates (async-lane shadow over a REAL sqlite backend via openBackend — + * sync === null forces record() onto enqueueAsyncRecord's serial queue, the + * same queue an opt-in network backend runs on): + * A. POSITIVE GATE. A drift-latched folded save runs the authoritative-set + * sweep and retires the foreign row out of the index. + * B. NEGATIVE GATE. An UNLATCHED folded save leaves foreign rows untouched + * (no raw tail, no latch -> no sweep authority over the table) — the + * historical behavior must not leak into async either way. + * + * Hardened 2026-11-11 during review of PR #5421: the original gates waited + * a fixed settle(300ms) for the serial queue to drain, which could false-red + * gate A on loaded runners and pass gate B vacuously if the folded save had + * not been processed yet. Both gates now poll bounded conditions (row + * retired / ingests counter) instead of sleeping; cleanup uses closeAsync() + * (close() is a no-op in async mode and leaked the sqlite handle); the + * BILI_STORAGE_UNIFIED restore captures the previous value before deleting + * it. */ import { test } from "node:test"; @@ -31,6 +47,7 @@ process.env.BILI_PERSIST_ZSTD = "0"; // Hermeticity pin, same family as the #4323/#5408 suites: a developer // machine with storage.unified in its real config must not leak a tier. const HERMETIC = fs.mkdtempSync(path.join(os.tmpdir(), "bili-5412-hermetic-")); +const PREV_UNIFIED = process.env.BILI_STORAGE_UNIFIED; process.env.BILI_CONFIG_FILE = path.join(HERMETIC, "no-config.json"); delete process.env.BILI_STORAGE_UNIFIED; @@ -46,11 +63,6 @@ function tmpDir(tag: string): string { return fs.mkdtempSync(path.join(HERMETIC, `${tag}-`)); } -/** Give the serial backend queue a turn so enqueueAsyncRecord drains. */ -function settle(ms = 300): Promise { - return new Promise((r) => setTimeout(r, ms)); -} - function msg(i: number, text = `hello ${i}`): CoreMessage { return { id: `h_msg${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text } as CoreMessage; } @@ -75,6 +87,16 @@ function mkLegacy(id: string, lastMessages: LegacySessionLike["lastMessages"]): }; } +interface StatsView { + ingests: number; + verifyOk: number; + verifyDrift: number; +} + +function stats(shadow: UnifiedShadow): StatsView { + return (shadow as unknown as { stats: StatsView }).stats; +} + /** Async-lane shadow over a REAL sqlite backend: sync === null forces * record() onto enqueueAsyncRecord — the same queue an opt-in network * backend runs on, without needing a live mysql server. */ @@ -85,6 +107,17 @@ function openAsyncShadow(dir: string): Promise<{ shadow: UnifiedShadow }> { })); } +/** Count the session's live (non-retired) message rows. */ +function countMessages(dir: string, sessionId: string): number | null { + const conn = openSqliteDatabase(path.join(dir, "index.db")).db; + try { + const row = conn.prepare("SELECT COUNT(*) AS n FROM messages WHERE session_id = ? AND retired_at IS NULL").get(sessionId) as { n: number } | undefined; + return row ? row.n : null; + } finally { + conn.close(); + } +} + /** Insert a foreign message row (identity absent from any plan this test * produces) — the residue shape a second writer or a lost rewind leaves. * (Replicated from the #5408 suite.) */ @@ -117,6 +150,16 @@ function latchDrift(shadow: UnifiedShadow, id: string): void { sh.driftedSessions.add(id); } +/** Poll an async-queue effect to completion (bounded) instead of a fixed sleep. */ +async function waitFor(fn: () => boolean, what: string, timeoutMs = 5000): Promise { + const deadline = Date.now() + timeoutMs; + for (;;) { + if (fn()) return; + if (Date.now() > deadline) throw new Error(`timed out waiting for ${what}`); + await new Promise((r) => setTimeout(r, 10)); + } +} + test("#5412 A: an async drift-latched folded save sweeps the foreign row out of the index", async () => { const dir = tmpDir("latched"); try { @@ -124,17 +167,17 @@ test("#5412 A: an async drift-latched folded save sweeps the foreign row out of const ids = "5412-latched"; try { shadow.record(mkLegacy(ids, Array.from({ length: 5 }, (_, i) => msg(i)))); - await settle(); + await waitFor(() => (countMessages(dir, ids) ?? 0) >= 5, "first ingest to land 5 rows"); insertForeignRow(dir, ids); assert.equal(foreignRowRetired(dir, ids), false, "foreign row present before the latched save"); latchDrift(shadow, ids); shadow.record({ ...mkLegacy(ids, []), lastMessagesFolded: true }); - await settle(); + await waitFor(() => foreignRowRetired(dir, ids) === true, "latched folded save to run the authoritative-set sweep"); assert.equal(foreignRowRetired(dir, ids), true, "latched folded save on the async queue must run the authoritative-set sweep and retire the foreign row"); } finally { - shadow.close(); + await shadow.closeAsync(); } } finally { rmBestEffort(dir); @@ -148,19 +191,24 @@ test("#5412 B negative gate: an async UNLATCHED folded save leaves foreign rows const ids = "5412-control"; try { shadow.record(mkLegacy(ids, Array.from({ length: 5 }, (_, i) => msg(i)))); - await settle(); + await waitFor(() => (countMessages(dir, ids) ?? 0) >= 5, "first ingest to land 5 rows"); insertForeignRow(dir, ids); // No latch: the historical behavior — a folded save (no raw tail) // does not claim authoritative-set authority over the table. shadow.record({ ...mkLegacy(ids, []), lastMessagesFolded: true }); - await settle(); + await waitFor(() => stats(shadow).ingests >= 2, "folded save to process"); assert.equal(foreignRowRetired(dir, ids), false, "unlatched folded save must not sweep (no raw tail, no latch)"); } finally { - shadow.close(); + await shadow.closeAsync(); } } finally { rmBestEffort(dir); } }); + +test.after(() => { + if (PREV_UNIFIED === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = PREV_UNIFIED; +}); diff --git a/tests/issue5415-async-drift-latch.test.ts b/tests/issue5415-async-drift-latch.test.ts deleted file mode 100644 index 8d8247cae3..0000000000 --- a/tests/issue5415-async-drift-latch.test.ts +++ /dev/null @@ -1,203 +0,0 @@ -/** - * #5415: async storage backend — the drift latch never releases once engaged. - * - * #5408/#5409 gave the SYNC sidecar latched-sweep convergence: while drift- - * latched the full re-ingest sweeps foreign rows out of the index even without - * a raw tail (the envelope is what load routing serves under the latch, so the - * index must converge to it — persist.ts lazySource serves the FILE for a - * latched id on BOTH backends). The ASYNC backend twin (enqueueAsyncRecord) - * kept `{ completeLog: hasRawTail }`, so a folded-resident save (no raw tail) - * never swept here: foreign rows rode every replay, each sampled verify kept - * drifting, the watermark was cleared every time, and the latch could never - * release. - * - * Async-backend mirror of B1 in tests/issue5408-frozen-recovery.test.ts, driven - * through the REAL async path (UnifiedShadow.fromBackend + SqliteBackend -> - * record() -> enqueueAsyncRecord's serial queue): - * - * A. POSITIVE GATE. A drift-latched folded save runs the authoritative-set - * sweep and retires the foreign row out of the index. - * B. NEGATIVE GATE. An UNLATCHED folded save leaves foreign rows untouched - * (no raw tail, no latch -> no sweep authority over the table). - */ - -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 { SqliteBackend } from "../src/storage/backend.ts"; -import { openSqliteDatabase } from "../src/storage/driver.ts"; -import type { LegacySessionLike } from "../src/storage/ingest.ts"; -import type { CoreMessage } from "acp-kernel"; - -process.env.BILI_PERSIST_ZSTD = "0"; -// Same hermeticity pin as the #5408 suite: 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-5415-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; - -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 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 { - ingests: number; - verifyOk: number; - verifyDrift: number; -} - -function stats(shadow: UnifiedShadow): StatsView { - return (shadow as unknown as { stats: StatsView }).stats; -} - -/** Build an ASYNC-mode shadow (sync fast lane null) over a real SQLite store — - * record() routes through enqueueAsyncRecord's serial queue exactly like the - * mysql backend, but stays queryable via plain SQLite. */ -function openAsyncShadow(dir: string): { shadow: UnifiedShadow; store: UnifiedStore } { - const store = UnifiedStore.open(path.join(dir, "index.db")); - const shadow = UnifiedShadow.fromBackend("shadow", new SqliteBackend(store), (r) => r as LegacySessionLike); - return { shadow, store }; -} - -/** Count the session's live (non-retired) message rows. */ -function countMessages(dir: string, sessionId: string): number | null { - const conn = openSqliteDatabase(path.join(dir, "index.db")).db; - try { - const row = conn.prepare("SELECT COUNT(*) AS n FROM messages WHERE session_id = ? AND retired_at IS NULL").get(sessionId) as { n: number } | undefined; - return row ? row.n : null; - } finally { - conn.close(); - } -} - -/** Insert a foreign message row (identity absent from any plan this test - * produces) — the residue shape a second writer or a lost rewind leaves. */ -function insertForeignRow(dir: string, sessionId: string): void { - 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, "base content row must exist"); - conn - .prepare("INSERT INTO messages (session_id, seq, msg_id, role, kind, content_hash, identity) VALUES (?, ?, ?, ?, ?, ?, ?)") - .run(sessionId, 99_999, "foreign-msg", "user", "message", row.content_hash, "foreign-identity"); - } finally { - conn.close(); - } -} - -function foreignRowRetired(dir: string, sessionId: string): boolean | null { - const conn = openSqliteDatabase(path.join(dir, "index.db")).db; - try { - const row = conn.prepare("SELECT retired_at FROM messages WHERE session_id = ? AND msg_id = 'foreign-msg'").get(sessionId) as { retired_at: number | null } | undefined; - return row ? row.retired_at !== null : null; - } finally { - conn.close(); - } -} - -function latchDrift(shadow: UnifiedShadow, id: string): void { - const sh = shadow as unknown as { driftedSessions?: Set }; - if (!sh.driftedSessions) sh.driftedSessions = new Set(); - sh.driftedSessions.add(id); -} - -/** Poll an async-queue effect to completion (bounded) instead of a fixed sleep. */ -async function waitFor(fn: () => boolean, what: string, timeoutMs = 5000): Promise { - const deadline = Date.now() + timeoutMs; - for (;;) { - if (fn()) return; - if (Date.now() > deadline) throw new Error(`timed out waiting for ${what}`); - await new Promise((r) => setTimeout(r, 10)); - } -} - -test("#5415 A: a drift-latched folded save sweeps the foreign row out of the async index", async () => { - const dir = tmpDir("a-latched"); - try { - const { shadow, store } = openAsyncShadow(dir); - try { - const ids = "a-latched"; - shadow.record(mkLegacy(ids, Array.from({ length: 5 }, (_, i) => msg(i)))); - await waitFor(() => (countMessages(dir, ids) ?? 0) >= 5, "first ingest to land 5 rows"); - insertForeignRow(dir, ids); - assert.equal(foreignRowRetired(dir, ids), false, "foreign row present before the latched save"); - - latchDrift(shadow, ids); - shadow.record({ ...mkLegacy(ids, []), lastMessagesFolded: true }); - - await waitFor(() => foreignRowRetired(dir, ids) === true, "latched folded save to run the authoritative-set sweep"); - assert.equal(foreignRowRetired(dir, ids), true, "latched folded save retires the foreign row on the async path too"); - } finally { - store.close(); - } - } finally { - rmBestEffort(dir); - } -}); - -test("#5415 B negative gate: an UNLATCHED folded save leaves foreign rows untouched on the async path", async () => { - const dir = tmpDir("b-control"); - try { - const { shadow, store } = openAsyncShadow(dir); - try { - const ids = "b-control"; - shadow.record(mkLegacy(ids, Array.from({ length: 5 }, (_, i) => msg(i)))); - await waitFor(() => (countMessages(dir, ids) ?? 0) >= 5, "first ingest to land 5 rows"); - insertForeignRow(dir, ids); - - // No latch: historical behavior — a folded save (no raw tail) does - // not claim authoritative-set authority over the table. - shadow.record({ ...mkLegacy(ids, []), lastMessagesFolded: true }); - await waitFor(() => stats(shadow).ingests >= 2, "folded save to process"); - - assert.equal(foreignRowRetired(dir, ids), false, "unlatched folded save must not sweep (no raw tail, no latch)"); - } finally { - store.close(); - } - } finally { - rmBestEffort(dir); - } -}); - -test.after(() => { - if (PREV_UNIFIED === undefined) delete process.env.BILI_STORAGE_UNIFIED; - else process.env.BILI_STORAGE_UNIFIED = PREV_UNIFIED; -});