feat(runtime): add preflight auto-compaction before model requests#2978
Open
123qwe55aa wants to merge 1 commit intoultraworkers:mainfrom
Open
feat(runtime): add preflight auto-compaction before model requests#2978123qwe55aa wants to merge 1 commit intoultraworkers:mainfrom
123qwe55aa wants to merge 1 commit intoultraworkers:mainfrom
Conversation
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"
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.
Summary
maybe_preflight_compact()that runs before every model request to prevent context overflow errorsestimate_session_tokens()to check current session size (vs post-turn uses cumulative usage tokens)Changes
ConversationRuntime: addedpreflight_compaction_input_tokens_thresholdfield (default: 80,000)CLAUDE_CODE_PREFLIGHT_COMPACT_TOKENS(vs existingCLAUDE_CODE_AUTO_COMPACT_INPUT_TOKENS)with_preflight_compaction_input_tokens_threshold()TurnSummary: addedpreflight_auto_compactionfield to distinguish preflight vs post-turn compactionsTest plan
preflight_skips_compaction_under_thresholdpreflight_compacts_before_api_requestpreflight_compaction_threshold_comes_from_env