Skip to content

The agent that grows with you

fix(gateway): a detached root gateway no longer boots the sticky active profile

Teknium9ec17f7f4fe5
diff --git a/hermes_cli/gateway_launchd.py b/hermes_cli/gateway_launchd.py
index de8a8a1decd..2b70bdb1a26 100644
--- a/hermes_cli/gateway_launchd.py
+++ b/hermes_cli/gateway_launchd.py
@@ -212,7 +212,13 @@ def _launchd_unsupported_marker_exists() -> bool:
def _gateway_run_command() -> list[str]:
from hermes_cli._launchers import runtime_command
- return runtime_command(_gw().PROJECT_ROOT, [*shlex.split(_gw()._profile_arg()), "gateway", "run", "--replace"],
+ from hermes_constants import get_default_hermes_root
+ # Every caller spawns an UNSUPERVISED child, which follows the sticky active_profile unless a
+ # selector pins it (main._apply_profile_override): the root's replacement booted that profile.
+ selector = shlex.split(_gw()._profile_arg())
+ if not selector and _gw().get_hermes_home().resolve() == get_default_hermes_root().resolve():
+ selector = ["--profile", "default"]
+ return runtime_command(_gw().PROJECT_ROOT, [*selector, "gateway", "run", "--replace"],
python=_gw().get_python_path())
diff --git a/tests/hermes_cli/test_gateway_host_verbs.py b/tests/hermes_cli/test_gateway_host_verbs.py
index 97164ace7df..fb6ec46d6d7 100644
--- a/tests/hermes_cli/test_gateway_host_verbs.py
+++ b/tests/hermes_cli/test_gateway_host_verbs.py
@@ -211,7 +211,7 @@ def test_named_profile_restart_all_with_host_down_restarts_the_default_root(monk
assert len(spawned) == 1
cmd, kwargs = spawned[0]
assert cmd[-3:] == ["gateway", "run", "--replace"]
- assert "--profile" not in cmd
+ assert cmd[-5:-3] == ["--profile", "default"] and "leinad" not in cmd
env = kwargs["env"]
assert Path(env["HERMES_HOME"]) == root
assert env.get("DISCORD_BOT_TOKEN") == "root-discord"
diff --git a/tests/hermes_cli/test_gateway_restart_no_unit_linger.py b/tests/hermes_cli/test_gateway_restart_no_unit_linger.py
index 6ae4ac600bd..071e014758d 100644
--- a/tests/hermes_cli/test_gateway_restart_no_unit_linger.py
+++ b/tests/hermes_cli/test_gateway_restart_no_unit_linger.py
@@ -110,10 +110,15 @@ def test_detached_restart_keeps_force_for_the_replacement(monkeypatch):
@pytest.mark.platforms("linux")
def test_detached_restart_all_never_runs_the_host_gateway_in_the_caller(monkeypatch, tmp_path):
- """`restart --all --detach` from the root profile with no service took the in-process path."""
+ """`restart --all --detach` from the root profile with no service took the in-process path.
+ The detached replacement is pinned to the root: a bare child follows the sticky active_profile."""
+ import sys
+
import hermes_constants
from hermes_cli import gateway as gw
+ (tmp_path / "profiles" / "work").mkdir(parents=True)
+ (tmp_path / "active_profile").write_text("work\n", encoding="utf-8")
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
hermes_constants._default_hermes_root_memo = None
monkeypatch.setattr(gw, "_refuse_from_inside_gateway", lambda *a, **k: None)
@@ -125,10 +130,12 @@ def test_detached_restart_all_never_runs_the_host_gateway_in_the_caller(monkeypa
monkeypatch.setattr(gw, "_wait_for_api_server_port_free", lambda **k: None)
monkeypatch.setattr(gw, "_discard_dead_host_record", lambda: False)
monkeypatch.setattr(gw, "_installed_service_kind_for", lambda windows: None)
+ monkeypatch.setattr(gw, "get_python_path", lambda: sys.executable)
monkeypatch.setattr(gw, "run_gateway", lambda **k: pytest.fail("the caller became the host gateway"))
- detached = []
- monkeypatch.setattr(gw, "_spawn_detached_gateway", lambda: detached.append(True) or True)
+ launched = []
+ monkeypatch.setattr(gw.subprocess, "Popen", lambda cmd, **k: launched.append(cmd) or SimpleNamespace())
gw._cmd_restart(SimpleNamespace(system=False, all=True, force=False, detach=True))
- assert detached == [True]
+ assert len(launched) == 1
+ assert launched[0][-5:] == ["--profile", "default", "gateway", "run", "--replace"]