mail_evidence costs about 2 s of a full core per call: the Sent query scans every Sent message with instr() #136
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
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
dev/mail-mind-mcp#136
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?
Problem
One
mail_evidencecall costs about 2 seconds of a full CPU core in theapicontainer on the live piers instance. The People Mind cold pass calls it once per Handle that is not the Owner. On the live mailbox that is 5,505 calls, so the first pass takes about 3 hours. The People Mind client pacer allows 4 calls per second and never engages.Measurement (2026-11-02, mailmind 0.10.2 on starbase-syd, People Mind 0.1.0-rc.4)
CallToolRequestlines in theapilogapicontainer CPU during the passsynccontainer CPU at the same timesynccontainer CPUThe caller runs one MCP session and one call at a time, so the gap is the server's time per call.
Cause
Store.evidence()insrc/mail_mind_mcp/store.py(0.10.2, from line 1886) runs three statements per call. Two cannot use an index:sent:FROM messages m LEFT JOIN mailboxes b ... WHERE b.role = 'sent' AND (instr(lower(m.to_addrs), ?) > 0 OR instr(lower(m.cc_addrs), ?) > 0).instrover a lowered column scans every Sent message on every call. The parsed-cell check in Python then filters the matches.by_roleandnewestusemessages_from (account, from_addr)for the address, but theshownclause is a correlatedNOT EXISTSovermessages cper candidate row.The schema has
messages_from,messages_date,messages_msgidandmessages_copies. It has no table or index that maps a recipient address to a message.Options
recipients (account, address, message_id)table filled on ingest from the parsed To and Cc cells, with an index on(account, address). Thesentquery becomes an index lookup and the Python re-check goes away.mail_evidencereads one row and runs only thenewestquery.mail_evidencethat takes up to N addresses per call. This cuts round trips but not the scan, so it is a complement, not the fix.Acceptance
mail_evidencep50 under 100 ms on the live piers store with the Sent mailbox at its current size.Relation
Filed under: operator word "accept" sha256:c125d0397b60
Agent Brief
Category: bug
Summary: The default
mail_evidencecall scans the wholemessagestable, becausemessages_from (account, from_addr)cannot serve a lookup with no account. Add an index that can.Current behavior:
Verified on
mainat28839b9against a synthetic store of 180k messages (150k inbox, 30k Sent).mail_evidencepassesaccount=Noneandexclude_sent=Trueby default (server.py:667).by_role(store.py:1927) plans asSCAN m, a full table read. Themessages_fromindex leads withaccount, and the query gives none.newestplans asSCAN m USING INDEX messages_from, a walk of the whole index.exclude_sentsubquery (_SELF_SENDERS AND s.from_addr = ?,store.py:419and1914) scans every Sent and Drafts row in each statement.ANALYZE, so SQLite has no statistics and no skip-scan. The live store plans the same way.Timings (median of 5): 123 ms as is, 37 ms with
exclude_sent=False, and 13 ms with one index onmessages(from_addr). Live rows average about 13 KB (a 2 GB store), so each full read costs far more there. That fits the measured 2 s per call.Correction to the issue text: the
instr()sentstatement is not the main cost. It reads only Sent rows through the(account, mailbox)index, 11 ms for 30k Sent rows here. #138 moves it onto a recipients table.Desired behavior:
from_addrlookup with or without an account, for examplemessages (from_addr, account).by_role,newestand theexclude_sentsubquery all search that index. Themail_peopleandpeoplepaths keep working, and use it where the planner picks it.SCHEMA_VERSIONgoes up by one.Store(reader=True)never creates it and refuses an older schema, as today (store.py:561-568).mail_evidenceoutput is unchanged for every input (ADR 0008 §2).Key interfaces:
store.py:112-116),SCHEMA_VERSION(store.py:31) and the migration path.Store.evidence(store.py:1898): no query text change is expected. Change it only if the planner needs help, and say why in the PR.Acceptance criteria:
EXPLAIN QUERY PLANon theby_roleandneweststatements and theexclude_sentsubquery withaccount=None. Each shows aSEARCHusing the new index, and none showsSCAN morSCAN s.mail_evidence,mail_peopleandpeopletest passes unchanged.Store.evidencetime before and after on a synthetic store of at least 150k messages with a Sent mailbox. The image ships SQLite 3.40.1 (#96), so run it in the image, head and base alternating in one container.make checkpasses. Tests run asuv run python3 -m pytest.Out of scope:
sentstatement (#138).mail_evidence, or any new field.ANALYZEorPRAGMA optimize.Order: #136 lands before #138 and #108, because each of them also raises
SCHEMA_VERSION.Operator check after deploy: time
mail_evidenceon the live piers store. The target is p50 under 100 ms, and a People Mind cold pass under 15 minutes. If the target is missed, the remaining cost is thesentscan, which #138 removes.⏸ SVD architect gate: dev/mail-mind-mcp#136 mail-evidence-costs-about-2-s-of-a-full-core-per-call-the-se
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:
20261102T074145Z-e10bc52eHeld because: CLEAN but 3 open risk(s) (2 verification · 1 decision) — HOLD-ALL parks everything. The risks, verbatim:
Open risks:
Change: Add
CREATE INDEX IF NOT EXISTS messages_sender ON messages (from_addr, account, message_id);to the_TABLESindex list after messages_copies (store.py:116). Writer opens run it via executescript(_SCHEMA) at store.py:525, so an existing v11 store gains it. Bump SCHEMA_VERSION 11→12 (store.py:31); the reader wait/refuse at :554-569 is unchanged. Use 3 columns, not the brief's 2-column example: measured on the host, (from_addr, account) makes account-scoped evidence quadratic (452 ms → 21.4 s for a 3,000-message sender), because the correlated _same_message copy lookup switches off messages_msgid. With message_id third, that lookup is a 3-equality search. Store.evidence query text is unchanged. In Store.people (store.py:1887), GROUP BY+m.from_addronly when account is None, because the new index otherwise turns the unscoped table scan into a slower index walk (605→927 ms). Mirror the096208fschema-v11 bump: the RecordingConnection + EXPLAIN QUERY PLAN tests (tests/test_embed_scans.py:84-104) and the v10 migration/reader-refusal test (:154-170). Bump every== 11/schema v11test literal. Add scripts/bench_evidence.py mirroring scripts/bench_embed.py, and README/CLAUDE.md notes.Files:
src/mail_mind_mcp/store.py·tests/test_evidence.py·tests/test_embed_scans.py·tests/test_store.py·tests/test_copy_collapse.py·tests/test_attachment_vectors.py·tests/test_attachments.py·scripts/bench_evidence.py·README.md·CLAUDE.mdDecision: 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):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-mail-evidence-costs-about-2-s-of-a-full-core-per-call-the-se.md— next:codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-02-mail-evidence-costs-about-2-s-of-a-full-core-per-call-the-se.mdspec-verify-dispatch Half B for dev/mail-mind-mcp#136 mail-evidence-costs-about-2-s-of-a-full-core-per-call-the-se completed integration verify.
8f80b5243b8f80b52(empty Forgejo status list). Local: 839 passed, 8 skipped, 0 failed. No blockers.8f80b52is empty, so no remote CI context reported. CLAUDE.md says CI files are added by the architect.make checklint, mypy and audit were not re-run by this stage.codex-svd ship --run-id 20261102T081440Z-dc251286 --confirm-merge)