Skip to content

The agent that grows with you

fix(api): a busy store during the header-less session lookup is a retryable 503

unsupportedpastels4154648d3f3e
diff --git a/gateway/platforms/api_server_openai_routes.py b/gateway/platforms/api_server_openai_routes.py
index bb7bd81de5e..9b6c1bfad69 100644
--- a/gateway/platforms/api_server_openai_routes.py
+++ b/gateway/platforms/api_server_openai_routes.py
@@ -1024,8 +1024,9 @@ class OpenAICompatRoutesMixin:
async def _implicit_chat_session(self, request: web.Request, session_id: str) -> tuple:
"""A derived (header-less) id whose session was deleted names a new conversation with the
same opening: the owning profile's store picks its generation (``implicit_api_session``).
- -> ``(session_id, None)`` or ``(session_id, 500 response)``: a store that cannot answer
- gets the same OpenAI error envelope as ``_run_idempotent``, before either response starts."""
+ -> ``(session_id, None)`` or ``(session_id, error response)``, before either response starts:
+ a refusal keeps its reason and a busy or unreadable store is a retryable 503
+ ``storage_unavailable`` (``store_error_response``); anything else is the OpenAI 500."""
if getattr(self.gateway_runner, 'session_authority', None) is None:
return session_id, None
from gateway.session_api import implicit_api_session
@@ -1033,11 +1034,12 @@ class OpenAICompatRoutesMixin:
authority = active_authority(self.gateway_runner)
if authority is None:
return session_id, None # admission refuses the unserved profile with its own verdict
+ import sqlite3
from hermes_state_runtime import RuntimeStoreError
try:
return await asyncio.to_thread(implicit_api_session, authority.db, session_id,
self._chat_request_id(request)), None
- except RuntimeStoreError as exc:
+ except (RuntimeStoreError, sqlite3.Error, OSError) as exc:
from gateway.platforms.api_server import _openai_error
from gateway.platforms.api_server_authority_runs import store_error_response
return session_id, store_error_response(_openai_error, exc)
diff --git a/tests/gateway/test_api_implicit_session_after_delete.py b/tests/gateway/test_api_implicit_session_after_delete.py
index 2e1de8f0be0..bd055dfbf01 100644
--- a/tests/gateway/test_api_implicit_session_after_delete.py
+++ b/tests/gateway/test_api_implicit_session_after_delete.py
@@ -5,6 +5,8 @@ session's id stays retired for good. A new conversation with the same opening mo
generation of that opening instead of being refused forever; its later turns keep landing there.
An explicit X-Hermes-Session-Id and a spent Idempotency-Key are never moved.
"""
+import json
+
import pytest
from aiohttp import web
from aiohttp.test_utils import TestClient, TestServer
@@ -127,6 +129,8 @@ async def test_an_idempotency_key_retry_keeps_its_deleted_session(api, owner, se
@pytest.mark.asyncio
@pytest.mark.parametrize('stream', [False, True], ids=['json', 'stream'])
async def test_a_store_that_cannot_answer_the_lookup_returns_an_openai_error(api, owner, served, monkeypatch, stream):
+ """A busy or unreadable store is retryable: 503 ``storage_unavailable`` with Retry-After, the
+ same verdict ``store_error_response`` gives every other admission door, never the raw error."""
import sqlite3
def unreadable(*_a, **_k):
@@ -137,10 +141,11 @@ async def test_a_store_that_cannot_answer_the_lookup_returns_an_openai_error(api
response = await client.post('/v1/chat/completions',
json={'model': 'hermes-agent', 'messages': _OPENING, 'stream': stream},
headers={'Authorization': 'Bearer ' + _API_KEY})
- assert response.status == 500
assert response.content_type == 'application/json', await response.text()
payload = await response.json()
- assert payload['error']['type'] == 'server_error', payload
+ assert response.status == 503 and payload['error']['code'] == 'storage_unavailable', payload
+ assert response.headers.get('Retry-After') == '1'
+ assert 'disk I/O error' not in json.dumps(payload)
assert served == []