Skip to content

Improve tool search ranking and result format #2893

Open

Description

@sentry-junior

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 separate searchMcpTools scorer.
  • 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 full input_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 example github_createIssue) without saying they are reached through executeTool.
  • 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
  1. 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.
  2. Name matches saturate. With IDF near 0, the 8× name weight cannot lift getIssue above PR tools whose descriptions repeat "comment".
  3. Schema JSON is indexed as prose. Property descriptions leak words like "status" and "comments" into unrelated tools.
  4. No stemming: comments ≠ comment, and "view" or "close" match nothing.
  5. 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.

  1. 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.
  2. Leave out tools the model can already call. When a direct tool matches, return one line ("github_commentOnPullRequest is already loaded"), not its schema (Codex).
  3. 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).
  4. Make results light. Return tool_name, a one-line description, and a short schema summary (required params and types, via summarizeInputSchema). Drop exposure and annotations. Return the full schema when executeTool validation fails, or for the top result only. This cuts ~5.7k chars to an estimated ~1k for 5 results.
  5. 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.
  6. 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 executeTool later calls.
  7. Consider loading found tools into the native tool list. Anthropic, OpenAI, Codex and bigtool all do this, which removes the executeTool hop. It conflicts with the per-Conversation tool list and the deferred-tool-discovery non-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.

Related: #2455, #2040, #2891.

via @dcramer.

--

View Session [Sentry]

Activity

  1. sentry-junior commented on Nov 7, 2026

    @sentry-junior
    ContributorAuthor

    Future improvement: embeddings cache for tool search

    Store tool embeddings in the database. Add them when Junior finds new tools.

    • Key: a hash of the tool name, description, and input schema. A tool that changes gets a new entry.
    • Fill: lazily. When a search finds a catalog tool with no entry, compute the embedding and store it. Later searches reuse it. This needs no boot step, and it covers MCP tools, because Junior sees their tool list when the server connects.
    • Search: embed the query, then merge the vector ranking with the BM25 ranking (for example, reciprocal rank fusion). BM25 stays the fallback when an embedding is not ready yet.
    • Cost: one embedding call for each tool version and one for each query. The catalog will grow with more plugins, and the cost grows with distinct tools, not with searches.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions