From 2bcf39f69b87cf1aea39d9d012350dd34a513e3a Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 16 Oct 2026 15:20:07 +0800 Subject: [PATCH] fix(#3358): exclude google functionCall tool-call args from the text-run guard --- src/runaway-guard.ts | 8 +- ...issue3358-google-functioncall-args.test.ts | 87 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/issue3358-google-functioncall-args.test.ts diff --git a/src/runaway-guard.ts b/src/runaway-guard.ts index 0b08e349c3..8eae03137a 100644 --- a/src/runaway-guard.ts +++ b/src/runaway-guard.ts @@ -110,11 +110,17 @@ const TEXT_LEAF_KEYS: ReadonlySet = new Set([ "content", "text", "thinking", "reasoning", "reasoning_content", "delta", ]); // Branches whose strings are NEVER model output. Tool-call arguments are user intent -// (#1039): a write-tool payload of 300 '=' must not abort the turn. +// (#1039): a write-tool payload of 300 '=' must not abort the turn. Each wire names +// its argument branch differently — openai "arguments", anthropic "input", google +// "functionCall.args" (#3358: neither was excluded, and google frames carry no +// top-level "type" for the event-level exemption, so an arg field named content/text +// hit TEXT_LEAF_KEYS by name) — so the set covers all three plus the generic +// containers, keyed by BRANCH NAME only (the walker propagates exclusion downward). const EXCLUDED_BRANCHES: ReadonlySet = new Set([ "arguments", "input", "partial_json", "tool_calls", "tools", "functions", "system", "instructions", "metadata", "usage", "usageMetadata", "logprobs", "annotations", "error", "id", "url", + "functionCall", "args", "parameters", ]); export function createRunawayGuard(): RunawayGuard { diff --git a/tests/issue3358-google-functioncall-args.test.ts b/tests/issue3358-google-functioncall-args.test.ts new file mode 100644 index 0000000000..ddb5d5f10b --- /dev/null +++ b/tests/issue3358-google-functioncall-args.test.ts @@ -0,0 +1,87 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + wrapStreamWithRunawayGuard, + TEXT_RUN_THRESHOLD, + type RunawayVerdict, +} from "../src/runaway-guard.ts"; + +// #3358 regression: the google lane nests tool calls under +// candidates[].content.parts[].functionCall.args. Neither "functionCall" nor "args" +// sat in EXCLUDED_BRANCHES, and google frames carry no top-level "type" for the +// event-level exemption (processParsed), so an arg value field named content/text — +// both in TEXT_LEAF_KEYS — was fed to the text-run tracker: a write-tool payload of +// 300 '=' aborted the whole answer (#1039: tool args are user intent, never counted). +// Pins BOTH directions: excluded args must NOT trip while a genuine parts[].text run +// of the same length MUST still trip (guards against over-exclusion). + +const enc = new TextEncoder(); + +function sseFrame(json: string): Uint8Array { + return enc.encode(`data: ${json}\n\n`); +} + +function googleFrame(...parts: Record[]): string { + return JSON.stringify({ + candidates: [{ content: { role: "model", parts }, finishReason: "STOP", index: 0 }], + usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 }, + modelVersion: "gemini-2.5-flash", + }); +} + +async function drive(parts: Uint8Array[]): Promise<{ verdicts: RunawayVerdict[]; out: Buffer }> { + const verdicts: RunawayVerdict[] = []; + const src = new ReadableStream({ + start(c) { + for (const p of parts) c.enqueue(p); + c.close(); + }, + }); + const r = wrapStreamWithRunawayGuard(src, (v) => verdicts.push(v)).getReader(); + const chunks: Uint8Array[] = []; + for (;;) { + const { done, value } = await r.read(); + if (done) break; + chunks.push(value); + } + return { verdicts, out: Buffer.concat(chunks) }; +} + +test("wrap: google functionCall.args.content with 300 '=' does NOT trip (#1039 user intent)", async () => { + const frame = googleFrame({ + functionCall: { name: "write_file", args: { path: "/tmp/blob.txt", content: "=".repeat(300) } }, + }); + const parts = [sseFrame(frame), sseFrame(googleFrame({ text: " done." }))]; + const { verdicts, out } = await drive(parts); + assert.equal(verdicts.length, 0, JSON.stringify(verdicts)); + assert.deepEqual(out, Buffer.concat(parts)); +}); + +test("wrap: google functionCall.args.text (second leaf-key spelling) with 300 '-' does NOT trip", async () => { + const frame = googleFrame({ + functionCall: { name: "edit_file", args: { text: "-".repeat(300) } }, + }); + const parts = [sseFrame(frame)]; + const { verdicts, out } = await drive(parts); + assert.equal(verdicts.length, 0, JSON.stringify(verdicts)); + assert.deepEqual(out, Buffer.concat(parts)); +}); + +test("wrap: control — google parts[].text with 250 '=' DOES trip as text-run (no over-exclusion)", async () => { + const parts = [sseFrame(googleFrame({ text: "=".repeat(TEXT_RUN_THRESHOLD + 50) }))]; + const { verdicts } = await drive(parts); + assert.equal(verdicts.length, 1, JSON.stringify(verdicts)); + assert.equal(verdicts[0].reason, "text-run"); + assert.equal(verdicts[0].detail?.textRunChar, "="); +}); + +test("wrap: mixed google frame — prose part + 300-'=' arg part coexist without tripping", async () => { + const frame = googleFrame( + { text: "Writing the file now." }, + { functionCall: { name: "write_file", args: { path: "/tmp/x.txt", content: "=".repeat(300) } } }, + ); + const parts = [sseFrame(frame), sseFrame(googleFrame({ text: " done." }))]; + const { verdicts, out } = await drive(parts); + assert.equal(verdicts.length, 0, JSON.stringify(verdicts)); + assert.deepEqual(out, Buffer.concat(parts)); +});