The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(api): chained Responses turns replay the earlier turn's api_content
Teknium3ac1d17b8233
diff --git a/gateway/platforms/api_server_openai_routes.py b/gateway/platforms/api_server_openai_routes.pyindex e0aef9bd458..7adeeea105f 100644--- a/gateway/platforms/api_server_openai_routes.py+++ b/gateway/platforms/api_server_openai_routes.py@@ -1443,6 +1443,8 @@ class OpenAICompatRoutesMixin:from gateway.platforms.api_server import APIServerAdapterprior = list(conversation_history)current_user = {"role": "user", "content": user_message}+ if isinstance(result, dict) and isinstance(result.get("_turn_user_row"), dict):+ current_user = dict(result["_turn_user_row"]) # settled canonical turn: carries api_contentagent_messages = result.get("messages") if isinstance(result, dict) else Noneif isinstance(agent_messages, list) and agent_messages:turn_start = APIServerAdapter._response_messages_turn_start_index(diff --git a/hermes_state_terminal.py b/hermes_state_terminal.pyindex 69fef49ecd0..060018b3f54 100644--- a/hermes_state_terminal.py+++ b/hermes_state_terminal.py@@ -38,6 +38,10 @@ def compact_result(result, *, user_message=None):and isinstance(messages[anchor], dict) and messages[anchor].get('role') == 'user'):anchor = reanchor_current_turn_user_idx(messages, user_message)value['messages'] = messages[anchor + 1:]+ if anchor >= 0 and isinstance(messages[anchor], dict) and messages[anchor].get('api_content'):+ # The user row as the model saw it (memory prefetch, plugin context): a chained+ # Responses turn replays it instead of a row rebuilt from the raw request text.+ value['_turn_user_row'] = {k: messages[anchor][k] for k in ('role', 'content', 'api_content')}# Recovery/verification nudges are later user-role rows within this same turn.# Consumers must not infer a new boundary from them after the real anchor is gone.value['_messages_are_turn_suffix'] = Truediff --git a/tests/gateway/test_responses_chain_api_content.py b/tests/gateway/test_responses_chain_api_content.pynew file mode 100644index 00000000000..6603af2620d--- /dev/null+++ b/tests/gateway/test_responses_chain_api_content.py@@ -0,0 +1,38 @@+"""A chained Responses turn replays the earlier turn's user row as the model saw it, including+its ``api_content`` (memory prefetch, plugin context), not a synthetic row rebuilt from the raw+request text."""+import pytest+from aiohttp import web+from aiohttp.test_utils import TestClient, TestServer++[email protected]('stream', [False, True], ids=['json', 'sse'])+async def test_previous_response_chain_keeps_turn_user_api_content(api, owner, stream):+ seen = []++ async def handle(event):+ from gateway.session_api_turn import api_execution+ from gateway.session_results import execution_result+ history = api_execution.get()['history']+ seen.append(history)+ user = {'role': 'user', 'content': event.text}+ if event.text == 'first':+ user['api_content'] = 'first\n\n<user_memory>project is ALPHA</user_memory>'+ execution_result.get()['result'] = {'final_response': 'a', 'messages': [+ *history, user, {'role': 'assistant', 'content': 'a'}]}+ return 'a'+ owner.runner._handle_message = handle+ app = web.Application()+ app.router.add_post('/v1/responses', api._handle_responses)+ async with TestClient(TestServer(app)) as client:+ first = await client.post('/v1/responses', json={'input': 'first'})+ rid = (await first.json())['id']+ second = await client.post('/v1/responses', json={+ 'input': 'second', 'previous_response_id': rid, 'stream': stream})+ assert second.status == 200, await second.text()+ await second.text()+ users = [m for m in seen[1] if m['role'] == 'user']+ assert users[0]['content'] == 'first'+ assert users[0].get('api_content', '').endswith('ALPHA</user_memory>'), seen[1]+ assert [m['role'] for m in seen[1]] == ['user', 'assistant']