Skip to content

The agent that grows with you

perf(gateway): scan API tombstones only for a client Idempotency-Key

unsupportedpastelsdedd8f258b1b
diff --git a/gateway/session_api_turn.py b/gateway/session_api_turn.py
index 81419bc58ee..e9df4d12549 100644
--- a/gateway/session_api_turn.py
+++ b/gateway/session_api_turn.py
@@ -262,27 +262,32 @@ def _admit_api_payload(authority, adapter, sid, request_id, payload, settings, d
check_api_turn(authority, ref, payload)
row = yield partial(admit_session_input, authority.db, epoch=authority.epoch, principal_id='api',
session_id=sid, request_id=request_id, payload=payload,
- _authorize_write=authority._admission_gate(partial(_admit_target, sid, request_id, payload['api_turn_v1']['settings'])))
+ _authorize_write=authority._admission_gate(partial(
+ _admit_target, sid, request_id, payload['api_turn_v1']['settings'],
+ client_key=bool(kwargs.get('request_id')))))
return authority, ref, row
-def _admit_target(session_id, request_id, settings, conn):
+def _admit_target(session_id, request_id, settings, conn, *, client_key=True):
"""Inside the admission's own off-loop transaction: one target per request id, and the
session's settings for its later payload-less (internal) turns, committed with the admitted
turn rather than by the drain on the owner loop (N50)."""
- _one_target(session_id, request_id, conn)
+ _one_target(session_id, request_id, conn, client_key=client_key)
conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value',
(_SETTINGS_PREFIX + session_id, _json(settings)))
-def _one_target(session_id, request_id, conn):
+def _one_target(session_id, request_id, conn, *, client_key=True):
"""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.
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)."""
+ (a target that already holds the key replays before this guard runs). Only a client key can
+ name a tombstone: a server-minted id (key-less turn, run id) is fresh, so it skips that scan."""
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')
+ if not client_key:
+ return
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')
diff --git a/tests/gateway/test_api_idempotency_after_delete.py b/tests/gateway/test_api_idempotency_after_delete.py
index a9523549401..43e9c71f23b 100644
--- a/tests/gateway/test_api_idempotency_after_delete.py
+++ b/tests/gateway/test_api_idempotency_after_delete.py
@@ -104,3 +104,20 @@ async def test_a_live_session_that_already_ran_the_key_still_replays_it(api, own
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']
+
+
+def test_only_a_client_key_pays_for_the_tombstone_scan(api, owner, monkeypatch):
+ """Only a client Idempotency-Key can name a deleted session's admission. A key-less turn's
+ fresh id (or a server-minted run id) never matches one, so the admission's write transaction
+ skips the tombstone scan for it."""
+ import hermes_state_terminal
+ from gateway.session_api_turn import admit_api_turn
+ real = hermes_state_terminal.terminal_api_admissions
+ scanned = []
+ monkeypatch.setattr(hermes_state_terminal, 'terminal_api_admissions',
+ lambda conn, request_id: scanned.append(request_id) or real(conn, request_id))
+ admit_api_turn(api, session_id='keyless', user_message='hi', conversation_history=[])
+ admit_api_turn(api, session_id='run', active_run_id='run_minted', user_message='hi', conversation_history=[])
+ assert scanned == []
+ admit_api_turn(api, session_id='keyed', request_id='chat:scope:key', user_message='hi', conversation_history=[])
+ assert scanned == ['chat:scope:key']