Skip to content

The agent that grows with you

fix(gateway): offload hygiene streak persistence

fangliquanflqb3df990832cd
diff --git a/gateway/run.py b/gateway/run.py
index 037997d5a92..f81e364aad3 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -19118,12 +19118,15 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
)
_hyg_cleanup_deferred = True
if _hyg_failure_cooldown_seconds >= 0:
+ _hyg_cooldown = await asyncio.to_thread(
+ _hygiene_cooldown_for_failure,
+ self,
+ session_key,
+ _hyg_failure_cooldown_seconds,
+ )
_record_hygiene_cooldown(
self, session_entry.session_id,
- _hygiene_cooldown_for_failure(
- self, session_key,
- _hyg_failure_cooldown_seconds,
- ),
+ _hyg_cooldown,
"session hygiene compression "
"timed out with no output from "
"the summary model",
@@ -19355,17 +19358,22 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
approx_tokens=_approx_tokens,
new_tokens=_new_tokens,
):
- _reset_hygiene_failure_streak(
- self, session_key
+ await asyncio.to_thread(
+ _reset_hygiene_failure_streak,
+ self,
+ session_key,
)
if _hyg_aborted:
if _hyg_failure_cooldown_seconds >= 0:
+ _hyg_cooldown = await asyncio.to_thread(
+ _hygiene_cooldown_for_failure,
+ self,
+ session_key,
+ _hyg_failure_cooldown_seconds,
+ )
_record_hygiene_cooldown(
self, session_entry.session_id,
- _hygiene_cooldown_for_failure(
- self, session_key,
- _hyg_failure_cooldown_seconds,
- ),
+ _hyg_cooldown,
getattr(
_comp, "_last_summary_error", None
),
diff --git a/tests/gateway/test_session_hygiene.py b/tests/gateway/test_session_hygiene.py
index 9a3c8d80b73..12b532466a9 100644
--- a/tests/gateway/test_session_hygiene.py
+++ b/tests/gateway/test_session_hygiene.py
@@ -1092,11 +1092,26 @@ async def test_hygiene_compression_cooldown_survives_gateway_restart(
"""
from hermes_state import SessionDB
+ gateway_run = importlib.import_module("gateway.run")
session_id = "sess-restart"
db = SessionDB(db_path=tmp_path / "state.db")
try:
db.create_session(session_id, "telegram")
+ main_thread = threading.get_ident()
+ streak_threads = []
+ original_cooldown_for_failure = gateway_run._hygiene_cooldown_for_failure
+
+ def tracked_cooldown_for_failure(*args, **kwargs):
+ streak_threads.append(threading.get_ident())
+ return original_cooldown_for_failure(*args, **kwargs)
+
+ monkeypatch.setattr(
+ gateway_run,
+ "_hygiene_cooldown_for_failure",
+ tracked_cooldown_for_failure,
+ )
+
class AbortingCompressAgent:
instances = 0
@@ -1124,6 +1139,8 @@ async def test_hygiene_compression_cooldown_survives_gateway_restart(
)
assert await runner1._handle_message(event1) == "ok"
assert AbortingCompressAgent.instances == 1
+ assert len(streak_threads) == 1
+ assert streak_threads[0] != main_thread
# The abort must have persisted a cooldown to the DB.
state = db.get_compression_failure_cooldown(session_id)
@@ -1172,6 +1189,8 @@ async def test_hygiene_compression_cooldown_survives_gateway_restart(
)
assert await runner3._handle_message(event3) == "ok"
assert AbortingCompressAgent.instances == 2
+ assert len(streak_threads) == 2
+ assert all(thread_id != main_thread for thread_id in streak_threads)
escalated = db.get_compression_failure_cooldown(session_id)
assert escalated is not None
assert escalated["remaining_seconds"] == pytest.approx(900, abs=5)