fix: emit a tool call that arrives with no arguments
A tool call whose arguments never arrive — a no-argument tool, or a model that
sends id and name and then stops — was accumulated into toolCallsState but
never added to the message, because the content block was only created inside
`if (tc.function?.arguments)`.
The finalizer then checked `toolCallsState.length > 0` and set
stopReason "toolUse" on a message containing no tool call at all. pi's agent
loop continues only on tool calls, so it had nothing to execute and the turn
ended: mid-task, no tool run, no error, nothing in the transcript except the
assistant saying what it was about to do. Observed against a real session where
the model announced "let me consult the advisor" four times in a row and the
run then stopped dead; its final message on disk is stopReason "toolUse" with a
single text block and no toolCall.
Two changes:
- open the block as soon as the call is identifiable (id or name), and keep
its id/name in step if they arrive in a later delta, since the finalizer
only rewrites `arguments`;
- guard the stopReason on blocks that actually reached the message rather
than on the state array being non-empty, so a genuinely malformed stream
reports "stop" instead of an unactionable "toolUse".
Tests: no-argument call, identity arriving after the block is open, and a
malformed delta that must not claim toolUse.
This commit is contained in:
+34
-9
@@ -407,17 +407,39 @@ export function streamQoder(
|
||||
const state = toolCallsState[idx];
|
||||
if (tc.id) state.id = tc.id;
|
||||
if (tc.function?.name) state.name = tc.function.name;
|
||||
|
||||
// Open the block as soon as the call is IDENTIFIABLE, not
|
||||
// when its first argument byte arrives. A call whose
|
||||
// arguments are absent or an empty string — a no-argument
|
||||
// tool, or a model that sends id+name and then stops — used
|
||||
// to create a toolCallsState entry and no content block, so
|
||||
// the finalizer below saw a non-empty state array, set
|
||||
// stopReason "toolUse", and handed back a message with no
|
||||
// tool call in it. The agent loop then had nothing to run
|
||||
// and the turn simply ended, mid-task and without an error.
|
||||
if (state.emittedStart === undefined && (state.id || state.name)) {
|
||||
state.emittedStart = true;
|
||||
state.contentIndex = output.content.length;
|
||||
output.content.push({
|
||||
type: "toolCall",
|
||||
id: state.id,
|
||||
name: state.name,
|
||||
arguments: {},
|
||||
} satisfies ToolCall);
|
||||
stream.push({ type: "toolcall_start", contentIndex: state.contentIndex, partial: output });
|
||||
}
|
||||
|
||||
// id and name can arrive after the block is open; keep it
|
||||
// in step, since the finalizer only rewrites `arguments`.
|
||||
if (state.emittedStart) {
|
||||
const block = output.content[state.contentIndex] as ToolCall;
|
||||
block.id = state.id;
|
||||
block.name = state.name;
|
||||
}
|
||||
|
||||
if (tc.function?.arguments) {
|
||||
const argDelta = tc.function.arguments;
|
||||
state.arguments += argDelta;
|
||||
|
||||
if (state.emittedStart === undefined) {
|
||||
state.emittedStart = true;
|
||||
state.contentIndex = output.content.length;
|
||||
const block: ToolCall = { type: "toolCall", id: state.id, name: state.name, arguments: {} };
|
||||
output.content.push(block);
|
||||
stream.push({ type: "toolcall_start", contentIndex: state.contentIndex, partial: output });
|
||||
}
|
||||
stream.push({
|
||||
type: "toolcall_delta",
|
||||
contentIndex: state.contentIndex,
|
||||
@@ -487,7 +509,10 @@ export function streamQoder(
|
||||
}
|
||||
}
|
||||
|
||||
if (toolCallsState.length > 0) {
|
||||
// Guarded on blocks that actually reached the message, not on the state
|
||||
// array being non-empty. Claiming "toolUse" for a message carrying no
|
||||
// tool call is what turned a malformed stream into a silent dead end.
|
||||
if (toolCallsState.some((state) => state?.emittedStart)) {
|
||||
output.stopReason = "toolUse";
|
||||
}
|
||||
// Otherwise keep whatever finish_reason set upstream (defaults to "stop").
|
||||
|
||||
Reference in New Issue
Block a user