Skip to content

Commit 4935751

Browse files
PierrunoYTclaude
andcommitted
fix(task): lead each request user message with its tool_result blocks
History saved with image blocks between tool_result blocks (read_file on an image alongside another tool call in the same turn) is rejected by Anthropic with a 400 on every retry, leaving the task stuck. Reorder tool_result blocks to the front of each user message when shaping the API request, after merging consecutive user messages. Stored history is unchanged, so tasks already saved with the broken shape recover on their next retry. This complements Zoo-Code-Org#1705, which stops new messages from being built with the interleaved shape. Fixes Zoo-Code-Org#1774 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent ca05b73 commit 4935751

4 files changed

Lines changed: 181 additions & 1 deletion

File tree

‎src/core/task/Task.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ import { type AutoDenyDetail, AutoApprovalHandler, checkAutoApproval } from "../
140140
import { MessageManager } from "../message-manager"
141141
import { validateAndFixToolResultIds } from "./validateToolResultIds"
142142
import { mergeConsecutiveApiMessages } from "./mergeConsecutiveApiMessages"
143+
import { orderToolResultsFirst } from "./orderToolResultsFirst"
143144
import { prepareApiConversationMessage } from "./apiConversationHistory"
144145
import { shouldAddUserMessageToHistory } from "./messageCounting"
145146
import { type TaskExecutionContext } from "./providerHandoff"
@@ -5830,7 +5831,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
58305831
const messagesSinceLastSummary = getMessagesSinceLastSummary(effectiveHistory)
58315832
// For API only: merge consecutive user messages (excludes summary messages per
58325833
// mergeConsecutiveApiMessages implementation) without mutating stored history.
5833-
const mergedForApi = mergeConsecutiveApiMessages(messagesSinceLastSummary, { roles: ["user"] })
5834+
// Then lead each user message with its tool_result blocks, which also repairs history saved
5835+
// with images between results (#1774) so a stuck task can retry successfully.
5836+
const mergedForApi = orderToolResultsFirst(
5837+
mergeConsecutiveApiMessages(messagesSinceLastSummary, { roles: ["user"] }),
5838+
)
58345839
const messagesWithoutImages = maybeRemoveImageBlocks(mergedForApi, this.api)
58355840
const cleanConversationHistory = this.buildCleanConversationHistory(
58365841
messagesWithoutImages as ApiMessage[],

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,6 +2971,63 @@ describe("Cline", () => {
29712971
expect(Object.keys(cleanConversationHistory[0]!)).toEqual(["role", "content"])
29722972
})
29732973

2974+
it("sends tool_result blocks first when stored history has images between them (#1774)", async () => {
2975+
const cline = new Task({
2976+
provider: mockProvider,
2977+
apiConfiguration: mockApiConfig,
2978+
task: "test task",
2979+
startTask: false,
2980+
})
2981+
vi.spyOn(getTaskTestAccess(cline), "getSystemPrompt").mockResolvedValue("mock system prompt")
2982+
const mockStream = {
2983+
async *[Symbol.asyncIterator]() {
2984+
yield { type: "text", text: "response" }
2985+
},
2986+
} as AsyncGenerator<ApiStreamChunk>
2987+
const createMessageSpy = vi.spyOn(cline.api, "createMessage").mockReturnValue(mockStream)
2988+
2989+
// History as persisted by a turn that read an image alongside another tool call.
2990+
cline.apiConversationHistory = [
2991+
{ role: "user", content: [{ type: "text", text: "read the logo and list files" }], ts: 1 },
2992+
{
2993+
role: "assistant",
2994+
content: [
2995+
{ type: "tool_use", id: "read", name: "read_file", input: { path: "logo.jpg" } },
2996+
{ type: "tool_use", id: "cmd", name: "execute_command", input: { command: "ls" } },
2997+
],
2998+
ts: 2,
2999+
},
3000+
{
3001+
role: "user",
3002+
content: [
3003+
{ type: "tool_result", tool_use_id: "read", content: "File: logo.jpg" },
3004+
{ type: "image", source: { type: "base64", media_type: "image/jpeg", data: "abc" } },
3005+
{ type: "tool_result", tool_use_id: "cmd", content: "Exit code: 0" },
3006+
{ type: "text", text: "<environment_details>" },
3007+
],
3008+
ts: 3,
3009+
},
3010+
]
3011+
3012+
const iterator = cline.attemptApiRequest(0)
3013+
await iterator.next()
3014+
3015+
const [, sentHistory] = requireDefined(createMessageSpy.mock.calls[0])
3016+
const lastUserContent = requireDefined(sentHistory.at(-1)).content as Array<{ type: string }>
3017+
expect(lastUserContent.slice(0, 2)).toMatchObject([
3018+
{ type: "tool_result", tool_use_id: "read" },
3019+
{ type: "tool_result", tool_use_id: "cmd" },
3020+
])
3021+
expect(lastUserContent.slice(2).some((block) => block.type === "tool_result")).toBe(false)
3022+
// The stored history keeps its original shape.
3023+
expect(requireDefined(cline.apiConversationHistory.at(-1)).content).toMatchObject([
3024+
{ type: "tool_result" },
3025+
{ type: "image" },
3026+
{ type: "tool_result" },
3027+
{ type: "text" },
3028+
])
3029+
})
3030+
29743031
it("should shape image blocks for API compatibility before request construction", async () => {
29753032
const conversationHistory = [
29763033
{
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// npx vitest run core/task/__tests__/orderToolResultsFirst.spec.ts
2+
3+
import type { Anthropic } from "@anthropic-ai/sdk"
4+
5+
import type { ApiMessage } from "../../task-persistence"
6+
import { mergeConsecutiveApiMessages } from "../mergeConsecutiveApiMessages"
7+
import { orderToolResultsFirst } from "../orderToolResultsFirst"
8+
9+
const toolResult = (id: string, content = `result ${id}`): Anthropic.Messages.ToolResultBlockParam => ({
10+
type: "tool_result",
11+
tool_use_id: id,
12+
content,
13+
})
14+
const image = (data: string): Anthropic.Messages.ImageBlockParam => ({
15+
type: "image",
16+
source: { type: "base64", media_type: "image/jpeg", data },
17+
})
18+
const text = (value: string): Anthropic.Messages.TextBlockParam => ({ type: "text", text: value })
19+
20+
describe("orderToolResultsFirst", () => {
21+
it("repairs the persisted #1774 shape so every tool_result leads the message", () => {
22+
// [tool_result, image, tool_result, text] as saved when read_file on an image ran
23+
// alongside another tool call in the same turn.
24+
const stuck: ApiMessage = {
25+
role: "user",
26+
ts: 16,
27+
content: [toolResult("read"), image("jpeg"), toolResult("cmd"), text("<environment_details>")],
28+
}
29+
30+
const [repaired] = orderToolResultsFirst([stuck])
31+
32+
expect(repaired.content).toEqual([
33+
toolResult("read"),
34+
toolResult("cmd"),
35+
image("jpeg"),
36+
text("<environment_details>"),
37+
])
38+
expect(repaired.ts).toBe(16)
39+
// Stored history is untouched.
40+
expect(stuck.content).toEqual([
41+
toolResult("read"),
42+
image("jpeg"),
43+
toolResult("cmd"),
44+
text("<environment_details>"),
45+
])
46+
})
47+
48+
it("keeps the order of results and of other blocks with several interleaved images", () => {
49+
const [repaired] = orderToolResultsFirst([
50+
{
51+
role: "user",
52+
content: [toolResult("a"), toolResult("b"), image("1"), toolResult("c"), image("2"), text("t")],
53+
},
54+
])
55+
56+
expect(repaired.content).toEqual([
57+
toolResult("a"),
58+
toolResult("b"),
59+
toolResult("c"),
60+
image("1"),
61+
image("2"),
62+
text("t"),
63+
])
64+
})
65+
66+
it("repairs interleaving introduced by merging consecutive user messages", () => {
67+
const merged = mergeConsecutiveApiMessages([
68+
{ role: "user", content: [toolResult("a"), text("note")] },
69+
{ role: "user", content: [toolResult("b")] },
70+
])
71+
72+
expect(orderToolResultsFirst(merged)[0].content).toEqual([toolResult("a"), toolResult("b"), text("note")])
73+
})
74+
75+
it("returns already-ordered, string, and assistant messages unchanged", () => {
76+
const ordered: ApiMessage = { role: "user", content: [toolResult("a"), image("1"), text("t")] }
77+
const plain: ApiMessage = { role: "user", content: "hello" }
78+
const noResults: ApiMessage = { role: "user", content: [text("a"), image("1")] }
79+
const assistant: ApiMessage = { role: "assistant", content: [text("a")] }
80+
81+
const out = orderToolResultsFirst([ordered, plain, noResults, assistant])
82+
83+
out.forEach((message, index) => expect(message).toBe([ordered, plain, noResults, assistant][index]))
84+
})
85+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { ApiMessage } from "../task-persistence"
2+
3+
/**
4+
* Moves `tool_result` blocks to the front of each user message, keeping the relative order of
5+
* the results and of the remaining blocks.
6+
*
7+
* Anthropic requires the results for the previous turn's `tool_use` blocks to lead the next user
8+
* message. History saved with image blocks between results (#1307, #1774), or produced by merging
9+
* consecutive user messages, is otherwise rejected with a 400 on every retry.
10+
*
11+
* Used for *API request shaping only* (do not use for storage). Messages already in order are
12+
* returned unchanged.
13+
*/
14+
export function orderToolResultsFirst(messages: ApiMessage[]): ApiMessage[] {
15+
return messages.map((message) => {
16+
if (message.role !== "user" || !Array.isArray(message.content)) {
17+
return message
18+
}
19+
const firstOther = message.content.findIndex((block) => block.type !== "tool_result")
20+
const hasLaterResult =
21+
firstOther !== -1 && message.content.slice(firstOther).some((block) => block.type === "tool_result")
22+
if (!hasLaterResult) {
23+
return message
24+
}
25+
return {
26+
...message,
27+
content: [
28+
...message.content.filter((block) => block.type === "tool_result"),
29+
...message.content.filter((block) => block.type !== "tool_result"),
30+
],
31+
}
32+
})
33+
}

0 commit comments

Comments
 (0)