Skip to content

Commit aa6a4f9

Browse files
authored
fix(ai): keep system updates after pending tool results (#52426)
1 parent 8433dd7 commit aa6a4f9

4 files changed

Lines changed: 72 additions & 8 deletions

File tree

‎packages/ai/src/tool-history.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ const MISSING_TOOL_RESULT = "Tool result missing"
66
export function normalizeToolHistory(messages: ReadonlyArray<Message>) {
77
const normalized: Message[] = []
88
const pending = new Map<string, ToolCallPart>()
9+
// System updates cannot sit between a tool call and its results, so they wait until every pending call is answered.
10+
const held: Message[] = []
11+
const releaseHeld = () => {
12+
if (pending.size > 0) return
13+
normalized.push(...held)
14+
held.length = 0
15+
}
916
const appendMissingResults = () => {
10-
if (pending.size === 0) return
11-
normalized.push(missingToolResults(pending.values()))
17+
if (pending.size > 0) normalized.push(missingToolResults(pending.values()))
1218
pending.clear()
19+
releaseHeld()
1320
}
1421

1522
for (const message of messages) {
@@ -18,6 +25,12 @@ export function normalizeToolHistory(messages: ReadonlyArray<Message>) {
1825
if (message.role === "tool") {
1926
const tool = normalizeToolMessage(message, pending)
2027
if (tool) normalized.push(tool)
28+
releaseHeld()
29+
continue
30+
}
31+
32+
if (message.role === "system" && pending.size > 0) {
33+
held.push(message)
2134
continue
2235
}
2336

‎packages/ai/test/effort-updates.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe("Anthropic Messages effort updates", () => {
147147
}),
148148
)
149149

150-
it.effect("accepts a marker between a tool call and its result", () =>
150+
it.effect("moves a marker between a tool call and its result after the result", () =>
151151
Effect.gen(function* () {
152152
const prepared = yield* compileRequest(
153153
LLM.request({
@@ -166,8 +166,8 @@ describe("Anthropic Messages effort updates", () => {
166166
expect(prepared.body.messages).toEqual([
167167
{ role: "user", content: [{ type: "text", text: "Weather?" }] },
168168
{ role: "assistant", content: [{ type: "tool_use", id: "call_1", name: "lookup", input: {} }] },
169-
{ role: "system", content: [], output_config: { effort: "low" } },
170169
{ role: "user", content: [{ type: "tool_result", tool_use_id: "call_1", content: '{"temp":72}' }] },
170+
{ role: "system", content: [], output_config: { effort: "low" } },
171171
])
172172
}),
173173
)

‎packages/ai/test/provider/anthropic-messages.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,9 @@ describe("Anthropic Messages route", () => {
588588
}),
589589
)
590590

591-
it.effect("rejects a system update between a local tool call and its result", () =>
591+
it.effect("moves a system update between a local tool call and its result after the result", () =>
592592
Effect.gen(function* () {
593-
const error = yield* compileRequest(
593+
const prepared = yield* compileRequest(
594594
LLM.request({
595595
model: opus48,
596596
messages: [
@@ -601,9 +601,13 @@ describe("Anthropic Messages route", () => {
601601
],
602602
cache: "none",
603603
}),
604-
).pipe(Effect.flip)
604+
)
605605

606-
expect(error.message).toContain("system updates cannot split a local tool call from its tool result")
606+
expect(prepared.body.messages.slice(1)).toEqual([
607+
{ role: "assistant", content: [{ type: "tool_use", id: "call_1", name: "lookup", input: {} }] },
608+
{ role: "user", content: [{ type: "tool_result", tool_use_id: "call_1", content: '"Done."' }] },
609+
{ role: "system", content: [{ type: "text", text: "Too early.", cache_control: undefined }] },
610+
])
607611
}),
608612
)
609613

‎packages/ai/test/tool-history.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,51 @@ describe("tool history normalization", () => {
112112

113113
expect(normalized[1]?.content[0]).toMatchObject({ name: "lookup", namespace: undefined })
114114
})
115+
116+
test("moves system updates after the results of pending calls", () => {
117+
const calls = Message.assistant([toolCall("first"), toolCall("second")])
118+
const first = toolResult("first", "one", "first", "text")
119+
const second = toolResult("second", "two", "second", "text")
120+
const update = Message.system("First update.")
121+
const later = Message.system("Second update.")
122+
const user = Message.user("Continue.")
123+
const missing = (id: string) =>
124+
Message.tool(ToolResultPart.make({ id, name: id, result: "Tool result missing", resultType: "error" }))
125+
126+
expect(normalizeToolHistory([calls, first, update, later, second, user])).toEqual([
127+
calls,
128+
first,
129+
second,
130+
update,
131+
later,
132+
user,
133+
])
134+
expect(normalizeToolHistory([Message.assistant(toolCall("first")), update, user])).toEqual([
135+
Message.assistant(toolCall("first")),
136+
missing("first"),
137+
update,
138+
user,
139+
])
140+
expect(normalizeToolHistory([Message.assistant(toolCall("first")), update])).toEqual([
141+
Message.assistant(toolCall("first")),
142+
missing("first"),
143+
update,
144+
])
145+
})
146+
147+
test("moves effort updates after the results of pending calls", () => {
148+
const call = Message.assistant(toolCall("first"))
149+
const result = toolResult("first", "one", "first", "text")
150+
const effort = Message.effort({ effort: "low", previous: "high" })
151+
152+
expect(normalizeToolHistory([call, effort, result])).toEqual([call, result, effort])
153+
})
154+
155+
test("keeps system updates in place when no call is pending", () => {
156+
const history = [Message.assistant(toolCall("first")), toolResult("first", "one", "first", "text")]
157+
const update = Message.system("Update.")
158+
const input = [Message.user("Start."), update, ...history, update, Message.user("Continue.")]
159+
160+
expect(normalizeToolHistory(input)).toBe(input)
161+
})
115162
})

0 commit comments

Comments
 (0)