Snapshot activation scans every staged row per Mail interaction and locks the store #81

Closed
opened 2026-11-04 12:09:46 +00:00 by svc-svd-writer · 2 comments
Collaborator

Problem

Snapshot activation (ADR 0010 rule 6, step 3) takes time proportional to Mail interactions times staged rows. During that time the writer holds the store lock, so every other write fails with store-unavailable.

Observed live on 2026-11-04 (People 0.1.0-rc.8, Mail Mind 0.13.0):

  • At 11:44Z the pass negotiated mail-people/1.1 and started the planned rebuild (#68). Staging completed with 259,873 rows; 83,377 Mail message: interactions existed.
  • Activation started at 11:55:04Z. The sync container then ran at one full core with no WAL growth for 10 minutes or more.
  • Meanwhile every CardDAV pass and embed run failed with store-unavailable. Reads kept working.

Cause

src/people_mind_mcp/sync.py:321-327 (main 98ad738) deletes interactions with this subquery:

NOT EXISTS (SELECT 1 FROM mail_staged s WHERE s.source=interactions.source
  AND s.address=interactions.source_record AND s.entity_id=substr(interactions.ref,9)
  AND s.direction=interactions.direction AND s.date=interactions.date)

The mail_staged primary key is (source, snapshot_id, entity_id, address, direction). The subquery does not bind snapshot_id, so SQLite can use only the source prefix: SEARCH s USING INDEX sqlite_autoindex_mail_staged_1 (source=?). Each interaction scans every staged row of the Source.

Measured on a copy with the live staged count (259,873 rows) and 8,338 interactions, which is 1/10 of the live interactions:

  • the query as written: 55.8 s;
  • the same query with s.snapshot_id=? bound: 0.014 s.

The query landed with #51 (24c0c17). It became visible now because the first live activation ran on a store with few Mail interactions.

Fix

  1. Bind the active snapshot id in the subquery (s.snapshot_id = ?, the page.snapshot_id that activates), so the lookup uses the primary key prefix (source, snapshot_id, entity_id).
  2. Check the other mail_staged statements on the activation path for the same pattern, and bind snapshot_id where the snapshot is known: sync.py:309-311 (the per-address delete of disallowed addresses) and sync.py:315 (the affected-address union).
  3. Do not change the schema unless a statement cannot bind snapshot_id. If one cannot, add the narrowest index and record it as a schema step.

Acceptance criteria

  • A test asserts that EXPLAIN QUERY PLAN of the activation retraction searches mail_staged with snapshot_id=? in its index constraint, and does not search on source=? alone.
  • A test activates a snapshot over a store that already holds Mail interactions, some supported and some not, and asserts the same retractions as today (C06 and rule 10 behaviour unchanged).
  • make check passes.

Out of scope

  • The per-page COUNT(*) of mail_staged and mail_staged_fences (sync.py:286-287) also grows with the staged set. It is linear per page, not per interaction; leave it unless the fix makes it the bottleneck.
  • graph.py:30 DELETE_MAIL_STAGED (Forget) scans by address. Forget is rare; leave it.

Context

  • ADR 0010 rule 6 accepts one unbounded activation transaction, bounded by max_staged_records. This defect makes that transaction quadratic, which the ADR did not accept.
  • The #49 I02 soak needs a rebuild on a populated store, so it waits for this fix.

Filed under: operator word "accept" sha256:c125d0397b60 (#10 gate)

## Problem Snapshot activation (ADR 0010 rule 6, step 3) takes time proportional to Mail interactions times staged rows. During that time the writer holds the store lock, so every other write fails with `store-unavailable`. Observed live on 2026-11-04 (People `0.1.0-rc.8`, Mail Mind `0.13.0`): - At 11:44Z the pass negotiated `mail-people/1.1` and started the planned rebuild (#68). Staging completed with 259,873 rows; 83,377 Mail `message:` interactions existed. - Activation started at 11:55:04Z. The sync container then ran at one full core with no WAL growth for 10 minutes or more. - Meanwhile every CardDAV pass and embed run failed with `store-unavailable`. Reads kept working. ## Cause `src/people_mind_mcp/sync.py:321-327` (main `98ad738`) deletes interactions with this subquery: ```sql NOT EXISTS (SELECT 1 FROM mail_staged s WHERE s.source=interactions.source AND s.address=interactions.source_record AND s.entity_id=substr(interactions.ref,9) AND s.direction=interactions.direction AND s.date=interactions.date) ``` The `mail_staged` primary key is `(source, snapshot_id, entity_id, address, direction)`. The subquery does not bind `snapshot_id`, so SQLite can use only the `source` prefix: `SEARCH s USING INDEX sqlite_autoindex_mail_staged_1 (source=?)`. Each interaction scans every staged row of the Source. Measured on a copy with the live staged count (259,873 rows) and 8,338 interactions, which is 1/10 of the live interactions: - the query as written: 55.8 s; - the same query with `s.snapshot_id=?` bound: 0.014 s. The query landed with #51 (`24c0c17`). It became visible now because the first live activation ran on a store with few Mail interactions. ## Fix 1. Bind the active snapshot id in the subquery (`s.snapshot_id = ?`, the `page.snapshot_id` that activates), so the lookup uses the primary key prefix `(source, snapshot_id, entity_id)`. 2. Check the other `mail_staged` statements on the activation path for the same pattern, and bind `snapshot_id` where the snapshot is known: `sync.py:309-311` (the per-address delete of disallowed addresses) and `sync.py:315` (the affected-address union). 3. Do not change the schema unless a statement cannot bind `snapshot_id`. If one cannot, add the narrowest index and record it as a schema step. ## Acceptance criteria - [ ] A test asserts that `EXPLAIN QUERY PLAN` of the activation retraction searches `mail_staged` with `snapshot_id=?` in its index constraint, and does not search on `source=?` alone. - [ ] A test activates a snapshot over a store that already holds Mail interactions, some supported and some not, and asserts the same retractions as today (C06 and rule 10 behaviour unchanged). - [ ] `make check` passes. ## Out of scope - The per-page `COUNT(*)` of `mail_staged` and `mail_staged_fences` (`sync.py:286-287`) also grows with the staged set. It is linear per page, not per interaction; leave it unless the fix makes it the bottleneck. - `graph.py:30` `DELETE_MAIL_STAGED` (Forget) scans by address. Forget is rare; leave it. ## Context - ADR 0010 rule 6 accepts one unbounded activation transaction, bounded by `max_staged_records`. This defect makes that transaction quadratic, which the ADR did not accept. - The #49 I02 soak needs a rebuild on a populated store, so it waits for this fix. Filed under: operator word "accept" sha256:c125d0397b60 (#10 gate)
Author
Collaborator

⏸ SVD architect gate: dev/people-mind-mcp#81 snapshot-activation-scans-every-staged-row-per-mail-interact

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: 20261104T122137Z-e7753f8f

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

Open risks:

  1. Live effect is provable only after deploy. Check: after the next rebuild activation on the populated store (about 260k staged rows and 83k Mail interactions), the mail pass completes in minutes rather than tens of minutes. CardDAV passes and embed runs in that window must not log store-unavailable. The planned vehicle is the #49 I02 soak. [verification]
  2. make check and the two new tests were not run at spec time: uv is not on PATH, the sandbox denies the .venv interpreter, and the repo is read-only. Before the first edit, dispatch must record the pristine-base make check failure set. After the change, the set must be unchanged apart from the two new tests passing, and test 2 must also pass on the pristine base. [verification]

Change: In src/people_mind_mcp/sync.py _mail_snapshot_page, move the activation retraction (sync.py:320-327) into a module constant RETRACT_UNSTAGED, placed directly below RETRACT_UNSUPPORTED (sync.py:27-32) and shaped the same way (triple-quoted, numbered params). It binds s.snapshot_id = ?2, and the call becomes conn.execute(RETRACT_UNSTAGED, (name, page.snapshot_id)). I probed this at spec time: the subquery plan goes from (source=?) to the full key (source=? AND snapshot_id=? AND entity_id=? AND address=? AND direction=?). Also bind page.snapshot_id in the other activation reads: the DISTINCT address select and per-address DELETE (309, 311), the affected-address union (315), and the mail_support and mail_fences copies from staging (334-342). Leave the COUNTs (286-287), the clean-up deletes (360-361), _mail_reset_snapshot, graph.py and the schema untouched. Binding keeps behaviour the same because every staged row of the Source carries page.snapshot_id at activation (invariant I1).

Files: src/people_mind_mcp/sync.py · tests/test_mailpeer.py

Decision: 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):

  • 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-04-snapshot-activation-scans-every-staged-row-per-mail-interact.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-04-snapshot-activation-scans-every-staged-row-per-mail-interact.md

### ⏸ SVD architect gate: dev/people-mind-mcp#81 snapshot-activation-scans-every-staged-row-per-mail-interact **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:** `20261104T122137Z-e7753f8f` **Held because:** CLEAN but 2 open risk(s) (2 verification) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. Live effect is provable only after deploy. Check: after the next rebuild activation on the populated store (about 260k staged rows and 83k Mail interactions), the mail pass completes in minutes rather than tens of minutes. CardDAV passes and embed runs in that window must not log store-unavailable. The planned vehicle is the #49 I02 soak. [verification] 2. make check and the two new tests were not run at spec time: uv is not on PATH, the sandbox denies the .venv interpreter, and the repo is read-only. Before the first edit, dispatch must record the pristine-base make check failure set. After the change, the set must be unchanged apart from the two new tests passing, and test 2 must also pass on the pristine base. [verification] **Change:** In src/people_mind_mcp/sync.py _mail_snapshot_page, move the activation retraction (sync.py:320-327) into a module constant RETRACT_UNSTAGED, placed directly below RETRACT_UNSUPPORTED (sync.py:27-32) and shaped the same way (triple-quoted, numbered params). It binds `s.snapshot_id = ?2`, and the call becomes conn.execute(RETRACT_UNSTAGED, (name, page.snapshot_id)). I probed this at spec time: the subquery plan goes from `(source=?)` to the full key `(source=? AND snapshot_id=? AND entity_id=? AND address=? AND direction=?)`. Also bind page.snapshot_id in the other activation reads: the DISTINCT address select and per-address DELETE (309, 311), the affected-address union (315), and the mail_support and mail_fences copies from staging (334-342). Leave the COUNTs (286-287), the clean-up deletes (360-361), _mail_reset_snapshot, graph.py and the schema untouched. Binding keeps behaviour the same because every staged row of the Source carries page.snapshot_id at activation (invariant I1). **Files:** `src/people_mind_mcp/sync.py` · `tests/test_mailpeer.py` **Decision:** 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`): - 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-04-snapshot-activation-scans-every-staged-row-per-mail-interact.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-04-snapshot-activation-scans-every-staged-row-per-mail-interact.md`</sub>
Author
Collaborator

spec-verify-dispatch Half B for dev/people-mind-mcp#81 snapshot-activation-scans-every-staged-row-per-mail-interact completed integration verify.

  • PR: #84
  • Branch: feat/svd-dev-people-mind-mcp-81-snapshot-activation-scans-eve-0ae89b3b
  • Commit: df2a06a92d
  • Build: none
  • Test: green
  • Reader audit clean: True
  • Spec fidelity: True
  • Ready for merge: True
  • Check ci/woodpecker/pr/validation: green https://ci.groupthink.asia/repos/73/pipeline/48/1
  • Checks: ci/woodpecker/pr/validation: green at df2a06a. No other required checks.
  • Warning: Host python3 lacks phonenumbers, so bare python3 -m pytest fails at conftest import; collection and tests were run through uv run instead, which worked.
  • Warning: Open Risk 1 (live effect after deploy) stays a post-merge verification via the #49 I02 soak.
  • Decision: review the PR and merge? (codex-svd ship --run-id 20261104T123646Z-0ae89b3b --confirm-merge)
spec-verify-dispatch Half B for dev/people-mind-mcp#81 snapshot-activation-scans-every-staged-row-per-mail-interact completed integration verify. - PR: https://git.groupthink.asia/dev/people-mind-mcp/pulls/84 - Branch: feat/svd-dev-people-mind-mcp-81-snapshot-activation-scans-eve-0ae89b3b - Commit: df2a06a92dac22f1ffaef1dc8520d5a10609edd8 - Build: none - Test: green - Reader audit clean: True - Spec fidelity: True - Ready for merge: True - Check ci/woodpecker/pr/validation: green https://ci.groupthink.asia/repos/73/pipeline/48/1 - Checks: ci/woodpecker/pr/validation: green at df2a06a. No other required checks. - Warning: Host python3 lacks phonenumbers, so bare `python3 -m pytest` fails at conftest import; collection and tests were run through `uv run` instead, which worked. - Warning: Open Risk 1 (live effect after deploy) stays a post-merge verification via the #49 I02 soak. - Decision: review the PR and merge? (`codex-svd ship --run-id 20261104T123646Z-0ae89b3b --confirm-merge`)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
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/people-mind-mcp#81
No description provided.