Keep the semantic vector scans on their keyset range when account is set, and drop their per-row copy probes #102

Closed
opened 2026-11-01 11:36:23 +00:00 by svc-agent-writer · 3 comments

Follow-up to #96, split at its architect gate (ruling R3).

Problem

#96 makes keyword and filter searches cheap with the v10 copy_key column. The semantic path keeps its per-row probes:

  • Store.vector_chunks and Store.attachment_vector_chunks take their filters and the row shown from _vector_filters.
  • _vector_filters adds _shown_id and a NOT EXISTS copy probe for every vector-bearing row.
  • Both scans page by key in chunks of 2048 rows. A set-based collapse per chunk was judged no cheaper at the #96 gate.

So semantic mode, hybrid mode and similar_to still run about one probe per vector-bearing row. On a mirror near the brute-force bound of ADR 0005 §8, which is about 200,000 vectors, that is one probe per row per query.

To find out (decide at triage)

  • The cost today: time semantic, hybrid and similar_to on the scripts/bench_search.py store with vectors, before and after #96.
  • Whether copy_key with its index can choose the representative once per query, before the scan, for example as a temporary set of shown ids. The scan would then join to that set instead of probing.
  • The rule from #59 holds, whichever option is chosen: the row shown is chosen among the rows that pass the filters.

Acceptance

  • To be set at triage, against the measured numbers.
  • The output of every semantic mode stays byte-for-byte the same.

Blocked by

Filed under: operator word "File a follow-up (Recommended)" sha256:7e1e5164212a

Follow-up to #96, split at its architect gate (ruling R3). ## Problem #96 makes keyword and filter searches cheap with the v10 `copy_key` column. The semantic path keeps its per-row probes: - `Store.vector_chunks` and `Store.attachment_vector_chunks` take their filters and the row shown from `_vector_filters`. - `_vector_filters` adds `_shown_id` and a `NOT EXISTS` copy probe for every vector-bearing row. - Both scans page by key in chunks of 2048 rows. A set-based collapse per chunk was judged no cheaper at the #96 gate. So semantic mode, hybrid mode and `similar_to` still run about one probe per vector-bearing row. On a mirror near the brute-force bound of ADR 0005 §8, which is about 200,000 vectors, that is one probe per row per query. ## To find out (decide at triage) - The cost today: time semantic, hybrid and `similar_to` on the `scripts/bench_search.py` store with vectors, before and after #96. - Whether `copy_key` with its index can choose the representative once per query, before the scan, for example as a temporary set of shown ids. The scan would then join to that set instead of probing. - The rule from #59 holds, whichever option is chosen: the row shown is chosen among the rows that pass the filters. ## Acceptance - To be set at triage, against the measured numbers. - The output of every semantic mode stays byte-for-byte the same. ## Blocked by - #96 Filed under: operator word "File a follow-up (Recommended)" sha256:7e1e5164212a
Author
Member

This was generated by AI during triage.

Agent Brief

Category: bug
Summary: An account filter makes the semantic vector scans quadratic. Keep each scan on its keyset range, then replace the per-row copy probes with the #96 copy_key column if that measurably helps.

Operator rulings (2026-11-01). Where this brief differs from the issue body, the brief holds.

  1. Part 1 is required. It fixes the index choice.
  2. Part 2 is conditional. It replaces the copy probes, and lands only if it cuts the unfiltered scan by 20% or more with identical output.
  3. Category: bug. The issue was filed as a cost question. Triage measured a bug.

Current behavior (measured 2026-11-01 on main d0ee0f1, medians of 3):

  • The store held 210,000 rows: 200,000 in account a, a copy of every twentieth in account b, and a random 384-dimension vector per row. Scoring used semantic.top_k with k = 25.
Scan ms
plain keyset scan of embeddings plus scoring 169
Store.vector_chunks, no filter 642
Store.vector_chunks, account="a" 16,676 to 17,303
the same, with +m.account = ? in that scan's WHERE 715 (same top results)
Store.vector_chunks, since= 412, unchanged by the +
  • Cause. With account set, SQLite drives the scan from the index messages_msgid (account, message_id) (EXPLAIN QUERY PLAN: SEARCH m USING INDEX messages_msgid (account=?), then USE TEMP B-TREE FOR ORDER BY). It does not use the m.id > ? rowid range. So every 2048-row keyset page re-reads and re-sorts the whole account, about 100 pages for 200,000 rows. The other (account, …) indexes (messages_date, messages_from, messages_thread) can win the same way.
  • Scope. Store.vector_chunks and Store.attachment_vector_chunks both take their WHERE from Store._vector_filters, which uses Store._filters. Semantic mode, hybrid mode and similar_to all go through these scans.
  • Copy probes. _vector_filters adds _shown_id and a NOT EXISTS copy probe per vector-bearing row. That accounts for most of the gap between 169 ms and 642 ms. #96 adds the v10 copy_key column and leaves these probes to this issue (#96 gate ruling R3).

Desired behavior:

  1. Part 1. Each keyset vector scan walks its own keyset order: m.id for vector_chunks, and ae.rowid for attachment_vector_chunks. No filter may switch it to an (account, …) index.
    • One way: a unary + on the filter's account term, only in those scans. Another: a CROSS JOIN order.
    • Store._filters itself keeps its current SQL, because keyword search and filter search want the account index.
  2. Part 2. Choose the row shown, and leave out the copies behind it, through copy_key, not through per-row probes.
    • The #59 rule still holds: the row shown is chosen among the rows that pass the filters.
    • Keep part 2 only if the unfiltered vector_chunks scan drops by 20% or more against part 1 alone, and every semantic output stays the same. Otherwise drop it and record the numbers in the PR.
  3. Bench. scripts/bench_search.py (from #96) gains semantic cases: no filter, account=a and since=. It uses random unit vectors and the plain scan as the floor, and needs no model.

Key interfaces:

  • Store._vector_filters, Store.vector_chunks, Store.attachment_vector_chunks and Store._filters in src/imap_mailmind_mcp/store.py.
  • The v10 copy_key column and the messages_copy_key index from #96.

Acceptance criteria:

  • A test asserts the EXPLAIN QUERY PLAN of both vector scans with account set: the outer table is searched by its keyset range (rowid), and not by an (account, …) index.
  • On the bench store, vector_chunks with account="a" costs at most 1.2 times the unfiltered scan. Put the numbers in the PR.
  • Semantic, hybrid and similar_to results are byte-for-byte the same as on main, with copies, filters and attachment chunks. Add an equivalence test.
  • Part 2, if kept, cuts the unfiltered scan by 20% or more against part 1 alone. Put the numbers for both in the PR.
  • tests/test_source.py passes: there is no mail-server call in server.py.
  • make check passes.

Out of scope:

  • Keyword and filter search, which are #96.
  • A vector index such as sqlite-vec, which ADR 0005 §8 calls a new decision.
  • Any change to the collapse key or to the rule for the row shown.

Blocked by

> *This was generated by AI during triage.* ## Agent Brief **Category:** bug **Summary:** An `account` filter makes the semantic vector scans quadratic. Keep each scan on its keyset range, then replace the per-row copy probes with the #96 `copy_key` column if that measurably helps. **Operator rulings (2026-11-01).** Where this brief differs from the issue body, the brief holds. 1. **Part 1 is required.** It fixes the index choice. 2. **Part 2 is conditional.** It replaces the copy probes, and lands only if it cuts the unfiltered scan by 20% or more with identical output. 3. **Category: bug.** The issue was filed as a cost question. Triage measured a bug. **Current behavior** (measured 2026-11-01 on main `d0ee0f1`, medians of 3): - The store held 210,000 rows: 200,000 in account `a`, a copy of every twentieth in account `b`, and a random 384-dimension vector per row. Scoring used `semantic.top_k` with k = 25. | Scan | ms | |---|---| | plain keyset scan of `embeddings` plus scoring | 169 | | `Store.vector_chunks`, no filter | 642 | | `Store.vector_chunks`, `account="a"` | 16,676 to 17,303 | | the same, with `+m.account = ?` in that scan's WHERE | 715 (same top results) | | `Store.vector_chunks`, `since=` | 412, unchanged by the `+` | - **Cause.** With `account` set, SQLite drives the scan from the index `messages_msgid (account, message_id)` (`EXPLAIN QUERY PLAN`: `SEARCH m USING INDEX messages_msgid (account=?)`, then `USE TEMP B-TREE FOR ORDER BY`). It does not use the `m.id > ?` rowid range. So every 2048-row keyset page re-reads and re-sorts the whole account, about 100 pages for 200,000 rows. The other `(account, …)` indexes (`messages_date`, `messages_from`, `messages_thread`) can win the same way. - **Scope.** `Store.vector_chunks` and `Store.attachment_vector_chunks` both take their WHERE from `Store._vector_filters`, which uses `Store._filters`. Semantic mode, hybrid mode and `similar_to` all go through these scans. - **Copy probes.** `_vector_filters` adds `_shown_id` and a `NOT EXISTS` copy probe per vector-bearing row. That accounts for most of the gap between 169 ms and 642 ms. #96 adds the v10 `copy_key` column and leaves these probes to this issue (#96 gate ruling R3). **Desired behavior:** 1. **Part 1.** Each keyset vector scan walks its own keyset order: `m.id` for `vector_chunks`, and `ae.rowid` for `attachment_vector_chunks`. No filter may switch it to an `(account, …)` index. - One way: a unary `+` on the filter's account term, only in those scans. Another: a `CROSS JOIN` order. - `Store._filters` itself keeps its current SQL, because keyword search and filter search want the account index. 2. **Part 2.** Choose the row shown, and leave out the copies behind it, through `copy_key`, not through per-row probes. - The #59 rule still holds: the row shown is chosen among the rows that pass the filters. - Keep part 2 only if the unfiltered `vector_chunks` scan drops by 20% or more against part 1 alone, and every semantic output stays the same. Otherwise drop it and record the numbers in the PR. 3. **Bench.** `scripts/bench_search.py` (from #96) gains semantic cases: no filter, `account=a` and `since=`. It uses random unit vectors and the plain scan as the floor, and needs no model. **Key interfaces:** - `Store._vector_filters`, `Store.vector_chunks`, `Store.attachment_vector_chunks` and `Store._filters` in `src/imap_mailmind_mcp/store.py`. - The v10 `copy_key` column and the `messages_copy_key` index from #96. **Acceptance criteria:** - [ ] A test asserts the `EXPLAIN QUERY PLAN` of both vector scans with `account` set: the outer table is searched by its keyset range (rowid), and not by an `(account, …)` index. - [ ] On the bench store, `vector_chunks` with `account="a"` costs at most 1.2 times the unfiltered scan. Put the numbers in the PR. - [ ] Semantic, hybrid and `similar_to` results are byte-for-byte the same as on main, with copies, filters and attachment chunks. Add an equivalence test. - [ ] Part 2, if kept, cuts the unfiltered scan by 20% or more against part 1 alone. Put the numbers for both in the PR. - [ ] `tests/test_source.py` passes: there is no mail-server call in `server.py`. - [ ] `make check` passes. **Out of scope:** - Keyword and filter search, which are #96. - A vector index such as `sqlite-vec`, which ADR 0005 §8 calls a new decision. - Any change to the collapse key or to the rule for the row shown. ## Blocked by - #96
svc-agent-writer changed title from Make the semantic scans use the copy_key column instead of per-row copy probes to Keep the semantic vector scans on their keyset range when account is set, and drop their per-row copy probes 2026-11-01 11:41:03 +00:00
Collaborator

⏸ SVD architect gate: dev/imap-mailmind-mcp#102 keep-the-semantic-vector-scans-on-their-keyset-range-when-ac

Status: CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity standard · dispatch effort medium

Run: 20261101T202518Z-5014d0ee

Held because: CLEAN but 3 open risk(s) (2 verification · 1 decision) — HOLD-ALL parks everything. The risks, verbatim:

Open risks:

  1. Part 2 keeps shown_of/hidden maps for one scan. Their size is the filter-passing vector-bearing rows in repeated-key groups: about 10k on the bench store, up to about half the mirror when two accounts fully copy each other. The #96 spec (open risk 3) cited the 'memory does not grow with the mirror' docstring against whole-scan materialization. Question: is memory proportional to duplicated rows acceptable for Part 2, with the docstring amended? Lean: yes, because they are int maps and Part 2 must still clear the 20% gate. If no, drop Part 2: a per-chunk copy_key IN (...) lookup is the variant the #96 gate already judged no cheaper. [decision]
  2. Part 2 reads copy groups once at scan start. Today's probes re-read them per page. The m.id <= high bound keeps rows inserted mid-scan out of the query, but a copy whose from or date is edited mid-scan could be classified from the earlier snapshot for one query. Equivalence is proven on a quiescent store only. Check: the equivalence tests pass at commit B, and review confirms that high and the maps are read under one self._lock acquisition. [verification]
  3. The bench gates are host-only, because the Half-A sandbox could not access .venv to run pytest or the bench. Check: the PR body quotes uv run python scripts/bench_search.py --semantic output showing vector account=a at most 1.2x vector none at commit A. For Part 2 it must show vector none(B) at most 0.8x vector none(A); otherwise commit B is reverted and both numbers are recorded. [verification]

Change: Land two commits. Commit A (Part 1): add a keyword-only keyset=False parameter to Store._filters (store.py:1393-1444). When it is true, the account term becomes +{alias}.account = ?. Only _vector_filters (store.py:1625-1635) passes keyset=True, and only for its outer m terms. Every other caller keeps its SQL byte-for-byte. A probe this session showed + restores SEARCH m USING INTEGER PRIMARY KEY (rowid>?) and SEARCH ae … (rowid>?) for every account filter set. Commit A also gives scripts/bench_search.py a --semantic flag (random unit vectors, a plain embeddings keyset scan as the floor, cases none, account=a and since=) and adds tests/test_vector_scans.py. Commit B (Part 2, conditional): mirror _collapse_ctes' cand/dup/ranked (store.py:1465-1508) in a once-per-scan _vector_copies helper. It returns shown_of (first vector-bearing copy → rn=1 representative), hidden (other vector-bearing copies) and a high id bound. The scans drop _shown_id and the NOT EXISTS probe, filter and label rows in Python, move the cursor on the last raw row, never yield an empty chunk (best_k reshape at semantic.py:38) and bound by m.id <= high. Keep commit B only if unfiltered vector_chunks is at most 0.8× commit A on the bench. Otherwise revert it and record both numbers in the PR.

Files: src/imap_mailmind_mcp/store.py · scripts/bench_search.py · tests/test_vector_scans.py · README.md

Decision: accept the 3 open risk(s) above and dispatch this spec (svd:go), or park (svd:skip) / re-spec (svd:respec)?

Your call (when this issue is held at the gate via svd:hold):

  • add label svd:go — approve; Half-B dispatch accepts exactly this spec (open risks accepted)
  • add label svd:fire beside svd:go — approve AND authorise the Half-B fire from the phone; never on its own
  • add label svd:skip — park it; dispatch refuses it and the issue stays for a human
  • add label svd:respec — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context

Spec: /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-keep-the-semantic-vector-scans-on-their-keyset-range-when-ac.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-keep-the-semantic-vector-scans-on-their-keyset-range-when-ac.md

### ⏸ SVD architect gate: dev/imap-mailmind-mcp#102 keep-the-semantic-vector-scans-on-their-keyset-range-when-ac **Status:** CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity `standard` · dispatch effort `medium` **Run:** `20261101T202518Z-5014d0ee` **Held because:** CLEAN but 3 open risk(s) (2 verification · 1 decision) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. Part 2 keeps shown_of/hidden maps for one scan. Their size is the filter-passing vector-bearing rows in repeated-key groups: about 10k on the bench store, up to about half the mirror when two accounts fully copy each other. The #96 spec (open risk 3) cited the 'memory does not grow with the mirror' docstring against whole-scan materialization. Question: is memory proportional to duplicated rows acceptable for Part 2, with the docstring amended? Lean: yes, because they are int maps and Part 2 must still clear the 20% gate. If no, drop Part 2: a per-chunk copy_key IN (...) lookup is the variant the #96 gate already judged no cheaper. [decision] 2. Part 2 reads copy groups once at scan start. Today's probes re-read them per page. The m.id <= high bound keeps rows inserted mid-scan out of the query, but a copy whose from or date is edited mid-scan could be classified from the earlier snapshot for one query. Equivalence is proven on a quiescent store only. Check: the equivalence tests pass at commit B, and review confirms that high and the maps are read under one self._lock acquisition. [verification] 3. The bench gates are host-only, because the Half-A sandbox could not access .venv to run pytest or the bench. Check: the PR body quotes `uv run python scripts/bench_search.py --semantic` output showing vector account=a at most 1.2x vector none at commit A. For Part 2 it must show vector none(B) at most 0.8x vector none(A); otherwise commit B is reverted and both numbers are recorded. [verification] **Change:** Land two commits. Commit A (Part 1): add a keyword-only `keyset=False` parameter to `Store._filters` (store.py:1393-1444). When it is true, the account term becomes `+{alias}.account = ?`. Only `_vector_filters` (store.py:1625-1635) passes `keyset=True`, and only for its outer `m` terms. Every other caller keeps its SQL byte-for-byte. A probe this session showed `+` restores `SEARCH m USING INTEGER PRIMARY KEY (rowid>?)` and `SEARCH ae … (rowid>?)` for every account filter set. Commit A also gives scripts/bench_search.py a `--semantic` flag (random unit vectors, a plain embeddings keyset scan as the floor, cases none, account=a and since=) and adds tests/test_vector_scans.py. Commit B (Part 2, conditional): mirror `_collapse_ctes`' `cand`/`dup`/`ranked` (store.py:1465-1508) in a once-per-scan `_vector_copies` helper. It returns `shown_of` (first vector-bearing copy → rn=1 representative), `hidden` (other vector-bearing copies) and a `high` id bound. The scans drop `_shown_id` and the NOT EXISTS probe, filter and label rows in Python, move the cursor on the last raw row, never yield an empty chunk (best_k reshape at semantic.py:38) and bound by `m.id <= high`. Keep commit B only if unfiltered `vector_chunks` is at most 0.8× commit A on the bench. Otherwise revert it and record both numbers in the PR. **Files:** `src/imap_mailmind_mcp/store.py` · `scripts/bench_search.py` · `tests/test_vector_scans.py` · `README.md` **Decision:** accept the 3 open risk(s) above and dispatch this spec (`svd:go`), or park (`svd:skip`) / re-spec (`svd:respec`)? **Your call** (when this issue is held at the gate via `svd:hold`): - add label `svd:go` — approve; Half-B dispatch accepts exactly this spec (open risks accepted) - add label `svd:fire` **beside** `svd:go` — approve AND authorise the Half-B fire from the phone; never on its own - add label `svd:skip` — park it; dispatch refuses it and the issue stays for a human - add label `svd:respec` — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context <sub>Spec: `/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-keep-the-semantic-vector-scans-on-their-keyset-range-when-ac.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-keep-the-semantic-vector-scans-on-their-keyset-range-when-ac.md`</sub>
Collaborator

spec-verify-dispatch Half B for dev/imap-mailmind-mcp#102 keep-the-semantic-vector-scans-on-their-keyset-range-when-ac completed integration verify.

  • PR: dev/imap-mailmind-mcp#107
  • Branch: feat/svd-dev-imap-mailmind-mcp-102-keep-the-semantic-vector-s-3057ed21
  • Commit: 0003f3ea32
  • Build: none
  • Test: green
  • Reader audit clean: True
  • Spec fidelity: True
  • Ready for merge: True
  • Check commit statuses on PR head 0003f3e: absent
  • Checks: No remote checks exist for 0003f3e (the statuses list is empty, so the row is absent). Local make check is green with 754 passed. The spec's fast-verify suite is green with 382 passed.
  • Warning: No remote CI reported on the PR head; the Forgejo statuses list is empty.
  • Warning: The subagent's bench numbers come from the host. The architect's image measurements are still required before merge (A account/unfiltered 0.9782 against the 1.2 limit; B/A unfiltered 0.7791 against the 0.8 limit).
  • Warning: The PR is a single commit holding Parts 1 and 2. The spec's lean asked for two commits (A, then B). Part 2 met its 20% gate, so B stays and no revert is needed.
  • Warning: The subagent measured Parts A and B without commits, per the runner's edit-and-test-only instruction.
  • Warning: The audit needed UV_TOOL_DIR=/tmp/svd-uv-tools because of a sandbox restriction on uv's default tool directory.
  • Warning: no CI for commit statuses on PR head 0003f3e: The Forgejo statuses API returns an empty list for the PR head; the repo has no CI files (CLAUDE.md: .woodpecker is added by the architect). No tracking pointer exists. — reported as absent, not blocking (gh#633)
  • Decision: review the PR and merge? (codex-svd ship --run-id 20261101T203624Z-3057ed21 --confirm-merge)
spec-verify-dispatch Half B for dev/imap-mailmind-mcp#102 keep-the-semantic-vector-scans-on-their-keyset-range-when-ac completed integration verify. - PR: https://git.groupthink.asia/dev/imap-mailmind-mcp/pulls/107 - Branch: feat/svd-dev-imap-mailmind-mcp-102-keep-the-semantic-vector-s-3057ed21 - Commit: 0003f3ea32854304aa9c5e241dfbc942d9bf59cd - Build: none - Test: green - Reader audit clean: True - Spec fidelity: True - Ready for merge: True - Check commit statuses on PR head 0003f3e: absent - Checks: No remote checks exist for 0003f3e (the statuses list is empty, so the row is absent). Local make check is green with 754 passed. The spec's fast-verify suite is green with 382 passed. - Warning: No remote CI reported on the PR head; the Forgejo statuses list is empty. - Warning: The subagent's bench numbers come from the host. The architect's image measurements are still required before merge (A account/unfiltered 0.9782 against the 1.2 limit; B/A unfiltered 0.7791 against the 0.8 limit). - Warning: The PR is a single commit holding Parts 1 and 2. The spec's lean asked for two commits (A, then B). Part 2 met its 20% gate, so B stays and no revert is needed. - Warning: The subagent measured Parts A and B without commits, per the runner's edit-and-test-only instruction. - Warning: The audit needed UV_TOOL_DIR=/tmp/svd-uv-tools because of a sandbox restriction on uv's default tool directory. - Warning: no CI for commit statuses on PR head 0003f3e: The Forgejo statuses API returns an empty list for the PR head; the repo has no CI files (CLAUDE.md: .woodpecker is added by the architect). No tracking pointer exists. — reported as absent, not blocking (gh#633) - Decision: review the PR and merge? (`codex-svd ship --run-id 20261101T203624Z-3057ed21 --confirm-merge`)
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dev/mail-mind-mcp#102
No description provided.