Skip to content

reply_to_email: target the reply's own window by id, never window 1 (#157) - #160

Merged
patrickfreyer merged 2 commits into
mainfrom
maint/157-reply-draft-window
Dec 2, 2026
Merged

patrickfreyer merged 2 commits into
mainfrom
maint/157-reply-draft-window

Conversation

@patrickfreyer

Copy link
Copy Markdown
Owner

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

Fixes #157 (part of #80).

Problem. reply_to_email(mode="draft") saved the reply with close window 1 saving yes after a fixed delay. window 1 is whichever Mail window is in front. When Mail wasn't frontmost, or a viewer window was raised, it closed the wrong window and left the reply open, empty and unsaved (reported on Exchange in #99).

Change (tools/compose.py, reply_to_email only):

  • Before reply, note the ids of Mail's open windows. After it, poll up to ~5 s for new ones. Exactly one new window is taken as the reply's (replyWindowID).
  • If none or more than one new window appears, raise an error before the paste. Nothing is pasted, saved or sent, and the error says a reply window may be open in Mail.
  • Raise that window (set index of window id replyWindowID to 1) before activating Mail and again just before Cmd+V, so the paste lands in the reply.
  • Draft mode closes window id replyWindowID saving yes. save replyMessage runs only if that close fails, with a note saying so. Send and open modes close no window.
  • The existing draft read-back (bug: verify reply drafts against actual recipients, sender, and source thread #70/bug: prevent reply bodies from being saved inside quoted text #71) still decides VERIFIED/FAILED.

Why close by id rather than save first: closing the reply's own window with saving yes is how this tool already saved reply drafts when window 1 happened to be the reply. Calling save on a windowed reply and then closing it hasn't been checked on a real Mail and could leave two draft versions. So save is only the fallback.

Not verified on a real Mail (CI and this run are Linux with mocked AppleScript). Worth a quick manual check on macOS: reply in draft mode with Mail in the background and a second viewer window in front.

Tests: 7 new tests in ReplyWindowTargetingTests. 6 of them fail without the fix. They check that no mode references window 1, the order snapshot → reply → capture → raise → paste, that the error stops before paste and send, that draft mode closes by id after the paste with save only in the on error branch, and that an unidentified-window error isn't reported as saved or verified. Suite: 352 passed, 1 skipped on fastmcp 4.2.6 / mcp 2.3.0 and on 3.1.0 / 1.30.0. check_versions.py OK.


Generated by Claude Code

…157)

Snapshot Mail's window ids before `reply`, take the single new id as the
reply's window, raise it before the Cmd+V paste, and in draft mode close
that window (saving yes) instead of `window 1`. If the reply's window
can't be identified, raise before pasting so nothing is pasted, saved or
sent. `save replyMessage` is only a fallback when closing by id fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CoqrHX93Cthp6tq6oPot7k
…tified

Say the body wasn't pasted and nothing was sent (an empty reply window
may remain and be autosaved), and drop the account-name hint for this
error. From the second review of #160.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CoqrHX93Cthp6tq6oPot7k

Copy link
Copy Markdown
Owner Author

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

Merging. This fixes #157: reply_to_email no longer touches window 1. It captures the reply's own window id, which is the one new window after reply, raises that window before pasting, and in draft mode closes it by id with saving yes. That is the only mechanism already known to save reply drafts. If the reply's window can't be told apart, the script errors before the paste, so no empty or misplaced reply is sent. The existing read-back verification still decides whether a draft counts as saved.

It's safe because send and open modes close no window. The send still runs only after the capture check, and the error path keeps the temp-file cleanup and clipboard restore.

An independent second reviewer reached MERGE. I then applied two of its wording notes: the error no longer claims "nothing saved", since Mail may autosave the empty reply window, and the error drops the misleading account-name hint. Its remaining note: if the user opens another Mail window in the instant before the reply's window appears, that window could be taken as the reply's. This is very unlikely, because reply ... with opening window opens the window before it returns. It's noted for #80 follow-up.

Checks: CI green. Locally 353 passed, 1 skipped on fastmcp 4.2.6 and on 3.1.0. Versions agree. The new tests fail on the old code. Not yet exercised on a real Mail, so it's worth a manual check before tagging.


Generated by Claude Code

@patrickfreyer
patrickfreyer merged commit 8f748bc into main Dec 2, 2026
5 checks passed
patrickfreyer pushed a commit that referenced this pull request Dec 2, 2026
… 2026-12-02

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CoqrHX93Cthp6tq6oPot7k
@patrickfreyer patrickfreyer mentioned this pull request Dec 2, 2026
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.

reply_to_email(mode="draft") closes window 1 instead of the reply, so the draft can be lost

2 participants