Add the write tools with scope, daily caps and a bounded wait #30

Merged
svc-agent-writer merged 1 commit from feat/write-tools into main 2026-10-31 04:50:22 +00:00

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

What changes

  • Five write tools: mail_draft, mail_flag, mail_move, mail_undo and mail_actions.
    • Each requires the write scope. build_app registers them only when auth.write_token_file is set.
    • Each validates its input, queues one row in actions, and polls for up to writes.wait_seconds (default 10) for the sync worker to settle it.
    • The answer is action=N kind=K state=applied, with draft_id= for a draft and ids= for a move. It is state=queued when the wait runs out.
    • A refused or failed action comes back as an Error: line that names the action id.
    • server.py still makes no IMAP call.
  • Refusals before queueing: bad recipients or a CR/LF, Trash or Junk targets, a target that is not mirrored, messages from more than one mailbox, and an undo of an undo, of an unapplied action, or of an action already undone. None of these uses up a cap.
  • Caps: [writes] sets per-day caps per kind: drafts 20, flags 100, moves 20, undos 50. The count and the insert share one transaction, so concurrent calls cannot pass the cap together. Over the cap the tool answers rate_limited and queues nothing.
  • Draft tag: every draft carries X-Mailmind-Action: <id>, so a draft the bot wrote can be told apart and matched to mail_actions. A subject tag would go out if the owner sent the draft, so there is none.
  • Audit: mail_actions lists id | kind | state | code | requested | applied | detail and never echoes a body.
  • Docs: README has a write-tool table. SECURITY.md replaces the "read-only IMAP" row with the write controls. config.example.toml gains [writes].

On "insufficient_scope"

FastMCP hides a tool from a token that lacks its scope. The read token therefore does not see the write tools in tools/list, and a call to one fails as an unknown tool. It does not get an insufficient_scope error. The tests assert that each call fails and that nothing is queued. For the starbase#271 evidence, record that a write tool is absent and its call fails, rather than an insufficient_scope string.

Tests

  • make check: 79 unit tests pass, plus ruff, mypy --strict and pip-audit. New tests cover:
    • the scope and the registration;
    • the cap at its limit;
    • queued when no worker runs;
    • undo once, and each refusal;
    • a reply draft through a stand-in worker.
  • make test-integration: 7 pass against GreenMail. The new one runs the deployed shape: an api on a read-only store and a separate run_forever worker loop. mail_draft over HTTP comes back applied, the server copy carries X-Mailmind-Action, and mail_undo removes the draft.

🤖 Generated with Claude Code

Closes #13. Part of the phase 2 epic (ADR 0004). ## What changes - **Five write tools:** `mail_draft`, `mail_flag`, `mail_move`, `mail_undo` and `mail_actions`. - Each requires the write scope. `build_app` registers them only when `auth.write_token_file` is set. - Each validates its input, queues one row in `actions`, and polls for up to `writes.wait_seconds` (default 10) for the sync worker to settle it. - The answer is `action=N kind=K state=applied`, with `draft_id=` for a draft and `ids=` for a move. It is `state=queued` when the wait runs out. - A refused or failed action comes back as an `Error:` line that names the action id. - `server.py` still makes no IMAP call. - **Refusals before queueing:** bad recipients or a CR/LF, Trash or Junk targets, a target that is not mirrored, messages from more than one mailbox, and an undo of an undo, of an unapplied action, or of an action already undone. None of these uses up a cap. - **Caps:** `[writes]` sets per-day caps per kind: drafts 20, flags 100, moves 20, undos 50. The count and the insert share one transaction, so concurrent calls cannot pass the cap together. Over the cap the tool answers `rate_limited` and queues nothing. - **Draft tag:** every draft carries `X-Mailmind-Action: <id>`, so a draft the bot wrote can be told apart and matched to `mail_actions`. A subject tag would go out if the owner sent the draft, so there is none. - **Audit:** `mail_actions` lists `id | kind | state | code | requested | applied | detail` and never echoes a body. - **Docs:** README has a write-tool table. SECURITY.md replaces the "read-only IMAP" row with the write controls. `config.example.toml` gains `[writes]`. ## On "insufficient_scope" FastMCP hides a tool from a token that lacks its scope. The read token therefore does not see the write tools in `tools/list`, and a call to one fails as an unknown tool. It does not get an `insufficient_scope` error. The tests assert that each call fails and that nothing is queued. For the starbase#271 evidence, record that a write tool is absent and its call fails, rather than an `insufficient_scope` string. ## Tests - `make check`: 79 unit tests pass, plus ruff, mypy --strict and pip-audit. New tests cover: - the scope and the registration; - the cap at its limit; - `queued` when no worker runs; - undo once, and each refusal; - a reply draft through a stand-in worker. - `make test-integration`: 7 pass against GreenMail. The new one runs the deployed shape: an api on a read-only store and a separate `run_forever` worker loop. `mail_draft` over HTTP comes back `applied`, the server copy carries `X-Mailmind-Action`, and `mail_undo` removes the draft. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- mail_draft, mail_flag, mail_move, mail_undo and mail_actions, each
  behind the write scope and registered only with a write token. They
  validate, queue one action and wait up to writes.wait_seconds for the
  sync worker; server.py still makes no IMAP call.
- [writes] sets per-day caps per kind (drafts 20, flags 100, moves 20,
  undos 50). The count and the insert share one transaction; over the
  cap the tool answers rate_limited and queues nothing.
- Every draft carries X-Mailmind-Action with its action id.
- README, SECURITY.md and config.example.toml describe the write path.

Closes #13

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