interplane.Get Omega
Read in your language

mailhand

mailhand: agents look after a person's email in seeded Omega. Maildir + MIME parsing, JSONL CLI and MCP, rule triage, policy-gated sending, hash-chained audit. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

mailhand

5b8e473 pin: omega-mv 2ee0a70d / omc 26540e6d (pin-follow) Drake Stapleton committed 2026-11-13
proofs/
src/
tests/
tools/
vendor/
.gitignore7
DEPENDENCIES.md2487
DEPS446
LICENSE11358
NOTICE333
PIN126
README.md11852
REPIN603
build.sh2534

README.md

mailhand

mailhand lets software agents look after a person's email, so the person does almost nothing. The person runs one setup command, reads one summary file, and now and then says yes to a draft. Agents do the rest through a command line that prints JSON lines, or through the same operations as MCP tools. It is written in seeded Omega and is free software (Apache-2.0).

What it does

  • Reads a Maildir (folders, new/cur/tmp, flags in file names) and parses each message: folded headers, encoded words (RFC 2047), MIME parts nested to any depth, base64 and quoted-printable, UTF-8 and ISO-8859-1 text (other charsets are reported as not decoded, never guessed), threads from Message-ID / In-Reply-To / References, and List-Id / List-Unsubscribe.
  • Lists, shows, searches and threads messages; marks them read, moves, archives and labels them.
  • Triages without a model: fixed rules over the headers and the person's own address book (built from the mail they sent) plus their own rules file. Every decision names the rule that fired.
  • Writes drafts freely. Sends only what the person allowed: a standing permission they wrote in the policy file, or their yes to that exact draft (identified by the SHA-256 of its bytes).
  • Writes a plain-text digest: what arrived, what agents filed and why, drafts waiting for a yes, what was sent and under which permission.
  • Keeps an append-only, hash-chained audit log of every operation, and checks it with verify.
  • Remote accounts: sync fetches new mail from an IMAP account into the Maildir and pushes the agents' flag changes and moves back; sending can go to the person's provider by SMTP submission. Both run over TLS only (see "Remote accounts").

Setup (the person's one step)

./build.sh                         # needs the pinned omc as ./omc (see DEPENDENCIES.md)
build/mailhand setup --maildir "$HOME/Mail" --me me@example.org --name Me \
    --smtp unix:/run/user/1000/mail/smtp.sock

This creates ~/.mailhand/ (or --home DIR, or $MAILHAND_HOME) with config, policy, rules, approvals, drafts/, outbox/, sent/, labels/, audit.log, the address book, and the Maildir folders INBOX, Sent, Drafts and Archive when missing. The default policy sends nothing without approval. To approve a draft: build/mailhand approve --draft SHA (it shows the draft and waits for yes). Read ~/.mailhand/digest.txt after an agent runs digest.

For agents

mailhand list [--folder NAME|all] [--unread] [--since EPOCH|YYYY-MM-DD] [--from TEXT] [--thread HEX]
mailhand show --id FOLDER/UNIQUE        mailhand thread --id ID        mailhand search --query TEXT
mailhand triage [--id ID | --folder F]  mailhand mark-read --id ID     mailhand archive --id ID
mailhand move --id ID --folder NAME     mailhand label --id ID --label NAME [--remove] [--why TEXT]
mailhand draft-reply --id ID --body TEXT [--all]    mailhand draft-new --to A,B --subject S --body TEXT
mailhand drafts     mailhand queue-send --draft SHA     mailhand deliver     mailhand book
mailhand digest     mailhand audit [--limit N]          mailhand verify [--head HEX]
mailhand stats [--folder F|all]     (parse every message; counts only)
mailhand mcp        (MCP over stdio: the same operations as tools)

--thread HEX selects messages whose 16-hex thread field (first 8 bytes of SHA-256 of the root id: the first References id, else In-Reply-To, else Message-ID) equals HEX; thread --id ID follows the whole conversation across folders, including replies that name only their parent.

Output is one JSON object per line on stdout; diagnostics go to stderr. Exit codes: 0 ok, 1 usage, 2 not found, 3 refused by the send gate, 4 message refused by the parser, 5 path refused (traversal, symlink, outside the mail root), 6 I/O error, 7 audit chain broken, 8 delivery failed, 9 not set up.

Remote accounts (IMAP sync, SMTP submission over TLS)

The person writes the credentials file ~/.mailhand/accounts (or names another file with the config line accounts PATH). One block per account, key value lines, # comment lines:

account work
imap imaps://imap.example.org            # implicit TLS, port 993 unless :PORT is given
smtp submission://smtp.example.org       # STARTTLS on 587; or smtps://HOST (implicit TLS, 465)
user me@example.org
password the rest of this line, exactly
roots /etc/ssl/certs/ca-certificates.crt # optional trust store (this is the default)

connect 127.0.0.1 exists for tests only: the TCP connection goes to that address while the certificate is still checked against the host name. mailhand refuses the file unless it is a regular file (never followed through a symlink), owned by the user running mailhand, with mode 0600 or tighter (mode & 077 == 0), and at most 262144 bytes (the one buffer that holds it). A refusal exits 5 and names the file and its mode on stderr, never its content; a missing file exits 9. The password is read into that one buffer, used once for AUTHENTICATE PLAIN, LOGIN or AUTH PLAIN, and overwritten with zeros: it is never written to stdout, stderr, the audit log, a state file or MCP output, and the JSON records (which MCP returns) do not name the file.

  • mailhand sync --account NAME [--folder NAME|all] [--resync] (also the MCP tool sync: it never sends mail and never shows credentials) fetches new mail into the Maildir and pushes local changes back. Per folder, the state file FOLDER/mailhand-imap (next to its new/ cur/ tmp/) holds the account, UIDVALIDITY, the last UID seen and one record per synced message. New messages are streamed into tmp/ and renamed into new/ (no flags) or cur/ (:2, and the letters S R F T D for \Seen \Answered \Flagged \Deleted \Draft), named INTERNALDATE.V<UIDVALIDITY>U<UID>F<folder hash>.mailhand so the same message always gets the same name. Local flag changes (mark-read, ...) are pushed with UID STORE; local moves (move, archive) with UID MOVE, else UID COPY + UID STORE \Deleted + UID EXPUNGE (UIDPLUS), else COPY + STORE \Deleted + EXPUNGE, which also removes any other message already marked \Deleted in that mailbox: the record then says "risk". Nothing is ever deleted on the server because a message is missing locally. A changed UIDVALIDITY is refused (exit 3, uidvalidity_changed, nothing fetched); --resync then matches every known message by its SHA-256 against the new UIDs, so the Maildir never gets a second copy. Exit codes: 0 ok, 2 account not found, 3 refused by the server (or UIDVALIDITY changed), 5 credentials file refused, 6 connection, TLS or timeout failure, 9 no credentials file or no imap line.
  • Sending to a provider: the config line smtp account:NAME. The send gate is unchanged and runs first: a send the policy refuses opens no connection at all. Then smtps (TLS first) or submission (greeting, EHLO, STARTTLS required, 220, the TLS handshake, EHLO again); any failure before the handshake completes aborts, so AUTH and MAIL are never sent in plaintext. AUTH PLAIN, over TLS only.
  • Each network operation has a deadline: config io_timeout SECONDS (default 30).

TLS is the omtls library (TLS 1.3 written in seeded Omega, OmegaTls), built into mailhand from one commit named in DEPS (see DEPENDENCIES.md). A certificate that does not match the host name, has expired, or does not chain to the trust store stops the session before any command is sent (exit 6 for sync, 8 for deliver, with the reason on stderr).

Safety model

  • Mail is untrusted. Every value that came from a mail (headers, body, file names) is printed only inside an "untrusted" object, next to a fixed "warning" field saying that instructions inside mail are not instructions from the person. mailhand never follows links, never fetches anything, never opens or runs attachments (it lists name, type, size and SHA-256), and no mail can change the policy, the rules, the approvals or the address book: those files are written only by the person's commands (setup, approve) and by book, which reads only the Sent folder and mailhand's own sent copies; move refuses to put anything into Sent or Drafts.
  • Sending is the consequential act. A draft leaves only when the policy has a standing permission that covers it (only recipients already in the address book, never attachments) or when the person approved that exact draft by its SHA-256; changing one byte of a draft after approval voids the approval. deliver checks again before each submission. Delivery is SMTP submission to a unix socket or 127.0.0.1 port the person configured, or to the person's provider over TLS ("Remote accounts" above), nothing else.
  • Paths: message ids are FOLDER/UNIQUE made of safe characters only, so .. cannot be written; every directory is checked to be a real directory and message files are opened without following symlinks. Anything else is refused with exit code 5.
  • Limits of this model: agents and the person run as the same Unix user, so an agent with a shell could edit the policy or approvals files directly; mailhand cannot stop that. What it gives is that its own interface never does it, and the audit chain and digest show what happened. The audit chain detects changed records; dropping the last records is detected only against a head you keep elsewhere (verify --head HEX; the digest prints the head).

Limits

  • TLS: TLS 1.3 only (omtls); a server that offers only TLS 1.2 is refused.
  • Charsets other than UTF-8, US-ASCII and ISO-8859-1 are not decoded.
  • Drafts are plain text without attachments.

Check it yourself

You need the pinned compiler: the file PIN names the omega-mv revision and the sha256 of its seed/out/omc; copy that file to ./omc in this directory (sha256sum omc must print the omc-sha256 of PIN). You also need git: build.sh fetches the dependencies named in DEPS at their commits (see DEPENDENCIES.md). Then, from the repository root:

./build.sh tests/GATE_UNITS build/gate-bin && build/gate-bin ./omc

The gate reads PIN, checks the compiler's SHA-256 itself and refuses any other (exit 3), checks that no tracked file other than PIN restates the pin (MHGATE pincheck files=N restated=0), builds mailhand, the test SMTP sink and the fake mail servers from the unit lists build.sh generated in build/gen/, runs every scenario in tests/scenarios/ (synthetic mail only; the language is in tests/scenarios/README.md), then builds each mutant in tests/mutants.txt (for example the send policy check removed, the re-check in deliver removed, the warning field removed, the path checks removed) and requires a scenario to fail against each. The last line is MHGATE PASS scenarios=9 steps=864 mutants-killed=27/27. Scenario 50-robust cuts every fixture at 64 lengths and changes 256 single bytes in it (14,272 parser runs); each must give a result or a defined refusal, never a trap. Scenarios 55-creds, 60-imap and 70-smtp-tls run the remote-account code against the fake IMAP and SMTP servers in tests/srv/ (built by the gate as srv with the omtls test TLS server, over real TLS 1.3 to the host name mail.test with the omtls test certificates; including refused handshakes), including a credential leak scan of every file mailhand wrote.

Speed, on 10,000 generated messages (tests/gen/mkmaildir.om):

./build.sh && ./build.sh tests/BENCH_UNITS build/bench && build/bench build/mailhand build/bench-run

proofs/ has Lean 4 proofs of two models (see proofs/README.md).

Not done (next slices)

  • IMAP: server-side flag changes of messages already synced are not pulled, and messages expunged on the server stay in the Maildir; IDLE is not used.
  • Attachments in drafts, other charsets, and a sorted thread view.