The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): one desktop-owned-child predicate, exit 78 on host-owner refusal
teknium19ca3b54e9133
diff --git a/hermes_cli/main.py b/hermes_cli/main.pyindex fc581ec3885..d9c90d426e5 100644--- a/hermes_cli/main.py+++ b/hermes_cli/main.py@@ -792,12 +792,12 @@ from hermes_cli.main_platform_setup import (cmd_whatsapp,cmd_whatsapp_cloud,)+from hermes_cli.process_identity import is_desktop_owned_backend as _is_desktop_owned_backendfrom hermes_cli.main_dashboard import (_attach_to_host_backend,_finalize_update_output,_find_stale_dashboard_pids,_install_hangup_protection,- _is_desktop_owned_backend,_is_electron_packaged_web_dist,_maybe_setup_dashboard_auth_interactively,_read_ssh_session_token_file,diff --git a/hermes_cli/main_dashboard.py b/hermes_cli/main_dashboard.pyindex a63b6e88ffc..600e3cfc858 100644--- a/hermes_cli/main_dashboard.py+++ b/hermes_cli/main_dashboard.py@@ -14,6 +14,7 @@ import sysfrom pathlib import Pathfrom typing import NoReturnfrom hermes_cli.cli_output import line_input+from hermes_cli.process_identity import is_desktop_owned_backend as _is_desktop_owned_backend_PRE_BUILD_HINT = " Pre-build first: npm install --workspace web && npm run build -w web"@@ -751,20 +752,6 @@ def _is_electron_packaged_web_dist(path: str) -> bool:return "app.asar" in path.replace("\\", "/")-def _is_desktop_owned_backend() -> bool:- """Whether this process is the backend that Desktop spawned and owns.-- ``HERMES_DESKTOP`` is a marker inherited by shells launched from Desktop;- it is not ownership proof. Desktop gives only its backend a fresh session- credential, so requiring both values keeps inherited terminal commands on- the normal one-host-backend path.- """- return (- os.environ.get("HERMES_DESKTOP") == "1"- and bool(os.environ.get("HERMES_DASHBOARD_SESSION_TOKEN"))- )--def _host_backend_attachment():"""Live host serve/dashboard record to attach to, or ``None``.@@ -834,8 +821,10 @@ def _attach_to_host_backend(args, headless_backend: bool) -> None:graceful-shutdown window or a foreign listener that inherited the port) — a supervisor or`hermes update` relaunch landing in that window would otherwise exit 0 with NOTHINGlistening, reporting success for a dead service;- * an explicitly typed ``--port``/``--host`` the owner cannot serve is a non-zero REFUSAL- naming the owner, never a silent redirect;+ * an explicitly typed ``--port``/``--host`` the owner cannot serve is a REFUSAL naming the+ owner, never a silent redirect. It exits 78 (EX_CONFIG), the deliberate-refusal code+ ``RestartPreventExitStatus=78`` parks on: exit 1 under ``Restart=always`` was an infinite+ restart loop with nothing listening on the ingress port (#119824);* a `hermes dashboard` user is never handed a headless backend's URL (no SPA behind it).Returns normally — leaving the caller to BIND — when no owner answers.@@ -847,6 +836,7 @@ def _attach_to_host_backend(args, headless_backend: bool) -> None:returnfrom gateway import host_rendezvous as hr+ from gateway.restart import GATEWAY_FATAL_CONFIG_EXIT_CODEidentity = hr.probe_owner(record)if identity is None:@@ -860,13 +850,13 @@ def _attach_to_host_backend(args, headless_backend: bool) -> None:print(f"Refusing to start: this host is already served by {hr.describe(record)}.")print(f" You asked for {conflict}.")print(" Stop that backend, or drop the flag to use the running one.")- sys.exit(1)+ sys.exit(GATEWAY_FATAL_CONFIG_EXIT_CODE)if not headless_backend and not identity.get("servesSpa"):print(f"Refusing to start: this host is already served by {hr.describe(record)}, ""which is a headless `hermes serve` backend with no dashboard UI.")print(" Stop it and run `hermes dashboard`, or use --isolated for a dedicated server.")- sys.exit(1)+ sys.exit(GATEWAY_FATAL_CONFIG_EXIT_CODE)try:from hermes_cli.profiles import get_active_profile_namediff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.pyindex b71a0662157..70bd6fe59b7 100644--- a/hermes_cli/process_identity.py+++ b/hermes_cli/process_identity.py@@ -218,6 +218,20 @@ def register_self(purpose: str, *, project_root: Optional[Path] = None, detail:return _append_entry(entry)+def is_desktop_owned_backend() -> bool:+ """Whether this process is the backend Desktop spawned and owns.++ ``HERMES_DESKTOP=1`` is inherited by every shell and agent child the app launches, so the+ flag alone is not ownership proof (same class as #116107). Desktop mints a per-spawn+ ``HERMES_DASHBOARD_SESSION_TOKEN`` only for its backend; the terminal pane never receives+ it and the terminal tool's env policy strips it from agent children.+ """+ return (+ os.environ.get("HERMES_DESKTOP") == "1"+ and bool(os.environ.get("HERMES_DASHBOARD_SESSION_TOKEN"))+ )++def _desktop_spawner_identity() -> tuple[Optional[int], Optional[float]]:"""Spawner ``(pid, create_time)`` from the Electron app's HERMES_PARENT_PID (+ optional``winms:<ms>`` start marker) parent-death watchdog vars, so ledger lineage works with everydiff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.pyindex 2460338d77d..8ae2774476c 100644--- a/hermes_cli/web_server.py+++ b/hermes_cli/web_server.py@@ -1212,11 +1212,15 @@ def _best_effort(what: str, fn) -> None:def _publish_host_rendezvous(host: str, port: int) -> None:- """Publish the machine-level serve owner unless this is a Desktop pool child."""- # Desktop-spawned backends have a per-profile lifecycle. Publishing one as- # the host owner makes a later independently supervised dashboard attach to- # (or refuse behind) the private loopback child instead of its public bind.- if os.getenv("HERMES_DESKTOP") == "1":+ """Publish the machine-level serve owner unless this is a Desktop-owned pool child."""+ # Desktop-spawned backends (flag + per-spawn token; the bare flag is inherited by every+ # Desktop shell) are loopback, random-port and per-profile. Recording one as the host owner+ # made a later independently supervised `dashboard --host 0.0.0.0 --port N` refuse behind+ # the private child on every restart (#119824). Desktop discovers its own backends through+ # spawn-ledger.json, never through this record.+ from hermes_cli.process_identity import is_desktop_owned_backend++ if is_desktop_owned_backend():returnfrom gateway import host_rendezvous as hrdiff --git a/tests/hermes_cli/test_dashboard_unified_launch.py b/tests/hermes_cli/test_dashboard_unified_launch.pyindex 2e79f55f23a..3e9c8792dc7 100644--- a/tests/hermes_cli/test_dashboard_unified_launch.py+++ b/tests/hermes_cli/test_dashboard_unified_launch.py@@ -62,31 +62,6 @@ class TestUnifiedDashboardRouting:assert env.get("HERMES_HOME") == str(get_default_hermes_root())- def test_inherited_desktop_marker_routes_named_profile_to_machine_dashboard(- self, main_mod, monkeypatch- ):- """A Desktop shell is not its credential-bearing backend child."""- monkeypatch.setenv("HERMES_DESKTOP", "1")- monkeypatch.delenv("HERMES_DASHBOARD_SESSION_TOKEN", raising=False)- monkeypatch.setattr(- "hermes_cli.profiles.get_active_profile_name", lambda: "worker_x"- )- monkeypatch.setattr(main_dashboard, "_dashboard_listening", lambda host, port: False)- execs = []-- def fake_exec(exe, argv, env):- execs.append((exe, argv, env))- raise SystemExit(0)-- monkeypatch.setattr(main_mod.os, "execvpe", fake_exec)-- with pytest.raises(SystemExit):- main_mod.cmd_dashboard(_args())-- assert len(execs) == 1- assert "-p" in execs[0][1] and execs[0][1][execs[0][1].index("-p") + 1] == "default"--def test_desktop_profile_backend_skips_machine_dashboard_reroute(self, main_mod, monkeypatch):"""A desktop-spawned named-profile backend (HERMES_DESKTOP=1) must NOTreroute into the machine dashboard. The reroute re-execs as the defaultdiff --git a/tests/hermes_cli/test_serve_host_attach.py b/tests/hermes_cli/test_serve_host_attach.pyindex 6d77c67a60c..61dcc9b3e4d 100644--- a/tests/hermes_cli/test_serve_host_attach.py+++ b/tests/hermes_cli/test_serve_host_attach.py@@ -14,6 +14,7 @@ from types import SimpleNamespaceimport pytestfrom gateway import host_rendezvous as hr+from gateway.restart import GATEWAY_FATAL_CONFIG_EXIT_CODEfrom hermes_cli.main_dashboard import _attach_to_host_backend@@ -164,7 +165,9 @@ def test_an_explicit_endpoint_the_owner_cannot_serve_is_refused(host_dir, owner,with pytest.raises(SystemExit) as exc:_attach_to_host_backend(_args(**over), headless_backend=True)- assert exc.value.code == 1+ # 78 (EX_CONFIG) is the deliberate refusal a supervisor parks on; exit 1 under+ # Restart=always was an infinite loop with nothing listening (#119824).+ assert exc.value.code == GATEWAY_FATAL_CONFIG_EXIT_CODEassert f"PID {os.getpid()}" in capsys.readouterr().out@@ -177,5 +180,5 @@ def test_dashboard_is_never_routed_to_a_headless_backend(host_dir, owner, capsyswith pytest.raises(SystemExit) as exc:_attach_to_host_backend(_args(), headless_backend=False)- assert exc.value.code == 1+ assert exc.value.code == GATEWAY_FATAL_CONFIG_EXIT_CODEassert "no dashboard UI" in capsys.readouterr().outdiff --git a/tests/hermes_cli/test_web_server.py b/tests/hermes_cli/test_web_server.pyindex 4a555a10a5d..43c9abb7529 100644--- a/tests/hermes_cli/test_web_server.py+++ b/tests/hermes_cli/test_web_server.py@@ -3180,6 +3180,7 @@ class TestDesktopHostRendezvousIsolation:import hermes_cli.web_server as web_servermonkeypatch.setenv("HERMES_DESKTOP", "1")+ monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN", "desktop-spawn-token")claimed = []monkeypatch.setattr(hr, "claim_host_lock", lambda role: claimed.append(role))@@ -3188,11 +3189,13 @@ class TestDesktopHostRendezvousIsolation:assert claimed == []def test_standalone_backend_still_claims_the_host_serve_record(self, monkeypatch):- """The Desktop exclusion must not alter standalone dashboard discovery."""+ """The Desktop exclusion must not alter standalone dashboard discovery — including a+ supervised service whose shell merely inherited HERMES_DESKTOP=1 without the token."""from gateway import host_rendezvous as hrimport hermes_cli.web_server as web_server- monkeypatch.delenv("HERMES_DESKTOP", raising=False)+ monkeypatch.setenv("HERMES_DESKTOP", "1")+ monkeypatch.delenv("HERMES_DASHBOARD_SESSION_TOKEN", raising=False)claimed = []published = []monkeypatch.setattr(diff --git a/website/docs/user-guide/features/web-dashboard.md b/website/docs/user-guide/features/web-dashboard.mdindex 3b123023edb..bed869e5373 100644--- a/website/docs/user-guide/features/web-dashboard.md+++ b/website/docs/user-guide/features/web-dashboard.md@@ -182,8 +182,18 @@ Set a username and password, then run the dashboard bound to a reachable addressEnvironmentFile=%h/.hermes/.envExecStart=/path/to/venv/bin/python -m hermes_cli.main dashboard \--host 0.0.0.0 --port 9119 --no-open+Restart=always+RestartSec=10+# Exit 78 (EX_CONFIG) is a deliberate refusal ("this host is already served by PID … on+# another port"); parking on it beats an infinite restart loop with nothing listening.+RestartPreventExitStatus=78```+One backend serves a whole host, so when `hermes dashboard` finds a live backend it cannot+serve your typed `--host`/`--port` with, it refuses with exit 78 and names the owner. Stop that+backend, or give the service its own dedicated server with `--isolated`. The Desktop app's own+loopback backends never claim host ownership, so the two can coexist without either flag.+with `~/.hermes/.env` containing:```bash