Skip to content

feat: add ID-first verified draft editing and review - #143

Open
YassineElbouchaibi wants to merge 2 commits into
patrickfreyer:mainfrom
YassineElbouchaibi:feature/verified-draft-editing
Open

YassineElbouchaibi wants to merge 2 commits into
patrickfreyer:mainfrom
YassineElbouchaibi:feature/verified-draft-editing

Conversation

@YassineElbouchaibi

Copy link
Copy Markdown
Contributor

Saved drafts currently cannot be read or updated through manage_drafts, subject substrings silently select the first match, and open promises an editor while opening a message viewer. Replacing a draft manually therefore risks deleting the original before its replacement has been verified.

This adds an ID-first lifecycle on current main:

  • Numeric message_id or exact internet_message_id targeting, with ambiguous subject fallbacks rejected.
  • Structured list/read output containing the actual saved recipients, attachment names, body preview, and identifiers.
  • update and edit: save a standalone replacement, read back its recipients/body/attachment bytes, check that the original source is unchanged, then retire the original to Trash. edit uses a real outgoing composer. Failed verification retains the original and reports the replacement ID.
  • Reuse of authored HTML without accumulating Mail's generated share-wrapper blockquotes. Unsupported reply headers, inline images, digitally signed/encrypted MIME, unavailable attachments, and document-level styling are refused rather than silently lost.
  • Truthful open output identifying the read-only viewer. Send requests check Mail's acceptance and do not claim verified delivery; a separate verify_sent action checks Sent by RFC Message-ID and reports leftover draft IDs without deleting them.
  • Plain compose and legacy draft creation save only their own outgoing object, never an unrelated window 1.

This is a conservative standalone-draft addition toward #80. It does not replace the reply design or HTML compose implementation discussed in #99. A leftover draft is not treated as proof of failed sending, and no same-subject deduplication is attempted.

Validation:

  • 27 new lifecycle/MIME regression tests, including ambiguous targeting, read-only delivery guards, failed recipient/body/attachment verification, concurrent-source guards, and authored-quote preservation.
  • Full upstream-based macOS suite: 321 passed, 93 subtests passed, with the existing /var versus /private/var rich-draft path assertion failing. The version-consistency guard passes. The maintained local integration, which already normalizes that assertion, passes all 331 tests.
  • Native Mail checks using disposable unsent drafts verified an update with cleared CC, attachment-byte preservation, HTML-preserving subject changes, and a genuine editable outgoing composer. The test drafts were cleaned up. No messages were sent in live checks; sending is mocked in the regression suite.

Copilot AI balanced review requested due to automatic review settings November 2, 2026 02:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

patrickfreyer commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

Posted by Claude (AI maintainer agent) on Patrick's behalf.

Full review, 2026-11-11 (this replaces my 2026-11-04 note). @YassineElbouchaibi, thank you. This is the most careful draft-lifecycle work we've had toward #80. The replace → read back → verify (recipients, sender, attachment SHA-256, body) → source-guarded retire sequence is right, and every failure path I traced keeps the original. Exact-id selection and refusing ambiguous subject matches are also right. I tested it merged with current main: 328 passed on fastmcp 4.1.8 and on the 3.1.0 floor. An independent second review reached the same three blockers.

Blockers

  1. The draft save path changed with no read-back (compose.py ~1261 close newMessage saving yes; ~1710 close newDraft saving yes in legacy create).
    • Moving off window 1 is the right goal. But nobody has shown that close <outgoing message> saving yes on an invisible message actually writes a draft (Drafts start with a leading blank line, and visible:false leaves orphaned outgoing messages #96 saw close … saving no report success and do nothing).
    • Both paths still print "✓ saved" unconditionally. If close discards the message instead of saving it, compose_email(mode="draft") loses drafts silently for every user.
    • Your own _save_replacement doesn't trust close alone: it calls save newDraft and then polls Drafts for the new id.
    • Suggested fix: do the same here (save newMessage, then poll Drafts for the new id and report it, or fail loudly). Or split this change into its own small PR with a test on a real Mail.
  2. A real send can come back as an error (drafts.py ~705, set accepted to send targetDraft / if not accepted then error).
    • targetDraft is a stored message, not an outgoing message. Mail only defines send with a boolean result for outgoing messages.
    • If send dispatches and returns nothing, the script errors after the mail has gone. The tool then says "Error", and an agent will retry, so the mail goes out twice.
    • Suggested fix: don't depend on the return value. On any error after send has been issued, return "send may have been dispatched; run verify_sent before retrying", never a plain Error:.
  3. verify_sent uses an unbounded whose over Sent (_query, drafts.py ~91 with sent=True).

Non-blocking, but please address

  • Mailbox lookup. _mailbox duplicates core.build_mailbox_ref/localized lookups. A mailbox "Sent" reference may not error until it is used, so for Gmail ("Sent Mail") the fallback loop may never get past the first name. Resolve and touch the mailbox (e.g. count messages) inside the try.
  • Error when no replacement shows up. If nothing appears within the 5 s poll, the error says "Replacement identity is ambiguous ([])". Say "replacement not observed within 5 s; check Drafts before retrying", because it may land later.
  • Update on drafts Mail wrote itself. Mail-authored drafts usually carry an HTML part, so even a cc-only update goes through html content (undocumented, may do nothing) and an exact fragment comparison. That fails safely but leaves an unverified duplicate in Drafts. Consider deleting the unverified replacement (it is never the only copy). A note on whether you ran update/edit on a real Mail would also help (macOS/Mail version, account type).
  • CHANGELOG. [Unreleased] still needs an entry. Call out the output changes for existing callers: list loses "Created:", the send/open/delete text changed, and an ambiguous subject now fails instead of using the first match.
  • Scope. If it's easier, split it: list/read/ID selection/ambiguity refusal/delete-by-id (low risk; I'd merge that quickly), then update/edit/send/verify_sent with the fixes above.

Reply here when it's updated and I'll re-review on the next run. Please push new commits rather than force-pushing; merging main is fine. Also, CI on fork PRs here needs a maintainer to approve the run, so I run the suite locally until then.


Generated by Claude Code

Copy link
Copy Markdown
Owner

Posted by Claude (AI maintainer agent) on Patrick's behalf.

Status check, 2026-11-25. @YassineElbouchaibi, are you still working on this? Two updates since the 2026-11-11 review:

  • Blocker 1 is now handled on main's side. Save drafts without a window, address drafts by id, and add draft/open modes to forward_email #154 (being carried as carry/154-windowless-drafts) makes compose_email(mode="draft") and manage_drafts(action="create") call save on the hidden message, poll Drafts for the new id, and report it. It also adds manage_drafts(draft_id=...) for open/send/delete and shows ids in list. Once it merges, please rebase or merge main and drop the overlapping save-path and id-selection changes here, keeping your _save_replacement flow.
  • What's left is blockers 2 and 3: don't treat a missing send return value as an error after the send was issued, and use a bounded newest-first walk in verify_sent. Plus the CHANGELOG entry.

If I don't hear back by the next couple of runs, I'll carry it myself: merge your branch (keeping your commits and authorship), apply those fixes, and credit you.


Generated by Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants