The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(api): a keyed Responses replay re-sends the incremental tool frames as first sent
Tekniume312ea008030
diff --git a/gateway/platforms/api_server_openai_routes.py b/gateway/platforms/api_server_openai_routes.pyindex 96b51ef84d5..9fe4c626a8e 100644--- a/gateway/platforms/api_server_openai_routes.py+++ b/gateway/platforms/api_server_openai_routes.py@@ -7,6 +7,7 @@ module (top-level import = cycle), and lazy lookup keeps ``patch("...api_server."""import asyncio+import copyimport jsonimport loggingimport re@@ -311,7 +312,8 @@ class _ResponsesStream:data["sequence_number"] = self.sequence_numberself.sequence_number += 1if self.durable_key: # the only reader is the keyed replay record (emit_completed)- self.recorded_events.append((event_type, data))+ # Snapshot: terminal trimming later edits the nested tool output these frames share.+ self.recorded_events.append((event_type, copy.deepcopy(data)))await self.response.write(self._api._sse_frame(data, event=event_type))def _item_id(self, kind, *, call_id=None, commentary=False):diff --git a/tests/gateway/test_api_responses_replay_frozen_tool_events.py b/tests/gateway/test_api_responses_replay_frozen_tool_events.pynew file mode 100644index 00000000000..46982b2f320--- /dev/null+++ b/tests/gateway/test_api_responses_replay_frozen_tool_events.py@@ -0,0 +1,55 @@+"""A keyed streaming Responses retry replays the incremental tool frames exactly as first sent:+terminal trimming of a long tool result must not reach the recorded events."""+import json++import pytest+from aiohttp import web+from aiohttp.test_utils import TestClient, TestServer+++def _tool_output_texts(frames):+ return [frame['item']['output'][0]['text'] for frame in frames+ if frame.get('item', {}).get('type') == 'function_call_output']++[email protected]('size', [1500, 200])+async def test_exact_retry_replays_full_incremental_tool_output(api, owner, size):+ result_text = 'x' * size+ calls = []++ async def handle(event):+ from gateway.session_api_turn import publish_api_tool_event+ from gateway.session_results import execution_result+ calls.append(event.text)+ sid = event.source.chat_id+ generation = owner.sessions[sid].event_stream.execution['execution_generation']+ publish_api_tool_event(owner, sid, generation, 'tool.start', 'call_1', 'read_file', {'path': 'f'})+ publish_api_tool_event(owner, sid, generation, 'tool.complete', 'call_1', 'read_file', {'path': 'f'},+ result_text)+ execution_result.get()['result'] = {'final_response': 'done', 'messages': [+ {'role': 'assistant', 'content': '', 'tool_calls': [+ {'id': 'call_1', 'type': 'function',+ 'function': {'name': 'read_file', 'arguments': json.dumps({'path': 'f'})}}]},+ {'role': 'tool', 'tool_call_id': 'call_1', 'content': result_text},+ {'role': 'assistant', 'content': 'done'}]}+ return 'done'++ owner.runner._handle_message = handle+ app = web.Application()+ app.router.add_post('/v1/responses', api._handle_responses)+ body = {'input': 'read it', 'stream': True}+ async with TestClient(TestServer(app)) as client:+ streams = []+ for key in ('frozen-tool', 'frozen-tool', 'fresh-key'):+ resp = await client.post('/v1/responses', json=body, headers={'Idempotency-Key': key})+ assert resp.status == 200+ streams.append([json.loads(line[6:]) for line in (await resp.text()).splitlines()+ if line.startswith('data: ')])+ first, retry, fresh = streams+ assert _tool_output_texts(first) == [result_text, result_text]+ assert _tool_output_texts(retry) == _tool_output_texts(first)+ assert retry == first+ assert _tool_output_texts(fresh) == [result_text, result_text]+ assert retry[-1]['response'] == first[-1]['response']+ assert calls == ['read it', 'read it']