fix(agent): clamp compression threshold below context_length to ensure compression can trigger (#14690)#15431
Open
Tranquil-Flow wants to merge 1 commit into
Open
Conversation
Collaborator
1484a55 to
72ca28d
Compare
72ca28d to
387dffe
Compare
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.
What does this PR do?
When
context_length == MINIMUM_CONTEXT_LENGTH(64000), the threshold calculationmax(int(context_length * 0.50), MINIMUM_CONTEXT_LENGTH)producesmax(32000, 64000) = 64000, setting the compression threshold to 100% of the context window. The API errors before tokens ever reach that value, so auto-compression never triggers for models at the minimum context length.The fix clamps
threshold_tokensto at most 95% ofcontext_lengthafter the existing floor, preserving theMINIMUM_CONTEXT_LENGTHfloor for large-context models (where the clamp is a no-op) while ensuring compression can always trigger before the API limit.Related Issue
Fixes #14690
Type of Change
Changes Made
agent/context_compressor.py: After the existingmax()floor, clampthreshold_tokensto at most 95% ofcontext_length. Applied in both__init__andupdate_model(). Additionally,update_model()now recomputes all derived budgets (tail_token_budget,max_summary_tokens) — previously it only updatedthreshold_tokens, leaving stale budget values after model switches.How to Test
TestThresholdClampAtMinimumContext:should_compress()triggers correctly at threshold boundaryupdate_model()produces a working thresholdTested on macOS (Python 3.11).
Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AScreenshots / Logs