The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway_runtime): a named-profile client starts the multiplexer, never a per-profile daemon
teknium1b3aa491ffeb8
diff --git a/hermes_cli/gateway_runtime.py b/hermes_cli/gateway_runtime.pyindex 11f7be36164..f45deaedb15 100644--- a/hermes_cli/gateway_runtime.py+++ b/hermes_cli/gateway_runtime.py@@ -93,18 +93,10 @@ def control_home_for(home: Path, endpoint: GatewayEndpoint | None) -> Path:return home-def _multiplexer_home_for(home: Path) -> Path | None:- """Default root that may multiplex *home*, when *home* is a named profile under it."""- from hermes_constants import named_profile_home- if named_profile_home(home) is None:- return None- root = home.parent.parent- return root if root != home else None--def _multiplexer_starting(home: Path) -> bool:from hermes_cli.gateway_runtime_discovery import missing_owner_state- root = _multiplexer_home_for(home)+ from hermes_cli.gateway_runtime_multiplex import multiplexer_root_for+ root = multiplexer_root_for(home)return root is not None and missing_owner_state(root) == "starting"@@ -113,7 +105,8 @@ def _served_by_multiplexer(home: Path, *, timeout: float) -> GatewayDiscovery |answers for it, and its ``identify`` lists the home under ``served_profiles``. Only themultiplexer's live descriptor proves service; a held ``gateway.lock`` alone does not."""from hermes_cli.gateway_runtime_discovery import DiscoveryError, query_identify- root = _multiplexer_home_for(home)+ from hermes_cli.gateway_runtime_multiplex import multiplexer_root_for+ root = multiplexer_root_for(home)if root is None:return Nonetry:@@ -198,7 +191,12 @@ def ensure_gateway_runtime(profile_home: str | Path, *, timeout: float = 30.0) -if observed.state == "absent" and not requested and _multiplexer_starting(home):requested = Trueif observed.state == "absent" and not requested:- service = discover_existing_gateway_service(home, deadline=deadline)+ # A named profile the default multiplexer serves has no daemon of its own: start+ # (or await) the MULTIPLEXER. A per-profile spawn here would become a second owner+ # that blocks the multiplexer's next all-or-nothing reserve.+ from hermes_cli.gateway_runtime_multiplex import multiplexer_serves_home+ target = multiplexer_serves_home(home) or home+ service = discover_existing_gateway_service(target, deadline=deadline)# Runtime locks settle races remaining after this second probe.observed = discover_gateway_endpoint(home, timeout=remaining(deadline))if observed.state != "absent":@@ -207,7 +205,7 @@ def ensure_gateway_runtime(profile_home: str | Path, *, timeout: float = 30.0) -start_existing_gateway_service(service, deadline=deadline)else:from hermes_cli.gateway_runtime_start import spawn_unmanaged_gateway- spawn_unmanaged_gateway(home, deadline=deadline)+ spawn_unmanaged_gateway(target, deadline=deadline)requested = Truetime.sleep(min(delay, remaining(deadline)))delay = min(delay * 1.5, 0.25)diff --git a/hermes_cli/gateway_runtime_multiplex.py b/hermes_cli/gateway_runtime_multiplex.pynew file mode 100644index 00000000000..229ed0185ac--- /dev/null+++ b/hermes_cli/gateway_runtime_multiplex.py@@ -0,0 +1,49 @@+"""Which daemon a named-profile client must start when nothing serves its home.++A ``<root>/profiles/<name>`` home that the default multiplexer serves has no daemon of its own.+When the multiplexer is down (restart, update, crash) the profile looks ``absent`` and the ensure+loop would spawn a per-profile gateway for it — a second owner that answers this client and then+blocks the multiplexer's next all-or-nothing reserve (``Cannot reserve gateway profiles``). The+client must start the multiplexer instead, so the start target is decided here from the same facts+the multiplexer boots on: the default profile's explicit ``gateway.multiplex_profiles`` flag, or —+when the flag is unset and settled at boot — the served set the last multiplexer recorded in the+root's ``gateway_state.json`` (a standalone default gateway clears it).+"""+from __future__ import annotations++from pathlib import Path+++def multiplexer_root_for(home: Path) -> Path | None:+ """Default root that may multiplex *home*, when *home* is a named profile under it."""+ from hermes_constants import named_profile_home+ if named_profile_home(home) is None:+ return None+ root = home.parent.parent+ return root if root != home else None+++def multiplexer_serves_home(home: Path) -> Path | None:+ """The root whose (possibly stopped) multiplexer serves *home*, else None.++ ``hermes_cli.gateway.named_profile_served_by_running_multiplexer`` answers the live question;+ this is its offline twin for the moment no gateway runs: an explicit ``true`` on the default+ profile, or a recorded ``served_profiles`` naming this profile (the boot-time verdict of an unset+ flag). An explicit ``false``, or no evidence, keeps the per-profile daemon.+ """+ root = multiplexer_root_for(home)+ if root is None:+ return None+ from hermes_cli.gateway_multiplex_mode import explicit_multiplex_flag+ flag = explicit_multiplex_flag(root)+ if flag is True:+ return root+ if flag is False:+ return None+ from gateway.status import read_runtime_status+ from hermes_cli.profiles import normalize_profile_name+ served = (read_runtime_status(root / "gateway_state.json") or {}).get("served_profiles")+ if not isinstance(served, list):+ return None+ names = {normalize_profile_name(str(p)) for p in served if p}+ return root if normalize_profile_name(home.name) in names else Nonediff --git a/tests/gateway/test_multiplex_hot_serve_live.py b/tests/gateway/test_multiplex_hot_serve_live.pyindex 3d44af06bfe..1587a936840 100644--- a/tests/gateway/test_multiplex_hot_serve_live.py+++ b/tests/gateway/test_multiplex_hot_serve_live.py@@ -7,6 +7,7 @@ owner instead of spawning a stray per-profile daemon that would later collide wimultiplexer's all-or-nothing reserve. A secondary whose ``state.db`` is unusable parks only itself."""import asyncio+import contextlibimport jsonimport osfrom pathlib import Path@@ -185,6 +186,53 @@ def test_profile_created_under_live_multiplexer_is_served_and_owned(mux):stop(proc, expect=None)+def kill_recorded_gateway(root, timeout=45):+ """SIGINT the gateway whose PID ``gateway_state.json`` records (a detached spawn is not our child)."""+ pid = json.loads((root / 'gateway_state.json').read_text(encoding='utf-8')).get('pid')+ if pid:+ with contextlib.suppress(ProcessLookupError):+ os.kill(pid, signal.SIGINT)+ deadline = time.monotonic() + timeout+ while time.monotonic() < deadline:+ try:+ os.kill(pid, 0)+ except ProcessLookupError:+ return+ time.sleep(.2)+ with contextlib.suppress(ProcessLookupError):+ os.kill(pid, signal.SIGKILL)++[email protected]_system_guard_bypass+def test_named_profile_ensure_during_multiplexer_downtime_starts_the_multiplexer(mux):+ """The multiplexer is down (restart/update) and ``hermes -p boot gateway ensure`` runs: the client+ must start the MULTIPLEXER, never a per-profile daemon (which would answer this client and then+ block the multiplexer's next all-or-nothing reserve with ``Cannot reserve gateway profiles``)."""+ root, env, boot = mux['root'], mux['env'], mux['boot']+ proc, desc = start_daemon(mux)+ stop(proc)+ assert recorded_served(root) == ['default', 'boot']+ try:+ ensured = subprocess.run([sys.executable, '-m', 'hermes_cli.main', '-p', 'boot', 'gateway', 'ensure',+ '--json', '--timeout', '90'], cwd=ROOT, env=env, capture_output=True, text=True, timeout=120)+ payload = json.loads(ensured.stdout.strip().splitlines()[-1])+ assert payload['state'] == 'ready', (payload, ensured.stderr)+ # The answering owner is the multiplexer at the root, serving boot as a secondary.+ assert Path(payload['endpoint']['control_home']).resolve() == root.resolve(), payload+ assert served_homes(control(root, 'identify')) == {root.resolve(), boot.resolve()}+ assert not (boot / 'gateway.pid').exists() and not (boot / 'gateway.sock').exists()+ finally:+ kill_recorded_gateway(root)+ # A fresh multiplexer boot reserves every profile: nothing stray owns boot's home.+ proc, desc = start_daemon(mux)+ try:+ assert served_homes(desc) == {root.resolve(), boot.resolve()}+ assert 'Cannot reserve gateway profiles' not in tail(mux)+ stop(proc)+ finally:+ stop(proc, expect=None)++def test_broken_secondary_state_db_parks_only_that_profile(mux):root = mux['root']broken = root / 'profiles' / 'broken'