Skip to content

The agent that grows with you

fix(acp): resolve private overlay gateway homes

KoNit-K90ed2494c006
diff --git a/hermes_cli/gateway_runtime.py b/hermes_cli/gateway_runtime.py
index e400ebb4ac7..10c01aaec29 100644
--- a/hermes_cli/gateway_runtime.py
+++ b/hermes_cli/gateway_runtime.py
@@ -65,6 +65,27 @@ def _canonical_home(home: str | Path) -> str:
return os.path.normcase(str(Path(home).expanduser().resolve()))
+def _overlay_owner_home(home: Path) -> Path:
+ """Return the owner named by a private overlay's gateway lock, if it has one."""
+ if os.name == "nt":
+ return home
+ from hermes_cli.gateway_runtime_discovery import DiscoveryError, _private_lock, _private_node
+ lock = home / "gateway.lock"
+ try:
+ # An overlay home intentionally shares only the owner's lock. Do not turn an arbitrary
+ # link into a profile alias: both homes and the target lock must meet ordinary discovery's
+ # private-control checks.
+ _private_node(home, kind="directory", home=True)
+ target = lock.resolve(strict=True)
+ if not lock.is_symlink() or target.name != "gateway.lock":
+ return home
+ _private_node(target.parent, kind="directory", home=True)
+ _private_lock(target)
+ except (FileNotFoundError, OSError, DiscoveryError):
+ return home
+ return target.parent
+
+
def _endpoint(payload: dict, home: Path, control_home: Path | None = None) -> GatewayDiscovery:
if type(payload.get("runtime_protocol")) is not int or payload["runtime_protocol"] != 1:
return GatewayDiscovery("incompatible", reason_code="runtime_protocol")
@@ -198,7 +219,7 @@ def discover_gateway_endpoint(profile_home: str | Path, *, timeout: float = 2.0)
if not math.isfinite(timeout) or timeout <= 0:
raise ValueError("timeout must be finite and positive")
- home = Path(_canonical_home(profile_home))
+ home = _overlay_owner_home(Path(_canonical_home(profile_home)))
try:
return _endpoint(query_identify(home, timeout=timeout), home)
except (FileNotFoundError, ConnectionRefusedError):
@@ -313,7 +334,7 @@ def ensure_gateway_runtime(profile_home: str | Path, *, timeout: float = DEFAULT
if not math.isfinite(timeout) or timeout <= 0:
raise ValueError("timeout must be finite and positive")
deadline = time.monotonic() + timeout
- home = Path(_canonical_home(profile_home))
+ home = _overlay_owner_home(Path(_canonical_home(profile_home)))
requested = False
child = None
child_home, child_offset = home, 0
diff --git a/tests/hermes_cli/test_gateway_runtime_discovery.py b/tests/hermes_cli/test_gateway_runtime_discovery.py
index 920694dd680..cb62a994e19 100644
--- a/tests/hermes_cli/test_gateway_runtime_discovery.py
+++ b/tests/hermes_cli/test_gateway_runtime_discovery.py
@@ -11,6 +11,83 @@ from gateway.control_socket import _home_hash
from hermes_cli.gateway_runtime_discovery import DiscoveryError, home_mode_unsafe, query_identify
+def _ready_owner_payload(home):
+ return {
+ "runtime_protocol": 1,
+ "state": "ready",
+ "served_profiles": [{"home": str(home), "profile_id": str(home)}],
+ "capabilities": ["session-authority-v1"],
+ "authority_epoch": 1,
+ "instance_id": "owner",
+ "api_origin": "http://127.0.0.1:8765",
+ "supervisor": "none",
+ }
+
+
[email protected]("linux", "macos")
+def test_discovery_resolves_a_private_overlay_lock_to_its_owner(tmp_path, monkeypatch):
+ """ACP task homes share the served profile's lock instead of publishing a socket of their own."""
+ import hermes_cli.gateway_runtime as runtime
+ import hermes_cli.gateway_runtime_discovery as discovery
+
+ owner = tmp_path / "owner"
+ overlay = tmp_path / "task-home"
+ owner.mkdir(mode=0o700)
+ overlay.mkdir(mode=0o700)
+ lock = owner / "gateway.lock"
+ lock.touch(mode=0o600)
+ (overlay / "gateway.lock").symlink_to(lock)
+ queried = []
+
+ def identify(home, *, timeout):
+ queried.append(home)
+ return _ready_owner_payload(owner)
+
+ monkeypatch.setattr(discovery, "query_identify", identify)
+ result = runtime.discover_gateway_endpoint(overlay)
+
+ assert result.state == "ready"
+ assert result.endpoint is not None and result.endpoint.profile_id == str(owner)
+ assert queried == [owner]
+
+ checked = []
+
+ def ready(home, *, timeout):
+ checked.append(home)
+ return runtime.GatewayDiscovery("ready")
+
+ monkeypatch.setattr(runtime, "discover_gateway_endpoint", ready)
+ assert runtime.ensure_gateway_runtime(overlay).state == "ready"
+ assert checked == [owner]
+
+
[email protected]("linux", "macos")
+def test_discovery_does_not_follow_an_overlay_lock_from_a_writable_home(tmp_path, monkeypatch):
+ """A task directory another user can replace must not select a gateway owner."""
+ from hermes_cli.gateway_runtime import discover_gateway_endpoint
+ import hermes_cli.gateway_runtime_discovery as discovery
+
+ owner = tmp_path / "owner"
+ overlay = tmp_path / "task-home"
+ owner.mkdir(mode=0o700)
+ overlay.mkdir(mode=0o700)
+ overlay.chmod(0o707)
+ lock = owner / "gateway.lock"
+ lock.touch(mode=0o600)
+ (overlay / "gateway.lock").symlink_to(lock)
+ queried = []
+
+ def identify(home, *, timeout):
+ queried.append(home)
+ return _ready_owner_payload(owner)
+
+ monkeypatch.setattr(discovery, "query_identify", identify)
+ result = discover_gateway_endpoint(overlay)
+
+ assert result.state == "inaccessible"
+ assert queried == [overlay]
+
+
@pytest.mark.platforms("linux")
def test_fallback_pointer_uses_owner_location_and_keeps_identity_checks(tmp_path):
home = tmp_path / 'profile'