The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): owner home for a pid follows the real profile resolver
teknium1537ed2a45e68
diff --git a/hermes_cli/dashboard_procs.py b/hermes_cli/dashboard_procs.pyindex 987699b483b..1433eb36a0b 100644--- a/hermes_cli/dashboard_procs.py+++ b/hermes_cli/dashboard_procs.py@@ -127,27 +127,39 @@ def _hermes_home_for_pid(pid: int) -> str | None:"""The Hermes home *pid* runs on, tri-state: ``None`` ONLY when its environment is unreadable(another user, hardened ``/proc``) — callers spare those, never guess.- A readable environment always resolves: ``HERMES_HOME`` when exported at exec time, else the- platform default home of the process's own ``HOME`` / ``LOCALAPPDATA`` (the common install shape- exports nothing). ``hermes --profile X serve`` sets ``HERMES_HOME`` in ``os.environ`` AFTER- startup, which ``/proc/<pid>/environ`` never reflects, so a ``--profile``/``-p`` flag in the- argv selects ``<default root>/profiles/X`` — the home ``_apply_profile_override`` resolves.+ A readable environment always resolves, replaying ``_apply_profile_override`` on the target's+ exec-time env + argv (``hermes -p X serve`` rewrites ``HERMES_HOME`` in ``os.environ`` AFTER+ startup, which ``/proc/<pid>/environ`` never reflects): a profile-shaped ``HERMES_HOME``+ without a flag is the home; otherwise the root is ``HERMES_HOME`` (its grandparent when+ profile-shaped) or the platform default of the process's own ``HOME`` / ``LOCALAPPDATA``, and+ the profile is the ``--profile``/``-p`` flag, else the root's sticky ``active_profile`` unless+ the process has a fixed identity (supervised child, post-swap updater, Desktop SSH backend)."""env = _pid_environ(pid)if env is None:return None- if home := env.get("HERMES_HOME", "").strip():- return home+ from hermes_cli.main_dashboard import _dashboard_cmdline_for_pid+ from hermes_cli.profiles import get_active_profile, normalize_profile_name, profile_root_for_env_home+ argv = _dashboard_cmdline_for_pid(pid) or []+ env_home = env.get("HERMES_HOME", "").strip()+ profile = _profile_flag_value(argv)+ if profile is None and env_home and (+ Path(env_home).parent.name == "profiles" or env.get("HERMES_UPDATE_POST_SWAP") == "1"+ ):+ return env_homeif sys.platform == "win32":local_appdata = env.get("LOCALAPPDATA", "").strip()base = Path(local_appdata) if local_appdata else Path(env.get("USERPROFILE") or Path.home()) / "AppData" / "Local"default_home = base / "hermes"else:default_home = Path(env.get("HOME") or Path.home()) / ".hermes"- from hermes_cli.main_dashboard import _dashboard_cmdline_for_pid- if profile := _profile_flag_value(_dashboard_cmdline_for_pid(pid) or []):- return str(default_home / "profiles" / profile)- return str(default_home)+ root = profile_root_for_env_home(env_home, default_home)+ fixed_identity = any(env.get(k) for k in ("HERMES_SUPERVISED_CHILD", "HERMES_S6_SUPERVISED_CHILD",+ "HERMES_GATEWAY_EXTERNAL_SUPERVISOR")) or "--ssh-session-token-file" in argv+ if profile is None and not fixed_identity:+ profile = get_active_profile(root)+ canon = normalize_profile_name(profile) if profile else "default"+ return str(root) if canon == "default" else str(root / "profiles" / canon)def _dashboard_subcommand_index(argv: list[str]) -> int | None:diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.pyindex 5ca7885ccd5..9cc7e62db69 100644--- a/hermes_cli/profiles.py+++ b/hermes_cli/profiles.py@@ -1611,9 +1611,9 @@ def _stop_gateway_process(profile_dir: Path) -> None:# Active profile (sticky default)-def get_active_profile() -> str:- """Read the sticky active profile name."""- path = _get_active_profile_path()+def get_active_profile(root: Path | None = None) -> str:+ """Read the sticky active profile name (of *root*, default: this process's Hermes root)."""+ path = root / "active_profile" if root is not None else _get_active_profile_path()try:return path.read_text(encoding="utf-8").strip() or "default"except (UnicodeDecodeError, OSError):@@ -2003,6 +2003,17 @@ def rename_profile(old_name: str, new_name: str) -> Path:# Profile env resolution (called from _apply_profile_override)+def profile_root_for_env_home(env_home: str, default_root: Path) -> Path:+ """Hermes root named by an exported ``HERMES_HOME``: the grandparent of a profile-shaped value+ (``<root>/profiles/<name>``, mirrors ``get_default_hermes_root()``), the value itself otherwise,+ *default_root* when unset. Pure: callers pass any process's env, not only ``os.environ``."""+ env_home = env_home.strip()+ if not env_home:+ return default_root+ env_path = Path(env_home)+ return env_path.parent.parent if env_path.parent.name == "profiles" else env_path++def resolve_profile_env(profile_name: str) -> str:"""Resolve a profile name to a HERMES_HOME path string. Called early in the CLI entrypoint, before hermes modules are imported, to set HERMES_HOME.@@ -2014,14 +2025,7 @@ def resolve_profile_env(profile_name: str) -> str:(junction-transparent); only the spelling is preserved."""canon = _canon_valid(profile_name)- env_home = os.environ.get("HERMES_HOME", "").strip()- if env_home:- env_path = Path(env_home)- # A profile-shaped env value means the root is the grandparent (mirrors- # get_default_hermes_root()).- root = env_path.parent.parent if env_path.parent.name == "profiles" else env_path- else:- root = _get_default_hermes_home()+ root = profile_root_for_env_home(os.environ.get("HERMES_HOME", ""), _get_default_hermes_home())if canon == "default":return str(root)profile_dir = root / "profiles" / canondiff --git a/tests/hermes_cli/test_stale_pid_guard.py b/tests/hermes_cli/test_stale_pid_guard.pyindex dbfcb5fa45f..9bbc40dec66 100644--- a/tests/hermes_cli/test_stale_pid_guard.py+++ b/tests/hermes_cli/test_stale_pid_guard.py@@ -243,19 +243,40 @@ class TestKillStaleDashboardProcesses:class TestHermesHomeForPid:"""Tri-state owner resolution: a readable environment always names a home."""- def test_readable_env_without_var_resolves_to_that_process_default_home(self, monkeypatch):+ @pytest.mark.skipif(sys.platform == "win32", reason="POSIX default home is $HOME/.hermes")+ def test_readable_env_without_var_resolves_to_that_process_default_home(self, monkeypatch, tmp_path):"""The common install shape exports no HERMES_HOME: the backend lives in its user'splatform default home, and a default-home ``--stop`` must still find it (#113978)."""- monkeypatch.setattr(sys, "platform", "linux")- monkeypatch.setattr(dashboard_procs, "_pid_environ", lambda pid: {"HOME": "/home/alice"})+ home = str(tmp_path / "alice")+ monkeypatch.setattr(dashboard_procs, "_pid_environ", lambda pid: {"HOME": home})from hermes_cli import main_dashboardmonkeypatch.setattr(main_dashboard, "_dashboard_cmdline_for_pid",lambda pid: ["hermes", "--profile", "work", "serve"] if pid == 2 else ["hermes", "serve"])- assert dashboard_procs._hermes_home_for_pid(1) == "/home/alice/.hermes"+ assert dashboard_procs._hermes_home_for_pid(1) == f"{home}/.hermes"# ``-p``/``--profile`` is applied to os.environ after exec, invisible in /proc environ.- assert dashboard_procs._hermes_home_for_pid(2) == "/home/alice/.hermes/profiles/work"- assert dashboard_procs._pids_owned_by_hermes_home([1, 2], "/home/alice/.hermes") == [1]+ assert dashboard_procs._hermes_home_for_pid(2) == f"{home}/.hermes/profiles/work"+ assert dashboard_procs._pids_owned_by_hermes_home([1, 2], f"{home}/.hermes") == [1]++ def test_root_shaped_hermes_home_follows_the_flag_and_the_sticky_active_profile(self, monkeypatch, tmp_path):+ """Mirror ``_apply_profile_override``: an exported root ``HERMES_HOME`` is the root, not the+ home — ``-p work`` and ``hermes profile use work`` both land in ``<root>/profiles/work``."""+ root = tmp_path / ".hermes"+ root.mkdir()+ monkeypatch.setattr(dashboard_procs, "_pid_environ",+ lambda pid: {"HOME": str(tmp_path), "HERMES_HOME": str(root)})+ from hermes_cli import main_dashboard+ monkeypatch.setattr(main_dashboard, "_dashboard_cmdline_for_pid",+ lambda pid: ["hermes", "-p", "work", "serve"] if pid == 2 else ["hermes", "serve"])++ assert dashboard_procs._hermes_home_for_pid(2) == str(root / "profiles" / "work")+ assert dashboard_procs._hermes_home_for_pid(1) == str(root) # no flag, no active_profile+ (root / "active_profile").write_text("work", encoding="utf-8")+ assert dashboard_procs._hermes_home_for_pid(1) == str(root / "profiles" / "work")+ # A profile-shaped HERMES_HOME without a flag is the home itself (root = its grandparent).+ monkeypatch.setattr(dashboard_procs, "_pid_environ",+ lambda pid: {"HERMES_HOME": str(root / "profiles" / "ops")})+ assert dashboard_procs._hermes_home_for_pid(1) == str(root / "profiles" / "ops")def test_unreadable_env_is_none_and_spared(self, monkeypatch):monkeypatch.setattr(dashboard_procs, "_pid_environ", lambda pid: None)