Skip to content

perf(core): stop runaway vector widening and cap note text in search reads - #1750

Merged
phernandez merged 2 commits into
mainfrom
perf/1746-search-stopgap
Oct 11, 2026
Merged

phernandez merged 2 commits into
mainfrom
perf/1746-search-stopgap

Conversation

@phernandez

@phernandez phernandez commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

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_window widened a filtered vector 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 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_notes defaults entity_types=["entity"]), which is why 55% of production searches ran two or more rounds.

  • The first read still takes the full chunk budget, the same window an unfiltered query reads, so a filtered ranking is the unfiltered ranking restricted to the filter.
  • Widening continues only until the window admits the rows the request needs: offset + limit, or the whole rerank prefix.
  • A search.vector_window Logfire event records rounds, chunks scanned, candidate limit, row target and rows admitted.

2. Cap note text in search reads; keys-only filter pass

content_snippet stores 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.

  • 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 (4000), the small-note passage (2000), and the reranker's fallback document (reranker_max_document_chars, 2000 by default).
  • content_length, content_truncated and the small-note rule read the stored length, so every public search field is unchanged.
  • New FtsBackend.admitted_keys answers 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_chars is set above 4000 or to 0 (unbounded). The #1234 sweep measured identical quality for every cap of 2000 or more.

Measured against main on a real project

A 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_more and every text field.

  • 137 of 138 rankings are identical, with identical scores. The exception is a category-filtered hybrid search, where positions 8 to 10 changed: main's extra widening rounds had fed more vector rows into fusion. Filtered searches now fuse the same window an unfiltered search reads.
  • matched_chunk excerpts 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

  • New test_vector_filter_window cases 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.
  • The existing preview-bound test (a 135 KB lexical-only note) now also asserts that the row carried only the capped text and the stored length. Public content, content_length, content_truncated and matched_chunk are unchanged.
  • SearchIndexRow test for a capped read reporting the stored length.
  • just fast-check
  • just test-unit-sqlite: 8229 passed
  • just test-int-sqlite: 701 passed
  • Postgres (testcontainers): tests/repository, search routers, search service: 1301 passed
  • Full Postgres suite (CI)

Follow-ups: complete fix stacked on this PR; fusion benchmarking in #1748.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EQPZkbnuxxtTEJoNJCkt45

phernandez and others added 2 commits October 11, 2026 13:50
…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>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T19:31:50.038407Z abbc6d3 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@phernandez
phernandez added this pull request to stack #1756 October 11, 2026 22:03
@phernandez
phernandez merged commit 70edf58 into main Oct 11, 2026
33 checks passed
@phernandez
phernandez deleted the perf/1746-search-stopgap branch October 11, 2026 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant