Sync: watermarks, Backfill, Edits and Unsends against the fake Feeder #12

Closed
opened 2026-11-12 20:18:15 +00:00 by svc-agent-writer · 3 comments

Parent: #6. Records: ADR 0001 (docs/adr/0001-service-and-read-only-scope.md), ADR 0004 (docs/adr/0004-source-mac-and-feeder-network-face.md), ADR 0006 (docs/adr/0006-retention-deletion-and-forget.md).

What to build

The sync worker. It is the only client of the Feeder and the only writer of the Mirror.

  • A Source seam. sync reads through a read-only Source interface, so a second Source kind can be added later with no change to the store or the tools.
  • Two integers. A high position reads new rows in ascending row id. A low position runs the Backfill in descending row id. That order is the order of arrival on the Source Mac and not date order, so no code may assume that a lower row id is an older message.
  • Changed rows. A third position reads rows by edit time. An Edit replaces the text and its index entry; the Mirror keeps only the latest text. An Unsend deletes the text and its index entry and keeps the empty row.
  • Shape. A Tapback is stored against the message it reacts to. A reply keeps its target. An attachment is stored as type and size only.
  • One event log. Every added, changed and removed row writes one event.
  • Run modes. A loop with a poll interval, and a single pass for tests and operators. A sync health endpoint. The data for messages_status: last sync, newest message time and Backfill progress.
  • Token. The Feeder token is read from a file, sent in a header and never logged.

Acceptance criteria

  • Against the fake Feeder, a Backfill followed by a live run mirrors every fixture row exactly once.
  • A position advances in the same transaction as the rows it covers. A test kills a pass midway and proves that the next pass neither skips nor repeats a row.
  • Tests cover: an Edit, an Unsend, a Tapback added and removed, a row inserted with an old date and a new row id, and a Chat that holds both Transports.
  • A test proves that the text of an unsent message is gone from the store and from the full-text index.
  • When the Feeder is unreachable, sync keeps its positions, reports the failure on the health endpoint and the api keeps serving.

Blocked by

Filed under: operator word "go" sha256:4cd0e21a9a07

Parent: #6. Records: ADR 0001 (`docs/adr/0001-service-and-read-only-scope.md`), ADR 0004 (`docs/adr/0004-source-mac-and-feeder-network-face.md`), ADR 0006 (`docs/adr/0006-retention-deletion-and-forget.md`). ## What to build The `sync` worker. It is the only client of the Feeder and the only writer of the Mirror. - **A Source seam.** `sync` reads through a read-only Source interface, so a second Source kind can be added later with no change to the store or the tools. - **Two integers.** A high position reads new rows in ascending row id. A low position runs the Backfill in descending row id. That order is the order of arrival on the Source Mac and not date order, so no code may assume that a lower row id is an older message. - **Changed rows.** A third position reads rows by edit time. An Edit replaces the text and its index entry; the Mirror keeps only the latest text. An Unsend deletes the text and its index entry and keeps the empty row. - **Shape.** A Tapback is stored against the message it reacts to. A reply keeps its target. An attachment is stored as type and size only. - **One event log.** Every added, changed and removed row writes one event. - **Run modes.** A loop with a poll interval, and a single pass for tests and operators. A sync health endpoint. The data for `messages_status`: last sync, newest message time and Backfill progress. - **Token.** The Feeder token is read from a file, sent in a header and never logged. ## Acceptance criteria - Against the fake Feeder, a Backfill followed by a live run mirrors every fixture row exactly once. - A position advances in the same transaction as the rows it covers. A test kills a pass midway and proves that the next pass neither skips nor repeats a row. - Tests cover: an Edit, an Unsend, a Tapback added and removed, a row inserted with an old date and a new row id, and a Chat that holds both Transports. - A test proves that the text of an unsent message is gone from the store and from the full-text index. - When the Feeder is unreachable, `sync` keeps its positions, reports the failure on the health endpoint and the api keeps serving. ## Blocked by - #8 Filed under: operator word "go" sha256:4cd0e21a9a07
Author
Member

Two facts from the slices that are now built. Both bind this slice.

The Mirror needs three additions to hold what feeder-v1 serves (pull request #29).

  • A reply target for a message: reply_to, a guid or null.
  • The attachments of a message: a type and a size for each.
  • The action of a Tapback: add or remove.

The store of #7 has no column for them. The section "What sync must do" of contracts/feeder-v1/README.md lists the duties of this slice. One of them needs a lookup: a hard-deletion record holds guid_hash, the SHA-256 of a guid, so sync matches it by the hash of each guid that the Mirror holds.

GET /healthz/sync already exists on the api (#17, merged).

  • It reads the record last_sync_at (store.SYNC_LAST_AT).
  • It answers ok when a sync ended in the last 900 seconds (server.SYNC_STALE_SECONDS), and 503 otherwise.
  • No sync writes that record yet, so it answers 503 today.

This slice writes last_sync_at at the end of each pass. Its poll interval must fit the 900 s limit, or this slice changes the limit and states why.

Two facts from the slices that are now built. Both bind this slice. **The Mirror needs three additions to hold what `feeder-v1` serves** (pull request #29). - A reply target for a message: `reply_to`, a `guid` or `null`. - The attachments of a message: a type and a size for each. - The action of a Tapback: `add` or `remove`. The store of #7 has no column for them. The section "What `sync` must do" of `contracts/feeder-v1/README.md` lists the duties of this slice. One of them needs a lookup: a hard-deletion record holds `guid_hash`, the SHA-256 of a `guid`, so `sync` matches it by the hash of each `guid` that the Mirror holds. **`GET /healthz/sync` already exists on the api** (#17, merged). - It reads the record `last_sync_at` (`store.SYNC_LAST_AT`). - It answers `ok` when a sync ended in the last 900 seconds (`server.SYNC_STALE_SECONDS`), and `503` otherwise. - No sync writes that record yet, so it answers `503` today. This slice writes `last_sync_at` at the end of each pass. Its poll interval must fit the 900 s limit, or this slice changes the limit and states why.
Collaborator

⏸ SVD architect gate: dev/messages-mind-mcp#12 sync-watermarks-backfill-edits-and-unsends-against-the-fake

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: 20261113T050330Z-1c4188af

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

Open risks:

  1. The binding comment says the contract's 'What sync must do' list (contracts/feeder-v1/README.md:270-280) binds this slice, including hard-deletion guid_hash matching. The spec includes the token from a file, whole-response refusal, the smaller-limit retry, never logging, the position stored in the same transaction, row-id order and the source-replaced stop. It excludes Recently Deleted, hard deletions with guid_hash matching, Reconcile (both count-triggered and daily) and deny-list removal with del events, because adding them would likely pass the 16-file ceiling. Question: should these go to a sibling slice under #6, or should #12 be re-sliced to include them? No sibling issue ref could be verified this session. [decision]
  2. Event envelopes are written as NULL. ADR 0010 says 'to' is the membership at the time the record was written to the Mirror, which implies the envelope is computed when the add event is written. Question: should this slice write the ADR 0010 envelope on each add event now, or leave it to the peer-producer slice and accept that earlier events carry none? [decision]
  3. Tapback rows write no event, because events.message_id is NOT NULL and the event kinds are message kinds (store.py:59-62). The issue says 'every added, changed and removed row writes one event'. Question: does 'row' mean a message row (the current approach), or must Tapbacks also write events, which would need an events schema change in migration 2? [decision]
  4. The Feeder client uses stdlib http.client rather than httpx. httpx is only a dev dependency or a transitive one through fastmcp, and CI runs with UV_LOCKED=1. Declaring httpx as a runtime dependency to mirror conformance.py would add pyproject.toml and uv.lock to the written set (16 files, still in scope). Question: accept stdlib, or declare httpx>=0.27,<1? [decision]
  5. After merge, an api running against an existing v1 store answers 'Error: code=store-schema-behind' until the first sync opens the writer and migrates it. Post-merge check, on a copy of a v1 store: run 'messagesmind sync --config C --once' against a fake or real Feeder, then 'messagesmind check --config C'. Expect 'store=ok schema=2', and expect messages_status to return a status line rather than an error code. [verification]
  6. /healthz/sync answers 503 straight after a failed pass, even if a successful pass ended less than 900 s ago. The alternative is to report only through staleness, which takes up to 15 minutes. Question: is the immediate 503 the intended meaning of 'reports the failure on the health endpoint'? Either way, README line 26 changes. [decision]
  7. The Tombstone skip is included because ADR 0006 binds the Backfill ('Backfill, Reconcile and a rebuild skip a message that matches a Tombstone'), although the issue does not list it. Forget is not built yet, so the Tombstone store is empty in practice. Question: keep it in this slice (one test, no extra files) or defer it to the Forget slice? [decision]

Change: Six changes:

  1. Migration 2 in store.py. Append a second tuple to MIGRATIONS (store.py:31-78) and set SCHEMA_VERSION=2:
    • messages.reply_to;
    • a STRICT attachments table (message_id FK ON DELETE CASCADE, position, type, size);
    • tapbacks.action, NOT NULL, DEFAULT 'add', with a CHECK.
      Add a SYNC_FAILED_AT key next to SYNC_LAST_AT (store.py:91-95). The existing open_writer loop (store.py:198-231) migrates v1 stores forward in place.
  2. New source.py. A read-only Source Protocol with five methods: check, new_rows, backfill, changed, chats. Each returns a wire model.
    • FeederSource is a stdlib http.client client over TCP or a Unix socket. It mirrors conformance.Feeder.send (conformance.py:116-185): Bearer header only, a read cap of wire.MAX_RESPONSE_BYTES, and model_validate_json that refuses a bad response whole.
    • Errors carry coded SourceError values only.
  3. New sync.py.
    • run_pass: check (stop on schema-pin or source-replaced); seed the positions on the first pass; read the whole Chats list; then new rows, Backfill (capped at 20 pages per pass) and changed rows. Each page is applied with its position in one store.transaction. The pass ends by writing last_sync_at. A failure rolls back and writes last_failure_at.
    • A single apply_row upserts by guid: insert with one 'add' event; when edited_at is newer, apply an Edit or Unsend with one 'edit' or 'unsend' event (the FTS secure-delete triggers already exist); store Tapbacks with ON CONFLICT DO NOTHING; skip any row that matches a Tombstone.
    • sync_once and sync_loop print counts and codes only.
  4. Config and CLI. config.py gets an optional [feeder] table: url or socket, token_file, poll_seconds between 5 and 300 s. startup.Secrets gets feeder_token. cli.py gets a 'sync [--once]' verb that imports sync lazily, as cli.py:40-41 does for the api.
  5. Readers. views.TAPBACKS (views.py:56-59) hides an add that a later remove (higher source_rowid) cancels. A new views.last_failure feeds server.synced_recently (server.py:283-292), keeping its lambda lookup of views.last_sync.
  6. Docs. Update README.md, SECURITY.md and config.example.toml.

Deletions, Reconcile and event envelopes are out of scope.

Files: src/messages_mind_mcp/store.py · src/messages_mind_mcp/source.py · src/messages_mind_mcp/sync.py · src/messages_mind_mcp/config.py · src/messages_mind_mcp/startup.py · src/messages_mind_mcp/cli.py · src/messages_mind_mcp/server.py · src/messages_mind_mcp/views.py · config.example.toml · README.md · SECURITY.md · tests/test_sync.py · tests/test_store.py · tests/test_config_cli.py

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

  • 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-13-sync-watermarks-backfill-edits-and-unsends-against-the-fake.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-13-sync-watermarks-backfill-edits-and-unsends-against-the-fake.md

### ⏸ SVD architect gate: dev/messages-mind-mcp#12 sync-watermarks-backfill-edits-and-unsends-against-the-fake **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:** `20261113T050330Z-1c4188af` **Held because:** CLEAN but 7 open risk(s) (1 verification · 6 decision) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. The binding comment says the contract's 'What sync must do' list (contracts/feeder-v1/README.md:270-280) binds this slice, including hard-deletion guid_hash matching. The spec includes the token from a file, whole-response refusal, the smaller-limit retry, never logging, the position stored in the same transaction, row-id order and the source-replaced stop. It excludes Recently Deleted, hard deletions with guid_hash matching, Reconcile (both count-triggered and daily) and deny-list removal with del events, because adding them would likely pass the 16-file ceiling. Question: should these go to a sibling slice under #6, or should #12 be re-sliced to include them? No sibling issue ref could be verified this session. [decision] 2. Event envelopes are written as NULL. ADR 0010 says 'to' is the membership at the time the record was written to the Mirror, which implies the envelope is computed when the add event is written. Question: should this slice write the ADR 0010 envelope on each add event now, or leave it to the peer-producer slice and accept that earlier events carry none? [decision] 3. Tapback rows write no event, because events.message_id is NOT NULL and the event kinds are message kinds (store.py:59-62). The issue says 'every added, changed and removed row writes one event'. Question: does 'row' mean a message row (the current approach), or must Tapbacks also write events, which would need an events schema change in migration 2? [decision] 4. The Feeder client uses stdlib http.client rather than httpx. httpx is only a dev dependency or a transitive one through fastmcp, and CI runs with UV_LOCKED=1. Declaring httpx as a runtime dependency to mirror conformance.py would add pyproject.toml and uv.lock to the written set (16 files, still in scope). Question: accept stdlib, or declare httpx>=0.27,<1? [decision] 5. After merge, an api running against an existing v1 store answers 'Error: code=store-schema-behind' until the first sync opens the writer and migrates it. Post-merge check, on a copy of a v1 store: run 'messagesmind sync --config C --once' against a fake or real Feeder, then 'messagesmind check --config C'. Expect 'store=ok schema=2', and expect messages_status to return a status line rather than an error code. [verification] 6. /healthz/sync answers 503 straight after a failed pass, even if a successful pass ended less than 900 s ago. The alternative is to report only through staleness, which takes up to 15 minutes. Question: is the immediate 503 the intended meaning of 'reports the failure on the health endpoint'? Either way, README line 26 changes. [decision] 7. The Tombstone skip is included because ADR 0006 binds the Backfill ('Backfill, Reconcile and a rebuild skip a message that matches a Tombstone'), although the issue does not list it. Forget is not built yet, so the Tombstone store is empty in practice. Question: keep it in this slice (one test, no extra files) or defer it to the Forget slice? [decision] **Change:** Six changes: 1. **Migration 2 in store.py.** Append a second tuple to MIGRATIONS (store.py:31-78) and set SCHEMA_VERSION=2: - messages.reply_to; - a STRICT attachments table (message_id FK ON DELETE CASCADE, position, type, size); - tapbacks.action, NOT NULL, DEFAULT 'add', with a CHECK. Add a SYNC_FAILED_AT key next to SYNC_LAST_AT (store.py:91-95). The existing open_writer loop (store.py:198-231) migrates v1 stores forward in place. 2. **New source.py.** A read-only Source Protocol with five methods: check, new_rows, backfill, changed, chats. Each returns a wire model. - FeederSource is a stdlib http.client client over TCP or a Unix socket. It mirrors conformance.Feeder.send (conformance.py:116-185): Bearer header only, a read cap of wire.MAX_RESPONSE_BYTES, and model_validate_json that refuses a bad response whole. - Errors carry coded SourceError values only. 3. **New sync.py.** - run_pass: check (stop on schema-pin or source-replaced); seed the positions on the first pass; read the whole Chats list; then new rows, Backfill (capped at 20 pages per pass) and changed rows. Each page is applied with its position in one store.transaction. The pass ends by writing last_sync_at. A failure rolls back and writes last_failure_at. - A single apply_row upserts by guid: insert with one 'add' event; when edited_at is newer, apply an Edit or Unsend with one 'edit' or 'unsend' event (the FTS secure-delete triggers already exist); store Tapbacks with ON CONFLICT DO NOTHING; skip any row that matches a Tombstone. - sync_once and sync_loop print counts and codes only. 4. **Config and CLI.** config.py gets an optional [feeder] table: url or socket, token_file, poll_seconds between 5 and 300 s. startup.Secrets gets feeder_token. cli.py gets a 'sync [--once]' verb that imports sync lazily, as cli.py:40-41 does for the api. 5. **Readers.** views.TAPBACKS (views.py:56-59) hides an add that a later remove (higher source_rowid) cancels. A new views.last_failure feeds server.synced_recently (server.py:283-292), keeping its lambda lookup of views.last_sync. 6. **Docs.** Update README.md, SECURITY.md and config.example.toml. Deletions, Reconcile and event envelopes are out of scope. **Files:** `src/messages_mind_mcp/store.py` · `src/messages_mind_mcp/source.py` · `src/messages_mind_mcp/sync.py` · `src/messages_mind_mcp/config.py` · `src/messages_mind_mcp/startup.py` · `src/messages_mind_mcp/cli.py` · `src/messages_mind_mcp/server.py` · `src/messages_mind_mcp/views.py` · `config.example.toml` · `README.md` · `SECURITY.md` · `tests/test_sync.py` · `tests/test_store.py` · `tests/test_config_cli.py` **Decision:** 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`): - 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-13-sync-watermarks-backfill-edits-and-unsends-against-the-fake.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-13-sync-watermarks-backfill-edits-and-unsends-against-the-fake.md`</sub>
Collaborator

spec-verify-dispatch Half B for dev/messages-mind-mcp#12 sync-watermarks-backfill-edits-and-unsends-against-the-fake completed integration verify.

  • PR: #30
  • Branch: feat/svd-dev-messages-mind-mcp-12-sync-watermarks-backfill-ed-adcfca98
  • Commit: 9a43a9ec7e
  • 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/75/pipeline/10/1
  • Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll
  • Warning: Remote CI ci/woodpecker/pr/validation still running at verify time; the runner should re-poll.
  • Warning: The subagent did not measure a pristine-base make check baseline; the final run is green, so this is informational.
  • CI wait: ci/woodpecker/pr/validation -> pass
  • Decision: review the PR and merge? (codex-svd ship --run-id 20261113T053602Z-adcfca98 --confirm-merge)
spec-verify-dispatch Half B for dev/messages-mind-mcp#12 sync-watermarks-backfill-edits-and-unsends-against-the-fake completed integration verify. - PR: https://git.groupthink.asia/dev/messages-mind-mcp/pulls/30 - Branch: feat/svd-dev-messages-mind-mcp-12-sync-watermarks-backfill-ed-adcfca98 - Commit: 9a43a9ec7ec864570e533cd98962fa6b3b640b9f - 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/75/pipeline/10/1 - Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll - Warning: Remote CI ci/woodpecker/pr/validation still running at verify time; the runner should re-poll. - Warning: The subagent did not measure a pristine-base make check baseline; the final run is green, so this is informational. - CI wait: ci/woodpecker/pr/validation -> pass - Decision: review the PR and merge? (`codex-svd ship --run-id 20261113T053602Z-adcfca98 --confirm-merge`)
svc-agent-writer 2026-11-13 06:24:01 +00:00
  • closed this issue
  • removed the
    svd:go
    label
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/messages-mind-mcp#12
No description provided.