Skip to content

The agent that grows with you

fix(gateway): record heartbeat ownership when a CLI starts its watchdog

unsupportedpastelseef229a617f0
diff --git a/gateway/session_viewer_automation.py b/gateway/session_viewer_automation.py
index 6e6c25cb91b..385ec55bf14 100644
--- a/gateway/session_viewer_automation.py
+++ b/gateway/session_viewer_automation.py
@@ -29,6 +29,8 @@ _CALLS = {
"heartbeat": frozenset({"state", "set", "pause", "resume", "clear", "due_prompt", "abandon_fire"}),
}
_READS = frozenset({"state", "is_waiting", "goal_blocks_tick"})
+# /heartbeat set and resume start the CLI's watchdog (cli_commands_mixin); only it calls due_prompt.
+_WATCHDOG_CALLS = frozenset({"set", "resume", "due_prompt"})
_FIELDS = frozenset({"session_id", "profile", "kind", "call", "args", "kwargs"})
_ERRORS = (RuntimeError, ValueError, IndexError)
@@ -150,8 +152,8 @@ async def automation(connection, ref, params):
if not hasattr(authority, "viewer_automation"):
authority.viewer_automation = {}
authority.viewer_automation[ref.session_id] = connection
- if kind == "heartbeat" and call == "due_prompt":
- # Only a running CLI watchdog polls due_prompt: from then on the gateway poller leaves the ticks to it.
+ if kind == "heartbeat" and call in _WATCHDOG_CALLS:
+ # The CLI's watchdog runs from these on: from then on the gateway poller leaves the ticks to it.
if not hasattr(authority, "viewer_heartbeat"):
authority.viewer_heartbeat = {}
authority.viewer_heartbeat[ref.session_id] = connection
diff --git a/tests/gateway/test_local_chat_delivery.py b/tests/gateway/test_local_chat_delivery.py
index 2d44c765003..46681752cba 100644
--- a/tests/gateway/test_local_chat_delivery.py
+++ b/tests/gateway/test_local_chat_delivery.py
@@ -87,9 +87,9 @@ async def _attached_cli(tmp_path, monkeypatch):
ref = create_local_session(authority, cli.actor, dict(request_id='cli', source='cli', model='fixture', toolsets=[]))
cli.subscriptions[ref.session_id] = 'sub-1'
- async def automation(kind, call):
+ async def automation(kind, call, *args):
reply = await cli.dispatch({'id': 1, 'method': 'session.automation', 'params': {
- 'session_id': ref.session_id, 'kind': kind, 'call': call, 'args': [], 'kwargs': {}}})
+ 'session_id': ref.session_id, 'kind': kind, 'call': call, 'args': list(args), 'kwargs': {}}})
return reply['result']
return runner, authority, ref, automation
@@ -98,12 +98,10 @@ async def _attached_cli(tmp_path, monkeypatch):
async def test_a_classic_cli_driving_its_heartbeat_keeps_the_tick(tmp_path, monkeypatch):
"""The classic CLI paints only the ticks its own watchdog fires, so the poller leaves them to it."""
from gateway.run_heartbeat_restore import restore_heartbeat_watches
- from hermes_cli.heartbeat import HeartbeatManager
runner, authority, ref, automation = await _attached_cli(tmp_path, monkeypatch)
- HeartbeatManager(ref.session_id).set('status tick', 60)
- await restore_heartbeat_watches(runner)
- assert (await automation('heartbeat', 'due_prompt'))['value'] is None # the CLI's watchdog polls, not due yet
+ await automation('heartbeat', 'set', 'status tick', 60) # /heartbeat set starts the CLI's watchdog
_overdue_heartbeat(ref.session_id)
+ await restore_heartbeat_watches(runner)
await runner._heartbeat_poll_once(runner._heartbeat_watch)
assert not _heartbeat_admissions(authority, ref)
assert 'status tick' in (await automation('heartbeat', 'due_prompt'))['value']