Skip to content

The agent that grows with you

fix(gateway): restart refuses before it stops anything (#125394)

teknium1a0e71746f407
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index ac028811533..b32f158ce28 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -5496,6 +5496,11 @@ def _cmd_restart(args):
kind = _installed_service_kind_for(is_windows)
service_configured = kind is not None and (kind != "windows" or _gw_windows().is_installed())
if kind is not None:
+ # Reap orphans only past the refusal guards above (#125394): the Desktop backend used to
+ # reap before spawning this command, so a refused restart still cost the profile its
+ # gateway. A service-manager restart never sweeps orphans itself (#77276); the manual
+ # fallback below reaps inside stop_profile_gateway().
+ _reap_unsupervised_gateway_orphans()
swallow = (RuntimeError, OSError) if kind == "windows" else ()
try:
_service_call(kind, "restart", system)
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index fd1f4069ebb..38a6cc14a39 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -895,17 +895,12 @@ def _spawn_gateway_restart(profile: Optional[str] = None) -> Tuple[subprocess.Po
Concurrent children race each other on the kill-and-start path, so a live
child is reused; requests within ``GATEWAY_RESTART_COOLDOWN_SECONDS`` for the
- same profile coalesce onto the last spawn too (#89034). Orphaned gateways
- are reaped first so the fresh one doesn't stack a duplicate (#77276).
- Returns ``(proc, reused)``.
+ same profile coalesce onto the last spawn too (#89034). This process stops
+ nothing itself: the child decides whether the restart is even allowed
+ (``_cmd_restart``'s multiplexer guard), so the orphan reap (#77276) runs
+ there, after that guard — reaping here killed the profile's gateway and then
+ the child refused to start a replacement (#125394). Returns ``(proc, reused)``.
"""
- try:
- from hermes_cli.gateway import _reap_unsupervised_gateway_orphans
-
- _reap_unsupervised_gateway_orphans()
- except Exception:
- pass # best-effort — don't block the restart on a reap failure
-
global _LAST_GATEWAY_RESTART
subcommand = _gateway_mod._gateway_subcommand(profile, "restart")
diff --git a/tests/hermes_cli/test_spawn_gateway_restart_reap.py b/tests/hermes_cli/test_spawn_gateway_restart_reap.py
index 54f103045c9..16dd77c4b22 100644
--- a/tests/hermes_cli/test_spawn_gateway_restart_reap.py
+++ b/tests/hermes_cli/test_spawn_gateway_restart_reap.py
@@ -1,20 +1,23 @@
-"""Tests for _spawn_gateway_restart orphan-reap guard (#77276)."""
+"""The gateway-restart flow refuses BEFORE it stops anything (#125394, #77276).
+
+The Desktop backend's ``_spawn_gateway_restart`` used to reap "orphan" gateways
+before spawning ``hermes gateway restart``; when that child then refused (a
+named profile under the host multiplexer) the profile had already lost its
+gateway. The reap belongs to the child, after its refusal guard.
+"""
from __future__ import annotations
import subprocess
+from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
+from hermes_cli import gateway as gw
+
@pytest.fixture(autouse=True)
def reset_restart_cooldown():
- """Clear the #89034 repeat-restart cooldown between cases.
-
- ``_spawn_gateway_restart`` now coalesces a second restart request that
- arrives within ``GATEWAY_RESTART_COOLDOWN_SECONDS`` of the last spawn, so
- without this the first case's spawn suppresses the second case's.
- """
import hermes_cli.web_server as web_server
web_server._LAST_GATEWAY_RESTART = None
@@ -22,26 +25,33 @@ def reset_restart_cooldown():
web_server._LAST_GATEWAY_RESTART = None
-class TestSpawnGatewayRestartReapsOrphans:
- """_spawn_gateway_restart must reap orphaned gateways before spawning."""
+@patch("hermes_cli.web_server_gateway._gateway_subcommand", return_value=["gateway", "restart"])
+@patch("hermes_cli.web_server_gateway._spawn_hermes_action")
+@patch("hermes_cli.web_server_gateway._ACTION_PROCS", {})
+def test_backend_spawns_restart_without_stopping_anything(mock_spawn, mock_subcmd):
+ mock_proc = MagicMock(spec=subprocess.Popen)
+ mock_proc.poll.return_value = None
+ mock_spawn.return_value = mock_proc
+
+ from hermes_cli.web_server import _spawn_gateway_restart
+
+ with patch("hermes_cli.gateway._reap_unsupervised_gateway_orphans") as reap:
+ proc, reused = _spawn_gateway_restart()
+ reap.assert_not_called()
+ mock_spawn.assert_called_once()
+ assert proc is mock_proc and not reused
- @patch("hermes_cli.web_server_gateway._gateway_subcommand", return_value=["gateway", "restart"])
- @patch("hermes_cli.web_server_gateway._spawn_hermes_action")
- @patch("hermes_cli.web_server_gateway._ACTION_PROCS", {})
- def test_reap_failure_does_not_block_spawn(self, mock_spawn, mock_subcmd):
- """If reap raises, the restart still proceeds."""
- mock_proc = MagicMock(spec=subprocess.Popen)
- mock_proc.poll.return_value = None
- mock_spawn.return_value = mock_proc
- from hermes_cli.web_server import _spawn_gateway_restart
+def test_cli_restart_reaps_after_the_refusal_guard(monkeypatch):
+ order: list[str] = []
+ monkeypatch.setattr(gw, "_refuse_from_inside_gateway", lambda *a, **k: None)
+ monkeypatch.setattr(gw, "_guard_named_profile_under_multiplexer", lambda **k: order.append("guard"))
+ monkeypatch.setattr(gw, "_dispatch_via_service_manager_if_s6", lambda *a, **k: False)
+ monkeypatch.setattr(gw, "_installed_service_kind_for", lambda windows: "launchd")
+ monkeypatch.setattr(gw, "_reap_unsupervised_gateway_orphans", lambda *a, **k: order.append("reap"))
+ monkeypatch.setattr(gw, "_service_call", lambda *a, **k: order.append("restart"))
- with patch(
- "hermes_cli.gateway._reap_unsupervised_gateway_orphans",
- side_effect=OSError("permission denied"),
- ):
- proc, reused = _spawn_gateway_restart()
+ gw._cmd_restart(SimpleNamespace(system=False, all=False, force=False))
- mock_spawn.assert_called_once()
- assert proc is mock_proc
+ assert order == ["guard", "reap", "restart"]