Epic: Write v2: Trash, forward drafts and draft edits (no SMTP) #187

Closed
opened 2026-11-12 02:14:16 +00:00 by svc-agent-writer · 1 comment

Problem Statement

The owner's agent needs a complete write path that never sends mail. Four capabilities exist since 0.2.0 (ADR 0004): drafts, flags, moves, and undo with an audit log. Three things the owner asks for in ordinary triage are missing:

  • The agent cannot delete. mail_move refuses Trash, and ADR 0004 requires a new ADR before any delete.
  • The agent cannot forward. A draft carries only the text the agent writes.
  • The agent cannot correct a draft. It must undo the draft and write a new one, which costs two actions and two cap counts.

Solution

This epic covers every write the service offers. Four capabilities already exist and need no code change. Three are new. All stay reversible, capped and audited through the outbox of ADR 0004. The service still never sends mail and never expunges a message it did not create.

Capability Tool State
New draft and reply draft mail_draft Exists
Seen, flagged and flag colour mail_flag Exists
Move between mirrored mailboxes mail_move Exists
Undo and audit log mail_undo, mail_actions Exists
Forward draft mail_draft with forward_id New
Edit a draft mail_draft_edit New
Move to Trash mail_trash New

User Stories

Drafts (exists)

  1. As an owner, I want my agent to write a new draft or a threaded reply into Drafts, so that I only review and send.
  2. As an owner, I want From to always be my account address, so that an agent cannot write as someone else.
  3. As an owner, I want a draft removed by undo only while I have not changed it, so that my edits are safe.

Flags (exists)

  1. As an owner, I want my agent to mark messages read or unread, so that my unread count shows what still needs me.
  2. As an owner, I want my agent to flag and unflag messages, so that I can find them in my mail app.
  3. As an owner, I want my agent to set one of the seven flag colours as iOS Mail shows them, so that the colours mean the same on my phone.
  4. As an owner, I want a colour never to mark a message read, so that read state changes only when I ask.
  5. As an owner, I want the undo of a flag change to keep any flag I changed since, so that undo never overrides me.
  6. As an agent, I want unsupported when a mailbox keeps no keywords, so that I can tell the owner a colour is not possible there.
  7. As an agent, I want no way to name a raw keyword, so that mail content cannot make me invent tags.

Moves (exists)

  1. As an owner, I want my agent to archive or file messages into a mirrored mailbox by name or role, so that my inbox stays short.
  2. As an owner, I want mail_undo to move them back, so that a wrong filing costs one call.
  3. As an agent, I want the result to carry the ids in the new mailbox, so that I can keep working on those messages.

Trash (new)

  1. As an owner, I want my agent to move named messages to Trash, so that I can clear mail by asking.
  2. As an owner, I want Trash to be the only delete, so that the server's Trash retention is my safety net.
  3. As an owner, I want mail_undo to move trashed messages back to their mailbox, so that a mistake costs one call.
  4. As an owner, I want an undo to touch nothing when I have already emptied Trash, so that an undo never acts on the wrong message.
  5. As an owner, I want Junk to stay forbidden, so that an agent cannot train my spam filter.
  6. As an owner, I want a small daily Trash cap counted in messages, so that a hostile email cannot steer the agent into mass deletion.
  7. As an owner on Fastmail, I want a trashed message that had several labels to get them all back on undo, so that undo restores the exact prior state.
  8. As an agent, I want mail_trash to take up to 100 ids in one mailbox, so that it matches mail_flag and mail_move.
  9. As an agent, I want the result line to name the action id, so that I can undo it later.
  10. As an agent, I want unsupported when the account has no single Trash mailbox, so that I can tell the owner why.
  11. As an agent, I want invalid_argument when a message is already in Trash, so that I do not spend the cap.
  12. As an agent, I want the trashed messages to leave search results when the mirror excludes Trash, so that reads match the mailbox.
  13. As a host application, I want mail_trash to carry the destructive hint, so that I can ask the owner to confirm.
  14. As a feed consumer, I want a trash to appear as a del or a move change, as the owner's own action would, so that my consumer needs no new event kind.

Forward (new)

  1. As an owner, I want my agent to prepare a forward of a message to a person I name, so that I only review and send.
  2. As an owner, I want the forward to quote the original sender, date, subject and text, so that the recipient has the context.
  3. As an owner, I want the draft to state when the original had attachments that are not included, so that I can attach them before I send.
  4. As an owner, I want the subject to default to Fwd: and the original subject, so that it reads as a normal forward.
  5. As an agent, I want to add my own text above the quoted block, so that I can write the covering note.
  6. As an agent, I want invalid_argument when I name both a reply and a forward, so that the draft has one purpose.
  7. As an agent, I want a long original to be cut at the body limit with a visible marker, so that the draft is never refused for size alone.

Edit (new)

  1. As an owner, I want my agent to correct a draft it wrote, so that I do not find two versions in Drafts.
  2. As an owner, I want an edit refused once I have changed the draft myself, so that my own words are never overwritten.
  3. As an owner, I want a failed edit to leave the old draft in place, so that a draft is never lost.
  4. As an owner, I want mail_undo of an edit to restore the previous text, so that every action stays reversible.
  5. As an agent, I want to name the draft by its action id, so that I can only edit what mailmind created.
  6. As an agent, I want the result line to carry the new draft_id, so that I can read the draft back.
  7. As an agent, I want an edit of a reply or forward to keep its threading headers, so that the thread does not break.
  8. As an agent, I want invalid_argument when I name an action whose draft a later action replaced, so that I act on the newest one.

Operation and audit

  1. As an operator, I want trash_per_day in [writes], so that I can tune or zero the cap.
  2. As an operator, I want an edit to count against drafts_per_day, so that there is no new cap to tune.
  3. As an operator, I want the new tools absent without a write token, so that a read-only instance does not change.
  4. As an operator, I want mail_actions to list trash and edit rows with counts and mailbox names only, so that the audit log carries no content.
  5. As an operator, I want logs to carry kinds, counts and codes only, so that the logging rule holds.
  6. As an operator, I want an older reader image to still open the store, so that rollback stays possible.
  7. As a security reviewer, I want SECURITY.md and the README to state exactly what a leaked write token can now do, so that the risk statement is true.
  8. As a maintainer, I want ADR 0013 to record the Trash exception and the two verbs, so that ADR 0004's "a new ADR for Trash" clause is met.

Implementation Decisions

Decision record

  • ADR 0013 amends ADR 0004 §1, §4 and its consequences. It keeps §2, §3, §5, §6, §7 and §8 unchanged.
  • The service has no SMTP path. Junk stays a forbidden target. No tool expunges mail it did not create.

Baseline (exists)

  • The four existing capabilities keep their tools, arguments, caps and result lines. No behaviour changes.
  • Tagging stays the closed set: seen, flagged and seven colours. ADR 0013 records that free keywords were considered and declined.

Trash

  • A new tool mail_trash(message_ids) and a new action kind trash. mail_move keeps refusing Trash and Junk, so the two intents stay separate in the tool list, the caps and the audit log.
  • The worker resolves the target by the trash role, whether or not the mirror includes that mailbox. Zero or several such mailboxes is unsupported.
  • IMAP: the same transport as a move (MOVE, or COPY with UID EXPUNGE of those UIDs only). The inverse stores the source mailbox, the Trash mailbox, its UIDVALIDITY and the new UIDs. A server that reports no new UIDs is unsupported, because undo would be a guess.
  • JMAP: one Email/set of mailboxIds with ifInState. The inverse stores each message's previous mailboxIds.
  • Undo: move back only the messages still in Trash at the stored identity. If none remain, the undo is refused with not_found and nothing changes.
  • Mirror: when the mirror excludes Trash, the rows leave the mirror in the same transaction as the action result. An undo restores them on the same terms as any message that arrives in a mirrored mailbox.
  • Cap: trash_per_day, default 20, counted in messages. Every other cap still counts requests. A request that would pass the cap is refused whole with rate_limited and queues nothing.
  • Tool annotation: destructive hint set. Every other write tool keeps its present hints.

Forward

  • mail_draft gains forward_id. It is exclusive with reply_to_id and implies the account.
  • Composition is pure and lives with the outbox rules. The quoted block holds four header lines (From, Date, Subject, To) and the mirrored body text.
  • A forward sets no In-Reply-To and no References.
  • No attachment bytes: the store holds text only (ADR 0007). When the original had attachments, the draft carries one line with their count and names.
  • The existing body limit applies to the whole draft. The quoted text is cut to fit and ends with a marker line.

Edit

  • A new tool mail_draft_edit(action_id, to, body, cc, subject) and a new action kind edit. The arguments replace the whole draft. Threading headers and forward context come from the action that is replaced.
  • The target must be an applied draft or edit action that holds the live draft. A replaced, undone or failed action is invalid_argument.
  • Guard: the same one as the undo of a draft. The old message must still carry the draft flag and its stored size (IMAP) or blob (JMAP).
  • Order: write the new draft first, then remove the old one. A crash between the two leaves two drafts, never none.
  • The draft id changes on both sources, because a JMAP Email is immutable. The result line carries the new id.
  • Undo of an edit removes the edited draft under the same guard and writes the previous content again.
  • An edit counts against drafts_per_day.

Shared

  • Both sources implement trash and edit behind the existing source apply interface. The tools still only queue.
  • mail_actions gains detail text for the two kinds: counts and mailbox names only.
  • The schema version rises only if the store constrains action kinds. If it rises, it adds nothing that an older reader needs.
  • The server instructions and each new docstring name the untrusted fields in one sentence.
  • Release as 0.14.0, with a README upgrade note.

Testing Decisions

A good test states what a caller or the mail server observes: the tool's answer, the action row, the mirror and the mailbox. It does not assert on private helpers.

Four seams, all existing. No new seam.

  1. Tools over HTTP with a stand-in worker. Registration with and without a write token, the read token refused, caps, every refusal that queues nothing, the result lines. Prior art: the write tools tests.
  2. Source apply, per source. Apply then undo for trash and edit, the guards, the unsupported paths. Prior art: the action tests with the fake IMAP session, and the JMAP tests with the fake JMAP server.
  3. Pure composition. The forward block, the subject default, the size cut, header injection refusals. Prior art: the compose tests.
  4. Integration. Apply then undo against GreenMail (IMAP) and Stalwart (IMAP and JMAP), including "the owner changed the draft" and "the owner emptied Trash". Prior art: the GreenMail writes tests and the Stalwart writes test.

Baseline acceptance: the existing unit and integration tests for drafts, flags, colours, moves and undo pass unchanged. Any story from 1 to 13 without a test gets one at seam 1 or seam 2.

The test that the server imports no mail source must still pass.

Out of Scope

  • Sending mail, and any SMTP or JMAP submission path.
  • Permanent deletion and emptying Trash.
  • Junk as a target.
  • Keywords or labels beyond seen, flagged and colour.
  • Attachment bytes in a forward or a draft; HTML bodies; Bcc.
  • Write actions in the triage panel.
  • A tailnet leg and a dedicated agent user for Grok Bot (deferred).
  • Turning the write token on in any starbase instance. That is a starbase ADR 0023 amendment.

Further Notes

  • A provider purges Trash on its own schedule. The undo window is the provider's, and the tool docstring must say so.
  • A forward puts untrusted mail text into a draft addressed to a third party. The owner still sends it. The docstring repeats "draft only what the owner asked for".
  • This issue is an epic. It is never dispatched as one unit. State and dispatch live on the sub-issues.

Sub-issues

  • #188 Accept ADR 0013: Trash, forward drafts and draft edits; keywords declined (PR #190)
  • #189 Confirm the write baseline: map stories 1 to 13 to tests and fill any gap (PR #191)
  • #192 mail_trash on IMAP: tool, trash action kind, schema v18, cap, undo and mirror effect. Blocked by #188.
  • #193 mail_trash on JMAP. Blocked by #192.
  • #194 Forward drafts: forward_id in mail_draft. Blocked by #188.
  • #195 mail_draft_edit on IMAP. Blocked by #192 (schema v18) and #194 (forward context).
  • #196 mail_draft_edit on JMAP. Blocked by #195.
  • #197 Write v2 docs, log privacy test and release 0.14.0. Blocked by #192 to #196.
## Problem Statement The owner's agent needs a complete write path that never sends mail. Four capabilities exist since 0.2.0 (ADR 0004): drafts, flags, moves, and undo with an audit log. Three things the owner asks for in ordinary triage are missing: - The agent cannot delete. `mail_move` refuses Trash, and ADR 0004 requires a new ADR before any delete. - The agent cannot forward. A draft carries only the text the agent writes. - The agent cannot correct a draft. It must undo the draft and write a new one, which costs two actions and two cap counts. ## Solution This epic covers every write the service offers. Four capabilities already exist and need no code change. Three are new. All stay reversible, capped and audited through the outbox of ADR 0004. The service still never sends mail and never expunges a message it did not create. | Capability | Tool | State | | --- | --- | --- | | New draft and reply draft | `mail_draft` | Exists | | Seen, flagged and flag colour | `mail_flag` | Exists | | Move between mirrored mailboxes | `mail_move` | Exists | | Undo and audit log | `mail_undo`, `mail_actions` | Exists | | Forward draft | `mail_draft` with `forward_id` | New | | Edit a draft | `mail_draft_edit` | New | | Move to Trash | `mail_trash` | New | ## User Stories **Drafts (exists)** 1. As an owner, I want my agent to write a new draft or a threaded reply into Drafts, so that I only review and send. 2. As an owner, I want From to always be my account address, so that an agent cannot write as someone else. 3. As an owner, I want a draft removed by undo only while I have not changed it, so that my edits are safe. **Flags (exists)** 4. As an owner, I want my agent to mark messages read or unread, so that my unread count shows what still needs me. 5. As an owner, I want my agent to flag and unflag messages, so that I can find them in my mail app. 6. As an owner, I want my agent to set one of the seven flag colours as iOS Mail shows them, so that the colours mean the same on my phone. 7. As an owner, I want a colour never to mark a message read, so that read state changes only when I ask. 8. As an owner, I want the undo of a flag change to keep any flag I changed since, so that undo never overrides me. 9. As an agent, I want `unsupported` when a mailbox keeps no keywords, so that I can tell the owner a colour is not possible there. 10. As an agent, I want no way to name a raw keyword, so that mail content cannot make me invent tags. **Moves (exists)** 11. As an owner, I want my agent to archive or file messages into a mirrored mailbox by name or role, so that my inbox stays short. 12. As an owner, I want `mail_undo` to move them back, so that a wrong filing costs one call. 13. As an agent, I want the result to carry the ids in the new mailbox, so that I can keep working on those messages. **Trash (new)** 14. As an owner, I want my agent to move named messages to Trash, so that I can clear mail by asking. 15. As an owner, I want Trash to be the only delete, so that the server's Trash retention is my safety net. 16. As an owner, I want `mail_undo` to move trashed messages back to their mailbox, so that a mistake costs one call. 17. As an owner, I want an undo to touch nothing when I have already emptied Trash, so that an undo never acts on the wrong message. 18. As an owner, I want Junk to stay forbidden, so that an agent cannot train my spam filter. 19. As an owner, I want a small daily Trash cap counted in messages, so that a hostile email cannot steer the agent into mass deletion. 20. As an owner on Fastmail, I want a trashed message that had several labels to get them all back on undo, so that undo restores the exact prior state. 21. As an agent, I want `mail_trash` to take up to 100 ids in one mailbox, so that it matches `mail_flag` and `mail_move`. 22. As an agent, I want the result line to name the action id, so that I can undo it later. 23. As an agent, I want `unsupported` when the account has no single Trash mailbox, so that I can tell the owner why. 24. As an agent, I want `invalid_argument` when a message is already in Trash, so that I do not spend the cap. 25. As an agent, I want the trashed messages to leave search results when the mirror excludes Trash, so that reads match the mailbox. 26. As a host application, I want `mail_trash` to carry the destructive hint, so that I can ask the owner to confirm. 27. As a feed consumer, I want a trash to appear as a `del` or a `move` change, as the owner's own action would, so that my consumer needs no new event kind. **Forward (new)** 28. As an owner, I want my agent to prepare a forward of a message to a person I name, so that I only review and send. 29. As an owner, I want the forward to quote the original sender, date, subject and text, so that the recipient has the context. 30. As an owner, I want the draft to state when the original had attachments that are not included, so that I can attach them before I send. 31. As an owner, I want the subject to default to `Fwd:` and the original subject, so that it reads as a normal forward. 32. As an agent, I want to add my own text above the quoted block, so that I can write the covering note. 33. As an agent, I want `invalid_argument` when I name both a reply and a forward, so that the draft has one purpose. 34. As an agent, I want a long original to be cut at the body limit with a visible marker, so that the draft is never refused for size alone. **Edit (new)** 35. As an owner, I want my agent to correct a draft it wrote, so that I do not find two versions in Drafts. 36. As an owner, I want an edit refused once I have changed the draft myself, so that my own words are never overwritten. 37. As an owner, I want a failed edit to leave the old draft in place, so that a draft is never lost. 38. As an owner, I want `mail_undo` of an edit to restore the previous text, so that every action stays reversible. 39. As an agent, I want to name the draft by its action id, so that I can only edit what mailmind created. 40. As an agent, I want the result line to carry the new `draft_id`, so that I can read the draft back. 41. As an agent, I want an edit of a reply or forward to keep its threading headers, so that the thread does not break. 42. As an agent, I want `invalid_argument` when I name an action whose draft a later action replaced, so that I act on the newest one. **Operation and audit** 43. As an operator, I want `trash_per_day` in `[writes]`, so that I can tune or zero the cap. 44. As an operator, I want an edit to count against `drafts_per_day`, so that there is no new cap to tune. 45. As an operator, I want the new tools absent without a write token, so that a read-only instance does not change. 46. As an operator, I want `mail_actions` to list trash and edit rows with counts and mailbox names only, so that the audit log carries no content. 47. As an operator, I want logs to carry kinds, counts and codes only, so that the logging rule holds. 48. As an operator, I want an older reader image to still open the store, so that rollback stays possible. 49. As a security reviewer, I want `SECURITY.md` and the README to state exactly what a leaked write token can now do, so that the risk statement is true. 50. As a maintainer, I want ADR 0013 to record the Trash exception and the two verbs, so that ADR 0004's "a new ADR for Trash" clause is met. ## Implementation Decisions **Decision record** - ADR 0013 amends ADR 0004 §1, §4 and its consequences. It keeps §2, §3, §5, §6, §7 and §8 unchanged. - The service has no SMTP path. Junk stays a forbidden target. No tool expunges mail it did not create. **Baseline (exists)** - The four existing capabilities keep their tools, arguments, caps and result lines. No behaviour changes. - Tagging stays the closed set: seen, flagged and seven colours. ADR 0013 records that free keywords were considered and declined. **Trash** - A new tool `mail_trash(message_ids)` and a new action kind `trash`. `mail_move` keeps refusing Trash and Junk, so the two intents stay separate in the tool list, the caps and the audit log. - The worker resolves the target by the `trash` role, whether or not the mirror includes that mailbox. Zero or several such mailboxes is `unsupported`. - IMAP: the same transport as a move (MOVE, or COPY with UID EXPUNGE of those UIDs only). The inverse stores the source mailbox, the Trash mailbox, its UIDVALIDITY and the new UIDs. A server that reports no new UIDs is `unsupported`, because undo would be a guess. - JMAP: one `Email/set` of `mailboxIds` with `ifInState`. The inverse stores each message's previous `mailboxIds`. - Undo: move back only the messages still in Trash at the stored identity. If none remain, the undo is refused with `not_found` and nothing changes. - Mirror: when the mirror excludes Trash, the rows leave the mirror in the same transaction as the action result. An undo restores them on the same terms as any message that arrives in a mirrored mailbox. - Cap: `trash_per_day`, default 20, counted in **messages**. Every other cap still counts requests. A request that would pass the cap is refused whole with `rate_limited` and queues nothing. - Tool annotation: destructive hint set. Every other write tool keeps its present hints. **Forward** - `mail_draft` gains `forward_id`. It is exclusive with `reply_to_id` and implies the account. - Composition is pure and lives with the outbox rules. The quoted block holds four header lines (From, Date, Subject, To) and the mirrored body text. - A forward sets no `In-Reply-To` and no `References`. - No attachment bytes: the store holds text only (ADR 0007). When the original had attachments, the draft carries one line with their count and names. - The existing body limit applies to the whole draft. The quoted text is cut to fit and ends with a marker line. **Edit** - A new tool `mail_draft_edit(action_id, to, body, cc, subject)` and a new action kind `edit`. The arguments replace the whole draft. Threading headers and forward context come from the action that is replaced. - The target must be an applied `draft` or `edit` action that holds the live draft. A replaced, undone or failed action is `invalid_argument`. - Guard: the same one as the undo of a draft. The old message must still carry the draft flag and its stored size (IMAP) or blob (JMAP). - Order: write the new draft first, then remove the old one. A crash between the two leaves two drafts, never none. - The draft id changes on both sources, because a JMAP Email is immutable. The result line carries the new id. - Undo of an edit removes the edited draft under the same guard and writes the previous content again. - An edit counts against `drafts_per_day`. **Shared** - Both sources implement `trash` and `edit` behind the existing source `apply` interface. The tools still only queue. - `mail_actions` gains detail text for the two kinds: counts and mailbox names only. - The schema version rises only if the store constrains action kinds. If it rises, it adds nothing that an older reader needs. - The server instructions and each new docstring name the untrusted fields in one sentence. - Release as 0.14.0, with a README upgrade note. ## Testing Decisions A good test states what a caller or the mail server observes: the tool's answer, the action row, the mirror and the mailbox. It does not assert on private helpers. Four seams, all existing. No new seam. 1. **Tools over HTTP with a stand-in worker.** Registration with and without a write token, the read token refused, caps, every refusal that queues nothing, the result lines. Prior art: the write tools tests. 2. **Source `apply`, per source.** Apply then undo for `trash` and `edit`, the guards, the `unsupported` paths. Prior art: the action tests with the fake IMAP session, and the JMAP tests with the fake JMAP server. 3. **Pure composition.** The forward block, the subject default, the size cut, header injection refusals. Prior art: the compose tests. 4. **Integration.** Apply then undo against GreenMail (IMAP) and Stalwart (IMAP and JMAP), including "the owner changed the draft" and "the owner emptied Trash". Prior art: the GreenMail writes tests and the Stalwart writes test. Baseline acceptance: the existing unit and integration tests for drafts, flags, colours, moves and undo pass unchanged. Any story from 1 to 13 without a test gets one at seam 1 or seam 2. The test that the server imports no mail source must still pass. ## Out of Scope - Sending mail, and any SMTP or JMAP submission path. - Permanent deletion and emptying Trash. - Junk as a target. - Keywords or labels beyond seen, flagged and colour. - Attachment bytes in a forward or a draft; HTML bodies; Bcc. - Write actions in the triage panel. - A tailnet leg and a dedicated agent user for Grok Bot (deferred). - Turning the write token on in any starbase instance. That is a starbase ADR 0023 amendment. ## Further Notes - A provider purges Trash on its own schedule. The undo window is the provider's, and the tool docstring must say so. - A forward puts untrusted mail text into a draft addressed to a third party. The owner still sends it. The docstring repeats "draft only what the owner asked for". - This issue is an epic. It is never dispatched as one unit. State and dispatch live on the sub-issues. ## Sub-issues - [x] #188 Accept ADR 0013: Trash, forward drafts and draft edits; keywords declined (PR #190) - [x] #189 Confirm the write baseline: map stories 1 to 13 to tests and fill any gap (PR #191) - [x] #192 `mail_trash` on IMAP: tool, trash action kind, schema v18, cap, undo and mirror effect. Blocked by #188. - [x] #193 `mail_trash` on JMAP. Blocked by #192. - [x] #194 Forward drafts: `forward_id` in `mail_draft`. Blocked by #188. - [x] #195 `mail_draft_edit` on IMAP. Blocked by #192 (schema v18) and #194 (forward context). - [x] #196 `mail_draft_edit` on JMAP. Blocked by #195. - [x] #197 Write v2 docs, log privacy test and release 0.14.0. Blocked by #192 to #196.
Author
Member

All 8 sub-issues are closed. Release 0.14.0 is tagged (v0.14.0 on 3ba9d20). Closing the epic.

All 8 sub-issues are closed. Release 0.14.0 is tagged (`v0.14.0` on `3ba9d20`). Closing the epic.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dev/mail-mind-mcp#187
No description provided.