The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
compression: emit periodic client-visible status during compaction
Jack8381d4fadfe5
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.pyindex 61845604998..a4922c7f594 100644--- a/agent/conversation_compression.py+++ b/agent/conversation_compression.py@@ -2337,6 +2337,7 @@ class _CompressionActivityHeartbeat:# if a prior timeout/cooldown stamp is still on the agent.self._suppressed = Falseself._touch("context compression started", allow_terminal_overwrite=True)+ self._emit_progress_status()self._thread.start()return self@@ -2397,11 +2398,38 @@ class _CompressionActivityHeartbeat:except Exception:logger.debug("compression activity heartbeat touch failed", exc_info=True)+ def _emit_progress_status(self) -> None:+ """Publish a client-visible ``compacting`` status line.++ Compression can stream for minutes with no deltas, tool events, or+ status lines reaching remote transports. Idle-progress watchdogs on+ those clients (e.g. the Android relay app's 180s turn watchdog)+ treat the silence as a dead turn and fire ``session.interrupt`` β+ killing a healthy compression mid-flight and rolling back its work,+ which retriggers on the next prompt and loops forever on sessions+ near the context ceiling. A periodic ``status_callback`` heartbeat+ gives every transport a progress event to re-arm on.+ """+ status_callback = getattr(self._agent, "status_callback", None)+ if not status_callback:+ return+ try:+ status_callback(+ "compacting",+ f"ποΈ {COMPACTION_STATUS_MARKER} β still summarizing "+ "earlier conversation so I can continue...",+ )+ except Exception:+ logger.debug(+ "status_callback error in compression heartbeat", exc_info=True+ )+def _run(self) -> None:while not self._stop.wait(self._interval_seconds):if self._should_suppress():returnself._touch("context compression in progress")+ self._emit_progress_status()def _direct_messages_for_pre_compress_memory(messages: Any) -> list[dict[str, Any]]:"""Return direct user/assistant evidence safe for memory checkpointing.diff --git a/tests/agent/test_compression_concurrent_fork.py b/tests/agent/test_compression_concurrent_fork.pyindex a155b5ab659..9567fddbda2 100644--- a/tests/agent/test_compression_concurrent_fork.py+++ b/tests/agent/test_compression_concurrent_fork.py@@ -167,6 +167,48 @@ def test_compression_activity_heartbeat_touches_agent_during_long_compress(tmp_passert db.get_compression_lock_holder(session_id) is None+def test_compression_activity_heartbeat_emits_client_status_events(tmp_path: Path) -> None:+ """The heartbeat must emit ``compacting`` status events, not just DB touches.++ Remote transports (e.g. the Android relay app) run idle-progress turn+ watchdogs that ``session.interrupt`` a turn after ~180s with no gateway+ events. Compression is silent on the event stream, so without periodic+ ``status_callback`` heartbeats a long compression is killed mid-flight+ and retriggers forever on sessions near the context ceiling.+ """+ db = SessionDB(db_path=tmp_path / "state.db")+ session_id = "HEARTBEAT_STATUS_TEST"+ db.create_session(session_id, source="test")++ agent = _build_agent_with_db(db, session_id)+ agent._compression_activity_heartbeat_interval = 0.1+ touch_calls: list[str] = []+ agent._touch_activity = lambda desc, **_kw: touch_calls.append(desc)+ status_events: list[tuple[str, str]] = []+ setattr(+ agent,+ "status_callback",+ lambda event, message: status_events.append((event, message)),+ )++ def _slow_compress(*_a, **_kw):+ _wait_for_touch(touch_calls, "context compression in progress")+ return [+ {"role": "user", "content": "[CONTEXT COMPACTION] summary"},+ {"role": "user", "content": "tail"},+ ]++ agent.context_compressor.compress.side_effect = _slow_compress+ messages = [{"role": "user", "content": f"m{i}"} for i in range(20)]++ agent._compress_context(messages, "sys", approx_tokens=120_000)++ compacting = [event for event, _message in status_events if event == "compacting"]+ # One emit at heartbeat start plus at least one periodic tick while the+ # summary call blocks.+ assert len(compacting) >= 2++def test_lock_contender_preserves_terminal_compaction_lifecycle(tmp_path: Path) -> None:"""A lock loser still closes the structured compaction lifecycle.