feat(tool_search): optional embedding reranker for progressive tool disclosure - #44272
Closed
davidgut1982 wants to merge 2 commits into
Closed
feat(tool_search): optional embedding reranker for progressive tool disclosure#44272davidgut1982 wants to merge 2 commits into
davidgut1982 wants to merge 2 commits into
Conversation
…isclosure Adds an optional, opt-in embedding reranker to the tool_search BM25 bridge (PR NousResearch#34493). Default OFF — when disabled the BM25 path is byte-for-byte identical to upstream. urllib-only (no new deps), task-prefixed, md5-cached tool embeddings, full-catalog retrieve, rerank/RRF(k=10) modes, graceful BM25 fallback on any endpoint failure. Backend is any OpenAI-compatible /v1/embeddings endpoint (cloud, local CPU, or GPU). Live-validated (194 tools / 98 labeled queries, nomic-embed-text-v2-moe): overall Recall@5 0.617 -> 0.810, SEMANTIC 0.500 -> 0.849, LEXICAL preserved at 1.000; warm per-query ~146ms, dead-endpoint fallback ~8ms. Fulfills NousResearch#13332.
…ding churn (NousResearch#13332) Replace the single-slot module-level reranker singleton (_reranker / _reranker_catalog_key) with a bounded scope-keyed dict (_reranker_cache, max 8 entries, FIFO eviction). Each distinct toolset-scope (keyed by md5(endpoint + model + tool_names)) now retains its own EmbeddingReranker instance and its own per-tool embedding cache independently of concurrent agents operating on different toolsets. Old behaviour: agent A (toolset X) and agent B (toolset Y) racing through _get_reranker() caused the second call to rebuild the singleton and discard the first agent's cached embeddings, forcing repeated endpoint calls. New behaviour: both scopes coexist in the dict; re-requesting scope A after scope B is created returns the original scope-A instance with its embedding cache intact. Thread-safety is preserved via double-checked locking on the dict + order list, guarded by the existing _GLOBAL_LOCK. New tests (TestEmbedCacheInvalidation): - test_concurrent_scopes_do_not_share_reranker: proves scope B creation does NOT evict scope A's instance or its embedding cache (mocks _embed and asserts zero extra calls on scope-A re-access after scope B is created). - test_reranker_cache_evicts_oldest_scope_when_full: fills cache to _RERANKER_CACHE_MAX_SIZE (8), adds an overflow scope, and asserts FIFO eviction dropped the oldest key from _reranker_cache. Existing tests updated to reset _reranker_cache / _reranker_cache_order instead of the retired _reranker / _reranker_catalog_key globals. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Collaborator
Contributor
Author
|
Closing as a duplicate of #35457 (feat/tool-search-hybrid-rerank), which is the canonical PR for the tool_search embedding reranker. This PR was opened in error during a PR-split cleanup. |
This was referenced Jul 28, 2026
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.
What does this PR do?
Adds an optional, opt-in embedding reranker to the
tool_searchBM25 bridge (built on top of #34493) for progressive tool disclosure. It is default OFF — when disabled the BM25 path is byte-for-byte identical to upstream, so there is zero behavior change unless a user explicitly enables it.When enabled, tool candidates retrieved by BM25 are reranked against any OpenAI-compatible
/v1/embeddingsendpoint (cloud, local CPU, or GPU). This substantially improves semantic recall for tool selection while preserving exact lexical matches.The second commit hardens the reranker's caching: it replaces the single-slot module-level singleton with a bounded, per-scope cache so concurrent agents operating on different toolsets don't evict each other's cached embeddings.
Related Issue
Fulfills #13332.
Fixes #
Type of Change
Changes Made
tools/tool_search.py: optional embedding reranker on the BM25 bridge. urllib-only (no new deps), task-prefixed and md5-cached tool embeddings, full-catalog retrieve,rerank/ RRF(k=10) modes, and graceful BM25 fallback on any endpoint failure. Default OFF.tools/tool_search.py: per-scope reranker cache — replaced the single-slot module-level singleton (_reranker/_reranker_catalog_key) with a bounded scope-keyed dict (_reranker_cache, max 8 entries, FIFO eviction), keyed bymd5(endpoint + model + tool_names). Each toolset-scope keeps its ownEmbeddingRerankerand embedding cache. Thread-safety preserved via double-checked locking guarded by the existing_GLOBAL_LOCK.tests/tools/test_tool_search.py: config parsing, rerank/RRF modes, fallback path, andTestEmbedCacheInvalidation(concurrent scopes do not share/evict; FIFO eviction at max size).website/docs/user-guide/features/tool-search.md: documents the opt-in reranker, config keys, and backend requirements.How to Test
tool_searchoutput is unchanged from upstream BM25./v1/embeddingsendpoint and confirm semantic queries surface the right tools; kill the endpoint and confirm graceful fallback to BM25.pytest tests/tools/test_tool_search.py -q→ 63 passed.Live-validated (194 tools / 98 labeled queries, nomic-embed-text-v2-moe): overall Recall@5 0.617 → 0.810, SEMANTIC 0.500 → 0.849, LEXICAL preserved at 1.000; warm per-query ~146ms, dead-endpoint fallback ~8ms.
Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/tools/test_tool_search.py -qand all tests pass (63 passed)Documentation & Housekeeping
website/docs/user-guide/features/tool-search.mdcovers the opt-in rerankercli-config.yaml.exampleif I added/changed config keys — N/A (documented in the tool-search feature doc; defaults keep it disabled)CONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — N/Atool_searchschema unchanged when disabledScreenshots / Logs