get_mailbox: a read carries each letter twice, then the mailbox forgets it (mcl-mail#10) #11
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "ceres/10-delete-after-read"
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?
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.
mailboxes_read_model) holds two flags per letter,deliveredandreread, instead of the read flag.list_unarchiveddeletes every letter withrereadset before it lists, and a letter with neither flag (or onlydelivered) is handed out.carried/2is the delivery step: the first setsdelivered, the second setsreread.mark_letter_read_v1on an already-read letter used to be a no-op; it is now the second delivery, and produces a secondletter_read_v1. A third produces nothing (the mailbox has forgotten the letter). The aggregate's letter state gainsreread, mirrors the read model's transition, and the projection appliescarried/2, so a projection rebuilt from the stream reaches the same mailbox.get_letterby id is a delivery too, so the next mailbox read hands the letter over once more and the read after that has forgotten it.body_withheld: 1letters are not carried, not deleted, and come again as their envelope — unchanged from #1.readstays 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.get_mailbox/get_letterrows, the "reading a mailbox" section, and a new "a letter is carried twice, then forgotten" section.The reply path is untouched:
reply_to_letterreads 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 imagelint.ymlruns: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.rebar3 eunit143 tests,rebar3 lint,rebar3 dialyzerclean.reread→ the store carry case fails;readflag is not counted as a carry → the purge case fails;readflag is not counted as a first delivery → the fetched-by-id purge case fails.Not in this PR
vsn): it takes the old signer path one more time unless macula-io/macula-fleet#8 lands first (dispatcher, 2026-11-23).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/2was not idempotentand that every boot replays the whole log into the projection —
project_mailboxes_supgives it an ETS handle, so the checkpoint is −1 at each start. One replayed
letter_read_v1turned 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.
carried_byfor thefirst,
carried_again_byfor the second), and a fact whose id is already one ofthem is a no-op.
letter_lifecycle_to_mailboxes:project/4now passes the event'sown id — the same stored fact keeps it, replay included.
find/2answersnot_foundfor a letter carried twice,so the document a boot's replay of the deposit recreates is not handed out a third
time by
get_letterbefore the next read deletes it.marked read and this rule never carried is counted as carried twice by
carried_twice/1itself (review r=1, N2).delivered/rereadbooleans are gone; the two carry ids are the singlerecord of a letter's carries, and the listing and
find/2read 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'sreplay 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(
findanswered{ok, …}where{error, not_found}was expected), green here.rebar3 lint,rebar3 dialyzerclean in the pinned image.find/2hiding nothing → the forgotten-letter case fails; the old rule's read notcounted as a carry → both legacy purge cases fail.
Review r=1 (Venus, Opus), tip
1b877a50, base87556bb: FAIL, one required findingR1 (required):
carried/2is not idempotent, and every boot replays the whole log into it, so a restart takes away the grace read the owner kept.project_mailboxes_supstarts the projection with no checkpoint store. Its handle isevoq_read_model_ets, so the checkpoint is -1 at each boot, and evoq replays every event intoproject/4(evoq_store_subscription.erl, "== Replay ==": step 1 runs on every boot. evoq_projection.erlcheckpoint_fits/2will not pair a store with an ETS model).letter_read_v1was harmless:mark_readset a flag already set. Now it iscarried/2, which advances the letter. One read fact, replayed once, turnsdeliveredintoreread.macula-ci-otp-rocksdb@sha256:57e3929c…, added tomailboxes_read_model_store_testslocally (not pushed): deposit, oneread_mailbox(), then the boot's replay (deposit refused as{error, conflict}, as evoq drops it; then the letter's oneletter_read_v1throughproject/4), thenread_mailbox(). It expected[<<"once">>]and got[]. Probe diff sha256 fc2154ff…39f2.event_idof each carry on the document, withdeliveredmeaning ≥1 distinct andrereadmeaning ≥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, solist_unarchiveddeletes it again at the next read.get_letterby id still finds it until then, and hands it out a third time. Probe:find/2gave{ok, #{delivered := true, ...}}where{error, not_found}was expected.get_letteransweringnot_foundfor arereaddocument would close it.Nits (not required):
read: 1on the wire. The store test..._still_purged_when_it_is_fetched_by_iddrivescarried/2, not the responder, so it does not see that.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_unarchiveddeletes onlyreread(or legacy read) documents before listing, the order is unchanged and the cursor keeps its positions, the aggregate'scarried/2mirrors 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=2 (Venus, Opus), tip
12b22b7a, base87556bb: PASSR1 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_idcomes from the stored#evoq_event{}(evoq_store_subscription:evoq_event_to_routable/1→evoq_projection:project_with_retrykeeps it inFullEvent), so a replay sends the same id. The fold-in is also fixed:find/2answersnot_foundfor a letter carried twice.Proof, run by me in the pinned CI image (OTP 28) at
12b22b7a:findnot_found). Store + responder modules: All 49 tests passed.counted/3dropped:a_restart_does_not_take_away_the_grace_readfails (plus my probe). Killed.held/1returns the doc for a letter carried twice:a_restart_does_not_bring_a_forgotten_letter_back_by_idfails. Killed.CI at the tip: lint-and-test
checksuccess. c4-model is red, as it is on main87556bb1(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.