Skip to content

fix(agent): guide recovery from oversized tool results - #4925

Merged
chengyongru merged 1 commit into
mainfrom
fix/context-budget-reprompt
Jul 18, 2026
Merged

chengyongru merged 1 commit into
mainfrom
fix/context-budget-reprompt

Conversation

@chengyongru

@chengyongru chengyongru commented Jul 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Reuse the existing in-flight context governor to catch current-turn tool output that cannot fit the next model request.
  • Replace the model-facing tool result with a bounded, actionable instruction before calling the provider.
  • Let the normal agent loop ask the model to retry with narrower arguments, use another tool, or explain the context limitation to the user.

Fixes #2343.

Behavior

After a tool call completes, the next normal agent iteration already runs ContextGovernor.prepare_for_model() before making a provider request. If the estimated prompt exceeds the configured input budget, compact_inflight_overflow() replaces oversized in-flight tool results in the model-facing copy.

The replacement now tells the model that:

  1. the previous result was too large to fit the available context;
  2. the same call should not be repeated unchanged;
  3. it should retry with a narrower path, query, range, or result limit, or use another tool;
  4. if no smaller approach is possible, it should tell the user that the task cannot fit in the available context.

Tools remain available on the next ordinary model call. The context governor continues to leave persisted session history untouched and keeps the compacted model-facing result stable for the rest of the turn.

Deliberate boundaries

  • No provider-error classification or provider-specific parsing.
  • No nested model retry or second recovery state machine.
  • No retry/fallback/circuit-breaker changes.
  • No new stop_reason, cross-turn state, configuration, or checkpoint behavior.

Tests

  • python -m pytest tests/agent/test_runner_governance.py -q — 28 passed.
  • python -m pytest tests/agent/test_runner_core.py tests/agent/test_runner_governance.py tests/agent/test_loop_runner_integration.py -q — 59 passed.
  • Focused runner smoke: a 50,000-character read_file result was replaced before the next provider request; the model then issued a narrower offset/limit call and completed normally.
  • Targeted Ruff checks and git diff --check pass.

@chengyongru chengyongru added priority: p1 Important user-impacting issue or project priority with bounded impact or a reasonable workaround. fix bug Something isn't working test labels Jul 14, 2026
@chengyongru
chengyongru marked this pull request as draft July 14, 2026 10:33
@chengyongru
chengyongru marked this pull request as ready for review July 15, 2026 02:18
@chengyongru
chengyongru force-pushed the fix/context-budget-reprompt branch from d6a6486 to 01723f5 Compare July 15, 2026 03:37
@chengyongru chengyongru changed the title fix(agent): reprompt on hard context overflow fix(agent): report hard context overflow clearly Jul 17, 2026
@chengyongru
chengyongru force-pushed the fix/context-budget-reprompt branch 2 times, most recently from 9de2fe9 to 87e47df Compare July 18, 2026 16:57
Use the existing in-flight context governor to replace tool output that cannot fit the next model request with a bounded, actionable instruction. The model can retry with narrower arguments, use another tool, or explain the context limit without a second recovery state machine.
@chengyongru
chengyongru force-pushed the fix/context-budget-reprompt branch from 87e47df to 701467d Compare July 18, 2026 17:02
@chengyongru chengyongru changed the title fix(agent): report hard context overflow clearly fix(agent): guide recovery from oversized tool results Jul 18, 2026
@chengyongru
chengyongru merged commit cf00f53 into main Jul 18, 2026
4 checks passed
@chengyongru
chengyongru deleted the fix/context-budget-reprompt branch July 22, 2026 05:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fix priority: p1 Important user-impacting issue or project priority with bounded impact or a reasonable workaround. test

Projects

None yet

1 participant