From 5ce8e347672caa6365bf4aa6ff3875f08bc44eeb Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Tue, 28 Jul 2026 18:51:55 +0500 Subject: [PATCH] test(compression): resolve context_length inside mocks across suites hit by lazy-init deferral Baseline diff vs clean upstream/main (182 pre-existing environmental failures on both sides) showed exactly 6 PR-caused failures, all the same mechanism: tests construct ContextCompressor under a get_model_context_length mock and read threshold_percent/threshold_tokens after the with block, or build via object.__new__ and assign context_length AFTER the derived budgets (the setter now resets the lazily-cached budgets). - test_compression_small_ctx_threshold_floor: resolve inside _make() - test_cjk_token_estimation: resolve inside mock - test_per_model_compression_threshold: resolve inside mock (2 tests) - test_pre_compress_memory_context: assign context_length before threshold/tail/summary budgets; add summary_target_ratio --- tests/agent/test_cjk_token_estimation.py | 2 ++ tests/agent/test_compression_small_ctx_threshold_floor.py | 6 +++++- tests/agent/test_pre_compress_memory_context.py | 6 +++++- tests/run_agent/test_per_model_compression_threshold.py | 4 ++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/agent/test_cjk_token_estimation.py b/tests/agent/test_cjk_token_estimation.py index 71299a7ce112..aee030298487 100644 --- a/tests/agent/test_cjk_token_estimation.py +++ b/tests/agent/test_cjk_token_estimation.py @@ -35,6 +35,8 @@ def test_cjk_tail_does_not_expand_to_english_char_budget(): summary_target_ratio=0.2, quiet_mode=True, ) + # Resolve while the mock is active (lazy init, #32221). + _ = compressor.context_length messages = [ {"role": "user", "content": "head 1"}, diff --git a/tests/agent/test_compression_small_ctx_threshold_floor.py b/tests/agent/test_compression_small_ctx_threshold_floor.py index 03c64a00cbde..5b8c0a5010cc 100644 --- a/tests/agent/test_compression_small_ctx_threshold_floor.py +++ b/tests/agent/test_compression_small_ctx_threshold_floor.py @@ -20,9 +20,13 @@ from agent.context_compressor import ContextCompressor def _make(ctx: int, pct: float = 0.50) -> ContextCompressor: with patch.object(cc, "get_model_context_length", return_value=ctx): - return ContextCompressor( + comp = ContextCompressor( model="test/model", threshold_percent=pct, quiet_mode=True, ) + # Resolve while the mock is active — lazy init (#32221) defers the + # window probe (and the floor application) past __init__. + _ = comp.context_length + return comp class TestSmallContextThresholdFloor: diff --git a/tests/agent/test_pre_compress_memory_context.py b/tests/agent/test_pre_compress_memory_context.py index 14f8addb42a8..d33bc7fa4ad4 100644 --- a/tests/agent/test_pre_compress_memory_context.py +++ b/tests/agent/test_pre_compress_memory_context.py @@ -13,10 +13,14 @@ def _make_compressor(): compressor = ContextCompressor.__new__(ContextCompressor) compressor.protect_first_n = 2 compressor.protect_last_n = 5 - compressor.tail_token_budget = 20_000 + # Set context_length BEFORE the derived budgets: its setter resets the + # lazily-cached threshold/tail/summary budgets (#32221 lazy init), so + # assigning it later would clear the explicit values below. compressor.context_length = 200_000 compressor.threshold_percent = 0.80 compressor.threshold_tokens = 160_000 + compressor.summary_target_ratio = 0.20 + compressor.tail_token_budget = 20_000 compressor.max_summary_tokens = 10_000 compressor.quiet_mode = True compressor.compression_count = 0 diff --git a/tests/run_agent/test_per_model_compression_threshold.py b/tests/run_agent/test_per_model_compression_threshold.py index 575cd6b165e4..1259b0403175 100644 --- a/tests/run_agent/test_per_model_compression_threshold.py +++ b/tests/run_agent/test_per_model_compression_threshold.py @@ -89,6 +89,8 @@ class TestContextCompressorModelThresholds: model_thresholds={"glm-5.2": 0.40}, quiet_mode=True, ) + # Resolve while mock is active (lazy init defers floor past __init__). + _ = cc.context_length # 256K < 512K → floor at 0.75; override 0.40 < 0.75, so floor wins assert cc.threshold_percent == 0.75 assert cc.threshold_tokens == int(256_000 * 0.75) @@ -113,6 +115,8 @@ class TestContextCompressorModelThresholds: threshold_percent=0.50, quiet_mode=True, ) + # Resolve while mock is active (lazy init defers floor past __init__). + _ = cc.context_length # 256K < 512K → floored at 0.75 assert cc.threshold_percent == 0.75 assert cc.model_thresholds == {}