fix(stream): arm the post-tool retry only for the latest turn's tool results

requestFollowsToolCalls() searched the whole history, so a tool exchange
that the assistant had already answered armed the buffered empty-turn
retry on every later follow-up and cost those turns incremental
streaming. Walk back from the newest entry and stop at the latest
assistant turn (assistant message, Responses function_call, Gemini model
turn): only tool results after it make the request a post-tool
continuation. A user or system note appended after the tool results
still counts. Covers all four client formats with negative and positive
cases (#20092).
This commit is contained in:
Afonso Dutra Nogueira Filho
2026-11-18 19:04:46 -03:00
committed by diegosouzapw
parent f903dcf606
commit db8e10356c
2 changed files with 170 additions and 27 deletions

View File

@@ -136,49 +136,83 @@ export function isMuteDirectTurn(summary: EmptyTurnSummary): boolean {
}
/**
* True when the request's message history already carries submitted tool
* results — i.e. this turn is a post-tool continuation, the case where a
* degenerate empty upstream turn silently breaks an agentic loop (#13600).
* The client body is the reliable signal: it holds the results the client
* submitted even when a translation step mangled them upstream. Format-
* agnostic markers: OpenAI `role:"tool"`/`"function"` messages, Claude
* `tool_result` content blocks, Responses `function_call_output` input
* items, Gemini `functionResponse` parts.
* True when the request's latest turn carries submitted tool results — i.e.
* this turn is a post-tool continuation, the case where a degenerate empty
* upstream turn silently breaks an agentic loop (#13600). Only tool results
* AFTER the most recent assistant turn count: an earlier, already-answered tool
* exchange in the history must not arm the retry (and its buffering) on every
* later follow-up. Messages appended after the tool results (a user or system
* note) keep the turn a continuation. The client body is the reliable signal:
* it holds the results the client submitted even when a translation step
* mangled them upstream. Format-agnostic markers: OpenAI `role:"tool"`/
* `"function"` messages, Claude `tool_result` content blocks, Responses
* `function_call_output` input items, Gemini `functionResponse` parts.
*/
export function requestFollowsToolCalls(body: unknown): boolean {
if (!body || typeof body !== "object") return false;
const messages = (body as { messages?: unknown }).messages;
if (Array.isArray(messages)) {
return messages.some((message) => {
if (!message || typeof message !== "object") return false;
const role = (message as { role?: unknown }).role;
if (role === "tool" || role === "function") return true;
const content = (message as { content?: unknown }).content;
return (
Array.isArray(content) &&
content.some((part) => (part as { type?: unknown } | null)?.type === "tool_result")
);
});
return hasToolResultAfterLatestAssistant(messages, isChatAssistantTurn, isChatToolResult);
}
const input = (body as { input?: unknown }).input;
if (Array.isArray(input)) {
return input.some(
(item) => (item as { type?: unknown } | null)?.type === "function_call_output"
return hasToolResultAfterLatestAssistant(
input,
isResponsesAssistantItem,
(item) => readField(item, "type") === "function_call_output"
);
}
const contents = (body as { contents?: unknown }).contents;
if (Array.isArray(contents)) {
return contents.some((entry) => {
const parts = (entry as { parts?: unknown } | null)?.parts;
return (
Array.isArray(parts) &&
parts.some((part) => (part as Record<string, unknown> | null)?.functionResponse != null)
);
});
return hasToolResultAfterLatestAssistant(
contents,
(entry) => readField(entry, "role") === "model",
isGeminiFunctionResponse
);
}
return false;
}
function readField(value: unknown, key: string): unknown {
return value && typeof value === "object" ? (value as Record<string, unknown>)[key] : undefined;
}
/** Walk back from the newest entry: a tool result before any assistant turn wins. */
function hasToolResultAfterLatestAssistant(
entries: unknown[],
isAssistantTurn: (entry: unknown) => boolean,
isToolResult: (entry: unknown) => boolean
): boolean {
for (let index = entries.length - 1; index >= 0; index--) {
const entry = entries[index];
if (isToolResult(entry)) return true;
if (isAssistantTurn(entry)) return false;
}
return false;
}
function isChatAssistantTurn(message: unknown): boolean {
return readField(message, "role") === "assistant";
}
function isChatToolResult(message: unknown): boolean {
const role = readField(message, "role");
if (role === "tool" || role === "function") return true;
const content = readField(message, "content");
return (
Array.isArray(content) && content.some((part) => readField(part, "type") === "tool_result")
);
}
function isResponsesAssistantItem(item: unknown): boolean {
return readField(item, "role") === "assistant" || readField(item, "type") === "function_call";
}
function isGeminiFunctionResponse(entry: unknown): boolean {
const parts = readField(entry, "parts");
return Array.isArray(parts) && parts.some((part) => readField(part, "functionResponse") != null);
}
/** A read that outlived the idle budget, kept distinct from a real chunk. */
const IDLE_READ = Symbol("idle-read");

View File

@@ -187,6 +187,115 @@ test("requestFollowsToolCalls detects tool results across client formats", () =>
);
});
test("requestFollowsToolCalls only counts tool results after the latest assistant turn", () => {
// A completed tool exchange followed by the assistant's answer and a new user
// turn is an ordinary follow-up: it must keep incremental streaming.
assert.equal(
requestFollowsToolCalls({
messages: [
{ role: "user", content: "weather?" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1" }] },
{ role: "tool", tool_call_id: "c1", content: "sunny" },
{ role: "assistant", content: "It is sunny." },
{ role: "user", content: "thanks, and tomorrow?" },
],
}),
false,
"OpenAI: an earlier tool exchange must not arm a later turn"
);
assert.equal(
requestFollowsToolCalls({
messages: [
{ role: "user", content: "weather?" },
{ role: "assistant", content: [{ type: "tool_use", id: "u1", name: "w", input: {} }] },
{ role: "user", content: [{ type: "tool_result", tool_use_id: "u1", content: "sunny" }] },
{ role: "assistant", content: [{ type: "text", text: "It is sunny." }] },
{ role: "user", content: "and tomorrow?" },
],
}),
false,
"Claude: an earlier tool exchange must not arm a later turn"
);
assert.equal(
requestFollowsToolCalls({
input: [
{ role: "user", content: "weather?" },
{ type: "function_call", call_id: "c1", name: "w", arguments: "{}" },
{ type: "function_call_output", call_id: "c1", output: "sunny" },
{ type: "message", role: "assistant", content: [{ type: "output_text", text: "Sunny." }] },
{ role: "user", content: "and tomorrow?" },
],
}),
false,
"Responses: an earlier tool exchange must not arm a later turn"
);
assert.equal(
requestFollowsToolCalls({
contents: [
{ role: "user", parts: [{ text: "weather?" }] },
{ role: "model", parts: [{ functionCall: { name: "w", args: {} } }] },
{ role: "user", parts: [{ functionResponse: { name: "w", response: {} } }] },
{ role: "model", parts: [{ text: "Sunny." }] },
{ role: "user", parts: [{ text: "and tomorrow?" }] },
],
}),
false,
"Gemini: an earlier tool exchange must not arm a later turn"
);
// A continuation may append a user/system message after the tool results; it
// is still a post-tool turn.
assert.equal(
requestFollowsToolCalls({
messages: [
{ role: "user", content: "weather?" },
{ role: "assistant", content: null, tool_calls: [{ id: "c1" }] },
{ role: "tool", tool_call_id: "c1", content: "sunny" },
{ role: "user", content: "keep going" },
],
}),
true,
"OpenAI: tool results followed by an appended message still arm the retry"
);
assert.equal(
requestFollowsToolCalls({
messages: [
{ role: "assistant", content: [{ type: "tool_use", id: "u1", name: "w", input: {} }] },
{
role: "user",
content: [
{ type: "tool_result", tool_use_id: "u1", content: "sunny" },
{ type: "text", text: "keep going" },
],
},
],
}),
true,
"Claude: a tool_result after the latest assistant turn arms the retry"
);
assert.equal(
requestFollowsToolCalls({
input: [
{ type: "function_call", call_id: "c1", name: "w", arguments: "{}" },
{ type: "function_call_output", call_id: "c1", output: "sunny" },
{ role: "system", content: "be brief" },
],
}),
true,
"Responses: tool output followed by an appended message still arms the retry"
);
assert.equal(
requestFollowsToolCalls({
contents: [
{ role: "model", parts: [{ functionCall: { name: "w", args: {} } }] },
{ role: "user", parts: [{ functionResponse: { name: "w", response: {} } }] },
{ role: "user", parts: [{ text: "keep going" }] },
],
}),
true,
"Gemini: functionResponse after the latest model turn arms the retry"
);
});
test("flag off: a post-tool continuation still retries a mute turn", async () => {
await seedGroq("post-tool-a", "sk-post-tool-a");
await seedGroq("post-tool-b", "sk-post-tool-b");