fix(ai): return tools already offered from lazy discovery instead of "Unknown tool" #2174
main fromrefs/pull/2174/head·merged by tombeckenham View on GitHubWhen a model asks the lazy discovery tool for a tool it can already call, discovery answers Unknown tool. Some models then stop using that tool. This PR makes discovery return the definition of every tool offered to the model in that step, the same way it already does for a lazy tool the model asks for again.
🎯 Changes#
LazyToolManager.getExecutableTools()keeps the tools offered in this step. The engine passes it the set after middleware, so this includes tools that a middleware added and leaves out tools that a middleware removed.- The discovery tool looks up a name in the lazy tools first, then in that offered set. An offered tool comes back in
toolswith its schema. It does not start a tool-list refresh. - Docs: one paragraph in
docs/tools/lazy-tool-discovery.md. - Changeset:
@tanstack/aipatch.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. (Node 24.22.1, no cache: 461 of 464 tasks pass. The 3 failures are macOS-only sandbox tests that this PR does not touch. See Testing.) - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Root cause#
Issue. An app sends eager and lazy tools to chat(), or a middleware adds tools in onConfig. The model calls __lazy__tool__discovery__ with the name of a tool it can already call. Discovery returns { tools: [], errors: ["Unknown tool: '<name>'. ..."] }. The model reads this as "the tool does not exist". We saw Claude Haiku stop a task for this reason.
Cause. The execute function of the discovery tool, in createDiscoveryTool() in lazy-tool-manager.ts, looks only in lazyToolMap. Each name that is not lazy becomes Unknown tool, even when the engine offered that tool to the model in this step.
Fix. getExecutableTools() gets the offered tools from the engine just before it runs the tool calls of each step. It now stores them. Discovery falls back to that set before it reports Unknown tool.
Possible alternatives#
- Look up
eagerToolsinstead of the offered set. This is a smaller change. It misses tools that a middleware adds, and it would announce eager tools that a middleware removed. - Return a new field such as
alreadyAvailableinstead of the schema. This changes the type of the result for no gain. Returning the schema intoolsis what discovery already does for a lazy tool that the model asks for again.
Testing#
Commands run
- Unit repro, written for this PR, in
packages/ai/tests/lazy-tool-manager.test.ts, blockdiscovery of tools that are already offered.- On
main(code unchanged):× returns an eager tool instead of "Unknown tool",× returns a tool that a middleware added to the offered set. Result:2 failed | 1 passed. - With this PR:
3 passed.
- On
- E2E
pnpm --filter @tanstack/ai-e2e test:e2e tests/lazy-tools.spec.ts.- On
main:✘ discovery of an already offered tool returns it, not "Unknown tool", received{"tools":[],"errors":["Unknown tool: 'get_weather'. Available tools: [search_inventory]"]}. - With this PR:
2 passed.
- On
nx run @tanstack/ai:test:lib,test:types,test:oxlint: pass (2713 tests).pnpm test:docs: pass.pnpm test:pron Node 24.22.1 (from.nvmrc), macOS, with--skip-nx-cache: 461 of 464 tasks pass. The 3 failures are in sandbox packages that this PR does not change:@tanstack/ai-sandbox-local-process: passes when run alone. It fails only under the load of the full parallel run.@tanstack/ai-sandbox,tool-bridge.test.ts"rejects when the listener cannot start": times out after 5 s. It also fails on cleanmainon this machine.@tanstack/ai-sandbox-cloudflare,container-checkpoint.test.ts:chmod: --: No such file or directory. The macOSchmoddoes not accept--. It also fails on cleanmainon this machine.
Manual test
- On
main, runpnpm --filter @tanstack/ai-e2e test:e2e tests/lazy-tools.spec.ts. The new test fails withUnknown tool: 'get_weather'. - On this branch, run the same command. Both tests pass.
How this PR makes testing easy
- 3 unit tests: an eager tool, a tool added by a middleware, and an eager tool removed by a middleware (still
Unknown tool). - A new E2E scenario,
lazy-tool-discovery-offered, in the tools-test page.
Risk / rollback#
Low. Only the result of a discovery call for a name that is not lazy changes. Revert the PR to undo.
Known limit, not changed here: a lazy tool that a middleware removes from the offered set is still returned by discovery, because discovery looks in lazyToolMap first. That was already the behavior before this PR.
🤖 Generated with Claude Code
Summary by CodeRabbit#
- New Features
- Tool discovery now returns definitions for tools already offered in the current step, including eager, previously discovered, and middleware-added tools.
- Lazy tool definitions remain available through discovery even if middleware removes the tool.
- Requests for eager tools removed by middleware return an “Unknown tool” error.
- Documentation
- Updated the lazy tool discovery guide to describe these behaviors.
- 1
- 2
Pushed04235bb
tombeckenham approved these changes
Thanks for this fix. I reproduced the bug on main (discovery returned "Unknown tool" for an eager tool) and confirmed the PR resolves it; the lookup uses the post-middleware offered set and typos still error. I pushed one small commit that corrects a docs sentence and drops an e2e assertion that also passed on main.
tombeckenham merged thise5bb4d5
tombeckenham closed this
Sign in to join this conversation.
coderabbitai[bot](GitHub profile, opens in a new tab) reviewed
85d09ef5-153e-4430-a34a-3155353ddddaReviewing files that changed from the base of the PR and between 7f38b550341986daa3cb3bcf5d7b3f3a9bb0a0c7 and 04235bbac43c9e2e4f5a0c03946af81f887218c4.
.changeset/lazy-discovery-offered-tools.mddocs/config.jsondocs/tools/lazy-tool-discovery.mdtesting/e2e/src/lib/tools-test-tools.tstesting/e2e/tests/lazy-tools.spec.tsIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.