perf(tools): add exact-match fast path to _select_tool#6321
perf(tools): add exact-match fast path to _select_tool#6321HumphreySun98 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Summary: This PR optimizes internal tool selection by adding an exact sanitized-name match before the existing fuzzy fallback, without introducing new external inputs, authentication changes, or data access paths.
Risk: Low risk. No exploitable security vulnerabilities were identified because the change preserves the prior tool-selection behavior and does not modify authorization boundaries, file/network access, or command execution logic.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesTool selection exact-match fast path
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
_select_tool sorted ALL tools by fuzzy SequenceMatcher ratio (an O(L^2) string diff) on every tool-call iteration, even when the LLM emitted a valid, exactly-matching tool name — the overwhelmingly common case. An exact match has ratio 1.0 and would always win that sort anyway. Resolve exact (sanitized) name matches with a single O(n) scan before falling back to the fuzzy sort, which is retained unchanged for typo tolerance. Scanning self.tools in order preserves the prior stable tie-break. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
564c6bc to
a56bf0c
Compare
Summary
ToolUsage._select_toolruns on every text-based (ReAct) tool-call iteration. It sorted all tools by fuzzydifflib.SequenceMatcherratio (an O(L²) string diff per comparison, each also re-sanitizing the tool name) — even when the LLM emitted a valid, exactly-matching tool name, which is the overwhelmingly common case. An exact match has ratio1.0and would always win that sort anyway.Change
Resolve an exact (sanitized) name match in a single O(n) scan before falling back to the fuzzy sort. The fuzzy fallback (and its
> 0.85threshold) is retained unchanged for typo tolerance, and scanningself.toolsin order preserves the prior stable tie-break.Behavior
Identical results — proven by
test_select_tool_matches_legacy_behavior, which asserts the new selection equals a reference implementation of the old fuzzy-sort logic across exact names, sanitized names, and typos. The existing error-path test (test_tool_selection_error_event_direct) is unaffected.Benchmark
8 typical tools, exact-match target (local,
timeit):~5× on the common path here; larger as the tool count grows, and it compounds with tool-name sanitization caching.
Tests
ruff+mypyclean; added_select_toolexact/fuzzy/equivalence tests; surroundingtest_tool_usage.pygreen.This PR was authored with Claude Code. Per
CONTRIBUTING.md, AI-generated contributions require thellm-generatedlabel — I don't have triage permission to set it, so could a maintainer please add it? 🤖 Generated with Claude CodeSummary by CodeRabbit
Bug Fixes
Tests