Embedder scans the whole account twice per batch of 32: unembedded() and copy_embeddings() #119
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#119
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?
Split from the #91 investigation. On mailmind-piers (0.8.0, 2026-11-01), with no backfill running, embed batches of 32 took 8 to 125 s (p50 8.8 s, mean per 5-minute window 8.5 to 67.5 s) at low CPU. The time is in two store calls, not in the model.
Today
Store.unembeddedruns a LEFT JOIN over every message of the account, ordered byjulianday(COALESCE(internaldate, date)) DESC. No index serves that order; the plan is a full account scan into a temp B-tree, fetchingm.*(bodies included) for every unembedded row.Store.copy_embeddingsruns underBEGIN IMMEDIATEand joins every message of the account to its #59 copies with a NOT EXISTS probe. It holds the database write lock for the whole scan.unembedded6.0 s,copy_embeddings1.1 s. On piers the database is 2.5 GB and the sync container has about 160 Mi of page cache under its 768 Mi limit, so the scans read from block storage each time.secondscovers the model plusset_embeddingspluscopy_embeddings, and notunembedded, so the true per-batch cost is higher than the log shows.copy_embeddingsruns, Litestream's checkpoint fails withdatabase is locked(82 times in 52 minutes on 2026-11-01) and the sync worker's other writes wait on the same lock.Proposal (decide at triage)
copy_embeddingsonce per pass per account, or restrict it to the #59 keys of the rows just embedded, and do the scan as a read before a short write transaction.unembeddedindex-served: a cursor over(account, internaldate_or_date_key, id)the worker advances, or an index on an expression the store already maintains (the UTC key planned in #108 would serve both). Stop selectingm.*; fetch the 32 rows by id after ranking.unembeddedtime with the batch, or include it inseconds.Acceptance
set_embeddingsinsert itself.make checkpasses.Out of scope
Filed under: operator word "implement #91" sha256:4cf5e63dc126
Decided at triage 2026-11-02: restrict the per-batch copy to the batch's #59 keys plus one sweep per pass; an expression index for the newest-first lookup, ids first and rows second; the lookup time as its own log field.
Agent Brief
Category: bug
Summary: Stop the embedder scanning the whole account twice per batch of 32
Current behavior:
Before each batch the worker asks the store for the newest rows without a vector. That query LEFT JOINs every message of the account to the embeddings table, orders by
julianday(COALESCE(internaldate, date)) DESC, id DESCwith no index to serve it, and fetches every column of every candidate row into a temp B-tree. After each batch the worker calls the copy step, which underBEGIN IMMEDIATEjoins every message of the account to its #59 copies with a NOT EXISTS probe, holding the write lock for the whole scan. On a 150k-row store the two calls cost 6.0 s and 1.1 s per batch with a warm cache. On a 2.5 GB mirror under a 768 Mi container limit they read from block storage, 60 to 125 s per batch at low CPU, and Litestream's checkpoint fails with "database is locked" while the copy step runs. The logged batchsecondsexcludes the lookup.Desired behavior:
(account, julianday(COALESCE(internaldate, date)) DESC, id DESC)created in the schema migration list. The query selects ids only, then loads the rows for those ids. Behaviour is unchanged: newest first,batchrows, rows with a vector from the current model excluded.lookup_secondsbeside the existingseconds. Counts only, no identifiers.Key interfaces:
Store.unembeddedandStore.unembedded_attachments: same signature and order, ids-first plan.Store.copy_embeddingsandStore.copy_attachment_embeddings: gain a keyed form taking the rows or keys just embedded; the unkeyed form stays for the sweep.EmbedWorker._account: per-batch keyed copy, one sweep at pass end, the new log field.Store(reader=True)on an old schema behaves as it does for other migrations.Acceptance criteria:
EXPLAIN QUERY PLANfor the lookup shows the new index and no temp B-tree for ORDER BY; a test asserts it.BEGIN IMMEDIATEand asserts none touchesmessageswithout an index onidor the new index.lookup_seconds; existing fields are unchanged.make checkpasses; the model tests pass aftermake model.Out of scope:
⏸ SVD architect gate: dev/imap-mailmind-mcp#119 embedder-scans-the-whole-account-twice-per-batch-of-32-unemb
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:
20261101T222737Z-b6c4c7c9Held because: CLEAN but 7 open risk(s) (5 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim:
Open risks:
uv run python scripts/bench_search.pyon base and branch, record medians in the PR against #96's 1.5x bound, and confirm tests/test_vector_scans.py and test_copy_collapse.py::test_account_plan_looks_up_ranked_copies_by_key pass. [verification]Change: store.py: add
_received(row)beside_copy_date(store.py:256-259) as a guarded expression,CASE WHEN COALESCE(internaldate,date) GLOB '[0-9]*' THEN julianday(...) END. The brief's literal julianday index fails on 'now'/'subsec' dates (measured; tests/test_copy_collapse.py:225-244 inserts them). Bump SCHEMA_VERSION 10→11. Addmessages_received ON messages (account, (_received()) DESC, id DESC)to_V5_INDEXES(store.py:2093) using the_SCHEMA.replace idiom (store.py:280). The reader branch runs no DDL.unembeddedandunembedded_attachmentsselect ids (and parts) ordered by_received('m')DESC, m.id DESC, then load rows viamessages_by_ids(store.py:1685-1700). The measured plan is a COVERING INDEX messages_received search with no temp B-tree: 0.02 ms on 150k rows. Newcopy_embeddings_from(row_ids, model)andcopy_attachment_embeddings_from(parts, model)read target/origin pairs viacopy_keyequality outside any transaction. They then write in_tx()chunks of 500 with the set_embeddings deleted-row guard (store.py:891-899), pushing to copies in every account. The sweepscopy_embeddingsandcopy_attachment_embeddingskeep their signatures and selects but read pairs first, then write in short chunked transactions. embed.py_account(177-236) keeps the one sweep at the pass start, which existing tests pin (test_embed.py:100-104, test_attachment_vectors.py:296-298). Per round it calls the keyed copy and addslookup_seconds(round 3) to the batch log extra. Mirror bench_search.py for the new scripts/bench_embed.py; doc lines go in README.md:209-211 and CLAUDE.md:31.Files:
src/imap_mailmind_mcp/store.py·src/imap_mailmind_mcp/embed.py·tests/test_embed_scans.py·tests/test_cross_account_copies.py·tests/test_attachment_vectors.py·tests/test_store.py·tests/test_attachments.py·tests/test_copy_collapse.py·scripts/bench_embed.py·README.md·CLAUDE.mdDecision: accept the 7 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-embedder-scans-the-whole-account-twice-per-batch-of-32-unemb.md— next:codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-embedder-scans-the-whole-account-twice-per-batch-of-32-unemb.mdspec-verify-dispatch Half B for dev/imap-mailmind-mcp#119 embedder-scans-the-whole-account-twice-per-batch-of-32-unemb completed integration verify.
096208fcb0096208f: no Forgejo commit statuses (absent, non-blocking). Local: spec pytest set 454 passed; make check 779 passed, audit clean.096208f; remote CI is unverified.096208fhas no commit statuses on git.groupthink.asia; CI files (.woodpecker/*) are added by the architect, so no required check has run. — reported as absent, not blocking (gh#633)codex-svd ship --run-id 20261101T225227Z-f150e256 --confirm-merge)