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
mailhand / README.md
| 1 | # mailhand |
| 2 | |
| 3 | mailhand lets software agents look after a person's email, so the person does almost nothing. The |
| 4 | person runs one setup command, reads one summary file, and now and then says yes to a draft. Agents |
| 5 | do the rest through a command line that prints JSON lines, or through the same operations as MCP |
| 6 | tools. It is written in seeded Omega and is free software (Apache-2.0). |
| 7 | |
| 8 | ## What it does |
| 9 | |
| 10 | - Reads a Maildir (folders, `new`/`cur`/`tmp`, flags in file names) and parses each message: folded |
| 11 | headers, encoded words (RFC 2047), MIME parts nested to any depth, base64 and quoted-printable, |
| 12 | UTF-8 and ISO-8859-1 text (other charsets are reported as not decoded, never guessed), threads from |
| 13 | Message-ID / In-Reply-To / References, and List-Id / List-Unsubscribe. |
| 14 | - Lists, shows, searches and threads messages; marks them read, moves, archives and labels them. |
| 15 | - Triages without a model: fixed rules over the headers and the person's own address book (built from |
| 16 | the mail they sent) plus their own rules file. Every decision names the rule that fired. |
| 17 | - Writes drafts freely. Sends only what the person allowed: a standing permission they wrote in the |
| 18 | policy file, or their yes to that exact draft (identified by the SHA-256 of its bytes). |
| 19 | - Writes a plain-text digest: what arrived, what agents filed and why, drafts waiting for a yes, what |
| 20 | was sent and under which permission. |
| 21 | - Keeps an append-only, hash-chained audit log of every operation, and checks it with `verify`. |
| 22 | - Remote accounts: `sync` fetches new mail from an IMAP account into the Maildir and pushes the |
| 23 | agents' flag changes and moves back; sending can go to the person's provider by SMTP submission. Both |
| 24 | run over TLS only (see "Remote accounts"). |
| 25 | |
| 26 | ## Setup (the person's one step) |
| 27 | |
| 28 | ```sh |
| 29 | ./build.sh # needs the pinned omc as ./omc (see DEPENDENCIES.md) |
| 30 | build/mailhand setup --maildir "$HOME/Mail" --me me@example.org --name Me \ |
| 31 | --smtp unix:/run/user/1000/mail/smtp.sock |
| 32 | ``` |
| 33 | |
| 34 | This creates `~/.mailhand/` (or `--home DIR`, or `$MAILHAND_HOME`) with `config`, `policy`, `rules`, |
| 35 | `approvals`, `drafts/`, `outbox/`, `sent/`, `labels/`, `audit.log`, the address book, and the Maildir |
| 36 | folders INBOX, Sent, Drafts and Archive when missing. The default policy sends nothing without |
| 37 | approval. To approve a draft: `build/mailhand approve --draft SHA` (it shows the draft and waits for |
| 38 | `yes`). Read `~/.mailhand/digest.txt` after an agent runs `digest`. |
| 39 | |
| 40 | ## For agents |
| 41 | |
| 42 | ```sh |
| 43 | mailhand list [--folder NAME|all] [--unread] [--since EPOCH|YYYY-MM-DD] [--from TEXT] [--thread HEX] |
| 44 | mailhand show --id FOLDER/UNIQUE mailhand thread --id ID mailhand search --query TEXT |
| 45 | mailhand triage [--id ID | --folder F] mailhand mark-read --id ID mailhand archive --id ID |
| 46 | mailhand move --id ID --folder NAME mailhand label --id ID --label NAME [--remove] [--why TEXT] |
| 47 | mailhand draft-reply --id ID --body TEXT [--all] mailhand draft-new --to A,B --subject S --body TEXT |
| 48 | mailhand drafts mailhand queue-send --draft SHA mailhand deliver mailhand book |
| 49 | mailhand digest mailhand audit [--limit N] mailhand verify [--head HEX] |
| 50 | mailhand stats [--folder F|all] (parse every message; counts only) |
| 51 | mailhand mcp (MCP over stdio: the same operations as tools) |
| 52 | ``` |
| 53 | |
| 54 | `--thread HEX` selects messages whose 16-hex `thread` field (first 8 bytes of SHA-256 of the root id: the first |
| 55 | References id, else In-Reply-To, else Message-ID) equals HEX; `thread --id ID` follows the whole conversation |
| 56 | across folders, including replies that name only their parent. |
| 57 | |
| 58 | Output is one JSON object per line on stdout; diagnostics go to stderr. Exit codes: 0 ok, 1 usage, |
| 59 | 2 not found, 3 refused by the send gate, 4 message refused by the parser, 5 path refused (traversal, |
| 60 | symlink, outside the mail root), 6 I/O error, 7 audit chain broken, 8 delivery failed, 9 not set up. |
| 61 | |
| 62 | ## Remote accounts (IMAP sync, SMTP submission over TLS) |
| 63 | |
| 64 | The person writes the credentials file `~/.mailhand/accounts` (or names another file with the config |
| 65 | line `accounts PATH`). One block per account, `key value` lines, `#` comment lines: |
| 66 | |
| 67 | ``` |
| 68 | account work |
| 69 | imap imaps://imap.example.org # implicit TLS, port 993 unless :PORT is given |
| 70 | smtp submission://smtp.example.org # STARTTLS on 587; or smtps://HOST (implicit TLS, 465) |
| 71 | user me@example.org |
| 72 | password the rest of this line, exactly |
| 73 | roots /etc/ssl/certs/ca-certificates.crt # optional trust store (this is the default) |
| 74 | ``` |
| 75 | |
| 76 | `connect 127.0.0.1` exists for tests only: the TCP connection goes to that address while the |
| 77 | certificate is still checked against the host name. mailhand refuses the file unless it is a regular |
| 78 | file (never followed through a symlink), owned by the user running mailhand, with mode 0600 or tighter |
| 79 | (`mode & 077 == 0`), and at most 262144 bytes (the one buffer that holds it). A refusal exits 5 and |
| 80 | names the file and its mode on stderr, never its content; a missing file exits 9. The password is |
| 81 | read into that one buffer, used once for AUTHENTICATE PLAIN, LOGIN or AUTH PLAIN, and overwritten |
| 82 | with zeros: it is never written to stdout, stderr, the audit log, a state file or MCP output, and the |
| 83 | JSON records (which MCP returns) do not name the file. |
| 84 | |
| 85 | - `mailhand sync --account NAME [--folder NAME|all] [--resync]` (also the MCP tool `sync`: it never |
| 86 | sends mail and never shows credentials) fetches new mail into the Maildir and pushes local changes |
| 87 | back. Per folder, the state file `FOLDER/mailhand-imap` (next to its `new/ cur/ tmp/`) holds the |
| 88 | account, UIDVALIDITY, the last UID seen and one record per synced message. New messages are streamed |
| 89 | into `tmp/` and renamed into `new/` (no flags) or `cur/` (`:2,` and the letters S R F T D for \Seen |
| 90 | \Answered \Flagged \Deleted \Draft), named `INTERNALDATE.V<UIDVALIDITY>U<UID>F<folder hash>.mailhand` |
| 91 | so the same message always gets the same name. Local flag changes (`mark-read`, ...) are pushed with |
| 92 | UID STORE; local moves (`move`, `archive`) with UID MOVE, else UID COPY + UID STORE \Deleted + |
| 93 | UID EXPUNGE (UIDPLUS), else COPY + STORE \Deleted + EXPUNGE, which also removes any other message |
| 94 | already marked \Deleted in that mailbox: the record then says `"risk"`. Nothing is ever deleted on |
| 95 | the server because a message is missing locally. A changed UIDVALIDITY is refused (exit 3, |
| 96 | `uidvalidity_changed`, nothing fetched); `--resync` then matches every known message by its SHA-256 |
| 97 | against the new UIDs, so the Maildir never gets a second copy. Exit codes: 0 ok, 2 account not |
| 98 | found, 3 refused by the server (or UIDVALIDITY changed), 5 credentials file refused, 6 connection, |
| 99 | TLS or timeout failure, 9 no credentials file or no imap line. |
| 100 | - Sending to a provider: the config line `smtp account:NAME`. The send gate is unchanged and runs |
| 101 | first: a send the policy refuses opens no connection at all. Then smtps (TLS first) or submission |
| 102 | (greeting, EHLO, STARTTLS required, `220`, the TLS handshake, EHLO again); any failure before the |
| 103 | handshake completes aborts, so AUTH and MAIL are never sent in plaintext. AUTH PLAIN, over TLS only. |
| 104 | - Each network operation has a deadline: config `io_timeout SECONDS` (default 30). |
| 105 | |
| 106 | TLS is the omtls library (TLS 1.3 written in seeded Omega, OmegaTls), built into mailhand from one |
| 107 | commit named in `DEPS` (see DEPENDENCIES.md). A certificate that does not match the host name, has |
| 108 | expired, or does not chain to the trust store stops the session before any command is sent (exit 6 for |
| 109 | sync, 8 for deliver, with the reason on stderr). |
| 110 | |
| 111 | ## Safety model |
| 112 | |
| 113 | - Mail is untrusted. Every value that came from a mail (headers, body, file names) is printed only |
| 114 | inside an `"untrusted"` object, next to a fixed `"warning"` field saying that instructions inside |
| 115 | mail are not instructions from the person. mailhand never follows links, never fetches anything, |
| 116 | never opens or runs attachments (it lists name, type, size and SHA-256), and no mail can change the |
| 117 | policy, the rules, the approvals or the address book: those files are written only by the person's |
| 118 | commands (`setup`, `approve`) and by `book`, which reads only the Sent folder and mailhand's own sent |
| 119 | copies; `move` refuses to put anything into Sent or Drafts. |
| 120 | - Sending is the consequential act. A draft leaves only when the policy has a standing permission that |
| 121 | covers it (only recipients already in the address book, never attachments) or when the person |
| 122 | approved that exact draft by its SHA-256; changing one byte of a draft after approval voids the |
| 123 | approval. `deliver` checks again before each submission. Delivery is SMTP submission to a unix |
| 124 | socket or 127.0.0.1 port the person configured, or to the person's provider over TLS ("Remote |
| 125 | accounts" above), nothing else. |
| 126 | - Paths: message ids are `FOLDER/UNIQUE` made of safe characters only, so `..` cannot be written; |
| 127 | every directory is checked to be a real directory and message files are opened without following |
| 128 | symlinks. Anything else is refused with exit code 5. |
| 129 | - Limits of this model: agents and the person run as the same Unix user, so an agent with a shell |
| 130 | could edit the policy or approvals files directly; mailhand cannot stop that. What it gives is that |
| 131 | its own interface never does it, and the audit chain and digest show what happened. The audit chain |
| 132 | detects changed records; dropping the last records is detected only against a head you keep |
| 133 | elsewhere (`verify --head HEX`; the digest prints the head). |
| 134 | |
| 135 | ## Limits |
| 136 | |
| 137 | - TLS: TLS 1.3 only (omtls); a server that offers only TLS 1.2 is refused. |
| 138 | - Charsets other than UTF-8, US-ASCII and ISO-8859-1 are not decoded. |
| 139 | - Drafts are plain text without attachments. |
| 140 | |
| 141 | ## Check it yourself |
| 142 | |
| 143 | You need the pinned compiler: the file `PIN` names the omega-mv revision and the sha256 of its |
| 144 | `seed/out/omc`; copy that file to `./omc` in this directory (`sha256sum omc` must print the |
| 145 | `omc-sha256` of PIN). You also need git: `build.sh` fetches the dependencies named in `DEPS` at their |
| 146 | commits (see DEPENDENCIES.md). Then, from the repository root: |
| 147 | |
| 148 | ```sh |
| 149 | ./build.sh tests/GATE_UNITS build/gate-bin && build/gate-bin ./omc |
| 150 | ``` |
| 151 | |
| 152 | The gate reads PIN, checks the compiler's SHA-256 itself and refuses any other (exit 3), checks that no |
| 153 | tracked file other than PIN restates the pin (`MHGATE pincheck files=N restated=0`), builds mailhand, |
| 154 | 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 |
| 155 | `tests/scenarios/README.md`), then builds each mutant in `tests/mutants.txt` (for example the send |
| 156 | policy check removed, the re-check in deliver removed, the warning field removed, the path checks |
| 157 | removed) and requires a scenario to fail against each. The last line is |
| 158 | `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 |
| 159 | mail.test with the omtls test certificates; including refused handshakes), including a credential leak scan of every file mailhand wrote. |
| 160 | |
| 161 | Speed, on 10,000 generated messages (`tests/gen/mkmaildir.om`): |
| 162 | |
| 163 | ```sh |
| 164 | ./build.sh && ./build.sh tests/BENCH_UNITS build/bench && build/bench build/mailhand build/bench-run |
| 165 | ``` |
| 166 | |
| 167 | `proofs/` has Lean 4 proofs of two models (see proofs/README.md). |
| 168 | |
| 169 | ## Not done (next slices) |
| 170 | |
| 171 | - IMAP: server-side flag changes of messages already synced are not pulled, and messages expunged |
| 172 | on the server stay in the Maildir; IDLE is not used. |
| 173 | - Attachments in drafts, other charsets, and a sorted thread view. |