The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(api): only the publishing Responses stream writes the idem: replay record
Tekniumdc689402b1aa
diff --git a/gateway/platforms/api_server_openai_routes.py b/gateway/platforms/api_server_openai_routes.pyindex fb0c65267bd..dcb8a05ad29 100644--- a/gateway/platforms/api_server_openai_routes.py+++ b/gateway/platforms/api_server_openai_routes.py@@ -634,9 +634,12 @@ class _ResponsesStream:if self.durable_key:headers = {k: v for k, v in self.response.headers.items() if k.startswith('X-Hermes-')}terminal_event = {'type': 'response.' + status, 'response': env, 'sequence_number': self.sequence_number}- self.response_store.put(self.durable_key[0], {- 'fingerprint': self.durable_key[1], 'response': env, 'headers': headers,- 'events': [*self.recorded_events, ('response.' + status, terminal_event)]})+ if not self.terminal_replay:+ # Only the publisher recorded the deltas; a replay observer winning the+ # INSERT OR IGNORE would leave every later replay without them.+ self.response_store.put(self.durable_key[0], {+ 'fingerprint': self.durable_key[1], 'response': env, 'headers': headers,+ 'events': [*self.recorded_events, ('response.' + status, terminal_event)]})stored = self.response_store.get(self.durable_key[0])if _settled(stored):env = stored['response']diff --git a/tests/gateway/test_responses_idem_replay_record.py b/tests/gateway/test_responses_idem_replay_record.pynew file mode 100644index 00000000000..93c603fe636--- /dev/null+++ b/tests/gateway/test_responses_idem_replay_record.py@@ -0,0 +1,59 @@+"""Only the publishing Responses stream writes the durable ``idem:`` replay record."""+import asyncio+import json++import pytest+from aiohttp import web+from aiohttp.test_utils import TestClient, TestServer+++def _frames(text):+ return [json.loads(line[6:]) for line in text.splitlines() if line.startswith('data: ')]+++async def test_in_flight_retry_does_not_win_the_replay_record_without_deltas(api, owner, monkeypatch):+ release = asyncio.Event()+ calls = []++ async def handle(event):+ from gateway.session_api_turn import publish_api_event+ from gateway.session_results import execution_result+ calls.append(event.text)+ await release.wait()+ publish_api_event(owner, event.source.chat_id, 'message.delta', {'text': 'answer'})+ execution_result.get()['result'] = {'final_response': 'answer', 'messages': [+ {'role': 'assistant', 'content': 'answer'}]}+ return 'answer'+ owner.runner._handle_message = handle+ from gateway.platforms import api_server_openai_routes as routes+ emit, retried = routes._ResponsesStream.emit_completed, asyncio.Event()++ async def lagging_publisher(stream):+ # The original client lags: the joined retry reaches its terminal write first.+ if stream.terminal_replay:+ try:+ return await emit(stream)+ finally:+ retried.set()+ await asyncio.wait_for(retried.wait(), 10)+ return await emit(stream)+ monkeypatch.setattr(routes._ResponsesStream, 'emit_completed', lagging_publisher)+ app = web.Application()+ app.router.add_post('/v1/responses', api._handle_responses)+ body, headers = {'input': 'work', 'stream': True}, {'Idempotency-Key': 'lagging-publisher'}+ async with TestClient(TestServer(app)) as client:+ original = asyncio.create_task(client.post('/v1/responses', json=body, headers=headers))+ async with asyncio.timeout(10):+ while not calls:+ await asyncio.sleep(.01)+ retry = asyncio.create_task(client.post('/v1/responses', json=body, headers=headers))+ await asyncio.sleep(.2) # the retry joins the in-flight admission as an observer+ release.set()+ first, joined = await original, await retry+ assert first.status == joined.status == 200+ await first.text(), await joined.text()+ later = await client.post('/v1/responses', json=body, headers=headers)+ replayed = _frames(await later.text())+ assert calls == ['work']+ assert any(f['type'] == 'response.output_text.delta' for f in replayed), [f['type'] for f in replayed]