Skip to content

fix(core): fill a filtered vector window instead of stopping at rejected neighbours - #1570

Closed
phernandez wants to merge 2 commits into
feat/1558-scoped-search-4a-ann-prefilterfrom
feat/1558-scoped-search-4b-filtered-window
Closed

phernandez wants to merge 2 commits into
feat/1558-scoped-search-4a-ann-prefilterfrom
feat/1558-scoped-search-4b-filtered-window

Conversation

@phernandez

Copy link
Copy Markdown
Member

Summary

Seventh PR in the #1558 stack, on top of #1569. Second half of the "vector order" item: a vector or hybrid search that carries structured filters now fills its candidate window.

What was wrong. The vector index ranks by similarity alone and cannot evaluate note types, dates, categories, metadata, path prefixes, or valid time. Retrieval took one window of candidate_limit chunks from that ranking and asked the full-text pass which of them the filters admitted. When the nearest chunks belonged to rows the filter rejects, the window held few admitted rows while more sat just past it, and the page came back short although matches existed. Twelve archive notes nearer the query than three matching notes were enough to turn a file_path_prefix="notes" search into an empty answer.

What changes. vector_only resolves its window through a small loop: read the window, resolve chunks to rows (keys, threshold, row fetch, filter), and if the query has filters and the window is not full, read it again twice as wide. The loop stops when the window holds candidate_limit admitted rows, when the ranking is exhausted (a short read, no growth between reads, or the existing 50k scan cap), or when the tail of the ranking has fallen below the similarity threshold, past which nothing further can qualify. A query without filters reads its window once, exactly as before.

This is the same rule for both routes and every adapter, and it adapts to filter selectivity without estimating it: a broad filter finishes in one read, a narrow one pays a few more reads of the same ranking. It replaces neither the scope pre-filter from #1569 nor the candidate-key restriction from #1431; it sits on top of both.

Shape. The chunk-to-row resolution that used to sit inline in vector_only moves into _resolve_rows, returning a frozen CandidateWindow. _run_vector_query, hydration, and the trace stage are unchanged; each round replaces the previous round's rejection lists, so a trace reads the final window.

Test plan

  • just fast-check
  • New tests/repository/test_vector_filter_window.py: the window widens 20 then 40 and returns the admitted rows; an unfiltered query reads once and never asks the filter; an exhausted ranking returns what it admitted; a ranking that stops growing ends the loop; a sub-threshold tail ends it; the widening is bounded by the scan cap; each round asks the filter only about the candidates it holds. Five of the seven fail on the previous reader.
  • New repository test through the real sqlite-vec adapter and manifest: twelve nearer rejected rows in front of three admitted ones, window of ten, page of one; previously empty.
  • Reader, trace, rerank, pagination, temporal, path-prefix, and candidate-restriction suites (202 passed)
  • tests/repository tests/services tests/api plus the search tool tests on SQLite (2193 passed)
  • Postgres container: repository, candidate restriction, trace, window, reader, vector sync, scoped route, semantic service (151 passed)
  • test-int semantic coverage, search diagnostics, vec0 status (13 passed)

Refs #1558

🤖 Generated with Claude Code

https://claude.ai/code/session_019YW9ysxugGGBCNEGzsxtFV

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@phernandez
phernandez added this pull request to stack #1572 September 15, 2026 23:56
@phernandez
phernandez force-pushed the feat/1558-scoped-search-4b-filtered-window branch from 17587fd to f4604ab Compare September 16, 2026 00:00
phernandez and others added 2 commits September 15, 2026 19:00
Both built-in vector adapters took the k nearest vectors across the whole
database and applied the scope afterwards. In a shared database a small project
could get an under-filled or empty page for a query its own notes answered,
because a larger neighbour's vectors filled the window first. On a 20k-chunk
corpus where one project holds 2% of the vectors, a limit-100 query for that
project returned 1 row from sqlite-vec.

sqlite-vec: `project_id` becomes a vec0 partition key, so the KNN ranks each
partition in scope and the outer ORDER BY merges them (100 of 100 rows, 0.2 ms
against 3.1 ms). Existing local storage is carried into the partitioned table
through a temporary table inside one transaction; vectors and ready manifests
survive, nothing is re-embedded. Dimension and source_hash mismatches still
rebuild as before.

pgvector: the statement's tie-break sort keys had kept the planner off the HNSW
index entirely, so every vector query was an exact scan of the table. The window
is now taken by distance alone inside a materialized CTE and re-sorted with the
tie-breaks outside it. `hnsw.ef_search` is set per transaction to the window the
query must fill (the default 40 capped every larger candidate pool), and
`hnsw.iterative_scan = relaxed_order` lets the scan continue until the scope and
manifest filters have admitted enough rows. That scan needs pgvector 0.8; an
older extension now raises SemanticDependenciesMissingError at first use rather
than returning short windows. Neon tenants run 0.8.0.

Refs #1558

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_019YW9ysxugGGBCNEGzsxtFV
Signed-off-by: phernandez <[email protected]>
…ted neighbours

The vector index ranks by similarity alone and cannot evaluate structured
filters (note types, dates, categories, metadata, path prefixes, valid time).
Vector and hybrid retrieval took one window of `candidate_limit` chunks from
that ranking and asked the full-text pass which of them the filters admitted.
When the nearest chunks belonged to rows the filter rejects, the window held
few admitted rows while more sat just past it, and the page came back short
although matches existed.

`vector_only` now resolves its window through `_candidate_window`, which
re-reads the ranking with a bounded geometric overfetch until the window holds
`candidate_limit` admitted rows, the ranking is exhausted (a short read, no
growth, or the scan cap), or its tail has fallen below the similarity
threshold, past which nothing further can qualify. A query without filters
resolves its window once, as before. The chunk-to-row resolution (keys,
threshold, row fetch, filter) moves into `_resolve_rows`, returning a frozen
`CandidateWindow`; `_run_vector_query` and the trace stage are unchanged, and
each round replaces the previous round's rejection lists so the trace reads
the final window.

Refs #1558

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_019YW9ysxugGGBCNEGzsxtFV
Signed-off-by: phernandez <[email protected]>
@phernandez
phernandez force-pushed the feat/1558-scoped-search-4b-filtered-window branch from f4604ab to 7016dae Compare September 16, 2026 00:00
@phernandez phernandez closed this Sep 16, 2026
@phernandez
phernandez deleted the feat/1558-scoped-search-4b-filtered-window branch September 16, 2026 00:00
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