From bf5592c9083323030326faf9a539307489129c9e Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 18:56:37 +0800 Subject: [PATCH] perf: rewrite fold coverage without repeated scans --- src/fold-reconcile.ts | 36 +++++++---- tests/fold-rewrite-work.test.ts | 110 ++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 tests/fold-rewrite-work.test.ts diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index 880be7e944..d33f49bbe3 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1640,29 +1640,37 @@ function rewriteBlocks(blocks: BlockLike[], claims: Map): bool for (const block of blocks) { const lists = [block.effectiveMessageIds, block.directMessageIds] as (string[] | undefined)[]; for (const list of lists) { - if (list === undefined) continue; - for (let i = 0; i < list.length; i++) { - const replacement = claims.get(list[i]); - if (replacement === undefined || replacement.length === 0) continue; + if (list === undefined || !list.some((id) => (claims.get(id)?.length ?? 0) > 0)) continue; + // Counts include unread originals and already emitted replacements. + const present = new Map(); + for (const id of list) present.set(id, (present.get(id) ?? 0) + 1); + const rewritten: string[] = []; + for (const id of list) { + const replacement = claims.get(id); + if (replacement === undefined || replacement.length === 0) { + rewritten.push(id); + continue; + } // Claims map 1→N (a merged core stands in for a reasoning+text // pair) and N→1 (both pair members claim one merged witness). - // Splice the targets in and drop any already present — a + // Write the targets in order and drop any already present — a // block must never list the same message id twice. When the // replacement is already in the list (the second member of an // N→1 collapse), the old id is removed, not kept: two covered // ids collapsing onto one live id is exactly the model-switch // pair→merged shape (#2636). - const keep = replacement.filter((t) => !list.includes(t)); - if (keep.length === 0) { - list.splice(i, 1); - i--; - touched = true; - continue; + const keep = replacement.filter((target) => !present.has(target)); + const remaining = present.get(id)! - 1; + if (remaining === 0) present.delete(id); + else present.set(id, remaining); + for (const target of keep) { + rewritten.push(target); + present.set(target, (present.get(target) ?? 0) + 1); } - list.splice(i, 1, ...keep); - i += keep.length - 1; - touched = true; } + list.length = rewritten.length; + for (let i = 0; i < rewritten.length; i++) list[i] = rewritten[i]; + touched = true; } } return touched; diff --git a/tests/fold-rewrite-work.test.ts b/tests/fold-rewrite-work.test.ts new file mode 100644 index 0000000000..f3f32869bd --- /dev/null +++ b/tests/fold-rewrite-work.test.ts @@ -0,0 +1,110 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { reconcileFoldCoverage } from "../src/fold-reconcile.ts"; +import type { Session } from "../src/session.ts"; + +const options = { mode: "repair" as const, sessionId: "rewrite-work", log: () => {} }; +const message = (id: string, role: "user" | "assistant", text: string, contentType: "text" | "reasoning" = "text"): CoreMessage => ({ id, role, text, contentType }); + +function fixture(ids: string[], shared = false) { + let work = 0; + const tracked = () => new Proxy([...ids], { + get(target, key, receiver) { + if (typeof key === "string" && /^(0|[1-9]\d*)$/.test(key)) work++; + return Reflect.get(target, key, receiver); + }, + set(target, key, value, receiver) { + if (typeof key === "string" && /^(0|[1-9]\d*)$/.test(key)) work++; + return Reflect.set(target, key, value, receiver); + }, + }); + const effectiveMessageIds = tracked(); + const directMessageIds = shared ? effectiveMessageIds : tracked(); + const block = { blockId: "covered", active: true, effectiveMessageIds, directMessageIds }; + const session = { id: "rewrite-work", state: { blocks: [block] }, metadata: {} } as unknown as Session; + return { session, block, reset: () => { work = 0; }, work: () => work }; +} + +function repair(originals: CoreMessage[], incoming: CoreMessage[], ids: string[], expected: string[], shared = false) { + const f = fixture(ids, shared); + reconcileFoldCoverage(f.session, originals, options); + const effective = f.block.effectiveMessageIds; + const direct = f.block.directMessageIds; + f.reset(); + const result = reconcileFoldCoverage(f.session, incoming, options); + const work = f.work(); + assert.equal(result.kind, "reanchored"); + assert.equal(result.unmatched, 0); + assert.equal(f.block.effectiveMessageIds, effective); + assert.equal(f.block.directMessageIds, direct); + assert.deepEqual([...effective], expected); + assert.deepEqual([...direct], expected); + assert.ok(work < 80 * (ids.length + expected.length), `${work} coverage-array reads/writes for ${ids.length} old and ${expected.length} new ids`); +} + +test("reconciles a full remint without repeatedly scanning covered ids", () => { + const originals = Array.from({ length: 1_600 }, (_, index) => message(`old-${index}`, "user", `stable instruction number ${index}`)); + const incoming = originals.map((item, index) => ({ ...item, id: `new-${index}` })); + repair(originals, incoming, originals.map((item) => item.id), incoming.map((item) => item.id)); +}); + +function turns(count: number) { + const split: CoreMessage[] = []; + const merged: CoreMessage[] = []; + const pairIds: string[] = []; + const mergedIds: string[] = []; + for (let index = 0; index < count; index++) { + const user = message(`u-${index}`, "user", `question ${index}`); + const reasoning = message(`r-${index}`, "assistant", `thinking about question ${index}`, "reasoning"); + const text = message(`t-${index}`, "assistant", `answer to question ${index}`); + const whole = message(`m-${index}`, "assistant", `${reasoning.text}\n${text.text}`); + split.push(user, reasoning, text); + merged.push(user, whole); + pairIds.push(reasoning.id, text.id); + mergedIds.push(whole.id); + } + return { split, merged, pairIds, mergedIds }; +} + +test("collapses reasoning/text pairs without repeated tail shifts and keeps shared coverage arrays", () => { + const { split, merged, pairIds, mergedIds } = turns(600); + repair(split, merged, pairIds, mergedIds, true); +}); + +test("expands merged assistant turns without repeatedly shifting the remaining ids", () => { + const { split, merged, pairIds, mergedIds } = turns(600); + repair(merged, split, mergedIds, pairIds); +}); + +test("warn mode diagnoses remints without changing either coverage list", () => { + const originals = Array.from({ length: 12 }, (_, index) => message(`old-${index}`, "user", `instruction ${index}`)); + const ids = originals.map((item) => item.id); + const f = fixture(ids); + reconcileFoldCoverage(f.session, originals, options); + const result = reconcileFoldCoverage(f.session, originals.map((item, index) => ({ ...item, id: `new-${index}` })), { ...options, mode: "warn" }); + assert.equal(result.kind, "unmatched"); + assert.equal(result.claims, originals.length); + assert.deepEqual([...f.block.effectiveMessageIds], ids); + assert.deepEqual([...f.block.directMessageIds], ids); +}); + +test("unchanged resends retain coverage values and array identity", () => { + const originals = Array.from({ length: 12 }, (_, index) => message(`stable-${index}`, "user", `instruction ${index}`)); + const ids = originals.map((item) => item.id); + const f = fixture(ids); + const effective = f.block.effectiveMessageIds; + const direct = f.block.directMessageIds; + reconcileFoldCoverage(f.session, originals, options); + assert.equal(reconcileFoldCoverage(f.session, originals, options).kind, "resend"); + assert.equal(f.block.effectiveMessageIds, effective); + assert.equal(f.block.directMessageIds, direct); + assert.deepEqual([...effective], ids); + assert.deepEqual([...direct], ids); +}); + +test("repairs selected ids in order and collapses repeated claims while keeping stable entries", () => { + const originals = Array.from({ length: 12 }, (_, index) => message(`old-${index}`, "user", `instruction ${index}`)); + const incoming = originals.map((item, index) => index < 2 ? { ...item, id: `new-${index}` } : item); + repair(originals, incoming, ["old-0", "old-1", "old-1", "old-3"], ["new-0", "new-1", "old-3"]); +});