fix(search): escape FTS5 metacharacters in hybrid query path (#366)#376
Merged
Conversation
…#366) In --mode hybrid the engine bound the raw FreeText straight into the fused CTE's `messages_fts MATCH`, so FTS5 metacharacters in a natural- language query — e.g. the "?" and "," in `what's the budget, roughly?` — reached the FTS5 parser unescaped and raised "fts5: syntax error near ...". --mode fts and --mode vector were unaffected (the former sanitizes via Store.SearchMessages, the latter has no BM25 branch). FusedRequest.FTSQuery is documented as a pre-tokenized FTS5 MATCH expression. When the caller supplies no explicit override, derive one from FreeText the same way the --mode fts path does: split on whitespace, drop terms the FTS5 tokenizer would discard (punctuation-only), and quote-escape + prefix-match each via the existing query.SQLiteQueryDialect.BuildFTSTerm. An all-tokenless query yields "", which the fused query already treats as "skip BM25" (vector-only) rather than dispatching a malformed MATCH. An explicit FTSQuery override still passes through verbatim. Tests: TestBuildFTSMatch covers the sanitizer directly; TestEngine_Hybrid_PunctuationQuery reproduces kenn-io#366 end-to-end against the real sqlitevec backend and asserts the metacharacter query both succeeds and still matches on its real terms. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
roborev: Combined Review (
|
Member
|
looks good, thanks! |
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.
Fixes #366.
A hybrid search fails with
fts5: syntax error near "?"when the query has FTS5 metacharacters in it, like the?and,inwhat's the budget, roughly?. The hybrid path passed the raw query straight into the BM25MATCH. Theftsandvectormodes don't, so only hybrid hit this.Hybrid now runs free text through the same sanitizing the
ftspath uses: quote each term, append a*prefix, and drop punctuation-only terms. An explicit pre-tokenizedFTSQuerystill passes through untouched. Natural-language questions, which is what most MCP/LLM clients send, are the main thing this unblocks.