Skip to content

Return UIDs from search_mail_messages - #11

Open
ivarsb wants to merge 1 commit into
mainfrom
bugfix/search-returns-uids
Open

Return UIDs from search_mail_messages#11
ivarsb wants to merge 1 commit into
mainfrom
bugfix/search-returns-uids

Conversation

@ivarsb

@ivarsb ivarsb commented Aug 25, 2026

Copy link
Copy Markdown
Member

Problem

search_messages issued IMAP SEARCH, which returns message sequence numbers. The tool serialised them under a uids key, but every tool a caller would chain into — get_mail_message, delete_mail_message, move_mail_message, update_mail_message_flags — issues UID commands (uid_fetch, uid_store, uid_move). Search results therefore addressed the wrong messages.

The two agree only while a folder has never been expunged: sequence numbers are renumbered on every expunge, UIDs are stable. In a fresh test mailbox seq == UID, so the mismatch looks like it works and then silently drifts — get_mail_message returns a different message than the one matched, and delete/move act on it.

Fix

Switch to uid_search, matching list_messages, which already uses it. Search was the only sequence-number leak in ImapClient; everything else was already UID-based.

Also documents the return contract in the MCP tool description and the README tool table.

Tests

The specs stubbed :search and asserted the return value passed through, so the suite pinned the buggy contract rather than catching it. They now stub :uid_search. Net::IMAP is stubbed with instance_spy, a verifying double, so these also check uid_search exists and accepts the array-of-criteria form.

Verified by reverting the one-word change and re-running: both search examples fail. Full suite 84 examples / 0 failures, RuboCop clean.

Release

No version bump included. Production installs via gem install mail_mcp -v X, so merging this does not deploy it — it needs a separate release commit, as with v1.0.3.

🤖 Generated with Claude Code

search_messages issued IMAP SEARCH, which returns message sequence numbers.
The tool serialised them under a `uids` key, but every tool a caller would
chain into — get_mail_message, delete_mail_message, move_mail_message,
update_mail_message_flags — issues UID commands (uid_fetch, uid_store,
uid_move). Search results therefore addressed the wrong messages.

The two agree only while a folder has never been expunged, since sequence
numbers are renumbered on every expunge while UIDs are stable. In a fresh
test mailbox seq == UID, so the mismatch looks like it works and then
silently drifts: get_mail_message returns a different message than the one
matched, and delete/move act on it.

Switch to uid_search, matching list_messages, which already uses it.

The specs stubbed :search and asserted the return value passed through, so
the suite pinned the buggy contract rather than catching it — they now stub
:uid_search. Net::IMAP is stubbed with instance_spy, a verifying double, so
these also check uid_search exists and accepts the array-of-criteria form.

No release included; production installs via `gem install mail_mcp -v X`,
so this needs a version bump to land.

Co-Authored-By: Claude Opus 5 (1M context) <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.

1 participant