From 8bdec8088204a321b15dabe7c0df731ad3a66ae1 Mon Sep 17 00:00:00 2001 From: ai-ag2026 <261867348+ai-ag2026@users.noreply.github.com> Date: Sun, 3 May 2026 16:01:28 +0200 Subject: [PATCH] fix(agent): surface preflight compression status Preflight compression can run synchronously before the first model call when a loaded session exceeds the active context threshold. Gateway users saw no visible progress while the compression LLM call was in flight, which can look like a dropped message during long compactions.\n\nEmit the existing lifecycle status through _emit_status before starting preflight compression so CLI, gateway, and WebUI status callbacks all get immediate feedback.\n\nAdds a regression assertion for the preflight path. --- run_agent.py | 10 +++++----- tests/run_agent/test_413_compression.py | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/run_agent.py b/run_agent.py index cfcd325eb..2e0baa2a4 100644 --- a/run_agent.py +++ b/run_agent.py @@ -10623,11 +10623,11 @@ class AIAgent: self.model, f"{self.context_compressor.context_length:,}", ) - if not self.quiet_mode: - self._safe_print( - f"📦 Preflight compression: ~{_preflight_tokens:,} tokens " - f">= {self.context_compressor.threshold_tokens:,} threshold" - ) + self._emit_status( + f"📦 Preflight compression: ~{_preflight_tokens:,} tokens " + f">= {self.context_compressor.threshold_tokens:,} threshold. " + "This may take a moment." + ) # May need multiple passes for very large sessions with small # context windows (each pass summarises the middle N turns). for _pass in range(3): diff --git a/tests/run_agent/test_413_compression.py b/tests/run_agent/test_413_compression.py index 8bd357d3d..5410f196e 100644 --- a/tests/run_agent/test_413_compression.py +++ b/tests/run_agent/test_413_compression.py @@ -432,6 +432,8 @@ class TestPreflightCompression: ok_resp = _mock_response(content="After preflight", finish_reason="stop") agent.client.chat.completions.create.side_effect = [ok_resp] + status_messages = [] + agent.status_callback = lambda ev, msg: status_messages.append((ev, msg)) with ( patch.object(agent, "_compress_context") as mock_compress, @@ -460,6 +462,10 @@ class TestPreflightCompression: ) assert result["completed"] is True assert result["final_response"] == "After preflight" + assert any( + ev == "lifecycle" and "Preflight compression" in msg + for ev, msg in status_messages + ) def test_no_preflight_when_under_threshold(self, agent): """When history fits within context, no preflight compression needed."""