Skip to content

fix(ai): truncate replayed names of undeclared tool calls - #65860

Open
four1er wants to merge 1 commit into
anomalyco:devfrom
four1er:unknown-tool-names
Open

four1er wants to merge 1 commit into
anomalyco:devfrom
four1er:unknown-tool-names

Conversation

@four1er

@four1er four1er commented Nov 23, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #65822

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

A model sometimes calls a tool no one declares, under a made-up name. That call is stored and replayed on later requests, and providers cap tool names at 64 characters (OpenAI, Anthropic, Bedrock Converse, Gemini). The replayed name was sanitized but never truncated, so the next request was rejected and the session died.

Changes

  • packages/ai/src/protocols/shared.ts: new replayToolName helper — a declared name is kept as-is, anything else is sanitized to provider-accepted characters and truncated to 64 characters. flattenToolName uses it, so every protocol without native namespaces (OpenAI Chat, Anthropic, Gemini, Mistral, Bedrock Converse, ...) replays unknown calls under a valid name. The Bedrock direct path already sliced calls to 64 in its own lowering; this makes the gateway/OpenAI-compatible path match.
  • packages/ai/src/protocols/open-responses.ts: Responses routes keep native namespaces, so they never see flattenRequest. lowerToolCall applies the same rule (declared tools keep name and namespace, custom tools keep their own name).

Complementary to #59915, which only normalizes the characters; this caps the length. No feature flag: this is a fix, per the repo guide.

How did you verify your code works?

  • New regression test in packages/ai/test/provider/openai-chat.test.ts using the exact made-up name from the issue (70 chars sanitized → replayed at 64), plus a 65-char name and a declared name kept as-is; the recorded history is asserted unmutated.
  • New regression test in packages/ai/test/provider/conversation-lowering.test.ts for the Responses lowering.
  • Both new tests fail on unpatched code and pass with the fix.
  • Full packages/ai suite: 2289 pass, 29 skip, 0 fail. bun run check from the repo root: 36/36 tasks successful.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #54146

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Nov 23, 2026

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks sound and fixes the problem. I ran the #65822 reproduction from source against a mock OpenAI-compatible gateway that rejects replayed tool names over 64 characters. On the base, the unknown-tool call is replayed under a 70-character name and the next request fails with Member must have length less than or equal to 64. With this PR, it is replayed as tools__websearch-demo-agent____websearch-web-search-tool___WebSe (64 characters) and the session goes on. I also checked the Responses lowering on the OpenAI Responses route, which keeps native namespaces: declared namespaced tools keep their name and namespace, including one added and later removed mid-conversation, and custom tools are unchanged. The nearby provider, tool-update and compile tests pass (1564, 0 fail), and tsc --noEmit passes for packages/ai.

The public API is unchanged. replayToolName is a new export of the internal ProviderShared module only.

No blocking concerns. Two notes: cutting names to 64 characters can make two different made-up names identical, which is harmless because results are matched by call ID. And #65739 rewrites this tool-name code, so whichever PR lands second will need to carry the 64-character limit over.

Related: #54146

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Nov 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@four1er

four1er commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks for flagging — I've updated the PR description to follow the PR template (issue link, type of change, description, verification, checklist). Please let me know if anything is still missing.

@opencodejr

opencodejr Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for the update. My review didn't ask for any changes. The fix worked when I ran the #65822 reproduction, the nearby tests pass, and I had no blocking concerns, so nothing is missing from my side. The PR template request seems to come from a separate check (the needs:compliance label), which isn't part of my review.

A model sometimes calls a tool no one declares, under a made-up name.
That call is stored and replayed on later requests, and providers cap
tool names at 64 characters (OpenAI, Anthropic, Bedrock Converse,
Gemini). The replayed name was sanitized but never truncated, so the
next request was rejected and the session died (anomalyco#65822).

Cap undeclared replayed names at 64 characters in the shared replay
helper used by flattenRequest, which covers every protocol without
native namespaces, and apply the same rule in the Responses lowering,
which keeps native namespaces and never sees flattenRequest. Declared
names are kept as-is. This is complementary to anomalyco#59915, which only
normalizes the characters.

Fixes anomalyco#65822

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v2: a call to an unknown tool is replayed under its made-up name, so a provider that rejects the name kills the session (Bedrock 64-char limit)

1 participant