Skip to content

The agent that grows with you

fix(cli): refresh the heartbeat proxy on every watchdog poll

unsupportedpastelsc76d50981709
diff --git a/hermes_cli/cli_loops_mixin.py b/hermes_cli/cli_loops_mixin.py
index 8041a09c750..3f4cf2b22d2 100644
--- a/hermes_cli/cli_loops_mixin.py
+++ b/hermes_cli/cli_loops_mixin.py
@@ -658,6 +658,7 @@ class CLILoopsMixin:
if getattr(self, "_heartbeat_watchdog_started", False):
return
self._heartbeat_watchdog_started = True
+ from hermes_cli.gateway_automation import GatewayHeartbeatManager
from hermes_cli.heartbeat import POLL_SECONDS
def _loop():
@@ -666,6 +667,10 @@ class CLILoopsMixin:
time.sleep(POLL_SECONDS)
try:
mgr = self._get_heartbeat_manager()
+ if isinstance(mgr, GatewayHeartbeatManager):
+ # The gateway poller stands down while this watchdog runs, so a resume,
+ # set or re-anchor from another viewer must reach this copy.
+ mgr.refresh()
if mgr is None or not mgr.is_active():
continue
busy = (
diff --git a/hermes_cli/gateway_automation.py b/hermes_cli/gateway_automation.py
index 49e561c2f36..435ce3e115b 100644
--- a/hermes_cli/gateway_automation.py
+++ b/hermes_cli/gateway_automation.py
@@ -247,7 +247,7 @@ class GatewayHeartbeatManager(_Remote):
def due_prompt(self, now=None):
s = self._state
if s is None or not s.is_due(now):
- return None # main's cheap local check first: the idle poll costs no round trip
+ return None # main's cheap local check first (the watchdog refreshes the state each poll)
return self._rpc("due_prompt")
def abandon_fire(self):
diff --git a/tests/hermes_cli/test_gateway_automation.py b/tests/hermes_cli/test_gateway_automation.py
index 5103bfd048a..9d4253b94e3 100644
--- a/tests/hermes_cli/test_gateway_automation.py
+++ b/tests/hermes_cli/test_gateway_automation.py
@@ -81,6 +81,32 @@ def test_idle_polls_cost_no_round_trip(cli):
assert loop.complete_tick('done')['message'] == '✓ Loop finished — ran 1/1 times.'
+def test_the_watchdog_fires_a_heartbeat_another_viewer_resumed(cli, monkeypatch):
+ """The poller stands down for a CLI running its watchdog, so the watchdog must see a resume made elsewhere."""
+ import queue
+ import time
+ from hermes_cli import cli_loops_mixin
+ from hermes_cli.gateway_automation import GatewayHeartbeatManager
+ from hermes_cli.heartbeat import load_heartbeat, save_heartbeat
+ hb = cli.cli._get_heartbeat_manager()
+ hb.set('pulse', 60)
+ hb.pause() # /heartbeat pause in this CLI
+ GatewayHeartbeatManager('sid-1', cli.conn).resume() # Desktop resumes it
+ state = load_heartbeat('sid-1')
+ state.last_fired_at -= 120 # and an interval passes
+ save_heartbeat('sid-1', state)
+
+ cli.cli._pending_input, cli.cli._agent_running = queue.Queue(), False
+ # One poll: the sleep before the tick asks the loop to exit after it.
+ monkeypatch.setattr(cli_loops_mixin, 'time', SimpleNamespace(
+ sleep=lambda _s: setattr(cli.cli, '_should_exit', True), time=time.time))
+ cli.cli._start_heartbeat_watchdog()
+ deadline = time.monotonic() + 5
+ while cli.cli._heartbeat_watchdog_started and time.monotonic() < deadline:
+ time.sleep(0.01)
+ assert [p for p in cli.cli._pending_input.queue if 'pulse' in p]
+
+
def test_goal_draft_drafts_on_the_gateway(cli, monkeypatch):
from hermes_cli import goals
from hermes_cli.gateway_automation import dispatch_goal_command