Skip to content

Fix body_text search timeouts on mailboxes under 5,000 messages; add max_body_reads - #149

Merged
patrickfreyer merged 4 commits into
patrickfreyer:mainfrom
brian-maloney:fix/body-search-filter-before-content
Nov 18, 2026
Merged

patrickfreyer merged 4 commits into
patrickfreyer:mainfrom
brian-maloney:fix/body-search-filter-before-content

Conversation

@brian-maloney

Copy link
Copy Markdown
Contributor

Problem

search_emails(body_text=...) on a mailbox under WHOSE_CLAUSE_SAFE_LIMIT (5,000) took a path that ran every message of currentMailbox and read the content of every message before checking any filter, and never stopped at limit. On a ~2,000-message IMAP inbox, a body search with a two-day date_from and limit=3 read 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 with Mail 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

  1. Filter before reading bodies; bound every mailbox size. Body search always uses the newest-first scan: date-window cutoff first, then cheap metadata filters, and only then content of, with body reads (not scanned messages) counted against BOUNDED_BODY_SCAN_MAX_MESSAGES and an early exit once the page is filled.
  2. Prefilter with a whose clause. 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 + sender with no date window. For mailboxes under the limit, candidates are now narrowed with the same whose clause 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.
  3. max_body_reads parameter (default 50, which is the current behavior; range 1–500). A body search over a busy window can come back scan_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-mode scan_limited note 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, the whose prefilter, pure-body scans skipping metadata reads, and max_body_reads default, override and range validation.
  • Manual, on macOS against a real ~2,000-message IMAP inbox via Claude Desktop:
Search Before After
body_text + date_from (2 days), limit=3 timed out (>80 s) returns; scan_limited at default 50
same with max_body_reads=200 n/a returns, finds a match the 50 cap missed, scan_limited: false
body_text + sender, no date timed out returns all 4 matches
body_text only timed out / lock held returns
subject_keyword only fast fast (unchanged)

Changelog entries are under Unreleased.

🤖 Generated with Claude Code

https://claude.ai/code/session_014vce8TBD74H2bFq6V8wpi3

brian-maloney and others added 4 commits November 14, 2026 18:55
…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
brian-maloney force-pushed the fix/body-search-filter-before-content branch from bc5b135 to d09ce76 Compare November 16, 2026 00:38
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
@patrickfreyer
patrickfreyer merged commit a07ce1c into patrickfreyer:main Nov 18, 2026

Copy link
Copy Markdown
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 main. 3.4.0 had moved the [Unreleased] section, so your CHANGELOG entries are in the new one with credit. The before/after table against a real inbox made this easy to review. Closing in favour of #152.

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. date_from keeps it short.


Generated by Claude Code

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.

2 participants