The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): require fresh positive ownership before stopping a PID
hxy91819db3f202099c2
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 709ae094d15..04a152dee88 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -1984,16 +1984,7 @@ def stop_profile_gateway() -> bool:pid = _owned_gateway_pid()if pid is None:- return _reap_unsupervised_gateway_orphans()- host = _host_multiplexer_for_all_verb()- if host is not None and pid == host.pid and not _host_multiplexer_is_ours(host):- # Second blind spot of #141133: this profile's ``gateway.lock`` names the host multiplexer.- # When the control socket (or the host probe) does not answer, ``profile_lifecycle`` and- # ``_cmd_stop``'s own guard both miss it, and the SIGTERM below took every served profile- # offline. A pid that belongs to the live host is never this profile's gateway to stop.- print_error(f"PID {pid} is the host gateway serving this profile; stopping it would take "- f"every served profile offline. Stop the host instead: hermes gateway stop --all")- return False+ return False if get_running_pid() is not None else _reap_unsupervised_gateway_orphans()if is_windows():# Windows maps SIGTERM to TerminateProcess. The marker watcher is thediff --git a/hermes_cli/gateway_profile_lifecycle.py b/hermes_cli/gateway_profile_lifecycle.pyindex 207eb16bfd7..492239c1c1e 100644--- a/hermes_cli/gateway_profile_lifecycle.py+++ b/hermes_cli/gateway_profile_lifecycle.py@@ -1,10 +1,15 @@"""Named-profile lifecycle inside the host gateway, never a service-manager action."""from __future__ import annotations+import logging+from hermes_constants import get_hermes_home, get_default_hermes_rootfrom hermes_cli.profiles import parked_marker_path, profile_is_parked, profile_is_standalone, profiles_to_serve+logger = logging.getLogger(__name__)++def ack_confirmed(answer, key, name):return isinstance(answer, dict) and answer.get(key) == name and not answer.get("error")@@ -144,18 +149,31 @@ def print_parked_status() -> bool:def owned_gateway_pid() -> int | None:- """A gateway this profile may stop, excluding its shared host's liveness PID.+ """Return only a freshly confirmed process owned by this profile.- Runtime status for a satellite reports the host PID, including briefly after- unserve while status catches up. Check host ownership regardless of its roster.- A separate forced/standalone gateway still owns its normal process lifecycle.+ A missing (or cached missing) host is not ownership evidence. Refresh the host+ probe, then resolve the candidate's live process home independently of runtime+ status: a satellite's status legitimately carries another home's shared PID."""- from gateway.status import get_running_pid- from hermes_cli import gateway as gw+ from gateway import host_attach+ from gateway.status import get_running_pid, _get_process_hermes_home, _same_hermes_home+ from hermes_cli.dashboard_procs import _hermes_home_for_pid- pid = get_running_pid()- if pid is not None:- owner = gw._host_multiplexer_for_all_verb()- if owner is not None and owner.pid == pid and not gw._host_multiplexer_is_ours(owner):+ try:+ pid = get_running_pid()+ if pid is None:+ return None+ host_attach.invalidate_host_gateway_cache()+ owner = host_attach.host_gateway()+ home = _get_process_hermes_home()+ if owner is not None and owner.pid == pid and not _same_hermes_home(owner.home, home):return None- return pid+ process_home = _hermes_home_for_pid(pid)+ 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.+ return pid if get_running_pid() == pid else None+ except Exception:+ # An inaccessible rendezvous or process is never permission to kill it.+ logger.debug("Could not prove gateway process ownership", exc_info=True)+ return Nonediff --git a/tests/gateway/test_multiplex_hot_serve_live.py b/tests/gateway/test_multiplex_hot_serve_live.pyindex cb9724dcae6..8716e5636c6 100644--- a/tests/gateway/test_multiplex_hot_serve_live.py+++ b/tests/gateway/test_multiplex_hot_serve_live.py@@ -386,3 +386,29 @@ def test_stop_secondary_keeps_host_and_survives_host_restart(mux, peer):finally:peer.release.set()stop(proc, expect=None)+++def test_standalone_secondary_stops_without_stopping_host(mux):+ """Positive OS ownership still permits stopping a real independent gateway."""+ root, boot = mux['root'], mux['boot']+ config = {**mux['config'], 'gateway': {'standalone': True, 'multiplex_profiles': False}}+ (boot / 'config.yaml').write_text(json.dumps(config))+ host, desc = start_daemon(mux)+ child = None+ try:+ assert served_homes(desc) == {root.resolve()}+ standalone = {**mux, 'root': boot, 'env': {**mux['env'], 'HERMES_HOME': str(boot)},+ 'log': mux['log'].with_name('standalone.log')}+ child, _ = start_daemon(standalone)+ stopped = subprocess.run(+ [sys.executable, '-m', 'hermes_cli.main', '-p', 'boot', 'gateway', 'stop'],+ cwd=ROOT, env=mux['env'], capture_output=True, text=True, timeout=45, check=False)+ assert stopped.returncode == 0, (stopped.stdout, stopped.stderr)+ child.wait(timeout=30)+ assert host.poll() is None+ assert control(root, 'identify')['instance_id'] == desc['instance_id']+ assert not (boot / 'gateway.parked').exists()+ finally:+ if child is not None:+ stop(child, expect=None)+ stop(host, expect=None)diff --git a/tests/hermes_cli/test_gateway.py b/tests/hermes_cli/test_gateway.pyindex 7a3d9a35494..ae4890b66f1 100644--- a/tests/hermes_cli/test_gateway.py+++ b/tests/hermes_cli/test_gateway.py@@ -553,6 +553,15 @@ class TestRestartWaitsForApiServerPort:class TestStopProfileGateway:++ @pytest.fixture(autouse=True)+ def owned_process_home(self, monkeypatch):+ # These signal/reaping tests simulate an independently owned process.+ # Ambiguous/shared ownership is exercised by the lifecycle regressions.+ 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()))@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.pyindex 0b559d3094e..9377a9ed706 100644--- a/tests/hermes_cli/test_gateway_multiplex_lifecycle.py+++ b/tests/hermes_cli/test_gateway_multiplex_lifecycle.py@@ -328,12 +328,13 @@ def test_status_flags_a_gateway_this_hermes_cannot_attach(homes, monkeypatch, ca@pytest.mark.parametrize('served', [True, False])def test_process_discovery_excludes_host_even_after_unserve(homes, monkeypatch, served):- from gateway import status- from hermes_cli import gateway as gw+ from gateway import status, host_attach+ from hermes_cli import gateway as gw, dashboard_procsroot, secondary = homesowner = SimpleNamespace(pid=4242, home=root, profiles=('default', 'worker') if served else ('default',))monkeypatch.setenv('HERMES_HOME', str(secondary))- monkeypatch.setattr(gw, '_host_multiplexer_for_all_verb', lambda: owner)+ 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(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)@@ -346,3 +347,61 @@ def test_process_discovery_excludes_host_even_after_unserve(homes, monkeypatch,monkeypatch.setenv('HERMES_HOME', str(root))monkeypatch.setattr(status, 'get_running_pid', lambda **kw: owner.pid)assert gw.find_gateway_pids() == [owner.pid]++[email protected]('probe', ['cached_absent', 'failure', 'switched'])+def test_stop_never_owns_shared_pid_from_an_absent_or_old_probe(homes, monkeypatch, probe):+ import time+ from gateway import host_attach, status+ from hermes_cli import gateway as gw, dashboard_procs+ root, secondary = homes+ monkeypatch.setenv('HERMES_HOME', str(secondary))+ 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))+ 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))++ def fresh(_wait):+ if probe == 'failure':+ raise OSError('rendezvous unavailable')+ return current++ monkeypatch.setattr(host_attach, '_probe_host_gateway', fresh)+ monkeypatch.setattr(gw.os, 'kill', lambda *a: pytest.fail('shared host was signalled'))+ assert gw.find_gateway_pids() == []+ assert gw.stop_profile_gateway() is False++[email protected]('process_home', [None, 'foreign', 'error'])+def test_absent_host_requires_positive_process_ownership(homes, monkeypatch, process_home):+ from gateway import host_attach, status+ from hermes_cli import gateway as gw, dashboard_procs+ root, secondary = homes+ monkeypatch.setenv('HERMES_HOME', str(secondary))+ monkeypatch.setattr(status, 'get_running_pid', lambda **kw: 4242)+ monkeypatch.setattr(host_attach, '_probe_host_gateway', lambda wait: None)++ def home(pid):+ if process_home == 'error':+ raise PermissionError('process inaccessible')+ return str(root) if process_home == 'foreign' else None++ monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', home)+ monkeypatch.setattr(gw, '_reap_unsupervised_gateway_orphans', lambda: pytest.fail('unproven PID fell through to reaper'))+ monkeypatch.setattr(gw.os, 'kill', lambda *a: pytest.fail('unproven PID was signalled'))+ assert gw.stop_profile_gateway() is False+++def test_owner_switch_during_probe_is_rejected(homes, monkeypatch):+ from gateway import host_attach, status+ from hermes_cli import gateway as gw, dashboard_procs+ _, secondary = homes+ monkeypatch.setenv('HERMES_HOME', str(secondary))+ 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))+ assert gw._owned_gateway_pid() is None