Skip to content

The agent that grows with you

fix(heartbeat): a stale manager loses a tick cleared or paused since

unsupportedpastels8177b536dd51
diff --git a/hermes_cli/heartbeat.py b/hermes_cli/heartbeat.py
index 2e5548bf969..851d17c2bd9 100644
--- a/hermes_cli/heartbeat.py
+++ b/hermes_cli/heartbeat.py
@@ -227,9 +227,10 @@ class HeartbeatManager:
if s is None or not s.is_due(now):
return None
# Compare-and-set on the stored row: the gateway poller and a viewer's due_prompt RPC each hold
- # their own manager over it, and only one may take the tick.
+ # their own manager over it, and only one may take the tick. A row cleared or paused since is lost too.
stored = load_heartbeat(self.session_id)
- if stored is not None and (stored.last_fired_at, stored.fire_count) != (s.last_fired_at, s.fire_count):
+ if stored is None or stored.status != "active" or (
+ stored.last_fired_at, stored.fire_count) != (s.last_fired_at, s.fire_count):
self._state = stored
return None
self._last_claim = (s.last_fired_at, s.fire_count)
diff --git a/tests/hermes_cli/test_heartbeat.py b/tests/hermes_cli/test_heartbeat.py
index 59a306001cc..56720a9dfff 100644
--- a/tests/hermes_cli/test_heartbeat.py
+++ b/tests/hermes_cli/test_heartbeat.py
@@ -146,6 +146,16 @@ def test_due_prompt_fires_once_and_reanchors():
assert mgr.due_prompt() is None
[email protected]("stop,stored", [("clear", None), ("pause", "paused")])
+def test_a_stale_manager_does_not_fire_a_heartbeat_stopped_since(stop, stored):
+ mgr = HeartbeatManager(session_id=f"hb-stale-{stop}")
+ mgr.set("tick", 600).created_at = time.time() - 700 # due in this manager's snapshot
+ getattr(HeartbeatManager(session_id=f"hb-stale-{stop}"), stop)() # another driver stops it
+ assert mgr.due_prompt() is None
+ state = load_heartbeat(f"hb-stale-{stop}")
+ assert (state.status if state else None) == stored
+
+
def test_missed_ticks_coalesce():
mgr = HeartbeatManager(session_id="hb-coalesce-sid")
mgr.set("tick", 600)