Add JMAP push through EventSource #66

Merged
svc-agent-writer merged 1 commit from feat/jmap-push into main 2026-10-31 23:50:01 +00:00

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

Stacked on #65. This branch includes the Stalwart commit, because the push test runs on Stalwart. Merge #65 first; this PR then shows one commit.

What this adds

  • PushStream runs in a daemon thread per JMAP account. It opens the session's eventSourceUrl with types=Email,Mailbox, closeafter=no and ping=30.
    • The URL follows the same scheme rule as apiUrl, and the stream sends the same bearer token.
    • A StateChange that names Email or Mailbox for this account sets changed. Other accounts and other types are ignored.
    • Each connect sets changed too, including each reconnect, so mail that arrived while the stream was down is mirrored by the next pass.
    • A dropped or failed stream retries with doubling backoff, from 1 s up to 300 s. A server with no EventSource URL stops the thread, and the source polls.
  • JmapSource.wait starts the stream and then calls source.sleep, which now takes an optional changed event. The wait still checks the outbox every outbox_poll_seconds and still ends at interval_seconds. While the stream is down, it is the old sleep.
  • sync.idle = false turns push off, as it turns off IMAP IDLE. The config comment, README, config.example.toml, SECURITY.md (push is one more outbound request, with no inbound port), ADR 0006 §9 and CLAUDE.md are updated.
  • check reports push=yes|no.
  • httpx-sse is now a direct dependency (0.4.3, already in the lock through mcp).
  • Assessor and embedder nudges need no change. The worker loop nudges after any pass that adds mail, whichever source ran it.

Tests

  • Fake server, 9 new tests:
    • the stream query and the bearer token;
    • a pushed change for mail in Archive ends a 120-second wait in under 3 s;
    • type and account filtering;
    • a dropped stream reconnects and wakes a pass that mirrors mail sent while it was down;
    • a 503 stream falls back to the interval;
    • a queued action ends the wait while the stream is open;
    • idle = false opens no stream;
    • close stops reconnects;
    • event URLs with and without a template.
  • Stalwart: mail appended to Archive ends a 120-second wait in under 10 s, and the next pass mirrors it.
  • make check: 172 passed; ruff, mypy and pip-audit are clean.
  • make test-integration: 12 passed.

Not covered

  • The reconnect test runs on the fake server only. On Stalwart, killing the stream means restarting the container in the middle of a test.

🤖 Generated with Claude Code

Closes #56. Part of epic #51 (phase 6). **Stacked on #65.** This branch includes the Stalwart commit, because the push test runs on Stalwart. Merge #65 first; this PR then shows one commit. ## What this adds - **`PushStream`** runs in a daemon thread per JMAP account. It opens the session's `eventSourceUrl` with `types=Email,Mailbox`, `closeafter=no` and `ping=30`. - The URL follows the same scheme rule as `apiUrl`, and the stream sends the same bearer token. - A `StateChange` that names `Email` or `Mailbox` for this account sets `changed`. Other accounts and other types are ignored. - Each connect sets `changed` too, including each reconnect, so mail that arrived while the stream was down is mirrored by the next pass. - A dropped or failed stream retries with doubling backoff, from 1 s up to 300 s. A server with no EventSource URL stops the thread, and the source polls. - **`JmapSource.wait`** starts the stream and then calls `source.sleep`, which now takes an optional `changed` event. The wait still checks the outbox every `outbox_poll_seconds` and still ends at `interval_seconds`. While the stream is down, it is the old sleep. - **`sync.idle = false`** turns push off, as it turns off IMAP IDLE. The config comment, README, `config.example.toml`, SECURITY.md (push is one more outbound request, with no inbound port), ADR 0006 §9 and CLAUDE.md are updated. - **`check`** reports `push=yes|no`. - **`httpx-sse`** is now a direct dependency (0.4.3, already in the lock through mcp). - **Assessor and embedder nudges** need no change. The worker loop nudges after any pass that adds mail, whichever source ran it. ## Tests - **Fake server**, 9 new tests: - the stream query and the bearer token; - a pushed change for mail in Archive ends a 120-second wait in under 3 s; - type and account filtering; - a dropped stream reconnects and wakes a pass that mirrors mail sent while it was down; - a 503 stream falls back to the interval; - a queued action ends the wait while the stream is open; - `idle = false` opens no stream; - `close` stops reconnects; - event URLs with and without a template. - **Stalwart:** mail appended to Archive ends a 120-second wait in under 10 s, and the next pass mirrors it. - `make check`: 172 passed; ruff, mypy and pip-audit are clean. - `make test-integration`: 12 passed. ## Not covered - The reconnect test runs on the fake server only. On Stalwart, killing the stream means restarting the container in the middle of a test. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
scripts/stalwart.sh starts Stalwart 0.16.26, pinned by digest, with one test user over IMAPS and JMAP. Setup skips the web wizard: it submits the Bootstrap object, loads the blade's localhost certificate, and creates the user and an API key over the JMAP management API. The API key works as a bearer token on JMAP mail, so the client needs no change.

The integration tests run the JMAP source against it: backfill, a delta pass with an add, a flag change, a move that keeps its id, and a delete, and the switch from IMAP on one account, which keeps the triage class. make test-integration runs Stalwart beside GreenMail; make test-stalwart runs it alone.

Closes #63

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A JMAP source now waits on the session's EventSource stream for Email and Mailbox changes, so new mail in any mailbox starts a pass within seconds instead of at the next interval.

- The stream runs in a daemon thread per account, with a 30-second ping, and reconnects with doubling backoff. Each connect wakes one pass, so mail that arrived while it was down is mirrored.
- The wait still checks the outbox every outbox_poll_seconds and still ends at interval_seconds; with the stream down it is the plain sleep. sync.idle = false turns push off.
- check reports push=yes|no. Logs carry counts and codes only.
- httpx-sse becomes a direct dependency, as ADR 0006 §12 says.

Tested against the fake server (connect, change, drop and reconnect, failure fallback, outbox, idle off, close) and against Stalwart (mail appended to Archive ends a 120-second wait in seconds).

Closes #56

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