Skip to content

The agent that grows with you

fix(api-server): namespace the derived chat session id by the routed profile

liuhao10242b3b867db579
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index d89c44006f7..9db30feba89 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -1057,10 +1057,15 @@ def _make_request_fingerprint(body: Dict[str, Any], keys: List[str]) -> str:
return hashlib.sha256(repr(subset).encode("utf-8")).hexdigest()
-def _derive_chat_session_id(system_prompt: Optional[str], first_user_message: str) -> str:
+def _derive_chat_session_id(system_prompt: Optional[str], first_user_message: str,
+ profile: Optional[str] = None) -> str:
"""Stable session id from the system prompt + first user message (constant across all
- turns of an Open WebUI-style conversation), so one Hermes session/sandbox is reused."""
- seed = f"{system_prompt or ''}\n{first_user_message}"
+ turns of an Open WebUI-style conversation), so one Hermes session/sandbox is reused.
+ ``profile`` namespaces the fingerprint: the id keys process-global resources (the session
+ store; the persistent Docker sandbox labeled ``hermes-task-id``), so under
+ ``gateway.multiplex_profiles`` two header-less conversations that open with identical text
+ on different profiles must not land on one session/container (#123989)."""
+ seed = f"{profile or 'default'}\0{system_prompt or ''}\n{first_user_message}"
digest = hashlib.sha256(seed.encode("utf-8")).hexdigest()[:16]
return f"api-{digest}"
diff --git a/gateway/platforms/api_server_openai_routes.py b/gateway/platforms/api_server_openai_routes.py
index 536b6c654eb..a050759d893 100644
--- a/gateway/platforms/api_server_openai_routes.py
+++ b/gateway/platforms/api_server_openai_routes.py
@@ -633,7 +633,7 @@ class OpenAICompatRoutesMixin:
async def _handle_chat_completions(self, request: "web.Request") -> "web.Response":
"""POST /v1/chat/completions — OpenAI Chat Completions format."""
from gateway.platforms.api_server import (
- ThreadSafeAsyncQueue, _chat_usage_payload, _coerce_request_bool,
+ ThreadSafeAsyncQueue, _api_request_profile, _chat_usage_payload, _coerce_request_bool,
_content_has_visible_payload, _derive_chat_session_id, _error_response, _invalid_request,
_multimodal_validation_error, _normalize_chat_content, _normalize_multimodal_content,
_openai_error, _redact_api_error_text, _resolve_media_to_data_urls)
@@ -712,10 +712,13 @@ class OpenAICompatRoutesMixin:
history = []
else:
# Stable id from the conversation fingerprint so Open WebUI-style clients map onto
- # one Hermes session.
+ # one Hermes session. Namespaced by the routed profile: under multiplexing the id
+ # keys process-global session stores and persistent sandboxes, and two header-less
+ # conversations opening with identical text on different profiles must not share
+ # one (#123989).
first_user = next(
(cm.get("content", "") for cm in conversation_messages if cm.get("role") == "user"), "")
- session_id = _derive_chat_session_id(system_prompt, first_user)
+ session_id = _derive_chat_session_id(system_prompt, first_user, _api_request_profile.get())
completion_id = f"chatcmpl-{uuid.uuid4().hex[:29]}"
model_name = body.get("model", self._model_name)
created = int(time.time())
diff --git a/tests/gateway/test_api_server.py b/tests/gateway/test_api_server.py
index 2ba16401ac6..ae38c5b460e 100644
--- a/tests/gateway/test_api_server.py
+++ b/tests/gateway/test_api_server.py
@@ -1403,6 +1403,21 @@ class TestDeriveChatSessionId:
b = _derive_chat_session_id("You are a robot.", "Hello")
assert a != b
+ def test_identical_text_on_different_profiles_yields_different_ids(self):
+ """Under multiplexing the id keys process-global session stores and persistent Docker
+ sandboxes (``hermes-task-id``): two header-less conversations opening with the same text
+ on different profiles must not share one session/container (#123989)."""
+ a = _derive_chat_session_id("sys", "hello", "default")
+ b = _derive_chat_session_id("sys", "hello", "research")
+ assert a != b
+
+ def test_profile_none_matches_default_and_is_stable(self):
+ """A standalone listener (no profile-prefix middleware, ContextVar default None) lands on
+ the same namespace as an explicit ``default`` profile, and stays stable across turns."""
+ assert _derive_chat_session_id("sys", "hello") == _derive_chat_session_id("sys", "hello", "default")
+ assert _derive_chat_session_id("sys", "hello", "research") == _derive_chat_session_id(
+ "sys", "hello", "research")
+
# ---------------------------------------------------------------------------
# /v1/responses endpoint