Repository navigation
fix(email): bound mail reads per turn, not just per message - #4111
Conversation
…r-message A triage turn could read several long emails in a row with no combined limit, quietly filling the context window and leaving the model answering from a partial conversation with no error anywhere. read_email now tracks the bounded body chars it has pulled into the current turn and refuses once that reaches the same per-tool-result budget the agent already uses for large results (Agent._truncation_budget, scaled off the device's real context window). The refusal is a structured result the model must relay, naming what stopped, what to do, and where to look. The base Agent gains a monotonic _turn_seq counter so the per-turn ledger knows when a new turn has started.
EvidenceReal-world tier excluded up front: proving a character budget needs no mailbox and no inference, and this session holds no live Gmail/Graph grant. The strongest available surface is the real Integration — the budget driven through a real
|
The per-turn counter was created in Agent.__init__, so any subclass or test double that builds an instance directly hit AttributeError the moment a turn started — three unit-test jobs went red on it. Declaring it on the class gives every instance a starting value; the increment still writes per-instance.
|
Three unit-test jobs went red after this went ready for review. The per-turn counter only existed once an agent had run through its normal constructor, so any lightweight subclass or test double that skips it errored out the moment a turn began. It is now a class-level default, so every instance starts at zero and the increment still stays per-instance. A test pins that an Evidence below is measured on 🔍 Technical detailsDiscriminated against After the fix: CI at this head: Unit Tests py3.10 / py3.12 / macOS smoke all pass, alongside Code Quality, CodeQL, Email Agent, Chat Agent, Gaia Agent, Security, build. The earlier turn-budget evidence is unaffected — this change alters only where the counter's initial value lives, not when it moves or what the budget does. The red |
|
Rebuilt on current
Measured on CI at this head: 30 checks green, including all three Unit Tests legs, Code Quality, CodeQL, Security, and the Email / Chat / Gaia agent suites. The review bot also ran clean here ( 🔍 Technical details
Five self-hosted hardware lanes remain queued and have not started: API Server Tests, GAIA CLI on Windows, Agent SDK on Windows (Lemonade), Example Agents Integration (stx), and behavior-e2e. That pool has been saturated repo-wide for several hours — #4109 merged with the same four lanes still pending, so they are not gating and re-queueing would not change it. None of them exercise this diff, which is pure Python in the agent base and the email tool mixin; the lanes that do cover it are green above. |
The budget lint this PR adds fails the moment it meets current main: amd#4111 landed a fourth paragraph on `read_email` while this branch was open, taking its schema description to 590 chars against a 400 budget. The failure is real — that text is re-sent on every model call of every turn — so the fix is to trim it, not to widen the budget. Both contracts the paragraphs carried are kept: a truncated body still announces itself with its original length, and a turn that has spent its mail budget still tells the model to stop rather than retry.
Closes #4086
Asking the agent "what needs me today?" makes it read several emails in one go. Each single message was already capped at 12,000 characters, but nothing capped the pile — read four or five long quoted threads and the model quietly ran out of room, losing the earlier half of its own conversation and answering from a partial view with no error anywhere. Mail reading now has a per-turn character budget sized from the machine's real context window, and once a turn has spent it the next read is refused with a result that tells the model to stop, answer from what it already read, and say so. The budget resets on the next turn.
The bound sits in the shared mixin, above both mailbox backends, so Gmail and Outlook behave identically; neither backend file is touched.
🔍 Technical details
No new tuning constant. The turn budget is
Agent._truncation_budget()[0]— the same per-tool-result character cap already applied to any other large tool output, derived from the device profile viabudget_for_ctx. Policy in one sentence: all the mail bodies a turn pulls in together may occupy what one large tool result is allowed to. It is therefore profile-derived by construction, and already correct for a-FLMNPU model, a cloud/gateway session, and Claude's 200K window.Admission rule. A read is admitted while
used < budget, so at most one message crosses the line and that one is itself capped at_MAX_BODY_CHARS— a turn can never admit an unbounded pile. Refusing earlier (whenbudget - used < _MAX_BODY_CHARS) would make the bound strictly ≤ budget but costs a whole read whenever bodies are small: two reads on NPU, too stingy for the triage case the issue is about.Turn detection.
Agentgains a monotonic_turn_seq, incremented in_process_query_impl— the single path every entry point (CLI, UI, API server, sidecar, hub servers) goes through. The mixin reads it withgetattrand resets its ledger when it moves._on_task_startwas not usable:EmailToolsMixinis listed afterChatAgentinGaiaAgent's bases, soAgent's no-op would win the MRO — the exact trapProjectMapMixin.__init_subclass__exists to catch — and reordering would put a tool mixin ahead of the base agent, which the comment atgaia_agent/agent.py:236warns against.Replaced rather than adapted the retired sidecar's
context_budget.py. The flagship wheel cannot import it (hub package, not a dependency) and its numbers are pinned to that sidecar's 16K eval target. What carried over is the method: derive from the device profile, and be pessimistic about chars per token.Out of scope, stated:
list_inbox/search_emailreturn previews bounded bylimit <= 100and are not charged;_MAX_BODY_CHARS(#4085's constant) is unchanged; neither message-id guard was touched — they differ deliberately between the two backends.Test plan
pytest tests/unit/agents/— 1234 passed, 50 skippedpython util/lint.py --black --isort— both PASSif used >= budget→if False) and 5 of the 6 new tests failed; reverted to a byte-identical tree (git statusclean). The sixth is the profile-derivation test, which correctly does not depend on the guard.gaia eval— the judge credential is failing intermittently in CI and would not produce an attributable signal.