Skip to content

Commit 763a08a

Browse files
myk1ytZoo (VP)edelauna
authored andcommitted
fix(anthropic): keep tool_result blocks contiguous by grouping image blocks separately (Zoo-Code-Org#2243)
* fix(anthropic): keep tool_result blocks contiguous by grouping image blocks separately When a turn issued multiple tool_use blocks and tool results carried image blocks (read_file on images, screenshots, feedback images), presentAssistantMessage pushed each tool_result followed immediately by its images, yielding tool_result/image interleaving in userMessageContent. Task.ts then consumed the array verbatim, and the Anthropic API rejected the request with a 400: "tool_use ids were found without tool_result blocks immediately after" (Zoo-Code-Org#1307). pushToolResultToUserContent is the single choke point every tool_result push flows through, so it now maintains the invariant: when a new tool_result is appended, image blocks already accumulated are moved after it. The resulting user message is tool_results first (in tool_use order), then images, then other blocks — matching the shape the issue expects. A single tool result with images and image-free messages are unchanged. * fix(anthropic): keep all tool_result blocks ahead of other user-message content * test(anthropic): bind the real tool_result grouping and cover mixed interleave Addresses edelauna's two review asks on Zoo-Code-Org#1705. 1. presentAssistantMessage-images.spec.ts bound a hand-copied partition of Task.pushToolResultToUserContent, so the Zoo-Code-Org#1307 tests were tautological: deleting the grouping from Task.ts still passed them. The mock now binds the real Task.prototype.pushToolResultToUserContent via vi.importActual. Verified: removing the partition from Task.ts fails both Zoo-Code-Org#1307 tests. 2. Task.spec.ts only covered "images after the first result". Added the mixed interleave [image, text, r1, text, r2] -> [r1, r2, image, text, text2], which fails under an image-only grouping and only passes with the full tool_result-first partition. Also carries main's new policySnapshot argument at the 2 integration call sites, and drops this spec's no-explicit-any suppressions from 16 to 14. * refactor(task): drop redundant guard in pushToolResultToUserContent --------- Co-authored-by: Zoo (VP) <[email protected]> Co-authored-by: Elliott de Launay <[email protected]> (cherry picked from commit 70eba96)
1 parent a0bd097 commit 763a08a

2 files changed

Lines changed: 110 additions & 8 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,13 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
469469
* Push a tool_result block to userMessageContent, preventing duplicates.
470470
* Duplicate tool_use_ids cause API errors.
471471
*
472+
* Keeps all tool_result blocks first and contiguous, as the Anthropic API
473+
* requires: a user message responding to tool_use blocks must begin with
474+
* the tool_result blocks, and any other content (images, text) between or
475+
* ahead of them is rejected ("tool_use ids were found without tool_result
476+
* blocks immediately after"). All tool_result blocks are therefore kept
477+
* ahead of other content in this array.
478+
*
472479
* @param toolResult - The tool_result block to add
473480
* @returns true if added, false if duplicate was skipped
474481
*/
@@ -484,6 +491,16 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
484491
return false
485492
}
486493
this.userMessageContent.push(toolResult)
494+
495+
// Partition into [tool_results, other blocks], preserving order within
496+
// each group, so results stay first and contiguous regardless of what
497+
// other content (images, text) arrived between pushes.
498+
const results = this.userMessageContent.filter(
499+
(block): block is Anthropic.ToolResultBlockParam => block.type === "tool_result",
500+
)
501+
const others = this.userMessageContent.filter((block) => block.type !== "tool_result")
502+
this.userMessageContent.splice(0, this.userMessageContent.length, ...results, ...others)
503+
487504
return true
488505
}
489506
didRejectTool = false

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

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6270,7 +6270,7 @@ describe("pushToolResultToUserContent", () => {
62706270
expect(task.userMessageContent[0]).toEqual(errorResult)
62716271
})
62726272

6273-
it("should not interfere with other content types in userMessageContent", () => {
6273+
it("should preserve other content types while keeping tool_results contiguous", () => {
62746274
const task = new Task({
62756275
provider: mockProvider,
62766276
apiConfiguration: mockApiConfig,
@@ -6279,10 +6279,11 @@ describe("pushToolResultToUserContent", () => {
62796279
})
62806280

62816281
// Add text and image blocks manually
6282-
task.userMessageContent.push(
6283-
{ type: "text", text: "Some text" },
6284-
{ type: "image", source: { type: "base64", media_type: "image/png", data: "base64data" } },
6285-
)
6282+
const image: Anthropic.ImageBlockParam = {
6283+
type: "image",
6284+
source: { type: "base64", media_type: "image/png", data: "base64data" },
6285+
}
6286+
task.userMessageContent.push({ type: "text", text: "Some text" }, image)
62866287

62876288
const toolResult: Anthropic.ToolResultBlockParam = {
62886289
type: "tool_result",
@@ -6294,8 +6295,92 @@ describe("pushToolResultToUserContent", () => {
62946295

62956296
expect(added).toBe(true)
62966297
expect(task.userMessageContent).toHaveLength(3)
6297-
expect(task.userMessageContent[0].type).toBe("text")
6298-
expect(task.userMessageContent[1].type).toBe("image")
6299-
expect(task.userMessageContent[2]).toEqual(toolResult)
6298+
// Results must lead the message (Anthropic rejects other content ahead
6299+
// of or between tool_results), so the pre-existing text and image move
6300+
// after the result; blocks keep their relative order within each group.
6301+
expect(task.userMessageContent[0]).toEqual(toolResult)
6302+
expect(task.userMessageContent[1].type).toBe("text")
6303+
expect(task.userMessageContent[2]).toEqual(image)
6304+
})
6305+
6306+
it("keeps tool_result blocks contiguous when a later result has no images (GitHub #1307)", () => {
6307+
const task = new Task({
6308+
provider: mockProvider,
6309+
apiConfiguration: mockApiConfig,
6310+
task: "test task",
6311+
startTask: false,
6312+
})
6313+
6314+
const toolResult1: Anthropic.ToolResultBlockParam = {
6315+
type: "tool_result",
6316+
tool_use_id: "id-1",
6317+
content: "Result 1",
6318+
}
6319+
const toolResult2: Anthropic.ToolResultBlockParam = {
6320+
type: "tool_result",
6321+
tool_use_id: "id-2",
6322+
content: "Result 2",
6323+
}
6324+
const image1: Anthropic.ImageBlockParam = {
6325+
type: "image",
6326+
source: { type: "base64", media_type: "image/png", data: "img1" },
6327+
}
6328+
const image2: Anthropic.ImageBlockParam = {
6329+
type: "image",
6330+
source: { type: "base64", media_type: "image/png", data: "img2" },
6331+
}
6332+
6333+
// First tool result arrives with its images appended right after it (as
6334+
// presentAssistantMessage does); a second result without images follows.
6335+
task.pushToolResultToUserContent(toolResult1)
6336+
task.userMessageContent.push(image1, image2)
6337+
task.pushToolResultToUserContent(toolResult2)
6338+
6339+
expect(task.userMessageContent.map((block) => block.type)).toEqual([
6340+
"tool_result",
6341+
"tool_result",
6342+
"image",
6343+
"image",
6344+
])
6345+
expect(task.userMessageContent[0]).toEqual(toolResult1)
6346+
expect(task.userMessageContent[1]).toEqual(toolResult2)
6347+
expect(task.userMessageContent[2]).toEqual(image1)
6348+
expect(task.userMessageContent[3]).toEqual(image2)
6349+
})
6350+
6351+
it("moves text as well as images behind the tool_results, preserving order within each group", () => {
6352+
const task = new Task({
6353+
provider: mockProvider,
6354+
apiConfiguration: mockApiConfig,
6355+
task: "test task",
6356+
startTask: false,
6357+
})
6358+
6359+
const toolResult1: Anthropic.ToolResultBlockParam = {
6360+
type: "tool_result",
6361+
tool_use_id: "id-1",
6362+
content: "Result 1",
6363+
}
6364+
const toolResult2: Anthropic.ToolResultBlockParam = {
6365+
type: "tool_result",
6366+
tool_use_id: "id-2",
6367+
content: "Result 2",
6368+
}
6369+
const image: Anthropic.ImageBlockParam = {
6370+
type: "image",
6371+
source: { type: "base64", media_type: "image/png", data: "img1" },
6372+
}
6373+
const text1: Anthropic.TextBlockParam = { type: "text", text: "text one" }
6374+
const text2: Anthropic.TextBlockParam = { type: "text", text: "text two" }
6375+
6376+
// Interleaved arrival: image, text, r1, more text, r2. Grouping only
6377+
// images would leave text1 ahead of r1, which Anthropic rejects; every
6378+
// non-tool_result block must move behind the contiguous result run.
6379+
task.userMessageContent.push(image, text1)
6380+
task.pushToolResultToUserContent(toolResult1)
6381+
task.userMessageContent.push(text2)
6382+
task.pushToolResultToUserContent(toolResult2)
6383+
6384+
expect(task.userMessageContent).toEqual([toolResult1, toolResult2, image, text1, text2])
63006385
})
63016386
})

0 commit comments

Comments
 (0)