Skip to content

Commit 73038d0

Browse files
committed
test(anthropic): bind the real tool_result grouping and cover mixed interleave
Addresses edelauna's two review asks on #1705. 1. presentAssistantMessage-images.spec.ts bound a hand-copied partition of Task.pushToolResultToUserContent, so the #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 #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.
1 parent 4db2707 commit 73038d0

3 files changed

Lines changed: 61 additions & 25 deletions

File tree

‎src/core/assistant-message/__tests__/presentAssistantMessage-images.spec.ts‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-images.spec.ts
22

3-
import { describe, it, expect, beforeEach, vi } from "vitest"
3+
import { describe, it, expect, beforeEach, beforeAll, vi } from "vitest"
4+
import type { Anthropic } from "@anthropic-ai/sdk"
45
import { presentAssistantMessage } from "../presentAssistantMessage"
56
import { validateToolUse } from "../../tools/validateToolUse"
67
import type { RequestPolicySnapshot } from "../../prompts/tools/effective-tool-policy"
@@ -33,6 +34,21 @@ const baseSnapshot: RequestPolicySnapshot = {
3334
customModes: [],
3435
}
3536

37+
// The real Task.prototype.pushToolResultToUserContent, imported past the
38+
// module-level automock. Binding the real implementation (rather than a copy of
39+
// its partition logic) is what makes the #1307 tests non-tautological: deleting
40+
// the grouping from Task.ts makes these tests fail.
41+
type RealPushToolResult = (
42+
this: { userMessageContent: UserContentBlock[] },
43+
toolResult: Anthropic.ToolResultBlockParam,
44+
) => boolean
45+
let realPushToolResultToUserContent: RealPushToolResult
46+
47+
beforeAll(async () => {
48+
const actual = await vi.importActual<typeof import("../../task/Task")>("../../task/Task")
49+
realPushToolResultToUserContent = actual.Task.prototype.pushToolResultToUserContent
50+
})
51+
3652
describe("presentAssistantMessage - Image Handling in Native Tool Calling", () => {
3753
let mockTask: any
3854

@@ -76,27 +92,11 @@ describe("presentAssistantMessage - Image Handling in Native Tool Calling", () =
7692
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
7793
}
7894

79-
// Mirrors Task.pushToolResultToUserContent: duplicate guard plus keeping
80-
// tool_result blocks contiguous by moving image blocks after the new result.
81-
mockTask.pushToolResultToUserContent = vi.fn().mockImplementation((toolResult: any) => {
82-
const existingResult = mockTask.userMessageContent.find(
83-
(block: any) => block.type === "tool_result" && block.tool_use_id === toolResult.tool_use_id,
84-
)
85-
if (existingResult) {
86-
return false
87-
}
88-
mockTask.userMessageContent.push(toolResult)
89-
const results = mockTask.userMessageContent.filter(
90-
(block: UserContentBlock) => block.type === "tool_result",
91-
)
92-
if (results.length !== mockTask.userMessageContent.length) {
93-
const others = mockTask.userMessageContent.filter(
94-
(block: UserContentBlock) => block.type !== "tool_result",
95-
)
96-
mockTask.userMessageContent.splice(0, mockTask.userMessageContent.length, ...results, ...others)
97-
}
98-
return true
99-
})
95+
// Bind the REAL Task.prototype method to this structural double: it only
96+
// reads and rewrites `userMessageContent`, which the double provides.
97+
mockTask.pushToolResultToUserContent = vi.fn((toolResult: Anthropic.ToolResultBlockParam) =>
98+
realPushToolResultToUserContent.call(mockTask, toolResult),
99+
)
100100
})
101101

102102
it("should preserve images in tool_result for native tool calling", async () => {
@@ -378,7 +378,7 @@ describe("presentAssistantMessage - Image Handling in Native Tool Calling", () =
378378
})
379379
})
380380

381-
await presentAssistantMessage(mockTask)
381+
await presentAssistantMessage(mockTask, baseSnapshot)
382382

383383
// Anthropic rejects user messages where image blocks sit between
384384
// tool_result blocks, so images from both tools must be grouped
@@ -439,7 +439,7 @@ describe("presentAssistantMessage - Image Handling in Native Tool Calling", () =
439439
return Promise.resolve({ response: "yesButtonClicked", text: "answer b", images: undefined })
440440
})
441441

442-
await presentAssistantMessage(mockTask)
442+
await presentAssistantMessage(mockTask, baseSnapshot)
443443

444444
expect(mockTask.userMessageContent.map((block: UserContentBlock) => block.type)).toEqual([
445445
"tool_result",

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8329,4 +8329,40 @@ describe("pushToolResultToUserContent", () => {
83298329
expect(task.userMessageContent[2]).toEqual(image1)
83308330
expect(task.userMessageContent[3]).toEqual(image2)
83318331
})
8332+
8333+
it("moves text as well as images behind the tool_results, preserving order within each group", () => {
8334+
const task = new Task({
8335+
provider: mockProvider,
8336+
apiConfiguration: mockApiConfig,
8337+
task: "test task",
8338+
startTask: false,
8339+
})
8340+
8341+
const toolResult1: Anthropic.ToolResultBlockParam = {
8342+
type: "tool_result",
8343+
tool_use_id: "id-1",
8344+
content: "Result 1",
8345+
}
8346+
const toolResult2: Anthropic.ToolResultBlockParam = {
8347+
type: "tool_result",
8348+
tool_use_id: "id-2",
8349+
content: "Result 2",
8350+
}
8351+
const image: Anthropic.ImageBlockParam = {
8352+
type: "image",
8353+
source: { type: "base64", media_type: "image/png", data: "img1" },
8354+
}
8355+
const text1: Anthropic.TextBlockParam = { type: "text", text: "text one" }
8356+
const text2: Anthropic.TextBlockParam = { type: "text", text: "text two" }
8357+
8358+
// Interleaved arrival: image, text, r1, more text, r2. Grouping only
8359+
// images would leave text1 ahead of r1, which Anthropic rejects; every
8360+
// non-tool_result block must move behind the contiguous result run.
8361+
task.userMessageContent.push(image, text1)
8362+
task.pushToolResultToUserContent(toolResult1)
8363+
task.userMessageContent.push(text2)
8364+
task.pushToolResultToUserContent(toolResult2)
8365+
8366+
expect(task.userMessageContent).toEqual([toolResult1, toolResult2, image, text1, text2])
8367+
})
83328368
})

‎src/eslint-suppressions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@
571571
},
572572
"core/assistant-message/__tests__/presentAssistantMessage-images.spec.ts": {
573573
"@typescript-eslint/no-explicit-any": {
574-
"count": 16
574+
"count": 14
575575
}
576576
},
577577
"core/assistant-message/__tests__/presentAssistantMessage-unknown-tool.spec.ts": {

0 commit comments

Comments
 (0)