Skip to content

Commit c250638

Browse files
PierrunoYTclaude
andcommitted
fix(task): mark synthetic interrupted tool results as errors
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 2eacc2e commit c250638

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎src/core/task/__tests__/normalizeToolResultPairing.spec.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ type Block = Anthropic.Messages.ContentBlockParam
77

88
const use = (id: string): Block => ({ type: "tool_use", id, name: "read_file", input: {} })
99
const result = (id: string, content = `result ${id}`): Block => ({ type: "tool_result", tool_use_id: id, content })
10+
const interrupted = (id: string): Block => ({
11+
type: "tool_result",
12+
tool_use_id: id,
13+
content: "Tool execution was interrupted before completion.",
14+
is_error: true,
15+
})
1016
const text = (value: string): Block => ({ type: "text", text: value })
1117
const image: Block = { type: "image", source: { type: "base64", media_type: "image/png", data: "AAAA" } }
1218
const assistant = (...content: Block[]): ApiMessage => ({ role: "assistant", content })
@@ -32,11 +38,13 @@ describe("normalizeToolResultPairing", () => {
3238
it("adds a synthetic error result for an interrupted tool_use", () => {
3339
const input = [assistant(use("A"), use("B")), user(result("A"), text("next"))]
3440

35-
expect(normalizeToolResultPairing(input)[1].content).toEqual([
36-
result("A"),
37-
{ type: "tool_result", tool_use_id: "B", content: "Tool execution was interrupted before completion." },
38-
text("next"),
39-
])
41+
expect(normalizeToolResultPairing(input)[1].content).toEqual([result("A"), interrupted("B"), text("next")])
42+
})
43+
44+
it("preserves string content in the user turn after a tool_use", () => {
45+
const out = normalizeToolResultPairing([assistant(use("A")), { role: "user", content: "hi" }])
46+
47+
expect(out[1].content).toEqual([interrupted("A"), text("hi")])
4048
})
4149

4250
it("inserts a user turn when an assistant turn is followed by another assistant turn", () => {

‎src/core/task/normalizeToolResultPairing.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ type ToolResultBlock = Anthropic.Messages.ToolResultBlockParam
77

88
const INTERRUPTED_RESULT = "Tool execution was interrupted before completion."
99

10+
function interruptedResult(toolUseId: string): ToolResultBlock {
11+
return { type: "tool_result", tool_use_id: toolUseId, content: INTERRUPTED_RESULT, is_error: true }
12+
}
13+
1014
function toBlocks(content: ApiMessage["content"]): Block[] {
1115
if (Array.isArray(content)) return content as Block[]
1216
if (content === undefined || content === null || content === "") return []
@@ -51,9 +55,9 @@ export function normalizeToolResultPairing(messages: ApiMessage[]): ApiMessage[]
5155
}
5256
}
5357

54-
const results: ToolResultBlock[] = toolUseIds.map(
55-
(id) => resultsById.get(id) ?? { type: "tool_result", tool_use_id: id, content: INTERRUPTED_RESULT },
56-
)
58+
// is_error keeps an interrupted call (e.g. attempt_completion) from reading as a success,
59+
// matching the synthetic results written by Task.resumeTaskFromHistory.
60+
const results: ToolResultBlock[] = toolUseIds.map((id) => resultsById.get(id) ?? interruptedResult(id))
5761

5862
if (next.role === "user") {
5963
const rest = nextBlocks.filter((block) => block.type !== "tool_result")

0 commit comments

Comments
 (0)