Add the IMAP write primitives and drain the outbox #29

Merged
svc-agent-writer merged 1 commit from feat/write-primitives into main 2026-10-31 04:33:21 +00:00

Closes #12. Part of the phase 2 epic (ADR 0004).

What changes

  • actions.py applies the four action kinds for the sync worker. No tool exposes them yet; #13 adds the tools.
    • draft: APPEND to the Drafts role with \Draft. From comes from from_address or the login. Recipients are one address each, and CR or LF anywhere is refused. A reply takes In-Reply-To, References and "Re:" from the mirrored message.
    • flag: \Seen and \Flagged only, in one mailbox, with UIDVALIDITY checked.
    • move: MOVE when offered. Otherwise COPY, then \Deleted and UID EXPUNGE of those UIDs only. With neither MOVE nor UIDPLUS the move is refused as unsupported. Trash and Junk are never targets, and the target must be mirrored. New UIDs come from COPYUID, else from a Message-ID search.
    • undo: replays the stored inverse once. A draft is removed only while it still has \Draft and its size is unchanged.
  • The worker drains queued actions before each pass. While it waits, in IDLE or in sleep, it checks the outbox every outbox_poll_seconds (default 2) and drains as soon as an action is queued.
  • Each action is applied at most once. A failure, a dropped connection included, settles as failed and is never retried, so a retry cannot append a second draft. Request errors settle as refused.
  • After a write, the worker mirrors the touched mailbox at once. If that refresh fails, the write still stands and the next pass catches up.
  • Schema: actions gains an account column. MessageRow exposes in_reply_to. Accounts take an optional from_address.

Note on schema v2

The account column changes the v2 actions table that #26 added. v2 has not been released, so no deployed mirror has the old shape. A local mirror created from main since #26 needs its actions table dropped, or a fresh mirror.

Tests

  • make check: 73 unit tests pass, plus ruff, mypy --strict and pip-audit.
  • make test-integration: 6 pass against GreenMail 2.1.15. They cover a reply draft and its undo, a changed draft left in place, a flag and its undo, a move and its undo on the MOVE path and on the COPY + UID EXPUNGE path, and the refusals for a Trash target and a stale UID. The tests delete the folders they create.

🤖 Generated with Claude Code

Closes #12. Part of the phase 2 epic (ADR 0004). ## What changes - `actions.py` applies the four action kinds for the sync worker. No tool exposes them yet; #13 adds the tools. - **draft:** APPEND to the Drafts role with `\Draft`. From comes from `from_address` or the login. Recipients are one address each, and CR or LF anywhere is refused. A reply takes In-Reply-To, References and "Re:" from the mirrored message. - **flag:** `\Seen` and `\Flagged` only, in one mailbox, with UIDVALIDITY checked. - **move:** MOVE when offered. Otherwise COPY, then `\Deleted` and UID EXPUNGE of those UIDs only. With neither MOVE nor UIDPLUS the move is refused as `unsupported`. Trash and Junk are never targets, and the target must be mirrored. New UIDs come from COPYUID, else from a Message-ID search. - **undo:** replays the stored inverse once. A draft is removed only while it still has `\Draft` and its size is unchanged. - The worker drains queued actions before each pass. While it waits, in IDLE or in sleep, it checks the outbox every `outbox_poll_seconds` (default 2) and drains as soon as an action is queued. - Each action is applied at most once. A failure, a dropped connection included, settles as `failed` and is never retried, so a retry cannot append a second draft. Request errors settle as `refused`. - After a write, the worker mirrors the touched mailbox at once. If that refresh fails, the write still stands and the next pass catches up. - Schema: `actions` gains an `account` column. `MessageRow` exposes `in_reply_to`. Accounts take an optional `from_address`. ## Note on schema v2 The `account` column changes the v2 `actions` table that #26 added. v2 has not been released, so no deployed mirror has the old shape. A local mirror created from `main` since #26 needs its `actions` table dropped, or a fresh mirror. ## Tests - `make check`: 73 unit tests pass, plus ruff, mypy --strict and pip-audit. - `make test-integration`: 6 pass against GreenMail 2.1.15. They cover a reply draft and its undo, a changed draft left in place, a flag and its undo, a move and its undo on the MOVE path and on the COPY + UID EXPUNGE path, and the refusals for a Trash target and a stale UID. The tests delete the folders they create. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- actions.py applies draft, flag, move and undo (ADR 0004). Drafts are
  composed with From from the config, CR/LF refused, recipients parsed
  one address each, replies threaded from the mirror. Flags are \Seen
  and \Flagged only. Moves use MOVE, else COPY + \Deleted + UID EXPUNGE
  of those UIDs only, and never target Trash or Junk. Each applied
  action stores its inverse; undo replays it once.
- The worker drains queued actions before each pass and when a wait
  sees one queued (outbox_poll_seconds), at most once each.
- actions gains an account column; MessageRow exposes in_reply_to;
  accounts take an optional from_address.
- GreenMail tests cover append, flag, both move paths and every undo.

Closes #12

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