The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): retry stalled fallback in the same turn
KoNit-K8e64cc3347d1
diff --git a/agent/compression_facade.py b/agent/compression_facade.pyindex 6d55c2ab6db..2e4c0b75c52 100644--- a/agent/compression_facade.py+++ b/agent/compression_facade.py@@ -126,6 +126,21 @@ def _run_under_progress_timeout(result_msgs, result_prompt = run(fence, target_messages=snapshot)return (messages if result_msgs is snapshot else result_msgs), result_prompt+ def _same_turn_fallback_worker(fence=None):+ """Run the pinned fallback as recovery for the just-stalled attempt.++ ``_report_compression_timeout`` records the primary route's cooldown+ only after the retry returns. The primary worker can still record its+ own cooldown while unwinding, however, so the retry must explicitly+ bypass that one guard. It does not clear the cooldown and leaves the+ structural breakers in force.+ """+ snapshot = copy.deepcopy(messages)+ result_msgs, result_prompt = run(+ fence, target_messages=snapshot, same_turn_fallback_recovery=True,+ )+ return (messages if result_msgs is snapshot else result_msgs), result_prompt+timeout_cause = {"total_exhausted": False, "progress_observed": False}def _on_timeout_cause(total_exhausted, progress_observed):@@ -150,7 +165,7 @@ def _run_under_progress_timeout(idle_timeout_seconds=idle_timeout, total_ceiling_seconds=total_ceiling, on_timeout=_on_timeout,on_timeout_cause=_on_timeout_cause,on_commit_overrun=lambda waited, ceiling: _warn_commit_overrun(agent, waited, ceiling), fence=active_fence,- telemetry_agent=agent, new_fence=_publish_new_fence,+ telemetry_agent=agent, new_fence=_publish_new_fence, fallback_worker=_same_turn_fallback_worker,)@@ -242,11 +257,11 @@ class CompressionFacadeMixin:self._active_compression_commit_fence = active_fencetry:- def _run(fence=None, target_messages=None):+ def _run(fence=None, target_messages=None, same_turn_fallback_recovery=False):return compress_context(self, target_messages if target_messages is not None else messages, system_message,approx_tokens=approx_tokens, task_id=task_id, focus_topic=focus_topic, force=force,- bypass_cooldown=bypass_cooldown,+ bypass_cooldown=bypass_cooldown or same_turn_fallback_recovery,defer_context_engine_notification=(defer_context_engine_notification), commit_fence=fence,)diff --git a/agent/conversation_compression.py b/agent/conversation_compression.pyindex 291b6e565da..069ac3a48a5 100644--- a/agent/conversation_compression.py+++ b/agent/conversation_compression.py@@ -1027,6 +1027,7 @@ def run_compress_context_with_progress_timeout(on_commit_overrun: Optional[Callable[[float, float], None]] = None,fence: Optional[CompressionCommitFence] = None, telemetry_agent: Any = None, stall_fallback: bool = True,new_fence: Optional[Callable[[], CompressionCommitFence]] = None,+ fallback_worker: Optional[Callable[[CompressionCommitFence], Tuple[list, str]]] = None,) -> Tuple[list, str]:"""Run ``worker(fence)`` under a sync progress-aware (idle + ceiling) timeout.Budgets bound the PRE-commit phase only: an admitted commit always completes (overrun logged, surfaced@@ -1139,7 +1140,7 @@ def run_compress_context_with_progress_timeout(# the summary-failure cooldown, which would no-op the retry's summary call.if stall_fallback:recovered = _retry_compression_on_fallback_chain(- worker=worker, messages=messages, system_prompt_fallback=system_prompt_fallback,+ worker=fallback_worker or worker, messages=messages, system_prompt_fallback=system_prompt_fallback,idle_timeout_seconds=idle, total_ceiling_seconds=ceiling, on_commit_overrun=on_commit_overrun,on_timeout_cause=on_timeout_cause, telemetry_agent=telemetry_agent, new_fence=new_fence,)diff --git a/tests/agent/test_compression_stall_fallback.py b/tests/agent/test_compression_stall_fallback.pyindex 0c802d9f066..9fb6c3cc33c 100644--- a/tests/agent/test_compression_stall_fallback.py+++ b/tests/agent/test_compression_stall_fallback.py@@ -29,6 +29,7 @@ from agent.context_compressor import (pin_summary_route,take_pinned_summary_route,)+from agent.compression_facade import CompressionFacadeMixinfrom agent.conversation_compression import (CompressionCommitFence,resolve_compression_fallback_route,@@ -230,6 +231,53 @@ def test_fallback_that_also_stalls_degrades_after_one_attempt():assert len(timeouts) == 1, "the degrade must be reported exactly once"+class _FacadeStallAgent(CompressionFacadeMixin):+ """Small facade host for the cooldown-bypass handoff contract."""++ session_id = "STALL_FALLBACK_FACADE"+ _cached_system_prompt = "cached-prompt"++ def _conversation_root_id(self):+ return None+++def test_facade_marks_stall_fallback_as_same_turn_recovery(monkeypatch):+ """A primary timeout records its cooldown before the fallback worker runs.++ The retry is still part of that same recovery attempt, so it must reach+ ``compress_context`` with the narrow cooldown bypass. A normal automatic+ attempt remains subject to the cooldown on the next turn.+ """+ agent = _FacadeStallAgent()+ original = [{"role": "user", "content": "keep-me"}]+ calls = []++ def _compress_context(_agent, messages, _system_message, *, commit_fence, bypass_cooldown=False, **_kwargs):+ route = take_pinned_summary_route()+ calls.append((route, bypass_cooldown))+ if route is None:+ while not commit_fence.is_cancelled:+ threading.Event().wait(0.001)+ return messages, "primary-cancelled"+ if not bypass_cooldown:+ return messages, "cooldown-blocked"+ return [{"role": "user", "content": "fallback summary"}], "fallback-prompt"++ monkeypatch.setattr("agent.conversation_compression.compress_context", _compress_context)+ monkeypatch.setattr(+ "agent.conversation_compression.resolve_context_compression_timeouts", lambda: (0.02, 1.0)+ )++ with _patch_chain([CHAIN_ENTRY]):+ messages, prompt = agent._compress_context(original, "system")++ assert calls[0] == (None, False)+ assert calls[1][0] is not None+ assert calls[1][1] is True+ assert messages == [{"role": "user", "content": "fallback summary"}]+ assert prompt == "fallback-prompt"++# ---------------------------------------------------------------------------# Route resolution: a chain entry becomes an explicit summary route# ---------------------------------------------------------------------------