The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): adopt compression tip for explicit-header chat continuations (#98619)
liuhao1024705bcdcf3747
diff --git a/gateway/platforms/api_server_openai_routes.py b/gateway/platforms/api_server_openai_routes.pyindex 9adbd451d3e..56e8da2a782 100644--- a/gateway/platforms/api_server_openai_routes.py+++ b/gateway/platforms/api_server_openai_routes.py@@ -473,6 +473,13 @@ class OpenAICompatRoutesMixin:try:db = await self._ensure_session_db_async()if db is not None:+ # #98619/#13437: a client-addressed id from before a compression rotation+ # must adopt the live continuation tip — history loads from it, the turn and+ # the wake target bind it, and a detached delegation delivery row persisted+ # on the tip is what this continuation consumes. Same canonical resolution+ # the delivery writer (gateway/wake.py) and /v1/runs use; fails open.+ from gateway.platforms.api_server_runs import _resolve_live_session_id+ session_id = await _resolve_live_session_id(self, provided_session_id)history = await asyncio.to_thread(db.get_messages_as_conversation, session_id)except Exception as e:logger.warning("Failed to load session history for %s: %s", session_id, e)@@ -530,9 +537,14 @@ class OpenAICompatRoutesMixin:agent_task, agent_ref = self._spawn_stream_agent(_stream_q, tool_start_callback=_on_tool_start,tool_complete_callback=_on_tool_complete, **run_kwargs)+ # #13437 identity contract: an explicit-header client keeps addressing the id it+ # sent; the response echoes that stable id while reads/writes adopt the live tip,+ # so a rotation mid-turn (after these headers are prepared) never changes what the+ # client should send next — it re-sends the same id and the tip resolution above+ # finds whatever session is live by then.return await self._write_sse_chat_completion(request, completion_id, model_name, created, _stream_q,- agent_task, agent_ref, session_id=session_id,+ agent_task, agent_ref, session_id=(provided_session_id or session_id),gateway_session_key=gateway_session_key)async def _compute_completion():@@ -549,7 +561,10 @@ class OpenAICompatRoutesMixin:if err_msg:err_msg = _redact_api_error_text(err_msg)finish_reason = _finish_reason(completed, is_partial, is_failed, err_msg)- response_headers = {"X-Hermes-Session-Id": result.get("session_id", session_id)}+ # Same #13437 identity contract as the SSE path: an explicit-header client is echoed+ # the stable id it sent; a fingerprint-derived (header-less) turn keeps reporting the+ # id the agent actually resolved, so headerless clients still learn where the turn went.+ response_headers = {"X-Hermes-Session-Id": (provided_session_id or result.get("session_id", session_id))}if gateway_session_key:response_headers["X-Hermes-Session-Key"] = gateway_session_key# Hard fail (no usable text AND a real failure) -> 502 OpenAI error envelope so SDKdiff --git a/tests/gateway/test_api_server.py b/tests/gateway/test_api_server.pyindex be94faa7ad8..90e76372c3f 100644--- a/tests/gateway/test_api_server.py+++ b/tests/gateway/test_api_server.py@@ -2359,6 +2359,8 @@ class TestSessionIdHeader:]mock_db = MagicMock()mock_db.get_messages_as_conversation.return_value = db_history+ # Non-rotated control: the canonical tip resolver resolves the id to itself.+ mock_db.resolve_resume_session_id.side_effect = lambda sid: sidauth_adapter._session_db = mock_dbapp = _create_app(auth_adapter)async with TestClient(TestServer(app)) as cli:@@ -2419,6 +2421,103 @@ class TestSessionIdHeader:assert resp.status == 200assert mock_run.call_args.kwargs["wake_capable"] == ""+ @staticmethod+ def _rotated_session_db(tmp_path):+ """Real SessionDB with a compression-rotated pair: closed parent ``parent-session``+ and its live continuation ``child-session`` — plus a late async-delegation delivery+ row persisted on the tip through the stale origin id (#98619 e2e shape)."""+ from hermes_state import SessionDB++ db = SessionDB(db_path=tmp_path / "state.db")+ db.create_session("parent-session", source="api_server")+ db.append_message("parent-session", "user", "run the batch")+ db.end_session("parent-session", "compression")+ db.create_session(+ "child-session", source="api_server", parent_session_id="parent-session"+ )+ return db++ @pytest.mark.asyncio+ async def test_provided_session_id_adopts_compression_tip_for_history_bind_and_wake(+ self, auth_adapter, tmp_path+ ):+ """#98619/#13437: a client re-sending a pre-rotation X-Hermes-Session-Id must read the+ live tip's history (including a detached delegation delivery row persisted there),+ bind the turn and the wake target to the tip, keep wake capability for the explicit+ header, and still be echoed the stable client id it sent."""+ from gateway.wake import persist_delegation_delivery++ db = self._rotated_session_db(tmp_path)+ auth_adapter._session_db = db+ # The detached completion arrives after the rotation, addressed to the captured+ # (now-stale) origin id — the delivery writer adopts the tip, as at runtime.+ await persist_delegation_delivery(+ auth_adapter,+ text="[delegated task complete] rotated result",+ session_id="parent-session",+ evt={"type": "async_delegation"},+ )+ mock_result = {"final_response": "OK", "session_id": "child-session",+ "messages": [], "api_calls": 1}+ app = _create_app(auth_adapter)+ async with TestClient(TestServer(app)) as cli:+ with patch.object(auth_adapter, "_run_agent", new_callable=AsyncMock) as mock_run:+ mock_run.return_value = (mock_result, {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0})++ resp = await cli.post(+ "/v1/chat/completions",+ headers={"X-Hermes-Session-Id": "parent-session", "Authorization": "Bearer sk-secret"},+ json={"model": "hermes-agent", "messages": [{"role": "user", "content": "follow up"}]},+ )++ assert resp.status == 200+ call_kwargs = mock_run.call_args.kwargs+ # Read, turn, and wake target all select the live tip the delivery row landed on.+ assert call_kwargs["session_id"] == "child-session"+ assert call_kwargs["wake_capable"] == "1"+ assert any("rotated result" in m.get("content", "") for m in call_kwargs["conversation_history"])+ # Identity contract: the client is echoed the stable id it sent, not the tip and not+ # the turn's reported session_id.+ assert resp.headers.get("X-Hermes-Session-Id") == "parent-session"++ @pytest.mark.asyncio+ async def test_streamed_session_id_header_adopts_tip_and_echoes_stable_id(+ self, auth_adapter, tmp_path+ ):+ """Streaming half of the interlock: SSE response headers are prepared before the turn+ runs, so they must carry the stable client id (a rotation after prepare never changes+ what the client should re-send), while the streamed turn still runs against the tip."""+ db = self._rotated_session_db(tmp_path)+ auth_adapter._session_db = db++ async def _mock_run_agent(**kwargs):+ cb = kwargs.get("stream_delta_callback")+ if cb:+ cb("ok")+ return (+ {"final_response": "ok", "session_id": "child-session", "messages": [], "api_calls": 1},+ {"input_tokens": 1, "output_tokens": 1, "total_tokens": 2},+ )++ app = _create_app(auth_adapter)+ async with TestClient(TestServer(app)) as cli:+ with patch.object(auth_adapter, "_run_agent", side_effect=_mock_run_agent) as mock_run:+ resp = await cli.post(+ "/v1/chat/completions",+ headers={"X-Hermes-Session-Id": "parent-session", "Authorization": "Bearer sk-secret"},+ json={"model": "hermes-agent",+ "messages": [{"role": "user", "content": "follow up"}],+ "stream": True},+ )+ assert resp.status == 200+ assert resp.headers.get("X-Hermes-Session-Id") == "parent-session"+ body = await resp.text()++ assert "data: " in body+ call_kwargs = mock_run.call_args.kwargs+ assert call_kwargs["session_id"] == "child-session"+ assert call_kwargs["wake_capable"] == "1"+# ---------------------------------------------------------------------------# X-Hermes-Session-Key header (long-term memory scoping)