The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): never SIGTERM the pid of the host that serves this profile
686f6c6111ff2c978cad
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 6b2eb9b2670..96fd09809fe 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -1965,16 +1965,7 @@ def _force_kill_survivors(survivors, *, kill=None) -> None:kill(pid, getattr(signal, "SIGKILL", signal.SIGTERM))-def _mark_planned_stop(pid: int | None = None) -> None:- """Best-effort planned-stop marker for ``pid`` (default: the recorded gateway PID)."""- try:- from gateway.status import get_running_pid, write_planned_stop_marker- if pid is None:- pid = get_running_pid(cleanup_stale=False)- if pid is not None:- write_planned_stop_marker(pid)- except Exception:- pass+from hermes_cli.gateway_profile_lifecycle import mark_planned_stop as _mark_planned_stopdef stop_profile_gateway() -> bool:@@ -1994,6 +1985,15 @@ def stop_profile_gateway() -> bool:pid = get_running_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 Falseif 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 11e004df06f..a6a60928b8b 100644--- a/hermes_cli/gateway_profile_lifecycle.py+++ b/hermes_cli/gateway_profile_lifecycle.py@@ -15,6 +15,18 @@ def ack_failure(answer):return "host control socket did not answer"+def mark_planned_stop(pid: int | None = None) -> None:+ """Best-effort planned-stop marker for ``pid`` (default: the recorded gateway PID)."""+ try:+ from gateway.status import get_running_pid, write_planned_stop_marker+ if pid is None:+ pid = get_running_pid(cleanup_stale=False)+ if pid is not None:+ write_planned_stop_marker(pid)+ except Exception:+ pass++def host_scope_for_all_verb(owner):"""Home a ``--all`` verb acts as: the live host owner's, else the default root.diff --git a/tests/hermes_cli/test_gateway_host_stop_refusal.py b/tests/hermes_cli/test_gateway_host_stop_refusal.pynew file mode 100644index 00000000000..2c1b67e99c1--- /dev/null+++ b/tests/hermes_cli/test_gateway_host_stop_refusal.py@@ -0,0 +1,68 @@+"""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.+"""+from types import SimpleNamespace++import pytest+++def homes(tmp_path, monkeypatch):+ from hermes_cli import profiles+ root = tmp_path / '.hermes'+ secondary = root / 'profiles' / 'worker'+ secondary.mkdir(parents=True)+ (secondary / 'config.yaml').write_text('model: {default: worker-model}\n')+ (root / 'config.yaml').write_text('gateway: {multiplex_profiles: true}\n')+ monkeypatch.setattr('pathlib.Path.home', lambda: tmp_path)+ monkeypatch.setenv('HERMES_HOME', str(root))+ monkeypatch.setattr(profiles, 'get_active_profile_name', lambda: 'default')+ 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 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)+ 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.os, 'kill', lambda *a: pytest.fail('stop_profile_gateway SIGTERMed the host pid'))+ monkeypatch.setattr('gateway.status.get_running_pid', lambda **kw: host.pid)++ assert gw.stop_profile_gateway() is False+ out = capsys.readouterr().out+ assert 'host gateway serving this profile' in out+ assert 'gateway stop --all' in out+++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)+ 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(gw, '_reap_unsupervised_gateway_orphans', lambda **kw: [])++ assert gw.stop_profile_gateway() is True+ assert killed == [host.pid]