mail_draft_edit on IMAP: tool, edit action kind, guard and undo #195

Closed
opened 2026-11-12 05:52:19 +00:00 by svc-agent-writer · 3 comments

Part of epic #187. Implements ADR 0013 item 8 for IMAP.

Read docs/adr/0013-trash-forward-and-draft-edits.md and ADR 0004 first. ADR 0013 is the contract. If the code shows that a ruling cannot work, stop and report; do not change the ruling here.

Standing rules: tools only queue to the outbox and never call a mail server. The sync worker applies each action through the existing source apply interface. Logs carry counts, roles and error codes only. Secrets are files. Add no new test seam: use the tools over HTTP with the stand-in worker, the source apply interface with the fake IMAP session or the fake JMAP server, and the GreenMail and Stalwart integration targets.

What to build

  1. Tool mail_draft_edit(action_id, to, body, cc, subject). Scope write. It queues an edit action. Schema v18 from the IMAP trash issue already admits the kind; add no schema change here.
  2. Target rule. The target must be an applied draft or edit action that holds the live draft this service created. A replaced, undone or failed action is invalid_argument, refused before it queues.
  3. Content. The edit replaces the whole draft. It keeps the threading headers and the forward context of the replaced action.
  4. IMAP guard and order. The old draft must still carry \Draft and its recorded size. Write the new draft first, then remove the old one. A crash between the two leaves two drafts, never none. The new UID gives a new draft id, returned in the result.
  5. Undo of an edit. Check the same guard on the edited draft, write the previous content again, then remove the edited draft (owner ruling of 2026-11-13; ADR 0013 item 8 is amended in PR #203). The restored draft is not a valid target for a further edit or undo (#204).
  6. Undo of a replaced action. mail_undo of a draft or edit that a later edit replaced is refused before it queues with invalid_argument.
  7. Cap. An edit counts with drafts against drafts_per_day.
  8. JMAP until its own issue lands. An edit action on a JMAP account is refused with unsupported.
  9. Update the worker undo dispatch, the per-kind branches of mail_undo and the action detail, and the mail_actions display for the new kind.

Notes from the earlier issues (#192, #194)

  • Schema v18 already admits the kind edit (PR #199). A test pins that the IMAP worker refuses an edit action with invalid_argument. Replace that test.
  • A forward draft stores a reference to the original message in the action params under the key forward, in the same shape as reply_to (PR #201). The quoted text is never in the params; each source loads the mirror row at apply time and calls the shared composition. An edit keeps the reply_to or forward reference of the action it replaces, so the new draft is composed the same way with the new to, cc, subject and body.
  • The worker reports the outcome that the store recorded (PR #202). Keep that.
  • The readers of draft params are the tool, the IMAP draft writer, the JMAP draft writer and the action detail. Check each for the new kind.

Acceptance criteria

  • Over HTTP: an edit of an applied draft queues one edit action. An edit of a replaced, undone, failed or foreign action is invalid_argument and queues nothing.
  • A chain of two edits: only the last action is the valid target for a third edit and for undo.
  • Fake IMAP session: the new draft is appended before the old one is removed. A draft the owner changed is left in place and the action is refused as for draft undo.
  • An edit of a reply keeps In-Reply-To and References. An edit of a forward keeps the forward block.
  • Undo of an edit restores the previous content and returns its new draft id.
  • Drafts plus edits together stop at drafts_per_day.
  • GreenMail: draft, edit, undo.
  • make check passes.

Out of scope

JMAP edits. Edits of drafts that this service did not write.

Blocked by

  • #188 (ADR 0013 merged and accepted)
  • #192 (schema v18)
  • #194 (forward context)
Part of epic #187. Implements ADR 0013 item 8 for IMAP. Read `docs/adr/0013-trash-forward-and-draft-edits.md` and ADR 0004 first. ADR 0013 is the contract. If the code shows that a ruling cannot work, stop and report; do not change the ruling here. Standing rules: tools only queue to the outbox and never call a mail server. The sync worker applies each action through the existing source `apply` interface. Logs carry counts, roles and error codes only. Secrets are files. Add no new test seam: use the tools over HTTP with the stand-in worker, the source `apply` interface with the fake IMAP session or the fake JMAP server, and the GreenMail and Stalwart integration targets. ## What to build 1. **Tool `mail_draft_edit(action_id, to, body, cc, subject)`.** Scope `write`. It queues an `edit` action. Schema v18 from the IMAP trash issue already admits the kind; add no schema change here. 2. **Target rule.** The target must be an applied `draft` or `edit` action that holds the live draft this service created. A replaced, undone or failed action is `invalid_argument`, refused before it queues. 3. **Content.** The edit replaces the whole draft. It keeps the threading headers and the forward context of the replaced action. 4. **IMAP guard and order.** The old draft must still carry `\Draft` and its recorded size. Write the new draft first, then remove the old one. A crash between the two leaves two drafts, never none. The new UID gives a new draft id, returned in the result. 5. **Undo of an edit.** Check the same guard on the edited draft, write the previous content again, then remove the edited draft (owner ruling of 2026-11-13; ADR 0013 item 8 is amended in PR #203). The restored draft is not a valid target for a further edit or undo (#204). 6. **Undo of a replaced action.** `mail_undo` of a draft or edit that a later edit replaced is refused before it queues with `invalid_argument`. 7. **Cap.** An edit counts with drafts against `drafts_per_day`. 8. **JMAP until its own issue lands.** An `edit` action on a JMAP account is refused with `unsupported`. 9. Update the worker undo dispatch, the per-kind branches of `mail_undo` and the action detail, and the `mail_actions` display for the new kind. ## Notes from the earlier issues (#192, #194) - Schema v18 already admits the kind `edit` (PR #199). A test pins that the IMAP worker refuses an `edit` action with `invalid_argument`. Replace that test. - A forward draft stores a reference to the original message in the action params under the key `forward`, in the same shape as `reply_to` (PR #201). The quoted text is never in the params; each source loads the mirror row at apply time and calls the shared composition. An edit keeps the `reply_to` or `forward` reference of the action it replaces, so the new draft is composed the same way with the new `to`, `cc`, `subject` and `body`. - The worker reports the outcome that the store recorded (PR #202). Keep that. - The readers of draft params are the tool, the IMAP draft writer, the JMAP draft writer and the action detail. Check each for the new kind. ## Acceptance criteria - [ ] Over HTTP: an edit of an applied draft queues one `edit` action. An edit of a replaced, undone, failed or foreign action is `invalid_argument` and queues nothing. - [ ] A chain of two edits: only the last action is the valid target for a third edit and for undo. - [ ] Fake IMAP session: the new draft is appended before the old one is removed. A draft the owner changed is left in place and the action is refused as for draft undo. - [ ] An edit of a reply keeps `In-Reply-To` and `References`. An edit of a forward keeps the forward block. - [ ] Undo of an edit restores the previous content and returns its new draft id. - [ ] Drafts plus edits together stop at `drafts_per_day`. - [ ] GreenMail: draft, edit, undo. - [ ] `make check` passes. ## Out of scope JMAP edits. Edits of drafts that this service did not write. ## Blocked by - #188 (ADR 0013 merged and accepted) - #192 (schema v18) - #194 (forward context)
Collaborator

⏸ SVD architect gate: dev/mail-mind-mcp#195 mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo

Status: CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity substrate · dispatch effort high

Run: 20261112T205047Z-e3b32389

Held because: CLEAN but 5 open risk(s) (1 verification · 4 decision) — HOLD-ALL parks everything. The risks, verbatim:

Open risks:

  1. Undo-of-edit order: issue item 5 says to remove the edited draft and then write the previous content. A crash between the two steps leaves zero drafts, which is the opposite of the edit's own 'two drafts, never none' rule. The previous content stays recoverable from the replaced action's params in the audit log. The lean follows the issue literally and does all composition before the removal. Question: keep remove-then-write, or should undo also append first and remove second? [decision]
  2. After an edit is undone, the restored draft's location is recorded on the undo row's inverse and reported as draft_id. Per ADR 0013 item 8, only an applied draft or edit is a valid target, so the restored draft cannot be edited or undone through the tools. Question: accept this literal reading, or plan an ADR amendment that makes an applied undo-of-edit a valid target? [decision]
  3. mail_draft_edit is annotated WRITE_NEW (destructiveHint=False, idempotentHint=False), because ADR 0013 item 6 reserves destructiveHint for mail_trash. An edit removes the old draft, but reversibly, as a move does. Question: is WRITE_NEW right, or should it be WRITE_DESTRUCTIVE? [decision]
  4. ADR 0013 Consequences lists docs follow-ups: README, CLAUDE.md, GLOSSARY, SECURITY.md, config.example.toml and the [writes] cap docs. Sibling PRs #200 and #201 left the README write-tools table without mail_trash or forward_id, so this phase defers docs too. Question: which epic-#187 issue owns the docs pass, or should a README row for mail_draft_edit be added here? [decision]
  5. The GreenMail acceptance criterion (draft, edit, undo) cannot run in the Half-A sandbox. Check: on the host, make test-integration passes, including tests/integration/test_greenmail_writes.py::test_draft_edit_then_undo, and make check passes. [verification]

Change: Edit params are {action: target id, to, cc, subject, body} plus the target's reply_to or forward ref, copied verbatim. draft_from (outbox.py:175) therefore composes drafts and edits the same way. An edit's inverse is the new draft's location, in the same {mailbox, uidvalidity, uid, size} shape _draft returns (actions.py:245), so the existing _undo_draft guard (actions.py:471-485) applies to edits unchanged.

IMAP runner (actions.py):

  • Split _draft into _compose and _append_draft, and split _undo_draft into _check_draft and _expunge_draft. Behaviour does not change.
  • _edit order: edit_target, then compose, then the UIDPLUS check, then guard the old draft, append the new draft, re-guard, and expunge the old draft. A refusal before the append changes nothing on the server.
  • _undo_edit order: re-compose the replaced action's params, then _undo_draft on the edit's draft, then append. It returns the restored location, which settle stores on the undo row.
  • _undo returns dict | None.

source.py: add edit_target, mirroring undo_target (source.py:120-133). It refuses an undone or replaced target, using the queued-earlier race rule. undo_target also refuses a replaced draft or edit.

store.py: add Store.edits_of, mirroring undos_of (store.py:3367) with json_extract(params,'$.action'). _day_count_sql and both of its call sites count kind IN ('draft','edit').

config.py: WritesConfig.cap('edit') returns drafts_per_day.

jmap.py: the edit kind is refused with unsupported.

server.py:

  • mail_draft_edit mirrors mail_draft (server.py:1535-1573), with a _live_draft target rule that mirrors mail_undo (server.py:1638-1652).
  • mail_undo refuses a replaced draft or edit.
  • _draft_id covers edit and undo rows; _action_detail gets an edit branch and adds draft_id to undo rows.
  • WRITE_TOOLS and TOOL_HINTS gain mail_draft_edit with WRITE_NEW.

Files: src/mail_mind_mcp/server.py · src/mail_mind_mcp/actions.py · src/mail_mind_mcp/source.py · src/mail_mind_mcp/store.py · src/mail_mind_mcp/config.py · src/mail_mind_mcp/jmap.py · tests/test_actions.py · tests/test_write_tools.py · tests/test_store.py · tests/test_jmap.py · tests/test_server.py · tests/test_config.py · tests/integration/test_greenmail_writes.py

Decision: accept the 5 open risk(s) above and dispatch this spec (svd:go), or park (svd:skip) / re-spec (svd:respec)?

Your call (when this issue is held at the gate via svd:hold):

  • add label svd:go — approve; Half-B dispatch accepts exactly this spec (open risks accepted)
  • add label svd:fire beside svd:go — approve AND authorise the Half-B fire from the phone; never on its own
  • add label svd:skip — park it; dispatch refuses it and the issue stays for a human
  • add label svd:respec — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context

Spec: /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo.md

### ⏸ SVD architect gate: dev/mail-mind-mcp#195 mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo **Status:** CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity `substrate` · dispatch effort `high` **Run:** `20261112T205047Z-e3b32389` **Held because:** CLEAN but 5 open risk(s) (1 verification · 4 decision) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. Undo-of-edit order: issue item 5 says to remove the edited draft and then write the previous content. A crash between the two steps leaves zero drafts, which is the opposite of the edit's own 'two drafts, never none' rule. The previous content stays recoverable from the replaced action's params in the audit log. The lean follows the issue literally and does all composition before the removal. Question: keep remove-then-write, or should undo also append first and remove second? [decision] 2. After an edit is undone, the restored draft's location is recorded on the undo row's inverse and reported as draft_id. Per ADR 0013 item 8, only an applied draft or edit is a valid target, so the restored draft cannot be edited or undone through the tools. Question: accept this literal reading, or plan an ADR amendment that makes an applied undo-of-edit a valid target? [decision] 3. mail_draft_edit is annotated WRITE_NEW (destructiveHint=False, idempotentHint=False), because ADR 0013 item 6 reserves destructiveHint for mail_trash. An edit removes the old draft, but reversibly, as a move does. Question: is WRITE_NEW right, or should it be WRITE_DESTRUCTIVE? [decision] 4. ADR 0013 Consequences lists docs follow-ups: README, CLAUDE.md, GLOSSARY, SECURITY.md, config.example.toml and the [writes] cap docs. Sibling PRs #200 and #201 left the README write-tools table without mail_trash or forward_id, so this phase defers docs too. Question: which epic-#187 issue owns the docs pass, or should a README row for mail_draft_edit be added here? [decision] 5. The GreenMail acceptance criterion (draft, edit, undo) cannot run in the Half-A sandbox. Check: on the host, make test-integration passes, including tests/integration/test_greenmail_writes.py::test_draft_edit_then_undo, and make check passes. [verification] **Change:** Edit params are {action: target id, to, cc, subject, body} plus the target's reply_to or forward ref, copied verbatim. draft_from (outbox.py:175) therefore composes drafts and edits the same way. An edit's inverse is the new draft's location, in the same {mailbox, uidvalidity, uid, size} shape _draft returns (actions.py:245), so the existing _undo_draft guard (actions.py:471-485) applies to edits unchanged. IMAP runner (actions.py): - Split _draft into _compose and _append_draft, and split _undo_draft into _check_draft and _expunge_draft. Behaviour does not change. - _edit order: edit_target, then compose, then the UIDPLUS check, then guard the old draft, append the new draft, re-guard, and expunge the old draft. A refusal before the append changes nothing on the server. - _undo_edit order: re-compose the replaced action's params, then _undo_draft on the edit's draft, then append. It returns the restored location, which settle stores on the undo row. - _undo returns dict | None. source.py: add edit_target, mirroring undo_target (source.py:120-133). It refuses an undone or replaced target, using the queued-earlier race rule. undo_target also refuses a replaced draft or edit. store.py: add Store.edits_of, mirroring undos_of (store.py:3367) with json_extract(params,'$.action'). _day_count_sql and both of its call sites count kind IN ('draft','edit'). config.py: WritesConfig.cap('edit') returns drafts_per_day. jmap.py: the edit kind is refused with unsupported. server.py: - mail_draft_edit mirrors mail_draft (server.py:1535-1573), with a _live_draft target rule that mirrors mail_undo (server.py:1638-1652). - mail_undo refuses a replaced draft or edit. - _draft_id covers edit and undo rows; _action_detail gets an edit branch and adds draft_id to undo rows. - WRITE_TOOLS and TOOL_HINTS gain mail_draft_edit with WRITE_NEW. **Files:** `src/mail_mind_mcp/server.py` · `src/mail_mind_mcp/actions.py` · `src/mail_mind_mcp/source.py` · `src/mail_mind_mcp/store.py` · `src/mail_mind_mcp/config.py` · `src/mail_mind_mcp/jmap.py` · `tests/test_actions.py` · `tests/test_write_tools.py` · `tests/test_store.py` · `tests/test_jmap.py` · `tests/test_server.py` · `tests/test_config.py` · `tests/integration/test_greenmail_writes.py` **Decision:** accept the 5 open risk(s) above and dispatch this spec (`svd:go`), or park (`svd:skip`) / re-spec (`svd:respec`)? **Your call** (when this issue is held at the gate via `svd:hold`): - add label `svd:go` — approve; Half-B dispatch accepts exactly this spec (open risks accepted) - add label `svd:fire` **beside** `svd:go` — approve AND authorise the Half-B fire from the phone; never on its own - add label `svd:skip` — park it; dispatch refuses it and the issue stays for a human - add label `svd:respec` — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context <sub>Spec: `/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo.md`</sub>
Collaborator

✅ SVD architect gate: dev/mail-mind-mcp#195 mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo

Status: CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity unclassified · dispatch effort unclassified

Run: 20261112T205047Z-e3b32389

Files: src/mail_mind_mcp/server.py · src/mail_mind_mcp/actions.py · src/mail_mind_mcp/source.py · src/mail_mind_mcp/store.py · src/mail_mind_mcp/config.py · src/mail_mind_mcp/jmap.py · tests/test_actions.py · tests/test_write_tools.py · tests/test_store.py · tests/test_jmap.py · tests/test_server.py · tests/test_config.py · tests/integration/test_greenmail_writes.py

Write-set divergence: present table wins. 0 sourceHashes vs 20-row table.

Decision: dispatch this spec (svd:go)?

Your call (when this issue is held at the gate via svd:hold):

  • add label svd:go — approve; Half-B dispatch accepts exactly this spec (open risks accepted)
  • add label svd:fire beside svd:go — approve AND authorise the Half-B fire from the phone; never on its own
  • add label svd:skip — park it; dispatch refuses it and the issue stays for a human
  • add label svd:respec — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context

Spec: /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo.md

### ✅ SVD architect gate: dev/mail-mind-mcp#195 mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo **Status:** CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity `unclassified` · dispatch effort `unclassified` **Run:** `20261112T205047Z-e3b32389` **Files:** `src/mail_mind_mcp/server.py` · `src/mail_mind_mcp/actions.py` · `src/mail_mind_mcp/source.py` · `src/mail_mind_mcp/store.py` · `src/mail_mind_mcp/config.py` · `src/mail_mind_mcp/jmap.py` · `tests/test_actions.py` · `tests/test_write_tools.py` · `tests/test_store.py` · `tests/test_jmap.py` · `tests/test_server.py` · `tests/test_config.py` · `tests/integration/test_greenmail_writes.py` **Write-set divergence:** present table wins. 0 sourceHashes vs 20-row table. **Decision:** dispatch this spec (`svd:go`)? **Your call** (when this issue is held at the gate via `svd:hold`): - add label `svd:go` — approve; Half-B dispatch accepts exactly this spec (open risks accepted) - add label `svd:fire` **beside** `svd:go` — approve AND authorise the Half-B fire from the phone; never on its own - add label `svd:skip` — park it; dispatch refuses it and the issue stays for a human - add label `svd:respec` — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context <sub>Spec: `/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo.md`</sub>
Collaborator

spec-verify-dispatch Half B for dev/mail-mind-mcp#195 mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo completed integration verify.

  • PR: #205
  • Branch: feat/svd-dev-mail-mind-mcp-195-mail-draft-edit-on-imap-tool-e-d2d9eeb4
  • Commit: 94a82883f4
  • Build: none
  • Test: green
  • Reader audit clean: True
  • Spec fidelity: True
  • Ready for merge: True
  • Check ci/woodpecker/pr/validation: green https://ci.groupthink.asia/repos/72/pipeline/87/1
  • Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll
  • Warning: GreenMail integration test test_draft_edit_then_undo exists but was not run (Podman socket unavailable per subagent); it needs a host with ../imap-blade-mcp.
  • CI wait: ci/woodpecker/pr/validation -> pass
  • Decision: review the PR and merge? (codex-svd ship --run-id 20261112T211050Z-d2d9eeb4 --confirm-merge)
spec-verify-dispatch Half B for dev/mail-mind-mcp#195 mail-draft-edit-on-imap-tool-edit-action-kind-guard-and-undo completed integration verify. - PR: https://git.groupthink.asia/dev/mail-mind-mcp/pulls/205 - Branch: feat/svd-dev-mail-mind-mcp-195-mail-draft-edit-on-imap-tool-e-d2d9eeb4 - Commit: 94a82883f4018d4e99dcad7207c111ee62a3dac0 - Build: none - Test: green - Reader audit clean: True - Spec fidelity: True - Ready for merge: True - Check ci/woodpecker/pr/validation: green https://ci.groupthink.asia/repos/72/pipeline/87/1 - Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll - Warning: GreenMail integration test test_draft_edit_then_undo exists but was not run (Podman socket unavailable per subagent); it needs a host with ../imap-blade-mcp. - CI wait: ci/woodpecker/pr/validation -> pass - Decision: review the PR and merge? (`codex-svd ship --run-id 20261112T211050Z-d2d9eeb4 --confirm-merge`)
svc-svd-writer 2026-11-12 21:33:54 +00:00
  • closed this issue
  • removed the
    svd:go
    label
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
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#195
No description provided.