From 5ed5aee6dd5278b115c37b7e4e47a2c31de070a0 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 16 Oct 2026 14:19:41 +0800 Subject: [PATCH 1/2] fix: runaway text guard no longer reads Google tool-call arguments as model output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #3260 text-run lane fed candidates[].content.parts[].functionCall.args string leaves to the run tracker: the Google wire has no top-level "type" field, so the function_call frame gate could not exempt it, and the payload leaf is usually named "content" — a TEXT_LEAF_KEYS name. A write-tool argument of >= 200 identical characters (e.g. a divider or padding run in a generated file) aborted the whole turn mid-stream via relay's stream guard. Add args/functionCall/parameters to EXCLUDED_BRANCHES (the openai arguments/partial_json keys were already excluded) and pin both directions: 300 '=' in functionCall.args must not trip, 300 '=' in parts[].text must. --- src/runaway-guard.ts | 10 +++++-- tests/issue3260-text-run-guard.test.ts | 39 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) 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..f53b2639c4 100644 --- a/tests/issue3260-text-run-guard.test.ts +++ b/tests/issue3260-text-run-guard.test.ts @@ -197,6 +197,45 @@ 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 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); From d686c80fd99f8b81d027d978a76f76b2378bcee1 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 16 Oct 2026 15:39:27 +0800 Subject: [PATCH 2/2] test: port two google-lane regression cases from duplicate PR #3375 (#3358) The closed duplicate covered the args.text leaf-key spelling and the mixed prose+arg frame shape; absorb both so the keeper PR pins them too. --- tests/issue3260-text-run-guard.test.ts | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/issue3260-text-run-guard.test.ts b/tests/issue3260-text-run-guard.test.ts index f53b2639c4..9167b245c5 100644 --- a/tests/issue3260-text-run-guard.test.ts +++ b/tests/issue3260-text-run-guard.test.ts @@ -220,6 +220,54 @@ test("wrap: Google wire functionCall.args with 300 '=' does NOT trip (argument b 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