Skip to content

The agent that grows with you

refactor(hermes_cli): main_dashboard — shared probe-run/cgroup readers, cancel helper, flattened profile routing

Tekniumf6cb90e9d1c1
diff --git a/hermes_cli/main_dashboard.py b/hermes_cli/main_dashboard.py
index cb7f24640fa..923784dc3df 100644
--- a/hermes_cli/main_dashboard.py
+++ b/hermes_cli/main_dashboard.py
@@ -14,13 +14,13 @@ import subprocess
import sys
from pathlib import Path
+from typing import NoReturn
from hermes_cli.cli_output import line_input
+_PRE_BUILD_HINT = " Pre-build first: npm install --workspace web && npm run build -w web"
-def _find_stale_dashboard_pids(
- *,
- exclude_pids: set[int] | None = None,
-) -> list[int]:
+
+def _find_stale_dashboard_pids(*, exclude_pids: set[int] | None = None) -> list[int]:
"""Return PIDs of stale ``dashboard``/``serve`` processes for update cleanup."""
from hermes_cli.main import _self
return [pid for pid, _cmd in _self()._scan_dashboard_processes(exclude_pids=exclude_pids)]
@@ -29,24 +29,11 @@ def _find_stale_dashboard_pids(
def _parse_dashboard_runtime(command: str) -> tuple[str, str, int] | None:
"""Best-effort parse of a dashboard/server cmdline into mode, host, and port."""
mode = None
- if any(
- pattern in command
- for pattern in (
- "hermes dashboard",
- "hermes_cli.main dashboard",
- "hermes_cli/main.py dashboard",
- )
- ):
- mode = "dashboard"
- elif any(
- pattern in command
- for pattern in (
- "hermes serve",
- "hermes_cli.main serve",
- "hermes_cli/main.py serve",
- )
- ):
- mode = "serve"
+ for candidate in ("dashboard", "serve"):
+ patterns = (f"hermes {candidate}", f"hermes_cli.main {candidate}", f"hermes_cli/main.py {candidate}")
+ if any(pattern in command for pattern in patterns):
+ mode = candidate
+ break
if mode is None:
return None
@@ -77,70 +64,63 @@ def _dashboard_probe_host(host: str | None) -> str:
_DASHBOARD_SYSTEMD_UNIT = "hermes-dashboard.service"
+_SYSTEMCTL_ERRORS = (FileNotFoundError, subprocess.TimeoutExpired, OSError)
-def _restart_managed_dashboard_service(
- reason: str,
- unit: str = _DASHBOARD_SYSTEMD_UNIT,
-) -> bool:
+
+def _run_probe(cmd: list[str], *, timeout: int) -> subprocess.CompletedProcess:
+ """Captured, text-decoded ``subprocess.run`` for short local probes (systemctl, ps)."""
+ return subprocess.run(
+ cmd,
+ capture_output=True,
+ text=True, encoding="utf-8", errors="replace",
+ timeout=timeout,
+ )
+
+
+def _restart_managed_dashboard_service(reason: str, unit: str = _DASHBOARD_SYSTEMD_UNIT) -> bool:
"""Restart a systemd-managed dashboard instead of raw-killing its PID.
Returns True when a dashboard unit was found and handled (successfully or
- with a printed actionable failure). Returning True deliberately prevents
- the caller from falling back to ``os.kill``: systemd treats a direct
- SIGTERM of the service's main PID as a clean stop, so ``Restart=on-failure``
- will not bring the dashboard back.
+ with a printed actionable failure). Returning True deliberately prevents the
+ caller from falling back to ``os.kill``: systemd treats a direct SIGTERM of
+ the main PID as a clean stop, so ``Restart=on-failure`` won't bring it back.
"""
if sys.platform == "win32":
return False
def _systemctl(*args: str, timeout: int = 10) -> subprocess.CompletedProcess:
- return subprocess.run(
- ["systemctl", *args],
- capture_output=True,
- text=True, encoding="utf-8", errors="replace",
- timeout=timeout,
- )
+ return _run_probe(["systemctl", *args], timeout=timeout)
- # Probe the user manager first: Hermes installs Linux services in the
- # user's systemd scope by default. Only fall back to the system manager
- # when the unit is not present there, preserving root/system deployments.
- # Crucially, keep the selected scope for *all* probes and the restart — a
- # user unit must never be restarted through the system manager (or raw-killed).
+ # User manager first (Hermes installs Linux services in the user scope by
+ # default), system manager only when the unit isn't there. Keep the selected
+ # scope for ALL probes and the restart — a user unit must never be restarted
+ # through the system manager (or raw-killed).
scope: tuple[str, ...] | None = None
- listed: subprocess.CompletedProcess | None = None
for candidate in (("--user",), ()):
try:
- result = _systemctl(
- *candidate, "list-unit-files", unit, "--no-legend", "--no-pager"
- )
- except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
+ result = _systemctl(*candidate, "list-unit-files", unit, "--no-legend", "--no-pager")
+ except _SYSTEMCTL_ERRORS:
continue
if result.returncode != 0:
continue
unit_rows = (result.stdout or "").splitlines()
if any(row.split()[0:1] == [unit] for row in unit_rows if row.split()):
scope = candidate
- listed = result
break
- if scope is None or listed is None:
+ if scope is None:
return False
try:
active = _systemctl(*scope, "is-active", unit)
enabled = _systemctl(*scope, "is-enabled", unit)
- except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
+ except _SYSTEMCTL_ERRORS:
return False
active_state = (active.stdout or "").strip()
enabled_state = (enabled.stdout or "").strip()
if active_state != "active" and enabled_state not in {
- "enabled",
- "enabled-runtime",
- "linked",
- "linked-runtime",
- "static",
- "generated",
+ "enabled", "enabled-runtime", "linked", "linked-runtime", "static", "generated",
}:
return False
@@ -148,8 +128,7 @@ def _restart_managed_dashboard_service(
print(f"⟲ Restarting managed dashboard service ({reason})")
scope_label = "systemctl --user" if scope else "sudo systemctl"
- restart = ("systemctl", *scope, "restart", unit)
- commands = [restart]
+ commands = [("systemctl", *scope, "restart", unit)]
if not scope:
# System units may require privilege escalation; user units must use
# the user manager directly and never prompt for sudo.
@@ -158,21 +137,14 @@ def _restart_managed_dashboard_service(
errors: list[str] = []
for command in commands:
try:
- result = subprocess.run(
- list(command),
- capture_output=True,
- text=True, encoding="utf-8", errors="replace",
- timeout=60,
- )
- except (FileNotFoundError, subprocess.TimeoutExpired, OSError) as e:
+ result = _run_probe(list(command), timeout=60)
+ except _SYSTEMCTL_ERRORS as e:
errors.append(f"{' '.join(command)}: {e}")
continue
if result.returncode == 0:
print(f" ✓ restarted {unit}")
return True
- errors.append(
- f"{' '.join(command)}: {(result.stderr or result.stdout or '').strip()}"
- )
+ errors.append(f"{' '.join(command)}: {(result.stderr or result.stdout or '').strip()}")
print(f" ✗ failed to restart {unit}")
for err in errors:
@@ -186,43 +158,36 @@ def _restart_managed_dashboard_service(
return True
-def _get_systemd_service_for_pid(pid: int) -> str | None:
- """If *pid* belongs to a systemd service unit, return the unit name.
-
- Reads ``/proc/<pid>/cgroup`` and extracts the service name (e.g.
- ``hermes-serve.service``). Returns ``None`` when the PID is not
- part of a systemd service, when the file is unreadable, or on
- non-Linux platforms.
- """
+def _pid_unified_cgroup_entries(pid: int):
+ """Yield the ``0::<path>`` cgroup paths from ``/proc/<pid>/cgroup``; nothing when unreadable."""
try:
cgroup_path = Path(f"/proc/{pid}/cgroup")
if not cgroup_path.is_file():
- return None
+ return
text = cgroup_path.read_text(encoding="utf-8", errors="replace")
- for line in text.splitlines():
- line = line.strip()
- # Format: 0::/system.slice/hermes-serve.service
- # 0::/user.slice/user-1000.slice/session-42.scope
- parts = line.split("::", 1)
- if len(parts) != 2:
- continue
- cg_path = parts[1]
- if cg_path.endswith(".service"):
- svc_name = cg_path.rsplit("/", 1)[-1]
- if svc_name:
- return svc_name
except (OSError, PermissionError):
- pass
- return None
+ return
+ for line in text.splitlines():
+ parts = line.strip().split("::", 1)
+ if len(parts) == 2:
+ yield parts[1]
-def _extract_scope_from_cgroup(cgroup_entry: str) -> str | None:
- """Extract the systemd scope (``user`` or ``system``) from a cgroup path.
+def _get_systemd_service_for_pid(pid: int) -> str | None:
+ """The systemd service unit name *pid* belongs to (``hermes-serve.service``), or None.
- The cgroup path format is ``/system.slice/<name>.service`` for system
- services and ``/user.slice/user-<uid>.slice/<name>.service`` for user
- services. Returns ``None`` when the scope cannot be determined.
+ None when the PID isn't part of a service, the file is unreadable, or off Linux.
"""
+ for cg_path in _pid_unified_cgroup_entries(pid):
+ if cg_path.endswith(".service"):
+ svc_name = cg_path.rsplit("/", 1)[-1]
+ if svc_name:
+ return svc_name
+ return None
+
+
+def _extract_scope_from_cgroup(cgroup_entry: str) -> str | None:
+ """``user`` / ``system`` from a cgroup path (``/user.slice/…`` vs ``/system.slice/…``), else None."""
if "/system.slice/" in cgroup_entry:
return "system"
if "/user.slice/" in cgroup_entry:
@@ -231,75 +196,33 @@ def _extract_scope_from_cgroup(cgroup_entry: str) -> str | None:
def _get_pid_cgroup_path(pid: int) -> str | None:
- """Return the cgroup path from ``/proc/<pid>/cgroup``, or ``None``.
-
- Only the unified (``0::``) hierarchy cgroup entry is examined.
- """
- try:
- cgroup_path = Path(f"/proc/{pid}/cgroup")
- if not cgroup_path.is_file():
- return None
- text = cgroup_path.read_text(encoding="utf-8", errors="replace")
- for line in text.splitlines():
- line = line.strip()
- parts = line.split("::", 1)
- if len(parts) == 2:
- return parts[1]
- except (OSError, PermissionError):
- pass
- return None
+ """The unified (``0::``) cgroup path from ``/proc/<pid>/cgroup``, or None."""
+ return next(_pid_unified_cgroup_entries(pid), None)
def _try_restart_systemd_service(svc_name: str, cgroup_path: str | None = None) -> bool:
- """Attempt to restart *svc_name* via systemctl.
+ """Restart *svc_name* via systemctl (``--user`` for user-scope units). True on success.
- Uses ``systemctl --user`` for user-scope services and ``systemctl``
- for system-scope services. Returns ``True`` on success.
+ Unknown scope tries system first, then user.
"""
scope = _extract_scope_from_cgroup(cgroup_path) if cgroup_path else None
- if scope == "user":
- cmd = ["systemctl", "--user", "restart", svc_name]
- elif scope == "system":
- cmd = ["systemctl", "restart", svc_name]
- else:
- # Unknown scope — try system first, then user
- cmd = None
- for candidate in (
- ["systemctl", "restart", svc_name],
- ["systemctl", "--user", "restart", svc_name],
- ):
- try:
- r = subprocess.run(
- candidate,
- capture_output=True,
- text=True, encoding="utf-8", errors="replace",
- timeout=15,
- )
- if r.returncode == 0:
- return True
- except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
- continue
- return False
-
- try:
- r = subprocess.run(
- cmd,
- capture_output=True,
- text=True, encoding="utf-8", errors="replace",
- timeout=15,
- )
- return r.returncode == 0
- except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
- return False
+ system_cmd = ["systemctl", "restart", svc_name]
+ user_cmd = ["systemctl", "--user", "restart", svc_name]
+ candidates = {"user": [user_cmd], "system": [system_cmd]}.get(scope, [system_cmd, user_cmd])
+ for cmd in candidates:
+ try:
+ if _run_probe(cmd, timeout=15).returncode == 0:
+ return True
+ except _SYSTEMCTL_ERRORS:
+ continue
+ return False
def _dashboard_cmdline_for_pid(pid: int) -> list[str] | None:
- """Return the exact argv of a running process, when recoverable.
+ """The exact argv of a running process, when recoverable.
- Linux: reads ``/proc/<pid>/cmdline`` (NUL-separated, lossless).
- macOS: falls back to ``ps -o command=`` + shlex (best effort — quoting
- is reconstructed, but hermes launch commands don't embed exotic args).
- Windows: returns ``None``; taskkill /F gives no graceful window and the
+ Linux: ``/proc/<pid>/cmdline`` (lossless). macOS: ``ps -o command=`` + shlex
+ (best effort). Windows: None — taskkill /F gives no graceful window and the
desktop app manages its own backend there.
"""
if sys.platform == "win32":
@@ -309,19 +232,9 @@ def _dashboard_cmdline_for_pid(pid: int) -> list[str] | None:
if os.path.exists(cmdline_path):
with open(cmdline_path, "rb") as f:
raw = f.read()
- argv = [
- part.decode("utf-8", errors="replace")
- for part in raw.split(b"\x00")
- if part
- ]
+ argv = [part.decode("utf-8", errors="replace") for part in raw.split(b"\x00") if part]
return argv or None
- # macOS (no /proc): best-effort via ps.
- result = subprocess.run(
- ["ps", "-p", str(pid), "-o", "command="],
- capture_output=True,
- text=True, encoding="utf-8", errors="replace",
- timeout=10,
- )
+ result = _run_probe(["ps", "-p", str(pid), "-o", "command="], timeout=10)
if result.returncode != 0:
return None
command = (result.stdout or "").strip()
@@ -339,13 +252,11 @@ def _dashboard_cmdline_for_pid(pid: int) -> list[str] | None:
def _respawn_dashboard_processes(commands: list[list[str]]) -> list[list[str]]:
"""Best-effort respawn of manually-started dashboards after ``hermes update``.
- Spawns each recovered argv detached (new session, output to the profile's
- ``logs/dashboard-restart.log``). Returns the commands that failed to
- spawn; the caller prints the manual hint for those.
-
- Callers must pre-filter via ``_filter_dashboard_respawn_candidates`` so
- Desktop ``serve|dashboard --port 0`` backends are not replayed and
- duplicates are capped per profile (#78821).
+ Spawns each argv detached (new session, output appended to the profile's
+ ``logs/dashboard-restart.log``). Returns the commands that failed to spawn so
+ the caller can print the manual hint. Callers must pre-filter via
+ ``_filter_dashboard_respawn_candidates`` (no Desktop ``--port 0`` backends,
+ duplicates capped per profile).
"""
from hermes_constants import get_hermes_home
@@ -386,21 +297,15 @@ def _respawn_dashboard_processes(commands: list[list[str]]) -> list[list[str]]:
class _UpdateOutputStream:
"""Stream wrapper used during ``hermes update`` to survive terminal loss.
- Wraps the process's original stdout/stderr so that:
-
- * Every write is also mirrored to an append-only log file
- (``~/.hermes/logs/update.log``) that users can inspect after the
- terminal disconnects.
- * Writes to the original stream that fail with ``BrokenPipeError`` /
- ``OSError`` / ``ValueError`` (closed file) no longer cascade into
- process exit — the update keeps going, only the on-screen output
- stops.
-
- Combined with ``SIGHUP -> SIG_IGN`` installed by
- ``_install_hangup_protection``, this makes ``hermes update`` safe to
- run in a plain SSH session that might disconnect mid-install.
+ Every write is mirrored to an append-only log (``~/.hermes/logs/update.log``)
+ and writes to the original stream that fail with ``BrokenPipeError`` /
+ ``OSError`` / ``ValueError`` stop the on-screen output instead of the update.
+ With ``SIGHUP -> SIG_IGN`` from ``_install_hangup_protection`` this makes
+ ``hermes update`` safe in an SSH session that disconnects mid-install.
"""
+ _BROKEN = (BrokenPipeError, OSError, ValueError)
+
def __init__(self, original, log_file):
self._original = original
self._log = log_file
@@ -412,19 +317,13 @@ class _UpdateOutputStream:
try:
self._log.write(data)
except Exception:
- # Log errors should never abort the update.
pass
-
- if self._original_broken:
- return len(data) if isinstance(data, (str, bytes)) else 0
-
- try:
- return self._original.write(data)
- except (BrokenPipeError, OSError, ValueError):
- # Terminal vanished (SSH disconnect, shell close). Stop trying
- # to write to it, but keep the update running.
- self._original_broken = True
- return len(data) if isinstance(data, (str, bytes)) else 0
+ if not self._original_broken:
+ try:
+ return self._original.write(data)
+ except self._BROKEN:
+ self._original_broken = True # terminal vanished; keep updating
+ return len(data) if isinstance(data, (str, bytes)) else 0
def flush(self):
if self._log is not None:
@@ -436,7 +335,7 @@ class _UpdateOutputStream:
return
try:
self._original.flush()
- except (BrokenPipeError, OSError, ValueError):
+ except self._BROKEN:
self._original_broken = True
def isatty(self):
@@ -448,8 +347,7 @@ class _UpdateOutputStream:
return False
def fileno(self):
- # Some tools probe fileno(); defer to the underlying stream and let
- # callers handle failures (same behaviour as the unwrapped stream).
+ # Defer to the underlying stream; callers handle failures as when unwrapped.
return self._original.fileno()
def __getattr__(self, name):
@@ -459,31 +357,14 @@ class _UpdateOutputStream:
def _install_hangup_protection(gateway_mode: bool = False):
"""Protect ``cmd_update`` from SIGHUP and broken terminal pipes.
- Users commonly run ``hermes update`` in an SSH session or a terminal
- that may close mid-install. Without protection, ``SIGHUP`` from the
- terminal kills the Python process during ``pip install`` and leaves
- the venv half-installed; the documented workaround ("use screen /
- tmux") shouldn't be required for something as routine as an update.
-
- Protections installed:
-
- 1. ``SIGHUP`` is set to ``SIG_IGN``. POSIX preserves ``SIG_IGN``
- across ``exec()``, so pip and git subprocesses also stop dying on
- hangup.
- 2. ``sys.stdout`` / ``sys.stderr`` are wrapped to mirror output to
- ``~/.hermes/logs/update.log`` and to silently absorb
- ``BrokenPipeError`` when the terminal vanishes.
-
- ``SIGINT`` (Ctrl-C) and ``SIGTERM`` (systemd shutdown) are
- **intentionally left alone** — those are legitimate cancellation
- signals the user or OS sent on purpose.
-
- In gateway mode (``hermes update --gateway``) the update is already
- spawned detached from a terminal, so this function is a no-op.
+ 1. ``SIGHUP`` → ``SIG_IGN`` (preserved across exec, so pip/git children
+ survive hangup too). ``SIGINT``/``SIGTERM`` are intentionally left alone —
+ those are legitimate cancellation signals.
+ 2. ``sys.stdout``/``sys.stderr`` are wrapped to mirror to
+ ``~/.hermes/logs/update.log`` and absorb ``BrokenPipeError``.
- Returns a dict that ``cmd_update`` can pass to
- ``_finalize_update_output`` on exit. Returning a dict rather than a
- tuple keeps the call site forward-compatible with future additions.
+ No-op in gateway mode (already detached). Returns the state dict that
+ ``_finalize_update_output`` consumes on exit.
"""
state = {
"prev_stdout": sys.stdout,
@@ -497,17 +378,13 @@ def _install_hangup_protection(gateway_mode: bool = False):
import signal as _signal
- # (1) Ignore SIGHUP for the remainder of this process.
if hasattr(_signal, "SIGHUP"):
try:
_signal.signal(_signal.SIGHUP, _signal.SIG_IGN)
except (ValueError, OSError):
- # Called from a non-main thread — not fatal. The update still
- # runs, just without hangup protection.
- pass
+ pass # non-main thread — update still runs, just without hangup protection
- # (2) Mirror output to update.log and wrap stdio for broken-pipe
- # tolerance. Any failure here is non-fatal; we just skip the wrap.
+ # Any failure here is non-fatal; we just skip the wrap.
try:
# Late-bound import so tests can monkeypatch
# hermes_cli.config.get_hermes_home to simulate setup failure.
@@ -515,23 +392,17 @@ def _install_hangup_protection(gateway_mode: bool = False):
logs_dir = _get_hermes_home() / "logs"
logs_dir.mkdir(parents=True, exist_ok=True)
- log_path = logs_dir / "update.log"
- log_file = open(log_path, "a", buffering=1, encoding="utf-8")
+ log_file = open(logs_dir / "update.log", "a", buffering=1, encoding="utf-8")
import datetime as _dt
- log_file.write(
- f"\n=== hermes update started "
- f"{_dt.datetime.now().isoformat(timespec='seconds')} ===\n"
- )
+ log_file.write(f"\n=== hermes update started {_dt.datetime.now().isoformat(timespec='seconds')} ===\n")
state["log_file"] = log_file
sys.stdout = _UpdateOutputStream(state["prev_stdout"], log_file)
sys.stderr = _UpdateOutputStream(state["prev_stderr"], log_file)
state["installed"] = True
except Exception:
- # Leave stdio untouched on any setup failure. Update continues
- # without mirroring.
state["log_file"] = None
return state
@@ -562,11 +433,8 @@ def _finalize_update_output(state):
def _report_dashboard_status() -> int:
"""Print live listening dashboard/serve processes and return the count.
- Serve-mode backends are INCLUDED (#81564): `--stop` kills them, so
- `--status` hiding them left Desktop SSH backends invisible to the CLI —
- an operator could kill what they couldn't see. Ledger-registered serves
- (profiled launches the argv scan can't match) surface via the
- spawn-ledger augmentation in _scan_dashboard_processes.
+ Serve-mode backends are INCLUDED: ``--stop`` kills them, so hiding them from
+ ``--status`` let an operator kill what they couldn't see.
"""
from hermes_cli.main import _dashboard_listening, _self
from gateway.status import _pid_exists
@@ -577,9 +445,7 @@ def _report_dashboard_status() -> int:
if runtime is None:
continue
mode, host, port = runtime
- if port <= 0 or not _pid_exists(pid):
- continue
- if not _dashboard_listening(host, port):
+ if port <= 0 or not _pid_exists(pid) or not _dashboard_listening(host, port):
continue
live.append((pid, command, mode))
@@ -594,11 +460,7 @@ def _report_dashboard_status() -> int:
def _dashboard_listening(host: str, port: int) -> bool:
- """True when something is accepting TCP connections at host:port.
-
- Any listener counts — even a 401 response proves a dashboard is up.
- Used by the unified profile-launch routing to decide attach-vs-start.
- """
+ """True when something accepts TCP connections at host:port (even a 401 proves a dashboard is up)."""
import socket
try:
@@ -608,42 +470,37 @@ def _dashboard_listening(host: str, port: int) -> bool:
return False
+def _cancel(message: str = " Cancelled.") -> NoReturn:
+ print(message)
+ sys.exit(1)
+
+
def _maybe_setup_dashboard_auth_interactively(args) -> None:
"""Offer to configure dashboard auth when the gate engages and none exists.
- Called from ``cmd_dashboard`` just before ``start_server``. The auth
- gate engages on every non-loopback bind (``--insecure`` is a no-op since
- the June 2026 hardening) and whenever ``dashboard.public_url`` declares a
- non-loopback browser-facing hostname. ``start_server`` fails closed when no
- ``DashboardAuthProvider`` is registered. Rather than greet an interactive
- operator with that hard error, prompt them to set up the bundled password
- provider on the spot — or point them at ``hermes dashboard register`` for
- OAuth.
-
- No-ops (so the existing fail-closed ``SystemExit`` remains the backstop)
- when:
- * neither the bind nor configured public URL engages the gate, or
- * a provider is already registered, or
- * stdin/stdout isn't a TTY (Docker/s6, CI, piped ``--no-open`` runs).
+ Called from ``cmd_dashboard`` just before ``start_server``, which fails closed
+ when no ``DashboardAuthProvider`` is registered for a non-loopback bind or a
+ non-loopback ``dashboard.public_url``. Prompt an interactive operator to set
+ up the bundled password provider (or point at ``hermes dashboard register``
+ for OAuth). No-op — leaving the fail-closed ``SystemExit`` as backstop — when
+ the gate doesn't engage, a provider exists, or stdin/stdout isn't a TTY.
"""
host = getattr(args, "host", "127.0.0.1") or "127.0.0.1"
try:
from hermes_cli.web_server import should_require_dashboard_auth
if not should_require_dashboard_auth(host):
- return # local-only bind and URL — gate does not engage
+ return
except Exception:
return # if we can't tell, defer to start_server's own gate
try:
from hermes_cli.dashboard_auth import list_providers
if list_providers():
- return # a provider is already configured/registered
+ return
except Exception:
return
- # Only prompt an interactive operator. Non-TTY callers fall through to
- # start_server's fail-closed SystemExit (with the corrected fix hint).
if not (sys.stdin.isatty() and sys.stdout.isatty()):
return
@@ -663,8 +520,7 @@ def _maybe_setup_dashboard_auth_interactively(args) -> None:
try:
choice = input(" Choice [1]: ").strip() or "1"
except (EOFError, KeyboardInterrupt):
- print("\n Cancelled.")
- sys.exit(1)
+ _cancel("\n Cancelled.")
if choice == "2":
print()
@@ -679,11 +535,9 @@ def _maybe_setup_dashboard_auth_interactively(args) -> None:
)
sys.exit(0)
- if choice not in ("1",):
- print(" Cancelled.")
- sys.exit(1)
+ if choice != "1":
+ _cancel()
- # ── Username/password setup ──────────────────────────────────────────
import getpass
import secrets
@@ -693,21 +547,17 @@ def _maybe_setup_dashboard_auth_interactively(args) -> None:
password = getpass.getpass(" Password: ")
confirm = getpass.getpass(" Confirm password: ")
except (EOFError, KeyboardInterrupt):
- print("\n Cancelled.")
- sys.exit(1)
+ _cancel("\n Cancelled.")
if not password:
- print(" ✗ Empty password — aborting.")
- sys.exit(1)
+ _cancel(" ✗ Empty password — aborting.")
if password != confirm:
- print(" ✗ Passwords don't match — aborting.")
- sys.exit(1)
+ _cancel(" ✗ Passwords don't match — aborting.")
try:
from plugins.dashboard_auth.basic import hash_password
except Exception as exc:
- print(f" ✗ Could not load the password provider: {exc}")
- sys.exit(1)
+ _cancel(f" ✗ Could not load the password provider: {exc}")
password_hash = hash_password(password)
# A stable token-signing secret so sessions survive a dashboard restart.
@@ -718,31 +568,22 @@ def _maybe_setup_dashboard_auth_interactively(args) -> None:
from hermes_cli.plugins_cmd import ensure_basic_auth_plugin_enabled_in_config
cfg = load_config()
- dash = cfg.setdefault("dashboard", {})
- basic = dash.setdefault("basic_auth", {})
+ basic = cfg.setdefault("dashboard", {}).setdefault("basic_auth", {})
basic["username"] = username
basic["password_hash"] = password_hash
- # Never persist plaintext: clear any stale plaintext password key.
- basic["password"] = ""
+ basic["password"] = "" # never persist plaintext
if not str(basic.get("secret", "") or "").strip():
basic["secret"] = secret
- # The bundled basic provider is a backend plugin that still honours
- # plugins.disabled. Unblock it when we just wrote basic_auth so the
- # discover_plugins(force=True) call below can register the provider
- # (#54489). Surface the mutation so an operator who deliberately
- # disabled it isn't surprised.
+ # The bundled basic provider is a backend plugin that honours
+ # plugins.disabled; unblock it so discover_plugins below registers it,
+ # and tell an operator who deliberately disabled it.
if ensure_basic_auth_plugin_enabled_in_config(cfg):
- print(
- " ✓ Re-enabled the bundled 'basic' auth plugin "
- "(was in plugins.disabled)"
- )
+ print(" ✓ Re-enabled the bundled 'basic' auth plugin (was in plugins.disabled)")
save_config(cfg)
except Exception as exc:
- print(f" ✗ Failed to write config.yaml: {exc}")
- sys.exit(1)
+ _cancel(f" ✗ Failed to write config.yaml: {exc}")
- # Re-run plugin discovery so the basic provider registers from the
- # just-written config before start_server's gate check runs.
+ # Re-run plugin discovery so the provider registers before start_server's gate.
try:
from hermes_cli.plugins import discover_plugins
@@ -765,22 +606,17 @@ def _read_ssh_session_token_file(path: str) -> str:
return read_token(path)
import stat as _stat
- from pathlib import Path as _Path
if not os.path.isabs(path):
raise SystemExit("--ssh-session-token-file must be absolute")
- token_path = _Path(path)
- # The Desktop client writes the token under $HOME/.hermes/desktop-ssh: a
- # literal "~/.hermes/desktop-ssh" in apps/desktop/electron/remote-lifecycle.ts
- # expanded against the account's $HOME, independent of HERMES_HOME and the
- # active profile. Anchor validation to that same OS-home path, NOT to
- # get_hermes_home(): a non-default sticky profile (or any HERMES_HOME pointing
- # elsewhere, e.g. a Docker /opt/data root) re-homes get_hermes_home() and
- # would otherwise reject every token the client legitimately wrote (#69551).
- token_root = _Path.home() / ".hermes" / "desktop-ssh"
+ # The Desktop client writes the token under the account's $HOME/.hermes/
+ # desktop-ssh, independent of HERMES_HOME and the active profile. Anchor
+ # validation there, NOT get_hermes_home(): a non-default profile or a Docker
+ # /opt/data root re-homes get_hermes_home() and would reject every token.
+ token_root = Path.home() / ".hermes" / "desktop-ssh"
try:
- relative = token_path.relative_to(token_root)
+ relative = Path(path).relative_to(token_root)
except ValueError as exc:
raise SystemExit("--ssh-session-token-file must be under the desktop-ssh directory") from exc
if len(relative.parts) != 2 or not re.fullmatch(r"[0-9a-f]{32}", relative.parts[0]):
@@ -790,25 +626,26 @@ def _read_ssh_session_token_file(path: str) -> str:
directory_flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
file_flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
+ uid = os.getuid() if hasattr(os, "getuid") else None
+
+ def _check_dir(fd: int, what: str) -> None:
+ st = os.fstat(fd)
+ if not _stat.S_ISDIR(st.st_mode):
+ raise SystemExit(f"--ssh-session-token-file has an unsafe {what}")
+ if uid is not None and st.st_uid != uid:
+ raise SystemExit(f"--ssh-session-token-file {what} has the wrong owner")
+ if what == "parent directory" and (st.st_mode & 0o777) != 0o700:
+ raise SystemExit("--ssh-session-token-file parent has unsafe permissions")
+
root_fd = -1
directory_fd = -1
file_fd = -1
try:
try:
root_fd = os.open(token_root, directory_flags)
- root_stat = os.fstat(root_fd)
- if not _stat.S_ISDIR(root_stat.st_mode):
- raise SystemExit("--ssh-session-token-file has an unsafe runtime root")
- if hasattr(os, "getuid") and root_stat.st_uid != os.getuid():
- raise SystemExit("--ssh-session-token-file runtime root has the wrong owner")
+ _check_dir(root_fd, "runtime root")
directory_fd = os.open(relative.parts[0], directory_flags, dir_fd=root_fd)
- directory_stat = os.fstat(directory_fd)
- if not _stat.S_ISDIR(directory_stat.st_mode):
- raise SystemExit("--ssh-session-token-file has an unsafe parent directory")
- if hasattr(os, "getuid") and directory_stat.st_uid != os.getuid():
- raise SystemExit("--ssh-session-token-file parent has the wrong owner")
- if (directory_stat.st_mode & 0o777) != 0o700:
- raise SystemExit("--ssh-session-token-file parent has unsafe permissions")
+ _check_dir(directory_fd, "parent directory")
file_fd = os.open(relative.parts[1], file_flags, dir_fd=directory_fd)
except SystemExit:
raise
@@ -822,9 +659,9 @@ def _read_ssh_session_token_file(path: str) -> str:
raise SystemExit("--ssh-session-token-file is not a regular file")
if file_stat.st_size != 64:
raise SystemExit("--ssh-session-token-file contains an invalid token")
- if hasattr(os, "getuid") and file_stat.st_uid != os.getuid():
+ if uid is not None and file_stat.st_uid != uid:
raise SystemExit("--ssh-session-token-file has the wrong owner")
- if hasattr(os, "getuid") and (file_stat.st_mode & 0o777) & ~0o600:
+ if uid is not None and (file_stat.st_mode & 0o777) & ~0o600:
raise SystemExit("--ssh-session-token-file has unsafe permissions")
with os.fdopen(file_fd, "r", encoding="utf-8") as token_stream:
@@ -848,34 +685,26 @@ def _read_ssh_session_token_file(path: str) -> str:
def _is_electron_packaged_web_dist(path: str) -> bool:
- """True when *path* looks like an Electron-packaged renderer dist.
+ """True when *path* is an Electron-packaged renderer dist (``app.asar[.unpacked]/dist``).
- Packaged Desktop sets ``HERMES_WEB_DIST`` to ``.../app.asar/dist`` or
- ``.../app.asar.unpacked/dist``. A standalone ``hermes dashboard`` that
- inherits that value serves the desktop frontend in the browser
- (issue #52945 — "Desktop IPC bridge is unavailable").
+ A standalone ``hermes dashboard`` inheriting that ``HERMES_WEB_DIST`` would
+ serve the desktop frontend in the browser ("Desktop IPC bridge is unavailable").
"""
if not path:
return False
- # Both app.asar and app.asar.unpacked contain this marker; normalize
- # separators so Windows paths match too.
return "app.asar" in path.replace("\\", "/")
-def _route_named_profile_dashboard(
- args, _headless_backend: bool, _ssh_owner_nonce: str, _token_file: str
-) -> None:
+def _route_named_profile_dashboard(args, _headless_backend: bool, _ssh_owner_nonce: str, _token_file: str) -> None:
"""Named-profile launches route to the single MACHINE dashboard.
- The dashboard manages every profile via per-request ``?profile=`` scoping,
- so one server per profile only fragments it (port collisions, N
- processes). When a named profile launches: if the machine dashboard is
- already listening, open the browser at ``?profile=<name>`` and exit;
- otherwise re-exec as the machine dashboard pinned to ``-p default`` (so
- ``_apply_profile_override`` can't re-route through the sticky
- active_profile file) with this profile preselected. ``--isolated`` opts
- out; Desktop pool backends (HERMES_DESKTOP=1) stay per-profile. Returns
- normally when no routing applies.
+ The dashboard manages every profile via per-request ``?profile=`` scoping, so
+ one server per profile only fragments it. If the machine dashboard is already
+ listening, open the browser at ``?profile=<name>`` and exit; otherwise re-exec
+ as the machine dashboard pinned to ``-p default`` (so ``_apply_profile_override``
+ can't re-route through the sticky active_profile file) with this profile
+ preselected. ``--isolated`` opts out; Desktop pool backends (HERMES_DESKTOP=1)
+ stay per-profile. Returns normally when no routing applies.
"""
from hermes_cli.main import _dashboard_listening
try:
@@ -885,117 +714,99 @@ def _route_named_profile_dashboard(
_launch_profile = "default"
if (
- _launch_profile not in ("default", "custom")
- and not getattr(args, "isolated", False)
- and not getattr(args, "open_profile", "")
- # Desktop pool backends are intentionally per-profile.
- and os.environ.get("HERMES_DESKTOP") != "1"
+ _launch_profile in ("default", "custom")
+ or getattr(args, "isolated", False)
+ or getattr(args, "open_profile", "")
+ or os.environ.get("HERMES_DESKTOP") == "1"
):
- url = f"http://{args.host or '127.0.0.1'}:{args.port}/?profile={_launch_profile}"
- if _dashboard_listening(args.host, args.port):
- print(f"Machine dashboard already running on port {args.port}.")
- print(f" Managing profile '{_launch_profile}': {url}")
- if not args.no_open:
- try:
- import webbrowser
- webbrowser.open(url)
- except Exception:
- pass
- sys.exit(0)
+ return
- print(
- f"Routing to the machine dashboard (profile '{_launch_profile}' "
- f"preselected). Use --isolated for a dedicated per-profile server."
- )
- reexec_argv = [
- sys.executable, "-m", "hermes_cli.main",
- "-p", "default",
- # Preserve the lean serve path across the re-exec so a named-profile
- # `serve` doesn't silently rebuild the UI as `dashboard`.
- "serve" if _headless_backend else "dashboard",
- "--port", str(args.port),
- "--host", args.host,
- "--open-profile", _launch_profile,
- ]
- if _ssh_owner_nonce:
- reexec_argv.extend(["--ssh-owner-nonce", _ssh_owner_nonce])
- if _token_file:
- reexec_argv.extend(["--ssh-session-token-file", _token_file])
- if args.no_open:
- reexec_argv.append("--no-open")
- if getattr(args, "insecure", False):
- reexec_argv.append("--insecure")
- if getattr(args, "skip_build", False):
- reexec_argv.append("--skip-build")
- from tools.environments.local import build_subprocess_env
- # Exact env preservation: HERMES_HOME is explicitly pinned to the
- # machine root below — the factory must not re-inject a profile home.
- env = build_subprocess_env(scrub_secrets=False, inherit_profile_home=False)
- # Pin the child to the machine ROOT, not the launching profile's
- # HERMES_HOME. We must resolve the root explicitly instead of just
- # dropping HERMES_HOME: in the Docker layout the machine root is
- # /opt/data (set via `ENV HERMES_HOME=/opt/data`), so an unset
- # HERMES_HOME falls back to $HOME/.hermes = /opt/data/.hermes — an
- # empty, auto-seeded home where the dashboard sees only the default
- # profile and the install-method stamp is missing (so the Docker
- # update-button guard also misfires). get_default_hermes_root()
- # returns the root for both layouts: ~/.hermes for a standard install
- # and /opt/data for Docker (it strips a trailing profiles/<name>).
- # See the support report for the double-mount workaround this avoids.
- try:
- from hermes_constants import get_default_hermes_root
- env["HERMES_HOME"] = str(get_default_hermes_root())
- except Exception:
- # Best-effort: if root resolution fails, fall back to the prior
- # behaviour (drop HERMES_HOME) rather than block the reroute.
- env.pop("HERMES_HOME", None)
- # On Windows, os.execvpe() does not truly replace the process — it
- # spawns via CreateProcess then the parent exits. Under Python 3.14+
- # this can crash with STATUS_ACCESS_VIOLATION (0xC0000005) when
- # re-executing the dashboard for a non-default profile. Use
- # subprocess.Popen + sys.exit() on Windows to avoid the crash.
- if sys.platform == "win32":
- proc = subprocess.Popen(reexec_argv, env=env)
- sys.exit(proc.wait())
- else:
- os.execvpe(sys.executable, reexec_argv, env)
+ url = f"http://{args.host or '127.0.0.1'}:{args.port}/?profile={_launch_profile}"
+ if _dashboard_listening(args.host, args.port):
+ print(f"Machine dashboard already running on port {args.port}.")
+ print(f" Managing profile '{_launch_profile}': {url}")
+ if not args.no_open:
+ try:
+ import webbrowser
+ webbrowser.open(url)
+ except Exception:
+ pass
+ sys.exit(0)
+
+ print(
+ f"Routing to the machine dashboard (profile '{_launch_profile}' "
+ f"preselected). Use --isolated for a dedicated per-profile server."
+ )
+ reexec_argv = [
+ sys.executable, "-m", "hermes_cli.main",
+ "-p", "default",
+ # Preserve the lean serve path so a named-profile `serve` doesn't
+ # silently rebuild the UI as `dashboard`.
+ "serve" if _headless_backend else "dashboard",
+ "--port", str(args.port),
+ "--host", args.host,
+ "--open-profile", _launch_profile,
+ ]
+ if _ssh_owner_nonce:
+ reexec_argv.extend(["--ssh-owner-nonce", _ssh_owner_nonce])
+ if _token_file:
+ reexec_argv.extend(["--ssh-session-token-file", _token_file])
+ if args.no_open:
+ reexec_argv.append("--no-open")
+ if getattr(args, "insecure", False):
+ reexec_argv.append("--insecure")
+ if getattr(args, "skip_build", False):
+ reexec_argv.append("--skip-build")
+ from tools.environments.local import build_subprocess_env
+ # HERMES_HOME is pinned to the machine root below — the factory must not
+ # re-inject a profile home.
+ env = build_subprocess_env(scrub_secrets=False, inherit_profile_home=False)
+ # Pin the child to the machine ROOT, resolved explicitly rather than by
+ # dropping HERMES_HOME: in the Docker layout the root is /opt/data, and an
+ # unset HERMES_HOME would fall back to $HOME/.hermes = /opt/data/.hermes — an
+ # empty auto-seeded home with only the default profile and no install stamp.
+ # get_default_hermes_root() strips a trailing profiles/<name> for both layouts.
+ try:
+ from hermes_constants import get_default_hermes_root
+ env["HERMES_HOME"] = str(get_default_hermes_root())
+ except Exception:
+ env.pop("HERMES_HOME", None) # prior behaviour rather than blocking the reroute
+ # On Windows os.execvpe() spawns via CreateProcess then exits, which under
+ # Python 3.14+ can crash with STATUS_ACCESS_VIOLATION; use Popen + exit.
+ if sys.platform == "win32":
+ proc = subprocess.Popen(reexec_argv, env=env)
+ sys.exit(proc.wait())
+ else:
+ os.execvpe(sys.executable, reexec_argv, env)
def _resolve_dashboard_web_dist(args, _headless_backend: bool) -> None:
"""Build or validate the web UI dist before the server imports.
- ``serve`` sets HERMES_SERVE_HEADLESS so mount_spa() stays off even if a
- stray dist exists. Otherwise build unless HERMES_WEB_DIST or --skip-build
- says a dist is pre-built — in which case verify index.html exists (an
- unverified promise means the server starts and serves 404s, #23817).
- --skip-build on the default dist location gets ONE recovery build
- (#59288); a caller-managed HERMES_WEB_DIST cannot be populated and is
- written back expanded because web_server reads it raw at import.
+ ``serve`` sets HERMES_SERVE_HEADLESS so mount_spa() stays off even if a stray
+ dist exists. Otherwise build unless HERMES_WEB_DIST or --skip-build says a
+ dist is pre-built — then verify index.html exists (an unverified promise
+ means the server starts and serves 404s). --skip-build on the default dist
+ location gets ONE recovery build; a caller-managed HERMES_WEB_DIST cannot be
+ populated and is written back expanded because web_server reads it raw.
"""
from hermes_cli.main import PROJECT_ROOT, _build_web_ui
+ skip_build = getattr(args, "skip_build", False)
if _headless_backend:
- # Don't build the SPA, and tell mount_spa() (read at web_server import
- # below) to disable it even if a stray dist exists. Set it first.
- os.environ["HERMES_SERVE_HEADLESS"] = "1"
- elif "HERMES_WEB_DIST" not in os.environ and not getattr(args, "skip_build", False):
+ os.environ["HERMES_SERVE_HEADLESS"] = "1" # set before web_server import
+ elif "HERMES_WEB_DIST" not in os.environ and not skip_build:
if not _build_web_ui(PROJECT_ROOT / "web", fatal=True):
sys.exit(1)
- elif getattr(args, "skip_build", False):
- # --build-mode skip trusts the caller to have pre-built the web UI.
- # Verify the dist actually exists; otherwise the server will start
- # and serve 404s with no obvious cause (issue #23817).
+ elif skip_build:
_dist_root = (
Path(os.environ["HERMES_WEB_DIST"])
if "HERMES_WEB_DIST" in os.environ
else PROJECT_ROOT / "hermes_cli" / "web_dist"
)
if not (_dist_root / "index.html").exists():
- # The caller promised a pre-built dist but there isn't one.
- # Instead of hard-failing (issue #59288 — desktop launches with
- # --build-mode skip after a wipe of web_dist), warn and attempt
- # ONE recovery build through the normal build path. Only the
- # default dist location is recoverable: a custom HERMES_WEB_DIST
- # points at a caller-managed directory the build cannot populate.
+ # Only the default dist location is recoverable (desktop launches with
+ # --build-mode skip after a wipe of web_dist); a custom HERMES_WEB_DIST
+ # is a caller-managed directory the build cannot populate.
_recoverable = "HERMES_WEB_DIST" not in os.environ
if _recoverable:
print(f"⚠ --skip-build was passed but no web dist found at: {_dist_root}")
@@ -1005,25 +816,21 @@ def _resolve_dashboard_web_dist(args, _headless_backend: bool) -> None:
print(f"✗ --skip-build was passed but no web dist found at: {_dist_root}")
if _recoverable:
print(" The recovery build did not produce a usable dist.")
- print(" Pre-build first: npm install --workspace web && npm run build -w web")
+ print(_PRE_BUILD_HINT)
print(" Or drop --skip-build to build automatically.")
sys.exit(1)
print(" ✓ Recovery build produced a web dist")
print(f"→ Skipping web UI build (--skip-build); using dist at {_dist_root}")
else:
- # HERMES_WEB_DIST is set without --skip-build: the build is skipped
- # (the env var points at a caller-managed dist), so validate it the
- # same way the --skip-build branch does — otherwise the server starts
- # and serves 404s with no obvious cause (same failure mode as #23817,
- # via the env-var path).
+ # HERMES_WEB_DIST without --skip-build: the env var points at a
+ # caller-managed dist, so validate it like the --skip-build branch.
_dist_root = Path(os.environ["HERMES_WEB_DIST"]).expanduser()
if not (_dist_root / "index.html").exists():
print(f"✗ HERMES_WEB_DIST is set but no web dist found at: {_dist_root}")
- print(" Pre-build first: npm install --workspace web && npm run build -w web")
+ print(_PRE_BUILD_HINT)
print(" Or unset HERMES_WEB_DIST to build and use the default web UI dist.")
sys.exit(1)
- # Write the expanded path back: web_server reads HERMES_WEB_DIST raw
- # at import (no expanduser), so a validated "~/dist" would otherwise
- # pass here and still 404 there.
+ # web_server reads HERMES_WEB_DIST raw at import (no expanduser), so a
+ # validated "~/dist" would otherwise pass here and still 404 there.
os.environ["HERMES_WEB_DIST"] = str(_dist_root)
print(f"→ Using web dist from HERMES_WEB_DIST: {_dist_root}")