Skip to content

The agent that grows with you

fix(compression): slim the same-turn fallback worker and pin it with a real-path test

teknium120d2903e391d
diff --git a/agent/compression_facade.py b/agent/compression_facade.py
index 2e4c0b75c52..52ef5ff5dee 100644
--- a/agent/compression_facade.py
+++ b/agent/compression_facade.py
@@ -6,8 +6,9 @@ Extracted from ``run_agent.py``; every method resolves through ``AIAgent``'s MRO
"""
import contextlib
-import logging
import copy
+import functools
+import logging
import threading
from agent.session_activity import ActivityProvenance
@@ -113,7 +114,7 @@ def _run_under_progress_timeout(
returns the snapshot unchanged, so the ORIGINAL list is handed back to keep identity semantics."""
from agent.conversation_compression import CompressionCommitFence, run_compress_context_with_progress_timeout
- def _snapshot_worker(fence=None):
+ def _snapshot_worker(fence=None, *, same_turn_fallback_recovery=False):
# #76354 review F3: the pooled worker must NEVER share the caller's live transcript. Plugin/legacy
# context engines are allowed to mutate their input list in place; after a host timeout the worker
# stays alive, so a shared list would let a late engine rewrite the live conversation (roles,
@@ -123,24 +124,17 @@ def _run_under_progress_timeout(
# on timeout/cancel); durable SessionDB mutation is already gated behind the commit fence inside
# compress_context.
snapshot = copy.deepcopy(messages)
- 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,
+ fence, target_messages=snapshot, same_turn_fallback_recovery=same_turn_fallback_recovery
)
return (messages if result_msgs is snapshot else result_msgs), result_prompt
+ # The stall-fallback retry is the same recovery attempt as the stalled primary, but the cancelled primary
+ # worker records its stall_interrupted cooldown while unwinding — racing the retry's automatic gate
+ # (#112387). The retry therefore bypasses ONLY the summary-failure cooldown (never clears it; the
+ # structural breakers stay in force), exactly like provider-proven overflow recovery.
+ _same_turn_fallback_worker = functools.partial(_snapshot_worker, same_turn_fallback_recovery=True)
+
timeout_cause = {"total_exhausted": False, "progress_observed": False}
def _on_timeout_cause(total_exhausted, progress_observed):
diff --git a/tests/agent/test_compression_stall_fallback.py b/tests/agent/test_compression_stall_fallback.py
index 9fb6c3cc33c..617c49c26d7 100644
--- a/tests/agent/test_compression_stall_fallback.py
+++ b/tests/agent/test_compression_stall_fallback.py
@@ -29,7 +29,6 @@ from agent.context_compressor import (
pin_summary_route,
take_pinned_summary_route,
)
-from agent.compression_facade import CompressionFacadeMixin
from agent.conversation_compression import (
CompressionCommitFence,
resolve_compression_fallback_route,
@@ -231,51 +230,68 @@ 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):
+def test_same_turn_fallback_retry_is_not_gated_by_the_primary_stall_backoff(tmp_path, monkeypatch):
+ """The cancelled primary worker persists ``stall_interrupted`` while the fallback retry is already
+ running; that cooldown must not no-op the retry (#112387). Real AIAgent, real ``compress_context``
+ and facade timeout wrap; only the summary route (``compress``) is stubbed: the primary route stalls
+ silently until the host cancels its fence, the pinned fallback route returns a real compressed list."""
+ import os
+ import time
+ from pathlib import Path
+
+ import agent.conversation_compression as cc
+ from agent.auxiliary_client import AuxiliaryExplicitCancellation
+ from hermes_state import SessionDB
+
+ session_id = "STALL_FALLBACK_SAME_TURN"
+ db = SessionDB(db_path=Path(tmp_path) / "state.db")
+ db.create_session(session_id, source="cli")
+ with patch.dict(os.environ, {"OPENROUTER_API_KEY": "test-key"}):
+ from run_agent import AIAgent
+
+ agent = AIAgent(
+ api_key="test-key", base_url="https://openrouter.ai/api/v1", model="test/model", quiet_mode=True,
+ session_db=db, session_id=session_id, skip_context_files=True, skip_memory=True,
+ )
+ agent._compression_feasibility_checked = True
+ agent.compression_in_place = True
+ agent._cached_system_prompt = "sys"
+ compressor = agent.context_compressor
+ compressor.threshold_tokens = 1_000
+ original = [{"role": "user", "content": f"m{i} " + "x" * 400} for i in range(20)]
+ live = list(original)
+ routes = []
+
+ def fake_compress(messages, **kwargs):
route = take_pinned_summary_route()
- calls.append((route, bypass_cooldown))
+ routes.append((route["label"] if route else None, kwargs.get("bypass_cooldown", False)))
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)
- )
-
+ cancelled = getattr(compressor, "_compression_cancelled_check", None)
+ deadline = time.monotonic() + 5
+ while time.monotonic() < deadline and not (callable(cancelled) and cancelled()):
+ time.sleep(0.0005)
+ raise AuxiliaryExplicitCancellation()
+ return [{"role": "user", "content": "summary of earlier turns"}, original[-1]]
+
+ compressor.compress = fake_compress
+ real_route = cc.resolve_compression_fallback_route
+
+ def route_after_primary_unwound():
+ # Pin the race outcome from the report: the primary persisted its backoff before the retry ran.
+ deadline = time.monotonic() + 3
+ while time.monotonic() < deadline and compressor._summary_failure_cooldown_until <= time.monotonic():
+ time.sleep(0.001)
+ return real_route()
+
+ monkeypatch.setattr(cc, "resolve_context_compression_timeouts", lambda compression_cfg=None: (0.4, 4.0))
+ monkeypatch.setattr(cc, "resolve_compression_fallback_route", route_after_primary_unwound)
with _patch_chain([CHAIN_ENTRY]):
- messages, prompt = agent._compress_context(original, "system")
+ out_msgs, _prompt = agent._compress_context(live, "sys", approx_tokens=50_000)
- 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"
+ assert [r[0] for r in routes] == [None, "fallback_chain[0](custom)"]
+ assert routes[1][1] is True, "the same-turn retry must bypass the cooldown the primary just armed"
+ assert len(out_msgs) == 2 and out_msgs is not live
+ assert getattr(agent, "_last_compression_timed_out", None) is not True
# ---------------------------------------------------------------------------