No description
  • Erlang 89.7%
  • Python 6.5%
  • Dockerfile 2.1%
  • Shell 1.7%
Find a file
beamologist 80f80fb1df
All checks were successful
build-and-push / The tested lock arrives whole (push) Has been skipped
build-and-push / test (push) Successful in 0s
build-and-push / File the tested resolve for the signer (push) Successful in 4s
build-and-push / build-and-push (push) Successful in 3s
build-and-push / guard-1 (push) Has been skipped
c4 model / c4-model-1 (push) Successful in 4s
c4 model / c4-model (push) Successful in 0s
build-and-push / check (push) Successful in 3m59s
build-and-push / guard (push) Has been skipped
build-and-push / attest (push) Successful in 0s
Merge PR 26: Read model gate: a report counts only from its own replay driver
2026-11-24 04:10:24 +01:00
.github read model: ETS rebuilt from the log, and no rocksdb 2026-11-24 03:26:11 +01:00
apps Read model gate: a report counts only from its own replay driver 2026-11-24 03:54:58 +01:00
architecture read model: ETS rebuilt from the log, and no rocksdb 2026-11-24 03:26:11 +01:00
assets read model: ETS rebuilt from the log, and no rocksdb 2026-11-24 03:26:11 +01:00
config read model: ETS rebuilt from the log, and no rocksdb 2026-11-24 03:26:11 +01:00
deploy read model: ETS rebuilt from the log, and no rocksdb 2026-11-24 03:26:11 +01:00
scripts start.sh: write the cookie through a temp file at 0600 and refuse an empty one (review notes c, d) 2026-11-23 16:08:14 +01:00
.gitignore scaffold: rebar3 new mcl_service store=1 2026-09-23 23:07:07 +02:00
.tool-versions scaffold: rebar3 new mcl_service store=1 2026-09-23 23:07:07 +02:00
CHANGELOG.md read model: ETS rebuilt from the log, and no rocksdb 2026-11-24 03:26:11 +01:00
Containerfile read model: ETS rebuilt from the log, and no rocksdb 2026-11-24 03:26:11 +01:00
cosign.pub mcl-mail#21: publish the image signer's public key, and the verify line 2026-11-23 22:50:50 +01:00
LICENSE scaffold: rebar3 new mcl_service store=1 2026-09-23 23:07:07 +02:00
README.md read model: ETS rebuilt from the log, and no rocksdb 2026-11-24 03:26:11 +01:00
rebar.config read model: ETS rebuilt from the log, and no rocksdb 2026-11-24 03:26:11 +01:00
rebar.config.script mcl-mail#8: the linter only when MCL_LINT=1, so the image build fetches no plugin 2026-11-23 07:46:21 +01:00
SECURITY.md SECURITY.md: response times, attachment and identity rules, drills (macula-io/macula#174) 2026-10-31 02:59:25 +01:00

mcl-mail

CI License git.macula.io BEAM Buy Me a Coffee

Macula

Async mailboxes for the Macula mesh: an agent leaves work for a citizen who is not online right now


What it does

A citizen (a person, an agent or a service, anything with a macula node identity) keeps a mailbox here. Anyone can leave a letter in it while the citizen is offline; the citizen reads, replies and archives when they are back.

Seven request-and-reply procedures of its own, served under the org mcl-mail:

Procedure Payload Reply Acts on
mcl-mail/initiate_mailbox none #{} the caller's mailbox comes into being
mcl-mail/open_mailbox none #{} the caller's mailbox starts receiving mail
mcl-mail/deposit_letter to_citizen_did (64 hex), subject, body, optional reply_letter_id letter_id the recipient's mailbox; the sender is the caller
mcl-mail/reply_to_letter letter_id, subject, body letter_id marks the letter answered in the caller's mailbox, deposits the reply with the original sender
mcl-mail/archive_letter letter_id letter_id the caller's mailbox
mcl-mail/get_mailbox optional limit, before letters, and next when more remain the caller's unarchived letters, one bounded page at a time, each letter carried twice and then forgotten
mcl-mail/get_letter letter_id letter one of the caller's letters, carried like a mailbox read

A refusal comes back as the call's error with a short reason: no_caller, invalid_to_citizen_did, not_initiated, already_initiated, already_open, archived, mailbox_not_opened, mailbox_closed, mailbox_archived, letter_not_found, letter_archived, missing_letter_id, invalid_limit, invalid_cursor, not_found, replied_but_not_delivered.

A letter goes out as letter_id, from_did (64 hex), subject, body, deposited_at (unix ms), read, replied, archived (each 0 or 1), and reply_letter_id when it answers another. Text is CBOR text, never bytes. A letter too large for a page goes out as its envelope instead: body_withheld (1) and body_size (the withheld body's size in bytes) in place of subject and body.

Reading a mailbox: one bounded page at a time

get_mailbox answers at most one page (mcl-mail#1). A mailbox of any size is readable, and no answer is ever large enough for the host to refuse it:

  • limit (optional) is the most letters one answer carries. Without it the service applies its own fixed page count, and no answer ever carries more than the text budget below allows.
  • the answer also stops at a fixed text budget, whichever is reached first, so a mailbox of long letters is still answered in pieces: a letter that no longer fits ends the page and arrives whole on the next one. deposit_letter puts no bound on a letter's text, so a letter can be larger than a whole page may carry: that one comes as its envelope, with body_withheld: 1 and body_size and no subject or body, so the page fits and the cursor moves past it. Its text is not carried by this read at all — it becomes reachable when a deposit bounds a letter's text, which is a later change.
  • next is present when the mailbox holds more letters past the page: pass it back as before to read on. before holds the deposited_at and letter_id of the last letter the previous page carried, and the next page is everything strictly older than that pair.
  • letters come newest first, and only the letters a page carries in full are carried: a letter left for a later page was not handed over, and neither is one that came as its envelope, because its body was not delivered. Unread first is not the order and cannot be: the order has to be one a cursor can walk, and letters come again on the next read (below).

A letter is carried twice, then forgotten

A read carries every letter it hands over in full (mcl-mail#10, the owner's ruling: a mailbox should not fill up with letters already seen). The next read hands the same letters over once more, so a session that dies while handling them finds them again, and the read after that has forgotten them: they are deleted from the mailbox and never handed out again. get_letter by id is a read too, so a letter fetched by id is carried like one.

The carries are counted by the read facts themselves, not by arrivals, because the read model is replayed from the event log at every boot: a fact already counted does not count again, so a deploy does not turn a letter carried once into one carried twice (nor hand a forgotten letter out a third time).

A letter stored before this rule that was already marked read was carried twice by that rule's own account, so each mailbox's first read under this rule deletes those and hands out none of them; a letter unread at that moment is untouched. The read flag stays on the wire for clients that know it and is now always 0 for a letter a read hands out.

Who a call acts as

A procedure acts as the CALL's caller. macula puts caller on each inbound CALL: the node id of the identity key that signed the request, verified by every station on the path and again by this provider, and written over any caller the payload sends. A citizen's DID is that same node id. So a citizen can only open, read, reply from and archive in their own mailbox, and a letter's from_did is whoever signed the deposit, never something they typed. No separate ownership proof is needed, and none is accepted.

A deposit needs the recipient's mailbox to be initiated and open. Nothing opens a mailbox on a citizen's behalf, so a stranger cannot bring a mailbox into being for somebody who never asked for one.

Who can read a letter

The answers are always sealed. A request is sealed only if its caller seals it. Every procedure is confidential => required, and the service names its ML-KEM key in each advertisement (kem_advertise). A caller that seals its request to that key gets a sealed answer, and no station on the path can read either. A request that arrives in the clear is refused with sealed_required: the handler never runs and nothing is answered in the clear. But that request, with the letter's subject, body and recipient in it, has already crossed every station on the way to the provider. The provider cannot un-send it. So whether a deposit is protected in transit is the CALLER's choice:

  • A caller that passes confidential: "required" never sends a mailbox request in the clear. A provider that names no key is refused before anything is sent. crew-code's crew mail is not built yet and must pass "required" on every mail call when it is.
  • A caller on the default "preferred" (macula-mcp's mesh_call today, and the TypeScript SDK's default) seals whenever the advertisement it holds names a key. It goes out in the clear when that advertisement is keyless, which includes a stale 0.5.0 advertisement still within its lifetime for up to ten minutes after this release rolls. The provider then refuses it, and the refusal carries no key, so the call keeps failing until the caller fetches a fresh advertisement. Either way the clear copy has already travelled. macula-io/macula-mcp#76 asks for "required" as the default.
  • A client on an SDK older than macula 13 cannot seal at all. Every request it sends crosses in the clear and is refused.

Ask your client for the seal report (sealed: 1) to see it for a given call.

What sealing does not hide, stated so nobody assumes otherwise:

  • Routing. A station still sees the procedure name, the caller's node id, the sizes and the timing: who uses mail and when, though not what they wrote.
  • The box that runs mcl-mail. Letters are stored in plaintext in its event store and read model, so whoever operates that box can read every mailbox it holds. Encrypting a letter to its recipient is a separate design.
  • The sender's intent. Anyone on the realm can leave a letter in an open mailbox. The sender is whoever signed the deposit and cannot be forged, but what they wrote is untrusted. A client shows a letter's body as data, never as instructions.

How it is built

The C4 model is in architecture/README.md: what the service talks to, and what it is made of.

An umbrella of four apps, one per department:

  • guide_mailbox_lifecycle (CMD): the mailbox aggregate, one event-sourced stream per citizen (mailbox-<128-bit digest of the DID>), in the reckon-db store mcl_mail_store. Nine desks: initiate, open, close, archive and unarchive a mailbox; deposit, mark read, reply to and archive a letter. Status is bit flags (mailbox_status.hrl). The five CMD responders live beside their desks.
  • project_mailboxes (PRJ): letter_lifecycle_to_mailboxes projects the four letter events into the read model, an ETS table owned by mailboxes_read_model, one row per letter; mailboxes_read_model_replay fills it from the event store at every boot.
  • query_mailboxes (QRY): get_mailbox and get_letter, read from that model.
  • mcl_mail: the mcl_om_service contract.

close, archive and unarchive a mailbox are tested desks whose commands no responder serves yet, because no client needs them. Marking a letter read is folded into the two reads.

mcl-mail/info is not this service's own: mcl_om adds mcl-mail/info and mcl-mail/get_limits to every service, both open, and capabilities/0 is the list of the service's own procedures. info carries its name, version and description, its org and claim labels, node id, the macula and mcl_om versions, uptime, the health word and the procedures it advertises; get_limits carries the guard's limits and window. Both are open to any mesh caller, public facts only.

Running it

rebar3 compile
rebar3 eunit
MCL_LINT=1 rebar3 lint                 # local lint; see rebar.config.script (#8)
rebar3 dialyzer

scripts/health.sh                      # against a running node

The build needs nothing beyond OTP 28.4.3 and rebar3 (see "The images" below); the tests build no C++ and link no system library. Run them inside git.macula.io/macula-io/macula-ci-otp, the image CI uses, or on a VM with the .tool-versions release:

podman build -t mcl-mail -f Containerfile .

Run it on your own box

What a stranger needs, all of it public: no account, no token. Walked end to end on a fresh Ubuntu 24.04 box on 2026-11-23 (mcl-mail#16).

  1. A dual-stack box with Docker and Compose. The realm and the forge answer on IPv4 only, and the stations on IPv6 only, so the box needs both. On Ubuntu: apt-get install docker.io docker-compose-v2 git jq.

  2. The repository, anonymously: git clone https://git.macula.io/macula-services/mcl-mail.git.

  3. The image digest. Releases are on the forge registry, and pull anonymously. Pull the version you want and read its digest:

    docker pull git.macula.io/macula-services/mcl-mail:0.6.2
    docker image inspect --format '{{index .RepoDigests 0}}' git.macula.io/macula-services/mcl-mail:0.6.2
    

    Set MCL_MAIL_IMAGE_DIGEST to the sha256:... part, after checking that the image is ours. A release is signed by digest with the key in cosign.pub, at the root of this repository (sha256 60d955530a80f6e8d8fb5f0df86e576ee18323a263391e4778c96bcb1a6a3375):

    cosign verify --key cosign.pub --insecure-ignore-tlog \
      git.macula.io/macula-services/mcl-mail@$MCL_MAIL_IMAGE_DIGEST
    

    It needs no account. --insecure-ignore-tlog is required: the signer does not write to a public transparency log, so the key is the only thing checked. For 0.6.2 (sha256:19424c81...734d78) it prints "The signatures were verified against the specified public key", and the signed claims name ref v0.6.2 and repo macula-services/mcl-mail (mcl-mail#21).

  4. The realm (io.macula). Both values come from the realm's well-known document:

    J=$(curl -s https://realm.macula.io/.well-known/macula-realm.json)
    MCL_REALM=$(jq -r .realm_id <<<"$J")
    MCL_REALM_KEY=$(jq -r .public_key <<<"$J" | base64 -d | od -An -v -tx1 | tr -d ' \n')
    
  5. The stations. Seeds and node ids, index-paired. Today they are published only in mcl-echo's apps/mcl_echo/src/mcl_echo_stations.erl (macula-realm#7). Three of them:

    MACULA_STATION_SEEDS=station-de-falkenstein.macula.io:4433,station-fi-helsinki.macula.io:4433,station-de-frankfurt.macula.io:4433
    MACULA_STATION_NODE_IDS=00df68247d119685f94030afdb203ab7a2a105fb6093a964dbf0509a57e86435,004d1f470097ccf8826ce291900e882fdb1f20375e53901facaec0f23eb4efd8,00cd0008ec2e72b6572b7bf6fc8b048d7fe83993faf1fc544370f2bc1eb71f85
    
  6. A data directory. MCL_DATA defaults to a fleet path; set your own, for example MCL_DATA=/srv/mcl-mail, and MCL_BOX to a name for the box.

  7. Start it: docker compose -p mcl-mail -f deploy/docker-compose.yml up -d with those variables exported, then scripts/health.sh.

What you see next: the node connects to the stations and files its boot claim, and scripts/health.sh answers degraded (HTTP 503) with "claim": {"state": "pending"} and every procedure not_granted (operator_must_grant). The log says it: "an operator must admit this node before its procedures can be advertised". The realm's operator admits it; how to ask for that is not written down yet (macula-realm#8). Until it is admitted, the node serves nothing on the mesh.

Configuration

Variable Default Meaning
MCL_REALM required 64-hex realm tag, the sha256 of the realm's name. No default: a service that guesses its realm announces itself where nobody can attribute it.
MCL_REALM_KEY required The realm's public signing key, hex encoded: the trust anchor, not an identifier. Every org-namespaced advertisement is verified against it, so without it nothing resolves, the boot claim never reaches the realm, and the service stays green while unreachable. Public material, not a secret.
MACULA_STATION_SEEDS required Station hosts to dial, host[:port], comma-separated. No default: naming a realm costs nothing, dialling a fleet station from every dev clone does.
MACULA_STATION_NODE_IDS required The matching 64-hex station node ids, comma-separated, index-paired with the seeds. The dial is pinned (D5): mcl_om refuses to boot a pool with an unpinned seed.
MCL_NODE_NAME mcl_mail Erlang node name.
MCL_NODE_HOST 127.0.0.1 Erlang node host.
MCL_COOKIE unset Erlang cookie. Unset, the image makes a random one on first start in the secrets volume (/etc/mcl/secrets/erlang.cookie, 0600) and keeps it. Distribution and epmd listen on 127.0.0.1 only. A remote shell: docker exec -it mcl-mail /app/start.sh remote_console.
MCL_DATA_DIR /data in the image Where the store (mcl_mail_store/) lives, and the only thing there: the letter read model is ETS, rebuilt from that store at every boot. The compose file mounts a host directory there.
MCL_SERVICE_NAME mcl-mail Label on the boot claim the realm's operator sees on the Providers desk.
MCL_BOX empty Label naming the host, also on the boot claim. Set it where you deploy.
MCL_DATA /bulk0/mcl-mail in the compose file The host directory mounted on /data. On a fleet node, a bulk drive, never the small root partition.
MCL_MAIL_IMAGE_DIGEST required by the compose file sha256:<digest> of the released image to run. The compose file runs the image by digest, never by tag.

deploy/docker-compose.yml runs it, a minimal runnable example that carries what the service knows about itself. The fleet deploys services from its own compose files in macula-fleet, in the same shape: the image by digest, the digest pinned there by its release workflow. If you deploy through something else, let that carry placement: which host, which station, which realm, which secret store. Keeping the two apart is what stops a config table in a README and the real environment drifting.

Deployment

A v* tag publishes git.macula.io/macula-services/mcl-mail:<version> and nothing else: host00's image signer builds the tag itself, signs it by digest and pushes it to the forge registry (mcl-mail#6). On GitHub, build-push.yml still publishes to ghcr, signed by digest with its SBOM and provenance (macula-ci-images' attest-image.yml); ghcr's newest release is 0.6.0. A push to main publishes :main and :<sha> instead of :latest, and nothing follows them. The fleet runs a release by digest: macula-fleet's pin-releases workflow finds the signed release, verifies it was signed on its tag and pins <version>@sha256:<digest> (macula-fleet#14, #15), so a green v* tag is the deploy. To roll back, revert the pin and hold the image there. A push that changes only documentation builds no image (scripts/is_image_push.sh).

The service's org, the <org> its own procedure names carry (<org>/<name>), is this repository's name, fixed in config/sys.config.src. The realm's grant names it; without an org mcl_om refuses to boot, naming the setting.

Two things CI cannot do for you, both of which have bitten:

  1. The registry package may be created private, and the pull then fails on the host with a bare unauthorized that names nothing. Check it after the first build. On ghcr the org.opencontainers.image.source label in the Containerfile is what links the package to the repository.
  2. The host needs MCL_REALM (a public constant in macula-fleet's compose), MCL_REALM_KEY, MCL_BOX and the station seeds with their matching node ids from the box's committed config env in macula-fleet (mcl-mail-config.env), the cookie from its secrets file, and the image by digest in that compose.

The service contract

Six callbacks in mcl_mail_service, all required, all resolved by name by mcl_om at startup on a live node. The -behaviour(mcl_om_service) attribute turns a missing one into a compile error rather than an undef where nobody is watching, and the eunit suite guards the attribute itself.

The store and the read model

The service owns a reckon-db store, mcl_mail_store. mcl_om opens no store (0.35 on, mcl-om#10), so the service declares reckon_db, evoq and reckon_evoq itself, mcl_mail_service:event_store/0 describes the store, and mcl_mail_app opens it and its evoq subscription (mcl_mail_store) before mcl_om:boot/1. config/sys.config.src carries the evoq adapter block that the subscription requires.

The letter read model is an ETS table owned by mailboxes_read_model, in the PRJ department. It keeps nothing on disk: at every start mailboxes_read_model_replay reads the whole event store through the projection's own project/4 and fills the table, then turns on the readiness flag. Until that flag is on, get_mailbox and get_letter answer replaying — a mailbox still filling must never answer "no mail". The flag lives with the table: a restarted owner starts closed, and the projection is restarted with it (rest_for_one), so it is never open over an empty table.

⚠ The store id is written in two places, event_store/0's id and the evoq block, and nothing makes them agree by itself. A test compares them, along with a second one asserting the evoq block is present at all. Keep both.

⚠ deploy/docker-compose.yml mounts a volume, and on a node it must. Without it the mail lives inside the container and every recreate destroys it, which is the same as not keeping any.

The store is node-local: one node serves the mail it holds.

The images

The plain pair, on the forge registry, pinned by digest: the image builds in git.macula.io/macula-io/macula-ci-otp and runs on git.macula.io/macula-io/macula-pq-runtime, both Debian trixie on OTP 28.4.3, and CI runs in the same build image. Nothing is compiled from C++ beyond what macula's own NIFs need, and no system library is linked at run time. A test holds the pins, the absence of the rocksdb dependency and override it replaced (macula-ci-images#19), and the OTP release checks in place.

Licence

Apache-2.0.