Skip to content

The agent that grows with you

fix(gateway): never authorize termination from a mutable sticky profile

hxy91819747ab06430ce
diff --git a/hermes_cli/dashboard_procs.py b/hermes_cli/dashboard_procs.py
index 3f8d9b24ab0..8eec8c31a64 100644
--- a/hermes_cli/dashboard_procs.py
+++ b/hermes_cli/dashboard_procs.py
@@ -163,9 +163,12 @@ def _pid_passwd_home(pid: int) -> str | None:
return None
-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.
+def _hermes_home_for_pid(pid: int, *, require_launch_identity: bool = False) -> str | None:
+ """Resolve a process home for discovery, or require pinned launch identity for termination.
+
+ By default, ``None`` means an unreadable environment (another user, hardened ``/proc``).
+ ``require_launch_identity`` also refuses missing argv, relative/missing home anchors and
+ any profile selection that would depend on today's sticky ``active_profile``.
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
@@ -176,14 +179,28 @@ def _hermes_home_for_pid(pid: int) -> str | None:
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).
"""
+ # Destructive callers require launch evidence; discovery may still use sticky selection.
env = _pid_environ(pid)
if env is None:
return None
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 []
+ argv = _dashboard_cmdline_for_pid(pid)
+ if require_launch_identity and argv is None:
+ # The dashboard argv reader has no Windows implementation; psutil does.
+ import psutil
+ argv = psutil.Process(pid).cmdline()
+ if require_launch_identity and not argv:
+ return None
+ argv = argv or []
env_home = env.get("HERMES_HOME", "").strip()
profile = _profile_flag_value(argv)
+ if require_launch_identity:
+ anchor = (env.get('LOCALAPPDATA') or env.get('USERPROFILE')) if sys.platform == 'win32' else env.get('HOME')
+ if env_home and not Path(env_home).is_absolute():
+ return None
+ if not env_home and (not anchor or not Path(anchor).is_absolute()):
+ return None
if profile is None and env_home and (
Path(env_home).parent.name == "profiles" or env.get("HERMES_UPDATE_POST_SWAP") == "1"
):
@@ -198,6 +215,8 @@ def _hermes_home_for_pid(pid: int) -> str | None:
fixed_identity = any(env.get(k) for k in ("HERMES_SUPERVISED_CHILD", "HERMES_S6_SUPERVISED_CHILD",
"HERMES_GATEWAY_EXTERNAL_SUPERVISOR")) or is_desktop_ssh_backend_argv(argv)
if profile is None and not fixed_identity:
+ if require_launch_identity:
+ return None # The sticky profile at launch is unknowable; never read today's value.
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)
diff --git a/hermes_cli/gateway_profile_lifecycle.py b/hermes_cli/gateway_profile_lifecycle.py
index 492239c1c1e..b4a0c960bdc 100644
--- a/hermes_cli/gateway_profile_lifecycle.py
+++ b/hermes_cli/gateway_profile_lifecycle.py
@@ -168,7 +168,7 @@ def owned_gateway_pid() -> int | None:
home = _get_process_hermes_home()
if owner is not None and owner.pid == pid and not _same_hermes_home(owner.home, home):
return None
- process_home = _hermes_home_for_pid(pid)
+ process_home = _hermes_home_for_pid(pid, require_launch_identity=True)
if process_home is None or not _same_hermes_home(process_home, home):
return None
# The runtime may have switched owners while the OS/control probes ran.
diff --git a/tests/hermes_cli/test_gateway.py b/tests/hermes_cli/test_gateway.py
index ae4890b66f1..0bf189f1e3b 100644
--- a/tests/hermes_cli/test_gateway.py
+++ b/tests/hermes_cli/test_gateway.py
@@ -561,7 +561,7 @@ class TestStopProfileGateway:
from gateway import host_attach
from hermes_cli import dashboard_procs
monkeypatch.setattr(host_attach, '_probe_host_gateway', lambda wait: None)
- monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid: str(gateway.get_hermes_home()))
+ monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid, **kw: str(gateway.get_hermes_home()))
@pytest.mark.platforms("windows")
def test_windows_stop_drains_marker_before_force_termination(self, monkeypatch):
"""Windows must let the marker watcher run before escalating (#112750)."""
diff --git a/tests/hermes_cli/test_gateway_multiplex_lifecycle.py b/tests/hermes_cli/test_gateway_multiplex_lifecycle.py
index 9377a9ed706..2c98fd404e3 100644
--- a/tests/hermes_cli/test_gateway_multiplex_lifecycle.py
+++ b/tests/hermes_cli/test_gateway_multiplex_lifecycle.py
@@ -334,7 +334,7 @@ def test_process_discovery_excludes_host_even_after_unserve(homes, monkeypatch,
owner = SimpleNamespace(pid=4242, home=root, profiles=('default', 'worker') if served else ('default',))
monkeypatch.setenv('HERMES_HOME', str(secondary))
monkeypatch.setattr(host_attach, '_probe_host_gateway', lambda wait: owner)
- monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid: str(root if pid == 4242 else secondary))
+ monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid, **kw: str(root if pid == 4242 else secondary))
monkeypatch.setattr(gw, '_get_service_pids', lambda **kw: [])
monkeypatch.setattr(gw, '_scan_gateway_pids', lambda *a, **kw: [])
monkeypatch.setattr(status, 'get_running_pid', lambda **kw: owner.pid)
@@ -359,7 +359,7 @@ def test_stop_never_owns_shared_pid_from_an_absent_or_old_probe(homes, monkeypat
monkeypatch.setattr(status, 'get_running_pid', lambda **kw: 4242)
monkeypatch.setattr(gw, '_get_service_pids', lambda **kw: [])
monkeypatch.setattr(gw, '_scan_gateway_pids', lambda *a, **kw: [])
- monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid: str(root))
+ monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid, **kw: str(root))
old = host_attach.HostGateway(4141, root, ('default', 'worker'))
current = host_attach.HostGateway(4242, root, ('default', 'worker'))
monkeypatch.setattr(host_attach, '_cached_probe', (time.monotonic(), old if probe == 'switched' else None))
@@ -384,7 +384,7 @@ def test_absent_host_requires_positive_process_ownership(homes, monkeypatch, pro
monkeypatch.setattr(status, 'get_running_pid', lambda **kw: 4242)
monkeypatch.setattr(host_attach, '_probe_host_gateway', lambda wait: None)
- def home(pid):
+ def home(pid, **kw):
if process_home == 'error':
raise PermissionError('process inaccessible')
return str(root) if process_home == 'foreign' else None
@@ -403,5 +403,53 @@ def test_owner_switch_during_probe_is_rejected(homes, monkeypatch):
pids = iter([4343, 4242])
monkeypatch.setattr(status, 'get_running_pid', lambda **kw: next(pids))
monkeypatch.setattr(host_attach, '_probe_host_gateway', lambda wait: None)
- monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid: str(secondary))
+ monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid, **kw: str(secondary))
assert gw._owned_gateway_pid() is None
+
+
[email protected]('rendezvous', ['absent', 'unreadable'])
+def test_sticky_profile_cannot_reassign_running_host(homes, monkeypatch, rendezvous):
+ from gateway import host_attach, status
+ from hermes_cli import dashboard_procs, gateway as gw, main_dashboard
+ root, secondary = homes
+ monkeypatch.setenv('HERMES_HOME', str(secondary))
+ # The process started at the root, before the user's later sticky switch.
+ monkeypatch.setattr(dashboard_procs, '_pid_environ', lambda pid: {'HERMES_HOME': str(root)})
+ monkeypatch.setattr(main_dashboard, '_dashboard_cmdline_for_pid', lambda pid: ['hermes', 'gateway', 'run'])
+ (root / 'active_profile').write_text('worker\n')
+ assert dashboard_procs._hermes_home_for_pid(4242) == str(secondary)
+
+ def probe(wait):
+ if rendezvous == 'unreadable':
+ raise PermissionError('rendezvous unreadable')
+
+ monkeypatch.setattr(host_attach, '_probe_host_gateway', probe)
+ monkeypatch.setattr(status, 'get_running_pid', lambda **kw: 4242)
+ monkeypatch.setattr(gw, '_reap_unsupervised_gateway_orphans', lambda: pytest.fail('unproven PID reached reaper'))
+ monkeypatch.setattr(gw.os, 'kill', lambda *a: pytest.fail('shared host was signalled'))
+ assert gw._owned_gateway_pid() is None
+ assert gw.stop_profile_gateway() is False
+
+
[email protected]('identity', ['explicit', 'profile_home', 'supervised', 'ambiguous'])
+def test_launch_identity_does_not_follow_sticky_selection(homes, monkeypatch, identity):
+ from gateway import host_attach, status
+ from hermes_cli import dashboard_procs, gateway as gw, main_dashboard
+ root, secondary = homes
+ (root / 'active_profile').write_text('other\n')
+ env = {'HERMES_HOME': str(root)}
+ argv = ['hermes', 'gateway', 'run']
+ target = secondary
+ if identity == 'explicit':
+ argv[1:1] = ['-p', 'worker']
+ elif identity == 'profile_home':
+ env['HERMES_HOME'] = str(secondary)
+ elif identity == 'supervised':
+ env['HERMES_GATEWAY_EXTERNAL_SUPERVISOR'] = '1'
+ target = root
+ monkeypatch.setenv('HERMES_HOME', str(target))
+ monkeypatch.setattr(dashboard_procs, '_pid_environ', lambda pid: env)
+ monkeypatch.setattr(main_dashboard, '_dashboard_cmdline_for_pid', lambda pid: argv)
+ monkeypatch.setattr(host_attach, '_probe_host_gateway', lambda wait: None)
+ monkeypatch.setattr(status, 'get_running_pid', lambda **kw: 4242)
+ assert gw._owned_gateway_pid() == (None if identity == 'ambiguous' else 4242)
diff --git a/tests/hermes_cli/test_gateway_scan_home_identity.py b/tests/hermes_cli/test_gateway_scan_home_identity.py
index a96f2fac239..7d8a3fdbd16 100644
--- a/tests/hermes_cli/test_gateway_scan_home_identity.py
+++ b/tests/hermes_cli/test_gateway_scan_home_identity.py
@@ -62,7 +62,7 @@ def test_scan_claims_a_bare_gateway_only_for_the_home_its_environment_names(tmp_
real_home_for_pid = dashboard_procs._hermes_home_for_pid
monkeypatch.setattr(
dashboard_procs, "_hermes_home_for_pid",
- lambda pid: real_home_for_pid(pid) if pid == proc.pid else None,
+ lambda pid, **kw: real_home_for_pid(pid, **kw) if pid == proc.pid else None,
)
try:
_wait_for(ready, proc)