Ignore tool calls from aborted assistant turns

This commit is contained in:
Mariano Belinky 2026-01-28 09:10:28 +01:00
parent 85615ef570
commit c96d858baa
3 changed files with 24 additions and 0 deletions

View File

@ -52,6 +52,28 @@ describe("installSessionToolResultGuard", () => {
expect(messages.map((m) => m.role)).toEqual(["assistant", "toolResult"]); expect(messages.map((m) => m.role)).toEqual(["assistant", "toolResult"]);
}); });
it("ignores tool calls from aborted assistant messages", () => {
const sm = SessionManager.inMemory();
installSessionToolResultGuard(sm);
sm.appendMessage({
role: "assistant",
stopReason: "aborted",
content: [{ type: "toolCall", id: "call_abort", name: "read", arguments: {} }],
} as AgentMessage);
sm.appendMessage({
role: "assistant",
content: [{ type: "text", text: "next" }],
} as AgentMessage);
const messages = sm
.getEntries()
.filter((e) => e.type === "message")
.map((e) => (e as { message: AgentMessage }).message);
expect(messages.map((m) => m.role)).toEqual(["assistant", "assistant"]);
});
it("does not add synthetic toolResult when a matching one exists", () => { it("does not add synthetic toolResult when a matching one exists", () => {
const sm = SessionManager.inMemory(); const sm = SessionManager.inMemory();
installSessionToolResultGuard(sm); installSessionToolResultGuard(sm);

View File

@ -7,6 +7,7 @@ import { emitSessionTranscriptUpdate } from "../sessions/transcript-events.js";
type ToolCall = { id: string; name?: string }; type ToolCall = { id: string; name?: string };
function extractAssistantToolCalls(msg: Extract<AgentMessage, { role: "assistant" }>): ToolCall[] { function extractAssistantToolCalls(msg: Extract<AgentMessage, { role: "assistant" }>): ToolCall[] {
if (msg.stopReason === "aborted") return [];
const content = msg.content; const content = msg.content;
if (!Array.isArray(content)) return []; if (!Array.isArray(content)) return [];

View File

@ -8,6 +8,7 @@ type ToolCallLike = {
function extractToolCallsFromAssistant( function extractToolCallsFromAssistant(
msg: Extract<AgentMessage, { role: "assistant" }>, msg: Extract<AgentMessage, { role: "assistant" }>,
): ToolCallLike[] { ): ToolCallLike[] {
if (msg.stopReason === "aborted") return [];
const content = msg.content; const content = msg.content;
if (!Array.isArray(content)) return []; if (!Array.isArray(content)) return [];