Skip to content

The agent that grows with you

fix(gateway): name an elevated gateway for a multiplexed profile too

unsupportedpastels0b5505c0a43f
diff --git a/hermes_cli/gateway_runtime.py b/hermes_cli/gateway_runtime.py
index 638f33930f6..f434eb95985 100644
--- a/hermes_cli/gateway_runtime.py
+++ b/hermes_cli/gateway_runtime.py
@@ -198,6 +198,14 @@ def _parked_by_multiplexer(payload: dict, home: Path) -> GatewayDiscovery | None
return GatewayDiscovery("inaccessible", reason_code="profile_parked", detail=str(reason)[:500] or None)
+def _refused_owner_verdict(exc: PermissionError) -> GatewayDiscovery | None:
+ """The named refusal when the client proved the boundary from the control pipe, else None."""
+ from gateway.runtime_bootstrap_windows import RefusedOwner
+ if isinstance(exc, RefusedOwner):
+ return GatewayDiscovery("inaccessible", reason_code=exc.reason_code, detail=str(exc))
+ return None
+
+
def _served_by_multiplexer(home: Path, *, timeout: float) -> GatewayDiscovery | None:
"""A served secondary has no socket of its own: the default multiplexer's control socket
answers for it, and its ``identify`` lists the home under ``served_profiles``. Only the
@@ -209,7 +217,10 @@ def _served_by_multiplexer(home: Path, *, timeout: float) -> GatewayDiscovery |
return None
try:
payload = query_identify(root, timeout=timeout)
- except (FileNotFoundError, ConnectionRefusedError, TimeoutError, DiscoveryError, PermissionError,
+ except PermissionError as exc:
+ # The multiplexer's pipe proved the boundary: name it for the secondary too (#140706).
+ return _refused_owner_verdict(exc)
+ except (FileNotFoundError, ConnectionRefusedError, TimeoutError, DiscoveryError,
OSError, ValueError, TypeError):
return None
result = _endpoint(payload, home, control_home=root)
@@ -296,10 +307,8 @@ def discover_gateway_endpoint(profile_home: str | Path, *, timeout: float = 2.0)
# The code stays bounded; the detail names the local node, its owner or mode, and the fix.
return GatewayDiscovery("inaccessible", reason_code=exc.reason, detail=exc.bounded_detail(500))
except PermissionError as exc:
- from gateway.runtime_bootstrap_windows import RefusedOwner
- if isinstance(exc, RefusedOwner): # the client proved the boundary from the control pipe (#140706)
- return GatewayDiscovery("inaccessible", reason_code=exc.reason_code, detail=str(exc))
- return _refused(home)
+ named = _refused_owner_verdict(exc) # the client proved the boundary from the control pipe (#140706)
+ return named if named is not None else _refused(home)
except (OSError, ValueError, TypeError):
# Raw peer data, paths and URLs may contain secrets; report bounded codes.
return GatewayDiscovery("inaccessible", reason_code="invalid_control_peer")
diff --git a/tests/gateway/test_runtime_bootstrap_server_identity.py b/tests/gateway/test_runtime_bootstrap_server_identity.py
index 4bd83fc4791..260f8dfce2b 100644
--- a/tests/gateway/test_runtime_bootstrap_server_identity.py
+++ b/tests/gateway/test_runtime_bootstrap_server_identity.py
@@ -14,6 +14,7 @@ this client's token. ``test_runtime_bootstrap.py`` reads a real pipe's facts on
elevated-server / standard-user-client attach.
"""
import ctypes
+from pathlib import Path
import pytest
@@ -128,6 +129,72 @@ def test_discovery_reports_the_named_refusal_without_any_owner_record(pipe, monk
assert ensure_exit_code(result) == 4
+def served_secondary(pipe, monkeypatch, tmp_path):
+ """A named profile the default multiplexer serves: no pipe of its own, so discovery asks the
+ multiplexer's pipe. *root_refusal* is what that query raises. Every way ensure could start a
+ gateway is recorded instead of run."""
+ from hermes_cli import gateway_runtime_discovery as discovery
+ from hermes_cli import gateway_runtime_multiplex as multiplex
+ from hermes_cli import gateway_runtime_service as service
+ from hermes_cli import gateway_runtime_start as start
+ root = tmp_path / "root"
+ home = root / "profiles" / "work"
+ home.mkdir(parents=True)
+ starts = []
+
+ def install(root_refusal):
+ def query_identify(target, **_k):
+ if Path(target) != root:
+ raise FileNotFoundError(target) # the secondary's own pipe is absent
+ root_refusal()
+ monkeypatch.setattr(discovery, "query_identify", query_identify)
+ monkeypatch.setattr(multiplex, "multiplexer_root_for", lambda _home: root)
+ monkeypatch.setattr(multiplex, "implied_host_root", lambda _home: None)
+ monkeypatch.setattr(multiplex, "multiplexer_serves_home", lambda _home: None)
+ monkeypatch.setattr(service, "discover_existing_gateway_service", lambda *_a, **_k: None)
+
+ def record(*args, **_k):
+ starts.append(args)
+ raise TimeoutError # ends ensure as a deadline instead of a real start
+ monkeypatch.setattr(service, "start_existing_gateway_service", record)
+ monkeypatch.setattr(start, "spawn_unmanaged_gateway", record)
+ return home, starts
+ pipe(process_sid=None, owner=ADMINS)
+ return install
+
+
+def test_a_served_secondary_discovers_its_elevated_multiplexer_by_name(served_secondary):
+ """The refusal from the multiplexer's pipe is the secondary's discovery verdict, with the
+ same reason and remedy direct discovery reports."""
+ from hermes_cli import gateway_runtime as runtime
+ home, _starts = served_secondary(lambda: native._peer_subject(HANDLE, server=True))
+ found = runtime.discover_gateway_endpoint(home, timeout=1)
+ assert (found.state, found.reason_code, found.detail) == (
+ "inaccessible", "gateway_elevated", native.refused_owner_remedy("gateway_elevated", SERVER_PID, 1))
+
+
+def test_ensuring_a_served_secondary_returns_the_named_refusal_and_starts_nothing(served_secondary):
+ """Ensure answers at once instead of waiting out the deadline, and never starts a gateway."""
+ import time
+ from hermes_cli import gateway_runtime as runtime
+ home, starts = served_secondary(lambda: native._peer_subject(HANDLE, server=True))
+ began = time.monotonic()
+ ensured = runtime.ensure_gateway_runtime(home, timeout=5)
+ assert (ensured.state, ensured.reason_code, ensured.detail) == (
+ "inaccessible", "gateway_elevated", native.refused_owner_remedy("gateway_elevated", SERVER_PID, 1))
+ assert starts == []
+ assert time.monotonic() - began < 2
+
+
+def test_a_served_secondary_keeps_ignoring_any_other_multiplexer_refusal(served_secondary):
+ """Unchanged: a generic refusal from the multiplexer is not service, so the secondary's own
+ (absent) state answers."""
+ from hermes_cli import gateway_runtime as runtime
+ home, _starts = served_secondary(lambda: (_ for _ in ()).throw(PermissionError("no token")))
+ assert runtime.discover_gateway_endpoint(home, timeout=1).state == "absent"
+
+
# -- what must stay refused, never attached and never named -------------------------------------