Skip to content

fix: add stream retry cap and context recovery hint for provider errors#12091

Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/12087-context-loss-on-provider-error
Draft

fix: add stream retry cap and context recovery hint for provider errors#12091
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/12087-context-loss-on-provider-error

Conversation

@roomote-v0
Copy link
Copy Markdown
Contributor

@roomote-v0 roomote-v0 bot commented Apr 10, 2026

Related GitHub Issue

Closes: #12087

Description

This PR attempts to address Issue #12087 where provider errors during API requests cause the agent to lose context and hallucinate about previously completed tasks.

Two key improvements:

  1. Stream retry cap (MAX_STREAM_RETRIES = 5): Both the first-chunk error retry path (in attemptApiRequest) and the mid-stream error retry path (in recursivelyMakeClineRequests) now have a maximum retry limit. Previously, only context window errors had a cap (MAX_CONTEXT_WINDOW_RETRIES), while stream/provider errors could retry indefinitely with exponential backoff. After exceeding the limit, the error is presented to the user for manual retry, which resets the counter.

  2. Context recovery hint on retry: When a retry occurs (retryAttempt > 0), a [CONTEXT RECOVERY NOTE] is prepended to the user content. This helps weaker models re-orient to the current task instead of latching onto earlier completed task context. The hint explicitly instructs the model to focus on the most recent request and not repeat previously completed tasks. Additionally, the last user message in apiConversationHistory is updated with the hint and refreshed environment details.

Feedback and guidance are welcome.

Test Procedure

  • Unit tests added in src/core/task/__tests__/stream-retry-limit.spec.ts (17 tests, all passing)
  • Existing grace-retry-errors.spec.ts tests still pass (11 tests)
  • All lint checks pass
  • All type checks pass across the monorepo

To run the new tests:

cd src && npx vitest run core/task/__tests__/stream-retry-limit.spec.ts

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

The context recovery hint is designed to be model-agnostic and uses clear, directive language to help even weaker models (like glm-5 via OpenAI Compatible endpoints) maintain focus on the current task after error recovery. The retry cap of 5 was chosen to match a reasonable threshold -- with exponential backoff starting at the configured base delay, 5 retries provides sufficient recovery window while preventing runaway loops.

Interactively review PR in Roo Code Cloud

…rs (#12087)

- Add MAX_STREAM_RETRIES (5) to cap first-chunk and mid-stream error retries,
  preventing indefinite retry loops when auto-approval is enabled
- Add context recovery hint prepended to user content on retry attempts,
  helping weaker models re-orient to the current task instead of
  hallucinating about previously completed tasks
- When max retries are exceeded, present the error to the user for manual
  retry instead of continuing to auto-retry indefinitely
- Update last user message in API history on retry with recovery hint and
  refreshed environment details
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.

[BUG] Context loss on Provider Error causes agent to forget the latest prompt and hallucinate previous task

1 participant