Skip to content

fix(agent): auto-handoff context overflow - #1829

Merged
zerob13 merged 2 commits into
devfrom
fix/context-overflow-auto-handoff
Jun 28, 2026
Merged

fix(agent): auto-handoff context overflow#1829
zerob13 merged 2 commits into
devfrom
fix/context-overflow-auto-handoff

Conversation

@yyhhyyyyyy

@yyhhyyyyyy yyhhyyyyyy commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator
  • Recover provider context-window failures before first output
  • Keep recovery independent from Memory settings
  • Add bounded classifier coverage for wrapped provider errors.

Summary by CodeRabbit

  • New Features

    • Improved automatic recovery from provider context-window overflows, including a single retry path before any streamed reply is shown.
    • Refined auto-compaction behavior: when disabled, summaries are skipped but the request view is still trimmed to avoid provider errors.
  • Bug Fixes

    • More accurate context-window error detection across varied provider/SDK error formats, reducing misclassification of quota/rate-limit failures.
    • Added guardrails to prevent repeated overflow retries and inconsistent recovery outcomes.
  • Documentation

    • Added a spec, implementation plan, and task checklist for context overflow auto-handoff behavior.
    • Updated auto-compaction UI text across multiple languages.

@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds shared context-window error classification, refactors provider streaming to retry once on early overflow, migrates process finalization to the shared predicate, updates auto-compaction copy across locales, and adds specs plus tests for the new overflow flow.

Changes

Context Overflow Auto-Handoff

Layer / File(s) Summary
Spec, plan, and tasks
docs/issues/context-overflow-auto-handoff/*
Adds spec, plan, and task documents covering behavior, constraints, implementation steps, and validation coverage.
Context-window error classifier
src/main/presenter/agentRuntimePresenter/contextWindowError.ts, test/main/presenter/agentRuntimePresenter/contextWindowError.test.ts
Adds isContextWindowErrorLike with bounded recursive scanning and a test suite covering positive, negative, nested, array-shaped, cyclic, and envelope-wrapped cases.
process.ts migration
src/main/presenter/agentRuntimePresenter/process.ts
Replaces local context-window error matching with the shared classifier in the tool-loop finalization branch.
AgentRuntimePresenter overflow retry loop
src/main/presenter/agentRuntimePresenter/index.ts
Adds overflow retry helpers and rewrites runStreamForMessage into an attempt loop with preflight, overflow detection, one strict retry, and recovery-failure messaging.
Runtime and compaction tests
test/main/presenter/agentRuntimePresenter/agentRuntimePresenter.test.ts, test/main/presenter/agentRuntimePresenter/compactionService.test.ts
Adds helper-based runtime coverage for overflow events, ACP bypass, strict-trim retry, manifest/anchor assertions, and recovery anchor naming.
Auto-compaction copy updates
src/renderer/src/i18n/*/settings.json
Updates auto-compaction description strings across locale files to describe no-summary behavior with request trimming when disabled.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant runStreamForMessage
  participant CompactionService
  participant coreStream

  Client->>runStreamForMessage: send message
  runStreamForMessage->>CompactionService: preflight recovery check
  CompactionService-->>runStreamForMessage: recovery intent
  runStreamForMessage->>coreStream: stream provider attempt 1
  coreStream-->>runStreamForMessage: context-overflow event before output
  runStreamForMessage->>CompactionService: prepareForContextPressureRecovery
  runStreamForMessage->>coreStream: stream provider attempt 2
  coreStream-->>runStreamForMessage: stream events or overflow failure
  runStreamForMessage-->>Client: output or recovery-failure error
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • ThinkInAIXYZ/deepchat#1551: Shares the same request-context budgeting and max-token fitting path used by the overflow retry logic here.
  • ThinkInAIXYZ/deepchat#1600: Also changes the agent runtime context-window recovery flow in runStreamForMessage and related budgeting code.
  • ThinkInAIXYZ/deepchat#1605: Touches the same overflow/recovery logic and context-budget diagnostics in src/main/presenter/agentRuntimePresenter.

Poem

A rabbit watched the context grow,
Then trimmed the view to ease the flow.
One overflow, one retry bright,
Then hop! the stream came back to light.
With anchors set and tokens neat,
This little hutch now stays petite. 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.25% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding agent auto-handoff recovery for context overflow failures.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/context-overflow-auto-handoff

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
docs/issues/context-overflow-auto-handoff/spec.md (1)

15-16: 📐 Maintainability & Code Quality | 🔵 Trivial

External reference clarity.

The reference to "Bub auto_handoff/context_overflow pattern" assumes reader familiarity with an external system. Consider adding a brief parenthetical explaining Bub is an internal/external reference, or link to documentation if available. This is optional polish for future readers.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/issues/context-overflow-auto-handoff/spec.md` around lines 15 - 16, The
spec text uses the external “Bub auto_handoff/context_overflow” reference
without enough context, so update the wording in the affected documentation
section to briefly explain what Bub refers to or point to a local doc reference.
Keep the existing intent in place, but make the mention self-contained and easy
for future readers to understand when editing the context-overflow auto-handoff
flow.
src/renderer/src/i18n/es-ES/settings.json (1)

24-24: 🧹 Nitpick | 🔵 Trivial | 💤 Low value

Consider smoother Spanish phrasing for "raw context-window errors."

Current: "errores context-window sin procesar del proveedor" — the adjective placement after the compound noun is slightly unnatural. Consider "errores sin procesar de context-window del proveedor" or "errores raw de context-window del proveedor" for more idiomatic Spanish.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/renderer/src/i18n/es-ES/settings.json` at line 24, The Spanish text in
the settings translation uses an unnatural phrase for “raw context-window
errors” in the description string. Update the string in the i18n entry for the
settings description to use smoother Spanish wording, keeping the meaning
intact, and specifically adjust the part around “errores context-window sin
procesar del proveedor” to a more idiomatic form.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/presenter/agentRuntimePresenter/contextWindowError.ts`:
- Around line 6-12: The strong context-window detection in
STRONG_CONTEXT_WINDOW_ERROR_PATTERNS is too broad because “input exceeds” can
match unrelated failures and trigger the wrong recovery path. Update the
matching logic in contextWindowError.ts to either remove “input exceeds” from
the strong list or guard it behind clearer token/context cues, keeping the real
provider error from being replaced by overflow guidance. Use
STRONG_CONTEXT_WINDOW_ERROR_PATTERNS and any helper that consumes it to make the
change consistently.

In `@src/main/presenter/agentRuntimePresenter/index.ts`:
- Around line 3294-3299: The overflow handling in the presenter flow is
short-circuiting too early: in the `recoverProviderContextOverflow()` path, a
second provider overflow reaches the `buildProviderOverflowRetryFailure` check
before the reduced-token strict retry can run. Update the overflow handling
around `providerOverflowRecoveryAttempted`,
`strictProviderOverflowRetryUsedForRun`, and the retry scheduling logic so the
strict retry is enqueued/executed before throwing, and apply the same fix in the
corresponding overflow branch near the later retry path as well.

---

Nitpick comments:
In `@docs/issues/context-overflow-auto-handoff/spec.md`:
- Around line 15-16: The spec text uses the external “Bub
auto_handoff/context_overflow” reference without enough context, so update the
wording in the affected documentation section to briefly explain what Bub refers
to or point to a local doc reference. Keep the existing intent in place, but
make the mention self-contained and easy for future readers to understand when
editing the context-overflow auto-handoff flow.

In `@src/renderer/src/i18n/es-ES/settings.json`:
- Line 24: The Spanish text in the settings translation uses an unnatural phrase
for “raw context-window errors” in the description string. Update the string in
the i18n entry for the settings description to use smoother Spanish wording,
keeping the meaning intact, and specifically adjust the part around “errores
context-window sin procesar del proveedor” to a more idiomatic form.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: af597ab4-c282-4b79-96b3-24be349bf708

📥 Commits

Reviewing files that changed from the base of the PR and between a124cd5 and 6aa6085.

📒 Files selected for processing (29)
  • docs/issues/context-overflow-auto-handoff/plan.md
  • docs/issues/context-overflow-auto-handoff/spec.md
  • docs/issues/context-overflow-auto-handoff/tasks.md
  • src/main/presenter/agentRuntimePresenter/contextWindowError.ts
  • src/main/presenter/agentRuntimePresenter/index.ts
  • src/main/presenter/agentRuntimePresenter/process.ts
  • src/renderer/src/i18n/da-DK/settings.json
  • src/renderer/src/i18n/de-DE/settings.json
  • src/renderer/src/i18n/en-US/settings.json
  • src/renderer/src/i18n/es-ES/settings.json
  • src/renderer/src/i18n/fa-IR/settings.json
  • src/renderer/src/i18n/fr-FR/settings.json
  • src/renderer/src/i18n/he-IL/settings.json
  • src/renderer/src/i18n/id-ID/settings.json
  • src/renderer/src/i18n/it-IT/settings.json
  • src/renderer/src/i18n/ja-JP/settings.json
  • src/renderer/src/i18n/ko-KR/settings.json
  • src/renderer/src/i18n/ms-MY/settings.json
  • src/renderer/src/i18n/pl-PL/settings.json
  • src/renderer/src/i18n/pt-BR/settings.json
  • src/renderer/src/i18n/ru-RU/settings.json
  • src/renderer/src/i18n/tr-TR/settings.json
  • src/renderer/src/i18n/vi-VN/settings.json
  • src/renderer/src/i18n/zh-CN/settings.json
  • src/renderer/src/i18n/zh-HK/settings.json
  • src/renderer/src/i18n/zh-TW/settings.json
  • test/main/presenter/agentRuntimePresenter/agentRuntimePresenter.test.ts
  • test/main/presenter/agentRuntimePresenter/compactionService.test.ts
  • test/main/presenter/agentRuntimePresenter/contextWindowError.test.ts

Comment thread src/main/presenter/agentRuntimePresenter/contextWindowError.ts
Comment thread src/main/presenter/agentRuntimePresenter/index.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
docs/issues/context-overflow-auto-handoff/plan.md (1)

76-79: 📐 Maintainability & Code Quality | 🔵 Trivial

Vary sentence structure in test list items.

Lines 76–79 each begin with "- Add classifier coverage..." creating repetitive prose. Reword one or two items for readability, e.g., "Verify generic input exceeds..." or "Include manifest coverage...".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/issues/context-overflow-auto-handoff/plan.md` around lines 76 - 79, The
test plan bullets under the classifier coverage section are too repetitive, so
rephrase one or two items to vary the sentence structure while keeping the same
coverage intent. Update the list items in the plan content near the classifier
coverage entries so they don’t all start with the same “Add classifier
coverage...” pattern, using the existing phrasing around SDK Error objects,
bounded errors[]/issues[] payloads, and generic input exceeds failures as
anchors.

Source: Linters/SAST tools

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@docs/issues/context-overflow-auto-handoff/plan.md`:
- Around line 76-79: The test plan bullets under the classifier coverage section
are too repetitive, so rephrase one or two items to vary the sentence structure
while keeping the same coverage intent. Update the list items in the plan
content near the classifier coverage entries so they don’t all start with the
same “Add classifier coverage...” pattern, using the existing phrasing around
SDK Error objects, bounded errors[]/issues[] payloads, and generic input exceeds
failures as anchors.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 876dccf7-0c6a-46b4-95ee-c8c835d385e6

📥 Commits

Reviewing files that changed from the base of the PR and between 6aa6085 and 0785cc5.

📒 Files selected for processing (6)
  • docs/issues/context-overflow-auto-handoff/plan.md
  • docs/issues/context-overflow-auto-handoff/spec.md
  • docs/issues/context-overflow-auto-handoff/tasks.md
  • src/main/presenter/agentRuntimePresenter/contextWindowError.ts
  • src/renderer/src/i18n/es-ES/settings.json
  • test/main/presenter/agentRuntimePresenter/contextWindowError.test.ts
✅ Files skipped from review due to trivial changes (3)
  • src/renderer/src/i18n/es-ES/settings.json
  • docs/issues/context-overflow-auto-handoff/spec.md
  • docs/issues/context-overflow-auto-handoff/tasks.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • test/main/presenter/agentRuntimePresenter/contextWindowError.test.ts
  • src/main/presenter/agentRuntimePresenter/contextWindowError.ts

@zerob13
zerob13 merged commit 1b810b2 into dev Jun 28, 2026
8 checks passed
@zhangmo8
zhangmo8 deleted the fix/context-overflow-auto-handoff branch June 30, 2026 01:44
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