Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/core/task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ import { type AutoDenyDetail, AutoApprovalHandler, checkAutoApproval } from "../
import { MessageManager } from "../message-manager"
import { validateAndFixToolResultIds } from "./validateToolResultIds"
import { mergeConsecutiveApiMessages } from "./mergeConsecutiveApiMessages"
import { normalizeToolResultPairing } from "./normalizeToolResultPairing"
import { prepareApiConversationMessage } from "./apiConversationHistory"
import { shouldAddUserMessageToHistory } from "./messageCounting"
import { type TaskExecutionContext } from "./providerHandoff"
Expand Down Expand Up @@ -575,7 +576,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
}

/**
* Push a tool_result block to userMessageContent, preventing duplicates.
* Push a tool_result block to userMessageContent after any existing tool_results
* (ahead of images and text), preventing duplicates.
* Duplicate tool_use_ids cause API errors.
*
* Keeps all tool_result blocks first and contiguous, as the Anthropic API
Expand Down Expand Up @@ -609,7 +611,6 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
)
const others = this.userMessageContent.filter((block) => block.type !== "tool_result")
this.userMessageContent.splice(0, this.userMessageContent.length, ...results, ...others)

return true
}

Expand Down Expand Up @@ -5880,7 +5881,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
// For API only: merge consecutive user messages (excludes summary messages per
// mergeConsecutiveApiMessages implementation) without mutating stored history.
const mergedForApi = mergeConsecutiveApiMessages(messagesSinceLastSummary, { roles: ["user"] })
const messagesWithoutImages = maybeRemoveImageBlocks(mergedForApi, this.api)
const messagesWithoutImages = maybeRemoveImageBlocks(normalizeToolResultPairing(mergedForApi), this.api)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test showing this call runs before maybeRemoveImageBlocks? If someone removes it or moves it after, the current tests would still pass. The attemptApiRequest harness in Task.spec.ts (~2995-3025) looks like it could assert on the history that reaches createMessage.

const cleanConversationHistory = this.buildCleanConversationHistory(
messagesWithoutImages as ApiMessage[],
requestModelInfo,
Expand Down
24 changes: 24 additions & 0 deletions src/core/task/__tests__/Task.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8256,6 +8256,30 @@ describe("pushToolResultToUserContent", () => {
warnSpy.mockRestore()
})

it("keeps tool_results ahead of images queued by earlier parallel tool calls", () => {
const task = new Task({
provider: mockProvider,
apiConfiguration: mockApiConfig,
task: "test task",
startTask: false,
})
const image: Anthropic.ImageBlockParam = {
type: "image",
source: { type: "base64", media_type: "image/png", data: "AAAA" },
}
const result = (id: string): Anthropic.ToolResultBlockParam => ({
type: "tool_result",
tool_use_id: id,
content: id,
})

task.pushToolResultToUserContent(result("A"))
task.userMessageContent.push(image)
task.pushToolResultToUserContent(result("B"))

expect(task.userMessageContent).toEqual([result("A"), result("B"), image])
})

it("should allow different tool_use_ids to be added", () => {
const task = new Task({
provider: mockProvider,
Expand Down
89 changes: 89 additions & 0 deletions src/core/task/__tests__/normalizeToolResultPairing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { Anthropic } from "@anthropic-ai/sdk"

import type { ApiMessage } from "../../task-persistence"
import { normalizeToolResultPairing } from "../normalizeToolResultPairing"

type Block = Anthropic.Messages.ContentBlockParam

const use = (id: string): Block => ({ type: "tool_use", id, name: "read_file", input: {} })
const result = (id: string, content = `result ${id}`): Block => ({ type: "tool_result", tool_use_id: id, content })
const interrupted = (id: string): Block => ({
type: "tool_result",
tool_use_id: id,
content: "Tool execution was interrupted before completion.",
is_error: true,
})
const text = (value: string): Block => ({ type: "text", text: value })
const image: Block = { type: "image", source: { type: "base64", media_type: "image/png", data: "AAAA" } }
const assistant = (...content: Block[]): ApiMessage => ({ role: "assistant", content })
const user = (...content: Block[]): ApiMessage => ({ role: "user", content })

describe("normalizeToolResultPairing", () => {
it("moves interleaved tool results ahead of other content", () => {
const input = [
user(text("go")),
assistant(text("ok"), use("A"), use("B")),
user(result("A"), image, result("B")),
]

expect(normalizeToolResultPairing(input)[2].content).toEqual([result("A"), result("B"), image])
})

it("orders results by tool_use order and drops duplicates", () => {
const input = [assistant(use("A"), use("B")), user(result("B"), result("A"), result("A", "dup"))]

expect(normalizeToolResultPairing(input)[1].content).toEqual([result("A"), result("B")])
})

it("adds a synthetic error result for an interrupted tool_use", () => {
const input = [assistant(use("A"), use("B")), user(result("A"), text("next"))]

expect(normalizeToolResultPairing(input)[1].content).toEqual([result("A"), interrupted("B"), text("next")])
})

it("preserves string content in the user turn after a tool_use", () => {
const out = normalizeToolResultPairing([assistant(use("A")), { role: "user", content: "hi" }])

expect(out[1].content).toEqual([interrupted("A"), text("hi")])
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

it("inserts a user turn when an assistant turn is followed by another assistant turn", () => {
const out = normalizeToolResultPairing([assistant(use("A")), assistant(text("again"))])

expect(out.map((m) => m.role)).toEqual(["assistant", "user", "assistant"])
expect(out[1].content).toMatchObject([{ type: "tool_result", tool_use_id: "A" }])
})

it.each([
{ name: "result for an unknown id", messages: [assistant(use("A")), user(result("A"), result("X"))] },
{
name: "result after a text-only assistant turn",
messages: [assistant(text("hi")), user(result("X"), text("q"))],
},
])("drops orphan tool results: $name", ({ messages }) => {
const ids = normalizeToolResultPairing(messages)
.flatMap((m) => (Array.isArray(m.content) ? m.content : []))
.flatMap((b) => (b.type === "tool_result" ? [b.tool_use_id] : []))

expect(ids).not.toContain("X")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this assert the full output with toEqual? With only not.toContain("X"), a bug that drops every tool_result, or drops the "q" text, would still pass.

})

it("removes a user message that only held orphan results", () => {
expect(normalizeToolResultPairing([assistant(text("hi")), user(result("X"))])).toHaveLength(1)
})

it("leaves a trailing assistant tool_use and well-formed history unchanged", () => {
const wellFormed = [user(text("go")), assistant(use("A")), user(result("A"), text("done")), assistant(use("B"))]

expect(normalizeToolResultPairing(wellFormed)).toEqual(wellFormed)
})

it("does not mutate its input", () => {
const input = [assistant(use("A")), user(image, result("A"))]
const snapshot = structuredClone(input)

normalizeToolResultPairing(input)

expect(input).toEqual(snapshot)
})
})
83 changes: 83 additions & 0 deletions src/core/task/normalizeToolResultPairing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Anthropic } from "@anthropic-ai/sdk"

import type { ApiMessage } from "../task-persistence"

type Block = Anthropic.Messages.ContentBlockParam
type ToolResultBlock = Anthropic.Messages.ToolResultBlockParam

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

function interruptedResult(toolUseId: string): ToolResultBlock {
return { type: "tool_result", tool_use_id: toolUseId, content: INTERRUPTED_RESULT, is_error: true }
}

function toBlocks(content: ApiMessage["content"]): Block[] {
if (Array.isArray(content)) return content as Block[]
if (content === undefined || content === null || content === "") return []

Check warning on line 16 in src/core/task/normalizeToolResultPairing.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/task/normalizeToolResultPairing.ts:16: 9 mutation test gaps; example: NoCoverage ArrayDeclaration mutant (replacement: ["Stryker was here"]). See the job summary for the complete list and resolution guidance.
return [{ type: "text", text: String(content) }]
}

function toolUseIdsOf(message: ApiMessage): string[] {
if (message.role !== "assistant" || !Array.isArray(message.content)) return []

Check warning on line 21 in src/core/task/normalizeToolResultPairing.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/task/normalizeToolResultPairing.ts:21: 4 mutation test gaps; example: NoCoverage ArrayDeclaration mutant (replacement: ["Stryker was here"]). See the job summary for the complete list and resolution guidance.
return (message.content as Block[]).flatMap((block) => (block.type === "tool_use" ? [block.id] : []))
}

/**
* Shapes outgoing history so tool results satisfy Anthropic's adjacency rule: every
* `tool_use` in an assistant message is answered by a `tool_result` in the very next
* user message, `tool_result` blocks come first, and no `tool_result` refers to a
* `tool_use` outside the immediately preceding assistant message.
*
* - Results are moved ahead of other content, in `tool_use` order (duplicates dropped).
* - Results without a matching `tool_use` are dropped.
* - Interrupted calls get a synthetic error result.
*
* Used for API request shaping only; stored history is not mutated. A trailing assistant
* message is left alone because its results have not been produced yet.
*/
export function normalizeToolResultPairing(messages: ApiMessage[]): ApiMessage[] {
const out: ApiMessage[] = []

for (let i = 0; i < messages.length; i++) {
const message = messages[i]

if (message.role === "assistant") {
out.push(message)
const toolUseIds = toolUseIdsOf(message)
if (toolUseIds.length === 0 || i === messages.length - 1) continue

const next = messages[i + 1]
const nextBlocks = next.role === "user" ? toBlocks(next.content) : []

Check warning on line 50 in src/core/task/normalizeToolResultPairing.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/task/normalizeToolResultPairing.ts:50: 2 mutation test gaps; example: Survived ConditionalExpression mutant (replacement: true). See the job summary for the complete list and resolution guidance.
const resultsById = new Map<string, ToolResultBlock>()
for (const block of nextBlocks) {
if (block.type === "tool_result" && !resultsById.has(block.tool_use_id)) {

Check warning on line 53 in src/core/task/normalizeToolResultPairing.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/task/normalizeToolResultPairing.ts:53: Survived ConditionalExpression mutant (replacement: true). See the job summary for the complete list and resolution guidance.
resultsById.set(block.tool_use_id, block)
}
}

// is_error keeps an interrupted call (e.g. attempt_completion) from reading as a success,
// matching the synthetic results written by Task.resumeTaskFromHistory.
const results: ToolResultBlock[] = toolUseIds.map((id) => resultsById.get(id) ?? interruptedResult(id))

if (next.role === "user") {
const rest = nextBlocks.filter((block) => block.type !== "tool_result")
out.push({ ...next, content: [...results, ...rest] })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do any tests check that fields such as ts, isSummary, condenseParent and reasoning survive the { ...next, content } spread? Every fixture holds only role and content, so a bare { role, content } would pass. The same question applies to line 79.

i++
} else {
// Another assistant message follows: answer the calls with a user turn of their own.
out.push({ role: "user", content: results })
}
continue
}

// A user message not directly after a tool_use turn cannot carry tool results.
if (!Array.isArray(message.content) || !message.content.some((block) => block.type === "tool_result")) {

Check warning on line 74 in src/core/task/normalizeToolResultPairing.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/task/normalizeToolResultPairing.ts:74: 3 mutation test gaps; example: Survived ConditionalExpression mutant (replacement: false). See the job summary for the complete list and resolution guidance.
out.push(message)
continue
}
const kept = (message.content as Block[]).filter((block) => block.type !== "tool_result")

Check warning on line 78 in src/core/task/normalizeToolResultPairing.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/task/normalizeToolResultPairing.ts:78: 2 mutation test gaps; example: Survived ArrowFunction mutant (replacement: () => undefined). See the job summary for the complete list and resolution guidance.
if (kept.length > 0) out.push({ ...message, content: kept })

Check warning on line 79 in src/core/task/normalizeToolResultPairing.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/task/normalizeToolResultPairing.ts:79: 2 mutation test gaps; example: Survived ConditionalExpression mutant (replacement: false). See the job summary for the complete list and resolution guidance.
}

return out
}
Loading