Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/codex): replay the accepted tool-result response on a retried identical request instead of 409 (#14760)

Reinier van der Leerc3911c474600
diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py
index 0fc2bb91fe..16f083d80d 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py
@@ -35,6 +35,15 @@ from backend.integrations.codex.transport import (
from backend.integrations.credential_lease import CredentialLease
_MAX_REQUEST_BYTES = 16 * 1024 * 1024
+# Bounds on the per-conversation replay cache. A retry arrives seconds to
+# minutes after the original, so no entry has to outlive its own turn: over 30
+# days of Dev logs the longest Codex turn ran 1,406 s and resolved 67 tool calls
+# (2026-09-18), one boundary each. An entry holds only the response body -- a
+# tool_use boundary or the final text -- never the 0.97M-5.8M token request that
+# provoked the retry, and the byte cap is half the request limit above.
+_MAX_REPLAY_ENTRIES = 128
+_MAX_REPLAY_BYTES = 8 * 1024 * 1024
+_REPLAY_TTL_SECONDS = 1800.0
_MAX_LOGGED_ERROR_MESSAGE_CHARS = 240
_REDACTED = "[REDACTED]"
_TOOL_NAME_PATTERN = re.compile(r"^[a-zA-Z0-9_-]{1,128}$")
@@ -76,6 +85,10 @@ class _AgentSession(Protocol):
) -> CodexInvocationResult: ...
+class _SseSink(Protocol):
+ async def write(self, data: bytes) -> None: ...
+
+
class _GatewayState(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
@@ -101,6 +114,16 @@ class _Failed(_GatewayState):
_ConversationEvent = _TextDelta | _ToolUse | _Completed | _Failed
+class _Replay(_GatewayState):
+ """A response already delivered, kept to answer a byte-identical retry."""
+
+ status: int
+ headers: dict[str, str]
+ body: bytes
+ streamed: bool
+ expires_at: float
+
+
class _Conversation(_GatewayState):
id: str
queue: asyncio.Queue[_ConversationEvent] = Field(default_factory=asyncio.Queue)
@@ -108,6 +131,17 @@ class _Conversation(_GatewayState):
response_lock: asyncio.Lock = Field(default_factory=asyncio.Lock)
task: asyncio.Task[None] | None = None
result: CodexInvocationResult | None = None
+ replays: dict[str, _Replay] = Field(default_factory=dict)
+
+
+class _Continuation(_GatewayState):
+ conversation: _Conversation
+ replay_key: str
+
+
+class _DuplicateSubmission(_GatewayState):
+ conversation: _Conversation
+ replay_key: str
class _ToolCallRecord(_GatewayState):
@@ -122,6 +156,8 @@ class _ToolCallRecord(_GatewayState):
_Conversation.model_rebuild()
_ToolCallRecord.model_rebuild()
+_Continuation.model_rebuild()
+_DuplicateSubmission.model_rebuild()
class _DuplicateToolResultError(ValueError):
@@ -327,9 +363,7 @@ class CodexAnthropicGateway:
)
try:
- conversation = self._continue_conversation(payload)
- if conversation is None:
- conversation = self._start_conversation(payload)
+ continuation = self._continue_conversation(payload)
except _DuplicateToolResultError as exc:
return _anthropic_error(
409,
@@ -343,19 +377,65 @@ class CodexAnthropicGateway:
str(exc),
)
+ streamed = payload.get("stream") is True
+ if isinstance(continuation, _DuplicateSubmission):
+ return await self._duplicate_response(request, continuation, streamed)
+ if continuation is None:
+ conversation, replay_key = self._start_conversation(payload), None
+ else:
+ conversation = continuation.conversation
+ replay_key = continuation.replay_key
+
input_tokens = _estimate_input_tokens(payload)
- if payload.get("stream") is True:
+ if streamed:
return await self._streaming_response(
request,
conversation,
input_tokens,
+ replay_key,
+ )
+ return await self._nonstreaming_response(
+ conversation,
+ input_tokens,
+ replay_key,
+ )
+
+ async def _duplicate_response(
+ self,
+ request: web.Request,
+ duplicate: _DuplicateSubmission,
+ streamed: bool,
+ ) -> web.StreamResponse:
+ # The lock is held while the accepted request is still writing, so
+ # waiting on it is what gives a concurrent duplicate the same answer.
+ async with duplicate.conversation.response_lock:
+ replay = self._take_replay(
+ duplicate.conversation,
+ duplicate.replay_key,
+ streamed=streamed,
)
- return await self._nonstreaming_response(conversation, input_tokens)
+ if replay is None:
+ return _anthropic_error(
+ 409,
+ "invalid_request_error",
+ "This tool-result request was already accepted",
+ )
+ if not replay.streamed:
+ return web.Response(
+ status=replay.status,
+ body=replay.body,
+ headers=replay.headers,
+ )
+ response = web.StreamResponse(status=replay.status, headers=replay.headers)
+ await response.prepare(request)
+ await response.write(replay.body)
+ await response.write_eof()
+ return response
def _continue_conversation(
self,
payload: dict[str, object],
- ) -> _Conversation | None:
+ ) -> _Continuation | _DuplicateSubmission | None:
tool_results = _extract_tool_results(payload.get("messages"))
known = [
(self._tool_calls[call_id], result)
@@ -378,9 +458,15 @@ class CodexAnthropicGateway:
]
fingerprint = _tool_result_request_fingerprint(payload)
if not claimable:
- if any(record.claim_fingerprint == fingerprint for record, _ in known):
- raise _DuplicateToolResultError(
- "This tool-result request was already accepted"
+ claimed_by = [
+ record for record, _ in known if record.claim_fingerprint == fingerprint
+ ]
+ if claimed_by:
+ # The CLI re-sends this verbatim when its first attempt times
+ # out, so the response that request produced is the answer.
+ return _DuplicateSubmission(
+ conversation=claimed_by[0].conversation,
+ replay_key=fingerprint,
)
if any(record.result is None for record, _ in known):
raise _DuplicateToolResultError(
@@ -400,7 +486,7 @@ class CodexAnthropicGateway:
for record, result in claimable:
if not record.future.done():
record.future.set_result(result)
- return conversation
+ return _Continuation(conversation=conversation, replay_key=fingerprint)
def _start_conversation(self, payload: dict[str, object]) -> _Conversation:
agent_session = self._agent_session
@@ -518,12 +604,14 @@ class CodexAnthropicGateway:
request: web.Request,
conversation: _Conversation,
input_tokens: int,
+ replay_key: str | None = None,
) -> web.StreamResponse:
async with conversation.response_lock:
return await self._streaming_response_locked(
request,
conversation,
input_tokens,
+ replay_key,
)
async def _streaming_response_locked(
@@ -531,24 +619,24 @@ class CodexAnthropicGateway:
request: web.Request,
conversation: _Conversation,
input_tokens: int,
+ replay_key: str | None,
) -> web.StreamResponse:
first = await conversation.queue.get()
if isinstance(first, _Failed):
return self._failure_response(first)
- response = web.StreamResponse(
- status=200,
- headers={
- "Content-Type": "text/event-stream",
- "Cache-Control": "no-cache",
- "Connection": "keep-alive",
- },
- )
+ headers = {
+ "Content-Type": "text/event-stream",
+ "Cache-Control": "no-cache",
+ "Connection": "keep-alive",
+ }
+ response = web.StreamResponse(status=200, headers=headers)
await response.prepare(request)
+ sink = _RecordingSink(response)
message_id = f"msg_codex_{uuid4().hex}"
try:
await _write_sse(
- response,
+ sink,
{
"type": "message_start",
"message": {
@@ -566,17 +654,28 @@ class CodexAnthropicGateway:
},
},
)
- await self._write_boundary(response, conversation, first)
+ await self._write_boundary(sink, conversation, first)
await response.write_eof()
except (ConnectionError, ConnectionResetError, asyncio.CancelledError):
if conversation.task is not None:
conversation.task.cancel()
raise
+ self._record_replay(
+ conversation,
+ replay_key,
+ _Replay(
+ status=200,
+ headers=headers,
+ body=sink.body,
+ streamed=True,
+ expires_at=_now() + _REPLAY_TTL_SECONDS,
+ ),
+ )
return response
async def _write_boundary(
self,
- response: web.StreamResponse,
+ sink: _SseSink,
conversation: _Conversation,
first: _ConversationEvent,
) -> None:
@@ -589,7 +688,7 @@ class CodexAnthropicGateway:
emitted_text = True
if not text_open:
await _write_sse(
- response,
+ sink,
{
"type": "content_block_start",
"index": index,
@@ -598,7 +697,7 @@ class CodexAnthropicGateway:
)
text_open = True
await _write_sse(
- response,
+ sink,
{
"type": "content_block_delta",
"index": index,
@@ -608,19 +707,19 @@ class CodexAnthropicGateway:
elif isinstance(event, _ToolUse):
if text_open:
await _write_sse(
- response,
+ sink,
{"type": "content_block_stop", "index": index},
)
index += 1
text_open = False
- await _write_tool_use(response, index, event)
- await _write_message_end(response, "tool_use", 0)
+ await _write_tool_use(sink, index, event)
+ await _write_message_end(sink, "tool_use", 0)
return
elif isinstance(event, _Completed):
if not emitted_text and event.result.final_response:
if not text_open:
await _write_sse(
- response,
+ sink,
{
"type": "content_block_start",
"index": index,
@@ -629,7 +728,7 @@ class CodexAnthropicGateway:
)
text_open = True
await _write_sse(
- response,
+ sink,
{
"type": "content_block_delta",
"index": index,
@@ -641,13 +740,13 @@ class CodexAnthropicGateway:
)
if text_open:
await _write_sse(
- response,
+ sink,
{"type": "content_block_stop", "index": index},
)
output_tokens = (
event.result.usage.output_tokens if event.result.usage else 0
)
- await _write_message_end(response, "end_turn", output_tokens)
+ await _write_message_end(sink, "end_turn", output_tokens)
return
elif isinstance(event, _Failed):
# The HTTP status can no longer change once streaming starts,
@@ -657,7 +756,7 @@ class CodexAnthropicGateway:
self._failure_response(event)
failure = self.last_failure
await _write_sse(
- response,
+ sink,
{
"type": "error",
"error": {
@@ -673,7 +772,7 @@ class CodexAnthropicGateway:
return
else:
await _write_sse(
- response,
+ sink,
{
"type": "error",
"error": {
@@ -689,17 +788,20 @@ class CodexAnthropicGateway:
self,
conversation: _Conversation,
input_tokens: int,
+ replay_key: str | None = None,
) -> web.Response:
async with conversation.response_lock:
return await self._nonstreaming_response_locked(
conversation,
input_tokens,
+ replay_key,
)
async def _nonstreaming_response_locked(
self,
conversation: _Conversation,
input_tokens: int,
+ replay_key: str | None,
) -> web.Response:
content: list[dict[str, object]] = []
text_parts: list[str] = []
@@ -738,7 +840,7 @@ class CodexAnthropicGateway:
"api_error",
"Codex model transport failed",
)
- return web.json_response(
+ response = web.json_response(
{
"id": f"msg_codex_{uuid4().hex}",
"type": "message",
@@ -753,6 +855,48 @@ class CodexAnthropicGateway:
},
}
)
+ self._record_replay(
+ conversation,
+ replay_key,
+ _Replay(
+ status=response.status,
+ headers={"Content-Type": response.headers["Content-Type"]},
+ body=cast(bytes, response.body),
+ streamed=False,
+ expires_at=_now() + _REPLAY_TTL_SECONDS,
+ ),
+ )
+ return response
+
+ def _record_replay(
+ self,
+ conversation: _Conversation,
+ replay_key: str | None,
+ replay: _Replay,
+ ) -> None:
+ if replay_key is None or len(replay.body) > _MAX_REPLAY_BYTES:
+ return
+ replays = conversation.replays
+ replays.pop(replay_key, None)
+ replays[replay_key] = replay
+ stored = sum(len(entry.body) for entry in replays.values())
+ while len(replays) > _MAX_REPLAY_ENTRIES or stored > _MAX_REPLAY_BYTES:
+ stored -= len(replays.pop(next(iter(replays))).body)
+
+ def _take_replay(
+ self,
+ conversation: _Conversation,
+ replay_key: str,
+ *,
+ streamed: bool,
+ ) -> _Replay | None:
+ replay = conversation.replays.get(replay_key)
+ if replay is None or replay.streamed != streamed:
+ return None
+ if replay.expires_at <= _now():
+ conversation.replays.pop(replay_key, None)
+ return None
+ return replay
def _is_authorized(self, request: web.Request) -> bool:
authorization = request.headers.get("Authorization", "")
@@ -766,6 +910,26 @@ class CodexAnthropicGateway:
)
+class _RecordingSink:
+ """Mirrors the stream into a buffer so a retry can be answered verbatim."""
+
+ def __init__(self, response: web.StreamResponse) -> None:
+ self._response = response
+ self._chunks: list[bytes] = []
+
+ async def write(self, data: bytes) -> None:
+ self._chunks.append(data)
+ await self._response.write(data)
+
+ @property
+ def body(self) -> bytes:
+ return b"".join(self._chunks)
+
+
+def _now() -> float:
+ return asyncio.get_running_loop().time()
+
+
def _parse_tools(
value: object,
) -> tuple[list[CodexDynamicToolSpec], dict[str, str]]:
@@ -963,19 +1127,19 @@ def _safe_exception_message(
return message
-async def _write_sse(response: web.StreamResponse, event: dict[str, object]) -> None:
+async def _write_sse(sink: _SseSink, event: dict[str, object]) -> None:
event_type = str(event["type"])
payload = json.dumps(event, ensure_ascii=False, separators=(",", ":"))
- await response.write(f"event: {event_type}\ndata: {payload}\n\n".encode())
+ await sink.write(f"event: {event_type}\ndata: {payload}\n\n".encode())
async def _write_tool_use(
- response: web.StreamResponse,
+ sink: _SseSink,
index: int,
event: _ToolUse,
) -> None:
await _write_sse(
- response,
+ sink,
{
"type": "content_block_start",
"index": index,
@@ -988,7 +1152,7 @@ async def _write_tool_use(
},
)
await _write_sse(
- response,
+ sink,
{
"type": "content_block_delta",
"index": index,
@@ -999,25 +1163,25 @@ async def _write_tool_use(
},
)
await _write_sse(
- response,
+ sink,
{"type": "content_block_stop", "index": index},
)
async def _write_message_end(
- response: web.StreamResponse,
+ sink: _SseSink,
stop_reason: str,
output_tokens: int,
) -> None:
await _write_sse(
- response,
+ sink,
{
"type": "message_delta",
"delta": {"stop_reason": stop_reason, "stop_sequence": None},
"usage": {"output_tokens": output_tokens},
},
)
- await _write_sse(response, {"type": "message_stop"})
+ await _write_sse(sink, {"type": "message_stop"})
def _anthropic_error(status: int, error_type: str, message: str) -> web.Response:
diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway_test.py b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway_test.py
index 1425ee05fa..e9591cd58c 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway_test.py
@@ -8,6 +8,7 @@ from typing import cast
import pytest
from aiohttp import ClientSession
+from backend.copilot.sdk import codex_compat_gateway
from backend.copilot.sdk.codex_compat_gateway import (
CodexAnthropicGateway,
_safe_tool_name,
@@ -234,6 +235,18 @@ def _tool_result_request(
return payload
+def _streamed_tool_use_id(events: list[dict[str, object]]) -> str:
+ start = next(
+ event
+ for event in events
+ if event["type"] == "content_block_start"
+ and cast(dict[str, object], event["content_block"])["type"] == "tool_use"
+ )
+ call_id = cast(dict[str, object], start["content_block"])["id"]
+ assert isinstance(call_id, str)
+ return call_id
+
+
def _tool_use_id(payload: dict[str, object]) -> str:
content = payload["content"]
assert isinstance(content, list)
@@ -688,6 +701,12 @@ async def test_raw_codex_call_id_collisions_are_isolated_by_gateway_ids() -> Non
@pytest.mark.asyncio
async def test_duplicate_tool_result_request_is_claimed_once_without_new_turn() -> None:
+ """A concurrent duplicate waits for the accepted request's answer.
+
+ It used to lose the race with a 409; it now receives the same response,
+ and the invariant this test exists for -- one turn reaches the model --
+ is unchanged.
+ """
agent_session = _FakeAgentSession(use_tool=True)
transport = _FakeTransport(agent_session)
async with CodexAnthropicGateway(
@@ -716,19 +735,10 @@ async def test_duplicate_tool_result_request_is_claimed_once_without_new_turn()
json=continuation,
),
)
- payloads = await asyncio.gather(
- *(response.json() for response in responses)
- )
+ bodies = await asyncio.gather(*(response.read() for response in responses))
- assert sorted(response.status for response in responses) == [200, 409]
- duplicate_payload = next(
- payload
- for response, payload in zip(responses, payloads, strict=True)
- if response.status == 409
- )
- assert duplicate_payload["error"]["message"] == (
- "This tool-result request was already accepted"
- )
+ assert [response.status for response in responses] == [200, 200]
+ assert bodies[0] == bodies[1]
assert len(agent_session.requests) == 1
assert agent_session.tool_result == CodexDynamicToolResult(
content="one result",
@@ -824,3 +834,185 @@ def test_serialize_messages_keeps_inline_system_turns() -> None:
def test_serialize_messages_rejects_unknown_roles() -> None:
with pytest.raises(ValueError):
_serialize_messages([{"role": "tool", "content": "result"}])
+
+
+async def test_identical_tool_result_retry_replays_the_streamed_response() -> None:
+ agent_session = _FakeAgentSession(use_tool=True)
+ transport = _FakeTransport(agent_session)
+ async with CodexAnthropicGateway(
+ credential_lease=_lease(),
+ model="gpt-5.6-terra",
+ transport=transport,
+ ) as gateway:
+ async with ClientSession() as client:
+ first = await client.post(
+ f"{gateway.base_url}/v1/messages",
+ headers=_headers(gateway),
+ json=_tool_request("run", stream=True),
+ )
+ gateway_call_id = _streamed_tool_use_id(_events(await first.text()))
+ continuation = _tool_result_request(
+ gateway_call_id,
+ "one result",
+ stream=True,
+ )
+ accepted = await client.post(
+ f"{gateway.base_url}/v1/messages",
+ headers=_headers(gateway),
+ json=continuation,
+ )
+ accepted_body = await accepted.read()
+ retried = await client.post(
+ f"{gateway.base_url}/v1/messages",
+ headers=_headers(gateway),
+ json=continuation,
+ )
+ retried_body = await retried.read()
+
+ assert accepted.status == 200
+ assert retried.status == 200
+ assert retried_body == accepted_body
+ assert retried.headers["Content-Type"] == accepted.headers["Content-Type"]
+ assert len(agent_session.requests) == 1
+
+
+async def test_identical_tool_result_retry_replays_the_json_response() -> None:
+ agent_session = _FakeAgentSession(use_tool=True)
+ transport = _FakeTransport(agent_session)
+ async with CodexAnthropicGateway(
+ credential_lease=_lease(),
+ model="gpt-5.6-terra",
+ transport=transport,
+ ) as gateway:
+ async with ClientSession() as client:
+ first = await client.post(
+ f"{gateway.base_url}/v1/messages",
+ headers=_headers(gateway),
+ json=_tool_request("run"),
+ )
+ gateway_call_id = _tool_use_id(await first.json())
+ continuation = _tool_result_request(gateway_call_id, "one result")
+ accepted = await client.post(
+ f"{gateway.base_url}/v1/messages",
+ headers=_headers(gateway),
+ json=continuation,
+ )
+ accepted_body = await accepted.read()
+ retried = await client.post(
+ f"{gateway.base_url}/v1/messages",
+ headers=_headers(gateway),
+ json=continuation,
+ )
+ retried_body = await retried.read()
+
+ assert accepted.status == 200
+ assert retried.status == 200
+ assert retried_body == accepted_body
+ assert json.loads(retried_body)["id"] == json.loads(accepted_body)["id"]
+ assert len(agent_session.requests) == 1
+
+
+async def test_evicted_replay_falls_back_to_the_duplicate_error(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(codex_compat_gateway, "_MAX_REPLAY_ENTRIES", 0)
+ agent_session = _FakeAgentSession(use_tool=True)
+ transport = _FakeTransport(agent_session)
+ async with CodexAnthropicGateway(
+ credential_lease=_lease(),
+ model="gpt-5.6-terra",
+ transport=transport,
+ ) as gateway:
+ async with ClientSession() as client:
+ first = await client.post(
+ f"{gateway.base_url}/v1/messages",
+ headers=_headers(gateway),
+ json=_tool_request("run"),
+ )
+ gateway_call_id = _tool_use_id(await first.json())
+ continuation = _tool_result_request(gateway_call_id, "one result")
+ accepted = await client.post(
+ f"{gateway.base_url}/v1/messages",
+ headers=_headers(gateway),
+ json=continuation,
+ )
+ await accepted.read()
+ retried = await client.post(
+ f"{gateway.base_url}/v1/messages",
+ headers=_headers(gateway),
+ json=continuation,
+ )
+ retried_payload = await retried.json()
+
+ assert accepted.status == 200
+ assert retried.status == 409
+ assert retried_payload["error"]["message"] == (
+ "This tool-result request was already accepted"
+ )
+
+
+async def test_replay_entries_are_per_conversation_and_expire() -> None:
+ # A same-fingerprint request cannot cross conversations over HTTP -- the
+ # fingerprint covers the gateway tool_use ids, which are unique per
+ # conversation -- so the scoping is asserted on the store itself.
+ gateway = CodexAnthropicGateway(
+ agent_session=_FakeAgentSession(),
+ model="gpt-5.6-terra",
+ )
+ mine = codex_compat_gateway._Conversation(id="mine")
+ theirs = codex_compat_gateway._Conversation(id="theirs")
+ replay = codex_compat_gateway._Replay(
+ status=200,
+ headers={"Content-Type": "application/json"},
+ body=b"{}",
+ streamed=False,
+ expires_at=asyncio.get_running_loop().time() + 60,
+ )
+ gateway._record_replay(mine, "fingerprint", replay)
+
+ assert gateway._take_replay(mine, "fingerprint", streamed=False) is replay
+ assert gateway._take_replay(theirs, "fingerprint", streamed=False) is None
+ assert gateway._take_replay(mine, "fingerprint", streamed=True) is None
+
+ expired = replay.model_copy(
+ update={"expires_at": asyncio.get_running_loop().time() - 1}
+ )
+ gateway._record_replay(mine, "fingerprint", expired)
+ assert gateway._take_replay(mine, "fingerprint", streamed=False) is None
+ assert "fingerprint" not in mine.replays
+
+
+def test_replay_cache_evicts_the_oldest_entry_first() -> None:
+ gateway = CodexAnthropicGateway(
+ agent_session=_FakeAgentSession(),
+ model="gpt-5.6-terra",
+ )
+ conversation = codex_compat_gateway._Conversation(id="conversation")
+
+ def _entry(body: bytes) -> codex_compat_gateway._Replay:
+ return codex_compat_gateway._Replay(
+ status=200,
+ headers={},
+ body=body,
+ streamed=False,
+ expires_at=float("inf"),
+ )
+
+ for index in range(codex_compat_gateway._MAX_REPLAY_ENTRIES + 2):
+ gateway._record_replay(conversation, f"key-{index}", _entry(b"x"))
+
+ assert len(conversation.replays) == codex_compat_gateway._MAX_REPLAY_ENTRIES
+ assert "key-0" not in conversation.replays
+ assert "key-1" not in conversation.replays
+ assert f"key-{codex_compat_gateway._MAX_REPLAY_ENTRIES + 1}" in conversation.replays
+
+ gateway._record_replay(
+ conversation,
+ "oversized",
+ _entry(b"x" * (codex_compat_gateway._MAX_REPLAY_BYTES + 1)),
+ )
+ assert "oversized" not in conversation.replays