From 7b825c800198764b6b17c0c709535c07c8ea7048 Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 10:53:43 +0800 Subject: [PATCH 1/2] fix(persist): recover cross-route fold ledger on cold load after provider switch (#3399) A provider switch mid-conversation leaves the fold ledger under its old route name (/_.json); the cold load after restart probed only the requested route, so the session came back empty (requests=0, blocks=0) and the client re-sent the full unfolded history - refolded from scratch or rejected with a local 413 at the doomed-probe gate. - loadEnvelope resolves same-protocol namespaced files for a conversation id as ONE candidate pool when the request carries protocol meta: the computed exact-route name plus walked siblings sharing the id's sha suffix (.fb.json spills reconciled to their canonical copy by savedAt), each re-verified for body id and protocol (cross-protocol isolation), corrupt members quarantined per #3157. Winner by fold progress - requests, then nextBlockId (staleDiskPayload discipline), exact route on ties - never by probe order or savedAt, so a fresh-but-empty current-route twin loses to the richer old-route ledger and a successfully continued branch is never rolled back by a stale one. - A session restored from an off-route file adopts the requesting upstream origin (getSession fill-if-absent cannot override a stale-but-present one), so the next save converges under the live route instead of re-forking. Label/title are identity and stay put; no-meta callers keep byte-identical probe behavior. Tests: tests/issue3399-cross-route-load.test.ts (11 legs incl. both-twin arbitration, convergence, no-rollback, pure miss, corrupt fail-closed, tie-break, cross-protocol isolation, CCR sidecar, legacy lane). Closes #3399 --- src/persist.ts | 95 ++++++- tests/issue3399-cross-route-load.test.ts | 299 +++++++++++++++++++++++ 2 files changed, 391 insertions(+), 3 deletions(-) create mode 100644 tests/issue3399-cross-route-load.test.ts diff --git a/src/persist.ts b/src/persist.ts index d8e9c5bc0a..991dbf327c 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -1106,10 +1106,17 @@ export class SessionStore { } } - /** Shared envelope probe for the sync read paths: namespaced path - * first, then the _unknown/ location, letting the kernel store layer - * also check its discovered map and the flat legacy name. */ + /** Shared envelope probe for the sync read paths. When the request names + * a protocol, the namespaced files for this id form ONE candidate pool + * (#3399 — see loadNamespacedPool); on pool miss (or without protocol + * meta) the probes fall back to the _unknown/ location, letting the + * kernel store layer also check its discovered map and the flat legacy + * name. */ private loadEnvelope(id: string, meta?: { protocol?: string; upstreamOrigin?: string }): PersistedEnvelope | null { + if (meta?.protocol) { + const pooled = this.loadNamespacedPool(id, { protocol: meta.protocol, upstreamOrigin: meta.upstreamOrigin }); + if (pooled) return pooled; + } const livePaths = new Set([ path.join(this.sessionsDir, relPathFor(id, meta?.protocol, meta?.upstreamOrigin)), path.join(this.sessionsDir, relPathFor(id)), @@ -1150,6 +1157,86 @@ export class SessionStore { return this.store.loadSync(id, relPathFor(id, meta?.protocol, meta?.upstreamOrigin)); } + /** #3399: cross-route ledger recovery. The conversation id is the identity; + * the host prefix in the filename is display metadata derived from the + * request's upstream origin (#280/#286 — those mutate mid-conversation). + * A provider switch therefore leaves the fold ledger under its OLD route + * name while the probes below target only the NEW one — the cold load + * returns empty state and the client re-sends the full unfolded history + * (refold from scratch, or a local 413 once the core text exceeds the + * doomed-probe gate). Collect every same-protocol file whose name carries + * this id's sha suffix — the computed exact-route name always (covers + * files created after boot, which the walked index lacks), plus the + * walked siblings — and pick the winner by fold progress: requests, then + * nextBlockId, mirroring staleDiskPayload's clobber guard. Counters + * measure how much folded history a copy carries; savedAt only records + * who last touched disk and would crown a fresh-but-empty twin, so it is + * tie-break material at best. Canonical/spill (.fb.json) pairs reconcile + * to their freshest copy first (kernel loadAll convention); corrupt + * members still quarantine per #3157; a pool with no surviving member is + * a miss and falls through to the legacy probes below. */ + private loadNamespacedPool(id: string, meta: { protocol: string; upstreamOrigin?: string }): PersistedEnvelope | null { + const proto = meta.protocol; + const dir = path.join(this.sessionsDir, proto); + const logicalFor = (file: string): string => file.endsWith(".fb.json") ? `${file.slice(0, -".fb.json".length)}.json` : file; + const candidates = new Set([path.join(dir, path.basename(relPathFor(id, proto, meta.upstreamOrigin)))]); + const suffix = `_${path.basename(relPathFor(id))}`; + for (const file of this.diskFiles) { + const base = path.basename(file); + if (base.endsWith(".content-store.json")) continue; + if (path.dirname(file) !== dir || !logicalFor(base).endsWith(suffix)) continue; + candidates.add(file); + } + const valid = new Map }>(); + for (const file of candidates) { + if (!existsSync(file)) continue; + const env = this.readEnvelopeFile(file, id); + if (!env) continue; + const savedProtocol = env.payload.meta?.protocol ?? env.payload.protocol; + if (savedProtocol && savedProtocol !== proto) continue; + const prior = valid.get(logicalFor(file)); + if (!prior || (env.savedAt ?? 0) > (prior.env.savedAt ?? 0)) valid.set(logicalFor(file), { file, env }); + } + if (valid.size === 0) return null; + const exact = path.join(this.sessionsDir, relPathFor(id, proto, meta.upstreamOrigin)); + const winner = [...valid.values()].sort((a, b) => { + const ra = a.env.payload.stats?.requests ?? a.env.payload.requests ?? 0; + const rb = b.env.payload.stats?.requests ?? b.env.payload.requests ?? 0; + if (ra !== rb) return rb - ra; + const na = a.env.payload.state?.nextBlockId ?? 0; + const nb = b.env.payload.state?.nextBlockId ?? 0; + if (na !== nb) return nb - na; + if (a.file === exact) return -1; + if (b.file === exact) return 1; + return (b.env.savedAt ?? 0) - (a.env.savedAt ?? 0); + })[0]; + this.sourceFiles.set(id, winner.file); + if (winner.file !== exact) { + this.log("info", `[persist] restored ${id} from another upstream route (${path.basename(winner.file)}; ${winner.env.payload.stats?.requests ?? winner.env.payload.requests ?? 0} requests) under protocol ${proto} (#3399)`); + } + return winner.env; + } + + /** #3399: a cold load that recovered a session from a file NOT on the + * requested route must adopt the requesting route's upstream origin — + * getSession's fill-if-absent merge cannot override a stale-but-present + * origin, and without adoption every subsequent save re-forks under the + * old host name (the degraded twin survives, the restored ledger never + * converges onto the live route). Adopting makes the next save rewrite + * the current-route file with the full content. Only the origin is + * adopted: the protocol of a surviving candidate already equals the + * request's (pool isolation / alias re-check), and label/title stay put + * — they are identity, not route. */ + private adoptRequestedRoute(id: string, session: Session, meta?: { protocol?: string; upstreamOrigin?: string }): void { + if (!meta?.protocol || !meta.upstreamOrigin) return; + const src = this.sourceFiles.get(id); + if (!src || src === path.join(this.sessionsDir, relPathFor(id, meta.protocol, meta.upstreamOrigin))) return; + if (session.meta.upstreamOrigin === meta.upstreamOrigin) return; + session.meta.upstreamOrigin = meta.upstreamOrigin; + this.log("info", `[persist] adopted route ${hostLabel(meta.upstreamOrigin)} for ${id} restored from ${path.basename(src)} (#3399)`); + this.scheduleSave(session); + } + /** Synchronous reload of a single session. Used on a memory miss (after * LRU eviction). Sync fs is acceptable here because a miss is rare and * reads a single small file (~1ms). Returns null if missing/corrupt or the @@ -1204,6 +1291,7 @@ export class SessionStore { this.log("info", `[persist] clamped negative token stats on reload for ${id} (#408)`); } this.logColdLoad(id, "legacy file", session, envelope.payload.savedAt); + this.adoptRequestedRoute(id, session, meta); return session; } const envelope = this.loadEnvelope(id, meta); @@ -1221,6 +1309,7 @@ export class SessionStore { this.log("info", `[persist] clamped negative token stats on reload for ${id} (#408)`); } this.logColdLoad(id, "legacy file", session, envelope.payload.savedAt); + this.adoptRequestedRoute(id, session, meta); return session; } diff --git a/tests/issue3399-cross-route-load.test.ts b/tests/issue3399-cross-route-load.test.ts new file mode 100644 index 0000000000..419642b165 --- /dev/null +++ b/tests/issue3399-cross-route-load.test.ts @@ -0,0 +1,299 @@ +// #3399: a provider switch mid-conversation leaves the fold ledger under its +// OLD route name (/_.json); the cold load after a +// restart probed only the NEW route → empty session → full unfolded history +// re-sent → refold from scratch or a local 413 at the doomed-probe gate. +// Namespaced files for a conversation id must resolve as ONE candidate pool +// arbitrated by fold progress (requests → nextBlockId), never by probe order +// or savedAt — a fresh-but-empty current-route twin must lose to the richer +// old-route ledger, and a successfully continued branch must never be rolled +// back by a stale one. +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import * as path from "node:path"; +import { SessionStore, _setStoreForTest, relPathFor } from "../src/persist.ts"; +import { createSession, _resetSessionsForTest } from "../src/session.ts"; +import type { WireProtocol } from "../src/util.ts"; + +const ID = "01a12b4a-conv-native-id"; +const ORIGIN_A = "https://a.example.com/v1"; +const ORIGIN_B = "https://b.example.com/v1"; +const PROTO: WireProtocol = "responses"; +const BOOT_LIMITS = { maxSessions: 8, maxBytes: 67_108_864 }; + +interface SeedOpts { + origin?: string; + protocol?: WireProtocol; + requests: number; + blockText: string; +} + +async function seedOnce(dir: string, opts: SeedOpts): Promise { + _resetSessionsForTest(256, 512 * 1024 * 1024); + const store = new SessionStore({ dir, enabled: true, debounceMs: 0 }); + _setStoreForTest(store); + try { + const s = createSession(ID, { protocol: opts.protocol ?? PROTO, upstreamOrigin: opts.origin ?? ORIGIN_A, label: ID }); + s.stats.requests = opts.requests; + s.state.blocks.push({ id: "b1", startRef: "m00001", endRef: "m00120", tokens: 12_000, createdAt: Date.now() } as never); + s.blockContents.set("b1", { one: null, full: { text: opts.blockText, count: 1 } }); + await store.writeNow(s); + } finally { + store.closeQuietly(); + _resetSessionsForTest(256, 512 * 1024 * 1024); + _setStoreForTest(new SessionStore({ enabled: false })); + } +} + +function bootStore(dir: string): SessionStore { + _resetSessionsForTest(256, 512 * 1024 * 1024); + const store = new SessionStore({ dir, enabled: true, debounceMs: 0 }); + _setStoreForTest(store); + return store; +} + +function teardown(store: SessionStore | null, root: string): void { + store?.closeQuietly(); + _resetSessionsForTest(256, 512 * 1024 * 1024); + _setStoreForTest(new SessionStore({ enabled: false })); + rmSync(root, { recursive: true, force: true }); +} + +function readRecord(dir: string, rel: string): Record { + const raw = JSON.parse(readFileSync(path.join(dir, rel), "utf8")) as Record; + return raw && typeof raw.payload === "object" && raw.payload !== null ? raw.payload as Record : raw; +} + +function blockTextOf(sessionOrRecord: unknown): string { + const bc = (sessionOrRecord as { blockContents?: unknown }).blockContents; + if (bc instanceof Map) return (bc.get("b1") as { full?: { text?: string } } | undefined)?.full?.text ?? ""; + const rec = bc as Record | undefined; + return rec?.b1?.full?.text ?? ""; +} + +test("#3399 production shape: degraded current-route twin loses to the richer old-route ledger (shadow default)", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-twin-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "intact A-era folded originals" }); + await seedOnce(dir, { origin: ORIGIN_B, requests: 2, blockText: "fresh B-era rebuild" }); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded, "cross-route cold load must recover the ledger"); + assert.equal(reloaded.stats.requests, 3697, "richer old-route ledger must win over the degraded twin"); + assert.equal(blockTextOf(reloaded), "intact A-era folded originals"); + // Route adoption: the restored session serves the requested route so + // the next save converges instead of re-forking under the old host. + assert.equal(reloaded.meta.upstreamOrigin, ORIGIN_B, "restored session must adopt the requesting route"); + assert.equal(reloaded.meta.label, ID, "identity (label) must stay put"); + } finally { + teardown(store, root); + } +}); + +test("#3399 convergence: save after cross-route restore lands under the requested route with full content", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-converge-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "intact A-era folded originals" }); + await seedOnce(dir, { origin: ORIGIN_B, requests: 2, blockText: "fresh B-era rebuild" }); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded); + await store.writeNow(reloaded); + const relB = relPathFor(ID, PROTO, ORIGIN_B); + assert.ok(existsSync(path.join(dir, relB)), "post-restore save must land under the requested route"); + const recB = readRecord(dir, relB); + assert.equal((recB.stats as { requests?: number })?.requests ?? (recB.requests as number | undefined), 3697); + assert.equal(blockTextOf(recB), "intact A-era folded originals", "degraded twin must be overwritten with the restored content"); + } finally { + teardown(store, root); + } +}); + +test("#3399 normal continuation: a successfully continued current-route branch is never rolled back by the stale old ledger", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-norollback-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 2, blockText: "stale A-era fragment" }); + await seedOnce(dir, { origin: ORIGIN_B, requests: 3697, blockText: "continued B-era ledger" }); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded); + assert.equal(reloaded.stats.requests, 3697, "the branch with more fold progress must win"); + assert.equal(blockTextOf(reloaded), "continued B-era ledger"); + } finally { + teardown(store, root); + } +}); + +test("#3399 pure miss: only the old route exists when the daemon reboots on the new route", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-puremiss-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "intact A-era folded originals" }); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded, "pure-miss cold load must recover the ledger"); + assert.equal(reloaded.stats.requests, 3697); + assert.equal(blockTextOf(reloaded), "intact A-era folded originals"); + } finally { + teardown(store, root); + } +}); + +test("#3399 same-route control: unchanged behavior for the exact route", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-sameroute-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "same-route ledger" }); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_A }); + assert.ok(reloaded); + assert.equal(reloaded.stats.requests, 3697); + assert.equal(blockTextOf(reloaded), "same-route ledger"); + } finally { + teardown(store, root); + } +}); + +test("#3399 fail-closed: corrupt richer sibling quarantines (#3157) and the degraded twin serves", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-corrupt-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "intact A-era folded originals" }); + await seedOnce(dir, { origin: ORIGIN_B, requests: 2, blockText: "fresh B-era rebuild" }); + const relA = relPathFor(ID, PROTO, ORIGIN_A); + writeFileSync(path.join(dir, relA), "{ not json"); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded, "corruption of one twin must not nuke the other"); + assert.equal(reloaded.stats.requests, 2, "degraded twin serves when the richer one is undecodable"); + assert.ok(!existsSync(path.join(dir, relA)), "corrupt file must be isolated from the live name"); + const quarantined = readdirSync(dir).length >= 0 && readdirSync(path.join(dir, PROTO)).filter(f => f.includes(".corrupt-")).length; + assert.equal(quarantined, 1, "exactly one .corrupt- forensics copy beside the original"); + } finally { + teardown(store, root); + } +}); + +test("#3399 fail-closed: every candidate corrupt → honest fresh start (null)", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-allcorrupt-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "intact A-era folded originals" }); + await seedOnce(dir, { origin: ORIGIN_B, requests: 2, blockText: "fresh B-era rebuild" }); + writeFileSync(path.join(dir, relPathFor(ID, PROTO, ORIGIN_A)), "{ not json"); + writeFileSync(path.join(dir, relPathFor(ID, PROTO, ORIGIN_B)), "{ not json"); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.equal(reloaded, null, "no surviving candidate means a fresh start, not a guess"); + const quarantined = readdirSync(path.join(dir, PROTO)).filter(f => f.includes(".corrupt-")).length; + assert.equal(quarantined, 2, "both corrupt twins quarantined for forensics"); + } finally { + teardown(store, root); + } +}); + +test("#3399 tie-break: equal fold progress prefers the requested route deterministically", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-tie-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 50, blockText: "tie-A" }); + await seedOnce(dir, { origin: ORIGIN_B, requests: 50, blockText: "tie-B" }); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded); + assert.equal(blockTextOf(reloaded), "tie-B", "indistinguishable progress → the live route owns the slot"); + } finally { + teardown(store, root); + } +}); + +test("#3399 cross-protocol isolation: the same id under another protocol dir is never picked", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-xproto-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, protocol: "anthropic", requests: 3697, blockText: "anthropic-era ledger" }); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.equal(reloaded, null, "different protocol = different wire shape; no silent adoption"); + } finally { + teardown(store, root); + } +}); + +test("#3399 CCR sidecar follows the restored source file (originals survive the route switch)", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-ccr-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "intact A-era folded originals" }); + const relA = relPathFor(ID, PROTO, ORIGIN_A); + const sidecar = { version: 1, byHash: { h1: { text: "original bytes", count: 1 } }, byRef: { m00001: "h1" } }; + writeFileSync(path.join(dir, relA.slice(0, -".json".length) + ".content-store.json"), JSON.stringify(sidecar)); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded); + const cs = store.loadContentStore(reloaded); + assert.ok(cs, "content store must resolve through the restored source file"); + assert.deepEqual(cs?.byRef, { m00001: "h1" }); + } finally { + teardown(store, root); + } +}); + +test("#3399 legacy storage mode: same recovery on the explicit legacy lane", async () => { + const prevUnified = process.env.BILI_STORAGE_UNIFIED; + const prevCfg = process.env.BILI_CONFIG_FILE; + process.env.BILI_STORAGE_UNIFIED = "legacy"; + delete process.env.BILI_CONFIG_FILE; + const root = mkdtempSync(path.join(tmpdir(), "bili-3399-legacy-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "intact A-era folded originals" }); + await seedOnce(dir, { origin: ORIGIN_B, requests: 2, blockText: "fresh B-era rebuild" }); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded, "legacy mode must recover the ledger too"); + assert.equal(reloaded.stats.requests, 3697); + assert.equal(blockTextOf(reloaded), "intact A-era folded originals"); + } finally { + teardown(store, root); + if (prevUnified === undefined) delete process.env.BILI_STORAGE_UNIFIED; else process.env.BILI_STORAGE_UNIFIED = prevUnified; + if (prevCfg !== undefined) process.env.BILI_CONFIG_FILE = prevCfg; + } +}); From c92da53cd3a17db60bc5bd457a592e08a7d4e3ca Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 14:13:06 +0800 Subject: [PATCH 2/2] fix(persist): bind cross-route CCR originals to the winning source (#3765 review) readContentStoreRels probed the request's current-route sidecar before the winning ledger's own content-store. adoptRequestedRoute rewrites session.meta.upstreamOrigin to the requesting route during loadSync, so that current-route path targeted a degraded twin whose valid-but-incomplete CCR shadowed the richer recovered ledger's originals (missing refs -> DIGEST DRIFT) on the legacy lane. Probe the sourceFiles-derived CCR first so originals always follow the same file the fold ledger was loaded from; derive its canonical .content-store.json name for .fb.json fallback sources (CCRs are never .fb-named). Adds two regression legs (shadowing; .fb.json->canonical) to tests/issue3399-cross-route-load.test.ts; both fail on the pre-fix code. Pre-merge coverage from the #3765 review. --- src/persist.ts | 15 +++++- tests/issue3399-cross-route-load.test.ts | 66 +++++++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/persist.ts b/src/persist.ts index 991dbf327c..14c8b3dbe3 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -507,9 +507,20 @@ export class SessionStore { private readContentStoreRels(id: string, rels: string[]): MessageContentStore | null { if (!this.enabled) return null; + // #3399/#3765: probe the winning source's CCR BEFORE rels[0]. Adoption + // rewrites meta.upstreamOrigin to the requesting route during loadSync, + // so rels[0] targets that route's sidecar — a degraded current-route twin + // with a valid-but-incomplete CCR would otherwise shadow the winning + // ledger's originals (missing refs → DIGEST DRIFT). A .fb.json fallback + // source keeps its canonical .content-store.json name. const source = this.sourceFiles.get(id); - const paths = [...rels.map(rel => path.join(this.sessionsDir, rel)), - ...(source ? [source.replace(/\.json$/, ".content-store.json")] : []), + const sourceCcr = source + ? (source.endsWith(".fb.json") + ? `${source.slice(0, -".fb.json".length)}.content-store.json` + : `${source.slice(0, -".json".length)}.content-store.json`) + : null; + const paths = [...(sourceCcr ? [sourceCcr] : []), + ...rels.map(rel => path.join(this.sessionsDir, rel)), ...this.diskFiles.filter(file => fileNameMatchesId(path.basename(file), id)) .map(file => file.replace(/\.json$/, ".content-store.json"))]; for (const file of new Set(paths)) { diff --git a/tests/issue3399-cross-route-load.test.ts b/tests/issue3399-cross-route-load.test.ts index 419642b165..9cff5f7863 100644 --- a/tests/issue3399-cross-route-load.test.ts +++ b/tests/issue3399-cross-route-load.test.ts @@ -9,7 +9,7 @@ // back by a stale one. import assert from "node:assert/strict"; import { test } from "node:test"; -import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import * as path from "node:path"; import { SessionStore, _setStoreForTest, relPathFor } from "../src/persist.ts"; @@ -297,3 +297,67 @@ test("#3399 legacy storage mode: same recovery on the explicit legacy lane", asy if (prevCfg !== undefined) process.env.BILI_CONFIG_FILE = prevCfg; } }); + +// #3765 review (@darkness-A): the sidecar must follow the WINNING source, not +// the request's current route — adoption rewrites meta.upstreamOrigin before +// the lazy CCR load, so a degraded current-route twin with a valid-but- +// incomplete CCR must not shadow the richer ledger's originals. +test("#3765 CCR: degraded current-route twin's sidecar must not shadow the winning ledger's originals", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3765-ccr-shadow-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "intact A-era folded originals" }); + await seedOnce(dir, { origin: ORIGIN_B, requests: 2, blockText: "fresh B-era rebuild" }); + const ccrA = { version: 1, byHash: { hA: { text: "A original bytes", count: 2 } }, byRef: { m00001: "hA", m00002: "hA" } }; + const ccrB = { version: 1, byHash: { hB: { text: "B partial bytes", count: 1 } }, byRef: { m00001: "hB" } }; + const relA = relPathFor(ID, PROTO, ORIGIN_A); + const relB = relPathFor(ID, PROTO, ORIGIN_B); + writeFileSync(path.join(dir, relA.slice(0, -".json".length) + ".content-store.json"), JSON.stringify(ccrA)); + writeFileSync(path.join(dir, relB.slice(0, -".json".length) + ".content-store.json"), JSON.stringify(ccrB)); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded, "cross-route cold load must recover the richer ledger"); + assert.equal(reloaded.stats.requests, 3697, "richer old-route ledger must win"); + const cs = store.loadContentStore(reloaded); + assert.ok(cs, "content store must resolve"); + assert.deepEqual(cs?.byRef, ccrA.byRef, "originals must come from the winning source, not the degraded current-route twin"); + assert.notDeepEqual(cs?.byRef, ccrB.byRef, "must not be shadowed by the new-route sidecar"); + } finally { + teardown(store, root); + } +}); + +// #3765 review (@darkness-A): a .fb.json fallback source keeps its canonical +// .content-store.json name — deriving *.fb.content-store.json would miss. +test("#3765 CCR: a .fb.json fallback source resolves its canonical content-store, not *.fb.content-store.json", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-3765-ccr-fb-")); + const dir = path.join(root, "sessions"); + let store: SessionStore | null = null; + try { + await seedOnce(dir, { origin: ORIGIN_A, requests: 3697, blockText: "intact A-era folded originals" }); + await seedOnce(dir, { origin: ORIGIN_B, requests: 2, blockText: "fresh B-era rebuild" }); + // Demote A's ledger to its .fb.json fallback name; its CCR stays canonical. + const relA = relPathFor(ID, PROTO, ORIGIN_A); + const absA = path.join(dir, relA); + renameSync(absA, absA.slice(0, -".json".length) + ".fb.json"); + const ccrA = { version: 1, byHash: { hA: { text: "A original bytes", count: 2 } }, byRef: { m00001: "hA", m00002: "hA" } }; + const ccrB = { version: 1, byHash: { hB: { text: "B partial bytes", count: 1 } }, byRef: { m00001: "hB" } }; + writeFileSync(path.join(dir, relA.slice(0, -".json".length) + ".content-store.json"), JSON.stringify(ccrA)); + const relB = relPathFor(ID, PROTO, ORIGIN_B); + writeFileSync(path.join(dir, relB.slice(0, -".json".length) + ".content-store.json"), JSON.stringify(ccrB)); + + store = bootStore(dir); + await store.boot(BOOT_LIMITS); + const reloaded = store.loadSync(ID, { protocol: PROTO, upstreamOrigin: ORIGIN_B }); + assert.ok(reloaded, "fallback-name ledger must still recover"); + assert.equal(reloaded.stats.requests, 3697); + const cs = store.loadContentStore(reloaded); + assert.ok(cs, "content store must resolve through the .fb.json source's canonical sidecar"); + assert.deepEqual(cs?.byRef, ccrA.byRef, "must read the canonical .content-store.json, not a *.fb.content-store.json"); + } finally { + teardown(store, root); + } +});