Skip to content

Commit da098a4

Browse files
committed
fix(anthropic): allocate tool call ids per request instead of per id
Three defects, all reachable from the same history replay: The raw-id fallback restored values the helper had deliberately refused. `anthropicToolCallId("")` returns undefined precisely because an empty id is not usable, and `?? rawId` put it back on the wire. An unrepresentable call now becomes text and its result follows it there. Result matching compared domains. `requiredIds` held normalized ids while the result scan tested the raw `toolCallId`, so every rewritten pair lost its real result to orphan text AND gained a synthetic missing-result block -- worse than the bug it was meant to repair. Matching is now on the wire id, via a lookup that never mints one, so a result with no call still stays orphan. The transform was not injective despite saying it was. `call:a` maps to `call_a_<hash>`, and a raw id already equal to that value conforms and passes through unchanged: two sources, one wire id. Ordinary 32-bit hash collisions had the same effect. Ids are now allocated by a request-scoped allocator that reserves conforming ids first and appends a numeric suffix when a candidate is taken. Also adds the length bound the issue named. `isConformingToolCallId` now includes it, so a character-valid but over-long id is rewritten rather than reserved verbatim, and candidates are assembled as `prefix_hash` with the prefix truncated -- slicing the finished string would have removed the discriminator. Collision suffixes reserve their own space. `src/adapters/google.ts` shares the allocator rather than aliasing the stateless helper, which would have kept the collision defect there. Regressions in tests/adapter-usage.test.ts drive the real buildRequest path: rewritten pair stays paired, empty id never reaches the wire, rewritten id does not collide with a conforming id equal to its normalized form, result-only id mints nothing, over-length id is rewritten to fit, conforming pairs unchanged.
1 parent 388c4c2 commit da098a4

5 files changed

Lines changed: 341 additions & 27 deletions

File tree

‎src/adapters/anthropic.ts‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IncomingMeta, ProviderAdapter } from "./base";
2-
import { anthropicToolCallId } from "./tool-call-id";
2+
import { createToolCallIdAllocator, type ToolCallIdAllocator } from "./tool-call-id";
33
import { debugDroppedFrame } from "../lib/debug";
44
import type {
55
AdapterEvent,
@@ -560,7 +560,7 @@ function buildToolNameTransforms(provider: OcxProviderConfig): { toWire: (name:
560560
return { toWire: (name) => name, fromWire: (name) => name };
561561
}
562562

563-
function toAnthropicToolResult(msg: OcxToolResultMessage): Record<string, unknown> {
563+
function toAnthropicToolResult(msg: OcxToolResultMessage, wireCallId: string): Record<string, unknown> {
564564
// Anthropic tool_result accepts a string OR content blocks — render images natively
565565
// (e.g. Codex view_image output) instead of dropping them.
566566
let content: string | unknown[];
@@ -575,12 +575,17 @@ function toAnthropicToolResult(msg: OcxToolResultMessage): Record<string, unknow
575575
}
576576
return {
577577
type: "tool_result",
578-
tool_use_id: anthropicToolCallId(msg.toolCallId) ?? msg.toolCallId,
578+
tool_use_id: wireCallId,
579579
content,
580580
...(msg.isError ? { is_error: true } : {}),
581581
};
582582
}
583583

584+
function unrepresentableToolCallText(tc: OcxToolCall, wireName: string): string {
585+
const args = typeof tc.arguments === "string" ? tc.arguments : JSON.stringify(tc.arguments);
586+
return `[tool_use without a usable id: ${wireName}]\n${args}`;
587+
}
588+
584589
function orphanToolResultText(msg: OcxToolResultMessage): string {
585590
const label = msg.toolName ? `${msg.toolName} (${msg.toolCallId})` : msg.toolCallId;
586591
const content = typeof msg.content === "string"
@@ -593,6 +598,19 @@ function messagesToAnthropicFormat(
593598
parsed: OcxParsedRequest,
594599
toolNames: { toWire: (name: string) => string },
595600
): { system: string | undefined; messages: unknown[] } {
601+
// One allocator for the whole request: a tool_result must resolve to the SAME wire id its
602+
// call got, and two distinct raw ids must never collapse into one. Conforming ids are claimed
603+
// first so a rewritten id can never squat on an id another call legitimately owns.
604+
const callIds = createToolCallIdAllocator();
605+
for (const message of parsed.context.messages) {
606+
if (message.role === "assistant") {
607+
for (const part of (message as OcxAssistantMessage).content) {
608+
if (part.type === "toolCall") callIds.reserve((part as OcxToolCall).id);
609+
}
610+
} else if (message.role === "toolResult") {
611+
callIds.reserve((message as OcxToolResultMessage).toolCallId);
612+
}
613+
}
596614
const toolCatalogNudge = buildNonOpenAIToolCatalogNudgeForTools(
597615
parsed.context.tools,
598616
parsed.options.toolChoice,
@@ -646,7 +664,13 @@ function messagesToAnthropicFormat(
646664
const flatName = namespacedToolName(tc.namespace, tc.name);
647665
// Normalized here, and identically for the matching tool_result above, so a history
648666
// replayed from another provider path keeps its call/result pairing (#1767).
649-
const wireCallId = anthropicToolCallId(tc.id) ?? tc.id;
667+
// No raw fallback: restoring an empty/unusable id puts a value on the wire Anthropic
668+
// rejects. An unrepresentable call becomes text instead, and its result follows it there.
669+
const wireCallId = callIds.allocate(tc.id);
670+
if (wireCallId === undefined) {
671+
preface.push({ type: "text", text: unrepresentableToolCallText(tc, toolNames.toWire(flatName)) });
672+
continue;
673+
}
650674
toolUseIds.push(wireCallId);
651675
toolUses.push({ type: "tool_use", id: wireCallId, name: toolNames.toWire(flatName), input: tc.arguments });
652676
}
@@ -664,9 +688,13 @@ function messagesToAnthropicFormat(
664688
let j = i + 1;
665689
while (j < parsed.context.messages.length && parsed.context.messages[j].role === "toolResult") {
666690
const tr = parsed.context.messages[j] as OcxToolResultMessage;
667-
if (requiredIds.has(tr.toolCallId) && !seen.has(tr.toolCallId)) {
668-
resultBlocks.push(toAnthropicToolResult(tr));
669-
seen.add(tr.toolCallId);
691+
// Match on the WIRE id. requiredIds holds normalized ids, so comparing the raw result id
692+
// made every rewritten pair lose its result to orphan text and gain a synthetic
693+
// missing-result block. lookup() never mints an id: a result with no call stays orphan.
694+
const wireResultId = callIds.lookup(tr.toolCallId);
695+
if (wireResultId !== undefined && requiredIds.has(wireResultId) && !seen.has(wireResultId)) {
696+
resultBlocks.push(toAnthropicToolResult(tr, wireResultId));
697+
seen.add(wireResultId);
670698
} else {
671699
orphanBlocks.push({ type: "text", text: orphanToolResultText(tr) });
672700
}

‎src/adapters/google.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AdapterFetchContext, AdapterRequest, ProviderAdapter } from "./base";
22
import { debugDroppedFrame } from "../lib/debug";
3-
import { anthropicToolCallId } from "./tool-call-id";
3+
import { createToolCallIdAllocator } from "./tool-call-id";
44
import { createImageBudget, materializeInlineImage, MAX_ENCODED_BYTES_PER_IMAGE, artifactHttpUrl } from "../images/artifacts";
55
import type {
66
AdapterEvent,
@@ -11,6 +11,7 @@ import type {
1111
OcxProviderOpaqueToolCallMetadata,
1212
OcxTextContent,
1313
OcxToolCall,
14+
OcxToolResultMessage,
1415
OcxUsage,
1516
} from "../types";
1617
import { isAllowedToolChoice, namespacedToolName, resolveToolChoiceWireName, toolAllowedByChoice } from "../types";
@@ -95,7 +96,9 @@ function vertexReplaySessionId(parsed: OcxParsedRequest): string {
9596
* the call/response pairing is preserved. Returns `undefined` for an empty id so the caller omits the
9697
* field entirely rather than inventing a non-matching one.
9798
*/
98-
const geminiToolCallId = anthropicToolCallId;
99+
// Aliasing the stateless transform here would reintroduce the collision it cannot prevent:
100+
// a rewritten id can equal a distinct raw id that already conforms. Use a request-scoped
101+
// allocator, exactly as the Anthropic adapter does, so call/response pairing stays injective.
99102

100103
/**
101104
* Inline image parts (Gemini `inline_data`) extracted from tool-result content. Only base64 data URLs
@@ -158,6 +161,16 @@ function messagesToGeminiFormat(
158161

159162
const contents: unknown[] = [];
160163

164+
const callIds = createToolCallIdAllocator();
165+
for (const msg of parsed.context.messages) {
166+
if (msg.role === "assistant") {
167+
for (const part of (msg as OcxAssistantMessage).content) {
168+
if (part.type === "toolCall") callIds.reserve((part as OcxToolCall).id);
169+
}
170+
} else if (msg.role === "toolResult") {
171+
callIds.reserve((msg as OcxToolResultMessage).toolCallId);
172+
}
173+
}
161174
for (const msg of parsed.context.messages) {
162175
switch (msg.role) {
163176
case "user":
@@ -196,7 +209,7 @@ function messagesToGeminiFormat(
196209
// streaming covered by the replay cache. Only forward a REAL upstream signature — the
197210
// Responses parser also stashes synthetic item ids (`fc_...`) on this field, and sending
198211
// those as a thoughtSignature breaks continuity (the replay cache supplies the real one).
199-
const callId = geminiToolCallId(tc.id);
212+
const callId = callIds.allocate(tc.id);
200213
const functionCall: Record<string, unknown> = { name: namespacedToolName(tc.namespace, tc.name), args: tc.arguments };
201214
// Claude-on-Antigravity maps this id to Anthropic `tool_use.id`; without it the upstream
202215
// conversion 400s. Gemini accepts the optional id and pairs call/response by it.
@@ -221,7 +234,8 @@ function messagesToGeminiFormat(
221234
// functionResponse, but it does accept sibling inline_data parts in the same user turn, so
222235
// tool-result screenshots (e.g. Computer Use) ride along as inline_data instead of being
223236
// flattened to a "[image]" marker the model can't actually see.
224-
const responseId = geminiToolCallId(msg.toolCallId);
237+
// lookup(), not allocate(): a response must reuse its call's id and must never mint a new one.
238+
const responseId = callIds.lookup(msg.toolCallId);
225239
const functionResponse: Record<string, unknown> = { name: namespacedToolName(msg.toolNamespace, msg.toolName), response: { result: geminiToolResultText(msg.content) } };
226240
// Mirror the matching functionCall id so Claude-on-Antigravity can pair this result with its
227241
// `tool_use` block (-> Anthropic `tool_result.tool_use_id`).

‎src/adapters/tool-call-id.ts‎

Lines changed: 109 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,119 @@
11
import { createHash } from "node:crypto";
22

33
/**
4-
* Normalize a tool call id into the character set Anthropic accepts.
4+
* Anthropic rejects a `tool_use.id` longer than this. Collision disambiguation has to fit
5+
* inside it too, which is why candidates are assembled from parts instead of sliced at the end.
6+
*/
7+
export const MAX_TOOL_CALL_ID_LENGTH = 64;
8+
9+
/** Hex characters of the deterministic tail. Fixed width: it is a discriminator, not a payload. */
10+
const TOOL_CALL_ID_HASH_WIDTH = 8;
11+
12+
const CONFORMING_TOOL_CALL_ID = /^[a-zA-Z0-9_-]+$/;
13+
14+
/** An id Anthropic accepts as-is: right character set AND within the length bound. */
15+
export function isConformingToolCallId(rawId: string): boolean {
16+
return rawId.length > 0
17+
&& rawId.length <= MAX_TOOL_CALL_ID_LENGTH
18+
&& CONFORMING_TOOL_CALL_ID.test(rawId);
19+
}
20+
21+
/**
22+
* The parts a rewritten id is built from: the sanitized prefix and a deterministic tail
23+
* derived from the raw id. Kept separate so collision handling can truncate the prefix
24+
* without destroying the discriminator.
25+
*/
26+
function toolCallIdComponents(rawId: string | undefined): { cleaned: string; hash: string } | undefined {
27+
const raw = rawId ?? "";
28+
if (raw.length === 0) return undefined;
29+
const cleaned = raw.replace(/[^a-zA-Z0-9_-]/g, "_");
30+
const hash = createHash("sha256").update(raw).digest("hex").slice(0, TOOL_CALL_ID_HASH_WIDTH);
31+
return { cleaned, hash };
32+
}
33+
34+
/** Assemble `prefix_hash`, truncating only the prefix, leaving `reserve` characters spare. */
35+
function fitToolCallId(cleaned: string, hash: string, reserve = 0): string {
36+
const tail = `_${hash}`;
37+
const room = MAX_TOOL_CALL_ID_LENGTH - reserve - tail.length;
38+
return cleaned.slice(0, Math.max(1, room)) + tail;
39+
}
40+
41+
/**
42+
* Normalize a tool call id into the character set and length Anthropic accepts.
543
*
6-
* Anthropic validates `tool_use.id` against `[a-zA-Z0-9_-]`, so non-conforming characters are mapped
7-
* to `_`. To keep the mapping injective (so two distinct raw ids like `call:a` and `call/a` cannot
8-
* collide into one `tool_use.id` within a request), a short hash of the original raw id is appended
9-
* whenever any character had to be rewritten. The transform is deterministic, so a call id and its
10-
* matching result id — equal at the source, since the client pairs them — still normalize identically
11-
* and the call/result pairing is preserved. Returns `undefined` for an empty id so the caller can omit
12-
* the field entirely rather than inventing a non-matching one.
44+
* This is the stateless view, kept for callers that only need the shape of one id. It is NOT
45+
* injective on its own: `anthropicToolCallId("call:a")` returns something like `call_a_1f2e3d4c`,
46+
* and a raw id that already equals that value is returned unchanged — two distinct sources, one
47+
* wire id. Anything building a whole request must use {@link createToolCallIdAllocator}, which
48+
* reserves the conforming ids first and resolves collisions.
1349
*
14-
* Ids that already conform are returned unchanged, which is the common case: ids minted by Anthropic
15-
* itself round-trip untouched.
50+
* Returns `undefined` for an empty id. Callers must handle that rather than falling back to the
51+
* raw value: restoring `""` puts an id on the wire that Anthropic rejects (#1767).
1652
*/
1753
export function anthropicToolCallId(rawId: string | undefined): string | undefined {
1854
const raw = rawId ?? "";
1955
if (raw.length === 0) return undefined;
20-
const cleaned = raw.replace(/[^a-zA-Z0-9_-]/g, "_");
21-
if (cleaned === raw) return cleaned;
22-
// Lossy rewrite happened: disambiguate with a deterministic suffix derived from the raw id.
23-
const suffix = createHash("sha256").update(raw).digest("hex").slice(0, 8);
24-
return `${cleaned}_${suffix}`;
56+
if (isConformingToolCallId(raw)) return raw;
57+
const parts = toolCallIdComponents(raw);
58+
if (!parts) return undefined;
59+
return fitToolCallId(parts.cleaned, parts.hash);
60+
}
61+
62+
export type ToolCallIdAllocator = {
63+
/** Claim an already-conforming source id so no rewrite can be handed the same value. */
64+
reserve(rawId: string | undefined): void;
65+
/** Wire id for a raw id, stable within the request. `undefined` means "not representable". */
66+
allocate(rawId: string | undefined): string | undefined;
67+
/** Wire id previously allocated for this raw id, without creating one. */
68+
lookup(rawId: string | undefined): string | undefined;
69+
};
70+
71+
/**
72+
* Request-scoped raw-id to wire-id mapping.
73+
*
74+
* Two properties the stateless transform cannot provide:
75+
*
76+
* - **Injective.** Reserve every already-conforming id first, then allocate rewrites around them,
77+
* appending a numeric suffix when a candidate is taken. Two distinct raw ids never share a wire id,
78+
* including the case where one raw id already looks like another's normalized form, and including
79+
* an ordinary 32-bit hash collision.
80+
* - **Stable.** A tool result asks for the same raw id its call used and gets the same wire id, so
81+
* call/result pairing survives normalization.
82+
*/
83+
export function createToolCallIdAllocator(): ToolCallIdAllocator {
84+
const rawToWire = new Map<string, string>();
85+
const occupied = new Set<string>();
86+
87+
return {
88+
reserve(rawId) {
89+
if (!rawId || rawToWire.has(rawId)) return;
90+
if (!isConformingToolCallId(rawId)) return;
91+
rawToWire.set(rawId, rawId);
92+
occupied.add(rawId);
93+
},
94+
allocate(rawId) {
95+
if (!rawId) return undefined;
96+
const existing = rawToWire.get(rawId);
97+
if (existing) return existing;
98+
if (isConformingToolCallId(rawId) && !occupied.has(rawId)) {
99+
rawToWire.set(rawId, rawId);
100+
occupied.add(rawId);
101+
return rawId;
102+
}
103+
const parts = toolCallIdComponents(rawId);
104+
if (!parts) return undefined;
105+
let candidate = fitToolCallId(parts.cleaned, parts.hash);
106+
for (let n = 2; occupied.has(candidate); n++) {
107+
const suffix = `_${n}`;
108+
candidate = fitToolCallId(parts.cleaned, parts.hash, suffix.length) + suffix;
109+
}
110+
rawToWire.set(rawId, candidate);
111+
occupied.add(candidate);
112+
return candidate;
113+
},
114+
lookup(rawId) {
115+
if (!rawId) return undefined;
116+
return rawToWire.get(rawId);
117+
},
118+
};
25119
}

0 commit comments

Comments
 (0)