Serve the Mail peer snapshot and its feed handoff #161

Closed
opened 2026-11-03 07:27:22 +00:00 by svc-agent-writer · 2 comments

Outcome

A new peer can bootstrap from a consistent snapshot of Mail at watermark H and continue on the feed with no gap. Unit 3 of the epic #152; it completes the Mail provider claims P01–P09.

Decision substrate

ADR 0012 (rules 5, 10, 11, 13, 14), ADR 0010, the contract kit contracts/mind-v1/ (README "Snapshot and handoff", schema.json, fixtures, recovery.md), and ADR 0008. The grill record is on #152.

Scope and acceptance criteria

  1. mail_peer_snapshot_v1 with the exact negotiated schemas, evidence scope and the existing rate-limit bucket. The first call (snapshot_cursor: null) captures H and returns snapshot_id, snapshot_at, through=H, expires_at (snapshot_at plus snapshot_lease_seconds), complete, next_snapshot_cursor and resume_cursor.
  2. Read-only reconstruction (ADR 0012 rule 5): no snapshot copy, no API write, each page a short keyset read ordered by entity_id under BINARY collation. Rows carry identity, revision at H, mutation time and envelope; no row has a revision above H.
  3. The final page has complete=true, next_snapshot_cursor=null and a feed resume_cursor immediately after H; an empty snapshot is an explicit final page. Page retries are byte-identical. An expired lease returns snapshot_expired; every page is reauthorised; a cursor from another scope returns invalid_cursor.
  4. mail_peer_info advertises snapshot only after the cases below pass. ADR 0008 tool-list amendment.
  5. P06 (mutations between every page and before activation: a stable projection at H; every later mutation, including deletions, arrives after resume_cursor) and P07 (lease expiry and token revocation mid-pagination; no further rows). Check the ADR 0012 assumption that an entity_id is never reused after deletion, for IMAP and JMAP ids.
  6. mail_peer_evidence_v1's watermark through reads the peer_events high-water mark, not MAX(seq) from the legacy changes table (store.py, the #151 evidence query). The contract has People hold invalidations until the feed catches up, which compares the evidence watermark with the feed through, so both must be the same counter. Ruled at the #160 gate, 2026-11-03.
  7. I02 on the 210k-row synthetic store with budgets declared in ADR 0012 rule 14: health p99 at most 100 ms with no failures, mail_search p95 at most 25 % above idle, snapshot and feed page p99 at most 250 ms, RSS growth at most 64 MiB. make check passes.

Non-goals

People consumer changes, pair acceptance (I01–I04), release or deploy, a broker.

Blocked by

  • dev/mail-mind-mcp#160 — the feed the handoff resumes on.
  • dev/mail-mind-mcp#167 — the feed must meet the I02 search budget first; the snapshot pages run under the same budget (ruled at the #160 gate, 2026-11-03).
## Outcome A new peer can bootstrap from a consistent snapshot of Mail at watermark H and continue on the feed with no gap. Unit 3 of the epic #152; it completes the Mail provider claims P01–P09. ## Decision substrate ADR 0012 (rules 5, 10, 11, 13, 14), ADR 0010, the contract kit `contracts/mind-v1/` (README "Snapshot and handoff", `schema.json`, fixtures, `recovery.md`), and ADR 0008. The grill record is on #152. ## Scope and acceptance criteria 1. `mail_peer_snapshot_v1` with the exact negotiated schemas, evidence scope and the existing rate-limit bucket. The first call (`snapshot_cursor: null`) captures H and returns `snapshot_id`, `snapshot_at`, `through=H`, `expires_at` (`snapshot_at` plus `snapshot_lease_seconds`), `complete`, `next_snapshot_cursor` and `resume_cursor`. 2. Read-only reconstruction (ADR 0012 rule 5): no snapshot copy, no API write, each page a short keyset read ordered by `entity_id` under BINARY collation. Rows carry identity, revision at H, mutation time and envelope; no row has a revision above H. 3. The final page has `complete=true`, `next_snapshot_cursor=null` and a feed `resume_cursor` immediately after H; an empty snapshot is an explicit final page. Page retries are byte-identical. An expired lease returns `snapshot_expired`; every page is reauthorised; a cursor from another scope returns `invalid_cursor`. 4. `mail_peer_info` advertises `snapshot` only after the cases below pass. ADR 0008 tool-list amendment. 5. P06 (mutations between every page and before activation: a stable projection at H; every later mutation, including deletions, arrives after `resume_cursor`) and P07 (lease expiry and token revocation mid-pagination; no further rows). Check the ADR 0012 assumption that an `entity_id` is never reused after deletion, for IMAP and JMAP ids. 6. `mail_peer_evidence_v1`'s watermark `through` reads the `peer_events` high-water mark, not `MAX(seq)` from the legacy `changes` table (`store.py`, the #151 evidence query). The contract has People hold invalidations until the feed catches up, which compares the evidence watermark with the feed `through`, so both must be the same counter. Ruled at the #160 gate, 2026-11-03. 7. I02 on the 210k-row synthetic store with budgets declared in ADR 0012 rule 14: health p99 at most 100 ms with no failures, `mail_search` p95 at most 25 % above idle, snapshot and feed page p99 at most 250 ms, RSS growth at most 64 MiB. `make check` passes. ## Non-goals People consumer changes, pair acceptance (I01–I04), release or deploy, a broker. ## Blocked by - dev/mail-mind-mcp#160 — the feed the handoff resumes on. - dev/mail-mind-mcp#167 — the feed must meet the I02 search budget first; the snapshot pages run under the same budget (ruled at the #160 gate, 2026-11-03).
Collaborator

⏸ SVD architect gate: dev/mail-mind-mcp#161 serve-the-mail-peer-snapshot-and-its-feed-handoff

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 effort high

Run: 20261103T115256Z-50d1adc3

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

Open risks:

  1. Is freezing freshness at capture acceptable? The issue requires byte-identical page retries, but live freshness.generated_at changes on every call. The lean seals the captured last_successful_sync_at and state into the snapshot cursor and serves every page with generated_at = snapshot_at, which satisfies conformance.py:113-115. Architect: accept frozen freshness, or relax criterion 3 to 'byte-identical except freshness' with live per-page freshness? [decision]
  2. Is the reuse handling sufficient? ADR 0012's assumption that an entity_id is never reused after deletion is false in today's code. JMAP: _refresh puts an email that leaves every wanted mailbox into delete_sources, then re-adds it via _add with the same source_id (jmap.py:663-692). IMAP: drop_mailbox runs on wanted-set exit, and the mailbox returns with the same UIDVALIDITY and UIDs (store.py:1125-1153, ingest.py:131-134). The lean keeps rule 5. It argues the projection envelope is still correct because a reused id names the same immutable message (an IMAP UID under one UIDVALIDITY, or a JMAP Email id with immutable headers). It amends ADR 0012's Consequences and adds JMAP and IMAP reuse tests. Architect: accept that, or require defensive handling, such as refusing or flagging rows whose projection add_seq > H while they existed at H? [decision]
  3. May the I02 bench layout change? The two-slot Jobs pool means a third concurrent peer caller measures busy admissions, not page latency. The lean replaces #160's two independent feed pollers with one snapshot pager plus one feed poller that alternates the all and single scopes; any busy fails the run. Architect: accept this layout, or keep three callers with busy counted and budgeted separately (ADR 0012 rule 14 declares no busy budget)? [decision]
  4. I02 cannot run in the sandbox or a dispatch worktree. After dispatch the architect runs uv run python3 scripts/bench_feed.py three times on host voyager at the PR head. Each report must show passes = {feed, health, rss, search, snapshot} all true, snapshot p99 <= 250 ms, and loaded/idle search p95 <= 1.25. Record the lines in ADR 0008 'Local implementation observations (#161)' and the PR. A failure blocks advertising snapshot. [verification]
  5. Long runs of skipped rows are bounded only by Budget (DATABASE_SECONDS 10 s). Conformance forbids an empty nonfinal page, so the scan cannot be capped by row count. Tombstones at or below H are skipped in SQL; post-H adds and deletions are filtered in Python. The unit test covers 5,000 tombstones. Check on a large live store: a page after a 100k+ message deletion inside the lease returns within 250 ms, or as busy, and never hangs. [verification]
  6. The query plans were probed only on local SQLite 3.53.3. The new plan test (peer_events_entity used, no TEMP B-TREE, for single and multi-account scopes) must pass in Woodpecker make check on python:3.12-slim-bookworm's SQLite. If it fails there, the fallback is the unary-plus +account IN (...) hint, decided at the PR. [verification]

Change: Store.peer_snapshot copies the skeleton of Store.peer_changes (store.py:2697-2800): a dedicated read-only connection, BEGIN…ROLLBACK, identity, key and floor validation, and closed error mapping. It then does a keyset scan of peer_entities ordered by entity (BINARY): account = ? or account IN (...), entity > after, and tombstones at or below H filtered in SQL. Each row follows ADR 0012 rule 5. A row with revision ≤ H is served from the projection. A row with revision > H is decided by its first peer_events row after H: an add means the entity did not exist at H; any other kind means it existed, at prev_revision and prev_at. That lookup is served by a new idempotent index, peer_events_entity (entity, seq), added in _SCHEMA after store.py:241; schema version stays 16 (DD-271, mirroring #160's peer_events_account). peer.render_snapshot mirrors render_changes (peer.py:595-648): the same byte cut, record_too_large at the head, a resume_cursor sealed as a changes cursor at H, and next_snapshot_cursor from a new seal_snapshot_cursor/open_snapshot_cursor pair. The pair shares a factored MAC-before-parse helper with open_cursor, whose behaviour stays unchanged. The cursor carries snapshot_id, H, snapshot_at, expires_at, the last entity and the captured freshness, so retries are byte-identical. server.mail_peer_snapshot_v1 and PeerSnapshotTool mirror mail_peer_changes_v1 and PeerChangesTool (server.py:1040-1042, 1134-1177, 1198-1205). store.py:2860 switches the evidence watermark to the sqlite_sequence peer_events high-water mark (the same statement as :2756). OPERATIONS becomes ("changes","snapshot","evidence") last, once the P06 and P07 tests pass. bench_feed.py gains a snapshot pager leg. ADR 0008 gains §10; ADR 0012, README, SECURITY.md and CLAUDE.md list the tool. One finding goes into ADR 0012: entity_id reuse after deletion is reachable (JMAP _refresh→delete_sources→_add, and IMAP drop_mailbox and re-add). Rule 5 stays correct because a reused id names the same immutable message.

Files: src/mail_mind_mcp/peer.py · src/mail_mind_mcp/store.py · src/mail_mind_mcp/server.py · tests/test_peer_snapshot.py · tests/test_peer_changes.py · tests/test_peer_evidence.py · tests/test_evidence.py · tests/test_server.py · scripts/bench_feed.py · docs/adr/0008-evidence-scope.md · docs/adr/0012-mail-peer-feed-implementation.md · README.md · SECURITY.md · CLAUDE.md

Decision: accept the 6 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-03-serve-the-mail-peer-snapshot-and-its-feed-handoff.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-03-serve-the-mail-peer-snapshot-and-its-feed-handoff.md

### ⏸ SVD architect gate: dev/mail-mind-mcp#161 serve-the-mail-peer-snapshot-and-its-feed-handoff **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 effort `high` **Run:** `20261103T115256Z-50d1adc3` **Held because:** CLEAN but 6 open risk(s) (3 verification · 3 decision) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. Is freezing freshness at capture acceptable? The issue requires byte-identical page retries, but live freshness.generated_at changes on every call. The lean seals the captured last_successful_sync_at and state into the snapshot cursor and serves every page with generated_at = snapshot_at, which satisfies conformance.py:113-115. Architect: accept frozen freshness, or relax criterion 3 to 'byte-identical except freshness' with live per-page freshness? [decision] 2. Is the reuse handling sufficient? ADR 0012's assumption that an entity_id is never reused after deletion is false in today's code. JMAP: _refresh puts an email that leaves every wanted mailbox into delete_sources, then re-adds it via _add with the same source_id (jmap.py:663-692). IMAP: drop_mailbox runs on wanted-set exit, and the mailbox returns with the same UIDVALIDITY and UIDs (store.py:1125-1153, ingest.py:131-134). The lean keeps rule 5. It argues the projection envelope is still correct because a reused id names the same immutable message (an IMAP UID under one UIDVALIDITY, or a JMAP Email id with immutable headers). It amends ADR 0012's Consequences and adds JMAP and IMAP reuse tests. Architect: accept that, or require defensive handling, such as refusing or flagging rows whose projection add_seq > H while they existed at H? [decision] 3. May the I02 bench layout change? The two-slot Jobs pool means a third concurrent peer caller measures busy admissions, not page latency. The lean replaces #160's two independent feed pollers with one snapshot pager plus one feed poller that alternates the all and single scopes; any busy fails the run. Architect: accept this layout, or keep three callers with busy counted and budgeted separately (ADR 0012 rule 14 declares no busy budget)? [decision] 4. I02 cannot run in the sandbox or a dispatch worktree. After dispatch the architect runs `uv run python3 scripts/bench_feed.py` three times on host voyager at the PR head. Each report must show passes = {feed, health, rss, search, snapshot} all true, snapshot p99 <= 250 ms, and loaded/idle search p95 <= 1.25. Record the lines in ADR 0008 'Local implementation observations (#161)' and the PR. A failure blocks advertising snapshot. [verification] 5. Long runs of skipped rows are bounded only by Budget (DATABASE_SECONDS 10 s). Conformance forbids an empty nonfinal page, so the scan cannot be capped by row count. Tombstones at or below H are skipped in SQL; post-H adds and deletions are filtered in Python. The unit test covers 5,000 tombstones. Check on a large live store: a page after a 100k+ message deletion inside the lease returns within 250 ms, or as busy, and never hangs. [verification] 6. The query plans were probed only on local SQLite 3.53.3. The new plan test (peer_events_entity used, no TEMP B-TREE, for single and multi-account scopes) must pass in Woodpecker make check on python:3.12-slim-bookworm's SQLite. If it fails there, the fallback is the unary-plus +account IN (...) hint, decided at the PR. [verification] **Change:** Store.peer_snapshot copies the skeleton of Store.peer_changes (store.py:2697-2800): a dedicated read-only connection, BEGIN…ROLLBACK, identity, key and floor validation, and closed error mapping. It then does a keyset scan of peer_entities ordered by entity (BINARY): `account = ?` or `account IN (...)`, `entity > after`, and tombstones at or below H filtered in SQL. Each row follows ADR 0012 rule 5. A row with revision ≤ H is served from the projection. A row with revision > H is decided by its first peer_events row after H: an add means the entity did not exist at H; any other kind means it existed, at prev_revision and prev_at. That lookup is served by a new idempotent index, peer_events_entity (entity, seq), added in _SCHEMA after store.py:241; schema version stays 16 (DD-271, mirroring #160's peer_events_account). peer.render_snapshot mirrors render_changes (peer.py:595-648): the same byte cut, record_too_large at the head, a resume_cursor sealed as a changes cursor at H, and next_snapshot_cursor from a new seal_snapshot_cursor/open_snapshot_cursor pair. The pair shares a factored MAC-before-parse helper with open_cursor, whose behaviour stays unchanged. The cursor carries snapshot_id, H, snapshot_at, expires_at, the last entity and the captured freshness, so retries are byte-identical. server.mail_peer_snapshot_v1 and PeerSnapshotTool mirror mail_peer_changes_v1 and PeerChangesTool (server.py:1040-1042, 1134-1177, 1198-1205). store.py:2860 switches the evidence watermark to the sqlite_sequence peer_events high-water mark (the same statement as :2756). OPERATIONS becomes ("changes","snapshot","evidence") last, once the P06 and P07 tests pass. bench_feed.py gains a snapshot pager leg. ADR 0008 gains §10; ADR 0012, README, SECURITY.md and CLAUDE.md list the tool. One finding goes into ADR 0012: entity_id reuse after deletion is reachable (JMAP _refresh→delete_sources→_add, and IMAP drop_mailbox and re-add). Rule 5 stays correct because a reused id names the same immutable message. **Files:** `src/mail_mind_mcp/peer.py` · `src/mail_mind_mcp/store.py` · `src/mail_mind_mcp/server.py` · `tests/test_peer_snapshot.py` · `tests/test_peer_changes.py` · `tests/test_peer_evidence.py` · `tests/test_evidence.py` · `tests/test_server.py` · `scripts/bench_feed.py` · `docs/adr/0008-evidence-scope.md` · `docs/adr/0012-mail-peer-feed-implementation.md` · `README.md` · `SECURITY.md` · `CLAUDE.md` **Decision:** accept the 6 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-03-serve-the-mail-peer-snapshot-and-its-feed-handoff.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-03-serve-the-mail-peer-snapshot-and-its-feed-handoff.md`</sub>
Collaborator

spec-verify-dispatch Half B for dev/mail-mind-mcp#161 serve-the-mail-peer-snapshot-and-its-feed-handoff completed integration verify.

  • PR: #169
  • Branch: feat/svd-dev-mail-mind-mcp-161-serve-the-mail-peer-snapshot-a-3ff10f55
  • Commit: cc56ba7139
  • 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/72/pipeline/31/1
  • Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll
  • Warning: I02 (bench_feed.py, three voyager runs) was not run, as specified. It remains the architect's gate before merge, and ADR 0008 keeps the placeholder.
  • Warning: The spec has no Scenario table. Some planned test names differ slightly from the spec: test_snapshot_shares_http_bucket_and_two_jobs is split into test_snapshot_shares_http_bucket and test_snapshot_shares_two_jobs. test_entity_reuse_after_deletion_{jmap,imap} is one parametrized test with [jmap] and [imap]. Coverage is equivalent.
  • Warning: The PR body ends with 'Co-Authored-By: gpt-6.1-sol'. This is non-blocking.
  • Warning: Woodpecker validation is still pending. The runner should re-poll it.
  • CI wait: ci/woodpecker/pr/validation -> pass
  • Decision: review the PR and merge? (codex-svd ship --run-id 20261103T185336Z-3ff10f55 --confirm-merge)
spec-verify-dispatch Half B for dev/mail-mind-mcp#161 serve-the-mail-peer-snapshot-and-its-feed-handoff completed integration verify. - PR: https://git.groupthink.asia/dev/mail-mind-mcp/pulls/169 - Branch: feat/svd-dev-mail-mind-mcp-161-serve-the-mail-peer-snapshot-a-3ff10f55 - Commit: cc56ba713960203eda9c3f81e9a4cd07a631bb48 - 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/72/pipeline/31/1 - Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll - Warning: I02 (bench_feed.py, three voyager runs) was not run, as specified. It remains the architect's gate before merge, and ADR 0008 keeps the placeholder. - Warning: The spec has no Scenario table. Some planned test names differ slightly from the spec: test_snapshot_shares_http_bucket_and_two_jobs is split into test_snapshot_shares_http_bucket and test_snapshot_shares_two_jobs. test_entity_reuse_after_deletion_{jmap,imap} is one parametrized test with [jmap] and [imap]. Coverage is equivalent. - Warning: The PR body ends with 'Co-Authored-By: gpt-6.1-sol'. This is non-blocking. - Warning: Woodpecker validation is still pending. The runner should re-poll it. - CI wait: ci/woodpecker/pr/validation -> pass - Decision: review the PR and merge? (`codex-svd ship --run-id 20261103T185336Z-3ff10f55 --confirm-merge`)
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
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/mail-mind-mcp#161
No description provided.