The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): served-profile guard only counts a host gateway that is ours and not our own (#121352, #120871, salvage #121574 #122169)
teknium1f1a38955bd2d
diff --git a/hermes_cli/cron.py b/hermes_cli/cron.pyindex 4b86caa0c4b..c73465fab94 100644--- a/hermes_cli/cron.py+++ b/hermes_cli/cron.py@@ -514,8 +514,14 @@ def cron_status():if host is not None:print(f" Scheduler host: {host.describe()}")# `hermes gateway restart` exits 78 for a served NAMED profile- # (_guard_named_profile_under_multiplexer): the one host process is the default's.- _print_ticker_health([host.pid], restart_command="hermes --profile default gateway restart")+ # (_guard_named_profile_under_multiplexer): name the profile that LAUNCHED the host+ # process. On a standalone fleet that is this profile itself, not default (#120871).+ owner = None+ with contextlib.suppress(Exception):+ from hermes_cli.gateway import host_multiplexer_serving+ owner = host_multiplexer_serving(active)+ host_profile = owner.profile_label if owner is not None else "default"+ _print_ticker_health([host.pid], restart_command=f"hermes --profile {host_profile} gateway restart")elif pids or gateway_alive_via_lock or served_by_multiplexer or in_process_ticker:if served_by_multiplexer:print(" Scheduler host: the host gateway (multiplexing this profile)")diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 2079f9bc45b..850982410c6 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -4131,7 +4131,19 @@ def host_multiplexer_serving(profile_name: str | None = None):try:from gateway.host_attach import host_gateway_servingname = profile_name if profile_name is not None else _current_profile_name()- return host_gateway_serving(name or "default")+ gateway = host_gateway_serving(name or "default")+ if gateway is None:+ return None+ # The host record is HOST-wide, and two Hermes tenants on one host (separate HERMES_HOMEs)+ # each expose a profile named 'default': a same-named profile under ANOTHER tenant root is a+ # collision, not coverage. decide() / _claim_host_gateway_role already start beside such an+ # owner; the CLI guards read this probe and refused with exit 78 instead (#121352).+ from hermes_constants import get_default_hermes_root+ from gateway.status import _same_hermes_home+ if not _same_hermes_home(get_default_hermes_root(home=gateway.home), get_default_hermes_root()):+ logger.debug("Host gateway %s belongs to another Hermes home; not ours", gateway.describe())+ return None+ return gatewayexcept Exception:logger.debug("Host multiplexer probe failed", exc_info=True)return None@@ -4170,9 +4182,15 @@ def named_profile_served_by_running_multiplexer(profile_name: str | None = None)return False# The host record answers first: it names the live host process whatever home launched it, so a- # multiplexer started by a named profile is visible here too.- if host_multiplexer_serving(suffix) is not None:- return True+ # multiplexer started by a named profile is visible here too. A record launched by THIS profile's+ # own home is its own gateway (a standalone fleet member, or a multiplexer it hosts), never a+ # multiplexer serving a satellite: counting it refused the owner's own restart with exit 78 and+ # pointed it at `-p default`, whose gateway was not running (#120871).+ gateway = host_multiplexer_serving(suffix)+ if gateway is not None:+ from hermes_cli.profiles import normalize_profile_name+ if normalize_profile_name(gateway.profile_label) != normalize_profile_name(suffix):+ return Truetry:from hermes_constants import get_default_hermes_rootdiff --git a/tests/hermes_cli/test_gateway_served_guard_ownership.py b/tests/hermes_cli/test_gateway_served_guard_ownership.pynew file mode 100644index 00000000000..43e5235e565--- /dev/null+++ b/tests/hermes_cli/test_gateway_served_guard_ownership.py@@ -0,0 +1,131 @@+"""The CLI served-profile guard only counts a host gateway that is OURS and NOT our own process.++``host_multiplexer_serving`` reads the HOST-wide rendezvous record, so two readers were fooled:++* #121352 — two Hermes tenants (separate ``HERMES_HOME`` roots) on one host each expose a profile+ named ``default``; tenant B's guard saw tenant A's multiplexer "serving default" and refused+ with exit 78, parking B's launchd unit. A host gateway under ANOTHER tenant root never serves us.+* #120871 — a standalone fleet member (``-p argus gateway run``, no default gateway) publishes the+ host record itself; ``named_profile_served_by_running_multiplexer`` counted the profile's OWN+ process as "a multiplexer serves you" and ``gateway restart`` refused, pointing at ``-p default``.++Control: a satellite whose own home is NOT the host record's home, while the same tenant's live+default multiplexer lists it, is still refused without ``--force``.+"""++from __future__ import annotations++import io+from contextlib import redirect_stdout+from pathlib import Path++import pytest++import hermes_constants+from gateway import host_attach+from gateway.host_attach import HostGateway+++def _use_home(monkeypatch, fake_home: Path, hermes_home: Path) -> None:+ hermes_home.mkdir(parents=True, exist_ok=True)+ (hermes_home / "config.yaml").write_text("{}\n", encoding="utf-8")+ monkeypatch.setenv("HOME", str(fake_home))+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))+ monkeypatch.setattr(hermes_constants, "_default_hermes_root_memo", None)+ monkeypatch.setattr("gateway.status._get_process_hermes_home", lambda: hermes_home)+++def _publish_host(monkeypatch, home: Path, served: tuple[str, ...]) -> HostGateway:+ gateway = HostGateway(4242, home, served)+ monkeypatch.setattr(host_attach, "host_gateway_serving",+ lambda profile, **kw: gateway if gateway.serves(profile) else None)+ return gateway+++def _refusal(gw) -> tuple[bool, str]:+ buf = io.StringIO()+ with redirect_stdout(buf):+ refused = gw._named_profile_refused_under_multiplexer()+ return refused, buf.getvalue()+++def gw(monkeypatch):+ from hermes_cli import gateway as gw+ monkeypatch.setattr(gw, "_is_service_installed", lambda: True)+ return gw+++def test_foreign_tenants_host_gateway_never_serves_this_home(tmp_path, monkeypatch, gw):+ """#121352 (T2 vs a second tenant home): tenant A's multiplexer serves A's 'default' and 'coder';+ tenant B's default AND B's coder start beside it without --force."""+ fake_home = tmp_path / "fakehome"+ root_a = fake_home / "hermes-a" / "home"+ root_b = fake_home / "hermes-b" / "home"+ _use_home(monkeypatch, fake_home, root_b)+ _publish_host(monkeypatch, root_a, ("default", "coder"))++ assert gw.host_multiplexer_serving("default") is None+ assert gw._served_by_another_host_gateway() is None+ assert gw.named_profile_served_by_running_multiplexer("coder") is False+ refused, out = _refusal(gw)+ assert (refused, out) == (False, "")+++def test_own_standalone_gateway_is_not_a_multiplexer_refusing_its_restart(tmp_path, monkeypatch, gw):+ """#120871 (T1/T6 standalone fleet): argus's own host record is ours to restart."""+ fake_home = tmp_path / "fakehome"+ own_home = fake_home / ".hermes" / "profiles" / "argus"+ _use_home(monkeypatch, fake_home, own_home)+ _publish_host(monkeypatch, own_home, ("argus",))++ assert gw._served_by_another_host_gateway() is None+ assert gw.named_profile_served_by_running_multiplexer() is False+ refused, out = _refusal(gw)+ assert (refused, out) == (False, "")+ gw._guard_named_profile_under_multiplexer() # must not sys.exit(78)+++def test_satellite_served_by_same_tenants_multiplexer_is_still_refused(tmp_path, monkeypatch, gw):+ """Control: the default multiplexer of OUR tenant lists coder -> refused, hint names the owner."""+ fake_home = tmp_path / "fakehome"+ root = fake_home / ".hermes"+ _use_home(monkeypatch, fake_home, root / "profiles" / "coder")+ _publish_host(monkeypatch, root, ("default", "coder"))++ assert gw.named_profile_served_by_running_multiplexer() is True+ refused, out = _refusal(gw)+ assert refused is True+ assert "hermes -p default gateway restart" in out+ with pytest.raises(SystemExit) as exc:+ with redirect_stdout(io.StringIO()):+ gw._guard_named_profile_under_multiplexer()+ assert exc.value.code == gw.GATEWAY_FATAL_CONFIG_EXIT_CODE+++def test_cron_status_restart_hint_names_the_profile_hosting_the_scheduler(tmp_path, monkeypatch, gw):+ """#120871: `hermes -p argus cron status` on a standalone fleet points at argus, not default."""+ from gateway.host_topology import HostGatewayTopology+ from hermes_cli import cron as cron_mod++ fake_home = tmp_path / "fakehome"+ own_home = fake_home / ".hermes" / "profiles" / "argus"+ _use_home(monkeypatch, fake_home, own_home)+ _publish_host(monkeypatch, own_home, ("argus",))+ monkeypatch.setattr(cron_mod, "_active_cron_provider_name", lambda: "builtin")+ monkeypatch.setattr("hermes_cli.profiles.get_active_profile_name", lambda: "argus")+ monkeypatch.setattr("gateway.host_topology.host_gateway_serving",+ lambda name=None: HostGatewayTopology(4242, ("argus",), "host_record"))+ seen = []++ class _Stop(Exception):+ pass++ def _capture(pids, restart_command="hermes gateway restart"):+ seen.append(restart_command)+ raise _Stop++ monkeypatch.setattr(cron_mod, "_print_ticker_health", _capture)+ with pytest.raises(_Stop), redirect_stdout(io.StringIO()):+ cron_mod.cron_status()+ assert seen == ["hermes --profile argus gateway restart"]