Skip to content

The agent that grows with you

fix(gateway): park a served profile without stopping the host multiplexer

686f6c61de7479fbf02a
diff --git a/hermes_cli/gateway_profile_lifecycle.py b/hermes_cli/gateway_profile_lifecycle.py
index 661cc6b24aa..11e004df06f 100644
--- a/hermes_cli/gateway_profile_lifecycle.py
+++ b/hermes_cli/gateway_profile_lifecycle.py
@@ -64,8 +64,16 @@ 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.
- if gw.find_gateway_pids():
+ # 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]:
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_multiplex_lifecycle.py b/tests/hermes_cli/test_gateway_multiplex_lifecycle.py
index b80b31decb5..78abf6f90eb 100644
--- a/tests/hermes_cli/test_gateway_multiplex_lifecycle.py
+++ b/tests/hermes_cli/test_gateway_multiplex_lifecycle.py
@@ -143,6 +143,54 @@ def test_force_restart_keeps_parked_profile_gateway_ownership(homes, monkeypatch
assert profile_lifecycle('restart', SimpleNamespace()) is False
+def test_stop_parks_a_served_profile_instead_of_stopping_the_host(homes, monkeypatch, capsys):
+ """The host publishes its own pid into a served profile's runtime record (#141133)."""
+ from hermes_cli import gateway as gw
+ from gateway import control_socket
+ root, secondary = homes
+ marker = secondary / 'gateway.parked'
+ host_pid = 4242
+ owner = 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, '_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)
+ monkeypatch.setattr(gw, '_served_by_another_host_gateway', lambda *a: owner)
+ monkeypatch.setattr(gw, 'named_profile_served_by_running_multiplexer', lambda *a: True)
+ calls = []
+
+ def unserve(home, name):
+ assert home == root and name == 'worker' and marker.exists()
+ calls.append('unserve')
+ return {'unserved': name, 'served_profiles': ['default']}
+
+ def host_stop(*args, **kwargs):
+ raise AssertionError('stop killed the host gateway pid')
+
+ monkeypatch.setattr(control_socket, 'request_unserve_profile', unserve, raising=False)
+ monkeypatch.setattr(gw, 'stop_profile_gateway', host_stop)
+ gw._cmd_stop(SimpleNamespace())
+ assert calls == ['unserve']
+ assert marker.exists()
+ assert "parked; its bots and cron are stopped" in capsys.readouterr().out
+
+
+def test_separate_gateway_beside_the_host_keeps_its_own_lifecycle(homes, monkeypatch):
+ from hermes_cli import gateway as gw
+ from hermes_cli.gateway_profile_lifecycle import profile_lifecycle
+ root, secondary = homes
+ host_pid = 4242
+ owner = 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, '_current_profile_name', lambda: 'worker')
+ monkeypatch.setattr(gw, 'find_gateway_pids', lambda **kw: [host_pid, 5151])
+ monkeypatch.setattr(gw, 'host_multiplexer_serving', lambda *a: owner)
+ assert profile_lifecycle('stop', SimpleNamespace()) is False
+
+
def test_parked_status_and_topology_keep_roster(homes, monkeypatch, capsys):
from hermes_cli import gateway as gw, profiles
from hermes_cli.web_server_gateway import _collect_profile_gateway_topology