Skip to content

The agent that grows with you

fix(gateway): leave a classic CLI's heartbeat ticks to its own watchdog

unsupportedpastels19413ccd3efd
diff --git a/gateway/run_goals.py b/gateway/run_goals.py
index 18b42c4a437..58ad90ce33b 100644
--- a/gateway/run_goals.py
+++ b/gateway/run_goals.py
@@ -147,9 +147,13 @@ class GatewayGoalsMixin:
watch[quick_key] = (source, session_id)
adapter = self._delivery_adapter_for(source)
from gateway.session_local import LocalSessionAdapter
+ from gateway.session_viewer_automation import viewer_driven
+ local = isinstance(adapter, LocalSessionAdapter)
# A local chat has no message handler: its owner admits the tick (``admit_heartbeat``).
- if adapter is None or not (adapter._message_handler or isinstance(adapter, LocalSessionAdapter)):
+ if adapter is None or not (adapter._message_handler or local):
return
+ if local and viewer_driven(adapter.authority, source.chat_id):
+ 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
await admit_heartbeat(self, adapter, source, session_id, quick_key)
diff --git a/tests/gateway/test_local_chat_delivery.py b/tests/gateway/test_local_chat_delivery.py
index 8736ce3e034..7d4dd6c70ab 100644
--- a/tests/gateway/test_local_chat_delivery.py
+++ b/tests/gateway/test_local_chat_delivery.py
@@ -75,6 +75,33 @@ 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
+ from gateway.session_controls import AuthorityConnection
+ from gateway.session_local import create_local_session
+ from hermes_cli import goals
+ runner, authority, _, _ = await _local(tmp_path, monkeypatch)
+ # The RPC runs under the owner's profile scope, ``Path('default')`` here: keep it in tmp_path, on its db.
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(goals, '_get_session_db', lambda: authority.db)
+ 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):
+ reply = await cli.dispatch({'id': 1, 'method': 'session.automation', 'params': {
+ 'session_id': ref.session_id, 'kind': 'heartbeat', 'call': call, 'args': [], 'kwargs': {}}})
+ return reply['result']
+ await automation('state') # the attached CLI's heartbeat manager
+ await runner._heartbeat_poll_once(runner._heartbeat_watch)
+ assert not _heartbeat_admissions(authority, ref)
+ assert 'status tick' in (await automation('due_prompt'))['value']
+
+
@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'])