From 360057d1c9cb8632bb02ffa9f4747a021802f7ed Mon Sep 17 00:00:00 2001 From: Zhishu <963336185@qq.com> Date: Thu, 22 Oct 2026 12:47:22 +0800 Subject: [PATCH 01/78] fix(overflow): recognize the DashScope input-length-range 400 so the probe path can arm (#4262) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DashScope compatible-mode rejects an over-long input as "Range of input length should be [1, 1000000]" — no token/length subject phrase — so inspectContextOverflow did not treat it as an overflow: the #2313 probe forward took the 400, armOverflowShrink never armed, and every attempt re-forwarded the identical 16.9MiB payload. A session past the real 1M input cap could only livelock (timer, no reply). Add the marker to CONTEXT_OVERFLOW_PATTERNS and read the upper bound as the window in parseOverflowWindow; the stated range then arms the emergency shrink and the #1195 in-request refold recovers the turn. Tests: unit cases for the real body (window 1000000) and for a parameter-range 400 that must NOT match, plus e2e #2313 e on the probe path (fails before this change with the 400 passed through, passes after). (cherry picked from commit 931a1008d5f0d8dc0ec620d0c487575783e0901b) --- src/util.ts | 8 ++++++ tests/context-overflow.test.ts | 22 ++++++++++++++++ tests/issue2313-probe-forward.test.ts | 38 +++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/src/util.ts b/src/util.ts index 7755425244..dfec17a70a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -398,6 +398,10 @@ const CONTEXT_OVERFLOW_PATTERNS: RegExp[] = [ /输入[^。;;!!\n]{0,12}(?:超出|超过|超限)/, // (f) #3240/#3246 consolidation: mixed-language gateways write "prompt 过长". /prompt[^。;;!!\n]{0,6}(?:过长|太长|超长)/i, + // DashScope compatible-mode states the ceiling as an input-length RANGE + // ("Range of input length should be [1, 1000000]"), with no token or length + // subject phrase: unlisted here, the 400 is not an overflow and nothing arms. + /range of input length should be \[/i, ]; function toTokenNumber(s: string): number | undefined { @@ -463,6 +467,10 @@ function parseOverflowWindow(text: string): number | undefined { // first parenthesized number is the rejected payload and must not be used. m = text.match(/maximum number of tokens allowed\s*\((\d[\d,]*)\)/i); if (m) return toTokenNumber(m[1]); + // DashScope: "Range of input length should be [1, 1000000]" — the upper + // bound is the input ceiling; the lower bound is not a window. + m = text.match(/range of input length should be \[\s*\d[\d,]*\s*,\s*(\d[\d,]*)\s*\]/i); + if (m) return toTokenNumber(m[1]); m = text.match(/maximum context length is (\d[\d,]*)/i) ?? text.match(/maximum context length of (\d[\d,]*)/i) ?? diff --git a/tests/context-overflow.test.ts b/tests/context-overflow.test.ts index c2720a0a0d..8f00d7c89b 100644 --- a/tests/context-overflow.test.ts +++ b/tests/context-overflow.test.ts @@ -168,6 +168,28 @@ test("Gemini 'input token count (N) exceeds' states the input count (#3450)", () assert.equal(info.requestedCompletion, undefined); }); +test("DashScope 'Range of input length should be [1, N]' → overflow + input ceiling", () => { + // The real compatible-mode rejection (no token/length-subject phrase): while + // it went unrecognized, the 400 never armed the emergency shrink, so the + // #2313 probe kept re-forwarding the same over-long payload. + const info = inspectContextOverflow( + 400, + '{"error":{"message":"<400> InternalError.Algo.InvalidParameter: Range of input length should be [1, 1000000]","type":"invalid_request_error","param":null,"code":"invalid_parameter_error"},"id":"chatcmpl-55bbf071-1040-9261-8cd4-76088d25ee5c","request_id":"55bbf071-1040-9261-8cd4-76088d25ee5c"}', + ); + assert.equal(info.isOverflow, true); + assert.equal(info.window, 1000000); + assert.equal(info.requestedMessages, undefined); + assert.equal(info.requestedCompletion, undefined); +}); + +test("a parameter-range 400 without the input-length phrasing is NOT an overflow", () => { + const info = inspectContextOverflow( + 400, + '{"error":{"message":"InvalidParameter: temperature should be in range [0, 2]","code":"invalid_parameter_error"}}', + ); + assert.equal(info.isOverflow, false); +}); + test("overflow body WITHOUT stated counts → no requested* fields (#3450 regression guard)", () => { // The legacy phrasing must keep behaving exactly as before (selfheal T3 // relies on it): window only, no invented counts. diff --git a/tests/issue2313-probe-forward.test.ts b/tests/issue2313-probe-forward.test.ts index 3cdc538dd9..844b8e4c02 100644 --- a/tests/issue2313-probe-forward.test.ts +++ b/tests/issue2313-probe-forward.test.ts @@ -285,3 +285,41 @@ test("e2e #2313 d: probe 400 without a parseable window arms the fallback shrink await close(); } }); + +test("e2e #2313 e: a probe 400 stating only an input-length RANGE arms the shrink — the DashScope livelock recovers", async () => { + // DashScope compatible-mode rejects an over-long input as "Range of input + // length should be [1, 20000]" — no token/length-subject phrase, so while it + // was unrecognized as an overflow the arm never fired, the probe gate + // re-forwarded the same raw body on every attempt and the session could + // only 400 (each turn burning the client's ~300s stream idle). + const rejectBody = JSON.stringify({ + error: { + message: "<400> InternalError.Algo.InvalidParameter: Range of input length should be [1, 20000]", + type: "invalid_request_error", + param: null, + code: "invalid_parameter_error", + }, + id: "chatcmpl-55bbf071-1040-9261-8cd4-76088d25ee5c", + request_id: "55bbf071-1040-9261-8cd4-76088d25ee5c", + }); + const { ctx, calls, close } = await setup(rejectBody); + try { + await seedStuckMeter(ctx, "i2313-e"); + + const r = await post(ctx, bigMessages()); + assert.equal(r.status, 200, "the stated range is an overflow on the probe path, and the in-request refold recovers"); + await r.text(); + + const forwards = calls.filter((c) => !c.summary); + assert.equal(forwards.length, 3, "turn 1 + raw probe forward + one folded retry"); + assert.ok(calls.some((c) => c.summary), "the refold made summarization call(s)"); + assert.ok(forwards[2]!.contentChars < forwards[1]!.contentChars, "the retry carries the folded payload"); + + const s = getSession("i2313-e"); + assert.ok(s); + assert.equal(s.stats.overflowArmTokens, undefined, "the successful retry's usage report retires the one-shot arm (#1110)"); + assert.equal(s.stats.lastInputTokensSource, "usage", "the folded retry settles the final baseline"); + } finally { + await close(); + } +}); From 0861f400cf1bd0c60edb5fa0feec3bea27421d28 Mon Sep 17 00:00:00 2001 From: "V.Matyukha" Date: Fri, 23 Oct 2026 09:26:35 +0300 Subject: [PATCH 02/78] fix(pi): normalize Fleet CRLF and paint truncated rows (cherry picked from commit dac285fd89e792ebb3819259e8a4999345b0e430) --- pi-subagents/src/fleet-inspector.ts | 12 ++-- pi-subagents/tests/fleet-inspector.test.ts | 84 +++++++++++++++++++++- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/pi-subagents/src/fleet-inspector.ts b/pi-subagents/src/fleet-inspector.ts index 44a113fc44..baf47d2d1f 100644 --- a/pi-subagents/src/fleet-inspector.ts +++ b/pi-subagents/src/fleet-inspector.ts @@ -1,6 +1,6 @@ import { openSync, fstatSync, readSync, closeSync } from "node:fs"; import type { ExtensionContext } from "@earendil-works/pi-coding-agent"; -import { Key, Markdown, matchesKey, truncateToWidth, wrapTextWithAnsi, type MarkdownTheme } from "@earendil-works/pi-tui"; +import { Key, Markdown, matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi, type MarkdownTheme } from "@earendil-works/pi-tui"; import type { Component } from "@earendil-works/pi-tui"; import { OUT_DIR, fleetRunsSnapshot, type FleetRunView } from "./delegate-tool.js"; @@ -172,7 +172,7 @@ export function formatToolLabel(name: string, args: unknown): string { case "write": return `write ${s(a.path ?? a.file_path)}`; case "edit": return `edit ${s(a.path ?? a.file_path)}`; case "bash": { - const cmd = s(a.command).replace(/[\n\t]/g, " ").trim(); + const cmd = s(a.command).replace(/\r?\n|\t/g, " ").trim(); return `bash ${cmd.length > 60 ? `${cmd.slice(0, 60)}…` : cmd}`; } case "grep": return `grep /${s(a.pattern)}/ in ${s(a.path ?? ".")}`; @@ -270,8 +270,10 @@ function buildMarkdownTheme(theme: InspectorTheme): MarkdownTheme { export function renderTranscriptBlocks(blocks: TranscriptBlock[], theme: InspectorTheme, innerW: number): string[] { const out: string[] = []; const wrapW = Math.max(10, innerW); - // Full-width background bar; truncateToWidth pads to innerW (ANSI + CJK aware) so the bg spans the row. - const bar = (bgColor: string, content: string): string => theme.bg(bgColor, truncateToWidth(content, innerW, undefined, true)); + const bar = (bgColor: string, content: string): string => { + const clipped = truncateToWidth(content, innerW, theme.bg(bgColor, "...")); + return theme.bg(bgColor, clipped) + theme.bg(bgColor, " ".repeat(Math.max(0, innerW - visibleWidth(clipped)))); + }; let i = 0; while (i < blocks.length) { const b = blocks[i]; @@ -329,7 +331,7 @@ function pushToolGroup(out: string[], bar: (bg: string, c: string) => string, th const label = formatToolLabel(name, args); if (label) out.push(bar(bgc, theme.fg("dim", label))); if (result) { - const rawLines = result.text.replace(/\n+$/, "").split("\n"); + const rawLines = result.text.replace(/\r\n/g, "\n").replace(/\n+$/, "").split("\n"); const shown = rawLines.slice(0, MAX_RESULT_LINES); for (const l of shown) out.push(bar(bgc, theme.fg("toolOutput", l))); if (rawLines.length > shown.length) out.push(bar(bgc, theme.fg("dim", `… +${rawLines.length - shown.length} more`))); diff --git a/pi-subagents/tests/fleet-inspector.test.ts b/pi-subagents/tests/fleet-inspector.test.ts index 2367d4d8bc..2b73bc5769 100644 --- a/pi-subagents/tests/fleet-inspector.test.ts +++ b/pi-subagents/tests/fleet-inspector.test.ts @@ -48,7 +48,7 @@ test("usageSummary hides zero-usage and formats tokens", () => { assert.equal(usageSummary(undefined), undefined); assert.equal(usageSummary({ input: 0, output: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } }), undefined); const s = usageSummary({ input: 1234, output: 56, totalTokens: 1290, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } }); - assert.ok(s!.includes("1,234") && s!.includes("56"), s); + assert.ok(s!.includes((1234).toLocaleString()) && s!.includes("56"), s); }); // ─── readTailSync ─────────────────────────────────────────────────────────── @@ -302,3 +302,85 @@ test("renderTranscriptBlocks renders assistant text as markdown (heading/list)", assert.ok(!lines.some((l) => l.trim().startsWith("#")), "markdown heading marker stripped"); assert.ok(lines.some((l) => l.includes("first point")), "list item present"); }); + +test("transcript rendering treats CRLF as newlines without changing source blocks", () => { + const ansiTheme: InspectorTheme = { + fg: (_c, t) => `\x1b[32m${t}\x1b[39m`, + bold: (t) => `\x1b[1m${t}\x1b[22m`, + bg: (_c, t) => `\x1b[42m${t}\x1b[49m`, + }; + const blocks: TranscriptBlock[] = [ + { kind: "user", text: "first\r\n\r\nsecond\r\n" }, + { kind: "thinking", text: "thinking\r\nnext" }, + { kind: "text", text: "# Heading\r\n\r\nbody" }, + { kind: "toolCall", name: "read", text: "", args: { path: "fixture.txt" } }, + { kind: "toolResult", name: "read", text: "short\r\n123456\r\n\r\ntail\r\n" }, + { kind: "toolCall", name: "bash", text: "", args: { command: "echo first\r\necho second" } }, + { kind: "toolResult", name: "bash", text: " M file.txt\r\n", isError: true }, + ]; + const original = JSON.stringify(blocks); + const lf = blocks.map((b) => ({ ...b, text: b.text.replace(/\r\n/g, "\n") })); + const paintRow = (row: string): string => { + const cells: string[] = []; + let x = 0; + for (const char of row) { + if (char === "\r") x = 0; + else cells[x++] = char; + } + return cells.join(""); + }; + const frame = (row: string, width: number): string => `│ ${row}${" ".repeat(width - visibleWidth(row))} │`; + assert.ok(paintRow(frame("short\r", 20)).lastIndexOf("│") < 23, "negative control: an unnormalized CR shifts the border left and overwrites text"); + for (const width of [16, 40, 80, 120]) { + assert.deepEqual(renderTranscriptBlocks(blocks, ansiTheme, width), renderTranscriptBlocks(lf, ansiTheme, width)); + const lines = renderTranscriptBlocks(blocks, plainTheme, width); + assert.ok(lines.every((line) => !line.includes("\r")), "CR must not move the terminal cursor inside a framed row"); + for (const line of lines) { + const framed = frame(line, width); + assert.equal(paintRow(framed), framed, "padding must not overwrite text or shift the border"); + } + } + assert.equal(JSON.stringify(blocks), original, "stored text and tool arguments remain unchanged"); +}); + + +test("bash command previews treat CRLF as LF without changing arguments", () => { + const args = { command: "echo first\r\necho second\tend" }; + const original = args.command; + assert.equal(formatToolLabel("bash", args), formatToolLabel("bash", { command: "echo first\necho second\tend" })); + assert.ok(!formatToolLabel("bash", args).includes("\r")); + assert.equal(args.command, original); +}); + +test("truncated tool output keeps the generated ellipsis background, including wide graphemes", () => { + const ansiTheme: InspectorTheme = { + fg: (_c, t) => `\x1b[32m${t}\x1b[39m`, + bold: (t) => `\x1b[1m${t}\x1b[22m`, + bg: (_c, t) => `\x1b[42m${t}\x1b[49m`, + }; + function backgroundSpans(line: string): { text: string; painted: boolean }[] { + let painted = false; + const spans: { text: string; painted: boolean }[] = []; + for (const match of line.matchAll(/\x1b\[([\d;]*)m|([^\x1b]+)/g)) { + if (match[1] !== undefined) { + for (const code of match[1].split(";").map(Number)) { + if (code === 0 || code === 49) painted = false; + else if (code === 42) painted = true; + } + } else spans.push({ text: match[2]!, painted }); + } + return spans; + } + for (const width of [1, 2, 3, 10, 21, 40, 80, 120]) { + for (const text of ["x".repeat(200), "界".repeat(200), "👩‍💻é".repeat(200), "\x1b[31m" + "x".repeat(200) + "\x1b[39m"]) { + const lines = renderTranscriptBlocks([{ kind: "toolResult", name: "read", text }], ansiTheme, width); + assertWidthSafe(lines, width); + const line = lines.at(-1)!; + const spans = backgroundSpans(line); + assert.ok(spans.every((s) => s.painted), `default-background notch: ${JSON.stringify(line)}`); + assert.ok(spans.map((s) => s.text).join("").trimEnd().endsWith("."), "truncation remains visible"); + assert.equal(visibleWidth(line), width, "wide-character truncation padding remains painted"); + assert.ok(line.endsWith("\x1b[49m"), "background does not leak out of the row"); + } + } +}); From 666141d7ed5dc568a950a05c7f178c8401cb9992 Mon Sep 17 00:00:00 2001 From: ossa Date: Sun, 25 Oct 2026 13:59:08 +0300 Subject: [PATCH 03/78] fix(window): size a child conversation from its parent's runtime-info (#4682) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Clients mint child conversation ids for work that runs on the parent's own route+model — omp opens idle-recap ephemeral turns as `:side:` (plus a `:side:conversation:` variant) and subagents as `:sub:` — and register runtime-info only under the parent uuid. The window ladder looked the id up literally, so a child missed the report and fell through to the registry peek, where a tier-gated family is capped at the built-in standard 200K without tier evidence (#1321). A session that reported 1M therefore had its own recap turns sized at 200K: preflight compressed ~174K of context toward a 150K headroom target, committed early SSE headers, and the client gave up while the summarizer was still running. pluginRuntimeInfoForConversation now strips one client-minted child suffix at a time and resolves the nearest reporting ancestor (bounded to 4 strips), keeping the model-equality gate at whichever level answers: a child on a different model inherits nothing, and a direct hit is never overridden by a parent. tests/side-window-inherit.test.ts drives the real ladder — a `:side:` child of a 1M-reporting parent budgets 1_000_000 (200_000 before the fix) — and tests/runtime-info.test.ts carries the unit-level cases. Docs: TECHNICAL-NOTES(+zh) records the child-id resolution and the CHANGELOG carries the incident shape. (cherry picked from commit 1fafccf815b55e2deb7180286d2da0f132e42d67) --- src/plugin.ts | 30 ++++- tests/runtime-info.test.ts | 39 +++++++ tests/side-window-inherit.test.ts | 178 ++++++++++++++++++++++++++++++ 3 files changed, 243 insertions(+), 4 deletions(-) create mode 100644 tests/side-window-inherit.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index a4824a39d0..e9c9c1ec83 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -735,13 +735,35 @@ export function pluginRuntimeInfoFor(agent: string | undefined, model: string | return entry; } +/** Clients mint child conversation ids for work that runs on the parent's own + * route+model: omp opens ephemeral turns as `:side:` and + * subagents as `:sub:` (with `:side:conversation:` also + * seen in the wild), and never registers runtime-info under them — the + * window belongs to route+model+tier, so a child resolves up to the nearest + * ancestor that reported one instead of falling to the registry/#1321 cap. */ +const CHILD_CONVERSATION_SUFFIX = /:(?:side(?::conversation)?|sub):[^:]+$/; +const MAX_CHILD_CONVERSATION_DEPTH = 4; + +function parentConversationId(conversationId: string): string | undefined { + const parent = conversationId.replace(CHILD_CONVERSATION_SUFFIX, ""); + return parent !== conversationId && parent.length > 0 ? parent : undefined; +} + /** Latest runtime-info registered under a conversation id, usable for - * `model` only (#1531). */ + * `model` only (#1531). A client-minted child id resolves to its nearest + * reporting ancestor; the model check is applied at whichever level answers, + * so a child on a different model never inherits a stale window. */ export function pluginRuntimeInfoForConversation(conversationId: string | undefined, model: string | undefined): PluginRuntimeInfo | undefined { if (conversationId === undefined || model === undefined) return undefined; - const entry = pluginRuntimeByConversation.get(conversationId); - if (entry === undefined || entry.model !== model) return undefined; - return entry; + const exact = pluginRuntimeByConversation.get(conversationId); + if (exact !== undefined) return exact.model === model ? exact : undefined; + let parent = parentConversationId(conversationId); + for (let depth = 0; parent !== undefined && depth < MAX_CHILD_CONVERSATION_DEPTH; depth++) { + const entry = pluginRuntimeByConversation.get(parent); + if (entry !== undefined) return entry.model === model ? entry : undefined; + parent = parentConversationId(parent); + } + return undefined; } /** The narrow conversation signal a header-less runtime-info lookup can diff --git a/tests/runtime-info.test.ts b/tests/runtime-info.test.ts index ea4ab336e4..5b9a977ebd 100644 --- a/tests/runtime-info.test.ts +++ b/tests/runtime-info.test.ts @@ -546,3 +546,42 @@ describe("runtime-info pre-first-request status (#955)", () => { assert.equal(json.conversationId, "conv-pre-1"); }); }); + +// Client-minted child conversations: a runtime-info lookup for a child id must +// resolve the nearest reporting ancestor (see pluginRuntimeInfoForConversation). +describe("runtime-info: child conversations inherit the parent's report", () => { + beforeEach(() => _resetPluginStateForTest()); + + it("a `:side:` ephemeral turn resolves the parent's window and output cap", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 1_000_000, maxOutput: 128_000, conversationId: "sid-parent", source: "client-config", ts: 1 }); + const hit = pluginRuntimeInfoForConversation("sid-parent:side:15b3f37086a59857", "astra"); + assert.equal(hit?.contextWindow, 1_000_000); + assert.equal(hit?.maxOutput, 128_000); + }); + + it("`:side:conversation:` and `:sub:` ids resolve too, including nested ones", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 262_144, conversationId: "sid-parent", source: "client-config", ts: 1 }); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:conversation:0f3d", "astra")?.contextWindow, 262_144); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:sub:agent-7", "astra")?.contextWindow, 262_144); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:abc:sub:agent-7", "astra")?.contextWindow, 262_144); + }); + + it("the model gate still holds: a child on a different model inherits nothing", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 262_144, conversationId: "sid-parent", source: "client-config", ts: 1 }); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:abc", "gemini"), undefined); + }); + + it("a child that reported its own window keeps it — inheritance never overrides a direct hit", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 262_144, conversationId: "sid-parent", source: "client-config", ts: 1 }); + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 65_536, conversationId: "sid-parent:side:abc", source: "client-config", ts: 2 }); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:abc", "astra")?.contextWindow, 65_536); + }); + + it("no prefix matching: an unrelated id or a marker with no tail stays a miss", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 262_144, conversationId: "sid-parent", source: "client-config", ts: 1 }); + assert.equal(pluginRuntimeInfoForConversation("sid-parent2:side:abc", "astra"), undefined); + assert.equal(pluginRuntimeInfoForConversation("sid-parentx", "astra"), undefined); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:", "astra"), undefined); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:sideways:abc", "astra"), undefined); + }); +}); diff --git a/tests/side-window-inherit.test.ts b/tests/side-window-inherit.test.ts new file mode 100644 index 0000000000..bdeaa59bb9 --- /dev/null +++ b/tests/side-window-inherit.test.ts @@ -0,0 +1,178 @@ +// Clients mint child conversation ids for work that runs on the parent's own +// route+model: omp opens idle-recap ephemeral turns as +// `:side:` and subagents as `:sub:`, and +// registers runtime-info only under the parent uuid. The window ladder looked +// the id up literally, so the child missed the parent's report and fell to the +// registry peek — where a tier-gated family is capped at the built-in standard +// 200K without tier evidence (#1321). A 1M-window host therefore sized its own +// recap turns at 200K, and preflight rewrote/aborted a request the client had +// every right to send. +// +// Fix: pluginRuntimeInfoForConversation strips a client-minted child suffix and +// resolves the nearest reporting ancestor, keeping the model-equality gate at +// whichever level answers. + +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; +import { _resetPluginStateForTest, recordPluginRuntimeInfo } from "../src/plugin.ts"; + +const MODEL = "claude-opus-5-5"; // 200K standard; models.dev advertises 1M +const ONE_M = 1_000_000; +const PARENT = "01a16993-6110-7152-a930-ed7e8b180820"; + +function okSse(): string { + return ( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: 1000 } } })}\n\n` + + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` + + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` + + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` + + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` + + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n` + ); +} + +interface Rig { + proxyPort: number; + upstreamPort: number; + proxy: http.Server; + upstream: http.Server; +} + +async function startRig(): Promise { + const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse()); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + _resetPluginStateForTest(); + // models.dev advertises the MAX tier — the same shape that gets capped at + // the built-in standard window when no tier evidence rides the request. + setRegistryForTest({ [`anthropic/${MODEL}`]: { limit: { context: ONE_M } } }); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: {} }, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + autoRestartOnUpdate: false, + advisoryCheck: false, + releaseNotesCheck: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + updateTag: "latest", + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + return { proxyPort, upstreamPort, proxy, upstream }; +} + +function url(rig: Rig): string { + return `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/messages`; +} + +async function closeRig(rig: Rig): Promise { + rig.proxy.close(); + rig.proxy.closeIdleConnections?.(); + await once(rig.proxy, "close"); + rig.upstream.close(); + rig.upstream.closeIdleConnections?.(); + await once(rig.upstream, "close"); +} + +/** Sends one request under `sessionId` and returns the effective window the + * proxy budgeted for that session. */ +async function effectiveWindow(rig: Rig, sessionId: string, model = MODEL): Promise { + const r = await fetch(url(rig), { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": sessionId }, + body: JSON.stringify({ model, max_tokens: 1024, stream: true, messages: [{ role: "user", content: "hi" }] }), + }); + assert.equal(r.status, 200); + await r.text(); + const limit = listSessions()[0]?.metadata.effectiveContextLimit; + return typeof limit === "number" ? limit : undefined; +} + +function reportParent(conversationId = PARENT, model = MODEL): void { + recordPluginRuntimeInfo({ agent: "omp", model, contextWindow: ONE_M, maxOutput: 128_000, conversationId, source: "client-config", ts: 1 }); +} + +test("e2e: the parent's own request keeps the reported 1M window", async () => { + const rig = await startRig(); + try { + reportParent(); + assert.equal(await effectiveWindow(rig, PARENT), ONE_M); + } finally { + await closeRig(rig); + } +}); + +test("e2e: a `:side:` ephemeral turn inherits the parent's 1M window, not the 200K cap", async () => { + const rig = await startRig(); + try { + reportParent(); + assert.equal(await effectiveWindow(rig, `${PARENT}:side:15b3f37086a59857`), ONE_M, "pre-fix: 200_000 via the registry/#1321 cap"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: `:side:conversation:` and `:sub:` children inherit too", async () => { + const rig = await startRig(); + try { + reportParent(); + assert.equal(await effectiveWindow(rig, `${PARENT}:side:conversation:0f3d`), ONE_M); + assert.equal(await effectiveWindow(rig, `${PARENT}:sub:agent-7`), ONE_M); + } finally { + await closeRig(rig); + } +}); + +test("e2e: the model gate holds at the parent — a child on another model is not sized by it", async () => { + const rig = await startRig(); + try { + reportParent(PARENT, "claude-sonnet-4-5"); + assert.equal(await effectiveWindow(rig, `${PARENT}:side:15b3f37086a59857`), 200_000, "stale window from a different model must not be inherited"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: an unknown parent still falls back to the capped registry window", async () => { + const rig = await startRig(); + try { + reportParent(); + assert.equal(await effectiveWindow(rig, "no-such-parent:side:abc"), 200_000); + assert.equal(await effectiveWindow(rig, `${PARENT}2:side:abc`), 200_000, "no accidental prefix match"); + } finally { + await closeRig(rig); + } +}); From 6c0079b5f0ec5d2c8604731a4e019f93187c8501 Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Sun, 25 Oct 2026 09:52:00 -0700 Subject: [PATCH 04/78] fix: release input-only request data before streaming (#4753) (cherry picked from commit 9c8c72c7810669eed18ac9ad0e6d18617f75b19c) --- src/plugin.ts | 45 +++++++-- src/server.ts | 46 +++++++-- src/server/handle.ts | 24 +++-- src/server/relay.ts | 10 +- tests/plugin-wire-metadata.test.ts | 119 ++++++++++++++++++++++ tests/proxy-readbody.test.ts | 56 +++++++++++ tests/request-input-lifetime.test.ts | 142 +++++++++++++++++++++++++++ 7 files changed, 414 insertions(+), 28 deletions(-) create mode 100644 tests/plugin-wire-metadata.test.ts create mode 100644 tests/request-input-lifetime.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index e9c9c1ec83..d58b627b80 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -528,10 +528,30 @@ export function recordPluginSession(conversationId: string, sessionId: string): scheduleSaveConversations(); } +interface PluginWireMetadata { + wireHash: string; + textComparable: boolean; +} + +/** Capture the raw request's snapshot evidence before prepare mutates its + * parsed view. Only these scalars need to survive forwarding. */ +export function preparePluginWireMetadata(rawWire: Buffer, parsedWire: unknown): PluginWireMetadata { + let textComparable = false; + try { + if (parsedWire !== null && typeof parsedWire === "object") { + const wire = parsedWire as Record; + textComparable = comparableHistory(wire.messages ?? wire.input ?? wire.contents); + } + } catch { + textComparable = false; + } + return { wireHash: createHash("sha256").update(rawWire).digest("hex"), textComparable }; +} + /** Keep the last prepare()'s view for a plugin session so tool-API execution * sees the exact refs the model was shown (mirrors the wire-mode loop, which * runs executeProxyTool against prepared.processedMessages). */ -export function rememberPluginMessages(sessionId: string, processed: CoreMessage[], original: CoreMessage[], nudge?: NudgeDecision, rawWire?: Buffer): void { +export function rememberPluginMessages(sessionId: string, processed: CoreMessage[], original: CoreMessage[], nudge?: NudgeDecision, rawWire?: Buffer | PluginWireMetadata): void { // #1307: auxiliary requests (auto-review / classifier prompts) bound to the // same session key can carry a normal output budget and any message count, // so both the ≤200 heuristic and size-based guards are proxies that a new @@ -652,17 +672,22 @@ export function rememberPluginMessages(sessionId: string, processed: CoreMessage snapshotRefsEvaluatedStore.set(session, session.contentStore); } if (rawWire !== undefined) { - // The comparable verdict is a property of the wire body: hash it - // (native speed) and only re-parse + walk when the body actually - // changed, so a retry with an identical body stays cheap. - const wireHash = createHash("sha256").update(rawWire).digest("hex"); + // The handler captures this evidence before forwarding so it need + // not retain the raw body. Buffer callers keep the hash-gated parse. + const wireHash = Buffer.isBuffer(rawWire) + ? createHash("sha256").update(rawWire).digest("hex") + : rawWire.wireHash; if (session.metadata.publicSnapshotWireHash !== wireHash) { let comparable: boolean; - try { - const wire = JSON.parse(rawWire.toString("utf8")) as Record; - comparable = comparableHistory(wire.messages ?? wire.input ?? wire.contents); - } catch { - comparable = false; + if (Buffer.isBuffer(rawWire)) { + try { + const wire = JSON.parse(rawWire.toString("utf8")) as Record; + comparable = comparableHistory(wire.messages ?? wire.input ?? wire.contents); + } catch { + comparable = false; + } + } else { + comparable = rawWire.textComparable; } session.metadata.publicSnapshotWireHash = wireHash; if (session.metadata.publicSnapshotTextComparable !== comparable) { diff --git a/src/server.ts b/src/server.ts index f06ee81a2b..c0fd2216e5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4811,20 +4811,48 @@ export function readBody(req: http.IncomingMessage): Promise { return new Promise((resolve, reject) => { const chunks: Buffer[] = []; let size = 0; - let aborted = false; - req.on("data", (c: Buffer) => { - if (aborted) return; + let settled = false; + const cleanup = () => { + settled = true; + chunks.length = 0; + req.off("data", onData); + req.off("end", onEnd); + req.off("error", onError); + req.off("close", onClose); + }; + const onError = (error: unknown) => { + if (settled) return; + cleanup(); + reject(error); + }; + const onData = (c: Buffer) => { + if (settled) return; size += c.length; - if (size > knobLimitsReceiveBytes()) { - aborted = true; + const limit = knobLimitsReceiveBytes(); + if (size > limit) { + onError(new BodyTooLargeError(limit)); req.destroy(); - reject(new BodyTooLargeError(knobLimitsReceiveBytes())); return; } chunks.push(c); - }); - req.on("end", () => { if (!aborted) resolve(Buffer.concat(chunks)); }); - req.on("error", (e) => { if (!aborted) reject(e); }); + }; + const onEnd = () => { + if (settled) return; + try { + const body = Buffer.concat(chunks); + cleanup(); + resolve(body); + } catch (error) { + onError(error); + } + }; + // IncomingMessage also closes after a normal end, whose handler has + // already settled and removed this premature-close fallback. + const onClose = () => onError(new Error("request closed before body completed")); + req.on("data", onData); + req.on("end", onEnd); + req.on("error", onError); + req.on("close", onClose); }); } diff --git a/src/server/handle.ts b/src/server/handle.ts index 3c25753341..ad4141aea3 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -56,7 +56,7 @@ import { maybeAdoptForkBlocks, maybeAdoptResume, maybeAdoptDerivedFork } from ". import { executeThreadsReforge } from "./threads-lane.js"; import { resetThreadTailTracking } from "./thread-tracking.js"; import { resolveClaudeTranscriptLineage } from "../claude-transcript-lineage.js"; -import { consumePluginRegisterFor, pluginAgentHeader, pluginCompressionProxyHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; +import { consumePluginRegisterFor, pluginAgentHeader, pluginCompressionProxyHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, preparePluginWireMetadata, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; import { scheduleAffinityPersist } from "../affinity-persist.js"; import { evaluateChain, extractChainCarriers } from "../chain-checkpoint.js"; import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, isLoopbackAddress, normalizeUpstreamOrigin, provenCompletionReserve, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, type WireProtocol } from "../util.js"; @@ -658,6 +658,9 @@ async function handleRequest( parsed = null; } } + // Capture the raw-wire verdict before unwrapping or preparing can mutate + // parsed. Only these scalars are needed for the post-forward bookkeeping. + const rawWireMetadata = protocol ? preparePluginWireMetadata(bodyBuffer, parsed) : undefined; // #3129: CloudCode/Antigravity v1internal dialect — unwrap the outer envelope // so the pipeline sees a standard GoogleRequest, and decide identity. The // inner request.sessionId is a constant FNV-1a hash, so identity comes from @@ -712,6 +715,7 @@ async function handleRequest( // index builds (each one re-converts the full history wire→core). The // predicate over-approximates by design; see bodyMightCarryImages. const bodyMayCarryImages = protocol !== null && bodyText !== undefined ? bodyMightCarryImages(bodyText, protocol) : false; + bodyText = undefined; // #903: inbound message count for the per-request cost line — messages // (anthropic/openai) or input (responses); null when the body has neither. const inboundMsgs: number | null = (() => { @@ -2902,11 +2906,18 @@ async function handleRequest( // #5683: this round's merged three-level compress settings, captured // so the post-prepare gate stamp below can read the policy knobs. let preparedCs: CompressSettings | undefined; - const runPreparedPipeline = async ( + let runPreparedPipeline = async ( respondFailFast: boolean, overflowWindow?: number, ): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => { const runPrepareInner = async (): Promise => { +======= + let runPreparedPipeline: (( + respondFailFast: boolean, + overflowWindow?: number, + ) => Promise<{ body: string | Buffer; prepared: Prepared | null } | null>) | undefined = async (respondFailFast, overflowWindow) => { + const runPrepare = async (): Promise => { +>>>>>>> 9c8c72c78 (fix: release input-only request data before streaming (#4753)) // #1820: this prepare consumes one unit of post-rebuild anchor // validity (the last one deletes it — see session.ts). tickPostRebuildAnchor(session); @@ -3335,7 +3346,7 @@ async function handleRequest( if (guardScheme !== undefined && !resignArmable && !guardResign.enabled) { clearSignedRefusal(guardScheme); } - const pendingForward = await withSessionLock(session, () => runPreparedPipeline(true)); + const pendingForward = await withSessionLock(session, () => runPreparedPipeline!(true)); if (pendingForward) { forwarded = true; // #1195: wire clients (omp/pi on the plain proxy path) treat an @@ -3350,7 +3361,7 @@ async function handleRequest( // original 400 through verbatim. const overflowRefold = pendingForward.prepared ? async (realWindow: number | undefined): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => { - const next = await withSessionLock(session, () => runPreparedPipeline(false, realWindow)); + const next = await withSessionLock(session, () => runPreparedPipeline!(false, realWindow)); if (!next || String(next.body) === String(pendingForward.body)) return null; pendingForward.body = next.body; pendingForward.prepared = next.prepared; @@ -3359,14 +3370,13 @@ async function handleRequest( : undefined; await forward(req, res, opts, pendingForward.body, pendingForward.prepared, core, reqConfig, log, route, instanceId, affinity, overflowRefold, v1Meta, throttleModel, (outbound) => { logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0, outbound); - }); - // Remember for ALL modes (not just plugin): wire clients (dsh, + }); // Remember for ALL modes (not just plugin): wire clients (dsh, // hermes, unplug'd pi) read the same panel via /__bili/plugin/status // and need the nudge/breakdown sections too; locked so a racing // plugin tool call sees a consistent window. if (pendingForward.prepared) { const preparedToRemember = pendingForward.prepared; - await withSessionLock(session, () => rememberPluginMessages(sessionId, preparedToRemember.processedMessages, preparedToRemember.originalMessages, preparedToRemember.nudge, bodyBuffer)); + await withSessionLock(session, () => rememberPluginMessages(sessionId, preparedToRemember.processedMessages, preparedToRemember.originalMessages, preparedToRemember.nudge, rawWireMetadata)); } } } finally { diff --git a/src/server/relay.ts b/src/server/relay.ts index 7e34790cf8..ad5066d91f 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -346,14 +346,14 @@ async function forwardCaptured( v1Meta?: Record, throttleModelHint?: string, onWireBody?: (body?: Buffer | string) => void, + releaseRequestInput?: () => void, ): Promise { const forwardStartedAt = Date.now(); const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session, () => !res.destroyed) : undefined; // E2: a codex native-compaction request intercepted in prepare() carries a // forged success response — serve it without contacting upstream. if (prepared?.codexForge) { - onWireBody?.(); - log("info", `[${prepared.session.id}] codex compact served locally (${prepared.codexForge.kind}); upstream not contacted`); + onWireBody?.(); releaseRequestInput?.(); log("info", `[${prepared.session.id}] codex compact served locally (${prepared.codexForge.kind}); upstream not contacted`); if (!res.headersSent) res.writeHead(200, { "content-type": prepared.codexForge.contentType }); res.end(prepared.codexForge.body); return; @@ -362,6 +362,7 @@ async function forwardCaptured( // zero-egress contract as codexForge; the audit line was written in // prepare (this line is the egress-proof for the serve itself). if (prepared?.autoModeForge) { + releaseRequestInput?.(); log("info", `[${prepared.session.id}] auto-mode classifier served locally (${prepared.autoModeForge.variant}); upstream not contacted`); if (!res.headersSent) res.writeHead(200, { "content-type": prepared.autoModeForge.contentType }); res.end(prepared.autoModeForge.body); @@ -1965,6 +1966,11 @@ async function forwardCaptured( } } } + // No further overflow refold can run. Release its inbound-only state before + // consuming a potentially long response; wireBody still owns retry/echo data. + releaseRequestInput?.(); + releaseRequestInput = undefined; + overflowRefold = undefined; const { response: upstream, clearTimer: clearUpstreamTimer } = upstreamResult; // [#1343] delivery decided: the attached full text rode THIS request's body, so settle // its lifecycle here — delivered on a 2xx, dropped-and-logged otherwise. The ack is diff --git a/tests/plugin-wire-metadata.test.ts b/tests/plugin-wire-metadata.test.ts new file mode 100644 index 0000000000..f1f3906cfe --- /dev/null +++ b/tests/plugin-wire-metadata.test.ts @@ -0,0 +1,119 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; +import type { CoreMessage } from "acp-kernel"; +import { preparePluginWireMetadata, rememberPluginMessages } from "../src/plugin.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetSessionsForTest, getSession } from "../src/session.ts"; + +const messages: CoreMessage[] = [ + { id: "raw-a", role: "user", contentType: "text", text: "first" }, + { id: "raw-b", role: "assistant", contentType: "text", text: "second" }, + { id: "raw-c", role: "user", contentType: "text", text: "third" }, +]; + +test.beforeEach(() => { + _resetSessionsForTest(); + _setStoreForTest(new SessionStore({ dir: join(tmpdir(), "bili-wire-metadata-disabled"), enabled: false })); +}); +test.afterEach(() => _resetSessionsForTest()); + +function pluginSession(id: string) { + const session = getSession(id); + session.metadata.pluginAgent = "codex"; + return session; +} + +test("precomputed wire metadata matches raw-buffer snapshot evidence across wire shapes", () => { + const cases: { wire: unknown; comparable: boolean; raw?: string }[] = [ + { wire: { messages: [{ role: "user", content: "中😀" }] }, comparable: true }, + { wire: { input: [{ type: "message", content: [{ type: "input_text", text: "text" }] }] }, comparable: true }, + { wire: { contents: [{ role: "user", parts: [{ text: "text" }] }] }, comparable: true }, + { wire: { messages: null, input: "fallback" }, comparable: true }, + { wire: { messages: [{ content: [{ type: "image", source: {} }] }], input: "ignored" }, comparable: false }, + { wire: { messages: [{ content: [{ type: "tool_use", input: { type: "image" } }] }] }, comparable: true }, + { wire: { messages: [{ content: [{ type: "thinking", thinking: "signed" }] }] }, comparable: false }, + { wire: { messages: [{ content: "text", audio: {} }] }, comparable: false }, + { wire: null, comparable: false }, + { wire: 42, comparable: false }, + { wire: "text", comparable: false }, + { wire: undefined, raw: "{invalid", comparable: false }, + ]; + for (const [i, { wire, comparable, raw }] of cases.entries()) { + const body = Buffer.from(raw ?? JSON.stringify(wire, null, 2)); + const evidence = preparePluginWireMetadata(body, wire); + assert.deepEqual(evidence, { + wireHash: createHash("sha256").update(body).digest("hex"), + textComparable: comparable, + }); + const legacy = pluginSession(`raw-${i}`); + const prepared = pluginSession(`prepared-${i}`); + rememberPluginMessages(legacy.id, messages, messages, undefined, body); + rememberPluginMessages(prepared.id, messages, messages, undefined, evidence); + assert.equal(prepared.metadata.publicSnapshotWireHash, legacy.metadata.publicSnapshotWireHash); + assert.equal(prepared.metadata.publicSnapshotTextComparable, legacy.metadata.publicSnapshotTextComparable); + assert.equal(prepared.revisionEpoch, legacy.revisionEpoch); + } +}); + +test("preparing wire metadata neither reparses nor commits, and survives input mutation", (t) => { + const session = pluginSession("deferred"); + const wire: Record = { messages: [{ role: "user", content: "original" }] }; + const body = Buffer.from(JSON.stringify(wire)); + const expectedHash = createHash("sha256").update(body).digest("hex"); + const parse = t.mock.method(JSON, "parse", () => { throw new Error("must reuse the parsed request"); }); + const evidence = preparePluginWireMetadata(body, wire); + assert.equal(parse.mock.callCount(), 0); + parse.mock.restore(); + assert.equal(session.metadata.publicSnapshotWireHash, undefined); + assert.equal(session.metadata.publicSnapshotTextComparable, undefined); + assert.equal(session.pluginSnapshot, undefined); + assert.equal(session.revisionEpoch, undefined); + + body.fill(0); + wire.messages = [{ content: [{ type: "image", source: {} }] }]; + rememberPluginMessages(session.id, messages, messages, undefined, evidence); + assert.equal(session.metadata.publicSnapshotWireHash, expectedHash); + assert.equal(session.metadata.publicSnapshotTextComparable, true); +}); + +test("wire evidence preserves hash-gated bookkeeping and rejected-view guards", () => { + const session = pluginSession("bookkeeping"); + const wire = { messages: [{ role: "user", content: "text" }] }; + const evidence = preparePluginWireMetadata(Buffer.from(JSON.stringify(wire)), wire); + rememberPluginMessages(session.id, messages, messages, undefined, evidence); + const revision = session.revisionEpoch; + const snapshot = session.pluginSnapshot; + rememberPluginMessages(session.id, messages, messages, undefined, evidence); + assert.equal(session.revisionEpoch, revision, "identical retries do not mark dirty"); + + const reformatted = preparePluginWireMetadata(Buffer.from(JSON.stringify(wire, null, 2)), wire); + assert.notEqual(reformatted.wireHash, evidence.wireHash, "hash follows exact request bytes"); + rememberPluginMessages(session.id, messages, messages, undefined, reformatted); + assert.equal(session.metadata.publicSnapshotWireHash, reformatted.wireHash); + assert.equal(session.revisionEpoch, revision, "a new hash with the same verdict does not mark dirty"); + + const opaque = { input: [{ type: "input_image", image_url: "data:image/png;base64,AA==" }] }; + const opaqueEvidence = preparePluginWireMetadata(Buffer.from(JSON.stringify(opaque)), opaque); + rememberPluginMessages(session.id, messages, messages, undefined, opaqueEvidence); + assert.equal(session.metadata.publicSnapshotTextComparable, false); + assert.equal(session.revisionEpoch, (revision ?? 0) + 1, "changed comparability marks dirty"); + assert.equal(session.pluginSnapshot, snapshot, "wire evidence does not replace the message snapshot"); + rememberPluginMessages(session.id, messages, messages, undefined, { ...opaqueEvidence, textComparable: true }); + assert.equal(session.metadata.publicSnapshotTextComparable, false, "the saved hash gates verdict changes"); + + const side: CoreMessage[] = [{ id: "unrelated", role: "user", contentType: "text", text: "side request" }]; + rememberPluginMessages(session.id, side, side, undefined, evidence); + assert.equal(session.metadata.publicSnapshotWireHash, opaqueEvidence.wireHash); + assert.equal(session.metadata.publicSnapshotTextComparable, false); + assert.equal(session.revisionEpoch, (revision ?? 0) + 1); + + const nonPlugin = getSession("non-plugin"); + rememberPluginMessages(nonPlugin.id, messages, messages, undefined, evidence); + assert.equal(nonPlugin.metadata.publicSnapshotWireHash, undefined); + const empty = pluginSession("empty"); + rememberPluginMessages(empty.id, [], [], undefined, evidence); + assert.equal(empty.metadata.publicSnapshotWireHash, undefined); +}); diff --git a/tests/proxy-readbody.test.ts b/tests/proxy-readbody.test.ts index 2349ea2b55..6903af5108 100644 --- a/tests/proxy-readbody.test.ts +++ b/tests/proxy-readbody.test.ts @@ -2,6 +2,8 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { Readable } from "node:stream"; import { EventEmitter } from "node:events"; +import { IncomingMessage } from "node:http"; +import { Socket } from "node:net"; import { BodyTooLargeError, readBody } from "../src/server.ts"; import { MAX_REQUEST_BYTES } from "../src/fetch-util.ts"; @@ -19,6 +21,60 @@ test("readBody: returns buffer for a normal request", async () => { assert.equal(out.toString("utf8"), "hello"); }); +for (const outcome of ["success", "error"] as const) { + test(`readBody: releases its request listeners after ${outcome}`, async () => { + const req = new EventEmitter(); + const externalListener = () => {}; + const events = ["data", "end", "error", "close"] as const; + for (const event of events) req.on(event, externalListener); + const pending = readBody(req as never); + req.emit("data", Buffer.from("hello")); + if (outcome === "success") { + req.emit("end"); + assert.equal((await pending).toString("utf8"), "hello"); + } else { + const error = new Error("read failure"); + const rejection = assert.rejects(pending, (actual: unknown) => actual === error); + req.emit("error", error); + await rejection; + } + for (const event of events) { + assert.deepEqual(req.listeners(event), [externalListener], `${event}: readBody must remove only its own listener`); + } + }); +} + +for (const withError of [false, true]) { + test(`readBody: real IncomingMessage cancellation ${withError ? "with" : "without"} an error settles and cleans up`, { timeout: 2000 }, async () => { + const req = new IncomingMessage(new Socket()); + const error = withError ? new Error("client disconnected") : undefined; + const closed = new Promise((resolve) => req.once("close", resolve)); + const pending = readBody(req); + const rejection = assert.rejects(pending, (actual: unknown) => withError ? actual === error : actual instanceof Error); + req.push(Buffer.from("hello")); + setImmediate(() => req.destroy(error)); + await rejection; + await closed; + for (const event of ["data", "end", "error", "close"]) assert.equal(req.listenerCount(event), 0, event); + }); +} + +test("readBody: a real IncomingMessage over the limit keeps BodyTooLargeError through destroy", { timeout: 2000 }, async (t) => { + const previousLimit = process.env.BILI_RECEIVE_BODY_BYTES; + process.env.BILI_RECEIVE_BODY_BYTES = "8"; + t.after(() => { process.env.BILI_RECEIVE_BODY_BYTES = previousLimit; }); + const req = new IncomingMessage(new Socket()); + const closed = new Promise((resolve) => req.once("close", resolve)); + const pending = readBody(req); + const rejection = assert.rejects(pending, (error: unknown) => error instanceof BodyTooLargeError && error.limit === 8); + req.push(Buffer.from("1234")); + req.push(Buffer.from("56789")); + await rejection; + await closed; + assert.equal(req.destroyed, true); + for (const event of ["data", "end", "error", "close"]) assert.equal(req.listenerCount(event), 0, event); +}); + test("readBody: rejects with BodyTooLargeError AND destroys socket when body exceeds MAX_REQUEST_BYTES", async () => { const req = new EventEmitter(); let destroyed = false; diff --git a/tests/request-input-lifetime.test.ts b/tests/request-input-lifetime.test.ts new file mode 100644 index 0000000000..bdc8f8711c --- /dev/null +++ b/tests/request-input-lifetime.test.ts @@ -0,0 +1,142 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { randomUUID } from "node:crypto"; +import test from "node:test"; +import { createCore, defaultConfig, type CoreMessage } from "acp-kernel"; +import type { ProxyOptions } from "../src/config.ts"; +import type { Prepared } from "../src/server.ts"; +import { forward } from "../src/server/relay.ts"; +import { getSession } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _liveUpstreamTimersForTest, _resetFetchUtilForTest } from "../src/fetch-util.ts"; + +process.env.NODE_ENV = "test"; +_setStoreForTest(new SessionStore({ enabled: false })); + +const OVERFLOW = JSON.stringify({ error: { type: "invalid_request_error", message: "prompt is too long: 130000 tokens > 128000 maximum" } }); +const RETRY_REJECTION = JSON.stringify({ error: { type: "invalid_request_error", message: "refold retry rejected" } }); +function deferred() { + let resolve!: (value: T) => void; + let reject!: (error: unknown) => void; + const promise = new Promise((yes, no) => { resolve = yes; reject = no; }); + return { promise, resolve, reject }; +} + +for (const scenario of ["success", "refold", "declined", "throwing", "rejected"] as const) { + test(`request input release boundary: ${scenario}`, { timeout: 10_000 }, async t => { + const url = "http://request-lifetime.test/responses"; + const session = getSession(`input-lifetime-${randomUUID()}`); + const core = createCore(); + const config = defaultConfig(200_000); + const messages: CoreMessage[] = [{ id: "m1", role: "user", contentType: "text", text: "Continue." }]; + const body = JSON.stringify({ model: "test-model", stream: true, input: [{ role: "user", content: "Continue." }] }); + const prepared: Prepared = { + body, session, processedMessages: messages, originalMessages: messages, + protocol: "responses", stream: true, compressInjected: false, + }; + const opts: ProxyOptions = { + host: "127.0.0.1", port: 0, upstream: url, routes: {}, + proxy: "", proxyMode: "direct", proxySource: "direct", + modelContextLimit: 200_000, kernelConfig: config, + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, compat: { roles: {} }, + streamErrorShape: "protocol", sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, passthroughSource: null, + autoUpdate: false, autoRestartOnUpdate: false, updateTag: "latest", + advisoryCheck: false, releaseNotesCheck: false, mitm: { enabled: false, domains: [] }, + }; + const released = deferred(); + const completed = deferred(); + const trace: string[] = []; + let releaseCount = 0; + let forwardCompleted = false; + let upstreamCalls = 0; + let streamController: ReadableStreamDefaultController | undefined; + const nativeFetch = globalThis.fetch; + t.mock.method(globalThis, "fetch", async (_input: unknown, init?: RequestInit) => { + upstreamCalls++; + trace.push(`fetch:${upstreamCalls}`); + assert.equal(releaseCount, 0, "inbound reconstruction must remain available until the retry is accepted"); + if (scenario !== "success" && upstreamCalls === 1) { + return new Response(OVERFLOW, { status: 400, headers: { "content-type": "application/json" } }); + } + assert.equal(init?.body, upstreamCalls === 1 ? body : `${body} `, "the accepted request keeps its outgoing bytes"); + if (scenario === "rejected") { + return new Response(RETRY_REJECTION, { status: 422, headers: { "content-type": "application/json" } }); + } + return new Response(new ReadableStream({ start(controller) { streamController = controller; } }), { + headers: { "content-type": "text/event-stream" }, + }); + }); + const server = http.createServer((req, res) => { + req.resume(); + req.url = url; + void forward(req, res, opts, body, prepared, core, config, () => {}, undefined, "input-lifetime", + undefined, + async () => { + trace.push("refold"); + assert.equal(releaseCount, 0, "the refold callback still owns original input"); + if (scenario === "declined") return null; + if (scenario === "throwing") throw new Error("cannot refold"); + return { body: `${body} `, prepared: { ...prepared, body: `${body} ` } }; + }, + undefined, + () => { releaseCount++; trace.push("release"); released.resolve(); }, + ).then(() => { forwardCompleted = true; completed.resolve(); }, error => { res.destroy(); completed.reject(error); }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address === "object"); + const client = nativeFetch(`http://127.0.0.1:${address.port}`, { method: "POST", body }); + // Attach rejection handlers immediately; cleanup can abort either side. + void client.catch(() => {}); + void completed.promise.catch(() => {}); + let deadline: ReturnType | undefined; + try { + if (scenario === "success" || scenario === "refold") { + await Promise.race([ + released.promise, + completed.promise.then(() => assert.fail("forward finished before input release")), + new Promise((_, reject) => { deadline = setTimeout(() => reject(new Error("input retained while accepted response stream is pending")), 5000); }), + ]); + assert.equal(forwardCompleted, false); + assert.equal(releaseCount, 1); + assert.ok(streamController, "upstream stream remains pending at the release boundary"); + streamController.enqueue(new TextEncoder().encode(`event: response.completed\ndata: ${JSON.stringify({ type: "response.completed", response: { id: "r1", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "done" }] }] } })}\n\n`)); + streamController.close(); + streamController = undefined; + } + const response = await client; + const responseText = await response.text(); + await completed.promise; + assert.equal(releaseCount, 1, "release runs exactly once, including error responses"); + if (scenario === "declined" || scenario === "throwing") { + assert.equal(response.status, 400); + assert.equal(responseText, OVERFLOW); + } else if (scenario === "rejected") { + assert.equal(response.status, 422); + assert.equal(responseText, RETRY_REJECTION); + } else { + assert.equal(response.status, 200); + assert.ok(responseText.includes("done")); + } + const expectsRetry = scenario === "refold" || scenario === "rejected"; + assert.equal(upstreamCalls, expectsRetry ? 2 : 1); + assert.deepEqual(trace, scenario === "success" ? ["fetch:1", "release"] + : expectsRetry ? ["fetch:1", "refold", "fetch:2", "release"] + : ["fetch:1", "refold", "release"]); + } finally { + if (deadline) clearTimeout(deadline); + streamController?.close(); + const closed = once(server, "close"); + server.close(); + server.closeAllConnections(); + await closed; + await completed.promise.catch(() => {}); + assert.equal(_liveUpstreamTimersForTest(), 0); + _resetFetchUtilForTest(); + } + }); +} From 4355ee22bf3874a614380d3565dfbe5a5fde7ec0 Mon Sep 17 00:00:00 2001 From: yanhuojiyusheng <2398249916@qq.com> Date: Mon, 26 Oct 2026 22:06:18 +0800 Subject: [PATCH 05/78] fix(handoff): persist a completed reply when the client hangs up MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The handoff snapshot was gated on the client socket still being writable (relay passed canSave=() => !res.destroyed, and both pipes required !res.destroyed before reply.save()). Clients that close the connection the instant they hold the terminal frame — pi does exactly this — therefore lost every assistant reply: the pipe threw 'This operation was aborted' after sawTerminal=true, took the client-abort branch and returned without saving. Persistence now answers 'did the upstream turn finish?', not 'is the socket still open?': the pipes save on sawTerminal/replyCompleted even from the client-abort path, and the relay no longer couples canSave to res.destroyed. Partial turns (no terminal) are still never captured. Refs #4247 (cherry picked from commit 63e13bec8dd9d88638a5373d9f9a0072ca16194d) --- src/plugin.ts | 16 ++- src/server/relay.ts | 2 +- tests/handoff-client-abort.test.ts | 187 +++++++++++++++++++++++++++++ tests/handoff-response.test.ts | 2 +- 4 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 tests/handoff-client-abort.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index d58b627b80..634b4f807b 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3956,7 +3956,11 @@ export async function pipePluginChatWithStrip( maybeWarnNamelessToolCalls(); maybeSummarizeStrips(); settleWitnesses(); - if (sawTerminal && !sawReplyError && !res.destroyed && !res.writableEnded) reply?.save(); + // #4247-followup: persistence answers "did the upstream turn finish?", + // never "is the client socket still open?". pi's client hangs up the + // moment it holds the terminal frame, so gating this save on client + // liveness dropped every pi reply from the handoff render. + if (sawTerminal && !sawReplyError) reply?.save(); if (truncated) { reply?.discard(); // #721 → #2563: the client still owns the stream from here. @@ -3974,8 +3978,12 @@ export async function pipePluginChatWithStrip( settleWitnesses(); if (res.destroyed || res.writableEnded) { log?.("client aborted mid-stream"); +<<<<<<< HEAD // #5293: the partial output of an aborted turn never reports usage — record the pre-abort anchor so the first nudge decision after resume can absorb that debt. if (session) noteAbortedTurn(session); + // A terminal already arrived before the client left: the reply is + // complete, so it still belongs in the handoff snapshot. + if (sawTerminal && !sawReplyError) reply?.save(); return; } // #721: upstream read failed while the client is still connected — @@ -4994,7 +5002,8 @@ export async function pipePluginResponsesWithStrip( maybeSummarizeStrips(); settleUsage(); settleWitnesses(); - if (replyCompleted && !res.destroyed && !res.writableEnded) reply?.save(); + // Same rule as the chat pipe: upstream completion decides, not the socket. + if (replyCompleted) reply?.save(); // #721 → #2563: never leave the stream hanging on a missing done-family // event. Permitlist hosts (pi) get the raw cut for their native // classifier; everyone else gets the in-band error frame. @@ -5009,8 +5018,11 @@ export async function pipePluginResponsesWithStrip( settleWitnesses(); if (res.destroyed || res.writableEnded) { log?.("client aborted mid-stream"); +<<<<<<< HEAD // #5293: same as the chat lane — record the pre-abort anchor for the post-resume re-anchor. if (session) noteAbortedTurn(session); + // Completed turn, dead socket: keep the reply for the handoff render. + if (replyCompleted) reply?.save(); return; } // #721: upstream read failed while the client is still connected — diff --git a/src/server/relay.ts b/src/server/relay.ts index ad5066d91f..e3aac0cd74 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -349,7 +349,7 @@ async function forwardCaptured( releaseRequestInput?: () => void, ): Promise { const forwardStartedAt = Date.now(); - const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session, () => !res.destroyed) : undefined; + const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session) : undefined; // E2: a codex native-compaction request intercepted in prepare() carries a // forged success response — serve it without contacting upstream. if (prepared?.codexForge) { diff --git a/tests/handoff-client-abort.test.ts b/tests/handoff-client-abort.test.ts new file mode 100644 index 0000000000..6565092b9c --- /dev/null +++ b/tests/handoff-client-abort.test.ts @@ -0,0 +1,187 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; + +// #4247 followup: agents that close the socket the instant they hold the +// terminal frame (pi does) used to lose the reply entirely — the handoff save +// was gated on the client socket still being writable. These tests pin the +// rule: a completed upstream turn is persisted even when the client is gone. + +function sse(data: unknown): string { + return `data: ${JSON.stringify(data)}\n\n`; +} + +function roleChunk(): string { + return sse({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt-test", choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }], usage: { prompt_tokens: 55, completion_tokens: 0, total_tokens: 55 } }); +} + +function textChunk(text: string): string { + return sse({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt-test", choices: [{ index: 0, delta: { content: text }, finish_reason: null }] }); +} + +function terminalChunk(reason = "stop"): string { + return sse({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt-test", choices: [{ index: 0, delta: {}, finish_reason: reason }] }); +} + +interface Harness { + proxyPort: number; + upstreamPort: number; + close(): Promise; +} + +/** Upstream that writes `head` and then holds the stream open — the real + * slow-model shape: the client has its terminal frame while the connection + * is still alive. */ +async function startHarness(head: string[], opts: { hold?: boolean; doneAfterMs?: number } = {}): Promise { + const upstream = http.createServer((req, res) => { + req.on("data", () => {}); + req.on("end", () => { + res.writeHead(200, { "content-type": "text/event-stream" }); + for (const line of head) res.write(line); + if (opts.doneAfterMs !== undefined) { + // [DONE] lands after the client has had time to hang up, so the + // pipe's write of that frame hits a dead socket. + setTimeout(() => { if (!res.destroyed) res.write("data: [DONE]\n\n"); if (!res.destroyed) res.end(); }, opts.doneAfterMs); + } else if (!opts.hold) res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetPluginStateForTest(); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + return { + proxyPort, + upstreamPort, + close: async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeAllConnections?.(); + await once(upstream, "close"); + }, + }; +} + +function request(h: Harness, conv: string, body: Record): http.ClientRequest { + return http.request({ + host: "127.0.0.1", + port: h.proxyPort, + path: `/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "pi", "x-bili-plugin-conversation": conv }, + }); +} + +async function handoffMd(h: Harness, label: string): Promise { + const stats = (await (await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/stats`)).json()) as { sessions: Array<{ id: string; label?: string }> }; + const id = stats.sessions.find((s) => s.label === label)?.id; + assert.ok(id, `session ${label} not registered`); + const detail = (await (await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/sessions/${id}/detail`)).json()) as { handoffMd: string }; + return detail.handoffMd; +} + +function payload(stream: boolean): Record { + return { + model: "gpt-test", + max_tokens: 1024, + stream, + messages: [{ role: "user", content: "hello" }], + tools: [{ type: "function", function: { name: "read_file", description: "r", parameters: { type: "object", properties: {} } } }], + }; +} + +test("keeps the reply when the client closes right after the terminal frame", async () => { + // Upstream delivers the whole turn and then holds the connection open, so + // the pipe is still reading when the client hangs up — the shape pi leaves. + const h = await startHarness([roleChunk(), textChunk("final answer"), terminalChunk(), "data: [DONE]\n\n"], { hold: true }); + try { + const req = request(h, "handoff-abort-after-terminal", payload(true)); + let seen = ""; + req.on("response", (res) => { + res.on("data", (c: Buffer) => { + seen += c.toString("utf8"); + if (seen.includes("data: [DONE]") && !req.destroyed) { + req.destroy(); + res.destroy(); + } + }); + }); + req.on("error", () => {}); + req.end(JSON.stringify(payload(true))); + const deadline = Date.now() + 5_000; + while (Date.now() < deadline && !seen.includes("data: [DONE]")) await new Promise((r) => setTimeout(r, 25)); + assert.match(seen, /final answer/); + await new Promise((r) => setTimeout(r, 500)); + // give the store/render a beat — the metadata write itself is synchronous + const md = await handoffMd(h, "handoff-abort-after-terminal"); + assert.match(md, /### assistant/); + assert.match(md, /final answer/); + } finally { + await h.close(); + } +}); + +test("does not persist a reply the upstream never terminated", async () => { + const h = await startHarness([roleChunk(), textChunk("half an ans")]); + try { + const req = request(h, "handoff-abort-mid-stream", payload(true)); + let seen = ""; + req.on("response", (res) => { + res.on("data", (c: Buffer) => { + seen += c.toString("utf8"); + if (seen.includes("half an ans") && !req.destroyed) { + req.destroy(); + res.destroy(); + } + }); + }); + req.on("error", () => {}); + req.end(JSON.stringify(payload(true))); + const deadline = Date.now() + 5_000; + while (Date.now() < deadline && !seen.includes("half an ans")) await new Promise((r) => setTimeout(r, 25)); + await new Promise((r) => setTimeout(r, 500)); + const md = await handoffMd(h, "handoff-abort-mid-stream"); + assert.doesNotMatch(md, /half an ans/); + } finally { + await h.close(); + } +}); diff --git a/tests/handoff-response.test.ts b/tests/handoff-response.test.ts index d299cde36e..0beee977ad 100644 --- a/tests/handoff-response.test.ts +++ b/tests/handoff-response.test.ts @@ -13,7 +13,7 @@ async function serve(protocol: string, stream: boolean, chunks: Array { - const reply = opts.capture === false ? undefined : createHandoffReply(session, () => !opts.abort && !res.destroyed); + const reply = opts.capture === false ? undefined : createHandoffReply(session); const input = new ReadableStream({ start(controller) { for (const chunk of chunks) controller.enqueue(Buffer.from(chunk)); if (opts.error) controller.enqueue(Buffer.from(event({ type: "error", error: { message: "failed" } }))); From ad09789f5226406006866307e192fa0db2f7c0a7 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 13 Nov 2026 10:31:29 +0800 Subject: [PATCH 06/78] fix(handoff): drop leftover conflict markers (merge slip) --- src/plugin.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 634b4f807b..d1757278c2 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3978,7 +3978,6 @@ export async function pipePluginChatWithStrip( settleWitnesses(); if (res.destroyed || res.writableEnded) { log?.("client aborted mid-stream"); -<<<<<<< HEAD // #5293: the partial output of an aborted turn never reports usage — record the pre-abort anchor so the first nudge decision after resume can absorb that debt. if (session) noteAbortedTurn(session); // A terminal already arrived before the client left: the reply is @@ -5018,7 +5017,6 @@ export async function pipePluginResponsesWithStrip( settleWitnesses(); if (res.destroyed || res.writableEnded) { log?.("client aborted mid-stream"); -<<<<<<< HEAD // #5293: same as the chat lane — record the pre-abort anchor for the post-resume re-anchor. if (session) noteAbortedTurn(session); // Completed turn, dead socket: keep the reply for the handoff render. From 6706e619b43ad60a07a2131ad81e4992dfddcf2c Mon Sep 17 00:00:00 2001 From: Jonathan Shin Date: Sun, 25 Oct 2026 16:09:58 -0700 Subject: [PATCH 07/78] fix: preserve changed user instructions during reconciliation (cherry picked from commit c9a81ddde98900a27f28910fb90a92addce3010f) --- src/fold-reconcile.ts | 3 + .../issue2915-order-centric-reconcile.test.ts | 60 +++++++++++++++---- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index 2475a0c657..c62383587c 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1394,6 +1394,8 @@ export function planReconciliation( const anchor = anchors[gap.oldId]; const message = byId.get(gap.newId); if (anchor === undefined || message === undefined) continue; + // Changed user instructions must reach the model, even between unchanged neighbors. + if (anchor.r === "user" && anchor.c !== "image") continue; if (anchor.r !== undefined && anchor.r !== message.role) continue; const len = message.text?.length ?? 0; if (Math.abs(len - anchor.b) > Math.max(256, anchor.b >> 2)) continue; @@ -1450,6 +1452,7 @@ export function planReconciliation( const residualMiddle = missingMiddle.filter((id) => !plan.claims.has(id)); if (residualMiddle.length >= SHAPE_MASS_MIN) { const looseProse = (anchor: FoldAnchor, m: CoreMessage): boolean => { + if (anchor.r === "user" && anchor.c !== "image") return false; if (anchor.wh === undefined || anchor.sh === undefined) return false; if (anchor.r !== undefined && anchor.r !== m.role) return false; const len = m.text?.length ?? 0; diff --git a/tests/issue2915-order-centric-reconcile.test.ts b/tests/issue2915-order-centric-reconcile.test.ts index 46c24a54ea..ded2be064e 100644 --- a/tests/issue2915-order-centric-reconcile.test.ts +++ b/tests/issue2915-order-centric-reconcile.test.ts @@ -28,6 +28,7 @@ // again (streak reset clears the latch) — upgrade-in-place recovery. import { test, describe } from "node:test"; import assert from "node:assert/strict"; +import { prune } from "acp-kernel"; import { simhashHex, hammingHex } from "../src/affinity-simhash.ts"; import { normalizedIdentity, @@ -42,7 +43,7 @@ import { } from "../src/fold-reconcile.ts"; import type { CoreMessage } from "acp-kernel"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; -import type { Session } from "../src/session.ts"; +import { createSession, type Session } from "../src/session.ts"; function msg(id: string, role: string, text: string, extra?: Partial): CoreMessage { return { id, role, contentType: "text", text, ...extra } as CoreMessage; @@ -116,6 +117,43 @@ function planFor(old: CoreMessage[], next: CoreMessage[], positions?: { ids: str const storedOf = (msgs: CoreMessage[]): { ids: string[]; canon: string[] } => ({ ids: msgs.map((m) => m.id as string), canon: msgs.map((m) => positionalFingerprint(m)) }); +for (const [name, editedIndexes, before, after] of [ + ["changed approval", [5], "Deployment is APPROVED.", "Deployment is FORBIDDEN."], + ["reordered instructions", [5], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], + ["several reordered instructions", [5, 7, 9, 11], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], +] satisfies Array<[string, number[], string, string]>) { + test(`outgoing history retains ${name} after message ids change`, () => { + const originals = Array.from({ length: 30 }, (_, i): CoreMessage => msg( + `original-${i}`, i % 2 === 0 ? "assistant" : "user", + `Historical context for step ${i}. `.repeat(100) + (editedIndexes.includes(i) ? before : ""), + )); + const covered = Array.from({ length: 10 }, (_, i) => `original-${i + 3}`); + const session = createSession(`instruction-edit-${name}`); + session.state.blocks.push({ + blockId: "instruction-block", runId: "instruction-run", tier: 1, + summary: before, directMessageIds: [...covered], effectiveMessageIds: [...covered], + directBlockIds: [], compressedTokens: 4000, createdAt: 1, + survivedCount: 0, generation: "young", active: true, + }); + reconcileFoldCoverage(session, originals, opts()); + const resent = originals.map((m, i): CoreMessage => ({ + ...m, id: `resent-${i}`, + text: editedIndexes.includes(i) ? (m.text ?? "").replace(before, after) : m.text, + })); + resent.push(msg("continue", "user", "Continue the current task.")); + reconcileFoldCoverage(session, resent, opts()); + const outgoing = prune(resent, session.state); + for (const i of editedIndexes) { + assert.ok(outgoing.some((m) => m.id === `resent-${i}` && m.text?.includes(after)), + `the corrected instruction at ${i} must reach the model`); + } + assert.ok(outgoing.some((m) => m.id === "acp_summary_instruction-block"), + "unchanged history remains compressed"); + assert.ok(!outgoing.some((m) => m.id === "resent-4"), + "an unchanged covered message stays folded"); + }); +} + describe("Pass 0 order-centric resync (#2915)", () => { test("#2821 replay: full id re-mint + changed preamble + appended turn — order carries, one resync, no mass loss", () => { const old = history(); @@ -130,21 +168,17 @@ describe("Pass 0 order-centric resync (#2915)", () => { assert.equal(plan.byTwin, 0, "positional coverage leaves nothing for the twin pass"); }); - test("replacement island mid-history: resync claims both flanks, the bracket tier claims the slot (owner rule)", () => { + test("replacement user message stays unmatched while resync preserves both flanks", () => { const old = history(); const next = remint(old, "g1"); next[5] = msg("g1_5", "user", "EDITED: content genuinely changed"); // index 5 island (m3, prose) const plan = planFor(old, next, storedOf(old)); - // the island content is neither normalized-equal nor simhash-close - // (JSON vs prose ≈ 30 bits), but it sits at the SAME INDEX between - // verified claims — owner rule: flanks match → the fold asserts the - // slot, not the stale bytes. assert.ok(plan.resyncs >= 1, `expected resyncs, got ${plan.resyncs}`); assert.equal(plan.byPos, 11); - assert.equal(plan.byOrder, 1, "the bracket tier claims the island slot"); - assert.deepEqual(plan.unmatched, [], "no island loss"); + assert.equal(plan.byOrder, 0, "neighboring messages cannot authorize hiding a user edit"); + assert.deepEqual(plan.unmatched, ["m3"], "the changed user message re-enters the wire"); assert.deepEqual(plan.claims.get("end"), ["g1_11"], "the tail span past the island keeps coverage"); - assert.deepEqual(plan.claims.get("m3"), ["g1_5"]); + assert.equal(plan.claims.has("m3"), false); }); test("insertion (net offset) still lands on the flank scans — resync never mispairs shifted cells", () => { @@ -323,15 +357,15 @@ describe("gap resolution tiers (owner rule 2026-10-12: any channel → flanks assert.deepEqual(plan.claims.get("m4"), ["g1_6"]); }); - test("delete + insert at the same index: the bracket claims the slot (owner-sanctioned stale-cell tradeoff)", () => { + test("delete + insert of a user message at the same index leaves the replacement visible", () => { const old = history(); const next = remint(old, "g1"); next[5] = msg("g1_5", "user", "a genuinely different replacement turn"); // m3 replaced in place const plan = planFor(old, next, storedOf(old)); assert.equal(plan.byPos, 11); - assert.equal(plan.byOrder, 1, "same index, verified claims on BOTH sides → assert the slot"); - assert.deepEqual(plan.unmatched, []); - assert.deepEqual(plan.claims.get("m3"), ["g1_5"], "coverage rides the slot — documented tradeoff, not a guess"); + assert.equal(plan.byOrder, 0); + assert.deepEqual(plan.unmatched, ["m3"]); + assert.equal(plan.claims.has("m3"), false, "the old summary does not cover a new user instruction"); }); }); From d26b8d2708a4a2d5d28484da01ab6c4cd970cab0 Mon Sep 17 00:00:00 2001 From: Jonathan Shin Date: Sun, 25 Oct 2026 16:21:02 -0700 Subject: [PATCH 08/78] test: address instruction-preservation review findings (cherry picked from commit cd3510b584ffca02a23173b684795ad48045f11a) --- src/fold-reconcile.ts | 8 ++++---- tests/issue2915-order-centric-reconcile.test.ts | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index c62383587c..5cf9322fb4 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1362,7 +1362,7 @@ export function planReconciliation( // byTwin / byNorm / byTurn / byImage) run BELOW Pass 0, so by the time // this tier sees a gap cell every exact channel has already had its // bite — what remains is tiered corroboration, in order of evidence - // strength: + // strength. User-role messages stay on the exact passes above: // (a) simhash: a re-rendered cell (formatting churn normalization // still misses — key reordering beyond canonical-Json's reach, // template re-stamps) keeps its word-shingle mass, so a 64-bit @@ -1395,7 +1395,7 @@ export function planReconciliation( const message = byId.get(gap.newId); if (anchor === undefined || message === undefined) continue; // Changed user instructions must reach the model, even between unchanged neighbors. - if (anchor.r === "user" && anchor.c !== "image") continue; + if (anchor.r === "user") continue; if (anchor.r !== undefined && anchor.r !== message.role) continue; const len = message.text?.length ?? 0; if (Math.abs(len - anchor.b) > Math.max(256, anchor.b >> 2)) continue; @@ -1441,7 +1441,7 @@ export function planReconciliation( // the words still on the wire. Doctrine (order carries, content gates, // the id is only a hint): when a MISSING MASSES of cells still carry // word evidence, pair them in order instead of diagnosing destruction. - // prose anchors: wordbag equality + simhash ≤ GAP bar (the same + // non-user prose anchors: wordbag equality + simhash ≤ GAP bar (the same // edit-honesty gate as the sim tier) — role-checked, length-guarded; // textless anchors (image families, no sh/wh by construction): k-th // family pairing, only when the anchor/candidate counts per @@ -1452,7 +1452,7 @@ export function planReconciliation( const residualMiddle = missingMiddle.filter((id) => !plan.claims.has(id)); if (residualMiddle.length >= SHAPE_MASS_MIN) { const looseProse = (anchor: FoldAnchor, m: CoreMessage): boolean => { - if (anchor.r === "user" && anchor.c !== "image") return false; + if (anchor.r === "user") return false; if (anchor.wh === undefined || anchor.sh === undefined) return false; if (anchor.r !== undefined && anchor.r !== m.role) return false; const len = m.text?.length ?? 0; diff --git a/tests/issue2915-order-centric-reconcile.test.ts b/tests/issue2915-order-centric-reconcile.test.ts index ded2be064e..45057cbff2 100644 --- a/tests/issue2915-order-centric-reconcile.test.ts +++ b/tests/issue2915-order-centric-reconcile.test.ts @@ -149,8 +149,10 @@ for (const [name, editedIndexes, before, after] of [ } assert.ok(outgoing.some((m) => m.id === "acp_summary_instruction-block"), "unchanged history remains compressed"); - assert.ok(!outgoing.some((m) => m.id === "resent-4"), - "an unchanged covered message stays folded"); + for (const i of [3, 4]) { + assert.ok(!outgoing.some((m) => m.id === `resent-${i}`), + "unchanged covered user and assistant messages stay folded"); + } }); } From d0fcba7a2637bb56f32c131725437b1a9a2719fa Mon Sep 17 00:00:00 2001 From: Jonathan Shin Date: Tue, 27 Oct 2026 07:17:41 -0700 Subject: [PATCH 09/78] fix(reconcile): recover exact user text after line wrapping Restore formatting-only mass re-anchoring by requiring the unwrapped candidate to match the stored normalized identity. Keep user messages out of approximate matching and preserve changed punctuation and instruction order. Pin the non-user order-bracket path and quiet folded resends. Implementation drafted by Grok 4.6; design, review and verification by Codex. (cherry picked from commit c5279e632e6e83d4344d7d897138010080270640) --- src/fold-reconcile.ts | 12 ++++-- .../issue2915-order-centric-reconcile.test.ts | 38 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index 5cf9322fb4..7082c02f19 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1441,7 +1441,9 @@ export function planReconciliation( // the words still on the wire. Doctrine (order carries, content gates, // the id is only a hint): when a MISSING MASSES of cells still carry // word evidence, pair them in order instead of diagnosing destruction. - // non-user prose anchors: wordbag equality + simhash ≤ GAP bar (the same + // text user anchors: exact normalizedIdentity after newline→space unwrap + // (line wrap that reconstructs the stored identity; never wordbag/simhash); + // other prose anchors: wordbag equality + simhash ≤ GAP bar (the same // edit-honesty gate as the sim tier) — role-checked, length-guarded; // textless anchors (image families, no sh/wh by construction): k-th // family pairing, only when the anchor/candidate counts per @@ -1452,11 +1454,15 @@ export function planReconciliation( const residualMiddle = missingMiddle.filter((id) => !plan.claims.has(id)); if (residualMiddle.length >= SHAPE_MASS_MIN) { const looseProse = (anchor: FoldAnchor, m: CoreMessage): boolean => { - if (anchor.r === "user") return false; - if (anchor.wh === undefined || anchor.sh === undefined) return false; if (anchor.r !== undefined && anchor.r !== m.role) return false; const len = m.text?.length ?? 0; if (Math.abs(len - anchor.b) > Math.max(256, anchor.b >> 2)) return false; + if (anchor.r === "user") { + if (anchor.c !== "text" || m.contentType !== "text") return false; + const unwrapped: CoreMessage = { ...m, text: (m.text ?? "").replace(/\r?\n/g, " ") }; + return normalizedIdentity(unwrapped) === anchor.n; + } + if (anchor.wh === undefined || anchor.sh === undefined) return false; const wh = wordBagDigest(canonicalTextOf(m.text ?? "")); if (wh !== anchor.wh) return false; const dist = hammingHex(anchor.sh, simhashHex(canonicalTextOf(m.text ?? ""))); diff --git a/tests/issue2915-order-centric-reconcile.test.ts b/tests/issue2915-order-centric-reconcile.test.ts index 45057cbff2..7b81cc5a3f 100644 --- a/tests/issue2915-order-centric-reconcile.test.ts +++ b/tests/issue2915-order-centric-reconcile.test.ts @@ -121,6 +121,7 @@ for (const [name, editedIndexes, before, after] of [ ["changed approval", [5], "Deployment is APPROVED.", "Deployment is FORBIDDEN."], ["reordered instructions", [5], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], ["several reordered instructions", [5, 7, 9, 11], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], + ["several punctuation edits", [5, 7, 9, 11], "Allow Alice; deny Bob.", "Allow Alice? deny Bob."], ] satisfies Array<[string, number[], string, string]>) { test(`outgoing history retains ${name} after message ids change`, () => { const originals = Array.from({ length: 30 }, (_, i): CoreMessage => msg( @@ -157,6 +158,34 @@ for (const [name, editedIndexes, before, after] of [ } describe("Pass 0 order-centric resync (#2915)", () => { + test("line-wrapped user history stays folded and the next resend is quiet", () => { + const originals = Array.from({ length: 30 }, (_, i): CoreMessage => msg( + `original-${i}`, i % 2 === 0 ? "assistant" : "user", + `Historical context for step ${i}. `.repeat(100), + )); + const covered = originals.slice(3, 13).map((m) => m.id!); + const session = createSession("instruction-line-wrap"); + session.state.blocks.push({ + blockId: "wrapped-block", runId: "wrapped-run", tier: 1, + summary: "Historical steps.", directMessageIds: [...covered], effectiveMessageIds: [...covered], + directBlockIds: [], compressedTokens: 4000, createdAt: 1, + survivedCount: 0, generation: "young", active: true, + }); + reconcileFoldCoverage(session, originals, opts()); + const reshaped = originals.map((m, i): CoreMessage => ({ + ...m, id: `wrapped-${i}`, text: (m.text ?? "").replace(/ /g, "\n"), + })); + reconcileFoldCoverage(session, reshaped, opts()); + const outgoing = prune(reshaped, session.state); + for (let i = 3; i < 13; i++) { + assert.ok(!outgoing.some((m) => m.id === `wrapped-${i}`), "unchanged user and assistant content stays folded"); + } + const logs: string[] = []; + reconcileFoldCoverage(session, reshaped, opts((_level, text) => logs.push(text))); + assert.ok(!logs.some((text) => text.includes("drifted") || text.includes("destroyed")), + "an unchanged resend must not keep reporting drift"); + }); + test("#2821 replay: full id re-mint + changed preamble + appended turn — order carries, one resync, no mass loss", () => { const old = history(); const next = remint(old, "g1", true, "brand new request turn"); @@ -337,13 +366,8 @@ describe("gap resolution tiers (owner rule 2026-10-12: any channel → flanks "The quick brown fox jumps over the lazy dog near the river bank at dawn while birds chirp"); const plan = planFor(old, next, storedOf(old)); assert.equal(plan.bySim, 0, "a word change is never swallowed by the simhash tier"); - // The bracket tier (same index, fenced between verified flanks — the - // owner-sanctioned delete+insert tradeoff, #2821) may still claim it: - // order evidence, not content similarity, is the carrier there. - const claimed = plan.byOrder === 1; - assert.ok(claimed || plan.unmatched.includes("m4"), - "falls through to the bracket tier or honest unmatched — never bySim"); - if (claimed) assert.deepEqual(plan.claims.get("m4"), ["g1_7"]); + assert.equal(plan.byOrder, 1, "verified flanks pin the non-user slot to the order bracket"); + assert.deepEqual(plan.claims.get("m4"), ["g1_7"]); }); test("mid-column deletion (#2915 review gap ③): flanks absorb the shift, the deleted id stays honestly unmatched", () => { From 8aee68526e232b1473d66861fc0d010e47342bff Mon Sep 17 00:00:00 2001 From: Aldo Adirajasa Fathoni Date: Tue, 27 Oct 2026 13:53:48 +0700 Subject: [PATCH 10/78] fix(loop): retry stalled bare-announcement terminal turns (#3905) (cherry picked from commit c43dd22e490333a5feca8dbd6a9744c1a8184c03) --- src/degenerate-turn.ts | 20 +++ src/loop/core.ts | 53 ++++++- src/plugin.ts | 39 +++-- tests/issue3905-announcement-tail.test.ts | 179 ++++++++++++++++++++++ 4 files changed, 270 insertions(+), 21 deletions(-) create mode 100644 tests/issue3905-announcement-tail.test.ts diff --git a/src/degenerate-turn.ts b/src/degenerate-turn.ts index 2092f58f32..7bb11d922b 100644 --- a/src/degenerate-turn.ts +++ b/src/degenerate-turn.ts @@ -72,6 +72,26 @@ export function endsWithDraftClose(text: string): boolean { return text.length > 0 && DRAFT_CLOSE_TAIL.test(text); } +// #3905: the announcement-tail shape. Unlike #2303 there is no draft tag: the +// ENTIRE visible output of the turn is a one-line promise of the next action +// ("Running verify after formatting:") that ends on a colon or ellipsis, with +// no tool call and a clean stop. The upstream reports a healthy turn, the host +// gets nothing actionable, and the session stalls until the user types +// something. Observed verbatim at 24 output tokens / 32 visible chars behind a +// deepseek-v4.1-flash combo member. +// +// The character cap is the whole precision boundary, not a performance guard: +// a genuine answer that happens to end on a colon is prose, and prose is long. +// 400 chars sits an order of magnitude above the observed stub and well below +// any real answer tail. +const ANNOUNCE_TAIL = /(?::|\u2026|\.\.\.)\s*$/; +const ANNOUNCE_MAX_CHARS = 400; +export function endsWithAnnouncement(text: string): boolean { + const t = text.trimEnd(); + if (t.length === 0 || t.length > ANNOUNCE_MAX_CHARS) return false; + return ANNOUNCE_TAIL.test(t); +} + // #2612: the #2303 draft-tail shape has a legitimate producer — the CLIENT's // own summarization request. Claude Code's compaction (/compact, auto-compact, // precomputed compression) instructs the model, in the final user message, to diff --git a/src/loop/core.ts b/src/loop/core.ts index b896377feb..47a457e4e0 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -48,7 +48,7 @@ import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoBody } from "../st import { log as loggerLog } from "../logger.js"; import { promptInputTotal, type WireProtocol } from "../util.js"; import { DEGENERATE_RETRY_NUDGE } from "../degenerate-retry.js"; -import { endsWithDraftClose, lastUserSummaryInstruction, requestExpectsProseSummary } from "../degenerate-turn.js"; +import { endsWithAnnouncement, endsWithDraftClose, lastUserSummaryInstruction, requestExpectsProseSummary } from "../degenerate-turn.js"; import { safePrefix, safeSuffix, scrubLoneSurrogateEscapesOnJson } from "../text-safe.js"; export const MAX_LOOP_ROUNDS = 10; @@ -267,6 +267,13 @@ export interface LoopCtx { * rebuilt history. Default (undefined) keeps markers on. Paired * tool-call/tool-result messages are unaffected. */ visibilityMarkers?: boolean; + /** #3906: when `false`, keep the 📦/❌ ACP receipt in rebuilt history (and in + * the round-N+1 system message the model sees) but stop emitting it into + * the client's content stream. Independent of `visibilityMarkers`: this is + * the cosmetic-only switch for clients that merge the marker into the + * assistant's single text part, which makes the answer render above the + * reasoning that produced it. Default (undefined) keeps streaming. */ + streamMarkers?: boolean; /** #1455: tee loop-originated upstream responses (re-request and every * retry fetch) to raw SSE files — the outer request's ACP_DUMP_SSE tee * only covers the FIRST response, so an internal re-request that dies @@ -1268,6 +1275,18 @@ export async function* runCompressLoop( } } + // #2303 / #3905: classify the terminal tail shape. Both shapes are + // non-converged handoffs — visible prose with no tool call — and share + // the budget, the openai/anthropic framing boundary and the #2612 + // exemption below. + const stalledTail: "draft" | "announce" | null = + assistantText.length === 0 || calls.length > 0 + ? null + : endsWithDraftClose(assistantText) + ? "draft" + : endsWithAnnouncement(assistantText) + ? "announce" + : null; // #2303: a terminal turn that DID deliver visible prose but ends with a // compression-draft closing tag (summary/analysis close forms) and no // tool call — the model wrote a handoff/compression draft in prose @@ -1293,7 +1312,7 @@ export async function* runCompressLoop( !ctx.textProtocol && assistantText.length > 0 && calls.length === 0 && - endsWithDraftClose(assistantText) && + (stalledTail === "draft" || stalledTail === "announce") && // #2612: the client's own compaction request instructs the // model to answer with a plain-text summary block and no // tool call — that compliant reply is a draft tail by @@ -1303,8 +1322,13 @@ export async function* runCompressLoop( !signal?.aborted ) { degenerateRetried = true; - ctx.log(`[acp-loop] round ${round}: terminal turn ends with a compression-draft closing tag and no tool call; retrying once with continuation nudge (#2303)`); - loggerLog("warn", `[acp-loop] draft-tail auto-retry round ${round} (session ${ctx.session.id})`); + if (stalledTail === "announce") { + ctx.log(`[acp-loop] round ${round}: terminal turn is a bare announcement (ends mid-promise, no tool call); retrying once with continuation nudge (#3905)`); + loggerLog("warn", `[acp-loop] announcement-tail auto-retry round ${round} (session ${ctx.session.id})`); + } else { + ctx.log(`[acp-loop] round ${round}: terminal turn ends with a compression-draft closing tag and no tool call; retrying once with continuation nudge (#2303)`); + loggerLog("warn", `[acp-loop] draft-tail auto-retry round ${round} (session ${ctx.session.id})`); + } const nudge: CoreMessage = { id: `acp_draft_tail_retry_r${round}`, role: "user", @@ -1324,11 +1348,11 @@ export async function* runCompressLoop( continue; } catch (e) { if (e instanceof UpstreamHttpError) { - ctx.log(`[acp-loop] round ${round}: draft-tail retry failed (upstream error ${e.status}: ${e.body.slice(0, 200)}); passing the turn through`); + ctx.log(`[acp-loop] round ${round}: stalled-tail retry failed (upstream error ${e.status}: ${e.body.slice(0, 200)}); passing the turn through`); } else { - ctx.log(`[acp-loop] round ${round}: draft-tail retry failed (${e instanceof Error ? e.message : String(e)}); passing the turn through`); + ctx.log(`[acp-loop] round ${round}: stalled-tail retry failed (${e instanceof Error ? e.message : String(e)}); passing the turn through`); } - loggerLog("warn", `[acp-loop] draft-tail auto-retry failed round ${round}: ${e instanceof Error ? e.message : String(e)}`); + loggerLog("warn", `[acp-loop] stalled-tail auto-retry failed round ${round}: ${e instanceof Error ? e.message : String(e)}`); } } break; @@ -1424,7 +1448,20 @@ export async function* runCompressLoop( ctx.log(`[acp-loop] suppressed duplicate ${call.name} status marker (identical failure repeated this request)`); } else { seenMarkers.add(markerKey); - yield adapter.emitMarker(call.name, result.text); + // #3906: streamMarkers=false keeps the receipt in the + // rebuilt history (and in the round-N+1 system message + // below) while keeping it OUT of the client's content + // stream. The marker is an assistant-prose delta emitted + // before the continuation re-request, so on a client that + // keys one text part per assistant message (opencode's + // openai-compatible transform uses a fixed txt-0) it opens + // the text part and the round-2 answer merges into it while + // round-2 reasoning opens a NEW part after it — the answer + // renders above the thinking that produced it and the turn + // appears to end mid-thought. + if (ctx.streamMarkers !== false) yield adapter.emitMarker(call.name, result.text); + // The model-side recovery receipt keeps the marker line even when + // the client stream is withheld. streamedMarkers.push(buildVisibilityMarker(call.name, result.text)); } } diff --git a/src/plugin.ts b/src/plugin.ts index d1757278c2..67cc7e9307 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -59,7 +59,7 @@ function tailLooksTerminal(tail: string, protocol: WireProtocol | "responses"): return /event: response\.(?:completed|failed|incomplete)/.test(tail.slice(0, 64)) || /"type"\s*:\s*"response\.(?:completed|failed|incomplete)"/.test(tail); } -import { degenerateTurnWarning, endsWithDraftClose, requestExpectsProseSummary } from "./degenerate-turn.js"; +import { degenerateTurnWarning, endsWithAnnouncement, endsWithDraftClose, requestExpectsProseSummary } from "./degenerate-turn.js"; import { getChangelog, latestChangelog, warmChangelogOnce } from "./web/changelog.js"; import { warmBlogRefreshOnce } from "./web/blog-refresh.js"; import { PANEL_BOX_FOOTER } from "./acp-panel.js"; @@ -2967,6 +2967,12 @@ export async function pipePluginChatWithStrip( // answer — a client compaction asking for a plain-text block // must not be retried (its compliant reply IS a draft tail). const draftTail = visibleTextChars > 0 && !sawToolUse && endsWithDraftClose(proseAcc) && !requestExpectsProseSummary(requestText); + // #3905: the same non-convergence without a draft tag — the whole visible + // output is a one-line promise of the next action ("Running verify after + // formatting:"). Without this branch the answerChars gate below would read + // that promise as a delivered answer and refuse the retry. + const announceTail = !draftTail && visibleTextChars > 0 && !sawToolUse && endsWithAnnouncement(proseAcc) && !requestExpectsProseSummary(requestText); + const stalledTail = draftTail || announceTail; // #2756: the gate asks "did the host receive ANSWER text?" — prose that // is nothing but whitespace (a bare "\n\n" from a degenerate stop, the // B-accident shape) is not an answer; the empty-turn retry must fire for @@ -2974,9 +2980,9 @@ export async function pipePluginChatWithStrip( // chars are already excluded here), and real text keeps refusing the // retry byte-for-byte, so a delivered answer is never duplicated. const answerChars = answerProse.trim().length; - if (!draftTail && (answerChars > 0 || sawToolUse)) return false; + if (!stalledTail && (answerChars > 0 || sawToolUse)) return false; if (reason === undefined || !CLEAN_TURN_REASONS.has(reason)) { - if (!sawToolUse && !draftTail && answerChars === 0) log?.(`[plugin] degenerate terminal turn not retried (finish ${reason ?? "none"} is not a clean stop): visibleTextChars=${visibleTextChars}, releasedMarkupChars=${releasedMarkupChars} (#2756)`); + if (!sawToolUse && !stalledTail && answerChars === 0) log?.(`[plugin] degenerate terminal turn not retried (finish ${reason ?? "none"} is not a clean stop): visibleTextChars=${visibleTextChars}, releasedMarkupChars=${releasedMarkupChars} (#2756)`); return false; } if (res.destroyed || res.writableEnded) return false; @@ -3002,14 +3008,16 @@ export async function pipePluginChatWithStrip( // re-issuing it double-bills an empty completion (#732/#821 keep the // same boundary in the compress loop). A draft-tail turn is NOT bare: // it delivered a full handoff draft, which is precisely the stall signal. - if (!sawThinking && !sawStrippedEcho && releasedMarkupChars === 0 && !draftTail) { + if (!sawThinking && !sawStrippedEcho && releasedMarkupChars === 0 && !stalledTail) { log?.(`[plugin] degenerate terminal turn not retried (bare: no thinking, no stripped echo, no released markup): visibleTextChars=${visibleTextChars}, releasedMarkupChars=${releasedMarkupChars} (#2756)`); return false; } degenerateRetried = true; - log?.(draftTail - ? "[plugin] terminal turn ends with a compression-draft closing tag and no tool call; retrying once with a continuation nudge (#2303)" - : "[plugin] degenerate terminal turn (no usable output); retrying once with a continuation nudge (#732/#821)"); + log?.(announceTail + ? "[plugin] terminal turn is a bare announcement (ends mid-promise, no tool call); retrying once with a continuation nudge (#3905)" + : draftTail + ? "[plugin] terminal turn ends with a compression-draft closing tag and no tool call; retrying once with a continuation nudge (#2303)" + : "[plugin] degenerate terminal turn (no usable output); retrying once with a continuation nudge (#732/#821)"); let next: ReadableStream | null = null; try { next = await refetch(); @@ -4640,6 +4648,9 @@ export async function pipePluginResponsesWithStrip( // #2612: except when the request itself demands that shape (client // compaction summary instruction) — see the chat-pipe twin. const draftTail = (visibleTextChars > 0 || heldVisibleChars > 0) && !sawFunctionCall && endsWithDraftClose(proseAcc) && !requestExpectsProseSummary(requestText); + // #3905: bare-announcement twin — see the chat-pipe twin above. + const announceTail = !draftTail && (visibleTextChars > 0 || heldVisibleChars > 0) && !sawFunctionCall && endsWithAnnouncement(proseAcc) && !requestExpectsProseSummary(requestText); + const stalledTail = draftTail || announceTail; // #2756: same question as the chat-pipe twin — "did the host receive // ANSWER text?". Reasoning inlined into item.content[] (vLLM/ // llama.cpp reasoning_text) and whitespace-only output_text (a bare @@ -4647,9 +4658,9 @@ export async function pipePluginResponsesWithStrip( // bare check below, but whitespace-only answer prose must also retry. // Real answer text still refuses the retry byte-for-byte. const answerChars = answerProse.trim().length; - if (!draftTail && (answerChars > 0 || sawFunctionCall)) return false; + if (!stalledTail && (answerChars > 0 || sawFunctionCall)) return false; if (status !== "completed") { - if (!sawFunctionCall && !draftTail && answerChars === 0) log?.(`[plugin] degenerate terminal turn not retried (status ${status ?? "none"} is not completed): visibleTextChars=${visibleTextChars}, heldVisibleChars=${heldVisibleChars} (#2756)`); + if (!sawFunctionCall && !stalledTail && answerChars === 0) log?.(`[plugin] degenerate terminal turn not retried (status ${status ?? "none"} is not completed): visibleTextChars=${visibleTextChars}, heldVisibleChars=${heldVisibleChars} (#2756)`); return false; } if (res.destroyed || res.writableEnded) return false; @@ -4658,14 +4669,16 @@ export async function pipePluginResponsesWithStrip( // double-bills an empty completion (#732/#821 keep the same boundary in // the compress loop). A draft-tail turn delivered a full handoff draft: // precisely the stall signal. - if (!sawReasoning && !sawStrippedEcho && !draftTail) { + if (!sawReasoning && !sawStrippedEcho && !stalledTail) { log?.(`[plugin] degenerate terminal turn not retried (bare: no reasoning, no stripped echo): visibleTextChars=${visibleTextChars}, heldVisibleChars=${heldVisibleChars} (#2756)`); return false; } degenerateRetried = true; - log?.(draftTail - ? "[plugin] terminal turn ends with a compression-draft closing tag and no function call; retrying once with a continuation nudge (#2303)" - : "[plugin] degenerate terminal turn (no visible output); retrying once with a continuation nudge (#732/#821)"); + log?.(announceTail + ? "[plugin] terminal turn is a bare announcement (ends mid-promise, no function call); retrying once with a continuation nudge (#3905)" + : draftTail + ? "[plugin] terminal turn ends with a compression-draft closing tag and no function call; retrying once with a continuation nudge (#2303)" + : "[plugin] degenerate terminal turn (no visible output); retrying once with a continuation nudge (#732/#821)"); let next: ReadableStream | null = null; try { next = await refetch(); diff --git a/tests/issue3905-announcement-tail.test.ts b/tests/issue3905-announcement-tail.test.ts new file mode 100644 index 0000000000..f3e93216cf --- /dev/null +++ b/tests/issue3905-announcement-tail.test.ts @@ -0,0 +1,179 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState } from "acp-kernel"; +import { pipePluginChatWithStrip, pipePluginResponsesWithStrip } from "../src/plugin.ts"; +import { endsWithAnnouncement, endsWithDraftClose } from "../src/degenerate-turn.ts"; +import type { Session } from "../src/session.ts"; +import { runCompressLoop, createOpenaiAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +// #2303: a terminal turn whose visible prose ends with a compression-draft +// closing tag ( / ) and no tool call used to read as a +// healthy completed turn — the model wrote a handoff/compression draft in prose +// instead of issuing the action it described (149 silent stops / 70 sessions, +// DSH native). All three terminal-verdict lanes must now treat it as non- +// converged and spend their one-shot continuation nudge on it: the plugin chat +// pipe, the plugin responses pipe, and the compress loop. + +// ---------- helper ---------- + +test("endsWithAnnouncement: the production stub, its ellipsis forms, and the prose boundary", () => { + // Verbatim production stub (ses_eb6da0d60ffeYpnNQDoGz000Pc, 2026-10-19 11:21:43Z, + // 24 output tokens / 32 visible chars, finish=stop, no tool call). + assert.equal(endsWithAnnouncement("\nRunning verify after formatting:"), true); + assert.equal(endsWithAnnouncement("Running verify after formatting:"), true); + assert.equal(endsWithAnnouncement("Now I'll check the three suites:"), true); + assert.equal(endsWithAnnouncement("Let me look at that output\u2026"), true, "unicode ellipsis"); + assert.equal(endsWithAnnouncement("Let me look at that output..."), true, "ascii ellipsis"); + assert.equal(endsWithAnnouncement("Running verify after formatting: \r\n"), true, "trailing whitespace"); +}); + +test("endsWithAnnouncement: rejects everything that is not a stub", () => { + assert.equal(endsWithAnnouncement(""), false); + assert.equal(endsWithAnnouncement(" \n\t "), false, "whitespace-only is the empty-turn shape, not this one"); + assert.equal(endsWithAnnouncement("All done."), false, "a normal sentence end is not a promise"); + assert.equal(endsWithAnnouncement("Deleted."), false); + assert.equal(endsWithAnnouncement("The answer is 42:"), true, "documented cost: a very short answer ending on a colon is indistinguishable from a stub"); + // The 400-char cap is the entire recall/precision boundary: a long answer + // that happens to end on a colon is prose. + assert.equal(endsWithAnnouncement("x".repeat(399) + ":"), true, "exactly 400 chars is still inside the cap"); + assert.equal(endsWithAnnouncement("x".repeat(400) + ":"), false, "401 chars is past the cap"); + assert.equal(endsWithAnnouncement("y".repeat(400)), false, "no promise tail at all"); +}); + +test("endsWithAnnouncement: disjoint from endsWithDraftClose", () => { + assert.equal(endsWithAnnouncement("handoff draft\n"), false, "#2303 owns the draft-close shape"); + assert.equal(endsWithDraftClose("Running verify after formatting:"), false, "#3905 owns the announcement shape"); +}); + +// ---------- compress loop (proxy mode) ---------- + +function makeCtx(id: string, protocol: "responses" | "openai" = "openai") { + return { + core: createCore(), + config: { modelContextLimit: 200000 } as Config, + messages: [] as CoreMessage[], + session: { + id, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + } as unknown as Session, + log: () => {}, + protocol, + }; +} + +function openaiSse(frames: Array>): string { + return frames.map((f) => `data: ${JSON.stringify(f)}\n\n`).join("") + "data: [DONE]\n\n"; +} + +const ANNOUNCE_STUB = "Running verify after formatting:"; + +const OPENAI_ANNOUNCE = openaiSse([ + { id: "chatcmpl_a1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_a1", choices: [{ index: 0, delta: { content: ANNOUNCE_STUB }, finish_reason: null }] }, + { id: "chatcmpl_a1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, +]); + +const OPENAI_ANSWER_END_COLON = openaiSse([ + { id: "chatcmpl_n1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_n1", choices: [{ index: 0, delta: { content: "Here is the full explanation of the three failing suites:" }, finish_reason: null }] }, + { id: "chatcmpl_n1", choices: [{ index: 0, delta: { content: " 1) aaaa 2) bbbb 3) cccc and the remainder of a genuinely long answer that keeps going well past the cap" }, finish_reason: null }] }, + { id: "chatcmpl_n1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, +]); + +const OPENAI_GOOD = openaiSse([ + { id: "chatcmpl_g1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_g1", choices: [{ index: 0, delta: { content: "continued after nudge" }, finish_reason: null }] }, + { id: "chatcmpl_g1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, +]); + +async function drainOpenai(first: string, retries: string[], id: string): Promise<{ out: string; fetchCalls: number; bodies: string[] }> { + let fetchCalls = 0; + const bodies: string[] = []; + const orig = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: { body?: unknown }) => { + fetchCalls++; + if (init?.body !== undefined) bodies.push(typeof init.body === "string" ? init.body : String(init.body)); + const body = retries[fetchCalls - 1] ?? ""; + return new Response(body, { status: 200 }); + }) as typeof fetch; + const chunks: Buffer[] = []; + try { + const ctx = makeCtx(id, "openai"); + for await (const chunk of runCompressLoop( + new Response(first, { status: 200 }).body!, + ctx, + { model: "deepseek-chat", stream: true }, + { url: "http://mock", headers: {} }, + createOpenaiAdapter({ model: "deepseek-chat", stream: true }), + buildCompressSystemPrompt(), + )) { + chunks.push(chunk); + } + } finally { + globalThis.fetch = orig; + } + return { out: Buffer.concat(chunks).toString("utf8"), fetchCalls, bodies }; +} + +test("#3905 loop: a bare announcement → one invisible retry with the continuation nudge", async () => { + const { out, fetchCalls, bodies } = await drainOpenai(OPENAI_ANNOUNCE, [OPENAI_GOOD], "at-l1"); + assert.equal(fetchCalls, 1, "exactly one announcement-tail auto-retry"); + assert.ok(bodies[0]!.includes("ended without a tool call"), "the retry carries the continuation nudge"); + assert.ok(out.includes(ANNOUNCE_STUB), "the stub itself reached the client (the host already rendered it)"); + assert.ok(out.includes("continued after nudge"), "the retried turn's content was delivered"); +}); + +test("#3905 loop: one-shot bound — an announcement RETRY is not retried again", async () => { + const { fetchCalls } = await drainOpenai(OPENAI_ANNOUNCE, [OPENAI_ANNOUNCE], "at-l2"); + assert.equal(fetchCalls, 1, "one retry max; a second announcement falls through to the plain completion"); +}); + +test("#3905 loop: a long answer ending on a colon is NOT retried", async () => { + const { fetchCalls } = await drainOpenai(OPENAI_ANSWER_END_COLON, [OPENAI_GOOD], "at-l3"); + assert.equal(fetchCalls, 0, "the 400-char cap keeps real prose out of the retry"); +}); + +test("#3905 loop: a normal completed turn is NOT retried", async () => { + const normal = openaiSse([ + { id: "chatcmpl_z1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_z1", choices: [{ index: 0, delta: { content: "all steps finished, nothing pending" }, finish_reason: null }] }, + { id: "chatcmpl_z1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, + ]); + const { fetchCalls } = await drainOpenai(normal, [OPENAI_GOOD], "at-l4"); + assert.equal(fetchCalls, 0); +}); + +test("#3905 loop: an announcement WITH a tool call is NOT retried", async () => { + const withCall = openaiSse([ + { id: "chatcmpl_t1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_t1", choices: [{ index: 0, delta: { content: ANNOUNCE_STUB }, finish_reason: null }] }, + { id: "chatcmpl_t1", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "bash", arguments: "{}" } }] }, finish_reason: null }] }, + { id: "chatcmpl_t1", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }, + ]); + const { fetchCalls } = await drainOpenai(withCall, [OPENAI_GOOD], "at-l5"); + assert.equal(fetchCalls, 0, "a tool call means the step converges"); +}); + +test("#3905 loop: a length-cut announcement retries like the #2303 sibling", async () => { + const lengthCut = openaiSse([ + { id: "chatcmpl_l1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_l1", choices: [{ index: 0, delta: { content: ANNOUNCE_STUB }, finish_reason: null }] }, + { id: "chatcmpl_l1", choices: [{ index: 0, delta: {}, finish_reason: "length" }] }, + ]); + const { fetchCalls } = await drainOpenai(lengthCut, [OPENAI_GOOD], "at-l6"); + // Verified against the shipped #2303 implementation: a draft tail ending on + // finish=length is retried there too (fetchCalls === 1). The loop's gate + // excludes only failed/incomplete/error, so `length` is in scope for both + // shapes — matching the sibling rather than inventing a stricter rule. + assert.equal(fetchCalls, 1, "matches the #2303 sibling's treatment of finish=length"); +}); From 872d07ea9cb115f41593b56ee65425e6db63595b Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 16:25:07 +0800 Subject: [PATCH 11/78] =?UTF-8?q?refactor(loop):=20strip=20#3906=20streamM?= =?UTF-8?q?arkers=20hunks=20=E2=80=94=20owned=20by=20PR=20#4934?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Align-refactor (review of #4935): this PR is scoped to the bare-announcement terminal-turn retry (#3905 symptom), but it also carried the LoopCtx streamMarkers field and the emitMarker consumption gate that belong to the streamMarkers setting feature. That feature has its own PR (#4934, which wires the config surface but lacks these two hunks) — keeping them here makes between two independent features. The hunks move to #4934 so each PR is a closed loop. (cherry picked from commit fa60f8f7f57fcf65153d20f52f8c8108370af5c9) --- src/loop/core.ts | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/loop/core.ts b/src/loop/core.ts index 47a457e4e0..21796da744 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -267,13 +267,6 @@ export interface LoopCtx { * rebuilt history. Default (undefined) keeps markers on. Paired * tool-call/tool-result messages are unaffected. */ visibilityMarkers?: boolean; - /** #3906: when `false`, keep the 📦/❌ ACP receipt in rebuilt history (and in - * the round-N+1 system message the model sees) but stop emitting it into - * the client's content stream. Independent of `visibilityMarkers`: this is - * the cosmetic-only switch for clients that merge the marker into the - * assistant's single text part, which makes the answer render above the - * reasoning that produced it. Default (undefined) keeps streaming. */ - streamMarkers?: boolean; /** #1455: tee loop-originated upstream responses (re-request and every * retry fetch) to raw SSE files — the outer request's ACP_DUMP_SSE tee * only covers the FIRST response, so an internal re-request that dies @@ -1448,22 +1441,7 @@ export async function* runCompressLoop( ctx.log(`[acp-loop] suppressed duplicate ${call.name} status marker (identical failure repeated this request)`); } else { seenMarkers.add(markerKey); - // #3906: streamMarkers=false keeps the receipt in the - // rebuilt history (and in the round-N+1 system message - // below) while keeping it OUT of the client's content - // stream. The marker is an assistant-prose delta emitted - // before the continuation re-request, so on a client that - // keys one text part per assistant message (opencode's - // openai-compatible transform uses a fixed txt-0) it opens - // the text part and the round-2 answer merges into it while - // round-2 reasoning opens a NEW part after it — the answer - // renders above the thinking that produced it and the turn - // appears to end mid-thought. - if (ctx.streamMarkers !== false) yield adapter.emitMarker(call.name, result.text); - // The model-side recovery receipt keeps the marker line even when - // the client stream is withheld. - streamedMarkers.push(buildVisibilityMarker(call.name, result.text)); - } + yield adapter.emitMarker(call.name, result.text); } } } else { realToolCalls.push(call); From 5f8e8732ce6e40544658b3120be77bf40dde5384 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 16:25:07 +0800 Subject: [PATCH 12/78] fix(degenerate-turn): implement the action-cue + closed-fence gates the PR body describes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Align-refactor (review of #4935): the PR body promises detection is 'accompanied by common action announcement verbs' and requires 'no markdown code fence open', but the shipped detector was tail-char + length only — any turn under 400 chars ending in ':' or ellipsis qualified, including legitimate short answers ('The answer is 42:') that would now cost an extra billed completion call each. Implement both promised gates: an ANNOUNCE_CUE word list (progressing forms + intent phrases only, mirroring the body's examples) and a reject on odd backtick-fence count (an unclosed fence is the #1755/#2190 truncation class, not a stall). English-first by design: non-English announcements keep the pre-#3905 no-retry behavior until traffic evidence justifies extending the cue list. (cherry picked from commit aec1e1b01db5586dca3e0e6865602c49e95a035c) --- src/degenerate-turn.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/degenerate-turn.ts b/src/degenerate-turn.ts index 7bb11d922b..346f7ee7c4 100644 --- a/src/degenerate-turn.ts +++ b/src/degenerate-turn.ts @@ -73,23 +73,38 @@ export function endsWithDraftClose(text: string): boolean { } // #3905: the announcement-tail shape. Unlike #2303 there is no draft tag: the -// ENTIRE visible output of the turn is a one-line promise of the next action +// ENTIRE visible output of the turn is a short promise of the next action // ("Running verify after formatting:") that ends on a colon or ellipsis, with // no tool call and a clean stop. The upstream reports a healthy turn, the host // gets nothing actionable, and the session stalls until the user types // something. Observed verbatim at 24 output tokens / 32 visible chars behind a // deepseek-v4.1-flash combo member. // -// The character cap is the whole precision boundary, not a performance guard: -// a genuine answer that happens to end on a colon is prose, and prose is long. -// 400 chars sits an order of magnitude above the observed stub and well below -// any real answer tail. -const ANNOUNCE_TAIL = /(?::|\u2026|\.\.\.)\s*$/; +// Three precision boundaries — each one denies a real answer class that the +// raw tail shape alone would misread as a stall (a false positive burns the +// one-shot BILLED retry on a turn that was already complete): +// 1. length cap: a genuine answer that happens to end on a colon is prose, +// and prose is long. 400 chars sits an order of magnitude above the +// observed stub and well below any real answer tail. +// 2. action-intent cue: a bare colon/ellipsis appears in legitimate short +// answers ("The answer is 42:", "I'm not sure…"). Only progressive-form +// action verbs and first-person intent phrases count as cues — the forms +// the production stubs actually use. The list is English-first by design: +// non-English announcements keep pre-#3905 behavior (no retry) until +// traffic evidence justifies more languages. +// 3. closed code fence: an UNCLOSED fence means the output was cut inside a +// code block — the truncated-output defect class (#1755/#2190), not a +// stalled promise; retrying it as an announcement would double-bill a +// length cut. +const ANNOUNCE_TAIL = /(?::|…|\.\.\.)\s*$/; const ANNOUNCE_MAX_CHARS = 400; +const ANNOUNCE_CUE = /\b(?:let me|i(?:'ll| will)|going to|gonna|running|checking|testing|inspecting|examining|looking|applying|adding|removing|updating|fixing|building|installing|scanning|verifying|formatting|cleaning|writing|reading|searching|starting|committing|pushing|deploying|restarting|compiling|linting|patching)\b/i; export function endsWithAnnouncement(text: string): boolean { const t = text.trimEnd(); if (t.length === 0 || t.length > ANNOUNCE_MAX_CHARS) return false; - return ANNOUNCE_TAIL.test(t); + if ((t.match(/```/g)?.length ?? 0) % 2 === 1) return false; + if (!ANNOUNCE_TAIL.test(t)) return false; + return ANNOUNCE_CUE.test(t); } // #2612: the #2303 draft-tail shape has a legitimate producer — the CLIENT's From 1773bf3df30661a00a300858f6ff379a22613fec Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 16:25:07 +0800 Subject: [PATCH 13/78] test(issue3905): cover plugin chat/responses lanes like the #2303 sibling; pin the 400-char cap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Align-refactor (review of #4935): the suite only covered unit + compress-loop lanes while the change also rewires pipePluginChatWithStrip and pipePluginResponsesWithStrip terminal classification — the #2303 sibling suite covers all three lanes, so match that bar: plugin chat positive (#3905 log line, one [DONE], single finish_reason stop), colon-answer and ellipsis-answer negatives, tool-call exclusion, retry-degenerates in-band error (#870); responses-pipe mirror set. Also fix at-l3, whose 'long answer' fixture did not actually end on the tail char and passed for the wrong reason — the rewritten fixture (441 chars, cue-bearing, colon tail) pins the length cap itself at loop level. Drop dead imports. (cherry picked from commit 206ddc973178f753ab85e3a6b9bbf2721ec76780) --- tests/issue3905-announcement-tail.test.ts | 305 +++++++++++++++++++--- 1 file changed, 271 insertions(+), 34 deletions(-) diff --git a/tests/issue3905-announcement-tail.test.ts b/tests/issue3905-announcement-tail.test.ts index f3e93216cf..75c4f1635b 100644 --- a/tests/issue3905-announcement-tail.test.ts +++ b/tests/issue3905-announcement-tail.test.ts @@ -8,43 +8,280 @@ import type { Session } from "../src/session.ts"; import { runCompressLoop, createOpenaiAdapter } from "../src/loop/index.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; -// #2303: a terminal turn whose visible prose ends with a compression-draft -// closing tag ( / ) and no tool call used to read as a -// healthy completed turn — the model wrote a handoff/compression draft in prose -// instead of issuing the action it described (149 silent stops / 70 sessions, -// DSH native). All three terminal-verdict lanes must now treat it as non- -// converged and spend their one-shot continuation nudge on it: the plugin chat -// pipe, the plugin responses pipe, and the compress loop. - -// ---------- helper ---------- - -test("endsWithAnnouncement: the production stub, its ellipsis forms, and the prose boundary", () => { - // Verbatim production stub (ses_eb6da0d60ffeYpnNQDoGz000Pc, 2026-10-19 11:21:43Z, - // 24 output tokens / 32 visible chars, finish=stop, no tool call). - assert.equal(endsWithAnnouncement("\nRunning verify after formatting:"), true); - assert.equal(endsWithAnnouncement("Running verify after formatting:"), true); +// #3905: a terminal turn whose ENTIRE visible output is a short promise of the +// next action ("Running verify after formatting:") ending on a colon or +// ellipsis, with no tool call and a clean stop, used to read as a healthy +// completed turn — the model announced the action instead of issuing it, and +// the session stalls until the user types something (verbatim production stub: +// ses_eb6da0d60ffeYpnNQDoGz000Pc, deepseek-v4.1-flash combo member, 24 output +// tokens / 32 visible chars, finish=stop). All three terminal-verdict lanes +// must treat it as non-converged and spend their one-shot continuation nudge +// on it: the plugin chat pipe, the plugin responses pipe, and the compress +// loop — mirroring the #2303 draft-tail suite. + +// ---------- detector unit ---------- + +const ANNOUNCE_STUB = "Running verify after formatting:"; + +test("endsWithAnnouncement: the production stubs and their ellipsis forms", () => { + assert.equal(endsWithAnnouncement("\n" + ANNOUNCE_STUB), true, "leading newline is stream residue"); + assert.equal(endsWithAnnouncement(ANNOUNCE_STUB), true); assert.equal(endsWithAnnouncement("Now I'll check the three suites:"), true); assert.equal(endsWithAnnouncement("Let me look at that output\u2026"), true, "unicode ellipsis"); assert.equal(endsWithAnnouncement("Let me look at that output..."), true, "ascii ellipsis"); - assert.equal(endsWithAnnouncement("Running verify after formatting: \r\n"), true, "trailing whitespace"); + assert.equal(endsWithAnnouncement(`${ANNOUNCE_STUB} \r\n`), true, "trailing whitespace"); }); -test("endsWithAnnouncement: rejects everything that is not a stub", () => { +test("endsWithAnnouncement: legitimate short answers ending on a colon or ellipsis are NOT stubs", () => { assert.equal(endsWithAnnouncement(""), false); assert.equal(endsWithAnnouncement(" \n\t "), false, "whitespace-only is the empty-turn shape, not this one"); assert.equal(endsWithAnnouncement("All done."), false, "a normal sentence end is not a promise"); assert.equal(endsWithAnnouncement("Deleted."), false); - assert.equal(endsWithAnnouncement("The answer is 42:"), true, "documented cost: a very short answer ending on a colon is indistinguishable from a stub"); - // The 400-char cap is the entire recall/precision boundary: a long answer - // that happens to end on a colon is prose. - assert.equal(endsWithAnnouncement("x".repeat(399) + ":"), true, "exactly 400 chars is still inside the cap"); - assert.equal(endsWithAnnouncement("x".repeat(400) + ":"), false, "401 chars is past the cap"); + assert.equal(endsWithAnnouncement("The answer is 42:"), false, "colon without an action-intent cue is a complete short answer"); + assert.equal(endsWithAnnouncement("I'm not sure\u2026"), false, "ellipsis without a cue is prose"); + assert.equal(endsWithAnnouncement("I'm not sure..."), false); + assert.equal(endsWithAnnouncement("Here is how to run it:"), false, "base-form 'run' inside an explanation is not a cue (progressive forms + intent phrases only)"); +}); + +test("endsWithAnnouncement: the 400-char cap stays the length boundary once a cue is present", () => { + const pad = (total: number) => `${ANNOUNCE_STUB.slice(0, -1)}${"a".repeat(total - ANNOUNCE_STUB.length)}:`; + assert.equal(endsWithAnnouncement(pad(400)), true, "exactly 400 chars is still inside the cap"); + assert.equal(endsWithAnnouncement(pad(401)), false, "401 chars is past the cap"); assert.equal(endsWithAnnouncement("y".repeat(400)), false, "no promise tail at all"); }); +test("endsWithAnnouncement: an unclosed code fence is the truncation class, not a stall (#1755/#2190)", () => { + assert.equal(endsWithAnnouncement("Running the tests:\n```\nstep one:"), false, "odd fence count — cut inside a code block"); + assert.equal(endsWithAnnouncement("Running the tests:\n```\nconst a = 1;\n```"), false, "closed fence, and no promise tail either"); +}); + test("endsWithAnnouncement: disjoint from endsWithDraftClose", () => { assert.equal(endsWithAnnouncement("handoff draft\n"), false, "#2303 owns the draft-close shape"); - assert.equal(endsWithDraftClose("Running verify after formatting:"), false, "#3905 owns the announcement shape"); + assert.equal(endsWithDraftClose(ANNOUNCE_STUB), false, "#3905 owns the announcement shape"); +}); + +// ---------- shared fixtures ---------- + +function makeSession(): Session { + return { + id: "testsess", + protocol: "openai", + upstreamOrigin: "http://127.0.0.1:9/v1", + label: "test", + createdAt: 0, + lastUsedAt: 0, + requests: 0, + lastInputTokens: 0, + stats: {}, + dirty: false, + } as unknown as Session; +} + +function makeRes(chunks: string[]) { + return { + writes: chunks, + write(b: Buffer | string) { + chunks.push(typeof b === "string" ? b : b.toString("utf8")); + return true; + }, + end(b?: Buffer | string) { + if (b !== undefined) chunks.push(typeof b === "string" ? b : b.toString("utf8")); + }, + once() {}, + destroyed: false, + writableEnded: false, + } as unknown as import("node:http").ServerResponse; +} + +function streamOf(events: string[]): ReadableStream { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream({ + pull(controller) { + if (i < events.length) { + controller.enqueue(enc.encode(events[i])); + i += 1; + } else { + controller.close(); + } + }, + }); +} + +function chatChunk(delta: Record): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: null }] })}\n\n`; +} + +const DONE = "data: [DONE]\n\n"; + +function chatStop(reason = "stop"): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta: {}, finish_reason: reason }] })}\n\n`; +} + +const sse = (event: string, data: unknown): string => `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; + +/** The production shape (sanitized): the whole visible output is the one-line + * promise, delivered as the message's text with no tool call. */ +function announceTurn(): string[] { + return [chatChunk({ role: "assistant" }), chatChunk({ content: `\n${ANNOUNCE_STUB}` }), chatStop(), DONE]; +} + +function proseTurn(text: string): string[] { + return [chatChunk({ role: "assistant" }), chatChunk({ content: text }), chatStop(), DONE]; +} + +function textDeltas(raw: string): string { + return [...raw.matchAll(/"content":"((?:[^"\\]|\\.)*)"/g)].map((m) => JSON.parse(`"${m[1]}"`) as string).join(""); +} + +// ---------- plugin chat pipe (the DSH native evidence lane) ---------- + +test("#3905 chat(openai): bare-announcement terminal turn spends the continuation nudge once", async () => { + const out: string[] = []; + const logs: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("real action issued after the nudge"))); + }; + await pipePluginChatWithStrip(streamOf(announceTurn()), makeRes(out), "openai", makeSession(), (m) => logs.push(m), refetch); + const text = out.join(""); + assert.equal(calls, 1, "exactly one re-issue"); + assert.ok(textDeltas(text).includes(ANNOUNCE_STUB), "the first attempt's stub still reaches the client"); + assert.ok(textDeltas(text).endsWith("real action issued after the nudge"), "the retry's content appends to the same stream"); + assert.equal((text.match(/\[DONE\]/g) ?? []).length, 1, "one turn, one terminal"); + assert.equal((text.match(/"finish_reason":"stop"/g) ?? []).length, 1, "the first attempt's terminal is dropped, not doubled"); + assert.ok(logs.some((l) => l.includes("#3905")), `the distinct #3905 log line fired, got: ${JSON.stringify(logs)}`); +}); + +test("#3905 chat: a legitimate short answer ending on a colon is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + await pipePluginChatWithStrip(streamOf(proseTurn("The answer is 42:")), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "no action-intent cue — the delivered answer is never duplicated or re-billed"); +}); + +test("#3905 chat: a legitimate short answer ending on an ellipsis is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + await pipePluginChatWithStrip(streamOf(proseTurn("I'm not sure\u2026")), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "ellipsis without a cue is prose, not a promise"); +}); + +test("#3905 chat: a bare announcement WITH a tool call is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + const events = [ + chatChunk({ role: "assistant" }), + chatChunk({ content: ANNOUNCE_STUB }), + `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "compress", arguments: "{}" } }] }, finish_reason: null }] })}\n\n`, + chatStop("tool_calls"), + DONE, + ]; + await pipePluginChatWithStrip(streamOf(events), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "a tool call means the step converges"); + assert.ok(out.join("").includes("compress"), "the agent's own tool call still passes through"); +}); + +test("#3905 chat: an announcement RETRY degenerates into the in-band error (#870 path)", async () => { + const out: string[] = []; + const refetch = () => Promise.resolve(streamOf(announceTurn())); + await pipePluginChatWithStrip(streamOf(announceTurn()), makeRes(out), "openai", makeSession(), undefined, refetch); + const text = out.join(""); + assert.ok( + text.includes("[ACP] stream error"), + "the client is told, rather than left with a second silent announcement", + ); +}); + +// ---------- plugin responses pipe ---------- + +function responsesAnnounceTurn(responseId = "resp_1", itemId = "item_1"): string[] { + const text = ANNOUNCE_STUB; + return [ + sse("response.created", { type: "response.created", response: { id: responseId, status: "in_progress" } }), + sse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { id: itemId, type: "message", content: [] } }), + sse("response.content_part.added", { type: "response.content_part.added", item_id: itemId, output_index: 0, part: { type: "output_text", text: "" } }), + sse("response.output_text.delta", { type: "response.output_text.delta", item_id: itemId, output_index: 0, delta: text }), + sse("response.output_text.done", { type: "response.output_text.done", item_id: itemId, output_index: 0, text }), + sse("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { id: itemId, type: "message", content: [{ type: "output_text", text }] } }), + sse("response.completed", { type: "response.completed", response: { id: responseId, status: "completed", output: [{ id: itemId, type: "message", content: [{ type: "output_text", text }] }] } }), + ]; +} + +function responsesProseTurn(text: string, responseId = "resp_2", itemId = "item_2"): string[] { + return [ + sse("response.created", { type: "response.created", response: { id: responseId, status: "in_progress" } }), + sse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { id: itemId, type: "message", content: [] } }), + sse("response.content_part.added", { type: "response.content_part.added", item_id: itemId, output_index: 0, part: { type: "output_text", text: "" } }), + sse("response.output_text.delta", { type: "response.output_text.delta", item_id: itemId, output_index: 0, delta: text }), + sse("response.output_text.done", { type: "response.output_text.done", item_id: itemId, output_index: 0, text }), + sse("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { id: itemId, type: "message", content: [{ type: "output_text", text }] } }), + sse("response.completed", { type: "response.completed", response: { id: responseId, status: "completed", output: [{ id: itemId, type: "message", content: [{ type: "output_text", text }] }] } }), + ]; +} + +function responsesDeltas(raw: string): string { + return [...raw.matchAll(/"delta":"((?:[^"\\]|\\.)*)"/g)].map((m) => JSON.parse(`"${m[1]}"`) as string).join(""); +} + +test("#3905 responses: bare-announcement completed turn spends the continuation nudge once", async () => { + const out: string[] = []; + const logs: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("recovered after the nudge"))); + }; + await pipePluginResponsesWithStrip(streamOf(responsesAnnounceTurn()), makeRes(out), makeSession(), (m) => logs.push(m), refetch); + const text = out.join(""); + assert.equal(calls, 1, "exactly one re-issue"); + assert.ok(responsesDeltas(text).includes(ANNOUNCE_STUB), "the first attempt's stub still reaches the client"); + assert.ok(responsesDeltas(text).endsWith("recovered after the nudge"), "the retry's prose appends to the same item"); + assert.equal((text.match(/"type":"response\.created"/g) ?? []).length, 1, "the retry does not open a second response"); + assert.equal((text.match(/"type":"response\.completed"/g) ?? []).length, 1, "one turn, one terminal"); + assert.ok(!text.includes("item_2") && !text.includes("resp_2"), `the retry's ids are rewritten onto the client's, got: ${text}`); + assert.ok(logs.some((l) => l.includes("#3905")), `the distinct #3905 log line fired, got: ${JSON.stringify(logs)}`); +}); + +test("#3905 responses: a legitimate short answer ending on an ellipsis is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("unwanted"))); + }; + await pipePluginResponsesWithStrip(streamOf(responsesProseTurn("I'm not sure...")), makeRes(out), makeSession(), undefined, refetch); + assert.equal(calls, 0, "ellipsis without a cue is prose, not a promise"); +}); + +test("#3905 responses: a bare announcement WITH a function call is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("unwanted"))); + }; + const events = [ + sse("response.created", { type: "response.created", response: { id: "resp_1", status: "in_progress" } }), + sse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { id: "item_1", type: "function_call", name: "read" } }), + sse("response.output_text.delta", { type: "response.output_text.delta", item_id: "item_1", output_index: 0, delta: ANNOUNCE_STUB }), + sse("response.completed", { type: "response.completed", response: { id: "resp_1", status: "completed", output: [{ id: "item_1", type: "function_call" }] } }), + ]; + await pipePluginResponsesWithStrip(streamOf(events), makeRes(out), makeSession(), undefined, refetch); + assert.equal(calls, 0, "a function call means the step converges"); }); // ---------- compress loop (proxy mode) ---------- @@ -75,21 +312,12 @@ function openaiSse(frames: Array>): string { return frames.map((f) => `data: ${JSON.stringify(f)}\n\n`).join("") + "data: [DONE]\n\n"; } -const ANNOUNCE_STUB = "Running verify after formatting:"; - const OPENAI_ANNOUNCE = openaiSse([ { id: "chatcmpl_a1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, { id: "chatcmpl_a1", choices: [{ index: 0, delta: { content: ANNOUNCE_STUB }, finish_reason: null }] }, { id: "chatcmpl_a1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, ]); -const OPENAI_ANSWER_END_COLON = openaiSse([ - { id: "chatcmpl_n1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, - { id: "chatcmpl_n1", choices: [{ index: 0, delta: { content: "Here is the full explanation of the three failing suites:" }, finish_reason: null }] }, - { id: "chatcmpl_n1", choices: [{ index: 0, delta: { content: " 1) aaaa 2) bbbb 3) cccc and the remainder of a genuinely long answer that keeps going well past the cap" }, finish_reason: null }] }, - { id: "chatcmpl_n1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, -]); - const OPENAI_GOOD = openaiSse([ { id: "chatcmpl_g1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, { id: "chatcmpl_g1", choices: [{ index: 0, delta: { content: "continued after nudge" }, finish_reason: null }] }, @@ -138,8 +366,17 @@ test("#3905 loop: one-shot bound — an announcement RETRY is not retried again" assert.equal(fetchCalls, 1, "one retry max; a second announcement falls through to the plain completion"); }); -test("#3905 loop: a long answer ending on a colon is NOT retried", async () => { - const { fetchCalls } = await drainOpenai(OPENAI_ANSWER_END_COLON, [OPENAI_GOOD], "at-l3"); +test("#3905 loop: a cue-bearing answer PAST the 400-char cap is NOT retried", async () => { + // 441 chars with a real action cue ("Running") and a colon tail: ONLY the + // length cap can deny it, so this pins the cap itself at loop level. + const longCueAnswer = ("Running through the failing suites: " + "lorem ipsum dolor sit amet ".repeat(15)).trimEnd() + ":"; + assert.ok(longCueAnswer.length > 400, `fixture drifted: ${longCueAnswer.length}`); + const longTurn = openaiSse([ + { id: "chatcmpl_n1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_n1", choices: [{ index: 0, delta: { content: longCueAnswer }, finish_reason: null }] }, + { id: "chatcmpl_n1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, + ]); + const { fetchCalls } = await drainOpenai(longTurn, [OPENAI_GOOD], "at-l3"); assert.equal(fetchCalls, 0, "the 400-char cap keeps real prose out of the retry"); }); From e51466127e6f9fa7d60ac7fb75885026a999f0a4 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 16:35:39 +0800 Subject: [PATCH 14/78] fix(plugin): announcement-tail detector must read answer prose, not proseAcc (#3905) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit proseAcc accumulates every streamed text field including the thinking/ reasoning channel. endsWithAnnouncement(proseAcc) therefore saw the tail of the model's internal monologue: a thought ending mid-sentence on ':' or an ellipsis followed by a whitespace-only answer was reclassified as a bare announcement (the new #3905 branch) instead of the #2756 degenerate shape — the retry still fired but under the wrong log line, and a whitespace-only turn on an unclean finish lost its 'not retried ... not a clean stop' refusal log entirely (stalledTail suppressed it). Two subtests in tests/issue2756-degenerate-gate.test.ts went red on every shard-3 CI leg. Operand swap to answerProse in both plugin pipes (chat + responses). The compress-loop twin already tests text-only assistantText and is unaffected. (cherry picked from commit 76c98b8aec00e59c33bcb1976abbc0e3380fd514) --- src/plugin.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 67cc7e9307..6f6a9a33aa 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -2971,7 +2971,12 @@ export async function pipePluginChatWithStrip( // output is a one-line promise of the next action ("Running verify after // formatting:"). Without this branch the answerChars gate below would read // that promise as a delivered answer and refuse the retry. - const announceTail = !draftTail && visibleTextChars > 0 && !sawToolUse && endsWithAnnouncement(proseAcc) && !requestExpectsProseSummary(requestText); + // The detector reads ANSWER prose, not proseAcc: proseAcc also carries + // the thinking/reasoning channel, and a thought that ends mid-sentence on + // ":" or "…" is internal monologue, not an announcement — feeding it here + // reclassified #2756's whitespace-only degenerate stops as announcements + // and swallowed their refusal/retry logs. + const announceTail = !draftTail && visibleTextChars > 0 && !sawToolUse && endsWithAnnouncement(answerProse) && !requestExpectsProseSummary(requestText); const stalledTail = draftTail || announceTail; // #2756: the gate asks "did the host receive ANSWER text?" — prose that // is nothing but whitespace (a bare "\n\n" from a degenerate stop, the @@ -4648,8 +4653,9 @@ export async function pipePluginResponsesWithStrip( // #2612: except when the request itself demands that shape (client // compaction summary instruction) — see the chat-pipe twin. const draftTail = (visibleTextChars > 0 || heldVisibleChars > 0) && !sawFunctionCall && endsWithDraftClose(proseAcc) && !requestExpectsProseSummary(requestText); - // #3905: bare-announcement twin — see the chat-pipe twin above. - const announceTail = !draftTail && (visibleTextChars > 0 || heldVisibleChars > 0) && !sawFunctionCall && endsWithAnnouncement(proseAcc) && !requestExpectsProseSummary(requestText); + // #3905: bare-announcement twin — see the chat-pipe twin above + // (including why the detector reads answerProse, not proseAcc). + const announceTail = !draftTail && (visibleTextChars > 0 || heldVisibleChars > 0) && !sawFunctionCall && endsWithAnnouncement(answerProse) && !requestExpectsProseSummary(requestText); const stalledTail = draftTail || announceTail; // #2756: same question as the chat-pipe twin — "did the host receive // ANSWER text?". Reasoning inlined into item.content[] (vLLM/ From 206180d1adccc310e88af2459f1e4f50ce845914 Mon Sep 17 00:00:00 2001 From: "huzhening.hzn" Date: Wed, 28 Oct 2026 14:02:25 +0800 Subject: [PATCH 15/78] fix: arm the pi lane's direct-send window guard so a proxy outage cannot egress an oversized history (#5025) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without a live proxy, directSendOrRefuse measured the raw body against a window it read from the stamped x-bili-plugin-context-window header or from state.declaredWindowForDirect. On the pi lane BOTH were empty on exactly that path: before_provider_headers returns at `proxyBase === undefined` before it stamps the header, and the lane never armed the slot (#4894 armed dsh only). The check then failed open by construction, so a long session's unfurled history (multi-million tokens) went straight to the provider, whose "input exceeds the context window" 400 repeated on every retry and made the session unrecoverable for as long as the proxy stayed down. Record the per-request ctx.model.contextWindow at the TOP of the headers hook (before the readiness return) and arm it as declaredWindowForDirect in pi-native.ts. An unknown window records undefined — the guard keeps failing open instead of guessing — and a healthy proxy is unchanged, since the stamped header still outranks the reader. Verification: tests/issue5025-pi-direct-window.test.ts (5 cases: recording before the early return, unknown-window reset, the armed reader, local oversize refusal with no upstream egress, and no false refusal when the body fits); typecheck; issue4894/pi-native/issue3433/issue2072/fix-1897 suites; unused-export and test-import-graph gates. (cherry picked from commit cfcb3f91f2456bce2b65b82abb593aba46d47e42) --- src/agent/pi-native.ts | 14 +- src/agent/pi.ts | 32 +++++ tests/issue5025-pi-direct-window.test.ts | 176 +++++++++++++++++++++++ 3 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 tests/issue5025-pi-direct-window.test.ts diff --git a/src/agent/pi-native.ts b/src/agent/pi-native.ts index 5a74613f78..e6a38bf818 100644 --- a/src/agent/pi-native.ts +++ b/src/agent/pi-native.ts @@ -29,7 +29,7 @@ import os from "node:os"; import path from "node:path"; import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST, SPAWN_BUDGET_MS } from "../launcher.js"; import { piPackageStrings } from "../plugin-install.js"; -import { createBiliPlugin, createPiDirectSendGuard } from "./pi.js"; +import { createBiliPlugin, createPiDirectSendGuard, piDeclaredWindowForDirect } from "./pi.js"; import { isLegacyBcpEntry, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; import { loadConfigFile } from "../config.js"; import { getInlineEngine, INLINE_ORIGIN } from "../inline-engine.js"; @@ -144,6 +144,13 @@ const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve( // sniffs the outgoing envelope and restores the stashed original before any // direct-send fall-back reaches the real upstream. state.prepareDirectSend = createPiDirectSendGuard(); +// #4894: arm the shared oversize arm for the degraded direct path. pi stamps +// x-bili-plugin-context-window only on proxy-bound requests (and only when the +// proxy base resolves), so the not-ready path — the one that egresses the raw +// history uncompressed — arrives with no header. Without this the guard fell +// open and a multi-million-token history rode straight to the provider, whose +// 400 comes back as an unrecoverable "input exceeds the context window". +state.declaredWindowForDirect = piDeclaredWindowForDirect; async function bootstrap(): Promise { try { @@ -361,6 +368,11 @@ export function _stateRespawnForTest(): (() => Promise) | un return state.respawn; } +/** Test hook (#4894): the armed direct-send window reader, if any. */ +export function _declaredWindowHookForTest(): (() => number | undefined) | undefined { + return state.declaredWindowForDirect; +} + /** Test hook (#1365): record routed-channel evidence without the fetch patch. */ export function _noteRoutedForTest(url: string): void { noteRoutedOrigin(state, url); diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 83792937a7..66cb84fadc 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -860,6 +860,32 @@ export function __stashPiIncrementalOriginalForTests(conversationId: string, pay stashPiIncrementalOriginal(conversationId, payload); } +// #4894 (pi lane): the declared context window of the session's CURRENT model, +// recorded on every before_provider_headers event. The shared oversize arm in +// directSendOrRefuse (native-intercept.ts) prefers the stamped +// x-bili-plugin-context-window header — but this lane stamps that header only +// on the proxy branch, so a request that finds NO live proxy (respawn failed / +// spawn budget missed) carried none and the arm stayed disarmed in exactly the +// state where an uncompressed egress is likeliest to blow the window: the raw +// history then rode straight to the provider and came back as "input exceeds +// the context window", unrecoverable while the proxy stayed down. Recorded +// BEFORE the proxy-readiness early return below for the same reason. +// `undefined` means UNKNOWN (no ctx.model / no window configured) and the arm +// then fails open rather than guessing a window for this request. +let piDeclaredWindow: number | undefined; + +/** #4894: window reader the native intercept arms as declaredWindowForDirect + * (see pi-native.ts). Process-scoped: pi dispatches model requests + * sequentially per session and subagents run in their own processes. */ +export function piDeclaredWindowForDirect(): number | undefined { + return piDeclaredWindow; +} + +/** Test hook: forget the recorded window (fixtures share one process). */ +export function _resetPiDeclaredWindowForTest(): void { + piDeclaredWindow = undefined; +} + /** #3304: prepareDirectSend for the pi/omp extension lanes (pi-native arms * it on its NativeInterceptState). A body that IS an incremental envelope * was swapped by maybeIncrementalTurn → restore the stashed original @@ -1308,6 +1334,12 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM }); pi.on("before_provider_headers", async (event, ctx) => { try { + // #4894: record this request's window FIRST — the + // proxy-readiness return right below is precisely the + // degraded direct-send path the guard measures against, so + // the window must not depend on the proxy being up. + const liveWindow = ctx.model?.contextWindow; + piDeclaredWindow = typeof liveWindow === "number" && Number.isFinite(liveWindow) && liveWindow > 0 ? Math.floor(liveWindow) : undefined; // #1243: on the native lane the proxy origin lands via an async // bootstrap that writes BILLION_CONTEXT_PROXY only after the spawn; // a one-shot (-p) fires this event exactly once, inside that diff --git a/tests/issue5025-pi-direct-window.test.ts b/tests/issue5025-pi-direct-window.test.ts new file mode 100644 index 0000000000..92a4189447 --- /dev/null +++ b/tests/issue5025-pi-direct-window.test.ts @@ -0,0 +1,176 @@ +// #5025: without a live proxy the pi lane sent the raw history straight to the +// provider — a multi-million-token body came back as "input exceeds the context +// window" on every retry, unrecoverable while the proxy stayed down. The shared +// oversize arm in directSendOrRefuse needed a window to measure against, and +// both its sources were empty on exactly that path: the +// x-bili-plugin-context-window header is stamped only on the proxy branch (the +// handler returns at `proxyBase === undefined` before stamping), and the pi lane +// never armed declaredWindowForDirect (#4894 armed dsh only). These cases pin +// the pi half: the window is recorded BEFORE the readiness return, an unknown +// window clears it (fail open — never a guessed window), pi-native arms the +// reader, and the armed pair refuses locally instead of egressing. Hermetic like +// issue4894-direct-window-guard.test.ts: XDG_STATE_HOME redirected so no event +// reaches a real log file, proxy env cleared so no probe targets a live proxy. +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { after, beforeEach, test } from "node:test"; +import { createBiliPlugin, piDeclaredWindowForDirect, _resetPiDeclaredWindowForTest } from "../src/agent/pi.ts"; +import { _declaredWindowHookForTest } from "../src/agent/pi-native.ts"; + +const STATE_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "bili-5025-state-")); +const PREV_ENV: Record = {}; +for (const key of ["XDG_STATE_HOME", "BILLION_CONTEXT_PROXY", "BILLION_CONTEXT_ATTACH", "BILLION_CONTEXT_PLUGIN", "BILI_NATIVE_PI"]) { + PREV_ENV[key] = process.env[key]; +} +// The native entry arms at import time; a dev shell's live proxy must not be +// probed from inside this suite (same guard as pi-native.test.ts). +delete process.env.BILLION_CONTEXT_PROXY; +delete process.env.BILLION_CONTEXT_ATTACH; +process.env.XDG_STATE_HOME = STATE_ROOT; +after(() => { + for (const [key, value] of Object.entries(PREV_ENV)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + fs.rmSync(STATE_ROOT, { recursive: true, force: true }); +}); + +const UPSTREAM = "http://127.0.0.1:8199/v1/chat/completions"; + +type FakePi = { + events: Map unknown>; + tools: Array<{ name: string }>; + on: (event: string, handler: (event: never, ctx: never) => unknown) => void; + registerTool: (tool: { name: string }) => void; +}; + +function makeFakePi(): FakePi { + const events = new Map unknown>(); + const tools: Array<{ name: string }> = []; + return { + events, + tools, + on: (event, handler) => { + const h = handler as (event: unknown, ctx: unknown) => unknown; + const prev = events.get(event); + events.set(event, prev ? ((e, c) => { prev(e, c); return h(e, c); }) : h); + }, + registerTool: (tool) => { + const i = tools.findIndex((t) => t.name === tool.name); + if (i >= 0) tools[i] = tool; + else tools.push(tool); + }, + }; +} + +/** A pi ctx with NO proxy in the model baseUrl — the reported degraded state: + * proxyBaseForCtx resolves undefined, so the header branch returns early. */ +function piCtx(contextWindow: number | undefined, sid = "pi-5025") { + return { + sessionManager: { getSessionId: () => sid }, + model: { id: "gpt-test", api: "openai-completions", baseUrl: "http://127.0.0.1:1/v1", ...(contextWindow !== undefined ? { contextWindow } : {}) }, + cwd: "/tmp", + }; +} + +function headersHandler(): (event: unknown, ctx: unknown) => unknown { + const pi = makeFakePi(); + createBiliPlugin("pi")(pi as never); + const handler = pi.events.get("before_provider_headers"); + assert.ok(handler, "before_provider_headers handler registered"); + return handler; +} + +function chatBody(textChars: number): string { + return JSON.stringify({ model: "gpt-test", messages: [{ role: "user", content: "x".repeat(textChars) }] }); +} + +beforeEach(() => { + _resetPiDeclaredWindowForTest(); + delete process.env.BILLION_CONTEXT_PROXY; +}); + +test("#5025 pi records the window even when no proxy base resolves (the direct-send path)", async () => { + const handler = headersHandler(); + const headers: Record = {}; + await handler({ headers }, piCtx(500_000)); + assert.equal(piDeclaredWindowForDirect(), 500_000, "recorded before the proxy-readiness return"); + // The premise of the bug: in this state the header is never stamped, which + // is why the shared guard had nothing to measure against. + assert.equal(headers["x-bili-plugin-context-window"], undefined, "no proxy base → no stamped window"); + assert.equal(headers["x-bili-plugin"], undefined, "no proxy base → no ownership marker either"); +}); + +test("#5025 unknown window clears the record — the arm fails open instead of guessing", async () => { + const handler = headersHandler(); + await handler({ headers: {} }, piCtx(500_000)); + assert.equal(piDeclaredWindowForDirect(), 500_000); + await handler({ headers: {} }, piCtx(undefined)); + assert.equal(piDeclaredWindowForDirect(), undefined, "a model with no declared window must not inherit the previous one"); +}); + +test("#5025 pi-native arms the shared guard with the pi window reader", async () => { + const hook = _declaredWindowHookForTest(); + assert.equal(typeof hook, "function", "declaredWindowForDirect armed on the pi NativeInterceptState"); + await headersHandler()({ headers: {} }, piCtx(272_000)); + assert.equal(hook?.(), 272_000, "the armed reader answers the recorded window"); +}); + +test("#5025 armed pi reader + not-ready intercept: oversized body refused locally, nothing egresses", async () => { + const saved = globalThis.fetch; + const mod = await import("../src/agent/native-intercept.ts"); + const sink: string[] = []; + mod._resetForTest(); + globalThis.fetch = (async (input: string | URL | Request) => { + sink.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return new Response("{}", { status: 200 }); + }) as typeof fetch; + try { + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { + origin: undefined, + ready: Promise.resolve(undefined), + respawn: async () => undefined, + declaredWindowForDirect: piDeclaredWindowForDirect, + }; + assert.equal(mod.installNativeFetchIntercept(state), true); + await headersHandler()({ headers: {} }, piCtx(500_000)); + const res = await globalThis.fetch(UPSTREAM, { method: "POST", headers: { "content-type": "application/json" }, body: chatBody(4_200_000) }); + const body = (await res.json()) as Record; + assert.equal(res.status, 422); + assert.equal(res.headers.get("x-bili-gate"), "oversize-refusal-fallback"); + assert.equal((body.error as { code: string }).code, "bili_gate_oversize_refusal_fallback"); + assert.deepEqual(sink, [], "no upstream egress"); + } finally { + globalThis.fetch = saved; + mod._resetForTest(); + } +}); + +test("#5025 armed pi reader + body that fits: direct send unchanged (no false refusal)", async () => { + const saved = globalThis.fetch; + const mod = await import("../src/agent/native-intercept.ts"); + const sink: string[] = []; + mod._resetForTest(); + globalThis.fetch = (async (input: string | URL | Request) => { + sink.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return new Response("{}", { status: 200 }); + }) as typeof fetch; + try { + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { + origin: undefined, + ready: Promise.resolve(undefined), + respawn: async () => undefined, + declaredWindowForDirect: piDeclaredWindowForDirect, + }; + assert.equal(mod.installNativeFetchIntercept(state), true); + await headersHandler()({ headers: {} }, piCtx(500_000)); + const res = await globalThis.fetch(UPSTREAM, { method: "POST", headers: { "content-type": "application/json" }, body: chatBody(10_000) }); + assert.equal(res.status, 200, "direct send proceeds"); + assert.deepEqual(sink, [UPSTREAM]); + } finally { + globalThis.fetch = saved; + mod._resetForTest(); + } +}); From 852f8020a9c3bf81fa83c46b500babc874432e6c Mon Sep 17 00:00:00 2001 From: Evan Light Date: Wed, 28 Oct 2026 18:02:01 -0400 Subject: [PATCH 16/78] fix: forward toolless structured-output requests as side requests A request with no tools that asks for a JSON reply (response_format json_schema/json_object, or text.format on the Responses wire) is a utility call, not an agent turn. Child processes of a host inherit the proxy through HTTP(S)_PROXY, and adding the ACP tools to their extraction calls makes backends that refuse tools plus response_format answer 400. Tool-carrying requests and an explicit main persona are still main turns. Fixes #5073 Co-Authored-By: Claude Opus 5.5 (cherry picked from commit ef508e926acce62c42a2f97715b3d925286ecf7b) --- src/server/side-request.ts | 19 ++++++++++++ tests/side-request-isolation.test.ts | 45 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 811d53c6c4..79a869458c 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -72,6 +72,20 @@ export function isServerToolUtilityCall(parsed: unknown): boolean { return false; } +const STRUCTURED_FORMAT_TYPES: ReadonlySet = new Set(["json_schema", "json_object"]); + +/** Whether the body asks for a JSON reply: OpenAI chat `response_format` or + * the Responses wire's `text.format`. `{type:"text"}` is plain prose and does + * not count. */ +export function requestsStructuredOutput(p: Record): boolean { + const text = p.text; + const format = p.response_format + ?? (text !== null && typeof text === "object" ? (text as { format?: unknown }).format : undefined); + if (format === null || typeof format !== "object") return false; + const type = (format as { type?: unknown }).type; + return typeof type === "string" && STRUCTURED_FORMAT_TYPES.has(type); +} + export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { if (!parsed || typeof parsed !== "object") return false; if (requestAgent !== undefined && SIDE_REQUEST_AGENTS.has(requestAgent)) return true; @@ -84,6 +98,11 @@ export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { // gets injected into it. Explicit host intent above still outranks this. if (isServerToolUtilityCall(p)) return true; if (Array.isArray(p.tools) && p.tools.length > 0) return false; + // A toolless request that demands a structured JSON reply is a utility + // call, not an agent turn (memory-extraction daemons such as Hindsight + // that inherit the host's HTTPS_PROXY, with a large output budget). Wire + // tools injected into it make strict backends reject the pair with a 400. + if (requestsStructuredOutput(p)) return true; const field = outputBudgetField(parsed); if (!field) return false; const raw = readOutputBudget(p, field); diff --git a/tests/side-request-isolation.test.ts b/tests/side-request-isolation.test.ts index d662b32acc..cd3584411f 100644 --- a/tests/side-request-isolation.test.ts +++ b/tests/side-request-isolation.test.ts @@ -41,6 +41,21 @@ test("isSideRequest: tiny output budget across protocol field names", () => { assert.equal(isSideRequest({ max_output_tokens: 16, tools: [{ type: "function", function: { name: "f" } }] }), false, "#546: responses wire, starved budget + tools → main"); }); +test("isSideRequest: a toolless structured-output request is a side request", () => { + // A memory-extraction daemon (Hindsight) inherits the host's HTTPS_PROXY and + // sends JSON-schema extraction calls with a large budget. Injected wire tools + // turn those into tools + response_format, which strict backends reject. + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "json_object" } }), true, "chat json_object"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "json_schema", json_schema: { name: "r", schema: {} } } }), true, "chat json_schema"); + assert.equal(isSideRequest({ max_output_tokens: 64000, text: { format: { type: "json_schema", name: "r", schema: {} } } }), true, "responses text.format json_schema"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "text" } }), false, "plain text format is not structured"); + assert.equal(isSideRequest({ max_output_tokens: 64000, text: { format: { type: "text" } } }), false, "responses plain text format"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: null }), false, "null response_format"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: "json_object" }), false, "non-object response_format"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "json_object" }, tools: [{ type: "function", function: { name: "f" } }] }), false, "#546 still wins: a tool-carrying structured request is a main turn"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "json_object" } }, "main"), false, "explicit main persona outranks the shape"); +}); + test("isSideRequest: host-declared side-request agent outranks the token-budget heuristic (#1699)", () => { // opencode v2 title-gen carries NO max_tokens (options {} for kind==="title"), // so the budget path can never see it. Declaring the persona by intent fixes @@ -791,6 +806,36 @@ test("e2e: opencode v2 title-gen is classified by intent, not token budget (#169 } }); +test("e2e: a toolless structured-output request is forwarded without injected tools", async () => { + const rig = await startRig(); + try { + const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; + const headers = { "content-type": "application/json", "x-acp-session": SESSION }; + const messages = [ + { role: "system", content: "Extract facts from the conversation." }, + { role: "user", content: "User: I moved to Lisbon last spring." }, + ]; + // Control: the same shape without response_format is a main turn and gets the wire tools. + const rControl = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, stream: true, max_tokens: 64000, messages }) }); + assert.equal(rControl.status, 200); + await rControl.text(); + const controlTools = (rig.lastBody as { tools?: unknown[] } | null)?.tools; + assert.ok(Array.isArray(controlTools) && controlTools.length > 0, "control: a toolless main turn gets the ACP tools injected"); + + const body = { model: MODEL, stream: true, max_tokens: 64000, messages, response_format: { type: "json_object" } }; + const rFix = await fetch(url, { method: "POST", headers, body: JSON.stringify(body) }); + assert.equal(rFix.status, 200); + await rFix.text(); + const fwd = rig.lastBody as { tools?: unknown; messages?: unknown; response_format?: unknown } | null; + assert.ok(fwd, "upstream received the request"); + assert.equal(fwd?.tools, undefined, "no tools injected beside response_format"); + assert.deepEqual(fwd?.messages, messages, "messages forwarded verbatim"); + assert.deepEqual(fwd?.response_format, body.response_format, "response_format untouched"); + } finally { + await closeRig(rig); + } +}); + test("e2e: side request response still gets render-tag stripping (#460 contract)", async () => { const LT = "\x3c"; const GT = "\x3e"; From cb5f44e7cf4225179d79be7e5544952c7b3f3100 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 29 Oct 2026 08:40:54 +0800 Subject: [PATCH 17/78] fix(#5074): drop the unused export from requestsStructuredOutput The helper is only called by isSideRequest() inside the same module; no other file imports it, so the unused-exports gate rejects the leading export. Behavior is unchanged. (cherry picked from commit 7a29d546c6b999e1e381929234cff67dbf314a5a) --- src/server/side-request.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 79a869458c..81504c1b13 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -77,7 +77,7 @@ const STRUCTURED_FORMAT_TYPES: ReadonlySet = new Set(["json_schema", "js /** Whether the body asks for a JSON reply: OpenAI chat `response_format` or * the Responses wire's `text.format`. `{type:"text"}` is plain prose and does * not count. */ -export function requestsStructuredOutput(p: Record): boolean { +function requestsStructuredOutput(p: Record): boolean { const text = p.text; const format = p.response_format ?? (text !== null && typeof text === "object" ? (text as { format?: unknown }).format : undefined); From bb1ecde9b616091776ea3889b654013d1e5ad072 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 29 Oct 2026 09:00:29 +0800 Subject: [PATCH 18/78] chore: anchor the new side-request rule comments to #5073 Review supplement on 7a29d546c (#5074): every classification rule in this file carries its issue number as a comment anchor (#2500/#2503/#3149/#4438); the two comments added by ef508e926 did not, so the rule's provenance was not greppable. Comments only - no behavior change. (cherry picked from commit 17e82b7a28ab1b398d58f6a9e36c590e65509fc9) --- src/server/side-request.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 81504c1b13..603e54b474 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -74,9 +74,9 @@ export function isServerToolUtilityCall(parsed: unknown): boolean { const STRUCTURED_FORMAT_TYPES: ReadonlySet = new Set(["json_schema", "json_object"]); -/** Whether the body asks for a JSON reply: OpenAI chat `response_format` or - * the Responses wire's `text.format`. `{type:"text"}` is plain prose and does - * not count. */ +/** #5073: whether the body asks for a JSON reply: OpenAI chat `response_format` + * or the Responses wire's `text.format`. `{type:"text"}` is plain prose and + * does not count. */ function requestsStructuredOutput(p: Record): boolean { const text = p.text; const format = p.response_format @@ -98,10 +98,11 @@ export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { // gets injected into it. Explicit host intent above still outranks this. if (isServerToolUtilityCall(p)) return true; if (Array.isArray(p.tools) && p.tools.length > 0) return false; - // A toolless request that demands a structured JSON reply is a utility - // call, not an agent turn (memory-extraction daemons such as Hindsight - // that inherit the host's HTTPS_PROXY, with a large output budget). Wire - // tools injected into it make strict backends reject the pair with a 400. + // #5073: a toolless request that demands a structured JSON reply is a + // utility call, not an agent turn (memory-extraction daemons such as + // Hindsight that inherit the host's HTTPS_PROXY, with a large output + // budget). Wire tools injected into it make strict backends reject the + // pair with a 400. if (requestsStructuredOutput(p)) return true; const field = outputBudgetField(parsed); if (!field) return false; From c66905c00badb25b4fa7d014fa21708e8f800511 Mon Sep 17 00:00:00 2001 From: Evan Light <10112+elight@users.noreply.github.com> Date: Thu, 5 Nov 2026 12:04:18 -0500 Subject: [PATCH 19/78] fix(hermes): align the #5073 side-call fix with #546/#388/#2500 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ship the classification as the exported isStructuredOutputCall predicate with the #546 no-tools veto built in, so handle() can consult it independently of isSideRequest and skip restoreOutputBudget for the call — its 64000 budget sizes a utility response, not a main turn, and must not seed outputBudgetHighWater (#1897/#2500 class). Give the #388 side passthrough an honest log label, and extend the e2e to assert kernel state, stats, snapshot, request counter and high-water are all untouched, mirroring the #2500 suite. The new high-water assertion fails without the restore skip and passes with it. (cherry picked from commit a828e596eeb3478e2bb61b7a4d1c08d716a64f05) --- src/server.ts | 2 +- src/server/handle.ts | 14 +++++-- src/server/side-request.ts | 27 +++++++++--- tests/side-lane-invariants.test.ts | 10 +++-- tests/side-request-isolation.test.ts | 61 ++++++++++++++++++++++++---- 5 files changed, 93 insertions(+), 21 deletions(-) diff --git a/src/server.ts b/src/server.ts index c0fd2216e5..59a9e6c4f8 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4863,5 +4863,5 @@ function logMsg(opts: ProxyOptions, level: string, msg: string): void { export { getUnrecognizedPathStats, logDumpFailure, logUnrecognizedPath } from "./server/observability.js"; export { BILI_HOP_HEADER, parseLauncherModelWindows, anthropicBetaContextWindow, capRegistryWindowByStandard, expandedContextSuffixWindow } from "./server/context-window.js"; -export { BILI_TOOL_NAMES, isSideRequest, outputBudgetField, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR, type OutputBudgetField } from "./server/side-request.js"; +export { BILI_TOOL_NAMES, isSideRequest, isStructuredOutputCall, outputBudgetField, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR, type OutputBudgetField } from "./server/side-request.js"; export { countSystemAndToolsTokens, estimateInputTokens, estimateWireOverhead, clampOutputBudget, emergencyNudge, projectThinkingMass, type ThinkingMassInput } from "./server/budget.js"; diff --git a/src/server/handle.ts b/src/server/handle.ts index ad4141aea3..696c011566 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -70,7 +70,7 @@ import { buildForgeResponse, classifierForgeVariant, estimateCjkTokens, isAutoMo import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, unwrapV1Internal } from "../v1internal.js"; import { anthropicBetaContextWindow, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; -import { demoteGate, hasLeakedBiliToolsOnly, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; +import { demoteGate, hasLeakedBiliToolsOnly, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; import { buildDshCompactionForgeBody, renderDshAuthoredCheckpoint } from "./dsh-authored-compaction.js"; import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js"; @@ -2368,6 +2368,14 @@ async function handleRequest( // not a main turn, and seeding outputBudgetHighWater from it would // poison the first starved restore (same class as the #1897 skip). const serverToolUtility = isServerToolUtilityCall(parsed); + // #5073: a toolless structured-output request (Hindsight fact extraction) + // is the same category — a host-internal utility call under the main + // session id. It rides the #388 side passthrough via isSideRequest, and + // must ALSO skip restoreOutputBudget here: its max_tokens (64000) sizes + // the extraction response, not a main turn, and seeding + // outputBudgetHighWater from it would poison the first starved restore + // (same class as the #1897/#2500 skip). + const structuredOutputUtility = isStructuredOutputCall(parsed); // #546: restore a client-shrunk output budget BEFORE the side gate so a // tool-carrying main request re-enters the pipeline at full budget (see // restoreOutputBudget for the starvation mechanism). #1665/#1840: the @@ -2379,7 +2387,7 @@ async function handleRequest( // outputBudgetHighWater from it would poison the first starved restore // (title requests arrive FIRST, at session start). Starved all-bili requests // never reach here demoted: stripLeakedBiliTools vetoes them per #546. - if (!demotedSide && !serverToolUtility) { + if (!demotedSide && !serverToolUtility && !structuredOutputUtility) { // #3438: the dsh host contract (pi-ai buildBaseOptions) stamps the model-declared // maxTokens on every main turn, so on this lane a below-ceiling tool-carrying // budget is a raw-history collapse, not intent — arm the mid-band rescue. @@ -2580,7 +2588,7 @@ async function handleRequest( // #2500 server-tool utility shape) diverts under a receipt. // #2170 measure 1: the decision itself is resolveSideLane() (pure, // truth-table-tested); demotedSide ⊆ lane==="side" by construction. - const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: serverToolUtility ? "server-tool utility call (#2500)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); + const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); if (sideLane.lane === "side") { // #554: the passthrough below skips EVERY input-side guard by design // (#388) — a full-history side request over the window is a diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 603e54b474..e38ea78494 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -74,10 +74,24 @@ export function isServerToolUtilityCall(parsed: unknown): boolean { const STRUCTURED_FORMAT_TYPES: ReadonlySet = new Set(["json_schema", "json_object"]); -/** #5073: whether the body asks for a JSON reply: OpenAI chat `response_format` - * or the Responses wire's `text.format`. `{type:"text"}` is plain prose and - * does not count. */ -function requestsStructuredOutput(p: Record): boolean { +/** #5073: structural identification of a tool-less structured-output utility + * call — a memory-extraction daemon such as Hindsight (inheriting the host's + * HTTPS_PROXY, under the main session id) sends no tools, asks for a JSON + * reply (`response_format` of type `json_schema`/`json_object` on the chat + * wire, or `text.format` of those types on the Responses wire), and carries a + * large output budget. Such a call is a one-shot utility request, not an + * agent turn: agent turns always carry an action surface (#546), so a strict + * backend rejects `tools` injected beside `response_format` with a 400. It + * therefore rides the #388 verbatim side passthrough. + * The no-tools veto is part of the predicate (not only the caller's #546 + * guard) so callers that consult it independently of `isSideRequest` — the + * restoreOutputBudget skip in handle.ts, the same category as the #2500 + * server-tool predicate — can never demote a tool-carrying main turn. + * `{type:"text"}` is plain prose and does not count. */ +export function isStructuredOutputCall(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const p = parsed as Record; + if (Array.isArray(p.tools) && p.tools.length > 0) return false; const text = p.text; const format = p.response_format ?? (text !== null && typeof text === "object" ? (text as { format?: unknown }).format : undefined); @@ -102,8 +116,9 @@ export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { // utility call, not an agent turn (memory-extraction daemons such as // Hindsight that inherit the host's HTTPS_PROXY, with a large output // budget). Wire tools injected into it make strict backends reject the - // pair with a 400. - if (requestsStructuredOutput(p)) return true; + // pair with a 400. Routed through the #388 side passthrough like the #2500 + // server-tool shape above. + if (isStructuredOutputCall(parsed)) return true; const field = outputBudgetField(parsed); if (!field) return false; const raw = readOutputBudget(p, field); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 21485d764a..39363a59ab 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -349,13 +349,15 @@ test("(F) extracted protocol modules keep the side-lane boundary (#1440 P2 cut 2 // isDshSingleShotSidecar — same category once more: the generic single-shot // dsh sidecar shape predicate (no tools + one-shot body, any system text) // consulted to set sideIntent and to keep unknown sidecars off the raw-key -// anchor before the first main turn can claim it. -test("(G) extracted pipeline module keeps the side-lane boundary (#1440 P2 cut 3)", () => { +// anchor before the first main turn can claim it.// title call out of the #4774 raw-key anchor race. #5073 adds +// isStructuredOutputCall — same category once more: a pure toolless +// structured-output shape predicate (no threshold, no session state) consulted +// to set sideIntent and to keep the call out of the restoreOutputBudget +// high-water path, exactly like the #2500 server-tool predicate.test("(G) extracted pipeline module keeps the side-lane boundary (#1440 P2 cut 3)", () => { const text = readFileSync(fileURLToPath(new URL("../src/server/handle.ts", import.meta.url)), "utf8"); const laneInternals = /\bSIDE_REQUEST_MAX_TOKENS\b|\bSIDE_REQUEST_AGENTS\b|\bDemoteGateSignals\b|\bSideLaneSignals\b|\bSideLaneDecision\b|\bBILI_TOOL_NAMES\b|\bOutputBudgetField\b|\boutputBudgetField\b|\breadOutputBudget\b|\bwriteOutputBudget\b|\b_resetNoOutputCeilingWarningsForTest\b/; assert.doesNotMatch(text, laneInternals, "handle.ts must stay decoupled from the side-lane engine internals"); - const allowlist = new Set(["hasLeakedBiliToolsOnly", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); - let sawImport = false; + const allowlist = new Set(["hasLeakedBiliToolsOnly", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); const allowlist = new Set(["hasLeakedBiliToolsOnly", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); let sawImport = false; for (const m of text.matchAll(/\{([^}]*)\}\s*from\s*["'][^"']*side-request\.js["']/g)) { sawImport = true; for (const raw of m[1].split(",")) { diff --git a/tests/side-request-isolation.test.ts b/tests/side-request-isolation.test.ts index cd3584411f..e14cdc10bd 100644 --- a/tests/side-request-isolation.test.ts +++ b/tests/side-request-isolation.test.ts @@ -3,7 +3,8 @@ import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; import { defaultConfig, createInitialState, defaultCountTokens } from "acp-kernel"; -import { startServer, isSideRequest, outputBudgetField, restoreOutputBudget, sideRequestGuard, resolveKnownOutputCeiling, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR } from "../src/server.ts"; +import { startServer, isSideRequest, isStructuredOutputCall, outputBudgetField, restoreOutputBudget, sideRequestGuard, resolveKnownOutputCeiling, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR } from "../src/server.ts"; +import { resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; import type { ProxyOptions } from "../src/config.ts"; import { recordPluginRuntimeInfo, _resetPluginStateForTest } from "../src/plugin.ts"; import { estimateRawBodyTokens, estimateRawBodyBreakdown } from "../src/preflight.ts"; @@ -56,6 +57,30 @@ test("isSideRequest: a toolless structured-output request is a side request", () assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "json_object" } }, "main"), false, "explicit main persona outranks the shape"); }); +test("isStructuredOutputCall: the predicate is self-contained for the restoreOutputBudget skip (#2500 parity)", () => { + // handle.ts consults this predicate independently of the #546 guard, so the + // no-tools veto lives IN the predicate — a tool-carrying structured request + // must never be demoted into the high-water skip by a standalone caller. + assert.equal(isStructuredOutputCall({ max_tokens: 64000, response_format: { type: "json_object" } }), true, "toolless chat json_object"); + assert.equal(isStructuredOutputCall({ max_output_tokens: 64000, text: { format: { type: "json_schema" } } }), true, "toolless responses text.format"); + assert.equal(isStructuredOutputCall({ max_tokens: 64000, response_format: { type: "json_object" }, tools: [{}] }), false, "any tool entry vetoes (#546)"); + assert.equal(isStructuredOutputCall({ max_tokens: 64000, response_format: { type: "text" } }), false, "plain text format does not count"); + assert.equal(isStructuredOutputCall(null), false, "null body"); + assert.equal(isStructuredOutputCall("nope"), false, "non-object body"); +}); + +test("resolveSideLane: the #5073 label names the structured-output shape in the log line", () => { + assert.equal( + resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "openai", stripApplied: false, sideIntent: true, requestAgent: undefined, sideLabel: "structured-output utility call (#5073)" }).reason, + "structured-output utility call (#5073)", + ); + // Without a label the historical reason strings stay byte-stable. + assert.equal( + resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "openai", stripApplied: false, sideIntent: true, requestAgent: undefined }).reason, + `max_tokens<=${SIDE_REQUEST_MAX_TOKENS}`, + ); +}); + test("isSideRequest: host-declared side-request agent outranks the token-budget heuristic (#1699)", () => { // opencode v2 title-gen carries NO max_tokens (options {} for kind==="title"), // so the budget path can never see it. Declaring the persona by intent fixes @@ -806,7 +831,7 @@ test("e2e: opencode v2 title-gen is classified by intent, not token budget (#169 } }); -test("e2e: a toolless structured-output request is forwarded without injected tools", async () => { +test("e2e: a toolless structured-output request rides the #388 passthrough — no injection, no high-water poison (#5073)", async () => { const rig = await startRig(); try { const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; @@ -815,22 +840,44 @@ test("e2e: a toolless structured-output request is forwarded without injected to { role: "system", content: "Extract facts from the conversation." }, { role: "user", content: "User: I moved to Lisbon last spring." }, ]; - // Control: the same shape without response_format is a main turn and gets the wire tools. - const rControl = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, stream: true, max_tokens: 64000, messages }) }); + // Main turn: the same toolless shape WITHOUT response_format is a real + // turn — it gets the ACP tools injected and learns a 1024 high-water. + // That is the value the side call's 64000 budget must NOT overwrite + // (the #1897/#2500 poisoning class). + const rControl = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, stream: true, max_tokens: 1024, messages }) }); assert.equal(rControl.status, 200); await rControl.text(); const controlTools = (rig.lastBody as { tools?: unknown[] } | null)?.tools; assert.ok(Array.isArray(controlTools) && controlTools.length > 0, "control: a toolless main turn gets the ACP tools injected"); - + const s1 = getSession(SESSION); + assert.ok(s1, "session exists after the main request"); + assert.equal(s1.metadata.outputBudgetHighWater, 1024, "high-water learned from the healthy main turn"); + const stateAfterMain = JSON.stringify(s1.state); + const statsAfterMain = JSON.stringify(s1.stats); + const snapshotAfterMain = JSON.stringify(s1.lastMessages); + const requestsAfterMain = s1.stats.requests; + + // The Hindsight fact-extraction shape: no tools, response_format, a 64000 + // budget that would seed the high-water were restoreOutputBudget not skipped. const body = { model: MODEL, stream: true, max_tokens: 64000, messages, response_format: { type: "json_object" } }; const rFix = await fetch(url, { method: "POST", headers, body: JSON.stringify(body) }); assert.equal(rFix.status, 200); await rFix.text(); - const fwd = rig.lastBody as { tools?: unknown; messages?: unknown; response_format?: unknown } | null; + const fwd = rig.lastBody as { tools?: unknown; messages?: unknown; response_format?: unknown; max_tokens?: unknown } | null; assert.ok(fwd, "upstream received the request"); assert.equal(fwd?.tools, undefined, "no tools injected beside response_format"); - assert.deepEqual(fwd?.messages, messages, "messages forwarded verbatim"); + assert.deepEqual(fwd?.messages, messages, "messages forwarded verbatim (no prompt/render tags injected)"); assert.deepEqual(fwd?.response_format, body.response_format, "response_format untouched"); + assert.equal(fwd?.max_tokens, 64000, "utility-call budget must NOT be mutated by restoreOutputBudget"); + + // Kernel state, stats, snapshot, counter and high-water all untouched (#388). + const s2 = getSession(SESSION); + assert.ok(s2, "session still exists after the side request"); + assert.equal(JSON.stringify(s2.state), stateAfterMain, "side request must NOT mutate kernel state"); + assert.equal(JSON.stringify(s2.stats), statsAfterMain, "side request must NOT mutate stats"); + assert.equal(JSON.stringify(s2.lastMessages), snapshotAfterMain, "side request must NOT clobber the message snapshot"); + assert.equal(s2.stats.requests, requestsAfterMain, "side request must NOT increment the main request counter"); + assert.equal(s2.metadata.outputBudgetHighWater, 1024, "the 64000 utility budget must NOT seed the high-water (#1897/#2500 class)"); } finally { await closeRig(rig); } From 69ca2dc9d14e3e81b1262bba6f6946a2aea8886f Mon Sep 17 00:00:00 2001 From: Evan Light Date: Thu, 29 Oct 2026 03:54:27 -0400 Subject: [PATCH 20/78] fix(hermes): route a plain-http provider base_url through the proxy (#5116) The native plugin set only HTTPS_PROXY, so a plain-http base_url (a LAN llama.cpp, vLLM or ollama server) bypassed the proxy and its sessions were never compressed. apply_env now sets the HTTP pair too and defaults NO_PROXY to loopback, matching the launcher's own plain-http routes. All of them sit in _ENV_ROUTED_KEYS, so the #4644 watchdog unroutes and restores them along with the HTTPS pair. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MMUmTHLFoNYr4iAsprTFfk (cherry picked from commit 461504c31bbdc5a3f2c7376db383e444c85fb9a0) --- hermes-plugin/__init__.py | 13 ++++++++++++- tests/hermes-native.test.ts | 24 +++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/hermes-plugin/__init__.py b/hermes-plugin/__init__.py index 3b3ccb8867..c9e04e4d8d 100644 --- a/hermes-plugin/__init__.py +++ b/hermes-plugin/__init__.py @@ -1211,7 +1211,11 @@ def ensure_origin(sidecar: Dict[str, str], deadline: Optional[float] = None) -> # — runtime effects ------------------------------------------------------------- # The exact keys apply_env routes; _unroute_env restores them one-for-one on origin death (#4644). -_ENV_ROUTED_KEYS = ("HTTPS_PROXY", "https_proxy", "HERMES_CA_BUNDLE", "SSL_CERT_FILE") +# The HTTP pair and NO_PROXY are here too, so a plain-http provider is unrouted with the rest. +_ENV_ROUTED_KEYS = ("HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", + "NO_PROXY", "no_proxy", "HERMES_CA_BUNDLE", "SSL_CERT_FILE") +# Loopback stays direct, as the launcher's own plain-http routes do (LAUNCHER_LOOPBACK_NO_PROXY). +_LOOPBACK_NO_PROXY = "127.0.0.1,localhost,::1" def apply_env(origin: str, sidecar: Optional[Dict[str, str]] = None, ctx: Any = None) -> bool: @@ -1231,6 +1235,13 @@ def apply_env(origin: str, sidecar: Optional[Dict[str, str]] = None, ctx: Any = _state["env_backup"] = {key: os.environ.get(key) for key in _ENV_ROUTED_KEYS} os.environ["HTTPS_PROXY"] = origin os.environ["https_proxy"] = origin + # A plain-http provider base_url (a LAN llama.cpp / vLLM / ollama server) is only routed + # by HTTP_PROXY; with the HTTPS pair alone its traffic bypasses the proxy entirely and + # the session gets no compression. The proxy forwards absolute-form http requests. + os.environ["HTTP_PROXY"] = origin + os.environ["http_proxy"] = origin + os.environ.setdefault("NO_PROXY", _LOOPBACK_NO_PROXY) + os.environ.setdefault("no_proxy", _LOOPBACK_NO_PROXY) ca_dir = data_dir() / "ca" root = ca_dir / "root-ca.pem" if root.is_file(): diff --git a/tests/hermes-native.test.ts b/tests/hermes-native.test.ts index 378ca1d767..2c3c79dc26 100644 --- a/tests/hermes-native.test.ts +++ b/tests/hermes-native.test.ts @@ -227,7 +227,7 @@ os.environ.pop("BILI_CONFIG_FILE", None) for k in ("BILLION_CONTEXT_ATTACH", "BILLION_CONTEXT_PROXY", "BILI_NATIVE_HERMES", "BILLION_CONTEXT_PLUGIN", "BILI_PROVIDER_REWRITES", "HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", - "ALL_PROXY", "all_proxy", "HERMES_CA_BUNDLE", "SSL_CERT_FILE"): + "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy", "HERMES_CA_BUNDLE", "SSL_CERT_FILE"): os.environ.pop(k, None) RECORDED = {"tool": [], "runtime_info": [], "watcher": [], "health": [], "status": []} @@ -939,17 +939,21 @@ elif SCENARIO == "origin-death": ready.wait(10) out["death_initial_ready"] = (mod._state.get("bootstrap") or {}).get("state") out["death_env_routed"] = os.environ.get("HTTPS_PROXY") + out["death_http_routed"] = os.environ.get("HTTP_PROXY") SLOW["enabled"] = True def release_later(): __import__("time").sleep(1.5) SLOW["release"].set() threading.Thread(target=release_later, daemon=True).start() gap_values = [] + http_gap = [] saw_gap = False end = __import__("time").monotonic() + 20 while __import__("time").monotonic() < end: v = os.environ.get("HTTPS_PROXY") gap_values.append(v) + if v != ORIGIN: + http_gap.append(os.environ.get("HTTP_PROXY")) if v != ORIGIN: saw_gap = True rec = mod._state.get("bootstrap") or {} @@ -960,6 +964,8 @@ elif SCENARIO == "origin-death": rec = mod._state.get("bootstrap") or {} out["death_final_state"] = rec.get("state") out["death_final_env"] = os.environ.get("HTTPS_PROXY") + out["death_http_gap"] = list(dict.fromkeys(http_gap)) + out["death_final_http"] = os.environ.get("HTTP_PROXY") out["death_tools"] = sorted(ctx.tools.keys()) out["death_register_calls"] = sum(ctx.reg_counts.values()) out["death_warnings"] = [w for w in WARNINGS if "is gone" in w] @@ -1119,6 +1125,9 @@ out.update({ "pid": os.getpid(), "env_https_proxy": os.environ.get("HTTPS_PROXY"), "env_https_proxy_lc": os.environ.get("https_proxy"), + "env_http_proxy": os.environ.get("HTTP_PROXY"), + "env_http_proxy_lc": os.environ.get("http_proxy"), + "env_no_proxy": os.environ.get("NO_PROXY"), "env_ca_bundle": os.environ.get("HERMES_CA_BUNDLE"), "env_ssl_cert_file": os.environ.get("SSL_CERT_FILE"), "marker_left": os.path.exists(os.path.join(os.environ["XDG_STATE_HOME"], "billion-context", "proxy-starting")), @@ -1147,6 +1156,9 @@ interface DriverOut { hooks: string[]; env_https_proxy?: string | null; env_https_proxy_lc?: string | null; + env_http_proxy?: string | null; + env_http_proxy_lc?: string | null; + env_no_proxy?: string | null; env_ca_bundle?: string | null; env_ssl_cert_file?: string | null; marker_left?: boolean; @@ -1239,6 +1251,9 @@ interface DriverOut { death_gap_values?: Array; death_final_state?: string; death_final_env?: string | null; + death_http_routed?: string | null; + death_http_gap?: Array; + death_final_http?: string | null; death_tools?: string[]; death_register_calls?: number; death_warnings?: string[]; @@ -1297,6 +1312,9 @@ describe("python plugin runtime (subprocess)", () => { assert.equal(out!.req_unmutated, true); assert.equal(out!.env_https_proxy, out!.origin); assert.equal(out!.env_https_proxy_lc, out!.origin); + assert.equal(out!.env_http_proxy, out!.origin, "a plain-http provider base_url is routed too"); + assert.equal(out!.env_http_proxy_lc, out!.origin); + assert.equal(out!.env_no_proxy, "127.0.0.1,localhost,::1", "loopback stays direct"); assert.match(out!.env_ca_bundle!, /root-ca\.pem$/); assert.match(out!.env_ssl_cert_file!, /combined-ca\.pem$/, "#1375: ambient trust rides the combined bundle"); const h1 = out!.first_headers!; @@ -1740,6 +1758,10 @@ describe("python plugin runtime (subprocess)", () => { assert.ok(out, err); assert.equal(out!.death_initial_ready, "ready"); assert.equal(out!.death_env_routed, out!.origin); + assert.equal(out!.death_http_routed, out!.origin); + assert.ok(out!.death_http_gap!.length > 0 && !out!.death_http_gap!.includes(out!.origin), + "HTTP_PROXY is unrouted with the HTTPS pair, not left pointing at the dead origin"); + assert.equal(out!.death_final_http, out!.origin); assert.ok(out!.death_gap_values!.includes("http://user-proxy.example:9999"), "the unroute RESTORED the pre-existing user proxy instead of deleting the key"); assert.equal(out!.death_final_state, "ready", "the watchdog re-bootstrapped a healthy origin"); From f7406e5ad9bb937cd2712c5547eb0396ead91a08 Mon Sep 17 00:00:00 2001 From: rain Date: Fri, 30 Oct 2026 00:07:53 +0800 Subject: [PATCH 21/78] sync: decide block liveness by coverage shape, not existence A compressed block's summary may only be injected while it still describes content on the wire. The current predicate asks whether ANY covered message is still present, so a host rewind that truncates part of a span keeps the whole summary alive and the model reads a description of history that no longer exists. Reap (#2695) does not catch it either: it needs a strict majority of covered ids absent for three consecutive passes, and a partially truncated span keeps its majority. The gap's shape, in the block's own coverage order, is the discriminator: - survivors a prefix, missing the newest end -> the rewind shape; deactivate - survivors a suffix, missing the oldest end -> the host dropped old history; the summary is the only record of it, so keep it active - interior/ragged holes -> the id-churn shape (ids are content hashes); keep it and let fold-reconcile re-key the churned ids - nothing covered visible -> defer to the positional witness (#4316, unchanged): a surviving ledger position is the churn / merged-pair shape, not a deletion Everything except the tail gap keeps today's keep-alive behaviour, so the only behaviour that changes is the shape this change names. The positional witness is not touched: narrowing it to exact coverage would false-kill merged content (a pair collapse covers fewer ids than positions). Tests: sync-config gains the four shapes plus the positional-witness composition and the no-witness deletion case. (cherry picked from commit 7c4d4b92394d9aa3597f3e1d8497ac4d9b7e18c2) --- kernel/src/sync.ts | 80 +++++++++++++++++++++++++---- kernel/tests/sync-config.test.ts | 88 ++++++++++++++++++++++++++++++-- 2 files changed, 154 insertions(+), 14 deletions(-) diff --git a/kernel/src/sync.ts b/kernel/src/sync.ts index 87008df298..b1e462908a 100644 --- a/kernel/src/sync.ts +++ b/kernel/src/sync.ts @@ -5,7 +5,13 @@ import type { CompressionState, CoreMessage } from "./types.js"; * inside the block's exact interval claim. Unbounded blocks and unstamped * views answer false — the id path decides those. The -1/-1 "no claim" * sentinel (PR #4725 N1) also answers false: it is absence of evidence, not - * a positional claim (a [0,0] row IS a real claim over seq 0). */ + * a positional claim (a [0,0] row IS a real claim over seq 0). + * + * Kept exactly as upstream states it: when NO covered id survives, a position + * inside the interval is far more likely id churn (re-mint, merged #2636 pair) + * than deletion, and a merge legitimately covers fewer ids than positions. The + * coverage-shape rule below only governs the shapes where ids are present. + */ function blockSeqPresent( block: { startSeq?: number; endSeq?: number }, messages: CoreMessage[], @@ -32,6 +38,33 @@ function blockSeqPresent( return false; } +type CoverageShape = "full" | "tail-gap" | "head-gap" | "holes" | "none"; + +/** Where the missing covered ids sit, in the block's own coverage order. + * + * - `tail-gap` survivors are a prefix, everything missing is the newest end: + * a rewind removed the span's tail -> the summary is stale. + * - `head-gap` survivors are a suffix, everything missing is the oldest end: + * the host dropped old history; the summary is the ONLY record + * of it, so dropping it too would be the actual loss. + * - `holes` missing cells are interior/ragged: the churn shape (client + * re-serialized history), never the rewind shape. Do not guess. + * - `none` nothing covered is visible; ordering evidence decides. + */ +function coverageShape( + block: { effectiveMessageIds: string[] }, + presentBases: Set, +): CoverageShape { + const present = block.effectiveMessageIds.map((id) => presentBases.has(baseIdOf(id))); + if (present.length === 0 || present.every(Boolean)) return "full"; + if (!present.some(Boolean)) return "none"; + const firstPresent = present.indexOf(true); + const lastPresent = present.lastIndexOf(true); + if (present.slice(0, lastPresent + 1).every(Boolean)) return "tail-gap"; + if (present.slice(firstPresent).every(Boolean)) return "head-gap"; + return "holes"; +} + export interface SyncResult { state: CompressionState; deactivated: string[]; @@ -178,15 +211,42 @@ export function syncBlocks( // (pruned view) is still present — the summary IS the block's visible // representation. Without this, hosts passing pruned views would lose // block activity every turn. - // #4316 layer 3: a block with an exact ledger interval is also still - // present when ANY visible message carries a ledgerSeq stamp inside the - // interval — the host restored/re-stamped the originals (fresh ids, same - // positions), and the positional claim survives the id churn that the - // id-membership check above just missed. - const stillPresent = - block.effectiveMessageIds.some((id) => presentBases.has(baseIdOf(id))) || - presentIds.has(summaryMessageId(block.blockId)) || - blockSeqPresent(block, messages); + // #4592: the question is COVERAGE, not existence. `some` answered "is any of + // it still there" and kept injecting a summary that described a half-rewound + // span; `every` answered "is all of it there" but treated id churn like a + // deletion. The shape of the gap decides: + // + // tail gap -> rewind (the host withdrew the newest end) -> stale, drop + // head gap -> the host dropped OLD history; the summary is the only + // record of it, so keep it (dropping it would lose content) + // holes -> id churn (interior gaps are never the rewind shape): + // keep it; `fold-reconcile` re-keys churned ids upstream + // none -> nothing id-shaped survived: defer to the positional + // witness (upstream #4316, unchanged), which is the churn / + // merged-pair shape rather than a deletion + // + // Everything except the tail gap keeps today's keep-alive behaviour, so the + // only behaviour that changes is the one shape that can be named. + const shape = coverageShape(block, presentBases); + let stillPresent: boolean; + if (presentIds.has(summaryMessageId(block.blockId))) { + // Pruned view: the summary IS the block's visible representation. + stillPresent = true; + } else { + switch (shape) { + case "full": + case "head-gap": + case "holes": + stillPresent = true; + break; + case "tail-gap": + stillPresent = false; + break; + case "none": + stillPresent = blockSeqPresent(block, messages); + break; + } + } if (!stillPresent) { block.active = false; deactivated.push(block.blockId); diff --git a/kernel/tests/sync-config.test.ts b/kernel/tests/sync-config.test.ts index b376e43a6b..0ebbbc524b 100644 --- a/kernel/tests/sync-config.test.ts +++ b/kernel/tests/sync-config.test.ts @@ -31,21 +31,101 @@ test("syncBlocks deactivates blocks whose messages are all gone", () => { const state = createInitialState(); state.blocks.push( makeBlock({ blockId: "b1", effectiveMessageIds: ["gone1", "gone2"] }), - makeBlock({ blockId: "b2", effectiveMessageIds: ["kept", "also-gone"] }), ); const result = syncBlocks([msg("kept")], state); assert.deepEqual(result.deactivated, ["b1"]); assert.equal(result.state.blocks[0]!.active, false); - assert.equal(result.state.blocks[1]!.active, true); }); -test("syncBlocks leaves blocks intact when at least one message remains", () => { +// #4592: upstream kept a block alive as long as SOME covered message remained. +// That is exactly the shape a host rewind leaves behind — the survivors are a +// prefix and the newest end is gone, so the summary describes history that no +// longer exists. Coverage SHAPE decides, not existence: see the head-gap and +// holes tests below for the shapes that must KEEP their summary. +test("syncBlocks deactivates a block whose span was partially truncated (rewind)", () => { + const state = createInitialState(); + state.blocks.push( + makeBlock({ blockId: "b1", effectiveMessageIds: ["kept", "also-gone"] }), + ); + const result = syncBlocks([msg("kept")], state); + assert.deepEqual(result.deactivated, ["b1"]); + assert.equal(result.state.blocks[0]!.active, false); +}); + +// #4592: the mirror shape of a rewind. The host dropped OLD history from the +// head; the summary is the only record of it, so dropping the summary too is the +// actual loss. This is what makes sliding-window / head-truncating hosts safe. +test("syncBlocks keeps a block whose oldest coverage the host dropped (head gap)", () => { + const state = createInitialState(); + state.blocks.push( + makeBlock({ blockId: "b1", effectiveMessageIds: ["old-1", "old-2", "kept"] }), + ); + const result = syncBlocks([msg("kept"), msg("fresh")], state); + assert.deepEqual(result.deactivated, []); + assert.equal(result.state.blocks[0]!.active, true); +}); + +// #4592: interior gaps are the id-churn shape (client re-serialized its +// history), never the rewind shape. Do not guess: keep the summary and let +// fold-reconcile re-key the churned ids. +test("syncBlocks keeps a block with interior id gaps (churn shape)", () => { + const state = createInitialState(); + state.blocks.push( + makeBlock({ blockId: "b1", effectiveMessageIds: ["a", "churned", "c"] }), + ); + const result = syncBlocks([msg("a"), msg("c")], state); + assert.deepEqual(result.deactivated, []); + assert.equal(result.state.blocks[0]!.active, true); +}); + +// #4592: nothing id-shaped survives, but a visible message carries a ledger +// position INSIDE the block's interval — the churn / merged-pair shape (a merge +// legitimately covers fewer ids than positions). The positional witness is +// upstream's #4316 rule, unchanged by the shape decision. +test("syncBlocks keeps a fully-gone block with a position inside its interval (churn witness)", () => { + const state = createInitialState(); + state.blocks.push( + makeBlock({ + blockId: "b1", + effectiveMessageIds: ["gone-1", "gone-2"], + startSeq: 1, + endSeq: 2, + }), + ); + const visible: CoreMessage[] = [ + { id: "re-minted", role: "user", contentType: "text", text: "merged", ledgerSeq: 2 }, + ]; + const result = syncBlocks(visible, state); + assert.deepEqual(result.deactivated, []); + assert.equal(result.state.blocks[0]!.active, true); +}); + +// #4592: no id and no position survives -> dead (the plain deletion shape). +test("syncBlocks deactivates a fully-gone block with no positional witness", () => { + const state = createInitialState(); + state.blocks.push( + makeBlock({ + blockId: "b1", + effectiveMessageIds: ["gone-1"], + startSeq: 1, + endSeq: 1, + }), + ); + const visible: CoreMessage[] = [ + { id: "newer", role: "user", contentType: "text", text: "newer", ledgerSeq: 9 }, + ]; + const result = syncBlocks(visible, state); + assert.deepEqual(result.deactivated, ["b1"]); + assert.equal(result.state.blocks[0]!.active, false); +}); + +test("syncBlocks leaves blocks intact when every covered message remains", () => { const state = createInitialState(); state.blocks.push( makeBlock({ blockId: "b1", effectiveMessageIds: ["a", "b"] }), ); - const result = syncBlocks([msg("a")], state); + const result = syncBlocks([msg("a"), msg("b")], state); assert.deepEqual(result.deactivated, []); assert.equal(result.state.blocks[0]!.active, true); }); From 1eeddbd6f9f5ff893782d4afa31fe171e1a82662 Mon Sep 17 00:00:00 2001 From: TimBriseby Date: Thu, 29 Oct 2026 22:27:55 +0100 Subject: [PATCH 22/78] fix: report nested transport timeout diagnostics without assuming idle expiry (cherry picked from commit e3a3cbdffe6e0c0a52e369aa66179244003b2a6c) --- CONFIGURATION.md | 1 - CONFIGURATION.zh-CN.md | 1 - src/upstream-fail.ts | 8 +- src/upstream-proxy.ts | 39 ++++-- tests/upstream-proxy-routing.test.ts | 3 +- tests/upstream-timeout-diagnostics.test.ts | 140 +++++++++++++++++++++ 6 files changed, 174 insertions(+), 18 deletions(-) delete mode 100644 CONFIGURATION.md delete mode 100644 CONFIGURATION.zh-CN.md create mode 100644 tests/upstream-timeout-diagnostics.test.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md deleted file mode 100644 index c9d7248ed4..0000000000 --- a/CONFIGURATION.md +++ /dev/null @@ -1 +0,0 @@ -> Moved to [docs/configuration.md](docs/configuration.md). Redirect stub; scheduled for removal after 1–2 releases. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md deleted file mode 100644 index e136ffdd77..0000000000 --- a/CONFIGURATION.zh-CN.md +++ /dev/null @@ -1 +0,0 @@ -> 已迁移至 [docs/configuration.zh-CN.md](docs/configuration.zh-CN.md)。重定向占位,1–2 个版本后删除。 diff --git a/src/upstream-fail.ts b/src/upstream-fail.ts index c1214744da..3f870361cd 100644 --- a/src/upstream-fail.ts +++ b/src/upstream-fail.ts @@ -26,9 +26,9 @@ export type UpstreamFailureKind = /** Downstream client disconnected (external abort fired). Nothing bili * did or can retry — the request is dead by definition. */ | "client-abort" - /** Idle-budget / read-phase timeout expiry: our own watchdog aborted, or - * OS/undici reported a headers/body timeout. NOT retried — the budget - * already waited and the request may have reached the upstream. */ + /** Timeout: our idle watchdog, an undici headers/body timeout, or an OS + * ETIMEDOUT whose phase may be unknown. NOT retried — the request may + * have reached the upstream. This category alone does not prove idle expiry. */ | "upstream-timeout" /** Phase-1 FIRST-BYTE timeout: the operator tightened the first-byte budget * (network.upstreamFirstByteMs / BILI_UPSTREAM_FIRST_BYTE_MS below the idle @@ -266,7 +266,7 @@ export function isFailFastUpstreamKind(kind: UpstreamFailureKind): boolean { * taxonomy and the checklist never drift apart. */ export const UPSTREAM_FAIL_HINTS: Record = { "client-abort": "downstream client disconnected — no bili-side action", - "upstream-timeout": "idle budget expired (headers/body) — check upstream health; not retried by design", + "upstream-timeout": "transport timeout — inspect code/syscall and nested causes to identify the phase; not retried by design", "upstream-headers-timeout": "no first response byte within the explicitly-tightened first-byte budget (network.upstreamFirstByteMs / BILI_UPSTREAM_FIRST_BYTE_MS) — zero bytes were received, so a bounded transparent replay may be attempted (BILI_REPLAY_RETRY_MAX); the mid-stream (body) idle budget is unchanged", "connect-timeout": "TCP handshake never completed — upstream/proxy unreachable or blackholed; a bounded transparent replay may be attempted (BILI_REPLAY_RETRY_MAX)", "upstream-unreachable": "ICMP no-route rejection (EHOSTUNREACH/ENETUNREACH/EHOSTDOWN) — nothing reached the upstream; check routing/firewall/ACL (on macOS also the Local Network privacy grant for the app that owns the bili process, #5663); a bounded transparent replay may be attempted (BILI_REPLAY_RETRY_MAX)", diff --git a/src/upstream-proxy.ts b/src/upstream-proxy.ts index ebe3947506..1339299b11 100644 --- a/src/upstream-proxy.ts +++ b/src/upstream-proxy.ts @@ -4,7 +4,7 @@ import tls from "node:tls"; import { Pool, ProxyAgent } from "undici"; import type { ProviderRoutes } from "./config.js"; import { log as loggerLog } from "./logger.js"; -import { maskHostInText, maskHostPortForDisplay, maskUrlForLog } from "./log-mask.js"; +import { maskHostInText, maskHostPortForDisplay, maskUrlForLog, maskIpsInText, maskUrlsInText, redactSecretsInText } from "./log-mask.js"; import { upstreamTimeoutMs } from "./fetch-util.js"; import { classifyUpstreamFailure, hintWithSkippedProxy, isTlsTrustCode, redactProxyUrl, tlsTrustRemediation, type SkippedProxyInfo, type UpstreamFailureDetail } from "./upstream-fail.js"; import { upstreamCaIdentity, upstreamExtraCa } from "./upstream-ca.js"; @@ -537,22 +537,33 @@ export function connectThroughProxy(host: string, port: number, proxyUrl: string function errorChain(error: unknown): Array> { const chain: Array> = []; const seen = new Set(); - let current: unknown = error; - while (current && !seen.has(current)) { + // Bound both depth and visits so large or cyclic aggregates cannot flood diagnostics. + let remaining = 32; + let truncated = false; + const visit = (current: unknown, depth: number): void => { + if (remaining-- <= 0 || depth >= 8) { truncated = true; return; } + if (!current || seen.has(current)) return; seen.add(current); if (current instanceof Error) { - const record: Record = { message: current.message }; + const record: Record = { message: String(current.message) }; for (const key of ["code", "errno", "syscall", "address", "port"]) { const value = (current as unknown as Record)[key]; - if (value !== undefined) record[key] = value; + if (typeof value === "string" || typeof value === "number") record[key] = value; } chain.push(record); - current = current.cause; + if (current.cause !== undefined) visit(current.cause, depth + 1); + if (current instanceof AggregateError && Array.isArray(current.errors)) { + for (const child of current.errors) { + if (remaining <= 0) { truncated = true; break; } + visit(child, depth + 1); + } + } } else { chain.push({ message: String(current) }); - break; } - } + }; + visit(error, 0); + if (truncated) chain.push({ message: "[error chain truncated]" }); return chain; } @@ -579,9 +590,9 @@ export function describeUpstreamFailure(error: unknown, url: string, proxyUrl?: try { targetHostPort = new URL(url).host; } catch { /* not a URL — no display host */ } const maskHostIn = (s: string): string => { try { - return maskHostInText(s, new URL(url).hostname); - } catch { /* not a URL — nothing to mask */ } - return s; + s = maskHostInText(s, new URL(url).hostname); + } catch { /* not a URL — no target host to mask */ } + return redactSecretsInText(maskUrlsInText(maskIpsInText(s))); }; let codeValue: unknown; for (const field of fields) { @@ -593,6 +604,12 @@ export function describeUpstreamFailure(error: unknown, url: string, proxyUrl?: } const messages = chain.map((entry) => maskHostIn(String(entry.message ?? ""))).filter(Boolean); if (messages.length > 0) parts.push(`message=${messages.join(" <- ")}`); + if (chain.length > 1) { + const causes = chain.slice(1).map((entry) => Object.fromEntries( + Object.entries(entry).map(([key, value]) => [key, typeof value === "string" ? maskHostIn(value) : value]), + )); + parts.push(`causes=${JSON.stringify(causes)}`); + } parts.push(`url=${maskUrlForLog(url)}`); parts.push(`proxy=${redactProxyUrl(proxyUrl) ?? "direct"}`); let hint = hintWithSkippedProxy(kind, skipped); diff --git a/tests/upstream-proxy-routing.test.ts b/tests/upstream-proxy-routing.test.ts index 46a0602bb7..441dc8f70d 100644 --- a/tests/upstream-proxy-routing.test.ts +++ b/tests/upstream-proxy-routing.test.ts @@ -442,7 +442,8 @@ test("upstream failures expand nested causes with redacted proxy context", () => "https://chatgpt.com/backend-api/codex/responses", "http://user:secret@proxy.example:8080", ); - for (const expected of ["code=UND_ERR_CONNECT_TIMEOUT", "errno=-4039", "syscall=connect", "address=203.0.113.7", "port=443", "message=fetch failed <- connect timed out", "url=https://chatgpt.com/backend-api/codex/responses", "proxy=http://***:***@proxy.example:8080/"]) { + for (const expected of ["code=UND_ERR_CONNECT_TIMEOUT", "errno=-4039", "syscall=connect", "address=", "port=443", "message=fetch failed <- connect timed out", "url=https://chatgpt.com/backend-api/codex/responses", "proxy=http://***:***@proxy.example:8080/"]) { assert.ok(detail.includes(expected), expected); } + assert.ok(!detail.includes("203.0.113.7"), "nested addresses are masked before reaching client-facing diagnostics"); }); diff --git a/tests/upstream-timeout-diagnostics.test.ts b/tests/upstream-timeout-diagnostics.test.ts new file mode 100644 index 0000000000..550426c3da --- /dev/null +++ b/tests/upstream-timeout-diagnostics.test.ts @@ -0,0 +1,140 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { describeUpstreamFailure } from "../src/upstream-proxy.js"; +import { classifyUpstreamFailure, buildProxyFailureResponse, withUpstreamFailure } from "../src/upstream-fail.js"; +import { fetchWithRetry, fetchWithTransportRetry } from "../src/fetch-util.js"; +import { setMaskHostsEnabled } from "../src/log-mask.js"; + +function connectError(code: string, address: string, message = ""): Error { + return Object.assign(new Error(message), { code, errno: -110, syscall: "connect", address, port: 443 }); +} + +function familyFailure(): TypeError { + const aggregate = Object.assign(new AggregateError([ + connectError("ETIMEDOUT", "192.0.2.1"), + connectError("ENETUNREACH", "2001:db8::1"), + ], ""), { code: "ETIMEDOUT" }); + return new TypeError("fetch failed", { cause: aggregate }); +} + +test("ETIMEDOUT diagnostics do not claim that the idle watchdog expired", () => { + for (const error of [connectError("ETIMEDOUT", "192.0.2.1"), familyFailure()]) { + const parts = describeUpstreamFailure(error, "https://chatgpt.com/backend-api/codex/responses"); + assert.equal(parts.detail.kind, "upstream-timeout"); + assert.doesNotMatch(parts.detail.hint!, /idle budget expired/); + assert.match(parts.detail.hint!, /phase/); + assert.match(parts.detail.hint!, /not retried/); + } +}); + +test("aggregate diagnostics retain every address attempt with its own fields", () => { + setMaskHostsEnabled(false); + try { + const parts = describeUpstreamFailure(familyFailure(), "https://chatgpt.com/backend-api/codex/responses"); + assert.match(parts.message, /ENETUNREACH/); + assert.match(parts.message, /2001:db8::1/); + const causes = JSON.parse(parts.message.match(/causes=(\[.*\]) url=/)![1]!); + assert.ok(causes.some((entry: Record) => entry.code === "ETIMEDOUT" && entry.syscall === "connect" && entry.address === "192.0.2.1" && entry.port === 443)); + assert.ok(causes.some((entry: Record) => entry.code === "ENETUNREACH" && entry.address === "2001:db8::1")); + } finally { + setMaskHostsEnabled(true); + } +}); + +test("nested causes are masked before appearing in the client-facing 502 body", () => { + const secret = "sk-example-secret-for-diagnostics"; + const child = connectError("ETIMEDOUT", "192.0.2.1", `connect relay.internal 192.0.2.1 https://user:password@relay.internal/path?token=${secret} Bearer ${secret}`); + const error = new TypeError("fetch failed", { cause: new AggregateError([child], "") }); + const parts = describeUpstreamFailure(error, "https://relay.internal/v1/messages", "http://user:password@127.0.0.1:8080"); + const response = buildProxyFailureResponse(parts.message, withUpstreamFailure(new Error(parts.message), parts.detail)); + assert.equal(response.status, 502); + assert.match(response.body, /ETIMEDOUT/); + assert.match(response.body, /connect/); + for (const value of [secret, "relay.internal", "192.0.2.1", "user:password"]) assert.ok(!response.body.includes(value), value); +}); + +test("aggregate traversal handles cyclic causes and shared children once", () => { + const child = connectError("ENETUNREACH", "127.0.0.1", "unique-child"); + const aggregate = new AggregateError([child, child], "root"); + aggregate.cause = aggregate; + const parts = describeUpstreamFailure(aggregate, "https://chatgpt.com/backend-api/codex/responses"); + assert.match(parts.message, /ENETUNREACH/); + const causes = JSON.parse(parts.message.match(/causes=(\[.*\]) url=/)![1]!); + assert.equal(causes.filter((entry: Record) => entry.message === "unique-child").length, 1); +}); + +test("aggregate diagnostic work and output are bounded for large trees", () => { + const errors = Array.from({ length: 200 }, (_, i) => connectError("ETIMEDOUT", "127.0.0.1", `attempt-${i}`)); + const parts = describeUpstreamFailure(new AggregateError(errors, "root"), "https://chatgpt.com/backend-api/codex/responses"); + assert.match(parts.message, /attempt-0/); + assert.doesNotMatch(parts.message, /attempt-199/); + assert.match(parts.message, /truncated/); + assert.ok(parts.message.length < 20_000); +}); + +test("deep causes and repeated aggregate children cannot bypass traversal limits", () => { + let deep: Error = new Error("deepest"); + for (let i = 0; i < 100; i++) deep = new Error(`level-${i}`, { cause: deep }); + assert.match(describeUpstreamFailure(deep, "https://chatgpt.com/responses").message, /truncated/); + const child = new Error("shared"); + const repeated = new AggregateError(Array.from({ length: 200 }, () => child), "root"); + assert.match(describeUpstreamFailure(repeated, "https://chatgpt.com/responses").message, /truncated/); +}); + +test("non-scalar error metadata cannot break diagnostic serialization", () => { + const circular: Record = {}; + circular.self = circular; + const child = Object.assign(new Error("child"), { code: "ETIMEDOUT", address: circular, port: 443 }); + const parts = describeUpstreamFailure(new AggregateError([child], "root"), "https://chatgpt.com/responses"); + assert.match(parts.message, /ETIMEDOUT/); + assert.doesNotMatch(parts.message, /\\"self\\"/); +}); + +test("malformed aggregate containers do not replace the original failure", () => { + for (const errors of [null, 42, {}]) { + const aggregate = Object.assign(new AggregateError([], "original failure"), { errors, code: "ETIMEDOUT" }); + const parts = describeUpstreamFailure(aggregate, "https://chatgpt.com/responses"); + assert.match(parts.message, /original failure/); + assert.equal(parts.detail.code, "ETIMEDOUT"); + } +}); + +test("falsy aggregate children still consume the visit budget", () => { + let visited = 0; + const children = Array.from({ length: 200 }, () => null); + for (let i = 0; i < children.length; i++) { + Object.defineProperty(children, i, { get() { visited++; return null; } }); + } + const aggregate = new AggregateError([], "root"); + aggregate.errors = children; + const bounded = describeUpstreamFailure(aggregate, "https://chatgpt.com/responses"); + assert.match(bounded.message, /truncated/); + assert.ok(visited <= 32, String(visited)); +}); + +test("redacted causes remain valid JSON with quoted URLs and backslashes", () => { + const child = new Error('failed "https://api.openai.com/v1" in C:\\tmp\\file'); + const parts = describeUpstreamFailure(new AggregateError([child], "root"), "https://chatgpt.com/responses"); + const causes = JSON.parse(parts.message.match(/causes=(\[.*\]) url=/)![1]!); + assert.match(causes[0].message, /failed/); + assert.ok(causes[0].message.includes("C:\\tmp\\file")); +}); + +test("diagnostic improvements preserve conservative timeout classification and retry behavior", async () => { + for (const code of ["UND_ERR_HEADERS_TIMEOUT", "UND_ERR_BODY_TIMEOUT"]) { + assert.equal(classifyUpstreamFailure(Object.assign(new Error("timeout"), { code })), "upstream-timeout"); + } + const original = globalThis.fetch; + try { + for (const send of [fetchWithRetry, fetchWithTransportRetry]) { + for (const error of [connectError("ETIMEDOUT", "127.0.0.1"), familyFailure()]) { + let attempts = 0; + globalThis.fetch = async () => { attempts++; throw error; }; + await assert.rejects(send("https://chatgpt.com/backend-api/codex/responses", { method: "POST", body: "{}" }, 5000, undefined), (actual) => actual === error); + assert.equal(attempts, 1); + } + } + } finally { + globalThis.fetch = original; + } +}); From 592dee884757689b25d5189ca7595b00591663b8 Mon Sep 17 00:00:00 2001 From: LkxPro Date: Sun, 1 Nov 2026 03:57:37 +0800 Subject: [PATCH 23/78] fix(loop): classify proxy tool failures by status line (cherry picked from commit c5ea1e1babe669e8074add68ce7865c7522811ef) --- src/loop/core.ts | 17 +++++++--- tests/plugin-protocol.test.ts | 61 ++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/loop/core.ts b/src/loop/core.ts index 21796da744..03a7bf83bb 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -188,6 +188,13 @@ function isReasoningRequiredError(e: unknown): boolean { return e instanceof UpstreamHttpError && /must be passed back/i.test(e.body); } +function proxyToolResultFailed(result: string): boolean { + // A successful compression receipt embeds the model-authored summary in later fingerprint + // lines. That summary may legitimately quote an earlier FAILED tool result; only the status + // line is authoritative for whether this tool call itself failed. + return (result.trimStart().split("\n", 1)[0] ?? "").includes("FAILED"); +} + // #1453 client-facing labels for transport failures that end the turn in-band. // Deliberately kind-level only: raw error text can embed endpoint hostnames/IPs, // which must never reach the client stream (the full masked chain goes to the @@ -1616,7 +1623,7 @@ export async function* runCompressLoop( if (threadContinue) threadDelta.push(injection); } const anyCompressFailed = proxyResults.some( - (pr) => (pr.name === "compress" || pr.name === "decompress") && pr.result.includes("FAILED"), + (pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result), ); if (!ctx.textProtocol && !anyCompressFailed) { const hidden = hideConsumedCompressCalls(ctx.session.state, coreMessages, ctx.config.isCompressTool); @@ -1637,7 +1644,7 @@ export async function* runCompressLoop( const absorbName = activeAbsorbToolName(ctx.session, ctx.config); if ( ctx.refreshFolded && - proxyResults.some((pr) => (pr.name === "compress" || pr.name === absorbName) && !pr.result.includes("FAILED")) + proxyResults.some((pr) => (pr.name === "compress" || pr.name === absorbName) && !proxyToolResultFailed(pr.result)) ) { try { const refreshed = await ctx.refreshFolded(coreMessages); @@ -1727,7 +1734,7 @@ export async function* runCompressLoop( // carry server-invisible bytes upstream — exactly the seam #4186 // forbids. const failedMutating = proxyResults.filter( - (pr) => (pr.name === "compress" || pr.name === "decompress") && pr.result.includes("FAILED"), + (pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result), ); if (!probeWarned) { @@ -1845,14 +1852,14 @@ export async function* runCompressLoop( let committedTokens = 0; let committedBlocks = 0; for (const pr of proxyResults) { - if (pr.name === "compress" && !pr.result.includes("FAILED")) { + if (pr.name === "compress" && !proxyToolResultFailed(pr.result)) { const saved = /~(\d[\d,]*) tokens saved/.exec(pr.result); if (saved) committedTokens += Number(saved[1]!.replace(/,/g, "")); const blocks = /(\d+) block\(s\)/.exec(pr.result); if (blocks) committedBlocks += Number(blocks[1]); } } - const absorbed = proxyResults.some((pr) => pr.name === activeAbsorbToolName(ctx.session, ctx.config) && !pr.result.includes("FAILED")); + const absorbed = proxyResults.some((pr) => pr.name === activeAbsorbToolName(ctx.session, ctx.config) && !proxyToolResultFailed(pr.result)); const note = committedTokens > 0 ? `compression committed (~${committedTokens.toLocaleString("en-US")} tokens saved${committedBlocks > 0 ? ` in ${committedBlocks} block(s)` : ""}); ` : absorbed ? "context work committed; " : ""; diff --git a/tests/plugin-protocol.test.ts b/tests/plugin-protocol.test.ts index 3deba4eb93..ead40e5411 100644 --- a/tests/plugin-protocol.test.ts +++ b/tests/plugin-protocol.test.ts @@ -65,8 +65,8 @@ function textScript(): string[] { ]; } -function compressToolScript(): string[] { - const compressArgs = JSON.stringify({ startId: "m00001", endId: "m00002", topic: "plugin-e2e-topic", summary: "plugin e2e compress summary" }); +function compressToolScript(summary = "plugin e2e compress summary with enough context about the folded range"): string[] { + const compressArgs = JSON.stringify({ startId: "m00001", endId: "m00002", topic: "plugin-e2e-topic", summary }); return [ anthropicSse("message_start", { type: "message_start", @@ -172,14 +172,15 @@ async function callPluginAnthropic( h: Harness, conversationId: string, messages: AnthropicMessage[], - opts: { tools?: unknown[]; stream?: boolean; contextWindow?: number } = {}, + opts: { tools?: unknown[]; stream?: boolean; contextWindow?: number; compressionProxy?: boolean; agent?: string } = {}, ): Promise<{ raw: string; events: SseEvent[]; json: Record | undefined }> { const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", - "x-bili-plugin": "pi-plugin/0.0.1", + "x-bili-plugin": opts.agent ?? "pi-plugin/0.0.1", "x-bili-plugin-conversation": conversationId, + ...(opts.compressionProxy ? { "x-bili-plugin-compression": "proxy" } : {}), ...(opts.contextWindow !== undefined ? { "x-bili-plugin-context-window": String(opts.contextWindow) } : {}), }, body: JSON.stringify({ @@ -848,6 +849,58 @@ test("plugin-reported context window becomes the nudge denominator and is visibl } }); +function sizedPluginHistory(): AnthropicMessage[] { + const headFiller = "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ".repeat(28); + const tailFiller = "enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat duis aute irure dolor in reprehenderit in voluptate. ".repeat(28); + const history: AnthropicMessage[] = []; + for (let i = 1; i <= 2; i++) { + history.push({ role: "user", content: `turn-${i}-marker question: ${headFiller}` }); + history.push({ role: "assistant", content: `turn-${i}-marker answer: ${headFiller}` }); + } + for (let i = 3; i <= 5; i++) { + history.push({ role: "user", content: `turn-${i} padding question: ${tailFiller}` }); + history.push({ role: "assistant", content: `turn-${i} padding answer: ${tailFiller}` }); + } + return history; +} + +test("plugin-bound proxy compression continues the tool loop instead of ending on the marker", async () => { + const h = await startHarness([compressToolScript(), textScript()], undefined, { visibilityMarkers: false }); + try { + const { raw } = await callPluginAnthropic(h, "20261031_032204_cec004da", sizedPluginHistory(), { + agent: "hermes", + tools: ["tool_search", "tool_describe", "tool_call"].map((name) => ({ + name, description: `Hermes ${name}`, input_schema: { type: "object", properties: {} }, + })), + compressionProxy: true, + }); + + assert.equal(h.captured.length, 2, `proxy compression must re-request the model after executing compress; raw=${raw}`); + assert.ok(raw.includes("plain answer"), `expected the continued answer, got: ${raw}`); + } finally { + await h.close(); + } +}); + +test("a successful compression summary containing FAILED still continues with markers hidden", async () => { + const summary = "summary records an earlier FAILED tool result while this compression itself succeeds"; + const h = await startHarness([compressToolScript(summary), textScript()], undefined, { visibilityMarkers: false }); + try { + const { raw } = await callPluginAnthropic(h, "plug-conv-failed-in-summary", sizedPluginHistory(), { + agent: "hermes", + tools: ["tool_search", "tool_describe", "tool_call"].map((name) => ({ + name, description: `Hermes ${name}`, input_schema: { type: "object", properties: {} }, + })), + compressionProxy: true, + }); + + assert.equal(h.captured.length, 2, `a success marker must not be classified by FAILED inside its summary; raw=${raw}`); + assert.ok(raw.includes("plain answer"), `expected the continued answer, got: ${raw}`); + } finally { + await h.close(); + } +}); + // Hermes defers plugin toolsets through tool_search, so a healthy plugin session sends requests // whose tools array carries NONE of the ACP names. Those requests must still bind the plugin // identity and budget (that is exactly what /acp reads) while compression falls back to the proxy — From 518f0cecd70440400a1217824affc7005b1dbd3c Mon Sep 17 00:00:00 2001 From: mz Date: Sun, 1 Nov 2026 17:08:57 +0300 Subject: [PATCH 24/78] fix(diagnostics): distinguish size rejections and retain bounded causes (cherry picked from commit 3896863d9cfceb0691aa353425d3113eab8311fe) --- src/server/rejection-diagnostics.ts | 120 ++++++++++++++++++ src/server/relay.ts | 25 +++- src/util.ts | 32 +++-- tests/issue4452-markerless-reject.test.ts | 47 +++++-- tests/rejection-diagnostics.test.ts | 147 ++++++++++++++++++++++ 5 files changed, 345 insertions(+), 26 deletions(-) create mode 100644 src/server/rejection-diagnostics.ts create mode 100644 tests/rejection-diagnostics.test.ts diff --git a/src/server/rejection-diagnostics.ts b/src/server/rejection-diagnostics.ts new file mode 100644 index 0000000000..0aa5e76576 --- /dev/null +++ b/src/server/rejection-diagnostics.ts @@ -0,0 +1,120 @@ +/** + * #4452: bounded terminal-cause diagnostics for a relayed upstream REJECTION. + * + * A relay/gateway that fans a request out across candidate targets may answer a + * total failure with ONE aggregate body carrying every per-target attempt, e.g.: + * + * { "error": { "message": "All targets failed", "type": "upstream_error" }, + * "attempts": [ { "target": "…", "details": { "error": { "message": "…" } } }, … ] } + * + * bili's own log line (`← upstream : `) keeps only the first + * 600 bytes of that body, so the operator sees the OUTER narrative — "all + * targets failed" — while the per-target cause sits further in. This module + * reads exactly ONE such nested cause (the last usable attempt's) when a body + * has that shape, reporting its attempt position rather than guessing causality. + * + * Hard bounds, deliberately: + * - ONE message, never a recursive walk: no `JSON.stringify` of the payload, no + * traversal of `cause`/`errors`/`attempts` chains, no echoing of the request + * body. Only one `attempts[].details.error.message`, with the aggregate + * accepted directly or beneath its standard `error` envelope. + * - Credential scrubbing goes through the shared log-mask sanitizer + * (`redactSecretsInText`), the SAME choke point the logger applies, so a + * provider that echoes the rejected key cannot smuggle it into the log. It is + * a high-signal credential scrubber, NOT a general privacy filter: it does not + * mask arbitrary URLs or paths, and the far stronger guarantee here is + * structural — only this one nested string is ever read, never a body dump. + * - Bounded to `MAX_DIAGNOSTIC_CHARS`; ALL whitespace collapsed so a newline + * (including the `\r\n` frame-injection shape) can never forge a second log + * field. + * - Absent, non-string, empty, or structurally-unrelated payloads return + * `undefined`: the caller then logs exactly what it logged before, so a + * rejection that carries no attempt detail is unchanged byte-for-byte. + * + * Recovery policy is NOT touched here — this module only reads a rejection + * body; every retry/refold/arm decision stays in the relay. + */ + +import { redactSecretsInText } from "../log-mask.js"; + +/** Upper bound on the retained cause text, independent of the outer snippet. */ +const MAX_DIAGNOSTIC_CHARS = 300; + +/** Do not even attempt to parse a body larger than this: an aggregate error + * reply is small (a few KB); a multi-megabyte body is a stream (or an HTML + * error page), not a JSON rejection envelope, and parsing it would cost the + * request more than the diagnostic is worth. */ +const MAX_PARSED_BYTES = 64 * 1024; + +interface DiagnosticResult { + /** The last usable attempt's nested cause, sanitized + bounded. */ + message: string; + /** 0-based index of the attempt the cause came from (for the log line). */ + attemptIndex: number; + /** How many attempts the aggregate body carried. */ + attempts: number; +} + +/** Collapse ALL whitespace (newlines included) and clamp to the bound. */ +function bound(text: string): string { + const collapsed = text.replace(/[\u0000-\u001f\u007f]/g, " ").replace(/\s+/g, " ").trim(); + if (collapsed.length <= MAX_DIAGNOSTIC_CHARS) return collapsed; + return `${collapsed.slice(0, MAX_DIAGNOSTIC_CHARS)} …`; +} + +/** Read `value[name]` only when `value` is a non-array object, so a nested + * field is reached without a generic record guard that would prove nothing + * about the field actually consumed here. */ +function child(value: unknown, name: string): unknown { + if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined; + return (value as Record)[name]; +} + +/** + * Read the single last-reported nested cause out of a relay rejection body. + * + * `payload` is the raw upstream error-body text (the same string the relay + * already buffers to detect an overflow). Returns `undefined` whenever there is + * no usable `attempts[].details.error.message`, leaving the caller's existing + * logging untouched. + * + * Selection rule: the LAST attempt that carries a non-empty + * `details.error.message`, scanning from the end. If the terminal attempt is + * silent, an earlier cause is reported with its actual position and is NOT + * labeled terminal: its existence does not establish why the last stage failed. + */ +export function rejectionDiagnostics(payload: string | null | undefined): DiagnosticResult | undefined { + if (typeof payload !== "string" || payload.length === 0) return undefined; + if (Buffer.byteLength(payload, "utf8") > MAX_PARSED_BYTES) return undefined; + const trimmed = payload.trim(); + // A rejection body must be a JSON object envelope — anything else (an HTML + // error page, a bare text message, an SSE frame) carries no attempts[]. + if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) return undefined; + let parsed: unknown; + try { + parsed = JSON.parse(trimmed); + } catch { + return undefined; + } + const attempts = child(parsed, "attempts") ?? child(child(parsed, "error"), "attempts"); + if (!Array.isArray(attempts) || attempts.length === 0) return undefined; + for (let i = attempts.length - 1; i >= 0; i--) { + const raw = child(child(child(attempts[i], "details"), "error"), "message"); + if (typeof raw !== "string") continue; + const sanitized = bound(redactSecretsInText(raw)); + if (sanitized.length === 0) continue; + return { message: sanitized, attemptIndex: i, attempts: attempts.length }; + } + return undefined; +} + +/** Render the diagnostic as the compact suffix the relay appends to its + * `← upstream ` line. Returns "" when there is no usable cause, so the + * caller can concatenate unconditionally and stay byte-identical without one. + * Pure numbers and the sanitized cause only — never the payload. */ +export function rejectionDiagnosticSuffix(payload: string | null | undefined): string { + const diag = rejectionDiagnostics(payload); + if (diag === undefined) return ""; + const position = diag.attempts > 1 ? ` attempt ${diag.attemptIndex + 1}/${diag.attempts}` : ""; + return `; ${diag.attemptIndex === diag.attempts - 1 ? "terminal" : "reported"} cause${position}: ${diag.message}`; +} diff --git a/src/server/relay.ts b/src/server/relay.ts index e3aac0cd74..65ca9c80e0 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -73,6 +73,7 @@ import { applyOutputSteering, applyOutputSteeringJson } from "../output-steering import { estimateWireOverhead } from "./budget.js"; import { buildForwardHeaders, connectionNamedHeaders, RESPONSE_ONLY_STRIP_HEADERS, safeSessionId, UPSTREAM_HOP_HEADERS } from "./headers.js"; import { bodyDumpEnabled, logDumpFailure } from "./observability.js"; +import { rejectionDiagnosticSuffix } from "./rejection-diagnostics.js"; import { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-responses.js"; import { awaitDrain, bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./stream-io.js"; import { buildForwardTarget, measuredBreakdownClause, measuredOutbound, outboundPayloadBreakdown, registerRequestAbort, beginStreamKeepalive, effectiveTokenCount, googlePathKind, imageBillingFor, imageTokenCapFor, inboundResignCredential, outboundContextEstimates, resolveUpstream, stripKernelSummaries, usageGradeInputBaseline, DOOMED_PROBE_ABSOLUTE_BYTES, DOOMED_PROBE_BYTES_PER_TOKEN, type Prepared } from "../server.js"; @@ -2058,14 +2059,18 @@ async function forwardCaptured( } log("warn", `[${s.id}] upstream image-count wall (cap ${info.imageCountCap}, request carried ${info.imageCountSeen ?? "?"}) — learned for pre-send convergence on this route; no token-space arm (#4124)`); } else if (isMarkerlessOverflowReject(upstream.status, errText)) { - // #4452: the gateway rewrote this overflow-shaped reject into a body with no - // parseable marker, so bili's self-heal cannot engage from it — name that here - // (+ the last meter reading) so a body-rewriting gateway's silent starvation is - // diagnosable from the log rather than the opaque line further down. + // #4452: a 413 IS the payload-size verdict, yet this body carries no + // parseable marker, so bili's self-heal cannot engage from it — name that + // here (+ the last meter reading) so a body-rewriting gateway's silent + // starvation is diagnosable from the log rather than the opaque line + // further down. Only 413 reaches this branch: an unmarked 400 is the + // generic malformed/schema-rejected bucket across relays and says nothing + // about size, so it is never diagnosed as overflow here (see + // isMarkerlessOverflowReject). const est = s.stats.lastInputTokens > 0 ? s.stats.lastInputTokens : 0; const win = typeof s.metadata.effectiveContextLimit === "number" && s.metadata.effectiveContextLimit > 0 ? s.metadata.effectiveContextLimit : 0; const meterNote = est > 0 && win > 0 ? `; last settled input ~${Math.round(est)} vs effective window ${win}` : ""; - log("warn", `[${s.id}] ← upstream ${upstream.status} carries no parseable overflow marker (an intermediate gateway rewrote the rejection body?) — bili's overflow self-heal (refold / emergency-shrink / window-learn) CANNOT engage from this rejection${meterNote}; repeated at similar size = calibration-starved route (#4452)`); + log("warn", `[${s.id}] ← upstream ${upstream.status} carries no parseable overflow marker (an intermediate gateway rewrote the payload-size rejection body?) — bili's overflow self-heal (refold / emergency-shrink / window-learn) CANNOT engage from this rejection${meterNote}; repeated at similar size = calibration-starved route (#4452)`); } // #762: learn strict-echo on the MAIN request path too. The loop-only // learner (src/loop/core.ts) never sees client-originated 400s, so a @@ -2102,7 +2107,15 @@ async function forwardCaptured( let snippet = bodyText.slice(0, 600).replace(/\s+/g, " ").trim(); if (bodyText.length > 600) snippet += " …"; if (!snippet) snippet = "(no body)"; - loggerLog("warn", `[${errSid}] ← upstream ${upstream.status}${reqIdText}: ${snippet}`); + // #4452: a relay that fans out to several targets answers a total failure + // with ONE aggregate body whose real cause sits in the LAST attempt's + // `details.error.message` — past the 600-byte snippet above, so the log + // showed only "all targets failed". Append that single terminal cause + // (bounded + credential-scrubbed by the helper; empty when absent, leaving + // this line byte-identical to before). Read-only: no recovery decision + // here depends on it. + const terminalCause = rejectionDiagnosticSuffix(bodyText); + loggerLog("warn", `[${errSid}] ← upstream ${upstream.status}${reqIdText}: ${snippet}${terminalCause}`); // #4204: feed this send's FINAL verdict. A model-scoped KeyRing 429 is // selected only by its structured code plus exact request-model match; // keyring_all_limited and every ambiguous response remain account-wide. diff --git a/src/util.ts b/src/util.ts index dfec17a70a..09f8d705cb 100644 --- a/src/util.ts +++ b/src/util.ts @@ -564,18 +564,30 @@ export function inspectContextOverflow(status: number, bodyText: string): Contex }; } -// #4452: an intermediate gateway that rewrites EVERY rejection into one generic -// body (Cherry Studio et al. strip the token counts / window from each overflow -// 400) makes inspectContextOverflow return isOverflow:false even when the payload -// really overflows — so bili's whole overflow self-heal (#1195 same-request -// refold, armOverflowShrink, window learn) keys off a marker that is never there -// and silently never engages, starving the route of calibration. This predicate -// names exactly that shape — a 4xx overflow-SHAPED reject whose body matches no -// pattern — so the relay can log it loudly instead of the opaque `← upstream 400` -// line. Deliberately EXCLUDES 5xx (those already self-heal via the local estimate, +// #4452: an intermediate gateway that rewrites a payload-size rejection into one +// generic body (Cherry Studio et al. strip the token counts / window) makes +// inspectContextOverflow return isOverflow:false even when the payload really +// was too large — so bili's whole overflow self-heal (#1195 same-request refold, +// armOverflowShrink, window learn) keys off a marker that is never there and +// silently never engages, starving the route of calibration. This predicate +// names exactly that shape — an unmarked size-wall reject whose body matches no +// pattern — so the relay can log it loudly instead of the opaque +// `← upstream 413` line. +// +// ONLY 413 carries that meaning on its own. A 400 is the generic "malformed / +// schema-invalid / role-rejected / auth-shaped" bucket across relays: the SAME +// opaque body (`{"error":{"message":"Internal server error", +// "code":"bad_request"}}`) is emitted for a truncated JSON body, an unsupported +// field and a genuinely oversized payload alike, so treating every unmarked 400 +// as evidence of overflow manufactured a false overflow diagnosis for ordinary +// malformed requests (#4452 follow-up). 413 Request Entity Too Large is the +// status whose SEMANTICS are already the payload-size verdict, which is why it +// can be diagnosed without a body marker. +// +// Deliberately EXCLUDES 5xx (those already self-heal via the local estimate, // #604) and any body that DOES carry a marker (self-heal engages normally then). export function isMarkerlessOverflowReject(status: number, bodyText: string): boolean { - return (status === 400 || status === 413) && !CONTEXT_OVERFLOW_PATTERNS.some((p) => p.test(bodyText)); + return status === 413 && !CONTEXT_OVERFLOW_PATTERNS.some((p) => p.test(bodyText)); } /** Default cap on the output-headroom reservation, as a fraction of the diff --git a/tests/issue4452-markerless-reject.test.ts b/tests/issue4452-markerless-reject.test.ts index 817cbda1bb..d3bd1c9749 100644 --- a/tests/issue4452-markerless-reject.test.ts +++ b/tests/issue4452-markerless-reject.test.ts @@ -2,18 +2,27 @@ import test from "node:test"; import assert from "node:assert/strict"; import { inspectContextOverflow, isMarkerlessOverflowReject } from "../src/util.ts"; -// #4452: an intermediate gateway that rewrites every overflow rejection into one +// #4452: an intermediate gateway that rewrites a payload-size rejection into one // generic body (Cherry Studio strips the token counts / window) defeats the // overflow MARKER gate — inspectContextOverflow returns isOverflow:false, so the // whole self-heal never engages and the route starves of calibration. The relay // must surface that shape explicitly (isMarkerlessOverflowReject) instead of the -// opaque `← upstream 400` line. Gate discipline (#3617): assert BOTH directions — -// the marker-less shape IS flagged, and anything self-heal already handles (a real -// marker, a 5xx, a non-overflow 4xx) is NOT. +// opaque `← upstream 413` line. Gate discipline (#3617): assert BOTH directions — +// the marker-less SIZE verdict (413) IS flagged, and anything self-heal already +// handles (a real marker, a 5xx, a non-size 4xx) is NOT. +// +// #4452 follow-up: the generic 400 is NOT a size verdict. Every ordinary +// malformed / schema-invalid / role-rejected request across relays answers with +// the SAME opaque body, so diagnosing an unmarked 400 as markerless overflow +// manufactured a false overflow warning for requests that carry no evidence of +// being too large at all. // Verbatim from the #4438/#4452 field report — identical for every request id. const GENERIC_BODY = '{"error":{"message":"Internal server error","type":"invalid_request_error","code":"bad_request"}}'; +// A schema/malformed 400 body in the shape a strict backend emits. +const MALFORMED_BODY = '{"error":{"message":"Invalid request: messages[3].content must be a string or an array","type":"invalid_request_error","code":"invalid_value"}}'; + const OPENAI_OVERFLOW_BODY = JSON.stringify({ error: { message: @@ -25,35 +34,53 @@ const OPENAI_OVERFLOW_BODY = JSON.stringify({ const CJK_OVERFLOW_BODY = JSON.stringify({ error: { message: "上下文长度超出限制", type: "invalid_request_error" } }); -test("generic gateway body on 400 is overflow-shaped but carries no parseable marker", () => { +test("generic gateway body on 413 is the markerless SIZE verdict and is flagged", () => { // The first-order failure: the marker gate itself says "not overflow". + assert.equal(inspectContextOverflow(413, GENERIC_BODY).isOverflow, false); + // …yet a 413 whose body dropped the marker is exactly the shape the relay + // must flag as self-heal-starved. + assert.equal(isMarkerlessOverflowReject(413, GENERIC_BODY), true); +}); + +test("an unmarked 400 is NOT diagnosed as overflow (generic malformed/schema rejection)", () => { + // Neither body is overflow evidence: no status semantics and no marker. assert.equal(inspectContextOverflow(400, GENERIC_BODY).isOverflow, false); - // …yet it is exactly the shape the relay must now flag as self-heal-starved. - assert.equal(isMarkerlessOverflowReject(400, GENERIC_BODY), true); + assert.equal(isMarkerlessOverflowReject(400, GENERIC_BODY), false); + // The same must hold for a clearly schema-shaped 400 body. + assert.equal(inspectContextOverflow(400, MALFORMED_BODY).isOverflow, false); + assert.equal(isMarkerlessOverflowReject(400, MALFORMED_BODY), false); }); -test("generic gateway body on 413 is also flagged", () => { - assert.equal(inspectContextOverflow(413, GENERIC_BODY).isOverflow, false); - assert.equal(isMarkerlessOverflowReject(413, GENERIC_BODY), true); +test("an unmarked 413 is not double-counted as a marker overflow", () => { + // 413 without a marker: the self-heal gate is closed, which is exactly why + // the warning exists; it must never claim isOverflow. + assert.equal(inspectContextOverflow(413, MALFORMED_BODY).isOverflow, false); + assert.equal(isMarkerlessOverflowReject(413, MALFORMED_BODY), true); }); test("a body WITH an overflow marker is NOT markerless (self-heal engages normally)", () => { assert.equal(inspectContextOverflow(400, OPENAI_OVERFLOW_BODY).isOverflow, true); assert.equal(isMarkerlessOverflowReject(400, OPENAI_OVERFLOW_BODY), false); + assert.equal(inspectContextOverflow(413, OPENAI_OVERFLOW_BODY).isOverflow, true); + assert.equal(isMarkerlessOverflowReject(413, OPENAI_OVERFLOW_BODY), false); }); test("a CJK-marked overflow body is NOT markerless", () => { assert.equal(inspectContextOverflow(400, CJK_OVERFLOW_BODY).isOverflow, true); assert.equal(isMarkerlessOverflowReject(400, CJK_OVERFLOW_BODY), false); + assert.equal(isMarkerlessOverflowReject(413, CJK_OVERFLOW_BODY), false); }); test("5xx is excluded — those already self-heal via the local estimate (#604)", () => { assert.equal(isMarkerlessOverflowReject(502, GENERIC_BODY), false); assert.equal(isMarkerlessOverflowReject(503, GENERIC_BODY), false); + assert.equal(isMarkerlessOverflowReject(507, GENERIC_BODY), false); }); test("non-overflow 4xx and success codes are excluded (no false alarm on auth/malformed)", () => { assert.equal(isMarkerlessOverflowReject(200, GENERIC_BODY), false); assert.equal(isMarkerlessOverflowReject(401, GENERIC_BODY), false); + assert.equal(isMarkerlessOverflowReject(404, GENERIC_BODY), false); + assert.equal(isMarkerlessOverflowReject(422, GENERIC_BODY), false); assert.equal(isMarkerlessOverflowReject(429, GENERIC_BODY), false); }); diff --git a/tests/rejection-diagnostics.test.ts b/tests/rejection-diagnostics.test.ts new file mode 100644 index 0000000000..7c1733c348 --- /dev/null +++ b/tests/rejection-diagnostics.test.ts @@ -0,0 +1,147 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { rejectionDiagnostics, rejectionDiagnosticSuffix } from "../src/server/rejection-diagnostics.ts"; + +// #4452: a fan-out relay answers a total failure with ONE aggregate body whose +// real cause sits nested in the last attempt. bili's log line keeps only the +// first 600 bytes, i.e. "all targets failed" — the cause was undiagnosable. +// These pin the extractor's contract: read ONE terminal nested message, bounded +// and credential-scrubbed, changing nothing about a body that carries none. + +/** Build the aggregate shape a fan-out relay returns on total failure. */ +function aggregate(attempts: unknown[], outer = "All targets failed"): string { + return JSON.stringify({ error: { message: outer, type: "upstream_error" }, attempts }); +} + +function attempt(message: unknown): Record { + return { target: "t", details: { error: { message } } }; +} + +test("reads the terminal attempt's nested cause", () => { + const body = aggregate([attempt("connection reset by peer"), attempt("upstream returned 529 overloaded")]); + const diag = rejectionDiagnostics(body); + assert.ok(diag); + assert.equal(diag.message, "upstream returned 529 overloaded"); + assert.equal(diag.attemptIndex, 1); + assert.equal(diag.attempts, 2); +}); + +test("a single attempt is indexed 0 and the suffix omits the position", () => { + const diag = rejectionDiagnostics(aggregate([attempt("model not found")])); + assert.ok(diag); + assert.equal(diag.attemptIndex, 0); + assert.equal(diag.attempts, 1); +}); + +test("later attempts win over earlier retry noise", () => { + const body = aggregate([attempt("noise early"), attempt("noise mid"), attempt("densely informative final cause")]); + assert.equal(rejectionDiagnostics(body)?.message, "densely informative final cause"); +}); + +test("a terminal attempt with no usable message falls back to the nearest informative earlier one", () => { + const body = aggregate([attempt("real cause"), { target: "t", details: {} }, attempt(undefined)]); + const diag = rejectionDiagnostics(body); + assert.ok(diag); + assert.equal(diag.message, "real cause"); +}); + +test("an empty nested message is skipped", () => { + const body = aggregate([attempt("")]); + assert.equal(rejectionDiagnostics(body), undefined); + assert.equal(rejectionDiagnosticSuffix(body), ""); +}); + +test("empty / non-string message at every attempt yields no diagnostic", () => { + assert.equal(rejectionDiagnostics(aggregate([attempt(12), attempt(null), attempt("")])), undefined); + assert.equal(rejectionDiagnosticSuffix(aggregate([attempt(12), attempt(null)])), ""); +}); + +test("a payload with no attempts[] is untouched (no diagnostic)", () => { + const bodies = [ + '{"error":{"message":"Internal server error"}}', + '{"error":{"message":"context_length_exceeded: 200000 tokens"}}', + '{"attempts":[]}', + '{"attempts":"not-an-array"}', + "{not json", + "Internal Server Error", + "502 Bad Gateway", + "", + ]; + for (const body of bodies) { + assert.equal(rejectionDiagnostics(body), undefined, `expected none for: ${body}`); + assert.equal(rejectionDiagnosticSuffix(body), "", `expected empty suffix for: ${body}`); + } +}); + +test("non-object / non-array attempt entries are skipped, not thrown on", () => { + const body = aggregate([null, "string-attempt", 4, [], { details: null }, { details: { error: null } }, attempt("surviving cause")]); + assert.equal(rejectionDiagnostics(body)?.message, "surviving cause"); +}); + +test("null / undefined payload is tolerated and yields no suffix", () => { + assert.equal(rejectionDiagnostics(null), undefined); + assert.equal(rejectionDiagnostics(undefined), undefined); + assert.equal(rejectionDiagnosticSuffix(null), ""); + assert.equal(rejectionDiagnosticSuffix(undefined), ""); +}); + +test("whitespace, newlines and control shapes are collapsed into ONE line", () => { + const hostile = "boom\r\n[attacker] ← upstream 200: forged line\n\tsecond"; + const suffix = rejectionDiagnosticSuffix(aggregate([attempt(hostile)])); + assert.ok(!suffix.includes("\n"), "no newline may survive into the log line"); + assert.ok(!suffix.includes("\r"), "no carriage return may survive"); +}); + +test("credential-shaped text is scrubbed by the shared sanitizer", () => { + const body = aggregate([attempt("rejected: authorization: Bearer abcdefghijklmnopqrstuvwxyz012345")]); + const suffix = rejectionDiagnosticSuffix(body); + assert.ok(!suffix.includes("abcdefghijklmnopqrstuvwxyz012345"), "bearer token must not reach the log"); +}); + +test("an sk- API key echoed by a provider is masked", () => { + const suffix = rejectionDiagnosticSuffix(aggregate([attempt("incorrect key sk-proj-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA supplied")])); + assert.ok(!suffix.includes("sk-proj-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); +}); + +test("a long cause is clamped, and the clamp is bounded", () => { + const suffix = rejectionDiagnosticSuffix(aggregate([attempt("x".repeat(4000))])); + // 300 retained chars + the ellipsis marker + the fixed prefix. + assert.ok(suffix.length < 340, `suffix too long: ${suffix.length}`); + assert.ok(suffix.endsWith(" …")); +}); + +test("an oversized body is not parsed at all", () => { + const huge = aggregate([attempt("y".repeat(64 * 1024 + 10))]); + assert.equal(rejectionDiagnostics(huge), undefined); + assert.equal(rejectionDiagnosticSuffix(huge), ""); +}); + +test("the suffix never echoes the outer narrative or the raw payload", () => { + const body = aggregate([attempt("inner cause")], "All targets failed: secret-route-label"); + const suffix = rejectionDiagnosticSuffix(body); + assert.ok(!suffix.includes("All targets failed")); + assert.ok(!suffix.includes("secret-route-label")); + assert.ok(!suffix.includes("{"), "no JSON structure may leak into the log"); +}); + +test("reads the terminal provider cause from CCR's nested error envelope", () => { + const body = JSON.stringify({ + error: { + message: "All target providers failed.", + attempts: [attempt("earlier transient error"), attempt("messages.32: tool_use has no following tool_result")], + }, + }); + assert.equal(rejectionDiagnostics(body)?.message, "messages.32: tool_use has no following tool_result"); + assert.equal(rejectionDiagnostics(body)?.attemptIndex, 1); +}); + +test("terminal control bytes cannot reach the diagnostic line", () => { + const body = aggregate([attempt("refused\u001b[2J\u0000\u007f by provider")]); + assert.equal(rejectionDiagnostics(body)?.message, "refused [2J by provider"); +}); + +test("the parsing budget counts UTF-8 bytes rather than UTF-16 characters", () => { + const body = aggregate([attempt("错".repeat(24 * 1024))]); + assert.ok(body.length < 64 * 1024); + assert.equal(rejectionDiagnostics(body), undefined); +}); From cbba6b376fb46f3346d930a90ed3f8c24a79665b Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 2 Nov 2026 00:29:03 +0800 Subject: [PATCH 25/78] fix(diagnostics): strip C1 control chars (incl. NEL) from the bounded rejection cause MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-up on #5482. bound() collapsed only C0 + DEL before clamping, so C1 control characters survived into the appended log suffix — in particular U+0085 (NEXT LINE) and U+009C (file separator), which render as line/field breaks. A relay that echoed such a byte into attempts[].details.error.message could forge a second log field, contradicting the module's own "no newline may forge a second log field" invariant (pinned by the \r\n test). Widen the strip to C1 (U+007f-U+009f) and extend the control-shape regression test to pin NEL and file-separator. No behavior change beyond the log-line sanitization. (cherry picked from commit c749eb82e585b3e6c80ed5c619b3d1296f9ab59b) --- src/server/rejection-diagnostics.ts | 12 +++++++----- tests/rejection-diagnostics.test.ts | 6 ++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/server/rejection-diagnostics.ts b/src/server/rejection-diagnostics.ts index 0aa5e76576..729238c548 100644 --- a/src/server/rejection-diagnostics.ts +++ b/src/server/rejection-diagnostics.ts @@ -24,9 +24,9 @@ * a high-signal credential scrubber, NOT a general privacy filter: it does not * mask arbitrary URLs or paths, and the far stronger guarantee here is * structural — only this one nested string is ever read, never a body dump. - * - Bounded to `MAX_DIAGNOSTIC_CHARS`; ALL whitespace collapsed so a newline - * (including the `\r\n` frame-injection shape) can never forge a second log - * field. + * - Bounded to `MAX_DIAGNOSTIC_CHARS`; ALL control characters (C0+C1, incl. + * NEL U+0085) and whitespace collapsed so a newline (the `\r\n` shape or any + * C1 line-break) can never forge a second log field. * - Absent, non-string, empty, or structurally-unrelated payloads return * `undefined`: the caller then logs exactly what it logged before, so a * rejection that carries no attempt detail is unchanged byte-for-byte. @@ -55,9 +55,11 @@ interface DiagnosticResult { attempts: number; } -/** Collapse ALL whitespace (newlines included) and clamp to the bound. */ +/** Collapse control chars (C0+C1 — incl. NEL U+0085) and all whitespace to + * single spaces, then clamp, so no control byte can survive as a forged field + * break in the log line. */ function bound(text: string): string { - const collapsed = text.replace(/[\u0000-\u001f\u007f]/g, " ").replace(/\s+/g, " ").trim(); + const collapsed = text.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ").replace(/\s+/g, " ").trim(); if (collapsed.length <= MAX_DIAGNOSTIC_CHARS) return collapsed; return `${collapsed.slice(0, MAX_DIAGNOSTIC_CHARS)} …`; } diff --git a/tests/rejection-diagnostics.test.ts b/tests/rejection-diagnostics.test.ts index 7c1733c348..b6802fdb7a 100644 --- a/tests/rejection-diagnostics.test.ts +++ b/tests/rejection-diagnostics.test.ts @@ -85,11 +85,13 @@ test("null / undefined payload is tolerated and yields no suffix", () => { assert.equal(rejectionDiagnosticSuffix(undefined), ""); }); -test("whitespace, newlines and control shapes are collapsed into ONE line", () => { - const hostile = "boom\r\n[attacker] ← upstream 200: forged line\n\tsecond"; +test("whitespace, newlines and control shapes (incl. C1 NEL) are collapsed into ONE line", () => { + const hostile = "boom\r\n[attacker] ← upstream 200: forged line\u0085forged2\n\tsecond\u009c"; const suffix = rejectionDiagnosticSuffix(aggregate([attempt(hostile)])); assert.ok(!suffix.includes("\n"), "no newline may survive into the log line"); assert.ok(!suffix.includes("\r"), "no carriage return may survive"); + assert.ok(!suffix.includes("\u0085"), "C1 next-line (U+0085) may not survive as a field break"); + assert.ok(!suffix.includes("\u009c"), "C1 file-separator (U+009C) may not survive as a field break"); }); test("credential-shaped text is scrubbed by the shared sanitizer", () => { From f13d9bb461cd048e081e5f91b3007668a0958c86 Mon Sep 17 00:00:00 2001 From: ci-check Date: Fri, 6 Nov 2026 19:36:44 +0800 Subject: [PATCH 26/78] test(ci): issue5152 startReceiver waits for a valid port value, not file existence (#4599) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The receiver fixture creates its .port file before the write lands; on a loaded runner the existsSync poll can win that race and read back an empty file, so Number("") === 0 tripped the assertion (CI: ubuntu-24 shard 2, run 42202673910 — second occurrence of this signature after PR #5327's one-off, recorded in #4599 as record-only). Poll until the content parses as a positive integer instead; the 15s deadline and the diagnostic message are unchanged. (cherry picked from commit 8d283b9e55e111c858d388888d835282c566256b) --- ...ue5152-owner-beat-survives-host-stall.test.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/issue5152-owner-beat-survives-host-stall.test.ts b/tests/issue5152-owner-beat-survives-host-stall.test.ts index 671977001c..900a53e120 100644 --- a/tests/issue5152-owner-beat-survives-host-stall.test.ts +++ b/tests/issue5152-owner-beat-survives-host-stall.test.ts @@ -53,13 +53,21 @@ async function startReceiver(dir: string): Promise { const portFile = `${log}.port`; const child = spawn(process.execPath, [RECEIVER, log], { stdio: "ignore", detached: true }); child.unref(); + // Poll for a VALID port, not mere file existence: the receiver creates the + // file before its write lands, so existsSync can win that race on a loaded + // runner and read back an empty file (Number("") === 0). + let port = 0; + let portText = ""; const deadline = Date.now() + 15_000; - while (!existsSync(portFile) && Date.now() < deadline) { + while (port === 0 && Date.now() < deadline) { + if (existsSync(portFile)) { + portText = readFileSync(portFile, "utf8").trim(); + const parsed = Number(portText); + if (Number.isInteger(parsed) && parsed > 0) port = parsed; + } Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20); } - assert.ok(existsSync(portFile), "receiver must publish its port"); - const port = Number(readFileSync(portFile, "utf8").trim()); - assert.ok(Number.isInteger(port) && port > 0, `receiver port must be a number, got ${port}`); + assert.ok(port > 0, `receiver must publish its port (file held "${portText}")`); return { port, beats: () => (existsSync(log) ? readFileSync(log, "utf8").trim().split("\n").filter(Boolean).map(Number) : []), From 65a0794a045de824b092719c3f2e0ab169708e4a Mon Sep 17 00:00:00 2001 From: Longwu Ou Date: Tue, 3 Nov 2026 09:10:20 -0500 Subject: [PATCH 27/78] chore: add redacted Responses WebSocket cache diagnostics Correlate exact egress fingerprints, socket generations and send attempts with terminal usage-field presence. Preserve reported numeric zero versus absent cache usage without logging raw payloads or headers. This adds diagnostics, not a provider cache-miss fix. (cherry picked from commit 1d59e828c9b8444c3dad90e4deab3d2959507987) --- src/responses-ws.ts | 36 ++++ tests/responses-ws-cache-diagnostics.test.ts | 167 +++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 tests/responses-ws-cache-diagnostics.test.ts diff --git a/src/responses-ws.ts b/src/responses-ws.ts index b728054545..1f7caccb1b 100644 --- a/src/responses-ws.ts +++ b/src/responses-ws.ts @@ -1,3 +1,4 @@ +import { createHash } from "node:crypto"; import http from "node:http"; import type { ResponsesRequestBody } from "acp-kernel/wire"; import { WebSocket as UpstreamWebSocket, type Dispatcher } from "undici"; @@ -322,8 +323,14 @@ function bodyToText(body: RequestInit["body"]): string | undefined { return undefined; } +function cacheDiagnosticHash(text: string): string { + return createHash("sha256").update(text).digest("hex"); +} + export class ResponsesWsUpstream { private socket?: InstanceType; + private connectionGeneration = 0; + private sendSequence = 0; private key?: string; private history = new ResponsesWsHistory(); private active?: { fail: (error: Error, reason?: string) => void }; @@ -373,6 +380,7 @@ export class ResponsesWsUpstream { this.log("debug", "upstream connecting phase=handshake"); const socket = new UpstreamWebSocket(wsUrl, { headers, dispatcher: options.dispatcher as Dispatcher | undefined }); this.socket = socket; + this.connectionGeneration++; this.key = key; await new Promise((resolve, reject) => { const clean = (): void => { @@ -458,6 +466,10 @@ export class ResponsesWsUpstream { } if (this.active) throw new Error("Responses WebSocket exchange already active"); const request = this.history.continuation(body); + const connection = this.connectionGeneration; + const { input: _diagnosticInput, ...settings } = body; + const requestHash = cacheDiagnosticHash(JSON.stringify({ ...body, ...egressTransportFields, type: "response.create" })); + const settingsHash = cacheDiagnosticHash(canonical({ ...settings, ...egressTransportFields })); this.log("debug", `upstream request mode=${request.previous_response_id === undefined ? "full" : "delta"}`); return new Promise((resolve, reject) => { const output = new ResponsesWsOutput(); @@ -466,12 +478,21 @@ export class ResponsesWsUpstream { let ended = false; let retryFull = request.previous_response_id !== undefined; let receivedEvents = 0; + let send = 0; const encoder = new TextEncoder(); // #3701: log the EXACT outbound frame bytes — the upstream gateway judges by // bytes, not tokens, so this number is what the ws-byte-shed ladder targets. const sendCreate = (payload: JsonObject): void => { const frameText = JSON.stringify({ ...payload, ...egressTransportFields, type: "response.create" }); this.log("info", `upstream send mode=${payload.previous_response_id === undefined ? "full" : "delta"} bytes=${Buffer.byteLength(frameText)}`); + send = ++this.sendSequence; + // Allowlisted fingerprints only: never log payloads, headers, or the connection key. + this.log("debug", `upstream cache-diagnostic ${JSON.stringify({ + phase: "send", connection, send, + mode: payload.previous_response_id === undefined ? "full" : "delta", + frame_sha256: cacheDiagnosticHash(frameText), request_sha256: requestHash, settings_sha256: settingsHash, + previous_response_id_sha256: typeof payload.previous_response_id === "string" ? cacheDiagnosticHash(payload.previous_response_id) : null, + })}`); socket.send(frameText); }; // Backpressure (#2076): the WS socket cannot be paused, so frames arriving @@ -533,6 +554,21 @@ export class ResponsesWsUpstream { } if (!object(frame) || typeof frame.type !== "string") throw new Error("Invalid Responses WebSocket event"); receivedEvents++; + if (["response.completed", "response.failed", "response.incomplete", "error"].includes(frame.type)) { + const response = object(frame.response) ? frame.response : {}; + const usage = object(response.usage) ? response.usage : {}; + const details = object(usage.input_tokens_details) ? usage.input_tokens_details : {}; + const tokens = (value: unknown): number | null => typeof value === "number" && Number.isFinite(value) ? value : null; + this.log("debug", `upstream cache-diagnostic ${JSON.stringify({ + phase: "terminal", connection, send, event: frame.type, + response_id_sha256: typeof response.id === "string" ? cacheDiagnosticHash(response.id) : null, + usage_present: Object.hasOwn(response, "usage"), + input_tokens_details_present: Object.hasOwn(usage, "input_tokens_details"), + input_tokens_present: Object.hasOwn(usage, "input_tokens"), input_tokens: tokens(usage.input_tokens), + output_tokens_present: Object.hasOwn(usage, "output_tokens"), output_tokens: tokens(usage.output_tokens), + cached_tokens_present: Object.hasOwn(details, "cached_tokens"), cached_tokens: tokens(details.cached_tokens), + })}`); + } const completed = output.observe(frame); if (frame.type === "error" && !settled) { const error = object(frame.error) ? frame.error : {}; diff --git a/tests/responses-ws-cache-diagnostics.test.ts b/tests/responses-ws-cache-diagnostics.test.ts new file mode 100644 index 0000000000..90128fe5a3 --- /dev/null +++ b/tests/responses-ws-cache-diagnostics.test.ts @@ -0,0 +1,167 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { once } from "node:events"; +import test from "node:test"; +import { WebSocketServer } from "ws"; +import { ResponsesWsUpstream } from "../src/responses-ws.ts"; + +type Item = Record; +const hash = (text: string): string => createHash("sha256").update(text).digest("hex"); +const user = (text: string): Item => ({ type: "message", role: "user", content: text }); +const output: Item[] = [{ type: "reasoning", encrypted_content: "private-reasoning" }]; +const completed = (id: string, fields: Item = {}): Item => ({ + type: "response.completed", response: { id, status: "completed", output, ...fields }, +}); + +async function fixture(replies: Array) { + const server = new WebSocketServer({ port: 0, host: "127.0.0.1" }); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + const rows: string[] = []; + const sent: string[] = []; + const logs: string[] = []; + server.on("connection", peer => peer.on("message", raw => { + rows.push(raw.toString()); + const reply = replies.shift() ?? { type: "error", error: { code: "unexpected-request" } }; + for (const item of Array.isArray(reply) ? reply : [reply]) { + const frame = JSON.stringify(item); + sent.push(frame); + peer.send(frame); + } + })); + const transport = new ResponsesWsUpstream((_level, message) => logs.push(message), undefined, undefined, true); + return { + rows, sent, logs, transport, + diagnostics: (): Item[] => logs.filter(line => line.startsWith("upstream cache-diagnostic ")).map(line => JSON.parse(line.slice("upstream cache-diagnostic ".length)) as Item), + turn: async (body: Item): Promise => { + const response = await transport.fetch(`http://127.0.0.1:${address.port}/responses?api-key=private-query-key`, { + method: "POST", + headers: { authorization: "Bearer private-auth-key", session_id: "private-session-header" }, + body: JSON.stringify(body), + }); + return response.text(); + }, + close: async (): Promise => { + transport.close(); + for (const peer of server.clients) peer.terminate(); + await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); + }, + }; +} + +for (const stream of [true, false]) { + test(`WS cache diagnostics: zero and missing usage remain distinct (stream=${stream})`, { timeout: 10000 }, async () => { + const cases = [ + { fields: { usage: { input_tokens: 2048, output_tokens: 5, input_tokens_details: { cached_tokens: 0 } } }, usage: true, details: true, present: true, cached: 0 }, + { fields: {}, usage: false, details: false, present: false, cached: null }, + { fields: { usage: { input_tokens: 2048 } }, usage: true, details: false, present: false, cached: null }, + { fields: { usage: { input_tokens_details: {} } }, usage: true, details: true, present: false, cached: null }, + { fields: { usage: { input_tokens_details: { cached_tokens: 1024 } } }, usage: true, details: true, present: true, cached: 1024 }, + { fields: { usage: { input_tokens_details: { cached_tokens: "private-invalid-usage" } } }, usage: true, details: true, present: true, cached: null }, + { fields: { usage: null }, usage: true, details: false, present: false, cached: null }, + ]; + const f = await fixture(cases.map((row, i) => completed(`resp_${i}`, row.fields))); + try { + let input = [user("private-prompt")]; + for (const [i, row] of cases.entries()) { + const received = await f.turn({ model: "test-model", input, stream }); + if (stream) assert.equal(received, `event: response.completed\ndata: ${f.sent[i]}\n\n`); + else assert.deepEqual(JSON.parse(received), JSON.parse(f.sent[i]).response); + const terminal = f.diagnostics().filter(event => event.phase === "terminal").at(-1)!; + assert.equal(terminal.usage_present, row.usage); + assert.equal(terminal.input_tokens_details_present, row.details); + assert.equal(terminal.cached_tokens_present, row.present); + assert.equal(terminal.cached_tokens, row.cached); + assert.equal(terminal.input_tokens_present, i === 0 || i === 2); + assert.equal(terminal.input_tokens, i === 0 || i === 2 ? 2048 : null); + assert.equal(terminal.output_tokens_present, i === 0); + assert.equal(terminal.output_tokens, i === 0 ? 5 : null); + assert.equal(terminal.event, "response.completed"); + assert.equal(terminal.response_id_sha256, hash(`resp_${i}`)); + assert.equal(terminal.connection, 1); + assert.equal(terminal.send, i + 1); + input = [...input, ...output, user("private-next-prompt")]; + } + const log = f.logs.join("\n"); + for (const secret of ["private-prompt", "private-reasoning", "private-auth-key", "private-query-key", "private-session-header", "private-invalid-usage", "resp_0"]) assert.ok(!log.includes(secret), secret); + } finally { await f.close(); } + }); +} + +test("WS cache diagnostics: fingerprints describe exact egress, deltas and reconnects without changing bytes", { timeout: 10000 }, async () => { + const f = await fixture([completed("resp_first"), completed("resp_second"), completed("resp_third"), completed("resp_fourth")]); + try { + const settings = { model: "test-model", instructions: "private-instructions", tools: [{ type: "function", name: "private-tool", parameters: {} }], prompt_cache_key: "private-cache-key" }; + const transportFields = { stream: true, stream_options: { include_usage: true } }; + const firstInput = [user("private-prompt")]; + const secondInput = [...firstInput, ...output, user("private-next-prompt")]; + const thirdInput = [...secondInput, ...output, user("private-third-prompt")]; + await f.turn({ ...settings, input: firstInput, ...transportFields }); + await f.turn({ ...settings, input: secondInput, ...transportFields }); + const changedSettings = { ...settings, instructions: "private-changed-instructions" }; + await f.turn({ ...changedSettings, input: thirdInput, ...transportFields }); + f.transport.close(); + await f.turn({ ...changedSettings, input: thirdInput, ...transportFields }); + assert.deepEqual(f.rows, [ + JSON.stringify({ ...settings, input: firstInput, ...transportFields, type: "response.create" }), + JSON.stringify({ ...settings, input: [user("private-next-prompt")], previous_response_id: "resp_first", ...transportFields, type: "response.create" }), + JSON.stringify({ ...changedSettings, input: thirdInput, ...transportFields, type: "response.create" }), + JSON.stringify({ ...changedSettings, input: thirdInput, ...transportFields, type: "response.create" }), + ]); + const sends = f.diagnostics().filter(event => event.phase === "send"); + assert.equal(sends.length, 4); + for (const [i, row] of sends.entries()) { + assert.equal(row.frame_sha256, hash(f.rows[i])); + assert.equal(row.send, i + 1); + assert.equal(row.connection, i === 3 ? 2 : 1); + } + assert.deepEqual(sends.map(row => row.mode), ["full", "delta", "full", "full"]); + assert.equal(sends[0].request_sha256, sends[0].frame_sha256); + assert.equal(sends[1].request_sha256, hash(JSON.stringify({ ...settings, input: secondInput, ...transportFields, type: "response.create" }))); + assert.equal(sends[0].settings_sha256, sends[1].settings_sha256); + assert.notEqual(sends[1].settings_sha256, sends[2].settings_sha256); + assert.deepEqual(sends.map(row => row.previous_response_id_sha256), [null, hash("resp_first"), null, null]); + for (const secret of ["private-prompt", "private-instructions", "private-changed-instructions", "private-tool", "private-cache-key", "private-auth-key", "private-query-key", "private-session-header", "private-reasoning", "resp_first"]) assert.ok(!f.logs.join("\n").includes(secret), secret); + } finally { await f.close(); } +}); + +test("WS cache diagnostics: continuation recovery correlates terminal usage with the actual resend", { timeout: 10000 }, async () => { + const rejection = { type: "error", status: 400, error: { code: "previous_response_not_found", message: "private-error-message" } }; + const f = await fixture([completed("resp_seed"), rejection, completed("resp_recovered", { usage: { input_tokens_details: { cached_tokens: 0 } } })]); + try { + const input = [user("seed")]; + await f.turn({ model: "test-model", input, stream: true }); + await f.turn({ model: "test-model", input: [...input, ...output, user("next")], stream: true }); + const rows = f.diagnostics(); + assert.deepEqual(rows.filter(row => row.phase === "send").map(row => [row.send, row.mode, row.connection]), [[1, "full", 1], [2, "delta", 1], [3, "full", 1]]); + assert.deepEqual(rows.filter(row => row.phase === "terminal").map(row => [row.send, row.event]), [[1, "response.completed"], [2, "error"], [3, "response.completed"]]); + assert.equal(rows.at(-1)?.cached_tokens, 0); + assert.ok(!f.logs.join("\n").includes("private-error-message")); + } finally { await f.close(); } +}); + +for (const event of ["response.completed", "response.failed", "response.incomplete", "error"]) { + test(`WS cache diagnostics: only terminal ${event} is recorded`, { timeout: 10000 }, async () => { + const terminal = event === "error" + ? { type: event, error: { message: "private-error-message" } } + : { type: event, response: { id: "private-response-id", status: event.slice("response.".length), output, usage: { input_tokens_details: { cached_tokens: 0 } } } }; + const f = await fixture([[ + { type: "response.created", response: { id: "private-created-id", usage: { input_tokens_details: { cached_tokens: 9999 } } } }, + { type: "response.output_text.delta", delta: "private-text-delta" }, + terminal, + ]]); + try { + const received = await f.turn({ model: "test-model", input: [user("private-prompt")], stream: true }); + assert.equal(received, f.sent.map(frame => `event: ${(JSON.parse(frame) as Item).type}\ndata: ${frame}\n\n`).join("")); + const rows = f.diagnostics(); + assert.equal(rows.length, 2); + assert.equal(rows[0].phase, "send"); + assert.equal(rows[1].phase, "terminal"); + assert.equal(rows[1].event, event); + assert.equal(rows[1].cached_tokens, event === "error" ? null : 0); + assert.equal(rows[1].response_id_sha256, event === "error" ? null : hash("private-response-id")); + for (const secret of ["private-text-delta", "private-error-message", "private-created-id", "private-response-id"]) assert.ok(!f.logs.join("\n").includes(secret), secret); + } finally { await f.close(); } + }); +} From 6ee4adeedb7bec7740f900f5a0be664778b2a63a Mon Sep 17 00:00:00 2001 From: Longwu Ou Date: Tue, 10 Nov 2026 11:19:18 -0500 Subject: [PATCH 28/78] chore: drop the full-request hash from WS cache diagnostics request_sha256 serialized and hashed the whole effective request a second time on every send, including delta sends. A synthetic benchmark on Node 24 put that at about 2.4 ms per MB of body, run synchronously. The field added little. On full sends it hashed the same text as frame_sha256. On delta sends, ResponsesWsHistory.continuation() has already matched the settings and every prior input item, so mode=delta attests the same thing. The hash also changes every turn, so it cannot show prefix stability. The remaining send fields are unchanged. The test now pins the exact key set of the send record. (cherry picked from commit 30ee96bfb9a5884728ee4e74d2f4129d78a9204c) --- src/responses-ws.ts | 3 +-- tests/responses-ws-cache-diagnostics.test.ts | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/responses-ws.ts b/src/responses-ws.ts index 1f7caccb1b..d1c5abddd1 100644 --- a/src/responses-ws.ts +++ b/src/responses-ws.ts @@ -468,7 +468,6 @@ export class ResponsesWsUpstream { const request = this.history.continuation(body); const connection = this.connectionGeneration; const { input: _diagnosticInput, ...settings } = body; - const requestHash = cacheDiagnosticHash(JSON.stringify({ ...body, ...egressTransportFields, type: "response.create" })); const settingsHash = cacheDiagnosticHash(canonical({ ...settings, ...egressTransportFields })); this.log("debug", `upstream request mode=${request.previous_response_id === undefined ? "full" : "delta"}`); return new Promise((resolve, reject) => { @@ -490,7 +489,7 @@ export class ResponsesWsUpstream { this.log("debug", `upstream cache-diagnostic ${JSON.stringify({ phase: "send", connection, send, mode: payload.previous_response_id === undefined ? "full" : "delta", - frame_sha256: cacheDiagnosticHash(frameText), request_sha256: requestHash, settings_sha256: settingsHash, + frame_sha256: cacheDiagnosticHash(frameText), settings_sha256: settingsHash, previous_response_id_sha256: typeof payload.previous_response_id === "string" ? cacheDiagnosticHash(payload.previous_response_id) : null, })}`); socket.send(frameText); diff --git a/tests/responses-ws-cache-diagnostics.test.ts b/tests/responses-ws-cache-diagnostics.test.ts index 90128fe5a3..3e15d7e7a8 100644 --- a/tests/responses-ws-cache-diagnostics.test.ts +++ b/tests/responses-ws-cache-diagnostics.test.ts @@ -117,8 +117,9 @@ test("WS cache diagnostics: fingerprints describe exact egress, deltas and recon assert.equal(row.connection, i === 3 ? 2 : 1); } assert.deepEqual(sends.map(row => row.mode), ["full", "delta", "full", "full"]); - assert.equal(sends[0].request_sha256, sends[0].frame_sha256); - assert.equal(sends[1].request_sha256, hash(JSON.stringify({ ...settings, input: secondInput, ...transportFields, type: "response.create" }))); + // The send record is an allowlist. A hash of the full effective request is deliberately + // absent: it costs a second full-body serialization on every send, including deltas. + for (const row of sends) assert.deepEqual(Object.keys(row).sort(), ["connection", "frame_sha256", "mode", "phase", "previous_response_id_sha256", "send", "settings_sha256"]); assert.equal(sends[0].settings_sha256, sends[1].settings_sha256); assert.notEqual(sends[1].settings_sha256, sends[2].settings_sha256); assert.deepEqual(sends.map(row => row.previous_response_id_sha256), [null, hash("resp_first"), null, null]); From c15d9cecc47272aa16621596f2a857ae91813a87 Mon Sep 17 00:00:00 2001 From: Louie0806 Date: Thu, 5 Nov 2026 13:24:25 +0800 Subject: [PATCH 29/78] fix(#5687): a tool-less MAIN turn must not be mistaken for a dsh sidecar The generic single-shot gate added in #5687 matches "no tools + one user message + any system text", and both of its call sites opened on `agent !== "main"`. dsh's native client never stamps `x-bili-plugin-agent`, so that header reads `undefined`, and `undefined !== "main"` is always true: the FIRST unstyled main turn of a conversation matched the sidecar shape and rode the #388 verbatim side lane. The kernel never saw it, so the durable-state carriers (skills, workspace instructions, MCP catalog, runtime context) never received their BLOCKED refs (#2419/#2446/#2447/#2481), and the wire-byte contracts of #2817/#5418 reset. A genuine utility sidecar is distinguishable from that turn in two ways, and this patch requires both: - it pins a TINY fixed output budget (prompt-enhance sends 4096), whereas a main main turn either omits the field (openai wire) or sizes it from the conversation (#5656's own residual turn sends 64000) -- hasSingleShotBudget; - it never carries a dsh durable-state marker -- carriesDurableState, the same marker table fold-reconcile.ts already screens with. The #5656 e2e residual that pinned "tool-less turn 1 rides the side lane" is revoked and re-pinned to the corrected contract; the side-lane boundary test allowlists the new predicate like every other pure shape predicate. Tests: issue2419/2446/2447/2481 (4/0 each), issue2817 (1/0), issue5418 watermark (1/0), issue5656 (5/0), side-lane-invariants (15/0), issue5658 (2/0). Negative control: reverting only the two source files re-reds 2446 (3/1), 2817 (0/1), 5418 (0/1). (cherry picked from commit 427778b1495791bb5830a3ef83002a42bebbddae) --- src/server/handle.ts | 6 ++-- src/server/side-request.ts | 36 +++++++++++++++++++++ tests/issue5656-single-shot-sidecar.test.ts | 7 +++- tests/side-lane-invariants.test.ts | 3 +- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/server/handle.ts b/src/server/handle.ts index 696c011566..ead6d3bd60 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -70,7 +70,7 @@ import { buildForgeResponse, classifierForgeVariant, estimateCjkTokens, isAutoMo import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, unwrapV1Internal } from "../v1internal.js"; import { anthropicBetaContextWindow, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; -import { demoteGate, hasLeakedBiliToolsOnly, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; +import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; import { buildDshCompactionForgeBody, renderDshAuthoredCheckpoint } from "./dsh-authored-compaction.js"; import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js"; @@ -1227,6 +1227,7 @@ async function handleRequest( // anchor before the first main turn and exile the real // conversation onto a persisted `|sub:` ghost. || (pluginAgentHeader(req.headers) === "dsh" && sideAgent !== "main" + && hasSingleShotBudget(parsed) && isDshSingleShotSidecar(protocol, parsed)) // #5135: the claude code title sidecar stays off the raw-key // anchor claim and off the kernel pipeline entirely. @@ -2558,7 +2559,8 @@ async function handleRequest( // prompt-enhance-class calls ride the #388 passthrough (kernel // no-touch) instead of claiming the raw-key anchor; the sticky-bound leg // mirrors #4438 — the extension client can arrive unstamped. - const dshSingleShotSidecar = requestAgent !== "main" && isDshSingleShotSidecar(protocol, parsed) + const dshSingleShotSidecar = requestAgent !== "main" && hasSingleShotBudget(parsed) + && isDshSingleShotSidecar(protocol, parsed) && (pluginAgentHeader(req.headers) === "dsh" || (typeof session.metadata.pluginAgent === "string" && session.metadata.pluginAgent === "dsh")); // #5135: ccTitleSidecar joins the intent set so the title call rides diff --git a/src/server/side-request.ts b/src/server/side-request.ts index e38ea78494..3db722dd98 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -5,6 +5,7 @@ import { reserveOutputHeadroom, systemCarrierTexts } from "../util.js"; import { type ResolvedImageBilling } from "../image-tokens.js"; import { ACP_TOOL_NAMES, ABSORB_TOOL_NAME, BLOCK_OP_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "acp-kernel"; import { READ_ORIGINAL_IMAGE_TOOL_NAME } from "../compress-tool.js"; +import { dshCarrierRawMarkers } from "../durable-message-guards.js"; // #388: side requests (title-gen etc.) share the main session key but must not // touch kernel state. Identified by a tiny output budget (same heuristic as @@ -208,6 +209,33 @@ export function isDshAutoReviewRequest(protocol: string | null, parsed: unknown) // message) vetoes — see the comment above the constant. return users === 1 && userHasContent; } +/** #5687 follow-up: a sidecar never carries a durable-state marker. dsh injects + * its durable payloads (skills, workspace instructions, MCP catalog, runtime + * context, image typing, job notices) into the FIRST main turn's user blob. A + * tool-less single-user body carrying one of those markers is therefore a MAIN + * turn whose assistant surface happens to be empty — routing it to the verbatim + * side lane skips the kernel, so the classifier can never stamp the carrier's + * BLOCKED ref (#2419/#2446/#2447/#2481) and the wire-byte contracts of + * #2817/#5403 reset. Genuine sidecars (title / enhance / translate / + * auto-review) carry the host's own prompt and no marker, so they keep the + * side lane. */ +function carriesDurableState(text: string): boolean { + for (const marker of dshCarrierRawMarkers) if (text.indexOf(marker) >= 0) return true; + return false; +} + +/** #5687 follow-up (call-site narrowing): a utility sidecar pins a TINY fixed + * output budget (host presets: prompt-enhance 4096). Agent main turns either + * omit the field (openai wire) or size it from their conversation, so + * `isDshSingleSidecar` alone over-matches the FIRST unstyled main turn. */ +const SINGLE_SHOT_SIDECAR_MAX_BUDGET = 8192; +export function hasSingleShotBudget(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const p = parsed as Record; + const budget = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; + return typeof budget === "number" && budget > 0 && budget <= SINGLE_SHOT_SIDECAR_MAX_BUDGET; +} + // #5656/#5584: dsh's per-feature sidecar pins above (title #2503, auto-review // #4438) are whack-a-mole — every NEW one-shot sidecar dsh ships around the @@ -260,8 +288,16 @@ export function isDshSingleShotSidecar(protocol: string | null, parsed: unknown) const c = rec.content ?? rec.parts; if (typeof c === "string") { if (c.length > 0) userHasContent = true; + // #5687 follow-up: a durable-state marker means this is a MAIN turn's + // injected context, never a utility sidecar. + if (carriesDurableState(c)) return false; } else if (Array.isArray(c) && c.length > 0) { userHasContent = true; + for (const part of c) { + if (part === null || typeof part !== "object") continue; + const text = (part as { text?: unknown }).text; + if (typeof text === "string" && carriesDurableState(text)) return false; + } } } return users === 1 && userHasContent; diff --git a/tests/issue5656-single-shot-sidecar.test.ts b/tests/issue5656-single-shot-sidecar.test.ts index 42108e1e09..552e9185c7 100644 --- a/tests/issue5656-single-shot-sidecar.test.ts +++ b/tests/issue5656-single-shot-sidecar.test.ts @@ -260,7 +260,12 @@ test("e2e #5656 negative: a tool-less multi-message MAIN turn still goes through const stub = peekSession(CONV); const refs1 = stub ? Object.keys(stub.state.messageRefs.byRaw).length : 0; const reqs1 = stub ? stub.stats.requests : 0; - assert.ok(refs1 === 0 && reqs1 === 0, `tool-less single-shot turn 1 rides the side lane (refs=${refs1}, requests=${reqs1})`); + // #5687 follow-up: this residual is REVOKED. A tool-less turn 1 that + // carries a conversation-sized output budget (64000 here) is a MAIN turn, + // not a utility sidecar: the small-budget requirement is what keeps it on + // the kernel path. Pinning the old "rides the side lane" behavior here is + // exactly what let #2817/#5403 regress. + assert.ok(refs1 >= 1 && reqs1 >= 1, `tool-less turn 1 with a conversation budget goes through the full pipeline (refs=${refs1}, requests=${reqs1})`); // Turn 2 replays history (user+assistant+user): NOT single-shot — full // pipeline, claims the raw key, compression state builds. const r2 = await fetch(url, { diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 39363a59ab..43fcbdeeb3 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -357,7 +357,8 @@ test("(F) extracted protocol modules keep the side-lane boundary (#1440 P2 cut 2 const text = readFileSync(fileURLToPath(new URL("../src/server/handle.ts", import.meta.url)), "utf8"); const laneInternals = /\bSIDE_REQUEST_MAX_TOKENS\b|\bSIDE_REQUEST_AGENTS\b|\bDemoteGateSignals\b|\bSideLaneSignals\b|\bSideLaneDecision\b|\bBILI_TOOL_NAMES\b|\bOutputBudgetField\b|\boutputBudgetField\b|\breadOutputBudget\b|\bwriteOutputBudget\b|\b_resetNoOutputCeilingWarningsForTest\b/; assert.doesNotMatch(text, laneInternals, "handle.ts must stay decoupled from the side-lane engine internals"); - const allowlist = new Set(["hasLeakedBiliToolsOnly", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); const allowlist = new Set(["hasLeakedBiliToolsOnly", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); let sawImport = false; + const allowlist = new Set(["hasLeakedBiliToolsOnly", "hasSingleShotBudget", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); + let sawImport = false; for (const m of text.matchAll(/\{([^}]*)\}\s*from\s*["'][^"']*side-request\.js["']/g)) { sawImport = true; for (const raw of m[1].split(",")) { From 6c65641c4a134eabf6a74773733f42acfb3109be Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 17:02:08 +0800 Subject: [PATCH 30/78] fix: align the single-shot sidecar doc comments with the narrowed residual (cherry picked from commit 78c9db9885879830e8650de2475a4ccb5793cd9d) --- src/server/side-request.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 3db722dd98..0746290ab1 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -227,7 +227,7 @@ function carriesDurableState(text: string): boolean { /** #5687 follow-up (call-site narrowing): a utility sidecar pins a TINY fixed * output budget (host presets: prompt-enhance 4096). Agent main turns either * omit the field (openai wire) or size it from their conversation, so - * `isDshSingleSidecar` alone over-matches the FIRST unstyled main turn. */ + * `isDshSingleShotSidecar` alone over-matches the FIRST unstyled main turn. */ const SINGLE_SHOT_SIDECAR_MAX_BUDGET = 8192; export function hasSingleShotBudget(parsed: unknown): boolean { if (!parsed || typeof parsed !== "object") return false; @@ -236,7 +236,6 @@ export function hasSingleShotBudget(parsed: unknown): boolean { return typeof budget === "number" && budget > 0 && budget <= SINGLE_SHOT_SIDECAR_MAX_BUDGET; } - // #5656/#5584: dsh's per-feature sidecar pins above (title #2503, auto-review // #4438) are whack-a-mole — every NEW one-shot sidecar dsh ships around the // main conversation (prompt-enhance in #5656: "请重写以下提示词…" rewrites the @@ -259,10 +258,12 @@ export function hasSingleShotBudget(parsed: unknown): boolean { // A non-empty system carrier is REQUIRED so the kernel's empty-instructions // path (aux calls riding the main session through the full pipeline, pinned in // tests/dsh-persona-fingerprint.test.ts phase 6) keeps its contract. -// Residual, accepted: a tool-less MAIN turn 1 matches the single-shot shape and -// rides the verbatim side lane for that one turn (tiny — nothing to compress); -// turn 2+ replays history and rejoins the full pipeline. The caller must gate -// on the dsh lane (evidence-per-client discipline, cf. isDshTitleRequest). +// Residual, accepted (narrowed by #5687 follow-up): a tool-less MAIN turn 1 +// that ALSO passes the call-site budget screen (small pinned output, +// hasSingleShotBudget) and carries no durable-state marker still rides the +// verbatim side lane for that one turn; turn 2+ replays history and rejoins +// the full pipeline. The caller must gate on the dsh lane +// (evidence-per-client discipline, cf. isDshTitleRequest). export function isDshSingleShotSidecar(protocol: string | null, parsed: unknown): boolean { if (protocol === null || !parsed || typeof parsed !== "object") return false; const p = parsed as Record; From 0abde2d0479391d91437d9c98224248622c22d63 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 17:02:08 +0800 Subject: [PATCH 31/78] test: update stale side-lane comments and pin hasSingleShotBudget boundary rows (cherry picked from commit c769babfa912c27b31a31d8fd9f35ea1d99a8e8f) --- tests/issue5656-single-shot-sidecar.test.ts | 30 +++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/issue5656-single-shot-sidecar.test.ts b/tests/issue5656-single-shot-sidecar.test.ts index 552e9185c7..dad1fbf9b0 100644 --- a/tests/issue5656-single-shot-sidecar.test.ts +++ b/tests/issue5656-single-shot-sidecar.test.ts @@ -10,7 +10,9 @@ // feeding #5671's terminal 7.3×-window deaths). isDshSingleShotSidecar closes // the hole structurally: no action surface (no tools) + single-shot body (one // non-system message, no assistant turn) + a non-empty system carrier, ANY -// system text. These tests pin that contract in both directions (#3617). +// system text, and no durable-state marker in the user blob; the call sites +// additionally require a small pinned output budget (#5687 follow-up, +// hasSingleShotBudget). These tests pin that contract in both directions (#3617). import test from "node:test"; import assert from "node:assert/strict"; @@ -25,7 +27,7 @@ import { _resetPluginStateForTest, resolveConversation } from "../src/plugin.ts" import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { getSession, peekSession, _resetSessionsForTest } from "../src/session.ts"; -import { isDshSingleShotSidecar } from "../src/server/side-request.ts"; +import { hasSingleShotBudget, isDshSingleShotSidecar } from "../src/server/side-request.ts"; process.env.NODE_ENV = "test"; process.env.BILI_PERSIST = "0"; @@ -139,6 +141,21 @@ test("unit #5656: isDshSingleShotSidecar vetoes every main-turn and aux shape", assert.equal(isDshSingleShotSidecar("openai", "nope"), false, "non-object body"); }); +test("unit #5687 follow-up: hasSingleShotBudget accepts only a small pinned numeric budget", () => { + assert.equal(hasSingleShotBudget({ max_tokens: 4096 }), true, "anthropic wire preset (prompt-enhance 4096)"); + assert.equal(hasSingleShotBudget({ max_tokens: 8192 }), true, "boundary inclusive"); + assert.equal(hasSingleShotBudget({ max_tokens: 8193 }), false, "just past the boundary"); + assert.equal(hasSingleShotBudget({ max_completion_tokens: 4096 }), true, "openai chat modern field"); + assert.equal(hasSingleShotBudget({ max_output_tokens: 1024 }), true, "responses wire field"); + assert.equal(hasSingleShotBudget({ model: MODEL }), false, "field omitted (openai main-turn shape) -> full pipeline"); + assert.equal(hasSingleShotBudget({ max_tokens: 64_000 }), false, "conversation-sized main-turn budget"); + assert.equal(hasSingleShotBudget({ max_tokens: 0 }), false, "zero"); + assert.equal(hasSingleShotBudget({ max_tokens: -1 }), false, "negative"); + assert.equal(hasSingleShotBudget({ max_tokens: "4096" }), false, "numeric string is not a number (fail-safe direction)"); + assert.equal(hasSingleShotBudget(null), false, "null body"); + assert.equal(hasSingleShotBudget("nope"), false, "non-object body"); +}); + test("e2e #5656 (incident repro): a single-shot sidecar arriving FIRST never claims the raw-key anchor — the main conversation keeps the raw key", async () => { const rig = await startRig(); const CONV = "dshp-5656-openai"; @@ -244,9 +261,9 @@ test("e2e #5656 negative: a tool-less multi-message MAIN turn still goes through try { const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; const headers: Record = { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": CONV }; - // Turn 1 of a tools-disabled conversation matches the single-shot shape - // (system + 1 user, no tools): it rides the verbatim side lane for that - // one turn — tiny, nothing to compress, accepted residual. + // Turn 1 of a tools-disabled conversation matches the single-shot SHAPE + // (system + 1 user, no tools); the conversation-sized output budget + // (64000 below) is what keeps it off the side lane (#5687 follow-up). const r1 = await fetch(url, { method: "POST", headers, @@ -254,9 +271,6 @@ test("e2e #5656 negative: a tool-less multi-message MAIN turn still goes through }); assert.equal(r1.status, 200); await r1.text(); - // Accepted residual, pinned honestly: turn 1 rode the verbatim side lane - // — no refs minted, no request counted (the session shell may exist for - // lane bookkeeping, but it holds no compression state). const stub = peekSession(CONV); const refs1 = stub ? Object.keys(stub.state.messageRefs.byRaw).length : 0; const reqs1 = stub ? stub.stats.requests : 0; From f37fd037f053760dc2d25a6f4bee04120c589a81 Mon Sep 17 00:00:00 2001 From: wxy <2398249916@qq.com> Date: Thu, 5 Nov 2026 13:58:57 +0800 Subject: [PATCH 32/78] fix: identify billion-context in pi extensions (cherry picked from commit 420fb168d0b493d6709b595782ee05aa12c8096a) --- docs/configuration.md | 2 +- package.json | 2 +- scripts/write-build-info.mjs | 1 + tests/e2e/e2e-native-pi.test.ts | 6 +++--- tests/pi-native.test.ts | 5 +++++ 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index daefdcefec..382c4ab7da 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2518,7 +2518,7 @@ bili plugin list # install status for every supported host bili plugin remove pi # undo (original files backed up to *.bili-bak once) ``` -`install pi` also replaces any **legacy** billion-context entries (old `npm:billion-context-pi` references, stale `npm:billion-context@x.y.z`, leftover dev-checkout paths) so exactly one bili plugin stays live; when it drops entries the output also reminds you that a **project-scope** entry (`pi install -l`, written to `/.pi/settings.json`) is outside this global settings and must be removed by hand. With an npm-installed bili the written entry is the pi-managed spec `npm:billion-context` — pi installs it into `~/.pi/agent/npm/` automatically (also on startup if missing) and `pi update` upgrades it, so the entry survives node prefix moves across machines; a dev/checkout install keeps the machine-local abs path (pi loads local package dirs directly). Both load the `pi` manifest → `dist/agent/pi-native.js`, the self-spawning native entry — bare `pi` gets full plugin-mode compression with no launcher (#519). +`install pi` also replaces any **legacy** billion-context entries (old `npm:billion-context-pi` references, stale `npm:billion-context@x.y.z`, leftover dev-checkout paths) so exactly one bili plugin stays live; when it drops entries the output also reminds you that a **project-scope** entry (`pi install -l`, written to `/.pi/settings.json`) is outside this global settings and must be removed by hand. With an npm-installed bili the written entry is the pi-managed spec `npm:billion-context` — pi installs it into `~/.pi/agent/npm/` automatically (also on startup if missing) and `pi update` upgrades it, so the entry survives node prefix moves across machines; a dev/checkout install keeps the machine-local abs path (pi loads local package dirs directly). Both load the `pi` manifest → `dist/agent/billion-context.js`, which delegates to the self-spawning native entry `dist/agent/pi-native.js` — bare `pi` gets full plugin-mode compression with no launcher (#519). **Coexistence with the standalone `billion-context-pi` extension.** The two are mutually exclusive — both active means double compression. Three nets: the installer strip above (global settings), the `BILLION_CONTEXT_NATIVE=pi` marker the native entry sets synchronously at load (`billion-context-pi` **0.1.72+** re-reads it on every event and refuses; 0.1.71 and older check `BILLION_CONTEXT_PROXY` only at load time, before the native proxy exists), and a runtime scan: once its proxy is up, the pi-native entry reads both pi settings files and warns loudly about any co-resident legacy entry the installer never saw (project scope, or installed after the fact). diff --git a/package.json b/package.json index 13ff1da560..c8a40ff763 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ "pi": { "image": "https://raw.githubusercontent.com/ranxianglei/billion-context/master/docs/logo.png", "extensions": [ - "./dist/agent/pi-native.js" + "./dist/agent/billion-context.js" ] }, "publishConfig": { diff --git a/scripts/write-build-info.mjs b/scripts/write-build-info.mjs index e7cff9b618..b2fd48d54b 100644 --- a/scripts/write-build-info.mjs +++ b/scripts/write-build-info.mjs @@ -33,4 +33,5 @@ const info = { commit: buildCommit() }; const distDir = resolve(root, "dist"); mkdirSync(distDir, { recursive: true }); writeFileSync(resolve(distDir, "build-info.json"), `${JSON.stringify(info, null, 2)}\n`); +writeFileSync(resolve(distDir, "agent/billion-context.js"), 'export { default } from "./pi-native.js";\n'); console.log(`build-info: commit=${info.commit}`); diff --git a/tests/e2e/e2e-native-pi.test.ts b/tests/e2e/e2e-native-pi.test.ts index 7f0ee66eb3..151b1606e1 100644 --- a/tests/e2e/e2e-native-pi.test.ts +++ b/tests/e2e/e2e-native-pi.test.ts @@ -4,7 +4,7 @@ // interception, x-bili-plugin stamping, ACP tool registration, plugin-tool // execution (acp_status / compress) and real compression — runs in-process // with zero tokens and no network. Gated by ACP_TEST_E2E_NATIVE=1 (needs -// `npm run build` first — the pi package loads dist/agent/pi-native.js). +// `npm run build` first — the pi package loads dist/agent/billion-context.js). // // Assertions map 1:1 to #1239's acceptance list: // 1. traffic is intercepted + plugin-mode claimed (x-bili-plugin: pi) @@ -34,7 +34,7 @@ import { assertPortDead } from "../port-race.js"; const PI_BIN = process.env.E2E_PI_BIN ?? "pi"; const REPO_ROOT = path.resolve(import.meta.dirname, "../.."); -const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/pi-native.js"); +const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/billion-context.js"); const FAKE_UPSTREAM = path.join(import.meta.dirname, "fake-upstream-chat.mjs"); const TMO = Number(process.env.E2E_TMO ?? 150_000); const WORK_ROOT = path.join(process.cwd(), "tmp"); @@ -112,7 +112,7 @@ const skipReason = !run const suiteSkipReason = skipReason ?? (!distBuilt() - ? "dist/agent/pi-native.js missing — run `npm run build` first" + ? "dist/agent/billion-context.js missing — run `npm run build` first" : undefined); const checkOnly = process.env.E2E_CHECK === "1"; diff --git a/tests/pi-native.test.ts b/tests/pi-native.test.ts index e17d58da5d..2fa19d46bb 100644 --- a/tests/pi-native.test.ts +++ b/tests/pi-native.test.ts @@ -24,6 +24,11 @@ delete process.env.BILLION_CONTEXT_PROXY; // #820 coexistence: standalone billion-context-pi checks env at load time, so // the marker must be set synchronously during module evaluation (before our // async bootstrap writes BILLION_CONTEXT_PROXY) for its action-time back-off. +test("Pi package entry identifies billion-context", () => { + const manifest = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8")); + assert.deepEqual(manifest.pi.extensions, ["./dist/agent/billion-context.js"]); +}); + test("module evaluation marks the process as a native pi host", () => { assert.equal(process.env.BILLION_CONTEXT_NATIVE, "pi"); }); From 49dc5bd0e51d1254ce2f3108ac0d7dcfa4824bd8 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 14:57:19 +0800 Subject: [PATCH 33/78] test: align remaining pi-lane e2e + docs to the billion-context.js entry Follow-up to #5712 (which fixes #5711): that PR repointed the pi manifest at the new dist/agent/billion-context.js entry and updated e2e-native-pi.test.ts, but left three sibling references on the old dist/agent/pi-native.js name. Point them at the shipped entry so each real-pi suite's build-exists gate verifies the actual file pi loads, and keep the header comment accurate (billion-context.js is a re-export of pi-native.js). No behavior change. (cherry picked from commit 3f81eff4af8b20dd5fad9232ea7056dfeccc3bd3) --- tests/e2e/README.md | 4 ++-- tests/e2e/e2e-delegate-notify.test.ts | 9 +++++---- tests/e2e/e2e-pi-codex-ws.test.ts | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/e2e/README.md b/tests/e2e/README.md index d03c54c90b..f5bfb2f2cf 100644 --- a/tests/e2e/README.md +++ b/tests/e2e/README.md @@ -228,7 +228,7 @@ asserts the four user-facing guarantees of #1239: (`stopProxiesGracefully`) before asserting on-disk state; teardown then SIGKILLs survivors (fake + instance-record pids). - `E2E_CHECK=1` runs a zero-cost preflight (pi binary version, built - `dist/agent/pi-native.js`, fake `/v1/models` probe). `E2E_PI_BIN` / + `dist/agent/billion-context.js`, fake `/v1/models` probe). `E2E_PI_BIN` / `E2E_TMO` override the binary and per-run timeout. ### CI @@ -304,7 +304,7 @@ provider pointed at a local mock that speaks BOTH legs — POST/SSE and a WebSocket upgrade on one port — through the real built proxy. Zero tokens. Gated: `ACP_TEST_E2E_PI_WS=1` (skip-by-default like the other real-client -lanes); needs the built `dist/agent/pi-native.js`. Override the binary with +lanes); needs the built `dist/agent/billion-context.js`. Override the binary with `E2E_PI_BIN`. ```bash diff --git a/tests/e2e/e2e-delegate-notify.test.ts b/tests/e2e/e2e-delegate-notify.test.ts index b084fa2a76..3db59cd3ea 100644 --- a/tests/e2e/e2e-delegate-notify.test.ts +++ b/tests/e2e/e2e-delegate-notify.test.ts @@ -27,8 +27,9 @@ // new turn from idle. // // Gated by ACP_TEST_E2E_NATIVE=1 (same gate as e2e-native-pi.test.ts; needs -// `npm run build` first — the pi package loads dist/agent/pi-native.js, which -// inlines the vendored pi-subagents source). Skips when the pi CLI is absent. +// `npm run build` first — the pi package loads dist/agent/billion-context.js, a +// re-export of pi-native.js (which inlines the vendored pi-subagents source)). +// Skips when the pi CLI is absent. import { spawn, spawnSync } from "node:child_process"; import fs from "node:fs"; @@ -42,7 +43,7 @@ import { assertPortDead } from "../port-race.js"; const PI_BIN = process.env.E2E_PI_BIN ?? "pi"; const REPO_ROOT = path.resolve(import.meta.dirname, "../.."); -const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/pi-native.js"); +const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/billion-context.js"); const FAKE_UPSTREAM = path.join(import.meta.dirname, "fake-upstream-chat.mjs"); const TMO_MS = Number(process.env.E2E_TMO ?? 180_000); const QUIET_MS = 12_000; // sustained quiescence before declaring the scenario done @@ -386,7 +387,7 @@ function scenarioSkip(): string | false { if (process.env.ACP_TEST_E2E_NATIVE !== "1") { return "set ACP_TEST_E2E_NATIVE=1 (real pi RPC lane + local fake upstream; deterministic, zero tokens)"; } - if (!fs.existsSync(PI_NATIVE_ENTRY)) return "dist/agent/pi-native.js missing — run `npm run build` first"; + if (!fs.existsSync(PI_NATIVE_ENTRY)) return "dist/agent/billion-context.js missing — run `npm run build` first"; if (!piAvailable()) return `${PI_BIN} CLI not available`; return false; } diff --git a/tests/e2e/e2e-pi-codex-ws.test.ts b/tests/e2e/e2e-pi-codex-ws.test.ts index a8df48ae7c..15788e0224 100644 --- a/tests/e2e/e2e-pi-codex-ws.test.ts +++ b/tests/e2e/e2e-pi-codex-ws.test.ts @@ -32,7 +32,7 @@ process.env.NODE_ENV = "test"; const PI_BIN = process.env.E2E_PI_BIN ?? "pi"; const REPO_ROOT = path.resolve(import.meta.dirname, "../.."); -const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/pi-native.js"); +const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/billion-context.js"); const TMO = Number(process.env.E2E_TMO ?? 180_000); const WORK_ROOT = path.join(process.cwd(), "tmp"); fs.mkdirSync(WORK_ROOT, { recursive: true }); @@ -52,7 +52,7 @@ function piAvailable(): boolean { const skipReason = !run ? "set ACP_TEST_E2E_PI_WS=1 (real pi + local mock WS upstream; deterministic, zero tokens)" : !fs.existsSync(PI_NATIVE_ENTRY) - ? "dist/agent/pi-native.js missing — run `npm run build` first" + ? "dist/agent/billion-context.js missing — run `npm run build` first" : !piAvailable() ? `pi binary "${PI_BIN}" not found on PATH` : undefined; From 740771d376d6fa65a0a2641dadb9d71f04da1730 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 15:31:58 +0800 Subject: [PATCH 34/78] docs: drop stray closing paren in delegate-notify header comment (cherry picked from commit c73ac4c6951a6b4bdd5208d44126a0bccf4f31bd) --- tests/e2e/e2e-delegate-notify.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/e2e-delegate-notify.test.ts b/tests/e2e/e2e-delegate-notify.test.ts index 3db59cd3ea..0a5103b4b4 100644 --- a/tests/e2e/e2e-delegate-notify.test.ts +++ b/tests/e2e/e2e-delegate-notify.test.ts @@ -28,7 +28,7 @@ // // Gated by ACP_TEST_E2E_NATIVE=1 (same gate as e2e-native-pi.test.ts; needs // `npm run build` first — the pi package loads dist/agent/billion-context.js, a -// re-export of pi-native.js (which inlines the vendored pi-subagents source)). +// re-export of pi-native.js (which inlines the vendored pi-subagents source). // Skips when the pi CLI is absent. import { spawn, spawnSync } from "node:child_process"; From fe40084c875d805891596b7b8d3c97af4d4a4a9e Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 17:22:14 +0800 Subject: [PATCH 35/78] fix: create dist/agent in write-build-info so the shim write is self-sufficient (cherry picked from commit 08eba3a7277faaf03e0f4f9f4682c27b5d4841ad) --- scripts/write-build-info.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/write-build-info.mjs b/scripts/write-build-info.mjs index b2fd48d54b..ce707551dc 100644 --- a/scripts/write-build-info.mjs +++ b/scripts/write-build-info.mjs @@ -31,7 +31,7 @@ function buildCommit() { const info = { commit: buildCommit() }; const distDir = resolve(root, "dist"); -mkdirSync(distDir, { recursive: true }); +mkdirSync(resolve(distDir, "agent"), { recursive: true }); writeFileSync(resolve(distDir, "build-info.json"), `${JSON.stringify(info, null, 2)}\n`); writeFileSync(resolve(distDir, "agent/billion-context.js"), 'export { default } from "./pi-native.js";\n'); console.log(`build-info: commit=${info.commit}`); From 19ba8ad0968966b9d715b9011178e47624c3d4bf Mon Sep 17 00:00:00 2001 From: Mario <46114603+mariopasquali@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:55:36 +0100 Subject: [PATCH 36/78] fix(incremental): continue repeated-content IDs across delta turns (cherry picked from commit 9c3faf3cb9d3b7ab2b6d498892c47125cd552f87) --- src/incremental-lane.ts | 38 ++++++++++++++++++++- tests/incremental-content-clusters.test.ts | 34 +++++++++++++++++++ tests/opencode-rolling-cache-marks.test.ts | 39 ++++++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 tests/incremental-content-clusters.test.ts diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index a233e6f5b9..22ea84aa4c 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -561,6 +561,38 @@ function hashWireBoundary(m: { role: string; content: unknown }): string { return createHash("sha256").update(`${m.role}\u0000${JSON.stringify(m.content)}`).digest("hex").slice(0, 16); } +const CONTENT_ID = /^(h_[0-9a-f]{16})(?:_([1-9]\d*))?$/; + +/** Continue separately converted content-ID clusters without rewriting any + * payload. A sparse restored prefix must skip its highest retained ordinal. */ +export function continueContentClusters( + prefix: readonly BiliMessage[], + delta: { msgs: BiliMessage[]; cacheControls: Map }, +): { msgs: BiliMessage[]; cacheControls: Map } { + const offsets = new Map(); + for (const message of prefix) { + const hit = CONTENT_ID.exec(message.id); + if (!hit) continue; + const ordinal = Number(hit[2] ?? 0); + if (Number.isSafeInteger(ordinal + 1)) offsets.set(hit[1], Math.max(offsets.get(hit[1]) ?? 0, ordinal + 1)); + } + if (offsets.size === 0) return delta; + const renamed = new Map(); + const msgs = delta.msgs.map((message) => { + const hit = CONTENT_ID.exec(message.id); + if (!hit) return message; + const offset = offsets.get(hit[1]) ?? 0; + if (offset === 0) return message; + const ordinal = offset + Number(hit[2] ?? 0); + if (!Number.isSafeInteger(ordinal)) throw new Error("Content identity cluster exceeds safe ordinal range"); + const id = `${hit[1]}_${ordinal}`; + renamed.set(message.id, id); + return { ...message, id }; + }); + if (renamed.size === 0) return delta; + return { msgs, cacheControls: new Map([...delta.cacheControls].map(([id, mark]) => [renamed.get(id) ?? id, mark])) }; +} + function applyTurn( entry: Canonical, sinceSeq: number, @@ -615,7 +647,11 @@ function applyTurn( retouchMarks.push({ ids, marks, rawBlocks }); } } - const flat = deltaWire.length === 0 ? { msgs: [], cacheControls: new Map() } : anthropicToCore({ messages: deltaWire as never } as never); + const converted = deltaWire.length === 0 ? { msgs: [], cacheControls: new Map() } : anthropicToCore({ messages: deltaWire as never } as never); + // A new conversion starts each duplicate cluster at zero. Continue the + // acknowledged prefix so a repeated prompt cannot reuse an earlier row's + // identity (and receive the new row's cache mark as a side effect). + const flat = resync ? converted : continueContentClusters(entry.msgs, converted); let deltaMsgs = flat.msgs; if (deltaIds !== undefined) { const v = validateMsgIds(deltaIds, flat.msgs.length); diff --git a/tests/incremental-content-clusters.test.ts b/tests/incremental-content-clusters.test.ts new file mode 100644 index 0000000000..7ad887c8e7 --- /dev/null +++ b/tests/incremental-content-clusters.test.ts @@ -0,0 +1,34 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { anthropicToCore, type BiliMessage } from "acp-kernel/wire"; +import { continueContentClusters } from "../src/incremental-lane.js"; + +test("content clusters match one full conversion across repeated delta boundaries", () => { + const messages = Array.from({ length: 40 }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: [{ type: "text", text: i % 3 === 0 ? "continue" : "ok", cache_control: { type: "ephemeral" } }], + })); + for (let split = 1; split < messages.length; split++) { + const prefix = anthropicToCore({ messages: messages.slice(0, split) } as never); + const delta = anthropicToCore({ messages: messages.slice(split) } as never); + const before = JSON.stringify(delta.msgs); + const continued = continueContentClusters(prefix.msgs, delta); + const full = anthropicToCore({ messages } as never); + assert.deepEqual([...prefix.msgs, ...continued.msgs].map(({ id }) => id), full.msgs.map(({ id }) => id)); + assert.deepEqual(new Map([...prefix.cacheControls, ...continued.cacheControls]), full.cacheControls); + assert.equal(JSON.stringify(delta.msgs), before, "input messages remain unchanged"); + } +}); + +test("sparse content clusters skip the highest retained suffix; other namespaces remain exact", () => { + const base = "h_0123456789abcdef"; + const msg = (id: string): BiliMessage => ({ id, role: "user", contentType: "text", text: "literal payload", rawAnthropicBlock: { type: "document", source: { data: "exact bytes" } } }); + const delta = { msgs: [msg(base), msg("client-pinned")], cacheControls: new Map([[base, { type: "ephemeral" }]]) }; + const shifted = continueContentClusters([msg(base), msg(`${base}_9`)], delta); + assert.equal(shifted.msgs[0].id, `${base}_10`); + assert.equal(shifted.msgs[0].rawAnthropicBlock, delta.msgs[0].rawAnthropicBlock); + assert.equal(shifted.msgs[1], delta.msgs[1]); + assert.ok(shifted.cacheControls.has(`${base}_10`)); + assert.ok(!shifted.cacheControls.has(base)); + assert.equal(continueContentClusters([msg("client-pinned")], delta), delta); +}); diff --git a/tests/opencode-rolling-cache-marks.test.ts b/tests/opencode-rolling-cache-marks.test.ts index b2fbaa8ef4..69fccac194 100644 --- a/tests/opencode-rolling-cache-marks.test.ts +++ b/tests/opencode-rolling-cache-marks.test.ts @@ -230,6 +230,45 @@ test("rolling cache_control: a retouch whose content differs from the canonical } }); +test("rolling marks: repeated prompts retain distinct IDs and match full resend", async () => { + const history: Wire[] = []; + const requests: Wire[][] = []; + for (let i = 0; i < 6; i++) { + history.push({ role: "user", content: [{ type: "text", text: "continue" }] }); + requests.push(decorate(history)); + history.push({ role: "assistant", content: [{ type: "text", text: "ok" }] }); + } + const base = await harness(); + try { await runFullResend(base, requests); } finally { await base.close(); } + const inc = await harness(); + try { + const { modes } = await runIncremental(inc, requests); + assert.deepEqual(modes, ["seed", ...requests.slice(1).map(() => "delta")]); + for (let i = 0; i < requests.length; i++) { + assert.equal(JSON.stringify(inc.forwarded[i].messages), JSON.stringify(base.forwarded[i].messages)); + assert.equal(messageBreakpoints(inc.forwarded[i]), messageBreakpoints({ messages: requests[i] })); + } + } finally { await inc.close(); } +}); + +test("continued content IDs can be replaced by client pins without losing the new mark", async () => { + const inc = await harness(); + try { + const common = { conversationId: CONV, path: "/v1/messages", system: SYSTEM, request: { model: "claude-test", max_tokens: 1024 } }; + const seed = await inc.post("/__bili/plugin/turn", JSON.stringify({ ...common, sinceSeq: 0, resync: true, messages: [{ role: "user", content: [{ type: "text", text: "continue" }] }] }), laneHeaders); + assert.equal(seed.status, 200); + const delta = await inc.post("/__bili/plugin/turn", JSON.stringify({ ...common, sinceSeq: seed.appliedSeq, msgIds: ["pinned-reply", "pinned-repeat"], messages: [ + { role: "assistant", content: [{ type: "text", text: "ok" }] }, + { role: "user", content: [{ type: "text", text: "continue", cache_control: MARK }] }, + ] }), laneHeaders); + assert.equal(delta.status, 200); + const messages = inc.forwarded.at(-1)!.messages as Wire[]; + assert.equal(messageBreakpoints(inc.forwarded.at(-1)!), 1); + assert.deepEqual(messages.at(-1)?.content.at(-1)?.cache_control, MARK); + assert.ok(messages[0].content.every((block) => block.cache_control === undefined), "the earlier identical prompt stays unmarked"); + } finally { await inc.close(); } +}); + test("rolling marks follow client-pinned identities on seed and delta", async () => { const inc = await harness(); try { From bc07a9ce7d457616ee71e24c90de9716075d019e Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 21:24:29 +0800 Subject: [PATCH 37/78] test(rolling-marks): compare instances on substance, not wall-clock tag times MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rendered ACP tags embed a wall-clock time attribute (kernel render-refs), so raw byte-equality across two independent proxy instances can never hold once the two runs straddle a second boundary — every cross-instance comparison in this file was flaky by construction (observed: 8 of 10 pre-existing assertions plus the new repeated-prompt assertion failing on a single seconds digit while all substantive bytes matched). Strip the time attribute before comparing; every other byte still must match exactly. Test-only change; product behavior unchanged. (cherry picked from commit e2fad486cfc16c5279cfe895090055b09288b21a) --- tests/opencode-rolling-cache-marks.test.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/tests/opencode-rolling-cache-marks.test.ts b/tests/opencode-rolling-cache-marks.test.ts index 69fccac194..af920378aa 100644 --- a/tests/opencode-rolling-cache-marks.test.ts +++ b/tests/opencode-rolling-cache-marks.test.ts @@ -183,8 +183,7 @@ test("rolling cache_control: deltas after the seed, upstream bodies identical to assert.deepEqual(modes.slice(1), requests.slice(1).map(() => "delta"), "a moving breakpoint never forces a resync"); assert.equal(inc.forwarded.length, base.forwarded.length); for (let i = 0; i < requests.length; i++) { - assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages), `request ${i}: assembled messages byte-identical to the host's full resend`); - assert.equal(messageBreakpoints(inc.forwarded[i]), messageBreakpoints({ messages: requests[i] }), `request ${i}: only the host's rolling breakpoints (no accumulation)`); + assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages), `request ${i}: assembled messages byte-identical to the host's full resend`); assert.equal(messageBreakpoints(inc.forwarded[i]), messageBreakpoints({ messages: requests[i] }), `request ${i}: only the host's rolling breakpoints (no accumulation)`); } const last = sizes[sizes.length - 1]; assert.ok(last.sent * 3 < last.full, `delta (${last.sent} B) is a fraction of the full body (${last.full} B)`); @@ -206,8 +205,7 @@ test("rolling cache_control: a retouch against a cold canonical answers 409 and const { modes } = await runIncremental(inc, requests, { restartBefore: 3 }); assert.deepEqual(modes, ["seed", "delta", "delta", "delta:409", "seed", "delta", "delta"], "cold canonical: one 409, one re-seed, then deltas again"); for (let i = 0; i < requests.length; i++) { - assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages), `request ${i} byte-identical after recovery`); - } + assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages), `request ${i} byte-identical after recovery`); } } finally { await inc.close(); } @@ -245,7 +243,7 @@ test("rolling marks: repeated prompts retain distinct IDs and match full resend" const { modes } = await runIncremental(inc, requests); assert.deepEqual(modes, ["seed", ...requests.slice(1).map(() => "delta")]); for (let i = 0; i < requests.length; i++) { - assert.equal(JSON.stringify(inc.forwarded[i].messages), JSON.stringify(base.forwarded[i].messages)); + assert.equal(JSON.stringify(withoutTagTime(inc.forwarded[i].messages)), JSON.stringify(withoutTagTime(base.forwarded[i].messages))); assert.equal(messageBreakpoints(inc.forwarded[i]), messageBreakpoints({ messages: requests[i] })); } } finally { await inc.close(); } @@ -309,8 +307,7 @@ for (const kind of ["document", "image", "tool-result", "signed-thinking"] as co const { modes } = await runIncremental(inc, requests); assert.deepEqual(modes, ["seed", "delta"]); for (let i = 0; i < requests.length; i++) { - assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages)); - } + assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages)); } } finally { await inc.close(); } }); } @@ -341,7 +338,6 @@ for (const removed of ["signature", "is_error"] as const) { const good = await inc.post("/__bili/plugin/turn", JSON.stringify(body), laneHeaders); assert.equal(good.status, 200, "same sequence still accepts the exact original"); assert.equal(inc.forwarded.length, before + 1); - assert.equal(canonMessages(inc.forwarded.at(-1)?.messages), canonMessages(base.forwarded.at(-1)?.messages), "rejected retouch changed neither payload nor the valid retry's rendered output"); - } finally { await inc.close(); } + assert.equal(canonMessages(inc.forwarded.at(-1)?.messages), canonMessages(base.forwarded.at(-1)?.messages), "rejected retouch changed neither payload nor the valid retry's rendered output"); } finally { await inc.close(); } }); } From d6fb1cc02abb0310b247aa1078f017b5cd85a329 Mon Sep 17 00:00:00 2001 From: "billion-context[bot]" Date: Fri, 6 Nov 2026 09:30:12 +0800 Subject: [PATCH 38/78] fix(opencode): ride REAL host msgIds into the incremental lane from both seams MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (content clusters continued across delta turns) and give the lane what it was designed to receive — the host's own session-scoped message ids. What was wrong with the premise: opencode 2.x was assumed to expose no host ids ("weakest of the four host seams"). It does: ctx.session.hook("context") fires immediately before EVERY provider-request assembly carrying the full { id, role, content }[] conversation plus sessionID (verified live on @opencode/cli 2.0.4, anthropic AND openai-compatible providers). V1 keeps its experimental.chat.messages.transform seam as before; both now feed one per-session store. With real ids riding, byte-identical repeated prompts get distinct vouched ids (msg_r1 vs msg_r2), so the server-side pin creates one row per occurrence and the collision class of #5741 disappears at the root. Changes: - opencode-v2.ts: register the "context" hook in createOpencodeV2Setup (shared by V1+V2 hosts; try/catch — hosts without the seam keep the documented id-less degrade; never fail startup over it) - opencode-msgids.ts: deriveEntry now proves BOTH event shapes onto the wire 1:1 with semantic part tokens (text bytes; tool call/result anchored by provider tool-use id, not input bytes; reasoning↔thinking; media↔image); host role "tool" normalizes to wire "user"; bookkeeping parts skip; ANY unrecognized shape still drops the whole field (宁可降级不可错位) - tests/opencode-msgids.test.ts: tool-session end-to-end through the V2 seam, the #5741 repeated-prompt scenario (distinct fresh ids per occurrence), reasoning/thinking alignment, unknown-part and duplicate-id degrade paths Server side needed NO changes: the claimed-set validation and pin remap landed with #5742/#5743 already. continueContentClusters stays as the fallback path for hosts/turns without a usable harvest. Verified: tsc --noEmit clean; opencode-incremental/msgids/rolling-marks, incremental-content-clusters, msgid-pin/algebra/lane-flow, dsh/pi-msgids, incremental-lane, issue5403 fastpath, pins-wire-id-mints all green. (cherry picked from commit 2fa9dc47cc828d0cea417faa02c4c26d745ea493) --- src/agent/opencode-msgids.ts | 223 ++++++++++++++++----- src/agent/opencode-native.ts | 6 +- src/agent/opencode-v2.ts | 2 +- tests/opencode-msgids.test.ts | 199 +++++++++++++++++- tests/opencode-rolling-cache-marks.test.ts | 2 +- 5 files changed, 371 insertions(+), 61 deletions(-) diff --git a/src/agent/opencode-msgids.ts b/src/agent/opencode-msgids.ts index 53e08311e6..acc02ecd6b 100644 --- a/src/agent/opencode-msgids.ts +++ b/src/agent/opencode-msgids.ts @@ -1,34 +1,46 @@ // #4491 host wiring (opencode): client msgIds for incremental-lane turns. // -// opencode 1.x fires `experimental.chat.messages.transform` per LLM request -// with `{ info, parts }[]` — info.id is the host message id (msg_ + 12 hex + -// 14 base62; zero reuse observed across 378K messages, and a -// streaming-settled message re-fires under the SAME id with grown content — -// edit semantics the server-side history reconciles, so the host side only -// ever stores the LATEST fire per session). That hook is the ONLY opencode -// seam carrying host ids: the V2 `http.request` interception sees the final -// wire body and nothing else, so V2-shaped turns never carry msgIds — they -// degrade to the lane's content-derived identities by design (#4491 host -// wiring audit; opencode is the weakest of the four host seams). +// Two opencode host seams carry the host's own message ids (msg_ + base62 +// tail, zero reuse observed across 378K messages; a streaming-settled +// message re-fires under the SAME id with grown content — edit semantics +// the server-side history reconciles, so each harvester only ever stores +// the LATEST fire per session): +// • V1 (`opencode` ≤1.x): `experimental.chat.messages.transform` fires +// per LLM request with `{ info, parts }[]`. +// • V2 (`opencode` ≥2.0): `ctx.session.hook("context")` fires right +// before every provider request assembly with the full conversation +// list `{ id, role, content }[]` (roles user/assistant/tool) plus +// sessionID — verified live on 2.0.4, anthropic AND openai-compatible +// providers alike. (The V2 `http.request` seam itself sees only the +// final wire body; ids reach the planner through this harvest.) // // Alignment is proof, not guesswork (宁可降级不可错位): ids ride only when -// every harvested message provably lowers 1:1 onto the wire — text-only -// parts, role user|assistant, and the derived text sequence exactly equal to -// the observed wire messages (cache_control and other block props ignored). -// Any deviation — tool/reasoning/file parts, ignored or synthetic text, a -// missing or foreign-shaped id, a content drift, duplicate ids — drops the -// whole field and the turn rides exactly as before. Order-preserving lowering -// plus per-position equality is what makes the positional pairing safe; a -// compensating merge+split that still byte-matches every position can only -// swap ids between byte-identical content, which the ledger joins as one. +// every harvested message provably lowers onto the wire 1:1 — same role +// (host `tool` normalizes to wire `user`) and an exactly-equal ordered +// token sequence, where each token fingerprints one semantic part: +// t␀ text blocks (byte-compared, cache_control ignored) +// c␀␀ assistant tool call ↔ wire tool_use (anchored by the +// provider-unique tool-use id, not input bytes — the +// id IS the identity; input lowering fidelity varies) +// r␀␀ tool result ↔ wire tool_result (same anchor) +// o:r reasoning part ↔ thinking/redacted_thinking block +// o:m media part ↔ image/document block +// Host bookkeeping parts that never reach the wire (step markers, +// snapshots, patches) are skipped on the host side; ANY other unrecognized +// part type, a missing or foreign-shaped id, a count/role/token mismatch, +// or duplicate ids drops the WHOLE field and the turn rides exactly as +// before (content-derived identities). Order-preserving lowering plus +// per-position equality makes the positional pairing safe; a compensating +// merge+split that still byte-matches every position can only swap ids +// between identical-fingerprint content, which the ledger joins as one. interface HarvestedEntry { /** Host message id; "" when unusable (missing/foreign-shaped) — poisons * alignment for any list containing it, never the lane itself. */ id: string; - /** Derivable wire shape ({role, ordered text-part texts}), or null when + /** Derivable wire shape ({role, ordered part tokens}), or null when * this message cannot be proven to lower 1:1 onto one wire message. */ - shape: { role: "user" | "assistant"; texts: string[] } | null; + shape: { role: "user" | "assistant"; toks: string[] } | null; } const MAX_SESSIONS = 256; @@ -39,27 +51,97 @@ const harvests = new Map(); * and control characters, the kernel's acp_summary_ namespace, empty ids). */ const HOST_MSG_ID = /^msg_[A-Za-z0-9]+$/; -function deriveEntry(raw: unknown): HarvestedEntry { - const holder = raw as { info?: unknown; parts?: unknown } | null | undefined; - const info = holder !== null && typeof holder === "object" ? holder.info : undefined; - const parts = holder !== null && typeof holder === "object" ? holder.parts : undefined; - const infoObj = info !== null && typeof info === "object" ? (info as { id?: unknown; role?: unknown }) : undefined; - const id = typeof infoObj?.id === "string" && HOST_MSG_ID.test(infoObj.id) ? infoObj.id : ""; - if (infoObj?.role !== "user" && infoObj?.role !== "assistant") return { id, shape: null }; +/** Host bookkeeping parts that produce no wire block — present in the host + * store (and in these events) but rendered client-side only. */ +const NOISE_PART_TYPES = new Set(["step-start", "step-finish", "snapshot", "patch"]); + +const T = "\u0000"; // token separator — cannot appear in well-formed JSON text + +function str(v: unknown): string | undefined { + return typeof v === "string" ? v : undefined; +} + +function obj(v: unknown): Record | undefined { + return v !== null && typeof v === "object" ? (v as Record) : undefined; +} + +/** Tokenize ONE host part (V1 `{ parts }` and V2 `{ content }` share the + * relevant fields). Returns the token, `false` for known-noise parts, or + * `null` when the part type cannot be proven to lower onto the wire. */ +function hostPartToken(part: unknown, hostRole: string): string | false | null { + const p = obj(part); + if (p === undefined) return null; + const type = str(p.type); + switch (type) { + case "text": { + if (p.ignored === true || p.synthetic === true) return null; // never reaches the model + const text = str(p.text); + return text === undefined ? null : `t${T}${text}`; + } + case "reasoning": + return "o:r"; + case "file": + case "image": + return "o:m"; + // V2 ai-sdk style tool parts. + case "tool-call": { + const id = str(p.id); + const name = str(p.name); + if (id === undefined || name === undefined) return null; + return `c${T}${id}${T}${name}`; + } + case "tool-result": { + const id = str(p.id); + if (id === undefined) return null; + const result = obj(p.result); + const isErr = result?.type === "error"; + return `r${T}${id}${T}${isErr ? "1" : "0"}`; + } + // V1 legacy tool part: lives on the assistant (call) or user/tool + // (settled result) side — anchored by callID either way. + case "tool": { + const id = str(p.callID) ?? str(p.id); + if (id === undefined) return null; + if (hostRole === "assistant") { + const name = str(p.tool) ?? ""; + return `c${T}${id}${T}${name}`; + } + const state = obj(p.state); + const isErr = state?.status === "error"; + return `r${T}${id}${T}${isErr ? "1" : "0"}`; + } + default: + return type !== undefined && NOISE_PART_TYPES.has(type) ? false : null; + } +} + +/** Normalize one harvested host message (either seam's element shape) to + * { id, shape }. Never throws — unparseable elements poison their own + * session's alignment only. */ +export function deriveOpencodeEntry(raw: unknown): HarvestedEntry { + const holder = obj(raw); + // V1: { info: { id, role }, parts: [...] }; V2 context: { id, role, content: [...] }. + const info = holder === undefined ? undefined : obj(holder.info); + const idSrc = holder === undefined ? undefined : str(holder.id) ?? str(info?.id); + const id = idSrc !== undefined && HOST_MSG_ID.test(idSrc) ? idSrc : ""; + const roleRaw = info?.role ?? holder?.role; + const parts = holder?.parts ?? holder?.content; if (!Array.isArray(parts)) return { id, shape: null }; - const texts: string[] = []; + // Host `tool` messages lower onto wire `user` tool_result blocks. + const normRole = roleRaw === "user" || roleRaw === "assistant" ? roleRaw : roleRaw === "tool" ? "user" : undefined; + if (normRole === undefined) return { id, shape: null }; + const toks: string[] = []; for (const part of parts) { - const p = part !== null && typeof part === "object" ? (part as { type?: unknown; text?: unknown; ignored?: unknown; synthetic?: unknown }) : undefined; - // ignored text never reaches the model (host-render notices) and - // synthetic text has no proven lowering — both stay underivable. - if (p === undefined || p.type !== "text" || p.ignored === true || p.synthetic === true || typeof p.text !== "string") return { id, shape: null }; - texts.push(p.text); + const tok = hostPartToken(part, String(roleRaw)); + if (tok === null) return { id, shape: null }; + if (tok !== false) toks.push(tok); } - return { id, shape: { role: infoObj.role, texts } }; + return { id, shape: { role: normRole, toks } }; } -/** Record the latest messages.transform fire for one session (V1 hook). The - * host's outgoing messages are left untouched — harvesting never mutates. */ +/** Record the latest id-bearing fire for one session (V1 messages.transform + * or V2 "context"). The host's outgoing messages are left untouched — + * harvesting never mutates. */ export function harvestOpencodeMsgIds(sid: string | undefined, messages: unknown): void { if (typeof sid !== "string" || sid.length === 0 || !Array.isArray(messages)) return; if (!harvests.has(sid) && harvests.size >= MAX_SESSIONS) { @@ -70,27 +152,58 @@ export function harvestOpencodeMsgIds(sid: string | undefined, messages: unknown } if (oldestKey !== undefined) harvests.delete(oldestKey); } - harvests.set(sid, { at: Date.now(), entries: messages.map(deriveEntry) }); + harvests.set(sid, { at: Date.now(), entries: messages.map(deriveOpencodeEntry) }); +} + +/** Token for one ANTHROPIC-WIRE content block, or null when the block type + * cannot be matched against a host part. */ +function wireBlockToken(block: unknown): string | null { + const b = obj(block); + if (b === undefined) return null; + switch (b.type) { + case "text": { + const text = str(b.text); + return text === undefined ? null : `t${T}${text}`; + } + case "tool_use": { + const id = str(b.id); + const name = str(b.name); + return id === undefined || name === undefined ? null : `c${T}${id}${T}${name}`; + } + case "tool_result": { + const id = str(b.tool_use_id); + return id === undefined ? null : `r${T}${id}${T}${b.is_error === true ? "1" : "0"}`; + } + case "thinking": + case "redacted_thinking": + return "o:r"; + case "image": + case "document": + return "o:m"; + default: + // server_tool_use / tool_reference / anything newer: no proven + // host counterpart — degrade this turn rather than misalign. + return null; + } } -/** Wire text sequence of one wire message: string content → [text]; block - * array → text blocks' text in order (cache_control et al. ignored). Any - * non-text block (tool_use, tool_result, image, thinking…) → null. */ -function wireTexts(content: unknown): string[] | null { - if (typeof content === "string") return [content]; +/** Token sequence of one ANTHROPIC-WIRE message, or null on any unmatchable + * block. */ +function wireContentToks(content: unknown): string[] | null { + if (typeof content === "string") return [`t${T}${content}`]; if (!Array.isArray(content)) return null; - const texts: string[] = []; + const toks: string[] = []; for (const block of content) { - const b = block !== null && typeof block === "object" ? (block as { type?: unknown; text?: unknown }) : undefined; - if (b === undefined || b.type !== "text" || typeof b.text !== "string") return null; - texts.push(b.text); + const tok = wireBlockToken(block); + if (tok === null) return null; + toks.push(tok); } - return texts; + return toks; } /** Client ids for the FULL wire message list of one planned turn, or * undefined when alignment cannot be proven (no harvest for the session, - * count mismatch, underivable/divergent entries, duplicate ids). The + * count mismatch, unalignable/divergent entries, duplicate ids). The * planner slices the result in lockstep with the delta messages. */ export function alignedMsgIdsForWire(sid: string, wire: unknown): string[] | undefined { const stored = harvests.get(sid); @@ -100,12 +213,12 @@ export function alignedMsgIdsForWire(sid: string, wire: unknown): string[] | und for (let i = 0; i < wire.length; i++) { const entry = stored.entries[i]; if (entry.id === "" || entry.shape === null) return undefined; - const msg = wire[i] as { role?: unknown; content?: unknown } | null | undefined; - if (msg === null || typeof msg !== "object" || msg.role !== entry.shape.role) return undefined; - const texts = wireTexts(msg.content); - if (texts === null || texts.length !== entry.shape.texts.length) return undefined; - for (let j = 0; j < texts.length; j++) { - if (texts[j] !== entry.shape.texts[j]) return undefined; + const msg = obj(wire[i]); + if (msg === undefined || msg.role !== entry.shape.role) return undefined; + const toks = wireContentToks(msg.content); + if (toks === null || toks.length !== entry.shape.toks.length) return undefined; + for (let j = 0; j < toks.length; j++) { + if (toks[j] !== entry.shape.toks[j]) return undefined; } if (seen.has(entry.id)) return undefined; seen.add(entry.id); diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index 02e4bcb910..6aae0006a3 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -711,9 +711,9 @@ function lastSessionFromMessages(messages: unknown): string | undefined { } /** #4491 host wiring: harvest host message ids per LLM request from the V1 - * messages.transform hook — the only opencode seam that sees them (V2's - * http.request sees just the wire, so V2-shaped turns degrade to - * content-derived identities). Never mutates the host's messages. */ + * messages.transform hook (V2 hosts instead feed the same store from the + * "context" seam registered in createOpencodeV2Setup). Never mutates the + * host's messages. */ const harvestMsgIdsTransform = async (input: unknown, output: { messages?: unknown }): Promise => { harvestOpencodeMsgIds(lastSessionFromMessages(output?.messages), output?.messages); // #5403/#5418 unified watermark channel: feed the ONE chokepoint emitter diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index 2d109ae15f..e8e4f5a1ec 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -53,6 +53,7 @@ import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI } from "../compress-tool.js"; import { stampedMsgIdsHeaderFromMessages } from "./opencode-v2-msgids.js"; import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fitNoticeDescription, forwardTool, postIdentityRegister, proxyBaseFromEnv, proxyBaseFromUrl, reportCompactionBoundary, reportRuntimeInfoOnChange, V2_SYNTHETIC_TEXT } from "./shared.js"; import { createForkAdopter } from "./fork-adopt.js"; +import { harvestOpencodeMsgIds } from "./opencode-msgids.js"; import { logHostEvent } from "./host-log.js"; // OpenCode V2 TUI renders a synthetic message as a visible Notice row only when its display text fits the @@ -427,7 +428,6 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct // above, same event, so there is deliberately NO separate `context` // hook here (that seam fired pre-request in older builds and could // race the wire; reading this event can't). - if (options.onResponse !== undefined) { const onResponse = options.onResponse; try { diff --git a/tests/opencode-msgids.test.ts b/tests/opencode-msgids.test.ts index ae4b8d455e..177e4d507a 100644 --- a/tests/opencode-msgids.test.ts +++ b/tests/opencode-msgids.test.ts @@ -19,7 +19,7 @@ process.env.NODE_TEST_CONTEXT = "1"; const { maybeIncrementalOpencode, opencodeIncrementalOnResponse, __resetOpencodeIncrementalForTests, createV1ServerHooks } = await import("../src/agent/opencode-native.ts"); const { __resetIncrementalClientForTests } = await import("../src/agent/incremental-client.ts"); -const { __resetOpencodeMsgIdsForTests } = await import("../src/agent/opencode-msgids.ts"); +const { __resetOpencodeMsgIdsForTests, harvestOpencodeMsgIds, deriveOpencodeEntry } = await import("../src/agent/opencode-msgids.ts"); import type { V2HttpRequestEvent, V2State } from "../src/agent/opencode-v2.ts"; const MODEL_URL = "https://api.anthropic.com/v1/messages"; @@ -239,3 +239,200 @@ test("opencode msgids: a single-flight-blocked turn does not burn its ids (#4491 const body2 = await plannedBody(t2.e); assert.deepEqual(body2.msgIds, ["msg_a1", "msg_u2"], "blocked attempt did not burn the delta ids"); }); + +// ── #5743 redo: REAL host ids from both seams (V2 "context" event shapes, +// tool sessions, the #5741 repeated-prompt scenario). The host message +// shapes below mirror live 2.0.4 captures (anthropic provider): role +// user/assistant/tool, content parts text / tool-call / tool-result. + +function ctxMessage(id: string, role: string, content: unknown[]): { id: string; role: string; content: unknown[] } { + return { id, role, content }; +} + +/** Simulate one `ctx.session.hook("context")` fire (V2 seam). */ +function fireContext(sid: string, messages: unknown[]): void { + harvestOpencodeMsgIds(sid, messages); +} + +test("opencode msgids: V2 context seam carries real ids through a TOOL session", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + const sid = "ses_v2ctx_1"; + + // Turn 1: user asks, assistant calls read(), the result comes back as a + // host role-"tool" message carrying an ERROR result. + fireContext(sid, [ + ctxMessage("msg_vu1", "user", [{ type: "text", text: "read README.md" }]), + ]); + const t1 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "be terse", messages: [ + { role: "user", content: [{ type: "text", text: "read README.md" }] }, + { role: "assistant", content: [ + { type: "text", text: "Reading." }, + { type: "tool_use", id: "toolu_1", name: "read", input: { path: "README.md" }, cache_control: { type: "ephemeral" } }, + ] }, + { role: "user", content: [ + { type: "tool_result", tool_use_id: "toolu_1", content: "{\"error\":{}}", is_error: true, cache_control: { type: "ephemeral" } }, + ] }, + ] }, sid); + // NOTE: fired BEFORE the request assembled — order inside the handler: + // context first, http.request after; the seed below replans against this + // latest fire exactly like the live hook does. + fireContext(sid, [ + ctxMessage("msg_vu1", "user", [{ type: "text", text: "read README.md" }]), + ctxMessage("msg_va1", "assistant", [ + { type: "text", text: "Reading." }, + { type: "tool-call", id: "toolu_1", name: "read", input: { path: "README.md" }, providerExecuted: false }, + ]), + ctxMessage("msg_vt1", "tool", [ + { type: "tool-result", id: "toolu_1", name: "read", result: { type: "error", value: { error: {} } }, providerExecuted: false }, + ]), + ]); + await maybeIncrementalOpencode(t1.e, s); + const body1 = await plannedBody(t1.e); + assert.deepEqual(body1.msgIds, ["msg_vu1", "msg_va1", "msg_vt1"], + "seed rides REAL host ids — text, tool-call (assistant), tool-result (host role \"tool\" normalized to wire \"user\"); error flag matched via is_error"); + + opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(t1.e.request as Request)); + + // Turn 2: assistant settles on text; the delta must carry exactly its + // own fresh id, sliced in lockstep with the suffix messages. + fireContext(sid, [ + ctxMessage("msg_vu1", "user", [{ type: "text", text: "read README.md" }]), + ctxMessage("msg_va1", "assistant", [ + { type: "text", text: "Reading." }, + { type: "tool-call", id: "toolu_1", name: "read", input: { path: "README.md" }, providerExecuted: false }, + ]), + ctxMessage("msg_vt1", "tool", [ + { type: "tool-result", id: "toolu_1", name: "read", result: { type: "error", value: { error: {} } }, providerExecuted: false }, + ]), + ctxMessage("msg_va2", "assistant", [{ type: "text", text: "It failed: missing file." }]), + ctxMessage("msg_vu2", "user", [{ type: "text", text: "ok thanks" }]), + ]); + const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "be terse", messages: [ + { role: "user", content: [{ type: "text", text: "read README.md" }] }, + { role: "assistant", content: [ + { type: "text", text: "Reading." }, + { type: "tool_use", id: "toolu_1", name: "read", input: { path: "README.md" }, cache_control: { type: "ephemeral" } }, + ] }, + { role: "user", content: [ + { type: "tool_result", tool_use_id: "toolu_1", content: "{\"error\":{}}", is_error: true, cache_control: { type: "ephemeral" } }, + ] }, + { role: "assistant", content: [{ type: "text", text: "It failed: missing file." }] }, + { role: "user", content: [{ type: "text", text: "ok thanks" }] }, + ] }, sid); + await maybeIncrementalOpencode(t2.e, s); + const body2 = await plannedBody(t2.e); + assert.equal((body2.messages as unknown[]).length, 2, "delta rides only the suffix"); + assert.deepEqual(body2.msgIds, ["msg_va2", "msg_vu2"], "delta rides ONLY the fresh host ids"); +}); + +test("opencode msgids: #5741 repeated-prompt turns get DISTINCT real ids", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + const sid = "ses_repeat_1"; + + // Turn 1: prompt "go" → ids r1/a1. + fireContext(sid, [ctxMessage("msg_r1", "user", [{ type: "text", text: "go" }])]); + const w1 = { model: "claude-test", max_tokens: 512, system: "s", messages: [{ role: "user", content: [{ type: "text", text: "go" }] }] }; + const t1 = fakeEvent(w1, sid); + await maybeIncrementalOpencode(t1.e, s); + assert.deepEqual((await plannedBody(t1.e)).msgIds, ["msg_r1"]); + opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(t1.e.request as Request)); + + // Turn 2: the BYTE-IDENTICAL prompt resends — opencode mints a FRESH id + // for it. With derived content hashes both occurrences would collide; + // the client must vouch the SECOND id so the lane pins a second row. + fireContext(sid, [ + ctxMessage("msg_r1", "user", [{ type: "text", text: "go" }]), + ctxMessage("msg_a1", "assistant", [{ type: "text", text: "done" }]), + ctxMessage("msg_r2", "user", [{ type: "text", text: "go" }]), + ]); + const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "s", messages: [ + { role: "user", content: [{ type: "text", text: "go" }] }, + { role: "assistant", content: [{ type: "text", text: "done" }] }, + { role: "user", content: [{ type: "text", text: "go" }] }, + ] }, sid); + await maybeIncrementalOpencode(t2.e, s); + const body2 = await plannedBody(t2.e); + assert.equal((body2.messages as unknown[]).length, 2, + "delta covers the acked prompt's reply plus the repeated prompt itself"); + assert.deepEqual(body2.msgIds, ["msg_a1", "msg_r2"], + "the repeated prompt vouches its OWN fresh id (msg_r2) — never collides with the pinned msg_r1 row"); +}); + +test("opencode msgids: reasoning↔thinking aligns; noise parts skip; unknown parts degrade", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + + // reasoning part ↔ thinking block; v1 bookkeeping parts carry no wire block + const sid = "ses_think_1"; + fireContext(sid, [ + ctxMessage("msg_h1", "assistant", [ + { type: "reasoning", text: "hmm…" }, + { type: "text", text: "answer" }, + ]), + ]); + let t = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [ + { role: "assistant", content: [ + { type: "thinking", thinking: "hmm…", signature: "sig" }, + { type: "text", text: "answer" }, + ] }, + ] }, sid); + await maybeIncrementalOpencode(t.e, s); + assert.deepEqual((await plannedBody(t.e)).msgIds, ["msg_h1"], "reasoning part proves onto the thinking block positionally"); + + // an UNRECOGNIZED part type must drop the field (never guess) + const sid2 = "ses_unknown_1"; + fireContext(sid2, [ + ctxMessage("msg_k1", "user", [{ type: "source-url", url: "https://example.com" }]), + ]); + const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [ + { role: "user", content: [{ type: "text", text: "whatever" }] }, + ] }, sid2); + await maybeIncrementalOpencode(t2.e, s); + assert.equal("msgIds" in (await plannedBody(t2.e)), false, "unknown part types degrade to content-derived identities"); + + // duplicate ids drop the whole field even when every position otherwise aligns + const sid3 = "ses_dup_1"; + fireContext(sid3, [ + ctxMessage("msg_d1", "user", [{ type: "text", text: "a" }]), + ctxMessage("msg_d1", "assistant", [{ type: "text", text: "b" }]), + ]); + const t3 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [ + { role: "user", content: [{ type: "text", text: "a" }] }, + { role: "assistant", content: [{ type: "text", text: "b" }] }, + ] }, sid3); + await maybeIncrementalOpencode(t3.e, s); + assert.equal("msgIds" in (await plannedBody(t3.e)), false, "duplicate ids can never ride"); +}); + +test("opencode msgids: deriveOpencodeEntry unit-level normalization", () => { + const e1 = deriveOpencodeEntry(ctxMessage("msg_x1", "tool", [ + { type: "tool-result", id: "toolu_9", name: "bash", result: { type: "error", value: "boom" }, providerExecuted: false }, + ])); + assert.equal(e1.shape?.role, "user", "host role \"tool\" normalizes to wire \"user\""); + assert.deepEqual(e1.shape?.toks, [`r\u0000toolu_9\u00001`], "tool-result anchored by id + error flag"); + + const e2 = deriveOpencodeEntry({ info: { id: "msg_y1", role: "assistant", sessionID: "s" }, parts: [ + { type: "step-start" }, + { type: "text", text: "hi" }, + { type: "tool", tool: "bash", callID: "call_7", state: { status: "running" } }, + { type: "snapshot", snapshot: "abc" }, + ] }); + assert.deepEqual(e2.shape?.toks, [`t\u0000hi`, `c\u0000call_7\u0000bash`], "v1 bookkeeping parts (step-start/snapshot) skip; tool part anchors by callID+name"); + + assert.equal(deriveOpencodeEntry({ info: { role: "system" }, parts: [] }).shape, null, "non-conversation roles poison the entry only"); + assert.equal(deriveOpencodeEntry({ id: "not-a-host-id", role: "user", content: [] }).id, "", "foreign-shaped ids stay unusable"); +}); diff --git a/tests/opencode-rolling-cache-marks.test.ts b/tests/opencode-rolling-cache-marks.test.ts index af920378aa..13d982b25b 100644 --- a/tests/opencode-rolling-cache-marks.test.ts +++ b/tests/opencode-rolling-cache-marks.test.ts @@ -243,7 +243,7 @@ test("rolling marks: repeated prompts retain distinct IDs and match full resend" const { modes } = await runIncremental(inc, requests); assert.deepEqual(modes, ["seed", ...requests.slice(1).map(() => "delta")]); for (let i = 0; i < requests.length; i++) { - assert.equal(JSON.stringify(withoutTagTime(inc.forwarded[i].messages)), JSON.stringify(withoutTagTime(base.forwarded[i].messages))); + assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages)); assert.equal(messageBreakpoints(inc.forwarded[i]), messageBreakpoints({ messages: requests[i] })); } } finally { await inc.close(); } From cb28a5fa19e9aff3e9a34baec4430315179e4ef2 Mon Sep 17 00:00:00 2001 From: mariopasquali <46114603+mariopasquali@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:29:45 +0100 Subject: [PATCH 39/78] fix: preserve position fingerprints when reaping folds (cherry picked from commit 55c853ae4d5985b312fe39e38875bd991602f224) --- src/fold-reconcile.ts | 15 +++++++--- tests/fold-position-reap-integrity.test.ts | 32 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 tests/fold-position-reap-integrity.test.ts diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index 7082c02f19..880be7e944 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -2314,14 +2314,21 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt for (const id of block.directMessageIds ?? []) deadIds.add(id); } for (const id of deadIds) delete nextAnchors[id]; + const storedPositions = session.metadata[METADATA_POSITIONS] as { ids?: string[]; canon?: string[] } | undefined; + const positionIds = storedPositions?.ids; + const positionCanon = storedPositions?.canon; for (let i = nextOrder.length - 1; i >= 0; i--) { - if (deadIds.has(nextOrder[i])) nextOrder.splice(i, 1); + if (deadIds.has(nextOrder[i])) { + nextOrder.splice(i, 1); + if (positionIds === nextOrder && Array.isArray(positionCanon)) positionCanon.splice(i, 1); + } } - const storedPositions = session.metadata[METADATA_POSITIONS] as { ids?: string[] } | undefined; - const positionIds = storedPositions?.ids; if (Array.isArray(positionIds) && positionIds !== nextOrder) { for (let i = positionIds.length - 1; i >= 0; i--) { - if (deadIds.has(positionIds[i])) positionIds.splice(i, 1); + if (deadIds.has(positionIds[i])) { + positionIds.splice(i, 1); + if (Array.isArray(positionCanon)) positionCanon.splice(i, 1); + } } } recordConflict(session, "orphan-reap", `reaped ${reapedCount} zombie fold block(s) [${reaped.join(", ")}]: covered substrate majority-absent for ${FOLD_DRIFT_ESCALATE_PASSES} consecutive passes (#2695)`); diff --git a/tests/fold-position-reap-integrity.test.ts b/tests/fold-position-reap-integrity.test.ts new file mode 100644 index 0000000000..9b3bbad079 --- /dev/null +++ b/tests/fold-position-reap-integrity.test.ts @@ -0,0 +1,32 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { reconcileFoldCoverage, positionalFingerprint } from '../src/fold-reconcile.js'; +import { SessionStore, _setStoreForTest } from '../src/persist.js'; +import type { Session } from '../src/session.js'; +import type { CoreMessage } from 'acp-kernel'; + +test('reaping a partially surviving fold preserves position fingerprint associations', () => { + _setStoreForTest(new SessionStore({ enabled: false })); + const message = (id: string): CoreMessage => ({ id, role: 'user', contentType: 'text', text: `Unique invented payload ${id}` }); + const covered = ['a','b','c'].map(message); + const tail = Array.from({ length: 12 }, (_, index) => message(`tail-${index}`)); + const session = { + id: 'synthetic-reap-position-integrity', + state: { blocks: [{ active: true, blockId: 'synthetic-fold', effectiveMessageIds: ['a','b','c'] }], messageRefs: { byRaw: {}, byRef: {} } }, + metadata: {}, + } as unknown as Session; + reconcileFoldCoverage(session, [...covered,...tail], { mode: 'repair' }); + const before = session.metadata.foldPositions as { ids: string[]; canon: string[] }; + assert.deepEqual(before.canon, [...covered,...tail].map(positionalFingerprint)); + const surviving = [covered[0],...tail]; + for (let pass = 0; pass < 3; pass++) reconcileFoldCoverage(session, surviving, { mode: 'repair' }); + assert.equal(session.state.blocks.length, 0, 'fixture must actually reap the majority-missing fold'); + const after = session.metadata.foldPositions as { ids: string[]; canon: string[] }; + assert.equal(after.ids.length, after.canon.length, 'reaping must remove each position ID together with its fingerprint'); + const actual = new Map(surviving.map(item => [item.id,positionalFingerprint(item)])); + after.ids.forEach((id,index) => assert.equal(after.canon[index], actual.get(id), 'each surviving fingerprint must belong to its recorded ID')); + reconcileFoldCoverage(session, surviving, { mode: 'repair' }); + const next = session.metadata.foldPositions as { ids: string[]; canon: string[] }; + assert.deepEqual(next.canon, next.ids.map(id => actual.get(id)), + 'the next pass must not cache a shifted fingerprint under the wrong ID'); +}); From 1012e6153e20499d5f360386659359f8d0a49316 Mon Sep 17 00:00:00 2001 From: Andrew Trokhymenko Date: Mon, 9 Nov 2026 23:06:41 -0500 Subject: [PATCH 40/78] fix(google): promote stripped proxy call thoughtSignature onto surviving sibling functionCall (#6079, #5252) (cherry picked from commit 39145bd9b58d2539fd342a56568e05b067e2ba7d) --- src/loop/adapter-google.ts | 67 ++++-- src/loop/core.ts | 7 +- src/stream-google.ts | 24 +- tests/adapter-google.test.ts | 422 ++++++++++++++++++++++++++++++++++- 4 files changed, 503 insertions(+), 17 deletions(-) diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts index 21b77f82b9..bb45909cf9 100644 --- a/src/loop/adapter-google.ts +++ b/src/loop/adapter-google.ts @@ -20,6 +20,7 @@ const PROXY_TOOL_SET = new Set([ // here or their chunks raw-replay to the client as "real" calls. acp_cache // had the same gap before this change. "acp_retire", "acp_restore", "acp_rewrite", "acp_cache", + "acp_retrieve", "acp_rule", "acp_read_original_image", "bili_compress", "bili_decompress", "bili_search_context", "bili_status", ]); @@ -101,35 +102,53 @@ interface FunctionCallBuffer { } // A decision per buffered chunk that carried functionCall parts: "keep" = every -// call in it is real (replay verbatim, preserving signatures/ids/order), null = -// only proxy calls (drop — the proxy executes them server-side), or a rewritten -// copy keeping the chunk's other parts plus its real calls. +// call in it is real and needs no signature promotion (replay verbatim, +// preserving signatures/ids/order), null = only proxy calls (drop — the proxy +// executes them server-side), or a rewritten copy keeping the chunk's other +// parts plus its real calls (promoting a stripped proxy call's thoughtSignature +// onto the first surviving real functionCall part when Gemini placed the turn +// signature on the leading proxy call). function filterRealCallChunk( parsed: Record, realIndexes: Set, callIndexes: number[], + promoteSig?: { callIndex: number; signature: string }, ): "keep" | null | Record { const candidates = parsed.candidates as Array> | undefined; const parts = (candidates?.[0]?.content as { parts?: GooglePart[] } | undefined)?.parts; if (!Array.isArray(parts)) return "keep"; let anyReal = false; let anyProxy = false; - const keep: boolean[] = []; + let sigPromoted = false; + const outParts: GooglePart[] = []; let call = 0; for (const part of parts) { if (part && typeof part === "object" && part.functionCall) { - const real = realIndexes.has(callIndexes[call] ?? -1); + const callIdx = callIndexes[call] ?? -1; + const real = realIndexes.has(callIdx); call += 1; - keep.push(real); - if (real) anyReal = true; - else anyProxy = true; + if (real) { + anyReal = true; + if ( + promoteSig && + callIdx === promoteSig.callIndex && + (typeof part.thoughtSignature !== "string" || part.thoughtSignature.length === 0) + ) { + outParts.push({ ...part, thoughtSignature: promoteSig.signature }); + sigPromoted = true; + } else { + outParts.push(part); + } + } else { + anyProxy = true; + } } else { - keep.push(true); + outParts.push(part); } } - if (!anyProxy) return "keep"; + if (!anyProxy && !sigPromoted) return "keep"; if (!anyReal) return null; - return cloneChunk(parsed, { parts: parts.filter((_, i) => keep[i]) }); + return cloneChunk(parsed, { parts: outParts }); } function dataLine(frame: string): string | null { @@ -440,21 +459,43 @@ export function createGoogleAdapter( } return; } + // Gemini attaches thoughtSignature only to the FIRST functionCall + // part in a parallel-call turn. When a stripped proxy call (e.g. + // compress) led the turn and carried that signature, the first + // surviving real functionCall part becomes the turn's leading call + // on client replay and must inherit the signature or Gemini 3/4 + // rejects the next turn with 400 INVALID_ARGUMENT (missing + // thought_signature). + let firstRealIdx: number | undefined; + let firstProxySig = ""; + for (const [idx, tc] of pending) { + if (realIndexes.has(idx)) { + if (firstRealIdx === undefined && tc.name.length > 0) firstRealIdx = idx; + } else if (firstProxySig.length === 0 && tc.signature.length > 0) { + firstProxySig = tc.signature; + } + } + const firstRealTc = firstRealIdx !== undefined ? pending.get(firstRealIdx) : undefined; + const promoteSig = + firstRealIdx !== undefined && firstRealTc && firstRealTc.signature.length === 0 && firstProxySig.length > 0 + ? { callIndex: firstRealIdx, signature: firstProxySig } + : undefined; for (const [idx, tc] of pending) { if (realIndexes.has(idx) && tc.name.length > 0) { toolCallsEmitted++; + const sig = tc.signature || (promoteSig?.callIndex === idx ? promoteSig.signature : ""); yield { kind: "tool_call", name: tc.name, callId: tc.id, arguments: tc.args, passthrough: true, - ...(tc.signature ? { signature: tc.signature } : {}), + ...(sig ? { signature: sig } : {}), } as ParsedStreamEvent; } } for (const { json, parsed, callIndexes } of rawCallChunks) { - const filtered = filterRealCallChunk(parsed, realIndexes, callIndexes); + const filtered = filterRealCallChunk(parsed, realIndexes, callIndexes, promoteSig); if (filtered === "keep") { yield { kind: "meta", chunk: Buffer.from(`data: ${json}\n\n`, "utf8") } as ParsedStreamEvent; } else if (filtered !== null) { diff --git a/src/loop/core.ts b/src/loop/core.ts index 03a7bf83bb..b93ae72f17 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -1551,8 +1551,13 @@ export async function* runCompressLoop( coreMessages.push(redactedMsg); } } + const roundProxySignature = + ctx.protocol === "google" + ? proxyResults.find((p) => typeof p.signature === "string" && p.signature.length > 0)?.signature + : undefined; for (const pr of proxyResults) { if (functionCallIds.has(pr.callId)) { + const sig = pr.signature || roundProxySignature; coreMessages.push({ id: `acp_loop_r${round}_asst_tc_${pr.callId}`, role: "assistant", @@ -1561,7 +1566,7 @@ export async function* runCompressLoop( toolCallId: pr.callId, // #3661: guaranteed-valid-JSON replay (see wireArguments); raw model bytes stay in pr.arguments for #156 signatures. text: pr.wireArguments, - ...(ctx.protocol === "google" && pr.signature ? { googleThoughtSignature: pr.signature } : {}), + ...(ctx.protocol === "google" && sig ? { googleThoughtSignature: sig } : {}), }); const toolResultMsg: CoreMessage = { id: `acp_loop_r${round}_tool_${pr.callId}`, diff --git a/src/stream-google.ts b/src/stream-google.ts index 54dc5dc18a..7835f9207b 100644 --- a/src/stream-google.ts +++ b/src/stream-google.ts @@ -22,6 +22,7 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator 0) { + promotedSignature = part.thoughtSignature; + } const args = fc.args !== null && typeof fc.args === "object" ? (fc.args as Record) : {}; noteParts.push(yield { name: fc.name, args, id: typeof fc.id === "string" ? fc.id : undefined }); continue; @@ -53,10 +57,28 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator 0) { + const firstFcIdx = keptParts.findIndex((p) => p && typeof p === "object" && p.functionCall !== undefined); + if (firstFcIdx >= 0) { + const firstFc = keptParts[firstFcIdx]!; + if (typeof firstFc.thoughtSignature !== "string" || firstFc.thoughtSignature.length === 0) { + keptParts[firstFcIdx] = { ...firstFc, thoughtSignature: promotedSignature }; + } + } + } // The tool result is a NEW text part: an existing text part may carry a // thoughtSignature the client echoes back, and appending to its text would // leave the signature describing a string that no longer exists. - content.parts = [...keptParts, { text: noteParts.join("\n") }]; + const notePart: GooglePart = + !sawReal && + promotedSignature.length > 0 && + !keptParts.some((p) => p && typeof p === "object" && typeof p.thoughtSignature === "string" && p.thoughtSignature.length > 0) + ? { text: noteParts.join("\n"), thoughtSignature: promotedSignature } + : { text: noteParts.join("\n") }; + content.parts = [...keptParts, notePart]; if (!sawReal) { candidate.finishReason = "STOP"; } diff --git a/tests/adapter-google.test.ts b/tests/adapter-google.test.ts index ac3c7f6d3c..65a876bf85 100644 --- a/tests/adapter-google.test.ts +++ b/tests/adapter-google.test.ts @@ -1,8 +1,11 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import type { CoreMessage } from "acp-kernel"; -import { createGoogleAdapter } from "../src/loop/index.ts"; +import { createCore, defaultConfig, type CoreMessage } from "acp-kernel"; +import { coreToGoogle, googleToCore, type GooglePart } from "acp-kernel/wire"; +import { createGoogleAdapter, runCompressLoop } from "../src/loop/index.ts"; import type { CompressLoopAdapter, ParsedStreamEvent } from "../src/loop/index.ts"; +import { createSession } from "../src/session.ts"; +import { rewriteGoogleJsonResponseAsync } from "../src/stream-google.ts"; function sse(obj: unknown): string { return `data: ${JSON.stringify(obj)}\n\n`; @@ -349,3 +352,418 @@ test("google parseStream: a null sibling part does not break the edited frame", const frames = framesOf(events); assert.ok(frames.some((f) => f.includes("kept prose")), `the prose still reaches the client, got: ${frames.join(" | ")}`); }); + +test("google parseStream: parallel [compress (signed), write_file (unsigned)] in same chunk promotes thoughtSignature onto write_file", async () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-4-argon-medium"); + const events = await collect( + adapter, + sse({ + candidates: [{ + content: { + role: "model", + parts: [ + { + functionCall: { + name: "compress", + args: { content: "m00046-m00129: folded context\nsummary body" }, + id: "call_compress_1", + }, + thoughtSignature: "sig-parallel-same-chunk", + }, + { + functionCall: { + name: "write_file", + args: { path: "src/app.ts", content: "export const ok = true;\n" }, + id: "call_write_2", + }, + }, + { + functionCall: { + name: "read_file", + args: { path: "README.md" }, + id: "call_read_3", + }, + }, + ], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }), + ); + + const toolCalls = events.filter((e): e is Extract => e.kind === "tool_call"); + const writeEvent = toolCalls.find((e) => e.name === "write_file"); + const readEvent = toolCalls.find((e) => e.name === "read_file"); + const compressEvent = toolCalls.find((e) => e.name === "compress"); + assert.ok(writeEvent && writeEvent.passthrough === true, "write_file emitted as passthrough real call"); + assert.equal(writeEvent.signature, "sig-parallel-same-chunk", "first surviving real call event inherits thoughtSignature"); + assert.ok(readEvent && readEvent.passthrough === true, "read_file emitted as passthrough real call"); + assert.equal(readEvent.signature, undefined, "subsequent sibling real call stays unsigned"); + assert.ok(compressEvent && compressEvent.passthrough !== true, "compress emitted as server-side proxy call"); + + const callMeta = events.find((e) => e.kind === "meta" && e.chunk.toString("utf8").includes("write_file")); + assert.ok(callMeta && callMeta.kind === "meta", "replayed call frame present"); + const replayedParts = partsOf(frameJson(callMeta.chunk)); + assert.equal(replayedParts.length, 2, "compress stripped; two real calls kept"); + assert.deepEqual(replayedParts[0].functionCall, { + name: "write_file", + args: { path: "src/app.ts", content: "export const ok = true;\n" }, + id: "call_write_2", + }); + assert.equal(replayedParts[0].thoughtSignature, "sig-parallel-same-chunk", "first surviving real functionCall part carries promoted thoughtSignature"); + assert.equal(replayedParts[1].thoughtSignature, undefined, "second surviving real functionCall part stays unsigned"); +}); + +test("google parseStream: parallel [compress (signed), write_file (unsigned)] across separate chunks promotes thoughtSignature onto write_file chunk", async () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-4-argon-medium"); + const events = await collect( + adapter, + sse({ + candidates: [{ + content: { + role: "model", + parts: [{ + functionCall: { + name: "compress", + args: { content: "m00001-m00010: folded\nsummary" }, + id: "call_compress_chunk1", + }, + thoughtSignature: "sig-parallel-chunk1", + }], + }, + index: 0, + }], + }) + + sse({ + candidates: [{ + content: { + role: "model", + parts: [{ + functionCall: { + name: "write_file", + args: { path: "src/index.ts", content: "console.log(1);\n" }, + id: "call_write_chunk2", + }, + }], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }), + ); + + const allFrames = framesOf(events).join(""); + assert.equal(allFrames.includes("call_compress_chunk1"), false, "proxy-only chunk 1 is dropped from client stream"); + const callMeta = events.find((e) => e.kind === "meta" && e.chunk.toString("utf8").includes("write_file")); + assert.ok(callMeta && callMeta.kind === "meta", "chunk 2 replayed with write_file"); + const replayedParts = partsOf(frameJson(callMeta.chunk)); + assert.equal(replayedParts.length, 1); + assert.equal(replayedParts[0].thoughtSignature, "sig-parallel-chunk1", "chunk 2 real functionCall part inherits chunk 1 proxy call's thoughtSignature"); +}); + +test("google parseStream: real call with its own thoughtSignature is never overwritten by a sibling proxy call", async () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-4-argon-medium"); + const events = await collect( + adapter, + sse({ + candidates: [{ + content: { + role: "model", + parts: [ + { + functionCall: { name: "write_file", args: { path: "a.ts" }, id: "call_1" }, + thoughtSignature: "sig-own-real", + }, + { + functionCall: { name: "compress", args: {}, id: "call_2" }, + thoughtSignature: "sig-proxy-other", + }, + ], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }), + ); + + const callMeta = events.find((e) => e.kind === "meta" && e.chunk.toString("utf8").includes("write_file")); + assert.ok(callMeta && callMeta.kind === "meta"); + const replayedParts = partsOf(frameJson(callMeta.chunk)); + assert.equal(replayedParts.length, 1); + assert.equal(replayedParts[0].thoughtSignature, "sig-own-real", "real call's own signature is preserved"); +}); + +test("rewriteGoogleJsonResponseAsync: promotes stripped proxy call thoughtSignature onto surviving real functionCall part", async () => { + const core = createCore(); + const config = defaultConfig(400_000); + const session = createSession("google-json-sig"); + const messages: CoreMessage[] = [ + { id: "m1", role: "user", contentType: "text", text: "step 1" }, + { id: "m2", role: "assistant", contentType: "text", text: "ack 1" }, + { id: "m3", role: "user", contentType: "text", text: "step 2" }, + { id: "m4", role: "assistant", contentType: "text", text: "ack 2" }, + { id: "m5", role: "user", contentType: "text", text: "step 3" }, + { id: "m6", role: "assistant", contentType: "text", text: "ack 3" }, + { id: "m7", role: "user", contentType: "text", text: "step 4" }, + ]; + const turn = core.processTurn({ messages, state: session.state, config, tokenCount: 0 }); + session.state = turn.state; + + const responseBody = { + candidates: [{ + content: { + role: "model", + parts: [ + { + functionCall: { + name: "compress", + args: { content: "m00001-m00003: early steps\ncompleted step 1 and step 2" }, + id: "fc_compress", + }, + thoughtSignature: "sig-json-parallel", + }, + { + functionCall: { + name: "write_file", + args: { path: "out.txt", content: "done" }, + id: "fc_write", + }, + }, + ], + }, + finishReason: "STOP", + }], + }; + + const rewritten = (await rewriteGoogleJsonResponseAsync(responseBody, { + core, + config, + messages, + session, + log: () => {}, + })) as { candidates: Array<{ content: { parts: GooglePart[] } }> }; + + const outParts = rewritten.candidates[0].content.parts; + const fcParts = outParts.filter((p) => p.functionCall !== undefined); + assert.equal(fcParts.length, 1, "compress stripped, write_file kept"); + assert.equal(fcParts[0].functionCall?.name, "write_file"); + assert.equal(fcParts[0].thoughtSignature, "sig-json-parallel", "write_file inherited thoughtSignature from stripped compress call"); +}); + +test("runCompressLoop + next-turn Google replay: mixed [compress, write_file] turn keeps thoughtSignature on write_file after fold", async () => { + const core = createCore(); + const config = defaultConfig(400_000); + const session = createSession("google-loop-mixed-sig"); + + const initialContents = [ + { role: "user", parts: [{ text: "task 1" }] }, + { role: "model", parts: [{ text: "done 1" }] }, + { role: "user", parts: [{ text: "task 2" }] }, + { role: "model", parts: [{ text: "done 2" }] }, + { role: "user", parts: [{ text: "task 3" }] }, + { role: "model", parts: [{ text: "done 3" }] }, + { role: "user", parts: [{ text: "now compress and write file" }] }, + ]; + const flat1 = googleToCore({ contents: initialContents }); + const turn1 = core.processTurn({ messages: flat1.msgs, state: session.state, config, tokenCount: 0 }); + session.state = turn1.state; + + const upstreamSse = + sse({ + candidates: [{ + content: { + role: "model", + parts: [ + { thought: true, text: "I will compress m00001-m00004 and call write_file." }, + { + functionCall: { + name: "compress", + args: { content: "m00001-m00004: tasks 1-2\ncompleted tasks 1 and 2" }, + id: "call_compress_mixed", + }, + thoughtSignature: "sig-gemini4-active-turn", + }, + { + functionCall: { + name: "write_file", + args: { path: "result.txt", content: "hello" }, + id: "call_write_mixed", + }, + }, + ], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }); + + const reqBody: Record = { contents: initialContents }; + const adapter = createGoogleAdapter(reqBody, undefined, undefined, "gemini-4-argon-medium"); + const streamedChunks: string[] = []; + for await (const chunk of runCompressLoop( + new Response(upstreamSse, { status: 200 }).body!, + { + core, + config, + messages: turn1.messages, + compressMessages: flat1.msgs, + session, + protocol: "google", + log: () => {}, + }, + reqBody, + { url: "http://127.0.0.1:1/v1beta/models/gemini-4-argon-medium:streamGenerateContent?alt=sse", headers: {} }, + adapter, + "system prompt", + )) { + streamedChunks.push(chunk.toString("utf8")); + } + + // Reconstruct the model turn parts exactly as a Google SSE client accumulates them. + const clientReceivedParts: GooglePart[] = []; + for (const raw of streamedChunks.join("").split("\n\n")) { + const line = raw.split("\n").find((l) => l.startsWith("data:")); + if (!line) continue; + const parsed = JSON.parse(line.slice(5).trim()) as { + candidates?: Array<{ content?: { parts?: GooglePart[] } }>; + }; + const ps = parsed.candidates?.[0]?.content?.parts ?? []; + clientReceivedParts.push(...ps); + } + + const writePartOnWire = clientReceivedParts.find((p) => p.functionCall?.name === "write_file"); + assert.ok(writePartOnWire, "client received write_file functionCall part"); + assert.equal(writePartOnWire.thoughtSignature, "sig-gemini4-active-turn", "streamed write_file part carries thoughtSignature"); + + // Next turn: client appends the model turn + functionResponse for write_file, and bili prepares the folded outbound body. + const nextTurnContents = [ + ...initialContents, + { role: "model", parts: clientReceivedParts }, + { + role: "user", + parts: [{ + functionResponse: { + name: "write_file", + id: "call_write_mixed", + response: { output: "ok" }, + }, + }], + }, + ]; + const flat2 = googleToCore({ contents: nextTurnContents }); + const turn2 = core.processTurn({ messages: flat2.msgs, state: session.state, config, tokenCount: 0 }); + const outboundContents = coreToGoogle(turn2.messages); + const modelTurnWithWrite = outboundContents.find( + (c) => c.role === "model" && c.parts.some((p) => p.functionCall?.name === "write_file"), + ); + assert.ok(modelTurnWithWrite, "outbound Google contents contain the write_file model turn"); + const outboundWritePart = modelTurnWithWrite.parts.find((p) => p.functionCall?.name === "write_file"); + assert.ok(outboundWritePart, "write_file part present on outbound wire"); + assert.equal( + outboundWritePart.thoughtSignature, + "sig-gemini4-active-turn", + "surviving write_file part retains thoughtSignature after fold on next turn", + ); +}); + +test("runCompressLoop Google re-request: parallel proxy-only calls [compress (signed), acp_status (unsigned)] keep thoughtSignature on every rebuilt model functionCall turn", async () => { + const core = createCore(); + const config = defaultConfig(400_000); + const session = createSession("google-loop-multi-proxy-sig"); + + const initialContents = [ + { role: "user", parts: [{ text: "step 1" }] }, + { role: "model", parts: [{ text: "done 1" }] }, + { role: "user", parts: [{ text: "step 2" }] }, + { role: "model", parts: [{ text: "done 2" }] }, + { role: "user", parts: [{ text: "compress and check status" }] }, + ]; + const flat = googleToCore({ contents: initialContents }); + const turn = core.processTurn({ messages: flat.msgs, state: session.state, config, tokenCount: 0 }); + session.state = turn.state; + + const round1Sse = + sse({ + candidates: [{ + content: { + role: "model", + parts: [ + { + functionCall: { + name: "compress", + args: { content: "m00001-m00002: step 1\ncompleted step 1" }, + id: "call_proxy_compress", + }, + thoughtSignature: "sig-multi-proxy-round1", + }, + { + functionCall: { + name: "acp_status", + args: {}, + id: "call_proxy_status", + }, + }, + ], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }); + + const round2Sse = + sse({ candidates: [{ content: { role: "model", parts: [{ text: "All folded and checked." }] }, index: 0 }] }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }); + + let capturedRound2Body: { contents?: Array<{ role?: string; parts?: GooglePart[] }> } | undefined; + const origFetch = globalThis.fetch; + globalThis.fetch = (async (_input: unknown, init?: RequestInit) => { + capturedRound2Body = JSON.parse(String(init?.body ?? "{}")) as { + contents?: Array<{ role?: string; parts?: GooglePart[] }>; + }; + return new Response(round2Sse, { + status: 200, + headers: { "content-type": "text/event-stream" }, + }); + }) as typeof fetch; + + try { + const reqBody: Record = { contents: initialContents }; + const adapter = createGoogleAdapter(reqBody, undefined, undefined, "gemini-4-argon-medium"); + for await (const _ of runCompressLoop( + new Response(round1Sse, { status: 200 }).body!, + { + core, + config, + messages: turn.messages, + compressMessages: flat.msgs, + session, + protocol: "google", + log: () => {}, + }, + reqBody, + { url: "http://127.0.0.1:1/v1beta/models/gemini-4-argon-medium:streamGenerateContent?alt=sse", headers: {} }, + adapter, + "system prompt", + )) { + // drain + } + } finally { + globalThis.fetch = origFetch; + } + + assert.ok(capturedRound2Body?.contents, "round 2 re-request body captured"); + const modelFcTurns = capturedRound2Body.contents.filter( + (c) => c.role === "model" && (c.parts ?? []).some((p) => p.functionCall !== undefined), + ); + assert.ok(modelFcTurns.length >= 2, "both proxy tool calls are present in round 2 re-request"); + for (const modelTurn of modelFcTurns) { + const firstFc = (modelTurn.parts ?? []).find((p) => p.functionCall !== undefined); + assert.ok(firstFc, "model turn has functionCall part"); + assert.equal( + firstFc.thoughtSignature, + "sig-multi-proxy-round1", + `functionCall ${firstFc.functionCall?.name} carries round thoughtSignature`, + ); + } +}); From 4dba44172ad4db895fc30980541c94c4e4410fcc Mon Sep 17 00:00:00 2001 From: Gong Liu <1360187968@qq.com> Date: Tue, 10 Nov 2026 13:58:58 +0800 Subject: [PATCH 41/78] fix: recognize dsh 0.2.2's auto-review sidecar shape (#6104) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dsh 0.2.2-rc.1 broke BOTH bars isDshAutoReviewRequest was keyed on, and neither was identity: - REVIEW_POLICY now opens with a `REVIEW_POLICY` banner line, so the pinned sentence is no longer line 1 and the first-line startsWith missed it. - reviewMessages(snapshot) replays the whole conversation as role:"user" text blobs, so `users === 1` was false for any non-empty history. Every review call therefore ran the full pipeline under the main session id: reconcileFoldCoverage read the other serialization as a wholesale rewrite on both legs (1,006/1,007 alternating system fingerprints over 5 sessions), and the refs minted from it were tombstoned as DEAD — 107 `compress FAILED … cannot be anchored` receipts, 0 successful compressions. The pin stays version-pinned to the SENTENCE; only its position is read tolerantly (first few lines of the system carrier, never the whole prompt). The replay veto never needed the user count — the entry-level loop already vetoes any assistant/tool turn, which every real resend carries. (cherry picked from commit c357eb8651f7d3ea5e66074fce629a615bf4ccb9) --- src/server/side-request.ts | 48 ++++++--- ...ssue4438-dsh-review-lane-isolation.test.ts | 8 +- ...issue6104-dsh-review-sidecar-shape.test.ts | 98 +++++++++++++++++++ 3 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 tests/issue6104-dsh-review-sidecar-shape.test.ts diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 0746290ab1..d991c40feb 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -153,9 +153,9 @@ export function isDshTitleRequest(parsed: unknown): boolean { // #4438: dsh's auto-review sidecar (@deepseek-ai/dsh-experimental-auto-review's // classifyRisk()) fires before every native tool call under the Auto permission -// tier: a fixed REVIEW_POLICY system prompt + exactly ONE freshly-flattened user -// blob (ENVIRONMENT / PROJECT_INSTRUCTIONS / FILTERED_HISTORY / PENDING_ACTION), -// no tools, same conversation id as the main turn. On at least one deployment +// tier: a fixed REVIEW_POLICY system prompt + a freshly-flattened user body +// (ENVIRONMENT / PROJECT_INSTRUCTIONS / FILTERED_HISTORY / PENDING_ACTION), no +// tools, same conversation id as the main turn. On at least one deployment // class (web profile behind a gateway) the extension's own model client reaches // bili UNSTAMPED — no x-bili-plugin — while the session's sticky plugin binding // keeps the #2432 rebase gate armed for it: every header-gated isolation gate @@ -166,13 +166,38 @@ export function isDshTitleRequest(parsed: unknown): boolean { // skips the output clamp (#453) into permanent upstream 400s. // Shape-decisive WITHOUT a caller-side header gate — deliberately different from // isDshTitleRequest's discipline: the shape carries its own client evidence -// (dsh's fixed REVIEW_POLICY first line, version-pinned like +// (dsh's fixed REVIEW_POLICY sentence, version-pinned like // DSH_TITLE_SYSTEM_PREFIX / DSH_COMPACTION_INSTRUCTION_PREFIX), and the header // gate is precisely what failed here. A main turn can never match: it carries a -// tools surface and multi-turn history; the single-blob bar rejects every -// history-replaying request. +// tools surface, and every entry-level loop below vetoes any assistant/tool +// turn — which every real history resend has. +// #6104: two bars were host-side FORMAT decisions that moved, not identity. +// REVIEW_POLICY gained a `REVIEW_POLICY` banner line (the sentence is no longer +// line 1 ⇒ line-scan, see DSH_AUTO_REVIEW_HEAD_LINES), and 0.2.2's +// reviewMessages() replays the WHOLE conversation as role:"user" text blobs (so +// the old `users === 1` bar rejected the very request it was written for, and +// the review then ate the fold ledger it was supposed to leave alone: 107 DEAD +// tombstones, 0 compressions across the reporter's 5 sessions). The replay veto +// never needed the user count — the assistant/tool veto carries it. export const DSH_AUTO_REVIEW_SYSTEM_PREFIX = "You are the final authorization reviewer for exactly one pending tool call"; +// #6104: dsh versions its REVIEW_POLICY by PREFIXING it with a banner line +// (`REVIEW_POLICY` on 0.2.2-rc.1), so a first-line pin silently misses the +// whole review traffic on any host build that adds one more line above the +// sentence. The pin stays version-pinned to the SENTENCE; only its position is +// read tolerantly — the first few lines of the system carrier, never the whole +// prompt (a policy QUOTED deep inside some other agent's prompt must not +// identify a review call; the reporter's own #6104 body shows nothing else in +// the request can rescue the match, so a miss here runs the review through the +// full pipeline and tombstones its refs as DEAD). +const DSH_AUTO_REVIEW_HEAD_LINES = 5; + +/** The pinned reviewer sentence on any line of the system carrier's head. */ +function isDshAutoReviewPolicy(text: string): boolean { + const lines = text.split("\n", DSH_AUTO_REVIEW_HEAD_LINES); + return lines.some((line) => line.trimStart().startsWith(DSH_AUTO_REVIEW_SYSTEM_PREFIX)); +} + export function isDshAutoReviewRequest(protocol: string | null, parsed: unknown): boolean { if (protocol === null || !parsed || typeof parsed !== "object") return false; const p = parsed as Record; @@ -182,7 +207,7 @@ export function isDshAutoReviewRequest(protocol: string | null, parsed: unknown) // The fixed reviewer policy on ANY wire's system carrier (top-level system, // instructions, systemInstruction.parts, or a role:system entry in // messages[]/input[]) — systemCarrierTexts reads them all (#3149 lesson). - if (!systemCarrierTexts(p).some((t) => t.trimStart().startsWith(DSH_AUTO_REVIEW_SYSTEM_PREFIX))) return false; + if (!systemCarrierTexts(p).some(isDshAutoReviewPolicy)) return false; const entries = protocol === "responses" ? p.input : protocol === "google" ? p.contents : p.messages; if (!Array.isArray(entries)) return false; let users = 0; @@ -204,10 +229,11 @@ export function isDshAutoReviewRequest(protocol: string | null, parsed: unknown) userHasContent = true; } } - // Exactly ONE non-system message with content: the freshly-flattened review - // snapshot. A replayed history (any assistant/tool turn or a second user - // message) vetoes — see the comment above the constant. - return users === 1 && userHasContent; + // ONE or MORE non-system messages with content: the freshly-flattened + // review snapshot (#4438) or its history-replaying form (#6104). The replay + // veto is the loop above — any assistant/tool turn returns false — so a real + // history resend can never reach this line, whatever its user count. + return users >= 1 && userHasContent; } /** #5687 follow-up: a sidecar never carries a durable-state marker. dsh injects * its durable payloads (skills, workspace instructions, MCP catalog, runtime diff --git a/tests/issue4438-dsh-review-lane-isolation.test.ts b/tests/issue4438-dsh-review-lane-isolation.test.ts index 7529547617..88f32d64ee 100644 --- a/tests/issue4438-dsh-review-lane-isolation.test.ts +++ b/tests/issue4438-dsh-review-lane-isolation.test.ts @@ -224,8 +224,12 @@ test("unit: isDshAutoReviewRequest vetoes every main-turn shape (#4438)", () => assert.equal(isDshAutoReviewRequest("openai", { ...reviewBody(true), tools: [{ type: "function", function: { name: "bash" } }] }), false, "tools present"); // Multi-turn history (any assistant/tool turn) vetoes. assert.equal(isDshAutoReviewRequest("openai", { model: MODEL, messages: [{ role: "system", content: REVIEW_POLICY }, { role: "user", content: "a" }, { role: "assistant", content: "b" }, { role: "user", content: "c" }] }), false, "assistant turn in history"); - // Two user messages vetoes (the review is ONE freshly-flattened blob). - assert.equal(isDshAutoReviewRequest("openai", { model: MODEL, messages: [{ role: "system", content: REVIEW_POLICY }, { role: "user", content: "a" }, { role: "user", content: "b" }] }), false, "two user messages"); + // #6104: two+ user messages with NO assistant/tool turn is dsh 0.2.2's + // history-replaying review body (reviewMessages() flattens the whole + // conversation into role:"user" text blobs), not a main turn — a real + // resend always carries assistant turns (the case directly above), and a + // first main turn carries exactly one user message. + assert.equal(isDshAutoReviewRequest("openai", { model: MODEL, messages: [{ role: "system", content: REVIEW_POLICY }, { role: "user", content: "a" }, { role: "user", content: "b" }] }), true, "two user messages, no assistant turn: the #6104 review body"); // No system carrier at all — the prefix must ride a SYSTEM carrier. assert.equal(isDshAutoReviewRequest("openai", { model: MODEL, messages: [{ role: "user", content: "blob" }] }), false, "no system carrier"); // The prefix quoted inside the USER blob is not a policy carrier. diff --git a/tests/issue6104-dsh-review-sidecar-shape.test.ts b/tests/issue6104-dsh-review-sidecar-shape.test.ts new file mode 100644 index 0000000000..446baa6548 --- /dev/null +++ b/tests/issue6104-dsh-review-sidecar-shape.test.ts @@ -0,0 +1,98 @@ +// #6104: dsh 0.2.2-rc.1's auto-review sidecar stopped matching +// isDshAutoReviewRequest, so every review call ran the FULL pipeline under the +// main session id: reconcileFoldCoverage read the (differently serialized) +// history as a wholesale rewrite on both legs (1,006/1,007 alternating system +// fingerprints across 5 sessions), and the refs minted from the wrong +// serialization were tombstoned as DEAD — 107 `compress FAILED … cannot be +// anchored` receipts and 0 successful compressions in those sessions, with no +// un-tombstone path short of the message reappearing in the view. +// +// Both bars that failed were HOST-SIDE FORMAT decisions, not identity: +// (a) REVIEW_POLICY now opens with a `REVIEW_POLICY` banner line, so the +// pinned sentence is on line 2 and startsWith(line 1) is false; +// (b) reviewMessages(snapshot) replays the whole conversation as +// role:"user" text blobs, so `users === 1` is never true once history is +// non-empty. +// The identity is the pinned sentence (banner-tolerant, still version-pinned); +// the replay veto is carried by the assistant/tool veto inside the loop — a +// real resend always carries assistant turns — so the user COUNT itself never +// had to be 1. These cases pin both halves plus the vetoes that must not move. + +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; +process.env.BILI_PERSIST = "0"; + +import { DSH_AUTO_REVIEW_SYSTEM_PREFIX, isDshAutoReviewRequest } from "../src/server/side-request.ts"; + +// dsh 0.2.1-and-earlier REVIEW_POLICY: the pinned sentence IS the first line. +const POLICY_LEGACY = `${DSH_AUTO_REVIEW_SYSTEM_PREFIX} in this conversation.\n\nYou must decide whether the pending action is allowed, and reply with exactly one JSON verdict.`; + +// dsh 0.2.2-rc.1 REVIEW_POLICY: a `REVIEW_POLICY` banner line precedes the +// pinned sentence (the reporter's payload: system=4334 chars, policy[0] = +// "REVIEW_POLICY", policy[1] = the pinned sentence). +const POLICY_RC1 = `REVIEW_POLICY\n${DSH_AUTO_REVIEW_SYSTEM_PREFIX} in this conversation.\n\nYou must decide whether the pending action is allowed, and reply with exactly one JSON verdict.`; + +function anthropic(system: string, entries: unknown[], tools?: unknown[]): Record { + return tools === undefined ? { system, messages: entries } : { system, messages: entries, tools }; +} + +function userText(text: string): Record { + return { role: "user", content: [{ type: "text", text }] }; +} + +// reviewMessages(snapshot): [ENVIRONMENT, ...snapshot.history.map(json)], all +// replayed as role:"user" text blobs. +function reviewSnapshot(sections: string[]): Record[] { + return sections.map((s) => userText(s)); +} + +const SECTIONS = ["ENVIRONMENT\nprofile: web; model: deepseek-flash", "PROJECT_INSTRUCTIONS\n(none)", "FILTERED_HISTORY\n[…]", "PENDING_ACTION\n{\"tool\":\"bash\"}"]; + +test("#6104: the 0.2.2 REVIEW_POLICY banner no longer hides the pinned sentence", () => { + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, reviewSnapshot([SECTIONS[0]!]))), true); + // The legacy first-line shape stays a hit. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_LEGACY, reviewSnapshot([SECTIONS[0]!]))), true); +}); + +test("#6104: a history-replaying review body (N role:user blobs) is still the review", () => { + // Pre-fix this was false for every non-empty history: `users === 1`. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, reviewSnapshot(SECTIONS))), true); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_LEGACY, reviewSnapshot(SECTIONS))), true); + // Responses-wire spelling of the same body. + assert.equal(isDshAutoReviewRequest("responses", { instructions: POLICY_RC1, input: reviewSnapshot(SECTIONS) }), true); +}); + +test("#6104: an assistant or tool turn still vetoes — a real resend can never match", () => { + const replayed = [SECTIONS[0]!, { role: "assistant", content: [{ type: "text", text: "done" }] }, SECTIONS[1]!]; + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, replayed)), false); + // Responses-side typed non-message items can only appear in a real round-trip. + assert.equal(isDshAutoReviewRequest("responses", { instructions: POLICY_RC1, input: [SECTIONS[0]!, { type: "function_call", call_id: "c1", name: "bash", arguments: "{}" }] }), false); +}); + +test("#6104: the tools veto and the identity pin are untouched", () => { + // Any action surface marks an agent turn, even with the reviewer policy. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, reviewSnapshot(SECTIONS), [{ name: "bash", input_schema: {} }])), false); + // Same body shapes, unrelated system text: not the reviewer's call. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic("You are a helpful assistant.", reviewSnapshot(SECTIONS))), false); + // Empty content is never a review snapshot. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, [{ role: "user", content: "" }])), false); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, [])), false); +}); + +test("#6104: the banner tolerance is bounded to the system head, not the whole prompt", () => { + // A policy quoted deep inside someone else's prompt must not identify a review call. + const quoted = ["You are a coding agent.", "", "", "", "", "", `${DSH_AUTO_REVIEW_SYSTEM_PREFIX} in this conversation.`].join("\n"); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(quoted, reviewSnapshot(SECTIONS))), false); + // CRLF carriers (the reporter's host is Windows) read the same. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1.replace(/\n/g, "\r\n"), reviewSnapshot(SECTIONS))), true); + // Block-array system: systemCarrierTexts hands each block over separately, so + // a banner in its own block still leaves the sentence at the head of its own. + assert.equal(isDshAutoReviewRequest("anthropic", { system: [{ type: "text", text: "REVIEW_POLICY" }, { type: "text", text: POLICY_LEGACY }], messages: reviewSnapshot(SECTIONS) }), true); +}); + +test("#6104: non-anthropic wires keep their carrier reading", () => { + // Google: systemInstruction.parts carries the policy; contents hold the blobs. + assert.equal(isDshAutoReviewRequest("google", { systemInstruction: { parts: [{ text: POLICY_RC1 }] }, contents: reviewSnapshot(SECTIONS).map((e) => ({ role: "user", parts: [{ text: String((e.content as { text: string }[])[0]!.text) }] })) }), true); +}); From a6e074f8b0f80e5f7ac5059b78ce6f80f46d5291 Mon Sep 17 00:00:00 2001 From: Gong Liu <1360187968@qq.com> Date: Tue, 10 Nov 2026 15:04:51 +0800 Subject: [PATCH 42/78] test: pin the #6104 review shape in the side-lane invariants suite (lane-guard #2170) lane-guard requires any edit to src/server/side-request.ts to land with one of the four dedicated side-lane suites. Adds an e2e case (N): the 0.2.2 banner-shifted policy + replayed user-blob body rides the side passthrough (counts nowhere, mints no |sub lane, next main turn still resolves to the raw key), while the tools-surface and assistant-turn vetoes keep the request on the main pipeline. (cherry picked from commit 2c27223d3b415e1d11d93460992fe8dd21f63f0d) --- tests/side-lane-invariants.test.ts | 59 ++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 43fcbdeeb3..cf46234777 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -621,3 +621,62 @@ test("(M) #5135 claude-code title sidecar: no anchor claim in either arrival ord await h.close(); } }); + +test("(N) #6104 dsh 0.2.2 review shape: banner-shifted policy + replayed user body ride the side passthrough", async () => { + // #2170 duty: a lane-gate edit lands WITH the suite that pins it. The + // detector's two bars were host-side FORMAT decisions (policy line offset, + // number of user blobs), so both shapes are pinned here end-to-end, plus + // the vetoes that must never move. + const h = await harness(); + try { + const conv = "rev6104"; + const POLICY = "REVIEW_POLICY\nYou are the final authorization reviewer for exactly one pending tool call in this conversation.\n\nReply with exactly one JSON verdict."; + const BLOBS = [ + { role: "user", content: "ENVIRONMENT\nprofile: desktop; model: claude-test" }, + { role: "user", content: "PROJECT_INSTRUCTIONS\n(none)" }, + { role: "user", content: "FILTERED_HISTORY\nuser: main turn 0\nassistant: answer" }, + { role: "user", content: "PENDING_ACTION\n{\"tool\":\"bash\",\"args\":{\"command\":\"ls\"}}" }, + ]; + // The main turn anchors the raw key. + await h.dshSend(conv, [mainMsg(0)], "main", "MAIN OPERATING SYSTEM", 256); + assert.equal(resolveConversation(conv).session!.stats.requests, 1, "main turn anchors the raw key"); + const hits = h.forwarded.length; + + // The 0.2.2 review shape: host-sized budget (past the <=200 heuristic), + // no tools, no agent header, banner ABOVE the pinned sentence, and the + // whole history replayed as role:"user" blobs. + await h.dshSend(conv, BLOBS, undefined, POLICY, 384000); + assert.equal(h.forwarded.length, hits + 1, "the review call reaches the upstream"); + assert.equal(resolveConversation(conv).session!.stats.requests, 1, "the review call counts nowhere (side passthrough, #388)"); + assert.equal(listSessions().filter((s) => s.id.startsWith(conv + "|")).length, 0, "the review call mints no |sub lane"); + + // The next main turn still resolves to the SAME session and keeps + // compressing — the ledger-damage symptom was that it did not. + await h.dshSend(conv, [mainMsg(0), { role: "assistant", content: "answer" }, mainMsg(1)], "main", "MAIN OPERATING SYSTEM", 256); + assert.equal(resolveConversation(conv).session!.stats.requests, 2, "the next main turn lands on the raw key, not a drifted lane"); + assert.equal(listSessions().filter((s) => s.id.startsWith(conv + "|")).length, 0, "no drift on the main leg"); + + // Veto A — any tools surface marks an agent turn: full pipeline, counted. + // Counted ACROSS the raw key and any design persona child: a tool-carrying + // request with a foreign system prompt forks by design (#1916), and the + // point here is that it was NOT handed to the verbatim side lane. + const countAll = (id: string) => listSessions() + .filter((s) => s.id === id || s.id.startsWith(id + "|")) + .reduce((n, s) => n + (s.stats?.requests ?? 0), 0); + const convTools = "rev6104-tools"; + await h.dshSend(convTools, [mainMsg(0)], "main", "MAIN OPERATING SYSTEM", 256); + const hitsT = h.forwarded.length; + await h.dshSend(convTools, BLOBS, undefined, POLICY, 384000, [{ name: "bash", input_schema: { type: "object" } }]); + assert.equal(h.forwarded.length, hitsT + 1); + assert.equal(countAll(convTools), 2, "tools-shape request rides the full pipeline (unchanged veto)"); + + // Veto B — an assistant turn inside the body is a real resend, not the + // review (the load-bearing replay veto now that the user count is out). + const convReplay = "rev6104-replay"; + await h.dshSend(convReplay, [mainMsg(0)], "main", "MAIN OPERATING SYSTEM", 256); + await h.dshSend(convReplay, [...BLOBS.slice(0, 2), { role: "assistant", content: "earlier answer" }, ...BLOBS.slice(2)], undefined, POLICY, 384000); + assert.equal(countAll(convReplay), 2, "an assistant turn keeps the request on the main pipeline"); + } finally { + await h.close(); + } +}); From 02c9412850faeec67a2299ad63f2c5796500d934 Mon Sep 17 00:00:00 2001 From: Gong Liu <1360187968@qq.com> Date: Tue, 10 Nov 2026 16:00:25 +0800 Subject: [PATCH 43/78] test: pin both ends of the #6104 head window DSH_AUTO_REVIEW_HEAD_LINES is the entire contract of the banner tolerance, but the suite only pinned 'a deep quote does not match' (line 7). Pin line 5 as the last matching line and line 6 as the first non-matching one, so a future change to the constant has to face the case it stops catching. (cherry picked from commit 7f91ab7285c02a8db91c0b651e1a1cda0fca701e) --- tests/issue6104-dsh-review-sidecar-shape.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/issue6104-dsh-review-sidecar-shape.test.ts b/tests/issue6104-dsh-review-sidecar-shape.test.ts index 446baa6548..924e7baa17 100644 --- a/tests/issue6104-dsh-review-sidecar-shape.test.ts +++ b/tests/issue6104-dsh-review-sidecar-shape.test.ts @@ -85,6 +85,17 @@ test("#6104: the banner tolerance is bounded to the system head, not the whole p // A policy quoted deep inside someone else's prompt must not identify a review call. const quoted = ["You are a coding agent.", "", "", "", "", "", `${DSH_AUTO_REVIEW_SYSTEM_PREFIX} in this conversation.`].join("\n"); assert.equal(isDshAutoReviewRequest("anthropic", anthropic(quoted, reviewSnapshot(SECTIONS))), false); + // The window itself, line by line: DSH_AUTO_REVIEW_HEAD_LINES is the whole + // contract of the tolerance, so pin BOTH ends of it — a future "just raise + // the constant" edit has to face the case it stops catching. + const sentenceAt = (line: number): string => { + const lines = ["You are a coding agent.", "body", "body", "body", "body", "body", "body"]; + lines[line - 1] = `${DSH_AUTO_REVIEW_SYSTEM_PREFIX} in this conversation.`; + return lines.join("\n"); + }; + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(sentenceAt(1), reviewSnapshot(SECTIONS))), true, "line 1 (the legacy pin) still matches"); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(sentenceAt(5), reviewSnapshot(SECTIONS))), true, "line 5 is the last line inside the window"); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(sentenceAt(6), reviewSnapshot(SECTIONS))), false, "line 6 is outside the window"); // CRLF carriers (the reporter's host is Windows) read the same. assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1.replace(/\n/g, "\r\n"), reviewSnapshot(SECTIONS))), true); // Block-array system: systemCarrierTexts hands each block over separately, so From c8374046feacd529950c595603f3d36432c4a17a Mon Sep 17 00:00:00 2001 From: Sajjad Pakdamansavoji <44399382+SajjadPSavoji@users.noreply.github.com> Date: Tue, 10 Nov 2026 01:47:08 -0500 Subject: [PATCH 44/78] fix(omp): default PI_ANTHROPIC_MESSAGE_THREADS=0 so bili sees the full history (cherry picked from commit 92bb76b2414875bd9e6e9e98824c98299d3a31ee) --- src/agent/native-bootstrap.ts | 17 +++++++++++++++++ src/agent/omp-native.ts | 5 ++++- src/launcher.ts | 6 +++++- src/server.ts | 2 +- src/server/context-window.ts | 11 +++++++++++ src/server/handle.ts | 15 ++++++++++++++- tests/launcher.test.ts | 10 +++++++++- tests/model-windows.test.ts | 9 ++++++++- tests/omp-native.test.ts | 14 +++++++++++++- 9 files changed, 82 insertions(+), 7 deletions(-) diff --git a/src/agent/native-bootstrap.ts b/src/agent/native-bootstrap.ts index 23bbc2868b..a437f315a3 100644 --- a/src/agent/native-bootstrap.ts +++ b/src/agent/native-bootstrap.ts @@ -97,6 +97,23 @@ export function applyOmpFirstEventTimeout(env: NodeJS.ProcessEnv): void { if (value !== undefined) env[OMP_FIRST_EVENT_TIMEOUT_ENV] = value; } +/** #6088: OMP >= 18.18 sends official-URL Claude turns as Anthropic message + * threads — after the first turn each request carries only the newest + * message(s) and Anthropic keeps the history server-side, so bili never sees + * the conversation and never compresses it (silently). PI_PROXY_ANTHROPIC + * leaves the model's base URL official, so OMP's model policy keeps threads on. + * When bili owns the process's model traffic we therefore default threads off; + * an explicit user value wins (blank counts as unset). */ +export const OMP_MESSAGE_THREADS_ENV = "PI_ANTHROPIC_MESSAGE_THREADS"; + +/** Stamp "0" onto env for OMP's message-threads switch when (and only when) the + * user has not pinned a value. No-op otherwise. */ +export function applyOmpMessageThreadsOff(env: NodeJS.ProcessEnv): void { + const raw = env[OMP_MESSAGE_THREADS_ENV]; + if (raw !== undefined && raw.trim().length > 0) return; + env[OMP_MESSAGE_THREADS_ENV] = "0"; +} + /** Coexistence marker (#820): tells standalone in-process bili extensions * (billion-context-pi / opencode-acp) that a host-native entry owns THIS * process so they back off instead of double-compressing. Set synchronously diff --git a/src/agent/omp-native.ts b/src/agent/omp-native.ts index a18c0e9fb1..a908cdfe01 100644 --- a/src/agent/omp-native.ts +++ b/src/agent/omp-native.ts @@ -23,7 +23,7 @@ import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; import { createBiliPlugin } from "./pi.js"; -import { applyOmpFirstEventTimeout, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; +import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, setDeclaredModelEndpoints, type NativeInterceptState } from "./native-intercept.js"; import { loadDeclaredModelEndpoints } from "../model-endpoints.js"; import { clearIncrementalProbe, createPrepareDirectSend, hostMsgIdHintFor, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; @@ -411,8 +411,11 @@ if (plan.mode !== "off") armNativeOmp(plan); // preflight over a large context holds the response for minutes while OMP only // sees keep-alive comments, and its default 300s timer would abort mid-compression. // Sync at module eval so it lands before the first request; user-pinned values win. +// #6088: same for OMP's message threads — OMP reads the switch per request, so +// defaulting it off here makes every turn carry the full history bili compresses. if (process.env.NODE_TEST_CONTEXT === undefined && ompTrafficRidesBili(process.env)) { applyOmpFirstEventTimeout(process.env); + applyOmpMessageThreadsOff(process.env); } // node:test imports this module for shouldBootstrapNativeOmp — never diff --git a/src/launcher.ts b/src/launcher.ts index 285a07a6c6..ef02421b52 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -61,7 +61,7 @@ import { stateDir } from "./paths.js"; import { TWIN_ADOPT_EXIT_CODE } from "./listen-policy.js"; import { tcpConnectable } from "./liveness.js"; import { selfPackageRoot, isBiliPiEntry, ompPluginLoadedFrom, dshNativeInstalled, claudeNativeInstalled, piPackageStrings } from "./plugin-install.js"; -import { applyOmpFirstEventTimeout, envMillis } from "./agent/native-bootstrap.js"; +import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, envMillis } from "./agent/native-bootstrap.js"; import { logHostEvent } from "./agent/host-log.js"; import { compactionDisablePatchRows } from "./agent/dsh-compaction-disable.js"; import { log as teeLog, logTimestamp } from "./logger.js"; @@ -7147,6 +7147,10 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} // hold the first SSE event well past OMP's default 300s first-parsed-event // watchdog — export a wider first-event budget unless the user pinned one. applyOmpFirstEventTimeout(env); + // #6088: OMP >= 18.18 message threads send only the newest message per + // turn, so bili would never see (or compress) the conversation — turn + // them off unless the user pinned a value. + applyOmpMessageThreadsOff(env); const ompExt = selfDistFile("agent/omp.js"); if (ompExt && fs.existsSync(ompExt) && !ompPluginLoadedFrom(ompRealHome)) { clientArgs = ["-e", ompExt, ...clientArgs]; diff --git a/src/server.ts b/src/server.ts index 59a9e6c4f8..03912500e6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4862,6 +4862,6 @@ function logMsg(opts: ProxyOptions, level: string, msg: string): void { } export { getUnrecognizedPathStats, logDumpFailure, logUnrecognizedPath } from "./server/observability.js"; -export { BILI_HOP_HEADER, parseLauncherModelWindows, anthropicBetaContextWindow, capRegistryWindowByStandard, expandedContextSuffixWindow } from "./server/context-window.js"; +export { BILI_HOP_HEADER, parseLauncherModelWindows, anthropicBetaContextWindow, anthropicBetaMessageThreads, capRegistryWindowByStandard, expandedContextSuffixWindow } from "./server/context-window.js"; export { BILI_TOOL_NAMES, isSideRequest, isStructuredOutputCall, outputBudgetField, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR, type OutputBudgetField } from "./server/side-request.js"; export { countSystemAndToolsTokens, estimateInputTokens, estimateWireOverhead, clampOutputBudget, emergencyNudge, projectThinkingMass, type ThinkingMassInput } from "./server/budget.js"; diff --git a/src/server/context-window.ts b/src/server/context-window.ts index e17fdfe1f3..7029e73653 100644 --- a/src/server/context-window.ts +++ b/src/server/context-window.ts @@ -129,6 +129,17 @@ export function anthropicBetaContextWindow(headers: Record): boolean { + const raw = headers["anthropic-beta"]; + if (raw === undefined) return false; + const list = Array.isArray(raw) ? raw.join(",") : raw; + return list.split(",").some((part) => part.trim().toLowerCase().startsWith("message-threads")); +} + // #1321: an [Nm]-suffixed model name (e.g. "claude-opus-5-5[1m]") is the // client's own declaration that THIS request runs on the expanded tier — the // same evidence class as the context-Nm beta header, carried in the model id diff --git a/src/server/handle.ts b/src/server/handle.ts index ead6d3bd60..d37102c4be 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -68,7 +68,7 @@ import { matchModelEndpoint } from "../model-endpoints.js"; import { autoModeForgeEnabled, dshAuthoredCompactionEnabled, imageRescaleEnabled, limitsDecodedBytes as knobLimitsDecodedBytes, limitsIngestBytes as knobLimitsIngestBytes, noInjectTool as knobNoInjectTool, rawDumpDir as knobRawDumpDir } from "../knobs.js"; import { buildForgeResponse, classifierForgeVariant, estimateCjkTokens, isAutoModeClassifier, requestText, summarizeAction } from "../auto-mode-forge.js"; import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, unwrapV1Internal } from "../v1internal.js"; -import { anthropicBetaContextWindow, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; +import { anthropicBetaContextWindow, anthropicBetaMessageThreads, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; @@ -124,6 +124,12 @@ const codexConfigWindowWarned = new Set(); // truncated subviews never adopt weakly regardless of ratio. const WEAK_ADOPT_MIN_PREFIX_RATIO = 0.8; +// #6088: warn-once per (pre-fork) session when a request negotiates Anthropic +// message threads — every turn then carries only the newest message(s), so the +// session silently never compresses. Keyed by the base id so `|sub:` forks of +// the same conversation do not repeat the line. +const messageThreadsWarned = new Set(); + // #2986: the derivedFromSessionId link is what keeps a kind child session // reachable along its parent chain after a restart — without it the shared // prefix is re-sent whole and over-window requests 503 (the #2985 failure). It @@ -1636,6 +1642,13 @@ async function handleRequest( // embed the id manually. One statement, runs in this request's own // async context, so concurrent requests never cross-contaminate. enterSessionContext(sessionId); + if (protocol === "anthropic" && anthropicBetaMessageThreads(req.headers)) { + const baseId = sessionId.split("|sub:")[0]; + if (!messageThreadsWarned.has(baseId)) { + messageThreadsWarned.add(baseId); + loggerLog("warn", `[${baseId}] Anthropic message threads are on (anthropic-beta message-threads-*): the upstream holds the history and each request carries only the newest message(s), so bili cannot compress this session; for OMP set PI_ANTHROPIC_MESSAGE_THREADS=0 (#6088)`); + } + } // #1086/#1357: content-fallback chain observation, now that identity is known. // Artifacts + processed local state ⇒ self-produced: process normally. // Artifacts + NO local state ⇒ ADVISORY observation (#1357 Phase 1): the diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index b98142051c..1bb8f56d2f 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -734,6 +734,8 @@ test("runLaunch omp #535: refuses launch when http rewrites needed and extension const prevUserProfile = process.env.USERPROFILE; const prevClientBin = process.env.BILI_CLIENT_BIN; const prevOmpDir = process.env.PI_CODING_AGENT_DIR; + const prevThreads = process.env.PI_ANTHROPIC_MESSAGE_THREADS; + delete process.env.PI_ANTHROPIC_MESSAGE_THREADS; process.env.HOME = home; if (prevUserProfile !== undefined) process.env.USERPROFILE = home; delete process.env.PI_CODING_AGENT_DIR; @@ -750,9 +752,11 @@ test("runLaunch omp #535: refuses launch when http rewrites needed and extension if (distExisted) fs.renameSync(distAgent, distBackup); const clientArgsSeen: string[][] = []; - const spawnImpl: SpawnFn = (cmd, args) => { + let childEnv: NodeJS.ProcessEnv | undefined; + const spawnImpl: SpawnFn = (cmd, args, options) => { if (cmd === process.env.BILI_CLIENT_BIN) { clientArgsSeen.push([...args]); + childEnv = options.env; const child = makeFakeChild(0); const orig = child.on!.bind(child); (child as { on: SpawnChild["on"] }).on = (event, listener) => { @@ -793,6 +797,8 @@ test("runLaunch omp #535: refuses launch when http rewrites needed and extension assert.ok(!clientArgsSeen[0].includes("-e"), "installed entry loads the plugin — no -e double load"); assert.equal(fs.existsSync(`${ompHome}-bili`), false, "still no overlay dir"); assert.deepEqual(exitCalls, [0]); + // #6088: OMP message threads would send only the newest message per turn + assert.equal(childEnv?.PI_ANTHROPIC_MESSAGE_THREADS, "0", "omp message threads defaulted off"); } finally { process.exit = prevExit; if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; @@ -802,6 +808,8 @@ test("runLaunch omp #535: refuses launch when http rewrites needed and extension else process.env.USERPROFILE = prevUserProfile; if (prevOmpDir === undefined) delete process.env.PI_CODING_AGENT_DIR; else process.env.PI_CODING_AGENT_DIR = prevOmpDir; + if (prevThreads === undefined) delete process.env.PI_ANTHROPIC_MESSAGE_THREADS; + else process.env.PI_ANTHROPIC_MESSAGE_THREADS = prevThreads; if (distExisted) fs.renameSync(distBackup, distAgent); rmrf(home); } diff --git a/tests/model-windows.test.ts b/tests/model-windows.test.ts index 714db58036..2923ff9c21 100644 --- a/tests/model-windows.test.ts +++ b/tests/model-windows.test.ts @@ -12,7 +12,7 @@ import { collectModelWindows, type ClientConfig, } from "../src/client-config.ts"; -import { parseLauncherModelWindows, anthropicBetaContextWindow } from "../src/server.ts"; +import { parseLauncherModelWindows, anthropicBetaContextWindow, anthropicBetaMessageThreads } from "../src/server.ts"; import { MAX_ANTHROPIC_BETA_WINDOW } from "../src/server/context-window.ts"; test("parseOmpYaml: captures per-model contextWindow (models after baseUrl)", () => { @@ -222,6 +222,13 @@ test("parseLauncherModelWindows: valid JSON, invalid input, non-numeric filtered // overridden. The parser is per-request (the header may appear/disappear // between requests of the same session). +test("#6088 anthropicBetaMessageThreads: detects the message-threads beta among others", () => { + assert.equal(anthropicBetaMessageThreads({ "anthropic-beta": "oauth-2025-04-20, message-threads-2026-08-12" }), true); + assert.equal(anthropicBetaMessageThreads({ "anthropic-beta": ["context-1m-2025-08-07", "message-threads-2026-08-12"] }), true); + assert.equal(anthropicBetaMessageThreads({ "anthropic-beta": "context-1m-2025-08-07" }), false); + assert.equal(anthropicBetaMessageThreads({}), false); +}); + test("anthropicBetaContextWindow: context-1m beta → 1,000,000", () => { assert.equal(anthropicBetaContextWindow({ "anthropic-beta": "context-1m-2025-08-07" }), 1_000_000); }); diff --git a/tests/omp-native.test.ts b/tests/omp-native.test.ts index f61ccfce43..9f2bca97d1 100644 --- a/tests/omp-native.test.ts +++ b/tests/omp-native.test.ts @@ -7,7 +7,7 @@ import { pathToFileURL } from "node:url"; import { createServer } from "node:http"; import type { AddressInfo } from "node:net"; import factory, { ompTrafficRidesBili, shouldBootstrapNativeOmp, planNativeOmp, armNativeOmp, _resetNativeStateForTest, _setSpawnForTest, _stateRespawnForTest, _noteRoutedForTest } from "../src/agent/omp-native.ts"; -import { applyOmpFirstEventTimeout, nativeProxyScriptPath, ompFirstEventTimeoutValue, OMP_FIRST_EVENT_TIMEOUT_DEFAULT_MS, OMP_FIRST_EVENT_TIMEOUT_ENV } from "../src/agent/native-bootstrap.ts"; +import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, OMP_MESSAGE_THREADS_ENV, nativeProxyScriptPath, ompFirstEventTimeoutValue, OMP_FIRST_EVENT_TIMEOUT_DEFAULT_MS, OMP_FIRST_EVENT_TIMEOUT_ENV } from "../src/agent/native-bootstrap.ts"; import { pluginInstall, pluginRemove, pluginStatusAll, selfPackageRoot, ompPluginLoadedFrom } from "../src/plugin-install.ts"; import { rmrf } from "./tmp-rm.ts"; @@ -72,6 +72,18 @@ test("#1774 applyOmpFirstEventTimeout: stamps only when unpinned, idempotent on assert.equal(pinned[OMP_FIRST_EVENT_TIMEOUT_ENV], "45000"); }); +test("#6088 applyOmpMessageThreadsOff: defaults to \"0\" only when unpinned, blank counts as unset", () => { + const clean: NodeJS.ProcessEnv = {}; + applyOmpMessageThreadsOff(clean); + assert.equal(clean[OMP_MESSAGE_THREADS_ENV], "0"); + const blank: NodeJS.ProcessEnv = { [OMP_MESSAGE_THREADS_ENV]: " " }; + applyOmpMessageThreadsOff(blank); + assert.equal(blank[OMP_MESSAGE_THREADS_ENV], "0"); + const pinned: NodeJS.ProcessEnv = { [OMP_MESSAGE_THREADS_ENV]: "1" }; + applyOmpMessageThreadsOff(pinned); + assert.equal(pinned[OMP_MESSAGE_THREADS_ENV], "1", "explicit user value wins"); +}); + test("nativeProxyScriptPath: dist/agent/omp-native.js resolves to the package bin", () => { const agentFile = path.resolve(path.sep, "opt", "pkg", "dist", "agent", "omp-native.js"); const resolved = nativeProxyScriptPath(pathToFileURL(agentFile).href); From b64f1cfca2a1062af06d77c4649166b95bb50324 Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 11 Nov 2026 02:39:46 +0800 Subject: [PATCH 45/78] docs(omp): document exported PI_ANTHROPIC_MESSAGE_THREADS default (#6088) The launcher/extension now export PI_ANTHROPIC_MESSAGE_THREADS=0 for OMP traffic riding bili, but docs/configuration.md en+zh only listed the prior launcher export PI_PROXY_ANTHROPIC. Add both parameter-table rows and extend the omp lane-table cell in both languages, mirroring the PI_PROXY_ANTHROPIC style. (cherry picked from commit 4c96dc14f2d55dcc53529e64f187b9095ed6984a) --- docs/configuration.md | 3 ++- docs/configuration.zh-CN.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 382c4ab7da..772604ed48 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2191,6 +2191,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_ENCRYPTION_KEY` | Encrypt session files at rest (#708) for deployments on untrusted nodes. Exactly 32 bytes, hex (64 chars) or base64; unset = unencrypted plain-JSON files (or `BILIZSTD1` when `BILI_PERSIST_ZSTD=1` — see `BILI_PERSIST_ZSTD`). When set: every session file is written as `BILIENC1` AES-256-GCM over JSON that is zstd-compressed only when `BILI_PERSIST_ZSTD=1` (zstd on Node ≥ 22.15, raw body otherwise) — enabling compression also shrinks the file ~5–10×. Encryption and compression are now independent knobs (#1080). The key comes ONLY from this environment variable — never written to disk or logs — so keep it out of the same filesystem's reach. Invalid values abort startup (fail fast, never silently unencrypted). Booting with the wrong key skips the affected sessions as corrupt (logged, no crash). Losing the key makes encrypted sessions permanently unreadable. Symmetric by design (the same process encrypts and decrypts). Existing unencoded files are never rewritten at boot — they encrypt organically on their next save (downgrade safety; see `BILI_PERSIST_ZSTD`). Threat model (#708, owner-confirmed): it defends against **offline/mechanical** file acquisition — provider disk swaps, offline disk snapshots after node-image drift, stolen volumes, leaked backups, cloud-synced state dirs — where an offline third party cannot read the content without the key. It does NOT defend against targeted adversaries with live access to the node; for that tier move the trust root out of the proxy (KMS / TEE / confidential VMs, hardened permissions) instead of solving it inside the proxy — at that level far more than the key is exposed, so proxy-level key handling is not the boundary to hold (`BILI_PERSIST=0` disables persistence entirely). Double-encrypting the key with a second key from the same process/environment adds nothing: in every offline-theft scenario the attacker lacks exactly one artifact — your off-disk secret — whether it is called the data key or the wrapping key; only a wrapping key in a different trust domain (KMS/TPM/TEE) raises the bar, and that is scenario 2 above. | | `BILLION_CONTEXT_PROXY` | Exported by the launcher; client-side bili plugins/extensions detect it and self-disable their own compression (no double compression). | | `PI_PROXY_ANTHROPIC` | Exported by the `bili omp` launcher (only when neither `PI_PROXY` nor `PI_PROXY_ANTHROPIC` comes from the user's environment): points OMP's Cowork anthropic transport at the proxy so those requests ride the compressible lane instead of node:https direct (#2583/#3556). User-set values always win. | +| `PI_ANTHROPIC_MESSAGE_THREADS` | Exported as `0` by the `bili omp` launcher and set by the omp-native extension on load (only when the user has not set a non-blank value): OMP ≥ 18.18 otherwise sends official-URL Claude turns as Anthropic message threads, where every request carries only the newest message(s) and the upstream holds the history — bili never sees the conversation, so nothing folds (#6088). User-set values always win; pinning it yourself keeps threads on, and the proxy logs one warning per base session for such requests. | | `BILLION_CONTEXT_PLUGIN` | Set `0` to disable plugin mode entirely (wire-level tool injection resumes). | | `BILI_LAUNCHER_MODEL_WINDOWS` | Internal: the launcher hands the client's own per-model context windows (pi `models.json`, omp `models.yml`, opencode `models..limit`, codex `model_context_window`) to the spawned proxy as JSON, so the nudge denominator matches the real window for self-hosted models. Only the launcher sets it — no user configuration. | | `BILI_LAUNCHER_LANE` | Internal: the launcher hands its client's lane name (pi / codex / claude / …) to the spawned proxy, which records it in the instance file (#1225). Reuse is identity-based: two instances with *different declared* lanes never attach to each other; an instance without a declared lane is a manually-started user-zone daemon — attachable by default (#1660). Only the launcher sets it — no user configuration. | @@ -2433,7 +2434,7 @@ How each client is pointed at the proxy (set automatically in the child env): | Client | Redirect | CA trust | |---|---|---| | pi | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env manifest (extension `registerProvider`) | `NODE_EXTRA_CA_CERTS` | -| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env manifest (extension `registerProvider`) + `PI_PROXY_ANTHROPIC=` when neither `PI_PROXY` nor `PI_PROXY_ANTHROPIC` is user-set (routes OMP's Cowork anthropic lane through the proxy instead of node:https direct, #2583/#3556) | `NODE_EXTRA_CA_CERTS` | +| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env manifest (extension `registerProvider`) + `PI_PROXY_ANTHROPIC=` when neither `PI_PROXY` nor `PI_PROXY_ANTHROPIC` is user-set (routes OMP's Cowork anthropic lane through the proxy instead of node:https direct, #2583/#3556) + `PI_ANTHROPIC_MESSAGE_THREADS=0` when unset (otherwise OMP ≥ 18.18 keeps the conversation history server-side, invisible to bili, #6088) | `NODE_EXTRA_CA_CERTS` | | codex | `HTTPS_PROXY` + `-c key=value` overrides | `SSL_CERT_FILE` → `combined-ca.pem` | | claude | `ANTHROPIC_BASE_URL` = `/bili/` URL (Vertex: `ANTHROPIC_VERTEX_BASE_URL`, when `CLAUDE_CODE_USE_VERTEX=1`; + `NO_PROXY`/`no_proxy` loopback merge, #3446) | none needed | | opencode | `HTTPS_PROXY` + isolated `OPENCODE_CONFIG` | `NODE_EXTRA_CA_CERTS` | diff --git a/docs/configuration.zh-CN.md b/docs/configuration.zh-CN.md index 4da27b69a8..2f9be7f943 100644 --- a/docs/configuration.zh-CN.md +++ b/docs/configuration.zh-CN.md @@ -2159,6 +2159,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_ENCRYPTION_KEY` | 会话文件静态加密(#708),适用于部署在不可信节点的场景。密钥必须恰好 32 字节,hex(64 字符)或 base64;未设置 = 不加密的纯 JSON 文件(设 `BILI_PERSIST_ZSTD=1` 时为 `BILIZSTD1`——参见 `BILI_PERSIST_ZSTD`)。设置后:每个会话文件均以 `BILIENC1` 格式写入,即对 JSON 施加 AES-256-GCM 加密,JSON 仅在 `BILI_PERSIST_ZSTD=1` 时以 zstd 压缩(Node ≥ 22.15 使用 zstd,其余情况写入原始数据)——启用压缩还可将文件体积缩小约 5–10 倍。加密与压缩现为独立的配置项(#1080)。密钥只从该环境变量读取——永不落盘、永不进日志——请确保它不受同一文件系统上的其他进程触及。非法值会导致启动中止(快速失败,绝不静默明文运行)。用错误的密钥启动时,受影响的会话按损坏文件跳过(有日志,不崩溃)。丢失密钥将使已加密的会话永久不可读。对称加密为刻意设计(同一进程既加密又解密)。已有的未编码文件从不在启动时改写——在其下一次保存时自然加密(降级安全;参见 `BILI_PERSIST_ZSTD`)。威胁模型(#708,owner 确认):防的是**离线/机械性**的文件获取——云厂商换盘、节点镜像漂移后的离线磁盘快照、磁盘镜像失窃、备份泄露、被云同步的状态目录——离线第三方拿不到密钥即无法读取内容。不防御对活节点有访问权的定向攻击者;那一档应把信任根移出 proxy(KMS / TEE / 机密虚拟机 + 强化权限体系),而不是在 proxy 本身想办法——到了那个程度暴露的远不止密钥,proxy 层不是该守的边界(`BILI_PERSIST=0` 可彻底关闭持久化)。用同一进程/环境中的第二把密钥对密钥做二次加密不增加任何安全性:所有离线失窃场景里攻击者缺的始终只有一个工件——你的非落盘秘密——无论它叫数据密钥还是包裹密钥;只有把包裹密钥放进不同信任域(KMS/TPM/TEE)才能提高门槛,而那属于上面的场景 2。 | | `BILLION_CONTEXT_PROXY` | launcher 会导出它;客户端侧 bili 插件/扩展检测到后自禁用自身压缩(避免双重压缩)。 | | `PI_PROXY_ANTHROPIC` | 由 `bili omp` launcher 导出(仅当 `PI_PROXY` 与 `PI_PROXY_ANTHROPIC` 均未来自用户环境时):把 OMP 的 Cowork anthropic 传输指向代理,让这些请求走可压缩通道而非 node:https 直连(#2583/#3556)。用户设置的值始终优先。 | +| `PI_ANTHROPIC_MESSAGE_THREADS` | 由 `bili omp` launcher 导出为 `0`,omp-native 扩展加载时同样设置(仅当用户未设置非空值时):否则 OMP ≥ 18.18 会把官方 URL 的 Claude 轮次走 Anthropic message threads —— 每个请求只携带最新消息、历史由上游持有,bili 看不到会话内容、无从压缩(#6088)。用户设置的值始终优先;自行钉住该值可保留 threads,代理会对这类请求每个基础会话告警一次。 | | `BILLION_CONTEXT_PLUGIN` | 设 `0` 彻底关闭插件模式(恢复 wire 层工具注入)。 | | `BILI_LAUNCHER_MODEL_WINDOWS` | 内部使用:launcher 把客户端自身配置里的逐模型上下文窗口(pi `models.json`、omp `models.yml`、opencode `models..limit`、codex `model_context_window`)以 JSON 传给自己拉起的代理,让 nudge 分母对自托管模型也用真实窗口。只有 launcher 会设置,无需用户配置。 | | `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式);设 `1` 强制插件模式。默认注入——但 codex 上游为本地/私网地址时自动退回 wire 模式(sglang/vllm/ollama 不解析 codex 的 namespace 工具类型)。见[启动器参考](#启动器参考)。 | @@ -2400,7 +2401,7 @@ MITM 只对一份**白名单**中的模型域名生效(`open.bigmodel.cn`、`a | 客户端 | 重定向方式 | CA 信任 | |---|---|---| | pi | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`) | `NODE_EXTRA_CA_CERTS` | -| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`)+ 用户未设置 `PI_PROXY` / `PI_PROXY_ANTHROPIC` 时的 `PI_PROXY_ANTHROPIC=`(将 OMP 的 Cowork anthropic 通道经代理路由而非 node:https 直连,#2583/#3556) | `NODE_EXTRA_CA_CERTS` | +| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`)+ 用户未设置 `PI_PROXY` / `PI_PROXY_ANTHROPIC` 时的 `PI_PROXY_ANTHROPIC=`(将 OMP 的 Cowork anthropic 通道经代理路由而非 node:https 直连,#2583/#3556)+ 未设置时的 `PI_ANTHROPIC_MESSAGE_THREADS=0`(否则 OMP ≥ 18.18 的会话历史留在服务端、bili 不可见,#6088) | `NODE_EXTRA_CA_CERTS` | | codex | `HTTPS_PROXY` + `-c key=value` 覆盖 | `SSL_CERT_FILE` → `combined-ca.pem` | | claude | `ANTHROPIC_BASE_URL` = `/bili/` URL(Vertex:`ANTHROPIC_VERTEX_BASE_URL`,当 `CLAUDE_CODE_USE_VERTEX=1`) | 无需 | | opencode | `HTTPS_PROXY` + 隔离 `OPENCODE_CONFIG` | `NODE_EXTRA_CA_CERTS` | From 38c171f92d2c6386ca3cbca453642e9a254427c8 Mon Sep 17 00:00:00 2001 From: aftabkh4n Date: Wed, 11 Nov 2026 05:13:45 +0000 Subject: [PATCH 46/78] fix(win-cmd): double trailing backslashes when quoting for cmd.exe On the comspec path (.cmd/.bat shims such as claude.cmd, and dsh.cmd), quoteWinToken wrapped a whitespace-bearing token in quotes but left its trailing backslashes as they were. The program the shim starts parses 2n backslashes before a quote as n (MSVCRT / CommandLineToArgvW, the same rule splitWindowsCommandLine implements), so: - an odd run ("list C:\My Projects\") was refused by winCmdUnsafeToken as "an odd number of trailing backslashes"; - an even run passed the #2196 guard as safe but arrived with half its backslashes ("C:\My Dir\\" -> "C:\My Dir\"). Doubling the trailing run inside the quotes makes both lossless, so the odd-run refusal is removed. Round-tripping 200,000 random tokens of letters, spaces, tabs and backslashes through quoteWinToken and splitWindowsCommandLine now reproduces every token; before, 11,034 of the tokens the guard accepted came back changed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014TonJ1geSd97oAtSBZofUK (cherry picked from commit 67fc1eecc63cde71a36fb75215332fed33d90e86) --- src/dsh-channel.ts | 2 +- src/launcher.ts | 5 ++++- src/win-cmd.ts | 10 ++-------- tests/fix-679-windows-launch.test.ts | 8 ++++++++ tests/win-cmd-argv.test.ts | 16 ++++++++++++++-- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/dsh-channel.ts b/src/dsh-channel.ts index 9427c5cb0c..633b147b2a 100644 --- a/src/dsh-channel.ts +++ b/src/dsh-channel.ts @@ -435,7 +435,7 @@ export function resolveDshBinary( * launcher.ts import, which would cycle through plugin-install). */ function quoteWinToken(token: string): string { if (!/\s/.test(token) || token.includes('"')) return token; - return `"${token}"`; + return `"${token.replace(/(\\+)$/, "$1$1")}"`; } /** #679: which spawn form `dsh` needs. On Windows only .cmd/.bat shims and diff --git a/src/launcher.ts b/src/launcher.ts index ef02421b52..664f9c2950 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -6503,7 +6503,10 @@ async function abortLaunchOnBusyOverlay(handle: ProxyHandle, err: OverlayBusyErr * because such tokens never reach this path anymore. */ export function quoteWinToken(token: string): string { if (!/\s/.test(token) || token.includes('"')) return token; - return `"${token}"`; + // The receiving program reads 2n backslashes before a quote as n (MSVCRT / + // CommandLineToArgvW), so a trailing run is doubled or the closing quote halves it + // (an even run) or swallows itself (an odd one). + return `"${token.replace(/(\\+)$/, "$1$1")}"`; } /** #679: full command line for `comspec /d /s /c ` — tokens quoted as diff --git a/src/win-cmd.ts b/src/win-cmd.ts index ec3c68d88e..2dc7182aa3 100644 --- a/src/win-cmd.ts +++ b/src/win-cmd.ts @@ -18,7 +18,8 @@ /** Returns a human-readable reason why this token cannot cross the cmd.exe * line parser verbatim, or undefined when it can (the safe set): plain text, * whitespace-bearing text quoted whole, Unicode, semicolons/commas/equals, - * backslashes in even runs or outside quoted positions, and `!` (delayed + * backslashes anywhere (quoteWinToken doubles a trailing run inside quotes, so + * the receiving program reads it back verbatim), and `!` (delayed * expansion is opt-in via ^setlocal enabledelayedexpansion^ and standard npm * shims never enable it). The offending CONTENT is deliberately not part of * the return value — callers may log the reason but must not echo the token @@ -29,13 +30,6 @@ export function winCmdUnsafeToken(token: string): string | undefined { if (token.includes("%")) return "% (cmd.exe expands %VAR%)"; if (/[&|<>^()]/.test(token)) return "cmd metacharacters (& | < > ^ ( ))"; if (/[\r\n]/.test(token)) return "a line break"; - // Odd trailing-backslash run matters only when quoteWinToken will wrap the - // token in quotes (whitespace-bearing, quote-free): the final `\` then - // escapes the closing quote in MSVCRT parsing and swallows it. - if (/\s/.test(token)) { - const trail = /\\+$/.exec(token)?.[0].length ?? 0; - if (trail % 2 === 1) return "an odd number of trailing backslashes"; - } return undefined; } diff --git a/tests/fix-679-windows-launch.test.ts b/tests/fix-679-windows-launch.test.ts index 72f35c8712..29fa9a8300 100644 --- a/tests/fix-679-windows-launch.test.ts +++ b/tests/fix-679-windows-launch.test.ts @@ -35,6 +35,14 @@ test("quoteWinToken: whitespace-bearing tokens get wrapped in double quotes", () // embedded-quote tokens before they ever reach this function (see // win-cmd-argv.test.ts). The bare output here documents what the formatter // does with such input, NOT that it is a safe pass-through. +test("quoteWinToken: a trailing backslash run is doubled inside the quotes (MSVCRT 2n -> n)", () => { + assert.equal(quoteWinToken("C:\\My Dir\\"), '"C:\\My Dir\\\\"'); + assert.equal(quoteWinToken("a b\\\\"), '"a b\\\\\\\\"'); + // Backslashes that are not trailing, and space-free tokens, are untouched. + assert.equal(quoteWinToken("C:\\My Docs\\a.txt"), '"C:\\My Docs\\a.txt"'); + assert.equal(quoteWinToken("C:\\bin\\"), "C:\\bin\\"); +}); + test("quoteWinToken: embedded double quotes stay bare at the formatter level (rejected upstream since #2196)", () => { assert.equal(quoteWinToken('key="value"'), 'key="value"'); assert.equal(quoteWinToken('C:\\x "q" y'), 'C:\\x "q" y'); diff --git a/tests/win-cmd-argv.test.ts b/tests/win-cmd-argv.test.ts index 3578c64932..0ac2fc16d2 100644 --- a/tests/win-cmd-argv.test.ts +++ b/tests/win-cmd-argv.test.ts @@ -61,6 +61,8 @@ test("winCmdUnsafeToken: the safe set passes", () => { String.raw`C:\x\y\z`, "tailbs-nospace\\", String.raw`even trailing \\ with space`, + "odd trailing with space \\", + "C:\\My Projects\\", "!VAR! delayed expansion marker", "tab\there", "unicode ✓ 日本語 🎉", @@ -80,7 +82,6 @@ test("winCmdUnsafeToken: every #2196 corruption class is refused", () => { ["a^b", "cmd metacharacters (& | < > ^ ( ))"], ["(a)", "cmd metacharacters (& | < > ^ ( ))"], ["line\nbreak", "a line break"], - ["odd trailing \\", "an odd number of trailing backslashes"], ]; for (const [token, reason] of unsafe) { assert.equal(winCmdUnsafeToken(token), reason, `expected unsafe: ${JSON.stringify(token)}`); @@ -97,6 +98,13 @@ test("planClientSpawn win32: safe argv keeps the exact #679 plan (byte-identical assert.equal(p.windowsVerbatimArguments, true); }); +test("planClientSpawn win32: trailing backslashes in a quoted argument survive the comspec path", () => { + // MSVCRT reads 2n backslashes before a quote as n: an odd run used to be refused and an + // even one arrived halved. Doubling the run inside the quotes carries both through verbatim. + const p = planClientSpawn("C:\\npm\\claude.cmd", ["-p", "list C:\\My Projects\\", "ends in \\\\"], WIN_ENV, "win32"); + assert.equal(p.args[3], '"C:\\npm\\claude.cmd -p "list C:\\My Projects\\\\" "ends in \\\\\\\\""'); +}); + test("planClientSpawn win32: '!' and other non-metacharacters stay allowed on the comspec path", () => { const p = planClientSpawn("tool.cmd", ["!VAR!", "a;b,c=d"], WIN_ENV, "win32"); assert.equal(p.command, "C:\\Windows\\System32\\cmd.exe"); @@ -121,7 +129,6 @@ for (const [label, args] of [ ["empty argument", ["exec", ""]], ["%VAR%", ["exec", "literal %COMSPEC% marker"]], ["& metacharacter", ["exec", "hello&rem"]], - ["odd trailing backslash", ["exec", "odd tail \\"]], ] as const) { test(`planClientSpawn win32: refuses ${label} on the comspec path with an actionable error`, () => { assert.throws( @@ -178,6 +185,11 @@ test("planDshSpawn win32: safe profile args keep the #679 plan", () => { assert.equal(p.args[3], String.raw`"C:\tools\dsh.cmd plugin --profile "my profile" add billion-context"`); }); +test("planDshSpawn win32: a quoted token's trailing backslashes are doubled, not halved or refused", () => { + const p = planDshSpawn("C:\\tools\\dsh.cmd", ["plugin", "--profile", "C:\\My Profiles\\"], WIN_ENV, "win32"); + assert.equal(p.args[3], '"C:\\tools\\dsh.cmd plugin --profile "C:\\My Profiles\\\\""'); +}); + test("planDshSpawn win32: a metacharacter-laden profile name is refused, not mangled", () => { assert.throws( () => planDshSpawn(String.raw`C:\tools\dsh.cmd`, ["plugin", "--profile", "p&q", "add", "billion-context"], WIN_ENV, "win32"), From 4a08498725c65408646e03a9b5c1d43b50c01387 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 13 Nov 2026 02:59:57 +0800 Subject: [PATCH 47/78] test(win-cmd): pin the comspec trailing-backslash round-trip in CI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PR's exact-line tests pin the bytes quoteWinToken emits, but the property being fixed — that those bytes decode back to the token under the receiving program's CommandLineToArgvW rule — was only verified by a manual 200k-token probe. Add a committed round-trip (quoteWinToken -> splitWindowsCommandLine) so the lossless guarantee is testable in CI and a regression in either the doubling or the 2n->n halving is caught. Review of #6190 (small finding fixed on the spot). (cherry picked from commit d7f719b9e8cab77c2a1019924e30339743856315) --- tests/win-cmd-argv.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/win-cmd-argv.test.ts b/tests/win-cmd-argv.test.ts index 0ac2fc16d2..f27e528d97 100644 --- a/tests/win-cmd-argv.test.ts +++ b/tests/win-cmd-argv.test.ts @@ -13,6 +13,7 @@ import path from "node:path"; import { spawnSync } from "node:child_process"; import { planClientSpawn, + quoteWinToken, resolveClientCommand, resolveCodexOfficialJsEntry, resolveOnPath, @@ -20,6 +21,7 @@ import { type SpawnChild, type SpawnFn, } from "../src/launcher.ts"; +import { splitWindowsCommandLine } from "../src/claude-native-bootstrap.ts"; import { planDshSpawn } from "../src/dsh-channel.ts"; import { winCmdUnsafeToken } from "../src/win-cmd.ts"; import { rmrf } from "./tmp-rm.ts"; @@ -105,6 +107,28 @@ test("planClientSpawn win32: trailing backslashes in a quoted argument survive t assert.equal(p.args[3], '"C:\\npm\\claude.cmd -p "list C:\\My Projects\\\\" "ends in \\\\\\\\""'); }); +// The comspec leg's acceptance property, pinned in CI: a whitespace-bearing +// token quoted by quoteWinToken must parse back to itself under the receiving +// program's CommandLineToArgvW rule (splitWindowsCommandLine models it). The +// exact-line test above pins the bytes; this pins that those bytes decode back +// to the token — the lossless round-trip the trailing-backslash doubling buys. +test("comspec round-trip: quoteWinToken output parses back verbatim (MSVCRT 2n -> n)", () => { + const tokens = [ + "plain spaced", // no backslash + "C:\\My Docs\\a.txt", // interior backslashes only + "odd trailing \\", // 1 trailing (refused before this fix) + "even trailing \\\\", // 2 trailing (arrived halved before this fix) + "three trailing \\\\\\", // 3 trailing + ]; + for (const token of tokens) { + assert.deepEqual( + splitWindowsCommandLine(quoteWinToken(token)), + [token], + `round-trip lost: ${JSON.stringify(token)}`, + ); + } +}); + test("planClientSpawn win32: '!' and other non-metacharacters stay allowed on the comspec path", () => { const p = planClientSpawn("tool.cmd", ["!VAR!", "a;b,c=d"], WIN_ENV, "win32"); assert.equal(p.command, "C:\\Windows\\System32\\cmd.exe"); From 653df8767dff95c7e3a8ce26fc7866c33f37f366 Mon Sep 17 00:00:00 2001 From: vladkimo Date: Wed, 11 Nov 2026 18:58:49 +0300 Subject: [PATCH 48/78] fix(#5951): let the OMP host own truncation recovery, keep diagnostics local The reasoning-only replay predicate treated any open stream as pending answer content, so an OpenAI reasoning identity filter blocked the one-shot re-issue the pipe exists to spend. Only a non-reasoning open stream does that now. When the OMP host marks itself as the recovery owner, bili leaves the cut to the host instead of spending a second retry budget on the same stream. The marker is internal: consumed at the proxy and stripped before egress. Truncation diagnostics gained the sanitized read-error cause chain, upstream trace ids, and the replay predicate inputs, and they no longer travel in the client-visible SSE error frame. (cherry picked from commit a8de47cfc262c786582fbf44d87b7427b7bb368b) --- CHANGELOG.md | 3 + src/agent/omp-native.ts | 12 +- src/plugin.ts | 71 +++- src/server/headers.ts | 6 +- src/server/relay.ts | 27 +- src/stream-error.ts | 55 +++- src/util.ts | 5 + tests/claude-native.test.ts | 6 + ...sue2171-zero-byte-truncation-retry.test.ts | 41 +++ tests/issue2328-terminal-diag.test.ts | 305 +++++++----------- ...ssue5951-reasoning-only-cut-replay.test.ts | 152 +++++++-- tests/native-intercept-selfheal.test.ts | 24 ++ tests/omp-native.test.ts | 9 +- 13 files changed, 473 insertions(+), 243 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e538ab57a5..271767cc00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Fixed - **dsh 桌面副本自己更新自己时也跑 boot smoke(#2082 后续)**:#2091 的 boot smoke 门只接在 `refreshDshDesktopCopy` 上(别的 bili 进程替桌面副本刷新的那条路)。代理本身就从桌面副本运行时,`hostManagedInstall` 按 #1575 放行,走的是普通自更新路径——周期检查 / `bili update`(`checkForUpdate`)和 advisory 强制安装 / 面板安装(`forceInstallVersion`)——这两处调 `installViaTarball` 没带 `bootSmoke`,入口能解析但加载即抛错的包只过 `node --check` 就被换上去,正是这道门要挡的状态。现在两条路径在安装目录是 dsh 桌面副本时同样启用 boot smoke,失败回滚到旧副本;其他安装布局行为不变。 - **A compress call that folded nothing no longer rewrites every `` tag on the next request (follow-up to #5608-C/D, #5797).** `applyRanges` armed the deferred one-time ref/ledger alignment (`session.pendingLedgerAlign`) right after committing the kernel's returned state — before the `blocksCreated === 0` returns — so a refused compress (range too small, cannot anchor, already compressed) or a no-op armed it too. The next save then ran `restampRefsToLedger`, which raised `timestampsHighWaterRefSeq` to the highest ref unconditionally: on a session whose refs already sit on their ledger seats (`kept=N restamped=0 rehomed=0`) no number moved, yet every tag minted so far dropped below the mark and lost its `time=` attribute — the whole cached prefix re-billed after a call whose receipt says nothing was folded. Now the marker is armed only once a block was really created; a real fold arms it exactly as before. +- **OMP owns truncation recovery:** Native OMP requests carry an internal recovery-owner marker that is stripped before upstream egress; bili leaves truncation classification and retries to OMP instead of spending a second proxy retry budget. +- Upstream stream truncation errors expose only the retry code to clients; transport causes and upstream trace IDs stay in local proxy logs. +- **Reasoning-only stream recovery (#5951):** OpenAI reasoning identity filters no longer block the existing one-shot retry after an upstream EOF or read error. Pending answer filters, visible answer text, tool calls, and an exhausted retry budget still prevent replay. - **压缩树图:节点标题不再一闪即逝,点击压缩块能真正看到被压缩的消息内容**:此前树节点只有原生 SVG `` 悬停提示——选中节点或 5s 轮询重画时 DOM 一变提示就消失,实测「显示一下马上消失」;右侧 Block details 里块的消息行虽带消息编号(mNNNNN)但内容全是一句「消息已不在会话中」(宿主原生压缩把消息从上下文剪掉后,字节无处可取)。现在:① 悬停提示改为页面内常驻浮层(.g-tip)——跟随指针、显示完整块号/tier/消息范围/完整主题/tokens/消息数,选中节点或轮询重画都不再打断,离开图形区才隐藏;悬停时节点卡片上的主题行同时原地扩宽(~20 字 → 20 字上限的完整截断,离开复原);② 5s 轮询只回传瘦节点(无主题/摘要/tokens),重画前先与详情富块按 blockId 合并——节点标题、浮层元信息、消息数不再在每次 tick 后退化成光秃秃的块号;③ 服务端新增**冻结快照水合**:压缩块落盘时已把原始消息逐条存进 blockContents(`[role]`/`[role • tool]` 头 + 原文,pi 宿主 user 消息还带 mNNNNN 标记行)——水合按**标记锚定**对齐:pi 宿主打进 user 消息正文的 mNNNNN 标记是身份戳,每个标记用两条独立证据之一锚死——**恒等锚**(标记号=该位当前 ref,从未被重编号)或**冻结 span 算术锚**(标记的冻结编号 − 块冻结 span 起点 = 快照位序;span 与标记同批写入,冷加载 restamp(#5196 parked 段换新号)重编了整块 ref 也照样成立——实测 b27 全块 127 条因此恢复,旧规则因标记号已不在 ref 表里整块拒水合);锚点之间的段仅在条数平衡时按位 zip。标记既非恒等也非算术锚(冻结上下文混入外来消息/span 无法证明)、锚点乱序、无锚非叶块(快照 span 的是 effective 超集)一律拒绝,未证明的位置保留诚实的 missing 标记——宁可不显示,不冒错位张冠李戴的风险(预览 + 点击展开取原文,`/message?ref=` 同享该层)。实测大会话(253 块/9771 个直接消息引用):7789 条(80%)恢复出真实预览与原文,其余保持 missing。回归:tests/web-blocks-tree.test.ts 冻结快照水合 7 用例(按位水合 / 条数不符拒 / 标记移位旧锚 / 宿主剪块中消息的锚定恢复 / restamp 整块恢复 / 算术锚优先于巧合 ref 碰撞 / 无锚标记整块拒(含 click-to-open 双向断言)),源码级断言更新为合并后重画(renderCollapSvg(merged)。 - **交接文档「Compressed block summaries」附录不再被吞进最后一条消息卡片**:客户端 handoff 渲染器只认 `### user/assistant/tool` 三种角色分隔线(#2065 语义,正文内其他 `###` 标题是内容),export 端 #845 追加在对话之后的 `## Compressed block summaries` 整节(长会话可达 80+ 个 `### Block bN` 条目、数百行)因此全部落进最后一个 assistant 卡片,看起来像「压缩消息被追加成一条巨型消息」。现在客户端把 Conversation 之后的 `## ` 级标题识别为独立分节:`### Block bN — topic` 逐条渲染为小块卡片(mono 块号 + 主题截断标题 + tier/token 元行 + Markdown 正文),角色卡片只保留真实对话。回归:tests/web-handoff-roles.test.ts 新增附录分节用例(角色卡片数不膨胀、条目内容保真、#2065 不变量不受影响)。 - **会话历史的「条数」维度与超大未压缩请求:按真实维度拦下,判死信息点名超重字段(#5000 #5794 #5885)**:#5000 的 26,903 条消息会话(55.2MiB)穿过了**所有**字节/token 闸门、在上游死于消息条数上限——因为条数这个维度从来没有任何闸(它无法被任何字节/token 估算表达:每条消息还各带 ~69B 的 m-ref 标记,26,903 条 ≈ 1.8MB 纯标记)。现在新增按上游配置的**条数闸**(`network.maxMessages` / `BILI_MAX_MESSAGES`,默认 0=关闭——不存在全局「通用上限」,条数上限因上游而异):超限直接 413,信息点名 COUNT 维度并给出可执行指引(重启会话或回退到更早的时点;折叠在一轮 preflight 内关不掉 ~20K 条的缺口,所以是纯判死、不做条数折叠),且在 token 触发器**之前**运行——token 达标但条数超标的体(正是 #5000 的形状)也只有这道闸看得见。同时所有超窗判死信息(#5932 的三道计量闸)现在**点名超重的是哪个字段**——按 #554 计量器同源分桶(messages / tools[] / system / other),不再只给一个总数:#5000 的形状会显示 `tools[] ~X`,巨大粘贴/工具结果会显示 `messages ~X`,运维一眼知道该删什么。另新增 **32MiB 解码摄入成本带**(`network.ingestBodyBytes` / `BILI_INGEST_BODY_BYTES`):#5794 的 51.2MiB/29,619 条请求每轮独占事件循环 5.5–7.2s(本机 CPU 54.6% → 747s FATAL 自重启、全员缓存冷),成本带在 JSON 解析扫掠**之前**按解码后字节 413(stage=ingest)——任何可解析的**文本为主**的 32MiB+ 体都已是 ≥~8M token,超出所有真实模型窗口,放行必然烧钱再死。带图片的体在所有车道豁免(base64 像素按设计不进 token 计量——#496/#1800/#857「图片单独计费、绝不在原始字节上判死」;#3909 的 ~48MiB 图片密集 chat 会话与 #3701 的 ~38.5MiB WS 截图体都是受支持工作负载,由槽位预算/收件上限/车道自身的预发送字节预算+丢弃逻辑治理;Responses 车道无论如何整体豁免);并发槽位预算与 5GiB 收件上限继续独立生效。回归:tests/issue5000-count-gate(3 e2e:token 达标+条数超标→413 零上游 / 无 model 路径 / 条数达标照常转发)、tests/issue5794-ingest-cap(3 e2e:超带文本→413 stage ingest / 带内照常 / 超带带图不拦)、tests/side-request-isolation 分桶计量单测;既有 332 用例(含 #3701/#857/#496/#4079/#4185/#2490 全部回归面)全绿。 diff --git a/src/agent/omp-native.ts b/src/agent/omp-native.ts index a908cdfe01..66022f7288 100644 --- a/src/agent/omp-native.ts +++ b/src/agent/omp-native.ts @@ -30,6 +30,7 @@ import { clearIncrementalProbe, createPrepareDirectSend, hostMsgIdHintFor, INCRE import * as pfChannel from "./pf-channel.js"; import { fetchProxyVersion, waitForProxyVersion } from "./shared.js"; import { logHostEvent } from "./host-log.js"; +import { BILI_HOST_RECOVERY_OWNER_HEADER, BILI_HOST_RECOVERY_OWNER_OMP } from "../util.js"; /** Decides whether the native bootstrap should run in this process. */ export function shouldBootstrapNativeOmp(env: NodeJS.ProcessEnv): boolean { @@ -68,7 +69,11 @@ function errMessage(err: unknown): string { return err instanceof Error ? err.message : String(err); } -const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; +const state: NativeInterceptState = { + origin: undefined, + ready: Promise.resolve(undefined), + headersFor: () => ({ [BILI_HOST_RECOVERY_OWNER_HEADER]: BILI_HOST_RECOVERY_OWNER_OMP }), +}; // #3248 phase 2: incremental-turn divert for omp. omp never emits header // events, so the ONLY identity signal at the fetch layer is the @@ -454,6 +459,11 @@ export function _stateRespawnForTest(): (() => Promise<string | undefined>) | un return state.respawn; } +/** Test hook: inspect the owner marker installed by the native OMP host. */ +export function _headersForTest(url: string): Record<string, string> | undefined { + return state.headersFor?.(url); +} + /** Test hook (#1365): record routed-channel evidence without the fetch patch. */ export function _noteRoutedForTest(url: string): void { noteRoutedOrigin(state, url); diff --git a/src/plugin.ts b/src/plugin.ts index 6f6a9a33aa..9e1d913741 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -36,6 +36,9 @@ import { captureHandoffJson, HANDOFF_REPLY_MAX_BYTES } from "./server/handoff-re export interface UpstreamMeta { status?: number; contentType?: string; + requestId?: string; + opencodeLogId?: string; + cfRay?: string; } /** #2328 Q4: does the dangling partial event left at a cut carry the opening @@ -2779,6 +2782,9 @@ export async function pipePluginChatWithStrip( // default); hosts that need the legacy completed-turn shape opt back in with // streamErrorShape:"completion". streamErrorShape?: "protocol" | "completion", + // #5951: the requesting host already classifies this cut and retries on its + // own budget — the proxy must not spend a second one. + hostOwnsTruncationRecovery = false, ): Promise<void> { let reader = stream.getReader(); let decoder = new TextDecoder("utf-8"); @@ -3126,7 +3132,11 @@ export async function pipePluginChatWithStrip( // it too, so the two content-only counters are what separate an answer from // a thinking fragment. if (sawToolUse || blocksForwarded > 0 || visibleTextChars > 0 || releasedMarkupChars > 0 || - sawStrippedEcho || answerProse.trim().length > 0 || streams.size > 0) return false; + sawStrippedEcho || answerProse.trim().length > 0) return false; + // OpenAI reasoning identity filters are not pending answer content. + for (const { field } of streams.values()) { + if (protocol !== "openai" || (field !== "reasoning_content" && field !== "reasoning")) return false; + } if (res.destroyed || res.writableEnded) return false; let next: ReadableStream<Uint8Array> | null = null; try { @@ -3175,10 +3185,27 @@ export async function pipePluginChatWithStrip( // #5951: the read error itself, so a cut that was not replayed names its // cause instead of only its classification. ...(cause === "read-error" && readError !== undefined ? { readError: describeReadError(readError) } : {}), + ...(cause === "read-error" && readError !== undefined ? { readErrorCause: describeReadErrorCause(readError) } : {}), retryZeroByteCutSpent: truncationRetried, retryDegenerateSpent: degenerateRetried, + // #5951: the replay predicate's inputs, so a refused cut names the clause + // that refused it instead of only the read error. + replay: { + replayAvailable: refetch !== undefined, + sawThinking, + sawToolUse, + blocksForwarded, + openStreams: streams.size, + forwardedAny, + releasedMarkupChars, + proseChars: proseAcc.length, + answerProseChars: answerProse.trim().length, + }, ...(upstreamMeta?.status !== undefined ? { upstreamStatus: upstreamMeta.status } : {}), ...(upstreamMeta?.contentType !== undefined ? { upstreamContentType: upstreamMeta.contentType } : {}), + ...(safeDiagnosticId(upstreamMeta?.requestId) ? { upstreamRequestId: safeDiagnosticId(upstreamMeta?.requestId) } : {}), + ...(safeDiagnosticId(upstreamMeta?.opencodeLogId) ? { upstreamLogId: safeDiagnosticId(upstreamMeta?.opencodeLogId) } : {}), + ...(safeDiagnosticId(upstreamMeta?.cfRay) ? { upstreamCfRay: safeDiagnosticId(upstreamMeta?.cfRay) } : {}), }); /** While the retry stream feeds the client, the message the FIRST attempt * opened is still open: nothing may re-open it. Returns true when the event @@ -3818,14 +3845,14 @@ export async function pipePluginChatWithStrip( try { step = await reader.read(); } catch (e) { - if (await retryReasoningOnlyCut(e)) continue; + if (!hostOwnsTruncationRecovery && await retryReasoningOnlyCut(e)) continue; throw e; } const { done, value } = step; if (done) { // #2171: an EOF with nothing client-visible yet is safely // re-issuable — try the one-shot retry before giving up. - if (!sawTerminal && !res.destroyed && !res.writableEnded && ((await retryZeroByteCut()) || (await retryReasoningOnlyCut()))) continue; + if (!hostOwnsTruncationRecovery && !sawTerminal && !res.destroyed && !res.writableEnded && ((await retryZeroByteCut()) || (await retryReasoningOnlyCut()))) continue; // #2323: a CRLF/lone-CR whose final byte arrived last is held back // by the streaming normalizer; resolve it and re-drive the completed // event through the same path below before deciding truncation. @@ -4182,6 +4209,25 @@ function describeReadError(error: unknown): string { return `${name}: ${error.message}`.replace(/[\r\n]+/g, " ").slice(0, 200); } +/** Retain only bounded, structured cause identity; never serialize upstream text. */ +function describeReadErrorCause(error: unknown): Array<{ name: string; code?: string }> | undefined { + if (!(error instanceof Error)) return undefined; + const chain: Array<{ name: string; code?: string }> = []; + let cause: unknown = error.cause; + for (let depth = 0; depth < 3 && cause instanceof Error; depth++) { + const name = /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/.test(cause.name) ? cause.name : "Error"; + const codeValue = (cause as Error & { code?: unknown }).code; + const code = typeof codeValue === "string" && /^[A-Za-z0-9_]{1,64}$/.test(codeValue) ? codeValue : undefined; + chain.push({ name, ...(code ? { code } : {}) }); + cause = cause.cause; + } + return chain.length > 0 ? chain : undefined; +} + +function safeDiagnosticId(value: string | undefined): string | undefined { + return value !== undefined && value.length <= 128 && /^[A-Za-z0-9][A-Za-z0-9._:-]*$/.test(value) ? value : undefined; +} + function hadTextOtherThanTextFields(choices: unknown): boolean { if (!Array.isArray(choices)) return true; for (const c of choices) { @@ -4303,6 +4349,7 @@ export async function pipePluginResponsesWithStrip( // #2328 Q2 — see pipePluginChatWithStrip. upstreamMeta?: UpstreamMeta, reply?: HandoffReply, + hostOwnsTruncationRecovery = false, ): Promise<void> { let reader = stream.getReader(); let decoder = new TextDecoder("utf-8"); @@ -4610,11 +4657,27 @@ export async function pipePluginResponsesWithStrip( // #5951: the read error itself, so a cut that was not replayed names its // cause instead of only its classification. ...(cause === "read-error" && readError !== undefined ? { readError: describeReadError(readError) } : {}), + ...(cause === "read-error" && readError !== undefined ? { readErrorCause: describeReadErrorCause(readError) } : {}), retryZeroByteCutSpent: truncationRetried, retryDegenerateSpent: degenerateRetried, + // #5951: see the chat-pipe twin — the responses lane has no reasoning-only + // replay, so `replayAvailable` is the field that says so on a cut. + replay: { + replayAvailable: refetch !== undefined, + sawReasoning, + sawFunctionCall, + openStreams: argStreams.size, + forwardedAny, + proseChars: proseAcc.length, + fastPathChars, + heldVisibleChars, + }, heldEvents: heldEvents.length, ...(upstreamMeta?.status !== undefined ? { upstreamStatus: upstreamMeta.status } : {}), ...(upstreamMeta?.contentType !== undefined ? { upstreamContentType: upstreamMeta.contentType } : {}), + ...(safeDiagnosticId(upstreamMeta?.requestId) ? { upstreamRequestId: safeDiagnosticId(upstreamMeta?.requestId) } : {}), + ...(safeDiagnosticId(upstreamMeta?.opencodeLogId) ? { upstreamLogId: safeDiagnosticId(upstreamMeta?.opencodeLogId) } : {}), + ...(safeDiagnosticId(upstreamMeta?.cfRay) ? { upstreamCfRay: safeDiagnosticId(upstreamMeta?.cfRay) } : {}), }); /** Every id the retry carries is rewritten onto the first attempt's, so the * client's assembled item stays the one it already holds. */ @@ -4756,7 +4819,7 @@ export async function pipePluginResponsesWithStrip( if (done) { // #2171: an EOF with nothing client-visible yet is safely // re-issuable — try the one-shot retry before giving up. - if (!sawTerminal && !res.destroyed && !res.writableEnded && (await retryZeroByteCut())) continue; + if (!hostOwnsTruncationRecovery && !sawTerminal && !res.destroyed && !res.writableEnded && (await retryZeroByteCut())) continue; // #2323: a CRLF/lone-CR whose final byte arrived last is held back // by the streaming normalizer; resolve it and re-drive the completed // event through the same path below before deciding truncation. diff --git a/src/server/headers.ts b/src/server/headers.ts index e4c25e2dab..dc896a81c0 100644 --- a/src/server/headers.ts +++ b/src/server/headers.ts @@ -1,3 +1,5 @@ +import { BILI_HOST_RECOVERY_OWNER_HEADER } from "../util.js"; + /** Session ids are client-provided verbatim (#286) — sanitize before using * one in a debug-dump FILENAME so a hostile value cannot escape the dir. */ export function safeSessionId(id: string | undefined): string { @@ -26,6 +28,8 @@ export const UPSTREAM_HOP_HEADERS = new Set([ // #1117: bili-internal passthrough marker (native fetch patch → proxy); // never meaningful to a real upstream. "x-bili-passthrough", + // OMP host owns recovery; consume this marker before provider egress. + BILI_HOST_RECOVERY_OWNER_HEADER, // #5418: watermark fastpath handshake — client→proxy watermark claim and // proxy→client measured total. Pure billing-plane bookkeeping; the A/B // golden (tests/issue5403-watermark-fastpath.test.ts) pins that neither @@ -103,7 +107,7 @@ export function buildForwardHeaders(headers: Record<string, string>): Record<str for (const [k, v] of Object.entries(headers)) { // expect: undici fetch rejects it with UND_ERR_NOT_SUPPORTED — dropped // here so no egress path can forward it (#5369). - if (k.toLowerCase() === "content-length" || k.toLowerCase() === "host" || k.toLowerCase() === "expect") continue; + if (k.toLowerCase() === "content-length" || k.toLowerCase() === "host" || k.toLowerCase() === "expect" || k.toLowerCase() === BILI_HOST_RECOVERY_OWNER_HEADER) continue; out[k] = v; } out["content-type"] = "application/json"; diff --git a/src/server/relay.ts b/src/server/relay.ts index 65ca9c80e0..48e2a0edf7 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -64,7 +64,7 @@ import { clearUpstreamAlertsForHost, noteCircuitFailFastLog, recordUpstreamAlert import { describeUpstreamFailure, formatUpstreamError, proxyDispatcher, recordUpstreamConnection } from "../upstream-proxy.js"; import { markExpectedFailFast, withUpstreamFailure, type UpstreamFailureKind } from "../upstream-fail.js"; import { noteUpstreamThrottle429, noteUpstreamThrottleResolved, releaseUpstreamThrottleProbe, requestModelFromBody, throttle429ModelScope, type ThrottleLocalReject, upstreamThrottleGate, upstreamThrottlePeekReject } from "../upstream-throttle.js"; -import { applyEstimateCalibration, currentCalibrationFactor, inspectContextOverflow, isMarkerlessOverflowReject, MIN_ASSUMED_WINDOW_TOKENS, normalizeUpstreamOrigin, usageOutputTotal, usageTotals, type ContextOverflowInfo, type WireProtocol } from "../util.js"; +import { applyEstimateCalibration, BILI_HOST_RECOVERY_OWNER_HEADER, BILI_HOST_RECOVERY_OWNER_OMP, currentCalibrationFactor, inspectContextOverflow, isMarkerlessOverflowReject, MIN_ASSUMED_WINDOW_TOKENS, normalizeUpstreamOrigin, usageOutputTotal, usageTotals, type ContextOverflowInfo, type WireProtocol } from "../util.js"; import { billOutputUsage } from "../billing.js"; import { DUMP_PLAINTEXT_BANNER } from "../dump-banner.js"; import { recordPckAblation } from "../pck-probe.js"; @@ -350,8 +350,8 @@ async function forwardCaptured( releaseRequestInput?: () => void, ): Promise<void> { const forwardStartedAt = Date.now(); - const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session) : undefined; - // E2: a codex native-compaction request intercepted in prepare() carries a + const hostOwnsTruncationRecovery = req.headers[BILI_HOST_RECOVERY_OWNER_HEADER] === BILI_HOST_RECOVERY_OWNER_OMP; + const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session, () => !res.destroyed) : undefined; // E2: a codex native-compaction request intercepted in prepare() carries a // forged success response — serve it without contacting upstream. if (prepared?.codexForge) { onWireBody?.(); releaseRequestInput?.(); log("info", `[${prepared.session.id}] codex compact served locally (${prepared.codexForge.kind}); upstream not contacted`); @@ -2285,7 +2285,13 @@ async function forwardCaptured( // first attempt's status/content-type (a resolveFakeCompletion // re-send is not reflected; the pipes' own diag says which // retry budget was spent instead). - const upstreamMeta: UpstreamMeta = { status: upstream.status, contentType: upstream.headers.get("content-type") ?? undefined }; + const upstreamMeta: UpstreamMeta = { + status: upstream.status, + contentType: upstream.headers.get("content-type") ?? undefined, + requestId: upstream.headers.get("x-request-id") ?? upstream.headers.get("request-id") ?? undefined, + opencodeLogId: upstream.headers.get("x-opencode-log-id") ?? undefined, + cfRay: upstream.headers.get("cf-ray") ?? undefined, + }; // #2346: intrinsic runaway-enumeration terminator for the plugin-streamed // body (the incident lane). Applied after any fake-completion buffering so // both the live and replayed paths are guarded; byte-verbatim otherwise. @@ -2330,6 +2336,7 @@ async function forwardCaptured( wireBodyText, upstreamMeta, reply, + hostOwnsTruncationRecovery, ); } else { // #732/#821: the plugin pipe re-issues the agent's own body @@ -2360,6 +2367,7 @@ async function forwardCaptured( upstreamMeta, reply, opts.streamErrorShape, + hostOwnsTruncationRecovery, ); } } else { @@ -2464,7 +2472,13 @@ async function forwardCaptured( prepared.processedMessages.length > 0; if (!useRewriter || prepared === null) { // #2328: pipes cite the first upstream response's HTTP identity. - const upstreamMeta: UpstreamMeta = { status: upstream.status, contentType: upstream.headers.get("content-type") ?? undefined }; + const upstreamMeta: UpstreamMeta = { + status: upstream.status, + contentType: upstream.headers.get("content-type") ?? undefined, + requestId: upstream.headers.get("x-request-id") ?? upstream.headers.get("request-id") ?? undefined, + opencodeLogId: upstream.headers.get("x-opencode-log-id") ?? undefined, + cfRay: upstream.headers.get("cf-ray") ?? undefined, + }; if (prepared && prepared.resetAfterSuccess) { const [toClient, toObserve] = responseBody.tee(); const observed = observeResponsesTerminalState(toObserve, prepared.stream); @@ -2497,6 +2511,7 @@ async function forwardCaptured( undefined, upstreamMeta, reply, + hostOwnsTruncationRecovery, ); } else { await pipeThrough(toClient, res); @@ -2549,6 +2564,7 @@ async function forwardCaptured( undefined, upstreamMeta, reply, + hostOwnsTruncationRecovery, ); } else { await pipePluginChatWithStrip( @@ -2576,6 +2592,7 @@ async function forwardCaptured( upstreamMeta, reply, opts.streamErrorShape, + hostOwnsTruncationRecovery, ); } } else if ( diff --git a/src/stream-error.ts b/src/stream-error.ts index 74b25f7366..b6305194c8 100644 --- a/src/stream-error.ts +++ b/src/stream-error.ts @@ -173,11 +173,11 @@ export function emitStreamError(res: http.ServerResponse, protocol: Protocol, me * (it just does not reach the wire). * Never throws. */ -/** #2328 Q2: termination diagnostics carried on the in-band truncation - * error frame (`meta`) and in the log line, so "the upstream never sent a +/** #2328 Q2: termination diagnostics. They ride the proxy log line only and + * never a client-visible SSE frame (#5951), so "the upstream never sent a * terminal event" (#721's no-terminal-seen) and "a terminal was arriving but - * its bytes never completed / were unrecognizable" can be told apart from - * the client side (#2347 forensics). Every field is plain-JSON safe. */ + * its bytes never completed / were unrecognizable" can be told apart + * (#2347 forensics). Every field is plain-JSON safe. */ export interface TruncationDiag { /** Which pipe path fired: clean EOF without a terminal, or a read error. */ cause: "eof" | "read-error"; @@ -217,21 +217,46 @@ export interface TruncationDiag { upstreamStatus?: number; /** #5951: the failed read itself, named, on a cut the pipe could not replay. */ readError?: string; + /** Sanitized `Error.cause` chain; names and codes only, never error text. */ + readErrorCause?: Array<{ name: string; code?: string }>; + /** Safe upstream trace identifiers captured from response headers. */ + upstreamRequestId?: string; + upstreamLogId?: string; + upstreamCfRay?: string; + /** #5951: the replay predicate's own inputs, on both pipe lanes. */ + replay?: ReplayRefusalDiag; upstreamContentType?: string; } +/** #5951: why a cut was not replayed, counter by counter. The predicate itself + * lives in plugin.ts and is silent when it declines, so a refused cut names + * here the exact value that refused it. Absent `replay` = no replay door on + * this pipe at all. */ +interface ReplayRefusalDiag { + /** The pipe held a refetch, i.e. a replay was possible on this cut. */ + replayAvailable: boolean; + sawThinking?: boolean; + sawReasoning?: boolean; + sawToolUse?: boolean; + sawFunctionCall?: boolean; + blocksForwarded?: number; + openStreams?: number; + forwardedAny?: boolean; + releasedMarkupChars?: number; + proseChars?: number; + answerProseChars?: number; + fastPathChars?: number; + heldVisibleChars?: number; +} + export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Protocol, finished: boolean, log?: (msg: string) => void, diag?: TruncationDiag, transparent = false): void { const message = "upstream stream ended before a completion event; this turn may be incomplete"; const action = finished ? protocol === "google" ? "finish reason already delivered, stream complete" : "finish reason seen, synthesizing missing terminal byte" : transparent ? "passing through raw (host owns truncation handling, #2563)" : "emitting in-band error"; - // #2328: the diag blob rides the log line too — one grep for - // `upstream stream truncated` pulls the full classification + counters - // without needing the client-side error frame. + // Diagnostics stay in the proxy's local log. Never attach upstream IDs or + // transport details to client-visible SSE error metadata. log?.(`[acp-proxy: upstream stream truncated (${protocol}) — ${action}${diag ? `; classification=${diag.classification} cause=${diag.cause}; diag=${JSON.stringify(diag)}` : ""}]`); - // #2328: attach the diagnostics as `meta` on the in-band error object. - // Wire-shape change (new field on an error-only frame) — human-merge item. - const metaField = diag === undefined ? {} : { meta: diag }; try { if (transparent && !finished) { // #2563: host-owned handling — deliver exactly what arrived @@ -241,25 +266,25 @@ export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Proto if (finished) { safeWrite(res, "data: [DONE]\n\n"); } else { - safeWrite(res, `data: ${JSON.stringify({ error: { type: "server_error", code: "upstream_stream_truncated", message, ...metaField } })}\n\ndata: [DONE]\n\n`); + safeWrite(res, `data: ${JSON.stringify({ error: { type: "server_error", code: "upstream_stream_truncated", message } })}\n\ndata: [DONE]\n\n`); } } else if (protocol === "responses") { - safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", code: "upstream_stream_truncated", message, ...metaField })}\n\n`); + safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", code: "upstream_stream_truncated", message })}\n\n`); } else if (protocol === "google") { // The finishReason chunk IS Gemini's stream terminator: when it was // delivered, the client is already done and an extra terminal // object would read as a second answer. Only the mid-flight cut // needs the error frame (503/UNAVAILABLE — an upstream-cut stream). - if (!finished) safeWrite(res, `data: ${JSON.stringify({ error: { code: 503, message, status: "UNAVAILABLE", ...metaField } })}\n\n`); + if (!finished) safeWrite(res, `data: ${JSON.stringify({ error: { code: 503, message, status: "UNAVAILABLE" } })}\n\n`); } else if (protocol === "commandcode") { // The finish event IS this wire's terminator: when delivered there // is nothing to synthesize; only a mid-flight cut gets an error line. - if (!finished) safeWrite(res, `${JSON.stringify({ type: "error", error: "upstream_stream_truncated", message })}\n`); + if (!finished) safeWrite(res, `${JSON.stringify({ type: "error", error: "upstream_stream_truncated", message })}\n\n`); } else { if (finished) { safeWrite(res, `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`); } else { - safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "server_error", code: "upstream_stream_truncated", message, ...metaField } })}\n\n`); + safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "server_error", code: "upstream_stream_truncated", message } })}\n\n`); } } } catch { diff --git a/src/util.ts b/src/util.ts index 09f8d705cb..9f05351d4c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -17,6 +17,11 @@ export const BILI_PLUGIN_BYPASS_HEADER = "x-bili-plugin-bypass"; // mirroring what a direct send without the overlay would have been. export const BILI_PASSTHROUGH_HEADER = "x-bili-passthrough"; +// Internal request marker: OMP owns stream truncation recovery for its native +// fetch calls; the proxy consumes this header and strips it before upstream egress. +export const BILI_HOST_RECOVERY_OWNER_HEADER = "x-bili-host-recovery-owner"; +export const BILI_HOST_RECOVERY_OWNER_OMP = "omp"; + // #4079: the MINIMUM window both egress oversize guards size against. A // configured or declared window can be wrong SMALL — a stale // modelContextLimit, a registry miss, a model-catalog fallback (#3993: diff --git a/tests/claude-native.test.ts b/tests/claude-native.test.ts index a2cac4cd8b..1ac4d6b31c 100644 --- a/tests/claude-native.test.ts +++ b/tests/claude-native.test.ts @@ -405,6 +405,12 @@ test("UPSTREAM_HOP_HEADERS includes expect to prevent undici UND_ERR_NOT_SUPPORT assert.ok(UPSTREAM_HOP_HEADERS.has("expect")); }); +test("OMP recovery ownership marker is stripped before provider egress", () => { + assert.ok(UPSTREAM_HOP_HEADERS.has("x-bili-host-recovery-owner")); + const out = buildForwardHeaders({ "x-bili-host-recovery-owner": "omp", "content-type": "application/json" }); + assert.equal(out["x-bili-host-recovery-owner"], undefined); +}); + test("buildForwardHeaders drops expect so undici fetch does not throw UND_ERR_NOT_SUPPORTED (#5369)", async () => { const server = http.createServer((req, res) => { res.writeHead(200, { "content-type": "application/json" }); res.end("{}"); }); await new Promise<void>((resolve) => { server.listen(0, "127.0.0.1", resolve); }); diff --git a/tests/issue2171-zero-byte-truncation-retry.test.ts b/tests/issue2171-zero-byte-truncation-retry.test.ts index 89023135f7..b78d98aa11 100644 --- a/tests/issue2171-zero-byte-truncation-retry.test.ts +++ b/tests/issue2171-zero-byte-truncation-retry.test.ts @@ -55,6 +55,17 @@ function streamOf(events: string[]): ReadableStream<Uint8Array> { }); } +function erroredStream(events: string[]): ReadableStream<Uint8Array> { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream<Uint8Array>({ + pull(controller) { + if (i < events.length) controller.enqueue(enc.encode(events[i++])); + else controller.error(new Error("simulated upstream read failure")); + }, + }); +} + function chatChunk(delta: Record<string, unknown>, extra: Record<string, unknown> = {}): string { return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: null }], ...extra })}\n\n`; } @@ -220,6 +231,36 @@ test("chat pipe: a comment-only upstream stream (keep-alive pings, zero data fra assert.ok(!out.join("").includes(ERROR_SENTINEL)); }); +test("OMP-owned read error does not spend the proxy reasoning-only retry", async () => { + const out: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { refetches++; return Promise.resolve(streamOf([])); }; + await pipePluginChatWithStrip( + erroredStream([chatChunk({ reasoning_content: "private reasoning" })]), + makeRes(out), "openai", makeSession(), undefined, refetch, "http://up", undefined, undefined, undefined, undefined, undefined, true, + ); + assert.equal(refetches, 0); + assert.ok(out.join("").includes(ERROR_SENTINEL)); +}); + +test("OMP-owned truncation: chat pipe does not spend a proxy retry and returns the in-band error", async () => { + const out: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { refetches++; return Promise.resolve(streamOf([])); }; + await pipePluginChatWithStrip(streamOf([]), makeRes(out), "openai", makeSession(), undefined, refetch, "http://up", undefined, undefined, undefined, undefined, undefined, true); + assert.equal(refetches, 0); + assert.ok(out.join("").includes(ERROR_SENTINEL)); +}); + +test("responses pipe: OMP-owned truncation does not spend a proxy retry and returns the in-band error", async () => { + const out: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { refetches++; return Promise.resolve(streamOf([])); }; + await pipePluginResponsesWithStrip(streamOf([]), makeRes(out), makeSession(), undefined, refetch, "http://up", undefined, undefined, undefined, undefined, true); + assert.equal(refetches, 0); + assert.ok(out.join("").includes(ERROR_SENTINEL)); +}); + test("responses pipe: a comment-only upstream stream (keep-alive pings, zero data frames) still retries (#2171)", async () => { const out: string[] = []; let refetches = 0; diff --git a/tests/issue2328-terminal-diag.test.ts b/tests/issue2328-terminal-diag.test.ts index 458caec0f6..6bfdcbdfb9 100644 --- a/tests/issue2328-terminal-diag.test.ts +++ b/tests/issue2328-terminal-diag.test.ts @@ -1,245 +1,160 @@ -import { test } from "node:test"; import assert from "node:assert/strict"; +import { test } from "node:test"; +import type { ServerResponse } from "node:http"; import { pipePluginChatWithStrip, pipePluginResponsesWithStrip } from "../src/plugin.ts"; -// #2328 Q2/Q4: the plugin-lane strip pipes now attach termination -// diagnostics (meta) to the in-band upstream_stream_truncated error frame and -// to the log line, and classify the cut: -// - no-terminal-seen: EOF/read-error with no terminal bytes in flight. -// - terminal-bytes-unrecognized: the dangling partial event at the cut -// carries the opening bytes of the wire's own terminal ([DONE], -// message_stop, finishReason chunk, response.completed family). -// This is what makes "the upstream never finished the turn" distinguishable -// from "the terminal arrived but its bytes never completed" (#2347 forensics) -// without needing a raw-SSE dump. - -function makeRes(chunks: string[]) { +function makeRes(writes: string[]): ServerResponse { return { - writes: chunks, - write(b: Buffer | string) { - chunks.push(typeof b === "string" ? b : b.toString("utf8")); + write(chunk: Buffer | string) { + writes.push(typeof chunk === "string" ? chunk : chunk.toString("utf8")); return true; }, - end(b?: Buffer | string) { - if (b !== undefined) chunks.push(typeof b === "string" ? b : b.toString("utf8")); + end(chunk?: Buffer | string) { + if (chunk !== undefined) writes.push(typeof chunk === "string" ? chunk : chunk.toString("utf8")); }, once() {}, destroyed: false, writableEnded: false, - } as unknown as import("node:http").ServerResponse; + } as unknown as ServerResponse; } -function streamOf(events: string[], then?: "error"): ReadableStream<Uint8Array> { - const enc = new TextEncoder(); - let i = 0; +function streamOf(events: string[], failure?: Error): ReadableStream<Uint8Array> { + const encoder = new TextEncoder(); + let index = 0; return new ReadableStream<Uint8Array>({ pull(controller) { - if (i < events.length) { - controller.enqueue(enc.encode(events[i])); - i += 1; - } else if (then === "error") { - controller.error(new Error("upstream socket reset mid-stream")); - } else { - controller.close(); - } + if (index < events.length) controller.enqueue(encoder.encode(events[index++]!)); + else if (failure) controller.error(failure); + else controller.close(); }, }); } -/** Pull the first data-frame JSON that carries the truncation code. */ -function truncationMeta(writes: string[], at: "top" | "nested"): Record<string, unknown> | undefined { - for (const w of writes) { - for (const frame of w.split("\n\n")) { - const m = frame.match(/^(?:event: [^\n]+\n)?data: (.*)$/s); - if (!m) continue; - try { - const ev = JSON.parse(m[1]!) as Record<string, unknown>; - if (at === "top" && ev["code"] === "upstream_stream_truncated") return ev["meta"] as Record<string, unknown>; - if (at === "nested") { - const err = ev["error"] as Record<string, unknown> | undefined; - // google's error object carries a numeric code (503) + - // gRPC status instead of the string code. - if (err && (err["code"] === "upstream_stream_truncated" || err["status"] === "UNAVAILABLE")) return err["meta"] as Record<string, unknown>; - } - } catch { - /* not a JSON frame */ - } +function truncationErrorFrame(writes: string[]): Record<string, unknown> { + for (const frame of writes.join("").split("\n\n")) { + const dataLine = frame.split(/\r?\n/).find((line) => line.startsWith("data: ")); + const data = dataLine?.slice("data: ".length); + if (!data || data === "[DONE]") continue; + try { + const event = JSON.parse(data) as Record<string, unknown>; + const error = event["error"] as Record<string, unknown> | undefined; + const nestedError = error?.["error"] as Record<string, unknown> | undefined; + if (event["code"] === "upstream_stream_truncated" || error?.["code"] === "upstream_stream_truncated" || error?.["status"] === "UNAVAILABLE" || nestedError?.["code"] === "upstream_stream_truncated") return event; + } catch { + continue; } } - return undefined; + throw new Error("Expected an upstream truncation SSE frame"); } -const openaiChunk = (fr: string | null) => `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: fr === null ? { content: "hi" } : {}, finish_reason: fr }] })}\n\n`; - -test("chat pipe (openai): dangling `data: [DO` at EOF classifies as terminal-bytes-unrecognized", async () => { - const out: string[] = []; - const logs: string[] = []; - await pipePluginChatWithStrip( - streamOf([openaiChunk(null), "data: [DO"]), - makeRes(out), - "openai", - undefined, - (m) => logs.push(m), - ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta, "error frame carries meta"); - assert.equal(meta!["classification"], "terminal-bytes-unrecognized"); - assert.equal(meta!["cause"], "eof"); - assert.equal(meta!["protocol"], "openai"); - assert.equal(meta!["eofBufferBytes"], "data: [DO".length); - assert.equal(meta!["eofBufferHead"], "data: [DO"); - assert.deepEqual(meta!["lastEventTypes"], ["chunk"]); - assert.equal(meta!["events"], 1); - assert.ok((meta!["bytes"] as number) > 0); - assert.equal(meta!["retryZeroByteCutSpent"], false); - assert.equal(meta!["retryDegenerateSpent"], false); - // wire shape: openai meta nests inside `error`, and the [DONE] tail still - // follows the error frame (#721 contract unchanged). - assert.ok(out.join("").includes("data: [DONE]")); - // log line: one grep-able line with the classification + full diag. - const line = logs.find((l) => l.includes("upstream stream truncated")); - assert.ok(line, "log line emitted"); - assert.ok(line!.includes("classification=terminal-bytes-unrecognized")); - assert.ok(line!.includes("cause=eof")); - assert.ok(line!.includes("diag=")); -}); +function diagFromLogs(logs: string[]): Record<string, unknown> { + const line = logs.find((entry) => entry.includes("upstream stream truncated")); + const serialized = line?.match(/; diag=(\{.*\})\]$/)?.[1]; + if (!serialized) throw new Error("Expected truncation diagnostics in local logs"); + return JSON.parse(serialized) as Record<string, unknown>; +} -test("chat pipe (anthropic): clean EOF with no terminal classifies as no-terminal-seen", async () => { - const out: string[] = []; - await pipePluginChatWithStrip( - streamOf([ - `event: message_start\ndata: ${JSON.stringify({ type: "message_start" })}\n\n`, - `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } })}\n\n`, - ]), - makeRes(out), - "anthropic", - ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta, "error frame carries meta"); - assert.equal(meta!["classification"], "no-terminal-seen"); - assert.equal(meta!["cause"], "eof"); - assert.equal(meta!["eofBufferBytes"], 0); - assert.equal(meta!["eofBufferHead"], undefined); - assert.deepEqual(meta!["lastEventTypes"], ["message_start", "content_block_delta"]); - assert.ok((meta!["visibleChars"] as number) >= 2); - assert.ok(out.join("").includes("event: error")); -}); +const openaiChunk = (text: string) => `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { content: text }, finish_reason: null }] })}\n\n`; -test("chat pipe (google): meta rides the UNAVAILABLE error object and counts unparseable forwards", async () => { - const out: string[] = []; - await pipePluginChatWithStrip( - streamOf([ - // one parseable chunk, then one unparseable frame (forwarded raw), - // then EOF with nothing terminal in flight. - `data: ${JSON.stringify({ candidates: [{ index: 0, content: { role: "model", parts: [{ text: "hi" }] } }] })}\n\n`, - "data: {{{not-json\n\n", - ]), - makeRes(out), - "google", - ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta, "error frame carries meta"); - assert.equal(meta!["classification"], "no-terminal-seen"); - assert.equal(meta!["unparseableForwarded"], 1); - assert.deepEqual(meta!["lastEventTypes"], ["chunk", "unparseable"]); - const frame = out.join(""); - // Gemini's error object carries a numeric code + gRPC status, no string - // code (see emitUpstreamTruncation) — UNAVAILABLE identifies the frame. - assert.ok(frame.includes('"status":"UNAVAILABLE"')); - assert.ok(frame.includes('"code":503')); -}); +test("OpenAI truncation keeps transport causes and trace IDs in local logs only", async () => { + const writes: string[] = []; + const logs: string[] = []; + const socketError = Object.assign(new Error("other side closed"), { name: "SocketError", code: "UND_ERR_SOCKET" }); + const failure = new TypeError("terminated", { cause: socketError }); -test("chat pipe: upstreamMeta threads HTTP status/content-type into the diag", async () => { - const out: string[] = []; await pipePluginChatWithStrip( - streamOf([openaiChunk(null)]), - makeRes(out), + streamOf([openaiChunk("partial")], failure), + makeRes(writes), "openai", undefined, + (line) => logs.push(line), undefined, undefined, undefined, undefined, - undefined, - { status: 200, contentType: "text/event-stream" }, + { + status: 200, + contentType: "text/event-stream", + requestId: "01a1bf31-7f88-7c2c-a1b8-62b2ecd1d3a5", + opencodeLogId: "log-7f88", + cfRay: "a57b139e8f1bb5af-LHR", + }, ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta); - assert.equal(meta!["upstreamStatus"], 200); - assert.equal(meta!["upstreamContentType"], "text/event-stream"); -}); -test("responses pipe: dangling response.completed head classifies as terminal-bytes-unrecognized", async () => { - const out: string[] = []; - const sse = (e: string, d: Record<string, unknown>) => `event: ${e}\ndata: ${JSON.stringify({ type: e, ...d })}\n\n`; - await pipePluginResponsesWithStrip( - streamOf([ - sse("response.created", { response: { id: "resp_1" } }), - sse("response.output_item.added", { output_index: 0, item: { type: "message", id: "msg_1" } }), - sse("response.content_part.added", { item_id: "msg_1", output_index: 0 }), - sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, delta: "he" }), - 'event: response.completed\ndata: {"type":"response.comp', - ]), - makeRes(out), - ); - const meta = truncationMeta(out, "top"); - assert.ok(meta, "error frame carries meta"); - assert.equal(meta!["classification"], "terminal-bytes-unrecognized"); - assert.equal(meta!["cause"], "eof"); - assert.equal(meta!["protocol"], "responses"); - assert.ok((meta!["eofBufferHead"] as string).includes("response.comp")); - assert.deepEqual(meta!["lastEventTypes"], [ - "response.created", - "response.output_item.added", - "response.content_part.added", - "response.output_text.delta", - ]); - assert.equal(meta!["heldEvents"], 0); - assert.ok((meta!["visibleChars"] as number) >= 2); + const event = truncationErrorFrame(writes); + const error = event["error"] as Record<string, unknown>; + assert.equal(error["code"], "upstream_stream_truncated"); + assert.equal(error["meta"], undefined); + assert.ok(!writes.join("").includes("other side closed")); + + const diag = diagFromLogs(logs); + assert.deepEqual(diag["readErrorCause"], [{ name: "SocketError", code: "UND_ERR_SOCKET" }]); + assert.equal(diag["upstreamRequestId"], "01a1bf31-7f88-7c2c-a1b8-62b2ecd1d3a5"); + assert.equal(diag["upstreamLogId"], "log-7f88"); + assert.equal(diag["upstreamCfRay"], "a57b139e8f1bb5af-LHR"); + assert.equal(diag["upstreamStatus"], 200); + assert.equal(diag["upstreamContentType"], "text/event-stream"); + assert.ok(!JSON.stringify(diag).includes("other side closed")); }); -test("responses pipe: clean EOF after deltas classifies as no-terminal-seen", async () => { - const out: string[] = []; - const sse = (e: string, d: Record<string, unknown>) => `event: ${e}\ndata: ${JSON.stringify({ type: e, ...d })}\n\n`; +test("Responses EOF records classification locally and emits no diagnostic metadata", async () => { + const writes: string[] = []; + const logs: string[] = []; + const sse = (type: string, data: Record<string, unknown>) => `event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`; + await pipePluginResponsesWithStrip( streamOf([ sse("response.created", { response: { id: "resp_1" } }), - sse("response.output_item.added", { output_index: 0, item: { type: "message", id: "msg_1" } }), - sse("response.content_part.added", { item_id: "msg_1", output_index: 0 }), - sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, delta: "hi" }), + sse("response.output_text.delta", { output_index: 0, delta: "hi" }), ]), - makeRes(out), + makeRes(writes), + undefined, + (line) => logs.push(line), ); - const meta = truncationMeta(out, "top"); - assert.ok(meta); - assert.equal(meta!["classification"], "no-terminal-seen"); - assert.equal(meta!["eofBufferBytes"], 0); + + const event = truncationErrorFrame(writes); + assert.equal(event["code"], "upstream_stream_truncated"); + assert.equal(event["meta"], undefined); + const diag = diagFromLogs(logs); + assert.equal(diag["classification"], "no-terminal-seen"); + assert.equal(diag["cause"], "eof"); }); -test("responses pipe: upstream read error records cause=read-error", async () => { - const out: string[] = []; - const sse = (e: string, d: Record<string, unknown>) => `event: ${e}\ndata: ${JSON.stringify({ type: e, ...d })}\n\n`; - await pipePluginResponsesWithStrip( - streamOf([sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, delta: "x" })], "error"), - makeRes(out), +test("Google truncation retains its protocol error shape without exposing diagnostics", async () => { + const writes: string[] = []; + const logs: string[] = []; + await pipePluginChatWithStrip( + streamOf([`data: ${JSON.stringify({ candidates: [{ content: { role: "model", parts: [{ text: "hi" }] } }] })}\n\n`]), + makeRes(writes), + "google", + undefined, + (line) => logs.push(line), ); - const meta = truncationMeta(out, "top"); - assert.ok(meta); - assert.equal(meta!["cause"], "read-error"); - assert.equal(meta!["classification"], "no-terminal-seen"); + + const event = truncationErrorFrame(writes); + const error = event["error"] as Record<string, unknown>; + assert.equal(error["code"], 503); + assert.equal(error["status"], "UNAVAILABLE"); + assert.equal(error["meta"], undefined); + assert.equal(diagFromLogs(logs)["classification"], "no-terminal-seen"); }); -test("chat pipe (anthropic): dangling message_stop head classifies as terminal-bytes-unrecognized", async () => { - const out: string[] = []; +test("Anthropic truncation keeps provider error shape without client diagnostics", async () => { + const writes: string[] = []; + const logs: string[] = []; await pipePluginChatWithStrip( - streamOf([ - `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } })}\n\n`, - "event: message_stop\ndata: {\"type\":\"mess", - ]), - makeRes(out), + streamOf([`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } })}\n\n`]), + makeRes(writes), "anthropic", + undefined, + (line) => logs.push(line), ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta); - assert.equal(meta!["classification"], "terminal-bytes-unrecognized"); + + const event = truncationErrorFrame(writes); + const error = event["error"] as Record<string, unknown>; + assert.equal(error["code"], "upstream_stream_truncated"); + assert.equal(error["meta"], undefined); + const diag = diagFromLogs(logs); + assert.equal(diag["protocol"], "anthropic"); + assert.equal(diag["cause"], "eof"); }); diff --git a/tests/issue5951-reasoning-only-cut-replay.test.ts b/tests/issue5951-reasoning-only-cut-replay.test.ts index 1148a6d0c8..281323afd0 100644 --- a/tests/issue5951-reasoning-only-cut-replay.test.ts +++ b/tests/issue5951-reasoning-only-cut-replay.test.ts @@ -81,20 +81,11 @@ function chatFinish(reason = "stop"): string { const DONE = "data: [DONE]\n\n"; const ERROR_SENTINEL = "upstream_stream_truncated"; -/** The `meta` object riding the in-band truncation frame, or undefined when the - * turn completed without one. */ -function truncationMeta(body: string): Record<string, unknown> | undefined { - for (const frame of body.split("\n\n")) { - if (!frame.includes(ERROR_SENTINEL)) continue; - const payload = frame - .split("\n") - .filter((line) => line.startsWith("data:")) - .map((line) => line.slice(5).trim()) - .join("\n"); - const parsed = JSON.parse(payload) as { error?: { meta?: Record<string, unknown> } }; - return parsed.error?.meta; - } - return undefined; +function diagFromLogs(logs: string[]): Record<string, unknown> { + const line = logs.find((entry) => entry.includes("upstream stream truncated")); + const serialized = line?.match(/; diag=(\{.*\})\]$/)?.[1]; + if (!serialized) throw new Error("Expected truncation diagnostics in local logs"); + return JSON.parse(serialized) as Record<string, unknown>; } function healedTurn(): ReadableStream<Uint8Array> { @@ -106,6 +97,41 @@ function healedTurn(): ReadableStream<Uint8Array> { ]); } +for (const field of ["reasoning_content", "reasoning"]) { + for (const cause of ["read-error", "eof"]) { + test(`chat pipe: ${cause} after filtered ${field} still heals without replaying an answer (#5951)`, async () => { + const out: string[] = []; + const reasoning = "Inspect \x3cacp before answering"; + const frames = [chatChunk({ role: "assistant", [field]: reasoning })]; + const first = cause === "eof" ? eventsStream(frames) : failedStream(frames, new Error("peer closed")); + let refetches = 0; + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), () => {}, async () => { + refetches += 1; + return healedTurn(); + }, "http://up"); + + const body = out.join(""); + assert.equal(refetches, 1); + assert.ok(body.includes(reasoning), "the first attempt's reasoning remains byte-exact"); + assert.ok(body.includes("healed answer")); + assert.ok(!body.includes(ERROR_SENTINEL)); + }); + } +} + +test("chat pipe: pending answer markup after filtered reasoning is not replayed (#5951)", async () => { + const out: string[] = []; + const frames = [chatChunk({ reasoning_content: "Inspect \x3cacp" }), chatChunk({ content: "\x3cacp" })]; + let refetches = 0; + await pipePluginChatWithStrip(failedStream(frames, new Error("peer closed")), makeRes(out), "openai", makeSession(), () => {}, async () => { + refetches += 1; + return healedTurn(); + }, "http://up"); + assert.equal(refetches, 0, "the reasoning-filter exception must not accept a pending answer filter"); + assert.ok(out.join("").includes(ERROR_SENTINEL)); +}); + test("chat pipe: a read error after reasoning-only output re-issues once (#5951)", async () => { const out: string[] = []; const logs: string[] = []; @@ -225,16 +251,16 @@ test("chat pipe: the reasoning-only replay shares the single re-issue budget (#5 refetches += 1; // The retry is cut in the same place: the budget is already spent, so the // host gets the truncation signal rather than a second re-issue. - return Promise.resolve(failedStream([chatChunk({ role: "assistant", reasoning_content: "reasoning-c" })], new Error("upstream socket reset mid-stream"))); + return Promise.resolve(failedStream([chatChunk({ role: "assistant", reasoning_content: "\x3cacp retry" })], new Error("upstream socket reset mid-stream"))); }; - const first = failedStream([chatChunk({ role: "assistant", reasoning_content: "reasoning-a" })], new Error("upstream socket reset mid-stream")); + const first = failedStream([chatChunk({ role: "assistant", reasoning_content: "\x3cacp first" })], new Error("upstream socket reset mid-stream")); await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (m) => logs.push(m), refetch, "http://up"); const body = out.join(""); assert.equal(refetches, 1, "one re-issue per request, shared with the zero-byte path"); assert.ok(body.includes(ERROR_SENTINEL)); - assert.equal(truncationMeta(body)?.["retryZeroByteCutSpent"], true); + assert.equal(diagFromLogs(logs)["retryZeroByteCutSpent"], true); }); test("chat pipe: a cut that is not replayed names the read error in the diag (#5951)", async () => { @@ -247,8 +273,92 @@ test("chat pipe: a cut that is not replayed names the read error in the diag (#5 await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (m) => logs.push(m), undefined, "http://up"); - const meta = truncationMeta(out.join("")); - assert.ok(meta, "the truncation frame carries a diag"); - assert.equal(meta["cause"], "read-error"); - assert.equal(meta["readError"], "Error: upstream socket reset mid-stream"); + const diag = diagFromLogs(logs); + assert.equal(diag["cause"], "read-error"); + assert.equal(diag["readError"], "Error: upstream socket reset mid-stream"); +}); + +test("chat pipe: a cut refused for its answer text names that clause in the diag (#5951)", async () => { + const out: string[] = []; + const logs: string[] = []; + const first = failedStream( + [chatChunk({ role: "assistant", reasoning_content: "reasoning-a" }), chatChunk({ content: "partial answer" })], + new Error("upstream socket reset mid-stream"), + ); + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (line) => logs.push(line), () => Promise.resolve(healedTurn()), "http://up"); + + const replay = diagFromLogs(logs)["replay"] as Record<string, unknown> | undefined; + assert.ok(replay, "a refused cut carries the predicate's inputs"); + assert.equal(replay["replayAvailable"], true, "the refetch was there, so only the predicate refused"); + assert.equal(replay["sawThinking"], true); + assert.equal(replay["blocksForwarded"], 0); + assert.equal(replay["openStreams"], 0); + assert.equal(replay["answerProseChars"], "partial answer".length, "the delivered answer text is the flag that refused it"); +}); + +test("chat pipe: a cut refused for a started tool call names that clause in the diag (#5951)", async () => { + const out: string[] = []; + const logs: string[] = []; + const first = failedStream( + [ + chatChunk({ role: "assistant", reasoning_content: "reasoning-a" }), + chatChunk({ tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "read_file", arguments: '{"pa' } }] }), + ], + new Error("upstream socket reset mid-stream"), + ); + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (line) => logs.push(line), () => Promise.resolve(healedTurn()), "http://up"); + + const replay = diagFromLogs(logs)["replay"] as Record<string, unknown> | undefined; + assert.ok(replay); + assert.equal(replay["sawToolUse"], true, "the started tool call is why the replay was refused"); + assert.ok((replay["proseChars"] as number) > 0, "the partial argument the client already holds is counted too"); +}); + +test("chat pipe: a pipe with no refetch reports the replay as unavailable (#5951)", async () => { + const out: string[] = []; + const logs: string[] = []; + const first = failedStream([chatChunk({ role: "assistant", reasoning_content: "reasoning-a" })], new Error("upstream socket reset mid-stream")); + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (line) => logs.push(line), undefined, "http://up"); + + const replay = diagFromLogs(logs)["replay"] as Record<string, unknown> | undefined; + assert.ok(replay); + assert.equal(replay["replayAvailable"], false); + assert.equal(replay["sawThinking"], true, "the reasoning-only shape is visible even where no replay door exists"); +}); + +test("chat pipe: the host owning truncation recovery suppresses the replay (#5951)", async () => { + const out: string[] = []; + const logs: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { + refetches += 1; + return Promise.resolve(healedTurn()); + }; + const first = failedStream([chatChunk({ role: "assistant", reasoning_content: "reasoning-a" })], new Error("upstream socket reset mid-stream")); + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (line) => logs.push(line), refetch, "http://up", undefined, undefined, undefined, undefined, undefined, true); + + const body = out.join(""); + assert.equal(refetches, 0, "the host's own recovery owns the single retry budget"); + assert.ok(body.includes(ERROR_SENTINEL), "the host is told the cut happened, so its own retry can run"); + assert.equal(body.split("reasoning-a").length - 1, 1, "the partial reasoning the client already holds is still forwarded"); + const replay = diagFromLogs(logs)["replay"] as Record<string, unknown> | undefined; + assert.equal(replay?.["replayAvailable"], true, "the diag still records that a replay door existed"); +}); + +test("chat pipe: the host owning recovery also suppresses the zero-byte replay (#5951)", async () => { + const out: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { + refetches += 1; + return Promise.resolve(healedTurn()); + }; + + await pipePluginChatWithStrip(eventsStream([]), makeRes(out), "openai", makeSession(), () => {}, refetch, "http://up", undefined, undefined, undefined, undefined, undefined, true); + + assert.equal(refetches, 0, "a cut with no client-visible output is still the host's to recover"); + assert.ok(out.join("").includes(ERROR_SENTINEL)); }); diff --git a/tests/native-intercept-selfheal.test.ts b/tests/native-intercept-selfheal.test.ts index 4e47440c6a..155ecc3652 100644 --- a/tests/native-intercept-selfheal.test.ts +++ b/tests/native-intercept-selfheal.test.ts @@ -50,6 +50,30 @@ async function withHeal<T>(fn: (ctx: { sink: string[]; rearm: (v: typeof fetch) } } +test("native headersFor stamps a host recovery owner on the routed proxy request", async () => { + const captured: Headers[] = []; + const saved = globalThis.fetch; + globalThis.fetch = (async (_input: string | URL | Request, init?: RequestInit) => { + captured.push(new Headers(init?.headers)); + return new Response("{}", { status: 200 }); + }) as typeof fetch; + const { _resetForTest } = await import("../src/agent/native-intercept.js"); + const state: NativeInterceptState = { + origin: "http://127.0.0.1:40001", + ready: Promise.resolve("http://127.0.0.1:40001"), + headersFor: () => ({ "x-bili-host-recovery-owner": "omp" }), + }; + _resetForTest(); + try { + assert.equal(installNativeFetchIntercept(state), true); + await globalThis.fetch("http://127.0.0.1:8199/v1/chat/completions", { method: "POST", body: "{}" }); + assert.equal(captured[0]?.get("x-bili-host-recovery-owner"), "omp"); + } finally { + globalThis.fetch = saved; + _resetForTest(); + } +}); + test("#1158 self-heal: third-party reset to a frozen bare fetch re-chains and keeps routing through bili", async () => { const bareSink: string[] = []; const { sink } = await withHeal(async ({ rearm, fetch }) => { diff --git a/tests/omp-native.test.ts b/tests/omp-native.test.ts index 9f2bca97d1..c2c3500e16 100644 --- a/tests/omp-native.test.ts +++ b/tests/omp-native.test.ts @@ -6,10 +6,11 @@ import path from "node:path"; import { pathToFileURL } from "node:url"; import { createServer } from "node:http"; import type { AddressInfo } from "node:net"; -import factory, { ompTrafficRidesBili, shouldBootstrapNativeOmp, planNativeOmp, armNativeOmp, _resetNativeStateForTest, _setSpawnForTest, _stateRespawnForTest, _noteRoutedForTest } from "../src/agent/omp-native.ts"; +import factory, { ompTrafficRidesBili, shouldBootstrapNativeOmp, planNativeOmp, armNativeOmp, _resetNativeStateForTest, _setSpawnForTest, _stateRespawnForTest, _noteRoutedForTest, _headersForTest } from "../src/agent/omp-native.ts"; import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, OMP_MESSAGE_THREADS_ENV, nativeProxyScriptPath, ompFirstEventTimeoutValue, OMP_FIRST_EVENT_TIMEOUT_DEFAULT_MS, OMP_FIRST_EVENT_TIMEOUT_ENV } from "../src/agent/native-bootstrap.ts"; import { pluginInstall, pluginRemove, pluginStatusAll, selfPackageRoot, ompPluginLoadedFrom } from "../src/plugin-install.ts"; import { rmrf } from "./tmp-rm.ts"; +import { BILI_HOST_RECOVERY_OWNER_HEADER, BILI_HOST_RECOVERY_OWNER_OMP } from "../src/util.ts"; // #1135 wiring tests drive verifyAttachAndRecover against a dead target — cap // the routed-evidence grace so the probe fails fast instead of paying the 5s default. @@ -94,6 +95,12 @@ test("default export is an ExtensionFactory (loaded by omp's extensions loader)" assert.equal(typeof factory, "function"); }); +test("native OMP host stamps the private recovery-owner marker", () => { + assert.deepEqual(_headersForTest("https://api.example.test/v1/chat/completions"), { + [BILI_HOST_RECOVERY_OWNER_HEADER]: BILI_HOST_RECOVERY_OWNER_OMP, + }); +}); + // — installer: `bili plugin install omp` targets the native entry (#957) —— const NATIVE_ENTRY = path.join(selfPackageRoot(), "dist", "agent", "omp-native.js"); From 3e27168cea3d6a8fcfb267cf908ee675bf8a3274 Mon Sep 17 00:00:00 2001 From: ci-check <ci-check@local> Date: Fri, 13 Nov 2026 03:12:52 +0800 Subject: [PATCH 49/78] fix(#5951): commandcode truncation record uses single-newline JSONL terminator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The commandcode branch of emitUpstreamTruncation emitted a blank line ("\n\n") after the error record. The commandcode wire is bare JSONL and every other record on it (nativeErrorChunk, legacyCompletionChunk, emitPreflightError) is single-newline terminated; a blank line is not a valid JSONL record and chokes strict consumers at the moment the stream already failed. Revert to single newline and pin the invariant in the commandcode-plugin-pipe test (no blank line, endsWith }\n). PR description claimed the commandcode branch "keeps its shape" from #5958 — it did not; this restores it. (cherry picked from commit 910bc132498756c6325c0a58b93e7bfd834700bc) --- src/stream-error.ts | 2 +- tests/commandcode-plugin-pipe.test.ts | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/stream-error.ts b/src/stream-error.ts index b6305194c8..c1495a747e 100644 --- a/src/stream-error.ts +++ b/src/stream-error.ts @@ -279,7 +279,7 @@ export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Proto } else if (protocol === "commandcode") { // The finish event IS this wire's terminator: when delivered there // is nothing to synthesize; only a mid-flight cut gets an error line. - if (!finished) safeWrite(res, `${JSON.stringify({ type: "error", error: "upstream_stream_truncated", message })}\n\n`); + if (!finished) safeWrite(res, `${JSON.stringify({ type: "error", error: "upstream_stream_truncated", message })}\n`); } else { if (finished) { safeWrite(res, `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`); diff --git a/tests/commandcode-plugin-pipe.test.ts b/tests/commandcode-plugin-pipe.test.ts index 8408d03648..abbfea4138 100644 --- a/tests/commandcode-plugin-pipe.test.ts +++ b/tests/commandcode-plugin-pipe.test.ts @@ -157,16 +157,25 @@ test("stream-error emitters: commandcode branches speak bare JSONL", () => { assert.match(aOut, /"type":"error"/); assert.match(aOut, /boom/); assert.ok(!aOut.includes("data:"), "no SSE framing on the JSONL wire"); + assert.ok(!aOut.includes("\n\n"), "error record ends with exactly one newline"); + assert.ok(aOut.endsWith("}\n"), "bare JSONL record terminator"); const b = makeCollector(); emitUpstreamTruncation(b.res, "commandcode", false, undefined); - assert.match(b.out(), /"type"/, "truncation event is a JSONL line"); + const bOut = b.out(); + assert.match(bOut, /"type"/, "truncation event is a JSONL line"); + // Every record on this wire is single-newline terminated (the pipe writes + // `line + "\n"`); a blank line is not a valid JSONL record and would choke + // strict consumers at exactly the moment the stream already failed. + assert.ok(!bOut.includes("\n\n"), "truncation record carries no blank line"); + assert.ok(bOut.endsWith("}\n"), "truncation record ends with exactly one newline"); const c = makeCollector(); emitPreflightError(c.res, "commandcode", { message: "too big", retryable: false }, undefined); const cOut = c.out(); assert.match(cOut, /too big/); assert.ok(!cOut.includes("event:"), "no SSE event framing"); + assert.ok(!cOut.includes("\n\n"), "preflight error record ends with exactly one newline"); }); // F9c: duplicate tool names must not corrupt settleToolCalls' verbatim replay. From f912d91d673ed475d157c861beba10395dcbe3c5 Mon Sep 17 00:00:00 2001 From: myNameIsZhangYu <zhangyu_1215@sina.cn> Date: Thu, 12 Nov 2026 10:14:03 +0800 Subject: [PATCH 50/78] fix(openai): treat omitted cache fields as zero cached tokens (cherry picked from commit 8113815c5e53f9b4e2b095021e597e13fefa2c6a) --- src/loop/adapter-openai.ts | 7 +++++-- tests/loop-core.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index 282910787b..29f8654e44 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -163,11 +163,14 @@ function stripFinishReasonChunk(buf: Buffer): Buffer { // standard field is prompt_tokens_details.cached_tokens, while DeepSeek reports // KV-cache hits as top-level prompt_cache_hit_tokens (#779). Both mean "input // tokens served from cache", so normalize to one number. -function openaiCachedTokens(u: Record<string, unknown>): number | undefined { +function openaiCachedTokens(u: Record<string, unknown>): number | null { const pd = u.prompt_tokens_details as Record<string, unknown> | undefined; + if (typeof u.prompt_tokens === "number" + && typeof pd?.cached_tokens !== "number" + && typeof u.prompt_cache_hit_tokens !== "number") return 0; if (typeof pd?.cached_tokens === "number") return pd.cached_tokens; if (typeof u.prompt_cache_hit_tokens === "number") return u.prompt_cache_hit_tokens; - return undefined; + return null; } export function createOpenaiAdapter(requestBody: Record<string, unknown>, clientSystem?: string, absorbName?: string, notes?: string[], errorShape: "protocol" | "completion" = "protocol"): CompressLoopAdapter { diff --git a/tests/loop-core.test.ts b/tests/loop-core.test.ts index b0012ca38e..e420605678 100644 --- a/tests/loop-core.test.ts +++ b/tests/loop-core.test.ts @@ -4,6 +4,7 @@ import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; import type { Session } from "../src/session.ts"; import { runCompressLoop, createResponsesAdapter, createOpenaiAdapter, MAX_LOOP_ROUNDS } from "../src/loop/index.ts"; +import { buildSessionCacheReport } from "../src/cache-ledger.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; import { createHandoffReply } from "../src/server/handoff-response.ts"; import type { WireProtocol } from "../src/util.ts"; @@ -410,6 +411,28 @@ for (const completion of ["text", "tool"] as const) { } } +test("#6329: OpenAI cache-field omission means zero cached tokens, not unmeasured", async () => { + const ctx = makeCtx([], "openai"); + const body = { model: "m", messages: [], stream: true, stream_options: { include_usage: true } }; + const raw = [ + `data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "hello" } }] })}\n\n`, + `data: ${JSON.stringify({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 1000, completion_tokens: 10, total_tokens: 1010 } })}\n\n`, + "data: [DONE]\n\n", + ].join(""); + for await (const _chunk of runCompressLoop( + new Response(raw).body!, ctx, body, { url: "http://mock", headers: {} }, + createOpenaiAdapter(body), buildCompressSystemPrompt(), + )) {} + assert.equal(ctx.session.stats.inputTokens, 1000); + assert.equal(ctx.session.stats.cachedTokens, 0); + assert.equal(ctx.session.stats.cacheSamples, 1); + const report = buildSessionCacheReport(ctx.session); + assert.equal(report.unmeasured.samples, 0); + assert.equal(report.unmeasured.inputTokens, 0); + assert.equal(report.totals.input, 1000); + assert.equal(report.totals.cached, 0); +}); + test("loop usage: protocol unset → legacy additive behavior (prompt + cached)", async () => { const ctx = makeCtx(); await drain( From 708cef6ef9b89b929beb0171eb18d405ed278c1e Mon Sep 17 00:00:00 2001 From: myNameIsZhangYu <zhangyu_1215@sina.cn> Date: Thu, 12 Nov 2026 10:36:03 +0800 Subject: [PATCH 51/78] fix(storage): serialize archive exports and restore the newest dump (cherry picked from commit 5049b917716b70659cbe44eab7070e94cb12877f) --- src/storage/mysql.ts | 10 +++++++- src/storage/store.ts | 20 ++++++++++++---- tests/storage-archive-gc.test.ts | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index 5befe169b7..1f241320ac 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -1848,6 +1848,7 @@ export class MysqlBackend implements StorageBackend { }; // sqlite twin: newest matching line wins. let lineNo = -1; + let matchCount = 0; let rawLines: string[] = []; try { rawLines = readFileSync(exportPath, "utf8").split("\n"); @@ -1859,9 +1860,13 @@ export class MysqlBackend implements StorageBackend { } catch { continue; } + // #6317: count ALL entries for this session. The reverse scan + // meets the NEWEST line first — keep that; a plain + // `lineNo = i` without break would end on the OLDEST. if (candidate && typeof candidate === "object" && "sessionId" in candidate && candidate.sessionId === sessionId) { + if (lineNo === -1) lineNo = i; + matchCount += 1; lineNo = i; - break; } } } catch { @@ -1874,6 +1879,9 @@ export class MysqlBackend implements StorageBackend { return { ok: false, reason: "export-corrupt" }; } if (!dump.session || dump.session.sessionId !== sessionId) return { ok: false, reason: "export-mismatch" }; + if (matchCount > 1) { + loggerLog("warn", `[#6317] archive log contained ${matchCount} entries for session ${sessionId}; restoring the newest entry and removing all entries`); + } const sess = dump.session; await this.tx(async (c) => { const references = new Map<string, number>(); diff --git a/src/storage/store.ts b/src/storage/store.ts index 4e42316ec8..c4789dcb69 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -2153,13 +2153,16 @@ export class UnifiedStore { sessionExt: Record<string, unknown>[]; checkpoints: Record<string, unknown>[]; }; - // Scan the JSONL for the session's line (skip malformed/truncated + // Scan the JSONL for the session's NEWEST line (#6317: a pre-fix + // failed archive can leave stale lines behind; a later successful + // archive appends the newest dump). Skip malformed/truncated // trailing lines — an archive append interrupted by a crash must not // block restoring OTHER sessions). Newest first: when the log holds // more than one line for this session (an archive whose sweep never // committed, then a later successful one), the last line appended is // the state the session was actually archived in. let lineNo = -1; + let matchCount = 0; let rawLines: string[] = []; try { rawLines = readFileSync(exportPath, "utf8").split("\n"); @@ -2171,7 +2174,10 @@ export class UnifiedStore { } catch { continue; } - if (candidate && typeof candidate === "object" && "sessionId" in candidate && candidate.sessionId === sessionId) { lineNo = i; break; } + // #6317: count ALL entries for this session. The reverse scan + // meets the NEWEST line first — keep that; a plain + // `lineNo = i` without break would end on the OLDEST. + if (candidate && typeof candidate === "object" && "sessionId" in candidate && candidate.sessionId === sessionId) { if (lineNo === -1) lineNo = i; matchCount += 1; } } } catch { return { ok: false, reason: "export-corrupt" }; @@ -2183,6 +2189,9 @@ export class UnifiedStore { return { ok: false, reason: "export-corrupt" }; } if (!dump.session || dump.session.sessionId !== sessionId) return { ok: false, reason: "export-mismatch" }; + if (matchCount > 1) { + loggerLog("warn", `[#6317] archive log contained ${matchCount} entries for session ${sessionId}; restoring the newest entry and removing all entries`); + } const sess: NonNullable<typeof dump.session> = dump.session; this.db.transaction(() => { const references = new Map<string, number>(); @@ -2244,9 +2253,10 @@ export class UnifiedStore { sess.lastIngestedSeq ?? null, sess.orphansJson ?? null, sessionId, ); }); - // Drop the restored session's line(s) from the JSONL (a same-session - // archive can theoretically appear more than once across reinstalls; - // rewrite removes them all). Other sessions' lines are untouched. + // Drop the restored session's line(s) from the JSONL. #6317: a + // pre-fix failed archive can leave more than one line; restore now + // already used the newest match and removes all matches, warning when + // stale lines were present so the loss pattern is diagnosable. const kept: string[] = []; let ownLines = 0; for (const l of rawLines) { diff --git a/tests/storage-archive-gc.test.ts b/tests/storage-archive-gc.test.ts index 4180076f30..d60fa6d7c3 100644 --- a/tests/storage-archive-gc.test.ts +++ b/tests/storage-archive-gc.test.ts @@ -5,6 +5,7 @@ import * as os from "node:os"; import * as path from "node:path"; import { planIngest, type LegacySessionLike } from "../src/storage/ingest.ts"; import { UnifiedStore } from "../src/storage/store.ts"; +import { openSqliteDatabase } from "../src/storage/driver.ts"; import { resolveArchiveAfterDays } from "../src/knobs.ts"; /** @@ -170,3 +171,43 @@ test("gc(): age sweep archives only past-retention sessions; dormant shared rows try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handles */ } } }); + +test("#6317: failed archive sweep leaves no archive line; restore uses the newest entry", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "storage-archive-failed-")); + try { + const store = UnifiedStore.open(path.join(dir, "index.db")); + store.ingestSession(planIngest(mkSession("sess-A", [mkMsg("m-a", "old body ".repeat(50))]))); + const archiveDir = path.join(dir, "archive"); + const logPath = path.join(archiveDir, UnifiedStore.ARCHIVE_LOG_NAME); + fs.mkdirSync(archiveDir, { recursive: true }); + + const second = openSqliteDatabase(path.join(dir, "index.db")).db; + second.exec("BEGIN IMMEDIATE"); + try { + assert.throws( + () => store.archiveSession("sess-A", { archiveDir }), + /database is locked/, + "archive fails while another writer holds the lock", + ); + } finally { + second.exec("ROLLBACK"); + second.close(); + } + assert.ok(!fs.existsSync(logPath), "failed archive must not append a stale line"); + + store.ingestSession(planIngest(mkSession("sess-A", [mkMsg("m-a", "old body ".repeat(50)), mkMsg("m-a2", "new body ".repeat(50))]))); + const archived = store.archiveSession("sess-A", { archiveDir }); + assert.ok(archived.ok); + const lines = fs.readFileSync(logPath, "utf8").split("\n").filter((l) => l.trim()); + assert.equal(lines.length, 1, "only the successful archive writes a line"); + const dump = JSON.parse(lines[0]) as { messages?: { msg_id?: string }[] }; + assert.deepEqual(dump.messages?.map((m) => m.msg_id), ["m-a", "m-a2"]); + + const restored = store.restoreArchived("sess-A", { archiveDir }); + assert.ok(restored.ok); + assert.deepEqual(store.getMessages("sess-A").map((m) => m.msgId), ["m-a", "m-a2"], "restore returns the newest dump"); + store.close(); + } finally { + try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handles */ } + } +}); From 30fb96fb00e906f7da619121a8f7955cd46573e8 Mon Sep 17 00:00:00 2001 From: myNameIsZhangYu <zhangyu_1215@sina.cn> Date: Thu, 12 Nov 2026 10:44:44 +0800 Subject: [PATCH 52/78] fix(update): prefer atomic directory swaps during in-place updates (cherry picked from commit 5b5121a3b24e3217a52f11de1a9f63ad773c7c07) --- src/update.ts | 54 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/src/update.ts b/src/update.ts index 2886deeb12..e3c493bcff 100644 --- a/src/update.ts +++ b/src/update.ts @@ -2099,21 +2099,47 @@ export async function installViaTarball( } }; - // Copy staging over install dir using Node's built-in fs.cp (Node 16.7+). - // Cross-platform — no dependency on the `cp` binary (absent on Windows). - // `recursive: true` + the trailing `/.` semantics: fs.cp copies the - // *contents* of stagingDir into installDir, merging without nesting. - let copyError: string | null = null; + // #6309: prefer an atomic directory swap. Copy staging next to the + // install dir on the same volume, then rename the current install aside + // and rename the prepared copy into place. The old copy is the rollback + // source and is only removed after verification. If rename is not + // possible (common on Windows while the running install holds handles), + // fall back to the previous merge-copy path. + const swapCandidate = `${installDir}.update-new-${Date.now()}`; + let copiedForSwap = false; try { - await cp(stagingDir, installDir, { recursive: true, force: true }); - } catch (e) { - copyError = `failed to copy to install dir: ${String(e)}`; - } finally { + await cp(stagingDir, swapCandidate, { recursive: true, force: true }); + copiedForSwap = true; + await rename(installDir, backupDir); + try { + await rename(swapCandidate, installDir); + } catch (e) { + const swapError = String(e); + await rename(backupDir, installDir); + throw new Error(swapError); + } await rm(stagingDir, { recursive: true, force: true }); - } - if (copyError !== null) { - const rb = await restoreFromBackup(); - return { ok: false, error: rb ?? copyError }; + } catch { + if (copiedForSwap) await rm(swapCandidate, { recursive: true, force: true }); + if (!pnpmOldLink) { + try { + await cp(installDir, backupDir, { recursive: true, force: true }); + } catch (e) { + return { ok: false, error: `backup of current install failed (install left untouched): ${String(e)}` }; + } + } + let copyError: string | null = null; + try { + await cp(stagingDir, installDir, { recursive: true, force: true }); + } catch (e) { + copyError = `failed to copy to install dir: ${String(e)}`; + } finally { + await rm(stagingDir, { recursive: true, force: true }); + } + if (copyError !== null) { + const rb = await restoreFromBackup(); + return { ok: false, error: rb ?? copyError }; + } } // Final verification: version must match and every declared entry must @@ -2151,7 +2177,7 @@ export async function installViaTarball( // Success: the backup is no longer needed, and neither is the displaced // pnpm link (the store copy underneath it was left byte-identical). - await rm(backupDir, { recursive: true, force: true }); + if (!pnpmOldLink) await rm(backupDir, { recursive: true, force: true }); if (pnpmOldLink) { try { await unlink(pnpmOldLink); From 74026290fe30b783455af4461043e06768ba29f6 Mon Sep 17 00:00:00 2001 From: myNameIsZhangYu <zhangyu_1215@sina.cn> Date: Thu, 12 Nov 2026 11:58:16 +0800 Subject: [PATCH 53/78] test: regenerate storage-render goldens for OpenAI cache attribution change The #6329 fix changes how omitted cache fields are persisted: the sample is no longer quarantined as unmeasured but recorded with cached=0. This alters stats and cache-ledger metadata digests in the storage-render golden snapshots. Regenerated via scripts/update-storage-render-goldens.ts. (cherry picked from commit bc2be40557c69477038bfda5adb43fae51b26cc7) --- tests/golden/storage-render/chat.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index e0f1b4aa96..ae4923dd78 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "97b9991ae7a398fea5d620994df0101001d9c3f246615daac95e6afb4cee14a0", - "stateDigestAligned": "fc632ea30ecb84eeb10e2e0bb4d525f06b17ebd60c6296625d71f398b569ed0b", + "stateDigest": "a7b5a67f54a2cbd7978e2a57ad78f522f7af8f94a4e2402898aee4d93479541a", + "stateDigestAligned": "76f701f5ab1631fe1615a3f1cd8fa0fcd0a3fa19c02658cc5d077ae565f4897a", "sectionDigests": { "state": "90857fbf82001a2b", "blockContents": "94a58f96d7c5cfc3", - "stats": "3e6858671f4b9a9d", - "metadata": "faa2b34b71823646" + "stats": "5c2f0fe203d710e8", + "metadata": "0c7d1819e0d4c0f6" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", From ff497a1a1abd69b697877530357306fefbda2d53 Mon Sep 17 00:00:00 2001 From: myNameIsZhangYu <zhangyu_1215@sina.cn> Date: Thu, 12 Nov 2026 10:39:40 +0800 Subject: [PATCH 54/78] perf(storage): skip rewrites when large session columns are unchanged (cherry picked from commit 1c4d646b68b15ccf05b47849bf0f1a99db4aa2ee) --- src/storage/store.ts | 6 +++--- tests/storage-unified.test.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/storage/store.ts b/src/storage/store.ts index c4789dcb69..5256cfb38a 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -1044,10 +1044,10 @@ export class UnifiedStore { next_run_id = excluded.next_run_id, meta_json = excluded.meta_json, stats_json = excluded.stats_json, - metadata_json = excluded.metadata_json, - kstate_json = excluded.kstate_json, + metadata_json = CASE WHEN metadata_json IS excluded.metadata_json THEN sessions.metadata_json ELSE excluded.metadata_json END, + kstate_json = CASE WHEN kstate_json IS excluded.kstate_json THEN sessions.kstate_json ELSE excluded.kstate_json END, tail_json = excluded.tail_json, - ccr_json = excluded.ccr_json, + ccr_json = CASE WHEN ccr_json IS excluded.ccr_json THEN sessions.ccr_json ELSE excluded.ccr_json END, last_ingested_seq = excluded.last_ingested_seq, orphans_json = excluded.orphans_json`, ) diff --git a/tests/storage-unified.test.ts b/tests/storage-unified.test.ts index 15d2d1a04c..8e4908ba87 100644 --- a/tests/storage-unified.test.ts +++ b/tests/storage-unified.test.ts @@ -960,3 +960,36 @@ test("#4313: fork clone copies ts verbatim (inherited ledger rows keep their sta fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } }); + +test("#6325: unchanged large session columns are not rewritten on delta ingest", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "storage-6325-")); + try { + const store = UnifiedStore.open(path.join(dir, "index.db")); + const now = Date.now(); + const makeSession = (id: string, text: string): LegacySessionLike => ({ + id, + createdAt: now, + lastSeen: now, + meta: { protocol: "openai" }, + stats: {}, + metadata: { cacheLedger: { keep: "x".repeat(4096) } }, + state: { blocks: [], nextBlockId: 1, nextRunId: 1, messageRefs: { byRaw: {}, byRef: {} } }, + blockContents: new Map(), + lastMessages: [{ id: "m1", role: "user", contentType: "text", text } as unknown as CoreMessage], + lastMessagesFolded: false, + pluginSnapshot: undefined, + contentStore: undefined, + } as unknown as LegacySessionLike); + const session = makeSession("sess-6325", "hello"); + const before = store.ingestSession(planIngest(session)); + assert.ok(before); + const first = store.getSessionRow("sess-6325"); + assert.ok(first?.metadataJson?.includes("cacheLedger")); + store.ingestSession(planIngest({ ...session, lastMessages: [{ id: "m2", role: "assistant", contentType: "text", text: "world" } as unknown as CoreMessage] })); + const second = store.getSessionRow("sess-6325"); + assert.equal(second?.metadataJson, first?.metadataJson, "unchanged metadata_json should reuse the stored value"); + store.close(); + } finally { + try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handles */ } + } +}); From c297463ab88d16d63b5e7754b3967c67aad7c1b3 Mon Sep 17 00:00:00 2001 From: ci-check <ci-check@local> Date: Thu, 12 Nov 2026 17:18:16 +0800 Subject: [PATCH 55/78] =?UTF-8?q?test(loop):=20restore=20#6329=20regressio?= =?UTF-8?q?n=20=E2=80=94=20OpenAI=20cache-field=20omission=20is=20a=20meas?= =?UTF-8?q?ured=20miss?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The revert in the previous head deleted the regression test for the openaiCachedTokens fix. Restore it: an OpenAI upstream that omits prompt_tokens_details / prompt_cache_hit_tokens (sglang's behavior when cached_tokens=0) must record cachedTokens=0 and count a cache sample, not quarantine the request as unmeasured. Verified: fails on the pre-fix adapter, passes with the fix. (cherry picked from commit eef9f99b9cd81264e6b98da6a1d4fb0a646a6546) --- tests/loop-core.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/loop-core.test.ts b/tests/loop-core.test.ts index e420605678..2c08f2e531 100644 --- a/tests/loop-core.test.ts +++ b/tests/loop-core.test.ts @@ -6,6 +6,7 @@ import type { Session } from "../src/session.ts"; import { runCompressLoop, createResponsesAdapter, createOpenaiAdapter, MAX_LOOP_ROUNDS } from "../src/loop/index.ts"; import { buildSessionCacheReport } from "../src/cache-ledger.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; +import { buildSessionCacheReport } from "../src/cache-ledger.ts"; import { createHandoffReply } from "../src/server/handoff-response.ts"; import type { WireProtocol } from "../src/util.ts"; import { isTransientUpstreamError, REPLAY_MAX_ATTEMPTS, replayBackoffMs, replayMaxAttempts } from "../src/fetch-util.ts"; From d342342e10ba55c55adee5939637baf01e7234ef Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 10:44:47 +0800 Subject: [PATCH 56/78] test: pin #6323 auto_vacuum-before-WAL pragma ordering (cherry picked from commit a7fe2953e71f1fa0c3df9dbcdf0526bb6c0ee3aa) The code half of that commit was already on master via #6324/#6455, so only the regression test is added here. --- tests/issue3250-wal-checkpoint.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/issue3250-wal-checkpoint.test.ts b/tests/issue3250-wal-checkpoint.test.ts index e989760ae6..d5affc667d 100644 --- a/tests/issue3250-wal-checkpoint.test.ts +++ b/tests/issue3250-wal-checkpoint.test.ts @@ -334,3 +334,12 @@ test("#3250: a configured cap flows open() → journal_size_limit (custom limit rmBestEffort(dir); } }); + +test("#6323: auto_vacuum precedes WAL so fresh databases honor INCREMENTAL", () => { + const { db } = openSqliteDatabase(":memory:"); + for (const pragma of STORAGE_PRAGMAS) db.exec(pragma); + migrateSchema(db); + const row = db.prepare("PRAGMA auto_vacuum").get() as Record<string, unknown>; + assert.equal(Object.values(row)[0], 2, "auto_vacuum should be INCREMENTAL (2)"); + db.close(); +}); From c251e0cb16d710950e9e9aa951bf88c66208f68c Mon Sep 17 00:00:00 2001 From: myNameIsZhangYu <zhangyu_1215@sina.cn> Date: Thu, 12 Nov 2026 18:37:02 +0800 Subject: [PATCH 57/78] fix(test): remove duplicate buildSessionCacheReport import (cherry picked from commit cac80437e4efea890fcd47beb6617e5a9cb33db7) --- tests/loop-core.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/loop-core.test.ts b/tests/loop-core.test.ts index 2c08f2e531..e420605678 100644 --- a/tests/loop-core.test.ts +++ b/tests/loop-core.test.ts @@ -6,7 +6,6 @@ import type { Session } from "../src/session.ts"; import { runCompressLoop, createResponsesAdapter, createOpenaiAdapter, MAX_LOOP_ROUNDS } from "../src/loop/index.ts"; import { buildSessionCacheReport } from "../src/cache-ledger.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; -import { buildSessionCacheReport } from "../src/cache-ledger.ts"; import { createHandoffReply } from "../src/server/handoff-response.ts"; import type { WireProtocol } from "../src/util.ts"; import { isTransientUpstreamError, REPLAY_MAX_ATTEMPTS, replayBackoffMs, replayMaxAttempts } from "../src/fetch-util.ts"; From dcf4ff2004053f5a42d7adea6c763f6095901eee Mon Sep 17 00:00:00 2001 From: guowenjie <691675122@qq.com> Date: Mon, 9 Nov 2026 11:05:31 +0800 Subject: [PATCH 58/78] kernel: bridge span-safe gaps at the merge gate so mega-turn islands survive minCompressRange (production 2026-11-09 no-ranges incident) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Production shape (acp host, 2026-11-09 night): a mega-turn with a protected-tool exchange every ~4 rounds + a mid-turn block cut mid-group fragmented the compressible surface into 3-5K-char islands. Every island sat below compress.minCompressRange (5000 chars), mergeRangesToThreshold dropped them all, recommendedRanges=[] reached the host, and the emergency lane fell back to irreversible physical pruning three times in one night — while the host's message-level diagnostic still counted 84-149 'collectible' messages. 28 spine-cut no-range diagnostics same day, other sessions. Two independent killers, both fixed: 1. mergeRangesToThreshold closed its batch at every index gap, so sub-gate islands could never accumulate. It now bridges across gaps buildCompressibleRanges marks span-safe (bridgeableGapBefore): protected-tool exchanges (apply carves them via filterProtectedToolMessages, 'Bug 39') and integrity-withdrawn members (apply withdraws them again, they stay visible — #564 reverse-split semantics). The per-CALL #847 gate sums chars over all ranges in the request, so a bridged span clears the gate each island missed. Hard gaps (blocks, carriers, media, dead refs, unrefed, soft zone — whose interior hits truncate the fold at the first protected message, #3953) still close the batch, and sub-gate remnants still emit nothing. buildCompressibleRanges tracks gap composition (hardGapSinceCompressible) and chains it across withdrawals. 2. Ranges ending adjacent to a mid-group block cut died at screening regardless: the pair-boundary adjustment widened the span across the coverage boundary, dragging the block in as nested/consumed, and the merge-integrity path resolved the unit to fold-nothing (fatal=null, filteredIds=0). projectUnit now clamps the ADJUSTMENT's widened head/tail at the coverage boundary (clampAdjustmentAtCoverage) — safe because covered halves are invisible to the integrity judge (#5531); the requested span itself is never clamped, so refold candidates keep resolving. The clamp reuses the shared index map so the #5337 O(units x n) discipline holds. Observability (the incident was undiagnosable from the host side): screenRecommendedUnits warns (budgeted) for every silent drop class — fatal, empty projection, refold-guard — and the recommend node warns with stage-by-stage numbers when the funnel zeroes with material present. Tests: tests/megaturn-protected-gap-bridging.test.ts pins the incident shape end-to-end (bridge + clamp + full-chain advertise, plus a negative for hard gaps). issue5531 closed-loop updated: the recovery block is found by coverage now that the clamp keeps the seed block active instead of dragging it into a merge. Full suite 1626/1626. (cherry picked from commit fe375e573888a1a91389a941e59313e0303351ec) --- kernel/src/compress.ts | 18 ++ kernel/src/recommend.ts | 149 +++++++++-- kernel/src/types.ts | 16 ++ kernel/src/unit-projection.ts | 94 ++++++- ...sue5531-stranded-reasoning-residue.test.ts | 12 +- .../megaturn-protected-gap-bridging.test.ts | 246 ++++++++++++++++++ 6 files changed, 514 insertions(+), 21 deletions(-) create mode 100644 kernel/tests/megaturn-protected-gap-bridging.test.ts diff --git a/kernel/src/compress.ts b/kernel/src/compress.ts index d180a05b24..59c3c77771 100644 --- a/kernel/src/compress.ts +++ b/kernel/src/compress.ts @@ -49,6 +49,7 @@ import { buildCompressibleRanges, mergeRangesToThreshold, screenRecommendedUnits, + warnScreenDrop, } from "./recommend.js"; import { runPipeline, @@ -1501,6 +1502,23 @@ const recommendNode: PipelineNode = { ctx.config, computeProtectedRefs(screenArray, io.state, ctx.config, ctx.countTokens), ); + // Production 2026-11-09 mega-turn incident: the funnel can zero out BELOW + // the "nothing compressible" level — segments exist and hold messages, but + // every island dies at the merge gate or the screen, and the host only + // sees recommendedRanges=[] while its own message-level diagnostic still + // counts collectible material ("collectible>0 yet ranges=0" was + // undiagnosable from the host side for a full night). Warn (budgeted) + // with the stage-by-stage numbers so the killing stage is named directly. + if (contextRanges.compressible.length > 0 && recommendedRanges.length === 0) { + const segMsgs = contextRanges.compressible.reduce((s, r) => s + r.count, 0); + const minChars = ctx.config.compress.minCompressRange; + const subGate = contextRanges.compressible.filter( + (r) => (r.chars ?? r.tokens * 4) < minChars, + ).length; + warnScreenDrop( + `funnel zeroed with material present: segments=${contextRanges.compressible.length} (${segMsgs} msgs, ${subGate} below ${minChars}-char merge gate) → all died at merge/screen; host sees no-ranges`, + ); + } const recommendation: Recommendation = { contextRanges, recommendedRanges, diff --git a/kernel/src/recommend.ts b/kernel/src/recommend.ts index 23a2ca2631..1d413f66cd 100644 --- a/kernel/src/recommend.ts +++ b/kernel/src/recommend.ts @@ -278,6 +278,11 @@ export function buildCompressibleRanges( id: string; ref: string; gapBefore: boolean; + /** True when this message's preceding gap is HARD (contains at least one + * skip the apply side cannot tolerate inside a spanning fold — see + * hardGapSinceCompressible). False = the gap is span-safe (protected-tool + * exchanges and/or integrity-withdrawn members only) or absent. */ + gapHard: boolean; tokens: number; chars: number; isTool: boolean; @@ -316,6 +321,20 @@ export function buildCompressibleRanges( // two rules coincide, so ranges are byte-identical to the old behavior. let skipSinceCompressible = false; let skipSinceProtected = false; + // Span-safety classification (production 2026-11-09 mega-turn incident): a + // gap the apply side tolerates INSIDE a spanning fold — protected-tool + // exchanges (filterProtectedToolMessages carves them, "Bug 39") and + // integrity-withdrawn members (the fold gate withdraws them again at apply, + // they stay visible; #564 reverse-split semantics) — is SPAN-SAFE, and + // mergeRangesToThreshold may bridge across it so sub-gate islands can + // accumulate to minCompressRange. Every OTHER skip (block coverage, live + // checkpoint carrier, unrecoverable media, soft zone — whose interior hits + // TRUNCATE the fold at the first protected message (#3953) — dead-ref + // holes, BLOCKED/unrefed) is hard: no bridge, ever. Set on any skip other + // than the protected-exchange branch; cleared when a compressible message + // is pushed. The protected branch leaves it untouched, so a gap that mixes + // protected + hard skips stays hard in either order. + let hardGapSinceCompressible = false; let msgIndex = -1; // #3192/#3205: numeric set of recorded-dead refs. A dropped message leaves a // hole in the ref map that array-adjacency segmentation bridges, so we break @@ -330,10 +349,17 @@ export function buildCompressibleRanges( for (const msg of messages) { msgIndex++; const ref = state.messageRefs.byRaw[msg.id]; - if (!ref || ref === "BLOCKED") continue; + if (!ref || ref === "BLOCKED") { + // No ref = no slot in the ref map: mergeRangesToThreshold sees an index + // hole here even though no skip flag fires. Record it as a hard gap so + // bridging never spans an unrefed message (its foldability is untested). + hardGapSinceCompressible = true; + continue; + } if (isSyntheticOrPruned(msg, covered)) { skipSinceCompressible = true; skipSinceProtected = true; + hardGapSinceCompressible = true; continue; } @@ -349,6 +375,7 @@ export function buildCompressibleRanges( if (isLiveCheckpointCarrier(msg, state)) { skipSinceCompressible = true; skipSinceProtected = true; + hardGapSinceCompressible = true; continue; } @@ -361,6 +388,7 @@ export function buildCompressibleRanges( if (hasUnrecoverableMediaPayload(msg)) { skipSinceCompressible = true; skipSinceProtected = true; + hardGapSinceCompressible = true; continue; } @@ -382,12 +410,16 @@ export function buildCompressibleRanges( }); skipSinceProtected = false; skipSinceCompressible = true; + // Protected-exchange skip: the ONLY bridge-able skip class (see the + // hardGapSinceCompressible doc). Leave hardGapSinceCompressible + // untouched — a protected gap following a hard gap stays hard. continue; } if (protectedZoneRefs?.has(ref)) { skipSinceCompressible = true; skipSinceProtected = true; + hardGapSinceCompressible = true; continue; } @@ -404,12 +436,14 @@ export function buildCompressibleRanges( const hi = Math.max(curRefNum, lastCompressibleRefNum); if (hi - lo > 1 && hasDeadRefBetween(deadRefNums, lo, hi)) { skipSinceCompressible = true; + hardGapSinceCompressible = true; } } compressibleMsgs.push({ id: msg.id, ref, gapBefore: skipSinceCompressible, + gapHard: hardGapSinceCompressible, tokens: countMessageTokens(msg, countTokens), chars: (msg.text ?? "").length, isTool: isToolMessage(msg), @@ -419,6 +453,7 @@ export function buildCompressibleRanges( if (curRefNum !== null) lastCompressibleRefNum = curRefNum; skipSinceCompressible = false; skipSinceProtected = true; + hardGapSinceCompressible = false; } // Foldability: the fold gate (src/compress.ts) withdraws every message whose @@ -444,15 +479,31 @@ export function buildCompressibleRanges( covered, ).withdrawn; if (unfoldedIds.size > 0) { + // Withdrawn members are SPAN-SAFE (apply withdraws them again, they stay + // visible — see the hardGapSinceCompressible doc): removing one merges the + // gap before it with the gap after it, and the merged gap stays span-safe + // iff both were. Chain the hardness across consecutive withdrawals; the + // surviving record inherits the combined classification. let gapPending = false; + let pendingHard = false; const kept: typeof compressibleMsgs = []; for (const info of compressibleMsgs) { if (unfoldedIds.has(info.id)) { gapPending = true; + pendingHard = pendingHard || info.gapHard; continue; } - kept.push(gapPending ? { ...info, gapBefore: true } : info); + kept.push( + gapPending + ? { + ...info, + gapBefore: true, + gapHard: pendingHard || info.gapHard, + } + : info, + ); gapPending = false; + pendingHard = false; } compressibleMsgs = kept; } @@ -474,6 +525,9 @@ export function buildCompressibleRanges( toolPct: first.isTool ? 100 : 0, textPct: first.isTool ? 0 : 100, userMsgs: first.isUser ? 1 : 0, + ...(first.gapBefore && !first.gapHard + ? { bridgeableGapBefore: true } + : {}), }; for (let i = 1; i < group.length; i++) { const info = group[i]!; @@ -566,6 +620,10 @@ function mergeBatch(batch: CompressibleRange[]): CompressibleRange { // position-based merging (formatRanges) still sees it after this batch is // re-emitted as a single range. if (first.noMergeBefore === true) merged.noMergeBefore = true; + // Bridge flag propagates the same way: a batch that BRIDGED into this + // range keeps the marker so any later re-merge sees the span crosses + // span-safe gaps (and only those — hard gaps never set it). + if (first.bridgeableGapBefore === true) merged.bridgeableGapBefore = true; return merged; } @@ -586,7 +644,22 @@ function rangeChars(r: CompressibleRange): number { * `minChars`. A sub-threshold tail is folded into the preceding batch * (overshoot allowed); if no batch precedes it, nothing is emitted — the * whole remainder is below the gate, so no selection of it can pass and - * offering it only yields guaranteed-rejected calls (billion-context #847). */ + * offering it only yields guaranteed-rejected calls (billion-context #847). + * + * Exception — span-safe gap bridging (production 2026-11-09 mega-turn): + * batches accumulate ACROSS a gap the apply side tolerates inside a spanning + * fold (`bridgeableGapBefore`: gaps made exclusively of protected-tool + * exchanges — carved at apply, filterProtectedToolMessages "Bug 39" — and/or + * integrity-withdrawn members — withdrawn again at apply, they stay visible) + * instead of closing at it. The per-CALL #847 gate sums chars over every + * range in the request, so a bridged span clears the gate that each island + * individually missed. Hard gaps (blocks, carriers, media, dead-ref holes, + * unrefed messages, soft zone — whose interior hits truncate the fold at the + * first protected message, #3953) still close the batch — their spans are + * doomed or carve-to-nothing (#498 / #2663). Sub-threshold remnants STILL + * emit nothing: the apply gate is per-call, but a lone island below it has + * no legal partner to ride with, and advertising it only yields the + * guaranteed-rejected call #847 exists to prevent. */ export function mergeRangesToThreshold( ranges: CompressibleRange[], minChars: number, @@ -596,10 +669,11 @@ export function mergeRangesToThreshold( let batch: CompressibleRange[] = []; let batchChars = 0; // Close the running batch at a boundary: emit it only when it alone clears - // the gate. A sub-threshold segment bounded by a gap cannot be offered (it - // fails the apply-side #847 gate) and must not be stretched across the gap - // to reach the threshold, or the emitted span covers non-compressible - // content (#498). + // the gate. A sub-threshold segment bounded by a hard gap cannot be offered + // (it fails the apply-side #847 gate) and must not be stretched across the + // gap to reach the threshold, or the emitted span covers non-compressible + // content (#498). A span-safe gap does NOT close the batch — the span + // bridges it and apply carves/withdraws the interior (see the header doc). const closeBatch = () => { if (batch.length > 0) { if (batchChars >= minChars) result.push(mergeBatch(batch)); @@ -610,17 +684,22 @@ export function mergeRangesToThreshold( for (const r of ranges) { // #3192/#3205: hard boundary at a dead-ref hole — never bridge across it, // even though the hole carries no array-index gap (see noMergeBefore). + // (A dead-ref gap is a hard gap, so bridgeableGapBefore is unset on + // ranges following one; the explicit check keeps that invariant + // authoritative even on hand-built ranges that set both.) if (r.noMergeBefore) closeBatch(); const prev = batch[batch.length - 1]; - // A physical array-index gap between the previous range and this one marks - // a block / protected / pruned boundary (buildCompressibleRanges leaves a - // slot hole there; user-turn splits stay contiguous). Without index data we - // keep the legacy accumulate-across-everything behavior. + // A physical array-index gap between the previous range and this one + // marks a block / covered / carrier / pruned boundary (buildCompressibleRanges + // leaves a slot hole there; user-turn splits stay contiguous). Without + // index data we keep the legacy accumulate-across-everything behavior — + // EXCEPT that a span-safe gap does not close the batch (bridge). if ( prev && prev.endIndex !== undefined && r.startIndex !== undefined && - r.startIndex > prev.endIndex + 1 + r.startIndex > prev.endIndex + 1 && + !r.bridgeableGapBefore ) { closeBatch(); } @@ -634,7 +713,8 @@ export function mergeRangesToThreshold( !batch[0]!.noMergeBefore && (prev.endIndex === undefined || batch[0]!.startIndex === undefined || - batch[0]!.startIndex <= prev.endIndex + 1); + batch[0]!.startIndex <= prev.endIndex + 1 || + batch[0]!.bridgeableGapBefore === true); if (contiguous) { result[result.length - 1] = mergeBatch([prev, ...batch]); } else if (batchChars >= minChars) { @@ -694,19 +774,34 @@ export function screenRecommendedUnits( // config-validation warn in processTurn); behavior is unchanged — // still return false, the unit is never advertised. if (!(err instanceof BoundaryNotFoundError)) { - console.warn( - `[acp-kernel] screenRecommendedUnits dropped unit ${unit.startRef}..${unit.endRef} after projection error: ${ + warnScreenDrop( + `dropped unit ${unit.startRef}..${unit.endRef} after projection error: ${ err instanceof Error ? err.message : String(err) }`, ); } return false; } - if (!projection || projection.fatal !== null) return false; + if (!projection || projection.fatal !== null) { + // Production 2026-11-09 mega-turn incident: whole-funnel zeroing is + // undiagnosable when units vanish here with no trace — the host only + // sees "kernel ranges=0" while its own message-level diagnostic still + // counts collectible material. Warn (budgeted, same channel as the + // projection-error warn above) so a total screen wipe is attributable. + if (projection?.fatal) { + warnScreenDrop( + `dropped unit ${unit.startRef}..${unit.endRef}: ${projection.fatal.error.message}`, + ); + } + return false; + } if ( projection.filteredIds.length === 0 && projection.consumedBlockIds.length === 0 ) { + warnScreenDrop( + `dropped unit ${unit.startRef}..${unit.endRef}: nothing foldable in span (filteredIds=0, consumedBlocks=0)`, + ); return false; } // #3371: the apply lane's livelock guard (compress.ts, pi#199) throws on @@ -725,8 +820,30 @@ export function screenRecommendedUnits( !projection.isBlockBoundary && evaluateRefold(state, unit).kind !== "refold" ) { + warnScreenDrop( + `dropped unit ${unit.startRef}..${unit.endRef}: consumes only active block(s), not a refold`, + ); return false; } return true; }); } + +// Budgeted warn channel for screen-stage drops: the first SCREEN_DROP_WARN_BUDGET +// drops in the process are logged verbatim, afterwards only every Nth (a run of +// doomed units over a long session must stay observable without flooding the +// host log — the production incident produced 28 no-range events/day). Exported +// for the recommend pipeline node's funnel-zero warn (same channel, same budget). +const SCREEN_DROP_WARN_BUDGET = 40; +const SCREEN_DROP_WARN_STRIDE = 100; +let screenDropWarnCount = 0; +export function warnScreenDrop(detail: string): void { + screenDropWarnCount += 1; + if ( + screenDropWarnCount > SCREEN_DROP_WARN_BUDGET && + screenDropWarnCount % SCREEN_DROP_WARN_STRIDE !== 0 + ) { + return; + } + console.warn(`[acp-kernel] screenRecommendedUnits ${detail}`); +} diff --git a/kernel/src/types.ts b/kernel/src/types.ts index 7e7b4f4699..6ba7d8de0b 100644 --- a/kernel/src/types.ts +++ b/kernel/src/types.ts @@ -708,6 +708,22 @@ export interface CompressibleRange { * has no index gap and the index check alone would let mergeRangesToThreshold * bridge back into one doomed span bracketing the dead ref. */ noMergeBefore?: boolean; + /** buildCompressibleRanges sets this when the gap immediately before this + * range is SPAN-SAFE: composed exclusively of members the apply side + * tolerates INSIDE a spanning fold — protected-tool exchanges + * (config.protectedTools / always-protected list, carved by + * filterProtectedToolMessages, "Bug 39") and integrity-withdrawn members + * (the fold gate withdraws them again at apply; they stay visible, #564 + * reverse-split semantics). mergeRangesToThreshold bridges across such + * gaps so sub-gate islands can accumulate to minCompressRange instead of + * all dying below it (production 2026-11-09: a mega-turn with a protected + * exchange every ~4 rounds fragmented every island to 3-5K chars, + * mergeRangesToThreshold dropped them all → recommendedRanges=[] → the + * host's emergency lane fell back to irreversible physical pruning while + * the host's four-bucket diagnostic still showed 84 "collectible" + * messages). Hard gaps (blocks, carriers, media, dead refs, unrefed + * messages, soft zone) never set this — they keep splitting ranges. */ + bridgeableGapBefore?: boolean; } export interface ProtectedRange { diff --git a/kernel/src/unit-projection.ts b/kernel/src/unit-projection.ts index 428d3eb672..690015731c 100644 --- a/kernel/src/unit-projection.ts +++ b/kernel/src/unit-projection.ts @@ -165,10 +165,17 @@ export function projectUnit(input: UnitProjectionInput): UnitProjection { } } } - const adjustedIds = applyPairBoundaryAdjustments( + const adjustedIds = clampAdjustmentAtCoverage( + applyPairBoundaryAdjustments(resolved, input.messages, shared), resolved, input.messages, - shared, + // Fresh folds pass the active coverage; hand-built inputs without it + // (unit tests) keep the legacy unclamped adjustment behavior. + input.preExistingCoverage, + // #5337 discipline: reuse the shared index map — the clamp runs on every + // screened unit, and a per-call map rebuild would resurface the O(units×n) + // quadratic the shared context exists to prevent. + byId, ); const skippedCarrierIds = adjustedIds.filter((id) => liveCarrierIds.has(id)); if (skippedCarrierIds.length > 0) { @@ -916,6 +923,89 @@ function applyPairBoundaryAdjustments( return ids; } +/** + * Clamp the pair-boundary ADJUSTMENT's widened head/tail at active-coverage + * boundaries (production 2026-11-09 mega-turn incident). + * + * When a range ends (or starts) right where a block was cut mid-group — a + * spine-cut/fold boundary slicing between a round's reasoning and its + * tool-call — the pair/reasoning adjustment widens the range ACROSS the + * boundary to close the pair, pulling in already-covered members. The drag + * makes the block visible in range → nestedBlock/consumed machinery engages + * → the merge-integrity path resolves the unit to fold-nothing (fatal=null, + * filteredIds=0) or a split fatal: every range ending adjacent to a mid-group + * block cut dies at screening. This predates the merge-gate bridging fix — + * bridging only exposed it, by finally letting units reach the screen with + * that geometry. + * + * Clamping is safe precisely because the partner half is COVERED: #5531 makes + * covered halves invisible to the integrity judge (a covered call cannot + * strand its turn's reasoning; a covered result leaves no unanswered + * tool_call_id), so folding the visible part while the covered partner stays + * covered is the allowed reverse-split shape. The requested span itself is + * NEVER clamped — only the adjustment's widening — so hand-built ranges that + * deliberately target covered spans (refold candidates) keep resolving. + */ +function clampAdjustmentAtCoverage( + adjustedIds: string[], + resolved: { startIndex: number; endIndex: number; messageIds: string[] }, + messages: CoreMessage[], + coverage?: ReadonlySet<string>, + /** Shared id→index map (#5337): reuse the caller's O(n) derivation instead + * of rebuilding it per call on the screening hot path. */ + indexById?: Map<string, number>, +): string[] { + if (!coverage || coverage.size === 0 || adjustedIds.length === 0) { + return adjustedIds; + } + // Recover the adjusted span's index bounds (ids are contiguous by index — + // applyPairBoundaryAdjustments pushes by loop index). + let startIdx: number | undefined; + let endIdx: number | undefined; + if (indexById) { + startIdx = indexById.get(adjustedIds[0]!); + endIdx = indexById.get(adjustedIds[adjustedIds.length - 1]!); + } else { + const built = new Map<string, number>(); + for (let i = 0; i < messages.length; i++) { + const id = messages[i]?.id; + if (id !== undefined) built.set(id, i); + } + startIdx = built.get(adjustedIds[0]!); + endIdx = built.get(adjustedIds[adjustedIds.length - 1]!); + } + if (startIdx === undefined || endIdx === undefined) return adjustedIds; + const isCovered = (i: number): boolean => { + const id = messages[i]?.id; + return id !== undefined && coverage.has(id); + }; + // Pull the widened TAIL back to just before the first covered member, and + // the widened HEAD forward likewise. Only the WIDENED portion is clamped — + // covered ids inside the requested span stay (refold/consumed semantics). + let clamped = false; + for (let i = resolved.endIndex + 1; i <= endIdx; i++) { + if (isCovered(i)) { + endIdx = i - 1; + clamped = true; + break; + } + } + for (let i = resolved.startIndex - 1; i >= startIdx; i--) { + if (isCovered(i)) { + startIdx = i + 1; + clamped = true; + break; + } + } + if (!clamped) return adjustedIds; + const ids: string[] = []; + for (let i = startIdx; i <= endIdx; i++) { + const msg = messages[i]; + if (msg) ids.push(msg.id); + } + return ids; +} + function filterProtectedToolMessages( directMessageIds: string[], messages: CoreMessage[], diff --git a/kernel/tests/issue5531-stranded-reasoning-residue.test.ts b/kernel/tests/issue5531-stranded-reasoning-residue.test.ts index 4a84ab26a9..ffd7ca9c04 100644 --- a/kernel/tests/issue5531-stranded-reasoning-residue.test.ts +++ b/kernel/tests/issue5531-stranded-reasoning-residue.test.ts @@ -195,9 +195,15 @@ describe("#5531 recommend: stranded reasoning+text are advertised again", () => }); assert.deepEqual(res.result.errors, [], JSON.stringify(res.result.errors)); assert.ok(res.result.blocksCreated >= 1, "a recovery block must be created"); - const block = res.state.blocks.find((b) => b.active && b !== state.blocks[0]); - assert.ok(block, "new block distinct from the seed block"); - assert.ok(block.effectiveMessageIds.includes("r")); + // The recovery block is the one covering the stranded reasoning. The seed + // block (call+result only) may stay active alongside it: ranges ENDING at + // a mid-group block cut are clamped at the coverage boundary instead of + // dragging the seed in as consumed (unit-projection clampAdjustmentAtCoverage), + // so "distinct from the seed" is no longer the reliable discriminator here. + const block = res.state.blocks.find( + (b) => b.active && b.effectiveMessageIds.includes("r"), + ); + assert.ok(block, "a recovery block covering the stranded reasoning must exist"); assert.ok(block.effectiveMessageIds.includes("t")); }); }); diff --git a/kernel/tests/megaturn-protected-gap-bridging.test.ts b/kernel/tests/megaturn-protected-gap-bridging.test.ts new file mode 100644 index 0000000000..3604b7deff --- /dev/null +++ b/kernel/tests/megaturn-protected-gap-bridging.test.ts @@ -0,0 +1,246 @@ +// Regression pins for the production 2026-11-09 mega-turn no-ranges incident. +// +// Shape: a mega-turn whose rounds are interleaved with protected-tool +// exchanges (config.protectedTools — todo/tool_learn/…), with an active block +// cut mid-group through the middle. The protected exchanges + integrity +// withdrawals fragment the compressible surface into islands of ~3-5K chars — +// each individually BELOW the apply-side minCompressRange gate, and (because +// the apply gate sums chars per CALL, compress.ts #847) perfectly legal to +// fold together. Pre-fix, mergeRangesToThreshold dropped every sub-gate island +// → recommendedRanges=[] → the host's emergency lane fell back to irreversible +// physical pruning while its four-bucket diagnostic still counted 84 +// "collectible" messages. Three pins: +// 1. mergeRangesToThreshold BRIDGES across span-safe gaps so islands +// accumulate to the gate (and still never bridges a hard gap); +// 2. the bridged unit SURVIVES screening — projectUnit clamps the +// pair-boundary adjustment at the coverage boundary instead of dragging +// the adjacent block in as consumed (the second, independent killer); +// 3. the whole recommend chain advertises ≥1 range for the incident shape. + +import { describe, it } from "node:test"; +import assert from "node:assert/strict"; +import { + computeProtectedRefs, + buildCompressibleRanges, + mergeRangesToThreshold, + screenRecommendedUnits, +} from "../src/recommend.js"; +import { createInitialState } from "../src/state.js"; +import { assignRefs } from "../src/refs.js"; +import { defaultConfig } from "../src/config.js"; +import { defaultCountTokens } from "../src/tokenize.js"; +import type { + CompressibleRange, + CompressionState, + Config, + CoreMessage, +} from "../src/types.js"; + +const CJK = "上下文压缩水位判定折叠范围构建完整性撤回保护区覆盖"; +const cjk = (n: number): string => + Array.from({ length: n }, (_, i) => CJK[i % CJK.length]).join(""); + +const PROTECTED_TOOLS = [ + "tool_learn", + "skill_learn", + "todo", + "compress", + "acp_status", + "search_context", + "absorb", + "recall", +]; +const COMMON = ["read", "run_code", "glob", "grep", "browser_evaluate", "write"]; + +function config(): Config { + return defaultConfig(112000, { + protectedTools: [...PROTECTED_TOOLS], + preserveRecentMessages: 8, + preserveRecentTokens: 8000, + nudge: { ...defaultConfig(112000).nudge, maxContextLimitPct: 0.8 }, + }); +} + +/** 50 small rounds; every 4th round's exchange rides a protected tool; the + * newest 24 messages are unrefed (state persist lag, as in production). */ +function buildIncident(): { + messages: CoreMessage[]; + state: CompressionState; +} { + const messages: CoreMessage[] = []; + messages.push({ id: "m00001", role: "user", contentType: "text", text: cjk(600) }); + let n = 1; + const nid = () => `m${String(++n).padStart(5, "0")}`; + for (let r = 0; r < 50; r++) { + messages.push({ + id: nid(), + role: "assistant", + contentType: "reasoning", + text: cjk(250 + ((r * 137) % 450)), + }); + if (r % 2 === 0) { + messages.push({ id: nid(), role: "assistant", contentType: "text", text: cjk(60 + ((r * 61) % 150)) }); + } + const isProt = r > 2 && r % 4 === 0; + const tool = isProt + ? (["todo", "tool_learn", "search_context", "skill_learn"] as const)[r % 4]! + : COMMON[(r * 7) % COMMON.length]!; + const callId = `tc-${r}`; + messages.push({ + id: nid(), + role: "assistant", + contentType: "tool-call", + toolName: tool, + toolCallId: callId, + text: `{"r":${r}}`, + }); + messages.push({ + id: nid(), + role: "tool", + contentType: "tool-result", + toolName: tool, + toolCallId: callId, + text: cjk(200 + ((r * 331) % 1600)), + }); + } + const state = createInitialState(); + const noRefIds = new Set(messages.slice(-24).map((m) => m.id!)); + state.messageRefs = assignRefs(messages, { + existing: state.messageRefs, + nextIndex: 1, + shouldSkip: (m) => noRefIds.has(m.id!), + }).map; + // Active block cut mid-group through the middle (the earlier growth-lane + // fold): start on a compressible message so the round's call/result stay + // covered while the reasoning stays visible. + let startIdx = 30; + while ( + startIdx < messages.length && + messages[startIdx]!.contentType === "tool-result" + ) { + startIdx++; + } + const ids = messages.slice(startIdx, startIdx + 86).map((m) => m.id!); + state.blocks = [ + { + blockId: "b1", + runId: "r1", + tier: 1, + topic: "mid", + summary: cjk(400), + directMessageIds: ids, + effectiveMessageIds: ids, + directBlockIds: [], + compressedTokens: 40000, + createdAt: Date.now(), + survivedCount: 0, + generation: 0, + active: true, + }, + ]; + return { messages, state }; +} + +function minChars(cfg: Config): number { + return cfg.compress.minCompressRange; +} + +describe("mega-turn protected-gap bridging (2026-11-09 incident)", () => { + it("buildCompressibleRanges flags protected-only gaps as bridgeable", () => { + const { messages, state } = buildIncident(); + const cfg = config(); + const protectedRefs = computeProtectedRefs(messages, state, cfg, defaultCountTokens); + const ranges = buildCompressibleRanges(messages, state, cfg, protectedRefs, defaultCountTokens, messages); + assert.ok(ranges.compressible.length > 0, "islands must exist"); + const bridged = ranges.compressible.filter((r) => r.bridgeableGapBefore === true); + assert.ok(bridged.length > 0, "at least one island follows a span-safe gap"); + }); + + it("mergeRangesToThreshold bridges span-safe gaps so islands clear the gate", () => { + const { messages, state } = buildIncident(); + const cfg = config(); + const gate = minChars(cfg); + const protectedRefs = computeProtectedRefs(messages, state, cfg, defaultCountTokens); + const ranges = buildCompressibleRanges(messages, state, cfg, protectedRefs, defaultCountTokens, messages); + // Precondition of the incident: islands exist below the gate. + const subGate = ranges.compressible.filter((r) => (r.chars ?? 0) < gate); + assert.ok(subGate.length > 0, "fixture must produce sub-gate islands"); + const merged = mergeRangesToThreshold(ranges.compressible, gate); + assert.ok( + merged.length > 0, + "bridged islands must accumulate past the merge gate (pre-fix: all dropped)", + ); + for (const unit of merged) { + assert.ok( + (unit.chars ?? 0) >= gate, + `every emitted unit clears the gate, got ${unit.chars}`, + ); + } + }); + + it("mergeRangesToThreshold still refuses to bridge a hard gap", () => { + // Two islands separated by a block-coverage gap: no bridge flag on the + // second island → sub-gate islands on both sides stay unmerged. + const island = (startIdx: number): CompressibleRange => ({ + startRef: `m${String(startIdx).padStart(5, "0")}`, + endRef: `m${String(startIdx + 2).padStart(5, "0")}`, + startIndex: startIdx, + endIndex: startIdx + 2, + count: 3, + tokens: 900, + chars: 2000, + toolPct: 50, + textPct: 50, + userMsgs: 0, + }); + const merged = mergeRangesToThreshold([island(10), island(40)], 5000); + assert.deepEqual(merged, [], "hard-gapped sub-gate islands must not merge"); + }); + + it("the bridged unit survives screening next to a mid-group block cut", () => { + const { messages, state } = buildIncident(); + const cfg = config(); + const gate = minChars(cfg); + const protectedRefs = computeProtectedRefs(messages, state, cfg, defaultCountTokens); + const ranges = buildCompressibleRanges(messages, state, cfg, protectedRefs, defaultCountTokens, messages); + const merged = mergeRangesToThreshold(ranges.compressible, gate); + assert.ok(merged.length > 0); + const screened = screenRecommendedUnits(merged, messages, state, cfg, protectedRefs); + assert.ok( + screened.length > 0, + "bridged unit must survive projectUnit (clamp keeps the adjacent block out of the span)", + ); + // The surviving span must not reach into covered territory: its END stays + // on the visible side of the mid-group block cut. + const covered = new Set<string>(); + for (const b of state.blocks ?? []) { + if (b.active) for (const id of b.effectiveMessageIds ?? []) covered.add(id); + } + const indexById = new Map<string, number>(); + messages.forEach((m, i) => indexById.set(m.id!, i)); + for (const unit of screened) { + const endIdx = indexById.get(unit.endRef); + assert.ok(endIdx !== undefined, "endRef resolves to an array slot"); + assert.ok( + !covered.has(messages[endIdx!]!.id!), + "span end stays on the visible side of the block boundary", + ); + } + }); + + it("full recommend chain advertises ranges for the incident shape", () => { + const { messages, state } = buildIncident(); + const cfg = config(); + const gate = minChars(cfg); + const protectedRefs = computeProtectedRefs(messages, state, cfg, defaultCountTokens); + const ranges = buildCompressibleRanges(messages, state, cfg, protectedRefs, defaultCountTokens, messages); + const merged = mergeRangesToThreshold(ranges.compressible, gate); + const screened = screenRecommendedUnits(merged, messages, state, cfg, protectedRefs); + assert.ok( + screened.length > 0, + "pre-fix this funnel returned [] and the host fell back to physical pruning", + ); + const viable = screened.filter((r) => r.tokens >= 200); + assert.ok(viable.length > 0, "at least one unit clears the 200-token viable floor"); + }); +}); From 1559254ff7e86d0b47826bae4b75234018e1301e Mon Sep 17 00:00:00 2001 From: sorasoras <sorasoras@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:53:01 +0800 Subject: [PATCH 59/78] fix(generation-boundary): arm the native-compaction rebase on a proven ref-ledger reset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit detectGenerationBoundary() already proves that the persisted ref ledger belongs to a previous generation: >=2 distinct above-frontier citations at request #1 (#4305). It only recorded a standing note, so the compression ledger was never repaired. Observed on 0.1.212 with a ZCode client (#6061, #5589): acp_status advertised the current generation (highest allocated ref m00314) while compress validated against the stale map (highest allocated ref m159141, 586 blocks) and rejected every live ref with "every ref is unknown to this session [cause: stale-ref]" — 20 consecutive failures, circuit breaker armed, and the nudge kept offering the same unreachable ranges. The detector's verdict is exactly the evidence the native-compaction rebase has always required, so arm it there: markNativeCompactionBoundary() sets pendingRebase, and the next request runs resetSessionCompression() + reserveRefsThrough(), re-anchoring the map onto the generation the client is actually minting. Also honour a pending boundary at the head of the anthropic lane. Its rebase call sits behind the dsh/codex client-side-compaction signature, which a plain model or upstream switch never produces, so an armed boundary would otherwise wait forever on that lane (openai/google/responses reach reconcile through their own branches; responses already reconciles unconditionally). Verified: npm run typecheck clean; tests/generation-boundary.test.ts 12/12. (cherry picked from commit 7a1acae712e7429997155088113a78805213b1fb) --- src/generation-boundary.ts | 23 +++++++++++++++++------ src/server/prepare-anthropic.ts | 9 +++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/generation-boundary.ts b/src/generation-boundary.ts index 330d82f001..55f67cbee9 100644 --- a/src/generation-boundary.ts +++ b/src/generation-boundary.ts @@ -20,13 +20,16 @@ // renders for the session's lifetime — static text, byte-stable across turns, // prefix-cache safe after the (typically never-visible) one-time append. // -// Deliberately display-only: no storage rewrite, no ref-space surgery, no gate -// changes. The text carries no numbers: the frontier advances as the session -// allocates, and a fixed ceiling would go stale within turns. Concrete ceilings -// stay with the reactive notes (beyondFrontierNote / frontierClampNote), which -// fire on the actual failure and always quote the live value. +// The note itself stays display-only and numberless: the frontier advances as +// the session allocates, and a fixed ceiling would go stale within turns — +// concrete ceilings stay with the reactive notes (beyondFrontierNote / +// frontierClampNote), which fire on the actual failure and always quote the live +// value. Detection, however, now also ARMS the native-compaction rebase: proving +// the ledger belongs to a previous generation is exactly the evidence that rebase +// needed, and annotating it without repairing it leaves compress rejecting every +// live ref for the rest of the session (#6061, #5589). -import { markDirty, type Session } from "./session.js"; +import { markDirty, markNativeCompactionBoundary, type Session } from "./session.js"; import type { CoreMessage } from "acp-kernel"; /** Session-metadata key for the one-shot detection record. */ @@ -127,6 +130,14 @@ export function detectGenerationBoundary( }; (session.metadata ?? (session.metadata = {}))[STORAGE_KEY] = record; markDirty(session); + // #6061/#5589: this verdict is also the proof the native-compaction rebase has + // always required — the persisted ref ledger provably belongs to the previous + // generation. Arm it here so the next request runs resetSessionCompression + + // reserveRefsThrough and re-anchors the map onto the generation the client is + // actually minting. Without this the note only warns the model away from refs + // that compress would otherwise reject for the rest of the session (nudge and + // tool disagreeing about "highest allocated ref" is the same defect). + markNativeCompactionBoundary(session); log?.("info", `[${session.id}] [acp-gen-boundary] ${stale.size} distinct ref citation(s) above this session's frontier (${record.frontier || "no mapped refs yet"}) found in visible history — previous-generation residue confirmed; standing generation-boundary note active (highest stale ${record.maxStaleRef}) (#4305)`); return true; } diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index a7b1908856..51a28bd890 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -327,6 +327,15 @@ export async function prepareAnthropic( // intact history has no gap; a gap without framing falls through to // detectUnannouncedHistoryRewrite below). Title-gen side requests are // skipped like on the openai lane (same tiny-budget heuristic). + // #6061: honour a boundary armed on an earlier request. The detector in + // generation-boundary.ts arms it when stale above-frontier citations prove + // the persisted ledger is from a previous generation; the dsh branch below + // only fires on a client-side compaction signature, which a plain + // model/upstream switch does not produce — that left the two surfaces (nudge + // vs compress) permanently disagreeing about "highest allocated ref". + if (reconcileNativeCompactionBoundary(session)) { + log("info", `[${sessionId}] [acp-gen-boundary] ref ledger belongs to a previous generation — ACP compression state rebased onto the current one`); + } let dshRebased = false; const anthropicTitleGen = typeof parsed.max_tokens === "number" && parsed.max_tokens <= 200; if (!anthropicTitleGen && laneDecisions.profile.compactsClientSide && session.state.blocks.some((b) => b.active)) { From 7a2376d89536de498b3cf8eecdca8ab2b9833296 Mon Sep 17 00:00:00 2001 From: raydocs <139067258+raydocs@users.noreply.github.com> Date: Thu, 12 Nov 2026 04:04:26 -0700 Subject: [PATCH 60/78] kernel: heal unpaired server_tool_use / *_tool_result blocks before forwarding Server tools (advisor, web_search, code_execution, ...) carry both halves of an exchange inside one assistant message. anthropicToCore splits every block into its own BiliMessage, so a fold can cover one half and leave the other; Anthropic then rejects the whole request: messages.5.content.0: unexpected tool_use_id found in advisor_tool_result blocks: srvtoolu_... Each advisor_tool_result block must have a corresponding server_tool_use block in the same message. coreToAnthropic now drops a *_tool_result without its server_tool_use (and a server_tool_use without its result, except in the last message) within the same assistant message. Legal shapes round-trip byte-identical; plain tool_use/tool_result is untouched. Bumps acp-kernel to 0.0.196. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit ca177b9c7402c1ec39614d0dbb933b639d354070) --- kernel/src/wire/anthropic.ts | 80 +++++++++++ kernel/tests/server-tool-pair-heal.test.ts | 160 +++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 kernel/tests/server-tool-pair-heal.test.ts diff --git a/kernel/src/wire/anthropic.ts b/kernel/src/wire/anthropic.ts index ef070160d5..c99537e6e2 100644 --- a/kernel/src/wire/anthropic.ts +++ b/kernel/src/wire/anthropic.ts @@ -511,6 +511,85 @@ export function healAnthropicSystemPositions( return out; } +/** Server-tool pairing backstop. Server-side tools (advisor, web_search, + * code_execution, …) put BOTH halves of an exchange inside one assistant + * message: a `server_tool_use` block and its `<name>_tool_result` block + * (paired by `tool_use_id` → `id`). anthropicToCore splits every block into + * its own BiliMessage (unknown types ride the rawAnthropicBlock sidecar), so + * a fold can cover one half and leave the other. Anthropic then rejects the + * WHOLE request, permanently, e.g. + * 'messages.5.content.0: unexpected tool_use_id found in advisor_tool_result + * blocks: srvtoolu_… Each advisor_tool_result block must have a + * corresponding server_tool_use block in the same message'. + * Field repro: claude-code + advisor tool, bili v0.1.213 / acp-kernel 0.0.195. + * + * Heal: per assistant message, drop a `*_tool_result` whose `tool_use_id` + * has no `server_tool_use` in the same message, and a `server_tool_use` + * without its result — except in the LAST message, where an open server + * call is legal client state. Plain `tool_use`/`tool_result` are never + * touched (their pairing spans user/assistant and has its own contract). + * A message emptied by the heal gets a placeholder text block so roles stay + * alternating. Legal messages are left as the same object (no churn for the + * prefix cache); only messages that would 400 anyway are rewritten, so the + * thinking-signature rule (KDD #10) never loses a request that would have + * succeeded. Returns the number of dropped blocks. */ +function isServerToolResultBlock(b: { type?: unknown }): boolean { + return ( + typeof b.type === "string" && + b.type !== "tool_result" && + b.type.endsWith("_tool_result") + ); +} + +const SERVER_TOOL_FOLDED_PLACEHOLDER = "(server tool exchange folded)"; + +function healUnpairedServerToolBlocks(out: AnthropicWireMessage[]): number { + let dropped = 0; + for (let mi = 0; mi < out.length; mi++) { + const msg = out[mi]!; + if (msg.role !== "assistant" || !Array.isArray(msg.content)) continue; + const blocks = msg.content as unknown as Array<Record<string, unknown>>; + const uses = new Set<string>(); + const results = new Set<string>(); + for (const b of blocks) { + if (!b || typeof b !== "object") continue; + if (b.type === "server_tool_use" && typeof b.id === "string") + uses.add(b.id); + else if (isServerToolResultBlock(b) && typeof b.tool_use_id === "string") + results.add(b.tool_use_id); + } + if (uses.size === 0 && results.size === 0) continue; + const isLast = mi === out.length - 1; + const kept = blocks.filter((b) => { + if (!b || typeof b !== "object") return true; + if ( + b.type === "server_tool_use" && + typeof b.id === "string" && + !results.has(b.id) && + !isLast + ) + return false; + if ( + isServerToolResultBlock(b) && + typeof b.tool_use_id === "string" && + !uses.has(b.tool_use_id) + ) + return false; + return true; + }); + if (kept.length === blocks.length) continue; + dropped += blocks.length - kept.length; + out[mi] = { + role: "assistant", + content: + kept.length > 0 + ? (kept as unknown as AnthropicBlock[]) + : [{ type: "text", text: SERVER_TOOL_FOLDED_PLACEHOLDER }], + }; + } + return dropped; +} + export function coreToAnthropic( messages: BiliMessage[], cacheControls?: Map<string, unknown>, @@ -584,6 +663,7 @@ export function coreToAnthropic( } } flush(); + healUnpairedServerToolBlocks(out); healIllegalSystemPositions(out); return out; } diff --git a/kernel/tests/server-tool-pair-heal.test.ts b/kernel/tests/server-tool-pair-heal.test.ts new file mode 100644 index 0000000000..50228a23f2 --- /dev/null +++ b/kernel/tests/server-tool-pair-heal.test.ts @@ -0,0 +1,160 @@ +// Server-tool pairing backstop in coreToAnthropic. Server tools (advisor, +// web_search, …) carry server_tool_use + <name>_tool_result inside ONE +// assistant message; anthropicToCore splits each block into its own +// BiliMessage, so a fold can drop one half. Anthropic then 400s the whole +// request: "unexpected tool_use_id found in advisor_tool_result blocks … +// Each advisor_tool_result block must have a corresponding server_tool_use +// block in the same message". Positive AND negative rows (#3617): orphans +// are healed; every legal shape round-trips byte-identical. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { anthropicToCore, coreToAnthropic } from "../src/wire/anthropic.js"; +import type { AnthropicWireMessage } from "../src/wire/anthropic.js"; + +const use = (id: string) => ({ + type: "server_tool_use", + id, + name: "advisor", + input: {}, +}); +const res = (id: string) => ({ + type: "advisor_tool_result", + tool_use_id: id, + content: { type: "advisor_result", text: "advice" }, +}); +const wire = (messages: unknown[]) => messages as AnthropicWireMessage[]; +const roundTrip = (messages: AnthropicWireMessage[]) => { + const { msgs, cacheControls } = anthropicToCore({ + model: "m", + max_tokens: 1, + messages, + } as never); + return { msgs, out: coreToAnthropic(msgs, cacheControls) }; +}; + +test("legal server-tool exchanges round-trip byte-identical (negative rows)", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q1" }] }, + { + role: "assistant", + content: [ + use("srvtoolu_a"), + res("srvtoolu_a"), + { type: "text", text: "a1" }, + ], + }, + { role: "user", content: [{ type: "text", text: "q2" }] }, + { + role: "assistant", + content: [ + { type: "text", text: "searching" }, + { + type: "server_tool_use", + id: "srvtoolu_w", + name: "web_search", + input: { query: "x" }, + }, + { + type: "web_search_tool_result", + tool_use_id: "srvtoolu_w", + content: [], + }, + { type: "text", text: "a2" }, + ], + }, + { role: "user", content: [{ type: "text", text: "q3" }] }, + // Open server call in the LAST message is legal client state — kept. + { role: "assistant", content: [use("srvtoolu_open")] }, + ]); + assert.deepEqual(roundTrip(messages).out, messages); +}); + +test("plain tool_use / tool_result pairs are never touched", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q" }] }, + { + role: "assistant", + content: [{ type: "tool_use", id: "toolu_1", name: "Bash", input: {} }], + }, + { + role: "user", + content: [{ type: "tool_result", tool_use_id: "toolu_1", content: "ok" }], + }, + ]); + assert.deepEqual(roundTrip(messages).out, messages); +}); + +test("result whose server_tool_use was folded away is dropped", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q1" }] }, + { + role: "assistant", + content: [ + use("srvtoolu_a"), + res("srvtoolu_a"), + { type: "text", text: "a1" }, + ], + }, + { role: "user", content: [{ type: "text", text: "q2" }] }, + ]); + const { msgs } = roundTrip(messages); + // Simulate a fold that covered only the server_tool_use block. + const folded = msgs.filter( + (m) => + !(m.rawAnthropicBlock as { type?: string } | undefined)?.type?.startsWith( + "server_tool_use", + ), + ); + assert.equal(folded.length, msgs.length - 1); + const out = coreToAnthropic(folded); + assert.deepEqual(out[1], { + role: "assistant", + content: [{ type: "text", text: "a1" }], + }); +}); + +test("server_tool_use whose result was folded away is dropped (not in last message)", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q1" }] }, + { + role: "assistant", + content: [ + { type: "text", text: "a0" }, + use("srvtoolu_a"), + res("srvtoolu_a"), + ], + }, + { role: "user", content: [{ type: "text", text: "q2" }] }, + ]); + const { msgs } = roundTrip(messages); + const folded = msgs.filter( + (m) => + (m.rawAnthropicBlock as { type?: string } | undefined)?.type !== + "advisor_tool_result", + ); + const out = coreToAnthropic(folded); + assert.deepEqual(out[1], { + role: "assistant", + content: [{ type: "text", text: "a0" }], + }); +}); + +test("an assistant message emptied by the heal keeps role alternation via placeholder", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q1" }] }, + { role: "assistant", content: [use("srvtoolu_a"), res("srvtoolu_a")] }, + { role: "user", content: [{ type: "text", text: "q2" }] }, + ]); + const { msgs } = roundTrip(messages); + const folded = msgs.filter( + (m) => + (m.rawAnthropicBlock as { type?: string } | undefined)?.type !== + "server_tool_use", + ); + const out = coreToAnthropic(folded); + assert.equal(out.length, 3); + assert.equal(out[1]!.role, "assistant"); + assert.deepEqual(out[1]!.content, [ + { type: "text", text: "(server tool exchange folded)" }, + ]); +}); From d19da23c74d582361db750e5d4c5554efd22b90a Mon Sep 17 00:00:00 2001 From: xiaozhu <nihaoaheheda@gmail.com> Date: Thu, 12 Nov 2026 23:33:12 +0800 Subject: [PATCH 61/78] fix: teach direct ACP calls for Codex exec namespaces (cherry picked from commit 28048cad8c25e357a6b794ba2eb4acc5688dd82a) --- src/server/inject.ts | 30 +++++++++++++- tests/lane-decisions-matrix.test.ts | 5 +++ tests/ptc-runcode-tools.test.ts | 61 ++++++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/server/inject.ts b/src/server/inject.ts index 1ea1fab6e3..218488ac97 100644 --- a/src/server/inject.ts +++ b/src/server/inject.ts @@ -5,11 +5,35 @@ import { type AnthropicRequestBody, type GoogleFunctionDeclaration, type GoogleT import { appendSystemText } from "../util.js"; import { absorbEnabled, absorbToolName } from "../absorb.js"; import { blockOpsEnabled } from "../acp-block-ops.js"; -import { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, packOwnsDescription, ptcNoteMode, withAcpNotes, withAcpNotesOpenAI, withMarkerIntegrityNote, withMcpDialectNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { PROXY_TOOL_NAMES, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, packOwnsDescription, ptcNoteMode, withAcpNotes, withAcpNotesOpenAI, withMarkerIntegrityNote, withMcpDialectNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; import { conversationPromptSections, externalSummaryEnabled, messageTimestampSections, withExternalSummaryTools } from "../external-summary-surface.js"; import { forceTextProtocol as knobForceTextProtocol, renderNone as knobRenderNone } from "../knobs.js"; import { GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; +const CODEX_EXEC_EXEMPT_NOTE = " This proxy-provided tool has no functions.exec SDK binding: call it DIRECTLY as a normal tool call using its declared name and namespace. Do not invoke it through tools.* inside functions.exec; the proxy executes the direct call server-side."; + +// Hint detection must not widen the shared omnibus reachability gate (#5592). +function hasCodexExec(tools: readonly unknown[], namespace = ""): boolean { + for (const value of tools) { + if (value === null || typeof value !== "object") continue; + const tool = value as { type?: unknown; name?: unknown; tools?: unknown }; + const name = typeof tool.name === "string" ? tool.name : ""; + const qualifiedName = namespace ? `${namespace}.${name}` : name; + if ((tool.type === "custom" || tool.type === "function") && qualifiedName === "functions.exec") return true; + if (!Array.isArray(tool.tools)) continue; + if (tool.type === "namespace" && name && hasCodexExec(tool.tools, qualifiedName)) return true; + if (tool.type === "additional_tools" && hasCodexExec(tool.tools, namespace)) return true; + } + return false; +} + +function withCodexExecNotes<T extends { name?: string; description?: string }>(tools: readonly T[], skip: ReadonlySet<string>): T[] { + return tools.map((tool) => typeof tool.name === "string" && PROXY_TOOL_NAMES.has(tool.name) + && !skip.has(tool.name) && typeof tool.description === "string" + ? { ...tool, description: tool.description + CODEX_EXEC_EXEMPT_NOTE } + : tool); +} + export function injectSystem( parsed: AnthropicRequestBody, opts: ProxyOptions, @@ -122,6 +146,8 @@ export function injectResponsesTool(tools: unknown[] | undefined, toolsToAdd: re // #920 rule via mergeOwnedTools: bili owns these names (see core above). // #3252: exempt note when the client tools are run_code-shaped. const mode = ptcNoteMode((tools ?? []).map((t) => (t as { name?: string })?.name)); - const base = withAcpNotes(withExternalSummaryTools(applyAcpToolOverrides(toolsToAdd, toolPrompts), externalOn), mode, packOwnsDescription(toolPrompts)); + const ownedDescriptions = packOwnsDescription(toolPrompts); + let base = withAcpNotes(withExternalSummaryTools(applyAcpToolOverrides(toolsToAdd, toolPrompts), externalOn), mode, ownedDescriptions); + if (hasCodexExec(tools ?? [])) base = withCodexExecNotes(base, ownedDescriptions); return mergeOwnedTools<unknown>(tools, base, [], (t) => (t as { name?: string })?.name); } diff --git a/tests/lane-decisions-matrix.test.ts b/tests/lane-decisions-matrix.test.ts index 70559e0b40..d889ccf797 100644 --- a/tests/lane-decisions-matrix.test.ts +++ b/tests/lane-decisions-matrix.test.ts @@ -325,6 +325,11 @@ const ROWS: { name: string; lane: Lane; tools: string[]; injectTool?: boolean; t // wire carries the MCP-prefixed compress reads reachable, same as #3401's // bare-name observation on dsh. { name: "dsh mcp__bili__compress keeps the surface (#4344 dialect is lane-independent)", lane: "dsh", tools: ["mcp__bili__compress"], expect: () => ({ nudge: true, standingPrompt: true }) }, + // #5592: a Codex calling-convention hint does not prove an MCP binding exists. + ...["exec", "functions", "functions.exec"].map((name) => ({ + name: `dsh ${name} alone does not prove compression reachability (#5592)`, + lane: "dsh" as const, tools: [name], expect: () => ({ nudge: false, standingPrompt: false }), + })), ]; for (const wire of ["anthropic", "openai", "responses", "google"] as const) { diff --git a/tests/ptc-runcode-tools.test.ts b/tests/ptc-runcode-tools.test.ts index 8ea67e61cc..0e2f64cb24 100644 --- a/tests/ptc-runcode-tools.test.ts +++ b/tests/ptc-runcode-tools.test.ts @@ -32,7 +32,7 @@ const BASH = { name: "bash", description: "Shell out." }; function flat(tools: unknown[]): Map<string, string> { return new Map(tools.map((t) => { const o = t as { name?: unknown; description?: unknown }; - return [String(o.name), typeof o.description === "string" ? o.description : ""]; + return [String(o?.name), typeof o?.description === "string" ? o.description : ""]; })); } @@ -96,6 +96,65 @@ test("injectResponsesTool: run_code client → exempt note, flat shape", () => { assert.equal(out.get("run_code"), RUN_CODE.description, "client entry untouched"); }); +const CODEX_DIRECT_NOTE = "This proxy-provided tool has no functions.exec SDK binding"; +const CODEX_EXEC = { type: "custom", name: "exec", description: "Run JavaScript code to orchestrate tool calls." }; + +test("injectResponsesTool: qualified Codex exec teaches direct calls without changing host tools (#5592)", () => { + const shapes = [ + [{ ...CODEX_EXEC, name: "functions.exec" }], + [{ ...CODEX_EXEC, type: "function", name: "functions.exec" }], + [{ type: "namespace", name: "functions", tools: [CODEX_EXEC] }], + [{ type: "additional_tools", tools: [{ type: "namespace", name: "functions", tools: [CODEX_EXEC] }] }], + ]; + for (const tools of shapes) { + const before = JSON.stringify(tools); + const out = injectResponsesTool(tools); + const descriptions = flat(out); + assert.equal(out[0], tools[0], "host declarations retain object identity"); + assert.equal(JSON.stringify(tools), before, "host declarations remain byte-exact"); + for (const name of ["compress", "decompress", "search_context", "acp_status", "acp_cache"]) { + assert.ok(descriptions.get(name)?.includes(CODEX_DIRECT_NOTE), name); + assert.ok(!descriptions.get(name)?.includes(PTC_BINDING_NOTE), name); + } + assert.equal(JSON.stringify(out), JSON.stringify(injectResponsesTool(tools)), "stable prompt bytes"); + assert.deepEqual(injectResponsesTool(out), out, "repeated injection does not duplicate notes"); + } +}); + +test("injectResponsesTool: ordinary exec and unrelated namespaces do not acquire Codex hints (#5592)", () => { + const shapes: unknown[][] = [ + [CODEX_EXEC], + [{ type: "function", name: "exec", description: "Run a shell command." }], + [{ type: "namespace", name: "functions", tools: [{ type: "function", name: "shell" }] }], + [{ type: "namespace", name: "other", tools: [CODEX_EXEC] }], + [{ type: "namespace", name: "functions", tools: [{ type: "namespace", name: "exec", tools: [] }] }], + [{ type: "namespace", name: "functions", tools: null }], + [{ type: "namespace", name: "functions.exec", tools: [] }], + [{ type: "function", name: "functions", tools: [CODEX_EXEC] }], + [{ type: "namespace", name: "other", tools: [{ ...CODEX_EXEC, name: "functions.exec" }] }], + [null, undefined, "functions.exec"], + ]; + for (const tools of shapes) { + for (const description of flat(injectResponsesTool(tools)).values()) { + assert.ok(!description.includes(CODEX_DIRECT_NOTE)); + } + } +}); + +test("injectResponsesTool: Codex notes preserve native MCP tools and pack-owned descriptions (#5592)", () => { + const native = { type: "namespace", name: "mcp__bili", tools: [{ type: "function", name: "compress", description: "Native MCP binding" }] }; + const tools = [{ type: "namespace", name: "functions", tools: [CODEX_EXEC] }, native]; + const pack = { compress: { description: "Pack-owned compress wording." } }; + const out = injectResponsesTool(tools, undefined, pack); + assert.equal(out[1], native); + assert.equal(flat(out).get("compress"), pack.compress.description); + assert.ok(flat(out).get("acp_status")?.includes(CODEX_DIRECT_NOTE)); + for (const entries of Object.values(readManifestTools())) { + assert.ok(entries.get("compress")?.includes(PTC_BINDING_NOTE)); + assert.ok(!entries.get("compress")?.includes(CODEX_DIRECT_NOTE)); + } +}); + test("injectGoogleTool: run_code declaration → exempt note on appended declarations", () => { const out = injectGoogleTool([{ functionDeclarations: [{ name: "run_code", description: "prog" }] }] as never); const decls = new Map(out.flatMap((t) => (t.functionDeclarations ?? []).map((d) => [d.name, d.description] as const)) as [string, string][]); From 45fccba6ba0e22782b30d03da26074b56a16b00a Mon Sep 17 00:00:00 2001 From: ci-check <ci-check@local> Date: Fri, 13 Nov 2026 03:54:00 +0800 Subject: [PATCH 62/78] test: pin the full Codex exec note constant in the #5592 regression tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review fix (small, non-semantic): the new #5592 tests re-declared a substring of the note locally instead of importing the constant, unlike the file's existing PTC-note pattern. Export CODEX_EXEC_EXEMPT_NOTE and assert the full note — strictly stronger, no behavior change. (cherry picked from commit 06b51024f039f07fc33e45b7ebe26b35ebd2897a) --- src/server/inject.ts | 2 +- tests/ptc-runcode-tools.test.ts | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/server/inject.ts b/src/server/inject.ts index 218488ac97..b5ff8ae906 100644 --- a/src/server/inject.ts +++ b/src/server/inject.ts @@ -10,7 +10,7 @@ import { conversationPromptSections, externalSummaryEnabled, messageTimestampSec import { forceTextProtocol as knobForceTextProtocol, renderNone as knobRenderNone } from "../knobs.js"; import { GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; -const CODEX_EXEC_EXEMPT_NOTE = " This proxy-provided tool has no functions.exec SDK binding: call it DIRECTLY as a normal tool call using its declared name and namespace. Do not invoke it through tools.* inside functions.exec; the proxy executes the direct call server-side."; +export const CODEX_EXEC_EXEMPT_NOTE = " This proxy-provided tool has no functions.exec SDK binding: call it DIRECTLY as a normal tool call using its declared name and namespace. Do not invoke it through tools.* inside functions.exec; the proxy executes the direct call server-side."; // Hint detection must not widen the shared omnibus reachability gate (#5592). function hasCodexExec(tools: readonly unknown[], namespace = ""): boolean { diff --git a/tests/ptc-runcode-tools.test.ts b/tests/ptc-runcode-tools.test.ts index 0e2f64cb24..4203389d91 100644 --- a/tests/ptc-runcode-tools.test.ts +++ b/tests/ptc-runcode-tools.test.ts @@ -23,7 +23,7 @@ process.env.BILI_PERSIST = "0"; import { defaultConfig } from "acp-kernel"; import { handlePluginManifest } from "../src/plugin.ts"; import type { ProxyOptions } from "../src/config.ts"; -import { injectGoogleTool, injectOpenaiTool, injectResponsesTool, injectTool, injectSystem } from "../src/server/inject.ts"; +import { CODEX_EXEC_EXEMPT_NOTE, injectGoogleTool, injectOpenaiTool, injectResponsesTool, injectTool, injectSystem } from "../src/server/inject.ts"; import { PROJECT_URL_NOTE, PTC_BINDING_NOTE, PTC_EXEMPT_NOTE, PROJECT_ATTRIBUTION_LINE, withProjectAttribution } from "../src/compress-tool.ts"; const RUN_CODE = { name: "run_code", description: "Run a program in the sandbox." }; @@ -96,7 +96,6 @@ test("injectResponsesTool: run_code client → exempt note, flat shape", () => { assert.equal(out.get("run_code"), RUN_CODE.description, "client entry untouched"); }); -const CODEX_DIRECT_NOTE = "This proxy-provided tool has no functions.exec SDK binding"; const CODEX_EXEC = { type: "custom", name: "exec", description: "Run JavaScript code to orchestrate tool calls." }; test("injectResponsesTool: qualified Codex exec teaches direct calls without changing host tools (#5592)", () => { @@ -113,7 +112,7 @@ test("injectResponsesTool: qualified Codex exec teaches direct calls without cha assert.equal(out[0], tools[0], "host declarations retain object identity"); assert.equal(JSON.stringify(tools), before, "host declarations remain byte-exact"); for (const name of ["compress", "decompress", "search_context", "acp_status", "acp_cache"]) { - assert.ok(descriptions.get(name)?.includes(CODEX_DIRECT_NOTE), name); + assert.ok(descriptions.get(name)?.includes(CODEX_EXEC_EXEMPT_NOTE), name); assert.ok(!descriptions.get(name)?.includes(PTC_BINDING_NOTE), name); } assert.equal(JSON.stringify(out), JSON.stringify(injectResponsesTool(tools)), "stable prompt bytes"); @@ -136,7 +135,7 @@ test("injectResponsesTool: ordinary exec and unrelated namespaces do not acquire ]; for (const tools of shapes) { for (const description of flat(injectResponsesTool(tools)).values()) { - assert.ok(!description.includes(CODEX_DIRECT_NOTE)); + assert.ok(!description.includes(CODEX_EXEC_EXEMPT_NOTE)); } } }); @@ -148,10 +147,10 @@ test("injectResponsesTool: Codex notes preserve native MCP tools and pack-owned const out = injectResponsesTool(tools, undefined, pack); assert.equal(out[1], native); assert.equal(flat(out).get("compress"), pack.compress.description); - assert.ok(flat(out).get("acp_status")?.includes(CODEX_DIRECT_NOTE)); + assert.ok(flat(out).get("acp_status")?.includes(CODEX_EXEC_EXEMPT_NOTE)); for (const entries of Object.values(readManifestTools())) { assert.ok(entries.get("compress")?.includes(PTC_BINDING_NOTE)); - assert.ok(!entries.get("compress")?.includes(CODEX_DIRECT_NOTE)); + assert.ok(!entries.get("compress")?.includes(CODEX_EXEC_EXEMPT_NOTE)); } }); From bdf1ac0d57a2747bc91ddce76f372238f56311cf Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 10:46:45 +0800 Subject: [PATCH 63/78] fix(handoff): complete #4753 release-wiring dropped by a marker slip The 4756 pick (6c0079b5f) was committed with a leftover conflict marker at the runPreparedPipeline definition and the releaseRequestInput block around the main forward() call was lost. This restores both: the closure is back to `let ... | undefined` and the input-only bindings (bodyBuffer, parsed, affinityMessagesCache, runPreparedPipeline) are released at the upstream response boundary (try/finally + relay callback), per the original PR. --- src/server/handle.ts | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/server/handle.ts b/src/server/handle.ts index d37102c4be..a883122be8 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -2929,18 +2929,14 @@ async function handleRequest( // #5683: this round's merged three-level compress settings, captured // so the post-prepare gate stamp below can read the policy knobs. let preparedCs: CompressSettings | undefined; - let runPreparedPipeline = async ( - respondFailFast: boolean, - overflowWindow?: number, - ): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => { - const runPrepareInner = async (): Promise<Prepared> => { -======= + // #4753: `let` + `| undefined` so releaseRequestInput below can drop + // the closure (and with it its captured input-only state) once the + // upstream response boundary is reached. let runPreparedPipeline: (( respondFailFast: boolean, overflowWindow?: number, ) => Promise<{ body: string | Buffer; prepared: Prepared | null } | null>) | undefined = async (respondFailFast, overflowWindow) => { - const runPrepare = async (): Promise<Prepared> => { ->>>>>>> 9c8c72c78 (fix: release input-only request data before streaming (#4753)) + const runPrepareInner = async (): Promise<Prepared> => { // #1820: this prepare consumes one unit of post-rebuild anchor // validity (the last one deletes it — see session.ts). tickPostRebuildAnchor(session); @@ -3391,9 +3387,26 @@ async function handleRequest( return next; } : undefined; - await forward(req, res, opts, pendingForward.body, pendingForward.prepared, core, reqConfig, log, route, instanceId, affinity, overflowRefold, v1Meta, throttleModel, (outbound) => { - logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0, outbound); - }); // Remember for ALL modes (not just plugin): wire clients (dsh, + const releaseRequestInput = () => { + // #4753: refolding is finished before streaming starts. Drop + // the input-only bindings even if forward's wrapper retains + // the callback; accepted outbound bodies still support + // retries. + bodyBuffer = Buffer.alloc(0); + parsed = null; + affinityMessagesCache = null; + runPreparedPipeline = undefined; + }; + try { + await forward(req, res, opts, pendingForward.body, pendingForward.prepared, core, reqConfig, log, route, instanceId, affinity, overflowRefold, v1Meta, throttleModel, (outbound) => { + logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0, outbound); + }, releaseRequestInput); + } finally { + // Transport failures and locally forged replies can return + // before the upstream response boundary. + releaseRequestInput(); + } + // Remember for ALL modes (not just plugin): wire clients (dsh, // hermes, unplug'd pi) read the same panel via /__bili/plugin/status // and need the nudge/breakdown sections too; locked so a racing // plugin tool call sees a consistent window. From 6c818b3c6460e19286e270ae5eb7e04ec75020a0 Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 10:47:19 +0800 Subject: [PATCH 64/78] fix(test): repair comment/test join dropped by the #5074 union in the lane-invariant pins The 5074 pick concatenated the pick-side comment lines onto the master-side comment of the (G) pin without newlines, commenting out the test( line and breaking the file. --- tests/side-lane-invariants.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index cf46234777..2f9501f7c2 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -349,11 +349,13 @@ test("(F) extracted protocol modules keep the side-lane boundary (#1440 P2 cut 2 // isDshSingleShotSidecar — same category once more: the generic single-shot // dsh sidecar shape predicate (no tools + one-shot body, any system text) // consulted to set sideIntent and to keep unknown sidecars off the raw-key -// anchor before the first main turn can claim it.// title call out of the #4774 raw-key anchor race. #5073 adds +// anchor before the first main turn can claim it. +// #4774 raw-key anchor race. #5073 adds // isStructuredOutputCall — same category once more: a pure toolless // structured-output shape predicate (no threshold, no session state) consulted // to set sideIntent and to keep the call out of the restoreOutputBudget -// high-water path, exactly like the #2500 server-tool predicate.test("(G) extracted pipeline module keeps the side-lane boundary (#1440 P2 cut 3)", () => { +// high-water path, exactly like the #2500 server-tool predicate. +test("(G) extracted pipeline module keeps the side-lane boundary (#1440 P2 cut 3)", () => { const text = readFileSync(fileURLToPath(new URL("../src/server/handle.ts", import.meta.url)), "utf8"); const laneInternals = /\bSIDE_REQUEST_MAX_TOKENS\b|\bSIDE_REQUEST_AGENTS\b|\bDemoteGateSignals\b|\bSideLaneSignals\b|\bSideLaneDecision\b|\bBILI_TOOL_NAMES\b|\bOutputBudgetField\b|\boutputBudgetField\b|\breadOutputBudget\b|\bwriteOutputBudget\b|\b_resetNoOutputCeilingWarningsForTest\b/; assert.doesNotMatch(text, laneInternals, "handle.ts must stay decoupled from the side-lane engine internals"); From 65aede1a71046670af8c6e921ffe599018deac41 Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 10:47:54 +0800 Subject: [PATCH 65/78] fix(test): match the #4756 release hook to forward()'s master signature Master inserted throttleModelHint + onWireBody before the releaseRequestInput tail parameter; pass the extra undefined. --- tests/request-input-lifetime.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/request-input-lifetime.test.ts b/tests/request-input-lifetime.test.ts index bdc8f8711c..0ce037458b 100644 --- a/tests/request-input-lifetime.test.ts +++ b/tests/request-input-lifetime.test.ts @@ -82,6 +82,7 @@ for (const scenario of ["success", "refold", "declined", "throwing", "rejected"] return { body: `${body} `, prepared: { ...prepared, body: `${body} ` } }; }, undefined, + undefined, () => { releaseCount++; trace.push("release"); released.resolve(); }, ).then(() => { forwardCompleted = true; completed.resolve(); }, error => { res.destroy(); completed.reject(error); }); }); From 3f099ca5a939e511035882d65df6bc80b880d818 Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 10:55:38 +0800 Subject: [PATCH 66/78] fix(loop): restore the streamedMarkers push the #3906 strip mangled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The streamMarkers-gate removal accidentally fused the seenMarkers else-close onto the emitMarker line and deleted streamedMarkers.push(buildVisibilityMarker) entirely — the retry bodies lost their in-band visibility markers (broke the #5220 recovery-receipt and #3905 announcement pins). Restored. --- src/loop/core.ts | 4 +++- tests/plugin-wire-metadata.test.ts | 5 ++++- tests/request-input-lifetime.test.ts | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/loop/core.ts b/src/loop/core.ts index b93ae72f17..5b4cba8c65 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -1448,7 +1448,9 @@ export async function* runCompressLoop( ctx.log(`[acp-loop] suppressed duplicate ${call.name} status marker (identical failure repeated this request)`); } else { seenMarkers.add(markerKey); - yield adapter.emitMarker(call.name, result.text); } + yield adapter.emitMarker(call.name, result.text); + streamedMarkers.push(buildVisibilityMarker(call.name, result.text)); + } } } else { realToolCalls.push(call); diff --git a/tests/plugin-wire-metadata.test.ts b/tests/plugin-wire-metadata.test.ts index f1f3906cfe..dc519db53a 100644 --- a/tests/plugin-wire-metadata.test.ts +++ b/tests/plugin-wire-metadata.test.ts @@ -34,7 +34,10 @@ test("precomputed wire metadata matches raw-buffer snapshot evidence across wire { wire: { messages: null, input: "fallback" }, comparable: true }, { wire: { messages: [{ content: [{ type: "image", source: {} }] }], input: "ignored" }, comparable: false }, { wire: { messages: [{ content: [{ type: "tool_use", input: { type: "image" } }] }] }, comparable: true }, - { wire: { messages: [{ content: [{ type: "thinking", thinking: "signed" }] }] }, comparable: false }, + // #4266 (landed after this PR's base): a payload-bearing thinking block projects to a + // named reasoning message whose text carries the wire bytes — comparable by view. + // Only payload-less thinking stays non-comparable (fail-closed). + { wire: { messages: [{ content: [{ type: "thinking", thinking: "signed" }] }] }, comparable: true }, { wire: { messages: [{ content: "text", audio: {} }] }, comparable: false }, { wire: null, comparable: false }, { wire: 42, comparable: false }, diff --git a/tests/request-input-lifetime.test.ts b/tests/request-input-lifetime.test.ts index 0ce037458b..8e313a5ba8 100644 --- a/tests/request-input-lifetime.test.ts +++ b/tests/request-input-lifetime.test.ts @@ -83,6 +83,7 @@ for (const scenario of ["success", "refold", "declined", "throwing", "rejected"] }, undefined, undefined, + undefined, () => { releaseCount++; trace.push("release"); released.resolve(); }, ).then(() => { forwardCompleted = true; completed.resolve(); }, error => { res.destroy(); completed.reject(error); }); }); From 90ba031cdeaf0f16ba027240dfc19aa9f3d88b2d Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 10:59:10 +0800 Subject: [PATCH 67/78] fix(handoff): stop coupling the handoff canSave to res.destroyed (#4861) The client-abort save paths land, but the relay still passed canSave=() => !res.destroyed, so a finished reply was dropped the instant pi hung up on the terminal frame. Persistence now keys on the upstream turn finishing; partial turns stay uncaptured. --- src/server/relay.ts | 7 ++++++- tests/issue3905-announcement-tail.test.ts | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/server/relay.ts b/src/server/relay.ts index 48e2a0edf7..a9123e7253 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -351,7 +351,12 @@ async function forwardCaptured( ): Promise<void> { const forwardStartedAt = Date.now(); const hostOwnsTruncationRecovery = req.headers[BILI_HOST_RECOVERY_OWNER_HEADER] === BILI_HOST_RECOVERY_OWNER_OMP; - const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session, () => !res.destroyed) : undefined; // E2: a codex native-compaction request intercepted in prepare() carries a + // #4861: persistence answers "did the upstream turn finish?", not "is the +// client socket still open?" — a client that hangs up on the terminal frame +// (pi's shape) must not lose the finished reply. Partial turns (no terminal) +// are still never captured. +const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session) : undefined; + // E2: a codex native-compaction request intercepted in prepare() carries a // forged success response — serve it without contacting upstream. if (prepared?.codexForge) { onWireBody?.(); releaseRequestInput?.(); log("info", `[${prepared.session.id}] codex compact served locally (${prepared.codexForge.kind}); upstream not contacted`); diff --git a/tests/issue3905-announcement-tail.test.ts b/tests/issue3905-announcement-tail.test.ts index 75c4f1635b..be0bf43b4c 100644 --- a/tests/issue3905-announcement-tail.test.ts +++ b/tests/issue3905-announcement-tail.test.ts @@ -200,8 +200,12 @@ test("#3905 chat: an announcement RETRY degenerates into the in-band error (#870 const refetch = () => Promise.resolve(streamOf(announceTurn())); await pipePluginChatWithStrip(streamOf(announceTurn()), makeRes(out), "openai", makeSession(), undefined, refetch); const text = out.join(""); + // #5958 (771d5d908, landed after this PR's base): the double-degenerate + // terminal is shaped by compat.streamErrorShape, default "protocol" → a + // native error frame ("stream_error") instead of the legacy in-band + // "[ACP] stream error" completion shape. The client is told either way. assert.ok( - text.includes("[ACP] stream error"), + text.includes("stream_error") && text.includes("degenerated again"), "the client is told, rather than left with a second silent announcement", ); }); From 8edc22ab6ae0c0620d318bcc4e8cba731906ea4f Mon Sep 17 00:00:00 2001 From: ci-check <ci-check@local> Date: Fri, 13 Nov 2026 13:10:31 +0800 Subject: [PATCH 68/78] docs(architecture): add src/server/rejection-diagnostics.ts to the module map The #4452 batch added a new src/server/ module but the file-by-file map in reference/architecture.md was not regenerated, so the architecture-docs CI guard failed with 'missing: src/server/rejection-diagnostics.ts'. Add the row (alphabetically between relay.ts and request-cost.ts) so the map covers all src files again. --- reference/architecture.md | 1 + 1 file changed, 1 insertion(+) diff --git a/reference/architecture.md b/reference/architecture.md index 41bc0ffb6a..484cd70bf1 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -28,6 +28,7 @@ billion-context/ │ │ ├── prepare-openai.ts # OpenAI chat request preparation (#1440) │ │ ├── prepare-responses.ts # Responses request preparation + codex compact/prompt-cache helpers (#1440) │ │ ├── relay.ts # Upstream relay zone: forward() + fake-completion recovery (#1440 P2 cut 4) +│ │ ├── rejection-diagnostics.ts # Bounded terminal-cause diagnostic for a relayed upstream rejection: one nested attempts[].details.error.message, scrubbed + bounded, appended to the relay log line (#4452) │ │ ├── request-cost.ts # Per-request local-cost line (outbound measured at the real wire body) + #5408 heartbeat + wall-clock slow-session advisory, facts only (#903/#4528/#5744) │ │ ├── responses-ordering.ts # #564 Responses assistant-run ordering repair + producer-owned turn-separator provenance (#2627) │ │ ├── responses-separator.ts # Canonical Responses turn-separator literal and carrier predicates (#2627) From 0271bb4f107f104291eb3f1da0bf0fd51fe5a389 Mon Sep 17 00:00:00 2001 From: ci-check <ci-check@local> Date: Fri, 13 Nov 2026 13:10:42 +0800 Subject: [PATCH 69/78] chore(pi-subagents): bump version 0.2.3 -> 0.2.4 for the fleet-inspector CRLF fix The #4386 batch changed pi-subagents/src/fleet-inspector.ts (CRLF tool-label normalization + background-bar painting) without bumping the component version, so the pi-subagents-guard CI job failed. Bump pi-subagents/package.json (and the matching package-lock.json file: entry) so the version discipline guard passes. --- package-lock.json | 2 +- pi-subagents/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index e2ff6fb483..374543cafc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9283,7 +9283,7 @@ }, "pi-subagents": { "name": "billion-context-pi-subagents", - "version": "0.2.3", + "version": "0.2.4", "dev": true, "license": "MIT", "devDependencies": { diff --git a/pi-subagents/package.json b/pi-subagents/package.json index 4a7a958a52..5e764e3c19 100644 --- a/pi-subagents/package.json +++ b/pi-subagents/package.json @@ -1,6 +1,6 @@ { "name": "billion-context-pi-subagents", - "version": "0.2.3", + "version": "0.2.4", "description": "acp_delegate sub-agent tools for the Pi coding agent — split out of billion-context-pi (#612); vendored in-repo (#2384).", "main": "dist/index.js", "types": "dist/index.d.ts", From 91db32defe2bba163f39bfef9a7a2e96328df1cc Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 17:49:08 +0800 Subject: [PATCH 70/78] chore: re-run kernel guard (kernel-change declaration added to PR body) From 1b15692af419fa48f95b36eeb74b20517ba72443 Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 17:57:37 +0800 Subject: [PATCH 71/78] kernel: version 0.0.197 (batch4 kernel fixes: #6428 server-tool pair heal, #6345 span-safe gap bridging, #5166 liveness-by-coverage) --- kernel/package-lock.json | 4 ++-- kernel/package.json | 2 +- package-lock.json | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 42edfa9cb7..1173e00f65 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.196", + "version": "0.0.197", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.196", + "version": "0.0.197", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index f03fc7adee..955a551a83 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.196", + "version": "0.0.197", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/package-lock.json b/package-lock.json index 374543cafc..8b3f75a46a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "billion-context", - "version": "0.1.213", + "version": "0.0.197", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "billion-context", - "version": "0.1.213", + "version": "0.0.197", "license": "MIT", "dependencies": { "mysql2": "^3.15.3", @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.196", + "version": "0.0.197", "dev": true, "license": "MIT", "devDependencies": { From 60c66ad3d4dd36d002bd3a75570f77fab86ddd4b Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 18:04:09 +0800 Subject: [PATCH 72/78] fix(lock): restore root package version 0.1.213 (sed hit the wrong 0.0.196 line during the kernel bump) --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8b3f75a46a..85f7ba59df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "billion-context", - "version": "0.0.197", + "version": "0.1.213", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "billion-context", - "version": "0.0.197", + "version": "0.1.213", "license": "MIT", "dependencies": { "mysql2": "^3.15.3", From d1e15b073c7338636fa1d6e7a25e64ef286de2c2 Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 19:58:29 +0800 Subject: [PATCH 73/78] test(#2817): turn 1 carries the pinned one-shot budget for the narrowed residual (#5687 follow-up) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The batch's #5709 (PR by Louie0806) narrowed the accepted residual ride of a tool-less turn 1 onto the verbatim side lane: it now ALSO requires the small pinned one-shot output budget (utility sidecars pin 4096; agent main turns omit the field). The fixture's turn 1 omitted max_tokens, so it now correctly rides the MAIN lane and stats.requests counts from turn 1 — 9 instead of 8. Turn 1 now carries max_tokens: 4096 so the fixture keeps exercising the residual path the count pins; the cross-wire persona/fold assertions are untouched. CI: issue2817-crosswire-persona-fold '9 !== 8' on batch4. --- tests/issue2817-crosswire-persona-fold.test.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/issue2817-crosswire-persona-fold.test.ts b/tests/issue2817-crosswire-persona-fold.test.ts index 06b756713e..13cf09615b 100644 --- a/tests/issue2817-crosswire-persona-fold.test.ts +++ b/tests/issue2817-crosswire-persona-fold.test.ts @@ -154,7 +154,13 @@ test("issue #2817 regression: identified persona, PLUGIN-MODE fold on openai lan // Phase A — openai lane (commandcode-style). 8 turns ≈ 50KB of history. for (let t = 1; t <= 8; t++) { hist.push({ role: "user", text: `Turn ${t}: audit chapter ${t}. ` + FILLER(t, 6) }); - const resp = await fetch(openaiBase, { method: "POST", headers: dshHeaders, body: JSON.stringify({ model: "probe-model", stream: true, messages: renderOpenai() }) }); + // #5687 follow-up (#5709): the accepted residual ride of a tool-less + // turn 1 now ALSO requires the small pinned one-shot output budget + // (utility sidecars pin 4096; agent main turns omit the field). Turn + // 1 carries it so this fixture keeps exercising the residual path the + // count pins below. + const turn1Body = { model: "probe-model", stream: true, ...(t === 1 ? { max_tokens: 4096 } : {}), messages: renderOpenai() }; + const resp = await fetch(openaiBase, { method: "POST", headers: dshHeaders, body: JSON.stringify(turn1Body) }); assert.equal(resp.status, 200, `openai turn ${t}`); await resp.text(); hist.push({ role: "assistant", text: `Reply about chapter ${t}: acknowledged. ` + FILLER(t, 0.2) }); @@ -193,10 +199,12 @@ test("issue #2817 regression: identified persona, PLUGIN-MODE fold on openai lan assert.ok(blocksAfterFold.length >= 1, "plugin-mode fold landed on the openai lane"); const foldedBody = bodies.openai[bodies.openai.length - 1]!; assert.ok(!foldedBody.includes(EARLY_FILLER_TAG), "covered head content folded away on the openai lane"); - // #5656 residual: turn 1 is a tool-less single-shot shape and rides the - // verbatim side lane (kernel no-touch), so stats.requests counts from - // turn 2 — eight counted turns after nine wire turns. The fork teeth - // below are unchanged: a fork would still show as a missing increment. + // #5656 residual (narrowed by #5687 follow-up / #5709): turn 1 is a + // tool-less single-shot shape WITH the small pinned one-shot budget, so + // it rides the verbatim side lane (kernel no-touch) and stats.requests + // counts from turn 2 — eight counted turns after nine wire turns. The + // fork teeth below are unchanged: a fork would still show as a missing + // increment. assert.equal(main.stats.requests, 8); // Turns 10-12: openai lane stays folded. From 7d7631f194254c2b54a2b360cf5d924408dead01 Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 21:41:44 +0800 Subject: [PATCH 74/78] Revert "kernel: bridge span-safe gaps at the merge gate so mega-turn islands survive minCompressRange (production 2026-11-09 no-ranges incident)" This reverts commit dcf4ff2004053f5a42d7adea6c763f6095901eee. --- kernel/src/compress.ts | 18 -- kernel/src/recommend.ts | 149 ++--------- kernel/src/types.ts | 16 -- kernel/src/unit-projection.ts | 94 +------ ...sue5531-stranded-reasoning-residue.test.ts | 12 +- .../megaturn-protected-gap-bridging.test.ts | 246 ------------------ 6 files changed, 21 insertions(+), 514 deletions(-) delete mode 100644 kernel/tests/megaturn-protected-gap-bridging.test.ts diff --git a/kernel/src/compress.ts b/kernel/src/compress.ts index 59c3c77771..d180a05b24 100644 --- a/kernel/src/compress.ts +++ b/kernel/src/compress.ts @@ -49,7 +49,6 @@ import { buildCompressibleRanges, mergeRangesToThreshold, screenRecommendedUnits, - warnScreenDrop, } from "./recommend.js"; import { runPipeline, @@ -1502,23 +1501,6 @@ const recommendNode: PipelineNode = { ctx.config, computeProtectedRefs(screenArray, io.state, ctx.config, ctx.countTokens), ); - // Production 2026-11-09 mega-turn incident: the funnel can zero out BELOW - // the "nothing compressible" level — segments exist and hold messages, but - // every island dies at the merge gate or the screen, and the host only - // sees recommendedRanges=[] while its own message-level diagnostic still - // counts collectible material ("collectible>0 yet ranges=0" was - // undiagnosable from the host side for a full night). Warn (budgeted) - // with the stage-by-stage numbers so the killing stage is named directly. - if (contextRanges.compressible.length > 0 && recommendedRanges.length === 0) { - const segMsgs = contextRanges.compressible.reduce((s, r) => s + r.count, 0); - const minChars = ctx.config.compress.minCompressRange; - const subGate = contextRanges.compressible.filter( - (r) => (r.chars ?? r.tokens * 4) < minChars, - ).length; - warnScreenDrop( - `funnel zeroed with material present: segments=${contextRanges.compressible.length} (${segMsgs} msgs, ${subGate} below ${minChars}-char merge gate) → all died at merge/screen; host sees no-ranges`, - ); - } const recommendation: Recommendation = { contextRanges, recommendedRanges, diff --git a/kernel/src/recommend.ts b/kernel/src/recommend.ts index 1d413f66cd..23a2ca2631 100644 --- a/kernel/src/recommend.ts +++ b/kernel/src/recommend.ts @@ -278,11 +278,6 @@ export function buildCompressibleRanges( id: string; ref: string; gapBefore: boolean; - /** True when this message's preceding gap is HARD (contains at least one - * skip the apply side cannot tolerate inside a spanning fold — see - * hardGapSinceCompressible). False = the gap is span-safe (protected-tool - * exchanges and/or integrity-withdrawn members only) or absent. */ - gapHard: boolean; tokens: number; chars: number; isTool: boolean; @@ -321,20 +316,6 @@ export function buildCompressibleRanges( // two rules coincide, so ranges are byte-identical to the old behavior. let skipSinceCompressible = false; let skipSinceProtected = false; - // Span-safety classification (production 2026-11-09 mega-turn incident): a - // gap the apply side tolerates INSIDE a spanning fold — protected-tool - // exchanges (filterProtectedToolMessages carves them, "Bug 39") and - // integrity-withdrawn members (the fold gate withdraws them again at apply, - // they stay visible; #564 reverse-split semantics) — is SPAN-SAFE, and - // mergeRangesToThreshold may bridge across it so sub-gate islands can - // accumulate to minCompressRange. Every OTHER skip (block coverage, live - // checkpoint carrier, unrecoverable media, soft zone — whose interior hits - // TRUNCATE the fold at the first protected message (#3953) — dead-ref - // holes, BLOCKED/unrefed) is hard: no bridge, ever. Set on any skip other - // than the protected-exchange branch; cleared when a compressible message - // is pushed. The protected branch leaves it untouched, so a gap that mixes - // protected + hard skips stays hard in either order. - let hardGapSinceCompressible = false; let msgIndex = -1; // #3192/#3205: numeric set of recorded-dead refs. A dropped message leaves a // hole in the ref map that array-adjacency segmentation bridges, so we break @@ -349,17 +330,10 @@ export function buildCompressibleRanges( for (const msg of messages) { msgIndex++; const ref = state.messageRefs.byRaw[msg.id]; - if (!ref || ref === "BLOCKED") { - // No ref = no slot in the ref map: mergeRangesToThreshold sees an index - // hole here even though no skip flag fires. Record it as a hard gap so - // bridging never spans an unrefed message (its foldability is untested). - hardGapSinceCompressible = true; - continue; - } + if (!ref || ref === "BLOCKED") continue; if (isSyntheticOrPruned(msg, covered)) { skipSinceCompressible = true; skipSinceProtected = true; - hardGapSinceCompressible = true; continue; } @@ -375,7 +349,6 @@ export function buildCompressibleRanges( if (isLiveCheckpointCarrier(msg, state)) { skipSinceCompressible = true; skipSinceProtected = true; - hardGapSinceCompressible = true; continue; } @@ -388,7 +361,6 @@ export function buildCompressibleRanges( if (hasUnrecoverableMediaPayload(msg)) { skipSinceCompressible = true; skipSinceProtected = true; - hardGapSinceCompressible = true; continue; } @@ -410,16 +382,12 @@ export function buildCompressibleRanges( }); skipSinceProtected = false; skipSinceCompressible = true; - // Protected-exchange skip: the ONLY bridge-able skip class (see the - // hardGapSinceCompressible doc). Leave hardGapSinceCompressible - // untouched — a protected gap following a hard gap stays hard. continue; } if (protectedZoneRefs?.has(ref)) { skipSinceCompressible = true; skipSinceProtected = true; - hardGapSinceCompressible = true; continue; } @@ -436,14 +404,12 @@ export function buildCompressibleRanges( const hi = Math.max(curRefNum, lastCompressibleRefNum); if (hi - lo > 1 && hasDeadRefBetween(deadRefNums, lo, hi)) { skipSinceCompressible = true; - hardGapSinceCompressible = true; } } compressibleMsgs.push({ id: msg.id, ref, gapBefore: skipSinceCompressible, - gapHard: hardGapSinceCompressible, tokens: countMessageTokens(msg, countTokens), chars: (msg.text ?? "").length, isTool: isToolMessage(msg), @@ -453,7 +419,6 @@ export function buildCompressibleRanges( if (curRefNum !== null) lastCompressibleRefNum = curRefNum; skipSinceCompressible = false; skipSinceProtected = true; - hardGapSinceCompressible = false; } // Foldability: the fold gate (src/compress.ts) withdraws every message whose @@ -479,31 +444,15 @@ export function buildCompressibleRanges( covered, ).withdrawn; if (unfoldedIds.size > 0) { - // Withdrawn members are SPAN-SAFE (apply withdraws them again, they stay - // visible — see the hardGapSinceCompressible doc): removing one merges the - // gap before it with the gap after it, and the merged gap stays span-safe - // iff both were. Chain the hardness across consecutive withdrawals; the - // surviving record inherits the combined classification. let gapPending = false; - let pendingHard = false; const kept: typeof compressibleMsgs = []; for (const info of compressibleMsgs) { if (unfoldedIds.has(info.id)) { gapPending = true; - pendingHard = pendingHard || info.gapHard; continue; } - kept.push( - gapPending - ? { - ...info, - gapBefore: true, - gapHard: pendingHard || info.gapHard, - } - : info, - ); + kept.push(gapPending ? { ...info, gapBefore: true } : info); gapPending = false; - pendingHard = false; } compressibleMsgs = kept; } @@ -525,9 +474,6 @@ export function buildCompressibleRanges( toolPct: first.isTool ? 100 : 0, textPct: first.isTool ? 0 : 100, userMsgs: first.isUser ? 1 : 0, - ...(first.gapBefore && !first.gapHard - ? { bridgeableGapBefore: true } - : {}), }; for (let i = 1; i < group.length; i++) { const info = group[i]!; @@ -620,10 +566,6 @@ function mergeBatch(batch: CompressibleRange[]): CompressibleRange { // position-based merging (formatRanges) still sees it after this batch is // re-emitted as a single range. if (first.noMergeBefore === true) merged.noMergeBefore = true; - // Bridge flag propagates the same way: a batch that BRIDGED into this - // range keeps the marker so any later re-merge sees the span crosses - // span-safe gaps (and only those — hard gaps never set it). - if (first.bridgeableGapBefore === true) merged.bridgeableGapBefore = true; return merged; } @@ -644,22 +586,7 @@ function rangeChars(r: CompressibleRange): number { * `minChars`. A sub-threshold tail is folded into the preceding batch * (overshoot allowed); if no batch precedes it, nothing is emitted — the * whole remainder is below the gate, so no selection of it can pass and - * offering it only yields guaranteed-rejected calls (billion-context #847). - * - * Exception — span-safe gap bridging (production 2026-11-09 mega-turn): - * batches accumulate ACROSS a gap the apply side tolerates inside a spanning - * fold (`bridgeableGapBefore`: gaps made exclusively of protected-tool - * exchanges — carved at apply, filterProtectedToolMessages "Bug 39" — and/or - * integrity-withdrawn members — withdrawn again at apply, they stay visible) - * instead of closing at it. The per-CALL #847 gate sums chars over every - * range in the request, so a bridged span clears the gate that each island - * individually missed. Hard gaps (blocks, carriers, media, dead-ref holes, - * unrefed messages, soft zone — whose interior hits truncate the fold at the - * first protected message, #3953) still close the batch — their spans are - * doomed or carve-to-nothing (#498 / #2663). Sub-threshold remnants STILL - * emit nothing: the apply gate is per-call, but a lone island below it has - * no legal partner to ride with, and advertising it only yields the - * guaranteed-rejected call #847 exists to prevent. */ + * offering it only yields guaranteed-rejected calls (billion-context #847). */ export function mergeRangesToThreshold( ranges: CompressibleRange[], minChars: number, @@ -669,11 +596,10 @@ export function mergeRangesToThreshold( let batch: CompressibleRange[] = []; let batchChars = 0; // Close the running batch at a boundary: emit it only when it alone clears - // the gate. A sub-threshold segment bounded by a hard gap cannot be offered - // (it fails the apply-side #847 gate) and must not be stretched across the - // gap to reach the threshold, or the emitted span covers non-compressible - // content (#498). A span-safe gap does NOT close the batch — the span - // bridges it and apply carves/withdraws the interior (see the header doc). + // the gate. A sub-threshold segment bounded by a gap cannot be offered (it + // fails the apply-side #847 gate) and must not be stretched across the gap + // to reach the threshold, or the emitted span covers non-compressible + // content (#498). const closeBatch = () => { if (batch.length > 0) { if (batchChars >= minChars) result.push(mergeBatch(batch)); @@ -684,22 +610,17 @@ export function mergeRangesToThreshold( for (const r of ranges) { // #3192/#3205: hard boundary at a dead-ref hole — never bridge across it, // even though the hole carries no array-index gap (see noMergeBefore). - // (A dead-ref gap is a hard gap, so bridgeableGapBefore is unset on - // ranges following one; the explicit check keeps that invariant - // authoritative even on hand-built ranges that set both.) if (r.noMergeBefore) closeBatch(); const prev = batch[batch.length - 1]; - // A physical array-index gap between the previous range and this one - // marks a block / covered / carrier / pruned boundary (buildCompressibleRanges - // leaves a slot hole there; user-turn splits stay contiguous). Without - // index data we keep the legacy accumulate-across-everything behavior — - // EXCEPT that a span-safe gap does not close the batch (bridge). + // A physical array-index gap between the previous range and this one marks + // a block / protected / pruned boundary (buildCompressibleRanges leaves a + // slot hole there; user-turn splits stay contiguous). Without index data we + // keep the legacy accumulate-across-everything behavior. if ( prev && prev.endIndex !== undefined && r.startIndex !== undefined && - r.startIndex > prev.endIndex + 1 && - !r.bridgeableGapBefore + r.startIndex > prev.endIndex + 1 ) { closeBatch(); } @@ -713,8 +634,7 @@ export function mergeRangesToThreshold( !batch[0]!.noMergeBefore && (prev.endIndex === undefined || batch[0]!.startIndex === undefined || - batch[0]!.startIndex <= prev.endIndex + 1 || - batch[0]!.bridgeableGapBefore === true); + batch[0]!.startIndex <= prev.endIndex + 1); if (contiguous) { result[result.length - 1] = mergeBatch([prev, ...batch]); } else if (batchChars >= minChars) { @@ -774,34 +694,19 @@ export function screenRecommendedUnits( // config-validation warn in processTurn); behavior is unchanged — // still return false, the unit is never advertised. if (!(err instanceof BoundaryNotFoundError)) { - warnScreenDrop( - `dropped unit ${unit.startRef}..${unit.endRef} after projection error: ${ + console.warn( + `[acp-kernel] screenRecommendedUnits dropped unit ${unit.startRef}..${unit.endRef} after projection error: ${ err instanceof Error ? err.message : String(err) }`, ); } return false; } - if (!projection || projection.fatal !== null) { - // Production 2026-11-09 mega-turn incident: whole-funnel zeroing is - // undiagnosable when units vanish here with no trace — the host only - // sees "kernel ranges=0" while its own message-level diagnostic still - // counts collectible material. Warn (budgeted, same channel as the - // projection-error warn above) so a total screen wipe is attributable. - if (projection?.fatal) { - warnScreenDrop( - `dropped unit ${unit.startRef}..${unit.endRef}: ${projection.fatal.error.message}`, - ); - } - return false; - } + if (!projection || projection.fatal !== null) return false; if ( projection.filteredIds.length === 0 && projection.consumedBlockIds.length === 0 ) { - warnScreenDrop( - `dropped unit ${unit.startRef}..${unit.endRef}: nothing foldable in span (filteredIds=0, consumedBlocks=0)`, - ); return false; } // #3371: the apply lane's livelock guard (compress.ts, pi#199) throws on @@ -820,30 +725,8 @@ export function screenRecommendedUnits( !projection.isBlockBoundary && evaluateRefold(state, unit).kind !== "refold" ) { - warnScreenDrop( - `dropped unit ${unit.startRef}..${unit.endRef}: consumes only active block(s), not a refold`, - ); return false; } return true; }); } - -// Budgeted warn channel for screen-stage drops: the first SCREEN_DROP_WARN_BUDGET -// drops in the process are logged verbatim, afterwards only every Nth (a run of -// doomed units over a long session must stay observable without flooding the -// host log — the production incident produced 28 no-range events/day). Exported -// for the recommend pipeline node's funnel-zero warn (same channel, same budget). -const SCREEN_DROP_WARN_BUDGET = 40; -const SCREEN_DROP_WARN_STRIDE = 100; -let screenDropWarnCount = 0; -export function warnScreenDrop(detail: string): void { - screenDropWarnCount += 1; - if ( - screenDropWarnCount > SCREEN_DROP_WARN_BUDGET && - screenDropWarnCount % SCREEN_DROP_WARN_STRIDE !== 0 - ) { - return; - } - console.warn(`[acp-kernel] screenRecommendedUnits ${detail}`); -} diff --git a/kernel/src/types.ts b/kernel/src/types.ts index 6ba7d8de0b..7e7b4f4699 100644 --- a/kernel/src/types.ts +++ b/kernel/src/types.ts @@ -708,22 +708,6 @@ export interface CompressibleRange { * has no index gap and the index check alone would let mergeRangesToThreshold * bridge back into one doomed span bracketing the dead ref. */ noMergeBefore?: boolean; - /** buildCompressibleRanges sets this when the gap immediately before this - * range is SPAN-SAFE: composed exclusively of members the apply side - * tolerates INSIDE a spanning fold — protected-tool exchanges - * (config.protectedTools / always-protected list, carved by - * filterProtectedToolMessages, "Bug 39") and integrity-withdrawn members - * (the fold gate withdraws them again at apply; they stay visible, #564 - * reverse-split semantics). mergeRangesToThreshold bridges across such - * gaps so sub-gate islands can accumulate to minCompressRange instead of - * all dying below it (production 2026-11-09: a mega-turn with a protected - * exchange every ~4 rounds fragmented every island to 3-5K chars, - * mergeRangesToThreshold dropped them all → recommendedRanges=[] → the - * host's emergency lane fell back to irreversible physical pruning while - * the host's four-bucket diagnostic still showed 84 "collectible" - * messages). Hard gaps (blocks, carriers, media, dead refs, unrefed - * messages, soft zone) never set this — they keep splitting ranges. */ - bridgeableGapBefore?: boolean; } export interface ProtectedRange { diff --git a/kernel/src/unit-projection.ts b/kernel/src/unit-projection.ts index 690015731c..428d3eb672 100644 --- a/kernel/src/unit-projection.ts +++ b/kernel/src/unit-projection.ts @@ -165,17 +165,10 @@ export function projectUnit(input: UnitProjectionInput): UnitProjection { } } } - const adjustedIds = clampAdjustmentAtCoverage( - applyPairBoundaryAdjustments(resolved, input.messages, shared), + const adjustedIds = applyPairBoundaryAdjustments( resolved, input.messages, - // Fresh folds pass the active coverage; hand-built inputs without it - // (unit tests) keep the legacy unclamped adjustment behavior. - input.preExistingCoverage, - // #5337 discipline: reuse the shared index map — the clamp runs on every - // screened unit, and a per-call map rebuild would resurface the O(units×n) - // quadratic the shared context exists to prevent. - byId, + shared, ); const skippedCarrierIds = adjustedIds.filter((id) => liveCarrierIds.has(id)); if (skippedCarrierIds.length > 0) { @@ -923,89 +916,6 @@ function applyPairBoundaryAdjustments( return ids; } -/** - * Clamp the pair-boundary ADJUSTMENT's widened head/tail at active-coverage - * boundaries (production 2026-11-09 mega-turn incident). - * - * When a range ends (or starts) right where a block was cut mid-group — a - * spine-cut/fold boundary slicing between a round's reasoning and its - * tool-call — the pair/reasoning adjustment widens the range ACROSS the - * boundary to close the pair, pulling in already-covered members. The drag - * makes the block visible in range → nestedBlock/consumed machinery engages - * → the merge-integrity path resolves the unit to fold-nothing (fatal=null, - * filteredIds=0) or a split fatal: every range ending adjacent to a mid-group - * block cut dies at screening. This predates the merge-gate bridging fix — - * bridging only exposed it, by finally letting units reach the screen with - * that geometry. - * - * Clamping is safe precisely because the partner half is COVERED: #5531 makes - * covered halves invisible to the integrity judge (a covered call cannot - * strand its turn's reasoning; a covered result leaves no unanswered - * tool_call_id), so folding the visible part while the covered partner stays - * covered is the allowed reverse-split shape. The requested span itself is - * NEVER clamped — only the adjustment's widening — so hand-built ranges that - * deliberately target covered spans (refold candidates) keep resolving. - */ -function clampAdjustmentAtCoverage( - adjustedIds: string[], - resolved: { startIndex: number; endIndex: number; messageIds: string[] }, - messages: CoreMessage[], - coverage?: ReadonlySet<string>, - /** Shared id→index map (#5337): reuse the caller's O(n) derivation instead - * of rebuilding it per call on the screening hot path. */ - indexById?: Map<string, number>, -): string[] { - if (!coverage || coverage.size === 0 || adjustedIds.length === 0) { - return adjustedIds; - } - // Recover the adjusted span's index bounds (ids are contiguous by index — - // applyPairBoundaryAdjustments pushes by loop index). - let startIdx: number | undefined; - let endIdx: number | undefined; - if (indexById) { - startIdx = indexById.get(adjustedIds[0]!); - endIdx = indexById.get(adjustedIds[adjustedIds.length - 1]!); - } else { - const built = new Map<string, number>(); - for (let i = 0; i < messages.length; i++) { - const id = messages[i]?.id; - if (id !== undefined) built.set(id, i); - } - startIdx = built.get(adjustedIds[0]!); - endIdx = built.get(adjustedIds[adjustedIds.length - 1]!); - } - if (startIdx === undefined || endIdx === undefined) return adjustedIds; - const isCovered = (i: number): boolean => { - const id = messages[i]?.id; - return id !== undefined && coverage.has(id); - }; - // Pull the widened TAIL back to just before the first covered member, and - // the widened HEAD forward likewise. Only the WIDENED portion is clamped — - // covered ids inside the requested span stay (refold/consumed semantics). - let clamped = false; - for (let i = resolved.endIndex + 1; i <= endIdx; i++) { - if (isCovered(i)) { - endIdx = i - 1; - clamped = true; - break; - } - } - for (let i = resolved.startIndex - 1; i >= startIdx; i--) { - if (isCovered(i)) { - startIdx = i + 1; - clamped = true; - break; - } - } - if (!clamped) return adjustedIds; - const ids: string[] = []; - for (let i = startIdx; i <= endIdx; i++) { - const msg = messages[i]; - if (msg) ids.push(msg.id); - } - return ids; -} - function filterProtectedToolMessages( directMessageIds: string[], messages: CoreMessage[], diff --git a/kernel/tests/issue5531-stranded-reasoning-residue.test.ts b/kernel/tests/issue5531-stranded-reasoning-residue.test.ts index ffd7ca9c04..4a84ab26a9 100644 --- a/kernel/tests/issue5531-stranded-reasoning-residue.test.ts +++ b/kernel/tests/issue5531-stranded-reasoning-residue.test.ts @@ -195,15 +195,9 @@ describe("#5531 recommend: stranded reasoning+text are advertised again", () => }); assert.deepEqual(res.result.errors, [], JSON.stringify(res.result.errors)); assert.ok(res.result.blocksCreated >= 1, "a recovery block must be created"); - // The recovery block is the one covering the stranded reasoning. The seed - // block (call+result only) may stay active alongside it: ranges ENDING at - // a mid-group block cut are clamped at the coverage boundary instead of - // dragging the seed in as consumed (unit-projection clampAdjustmentAtCoverage), - // so "distinct from the seed" is no longer the reliable discriminator here. - const block = res.state.blocks.find( - (b) => b.active && b.effectiveMessageIds.includes("r"), - ); - assert.ok(block, "a recovery block covering the stranded reasoning must exist"); + const block = res.state.blocks.find((b) => b.active && b !== state.blocks[0]); + assert.ok(block, "new block distinct from the seed block"); + assert.ok(block.effectiveMessageIds.includes("r")); assert.ok(block.effectiveMessageIds.includes("t")); }); }); diff --git a/kernel/tests/megaturn-protected-gap-bridging.test.ts b/kernel/tests/megaturn-protected-gap-bridging.test.ts deleted file mode 100644 index 3604b7deff..0000000000 --- a/kernel/tests/megaturn-protected-gap-bridging.test.ts +++ /dev/null @@ -1,246 +0,0 @@ -// Regression pins for the production 2026-11-09 mega-turn no-ranges incident. -// -// Shape: a mega-turn whose rounds are interleaved with protected-tool -// exchanges (config.protectedTools — todo/tool_learn/…), with an active block -// cut mid-group through the middle. The protected exchanges + integrity -// withdrawals fragment the compressible surface into islands of ~3-5K chars — -// each individually BELOW the apply-side minCompressRange gate, and (because -// the apply gate sums chars per CALL, compress.ts #847) perfectly legal to -// fold together. Pre-fix, mergeRangesToThreshold dropped every sub-gate island -// → recommendedRanges=[] → the host's emergency lane fell back to irreversible -// physical pruning while its four-bucket diagnostic still counted 84 -// "collectible" messages. Three pins: -// 1. mergeRangesToThreshold BRIDGES across span-safe gaps so islands -// accumulate to the gate (and still never bridges a hard gap); -// 2. the bridged unit SURVIVES screening — projectUnit clamps the -// pair-boundary adjustment at the coverage boundary instead of dragging -// the adjacent block in as consumed (the second, independent killer); -// 3. the whole recommend chain advertises ≥1 range for the incident shape. - -import { describe, it } from "node:test"; -import assert from "node:assert/strict"; -import { - computeProtectedRefs, - buildCompressibleRanges, - mergeRangesToThreshold, - screenRecommendedUnits, -} from "../src/recommend.js"; -import { createInitialState } from "../src/state.js"; -import { assignRefs } from "../src/refs.js"; -import { defaultConfig } from "../src/config.js"; -import { defaultCountTokens } from "../src/tokenize.js"; -import type { - CompressibleRange, - CompressionState, - Config, - CoreMessage, -} from "../src/types.js"; - -const CJK = "上下文压缩水位判定折叠范围构建完整性撤回保护区覆盖"; -const cjk = (n: number): string => - Array.from({ length: n }, (_, i) => CJK[i % CJK.length]).join(""); - -const PROTECTED_TOOLS = [ - "tool_learn", - "skill_learn", - "todo", - "compress", - "acp_status", - "search_context", - "absorb", - "recall", -]; -const COMMON = ["read", "run_code", "glob", "grep", "browser_evaluate", "write"]; - -function config(): Config { - return defaultConfig(112000, { - protectedTools: [...PROTECTED_TOOLS], - preserveRecentMessages: 8, - preserveRecentTokens: 8000, - nudge: { ...defaultConfig(112000).nudge, maxContextLimitPct: 0.8 }, - }); -} - -/** 50 small rounds; every 4th round's exchange rides a protected tool; the - * newest 24 messages are unrefed (state persist lag, as in production). */ -function buildIncident(): { - messages: CoreMessage[]; - state: CompressionState; -} { - const messages: CoreMessage[] = []; - messages.push({ id: "m00001", role: "user", contentType: "text", text: cjk(600) }); - let n = 1; - const nid = () => `m${String(++n).padStart(5, "0")}`; - for (let r = 0; r < 50; r++) { - messages.push({ - id: nid(), - role: "assistant", - contentType: "reasoning", - text: cjk(250 + ((r * 137) % 450)), - }); - if (r % 2 === 0) { - messages.push({ id: nid(), role: "assistant", contentType: "text", text: cjk(60 + ((r * 61) % 150)) }); - } - const isProt = r > 2 && r % 4 === 0; - const tool = isProt - ? (["todo", "tool_learn", "search_context", "skill_learn"] as const)[r % 4]! - : COMMON[(r * 7) % COMMON.length]!; - const callId = `tc-${r}`; - messages.push({ - id: nid(), - role: "assistant", - contentType: "tool-call", - toolName: tool, - toolCallId: callId, - text: `{"r":${r}}`, - }); - messages.push({ - id: nid(), - role: "tool", - contentType: "tool-result", - toolName: tool, - toolCallId: callId, - text: cjk(200 + ((r * 331) % 1600)), - }); - } - const state = createInitialState(); - const noRefIds = new Set(messages.slice(-24).map((m) => m.id!)); - state.messageRefs = assignRefs(messages, { - existing: state.messageRefs, - nextIndex: 1, - shouldSkip: (m) => noRefIds.has(m.id!), - }).map; - // Active block cut mid-group through the middle (the earlier growth-lane - // fold): start on a compressible message so the round's call/result stay - // covered while the reasoning stays visible. - let startIdx = 30; - while ( - startIdx < messages.length && - messages[startIdx]!.contentType === "tool-result" - ) { - startIdx++; - } - const ids = messages.slice(startIdx, startIdx + 86).map((m) => m.id!); - state.blocks = [ - { - blockId: "b1", - runId: "r1", - tier: 1, - topic: "mid", - summary: cjk(400), - directMessageIds: ids, - effectiveMessageIds: ids, - directBlockIds: [], - compressedTokens: 40000, - createdAt: Date.now(), - survivedCount: 0, - generation: 0, - active: true, - }, - ]; - return { messages, state }; -} - -function minChars(cfg: Config): number { - return cfg.compress.minCompressRange; -} - -describe("mega-turn protected-gap bridging (2026-11-09 incident)", () => { - it("buildCompressibleRanges flags protected-only gaps as bridgeable", () => { - const { messages, state } = buildIncident(); - const cfg = config(); - const protectedRefs = computeProtectedRefs(messages, state, cfg, defaultCountTokens); - const ranges = buildCompressibleRanges(messages, state, cfg, protectedRefs, defaultCountTokens, messages); - assert.ok(ranges.compressible.length > 0, "islands must exist"); - const bridged = ranges.compressible.filter((r) => r.bridgeableGapBefore === true); - assert.ok(bridged.length > 0, "at least one island follows a span-safe gap"); - }); - - it("mergeRangesToThreshold bridges span-safe gaps so islands clear the gate", () => { - const { messages, state } = buildIncident(); - const cfg = config(); - const gate = minChars(cfg); - const protectedRefs = computeProtectedRefs(messages, state, cfg, defaultCountTokens); - const ranges = buildCompressibleRanges(messages, state, cfg, protectedRefs, defaultCountTokens, messages); - // Precondition of the incident: islands exist below the gate. - const subGate = ranges.compressible.filter((r) => (r.chars ?? 0) < gate); - assert.ok(subGate.length > 0, "fixture must produce sub-gate islands"); - const merged = mergeRangesToThreshold(ranges.compressible, gate); - assert.ok( - merged.length > 0, - "bridged islands must accumulate past the merge gate (pre-fix: all dropped)", - ); - for (const unit of merged) { - assert.ok( - (unit.chars ?? 0) >= gate, - `every emitted unit clears the gate, got ${unit.chars}`, - ); - } - }); - - it("mergeRangesToThreshold still refuses to bridge a hard gap", () => { - // Two islands separated by a block-coverage gap: no bridge flag on the - // second island → sub-gate islands on both sides stay unmerged. - const island = (startIdx: number): CompressibleRange => ({ - startRef: `m${String(startIdx).padStart(5, "0")}`, - endRef: `m${String(startIdx + 2).padStart(5, "0")}`, - startIndex: startIdx, - endIndex: startIdx + 2, - count: 3, - tokens: 900, - chars: 2000, - toolPct: 50, - textPct: 50, - userMsgs: 0, - }); - const merged = mergeRangesToThreshold([island(10), island(40)], 5000); - assert.deepEqual(merged, [], "hard-gapped sub-gate islands must not merge"); - }); - - it("the bridged unit survives screening next to a mid-group block cut", () => { - const { messages, state } = buildIncident(); - const cfg = config(); - const gate = minChars(cfg); - const protectedRefs = computeProtectedRefs(messages, state, cfg, defaultCountTokens); - const ranges = buildCompressibleRanges(messages, state, cfg, protectedRefs, defaultCountTokens, messages); - const merged = mergeRangesToThreshold(ranges.compressible, gate); - assert.ok(merged.length > 0); - const screened = screenRecommendedUnits(merged, messages, state, cfg, protectedRefs); - assert.ok( - screened.length > 0, - "bridged unit must survive projectUnit (clamp keeps the adjacent block out of the span)", - ); - // The surviving span must not reach into covered territory: its END stays - // on the visible side of the mid-group block cut. - const covered = new Set<string>(); - for (const b of state.blocks ?? []) { - if (b.active) for (const id of b.effectiveMessageIds ?? []) covered.add(id); - } - const indexById = new Map<string, number>(); - messages.forEach((m, i) => indexById.set(m.id!, i)); - for (const unit of screened) { - const endIdx = indexById.get(unit.endRef); - assert.ok(endIdx !== undefined, "endRef resolves to an array slot"); - assert.ok( - !covered.has(messages[endIdx!]!.id!), - "span end stays on the visible side of the block boundary", - ); - } - }); - - it("full recommend chain advertises ranges for the incident shape", () => { - const { messages, state } = buildIncident(); - const cfg = config(); - const gate = minChars(cfg); - const protectedRefs = computeProtectedRefs(messages, state, cfg, defaultCountTokens); - const ranges = buildCompressibleRanges(messages, state, cfg, protectedRefs, defaultCountTokens, messages); - const merged = mergeRangesToThreshold(ranges.compressible, gate); - const screened = screenRecommendedUnits(merged, messages, state, cfg, protectedRefs); - assert.ok( - screened.length > 0, - "pre-fix this funnel returned [] and the host fell back to physical pruning", - ); - const viable = screened.filter((r) => r.tokens >= 200); - assert.ok(viable.length > 0, "at least one unit clears the 200-token viable floor"); - }); -}); From d0582823109a22606d31877adbd635f7c3cb5f2c Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 21:42:24 +0800 Subject: [PATCH 75/78] fix(storage): mysql archive restore keeps the NEWEST matching dump line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #6317 union of master's newest-wins scan with the count-all variant left a stray unconditional 'lineNo = i;' that clobbered the guard and ended the reverse scan on the OLDEST matching line — restoring a stale 2-message dump where the newest 5-message dump was expected (tests/storage-archive-failed-sweep.test.ts, failed once in CI). Drop the stray assign; the sibling sqlite path (src/storage/store.ts) was already correct. --- src/storage/mysql.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index 1f241320ac..72bdf1268d 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -1866,7 +1866,6 @@ export class MysqlBackend implements StorageBackend { if (candidate && typeof candidate === "object" && "sessionId" in candidate && candidate.sessionId === sessionId) { if (lineNo === -1) lineNo = i; matchCount += 1; - lineNo = i; } } } catch { From f1d696e1a3a918c759df43c32ad588fd7925d1cf Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Fri, 13 Nov 2026 22:19:49 +0800 Subject: [PATCH 76/78] Revert "sync: decide block liveness by coverage shape, not existence" This reverts commit f7406e5ad9bb937cd2712c5547eb0396ead91a08. --- kernel/src/sync.ts | 80 ++++------------------------- kernel/tests/sync-config.test.ts | 88 ++------------------------------ 2 files changed, 14 insertions(+), 154 deletions(-) diff --git a/kernel/src/sync.ts b/kernel/src/sync.ts index b1e462908a..87008df298 100644 --- a/kernel/src/sync.ts +++ b/kernel/src/sync.ts @@ -5,13 +5,7 @@ import type { CompressionState, CoreMessage } from "./types.js"; * inside the block's exact interval claim. Unbounded blocks and unstamped * views answer false — the id path decides those. The -1/-1 "no claim" * sentinel (PR #4725 N1) also answers false: it is absence of evidence, not - * a positional claim (a [0,0] row IS a real claim over seq 0). - * - * Kept exactly as upstream states it: when NO covered id survives, a position - * inside the interval is far more likely id churn (re-mint, merged #2636 pair) - * than deletion, and a merge legitimately covers fewer ids than positions. The - * coverage-shape rule below only governs the shapes where ids are present. - */ + * a positional claim (a [0,0] row IS a real claim over seq 0). */ function blockSeqPresent( block: { startSeq?: number; endSeq?: number }, messages: CoreMessage[], @@ -38,33 +32,6 @@ function blockSeqPresent( return false; } -type CoverageShape = "full" | "tail-gap" | "head-gap" | "holes" | "none"; - -/** Where the missing covered ids sit, in the block's own coverage order. - * - * - `tail-gap` survivors are a prefix, everything missing is the newest end: - * a rewind removed the span's tail -> the summary is stale. - * - `head-gap` survivors are a suffix, everything missing is the oldest end: - * the host dropped old history; the summary is the ONLY record - * of it, so dropping it too would be the actual loss. - * - `holes` missing cells are interior/ragged: the churn shape (client - * re-serialized history), never the rewind shape. Do not guess. - * - `none` nothing covered is visible; ordering evidence decides. - */ -function coverageShape( - block: { effectiveMessageIds: string[] }, - presentBases: Set<string>, -): CoverageShape { - const present = block.effectiveMessageIds.map((id) => presentBases.has(baseIdOf(id))); - if (present.length === 0 || present.every(Boolean)) return "full"; - if (!present.some(Boolean)) return "none"; - const firstPresent = present.indexOf(true); - const lastPresent = present.lastIndexOf(true); - if (present.slice(0, lastPresent + 1).every(Boolean)) return "tail-gap"; - if (present.slice(firstPresent).every(Boolean)) return "head-gap"; - return "holes"; -} - export interface SyncResult { state: CompressionState; deactivated: string[]; @@ -211,42 +178,15 @@ export function syncBlocks( // (pruned view) is still present — the summary IS the block's visible // representation. Without this, hosts passing pruned views would lose // block activity every turn. - // #4592: the question is COVERAGE, not existence. `some` answered "is any of - // it still there" and kept injecting a summary that described a half-rewound - // span; `every` answered "is all of it there" but treated id churn like a - // deletion. The shape of the gap decides: - // - // tail gap -> rewind (the host withdrew the newest end) -> stale, drop - // head gap -> the host dropped OLD history; the summary is the only - // record of it, so keep it (dropping it would lose content) - // holes -> id churn (interior gaps are never the rewind shape): - // keep it; `fold-reconcile` re-keys churned ids upstream - // none -> nothing id-shaped survived: defer to the positional - // witness (upstream #4316, unchanged), which is the churn / - // merged-pair shape rather than a deletion - // - // Everything except the tail gap keeps today's keep-alive behaviour, so the - // only behaviour that changes is the one shape that can be named. - const shape = coverageShape(block, presentBases); - let stillPresent: boolean; - if (presentIds.has(summaryMessageId(block.blockId))) { - // Pruned view: the summary IS the block's visible representation. - stillPresent = true; - } else { - switch (shape) { - case "full": - case "head-gap": - case "holes": - stillPresent = true; - break; - case "tail-gap": - stillPresent = false; - break; - case "none": - stillPresent = blockSeqPresent(block, messages); - break; - } - } + // #4316 layer 3: a block with an exact ledger interval is also still + // present when ANY visible message carries a ledgerSeq stamp inside the + // interval — the host restored/re-stamped the originals (fresh ids, same + // positions), and the positional claim survives the id churn that the + // id-membership check above just missed. + const stillPresent = + block.effectiveMessageIds.some((id) => presentBases.has(baseIdOf(id))) || + presentIds.has(summaryMessageId(block.blockId)) || + blockSeqPresent(block, messages); if (!stillPresent) { block.active = false; deactivated.push(block.blockId); diff --git a/kernel/tests/sync-config.test.ts b/kernel/tests/sync-config.test.ts index 0ebbbc524b..b376e43a6b 100644 --- a/kernel/tests/sync-config.test.ts +++ b/kernel/tests/sync-config.test.ts @@ -31,101 +31,21 @@ test("syncBlocks deactivates blocks whose messages are all gone", () => { const state = createInitialState(); state.blocks.push( makeBlock({ blockId: "b1", effectiveMessageIds: ["gone1", "gone2"] }), + makeBlock({ blockId: "b2", effectiveMessageIds: ["kept", "also-gone"] }), ); const result = syncBlocks([msg("kept")], state); assert.deepEqual(result.deactivated, ["b1"]); assert.equal(result.state.blocks[0]!.active, false); + assert.equal(result.state.blocks[1]!.active, true); }); -// #4592: upstream kept a block alive as long as SOME covered message remained. -// That is exactly the shape a host rewind leaves behind — the survivors are a -// prefix and the newest end is gone, so the summary describes history that no -// longer exists. Coverage SHAPE decides, not existence: see the head-gap and -// holes tests below for the shapes that must KEEP their summary. -test("syncBlocks deactivates a block whose span was partially truncated (rewind)", () => { - const state = createInitialState(); - state.blocks.push( - makeBlock({ blockId: "b1", effectiveMessageIds: ["kept", "also-gone"] }), - ); - const result = syncBlocks([msg("kept")], state); - assert.deepEqual(result.deactivated, ["b1"]); - assert.equal(result.state.blocks[0]!.active, false); -}); - -// #4592: the mirror shape of a rewind. The host dropped OLD history from the -// head; the summary is the only record of it, so dropping the summary too is the -// actual loss. This is what makes sliding-window / head-truncating hosts safe. -test("syncBlocks keeps a block whose oldest coverage the host dropped (head gap)", () => { - const state = createInitialState(); - state.blocks.push( - makeBlock({ blockId: "b1", effectiveMessageIds: ["old-1", "old-2", "kept"] }), - ); - const result = syncBlocks([msg("kept"), msg("fresh")], state); - assert.deepEqual(result.deactivated, []); - assert.equal(result.state.blocks[0]!.active, true); -}); - -// #4592: interior gaps are the id-churn shape (client re-serialized its -// history), never the rewind shape. Do not guess: keep the summary and let -// fold-reconcile re-key the churned ids. -test("syncBlocks keeps a block with interior id gaps (churn shape)", () => { - const state = createInitialState(); - state.blocks.push( - makeBlock({ blockId: "b1", effectiveMessageIds: ["a", "churned", "c"] }), - ); - const result = syncBlocks([msg("a"), msg("c")], state); - assert.deepEqual(result.deactivated, []); - assert.equal(result.state.blocks[0]!.active, true); -}); - -// #4592: nothing id-shaped survives, but a visible message carries a ledger -// position INSIDE the block's interval — the churn / merged-pair shape (a merge -// legitimately covers fewer ids than positions). The positional witness is -// upstream's #4316 rule, unchanged by the shape decision. -test("syncBlocks keeps a fully-gone block with a position inside its interval (churn witness)", () => { - const state = createInitialState(); - state.blocks.push( - makeBlock({ - blockId: "b1", - effectiveMessageIds: ["gone-1", "gone-2"], - startSeq: 1, - endSeq: 2, - }), - ); - const visible: CoreMessage[] = [ - { id: "re-minted", role: "user", contentType: "text", text: "merged", ledgerSeq: 2 }, - ]; - const result = syncBlocks(visible, state); - assert.deepEqual(result.deactivated, []); - assert.equal(result.state.blocks[0]!.active, true); -}); - -// #4592: no id and no position survives -> dead (the plain deletion shape). -test("syncBlocks deactivates a fully-gone block with no positional witness", () => { - const state = createInitialState(); - state.blocks.push( - makeBlock({ - blockId: "b1", - effectiveMessageIds: ["gone-1"], - startSeq: 1, - endSeq: 1, - }), - ); - const visible: CoreMessage[] = [ - { id: "newer", role: "user", contentType: "text", text: "newer", ledgerSeq: 9 }, - ]; - const result = syncBlocks(visible, state); - assert.deepEqual(result.deactivated, ["b1"]); - assert.equal(result.state.blocks[0]!.active, false); -}); - -test("syncBlocks leaves blocks intact when every covered message remains", () => { +test("syncBlocks leaves blocks intact when at least one message remains", () => { const state = createInitialState(); state.blocks.push( makeBlock({ blockId: "b1", effectiveMessageIds: ["a", "b"] }), ); - const result = syncBlocks([msg("a"), msg("b")], state); + const result = syncBlocks([msg("a")], state); assert.deepEqual(result.deactivated, []); assert.equal(result.state.blocks[0]!.active, true); }); From 369be2dab714235182e4ce8c38d48820838289e1 Mon Sep 17 00:00:00 2001 From: ranxianglei <ranxianglei@gmail.com> Date: Sat, 14 Nov 2026 00:46:23 +0800 Subject: [PATCH 77/78] test(notices): delete the CI baseline's BILI_STORAGE_UNIFIED override for this suite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This suite asserts the PRODUCTION default storage posture. The CI corpus baseline (tests/preload-storage-baseline.mjs, #2815) forces BILI_STORAGE_UNIFIED=legacy for every test file; under that override the pure-sqlite-default notice added by #6480 (5a80e2b8e) is correctly reported as not-in-effect, so its fresh-install and dismiss subtests drop storage-sqlite-default from the expected list — deterministic red on the full matrix, first exposure on the post-#6480 runs of this PR. The preload documents the rule: any suite whose assertions depend on a specific storage mode must pin its own selection (the #4011 lesson). This adopts master's version of the file and deletes the baseline override for this file only, restoring the production default the assertions pin. Verified 5/5 under the exact CI preload set on node 22.23.4 against the merged tree. --- tests/notice-registry.test.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/notice-registry.test.ts b/tests/notice-registry.test.ts index 65c02544f9..c153afcceb 100644 --- a/tests/notice-registry.test.ts +++ b/tests/notice-registry.test.ts @@ -18,14 +18,21 @@ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), "notice-reg-dsh-")) process.env.XDG_STATE_HOME = stateDir; process.env.BILI_CONFIG_FILE = cfgPath; -const { NOTICES, visibleNotices } = await import("../src/notices.ts"); +// This suite asserts the PRODUCTION default storage posture. The CI corpus +// baseline (tests/preload-storage-baseline.mjs, #2815) forces +// BILI_STORAGE_UNIFIED=legacy for every test file, which flips the +// pure-sqlite-default notice (#6480) off — pin our own selection per the +// baseline's documented rule (the #4011 lesson). +delete process.env.BILI_STORAGE_UNIFIED; + +const { NOTICES, visibleNotices, SQLITE_DEFAULT_NOTICE_ID } = await import("../src/notices.ts"); const { IOT_INCOMPAT_NOTICE_ID, PCK_NOTICE_ID, WM_FASTPATH_INTRO_ID, dismissUpgradeNotice } = await import("../src/pck-notice.ts"); const { MESSAGES, LOCALES } = await import("../src/web/i18n.ts"); test("registry: unique ids, valid tones, every key resolves in both locales", () => { const ids = NOTICES.map((n) => n.id); assert.equal(new Set(ids).size, ids.length, "ids must be unique"); - assert.deepEqual([...ids].sort(), [IOT_INCOMPAT_NOTICE_ID, PCK_NOTICE_ID, WM_FASTPATH_INTRO_ID].sort()); + assert.deepEqual([...ids].sort(), [IOT_INCOMPAT_NOTICE_ID, PCK_NOTICE_ID, SQLITE_DEFAULT_NOTICE_ID, WM_FASTPATH_INTRO_ID].sort()); for (const n of NOTICES) { assert.ok(n.tone === "ok" || n.tone === "warn", `${n.id} tone`); for (const locale of LOCALES) { @@ -37,10 +44,16 @@ test("registry: unique ids, valid tones, every key resolves in both locales", () } }); -test("visibleNotices: fresh install sees both, registry order (iot absent ⇒ hidden)", () => { +test("visibleNotices: fresh install sees the default flip + pck + wm intro, registry order (iot absent ⇒ hidden)", () => { const v = visibleNotices(); - assert.deepEqual(v.map((n) => n.id), [PCK_NOTICE_ID, WM_FASTPATH_INTRO_ID]); - assert.deepEqual(v.map((n) => n.tone), ["warn", "ok"]); + assert.deepEqual(v.map((n) => n.id), [PCK_NOTICE_ID, WM_FASTPATH_INTRO_ID, SQLITE_DEFAULT_NOTICE_ID]); + assert.deepEqual(v.map((n) => n.tone), ["warn", "ok", "warn"]); +}); + +test("storage-sqlite-default notice id is a dismissible builtin (no 400 on close)", () => { + assert.equal(dismissUpgradeNotice(SQLITE_DEFAULT_NOTICE_ID, process.env), true); + // restore visibility for the later tests (fresh-state dismissal is persisted per XDG dir) + fs.rmSync(stateDir, { recursive: true, force: true }); }); test("iot notice id is a dismissible builtin (no 400 on close)", () => { @@ -50,5 +63,5 @@ test("iot notice id is a dismissible builtin (no 400 on close)", () => { test("visibleNotices: dismissing one hides only that one", () => { assert.equal(dismissUpgradeNotice(WM_FASTPATH_INTRO_ID, process.env), true); const v = visibleNotices(); - assert.deepEqual(v.map((n) => n.id), [PCK_NOTICE_ID]); + assert.deepEqual(v.map((n) => n.id), [PCK_NOTICE_ID, SQLITE_DEFAULT_NOTICE_ID]); }); From d04a8adcb9a9ac7275400a85124af88521439f6f Mon Sep 17 00:00:00 2001 From: ci-check <ci-check@local> Date: Sat, 14 Nov 2026 05:51:48 +0800 Subject: [PATCH 78/78] test(#5025): reset the native origin waiter so the suite's static pi-native import cannot leak INLINE_ORIGIN egress The #5025 test statically imports pi-native.ts, which arms at module load. Since the #5428 inline default flip, plan.mode is 'inline' with no explicit env/file choice, so armNativePi registers the origin waiter with INLINE_ORIGIN. The waiter persists into the shared factory's awaitNativeProxyOrigin(), which makes the before_provider_headers handler resolve proxyBase=INLINE_ORIGIN and call registerTools -> fetchManifest, egressing to the inline origin inside the hermetic suite. Add a _resetNativeOriginWaiterForTest hook (native-bootstrap) and call it in the suite's beforeEach so the waiter is cleared per test. Fixes the 6 red test lanes (issue5025-pi-direct-window.test.ts 1630/1631) on head e484c0322f. --- src/agent/native-bootstrap.ts | 7 +++++++ tests/issue5025-pi-direct-window.test.ts | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/agent/native-bootstrap.ts b/src/agent/native-bootstrap.ts index a437f315a3..026c34b4a5 100644 --- a/src/agent/native-bootstrap.ts +++ b/src/agent/native-bootstrap.ts @@ -185,3 +185,10 @@ export function setNativeOriginWaiter(waiter: NativeOriginWaiter): void { export async function awaitNativeProxyOrigin(): Promise<string | undefined> { return originWaiter === undefined ? undefined : originWaiter.wait(); } + +/** Test hook: clear the registered origin waiter so suites that import a + * native entry (which arms at module load) do not leak its INLINE_ORIGIN + * waiter into the shared factory's awaitNativeProxyOrigin(). */ +export function _resetNativeOriginWaiterForTest(): void { + originWaiter = undefined; +} diff --git a/tests/issue5025-pi-direct-window.test.ts b/tests/issue5025-pi-direct-window.test.ts index 92a4189447..f8056c1844 100644 --- a/tests/issue5025-pi-direct-window.test.ts +++ b/tests/issue5025-pi-direct-window.test.ts @@ -18,6 +18,7 @@ import path from "node:path"; import { after, beforeEach, test } from "node:test"; import { createBiliPlugin, piDeclaredWindowForDirect, _resetPiDeclaredWindowForTest } from "../src/agent/pi.ts"; import { _declaredWindowHookForTest } from "../src/agent/pi-native.ts"; +import { _resetNativeOriginWaiterForTest } from "../src/agent/native-bootstrap.ts"; const STATE_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "bili-5025-state-")); const PREV_ENV: Record<string, string | undefined> = {}; @@ -89,6 +90,7 @@ function chatBody(textChars: number): string { beforeEach(() => { _resetPiDeclaredWindowForTest(); + _resetNativeOriginWaiterForTest(); delete process.env.BILLION_CONTEXT_PROXY; });