Skip to content

The agent that grows with you

fix(gateway): stalled-session spool replay probe no longer warns on every append

teknium1f2755aee20d7
diff --git a/gateway/session_transcript.py b/gateway/session_transcript.py
index d666b16ee59..ccc1e76b94b 100644
--- a/gateway/session_transcript.py
+++ b/gateway/session_transcript.py
@@ -349,8 +349,13 @@ class SessionTranscriptMixin:
return
try:
from gateway.shutdown_flush import drain_transcript_spool
+ # Inside an outage the append that follows logs/escalates the same failure; the
+ # replay attempt is only the order-preserving probe, so its failure stays at DEBUG.
+ with self._transcript_retry_lock:
+ known_failing = bool(self._transcript_append_failures.get(session_id))
_replayed, remaining = drain_transcript_spool(
session_id, lambda message: self._append_transcript_message(session_id, message),
+ db_known_failing=known_failing,
)
if not remaining:
spooled_sessions.discard(session_id)
diff --git a/gateway/shutdown_flush.py b/gateway/shutdown_flush.py
index ee2fc742737..405ac2c3696 100644
--- a/gateway/shutdown_flush.py
+++ b/gateway/shutdown_flush.py
@@ -132,11 +132,13 @@ def spool_dropped_transcript_message(session_id: str, message: Dict[str, Any]) -
return None
-def drain_transcript_spool(session_id: str, replay) -> tuple[int, int]:
+def drain_transcript_spool(session_id: str, replay, *, db_known_failing: bool = False) -> tuple[int, int]:
"""Replay cap-dropped transcript messages spooled for *session_id*; return ``(replayed,
remaining)``. ``replay(message_dict)`` runs per message in drop order; a spool file is deleted
only after its replay succeeds. The first failure stops the drain (the DB is likely still
- unhealthy) and keeps the rest for retry.
+ unhealthy) and keeps the rest for retry. With ``db_known_failing`` (the caller's last write
+ already failed and is being logged/escalated) a replay failure is expected and logs at DEBUG,
+ so a stalled session does not add one WARNING per append on top of its ERROR (#114266).
"""
try:
candidates = list(_get_flush_dir().glob("pending-*.json"))
@@ -166,8 +168,9 @@ def drain_transcript_spool(session_id: str, replay) -> tuple[int, int]:
try:
replay(message)
except Exception as exc:
- logger.warning("Replay of spooled transcript message %s for %s failed; "
- "keeping spool file for retry: %s", path, session_id, exc)
+ (logger.debug if db_known_failing else logger.warning)(
+ "Replay of spooled transcript message %s for %s failed; "
+ "keeping spool file for retry: %s", path, session_id, exc)
remaining = len(ordered) - idx
break
path.unlink(missing_ok=True)
diff --git a/tests/gateway/test_session.py b/tests/gateway/test_session.py
index f8c787ed726..824a830eb44 100644
--- a/tests/gateway/test_session.py
+++ b/tests/gateway/test_session.py
@@ -1674,6 +1674,42 @@ class TestGatewaySessionDbRecovery:
assert rows == [f"m{i}" for i in range(threshold)] + ["recovered"] # replayed in order
assert list((tmp_path / "pending_messages").glob("pending-*.json")) == []
+ def test_stalled_session_spool_replay_does_not_warn_per_append(self, caplog, tmp_path, monkeypatch):
+ """Live finding on #114266: once a stalled session's backlog is spooled, the pre-write
+ replay probe on a still-dead DB must not add a shutdown_flush WARNING per append — the
+ per-append ERROR escalation already covers the outage. Recovery still replays in order."""
+ import threading
+ from types import SimpleNamespace
+ import hermes_constants
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: tmp_path)
+
+ def _fail(**kwargs):
+ raise RuntimeError("disk I/O error")
+
+ store = object.__new__(SessionStore)
+ store._db = SimpleNamespace(append_message=_fail)
+ store._transcript_retry_lock = threading.Lock()
+ store._dirty_transcripts = {}
+ store._transcript_append_failures = {}
+ store._fts_rebuild_last_attempt_at = time.monotonic()
+ threshold = store._TRANSCRIPT_APPEND_FAILURE_ESCALATION_THRESHOLD
+ n_appends = threshold + 3
+ with caplog.at_level(logging.DEBUG):
+ for i in range(n_appends):
+ store.append_to_transcript("s-stall", {"role": "user", "content": f"m{i}"})
+ errors = [r for r in caplog.records if "session is stalled" in r.getMessage()]
+ assert len(errors) == n_appends - threshold + 1 and {r.levelno for r in errors} == {logging.ERROR}
+ replay_failed = [r for r in caplog.records if "Replay of spooled transcript message" in r.getMessage()]
+ assert replay_failed, "spool replay was attempted before each write"
+ assert [r.levelno for r in replay_failed if r.levelno >= logging.WARNING] == []
+
+ rows = []
+ store._db = SimpleNamespace(append_message=lambda **kw: rows.append(kw["content"]))
+ store.append_to_transcript("s-stall", {"role": "assistant", "content": "recovered"})
+ assert rows == [f"m{i}" for i in range(n_appends)] + ["recovered"]
+
def test_pending_queue_caps_at_max(self):
"""Pending queue should drop oldest messages when exceeding the cap
to prevent unbounded memory growth on persistent DB failure."""