Skip to content

fix: prevent invalid tool from leaking into model error messages#23264

Open
nitishagar wants to merge 1 commit intoanomalyco:devfrom
nitishagar:contrib/issue-21900
Open

fix: prevent invalid tool from leaking into model error messages#23264
nitishagar wants to merge 1 commit intoanomalyco:devfrom
nitishagar:contrib/issue-21900

Conversation

@nitishagar
Copy link
Copy Markdown

Issue for this PR

Closes #21900

Type of change

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

What does this PR do?

When a model emits an invalid tool call, the AI SDK's NoSuchToolError includes the raw "Available tools: invalid, bash, read, ..." list. This gets passed through experimental_repairToolCall → the invalid tool → back into the model's context. The model sees "invalid" listed as a tool and may try calling it, entering a loop that the doom loop detector can't catch because each call has different input.

Three fixes:

  1. llm.ts: Extract repairToolCall and build a clean error message that never leaks internal tool names or the "Available tools" list. Uses duck-typing on the error object to distinguish "tool not found" from "bad arguments."

  2. processor.ts: Extend doom loop detection to circuit-break consecutive invalid tool calls regardless of input variation. The existing detector only catches identical calls (same name + same input).

  3. invalid.ts: Strip any residual "Available tools: ..." text from the output as defense-in-depth.

How did you verify your code works?

  • bun run typecheck passes
  • All 30 existing + new tests pass (bun test test/session/llm.test.ts test/session/processor-effect.test.ts)
  • 4 new unit tests for repairToolCall covering: case-insensitive repair, unknown tool clean error, invalid arguments error, exact match fallback

Screenshots / recordings

N/A - no UI changes

Checklist

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

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Related PRs Found:

  1. PR fix(opencode): repair malformed JSON in tool call arguments #23067 - fix(opencode): repair malformed JSON in tool call arguments

  2. PR feat: configurable tool alias map for repairing miscalled tools #14085 - feat: configurable tool alias map for repairing miscalled tools

  3. PR fix(opencode): retry transient provider failures and tool-call diff/input errors #15850 - fix(opencode): retry transient provider failures and tool-call diff/input errors

These PRs all touch on tool call repair and error handling, though they may address different aspects. PR #23067 seems most directly related as it also deals with tool call argument repair. You may want to verify if these are solving overlapping issues or if they're complementary fixes.

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.

Bug Report: invalid internal tool is exposed and can spam repeated calls

1 participant