Skip to content

The agent that grows with you

fix(gateway): run every `--all` verb as the host root when a named profile is active (#124473)

teknium1c3251f86806d
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 0378518fb21..22f2b7c043b 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -5281,7 +5281,7 @@ def _print_unfolded_gateway_note(owner) -> None:
def _cmd_start(args):
- from hermes_cli.gateway_profile_lifecycle import profile_lifecycle
+ from hermes_cli.gateway_profile_lifecycle import host_scope_for_all_verb, profile_lifecycle
if profile_lifecycle("start", args):
return
system = getattr(args, "system", False)
@@ -5296,13 +5296,10 @@ def _cmd_start(args):
print(" One gateway per host serves every profile; nothing to start.")
_print_unfolded_gateway_note(owner)
return
- # `--all` names the ONE host gateway, regardless of which profile invoked the CLI.
- # Bind the same service-manager scope used by migration so stale-owner cleanup, Windows
- # task names/launchers, systemd/launchd identity files and raw HERMES_HOME readers all
- # target the default root.
- from hermes_cli.gateway_migrate import _home_env
- from hermes_constants import get_default_hermes_root
- start_scope = _home_env(get_default_hermes_root())
+ # `--all` names the ONE host gateway, regardless of which profile invoked the CLI: stale-owner
+ # cleanup, Windows task names/launchers, systemd/launchd identity files and raw HERMES_HOME
+ # readers all target the default root (see host_scope_for_all_verb).
+ start_scope = host_scope_for_all_verb(owner)
else:
_guard_named_profile_under_multiplexer(force=force)
if _dispatch_via_service_manager_if_s6("start"):
@@ -5327,7 +5324,7 @@ def _cmd_start(args):
def _cmd_stop(args):
_refuse_from_inside_gateway("stop", "restart loops")
- from hermes_cli.gateway_profile_lifecycle import profile_lifecycle
+ from hermes_cli.gateway_profile_lifecycle import host_scope_for_all_verb, profile_lifecycle
if profile_lifecycle("stop", args):
return
stop_all = getattr(args, "all", False)
@@ -5354,20 +5351,23 @@ def _cmd_stop(args):
if not stop_all and _dispatch_via_service_manager_if_s6("stop"):
return
- service_available = _stop_installed_service(system)
if stop_all:
+ # Stop the HOST's installed service (a bare pkill under a supervisor is a restart, not a
+ # stop), whichever profile invoked the CLI.
+ with host_scope_for_all_verb(_host_multiplexer_for_all_verb()):
+ service_available = _stop_installed_service(system)
total = kill_gateway_processes(all_profiles=True) + (1 if service_available else 0)
if total:
print(f"✓ Stopped {total} gateway process(es) across all profiles")
else:
print("✗ No gateway processes found")
- elif not service_available:
- if stop_profile_gateway():
- print("✓ Stopped gateway for this profile")
- else:
- print("✗ No gateway running for this profile")
- else:
+ return
+ if _stop_installed_service(system):
print(f"✓ Stopped {get_service_name()} service")
+ elif stop_profile_gateway():
+ print("✓ Stopped gateway for this profile")
+ else:
+ print("✗ No gateway running for this profile")
def _stop_host_multiplexer(owner) -> int:
@@ -5394,6 +5394,7 @@ def _discard_dead_host_record() -> bool:
def _restart_all(system: bool) -> None:
+ from hermes_cli.gateway_profile_lifecycle import host_scope_for_all_verb
owner = _host_multiplexer_for_all_verb()
if owner is not None and not _host_multiplexer_is_ours(owner):
# `--all` means "restart the ONE host multiplexer" — and this profile does not own it.
@@ -5405,7 +5406,13 @@ def _restart_all(system: bool) -> None:
print()
print(f" hermes -p {owner.profile_label} gateway restart --all")
sys.exit(GATEWAY_FATAL_CONFIG_EXIT_CODE)
+ # No live owner: stop/kill/start as the host root, not as the invoking profile (whose own
+ # gateway the run-side guard would refuse — the host stayed down after a Desktop update).
+ with host_scope_for_all_verb(owner):
+ _restart_all_as_host(owner, system)
+
+def _restart_all_as_host(owner, system: bool) -> None:
service_stopped = _stop_installed_service(system)
if owner is not None:
# Stop ONLY the host process: its served set comes back with it, and any per-profile
diff --git a/hermes_cli/gateway_profile_lifecycle.py b/hermes_cli/gateway_profile_lifecycle.py
index c0380690b53..11330c5d42d 100644
--- a/hermes_cli/gateway_profile_lifecycle.py
+++ b/hermes_cli/gateway_profile_lifecycle.py
@@ -15,6 +15,18 @@ def _failure(answer):
return "host control socket did not answer"
+def host_scope_for_all_verb(owner):
+ """Home a ``--all`` verb acts as: the live host owner's, else the default root.
+
+ ``--all`` names the ONE host gateway, whichever profile invoked the CLI — but the active home
+ decides the service name, the Windows task/launcher and the detached child's ``--profile``, so
+ with the host DOWN ``-p X gateway start|restart --all`` re-launched X's own gateway, which the
+ named-profile guard then refused (exit 78): the host stayed down after a Desktop update.
+ """
+ from hermes_cli.gateway_migrate import _home_env
+ return _home_env(owner.home if owner is not None else get_default_hermes_root())
+
+
def profile_lifecycle(command: str, args) -> bool:
"""True when a named-profile command was handled (including an unconfirmed request)."""
from hermes_cli import gateway as gw
diff --git a/tests/hermes_cli/test_gateway_host_verbs.py b/tests/hermes_cli/test_gateway_host_verbs.py
index 1768e8228ce..a4c244a5020 100644
--- a/tests/hermes_cli/test_gateway_host_verbs.py
+++ b/tests/hermes_cli/test_gateway_host_verbs.py
@@ -162,6 +162,42 @@ def test_named_profile_start_all_starts_the_default_host_service(monkeypatch, tm
assert os.environ["HERMES_HOME"] == str(named)
+def test_named_profile_restart_all_with_host_down_restarts_the_default_root(monkeypatch, tmp_path):
+ """No live owner: `-p X gateway restart --all` must relaunch the host root, never X's gateway.
+
+ Post-update the host is down, so the ownership refusal cannot fire; the sweep-and-relaunch
+ then ran as X, whose own gateway the run-side named-profile guard refuses (exit 78).
+ """
+ import hermes_constants
+
+ root = tmp_path / "hermes"
+ named = root / "profiles" / "leinad"
+ named.mkdir(parents=True)
+ (root / "config.yaml").write_text("{}\n", encoding="utf-8")
+ (named / "config.yaml").write_text("{}\n", encoding="utf-8")
+
+ monkeypatch.setenv("HERMES_HOME", str(named))
+ hermes_constants._default_hermes_root_memo = None
+ monkeypatch.setattr(gw, "_host_multiplexer_for_all_verb", lambda: None)
+ monkeypatch.setattr(gw, "_stop_installed_service", lambda system: False)
+ monkeypatch.setattr(gw, "kill_gateway_processes", lambda **k: 0)
+ monkeypatch.setattr(gw, "_wait_for_gateway_exit", lambda **k: True)
+ monkeypatch.setattr(gw, "_wait_for_api_server_port_free", lambda **k: True)
+ monkeypatch.setattr(gw, "_discard_dead_host_record", lambda: False)
+ monkeypatch.setattr(gw, "_installed_service_kind_for", lambda windows: None)
+
+ started_as = []
+ monkeypatch.setattr(
+ gw, "run_gateway",
+ lambda **k: started_as.append((Path(hermes_constants.get_hermes_home()), k.get("replace"))),
+ )
+
+ gw._restart_all(system=False)
+
+ assert started_as == [(root, True)]
+ assert hermes_constants.get_hermes_home() == named
+
+
def test_restart_all_refuses_to_sweep_a_host_multiplexer_owned_by_another_profile(
host_owner, monkeypatch, tmp_path):
def _never(*a, **k):