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/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/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/acp-cache-diff.ts b/src/acp-cache-diff.ts index 15d28b0358..023955e941 100644 --- a/src/acp-cache-diff.ts +++ b/src/acp-cache-diff.ts @@ -227,6 +227,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; } @@ -235,16 +260,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/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/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/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/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/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/src/fold-reconcile.ts b/src/fold-reconcile.ts index 264ffff8ae..81bcea8277 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1641,29 +1641,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/src/image-restore.ts b/src/image-restore.ts index 4282aeb106..5c59fe4d40 100644 --- a/src/image-restore.ts +++ b/src/image-restore.ts @@ -203,6 +203,20 @@ function coreMsgsOf(result: unknown): CoreMessage[] | undefined { return undefined; } +function jsonAsciiMarker(text: string, foldCase = false): string { + return [...text].map((character) => { + const variants = foldCase ? [...new Set([character.toLowerCase(), character.toUpperCase()])] : [character]; + return `(?:${variants.flatMap((value) => { + const hex = value.charCodeAt(0).toString(16).padStart(4, "0").replace(/[a-f]/g, (digit) => `[${digit}${digit.toUpperCase()}]`); + return [value, `\\\\u${hex}`]; + }).join("|")})`; + }).join(""); +} + +const IMAGE_DATA_URL_MARKER = new RegExp(`"${jsonAsciiMarker("data:", true)}[^;,"]+(?:${jsonAsciiMarker(";base64", true)})?${jsonAsciiMarker(",")}`); +const ANTHROPIC_BASE64_MARKER = new RegExp(`"${jsonAsciiMarker("type")}"\\s*:\\s*"${jsonAsciiMarker("base64")}"`); +const GOOGLE_INLINE_DATA_MARKER = new RegExp(`"${jsonAsciiMarker("inlineData")}"\\s*:`); + // #2824: raw-text pre-check — can this body carry indexable image bytes at // ALL? buildIncomingImageIndex pays a full wire→core conversion of the whole // history (twice per request with the post-prepare refresh) only to discover @@ -215,15 +229,16 @@ function coreMsgsOf(result: unknown): CoreMessage[] | undefined { // - anthropic source.type "base64": raw base64 payload, NO data: prefix; // - google inlineData parts (top-level AND nested in functionResponse): // raw base64 .data field, NO data: prefix. -// Keyed markers tolerate JSON whitespace ("type": "base64" vs "type":"base64"). +// JSON string characters may use Unicode escapes (RFC 8259 section 7); match the ASCII +// markers directly without decoding or copying the complete request. export function bodyMightCarryImages(bodyText: string, protocol: WireProtocol): boolean { switch (protocol) { case "anthropic": - return /"data:[^;,"]+(?:;base64)?,/i.test(bodyText) || /"type"\s*:\s*"base64"/.test(bodyText); + return IMAGE_DATA_URL_MARKER.test(bodyText) || ANTHROPIC_BASE64_MARKER.test(bodyText); case "google": - return /"inlineData"\s*:/.test(bodyText); + return GOOGLE_INLINE_DATA_MARKER.test(bodyText); default: - return /"data:[^;,"]+(?:;base64)?,/i.test(bodyText); + return IMAGE_DATA_URL_MARKER.test(bodyText); } } diff --git a/src/loop/core.ts b/src/loop/core.ts index 85af413766..4b66472153 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -1787,7 +1787,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/src/persist.ts b/src/persist.ts index 179db12ec5..4b16a3ebb9 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 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/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/src/sse-util.ts b/src/sse-util.ts index b21ae622fd..9aa7f03a7f 100644 --- a/src/sse-util.ts +++ b/src/sse-util.ts @@ -65,14 +65,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/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/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/src/upstream-proxy.ts b/src/upstream-proxy.ts index 5a26d41bff..4d26784ff8 100644 --- a/src/upstream-proxy.ts +++ b/src/upstream-proxy.ts @@ -214,6 +214,12 @@ export function matchesNoProxy(target: URL, noProxy: string | undefined, options } } if (tokenPort !== undefined && tokenPort !== port) continue; + // URL already canonicalizes the target's IPv6 spelling. Do the same + // for literal entries, including expanded and IPv4-embedded forms. + if (host.includes(":") && net.isIP(token) === 6) { + const literal = targetUrl(`http://[${token}]`); + if (literal) token = literal.hostname.slice(1, -1); + } if (options.windowsBypass && token.includes("*") && wildcardHostMatch(host, token)) return true; const suffix = token.startsWith("*.") ? token.slice(1) : token.startsWith(".") ? token : undefined; if (suffix ? host.endsWith(suffix) || host === suffix.slice(1) : host === token) return true; diff --git a/src/web/mdrender.ts b/src/web/mdrender.ts index 5735e0953f..d414cf5f23 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 @@ -206,7 +207,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/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); + } +}); 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{}"]], + ]); +}); 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/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, []); +}); 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) => { 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) => { 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"]); +}); diff --git a/tests/image-marker-escapes.test.ts b/tests/image-marker-escapes.test.ts new file mode 100644 index 0000000000..a3c53d3252 --- /dev/null +++ b/tests/image-marker-escapes.test.ts @@ -0,0 +1,111 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { createInitialState, defaultConfig } from "acp-kernel"; +import { anthropicToCore, googleToCore, openaiToCore, responsesToCore } from "acp-kernel/wire"; +import { bodyMightCarryImages, buildIncomingImageIndex } from "../src/image-restore.ts"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import type { WireProtocol } from "../src/util.ts"; + +const root = mkdtempSync(join(tmpdir(), "bili-image-markers-")); +process.env.XDG_STATE_HOME = join(root, "state"); +process.env.XDG_DATA_HOME = join(root, "data"); +process.env.XDG_CACHE_HOME = join(root, "cache"); +process.env.XDG_CONFIG_HOME = join(root, "config"); +test.after(() => rmSync(root, { recursive: true, force: true })); + +const PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="; +const dataUrl = `data:image/png;base64,${PNG}`; +const openai = { model: "gpt-fixture", stream: false, messages: [{ role: "user", content: [{ type: "text", text: "Describe this image." }, { type: "image_url", image_url: { url: dataUrl } }] }] }; +const fixtures: Array<{ protocol: WireProtocol; body: unknown; marker: string; escaped: string }> = [ + { protocol: "openai", body: openai, marker: "data:", escaped: "\\u0064ata\\u003a" }, + { protocol: "responses", body: { model: "gpt-fixture", input: [{ type: "message", role: "user", content: [{ type: "input_image", image_url: dataUrl }] }] }, marker: "data:", escaped: "\\u0064ata\\u003A" }, + { protocol: "anthropic", body: { model: "claude-fixture", max_tokens: 10, messages: [{ role: "user", content: [{ type: "image", source: { type: "base64", media_type: "image/png", data: PNG } }] }] }, marker: '"base64"', escaped: '"base\\u00364"' }, + { protocol: "google", body: { contents: [{ role: "user", parts: [{ inlineData: { mimeType: "image/png", data: PNG } }] }] }, marker: "inlineData", escaped: "\\u0069nlineD\\u0061ta" }, +]; +const converters = { anthropic: anthropicToCore, openai: openaiToCore, responses: responsesToCore, google: googleToCore }; + +function escapeStrings(body: unknown): string { + return JSON.stringify(body).replace(/"(?:\\.|[^"\\])*"/g, (token) => { + const value = JSON.parse(token) as string; + return `"${[...value].map((character) => `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`).join("")}"`; + }); +} + +for (const fixture of fixtures) { + for (const escaped of [false, true]) { + test(`${fixture.protocol} image indexing survives ${escaped ? "escaped" : "literal"} JSON markers`, () => { + const plain = JSON.stringify(fixture.body); + const wire = escaped ? plain.replace(fixture.marker, fixture.escaped) : plain; + const parsed = JSON.parse(wire); + assert.deepEqual(parsed, fixture.body); + const converted = (converters[fixture.protocol] as (body: unknown) => unknown)(parsed); + const messages = (Array.isArray(converted) ? converted : (converted as { msgs: unknown[] }).msgs) as Array<{ id: string }>; + const state = createInitialState(); + messages.forEach((message, index) => { state.messageRefs.byRaw[message.id] = `m${String(index + 1).padStart(5, "0")}`; }); + const expected = buildIncomingImageIndex(parsed, fixture.protocol, state, `fixture-${fixture.protocol}`); + assert.equal(expected.size, 1, "the protocol converter exposes actual recoverable image bytes"); + for (const images of expected.values()) assert.deepEqual(readFileSync(images[0]!.path), Buffer.from(PNG, "base64")); + const guarded = bodyMightCarryImages(wire, fixture.protocol) + ? buildIncomingImageIndex(parsed, fixture.protocol, state, `fixture-${fixture.protocol}`) : new Map(); + assert.deepEqual(guarded, expected); + }); + } + test(`${fixture.protocol} escaped text remains an image-free fast path`, () => { + const text = '{"text":"\\u0068ello \\u4e16\\u754c and \\\\u0061 literal"}'; + assert.equal(bodyMightCarryImages(text, fixture.protocol), false); + }); +} + +test("fully escaped JSON keys, media markers, and separators remain detectable", () => { + for (const fixture of fixtures) { + const wire = escapeStrings(fixture.body); + assert.deepEqual(JSON.parse(wire), fixture.body); + assert.equal(bodyMightCarryImages(wire, fixture.protocol), true, fixture.protocol); + } +}); + +test("encoded data URL tokens keep the existing case-insensitive recognition", () => { + const body = { image_url: `DATA:IMAGE/PNG;BASE64,${PNG}` }; + assert.equal(bodyMightCarryImages(JSON.stringify(body), "openai"), true); + assert.equal(bodyMightCarryImages(escapeStrings(body), "openai"), true); +}); + +test("real HTTP preparation archives the same pixels with escaped and literal data URL markers", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const upstream = http.createServer(async (request, response) => { + for await (const _chunk of request) { /* consume the actual forwarded request */ } + response.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ id: "fixture", object: "chat.completion", model: "gpt-fixture", choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], usage: { prompt_tokens: 10, completion_tokens: 1, total_tokens: 11 } })); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const origin = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: origin, routes: { [origin]: { models: { "gpt-fixture": { context: 100_000 } } } }, modelContextLimit: 100_000, kernelConfig: defaultConfig(100_000), compress: { injectTool: true, injectNudge: true }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, passthroughSource: null, autoUpdate: false, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, compat: { roles: {} }, streamErrorShape: "protocol", mitm: { enabled: false, domains: [] } } as ProxyOptions); + if (!proxy.listening) await once(proxy, "listening"); + try { + for (const escaped of [false, true]) { + const label = `image-markers-${escaped}`; + const plain = JSON.stringify(openai); + const wire = escaped ? plain.replace("data:", "\\u0064ata\\u003a") : plain; + const response = await fetch(`http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/${origin}/v1/chat/completions`, { method: "POST", headers: { "content-type": "application/json", "x-acp-session": label }, body: wire, signal: AbortSignal.timeout(10_000) }); + assert.equal(response.status, 200, await response.text()); + const session = listSessions().find((candidate) => candidate.meta.label === label); + assert.ok(session, "the real preparation path tracked the conversation"); + assert.equal(session.incomingImageIndex?.size, 1, label); + const image = [...session.incomingImageIndex!.values()][0]![0]!; + assert.deepEqual(readFileSync(image.path), Buffer.from(PNG, "base64")); + } + } finally { + proxy.closeAllConnections(); upstream.closeAllConnections(); + await Promise.all([new Promise(resolve => proxy.close(() => resolve())), new Promise(resolve => upstream.close(() => resolve()))]); + } +}); 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"); 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/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;

"], + [">