Skip to content

The agent that grows with you

fix(peer): never guess /p/default for @hermes on an older peer's generic 404

Teknium7269f5acde2d
diff --git a/gateway/platforms/api_server_profile_prefix.py b/gateway/platforms/api_server_profile_prefix.py
index 4da71630df6..ad3dfed62c0 100644
--- a/gateway/platforms/api_server_profile_prefix.py
+++ b/gateway/platforms/api_server_profile_prefix.py
@@ -7,7 +7,8 @@ logger = logging.getLogger(__name__)
# The @handle Bot Mode advertises a host's default profile under (apps/desktop hermes-bots
# ``botHandle``). New profiles cannot take the name, but a pre-reservation profiles/hermes can exist.
DEFAULT_PROFILE_HANDLE = "hermes"
-# 404 body for a prefix naming no profile here; peers key their @hermes -> /p/default fallback on it.
+# 404 body for a prefix naming no profile here. Older gateways also send it for a 'hermes' profile
+# they do not serve, so peer clients treat it as ambiguous and never reroute @hermes on it.
UNKNOWN_PROFILE_ERROR = "Unknown or unconfigured profile"
diff --git a/hermes_cli/subcommands/peer.py b/hermes_cli/subcommands/peer.py
index 1fda8eac53c..897836c6413 100644
--- a/hermes_cli/subcommands/peer.py
+++ b/hermes_cli/subcommands/peer.py
@@ -115,11 +115,14 @@ def _peer_lacks_profile(exc: Exception) -> bool:
return isinstance(body, dict) and body.get("error") == _UNKNOWN_PROFILE_ERROR
-def _resolve_base(peer: dict, profile: str | None, key: str) -> str:
+def _resolve_base(peer: dict, profile: str | None, key: str, peer_name: str = "") -> str:
"""The mirror a target addresses. Bot Mode shows a peer's default profile as @hermes
- (#118076), but a peer may also serve a real pre-reservation profile named ``hermes``, which
- must keep winning — so ``/p/hermes`` is tried first and ``/p/default`` is used only when the
- peer itself answers that it has no such profile (an older peer has no alias on its side)."""
+ (#118076); an upgraded peer aliases ``/p/hermes`` to its default itself, unless it has a real
+ pre-reservation profile named ``hermes``, which keeps the handle. The client never reroutes on
+ its own: an older peer answers the same generic 404 for "no such profile" and for a real
+ ``hermes`` profile it does not serve (parked, standalone), so guessing ``/p/default`` there
+ delivers into the wrong agent. That ambiguous answer becomes an error naming the explicit
+ ``<peer>/default`` address instead."""
base = _base_url(peer, profile)
if profile != DEFAULT_PROFILE_HANDLE:
return base
@@ -127,7 +130,12 @@ def _resolve_base(peer: dict, profile: str | None, key: str) -> str:
_request(f"{base}/health", key)
except (urllib.error.URLError, TimeoutError, OSError, RuntimeError) as exc:
if _peer_lacks_profile(exc):
- return _base_url(peer, "default")
+ name = peer_name or "<peer>"
+ raise LookupError(
+ f"Peer '{name}' serves no '{DEFAULT_PROFILE_HANDLE}' profile, and its gateway is too "
+ f"old to say whether '{DEFAULT_PROFILE_HANDLE}' means its default profile or a "
+ f"'{DEFAULT_PROFILE_HANDLE}' profile it does not serve. Address its default bot as "
+ f"{name}/default, or update the peer's hermes-agent.") from exc
return base
@@ -190,7 +198,7 @@ def _parse_target(target: str) -> tuple[str, str | None]:
raise ValueError(f"Invalid agent/profile name: {profile!r}")
if profile:
# Profile ids are lowercase (normalize_profile_name); 'box/Hermes' must address the
- # peer's real 'hermes' profile, never reach the /p/default fallback by case alone.
+ # peer's real 'hermes' profile on every peer, including those that compare prefixes exactly.
from hermes_cli.profiles import normalize_profile_name
profile = normalize_profile_name(profile)
return peer, profile
@@ -449,7 +457,11 @@ def cmd_peer(args) -> int:
except (LookupError, PermissionError) as exc:
print(str(exc), file=sys.stderr)
return 1
- base = _resolve_base(peer, profile, key)
+ try:
+ base = _resolve_base(peer, profile, key, peer_name)
+ except LookupError as exc:
+ print(str(exc), file=sys.stderr)
+ return 1
if action in {"status", "stop"}:
return _peer_run_ctl(args, action, peer_name, profile, base, key)
message = _message_from_args(args)
diff --git a/tests/hermes_cli/test_peer_cmd.py b/tests/hermes_cli/test_peer_cmd.py
index cd2ec0f3fef..6428357844b 100644
--- a/tests/hermes_cli/test_peer_cmd.py
+++ b/tests/hermes_cli/test_peer_cmd.py
@@ -84,15 +84,17 @@ _NOT_SERVED = {"error": {"message": "Profile 'hermes' exists on this host but th
@pytest.mark.parametrize("served,probe_status,refusal,expected", [
({"default", "hermes"}, 200, None, "hermes"), # a real legacy 'hermes' profile keeps precedence
- ({"default"}, 200, None, "default"), # the peer says it has none: @hermes is its default
+ # An older peer's generic 404 also covers a real 'hermes' profile it parks or runs standalone:
+ # ambiguous, so it is an error naming box/default, never a guessed delivery into the default.
+ ({"default"}, 200, None, None),
({"default", "hermes"}, 503, None, "hermes"), # a transient failure is no verdict, so never reroute
({"default"}, 200, _NOT_SERVED, None), # it has one but does not serve it: an error, not default
])
def test_hermes_handle_reaches_the_profile_the_peer_serves(
monkeypatch, capsys, served, probe_status, refusal, expected):
- """``box/hermes`` names the peer's real 'hermes' profile when it has one, and its default
- profile (Bot Mode's @hermes handle, #118076) only when the peer answers it has no such profile;
- dm, status and stop all address the same mirror."""
+ """``box/hermes`` names the peer's real 'hermes' profile when it has one; the client never
+ reroutes it to /p/default on its own (an upgraded peer aliases the handle server-side); dm,
+ status and stop all address the same mirror."""
_ProfilePeer.served, _ProfilePeer.probe_status, _ProfilePeer.hits = served, probe_status, []
_ProfilePeer.refusal = refusal or {"error": "Unknown or unconfigured profile"}
server = HTTPServer(("127.0.0.1", 0), _ProfilePeer)
@@ -107,9 +109,11 @@ def test_hermes_handle_reaches_the_profile_the_peer_serves(
]
finally:
server.shutdown()
- out = [json.loads(line) for line in capsys.readouterr().out.splitlines()]
+ captured = capsys.readouterr()
+ out, err = [json.loads(line) for line in captured.out.splitlines()], captured.err
if expected is None:
assert (replies, out) == ([1, 1, 1], [])
+ assert "box/default" in err or "Profile 'hermes' exists" in err
assert {hit.split("/")[2] for hit in _ProfilePeer.hits} == {"hermes"}
return
assert replies == [0, 0, 0]