Skip to content

fix(ai): normalize invalid tool names in history - #59915

Open
nateEc wants to merge 3 commits into
anomalyco:devfrom
nateEc:replay-tool-names
Open

nateEc wants to merge 3 commits into
anomalyco:devfrom
nateEc:replay-tool-names

Conversation

@nateEc

@nateEc nateEc commented Oct 28, 2026 •

Copy link
Copy Markdown

Issue for this PR

Fixes #50237

Type of change

  • Bug fix

What does this PR do?

A model-emitted browser.tabs.open call is rejected by the tool registry, but replaying that name can make later OpenAI-compatible requests fail. Normalize unsupported characters in unadvertised names only when serializing history for the Chat and Responses protocols. The original call, its failure, and call IDs stay intact; advertised names are preserved.

How did you verify your code works?

  • Regression tests for both compatible protocols and freeform tool replay; 1,100 tests passed across 10 repeated runs.
  • bun test --only-failures in packages/ai: 1,972 passed, 28 skipped.
  • bun typecheck in packages/ai and root bun run check passed with Bun 1.4.4.
  • Tests compile requests locally; a live Console endpoint was not exercised.

Visual

<svg xmlns="http://www.w3.org/2000/svg" width="240" height="72" viewBox="0 0 240 72" fill="none">
  <rect x="8" y="15" width="224" height="42" rx="8" stroke="currentColor" opacity=".4"/>
  <path d="M24 36h30m-6-6 6 6-6 6" stroke="currentColor" stroke-width="2" opacity=".7"/>
  <text x="66" y="33" fill="currentColor" font-size="10">browser.tabs.open</text>
  <text x="66" y="47" fill="currentColor" font-size="10" opacity=".7">browser_tabs_open</text>
  <circle cx="215" cy="36" r="3" fill="currentColor"><animate attributeName="opacity" values="1;.3;1" dur="2s" repeatCount="indefinite"/></circle>
</svg>

Screenshots / recordings

No UI layout change.

Checklist

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

@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.

This fixes the reported case. I ran the #50237 reproduction (simulated model, OpenAI-compatible Chat) on the base and on this branch. The base's follow-up request still sent browser.tabs.open. With this change it sends browser_tabs_open, and no name has more than one dot. The OpenAI Chat/Responses provider tests in packages/ai pass, and tsc --noEmit passes for the package.

The public API is unchanged: openAIReplayToolName is an internal helper of the protocol layer.

Two things are worth addressing (details inline):

  • The rule only rewrites names with two or more dots and only replaces .. That is tuned to one gateway's error message, not to the provider name rules.
  • The dispatch test can be dropped.

}

/** Keep unadvertised multi-segment names from wedging strict OpenAI-compatible history. */
export const openAIReplayToolName = (name: string, tools: ReadonlySet<string>) =>

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.

This only rewrites names with two or more dots, and only replaces .. That matches the one gateway error in #50237, but OpenAI-style endpoints generally limit tool names to [a-zA-Z0-9_-]. So a replayed unknown name like other.lookup, or one with spaces or /, would still be sent as-is and could wedge the session the same way. The Bedrock fix for the same problem (fb3c10ca6) replaces every character outside that set. Since the name is only rewritten when it isn't advertised, cleaning every invalid character looks just as safe and covers the whole class of bug. The "preserves names with one dot" tests would then need to change.

}),
)

it.effect("does not dispatch an unknown dotted name as its replay spelling", () =>

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.

This test only checks the existing ToolRuntime.dispatch unknown-tool behaviour. The PR doesn't change that, and the rewrite only happens when history is serialized. It can be dropped so the change stays focused on the replay tests.

@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.

Both earlier points are addressed. Tool names that aren't in the request's tool list now have every character outside [a-zA-Z0-9_-] replaced with _, matching the Bedrock fix. The tests cover the one-dot and space/slash cases, and the dispatch test is gone. I re-ran the #50237 reproduction on this head: the replayed call is sent as browser_tabs_open, and no name in the follow-up request has dots. The OpenAI Chat/Responses provider tests and tsc --noEmit in packages/ai pass. The public API is unchanged. No new concerns.

@nateEc nateEc changed the title fix(ai): normalize invalid multi-dot tool names in history fix(ai): normalize invalid tool names in history Oct 28, 2026
在 OpenAI 兼容请求中规范化未声明的多段历史工具名,保留已声明名称和调用配对。

覆盖工具分发与两种协议的重放,并运行 AI 包测试。
按审查建议规范化未注册工具名称中的点号、空格和斜杠,同时保留已注册的名称。

覆盖 Chat 与 Responses 协议的回归场景,完整 AI 测试套件通过。
验证未注册的自由格式工具回放为规范化函数调用,并避免复用不匹配的项目标识。

重复运行相关协议测试并通过完整 AI 测试套件。
@nateEc
nateEc force-pushed the replay-tool-names branch from 7ec12f8 to 8e7b0b7 Compare October 29, 2026 02:12

@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 new commits rebase the fix onto the freeform-tools changes on dev and add one test. Both earlier points (cleaning every invalid character, dropping the dispatch test) remain addressed. Only plain function calls are renamed. A custom_tool_call is used only for an advertised custom tool, whose name is already valid. The new freeform test covers an unknown custom call replayed as a plain function call on both Responses and Chat. I re-ran the #50237 reproduction on this head: the call is replayed as browser_tabs_open, with no dotted names. The provider tests and tsc --noEmit in packages/ai pass. The public API is unchanged. No new concerns.

four1er added a commit to four1er/opencode that referenced this pull request Nov 24, 2026
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.

provider: openai-compatible endpoint rejects multi-dot tool names

1 participant