Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(tools): unwrap <tool_call> envelopes before the strict parser sees them

Jack Rudenko58b577512cbd
diff --git a/packages/cli/src/handlers/shared/tool-call-recovery.test.ts b/packages/cli/src/handlers/shared/tool-call-recovery.test.ts
index d50ecba9..12c6d82b 100644
--- a/packages/cli/src/handlers/shared/tool-call-recovery.test.ts
+++ b/packages/cli/src/handlers/shared/tool-call-recovery.test.ts
@@ -119,3 +119,72 @@ describe("parseFunctionTagEnvelope strictness", () => {
expect(extractToolCallsFromText(envelope, ["Read"])).toEqual([]);
});
});
+
+/**
+ * The `<tool_call>`-wrapped envelope.
+ *
+ * Reproduced live against the published 9.5.0 binary through a mock upstream:
+ * the wrapped form produced `{"city":"Paris</parameter></function></tool_call>"}`
+ * while the identical unwrapped form produced `{"city":"Paris"}`. The failure is
+ * silent — the tool runs on the corrupted argument — so every assertion here
+ * checks the VALUE, not merely that a call came out.
+ */
+describe("parseFunctionTagEnvelope with a <tool_call> wrapper", () => {
+ it("parses the wrapped form identically to the unwrapped one", () => {
+ const wrapped =
+ "<tool_call><function=get_weather><parameter=city>Paris</parameter></function></tool_call>";
+ const unwrapped = "<function=get_weather><parameter=city>Paris</parameter></function>";
+ expect(parseFunctionTagEnvelope(wrapped)).toEqual(parseFunctionTagEnvelope(unwrapped));
+ expect(parseFunctionTagEnvelope(wrapped)).toEqual([
+ { name: "get_weather", arguments: { city: "Paris" }, source: "xml_text" },
+ ]);
+ });
+
+ it("never leaves a closing tag inside the last parameter value", () => {
+ const calls = extractToolCallsFromText(
+ "<tool_call><function=get_weather><parameter=city>Paris</parameter></function></tool_call>",
+ ["get_weather"]
+ );
+ expect(calls).toHaveLength(1);
+ expect(calls[0].arguments.city).toBe("Paris");
+ expect(JSON.stringify(calls[0].arguments)).not.toContain("</");
+ });
+
+ it("handles several wrapped calls in one response", () => {
+ expect(
+ parseFunctionTagEnvelope(
+ "<tool_call><function=Read><parameter=file_path>/a</parameter></function></tool_call>\n" +
+ "<tool_call><function=Bash><parameter=command>ls -la</parameter></function></tool_call>"
+ )
+ ).toEqual([
+ { name: "Read", arguments: { file_path: "/a" }, source: "xml_text" },
+ { name: "Bash", arguments: { command: "ls -la" }, source: "xml_text" },
+ ]);
+ });
+
+ it("accepts a wrapper the stream never closed", () => {
+ expect(parseFunctionTagEnvelope("<tool_call><function=Read><parameter=file_path>/a")).toEqual([
+ { name: "Read", arguments: { file_path: "/a" }, source: "xml_text" },
+ ]);
+ });
+
+ it("still refuses a wrapped envelope with prose in front of it", () => {
+ expect(
+ parseFunctionTagEnvelope(
+ "Let me check: <tool_call><function=Read><parameter=file_path>/a</parameter></function></tool_call>"
+ )
+ ).toBeNull();
+ });
+
+ it("leaves the JSON-payload <tool_call> shape to the legacy pattern that owns it", () => {
+ const json = '<tool_call>{"name": "Read", "arguments": {"file_path": "/a"}}</tool_call>';
+ expect(parseFunctionTagEnvelope(json)).toBeNull();
+ // Patterns 1 and 2 both match this shape, so the legacy path returns it
+ // twice — pre-existing, and unchanged by the unwrap.
+ expect(extractToolCallsFromText(json, ["Read"])).toContainEqual({
+ name: "Read",
+ arguments: { file_path: "/a" },
+ source: "xml_text",
+ });
+ });
+});
diff --git a/packages/cli/src/handlers/shared/tool-call-recovery.ts b/packages/cli/src/handlers/shared/tool-call-recovery.ts
index e24e5cef..f127cb60 100644
--- a/packages/cli/src/handlers/shared/tool-call-recovery.ts
+++ b/packages/cli/src/handlers/shared/tool-call-recovery.ts
@@ -127,6 +127,65 @@ const FUNCTION_TAG_AT_CURSOR = new RegExp(`<function=(${TOOL_NAME_SOURCE})>`, "y
/** `<parameter=NAME>` at exactly the cursor, inside a function block. */
const PARAMETER_TAG_AT_CURSOR = /<parameter=([^>\s]+)>/y;
+/** `<tool_call>` at exactly the cursor, with any whitespace after it. Sticky. */
+const TOOL_CALL_OPEN_AT_CURSOR = /<tool_call>\s*/y;
+const TOOL_CALL_CLOSE_TAG = "</tool_call>";
+
+/**
+ * Unwrap the `<tool_call>` envelope Qwen- and Hermes-family models put around a
+ * function-tag block, so the envelope parser below sees the shape it was written
+ * for.
+ *
+ * This is an ENTRY condition, not a parser change. The wrapped form
+ *
+ * <tool_call><function=get_weather><parameter=city>Paris</parameter></function></tool_call>
+ *
+ * failed `startsWith("<function=")`, fell through to the six loose regex
+ * patterns, and came out as `{"city":"Paris</parameter></function></tool_call>"}`
+ * — the closing tags swallowed into the value, silently, and a tool then ran on
+ * the corrupted argument. Measured live against the published 9.5.0 binary; the
+ * same input without the wrapper produced `{"city":"Paris"}`.
+ *
+ * The wrapper is REMOVED here rather than tolerated inside the parser, which is
+ * what keeps `</tool_call>` out of the last parameter's value: the parameter
+ * value scanner stops at `</parameter>`, `</function>`, `<parameter=` and
+ * `<function=`, and nothing it can see afterwards belongs to any value.
+ *
+ * Multiple wrapped calls in one response fall out of the same loop: each block
+ * is unwrapped in turn and they are rejoined, so the existing "a block ends at
+ * the next `<function=`" rule handles the boundary.
+ *
+ * Returns `null` — declining, so the caller keeps the original text — for
+ * anything that is not exactly this shape: a leading `<tool_call>` that wraps
+ * something other than a function tag (Pattern 1's JSON payload is precisely
+ * that), or trailing content after the last `</tool_call>`. An UNTERMINATED
+ * wrapper is accepted, because a truncated stream is a real capture and the
+ * block inside it is still unambiguous.
+ */
+function unwrapToolCallTags(body: string): string | null {
+ if (!body.startsWith("<tool_call>")) return null;
+
+ const blocks: string[] = [];
+ let cursor = 0;
+
+ while (cursor < body.length) {
+ TOOL_CALL_OPEN_AT_CURSOR.lastIndex = cursor;
+ if (!TOOL_CALL_OPEN_AT_CURSOR.exec(body)) return null;
+ cursor = TOOL_CALL_OPEN_AT_CURSOR.lastIndex;
+
+ const close = body.indexOf(TOOL_CALL_CLOSE_TAG, cursor);
+ const block = (close === -1 ? body.slice(cursor) : body.slice(cursor, close)).trim();
+ if (!block.startsWith("<function=")) return null;
+ blocks.push(block);
+ if (close === -1) break;
+
+ cursor = close + TOOL_CALL_CLOSE_TAG.length;
+ cursor += /^\s*/.exec(body.slice(cursor))?.[0].length ?? 0;
+ }
+
+ return blocks.length > 0 ? blocks.join("\n") : null;
+}
+
/**
* Parse a response that is ENTIRELY a `<function=NAME><parameter=P>V` envelope.
*
@@ -150,8 +209,11 @@ const PARAMETER_TAG_AT_CURSOR = /<parameter=([^>\s]+)>/y;
* describes one.
*/
export function parseFunctionTagEnvelope(text: string): ExtractedToolCall[] | null {
- const body = text.trim();
- if (body.length === 0) return null;
+ const trimmed = text.trim();
+ if (trimmed.length === 0) return null;
+ // The `<tool_call>` wrapper is stripped BEFORE the strict test, never parsed
+ // through: see `unwrapToolCallTags`.
+ const body = unwrapToolCallTags(trimmed) ?? trimmed;
if (!body.startsWith("<function=")) return null;
const calls: ExtractedToolCall[] = [];