Skip to content

feat: auto-compact and retry on context window errors#2808

Draft
TheArchitectit wants to merge 1 commit intoultraworkers:mainfrom
TheArchitectit:feat/auto-compact-upstream
Draft

feat: auto-compact and retry on context window errors#2808
TheArchitectit wants to merge 1 commit intoultraworkers:mainfrom
TheArchitectit:feat/auto-compact-upstream

Conversation

@TheArchitectit
Copy link
Copy Markdown

Summary

  • Automatically compact the session and retry when the model API returns a context window blocked error (e.g. context_window_blocked)
  • Eliminates the need for users to manually run /compact when hitting context limits — recovery happens transparently
  • Detects context window errors by checking for "context_window" or "Context window" in the error message, covering variants like context_window_blocked and token-limit messages
  • After compaction, rebuilds the runtime with the compacted session and retries the original request
  • Reports compaction results and any additional auto-compaction during retry to the user

Changes

  • rust/crates/rusty-claude-cli/src/main.rs: Added auto-compact retry block after the existing error path in the turn loop. When a context window error is detected, the session is compacted with max_estimated_tokens: 0 (aggressive), the turn is retried with a fresh runtime, and results are reported.

Test plan

  • Run a session that exceeds the model's context window and verify automatic compaction + retry occurs
  • Verify that non-context-window errors still propagate normally without triggering auto-compact
  • Verify that if the retry also fails, the new error is propagated correctly
  • Verify session is persisted to disk after successful auto-compact retry

🤖 Generated with Claude Code

When the model API returns a context_window_blocked error (because the request
exceeds the model's context window), the CLI now automatically:

1. Compact the session (remove old messages to free up space)
2. Retry the original request with the compacted session
3. Report results to the user

This eliminates the need for users to manually run /compact when they
hit context limits - the recovery happens automatically.

## Technical Details

- Detection: Looks for 'context_window' or 'Context window' in error message
- Uses runtime::compact_session() to aggressively compact (max_estimated_tokens=0)
- Creates new runtime with compacted session and retries the turn
- Reports compaction results and final status to user

## Testing

Tested successfully with a request that exceeded model's context:
- Auto-compact triggered: 'Messages removed 19, Messages kept 5'
- Successfully retried and completed after compaction
@TheArchitectit TheArchitectit marked this pull request as draft April 26, 2026 21:24
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.

1 participant