Skip to content

Tool RAG: lexical projection of the bound tool catalog (off by default) - #204

Merged
BaaRai merged 2 commits into
mainfrom
feat/tool-rag-lexical
Nov 12, 2026
Merged

BaaRai merged 2 commits into
mainfrom
feat/tool-rag-lexical

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

What

When an agent's bound tool list is large (custom tools, host tools), inject only the subset relevant to the current intent/round instead of every schema. This is a projection of the already-bound list: it only removes tools, never adds one, never edits a description or schema, never reads the database or touches the UI. Bindings (the tools table via ToolResolve) stay the source of truth.

Shipped OFF by default. Nothing changes until ARTEX_TOOL_RAG is set. This is a deliberate choice so behaviour does not move before it is measured; the owner decides whether to flip it.

Design

  • Insertion point: agent/assembly.go, AugmentTools, right after ToolResolve (binding + enabled filters) and before findingWorkflowTools, so the finding guidance is computed on the projected list and never mentions a hidden tool.
  • Query: carried on the context (WithToolQuery). Worker: the intent summary (nodeSummary(intent); the worker has no goal in scope). Planner: plannerToolQuery(goal, triggers) = goal + the short texts the round's triggers already carry (no store lookups). Main agent and chat set no query, so they always get the full list.
  • Scoring interface: type ToolScorer func(query string, tools []actool.CoreTool) ([]float64, error). The default is bm25Score, Okapi BM25 (k1=1.2, b=0.75) over the tool name, description and parameter names. Tokens are lowercased and split on non-alphanumerics, and IDF is computed over the candidate set. It is pure: no network, no embeddings, no external calls.
  • Ranking: score descending, tie-break by tool name. The output keeps the input (assembly) order of the kept tools, so it is deterministic.
  • Keep set = core + harness + deferred. These tools are never hidden and do not count against k; each is kept only if it is already bound. The core list from the spec is a minimum, not a full whitelist.
    • Core: record_fact, insert_assets, report_finding, list_findings, graph_overview, node_detail, add_intent, steer_work, ask_operator, prove_goal, goal_met, list_assets.
    • Harness: the SDK tools (Read/Write/Edit/MultiEdit/LS/Glob/Grep/Bash/Sleep), the interactive shell_* tools and the Skill meta-tool. Removing them would break the worker (Bash, Read, Write and Skill are how it does its work).
    • Deferred: MCP tools and deferred custom tools. Removing them would desync the deferred-tools block and the skill unlock gate.
  • Known gap: MCP tools are not projected. Their schemas are already withheld from the prompt (only names appear in the deferred block). Projecting their names would desync that block and the skill unlock gate, so they always pass through.
  • Fallback to the full list: empty query, a list under 20 tools, or a scorer error (logged, never fatal; a scorer returning the wrong number of scores also counts as an error).
  • Config: ARTEX_TOOL_RAG (1/true/on/yes enables; default off), ARTEX_TOOL_RAG_TOPK (non-core tools kept; default 12).
  • Logging: when enabled, one [tool-rag] line per assembly, with either the selected tool names or the fallback reason.

Tests (agent/toolrag_test.go, pure: no database, no network)

  • A SQL-injection query ranks the injection and web fixture tools above a port-scan fixture.
  • Core tools are present when bound, an unbound core tool is never added, and the non-core count is at most k.
  • A tool dropped by ToolResolve (unbound or disabled) is never selected, even when it is the best lexical match. This one runs through AugmentTools with a stub resolver.
  • Fallback to the full list on an empty query, a catalog under 20, and a failing scorer.
  • Determinism: repeated runs give the same ordered output, and score ties are ordered by name.
  • Keep set pinned: a catalog of more than 20 tools containing actool.DefaultTools(), Skill and one deferred name, with a query that matches something else. Bash, the other SDK tools, Skill and the deferred tool all stay; only the top-1 candidate is added.

Expected effect today: none

With today's built-in tool sets, the projection does nothing. The worker has 17 tools, under the threshold of 20. The planner's projectable tools fit within k. The projection only takes effect when custom or host tools are bound on top of the built-ins.

Not done

  • No dynamic tool creation, no MCP discovery, no permission changes, no embeddings service.
  • No end-to-end run with a real LLM.
  • No MCP projection (see the known gap above).
  • Offline fixture measurement only. With 30 synthetic custom tools added, it cut the tool text (name + description + schema JSON) from 26.6 KB to 21.4 KB for the worker and from 31.4 KB to 25.2 KB for the planner. This was not measured on a live agent.

When ARTEX_TOOL_RAG is enabled, AugmentTools keeps the core tools plus the
top-k (ARTEX_TOOL_RAG_TOPK, default 12) BM25-ranked bound tools for the
worker intent / planner goal. Runs after ToolResolve, so it only removes;
falls back to the full list on empty query, catalog under 20, or scorer error.
@BaaRai
BaaRai merged commit b789dd2 into main Nov 12, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant