Keep a durable Mail peer event log, projection and epoch continuity #159
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#159
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?
Outcome
The sync writer keeps a durable, immutable v1 event log and a reference projection, proves continuity across restarts and restores, and trims history to a bounded floor. No peer tool is added. Unit 1 of the epic #152.
Decision substrate
ADR 0012 (rules 2–8, 11, 12), ADR 0010, the contract kit
contracts/mind-v1/(README andrecovery.md), and ADR 0008 for the envelope rules. The grill record is on #152.Scope and acceptance criteria
peer_events(seq INTEGER PRIMARY KEY AUTOINCREMENT; entity, kind, account,at, the authorised envelope for anadd, previous revision and time) andpeer_entities(entity, account, add sequence, current revision and time, envelope, deletion sequence).Store._emit(...)beside every existingchangesinsert, in the same transaction, at every mutation path: IMAP and stable-id adds, flags, deletes, stable-id moves,drop_mailbox, and the orphan and stable-id migrations. Inventory every writer and reader in the spec. A guard test fails on any statement that writesmessageswithout_emit.changesandmail_changesstay byte-identical.record_too_large, never truncated.peer_entitiesfrom current rows in one transaction at the first start of the new writer, records the base watermark, and keeps #151'ssource_idand epoch.<db>.peer-marker({source_id, epoch, max_seq}), written atomically after every event-emitting transaction; a failed write fails closed. At startup: a non-empty log with no marker, or a databasemax_seqbelow the marker's, starts a new epoch.peer_eventsrow, rebuildpeer_entitiesat a new base watermark, write the new epoch and a new HMAC cursor key inmeta, rewrite the marker.mailmind peer new-identitydoes the same with a newsource_id. Document both in the restore and clone runbook.[peer]config section:retention_seconds(default 604,800) andsnapshot_lease_seconds(default 3,600); the config check refuses retention shorter than twice the lease. The writer records the floor inmeta, then trims expiredpeer_eventsand tombstones in bounded batches after each sync pass.config.example.tomldocuments the section.make checkpasses. First-start migration time measured on the 210k-row benchmark store and reported in the PR.Non-goals
Any MCP tool (
mail_peer_info,mail_peer_changes_v1,mail_peer_snapshot_v1), any ADR 0008 tool-list change, People consumer changes, release or deploy.Blocked by
mainbefore Half-A.⏸ SVD architect gate: dev/mail-mind-mcp#159 keep-a-durable-mail-peer-event-log-projection-and-epoch-cont
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:
20261103T080321Z-d724f809Held because: CLEAN but 7 open risk(s) (2 verification · 5 decision) — HOLD-ALL parks everything. The risks, verbatim:
Open risks:
grep -n orphan srcreturns nothing. The spec maps it to drop_mailbox for a mailbox gone from the server (ingest.py:131-133), and the stable-id migration to retire_imap_rows (jmap.py:644). Question: did the #152 grill mean another path? [decision]mailmind peer new-identitytakes no lock against a running sync writer, so their marker writes can race; the outcome is an extra epoch rotation at the next start, which is the safe direction. The runbook says to stop sync first. Question: is documentation enough (lean), or should the CLI refuse while a writer is active? [decision]Change: 1. Schema v16 in store.py _TABLES. Add peer_events (seq AUTOINCREMENT, like wakes at store.py:192-203; an immutability trigger, like the actions triggers at store.py:181-188; a CHECK that only 'add' rows carry an envelope). Add peer_entities (WITHOUT ROWID, entity is the primary key under BINARY collation, a partial index on tombstones).
2. Store._emit(conn, kind, account, mailbox, uidvalidity, uid, source_id, at, headers), called right after each of the 8 INSERT INTO changes sites in the same _tx: upsert_messages:905, update_flags:984, delete_uids:1002, drop_mailbox:810, record_move:1500, update_source_flags:1567, delete_sources:1586, retire_imap_rows:1626. Only the SELECT/RETURNING lists that locate the row are widened (source_id, from_addr, to_addrs, cc_addrs). The changes SQL itself is never altered.
3. Entity is the same rule as ids.encode (ids.py:55-58), reached through a function-level import, because ids.py imports Store at module level.
4. The envelope comes from evidence.address and evidence.addresses filtered by the contract pattern ^[^\s@,]+@[^\s@,]+$. Records over the 320-char, 1,024-address or 32,768-byte caps are stored in full and flagged oversize=1, never truncated.
5. Marker hook inside _tx (store.py:731-742). The payload is read inside the transaction; after COMMIT it is written atomically (tmp file, fsync, os.replace, as in cli.py:264-269), and a write failure raises.
6. Startup goes between the identity transaction (store.py:638-641) and the schema_version publish (store.py:642-647). If peer_base_seq is missing, run _init_peer: one transaction that bumps sqlite_sequence to W = max_seq + 1 and builds the projection in fetchmany batches, keeping identity. Otherwise check continuity: rotate when the marker is missing or corrupt, its identity differs, or its max_seq is above the database's.
7. rotate_epoch and new_identity, plus a
mailmind peer new-identitysubparser following the keys block (cli.py:317-324).8. trim_peer: record a monotonic floor, then delete in batches of 1,000 (shape of _fill_utc_keys at store.py:667-684). It runs after each pass in SyncWorker._loop (source.py:445) and in run_once.
9. PeerConfig after SyncConfig (config.py:131-162), with a model_validator requiring retention >= 2 x lease.
10. A README "Restore and clone" runbook section, and a [peer] section in config.example.toml.
Files:
src/mail_mind_mcp/store.py·src/mail_mind_mcp/config.py·src/mail_mind_mcp/cli.py·src/mail_mind_mcp/source.py·config.example.toml·README.md·tests/test_store.py·tests/test_peer_evidence.py·tests/test_mailbox_search.py·tests/test_embed_scans.py·tests/test_copy_collapse.py·tests/test_recipients.py·tests/test_attachment_vectors.py·tests/test_attachments.py·tests/test_utc_dates.py·tests/test_evidence.pyDecision: 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-03-keep-a-durable-mail-peer-event-log-projection-and-epoch-cont.md— next:codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-03-keep-a-durable-mail-peer-event-log-projection-and-epoch-cont.mdspec-verify-dispatch Half B for dev/mail-mind-mcp#159 keep-a-durable-mail-peer-event-log-projection-and-epoch-cont completed integration verify.
73b8414ec3codex-svd ship --run-id 20261103T083717Z-b585edf8 --confirm-merge)