Add a recipients table: an exact recipient filter on mail_search, and mail_evidence Sent lookups by index #138
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#138
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?
Raised during the triage of #136 on 2026-11-02. A recipients table makes recipient lookups exact and indexed. This issue builds it, adds the recipient filter, and moves the
mail_evidenceSent query onto it.Agent Brief
Category: enhancement
Summary: A
recipientstable filled by the sync worker, arecipientfilter onmail_search, and themail_evidenceSent query moved onto that tableCurrent behavior:
mail_searchfilters onsender, but not on a recipient. "What did I send to X" or "mail where X was on Cc" can only use the full-text query. FTS indexesto_addrs(store.py:40-59), but it matches tokens, not exact addresses, and it never seescc_addrs.Store.evidencefinds Sent messages that name an address withinstr(lower(m.to_addrs), ?)andinstr(lower(m.cc_addrs), ?)over every Sent row, then re-checks the parsed cells in Python (store.py:1932-1953). The cost grows with the Sent mailbox.Desired behavior:
recipients (message_id, account, address, kind).message_idismessages.id, andkindistoorcc. It has one row per parsed address per message, using the #129 rule (evidence.address: lowercase, an@, no whitespace). An address that does not parse gets no row. Bcc is not stored, as today.address, so a lookup with no account uses it. Themessages_from (account, from_addr)index cannot serve an account-free lookup, which is the #136 defect, so do not repeat that shape.Store.upsert_messages(store.py:697) fills the table. Both sources write through it (ingest.py:234,jmap.py:621). When an upsert changesto_addrsorcc_addrs, the rows for that message are replaced.AFTER DELETE ON messagestrigger removes a message's rows, so every delete path is covered (delete_uids,delete_sourcesand any other). The store does not enablePRAGMA foreign_keys, so a foreign key alone would not do this.to_addrsandcc_addrsin the writer, in batches. The parse runs in Python, so the SQL and Python rules cannot drift.Store(reader=True)never writes it and refuses an older schema, as today (store.py:561-568).SearchParamsgainsrecipient: str | None. The filter matches when the address is in the message's To or Cc. The match is exact, and the argument is lowercased.mail_searchtakesrecipient, and its description and the README row say that it matches To or Cc exactly and never Bcc.mail_count(#111) reusesSearchParams, so it gets the filter with no further work.Store.evidencereads its Sent rows throughrecipients. Theinstrscan and the Python re-check go away. The copy-key dedupe stays. Themail_evidenceoutput is unchanged for every input: same fields and same values (ADR 0008 §2).Key interfaces:
Store.upsert_messages, the schema,SCHEMA_VERSIONand the migration list.SearchParamsandStore._filters(store.py:1518).mail_searchsignature and docstring.Store.evidence: thesentstatement only.Acceptance criteria:
a@example.testwith Ccb@example.testgives two rows,toandcc. A To cell holdingfirst last@example.testgives no row.delete_uidsordelete_sourcesremoves them.mail_search(recipient="b@example.test")returns the message above.recipient="example.test"returns nothing, because the match is exact.EXPLAIN QUERY PLANfor the recipient filter and for the evidence Sent statement, with no account given, shows a search using the new index, not a scan. Assert this in a test.mail_evidenceoutput is byte-for-byte unchanged on the existing evidence test cases, including a longer address that contains the queried one (xa@example.testagainsta@example.test).make checkpasses. Tests run asuv run python3 -m pytest.Primary files:
src/mail_mind_mcp/store.pysrc/mail_mind_mcp/server.py(mail_searchonly)README.md(themail_searchrow)tests/test_recipients.py(new)tests/test_store.py,tests/test_evidence.py(only where an existing case needs the new schema)Out of scope:
mail_people. The operator rated it "might be useful" on 2026-11-02. It would be a second consumer of this table, and it needs an ADR 0008 amendment, becausemail_people's fields are in the evidence scope. Not filed.mail_reador themail_changesenvelope.from_addrindex forby_roleandnewest(#136).Blocked by
SCHEMA_VERSION; #136 is the smaller change and lands first)Filed under: operator word "1 yes" sha256:c592151fdf13
⏸ SVD architect gate: dev/mail-mind-mcp#138 add-a-recipients-table-an-exact-recipient-filter-on-mail-sea
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:
20261102T085850Z-3ef3ee29Held because: CLEAN but 2 open risk(s) (1 verification · 1 decision) — HOLD-ALL parks everything. The risks, verbatim:
Open risks:
uv run python scripts/bench_evidence.pyin the image (at least 150k rows, base and head alternated in one container). Its 'sent account=None plan:' block must contain 'SEARCH r USING COVERING INDEX recipients_address (address=?)' and no 'SCAN m'; record it in the PR. If 3.40.1 plans differently, stop and report rather than adding planner hints. Live check after deploy: the first v13 sync start logs 'recipients filled for schema v13' once with a rows count, and the api container comes up within its 60 s schema wait. [verification]Change: Follow the child-table + AFTER DELETE trigger shape of embeddings/attachment_text in store.py:215-253. Add
recipients (message_id, account, address, kind) PRIMARY KEY (message_id, kind, address) WITHOUT ROWID, withrecipients_address (address, message_id)and amessages_recipients_adtrigger. One module helper,_write_recipients(conn, row_id, account, to, cc), deletes the message's rows and then insertsevidence.addresses()per kind. It is called fromupsert_messages(RETURNING idadded to the upsert at store.py:709-752) and from a new batchedStore._fill_recipients(keyset by id, one_txper 2,000 rows, mirroring_write_embedding_copiesat :955-966). The fill runs in the writer__init__between_add_columnsand the version upsert when_schema_version < 13, and logsrowsonly, like_migrate_ftsat :2302-2317.SCHEMA_VERSION12 → 13.SearchParams.recipientplus a_filtersterm{alias}.id IN (SELECT rcp.message_id FROM recipients rcp WHERE rcp.address = ?), lowercased, after the sender block at :1545-1548.mail_searchgets arecipientargument aftersender(server.py:238/296).Store.evidence's sent statement becomes... AND m.id IN (SELECT r.message_id FROM recipients r WHERE r.address = ?); the instr scan and theaddresses()re-check go, and the copy-key dedupe stays. Other edits: README row 47 and the v13 notes at :92/:238;bench_evidence.pyrewinds meta to 12 after its raw inserts so the fill runs, and prints the sent plan; the 12→13 test literals; thetest_evidence.py:119-125prefix and skip update. The whole lean was run in the Half-A sandbox: same failure set as base, plus 6 new passes; mypy clean.Files:
src/mail_mind_mcp/store.py·src/mail_mind_mcp/server.py·README.md·tests/test_recipients.py·tests/test_evidence.py·tests/test_store.py·tests/test_embed_scans.py·tests/test_copy_collapse.py·tests/test_attachment_vectors.py·tests/test_attachments.py·scripts/bench_evidence.pyDecision: accept the 2 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-02-add-a-recipients-table-an-exact-recipient-filter-on-mail-sea.md— next:codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-02-add-a-recipients-table-an-exact-recipient-filter-on-mail-sea.mdspec-verify-dispatch Half B for dev/mail-mind-mcp#138 add-a-recipients-table-an-exact-recipient-filter-on-mail-sea completed integration verify.
2cfa22773c2cfa227(no CI in repo) - absent, non-blocking. Local: 846 passed, 8 skipped, 0 failed; ruff and mypy clean.codex-svd ship --run-id 20261102T093644Z-7362604b --confirm-merge)