The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): persist stall-interrupted backoff on pre-commit cancel (#96775)
HexLab98027b339e796b
diff --git a/agent/context_compressor.py b/agent/context_compressor.pyindex 6961cd43ad2..f690548e45c 100644--- a/agent/context_compressor.py+++ b/agent/context_compressor.py@@ -2947,9 +2947,16 @@ class ContextCompressor(ContextEngine):cooldown_seconds: float,error: Optional[str],) -> None:- cooldown_until = time.time() + cooldown_seconds- self._summary_failure_cooldown_until = time.monotonic() + cooldown_seconds+ now_mono = time.monotonic()+ new_mono = now_mono + float(cooldown_seconds)+ # Never shorten a longer live deadline (#96775). A later stall or+ # timeout records the latest error text but keeps the later of the+ # two clocks.+ if new_mono > self._summary_failure_cooldown_until:+ self._summary_failure_cooldown_until = new_monoself._last_summary_error = error+ remaining = max(0.0, self._summary_failure_cooldown_until - time.monotonic())+ cooldown_until = time.time() + remainingsession_db = getattr(self, "_session_db", None)session_id = getattr(self, "_session_id", "")@@ -2971,12 +2978,11 @@ class ContextCompressor(ContextEngine):logger.debug("compression failure cooldown persist failed (non-sqlite): %s", exc)def record_timeout_failure(self, error: str) -> None:- """Record a consecutive timeout failure using the shared cooldown ladder.+ """Record a consecutive timeout/stall failure using the shared ladder.- Used by both the summary-LLM exception handler (inline at line ~3714)- and the host-level ``compress_context`` timeout wrapper in- ``run_compress_context_with_progress_timeout``. Avoids re-implementing- the ladder at each call site (#62452).+ Used by the summary-LLM exception handler, the host-level+ ``compress_context`` timeout wrapper, and stall-interrupted+ pre-commit cancellation (#62452, #96775)."""_TIMEOUT_COOLDOWN_LADDER = (60, 300, 900)self._consecutive_timeout_failures = (diff --git a/agent/conversation_compression.py b/agent/conversation_compression.pyindex a6b0968cfb3..60e7e5d88d1 100644--- a/agent/conversation_compression.py+++ b/agent/conversation_compression.py@@ -909,6 +909,12 @@ class CompressionCommitFence:DEFAULT_CONTEXT_TIMEOUT_SECONDS = 120.0DEFAULT_CONTEXT_TOTAL_CEILING_SECONDS = 600.0+# Distinct from ``explicit_interrupt``: a /stop that arrived after the summary+# stream had already crossed the no-progress stall window (#96775). Ordinary+# early /stop stays cooldown-neutral; this class arms the durable backoff so+# the next automatic turn does not re-enter the same stalled strategy.+STALL_INTERRUPTED_FAILURE_CLASS = "stall_interrupted"+# Shared daemon pool for sync compress_context timeout wraps — analogous to# asyncio's default executor used by gateway session hygiene's# ``loop.run_in_executor(None, ...)``, but daemon so a fence-cancelled hung@@ -1030,6 +1036,101 @@ def resolve_context_compression_timeouts(return idle, ceiling+def compression_attempt_stalled(+ *,+ commit_fence: Optional[CompressionCommitFence],+ started_at: float,+ idle_timeout_seconds: Optional[float] = None,+) -> bool:+ """Return whether a pre-commit cancel landed after the stall window.++ An ordinary early ``/stop`` must stay cooldown-neutral. When the fence+ (or, without a fence, the attempt clock) has already sat idle for the+ configured compression inactivity budget, the interrupt is a stalled+ attempt — the same condition the host timeout uses — and the next+ automatic turn must not blindly retry that strategy (#96775).+ """+ idle = idle_timeout_seconds+ if idle is None:+ idle, _ceiling = resolve_context_compression_timeouts()+ try:+ idle = float(idle)+ except (TypeError, ValueError):+ return False+ if idle <= 0:+ return False+ if commit_fence is not None:+ try:+ return float(commit_fence.seconds_since_progress()) >= idle+ except Exception:+ return False+ try:+ return (time.monotonic() - float(started_at)) >= idle+ except (TypeError, ValueError):+ return False+++def _stall_source_fingerprint(+ agent: Any,+ messages: Any,+ approx_tokens: Optional[int],+) -> str:+ """Identity of the stalled source context + summary strategy."""+ compressor = getattr(agent, "context_compressor", None)+ model = (+ getattr(compressor, "summary_model", None)+ or getattr(agent, "model", None)+ or ""+ )+ n_messages = len(messages) if isinstance(messages, list) else 0+ try:+ tokens = int(approx_tokens or 0)+ except (TypeError, ValueError):+ tokens = 0+ return f"msgs={n_messages}:tokens={tokens}:model={model}"+++def _record_stall_interrupted_backoff(+ agent: Any,+ *,+ commit_fence: Optional[CompressionCommitFence],+ started_at: float,+ messages: Any,+ approx_tokens: Optional[int],+) -> bool:+ """Persist a stall-interrupted cooldown after snapshot restore.++ Must run *after* ``_restore_compressor_attempt_state`` so rollback cannot+ wipe the new row. Returns True when the stall backoff was recorded.+ """+ if not compression_attempt_stalled(+ commit_fence=commit_fence, started_at=started_at+ ):+ return False+ compressor = getattr(agent, "context_compressor", None)+ record = getattr(compressor, "record_timeout_failure", None)+ if not callable(record):+ return False+ error = (+ f"{STALL_INTERRUPTED_FAILURE_CLASS}:"+ f"{_stall_source_fingerprint(agent, messages, approx_tokens)}"+ )+ try:+ record(error)+ except Exception:+ logger.debug(+ "stall-interrupted compression cooldown persist failed",+ exc_info=True,+ )+ return False+ logger.info(+ "Recorded stall-interrupted compression backoff (session=%s, %s)",+ getattr(agent, "session_id", None) or "none",+ error,+ )+ return True++def resolve_compression_fallback_route() -> Optional[dict]:"""Return the first usable ``auxiliary.compression.fallback_chain`` entry.@@ -3716,6 +3817,15 @@ def compress_context(and messages != messages_before_compression):messages[:] = copy.deepcopy(messages_before_compression)+ # Record after restore so rollback cannot wipe a stall backoff, and+ # while the lease is still held so the next turn cannot race it.+ _stall_backoff = _record_stall_interrupted_backoff(+ agent,+ commit_fence=commit_fence,+ started_at=_attempt_started_at,+ messages=messages,+ approx_tokens=approx_tokens,+ )if _activity_heartbeat is not None:_activity_heartbeat.stop("context compression cancelled")_activity_heartbeat = None@@ -3725,7 +3835,11 @@ def compress_context(started_at=_attempt_started_at,commit_status="aborted",split_status="aborted",- failure_class="explicit_interrupt",+ failure_class=(+ STALL_INTERRUPTED_FAILURE_CLASS+ if _stall_backoff+ else "explicit_interrupt"+ ),)_existing_sp = getattr(agent, "_cached_system_prompt", None)if not _existing_sp:@@ -3870,6 +3984,13 @@ def compress_context(agent.session_id or "none",)agent._last_compaction_in_place = False+ _stall_backoff = _record_stall_interrupted_backoff(+ agent,+ commit_fence=commit_fence,+ started_at=_attempt_started_at,+ messages=messages,+ approx_tokens=approx_tokens,+ )_existing_sp = getattr(agent, "_cached_system_prompt", None)if not _existing_sp:_existing_sp = agent._build_system_prompt(system_message)@@ -3878,7 +3999,11 @@ def compress_context(started_at=_attempt_started_at,commit_status="aborted",split_status="aborted",- failure_class="commit_fence_cancelled",+ failure_class=(+ STALL_INTERRUPTED_FAILURE_CLASS+ if _stall_backoff+ else "commit_fence_cancelled"+ ),)_release_lock()return messages, _existing_spdiff --git a/hermes_state.py b/hermes_state.pyindex dac6fcd2af4..37b7003adc1 100644--- a/hermes_state.py+++ b/hermes_state.py@@ -7438,10 +7438,16 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)returndef _do(conn):+ # Merge-max with any longer live deadline so a later shorter+ # write cannot reopen the thrash window (#96775). The error+ # column always takes the latest diagnostic.conn.execute(- "UPDATE sessions SET compression_failure_cooldown_until = ?, "+ "UPDATE sessions SET compression_failure_cooldown_until = CASE "+ "WHEN compression_failure_cooldown_until IS NOT NULL "+ " AND compression_failure_cooldown_until > ? "+ "THEN compression_failure_cooldown_until ELSE ? END, ""compression_failure_error = ? WHERE id = ?",- (cooldown_until, error, session_id),+ (cooldown_until, cooldown_until, error, session_id),)try: