Skip to content

The agent that grows with you

fix(compression): classify a Codex stream-guard stall as a timeout, not a terminal network failure (#124077)

happy531856ee7d6f0992
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 87fbe7c8670..5836a869afc 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -744,16 +744,22 @@ def _classify_summary_failure(e: Exception) -> _SummaryFailureKind:
"""
status = _exc_status_code(e)
err = str(e).lower()
+ timeout = (isinstance(e, TimeoutError)
+ or status in {408, 429, 502, 504}
+ or "timeout" in err or "timed out" in err or "stalled" in err)
return _SummaryFailureKind(
# Permanent-looking error on a distinct summary model: fall back to main instead of cooldown.
model_not_found=status in {404, 503}
or any(m in err for m in ("model_not_found", "does not exist", "no available channel")),
- timeout=status in {408, 429, 502, 504} or "timeout" in err or "timed out" in err,
+ timeout=timeout,
# Malformed/non-JSON bodies (HTML 502 as application/json) surface as JSONDecodeError or
# APIResponseValidationError "expecting value"; treat as transient.
json_decode=isinstance(e, json.JSONDecodeError) or "expecting value" in err,
# httpx premature-close errors are transient; treat like a timeout, not a 60s cooldown.
- streaming_closed=_is_connection_error(e),
+ # Anything classified as a timeout (including a "timed out"/"stalled" message on a
+ # non-TimeoutError type) stays out of this class: it is a retry-ladder timeout, NOT a
+ # terminal network failure that aborts compression (#124077).
+ streaming_closed=_is_connection_error(e) and not timeout,
# HTTP 200 with empty body from a degraded provider, plus the sibling "no usable response"
# shapes from _validate_llm_response.
empty_content=isinstance(e, RuntimeError) and any(
diff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.py
index b5fca9fc370..f3b3a6e759b 100644
--- a/tests/agent/test_context_compressor.py
+++ b/tests/agent/test_context_compressor.py
@@ -3,6 +3,24 @@
import json
import re
import sqlite3
+
+# Local-env workaround (#124077 tests): the checkout's venv python3 is a
+# symlink into <home>/hermes-agent/.hermes-runtime/..., so stdlib zoneinfo's
+# first import (sysconfig._safe_realpath(sys.executable)) trips the real-home
+# IO guard after it installs. Importing here — during collection, before any
+# autouse guard fixture — caches the module. CI pythons are not symlinked into
+# the home, so this is a no-op there.
+import zoneinfo # noqa: F401
+
+# Same family of local-env workaround: pydantic's plugin discovery scans ALL
+# distributions on first model construction via importlib.metadata — under the
+# IO guard that scan hits the venv site-packages inside the real home. Pre-warm
+# the plugin loader here (during collection, before any autouse guard fixture)
+# so the scan happens exactly once, outside the guard. CI pythons do not live
+# under the home, so this is a no-op there.
+from pydantic.plugin import _loader as _pydantic_plugin_loader # noqa: E402
+_pydantic_plugin_loader.get_plugins() # noqa: E402 (warm the scan cache)
+
import pytest
import time
from unittest.mock import patch, MagicMock
@@ -3693,3 +3711,62 @@ class TestSanitizeToolPairsWhitespace:
tool_call_ids = [m.get("tool_call_id") for m in out if m.get("role") == "tool"]
assert "call_orphan" not in tool_call_ids, "genuinely orphaned result must be removed"
assert " call_orphan " not in tool_call_ids, "original whitespace form must also be gone"
+
+
+class TestSummaryFailureClassification124077:
+ """#124077: a Codex stream-guard stall raises TimeoutError whose message
+ contains 'stalled' (not 'timeout'); it must classify as a timeout (retry
+ ladder + deterministic fallback) instead of a terminal network failure
+ that aborts compression and lets the gateway wipe the session."""
+
+ def _make_compressor(self):
+ with patch("agent.context_compressor.get_model_context_length", return_value=100000):
+ return ContextCompressor(model="test", quiet_mode=True)
+
+ def test_classify_codex_stall_is_timeout_not_streaming_closed(self):
+ from agent.context_compressor import _classify_summary_failure
+ kind = _classify_summary_failure(TimeoutError(
+ "Codex auxiliary Responses stream stalled: no new output for 60.0s (67.7s elapsed)"))
+ assert kind.timeout is True
+ assert kind.streaming_closed is False
+
+ def test_classify_plain_connection_error_stays_streaming_closed(self):
+ from agent.context_compressor import _classify_summary_failure
+ kind = _classify_summary_failure(ConnectionError("Connection error."))
+ assert kind.timeout is False
+ assert kind.streaming_closed is True
+
+ def test_classify_timed_out_message_is_timeout(self):
+ from agent.context_compressor import _classify_summary_failure
+ kind = _classify_summary_failure(RuntimeError("request timed out after 120s"))
+ assert kind.timeout is True
+ assert kind.streaming_closed is False
+
+ def test_codex_stall_does_not_flag_terminal_network_failure(self):
+ """Integration: the stalled-summary path must NOT arm the terminal
+ network-failure abort (which kills the session on compression
+ exhaustion); it takes the retry-ladder cooldown instead."""
+ c = self._classify_compressor_with_stall()
+ assert c._last_summary_network_failure is False
+ assert c._last_summary_auth_failure is False
+
+ def _classify_compressor_with_stall(self):
+ c = self._classify_make()
+ with patch(
+ "agent.context_compressor.call_llm",
+ side_effect=TimeoutError(
+ "Codex auxiliary Responses stream stalled: no new output for 60.0s (67.7s elapsed)"),
+ ):
+ c._generate_summary(self._msgs())
+ return c
+
+ def _classify_make(self):
+ with patch("agent.context_compressor.get_model_context_length", return_value=100000):
+ return ContextCompressor(model="test", quiet_mode=True)
+
+ def _msgs(self):
+ return [
+ {"role": "system", "content": "sys"},
+ {"role": "user", "content": "msg 1"},
+ {"role": "assistant", "content": "msg 2"},
+ ]