Repository navigation
perf(core): stop runaway vector widening and cap note text in search reads - #1750
Merged
Merged
Conversation
…k budget `_candidate_window` widened a filtered window until `window.admitted >= candidate_limit`. `admitted` counts search rows; `candidate_limit` is a chunk budget, and in hybrid without a reranker it is multiplied by ten again. A note cut into ~52 chunks makes that target unreachable, so a filtered search doubled its window until the threshold, the end of the ranking, or the 50,000-chunk scan limit. Every default MCP search is filtered (entity_types=["entity"]), and in production 55% of searches ran two or more rounds, up to six rounds and 101 s (#1746). The first read still takes the full chunk budget, the same window an unfiltered query reads, so a filtered ranking is the unfiltered one restricted to the filter. Widening now continues only until the window admits the rows the request needs: offset + limit, or the whole rerank prefix. A `search.vector_window` event records rounds, chunks scanned, row target and rows admitted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EQPZkbnuxxtTEJoNJCkt45 Signed-off-by: phernandez <paul@basicmachines.co>
`content_snippet` stores the whole note (41 KB on average in a large production project). Both FTS statements and the vector candidate fetch selected it uncapped for every candidate row, and the vector filter pass ran a full FTS row query just to learn which keys survive. A hybrid search moved megabytes before returning ten results (#1746). - FTS and vector row reads select substr(content_snippet, 1, 4000) and length(content_snippet). 4000 is the most any reader of a search row uses: the displayed `content`, the 2,000-char small-note passage, and the reranker's fallback document. content_length, content_truncated and the small-note rule read the stored length. - FtsBackend.admitted_keys answers the vector filter pass with (type, id) pairs only, on SQLite and Postgres. Public search fields are unchanged; the existing preview-bound test now also asserts the row carried only the capped text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EQPZkbnuxxtTEJoNJCkt45 Signed-off-by: phernandez <paul@basicmachines.co>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This was referenced Oct 11, 2026
3 tasks done
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
Stopgap for #1746, the bottom of a stack. The complete fix (filters pushed into the vector search, rank by id, fetch text for the returned page only) stacks on this PR.
In production, hybrid search moved 40 to 350 MB of database egress per search, and some searches ran six widening rounds totalling 101 s. This PR removes the two causes that need no schema or algorithm change.
1. Stop filtered widening at a row target, not a chunk budget
_candidate_windowwidened a filtered vector window untilwindow.admitted >= candidate_limit.admittedcounts search rows;candidate_limitis a chunk budget, and in hybrid without a reranker it is multiplied by ten again. A note cut into ~52 chunks makes that target unreachable, so the loop doubled until the similarity threshold, the end of the ranking, or the 50,000-chunk scan limit. Every default MCP search is filtered (search_notesdefaultsentity_types=["entity"]), which is why 55% of production searches ran two or more rounds.offset + limit, or the whole rerank prefix.search.vector_windowLogfire event records rounds, chunks scanned, candidate limit, row target and rows admitted.2. Cap note text in search reads; keys-only filter pass
content_snippetstores the whole note (41 KB on average in the large production project). Both FTS statements and the vector candidate fetch selected it uncapped for every candidate row, and the vector filter pass ran a full FTS row query only to learn which keys survive.substr(content_snippet, 1, 4000)andlength(content_snippet). 4000 is the most any reader of a search row uses: the displayedcontent(4000), the small-note passage (2000), and the reranker's fallback document (reranker_max_document_chars, 2000 by default).content_length,content_truncatedand the small-note rule read the stored length, so every public search field is unchanged.FtsBackend.admitted_keysanswers the vector filter pass with(type, id)pairs only, on SQLite and Postgres.One behavior change: a full-text-only row in the rerank prefix has no matched chunk, so its rerank document falls back to note text, now capped at 4000 characters. This only matters if
reranker_max_document_charsis set above 4000 or to 0 (unbounded). The #1234 sweep measured identical quality for every cap of 2000 or more.Measured against
mainon a real projectA parity harness ran 138 searches against a copy of a real local project (485 notes, 5,671 vector chunks, notes up to 191 KB): hybrid, vector and Boolean text, with entity-type, note-type, tag and category filters, pages 1 to 3. It compared ranking, scores,
has_moreand every text field.main's extra widening rounds had fed more vector rows into fusion. Filtered searches now fuse the same window an unfiltered search reads.matched_chunkexcerpts differ on many filtered results. An excerpt joins the best chunks of a note that the window read.main's runaway widening read nearly every chunk of a note; a bounded window reads fewer, as unfiltered searches always have. The stacked follow-up computes each returned note's excerpt from that note's own chunks, so excerpts stop depending on window size.Test plan
test_vector_filter_windowcases with the production shape (52 chunks per note, every row admitted): the window is read once instead of doubling to the scan limit, and widening stops when the page is full.content,content_length,content_truncatedandmatched_chunkare unchanged.SearchIndexRowtest for a capped read reporting the stored length.just fast-checkjust test-unit-sqlite: 8229 passedjust test-int-sqlite: 701 passedtests/repository, search routers, search service: 1301 passedFollow-ups: complete fix stacked on this PR; fusion benchmarking in #1748.
🤖 Generated with Claude Code
https://claude.ai/code/session_01EQPZkbnuxxtTEJoNJCkt45