Skip to content

The agent that grows with you

fix(desktop): run the Desktop's boot work when it first connects to the gateway

alt-glitch9dea37ecae6f
diff --git a/tests/gateway/test_local_desktop_identity.py b/tests/gateway/test_local_desktop_identity.py
index 7e52686d576..ec1d820dc86 100644
--- a/tests/gateway/test_local_desktop_identity.py
+++ b/tests/gateway/test_local_desktop_identity.py
@@ -193,3 +193,25 @@ async def test_a_fresh_launch_claims_the_trial_and_opens_onboarding_on_it(tmp_pa
assert "error" not in resumed, resumed
finally:
await conn.connection.close()
+
+
+async def test_the_first_local_desktop_connection_runs_the_desktop_boot_work(tmp_path, monkeypatch):
+ """`hermes serve` recorded a fresh install at boot (onboarding stays due when the first launch misses
+ the run). The gateway never boots that way, so the local Desktop's first accepted connection does it,
+ as work for that Desktop, and only once."""
+ from hermes_cli import anon_auth, free_tier_bootstrap, onboarding_run
+ from tui_gateway import server
+ seen = []
+ monkeypatch.setattr(onboarding_run, "latch_first_install", lambda: seen.append(anon_auth.on_local_desktop_backend()))
+ monkeypatch.setattr(server, "_local_desktop_boot_started", False)
+ free_tier_bootstrap.reset_for_tests()
+ conn = await _connect(tmp_path, monkeypatch)
+ try:
+ await _request(conn, 1, "client.capabilities", {"desktop_local": True})
+ await _request(conn, 2, "client.capabilities", {"desktop_local": True})
+ assert free_tier_bootstrap.wait_for_record(timeout=20) is not None
+ assert seen == [True]
+ finally:
+ await conn.connection.close()
+ free_tier_bootstrap.reset_for_tests()
diff --git a/tui_gateway/methods_onboarding.py b/tui_gateway/methods_onboarding.py
index f75a63c9da3..b89fb23694b 100644
--- a/tui_gateway/methods_onboarding.py
+++ b/tui_gateway/methods_onboarding.py
@@ -104,6 +104,27 @@ def start_due_boot() -> None:
spawn_context_thread(finish, name="onboarding-due", args=(compute,)).start()
+_local_desktop_boot_lock = threading.Lock()
+_local_desktop_boot_started = False
+
+
+def begin_local_desktop_boot() -> None:
+ """The local Desktop's first accepted connection to this process. It no longer spawns its own
+ ``hermes serve``, so the boot work that ran there (record a fresh install, settle an interrupted
+ claim, the setup record and the due answer) runs here, once, as work for that Desktop."""
+ global _local_desktop_boot_started
+ from hermes_cli import free_tier_bootstrap
+ from hermes_cli.process_identity import serving_local_desktop
+
+ with _local_desktop_boot_lock:
+ if _local_desktop_boot_started:
+ return
+ _local_desktop_boot_started = True
+ with serving_local_desktop(True): # the threads below copy this context
+ free_tier_bootstrap.start_background_bootstrap()
+ start_due_boot()
+
+
# ── first-run onboarding: one chat in the default profile, driven by setup_beat ───────────────
# Onboarding always runs in the default profile, whichever profile this backend was launched under.
diff --git a/tui_gateway/methods_voice.py b/tui_gateway/methods_voice.py
index ff5646e7ec9..176efc93490 100644
--- a/tui_gateway/methods_voice.py
+++ b/tui_gateway/methods_voice.py
@@ -533,6 +533,9 @@ def _(rid, params: dict) -> dict:
server_requests.advertise(transport, bool(params.get("server_requests")))
if "desktop_local" in params and hasattr(transport, "auth_identity"): # a WebSocket client
transport.desktop_local = local_desktop_client(transport.auth_identity, params.get("desktop_local"))
+ if transport.desktop_local:
+ from tui_gateway import server
+ server.begin_local_desktop_boot()
return _ok(rid, {"server_requests": sorted(contracts.SERVER_REQUESTS), "declines_not_shown": True})