Skip to content

Replace pre-LLM skill routing with LLM-orchestrated skill search #1130

Closed

Description

@malibio

Overview

Today the agent uses a pre-LLM router: the skill pipeline runs semantic search before any model call, picks a single skill, and the LLM is invoked with only that skill's scoped tools/prompt. The model never sees the menu of skills — it's handed one. When no skill scores above the confidence threshold, the agent falls through to the full 13-tool path (or, after #1090, returns a hardcoded clarifying question based on a word-count heuristic).

This proposal flips the control: expose a search_skills(query) tool to the LLM and let the model decide when to search, judge the results, and either invoke a skill or ask for clarification using real context about what it searched and found.

Problem Statement

The pre-LLM router has fundamental limitations that heuristics can only paper over:

  1. User-corpus-agnostic routing is impossible. A query like "what's the capital of France?" might score 0.9 against one user's history-of-countries notes and 0.05 against another user's task list. The same query is in-scope or out-of-scope depending on what's stored. Hardcoded confidence floors can't capture this.

  2. Ambiguity heuristics are brittle. Ask for clarification when skill confidence is below threshold instead of falling through to full tool set #1090 added an is_ambiguous function (word count + proper-noun / URL / digit / code-token detection) to decide when to short-circuit. This is a patch on a fundamentally limited design — the router has no judgment, so we keep bolting on rules to approximate one.

  3. Multi-skill turns are impossible. Pre-routing picks one skill per turn. A request like "find my Q2 budget notes and create a task to review them" requires two skills and can't be expressed.

  4. Hidden routing produces opaque failures. When the router picks the wrong skill or finds nothing, the user gets either a wrong answer or a generic clarification with no context about what was searched. The model could ask a much better question if it knew what it didn't find.

  5. The schema list is decorative. The LLM already receives the schema list as context but doesn't use it for routing decisions — that work happens before the model is invoked.

Proposed Solution

Expose the existing skill pipeline as a tool the LLM calls itself.

New flow

  1. LLM receives the system prompt + schema list + a search_skills(query) tool
  2. User sends a message
  3. LLM decides whether a skill is needed
  4. If yes, LLM calls search_skills with its own query (which may differ from the raw user message)
  5. Pipeline returns top matches with scores and metadata
  6. LLM judges the results and either:
    • Invokes the matched skill with appropriate arguments
    • Asks the user for clarification, citing what it searched and didn't find
    • Responds directly (for conversational/social turns)
  7. For multi-skill turns, the model can call search_skills repeatedly and chain skill invocations

What gets deleted

  • is_ambiguous heuristic and its tests in packages/agent/src/local_agent/agent_loop.rs
  • Pre-LLM dispatch path in agent_loop.rs (run_turn short-circuit + pipeline-scoped prompt branch)
  • The CLARIFYING_QUESTION constant and the hardcoded fallback paths that reference it (model will produce its own clarifications)
  • Test infrastructure that mocked the skill router separately from the LLM

What gets kept

  • SkillPipeline itself (semantic search + scoring) — becomes the implementation of search_skills
  • SkillMatch struct and scoring logic
  • Skill definitions, schemas, and registration

Acceptance Criteria

  • search_skills(query: String) tool registered and exposed to the LLM in every turn
  • Tool returns a structured response (top-N matches with skill name, confidence, description, and any metadata the model needs to decide)
  • LLM can invoke a matched skill via normal tool calling after a search_skills result
  • LLM can ask for clarification with context when search returns nothing useful (no hardcoded CLARIFYING_QUESTION string)
  • Multi-skill turns work end-to-end (model calls search_skills more than once in a turn, chains skill invocations)
  • Pre-LLM routing path removed from run_turn (no more is_ambiguous, no skill-scoped prompt branch)
  • Test suite covers: confident match → skill invoked; no relevant skill → model asks for clarification; off-corpus query → model judges and responds appropriately; multi-skill turn → both skills invoked
  • Latency measured on a local 7B-class model and documented in PR (rough floor: how many extra ms per turn vs the current router)
  • Code passes bun run quality:fix
  • Tests pass (bun run test:all)

Technical Specifications

Reference Files

  • Agent loop: packages/agent/src/local_agent/agent_loop.rs — run_turn is where the pre-LLM dispatch lives today; the search_skills tool gets registered here and the short-circuit / scoped-prompt branches get removed
  • Skill pipeline: packages/agent/src/skill_pipeline.rs — SkillPipeline::search (or equivalent) becomes the implementation backing the new tool; SkillMatch is the natural return shape
  • Tool registration: wherever the 13 core tools are assembled for the LLM (in agent_loop.rs or an adjacent module) — search_skills is added to that set

Implementation Details

Tool signature (proposed):

// Tool exposed to the LLM
fn search_skills(query: String, limit: Option<usize>) -> SearchSkillsResponse;

struct SearchSkillsResponse {
    matches: Vec<SkillMatchSummary>,  // top-N, sorted by confidence
}

struct SkillMatchSummary {
    name: String,
    confidence: f32,
    description: String,
    // any other fields the model needs to judge relevance
}

The tool should always return — even an empty matches array is a useful signal. Let the model judge "low confidence" instead of filtering server-side.

Removing the pre-LLM path:

The current run_turn resolves Option<(&Arc<SkillPipeline>, SkillMatch)> before inference and branches on it. Replace with a single inference path that always exposes the full tool set including search_skills. The skill-scoped prompt variant goes away — there's only one prompt path now.

Conversation flow for clarification:

When the model decides nothing relevant exists, it produces a natural-language response that references what it searched. No CLARIFYING_QUESTION constant, no fallback string. If the model returns empty text, that's an inference bug, not a routing decision — handle it as an error, not a UX surface.

Open Questions

These should be resolved in implementation, documented in the PR:

  • Latency cost on local models. Every turn now pays one inference to decide whether to search. On a local 7B-class model this is probably 1–3 seconds added per interaction. Worth measuring before committing; if it's prohibitive, consider a fast-path for messages that obviously don't need a skill (greetings, etc.) — but only if measurement justifies it.
  • search_skills result shape. How much metadata does the model need per match? Description, schema hints, example invocations? Start minimal and expand based on actual model behavior.
  • Confidence in the response. Today's threshold is 0.8. The new design shows confidence to the model; should the model see raw scores, qualitative buckets (high/medium/low), or both? Start with raw scores and observe.
  • Skill argument inference. Today the router hands a matched skill to the model with a pre-built prompt. In the new design the model invokes the skill itself — does it need argument schemas surfaced via the schema list, or via the search_skills response?

Non-Goals

  • Replacing the skill pipeline's semantic search implementation (only its position in the control flow changes)
  • Changing skill definitions or schemas
  • Adding multi-turn clarification state tracking
  • Tuning model prompts beyond what's needed to make search_skills discoverable

Related Issues

Activity

  1. self-assigned this
    on May 18, 2026
  2. malibio commented on May 18, 2026

    @malibio
    CollaboratorAuthor

    Baseline: 3918 tests passed (128 files). Working in worktree on branch worktree-issue-1130-llm-skill-search.

  3. added a commit that references this issue on May 18, 2026
    5f6383a
  4. added a commit that references this issue on May 18, 2026
    ab4dc4f
  5. added a commit that references this issue on Jun 22, 2026
    33946e8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions