fix(compressor): prevent MINIMUM_CONTEXT_LENGTH floor from blocking compression at small contexts#15496
Closed
aj-nt wants to merge 1 commit into
Closed
Conversation
…ompression at small contexts When context_length equals MINIMUM_CONTEXT_LENGTH (64000), the max() floor in threshold_tokens calculation clamps the threshold to 64000, which is 100% of the context window. Since should_compress() checks prompt_tokens >= threshold_tokens, and the API errors out before reaching 64000 tokens, compression never fires. The same bug exists in both __init__ and update_model(). Fix: after computing max(pct_based, MINIMUM_CONTEXT_LENGTH), check if the result >= context_length (and context_length > 0). If so, fall back to the percentage-based value. This preserves the floor's original intent (prevent premature compression on large-context models) while ensuring compression can actually trigger on models with context_length at or near 64000. The floor still works correctly for large contexts (e.g. 200K at 30% gives 60000, which gets clamped up to 64000 — threshold is 32% of context, which is reasonable). Fixes NousResearch#14690
Collaborator
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.
Problem
When
context_lengthequalsMINIMUM_CONTEXT_LENGTH(64000 tokens), themax()floor inthreshold_tokenscalculation clamps the threshold to 64000 — which is 100% of the context window. Sinceshould_compress()checksprompt_tokens >= threshold_tokens, and the API errors out before reaching 64000 tokens, compression never fires.This affects any model where
context_length <= MINIMUM_CONTEXT_LENGTH / threshold_percent:context_length=64000, threshold=0.70→ threshold = 64000 (100%) — compression never triggerscontext_length=64000, threshold=0.50→ threshold = 64000 (100%) — compression never triggerscontext_length=80000, threshold=0.70→ threshold = 64000 (80%) — works but threshold is higher than configuredThe same bug exists in both
__init__andupdate_model().Fix
After computing
max(pct_based, MINIMUM_CONTEXT_LENGTH), check if the result>= context_length(andcontext_length > 0). If so, fall back to the percentage-based value.This preserves the floor's original intent (prevent premature compression on large-context models) while ensuring compression can actually trigger on models with context_length at or near 64000.
The floor still works correctly for large contexts — e.g. 200K at 30% gives 60000, clamped up to 64000 (32% of context, which is reasonable).
Testing
13 new tests in
tests/agent/test_context_compression_threshold.py:test_threshold_below_context_when_at_minimumtest_threshold_below_context_at_minimum_with_default_percenttest_threshold_at_85_percent_minimum_contexttest_should_compress_fires_at_minimum_contexttest_should_compress_does_not_fire_below_thresholdtest_floor_clamps_tiny_percentage_on_large_contexttest_large_context_percentage_above_floortest_threshold_never_equals_context_length_on_large_modeltest_update_model_threshold_below_context_at_minimumtest_update_model_preserves_floor_for_large_contexttest_context_length_slightly_above_minimumtest_threshold_at_boundarytest_context_length_below_minimum_at_zeroAll 63 tests pass (50 existing + 13 new). No regressions.
Fixes #14690