fix(agent): add time-based and growth-based recovery to compression anti-thrashing protection (#14694)#15432
Open
Tranquil-Flow wants to merge 2 commits into
Conversation
55a4c53 to
6a38520
Compare
6a38520 to
8c47d4d
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?
The anti-thrashing guard in
ContextCompressorpermanently disables auto-compression after 2 consecutive ineffective compressions (each saving <10%). The counter_ineffective_compression_countonly resets when a compression is effective or on session reset — there is no time-based or growth-based recovery. In long sessions, once 2 bad compressions happen, auto-compression is dead for the remainder of the session.This PR adds time-based and growth-based recovery: the counter resets if 300+ seconds have elapsed since the last compression, or if prompt tokens have grown by 20%+ since then. Both conditions indicate enough new context has accumulated to make another compression attempt worthwhile.
Related Issue
Fixes #14694
Type of Change
Changes Made
agent/context_compressor.py:_last_compression_timefield to track when the last compression occurred_last_compression_prompt_tokensfield to track prompt size at last compressionshould_compress(), before the>= 2check, reset the counter if either (a) 300+ seconds have elapsed since the last compression, or (b) prompt tokens have grown by 20%+ since the last compression.compress()on_session_reset()How to Test
Tests:
tests/agent/test_context_compressor.py— 5 new tests inTestAntiThrashingRecovery:Tested 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