fix(docs-fts): sanitize natural-language queries into safe FTS5 expressions#127
Merged
Merged
Conversation
…ssions
FtsSearch bound the raw query string straight into `docs_fts MATCH :q`. FTS5
treats that operand as a query expression, with two failure modes:
1. Punctuation is parsed as syntax — an apostrophe ("Marko's") or stray quote
raised "fts5: syntax error".
2. Multiple bare tokens are implicitly AND-ed, so a full question ("how do
observers react to events") required every word in one document and returned
zero results.
New FtsQueryBuilder tokenizes to alphanumeric words, drops stop words / FTS5
boolean keywords, quotes each remaining term, and OR-joins them so BM25 ranks by
term overlap. Empty input short-circuits to no results.
Smoke-tested against the real docs-markdown corpus: queries that previously
returned zero or the wrong docs (events, preferences, routing) now surface the
correct concept doc in the top 3.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Jun 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
search_docs(the MCP docs tool, backed bydocs-fts) returned syntax errors or zero results for ordinary natural-language questions.FtsSearch::search()bound the raw query intodocs_fts MATCH :q, and FTS5 interprets that operand as a query expression:Marko's) or stray quote raisedfts5: syntax error.how do observers react to events) required every token in one document → zero results.Fix
New
FtsQueryBuilder::toMatchExpression()converts NL input into a safe expression: tokenize to alphanumeric words (dropping punctuation), remove stop words + FTS5 boolean keywords, quote each term, OR-join them so BM25 ranks by overlap. Empty input short-circuits to no results.Evidence (real
docs-markdowncorpus)Queries that previously failed now surface the right doc in top-3:
concepts/eventsconcepts/preferences(rank 1)guides/routing(rank 1)This recovers ~3 of the 5 misses from an in-project audit (effective ~6/8). The two remaining (modularity ranking,
configvsconfigurationporter stemming) are genuine content gaps, tracked separately.Testing
packages/docs-ftsgreen: 36 passed. NewFtsQueryBuilderTest(7 cases) + updatedFtsSearchTest(malformed input is now sanitized, not thrown; NL + apostrophe queries return results).🤖 Generated with Claude Code