diff --git a/CLIENTS.md b/CLIENTS.md index bad598dec2..430df1b625 100644 --- a/CLIENTS.md +++ b/CLIENTS.md @@ -65,9 +65,26 @@ Two lanes, same plugin (#941): for conversation …` warning, and the dsh plugin logs each distinct endpoint the attribution gate lets through unproxied (once per process). Reliable workaround meanwhile: launch through `bili dsh` instead — the launcher's - settings overlay rewrites those providers' `baseURL`s to `/bili/` URLs, so - the traffic reaches the proxy regardless of which fetch the transport uses - or what the attribution state is. + settings overlay rewrites those providers' `baseURL`s to `/bili/` URLs, so + the traffic reaches the proxy regardless of which fetch the transport uses + or what the attribution state is. +- **Interop note — retry on malformed tool args (#1518):** in plugin mode the + model generates `compress` arguments and dsh parses them strictly. On long + free-text summaries the model occasionally emits JSON with escape errors — + unescaped `"` inside prose, illegal escapes (e.g. `` \` ``), raw newlines + or tabs inside string literals. That class is *semantic* (telling which + quotes are structural vs content requires understanding the text), so no + syntactic salvage ladder can repair it, and bili passes plugin-mode + responses through verbatim — it has no seat at the parsing table. When dsh + rejects such a call (`MALFORMED_RESPONSE`) the whole turn dies unless it is + retried. Host-side mitigation: add `MALFORMED_RESPONSE` to + `retryPolicy.retryableCodes` — escape errors are sampling noise, so one + extra full call usually succeeds (one field report: ~7% of compress calls + failed this way over 285 attempts, while all other tool calls had zero + failures). The root fix — provider-side constrained decoding (e.g. + DeepSeek's Beta `tools[].function.strict`, Chat Completions wire only) — is + a host/provider work item; bili cannot enable it in plugin mode because it + does not construct the provider request. Under a `bili dsh` launch the plugin ATTACHES to the launcher's proxy (no second spawn). Raw upstream URLs rewrite to `/bili/` like diff --git a/CLIENTS.zh-CN.md b/CLIENTS.zh-CN.md index 8f41e72afc..9a0c048164 100644 --- a/CLIENTS.zh-CN.md +++ b/CLIENTS.zh-CN.md @@ -13,6 +13,7 @@ - **Profile 安装(免启动器)——统一泳道(#966):** `bili plugin install dsh` 对每个已存在的 profile 执行 `dsh plugin --profile add billion-context` —— pnpm 把包装进各 profile 自己的 `node_modules`,dsh 自动挂载包内 patch 层(`dsh.bundle.patch.yml`)。装哪个源取决于 bili 自身的安装形态(#925):npm 安装传注册表名,checkout/dev 构建传绝对路径(`link:` 依赖,本地改动实时生效)。旧版受管块(`# bili begin` / `# bili end`,#966 之前的安装所写)在安装与卸载时都会被剥离 —— 用户条目与注释保留,清空的文件还原占位 `[]`。先在每个 profile 里跑过一次 dsh 让目录存在。插件加载时自拉起代理(已有健康实例则附看,不重复拉;父进程 pid 看门狗),经全局 fetch 补丁把模型流量改写为 `/bili/<上游URL>`,原样注册清单工具,并按工具就绪门控 plugin 模式头(第一轮走 wire 模式)。退出开关:`BILI_NATIVE_DSH=0`。卸载:`bili plugin remove dsh` 或 `dsh plugin --profile remove billion-context` —— 两者走同一通道。经注册表安装要求 npm 上已发布含 `dsh.bundle.patch.yml` 的版本。若 add 后 dsh 启动即报 `billion-context/dsh` 的 `ERR_MODULE_NOT_FOUND`,说明 profile 从陈旧的包元数据缓存里解析到了不含 bundle 子路径导出的旧版本(#953)——固定版本重装:`dsh plugin --profile add billion-context@latest`。 - **自动更新保持各 profile 同步:** 刷新有两个触发器 —— 全局自更新完成后,以及**profile 自己的代理**在周期检查里发现注册表有新版本时(全局 bili 从不运行也一样刷新,#1196 —— 插件市场安装的用户往往根本没有全局安装)。两种触发器都扫描 `~/.dsh/profiles/*/package.json`,把注册表钉住的 `billion-context` 依赖刷新到目标版本(全局触发器刷到新全局版本,自触发刷到注册表最新),统一经 dsh 自己的 `plugin add` 通道,绝不在位覆盖 —— 加载的插件与代理从此不再漂移(#953);钉在本地源的 profile 不动。刷新是尽力而为,失败下个周期重试,绝不会让代理或更新本身失败。 - **已报告:Profile 安装下部分传输零代理流量(#1158,调查中):** dsh `llm-pi-ai` 层的部分传输服务会话**从未有任何模型请求到达代理**(日志无 `processTurn`,调用 bili 工具返回 404 "no model request has arrived"),而同宿主的其他 provider 正常。根因仍在用运行时证据定性 —— 候选:传输层 fetch 形态(SDK 注入 fetch / 非全局 dispatcher)或宿主侧归属缺口导致流量未被 takeover gate 认领。检测:此类情况会打一次性 `[plugin] NO MODEL REQUESTS seen for conversation …` 告警,dsh 插件还会把归属 gate 放行的每个端点各记一条日志(每进程一次)。期间可靠规避:改用 `bili dsh` 启动 —— 启动器的 settings overlay 会把那些 provider 的 `baseURL` 重写为 `/bili/` URL,无论传输层使用哪种 fetch、归属状态如何,流量都必然过代理。 +- **互操作注记 —— 畸形工具参数应可重试(#1518):** plugin 模式下 `compress` 参数由模型生成、dsh 严格解析。长自由文本摘要中,模型偶发写出带转义错误的 JSON —— 行文里未转义的 `"`、非法转义(如 `` \` ``)、字符串字面量内的裸换行/TAB。这一类是*语义性*的(判断哪个引号是结构、哪个是内容需要理解文本),任何语法层 salvage 阶梯都救不回;而 bili 对 plugin 模式响应逐字节透传,不在解析桌上。dsh 拒绝此类调用(`MALFORMED_RESPONSE`)时,除非重试,整个 turn 终结。宿主侧缓解:把 `MALFORMED_RESPONSE` 加入 `retryPolicy.retryableCodes` —— 转义错误是采样噪声,一次额外完整调用通常即过(单例现场报告:285 次 compress 尝试中约 7% 以此方式失败,其余工具调用零失败)。根治手段 —— provider 侧约束解码(如 DeepSeek Beta 的 `tools[].function.strict`,仅 Chat Completions wire)—— 属宿主/provider 工作项;bili 在 plugin 模式下不构造 provider 请求,无法代开。 `bili dsh` 启动下插件**附看**(attach)启动器的代理(不二次拉起)。裸上游 URL 与 spawn 模式一样重写为 `/bili/`(回环代理目标永不被代理 env 拦截,等于直接绕开 MITM);已经路由的 `/bili/` 前缀请求原样放行、只盖章。已知局限:手动 `/compact` 没有 dsh 侧事件钩子,其边界交给内核的自然 ingest diff(自动压缩已关,影响罕见)。 diff --git a/src/plugin.ts b/src/plugin.ts index 649fda6aee..02e4720dae 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1172,6 +1172,50 @@ const CLEAN_TURN_REASONS = new Set(["stop", "end_turn", "stop_sequence"]); const ANTHROPIC_BLOCK_EVENT = /^content_block_(start|delta|stop)$/; +/** #1518 stage 1: post-hoc strict-JSON check of model-emitted tool-call + * arguments on the VERBATIM plugin lane. In plugin mode the host parses these + * strings strictly at its terminal (DSH: message_stop) and a malformed one + * kills the whole turn (MALFORMED_RESPONSE); bili forwards them untouched + * (#1039) and has no seat at the parsing table, so this observe-only pass + * records what the host is about to reject — tool name, V8 error class, + * length, error position — the telemetry that previously required reconstructing + * argument deltas by hand out of zstd-framed session logs (issue #1518). + * Forwarding bytes are never altered; the proxy's own salvage ladder + * (#603/#1508) stays off this lane by design. Gated on a bound session: the + * proxy-mode #460 lanes deliberately pass none, keeping their diagnostics in + * the ladder's own hands. Structured-object args (anthropic `input`, gemini + * `args`) are parse-valid by construction and never reach this. */ +function noteMalformedToolArgs( + session: Session | undefined, + protocol: string, + calls: ReadonlyArray<{ name: string; args: string }>, + log?: (msg: string) => void, +): void { + if (!session) return; + for (const c of calls) { + if (c.name.length === 0 || c.args.trim().length === 0) continue; + let errMessage = ""; + try { + JSON.parse(c.args); + } catch (e) { + errMessage = e instanceof Error ? e.message : String(e); + } + if (!errMessage) continue; + // V8 appends "in JSON at position N" + "(line L column C)" and, for + // short inputs, an embedded snippet of the offending text — strip all + // three so the error CLASS is the stable part of the line and no + // payload content is ever echoed into the log. + const pos = /position (\d+)/.exec(errMessage)?.[1]; + const cls = errMessage + .replace(/\s*in JSON at position \d+/, "") + .replace(/\s*\(line \d+ column \d+\)$/, "") + .replace(/,\s*".*" is not valid JSON$/s, ""); + const msg = `[plugin] [${session.id}] malformed tool-call args forwarded verbatim (${protocol}, tool=${c.name}, len=${c.args.length}${pos !== undefined ? `, err@${pos}` : ""}): ${cls} (#1518 observe-only)`; + loggerLog("warn", msg); + log?.(msg); + } +} + /** Plugin-mode streaming passthrough for the OpenAI chat-completions and * Anthropic wires: forward upstream events byte-identical (the agent's * native tool loop must see the model's tool calls untouched) while (a) @@ -1289,7 +1333,7 @@ export async function pipePluginChatWithStrip( // settles a once-per-response warn when upstream emits a call whose name // never arrives (#1484 class), so the observed rate can settle the // drop-vs-keep policy without touching fidelity. - const seenToolCalls = new Map(); + const seenToolCalls = new Map(); // Degenerate-turn retry (#732/#821 for this pipe). The first attempt's // terminal event is dropped when the retry takes over, so the client sees // one turn: its framing stays open, and the retry's content blocks are @@ -1523,13 +1567,18 @@ export async function pipePluginChatWithStrip( const t = tcf as Record; const tIdx = typeof t["index"] === "number" ? t["index"] : ti; const key = `${ci}:${tIdx}`; - const accTc = seenToolCalls.get(key) ?? { label: `idx=${tIdx}`, id: "", name: "", argsLen: 0, frags: 0 }; + const accTc = seenToolCalls.get(key) ?? { label: `idx=${tIdx}`, id: "", name: "", argsLen: 0, args: "", frags: 0 }; if (typeof t["id"] === "string" && t["id"]) accTc.id = t["id"]; const fn = t["function"]; if (fn && typeof fn === "object") { const f = fn as Record; if (typeof f["name"] === "string") accTc.name += f["name"]; - if (typeof f["arguments"] === "string") accTc.argsLen += f["arguments"].length; + if (typeof f["arguments"] === "string") { + // #1518 stage 1: keep the raw string itself so the + // stream-end pass can strict-parse it (#1501 kept length only). + accTc.argsLen += f["arguments"].length; + accTc.args += f["arguments"]; + } } accTc.frags++; seenToolCalls.set(key, accTc); @@ -1627,6 +1676,7 @@ export async function pipePluginChatWithStrip( id: cb && typeof cb["id"] === "string" ? cb["id"] : "", name: cb && typeof cb["name"] === "string" ? cb["name"] : "", argsLen: 0, + args: "", frags: 1, }); } else if (bt === "thinking" || bt === "redacted_thinking") sawThinking = true; @@ -1641,7 +1691,10 @@ export async function pipePluginChatWithStrip( // #1039 — argument bytes are user intent, forwarded verbatim. if (d?.["type"] === "input_json_delta") { const accTc = seenToolCalls.get(`block:${index}`); - if (accTc && typeof d["partial_json"] === "string") accTc.argsLen += d["partial_json"].length; + if (accTc && typeof d["partial_json"] === "string") { + accTc.argsLen += d["partial_json"].length; + accTc.args += d["partial_json"]; + } } const field = d?.["type"] === "thinking_delta" ? "thinking" : d?.["type"] === "text_delta" ? "text" : null; if (field === null || typeof d?.[field] !== "string") { @@ -1717,11 +1770,14 @@ export async function pipePluginChatWithStrip( // Gemini delivers functionCall whole in one part, so a missing // name is final here (#1501). Observe-only — bytes untouched. const fcObj = p["functionCall"] && typeof p["functionCall"] === "object" ? p["functionCall"] as Record : undefined; + // Gemini delivers args as a structured object (parse-valid by + // construction) — no argument string to check (#1518). seenToolCalls.set(`cand:${ci}/part:${pi}`, { label: `candidate=${ci}/part=${pi}`, id: "", name: fcObj && typeof fcObj["name"] === "string" ? fcObj["name"] : "", argsLen: fcObj && fcObj["args"] !== null && typeof fcObj["args"] === "object" ? JSON.stringify(fcObj["args"]).length : 0, + args: "", frags: 1, }); } @@ -1897,6 +1953,7 @@ export async function pipePluginChatWithStrip( maybeWarnDegenerate(); maybeWarnProtocolFragment(); maybeWarnNamelessToolCalls(); + noteMalformedToolArgs(session, protocol, [...seenToolCalls.values()].map((tc) => ({ name: tc.name, args: tc.args })), log); if (truncated) { emitUpstreamTruncation(res, protocol, finalFinishReason !== undefined, log); return; @@ -2024,6 +2081,9 @@ export async function pipePluginResponsesWithStrip( ); // #673: turn-level observability for degenerate terminal turns. let sawFunctionCall = false; + // #1518 stage 1: final argument string per function_call item — the host + // parses these strictly at completion; record what it would reject. + const respToolArgs = new Map(); let sawReasoning = false; /** The model emitted markup the filter stripped (or would strip): proof the * turn produced output, even when none survived to be visible. */ @@ -2284,6 +2344,15 @@ export async function pipePluginResponsesWithStrip( const item = ev["item"] as Record | undefined; const it = item?.["type"]; if (it === "function_call" || it === "custom_tool_call") sawFunctionCall = true; + // #1518 stage 1: added announces the name (arguments + // still ""); done carries the final arguments string. + if (it === "function_call" && item) { + const key = typeof item["id"] === "string" ? item["id"] : String(ev["output_index"] ?? 0); + const e = respToolArgs.get(key) ?? { name: "", args: "" }; + if (typeof item["name"] === "string") e.name = item["name"]; + if (typeof item["arguments"] === "string" && item["arguments"].length > 0) e.args = item["arguments"]; + respToolArgs.set(key, e); + } } const resp = ev["response"] as Record | undefined; if (resp && typeof resp["status"] === "string") responseStatus = resp["status"] as string; @@ -2302,9 +2371,25 @@ export async function pipePluginResponsesWithStrip( // function_call_arguments.done carries tool arguments, not // visible text: forwarded verbatim (#1039). if (type === "response.function_call_arguments.done") { + // #1518 stage 1: the done event's arguments string is the + // authoritative final value for its item. + const key = typeof ev["item_id"] === "string" ? ev["item_id"] : String(ev["output_index"] ?? 0); + const e = respToolArgs.get(key) ?? { name: "", args: "" }; + if (typeof ev["arguments"] === "string") e.args = ev["arguments"]; + respToolArgs.set(key, e); await write(flushArgTails() + rawEvent + "\n\n"); continue; } + // #1518 stage 1: accumulate argument deltas per item — bytes + // forward exactly as today (the generic write did that before). + if (type === "response.function_call_arguments.delta" && typeof ev["delta"] === "string") { + const key = typeof ev["item_id"] === "string" ? ev["item_id"] : String(ev["output_index"] ?? 0); + const e = respToolArgs.get(key) ?? { name: "", args: "" }; + e.args += ev["delta"]; + respToolArgs.set(key, e); + await write(rawEvent + "\n\n"); + continue; + } // #933: done-family events also carry full text payloads — strip those too. // The done is not visible text to the degenerate-turn retry below, so it // is stripped and released directly. @@ -2359,6 +2444,7 @@ export async function pipePluginResponsesWithStrip( // call would have blocked the retry via sawFunctionCall. for (const s of argStreams.values()) s.filter.flush(); argStreams.clear(); + respToolArgs.clear(); heldEvents = []; heldVisibleChars = 0; continue; @@ -2415,7 +2501,8 @@ export async function pipePluginResponsesWithStrip( } // Reasoning summary deltas carry visible prose; function_call // argument deltas are user intent and pass through verbatim - // (#1039), falling into the generic rawEvent write below. + // (#1039) — their own branch above accumulates them for the + // #1518 observe-only pass and writes them byte-identical. const argField = type === "response.reasoning_summary_text.delta" ? "delta" : null; if (argField !== null && typeof type === "string") { const v = ev[argField]; @@ -2452,6 +2539,7 @@ export async function pipePluginResponsesWithStrip( } maybeWarnDegenerate(); maybeWarnProtocolFragment(); + noteMalformedToolArgs(session, "responses", [...respToolArgs.values()], log); settleUsage(); // #721: same as the chat-pipe twin — never close bare on a missing // done-family event. Responses has no separate finish-reason concept @@ -2615,6 +2703,48 @@ export async function pipePluginJson( } // #1595: parsed success body carrying no input usage report — name it. if (session && !sawInputSample) diagnoseSuccessWithoutUsage(session, "plugin-json"); + // #1518 stage 1: the non-streaming twin — string-carried tool arguments + // (openai chat.completion, responses function_call) get the same post-hoc + // strict check; structured-object args are parse-valid by construction. + if (session) { + const calls: Array<{ name: string; args: string }> = []; + if (protocol === "responses") { + const output = json["output"]; + if (Array.isArray(output)) { + for (const item of output) { + if (!item || typeof item !== "object") continue; + const o = item as Record; + if (o["type"] !== "function_call") continue; + calls.push({ + name: typeof o["name"] === "string" ? o["name"] : "", + args: typeof o["arguments"] === "string" ? o["arguments"] : "", + }); + } + } + } else { + const choices = json["choices"]; + if (Array.isArray(choices)) { + for (const ch of choices) { + if (!ch || typeof ch !== "object") continue; + const msg = (ch as Record)["message"]; + if (!msg || typeof msg !== "object") continue; + const arr = (msg as Record)["tool_calls"]; + if (!Array.isArray(arr)) continue; + for (const tc of arr) { + if (!tc || typeof tc !== "object") continue; + const fn = (tc as Record)["function"]; + if (!fn || typeof fn !== "object") continue; + const fnRec = fn as Record; + calls.push({ + name: typeof fnRec["name"] === "string" ? fnRec["name"] : "", + args: typeof fnRec["arguments"] === "string" ? fnRec["arguments"] : "", + }); + } + } + } + } + noteMalformedToolArgs(session, protocol ?? "unknown", calls); + } } catch { /* non-JSON body — forward verbatim */ } if (json && (containsRenderTagText(text) || containsMarkerLineText(text) || containsBiliInternalText(text))) { // #206 parity for the non-streaming plugin path: the compress loop's diff --git a/tests/issue1086-chain-self-exemption.test.ts b/tests/issue1086-chain-self-exemption.test.ts index d0de78367e..dac34c8546 100644 --- a/tests/issue1086-chain-self-exemption.test.ts +++ b/tests/issue1086-chain-self-exemption.test.ts @@ -725,7 +725,7 @@ test("#1101 T9: BILI_CHAIN_CONTENT env parse — default OFF, 1 enables, env win assert.equal(loadOptions({ BILI_CHAIN_CONTENT: "0" }).chainContentDetection, false, "env =0 wins over file true"); } finally { if (prevFile === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = prevFile; - rmSync(root, { recursive: true, force: true }); + rmrf(root); } }); diff --git a/tests/malformed-toolcall-args-observe.test.ts b/tests/malformed-toolcall-args-observe.test.ts new file mode 100644 index 0000000000..a3c5edbe56 --- /dev/null +++ b/tests/malformed-toolcall-args-observe.test.ts @@ -0,0 +1,272 @@ +// #1518 stage 1: model-emitted tool-call arguments on the VERBATIM plugin lane +// are host-parsed strictly at the host's terminal (DSH: message_stop); a +// malformed one kills the whole turn while bili has already forwarded the bytes +// untouched (#1039). These pins lock the observe-only pass: exactly one warn per +// bad argument carrying tool name / V8 error class / length / position, zero +// forwarding-byte changes, proxy-mode lanes (no bound session) stay silent, and +// well-formed traffic produces nothing. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Session } from "../src/session.js"; +import { pipePluginChatWithStrip, pipePluginResponsesWithStrip, pipePluginJson } from "../src/plugin.js"; +import { setLogCapture } from "../src/logger.js"; + +function makeSession(): Session { + return { + id: "testsess", + protocol: "openai", + upstreamOrigin: "http://127.0.0.1:9/v1", + label: "test", + createdAt: 0, + lastUsedAt: 0, + requests: 0, + lastInputTokens: 0, + stats: {}, + dirty: false, + } as unknown as Session; +} + +function makeRes(chunks: string[]) { + return { + writes: chunks, + write(b: Buffer | string) { + chunks.push(typeof b === "string" ? b : b.toString("utf8")); + return true; + }, + end(b?: Buffer | string) { + if (b !== undefined) chunks.push(typeof b === "string" ? b : b.toString("utf8")); + }, + once() {}, + destroyed: false, + writableEnded: false, + } as unknown as import("node:http").ServerResponse; +} + +function streamOf(events: string[]): ReadableStream { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream({ + pull(controller) { + if (i < events.length) { + controller.enqueue(enc.encode(events[i])); + i += 1; + } else { + controller.close(); + } + }, + }); +} + +function chatChunk(delta: Record, extra: Record = {}): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: null }], ...extra })}\n\n`; +} + +const DONE = "data: [DONE]\n\n"; + +function sseLf(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +// The logged class must equal V8's own parse-error class with the unstable +// suffixes stripped — the same taxonomy the issue reporter computed on 21 +// production failures. Recomputed here so the pin holds across Node versions. +function v8Class(payload: string): string { + let msg = ""; + try { + JSON.parse(payload); + } catch (e) { + msg = e instanceof Error ? e.message : String(e); + } + assert.ok(msg, "test bug: payload must be malformed"); + return msg + .replace(/\s*in JSON at position \d+/, "") + .replace(/\s*\(line \d+ column \d+\)$/, "") + .replace(/,\s*".*" is not valid JSON$/s, ""); +} + +const QUOTE_ARGS = '{"topic":"summary","content":[{"startId":"m00001","endId":"m00002","summary":"he said "hi" and left"}]}'; +const ESCAPE_ARGS = '{"note":"escape demo: \\d and \\w"}'; + +function captureWarns(fn: () => Promise): Promise { + const warns: string[] = []; + setLogCapture((level, msg) => { + if (level === "warn") warns.push(msg); + }); + const done = fn().finally(() => setLogCapture(null)); + return done.then(() => warns); +} + +test("#1518 chat openai: malformed tool-call args reach client verbatim + exactly one warn", async () => { + const out: string[] = []; + const res = makeRes(out); + const events = [ + chatChunk({ role: "assistant" }), + chatChunk({ tool_calls: [{ index: 0, id: "call_c1", type: "function", function: { name: "compress", arguments: QUOTE_ARGS.slice(0, 40) } }] }), + chatChunk({ tool_calls: [{ index: 0, function: { arguments: QUOTE_ARGS.slice(40) } }] }), + chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }), + DONE, + ]; + const warns = await captureWarns(() => pipePluginChatWithStrip(streamOf(events), res, "openai", makeSession())); + const text = out.join(""); + assert.ok(text.includes("call_c1"), "upstream call-id bytes reach the client untouched"); + assert.ok(text.includes(JSON.stringify(QUOTE_ARGS.slice(0, 40))), "first argument fragment passes through verbatim"); + assert.ok(text.includes(JSON.stringify(QUOTE_ARGS.slice(40))), "second argument fragment passes through verbatim"); + const w = warns.filter((m) => m.includes("malformed tool-call args")); + assert.equal(w.length, 1, "exactly one warn per bad argument"); + assert.ok(w[0].includes("[testsess]"), "warn carries the session id"); + assert.ok(w[0].includes("openai"), "warn carries the protocol"); + assert.ok(w[0].includes("tool=compress"), "warn carries the tool name"); + assert.ok(w[0].includes(`len=${QUOTE_ARGS.length}`), "warn carries the argument byte count"); + assert.ok(w[0].includes(v8Class(QUOTE_ARGS)), "warn carries V8's native error class"); +}); + +test("#1518 chat openai: well-formed tool-call args produce no warn (negative control)", async () => { + const out: string[] = []; + const res = makeRes(out); + const good = '{"c":1}'; + const events = [ + chatChunk({ role: "assistant" }), + chatChunk({ tool_calls: [{ index: 0, id: "call_ok", type: "function", function: { name: "get_weather", arguments: good } }] }), + chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }), + DONE, + ]; + const warns = await captureWarns(() => pipePluginChatWithStrip(streamOf(events), res, "openai", makeSession())); + assert.ok(out.join("").includes("get_weather"), "named call passes through verbatim"); + assert.equal(warns.filter((m) => m.includes("malformed tool-call args")).length, 0, "no warn for well-formed traffic"); +}); + +test("#1518 chat openai: empty argument string is skipped, not reported", async () => { + const out: string[] = []; + const res = makeRes(out); + const events = [ + chatChunk({ role: "assistant" }), + chatChunk({ tool_calls: [{ index: 0, id: "call_e", type: "function", function: { name: "noop", arguments: "" } }] }), + chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }), + DONE, + ]; + const warns = await captureWarns(() => pipePluginChatWithStrip(streamOf(events), res, "openai", makeSession())); + assert.equal(warns.filter((m) => m.includes("malformed tool-call args")).length, 0, "empty args never reach a host parser"); +}); + +test("#1518 chat anthropic: malformed input_json_delta args reach client verbatim + exactly one warn", async () => { + const out: string[] = []; + const res = makeRes(out); + const events = [ + sseLf("message_start", { type: "message_start", message: { id: "msg_1", role: "assistant" } }), + sseLf("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_c1", name: "compress" } }), + sseLf("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: ESCAPE_ARGS.slice(0, 20) } }), + sseLf("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: ESCAPE_ARGS.slice(20) } }), + sseLf("content_block_stop", { type: "content_block_stop", index: 0 }), + sseLf("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use" } }), + sseLf("message_stop", { type: "message_stop" }), + ]; + const warns = await captureWarns(() => pipePluginChatWithStrip(streamOf(events), res, "anthropic", makeSession())); + const text = out.join(""); + assert.ok(text.includes("toolu_c1"), "upstream block bytes reach the client untouched"); + assert.ok(text.includes(JSON.stringify(ESCAPE_ARGS.slice(0, 20))), "first partial_json fragment passes through verbatim"); + const w = warns.filter((m) => m.includes("malformed tool-call args")); + assert.equal(w.length, 1, "exactly one warn per bad argument"); + assert.ok(w[0].includes("anthropic"), "warn carries the protocol"); + assert.ok(w[0].includes("tool=compress"), "warn carries the tool name"); + assert.ok(w[0].includes(`len=${ESCAPE_ARGS.length}`), "warn carries the argument byte count"); + assert.ok(w[0].includes(v8Class(ESCAPE_ARGS)), "warn carries V8's native error class (bad escaped character)"); +}); + +test("#1518 chat google: structured-object args never flagged (negative control)", async () => { + const out: string[] = []; + const res = makeRes(out); + const events = [ + `data: ${JSON.stringify({ candidates: [{ index: 0, content: { role: "model", parts: [{ functionCall: { name: "compress", args: { content: ["字"] } } }] } }] })}\n\n`, + DONE, + ]; + const warns = await captureWarns(() => pipePluginChatWithStrip(streamOf(events), res, "google", makeSession())); + assert.ok(out.join("").includes("functionCall"), "upstream part reaches the client untouched"); + assert.equal(warns.filter((m) => m.includes("malformed tool-call args")).length, 0, "structured args are parse-valid by construction"); +}); + +test("#1518 responses: malformed function_call_arguments reach client verbatim + exactly one warn", async () => { + const out: string[] = []; + const res = makeRes(out); + const frag = QUOTE_ARGS.slice(0, 30); + const events = [ + sseLf("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { type: "function_call", id: "fc_m1", call_id: "call_m1", name: "compress", arguments: "" } }), + sseLf("response.function_call_arguments.delta", { type: "response.function_call_arguments.delta", item_id: "fc_m1", output_index: 0, delta: frag }), + sseLf("response.function_call_arguments.done", { type: "response.function_call_arguments.done", item_id: "fc_m1", output_index: 0, arguments: QUOTE_ARGS }), + sseLf("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { type: "function_call", id: "fc_m1", call_id: "call_m1", name: "compress", arguments: QUOTE_ARGS } }), + sseLf("response.completed", { type: "response.completed", response: { id: "resp_1", status: "completed", output: [] } }), + ]; + const warns = await captureWarns(() => pipePluginResponsesWithStrip(streamOf(events), res, makeSession())); + const text = out.join(""); + assert.ok(text.includes("fc_m1"), "item bytes reach the client untouched"); + assert.ok(text.includes(JSON.stringify(frag)), "argument delta passes through verbatim"); + const w = warns.filter((m) => m.includes("malformed tool-call args")); + assert.equal(w.length, 1, "exactly one warn per bad argument"); + assert.ok(w[0].includes("responses"), "warn carries the protocol"); + assert.ok(w[0].includes("tool=compress"), "warn carries the tool name"); + assert.ok(w[0].includes(`len=${QUOTE_ARGS.length}`), "warn carries the final argument byte count"); + assert.ok(w[0].includes(v8Class(QUOTE_ARGS)), "warn carries V8's native error class"); +}); + +test("#1518 responses: well-formed function_call produces no warn (negative control)", async () => { + const out: string[] = []; + const res = makeRes(out); + const events = [ + sseLf("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { type: "function_call", id: "fc_ok", call_id: "call_ok", name: "get_weather", arguments: "" } }), + sseLf("response.function_call_arguments.delta", { type: "response.function_call_arguments.delta", item_id: "fc_ok", output_index: 0, delta: '{"c":1}' }), + sseLf("response.function_call_arguments.done", { type: "response.function_call_arguments.done", item_id: "fc_ok", output_index: 0, arguments: '{"c":1}' }), + sseLf("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { type: "function_call", id: "fc_ok", call_id: "call_ok", name: "get_weather", arguments: '{"c":1}' } }), + sseLf("response.completed", { type: "response.completed", response: { id: "resp_2", status: "completed", output: [] } }), + ]; + const warns = await captureWarns(() => pipePluginResponsesWithStrip(streamOf(events), res, makeSession())); + assert.equal(warns.filter((m) => m.includes("malformed tool-call args")).length, 0, "no warn for well-formed traffic"); +}); + +test("#1518 non-streaming openai JSON: malformed tool_call arguments kept verbatim + one warn", async () => { + const body = { + id: "chatcmpl-x", object: "chat.completion", created: 1, model: "gpt", + choices: [{ + index: 0, + message: { role: "assistant", content: null, tool_calls: [{ id: "call_1", type: "function", function: { name: "compress", arguments: QUOTE_ARGS } }] }, + finish_reason: "tool_calls", + }], + }; + const raw = JSON.stringify(body); + const out: string[] = []; + const res = makeRes(out); + const warns = await captureWarns(() => pipePluginJson(streamOf([raw]), res, makeSession(), "openai")); + assert.equal(out.join(""), raw, "body bytes untouched (no tag in sight: no reserialize)"); + const w = warns.filter((m) => m.includes("malformed tool-call args")); + assert.equal(w.length, 1, "exactly one warn per bad argument"); + assert.ok(w[0].includes("tool=compress"), "warn carries the tool name"); + assert.ok(w[0].includes(v8Class(QUOTE_ARGS)), "warn carries V8's native error class"); +}); + +test("#1518 non-streaming responses JSON: malformed function_call arguments kept verbatim + one warn", async () => { + const body = { + id: "resp_x", object: "response", status: "completed", + output: [{ type: "function_call", id: "fc_j1", call_id: "call_j1", name: "compress", arguments: QUOTE_ARGS }], + }; + const raw = JSON.stringify(body); + const out: string[] = []; + const res = makeRes(out); + const warns = await captureWarns(() => pipePluginJson(streamOf([raw]), res, makeSession(), "responses")); + assert.equal(out.join(""), raw, "body bytes untouched"); + const w = warns.filter((m) => m.includes("malformed tool-call args")); + assert.equal(w.length, 1, "exactly one warn per bad argument"); + assert.ok(w[0].includes("tool=compress"), "warn carries the tool name"); + assert.ok(w[0].includes(v8Class(QUOTE_ARGS)), "warn carries V8's native error class"); +}); + +test("#1518 proxy-mode lane (no bound session): malformed args stay silent — diagnostics belong to the ladder", async () => { + const out: string[] = []; + const res = makeRes(out); + const events = [ + chatChunk({ role: "assistant" }), + chatChunk({ tool_calls: [{ index: 0, id: "call_p1", type: "function", function: { name: "compress", arguments: QUOTE_ARGS } }] }), + chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }), + DONE, + ]; + const warns = await captureWarns(() => pipePluginChatWithStrip(streamOf(events), res, "openai", undefined)); + assert.ok(out.join("").includes("call_p1"), "bytes still forward verbatim on the #460 lane"); + assert.equal(warns.filter((m) => m.includes("malformed tool-call args")).length, 0, "observe-only pass is gated on a bound session"); +});