Make the #59 copy collapse cheap on searches with many hits #96
Labels
No labels
bug
enhancement
epic
needs-info
needs-triage
ready-for-agent
ready-for-human
svd:go
svd:hold
wontfix
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
dev/mail-mind-mcp#96
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Follow-up to #59. Filed after the 0.6.0 batch merged.
Problem
#59 collapses copies of one message in
mail_search.Store.searchfinds the row shown for each hit with a correlated subquery (_shown_id,store.py), and counts copies with a second one (_copies_count). Both run once per hit row. A search with few hits costs the same as before. A search with many hits, such as no query and no filter, oraccount=alone, costs about 4.5 times more.Evidence (synthetic, 2026-11-01)
A store of 210,000 messages: 200,000 in one account and every twentieth copied into a second account, with Message-ID, From and Date set. Medians of 5 runs, warm cache, no writer:
3af5d34)b34dd9d)account=a, limit 25sender=, 400 hitsThe #59 dispatch measured 62 ms to 293 ms (4.7×) on 105,000 rows. The cost grows with the number of hits, so a mirror of 300,000 or more messages will pass 1 s for an unfiltered search.
Options (decide at triage)
copy_key, null when Message-ID, From or Date is missing) and index it. Collapse withGROUP BY copy_keyor a window function over the hit set, instead of one probe per row. This needs a schema step. #79 also plans v8, so the two must agree on the number.limitplus a margin of rows, collapse them in Python, and fetch more if copies dropped rows.totalwould then count rows, not messages, unless it is computed another way. This changes thetotalcontract that #59 set.Acceptance
tests/test_copies.pytest passes unchanged.mail_searchoutput is byte-for-byte the same as on main for a store with copies.Filed under: operator word "proceed" sha256:119792136aa2
Agent Brief
Category: enhancement (performance)
Summary: Pick the row shown for each message with one pass over the hit set, not two correlated probes per hit row, so a search with many hits costs what it cost before #59.
Operator rulings (2026-11-01). Where this brief differs from the issue body, the brief holds.
_copy_key(src/imap_mailmind_mcp/store.py:362) already computes as one value. Add a stored key column only if the set-based query misses the 1.5× bound. A stored column takes the next schema version after main's at dispatch time. #79 plans v8, so do not assume a number._shown_id,store.py:1286). A flag kept by the writer cannot depend on the filters of a query.totalcontract that #59 set:totalcounts messages. It also fails this issue's own byte-for-byte acceptance bullet.Current behavior:
Store.search(store.py:1305) buildshitsasSELECT DISTINCT <_shown_id> ...._shown_idruns a correlated subquery per hit row. The page query then runs_copies_count, which is a second correlated subquery per row.keyword_ranked,vector_chunksandtriage_countsuse the same per-row probes (_shown_id,_same_message,_ahead).a, plus a copy of every twentieth row in accountb, with Message-ID, From and Date set. An unfiltered search with limit 25 takes 164 ms at3af5d34and 742 ms atb34dd9d.Desired behavior:
copies=Ncounts the copies that pass the filters;total,moreand the cursor count messages.ROW_NUMBER()and count them withCOUNT(*), both partitioned by the key. Keyless rows get a partition of their own, for example by row id.keyword_ranked,vector_chunksandtriage_countswhere it removes a per-row probe. Leave a site alone if the change does not make it measurably cheaper.scripts/bench_search.py. It builds the synthetic store above in a temporary directory and prints the medians of the three searches in the issue's table.make checkdoes not run it. The gate and later issues use it to check the bound.Key interfaces:
_copy_key,_same_message,_shown_id,_copies_countand_aheadinstore.py. Keep one definition of the key. If_shown_idor_copies_countloses its last caller, delete it.messages_copies (message_id, from_addr)index already exists. A new index is allowed.Acceptance criteria:
scripts/bench_search.pyreports each of the three searches within 1.5× of its value at3af5d34, measured with the same script on both commits. Put the numbers in the PR.tests/test_copies.pypasses unchanged.mail_searchoutput on a store with copies is byte-for-byte the same as on main, in keyword, semantic and hybrid modes and withsimilar_to. Add a test that compares the new query with the old one on a fixture that has copies, keyless rows, filters and an FTS match in a copy that is not shown.tests/test_source.pypasses: there is no mail-server call inserver.py.make checkpasses.Out of scope:
⏸ SVD architect gate: dev/imap-mailmind-mcp#96 make-the-59-copy-collapse-cheap-on-searches-with-many-hits
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
substrate· dispatch efforthighRun:
20261101T110952Z-efc8f0f6Held because: CLEAN but 4 open risk(s) (2 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim:
Open risks:
3af5d34and on the branch on the same host, with the ratios for none, account=a and sender= each ≤ 1.5. If plan differences under the image's sqlite 3.40.1 are a concern, run it once in the image too. [verification]Change: Schema v10 (main + 1): add a VIRTUAL generated column, messages.copy_key, built from the existing _copy_key expression. Give _copy_key a bare-column form (SQLite forbids '.' in generated columns) and move it above _SCHEMA at store.py:251. Fresh stores get the column through a {copy_key} placeholder in _TABLES. Old stores get it through _ADDED_COLUMNS at store.py:2009, and _add_columns at store.py:2045 must switch to PRAGMA table_xinfo, because table_info hides generated columns and would re-add the column on every boot. Add a covering partial index, messages_copy_key (copy_key, account, mailbox) WHERE copy_key IS NOT NULL, in the post-add index script next to _V5_INDEXES at store.py:2020. Do not put it in TABLES. Rewrite Store.search (store.py:1454) as one statement per existing if-query branch. With no query: a whole-table dup-key set, then ROW_NUMBER/COUNT windows over the filter-passing rows of the repeated keys only, then a page of filtered rows NOT IN the rn>1 rows; total = filtered count minus hidden count. With a query: hit, then dupkey (hit keys whose covering-index count is above 1), then ranked, then shown (keyless or unique hits, plus rn=1 rows), then page with COUNT(*) OVER (). Use CROSS JOIN to fix the join order, and add a count-only fallback when the page is empty. Apply the same CTEs to triage_counts (store.py:1486) and keyword_ranked (store.py:1511). Leave the vector* sites and messages_by_ids on the per-row probes, so _shown_id and _copies_count keep their callers. Add scripts/bench_search.py: a raw-SQL bulk build of a 210k-row store with bodies of about 2 KB, timing Store.search, and runnable at
3af5d34through PYTHONPATH.Files:
src/imap_mailmind_mcp/store.py·scripts/bench_search.py·tests/test_copy_collapse.py·tests/test_store.py·tests/test_attachments.py·tests/test_attachment_vectors.py·README.md·CLAUDE.mdDecision: accept the 4 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):svd:go— approve; Half-B dispatch accepts exactly this spec (open risks accepted)svd:firebesidesvd:go— approve AND authorise the Half-B fire from the phone; never on its ownsvd:skip— park it; dispatch refuses it and the issue stays for a humansvd:respec— discard this spec; the next Half-A run re-produces it with this record as prior-attempt contextSpec:
/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-make-the-59-copy-collapse-cheap-on-searches-with-many-hits.md— next:codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-make-the-59-copy-collapse-cheap-on-searches-with-many-hits.mdspec-verify-dispatch Half B for dev/imap-mailmind-mcp#96 make-the-59-copy-collapse-cheap-on-searches-with-many-hits completed integration verify.
30547f12b230547f1: no commit statuses and no CI checks on Forgejo. Local filtered tests (356) and make check (658 passed, 8 skipped) are green. No blockers.30547f1; the Forgejo repo reports no CI checks (CI files are added by the architect).3af5d34, not the spec based0ee0f1. The PR body says it used a temporary source export, so the baseline commit is unclear.codex-svd ship --run-id 20261101T113607Z-21adcf62 --confirm-merge)