The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(peer): a peer's default bot is reachable by its @hermes handle
Teknium7a2bf1af9fef
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.pyindex 8e49568f526..44b82c50856 100644--- a/gateway/platforms/api_server.py+++ b/gateway/platforms/api_server.py@@ -1501,29 +1501,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):# -- Multi-profile multiplexing (/p/<profile>/...) --------------------------------def _resolve_request_profile(self, request: web.Request):- """Resolve + validate the /p/<profile>/ prefix: ``None`` (no prefix, or multiplexing- off and the prefix names this gateway's own profile), the served profile name, or- ``_PROFILE_REJECTED`` (-> 404). Fail closed: a foreign prefix must never be ignored."""- profile = (request.match_info.get("profile") or "").strip()- if not profile:- return None- cfg = getattr(self.gateway_runner, "config", None)- if not getattr(cfg, "multiplex_profiles", False):- return None if _prefix_names_served_profile(profile) else _PROFILE_REJECTED- try:- from hermes_cli.profiles import profiles_to_serve- served = {name for name, _ in profiles_to_serve(multiplex=True)}- except Exception:- return _PROFILE_REJECTED- if profile in served:- return profile- # 'hermes' is the reserved @handle a peer always advertises its default profile- # under (apps/desktop hermes-bots/data.ts:botHandle), never a real profile name.- # Addressing the mirror by that handle must reach the same profile it labels- # (#118076), unless a profile is actually named 'hermes'.- if profile == "hermes" and "hermes" not in served and "default" in served:- return "default"- return _PROFILE_REJECTED+ from gateway.platforms.api_server_profile_prefix import resolve_request_profile+ return resolve_request_profile(self, request)@staticmethoddef _profile_scope(profile: Optional[str]):diff --git a/gateway/platforms/api_server_profile_prefix.py b/gateway/platforms/api_server_profile_prefix.pynew file mode 100644index 00000000000..89a04341eca--- /dev/null+++ b/gateway/platforms/api_server_profile_prefix.py@@ -0,0 +1,38 @@+"""``/p/<profile>/`` prefix resolution for the shared api_server listener."""+import logging+from typing import Any++logger = logging.getLogger(__name__)++# The @handle Bot Mode advertises a host's default profile under (apps/desktop hermes-bots+# ``botHandle``); it is a reserved profile name, so it never names a profile of its own.+DEFAULT_PROFILE_HANDLE = "hermes"+++def resolve_request_profile(adapter: Any, request: Any):+ """Resolve + validate the /p/<profile>/ prefix: ``None`` (no prefix, or multiplexing off and the+ prefix names this gateway's own profile), the served profile name, or ``_PROFILE_REJECTED``+ (-> 404). Fail closed: a foreign prefix must never be ignored. The default profile's handle+ ``hermes`` reaches the default profile, because peers address the mirror by the handle their+ roster shows (#118076); a served profile literally named ``hermes`` still wins."""+ from gateway.platforms.api_server import _PROFILE_REJECTED, _prefix_names_served_profile+ profile = (request.match_info.get("profile") or "").strip()+ if not profile:+ return None+ alias = profile == DEFAULT_PROFILE_HANDLE+ cfg = getattr(adapter.gateway_runner, "config", None)+ if not getattr(cfg, "multiplex_profiles", False):+ served = _prefix_names_served_profile(profile) or (alias and _prefix_names_served_profile("default"))+ return None if served else _PROFILE_REJECTED+ try:+ from hermes_cli.profiles import profiles_to_serve+ served = {name for name, _ in profiles_to_serve(multiplex=True)}+ except Exception:+ # Fail closed: a prefix this gateway cannot prove it serves is a 404.+ logger.warning("Cannot list served profiles for /p/%s/", profile, exc_info=True)+ return _PROFILE_REJECTED+ if profile in served:+ return profile+ if alias and "default" in served:+ return "default"+ return _PROFILE_REJECTEDdiff --git a/hermes_cli/subcommands/peer.py b/hermes_cli/subcommands/peer.pyindex ede98d3766e..22979b1e78b 100644--- a/hermes_cli/subcommands/peer.py+++ b/hermes_cli/subcommands/peer.py@@ -91,7 +91,11 @@ def _request(def _base_url(peer: dict, profile: str | None) -> str:url = str(peer.get("url") or "").rstrip("/")if profile:- # Multiplex mirror: same handlers, scoped to the named profile.+ # Multiplex mirror: same handlers, scoped to the named profile. The mirror is keyed by+ # profile name, and Bot Mode shows a peer's default profile as @hermes (#118076); an+ # older peer has no alias for it on its side.+ if profile.lower() == "hermes":+ profile = "default"return f"{url}/p/{urllib.parse.quote(profile, safe='')}"return urldiff --git a/tests/hermes_cli/test_peer_cmd.py b/tests/hermes_cli/test_peer_cmd.pyindex 1072aa36cca..de9badd2ac3 100644--- a/tests/hermes_cli/test_peer_cmd.py+++ b/tests/hermes_cli/test_peer_cmd.py@@ -41,6 +41,11 @@ def test_base_url_bare_and_profile():assert peer_cmd._base_url(peer, "researcher") == "http://spark.lan:8377/p/researcher"+def test_base_url_addresses_the_default_handle_by_profile_name():+ # Bot Mode shows a peer's default profile as @hermes; an older peer serves it only at /p/default (#118076).+ assert peer_cmd._base_url({"url": "http://spark.lan:8377"}, "Hermes") == "http://spark.lan:8377/p/default"++# ── registry round-trip (isolated config) ────────────────────────────────────