Skip to content

The agent that grows with you

fix(gateway): every "separate gateway?" check under -p drops the serving host's pid

Teknium4a99d8e2ce6d
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 96fd09809fe..7ec682d9966 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -5311,12 +5311,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
+ from hermes_cli.gateway_profile_lifecycle import host_scope_for_all_verb, profile_lifecycle, separate_gateway_pids
if profile_lifecycle("stop", args):
return
stop_all = getattr(args, "all", False)
system = getattr(args, "system", False)
- if not stop_all and not find_gateway_pids() and (
+ if not stop_all and not separate_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 a6a60928b8b..bfb0499882b 100644
--- a/hermes_cli/gateway_profile_lifecycle.py
+++ b/hermes_cli/gateway_profile_lifecycle.py
@@ -27,6 +27,22 @@ 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.
@@ -53,7 +69,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 gw.find_gateway_pids():
+ if command == "restart" and profile_is_parked(home) and not separate_gateway_pids(name):
# `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).
@@ -76,16 +92,8 @@ def profile_lifecycle(command: str, args) -> bool:
print("The host retries on its next rescan (within 30s). Check gateway status.")
return True
- # A separate --force gateway still owns its normal process/service lifecycle — but the host
- # multiplexer is not one of those. While it serves this profile it publishes its OWN pid into
- # the profile's runtime record, so ``find_gateway_pids()`` reported the shared host as "this
- # profile's gateway", the lifecycle stood down, and ``_cmd_stop`` fell through to
- # ``stop_profile_gateway()``: ``hermes -p <name> gateway stop`` took every served profile
- # offline instead of parking one (#141133). Exclude that pid; any other pid is still a
- # separate gateway and keeps its own lifecycle.
- host = gw.host_multiplexer_serving(name)
- host_pid = getattr(host, "pid", None)
- if [pid for pid in gw.find_gateway_pids() if pid != host_pid]:
+ # A separate --force gateway still owns its normal process/service lifecycle.
+ if separate_gateway_pids(name):
return False
owner = gw._served_by_another_host_gateway()
if owner is None and not gw.named_profile_served_by_running_multiplexer():
diff --git a/tests/hermes_cli/test_gateway_host_stop_refusal.py b/tests/hermes_cli/test_gateway_host_stop_refusal.py
index 2c1b67e99c1..10d5d27846f 100644
--- a/tests/hermes_cli/test_gateway_host_stop_refusal.py
+++ b/tests/hermes_cli/test_gateway_host_stop_refusal.py
@@ -66,3 +66,26 @@ def test_stop_from_the_hosts_own_home_still_stops_it(homes, monkeypatch):
assert gw.stop_profile_gateway() is True
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
+ root, secondary = homes
+ host = _host_owner(root)
+ 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'))
+
+ with pytest.raises(SystemExit) as exc:
+ 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