-
Notifications
You must be signed in to change notification settings - Fork 782
fix(anthropic): normalize tool call ids so cross-provider history replays #1780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lidge-jun
merged 2 commits into
lidge-jun:dev
from
ntdatt812:fix/anthropic-tool-call-id-sanitize
Aug 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { createHash } from "node:crypto"; | ||
|
|
||
| /** | ||
| * Anthropic rejects a `tool_use.id` longer than this. Collision disambiguation has to fit | ||
| * inside it too, which is why candidates are assembled from parts instead of sliced at the end. | ||
| */ | ||
| export const MAX_TOOL_CALL_ID_LENGTH = 64; | ||
|
|
||
| /** Hex characters of the deterministic tail. Fixed width: it is a discriminator, not a payload. */ | ||
| const TOOL_CALL_ID_HASH_WIDTH = 8; | ||
|
|
||
| const CONFORMING_TOOL_CALL_ID = /^[a-zA-Z0-9_-]+$/; | ||
|
|
||
| /** An id Anthropic accepts as-is: right character set AND within the length bound. */ | ||
| export function isConformingToolCallId(rawId: string): boolean { | ||
| return rawId.length > 0 | ||
| && rawId.length <= MAX_TOOL_CALL_ID_LENGTH | ||
| && CONFORMING_TOOL_CALL_ID.test(rawId); | ||
| } | ||
|
|
||
| /** | ||
| * The parts a rewritten id is built from: the sanitized prefix and a deterministic tail | ||
| * derived from the raw id. Kept separate so collision handling can truncate the prefix | ||
| * without destroying the discriminator. | ||
| */ | ||
| function toolCallIdComponents(rawId: string | undefined): { cleaned: string; hash: string } | undefined { | ||
| const raw = rawId ?? ""; | ||
| if (raw.length === 0) return undefined; | ||
| const cleaned = raw.replace(/[^a-zA-Z0-9_-]/g, "_"); | ||
| const hash = createHash("sha256").update(raw).digest("hex").slice(0, TOOL_CALL_ID_HASH_WIDTH); | ||
| return { cleaned, hash }; | ||
| } | ||
|
|
||
| /** Assemble `prefix_hash`, truncating only the prefix, leaving `reserve` characters spare. */ | ||
| function fitToolCallId(cleaned: string, hash: string, reserve = 0): string { | ||
| const tail = `_${hash}`; | ||
| const room = MAX_TOOL_CALL_ID_LENGTH - reserve - tail.length; | ||
| return cleaned.slice(0, Math.max(1, room)) + tail; | ||
| } | ||
|
|
||
| /** | ||
| * Normalize a tool call id into the character set and length Anthropic accepts. | ||
| * | ||
| * This is the stateless view, kept for callers that only need the shape of one id. It is NOT | ||
| * injective on its own: `anthropicToolCallId("call:a")` returns something like `call_a_1f2e3d4c`, | ||
| * and a raw id that already equals that value is returned unchanged — two distinct sources, one | ||
| * wire id. Anything building a whole request must use {@link createToolCallIdAllocator}, which | ||
| * reserves the conforming ids first and resolves collisions. | ||
| * | ||
| * Returns `undefined` for an empty id. Callers must handle that rather than falling back to the | ||
| * raw value: restoring `""` puts an id on the wire that Anthropic rejects (#1767). | ||
| */ | ||
| export function anthropicToolCallId(rawId: string | undefined): string | undefined { | ||
| const raw = rawId ?? ""; | ||
| if (raw.length === 0) return undefined; | ||
| if (isConformingToolCallId(raw)) return raw; | ||
| const parts = toolCallIdComponents(raw); | ||
| if (!parts) return undefined; | ||
| return fitToolCallId(parts.cleaned, parts.hash); | ||
| } | ||
|
|
||
| export type ToolCallIdAllocator = { | ||
| /** Claim an already-conforming source id so no rewrite can be handed the same value. */ | ||
| reserve(rawId: string | undefined): void; | ||
| /** Wire id for a raw id, stable within the request. `undefined` means "not representable". */ | ||
| allocate(rawId: string | undefined): string | undefined; | ||
| /** Wire id previously allocated for this raw id, without creating one. */ | ||
| lookup(rawId: string | undefined): string | undefined; | ||
| }; | ||
|
|
||
| /** | ||
| * Request-scoped raw-id to wire-id mapping. | ||
| * | ||
| * Two properties the stateless transform cannot provide: | ||
| * | ||
| * - **Injective.** Reserve every already-conforming id first, then allocate rewrites around them, | ||
| * appending a numeric suffix when a candidate is taken. Two distinct raw ids never share a wire id, | ||
| * including the case where one raw id already looks like another's normalized form, and including | ||
| * an ordinary 32-bit hash collision. | ||
| * - **Stable.** A tool result asks for the same raw id its call used and gets the same wire id, so | ||
| * call/result pairing survives normalization. | ||
| */ | ||
| export function createToolCallIdAllocator(): ToolCallIdAllocator { | ||
| const rawToWire = new Map<string, string>(); | ||
| const occupied = new Set<string>(); | ||
|
|
||
| return { | ||
| reserve(rawId) { | ||
| if (!rawId || rawToWire.has(rawId)) return; | ||
| if (!isConformingToolCallId(rawId)) return; | ||
| rawToWire.set(rawId, rawId); | ||
| occupied.add(rawId); | ||
| }, | ||
| allocate(rawId) { | ||
| if (!rawId) return undefined; | ||
| const existing = rawToWire.get(rawId); | ||
| if (existing) return existing; | ||
| if (isConformingToolCallId(rawId) && !occupied.has(rawId)) { | ||
| rawToWire.set(rawId, rawId); | ||
| occupied.add(rawId); | ||
| return rawId; | ||
| } | ||
| const parts = toolCallIdComponents(rawId); | ||
| if (!parts) return undefined; | ||
| let candidate = fitToolCallId(parts.cleaned, parts.hash); | ||
| for (let n = 2; occupied.has(candidate); n++) { | ||
| const suffix = `_${n}`; | ||
| candidate = fitToolCallId(parts.cleaned, parts.hash, suffix.length) + suffix; | ||
| } | ||
| rawToWire.set(rawId, candidate); | ||
| occupied.add(candidate); | ||
| return candidate; | ||
| }, | ||
| lookup(rawId) { | ||
| if (!rawId) return undefined; | ||
| return rawToWire.get(rawId); | ||
| }, | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.