/v1/chat/completions keys its canonical admission as chat:<principal scope>:<Idempotency-Key>, but the ledger's identity is (principal, target session, request id). The same key and body sent with a different X-Hermes-Session-Id therefore admitted and executed a second turn, where main's in-process _idem_cache (fingerprint over the body only) replayed the first answer. The API admission write now refuses, inside its own transaction, a request id that already names an admission on another target: admission_conflict (409), never a second execution. Test: tests/gateway/test_api_request_one_target.py::test_same_chat_key_on_another_session_is_refused_not_rerun Review: andrexibiza 5480715949-P2b (cherry picked from commit e5951367c99c67279e3637818e003f5aa9d12b26)
31 lines
1.3 KiB
Python
31 lines
1.3 KiB
Python
"""One API Idempotency-Key names one admission: a second session target never runs it again."""
|
|
import pytest
|
|
from aiohttp import web
|
|
from aiohttp.test_utils import TestClient, TestServer
|
|
|
|
KEY = 'request-scope-key-long-enough'
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_same_chat_key_on_another_session_is_refused_not_rerun(api, owner):
|
|
calls = []
|
|
|
|
async def handle(event):
|
|
from gateway.session_results import execution_result
|
|
calls.append(event.text)
|
|
execution_result.get()['result'] = {'final_response': 'ok', 'messages': []}
|
|
return 'ok'
|
|
owner.runner._handle_message = handle
|
|
api._api_key = KEY
|
|
app = web.Application()
|
|
app.router.add_post('/v1/chat/completions', api._handle_chat_completions)
|
|
body = {'messages': [{'role': 'user', 'content': 'charge the card'}]}
|
|
async with TestClient(TestServer(app)) as client:
|
|
statuses = []
|
|
for sid in ('first-session', 'second-session'):
|
|
response = await client.post('/v1/chat/completions', json=body, headers={
|
|
'Authorization': 'Bearer ' + KEY, 'Idempotency-Key': 'pay-once', 'X-Hermes-Session-Id': sid})
|
|
statuses.append((response.status, (await response.json()).get('error', {}).get('code')))
|
|
assert calls == ['charge the card'], calls
|
|
assert statuses == [(200, None), (409, 'admission_conflict')], statuses
|