The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(api): a busy store during a Responses or Runs chain lookup is a retryable 503
unsupportedpastels8a02b0ea1035
diff --git a/gateway/platforms/api_server_authority_runs.py b/gateway/platforms/api_server_authority_runs.pyindex 1899ab4b64f..7be5495d9b4 100644--- a/gateway/platforms/api_server_authority_runs.py+++ b/gateway/platforms/api_server_authority_runs.py@@ -32,6 +32,21 @@ def store_error_response(_openai_error, exc):headers={'Retry-After': '1'} if status == 503 else None)+async def continued_session(adapter, session_id, request_id=None):+ """``continued_api_session`` for an HTTP door, before its response starts -> ``(session_id,+ None)`` or ``(session_id, error response)``: a busy or unreadable store is the retryable 503+ ``storage_unavailable`` (``store_error_response``), as for the header-less chat lookup."""+ import asyncio+ import sqlite3+ from gateway.platforms.api_server import _openai_error+ from gateway.session_api import continued_api_session+ try:+ return await asyncio.to_thread(+ continued_api_session, adapter.gateway_runner, session_id, request_id), None+ except (RuntimeStoreError, sqlite3.Error, OSError) as exc:+ return session_id, store_error_response(_openai_error, exc)++def run_admission(adapter, run_id):authority = _authority(adapter)if authority is None:diff --git a/gateway/platforms/api_server_response_admissions.py b/gateway/platforms/api_server_response_admissions.pyindex a3451b00634..2ba5ee7f0dd 100644--- a/gateway/platforms/api_server_response_admissions.py+++ b/gateway/platforms/api_server_response_admissions.py@@ -154,9 +154,10 @@ async def prepare_context(adapter, request, body, gateway_session_key, durable_kreturn None, _error_response(f'Previous response not found: {previous}', 404)history = list(stored.get('conversation_history', []))stored_session_id = await asyncio.to_thread(_stored_owner, adapter, stored.get('session_id'))- from gateway.session_api import continued_api_session- stored_session_id = await asyncio.to_thread(- continued_api_session, adapter.gateway_runner, stored_session_id, request_id)+ from gateway.platforms.api_server_authority_runs import continued_session+ stored_session_id, error = await continued_session(adapter, stored_session_id, request_id)+ if error is not None:+ return None, errorif instructions is None:instructions = stored.get('instructions')history.extend(messages[:-1])diff --git a/gateway/platforms/api_server_runs.py b/gateway/platforms/api_server_runs.pyindex 73e85f4a7ec..2c8a8ee137b 100644--- a/gateway/platforms/api_server_runs.py+++ b/gateway/platforms/api_server_runs.py@@ -481,6 +481,18 @@ def _resolve_conversation_history(return conversation_history, instructions, stored_session_id, None+async def _resolve_chained_history(self, body: dict, raw_input: Any, *, _openai_error):+ """``_resolve_conversation_history`` with the chain's live session: a deleted chain moves to+ its next generation (an explicit body ``session_id`` still wins in the caller), and a store+ that cannot answer is the retryable store verdict (``continued_session``)."""+ from gateway.platforms.api_server_authority_runs import continued_session+ history, instructions, stored_session_id, error = _resolve_conversation_history(+ self, body, raw_input, _openai_error=_openai_error)+ if error is None:+ stored_session_id, error = await continued_session(self, stored_session_id)+ return history, instructions, stored_session_id, error++def _accepted_response(run_id: str, status: str, gateway_session_key, *, replayed: bool) -> web.Response:"""202 admission response; replays are flagged via ``Idempotency-Replayed``."""headers = {"Idempotency-Replayed": "true"} if replayed else {}@@ -734,14 +746,11 @@ async def _handle_runs(self, request: web.Request, *, _api_server) -> web.Responturn_author = _api_server._request_turn_author(body)except ValueError as exc:return _json_error(_openai_error, str(exc), code="invalid_author", status=400)- conversation_history, instructions, stored_session_id, history_err = (- _resolve_conversation_history(self, body, raw_input, _openai_error=_openai_error))+ conversation_history, instructions, stored_session_id, history_err = await _resolve_chained_history(+ self, body, raw_input, _openai_error=_openai_error)if history_err is not None:return history_errprevious_response_id = body.get("previous_response_id")- # A deleted chain moves to its next generation (an explicit body session_id still wins below).- from gateway.session_api import continued_api_session- stored_session_id = await asyncio.to_thread(continued_api_session, self.gateway_runner, stored_session_id)session_id = body.get("session_id") or stored_session_idroute = self._resolve_route(body.get("model"))agent_overrides = _api_server._request_agent_overrides(body, virtual_model=self._model_name)diff --git a/tests/gateway/test_retained_response_chain_after_delete.py b/tests/gateway/test_retained_response_chain_after_delete.pyindex 6a3de3967bb..7281aa0b7dd 100644--- a/tests/gateway/test_retained_response_chain_after_delete.py+++ b/tests/gateway/test_retained_response_chain_after_delete.py@@ -3,6 +3,7 @@ moves to a new generation: it is neither refused forever (``not_found``) nor allthe deleted transcript id. The history comes from the response store, so nothing is lost."""import json+import sqlite3import pytestfrom aiohttp import web@@ -52,3 +53,37 @@ async def test_a_deleted_chain_continues_on_a_new_generation(api, owner, path, cbody = await resumed.text()assert resumed.status in (200, 202), f'{resumed.status}: {body}'assert owner.db.get_session(deleted_sid) is None, 'the deleted transcript id was recreated'++[email protected]('path', ['/v1/responses', '/v1/runs'])+async def test_a_busy_store_during_the_chain_lookup_is_retryable(api, owner, monkeypatch, path):+ """The chain's generation lookup reads the owning store before the response starts: a busy+ store is the retryable JSON 503 ``storage_unavailable``, not aiohttp's plain-text 500."""+ from gateway import session_api+ from gateway.session_results import execution_result++ async def handle(event):+ execution_result.get()['result'] = {'final_response': 'ok', 'messages': []}+ return 'ok'++ owner.runner._handle_message = handle+ api._api_key = _API_KEY+ app = web.Application()+ app.router.add_post('/v1/responses', api._handle_responses)+ app.router.add_post('/v1/runs', api._handle_runs)+ async with TestClient(TestServer(app)) as client:+ opened = await client.post('/v1/responses', json={'input': 'hello'},+ headers={**_AUTH, 'Idempotency-Key': 'first'})+ assert opened.status == 200, await opened.text()+ response_id = json.loads(await opened.text())['id']++ def busy(*args, **kwargs):+ raise sqlite3.OperationalError('database is locked')++ monkeypatch.setattr(session_api, 'implicit_api_session', busy)+ resumed = await client.post(path, json={'input': 'next', 'previous_response_id': response_id},+ headers={**_AUTH, 'Idempotency-Key': 'second'})+ assert resumed.status == 503, (resumed.status, await resumed.text())+ assert resumed.headers.get('Retry-After') == '1'+ assert (await resumed.json())['error']['code'] == 'storage_unavailable'