Add the JMAP source read path #64

Merged
svc-agent-writer merged 1 commit from feat/jmap-source into main 2026-10-31 23:29:43 +00:00

Closes #55. Part of epic #51 (phase 6).

What this adds

A JMAP account mirrors into the same store as the IMAP accounts. One mail_search covers both. A JMAP id survives a move.

[[accounts]]
source = "jmap"
id = "fastmail"
display_name = "Fastmail"
credential_file = "/etc/mailmind/secrets/fastmail.secret"
  • Config. JmapAccountConfig sits beside the IMAP config, and source chooses between them. session_url defaults to Fastmail and must be https, or http on loopback for tests. The token file must not end in .token, because the k8s base routes those files to api.
  • Store, schema v6. A source_state table holds the Email state string, the mailbox id-to-path map and the per-account uid counter, as the #55 ruling decided. New methods cover stable-id flags, moves, deletes, renames, and the end of a switch.
  • jmap.py.
    • The client is httpx, with no redirects followed. Every URL the session names must keep its scheme.
    • Each pass reads every mailbox with Mailbox/get. A renamed mailbox renames its rows in place.
    • The first pass backfills with Email/query, then Email/get with fetchTextBodyValues and maxBodyValueBytes. An interrupted backfill resumes without duplicates.
    • Later passes read Email/changes and page while hasMoreChanges is true. cannotCalculateChanges, or a change in the wanted mailboxes, starts a full reconcile.
  • Switch from IMAP (ADR 0006 §1). The account id stays the same. Once the JMAP rows exist, each one copies the class and vector of its IMAP copy by Message-ID. Then the IMAP rows are deleted, so no Jev call and no model run happen again.
  • Writes settle as refused / unsupported until #57. EmailSubmission is never called. mailmind check warns when the token can send or change mail.
  • Errors.
    • 401 and 403 map to auth.
    • A timeout, a transport error, 429 and 5xx map to unavailable, which is retryable.
    • A bad shape maps to server_error.
    • The token is registered with the scrubber, and logs carry counts and codes only.

ADR 0006 §8 now records Mailbox/get each pass and the source_state table. README, config.example.toml, SECURITY.md and CLAUDE.md are updated. httpx is now a direct dependency (it was already in the lock through mcp).

Tests

tests/test_jmap.py runs against a fake JMAP server in the test process (19 tests). They cover:

  • backfill field mapping, including HTML bodies and nested mailbox paths;
  • backfill resume;
  • one search across an IMAP and a JMAP account;
  • the add, flags, move (same id) and del changes;
  • paging and renames;
  • cannotCalculateChanges, and narrowing the wanted mailboxes;
  • the switch from IMAP;
  • the auth, unavailable and scheme errors;
  • check and its CLI path;
  • refused writes;
  • config refusals.

Stalwart integration is #63.

  • make check: 163 passed, mypy and ruff clean, pip-audit clean.
  • make test-integration: 8 passed, so the IMAP path is unchanged.

Not in this PR

  • Push over EventSource (#56). A JMAP account polls at sync.interval_seconds.
  • JMAP writes (#57).

🤖 Generated with Claude Code

Closes #55. Part of epic #51 (phase 6). ## What this adds A JMAP account mirrors into the same store as the IMAP accounts. One `mail_search` covers both. A JMAP id survives a move. ```toml [[accounts]] source = "jmap" id = "fastmail" display_name = "Fastmail" credential_file = "/etc/mailmind/secrets/fastmail.secret" ``` - **Config.** `JmapAccountConfig` sits beside the IMAP config, and `source` chooses between them. `session_url` defaults to Fastmail and must be https, or http on loopback for tests. The token file must not end in `.token`, because the k8s base routes those files to `api`. - **Store, schema v6.** A `source_state` table holds the Email state string, the mailbox id-to-path map and the per-account uid counter, as the #55 ruling decided. New methods cover stable-id flags, moves, deletes, renames, and the end of a switch. - **`jmap.py`.** - The client is `httpx`, with no redirects followed. Every URL the session names must keep its scheme. - Each pass reads every mailbox with `Mailbox/get`. A renamed mailbox renames its rows in place. - The first pass backfills with `Email/query`, then `Email/get` with `fetchTextBodyValues` and `maxBodyValueBytes`. An interrupted backfill resumes without duplicates. - Later passes read `Email/changes` and page while `hasMoreChanges` is true. `cannotCalculateChanges`, or a change in the wanted mailboxes, starts a full reconcile. - **Switch from IMAP (ADR 0006 §1).** The account id stays the same. Once the JMAP rows exist, each one copies the class and vector of its IMAP copy by Message-ID. Then the IMAP rows are deleted, so no Jev call and no model run happen again. - **Writes** settle as `refused` / `unsupported` until #57. `EmailSubmission` is never called. `mailmind check` warns when the token can send or change mail. - **Errors.** - 401 and 403 map to `auth`. - A timeout, a transport error, 429 and 5xx map to `unavailable`, which is retryable. - A bad shape maps to `server_error`. - The token is registered with the scrubber, and logs carry counts and codes only. ADR 0006 §8 now records `Mailbox/get` each pass and the `source_state` table. README, `config.example.toml`, SECURITY.md and CLAUDE.md are updated. `httpx` is now a direct dependency (it was already in the lock through mcp). ## Tests `tests/test_jmap.py` runs against a fake JMAP server in the test process (19 tests). They cover: - backfill field mapping, including HTML bodies and nested mailbox paths; - backfill resume; - one search across an IMAP and a JMAP account; - the add, flags, move (same id) and del changes; - paging and renames; - `cannotCalculateChanges`, and narrowing the wanted mailboxes; - the switch from IMAP; - the auth, unavailable and scheme errors; - `check` and its CLI path; - refused writes; - config refusals. Stalwart integration is #63. - `make check`: 163 passed, mypy and ruff clean, pip-audit clean. - `make test-integration`: 8 passed, so the IMAP path is unchanged. ## Not in this PR - Push over EventSource (#56). A JMAP account polls at `sync.interval_seconds`. - JMAP writes (#57). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A JMAP account (source = "jmap", Fastmail by default) mirrors into the same store as IMAP accounts, with stable ids that survive a move.

- config: JmapAccountConfig beside the IMAP config, chosen by `source`; the token file must not end in .token; the session URL must be https, or http on loopback for tests.
- store: schema v6 adds source_state for state strings, the mailbox map and the uid counter, and methods for stable-id flags, moves, deletes, renames and the IMAP-to-JMAP switch.
- jmap: an httpx client with no redirects, and the source: Mailbox/get each pass, an Email/query and Email/get backfill that resumes, Email/changes deltas, and a full reconcile on cannotCalculateChanges.
- A switch from IMAP keeps each message's class and vector by Message-ID, then deletes the IMAP rows.
- Writes are refused as unsupported until #57. EmailSubmission is never called, and check warns when the token can send.

Closes #55

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.