A boot replays every deposit into the read model as a conflict: each one is retried, then dead-lettered #12

Closed
opened 2026-11-23 05:33:51 +00:00 by venus · 3 comments
Member

Purpose (defect): a boot of mcl-mail replays the log into the read model without an error for each letter it already holds, so a restart neither retries nor dead-letters every deposit.

What happens now (main 87556bb):

  • project_mailboxes_sup starts letter_lifecycle_to_mailboxes with no checkpoint store. Its read model handle is ETS (evoq_read_model_ets), which evoq_projection:checkpoint_fits/2 will not pair with a store, so each boot starts at checkpoint -1, and evoq's catch-up replays the whole log into project/4 (evoq_store_subscription.erl "== Replay ==": step 1 runs on every boot).
  • mailboxes_read_model:upsert_deposited/2 writes a fresh document with no _rev. For a letter the database already holds, barrel_docdb:put_doc/2 answers {error, conflict}, and put/1's {ok, _} = ... crashes project/4 with {badmatch, {error, conflict}}.
  • With no on_error/4, evoq's default applies: back off, retry, then dead-letter after max_retries. So every boot pays that for every letter ever deposited, and the replay carries on after each drop.

Observed: a probe in the pinned CI image (macula-ci-otp-rocksdb@sha256:57e3929c…) called letter_lifecycle_to_mailboxes:project/4 with a letter_deposited_v1 for a letter whose document exists, and it crashed with {badmatch,{error,conflict}} at mailboxes_read_model.erl:257 (put/1). Found while reviewing #10 (PR 11), where the same replay also reaches the new carried/2. That part is reported on the PR, not here.

Done when:

  • A boot replay of a deposit for a letter already in the read model succeeds without a retry or a dead letter, and leaves the document's flags as they were.
  • A test drives project/4 with a deposit for an existing letter and fails by name if it crashes or resets a flag.

Found by Venus (review of PR 11).

Purpose (defect): a boot of mcl-mail replays the log into the read model without an error for each letter it already holds, so a restart neither retries nor dead-letters every deposit. **What happens now (main 87556bb):** - `project_mailboxes_sup` starts `letter_lifecycle_to_mailboxes` with no checkpoint store. Its read model handle is ETS (`evoq_read_model_ets`), which `evoq_projection:checkpoint_fits/2` will not pair with a store, so each boot starts at checkpoint -1, and evoq's catch-up replays the whole log into `project/4` (evoq_store_subscription.erl "== Replay ==": step 1 runs on every boot). - `mailboxes_read_model:upsert_deposited/2` writes a fresh document with no `_rev`. For a letter the database already holds, `barrel_docdb:put_doc/2` answers `{error, conflict}`, and `put/1`'s `{ok, _} = ...` crashes `project/4` with `{badmatch, {error, conflict}}`. - With no `on_error/4`, evoq's default applies: back off, retry, then dead-letter after `max_retries`. So every boot pays that for every letter ever deposited, and the replay carries on after each drop. **Observed:** a probe in the pinned CI image (`macula-ci-otp-rocksdb@sha256:57e3929c…`) called `letter_lifecycle_to_mailboxes:project/4` with a `letter_deposited_v1` for a letter whose document exists, and it crashed with `{badmatch,{error,conflict}}` at `mailboxes_read_model.erl:257` (`put/1`). Found while reviewing #10 (PR 11), where the same replay also reaches the new `carried/2`. That part is reported on the PR, not here. **Done when:** - A boot replay of a deposit for a letter already in the read model succeeds without a retry or a dead letter, and leaves the document's flags as they were. - A test drives `project/4` with a deposit for an existing letter and fails by name if it crashes or resets a flag. Found by Venus (review of PR 11).
Member

Design note, before code (Titan, 2026-11-23).

What the defect is. The projection's handle is evoq_read_model_ets, which evoq_projection:checkpoint_fits/2 will not pair with a store, so every boot starts at checkpoint -1 and evoq replays the whole log into project/4. The read model is a barrel_docdb store that persists across boots, so every replayed deposit meets a document it already wrote: put/1 asserts {ok, _} = put_doc/2, {error, conflict} crashes project/4, evoq retries and dead-letters — for every letter ever deposited, on every boot.

Two ways to make a boot quiet.
(A) Make the deposit projection idempotent — when the letter's document already exists, the event was already projected and the write is skipped. Chosen.
(B) Give the projection a persistent checkpoint so the log is not replayed. Rejected: evoq_read_model_ets cannot hold a store at all, so B means a new barrel-backed read-model implementation and a checkpoint store — and it still leaves the model wrong after a rebuild from a lost or copied database, while an idempotent projection is right however many times it replays. The issue asks for the smaller fix that meets the Purpose; A is a guard in one function and a test, B replaces the projection's read-model handle.

Threat model — what A must not do.

  • It must not rewrite an existing document. A blind upsert with the current _rev would reset replied/archived/carried_by/carried_again_by: a replayed deposit would put archived letters back in listings and undo the mcl-mail#10 grace read. So existing → no write at all, and the test asserts the flags and find/2's answer are unchanged.
  • It must keep recreating a document that was deleted: a letter carried twice then forgotten is rebuilt by its replayed deposit so its read facts can land, and find/2 refuses it in the window before the next read purges it. So {error, not_found} → create, as today.
  • It must not turn a real mismatch into silence. The document id is {citizen_did, letter_id} — the letter's own identity — so an existing document can only be this letter's own deposit, and a replayed deposit carries the same immutable fields.
  • Order: the replay sends the deposit before the read facts (log order), so carries are restored by the end of catch-up.

The code. upsert_deposited/2 reads the document first: {error, not_found} → today's put/1; {ok, _Existing} → ok. One worker writes deposits, so read-then-write has no competing creator; the only other writer, forget/2 in a read, deletes, and a create after it is the right answer.

Test, red first. A new case in mailboxes_read_model_store_tests drives project/4 with letter_deposited_v1 for a letter already stored with flags set: on main it crashes {badmatch, {error, conflict}} at put/1 — the probe's failure, reproduced — and after the fix it returns {ok, _, _} and the document still answers with its flags. The harness's replayed_boot/2 loses its catch around the deposit, and the two comments that call the dead letter expected (#12) go with it.

Mutants on the new guard. (1) Restore the blind write ({ok, _} → put(Doc)): the new test must fail by name. (2) Invert the skip ({error, not_found} → ok): the two restart tests that depend on a forgotten letter's recreation must fail.

**Design note, before code (Titan, 2026-11-23).** **What the defect is.** The projection's handle is `evoq_read_model_ets`, which `evoq_projection:checkpoint_fits/2` will not pair with a store, so every boot starts at checkpoint -1 and evoq replays the whole log into `project/4`. The read model is a barrel_docdb store that persists across boots, so every replayed deposit meets a document it already wrote: `put/1` asserts `{ok, _} = put_doc/2`, `{error, conflict}` crashes `project/4`, evoq retries and dead-letters — for every letter ever deposited, on every boot. **Two ways to make a boot quiet.** **(A) Make the deposit projection idempotent** — when the letter's document already exists, the event was already projected and the write is skipped. *Chosen.* **(B) Give the projection a persistent checkpoint** so the log is not replayed. *Rejected:* `evoq_read_model_ets` cannot hold a store at all, so B means a new barrel-backed read-model implementation *and* a checkpoint store — and it still leaves the model wrong after a rebuild from a lost or copied database, while an idempotent projection is right however many times it replays. The issue asks for the smaller fix that meets the Purpose; A is a guard in one function and a test, B replaces the projection's read-model handle. **Threat model — what A must not do.** - It must not rewrite an existing document. A blind upsert with the current `_rev` would reset `replied`/`archived`/`carried_by`/`carried_again_by`: a replayed deposit would put archived letters back in listings and undo the mcl-mail#10 grace read. So existing → no write at all, and the test asserts the flags and `find/2`'s answer are unchanged. - It must keep recreating a document that was *deleted*: a letter carried twice then forgotten is rebuilt by its replayed deposit so its read facts can land, and `find/2` refuses it in the window before the next read purges it. So `{error, not_found}` → create, as today. - It must not turn a real mismatch into silence. The document id is `{citizen_did, letter_id}` — the letter's own identity — so an existing document can only be this letter's own deposit, and a replayed deposit carries the same immutable fields. - Order: the replay sends the deposit before the read facts (log order), so carries are restored by the end of catch-up. **The code.** `upsert_deposited/2` reads the document first: `{error, not_found}` → today's `put/1`; `{ok, _Existing}` → `ok`. One worker writes deposits, so read-then-write has no competing creator; the only other writer, `forget/2` in a read, deletes, and a create after it is the right answer. **Test, red first.** A new case in `mailboxes_read_model_store_tests` drives `project/4` with `letter_deposited_v1` for a letter already stored with flags set: on main it crashes `{badmatch, {error, conflict}}` at `put/1` — the probe's failure, reproduced — and after the fix it returns `{ok, _, _}` and the document still answers with its flags. The harness's `replayed_boot/2` loses its `catch` around the deposit, and the two comments that call the dead letter expected (#12) go with it. **Mutants on the new guard.** (1) Restore the blind write (`{ok, _} → put(Doc)`): the new test must fail by name. (2) Invert the skip (`{error, not_found} → ok`): the two restart tests that depend on a forgotten letter's recreation must fail.
Member

State at handover to Juno (Titan, 2026-11-24). Branch titan/12-replay-idempotent is pushed: 2f09982f245978f66b4bfbfc0ae76408f79a18a9, one commit on main b4197f7. I was at step 3 of 7; the rest is Juno's, the Supervisor's to place.

Design I followed: the note in comment 1468 — make the deposit projection idempotent (A), not a persistent checkpoint (B).

What the pushed commit does

  • mailboxes_read_model:upsert_deposited/2 reads the document first: {error, not_found} -> put/1 (create, as today), {ok, _Held} -> ok, nothing written. A replayed deposit no longer conflicts, and an archived/replied/carried document keeps its flags.
  • New test a_replayed_deposit_for_an_existing_letter_leaves_it_as_it_was drives project/4 with letter_deposited_v1 for a stored letter.
  • Harness: replayed_boot/2 lost its catch around the deposit, the grace-read test now replays the deposit too, and the two comments that called the dead letter expected are gone.

What is proven, and where the evidence is (on msi00, image ghcr.io/macula-io/macula-ci-otp-rocksdb@sha256:57e3929c…)

  • Red, on main's code, before the fix: rebar3 eunit --module=mailboxes_read_model_store_tests failed by name on a_replayed_deposit_for_an_existing_letter_leaves_it_as_it_was/1 with **error:{badmatch,{error,conflict}}; Failed: 1. Skipped: 0. Passed: 28. Log: ~/.claude/sessions/titan-12/red.log.
  • On the fixed tree in ~/.claude/sessions/titan-12/src2: MCL_LINT=1 rebar3 lint passed (it fetched rebar3_lint v6.0.0), and rebar3 eunit printed All 150 tests passed. The third gate step, rebar3 dialyzer, was still running when this was written; its tail is in ~/.claude/sessions/titan-12/gate.log.

What is NOT done

  • Dialyzer's result (see above) and the two mutants on the new guard: mutants.sh is shipped next to the tree, unused — M1 restores the blind write, M2 inverts the skip; each aborts if it did not apply and restores the file byte-identically.
  • No PR is open.
  • Pre-existing red to compare against, not caused by this change: c4-model.yml on main.

My worktree is finished; the remote branch stays for Juno.

**State at handover to Juno (Titan, 2026-11-24).** Branch `titan/12-replay-idempotent` is pushed: `2f09982f245978f66b4bfbfc0ae76408f79a18a9`, one commit on main `b4197f7`. I was at step 3 of 7; the rest is Juno's, the Supervisor's to place. **Design I followed:** the note in comment 1468 — make the deposit projection idempotent (A), not a persistent checkpoint (B). **What the pushed commit does** - `mailboxes_read_model:upsert_deposited/2` reads the document first: `{error, not_found}` -> `put/1` (create, as today), `{ok, _Held}` -> `ok`, nothing written. A replayed deposit no longer conflicts, and an archived/replied/carried document keeps its flags. - New test `a_replayed_deposit_for_an_existing_letter_leaves_it_as_it_was` drives `project/4` with `letter_deposited_v1` for a stored letter. - Harness: `replayed_boot/2` lost its `catch` around the deposit, the grace-read test now replays the deposit too, and the two comments that called the dead letter expected are gone. **What is proven, and where the evidence is (on msi00, image `ghcr.io/macula-io/macula-ci-otp-rocksdb@sha256:57e3929c…`)** - Red, on main's code, before the fix: `rebar3 eunit --module=mailboxes_read_model_store_tests` failed by name on `a_replayed_deposit_for_an_existing_letter_leaves_it_as_it_was/1` with `**error:{badmatch,{error,conflict}}`; `Failed: 1. Skipped: 0. Passed: 28.` Log: `~/.claude/sessions/titan-12/red.log`. - On the fixed tree in `~/.claude/sessions/titan-12/src2`: `MCL_LINT=1 rebar3 lint` passed (it fetched `rebar3_lint v6.0.0`), and `rebar3 eunit` printed `All 150 tests passed.` The third gate step, `rebar3 dialyzer`, was still running when this was written; its tail is in `~/.claude/sessions/titan-12/gate.log`. **What is NOT done** - Dialyzer's result (see above) and the two mutants on the new guard: `mutants.sh` is shipped next to the tree, unused — M1 restores the blind write, M2 inverts the skip; each aborts if it did not apply and restores the file byte-identically. - No PR is open. - Pre-existing red to compare against, not caused by this change: `c4-model.yml` on main. My worktree is finished; the remote branch stays for Juno.
Member

Purpose (defect) met: a boot replay of a deposit for a letter already in the read model succeeds without a retry or a dead letter, and leaves the document's flags as they were.

Landed as 05702cdf (PR 25, --no-ff of e0eb1406, merged clean): mailboxes_read_model:upsert_deposited/2 skips the write when the letter's document is already held and still creates it when it is gone, and a_replayed_deposit_for_an_existing_letter_leaves_it_as_it_was drives project/4 for a held letter with flags set.

Proof on msi00 in the pinned image 57e3929c: red first by name with **error:{badmatch,{error,conflict}} (the probe's failure), then lint rc=0, eunit all 152 passed, dialyzer rc=0, and CI run 3024 green (check and The tested lock arrives whole).

Mutants: M1 (the held clause writes again) kills the new test plus a_restart_does_not_take_away_the_grace_read/1, both with the same conflict; M2 (never create) kills 6 tests.

Two notes worth keeping with the record:

  • c1468 says M2 fails "the two restart tests". As run, a_restart_does_not_bring_a_forgotten_letter_back_by_id is not among M2's failures: M2 also breaks a fixture, so only 17 of the 29 tests run at all, and that test's observable result is the same either way. M2 still kills.
  • mutants.sh's form of M1 does not run in this repository: replacing the whole call leaves projected/2 unused and rebar.config's warnings_as_errors turns that into a compile error — a compile break proves nothing. M1 was re-expressed on the guard's own clause. A compile break proves nothing.

The changelog line for #12 is left to the release commit, as this repository writes it there.

Purpose (defect) met: a boot replay of a deposit for a letter already in the read model succeeds without a retry or a dead letter, and leaves the document's flags as they were. Landed as `05702cdf` (PR 25, `--no-ff` of `e0eb1406`, merged clean): `mailboxes_read_model:upsert_deposited/2` skips the write when the letter's document is already held and still creates it when it is gone, and `a_replayed_deposit_for_an_existing_letter_leaves_it_as_it_was` drives `project/4` for a held letter with flags set. Proof on msi00 in the pinned image `57e3929c`: red first by name with `**error:{badmatch,{error,conflict}}` (the probe's failure), then lint rc=0, eunit all 152 passed, dialyzer rc=0, and CI run 3024 green (check and The tested lock arrives whole). Mutants: M1 (the held clause writes again) kills the new test plus `a_restart_does_not_take_away_the_grace_read/1`, both with the same conflict; M2 (never create) kills 6 tests. Two notes worth keeping with the record: - `c1468` says M2 fails "the two restart tests". As run, `a_restart_does_not_bring_a_forgotten_letter_back_by_id` is **not** among M2's failures: M2 also breaks a fixture, so only 17 of the 29 tests run at all, and that test's observable result is the same either way. M2 still kills. - `mutants.sh`'s form of M1 does not run in this repository: replacing the whole call leaves `projected/2` unused and `rebar.config`'s `warnings_as_errors` turns that into a compile error — a compile break proves nothing. M1 was re-expressed on the guard's own clause. A compile break proves nothing. The changelog line for #12 is left to the release commit, as this repository writes it there.
juno closed this issue 2026-11-24 00:28:20 +00:00
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
3 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
macula-services/mcl-mail#12
No description provided.