mail_trash on JMAP: apply, undo and mirror effect #193

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

Part of epic #187. Implements ADR 0013 items 2 to 4 for JMAP.

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. JMAP apply. Replace the unsupported refusal from the IMAP trash issue. Resolve exactly one mailbox with the trash role; zero or several is unsupported. Send one Email/set with ifInState that replaces every mailbox membership of each message with Trash. Record the prior mailboxIds set of each message as the inverse.
  2. JMAP undo. Restore only the memberships that the trash action removed, and only for messages still in Trash. If none remain, refuse with not_found and change nothing.
  3. Mirror effect. The JMAP mirror helper handles per-path mailbox patches today. Make the trash outcome remove the rows when the mirror excludes Trash, in the same transaction as the action result. After an undo, the next pass adds the unknown ids again. Never refresh or mirror an excluded Trash.
  4. ADR 0006 §10. It says that JMAP writes keep ADR 0004 unchanged. Add one line that points to ADR 0013. Change no other ADR text.

Notes from the IMAP issue (#192, PRs #199 and #200)

  • The store has one settle path for trash that records the result, removes the source rows and writes the change and peer deletion events in one transaction. The worker calls it for a trash action that returns an inverse. Reuse both; add no second path.
  • The worker ignores the return value of that settle call. When the action was already settled, the call changes nothing, but the worker still reports the new state. Make the worker report the outcome that the store recorded, and add a test.
  • On IMAP, trash requires UIDPLUS (ADR 0013 item 2 as amended). That rule is IMAP only. JMAP keeps the Email id, so it needs no such rule.
  • The JMAP source now refuses trash with unsupported, and a test pins that. Replace the test.

Acceptance criteria

  • Fake JMAP server: apply sends one Email/set with ifInState; a message in two mailboxes ends in Trash only; the inverse holds both prior memberships.
  • A state mismatch gives the same outcome as the existing JMAP move.
  • Undo restores both memberships. A message the owner removed from Trash is skipped. None left gives not_found.
  • Zero and two trash-role mailboxes give unsupported.
  • With Trash excluded, the rows leave the mirror on apply and return on the pass after undo.
  • Stalwart: trash then undo against the real server.
  • make check passes.

Out of scope

Any change to the tool, the cap or the schema.

Blocked by

  • #188 (ADR 0013 merged and accepted)
  • #192
Part of epic #187. Implements ADR 0013 items 2 to 4 for JMAP. 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. **JMAP apply.** Replace the `unsupported` refusal from the IMAP trash issue. Resolve exactly one mailbox with the trash role; zero or several is `unsupported`. Send one `Email/set` with `ifInState` that replaces every mailbox membership of each message with Trash. Record the prior `mailboxIds` set of each message as the inverse. 2. **JMAP undo.** Restore only the memberships that the trash action removed, and only for messages still in Trash. If none remain, refuse with `not_found` and change nothing. 3. **Mirror effect.** The JMAP mirror helper handles per-path mailbox patches today. Make the trash outcome remove the rows when the mirror excludes Trash, in the same transaction as the action result. After an undo, the next pass adds the unknown ids again. Never refresh or mirror an excluded Trash. 4. **ADR 0006 §10.** It says that JMAP writes keep ADR 0004 unchanged. Add one line that points to ADR 0013. Change no other ADR text. ## Notes from the IMAP issue (#192, PRs #199 and #200) - The store has one settle path for trash that records the result, removes the source rows and writes the change and peer deletion events in one transaction. The worker calls it for a `trash` action that returns an inverse. Reuse both; add no second path. - The worker ignores the return value of that settle call. When the action was already settled, the call changes nothing, but the worker still reports the new state. Make the worker report the outcome that the store recorded, and add a test. - On IMAP, trash requires UIDPLUS (ADR 0013 item 2 as amended). That rule is IMAP only. JMAP keeps the Email id, so it needs no such rule. - The JMAP source now refuses `trash` with `unsupported`, and a test pins that. Replace the test. ## Acceptance criteria - [ ] Fake JMAP server: apply sends one `Email/set` with `ifInState`; a message in two mailboxes ends in Trash only; the inverse holds both prior memberships. - [ ] A state mismatch gives the same outcome as the existing JMAP move. - [ ] Undo restores both memberships. A message the owner removed from Trash is skipped. None left gives `not_found`. - [ ] Zero and two trash-role mailboxes give `unsupported`. - [ ] With Trash excluded, the rows leave the mirror on apply and return on the pass after undo. - [ ] Stalwart: trash then undo against the real server. - [ ] `make check` passes. ## Out of scope Any change to the tool, the cap or the schema. ## Blocked by - #188 (ADR 0013 merged and accepted) - #192
Collaborator

⏸ SVD architect gate: dev/mail-mind-mcp#193 mail-trash-on-jmap-apply-undo-and-mirror-effect

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: 20261112T195614Z-8b46fe9f

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

Open risks:

  1. ADR 0013 still reads 'Status: proposed' (docs/adr/0013-trash-forward-and-draft-edits.md:3), and ADR 0004 links it as '(proposed)'. Blocker #188 says 'merged and accepted', and the IMAP work (#200) already built on it. Question: is ADR 0013 accepted for dispatch? This spec leaves the status line alone because the issue says to change no other ADR text. [decision]
  2. ADR 0013 item 3 does not cover a JMAP trash undo where a prior mailbox no longer exists. The lean restores the surviving prior memberships and leaves a message whose prior mailboxes are all gone in Trash; if every message is skipped, the result is not_found. Question: accept the skip-per-message lean, or refuse the whole undo with not_found as _undo_move does (jmap.py:886-887)? [decision]
  3. When Trash is mirrored (not the default), the lean moves the rows into Trash in place through _mirror → record_move. That runs in its own transaction before settle, as the existing JMAP move does; ADR 0013 item 4 constrains only the excluded case. Question: is a separate-transaction move acceptable for a mirrored Trash, or must it also join finish_trash (which would need a record_move extraction)? [decision]
  4. The Stalwart integration test assumes Stalwart's 'Deleted Items' reports JMAP role 'trash' through Mailbox/get. Check: run make test-stalwart after dispatch and confirm that test_trash_and_its_undo passes rather than being refused as unsupported. [verification]
  5. No pass/fail baseline was measured: the Half-A sandbox has no .venv and the target is read-only. Check: on the pristine base 05ef02c, run make install && uv run python3 -m pytest -q -m "not integration" tests/test_jmap.py tests/test_store.py tests/test_actions.py before editing, then confirm make check passes after the change with only test_trash_is_unsupported_on_jmap replaced. [verification]

Change: Extend the single trash settle path rather than adding a second one, using the same extraction #200 did for delete_uids → _delete_uid_rows (store.py:1451-1483).

store.py: extract the body of delete_sources (store.py:2137-2167) into _delete_source_rows(conn, account, source_ids, at). Give finish_trash (store.py:3389) a source_ids: Iterable[str] = () keyword and delete those rows in the same transaction. This keeps the action result, the row deletes, the changes 'del' rows with source_id, and the peer del events in one transaction. Deleting through _delete_uid_rows instead would log the JMAP change without source_id, which gives the wrong public id.

source.py _settle_trash (159-177): branch on "source_ids" in inverse, the same test server._outcome uses at server.py:1522. A JMAP inverse is always applied, passes uids=[] and source_ids=inverse["dropped"]. The IMAP branch is unchanged. When finish_trash returns False, return and log the state the store holds (store.get_action), not the state this call meant to write.

jmap.py: remove the refusal at :491-492 and dispatch trash after the read-only check. Add _trash, mirroring _move (854-880):

  • resolve exactly one trash-role mailbox from _read_mailboxes()[1]; zero or several give unsupported;
  • read _current, then send one ifInState _set with per-path patches that null every prior membership and set Trash;
  • call _mirror only when Trash is wanted (the row moves in place);
  • return the inverse {from, from_uidvalidity:0, from_uids (row uids, for server trashed=N), to, to_id, source_ids, previous (full prior mailboxIds), dropped (source_ids when Trash is excluded, else [])}.

Add _undo_trash. It reads with a tolerant Email/get and skips ids that were destroyed or are no longer in Trash. It restores the prior memberships that still exist, removes Trash only if it was not a prior membership, and raises not_found when no patch remains. It sends one ifInState _set, then calls _mirror (a no-op for dropped rows; the next pass re-adds unknown ids).

ADR 0006 §10: add one sub-bullet line pointing to ADR 0013.

Files: src/mail_mind_mcp/jmap.py · src/mail_mind_mcp/source.py · src/mail_mind_mcp/store.py · tests/test_jmap.py · tests/test_store.py · tests/integration/test_stalwart.py · docs/adr/0006-pluggable-mail-sources.md

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-trash-on-jmap-apply-undo-and-mirror-effect.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-trash-on-jmap-apply-undo-and-mirror-effect.md

### ⏸ SVD architect gate: dev/mail-mind-mcp#193 mail-trash-on-jmap-apply-undo-and-mirror-effect **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:** `20261112T195614Z-8b46fe9f` **Held because:** CLEAN but 5 open risk(s) (2 verification · 3 decision) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. ADR 0013 still reads 'Status: proposed' (docs/adr/0013-trash-forward-and-draft-edits.md:3), and ADR 0004 links it as '(proposed)'. Blocker #188 says 'merged and accepted', and the IMAP work (#200) already built on it. Question: is ADR 0013 accepted for dispatch? This spec leaves the status line alone because the issue says to change no other ADR text. [decision] 2. ADR 0013 item 3 does not cover a JMAP trash undo where a prior mailbox no longer exists. The lean restores the surviving prior memberships and leaves a message whose prior mailboxes are all gone in Trash; if every message is skipped, the result is not_found. Question: accept the skip-per-message lean, or refuse the whole undo with not_found as _undo_move does (jmap.py:886-887)? [decision] 3. When Trash is mirrored (not the default), the lean moves the rows into Trash in place through _mirror → record_move. That runs in its own transaction before settle, as the existing JMAP move does; ADR 0013 item 4 constrains only the excluded case. Question: is a separate-transaction move acceptable for a mirrored Trash, or must it also join finish_trash (which would need a record_move extraction)? [decision] 4. The Stalwart integration test assumes Stalwart's 'Deleted Items' reports JMAP role 'trash' through Mailbox/get. Check: run `make test-stalwart` after dispatch and confirm that test_trash_and_its_undo passes rather than being refused as unsupported. [verification] 5. No pass/fail baseline was measured: the Half-A sandbox has no .venv and the target is read-only. Check: on the pristine base 05ef02c, run `make install && uv run python3 -m pytest -q -m "not integration" tests/test_jmap.py tests/test_store.py tests/test_actions.py` before editing, then confirm `make check` passes after the change with only test_trash_is_unsupported_on_jmap replaced. [verification] **Change:** Extend the single trash settle path rather than adding a second one, using the same extraction #200 did for delete_uids → _delete_uid_rows (store.py:1451-1483). store.py: extract the body of delete_sources (store.py:2137-2167) into _delete_source_rows(conn, account, source_ids, at). Give finish_trash (store.py:3389) a `source_ids: Iterable[str] = ()` keyword and delete those rows in the same transaction. This keeps the action result, the row deletes, the changes 'del' rows with source_id, and the peer del events in one transaction. Deleting through _delete_uid_rows instead would log the JMAP change without source_id, which gives the wrong public id. source.py _settle_trash (159-177): branch on `"source_ids" in inverse`, the same test server._outcome uses at server.py:1522. A JMAP inverse is always applied, passes uids=[] and source_ids=inverse["dropped"]. The IMAP branch is unchanged. When finish_trash returns False, return and log the state the store holds (store.get_action), not the state this call meant to write. jmap.py: remove the refusal at :491-492 and dispatch trash after the read-only check. Add _trash, mirroring _move (854-880): - resolve exactly one trash-role mailbox from _read_mailboxes()[1]; zero or several give unsupported; - read _current, then send one ifInState _set with per-path patches that null every prior membership and set Trash; - call _mirror only when Trash is wanted (the row moves in place); - return the inverse {from, from_uidvalidity:0, from_uids (row uids, for server trashed=N), to, to_id, source_ids, previous (full prior mailboxIds), dropped (source_ids when Trash is excluded, else [])}. Add _undo_trash. It reads with a tolerant Email/get and skips ids that were destroyed or are no longer in Trash. It restores the prior memberships that still exist, removes Trash only if it was not a prior membership, and raises not_found when no patch remains. It sends one ifInState _set, then calls _mirror (a no-op for dropped rows; the next pass re-adds unknown ids). ADR 0006 §10: add one sub-bullet line pointing to ADR 0013. **Files:** `src/mail_mind_mcp/jmap.py` · `src/mail_mind_mcp/source.py` · `src/mail_mind_mcp/store.py` · `tests/test_jmap.py` · `tests/test_store.py` · `tests/integration/test_stalwart.py` · `docs/adr/0006-pluggable-mail-sources.md` **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-trash-on-jmap-apply-undo-and-mirror-effect.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-trash-on-jmap-apply-undo-and-mirror-effect.md`</sub>
Collaborator

✅ SVD architect gate: dev/mail-mind-mcp#193 mail-trash-on-jmap-apply-undo-and-mirror-effect

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: 20261112T195614Z-8b46fe9f

Files: src/mail_mind_mcp/jmap.py · src/mail_mind_mcp/source.py · src/mail_mind_mcp/store.py · tests/test_jmap.py · tests/test_store.py · tests/integration/test_stalwart.py

Write-set divergence: present table wins. 0 sourceHashes vs 17-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-trash-on-jmap-apply-undo-and-mirror-effect.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-trash-on-jmap-apply-undo-and-mirror-effect.md

### ✅ SVD architect gate: dev/mail-mind-mcp#193 mail-trash-on-jmap-apply-undo-and-mirror-effect **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:** `20261112T195614Z-8b46fe9f` **Files:** `src/mail_mind_mcp/jmap.py` · `src/mail_mind_mcp/source.py` · `src/mail_mind_mcp/store.py` · `tests/test_jmap.py` · `tests/test_store.py` · `tests/integration/test_stalwart.py` **Write-set divergence:** present table wins. 0 sourceHashes vs 17-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-trash-on-jmap-apply-undo-and-mirror-effect.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-12-mail-trash-on-jmap-apply-undo-and-mirror-effect.md`</sub>
Collaborator

spec-verify-dispatch Half B for dev/mail-mind-mcp#193 mail-trash-on-jmap-apply-undo-and-mirror-effect completed integration verify.

  • PR: #202
  • Branch: feat/svd-dev-mail-mind-mcp-193-mail-trash-on-jmap-apply-undo-e443c69e
  • Commit: 039810b700
  • 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/83/1
  • Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll
  • Warning: Stalwart integration test test_trash_and_its_undo not run (no container daemon). The Deleted Items trash-role assumption is unverified; owner must run make test-stalwart after merge (spec Open Risk 4).
  • Warning: Spec-prescribed unit test ids all collect: 13 for the tests/test_jmap.py -k trash filter, 4 for the tests/test_store.py filter.
  • Warning: self-review entries (Stalwart quoting, edge-case tests) were recorded as fixed; no follow-up needed.
  • CI wait: ci/woodpecker/pr/validation -> pass
  • Rework: 2 round(s), outcome converged
  • Decision: review the PR and merge? (codex-svd ship --run-id 20261112T201217Z-e443c69e --confirm-merge)
spec-verify-dispatch Half B for dev/mail-mind-mcp#193 mail-trash-on-jmap-apply-undo-and-mirror-effect completed integration verify. - PR: https://git.groupthink.asia/dev/mail-mind-mcp/pulls/202 - Branch: feat/svd-dev-mail-mind-mcp-193-mail-trash-on-jmap-apply-undo-e443c69e - Commit: 039810b700fdd0201f775c7910648ee4da02a710 - 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/83/1 - Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll - Warning: Stalwart integration test test_trash_and_its_undo not run (no container daemon). The Deleted Items trash-role assumption is unverified; owner must run `make test-stalwart` after merge (spec Open Risk 4). - Warning: Spec-prescribed unit test ids all collect: 13 for the tests/test_jmap.py -k trash filter, 4 for the tests/test_store.py filter. - Warning: self-review entries (Stalwart quoting, edge-case tests) were recorded as fixed; no follow-up needed. - CI wait: ci/woodpecker/pr/validation -> pass - Rework: 2 round(s), outcome converged - Decision: review the PR and merge? (`codex-svd ship --run-id 20261112T201217Z-e443c69e --confirm-merge`)
svc-svd-writer 2026-11-12 20:39:18 +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#193
No description provided.