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
This commit is contained in:
kshitijk4poor 2026-07-28 18:51:55 +05:00 committed by kshitij
parent fd53fa3eac
commit 5ce8e34767
4 changed files with 16 additions and 2 deletions

View file

@ -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"},

View file

@ -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:

View file

@ -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

View file

@ -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 == {}