Skip to content

The agent that grows with you

fix(gateway): heartbeats respect hermes pause instead of spamming the paused notice

Dzianis Vashchukfaeb7eecc9d5
diff --git a/gateway/run_goals.py b/gateway/run_goals.py
index 4bb20245512..54d0e7863a1 100644
--- a/gateway/run_goals.py
+++ b/gateway/run_goals.py
@@ -148,6 +148,10 @@ class GatewayGoalsMixin:
or self._queue_depth(quick_key, adapter=adapter) > 0
):
return # keep missed intervals due until user work has drained
+ from agent.estop import check_paused
+
+ if check_paused("heartbeat", logger):
+ return # `hermes pause`: leave the tick unclaimed so it fires after `hermes resume`
from hermes_cli.heartbeat import HeartbeatManager
mgr = HeartbeatManager(session_id=session_id)
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index ce560d9044c..d5cbc84cf52 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -329,7 +329,9 @@ class GatewayInboundMixin:
self, event: "MessageEvent", source: SessionSource, is_internal: bool
) -> Optional[str]:
"""Global emergency-stop (`hermes pause`) notice when this turn must be blocked, else None.
- Placed after auth so unauthorized senders can't probe pause state."""
+ Placed after auth so unauthorized senders can't probe pause state. A synthetic heartbeat gets
+ "" (blocked, nothing sent): nobody typed it, and its tick stays due, so a notice would repeat
+ every poll."""
if is_internal:
return None
try:
@@ -339,6 +341,9 @@ class GatewayInboundMixin:
_paused_notice = _estop_paused_reply()
if _paused_notice is None or self._hm_estop_turn_allowed(event, source):
return None
+ if getattr(event, "_heartbeat_session_id", None):
+ logger.debug("Heartbeat turn dropped by global emergency stop")
+ return ""
logger.info(
"Gateway turn paused by global emergency stop (platform=%s chat=%s)",
getattr(getattr(source, "platform", None), "value", "unknown"),
diff --git a/tests/gateway/test_heartbeat_poller.py b/tests/gateway/test_heartbeat_poller.py
index f6e5b99f47d..9a7f5a8c2e1 100644
--- a/tests/gateway/test_heartbeat_poller.py
+++ b/tests/gateway/test_heartbeat_poller.py
@@ -117,3 +117,72 @@ async def test_unavailable_or_busy_session_leaves_persisted_tick_due(poller):
await asyncio.gather(*adapter._background_tasks)
assert HeartbeatManager("heartbeat-session").state.fire_count == 1
assert runner._queue_depth(key, adapter=adapter) == 0
+
+
+async def test_emergency_stop_holds_due_tick_without_messaging_until_resume(poller, monkeypatch):
+ """`hermes pause` must not turn a due heartbeat into a chat message on every poll (prod: ~10k
+ identical paused notices in 5h); the tick fires normally after `hermes resume`."""
+ from agent import estop
+
+ runner, adapter, watch, key, clock = poller
+ runner._is_user_authorized = lambda source: True
+ sent, received = [], []
+
+ async def send(chat_id, content, reply_to=None, metadata=None):
+ sent.append(content)
+ return SendResult(success=True, message_id="wire-1")
+
+ async def real_gateway(event):
+ received.append(event)
+ return await runner._handle_message(event)
+
+ monkeypatch.setattr(adapter, "send", send)
+ adapter.set_message_handler(real_gateway)
+ clock.now += 900
+ original = HeartbeatManager("heartbeat-session").state.to_json()
+ estop.engage(reason="maintenance")
+ for _ in range(3):
+ await runner._heartbeat_poll_once(watch)
+ await asyncio.gather(*adapter._background_tasks)
+ assert (received, sent) == ([], [])
+ assert HeartbeatManager("heartbeat-session").state.to_json() == original
+
+ async def agent_turn(event):
+ event._heartbeat_execution_started = True # fake agent execution boundary
+ received.append(event)
+ return None
+
+ estop.disengage()
+ adapter.set_message_handler(agent_turn)
+ await runner._heartbeat_poll_once(watch)
+ await asyncio.gather(*adapter._background_tasks)
+ assert len(received) == 1
+ assert HeartbeatManager("heartbeat-session").state.fire_count == 1
+
+
+async def test_emergency_stop_gate_drops_heartbeat_silently_but_tells_real_users(poller, monkeypatch):
+ from agent import estop
+
+ runner, adapter, watch, key, clock = poller
+ source = watch[key][0]
+ runner._is_user_authorized = lambda source: True
+ sent = []
+
+ async def send(chat_id, content, reply_to=None, metadata=None):
+ sent.append(content)
+ return SendResult(success=True, message_id="wire-1")
+
+ monkeypatch.setattr(adapter, "send", send)
+ adapter.set_message_handler(runner._handle_message)
+ estop.engage(reason="maintenance")
+ heartbeat = runner._synthetic_prompt_event(source, "check status")
+ heartbeat._heartbeat_session_id = "heartbeat-session"
+ await adapter.handle_message(heartbeat)
+ await asyncio.gather(*adapter._background_tasks)
+ assert sent == []
+
+ await adapter.handle_message(MessageEvent(text="are you there?", source=source))
+ await asyncio.gather(*adapter._background_tasks)
+ assert len(sent) == 1 and estop.paused_reply() in sent[0]