From 8a4c9c06f4bef07e15e8017385ec76d423201be2 Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Fri, 18 Sep 2026 16:23:58 +0800 Subject: [PATCH] fix: strip model-echoed render tags from tool-call arguments on every wire path #933: response-side tag stripping only covered text fields, so echoed refs inside tool-call arguments (OpenAI function.arguments, Anthropic input_json_delta, Responses function_call_arguments) reached clients verbatim and rendered in the host TUI. Strip is now structural (deep-scan of every string leaf) on streaming and non-streaming paths in both compression modes; ACP_RENDER_NONE is honored on all wire paths and documented. Rebased onto master 852b068; merge conflicts in src/plugin.ts and src/server.ts resolved (kept #728 localInputEstimate + ACP_RENDER_NONE gate; done-family arg/terminal split preserved). Fixes #933 --- CONFIGURATION.md | 1 + src/loop/adapter-anthropic.ts | 4 +- src/loop/adapter-openai.ts | 64 ++++- src/loop/adapter-responses.ts | 41 ++-- src/loop/tag-echo-filter.ts | 115 ++++++++- src/plugin.ts | 155 ++++++++++-- src/server.ts | 10 +- tests/fix-933-tag-echo-toolargs.test.ts | 305 ++++++++++++++++++++++++ 8 files changed, 643 insertions(+), 52 deletions(-) create mode 100644 tests/fix-933-tag-echo-toolargs.test.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 746914f777..b189bf9270 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -493,6 +493,7 @@ Environment variables take precedence over the config file. They are useful for | `BILI_UPSTREAM_TIMEOUT_MS` | Idle budget (ms) for upstream requests: time-to-first-byte and time between body chunks (default `720000` = 12 min). A healthy stream that keeps producing chunks is never cut mid-flight; a silent one is. The same value drives the underlying HTTP client's transport timeouts, so this single knob bounds long local-model prefills end-to-end (#551). | | `ACP_SESSION_HEADER` | Conversation-id header name (default `x-acp-session`). | | `ACP_REASONING_KEEP` | Responses API only: set `none` to drop all reasoning items. Default routes reasoning through the compression pipeline so it is hidden automatically once its turn is summarized (prevents the unbounded accumulation that broke Codex's prompt-cache prefix). | +| `ACP_RENDER_NONE` | Set to `1` to stop injecting per-message render tags (the `` `` `` markers carrying `mNNNNN` refs) into outgoing request history — applies to every wire format (OpenAI chat, Anthropic, Responses) and compact rebuilds (#933). Default is `text-only`: the model reads these refs to cite messages in `compress` calls, so only disable them once you've confirmed your workflow doesn't need ref-based compression (e.g. tag echoes leaking into client-visible output). Previously this variable was honored only on the Responses path and compact; #933 extended it to all paths. | | `ACP_LOG_FILE` | Log file path (default XDG state path; `off` disables the file, keeps stderr). Auto-rotates at 10 MB. | | `ACP_DUMP_SSE` | Directory to dump raw SSE frames for debugging. | | `BILI_LOG_MASK_HOSTS` | Set `0` to turn OFF host masking in proxy logs (#897): non-public target hosts (private relays, internal domains) are logged verbatim instead of ``. Default is ON (#255 — logs get pasted into public issues); credential-header masking is independent and always on. Real target hosts are always available without touching this flag: `GET /__bili/stats` → `blindTunnels`, `GET /__bili/health` (both loopback-only), and the `acp_status` output. | diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts index c0aebf2b68..8a7c566277 100644 --- a/src/loop/adapter-anthropic.ts +++ b/src/loop/adapter-anthropic.ts @@ -1,7 +1,7 @@ import type { CoreMessage } from "acp-kernel"; import { coreToAnthropic, extractSystem, buildSystem, type AnthropicRequestBody } from "acp-kernel/wire"; import { buildVisibilityMarker } from "../compress-loop.js"; -import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js"; +import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter, stripAcpTags } from "./tag-echo-filter.js"; import { degenerateTurnWarning } from "../degenerate-turn.js"; import { log as loggerLog } from "../logger.js"; import type { @@ -323,7 +323,7 @@ export function createAnthropicAdapter(requestBody: Record, ori kind: "tool_call", name: tb.name, callId: tb.id, - arguments: tb.json, + arguments: stripAcpTags(tb.json), } as ParsedStreamEvent; } else if (thinkingIndexes.delete(upstreamIndex)) { // Seal the current thinking segment so interleaved thinking diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index 495e8ece80..83516fb52b 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -1,7 +1,7 @@ import type { CoreMessage } from "acp-kernel"; import { coreToOpenai, injectOpenaiSystem } from "acp-kernel/wire"; import { buildVisibilityMarker } from "../compress-loop.js"; -import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js"; +import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter, stripAcpTags } from "./tag-echo-filter.js"; import { degenerateTurnWarning } from "../degenerate-turn.js"; import { log as loggerLog } from "../logger.js"; import { hardenOpenaiAssistantContent, systemToUser } from "../util.js"; @@ -253,7 +253,7 @@ export function createOpenaiAdapter(requestBody: Record, client kind: "tool_call", name: tc.name, callId: tc.id, - arguments: tc.arguments, + arguments: stripAcpTags(tc.arguments), } as ParsedStreamEvent; } } @@ -282,16 +282,70 @@ export function createOpenaiAdapter(requestBody: Record, client kind: "tool_call", name: tc.name, callId: tc.id, - arguments: tc.arguments, + arguments: stripAcpTags(tc.arguments), passthrough: true, } as ParsedStreamEvent; } } + // #933: a render tag echoed into a real tool call's arguments + // may span fragment boundaries, so per-fragment stripping is not + // enough. Everything is buffered by settle time: rebuild each + // tainted index as one chunk carrying the clean full arguments, + // suppress its remaining fragments, leave clean calls verbatim. + const taintedIdx = new Set(); + for (const [idx, tc] of pending) { + if (realIndexes.has(idx) && stripAcpTags(tc.arguments) !== tc.arguments) taintedIdx.add(idx); + } + const rebuiltTainted = new Set(); + const taintReplay = (obj: Record): Buffer | null => { + const chs = obj["choices"]; + if (!Array.isArray(chs)) return null; + let changed = false; + const outChoices = chs.map((c) => { + if (!c || typeof c !== "object") return c; + const co = c as Record; + const d = co["delta"]; + if (!d || typeof d !== "object") return c; + const dd = d as Record; + const tcs = dd["tool_calls"]; + if (!Array.isArray(tcs)) return c; + let chChanged = false; + const outTcs: unknown[] = []; + for (const tco of tcs) { + if (tco && typeof tco === "object" && typeof (tco as Record)["index"] === "number") { + const idx = (tco as Record)["index"] as number; + if (taintedIdx.has(idx)) { + if (rebuiltTainted.has(idx)) continue; + rebuiltTainted.add(idx); + const pt = pending.get(idx); + outTcs.push({ index: idx, id: pt?.id ?? "", type: "function", function: { name: pt?.name ?? "", arguments: stripAcpTags(pt?.arguments ?? "") } }); + chChanged = true; + continue; + } + } + outTcs.push(tco); + } + if (!chChanged) return c; + changed = true; + return { ...co, delta: { ...dd, tool_calls: outTcs } }; + }); + if (!changed) return null; + return Buffer.from("data: " + JSON.stringify({ ...obj, choices: outChoices }) + "\n\n", "utf8"); + }; for (const { json, parsed } of rawToolChunks) { const filtered = filterRealToolFragments(parsed, realIndexes); + const obj = filtered === "keep" ? parsed : filtered; + if (obj === null) continue; + if (taintedIdx.size > 0) { + const rebuilt = taintReplay(obj); + if (rebuilt) { + yield { kind: "meta", chunk: rebuilt } as ParsedStreamEvent; + continue; + } + } if (filtered === "keep") { yield { kind: "meta", chunk: Buffer.from("data: " + json + "\n\n", "utf8") } as ParsedStreamEvent; - } else if (filtered !== null) { + } else { yield { kind: "meta", chunk: Buffer.from("data: " + JSON.stringify(filtered) + "\n\n", "utf8") } as ParsedStreamEvent; } } @@ -302,7 +356,7 @@ export function createOpenaiAdapter(requestBody: Record, client kind: "tool_call", name: tc.name, callId: tc.id, - arguments: tc.arguments, + arguments: stripAcpTags(tc.arguments), } as ParsedStreamEvent; } } diff --git a/src/loop/adapter-responses.ts b/src/loop/adapter-responses.ts index 8cfeeb989e..df8b995390 100644 --- a/src/loop/adapter-responses.ts +++ b/src/loop/adapter-responses.ts @@ -4,7 +4,7 @@ import { coreToResponsesWithToolImages as coreToResponses, patchResponsesInputWi import { buildVisibilityMarker } from "../compress-loop.js"; import { hoistTrappedToolItems } from "../tool-pair-order.js"; import { hashId } from "../util.js"; -import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter, stripResponsesText, containsMarkerLineText, containsRenderTagText, ACP_NAME_ALT } from "./tag-echo-filter.js"; +import { composeStreamFilters, createMarkerLineFilter, createTagEchoFilter, stripResponsesText, stripAcpTags, containsMarkerLineText, containsRenderTagText, ACP_NAME_ALT } from "./tag-echo-filter.js"; import { degenerateTurnWarning } from "../degenerate-turn.js"; import { log as loggerLog } from "../logger.js"; import { ACP_TEXT_OPEN, ACP_TEXT_CLOSE, ACP_STATUS_OPEN, ACP_STATUS_CLOSE, ACP_SEARCH_OPEN, ACP_SEARCH_CLOSE, ACP_DECOMPRESS_OPEN, ACP_DECOMPRESS_CLOSE, COMPRESS_TOOL_NAME, PROXY_TOOL_NAMES } from "../compress-tool.js"; @@ -215,6 +215,7 @@ function buildMessageItemSequence(itemId: string, outputIndex: number, text: str } function buildFunctionCallEvents(fc: ToolCallEmit, itemId: string, outputIndex: number): Buffer { + const args = stripAcpTags(fc.arguments); return Buffer.from( [ `event: response.output_item.added\ndata: ${JSON.stringify({ @@ -225,17 +226,17 @@ function buildFunctionCallEvents(fc: ToolCallEmit, itemId: string, outputIndex: `event: response.function_call_arguments.delta\ndata: ${JSON.stringify({ type: "response.function_call_arguments.delta", item_id: itemId, - delta: fc.arguments, + delta: args, })}\n\n`, `event: response.function_call_arguments.done\ndata: ${JSON.stringify({ type: "response.function_call_arguments.done", item_id: itemId, - arguments: fc.arguments, + arguments: args, })}\n\n`, `event: response.output_item.done\ndata: ${JSON.stringify({ type: "response.output_item.done", output_index: outputIndex, - item: { type: "function_call", id: itemId, call_id: fc.callId, name: fc.name, arguments: fc.arguments }, + item: { type: "function_call", id: itemId, call_id: fc.callId, name: fc.name, arguments: args }, })}\n\n`, ].join(""), "utf8", @@ -380,17 +381,20 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp arguments: "", }); } else { - yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent; + const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf; + yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent; } } else if (item?.type === "custom_tool_call") { - yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent; + const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf; + yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent; } else if (item?.type === "message" && round > 1 && !suppressTextLifecycle) { const origId = typeof item.id === "string" ? item.id : ""; const mapped = { id: `msg-proxy-${round}-${hashId(origId || String(outputIndex))}`, index: outputIndex++ }; if (origId) remapped.set(origId, mapped); yield { kind: "meta", chunk: rewriteItemEvent(type, obj, mapped), firstRoundOnly: false } as ParsedStreamEvent; } else if (item?.type !== "message" || !suppressTextLifecycle) { - yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent; + const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf; + yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent; } } else if ( type === "response.content_part.added" || @@ -428,13 +432,19 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp const delta = typeof obj.delta === "string" ? obj.delta : ""; const fc = pending.get(itemId); if (fc) fc.arguments += delta; - else yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent; + else { + const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf; + yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent; + } } else if (type === "response.function_call_arguments.done") { const itemId = typeof obj.item_id === "string" ? obj.item_id : ""; const args = typeof obj.arguments === "string" ? obj.arguments : ""; const fc = pending.get(itemId); if (fc && args) fc.arguments = args; - else yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent; + else { + const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf; + yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent; + } } else if (type === "response.output_item.done") { const item = obj.item as Record | undefined; if (item?.type === "function_call") { @@ -448,21 +458,23 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp kind: "tool_call", name: fc.name, callId: fc.callId, - arguments: fc.arguments, + arguments: stripAcpTags(fc.arguments), } as ParsedStreamEvent; } else { - yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent; + const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf; + yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent; toolCallsEmitted++; yield { kind: "tool_call", name: typeof item.name === "string" ? item.name : "", callId: typeof item.call_id === "string" ? item.call_id : "", - arguments: typeof item.arguments === "string" ? item.arguments : "", + arguments: typeof item.arguments === "string" ? stripAcpTags(item.arguments) : "", passthrough: true, } as ParsedStreamEvent; } } else if (item?.type === "custom_tool_call") { - yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false } as ParsedStreamEvent; + const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf; + yield { kind: "meta", chunk, firstRoundOnly: false } as ParsedStreamEvent; } else if (item?.type === "message") { const origId = typeof item.id === "string" ? item.id : ""; const mapped = remapped.get(origId); @@ -474,7 +486,8 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent; } } else if (item?.type !== "message" || !suppressTextLifecycle) { - yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent; + const chunk = (containsRenderTagText(eventStr) || containsMarkerLineText(eventStr)) ? rebuildResponsesEvent(type, stripResponsesText(obj)) : rawBuf; + yield { kind: "meta", chunk, firstRoundOnly: true } as ParsedStreamEvent; } } else if (type === "response.completed") { yield* flushFilter(); diff --git a/src/loop/tag-echo-filter.ts b/src/loop/tag-echo-filter.ts index 95dfee2f9f..86711fc5ee 100644 --- a/src/loop/tag-echo-filter.ts +++ b/src/loop/tag-echo-filter.ts @@ -424,20 +424,106 @@ function stripParts(content: unknown): unknown { } function stripItemContent(it: unknown): unknown { - if (it && typeof it === "object" && Array.isArray((it as Record).content)) { - return { ...(it as Record), content: stripParts((it as Record).content) }; + if (!it || typeof it !== "object") return it; + const io = it as Record; + let out: Record | undefined; + const set = (k: string, v: unknown): void => { + out ??= { ...io }; + out[k] = v; + }; + if (Array.isArray(io.content)) set("content", stripParts(io.content)); + if (typeof io.arguments === "string") set("arguments", stripAcpTags(io.arguments)); + if (Array.isArray(io.summary)) { + set( + "summary", + io.summary.map((s) => + s && typeof s === "object" && typeof (s as Record).text === "string" + ? { ...(s as Record), text: stripAcpTags((s as Record).text as string) } + : s, + ), + ); } - return it; + return out ?? it; } function stripIfString(v: unknown): unknown { return typeof v === "string" ? stripAcpTags(v) : v; } +// #933: whole-object invariant — field-enumerated stripping kept missing new +// carriers (tool-call arguments); these walk every string leaf instead. +export function stripAcpTagsDeep(v: unknown): unknown { + if (typeof v === "string") return stripAcpTags(v); + if (Array.isArray(v)) { + let changed = false; + const out = v.map((x) => { + const r = stripAcpTagsDeep(x); + if (r !== x) changed = true; + return r; + }); + return changed ? out : v; + } + if (v && typeof v === "object") { + let changed = false; + const out: Record = {}; + for (const [k, x] of Object.entries(v as Record)) { + const r = stripAcpTagsDeep(x); + if (r !== x) changed = true; + out[k] = r; + } + return changed ? out : v; + } + return v; +} + +export function findAcpLikeTagSnippetsDeep(v: unknown, path = "$"): string[] { + const hits: string[] = []; + const visit = (x: unknown, p: string): void => { + if (typeof x === "string") { + const pairedSpans: Array<[number, number]> = []; + for (const m of x.matchAll(new RegExp(PAIRED.source, "g"))) { + hits.push(`${p}: ${m[0].slice(0, 60)}`); + pairedSpans.push([m.index, m.index + m[0].length]); + } + for (const re of [new RegExp(LONE_OPEN.source, "g"), new RegExp(LONE_CLOSE.source, "g")]) { + for (const m of x.matchAll(re)) { + if (pairedSpans.some(([s, e]) => m.index! >= s && m.index! < e)) continue; + hits.push(`${p}: ${m[0].slice(0, 60)}`); + } + } + for (const m of x.matchAll(MARKER_LINE)) hits.push(`${p}: ${m[0].slice(0, 60)}`); + return; + } + if (Array.isArray(x)) { + x.forEach((it, i) => visit(it, `${p}[${i}]`)); + return; + } + if (x && typeof x === "object") { + for (const [k, it] of Object.entries(x as Record)) visit(it, `${p}.${k}`); + } + }; + visit(v, path); + return hits; +} + +function stripToolCallArgs(tcs: unknown): unknown { + if (!Array.isArray(tcs)) return tcs; + return tcs.map((tc) => { + if (!tc || typeof tc !== "object") return tc; + const tco = tc as Record; + const fn = tco["function"]; + if (fn && typeof fn === "object" && typeof (fn as Record)["arguments"] === "string") { + return { ...tco, function: { ...(fn as Record), arguments: stripAcpTags((fn as Record)["arguments"] as string) } }; + } + return tc; + }); +} + // Plugin-passthrough parity for the OpenAI chat-completions wire (issue #14: // pi + qwen echoed render tags through the verbatim plugin stream): strip the -// text fields a chat chunk / completion carries — `choices[].delta.{content, -// reasoning_content, reasoning}` on streams and `choices[].message.*` on +// text fields and tool-call arguments a chat chunk / completion carries — +// `choices[].delta.{content,reasoning_content,reasoning}` + +// `tool_calls[].function.arguments` on streams, `choices[].message.*` on // non-streaming bodies. Mutates in place, mirroring stripResponsesText. export function stripOpenaiChatText(obj: T): T { if (!obj || typeof obj !== "object") return obj; @@ -453,6 +539,7 @@ export function stripOpenaiChatText(obj: T): T { hh["content"] = stripIfString(hh["content"]); hh["reasoning_content"] = stripIfString(hh["reasoning_content"]); hh["reasoning"] = stripIfString(hh["reasoning"]); + hh["tool_calls"] = stripToolCallArgs(hh["tool_calls"]); ch[holder] = hh; } } @@ -472,14 +559,22 @@ export function stripAnthropicText(obj: T): T { const dd = { ...(d as Record) }; dd["text"] = stripIfString(dd["text"]); dd["thinking"] = stripIfString(dd["thinking"]); + dd["partial_json"] = stripIfString(dd["partial_json"]); o["delta"] = dd; } if (Array.isArray(o["content"])) { o["content"] = (o["content"] as unknown[]).map((c) => { if (!c || typeof c !== "object") return c; const cc = c as Record; - if (typeof cc["text"] !== "string" && typeof cc["thinking"] !== "string") return c; - return { ...cc, text: stripIfString(cc["text"]), thinking: stripIfString(cc["thinking"]) }; + const hasText = typeof cc["text"] === "string" || typeof cc["thinking"] === "string"; + const input = cc["type"] === "tool_use" ? cc["input"] : undefined; + const hasInput = input != null && typeof input === "object"; + if (!hasText && !hasInput) return c; + return { + ...cc, + ...(hasText ? { text: stripIfString(cc["text"]), thinking: stripIfString(cc["thinking"]) } : {}), + ...(hasInput ? { input: stripAcpTagsDeep(input) } : {}), + }; }); } return obj; @@ -494,13 +589,13 @@ export function stripResponsesText(obj: T): T { if (!obj || typeof obj !== "object") return obj; const o = obj as Record; if (typeof o.text === "string") o.text = stripAcpTags(o.text); + if (typeof o.arguments === "string") o.arguments = stripAcpTags(o.arguments); + if (typeof o.delta === "string") o.delta = stripAcpTags(o.delta); if (o.part && typeof o.part === "object" && typeof (o.part as Record).text === "string") { o.part = { ...(o.part as Record), text: stripAcpTags((o.part as Record).text as string) }; } if (o.item && typeof o.item === "object") { - const item = { ...(o.item as Record) }; - if (Array.isArray(item.content)) item.content = stripParts(item.content); - o.item = item; + o.item = stripItemContent(o.item); } if (o.response && typeof o.response === "object") { const resp = { ...(o.response as Record) }; diff --git a/src/plugin.ts b/src/plugin.ts index d3d2d51d26..b6f1d168d1 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -841,7 +841,14 @@ export async function pipePluginChatWithStrip( }; // One state machine per (field, block/choice index) — interleaved choices // or content blocks must not share partial-tag state. - const streams = new Map(); + interface PipeStream { + filter: TagEchoFilter; + field: string; + index: number; + choice?: number; + toolIndex?: number; + } + const streams = new Map(); const filterFor = (field: string, index: number) => { const key = `${field}:${index}`; let s = streams.get(key); @@ -851,17 +858,29 @@ export async function pipePluginChatWithStrip( } return s; }; + const toolArgStreamFor = (choice: number, toolIndex: number) => { + const key = `tcall:${choice}:${toolIndex}`; + let s = streams.get(key); + if (!s) { + s = { filter: createTagEchoFilter(onTagDrop), field: "arguments", index: toolIndex, choice, toolIndex }; + streams.set(key, s); + } + return s; + }; const anyPending = () => { for (const s of streams.values()) if (s.filter.pending()) return true; return false; }; let lastChunkMeta: Record = {}; - const syntheticTail = (field: string, index: number, tail: string): string => { + const syntheticTail = (s: PipeStream, tail: string): string => { if (protocol === "anthropic") { - const deltaType = field === "text" ? "text_delta" : "thinking_delta"; - return `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: deltaType, [field]: tail } })}\n\n`; + const deltaType = s.field === "text" ? "text_delta" : s.field === "thinking" ? "thinking_delta" : "input_json_delta"; + return `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: s.index, delta: { type: deltaType, [s.field]: tail } })}\n\n`; + } + if (s.toolIndex !== undefined) { + return `data: ${JSON.stringify({ ...lastChunkMeta, object: "chat.completion.chunk", choices: [{ index: s.choice ?? 0, delta: { tool_calls: [{ index: s.toolIndex, function: { arguments: tail } }] } }] })}\n\n`; } - return `data: ${JSON.stringify({ ...lastChunkMeta, object: "chat.completion.chunk", choices: [{ index, delta: { [field]: tail } }] })}\n\n`; + return `data: ${JSON.stringify({ ...lastChunkMeta, object: "chat.completion.chunk", choices: [{ index: s.index, delta: { [s.field]: tail } }] })}\n\n`; }; // #673: turn-level observability for degenerate terminal turns. let sawToolUse = false; @@ -976,7 +995,7 @@ export async function pipePluginChatWithStrip( for (const s of streams.values()) { const tail = s.filter.flush(); if (tail.length > 0) { - out += syntheticTail(s.field, s.index, tail); + out += syntheticTail(s, tail); if (s.field === "content" || s.field === "text") { visibleTextChars += tail.length; releasedMarkupChars += tail.length; @@ -1095,6 +1114,33 @@ export async function pipePluginChatWithStrip( (rebuilt["choices"] as Record[])[ci]["delta"] = { ...((rebuilt["choices"] as Record[])[ci]["delta"] as Record), [field]: clean }; } } + const tcs = dd["tool_calls"]; + if (Array.isArray(tcs)) { + for (let ti = 0; ti < tcs.length; ti++) { + const tc = tcs[ti]; + if (!tc || typeof tc !== "object") continue; + const fn = (tc as Record)["function"]; + const av = fn && typeof fn === "object" ? (fn as Record)["arguments"] : undefined; + if (typeof av !== "string" || av.length === 0) continue; + if (!mayStartRenderTag(av) && !anyPending()) continue; + const choiceIdx = typeof ch?.["index"] === "number" ? ch["index"] : ci; + const tcIdx = typeof (tc as Record)["index"] === "number" ? ((tc as Record)["index"] as number) : ti; + const s = toolArgStreamFor(choiceIdx, tcIdx); + const clean = s.filter.push(av); + if (clean === av) continue; + if (!rebuilt) { + rebuilt = { ...ev, choices: choices.map((c) => ({ ...(c as Record), delta: { ...((c as Record)["delta"] as Record) } })) }; + } + const rch = (rebuilt["choices"] as Record[])[ci]; + const rdd = rch["delta"] as Record; + const rtcs = Array.isArray(rdd["tool_calls"]) ? (rdd["tool_calls"] as unknown[]) : []; + const rtc = rtcs[ti] ? { ...(rtcs[ti] as Record) } : { ...(tc as Record) }; + rtc["function"] = { ...((rtc["function"] ?? {}) as Record), arguments: clean }; + rtcs[ti] = rtc; + rdd["tool_calls"] = rtcs; + rch["delta"] = rdd; + } + } } if (rebuilt) { // Delta carried no visible text after stripping: drop the whole @@ -1122,7 +1168,7 @@ export async function pipePluginChatWithStrip( } const d = ev["delta"] as Record | undefined; const index = typeof ev["index"] === "number" ? ev["index"] : 0; - const field = d?.["type"] === "thinking_delta" ? "thinking" : d?.["type"] === "text_delta" ? "text" : null; + const field = d?.["type"] === "thinking_delta" ? "thinking" : d?.["type"] === "text_delta" ? "text" : d?.["type"] === "input_json_delta" ? "partial_json" : null; if (field === null || typeof d?.[field] !== "string") { return rawEvent + "\n\n"; } @@ -1316,11 +1362,12 @@ export async function pipePluginResponsesWithStrip( let decoder = new TextDecoder("utf-8"); let buf = ""; const acc: UsageSample = {}; + const onTagDrop = (snippet: string) => { + loggerLog("warn", `[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + log?.(`[tag-echo] stripped model-emitted render tag from plugin passthrough text`); + }; const tagFilter = composeStreamFilters( - createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); - log?.(`[tag-echo] stripped model-emitted render tag from plugin passthrough text`); - }), + createTagEchoFilter(onTagDrop), createMarkerLineFilter((snippet) => { loggerLog("warn", `[marker-echo] stripped model-emitted ACP confirmation marker (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); log?.(`[marker-echo] stripped model-emitted ACP confirmation marker from plugin passthrough text`); @@ -1513,6 +1560,39 @@ export async function pipePluginResponsesWithStrip( buf = ""; return true; }; + interface RespArgStream { + filter: TagEchoFilter; + type: string; + field: string; + meta: Record; + } + const argStreams = new Map(); + const argStreamFor = (type: string, field: string, ev: Record) => { + const id = typeof ev["item_id"] === "string" ? ev["item_id"] : String(ev["output_index"] ?? 0); + const key = `${type}:${id}`; + let s = argStreams.get(key); + if (!s) { + const meta: Record = {}; + for (const k of ["item_id", "output_index", "summary_index"]) { + if (ev[k] !== undefined) meta[k] = ev[k]; + } + s = { filter: createTagEchoFilter(onTagDrop), type, field, meta }; + argStreams.set(key, s); + } + return s; + }; + const argAnyPending = () => { + for (const s of argStreams.values()) if (s.filter.pending()) return true; + return false; + }; + const flushArgTails = () => { + let out = ""; + for (const s of argStreams.values()) { + const tail = s.filter.flush(); + if (tail.length > 0) out += `data: ${JSON.stringify({ type: s.type, ...s.meta, [s.field]: tail })}\n\n`; + } + return out; + }; try { for (;;) { const { done, value } = await reader.read(); @@ -1562,6 +1642,17 @@ export async function pipePluginResponsesWithStrip( } } if (retryFraming(type)) continue; + // #933: done-family events also carry full text payloads — strip those too. + // Arg-carrier dones are not visible text: the degenerate-turn retry below + // does not hold on them, so they are stripped and released directly. + if (type === "response.reasoning_summary_part.done" || type === "response.function_call_arguments.done") { + const hadEcho = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr); + if (hadEcho) sawStrippedEcho = true; + const evOut = hadEcho ? stripResponsesText(ev) : ev; + const out = hadEcho ? rebuildEvent(rawEvent, evOut) : rawEvent + "\n\n"; + await write(flushArgTails() + flushTail(out)); + continue; + } if (type === "response.output_text.done" || type === "response.content_part.done" || type === "response.output_item.done") { // HELD until the completion event decides the turn (see // retryEmptyTurn): releasing it earlier would hand the @@ -1581,19 +1672,28 @@ export async function pipePluginResponsesWithStrip( sawTerminal = true; if (await retryEmptyTurn(type === "response.completed" ? "completed" : undefined)) { // The retry took over: this attempt's held family and - // its own completion frame are dropped together. + // its own completion frame are dropped together. Arg + // streams are empty here by construction — a function + // call would have blocked the retry via sawFunctionCall. + for (const s of argStreams.values()) s.filter.flush(); + argStreams.clear(); heldEvents = []; heldVisibleChars = 0; continue; } const tailFrame = flushTail(""); if (tailFrame.length > 0) await write(tailFrame); + const argTail = flushArgTails(); + if (argTail.length > 0) await write(argTail); for (const held of heldEvents) await write(held); heldEvents = []; heldVisibleChars = 0; - // done-family events also carry full text payloads — strip those too. + // The completion frame itself closes the turn: strip it if it + // carries echoed text, rewrite retry ids onto the first attempt's. + const hadEchoText = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr); + if (hadEchoText) sawStrippedEcho = true; let evOut = ev; - let rebuild = containsRenderTagText(jsonStr) || containsMarkerLineText(jsonStr) || retryRewritePending(); + let rebuild = hadEchoText || retryRewritePending(); if (rebuild) evOut = stripResponsesText(ev); rewriteRetryIds(evOut); await write(rebuild ? rebuildEvent(rawEvent, evOut) : rawEvent + "\n\n"); @@ -1629,6 +1729,29 @@ export async function pipePluginResponsesWithStrip( await write(rebuildEvent(rawEvent, rebuilt)); continue; } + // Both delta event types carry the fragment in `delta`; + // only the .done variants carry `arguments`. + const argField = type === "response.function_call_arguments.delta" || type === "response.reasoning_summary_text.delta" ? "delta" : null; + if (argField !== null && typeof type === "string") { + const v = ev[argField]; + if (typeof v !== "string" || v.length === 0) { + await write(rawEvent + "\n\n"); + continue; + } + if (!mayStartRenderTag(v) && !argAnyPending() && !tagFilter.pending()) { + await write(rawEvent + "\n\n"); + continue; + } + const s = argStreamFor(type, argField, ev); + const clean = s.filter.push(v); + if (clean.length === 0) continue; + if (clean === v) { + await write(flushArgTails() + rawEvent + "\n\n"); + continue; + } + await write(flushArgTails() + rebuildEvent(rawEvent, { ...ev, [argField]: clean })); + continue; + } await write(rawEvent + "\n\n"); } } @@ -1637,7 +1760,7 @@ export async function pipePluginResponsesWithStrip( // Stream cut without a done-family event: flush whatever the tag // filter still holds so prose is never silently lost. if (!res.destroyed && !res.writableEnded) { - const rest = flushTail(""); + const rest = flushArgTails() + flushTail(""); if (rest.length > 0) await write(rest); } maybeWarnDegenerate(); @@ -1663,7 +1786,7 @@ export async function pipePluginResponsesWithStrip( // the top-level handler, which would close the stream bare. Flush // held tag tails first so partial prose is never silently lost. try { - const rest = flushTail(""); + const rest = flushArgTails() + flushTail(""); if (rest.length > 0) await write(rest); } catch { /* client half-gone; the emission below is best-effort too */ diff --git a/src/server.ts b/src/server.ts index c16577c247..8f38305ed2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1928,7 +1928,7 @@ function prepareAnthropic( // (applyAbsorbView hides regardless of enablement). const absorbActive = absorbEnabled(config) && opts.compress.injectTool; const loopConfig = absorbActive ? config : { ...config, absorb: undefined }; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: "text-only" }); + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); session.state = turn.state; // The fold from last turn's compress has now materialized in state — // future usage reports are post-fold reality, drop the credit. @@ -2000,7 +2000,7 @@ function prepareAnthropic( session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) + countSystemAndToolsTokens(extractSystem(systemOut), toolsOut) + imageTokensInParsedBody("anthropic", rebuilt); - return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, anthropicSystem: parsed.system, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, renderTags: "text-only" } as Prepared; + return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, anthropicSystem: parsed.system, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" } as Prepared; } function prepareOpenai( @@ -2065,7 +2065,7 @@ function prepareOpenai( // prefix-cache stability, so strip absorb from the loop config there. const absorbActive = absorbEnabled(config) && shouldInject; const loopConfig = absorbActive ? config : { ...config, absorb: undefined }; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: "text-only" }); + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); session.state = turn.state; // The fold from last turn's compress has now materialized in state — // future usage reports are post-fold reality, drop the credit. @@ -2168,7 +2168,7 @@ function prepareOpenai( } snapshotMessages(session, originalMessages); markDirty(session); - return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "openai", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, openaiSystemText, renderTags: "text-only" } as Prepared; + return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "openai", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, openaiSystemText, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" } as Prepared; } function prepareResponses( @@ -2476,7 +2476,7 @@ export function prepareCountTokens( const sessionId = session.id; try { const { msgs, cacheControls } = anthropicToCore(parsed); - const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: "text-only" }); + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); const stripped = stripKernelSummaries(turn.messages as BiliMessage[], turn.state); const rebuiltMessages = coreToAnthropic(stripped, cacheControls); log("info", `[${sessionId}] count_tokens pruned: ${msgs.length} → ${stripped.length} msgs`); diff --git a/tests/fix-933-tag-echo-toolargs.test.ts b/tests/fix-933-tag-echo-toolargs.test.ts new file mode 100644 index 0000000000..2fb9549a3f --- /dev/null +++ b/tests/fix-933-tag-echo-toolargs.test.ts @@ -0,0 +1,305 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip } from "../src/plugin.ts"; +import { findAcpLikeTagSnippetsDeep, stripAcpTagsDeep } from "../src/loop/tag-echo-filter.ts"; +import type { Session } from "../src/session.ts"; + +process.env.NODE_ENV = "test"; + +// Render-tag pieces are assembled from hex escapes so no literal tag sequence +// appears in this file's source. +const LT = "\x3c"; +const GT = "\x3e"; +const TAG = (id: string) => `${LT}acp tokens="247" type="text"${GT}${id}${LT}/acp${GT}`; +const OPEN_MARK = `${LT}acp `; +const CLOSE_MARK = `${LT}/acp${GT}`; + +function makeSession(): Session { + return { + id: "sess933", + 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 dataLines(text: string): Record[] { + const out: Record[] = []; + for (const block of text.split("\n\n")) { + const line = block.split("\n").find((l) => l.startsWith("data:")); + if (!line) continue; + const s = line.slice(5).replace(/^ /, "").trim(); + if (!s || s === "[DONE]") continue; + try { + out.push(JSON.parse(s)); + } catch { + continue; + } + } + return out; +} + +function assertNoTagsDeep(label: string, v: unknown): void { + const hits = findAcpLikeTagSnippetsDeep(v); + assert.deepEqual(hits, [], `${label}: ACP-like tag survived: ${JSON.stringify(hits)}`); +} + +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`; +} + +function asString(v: unknown): string { + return typeof v === "string" ? v : ""; +} + +function asObj(v: unknown): Record { + return v && typeof v === "object" && !Array.isArray(v) ? (v as Record) : {}; +} + +function asArr(v: unknown): unknown[] { + return Array.isArray(v) ? v : []; +} + +function openaiToolArgs(events: Record[]): string { + let out = ""; + for (const ev of events) { + for (const ch of asArr(ev["choices"])) { + const d = asObj(ch["delta"]); + for (const tc of asArr(d["tool_calls"])) { + out += asString(asObj(tc["function"])["arguments"]); + } + } + } + return out; +} + +function anthropicInput(events: Record[]): string { + let out = ""; + for (const ev of events) { + if (ev["type"] !== "content_block_delta") continue; + const d = asObj(ev["delta"]); + if (d["type"] === "input_json_delta") out += asString(d["partial_json"]); + } + return out; +} + +function responsesArgs(events: Record[]): string { + let out = ""; + for (const ev of events) { + if (ev["type"] === "response.function_call_arguments.delta") out += asString(ev["delta"]); + } + return out; +} + +test("#933 invariant: deep scan finds planted tags in every carrier field, clean objects report none", () => { + const tag = TAG("m00042"); + const openaiBody = { + choices: [ + { + message: { + role: "assistant", + content: `prose ${tag} here`, + tool_calls: [{ id: "c1", type: "function", function: { name: "f", arguments: `{"n": "x${tag}y"}` } }], + }, + }, + ], + }; + const anthropicBody = { + content: [ + { type: "text", text: `head ${tag}` }, + { type: "thinking", thinking: `think ${tag}` }, + { type: "tool_use", id: "t1", name: "f", input: { n: `x${tag}y`, list: [`a${tag}b`] } }, + ], + }; + const responsesBody = { + output: [ + { type: "message", content: [{ type: "output_text", text: `say ${tag}` }] }, + { type: "function_call", id: "fc1", call_id: "c1", name: "f", arguments: `{"n": "${tag}"}` }, + { type: "reasoning", summary: [{ type: "summary_text", text: `sum ${tag}` }] }, + ], + }; + for (const [label, body] of [ + ["openai", openaiBody], + ["anthropic", anthropicBody], + ["responses", responsesBody], + ] as const) { + const hits = findAcpLikeTagSnippetsDeep(body); + assert.ok(hits.length >= 2, `${label}: expected planted tags to be found, got ${JSON.stringify(hits)}`); + const cleaned = stripAcpTagsDeep(body); + assertNoTagsDeep(`${label} after stripAcpTagsDeep`, cleaned); + } + assertNoTagsDeep("clean object", { choices: [{ message: { content: "5 < 6 holds", tool_calls: [] } }], meta: { a: [1, null, { b: "" }] } }); +}); + +test("#933 openai chat: render tag echoed into tool_calls function.arguments is stripped (split mid-tag)", async () => { + const out: string[] = []; + const res = makeRes(out); + const events = [ + chatChunk({ role: "assistant" }), + chatChunk({ content: "Sure, checking " }), + chatChunk({ tool_calls: [{ index: 0, id: "call_9", type: "function", function: { name: "get_report", arguments: '{"note": "see ' } }] }), + chatChunk({ tool_calls: [{ index: 0, function: { arguments: TAG("m00042").slice(0, 30) } }] }), + chatChunk({ tool_calls: [{ index: 0, function: { arguments: TAG("m00042").slice(30) + ' "x"}' } }] }), + chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }), + "data: [DONE]\n\n", + ]; + await pipePluginChatWithStrip(streamOf(events), res, "openai", makeSession()); + const text = out.join(""); + assert.ok(!text.includes(OPEN_MARK), "client stream leaked a render open tag"); + assert.ok(!text.includes(CLOSE_MARK), "client stream leaked a render close tag"); + assert.ok(!text.includes("m00042"), "tag ref leaked into client stream"); + const parsed = dataLines(text); + assert.equal(openaiToolArgs(parsed), '{"note": "see "x"}', "assembled arguments equal original minus tag"); + assert.ok(text.includes("Sure, checking "), "prose chunk survives"); + assert.ok(text.includes("[DONE]"), "[DONE] forwarded"); +}); + +test("#933 anthropic: render tag echoed into input_json_delta is stripped (split mid-tag)", async () => { + const out: string[] = []; + const res = makeRes(out); + const tag = TAG("m00007"); + const events = [ + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", role: "assistant", usage: { input_tokens: 5 } } })}\n\n`, + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`, + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Result: " } })}\n\n`, + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`, + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 1, content_block: { type: "tool_use", id: "toolu_1", name: "get_report" } })}\n\n`, + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 1, delta: { type: "input_json_delta", partial_json: '{"q": "ref ' } })}\n\n`, + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 1, delta: { type: "input_json_delta", partial_json: tag.slice(0, 20) } })}\n\n`, + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 1, delta: { type: "input_json_delta", partial_json: tag.slice(20) + '"}' } })}\n\n`, + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 1 })}\n\n`, + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 9 } })}\n\n`, + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`, + ]; + await pipePluginChatWithStrip(streamOf(events), res, "anthropic", makeSession()); + const text = out.join(""); + assert.ok(!text.includes(OPEN_MARK), "client stream leaked a render open tag"); + assert.ok(!text.includes(CLOSE_MARK), "client stream leaked a render close tag"); + assert.ok(!text.includes("m00007"), "tag ref leaked into client stream"); + const parsed = dataLines(text); + assert.deepEqual(JSON.parse(anthropicInput(parsed)), { q: "ref " }, "assembled tool input equals original minus tag"); + assert.ok(text.includes("Result: "), "text block survives"); +}); + +test("#933 responses: render tag echoed into function_call_arguments deltas and done payloads is stripped", async () => { + const out: string[] = []; + const res = makeRes(out); + const tag = TAG("m00011"); + const dirtyArgs = `{"a": "see ${tag}"}`; + const events = [ + `event: response.created\ndata: ${JSON.stringify({ type: "response.created", response: { id: "r1", status: "created" } })}\n\n`, + `event: response.output_item.added\ndata: ${JSON.stringify({ type: "response.output_item.added", output_index: 0, item: { type: "function_call", id: "fc_1", call_id: "call_1", name: "get_report", arguments: "" } })}\n\n`, + `event: response.function_call_arguments.delta\ndata: ${JSON.stringify({ type: "response.function_call_arguments.delta", item_id: "fc_1", delta: '{"a": "see ' })}\n\n`, + `event: response.function_call_arguments.delta\ndata: ${JSON.stringify({ type: "response.function_call_arguments.delta", item_id: "fc_1", delta: tag.slice(0, 15) })}\n\n`, + `event: response.function_call_arguments.delta\ndata: ${JSON.stringify({ type: "response.function_call_arguments.delta", item_id: "fc_1", delta: tag.slice(15) + '"}' })}\n\n`, + `event: response.reasoning_summary_text.delta\ndata: ${JSON.stringify({ type: "response.reasoning_summary_text.delta", item_id: "rs_1", output_index: 1, summary_index: 0, delta: `thinking ${TAG("m00009")} more` })}\n\n`, + `event: response.function_call_arguments.done\ndata: ${JSON.stringify({ type: "response.function_call_arguments.done", item_id: "fc_1", arguments: dirtyArgs })}\n\n`, + `event: response.output_item.done\ndata: ${JSON.stringify({ type: "response.output_item.done", output_index: 0, item: { type: "function_call", id: "fc_1", call_id: "call_1", name: "get_report", arguments: dirtyArgs } })}\n\n`, + `event: response.completed\ndata: ${JSON.stringify({ type: "response.completed", response: { id: "r1", status: "completed", output: [{ type: "function_call", id: "fc_1", call_id: "call_1", name: "get_report", arguments: dirtyArgs }] } })}\n\n`, + ]; + await pipePluginResponsesWithStrip(streamOf(events), res, makeSession()); + const text = out.join(""); + assert.ok(!text.includes(OPEN_MARK), "client stream leaked a render open tag"); + assert.ok(!text.includes(CLOSE_MARK), "client stream leaked a render close tag"); + assert.ok(!text.includes("m00011"), "tag ref leaked into client stream"); + assert.ok(!text.includes("m00009"), "reasoning summary tag ref leaked"); + const parsed = dataLines(text); + assert.equal(responsesArgs(parsed), '{"a": "see "}', "delta-assembled arguments equal original minus tag"); + const done = parsed.find((e) => e["type"] === "response.function_call_arguments.done") ?? {}; + assertNoTagsDeep("function_call_arguments.done", done); + const itemDone = parsed.find((e) => e["type"] === "response.output_item.done") ?? {}; + assertNoTagsDeep("output_item.done", itemDone); + const completed = parsed.find((e) => e["type"] === "response.completed") ?? {}; + assertNoTagsDeep("response.completed", completed); +}); + +test("#933 non-streaming: pipePluginJson strips tags from tool-call argument payloads (all protocols)", async () => { + const tag = TAG("m00031"); + const cases = [ + { + protocol: "openai" as const, + body: { choices: [{ message: { role: "assistant", content: "ok", tool_calls: [{ id: "c1", type: "function", function: { name: "f", arguments: `{"n": "x${tag}y"}` } }] } }] }, + }, + { + protocol: "anthropic" as const, + body: { content: [{ type: "tool_use", id: "t1", name: "f", input: { n: `x${tag}y` } }] }, + }, + { + protocol: "responses" as const, + body: { output: [{ type: "function_call", id: "fc1", call_id: "c1", name: "f", arguments: `{"n": "x${tag}y"}` }] }, + }, + ]; + for (const c of cases) { + const out: string[] = []; + const res = makeRes(out); + await pipePluginJson(streamOf([JSON.stringify(c.body)]), res, makeSession(), c.protocol); + const text = out.join(""); + assert.ok(!text.includes(OPEN_MARK), `${c.protocol}: leaked open tag`); + assert.ok(!text.includes(CLOSE_MARK), `${c.protocol}: leaked close tag`); + assertNoTagsDeep(c.protocol, JSON.parse(text)); + } +}); + +test("#933 replay loop: assistant message rebuilt from the clean client stream carries no tags into the next request", async () => { + const out: string[] = []; + const res = makeRes(out); + const events = [ + chatChunk({ role: "assistant" }), + chatChunk({ content: "Sure, checking " }), + chatChunk({ tool_calls: [{ index: 0, id: "call_9", type: "function", function: { name: "get_report", arguments: `{"note": "see ${TAG("m00042")} "x"}"` } }] }), + chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }), + "data: [DONE]\n\n", + ]; + await pipePluginChatWithStrip(streamOf(events), res, "openai", makeSession()); + const parsed = dataLines(out.join("")); + const storedAssistant = { + role: "assistant", + content: parsed.flatMap((ev) => asArr(ev["choices"]).map((ch) => asString(asObj(ch["delta"])["content"]))).join(""), + tool_calls: [ + { + id: "call_9", + type: "function", + function: { name: "get_report", arguments: openaiToolArgs(parsed) }, + }, + ], + }; + const nextRequest = { messages: [{ role: "user", content: "compress please" }, storedAssistant, { role: "user", content: "go on" }] }; + assertNoTagsDeep("replayed next-turn request", nextRequest); +});