Repository navigation
Description
searchTools often gets the right tool wrong and pays a lot of tokens to do it. A search of source github for "comment on issue" matched all 15 GitHub tools and ranked github_commentOnPullRequest first, a direct tool the model already had. The result was ~5.7k chars for 5 tools, and the agent then used the pull request tool on an issue. #2891 added commentOnIssue, but the ranking problem remains: commentOnIssue now wins by 0.018 points.
Checked at 675b87379. Paths are under packages/junior/src/chat/ unless noted.
How search works today
- Catalog: direct and deferred tools both go into the catalog (
tool-exposure.ts:28-35), so search returns tools that the model can already call. Plugin tools default to deferred (plugins/agent-hooks.ts:905). MCP tools use a separatesearchMcpToolsscorer. - Ranking (
tools/search-tools.ts:147-302): BM25F over 3 fields weighted[8,4,1]: name; description; source id + source description +JSON.stringify(inputSchema)+ annotations. No stemming, no minimum score beyond> 0, ties broken alphabetically. - Result (
:367-383):tool_name, description cut to 180 chars,exposure, the fullinput_schema,call_notes,annotations. Default 5 results, max 20.summarizeInputSchema(tool-support/schema-summary.ts:20) exists but nothing calls it. - After search: found tools never join the native tool list; every call goes through
executeTool(agent/tools.ts:669-674). The tool list is fixed per Conversation for prompt caching (tools/index.ts:100-101). - Steering: the system prompt never mentions
searchTools.<tool-guidance>is built from direct tools only. Skills and egress denials name deferred tools (for examplegithub_createIssue) without saying they are reached throughexecuteTool. - No telemetry on query, source, match count, or the rank of the tool that was then called. No ranking eval; unit tests use synthetic catalogs.
Why the ranking fails
Reproduced with the real scorer over the real createGitHubTools output:
Query (source github) |
Result |
|---|---|
comment on issue |
commentOnIssue 2.900, commentOnPullRequest 2.882, updatePullRequestFeedback 2.772, submitPullRequestReview 2.498, then createIssue/updateIssue/getIssue at 0.064 |
issue view comments |
getIssue 3.92, submitPullRequestReview 2.10 ("comments" appears twice in its schema) |
close issue |
every tool 0.02–0.07; field length decides the order |
deployment status |
getStatus 3.24, updatePullRequestFeedback 2.73 ("status" in its schema), getDeployment 2.61 |
- The source description is in every tool's document. GitHub's says "deployment, issue, pull request, release, and repository workflows", so any of those words matches every tool, and their IDF drops to ~0. Within a source filter this field is pure noise.
- Name matches saturate. With IDF near 0, the 8× name weight cannot lift
getIssueabove PR tools whose descriptions repeat "comment". - Schema JSON is indexed as prose. Property descriptions leak words like "status" and "comments" into unrelated tools.
- No stemming:
comments≠comment, and "view" or "close" match nothing. - No cutoff: results get padded with near-zero filler instead of saying nothing fits.
Always-loaded GitHub PR tools
commentOnPullRequest, createPullRequest, submitPullRequestReview, resolvePullRequestReviewThread and updatePullRequestFeedback set exposure: "direct" (#1776, #1781, #2690). They cost ~5.2k chars (~1.3k tokens) on every Turn of every Conversation, PR-related or not. They also compete in search with their full schemas again. #1781 and #2690 do not say why these tools need direct exposure. Possibly because the egress denials (junior-github/src/egress-policy.ts:369-383) name them as if they could be called directly (inference).
Prior art
| Harness | Search | Searches loaded tools? | Result | Default N | Then |
|---|---|---|---|---|---|
| Anthropic tool search tool | regex or BM25 over name, description, arg names/descriptions | no | tool_reference (name only), expanded by the API |
5 | direct call |
| Claude Code | names + descriptions; only names load upfront | no | top 5, loaded until compaction | 5 | direct call |
OpenAI tool_search |
namespace load (paths), not keywords |
n/a | namespace with schemas, appended at the end to keep the cache | — | direct call |
Codex CLI (tool_search.rs) |
BM25 over name variants, server, description, param keys | no (is_deferred filter) |
specs grouped by namespace | 8 | direct call |
langgraph-bigtool |
embeddings over name: description |
n/a | names only | 2 | bound for next call |
| Goose code mode, MCP client best practices | list → details → execute | — | name + one line; schema on request | — | proxy |
| Cursor | tool descriptions synced to files; agent greps them | — | names in prompt | — | direct call |
| Junior | BM25F incl. source description + schema JSON | yes | full schema + annotations | 5 | executeTool |
Reported numbers (vendor-measured): Anthropic ~85% fewer tokens, MCP-eval accuracy 49%→74% (Opus 4) and 79.5%→88.1% (Opus 4.5), and they note accuracy drops past 30–50 loaded tools. Cursor: −46.9% agent tokens. RAG-MCP: 43% vs 14% selection accuracy. ToolRet: general-purpose retrievers do poorly on tool retrieval. Gemini CLI, opencode and Cline have no native tool search. MCP SEP-1821 (server-side tools/list?query=) was closed without being accepted.
The common pattern: discovery is cheap (names or one-liners, top-k), the schema is paid for once when a tool is chosen, and tools the model already has never come back.
Recommendations
In priority order. The first three are small changes in search-tools.ts.
- Fix the index. Use the source only as a filter, not as text in each tool's document. Index name parts, description, and schema property names, not schema JSON. Add plural/simple stemming. Weight name matches so a name hit beats a description hit.
- Leave out tools the model can already call. When a direct tool matches, return one line ("
github_commentOnPullRequestis already loaded"), not its schema (Codex). - Cut weak matches and say when nothing fits. Drop results below a fraction of the top score. When the best match is weak, return "no strong match in
github" plus the closest names, instead of filling up to 5 (Anthropic returns an empty list). - Make results light. Return
tool_name, a one-line description, and a short schema summary (required params and types, viasummarizeInputSchema). Dropexposureandannotations. Return the full schema whenexecuteToolvalidation fails, or for the top result only. This cuts ~5.7k chars to an estimated ~1k for 5 results. - Defer the 5 GitHub PR tools. If some must stay direct, gate them per Conversation, for example on PR event Conversations, which keeps the cached tool list stable. Make the egress denials and skills say how to reach deferred tools.
- Add a ranking eval and telemetry. Use real plugin catalogs and query → expected tool cases (the queries above are a start), scoring top-1 accuracy and result chars. Record query, source, matched/returned counts, and the rank of the tool that
executeToollater calls. - Consider loading found tools into the native tool list. Anthropic, OpenAI, Codex and bigtool all do this, which removes the
executeToolhop. It conflicts with the per-Conversation tool list and thedeferred-tool-discoverynon-goal. Appending at the end of the list keeps the cached prefix (OpenAI). This needs a separate design; it depends on pi-agent-core allowing the tool set to change between steps.
Smaller items: total_eligible_tools re-runs the whole search with "" (search-tools.ts:457-459); the 180-char description cut conflicts with policies/tool-design.md, which says to put selection guidance in the description.
via @dcramer.
--
Activity
fix(tools): Fold verb endings and soften description length in tool s…
fix(tools): Fold verb endings and soften description length in tool s…
feat(tools): Defer rarely used tools (#2895)
feat(tools): Match synonyms and abbreviations in tool search
fix(tools): Fold verb endings and soften description length in tool s…
feat(tools): Match synonyms and abbreviations in tool search
fix(tools): Apply the synonym weight after BM25 saturation
sentry-junior commentedon Nov 7, 2026
Future improvement: embeddings cache for tool search
Store tool embeddings in the database. Add them when Junior finds new tools.
The same cache can also hold generated keywords for each tool, which feed BM25 directly. Keywords may be the simpler first step. Use the ranking test from #2898 to compare the two approaches.
feat(tools): Improve tool search ranking with stemming, synonyms, and…
feat(tools): Rank tool names by action and object, and say when no to…
feat(tools): Rerank unclear tool search matches with the fast model
feat(tools): Rerank unclear tool search matches with the fast model
feat(tools): Rerank unclear tool search matches with the fast model (#…
feat(tools): Return a params summary instead of full schemas from sea…