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/copilot): deliver mid-task messages to ChatGPT, and name the account and reset time on its limits (#15387)
Nicholas Tindlebc1725941a2d
diff --git a/autogpt_platform/backend/backend/copilot/pending_messages.py b/autogpt_platform/backend/backend/copilot/pending_messages.pyindex 3fe1e4b7dd..1657039e15 100644--- a/autogpt_platform/backend/backend/copilot/pending_messages.py+++ b/autogpt_platform/backend/backend/copilot/pending_messages.py@@ -458,6 +458,19 @@ async def clear_pending_messages_unsafe(session_id: str) -> None:_FOLLOWUP_CONTENT_MAX_CHARS = 2_000_FOLLOWUP_TOTAL_MAX_CHARS = 6_000+# The follow-up block's tags. The Codex gateway finds the block by them, to+# pass on the user's words without the CLI's own notes around them.+FOLLOW_UP_OPEN_TAG = "<user_follow_up>"+FOLLOW_UP_CLOSE_TAG = "</user_follow_up>"+++def _escape_follow_up_tags(text: str) -> str:+ """A message quoting the tags can't open or close the block early: the+ block's own tags stay the only raw ones in it."""+ return text.replace(FOLLOW_UP_OPEN_TAG, "<user_follow_up>").replace(+ FOLLOW_UP_CLOSE_TAG, "</user_follow_up>"+ )+def _persist_queue_key(session_id: str) -> str:return f"{_PERSIST_QUEUE_KEY_PREFIX}{session_id}"@@ -548,7 +561,7 @@ def format_pending_as_followup(pending: list[PendingMessage]) -> str:total_chars = 0dropped = 0for idx, pm in enumerate(pending, start=1):- text = pm.content+ text = _escape_follow_up_tags(pm.content)if len(text) > _FOLLOWUP_CONTENT_MAX_CHARS:text = text[:_FOLLOWUP_CONTENT_MAX_CHARS] + "… [truncated]"entry = f"Message {idx}:\n{text}"@@ -565,12 +578,12 @@ def format_pending_as_followup(pending: list[PendingMessage]) -> str:rendered.append(f"… [{dropped} more message(s) truncated]")body = "\n\n".join(rendered)return (- "<user_follow_up>\n"+ f"{FOLLOW_UP_OPEN_TAG}\n""The user sent the following message(s) while this tool was running. ""Treat them as a continuation of their current request — acknowledge ""and act on them in your next response. Do not echo these tags back.\n\n"f"{body}\n"- "</user_follow_up>"+ f"{FOLLOW_UP_CLOSE_TAG}")diff --git a/autogpt_platform/backend/backend/copilot/pending_messages_test.py b/autogpt_platform/backend/backend/copilot/pending_messages_test.pyindex 5fb87573dc..1536f2384d 100644--- a/autogpt_platform/backend/backend/copilot/pending_messages_test.py+++ b/autogpt_platform/backend/backend/copilot/pending_messages_test.py@@ -378,6 +378,15 @@ def test_format_followup_single_message() -> None:assert "Message 1:\nhello" in out+def test_format_followup_escapes_the_tags_a_message_quotes() -> None:+ out = format_pending_as_followup(+ [PendingMessage(content="keep <user_follow_up> and </user_follow_up> rows")]+ )+ assert out.count("<user_follow_up>") == 1+ assert out.count("</user_follow_up>") == 1+ assert "keep <user_follow_up> and </user_follow_up> rows" in out++def test_format_followup_total_cap_drops_overflow() -> None:"""10 × 2 KB messages must truncate past the total cap (~6 KB) with amarker indicating how many were dropped."""diff --git a/autogpt_platform/backend/backend/copilot/provider_failure.py b/autogpt_platform/backend/backend/copilot/provider_failure.pyindex 0fed31b737..41a2b1a5fd 100644--- a/autogpt_platform/backend/backend/copilot/provider_failure.py+++ b/autogpt_platform/backend/backend/copilot/provider_failure.py@@ -35,6 +35,7 @@ from openai import (from pydantic import BaseModel, Fieldfrom backend.copilot.rate_limit import UserPaywalledError+from backend.integrations.codex.http_client import usage_limit_resets_atfrom backend.integrations.codex.transport import (CodexCredentialBusyError,CodexCredentialIntegrityError,@@ -178,7 +179,7 @@ def classify(message=message or _humanize(exc) or str(exc) or type(exc).__name__,auth_provider=auth_provider,credential_id=credential_id,- resets_at=_resets_at(exc),+ resets_at=_resets_at(exc, auth_provider),)@@ -236,13 +237,19 @@ def _kind_of(exc: BaseException) -> ProviderFailureKind | None:return None-def _resets_at(exc: BaseException) -> int | None:+def _resets_at(exc: BaseException, auth_provider: str | None = None) -> int | None:"""A reset time only if the provider actually sent one.- Read from the standard ``x-ratelimit-reset-*`` response headers. Anything- unparseable is dropped rather than approximated -- a wrong time is worse- than no time, because the user schedules around it.+ Read from the standard ``x-ratelimit-reset-*`` response headers -- or, for+ a spent ChatGPT quota, which sends none of those, from the 429's own body+ and quota headers. Anything unparseable is dropped rather than+ approximated -- a wrong time is worse than no time, because the user+ schedules around it."""+ if auth_provider == "codex" and isinstance(exc, RateLimitError):+ reported = usage_limit_resets_at(exc.response.headers, exc.body)+ if reported is not None:+ return reportedresponse = getattr(exc, "response", None)headers = getattr(response, "headers", None)if headers is None:diff --git a/autogpt_platform/backend/backend/copilot/provider_failure_test.py b/autogpt_platform/backend/backend/copilot/provider_failure_test.pyindex a8717dd3ff..30f057306f 100644--- a/autogpt_platform/backend/backend/copilot/provider_failure_test.py+++ b/autogpt_platform/backend/backend/copilot/provider_failure_test.py@@ -17,10 +17,15 @@ from backend.copilot.rate_limit import UserPaywalledErrorfrom backend.util.exceptions import ExecutionFailureReason-def _api_error(cls, status: int, headers: dict[str, str] | None = None):+def _api_error(+ cls,+ status: int,+ headers: dict[str, str] | None = None,+ body: object = None,+):request = httpx.Request("POST", "https://api.example.com/v1/chat/completions")response = httpx.Response(status, headers=headers or {}, request=request)- return cls("boom", response=response, body=None)+ return cls("boom", response=response, body=body)class TestWhatTheFailureIs:@@ -118,6 +123,37 @@ class TestWhenTheLimitLifts:assert classify(_api_error(RateLimitError, 429)).resets_at is None+class TestWhenAChatGPTLimitLifts:+ """ChatGPT reports its reset in the 429 itself, not ``x-ratelimit-reset-*``."""++ _FULL_WINDOW = {+ "x-codex-primary-used-percent": "100",+ "x-codex-primary-window-minutes": "300",+ "x-codex-primary-reset-at": "1788400000",+ }++ def test_the_error_body_names_the_reset(self) -> None:+ exc = _api_error(+ RateLimitError,+ 429,+ self._FULL_WINDOW,+ body={"type": "usage_limit_reached", "resets_at": 1788452827},+ )+ failure = classify(exc, auth_provider="codex")+ assert failure is not None+ assert failure.as_part()["resetsAt"] == 1788452827++ def test_the_quota_headers_name_it_when_the_body_does_not(self) -> None:+ exc = _api_error(RateLimitError, 429, self._FULL_WINDOW)+ assert classify(exc, auth_provider="codex").resets_at == 1788400000++ def test_another_route_is_not_read_as_chatgpt(self) -> None:+ exc = _api_error(+ RateLimitError, 429, self._FULL_WINDOW, body={"resets_at": 1788452827}+ )+ assert classify(exc, auth_provider="platform").resets_at is None++class TestWhichConnectionFailed:def test_the_envelope_names_the_connection_that_refused(self) -> None:failure = classify(diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_cli_test.py b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_cli_test.pyindex 2b997f3c33..f09f3cd4b6 100644--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_cli_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_cli_test.py@@ -7,11 +7,13 @@ import pytestfrom claude_agent_sdk import (ClaudeAgentOptions,ClaudeSDKClient,+ HookMatcher,ResultMessage,create_sdk_mcp_server,tool,)+from backend.copilot.pending_messages import PendingMessage, format_pending_as_followupfrom backend.copilot.sdk.codex_compat_gateway import CodexAnthropicGatewayfrom backend.copilot.sdk.env import build_sdk_envfrom backend.integrations.codex.models import (@@ -152,6 +154,9 @@ async def test_bundled_claude_cli_executes_mcp_tool_through_codex_gateway(assert agent_session.tool_result is not Noneassert agent_session.tool_result.successassert "echo:ping" in agent_session.tool_result.content+ # The CLI still sends its own token count after the round; that is not a+ # message from the user and must not reach Codex as one.+ assert agent_session.tool_result.followup is Noneassert any(isinstance(message, ResultMessage) and not message.is_errorfor message in received@@ -159,3 +164,80 @@ async def test_bundled_claude_cli_executes_mcp_tool_through_codex_gateway(assert gateway.result is not Noneassert gateway.result.usage is not Noneassert gateway.result.usage.total_tokens == 25+++async def test_bundled_claude_cli_delivers_a_post_tool_use_followup_to_codex(+ tmp_path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A message the user sends mid-turn reaches a ChatGPT model.++ The PostToolUse hook hands queued messages to the CLI, which sends them+ after the tool result rather than inside it -- for a non-Claude model, as+ a separate ``system`` message under the hook's label, which newer builds+ follow with the CLI's own token count. Exactly the queued block must+ reach Codex as a follow-up, leaving the tool's own output untouched.+ """+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path / "claude-config"))+ stderr_lines: list[str] = []+ queued = format_pending_as_followup([PendingMessage(content="also check the logs")])++ @tool("echo", "Echo a value", {"value": str})+ async def echo(arguments):+ return {"content": [{"type": "text", "text": f"echo:{arguments['value']}"}]}++ async def deliver_followup(_input_data, _tool_use_id, _context):+ return {+ "hookSpecificOutput": {+ "hookEventName": "PostToolUse",+ "additionalContext": queued,+ }+ }++ agent_session = _ClaudeHarnessAgentSession()+ gateway = CodexAnthropicGateway(+ credential_lease=cast(CredentialLease, object()),+ model="gpt-5.6-terra",+ transport=_ClaudeHarnessTransport(agent_session),+ )++ async with gateway:+ options = ClaudeAgentOptions(+ model="gpt-5.6-terra",+ system_prompt="Use the supplied echo tool once.",+ mcp_servers={+ "codex-conformance": create_sdk_mcp_server(+ name="codex-conformance", tools=[echo]+ )+ },+ allowed_tools=["mcp__codex-conformance__echo"],+ setting_sources=[],+ hooks={"PostToolUse": [HookMatcher(hooks=[deliver_followup])]},+ cwd=str(tmp_path),+ max_turns=3,+ stderr=stderr_lines.append,+ env=build_sdk_env(+ session_id="codex-cli-followup",+ sdk_cwd=str(tmp_path),+ model="gpt-5.6-terra",+ codex_gateway_url=gateway.base_url,+ codex_gateway_token=gateway.auth_token,+ ),+ )+ async with ClaudeSDKClient(options=options) as client:+ await client.query("Call echo with ping, then report completion.")+ try:+ async with asyncio.timeout(45):+ async for _ in client.receive_response():+ pass+ except TimeoutError as exc:+ raise AssertionError(+ f"Claude CLI did not finish; stderr={stderr_lines!r}"+ ) from exc++ result = agent_session.tool_result+ assert result is not None+ assert result.content == "echo:ping"+ assert result.followup == queueddiff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.pyindex 02c14dc003..f7b3695f5e 100644--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py@@ -14,6 +14,7 @@ from uuid import uuid4from aiohttp import webfrom pydantic import BaseModel, ConfigDict, Field+from backend.copilot.pending_messages import FOLLOW_UP_CLOSE_TAG, FOLLOW_UP_OPEN_TAGfrom backend.copilot.provider_failure import ProviderFailurefrom backend.copilot.provider_failure import classify as classify_provider_failurefrom backend.copilot.provider_failure import status_for@@ -29,6 +30,7 @@ from backend.integrations.codex.models import ()from backend.integrations.codex.transport import (CodexAgentSession,+ CodexCredentialLease,CodexTransport,get_codex_transport,)@@ -62,6 +64,19 @@ _SECRET_ASSIGNMENT_PATTERN = re.compile(r"\s*[\"']?\s*[:=]\s*(?:Bearer\s+)?"r"(?:\"(?:\\.|[^\"])*\"|'(?:\\.|[^'])*'|[^\s,;]+)")+# A queued user message the PostToolUse hook injected, wrapper included.+# `format_pending_as_followup` escapes the tags inside each message, so a+# block's own tags are the only raw ones in it. Greedy up to the next opening+# tag, never past it, so two blocks never merge into one match that carries+# the CLI's text between them.+_FOLLOW_UP_BLOCK = re.compile(+ re.escape(FOLLOW_UP_OPEN_TAG)+ + r"(?:(?!"+ + re.escape(FOLLOW_UP_OPEN_TAG)+ + r").)*"+ + re.escape(FOLLOW_UP_CLOSE_TAG),+ re.DOTALL,+)logger = logging.getLogger(__name__)@@ -170,6 +185,7 @@ class CodexAnthropicGateway:*,credential_lease: CredentialLease | None = None,agent_session: _AgentSession | None = None,+ credential_id: str | None = None,model: str,effort: CodexReasoningEffort | None = None,transport: CodexTransport | _AgentTransport | None = None,@@ -179,6 +195,9 @@ class CodexAnthropicGateway:"Codex gateway requires a credential lease or agent session")self._credential_lease = credential_lease+ # Named on every failure so the client can tell which ChatGPT account+ # refused. A bare agent session has no lease to read it from.+ self._credential_id = credential_id# The last failure this gateway named, kept so the service layer can# report *what* went wrong. By the time a failure reaches the CLI it# is an HTTP status and a sentence; the typed exception only exists@@ -480,12 +499,7 @@ class CodexAnthropicGateway:if any(record.conversation is not conversation for record, _ in claimable):raise ValueError("Tool results span multiple model conversations")- for record, result in claimable:- record.result = result- record.claim_fingerprint = fingerprint- for record, result in claimable:- if not record.future.done():- record.future.set_result(result)+ _claim(claimable, fingerprint, payload.get("messages"))return _Continuation(conversation=conversation, replay_key=fingerprint)def _start_conversation(self, payload: dict[str, object]) -> _Conversation:@@ -584,7 +598,8 @@ class CodexAnthropicGateway:failure = classify_provider_failure(failed.error,auth_provider="codex",- credential_id=(+ credential_id=self._credential_id+ or (self._credential_lease.credentials.idif self._credential_lease is not Noneelse None@@ -912,6 +927,27 @@ class CodexAnthropicGateway:)+def gateway_for_lease(+ lease: CredentialLease | CodexCredentialLease,+ *,+ model: str,+ effort: CodexReasoningEffort | None,+) -> CodexAnthropicGateway:+ """The gateway for a ChatGPT turn, told which account it spends.++ A failure it reports names the account, so the client offers the user's+ other ChatGPT accounts rather than hiding every one of them.+ """+ if isinstance(lease, CodexCredentialLease):+ return CodexAnthropicGateway(+ agent_session=lease,+ credential_id=lease.credentials.id,+ model=model,+ effort=effort,+ )+ return CodexAnthropicGateway(credential_lease=lease, model=model, effort=effort)++class _RecordingSink:"""Mirrors the stream into a buffer so a retry can be answered verbatim."""@@ -1089,6 +1125,77 @@ def _extract_tool_results(value: object) -> dict[str, CodexDynamicToolResult]:return results+def _claim(+ claimable: list[tuple[_ToolCallRecord, CodexDynamicToolResult]],+ fingerprint: str,+ messages: object,+) -> None:+ """Hand each result to the Codex call waiting on it.++ ``record.result`` stays exactly what the CLI sent as the result, since+ every later request is checked against it. The user's queued messages+ that came with the results travel with the last of them, so Codex reads+ them once.+ """+ followup = _followup_after_results(+ messages, [record.gateway_call_id for record, _ in claimable]+ )+ for record, result in claimable:+ record.result = result+ record.claim_fingerprint = fingerprint+ last = claimable[-1][0]+ for record, result in claimable:+ if not record.future.done():+ record.future.set_result(+ result.model_copy(update={"followup": followup})+ if record is last+ else result+ )+++def _followup_after_results(messages: object, call_ids: list[str]) -> str | None:+ """The user's queued messages the CLI sent with these tool results.++ The PostToolUse hook hands them over as ``<user_follow_up>`` blocks in its+ context, which the CLI sends after the result: for a non-Claude model+ name, in a ``system`` message after the result's message (CLI 2.1.284 and+ 2.1.296); older builds, in a text block beside the result. Only the blocks+ are passed on. The CLI's own notes can ride the same way (2.1.296 adds a+ ``<total_tokens>`` count every round) and must not become user turns. A+ block inside the result itself, the CLI's shape for Claude model names,+ is not read here: the result is forwarded whole, so Codex already has it.+ """+ if not isinstance(messages, list):+ return None+ start = next(+ (+ index+ for index, message in enumerate(messages)+ if _carries_tool_result(message, call_ids)+ ),+ None,+ )+ if start is None:+ return None+ texts = [+ _content_text(message.get("content"))+ for message in messages[start:]+ if isinstance(message, dict) and message.get("role") != "assistant"+ ]+ blocks = [block for text in texts for block in _FOLLOW_UP_BLOCK.findall(text)]+ return "\n\n".join(blocks) or None+++def _carries_tool_result(message: object, call_ids: list[str]) -> bool:+ content = message.get("content") if isinstance(message, dict) else None+ return isinstance(content, list) and any(+ isinstance(block, dict)+ and block.get("type") == "tool_result"+ and block.get("tool_use_id") in call_ids+ for block in content+ )++def _tool_result_request_fingerprint(payload: dict[str, object]) -> str:serialized = json.dumps(payload.get("messages"),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 082e7bb077..7a854bf02f 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 castimport pytestfrom aiohttp import ClientSession, web+from backend.copilot.pending_messages import PendingMessage, format_pending_as_followupfrom backend.copilot.sdk import codex_compat_gatewayfrom backend.copilot.sdk.codex_compat_gateway import (CodexAnthropicGateway,@@ -134,6 +135,36 @@ class _CollidingCallSession:return _result(f"completed:{result.content}")+class _TwoRoundSession:+ """Codex calling a tool, then another, as a long turn does."""++ def __init__(self) -> None:+ self.tool_results: list[CodexDynamicToolResult] = []++ async def invoke(+ self,+ request,+ dynamic_tools,+ tool_handler,+ event_handler=None,+ ) -> CodexInvocationResult:+ for round_index in range(2):+ self.tool_results.append(+ await tool_handler(+ CodexDynamicToolCall(+ thread_id="thread-1",+ turn_id="turn-1",+ call_id=f"call-{round_index}",+ tool=dynamic_tools[0].name,+ arguments={"round": round_index},+ )+ )+ )+ assert event_handler is not None+ await event_handler(CodexStreamEvent(type="text_delta", delta="done"))+ return _result("done")++class _FakeTransport:def __init__(self, session: _FakeAgentSession) -> None:self.session = session@@ -235,6 +266,90 @@ def _tool_result_request(return payload+_USER_FOLLOW_UP = format_pending_as_followup(+ [PendingMessage(content="also check the logs")]+)+_TOTAL_TOKENS = "<total_tokens>14989339 tokens left</total_tokens>"+# What the CLI sends with a tool result whose PostToolUse hook returned a+# queued message: the hook's label and context, then (CLI 2.1.296) its own+# token count.+_HOOK_CONTEXT = (+ "PostToolUse:mcp__copilot__run_block hook additional context: "+ f"{_USER_FOLLOW_UP}\n\n{_TOTAL_TOKENS}"+)+++def _with_cli_context(+ payload: dict[str, object],+ shape: str,+ text: str = _HOOK_CONTEXT,+) -> dict[str, object]:+ """Add what the CLI sends with a tool result, in one of its shapes.++ Given a non-Claude model name, as on the ChatGPT route, the CLI sends it+ as a ``system`` message after the tool_result's message: a text block on+ 2.1.284, which claude-agent-sdk 0.2.161 bundles, a string on 2.1.296.+ Older builds put it in a text block beside the tool_result; given a Claude+ model name, the CLI folds it into the tool_result's own content.+ """+ messages = cast(list[dict[str, object]], payload["messages"])+ if shape == "system-message":+ messages.append({"role": "system", "content": text})+ return payload+ if shape == "system-message-blocks":+ messages.append({"role": "system", "content": [{"type": "text", "text": text}]})+ return payload+ reminder = {+ "type": "text",+ "text": f"<system-reminder>\n{text}\n</system-reminder>",+ }+ content = cast(list[dict[str, object]], messages[-1]["content"])+ if shape == "beside-result":+ content.append(reminder)+ return payload+ result = content[0]+ result["content"] = [{"type": "text", "text": str(result["content"])}, reminder]+ return payload+++async def _answer_first_tool_call(+ client: ClientSession,+ gateway: CodexAnthropicGateway,+ shape: str,+ text: str = _HOOK_CONTEXT,+) -> dict[str, object]:+ """Start a turn, then build the request that answers its tool call."""+ 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()))+ return _with_cli_context(+ _tool_result_request(gateway_call_id, "one result", stream=True),+ shape,+ text,+ )+++def _tool_use_message(call_id: str) -> dict[str, object]:+ return {+ "role": "assistant",+ "content": [+ {"type": "tool_use", "id": call_id, "name": "mcp.tool/status", "input": {}}+ ],+ }+++def _tool_result_message(call_id: str, content: str) -> dict[str, object]:+ return {+ "role": "user",+ "content": [+ {"type": "tool_result", "tool_use_id": call_id, "content": content}+ ],+ }++def _streamed_tool_use_id(events: list[dict[str, object]]) -> str:start = next(event@@ -1050,6 +1165,232 @@ async def test_retry_with_a_changed_system_prompt_still_replays() -> None:assert len(agent_session.requests) == 1+ "shape, expected",+ [+ (+ "system-message",+ CodexDynamicToolResult(content="one result", followup=_USER_FOLLOW_UP),+ ),+ (+ "system-message-blocks",+ CodexDynamicToolResult(content="one result", followup=_USER_FOLLOW_UP),+ ),+ (+ "beside-result",+ CodexDynamicToolResult(content="one result", followup=_USER_FOLLOW_UP),+ ),+ # The result is forwarded whole, so Codex reads the message in the+ # tool's output and a follow-up would say it twice.+ (+ "inside-result",+ CodexDynamicToolResult(+ content=f"one result\n<system-reminder>\n{_HOOK_CONTEXT}\n"+ "</system-reminder>"+ ),+ ),+ ],+)+async def test_only_the_users_queued_messages_reach_codex_as_a_followup(+ shape: str,+ expected: CodexDynamicToolResult,+) -> None:+ """A message the user sends mid-turn rides in with a tool result.++ The PostToolUse hook drains it from the queue, so if the gateway drops it+ here it is gone. It reaches Codex as its own follow-up, wrapper and all,+ without the hook's label or the CLI's token count around it.+ """+ agent_session = _FakeAgentSession(use_tool=True)+ async with CodexAnthropicGateway(+ credential_lease=_lease(),+ model="gpt-5.6-terra",+ transport=_FakeTransport(agent_session),+ ) as gateway:+ async with ClientSession() as client:+ continuation = await _answer_first_tool_call(client, gateway, shape)+ continued = await client.post(+ f"{gateway.base_url}/v1/messages",+ headers=_headers(gateway),+ json=continuation,+ )+ await continued.read()++ assert continued.status == 200+ assert agent_session.tool_result == expected+++async def test_a_round_with_only_the_clis_own_note_forwards_nothing() -> None:+ """The CLI adds a token count after every tool round. It is not the user."""+ agent_session = _FakeAgentSession(use_tool=True)+ async with CodexAnthropicGateway(+ credential_lease=_lease(),+ model="gpt-5.6-terra",+ transport=_FakeTransport(agent_session),+ ) as gateway:+ async with ClientSession() as client:+ continuation = await _answer_first_tool_call(+ client, gateway, "system-message", _TOTAL_TOKENS+ )+ continued = await client.post(+ f"{gateway.base_url}/v1/messages",+ headers=_headers(gateway),+ json=continuation,+ )+ await continued.read()++ assert continued.status == 200+ assert agent_session.tool_result == CodexDynamicToolResult(content="one result")+++async def test_a_retried_followup_request_replays_and_reaches_codex_once() -> None:+ agent_session = _FakeAgentSession(use_tool=True)+ async with CodexAnthropicGateway(+ credential_lease=_lease(),+ model="gpt-5.6-terra",+ transport=_FakeTransport(agent_session),+ ) as gateway:+ async with ClientSession() as client:+ continuation = await _answer_first_tool_call(+ client, gateway, "system-message"+ )+ 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, retried.status] == [200, 200]+ assert retried_body == accepted_body+ assert len(agent_session.requests) == 1+ assert agent_session.tool_result is not None+ assert agent_session.tool_result.followup == _USER_FOLLOW_UP+++async def test_the_round_after_a_followup_is_not_a_conflict() -> None:+ """The follow-up stays in the history; the stored result must still match.++ Every request re-checks earlier tool results against what was claimed, so+ the claimed result has to stay exactly what the CLI sent as the result.+ """+ agent_session = _TwoRoundSession()+ transport = _FakeTransport(agent_session)+ async with CodexAnthropicGateway(+ credential_lease=_lease(),+ model="gpt-5.6-terra",+ transport=transport,+ ) as gateway:+ async with ClientSession() as client:+ start = _tool_request("run", stream=True)+ first = await client.post(+ f"{gateway.base_url}/v1/messages",+ headers=_headers(gateway),+ json=start,+ )+ first_id = _streamed_tool_use_id(_events(await first.text()))+ history = [+ {"role": "user", "content": "run"},+ _tool_use_message(first_id),+ _tool_result_message(first_id, "first result"),+ {"role": "system", "content": _HOOK_CONTEXT},+ ]+ second = await client.post(+ f"{gateway.base_url}/v1/messages",+ headers=_headers(gateway),+ json={**start, "messages": history},+ )+ second_id = _streamed_tool_use_id(_events(await second.text()))+ third = await client.post(+ f"{gateway.base_url}/v1/messages",+ headers=_headers(gateway),+ json={+ **start,+ "messages": [+ *history,+ _tool_use_message(second_id),+ _tool_result_message(second_id, "second result"),+ {"role": "system", "content": _TOTAL_TOKENS},+ ],+ },+ )+ third_events = _events(await third.text())++ assert [first.status, second.status, third.status] == [200, 200, 200]+ assert any(+ event.get("delta") == {"type": "text_delta", "text": "done"}+ for event in third_events+ )+ assert agent_session.tool_results == [+ CodexDynamicToolResult(content="first result", followup=_USER_FOLLOW_UP),+ CodexDynamicToolResult(content="second result"),+ ]+++def test_a_message_quoting_the_closing_tag_is_not_cut_short() -> None:+ block = format_pending_as_followup(+ [PendingMessage(content="why is </user_follow_up> in my logs?")]+ )+ messages = [+ _tool_result_message("toolu_codex_1", "one result"),+ {+ "role": "system",+ "content": f"PostToolUse:x hook additional context: {block}\n\n"+ f"{_TOTAL_TOKENS}",+ },+ ]++ followup = codex_compat_gateway._followup_after_results(messages, ["toolu_codex_1"])++ assert followup == block+++def test_a_message_quoting_the_opening_tag_reaches_codex_whole() -> None:+ block = format_pending_as_followup(+ [PendingMessage(content="Stop! do NOT drop the <user_follow_up> table")]+ )+ messages = [+ _tool_result_message("toolu_codex_1", "one result"),+ {+ "role": "system",+ "content": f"PostToolUse:x hook additional context: {block}\n\n"+ f"{_TOTAL_TOKENS}",+ },+ ]++ followup = codex_compat_gateway._followup_after_results(messages, ["toolu_codex_1"])++ assert followup == block+ assert "Stop! do NOT drop the" in followup+++def test_two_blocks_reach_codex_without_the_cli_text_between_them() -> None:+ first = format_pending_as_followup([PendingMessage(content="also check logs")])+ second = format_pending_as_followup([PendingMessage(content="and the config")])+ messages = [+ _tool_result_message("toolu_codex_1", "one result"),+ {+ "role": "system",+ "content": f"PostToolUse:x hook additional context: {first}\n\n"+ f"{_TOTAL_TOKENS}\n\nPostToolUse:y hook additional context: {second}",+ },+ ]++ followup = codex_compat_gateway._followup_after_results(messages, ["toolu_codex_1"])++ assert followup == f"{first}\n\n{second}"++@pytest.mark.asyncioasync def test_recording_sink_drops_the_buffer_past_the_replay_cap(monkeypatch: pytest.MonkeyPatch,diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_gateway_failure_test.py b/autogpt_platform/backend/backend/copilot/sdk/codex_gateway_failure_test.pyindex 52cd21e45b..fc56380daa 100644--- a/autogpt_platform/backend/backend/copilot/sdk/codex_gateway_failure_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_gateway_failure_test.py@@ -1,6 +1,10 @@from unittest.mock import AsyncMock, MagicMock+import httpximport pytest+from aiohttp import ClientSession+from openai import AsyncOpenAI, RateLimitError+from pydantic import SecretStrfrom backend.copilot.provider_failure import ProviderFailureKindfrom backend.copilot.sdk.codex_compat_gateway import (@@ -8,13 +12,38 @@ from backend.copilot.sdk.codex_compat_gateway import (_Conversation,_Failed,_TextDelta,+ gateway_for_lease,)+from backend.data.model import OAuth2Credentials+from backend.integrations.codex.http_session import CodexHttpSessionfrom backend.integrations.codex.transport import (CodexCredentialIntegrityError,+ CodexCredentialLease,CodexTransportError,CodexTransportOverloadedError,)+_RESETS_AT = 1788452827+++def _spent_quota(_request: httpx.Request) -> httpx.Response:+ """What ChatGPT answers once a subscription's quota is spent."""+ return httpx.Response(+ 429,+ headers={+ "x-codex-primary-used-percent": "100",+ "x-codex-primary-window-minutes": "300",+ "x-codex-primary-reset-at": "1788400000",+ },+ json={+ "error": {+ "type": "usage_limit_reached",+ "message": "The usage limit has been reached",+ "resets_at": _RESETS_AT,+ }+ },+ )+def _gateway() -> CodexAnthropicGateway:return CodexAnthropicGateway(@@ -79,3 +108,86 @@ class TestTheGatewayNamesItsFailures:assert gw.last_failure.kind is ProviderFailureKind.INVALID_CREDENTIALwire = b"".join(call.args[0] for call in response.write.await_args_list)assert b'"type":"invalid_credential"' in wire+++class TestASpentChatGPTQuota:+ """The switch offer needs the account; the limit card needs the reset.++ Without the account the client hides every ChatGPT connection, since it+ cannot tell which one refused, and drops the offer on reload. Without the+ reset it can only say "wait", not for how long.+ """++ @pytest.mark.asyncio+ async def test_names_the_account_and_when_it_lifts(self) -> None:+ async with httpx.AsyncClient(+ transport=httpx.MockTransport(_spent_quota)+ ) as http:+ session = CodexHttpSession(+ OAuth2Credentials(+ provider="codex",+ title="ChatGPT",+ access_token=SecretStr("access"),+ scopes=[],+ ),+ turn_timeout_seconds=30,+ tool_timeout_seconds=5,+ client=AsyncOpenAI(+ api_key="test",+ base_url="https://chatgpt.test",+ http_client=http,+ max_retries=0,+ ),+ )+ async with CodexAnthropicGateway(+ agent_session=session,+ credential_id="cred-chatgpt-2",+ model="gpt-5.6-terra",+ ) as gateway:+ async with ClientSession() as client:+ response = await client.post(+ f"{gateway.base_url}/v1/messages",+ headers={"Authorization": f"Bearer {gateway.auth_token}"},+ json={+ "stream": True,+ "messages": [{"role": "user", "content": "hello"}],+ },+ )+ await response.read()++ assert response.status == 429+ failure = gateway.last_failure+ assert failure is not None+ assert failure.kind is ProviderFailureKind.USAGE_LIMIT+ part = failure.as_part()+ assert part["authProvider"] == "codex"+ assert part["credentialId"] == "cred-chatgpt-2"+ assert part["resetsAt"] == _RESETS_AT++ def test_a_gateway_built_for_a_lease_names_its_account(self) -> None:+ # The service builds the gateway from the lease as an agent session,+ # which carries no credential of its own to name.+ credentials = OAuth2Credentials(+ id="cred-chatgpt-1",+ provider="codex",+ title="ChatGPT",+ access_token=SecretStr("access"),+ scopes=[],+ )+ gateway = gateway_for_lease(+ CodexCredentialLease(credentials, MagicMock()),+ model="gpt-5.6-terra",+ effort="high",+ )+ limit = RateLimitError(+ "usage limit",+ response=httpx.Response(+ 429, request=httpx.Request("POST", "https://chatgpt.test")+ ),+ body=None,+ )++ gateway._failure_response(_Failed(error=limit))++ assert gateway.last_failure is not None+ assert gateway.last_failure.credential_id == "cred-chatgpt-1"diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 629308b6c4..6b93a5caf2 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -62,7 +62,10 @@ from backend.copilot.provider_failure import ProviderFailurefrom backend.copilot.segments import Segment, stamp_segmentfrom backend.copilot.graphiti.ingest import enqueue_conversation_turnfrom backend.copilot.learning.capture import capture_chat_turn, find_initiating_user_row-from backend.copilot.sdk.codex_compat_gateway import CodexAnthropicGateway+from backend.copilot.sdk.codex_compat_gateway import (+ CodexAnthropicGateway,+ gateway_for_lease,+)from backend.copilot.sdk.cost_tracking import (TokenUsage as _TokenUsage,read_cli_session_usage,@@ -5252,18 +5255,9 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuestier_name,credential_lease,)- if isinstance(credential_lease, CodexCredentialLease):- codex_gateway = CodexAnthropicGateway(- agent_session=credential_lease,- model=sdk_model,- effort=codex_effort,- )- else:- codex_gateway = CodexAnthropicGateway(- credential_lease=credential_lease,- model=sdk_model,- effort=codex_effort,- )+ codex_gateway = gateway_for_lease(+ credential_lease, model=sdk_model, effort=codex_effort+ )await codex_gateway.start()fallback_model = Noneelse:diff --git a/autogpt_platform/backend/backend/integrations/codex/http_client.py b/autogpt_platform/backend/backend/integrations/codex/http_client.pyindex eca3256f04..19561863c0 100644--- a/autogpt_platform/backend/backend/integrations/codex/http_client.py+++ b/autogpt_platform/backend/backend/integrations/codex/http_client.py@@ -198,6 +198,37 @@ def parse_rate_limits(headers: Mapping[str, str]) -> CodexRateLimitsSnapshot:)+def usage_limit_resets_at(headers: Mapping[str, str], body: object) -> int | None:+ """When a spent ChatGPT quota lifts, as the 429 that reports it says.++ The error body names it outright (``resets_at``, epoch seconds). Failing+ that, the quota headers carry each window's reset, and a full window is+ what blocks the turn -- with both full, the later reset is the real wait.+ ``None`` when neither says: a guessed time is worse than none.+ """+ reported = _int(str(_error_object(body).get("resets_at", "")))+ if reported is not None and reported > 0:+ return reported+ limits = parse_rate_limits(headers)+ full = [+ window.resets_at+ for window in (limits.primary, limits.secondary)+ if window is not None+ and window.used_percent >= 100+ and window.resets_at is not None+ and window.resets_at > 0+ ]+ return max(full, default=None)+++def _error_object(body: object) -> dict[str, object]:+ """The error object, whether or not the OpenAI SDK already unwrapped it."""+ if not isinstance(body, dict):+ return {}+ inner = body.get("error")+ return inner if isinstance(inner, dict) else body++def _window(lookup: Mapping[str, str], scope: str) -> CodexRateLimitWindow | None:used = _int(lookup.get(f"x-codex-{scope}-used-percent"))window = _int(lookup.get(f"x-codex-{scope}-window-minutes"))@@ -216,7 +247,7 @@ def _int(value: str | None) -> int | None:return Nonetry:return int(float(value))- except ValueError:+ except (ValueError, OverflowError):return Nonediff --git a/autogpt_platform/backend/backend/integrations/codex/http_client_test.py b/autogpt_platform/backend/backend/integrations/codex/http_client_test.pyindex 8e5689d5fd..4a495ed6f0 100644--- a/autogpt_platform/backend/backend/integrations/codex/http_client_test.py+++ b/autogpt_platform/backend/backend/integrations/codex/http_client_test.py@@ -1,6 +1,7 @@import base64import json+import pytestfrom pydantic import SecretStrfrom backend.integrations.codex.auth_bundle import CodexAuthBundleV1, CodexAuthTokensV1@@ -10,6 +11,7 @@ from backend.integrations.codex.http_client import (account_id_for,account_snapshot,parse_rate_limits,+ usage_limit_resets_at,)@@ -196,3 +198,63 @@ def test_unparseable_numbers_do_not_raise() -> None:})assert limits.primary is None+++# --------------------------------------------------------------------------- #+# When a spent quota lifts+#+# A 429 from ChatGPT names its own reset; a guessed one is worse than none,+# because the user plans around it.+# --------------------------------------------------------------------------- #+++def _window(scope: str, used: str, resets_at: str) -> dict[str, str]:+ return {+ f"x-codex-{scope}-used-percent": used,+ f"x-codex-{scope}-window-minutes": "300",+ f"x-codex-{scope}-reset-at": resets_at,+ }+++def test_the_error_body_names_the_reset() -> None:+ # The OpenAI SDK hands over the inner ``error`` object; a raw body wraps it.+ inner = {"type": "usage_limit_reached", "resets_at": 1788452827}+ assert usage_limit_resets_at({}, inner) == 1788452827+ assert usage_limit_resets_at({}, {"error": inner}) == 1788452827+++def test_the_body_wins_over_the_quota_headers() -> None:+ headers = _window("primary", "100", "1788400000")+ assert usage_limit_resets_at(headers, {"resets_at": 1788452827}) == 1788452827+++def test_without_a_body_the_full_window_names_the_reset() -> None:+ headers = {+ **_window("primary", "100", "1788400000"),+ **_window("secondary", "40", "1789000000"),+ }+ assert usage_limit_resets_at(headers, None) == 1788400000+++def test_with_both_windows_full_the_later_reset_is_the_real_wait() -> None:+ headers = {+ **_window("primary", "100", "1788400000"),+ **_window("secondary", "100", "1789000000"),+ }+ assert usage_limit_resets_at(headers, "not json") == 1789000000+++def test_no_reset_is_invented_when_no_window_is_full() -> None:+ headers = _window("primary", "64", "1788400000")+ assert usage_limit_resets_at(headers, {"type": "usage_limit_reached"}) is None++[email protected]("raw", ["soon", float("inf"), float("nan")])+def test_an_unreadable_body_reset_falls_back_to_the_headers(raw: object) -> None:+ headers = _window("primary", "100", "1788400000")+ assert usage_limit_resets_at(headers, {"resets_at": raw}) == 1788400000++[email protected]("raw", ["-1", "0"])+def test_a_non_positive_header_reset_is_not_a_time(raw: str) -> None:+ assert usage_limit_resets_at(_window("primary", "100", raw), None) is Nonediff --git a/autogpt_platform/backend/backend/integrations/codex/http_session.py b/autogpt_platform/backend/backend/integrations/codex/http_session.pyindex ebd894271a..0319d56da6 100644--- a/autogpt_platform/backend/backend/integrations/codex/http_session.py+++ b/autogpt_platform/backend/backend/integrations/codex/http_session.py@@ -128,12 +128,15 @@ class CodexHttpSession:usage=usage,)- for call in turn.tool_calls:- conversation.append(await self._dispatch_tool(call, tool_handler))+ results = [+ await self._dispatch_tool(call, tool_handler)+ for call in turn.tool_calls+ ]+ conversation.extend(_tool_round_items(turn.tool_calls, results))async def _dispatch_tool(self, call: "_ToolCall", tool_handler: ToolHandler- ) -> dict[str, Any]:+ ) -> CodexDynamicToolResult:request = CodexDynamicToolCall(thread_id=call.response_id,turn_id=call.response_id,@@ -143,27 +146,24 @@ class CodexHttpSession:arguments=call.arguments,)try:- result = await asyncio.wait_for(+ return await asyncio.wait_for(tool_handler(request), timeout=self._tool_timeout_seconds)- output = result.contentexcept asyncio.TimeoutError:# Report the timeout to the model rather than failing the turn: it# can usually recover by trying something else.- output = f"Tool {call.name!r} timed out."logger.warning("Codex tool %s timed out", call.name)+ return CodexDynamicToolResult(+ content=f"Tool {call.name!r} timed out.", success=False+ )except Exception:# Tool failures are model-visible results, not transport failures:# the model can explain, retry, or choose another path. Do not# include exception text because tool errors may contain secrets.- output = f"Tool {call.name!r} failed."logger.exception("Codex tool %s failed", call.name)-- return {- "type": "function_call_output",- "call_id": call.call_id,- "output": output,- }+ return CodexDynamicToolResult(+ content=f"Tool {call.name!r} failed.", success=False+ )async def _stream_turn(self,@@ -306,6 +306,30 @@ def _user_message(prompt: str) -> dict[str, Any]:}+def _tool_round_items(+ calls: list[_ToolCall], results: list[CodexDynamicToolResult]+) -> list[dict[str, Any]]:+ """A round's tool outputs, then whatever the user said while it ran.++ A follow-up is the user speaking, not the tool, so it goes in as a user+ message rather than inside an output -- and only once every call in the+ round has its output, so the model's calls are answered before it hears+ anything new.+ """+ outputs = [+ {+ "type": "function_call_output",+ "call_id": call.call_id,+ "output": result.content,+ }+ for call, result in zip(calls, results, strict=True)+ ]+ followups = [+ _user_message(result.followup) for result in results if result.followup+ ]+ return outputs + followups++def _tool_payload(spec: CodexDynamicToolSpec) -> dict[str, Any]:return {"type": "function",diff --git a/autogpt_platform/backend/backend/integrations/codex/http_session_test.py b/autogpt_platform/backend/backend/integrations/codex/http_session_test.pyindex b8a4a603fc..8f3102cada 100644--- a/autogpt_platform/backend/backend/integrations/codex/http_session_test.py+++ b/autogpt_platform/backend/backend/integrations/codex/http_session_test.py@@ -210,6 +210,68 @@ async def test_a_tool_call_is_dispatched_and_its_output_fed_back() -> None:]+async def test_a_followup_reaches_the_model_as_a_user_message_after_the_output() -> (+ None+):+ """What the user said while the tool ran is the user talking, not the tool."""+ session, client = _session([_tool_turn("lookup", "{}"), _text_turn("done")])++ async def handler(_call: CodexDynamicToolCall) -> CodexDynamicToolResult:+ return CodexDynamicToolResult(content="tool output", followup="also do X")++ await session.invoke(_request(), [TOOL], handler)++ second_input = client.responses.requests[1]["input"]+ assert second_input[-2:] == [+ {"type": "function_call_output", "call_id": "call-1", "output": "tool output"},+ {+ "type": "message",+ "role": "user",+ "content": [{"type": "input_text", "text": "also do X"}],+ },+ ]+++async def test_a_followup_waits_until_every_call_in_the_round_has_its_output() -> None:+ first = _tool_turn("lookup", "{}", call_id="call-1")+ first.append(+ _Event(+ "response.output_item.done",+ item=_Item(+ type="function_call", name="lookup", arguments="{}", call_id="call-2"+ ),+ )+ )+ session, client = _session([first, _text_turn("done")])+ followups = iter(["first follow-up", None])++ async def handler(_call: CodexDynamicToolCall) -> CodexDynamicToolResult:+ return CodexDynamicToolResult(content="out", followup=next(followups))++ await session.invoke(_request(), [TOOL], handler)++ tail = client.responses.requests[1]["input"][-3:]+ assert [(item["type"], item.get("call_id")) for item in tail] == [+ ("function_call_output", "call-1"),+ ("function_call_output", "call-2"),+ ("message", None),+ ]+ assert tail[2]["content"] == [{"type": "input_text", "text": "first follow-up"}]+++async def test_a_round_without_a_followup_adds_no_message() -> None:+ session, client = _session([_tool_turn("lookup", "{}"), _text_turn("done")])++ await session.invoke(_request(), [TOOL], _echo_tool)++ second_input = client.responses.requests[1]["input"]+ assert second_input[-1]["type"] == "function_call_output"+ assert [item.get("role") for item in second_input].count("user") == 1++@pytest.mark.asyncioasync def test_every_output_item_is_replayed_so_reasoning_survives_a_tool_hop() -> None:"""Replaying only the tool calls would break the chain on reasoning models."""diff --git a/autogpt_platform/backend/backend/integrations/codex/models.py b/autogpt_platform/backend/backend/integrations/codex/models.pyindex 44e1e78ba0..876b02955c 100644--- a/autogpt_platform/backend/backend/integrations/codex/models.py+++ b/autogpt_platform/backend/backend/integrations/codex/models.py@@ -125,6 +125,10 @@ class CodexDynamicToolResult(BaseModel):content: strsuccess: bool = True+ # A message the user sent while the tool ran, which arrived with the+ # result without being part of it. The model reads it as a user turn after+ # the round's outputs, never inside this tool's output.+ followup: str | None = Noneclass CodexLoginCompletion(BaseModel):