From d86082ed8f0e3905bd031d9275cc1d183c76ffd1 Mon Sep 17 00:00:00 2001 From: MohammadErfan Jabbari <13975671+MohammadErfan-Jabbari@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:38:12 +0100 Subject: [PATCH 01/34] fix: pass Claude Code's MCP config as --mcp-config= so it cannot eat the prompt Claude Code's --mcp-config is variadic, so 'bili claude "do X"' failed with 'MCP config file not found: /do X'. Co-Authored-By: Claude Opus 5.5 --- src/launcher.ts | 2 +- tests/launcher.test.ts | 48 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/launcher.ts b/src/launcher.ts index 285a07a6c6..97edaca9a7 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -7685,7 +7685,7 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} const mcpFile = path.join(os.tmpdir(), `bili-mcp-${Date.now()}.json`); fs.writeFileSync(mcpFile, JSON.stringify(buildMcpConfig(origin))); tmpFiles.push(mcpFile); - clientArgs = ["--mcp-config", mcpFile, ...clientArgs]; + clientArgs = [`--mcp-config=${mcpFile}`, ...clientArgs]; } } diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index b98142051c..91c4b2f3ab 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -5849,6 +5849,54 @@ test("runLaunch codex: old binary without --no-daemon launches unchanged (#1867) } }); +test("runLaunch claude: the injected --mcp-config does not swallow a positional prompt", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-prompt-")); + const prevHome = process.env.HOME; + const prevClientBin = process.env.BILI_CLIENT_BIN; + const prevPlugin = process.env.BILI_LAUNCHER_PLUGIN; + process.env.HOME = home; + delete process.env.BILI_LAUNCHER_PLUGIN; + const fakeClaude = path.join(home, process.platform === "win32" ? "fake-claude.exe" : "fake-claude"); + fs.writeFileSync(fakeClaude, ""); + process.env.BILI_CLIENT_BIN = fakeClaude; + const clientArgs: string[][] = []; + const spawnImpl: SpawnFn = (cmd, args) => { + if (cmd === fakeClaude) { + clientArgs.push([...(args ?? [])]); + const child = makeFakeChild(0); + const orig = child.on!.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); + return child; + }; + return child; + } + return makeFakeChild(42422); + }; + const prevExit = process.exit; + process.exit = (() => undefined) as typeof process.exit; + try { + await runLaunch( + { client: "claude", clientArgs: ["say hi", "-p"], overrides: {} }, + { fetchImpl: async () => ({ ok: true }), fetchHealthInfo: async () => ({ ok: true, pid: lastFakeChildPid }), spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientArgs.length, 1); + const args = clientArgs[0]!; + // Claude Code's --mcp-config is variadic: a bare flag takes every following word as a config file. + assert.equal(args.includes("--mcp-config"), false, `bare --mcp-config would eat the prompt: ${JSON.stringify(args)}`); + assert.ok(args.some((a) => a.startsWith("--mcp-config=")), "bili's MCP config is still passed"); + assert.ok(args.includes("say hi")); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevClientBin; + if (prevPlugin !== undefined) process.env.BILI_LAUNCHER_PLUGIN = prevPlugin; + rmrf(home); + } +}); + test("runLaunch claude: CLAUDE_CODE_AUTO_COMPACT_WINDOW injected (built-in table window)", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-budget-")); const prevHome = process.env.HOME; From dfc1a291aff5c253d8b6c4b2858e4e0653e8db1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8D=93=E6=81=92=28Zhuoheng=20Zou=29?= Date: Fri, 13 Nov 2026 18:12:14 +0800 Subject: [PATCH 02/34] fix: retry context overflow with measured output headroom Fixes ranxianglei/billion-context#6528 --- docs/technical-notes.md | 3 + docs/technical-notes.zh-CN.md | 3 + src/server/relay.ts | 124 ++++++++++++++++-------- tests/issue1195-overflow-refold.test.ts | 96 +++++++++++++++++- 4 files changed, 180 insertions(+), 46 deletions(-) diff --git a/docs/technical-notes.md b/docs/technical-notes.md index 0e5142f790..c3ce68259d 100644 --- a/docs/technical-notes.md +++ b/docs/technical-notes.md @@ -7,6 +7,9 @@ here instead of between the options. ## Rejected-request token measurements Context-overflow recovery distinguishes the upstream context window from the rejected request's total, input-message count, and completion reservation. The parser accepts both the OpenAI/DeepSeek parenthesized split (`requested N tokens (M in the messages, C in the completion)`) and the SGLang/LiteLLM colon-separated split (`requested a total of N tokens: M tokens from the input messages and C tokens for the completion`). Explicit input counts feed the existing calibration ring; completion counts arm the existing output-headroom reservation. A window-only or incomplete error provides no invented token measurements. This parsing is shared by plugin and proxy modes and does not change the declared model window. + +When a 400/413 reports a complete, consistent token split and the input fits, the relay can retry that payload once with the output budget capped at the measured remaining space. It checks the reported completion reservation against the actual output field and preserves an explicit thinking budget. The same recovery applies after a refold. Requests with an egress transform configured use the existing refold path because the transform can change the input between attempts. + ## Fork recovery after a short source view Fork adoption judges retained compression blocks against the child replay, rather than the source's last `active` flags. A short source request may temporarily deactivate a fold whose complete original span returns in the child. Fully present retained spans are copied as active blocks; explicitly expanded blocks, consumed tier children, and spans crossing the fork boundary are not independently activated. Tier children accompany an adopted ancestor as inactive records. Planning and adoption never mutate the source session. diff --git a/docs/technical-notes.zh-CN.md b/docs/technical-notes.zh-CN.md index 89d4515675..f051d78c66 100644 --- a/docs/technical-notes.zh-CN.md +++ b/docs/technical-notes.zh-CN.md @@ -5,6 +5,9 @@ README 三种使用方式背后的机制级说明。README 里每种方式只保 ## 被拒请求的 token 计量 上下文溢出恢复分别处理上游窗口上限、被拒请求的总量、输入消息量和回复预留量。解析器同时识别 OpenAI/DeepSeek 的括号句式(`requested N tokens (M in the messages, C in the completion)`)及 SGLang/LiteLLM 的冒号句式(`requested a total of N tokens: M tokens from the input messages and C tokens for the completion`)。明确的输入计数进入现有校准环;回复计数触发现有的输出余量预留。只有窗口上限或拆分不完整的错误不会产生虚构计量。插件模式与代理模式共用该解析,不改写声明的模型窗口。 + +当 400/413 给出完整且一致的 token 计数,并且输入本身能放进窗口时,中继可以按实测剩余空间缩减回复额度,并用同一份输入重试一次。中继会核对报告的回复预留量与实际请求字段,并为显式思考预算保留空间。重新折叠后的请求也可以按同样规则恢复。配置了出站改写的请求继续走现有的重新折叠流程,因为改写可能让两次发送的输入不同。 + ## 来源短视图之后的 fork 恢复 fork 继承依据子会话重放内容判断保留的压缩块,不直接沿用来源上一次请求的 `active` 标记。来源的一次短请求可能暂时停用某个块,而子会话重放时包含它覆盖的完整原文。完整存在的保留块独立复制并激活;明确解压的块、被高层块消费的子块及跨越 fork 边界的块不会独立激活。被继承高层块的子块作为非活动记录随行。规划和继承均不修改来源会话。 diff --git a/src/server/relay.ts b/src/server/relay.ts index 7e34790cf8..d9db156374 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -74,9 +74,32 @@ 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 { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-responses.js"; +import { outputBudgetField, readOutputBudget, writeOutputBudget } from "./side-request.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"; +/** Retry the same input when the provider proved only its output reservation overflows. */ +function capOverflowOutput(body: string | Buffer, info: ContextOverflowInfo): { body: string; cap: number } | null { + const { window, requestedTotal: total, requestedMessages: input, requestedCompletion: output } = info; + if (!info.isOverflow || typeof body !== "string" || window === undefined || input === undefined || output === undefined) return null; + const cap = window - input; + if (!Number.isSafeInteger(window) || !Number.isSafeInteger(input) || input <= 0 || cap <= 0 || cap >= output) return null; + if (total !== input + output) return null; + + let parsed: Record; + try { + parsed = JSON.parse(body) as Record; + } catch { + return null; + } + const field = outputBudgetField(parsed); + if (field === null || readOutputBudget(parsed, field) !== output) return null; + const thinking = parsed.thinking as { budget_tokens?: unknown } | undefined; + if (typeof thinking?.budget_tokens === "number" && cap <= thinking.budget_tokens) return null; + writeOutputBudget(parsed, field, cap); + return { body: JSON.stringify(parsed), cap }; +} + /** Infer the wire protocol from the request path for compat-role rewrites on * requests the pipeline did not prepare (passthrough). Mirrors the path * checks in handleRequest; returns null when unknown (no rewrite). */ @@ -1813,12 +1836,9 @@ async function forwardCaptured( }; // #1195: a context overflow used to arm the shrink and pass the 400 through // verbatim, expecting the NEXT turn to fold — but wire clients treat the - // error as fatal and end the session, so the armed rescue never runs and the - // session locks. With a refold hook: arm, let the caller re-run prepare+ - // preflight against the window the upstream STATED (per-call override, no - // learning), and re-send the folded body ONCE within this same request. An - // unchanged body (nothing foldable), a null refold, or a transport failure - // falls back to today's verbatim passthrough below. + // error as fatal and end the session. Try a budget-only retry when the + // provider proves the current input fits. Otherwise refold within this + // request, with the same budget-only option on the newly measured body. if ( overflowRefold && prepared?.session && @@ -1921,46 +1941,64 @@ async function forwardCaptured( // margin under it, the same headroom discipline as the arm's // own min(declared, est) clamp — the rejection proved the // ceiling is below the payload, give the fold real room. - const armedWindow = overflowInfo.window ?? (prepared.session.stats.lastInputTokensSource === "overflow-arm" ? Math.floor(prepared.session.stats.lastInputTokens * 0.9) : undefined); - const refolded = await overflowRefold(armedWindow).catch(() => null); - if (refolded) { - try { - // #816 family: the refold rebuilds the body from state - // summaries — scrub string bodies again so a lone - // surrogate can't poison the retry (see the #1884 seam). - const wireRefolded = typeof refolded.body === "string" ? scrubLoneSurrogatesOnWire(refolded.body) : refolded.body; - const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: await finalizeEgress(wireRefolded) }, undefined, clientAbort.signal); - if (retried.response.ok) { - upstreamResult.clearTimer(); - wireBody = wireRefolded; // #1900: track the accepted re-send as the wire base - upstreamResult = retried; - log("info", `[${prepared.session.id}] context overflow — refolded and re-sent within the same request, upstream accepted (#1195)`); - prepared = refolded.prepared; - } else { - let retryErrText: string | null = null; - if (retried.response.body) { - try { - retryErrText = (await readStreamToBuffer(retried.response.body)).toString("utf8"); - } catch { - retryErrText = null; - } + const s = prepared.session; + const armedWindow = overflowInfo.window ?? (s.stats.lastInputTokensSource === "overflow-arm" ? Math.floor(s.stats.lastInputTokens * 0.9) : undefined); + // Keep the latest complete upstream verdict if recovery cannot continue. + const resendOverflow = async (retryBody: string | Buffer): Promise => { + const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: await finalizeEgress(retryBody) }, undefined, clientAbort.signal); + upstreamResult.clearTimer(); + if (retried.response.ok) { + wireBody = retryBody; // #1900: the accepted body is the next wire base + upstreamResult = retried; + return null; + } + const retryText = retried.response.body + ? await readStreamToBuffer(retried.response.body).then(body => body.toString("utf8"), () => "") + : ""; + upstreamResult = { + ...retried, + response: new Response(retryText, { + status: retried.response.status, + statusText: retried.response.statusText, + headers: new Headers(retried.response.headers), + }), + }; + if (retried.response.status !== 400 && retried.response.status !== 413) { + return { isOverflow: false, message: "" }; + } + return inspectContextOverflow(retried.response.status, retryText); + }; + // A user transform can change the input again on the retry stage, + // so the rejected token count cannot bound that send. + const mayRetryWithCap = !egressBaseFields; + const resendWithCap = async (recovery: { body: string; cap: number }): Promise => { + log("info", `[${s.id}] context overflow — retrying with output capped at ${recovery.cap} using upstream's measured input (#1195)`); + return resendOverflow(recovery.body); + }; + try { + let rejection: ContextOverflowInfo | null = overflowInfo; + const cap = mayRetryWithCap ? capOverflowOutput(wireBody, overflowInfo) : null; + if (cap !== null && !clientAbort.signal.aborted) { + rejection = await resendWithCap(cap); + if (rejection === null) log("info", `[${s.id}] context overflow — budget-only retry accepted (#1195)`); + } + if (rejection?.isOverflow && !clientAbort.signal.aborted) { + const refolded = await overflowRefold(rejection.window ?? armedWindow).catch(() => null); + if (refolded) { + const retryBody = typeof refolded.body === "string" ? scrubLoneSurrogatesOnWire(refolded.body) : refolded.body; + rejection = await resendOverflow(retryBody); + if (mayRetryWithCap && rejection?.isOverflow && !clientAbort.signal.aborted) { + const foldedCap = capOverflowOutput(retryBody, rejection); + if (foldedCap !== null) rejection = await resendWithCap(foldedCap); + } + if (rejection === null) { + prepared = refolded.prepared; + log("info", `[${s.id}] context overflow — refolded and re-sent within the same request, upstream accepted (#1195)`); } - // Answer with the retry's own verdict, not the stale first 400. - upstreamResult.clearTimer(); - upstreamResult = { - response: new Response(retryErrText ?? "", { - status: retried.response.status, - statusText: retried.response.statusText, - headers: new Headers(retried.response.headers), - }), - clearTimer: retried.clearTimer, - stopIdleTimer: retried.stopIdleTimer, - }; - log("warn", `[${prepared.session.id}] context overflow — refold retry still rejected (HTTP ${retried.response.status}); passing the retry response through (#1195)`); } - } catch { - // transport failure — the buffered original 400 answers below } + } catch { + // The most recent buffered rejection answers below. } } } diff --git a/tests/issue1195-overflow-refold.test.ts b/tests/issue1195-overflow-refold.test.ts index 1d4faca825..eca972199c 100644 --- a/tests/issue1195-overflow-refold.test.ts +++ b/tests/issue1195-overflow-refold.test.ts @@ -114,7 +114,10 @@ interface RecordedUpstream { // Records every request body in order. Streaming requests are answered by // `onStreaming(callIndex)`; stream:false (summarization) requests by the // in-band summary response. -async function recordedUpstream(onStreaming: (callIndex: number, res: http.ServerResponse) => void): Promise { +async function recordedUpstream( + onStreaming: (callIndex: number, res: http.ServerResponse, body: string) => void, + summarize = summaryJson, +): Promise { const bodies: string[] = []; let streamingCall = 0; const server = http.createServer((req, res) => { @@ -131,12 +134,12 @@ async function recordedUpstream(onStreaming: (callIndex: number, res: http.Serve } if (parsed.stream === false) { res.writeHead(200, { "content-type": "application/json" }); - res.end(summaryJson()); + res.end(summarize()); return; } const call = streamingCall; streamingCall += 1; - onStreaming(call, res); + onStreaming(call, res, raw); }); }); server.listen(0, "127.0.0.1"); @@ -336,3 +339,90 @@ test("e2e #1195 C: when the folded retry is ALSO rejected, the client sees the R assert.equal(s!.stats.overflowArmTokens, 128000, "#1110 arm record stays at the stated window"); assert.equal(s!.metadata.confirmedContextLimits, undefined, "#987: nothing learned"); }); + +for (const scenario of ["fits-before-refold", "fits-after-refold", "still-oversized", "budget-retry-rate-limited", "mismatched-output"] as const) { + test(`e2e #1195: LiteLLM input ${scenario}`, async (t) => { + const window = 128_000; + const firstInput = scenario === "fits-after-refold" || scenario === "still-oversized" ? 150_000 : 108_000; + const foldedInput = scenario === "still-oversized" ? 129_000 : 108_000; + const upstream = await recordedUpstream((call, res, raw) => { + const { max_tokens: output } = JSON.parse(raw) as { max_tokens: number }; + const input = call === 0 ? firstInput : foldedInput; + if (scenario === "budget-retry-rate-limited" && call === 1) { + res.writeHead(429, { "content-type": "application/json", "retry-after": "12" }); + res.end('{"error":{"message":"rate limited"}}'); + return; + } + if (input + output <= window) { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end( + 'data: {"id":"reply","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"recovered"},"finish_reason":null}]}\n\n' + + 'data: {"id":"reply","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":108000,"completion_tokens":1,"total_tokens":108001}}\n\n' + + "data: [DONE]\n\n", + ); + return; + } + const statedOutput = scenario === "mismatched-output" ? output + 1 : output; + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { type: "BadRequestError", message: + `Requested token count exceeds the model's maximum context length of ${window} tokens. You requested a total of ${input + statedOutput} tokens: ${input} tokens from the input messages and ${statedOutput} tokens for the completion.` + } })); + }, () => JSON.stringify({ choices: [{ message: { role: "assistant", content: SUMMARY_TEXT }, finish_reason: "stop" }] })); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer(proxyBaseOptions(upstream.port, 400_000)); + await once(proxy, "listening"); + t.after(async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.server.close(); + upstream.server.closeIdleConnections?.(); + await once(upstream.server, "close"); + }); + + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstream.port}/v1/chat/completions`; + const body = JSON.stringify({ model: "claude-test", max_tokens: 32_000, stream: true, messages: bigConversation() }); + const headers = { "content-type": "application/json", "x-acp-session": `refold-split-${scenario}` }; + const response = await fetch(url, { method: "POST", headers, body }); + const responseText = await response.text(); + const sends = upstream.bodies + .map(raw => JSON.parse(raw) as { stream?: boolean; max_tokens: number }) + .filter(body => body.stream === true); + if (scenario === "budget-retry-rate-limited") { + assert.equal(response.status, 429); + assert.equal(response.headers.get("retry-after"), "12"); + assert.match(responseText, /rate limited/); + assert.equal(upstream.bodies.length, 2); + return; + } + if (scenario === "mismatched-output") { + assert.equal(response.status, 400); + assert.equal(sends.length, 2, "only the existing refold is sent when reported output differs from the wire"); + assert.ok(JSON.stringify(sends[1]).includes(SUMMARY_TEXT)); + return; + } + if (scenario === "fits-before-refold") { + assert.equal(response.status, 200, responseText); + assert.match(responseText, /recovered/); + assert.equal(upstream.bodies.length, 2, "the exact payload fits after a budget-only retry, without any summarization"); + assert.deepEqual(sends[1], { ...sends[0], max_tokens: window - firstInput }); + return; + } + assert.ok(sends[1].max_tokens > window - foldedInput, "the folded retry must be rejected by the billed-token wall"); + assert.ok(JSON.stringify(sends[1]).includes(SUMMARY_TEXT), "the second send contains the refold"); + if (scenario === "fits-after-refold") { + assert.equal(response.status, 200, responseText); + assert.match(responseText, /recovered/); + assert.equal(sends.length, 3); + assert.equal(sends[2].max_tokens, window - foldedInput); + assert.deepEqual(sends[2], { ...sends[1], max_tokens: window - foldedInput }); + } else { + assert.equal(response.status, 400); + assert.match(responseText, /129000 tokens from the input messages/); + assert.equal(sends.length, 2, "an output clamp cannot rescue oversized input"); + } + }); +} From 99966e2afbee6bd8702eaa41bbadb085ae05a3e3 Mon Sep 17 00:00:00 2001 From: MohammadErfan Jabbari <13975671+MohammadErfan-Jabbari@users.noreply.github.com> Date: Fri, 13 Nov 2026 11:15:02 +0100 Subject: [PATCH 03/34] fix: un-show a nudge whose request the upstream rejected The kernel stamps a nudge as shown when it decides to inject it, before the request goes upstream. When the upstream rejects that request (a Claude Code session on v0.1.213, 2026-11-11: the nudge drew "role 'system' must precede an 'assistant' message"), the model never saw the nudge, yet the stamp holds the cadence as if it had: the next request was smaller than the rejected one, the decision read "growth=-21002 ... blocked: T1 (cadence)", and the next nudge waits until the context grows a full growth floor past the rejected request. On a 4xx for a request that carried a nudge, roll the shown stamps back to their pre-turn values and hold nudges until a request succeeds, so the next request goes out without the nudge that may have drawn the 400, and the turn after a success re-decides from the old reference. Co-Authored-By: Claude Opus 5.5 --- src/cache-ledger.ts | 1 + src/server.ts | 4 +- src/server/prepare-anthropic.ts | 10 +- src/server/relay.ts | 3 +- src/session.ts | 27 ++++ tests/rejected-nudge-rearm.test.ts | 198 +++++++++++++++++++++++++++++ 6 files changed, 238 insertions(+), 5 deletions(-) create mode 100644 tests/rejected-nudge-rearm.test.ts diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 3393e4ae5e..8cf2ab4a75 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1867,6 +1867,7 @@ export function settleUsageReport( // #793: a zero-total sample carries no information (gateway placeholder or // relay echo) — it must not clobber the last trusted lastInputTokens. if (s.total > 0) { + if (session.transient?.nudgeHeldAfterReject) delete session.transient.nudgeHeldAfterReject; session.stats.inputTokens += s.total; // #4899: USD spend ledger — input/cache-read/cache-write billed at the // per-request stamped price, in lockstep with the token counters above diff --git a/src/server.ts b/src/server.ts index f06ee81a2b..0c2a5da340 100644 --- a/src/server.ts +++ b/src/server.ts @@ -55,7 +55,7 @@ import { prepareResponses, prepareResponsesCompact } from "./server/prepare-resp import { FORCE_TEXT_PROTOCOL } from "./server/inject.js"; export { isChatGptCodexUpstream, isCodexResponsesLite, resolvePromptCacheKey, shouldInjectPromptCacheKey } from "./server/prepare-responses.js"; import { biliToolsDeclaredOnWire, countBiliToolUses, evaluateSelfHealRound, nudgeSuppressed, pluginLaneDegraded, pluginLaneRestore } from "./session-self-heal.js"; -import { getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, snapshotMessages, ensureCanonicalId, storeEffectiveConfig, foldCoverage, postRebuildAnchorTokens, setPostRebuildAnchor, tickPostRebuildAnchor, splitSessionWarnings, usageAnchorCycleStale, staleSmallAnchorFallback } from "./session.js"; +import { getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, type NudgeStamps, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, snapshotMessages, ensureCanonicalId, storeEffectiveConfig, foldCoverage, postRebuildAnchorTokens, setPostRebuildAnchor, tickPostRebuildAnchor, splitSessionWarnings, usageAnchorCycleStale, staleSmallAnchorFallback } from "./session.js"; import type { SeparatorManifest } from "./server/responses-ordering.js"; import { detectStaleInstall, isVersionNewer } from "./update.js"; import { decideEaddrinuse, eaddrinuseExitCode, isPortBusyError, postBindCanLadder, UPGRADE_REPLACEMENT_ENV, type FailFastReason } from "./listen-policy.js"; @@ -2059,6 +2059,8 @@ export type Prepared = { * before delivery was attempted (suppression / breaker / gate off) — * consumers test for "injected" (+ "gated" where pressure counts). */ nudgeOutcome?: "injected" | "vetoed" | "skipped" | "gated"; + /** The nudge stamps before this turn, so a rejected request can un-show its nudge (rejectNudge). */ + nudgeStampsBefore?: NudgeStamps; /** Render strategy the prepare used for processTurn ("none" for codex * compaction triggers / ACP_RENDER_NONE). The #422 fold-refresh hook in * forward() re-runs processTurn with the same strategy so the re-request diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index a7b1908856..f8e31afc42 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -16,7 +16,7 @@ import { maybeSnapshotView } from "./snapshot-render.js"; import { storedMessagesFor, storedRowLookupFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; -import { absorbAbortedTurnGrowth, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; +import { absorbAbortedTurnGrowth, nudgeStampsOf, restoreNudgeStamps, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { getStore } from "../persist.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; @@ -171,6 +171,7 @@ export async function prepareAnthropic( let judgeBaseline: readonly CoreMessage[] | undefined; let nudge: NudgeDecision | undefined; let nudgeOutcome: Prepared["nudgeOutcome"]; + let nudgeStampsBefore: Prepared["nudgeStampsBefore"]; let rebuiltMessages = parsed.messages; let anthropicCacheMarks: Map | undefined; let clientCacheControls: Map | undefined; @@ -366,8 +367,11 @@ export async function prepareAnthropic( await yieldToEventLoop(); // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); + nudgeStampsBefore = nudgeStampsOf(session); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, tokenCountSource, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; + const nudgeHeldAfterReject = session.transient?.nudgeHeldAfterReject === true; + if (nudgeHeldAfterReject) restoreNudgeStamps(session, nudgeStampsBefore); adoptContentStore(session, turn.contentStore); // #4528: split the kernel pass from its post-processing scan passes so // each is a bounded synchronous stretch rather than one long block. @@ -422,7 +426,7 @@ export async function prepareAnthropic( // #3482 Step 2.5: the ONE nudge gate. This lane has no compaction // trigger and no intercept health window — those differences are // explicit inputs on the responses lane. - const willInjectNudge = nudgeOn({ + const willInjectNudge = !nudgeHeldAfterReject && nudgeOn({ injectNudge: opts.compress.injectNudge, hasNudgePlan: !!turn.nudge, compressSurfaceOn: laneDecisions.compressSurfaceOn, @@ -678,7 +682,7 @@ export async function prepareAnthropic( // main-scale tiebreak sees this request's own upper bound. probeRequestMembership(session, collectRequestIds(incomingMsgIds, originalMessages), session.stats.localInputEstimate ?? 0); if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin; - return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; + return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } export const TRAILING_ASSISTANT_NUDGE = "[bili] Earlier context was compressed above. Continue the task."; diff --git a/src/server/relay.ts b/src/server/relay.ts index 7e34790cf8..b000f93b34 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -50,7 +50,7 @@ import { pipePluginChatWithStrip, pipePluginJson, pipePluginJsonlWithStrip, pipe import { describeCoreTextDuplication, estimateCoreMessages, estimateCoreMessagesUpperBytes } from "../preflight.js"; import { reasoningGuardEngages, runReasoningGuard } from "../reasoning-guard.js"; import { wrapStreamWithRunawayGuard } from "../runaway-guard.js"; -import { diagnoseSuccessWithoutUsage, markDirty, markNativeCompactionBoundary, withSessionLock, type Session } from "../session.js"; +import { diagnoseSuccessWithoutUsage, markDirty, rejectNudge, markNativeCompactionBoundary, withSessionLock, type Session } from "../session.js"; import { adoptContentStore, ccrLoopConfig, commitRetrievalNotes, commitRetrievals, contentStoreOf, dropRetrievals } from "../store.js"; import { emitStreamError } from "../stream-error.js"; import { rewriteGoogleJsonResponseAsync } from "../stream-google.js"; @@ -2097,6 +2097,7 @@ async function forwardCaptured( if (bodyText.length > 600) snippet += " …"; if (!snippet) snippet = "(no body)"; loggerLog("warn", `[${errSid}] ← upstream ${upstream.status}${reqIdText}: ${snippet}`); + if (prepared?.session && prepared.nudgeStampsBefore && upstream.status >= 400 && upstream.status < 500) rejectNudge(prepared.session, prepared.nudgeStampsBefore, upstream.status); // #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/session.ts b/src/session.ts index 33c9833c52..23d5900a29 100644 --- a/src/session.ts +++ b/src/session.ts @@ -338,6 +338,10 @@ export type Session = { * In-memory only: the abort→resume causality spans one process * lifetime, and a restart replants nothing. */ abortReanchor?: { anchor: number; at: number }; + /** The upstream rejected a request that carried a nudge: its stamps were + * rolled back, and no nudge rides until a request succeeds (the nudge may + * be what drew the rejection). Cleared by settleUsageReport. */ + nudgeHeldAfterReject?: boolean; /** #5683/#5943: the conversation's live nudge CREDIT plus the policy * flags, for the unprompted/out-of-bounds gates (src/unprompted-gate.ts). * Re-stamped on every prepare (injected rounds re-mint the window, @@ -1136,6 +1140,29 @@ export function absorbAbortedTurnGrowth(session: Session, tokenCount: number): v loggerLog("info", `[${session.id}] aborted-turn growth absorbed: nudge reference ${oldRef}→${nudge.lastPerMessageNudgeTokens} (meter ${tokenCount}, pre-abort anchor ${flag.anchor}, growth debt +${recovered} → ${nudge.absorbedDebt ?? 0} over ${nudge.absorbCount} absorb(s)) (#5293/#5415)`); } +export type NudgeStamps = { shown: number; byTier: Session["state"]["nudge"]["lastShownByTier"] }; + +export function nudgeStampsOf(session: Session): NudgeStamps { + return { shown: session.state.nudge.lastNudgeShownTokens, byTier: session.state.nudge.lastShownByTier }; +} + +export function restoreNudgeStamps(session: Session, stamps: NudgeStamps): void { + session.state.nudge.lastNudgeShownTokens = stamps.shown; + session.state.nudge.lastShownByTier = stamps.byTier; +} + +/** The kernel stamps a nudge as shown when it decides to inject it. When the + * upstream rejects that request the model never saw the nudge, so the stamp + * would hold the cadence against a nudge nobody received (a Claude Code session, + * 2026-11-11: the session's only nudge drew a 400 and it never nudged again). + * Roll the stamps back and hold nudges until the next success. */ +export function rejectNudge(session: Session, before: NudgeStamps, status: number): void { + restoreNudgeStamps(session, before); + (session.transient ??= {}).nudgeHeldAfterReject = true; + markDirty(session); + loggerLog("info", `[${session.id}] nudge rejected with its request (upstream ${status}): stamps rolled back to shown=${before.shown}; nudges held until a request succeeds`); +} + // #1820: post-rebuild meter anchor. Right after a big preflight rebuild the // usage-grade baseline is momentarily absent (the rebuild request's own report // hasn't landed yet, or the upstream never reports one), so effectiveTokenCount diff --git a/tests/rejected-nudge-rearm.test.ts b/tests/rejected-nudge-rearm.test.ts new file mode 100644 index 0000000000..fc4a780d73 --- /dev/null +++ b/tests/rejected-nudge-rearm.test.ts @@ -0,0 +1,198 @@ +/** + * A nudge the upstream rejects never reached the model, yet the kernel stamped it as + * shown when it decided to inject (lastNudgeShownTokens / lastShownByTier). The growth + * reference then sits at the rejected request's meter, so the cadence holds every later + * nudge until the context grows a full growth floor past the rejected request, although + * the model has seen no nudge. Seen in a Claude Code session on v0.1.213 (2026-11-11): + * the session's first nudge drew `400 messages.N: role 'system' must precede an + * 'assistant' message` (#5432), the next request was smaller than the rejected one and + * the decision read `growth=-21002 ... (ref=178592) blocked: T1 (cadence)`. + * + * Scenario (token model = ceil(chars/4), matching the mock's reported input_tokens): + * grow turn by turn until bili injects its nudge (request k) + * k+1 next request smaller than the rejected one → carries NO nudge (held until a success) + * k+2 next turn small growth → nudge present again (without the fix, cadence-blocked) + * Control session (same sizes, nudge accepted): k+2 carries no nudge — a delivered nudge + * still holds the cadence (negative direction). + */ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import { once } from "node:events"; +import { test } from "node:test"; +import type { ProxyOptions } from "../src/server.ts"; + +const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bc-rejected-nudge-")); +process.env.XDG_STATE_HOME = path.join(tmpRoot, "state"); +process.env.XDG_CACHE_HOME = path.join(tmpRoot, "cache"); +process.env.XDG_DATA_HOME = path.join(tmpRoot, "data"); +process.env.XDG_CONFIG_HOME = path.join(tmpRoot, "config"); +delete process.env.ACP_DUMP_BODY; + +const LIMIT = 200_000; +const MODEL = "claude-test"; + +const filler = (seed: number, chars: number): string => { + const words = ["context", "compression", "session", "anchor", "baseline", "growth", "interval", "meter"]; + let out = ""; + let i = 0; + while (out.length < chars) { + out += `${words[(i + seed) % words.length]}-${seed}-${i} `; + i += 1; + } + return out.slice(0, chars); +}; + +const SYSTEM = "You are a helpful coding assistant. Keep answers concise.\n" + filler(999, 800); +const TOOLS = [ + { name: "Bash", description: "Run a shell command.", input_schema: { type: "object", properties: { command: { type: "string" } }, required: ["command"] } }, + { name: "Read", description: "Read a file.", input_schema: { type: "object", properties: { path: { type: "string" } }, required: ["path"] } }, +]; + +type Msg = { role: "user" | "assistant"; content: Array<{ type: "text"; text: string }> }; +const um = (n: number, len: number): Msg => ({ role: "user", content: [{ type: "text", text: `Task ${n}:\n${filler(n, len)}` }] }); +const am = (n: number, len: number): Msg => ({ role: "assistant", content: [{ type: "text", text: `Step ${n} done.\n${filler(n + 20, len)}` }] }); + +const estTok = (s: string) => Math.ceil(s.length / 4); +const msgChars = (m: { content: unknown }): number => { + const c = m.content; + if (typeof c === "string") return c.length; + if (Array.isArray(c)) return c.reduce((n: number, p) => n + (typeof (p as { text?: unknown })?.text === "string" ? ((p as { text: string }).text).length : 0), 0); + return 0; +}; + +const NUDGE_MARK = "efficiency nudge"; + +test("a nudge the upstream rejected is re-armed after the next success; a delivered one is not", { timeout: 120_000 }, async () => { + const { defaultConfig } = await import("acp-kernel"); + const { startServer } = await import("../src/server.ts"); + const { SessionStore, _setStoreForTest } = await import("../src/persist.ts"); + const { _setForTest: setRegistryForTest } = await import("../src/registry.ts"); + const { rmrf } = await import("./tmp-rm.ts"); + const { setLogCapture } = await import("../src/logger.ts"); + const logLines: string[] = []; + setLogCapture((_level: string, msg: string) => { logLines.push(msg); }); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + let proxy: http.Server | undefined; + let upstream: http.Server | undefined; + const closeServer = (s: http.Server | undefined): Promise => s ? new Promise((resolve, reject) => { + s.closeAllConnections?.(); + s.close((e) => (e ? reject(e) : resolve())); + }) : Promise.resolve(); + + try { + const bodies: Record = {}; + const rejectFirstNudge = new Set(); + const nudgeAt: Record = {}; + upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const sid = String(req.headers["x-claude-code-session-id"] ?? ""); + const body = Buffer.concat(chunks).toString("utf8"); + (bodies[sid] ??= []).push(body); + if (rejectFirstNudge.has(sid) && body.includes(NUDGE_MARK)) { + rejectFirstNudge.delete(sid); + res.writeHead(400, { "content-type": "application/json", "request-id": "req_test" }); + res.end(JSON.stringify({ type: "error", error: { type: "invalid_request_error", message: "messages.5: role 'system' must precede an 'assistant' message or end the array" } })); + return; + } + const parsed = JSON.parse(body) as { messages?: Array<{ content: unknown }>; tools?: unknown }; + const inputTokens = estTok(SYSTEM) + estTok(JSON.stringify(parsed.tools ?? [])) + (parsed.messages ?? []).reduce((n, m) => n + Math.ceil(msgChars(m) / 4), 0); + const sse = (ev: string, data: object) => res.write(`event: ${ev}\ndata: ${JSON.stringify(data)}\n\n`); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + sse("message_start", { type: "message_start", message: { id: `msg-${bodies[sid]!.length}`, role: "assistant", model: MODEL, content: [], usage: { input_tokens: inputTokens, output_tokens: 0 } } }); + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }); + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } }); + sse("content_block_stop", { type: "content_block_stop", index: 0 }); + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } }); + sse("message_stop", { type: "message_stop" }); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const baseKernel = defaultConfig(LIMIT, { preserveRecentMessages: 3, preserveRecentTokens: 800 }); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [MODEL]: { context: LIMIT } } } } as ProxyOptions["routes"], + modelContextLimit: LIMIT, + kernelConfig: { ...baseKernel, nudge: { ...baseKernel.nudge, growthFloor: 4000, growthCap: 4000, minGrowthFloor: 100, minGrowthRatio: 0.1, tierGrowthTokens: { t1: 600 } } }, + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }; + proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`; + + const runSession = async (sid: string): Promise<{ statuses: number[]; nudged: boolean[] }> => { + const headers = { "content-type": "application/json", "user-agent": "claude-cli/2.1.322 (external, cli)", "x-claude-code-session-id": sid }; + const statuses: number[] = []; + const post = async (msgs: Msg[]): Promise => { + const res = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 4096, stream: true, system: SYSTEM, messages: msgs, tools: TOOLS }) }); + statuses.push(res.status); + await res.text(); + }; + // Grow one turn at a time until bili injects its nudge (request k). + let H: Msg[] = [um(1, 800)]; + let Hprev: Msg[] = H; + let k = -1; + for (let n = 1; n <= 8 && k < 0; n++) { + await post(H); + if ((bodies[sid] ?? []).at(-1)?.includes(NUDGE_MARK)) k = n - 1; + else { Hprev = H; H = [...H, am(n, 6000), um(n + 1, 700)]; } + } + assert.ok(k >= 0, `the nudge fires within 8 turns: ${JSON.stringify({ statuses, n: (bodies[sid] ?? []).map((b) => b.includes(NUDGE_MARK)), keys: Object.keys(bodies) })}`); + // The client's next request is smaller than the rejected one, then it takes a short turn. + const G: Msg[] = [...Hprev, um(50, 700)]; + await post(G); + await post([...G, am(99, 2400), um(100, 700)]); + nudgeAt[sid] = k; + return { statuses, nudged: (bodies[sid] ?? []).map((b) => b.includes(NUDGE_MARK)) }; + }; + + rejectFirstNudge.add("11111111-1111-4111-8111-111111111111"); + const rejected = await runSession("11111111-1111-4111-8111-111111111111"); + const control = await runSession("22222222-2222-4222-8222-222222222222"); + + const kr = nudgeAt["11111111-1111-4111-8111-111111111111"]!; + const kc = nudgeAt["22222222-2222-4222-8222-222222222222"]!; + assert.equal(rejected.statuses[kr], 400, "the upstream rejects the nudged request"); + assert.deepEqual(rejected.statuses.slice(kr + 1), [200, 200]); + assert.equal(rejected.nudged[kr + 1], false, "the request right after the 400 carries no nudge — the nudge may be what drew it"); + assert.equal(rejected.nudged[kr + 2], true, "after a request succeeds, the undelivered nudge is re-armed"); + assert.ok(control.statuses.every((st) => st === 200)); + assert.equal(control.nudged[kc + 2], false, "control: a delivered nudge still holds the cadence on the next turn"); + } catch (err) { + process.stderr.write(logLines.filter((l) => /nudge|upstream 4|error|502|fail/i.test(l)).join("\n") + "\n"); + throw err; + } finally { + setLogCapture(null); + await closeServer(proxy); + await closeServer(upstream); + try { rmrf(tmpRoot); } catch { /* ignore */ } + } +}); From 2a355a62bb8c26f002f15248713b3d949ff97aac Mon Sep 17 00:00:00 2001 From: tinysunspot <345811623+tinysunspot@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:56:13 +0800 Subject: [PATCH 04/34] Guard the google lane against inert-frame tail drains #5849 added hadTextOtherThanTextFields to the chat lane after a text-less frame drained a held render-tag head, letting the rest of the tag reach the host as prose. The google lane kept the unconditional drain, on the assumption that it never sees such frames -- an absence of observation, not a property of the lane. A Gemini progress frame whose only part is {text:""} reproduces the same split here, so gate that exit on googleFrameHasNonText, the predicate the rebuilt branch three lines above already uses. --- src/plugin.ts | 5 +- ...-passthrough-progress-frame-google.test.ts | 229 ++++++++++++++++++ 2 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 tests/plugin-passthrough-progress-frame-google.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index a4824a39d0..dd72be102e 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3745,7 +3745,10 @@ export async function pipePluginChatWithStrip( // structured or user data (#1039) and are excluded from the audit. if (!googleFrameHasNonText(ev)) auditRawForward(rawEvent); if (drain.length > 0) return drain + rawEvent + "\n\n"; - if (!hadText && anyPending()) return flushTails() + rawEvent + "\n\n"; + // #5848 twin on the google lane: a frame carrying only empty parts has + // nothing a held tail must precede, and draining on it split a render tag + // so the remainder reached the host as prose (the #5192 shape). + if (!hadText && anyPending() && googleFrameHasNonText(ev)) return flushTails() + rawEvent + "\n\n"; return rawEvent + "\n\n"; }; try { diff --git a/tests/plugin-passthrough-progress-frame-google.test.ts b/tests/plugin-passthrough-progress-frame-google.test.ts new file mode 100644 index 0000000000..c6984c9d9d --- /dev/null +++ b/tests/plugin-passthrough-progress-frame-google.test.ts @@ -0,0 +1,229 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { pipePluginChatWithStrip } from "../src/plugin.ts"; +import type { Session } from "../src/session.ts"; + +// #5848 shipped the inert-frame guard on the chat (OpenAI) lane only. The +// Google lane still drained the tag-echo filter on ANY text-less frame, so the +// same split-tag leak survived there: a render tag split at the frame boundary +// lost its held head and the remainder (` type="text" …>mNNNNN`) no longer +// started with `<`, so it reached the host as prose — the #5192 shape. Google +// frames that carry nothing but empty parts must not drain; a frame with a +// finishReason / functionCall / functionResponse / usageMetadata still must. + +function makeSession(): Session { + return { + id: "testsess", + protocol: "google", + 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(); + } + }, + }); +} + +const DONE = "data: [DONE]\n\n"; + +function gframe( + parts: Array>, + finishReason?: string, +): string { + const candidate: Record = { + index: 0, + content: { role: "model", parts }, + }; + if (finishReason) candidate["finishReason"] = finishReason; + return `data: ${JSON.stringify({ modelVersion: "gemini-test", candidates: [candidate] })}\n\n`; +} + +/** A progress frame in Gemini's own dialect: no usable text parts at all. */ +function gprogress(): string { + return `data: ${JSON.stringify({ modelVersion: "gemini-test", candidates: [{ index: 0, content: { role: "model", parts: [{ text: "" }] } }] })}\n\n`; +} + +function gTexts(f: Record): string[] { + const out: string[] = []; + const c = f["candidates"]; + if (!Array.isArray(c)) return out; + for (const cand of c) { + if (!cand || typeof cand !== "object") continue; + const content = (cand as Record)["content"]; + if (!content || typeof content !== "object") continue; + const parts = (content as Record)["parts"]; + if (!Array.isArray(parts)) continue; + for (const p of parts) { + if ( + p && + typeof p === "object" && + typeof (p as Record)["text"] === "string" + ) { + out.push((p as Record)["text"] as string); + } + } + } + return out; +} + +/** Concatenate every text part the client would render. */ +function visibleText(text: string): string { + let out = ""; + for (const block of text.split("\n\n")) { + const payload = block + .split("\n") + .filter((l) => l.startsWith("data:")) + .map((l) => l.slice(5).trim()) + .join(""); + if (!payload || payload === "[DONE]") continue; + let obj: Record; + try { + obj = JSON.parse(payload) as Record; + } catch { + continue; + } + for (const t of gTexts(obj)) out += t; + } + return out; +} + +async function visible( + contents: string[], + inertAfter: (i: number) => boolean, +): Promise { + const events: string[] = [gframe([{ text: "" }])]; + contents.forEach((c, i) => { + events.push(gframe([{ text: c }])); + if (inertAfter(i)) events.push(gprogress()); + }); + events.push(gframe([], "STOP"), DONE); + const chunks: string[] = []; + await pipePluginChatWithStrip( + streamOf(events), + makeRes(chunks), + "google", + makeSession(), + ); + return visibleText(chunks.join("")); +} + +const HEAD = 'm00039'; + +test("google: a text-less progress frame inside a paired render tag does not leak the tag's tail", async () => { + const out = await visible( + ["前の文章。", HEAD, REST + "", "後の文章"], + (i) => i === 1, + ); + assert.equal(out, "前の文章。後の文章"); + assert.doesNotMatch(out, /type="text"|m00039/); +}); + +test("google: a text-less progress frame inside an unclosed render tag yields the same text as without it", async () => { + const contents = [HEAD, REST + "\n", "子作成完了"]; + const without = await visible(contents, () => false); + const withProgress = await visible(contents, (i) => i === 0); + assert.equal(withProgress, without); + assert.doesNotMatch(withProgress, /type="text"|m00039/); +}); + +test("google: a text-less progress frame at every chunk boundary leaves the visible text unchanged", async () => { + const contents = [ + ..."前の文章。", + ...`m00039`, + ..."後の文章", + ]; + const without = await visible(contents, () => false); + const withProgress = await visible(contents, () => true); + assert.equal(without, "前の文章。後の文章"); + assert.equal(withProgress, without); +}); + +test("google: a frame carrying usageMetadata still drains the held tail (guard is not a blanket hold)", async () => { + const out: string[] = []; + const usage = `data: ${JSON.stringify({ + modelVersion: "gemini-test", + candidates: [{ index: 0, content: { role: "model", parts: [] } }], + usageMetadata: { + promptTokenCount: 10, + candidatesTokenCount: 3, + totalTokenCount: 13, + }, + })}\n\n`; + await pipePluginChatWithStrip( + streamOf([ + gframe([{ text: "hello world" }], "STOP"), + DONE, + ]), + makeRes(out), + "google", + makeSession(), + ); + const text = visibleText(out.join("")); + // The held ` world" }], "STOP"), + DONE, + ]), + makeRes(control), + "google", + makeSession(), + ); + assert.equal(text, visibleText(control.join(""))); +}); + +test("google: ordinary streams with no held tail are unchanged by the guard", async () => { + const out: string[] = []; + await pipePluginChatWithStrip( + streamOf([ + gframe([{ text: "plain " }]), + gprogress(), + gframe([{ text: "answer" }], "STOP"), + DONE, + ]), + makeRes(out), + "google", + makeSession(), + ); + assert.equal(visibleText(out.join("")), "plain answer"); +}); From 43836843bc3fcefae0a51a65be0fc361175c7ae5 Mon Sep 17 00:00:00 2001 From: tinysunspot <345811623+tinysunspot@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:27:07 +0800 Subject: [PATCH 05/34] Make a held head dropped at drain auditable (#5192) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The streaming tag filter reports its drops through a one-shot notice: the first drop of a response claims it and every later one is silent. A tag head still held when the stream ends is dropped inside flush(), so when a completed tag came first (the common case) the drain drop produced a counter bump and no line at all — the only trace was the strip snippet, which is why a 5-char ", ori const requestText = JSON.stringify(requestBody); const makeFilter = () => composeStreamFilters( composeStreamFilters( - createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }, (snippet) => { // #2190: residue audit — log-only, see plugin.ts twins. loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); diff --git a/src/loop/adapter-commandcode.ts b/src/loop/adapter-commandcode.ts index 81eb75a859..c18669c9ba 100644 --- a/src/loop/adapter-commandcode.ts +++ b/src/loop/adapter-commandcode.ts @@ -79,8 +79,9 @@ export function createCommandcodeAdapter( async *parseStream(upstream: ReadableStream, round: number): AsyncGenerator { void round; - const tagFilter = composeStreamFilters(createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag (commandcode proxy mode): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + const tagFilter = composeStreamFilters(createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag (commandcode proxy mode)"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }), createMarkerLineFilter()); let sawReasoning = false; let sawRealToolCall = false; diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts index 21b77f82b9..6fd7059447 100644 --- a/src/loop/adapter-google.ts +++ b/src/loop/adapter-google.ts @@ -318,8 +318,9 @@ export function createGoogleAdapter( const requestText = JSON.stringify(requestBody); const makeFilter = () => composeStreamFilters( composeStreamFilters( - createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }, (snippet) => { // #2190: residue audit — log-only, see plugin.ts twins. loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index 282910787b..dc74ce3540 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -278,8 +278,9 @@ export function createOpenaiAdapter(requestBody: Record, client const requestText = JSON.stringify(requestBody); const makeFieldFilter = () => composeStreamFilters( composeStreamFilters( - createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }, (snippet) => { // #2190: residue audit — log-only, see plugin.ts twins. loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); diff --git a/src/loop/adapter-responses.ts b/src/loop/adapter-responses.ts index c764c4791f..a0decf5065 100644 --- a/src/loop/adapter-responses.ts +++ b/src/loop/adapter-responses.ts @@ -402,8 +402,9 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp const absorbArmedLocal = absorbName !== undefined; const makeFilter = () => composeStreamFilters( composeStreamFilters( - createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }, (snippet) => { // #2190: residue audit — log-only, see plugin.ts twins. loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); diff --git a/src/loop/tag-echo-filter.ts b/src/loop/tag-echo-filter.ts index fb4f1d33f0..bd59b845eb 100644 --- a/src/loop/tag-echo-filter.ts +++ b/src/loop/tag-echo-filter.ts @@ -1007,7 +1007,7 @@ export function createBiliArtifactFilter(onDrop?: (snippet: string) => void): Ta }; } -export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidueWarn?: (snippet: string) => void, absorbInstructed?: boolean, requestText?: string): TagEchoFilter { +export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boolean) => void, onResidueWarn?: (snippet: string) => void, absorbInstructed?: boolean, requestText?: string): TagEchoFilter { let held = ""; /** Settled prose deferred alongside a NON-definite tag-head tail: it * carries a dead ref-close unit whose verdict is content-determined @@ -1055,17 +1055,29 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu // the cap drops repeatedly until its close. swallowSessionCounted keeps // any one swallow session at exactly one count. let swallowSessionCounted = false; - const recordDrop = (snippet: string) => { + // #5192: a drain drop is a different event from an ordinary strip — the + // stream ended while the filter still held bytes, so the one-shot notice + // below (spent by the response's FIRST drop) left it with no line at all. + // It gets its own one-shot budget so a held head at EOF is always audible. + let drainNoticed = false; + const recordDrop = (snippet: string, duringDrain = false) => { droppedAny = true; + if (duringDrain) { + if (onDrop && !drainNoticed) { + drainNoticed = true; + onDrop(snippet, true); + } + return; + } if (onDrop && !notified) { notified = true; onDrop(snippet); } }; /** One self-contained artifact resolved by a single drop call. */ - const drop = (snippet: string) => { + const drop = (snippet: string, duringDrain = false) => { dropCount += 1; - recordDrop(snippet); + recordDrop(snippet, duringDrain); }; /** Any drop inside a live swallow session — the session counts once. */ const dropSwallowSpan = (snippet: string) => { @@ -1596,12 +1608,12 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu } else { const t = new RegExp(TRUNC_OPEN.source).exec(rest); if (t) { - drop(t[0]); + drop(t[0], true); result = rest.slice(0, t.index); } else { const tc = new RegExp(TRUNC_CLOSE.source).exec(rest); if (tc) { - drop(tc[0]); + drop(tc[0], true); result = rest.slice(0, tc.index); } else { result = rest; diff --git a/src/plugin.ts b/src/plugin.ts index a4824a39d0..415b39bdc6 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -2757,10 +2757,11 @@ export async function pipePluginChatWithStrip( // write through the request logger landed in the SAME tee file, doubling // every count taken from bili.log. const who = session ? `[${session.id}] ` : ""; - const onTagDrop = (snippet: string) => { + const onTagDrop = (snippet: string, duringDrain?: boolean) => { droppedTagInFrame = true; sawStrippedEcho = true; - loggerLog("warn", `${who}[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag (plugin passthrough)"; + loggerLog("warn", `${who}[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }; // #2190: the filter released bytes that still carry echo-residue shape — // the leak is now observable instead of silent. Log-only; the bytes were @@ -3972,9 +3973,11 @@ export async function pipePluginJsonlWithStrip( // delta as a whole line, so the filters' held tails are only resolved at // the terminal frame / EOF / read failure — never per line (a per-line // flush destroys the cross-delta tag/marker state this filter exists for). - const onStrip = (snippet: string) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag (commandcode plugin mode): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); - log?.(`[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + const onStrip = (snippet: string, duringDrain?: boolean) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag (commandcode plugin mode)"; + const head = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + log?.(`[tag-echo] ${head}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }; const textFilter = composeStreamFilters(createTagEchoFilter(onStrip), createMarkerLineFilter()); const reasonFilter = composeStreamFilters(createTagEchoFilter(onStrip), createMarkerLineFilter()); @@ -4272,8 +4275,9 @@ export async function pipePluginResponsesWithStrip( if (diagLastTypes.length > 5) diagLastTypes.shift(); }; const who = session ? `[${session.id}] ` : ""; - const onTagDrop = (snippet: string) => { - loggerLog("warn", `${who}[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + const onTagDrop = (snippet: string, duringDrain?: boolean) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag (plugin passthrough)"; + loggerLog("warn", `${who}[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }; // #2190: log-only residue audit — see the twin above. const onResidueWarn = (snippet: string) => { diff --git a/tests/issue5192-drain-head-audit.test.ts b/tests/issue5192-drain-head-audit.test.ts new file mode 100644 index 0000000000..67d81efe80 --- /dev/null +++ b/tests/issue5192-drain-head-audit.test.ts @@ -0,0 +1,168 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createTagEchoFilter } from "../src/loop/tag-echo-filter.ts"; +import { pipePluginChatWithStrip } from "../src/plugin.ts"; +import { setLogCapture } from "../src/logger.ts"; +import type { Session } from "../src/session.ts"; + +const LT = "\x3c"; +const OPEN = `${LT}acp `; +const CLOSE = `${LT}/acp>`; + +interface Drop { + snippet: string; + duringDrain: boolean; +} + +/** Records what the filter reports, keeping the drain flag verbatim so a + * missing second argument is observable rather than silently falsy. */ +function recorder(): { drops: Drop[]; onDrop: (snippet: string, duringDrain?: boolean) => void } { + const drops: Drop[] = []; + return { + drops, + onDrop: (snippet: string, duringDrain?: boolean) => { + drops.push({ snippet, duringDrain: duringDrain === true }); + }, + }; +} + +function tag(ref: string, tokens = 9): string { + return `${OPEN}tokens="${tokens}" type="text">${ref}${CLOSE}`; +} + +const HEAD = `${LT}acp tokens="12" type="text" time="2026-11-13 17:51:02"`; + +test("#5192: a tag head discarded by flush() is reported as a drain drop", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前の文章。${HEAD}`), "前の文章。"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 1); + assert.equal(drops[0]!.snippet, HEAD); + assert.equal(drops[0]!.duringDrain, true); +}); + +test("#5192: a completed tag strip is not reported as a drain drop", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前の文章。${tag("m00011")}後`), "前の文章。後"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 1); + assert.equal(drops[0]!.duringDrain, false); +}); + +test("#5192: a drain drop is reported even after an earlier strip claimed the one-shot notice", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前の文章。${tag("m00011")}中${HEAD}`), "前の文章。中"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 2, "the drain drop must not be swallowed by the one-shot notice"); + assert.equal(drops[1]!.snippet, HEAD); + assert.equal(drops[1]!.duringDrain, true); +}); + +test("#5192: a truncated close tag discarded by flush() is also a drain drop", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前の文章。${LT}/acp`), "前の文章。"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 1); + assert.equal(drops[0]!.snippet, `${LT}/acp`); + assert.equal(drops[0]!.duringDrain, true); +}); + +test("#5192: the one-shot notice for ordinary strips is unchanged", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前${tag("m00011", 1)}中${tag("m00012", 2)}後`), "前中後"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 1, "only the first ordinary strip is announced"); + assert.equal(drops[0]!.duringDrain, false); +}); + +test("#5192: a drain drop carries no closing bracket, so it is never mistakable for a full tag", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + f.push(`前の文章。${HEAD}`); + f.flush(); + assert.equal(drops.length, 1); + assert.equal(drops[0]!.snippet.includes(">"), false); +}); + +function makeSession(): Session { + return { + id: "drainsess", + 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 { + 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`; +} + +test("#5192: the passthrough logs a held head dropped at stream end as a drain drop", async () => { + const logs: string[] = []; + setLogCapture((_level, msg) => { logs.push(msg); }); + try { + const out: string[] = []; + // The completed tag claims the one-shot strip notice; the head held at + // EOF is the second drop of the response and used to leave no line. + const events: string[] = [ + chatChunk({ role: "assistant" }), + chatChunk({ content: `前の文章。${tag("m00042")}中` }), + chatChunk({ content: `後${HEAD}` }), + "data: [DONE]\n\n", + ]; + await pipePluginChatWithStrip(streamOf(events), makeRes(out), "openai", makeSession()); + + const tagLines = logs.filter((l) => l.includes("[tag-echo]")); + const stripLines = tagLines.filter((l) => l.includes("stripped model-emitted render tag")); + const drainLines = tagLines.filter((l) => l.includes("held head dropped during drain")); + assert.equal(stripLines.length, 1, `expected one strip line, got: ${tagLines.join(" | ")}`); + assert.equal(drainLines.length, 1, `expected one drain line, got: ${tagLines.join(" | ")}`); + assert.ok(drainLines[0]!.includes("2026-11-13 17:51:02"), "the drained head itself is named in the line"); + const text = out.join(""); + assert.ok(!text.includes("m00042"), "the completed tag never reaches the client"); + assert.ok(text.includes("前の文章。") && text.includes("中") && text.includes("後"), "prose around the tag survives"); + } finally { + setLogCapture(null); + } +}); From 9c6e9b3650d1d8d0070625605cad965868aac0d9 Mon Sep 17 00:00:00 2001 From: ci-check Date: Fri, 13 Nov 2026 21:10:37 +0800 Subject: [PATCH 06/34] fix: route relay output-budget helpers via budget.js to keep the side-lane boundary relay.ts (the wire tail) must not import the side-lane engine directly (#1440 P2 cut 4; tests/side-lane-invariants.test.ts test H). The overflow recovery (#6528) added an import of outputBudgetField/readOutputBudget/ writeOutputBudget from ./side-request.js, coupling relay to the side-lane engine and breaking test H on 3 CI shards (master is green on the same shards). The three helpers are generic wire-shape helpers already imported by src/server/budget.ts; re-export them from budget.ts and point relay.ts at ./budget.js. No behavior change, no circular dependency. --- src/server/budget.ts | 4 ++-- src/server/relay.ts | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/server/budget.ts b/src/server/budget.ts index 014aaa23d2..fe1fa22c22 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -4,9 +4,9 @@ import { estimateCoreMessages } from "../preflight.js"; import { SELF_HEAL_SUPPRESS_CAP_MS, reductionKeyOf } from "../session-self-heal.js"; import type { Session } from "../session.js"; import { applyEstimateCalibration } from "../util.js"; -import { readOutputBudget, writeOutputBudget, type OutputBudgetField } from "./side-request.js"; +import { outputBudgetField, readOutputBudget, writeOutputBudget, type OutputBudgetField } from "./side-request.js"; -export { readOutputBudget }; +export { outputBudgetField, readOutputBudget, writeOutputBudget }; // #453 hard backstop: cap the forwarded output budget so input+output can never // exceed the window on request-rebuilding upstreams (vLLM rejects an oversized diff --git a/src/server/relay.ts b/src/server/relay.ts index d9db156374..efea220851 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -70,11 +70,10 @@ import { DUMP_PLAINTEXT_BANNER } from "../dump-banner.js"; import { recordPckAblation } from "../pck-probe.js"; import { detectThinkingSignatureRejection, rememberStrictThinkingPosture, stripUnsignedThinkingFromWire, thinkingPostureIsStrict } from "./thinking-posture.js"; import { applyOutputSteering, applyOutputSteeringJson } from "../output-steering.js"; -import { estimateWireOverhead } from "./budget.js"; +import { estimateWireOverhead, outputBudgetField, readOutputBudget, writeOutputBudget } from "./budget.js"; import { buildForwardHeaders, connectionNamedHeaders, RESPONSE_ONLY_STRIP_HEADERS, safeSessionId, UPSTREAM_HOP_HEADERS } from "./headers.js"; import { bodyDumpEnabled, logDumpFailure } from "./observability.js"; import { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-responses.js"; -import { outputBudgetField, readOutputBudget, writeOutputBudget } from "./side-request.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"; From 03c69150ffea0cadce065db1abb9dacf21522f4e Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Fri, 13 Nov 2026 14:04:51 +0000 Subject: [PATCH 07/34] test(loop): pin faithful forwarding of model-emitted calls after acp_status continuation A mock upstream replays the #5950 turn shape: round 1 carries reasoning plus the acp_status call, round 2 answers in text or emits the Read for the nonexistent path. Both directions asserted, per the gate discipline (#3617): a text-only round 2 reaches the client with no tool_call frame (the proxy fabricates nothing), and a model-emitted Read reaches the client byte-exact, once, with its id and arguments verbatim (the proxy suppresses and rewrites nothing). The round-2 request body is asserted too: the acp_status call/result pair replays with matching ids and the round-1 reasoning intact. Related to #5950. This test documents and locks in the correct behavior; it does not fix the model-side hallucination behind the report. --- .../issue5950-phantom-read-forwarding.test.ts | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 tests/issue5950-phantom-read-forwarding.test.ts diff --git a/tests/issue5950-phantom-read-forwarding.test.ts b/tests/issue5950-phantom-read-forwarding.test.ts new file mode 100644 index 0000000000..b62e03b773 --- /dev/null +++ b/tests/issue5950-phantom-read-forwarding.test.ts @@ -0,0 +1,142 @@ +// #5950: pins the faithful-forwarding contract for the incident shape — +// a server-side acp_status continuation followed by a model-emitted call. +// A mock upstream replays the turn: round 1 carries reasoning plus the +// acp_status call, round 2 either answers in text or emits the Read call +// for the nonexistent path from the report. Both directions: +// (a) the proxy never fabricates a tool call — a text-only round 2 +// reaches the client with no tool_call frame at all; +// (b) the proxy never suppresses or rewrites one — a model-emitted +// Read reaches the client byte-exact (same id, name, arguments). +// The round-2 request body is asserted in both cases: the continuation +// replays the acp_status call/result pair with matching ids and the +// round-1 reasoning intact. Related to #5950; this test documents and +// locks in the correct behavior, it does not fix the model-side +// hallucination behind the report. +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 type { Session } from "../src/session.ts"; +import { createOpenaiAdapter, runCompressLoop } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +const READ_PATH = "/Users/eee/.zcode/workspace/default/86f5c4b2cf708f31ec94f98b78e5f47c/test_user.py"; +const READ_ARGS = JSON.stringify({ path: READ_PATH }); + +function makeCtx(id: string) { + 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: "openai" as const, + }; +} + +function openaiSse(frames: Array>): string { + return frames.map((f) => `data: ${JSON.stringify(f)}\n\n`).join("") + "data: [DONE]\n\n"; +} + +// Round 1 of the incident turn: the model reasons, then calls acp_status +// (proxy-owned, executed server-side). The call carries a normal upstream id. +const ROUND1 = openaiSse([ + { id: "chatcmpl_1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_1", choices: [{ index: 0, delta: { reasoning_content: "present this concisely in Chinese" }, finish_reason: null }] }, + { id: "chatcmpl_1", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_5950_r1", type: "function", function: { name: "acp_status", arguments: "{}" } }] }, finish_reason: null }] }, + { id: "chatcmpl_1", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }, +]); + +// Round 2, variant A: the model answers in text only. +const ROUND2_TEXT = openaiSse([ + { id: "chatcmpl_2", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: { content: "status summarized" }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, +]); + +// Round 2, variant B: the model itself emits the phantom Read for the +// nonexistent path from #5950. +const ROUND2_READ = openaiSse([ + { id: "chatcmpl_2", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_read_1", type: "function", function: { name: "Read", arguments: READ_ARGS } }] }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }, +]); + +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); + 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 }; +} + +// The continuation request the proxy sends for round 2: history plus the +// round-1 acp_status call/result pair (matching ids, upstream id kept +// byte-exact) and the round-1 reasoning replayed intact. +function assertContinuationBody(rawBody: string): void { + const body = JSON.parse(rawBody) as { messages: Array> }; + const asst = body.messages.find((m) => m.role === "assistant" && Array.isArray(m.tool_calls)); + assert.ok(asst, `round-2 body must replay the acp_status call, got: ${rawBody.slice(0, 500)}`); + const tc = (asst!.tool_calls as Array>)[0]!; + assert.equal((tc.function as Record).name, "acp_status"); + assert.equal(tc.id, "call_5950_r1", "the upstream call id is kept byte-exact in the continuation"); + const toolMsg = body.messages.find((m) => m.role === "tool"); + assert.ok(toolMsg, "round-2 body must carry the acp_status tool result"); + assert.equal(toolMsg!.tool_call_id, "call_5950_r1", "tool result pairs with the assistant call on the same id"); + assert.ok(rawBody.includes("present this concisely in Chinese"), "round-1 reasoning is replayed into the continuation"); +} + +test("#5950: text-only round 2 — the proxy fabricates no tool call", async () => { + const { out, fetchCalls, bodies } = await drainOpenai(ROUND1, [ROUND2_TEXT], "s5950-f1"); + assert.equal(fetchCalls, 1, "the proxy call continues server-side exactly once"); + assert.equal(bodies.length, 1, "one re-request body observed"); + assertContinuationBody(bodies[0]!); + assert.ok(out.includes("[ACP] acp_status result:"), "the acp_status marker reaches the client"); + assert.ok(out.includes("status summarized"), "the round-2 text reaches the client"); + assert.ok(!out.includes("tool_calls"), `no tool_call frame may appear when the model answered in text, got: ${out.slice(0, 500)}`); + assert.ok(!out.includes("test_user.py"), "no phantom path may appear when the model answered in text"); +}); + +test("#5950: model-emitted Read in round 2 — the proxy forwards it byte-exact", async () => { + const { out, fetchCalls, bodies } = await drainOpenai(ROUND1, [ROUND2_READ], "s5950-f2"); + assert.equal(fetchCalls, 1, "the proxy call continues server-side exactly once"); + assert.equal(bodies.length, 1, "one re-request body observed"); + assertContinuationBody(bodies[0]!); + assert.ok(out.includes("[ACP] acp_status result:"), "the acp_status marker reaches the client"); + assert.ok(out.includes(READ_PATH), "the model-emitted Read path reaches the client verbatim"); + assert.ok(out.includes("call_read_1"), "the model-emitted call id reaches the client verbatim"); + assert.ok(out.includes(JSON.stringify(READ_ARGS)), "the model-emitted call arguments reach the client verbatim (as the wire-escaped arguments string)"); + assert.equal((out.match(/test_user\.py/g) || []).length, 1, "the Read frame is forwarded once, never duplicated or rewritten"); +}); From 296e2c0fccc3ba922813c3baf7dfb070e36a7cbb Mon Sep 17 00:00:00 2001 From: ci-check Date: Fri, 13 Nov 2026 23:26:06 +0800 Subject: [PATCH 08/34] fix: extend drain-drop auditability to swallow-span EOF drops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The original PR flagged only the TRUNC_OPEN/TRUNC_CLOSE settle-branch sites (a truncated tag head held at EOF). The swallow-span EOF drops (stream dies mid-tag AFTER a complete opening tag — the more common died-mid-tag shape) still went through dropSwallowSpan with duringDrain=false and produced no drain line. Extend the duringDrain flag to dropSwallowSpan and pass true at all 7 swallow-span EOF call sites (lines 1537, 1546, 1551, 1554, 1579, 1585, 1594). Add 2 regression tests for the swallow-span drain case. Per 连贯修复不拆片: one mechanism (drain-drop auditability) = one PR. --- src/loop/tag-echo-filter.ts | 18 ++++++++-------- tests/issue5192-drain-head-audit.test.ts | 26 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/loop/tag-echo-filter.ts b/src/loop/tag-echo-filter.ts index bd59b845eb..050df79fc3 100644 --- a/src/loop/tag-echo-filter.ts +++ b/src/loop/tag-echo-filter.ts @@ -1080,12 +1080,12 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo recordDrop(snippet, duringDrain); }; /** Any drop inside a live swallow session — the session counts once. */ - const dropSwallowSpan = (snippet: string) => { + const dropSwallowSpan = (snippet: string, duringDrain = false) => { if (!swallowSessionCounted) { dropCount += 1; swallowSessionCounted = true; } - recordDrop(snippet); + recordDrop(snippet, duringDrain); }; /** #2862: strip dead ref-close units (ref + unterminated close head) from * a span about to be RELEASED as prose; each unit is one self-contained @@ -1534,7 +1534,7 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo // imitation (never released, #1720), or an attrs-bearing // opening whose tail never closed — that tail is tag content // (a ref, possibly truncated), not prose (#644 EOF rule). - if (rest.length > 0) dropSwallowSpan(rest); + if (rest.length > 0) dropSwallowSpan(rest, true); result = ""; } else if (wasSwallowing) { // A BARE opening (#1881): prose may genuinely wear one, so an @@ -1543,15 +1543,15 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo // truncated open/close tail is dead markup. const t = new RegExp(TRUNC_OPEN.source).exec(rest); if (t) { - dropSwallowSpan(t[0]); + dropSwallowSpan(t[0], true); result = rest.slice(0, t.index); } else { const tc = new RegExp(TRUNC_CLOSE.source).exec(rest); if (tc) { - dropSwallowSpan(tc[0]); + dropSwallowSpan(tc[0], true); result = rest.slice(0, tc.index); } else if (REFS_ONLY.test(rest)) { - dropSwallowSpan(rest); + dropSwallowSpan(rest, true); result = ""; } else { // Payload accumulated under the session may carry @@ -1576,13 +1576,13 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo if (payload === "") break; const t2 = new RegExp(TRUNC_OPEN.source).exec(payload); if (t2 !== null) { - dropSwallowSpan(t2[0]); + dropSwallowSpan(t2[0], true); r += payload.slice(0, t2.index); break; } const tc2 = new RegExp(TRUNC_CLOSE.source).exec(payload); if (tc2 !== null) { - dropSwallowSpan(tc2[0]); + dropSwallowSpan(tc2[0], true); r += payload.slice(0, tc2.index); break; } @@ -1591,7 +1591,7 @@ export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boo // ahead of a refs tail was emitted, the tail is a // prose-cited ref and must be released (#1760). if (r === "" && REFS_ONLY.test(payload)) { - dropSwallowSpan(payload); + dropSwallowSpan(payload, true); break; } if (payload === prev || guard >= 64) { diff --git a/tests/issue5192-drain-head-audit.test.ts b/tests/issue5192-drain-head-audit.test.ts index 67d81efe80..36a2ea9f77 100644 --- a/tests/issue5192-drain-head-audit.test.ts +++ b/tests/issue5192-drain-head-audit.test.ts @@ -89,6 +89,32 @@ test("#5192: a drain drop carries no closing bracket, so it is never mistakable assert.equal(drops[0]!.snippet.includes(">"), false); }); +test("#5192: a swallowed tag tail discarded at EOF is reported as a drain drop", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + // A complete opening tag followed by content, but no close: the stream + // died mid-tag. The opening tag is an ordinary strip; the swallowed tail + // held at EOF is a drain drop (previously silent). + assert.equal(f.push(`prose ${LT}acp tokens="12" type="text">m00582`), "prose "); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 2, "the drain drop must not be swallowed by the one-shot notice"); + assert.equal(drops[0]!.duringDrain, false, "the opening tag strip is an ordinary drop"); + assert.equal(drops[1]!.snippet, "m00582"); + assert.equal(drops[1]!.duringDrain, true, "the swallowed tail is a drain drop"); +}); + +test("#5192: a swallowed tag tail drain drop is reported even after an earlier strip claimed the one-shot notice", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + // An earlier completed tag claims the one-shot strip notice; the swallowed + // tail of a later unclosed tag is a drain drop (previously silent). + assert.equal(f.push(`前${tag("m00011")}中${LT}acp tokens="12" type="text">m00582`), "前中"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 2, "the drain drop must not be swallowed by the one-shot notice"); + assert.equal(drops[1]!.snippet, "m00582"); + assert.equal(drops[1]!.duringDrain, true, "the swallowed tail is a drain drop"); +}); + function makeSession(): Session { return { id: "drainsess", From 8a082c24c617b3c4ee5def93e8043a822b5104fe Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Fri, 13 Nov 2026 16:03:01 +0000 Subject: [PATCH 09/34] test: unpin legacy envelope suites from the storage baseline (#6547) --- tests/notice-registry.test.ts | 13 +++++++++++++ tests/persist-metadata-fold-gate.test.ts | 16 ++++++++++++++++ tests/persist-zstd.test.ts | 11 +++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/tests/notice-registry.test.ts b/tests/notice-registry.test.ts index 6d3b60edb4..978d89f39c 100644 --- a/tests/notice-registry.test.ts +++ b/tests/notice-registry.test.ts @@ -17,6 +17,19 @@ const cfgPath = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "notice-reg-cfg- 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; +// #6547: the global baseline pins BILI_STORAGE_UNIFIED=legacy, but this suite +// asserts on the NEW default posture (the sqlite-default notice is active +// only when no storage mode is pinned) — clear the inherited storage knobs, +// same discipline as tests/sqlite-default-notice.test.ts. +for (const k of [ + "BILI_STORAGE_UNIFIED", + "BILI_STORAGE_JSON_WRITER", + "BILI_STORAGE_BACKEND", + "BILI_ENCRYPTION_KEY", + "BILI_PERSIST", +]) { + delete process.env[k]; +} 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"); diff --git a/tests/persist-metadata-fold-gate.test.ts b/tests/persist-metadata-fold-gate.test.ts index bf1a8301d6..ad9c32046d 100644 --- a/tests/persist-metadata-fold-gate.test.ts +++ b/tests/persist-metadata-fold-gate.test.ts @@ -48,10 +48,18 @@ function makeSession(id: string): Session { const foldGate = (store: SessionStore): { dropResidentMetadata(session: Session): void } => store as unknown as { dropResidentMetadata(session: Session): void }; +// #6547: full mode opens index.db (+shm/wal) in dir; Windows refuses to +// delete open files, so close every store a test created before rmrf. +function closeAll(stores: SessionStore[]): void { + for (const s of stores) s.closeQuietly(); +} + test("metadata fold is refused while live metadata diverges from the last committed bytes (#5506)", async () => { const dir = mkdtempSync(join(tmpdir(), "bili-fpgate-neg-")); + const stores: SessionStore[] = []; try { const store = new SessionStore({ dir, debounceMs: 60000, enabled: true }); + stores.push(store); const s = makeSession("fpgate-negative"); s.metadata.anonymousPrefixAffinity = { depth: 22, tailHash: "test-tail", via: "prefix" }; await store.writeNow(s); @@ -63,14 +71,17 @@ test("metadata fold is refused while live metadata diverges from the last commit assert.equal(isDroppedMetadata(s.metadata), false, "fold must be refused while live metadata diverges from committed bytes"); assert.equal((s.metadata as Record).lateStamp, "unsaved", "the unsaved mutation must stay resident"); } finally { + closeAll(stores); rmrf(dir); } }); test("metadata fold proceeds once the divergent mutation has been committed (#5506 positive direction)", async () => { const dir = mkdtempSync(join(tmpdir(), "bili-fpgate-pos-")); + const stores: SessionStore[] = []; try { const store = new SessionStore({ dir, debounceMs: 60000, enabled: true }); + stores.push(store); const s = makeSession("fpgate-positive"); s.metadata.anonymousPrefixAffinity = { depth: 22, tailHash: "test-tail", via: "prefix" }; await store.writeNow(s); @@ -81,25 +92,30 @@ test("metadata fold proceeds once the divergent mutation has been committed (#55 await store.writeNow(s); assert.equal(isDroppedMetadata(s.metadata), true, "fold must proceed once live == committed"); const reloaded = new SessionStore({ dir, debounceMs: 60000, enabled: true }); + stores.push(reloaded); const back = reloaded.loadSync("fpgate-positive"); assert.ok(back, "session reloaded"); assert.equal((back?.metadata as Record).lateStamp, "unsaved", "committed mutation survives the round trip"); await reloaded.flushAll([]); } finally { + closeAll(stores); rmrf(dir); } }); test("metadata fold stays closed for a session with no committed save yet (#5506 conservative default)", async () => { const dir = mkdtempSync(join(tmpdir(), "bili-fpgate-cold-")); + const stores: SessionStore[] = []; try { const store = new SessionStore({ dir, debounceMs: 60000, enabled: true }); + stores.push(store); const s = makeSession("fpgate-cold"); (s.metadata as Record).onlyInMemory = true; foldGate(store).dropResidentMetadata(s); assert.equal(isDroppedMetadata(s.metadata), false, "no commit proof yet ⇒ fold must stay closed"); assert.equal((s.metadata as Record).onlyInMemory, true); } finally { + closeAll(stores); rmrf(dir); } }); diff --git a/tests/persist-zstd.test.ts b/tests/persist-zstd.test.ts index 93c668f7d7..d3c5af1f8e 100644 --- a/tests/persist-zstd.test.ts +++ b/tests/persist-zstd.test.ts @@ -48,16 +48,21 @@ function makeSession(id: string, fill = 0, fillRandom = false): Session { interface Harness { dir: string; logs: LogLine[]; + stores: SessionStore[]; } async function withTempDir(name: string, fn: (h: Harness) => Promise): Promise { await test(name, async () => { const dir = mkdtempSync(join(tmpdir(), "bili-zstd-")); const logs: LogLine[] = []; - const h = { dir, logs }; + const h: Harness = { dir, logs, stores: [] }; try { await fn(h); } finally { + // #6547: full mode opens index.db (+shm/wal) in dir; Windows + // refuses to delete open files, so close every store this + // harness created before rmrf. + for (const s of h.stores) s.closeQuietly(); delete process.env.BILI_ENCRYPTION_KEY; delete process.env.BILI_PERSIST_ZSTD; _setZstdAvailableForTest(null); @@ -67,12 +72,14 @@ async function withTempDir(name: string, fn: (h: Harness) => Promise): Pro } function newStore(h: Harness, debounceMs = 0): SessionStore { - return new SessionStore({ + const store = new SessionStore({ dir: h.dir, debounceMs, enabled: true, log: (level, msg) => h.logs.push({ level, msg }), }); + h.stores.push(store); + return store; } function filePath(dir: string, id: string): string { From 1d2c8d2be74e54d2f159a72788f5fa13f4a124ed Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Fri, 13 Nov 2026 21:33:22 +0500 Subject: [PATCH 10/34] fix: forward single-shot Responses server-tool calls instead of 400 (#6548) A single-shot Responses call with string input and only server-executed search tools (pi-web-access xAI web search) is not a conversation turn: it carries no history to fingerprint and creates no session state, so the #286 refusal's rationale does not apply to it, and the client cannot carry an identity here. Forward it verbatim instead of hard-400ing. --- src/server/handle.ts | 15 +++++++- src/server/side-request.ts | 54 ++++++++++++++++++++++++++++ tests/e2e-session-identity.test.ts | 49 +++++++++++++++++++++++++ tests/side-lane-invariants.test.ts | 8 +++-- tests/websearch-side-request.test.ts | 45 ++++++++++++++++++++++- 5 files changed, 167 insertions(+), 4 deletions(-) diff --git a/src/server/handle.ts b/src/server/handle.ts index 0166ebde6b..7eb7a16e6d 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, isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, 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"; @@ -1610,6 +1610,19 @@ async function handleRequest( if (!clientProvided) { anonAffinity = prefixAffinity.resolve(affinityMessageList()); if (!anonAffinity) { + // #6548: a single-shot server-tool-only Responses call (the + // pi-web-access xAI web search: string input, web_search / + // x_search tools, no history) is not a conversation turn — it + // carries no history to fingerprint and creates no session + // state, so the #286 refusal's rationale does not apply to + // it, and the client cannot carry an identity here. Forward + // it verbatim (same raw relay as the #1117 passthrough) so + // the search backend keeps working instead of hard-400ing. + if (protocol === "responses" && isResponsesServerToolSingleShot(parsed)) { + log("info", `single-shot server-tool Responses call — no conversation identity possible; forwarding verbatim (#6548): ${maskUrlForLog(req.url ?? "")}`); + await forward(req, res, opts, scrubCompatDrop(scrubAnthropicPck(protocol, bodyBuffer, log), compatDropPaths, log), null, core, config, log, route, instanceId, undefined, undefined, undefined, googleModel ?? vertexModel); + return; + } log("warn", `400: no stable conversation identity on ${protocol} request → ${maskUrlsInText(upstreamOrigin)}; refusing to create a content-fingerprint session (#286)`); res.writeHead(400, { "content-type": "application/json" }); res.end(JSON.stringify(protocol === "anthropic" diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 811d53c6c4..c94809f26a 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -72,6 +72,60 @@ export function isServerToolUtilityCall(parsed: unknown): boolean { return false; } +// #6548: the Responses-wire analogue of isServerToolUtilityCall. pi-web-access +// sends its xAI web-search backend as a deliberately minimal single-shot +// Responses body — `{ model, input, tools: [{type:"web_search"}, +// {type:"x_search"}] }` — string input, no message history, no +// prompt_cache_key, and it exposes no config knob for extra headers or a +// session id, so the client cannot carry an identity here. That shape is NOT +// a conversation turn: it has no history to fingerprint and creates no +// session state, so the #286 refusal's rationale (content-fingerprint +// sessions would silently orphan state) does not apply to it — the refusal +// branch forwards it verbatim instead of hard-400ing. +// Decisive conditions (all must hold): +// - `input` is a plain string (the single-shot prompt); an array input is +// message history, and a missing input is degenerate; +// - non-empty tools where EVERY entry's type is a Responses server-executed +// search type (`web_search`, `web_search_preview`, `x_search`) — mixing +// ANY client-side tool (`function`, `mcp`, ...) vetoes; +// - no identity/history signal in the body: `previous_response_id`, +// `session_id` or `prompt_cache_key` present means the call chains onto +// a conversation (or carries an identity the #286 path already honors); +// - tool_choice absent, "auto" or "required", or an object forcing a +// DECLARED server tool; "none" vetoes (a request that forbids tool use +// is not a tool call), as does forcing a client tool or an unrecognized +// shape. +// Deliberately proto-safe by shape alone: the Anthropic detector above +// requires `messages`, so a Responses body can never match it — and this one +// requires string `input`, so an Anthropic body can never match it either. +const RESPONSES_SERVER_SEARCH_TOOL_TYPES: ReadonlySet = new Set(["web_search", "web_search_preview", "x_search"]); + +export function isResponsesServerToolSingleShot(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const p = parsed as Record; + if (typeof p.input !== "string") return false; + if (typeof p.previous_response_id === "string" && p.previous_response_id !== "") return false; + if (typeof p.session_id === "string" && p.session_id !== "") return false; + if (typeof p.prompt_cache_key === "string" && p.prompt_cache_key !== "") return false; + const tools = p.tools; + if (!Array.isArray(tools) || tools.length === 0) return false; + const declared = new Set(); + for (const t of tools) { + if (t === null || typeof t !== "object") return false; + const type = (t as { type?: unknown }).type; + if (typeof type !== "string" || !RESPONSES_SERVER_SEARCH_TOOL_TYPES.has(type)) return false; + declared.add(type); + } + const choice = p.tool_choice; + if (choice === undefined) return true; + if (choice === "auto" || choice === "required") return true; + if (choice !== null && typeof choice === "object") { + const c = choice as { type?: unknown }; + if (typeof c.type === "string" && declared.has(c.type)) return true; + } + return false; +} + 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; diff --git a/tests/e2e-session-identity.test.ts b/tests/e2e-session-identity.test.ts index 289709ff7d..b10ccbb674 100644 --- a/tests/e2e-session-identity.test.ts +++ b/tests/e2e-session-identity.test.ts @@ -375,6 +375,55 @@ test("e2e session identity: anonymous degenerate request (no history signal) → } }); +test("e2e session identity: anonymous single-shot Responses server-tool call forwards verbatim, no session (#6548)", async () => { + const h = await startHarness(); + try { + // pi-web-access's xAI web-search backend: string input, web_search / + // x_search tools only, no history, and no knob for a session id — the + // client cannot carry an identity here. Pre-fix the #286 refusal + // hard-400d it; post-fix it rides the verbatim forward like any + // utility call and creates no session state. + const body = { model: "gpt-test", stream: true, input: "latest news on context compression", tools: [{ type: "web_search" }, { type: "x_search" }] }; + const resp = await postRaw(h, 0, "/v1/responses", body, { authorization: "Bearer " }); + assert.equal(resp.status, 200, `proxy returned ${resp.status}: ${await resp.text()}`); + assert.equal((await getSessions(h)).length, 0, "verbatim forward must not create a session"); + } finally { + await h.close(); + } +}); + +test("e2e session identity: anonymous Responses single-shot shape with a client tool still 400s (#6548 negative)", async () => { + const h = await startHarness(); + try { + // A function tool makes it an agent turn, not a server-tool utility + // call — the #286 refusal still applies. + const body = { model: "gpt-test", stream: true, input: "what is the weather", tools: [{ type: "function", name: "get_weather", description: "weather", parameters: { type: "object", properties: {} } }] }; + const resp = await postRaw(h, 0, "/v1/responses", body, { authorization: "Bearer " }); + assert.equal(resp.status, 400); + const json = (await resp.json()) as { error?: { message?: string } }; + assert.match(json.error?.message ?? "", /Missing stable conversation identity/); + assert.equal((await getSessions(h)).length, 0); + } finally { + await h.close(); + } +}); + +test("e2e session identity: anonymous Responses search call chaining previous_response_id still 400s (#6548 negative)", async () => { + const h = await startHarness(); + try { + // previous_response_id chains the call onto a prior response — a + // conversation continuation, not a single shot. + const body = { model: "gpt-test", stream: true, input: "follow-up search", tools: [{ type: "web_search" }], previous_response_id: "resp_abc" }; + const resp = await postRaw(h, 0, "/v1/responses", body, { authorization: "Bearer " }); + assert.equal(resp.status, 400); + const json = (await resp.json()) as { error?: { message?: string } }; + assert.match(json.error?.message ?? "", /Missing stable conversation identity/); + assert.equal((await getSessions(h)).length, 0); + } finally { + await h.close(); + } +}); + test("e2e session identity: anonymous openai request → prefix-affinity session; distinct conversations split (#309)", async () => { const h = await startHarness(); try { diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 21485d764a..c9bf62b0a9 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -349,12 +349,16 @@ 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. +// anchor before the first main turn can claim it. #6548 adds +// isResponsesServerToolSingleShot — same category once more: the Responses +// server-tool-only single-shot shape predicate (string input + search tools, +// no history) consulted by the #286 refusal branch, which forwards that shape +// verbatim instead of hard-400ing a call that cannot carry an identity. 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"]); + const allowlist = new Set(["hasLeakedBiliToolsOnly", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isResponsesServerToolSingleShot", "isServerToolUtilityCall", "isSideRequest", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); let sawImport = false; for (const m of text.matchAll(/\{([^}]*)\}\s*from\s*["'][^"']*side-request\.js["']/g)) { sawImport = true; diff --git a/tests/websearch-side-request.test.ts b/tests/websearch-side-request.test.ts index 82772cde17..c21b3a8bdb 100644 --- a/tests/websearch-side-request.test.ts +++ b/tests/websearch-side-request.test.ts @@ -4,7 +4,7 @@ import http from "node:http"; import { once } from "node:events"; import { defaultConfig, createInitialState } from "acp-kernel"; import { startServer } from "../src/server.ts"; -import { isServerToolUtilityCall, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; +import { isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; @@ -72,6 +72,49 @@ test("isServerToolUtilityCall: negatives — every veto keeps the request on the assert.equal(isServerToolUtilityCall(searchBody({ tools: [{ type: "web_search", name: "web_search" }] })), false, "unversioned web_search type never matches"); }); +// #6548: pi-web-access's xAI web-search backend sends a deliberately minimal +// single-shot Responses body — string input, web_search/x_search tools only, +// no message history — and pi-web-access exposes no knob for a session id, +// so the client cannot carry an identity. The proxy's #286 refusal hard-400s +// it before the side lane ever sees it. The Responses-wire predicate below is +// the shape the refusal branch consults. +function responsesSearchBody(over: Record = {}): Record { + return { + model: "grok-4.7", + input: "latest news on context compression", + tools: [{ type: "web_search" }, { type: "x_search" }], + ...over, + }; +} + +test("isResponsesServerToolSingleShot: positive — the captured pi-web-access shape (#6548)", () => { + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody()), true, "captured shape (web_search + x_search, string input)"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "web_search" }] })), true, "web_search alone"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "web_search_preview" }] })), true, "web_search_preview family"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: "auto" })), true, "string auto"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: "required" })), true, "string required forces one of the declared server tools"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: { type: "web_search" } })), true, "object forcing a DECLARED server tool"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: undefined })), true, "absent tool_choice"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ instructions: "Answer concisely." })), true, "instructions carry no history"); +}); + +test("isResponsesServerToolSingleShot: negatives — every veto keeps the request under the #286 refusal", () => { + assert.equal(isResponsesServerToolSingleShot(null), false, "null body"); + assert.equal(isResponsesServerToolSingleShot({}), false, "empty body"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "q" }] }] })), false, "array input is message history, not a single shot"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ input: undefined })), false, "missing input"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "function", name: "get_weather" }] })), false, "client function tool does not match"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "web_search" }, { type: "function", name: "get_weather" }] })), false, "mixing ANY client-side tool vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [] })), false, "empty tools array"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [null] })), false, "non-object tool entry"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: "none" })), false, "string none vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: { type: "function", name: "get_weather" } })), false, "forcing a client tool vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: { type: "required" } })), false, "unrecognized tool_choice shape vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ previous_response_id: "resp_abc" })), false, "chained continuation is a conversation turn, not a single shot"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ session_id: "sess-1" })), false, "body session_id is an identity signal"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ prompt_cache_key: "pck-1" })), false, "prompt_cache_key is an identity signal"); +}); + test("isSideRequest: the structural shape outranks the #546 tools veto; explicit intent still outranks the structure", () => { const body = searchBody(); assert.equal(isSideRequest(body), true, "server-tool-only utility call is a side request despite non-empty tools and a large budget"); From de2212c113eb1664d739af4ba5ed88f6d56a187b Mon Sep 17 00:00:00 2001 From: Gong Liu <1360187968@qq.com> Date: Sat, 14 Nov 2026 07:17:33 +0800 Subject: [PATCH 11/34] fix(loop): drop a tool-typed tag-echo span whole instead of keeping its body (#6557) --- src/loop/tag-echo-filter.ts | 51 ++++++++- tests/issue6557-tool-typed-echo-span.test.ts | 107 +++++++++++++++++++ 2 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 tests/issue6557-tool-typed-echo-span.test.ts diff --git a/src/loop/tag-echo-filter.ts b/src/loop/tag-echo-filter.ts index fb4f1d33f0..60c0d994b0 100644 --- a/src/loop/tag-echo-filter.ts +++ b/src/loop/tag-echo-filter.ts @@ -247,6 +247,27 @@ const BROKEN_ATTRS = new RegExp("\x3c" + NAME + "\\s[^<>]*(?=\x3c)"); const MANGLED_OPEN = new RegExp("\x3c" + NAME + "\\s*=\\s*[^<>]*>"); const DEFINITE_TAIL = new RegExp("^\x3c" + NAME + "\\s|^\x3c" + NAME + "=|^\x3c\\/" + NAME); const OPEN_WITH_ATTRS = new RegExp("^\x3c" + NAME + "\\s"); +// #6557: a model-emitted opening whose `type` names a TOOL lane +// (`type="tool:acp_cache"`) is not prose wearing tags. The kernel renders tool +// results under that type, so a model writing one in assistant TEXT is +// inventing a tool result: the body between the tags is fabricated output, not +// its own words. The #1720/#2023 rule — "content that is not refs-only is +// prose wearing tags: the tags go, the content stays" — was written for +// `type="text"`, where the body IS the model's prose; applied to a tool-typed +// echo it commits the fabricated result into the assistant turn, and the model +// then imitates "tag + that body" at the head of every later reply +// (production session, #6557: the same JSON block 25 times in 23 minutes). So +// a tool-typed span dies WHOLE — attrs through the close, or through the end +// of the field when the imitation never closes — in the whole-text path +// (TOOL_TYPED_PAIR) and in the streaming swallow (swallowReleases, below). +// Case folds per #1731: an echoed type drifts casing the same way an echoed +// name does. The quote after `type=` is part of the match, so a `type=` that +// appears inside another attribute's VALUE (an unquoted-looking `data-x=a +// type=tool:` run) cannot be mistaken for the attribute itself. +const TOOL_TYPE_ATTR = "\\s+[tT][yY][pP][eE]\\s*=\\s*[\"']\\s*[tT][oO][oO][lL]:"; +const TOOL_TYPE_ATTR_RE = new RegExp(TOOL_TYPE_ATTR); +const TOOL_TYPED_OPEN = new RegExp("\x3c" + NAME + "(?:\\s[^<>]*?)?" + TOOL_TYPE_ATTR + "[^<>]*>"); +const TOOL_TYPED_PAIR = new RegExp(TOOL_TYPED_OPEN.source + "[\\s\\S]*?\x3c\\/" + NAME + ">"); const CLOSE_HEAD = "\x3c/"; const CLOSE_NAME_ANCHORED = new RegExp("^" + NAME); const HOLD_LIMIT = 128; @@ -468,6 +489,24 @@ function stripPass(text: string, dropToolCallEmission: boolean, requestText: str if (wrapped === null) break; out = out.slice(0, wrapped.start) + out.slice(wrapped.end); } + // #6557: a tool-typed echo dies WHOLE (see TOOL_TYPE_ATTR). RUNS BEFORE the + // paired/lone-open passes for the same reason the wrapped span does: either + // of those would strip the markup alone and leave the fabricated tool + // result as assistant text — the exact residue this channel exists to + // prevent. An opening with no close in the field owns the rest of it: the + // imitation was never terminated, so everything after it is its payload + // (the wrapped-turn EOF rule, reused). + for (;;) { + const open = TOOL_TYPED_OPEN.exec(out); + if (open === null) break; + const pair = TOOL_TYPED_PAIR.exec(out); + if (pair !== null && pair.index === open.index) { + out = out.slice(0, pair.index) + out.slice(pair.index + pair[0].length); + continue; + } + out = out.slice(0, open.index); + break; + } out = out.replace(new RegExp(PAIRED.source, "g"), ""); // #2023: a refs run sitting directly against an UNMATCHED close is residue // (its open was consumed elsewhere or never came) and goes with the close. @@ -1430,9 +1469,17 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu // than released at the #644 budget. Every genuine opening has // balanced quotes: `tokens="1" type="text"`. const wrapped = ((m[0].match(/"/g) ?? []).length & 1) === 1; + // #6557: a tool-typed opening frames a FABRICATED tool result, + // not the model's prose (see TOOL_TYPE_ATTR) — its body is + // discarded on the attested-imitation budget instead of being + // released at the #644 one, in both the closed and the + // never-closed (EOF) shape. Gated to attrs-bearing openings: a + // bare opening carries no type at all, so its #1881 + // release-as-prose path is untouched. + const toolTyped = !bare && TOOL_TYPE_ATTR_RE.test(m[0]); swallowUntilClose = true; - swallowLimit = wrapped ? IMITATION_SWALLOW_CAP : SWALLOW_CAP; - swallowReleases = !wrapped; + swallowLimit = wrapped || toolTyped ? IMITATION_SWALLOW_CAP : SWALLOW_CAP; + swallowReleases = !wrapped && !toolTyped; swallowBareOpen = bare; swallowed = ""; } else { diff --git a/tests/issue6557-tool-typed-echo-span.test.ts b/tests/issue6557-tool-typed-echo-span.test.ts new file mode 100644 index 0000000000..5279466f74 --- /dev/null +++ b/tests/issue6557-tool-typed-echo-span.test.ts @@ -0,0 +1,107 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { stripAcpTags, createTagEchoFilter, stripOpenaiChatText } from "../src/loop/tag-echo-filter.ts"; + +// #6557: a model-emitted opening whose type names a TOOL lane frames a +// FABRICATED tool result. The #1720 rule ("prose wearing tags: the tags go, +// the content stays") was written for type="text" and is wrong here: keeping +// the body commits the invention into the assistant turn, and the model then +// imitates "tag + that body" at the head of every later reply. The span dies +// whole, in the whole-text and the streaming path alike. +const LT = "\x3c"; +const GT = "\x3e"; +const FABRICATED = + '{"actualContent": "acp_cache read-only: grand total in=2,874,000 cacheRead=2,753,682", "reason": "result compressed for budget", "tokens": 1000}'; +const toolOpen = (type = "tool:acp_cache") => `${LT}acp tokens="2312" type="${type}" time="2026-11-13T15:52:59Z"${GT}`; +const textOpen = `${LT}acp tokens="78" type="text" time="2026-11-13T15:52:47Z"${GT}`; +const close = `${LT}/acp${GT}`; + +function stream(chunks: string[]): string { + const f = createTagEchoFilter(); + let out = ""; + for (const c of chunks) out += f.push(c); + out += f.flush(); + return out; +} + +/** Every 2-way split of s, plus byte-at-a-time — the chunk boundary must not + * change the verdict (the #4178/#4210 split-invariance discipline). */ +function allChunkings(s: string): string[][] { + const out: string[][] = [[s]]; + for (let i = 1; i < s.length; i++) out.push([s.slice(0, i), s.slice(i)]); + out.push([...s]); + return out; +} + +test("#6557: whole-text drops a tool-typed echo span, prose around it survives", () => { + const text = `before ${toolOpen()}${FABRICATED}${close} after`; + assert.equal(stripAcpTags(text), "before after"); +}); + +test("#6557: whole-text drops every tool-typed span in the field", () => { + const one = `${toolOpen()}${FABRICATED}${close}`; + const two = `${toolOpen("tool:acp_status")}{"blocks": []}${close}`; + assert.equal(stripAcpTags(`a ${one} b ${two} c`), "a b c"); +}); + +test("#6557: an unclosed tool-typed echo owns the rest of the field", () => { + // The imitation was never terminated: everything after the opening is its + // payload (the wrapped-turn EOF rule, reused). + assert.equal(stripAcpTags(`${toolOpen()}${FABRICATED}`), ""); + assert.equal(stripAcpTags(`lead ${toolOpen()}${FABRICATED}`), "lead "); +}); + +test("#6557: the type attribute is matched through casing drift", () => { + assert.equal(stripAcpTags(`x ${toolOpen("TOOL:acp_cache")}${FABRICATED}${close} y`), "x y"); + const driftedAttr = `${LT}acp tokens="2312" Type="tool:acp_cache"${GT}${FABRICATED}${close}`; + assert.equal(stripAcpTags(`x ${driftedAttr} y`), "x y"); +}); + +test("#6557: streaming drops the span — and the verdict is split-invariant", () => { + const text = `before ${toolOpen()}${FABRICATED}${close} after`; + for (const chunks of allChunkings(text)) { + assert.equal(stream(chunks), "before after", `chunking ${JSON.stringify(chunks.map((c) => c.length))}`); + } +}); + +test("#6557: streaming drops an unclosed span at EOF", () => { + const text = `lead ${toolOpen()}${FABRICATED}`; + for (const chunks of allChunkings(text)) { + assert.equal(stream(chunks), "lead ", `chunking ${JSON.stringify(chunks.map((c) => c.length))}`); + } +}); + +test("#6557: the drop is reported to the caller (visible in the log callback)", () => { + const seen: string[] = []; + const f = createTagEchoFilter((snippet) => seen.push(snippet)); + f.push(`before ${toolOpen()}${FABRICATED}${close} after`); + f.flush(); + assert.equal(seen.length, 1, "one artifact, one warn"); + assert.ok(seen[0].includes('type="tool:acp_cache"')); +}); + +// ── recall guards: the tool-typed rule must not eat anything that is prose ── + +test("#6557 guard: a type=\"text\" echo keeps its prose body (#1720 unchanged)", () => { + const text = `see ${textOpen}the model's own sentence${close} end`; + assert.equal(stripAcpTags(text), "see the model's own sentence end"); +}); + +test("#6557 guard: a text echo whose body is a refs-only run still dies whole (#1720)", () => { + assert.equal(stripAcpTags(`a ${textOpen}m00120–m00300${close} b`), "a b"); +}); + +test("#6557 guard: markup in a tool-call argument stays byte-exact (#1039)", () => { + const args = JSON.stringify({ command: `echo '${toolOpen()}${FABRICATED}${close}'` }); + const payload = { choices: [{ message: { content: "", tool_calls: [{ function: { name: "bash", arguments: args } }] } }] }; + const out = stripOpenaiChatText(payload) as typeof payload; + assert.equal(out.choices[0].message.tool_calls[0].function.arguments, args); +}); + +test("#6557 guard: a tool-typed echo inside a fenced code block still goes (no fence exemption elsewhere in the filter)", () => { + // Documented behavior of the whole channel: the filter has no markdown + // awareness (see tag-echo.test.ts), so the assertion here only pins that + // the tool-typed branch did not invent one. + const text = "```\n" + `${toolOpen()}${FABRICATED}${close}` + "\n```"; + assert.equal(stripAcpTags(text), "```\n\n```"); +}); From 32aa9d79aa8b0e02fc5c3f3ac279ddb135790a52 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 14 Nov 2026 09:02:47 +0800 Subject: [PATCH 12/34] fix(loop): a self-closing tool-typed echo is a unit, not an opening (#6557) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TOOL_TYPED_OPEN's tail [^<>]*> read a self-closing imitation () as an OPENING, so the whole-text path made it own the rest of the field (or pair it with a later orphan close) while the streaming path killed it in place via SELF_CLOSE — a whole-text/streaming divergence on the same input, the #4178/#4210 split-invariance class this channel exists to prevent, and a loss of the prose that follows the imitation. Exclude the self-closing form from the opening match (the character before the final > must not be /); it then falls through to the SELF_CLOSE pass, killed in place — the same verdict streaming already reaches, so both paths agree again. A refs-only body with a non-acplike degenerate close still owns the rest of the field in both paths: the corrupted close is not the span's terminator, consistent with the wrapped-turn EOF rule. +4 regression tests: self-close dies in place with following prose surviving (whole-text, both shapes), streaming split-invariance for the self-close shape, and a guard that a type="text" self-close keeps its following prose (SELF_CLOSE semantics unchanged). Reviewer fix-up on the PR branch: found by whole-text vs streaming differential repro during review; one-line regex change, no state-machine change. --- src/loop/tag-echo-filter.ts | 18 +++++++++++---- tests/issue6557-tool-typed-echo-span.test.ts | 24 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/loop/tag-echo-filter.ts b/src/loop/tag-echo-filter.ts index 60c0d994b0..05266cb3a1 100644 --- a/src/loop/tag-echo-filter.ts +++ b/src/loop/tag-echo-filter.ts @@ -259,14 +259,21 @@ const OPEN_WITH_ATTRS = new RegExp("^\x3c" + NAME + "\\s"); // (production session, #6557: the same JSON block 25 times in 23 minutes). So // a tool-typed span dies WHOLE — attrs through the close, or through the end // of the field when the imitation never closes — in the whole-text path -// (TOOL_TYPED_PAIR) and in the streaming swallow (swallowReleases, below). +// (TOOL_TYPED_PAIR) and in the streaming swallow (swallowReleases, below). A +// self-closing imitation is a complete unit, not an opening — it dies in place +// via SELF_CLOSE, prose after it surviving. // Case folds per #1731: an echoed type drifts casing the same way an echoed // name does. The quote after `type=` is part of the match, so a `type=` that // appears inside another attribute's VALUE (an unquoted-looking `data-x=a // type=tool:` run) cannot be mistaken for the attribute itself. const TOOL_TYPE_ATTR = "\\s+[tT][yY][pP][eE]\\s*=\\s*[\"']\\s*[tT][oO][oO][lL]:"; const TOOL_TYPE_ATTR_RE = new RegExp(TOOL_TYPE_ATTR); -const TOOL_TYPED_OPEN = new RegExp("\x3c" + NAME + "(?:\\s[^<>]*?)?" + TOOL_TYPE_ATTR + "[^<>]*>"); +// The `[^/<>]` before the final `>` excludes the SELF-CLOSING form +// (``): that is a complete unit, not an opening, so it must not be +// read as one and made to own the rest of the field. It falls through to the +// SELF_CLOSE pass (killed in place) — the same verdict the streaming path +// reaches via candidate order, keeping both paths split-invariant (#4178/#4210). +const TOOL_TYPED_OPEN = new RegExp("\x3c" + NAME + "(?:\\s[^<>]*?)?" + TOOL_TYPE_ATTR + "[^<>]*[^/<>]>"); const TOOL_TYPED_PAIR = new RegExp(TOOL_TYPED_OPEN.source + "[\\s\\S]*?\x3c\\/" + NAME + ">"); const CLOSE_HEAD = "\x3c/"; const CLOSE_NAME_ANCHORED = new RegExp("^" + NAME); @@ -585,9 +592,10 @@ export function stripAcpTags(text: string, dropToolCallEmission = false, request * so entry-vs-exit comparisons never false-positive on shapes one side * legitimately removed. Single source of truth by construction — when the * filter family grows, the judge's normalization grows with it. Content- - * preserving for prose (paired shapes keep their payload; only tag bytes go), - * and the tool-call emission span is deliberately NOT touched (args are user - * intent, #1039). */ + * preserving for prose (paired shapes keep their payload — except a tool-typed + * span, whose body is a fabricated tool result and dies with the tags, #6557; + * otherwise only tag bytes go), and the tool-call emission span is + * deliberately NOT touched (args are user intent, #1039). */ export function normalizeEchoArtifacts(text: string): string { if ( !containsRenderTagText(text) && diff --git a/tests/issue6557-tool-typed-echo-span.test.ts b/tests/issue6557-tool-typed-echo-span.test.ts index 5279466f74..03af5ec6cd 100644 --- a/tests/issue6557-tool-typed-echo-span.test.ts +++ b/tests/issue6557-tool-typed-echo-span.test.ts @@ -105,3 +105,27 @@ test("#6557 guard: a tool-typed echo inside a fenced code block still goes (no f const text = "```\n" + `${toolOpen()}${FABRICATED}${close}` + "\n```"; assert.equal(stripAcpTags(text), "```\n\n```"); }); + +// ── self-close corner: `` is a complete unit, not an opening ── + +test("#6557: whole-text: a self-closing tool-typed echo dies in place, following prose survives", () => { + const selfClose = `${LT}acp type="tool:acp_cache"/${GT}`; + assert.equal(stripAcpTags(`x ${selfClose} rest of prose`), "x rest of prose"); +}); + +test("#6557: whole-text: a self-closing tool-typed echo does not pair with a later orphan close", () => { + const selfClose = `${LT}acp type="tool:acp_cache"/${GT}`; + assert.equal(stripAcpTags(`x ${selfClose} body ${close} tail`), "x body tail"); +}); + +test("#6557: streaming: a self-closing tool-typed echo dies in place — and the verdict is split-invariant", () => { + const text = `x ${LT}acp type="tool:acp_cache"/${GT} rest of prose`; + for (const chunks of allChunkings(text)) { + assert.equal(stream(chunks), "x rest of prose", `chunking ${JSON.stringify(chunks.map((c) => c.length))}`); + } +}); + +test("#6557 guard: a self-closing type=\"text\" echo still keeps its following prose (SELF_CLOSE semantics unchanged)", () => { + const selfClose = `${LT}acp type="text"/${GT}`; + assert.equal(stripAcpTags(`x ${selfClose} body ${close} tail`), "x body tail"); +}); From 6190fb341dc56a924e8373d1154c75e1d9ab78ce Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Sat, 14 Nov 2026 07:12:09 +0500 Subject: [PATCH 13/34] fixup: restore import brace spacing in merged handle.ts The merge of master dropped the spaces inside the braces of the side-request.js import. Restore repo style; no semantic change. --- src/server/handle.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/handle.ts b/src/server/handle.ts index d6d1a1ce84..1791ad76be 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, 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, isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools} from "./side-request.js"; +import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isResponsesServerToolSingleShot, 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"; From cc2d09eba6b622a7aeeacb0f03936128c56f9513 Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Sat, 14 Nov 2026 02:14:46 +0000 Subject: [PATCH 14/34] fix(openai): route tool-less audio (TTS) requests through the side passthrough A TTS call on /chat/completions carries a top-level audio object and no tools. It ran through the full pipeline, which injected the ACP system prompt as messages[0] plus an prefix on every message, and TTS models 400 on the leading system message. isAudioOutputCall now recognizes the shape and routes it through the #388 verbatim side passthrough, with a WC-025 ledger entry and tripwire in the fakes. --- src/server.ts | 2 +- src/server/handle.ts | 14 +- src/server/side-request.ts | 35 +++++ tests/issue6636-tts-side-request.test.ts | 161 +++++++++++++++++++++++ tests/side-lane-invariants.test.ts | 8 +- tests/wire-contract-fakes.ts | 22 +++- tests/wire-contract.test.ts | 48 +++++++ 7 files changed, 282 insertions(+), 8 deletions(-) create mode 100644 tests/issue6636-tts-side-request.test.ts diff --git a/src/server.ts b/src/server.ts index 03912500e6..8fdf4854eb 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, 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 { BILI_TOOL_NAMES, isAudioOutputCall, 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 2627eb8841..5de3bbd079 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, 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 { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isAudioOutputCall, 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"; @@ -2416,6 +2416,14 @@ async function handleRequest( // outputBudgetHighWater from it would poison the first starved restore // (same class as the #1897/#2500 skip). const structuredOutputUtility = isStructuredOutputCall(parsed); + // #6636: a tool-less audio-output request (TTS on the chat wire) is + // the same category — a one-shot utility call under the main session + // id. It rides the #388 side passthrough via isSideRequest, and must + // ALSO skip restoreOutputBudget here: its max_tokens sizes the + // spoken reply, not a main turn, and seeding + // outputBudgetHighWater from it would poison the first starved + // restore (same class as the #1897/#2500/#5073 skip). + const audioOutputUtility = isAudioOutputCall(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 @@ -2427,7 +2435,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 && !structuredOutputUtility) { + if (!demotedSide && !serverToolUtility && !structuredOutputUtility && !audioOutputUtility) { // #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. @@ -2629,7 +2637,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)" : 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 }); + const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : audioOutputUtility ? "audio-output utility call (#6636)" : 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 d991c40feb..6fddeda8a4 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -101,6 +101,35 @@ export function isStructuredOutputCall(parsed: unknown): boolean { return typeof type === "string" && STRUCTURED_FORMAT_TYPES.has(type); } +/** #6636: structural identification of a tool-less audio-output utility + * call — a TTS request on the chat wire carries a top-level `audio` object + * (`{voice, format}`, OpenAI chat-completions audio output) and no tools. + * It is a one-shot media side call, not an agent turn: agent turns always + * carry an action surface (#546), and a TTS model behind an + * OpenAI-compatible endpoint rejects the ACP decoration outright — the + * injected leading system prompt 400s ("messages[0] system role is not + * allowed for TTS model", Xiaomi MiMo mimo-v2.5-tts) and the per-message + * prefixes would be read aloud as text. Routed through the #388 side + * passthrough like the #2500/#5073 utility shapes above, so the request + * leaves the machine exactly as the client wrote it. + * The no-tools veto is part of the predicate (same discipline as + * isStructuredOutputCall): a tool-carrying request is a main turn even + * when it asks for audio output. An empty `audio` object does not count — + * it carries no media request. Residual, accepted: a tool-less plain chat + * turn that asks for audio output (voice-mode chat with no tools) also + * rides the side lane for that one turn — it forwards verbatim and its + * fold state simply does not advance; the ACP decoration is never needed + * for the reply to work, and the next tool-carrying turn rejoins the full + * pipeline. */ +export function isAudioOutputCall(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 audio = p.audio; + if (audio === null || typeof audio !== "object" || Array.isArray(audio)) return false; + return Object.keys(audio).length > 0; +} + 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; @@ -120,6 +149,12 @@ export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { // pair with a 400. Routed through the #388 side passthrough like the #2500 // server-tool shape above. if (isStructuredOutputCall(parsed)) return true; + // #6636: a tool-less request asking for audio output (top-level `audio` + // object — TTS on the chat wire) is a one-shot media side call, not an + // agent turn. The ACP decoration 400s it (leading system prompt) and + // would corrupt the spoken text (per-message tags), so it rides the + // #388 side passthrough verbatim like the utility shapes above. + if (isAudioOutputCall(parsed)) return true; const field = outputBudgetField(parsed); if (!field) return false; const raw = readOutputBudget(p, field); diff --git a/tests/issue6636-tts-side-request.test.ts b/tests/issue6636-tts-side-request.test.ts new file mode 100644 index 0000000000..137a620951 --- /dev/null +++ b/tests/issue6636-tts-side-request.test.ts @@ -0,0 +1,161 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +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 { _resetPluginStateForTest, resolveConversation } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { isAudioOutputCall, isSideRequest, resolveSideLane } from "../src/server/side-request.ts"; + +// #6636: dsh-fish-soul synthesizes speech through a TTS model exposed on +// /chat/completions (Xiaomi MiMo mimo-v2.5-tts). The plugin sends a tool-less +// {model, messages:[user, assistant], audio:{format, voice}, stream:false} +// body — a one-shot media side call, not a conversation. Pre-fix bili ran it +// through the full pipeline: the FUNCTION_ACP_TAGS_TIMESTAMPED system prompt +// was injected as messages[0] and every message got an prefix, and the +// upstream 400d with "messages[0] system role is not allowed for TTS model" +// (the same messages undecorated return 200 — control experiment in the +// issue). Post-fix the tool-less audio shape rides the #388 verbatim side +// passthrough: nothing injected, kernel state untouched. Delete the predicate +// in src/server/side-request.ts and this goes red. +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-tts-side-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = testRoot; + +// The issue's exact TTS body (verbatim, including the Chinese text). +const TTS_BODY = { + model: "mimo-v2.5-tts", + messages: [ + { role: "user", content: "请用自然口语的方式朗读下面这句话" }, + { role: "assistant", content: "这条要念的话。" }, + ], + audio: { format: "mp3", voice: "mimo_default" }, + stream: false, +}; + +test("isAudioOutputCall: positive — the captured #6636 TTS shape", () => { + assert.equal(isAudioOutputCall(TTS_BODY), true, "issue shape (audio object, no tools)"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: { voice: "v" } }), true, "voice-only audio still counts"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: { format: "mp3" } }), true, "format-only audio still counts"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: { format: "mp3" }, tools: [] }), true, "an empty tools array is no action surface"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: { format: "mp3" }, stream: true }), true, "streaming TTS is still a media side call"); +}); + +test("isAudioOutputCall: negatives — every veto keeps the request on the main pipeline", () => { + assert.equal(isAudioOutputCall(null), false, "null body"); + assert.equal(isAudioOutputCall({}), false, "empty body"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }] }), false, "no audio field"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: {} }), false, "empty audio object carries no media request"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: "mp3" }), false, "audio must be an object"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: [{ format: "mp3" }] }), false, "audio must not be an array"); + assert.equal(isAudioOutputCall({ ...TTS_BODY, tools: [{ type: "function", function: { name: "bash", parameters: { type: "object", properties: {} } } }] }), false, "any tool entry vetoes (#546: an action surface marks a main turn)"); +}); + +test("isSideRequest: the audio shape is a side request; explicit intent still outranks the structure", () => { + assert.equal(isSideRequest(TTS_BODY), true, "tool-less audio request is a side request"); + assert.equal(isSideRequest(TTS_BODY, "main"), false, "explicit main intent vetoes the structure"); + assert.equal(isSideRequest(TTS_BODY, "title"), true, "declared side agent stays side"); + // The old rules are untouched for everything the structure does not claim. + assert.equal(isSideRequest({ max_tokens: 100 }), true, "tiny-budget heuristic intact"); + assert.equal(isSideRequest({ max_tokens: 1024, tools: [{ name: "compress" }] }), false, "#546 veto intact for client tools"); +}); + +test("resolveSideLane: the #6636 label names the structural shape in the log line", () => { + assert.equal( + resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "openai", stripApplied: false, sideIntent: true, requestAgent: undefined, sideLabel: "audio-output utility call (#6636)" }).reason, + "audio-output utility call (#6636)", + ); +}); + +async function harness() { + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + mkdirSync(join(dir, "billion-context", "sessions"), { recursive: true }); + mkdirSync(join(dir, "sessions"), { recursive: true }); + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: false, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const forwarded: Record[] = []; + const upstream = await new Promise<{ url: string; close: () => Promise }>((resolve) => { + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "chat_test", object: "chat.completion", choices: [{ index: 0, message: { role: "assistant", content: "answer" }, finish_reason: "stop" }], usage: { prompt_tokens: 10000, completion_tokens: 10, total_tokens: 10010 } })); + }); + }); + server.listen(0, "127.0.0.1", () => resolve({ url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, close: async () => { server.close(); } })); + }); + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstream.url, routes: { [upstream.url]: { models: { "fake-a": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: true, preserveRecentMessages: 1, preserveRecentTokens: 0, minCompressRangeChars: 100 }, 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 origin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + // dsh shape: x-bili-plugin + conversation headers always; the persona agent + // header is stamped on MAIN turns only — the TTS side call arrives without + // it, like the title sidecar. + const dshSend = async (conversationId: string, body: Record, agent?: string) => { + const headers: Record = { "content-type": "application/json", "x-acp-session": conversationId, "x-bili-plugin": "dsh", "x-bili-plugin-conversation": conversationId }; + if (agent !== undefined) headers["x-bili-plugin-agent"] = agent; + const response = await fetch(`${origin}/bili/${upstream.url}/v1/chat/completions`, { method: "POST", headers, body: JSON.stringify(body) }); + assert.equal(response.status, 200, await response.text()); + }; + const sessionIds = async () => { + const body = (await (await fetch(origin + "/__bili/sessions")).json()) as { sessions?: { id: string }[] }; + assert.ok(Array.isArray(body.sessions), `/__bili/sessions returned no list: ${JSON.stringify(body)}`); + return body.sessions.map((s) => s.id); + }; + const subSessions = async (prefix: string) => (await sessionIds()).filter((id) => id.startsWith(prefix + "|sub:")); + return { forwarded, dshSend, subSessions, origin, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await new Promise((r2) => setTimeout(r2, 50)); } }; +} + +test("e2e: the #6636 TTS call rides the #388 passthrough verbatim — no system prompt, no tags, no kernel touch", async () => { + const h = await harness(); + try { + // 1. TTS-first on a fresh session: the side call must not claim the + // raw-key anchor with anything (no |sub: ghost, no kernel request). + await h.dshSend("conv-tts-first", TTS_BODY); + assert.deepEqual(h.forwarded.at(-1), TTS_BODY, "TTS call forwarded byte-identical (no system injected, no tags, audio intact)"); + assert.deepEqual(await h.subSessions("conv-tts-first"), [], "the TTS call mints no |sub: ghost"); + assert.equal(resolveConversation("conv-tts-first").session?.stats.requests ?? 0, 0, "the TTS call runs no kernel request"); + + // 2. Control: a real main turn on a second session still gets the ACP + // decoration (leading system prompt + per-message tags). + const history = [ + { role: "user", content: "set up the workspace" }, + { role: "assistant", content: "done" }, + ]; + await h.dshSend("conv-tts-after-main", { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "system", content: "MAIN OPERATING SYSTEM" }, ...history, { role: "user", content: "now configure ci" }], tools: [{ type: "function", function: { name: "bash", description: "run a command", parameters: { type: "object", properties: {} } } }] }, "main"); + const mainFwd = h.forwarded.at(-1) as { messages: { role: string; content: string }[] }; + const mainSys = mainFwd.messages.filter((m) => m.role === "system").map((m) => m.content).join("\n"); + assert.ok(mainSys.includes("ACP TAGS"), "control: the main turn still gets the ACP system prompt"); + assert.ok(mainFwd.messages.some((m) => typeof m.content === "string" && m.content.includes(" tags"); + const mainSession = resolveConversation("conv-tts-after-main").session!; + assert.ok(mainSession, "raw-key session exists"); + assert.equal(mainSession.stats.requests, 1, "the main turn runs the full pipeline exactly once"); + const requestsAfterMain = mainSession.stats.requests; + const inputAfterMain = mainSession.stats.lastInputTokens; + + // 3. The TTS call on the SAME session, after anchoring: still verbatim, + // kernel state and usage baseline untouched. + await h.dshSend("conv-tts-after-main", TTS_BODY); + assert.deepEqual(h.forwarded.at(-1), TTS_BODY, "post-anchor TTS call still forwarded byte-identical"); + assert.equal(mainSession.stats.requests, requestsAfterMain, "the TTS call must not count as a main request"); + assert.equal(mainSession.stats.lastInputTokens, inputAfterMain, "the TTS call must not touch the usage baseline"); + assert.deepEqual(await h.subSessions("conv-tts-after-main"), [], "still no |sub: ghost after anchoring"); + } finally { + await h.close(); + } +}); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 2f9501f7c2..7919af2351 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -354,12 +354,16 @@ test("(F) extracted protocol modules keep the side-lane boundary (#1440 P2 cut 2 // 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. +// high-water path, exactly like the #2500 server-tool predicate. #6636 adds +// isAudioOutputCall — same category once more: a pure toolless audio-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 #5073 structured-output 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", "hasSingleShotBudget", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); + const allowlist = new Set(["hasLeakedBiliToolsOnly", "hasSingleShotBudget", "isAudioOutputCall", "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; diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index bfd64a4a9f..e1fafc4e07 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -220,6 +220,14 @@ export const WIRE_RULES: readonly WireRule[] = [ provenance: 'bili #5998 production report (DSH self-compression rewrote session messages and the rebuilt request carried signature-less thinking blocks; an opus→sonnet mid-session switch amplifies the failure because signatures are bound to the producing model). Mechanism verified in code: kernel anthropicToCore stamps thinkingSignature only when the inbound block carries one, while coreToAnthropic re-emits EVERY reasoning core as a thinking block — unsigned cores therefore produce signature-less blocks on the wire, which Anthropic proper rejects ("thinking.signature: Field required", #2501). The two affected upstream personalities are byte-indistinguishable on the inbound wire — never-signing relays (DeepSeek /anthropic, #5495) require the unsigned blocks back verbatim — so the repair is learn-on-failure at bili\'s relay egress seam (src/server/thinking-posture.ts): first "Field required" 400 ⇒ one strip-and-resend probe; accepted ⇒ the session drops unsigned thinking proactively for the rest of its life. Constraint source: Anthropic Messages API extended-thinking contract (preserved thinking must be replayed verbatim); the kernel round-trip suite pins the positive half ("signature reattached — Anthropic rejects thinking without it").', }, + { + id: "WC-025", + wire: "openai-chat", + summary: + 'a TTS-shaped chat-completions request (a top-level `audio` object — voice/format for audio output) must not carry a leading role:"system" message — TTS models behind OpenAI-compatible endpoints reject it with 400 ("messages[0] system role is not allowed for TTS model"); bili must never inject its ACP system prompt into such a request, and the per-message tags must never be stamped onto text that will be read aloud', + provenance: + 'bili #6636 production 400 (dsh-fish-soul TTS through bili 0.1.213, Xiaomi MiMo mimo-v2.5-tts over an OpenAI-compatible /chat/completions endpoint): the plugin sends {model, messages:[user, assistant], audio:{format:"mp3",voice:"mimo_default"}, stream:false} with no tools; bili injected the FUNCTION_ACP_TAGS_TIMESTAMPED system prompt as messages[0] plus an prefix on every message, and the upstream answered 400 "messages[0] system role is not allowed for TTS model". Control experiment in the issue: the same messages undecorated return 200 with audio. Repair: isAudioOutputCall (src/server/side-request.ts) routes tool-less audio requests through the #388 verbatim side passthrough, so neither the system prompt nor the tags are ever injected. The validator keys on the `audio` field, not the model name — the shape, not the spelling, is what makes the call a TTS request.', + }, ]; // --------------------------------------------------------------------------- @@ -422,10 +430,20 @@ function validateAnthropicBody(body: unknown): string[] { return out; } -/** WC-004 on an OpenAI chat/completions body (only function-typed entries). */ +/** WC-004 + WC-025 on an OpenAI chat/completions body (only function-typed entries for WC-004). */ export function validateOpenAiChatBody(body: unknown): string[] { const out: string[] = []; - if (!isPlainObject(body) || !Array.isArray(body.tools)) return out; + if (!isPlainObject(body)) return out; + // WC-025 (#6636): runs before the tools early-return — a TTS-shaped + // request (top-level `audio` object) must never carry a leading system + // message: TTS models behind OpenAI-compatible endpoints 400 it + // ("messages[0] system role is not allowed for TTS model"). The tripwire + // keys on the audio shape, not the model name. + if (isPlainObject(body.audio) && Object.keys(body.audio).length > 0 + && Array.isArray(body.messages) && body.messages.length > 0 + && isPlainObject(body.messages[0]) && body.messages[0].role === "system") + out.push(`WC-025 messages[0]: leading role:"system" message on a TTS-shaped request (top-level audio object) — TTS models reject a system message with 400 (#6636)`); + if (!Array.isArray(body.tools)) return out; body.tools.forEach((t, i) => { if (!isPlainObject(t)) return; const fn = t.function; diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index 6a3e05d4de..ef41dfd7df 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -166,6 +166,52 @@ test("WC-024 thinking blocks must carry a signature (#5998)", () => { })[0], /WC-024/); }); +test("WC-025 TTS-shaped requests must not carry a leading system message (#6636)", () => { + // Illegal: the #6636 shape — audio output + a leading system message is + // what bili injected (ACP tags prompt) and what the TTS upstream 400s. + assert.match(VALIDATORS["openai-chat"]({ + model: "mimo-v2.5-tts", + messages: [ + { role: "system", content: "ACP TAGS\n\nAddressable messages…" }, + { role: "user", content: "read this aloud" }, + { role: "assistant", content: "the text to speak." }, + ], + audio: { format: "mp3", voice: "mimo_default" }, + stream: false, + })[0], /WC-025/); + // Legal: the same TTS call without the leading system (the issue's + // control experiment — returns 200). + assert.deepEqual(VALIDATORS["openai-chat"]({ + model: "mimo-v2.5-tts", + messages: [ + { role: "user", content: "read this aloud" }, + { role: "assistant", content: "the text to speak." }, + ], + audio: { format: "mp3", voice: "mimo_default" }, + stream: false, + }), []); + // Legal: a leading system message with no audio object is a plain chat + // turn, not a TTS request — the rule keys on the audio shape. + assert.deepEqual(VALIDATORS["openai-chat"]({ + model: "fake-a", + messages: [ + { role: "system", content: "ACP TAGS\n\nAddressable messages…" }, + { role: "user", content: "hello" }, + ], + stream: false, + }), []); + // Legal: an empty audio object carries no media request. + assert.deepEqual(VALIDATORS["openai-chat"]({ + model: "mimo-v2.5-tts", + messages: [ + { role: "system", content: "ACP TAGS" }, + { role: "user", content: "hello" }, + ], + audio: {}, + stream: false, + }), []); +}); + test("WC-016 validates global-scope breakpoint ordering (render order: tools → system → messages)", () => { // #2648 shape: unmarked tools render ahead of a global system block. assert.match(VALIDATORS.anthropic({ @@ -274,6 +320,8 @@ test("wire-contract ledger: every rule has a live enforcement clause", () => { { model: "gemini-synthetic", tools: [{ type: "function", function: { name: "compress", parameters: { type: "object", properties: { content: { type: ["array", "string"] } } } } }] }, { tools: [{ type: "function", function: { name: "x".repeat(65), parameters: { type: "object", properties: {} } } }] }, { tools: [{ type: "function", function: { name: "ok", parameters: { anyOf: [] } } }] }, + // WC-025: the #6636 shape — TTS audio object + a leading system message. + { model: "mimo-v2.5-tts", messages: [{ role: "system", content: "ACP TAGS" }, { role: "user", content: "hi" }], audio: { format: "mp3", voice: "mimo_default" } }, ], responses: [ { type: "response.create", input: [], stream: true }, From 6321ba055c1bb29b9b5c74cebc08f59050926aec Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 14 Nov 2026 12:40:12 +0800 Subject: [PATCH 15/34] fix(tests): WC-025 validator mirrors the isAudioOutputCall no-tools veto (#6636 review) The tripwire flagged ANY audio-shaped body with a leading system message, but isAudioOutputCall vetoes tool-carrying requests (a main turn whose model has an action surface and accepts a leading system). A legitimate tool-carrying voice-chat main turn, decorated by bili, would have tripped the fake upstream. Add the same no-tools veto so the validator matches the predicate, and pin the boundary with a legal test row. --- tests/wire-contract-fakes.ts | 13 +++++++++---- tests/wire-contract.test.ts | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index e1fafc4e07..f47be55b01 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -435,11 +435,16 @@ export function validateOpenAiChatBody(body: unknown): string[] { const out: string[] = []; if (!isPlainObject(body)) return out; // WC-025 (#6636): runs before the tools early-return — a TTS-shaped - // request (top-level `audio` object) must never carry a leading system - // message: TTS models behind OpenAI-compatible endpoints 400 it + // request (top-level `audio` object, no tools) must never carry a leading + // system message: TTS models behind OpenAI-compatible endpoints 400 it // ("messages[0] system role is not allowed for TTS model"). The tripwire - // keys on the audio shape, not the model name. - if (isPlainObject(body.audio) && Object.keys(body.audio).length > 0 + // keys on the audio shape, not the model name. The no-tools veto mirrors + // isAudioOutputCall: a tool-carrying request is a main turn (its model + // has an action surface, is not a TTS model, and accepts a leading + // system), so bili's ACP decoration there is legal. + const wc025NoTools = !Array.isArray(body.tools) || body.tools.length === 0; + if (wc025NoTools + && isPlainObject(body.audio) && Object.keys(body.audio).length > 0 && Array.isArray(body.messages) && body.messages.length > 0 && isPlainObject(body.messages[0]) && body.messages[0].role === "system") out.push(`WC-025 messages[0]: leading role:"system" message on a TTS-shaped request (top-level audio object) — TTS models reject a system message with 400 (#6636)`); diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index ef41dfd7df..48642ef72a 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -210,6 +210,20 @@ test("WC-025 TTS-shaped requests must not carry a leading system message (#6636) audio: {}, stream: false, }), []); + // Legal: a tool-carrying audio request is a main turn, not a TTS call — + // its model has an action surface and accepts a leading system message, + // so bili's ACP decoration there is legal (no-tools veto, mirrors + // isAudioOutputCall). + assert.deepEqual(VALIDATORS["openai-chat"]({ + model: "voice-chat", + messages: [ + { role: "system", content: "ACP TAGS" }, + { role: "user", content: "hello" }, + ], + audio: { format: "wav", voice: "alloy" }, + tools: [{ type: "function", function: { name: "bash", parameters: { type: "object", properties: {} } } }], + stream: false, + }), []); }); test("WC-016 validates global-scope breakpoint ordering (render order: tools → system → messages)", () => { From baee51f0ab870a594be86eca5ea71c0bd1bde731 Mon Sep 17 00:00:00 2001 From: mariopasquali <46114603+mariopasquali@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:20:52 +0100 Subject: [PATCH 16/34] fix: retain large seed envelopes on the incremental lane --- src/incremental-lane.ts | 5 ++++- tests/incremental-large-body.test.ts | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/incremental-large-body.test.ts diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index 22ea84aa4c..ebc93e7520 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -77,7 +77,10 @@ export const INCREMENTAL_TURN_PATH = "/__bili/plugin/turn"; * conversations, side lanes) — the shape is the final word, so a marker'd * ordinary body replays through the normal pipeline untouched. */ export function incrementalTurnBodyShape(raw: Buffer): boolean { - if (raw.length < 2 || raw.length > 64 * 1024 * 1024) return false; + // readBody already enforces the receive-size budget before this check. + // A large lane-only envelope must not fall through to ordinary upstream + // relay merely because its seed crosses a second, unrelated size limit. + if (raw.length < 2) return false; let parsed: unknown; try { parsed = JSON.parse(raw.toString("utf8")); diff --git a/tests/incremental-large-body.test.ts b/tests/incremental-large-body.test.ts new file mode 100644 index 0000000000..e8d92696f0 --- /dev/null +++ b/tests/incremental-large-body.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { incrementalTurnBodyShape } from "../src/incremental-lane.js"; +import { incrementalEnvelopeConversationId } from "../src/agent/incremental-client.js"; + +test("large incremental envelopes keep the same lane classification as the client", () => { + const envelope = { conversationId: "synthetic-large-seed", sinceSeq: 0, messages: [{ role: "user", content: "synthetic" }], request: { model: "fixture-model" } }; + assert.equal(incrementalEnvelopeConversationId(envelope), envelope.conversationId); + // Valid JSON whitespace crosses the old heuristic without allocating a + // huge nested text object. readBody owns the actual receive-size budget. + const raw = Buffer.from(JSON.stringify(envelope) + " ".repeat(64 * 1024 * 1024)); + assert.ok(raw.length > 64 * 1024 * 1024); + assert.equal(incrementalTurnBodyShape(raw), true, "must not relay a lane-only envelope as an ordinary model request"); +}); + +test("large marked ordinary requests retain their ordinary classification", () => { + const ordinary = { model: "fixture-model", messages: [{ role: "user", content: "synthetic" }] }; + const raw = Buffer.from(JSON.stringify(ordinary) + " ".repeat(64 * 1024 * 1024)); + assert.equal(incrementalEnvelopeConversationId(ordinary), undefined); + assert.equal(incrementalTurnBodyShape(raw), false); +}); From ec576aa7cd14490da69db2c617448983a33b9fd9 Mon Sep 17 00:00:00 2001 From: Overview3833 <143307273+Overview3833@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:33:20 +0800 Subject: [PATCH 17/34] fix(hermes-plugin): resolve the bili dist without a bili.json sidecar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `hermes plugins install /hermes-plugin` never writes bili.json, so the plugin stood inert on any machine without `bili plugin install hermes`. load_sidecar now falls back to a host-managed copy (node_modules/billion-context) and then to the reference lane (#1196): the global dist via `bili` on PATH. Nothing is written, so the one-writer contract is untouched. plugin.yaml declares every tool/hook/middleware register() installs, so the host validates the real surface instead of a stale subset (hermes_cli/plugin_validate.py fails on undeclared, warns on declared-but-unregistered — the imageRescale=off surface is therefore a warning, never a failure). tests/hermes-native.test.ts covers the ladder end to end: sidecar -> node_modules -> PATH -> inert, plus the realpath/.js guard (a shell wrapper named `bili` is not a dist) and a read-only assertion. The no-sidecar scenario is pinned to a node-only PATH so a machine-wide `bili` can no longer decide its outcome. Proven red on the base: bundled/path resolve to null there. Fixes #6699 --- hermes-plugin/__init__.py | 40 +++++++++++++---- hermes-plugin/plugin.yaml | 9 ++++ tests/hermes-native.test.ts | 85 +++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 8 deletions(-) diff --git a/hermes-plugin/__init__.py b/hermes-plugin/__init__.py index c9e04e4d8d..4551e30419 100644 --- a/hermes-plugin/__init__.py +++ b/hermes-plugin/__init__.py @@ -193,21 +193,44 @@ def sidecar_path() -> Path: return Path(__file__).resolve().parent / "bili.json" +def bundled_dist() -> Optional[Path]: + """A host-managed copy of the package: `hermes plugins install` runs npm in this + plugin's own directory when its package.json declares billion-context.""" + dist = Path(__file__).resolve().parent / "node_modules" / "billion-context" / "dist" / "index.js" + return dist if dist.is_file() else None + + +def path_dist() -> Optional[Path]: + """The reference lane (#1196): the global install's dist, reached through the `bili` + entry point on PATH — an executable symlink onto dist/index.js.""" + exe = shutil.which("bili") + if not exe: + return None + real = Path(os.path.realpath(exe)) + return real if real.is_file() and real.suffix == ".js" else None + + def load_sidecar() -> Optional[Dict[str, str]]: - """The installer's sidecar: which bili dist + node binary to spawn.""" + """Which bili dist + node binary to spawn. First hit wins, nothing is ever written: + 1. bili.json, the sidecar `bili plugin install hermes` drops (reference lane); + 2. this plugin's node_modules, when the host installed the package itself; + 3. the global install's dist, via `bili` on PATH.""" try: data = json.loads(sidecar_path().read_text(encoding="utf-8")) except Exception: - return None + data = {} if not isinstance(data, dict): + data = {} + node = data.get("nodePath") or shutil.which("node") + if not node or not isinstance(node, str): return None script = data.get("proxyScript") - node = data.get("nodePath") or shutil.which("node") if not script or not isinstance(script, str) or not Path(script).is_file(): + script = "" + found = Path(script) if script else (bundled_dist() or path_dist()) + if found is None: return None - if not node or not isinstance(node, str): - return None - return {"script": script, "node": node} + return {"script": str(found), "node": node} # — gating -------------------------------------------------------------------- @@ -1871,8 +1894,9 @@ def register(ctx: Any) -> None: return sidecar = load_sidecar() if not sidecar: - logger.warning("billion-context: no usable bili.json sidecar next to the hermes plugin " - "(run `bili plugin install hermes`) — staying inert") + logger.warning("billion-context: no bili dist found (no usable bili.json sidecar, no " + "node_modules/billion-context, no `bili` on PATH) — run `bili plugin " + "install hermes` — staying inert") return # 1) Everything cheap and blocking-free goes in FIRST. The host caps import+register() at # its load deadline (10s) and ABANDONS the load on timeout — every registration made diff --git a/hermes-plugin/plugin.yaml b/hermes-plugin/plugin.yaml index 9ba101a03f..c46874b10c 100644 --- a/hermes-plugin/plugin.yaml +++ b/hermes-plugin/plugin.yaml @@ -6,5 +6,14 @@ provides_tools: - compress - decompress - acp_status + - acp_cache + - search_context + # withheld when imageRescale is off; the host warns on declared-but-unregistered, never fails + - acp_read_original_image +provides_hooks: + - pre_api_request +provides_middleware: + - llm_request +# alias for hosts that predate provides_hooks (plugins_cmd_catalog reads provides_hooks, then hooks) hooks: - pre_api_request diff --git a/tests/hermes-native.test.ts b/tests/hermes-native.test.ts index 2c3c79dc26..d8c33fbe39 100644 --- a/tests/hermes-native.test.ts +++ b/tests/hermes-native.test.ts @@ -368,6 +368,22 @@ def install_sidecar(bad): with open(os.path.join(PLUGIN_DIR, "bili.json"), "w") as f: json.dump({"proxyScript": script, "nodePath": os.environ["BC_NODE"]}, f) +def write_dist(base_dir): + d = os.path.join(base_dir, "billion-context", "dist") + os.makedirs(d, exist_ok=True) + p = os.path.join(d, "index.js") + with open(p, "w") as f: + f.write("// dummy bili dist entry point\n") + os.chmod(p, 0o755) # npm chmods the bin it links onto; `bili` must be executable on PATH + return p + +def stub_bili(bin_dir, target): + p = os.path.join(bin_dir, "bili") + if os.path.exists(p): + os.remove(p) + os.symlink(target, p) + return p + # --- #2905 instance-record fixtures (identity fields discovery now checks) --- def fp_of(bad): @@ -499,7 +515,42 @@ elif SCENARIO.startswith("gate-"): os.environ[envvar] = "http://127.0.0.1:1" if envvar == "BILLION_CONTEXT_PROXY" else "0" mod.register(ctx) elif SCENARIO == "no-sidecar": + # Hermetic: a `bili` on the machine's own PATH must not turn "no dist anywhere" into a + # resolution — the ladder's last rung is asserted against a PATH that holds node only. + os.environ["PATH"] = os.path.dirname(os.environ["BC_NODE"]) mod.register(ctx) +elif SCENARIO == "load-sidecar": + # #6672: the sidecar resolution ladder, first hit wins and nothing is ever written. + before = sorted(os.listdir(PLUGIN_DIR)) + res = {} + install_sidecar(False) + res["sidecar"] = mod.load_sidecar() + os.remove(os.path.join(PLUGIN_DIR, "bili.json")) + global_dist = write_dist(os.path.join(BASE, "global")) + write_dist(os.path.join(PLUGIN_DIR, "node_modules")) + res["bundled"] = mod.load_sidecar() + shutil.rmtree(os.path.join(PLUGIN_DIR, "node_modules")) + if os.name == "nt": + res["path"] = res["path_wrapper"] = res["none"] = "skip" + else: + bin_dir = os.path.join(BASE, "bin") + os.makedirs(bin_dir, exist_ok=True) + node_dir = os.path.dirname(os.environ["BC_NODE"]) + wrapper = os.path.join(BASE, "wrapper.sh") + with open(wrapper, "w") as f: + f.write("#!/bin/sh\nexit 0\n") + os.chmod(wrapper, 0o755) + stub_bili(bin_dir, global_dist) + os.environ["PATH"] = bin_dir + os.pathsep + node_dir + res["path"] = mod.load_sidecar() + # A shell wrapper is not the dist: the realpath+.js guard falls through to inert. + stub_bili(bin_dir, wrapper) + res["path_wrapper"] = mod.load_sidecar() + os.remove(os.path.join(bin_dir, "bili")) + os.environ["PATH"] = node_dir + res["none"] = mod.load_sidecar() + out["sidecar_resolution"] = res + out["plugin_dir_unchanged"] = sorted(os.listdir(PLUGIN_DIR)) == before elif SCENARIO == "spawn-fail": install_sidecar(True) mod.register(ctx) @@ -1272,6 +1323,8 @@ interface DriverOut { rf_dead?: unknown; rf_dup_pid?: string | null; rf_none?: unknown; + sidecar_resolution?: Record; + plugin_dir_unchanged?: boolean; } function runDriver(scenario: string, extraEnv: Record = {}): { out: DriverOut | null; err: string } { @@ -1667,6 +1720,38 @@ describe("python plugin runtime (subprocess)", () => { assert.equal(out!.env_https_proxy, null); }); + test("no sidecar: resolves the dist from a host-managed copy, then from `bili` on PATH (#6672)", () => { + if (!PY) { + console.warn("skip: no python3/python interpreter on this machine (hermes requires Python 3.11+)"); + return; + } + const { out, err } = runDriver("load-sidecar"); + assert.ok(out, err); + const res = out!.sidecar_resolution!; + const scriptOf = (v: unknown): string | null => (v && typeof v === "object" ? (v as { script: string }).script : null); + // 1. the installer's sidecar still wins untouched + assert.match(scriptOf(res.sidecar)!, /[/\\]proxy\.js$/, "bili.json stays the first hit"); + // 2. a host-managed copy in the plugin's own node_modules + assert.match( + scriptOf(res.bundled)!, + /[/\\]node_modules[/\\]billion-context[/\\]dist[/\\]index\.js$/, + "no sidecar: the plugin resolves the dist its own npm install put there", + ); + if (process.platform !== "win32") { + // 3. the global install, reached through `bili` on PATH (realpath onto dist/index.js) + assert.match( + scriptOf(res.path)!, + /[/\\]global[/\\]billion-context[/\\]dist[/\\]index\.js$/, + "no sidecar, no local copy: the reference lane's global dist is resolved off PATH", + ); + // 3b. a shell wrapper named bili is not the dist — the realpath+.js guard keeps it inert + assert.equal(res.path_wrapper, null, "a non-.js entry point falls through instead of spawning it"); + // 4. nothing anywhere is still inert + assert.equal(res.none, null); + } + assert.equal(out!.plugin_dir_unchanged, true, "resolution is read-only: no sidecar is ever written"); + }); + test("spawn failure: child dies -> inert, env untouched, starting marker cleaned up", () => { if (!PY) { console.warn("skip: no python3/python interpreter on this machine (hermes requires Python 3.11+)"); From d1b02b3a777b064abaec2956f65a9180e82c4b59 Mon Sep 17 00:00:00 2001 From: Delthin <1059661071@qq.com> Date: Sat, 14 Nov 2026 19:15:20 +0800 Subject: [PATCH 18/34] fix(dsh): restore tools after registration owner teardown --- package-lock.json | 41 +++ package.json | 1 + src/agent/dsh-native.ts | 168 ++++++----- tests/dsh-tool-registration-lifetime.test.ts | 288 +++++++++++++++++++ 4 files changed, 434 insertions(+), 64 deletions(-) create mode 100644 tests/dsh-tool-registration-lifetime.test.ts diff --git a/package-lock.json b/package-lock.json index 5800875683..aa5de419bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "bili-proxy": "dist/index.js" }, "devDependencies": { + "@deepseek-ai/cordis": "4.1.0", "@earendil-works/pi-coding-agent": "0.83.0", "@earendil-works/pi-tui": "0.83.0", "@types/node": "^22.0.0", @@ -104,6 +105,39 @@ "node": ">= 14.17.0" } }, + "node_modules/@deepseek-ai/cordis": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@deepseek-ai/cordis/-/cordis-4.1.0.tgz", + "integrity": "sha512-CJrjNyBkKn1SfvBQUcgFYKFnLjS+uKrhdMsbHU60xabvi12d+xQ+IQSNlME5Z+B+4Zi6n4BDknKY94tEPeo47w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@deepseek-ai/cosmokit": "~1.9.0", + "@standard-schema/spec": "^1.1.0" + }, + "bin": { + "cordis": "bin.js" + }, + "peerDependencies": { + "@deepseek-ai/cordis-plugin-include": "~1.1.0", + "@deepseek-ai/cordis-plugin-loader": "~1.1.0" + }, + "peerDependenciesMeta": { + "@deepseek-ai/cordis-plugin-include": { + "optional": true + }, + "@deepseek-ai/cordis-plugin-loader": { + "optional": true + } + } + }, + "node_modules/@deepseek-ai/cosmokit": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@deepseek-ai/cosmokit/-/cosmokit-1.9.0.tgz", + "integrity": "sha512-5O1S53mZiJyZYcvxxD0Qv1EY7qFPSxbGyD8CbVK6gw7W9FvcXZj2YIX1ZkOqyQQ3Xt2rFkZDHJv0+V3Zmmp8CA==", + "dev": true, + "license": "MIT" + }, "node_modules/@earendil-works/pi-coding-agent": { "version": "0.83.0", "resolved": "https://registry.npmjs.org/@earendil-works/pi-coding-agent/-/pi-coding-agent-0.83.0.tgz", @@ -3610,6 +3644,13 @@ "url": "https://github.com/sindresorhus/is?sponsor=1" } }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "dev": true, + "license": "MIT" + }, "node_modules/@tokenizer/inflate": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/@tokenizer/inflate/-/inflate-0.4.1.tgz", diff --git a/package.json b/package.json index ee35baf823..64d8438cea 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "jimp": "1.6.1" }, "devDependencies": { + "@deepseek-ai/cordis": "4.1.0", "@earendil-works/pi-coding-agent": "0.83.0", "@earendil-works/pi-tui": "0.83.0", "@types/node": "^22.0.0", diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index daecb744c2..01e0252ca4 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -149,7 +149,7 @@ type ToolDefinition = { type CommandOutcome = { kind: "success" | "error"; text: string }; export type PluginContext = { - tools: { register: (definition: ToolDefinition) => unknown }; + tools: { register: (definition: ToolDefinition) => unknown; get?: (name: string) => unknown }; commands: { register: (command: { name: string; description: string; handler: (invocation?: CommandInvocation) => Promise }) => unknown }; agents: { currentInitiator?: () => AgentLike | undefined; get?: (id: string) => AgentLike | undefined }; // Runtime-info sources (#955), resolved via dynamic ctx.inject when the @@ -216,18 +216,9 @@ export function planNativeDsh(env: NodeJS.ProcessEnv): { mode: "off" | "attach" const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; -// One registration lifecycle per HOST CONTEXT. toolsReady is the ONLY gate -// for header stamping (pi.ts discipline): stamped headers flip the proxy into -// plugin mode, which suppresses wire tool injection — stamping before the -// local tools exist would send a tool-less request. -// #3380: registrations live in the host process's tool layer and die ONLY -// with their context (the apply() re-arm below is the sole reset). Proxy -// death merely moves the origin — the execute closures read the LIVE base -// (#983) — so proxy-death paths must NOT clear toolsReady: doing so forced a -// doomed re-registration that DSH rejects ("tool X is already registered"), -// locked the latch false forever, and silently stripped every request of its -// identity headers (wire mode → anonymous zero-fold ledgers → 413 on large -// sessions). +// Tool readiness belongs to one activation, including Cordis restarts that +// reuse their context. Proxy death only moves the live base read by execute +// closures (#3380); it does not invalidate surviving host registrations. type RegisterState = { base: string | undefined; toolsReady: boolean; @@ -982,39 +973,109 @@ function fireGiveUp(): void { armRecoveryTimer(); } -async function registerTools(ctx: PluginContext): Promise { - if (register.pending !== undefined) return register.pending; - const base = register.base; - if (register.toolsReady || base === undefined) return; - register.pending = (async () => { - const tools = await fetchManifest(base, "anthropic"); - // #2082: land on the CURRENT context, not the one that started this - // flight — a re-activation mid-fetch must not register into (or die - // on) the deactivated context captured at call time. - const target = activeCtx ?? ctx; +type ToolsOwner = { + ctx: PluginContext; + closed: boolean; + restoring: boolean; + definitions: Map; +}; +let toolsOwner: ToolsOwner | undefined; + +function toolsAreReady(): boolean { + const owner = toolsOwner; + if (owner === undefined) return register.toolsReady; + register.toolsReady = !owner.closed && owner.definitions.size > 0 + && (owner.ctx.tools.get === undefined ? register.toolsReady + : [...owner.definitions.keys()].every((name) => owner.ctx.tools.get?.(name) !== undefined)); + return register.toolsReady; +} + +function restoreTools(owner: ToolsOwner): void { + if (owner !== toolsOwner || owner.closed || owner.restoring) return; + owner.restoring = true; + register.toolsReady = false; + try { let adopted = 0; - for (const tool of tools) { + for (const [name, definition] of owner.definitions) { + if (owner.ctx.tools.get?.(name) !== undefined) { + adopted++; + continue; + } try { - target.tools.register(toolDefinition(tool)); + // The HOST owns the registration's lifetime: DSH's ToolsService + // attaches it to the calling fiber via layers.effect(ctx, …), so + // a restart or dispose removes it and tools/change lets the next + // activation restore (#6702). Proxy death runs no effect — the + // registration survives it (#3380). + owner.ctx.tools.register(definition); } catch (err) { - // #3380: idempotent adoption — a name this process ALREADY - // holds in the host tool layer throws "already registered"; - // that registration is exactly what we want, so skip it and - // register the rest (also converges a partial registration - // left by an earlier interrupted flight). if (!errMessage(err).includes("already registered")) throw err; - adopted += 1; + adopted++; } } if (adopted > 0 && !regAdoptLogged) { regAdoptLogged = true; persistClientEvent(`tool registration adopted ${adopted} pre-existing registration(s) from this process — continuing`); } - register.toolsReady = true; + register.toolsReady = owner.definitions.size > 0; + if (!toolsAreReady()) throw new Error("bili: tool registrations did not become visible"); + } finally { + owner.restoring = false; + } +} + +function beginToolsOwner(ctx: PluginContext): ToolsOwner { + if (ctx.effect === undefined && toolsOwner?.ctx === ctx && !toolsOwner.closed) return toolsOwner; + if (toolsOwner !== undefined) toolsOwner.closed = true; + const owner: ToolsOwner = { ctx, closed: false, restoring: false, definitions: new Map() }; + toolsOwner = owner; + register.toolsReady = false; + register.pending = undefined; + register.retryAt = 0; + ctx.effect?.(() => () => { + owner.closed = true; + if (toolsOwner === owner) { + register.toolsReady = false; + register.pending = undefined; + } + }, "bili: tool registration lifetime"); + ctx.on?.("tools/change", () => { + if (owner !== toolsOwner || owner.closed || owner.restoring || owner.definitions.size === 0) return; + // A real Cordis teardown fires tools/change from the tool disposers + // BEFORE the fiber's own disposers run: restoring while the fiber is + // past its active window (states beyond LOADING=1/ACTIVE=2 — FAILED, + // DISPOSED, UNLOADING) would resurrect registrations that teardown is + // releasing. Hosts without a fiber expose no state and keep restoring. + const fiberState = (owner.ctx as { fiber?: { state?: number } }).fiber?.state; + if (fiberState !== undefined && fiberState !== 1 && fiberState !== 2) return; + try { + restoreTools(owner); + } catch (err) { + register.toolsReady = false; + register.lastRegistrationError = errMessage(err); + armRecoveryTimer(); + } + }); + return owner; +} + +async function registerTools(ctx: PluginContext): Promise { + const owner = toolsOwner; + if (owner === undefined || owner.closed || owner.ctx !== ctx) return; + if (register.pending !== undefined) return register.pending; + const base = register.base; + if (toolsAreReady() || base === undefined) return; + const pending = (async () => { + const tools = await fetchManifest(base, "anthropic"); + if (owner !== toolsOwner || owner.closed || register.base !== base) return; + owner.definitions = new Map(tools.map((tool) => [tool.name, toolDefinition(tool)])); + restoreTools(owner); register.failStreak = 0; register.lastRegistrationError = undefined; })() .catch((err: unknown) => { + if (owner !== toolsOwner || owner.closed) return; + register.toolsReady = false; // Cordis deactivates the plugin context while the host tears down // (dsh --help, early CLI exits): every later register attempt hits // "cannot get required service ... in inactive context" and would @@ -1042,9 +1103,10 @@ async function registerTools(ctx: PluginContext): Promise { persistClientEvent(`manifest registration failed (${errMessage(err)}) — retrying; requests stay in wire mode until it succeeds`); }) .finally(() => { - register.pending = undefined; + if (owner === toolsOwner && register.pending === pending) register.pending = undefined; }); - return register.pending; + register.pending = pending; + return pending; } // #2082: recovery retries are traffic-driven (headersFor on model @@ -1057,13 +1119,7 @@ async function registerTools(ctx: PluginContext): Promise { // ops knob); absent it tracks RETRY_INTERVAL_MS. Tests that do not opt in // never start a timer at all (NODE_TEST_CONTEXT guard keeps suites quiet). let recoveryTimer: NodeJS.Timeout | undefined; -// #2082: the LATEST context seen by apply()/maybeRetry. Every async recovery -// path (the timer tick below, an in-flight registration landing after a -// re-activation) must resolve its context at landing time, not from a capture -// taken when the retry was armed — a captured context may already be -// deactivated by then (dsh reload / profile switch), and registering into it -// throws "inactive context" and would re-poison register.dead, exactly what -// this fix removes. +// #2082: the recovery timer follows the latest activation's context. let activeCtx: PluginContext | undefined; function recoveryIntervalMs(): number { const raw = process.env.BILI_DSH_RECOVERY_INTERVAL_MS; @@ -1091,7 +1147,10 @@ function armRecoveryTimer(): void { } function maybeRetry(ctx: PluginContext): void { + if (toolsOwner === undefined) beginToolsOwner(ctx); + if (toolsOwner?.closed || toolsOwner?.ctx !== ctx) return; activeCtx = ctx; + toolsAreReady(); // #3380: the base-less state heals through the RESPAWN branch below even // when toolsReady is true (registrations survive proxy death) — gating on // toolsReady alone would strand a given-up register with no driver left. @@ -1909,28 +1968,7 @@ export async function apply(ctx: PluginContext): Promise { // (disabling the plugin must not brick session creation, #3662). declareDshCompactionActive(); - // #2082: a fresh activation invalidates the "host tearing down" verdict. - // Without this reset, ONE registerTools rejection carrying "inactive - // context" (a cordis deactivate/reactivate cycle — dsh reloading the - // plugin after the desktop profile copy was swapped in place, a profile - // switch, a settings toggle) poisons register.dead for the whole process - // lifetime: every later maybeRetry() returns immediately and the bili - // tools never come back until dsh restarts. - // #2101: a re-activation ALSO invalidates a SUCCESSFUL registration. - // Registrations die with their context: the tools registered into the - // previous activation are disposed when that context deactivates, but - // register.toolsReady keeps its stale stamp — and every - // registerTools()/maybeRetry() early-returns on it, so the new context - // never gets the bili tools (the #2082 symptom reached from the success - // ordering: a settings toggle, a profile switch or a plugin reload - // AFTER a healthy boot loses the tools until dsh restarts). Clear the - // stale stamp so the retry loop re-registers into the live context; - // retryAt goes with it (#1783: a fresh activation must not stand behind - // a stale back-off wall). - if (register.toolsReady && activeCtx !== undefined && activeCtx !== ctx) { - register.toolsReady = false; - register.retryAt = 0; - } + const owner = beginToolsOwner(ctx); register.dead = false; // #3380: a fresh activation starts a clean registration cycle — stale // failure bookkeeping from the previous context must not carry over @@ -2750,7 +2788,7 @@ export async function apply(ctx: PluginContext): Promise { resolveToolsGate(); } else { void state.ready.then(async (origin) => { - if (origin !== undefined) await registerTools(ctx).catch(() => {}); + if (origin !== undefined && owner === toolsOwner && !owner.closed) await registerTools(ctx).catch(() => {}); resolveToolsGate(); }); } @@ -2919,6 +2957,8 @@ export function _resetRegisterForTest(base: string | undefined, toolsReady = fal recoveryTimer = undefined; } activeCtx = undefined; + if (toolsOwner !== undefined) toolsOwner.closed = true; + toolsOwner = undefined; // #2187: apply() now reaches the respawn arming on every plan, so a stale // single-flight wrapper (capturing a previous test's spawn stub) must not // survive a reset. diff --git a/tests/dsh-tool-registration-lifetime.test.ts b/tests/dsh-tool-registration-lifetime.test.ts new file mode 100644 index 0000000000..a731b8cf70 --- /dev/null +++ b/tests/dsh-tool-registration-lifetime.test.ts @@ -0,0 +1,288 @@ +import test, { afterEach, mock } from "node:test"; +import assert from "node:assert/strict"; +import { + apply, maybeRetryForTest, _resetRegisterForTest, _settleNativeForTest, _nativeStateForTest, + _registerStateForTest, _stateToolsReadyForTest, _stateHeadersForTest, + type PluginContext, +} from "../src/agent/dsh-native.ts"; + +const names = ["compress", "decompress", "search_context", "acp_status", "acp_cache", "bili_status"]; +type Definition = Parameters[0]; +const manifest = (toolNames = names) => Response.json({ tools: { anthropic: toolNames.map((name) => ({ + name, description: name, input_schema: { type: "object", properties: {} }, +})) } }); +const deferred = () => { + let resolve!: () => void; + const promise = new Promise((done) => { resolve = done; }); + return { promise, resolve }; +}; + +function host() { + const definitions = new Map(); + const listeners = new Set<() => void>(); + const changed = () => { for (const listener of listeners) listener(); }; + function owner() { + const disposers: Array<() => void> = []; + const ctx: PluginContext = { + tools: { + get: (name) => definitions.get(name), + register: (definition) => { + if (definitions.has(definition.name)) throw new Error(`tool "${definition.name}" is already registered`); + definitions.set(definition.name, definition); + const dispose = () => { + if (definitions.get(definition.name) !== definition) return; + definitions.delete(definition.name); + changed(); + }; + disposers.push(dispose); + changed(); + return dispose; + }, + }, + commands: { register() {} }, + agents: { currentInitiator: () => ({ session: { id: "lifetime-session" } }) }, + inject() {}, + effect: (effect) => { const dispose = effect(); if (dispose) disposers.push(dispose); }, + on: (event, listener) => { + if (event !== "tools/change") return; + const changedListener = listener as () => void; + listeners.add(changedListener); + disposers.push(() => { listeners.delete(changedListener); }); + }, + }; + return { ctx, dispose: () => { for (const dispose of disposers.splice(0)) dispose(); } }; + } + return { definitions, owner }; +} + +function reset() { + _resetRegisterForTest("http://bili-lifetime.invalid"); + _nativeStateForTest().ready = Promise.resolve("http://bili-lifetime.invalid"); +} + +afterEach(() => { + mock.restoreAll(); + void _settleNativeForTest(); + _resetRegisterForTest(undefined); +}); + +test("same-context restart re-registers all tools and disposal clears readiness", async () => { + const registry = host(); + const owner = registry.owner(); + let requests = 0; + mock.method(globalThis, "fetch", async () => { requests++; return manifest(); }); + reset(); + await apply(owner.ctx); + await _stateToolsReadyForTest(); + assert.deepEqual([...registry.definitions.keys()], names); + owner.dispose(); + assert.equal(_registerStateForTest().toolsReady, false); + assert.equal(registry.definitions.size, 0); + await apply(owner.ctx); + await _stateToolsReadyForTest(); + assert.equal(requests, 2); + assert.deepEqual([...registry.definitions.keys()], names); + assert.equal(_stateHeadersForTest()?.("http://model.invalid/v1/messages")?.["x-bili-plugin"], "dsh"); + owner.dispose(); + maybeRetryForTest(owner.ctx); + assert.equal(registry.definitions.size, 0, "disposed activation must never resurrect its tools"); +}); + +test("borrowed registrations survive new-owner disposal and heal after old-owner disposal", async () => { + const registry = host(); + const oldOwner = registry.owner(); + const newOwner = registry.owner(); + mock.method(globalThis, "fetch", async () => manifest()); + reset(); + await apply(oldOwner.ctx); + await _stateToolsReadyForTest(); + const borrowed = registry.definitions.get("compress"); + await apply(newOwner.ctx); + await _stateToolsReadyForTest(); + assert.equal(registry.definitions.get("compress"), borrowed); + oldOwner.dispose(); + assert.deepEqual([...registry.definitions.keys()].sort(), [...names].sort()); + assert.notEqual(registry.definitions.get("compress"), borrowed); + assert.equal(_registerStateForTest().toolsReady, true); + const foreignOwner = registry.owner(); + const definition = registry.definitions.get("compress"); + assert.ok(definition); + newOwner.dispose(); + foreignOwner.ctx.tools.register(definition); + const borrowingOwner = registry.owner(); + await apply(borrowingOwner.ctx); + await _stateToolsReadyForTest(); + assert.equal(registry.definitions.get("compress"), definition); + borrowingOwner.dispose(); + assert.equal(registry.definitions.get("compress"), definition, "foreign registration keeps its original lifetime"); + foreignOwner.dispose(); + assert.equal(registry.definitions.size, 0); +}); + +test("a late old manifest cannot register tools or clear the new activation's pending flight", async () => { + const registry = host(); + const owner = registry.owner(); + const entered = [deferred(), deferred()]; + const held = [deferred(), deferred()]; + let requests = 0; + mock.method(globalThis, "fetch", async () => { + const index = requests++; + assert.ok(index < 2, "new activation must retain its single-flight latch"); + entered[index].resolve(); + await held[index].promise; + return manifest(index === 0 ? ["stale_tool"] : names); + }); + reset(); + await apply(owner.ctx); + await entered[0].promise; + const oldGate = _stateToolsReadyForTest(); + owner.dispose(); + await apply(owner.ctx); + await entered[1].promise; + held[0].resolve(); + await oldGate; + assert.equal(registry.definitions.size, 0, "old manifest must not pollute the new activation"); + assert.equal(_registerStateForTest().toolsReady, false); + maybeRetryForTest(owner.ctx); + held[1].resolve(); + await _stateToolsReadyForTest(); + assert.equal(requests, 2); + assert.deepEqual([...registry.definitions.keys()], names); + assert.equal(_registerStateForTest().toolsReady, true); + owner.dispose(); +}); + +// #6702: the mock owners above cannot reproduce the failure orderings that +// caused the bug — Cordis restarts reuse one context while its effect-owned +// registrations die — so the final scenario runs against a real fiber with the +// `@deepseek-ai/cordis` dev dependency. TestTools mirrors the real +// ToolsService ownership: extending cordis Service makes `this.ctx` resolve to +// the CALLING plugin's context, so each registration is held by that fiber's +// effect and teardown emits tools/change. +import { Context, Service, type Plugin } from "@deepseek-ai/cordis"; + +class TestTools extends Service { + definitions = new Map(); + + constructor(ctx: Context) { + super(ctx, "tools"); + } + + get(name: string) { + return this.definitions.get(name); + } + + register(definition: Definition) { + if (this.definitions.has(definition.name)) throw new Error(`tool "${definition.name}" is already registered`); + return this.ctx.effect(() => { + this.definitions.set(definition.name, definition); + return () => { + if (this.definitions.get(definition.name) !== definition) return; + this.definitions.delete(definition.name); + this.ctx.events.emit("tools/change"); + }; + }, `bili-test: ${definition.name}`); + } +} + +test("#6702 real Cordis fibers own registration lifetime across restart, adoption and teardown", async () => { + const ctx = new Context(); + const owners: PluginContext[] = []; + let tools: TestTools | undefined; + const liveNames = () => names.filter((name) => tools?.definitions.get(name) !== undefined); + const biliPlugin = { + inject: ["tools", "commands", "agents"], + apply(owner: PluginContext) { + owners.push(owner); + return apply(owner); + }, + }; + try { + await ctx.plugin({ apply(owner) { + owner.provide("commands", { register() {} }); + owner.provide("agents", { currentInitiator: () => ({ session: { id: "lifetime-session" } }) }); + tools = new TestTools(owner); + } }); + const load = (plugin: unknown) => ctx.plugin(plugin as Plugin.Object); + + // Same-context restart: one context, two activations, each owning its + // own registrations — the reused context never inherits the stale + // ready stamp, and the old activation's registrations die with it. + reset(); + let requests = 0; + mock.method(globalThis, "fetch", async () => { requests++; return manifest(); }); + const first = await load(biliPlugin); + await _stateToolsReadyForTest(); + assert.deepEqual(liveNames(), names); + assert.equal(_registerStateForTest().toolsReady, true); + await first.restart(); + await _stateToolsReadyForTest(); + assert.equal(owners[0], owners[1], "a Cordis restart reuses one context"); + assert.equal(requests, 2, "the reused context re-registers instead of trusting the stale latch"); + assert.deepEqual(liveNames(), names); + assert.equal(_registerStateForTest().toolsReady, true); + await first.dispose(); + assert.deepEqual(liveNames(), [], "disposing the fiber removes its own registrations"); + assert.equal(_registerStateForTest().toolsReady, false); + + // A same-named registration held by another fiber keeps its original + // lifetime: adopting it must not transfer ownership to bili, and the + // borrower's disposal must not remove it. + reset(); + const foreignDefinition: Definition = { + name: "compress", + description: "foreign", + parameters: { type: "object", properties: {} }, + output: { schema: {}, render: () => [{ type: "text", text: "foreign" }] }, + execute: async () => "foreign", + }; + const foreign = await ctx.plugin({ inject: ["tools"], apply(owner) { + (owner as Context & { tools: TestTools }).tools.register(foreignDefinition); + } }); + const borrower = await load(biliPlugin); + await _stateToolsReadyForTest(); + assert.deepEqual(liveNames(), names, "the new activation registers around the adopted foreign tool"); + await borrower.dispose(); + assert.deepEqual(liveNames(), ["compress"], "disposing the borrower keeps the foreign registration"); + assert.equal(_registerStateForTest().toolsReady, false); + const replacementBorrower = await load(biliPlugin); + await _stateToolsReadyForTest(); + await foreign.dispose(); + assert.deepEqual(liveNames(), names, "the active borrower restores registrations removed with the old owner"); + assert.notEqual(tools?.get("compress"), foreignDefinition); + await replacementBorrower.dispose(); + assert.deepEqual(liveNames(), [], "the replacement registrations belong to the active borrower"); + + // A late old manifest must not pollute the next activation or clear + // its single-flight latch. + reset(); + const entered = [deferred(), deferred()]; + const held = [deferred(), deferred()]; + let index = 0; + mock.method(globalThis, "fetch", async () => { + const i = index++; + assert.ok(i < 2, "the new activation must retain its single-flight latch"); + entered[i].resolve(); + await held[i].promise; + return manifest(i === 0 ? ["stale_tool"] : names); + }); + const pending = await load(biliPlugin); + await entered[0].promise; + const oldGate = _stateToolsReadyForTest(); + await pending.restart(); + await entered[1].promise; + held[0].resolve(); + await oldGate; + assert.deepEqual(liveNames(), [], "the old manifest must not pollute the new activation"); + assert.equal(_registerStateForTest().toolsReady, false); + maybeRetryForTest(owners.at(-1)!); + held[1].resolve(); + await _stateToolsReadyForTest(); + assert.equal(index, 2); + assert.deepEqual(liveNames(), names); + await pending.dispose(); + assert.deepEqual(liveNames(), [], "final disposal leaves no registrations behind"); + } finally { + await ctx.fiber.dispose(); + } +}); From 730877c4dfbee00a394b22b4626e1ae8489f1802 Mon Sep 17 00:00:00 2001 From: OMSociety <85862362+OMSociety@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:12:17 +0800 Subject: [PATCH 19/34] fix(web): bind color-scheme on the OS-driven dark palette The OS-driven dark block supplied the dark CSS vars but left `color-scheme` at the base `light dark`, which resolves to light when the OS asks for neither scheme. Native control chrome is painted from `color-scheme`, not from the vars, so the compress prompt-pack 's option popup, checkbox/radio glyphs, scrollbars. The base + // :root carries `light dark` (#2473, canvas/ground coupling), but the + // BASE value resolves to `light` whenever the OS asks for neither scheme, + // so each dark palette rebinds it to dark — otherwise the compress + // prompt-pack 's option popup, checkbox/radio glyphs, scrollbars. The base // :root carries `light dark` (#2473, canvas/ground coupling), but the From dd1bd8101df62bebdf4749f665211247db4606a2 Mon Sep 17 00:00:00 2001 From: OMSociety <85862362+OMSociety@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:03:11 +0800 Subject: [PATCH 21/34] fix: pin docs Markdown to LF in .gitattributes (#6692) --- .gitattributes | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitattributes b/.gitattributes index d59bfcb6e5..35d55e752c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -19,3 +19,8 @@ tests/golden/web-hitrate/*.svg text eol=lf # "every fixture artifact matches its recorded digest and size" failed every # windows-latest leg (run 38982246862). tests/fixtures/vertex-anthropic/* text eol=lf + +# #6692: docs/ Markdown is prose rather than byte-exact, but Git-for-Windows' +# default autocrlf=true rewrote whole files to CRLF on commit (a +18-line docs +# PR landed as +1509/-1489). Pin LF so Windows-side edits stay line-accurate. +docs/**/*.md text eol=lf From 42fd2ff7974e6af15afb76f09922b1bf173da077 Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Sat, 14 Nov 2026 12:06:16 +0000 Subject: [PATCH 22/34] fix(stream): keep requested ref numbers in stale-ref compress receipts When every requested ref is above the session frontier, the failure receipt scrubbed the endpoints to a bare [stale-ref], so the caller could not tell which refs it used. Emit a number-preserving marker ([stale-ref:00010]) instead: the range diagnostic stays readable while the bare mNNNNN token never reaches the client-persisted receipt, so the #2146 echo channel stays dead. Fixes #6712 --- src/stream.ts | 18 +++- tests/issue2146-compress-loop-breaker.test.ts | 3 +- ...ue6712-compress-receipt-stale-refs.test.ts | 83 +++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 tests/issue6712-compress-receipt-stale-refs.test.ts diff --git a/src/stream.ts b/src/stream.ts index 53d01fe76d..3c0b6f971a 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -415,6 +415,17 @@ function highestMappedRef(state: Pick): number return highest; } +/** #6712: number-preserving scrub marker for a stale requested ref. `m00010` + * becomes `[stale-ref:00010]` — the digits stay in the receipt so the caller + * can tell which ref it used (and the `range …:` diagnostic line stays + * readable), while the bare `mNNNNN` token never reaches the client-persisted + * receipt, so the #2146 echo channel stays dead. Non-m ids (unreachable when + * `beyond` fires, which requires m-refs) fall back to the bare marker. */ +function staleRefMarker(id: string): string { + const m = /^m(\d+)$/i.exec(id.trim()); + return m ? `[stale-ref:${m[1]}]` : "[stale-ref]"; +} + /** #2146: every requested endpoint sits strictly ABOVE the session's highest * mapped m-ref. Such refs cannot exist in this session (refs are never * pre-allocated); the kernel now classifies that case as above-frontier @@ -1494,10 +1505,15 @@ export function applyRanges(parsed: ReturnType, ctx: // keeps the real refs for the operator. Live refs are untouched: // when the note fires, every requested ref exceeds the session's // highest mapped ref, so no live snapshot number can collide. + // #6712: the scrub must not erase WHICH ref was requested — a bare + // `[stale-ref]` leaves the caller unable to tell what it asked + // for, so it cannot self-correct. The marker keeps the ref number + // (`[stale-ref:00010]`) while never emitting the bare `mNNNNN` + // token, so the #2146 echo channel stays dead. if (beyond !== "") { for (const rg of ranges) { for (const id of [rg.startRef, rg.endRef]) { - receipt = receipt.split(id).join("[stale-ref]"); + receipt = receipt.split(id).join(staleRefMarker(id)); } } } diff --git a/tests/issue2146-compress-loop-breaker.test.ts b/tests/issue2146-compress-loop-breaker.test.ts index 1618a0a14c..ffd18d83f1 100644 --- a/tests/issue2146-compress-loop-breaker.test.ts +++ b/tests/issue2146-compress-loop-breaker.test.ts @@ -188,7 +188,8 @@ test("#2146: stale-generation receipts scrub the phantom refs; in-frontier failu assert.ok(stale.startsWith("[Compression FAILED:"), `unknown refs fail (got: ${stale.slice(0, 120)})`); assert.match(stale, /above this session's highest ref \(m00008\)/, "frontier diagnosis present"); assert.doesNotMatch(stale, /m00010|m00014/, "phantom endpoints are NOT echoed back into the client-persisted receipt"); - assert.match(stale, /\[stale-ref\]/, "scrubbed marker takes their place"); + assert.match(stale, /\[stale-ref:00010\]/, "scrubbed marker takes their place — with the ref number kept (#6712)"); + assert.match(stale, /\[stale-ref:00014\]/, "end ref number kept too (#6712)"); const mixed = applyRanges(parseCompressInput(compressArgs("m00001", "m00009")), ctx).text; assert.ok(mixed.startsWith("[Compressed "), "#4521 flip: an out-frontier endId clamps to the highest allocated ref instead of failing"); diff --git a/tests/issue6712-compress-receipt-stale-refs.test.ts b/tests/issue6712-compress-receipt-stale-refs.test.ts new file mode 100644 index 0000000000..1873fad526 --- /dev/null +++ b/tests/issue6712-compress-receipt-stale-refs.test.ts @@ -0,0 +1,83 @@ +import assert from "node:assert/strict"; +import { randomUUID } from "node:crypto"; +import { test } from "node:test"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; + +_setStoreForTest(new SessionStore({ enabled: false })); + +const SUMMARY = "summary ".repeat(20); + +function textMsg(id: string, role: "user" | "assistant", text: string): CoreMessage { + return { id, role, contentType: "text", text }; +} + +function makeSession(): Session { + return { + id: `issue6712-${randomUUID()}`, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 15000, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }; +} + +function makeCtx(messages: CoreMessage[], config?: Config, logs?: string[]): RewriteCtx & { session: Session; config: Config } { + const session = makeSession(); + const res = assignRefs(messages, { existing: emptyRefMap(), nextIndex: 0 }); + session.state.messageRefs = res.map; + return { core: createCore(), config: config ?? defaultConfig(200000), messages, session, log: logs ? (m) => logs.push(m) : () => {} }; +} + +function compressArgs(startId: string, endId: string) { + return JSON.parse(JSON.stringify({ content: [{ startId, endId, summary: SUMMARY }] })); +} + +function compressArgsMulti(specs: Array<[string, string]>) { + return JSON.parse(JSON.stringify({ content: specs.map(([startId, endId]) => ({ startId, endId, summary: SUMMARY })) })); +} + +// Two sub-gate small messages + six big ones: eight refs, frontier m00008. +function fixtureMessages(): CoreMessage[] { + const msgs: CoreMessage[] = [ + textMsg("raw_1", "assistant", "a".repeat(800)), + textMsg("raw_2", "assistant", "b".repeat(800)), + ]; + for (let i = 3; i <= 8; i++) msgs.push(textMsg(`raw_${i}`, "assistant", "x".repeat(5000))); + return msgs; +} + +test("#6712: all-refs-above-frontier receipt keeps the requested ref numbers", () => { + const ctx = makeCtx(fixtureMessages()); + const out = applyRanges(parseCompressInput(compressArgs("m00010", "m00014")), ctx).text; + assert.ok(out.startsWith("[Compression FAILED:"), `unknown refs fail (got: ${out.slice(0, 120)})`); + assert.match(out, /above this session's highest ref \(m00008\)/, "frontier diagnosis present"); + // The #2146 invariant still holds: no bare phantom ref token reaches the + // client-persisted receipt, so the echo channel stays dead. + assert.doesNotMatch(out, /m00010|m00014/, "phantom endpoints are NOT echoed back as bare refs"); + // ...but the caller can still tell WHICH refs it used: the scrub marker + // carries the ref number, so the range line stays diagnosable. + assert.match(out, /\[stale-ref:00010\]/, "start ref number preserved in the scrub marker"); + assert.match(out, /\[stale-ref:00014\]/, "end ref number preserved in the scrub marker"); + assert.match(out, /range \[stale-ref:00010\]..\[stale-ref:00014\]/, "range diagnostic line names the requested numbers"); +}); + +test("#6712: every range in a multi-range request keeps its numbers", () => { + const ctx = makeCtx(fixtureMessages()); + const out = applyRanges(parseCompressInput(compressArgsMulti([["m00010", "m00011"], ["m00012", "m00014"]])), ctx).text; + assert.ok(out.startsWith("[Compression FAILED:"), `unknown refs fail (got: ${out.slice(0, 120)})`); + assert.doesNotMatch(out, /m00010|m00011|m00012|m00014/, "no bare phantom ref token anywhere"); + for (const n of ["00010", "00011", "00012", "00014"]) { + assert.match(out, new RegExp(`\\[stale-ref:${n}\\]`), `ref number ${n} preserved`); + } +}); From 2561732211e0c9939cb241691c681414b157aecc Mon Sep 17 00:00:00 2001 From: ework-daemon Date: Sat, 14 Nov 2026 20:32:54 +0800 Subject: [PATCH 23/34] style(tests): move cordis import to top of file Minor style nit from review: the @deepseek-ai/cordis import was placed mid-file (line 162, after a test). ESM hoisting means no behavior change; moving it to the top follows the repo convention of imports-first. --- tests/dsh-tool-registration-lifetime.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/dsh-tool-registration-lifetime.test.ts b/tests/dsh-tool-registration-lifetime.test.ts index a731b8cf70..d7b0bc71b2 100644 --- a/tests/dsh-tool-registration-lifetime.test.ts +++ b/tests/dsh-tool-registration-lifetime.test.ts @@ -1,5 +1,6 @@ import test, { afterEach, mock } from "node:test"; import assert from "node:assert/strict"; +import { Context, Service, type Plugin } from "@deepseek-ai/cordis"; import { apply, maybeRetryForTest, _resetRegisterForTest, _settleNativeForTest, _nativeStateForTest, _registerStateForTest, _stateToolsReadyForTest, _stateHeadersForTest, @@ -157,9 +158,8 @@ test("a late old manifest cannot register tools or clear the new activation's pe // registrations die — so the final scenario runs against a real fiber with the // `@deepseek-ai/cordis` dev dependency. TestTools mirrors the real // ToolsService ownership: extending cordis Service makes `this.ctx` resolve to -// the CALLING plugin's context, so each registration is held by that fiber's -// effect and teardown emits tools/change. -import { Context, Service, type Plugin } from "@deepseek-ai/cordis"; + // the CALLING plugin's context, so each registration is held by that fiber's + // effect and teardown emits tools/change. class TestTools extends Service { definitions = new Map(); From 983b27271386b08af282ced17e6370a1148abf2f Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 14 Nov 2026 21:11:15 +0800 Subject: [PATCH 24/34] fix: cite the correct issue (#6587, not #6588) for the color-scheme fix --- src/web/styles.ts | 2 +- tests/dsh-settings-ui.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/web/styles.ts b/src/web/styles.ts index 409a9333ca..183c8068ad 100644 --- a/src/web/styles.ts +++ b/src/web/styles.ts @@ -58,7 +58,7 @@ export const WEB_STYLES = String.raw` signal, so the media query below is narrowed to documents WITHOUT one; the pinned palette repeats the same list (CSS cannot share declarations across media boundaries — keep the two lists in sync). - #6588: each dark palette declares color-scheme: dark. The UA paints native + #6587: each dark palette declares color-scheme: dark. The UA paints native control chrome (a select's option popup, checkbox/radio glyphs, scrollbars) from color-scheme rather than from the CSS vars, so the declaration keeps those controls on the dark ground the palette establishes. */ diff --git a/tests/dsh-settings-ui.test.ts b/tests/dsh-settings-ui.test.ts index c73c4ef180..6c8ac47268 100644 --- a/tests/dsh-settings-ui.test.ts +++ b/tests/dsh-settings-ui.test.ts @@ -2261,7 +2261,7 @@ test("#4406: the Context tab marks the conversation scroll container and hides t assert.equal(scrollAttrs.has("data-bili-surface"), false, "leaving the tab drops the marker"); }); -test("#6588: both dark palettes bind color-scheme so native controls follow the theme", () => { +test("#6587: both dark palettes bind color-scheme so native controls follow the theme", () => { // `color-scheme` is what the UA paints native control chrome from: a //