fix(peer): live-verified cross-gateway bot DMs — session shape + virtual-model replay

Two bugs found by a REAL two-gateway live test (two isolated HERMES_HOMEs,
bravo running the api_server platform, alpha's agent autonomously running
`hermes peer dm` from its Bot Chat protocol; reply relayed correctly and
persisted in bravo's canonical Bot Chat):

1. peer dm parsed the session-create response flat, but api_server wraps
   the row: {"object": "hermes.session", "session": {...}} — every first DM
   to a fresh peer failed with "Peer did not return a session id" (and the
   orphaned Bot Chat then 400'd retries with duplicate-title). Parse the
   wrapped shape; the test fake now mirrors the real response shape so this
   class can't pass green again.

2. api_server: a session created with no model persists the advertised
   virtual model ("hermes-agent") on the row; session chat then replayed it
   as a REAL model id and the provider 400'd ("hermes-agent is not a valid
   model ID"). _request_agent_overrides already filters the virtual model
   for per-request bodies — apply the same filter to the stored session
   model at both chat sites (sync + stream), so it means "gateway default"
   exactly like the request-body path.

Live E2E transcript (bravo's Bot Chat, via /api/sessions/{id}/messages):
  user:      Message from 🤖 alpha (@alpha): What is your callsign?
  assistant: CALLSIGN-BRAVO-7
peer cmd unit suite 10/10 with the corrected fake.
This commit is contained in:
Teknium
2026-08-17 16:25:47 -07:00
parent 6f2dbf3a05
commit 189de16280
4 changed files with 44 additions and 4 deletions
+17 -2
View File
@@ -2320,6 +2320,21 @@ class APIServerAdapter(BasePlatformAdapter):
return None
return self._model_routes.get(model_alias)
def _stored_session_model(self, session: Any) -> Optional[str]:
"""The model persisted on a session row, minus the virtual alias.
The advertised virtual model (usually ``hermes-agent``) means "use
the gateway default". Session creation persists it when the client
sent no model, and replaying it upstream as a raw provider model id
400s ("hermes-agent is not a valid model ID") — the same filter
``_request_agent_overrides`` applies to per-request bodies. One
resolver for both session-chat sites (sync + stream).
"""
stored = session.get("model") if isinstance(session, dict) else None
if not stored or stored == self._model_name:
return None
return stored
@staticmethod
def _clean_runtime_id(value: Any, *, max_len: int = 200) -> str:
if value is None:
@@ -3740,7 +3755,7 @@ class APIServerAdapter(BasePlatformAdapter):
if runtime_request.get("model_options"):
agent_overrides["model_options"] = runtime_request["model_options"]
else:
stored_model = session.get("model") if isinstance(session, dict) else None
stored_model = self._stored_session_model(session)
stored_route = self._resolve_route(stored_model)
route = stored_route or self._resolve_route(body.get("model"))
session_model = stored_model if (stored_model and stored_route is None) else None
@@ -3850,7 +3865,7 @@ class APIServerAdapter(BasePlatformAdapter):
if runtime_request.get("model_options"):
agent_overrides["model_options"] = runtime_request["model_options"]
else:
stored_model = session.get("model") if isinstance(session, dict) else None
stored_model = self._stored_session_model(session)
stored_route = self._resolve_route(stored_model)
route = stored_route or self._resolve_route(body.get("model"))
session_model = stored_model if (stored_model and stored_route is None) else None
+3 -1
View File
@@ -126,7 +126,9 @@ def _ensure_bot_chat(base: str, key: str) -> str:
method="POST",
body={"title": BOT_CHAT_TITLE, "source": "bot_peer_dm"},
)
session_id = str(created.get("id") or created.get("session_id") or "")
# Real api_server wraps the row: {"object": "hermes.session", "session": {...}}.
session = created.get("session") if isinstance(created.get("session"), dict) else created
session_id = str(session.get("id") or session.get("session_id") or "")
if not session_id:
raise RuntimeError("Peer did not return a session id for the new Bot Chat")
return session_id
+21
View File
@@ -2592,6 +2592,27 @@ class TestModelRoutesAgentCreation:
assert captured["api_key"] == "sk-session"
class TestStoredSessionModelFilter:
"""A session row that persisted the advertised virtual model must read as
"no stored model" — replaying "hermes-agent" upstream 400s. Found live
(Aug 2026): the first cross-gateway `hermes peer dm` against a fresh
api_server failed every turn with "hermes-agent is not a valid model ID".
"""
def test_virtual_model_is_filtered(self):
adapter = _make_routing_adapter({})
assert adapter._stored_session_model({"model": adapter._model_name}) is None
def test_real_model_passes_through(self):
adapter = _make_routing_adapter({})
assert adapter._stored_session_model({"model": "google/gemini-3.7-flash"}) == "google/gemini-3.7-flash"
def test_missing_or_bad_shapes(self):
adapter = _make_routing_adapter({})
assert adapter._stored_session_model({}) is None
assert adapter._stored_session_model(None) is None
# ---------------------------------------------------------------------------
# Event-loop offloading for synchronous SessionDB calls (P1)
# ---------------------------------------------------------------------------
+3 -1
View File
@@ -116,7 +116,9 @@ class _FakePeer(BaseHTTPRequestHandler):
if self.path == "/api/sessions":
type(self).sessions.append("bc_1")
return self._json({"id": "bc_1", "title": body.get("title")}, 201)
# REAL api_server create shape: the row is wrapped under "session"
# (verified live Aug 2026 — a flat fake hid a parser bug).
return self._json({"object": "hermes.session", "session": {"id": "bc_1", "title": body.get("title")}}, 201)
if self.path.startswith("/api/sessions/") and self.path.endswith("/chat"):
type(self).chats.append(body.get("message"))