Skip to content

The agent that grows with you

fix(gateway): wire the desktop window bridges for local-viewer turns

Zek-Takai73b9f2af7b87
diff --git a/gateway/local_desktop_ui.py b/gateway/local_desktop_ui.py
new file mode 100644
index 00000000000..f4b36566606
--- /dev/null
+++ b/gateway/local_desktop_ui.py
@@ -0,0 +1,260 @@
+"""The desktop window bridges for a local viewer's turn on the session authority.
+
+Main's TUI sidecar wired two things per session (``tui_gateway/session_notifications.py::
+_wire_desktop_sinks`` and ``tui_gateway/agent_callbacks.py::_agent_cbs``):
+
+* the ``tools.desktop_ui`` emitter, so ``desktop_preview`` open/close, ``focus_pane``,
+ ``close_terminal`` and the other window events reach the renderer as ``event`` frames;
+* the GUI callbacks (``read_preview_callback``, ``drive_preview_callback``,
+ ``read_terminal_callback``, ``read_window_below_callback``, ``theme_callback``, ``tour_callback``,
+ ``setup_beat_callback``), answered by the renderer through ``preview.read`` / ``preview.act`` /
+ ``terminal.read`` / ``window.read`` / ``theme.apply`` / ``theme.pick`` / ``tour`` /
+ ``setup_beat`` server requests;
+* the process-registry sinks (``on_output`` / ``on_close``) that stream a background process into
+ its read-only terminal tab (``agent.terminal.output``) and let ``close_terminal`` drop the tab
+ (``terminal.close``).
+
+On the session authority none was installed, so every ``desktop_ui`` tool the session policy
+still offers a GUI session failed with "only available in the Hermes desktop app". Here the
+emitter and the process sinks are installed once per process; the emitter routes each event to the
+turn that raised it (by the turn's bound ``HERMES_SESSION_ID``) through the generation-fenced
+execution stream, and the process sinks route by the process's ``session_key`` to the owning
+session's event stream (a background process outlives the turn that started it). The callbacks
+are installed per turn on the cached agent and restored with the other turn sinks.
+
+Not bridged here: ``connection_callback`` (the manage_connections card). Its card answers through
+the ``connection.respond`` RPC, which the session authority does not serve yet, so installing the
+callback would show a card whose buttons go nowhere; the tool keeps its off-desktop result.
+"""
+from __future__ import annotations
+
+import json
+import logging
+import threading
+import time
+
+logger = logging.getLogger(__name__)
+
+# Agent attributes ``wire`` installs for one local-viewer turn (restored by ``local_viewer_events``).
+CALLBACK_ATTRS = ("read_terminal_callback", "read_preview_callback", "drive_preview_callback",
+ "read_window_below_callback", "theme_callback", "tour_callback", "setup_beat_callback")
+
+# main: tui_gateway/agent_callbacks.py ``_read_block`` budgets and tui_gateway/server.py's preview.act ladder.
+_TERMINAL_READ_TIMEOUT_S = 30
+_PREVIEW_READ_TIMEOUT_S = 45
+_WINDOW_READ_TIMEOUT_S = 30
+_PREVIEW_ACTION_TIMEOUT_S = 45
+_PREVIEW_ACTION_PROBE_TIMEOUT_S = 10
+_PREVIEW_ACTION_REPROBE_COOLDOWN_S = 30
+_PREVIEW_ACTION_BRIDGE_UNAVAILABLE = json.dumps({
+ "success": False,
+ "error": ("No Hermes Desktop window answered the preview action request. The drive_preview / "
+ "annotate_preview bridge is served by the desktop app's renderer, which updates "
+ "separately from this backend, so an app build older than the tool has nothing "
+ "listening. Update the Hermes Desktop app, open a page with open_preview, and try "
+ "again in this session after a short cooldown.")})
+
+# main: tui_gateway/theme_bridge.py and tui_gateway/server.py's tour ladder.
+_THEME_TIMEOUT_S = 15
+_THEME_PICK_TIMEOUT_S = 300
+_THEME_UNANSWERED = {"success": False, "error": "No Hermes Desktop window answered the theme request."}
+_TOUR_TIMEOUT_S = 45
+_TOUR_PROBE_TIMEOUT_S = 10
+_TOUR_BRIDGE_UNAVAILABLE = json.dumps({
+ "success": False,
+ "error": ("No Hermes Desktop window answered the tour request. The tour is driven by the desktop app's "
+ "renderer, which updates separately from this backend, so an app build older than the tour tool "
+ "has nothing listening. Update the Hermes Desktop app and start a new session. Do not retry tour "
+ "in this session.")})
+
+_lock = threading.Lock()
+# turn key (the turn's bound HERMES_SESSION_ID) -> publish(event_type, payload) of the live turn
+_turns: dict[str, object] = {}
+# canonical session id -> {"state": "answered"|"unanswered", "retry_at": monotonic}
+_preview_bridge: dict[str, dict] = {}
+# canonical session id -> "answered" | "unanswered" (main: the session record's ``tour_bridge``)
+_tour_bridge: dict[str, str] = {}
+# process ``session_key`` -> (authority, canonical session id) of the session that last ran a turn with it
+_process_routes: dict[str, tuple] = {}
+_emitter_installed = False
+_process_sinks_installed = False
+
+
+def _emit(sid, event, payload) -> None:
+ """``desktop_ui`` sink: publish *event* on the stream of the turn that raised it."""
+ from gateway.session_context import get_session_env
+ keys = [str(sid or ""), get_session_env("HERMES_SESSION_ID", "")]
+ with _lock:
+ publish = next((_turns[key] for key in keys if key and key in _turns), None)
+ if publish is None:
+ # Raised outside a local-viewer turn (a messaging route, a background thread): no window.
+ raise RuntimeError("no Hermes desktop window is attached to this turn")
+ publish(event, dict(payload or {}))
+
+
+def _install_emitter() -> None:
+ global _emitter_installed
+ if _emitter_installed:
+ return
+ from tools import desktop_ui
+ desktop_ui.set_emitter(_emit)
+ _emitter_installed = True
+
+
+def _publish_process(session, event_type, payload) -> None:
+ """Publish a process event on the stream of the session that owns the process."""
+ key = str(getattr(session, "session_key", "") or "") if session is not None else ""
+ with _lock:
+ route = _process_routes.get(key) if key else None
+ if route is None:
+ raise RuntimeError("no Hermes desktop window owns this process")
+ authority, session_id = route
+ live = authority.sessions.get(session_id)
+ if live is None:
+ raise RuntimeError("the session that owns this process is no longer live")
+ live.event_stream.publish(session_id, payload, event_type=event_type)
+
+
+def _install_process_sinks() -> None:
+ """main's ``_wire_desktop_sinks``: live output and tab close for background processes (once)."""
+ global _process_sinks_installed
+ if _process_sinks_installed:
+ return
+ from tools.process_registry import process_registry
+ if getattr(process_registry, "on_output", None) is None:
+ process_registry.on_output = lambda session, chunk: _publish_process(
+ session, "agent.terminal.output", {"process_id": session.id, "chunk": chunk})
+ if getattr(process_registry, "on_close", None) is None:
+ process_registry.on_close = lambda session, pid: _publish_process(
+ session, "terminal.close", {"process_id": pid})
+ _process_sinks_installed = True
+
+
+def _ask_result(turn, agent, method, params, timeout):
+ """One structured server request; the viewer's ``result`` dict, or None when none answered."""
+ from gateway.session_sudo_prompt import ask_viewer_result
+ authority, session_id, _generation = turn._approval_owner
+ live = authority.sessions.get(session_id)
+ if live is None:
+ return None
+ return ask_viewer_result(live.event_stream, session_id, method, dict(params), timeout=timeout,
+ cancelled=lambda: bool(getattr(agent, "_interrupt_requested", False)))
+
+
+def _theme(turn, agent, payload) -> dict:
+ """main's ``theme_request``: ``theme.apply`` for values, or a ``theme.pick`` card that waits."""
+ if "picker" not in payload:
+ answer = _ask(turn, agent, "theme.apply", payload, _THEME_TIMEOUT_S)
+ if not answer:
+ return dict(_THEME_UNANSWERED)
+ try:
+ return json.loads(answer)
+ except ValueError:
+ return {"success": True, "text": answer}
+ started = time.monotonic()
+ result = _ask_result(turn, agent, "theme.pick", payload, _THEME_PICK_TIMEOUT_S)
+ if result is None:
+ return {"timed_out": True} if time.monotonic() - started >= _THEME_PICK_TIMEOUT_S else {"cancelled": True}
+ if isinstance(result.get("picked"), dict):
+ return {"success": True, "picked": result["picked"]}
+ if isinstance(result.get("said"), str) and result["said"].strip():
+ return {"said": result["said"].strip()}
+ return {"cancelled": True}
+
+
+def _tour(turn, agent, payload) -> str:
+ """main's ``_tour_request``: probe once per session, then the full deadline or fail fast."""
+ session_id = turn._approval_owner[1]
+ with _lock:
+ state = _tour_bridge.get(session_id)
+ if state == "unanswered":
+ return _TOUR_BRIDGE_UNAVAILABLE
+ answer = _ask(turn, agent, "tour", payload, _TOUR_TIMEOUT_S if state == "answered" else _TOUR_PROBE_TIMEOUT_S)
+ with _lock:
+ if answer:
+ _tour_bridge[session_id] = "answered"
+ elif state != "answered" and not getattr(agent, "_interrupt_requested", False):
+ _tour_bridge[session_id] = "unanswered"
+ return answer or _TOUR_BRIDGE_UNAVAILABLE
+
+
+def _setup_beat(turn, agent, payload):
+ from agent.onboarding_flow import CARD_TIMEOUT_S
+ return _ask_result(turn, agent, "setup_beat", payload, CARD_TIMEOUT_S)
+
+
+def _ask(turn, agent, method, params, timeout):
+ """One ``ValueResult`` server request to the session's attached viewers; "" when none answered."""
+ from gateway.session_sudo_prompt import ask_viewer
+ authority, session_id, _generation = turn._approval_owner
+ live = authority.sessions.get(session_id)
+ if live is None:
+ return ""
+ value = ask_viewer(live.event_stream, session_id, method, dict(params), timeout=timeout,
+ cancelled=lambda: bool(getattr(agent, "_interrupt_requested", False)))
+ return value if isinstance(value, str) else json.dumps(value, ensure_ascii=False)
+
+
+def _read_block(turn, agent, method, timeout):
+ return lambda start=None, count=None: _ask(
+ turn, agent, method, {k: v for k, v in (("start", start), ("count", count)) if v is not None}, timeout)
+
+
+def _preview_action(turn, agent, payload) -> str:
+ """main's ``_preview_action_request`` ladder: a short probe until the renderer has answered once,
+ then the full deadline; an unanswered probe fails fast for a cooldown instead of stacking waits."""
+ session_id = turn._approval_owner[1]
+ now = time.monotonic()
+ with _lock:
+ bridge = _preview_bridge.setdefault(session_id, {})
+ state = bridge.get("state")
+ if state == "unanswered" and now < bridge.get("retry_at", 0):
+ return _PREVIEW_ACTION_BRIDGE_UNAVAILABLE
+ if state == "unanswered":
+ bridge["retry_at"] = now + _PREVIEW_ACTION_REPROBE_COOLDOWN_S
+ answer = _ask(turn, agent, "preview.act", payload,
+ _PREVIEW_ACTION_TIMEOUT_S if state == "answered" else _PREVIEW_ACTION_PROBE_TIMEOUT_S)
+ with _lock:
+ if answer:
+ bridge["state"] = "answered"
+ elif bridge.get("state") != "answered" and not getattr(agent, "_interrupt_requested", False):
+ bridge["state"] = "unanswered"
+ bridge["retry_at"] = time.monotonic() + _PREVIEW_ACTION_REPROBE_COOLDOWN_S
+ return answer or _PREVIEW_ACTION_BRIDGE_UNAVAILABLE
+
+
+def wire(turn, agent):
+ """Install the window bridges for one local-viewer turn; returns the registry key to release."""
+ owner = getattr(turn, "_approval_owner", None)
+ if owner is None:
+ return None
+ try:
+ _install_emitter()
+ _install_process_sinks()
+ except Exception: # health: allow BLE001 -- the window bridges are optional; the turn must run
+ logger.warning("Desktop UI sink install failed", exc_info=True)
+ key = str(getattr(turn._ctx, "session_id", "") or owner[1])
+ publish = turn._publish_execution
+ process_key = str(getattr(turn._ctx, "session_key", "") or "")
+ with _lock:
+ _turns[key] = publish
+ if process_key:
+ _process_routes[process_key] = (owner[0], owner[1])
+ agent.read_terminal_callback = _read_block(turn, agent, "terminal.read", _TERMINAL_READ_TIMEOUT_S)
+ agent.read_preview_callback = _read_block(turn, agent, "preview.read", _PREVIEW_READ_TIMEOUT_S)
+ agent.drive_preview_callback = lambda payload: _preview_action(turn, agent, dict(payload or {}))
+ agent.read_window_below_callback = lambda: _ask(turn, agent, "window.read", {}, _WINDOW_READ_TIMEOUT_S)
+ agent.theme_callback = lambda payload: _theme(turn, agent, dict(payload or {}))
+ agent.tour_callback = lambda payload: _tour(turn, agent, dict(payload or {}))
+ agent.setup_beat_callback = lambda payload: _setup_beat(turn, agent, dict(payload or {}))
+ return key, publish
+
+
+def release(token) -> None:
+ """Drop this turn's routing entry, unless a newer turn of the same session already replaced it."""
+ if not token:
+ return
+ key, publish = token
+ with _lock:
+ if _turns.get(key) is publish:
+ _turns.pop(key, None)
diff --git a/gateway/local_viewer_events.py b/gateway/local_viewer_events.py
index 2fc115dcaf0..234ae9a3d66 100644
--- a/gateway/local_viewer_events.py
+++ b/gateway/local_viewer_events.py
@@ -15,10 +15,15 @@ from __future__ import annotations
import logging
import time
+from gateway import local_desktop_ui
+
logger = logging.getLogger(__name__)
# Attributes ``wire`` may overwrite on the cached agent for one local-viewer turn.
-_TURN_ATTRS = ("tool_gen_callback", "_print_fn", "thinking_callback", "reaction_callback", "reasoning_callback")
+_TURN_ATTRS = ("tool_gen_callback", "_print_fn", "thinking_callback", "reaction_callback", "reasoning_callback",
+ *local_desktop_ui.CALLBACK_ATTRS)
+# Private key in ``wire``'s return value: the desktop window bridge registration ``restore`` releases.
+_DESKTOP_UI_TOKEN = "__desktop_ui_token__"
# MoA facade progress (agent/moa_loop._RELAY_EVENTS) -> the payload tui_gateway/tool_progress emits.
_MOA_KEYS = {"moa.reference": {"moa_index": "index", "moa_count": "count"},
"moa.progress": {"moa_refs_done": "refs_done", "moa_refs_total": "refs_total"},
@@ -45,6 +50,8 @@ def wire(turn, agent) -> dict:
if show_reasoning(turn):
# Only on request: a reasoning sink makes the agent ask the provider for reasoning.
agent.reasoning_callback = lambda text: publish("reasoning.delta", {"text": str(text or "")})
+ # The desktop window bridges (preview/terminal/window reads, preview actions, desktop_ui events).
+ previous[_DESKTOP_UI_TOKEN] = local_desktop_ui.wire(turn, agent)
return previous
@@ -84,6 +91,9 @@ def publish_progress(turn, event_type, name, preview, kwargs) -> None:
def restore(agent, previous: dict | None) -> None:
if not previous:
return
+ token = previous.pop(_DESKTOP_UI_TOKEN, None)
+ if token is not None:
+ local_desktop_ui.release(token)
for name, value in previous.items():
setattr(agent, name, value)
diff --git a/tests/gateway/test_local_desktop_ui.py b/tests/gateway/test_local_desktop_ui.py
new file mode 100644
index 00000000000..c765c7a4bcf
--- /dev/null
+++ b/tests/gateway/test_local_desktop_ui.py
@@ -0,0 +1,128 @@
+"""Desktop window bridges for a local viewer's turn on the session authority.
+
+A GUI session is offered the ``desktop_ui`` tools; on the authority their emitter, GUI callbacks
+and process-registry sinks must reach the session's viewers, and be released with the turn.
+"""
+from __future__ import annotations
+
+import json
+from types import SimpleNamespace
+
+import pytest
+
+from gateway import local_desktop_ui, local_viewer_events, session_sudo_prompt
+from gateway.session_context import clear_session_vars, set_session_vars
+from tools import desktop_ui
+from tools.process_registry import process_registry
+
+_ANSWERS = {
+ "preview.read": {"value": "page text"},
+ "terminal.read": {"value": "tick 1\ntick 2"},
+ "window.read": {"value": json.dumps({"app": "Finder"})},
+ "preview.act": {"value": json.dumps({"success": True, "acted": "clicked a"})},
+ "tour": {"value": json.dumps({"success": True})},
+ "theme.apply": {"value": json.dumps({"applied": {"accent": "violet"}})},
+ "theme.pick": {"picked": {"accent": "violet"}},
+ "setup_beat": {"choice": "next"},
+}
+
+
+class _Fanout:
+ """A viewer that answers every request it knows, unless told to stay silent."""
+
+ def __init__(self, requests, silent):
+ self.requests, self.silent = requests, silent
+
+ def transports(self):
+ return [object()]
+
+ def write(self, frame, **_kw):
+ self.requests.append(frame)
+ if frame.get("method") in _ANSWERS and frame["method"] not in self.silent:
+ session_sudo_prompt.resolve({"id": frame["id"], "result": _ANSWERS[frame["method"]]})
+ return True
+
+
+def window(monkeypatch):
+ monkeypatch.setattr(local_desktop_ui, "_turns", {})
+ monkeypatch.setattr(local_desktop_ui, "_preview_bridge", {})
+ monkeypatch.setattr(local_desktop_ui, "_tour_bridge", {})
+ monkeypatch.setattr(local_desktop_ui, "_process_routes", {})
+ monkeypatch.setattr(local_desktop_ui, "_emitter_installed", False)
+ monkeypatch.setattr(local_desktop_ui, "_process_sinks_installed", False)
+ monkeypatch.setattr(desktop_ui, "_emit", None)
+ monkeypatch.setattr(process_registry, "on_output", None)
+ monkeypatch.setattr(process_registry, "on_close", None)
+
+ published, streamed, requests, silent = [], [], [], set()
+ stream = SimpleNamespace(
+ fanout=_Fanout(requests, silent),
+ publish=lambda session_id, payload, *, event_type: streamed.append((session_id, event_type, payload)))
+ authority = SimpleNamespace(sessions={"canon": SimpleNamespace(event_stream=stream)})
+ turn = SimpleNamespace(
+ _ctx=SimpleNamespace(session_id="phys", session_key="agent:main:local:dm:1"),
+ _approval_owner=(authority, "canon", 1),
+ _publish_execution=lambda kind, payload: published.append((kind, payload)) or True)
+ agent = SimpleNamespace(_interrupt_requested=False)
+ tokens = set_session_vars(session_id="phys")
+ yield SimpleNamespace(turn=turn, agent=agent, published=published, streamed=streamed,
+ requests=requests, silent=silent)
+ clear_session_vars(tokens)
+
+
+def _emit(event, payload):
+ return desktop_ui.emit_or_error(event, payload, "failed: ", "desktop only", {"success": True})
+
+
+def test_window_events_reach_the_turns_stream(window):
+ sinks = local_viewer_events.wire(window.turn, window.agent)
+ assert json.loads(_emit("preview.open", {"url": "https://example.com"})) == {"success": True}
+ assert window.published[-1] == ("preview.open", {"url": "https://example.com"})
+ local_viewer_events.restore(window.agent, sinks)
+ assert "failed: " in _emit("preview.open", {"url": "https://example.com"})
+
+
+def test_gui_reads_are_server_requests_to_the_session_viewers(window):
+ local_viewer_events.wire(window.turn, window.agent)
+ assert window.agent.read_preview_callback() == "page text"
+ assert window.agent.read_terminal_callback(start=0, count=2) == "tick 1\ntick 2"
+ assert json.loads(window.agent.read_window_below_callback()) == {"app": "Finder"}
+ assert json.loads(window.agent.drive_preview_callback({"action": "click"}))["success"] is True
+ terminal = next(f for f in window.requests if f["method"] == "terminal.read")
+ assert terminal["params"] == {"session_id": "canon", "start": 0, "count": 2}
+
+
+def test_set_theme_applies_and_picks(window):
+ local_viewer_events.wire(window.turn, window.agent)
+ assert window.agent.theme_callback({"accent": "violet"}) == {"applied": {"accent": "violet"}}
+ assert window.agent.theme_callback({"picker": "accent"}) == {"success": True, "picked": {"accent": "violet"}}
+
+
+def test_tour_fails_fast_once_no_window_answered(window, monkeypatch):
+ monkeypatch.setattr(local_desktop_ui, "_TOUR_PROBE_TIMEOUT_S", 0.3)
+ window.silent.add("tour")
+ local_viewer_events.wire(window.turn, window.agent)
+ assert "No Hermes Desktop window answered the tour" in window.agent.tour_callback({"action": "start"})
+ sent = len(window.requests)
+ assert "No Hermes Desktop window answered the tour" in window.agent.tour_callback({"action": "start"})
+ assert len(window.requests) == sent
+
+
+def test_background_process_output_follows_its_session_after_the_turn(window):
+ sinks = local_viewer_events.wire(window.turn, window.agent)
+ local_viewer_events.restore(window.agent, sinks)
+ proc = SimpleNamespace(id="proc_1", session_key="agent:main:local:dm:1")
+ process_registry.on_output(proc, "tick 1\n")
+ process_registry.on_close(proc, "proc_1")
+ assert window.streamed == [
+ ("canon", "agent.terminal.output", {"process_id": "proc_1", "chunk": "tick 1\n"}),
+ ("canon", "terminal.close", {"process_id": "proc_1"})]
+
+
+def test_restore_puts_the_agents_callbacks_back(window):
+ sinks = local_viewer_events.wire(window.turn, window.agent)
+ local_viewer_events.restore(window.agent, sinks)
+ for attr in local_desktop_ui.CALLBACK_ATTRS:
+ assert getattr(window.agent, attr) is None
+ assert local_desktop_ui._turns == {}