Skip to content

The agent that grows with you

fix: return explicit unknown execution on API crash retries

Tekniumf26c5c432123
diff --git a/gateway/platforms/api_server_openai_routes.py b/gateway/platforms/api_server_openai_routes.py
index 2218d1af05d..ce92526b09b 100644
--- a/gateway/platforms/api_server_openai_routes.py
+++ b/gateway/platforms/api_server_openai_routes.py
@@ -584,6 +584,7 @@ class OpenAICompatRoutesMixin:
``((result, usage), None)`` or ``(None, 500 response)``."""
from gateway.platforms.api_server import (
_error_response, _idem_cache, _make_request_fingerprint)
+ from hermes_state_runtime import RuntimeStoreError
idempotency_key = request.headers.get("Idempotency-Key")
try:
if idempotency_key and getattr(self.gateway_runner, 'session_authority', None) is None:
@@ -592,6 +593,9 @@ class OpenAICompatRoutesMixin:
else:
result, usage = await compute()
return (result, usage), None
+ except RuntimeStoreError as exc:
+ from gateway.platforms.api_server import _openai_error
+ return None, web.json_response(_openai_error(exc.reason, code=exc.reason), status=409)
except Exception as e:
logger.error("Error running agent for %s: %s", log_label, e, exc_info=True)
return None, _error_response(f"Internal server error: {e}", 500, err_type="server_error")
diff --git a/gateway/session_api_turn.py b/gateway/session_api_turn.py
index b7ec4eba6a2..c7f64da4eca 100644
--- a/gateway/session_api_turn.py
+++ b/gateway/session_api_turn.py
@@ -125,6 +125,8 @@ async def run_api_turn(adapter, **kwargs):
async def observe_api_turn(admitted, **kwargs):
authority, ref, row = admitted
+ if row['status'] == 'unknown':
+ raise RuntimeStoreError('unknown_execution')
if row['status'] == 'terminal':
result = admission_result(authority.db, row['admission_id'])
if result is None:
diff --git a/tests/gateway/test_api_crash_recovery.py b/tests/gateway/test_api_crash_recovery.py
index 6cd3aa01e21..02483d01e9a 100644
--- a/tests/gateway/test_api_crash_recovery.py
+++ b/tests/gateway/test_api_crash_recovery.py
@@ -57,8 +57,16 @@ def test_api_crash_queue_and_identified_result_retry(tmp_path):
async with client.post(url + '/chat/completions', json={'messages': [{'role': 'user', 'content': 'WARM'}]},
headers={'X-Hermes-Session-Id': 'unknown'}) as response:
assert response.status == 200, await response.text()
- await run(client, 'started', 'BLOCK_STARTED', 'unknown')
+ async def block():
+ async with client.post(url + '/chat/completions',
+ json={'messages': [{'role': 'user', 'content': 'BLOCK_STARTED'}]},
+ headers={'X-Hermes-Session-Id': 'unknown', 'Idempotency-Key': 'started'}) as response:
+ await response.read()
+ blocked = asyncio.create_task(block())
assert await asyncio.to_thread(peer.blocked.wait, 15)
+ runs['started'] = 'chat:started'
+ blocked.cancel()
+ await asyncio.gather(blocked, return_exceptions=True)
await run(client, 'follower', 'NEVER_REPLAY', 'unknown')
await run(client, 'safe', 'SAFE_QUEUE', 'safe')
assert rows()[runs['safe']] == 'queued'
@@ -70,6 +78,11 @@ def test_api_crash_queue_and_identified_result_retry(tmp_path):
while rows()[runs['safe']] != 'terminal':
await asyncio.sleep(.05)
assert rows()[runs['started']] == 'unknown' and rows()[runs['follower']] == 'queued', rows()
+ async with client.post(url + '/chat/completions',
+ json={'messages': [{'role': 'user', 'content': 'BLOCK_STARTED'}]},
+ headers={'X-Hermes-Session-Id': 'unknown', 'Idempotency-Key': 'started'}) as response:
+ unknown = await response.json()
+ assert response.status == 409 and unknown['error']['code'] == 'unknown_execution', unknown
before = len(peer.requests)
await run(client, 'safe', 'SAFE_QUEUE', 'safe')
async with client.get(url + '/runs/' + runs['safe']) as response: