diff --git a/src/persist.ts b/src/persist.ts index 2746ad49f4..40a872a7bb 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -3382,9 +3382,22 @@ export class SessionStore { } }; this.writerOffPending.set(session.id, run); - queueMicrotask(() => { - if (this.writerOffPending.get(session.id) === run) run(); - }); + // An index-only build is synchronous and costs seconds on a large + // session; running it from a microtask mid-request put that cost + // between the pipeline and the upstream forward on every turn. + // While a request holds the session, re-check on the debounce + // interval instead and land the coalesced build once the request + // has released it (the legacy JSON writer is likewise debounced). + // writeNow / flushSync / shutdown still subsume an armed run. + const fire = (): void => { + if (this.writerOffPending.get(session.id) !== run) return; + if (session.inFlight > 0) { + setTimeout(fire, Math.max(50, this.debounceMs)).unref?.(); + return; + } + run(); + }; + queueMicrotask(fire); return; } // #6793-review: the durability arm is only consulted by the writer-off @@ -3908,20 +3921,27 @@ export class SessionStore { // #3133: same subsume rule as writeNow. this.writerOffPending.delete(session.id); const proofRevision = session.revisionEpoch; // #4788 D2: capture BEFORE the sync build - let rec: PersistedSession | null = null; - try { - rec = this.guardedBuild(session)(); - } catch (err) { - // Same never-throw flush contract as the kernel-store branch - // below: evictOldest (src/session.ts) calls this without an - // error boundary and relies on false to keep the session - // resident — a throw would escape the sync eviction path. - this.log("error", `[persist] writerOff flushSync build failed for ${session.id}: ${String(err)}`); - return false; + // The index copy already covers this exact revision (the same + // disk proof the idle spill trusts): rebuilding and re-ingesting + // an unchanged session would only stall the event loop, which on + // eviction of a large session costs tens of seconds. + const covered = this.durableRevision.get(session.id) === (proofRevision ?? 0); + if (!covered) { + let rec: PersistedSession | null = null; + try { + rec = this.guardedBuild(session)(); + } catch (err) { + // Same never-throw flush contract as the kernel-store branch + // below: evictOldest (src/session.ts) calls this without an + // error boundary and relies on false to keep the session + // resident — a throw would escape the sync eviction path. + this.log("error", `[persist] writerOff flushSync build failed for ${session.id}: ${String(err)}`); + return false; + } + this.maybeCutForkBase(session); + if (rec) this.noteMetaFp(session.id, rec.metadata ?? {}); // #5506: index-only flush landed + this.noteDurable(session.id, proofRevision); // #4788 D2: index-only flush landed } - this.maybeCutForkBase(session); - if (rec) this.noteMetaFp(session.id, rec.metadata ?? {}); // #5506: index-only flush landed - this.noteDurable(session.id, proofRevision); // #4788 D2: index-only flush landed this.lastSaveLanded.set(session.id, true); this.foldResidentTail(session); this.dropResidentBlockContents(session); diff --git a/tests/storage-writer-off-clean-evict.test.ts b/tests/storage-writer-off-clean-evict.test.ts new file mode 100644 index 0000000000..aba98233be --- /dev/null +++ b/tests/storage-writer-off-clean-evict.test.ts @@ -0,0 +1,68 @@ +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 { SessionStore } from "../src/persist.ts"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; + +process.env.BILI_PERSIST_ZSTD = "0"; +const hermetic = fs.mkdtempSync(path.join(os.tmpdir(), "bili-jw-clean-hermetic-")); +process.env.BILI_CONFIG_FILE = path.join(hermetic, "no-config.json"); +process.env.BILI_STORAGE_UNIFIED = "full"; +process.env.BILI_STORAGE_JSON_WRITER = "off"; + +const CORPUS_ROOT = path.resolve(import.meta.dirname, "golden/storage-corpus"); +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); + +function rmBestEffort(p: string): void { + try { + fs.rmSync(p, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); + } catch { + /* leaked temp dir is harmless */ + } +} + +let records = 0; +const originalRecord = UnifiedShadow.prototype.record; +UnifiedShadow.prototype.record = function (this: UnifiedShadow, ...args: Parameters): void { + records++; + originalRecord.apply(this, args); +}; + +test("jsonWriter=off: flushSync of a session whose index copy already covers its revision does not rebuild it", async () => { + const seed = fs.mkdtempSync(path.join(os.tmpdir(), "bili-jw-clean-seed-")); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-jw-clean-")); + try { + fs.cpSync(path.join(CORPUS_ROOT, "folded"), path.join(seed, "folded"), { recursive: true }); + const loader = new SessionStore({ dir: path.join(seed, "folded"), debounceMs: 1, enabled: true }); + const session = loader.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + loader.cancelAll(); + loader.closeQuietly(); + assert.ok(session); + + const store = new SessionStore({ dir, debounceMs: 5, enabled: true }); + try { + session.inFlight = 0; + session.revisionEpoch = (session.revisionEpoch ?? 0) + 1; + assert.equal(store.flushSync(session), true, "first flush builds the index copy"); + const first = records; + assert.ok(first > 0, "the first flush must record the session"); + + await sleep(30); + assert.equal(store.flushSync(session), true, "a clean flush still reports success (eviction may proceed)"); + assert.equal(records, first, "an unchanged session must not be rebuilt and re-ingested"); + + await sleep(30); + session.revisionEpoch = (session.revisionEpoch ?? 0) + 1; + assert.equal(store.flushSync(session), true); + assert.ok(records > first, "a newer revision must be written"); + } finally { + store.cancelAll(); + store.closeQuietly(); + } + } finally { + rmBestEffort(dir); + rmBestEffort(seed); + } +}); diff --git a/tests/storage-writer-off-in-flight.test.ts b/tests/storage-writer-off-in-flight.test.ts new file mode 100644 index 0000000000..c32f322026 --- /dev/null +++ b/tests/storage-writer-off-in-flight.test.ts @@ -0,0 +1,94 @@ +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 { SessionStore } from "../src/persist.ts"; +import { UnifiedStore } from "../src/storage/store.ts"; + +process.env.BILI_PERSIST_ZSTD = "0"; +const hermetic = fs.mkdtempSync(path.join(os.tmpdir(), "bili-jw-inflight-hermetic-")); +process.env.BILI_CONFIG_FILE = path.join(hermetic, "no-config.json"); +process.env.BILI_STORAGE_UNIFIED = "full"; +process.env.BILI_STORAGE_JSON_WRITER = "off"; + +const CORPUS_ROOT = path.resolve(import.meta.dirname, "golden/storage-corpus"); +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); + +function rmBestEffort(p: string): void { + try { + fs.rmSync(p, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); + } catch { + /* leaked temp dir is harmless */ + } +} + +function hasRow(dir: string, id: string): boolean { + const db = UnifiedStore.open(path.join(dir, "index.db")); + try { + return db.getSessionRow(id) !== undefined && db.getSessionRow(id) !== null; + } finally { + db.close(); + } +} + +test("jsonWriter=off: a save armed during a request lands after the request releases the session, not mid-request", async () => { + const seed = fs.mkdtempSync(path.join(os.tmpdir(), "bili-jw-inflight-seed-")); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-jw-inflight-")); + try { + fs.cpSync(path.join(CORPUS_ROOT, "folded"), path.join(seed, "folded"), { recursive: true }); + const loader = new SessionStore({ dir: path.join(seed, "folded"), debounceMs: 1, enabled: true }); + const session = loader.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + loader.cancelAll(); + loader.closeQuietly(); + assert.ok(session); + + const store = new SessionStore({ dir, debounceMs: 20, enabled: true }); + try { + session.inFlight = 1; + store.scheduleSave(session); + await sleep(120); + assert.equal(store.hasPending(session.id), true, "the build must stay armed while the request holds the session"); + assert.equal(fs.existsSync(path.join(dir, "index.db")) && hasRow(dir, session.id), false, "no index build may run mid-request"); + + session.inFlight = 0; + await sleep(200); + assert.equal(store.hasPending(session.id), false, "the armed build must land once the request released the session"); + assert.equal(hasRow(dir, session.id), true, "the session row must be in the index after release"); + assert.equal(await store.afterNextFlush(session.id), true); + } finally { + store.cancelAll(); + store.closeQuietly(); + } + } finally { + rmBestEffort(dir); + rmBestEffort(seed); + } +}); + +test("jsonWriter=off: an idle session still saves right away", async () => { + const seed = fs.mkdtempSync(path.join(os.tmpdir(), "bili-jw-idle-seed-")); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-jw-idle-")); + try { + fs.cpSync(path.join(CORPUS_ROOT, "folded"), path.join(seed, "folded"), { recursive: true }); + const loader = new SessionStore({ dir: path.join(seed, "folded"), debounceMs: 1, enabled: true }); + const session = loader.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); + loader.cancelAll(); + loader.closeQuietly(); + assert.ok(session); + const store = new SessionStore({ dir, debounceMs: 5000, enabled: true }); + try { + session.inFlight = 0; + store.scheduleSave(session); + await sleep(50); + assert.equal(store.hasPending(session.id), false); + assert.equal(hasRow(dir, session.id), true); + } finally { + store.cancelAll(); + store.closeQuietly(); + } + } finally { + rmBestEffort(dir); + rmBestEffort(seed); + } +});