Skip to content

Commit 88055e9

Browse files
committed
fix(tool-id): keep thought signatures intact in the tool_use history
1 parent 1c98607 commit 88055e9

5 files changed

Lines changed: 180 additions & 71 deletions

File tree

‎src/api/providers/__tests__/lite-llm.spec.ts‎

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Anthropic } from "@anthropic-ai/sdk"
33

44
import { LiteLLMHandler } from "../lite-llm"
55
import { getModels } from "../fetchers/modelCache"
6+
import { sanitizeToolUseId } from "../../../utils/tool-id"
67
import type { ApiHandlerCreateMessageMetadata } from "../../index"
78
import { ApiHandlerOptions } from "../../../shared/api"
89
import { type ModelRecord, litellmDefaultModelId, litellmDefaultModelInfo } from "@roo-code/types"
@@ -760,46 +761,60 @@ describe("LiteLLMHandler", () => {
760761
expect(assistantMessage.tool_calls[0].provider_specific_fields.thought_signature).toBe(dummySignature)
761762
})
762763

763-
it("should send tool call ids that carry a thought signature unchanged", async () => {
764-
// LiteLLM encodes the Gemini signature into the id; truncating it breaks the signature.
765-
const signature = "AY89a18ErUKBAfkTYnDbzq6mGnRG5QS8VFogncpDzYVXzC0i+aDwY5o/0123456789abcdef=="
766-
const toolCallId = `call_864944__thought__${signature}`
767-
expect(toolCallId.length).toBeGreaterThan(64)
768-
769-
const messages: Anthropic.Messages.MessageParam[] = [
770-
{ role: "user", content: "Hello" },
771-
{
772-
role: "assistant",
773-
content: [{ type: "tool_use", id: toolCallId, name: "read_file", input: { path: "test.txt" } }],
774-
},
775-
{
776-
role: "user",
777-
content: [{ type: "tool_result", tool_use_id: toolCallId, content: "file contents" }],
778-
},
779-
]
780-
mockCreate.mockReturnValue({
781-
withResponse: vi.fn().mockResolvedValue({
782-
data: asyncStreamFrom([{ choices: [{ delta: { content: "ok" } }] }]),
783-
}),
784-
})
785-
const metadata: ApiHandlerCreateMessageMetadata = {
786-
taskId: "task-1",
787-
tools: [
764+
it.each([
765+
{ path: "convertToOpenAiMessages", preserveReasoning: undefined },
766+
{ path: "convertToR1Format", preserveReasoning: true },
767+
])(
768+
"should send tool call ids that carry a thought signature unchanged ($path)",
769+
async ({ preserveReasoning }) => {
770+
const signature = "AY89a18ErUKBAfkTYnDbzq6mGnRG5QS8VFogncpDzYVXzC0i+aDwY5o/0123456789abcdef=="
771+
const toolCallId = `call_864944__thought__${signature}`
772+
const historyId = sanitizeToolUseId(toolCallId)
773+
expect(toolCallId.length).toBeGreaterThan(64)
774+
775+
vi.spyOn(handler, "fetchModel").mockResolvedValue({
776+
id: "gemini-3-pro",
777+
info: { ...litellmDefaultModelInfo, preserveReasoning },
778+
})
779+
const messages: Anthropic.Messages.MessageParam[] = [
780+
{ role: "user", content: "Hello" },
788781
{
789-
type: "function",
790-
function: { name: "read_file", description: "Read a file", parameters: {} },
782+
role: "assistant",
783+
content: [
784+
{ type: "tool_use", id: historyId, name: "read_file", input: { path: "test.txt" } },
785+
],
791786
},
792-
],
793-
}
787+
{
788+
role: "user",
789+
content: [{ type: "tool_result", tool_use_id: historyId, content: "file contents" }],
790+
},
791+
]
792+
mockCreate.mockReturnValue({
793+
withResponse: vi.fn().mockResolvedValue({
794+
data: asyncStreamFrom([{ choices: [{ delta: { content: "ok" } }] }]),
795+
}),
796+
})
797+
const metadata: ApiHandlerCreateMessageMetadata = {
798+
taskId: "task-1",
799+
tools: [
800+
{
801+
type: "function",
802+
function: { name: "read_file", description: "Read a file", parameters: {} },
803+
},
804+
],
805+
}
794806

795-
await collectStream(handler.createMessage("You are a helpful assistant", messages, metadata))
807+
await collectStream(handler.createMessage("You are a helpful assistant", messages, metadata))
796808

797-
const sent: OpenAI.Chat.ChatCompletionMessageParam[] = mockCreate.mock.calls[0][0].messages
798-
const assistantMessage = sent.find((msg) => msg.role === "assistant" && msg.tool_calls?.length)
799-
const toolMessage = sent.find((msg) => msg.role === "tool")
800-
expect(assistantMessage?.role === "assistant" && assistantMessage.tool_calls?.[0].id).toBe(toolCallId)
801-
expect(toolMessage?.role === "tool" && toolMessage.tool_call_id).toBe(toolCallId)
802-
})
809+
const sent: OpenAI.Chat.ChatCompletionMessageParam[] = mockCreate.mock.calls[0][0].messages
810+
const assistantMessage = sent.find((msg) => msg.role === "assistant" && msg.tool_calls?.length)
811+
const toolMessage = sent.find((msg) => msg.role === "tool")
812+
expect(assistantMessage?.role === "assistant" && assistantMessage.tool_calls?.[0].id).toBe(
813+
toolCallId,
814+
)
815+
expect(toolMessage?.role === "tool" && toolMessage.tool_call_id).toBe(toolCallId)
816+
},
817+
)
803818

804819
it("should not inject thought signatures for non-Gemini models", async () => {
805820
const optionsWithGPT4: ApiHandlerOptions = {

‎src/api/transform/__tests__/anthropic-filter.spec.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,39 @@ describe("anthropic-filter", () => {
140140
expect(result).toHaveLength(1)
141141
expect(result[0].content).toEqual([{ type: "text", text: "Valid text" }])
142142
})
143+
144+
it("makes tool ids from a thought-signature history match Anthropic's id pattern", () => {
145+
const historyId = "call_864944__thought__ab+c/d=="
146+
const messages: Anthropic.Messages.MessageParam[] = [
147+
{
148+
role: "assistant",
149+
content: [{ type: "tool_use", id: historyId, name: "read_file", input: {} }],
150+
},
151+
{
152+
role: "user",
153+
content: [{ type: "tool_result", tool_use_id: historyId, content: "ok" }],
154+
},
155+
]
156+
157+
const result = filterNonAnthropicBlocks(messages)
158+
159+
expect(result[0].content).toEqual([
160+
{ type: "tool_use", id: "call_864944__thought__ab_c_d__", name: "read_file", input: {} },
161+
])
162+
expect(result[1].content).toEqual([
163+
{ type: "tool_result", tool_use_id: "call_864944__thought__ab_c_d__", content: "ok" },
164+
])
165+
})
166+
167+
it("leaves valid tool ids unchanged", () => {
168+
const messages: Anthropic.Messages.MessageParam[] = [
169+
{
170+
role: "assistant",
171+
content: [{ type: "tool_use", id: "toolu_01AbC-xyz", name: "read_file", input: {} }],
172+
},
173+
]
174+
175+
expect(filterNonAnthropicBlocks(messages)).toEqual(messages)
176+
})
143177
})
144178
})

‎src/api/transform/anthropic-filter.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
22

3+
import { sanitizeToolIdForStrictProvider } from "../../utils/tool-id"
4+
35
/**
46
* Set of content block types that are valid for Anthropic API.
57
* Only these types will be passed through to the API.
@@ -22,6 +24,9 @@ export const VALID_ANTHROPIC_BLOCK_TYPES = new Set([
2224
* - Internal "reasoning" blocks (Roo Code's internal representation)
2325
* - Gemini's "thoughtSignature" blocks (encrypted reasoning continuity tokens)
2426
* - Any other unknown block types
27+
*
28+
* It also makes tool ids match Anthropic's ^[a-zA-Z0-9_-]+$ pattern. The history keeps
29+
* thought-signature ids from other providers unchanged, and those can contain `+`, `/` and `=`.
2530
*/
2631
export function filterNonAnthropicBlocks(
2732
messages: Anthropic.Messages.MessageParam[],
@@ -32,11 +37,13 @@ export function filterNonAnthropicBlocks(
3237
return message
3338
}
3439

35-
const filteredContent = message.content.filter((block) => {
36-
const blockType = (block as { type: string }).type
37-
// Only keep block types that Anthropic recognizes
38-
return VALID_ANTHROPIC_BLOCK_TYPES.has(blockType)
39-
})
40+
const filteredContent = message.content
41+
.filter((block) => {
42+
const blockType = (block as { type: string }).type
43+
// Only keep block types that Anthropic recognizes
44+
return VALID_ANTHROPIC_BLOCK_TYPES.has(blockType)
45+
})
46+
.map(withStrictToolId)
4047

4148
// If all content was filtered out, return undefined to filter the message later
4249
if (filteredContent.length === 0) {
@@ -50,3 +57,13 @@ export function filterNonAnthropicBlocks(
5057
})
5158
.filter((message): message is Anthropic.Messages.MessageParam => message !== undefined)
5259
}
60+
61+
function withStrictToolId<T extends Anthropic.Messages.ContentBlockParam>(block: T): T {
62+
if (block.type === "tool_use") {
63+
return { ...block, id: sanitizeToolIdForStrictProvider(block.id) }
64+
}
65+
if (block.type === "tool_result") {
66+
return { ...block, tool_use_id: sanitizeToolIdForStrictProvider(block.tool_use_id) }
67+
}
68+
return block
69+
}

‎src/utils/__tests__/tool-id.spec.ts‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import {
33
truncateOpenAiCallId,
44
sanitizeOpenAiCallId,
55
sanitizeLiteLlmCallId,
6+
sanitizeToolIdForStrictProvider,
67
OPENAI_CALL_ID_MAX_LENGTH,
78
} from "../tool-id"
89

10+
const signature = "AY89a18ErUKBAfkTYnDbzq6mGnRG5QS8VFogncpDzYVXzC0i+aDwY5o/0123456789abcdef=="
11+
const thoughtSignatureId = `call_864944__thought__${signature}`
12+
const strictToolIdPattern = /^[a-zA-Z0-9_-]+$/
13+
914
describe("sanitizeToolUseId", () => {
1015
describe("valid IDs pass through unchanged", () => {
1116
it("should preserve alphanumeric IDs", () => {
@@ -84,6 +89,36 @@ describe("sanitizeToolUseId", () => {
8489
})
8590
})
8691

92+
describe("sanitizeToolUseId with a thought signature", () => {
93+
it("keeps the marker and the Base64 signature unchanged", () => {
94+
expect(sanitizeToolUseId(thoughtSignatureId)).toBe(thoughtSignatureId)
95+
})
96+
97+
it("sanitizes only the call id before the marker", () => {
98+
expect(sanitizeToolUseId("call.1:x__thought__ab+c/d=")).toBe("call_1_x__thought__ab+c/d=")
99+
})
100+
101+
it("still replaces + / = in ids without the marker", () => {
102+
expect(sanitizeToolUseId("call_ab+c/d=")).toBe("call_ab_c_d_")
103+
})
104+
})
105+
106+
describe("sanitizeToolIdForStrictProvider", () => {
107+
it.each([thoughtSignatureId, "call.1:x__thought__ab+c/d=", "toolu_mcp.linear.create_issue"])(
108+
"makes %s match ^[a-zA-Z0-9_-]+$",
109+
(id) => {
110+
const result = sanitizeToolIdForStrictProvider(id)
111+
112+
expect(result).toMatch(strictToolIdPattern)
113+
expect(result).toHaveLength(id.length)
114+
},
115+
)
116+
117+
it("leaves a valid id unchanged", () => {
118+
expect(sanitizeToolIdForStrictProvider("toolu_01AbC-xyz_789")).toBe("toolu_01AbC-xyz_789")
119+
})
120+
})
121+
87122
describe("truncateOpenAiCallId", () => {
88123
describe("IDs within limit pass through unchanged", () => {
89124
it("should preserve short IDs", () => {
@@ -192,13 +227,9 @@ describe("OPENAI_CALL_ID_MAX_LENGTH constant", () => {
192227
})
193228

194229
describe("sanitizeLiteLlmCallId", () => {
195-
const signature = "AY89a18ErUKBAfkTYnDbzq6mGnRG5QS8VFogncpDzYVXzC0i+aDwY5o/0123456789abcdef=="
196-
197230
it("keeps an id that carries a thought signature intact, however long", () => {
198-
const id = `call_864944__thought__${signature}`
199-
200-
expect(id.length).toBeGreaterThan(OPENAI_CALL_ID_MAX_LENGTH)
201-
expect(sanitizeLiteLlmCallId(id)).toBe(id)
231+
expect(thoughtSignatureId.length).toBeGreaterThan(OPENAI_CALL_ID_MAX_LENGTH)
232+
expect(sanitizeLiteLlmCallId(thoughtSignatureId)).toBe(thoughtSignatureId)
202233
})
203234

204235
it("sanitizes only the call id before the marker, leaving the Base64 signature intact", () => {
@@ -216,3 +247,12 @@ describe("sanitizeLiteLlmCallId", () => {
216247
expect(sanitizeLiteLlmCallId("toolu_123")).toBe("toolu_123")
217248
})
218249
})
250+
251+
describe("sanitizeOpenAiCallId with a thought signature", () => {
252+
it("returns an id that OpenAI and Bedrock accept", () => {
253+
const result = sanitizeOpenAiCallId(thoughtSignatureId)
254+
255+
expect(result).toMatch(strictToolIdPattern)
256+
expect(result).toHaveLength(OPENAI_CALL_ID_MAX_LENGTH)
257+
})
258+
})

‎src/utils/tool-id.ts‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,32 @@ import * as crypto from "crypto"
77
export const OPENAI_CALL_ID_MAX_LENGTH = 64
88

99
/**
10-
* Sanitize a tool_use ID to match API validation pattern: ^[a-zA-Z0-9_-]+$
11-
* Replaces any invalid character with underscore.
10+
* LiteLLM carries a Gemini thought signature inside the tool call id, as
11+
* `<call id>__thought__<Base64 signature>`. Gemini rejects the next request if the
12+
* signature changes, so the marker and everything after it must stay byte-for-byte intact.
1213
*/
13-
export function sanitizeToolUseId(id: string): string {
14+
export const THOUGHT_SIGNATURE_MARKER = "__thought__"
15+
16+
/**
17+
* Replace every character outside the Anthropic/Bedrock tool id pattern ^[a-zA-Z0-9_-]+$
18+
* with underscore. Use this only at a request boundary that enforces the pattern.
19+
*/
20+
export function sanitizeToolIdForStrictProvider(id: string): string {
1421
return id.replace(/[^a-zA-Z0-9_-]/g, "_")
1522
}
1623

24+
/**
25+
* Sanitize a tool_use ID for the conversation history. Ids that carry a thought signature
26+
* keep the signature unchanged; only the call id before the marker is sanitized.
27+
*/
28+
export function sanitizeToolUseId(id: string): string {
29+
const markerIndex = id.indexOf(THOUGHT_SIGNATURE_MARKER)
30+
if (markerIndex === -1) {
31+
return sanitizeToolIdForStrictProvider(id)
32+
}
33+
return sanitizeToolIdForStrictProvider(id.slice(0, markerIndex)) + id.slice(markerIndex)
34+
}
35+
1736
/**
1837
* Truncate a call_id to fit within OpenAI's 64-character limit.
1938
* Uses a hash suffix to maintain uniqueness when truncation is needed.
@@ -50,30 +69,14 @@ export function truncateOpenAiCallId(id: string, maxLength: number = OPENAI_CALL
5069
* @returns The sanitized and truncated ID
5170
*/
5271
export function sanitizeOpenAiCallId(id: string, maxLength: number = OPENAI_CALL_ID_MAX_LENGTH): string {
53-
// First sanitize characters, then truncate
54-
const sanitized = sanitizeToolUseId(id)
55-
return truncateOpenAiCallId(sanitized, maxLength)
72+
return truncateOpenAiCallId(sanitizeToolIdForStrictProvider(id), maxLength)
5673
}
5774

5875
/**
59-
* LiteLLM carries a Gemini thought signature inside the tool call id, as
60-
* `<call id>__thought__<signature>`. Truncating such an id corrupts the signature.
61-
*/
62-
export const LITELLM_THOUGHT_SIGNATURE_MARKER = "__thought__"
63-
64-
/**
65-
* Normalize a tool call id for LiteLLM. For ids that carry a thought signature only the
66-
* call id before the marker is character-sanitized; the marker and the Base64 signature
67-
* (which may contain `+`, `/` and `=`) pass through unchanged, because Gemini must receive
68-
* the signature intact. Every other id gets the usual OpenAI 64-character treatment.
69-
*
70-
* @param id - The original call_id
71-
* @returns The id to send to LiteLLM
76+
* Normalize a tool call id for LiteLLM. Ids that carry a thought signature are never
77+
* truncated, because Gemini must receive the signature intact. Every other id gets the
78+
* usual OpenAI 64-character treatment.
7279
*/
7380
export function sanitizeLiteLlmCallId(id: string): string {
74-
const markerIndex = id.indexOf(LITELLM_THOUGHT_SIGNATURE_MARKER)
75-
if (markerIndex === -1) {
76-
return sanitizeOpenAiCallId(id)
77-
}
78-
return sanitizeToolUseId(id.slice(0, markerIndex)) + id.slice(markerIndex)
81+
return id.includes(THOUGHT_SIGNATURE_MARKER) ? sanitizeToolUseId(id) : sanitizeOpenAiCallId(id)
7982
}

0 commit comments

Comments
 (0)