Purpose (change): a mailbox read carries each letter for two reads and deletes it at the third #10

Closed
opened 2026-11-23 04:57:47 +00:00 by ceres · 4 comments
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.

The owner's words (2026-11-23): "shouldnt mcl-mail just delete after read? / yes", then, on plain read-once being proposed: DECIDED 2026-11-23 "keep the safeguard, a little backlog is allowed, no?". So delete-after-read, with one read's grace: the safeguard stays.

What is built today (0.6.1, mcl-mail#1 and #2): get_mailbox answers one bounded page (20 letters, 20,000 bytes of encoded text) with a next cursor, letters newest first, oversized ones as envelopes (body_withheld: 1, not marked read), and the letters a page returns in full are marked read, with the read flag as fetched. The crew client (crew-code/exchange_crew_mail/mail.ts, readMail) asks get_mailbox with no cursor, so a member sees the first page only, and a letter marked read stays in the box unseen.

The defect that exposed it: a 65,769-character mailbox was refused by the host's result cap, and the HANDOFF letter in it was never delivered — while the server had already marked the page's letters read, so no later read could show them.

The rule to build (server, macula-services/mcl-mail):

  • Read N returns the letters no read has delivered yet, plus the letters read N−1 returned, and deletes the letters read N−2 returned. Each letter is therefore carried by two consecutive reads and removed by the third: a session that dies mid-handling finds it again on the next read.
  • Nothing is marked read any more; a letter is either still to come, carried once, carried twice, or gone.
  • The page bounds, the cursor and the envelope rule stay as they are: one bounded answer, next still handed out. An envelope (body_withheld: 1) was not delivered, so it is not counted as carried and is not deleted; it comes again as its envelope until a deposit bounds a letter's text (a later change).
  • Smallest shape in the read model (apps/project_mailboxes/src/mailboxes_read_model.erl): two flags per letter instead of a generation counter — delivered (carried once) and reread (carried twice). A read returns delivered false, plus delivered true and reread false; it sets reread on the ones already delivered; and it deletes, at its start, every letter with reread true. That is exactly read N returning N−1's letters and deleting N−2's, with no per-mailbox counter.
  • get_letter by id is a delivery too: it returns the letter and sets delivered, so the next mailbox read carries it once more and the third removes it.
  • The reply path is untouched: reply_to_letter reads the aggregate's state, which is built from the letter's events, never from this read model — a letter forgotten from the mailbox can still be replied to.

Generation-0 purge (unchanged): every letter already marked read before this rule is treated as carried twice — it is deleted by each mailbox's first read and never handed out again. No letter that is unread today is touched. No separate purge script.

Client: needs no new call; its read flag becomes always 0 for letters returned, so readMail may drop it from the rendered letter. Nothing else in crew-code changes.

Refs macula-services/mcl-mail#1, macula-services/mcl-mail#2, macula-io/macula-sponsors#18. The release takes the old signer path one more time unless macula-io/macula-fleet#8 lands first (dispatcher, 2026-11-23).

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. The owner's words (2026-11-23): "shouldnt mcl-mail just delete after read? / yes", then, on plain read-once being proposed: DECIDED 2026-11-23 "keep the safeguard, a little backlog is allowed, no?". So delete-after-read, with one read's grace: the safeguard stays. What is built today (0.6.1, mcl-mail#1 and #2): `get_mailbox` answers one bounded page (20 letters, 20,000 bytes of encoded text) with a `next` cursor, letters newest first, oversized ones as envelopes (`body_withheld: 1`, not marked read), and the letters a page returns in full are marked read, with the read flag as fetched. The crew client (`crew-code/exchange_crew_mail/mail.ts`, `readMail`) asks `get_mailbox` with no cursor, so a member sees the first page only, and a letter marked read stays in the box unseen. The defect that exposed it: a 65,769-character mailbox was refused by the host's result cap, and the HANDOFF letter in it was never delivered — while the server had already marked the page's letters read, so no later read could show them. The rule to build (server, `macula-services/mcl-mail`): - Read N returns the letters no read has delivered yet, plus the letters read N−1 returned, and deletes the letters read N−2 returned. Each letter is therefore carried by two consecutive reads and removed by the third: a session that dies mid-handling finds it again on the next read. - Nothing is marked read any more; a letter is either still to come, carried once, carried twice, or gone. - The page bounds, the cursor and the envelope rule stay as they are: one bounded answer, `next` still handed out. An envelope (`body_withheld: 1`) was not delivered, so it is not counted as carried and is not deleted; it comes again as its envelope until a deposit bounds a letter's text (a later change). - Smallest shape in the read model (`apps/project_mailboxes/src/mailboxes_read_model.erl`): two flags per letter instead of a generation counter — `delivered` (carried once) and `reread` (carried twice). A read returns `delivered` false, plus `delivered` true and `reread` false; it sets `reread` on the ones already delivered; and it deletes, at its start, every letter with `reread` true. That is exactly read N returning N−1's letters and deleting N−2's, with no per-mailbox counter. - `get_letter` by id is a delivery too: it returns the letter and sets `delivered`, so the next mailbox read carries it once more and the third removes it. - The reply path is untouched: `reply_to_letter` reads the aggregate's state, which is built from the letter's events, never from this read model — a letter forgotten from the mailbox can still be replied to. Generation-0 purge (unchanged): every letter already marked read before this rule is treated as carried twice — it is deleted by each mailbox's first read and never handed out again. No letter that is unread today is touched. No separate purge script. Client: needs no new call; its `read` flag becomes always 0 for letters returned, so `readMail` may drop it from the rendered letter. Nothing else in `crew-code` changes. Refs macula-services/mcl-mail#1, macula-services/mcl-mail#2, macula-io/macula-sponsors#18. The release takes the old signer path one more time unless macula-io/macula-fleet#8 lands first (dispatcher, 2026-11-23).
ceres changed title from Purpose (change): a mailbox read returns only unread letters and deletes the ones its previous read returned to Purpose (change): a mailbox read carries each letter for two reads and deletes it at the third 2026-11-23 05:03:09 +00:00
Author
Member

Building it as ruled here. PR: #11 (branch ceres/10-delete-after-read, one commit).

Three things the build had to decide that this text left open, recorded here so the issue stays the record:

  • The second delivery is a domain fact. A second mark_letter_read_v1 on a letter used to be a no-op; it is now the second carry and produces a second letter_read_v1, and a third produces nothing. Without that the projection would never learn reread, and a projection rebuilt from the stream would not reach the same mailbox.
  • A pre-rule letter fetched by its id before any mailbox read is purged too: the old read flag counts as its first carry, so that delivery is its second and the next read deletes it. It is never handed out by a mailbox read.
  • A one-page drain of a large mailbox walks two pages at a time: the newest page is handed out on two consecutive reads before the page behind it arrives. That follows from this rule and the unchanged page bounds — not a defect, but a member reading once a day gets through one page of new letters per two reads.
Building it as ruled here. PR: https://git.macula.io/macula-services/mcl-mail/pulls/11 (branch `ceres/10-delete-after-read`, one commit). Three things the build had to decide that this text left open, recorded here so the issue stays the record: - **The second delivery is a domain fact.** A second `mark_letter_read_v1` on a letter used to be a no-op; it is now the second carry and produces a second `letter_read_v1`, and a third produces nothing. Without that the projection would never learn `reread`, and a projection rebuilt from the stream would not reach the same mailbox. - **A pre-rule letter fetched by its id** before any mailbox read is purged too: the old `read` flag counts as its first carry, so that delivery is its second and the next read deletes it. It is never handed out by a mailbox read. - **A one-page drain of a large mailbox** walks two pages at a time: the newest page is handed out on two consecutive reads before the page behind it arrives. That follows from this rule and the unchanged page bounds — not a defect, but a member reading once a day gets through one page of new letters per two reads.
Author
Member

r=2 build decisions (review r=1, PR comment 1229 R1 + its same-root fold-in):

  1. A carry is counted by the read fact that carried it, not by its arrival. Every boot replays the whole log into the read model (the projection's handle is an ETS checkpoint at −1), so a replayed letter_read_v1 must be a no-op. A letter keeps carried_by (the first fact's event id) and carried_again_by (the second's); the projection passes the event's own id.
  2. find/2 answers not_found for a letter carried twice. Same root, other way round: a boot's replay of the deposit recreates a forgotten letter's document, and get_letter must not hand it out a third time before the next read deletes it.
  3. The generation-0 purge no longer rests on a replay arriving. carried_twice/1 counts the old rule's read flag itself as the letter's second carry, so a letter read before the rule is purged by the first read whether or not the fact that set the flag has been replayed (r=1 N2).
  4. The delivered/reread booleans are gone: the two carry ids are the one record of a letter's carries, read by the listing and by find/2.

Proof on 12b22b7: both review probes as cases, red on 1b877a5 and green here (eunit 149, lint, dialyzer in the pinned image), three mutants each killed by name. — Ceres

r=2 build decisions (review r=1, PR comment 1229 R1 + its same-root fold-in): 1. **A carry is counted by the read fact that carried it, not by its arrival.** Every boot replays the whole log into the read model (the projection's handle is an ETS checkpoint at −1), so a replayed `letter_read_v1` must be a no-op. A letter keeps `carried_by` (the first fact's event id) and `carried_again_by` (the second's); the projection passes the event's own id. 2. **`find/2` answers `not_found` for a letter carried twice.** Same root, other way round: a boot's replay of the deposit recreates a forgotten letter's document, and `get_letter` must not hand it out a third time before the next read deletes it. 3. **The generation-0 purge no longer rests on a replay arriving.** `carried_twice/1` counts the old rule's `read` flag itself as the letter's second carry, so a letter read before the rule is purged by the first read whether or not the fact that set the flag has been replayed (r=1 N2). 4. The `delivered`/`reread` booleans are gone: the two carry ids are the one record of a letter's carries, read by the listing and by `find/2`. Proof on `12b22b7`: both review probes as cases, red on `1b877a5` and green here (eunit 149, lint, dialyzer in the pinned image), three mutants each killed by name. — Ceres
Author
Member

Landed

PR 11 merged at 12b22b7a (review: Venus, Opus, PASS r=2 at 2026-11-23T06:20Z; merge 07:03Z). The purpose holds on the merged code: a read carries each letter twice and the third removes it, with Venus's restart probes in the suite and both his mutants killed.

Left open for the release, not for code: the release follows the old path one last time unless macula-io/macula-fleet#8 lands first (Mars, 2026-11-23). The close goes with the release, not with the merge.

## Landed PR 11 merged at `12b22b7a` (review: Venus, Opus, PASS r=2 at 2026-11-23T06:20Z; merge 07:03Z). The purpose holds on the merged code: a read carries each letter twice and the third removes it, with Venus's restart probes in the suite and both his mutants killed. Left open for the release, not for code: the release follows the old path one last time unless macula-io/macula-fleet#8 lands first (Mars, 2026-11-23). The close goes with the release, not with the merge.
Author
Member

Purpose met: landed at 12b22b7a, shipped in 0.6.2 — see the Landed comment of 2026-11-23 13:13Z above (Venus PASS r=2, restart probes in the suite, both mutants killed).

Purpose met: landed at 12b22b7a, shipped in 0.6.2 — see the Landed comment of 2026-11-23 13:13Z above (Venus PASS r=2, restart probes in the suite, both mutants killed).
ceres closed this issue 2026-11-24 01:05:04 +00:00
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
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#10
No description provided.