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): stop the replay recorder from buffering past its cap on a streaming response (#14764)
Reinier van der Leer9f18dda31e85
diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.pyindex 16f083d80d..02c14dc003 100644--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py@@ -660,17 +660,19 @@ class CodexAnthropicGateway: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,- ),- )+ body = sink.body+ if body is not None:+ self._record_replay(+ conversation,+ replay_key,+ _Replay(+ status=200,+ headers=headers,+ body=body,+ streamed=True,+ expires_at=_now() + _REPLAY_TTL_SECONDS,+ ),+ )return responseasync def _write_boundary(@@ -916,14 +918,25 @@ class _RecordingSink:def __init__(self, response: web.StreamResponse) -> None:self._response = responseself._chunks: list[bytes] = []+ self._buffered = 0+ self._recording = Trueasync def write(self, data: bytes) -> None:- self._chunks.append(data)+ if self._recording:+ self._buffered += len(data)+ # A body past the cap can never be stored, so holding on to it+ # would buffer an unbounded stream for a replay nobody can keep.+ if self._buffered > _MAX_REPLAY_BYTES:+ self._chunks.clear()+ self._recording = False+ else:+ self._chunks.append(data)await self._response.write(data)@property- def body(self) -> bytes:- return b"".join(self._chunks)+ def body(self) -> bytes | None:+ """None once the stream outgrew the cap; a partial body must not be cached."""+ return b"".join(self._chunks) if self._recording else Nonedef _now() -> float: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.pyindex e9591cd58c..d765835bf4 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@@ -6,7 +6,7 @@ from contextlib import asynccontextmanagerfrom typing import castimport pytest-from aiohttp import ClientSession+from aiohttp import ClientSession, webfrom backend.copilot.sdk import codex_compat_gatewayfrom backend.copilot.sdk.codex_compat_gateway import (@@ -986,6 +986,112 @@ async def test_replay_entries_are_per_conversation_and_expire() -> None:assert "fingerprint" not in mine.replays+async def test_retry_with_a_changed_system_prompt_still_replays() -> None:+ # The continuation applies neither `system` nor `tools`, so a recomposed+ # system prompt is the same tool-result submission and must still replay.+ 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, "system": "recomposed for the retry"},+ )+ retried_body = await retried.read()++ assert accepted.status == 200+ assert retried.status == 200+ assert retried_body == accepted_body+ assert len(agent_session.requests) == 1+++async def test_recording_sink_drops_the_buffer_past_the_replay_cap(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(codex_compat_gateway, "_MAX_REPLAY_BYTES", 8)+ forwarded: list[bytes] = []++ class _Response:+ async def write(self, data: bytes) -> None:+ forwarded.append(data)++ sink = codex_compat_gateway._RecordingSink(cast(web.StreamResponse, _Response()))+ await sink.write(b"12345")++ assert sink.body == b"12345"++ await sink.write(b"67890")++ assert sink.body is None+ assert sink._chunks == []+ assert forwarded == [b"12345", b"67890"]+++async def test_a_stream_past_the_replay_cap_is_forwarded_whole_and_not_cached(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(codex_compat_gateway, "_MAX_REPLAY_BYTES", 8)+ 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_events = _events(await accepted.text())+ 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 [event["type"] for event in accepted_events][-1] == "message_stop"+ assert retried.status == 409+ assert retried_payload["error"]["message"] == (+ "This tool-result request was already accepted"+ )++def test_replay_cache_evicts_the_oldest_entry_first() -> None:gateway = CodexAnthropicGateway(agent_session=_FakeAgentSession(),