Skip to content

The agent that grows with you

fix(gateway): record heartbeat ownership only when the CLI watchdog starts

unsupportedpastels67dfcbb61691
diff --git a/gateway/session_viewer_automation.py b/gateway/session_viewer_automation.py
index 385ec55bf14..cf55f910325 100644
--- a/gateway/session_viewer_automation.py
+++ b/gateway/session_viewer_automation.py
@@ -29,7 +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.
+# /heartbeat set and resume start the CLI's watchdog when they return a state (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)
@@ -152,11 +153,6 @@ 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 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
runner = authority.runner
home = Path(authority.profile_id)
@@ -170,6 +166,11 @@ async def automation(connection, ref, params):
before = rows() if call in _READS else None
# The judge and gates block for seconds: off the loop, with the caller's context (profile scope).
result = await asyncio.to_thread(run)
+ if kind == "heartbeat" and call in _WATCHDOG_CALLS and (call == "due_prompt" or result.get("value") is not None):
+ # 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
if before is None or rows() != before:
# The classic CLI drives this session's automation: other viewers (Ink, Desktop) repaint from the event.
from gateway.session_control_actions import publish
diff --git a/tests/gateway/test_local_chat_delivery.py b/tests/gateway/test_local_chat_delivery.py
index 46681752cba..781e82b4c49 100644
--- a/tests/gateway/test_local_chat_delivery.py
+++ b/tests/gateway/test_local_chat_delivery.py
@@ -120,6 +120,18 @@ async def test_a_classic_cli_without_a_heartbeat_watchdog_leaves_the_tick_to_the
assert _heartbeat_admissions(authority, ref)
+async def test_a_classic_cli_resume_with_no_heartbeat_leaves_the_tick_to_the_poller(tmp_path, monkeypatch):
+ """/heartbeat resume with nothing to resume starts no watchdog, so a heartbeat set later elsewhere still fires."""
+ from gateway.run_heartbeat_restore import restore_heartbeat_watches
+ runner, authority, ref, automation = await _attached_cli(tmp_path, monkeypatch)
+ assert (await automation('heartbeat', 'resume'))['value'] is None
+ _overdue_heartbeat(ref.session_id) # Desktop sets it
+ await restore_heartbeat_watches(runner)
+ await runner._heartbeat_poll_once(runner._heartbeat_watch)
+ assert _heartbeat_admissions(authority, ref)
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize('live_adapters', [False, True], ids=['no-local-adapter-yet', 'in-gateway-ticker'])
@pytest.mark.parametrize('viewer', [False, True], ids=['unviewed', 'viewed'])