Skip to content

Give Stage-1 routing a bounded schema/entity-type signal so it stops treating known types as ambiguous #1980

Closed

Description

@malibio

Overview

Stage-1 routing (ADR-038) decides whether a user's message is clear enough to search the skill registry (route_query), too ambiguous to describe at all (route_clarify), or contains multiple distinct intents (route_multi) — entirely before any information about NodeSpace's actual data model reaches the model. This causes it to over-clarify on words that are, in fact, unambiguous built-in or user-defined types.

Problem Statement

Captured directly from a live session (via the NODESPACE_PROMPT_DUMP mechanism added in #1956): the message "Could you tell me how many tasks we have here?" produced a route_clarify decision, asking the user to disambiguate between "count existing tasks" and "explain how task management works in general."

Tracing the exact Stage-1 prompt sent to the model (STAGE1_SYSTEM_PROMPT + the three route_* tool declarations in packages/agent/src/local_agent/routing.rs) shows it contains no information about NodeSpace's domain at all — no built-in type names (task, text, date), no user-defined entity types, no schema descriptions. The model has to guess, from English alone, whether "tasks" refers to something concrete in this system.

By contrast, Stage 2's system prompt (built in packages/agent/src/local_agent/agent_loop.rs, ~1180) already includes a RELEVANT ENTITY TYPES block populated by context_ops.rs's semantic schema retrieval (emb.semantic_search_nodes_of_type(query, "schema", MAX_SEMANTIC_SCHEMAS, SCHEMA_SIMILARITY_THRESHOLD)), which Stage 1 never sees because route() (line ~1096) runs before workspace_context is assembled (line ~1180) in the current turn sequence.

The result: a request Stage 2 could resolve trivially, because it already knows task is a real built-in type, gets an unnecessary clarification round-trip at Stage 1 because Stage 1 doesn't have that context yet.

Proposed Solution

Give Stage 1 a bounded signal about what NodeSpace actually knows about, without reintroducing the unbounded-registry-enumeration problem ADR-038 was built to avoid. Two complementary, low-risk additions:

  1. Built-in type names (task, text, date, and any other hardcoded core types) — a small, fixed list, cheap to include directly in STAGE1_SYSTEM_PROMPT or as adjacent context. This never grows unboundedly regardless of how many custom schemas a user defines.
  2. Semantically-retrieved candidate schemas — reuse the existing semantic_search_nodes_of_type(query, "schema", MAX_SEMANTIC_SCHEMAS, SCHEMA_SIMILARITY_THRESHOLD) call (already used for Stage 2's RELEVANT ENTITY TYPES block) with the raw user message as the query, run before or alongside Stage 1 instead of only afterward for Stage 2. Bounded by the same MAX_SEMANTIC_SCHEMAS/SCHEMA_SIMILARITY_THRESHOLD constants already governing Stage 2's retrieval, so this does not reopen the "enumerate the whole registry" problem — it is the same bounded, score-gated retrieval Stage 2 already performs, just also made available to Stage 1's decision.

This does not change ADR-038's core architecture (routing stays retrieval-based, not enumeration-based; the model still judges ambiguity itself rather than a rule-based gate) — it extends what Stage 1 is allowed to see before making that judgment, using retrieval machinery that already exists.

Architecture Impact

This extends, rather than reverses, ADR-038 (skill-mediated tool routing). ADR-038's Stage 1 is deliberately minimal today — no domain context, only the raw message and the three routing tool declarations — which was correct for avoiding unbounded registry enumeration, but as this issue's captured example shows, it also means Stage 1 cannot distinguish "genuinely ambiguous English" from "unambiguous NodeSpace vocabulary the model just hasn't been told about." The proposed fix reuses ADR-038's own retrieval-not-enumeration principle (bounded, score-gated semantic search) rather than abandoning it, so no companion ADR change is required unless the resulting design meaningfully changes Stage 1's contract (e.g. adds a new turn/latency cost worth documenting the way ADR-038 documented its own +12% wall-clock measurement).

(Note: ../decisions/018-two-stage-intent-classification.md also nominally covers "two-stage" routing but describes an entirely different, apparently-abandoned architecture — DistilBERT gatekeeper, Gemma 3, Turso/Text-to-SQL — that does not match the current Gemma-4/tool-call-based implementation this issue is about. It is not marked Superseded despite clearly not describing current behavior; flagging this as a documentation gap, but resolving it is out of scope for this issue.)

Acceptance Criteria

  • Stage 1's prompt includes built-in type names (task, text, date, etc.) so it can recognize them as known vocabulary rather than ambiguous English
  • Stage 1's prompt includes (or has access to) semantically-retrieved candidate schema names/descriptions for the user's message, reusing the existing semantic_search_nodes_of_type retrieval bound by MAX_SEMANTIC_SCHEMAS/SCHEMA_SIMILARITY_THRESHOLD
  • The "Could you tell me how many tasks we have here?" case (or an equivalent captured via NODESPACE_PROMPT_DUMP/the agent-matrix eval) resolves via route_query without an unnecessary route_clarify round-trip
  • Latency/turn-count cost of the added retrieval is measured and documented (per ADR-038's own precedent of measuring its extra-turn cost), since this issue proposes moving or duplicating a retrieval call earlier in the turn sequence
  • route_clarify still fires correctly for requests that are genuinely ambiguous even with schema context (verify this isn't over-corrected into never clarifying)
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • packages/agent/src/local_agent/routing.rs — stage1_tool_definitions(), the three route_* tool declarations Stage 1 is shown
  • packages/agent/src/local_agent/agent_loop.rs — STAGE1_SYSTEM_PROMPT (~line 58); route() (~line 2188, called at ~line 1096); workspace_context assembly (~line 1180) that currently runs after route() and is Stage-2-only
  • packages/core/src/ops/context_ops.rs — existing bounded semantic schema retrieval (~line 312-341): emb.semantic_search_nodes_of_type(query, "schema", MAX_SEMANTIC_SCHEMAS, SCHEMA_SIMILARITY_THRESHOLD), parse_and_filter_non_core_schemas
  • packages/nlp-engine/src/chat/prompt_dump.rs — the dump mechanism (added in Expose Gemma 4 12B as a selectable native model (once verified on adequate hardware) #1956) used to capture the exact Stage-1 prompt/response evidence cited above; set NODESPACE_PROMPT_DUMP=<path> to reproduce

Example Captured Evidence

Stage-1 raw response for "Could you tell me how many tasks we have here?" (captured via the prompt dump):

<|tool_call>call:route_clarify{options:[<|"|>Count the number of tasks in a specific list or project.<|"|>,<|"|>Provide information about task management capabilities (e.g., how to create/delete tasks).<|"|>],question:<|"|>Are you asking me to count existing tasks within this application, or are you looking for general information on how tasks work?<|"|>}<tool_call|>

Related Issues

Activity

  1. added
    enhancement
    on Aug 5, 2026
  2. mstomar125 commented on Sep 12, 2026

    @mstomar125
    Contributor

    Status-confirmation triage (2026-09-12): the technical claim in this issue is still accurate against current main (0435c4d).

    • STAGE1_SYSTEM_PROMPT (packages/agent/src/local_agent/agent_loop.rs:59-67) is unchanged — still no built-in type names or domain vocabulary, just the routing instructions.
    • route() (agent_loop.rs:2928) builds its Stage-1 chat message from stage1_query() (agent_loop.rs:782), which blends only prior-turn text + the current message via build_retrieval_query — no schema/entity signal enters it.
    • session.dynamic_context (the schema/collection/playbook block Stage 2 uses) is populated once via set_session_context() (agent_loop.rs:3521) after session creation, and is read at agent_loop.rs:1655 — after route() is called at agent_loop.rs:1528. Stage 1 still runs with zero visibility into it.
    • packages/core/src/ops/context_ops.rs's bounded semantic schema retrieval (semantic_search_nodes_of_type, MAX_SEMANTIC_SCHEMAS=5, SCHEMA_SIMILARITY_THRESHOLD=0.2) is unchanged and still only feeds Stage 2's candidate block, never Stage 1.

    No PR has touched this since filing (only related work found: #1981, unrelated model-tier changes). Line numbers have drifted slightly from the issue's own citations due to intervening commits, but the architecture described is unchanged. This remains a real, well-scoped enhancement — leaving open in Backlog, not implementing (routing-latency-sensitive change, needs its own measurement per the issue's own acceptance criteria).

  3. malibio commented on Oct 2, 2026

    @malibio
    CollaboratorAuthor

    Baseline — Frontend: 309 test files passed, 5606 tests passed

  4. self-assigned this
    on Oct 2, 2026
  5. added 3 commits that reference this issue on Oct 2, 2026
    7582fa5
    d09572d
    b95a682
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

backendenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions