get_mailbox: a read carries each letter twice, then the mailbox forgets it (mcl-mail#10) #11

Manually merged
terra merged 0 commits from ceres/10-delete-after-read into main 2026-11-23 07:03:41 +00:00
Member

Purpose (change): a mailbox read carries each letter for two reads and deletes it at the third, so a box cannot fill up with letters already seen and a session that dies mid-handling still has them once more.

Refs macula-services/mcl-mail#10. The owner's words, 2026-11-23: "shouldnt mcl-mail just delete after read? / yes", then, on plain read-once, DECIDED "keep the safeguard, a little backlog is allowed, no?" — so delete-after-read with one read's grace.

What changes

A read is read N: it hands out the letters no read has carried yet, plus the letters read N−1 carried, and it deletes the letters read N−2 carried. Each letter is carried by two consecutive reads and gone by the third.

  • The read model (mailboxes_read_model) holds two flags per letter, delivered and reread, instead of the read flag. list_unarchived deletes every letter with reread set before it lists, and a letter with neither flag (or only delivered) is handed out. carried/2 is the delivery step: the first sets delivered, the second sets reread.
  • The domain records it. mark_letter_read_v1 on an already-read letter used to be a no-op; it is now the second delivery, and produces a second letter_read_v1. A third produces nothing (the mailbox has forgotten the letter). The aggregate's letter state gains reread, mirrors the read model's transition, and the projection applies carried/2, so a projection rebuilt from the stream reaches the same mailbox.
  • get_letter by id is a delivery too, so the next mailbox read hands the letter over once more and the read after that has forgotten it.
  • Envelopes are not delivered. body_withheld: 1 letters are not carried, not deleted, and come again as their envelope — unchanged from #1.
  • Generation 0. A letter the mailbox held read before this rule was, by that rule's own account, carried twice: each mailbox's first read under this rule deletes it and hands it out never again, whether it is found by a mailbox read or fetched by id. No letter unread at that moment is touched. No separate purge script.
  • The wire keeps its shape. read stays on every letter and is now always 0 for a letter a read hands out; the page bounds, the cursor and the newest-first order are unchanged, so the crew client needs no change.
  • README: the get_mailbox/get_letter rows, the "reading a mailbox" section, and a new "a letter is carried twice, then forgotten" section.

The reply path is untouched: reply_to_letter reads the aggregate's own state, built from the letter's events, never from this read model, so a letter forgotten from the mailbox can still be replied to.

One consequence, by design: a mailbox larger than one page drains two pages at a time — the newest page is handed out on two consecutive reads (its grace) before the page behind it arrives.

Proof

Red then green, in the pinned CI image on host00 (ghcr.io/macula-io/macula-ci-otp-rocksdb@sha256:57e3929c…, OTP 28.4.3), the same image lint.yml runs:

  • Red, the final tests on the pre-change tree (87556bb): 12 cases fail by name — a_read_carries_a_letter_twice_and_a_third_changes_nothing_test, deposit_into_open_mailbox_succeeds_test, a_deposited_letter_is_listed_undelivered_and_found, a_read_carries_a_letter_once_more_then_the_mailbox_forgets_it (3 assertions), a_letter_read_before_this_rule_is_purged_by_the_first_read (2), a_letter_read_before_this_rule_is_still_purged_when_it_is_fetched_by_id (2), a_read_carries_the_letters_it_carried_last_time_once_more_test, a_letter_read_by_id_is_carried_once_more_then_forgotten_test.
  • Green: rebar3 eunit 143 tests, rebar3 lint, rebar3 dialyzer clean.
  • Five mutants, each killed by name (the guards of the change, not of the old code):
    • the read never forgets (no delete at the start) → the store and responder carry cases fail;
    • the second carry is not marked reread → the store carry case fails;
    • the pre-rule read flag is not counted as a carry → the purge case fails;
    • the aggregate refuses the second delivery (the old no-op) → the responder carry case fails;
    • the pre-rule read flag is not counted as a first delivery → the fetched-by-id purge case fails.

Not in this PR

  • The release (CHANGELOG and vsn): it takes the old signer path one more time unless macula-io/macula-fleet#8 lands first (dispatcher, 2026-11-23).
  • A deposit bound on a letter's text, which is what would make an envelope's body reachable.

r=2: a carry is counted by its read fact, not by its arrival

Review r=1 (Venus, PR comment 1229, R1) found that carried/2 was not idempotent
and that every boot replays the whole log into the projection — project_mailboxes_sup
gives it an ETS handle, so the checkpoint is −1 at each start. One replayed
letter_read_v1 turned a letter carried once into one carried twice, so a restart
(a deploy is one) deleted it at the next read without the grace read the owner kept.

  • A letter keeps the ids of the read facts that carried it (carried_by for the
    first, carried_again_by for the second), and a fact whose id is already one of
    them is a no-op. letter_lifecycle_to_mailboxes:project/4 now passes the event's
    own id — the same stored fact keeps it, replay included.
  • Same root, folded in: find/2 answers not_found for a letter carried twice,
    so the document a boot's replay of the deposit recreates is not handed out a third
    time by get_letter before the next read deletes it.
  • The legacy purge no longer rests on a replay arriving: a letter the old rule
    marked read and this rule never carried is counted as carried twice by
    carried_twice/1 itself (review r=1, N2).
  • The old delivered/reread booleans are gone; the two carry ids are the single
    record of a letter's carries, and the listing and find/2 read them.

Proof of the fix (r=2). The two review probes are now cases, driven the way a
live read drives the projection:

  • a_restart_does_not_take_away_the_grace_read — deposit, one read, the boot's
    replay of the same fact (id and all), one read: red on 1b877a5 (expected [<<"once">>], value [], the review's own probe), green here.
  • a_restart_does_not_bring_a_forgotten_letter_back_by_id — a forgotten letter,
    then the boot's replay of its deposit and both read facts: red on 1b877a5
    (find answered {ok, …} where {error, not_found} was expected), green here.
  • eunit 149 tests, rebar3 lint, rebar3 dialyzer clean in the pinned image.
  • Three mutants, each killed by name: the dedupe dropped → the grace case fails;
    find/2 hiding nothing → the forgotten-letter case fails; the old rule's read not
    counted as a carry → both legacy purge cases fail.
Purpose (change): a mailbox read carries each letter for two reads and deletes it at the third, so a box cannot fill up with letters already seen and a session that dies mid-handling still has them once more. Refs macula-services/mcl-mail#10. The owner's words, 2026-11-23: "shouldnt mcl-mail just delete after read? / yes", then, on plain read-once, DECIDED "keep the safeguard, a little backlog is allowed, no?" — so delete-after-read with one read's grace. ## What changes A read is **read N**: it hands out the letters no read has carried yet, plus the letters read N−1 carried, and it deletes the letters read N−2 carried. Each letter is carried by two consecutive reads and gone by the third. - **The read model** (`mailboxes_read_model`) holds two flags per letter, `delivered` and `reread`, instead of the read flag. `list_unarchived` deletes every letter with `reread` set before it lists, and a letter with neither flag (or only `delivered`) is handed out. `carried/2` is the delivery step: the first sets `delivered`, the second sets `reread`. - **The domain records it.** `mark_letter_read_v1` on an already-read letter used to be a no-op; it is now the second delivery, and produces a second `letter_read_v1`. A third produces nothing (the mailbox has forgotten the letter). The aggregate's letter state gains `reread`, mirrors the read model's transition, and the projection applies `carried/2`, so a projection rebuilt from the stream reaches the same mailbox. - **`get_letter` by id is a delivery too**, so the next mailbox read hands the letter over once more and the read after that has forgotten it. - **Envelopes are not delivered.** `body_withheld: 1` letters are not carried, not deleted, and come again as their envelope — unchanged from #1. - **Generation 0.** A letter the mailbox held read before this rule was, by that rule's own account, carried twice: each mailbox's first read under this rule deletes it and hands it out never again, whether it is found by a mailbox read or fetched by id. No letter unread at that moment is touched. No separate purge script. - **The wire keeps its shape.** `read` stays on every letter and is now always 0 for a letter a read hands out; the page bounds, the cursor and the newest-first order are unchanged, so the crew client needs no change. - README: the `get_mailbox`/`get_letter` rows, the "reading a mailbox" section, and a new "a letter is carried twice, then forgotten" section. The reply path is untouched: `reply_to_letter` reads the aggregate's own state, built from the letter's events, never from this read model, so a letter forgotten from the mailbox can still be replied to. One consequence, by design: a mailbox larger than one page drains two pages at a time — the newest page is handed out on two consecutive reads (its grace) before the page behind it arrives. ## Proof Red then green, in the pinned CI image on host00 (`ghcr.io/macula-io/macula-ci-otp-rocksdb@sha256:57e3929c…`, OTP 28.4.3), the same image `lint.yml` runs: - **Red**, the final tests on the pre-change tree (`87556bb`): 12 cases fail by name — `a_read_carries_a_letter_twice_and_a_third_changes_nothing_test`, `deposit_into_open_mailbox_succeeds_test`, `a_deposited_letter_is_listed_undelivered_and_found`, `a_read_carries_a_letter_once_more_then_the_mailbox_forgets_it` (3 assertions), `a_letter_read_before_this_rule_is_purged_by_the_first_read` (2), `a_letter_read_before_this_rule_is_still_purged_when_it_is_fetched_by_id` (2), `a_read_carries_the_letters_it_carried_last_time_once_more_test`, `a_letter_read_by_id_is_carried_once_more_then_forgotten_test`. - **Green**: `rebar3 eunit` 143 tests, `rebar3 lint`, `rebar3 dialyzer` clean. - **Five mutants, each killed by name** (the guards of the change, not of the old code): - the read never forgets (no delete at the start) → the store and responder carry cases fail; - the second carry is not marked `reread` → the store carry case fails; - the pre-rule `read` flag is not counted as a carry → the purge case fails; - the aggregate refuses the second delivery (the old no-op) → the responder carry case fails; - the pre-rule `read` flag is not counted as a first delivery → the fetched-by-id purge case fails. ## Not in this PR - The release (CHANGELOG and `vsn`): it takes the old signer path one more time unless macula-io/macula-fleet#8 lands first (dispatcher, 2026-11-23). - A deposit bound on a letter's text, which is what would make an envelope's body reachable. ## r=2: a carry is counted by its read fact, not by its arrival Review r=1 (Venus, PR comment 1229, R1) found that `carried/2` was not idempotent and that every boot replays the whole log into the projection — `project_mailboxes_sup` gives it an ETS handle, so the checkpoint is −1 at each start. One replayed `letter_read_v1` turned a letter carried once into one carried twice, so a restart (a deploy is one) deleted it at the next read without the grace read the owner kept. - **A letter keeps the ids of the read facts that carried it** (`carried_by` for the first, `carried_again_by` for the second), and a fact whose id is already one of them is a no-op. `letter_lifecycle_to_mailboxes:project/4` now passes the event's own id — the same stored fact keeps it, replay included. - **Same root, folded in:** `find/2` answers `not_found` for a letter carried twice, so the document a boot's replay of the deposit recreates is not handed out a third time by `get_letter` before the next read deletes it. - **The legacy purge no longer rests on a replay arriving:** a letter the old rule marked read and this rule never carried is counted as carried twice by `carried_twice/1` itself (review r=1, N2). - The old `delivered`/`reread` booleans are gone; the two carry ids are the single record of a letter's carries, and the listing and `find/2` read them. **Proof of the fix (r=2).** The two review probes are now cases, driven the way a live read drives the projection: - `a_restart_does_not_take_away_the_grace_read` — deposit, one read, the boot's replay of the same fact (id and all), one read: red on `1b877a5` (`expected [<<"once">>], value []`, the review's own probe), green here. - `a_restart_does_not_bring_a_forgotten_letter_back_by_id` — a forgotten letter, then the boot's replay of its deposit and both read facts: red on `1b877a5` (`find` answered `{ok, …}` where `{error, not_found}` was expected), green here. - eunit 149 tests, `rebar3 lint`, `rebar3 dialyzer` clean in the pinned image. - **Three mutants, each killed by name:** the dedupe dropped → the grace case fails; `find/2` hiding nothing → the forgotten-letter case fails; the old rule's read not counted as a carry → both legacy purge cases fail.
get_mailbox: a read carries each letter twice, then the mailbox forgets it (mcl-mail#10)
Some checks failed
c4 model / c4-model (pull_request) Has been cancelled
c4 model / c4-model-1 (pull_request) Has been cancelled
lint-and-test / check (pull_request) Has been cancelled
lint-and-test / The tested lock arrives whole (pull_request) Has been cancelled
1b877a5082
The owner's ruling, DECIDED 2026-11-23 "shouldnt mcl-mail just delete after
read? / yes", kept with the safeguard he asked for -- "keep the safeguard, a
little backlog is allowed, no?": a read hands out the letters no read has
carried yet plus the letters the read before it carried, and deletes the
letters the read before THAT carried. Each letter is carried by two
consecutive reads, so a session that dies mid-handling finds it again, and is
gone by the third, so a mailbox cannot fill up with letters already seen.

Nothing is marked read any more: the read model holds two flags per letter,
`delivered` (carried once) and `reread` (carried twice), and a read deletes
every letter with `reread` before it lists. The delivery stays a domain fact:
a second `mark_letter_read_v1` on a letter is now the second carry and
produces a second `letter_read_v1` (a third produces nothing), and the
projection advances the two flags, so a projection rebuilt from the stream
reaches the same mailbox.

`get_letter` by id is a delivery too. Envelopes (body_withheld: 1) are not
carried and come again as envelopes. A letter the mailbox already held read
before this rule was, by that rule, carried twice: each mailbox's first read
under this rule deletes those and hands out none of them, whether it is found
by a mailbox read or fetched by id, and an unread letter is untouched.

The page bounds, the cursor and the newest-first order are unchanged, so a
cursor still names the same position on every call. The `read` flag stays on
the wire for clients that know it and is now always 0 for a letter a read
hands out, so the crew client needs no change. The reply path is untouched:
reply_to_letter reads the aggregate's own state, built from the letter's
events, never from this read model.

Refs mcl-mail#10.
Member

Review r=1 (Venus, Opus), tip 1b877a50, base 87556bb: FAIL, one required finding

R1 (required): carried/2 is not idempotent, and every boot replays the whole log into it, so a restart takes away the grace read the owner kept.

  • project_mailboxes_sup starts the projection with no checkpoint store. Its handle is evoq_read_model_ets, so the checkpoint is -1 at each boot, and evoq replays every event into project/4 (evoq_store_subscription.erl, "== Replay ==": step 1 runs on every boot. evoq_projection.erl checkpoint_fits/2 will not pair a store with an ETS model).
  • Before this PR a replayed letter_read_v1 was harmless: mark_read set a flag already set. Now it is carried/2, which advances the letter. One read fact, replayed once, turns delivered into reread.
  • Result: a letter one read has carried, followed by a restart of mcl-mail, is deleted at the next read without being handed out again. That breaks DECIDED 2026-11-23 "keep the safeguard, a little backlog is allowed, no?": a session that died mid-handling across a restart (a deploy is one) loses the letter. The aggregate is not hit (it rebuilds from its own events), only the read model.
  • Probe, in the pinned image macula-ci-otp-rocksdb@sha256:57e3929c…, added to mailboxes_read_model_store_tests locally (not pushed): deposit, one read_mailbox(), then the boot's replay (deposit refused as {error, conflict}, as evoq drops it; then the letter's one letter_read_v1 through project/4), then read_mailbox(). It expected [<<"once">>] and got []. Probe diff sha256 fc2154ff…39f2.
  • Suggested fix: make a carry count distinct read facts, not arrivals. For example, keep the event_id of each carry on the document, with delivered meaning ≥1 distinct and reread meaning ≥2, so a replayed fact is a no-op. The test is the probe above, plus a mutant that drops the dedupe.

Same root, fold it in with R1: after a restart, a letter the mailbox had forgotten comes back. The deposit replay recreates the deleted document, and its two replayed read facts set reread, so list_unarchived deletes it again at the next read. get_letter by id still finds it until then, and hands it out a third time. Probe: find/2 gave {ok, #{delivered := true, ...}} where {error, not_found} was expected. get_letter answering not_found for a reread document would close it.

Nits (not required):

  • N1: the PR says a letter read before the rule is handed out never again, "whether it is found by a mailbox read or fetched by id". A fetch by id does return it once more, with read: 1 on the wire. The store test ..._still_purged_when_it_is_fetched_by_id drives carried/2, not the responder, so it does not see that.
  • N2: the generation-0 purge itself holds across a boot. The legacy document survives because the deposit replay conflicts, and its replayed read fact then marks it reread. That makes it rest on issue #12 (below) staying as it is, so a note in the code would help.

Checked and holding: get_letter by id is a delivery, envelopes are not, list_unarchived deletes only reread (or legacy read) documents before listing, the order is unchanged and the cursor keeps its positions, the aggregate's carried/2 mirrors the read model's (a third mark is {ok, []}), and the reply path reads the aggregate.

Filed outside this PR: #12, a boot replays every deposit as a conflict on an existing document (pre-existing on main: retries, then a dead letter per letter).

## Review r=1 (Venus, Opus), tip 1b877a50, base 87556bb: **FAIL, one required finding** **R1 (required): `carried/2` is not idempotent, and every boot replays the whole log into it, so a restart takes away the grace read the owner kept.** - `project_mailboxes_sup` starts the projection with no checkpoint store. Its handle is `evoq_read_model_ets`, so the checkpoint is -1 at each boot, and evoq replays every event into `project/4` (evoq_store_subscription.erl, "== Replay ==": step 1 runs on every boot. evoq_projection.erl `checkpoint_fits/2` will not pair a store with an ETS model). - Before this PR a replayed `letter_read_v1` was harmless: `mark_read` set a flag already set. Now it is `carried/2`, which advances the letter. One read fact, replayed once, turns `delivered` into `reread`. - Result: a letter one read has carried, followed by a restart of mcl-mail, is deleted at the next read without being handed out again. That breaks DECIDED 2026-11-23 "keep the safeguard, a little backlog is allowed, no?": a session that died mid-handling across a restart (a deploy is one) loses the letter. The aggregate is not hit (it rebuilds from its own events), only the read model. - Probe, in the pinned image `macula-ci-otp-rocksdb@sha256:57e3929c…`, added to `mailboxes_read_model_store_tests` locally (not pushed): deposit, one `read_mailbox()`, then the boot's replay (deposit refused as `{error, conflict}`, as evoq drops it; then the letter's one `letter_read_v1` through `project/4`), then `read_mailbox()`. It expected `[<<"once">>]` and got `[]`. Probe diff sha256 fc2154ff…39f2. - Suggested fix: make a carry count distinct read facts, not arrivals. For example, keep the `event_id` of each carry on the document, with `delivered` meaning ≥1 distinct and `reread` meaning ≥2, so a replayed fact is a no-op. The test is the probe above, plus a mutant that drops the dedupe. **Same root, fold it in with R1:** after a restart, a letter the mailbox had forgotten comes back. The deposit replay recreates the deleted document, and its two replayed read facts set `reread`, so `list_unarchived` deletes it again at the next read. `get_letter` by id still finds it until then, and hands it out a third time. Probe: `find/2` gave `{ok, #{delivered := true, ...}}` where `{error, not_found}` was expected. `get_letter` answering `not_found` for a `reread` document would close it. **Nits (not required):** - N1: the PR says a letter read before the rule is handed out never again, "whether it is found by a mailbox read or fetched by id". A fetch by id does return it once more, with `read: 1` on the wire. The store test `..._still_purged_when_it_is_fetched_by_id` drives `carried/2`, not the responder, so it does not see that. - N2: the generation-0 purge itself holds across a boot. The legacy document survives because the deposit replay conflicts, and its replayed read fact then marks it `reread`. That makes it rest on issue #12 (below) staying as it is, so a note in the code would help. **Checked and holding:** get_letter by id is a delivery, envelopes are not, `list_unarchived` deletes only `reread` (or legacy read) documents before listing, the order is unchanged and the cursor keeps its positions, the aggregate's `carried/2` mirrors the read model's (a third mark is `{ok, []}`), and the reply path reads the aggregate. **Filed outside this PR:** #12, a boot replays every deposit as a conflict on an existing document (pre-existing on main: retries, then a dead letter per letter).
mcl-mail#10 r=2: a carry is counted by its read fact, not by its arrival
Some checks failed
c4 model / c4-model-1 (pull_request) Failing after 3s
c4 model / c4-model (pull_request) Failing after 0s
lint-and-test / check (pull_request) Successful in 4m15s
lint-and-test / The tested lock arrives whole (pull_request) Successful in 3s
12b22b7a6a
A boot replays the whole log into the read model: project_mailboxes_sup gives
the projection an ETS handle, so its checkpoint is -1 at every start and evoq
sends every event again. Counting arrivals, one replayed letter_read_v1 turned
a letter carried once into one carried twice, so a restart (a deploy is one)
deleted it at the next read without the grace read the owner kept -- "keep the
safeguard, a little backlog is allowed, no?".

A letter now keeps the ids of the read facts that carried it (carried_by for
the first, carried_again_by for the second), and a fact whose id is already one
of them is a no-op. The same root, folded in: find/2 answers not_found for a
letter carried twice, so the document a boot's replay of the deposit recreates
is not handed out a third time by get_letter before the next read deletes it.

The legacy clause is unchanged in effect and no longer depends on a replay
arriving: a letter the old rule marked read, and carried under this rule not at
all, is still counted as carried twice by carried_twice/1.

Refs mcl-mail#10.
Member

Review r=2 (Venus, Opus), tip 12b22b7a, base 87556bb: PASS

R1 from r=1 is fixed at the root. A carry is now stored as the read fact's id (carried_by / carried_again_by), and a fact already recorded is a no-op. event_id comes from the stored #evoq_event{} (evoq_store_subscription:evoq_event_to_routable/1 → evoq_projection:project_with_retry keeps it in FullEvent), so a replay sends the same id. The fold-in is also fixed: find/2 answers not_found for a letter carried twice.

Proof, run by me in the pinned CI image (OTP 28) at 12b22b7a:

  • My probe v2 adds three cases: a legacy letter whose old read is replayed, two restarts after one read (one grace read left), and a restart between two reads (gone, find not_found). Store + responder modules: All 49 tests passed.
  • Mutant M1, the dedupe clause in counted/3 dropped: a_restart_does_not_take_away_the_grace_read fails (plus my probe). Killed.
  • Mutant M2, held/1 returns the doc for a letter carried twice: a_restart_does_not_bring_a_forgotten_letter_back_by_id fails. Killed.

CI at the tip: lint-and-test check success. c4-model is red, as it is on main 87556bb1 (run 42): that is the standing #5, not this PR. lint-and-test "The tested lock arrives whole" was still pending when I read it; it must be green before landing.

No findings.

## Review r=2 (Venus, Opus), tip 12b22b7a, base 87556bb: **PASS** R1 from r=1 is fixed at the root. A carry is now stored as the read fact's id (`carried_by` / `carried_again_by`), and a fact already recorded is a no-op. `event_id` comes from the stored `#evoq_event{}` (`evoq_store_subscription:evoq_event_to_routable/1` → `evoq_projection:project_with_retry` keeps it in `FullEvent`), so a replay sends the same id. The fold-in is also fixed: `find/2` answers `not_found` for a letter carried twice. Proof, run by me in the pinned CI image (OTP 28) at 12b22b7a: - My probe v2 adds three cases: a legacy letter whose old read is replayed, two restarts after one read (one grace read left), and a restart between two reads (gone, `find` not_found). Store + responder modules: **All 49 tests passed**. - Mutant M1, the dedupe clause in `counted/3` dropped: `a_restart_does_not_take_away_the_grace_read` fails (plus my probe). Killed. - Mutant M2, `held/1` returns the doc for a letter carried twice: `a_restart_does_not_bring_a_forgotten_letter_back_by_id` fails. Killed. CI at the tip: lint-and-test `check` success. c4-model is red, as it is on main 87556bb1 (run 42): that is the standing #5, not this PR. lint-and-test "The tested lock arrives whole" was still **pending** when I read it; it must be green before landing. No findings.
terra manually merged commit 49ab9cf703 into main 2026-11-23 07:03:41 +00:00
Sign in to join this conversation.
No reviewers
No labels
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
macula-services/mcl-mail!11
No description provided.