Skip to content

The agent that grows with you

fix(api): keep an Idempotency-Key spent after its session is deleted

unsupportedpastelsd2fb19c1dd63
diff --git a/gateway/platforms/api_server_response_admissions.py b/gateway/platforms/api_server_response_admissions.py
index 2ba5ee7f0dd..0cc7273e0e4 100644
--- a/gateway/platforms/api_server_response_admissions.py
+++ b/gateway/platforms/api_server_response_admissions.py
@@ -1,7 +1,6 @@
"""Responses retries observe their original admission before resolving mutable conversation names."""
from functools import partial
import hashlib
-import json
import uuid
from hermes_state_runtime import RuntimeStoreError
@@ -16,15 +15,11 @@ def request_binding(adapter, request, durable_key):
def _request_admission_rows(db, request_id):
"""A lost derivative pointer cannot erase accepted identity on retirement."""
- from hermes_state_terminal import ADMISSION_PREFIX
+ from hermes_state_terminal import terminal_api_admissions
with db._read_ctx() as conn:
rows = [dict(row) for row in conn.execute(
"SELECT * FROM session_admissions WHERE principal_id='api' AND request_id=?", (request_id,))]
- retired = conn.execute(
- "SELECT value FROM state_meta WHERE key GLOB ? "
- "AND json_extract(value, '$.principal_id')='api' AND json_extract(value, '$.request_id')=?",
- (ADMISSION_PREFIX + '*', request_id))
- rows.extend(json.loads(row[0]) for row in retired)
+ rows.extend(terminal_api_admissions(conn, request_id))
return rows
diff --git a/gateway/session_api_turn.py b/gateway/session_api_turn.py
index 0aada2dcb9c..81419bc58ee 100644
--- a/gateway/session_api_turn.py
+++ b/gateway/session_api_turn.py
@@ -277,10 +277,15 @@ def _admit_target(session_id, request_id, settings, conn):
def _one_target(session_id, request_id, conn):
"""One API request id (an Idempotency-Key) names one admission. The ledger keys identity by
- target too, so the same key aimed at another session would otherwise run the work again."""
+ target too, so the same key aimed at another session would otherwise run the work again.
+ A deleted session's admission is gone from the ledger but its tombstone still spends the key
+ (a target that already holds the key replays before this guard runs)."""
if conn.execute("SELECT 1 FROM session_admissions WHERE principal_id='api' AND request_id=? "
'AND target_session_id!=? LIMIT 1', (request_id, session_id)).fetchone():
raise RuntimeStoreError('admission_conflict')
+ from hermes_state_terminal import terminal_api_admissions
+ if any(row['target_session_id'] != session_id for row in terminal_api_admissions(conn, request_id)):
+ raise RuntimeStoreError('admission_conflict')
def owns_api_run(adapter, run_id, owner_scope):
diff --git a/hermes_state_terminal.py b/hermes_state_terminal.py
index 060018b3f54..16547a5833f 100644
--- a/hermes_state_terminal.py
+++ b/hermes_state_terminal.py
@@ -55,6 +55,15 @@ def identity_key(principal_id, session_id, request_id):
payload={'principal': principal_id, 'request': request_id})
+def terminal_api_admissions(conn, request_id):
+ """Retired API admissions that carried *request_id*: a deleted session's tombstones, kept for
+ the exact-retry horizon, still spend its Idempotency-Key for every other target."""
+ return [json.loads(row[0]) for row in conn.execute(
+ "SELECT value FROM state_meta WHERE key GLOB ? "
+ "AND json_extract(value, '$.principal_id')='api' AND json_extract(value, '$.request_id')=?",
+ (ADMISSION_PREFIX + '*', request_id))]
+
+
def terminal_admission(conn, admission_id):
saved = conn.execute('SELECT value FROM state_meta WHERE key=?',
(ADMISSION_PREFIX + admission_id,)).fetchone()
diff --git a/tests/gateway/test_api_idempotency_after_delete.py b/tests/gateway/test_api_idempotency_after_delete.py
new file mode 100644
index 00000000000..a9523549401
--- /dev/null
+++ b/tests/gateway/test_api_idempotency_after_delete.py
@@ -0,0 +1,106 @@
+"""An Idempotency-Key stays exactly-once after its session is deleted.
+
+Deleting the session used to drop the admission row ``_one_target`` consults, so the same
+key aimed at another session ran the work a second time.
+"""
+import pytest
+from aiohttp import web
+from aiohttp.test_utils import TestClient, TestServer
+
+
+_API_KEY = 'idempotency-after-delete-key'
+
+
+def _execute(owner, calls):
+ async def handle(event):
+ from gateway.session_results import execution_result
+ calls.append(event.text)
+ text = 'answer ' + event.text
+ execution_result.get()['result'] = {'final_response': text, 'messages': []}
+ return text
+ owner.runner._handle_message = handle
+
+
+def _app(api):
+ app = web.Application()
+ app.router.add_post('/v1/chat/completions', api._handle_chat_completions)
+ app.router.add_delete('/api/sessions/{session_id}', api._handle_delete_session)
+ return app
+
+
+async def _post(client, session_id, *, key='once', content='same body'):
+ body = {'model': 'hermes-agent', 'messages': [{'role': 'user', 'content': content}]}
+ response = await client.post(
+ '/v1/chat/completions', json=body,
+ headers={'Authorization': 'Bearer ' + _API_KEY, 'Idempotency-Key': key,
+ 'X-Hermes-Session-Id': session_id})
+ return response.status, await response.json()
+
+
+async def _delete(client, session_id):
+ deleted = await client.delete('/api/sessions/' + session_id, headers={'Authorization': 'Bearer ' + _API_KEY})
+ assert deleted.status == 200, await deleted.text()
+ assert (await deleted.json())['deleted'] is True
+
+
+def _content(payload):
+ return payload['choices'][0]['message']['content']
+
+
+async def test_deleted_session_idempotency_key_does_not_run_again(api, owner):
+ """Same body and key: A runs once, B conflicts, deleting A does not let B run it."""
+ calls = []
+ api._api_key = _API_KEY
+ _execute(owner, calls)
+ async with TestClient(TestServer(_app(api))) as client:
+ status, first = await _post(client, 'session-a')
+ assert status == 200, first
+ original = _content(first)
+ status, conflict = await _post(client, 'session-b')
+ assert status == 409 and conflict['error']['code'] == 'admission_conflict', conflict
+
+ await _delete(client, 'session-a')
+ status, again = await _post(client, 'session-b')
+ assert status == 409, again
+ assert again['error']['code'] == 'admission_conflict'
+ assert calls == ['same body']
+
+ status, same = await _post(client, 'session-a')
+ assert status == 200 and _content(same) == original, same
+ assert owner.db.get_session('session-a') is None
+
+ status, fresh = await _post(client, 'session-b', key='fresh-key', content='new body')
+ assert status == 200 and _content(fresh) == 'answer new body', fresh
+ assert calls == ['same body', 'new body']
+
+
+async def test_a_live_session_that_already_ran_the_key_still_replays_it(api, owner):
+ """A session that ran the key while the deleted one's tombstone was unread (code from before
+ this check) still replays its own completion; only a session that never ran it conflicts."""
+ from hermes_state_terminal import ADMISSION_PREFIX
+ calls = []
+ api._api_key = _API_KEY
+ _execute(owner, calls)
+ async with TestClient(TestServer(_app(api))) as client:
+ status, first = await _post(client, 'session-a')
+ assert status == 200, first
+ await _delete(client, 'session-a')
+ with owner.db._read_ctx() as conn:
+ saved = list(conn.execute('SELECT key, value FROM state_meta WHERE key GLOB ?', (ADMISSION_PREFIX + '*',)))
+ assert saved
+ owner.db._execute_write(lambda conn: conn.execute('DELETE FROM state_meta WHERE key GLOB ?',
+ (ADMISSION_PREFIX + '*',)))
+ status, live = await _post(client, 'session-b')
+ assert status == 200, live
+ owner.db._execute_write(lambda conn: conn.executemany(
+ 'INSERT OR REPLACE INTO state_meta(key, value) VALUES(?, ?)', saved))
+
+ status, again = await _post(client, 'session-b')
+ assert status == 200 and _content(again) == _content(live), (status, again)
+ status, other = await _post(client, 'session-c')
+ assert status == 409 and other['error']['code'] == 'admission_conflict', other
+ status, changed = await _post(client, 'session-b', content='other body')
+ assert status == 409 and changed['error']['code'] == 'admission_conflict', changed
+ assert calls == ['same body', 'same body']