Repository navigation
feat: add query lexical/semantic classifier (opt-in, unused) - #159
Conversation
Ships QueryRouter with regex-based classification of user queries into
LEXICAL (identifiers, CVEs, hashes) vs SEMANTIC (natural language)
categories per ADR-002. Two-class output aligned with the FTS5 dispatch
path — low-hit fallback (min_hits) handles the ambiguous middle case.
Default patterns cover ADR-002 heuristics:
- [A-Z]{2,}-\d+ (identifiers like MDR-*, CWE-*)
- CVE-\d{4}-\d+
- ^[a-f0-9]{32,64}$ (hashes)
Configurable via search.lexical_fast_path.patterns YAML block.
First-match-wins ordering (OQ-2). Empty query is safe-default semantic.
Zero wiring in this PR - Task 03 dispatches routing inside
KnowledgeOrchestrator.query().
- New module mcp_server/query_router.py (37 lines)
- classify(query) -> QueryClass Literal[lexical, semantic]
- Patterns pre-compiled eagerly at __init__ (fail-fast on bad regex)
- 13 tests including Hypothesis property test (19 with parametrize)
- API surface baseline snapshot updated (no breaking changes)
- Test count baseline 288 -> 296 (+8)
Fase 2 of FTS5 Lexical Fast-Path plan.
Zero runtime behavior change.
📝 WalkthroughWalkthroughAdded an isolated ChangesQueryRouter
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/test_query_router.py`:
- Around line 68-71: The test_default_patterns_classify_lexical test uses
ROUTER_TEST_PATTERNS despite its name, so it does not validate DEFAULT_PATTERNS.
Either rename the test to reflect ROUTER_TEST_PATTERNS or construct QueryRouter
with DEFAULT_PATTERNS and retain the intended lexical classification assertion.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: de34dc39-e574-46cd-9fae-b56a1ab81f9b
📒 Files selected for processing (5)
.github/api-surface-baseline.json.github/test-count-baseline.txtREADME.mdmcp_server/query_router.pytests/test_query_router.py
| def test_default_patterns_classify_lexical(self, query: str) -> None: | ||
| """UT-002 — canonical lexical corpus classifies as lexical.""" | ||
| router = QueryRouter(ROUTER_TEST_PATTERNS) | ||
| assert router.classify(query) == "lexical" |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Test the pattern set named by the test.
Line 70 constructs QueryRouter with ROUTER_TEST_PATTERNS, not DEFAULT_PATTERNS. This test cannot detect classification regressions in DEFAULT_PATTERNS. Rename the test to describe the strict test patterns, or instantiate DEFAULT_PATTERNS and assert their intended behavior.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@tests/test_query_router.py` around lines 68 - 71, The
test_default_patterns_classify_lexical test uses ROUTER_TEST_PATTERNS despite
its name, so it does not validate DEFAULT_PATTERNS. Either rename the test to
reflect ROUTER_TEST_PATTERNS or construct QueryRouter with DEFAULT_PATTERNS and
retain the intended lexical classification assertion.
|
Applied skip-perf-gate: perf regression flagged on test_bench_orchestrator_idle_rss (+11.5% median 0.16->0.18 MB, ~20KB delta). Root cause is measurement noise on RSS at idle — query_router module ships opt-in unused (no wiring into KnowledgeOrchestrator until Task 03), so no code path introduced by this PR is executed during orchestrator idle. Same treatment as PR #158 (Task 01) which also hit this jitter. |
Summary
Ships
QueryRouter- regex-based classifier that categorizes user queries intolexicalorsemanticper ADR-002. Completely unwired in this PR - Task 03 will dispatch routing insideKnowledgeOrchestrator.query().Fase 2 of FTS5 Lexical Fast-Path plan (workflow artifacts local-only, gitignored).
What ships
mcp_server/query_router.py(37 lines) -QueryRouterclass withclassify(query)returningQueryClass = Literal["lexical", "semantic"]__init__(fail-fast on bad regex viare.error)re.Pattern.search(unanchored)semanticsafe-defaultsearch.lexical_fast_path.patterns(from Task 01)Design note - two-class output (no AMBIGUOUS)
The task spec initially considered a three-class
LEXICAL | AMBIGUOUS | SEMANTICoutput. Implementation collapses to two classes because:min_hitsthreshold at dispatch time (Task 03) already handles the ambiguous middle case (lexical query with few hits falls back to hybrid)What does NOT ship
KnowledgeOrchestrator.query()- Task 03Testing
tests/test_query_router.py, all passingUT-013): for any printable ASCII query, output is one of the two literalsmcp_server.query_router, no breaking changes)LEI 1 preserved
Zero change to the 13 frozen MCP tools.
mcp_server/server.pyuntouched.Local validation
pytest tests/test_query_router.py= 19/19 pass in 0.66sruff check+ruff format= cleanpython scripts/check_api_surface.py --check= OKpython scripts/check_test_count.py= blocked locally by pre-existingMCPServerimport failure (unrelated - reproduced on master, task_01 PR#158 had same note). CI env with pinned deps should pass.Rollback
git revert <sha>- module is opt-in unused, revert is safe.Summary by CodeRabbit
New Features
Documentation
Tests
Greptile Summary
The PR adds an opt-in, currently unwired regex classifier that categorizes queries as lexical or semantic.
Confidence Score: 5/5
The PR appears safe to merge.
No blocking failure remains.
Important Files Changed
Reviews (2): Last reviewed commit: "ci: retrigger quality-gate to honor skip..." | Re-trigger Greptile