Repository navigation
Fix body_text search timeouts on mailboxes under 5,000 messages; add max_body_reads - #149
Merged
patrickfreyer merged 4 commits intoNov 18, 2026
Conversation
…ll mailbox sizes Mailboxes under WHOSE_CLAUSE_SAFE_LIMIT took a path that ran 'every message of currentMailbox' and read the content of every message before checking any filter, and never stopped at the requested limit. A body search with a two-day date_from on a ~2,000-message inbox read every body and overran the client's tool timeout. Body search now always uses the newest-first index scan: date window cutoff first, cheap metadata filters (sender, subject, read, flags, attachments) next, and only then 'content of', with body reads counted against BOUNDED_BODY_SCAN_MAX_MESSAGES and an early exit once the page is filled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014vce8TBD74H2bFq6V8wpi3
Walking a ~2,000-message mailbox by index costs several Apple events per message (subject, sender, read status, date), which overran the client's 80 s tool timeout even with almost no body reads (e.g. body_text + sender with no date window). For mailboxes under WHOSE_CLAUSE_SAFE_LIMIT, body search now narrows candidates with the same whose clause the metadata search uses, in one Apple event, and only body-reads the survivors newest-first. Pure body searches with no cheap filters skip the per-message metadata reads and just read content, still capped by BOUNDED_BODY_SCAN_MAX_MESSAGES. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014vce8TBD74H2bFq6V8wpi3
The 50-body cap was fixed in code, so a body search over a busy date window could only come back scan_limited. search_emails now accepts an optional max_body_reads (default 50, unchanged; 1-500) and documents when to raise it, so an agent can retry a scan_limited body search with a bigger budget without changing behavior for anyone else. Also adds changelog entries for this and the body-search fixes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014vce8TBD74H2bFq6V8wpi3
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014vce8TBD74H2bFq6V8wpi3
brian-maloney
force-pushed
the
fix/body-search-filter-before-content
branch
from
November 16, 2026 00:38
bc5b135 to
d09ce76
Compare
patrickfreyer
added a commit
that referenced
this pull request
Nov 18, 2026
carry #149: filter before reading bodies in body_text search; add max_body_reads
Owner
|
Posted by Claude (AI maintainer agent) on Patrick's behalf. Thank you @brian-maloney! This is merged via #152, which carries your four commits unchanged plus a merge of current One follow-up: on mailboxes over 5,000 messages, body search with a metadata filter but no date window now walks the same 4000-message index scan as metadata-only search. That's the per-message cost tracked in #132. Generated by Claude Code |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
search_emails(body_text=...)on a mailbox underWHOSE_CLAUSE_SAFE_LIMIT(5,000) took a path that ranevery message of currentMailboxand read the content of every message before checking any filter, and never stopped atlimit. On a ~2,000-message IMAP inbox, a body search with a two-daydate_fromandlimit=3read every body and overran the MCP client's tool timeout (80 s in Claude Desktop). The automation lock was then held for the rest of the run, so following calls failed withMail automation is busy (lock held).Shrinking the inbox below 5,000 made this worse, not better: above the limit, body search used the capped index scan.
Changes
content of, with body reads (not scanned messages) counted againstBOUNDED_BODY_SCAN_MAX_MESSAGESand an early exit once the page is filled.whoseclause. Walking ~2,000 messages by index still costs several Apple events per message (subject, sender, read status, date) and timed out for e.g.body_text+senderwith no date window. For mailboxes under the limit, candidates are now narrowed with the samewhoseclause the metadata search uses (one Apple event), and only the survivors are body-read. A pure body search with no cheap filters skips the per-message metadata reads.max_body_readsparameter (default 50, which is the current behavior; range 1–500). A body search over a busy window can come backscan_limited; the agent can now retry with a larger budget instead of the cap being fixed in code. The docstring says when to raise it, and the text-modescan_limitednote mentions it.No behavior change for non-body searches.
Testing
pytest: 311 passed, 1 skipped. Updated the two tests that asserted the old two-loop structure; added tests for filter ordering, thewhoseprefilter, pure-body scans skipping metadata reads, andmax_body_readsdefault, override and range validation.body_text+date_from(2 days),limit=3scan_limitedat default 50max_body_reads=200scan_limited: falsebody_text+sender, no datebody_textonlysubject_keywordonlyChangelog entries are under Unreleased.
🤖 Generated with Claude Code
https://claude.ai/code/session_014vce8TBD74H2bFq6V8wpi3