From 94ae15f18dd5667972d3f2d0f541838763d12c3e Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 22:00:16 +0800 Subject: [PATCH 1/2] perf: avoid repeated fold closure scans within a batch --- scripts/bench-fold-registration.ts | 54 +++++++++ src/cache-ledger.ts | 11 +- tests/cache-fold-registration.test.ts | 160 ++++++++++++++++++++++++++ 3 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 scripts/bench-fold-registration.ts create mode 100644 tests/cache-fold-registration.test.ts diff --git a/scripts/bench-fold-registration.ts b/scripts/bench-fold-registration.ts new file mode 100644 index 0000000000..162c12a09d --- /dev/null +++ b/scripts/bench-fold-registration.ts @@ -0,0 +1,54 @@ +// Run from the repository root with the test environment/storage preloads and +// `node --import tsx scripts/bench-fold-registration.ts`. Compare independent +// processes on the base and patched revisions, alternating their order. Only +// recordCacheFoldsFromBlocks is timed; fixture construction and complete +// ledger/report checksums are outside that interval. +import { createHash } from "node:crypto"; +import { mock } from "node:test"; +import type { CompressionBlock } from "acp-kernel"; +import { buildSessionCacheReport, getCacheLedger, recordCacheFoldsFromBlocks } from "../src/cache-ledger.ts"; +import { createSession } from "../src/session.ts"; + +const T0 = Date.parse("2026-11-01T00:00:00Z"); +// Keep the report's generatedAt field comparable without omitting any field. +mock.method(Date, "now", () => T0); + +function block(id: number): CompressionBlock { + return { + blockId: `b${id}`, runId: "fold-benchmark", tier: 1, + summary: `retained summary ${id}`, startRef: "m00001", endRef: "m00001", + directMessageIds: ["raw-1"], effectiveMessageIds: ["raw-1"], directBlockIds: [], + compressedTokens: 1_000 + id, createdAt: T0 + id, survivedCount: 0, + generation: "young", active: true, + }; +} + +function checksum(value: unknown): string { + return createHash("sha256").update(JSON.stringify(value)).digest("hex"); +} + +const cases = [["cold-batch", 0, 6_000], ["long-history-batch", 6_000, 6_000], ["one-fold", 6_000, 1], ["short-batch", 0, 3]] as const; +function runCase([name, prior, added]: typeof cases[number]) { + const session = createSession(`bench-${name}`); + session.state.messageRefs.byRef = { m00001: "raw-1" }; + session.state.messageRefs.byRaw = { "raw-1": "m00001" }; + session.state.tokenSnapshot = { m00001: 10_000 }; + const existing = Array.from({ length: prior }, (_, i) => block(i + 1)); + getCacheLedger(session); + recordCacheFoldsFromBlocks(session, existing, { V: 50_000, Vp: 40_000 }); + session.state.blocks = existing; + const incoming = Array.from({ length: added }, (_, i) => block(prior + i + 1)); + session.state.blocks = existing.concat(incoming); + const start = performance.now(); + recordCacheFoldsFromBlocks(session, incoming, { V: 50_000, Vp: 40_000 }); + const elapsedMs = performance.now() - start; + const ledger = getCacheLedger(session); + return { + name, prior, added, elapsedMs, + folds: ledger.folds.length, lastBlockId: ledger.lastBlockId, + ledgerSha256: checksum(ledger), reportSha256: checksum(buildSessionCacheReport(session)), + }; +} +cases.forEach(runCase); // One untimed warmup per scenario in each fresh process. +const results = cases.map(runCase); +console.log(JSON.stringify({ node: process.version, platform: process.platform, arch: process.arch, results })); diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 9d6acbb3be..1e22572fe2 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1264,12 +1264,13 @@ export function getCacheLedger(session: Session): CacheLedger { return led; } -function pushFold(led: CacheLedger, f: Omit): void { +function pushFold(led: CacheLedger, f: Omit, closeFrom: number): void { // Freeze k = samples since each open fold (#1286): samples arrive in // order, so at this instant the count equals the batch-path // turnsToNextFold window (f.at < s.at <= nextFold.at); requestsAfter // keeps growing afterward to the full post-fold total. - for (const open of led.folds) { + for (let i = closeFrom; i < led.folds.length; i++) { + const open = led.folds[i]; if (open.k === null) open.k = open.requestsAfter; } led.foldSeqCounter += 1; @@ -1282,6 +1283,7 @@ function pushFold(led: CacheLedger, f: Omit = {}): CompressionBlock { + return { + blockId: `b${id}`, runId: "fold-registration", tier: 1, summary: `summary ${id}`, + directMessageIds: [], effectiveMessageIds: [], directBlockIds: [], + compressedTokens: id * 100, createdAt: T0 + id, survivedCount: 0, + generation: "young", active: true, ...overrides, + }; +} + +function countFoldReads(session: Session): () => number { + const ledger = getCacheLedger(session); + let reads = 0; + ledger.folds = new Proxy(ledger.folds, { + get(target, key, receiver) { + if (typeof key === "string" && /^(0|[1-9][0-9]*)$/.test(key)) reads++; + return Reflect.get(target, key, receiver); + }, + }); + return () => reads; +} + +for (const prior of [0, 500]) { + test(`a batch visits fold records linearly with ${prior} existing folds`, (t) => { + const session = createSession(`fold-work-${prior}`); + const count = 600; + recordCacheFoldsFromBlocks(session, Array.from({ length: prior }, (_, i) => block(i + 1))); + const reads = countFoldReads(session); + recordCacheFoldsFromBlocks(session, Array.from({ length: count }, (_, i) => block(prior + i + 1))); + const observed = reads(); + t.diagnostic(`${observed} fold reads for ${prior} existing + ${count} new records`); + assert.ok(observed <= 3 * (prior + count), `${observed} fold reads for ${prior} existing + ${count} new records`); + const ledger = getCacheLedger(session); + assert.equal(ledger.folds.length, prior + count); + assert.equal(ledger.foldSeqCounter, prior + count); + assert.equal(ledger.folds.at(-1)?.k, null); + assert.ok(ledger.folds.slice(0, -1).every((fold) => fold.k === 0)); + }); +} + +test("an empty call does not create a ledger or freeze an existing open fold", () => { + const session = createSession("empty-fold-batch"); + recordCacheFoldsFromBlocks(session, []); + assert.equal(session.metadata.cacheLedger, undefined); + recordCacheFoldsFromBlocks(session, [block(1)]); + const ledger = getCacheLedger(session); + ledger.folds[0].requestsAfter = 2.5; + const before = structuredClone(ledger); + recordCacheFoldsFromBlocks(session, []); + assert.deepEqual(ledger, before); +}); + +test("skipped blocks leave open folds and the block high-water mark untouched", () => { + const session = createSession("old-fold-batch"); + recordCacheFoldsFromBlocks(session, [block(10)]); + const ledger = getCacheLedger(session); + ledger.folds[0].requestsAfter = 4; + const before = structuredClone(ledger); + const ignored = block(9); + Object.defineProperty(ignored, "summary", { get: () => { throw new Error("old block summary must not be read"); } }); + recordCacheFoldsFromBlocks(session, [ignored, block(10), block(0)]); + assert.deepEqual(ledger, before); +}); + +test("batch records preserve geometry, token rounding, timestamps, and insertion order", (t) => { + const session = createSession("fold-values"); + session.state.messageRefs.byRef = { m00001: "raw-1", m00002: "raw-2", m00003: "raw-3" }; + session.state.tokenSnapshot = { m00001: 100, m00002: 200, m00003: 300 }; + t.mock.method(Date, "now", () => T0 + 500); + recordCacheFoldsFromBlocks(session, [ + block(2, { createdAt: 0, summary: "a😀bc", startRef: "m00003" }), + block(4, { summary: "", startRef: "m00002" }), + ], { V: 600, Vp: 100 }); + assert.deepEqual(getCacheLedger(session).folds, [ + { at: T0 + 500, S: 200, sigma: 2, X: 300, V: 600, Vp: 100, bid: "b2", seq: 1, T: 0, hPct: null, requestsAfter: 0, k: 0 }, + { at: T0 + 4, S: 400, sigma: 0, X: 100, V: 600, Vp: 100, bid: "b4", seq: 2, T: 0, hPct: null, requestsAfter: 0, k: null }, + ]); +}); + +test("the first new fold closes every legacy open record without replacing objects", () => { + const session = createSession("legacy-open-folds"); + recordCacheFoldsFromBlocks(session, [block(1), block(2), block(3)]); + const ledger = getCacheLedger(session); + const folds = ledger.folds; + const originals = [...folds]; + folds[0].k = null; + folds[0].requestsAfter = 2.5; + folds[1].k = 7; + folds[1].requestsAfter = 9; + folds[2].requestsAfter = 0.25; + recordCacheFoldsFromBlocks(session, [block(4), block(5)]); + assert.equal(ledger.folds, folds); + originals.forEach((fold, i) => assert.equal(folds[i], fold)); + assert.deepEqual(folds.map((fold) => fold.k), [2.5, 7, 0.25, 0, null]); + assert.deepEqual(folds.map((fold) => fold.requestsAfter), [2.5, 9, 0.25, 0, 0]); +}); + +test("duplicates and out-of-order block IDs keep the existing high-water behavior", () => { + const session = createSession("mixed-fold-ids"); + recordCacheFoldsFromBlocks(session, [block(1), block(5), block(3), block(5), block(7), block(0)]); + const ledger = getCacheLedger(session); + assert.equal(ledger.lastBlockId, 7); + assert.equal(ledger.foldSeqCounter, 3); + assert.deepEqual(ledger.folds.map((fold) => [fold.bid, fold.seq, fold.k]), [["b1", 1, 0], ["b5", 2, 0], ["b7", 3, null]]); +}); + +test("batches and individual calls produce identical complete ledgers and reports across usage samples", () => { + const batched = createSession("fold-comparison"); + const separate = createSession("fold-comparison"); + for (const session of [batched, separate]) recordCacheSample(session, { at: T0, input: 10_000, cached: 9_000 }); + for (const ids of [[1, 2, 3], [4, 5]]) { + const blocks = ids.map((id) => block(id)); + for (const session of [batched, separate]) session.state.blocks.push(...blocks); + recordCacheFoldsFromBlocks(batched, blocks, { V: 10_000, Vp: 8_000 }); + for (const item of blocks) recordCacheFoldsFromBlocks(separate, [item], { V: 10_000, Vp: 8_000 }); + for (const session of [batched, separate]) { + recordCacheSample(session, { at: T0 + ids.at(-1)! + 0.1, input: 8_000, cached: 7_000 }); + recordCacheSample(session, { at: T0 + ids.at(-1)! + 0.2, input: 8_500, cached: 8_000 }); + } + } + assert.deepEqual(getCacheLedger(batched), getCacheLedger(separate)); + assert.deepEqual(buildSessionCacheReport(batched), buildSessionCacheReport(separate)); + assert.deepEqual(getCacheLedger(batched).folds.map((fold) => fold.k), [0, 0, 2, 0, null]); +}); + +test("an interrupted batch leaves its last successful fold open and retries without duplication", () => { + const session = createSession("interrupted-fold-batch"); + recordCacheFoldsFromBlocks(session, [block(1)]); + const ledger = getCacheLedger(session); + ledger.folds[0].requestsAfter = 3; + const failure = new Error("summary unavailable"); + const bad = block(3); + Object.defineProperty(bad, "summary", { get: () => { throw failure; } }); + assert.throws(() => recordCacheFoldsFromBlocks(session, [block(2), bad, block(4)]), (error) => error === failure); + assert.equal(ledger.lastBlockId, 2); + assert.equal(ledger.foldSeqCounter, 2); + assert.deepEqual(ledger.folds.map((fold) => fold.k), [3, null]); + recordCacheFoldsFromBlocks(session, [block(2), block(3), block(4)]); + assert.equal(ledger.lastBlockId, 4); + assert.equal(ledger.foldSeqCounter, 4); + assert.deepEqual(ledger.folds.map((fold) => fold.k), [3, 0, 0, null]); +}); + +test("the lazy plugin sample path keeps all newly discovered fold records", () => { + const session = createSession("lazy-fold-batch"); + recordCacheSample(session, { at: T0, input: 10_000, cached: 9_000 }); + session.state.blocks.push(block(1), block(2), block(3)); + recordCacheSample(session, { at: T0 + 10, input: 8_000, cached: 7_000 }); + const ledger = getCacheLedger(session); + assert.deepEqual(ledger.folds.map((fold) => [fold.bid, fold.k, fold.requestsAfter]), [["b1", 0, 1], ["b2", 0, 1], ["b3", null, 1]]); + assert.equal(ledger.lastBlockId, 3); + assert.equal(buildSessionCacheReport(session).totals.balanced, true); +}); From 3664e6ef06ac3d023e2962d6bfd19986f6f14ed2 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 23:59:27 +0800 Subject: [PATCH 2/2] test: fix the fold report comparison clock --- tests/cache-fold-registration.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/cache-fold-registration.test.ts b/tests/cache-fold-registration.test.ts index 67c07555e2..51a6d5e079 100644 --- a/tests/cache-fold-registration.test.ts +++ b/tests/cache-fold-registration.test.ts @@ -111,7 +111,8 @@ test("duplicates and out-of-order block IDs keep the existing high-water behavio assert.deepEqual(ledger.folds.map((fold) => [fold.bid, fold.seq, fold.k]), [["b1", 1, 0], ["b5", 2, 0], ["b7", 3, null]]); }); -test("batches and individual calls produce identical complete ledgers and reports across usage samples", () => { +test("batches and individual calls produce identical complete ledgers and reports across usage samples", (t) => { + t.mock.method(Date, "now", () => T0 + 10_000); const batched = createSession("fold-comparison"); const separate = createSession("fold-comparison"); for (const session of [batched, separate]) recordCacheSample(session, { at: T0, input: 10_000, cached: 9_000 });