Repository navigation
fix(ai): demote unavailable Anthropic tool references in request payloads - #437
Merged
Merged
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Resuming a session whose history contains calls to tools that no longer exist in the request — e.g. an MCP server absent after
senpi --session <id>resume, a deactivated extension tool, or anonPayloadhook stripping a definition — fails the entire turn:Anthropic validates that every tool referenced by the message history is available in the same request: defined in
tools, or discovered through atool_referenceblock. The codebase already repaired pairing (orphantool_results, dangling calls) but never referential integrity between history and the tool definitions, so one missing MCP server bricked the whole resumed session.Fix
demoteUnavailableToolReferences()inpackages/ai/src/api/anthropic-messages.ts— a final payload pass applied aftersanitizeUnsupportedNativeTools()on every request, right before send:params.tools) and discovered names (any nestedtool_reference, including replayed server-side tool-search results).tool_useblocks referencing unavailable tools to plain text (name + input preserved).tool_results in lockstep (original result text preserved) so no orphan-pairing 400 replaces the original one.tool_referenceentries whose definition vanished (e.g. removed by a payload hook), never leaving emptytool_resultcontent.Deferred-tool discovery (
splitDeferredTools+tool_reference) is untouched and covered by a guard test.Evidence
packages/ai/test/anthropic-tool-reference-integrity.test.ts: 5 tests driving the full request path offline through a fake Anthropic client — single/mixed-turn demotion, still-available tools intact, deferredtool_referencediscovery intact, dangling-reference stripping. Red before, green after.packages/aifull suite: 1458 passed (the one initial suite failure was an unbuiltpackages/tui/distin the fresh worktree; green afternpm run buildthere — unrelated to this change).npm run check: exit 0.local-ignore/qa-evidence/20260728-anthropic-tool-ref-integrity/):mock-loop.mjs --self-test --api anthropic-messages: 20/20 passed, zero real provider calls, real auth unchanged.cli-smoke.mjs --self-test: 8/8 passed.Summary by cubic
Prevents Anthropic 400s when history references tools that aren’t in the current request by demoting those tool calls/results to text and removing dangling
tool_referenceentries so the turn can continue.demoteUnavailableToolReferences()inpackages/ai/src/api/anthropic-messages.ts, run aftersanitizeUnsupportedNativeTools()on every request.toolsand nestedtool_referenceblocks; converts missingtool_useto text (keeps name + input) and demotes pairedtool_result(keeps result text) to avoid orphan errors.tool_referenceitems whose definitions were removed (e.g., by anonPayloadhook) without leaving emptytool_resultcontent; deferred discovery remains intact.packages/ai/test/anthropic-tool-reference-integrity.test.tsto cover mixed turns, still-available tools, deferredtool_referenceflows, and dangling-reference stripping.Written for commit 5ecb304. Summary will update on new commits.