Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/runaway-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,16 @@ const TEXT_LEAF_KEYS: ReadonlySet<string> = 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. The exclusion is
// key-propagated, so every wire's argument container needs its own key here: openai
// chat tool_calls[].function.arguments ("arguments"), anthropic tool_use.input
// ("input"), responses function_call_arguments (skipped by event type in processParsed).
// Google has NEITHER a type field NOR an excluded container key — its calls ride
// parts[].functionCall.args with payload leaves named content/text (#3358: the #3946-
// class incident shape on the one wire the original list missed).
const EXCLUDED_BRANCHES: ReadonlySet<string> = new Set([
"arguments", "input", "partial_json", "tool_calls", "tools", "functions", "system",
"arguments", "args", "parameters", "functionCall", "input", "partial_json",
"tool_calls", "tools", "functions", "system",
"instructions", "metadata", "usage", "usageMetadata", "logprobs", "annotations",
"error", "id", "url",
]);
Expand Down
25 changes: 25 additions & 0 deletions tests/runaway-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,28 @@ test("wrapStreamWithRunawayGuard aborts on runaway: onTrip once, clean early EOF
const forwarded = out.toString("utf8");
assert.match(forwarded, /^\x3cacp tokens="0" type="text">m02040\x3c\/acp$/m);
});

// #3358 regression: Google-wire tool calls ride parts[].functionCall.args with payload
// leaves named content/text — argument bytes are user intent (#1039) and must never be
// counted by the text-run lane, exactly like openai arguments / anthropic input.
test("google functionCall.args with 300 identical chars does NOT trip the text-run lane", async () => {
const argRun = "=".repeat(300);
const body = JSON.stringify({ candidates: [{ content: { parts: [{ functionCall: { name: "write_file", args: { path: "/tmp/x", content: argRun } } }] } }] });
const frame = `data: ${body}\n\n`;
const src = new ReadableStream<Uint8Array>({ start(c) { c.enqueue(enc.encode(frame)); c.close(); } });
let tripped = false;
const out = await collect(wrapStreamWithRunawayGuard(src, () => (tripped = true)));
assert.equal(tripped, false, "tool-call argument run must not abort the turn");
assert.equal(out.toString("utf8"), frame, "byte-exact forward");
});

// Control for #3358: the SAME wire's genuine model output (parts[].text) must still trip.
test("google parts[].text with 250 identical chars still trips as text-run", async () => {
const textRun = "-".repeat(250);
const body = JSON.stringify({ candidates: [{ content: { parts: [{ text: textRun }] } }] });
const frame = `data: ${body}\n\n`;
const src = new ReadableStream<Uint8Array>({ start(c) { c.enqueue(enc.encode(frame)); c.close(); } });
let tripReason: string | undefined;
await collect(wrapStreamWithRunawayGuard(src, (v) => (tripReason = v.reason)));
assert.equal(tripReason, "text-run", "real model-output runs must still be caught");
});
Loading