From c4983d6d2e04cee24f8353a8cd2c601ca22c71f6 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 20:16:00 +0800 Subject: [PATCH 1/3] fix: auto-quarantine a corrupt index.db instead of degrading to legacy-only forever (#3959) --- src/storage/driver.ts | 11 ++ src/storage/shadow.ts | 79 +++++++-- tests/storage-corrupt-index-selfheal.test.ts | 160 +++++++++++++++++++ 3 files changed, 240 insertions(+), 10 deletions(-) create mode 100644 tests/storage-corrupt-index-selfheal.test.ts diff --git a/src/storage/driver.ts b/src/storage/driver.ts index 6bc44be0c7..58d736daf7 100644 --- a/src/storage/driver.ts +++ b/src/storage/driver.ts @@ -246,6 +246,17 @@ function loadHostBetterSqlite3(): BetterSqlite3Ctor { return require("better-sqlite3") as unknown as BetterSqlite3Ctor; } +/** #3959: genuine file CORRUPTION (SQLITE_CORRUPT / SQLITE_NOTADB) only — + * the class where quarantining index.db is safe. Deliberately narrow: + * CANTOPEN/BUSY/permission/no-engine failures may face a perfectly healthy + * file behind an OS lock (#3823) and must NOT trip quarantine. */ +export function isCorruptIndexError(err: unknown): boolean { + const code = (err as { code?: unknown } | null)?.code; + if (code === "SQLITE_CORRUPT" || code === "SQLITE_NOTADB") return true; + const msg = err instanceof Error ? err.message : String(err); + return /database disk image is malformed/i.test(msg) || /file is not a database/i.test(msg); +} + export function openSqliteDatabase(path: string, engine?: SqliteEngineName): OpenedDatabase { if (enginesUnavailableForTests) { throw new SqliteDriverError("no SQLite engine available", "node:sqlite"); diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index 0713128015..478c4303b4 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -18,9 +18,13 @@ * Failure policy: sidecar problems must NEVER break the legacy write path. * All work is try/catch'd; after MAX_CONSECUTIVE_FAILURES consecutive * failures the sidecar disables itself for the process (warn once) — in - * full mode that also flips reads back to legacy-only. + * full mode that also flips reads back to legacy-only. Genuine index.db + * CORRUPTION at open time self-heals instead (#3959): the dead file is + * quarantined (renamed, never deleted) and a fresh index opens — the legacy + * JSON files remain the source of truth, so nothing is lost. */ +import { renameSync } from "node:fs"; import { log as loggerLog } from "../logger.js"; import { planIngest, planIngestDelta, loadLegacyView, completeBlockContents, type LegacySessionLike } from "./ingest.ts"; import type { BlockView } from "../session.js"; @@ -29,6 +33,7 @@ import { UnifiedStore, type CheckpointRow, type SessionSummaryRow, type WebSumma import { resolveBackendName, backendSelectionNotice, selectedBackendRaw, openBackend, type StorageBackend } from "./backend.ts"; import { canonicalize, sha256 } from "./canonical.ts"; import { recordSidecarInstance, recordSidecarOpenFailure } from "./runtime-status.ts"; +import { isCorruptIndexError } from "./driver.ts"; import type { SqliteEngineName, SqliteEngineSource } from "./driver.ts"; import type { MessageRow } from "./store.js"; import type { CoreMessage, MessageContentStore } from "acp-kernel"; @@ -169,26 +174,54 @@ export class UnifiedShadow { const notice = backendSelectionNotice(selected); if (notice) loggerLog("warn", notice); } - let store: UnifiedStore; + const indexPath = `${dir}/index.db`; + let store: UnifiedStore | null = null; try { - store = UnifiedStore.open(`${dir}/index.db`, undefined, { walJournalSizeLimitBytes: walLimit }); + store = UnifiedStore.open(indexPath, undefined, { walJournalSizeLimitBytes: walLimit }); // #3285: watermark trust does not cross process boundaries — a // restart-handoff overlap leaves the previous writer's rows AND // watermark behind; drop them all so this process's first save // per session re-establishes trust from its own view. store.resetAllIngestWatermarks(); } catch (err) { - // #2771: a corrupt/unreadable index.db must degrade to legacy-only - // (same shape as the encryption opt-out), never brick SessionStore - // construction. No auto-delete: the index may hold pinned checkpoint - // blobs (#2671 Phase 2) — destructive recovery is operator policy. const errStr = String(err); + // #2771 kept corrupt-index recovery manual ("delete the file") + // because DESTRUCTION was operator policy: the index may hold + // pinned checkpoint blobs (#2671 Phase 2). #3959 field report: + // that made one corrupt index.db pin the machine to legacy-only + // forever until someone deleted it by hand. QUARANTINE (rename, + // never delete) removes the manual step without the data risk — + // the legacy JSON session files remain the source of truth in + // BOTH shadow and full mode (full keeps writing them), so a + // fresh index converges as sessions save/load again, and the old + // bytes stay on disk beside the original for forensics. + let quarantinedTo: string | null = null; + if (isCorruptIndexError(err)) { + quarantinedTo = quarantineCorruptIndex(indexPath); + if (quarantinedTo) { + try { + store = UnifiedStore.open(indexPath, undefined, { walJournalSizeLimitBytes: walLimit }); + store.resetAllIngestWatermarks(); + } catch (retryErr) { + store = null; + loggerLog("error", `[storage] fresh index open after quarantine also failed (${String(retryErr)}) — staying legacy-only`); + } + } else { + loggerLog("error", `[storage] could not quarantine corrupt index ${indexPath} (${errStr}) — staying legacy-only`); + } + } + if (!store) { + loggerLog( + "warn", + `[storage] unified sidecar failed to open ${dir}/index.db (${errStr}) — staying legacy-only${sqliteOpenFailureHint(errStr)}; delete the file to reset the index`, + ); + recordSidecarOpenFailure(errStr); + return null; + } loggerLog( "warn", - `[storage] unified sidecar failed to open ${dir}/index.db (${errStr}) — staying legacy-only${sqliteOpenFailureHint(errStr)}; delete the file to reset the index`, + `[storage] unified sidecar index was corrupt (${errStr}) — quarantined to ${quarantinedTo} and opened a fresh index; sessions re-ingest from their legacy files as they are saved/loaded`, ); - recordSidecarOpenFailure(errStr); - return null; } const shadow = new UnifiedShadow(mode, store, legacyLoad ?? (() => null)); recordSidecarInstance(shadow); @@ -1031,6 +1064,32 @@ export class UnifiedShadow { } } +/** #3959: rename a corrupt index.db (+ its -wal/-shm/-journal sidecars) out + * of the way under a `.corrupt-` suffix — the same quarantine + * pattern persist.ts applies to undecodable session state (#3157): kept for + * forensics, never deleted, and the suffix keeps directory walkers off it + * (they match *.json). Returns the main file's quarantine path, or null + * when even the main rename failed (caller keeps the legacy-only degrade). + * Sidecar renames are best-effort: a stale WAL left behind cannot poison + * the fresh db — verified empirically (node:sqlite 22.x): a fresh open over + * an orphaned unclean-exit WAL resets the WAL, integrity_check stays ok. */ +function quarantineCorruptIndex(file: string): string | null { + const quarantinePath = `${file}.corrupt-${Date.now()}`; + try { + renameSync(file, quarantinePath); + } catch { + return null; + } + for (const suffix of ["-wal", "-shm", "-journal"]) { + try { + renameSync(`${file}${suffix}`, `${quarantinePath}${suffix}`); + } catch { + // absent or locked — harmless (see doc comment) + } + } + return quarantinePath; +} + function shadowDigest(view: unknown): string { return sha256(JSON.stringify(canonicalize(view))); } diff --git a/tests/storage-corrupt-index-selfheal.test.ts b/tests/storage-corrupt-index-selfheal.test.ts new file mode 100644 index 0000000000..aef84a3101 --- /dev/null +++ b/tests/storage-corrupt-index-selfheal.test.ts @@ -0,0 +1,160 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { mkdtempSync, mkdirSync, readdirSync, readFileSync, writeFileSync, truncateSync, statSync, existsSync } from "node:fs"; +import { tmpdir } from "node:os"; +import * as path from "node:path"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; +import { UnifiedStore } from "../src/storage/store.ts"; +import { setSqliteEnginesUnavailableForTests } from "../src/storage/driver.ts"; +import { _resetStorageRuntimeStatusForTest, storageStatusWire } from "../src/storage/runtime-status.ts"; +import { rmrf } from "./tmp-rm.ts"; + +/** + * #3959: a corrupt index.db used to pin a machine to legacy-only forever — + * open failed with SQLITE_CORRUPT, the log said "delete the file to reset + * the index", and every restart re-failed until someone deleted it by hand. + * Now genuine corruption-class errors quarantine the dead file (rename to + * `.corrupt-`, never delete) and open a fresh index; the legacy JSON + * session files remain the source of truth, so nothing is lost. Gate + * discipline (#3617): both directions pinned — corruption recovers, and + * non-corruption failures (CANTOPEN, no-engine) do NOT trip quarantine. + */ + +const GARBAGE = "this is definitely not a sqlite database, just plain text garbage bytes 0123456789"; + +const ENV_KEYS = ["BILI_SESSIONS_DIR", "BILI_CONFIG_FILE", "XDG_CACHE_HOME", "XDG_STATE_HOME", "BILI_STORAGE_UNIFIED", "BILI_ENCRYPTION_KEY"] as const; + +function isolateEnv(base: string, sessions: string): Record { + const snap: Record = {}; + for (const k of ENV_KEYS) snap[k] = process.env[k]; + process.env.BILI_SESSIONS_DIR = sessions; + process.env.BILI_STORAGE_UNIFIED = "shadow"; + process.env.BILI_CONFIG_FILE = path.join(base, "absent-config.json"); + process.env.XDG_CACHE_HOME = path.join(base, "cache"); + process.env.XDG_STATE_HOME = path.join(base, "state"); + delete process.env.BILI_ENCRYPTION_KEY; + return snap; +} + +function restoreEnv(snap: Record): void { + for (const k of ENV_KEYS) { + if (snap[k] === undefined) delete process.env[k]; + else process.env[k] = snap[k]; + } +} + +function quarantineFiles(sessions: string): string[] { + return readdirSync(sessions).filter((f) => f.startsWith("index.db.corrupt-")); +} + +test("#3959: non-SQLite bytes in index.db are quarantined and a fresh index opens", () => { + const base = mkdtempSync(path.join(tmpdir(), "corrupt-index-notadb-")); + const sessions = path.join(base, "sessions"); + mkdirSync(sessions, { recursive: true }); + writeFileSync(path.join(sessions, "index.db"), GARBAGE); + const snap = isolateEnv(base, sessions); + let shadow: UnifiedShadow | null = null; + try { + _resetStorageRuntimeStatusForTest(); + shadow = UnifiedShadow.maybeOpen(sessions); + assert.ok(shadow, "sidecar self-heals instead of degrading"); + assert.equal(shadow.stats.disabled, false); + assert.equal(shadow.stats.lastError, null); + const wire = storageStatusWire(); + assert.equal(wire.degraded, false, "wire reports healthy after self-heal"); + assert.equal(wire.effective, "shadow"); + const qs = quarantineFiles(sessions); + assert.equal(qs.length, 1, "exactly one quarantine file"); + assert.equal(readFileSync(path.join(sessions, qs[0]), "utf8"), GARBAGE, "original bytes preserved for forensics"); + assert.ok(existsSync(path.join(sessions, "index.db")), "fresh index.db created in place"); + } finally { + shadow?.close(); + _resetStorageRuntimeStatusForTest(); + restoreEnv(snap); + rmrf(base); + } +}); + +test("#3959: a truncated real db (database disk image is malformed) is quarantined and a fresh index opens", () => { + const base = mkdtempSync(path.join(tmpdir(), "corrupt-index-truncated-")); + const sessions = path.join(base, "sessions"); + mkdirSync(sessions, { recursive: true }); + const dbPath = path.join(sessions, "index.db"); + // Build a real db with committed data so truncation lands mid-page. + const direct = UnifiedStore.open(dbPath); + direct.setKv("selfheal-probe", "key", "x".repeat(4000)); + direct.close(); + const size = statSync(dbPath).size; + truncateSync(dbPath, Math.floor(size / 2)); + const snap = isolateEnv(base, sessions); + let shadow: UnifiedShadow | null = null; + try { + _resetStorageRuntimeStatusForTest(); + shadow = UnifiedShadow.maybeOpen(sessions); + assert.ok(shadow, "sidecar self-heals a malformed db"); + const wire = storageStatusWire(); + assert.equal(wire.degraded, false, "wire reports healthy after self-heal"); + assert.equal(wire.effective, "shadow"); + const qs = quarantineFiles(sessions); + assert.equal(qs.length, 1, "exactly one quarantine file"); + assert.equal(statSync(path.join(sessions, qs[0])).size, Math.floor(size / 2), "truncated bytes preserved for forensics"); + assert.ok(existsSync(dbPath), "fresh index.db created in place"); + } finally { + shadow?.close(); + _resetStorageRuntimeStatusForTest(); + restoreEnv(snap); + rmrf(base); + } +}); + +test("#3959 negative: CANTOPEN (a directory named index.db) degrades WITHOUT quarantining anything", () => { + const base = mkdtempSync(path.join(tmpdir(), "corrupt-index-cantopen-")); + const sessions = path.join(base, "sessions"); + mkdirSync(sessions, { recursive: true }); + // A DIRECTORY named index.db: SQLite refuses it (SQLITE_CANTOPEN) + // deterministically on every platform — the same error class as the + // AV-lock / OneDrive / integrity-label field reports (#3823), where the + // "file" situation may be perfectly fine once the OS lock clears. + mkdirSync(path.join(sessions, "index.db")); + const snap = isolateEnv(base, sessions); + try { + _resetStorageRuntimeStatusForTest(); + const shadow = UnifiedShadow.maybeOpen(sessions); + assert.equal(shadow, null, "still degrades to legacy-only"); + assert.deepEqual(quarantineFiles(sessions), [], "no quarantine side effect on CANTOPEN"); + assert.ok(existsSync(path.join(sessions, "index.db")), "the obstructing entry is left untouched"); + const wire = storageStatusWire(); + assert.equal(wire.degraded, true); + assert.ok(wire.reason?.startsWith("index database failed to open"), `reason names the open failure (got: ${wire.reason})`); + } finally { + _resetStorageRuntimeStatusForTest(); + restoreEnv(snap); + rmrf(base); + } +}); + +test("#3959 negative: a no-engine failure degrades WITHOUT quarantining anything", () => { + const base = mkdtempSync(path.join(tmpdir(), "corrupt-index-noengine-")); + const sessions = path.join(base, "sessions"); + mkdirSync(sessions, { recursive: true }); + // A valid empty db file sits there — the failure class is "no engine", + // not "bad file": quarantining would move a healthy file aside. + const direct = UnifiedStore.open(path.join(sessions, "index.db")); + direct.close(); + const snap = isolateEnv(base, sessions); + try { + _resetStorageRuntimeStatusForTest(); + setSqliteEnginesUnavailableForTests(true); + const shadow = UnifiedShadow.maybeOpen(sessions); + assert.equal(shadow, null, "still degrades to legacy-only"); + assert.deepEqual(quarantineFiles(sessions), [], "no quarantine side effect without an engine"); + const wire = storageStatusWire(); + assert.equal(wire.degraded, true); + assert.ok(wire.reason, "reason recorded"); + } finally { + setSqliteEnginesUnavailableForTests(false); + _resetStorageRuntimeStatusForTest(); + restoreEnv(snap); + rmrf(base); + } +}); From 11fae9e673d6eaf4d443e38f52502afb2fa2fd89 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 20:40:41 +0800 Subject: [PATCH 2/3] test: pin the #3959 self-heal contract in review 3 (corrupt index.db no longer degrades permanently) The pre-existing #2771 review test asserted the OLD contract for corruption-class open failures: permanent legacy-only degrade until manual file deletion. #3959 deliberately changes that class to auto-quarantine + fresh reopen, so the expectation flips from replicasAvailable()==false to true, plus quarantine-file assertions. The original safety property (SessionStore construction must NOT throw / legacy load still works) is kept verbatim. --- tests/storage-review-fixes.test.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/storage-review-fixes.test.ts b/tests/storage-review-fixes.test.ts index ea5feb6d54..e1c07dafe9 100644 --- a/tests/storage-review-fixes.test.ts +++ b/tests/storage-review-fixes.test.ts @@ -136,7 +136,7 @@ test("review 2: destroyed-substrate blocks are deleted and cannot resurrect on a }); }); -test("review 3: corrupt index.db degrades to legacy-only instead of bricking the proxy", async () => { +test("review 3: corrupt index.db self-heals via quarantine instead of bricking the proxy (#2771 → #3959)", async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-review-corrupt-")); try { const session = loadCorpusCase(dir, "folded", "corpus-folded", "anthropic"); @@ -152,18 +152,28 @@ test("review 3: corrupt index.db degrades to legacy-only instead of bricking the await store.writeNow(session); const reloaded = store.loadSync(session.id, { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); assert.ok(reloaded, "legacy fallback load must succeed"); - assert.equal(store.replicasAvailable(), false, "shadow is unavailable while degraded"); + // #3959: a corruption-class open failure no longer degrades + // permanently — the dead file is quarantined (renamed, never + // deleted) and a fresh index opens, so the sidecar IS available. + assert.equal(store.replicasAvailable(), true, "shadow must self-heal a corrupt-class index.db"); + const quarantined = fs.readdirSync(dir).filter((f) => f.startsWith("index.db.corrupt-")); + assert.equal(quarantined.length, 1, "exactly one quarantined copy of the corrupt index"); + assert.ok(fs.existsSync(path.join(dir, "index.db")), "a fresh index.db must exist after self-heal"); await store.flushAll(); // flush the JSON record to disk (closeQuietly only cancels) } finally { store!.closeQuietly(); delete process.env.BILI_STORAGE_UNIFIED; } - // And a later process (fresh handle) still works the same way. + // And a later process (fresh handle) sees the HEALED index — no + // re-degrade, no second quarantine. process.env.BILI_STORAGE_UNIFIED = "shadow"; try { const again = new SessionStore({ dir, debounceMs: 1, enabled: true }); - assert.ok(again.loadSync(session.id, { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }), "second process must load via legacy"); + assert.ok(again.loadSync(session.id, { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }), "second process must load"); + assert.equal(again.replicasAvailable(), true, "second process must see the healed index, not re-degrade"); + const quarantined = fs.readdirSync(dir).filter((f) => f.startsWith("index.db.corrupt-")); + assert.equal(quarantined.length, 1, "no second quarantine on the healthy index"); again.closeQuietly(); } finally { delete process.env.BILI_STORAGE_UNIFIED; From 1158918b615f8ce5600f3ea3e325a01c7e27f589 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 21 Oct 2026 01:35:29 +0800 Subject: [PATCH 3/3] test: align the stale #2771/#3040 pins with the #3959 self-heal contract MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI auto-fix (issue #3965): two pre-existing pins asserted the OLD corrupt-index behavior (permanent legacy-only degrade) that this PR intentionally replaced with quarantine + fresh-open self-heal: - web-storage-banner (#3040): now asserts effective=full, degraded=false, live engine, and the .corrupt- quarantine artifact with the exact corrupt bytes preserved. - storage-engine-proofs (#2771): re-pointed the #2781 handle-release roundtrip to AFTER closeQuietly — under self-heal the store legitimately holds a live index.db handle until close, and Windows refuses to move a directory containing open handles (the old mid-test placement threw there, skipping cleanup and surfacing as a masked EBUSY in rmrf). Adds the forensics assertions (one quarantine copy, bytes verbatim). --- tests/storage-engine-proofs.test.ts | 54 ++++++++++++++++++----------- tests/web-storage-banner.test.ts | 25 ++++++++----- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/tests/storage-engine-proofs.test.ts b/tests/storage-engine-proofs.test.ts index 97b02837b0..3ea8b597e2 100644 --- a/tests/storage-engine-proofs.test.ts +++ b/tests/storage-engine-proofs.test.ts @@ -22,8 +22,9 @@ * loads with the exact digest. * SELF-DISABLE — 3 consecutive sidecar failures disable it; reads flip * back to legacy; the legacy file stays readable. - * CORRUPT INDEX — a corrupt index.db degrades the store to legacy-only - * instead of bricking construction (#2771). + * CORRUPT INDEX — a corrupt index.db self-heals by quarantine instead of + * bricking construction or pinning legacy-only forever + * (#2771 guard, #3959 recovery). * CAS REFCOUNT — shared content blobs survive deleteSession of one * session and are collected only at refcount 0. * @@ -375,10 +376,12 @@ test("storage engine: sidecar self-disables after 3 consecutive failures (reads }); // --------------------------------------------------------------------------- -// CORRUPT INDEX (#2771): construction degrades to legacy-only. +// CORRUPT INDEX (#2771/#3959): construction survives via quarantine — the +// dead index is renamed aside (never deleted), a fresh one opens, and reads +// recover from the legacy files. The machine is never pinned legacy-only. // --------------------------------------------------------------------------- -test("storage engine: corrupt index.db degrades to legacy-only instead of bricking construction (#2771)", async () => { +test("storage engine: corrupt index.db self-heals by quarantine instead of bricking construction or pinning legacy-only (#2771/#3959)", async () => { const dir = tmpdir("bili-engproof-corrupt-"); const prev = process.env.BILI_STORAGE_UNIFIED; try { @@ -388,37 +391,48 @@ test("storage engine: corrupt index.db degrades to legacy-only instead of bricki await store.writeNow(session); store.closeQuietly(); - // The clean read through full mode — the digest the corrupt-index - // fallback must reproduce exactly from the legacy file. + // The clean read through full mode — the digest the post-quarantine + // recovery must reproduce exactly from the legacy file. const clean = new SessionStore({ dir, debounceMs: 1, enabled: true }); const viaFull = clean.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); clean.closeQuietly(); assert.ok(viaFull, "full-mode read returned null"); - const digestClean = viewDigest(toLegacyLike(viaFull)); // Corrupt the index header (crashed checkpoint / stray write). const fd = fs.openSync(path.join(dir, "index.db"), "r+"); fs.writeSync(fd, Buffer.alloc(512, 0x5a), 0, 512, 0); fs.closeSync(fd); - // THE #2771 regression: construction must survive, reads must fall - // back to the legacy file, and the digest must be exact. + // THE #2771/#3959 regression pair: construction must survive AND the + // store must come back in full mode (not a permanent legacy pin). const store2 = new SessionStore({ dir, debounceMs: 1, enabled: true }); - const reloaded = store2.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); - store2.cancelAll(); - assert.ok(reloaded, "legacy fallback returned null over a corrupt index"); + try { + const reloaded = store2.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + assert.ok(reloaded, "post-quarantine read returned null over the fresh index"); + assert.equal(viewStateDigest(toLegacyLike(reloaded)), viewStateDigest(toLegacyLike(viaFull)), "self-healed read state digest drift vs clean full-mode read"); + // Writes keep working against the fresh index — no throw. + await store2.writeNow(reloaded); + } finally { + store2.closeQuietly(); + } - // Regression (#2781): the failed open must have CLOSED its handle — - // Windows refuses to move/delete a directory while any process holds - // a file inside it open. On POSIX the rename is a no-op that always - // succeeds, so this guard only bites where the bug lives. + // Regression (#2781): after close NOTHING may still hold a handle in + // the directory — Windows refuses to move/delete a directory while + // any process holds a file inside it open (the healed store's live + // index.db handle would trip exactly this guard if close leaked it). + // On POSIX the rename is a no-op that always succeeds, so it only + // bites where the bug lives. const moved = dir + "-moved"; fs.renameSync(dir, moved); fs.renameSync(moved, dir); - assert.equal(viewStateDigest(toLegacyLike(reloaded)), viewStateDigest(toLegacyLike(viaFull)), "corrupt-index fallback state digest drift vs clean full-mode read"); - // Writes must keep working (legacy path) — no throw, file refreshed. - await store2.writeNow(reloaded); - store2.closeQuietly(); + + // Forensics contract: the corrupt bytes survive beside the fresh + // index, never deleted, under the .corrupt- suffix. + const quarantined = fs.readdirSync(dir).filter((n) => n.startsWith("index.db.corrupt-")); + assert.equal(quarantined.length, 1, "exactly one quarantine copy of the dead index"); + const qHead = fs.readFileSync(path.join(dir, quarantined[0])).subarray(0, 512); + assert.ok(qHead.every((b) => b === 0x5a), "quarantined header keeps the corrupt bytes verbatim"); + assert.ok(fs.existsSync(path.join(dir, "index.db")), "a fresh index.db lives next to the quarantine"); } finally { if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; else process.env.BILI_STORAGE_UNIFIED = prev; diff --git a/tests/web-storage-banner.test.ts b/tests/web-storage-banner.test.ts index 5d4b44fcad..0b30211697 100644 --- a/tests/web-storage-banner.test.ts +++ b/tests/web-storage-banner.test.ts @@ -1,7 +1,9 @@ // #3040 web surface: SQLite unified storage degrades to legacy-only through // several fail-open paths (#2825 contract): engine ladder exhausted, -// self-download blocked in cooldown, corrupt index.db guard (#2771), 3-strike -// sidecar disable. The proxy keeps running on JSON — correct — but until now +// self-download blocked in cooldown, 3-strike sidecar disable. (A corrupt +// index.db is NO LONGER one of them since #3959: it self-heals by quarantine +// instead of pinning the machine legacy-only forever.) The proxy keeps +// running on JSON — correct — but until now // the condition was only visible as one-time log lines, so environments that // cannot run SQLite were never learned about before the upcoming full-mode // default flip. These tests pin the wire contract the web banner reads: @@ -20,7 +22,7 @@ import test from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; -import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs"; +import { mkdtempSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { defaultConfig } from "acp-kernel"; @@ -299,7 +301,7 @@ test("#3040 BILI_ENCRYPTION_KEY opt-out is intentional: reported, never warned", } }); -test("#3040 corrupt index.db (guard #2771): degraded with the open-failure reason", { skip: !(probeBuiltinSqlite() || probeHostBetterSqlite3()) && "no SQLite engine available on this host" }, async () => { +test("#3040 corrupt index.db (guard #2771): self-heals by quarantine — no permanent legacy pin, no banner (#3959)", { skip: !(probeBuiltinSqlite() || probeHostBetterSqlite3()) && "no SQLite engine available on this host" }, async () => { const snap = saveEnv(); const base = mkdtempSync(path.join(tmpdir(), "bili-storage-banner-")); let store: SessionStore | null = null; @@ -309,7 +311,8 @@ test("#3040 corrupt index.db (guard #2771): degraded with the open-failure reaso delete process.env.BILI_STORAGE_UNIFIED; delete process.env.BILI_ENCRYPTION_KEY; const dir = process.env.BILI_SESSIONS_DIR!; - writeFileSync(path.join(dir, "index.db"), Buffer.from("this is definitely not a sqlite database file".repeat(8))); + const corruptBytes = Buffer.from("this is definitely not a sqlite database file".repeat(8)); + writeFileSync(path.join(dir, "index.db"), corruptBytes); store = new SessionStore({ dir, enabled: true }); _setStoreForTest(store); const upstreamPort = await freePort(); @@ -319,11 +322,15 @@ test("#3040 corrupt index.db (guard #2771): degraded with the open-failure reaso try { const o = await fetchJson<{ storage: StorageWire }>(port, "/__bili/overview"); assert.equal(o.storage.requested, "full"); - assert.equal(o.storage.effective, "legacy"); - assert.equal(o.storage.degraded, true, "corrupt index.db trips the warning"); + assert.equal(o.storage.effective, "full", "self-heal recovers full mode instead of pinning legacy-only (#3959)"); + assert.equal(o.storage.degraded, false, "quarantine + fresh open must NOT raise the warning"); assert.equal(o.storage.intentionalOptOut, false); - assert.match(o.storage.reason ?? "", /index database failed to open/); - assert.equal(o.storage.engine, null); + assert.equal(o.storage.reason, null); + assert.ok(o.storage.engine, "the fresh index reports a live engine"); + // Forensics contract: the dead file is renamed aside, never deleted. + const quarantined = readdirSync(dir).filter((n) => n.startsWith("index.db.corrupt-")); + assert.equal(quarantined.length, 1, "exactly one quarantine copy beside the fresh index"); + assert.deepEqual(readFileSync(path.join(dir, quarantined[0])), corruptBytes, "quarantine preserves the exact corrupt bytes"); } finally { await close(proxy); }