From 3be4ed07567a5bf901f1563bb86a0f14fe4f1552 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 13:56:53 +0800 Subject: [PATCH 01/17] perf: bound the initial context profile sample --- src/context-analysis.ts | 14 ++++++++++--- tests/context-analysis.test.ts | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/context-analysis.ts b/src/context-analysis.ts index 4da34f5827..74a61e09d4 100644 --- a/src/context-analysis.ts +++ b/src/context-analysis.ts @@ -155,15 +155,23 @@ function snapshotInit(messages: CoreMessage[]): InitProfile { totalChars += chars; const tok = countMessageTokens(m, defaultCountTokens); estTokens += tok; - entries.push({ role: m.role, contentType: m.contentType, toolName: m.toolName, estTokens: tok }); + if (entries.length === INIT_PER_MSG_CAP && tok <= entries[entries.length - 1]!.estTokens) continue; + let lo = 0; + let hi = entries.length; + while (lo < hi) { + const mid = (lo + hi) >>> 1; + if (tok > entries[mid]!.estTokens) hi = mid; + else lo = mid + 1; + } + entries.splice(lo, 0, { role: m.role, contentType: m.contentType, toolName: m.toolName, estTokens: tok }); + if (entries.length > INIT_PER_MSG_CAP) entries.pop(); } - entries.sort((x, y) => y.estTokens - x.estTokens); return { ts: Date.now(), messageCount: messages.length, totalChars, estTokens, - perMessage: entries.slice(0, INIT_PER_MSG_CAP), + perMessage: entries, }; } diff --git a/tests/context-analysis.test.ts b/tests/context-analysis.test.ts index 1a05503ef5..35ef1a2753 100644 --- a/tests/context-analysis.test.ts +++ b/tests/context-analysis.test.ts @@ -6,6 +6,7 @@ // log forensics. These tests pin the four dimensions + the caps. import assert from "node:assert"; import { test } from "node:test"; +import { countMessageTokens, defaultCountTokens } from "acp-kernel"; import { contextAnalysisView, @@ -73,6 +74,42 @@ test("first request snapshots the init profile (size + per-message sample)", () if (!lines.some((l) => l.startsWith("init:"))) throw new Error(`init line present: ${lines.join(" | ")}`); }); +test("init sample retains the largest 100 while totals cover the full request", () => { + resetContextAnalysisForTest(); + const messages = Array.from({ length: 513 }, (_, i) => ({ + ...msg(String(i), (i * 137) % 1200, i % 2 ? "assistant" : "user"), + toolName: `tool-${String(i).padStart(3, "0")}`, + thinkingTokens: i % 17 === 0 ? 1000 : undefined, + })); + const expected = messages.map((m) => ({ + role: m.role, + contentType: m.contentType, + toolName: m.toolName, + estTokens: countMessageTokens(m, defaultCountTokens), + })).sort((a, b) => b.estTokens - a.estTokens).slice(0, 100); + recordNudgeObservation("s-many", { nudge: null, messages }, 50_000, 750_000, false); + recordNudgeObservation("s-many", { nudge: null, messages: [msg("later", 4)] }, 52_000, 750_000, false); + const init = contextAnalysisView("s-many")!.init!; + assert.equal(init.messageCount, messages.length); + assert.equal(init.totalChars, messages.reduce((sum, m) => sum + m.text.length + m.toolName.length, 0)); + assert.equal(init.estTokens, messages.reduce((sum, m) => sum + countMessageTokens(m, defaultCountTokens), 0)); + assert.deepEqual(init.perMessage, expected); +}); + +test("init sample keeps earlier messages when equal token sizes cross the cap", () => { + resetContextAnalysisForTest(); + const messages = Array.from({ length: 250 }, (_, i) => ({ + ...msg(String(i), 100), + toolName: `tool-${String(i).padStart(3, "0")}`, + })); + recordNudgeObservation("s-ties", { nudge: null, messages }, 50_000, 750_000, false); + recordNudgeObservation("s-ties", { nudge: null }, 50_000, 750_000, false); + assert.deepEqual( + contextAnalysisView("s-ties")!.init!.perMessage.map((m) => m.toolName), + messages.slice(0, 100).map((m) => m.toolName), + ); +}); + test("heavy-init verdict fires when the first request already carries ≥15% of the window", () => { resetContextAnalysisForTest(); withClock((now, set) => { From 8637f13e4c385d0caf52284b86d31b3045e3f1ce Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 14:24:36 +0800 Subject: [PATCH 02/17] fix(affinity): preserve parallel tool rounds across wire formats --- src/affinity-neutral.ts | 22 +++----- tests/affinity-crosswire-fold.test.ts | 30 +++++++++- tests/affinity-neutral.test.ts | 79 +++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 16 deletions(-) diff --git a/src/affinity-neutral.ts b/src/affinity-neutral.ts index 0ca4a7af47..11d79f9b5f 100644 --- a/src/affinity-neutral.ts +++ b/src/affinity-neutral.ts @@ -239,7 +239,7 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] { const role = m.role === "model" ? "assistant" : typeof m.role === "string" ? m.role : "user"; const texts: string[] = []; const calls: string[] = []; - let toolBody: string | undefined; + const toolBodies: string[] = []; for (const part of m.parts) { if (!part || typeof part !== "object") continue; const p = part as { text?: unknown; functionCall?: unknown; functionResponse?: unknown }; @@ -253,11 +253,11 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] { // gemini clients wrap the tool body under an arbitrary key // ({output:…}/{result:…}/{content:…}); a single-string-valued // object IS the body — unwrap so it equals the openai render. - if (typeof resp === "string") toolBody = resp; + if (typeof resp === "string") toolBodies.push(resp); else if (resp && typeof resp === "object" && !Array.isArray(resp)) { const entries = Object.entries(resp as Record); - toolBody = entries.length === 1 && typeof entries[0]![1] === "string" ? entries[0]![1]! : canon(resp); - } else toolBody = canon(resp ?? p.functionResponse); + toolBodies.push(entries.length === 1 && typeof entries[0]![1] === "string" ? entries[0]![1]! : canon(resp)); + } else toolBodies.push(canon(resp ?? p.functionResponse)); } } // model turns MERGE into the pending assistant run — gemini splits @@ -273,8 +273,8 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] { continue; } flushAssistant(); - if (toolBody !== undefined) turns.push({ kind: "tool", text: toolBody, frags: [fragmentHash("tool", toolBody, [])] }); - if (calls.length > 0 && toolBody === undefined) turns.push({ kind: "assistant", calls, frags: [fragmentHash("assistant", "", calls)] }); + for (const toolBody of toolBodies) turns.push({ kind: "tool", text: toolBody, frags: [fragmentHash("tool", toolBody, [])] }); + if (calls.length > 0 && toolBodies.length === 0) turns.push({ kind: "assistant", calls, frags: [fragmentHash("assistant", "", calls)] }); else if (texts.length > 0) pushTextTurn(role, texts.join(""), [fragmentHash(role, texts.join(""), [])]); continue; } @@ -285,13 +285,9 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] { const call = `${m.name}\u0000${canonicalCallArgs(m.arguments)}`; // codex splits assistant commentary and the tool call across items — // merge into the pending run so it reduces like the openai render. - if (asst !== null) { - asst.calls = [...(asst.calls ?? []), call]; - asstFrags.push(fragmentHash("assistant", "", [call])); - continue; - } - flushAssistant(); - turns.push({ kind: "assistant", calls: [call], frags: [fragmentHash("assistant", "", [call])] }); + asst = asst ?? { kind: "assistant", text: "", calls: [] }; + asst.calls = [...(asst.calls ?? []), call]; + asstFrags.push(fragmentHash("assistant", "", [call])); continue; } if (typeof m.type === "string" && (m.type === "function_call_output" || m.type === "local_shell_call_output")) { diff --git a/tests/affinity-crosswire-fold.test.ts b/tests/affinity-crosswire-fold.test.ts index 71aa1e4993..3a7a8b67b6 100644 --- a/tests/affinity-crosswire-fold.test.ts +++ b/tests/affinity-crosswire-fold.test.ts @@ -280,11 +280,33 @@ test("cross-wire switch onto the responses and google lanes keeps the session AN type Msg = { role: string; content: string }; const hist: Msg[] = []; + const parallelTools = [ + { id: "parallel_a", name: "read_file", args: { path: "/a.ts" }, output: "parallel result alpha" }, + { id: "parallel_b", name: "read_file", args: { path: "/b.ts" }, output: "parallel result beta" }, + ]; + const question = "Read both source files before the next audit turn."; + const withParallel = (messages: unknown[], round: unknown[]): unknown[] => messages.length > 20 + ? [...messages.slice(0, 20), ...round, ...messages.slice(20)] : messages; + const openaiRound = [ + { role: "user", content: question }, + { role: "assistant", content: null, tool_calls: parallelTools.map((t) => ({ id: t.id, type: "function", function: { name: t.name, arguments: JSON.stringify(t.args) } })) }, + ...parallelTools.map((t) => ({ role: "tool", tool_call_id: t.id, content: t.output })), + ]; + const responsesRound = [ + { type: "message", role: "user", content: [{ type: "input_text", text: question }] }, + ...parallelTools.map((t) => ({ type: "function_call", call_id: t.id, name: t.name, arguments: JSON.stringify(t.args) })), + ...parallelTools.map((t) => ({ type: "function_call_output", call_id: t.id, output: t.output })), + ]; + const googleRound = [ + { role: "user", parts: [{ text: question }] }, + { role: "model", parts: parallelTools.map((t) => ({ functionCall: { name: t.name, args: t.args } })) }, + { role: "user", parts: parallelTools.map((t) => ({ functionResponse: { name: t.name, response: { output: t.output } } })) }, + ]; try { // Phase A — openai lane establishes the anonymous session and folds. const openaiTurn = async (t: number): Promise => { hist.push({ role: "user", content: `Turn ${t}: audit province ${t}. ` + FILLER(t, 6) }); - const resp = await fetch(openaiBase, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "probe-model", stream: true, messages: [...hist] }) }); + const resp = await fetch(openaiBase, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "probe-model", stream: true, messages: withParallel(hist, openaiRound) }) }); assert.equal(resp.status, 200, `openai turn ${t}`); await resp.text(); hist.push({ role: "assistant", content: `Reply about province ${t}: acknowledged. ` + FILLER(t, 0.2) }); @@ -305,7 +327,7 @@ test("cross-wire switch onto the responses and google lanes keeps the session AN role: m.role, content: [{ type: m.role === "assistant" ? "output_text" : "input_text", text: m.content }], })); - const respB = await fetch(responsesBase, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "probe-model", stream: true, instructions: "crosswire probe", input: respItems }) }); + const respB = await fetch(responsesBase, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "probe-model", stream: true, instructions: "crosswire probe", input: withParallel(respItems, responsesRound) }) }); const respRaw = await respB.text(); if (respB.status !== 200) assert.fail(`responses switch turn: ${respB.status}: ${respRaw.slice(0, 200)}`); hist.push({ role: "assistant", content: "Responses reply about turn 13: acknowledged." }); @@ -314,6 +336,7 @@ test("cross-wire switch onto the responses and google lanes keeps the session AN let pfasB = prefixAffinity.trackedSessionIds().filter((id) => id.startsWith("pfa-")); assert.equal(pfasB.length, 1, `the responses switch must not fork: got ${pfasB.join(", ")}`); assert.equal(pfasB[0], sessionId); + for (const tool of parallelTools) assert.ok(responsesBody.includes(tool.output), "both tool results survive the switch"); assert.ok(responsesBody.includes(SUMMARY_TEXT), "the fold carrier is attached on the responses wire"); assert.equal((responsesBody.match(new RegExp(EARLY_FILLER_TAG, "g")) ?? []).length, 0, "covered early content must not replay unfolded on the responses switch"); assert.equal(foldDemands, foldsBeforeSwitch, "no re-fold demanded on the responses switch"); @@ -323,7 +346,7 @@ test("cross-wire switch onto the responses and google lanes keeps the session AN // history re-rendered as contents[].parts[]. hist.push({ role: "user", content: "Turn 14: continue with province seven." }); const contents = hist.map((m) => ({ role: m.role === "assistant" ? "model" : "user", parts: [{ text: m.content }] })); - const respG = await fetch(googleBase, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "probe-model", contents }) }); + const respG = await fetch(googleBase, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "probe-model", contents: withParallel(contents, googleRound) }) }); const googleRaw = await respG.text(); if (respG.status !== 200) assert.fail(`google switch turn: ${respG.status}: ${googleRaw.slice(0, 200)}`); @@ -331,6 +354,7 @@ test("cross-wire switch onto the responses and google lanes keeps the session AN pfasB = prefixAffinity.trackedSessionIds().filter((id) => id.startsWith("pfa-")); assert.equal(pfasB.length, 1, `the google switch must not fork: got ${pfasB.join(", ")}`); assert.equal(pfasB[0], sessionId); + for (const tool of parallelTools) assert.ok(googleBody.includes(tool.output), "both tool results survive the switch"); assert.ok(googleBody.includes(SUMMARY_TEXT), "the fold carrier is attached on the google wire"); assert.equal((googleBody.match(new RegExp(EARLY_FILLER_TAG, "g")) ?? []).length, 0, "covered early content must not replay unfolded on the google switch"); assert.equal(foldDemands, foldsBeforeSwitch, "no re-fold demanded on the google switch"); diff --git a/tests/affinity-neutral.test.ts b/tests/affinity-neutral.test.ts index 5cb0aed3dd..4da58bf4ba 100644 --- a/tests/affinity-neutral.test.ts +++ b/tests/affinity-neutral.test.ts @@ -225,3 +225,82 @@ test("#2791: pre-upgrade (raw-hash) entries still match through the v1 fallback assert.equal(crossed.via, "prefix", "re-noted entry must now match across wires"); assert.equal(crossed.sessionId, "pfa-legacy000000001"); }); + +const parallelTools = [ + { id: "parallel_a", name: "read_file", args: { path: "/src/a.ts" }, output: "first file contents" }, + { id: "parallel_b", name: "read_file", args: { path: "/src/b.ts" }, output: "second file contents" }, +]; + +function parallelToolRenders(): Record>> { + const question = "Read both source files before comparing their exports."; + return { + openai: [ + { role: "user", content: question }, + { role: "assistant", content: null, tool_calls: parallelTools.map((t) => ({ id: t.id, type: "function", function: { name: t.name, arguments: JSON.stringify(t.args) } })) }, + ...parallelTools.map((t) => ({ role: "tool", tool_call_id: t.id, content: t.output })), + ], + anthropic: [ + { role: "user", content: [{ type: "text", text: question }] }, + { role: "assistant", content: parallelTools.map((t) => ({ type: "tool_use", id: t.id, name: t.name, input: t.args })) }, + { role: "user", content: parallelTools.map((t) => ({ type: "tool_result", tool_use_id: t.id, content: t.output })) }, + ], + responses: [ + { type: "message", role: "user", content: [{ type: "input_text", text: question }] }, + ...parallelTools.map((t) => ({ type: "function_call", call_id: t.id, name: t.name, arguments: JSON.stringify(t.args) })), + ...parallelTools.map((t) => ({ type: "function_call_output", call_id: t.id, output: t.output })), + ], + google: [ + { role: "user", parts: [{ text: question }] }, + { role: "model", parts: parallelTools.map((t) => ({ functionCall: { name: t.name, args: t.args } })) }, + { role: "user", parts: parallelTools.map((t) => ({ functionResponse: { name: t.name, response: { output: t.output } } })) }, + ], + }; +} + +for (const wire of ["anthropic", "responses", "google"]) { + test(`parallel tools: ${wire} preserves the complete neutral call and result sequence`, () => { + const renders = parallelToolRenders(); + const expected = neutralTurns(renders.openai!); + const actual = neutralTurns(renders[wire]!); + assert.deepEqual(actual.map(neutralTurnCanonical), expected.map(neutralTurnCanonical)); + assert.deepEqual(actual.filter((t) => t.kind === "tool").map((t) => t.text), parallelTools.map((t) => t.output)); + }); + + test(`parallel tools: switching from openai to ${wire} retains the anonymous session`, () => { + const renders = parallelToolRenders(); + const resolver = new PrefixAffinityResolver(); + const first = resolver.resolve(renders.openai!)!; + resolver.note(first.sessionId, first.incomingDepth, first.tailHash, first.itemHashes, false, first.sketches, first.userFlags); + const switched = resolver.resolve([...renders[wire]!, { role: "user", content: "Now compare the exports." }])!; + assert.equal(switched.via, "prefix"); + assert.equal(switched.sessionId, first.sessionId); + assert.equal(switched.incomingDepth, first.incomingDepth + 1); + }); +} + +test("parallel tools: Gemini retains every result body and trailing user text", () => { + const bodies = ["plain", { output: "wrapped" }, { count: 2, ok: true }, ["a", "b"], null]; + const actual = neutralTurns([{ role: "user", parts: [ + ...bodies.map((response) => ({ functionResponse: { name: "lookup", response } })), + { text: "Continue after all results." }, + ] }]); + assert.deepEqual(actual.map((t) => [t.kind, t.text]), [ + ["tool", "plain"], ["tool", "wrapped"], ["tool", '{"count":2,"ok":true}'], + ["tool", '["a","b"]'], ["tool", '{"name":"lookup","response":null}'], + ["user", "Continue after all results."], + ]); +}); + +test("parallel tools: Responses results separate consecutive call rounds", () => { + const turns = neutralTurns([ + { type: "function_call", name: "first", arguments: "{}" }, + { type: "function_call", name: "second", arguments: "{}" }, + { type: "function_call_output", output: "first result" }, + { type: "function_call_output", output: "second result" }, + { type: "function_call", name: "third", arguments: "{}" }, + ]); + assert.deepEqual(turns.map((t) => [t.kind, t.calls ?? t.text]), [ + ["assistant", ["first\u0000{}", "second\u0000{}"]], + ["tool", "first result"], ["tool", "second result"], ["assistant", ["third\u0000{}"]], + ]); +}); From 94ec3dda2e6c03845c6b0ab1e0f35241b65b7737 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 14:45:14 +0800 Subject: [PATCH 03/17] perf(storage): build bounded boot file indexes incrementally --- scripts/bench-bounded-boot-index.mjs | 72 ++++++++++++++++++++++++++++ src/persist.ts | 4 +- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 scripts/bench-bounded-boot-index.mjs diff --git a/scripts/bench-bounded-boot-index.mjs b/scripts/bench-bounded-boot-index.mjs new file mode 100644 index 0000000000..ced1662c0e --- /dev/null +++ b/scripts/bench-bounded-boot-index.mjs @@ -0,0 +1,72 @@ +// Run with: node --max-old-space-size=512 --expose-gc --import tsx scripts/bench-bounded-boot-index.mjs [files-per-root] +import assert from "node:assert/strict"; +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { performance } from "node:perf_hooks"; + +const count = Number(process.argv[2] ?? 10_000); +if (!Number.isSafeInteger(count) || count < 1) throw new Error("files-per-root must be a positive safe integer"); +const root = mkdtempSync(path.join(tmpdir(), "bili-boot-index-bench-")); +const dir = path.join(root, "sessions"); +let store; +try { + process.env.BILI_SESSIONS_DIR = dir; + process.env.BILI_STORAGE_UNIFIED = "full"; + process.env.BILI_STORAGE_BACKEND = "sqlite"; + process.env.BILI_PERSIST_ZSTD = "0"; + delete process.env.BILI_CONFIG_FILE; + delete process.env.BILI_ENCRYPTION_KEY; + for (const kind of ["DATA", "STATE", "CONFIG", "CACHE"]) { + process.env[`XDG_${kind}_HOME`] = path.join(root, kind.toLowerCase()); + mkdirSync(process.env[`XDG_${kind}_HOME`], { recursive: true }); + } + const { SessionStore, relPathFor } = await import("../src/persist.ts"); + for (const [kind, base] of [["hot", dir], ["cold", `${dir}-cold`]]) { + mkdirSync(path.join(base, "_unknown"), { recursive: true }); + for (let i = 0; i < count; i++) { + const id = `bench-${kind}-${i}`; + const payload = { + version: 3, savedAt: 1000 + i, id, meta: { label: id }, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} } }, + blockContents: { b1: { one: null, full: { text: `original-${id}`, count: 1 } } }, + stats: { requests: i + 1 }, createdAt: 1000 + i, + }; + writeFileSync(path.join(base, relPathFor(id)), JSON.stringify({ version: 3, savedAt: payload.savedAt, id, payload })); + } + writeFileSync(path.join(base, "_unknown", "ignored.content-store.json"), "{}"); + } + let indexed = 0; + store = new SessionStore({ dir, enabled: true, debounceMs: 0, log: (_level, message) => { + const match = /^\[persist\] indexed (\d+) state files;/.exec(message); + if (match) indexed = Number(match[1]); + } }); + assert.ok(store.replicasAvailable(), "benchmark requires the SQLite sidecar"); + global.gc?.(); + const start = performance.now(); + const loaded = await store.boot({ maxSessions: 0, maxBytes: 64 * 1024 * 1024 }); + const bootMs = performance.now() - start; + assert.equal(loaded.size, 0); + assert.equal(indexed, count * 2, "both roots must be indexed and content-store companions excluded"); + await store.backgroundScanComplete(); + for (const kind of ["hot", "cold"]) { + for (const i of new Set([0, count - 1])) { + const id = `bench-${kind}-${i}`; + const session = store.loadSync(id); + assert.equal(session?.stats.requests, i + 1); + assert.equal(session?.blockContents.get("b1")?.full?.text, `original-${id}`); + } + } + console.log(JSON.stringify({ + files_per_root: count, + indexed_files: indexed, + warm_sessions: loaded.size, + boot_ms: bootMs, + max_rss_kib: process.resourceUsage().maxRSS, + node: process.version, + })); +} finally { + await store?.backgroundScanComplete(); + store?.closeQuietly(); + rmSync(root, { recursive: true, force: true }); +} diff --git a/src/persist.ts b/src/persist.ts index 5f5c0e7c14..bd1f19fcf7 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -1025,10 +1025,12 @@ export class SessionStore { const filesByRoot = new Map(); for (const root of [this.sessionsDir, `${this.sessionsDir}-cold`]) { const files = existsSync(root) ? await walkJsonFiles(root) : []; + const rootFiles: string[] = []; + filesByRoot.set(root, rootFiles); for (const file of files) { if (path.basename(file).endsWith(".content-store.json")) continue; this.diskFiles.push(file); - filesByRoot.set(root, [...(filesByRoot.get(root) ?? []), file]); + rootFiles.push(file); } } // #3199: unified mode (shadow or full) — the listener must not wait From d67259279dd297036ea6ecdcb091435f6bbe70db Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 15:24:58 +0800 Subject: [PATCH 04/17] perf: deduplicate captured subagent sessions with a set --- scripts/bench-subagent-capture.mjs | 22 ++++++++++++++++++++++ src/subagent-sessions.ts | 8 ++++---- tests/subagent-sessions.test.ts | 14 ++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 scripts/bench-subagent-capture.mjs diff --git a/scripts/bench-subagent-capture.mjs b/scripts/bench-subagent-capture.mjs new file mode 100644 index 0000000000..1019d3bbdb --- /dev/null +++ b/scripts/bench-subagent-capture.mjs @@ -0,0 +1,22 @@ +import assert from "node:assert/strict"; +import { performance } from "node:perf_hooks"; +import { captureSubagentSessionIds, subagentSessionsOf, syncSubagentSessions } from "../src/subagent-sessions.ts"; + +const count = Number(process.argv[2] ?? 20_000); +if (!Number.isSafeInteger(count) || count < 1 || count > 100_000) throw new Error("Expected 1..100000 unique subagent sessions"); +const expected = Array.from({ length: count }, (_, i) => `ses_${String(i).padStart(8, "0")}`); +const messages = expected.flatMap((id, i) => [ + { id: `call-${i}`, contentType: "tool-call", toolName: "task", text: JSON.stringify({ sessionId: id }) }, + { id: `result-${i}`, contentType: "tool-result", toolName: "task", text: `\nresult` }, +]); +const changed = [{ blockId: "b1", effectiveMessageIds: messages.map(m => m.id), directBlockIds: [] }]; +const elapsedMs = []; +for (let run = 0; run < 3; run++) { + const session = { metadata: { pluginAgent: "opencode" }, state: { blocks: [{ blockId: "b1", active: true }] } }; + const started = performance.now(); + syncSubagentSessions(session, changed, messages); + elapsedMs.push(performance.now() - started); + assert.deepEqual(subagentSessionsOf(session).b1, expected.slice(0, 100)); +} +assert.deepEqual(captureSubagentSessionIds(messages, new Set(changed[0].effectiveMessageIds)), expected); +console.log(JSON.stringify({ node: process.version, uniqueSessions: count, messages: messages.length, retainedIds: Math.min(count, 100), elapsedMs }, null, 2)); diff --git a/src/subagent-sessions.ts b/src/subagent-sessions.ts index 70c51f29fa..ca62c1bf1d 100644 --- a/src/subagent-sessions.ts +++ b/src/subagent-sessions.ts @@ -57,14 +57,14 @@ export function subagentSessionsOf(session: SubagentSession | undefined): Record const TASK_ENVELOPE_RE = /^<(?:task id|subagent sessionID)="(ses_[A-Za-z0-9]+)"(?:\s[^>]*)?>/; export function captureSubagentSessionIds(messages: SubagentMessage[], covered: Set): string[] { - const out: string[] = []; + const ids = new Set(); for (const m of messages) { if (!covered.has(m.id)) continue; if (typeof m.toolName !== "string" || !SUBAGENT_TOOL_NAMES.has(m.toolName)) continue; if (m.contentType === "tool-result") { const first = (m.text ?? "").split("\n", 1)[0]?.trim() ?? ""; const id = first.match(TASK_ENVELOPE_RE)?.[1]; - if (id !== undefined && !out.includes(id)) out.push(id); + if (id !== undefined) ids.add(id); continue; } let parsed: unknown; @@ -77,10 +77,10 @@ export function captureSubagentSessionIds(messages: SubagentMessage[], covered: const obj = parsed as Record; for (const key of ["sessionId", "sessionID", "session_id"]) { const v = obj[key]; - if (typeof v === "string" && SES_ID_RE.test(v) && !out.includes(v)) out.push(v); + if (typeof v === "string" && SES_ID_RE.test(v)) ids.add(v); } } - return out; + return [...ids]; } // Fold-commit hook. `changed` = new blocks plus refolds (summary replaced, diff --git a/tests/subagent-sessions.test.ts b/tests/subagent-sessions.test.ts index 4f12ddeecd..af2000cc40 100644 --- a/tests/subagent-sessions.test.ts +++ b/tests/subagent-sessions.test.ts @@ -147,6 +147,20 @@ test("sync is gated on opencode and prunes dead blocks; empty re-capture keeps t assert.equal(subagentSessionNote("b2", ["ses_old"]), "[acp-subagent-sessions b2: ses_old]"); }); +test("large dispatch histories preserve first-seen ID order across carriers and the persisted cap", () => { + const expected = Array.from({ length: 5_000 }, (_, i) => `ses_bulk${i}`); + const messages: CoreMessage[] = expected.flatMap((id, i) => [ + ...seedDispatch(`call-${i}`, `result-${i}`, id), + { id: `again-${i}`, role: "assistant", contentType: "tool-call", toolName: "subagent", text: JSON.stringify({ sessionId: id, sessionID: id, session_id: id }) }, + ]); + const covered = new Set(messages.map(message => message.id)); + assert.deepEqual(captureSubagentSessionIds(messages, covered), expected); + const session = { metadata: { pluginAgent: "opencode" }, state: { blocks: [{ blockId: "b1", active: true }] } }; + syncSubagentSessions(session, [{ blockId: "b1", effectiveMessageIds: [...covered], directBlockIds: [] }], messages); + assert.deepEqual(subagentSessionsOf(session), { b1: expected.slice(0, 100) }); + assert.deepEqual(captureSubagentSessionIds(messages, new Set(["again-4999", "result-1"])), ["ses_bulk1", "ses_bulk4999"]); +}); + test("attach appends the id line to the active carrier only, without mutating inputs", () => { const carrier = { id: summaryMessageId("b1"), role: "system", contentType: "text" as const, text: "SUMMARY-BODY" }; const other = { id: "raw_1", role: "user", contentType: "text" as const, text: "hello" }; From dadbcd35475b451cd2d8207c1f1cee23cecfbded Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 15:35:02 +0800 Subject: [PATCH 05/17] fix: parse buffered SSE fields without trimming boundaries --- src/sse-util.ts | 12 +++++----- tests/buffered-sse-fields.test.ts | 31 ++++++++++++++++++++++++++ tests/preflight-sse-extraction.test.ts | 6 +++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/buffered-sse-fields.test.ts diff --git a/src/sse-util.ts b/src/sse-util.ts index dcd7887230..0a1e9edc70 100644 --- a/src/sse-util.ts +++ b/src/sse-util.ts @@ -39,14 +39,16 @@ export function* bufferedSseEvents(text: string): Generator<{ event: string; dat let event = ""; let data: string[] = []; for (const line of finalizeSseLineEndings(text).split("\n")) { - if (line.trim() === "") { + if (line === "") { if (data.length) yield { event, data: data.join("\n") }; event = ""; data = []; - } else if (line.startsWith("event:")) { - event = line.slice(6).trim(); - } else if (line.startsWith("data:")) { - data.push(line.slice(5).replace(/^ /, "")); + } else { + const colon = line.indexOf(":"); + const field = colon < 0 ? line : line.slice(0, colon); + const value = colon < 0 ? "" : line.slice(colon + 1).replace(/^ /, ""); + if (field === "event") event = value; + else if (field === "data") data.push(value); } } if (data.length) yield { event, data: data.join("\n") }; diff --git a/tests/buffered-sse-fields.test.ts b/tests/buffered-sse-fields.test.ts new file mode 100644 index 0000000000..bbb04435a7 --- /dev/null +++ b/tests/buffered-sse-fields.test.ts @@ -0,0 +1,31 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { bufferedSseEvents } from "../src/sse-util.ts"; + +test("only an empty SSE line dispatches a buffered event", () => { + assert.deepEqual([...bufferedSseEvents("event: answer\ndata: first\n \n\t\ndata: second\n\n")], [ + { event: "answer", data: "first\nsecond" }, + ]); +}); + +test("a colon-free data field contributes an empty data line", () => { + assert.deepEqual([...bufferedSseEvents("data: first\ndata\ndata: last\n\n")], [ + { event: "", data: "first\n\nlast" }, + ]); + assert.deepEqual([...bufferedSseEvents("data\n\n")], [{ event: "", data: "" }]); +}); + +test("event fields remove only one optional space and can reset to empty", () => { + assert.deepEqual([...bufferedSseEvents("event: answer \ndata: value\n\n")], [ + { event: " answer ", data: "value" }, + ]); + assert.deepEqual([...bufferedSseEvents("event: answer\nevent\ndata: value\n\n")], [ + { event: "", data: "value" }, + ]); +}); + +test("unknown fields and comments do not reset fields, and real boundaries do", () => { + assert.deepEqual([...bufferedSseEvents("event: first\n: comment\nunknown\ndata: one\n\ndata: two\n\n")], [ + { event: "first", data: "one" }, { event: "", data: "two" }, + ]); +}); diff --git a/tests/preflight-sse-extraction.test.ts b/tests/preflight-sse-extraction.test.ts index a30ff9867c..c81330ea1e 100644 --- a/tests/preflight-sse-extraction.test.ts +++ b/tests/preflight-sse-extraction.test.ts @@ -31,6 +31,12 @@ for (const [protocol, body] of [ assert.equal(extractSummaryFromSse(protocol, multiline), "complete summary"); assert.equal(extractSummaryFromSse(protocol, multiline.replace(/\n/g, "\r\n")), "complete summary"); }); + test(`summary extraction ignores whitespace-only unknown SSE fields in ${protocol} events`, () => { + const multiline = body.replace(/^data: (.+)$/gm, (line, data: string) => data === "[DONE]" ? line : + JSON.stringify(JSON.parse(data), null, 2).split("\n").map((part) => `data: ${part}`).join("\n \n\t\n")); + assert.equal(extractSummaryFromSse(protocol, multiline), "complete summary"); + assert.equal(extractSummaryFromSse(protocol, multiline.replace(/\n/g, "\r\n")), "complete summary"); + }); } test("multiline SSE keeps frame-level event types and failure terminals", () => { From 99cf1088a27339c25fd9741648503a8a61a587a0 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 15:41:17 +0800 Subject: [PATCH 06/17] fix: filter long message histories without argument overflow --- src/agent/opencode-v2.ts | 12 ++++--- src/loop/core.ts | 6 +++- .../issue5220-failed-compress-receipt.test.ts | 31 +++++++++++++++++++ tests/opencode-v2.test.ts | 24 ++++++++++++++ 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index e8e4f5a1ec..8cb898c601 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -567,11 +567,13 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct if (ctx.session?.hook) { for (const name of ["context", "compaction", "title", "generate"]) { const reg = await ctx.session.hook(name, (event) => { - if (!event.messages) return; - const kept = event.messages.filter((message) => !( - message.role === "user" && BILI_REPORT_ID_RE.test(message.id ?? "") - )); - event.messages.splice(0, event.messages.length, ...kept); + const messages = event.messages; + if (!messages) return; + let kept = 0; + messages.forEach((message) => { + if (message.role !== "user" || !BILI_REPORT_ID_RE.test(message.id ?? "")) messages[kept++] = message; + }); + messages.length = kept; }); if (reg) registrations.push(reg); } diff --git a/src/loop/core.ts b/src/loop/core.ts index 5b4cba8c65..f3caa68d26 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -1779,7 +1779,11 @@ export async function* runCompressLoop( dropIds.add(`acp_loop_r${round}_asst_tc_${pr.callId}`); dropIds.add(`acp_loop_r${round}_tool_${pr.callId}`); } - coreMessages.splice(0, coreMessages.length, ...coreMessages.filter((m) => !dropIds.has(m.id))); + let kept = 0; + coreMessages.forEach((message) => { + if (!dropIds.has(message.id)) coreMessages[kept++] = message; + }); + coreMessages.length = kept; if (streamedMarkers.length > 0) { ctx.log(`[acp-loop] round ${round}: failed-only mutating round; feeding the model a recovery receipt (${streamedMarkers.length} marker line(s)) and re-requesting (#5220)`); coreMessages.push({ id: `acp_loop_r${round}_rcpt`, role: "assistant", contentType: "text", text: streamedMarkers.join("") }); diff --git a/tests/issue5220-failed-compress-receipt.test.ts b/tests/issue5220-failed-compress-receipt.test.ts index 68ff2cccf2..338c8335b7 100644 --- a/tests/issue5220-failed-compress-receipt.test.ts +++ b/tests/issue5220-failed-compress-receipt.test.ts @@ -173,6 +173,37 @@ test("#5220 T1: first-time failed compress → exactly one recovery-receipt re-r } }); +test("#5220 long history: a failed compress reaches the recovery re-request without an argument limit", async () => { + const messages = Array.from({ length: 250_000 }, (_, i) => textMsg(`m${i}`, i % 2 === 0 ? "user" : "assistant", `history ${i}`)); + const ctx = makeCtx(messages); + const bodies: string[] = []; + const orig = globalThis.fetch; + globalThis.fetch = (async (_u: unknown, init?: RequestInit) => { + bodies.push(String(init?.body)); + return new Response(NATURAL_END_ROUND, { status: 200, headers: { "content-type": "text/event-stream" } }); + }) as typeof fetch; + try { + const out = await drain( + new Response(failingRound(0), { status: 200 }).body!, + ctx, + { model: "gpt-4o", input: [], stream: true }, + { url: "http://mock", headers: {} }, + ); + assert.equal(bodies.length, 1); + assert.ok(out.includes("DONE-NATURAL-5220")); + const input = JSON.parse(bodies[0]).input as Array<{ role?: string; content?: unknown }>; + assert.equal(input.filter((message) => message.role === "user").length, 125_000); + assert.ok(bodies[0].includes("history 0")); + assert.ok(bodies[0].includes("history 249999")); + assert.ok(bodies[0].includes("❌")); + assert.ok(!bodies[0].includes("function_call")); + assert.ok(!bodies[0].includes("call_seq0")); + assert.equal(messages.length, 250_000); + } finally { + globalThis.fetch = orig; + } +}); + test("#5220 T3: identical failing call resubmitted → #156 signature breaker stops at round 2", async () => { let n = 0; const orig = globalThis.fetch; diff --git a/tests/opencode-v2.test.ts b/tests/opencode-v2.test.ts index ba5f79a639..f2ddb9ca11 100644 --- a/tests/opencode-v2.test.ts +++ b/tests/opencode-v2.test.ts @@ -247,6 +247,30 @@ test("v2 reports: outgoing hooks exclude only generated report IDs, including af } }); +for (const name of ["context", "compaction", "title", "generate"]) { + test(`v2 reports: ${name} filters long histories in place without an argument limit`, async () => { + const fake = makeFakeCtx(); + const cleanup = await biliOpencodePlugin.setup(fake.ctx as never); + try { + const reportId = `msg_bili_report_${"a".repeat(32)}`; + const kept = Array.from({ length: 250_000 }, (_, i) => ({ + id: `msg_${i}`, role: i % 2 === 0 ? "user" : "assistant", content: [], + })); + kept[1] = { id: reportId, role: "assistant", content: [] }; + const report = { id: reportId, role: "user", content: [] }; + const messages = [report, ...kept.slice(0, 125_000), report, ...kept.slice(125_000), report]; + const event = { sessionID: "ses_long_report", messages }; + await fake.contextHooks.get(name)!(event); + assert.strictEqual(event.messages, messages); + assert.deepStrictEqual(messages, kept); + assert.strictEqual(messages[0], kept[0]); + assert.strictEqual(messages.at(-1), kept.at(-1)); + } finally { + cleanup(); + } + }); +} + test("v2 reports: unsupported context hooks retain the inert body", async () => { const fake = makeFakeCtx(); const hook = fake.ctx.session.hook; From 3deaff518ec540f0c5a6f2b08dfda7fecc395de3 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 16:08:10 +0800 Subject: [PATCH 07/17] perf: count preflight covered message characters in linear time --- src/preflight.ts | 15 ++++- tests/preflight-fold-accounting.test.ts | 88 +++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 tests/preflight-fold-accounting.test.ts diff --git a/src/preflight.ts b/src/preflight.ts index c2f0177a0e..5f98e14d3f 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -328,6 +328,15 @@ function previewFoldTargetKey(core: CompressionCore, messages: CoreMessage[], st return target && foldTargetKey(target, state); } +function coveredMessageChars(messages: readonly CoreMessage[], ids: readonly string[]): number { + const covered = new Set(ids); + let chars = 0; + messages.forEach((message) => { + if (covered.has(message.id)) chars += (message.text ?? "").length; + }); + return chars; +} + export interface PreflightResult { compressedRanges: number; savedTokens: number; @@ -2551,7 +2560,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa // baseline currentTokens is char-based, so net the folded span's // char count against it instead of the token-based credit. const compressed = deps.session.stats.compressCreditTokens - creditBefore; - const folded = baselineKnown ? compressed : messages.filter((message) => effectiveMessageIds.includes(message.id)).reduce((total, message) => total + (message.text ?? "").length, 0); + const folded = baselineKnown ? compressed : coveredMessageChars(messages, effectiveMessageIds); currentTokens = Math.max(0, currentTokens - folded + countText(summary)); // The apply path (stream.ts applyRanges) already netted the gross // fold out of lastInputTokens and credited compressCreditTokens; @@ -2706,7 +2715,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa } const spanUnits = baselineKnown ? planned.compressedTokens - : messages.filter((message) => planned.effectiveMessageIds.includes(message.id)).reduce((total, message) => total + (message.text ?? "").length, 0); + : coveredMessageChars(messages, planned.effectiveMessageIds); const chunks = splitSummaryContent(content, budget, countText); // Track reservations locally: they count against the pass // budget only once the batch actually fires (≥2 nominations). @@ -2991,7 +3000,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa // summary exists; units match the post-fold accounting. const spanUnits = baselineKnown ? planned.compressedTokens - : messages.filter((message) => planned.effectiveMessageIds.includes(message.id)).reduce((total, message) => total + (message.text ?? "").length, 0); + : coveredMessageChars(messages, planned.effectiveMessageIds); if (emergencyFold) { // Zero-upstream-call fold. The digest fits maxSummaryLength AND // the net-shrink bound by construction (fallback forms), so it diff --git a/tests/preflight-fold-accounting.test.ts b/tests/preflight-fold-accounting.test.ts new file mode 100644 index 0000000000..3eb3557fb2 --- /dev/null +++ b/tests/preflight-fold-accounting.test.ts @@ -0,0 +1,88 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import http from "node:http"; +import test from "node:test"; +import { createCore, defaultConfig, defaultPrompts, type CoreMessage } from "acp-kernel"; +import { ConfiguredSummaryPlan } from "../src/external-summary-runtime.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { preflightCompress } from "../src/preflight.ts"; +import { createSession } from "../src/session.ts"; + +process.env.NODE_ENV = "test"; +_setStoreForTest(new SessionStore({ enabled: false })); +const SUMMARY = "The original task, agreed constraints, completed work and next steps are preserved in this compact fixture summary."; + +for (const batch of [false, true]) for (const unknownBaseline of [true, false]) test(`preflight fold accounting stays linear in covered message IDs (batch=${batch}, unknown=${unknownBaseline})`, async (t) => { + const count = 512; + const messages: CoreMessage[] = Array.from({ length: count }, (_, i) => ({ + id: `raw-${i}`, role: i === 0 || i === count - 1 ? "user" : "assistant", contentType: "text", + text: `${i}: ${"working context and concrete decisions ".repeat(25)}`, + })); + const core = createCore(); + const config = defaultConfig(1_000_000, { preserveRecentMessages: 0, preserveRecentTokens: 0 }); + config.compress.minCompressRange = 100; + const session = createSession(`preflight-accounting-${batch}`); + session.state = core.processTurn({ messages, state: session.state, config, tokenCount: 200_000, renderTags: "text-only" }).state; + session.stats.lastInputTokens = 130_000; + session.stats.requests = 1; + const realProcessTurn = core.processTurn.bind(core); + if (batch) core.processTurn = args => { + const turn = realProcessTurn(args); + if (turn.state.blocks.length > 0) return turn; + const refs = turn.state.messageRefs.byRaw; + const ranges = [[0, 254], [256, 509]].map(([a, z]) => ({ + startRef: refs[`raw-${a}`]!, endRef: refs[`raw-${z}`]!, + count: z! - a! + 1, tokens: 60_000, chars: 240_000, toolPct: 0, textPct: 1, + })); + return { ...turn, nudge: { ...turn.nudge!, compressibleRanges: ranges } }; + }; + let coveredIdReads = 0; + const realApply = core.applyCompression.bind(core); + core.applyCompression = args => { + const known = new Set(args.state.blocks.map(block => block.blockId)); + const result = realApply(args); + for (const block of result.state.blocks) if (!known.has(block.blockId)) { + block.effectiveMessageIds = new Proxy(block.effectiveMessageIds, { + get(target, key, receiver) { + if (typeof key === "string" && /^\d+$/.test(key)) coveredIdReads++; + return Reflect.get(target, key, receiver); + }, + }); + } + return result; + }; + let calls = 0; + const server = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + calls++; + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ output_text: SUMMARY, choices: [{ message: { role: "assistant", content: SUMMARY }, finish_reason: "stop" }] })); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + const url = `http://127.0.0.1:${address.port}`; + const logs: string[] = []; + try { + const externalSummary = batch ? new ConfiguredSummaryPlan({ enabled: true, targets: [{ name: "fixture", protocol: "openai", url, model: "fixture", apiKey: "fake" }] }) : undefined; + const result = await preflightCompress({ core, session, config, prompts: defaultPrompts, protocol: "responses", url, headers: {}, model: "fixture", unknownBaseline, compressionTarget: 5_000, externalSummary, log: (_level, line) => logs.push(line) }, messages); + assert.equal(calls, batch ? 2 : 1, logs.join("\n")); + assert.equal(result.fitsWindow, true, logs.join("\n")); + assert.ok(result.payloadEstimate < 5_000, logs.join("\n")); + assert.equal(result.compressedRanges, batch ? 2 : 1, logs.join("\n")); + assert.ok(session.state.blocks.every(block => block.summary === SUMMARY)); + const expected = batch ? [messages.slice(0, 255), messages.slice(256, 510)] : [messages.slice(0, 511)]; + const coverage = session.state.blocks.map(block => [...block.effectiveMessageIds]); + assert.deepEqual(coverage, expected.map(span => span.map(message => message.id))); + t.diagnostic(JSON.stringify({ coveredIdReads, calls, result, coverage: coverage.map(ids => ids.length) })); + assert.ok(coveredIdReads <= count * 100, `covered message IDs were visited ${coveredIdReads} times for ${count} messages`); + } finally { + const closed = once(server, "close"); + server.close(); + server.closeAllConnections(); + await closed; + } +}); From d55be36f5b9554338efda283fc0cc1427878a563 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 16:23:18 +0800 Subject: [PATCH 08/17] fix: retain recent tool-use routing witnesses across clock changes --- src/tool-ring.ts | 19 ++------ ...e4580-claudecode-tooluseid-routing.test.ts | 48 +++++++++++++++++-- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/tool-ring.ts b/src/tool-ring.ts index 5280454b1b..c84e5e4249 100644 --- a/src/tool-ring.ts +++ b/src/tool-ring.ts @@ -189,23 +189,12 @@ const toolUseIds = new Map(); * recordToolWitness. */ export function recordToolUseId(toolUseId: string, sessionId: string): void { if (toolUseId.length === 0 || toolUseId.length > 512) return; + // Refresh insertion order too: wall-clock ties and rollbacks are not recency. + toolUseIds.delete(toolUseId); toolUseIds.set(toolUseId, { sessionId, at: nowMs() }); while (toolUseIds.size > TOOL_USE_ID_CAPACITY) { - // Evict the entry with the smallest recorded time. Full scan — the cap - // is tiny and this only runs past it. A re-record keeps its insertion - // slot but refreshes `at`, so position order is NOT age order (a - // first-inserted id can be the freshest one in the ring). - let oldestKey: string | undefined; - let oldestAt = Infinity; - for (const [k, v] of toolUseIds) { - if (v.at < oldestAt) { - oldestAt = v.at; - oldestKey = k; - } - } - // Never evict what this call just wrote (clock went backwards): leave - // the ring one over the cap until a normal insert can evict someone else. - if (oldestKey === undefined || oldestKey === toolUseId) break; + const oldestKey = toolUseIds.keys().next().value; + if (oldestKey === undefined) break; toolUseIds.delete(oldestKey); } } diff --git a/tests/issue4580-claudecode-tooluseid-routing.test.ts b/tests/issue4580-claudecode-tooluseid-routing.test.ts index 1ba860e060..dfced5c418 100644 --- a/tests/issue4580-claudecode-tooluseid-routing.test.ts +++ b/tests/issue4580-claudecode-tooluseid-routing.test.ts @@ -156,9 +156,7 @@ describe("#4580 recording: response pipes ring tool_use ids per lane", () => { }); test("capacity eviction drops the least-recently-seen id, not the first-inserted", () => { - // A re-record keeps its insertion slot but refreshes its timestamp — - // the first-inserted id can be the FRESHEST entry, so eviction must - // order by time, never by map position. + // Re-recording an old ID makes it recent regardless of its first insertion. let clock = 2_000_000; _setToolRingClockForTest(() => clock); try { @@ -170,7 +168,7 @@ describe("#4580 recording: response pipes ring tool_use ids per lane", () => { clock += 1; } clock += 1000; - recordToolUseId(anchor, "lane-anchor"); // re-record: newest by time, still slot 1 + recordToolUseId(anchor, "lane-anchor"); // re-record: newest observation recordToolUseId("toolu_newest", "lane-newest"); // overflows the cap → one eviction assert.equal(lookupToolUseIdSession(anchor), "lane-anchor", "re-recorded anchor survives: it is the newest by time"); assert.equal(lookupToolUseIdSession("toolu_fill_0"), undefined, "true oldest entry (by time) is evicted"); @@ -179,6 +177,25 @@ describe("#4580 recording: response pipes ring tool_use ids per lane", () => { _setToolRingClockForTest(undefined); } }); + + test("capacity remains bounded while the wall clock moves backwards", () => { + let clock = 3_000_000; + _setToolRingClockForTest(() => clock); + try { + for (let i = 0; i < 2048; i++) { + clock--; + recordToolUseId(`clock-${i}`, "lane-clock"); + } + const retained = Array.from({ length: 2048 }, (_, i) => lookupToolUseIdSession(`clock-${i}`)) + .filter(id => id !== undefined); + assert.equal(retained.length, 1024); + assert.equal(lookupToolUseIdSession("clock-1023"), undefined); + assert.equal(lookupToolUseIdSession("clock-1024"), "lane-clock"); + assert.equal(lookupToolUseIdSession("clock-2047"), "lane-clock"); + } finally { + _setToolRingClockForTest(undefined); + } + }); }); // ─────────────────── 2. routing: the id outranks a stale binding ───────────── @@ -223,6 +240,29 @@ describe("#4580 routing: host-stamped toolCallId routes to the witnessing lane", assert.ok(logs.some((m) => m.includes("routed by host call id (#4580)") && m.includes(sub.id)), "must log the id-routed lane"); }); + for (const rollback of [false, true]) test(`refreshed host ID still routes to its subagent after ring overflow (clock rollback=${rollback})`, async () => { + const parent = getSession("recency-parent"); + const sub = getSession("recency-sub"); + parent.lastSeen = sub.lastSeen = Date.now(); + let clock = 4_000_000; + _setToolRingClockForTest(() => clock); + try { + recordToolUseId("recent-sub-call", sub.id); + for (let i = 0; i < 1023; i++) recordToolUseId(`fill-${i}`, parent.id); + if (rollback) clock -= 10_000; + recordToolUseId("recent-sub-call", sub.id); + if (rollback) clock++; + recordToolUseId("another-call", parent.id); + const result = await post({ conversationId: parent.id, tool: "acp_status", args: {}, toolCallId: "recent-sub-call" }); + assert.equal(result.status, 200); + assert.deepEqual(executedOn(), [sub.id]); + assert.equal(lookupToolUseIdSession("fill-0"), undefined); + assert.equal(lookupToolUseIdSession("another-call"), parent.id); + } finally { + _setToolRingClockForTest(undefined); + } + }); + test("id ring miss: identical POST without a known id keeps the legacy ladder (no silent fallback)", async () => { const parent = getSession("t4580-parent2"); const sub = getSession("t4580-sub2"); From 5a116b39362a8dd22f55aa0e8d4aad365cb53cb2 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 16:40:53 +0800 Subject: [PATCH 09/17] perf: reuse the fork prefix index when inheriting ref state --- scripts/bench-fork-ref-membership.mjs | 76 +++++++++++++++++++++++++++ src/plugin.ts | 10 ++-- tests/plugin-fork.test.ts | 34 ++++++++++++ 3 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 scripts/bench-fork-ref-membership.mjs diff --git a/scripts/bench-fork-ref-membership.mjs b/scripts/bench-fork-ref-membership.mjs new file mode 100644 index 0000000000..533f7843a4 --- /dev/null +++ b/scripts/bench-fork-ref-membership.mjs @@ -0,0 +1,76 @@ +// Public snapshot/fork handlers with deterministic in-memory parent state; no upstream model call. +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { performance } from "node:perf_hooks"; +import { handlePluginFork, handlePluginSnapshot, _resetPluginStateForTest } from "../src/plugin.ts"; +import { getSession, _resetSessionsForTest } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; + +const count = Number(process.argv[2] ?? 12000); +assert(Number.isSafeInteger(count) && count >= 4); +const digest = (value) => createHash("sha256").update(JSON.stringify(value)).digest("hex"); + +async function response(run) { + let status; + let body; + await run({ writeHead(value) { status = value; }, end(value) { body = JSON.parse(value); } }); + assert(status >= 200 && status < 300, JSON.stringify({ status, body })); + return body; +} + +const timings = []; +const outputs = new Set(); +for (let run = 0; run < 4; run++) { + const dir = mkdtempSync(join(tmpdir(), "bili-fork-ref-bench-")); + const store = new SessionStore({ dir, enabled: false }); + _setStoreForTest(store); + _resetSessionsForTest(); + _resetPluginStateForTest(); + try { + const parent = getSession("bench-parent"); + const refs = Array.from({ length: count }, (_, i) => `m${String(i + 1).padStart(5, "0")}`); + parent.pluginSnapshot = refs.map((ref, i) => { + const id = `source-${i}`; + parent.state.messageRefs.byRaw[id] = ref; + parent.state.messageRefs.byRef[ref] = id; + return { id, role: i % 2 ? "assistant" : "user", contentType: "text", text: `source text ${i}` }; + }); + parent.state.lastPassIds = parent.pluginSnapshot.map((message) => message.id); + parent.state.hiddenOrphanRefs = [...refs]; + parent.state.emergencyTruncateLatch = refs.map((ref) => ({ ref, text: `clipped ${ref}` })); + parent.state.renderDeferredRefs = [...refs]; + parent.state.deadRefs = [...refs]; + const snapshot = await response((res) => handlePluginSnapshot(parent.id, res)); + const orderedMessages = snapshot.orderedMessages.slice(0, Math.floor(count / 2)); + const payload = JSON.stringify({ + protocolVersion: 2, parentConversationId: parent.id, childConversationId: "bench-child", + parentRevision: snapshot.parentRevision, + branchPoint: { messageCount: orderedMessages.length, orderHash: digest(orderedMessages) }, + orderedMessages, idempotencyKey: "bench-fork", + }); + const started = performance.now(); + await response((res) => handlePluginFork(payload, res)); + const elapsed = performance.now() - started; + const child = getSession("bench-child"); + const state = child.state; + outputs.add(digest({ + hidden: state.hiddenOrphanRefs, clipped: state.emergencyTruncateLatch, + deferred: state.renderDeferredRefs, dead: state.deadRefs, messages: child.pluginSnapshot, + })); + assert.equal(state.hiddenOrphanRefs.length, orderedMessages.length); + assert.equal(state.deadRefs.length, count - orderedMessages.length); + if (run > 0) timings.push(elapsed); + } finally { + store.cancelAll(); + store.closeQuietly(); + _resetSessionsForTest(); + _resetPluginStateForTest(); + rmSync(dir, { recursive: true, force: true }); + } +} +assert.equal(outputs.size, 1); +timings.sort((a, b) => a - b); +console.log(JSON.stringify({ count, prefix: Math.floor(count / 2), medianMs: timings[1], measuredMs: timings, sha256: [...outputs][0] })); diff --git a/src/plugin.ts b/src/plugin.ts index 9e1d913741..7eaed67704 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1800,17 +1800,17 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr child.state.lastPassIds = prefix.map((m) => m.rawId); child.state.rules = structuredClone(parent.state.rules); child.state.nextRuleId = parent.state.nextRuleId; - child.state.hiddenOrphanRefs = parent.state.hiddenOrphanRefs?.filter((ref) => prefix.some((m) => m.ref === ref)); + const forkRefs = new Set(prefix.map((m) => m.ref)); + child.state.hiddenOrphanRefs = parent.state.hiddenOrphanRefs?.filter((ref) => forkRefs.has(ref)); // #3939 ③: only latch entries whose messages rode the forked // prefix — the child never sees the others, so their entries // would be dead weight (and retire on the next latch pass anyway). - child.state.emergencyTruncateLatch = parent.state.emergencyTruncateLatch?.filter((e) => prefix.some((m) => m.ref === e.ref)); + child.state.emergencyTruncateLatch = parent.state.emergencyTruncateLatch?.filter((e) => forkRefs.has(e.ref)); // #3939 ②: deferred refs are content-keyed (byRaw ids) — keep only // the ones whose carrier rode the forked prefix. - child.state.renderDeferredRefs = parent.state.renderDeferredRefs?.filter((r) => prefix.some((m) => m.ref === r)); + child.state.renderDeferredRefs = parent.state.renderDeferredRefs?.filter((r) => forkRefs.has(r)); // #3939 ⑤: view-latch entries are ref-keyed — keep only the // carriers that rode the forked prefix. - const forkRefs = new Set(prefix.map((m) => m.ref)); if (parent.state.viewLatches) { child.state.viewLatches = Object.fromEntries( Object.entries(parent.state.viewLatches).filter(([r]) => forkRefs.has(r)), @@ -1835,7 +1835,7 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr // #2362: inverse of the orphan filter — a dead ref whose message IS // in the forked prefix is visible again in the child; refs outside // it stay dead (their messages are absent from the child too). - child.state.deadRefs = parent.state.deadRefs?.filter((ref) => !prefix.some((m) => m.ref === ref)); + child.state.deadRefs = parent.state.deadRefs?.filter((ref) => !forkRefs.has(ref)); child.state.stats.tokensCompressed = child.state.blocks.filter((b) => b.active).reduce((sum, b) => sum + b.compressedTokens, 0); child.state.stats.compressionCount = blocks.length; if (prefix.length === snapshot.orderedMessages.length) child.state.nudge = structuredClone(parent.state.nudge); diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 0d4c37a5cb..8e555df402 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -414,6 +414,40 @@ function forkRequest(snapshot: { parentRevision: string; orderedMessages: unknow return { protocolVersion: 1, parentConversationId: "parent", childConversationId: child, parentRevision: snapshot.parentRevision, branchPoint: { messageCount: count, orderHash: hash(orderedMessages) }, orderedMessages, idempotencyKey: `fork-${child}` }; } +test("HTTP fork retains ref state in source order and removes only the excluded side", { timeout: 60_000 }, async () => { + const h = await harness(false, false); + try { + const count = 256; + const history = Array.from({ length: count }, (_, i) => ({ role: i % 2 ? "assistant" : "user", content: `fork source ${i}` })); + await sendIntentModel(h, "parent", history, [], "main"); + const snapshot = await h.request("/__bili/plugin/snapshot?conversationId=parent"); + assert.equal(snapshot.status, 200, JSON.stringify(snapshot.body)); + const refs = snapshot.body.orderedMessages.map((message) => message.ref); + const parent = resolveConversation("parent").session!; + parent.state.hiddenOrphanRefs = [refs[127]!, refs[128]!, refs[0]!, refs[127]!]; + parent.state.emergencyTruncateLatch = [ + { ref: refs[1]!, text: "first clipped text" }, + { ref: refs[200]!, text: "outside clipped text" }, + { ref: refs[126]!, text: "last clipped text" }, + ]; + parent.state.renderDeferredRefs = [refs[255]!, refs[2]!, refs[2]!]; + parent.state.deadRefs = [refs[3]!, refs[254]!, refs[255]!, refs[254]!]; + parent.state.viewLatches = { [refs[4]!]: { text: "inside" }, [refs[129]!]: { text: "outside" } }; + const before = structuredClone(parent.state); + + const fork = await h.request("/__bili/plugin/fork", forkRequest(snapshot.body, "child", 128)); + assert.equal(fork.status, 201, JSON.stringify(fork.body)); + const child = resolveConversation("child").session!; + assert.deepEqual(child.state.hiddenOrphanRefs, [refs[127], refs[0], refs[127]]); + assert.deepEqual(child.state.emergencyTruncateLatch, [before.emergencyTruncateLatch![0], before.emergencyTruncateLatch![2]]); + assert.deepEqual(child.state.renderDeferredRefs, [refs[2], refs[2]]); + assert.deepEqual(child.state.deadRefs, [refs[254], refs[255], refs[254]]); + assert.deepEqual(child.state.viewLatches, { [refs[4]!]: { text: "inside" } }); + assert.deepEqual(child.pluginSnapshot, parent.pluginSnapshot!.slice(0, 128)); + assert.deepEqual(parent.state, before, "fork filtering does not mutate the parent's state"); + } finally { await h.close(); } +}); + test("HTTP protected tools keep plain-text snapshots, forks and revisions usable (#2620)", { timeout: 60_000 }, async () => { const h = await harness(false, false, { protectedTools: ["ask_user_question"] }); try { From 8b72a0dca7c996680625e4f96d92d1b6e9ac0782 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 17:16:40 +0800 Subject: [PATCH 10/17] Index direct block IDs when preparing external summaries --- src/external-summary-compress.ts | 3 +- tests/external-summary-runtime.test.ts | 68 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/external-summary-compress.ts b/src/external-summary-compress.ts index 1b0fe34898..004032b072 100644 --- a/src/external-summary-compress.ts +++ b/src/external-summary-compress.ts @@ -89,11 +89,12 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx }).join("\n\n"); if (!content.trim()) continue; const covered = new Set(blocks.flatMap((block) => block.effectiveMessageIds)); + const directBlockIds = new Set(blocks.flatMap((block) => block.directBlockIds)); const reference = JSON.stringify({ submittedSummaryHint: range.summary, currentContext: messages.filter((message, index) => !covered.has(message.id) && (message.role === "user" || index >= messages.length - 5)).map(({ role, text }) => ({ role, text })), - otherSummaries: state.blocks.filter((candidate) => candidate.active && !blocks.some((block) => block.directBlockIds.includes(candidate.blockId))) + otherSummaries: state.blocks.filter((candidate) => candidate.active && !directBlockIds.has(candidate.blockId)) .map(({ topic, summary }) => ({ topic, summary })), }); valid.push(range); diff --git a/tests/external-summary-runtime.test.ts b/tests/external-summary-runtime.test.ts index dc00f10e82..b97cdc4ac0 100644 --- a/tests/external-summary-runtime.test.ts +++ b/tests/external-summary-runtime.test.ts @@ -364,6 +364,74 @@ test("batch protection checks the aggregate range length instead of rejecting va }, (_req, res) => { calls++; success(res); }); }); +test("external refolds index selected block ids once while preserving reference order", async (t) => { + let sentReference: Record | undefined; + let blockIdReads = 0; + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + const count = 512; + const selected = 384; + ctx.messages = [ + ...Array.from({ length: count }, (_, index): CoreMessage => ({ + id: `reference-source-${index}`, role: "assistant", contentType: "text", text: `Source ${index}. ${RAW}`, + })), + ctx.messages[1], + ]; + ctx.session.state = ctx.core.processTurn({ messages: ctx.messages, state: ctx.session.state, config: ctx.config, tokenCount: 200_000 }).state; + const seeded = ctx.core.applyCompression({ + messages: ctx.messages, state: ctx.session.state, config: ctx.config, + ranges: ctx.messages.slice(0, count).map((message, index) => ({ + startRef: ctx.session.state.messageRefs.byRaw[message.id], endRef: ctx.session.state.messageRefs.byRaw[message.id], + summary: `Historical summary ${index}. ${SUMMARY}`, topic: `topic-${index}`, + })), + }); + assert.equal(seeded.result.blocksCreated, count, JSON.stringify(seeded.result)); + ctx.session.state = seeded.state; + const lastId = seeded.state.blocks.at(-1)!.blockId; + const retired = ctx.core.applyCompression({ + messages: ctx.messages, state: ctx.session.state, config: ctx.config, + ranges: [{ startRef: lastId, endRef: lastId, summary: SUMMARY, topic: "retained-distillation" }], + }); + assert.equal(retired.result.blocksCreated, 1, JSON.stringify(retired.result)); + ctx.session.state = retired.state; + const initial = ctx.session.state; + const selectedIds = new Set(initial.blocks.slice(0, selected).map((block) => block.blockId)); + const expected = initial.blocks.filter((block) => block.active && !selectedIds.has(block.blockId)) + .map(({ topic, summary }) => ({ topic, summary })); + const applyCompression = ctx.core.applyCompression.bind(ctx.core); + let preview = true; + ctx.core = { ...ctx.core, applyCompression(input) { + const result = applyCompression(input); + if (!preview) return result; + preview = false; + for (const block of result.state.blocks) { + if (block.summary.startsWith("external-probe-")) block.directBlockIds = new Proxy(block.directBlockIds, { + get(target, key, receiver) { + if (typeof key === "string" && /^\d+$/.test(key)) blockIdReads++; + return Reflect.get(target, key, receiver); + }, + }); + } + return result; + } }; + + const result = await applyConfiguredCompression({ content: [{ + startId: initial.blocks[0].blockId, endId: initial.blocks[selected - 1].blockId, summary: "Retain the selected historical work.", + }] }, ctx); + + assert.equal(result.outcome, "applied", result.text); + assert.deepEqual(sentReference?.otherSummaries, expected); + assert.deepEqual(sentReference?.currentContext, ctx.messages.slice(-5).map(({ role, text }) => ({ role, text }))); + assert.equal(sentReference?.submittedSummaryHint, "Retain the selected historical work."); + t.diagnostic(`${blockIdReads} selected-block-id visits for ${count} source blocks`); + assert.ok(blockIdReads <= count * 8, `selected block ids were visited ${blockIdReads} times for ${count} source blocks`); + }, (_req, res, body) => { + const payload = JSON.parse((body.input as Array<{ content: string }>)[0].content) as { reference: string }; + sentReference = JSON.parse(payload.reference) as Record; + success(res); + }); +}); + test("inline restore refolds the original block even when originals have left the wire view", async () => { let calls = 0; await fixture(async (_base, externalSummary) => { From 0eb56edd1e0ce0e70a27bc932a6b17529515153c Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 17:29:39 +0800 Subject: [PATCH 11/17] Keep cache size-bucket reports within the argument limit --- src/cache-ledger.ts | 2 +- tests/cache-ledger-large-buckets.test.ts | 66 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/cache-ledger-large-buckets.test.ts diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 3393e4ae5e..9d6acbb3be 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -2276,7 +2276,7 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport { else byLo.set(lo, [l.hitPct]); } for (const [lo, hits] of [...byLo.entries()].sort((x, y) => x[0] - y[0]).slice(0, 24)) { - sizeBuckets.push({ lo, hi: lo + SIZE_BUCKET_TOK, n: hits.length, hitMedian: median(hits) ?? 0, hitMin: Math.min(...hits) }); + sizeBuckets.push({ lo, hi: lo + SIZE_BUCKET_TOK, n: hits.length, hitMedian: median(hits) ?? 0, hitMin: hits.reduce((min, hit) => Math.min(min, hit), Infinity) }); } } const lowGaps: number[] = []; diff --git a/tests/cache-ledger-large-buckets.test.ts b/tests/cache-ledger-large-buckets.test.ts new file mode 100644 index 0000000000..647bb06335 --- /dev/null +++ b/tests/cache-ledger-large-buckets.test.ts @@ -0,0 +1,66 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { buildSessionCacheReport, getCacheLedger, handleAcpCache, recordCacheSample } from "../src/cache-ledger.ts"; +import type { Session } from "../src/session.ts"; + +function session(id: string): Session { + return { + id, metadata: {}, stats: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} + +const T0 = Date.parse("2026-10-01T00:00:00Z"); + +test("cache reports remain available when one size bucket exceeds the argument limit", () => { + const s = session("large-cache-bucket"); + const count = 150_000; + for (let index = 0; index < count; index++) { + recordCacheSample(s, { at: T0 + index * 1000, input: 10_000, cached: index === count - 1 ? 5_000 : 9_000 }); + } + const summary = handleAcpCache(s); + const full = handleAcpCache(s, { detail: "full" }); + assert.deepEqual([summary.outcome, full.outcome], ["success", "success"], `${summary.text}\n${full.text}`); + assert.match(summary.text, /ACP CACHE REPORT \(large-cache-bucket\)/); + assert.match(full.text, /LINE ITEMS \(last 512 of 150000\):/); + const report = buildSessionCacheReport(s); + assert.deepEqual(report.stability.sizeBuckets, [{ lo: 0, hi: 20_000, n: count, hitMedian: 90, hitMin: 50 }]); + assert.equal(report.totals.requests, count); + assert.equal(report.totals.balanced, true); + assert.equal(report.lines.length, count); + assert.equal(report.linesOmitted, 0); + assert.equal(getCacheLedger(s).lines.length, count); +}); + +test("bucket minima and medians keep their numeric values and unknown exclusions", () => { + const s = session("small-cache-buckets"); + const samples = [ + { input: 40_000, cached: 10_000 }, + { input: 10_000, cached: 3_331 }, + { input: 10_000, cached: 10_000 }, + { input: 40_000, cached: 0 }, + { input: 10_000, cached: 0 }, + { input: 40_000, cached: 40_000 }, + { input: 10_000, cached: null }, + { input: 0, cached: 0 }, + ]; + samples.forEach((sample, index) => recordCacheSample(s, { at: T0 + index * 1000, ...sample })); + const report = buildSessionCacheReport(s); + assert.deepEqual(report.stability.sizeBuckets, [ + { lo: 0, hi: 20_000, n: 3, hitMedian: 33.3, hitMin: 0 }, + { lo: 40_000, hi: 60_000, n: 3, hitMedian: 25, hitMin: 0 }, + ]); + assert.equal(report.unmeasured.samples, 1); +}); + +test("size buckets keep ascending order and the existing 24-bucket display cap", () => { + const s = session("cache-bucket-cap"); + for (let index = 26; index >= 0; index--) { + const input = index * 20_000 + 10_000; + recordCacheSample(s, { at: T0 + (26 - index) * 1000, input, cached: input / 2 }); + } + assert.deepEqual(buildSessionCacheReport(s).stability.sizeBuckets, Array.from({ length: 24 }, (_, index) => ({ + lo: index * 20_000, hi: (index + 1) * 20_000, n: 1, hitMedian: 50, hitMin: 50, + }))); + assert.deepEqual(buildSessionCacheReport(session("empty-cache-buckets")).stability.sizeBuckets, []); +}); From 89ad7521bc8fc991017aafd47d6dcde178cba488 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 17:55:30 +0800 Subject: [PATCH 12/17] fix: locate root message arrays in cache-diff attribution --- src/acp-cache-diff.ts | 37 ++++++++--- tests/acp-cache-diff-root-array.test.ts | 88 +++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 tests/acp-cache-diff-root-array.test.ts diff --git a/src/acp-cache-diff.ts b/src/acp-cache-diff.ts index 6df7c6b095..4ba98d08e3 100644 --- a/src/acp-cache-diff.ts +++ b/src/acp-cache-diff.ts @@ -218,6 +218,31 @@ interface MsgArrayInfo { items: unknown[]; } +function rootArrayStart(text: string, key: string): number { + let depth = 0; + let found = -1; + for (let i = 0; i < text.length; i++) { + const c = text[i]!; + if (c === '"') { + const start = i++; + while (i < text.length) { + if (text[i] === "\\") { i += 2; continue; } + if (text[i] === '"') break; + i++; + } + if (depth !== 1) continue; + let value = i + 1; + while (value < text.length && " \t\n\r".includes(text[value]!)) value++; + if (text[value] !== ":" || JSON.parse(text.slice(start, i + 1)) !== key) continue; + value++; + while (value < text.length && " \t\n\r".includes(text[value]!)) value++; + found = text[value] === "[" ? value : -1; + } else if (c === "{" || c === "[") depth++; + else if (c === "}" || c === "]") depth--; + } + return found; +} + function msgArrayInfo(text: string): MsgArrayInfo | null { let parsed: unknown; try { parsed = JSON.parse(text); } catch { return null; } @@ -226,16 +251,8 @@ function msgArrayInfo(text: string): MsgArrayInfo | null { for (const key of MSG_KEYS) { const arr = obj[key]; if (!Array.isArray(arr)) continue; - const re = new RegExp(`"${key}"\\s*:\\s*\\[`, "g"); - let firstStart = -1; - let m: RegExpExecArray | null; - while ((m = re.exec(text)) !== null) { - const start = m.index + m[0].length - 1; - if (firstStart < 0) firstStart = start; - const spans = elementSpans(text, start); - if (spans.length === arr.length) return { key, spans, items: arr }; - } - if (firstStart >= 0) return { key, spans: elementSpans(text, firstStart), items: arr }; + const start = rootArrayStart(text, key); + if (start >= 0) return { key, spans: elementSpans(text, start), items: arr }; } return null; } diff --git a/tests/acp-cache-diff-root-array.test.ts b/tests/acp-cache-diff-root-array.test.ts new file mode 100644 index 0000000000..c169c26e5c --- /dev/null +++ b/tests/acp-cache-diff-root-array.test.ts @@ -0,0 +1,88 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { compareWires, runDiff } from "../src/acp-cache-diff.ts"; +import { rmrf } from "./tmp-rm.ts"; + +const first = { role: "user", content: "one" }; +const changed = { role: "user", content: "two" }; +const second = { role: "assistant", content: "answer" }; +const wire = (value: unknown): Buffer => Buffer.from(JSON.stringify(value)); + +function request(key: string, messages: unknown[], example = "sample"): Record { + return { + tools: [{ + type: "function", + function: { + name: "replay", + parameters: { type: "object", properties: { defaults: { default: { [key]: [{ role: "user", content: example }] } } } }, + }, + }], + [key]: messages, + }; +} + +for (const key of ["messages", "input", "contents"]) { + test(`cache diff: a nested ${key} example cannot hide a root history rewrite`, () => { + const result = compareWires(wire(request(key, [first])), wire(request(key, [changed]))); + assert.equal(result.rel, "diverge"); + assert.equal(result.append, null); + assert.equal(result.preArray, false); + }); + + test(`cache diff: root ${key} append uses its own element spans`, () => { + const result = compareWires(wire(request(key, [first])), wire(request(key, [first, second]))); + assert.equal(result.rel, "append"); + assert.deepEqual(result.append, { kind: "json-tail", countA: 1, countB: 2 }); + }); + + test(`cache diff: changing a nested ${key} example stays before the root array`, () => { + const result = compareWires(wire(request(key, [first], "before")), wire(request(key, [first], "AFTER!"))); + assert.equal(result.rel, "diverge"); + assert.equal(result.preArray, true); + }); +} + +test("cache diff: escaped root keys and delimiters inside strings retain root positions", () => { + const a = Buffer.from('{"model":"old","note":"} [ \\\"messages\\\": [","mess\\u0061ges":[{"role":"user","content":"one"}]}'); + const b = Buffer.from(a.toString().replace('"old"', '"new"')); + assert.doesNotThrow(() => JSON.parse(a.toString())); + assert.doesNotThrow(() => JSON.parse(b.toString())); + const result = compareWires(a, b); + assert.equal(result.rel, "diverge"); + assert.equal(result.preArray, true); +}); + +test("cache diff: duplicate root keys use the last value as JSON.parse does", () => { + const a = Buffer.from('{"messages":[{"role":"user","content":"example"}],"messages":[{"role":"user","content":"one"}]}'); + const b = Buffer.from(a.toString().replace('"one"', '"two"')); + assert.equal(compareWires(a, b).rel, "diverge"); +}); + +test("cache diff: ordinary arrays and strict byte prefixes keep their classifications", () => { + assert.equal(compareWires(wire({ messages: [first] }), wire({ messages: [first, second] })).append?.kind, "json-tail"); + assert.equal(compareWires(wire({ messages: [first] }), wire({ messages: [changed] })).rel, "diverge"); + assert.equal(compareWires(Buffer.from('{"a":1'), Buffer.from('{"a":12}')).append?.kind, "strict"); +}); + +test("cache diff: dumped nested examples cannot mask proxy rewriting in the public report", async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "acp-diff-root-array-")); + try { + const incoming = JSON.stringify(request("messages", [first])); + const rewritten = JSON.stringify(request("messages", [changed])); + for (const [stamp, kind, body] of [[1000, "INCOMING", incoming], [1001, "REQ", incoming], [2000, "INCOMING", incoming], [2001, "REQ", rewritten]]) { + fs.writeFileSync(path.join(dir, `${stamp}-ses_root-${kind}.txt`), `POST /v1/chat/completions\ncontent-type: application/json\n\n${body}`); + } + const report = await runDiff(dir, { noLog: true }); + assert.equal(report.totalPairs, 1); + const pair = report.sessions[0]!.pairs[0]!; + assert.equal(pair.category, "mid-stream-rewrite"); + assert.equal(pair.attribution, "proxy"); + assert.equal(pair.divergence?.messageIndex, 0); + assert.equal(pair.divergence?.roleType, "user"); + } finally { + rmrf(dir); + } +}); From a2a68ed3152fe35e2dac5a27fde1600f7b151bf8 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 18:03:09 +0800 Subject: [PATCH 13/17] fix: keep Markdown inline code literal in handoff rendering --- src/web/mdrender.ts | 3 ++- tests/mdrender.test.ts | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/web/mdrender.ts b/src/web/mdrender.ts index 5735e0953f..1cbed3939e 100644 --- a/src/web/mdrender.ts +++ b/src/web/mdrender.ts @@ -36,7 +36,8 @@ export function makeMdRender(): MdRenderApi { function inlineMd(s: string): string { let out = escape(s); - out = out.replace(/`([^`]+)`/g, (_m, code: string) => `${code}`); + out = out.replace(/`([^`]+)`/g, (_m, code: string) => + `${code.replace(/[*[\]]/g, (c) => `&#${c.charCodeAt(0)};`)}`); // ![alt](url) — must run before the link rule so the image syntax is // consumed whole. Only published image sources survive: http(s), or the // panel's own bundled-asset route (/__bili/blog-assets/*, see diff --git a/tests/mdrender.test.ts b/tests/mdrender.test.ts index e50e88a9fd..8a42d89a52 100644 --- a/tests/mdrender.test.ts +++ b/tests/mdrender.test.ts @@ -61,6 +61,50 @@ test("makeMdRender is self-contained (IIFE inlining contract)", async () => { assert.equal(det.highlight('{"a": 1}', "json"), md.highlight('{"a": 1}', "json")); }); +const literalCodeSamples = [ + "*literal* **also literal**", + "[guide](https://example.test/docs)", + "![diagram](https://example.test/image.png)", + "[blocked](javascript:alert(1))", + ' & "quotes"', + "plain_code", +]; + +function assertLiteralCode(html: string, expected: string): void { + const inner = /([\s\S]*?)<\/code>/.exec(html)?.[1]; + assert.notEqual(inner, undefined); + assert.ok(!inner!.includes("<"), "code contains text rather than nested markup"); + const entities: Record = { amp: "&", lt: "<", gt: ">", quot: '"' }; + const text = inner!.replace(/&#(\d+);|&#x([a-f\d]+);|&(amp|lt|gt|quot);/gi, (_match, decimal, hex, named) => + decimal ? String.fromCodePoint(Number(decimal)) : hex ? String.fromCodePoint(Number.parseInt(hex, 16)) : entities[named.toLowerCase()]!); + assert.equal(text, expected); +} + +for (const sample of literalCodeSamples) { + test(`server Markdown preserves inline code literally: ${sample}`, () => { + assertLiteralCode(markdownToHtml(`\`${sample}\``), sample); + }); +} + +test("detached browser renderer keeps inline code literal", async () => { + const detached = await makeDetached(); + for (const sample of literalCodeSamples) assertLiteralCode(detached.render(`\`${sample}\``), sample); +}); + +test("inline code keeps its literal content inside a link label", () => { + const html = md.render("[read `**literal**`](https://example.test/docs)"); + assert.match(html, / { + const html = md.render("**bold** and `*literal*` and *italic* and [link](https://example.test/docs)"); + assertLiteralCode(html, "*literal*"); + assert.match(html, /bold<\/strong>/); + assert.match(html, /italic<\/em>/); + assert.equal((html.match(/ \" into entities (single quote passes through, as in the old renderer)", () => { From 0eb1b13beab6fa5dc64a623039711c8919082f6e Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 18:17:18 +0800 Subject: [PATCH 14/17] Keep literal greater-than entities out of blockquote parsing --- src/web/mdrender.ts | 2 +- tests/mdrender-entities.test.ts | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/mdrender-entities.test.ts diff --git a/src/web/mdrender.ts b/src/web/mdrender.ts index 5735e0953f..b98283ebe1 100644 --- a/src/web/mdrender.ts +++ b/src/web/mdrender.ts @@ -206,7 +206,7 @@ export function makeMdRender(): MdRenderApi { html.push(`
${escape(buf.map((l) => l.slice(cut)).join("\n"))}
`); continue; } - if (/^\s*>\s?/.test(line) || /^\s*>\s?/.test(line)) { + if (/^\s*>\s?/.test(line)) { const buf: string[] = []; while (i < lines.length && /^\s*>\s?/.test(lines[i])) { buf.push(lines[i].replace(/^\s*>\s?/, "")); i += 1; } html.push(`

${inlineMd(buf.join(" "))}

`); diff --git a/tests/mdrender-entities.test.ts b/tests/mdrender-entities.test.ts new file mode 100644 index 0000000000..4c9e37cb44 --- /dev/null +++ b/tests/mdrender-entities.test.ts @@ -0,0 +1,46 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { runInNewContext } from "node:vm"; +import { build } from "esbuild"; +import { markdownToHtml } from "../src/web/markdown.ts"; +import { makeMdRender } from "../src/web/mdrender.ts"; + +const literalLines = [ + ["> example", "

&gt; example

"], + [" > example", "

&gt; example

"], + [">", "

&gt;

"], + [">