The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a standalone owner is the per-profile topology, not a refusal
kshitijk4poor804cc3c0215c
diff --git a/gateway/host_attach.py b/gateway/host_attach.pyindex 8985a1d465f..8b8ad0963e9 100644--- a/gateway/host_attach.py+++ b/gateway/host_attach.py@@ -10,8 +10,11 @@ one. Four outcomes, in order:* ``RESCAN``→ATTACH — it does not serve it yet: ask it to reconcile ``profiles/`` now (controlsocket ``rescan-profiles``) and attach once the answer includes us.* ``REPLACE_HOST`` — ``--replace`` names the host process as the target, whichever home launched it.-* ``REFUSE`` — a live host gateway exists and cannot be made to serve this profile. Never- start a second one silently.+* ``REFUSE`` — a live MULTIPLEXING gateway exists and cannot be made to serve this profile.+ Never start a second one silently.+* ``START`` — no live owner, or the owner answers ``multiplex: False``: it is another+ profile's standalone gateway (the documented one-process-per-profile topology), not a+ multiplexer that excluded us, so this profile runs its own gateway beside it as it always did.**The attach channel is the OWNER's control socket, never ours.** Ordering matters: the ownerpublishes its rendezvous record when it claims its PID file and binds its control socket a moment@@ -76,6 +79,9 @@ class HostGateway:#: False when the owner has not answered ``identify`` yet: an owner exists, but which profiles#: it serves is UNKNOWN. Never conflate that with "serves nothing" — see the module doc.served_known: bool = True+ #: True once the owner has said ``multiplex: False``: it is a per-profile gateway (the documented+ #: one-process-per-profile topology), not a multiplexer that can be asked to serve anyone else.+ standalone: bool = Falsedef serves(self, profile: str) -> bool:if not self.served_known:@@ -201,7 +207,9 @@ def host_gateway_serving(profile: str, *, wait_for_channel: float = 0.0) -> Optidef request_serve_profile(profile: str, *, timeout: float = 8.0,owner: Optional[HostGateway] = None) -> Optional[HostGateway]:"""Ask the live host gateway to reconcile ``profiles/`` now; return it once it serves- ``profile``. ``None`` when nobody answered or the answer still excludes the profile."""+ ``profile``. ``None`` when nobody answered or a multiplexer's roster still excludes the profile.+ An owner that answers ``multiplex: False`` comes back flagged ``standalone``: it cannot take the+ profile, and it is not a multiplexer that refused — the caller runs beside it, as before."""gateway = owner if owner is not None else host_gateway(wait_for_channel=ATTACH_CHANNEL_WAIT_S)if gateway is None or gateway.serves(profile):return gateway@@ -212,8 +220,10 @@ def request_serve_profile(profile: str, *, timeout: float = 8.0,except Exception:logger.debug("host gateway rescan failed", exc_info=True)return None- if not isinstance(answer, dict) or answer.get("multiplex") is False:+ if not isinstance(answer, dict):return None+ if answer.get("multiplex") is False:+ return HostGateway(gateway.pid, gateway.home, gateway.profiles, standalone=True)served = answer.get("served_profiles")rescanned = HostGateway(gateway.pid, gateway.home,@@ -297,6 +307,12 @@ def decide(our_home: Path, *, replace: bool = False) -> HostAttachDecision:attached = Noneif attached is not None and attached.serves(profile):return HostAttachDecision(ATTACH, attach_message(attached, profile), attached, transient=True)+ if attached is not None and attached.standalone:+ # One-process-per-profile fleet: the owner is another profile's standalone gateway. Refusing+ # here exits 78, which every supervisor treats as permanent — on a launchd fleet that parked+ # every unit but the first to claim the host lock. Start beside it; the host-lock claim logs+ # the topology and the `gateway migrate --multiplex` path stays the way to converge.+ return HostAttachDecision(START, "", attached)if not gateway.served_known:# The owner never answered, so we know only that it exists. ATTACH here (on the record's# word) parked a supervised unit against a served set nobody had committed to yet.diff --git a/tests/gateway/test_host_attach_lifecycle.py b/tests/gateway/test_host_attach_lifecycle.pyindex 9591df8a55c..67c4fe38c39 100644--- a/tests/gateway/test_host_attach_lifecycle.py+++ b/tests/gateway/test_host_attach_lifecycle.py@@ -137,16 +137,32 @@ def test_run_for_an_unserved_profile_rescans_then_attaches(tmp_path, monkeypatchdef test_host_gateway_refuses_when_it_will_not_serve_the_profile(tmp_path, monkeypatch, owner_pid):+ """A MULTIPLEXING owner whose roster still excludes us after a rescan is the permanent refusal."""owner_home = tmp_path / "root"_publish(owner_pid, owner_home, ("default",))_answer_identify(monkeypatch, owner_pid, owner_home, ["default"])monkeypatch.setattr(gateway_run, "get_hermes_home", lambda: owner_home / "profiles" / "other")monkeypatch.setattr("gateway.control_socket.rescan_gateway_profiles",- lambda home, timeout=8.0: {"multiplex": False})+ lambda home, timeout=8.0: {"multiplex": True, "served_profiles": ["default"]})assert asyncio.run(gateway_run._host_attach_or_none(replace=False)) is False+def test_a_standalone_owner_is_the_per_profile_topology_not_a_refusal(tmp_path, monkeypatch, owner_pid):+ """The owner answers ``multiplex: False``: it is a per-profile gateway, not a multiplexer that+ excluded us. Refusing here (exit 78 → launchd parks the unit) took every other profile's+ supervised gateway down at boot on a one-process-per-profile fleet. Start as before."""+ owner_home = tmp_path / "root" / "profiles" / "tank"+ _publish(owner_pid, owner_home, ("tank",))+ _answer_identify(monkeypatch, owner_pid, owner_home, ["tank"])+ monkeypatch.setattr(gateway_run, "get_hermes_home", lambda: tmp_path / "root" / "profiles" / "nous")+ monkeypatch.setattr("gateway.control_socket.rescan_gateway_profiles",+ lambda home, timeout=8.0: {"multiplex": False, "served_profiles": ["tank"]})++ assert host_attach.decide(tmp_path / "root" / "profiles" / "nous").outcome == host_attach.START+ assert asyncio.run(gateway_run._host_attach_or_none(replace=False)) is None++def test_replace_signals_the_owner_instead_of_standing_down(tmp_path, monkeypatch, owner_pid):"""``gateway run --replace`` against a live owner must REACH it — the branch was dead code."""owner_home = tmp_path / "root"diff --git a/website/docs/developer-guide/gateway-internals.md b/website/docs/developer-guide/gateway-internals.mdindex a47eddf75f9..543793d5076 100644--- a/website/docs/developer-guide/gateway-internals.md+++ b/website/docs/developer-guide/gateway-internals.md@@ -270,7 +270,7 @@ The gateway runs as a long-lived process, managed via:- `systemctl` (Linux) or `launchctl` (macOS) — service management- PID file at `~/.hermes/gateway.pid` — profile-scoped process tracking-**Profile-scoped vs global**: `start_gateway()` uses profile-scoped PID files. Standalone (one gateway per profile), `hermes -p x gateway stop` stops only that profile's gateway. Under multiplexing there is ONE gateway process per host, owned by whichever profile launched it (`gateway/host_rendezvous.py` publishes its PID, home and served set; `gateway/host_attach.py` is the attach/rescan/refuse decision every lifecycle verb goes through): `hermes gateway stop` on the owner takes every served profile down, and `hermes -p x gateway stop` for a served secondary refuses with exit 78 (it has no gateway of its own). A second `gateway run` for a served profile attaches and exits 0 — under a service supervisor it exits 75 (EX_TEMPFAIL) instead, so the redundant unit is RETRIED rather than parked: "someone else serves me right now" is a runtime observation that ends when that process does, and 78 (which systemd, s6 and launchd all treat as permanent) would strand the profile. ATTACH requires a live `identify` answer from the owner; a rendezvous record with nothing answering behind it proves an owner exists but never that it serves you, so it yields a transient refusal (exit 75), never an attach. `hermes gateway stop --all` uses global `ps aux` scanning to kill all gateway processes (used during updates). Liveness is decided by `gateway.status.live_gateway_pid_for_home` (PID + start-time fingerprint), never bare PID existence.+**Profile-scoped vs global**: `start_gateway()` uses profile-scoped PID files. Standalone (one gateway per profile), `hermes -p x gateway stop` stops only that profile's gateway. Under multiplexing there is ONE gateway process per host, owned by whichever profile launched it (`gateway/host_rendezvous.py` publishes its PID, home and served set; `gateway/host_attach.py` is the attach/rescan/refuse decision every lifecycle verb goes through): `hermes gateway stop` on the owner takes every served profile down, and `hermes -p x gateway stop` for a served secondary refuses with exit 78 (it has no gateway of its own). A second `gateway run` for a served profile attaches and exits 0 — under a service supervisor it exits 75 (EX_TEMPFAIL) instead, so the redundant unit is RETRIED rather than parked: "someone else serves me right now" is a runtime observation that ends when that process does, and 78 (which systemd, s6 and launchd all treat as permanent) would strand the profile. ATTACH requires a live `identify` answer from the owner; a rendezvous record with nothing answering behind it proves an owner exists but never that it serves you, so it yields a transient refusal (exit 75), never an attach. An owner that answers `multiplex: False` to the rescan is another profile's *standalone* gateway, not a multiplexer that excluded you: the verb starts this profile's own gateway beside it (the one-process-per-profile topology), it does not refuse — refusing there exited 78 and parked every launchd unit but the first to claim the host lock. `hermes gateway stop --all` uses global `ps aux` scanning to kill all gateway processes (used during updates). Liveness is decided by `gateway.status.live_gateway_pid_for_home` (PID + start-time fingerprint), never bare PID existence.## Multiplexed profiles