Skip to content

feat(runtime): add preflight auto-compaction before model requests#2978

Open
123qwe55aa wants to merge 1 commit intoultraworkers:mainfrom
123qwe55aa:feat/preflight-auto-compact
Open

feat(runtime): add preflight auto-compaction before model requests#2978
123qwe55aa wants to merge 1 commit intoultraworkers:mainfrom
123qwe55aa:feat/preflight-auto-compact

Conversation

@123qwe55aa
Copy link
Copy Markdown

Summary

  • Add maybe_preflight_compact() that runs before every model request to prevent context overflow errors
  • Previously, auto-compaction only ran after a completed turn, so large sessions could exceed the provider context window before compaction had a chance to run
  • Preflight compaction uses estimate_session_tokens() to check current session size (vs post-turn uses cumulative usage tokens)

Changes

  • ConversationRuntime: added preflight_compaction_input_tokens_threshold field (default: 80,000)
  • Environment variable: CLAUDE_CODE_PREFLIGHT_COMPACT_TOKENS (vs existing CLAUDE_CODE_AUTO_COMPACT_INPUT_TOKENS)
  • Builder method: with_preflight_compaction_input_tokens_threshold()
  • TurnSummary: added preflight_auto_compaction field to distinguish preflight vs post-turn compactions
  • Added 3 unit tests

Test plan

  • preflight_skips_compaction_under_threshold
  • preflight_compacts_before_api_request
  • preflight_compaction_threshold_comes_from_env
  • All 8 existing compaction tests still pass

Add maybe_preflight_compact() that runs before every api_client.stream()
call to prevent context overflow errors from the provider. Unlike the
existing post-turn compaction (which uses cumulative usage tokens),
preflight compaction estimates the current session token footprint
before the request is sent.

Key changes:
- Added preflight_compaction_input_tokens_threshold field (default 80k)
- Added CLAUDE_CODE_PREFLIGHT_COMPACT_TOKENS env var
- Added with_preflight_compaction_input_tokens_threshold() builder method
- Added preflight_auto_compaction to TurnSummary
- Added 3 unit tests covering threshold behavior

Fixes context overflow errors that caused CLI to exit when session
grew too large between post-turn compaction cycles.

PR title: "Add preflight auto-compaction before model requests"
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