From 8e9702d2278f358d5500e00d0004faaba5ff78d5 Mon Sep 17 00:00:00 2001 From: Shakti Prasad Mohapatra Date: Wed, 29 Jul 2026 22:46:09 +0530 Subject: [PATCH] fix: persist session hygiene compression cooldown to state DB The session hygiene compression path tracked its per-session failure cooldown in an in-memory dict (_hygiene_compression_failure_cooldowns). When the gateway restarted, the dict was gone, so the next message re-triggered the same failing compression, wedging session storage. The state DB already has a persistent column (sessions.compression_failure_cooldown_until) and full read/write/clear methods (record_/get_/clear_compression_failure_cooldown in hermes_state.py) used by the in-conversation compression path (context_compressor.py) but not by the session hygiene path in gateway/run.py. Fix: replace the in-memory dict with calls to the persistent DB methods: - Cooldown check: use get_compression_failure_cooldown instead of dict lookup - Timeout failure: use record_compression_failure_cooldown instead of dict write - Abort failure: use record_compression_failure_cooldown instead of dict write After a restart, a session whose compression is in cooldown is now skipped for the cooldown's remaining duration rather than re-attempted immediately. Fixes #74136 --- gateway/run.py | 72 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/gateway/run.py b/gateway/run.py index 3a890176c0..43853f9b66 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -115,6 +115,28 @@ _TELEGRAM_NOISY_STATUS_RE = re.compile( ) +def _record_hygiene_cooldown(gateway, session_id: str, cooldown_seconds: float) -> None: + """Persist a session-hygiene compression-failure cooldown to the state DB. + + Uses the same ``compression_failure_cooldown_until`` column and + ``record_compression_failure_cooldown`` method that the in-conversation + compression path (``agent/context_compressor.py``) already uses, so the + cooldown survives gateway restarts (#74136). + """ + import time as _time + session_db = getattr(gateway, "_session_db", None) + if session_db is None: + return + session_db = getattr(session_db, "_db", session_db) + recorder = getattr(session_db, "record_compression_failure_cooldown", None) + if recorder is None: + return + try: + recorder(session_id, _time.time() + cooldown_seconds) + except Exception as exc: + logger.debug("session hygiene cooldown persist failed: %s", exc) + + def _status_template_to_regex(template: str) -> str: """Compile a compression status template constant into a regex source. @@ -16049,20 +16071,28 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew ) if _needs_compress: - _cooldowns = getattr(self, "_hygiene_compression_failure_cooldowns", None) - if _cooldowns is None: - _cooldowns = {} - self._hygiene_compression_failure_cooldowns = _cooldowns - _cooldown_key = session_entry.session_id - _cooldown_until = float(_cooldowns.get(_cooldown_key) or 0.0) - if _cooldown_until > time.time(): - logger.info( - "Session hygiene: skipping compression for %s; " - "previous failure cooldown active for %.1fs", - _cooldown_key, - max(0.0, _cooldown_until - time.time()), - ) - _needs_compress = False + # Use the persistent DB-backed cooldown (same as the + # in-conversation compression path in context_compressor.py) + # so the cooldown survives gateway restarts. The in-memory + # dict was reset on every restart, re-triggering the same + # failing compression and wedging session storage (#74136). + _session_db = getattr(self, "_session_db", None) + if _session_db is not None: + _session_db = getattr(_session_db, "_db", _session_db) + _getter = getattr(_session_db, "get_compression_failure_cooldown", None) + if _getter is not None: + try: + _cooldown_state = _getter(session_entry.session_id) + except Exception: + _cooldown_state = None + if _cooldown_state and _cooldown_state.get("remaining_seconds", 0) > 0: + logger.info( + "Session hygiene: skipping compression for %s; " + "previous failure cooldown active for %.1fs", + session_entry.session_id, + _cooldown_state["remaining_seconds"], + ) + _needs_compress = False if _needs_compress: logger.info( @@ -16233,9 +16263,10 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew ) _hyg_cleanup_deferred = True if _hyg_failure_cooldown_seconds >= 0: - self._hygiene_compression_failure_cooldowns[ - session_entry.session_id - ] = time.time() + _hyg_failure_cooldown_seconds + _record_hygiene_cooldown( + self, session_entry.session_id, + _hyg_failure_cooldown_seconds, + ) logger.warning( "Session hygiene compression for session %s " "made no progress for %.1fs " @@ -16399,9 +16430,10 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew _comp = getattr(_hyg_agent, "context_compressor", None) if _comp is not None and getattr(_comp, "_last_compress_aborted", False): if _hyg_failure_cooldown_seconds >= 0: - self._hygiene_compression_failure_cooldowns[ - session_entry.session_id - ] = time.time() + _hyg_failure_cooldown_seconds + _record_hygiene_cooldown( + self, session_entry.session_id, + _hyg_failure_cooldown_seconds, + ) _err = getattr(_comp, "_last_summary_error", None) or "unknown error" # Force-redact: provider exception text # may contain credentials; this message