Skip to content

The agent that grows with you

fix(gateway): suppress the heartbeat poller only for a CLI whose watchdog runs

unsupportedpastelsbe6d37854493
diff --git a/gateway/run_goals.py b/gateway/run_goals.py
index 58ad90ce33b..42f6d3a4f81 100644
--- a/gateway/run_goals.py
+++ b/gateway/run_goals.py
@@ -152,7 +152,7 @@ class GatewayGoalsMixin:
# A local chat has no message handler: its owner admits the tick (``admit_heartbeat``).
if adapter is None or not (adapter._message_handler or local):
return
- if local and viewer_driven(adapter.authority, source.chat_id):
+ if local and viewer_driven(adapter.authority, source.chat_id, "viewer_heartbeat"):
return # the classic CLI's watchdog fires it: that CLI paints only the turns it submits
if getattr(self, 'session_authority', None) is not None:
from gateway.run_heartbeat_acceptance import admit_heartbeat
diff --git a/gateway/session_viewer_automation.py b/gateway/session_viewer_automation.py
index d998b5e8d17..6e6c25cb91b 100644
--- a/gateway/session_viewer_automation.py
+++ b/gateway/session_viewer_automation.py
@@ -33,11 +33,11 @@ _FIELDS = frozenset({"session_id", "profile", "kind", "call", "args", "kwargs"})
_ERRORS = (RuntimeError, ValueError, IndexError)
-def viewer_driven(authority, session_id) -> bool:
- """Whether a local viewer drives this session's goal/loop judging (see the module doc): only while
- that viewer's connection still holds its subscription, so a TUI or Desktop attached after it left
- gets the gateway's own post-turn hooks again."""
- connection = getattr(authority, "viewer_automation", {}).get(session_id) if authority is not None else None
+def viewer_driven(authority, session_id, registry="viewer_automation") -> bool:
+ """Whether a local viewer drives this session's goal/loop judging (see the module doc), or with
+ ``viewer_heartbeat`` its heartbeat ticks: only while that viewer's connection still holds its
+ subscription, so a TUI or Desktop attached after it left gets the gateway's own drivers again."""
+ connection = getattr(authority, registry, {}).get(session_id) if authority is not None else None
return bool(connection is not None and connection.subscriptions.get(session_id))
@@ -150,6 +150,11 @@ 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 not hasattr(authority, "viewer_heartbeat"):
+ authority.viewer_heartbeat = {}
+ authority.viewer_heartbeat[ref.session_id] = connection
runner = authority.runner
home = Path(authority.profile_id)
diff --git a/tests/gateway/test_local_chat_delivery.py b/tests/gateway/test_local_chat_delivery.py
index 7d4dd6c70ab..2d44c765003 100644
--- a/tests/gateway/test_local_chat_delivery.py
+++ b/tests/gateway/test_local_chat_delivery.py
@@ -75,10 +75,7 @@ async def test_overdue_heartbeat_fires_once_with_a_classic_cli_also_polling(tmp_
assert len(_heartbeat_admissions(authority, ref)) + bool(cli['value']) == 1
-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
+async def _attached_cli(tmp_path, monkeypatch):
from gateway.session_controls import AuthorityConnection
from gateway.session_local import create_local_session
from hermes_cli import goals
@@ -89,17 +86,40 @@ async def test_a_classic_cli_driving_its_heartbeat_keeps_the_tick(tmp_path, monk
cli = AuthorityConnection(authority, object(), {'user_id': 'human', 'profile_id': 'default'})
ref = create_local_session(authority, cli.actor, dict(request_id='cli', source='cli', model='fixture', toolsets=[]))
cli.subscriptions[ref.session_id] = 'sub-1'
- _overdue_heartbeat(ref.session_id)
- await restore_heartbeat_watches(runner)
- async def automation(call):
+ async def automation(kind, call):
reply = await cli.dispatch({'id': 1, 'method': 'session.automation', 'params': {
- 'session_id': ref.session_id, 'kind': 'heartbeat', 'call': call, 'args': [], 'kwargs': {}}})
+ 'session_id': ref.session_id, 'kind': kind, 'call': call, 'args': [], 'kwargs': {}}})
return reply['result']
- await automation('state') # the attached CLI's heartbeat manager
+ return runner, authority, ref, automation
+
+
+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
+ _overdue_heartbeat(ref.session_id)
await runner._heartbeat_poll_once(runner._heartbeat_watch)
assert not _heartbeat_admissions(authority, ref)
- assert 'status tick' in (await automation('due_prompt'))['value']
+ assert 'status tick' in (await automation('heartbeat', 'due_prompt'))['value']
+
+
+async def test_a_classic_cli_without_a_heartbeat_watchdog_leaves_the_tick_to_the_poller(tmp_path, monkeypatch):
+ """A CLI reopening a chat runs goal/loop reads but starts its watchdog only on /heartbeat set or resume."""
+ from gateway.run_heartbeat_restore import restore_heartbeat_watches
+ runner, authority, ref, automation = await _attached_cli(tmp_path, monkeypatch)
+ _overdue_heartbeat(ref.session_id)
+ await restore_heartbeat_watches(runner)
+ await automation('goal', 'state')
+ await automation('loop', 'state')
+ await runner._heartbeat_poll_once(runner._heartbeat_watch)
+ assert _heartbeat_admissions(authority, ref)
@pytest.mark.asyncio