The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): inline service-owner probe and scope the restart hint
kshitijk4poord9be0e350230
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 2e064bbaf90..d238e1508a2 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -5499,34 +5499,29 @@ def _restart_all_as_host(owner, system: bool) -> None:run_gateway(verbose=0, replace=True)-def _service_unit_owning_gateway(pid: int | None) -> str | None:- """systemd unit supervising gateway ``pid`` under ANY unit name, or None.-- Canonical-unit checks miss pre-convention installs (``hermes.service``), so the restart- fallback SIGKILLed a service-managed gateway and spawned an unsupervised orphan in the- caller's cgroup while the unit flapped against the stolen lock (#126474). Reuses the- dashboard's MainPID-verified lookup: a bare ``.service`` cgroup alone (the caller itself- started under some unit) never proves ownership.+def _refuse_restart_of_service_managed_gateway(pid: int | None) -> None:+ """Refuse the manual restart fallback when systemd owns the gateway under ANY unit name.++ Canonical-unit checks miss pre-convention installs (``hermes.service``), so the fallback's+ stop + in-process ``run_gateway`` SIGKILLed a live service-managed gateway and spawned an+ unsupervised orphan in the caller's cgroup while the unit flapped against the stolen lock+ (#126474). Reuses the dashboard's MainPID-verified lookup: a bare ``.service`` cgroup alone+ (the caller itself started under some unit) never proves ownership."""if not pid or pid <= 1:- return None+ returnfrom hermes_cli import main_dashboard as _dash- return _dash._get_systemd_service_for_pid(pid)--def _refuse_restart_of_service_managed_gateway(pid: int | None) -> None:- """Refuse the manual restart fallback when a supervisor owns the gateway (any unit name).-- The fallback's stop + in-process ``run_gateway`` would kill a live service-managed gateway- and stamp the CLI's PID into gateway.pid, wedging every supervisor respawn with "already- running". Restart through the unit instead — never from here.- """- unit = _service_unit_owning_gateway(pid)+ unit = _dash._get_systemd_service_for_pid(pid)if unit is None:return- from hermes_cli import main_dashboard as _dashscope = _dash._extract_scope_from_cgroup(_dash._get_pid_cgroup_path(pid) or "")- cmd = f"systemctl --user restart {unit}" if scope == "user" else f"sudo systemctl restart {unit}"+ if scope == "user":+ cmds = [f"systemctl --user restart {unit}"]+ elif scope == "system":+ cmds = [f"sudo systemctl restart {unit}"]+ else:+ cmds = [f"systemctl --user restart {unit}", f"sudo systemctl restart {unit} (system unit)"]_print_lines("",f"✗ Gateway (PID {pid}) is managed by systemd unit {unit}.",@@ -5534,7 +5529,7 @@ def _refuse_restart_of_service_managed_gateway(pid: int | None) -> None:" kill a live gateway and spawn an unsupervised replacement in this shell's"," cgroup while the unit keeps failing against the stolen lock."," Restart it through its unit instead:",- f" {cmd}",+ *(f" {c}" for c in cmds)," (Pre-convention `hermes.service` installs: `hermes gateway migrate-legacy`"," removes the legacy unit so the canonical one can own the gateway.)",)diff --git a/tests/hermes_cli/test_gateway_restart_external_supervisor.py b/tests/hermes_cli/test_gateway_restart_external_supervisor.pyindex b6ead94df1e..ab9d0a1aa7a 100644--- a/tests/hermes_cli/test_gateway_restart_external_supervisor.py+++ b/tests/hermes_cli/test_gateway_restart_external_supervisor.py@@ -49,6 +49,8 @@ def restart_calls(monkeypatch):monkeypatch.setattr(gw, "stop_profile_gateway", lambda: calls.__setitem__("stopped", True) or True)monkeypatch.setattr(gw, "_wait_for_gateway_exit", lambda **k: None)monkeypatch.setattr(gw, "run_gateway", lambda **k: calls.__setitem__("started", True))+ # Hermetic on Linux CI: the runner's own /proc + systemd must not make the gateway look service-owned.+ monkeypatch.setattr("hermes_cli.main_dashboard._get_systemd_service_for_pid", lambda pid: None)return calls@@ -99,24 +101,34 @@ def test_plain_manual_gateway_still_uses_stop_and_run(restart_calls, monkeypatchassert restart_calls["started"], "a plain manually-run gateway must still be restarted in-process"-def test_service_managed_gateway_refuses_fallback_takeover(restart_calls, monkeypatch, capsys):[email protected]("cgroup, hints", [+ ("/system.slice/hermes.service", ["sudo systemctl restart hermes.service"]),+ ("/user.slice/user-1000.slice/[email protected]/app.slice/hermes.service",+ ["systemctl --user restart hermes.service"]),+ (None, ["systemctl --user restart hermes.service", "sudo systemctl restart hermes.service"]),+])+def test_service_managed_gateway_refuses_fallback_takeover(restart_calls, monkeypatch, capsys, cgroup, hints):"""A gateway supervised under ANY unit name must not be taken over by the fallback (#126474).- Pre-convention ``hermes.service`` system unit, no ``--external-supervisor`` marker, control- socket unreachable while busy serving webhooks: the identify/argv probe missed it, so the- fallback SIGKILLed a live gateway and spawned an unsupervised orphan in the caller's cgroup- while the unit's ``Restart=`` flapped against the stolen lock for hours.+ Pre-convention ``hermes.service`` unit, no ``--external-supervisor`` marker, control socket+ unreachable while busy serving webhooks: the identify/argv probe missed it, so the fallback+ SIGKILLed a live gateway and spawned an unsupervised orphan in the caller's cgroup while the+ unit's ``Restart=`` flapped against the stolen lock for hours. The hint follows the unit's+ scope; an unknown scope prints both forms."""monkeypatch.setattr(gw, "_capture_gateway_argv",lambda pid: ["/usr/bin/python", "-m", "hermes_cli.main", "gateway", "run"])monkeypatch.setattr("hermes_cli.main_dashboard._get_systemd_service_for_pid",lambda pid: "hermes.service" if pid == 4321 else None)monkeypatch.setattr("hermes_cli.main_dashboard._get_pid_cgroup_path",- lambda pid: "/system.slice/hermes.service" if pid == 4321 else None)+ lambda pid: cgroup if pid == 4321 else None)with pytest.raises(SystemExit) as exc:_run_restart()assert exc.value.code == 1assert not restart_calls["stopped"], "must not SIGTERM a service-managed gateway"assert not restart_calls["started"], "must not spawn an unsupervised orphan"out = capsys.readouterr().out- assert "hermes.service" in out and "systemctl restart" in out+ for hint in hints:+ assert hint in out+ if len(hints) == 1:+ assert out.count("systemctl") == 1, "a known scope must print only its own command"