Skip to content

The agent that grows with you

fix(cli): scope dashboard stop to hermes home

KoNit-K5b93b7c1ac84
diff --git a/hermes_cli/dashboard_procs.py b/hermes_cli/dashboard_procs.py
index 51366807a4a..22be3642276 100644
--- a/hermes_cli/dashboard_procs.py
+++ b/hermes_cli/dashboard_procs.py
@@ -183,6 +183,23 @@ def _normalized_home_for_compare(home: str) -> str:
return os.path.normcase(str(_resolved_home(home)))
+def _pids_owned_by_hermes_home(pids: list[int], home: str) -> list[int]:
+ """Return only *pids* whose live environment names ``home`` exactly.
+
+ Dashboard argv is discovery-only: it is not an ownership proof because
+ several Hermes installs and profiles can run the same command on one
+ machine. An unreadable or missing process environment is deliberately
+ not treated as a match, so a stop request fails closed rather than taking
+ down an unrelated backend.
+ """
+ target = _normalized_home_for_compare(home)
+ return [
+ pid for pid in pids
+ if (pid_home := _hermes_home_for_pid(pid))
+ and _normalized_home_for_compare(pid_home) == target
+ ]
+
+
def _profile_key_for_respawn(argv: list[str], hermes_home: str | None = None) -> str:
"""Stable owner key: ``HERMES_HOME`` when known, else ``--profile`` / ``-p``.
@@ -491,6 +508,7 @@ def _kill_pids_posix(pids: list[int], killed: list[int], failed: list[tuple[int,
def _kill_stale_dashboard_processes(
reason: str = "the running backend no longer matches the updated frontend", *,
restart_managed: bool = False, already_restarted_units: "set[str] | None" = None,
+ scope_home: str | None = None,
) -> dict[str, list]:
"""Kill running ``hermes dashboard`` / ``hermes serve`` processes (update end, ``--stop``).
@@ -499,6 +517,10 @@ def _kill_stale_dashboard_processes(
manual PIDs are respawned from captured argv. PIDs owned by *already_restarted_units* (no
``.service`` suffix) are left untouched, not killed twice.
+ When *scope_home* is supplied, only processes with that exact live
+ ``HERMES_HOME`` are candidates; unknown ownership fails closed. This is
+ used by ``dashboard --stop`` and the per-profile update cleanup.
+
Manually-started dashboards are not auto-restarted because we don't know the original launch args
(--host, --port, --insecure, --tui, --no-open). See #68934.
*already_restarted_units* names units (no ``.service`` suffix) the caller already restarted directly —
@@ -520,6 +542,8 @@ def _kill_stale_dashboard_processes(
# client's fixed SSH port-forward. Same ownership records as the reaper.
exclude |= _lock_owned_serve_pids()
pids = _dash._find_stale_dashboard_pids(exclude_pids=exclude or None)
+ if scope_home:
+ pids = _pids_owned_by_hermes_home(pids, scope_home)
if not pids:
return _empty_result()
# Snapshot systemd unit/cgroup and argv BEFORE killing (the cgroup dies with the process).
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index d0039c15322..39957c9ef20 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -2478,8 +2478,11 @@ def _dashboard_lifecycle_flags(args, token_file) -> None:
# from the kill result, not a re-scan: a launchd KeepAlive job respawns
# its backend on a fresh PID, which is not a failed stop.
from hermes_cli.dashboard_procs import _kill_stale_dashboard_processes
+ from hermes_constants import get_hermes_home
- result = _kill_stale_dashboard_processes(reason="requested via --stop")
+ result = _kill_stale_dashboard_processes(
+ reason="requested via --stop", scope_home=str(get_hermes_home())
+ )
sys.exit(1 if result["failed"] else 0)
diff --git a/hermes_cli/update_cmd_maint.py b/hermes_cli/update_cmd_maint.py
index 537cb223d21..addfdc36333 100644
--- a/hermes_cli/update_cmd_maint.py
+++ b/hermes_cli/update_cmd_maint.py
@@ -253,8 +253,10 @@ def _finish_dashboard_update_cleanup(
return
try:
+ from hermes_constants import get_hermes_home
stop_result = _m()._kill_stale_dashboard_processes(
- restart_managed=True, already_restarted_units=already_restarted_units
+ restart_managed=True, already_restarted_units=already_restarted_units,
+ scope_home=str(get_hermes_home()),
)
except Exception as exc:
# Isolated like every sibling post-update step: a failure here (#112604) used to abort
diff --git a/tests/hermes_cli/test_stale_pid_guard.py b/tests/hermes_cli/test_stale_pid_guard.py
index b3d32deb5b1..7d1990af6e1 100644
--- a/tests/hermes_cli/test_stale_pid_guard.py
+++ b/tests/hermes_cli/test_stale_pid_guard.py
@@ -213,3 +213,25 @@ class TestKillStaleDashboardProcesses:
assert len(taskkill_calls) == 1
assert result["killed"] == [12345]
assert result["failed"] == []
+
+ def test_stop_only_targets_the_invoking_hermes_home(self, monkeypatch):
+ """An argv match from another profile is never a ``--stop`` target."""
+ own_home = "/tmp/hermes-own"
+ foreign_home = "/tmp/hermes-foreign"
+ monkeypatch.setenv("HERMES_HOME", own_home)
+
+ with self._patch_find((12345, 12346, 12347)), mock.patch.object(
+ dashboard_procs, "_hermes_home_for_pid",
+ side_effect=lambda pid: {
+ 12345: own_home,
+ 12346: foreign_home,
+ 12347: None,
+ }[pid],
+ ), mock.patch.object(
+ dashboard_procs, "_kill_pids_posix"
+ ) as kill:
+ result = dashboard_procs._kill_stale_dashboard_processes(scope_home=own_home)
+
+ kill.assert_called_once()
+ assert kill.call_args.args[0] == [12345]
+ assert result["matched"] == [12345]