Skip to content

fix: verify a saved reply lands outside Mail's quoted original (#71) - #91

Closed
proscar87 wants to merge 1 commit into
patrickfreyer:mainfrom
proscar87:fix/71-verify-reply-outside-quote
Closed

proscar87 wants to merge 1 commit into
patrickfreyer:mainfrom
proscar87:fix/71-verify-reply-outside-quote

Conversation

@proscar87

Copy link
Copy Markdown
Contributor

Fixes #71.

The gap

reply_to_email inserts the new reply body via a blind clipboard paste into Mail's native reply composer, which already contains the quoted original thread. That paste is never read back through content of replyMessage — Mail's GUI editor buffer is decoupled from that scriptable property, so a read-back there always reports the pre-paste value even after a successful paste (documented in the existing comment above the keystroke step in compose.py). The tool reported success from the AppleScript completing without error, with no check on where the pasted text actually landed relative to the quote.

What this adds

reply_verification.py: parse the saved draft's RFC 822 source (via the existing get_email_source tool — no new AppleScript) and verify the reply body is present in the region preceding Mail's quote container, HTML <blockquote type="cite"> or a plain-text >-quoted block. Three distinguishable outcomes:

  • Found outside the quote — pass.
  • Found only inside the quote — the bug this issue describes.
  • Not found at all — a different, pre-existing failure mode (see the focus-polling comment in compose.py about the paste landing nowhere if Mail isn't frontmost).

Wired into reply_to_email for mode="draft" only. That's the one case where the message is guaranteed to already be sitting in Drafts by the time the AppleScript that saved it has returned, so re-reading it is safe and synchronous. send moves the message to Sent before a read-back could happen; open leaves it unsaved in a compose window with nothing in a mailbox yet. Neither is covered here — flagging both as a natural follow-up rather than solving them in this PR.

Two bugs I found in my own first pass, before opening this

  1. Gating the read-back on not output_text.startswith("Error") misses "No email found matching: <keyword>" — a different failure with no saved draft to verify, that also doesn't start with "Error". Switched to checking for the AppleScript's own positive success marker (success_text) instead of the absence of one particular negative string. A test written against the intended behaviour caught this before it shipped — test_applescript_error_does_not_attempt_verification is the mutation that reverting to the negative check breaks, and it does.

  2. My first version made the existing sender-override tests issue real, unmocked AppleScript against whatever machine runs the suite — a fast "does this account exist" check that failed quickly since "Work" isn't a real account there, but real execution nonetheless. reply_to_email's existing send=False tests didn't anticipate a second read-back call, since there wasn't one before this change. Fixed by mocking get_email_source in those two tests too, and confirmed with a subprocess.run-tracking wrapper around the entire suite that zero real calls occur anywhere, not just in the tests I touched.

Verification

  • 9 unit tests for the parsing/detection logic in isolation: HTML and plain-text quote markers, body-inside-quote, body-missing-entirely (distinguished from body-inside-quote), multi-<div> HTML wrapping, no quote block at all (plain compose), empty body, no recognizable body part.
  • 6 integration tests for the reply_to_email wiring: pass, fail, get_email_source error, and that send/open/no-draft-saved paths never attempt verification.
  • Mutation-tested three points in the detection logic (blockquote detection, HTML tag stripping, >-marker detection) and the gating condition — each breaks exactly the test built to catch it, no others.
  • Full suite: 135 passed (was 120), matching CI's PYTHONPATH=plugin invocation exactly.
  • Zero real subprocess.run calls anywhere in the suite — confirmed by wrapping subprocess.run for the entire run, not just the new tests, and asserting the tracked-call count is 0.
  • scripts/check_versions.py (CI's version-drift guard): clean.

What I could not verify

Live behaviour against a real Mail.app draft. Everything above is against synthetic RFC 822 fixtures and mocked AppleScript output — I did not run this against an actual account.

🤖 Generated with Claude Code

…ckfreyer#71)

`reply_to_email` inserts the new body via a blind clipboard paste into
Mail's native reply composer, which already contains the quoted original
thread. That paste is never read back through `content of replyMessage` --
Mail's GUI editor buffer is decoupled from that scriptable property, so a
read-back there always reports the pre-paste value even after a
successful paste (documented in the existing comment above the keystroke
step). The tool therefore reported success from the AppleScript completing
without error, with no check on where the pasted text actually landed.

`reply_verification.py` adds that check: parse the saved draft's RFC 822
source (via the existing `get_email_source` tool, no new AppleScript) and
verify the reply body is present in the region preceding Mail's quote
container -- HTML `<blockquote type="cite">` or a plain-text `>`-quoted
block. If the body ends up inside the quote instead of above it (the bug
patrickfreyer#71 describes), or isn't found at all (a different, pre-existing failure
mode -- see the focus-polling comment in `compose.py`), the tool now says
so instead of reporting the same "saved!" text either way.

Wired into `reply_to_email` for `mode="draft"` only. That's the one case
where the message is guaranteed to already be sitting in Drafts by the
time the AppleScript that saved it returns, so re-reading it is safe and
synchronous. "send" moves the message to Sent before a read-back could
happen; "open" leaves it unsaved in a compose window with nothing in a
mailbox yet. Neither is covered here -- flagged as a natural follow-up
rather than solved in this PR.

Found and fixed two bugs in my own first pass, before opening anything:

1. Gating the read-back on `not output_text.startswith("Error")` misses
   "No email found matching: <keyword>" -- a different failure with no
   saved draft to verify, that also doesn't start with "Error". Switched
   to checking for the AppleScript's own positive success marker instead
   of the absence of one particular negative one. A test written against
   the *intended* behaviour caught this before it shipped: reverting to
   the negative check is exactly the mutation
   `test_applescript_error_does_not_attempt_verification` exists to catch,
   and it does.

2. My first version of this integration made the existing sender-override
   tests issue real, unmocked AppleScript against whatever machine runs
   the suite (a fast "does this account exist" check that failed quickly
   since "Work" isn't a real account there, but real execution
   nonetheless). `reply_to_email`'s existing tests using `send=False`
   didn't anticipate a second read-back call, since there wasn't one
   before this change. Fixed by mocking `get_email_source` in those two
   tests too, and confirmed with a `subprocess.run`-tracking wrapper
   around the *entire* suite that zero real calls occur anywhere in it,
   not just in the tests I touched.

Verification:
- 9 new unit tests for the parsing/detection logic in isolation (HTML and
  plain-text quote markers, body-inside-quote, body-missing-entirely
  distinguished from body-inside-quote, multi-div HTML wrapping, no quote
  block at all, empty body, no recognizable body part).
- 6 new integration tests for the `reply_to_email` wiring (pass, fail,
  get_email_source error, and that send/open/no-draft-saved paths do not
  attempt verification at all).
- Mutation-tested three points in the detection logic (blockquote
  detection, HTML tag stripping, '>' marker detection) and the gating
  condition: each breaks exactly the test built to catch it, no others.
- Full suite: 135 passed (was 120), matching CI's `PYTHONPATH=plugin`
  invocation. Zero real `subprocess.run` calls anywhere in the suite,
  confirmed by wrapping it for the entire run, not just the new tests.
- `scripts/check_versions.py` (CI's version-drift guard): clean.

What I could not verify: live behaviour against a real Mail.app draft.
Everything above is against synthetic RFC 822 fixtures and mocked
AppleScript output.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@patrickfreyer

Copy link
Copy Markdown
Owner

This review was written by Claude (AI assistant) on Patrick's behalf.

Thanks @proscar87! Since #92 is stacked on this branch (same commit 4bf8a34), we'll review and land both via #92. The detailed findings for the body-placement check are in the review there. Short version for this part:

  • The draft is located by subject (compose.py:659), so an older reply draft on the same thread could be the one verified. Matching the exact draft would be safer.
  • Three false FAILs we reproduced: _normalize strips tag-like text from plain bodies (reply_verification.py:87, e.g. "use tag" or "R&D"); with body_html, the plain reply_body fallback is checked instead of the sent HTML; the plain-text check (:201) has no whitespace normalization, so wrapped bodies fail.
  • A FAILED result is only appended below "Reply saved as draft!", which callers can miss. Epic Epic: reply-safety redesign (verified drafts, body placement, simpler compose surface) #80 asks for a structured failure.

Nice that it correctly flags a #88-wrapped body as "inside quote".

proscar87 added a commit to proscar87/apple-mail-mcp that referenced this pull request Oct 15, 2026
Review feedback on patrickfreyer#91 and patrickfreyer#92:
- The draft is identified by Message-ID: Drafts IDs are snapshotted
  before the reply is created, and only one new draft with the exact
  reply subject is accepted. No subject fallback; an ambiguous or missing
  identity is reported as a failure.
- Body placement: markup is only stripped from an actual HTML body, the
  HTML that was sent is checked instead of its plain fallback, and
  whitespace is normalized on both sides, so wrapped plain bodies pass.
- The result leads with 'Status: VERIFIED' or 'Status: FAILED — <why>'
  instead of a trailing line after 'Reply saved as draft!'.
- Cc/From are parsed with email.utils.getaddresses and compared as
  lowercased addresses.
- The saved To/Cc/From are always reported, including reply-all without
  overrides.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@proscar87

Copy link
Copy Markdown
Contributor Author

The body-placement fixes are in #92 (c7ce288), as you suggested: exact draft by Message-ID, the three false FAILs, and a leading Status: line. Details there.

@patrickfreyer

Copy link
Copy Markdown
Owner

This comment was written by Claude (AI assistant) on Patrick's behalf.

Thanks @proscar87! To keep things moving, we carried this over in #114 with your commits preserved (authorship intact), plus one small follow-up: the Drafts snapshot now has its own try, and Gmail autosave copies sharing one Message-ID count as one draft. #114 is now merged, so closing this in favour of it. Really appreciate the careful real-mailbox verification.

patrickfreyer added a commit that referenced this pull request Oct 15, 2026
ZiyadDiab pushed a commit to ZiyadDiab/apple-mail-mcp that referenced this pull request Nov 6, 2026
…aved reply drafts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

bug: prevent reply bodies from being saved inside quoted text

2 participants