diff --git a/src/runaway-guard.ts b/src/runaway-guard.ts index 0b08e349c3..e88ed2002f 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. `args` / +// `functionCall` cover the Google wire, where a tool call rides +// candidates[].content.parts[].functionCall.args and its payload fields are often +// named `content` — a TEXT_LEAF_KEYS name, so without the branch exclusion the +// argument bytes would be fed to the text-run lane (the openai `arguments` / +// responses `partial_json` keys were already excluded; Google events carry no +// top-level `type`, so the snapshot/function_call frame gate can't catch them). const EXCLUDED_BRANCHES: ReadonlySet = new Set([ "arguments", "input", "partial_json", "tool_calls", "tools", "functions", "system", "instructions", "metadata", "usage", "usageMetadata", "logprobs", "annotations", - "error", "id", "url", + "error", "id", "url", "args", "functionCall", "parameters", ]); export function createRunawayGuard(): RunawayGuard { diff --git a/tests/issue3260-text-run-guard.test.ts b/tests/issue3260-text-run-guard.test.ts index b19845d05b..9167b245c5 100644 --- a/tests/issue3260-text-run-guard.test.ts +++ b/tests/issue3260-text-run-guard.test.ts @@ -197,6 +197,93 @@ test("wrap: responses function_call_arguments.delta with 300 '=' does NOT trip ( assert.deepEqual(out, Buffer.concat(parts)); }); +test("wrap: Google wire functionCall.args with 300 '=' does NOT trip (argument bytes)", async () => { + // Google streaming chunk: the tool call rides candidates[].content.parts[].functionCall.args + // and its payload leaf is typically named `content` — a TEXT_LEAF_KEYS name. Google events + // carry no top-level `type`, so the function_call frame gate cannot exempt them; only the + // branch exclusion can. Before it, a write-tool payload of >= 200 identical characters + // aborted the whole turn mid-stream. + const frame = JSON.stringify({ + candidates: [ + { + content: { + role: "model", + parts: [{ functionCall: { name: "write", args: { path: "notes.ts", content: "=".repeat(300) } } }], + }, + }, + ], + }); + const parts = [sseFrame(frame), sseFrame(openaiDelta(" done."))]; + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 0, JSON.stringify(verdicts)); + assert.deepEqual(out, Buffer.concat(parts)); +}); + +test("wrap: Google wire functionCall.args.text (second leaf-key spelling) with 300 '-' does NOT trip", async () => { + // The argument field can carry ANY TEXT_LEAF_KEYS name; pin the second spelling + // so the exclusion holds by BRANCH name (functionCall/args), not field-name luck. + // Ported from duplicate PR #3375 (closed as a dup of this PR; #3358). + const frame = JSON.stringify({ + candidates: [ + { + content: { + role: "model", + parts: [{ functionCall: { name: "edit", args: { text: "-".repeat(300) } } }], + }, + }, + ], + }); + const parts = [sseFrame(frame)]; + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 0, JSON.stringify(verdicts)); + assert.deepEqual(out, Buffer.concat(parts)); +}); + +test("wrap: mixed Google frame — prose part + 300-'=' arg part coexist without tripping", async () => { + // One frame carrying BOTH a model-text part and a huge argument part: the run + // tracker must see only the prose, and every byte forwards untouched. + // Ported from duplicate PR #3375 (closed as a dup of this PR; #3358). + const frame = JSON.stringify({ + candidates: [ + { + content: { + role: "model", + parts: [ + { text: "Writing the file now." }, + { functionCall: { name: "write", args: { path: "/tmp/x.txt", content: "=".repeat(300) } } }, + ], + }, + }, + ], + }); + const parts = [ + sseFrame(frame), + sseFrame(JSON.stringify({ candidates: [{ content: { role: "model", parts: [{ text: " done." }] } }] })), + ]; + const verdicts: RunawayVerdict[] = []; + const out = await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 0, JSON.stringify(verdicts)); + assert.deepEqual(out, Buffer.concat(parts)); +}); + +test("wrap: Google wire parts[].text with 300 '=' still trips (exclusion must not swallow model text)", async () => { + // Companion to the args test: the same carrier shape with the run in the MODEL TEXT + // leaf (parts[].text) must still trip — proves the branch exclusion is scoped to + // functionCall.args, not to the whole Google candidates/content/parts subtree. + const parts: Uint8Array[] = []; + for (let i = 0; i < 3; i++) { + parts.push(sseFrame(JSON.stringify({ + candidates: [{ content: { role: "model", parts: [{ text: "=".repeat(120) }] } }], + }))); + } + const verdicts: RunawayVerdict[] = []; + await collect(wrapParts(parts, verdicts)); + assert.equal(verdicts.length, 1, JSON.stringify(verdicts)); + assert.equal(verdicts[0].reason, "text-run"); +}); + test("wrap: 3-byte-at-a-time feed crosses decoder/frame boundary splits and still trips", async () => { const sse = Array.from({ length: 7 }, () => `data: ${openaiDelta("=".repeat(37))}\n\n`).join(""); const full = enc.encode(sse);