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:
Oyster Lee
2026-08-03 06:50:04 +08:00
parent a9a3521b9b
commit b4416909f5
2 changed files with 101 additions and 9 deletions
+67
View File
@@ -5,6 +5,7 @@ import type {
AssistantMessageEventStream,
Context,
Model,
ToolCall,
} from "@earendil-works/pi-ai";
import { afterEach, describe, expect, it, vi } from "vitest";
import { streamQoder } from "../stream.js";
@@ -223,4 +224,70 @@ describe("streamQoder", () => {
const toolCall = msg.content.find((c) => c.type === "toolCall");
expect(toolCall).toBeDefined();
});
it("emits a tool call that arrives with no arguments", async () => {
// A no-argument tool, or a model that sends id+name and stops. The block
// used to be created only inside `if (tc.function?.arguments)`, so this
// produced a toolCallsState entry and NO content block — and the finalizer
// then set stopReason "toolUse" on a message with no tool call in it. pi's
// agent loop had nothing to execute and the turn ended silently, mid-task.
const sse =
sseEnvelope(
chunk({
tool_calls: [{ index: 0, id: "call_1", function: { name: "advisor", arguments: "" } }],
}),
) +
sseEnvelope(finishChunk("tool_calls")) +
DONE_SSE;
globalThis.fetch = mockFetch(sse);
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
const events = await consume(stream);
const done = events.find((e) => e.type === "done");
const msg = (done as { message: AssistantMessage }).message;
const toolCall = msg.content.find((c) => c.type === "toolCall") as ToolCall | undefined;
expect(toolCall, "a named tool call must reach the message even with no arguments").toBeDefined();
expect(toolCall?.name).toBe("advisor");
expect(toolCall?.id).toBe("call_1");
expect(toolCall?.arguments).toEqual({});
expect(msg.stopReason).toBe("toolUse");
});
it("picks up an id and name that arrive after the block is open", async () => {
// Streamed the other way round: arguments first, identity later.
const sse =
sseEnvelope(chunk({ tool_calls: [{ index: 0, function: { name: "bash", arguments: '{"comm' } }] })) +
sseEnvelope(chunk({ tool_calls: [{ index: 0, id: "call_9", function: { arguments: 'and":"ls"}' } }] })) +
sseEnvelope(finishChunk("tool_calls")) +
DONE_SSE;
globalThis.fetch = mockFetch(sse);
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
const events = await consume(stream);
const done = events.find((e) => e.type === "done");
const msg = (done as { message: AssistantMessage }).message;
const toolCall = msg.content.find((c) => c.type === "toolCall") as ToolCall | undefined;
expect(toolCall?.id).toBe("call_9");
expect(toolCall?.name).toBe("bash");
expect(toolCall?.arguments).toEqual({ command: "ls" });
});
it("does not claim toolUse when no tool call reached the message", async () => {
// A malformed stream: a tool_calls delta with neither id nor name. Better a
// clean "stop" than a message that says toolUse and carries nothing, which
// the agent loop cannot act on and cannot report.
const sse =
sseEnvelope(chunk({ content: "thinking about it", role: "assistant" })) +
sseEnvelope(chunk({ tool_calls: [{ index: 0, function: {} }] })) +
sseEnvelope(finishChunk("stop")) +
DONE_SSE;
globalThis.fetch = mockFetch(sse);
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
const events = await consume(stream);
const done = events.find((e) => e.type === "done");
const msg = (done as { message: AssistantMessage }).message;
expect(msg.content.find((c) => c.type === "toolCall")).toBeUndefined();
expect(msg.stopReason).toBe("stop");
});
});