Skip to content

carry #149: filter before reading bodies in body_text search; add max_body_reads - #152

Merged
patrickfreyer merged 6 commits into
mainfrom
carry/149-body-search
Nov 18, 2026
Merged

patrickfreyer merged 6 commits into
mainfrom
carry/149-body-search

Conversation

@patrickfreyer

Copy link
Copy Markdown
Owner

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

Carries #149 by @brian-maloney, keeping their 4 commits and authorship.

What

On mailboxes under 5,000 messages, search_emails(body_text=...) read every message's body before checking any filter, and it never stopped at limit. That overran client timeouts on a ~2,000-message inbox and left the automation lock held. Body search now:

  • prefilters with the same whose clause the metadata search uses (under the size limit), or walks the mailbox newest-first by index with a date cutoff (over the limit);
  • reads bodies only for messages that pass those filters, and stops once the page is full;
  • caps body reads with the new max_body_reads (default 50, the current cap; range 1–500).

Maintainer changes

  • Merged current main; moved the CHANGELOG entries into the new [Unreleased] with credit.

Testing

Known trade-off

On mailboxes over 5,000 messages, a body search that also has a metadata filter but no date window now walks up to BOUNDED_SCAN_MAX_MESSAGES (4000) by index, the same as a metadata-only search. Before, it stopped after 50 messages. So it shares the per-message cost tracked in #132 for very large Gmail mailboxes. Adding date_from keeps it fast, because the walk stops at the cutoff.


Generated by Claude Code

brian-maloney and others added 6 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
… credit

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

Copy link
Copy Markdown
Owner Author

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

Merging. On mailboxes under 5,000 messages, body_text search read every body before applying any filter and ignored limit. That made ordinary inbox body searches time out and hold the automation lock.

This change filters first: a whose prefilter under the size limit, or a newest-first index walk with a date cutoff above it. It reads bodies only for the survivors, stops when the page is full, and caps body reads with a tunable max_body_reads (default unchanged at 50). It reuses the existing whose/bounded-scan building blocks rather than adding a new search path. The new argument is additive with a default.

CI is green on the final head, and 324 passed locally on fastmcp 4.2.1 and 3.1.0. The new tests fail against the old search.py. The large-mailbox trade-off is described in the PR body and tracked under #132.


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.

3 participants