fix(session): add context overflow recovery for long-running sessions#1342
Open
walker83 wants to merge 1 commit into
Open
fix(session): add context overflow recovery for long-running sessions#1342walker83 wants to merge 1 commit into
walker83 wants to merge 1 commit into
Conversation
…g sessions Ported from OpenCode commit 820c984d475c5ad0b60c8a2f5aabc715e57eaf4c Problem: - Long-running sessions (>200k tokens) crash when context overflows - No recovery mechanism when compaction fails due to context overflow - Users lose entire session when overflow occurs Solution: 1. Add recovery attempt in compaction.ts when overflow is detected: - First overflow: return "text-repeat" to trigger retry with overflow flag - This allows stripping media and retrying before giving up - Only show error if retry (replay) also fails 2. Add detailed logging in processor.ts for overflow events: - Log when overflow is detected - Log recovery attempts - Include sessionID for debugging Changes: - packages/opencode/src/session/compaction.ts: - Add recovery logic before returning "stop" - Return "text-repeat" on first overflow to allow retry - Add structured logging with context.overflow.* events - packages/opencode/src/session/processor.ts: - Add detailed overflow logging in halt function - Log recovery attempts with sessionID Related Issues: - Fixes XiaomiMiMo#1221 (Long running sessions cause lag and crash) - Fixes XiaomiMiMo#813 (长时间思考会卡死) - Related: XiaomiMiMo#680 (超大内存占用), XiaomiMiMo#300 (疑似内存泄露) Refs: OpenCode PR #31005 Refs: anomalyco/opencode@820c984d
Author
|
Superseded by #1344 which includes the same overflow recovery changes plus the timeout.ts fix. This PR should be closed to avoid merge conflicts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: Fix context overflow recovery for long-running sessions
Summary
This PR adds a context overflow recovery mechanism ported from OpenCode, which allows sessions to gracefully handle context overflow instead of crashing.
Problem
As reported in #1221, long-running sessions (>200k tokens) crash when context overflows:
Related Issues
Solution
Ported from OpenCode commit
820c984d475c5ad0b60c8a2f5aabc715e57eaf4c(PR #31005).Key Changes
1. Compaction Recovery (
compaction.ts)Before: On overflow, immediately return "stop" with error
After:
overflowflag2. Enhanced Logging (
processor.ts)Added detailed overflow logging:
Recovery Flow
Changes
Files Modified
packages/opencode/src/session/compaction.tspackages/opencode/src/session/processor.tsNew Log Events
context.overflow.detected- When overflow is detectedcontext.overflow.attempting.recovery- When attempting recoverysession.message-limit-exceeded- When messages > 1000 (from previous commit)Testing
Manual Test Steps
context.overflow.attempting.recoveryExpected Behavior
Verification
Log Output
Backwards Compatibility
This change is fully backwards compatible:
Related Commits
820c984d475c5ad0b60c8a2f5aabc715e57eaf4cChecklist
Notes
This is a critical stability fix for long-running sessions. The recovery mechanism significantly improves user experience by:
Future improvements could include: