From faf3cb465378e2f7ca96a796cf027240cf78e93c Mon Sep 17 00:00:00 2001 From: tiisetso Date: Sun, 18 Oct 2026 11:20:45 +0200 Subject: [PATCH] fix(desktop): harden WPP tool-call parsing Only the two transport shapes now become tool calls: XML function_calls markup and JSON objects typed tool_call. Code fences and inline code hold examples and never run, and a JSON call must name a tool in the request. String parameters keep their exact bytes. Values are typed by the request's tool schemas, close tags inside a value no longer truncate it, CDATA is taken verbatim, entities decode once across all of Unicode, and valid JSON escapes are no longer rewritten. Batches keep every call. Distinct JSON calls are no longer dropped, a truncated XML batch fails as o1_code_incomplete_tool_call instead of running its prefix, repeated ids get fresh ones, and a malformed fallback candidate no longer hides a later valid one. JSON history keeps call ids. The XML reminder gains two rules about fences and CDATA, so the pinned Opus envelope hash is updated. --- AGENTS.md | 2 +- .../main/wpp-bridge/proxy/jsonToolFormat.mjs | 7 +- .../proxy/messageSerializer.test.mjs | 2 +- .../main/wpp-bridge/proxy/openaiCompat.mjs | 6 +- .../src/main/wpp-bridge/proxy/streamGate.mjs | 11 +- .../wpp-bridge/proxy/toolCallNormalizer.mjs | 1072 ++++++++--------- .../proxy/toolCallNormalizer.test.mjs | 217 ++++ .../wpp-bridge/proxy/toolCallReminder.mjs | 2 + 8 files changed, 735 insertions(+), 584 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index ccfcdf6070..a37a15fe92 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -251,7 +251,7 @@ The four numbered files above are the spine. These carry the rest of the behavio **Response out — parsing** -- `proxy/toolCallNormalizer.mjs` — the largest module in the bridge: turns model prose back into OpenAI `tool_calls`. It parses only _unfenced_ XML so that code fences inside a `content` parameter survive intact, and `chooseAssistantResponse` falls back through recorder tool-call parts and alternate assistant texts. Anyone touching model output lands here. +- `proxy/toolCallNormalizer.mjs` — the largest module in the bridge: turns model prose back into OpenAI `tool_calls`. Only two transport shapes count: XML `function_calls`/`invoke` markup and JSON objects whose `type` is `tool_call`. Calls inside Markdown code fences or inline code are examples and never run, while fences inside a parameter value survive intact. Callers pass the request's `tools`: XML values are typed by each tool's schema, so string parameters keep their exact bytes, and JSON calls must name a listed tool. A truncated XML batch throws `o1_code_incomplete_tool_call` instead of running its prefix. `chooseAssistantResponse` falls back through recorder tool-call parts and alternate assistant texts, parsing each one separately. Anyone touching model output lands here. - `proxy/anthropicToolFormat.mjs` / `jsonToolFormat.mjs` — render prior tool calls back out in the format the agent itself emits, so a continued thread reads its own history. Selected by `toolFormat`. - `proxy/streamGate.mjs` — decides from cumulative text whether a turn is prose (stream it live) or a tool call (suppress until normalized). Buffers the first `DECISION_THRESHOLD` (24) non-whitespace characters, holds back a trailing fragment that could be a marker split across frames, and flips to suppressed on a mid-stream marker. This is why prose starts a beat late; do not "fix" it by streaming raw deltas, or a tool-calling turn will spray XML into the UI and then be retroactively replaced. - `proxy/streamAdapter.mjs` — SSE chunk shaping for the streamed OpenAI response. diff --git a/packages/desktop/src/main/wpp-bridge/proxy/jsonToolFormat.mjs b/packages/desktop/src/main/wpp-bridge/proxy/jsonToolFormat.mjs index a8a6597151..5cb97f43a7 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/jsonToolFormat.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/jsonToolFormat.mjs @@ -3,13 +3,14 @@ import { parseToolArguments } from "./toolArguments.mjs"; // Render an OpenAI tool_call back into the JSON protocol the GPT/"o1-code-builder" agent emits, // so prior tool calls replayed into the transcript match the format the model is asked to produce // (see TOOL_CALL_SYSTEM_REMINDER_JSON). The proxy parses this same shape back out in -// src/toolCallNormalizer.mjs (parseTrailingToolCallJsons / parseLeadingToolCallJson). +// toolCallNormalizer.mjs, keeping the call id so a replayed call keeps its identity. // -// Mirrors src/anthropicToolFormat.mjs's formatAnthropicToolCall, but for JSON instead of XML. +// Mirrors anthropicToolFormat.mjs's formatAnthropicToolCall, but for JSON instead of XML. export function formatJsonToolCall(toolCall) { const fn = toolCall?.function || toolCall; const name = fn?.name || "unknown"; + const id = toolCall?.id || fn?.id || ""; const args = parseToolArguments(fn?.arguments); - return JSON.stringify({ type: "tool_call", tool: name, args }); + return JSON.stringify(id ? { type: "tool_call", id, tool: name, args } : { type: "tool_call", tool: name, args }); } diff --git a/packages/desktop/src/main/wpp-bridge/proxy/messageSerializer.test.mjs b/packages/desktop/src/main/wpp-bridge/proxy/messageSerializer.test.mjs index fc6b2f9ec5..20ceb9d5ef 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/messageSerializer.test.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/messageSerializer.test.mjs @@ -138,7 +138,7 @@ describe("CookieMonster request envelope", () => { test("keeps the established Opus envelope byte-identical", () => { const raw = serializeChatCompletionRequest(framed()) expect(new Bun.CryptoHasher("sha256").update(raw).digest("hex")).toBe( - "87f727b61b18d52b89403822ca36d1bc8dbe199c97ca91b32780c20cb66ba771", + "6e2449b2652abfb891eba2713a61d0be7a98318b02a6f2aac4ec2581de2468a9", ) }) diff --git a/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.mjs b/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.mjs index 0a4142ab9b..cade824c5c 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.mjs @@ -451,7 +451,7 @@ export async function handleChatCompletions(request, response, body, { bridge = tool_calls: undefined, finish_reason: "stop" } - : stripTaskCompleteMarker(chooseAssistantResponse(finalText, toolCallParts, o1CodeRun.response.alternateAssistantTexts)); + : stripTaskCompleteMarker(chooseAssistantResponse(finalText, toolCallParts, o1CodeRun.response.alternateAssistantTexts, { tools: body.tools })); if (!isCompaction) { normalizeToolCallArguments(normalized.tool_calls, body.tools); } @@ -643,7 +643,7 @@ export function shouldRecoverMissingRequiredToolCall(body, run, { isCompaction = const finalText = run?.response?.finalText || run?.finalText || ""; const toolCallParts = run?.response?.toolCallParts || run?.toolCallParts || {}; - const normalized = chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts(run)); + const normalized = chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts(run), { tools: body.tools }); if (normalized?.tool_calls?.length) return false; if (!activeTurnHasToolProgress(body)) return true; if (!commentaryPhaseEnabled(body)) return false; @@ -662,7 +662,7 @@ export function shouldRecoverIncompleteTask(body, run, { isCompaction = false } if (isCompaction || !Array.isArray(body?.tools) || body.tools.length === 0) return false; const finalText = run?.response?.finalText || run?.finalText || ""; const toolCallParts = run?.response?.toolCallParts || run?.toolCallParts || {}; - const normalized = chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts(run)); + const normalized = chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts(run), { tools: body.tools }); if (normalized?.tool_calls?.length) return false; const content = normalized?.content || ""; return !hasTaskCompleteMarker(content) diff --git a/packages/desktop/src/main/wpp-bridge/proxy/streamGate.mjs b/packages/desktop/src/main/wpp-bridge/proxy/streamGate.mjs index 50b85f4fd4..92fcbefd1a 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/streamGate.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/streamGate.mjs @@ -173,12 +173,11 @@ function trailingMarkerPrefixLength(text) { return longest; } -// True when the LEADING non-whitespace content looks like the start of a tool call. Mirrors the -// signals toolCallNormalizer.mjs keys on so the live decision matches the final normalization: -// Anthropic XML (optionally wrapped in a leading code fence) or a leading JSON object (which may -// be a {"type":"tool_call"} or delegated-agent call). A leading "{" is treated conservatively as -// a potential tool call — prose almost never starts with a brace, and if it turns out to be -// content the finalize path still emits it as a trailing diff (just not streamed live). +// True when the LEADING non-whitespace content might start a tool call. This is deliberately broader +// than toolCallNormalizer.mjs, which treats fenced markup as an example and runs only typed JSON for +// a listed tool. Here, XML markup (even inside a leading code fence) or any leading JSON object +// suppresses live output. Prose almost never starts that way, and over-suppressing only delays it: +// when the final normalization finds no call, finalizeStreamedCompletion still emits the content. export function leadingLooksLikeToolCall(text) { const trimmed = String(text || "").trimStart(); diff --git a/packages/desktop/src/main/wpp-bridge/proxy/toolCallNormalizer.mjs b/packages/desktop/src/main/wpp-bridge/proxy/toolCallNormalizer.mjs index 1096d36ecd..07f3878512 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/toolCallNormalizer.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/toolCallNormalizer.mjs @@ -1,244 +1,318 @@ import { looksLikeIncompleteAnthropicToolCall } from "./anthropicToolFormat.mjs"; -export function chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts = []) { - const fromText = normalizeAssistantMessage(finalText); - const fromParts = normalizeFromToolCallParts(toolCallParts); +// Turns model text into OpenAI tool_calls. Only the two transport protocols count, and only outside +// Markdown code: fenced blocks and inline code hold examples, so they never run. +// - XML: a function_calls wrapper holding invoke elements with parameter values. +// - JSON: an object whose type is "tool_call", with tool and args fields. +// Pass the request's tools as options.tools. XML values are then typed by each tool's schema, and a +// JSON call must name a listed tool, so JSON that only appears in an answer stays prose. +export function chooseAssistantResponse(finalText, toolCallParts, alternateAssistantTexts = [], options = {}) { + const fromText = attemptNormalize(finalText, options); - if (fromText?.finish_reason === "tool_calls" && fromText.tool_calls?.length) { - return fromText; + if (fromText.response?.finish_reason === "tool_calls" && fromText.response.tool_calls?.length) { + return fromText.response; } + const fromParts = normalizeFromToolCallParts(toolCallParts); + if (fromParts?.tool_calls?.length) { - return withPreamble(fromParts, finalText); + return withPreamble(fromParts, finalText, options); } - const fromAlternate = alternateAssistantTexts - .map((content) => normalizeAssistantMessage(content)) + // A malformed candidate must not hide a later valid one, so each is parsed on its own. + const fromAlternate = (Array.isArray(alternateAssistantTexts) ? alternateAssistantTexts : []) + .map((content) => attemptNormalize(content, options).response) .find((response) => response?.finish_reason === "tool_calls" && response.tool_calls?.length); if (fromAlternate) { - return withPreamble(fromAlternate, finalText); + return withPreamble(fromAlternate, finalText, options); } - return fromText; + if (fromText.error) { + throw fromText.error; + } + + return fromText.response; } export function normalizeFromToolCallParts(toolCallParts) { - const parts = Object.values(toolCallParts || {}).filter((p) => p?.name); + const parts = Object.values(toolCallParts || {}).filter((part) => part?.name); if (parts.length === 0) { return null; } - const tool_calls = parts.map((part) => ({ - id: part.id || `call_${crypto.randomUUID().replace(/-/g, "")}`, - type: "function", - function: { - name: part.name, - arguments: part.arguments || "{}" - } - })); - - return { - content: null, - tool_calls, - finish_reason: "tool_calls" - }; + return toolCallResponse( + null, + parts.map((part) => ({ id: part.id, name: part.name, arguments: part.arguments || "{}" })) + ); } -export function normalizeAssistantMessage(content) { - const unwrappedContent = unwrapSingleCodeFence(content); - const extractionContent = unwrappedContent === content - ? removeCodeFencedBlocks(content) - : unwrappedContent; - - // A real XML tool call is emitted bare (the system prompt forbids fencing it), so parse it from - // the RAW text starting at the first UNFENCED /. This keeps code fences - // INSIDE a parameter value intact — stripping them (removeCodeFencedBlocks) decapitates the - // closing when the model writes a markdown/code file, dropping the `content` arg. - // A fenced *example* has its marker inside a fence, so it isn't found here and stays inert. - let xmlCalls = null; - let xmlPreamble = null; - let incompleteXml = false; - if (unwrappedContent !== content) { - xmlCalls = parseAnthropicXmlToolCalls(unwrappedContent); - } else { - const start = unfencedToolCallStart(content); - if (start >= 0) { - const xmlContent = content.slice(start); - xmlCalls = parseAnthropicXmlToolCalls(xmlContent); - xmlPreamble = content.slice(0, start).trimEnd() || null; - incompleteXml = !xmlCalls && looksLikeIncompleteAnthropicToolCall(xmlContent); - } +export function normalizeAssistantMessage(content, options = {}) { + if (typeof content !== "string") { + return proseResponse(content); } - if (xmlCalls && xmlCalls.length > 0) { - return { - content: xmlPreamble, - tool_calls: xmlCalls.map((call) => ({ - id: call.id || `call_${crypto.randomUUID().replace(/-/g, "")}`, - type: "function", - function: { - name: call.name, - arguments: JSON.stringify(call.args || {}) - } - })), - finish_reason: "tool_calls" - }; - } + const schemas = toolSchemas(options.tools); + const code = markdownCodeRanges(content); - if (incompleteXml) { - const error = new Error("The WPP agent returned an incomplete XML tool call."); - error.statusCode = 502; - error.type = "o1_code_incomplete_tool_call"; - throw error; - } + // A real XML call is emitted bare, so parse from the first marker outside Markdown code. Code + // fences inside its parameter values come after that marker and stay part of the values. + const xmlStart = firstUnfencedMarker(content, code); - const jsonToolCalls = parseTrailingToolCallJsons(extractionContent); + if (xmlStart >= 0) { + const xmlContent = content.slice(xmlStart); + const xml = parseXmlToolCalls(xmlContent, schemas); - if (jsonToolCalls?.length) { - return { - content: null, - tool_calls: toolCallsFromJsonCalls(dedupeToolCalls(jsonToolCalls)), - finish_reason: "tool_calls" - }; + if (xml.incomplete || (xml.calls.length === 0 && looksLikeIncompleteAnthropicToolCall(xmlContent))) { + throw incompleteToolCallError(); + } + + if (xml.calls.length > 0) { + return toolCallResponse(content.slice(0, xmlStart).trimEnd() || null, xml.calls); + } } - // A model may emit several calls as one trailing JSON array ([{...},{...}]) rather than - // back-to-back objects; accept that shape too. - const arrayToolCalls = parseTrailingToolCallArray(extractionContent); + // XML markup is unambiguous, so its unknown names still reach OpenCode, which reports them to the + // model. JSON is checked against the request's tools because answers often contain JSON. + const jsonCalls = typedJsonToolCalls(content, code).filter((call) => allowedTool(call.tool, schemas)); - if (arrayToolCalls?.length) { - return { - content: null, - tool_calls: toolCallsFromJsonCalls(dedupeToolCalls(arrayToolCalls)), - finish_reason: "tool_calls" - }; + if (jsonCalls.length > 0) { + return toolCallResponse( + null, + dedupeToolCalls(jsonCalls).map((call) => ({ id: call.id, name: call.tool, args: call.args })) + ); } - const leadingJsonToolCall = parseLeadingToolCallJson(extractionContent); + const whole = parseWholeMessageJson(content); - if (leadingJsonToolCall) { - return { - content: null, - tool_calls: toolCallsFromJsonCalls([leadingJsonToolCall]), - finish_reason: "tool_calls" - }; + if (whole?.type === "final") { + return proseResponse(whole.message || ""); } - const embeddedJsonToolCalls = parseEmbeddedToolCallJsons(extractionContent); + const delegated = delegatedSubagentCall(whole); - if (embeddedJsonToolCalls?.length) { - return { - content: null, - tool_calls: toolCallsFromJsonCalls(dedupeToolCalls(embeddedJsonToolCalls)), - finish_reason: "tool_calls" - }; + if (delegated && allowedTool(delegated.name, schemas)) { + return toolCallResponse(null, [delegated]); } - const parsed = parseDelegatedAgentJson(extractionContent) - || parseStrictJson(unwrappedContent); + return proseResponse(content); +} - if (!parsed) { - return { - content, - tool_calls: undefined, - finish_reason: "stop" - }; +export function parseAnthropicXmlToolCalls(content, options = {}) { + if (typeof content !== "string") { + return null; } - if (parsed.type === "final") { - return { - content: parsed.message || "", - tool_calls: undefined, - finish_reason: "stop" - }; + const calls = parseXmlToolCalls(content, toolSchemas(options.tools)).calls; + return calls.length > 0 ? calls : null; +} + +function attemptNormalize(content, options) { + try { + return { response: normalizeAssistantMessage(content, options) }; + } catch (error) { + return { error }; } +} - if (parsed.type === "tool_call" && parsed.tool) { - return { - content: null, - tool_calls: [ - { - id: parsed.id || `call_${crypto.randomUUID().replace(/-/g, "")}`, - type: "function", - function: { - name: parsed.tool, - arguments: JSON.stringify(parsed.args || {}) - } - } - ], - finish_reason: "tool_calls" - }; +function withPreamble(message, content, options) { + const text = typeof content === "string" ? content.trimEnd() : ""; + + if (!text) { + return message; } + // Text that is itself a tool call, even a broken one, is not a preamble. + const parsed = attemptNormalize(text, options); + return parsed.error || parsed.response.tool_calls?.length ? message : { ...message, content: text }; +} + +function proseResponse(content) { + return { content, tool_calls: undefined, finish_reason: "stop" }; +} + +function toolCallResponse(content, calls) { + const seen = new Set(); + return { content, - tool_calls: undefined, - finish_reason: "stop" + tool_calls: calls.map((call) => { + const given = call.id == null ? "" : String(call.id); + // OpenCode keys tool results by id, so a repeated id gets a fresh one instead of colliding. + const id = given && !seen.has(given) ? given : generatedCallId(); + seen.add(id); + return { + id, + type: "function", + function: { + name: call.name, + arguments: call.arguments ?? JSON.stringify(call.args || {}) + } + }; + }), + finish_reason: "tool_calls" }; } -function withPreamble(message, content) { - const text = typeof content === "string" ? content.trimEnd() : ""; +function generatedCallId() { + return `call_${crypto.randomUUID().replace(/-/g, "")}`; +} - if (!text || normalizeAssistantMessage(text).tool_calls?.length) { - return message; - } +function incompleteToolCallError() { + const error = new Error("The WPP agent returned an incomplete XML tool call."); + error.statusCode = 502; + error.type = "o1_code_incomplete_tool_call"; + return error; +} - return { ...message, content: text }; +function toolSchemas(tools) { + return new Map((Array.isArray(tools) ? tools : []).flatMap((tool) => { + const fn = tool?.function || tool; + return typeof fn?.name === "string" ? [[fn.name, fn.parameters?.properties || {}]] : []; + })); } -// Index of the first / marker that is NOT inside a ``` fenced block, or -// -1. Fences are paired left-to-right (open/close); a real bare tool call's marker precedes any -// fences in its own parameter values, so it reads as unfenced, while a fenced example's marker sits -// inside a span and is skipped. -function unfencedToolCallStart(content) { - if (typeof content !== "string") { - return -1; +function allowedTool(name, schemas) { + return schemas.size === 0 || schemas.has(name); +} + +// Markdown code that holds examples: fenced blocks (backtick or tilde fences, closed by a matching +// fence or running to the end of the text, as in CommonMark) and inline code on a single line. +// Fences are accepted at any indent so examples nested in list items stay inert too. +function markdownCodeRanges(text) { + const ranges = []; + let fence = null; + let offset = 0; + + for (const line of text.split("\n")) { + const marker = line.match(/^[ \t]*(`{3,}|~{3,})/); + const lineEnd = offset + line.length; + + if (fence) { + const closes = marker + && marker[1][0] === fence.char + && marker[1].length >= fence.size + && !line.slice(marker[0].length).trim(); + + if (closes) { + ranges.push({ start: fence.start, end: lineEnd }); + fence = null; + } + } else if (marker && !(marker[1][0] === "`" && line.slice(marker[0].length).includes("`"))) { + fence = { char: marker[1][0], size: marker[1].length, start: offset }; + } else { + ranges.push(...inlineCodeRanges(line, offset)); + } + + offset = lineEnd + 1; } - const ticks = []; - const fence = /```/g; - let f; - while ((f = fence.exec(content)) !== null) { - ticks.push(f.index); + if (fence) { + ranges.push({ start: fence.start, end: text.length }); } - const spans = []; - for (let i = 0; i + 1 < ticks.length; i += 2) { - spans.push([ticks[i], ticks[i + 1] + 3]); + return ranges; +} + +function inlineCodeRanges(line, offset) { + const runs = [...line.matchAll(/`+/g)]; + const ranges = []; + + for (let index = 0; index < runs.length; index += 1) { + const close = runs.findIndex((run, later) => later > index && run[0].length === runs[index][0].length); + + if (close < 0) { + continue; + } + + ranges.push({ start: offset + runs[index].index, end: offset + runs[close].index + runs[close][0].length }); + index = close; } + return ranges; +} + +function rangeAt(index, ranges) { + return ranges.find((range) => index >= range.start && index < range.end); +} + +function firstUnfencedMarker(content, code) { const marker = / m.index >= start && m.index < end)) { - return m.index; + let match; + + while ((match = marker.exec(content)) !== null) { + const range = rangeAt(match.index, code); + + if (!range) { + return match.index; } + + marker.lastIndex = Math.max(marker.lastIndex, range.end); } return -1; } -export function parseAnthropicXmlToolCalls(content) { - if (typeof content !== "string" || !content.includes("]*)>([\s\S]*?)<\/invoke>/gi; + const open = /]*)>/gi; + let cursor = 0; let match; - while ((match = invokeRegex.exec(content)) !== null) { + while ((match = open.exec(content)) !== null) { + const bodyStart = open.lastIndex; + const close = invokeClose(content, bodyStart); + + if (!close) { + return { calls, incomplete: true }; + } + const attributes = parseXmlAttributes(match[1]); - const name = attributes.name; - if (!name) continue; - const body = match[2]; - calls.push(normalizeAnthropicInvoke({ id: attributes.id || "", name, args: extractInvokeParameters(body, name) })); + cursor = close.end; + open.lastIndex = close.end; + + if (attributes.name) { + calls.push(normalizeAnthropicInvoke({ + id: attributes.id || "", + name: attributes.name, + args: extractInvokeParameters(content.slice(bodyStart, close.start), attributes.name, schemas.get(attributes.name)) + })); + } } - return calls.length > 0 ? calls : null; + return { calls, incomplete: calls.length > 0 && truncatedInvokeTail(content.slice(cursor)) }; +} + +// An invoke ends at the first close tag followed by another invoke, the wrapper's close tag or the +// end of the text. A close tag inside a parameter value is followed by more value text instead. +// Without such a close, the last close tag is used so a call followed by stray prose still parses. +function invokeClose(content, from) { + const close = /<\/invoke\s*>/gi; + const structural = /\s*(?:|$)/y; + close.lastIndex = from; + let last = null; + let match; + + while ((match = close.exec(content)) !== null) { + const found = { start: match.index, end: close.lastIndex }; + structural.lastIndex = found.end; + + if (structural.test(content)) { + return found; + } + + last = found; + } + + return last; +} + +// After the last complete invoke, only whitespace, the wrapper's close tag or prose may follow. A +// partial invoke opening there means the output was cut off partway through the batch. +function truncatedInvokeTail(rest) { + const tail = rest.trim(); + return tail !== "" && (tail.startsWith(" values from an invoke body. Each value runs from its opening tag to -// the EARLIEST terminator: the protocol's , a name-based close the model sometimes -// emits instead (e.g. for name="content"), or the next opening. This -// tolerates the common Opus failure mode of closing the final/large parameter with the wrong tag or -// omitting it entirely, which otherwise drops the argument (observed: `write` losing `content`). -function extractInvokeParameters(body, toolName) { +// A parameter value is raw text up to a structural close: a close tag (the protocol's, or one named +// after the parameter, which Opus sometimes emits instead) followed by the next parameter opening or +// by the end of the invoke. Close tags inside the value are followed by more value text, so they no +// longer truncate it. A CDATA-wrapped value is taken verbatim. +function extractInvokeParameters(body, toolName, properties = {}) { const args = {}; - const openRegex = //gi; - const opens = []; - let open; + let open = firstParameterOpen(body, 0); - while ((open = openRegex.exec(body)) !== null) { - opens.push({ name: open[2], valueStart: openRegex.lastIndex, openStart: open.index }); - } - - for (let i = 0; i < opens.length; i += 1) { - const { name, valueStart } = opens[i]; - const hardEnd = i + 1 < opens.length ? opens[i + 1].openStart : body.length; - const slice = body.slice(valueStart, hardEnd); - const raw = decodeXmlEntities(trimParameterClose(slice, name)); + while (open) { + const value = cdataParameterValue(body, open) || plainParameterValue(body, open); + const schema = Object.hasOwn(properties, open.name) ? properties[open.name] : undefined; // Site-tool arguments are JSON text, not a nested transport object. - args[name] = toolName === "browser_execute_site_tool" && name === "arguments" ? raw : coerceParameterValue(raw); + args[open.name] = toolName === "browser_execute_site_tool" && open.name === "arguments" + ? value.text + : coerceParameterValue(value.text, schema); + open = value.next; } return args; } -function trimParameterClose(value, name) { - const ends = []; - const standard = value.indexOf(""); - if (standard >= 0) { - ends.push(standard); +function firstParameterOpen(body, from) { + const open = //gi; + open.lastIndex = from; + const match = open.exec(body); + return match ? { name: match[2], start: match.index, valueStart: open.lastIndex } : null; +} + +function parameterOpenAt(body, index) { + const open = /\s*/iy; + open.lastIndex = index; + const match = open.exec(body); + return match ? { name: match[2], start: match.index, valueStart: open.lastIndex } : null; +} + +function closeTagPattern(name, flags) { + return new RegExp(`<\\/(?:parameter|${escapeRegExp(name)})\\s*>`, `i${flags}`); +} + +function parameterBoundaryAt(body, index, name) { + const close = closeTagPattern(name, "y"); + close.lastIndex = index; + + if (!close.test(body)) { + return null; } - const named = value.search(new RegExp(``, "i")); - if (named >= 0) { - ends.push(named); + + const next = parameterOpenAt(body, close.lastIndex); + + if (next) { + return { next }; + } + + return body.slice(close.lastIndex).trim() === "" ? { next: null } : null; +} + +function plainParameterValue(body, open) { + const close = closeTagPattern(open.name, "g"); + close.lastIndex = open.valueStart; + let match; + + while ((match = close.exec(body)) !== null) { + const boundary = parameterBoundaryAt(body, match.index, open.name); + + if (boundary) { + return { text: decodeXmlEntities(body.slice(open.valueStart, match.index)), next: boundary.next }; + } } - return ends.length > 0 ? value.slice(0, Math.min(...ends)) : value; + // No structural close: the model omitted or mangled it. Keep the legacy guess of ending at the + // next opening or the end of the invoke, cut at the first close tag in between. + const next = firstParameterOpen(body, open.valueStart); + const slice = body.slice(open.valueStart, next ? next.start : body.length); + const stray = closeTagPattern(open.name, "").exec(slice); + return { text: decodeXmlEntities(stray ? slice.slice(0, stray.index) : slice), next }; +} + +function cdataParameterValue(body, open) { + const cdata = /\s*", innerStart); end >= 0; end = body.indexOf("]]>", end + 1)) { + const closeAt = end + 3 + body.slice(end + 3).search(/\S|$/); + const boundary = parameterBoundaryAt(body, closeAt, open.name); + + if (boundary) { + return { text: body.slice(innerStart, end), next: boundary.next }; + } + } + + return null; } function escapeRegExp(text) { @@ -316,197 +448,196 @@ function normalizeAnthropicInvoke(call) { return call; } +const XML_ENTITIES = { amp: "&", lt: "<", gt: ">", quot: "\"", apos: "'" }; + +// One pass, so an escaped ampersand is never decoded twice. Numeric references cover all of +// Unicode; invalid ones stay as written. function decodeXmlEntities(text) { - return text - .replace(/</g, "<") - .replace(/>/g, ">") - .replace(/"/g, "\"") - .replace(/'/g, "'") - .replace(/&#(\d+);/g, (_, code) => String.fromCharCode(Number(code))) - .replace(/&/g, "&"); -} + return text.replace(/&(?:#(\d+)|#x([0-9a-fA-F]+)|(amp|lt|gt|quot|apos));/g, (entity, decimal, hex, name) => { + if (name) { + return XML_ENTITIES[name]; + } -function coerceParameterValue(raw) { - const trimmed = raw.trim(); + const code = decimal ? Number(decimal) : Number.parseInt(hex, 16); + return code > 0 && code <= 0x10ffff && (code < 0xd800 || code > 0xdfff) ? String.fromCodePoint(code) : entity; + }); +} - if (trimmed === "") { +// A value stays the exact text unless the tool's schema rules out a string. Without a schema, keep +// the legacy guess so unknown tools still receive booleans, numbers and JSON. +function coerceParameterValue(raw, schema) { + if (schemaTypes(schema).has("string")) { return raw; } + const trimmed = raw.trim(); + + if (trimmed === "") return raw; if (trimmed === "true") return true; if (trimmed === "false") return false; if (trimmed === "null") return null; - if ((trimmed.startsWith("{") && trimmed.endsWith("}")) || - (trimmed.startsWith("[") && trimmed.endsWith("]"))) { - try { - return parseJsonWithRawWindowsPathRepair(trimmed); - } catch { - return raw; - } + if ((trimmed.startsWith("{") && trimmed.endsWith("}")) || (trimmed.startsWith("[") && trimmed.endsWith("]"))) { + return parseJsonLenient(trimmed) ?? raw; } - if (/^-?\d+$/.test(trimmed)) { - const n = Number(trimmed); - if (Number.isSafeInteger(n)) return n; - } - - if (/^-?\d+\.\d+$/.test(trimmed)) { - return Number(trimmed); - } + if (/^-?\d+$/.test(trimmed) && Number.isSafeInteger(Number(trimmed))) return Number(trimmed); + if (/^-?\d+\.\d+$/.test(trimmed)) return Number(trimmed); return raw; } -function unwrapSingleCodeFence(content) { - if (typeof content !== "string") { - return content; +function schemaTypes(schema) { + if (!schema || typeof schema !== "object") { + return new Set(); } - const match = content.trim().match(/^```(?:json|xml)?\s*\n?([\s\S]*?)\n?```$/); + const declared = [schema.type].flat().filter((type) => typeof type === "string"); + const values = [...(Array.isArray(schema.enum) ? schema.enum : []), ...("const" in schema ? [schema.const] : [])] + .map(jsonType); + const nested = [schema.anyOf, schema.oneOf, schema.allOf] + .filter(Array.isArray) + .flatMap((list) => list.flatMap((item) => [...schemaTypes(item)])); - return match ? match[1] : content; + return new Set([...declared, ...values, ...nested]); } -function removeCodeFencedBlocks(content) { - if (typeof content !== "string") { - return content; - } - - return content.replace(/```[\s\S]*?```/g, ""); +function jsonType(value) { + if (value === null) return "null"; + if (Array.isArray(value)) return "array"; + return typeof value; } -function parseStrictJson(content) { - if (typeof content !== "string") { - return null; +function typedJsonToolCalls(content, code) { + if (!content.includes("tool_call")) { + return []; } - const trimmed = content.trim(); + return jsonObjectCandidates(content, code) + .map((candidate) => parseJsonLenient(candidate)) + .filter(isTypedToolCall); +} - if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) { - return null; - } +// Every balanced object outside Markdown code that starts like JSON. Each start is scanned on its +// own, so a stray brace in prose cannot swallow a later call. +function jsonObjectCandidates(content, code) { + const candidates = []; + const start = /\{\s*"/g; + let lastEnd = 0; + let match; - try { - return parseJsonWithRawWindowsPathRepair(trimmed); - } catch { - return null; - } -} + while ((match = start.exec(content)) !== null) { + const range = rangeAt(match.index, code); -function parseJsonWithRawWindowsPathRepair(content) { - const repaired = repairRawWindowsBackslashesInJsonStrings(content); + // Backticks inside an accepted object's strings are data. A range they open can pair with + // backticks in a later object on the same line, so it must not hide that object. + if (range && range.start >= lastEnd) { + start.lastIndex = Math.max(start.lastIndex, range.end); + continue; + } - if (repaired !== content) { - try { - return JSON.parse(repaired); - } catch { - // Fall through to the original parse so callers keep their existing behavior. + const end = matchingBrace(content, match.index); + + if (end >= 0) { + candidates.push(content.slice(match.index, end)); + start.lastIndex = end; + lastEnd = end; } } - return JSON.parse(content); + return candidates; } -function repairRawWindowsBackslashesInJsonStrings(content) { - if (typeof content !== "string" || !/[A-Za-z]:\\/.test(content)) { - return content; - } +function matchingBrace(text, start) { + let depth = 0; + let inString = false; + let escaped = false; - let output = ""; - let index = 0; + for (let index = start; index < text.length; index += 1) { + const char = text[index]; - while (index < content.length) { - if (content[index] !== "\"") { - output += content[index]; - index += 1; + if (inString) { + if (escaped) { + escaped = false; + } else if (char === "\\") { + escaped = true; + } else if (char === "\"") { + inString = false; + } continue; } - const start = index; - index += 1; - const valueStart = index; - let slashCount = 0; - - while (index < content.length) { - const char = content[index]; + if (char === "\"") { + inString = true; + } else if (char === "{") { + depth += 1; + } else if (char === "}") { + depth -= 1; - if (char === "\"" && slashCount % 2 === 0) { - break; + if (depth === 0) { + return index + 1; } + } + } - if (char === "\\") { - slashCount += 1; - } else { - slashCount = 0; - } + return -1; +} - index += 1; - } +function isTypedToolCall(value) { + return Boolean(value) + && typeof value === "object" + && value.type === "tool_call" + && typeof value.tool === "string" + && value.tool.trim() !== ""; +} - const rawValue = content.slice(valueStart, index); - const value = hasRawWindowsBackslash(rawValue) - ? rawValue.replace(/(^|[^\\])\\(?!\\)/g, "$1\\\\") - : rawValue; +// Identical calls with the same id, or with no id, are one call repeated in the text. Calls that +// carry different ids are distinct even when their arguments match. +function dedupeToolCalls(calls) { + const seen = new Set(); - output += content[start] + value; + return calls.filter((call) => { + const key = JSON.stringify([call.id ?? "", call.tool, call.args ?? {}]); - if (index < content.length) { - output += content[index]; - index += 1; + if (seen.has(key)) { + return false; } - } - return output; + seen.add(key); + return true; + }); } -function hasRawWindowsBackslash(value) { - return /[A-Za-z]:/.test(value) - && /(^|[^\\])\\(?!\\)/.test(value); -} +function parseWholeMessageJson(content) { + const trimmed = content.trim(); -function parseDelegatedAgentJson(content) { - if (typeof content !== "string") { + if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) { return null; } - for (const candidate of extractJsonObjects(content).reverse()) { - try { - const parsed = JSON.parse(candidate); - const name = typeof parsed?.name === "string" ? parsed.name.trim() : ""; - - if (!name || parsed?.type === "tool_call") { - continue; - } + const parsed = parseJsonLenient(trimmed); + return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : null; +} - const args = { ...parsed }; - delete args.name; - - if (isSubagentName(name) && typeof args.prompt === "string") { - return { - type: "tool_call", - tool: "task", - args: { - subagent_type: name.toLowerCase(), - prompt: args.prompt, - description: typeof args.description === "string" - ? args.description - : summarizeTaskDescription(args.prompt) - } - }; - } +// Legacy delegation shape: a reply that is only a JSON object naming a subagent, with its prompt. +function delegatedSubagentCall(parsed) { + const name = typeof parsed?.name === "string" ? parsed.name.trim() : ""; - return { - type: "tool_call", - tool: name, - args - }; - } catch { - continue; - } + if (!isSubagentName(name) || typeof parsed.prompt !== "string") { + return null; } - return null; + return { + id: "", + name: "task", + args: { + subagent_type: name.toLowerCase(), + prompt: parsed.prompt, + description: typeof parsed.description === "string" + ? parsed.description + : summarizeTaskDescription(parsed.prompt) + } + }; } function isSubagentName(name) { @@ -524,275 +655,76 @@ function summarizeTaskDescription(prompt) { return words.length > 48 ? `${words.slice(0, 45)}...` : words; } -function toolCallsFromJsonCalls(calls) { - return calls.map((call) => ({ - id: call.id || `call_${crypto.randomUUID().replace(/-/g, "")}`, - type: "function", - function: { - name: call.tool, - arguments: JSON.stringify(call.args || {}) - } - })); -} - -// Accept a trailing JSON array whose elements are all tool_call objects, e.g. -// [{"type":"tool_call","tool":"a","args":{}},{"type":"tool_call","tool":"b","args":{}}]. -function parseTrailingToolCallArray(content) { - if (typeof content !== "string") { - return null; - } - - const arrays = extractJsonArraysWithPositions(content); - - if (arrays.length === 0) { - return null; - } - - const last = arrays[arrays.length - 1]; - - if (content.slice(last.end).trim()) { - return null; - } - - let parsed; - - try { - parsed = parseJsonWithRawWindowsPathRepair(last.text); - } catch { - return null; - } - - if (!Array.isArray(parsed) || parsed.length === 0) { - return null; - } - - if (!parsed.every((call) => call?.type === "tool_call" && call.tool)) { - return null; - } - - return parsed; -} - -function parseTrailingToolCallJsons(content) { - if (typeof content !== "string") { - return null; - } - - const objects = extractJsonObjectsWithPositions(content); - const calls = []; - let cursor = content.length; - - for (let index = objects.length - 1; index >= 0; index -= 1) { - const candidate = objects[index]; - - if (content.slice(candidate.end, cursor).trim()) { - break; - } - - let parsed; - - try { - parsed = parseJsonWithRawWindowsPathRepair(candidate.text); - } catch (err) { - process.stderr.write(`[toolCallNormalizer] JSON parse failed: ${err.message}\n`); - break; - } - - if (!(parsed?.type === "tool_call" && parsed.tool)) { - break; - } - - calls.unshift(parsed); - cursor = candidate.start; - } - - return calls.length > 0 ? calls : null; -} - -function parseLeadingToolCallJson(content) { - if (typeof content !== "string") { - return null; - } - - const trimmedStart = content.search(/\S/u); - - if (trimmedStart < 0) { - return null; - } - - const firstObject = extractJsonObjectsWithPositions(content) - .find((object) => object.start === trimmedStart); - - if (!firstObject) { - return null; - } - +// Strict JSON first, so valid escapes are never rewritten. Models sometimes leave Windows path +// backslashes unescaped, so only text that JSON rejects gets the path repair. Returns undefined +// when neither parse succeeds. +function parseJsonLenient(text) { try { - const parsed = parseJsonWithRawWindowsPathRepair(firstObject.text); - - return parsed?.type === "tool_call" && parsed.tool ? parsed : null; + return JSON.parse(text); } catch { - return null; - } -} - -// Collect every {"type":"tool_call",...} object embedded anywhere in the text, regardless of -// surrounding prose — the JSON analogue of parseAnthropicXmlToolCalls's global scan. -// Runs only AFTER the strict leading/trailing checks fail, so cleanly-shaped turns keep their -// existing behavior; this is the placement-tolerance path for models that narrate around their -// tool-call JSON ("I'll do X… {tool_call} …then Y"). Operates on the same fence-stripped -// extraction content as the XML scan, so fenced examples stay inert (XML parity). -function parseEmbeddedToolCallJsons(content) { - if (typeof content !== "string" || !content.includes("\"tool_call\"")) { - return null; - } - - const calls = []; - - for (const candidate of extractJsonObjectsWithPositions(content)) { - let parsed; - try { - parsed = parseJsonWithRawWindowsPathRepair(candidate.text); + return JSON.parse(repairRawWindowsBackslashesInJsonStrings(text)); } catch { - continue; - } - - if (parsed?.type === "tool_call" && parsed.tool) { - calls.push(parsed); + return undefined; } } - - return calls.length > 0 ? calls : null; } -function dedupeToolCalls(calls) { - const seen = new Set(); - const result = []; - - for (const call of calls) { - const key = JSON.stringify({ - tool: call.tool, - args: call.args || {} - }); - - if (seen.has(key)) { - continue; - } - - seen.add(key); - result.push(call); +function repairRawWindowsBackslashesInJsonStrings(content) { + if (typeof content !== "string" || !/[A-Za-z]:\\/.test(content)) { + return content; } - return result; -} - -function extractJsonObjects(content) { - return extractJsonObjectsWithPositions(content).map((object) => object.text); -} - -// Like extractJsonObjectsWithPositions but for top-level [...] arrays, with the same -// string/escape awareness so brackets inside string literals are ignored. -function extractJsonArraysWithPositions(content) { - const arrays = []; - let start = -1; - let depth = 0; - let inString = false; - let escaped = false; - - for (let i = 0; i < content.length; i++) { - const char = content[i]; - - if (inString) { - if (escaped) { - escaped = false; - } else if (char === "\\") { - escaped = true; - } else if (char === "\"") { - inString = false; - } - continue; - } + let output = ""; + let index = 0; - if (char === "\"") { - inString = true; + while (index < content.length) { + if (content[index] !== "\"") { + output += content[index]; + index += 1; continue; } - if (char === "[") { - if (depth === 0) { - start = i; - } - depth += 1; - continue; - } + const start = index; + index += 1; + const valueStart = index; + let slashCount = 0; - if (char === "]" && depth > 0) { - depth -= 1; + while (index < content.length) { + const char = content[index]; - if (depth === 0 && start !== -1) { - arrays.push({ - text: content.slice(start, i + 1), - start, - end: i + 1 - }); - start = -1; + if (char === "\"" && slashCount % 2 === 0) { + break; } - } - } - - return arrays; -} - -function extractJsonObjectsWithPositions(content) { - const objects = []; - let start = -1; - let depth = 0; - let inString = false; - let escaped = false; - for (let i = 0; i < content.length; i++) { - const char = content[i]; - - if (inString) { - if (escaped) { - escaped = false; - } else if (char === "\\") { - escaped = true; - } else if (char === "\"") { - inString = false; + if (char === "\\") { + slashCount += 1; + } else { + slashCount = 0; } - continue; - } - if (char === "\"") { - inString = true; - continue; + index += 1; } - if (char === "{") { - if (depth === 0) { - start = i; - } - depth += 1; - continue; - } + const rawValue = content.slice(valueStart, index); + const value = hasRawWindowsBackslash(rawValue) + ? rawValue.replace(/(^|[^\\])\\(?!\\)/g, "$1\\\\") + : rawValue; - if (char === "}" && depth > 0) { - depth -= 1; + output += content[start] + value; - if (depth === 0 && start !== -1) { - objects.push({ - text: content.slice(start, i + 1), - start, - end: i + 1 - }); - start = -1; - } + if (index < content.length) { + output += content[index]; + index += 1; } } - return objects; + return output; +} + +function hasRawWindowsBackslash(value) { + return /[A-Za-z]:/.test(value) + && /(^|[^\\])\\(?!\\)/.test(value); } export { looksLikeIncompleteAnthropicToolCall }; diff --git a/packages/desktop/src/main/wpp-bridge/proxy/toolCallNormalizer.test.mjs b/packages/desktop/src/main/wpp-bridge/proxy/toolCallNormalizer.test.mjs index a3979f8cc3..902e0dd993 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/toolCallNormalizer.test.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/toolCallNormalizer.test.mjs @@ -1,6 +1,223 @@ import { describe, expect, test } from "bun:test" +import { formatAnthropicToolCall } from "./anthropicToolFormat.mjs" +import { formatJsonToolCall } from "./jsonToolFormat.mjs" +import { CM_TASK_COMPLETE_PROTOCOL } from "./protocol.mjs" import { chooseAssistantResponse, normalizeAssistantMessage } from "./toolCallNormalizer.mjs" +// Markup is built at runtime so the source holds no literal tool call, which an agent editing this +// file through the bridge would otherwise trigger. +const lt = "<" +const amp = "&" +const fence = "`".repeat(3) +const param = (name, value) => `${lt}parameter name="${name}">${value}${lt}/parameter>` +const invoke = (name, params, id) => `${lt}invoke${id ? ` id="${id}"` : ""} name="${name}">${params}${lt}/invoke>` +const calls = (...invokes) => `${lt}function_calls>${invokes.join("")}${lt}/function_calls>` +const typed = (tool, args, id) => + JSON.stringify(id ? { type: "tool_call", id, tool, args } : { type: "tool_call", tool, args }) +const schema = (name, properties) => ({ + type: "function", + function: { name, parameters: { type: "object", properties } }, +}) +const tools = [ + schema("write", { filePath: { type: "string" }, content: { type: "string" } }), + schema("edit", { filePath: { type: "string" }, oldString: { type: "string" }, newString: { type: "string" } }), + schema("read", { filePath: { type: "string" }, limit: { type: "number" } }), + schema("question", { questions: { type: "array" } }), +] +const args = (response, index = 0) => JSON.parse(response.tool_calls[index].function.arguments) +const prose = (content) => ({ content, tool_calls: undefined, finish_reason: "stop" }) + +describe("ordinary text stays prose", () => { + test("JSON objects without the tool_call type are not calls", () => { + const pkg = JSON.stringify({ name: "cookiemonster", version: "1.0.0", private: true }, null, 2) + const inputs = [ + `Your package.json should contain:\n${pkg}\nThen run bun install.`, + pkg, + JSON.stringify({ name: "read", arguments: { filePath: "a.txt" } }), + `Result: ${JSON.stringify({ name: "read", path: "a.txt" })}`, + ] + for (const input of inputs) { + expect(normalizeAssistantMessage(input)).toEqual(prose(input)) + expect(normalizeAssistantMessage(input, { tools })).toEqual(prose(input)) + } + }) + + test("typed JSON that names an unlisted tool is not a call", () => { + const input = `The format is ${typed("tool_name", {})} and nothing else.` + expect(normalizeAssistantMessage(input, { tools })).toEqual(prose(input)) + }) + + test("fenced and inline-code examples never run", () => { + const xml = calls(invoke("write", param("content", "one"), "a")) + const json = typed("read", { filePath: "a.txt" }) + const inputs = [ + `${fence}xml\n${xml}\n${fence}`, + `Example:\n~~~xml\n${xml}\n~~~`, + `Example:\n${fence}xml\n${xml}`, + `Example:\n ${fence}xml\n ${xml}\n ${fence}\nDone.`, + `Use \`${xml}\` to write.`, + `${fence}json\n${json}\n${fence}`, + `The format is:\n${fence}json\n${json}\n${fence}\nUse it when needed.`, + `The format is \`${json}\`.`, + ] + for (const input of inputs) { + expect(normalizeAssistantMessage(input, { tools })).toEqual(prose(input)) + } + }) + + test("a real call after a fenced example still runs", () => { + const example = calls(invoke("read", param("filePath", "example.txt"))) + const preamble = `For example:\n${fence}xml\n${example}\n${fence}\nNow reading the real file.` + const response = normalizeAssistantMessage( + `${preamble}\n${calls(invoke("read", param("filePath", "a.txt"), "call-1"))}`, + { + tools, + }, + ) + expect(response.content).toBe(preamble) + expect(response.tool_calls.map((call) => call.id)).toEqual(["call-1"]) + expect(args(response)).toEqual({ filePath: "a.txt" }) + }) +}) + +describe("parameter values keep their bytes", () => { + const write = (content) => + normalizeAssistantMessage(calls(invoke("write", param("filePath", "f") + param("content", content))), { tools }) + + test("string-typed values are never coerced", () => { + for (const content of ["001", "true", "null", '\n{ "a": 1 }\n', "[1,2]", "-3.50"]) { + expect(args(write(content)).content).toBe(content) + } + }) + + test("non-string schema types and unknown tools still decode", () => { + const read = normalizeAssistantMessage(calls(invoke("read", param("filePath", "a.txt") + param("limit", "20"))), { + tools, + }) + expect(args(read)).toEqual({ filePath: "a.txt", limit: 20 }) + const question = normalizeAssistantMessage(calls(invoke("question", param("questions", '[{"question":"Q"}]'))), { + tools, + }) + expect(args(question).questions).toEqual([{ question: "Q" }]) + expect(args(normalizeAssistantMessage(calls(invoke("other", param("count", "3"))))).count).toBe(3) + }) + + test("close tags inside a value do not truncate it", () => { + const values = [ + `before${lt}/parameter>after`, + `${lt}feed>${lt}entry>${lt}content>x${lt}/content>${lt}/entry>${lt}/feed>`, + `a${lt}/invoke>b`, + `${fence}md\ncode\n${fence}\n`, + "line1\r\nline2\r\n", + ] + for (const content of values) { + expect(args(write(content)).content).toBe(content) + } + const oldString = `a${lt}/oldString>b` + const edit = normalizeAssistantMessage( + calls(invoke("edit", param("oldString", oldString) + param("newString", "c"))), + { + tools, + }, + ) + expect(args(edit)).toEqual({ oldString, newString: "c" }) + }) + + test("a final value closed by its own name, or not closed at all, is still accepted", () => { + const opening = `${lt}function_calls>${lt}invoke name="write">${lt}parameter name="content">body` + const ending = `${lt}/invoke>${lt}/function_calls>` + expect(args(normalizeAssistantMessage(`${opening}${lt}/content>${ending}`, { tools })).content).toBe("body") + expect(args(normalizeAssistantMessage(`${opening}${ending}`, { tools })).content).toBe("body") + }) + + test("CDATA values are taken verbatim", () => { + const content = `x${lt}/parameter>y ${amp}amp; z` + const value = `\n${lt}![CDATA[${content}]]>\n` + expect(args(write(value)).content).toBe(content) + }) + + test("entities decode once, including astral numeric references", () => { + const encoded = `${amp}lt; ${amp}amp;lt; ${amp}#128512; ${amp}#x1F600; ${amp}#xD800;` + expect(args(write(encoded)).content).toBe(`< ${amp}lt; \u{1F600} \u{1F600} ${amp}#xD800;`) + }) + + test("valid JSON escapes are not rewritten, and raw Windows paths are still repaired", () => { + const content = `Install to C:\\Program Files\nthen restart.\n${fence}js\nx\n${fence}after` + const response = normalizeAssistantMessage(typed("write", { filePath: "C:\\notes.md", content }), { tools }) + expect(args(response)).toEqual({ filePath: "C:\\notes.md", content }) + const raw = '{"type":"tool_call","tool":"read","args":{"filePath":"C:\\Users\\me\\a.txt"}}' + expect(args(normalizeAssistantMessage(raw, { tools })).filePath).toBe("C:\\Users\\me\\a.txt") + }) +}) + +describe("batches keep every call", () => { + test("JSON calls are all collected wherever they sit", () => { + const a = typed("read", { filePath: "a" }, "a") + const b = typed("read", { filePath: "b" }, "b") + const ids = (input) => normalizeAssistantMessage(input, { tools }).tool_calls.map((call) => call.id) + expect(ids(`${a}\n${b}\n${CM_TASK_COMPLETE_PROTOCOL}`)).toEqual(["a", "b"]) + expect(ids(`${a}\nThen\n${b}`)).toEqual(["a", "b"]) + expect(ids(`${a}\n${typed("read", { filePath: "a" }, "c")}`)).toEqual(["a", "c"]) + const repeated = typed("read", { filePath: "a" }) + expect(normalizeAssistantMessage(`${repeated}\n${repeated}`, { tools }).tool_calls).toHaveLength(1) + }) + + test("backticks in JSON string values do not hide a later call on the same line", () => { + const a = typed("write", { filePath: "a", content: `x${fence}` }, "a") + const b = typed("write", { filePath: "b", content: `${fence}y` }, "b") + expect(normalizeAssistantMessage(`${a}${b}`, { tools }).tool_calls.map((call) => call.id)).toEqual(["a", "b"]) + }) + + test("a truncated XML batch fails instead of running its prefix", () => { + const first = invoke("write", param("content", "one"), "a") + const tails = [`${lt}invoke id="b" name="write">${lt}parameter name="content">tw`, `${lt}invoke id="b" na`, lt] + for (const tail of tails) { + expect(() => normalizeAssistantMessage(`${lt}function_calls>${first}${tail}`, { tools })).toThrow( + expect.objectContaining({ type: "o1_code_incomplete_tool_call" }), + ) + } + expect(normalizeAssistantMessage(`${lt}function_calls>${first}`, { tools }).tool_calls).toHaveLength(1) + }) + + test("repeated XML ids are made unique", () => { + const response = normalizeAssistantMessage( + calls(invoke("write", param("content", "one"), "a"), invoke("write", param("content", "two"), "a")), + { tools }, + ) + expect(response.tool_calls[0].id).toBe("a") + expect(response.tool_calls[1].id).toMatch(/^call_/) + expect(response.tool_calls.map((_, index) => args(response, index).content)).toEqual(["one", "two"]) + }) +}) + +describe("fallback candidates", () => { + test("a malformed alternate does not hide a later valid one", () => { + const broken = `${lt}function_calls>${lt}invoke name="read">` + const response = chooseAssistantResponse("Done.", {}, [broken, typed("read", { filePath: "a" })], { tools }) + expect(response.tool_calls[0].function.name).toBe("read") + }) + + test("valid recorder parts win over an unterminated primary text", () => { + const broken = `${lt}function_calls>${lt}invoke name="write">` + const response = chooseAssistantResponse(broken, { a: { id: "part-1", name: "write", arguments: "{}" } }, [], { + tools, + }) + expect(response.tool_calls.map((call) => call.id)).toEqual(["part-1"]) + }) +}) + +describe("history round trips", () => { + test("rendered history parses back to the same calls", () => { + const content = `001\n${fence}js\nx\n${fence}\n${lt}/content> & done` + const call = { id: "stable", function: { name: "write", arguments: JSON.stringify({ filePath: "a.md", content }) } } + for (const rendered of [formatJsonToolCall(call), formatAnthropicToolCall(call)]) { + const response = normalizeAssistantMessage(rendered, { tools }) + expect(response.tool_calls[0].id).toBe("stable") + expect(args(response)).toEqual({ filePath: "a.md", content }) + } + }) +}) + describe("chooseAssistantResponse", () => { test("accepts a parsed tool call from an alternate assistant message", () => { const response = chooseAssistantResponse("I'll inspect the worktree.", {}, [ diff --git a/packages/desktop/src/main/wpp-bridge/proxy/toolCallReminder.mjs b/packages/desktop/src/main/wpp-bridge/proxy/toolCallReminder.mjs index d73512bfb8..b57df4e000 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/toolCallReminder.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/toolCallReminder.mjs @@ -13,6 +13,8 @@ Rules: - One value per . For object or array arguments, put JSON inside the parameter, e.g. [{"old":"a","new":"b"}]. - To run independent tools at once, emit several blocks inside one . Otherwise wait for each tool result before the next call. - Prior assistant tool calls in this thread already use this same XML format; the harness parses that same format back from you. +- Never put a tool call in a Markdown code fence or inline code. Calls written that way are treated as examples and are not run. +- Parameter values are plain text in which XML entities are decoded. If a value contains a closing parameter tag, or an entity that must stay as written, wrap the whole value as . - When finished with no further tools, answer in plain text only (no tool-call XML).`; export const TOOL_CALL_SYSTEM_REMINDER_JSON = `When you need a local harness tool, request it with a tool call as a single JSON object on its own, at the very end of your message (no markdown code fences around it):