Skip to content

The agent that grows with you

fix(gateway): owned_gateway_pid is the one ownership gate; sticky --force stays stoppable

Teknium20eef53819af
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index b477796e61c..99fa990b77c 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -5302,13 +5302,12 @@ def _cmd_start(args):
def _cmd_stop(args):
_refuse_from_inside_gateway("stop", "restart loops")
- from hermes_cli.gateway_profile_lifecycle import (
- host_scope_for_all_verb, profile_lifecycle, report_profile_stop_failure, separate_gateway_pids)
+ from hermes_cli.gateway_profile_lifecycle import host_scope_for_all_verb, profile_lifecycle, report_profile_stop_failure
if profile_lifecycle("stop", args):
return
stop_all = getattr(args, "all", False)
system = getattr(args, "system", False)
- if not stop_all and not separate_gateway_pids() and (
+ if not stop_all and not find_gateway_pids() and (
_served_by_another_host_gateway() or named_profile_served_by_running_multiplexer()):
# The launch/default-profile lifecycle still names the whole host.
owner = _served_by_another_host_gateway()
diff --git a/hermes_cli/gateway_profile_lifecycle.py b/hermes_cli/gateway_profile_lifecycle.py
index 3d25e47d7df..ac61b56edd3 100644
--- a/hermes_cli/gateway_profile_lifecycle.py
+++ b/hermes_cli/gateway_profile_lifecycle.py
@@ -32,22 +32,6 @@ def mark_planned_stop(pid: int | None = None) -> None:
pass
-def separate_gateway_pids(name=None):
- """This profile's gateway pids, minus the host multiplexer that serves it.
-
- While the host serves a profile it publishes its OWN pid into that profile's runtime record
- (``profiles/<name>/gateway.lock``), so ``find_gateway_pids()`` under ``-p <name>`` reports the
- shared host as "this profile's gateway". Every "is there a separate --force gateway?" check read
- it that way, stood down, and ``gateway stop`` fell through to SIGTERM the host: every served
- profile went offline instead of one being parked (#141133). Any other pid is still a separate
- gateway and keeps its own lifecycle.
- """
- from hermes_cli import gateway as gw
- host = gw.host_multiplexer_serving(name)
- host_pid = getattr(host, "pid", None)
- return [pid for pid in gw.find_gateway_pids() if pid != host_pid]
-
-
def host_scope_for_all_verb(owner):
"""Home a ``--all`` verb acts as: the live host owner's, else the default root.
@@ -74,7 +58,7 @@ def profile_lifecycle(command: str, args) -> bool:
# verbs keep addressing its own gateway process even while a stale host record lists it.
return False
marker = parked_marker_path(home)
- if command == "restart" and profile_is_parked(home) and not separate_gateway_pids(name):
+ if command == "restart" and profile_is_parked(home) and not gw.find_gateway_pids():
# `stop` parked the profile AND dropped it from the host's served set, so the unserve/serve
# path below finds no host serving it and would fall through to a standalone restart.
# Unpark like `start` does; a live --force gateway keeps its own restart (pids non-empty).
@@ -98,7 +82,7 @@ def profile_lifecycle(command: str, args) -> bool:
return True
# A separate --force gateway still owns its normal process/service lifecycle.
- if separate_gateway_pids(name):
+ if gw.find_gateway_pids():
return False
owner = gw._served_by_another_host_gateway()
if owner is None and not gw.named_profile_served_by_running_multiplexer():
@@ -149,32 +133,17 @@ def print_parked_status() -> bool:
def owned_gateway_pid() -> int | None:
- """Return only a freshly confirmed process owned by this profile.
+ """This profile's own gateway pid, only on positive, fresh ownership evidence; else None.
- A missing (or cached missing) host is not ownership evidence. Accept a fresh
- host identity matching PID/home, otherwise require pinned process launch evidence:
- a satellite's runtime status legitimately carries another home's shared PID.
+ A served profile's runtime record carries the host multiplexer's pid (#141133), so the recorded
+ pid alone never authorizes a stop. Every ``find_gateway_pids()`` caller (stop, uninstall's kill,
+ ``service_manager.is_running``, cron readiness, Windows start polls) reads it through here.
"""
- 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
+ from gateway.status import get_running_pid, _get_process_hermes_home
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:
- if not _same_hermes_home(owner.home, home):
- return None
- # host_gateway refreshed liveness/incarnation and matched the control
- # socket's PID/home. A bare foreground launch needs no argv profile.
- if owner.served_known:
- return pid if get_running_pid() == pid else None
- process_home = _hermes_home_for_pid(pid, require_launch_identity=True)
- if process_home is None or not _same_hermes_home(process_home, home):
+ if pid is None or not _pid_owned_by_home(pid, _get_process_hermes_home()):
return None
# The runtime may have switched owners while the OS/control probes ran.
return pid if get_running_pid() == pid else None
@@ -184,6 +153,50 @@ def owned_gateway_pid() -> int | None:
return None
+def _pid_owned_by_home(pid: int, home) -> bool:
+ """Ownership ladder, cheapest proof first; the control socket is dialled only for the host's pid.
+
+ ``gateway status`` asks once per profile and every served profile's record names the host, so the
+ host record (a file read plus an incarnation check) settles those without a dial.
+ """
+ from gateway import host_attach, host_rendezvous as hr
+ from gateway.status import _same_hermes_home
+ from hermes_cli.dashboard_procs import _hermes_home_for_pid
+
+ record = hr.read_record(hr.ROLE_GATEWAY)
+ is_host = record is not None and record.pid == pid and hr.record_names_live_owner(record)
+ if is_host and not _same_hermes_home(host_attach.record_home(record), home):
+ return False # the live host, launched from another home
+ if _launch_record_owns(pid, home):
+ return True
+ if is_host:
+ host_attach.invalidate_host_gateway_cache()
+ owner = host_attach.host_gateway()
+ # A verified identity answer from the host's own home needs no argv profile (bare launch).
+ if owner is not None and owner.pid == pid and owner.served_known:
+ return _same_hermes_home(owner.home, home)
+ process_home = _hermes_home_for_pid(pid, require_launch_identity=True)
+ return process_home is not None and _same_hermes_home(process_home, home)
+
+
+def _launch_record_owns(pid: int, home) -> bool:
+ """``<home>/gateway.pid`` names *pid*'s exact incarnation as launched from *home*.
+
+ A gateway writes that file once, about its own launch home (the host never writes one into a
+ served profile), so it pins the profile resolved at launch: a ``gateway run --force`` that relied
+ on the sticky ``active_profile`` stays stoppable after the sticky choice changes, which argv and
+ environment alone cannot prove.
+ """
+ from gateway.status import (
+ _GATEWAY_KIND, _get_process_start_time, _pid_from_record, _read_pid_record, _same_hermes_home)
+
+ record = _read_pid_record() or {}
+ started, recorded_home = record.get("start_time"), record.get("hermes_home")
+ return (record.get("kind") == _GATEWAY_KIND and _pid_from_record(record) == pid
+ and started is not None and started == _get_process_start_time(pid)
+ and isinstance(recorded_home, str) and _same_hermes_home(recorded_home, home))
+
+
def report_profile_stop_failure() -> None:
"""Do not report absence when a live PID was refused or the probe failed."""
from gateway.status import get_running_pid
@@ -199,4 +212,19 @@ def report_profile_stop_failure() -> None:
return
gw.print_error("Gateway stop was not confirmed; a gateway may still be running. "
"Check gateway status and process ownership.")
+ _print_host_hint()
raise SystemExit(1)
+
+
+def _print_host_hint() -> None:
+ """Name the refused pid when it is the host gateway serving this profile (#141133)."""
+ from gateway import host_rendezvous as hr
+ from gateway.status import get_running_pid
+
+ try:
+ pid, record = get_running_pid(), hr.read_record(hr.ROLE_GATEWAY)
+ except Exception:
+ return
+ if pid is not None and record is not None and record.pid == pid:
+ print(f" PID {pid} is the host gateway serving this profile; stopping it would take every "
+ f"served profile offline. Stop the host instead: hermes gateway stop --all")
diff --git a/tests/hermes_cli/test_gateway_host_stop_refusal.py b/tests/hermes_cli/test_gateway_host_stop_refusal.py
index 10d5d27846f..ab713350370 100644
--- a/tests/hermes_cli/test_gateway_host_stop_refusal.py
+++ b/tests/hermes_cli/test_gateway_host_stop_refusal.py
@@ -1,15 +1,21 @@
"""The host multiplexer is never the pid a served profile's ``gateway stop`` may SIGTERM (#141133).
-First blind spot: ``profile_lifecycle`` saw the host pid in the profile's runtime record and stood
-down. Second one, covered here: the host probe or the control socket does not answer, so
-``profile_lifecycle`` falls through and ``_cmd_stop``'s own guard does not fire either (it only
-fires while ``find_gateway_pids()`` is empty, and that record names the host). ``stop_profile_gateway``
-is the last place before the kill, so the refusal lives on the pid it is about to signal.
+While the host serves a profile it publishes its own pid into that profile's runtime record, so
+``get_running_pid()`` under ``-p <name>`` names the host. ``owned_gateway_pid`` (the single gate,
+read by ``find_gateway_pids`` and ``stop_profile_gateway``) answers only on positive ownership
+evidence; these cases drive it through the real host-record and launch-record readers.
"""
+import json
+import os
from types import SimpleNamespace
import pytest
+from gateway import host_attach, host_rendezvous as hr, status
+from hermes_cli import dashboard_procs, gateway as gw
+
+HOST_PID = 4242
+
@pytest.fixture
def homes(tmp_path, monkeypatch):
@@ -22,27 +28,44 @@ def homes(tmp_path, monkeypatch):
monkeypatch.setattr('pathlib.Path.home', lambda: tmp_path)
monkeypatch.setenv('HERMES_HOME', str(root))
monkeypatch.setattr(profiles, 'get_active_profile_name', lambda: 'default')
+ monkeypatch.setattr(gw, '_get_service_pids', lambda **kw: [])
+ monkeypatch.setattr(gw, '_scan_gateway_pids', lambda *a, **kw: [])
return root, secondary
-def _host_owner(root, pid=4242):
- return SimpleNamespace(home=root, pid=pid, profile_label='default',
- profiles=('default', 'worker'), describe=lambda: 'test host')
+def _live_host(monkeypatch, home, *, socket_answers=False):
+ """A live host record launched from *home*; its control socket answers only when asked to."""
+ record = SimpleNamespace(pid=HOST_PID, home=str(home), role=hr.ROLE_GATEWAY)
+ monkeypatch.setattr(hr, 'read_record', lambda role, **kw: record)
+ monkeypatch.setattr(hr, 'record_names_live_owner', lambda rec: True)
+ dials = []
+
+ def probe(wait):
+ dials.append(wait)
+ return host_attach.HostGateway(HOST_PID, home, ('default', 'worker'), served_known=socket_answers)
+
+ monkeypatch.setattr(host_attach, '_probe_host_gateway', probe)
+ monkeypatch.setattr(status, 'get_running_pid', lambda **kw: HOST_PID)
+ return dials
def test_stop_refuses_the_serving_host_when_the_socket_does_not_answer(homes, monkeypatch, capsys):
- from hermes_cli import gateway as gw
root, secondary = homes
- host = _host_owner(root)
+ _live_host(monkeypatch, root)
monkeypatch.setenv('HERMES_HOME', str(secondary))
- monkeypatch.setattr(gw, '_host_multiplexer_for_all_verb', lambda: host)
- monkeypatch.setattr(gw, '_host_multiplexer_is_ours', lambda owner: False)
- monkeypatch.setattr(gw, '_mark_planned_stop',
- lambda *a: pytest.fail('planned-stop marker written for the host pid'))
+ monkeypatch.setattr(gw, '_mark_planned_stop', lambda *a: pytest.fail('planned-stop marker written for the host pid'))
monkeypatch.setattr(gw.os, 'kill', lambda *a: pytest.fail('stop_profile_gateway SIGTERMed the host pid'))
- monkeypatch.setattr('gateway.status.get_running_pid', lambda **kw: host.pid)
+ monkeypatch.setattr(gw, '_reap_unsupervised_gateway_orphans', lambda **kw: pytest.fail('host pid reached the reaper'))
+ assert gw.find_gateway_pids() == []
assert gw.stop_profile_gateway() is False
+ # uninstall's per-profile kill reads the same gate.
+ monkeypatch.setattr(gw, 'terminate_pid', lambda *a, **kw: pytest.fail('uninstall kill reached the host pid'))
+ assert gw.kill_gateway_processes() == 0
+ from hermes_cli.gateway_profile_lifecycle import report_profile_stop_failure
+ with pytest.raises(SystemExit) as exc:
+ report_profile_stop_failure()
+ assert exc.value.code == 1
out = capsys.readouterr().out
assert 'host gateway serving this profile' in out
assert 'gateway stop --all' in out
@@ -50,37 +73,29 @@ def test_stop_refuses_the_serving_host_when_the_socket_does_not_answer(homes, mo
def test_stop_from_the_hosts_own_home_still_stops_it(homes, monkeypatch):
"""The owner case is unchanged: the home that launched the host stops its own gateway."""
- from hermes_cli import gateway as gw
- root, secondary = homes
- host = _host_owner(root)
+ root, _secondary = homes
+ _live_host(monkeypatch, root, socket_answers=True)
killed = []
- monkeypatch.setenv('HERMES_HOME', str(secondary))
- monkeypatch.setattr(gw, '_host_multiplexer_for_all_verb', lambda: host)
- monkeypatch.setattr(gw, '_host_multiplexer_is_ours', lambda owner: True)
monkeypatch.setattr(gw, '_mark_planned_stop', lambda *a: None)
monkeypatch.setattr(gw.os, 'kill', lambda pid, sig: killed.append(pid))
- monkeypatch.setattr('gateway.status.get_running_pid', lambda **kw: host.pid)
- monkeypatch.setattr('gateway.status.remove_pid_file', lambda: None)
- monkeypatch.setattr('gateway.status._pid_exists', lambda pid: False)
+ monkeypatch.setattr(status, 'remove_pid_file', lambda: None)
+ monkeypatch.setattr(status, '_pid_exists', lambda pid: False)
monkeypatch.setattr(gw, '_reap_unsupervised_gateway_orphans', lambda **kw: [])
assert gw.stop_profile_gateway() is True
- assert killed == [host.pid]
+ assert killed == [HOST_PID]
def test_cmd_stop_guard_names_the_host_when_the_lock_carries_its_pid(homes, monkeypatch, capsys):
- """``_cmd_stop``'s own "the host serves this profile" refusal fires on the host pid too.
-
- It only fired while ``find_gateway_pids()`` was empty, and a served profile's runtime record names
- the host, so a stop that bypasses the park path (``--force``) fell through to the kill."""
- from hermes_cli import gateway as gw
+ """``--force`` skips the park path; ``_cmd_stop``'s "the host serves this profile" refusal still
+ fires, because ``find_gateway_pids()`` no longer reports the host pid as this profile's gateway."""
root, secondary = homes
- host = _host_owner(root)
+ _live_host(monkeypatch, root)
+ host = SimpleNamespace(home=root, pid=HOST_PID, profile_label='default',
+ profiles=('default', 'worker'), describe=lambda: 'test host')
monkeypatch.setenv('HERMES_HOME', str(secondary))
monkeypatch.setattr(gw, '_refuse_from_inside_gateway', lambda *a: None)
monkeypatch.setattr(gw, '_current_profile_name', lambda: 'worker')
- monkeypatch.setattr(gw, 'find_gateway_pids', lambda **kw: [host.pid])
- monkeypatch.setattr(gw, 'host_multiplexer_serving', lambda *a: host)
monkeypatch.setattr(gw, '_served_by_another_host_gateway', lambda *a: host)
monkeypatch.setattr(gw, 'stop_profile_gateway', lambda: pytest.fail('stop reached the host pid'))
monkeypatch.setattr(gw, '_stop_installed_service', lambda *a: pytest.fail('stop reached the service'))
@@ -89,3 +104,46 @@ def test_cmd_stop_guard_names_the_host_when_the_lock_carries_its_pid(homes, monk
gw._cmd_stop(SimpleNamespace(force=True))
assert exc.value.code == gw.GATEWAY_FATAL_CONFIG_EXIT_CODE
assert "The host gateway serves profile 'worker'" in capsys.readouterr().out
+
+
+def test_sticky_profile_force_gateway_stays_stoppable(homes, monkeypatch):
+ """A ``gateway run --force`` launched without ``-p`` through the sticky profile carries no launch
+ identity in argv or env; its own ``gateway.pid`` (pid, start time, home) is the proof."""
+ import subprocess
+ import sys
+ root, secondary = homes
+ child = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(60)'])
+ try:
+ pid = child.pid
+ (root / 'active_profile').write_text('default\n') # the sticky choice moved on after launch
+ (secondary / 'gateway.pid').write_text(json.dumps({
+ 'pid': pid, 'kind': status._GATEWAY_KIND, 'argv': ['hermes', 'gateway', 'run', '--force'],
+ 'start_time': status._get_process_start_time(pid), 'hermes_home': str(secondary)}))
+ monkeypatch.setenv('HERMES_HOME', str(secondary))
+ monkeypatch.setattr(hr, 'read_record', lambda role, **kw: None)
+ monkeypatch.setattr(dashboard_procs, '_pid_environ',
+ lambda p: {'HERMES_HOME': str(root), 'HOME': str(root.parent)})
+ monkeypatch.setattr(status, 'get_running_pid', lambda **kw: pid)
+ assert dashboard_procs._hermes_home_for_pid(pid, require_launch_identity=True) is None
+ assert gw._owned_gateway_pid() == pid
+ assert gw.find_gateway_pids() == [pid]
+ # A recycled pid (another start time) is not that launch.
+ monkeypatch.setattr(status, '_get_process_start_time', lambda p: -1)
+ assert gw._owned_gateway_pid() is None
+ finally:
+ child.kill()
+ child.wait(timeout=10)
+
+
+def test_only_the_hosts_pid_dials_its_control_socket(homes, monkeypatch):
+ """``gateway status`` across N profiles calls ``find_gateway_pids`` per profile: a pid that is not
+ the live host's never dials, and the host's pid from another home is refused on the record alone."""
+ root, secondary = homes
+ dials = _live_host(monkeypatch, root, socket_answers=True)
+ monkeypatch.setenv('HERMES_HOME', str(secondary))
+ monkeypatch.setattr(status, 'get_running_pid', lambda **kw: 5151)
+ monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid, **kw: str(secondary))
+ assert gw.find_gateway_pids() == [5151]
+ monkeypatch.setattr(status, 'get_running_pid', lambda **kw: HOST_PID)
+ assert gw.find_gateway_pids() == []
+ assert dials == []
diff --git a/tests/hermes_cli/test_gateway_multiplex_lifecycle.py b/tests/hermes_cli/test_gateway_multiplex_lifecycle.py
index bada54fcad7..bfa95ac03c5 100644
--- a/tests/hermes_cli/test_gateway_multiplex_lifecycle.py
+++ b/tests/hermes_cli/test_gateway_multiplex_lifecycle.py
@@ -152,11 +152,16 @@ def test_stop_parks_a_served_profile_instead_of_stopping_the_host(homes, monkeyp
host_pid = 4242
owner = SimpleNamespace(home=root, pid=host_pid, profile_label='default',
profiles=('default', 'worker'), describe=lambda: 'test host')
+ from gateway import host_rendezvous as hr, status
monkeypatch.setenv('HERMES_HOME', str(secondary))
monkeypatch.setattr(gw, '_current_profile_name', lambda: 'worker')
monkeypatch.setattr(gw, '_refuse_from_inside_gateway', lambda *a: None)
- monkeypatch.setattr(gw, 'find_gateway_pids', lambda **kw: [host_pid])
- monkeypatch.setattr(gw, 'host_multiplexer_serving', lambda *a: owner)
+ # The real find_gateway_pids: the profile's record names the live host, launched from root.
+ monkeypatch.setattr(status, 'get_running_pid', lambda **kw: host_pid)
+ monkeypatch.setattr(hr, 'read_record', lambda role, **kw: SimpleNamespace(pid=host_pid, home=str(root)))
+ monkeypatch.setattr(hr, 'record_names_live_owner', lambda rec: True)
+ monkeypatch.setattr(gw, '_get_service_pids', lambda **kw: [])
+ monkeypatch.setattr(gw, '_scan_gateway_pids', lambda *a, **kw: [])
monkeypatch.setattr(gw, '_served_by_another_host_gateway', lambda *a: owner)
monkeypatch.setattr(gw, 'named_profile_served_by_running_multiplexer', lambda *a: True)
calls = []
@@ -492,8 +497,11 @@ def test_same_home_record_requires_identity_and_stable_pid(homes, monkeypatch, v
from hermes_cli import gateway as gw, dashboard_procs
root, _ = homes
monkeypatch.setenv('HERMES_HOME', str(root))
+ from gateway import host_rendezvous as hr
owner = host_attach.HostGateway(4242, root, ('default',), served_known=verified)
monkeypatch.setattr(host_attach, '_probe_host_gateway', lambda wait: owner)
+ monkeypatch.setattr(hr, 'read_record', lambda role, **kw: SimpleNamespace(pid=4242, home=str(root)))
+ monkeypatch.setattr(hr, 'record_names_live_owner', lambda rec: True)
monkeypatch.setattr(dashboard_procs, '_hermes_home_for_pid', lambda pid, **kw: None)
pids = iter([4242, 4343 if changed else 4242])
monkeypatch.setattr(status, 'get_running_pid', lambda: next(pids))