The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): preserve routed heartbeat ownership
KoNit-Kcf1e72706781
diff --git a/tests/tui_gateway/test_heartbeat_tui_tick.py b/tests/tui_gateway/test_heartbeat_tui_tick.pyindex 5052330d210..2edf0e938d9 100644--- a/tests/tui_gateway/test_heartbeat_tui_tick.py+++ b/tests/tui_gateway/test_heartbeat_tui_tick.py@@ -65,6 +65,8 @@ def test_notification_poller_fires_due_heartbeat_when_idle(server, session):"""The session-owner poller loop itself dispatches a due heartbeat exactly once; the same state onthe base loop never fired (armed-but-dead)."""sid, key, s = session+ s["source"] = "desktop"+ server._get_db().create_session(key, source="desktop")_arm_due(key)dispatched: list[str] = []@@ -90,6 +92,24 @@ def test_notification_poller_fires_due_heartbeat_when_idle(server, session):assert load_heartbeat(key).fire_count == 1 and not load_heartbeat(key).is_due()+def test_desktop_poller_leaves_gateway_owned_heartbeat_for_gateway(server, session):+ """A Desktop viewer must not consume a messaging session's routed heartbeat."""+ sid, key, s = session+ s["source"] = "desktop"+ server._get_db().create_session(key, source="telegram")+ _arm_due(key)++ p_submit, p_emit = _submits(server, MagicMock())+ with p_submit as submit, p_emit:+ server._maybe_fire_tui_heartbeat_tick(sid, s)++ from hermes_cli.heartbeat import load_heartbeat++ submit.assert_not_called()+ assert s["running"] is False+ assert load_heartbeat(key).fire_count == 0 and load_heartbeat(key).is_due()++@pytest.mark.parametrize("running,due", [(True, True), (False, False)])def test_heartbeat_tick_defers_when_busy_or_not_due(server, session, running, due):sid, key, s = sessiondiff --git a/tui_gateway/session_notifications.py b/tui_gateway/session_notifications.pyindex e138159f23b..00e58f7ed7d 100644--- a/tui_gateway/session_notifications.py+++ b/tui_gateway/session_notifications.py@@ -195,6 +195,22 @@ def _notif_slash_loop_tick(rid: str, sid: str, session: dict, mgr, wakeup: str)_notif_loop_status(sid, decision["message"])+def _notif_gateway_owns_heartbeat(session: dict, session_key: str) -> bool:+ """Whether the persisted conversation route owns heartbeat delivery.++ Desktop/TUI can attach to a messaging conversation, but its session-owner+ poller has no adapter route for the reply. The gateway's heartbeat poller+ retains that route, so leave its due tick untouched. A missing row is+ fail-open for a newly-created local session.+ """+ try:+ with _session_db(session) as db:+ row = db.get_session(session_key) if db is not None else None+ return bool(row and _is_gateway_owned_source(str(row.get("source") or "")))+ except Exception:+ return False++def _maybe_fire_tui_heartbeat_tick(sid: str, session: dict) -> None:"""Fire a due /heartbeat prompt for an idle TUI/Desktop/dashboard session (#102056, #103044).@@ -211,6 +227,8 @@ def _maybe_fire_tui_heartbeat_tick(sid: str, session: dict) -> None:returnif not (sid_key := session.get("session_key") or ""):return+ if _notif_gateway_owns_heartbeat(session, sid_key):+ return # the gateway poller owns the persisted messaging routemgr = HeartbeatManager(session_id=sid_key)if not mgr.is_active() or not mgr.state.is_due() or not _notif_claim_turn(session):return # not due, or busy — the tick coalesces to the next idle poll