The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): floor pre-API compaction pressure at the last real prompt size
Darafei Praliaskouski73b8ec3ceab2
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.pyindex 05b19e8b4e6..4fe3c9f7526 100644--- a/agent/conversation_loop.py+++ b/agent/conversation_loop.py@@ -609,6 +609,28 @@ def _image_error_max_dimension(error: Exception) -> Optional[int]:return None+def _pressure_with_real_floor(compressor: Any, rough_tokens: int) -> int:+ """Floor the rough pre-API pressure estimate at the last REAL prompt size.++ The chars/4 rough estimate under-counts Cyrillic (and other non-ASCII+ scripts) by up to ~2x, so a session can sit at the provider's real context+ ceiling while the rough figure stays under the compaction threshold — on+ silent-clip providers (ollama /v1) that is a truncation death spiral the+ reactive overflow handler never sees (observed live: real prompts+ 64,842→64,995 against a 55,705 threshold). The provider's own last+ reported prompt_tokens is authoritative; never let the pressure figure+ fall below it. Skipped for exactly one turn after a compaction, when+ last_real_prompt_tokens still holds the stale pre-compression value+ (#36718's awaiting_real_usage_after_compression window).+ """+ last_real = int(getattr(compressor, "last_real_prompt_tokens", 0) or 0)+ if last_real > rough_tokens and not getattr(+ compressor, "awaiting_real_usage_after_compression", False+ ):+ return last_real+ return rough_tokens++def _ollama_context_limit_error(agent: Any, request_tokens: int) -> Optional[str]:"""Return a user-facing error when Ollama is loaded with too little context."""if not getattr(agent, "tools", None):@@ -2882,6 +2904,9 @@ def run_conversation()if _anchored_pressure is not None:request_pressure_tokens = _anchored_pressure+ request_pressure_tokens = _pressure_with_real_floor(+ agent.context_compressor, request_pressure_tokens+ )total_chars = approx_tokens * 4# Stash this request's rough estimate so update_from_response() can# pair it with the provider's real prompt count — the (rough, real)diff --git a/tests/run_agent/test_infinite_compaction_loop.py b/tests/run_agent/test_infinite_compaction_loop.pyindex 79c6b734be6..6082cddff44 100644--- a/tests/run_agent/test_infinite_compaction_loop.py+++ b/tests/run_agent/test_infinite_compaction_loop.py@@ -262,3 +262,33 @@ class TestCodexSparkShortSessionBoundary:f"This would cause the silent context wipe described in #48621.")assert comp.has_content_to_compress(messages) is True+++class TestPressureRealFloor:+ """Regression: Cyrillic-heavy sessions under-count in the rough estimate,+ letting real prompts ride the provider window (64,842→64,995 observed)+ while the pre-API gate saw sub-threshold pressure."""++ def _compressor(self, last_real, awaiting=False):+ class _C:+ last_real_prompt_tokens = last_real+ awaiting_real_usage_after_compression = awaiting+ return _C()++ def test_real_floor_lifts_undercounted_rough(self):+ from agent.conversation_loop import _pressure_with_real_floor+ assert _pressure_with_real_floor(self._compressor(64_842), 45_000) == 64_842++ def test_rough_wins_when_larger(self):+ from agent.conversation_loop import _pressure_with_real_floor+ assert _pressure_with_real_floor(self._compressor(30_000), 45_000) == 45_000++ def test_stale_real_ignored_right_after_compaction(self):+ from agent.conversation_loop import _pressure_with_real_floor+ compressor = self._compressor(64_842, awaiting=True)+ assert _pressure_with_real_floor(compressor, 20_000) == 20_000++ def test_zero_and_missing_real_are_safe(self):+ from agent.conversation_loop import _pressure_with_real_floor+ assert _pressure_with_real_floor(self._compressor(0), 10_000) == 10_000+ assert _pressure_with_real_floor(object(), 10_000) == 10_000