Skip to content

The agent that grows with you

fix(compression): keep ContextCompressor init non-blocking when quiet_mode=False

kshitijk4poor44bd0521a479
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 38ed9b4df3a..27c98682273 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -1374,6 +1374,21 @@ class ContextCompressor(ContextEngine):
self.threshold_percent = self._effective_threshold_percent(
self._resolved_context_length, self._base_threshold_percent,
)
+ if self._log_init_summary:
+ # Emit once, on first resolution, so the informative startup
+ # line survives without forcing a synchronous probe in
+ # __init__ (#32221). Reads via the properties below are safe
+ # here because _resolved_context_length is already set.
+ self._log_init_summary = False
+ logger.info(
+ "Context compressor initialized: model=%s context_length=%d "
+ "threshold=%d (%.0f%%) target_ratio=%.0f%% tail_budget=%d "
+ "provider=%s base_url=%s",
+ self.model, self._resolved_context_length, self.threshold_tokens,
+ self.threshold_percent * 100, self.summary_target_ratio * 100,
+ self.tail_token_budget,
+ self.provider or "none", self.base_url or "none",
+ )
return self._resolved_context_length
@property
@@ -2078,16 +2093,12 @@ class ContextCompressor(ContextEngine):
self._max_summary_tokens: int | None = None
self.compression_count = 0
- if not quiet_mode:
- logger.info(
- "Context compressor initialized: model=%s context_length=%d "
- "threshold=%d (%.0f%%) target_ratio=%.0f%% tail_budget=%d "
- "provider=%s base_url=%s",
- model, self.context_length, self.threshold_tokens,
- threshold_percent * 100, self.summary_target_ratio * 100,
- self.tail_token_budget,
- provider or "none", base_url or "none",
- )
+ # The "initialized" log reports resolved token budgets, which would
+ # force the deferred get_model_context_length() probe to run inside
+ # __init__ and re-introduce the exact synchronous blocking this change
+ # removes (#32221). Emit it on first context-length resolution instead
+ # so construction stays non-blocking on every path (not just quiet).
+ self._log_init_summary = not quiet_mode
self._context_probed = False # True after a step-down from context error
self.last_prompt_tokens = 0
diff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.py
index 84ec3ac39df..a8cfedc6e8b 100644
--- a/tests/agent/test_context_compressor.py
+++ b/tests/agent/test_context_compressor.py
@@ -3527,6 +3527,49 @@ class TestLazyContextResolution:
_ = c.context_length
mock_get.assert_called_once()
+ def test_init_does_not_probe_when_not_quiet(self, caplog):
+ """quiet_mode=False must ALSO stay non-blocking in __init__.
+
+ Regression for the lazy-init defect: the "Context compressor
+ initialized" log reads context_length/threshold_tokens/tail_token_budget,
+ so emitting it in __init__ forced the deferred get_model_context_length()
+ probe to run during construction whenever quiet_mode was False (the
+ interactive CLI path) — silently re-introducing the #32221 blocking that
+ the original PR set out to remove. The informative line must instead be
+ emitted once, on first context-length resolution.
+ """
+ import logging
+
+ with patch(
+ "agent.context_compressor.get_model_context_length",
+ return_value=200_000,
+ ) as mock_get:
+ c = ContextCompressor(model="test/model", quiet_mode=False)
+ # No probe, and no init log, at construction time.
+ mock_get.assert_not_called()
+
+ with caplog.at_level(logging.INFO, logger="agent.context_compressor"):
+ _ = c.context_length
+ mock_get.assert_called_once()
+ init_lines = [
+ r for r in caplog.records
+ if "Context compressor initialized" in r.getMessage()
+ ]
+ assert len(init_lines) == 1, (
+ f"expected exactly one init log on first access, got {len(init_lines)}"
+ )
+
+ # Subsequent access must not re-probe or re-log.
+ with caplog.at_level(logging.INFO, logger="agent.context_compressor"):
+ _ = c.context_length
+ _ = c.threshold_tokens
+ mock_get.assert_called_once()
+ again = [
+ r for r in caplog.records
+ if "Context compressor initialized" in r.getMessage()
+ ]
+ assert len(again) == 1, "init log fired more than once"
+
def test_context_length_setter_bypasses_resolution(self):
"""Assigning to .context_length directly must skip the network probe
entirely and return the assigned value."""