mcl-mail: get_mailbox returns the whole mailbox in one answer, so a full mailbox cannot be read #1
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 (defect): a crew member can read a mailbox of any size — a read returns one bounded page and a way to ask for the next — so no mailbox is unreadable and no letter is marked read without being delivered.
Found by the Supervisor, relayed by Mars, 2026-11-22. Class: a read returns the whole store.
What happens
The Supervisor called
crew_mail_readand got no mailbox. The ledger line, verbatim (~/.claude/crew/ledger/2026-W47/, the note is the whole story):It happened twice that day (the earlier line is
at1795307804006, session778a890f-7445-4895-852e-c7ca12a0c55d), so it is not a one-off. The mailbox could not be read at all, including the letters that matter.Where the defect is
get_mailboxtakes no payload —get_mailbox_by_citizen_responder.erl:19-25readsmailbox_citizen:caller(Payload)and thenmailboxes_read_model:list_unarchived(CitizenDid), every unarchived letter, in one answer.README.md:42documents the payload asnoneand the answer as "the caller's unarchived letters, unread first": there is no page, no limit, and no unread-only read.crew-code/exchange_crew_mail/mail.ts:249-250callsget_mailboxwith{}, andreadMail(275-287) renders every letter it got; a letter body may be up to 16,000 characters.limit/after_seqbecause of it. Here the refused answer comes back as an error fromcalls.mesh.call, so crew-code records it asmail_unreachable: the ledger says the mailbox was out of reach when it was reached and its whole content refused.get_mailbox_by_citizen_responder.erl:24). So a read whose answer is refused has consumed the unread signal anyway: the letters stay listed (they are unarchived, so a later read sees them), but the unread-first ordering and "what arrived since last time" are gone. The Supervisor cannot tell which of the 1,133 lines are new.The tool side cannot fix this alone:
crew_mail_readhas no way to ask for less. There is no listing call that bounds or pages, andget_letterneeds ids it can only learn from the whole-list read. A fix starts in this repository, in the read.Does Martha's crew mail read the same way?
Yes, unless the bound exists before the port. Gap 16 of macula-io/martha#957 is "crew mail, crew memory and
bin/mail-ledger-check", a port of crew-code's slices, andcrew-code/docs/port_map.mdplacesexchange_crew_mailas one of them. Martha's own tree has no mailbox read today (noget_mailboxanywhere in macula-io/martha), and a member it launches reaches mail only through the crew plugin'screw_mail_read— the code above. So whatever builds gap 16 inherits this read unchanged.The check that must fail by name
Two tests, both red today, in this repository's suite beside the read's own tests (
apps/query_mailboxes/test/mailbox_query_responders_tests.erl):get_mailbox_answers_one_bounded_page_and_a_cursor_test— a mailbox holding more letters than one answer may carry comes back as a bounded page with a way to ask for the next, and the answer's size stays under the host's cap.a_letter_not_returned_in_the_page_is_not_marked_read_test— a read marks read only the letters it actually returns.And one on the crew side when it is touched: its read asks for one page rather than the whole mailbox.
Nothing beyond the design is decided here: the page size and the cursor's shape are the design's to name. No fix is proposed in this issue.
Design (step 1, before code)
Everything below is the issue's Done-when and nothing more: one bounded page, a
way to ask for the next, and no letter marked read without being delivered. No
new check beyond the two tests the issue names.
What a mailbox holds, and why a count alone is not a bound
The refusal that opened this issue was one answer of 277,461 characters
across 1,133 lines. That number is a size, not a count: the same 1,133 letters
with short bodies might have been accepted. So the bound cannot be "N letters":
at 16,000 characters (
crew-code/exchange_crew_mail/mail.ts,BODY_MAX) —but this service does not enforce that, so a producer's longer body is
possible;
was refused: a count-only bound would leave the mailbox unreadable in exactly
the case the issue is about.
The page
A page is bounded twice, and stops at whichever comes first:
limitfrom the payload when present (a positive integer);otherwise a fixed default of 20. This is what makes a page of short
letters cheap to read (
deposited_at,from_didand the framing are perletter too).
letter's
subjectandbodyplus 200 for its ids, flags and framing.This is what keeps the answer under the host's cap whatever the letters
hold. One letter is always returned, however long it is, so an
oversized letter is delivered in a page of its own rather than never.
The two numbers are named in the responder as
PAGE_LETTERS,PAGE_TEXTandLETTER_OVERHEAD, with this reasoning beside them; nothing else restates them.The cursor
nextin the answer, when the mailbox holds more past the page:the
deposited_atandletter_idof the last letter the page carried. Thecaller passes it back as
beforein the next request, and the page holds everyunarchived letter strictly older than that pair. The cursor names a
position, never an identity of its own: any client that can read
deposited_at/letter_idfrom a letter can also build one.A page request the responder cannot read is refused by name, and nothing is
marked read by a refused read:
invalid_limit(not a positive integer) andinvalid_cursor(nodeposited_at, or no non-emptyletter_id).The order, and the one behaviour this changes
The page walks the mailbox newest first:
deposited_atdescending, thenletter_iddescending when two letters share a millisecond. That pair is atotal order no read can change, which is what a cursor needs.
The read used to list unread first, and that cannot survive paging. A read
marks the letters it returns read, so an order ranking unread above read moves
those very letters down the list after they were delivered — and a cursor over
it hands them out a second time (a letter delivered on page 1, now read, sorts
after every unread letter, including the cursor's boundary). No stateless
cursor over a mutating rank can avoid that. So the order is newest first, each
letter carries its own
readflag, and the README states the trade. This isthe only user-visible behaviour change in the package;
README.mdand the readmodel's store test change with it.
Two letters of one mailbox cannot share a
letter_id(the document id carriescitizen_didandletter_id), so the timestamp tiebreak is the only tie.Marking read
Only the letters in the answer are marked read, after the page is fixed (count
and budget applied), by the existing
mark_fetched_letter_read. Today theresponder marks everything the listing returned before replying, so a refused
answer had already consumed the unread signal; after this change a letter held
for a later page is still unread when the next page — or a caller that never
pages — looks at it, and its
readflag still says it is new.Not in this package
crew_mail_read) still asks for one page and renders it;it does not follow
nextyet. The issue says the crew-side test comes "whenit is touched", and the client is another repository, so it is a follow-up
package after this one lands.
get_letter, and no new check.The code is written against this design and is held, unpushed, until an Opus
member has read it and Mars says go.
Design r=2 — R1 fixed: an oversized letter comes as its envelope, and stays unread
R1 is right, and my r=1 rule was wrong: "one letter is always returned, however
long" cannot hold. Deposit puts no bound on a letter's text
(
deposit_letter_v1:validatechecks onlyto_citizen_didandfrom_didone3d6311), so one sender can store a letter past the host's cap. Under my r=1rule that letter is page 1 of every read, the answer is refused,
nextneverarrives, every older letter is unreachable — and the letter was marked read
without being delivered.
The fix (your option (a))
The page rule becomes: a letter that does not fit the page's remaining text
budget is delivered as its envelope, with the text withheld, and the page ends
there. So a page always fits the host's cap, whatever the mailbox holds, and
nextalways advances past the oversized letter.The envelope of a withheld letter carries everything a caller needs to fetch it
whole:
no
subjectand nobody.get_letterwith itsletter_idfetches it whole —one letter past the page budget is still far below the 277,461 characters the
host refused, so nothing is lost, it is only not carried in a page.
A withheld letter is not marked read. Its body was not delivered, so the
unread signal stays: it comes back as unread on a later walk. It does not block
anything, because the order is positional (newest first), not unread first.
Why not your option (b)
A deposit bound would be a second, different behaviour change in a package whose
Done-when is the read, and it still needs this envelope path for letters stored
before it. The envelope rule alone makes every mailbox readable and every marked
letter delivered, for any letter, however large — so (b) stays a product
question for a later package, not a gap in this one. If you would rather have
(b) as well, say so and I will add it; the design below stands on its own
without it.
The two bounds, counted in bytes
Unchanged from r=1, with your note taken:
limitletters (default 20);byte_sizeofsubject+body, plus 200 bytes per letter for ids, flags and framing.Bytes, not codepoints: a codepoint count would let 48,000 four-byte characters
through. The margin is the safety: 48,000 bytes is under a fifth of the
277,461 characters the host refused, so JSON escaping (quotes and backslashes
inflate about 2x; the crew client's
JSON.stringifyemits UTF-8 and escapesonly those and control characters) and MCP framing still land well under the
cap. I do not measure the encoded answer here — the mesh encodes CBOR and the
host re-encodes JSON, so the responder cannot see the bytes the cap counts;
the margin is what covers the difference.
Everything else stands as r=1
next= the page's last letter's{deposited_at, letter_id}, passedback as
before; the page holds everything strictly older; a position, not anidentity of its own;
cannot be paged;
body it carried;
invalid_limit,invalid_cursorfor a page request it cannot read;Tests
get_mailbox_answers_one_bounded_page_and_a_cursor_testplants oneoversized letter among the letters — stored through the read model, since
deposit is untouched — and asserts: the page fits its budget,
nextisthere, the oversized letter comes as an envelope with
body_withheld: 1andits
body_size, and walking fromnextreaches every letter exactly once,the mailbox fully readable.
a_letter_not_returned_in_the_page_is_not_marked_read_testasserts theletters held back for a later page are not marked read, and that the
withheld letter is not marked read either.
The code is written and being changed to this; it stays unpushed until your
verdict and Mars's go.
r=2, as built: two deltas from the passed design
The design above (comment 316) was read as a PASS at r=2. Two changes were
made while building, on Mars's note, and they are recorded here rather than
edited into that comment.
is still reachable whole through
get_letter. That holds only below thehost's cap: a letter large enough to need an envelope can be large enough for
get_letterto be refused too. Corrected everywhere: this read carries notext for such a letter at all, and its text becomes reachable when a deposit
bounds a letter's text — the design's option (b), a later package. The code
comments, README and CHANGELOG say that and nothing more.
so it arrives whole at the head of the next page, where the budget starts
again. Only a letter too large for an empty page is carried as its
envelope. That keeps an envelope for the letters that genuinely cannot be
delivered in one answer, rather than for any letter that happened to fall
late in a page.
The bounds, cursor, order, the two refusals, marking only what was delivered in
full, and the two tests are as passed.
Purpose (defect) met: a read returns one bounded page with a way to ask for the next, so no mailbox is unreadable, and no letter is marked read without being delivered.
It was built and landed, but the forge never showed it as merged. PR 2's head
c9c43d7004403d1181aab8ca4249ec4fe911ee0a("get_mailbox: one bounded page and a cursor") is an ANCESTOR of main05702cdf2d87:git merge-base --is-ancestor c9c43d70 origin/mainanswers 0, andgit log origin/main --not c9c43d70holds only later merges. It was carried by PR 9 (release 0.6.1). PR 2 read closed-unmerged because its base wasceres/ceres-forge-workflows, not main.Checked against main
05702cdf, line by line against this issue's Purpose:apps/query_mailboxes/src/get_mailbox_by_citizen/get_mailbox_by_citizen_responder.erl:67-88takeslimit(default?PAGE_LETTERS) and stops at a text budget as well, so the answer stays under the host's cap whatever the mailbox holds.beforeis thenextcursor of the previous answer, and the answer carriesnextwhile a letter lies past the page. The cursor is a total order no read can change —{deposited_at, letter_id}descending (mailboxes_read_model.erl:188-197,older_than/2at 244-247), which is the part mcl-mail#1's own analysis said the old unread-first order could not give.marked_delivered/2(..._responder.erl:73-80) carries only a letter the page hands over IN FULL; a letter held back for the next page and a letter that came as its envelope — text withheld,body_withheld: 1,body_size— are not carried at all.list_unarchived/3always pages.Tests on main cover it:
apps/project_mailboxes/test/mailboxes_read_model_store_tests.erl:163-165walks three pages through the cursor, and the responder's tests cover limit, cursor and delivery (I have not re-run them here; the ranges that touched them landed green in PR 9/PR 10/PR 11).Its follow-ups are closed too: #10 and #11 (a read carries each letter twice, then forgets it).
Found while checking this against macula-io/macula-ci-images#19's mail step, which the design gated on this issue's PR 2.