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.

refactor(backend): run Codex over HTTP and drop the 391 MB bundled CLI (#14195)

Nicholas Tindle4b3df42c5065
diff --git a/.github/scripts/platform-single-container-smoke.sh b/.github/scripts/platform-single-container-smoke.sh
index 70ac881e2b..9bcdafcf3d 100644
--- a/.github/scripts/platform-single-container-smoke.sh
+++ b/.github/scripts/platform-single-container-smoke.sh
@@ -580,41 +580,28 @@ assert_codex_runtime_contract() {
--workdir /app/autogpt_platform/backend \
"${CONTAINER_NAME}" \
/app/autogpt_platform/backend/.venv/bin/python - <<'PY'
+import importlib.util
import os
-import subprocess
from pathlib import Path
-from backend.integrations.codex.runtime import (
- CODEX_RUNTIME_VERSION,
- assert_pinned_versions,
- build_runtime_config,
-)
-from backend.integrations.codex.temporary_home import TemporaryCodexHome
+from backend.integrations.codex.http_client import API_BASE
+from backend.integrations.codex.http_session import CodexHttpSession # noqa: F401
+from backend.integrations.oauth import DEVICE_HANDLERS_BY_NAME
root = Path(os.environ["CODEX_TEMP_ROOT"])
assert root == Path("/dev/shm/autogpt-codex")
-assert_pinned_versions()
-before = set(root.iterdir())
-with TemporaryCodexHome.create(root) as home:
- config = build_runtime_config(home)
- launch_args = config.launch_args_override
- assert launch_args is not None
- completed = subprocess.run(
- [*launch_args[:3], "--version"],
- env=config.env,
- check=False,
- capture_output=True,
- text=True,
- timeout=30,
- )
- output = completed.stdout + completed.stderr
- assert completed.returncode == 0, output
- assert CODEX_RUNTIME_VERSION in output, output
- home_path = home.path
-
-assert not home_path.exists()
-assert set(root.iterdir()) == before
-print("codex-sdk-runtime-ok")
+
+# Codex reaches ChatGPT over HTTPS now. The bundled CLI was ~391 MB and the
+# single biggest thing this image carried for it, so assert it is really
+# absent rather than merely unused -- a transitive dependency could quietly
+# drag it back in.
+for banned in ("openai_codex", "codex_cli_bin"):
+ assert importlib.util.find_spec(banned) is None, banned
+
+assert API_BASE.startswith("https://"), API_BASE
+assert "codex" in DEVICE_HANDLERS_BY_NAME, sorted(DEVICE_HANDLERS_BY_NAME)
+
+print("codex-http-runtime-ok")
PY
}
diff --git a/autogpt_platform/backend/backend/api/features/integrations/codex.py b/autogpt_platform/backend/backend/api/features/integrations/codex.py
index db70d84332..6c43461100 100644
--- a/autogpt_platform/backend/backend/api/features/integrations/codex.py
+++ b/autogpt_platform/backend/backend/api/features/integrations/codex.py
@@ -207,7 +207,7 @@ def _is_codex_lease(lease: CredentialLease) -> bool:
return (
credentials.type == "oauth2"
and credentials.provider == "codex"
- and credentials.refresh_strategy == "provider_runtime"
+ and credentials.refresh_strategy in ("oauth_handler", "provider_runtime")
)
diff --git a/autogpt_platform/backend/backend/api/features/integrations/codex_test.py b/autogpt_platform/backend/backend/api/features/integrations/codex_test.py
index 97b5857f39..12696b5ac1 100644
--- a/autogpt_platform/backend/backend/api/features/integrations/codex_test.py
+++ b/autogpt_platform/backend/backend/api/features/integrations/codex_test.py
@@ -10,6 +10,7 @@ from backend.api.features.integrations.codex import (
CODEX_LOGIN_STATE_KEY,
CodexDeviceLogin,
CodexDeviceLoginState,
+ _is_codex_lease,
build_device_login_cancel_url,
build_device_login_url,
render_device_login_page,
@@ -81,6 +82,14 @@ def _oauth_state(login_id: str = "login-123") -> OAuthState:
)
[email protected]("strategy", ["oauth_handler", "provider_runtime"])
+def test_codex_routes_accept_current_and_legacy_refresh_ownership(strategy: str):
+ lease = MagicMock()
+ lease.credentials = _credentials().model_copy(update={"refresh_strategy": strategy})
+
+ assert _is_codex_lease(lease)
+
+
def test_codex_login_reuses_generic_oauth_contract():
login = CodexDeviceLogin(
login_id="login-123",
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor.py b/autogpt_platform/backend/backend/copilot/executor/processor.py
index ecaf06b946..d025234c2b 100644
--- a/autogpt_platform/backend/backend/copilot/executor/processor.py
+++ b/autogpt_platform/backend/backend/copilot/executor/processor.py
@@ -551,12 +551,10 @@ class CoPilotProcessor:
)
except CodexCredentialBusyError:
# Re-raised as-is rather than flattened into a bare
- # RuntimeError: a ChatGPT connection serves one chat at a
- # time, so this is the single most likely thing a user
- # with two chats open will hit. Keeping the type is what
- # lets the failure say so, and say that waiting works --
- # ``RuntimeError("codex_credential_busy")`` reaches the
- # chat as an unexplained error with no advice attached.
+ # RuntimeError. HTTP turns run concurrently, but a brief
+ # rotating-token refresh or legacy-credential migration
+ # can still contend; preserving the type gives that
+ # retryable condition a useful message.
raise
except asyncio.CancelledError:
raise
diff --git a/autogpt_platform/backend/backend/copilot/model_router.py b/autogpt_platform/backend/backend/copilot/model_router.py
index 2308a0fc2d..d6ffeed1ff 100644
--- a/autogpt_platform/backend/backend/copilot/model_router.py
+++ b/autogpt_platform/backend/backend/copilot/model_router.py
@@ -53,7 +53,7 @@ from backend.copilot.model import RoutingSource
from backend.data.llm_registry.llm_models import LLMModel, transport_slug_candidates
from backend.integrations.codex.models import CodexModelInfo, CodexReasoningEffort
from backend.integrations.codex.transport import (
- PooledCodexRuntimeLease,
+ CodexCredentialLease,
get_codex_transport,
)
from backend.integrations.credential_lease import CredentialLease
@@ -330,7 +330,7 @@ async def resolve_model_route(
async def resolve_codex_model_route(
mode: ModelMode,
tier: ModelTier,
- credential_lease: CredentialLease | PooledCodexRuntimeLease,
+ credential_lease: CredentialLease | CodexCredentialLease,
) -> ResolvedCodexModel:
"""Resolve a Codex model against both the catalog and the account."""
advertised = await _advertised_codex_models(credential_lease)
@@ -348,9 +348,9 @@ async def resolve_codex_model_route(
async def _advertised_codex_models(
- credential_lease: CredentialLease | PooledCodexRuntimeLease,
+ credential_lease: CredentialLease | CodexCredentialLease,
) -> list[CodexModelInfo]:
- if isinstance(credential_lease, PooledCodexRuntimeLease):
+ if isinstance(credential_lease, CodexCredentialLease):
return await credential_lease.models()
return await get_codex_transport().models(credential_lease)
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.py
index a7f87224ca..4cb3bf7980 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
@@ -11,8 +11,6 @@ from claude_agent_sdk import (
create_sdk_mcp_server,
tool,
)
-from openai_codex.generated.v2_all import AgentMessageDeltaNotification
-from openai_codex.models import Notification
from backend.copilot.sdk.codex_compat_gateway import CodexAnthropicGateway
from backend.copilot.sdk.env import build_sdk_env
@@ -20,6 +18,7 @@ from backend.integrations.codex.models import (
CodexDynamicToolCall,
CodexDynamicToolResult,
CodexInvocationResult,
+ CodexStreamEvent,
CodexTokenUsage,
)
from backend.integrations.credential_lease import CredentialLease
@@ -51,14 +50,8 @@ class _ClaudeHarnessAgentSession:
)
assert event_handler is not None
await event_handler(
- Notification(
- method="item/agentMessage/delta",
- payload=AgentMessageDeltaNotification(
- delta="Claude harness round trip complete",
- itemId="item-cli",
- threadId="thread-cli",
- turnId="turn-cli",
- ),
+ CodexStreamEvent(
+ type="text_delta", delta="Claude harness round trip complete"
)
)
return CodexInvocationResult(
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 83144bc87c..1eefb03212 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py
@@ -12,8 +12,6 @@ from typing import Protocol, cast
from uuid import uuid4
from aiohttp import web
-from openai_codex.generated.v2_all import AgentMessageDeltaNotification
-from openai_codex.models import Notification
from pydantic import BaseModel, ConfigDict, Field
from backend.copilot.provider_failure import ProviderFailure
@@ -26,6 +24,7 @@ from backend.integrations.codex.models import (
CodexInvocationRequest,
CodexInvocationResult,
CodexReasoningEffort,
+ CodexStreamEvent,
CodexTokenUsage,
)
from backend.integrations.codex.transport import (
@@ -73,7 +72,7 @@ class _AgentSession(Protocol):
tool_handler: Callable[
[CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]
],
- event_handler: Callable[[Notification], Awaitable[None]] | None = None,
+ event_handler: Callable[[CodexStreamEvent], Awaitable[None]] | None = None,
) -> CodexInvocationResult: ...
@@ -435,10 +434,9 @@ class CodexAnthropicGateway:
tools: list[CodexDynamicToolSpec],
original_names: dict[str, str],
) -> None:
- async def handle_event(notification: Notification) -> None:
- payload = notification.payload
- if isinstance(payload, AgentMessageDeltaNotification) and payload.delta:
- await conversation.queue.put(_TextDelta(text=payload.delta))
+ async def handle_event(event: CodexStreamEvent) -> None:
+ if event.type == "text_delta" and event.delta:
+ await conversation.queue.put(_TextDelta(text=event.delta))
async def handle_tool(call: CodexDynamicToolCall) -> CodexDynamicToolResult:
future: asyncio.Future[CodexDynamicToolResult] = (
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 f0e1c81998..6bffdb31f0 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
@@ -7,8 +7,6 @@ from typing import cast
import pytest
from aiohttp import ClientSession
-from openai_codex.generated.v2_all import AgentMessageDeltaNotification
-from openai_codex.models import Notification
from backend.copilot.sdk.codex_compat_gateway import (
CodexAnthropicGateway,
@@ -20,6 +18,7 @@ from backend.integrations.codex.models import (
CodexDynamicToolSpec,
CodexInvocationRequest,
CodexInvocationResult,
+ CodexStreamEvent,
CodexTokenUsage,
)
from backend.integrations.credential_lease import CredentialLease
@@ -55,14 +54,8 @@ class _FakeAgentSession:
)
if event_handler is not None:
await event_handler(
- Notification(
- method="item/agentMessage/delta",
- payload=AgentMessageDeltaNotification(
- delta="done" if self.use_tool else "hello",
- itemId="item-1",
- threadId="thread-1",
- turnId="turn-1",
- ),
+ CodexStreamEvent(
+ type="text_delta", delta="done" if self.use_tool else "hello"
)
)
return _result("done" if self.use_tool else "hello")
@@ -95,17 +88,7 @@ class _FinalWithoutDeltaSession:
event_handler=None,
) -> CodexInvocationResult:
assert event_handler is not None
- await event_handler(
- Notification(
- method="item/agentMessage/delta",
- payload=AgentMessageDeltaNotification(
- delta="before tool",
- itemId="item-before",
- threadId="thread-before",
- turnId="turn-before",
- ),
- )
- )
+ await event_handler(CodexStreamEvent(type="text_delta", delta="before tool"))
await tool_handler(
CodexDynamicToolCall(
thread_id="thread-before",
@@ -144,15 +127,7 @@ class _CollidingCallSession:
self.results[invocation_index] = result
assert event_handler is not None
await event_handler(
- Notification(
- method="item/agentMessage/delta",
- payload=AgentMessageDeltaNotification(
- delta=f"completed:{result.content}",
- itemId=f"item-{invocation_index}",
- threadId=f"thread-{invocation_index}",
- turnId=f"turn-{invocation_index}",
- ),
- )
+ CodexStreamEvent(type="text_delta", delta=f"completed:{result.content}")
)
return _result(f"completed:{result.content}")
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 7dc540a3aa..f12ff049f3 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -57,7 +57,7 @@ from backend.data.db_accessors import chat_db
from backend.data.redis_client import get_redis_async
from backend.executor.cluster_lock import AsyncClusterLock
from backend.integrations.codex.models import CodexReasoningEffort, CodexTokenUsage
-from backend.integrations.codex.transport import PooledCodexRuntimeLease
+from backend.integrations.codex.transport import CodexCredentialLease
from backend.integrations.credential_lease import CredentialLease
from backend.util.exceptions import NotFoundError
from backend.util.feature_flag import Flag, is_feature_enabled
@@ -4415,7 +4415,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
request_arrival_at: float = 0.0,
organization_id: str | None = None,
team_id: str | None = None,
- credential_lease: CredentialLease | PooledCodexRuntimeLease | None = None,
+ credential_lease: CredentialLease | CodexCredentialLease | None = None,
**_kwargs: Any,
) -> AsyncGenerator[StreamBaseResponse, None]:
# Pyright's complexity heuristic bails on this ~1500 LoC function (retry
@@ -4836,7 +4836,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
tier_name,
credential_lease,
)
- if isinstance(credential_lease, PooledCodexRuntimeLease):
+ if isinstance(credential_lease, CodexCredentialLease):
codex_gateway = CodexAnthropicGateway(
agent_session=credential_lease,
model=sdk_model,
@@ -6362,7 +6362,7 @@ def _same_model(a: str, b: str | None) -> bool:
def _sdk_serving_segment(
- credential_lease: CredentialLease | PooledCodexRuntimeLease | None,
+ credential_lease: CredentialLease | CodexCredentialLease | None,
) -> Segment:
"""The immutable route that actually serves this SDK turn."""
if credential_lease is None:
diff --git a/autogpt_platform/backend/backend/integrations/codex/auth_bundle_test.py b/autogpt_platform/backend/backend/integrations/codex/auth_bundle_test.py
index a764a3e54c..a2e3e1cc4a 100644
--- a/autogpt_platform/backend/backend/integrations/codex/auth_bundle_test.py
+++ b/autogpt_platform/backend/backend/integrations/codex/auth_bundle_test.py
@@ -63,13 +63,30 @@ def test_bundle_round_trips_through_oauth_credentials(tmp_path: Path):
credentials = credentials_from_bundle(bundle)
assert credentials.provider == "codex"
- assert credentials.refresh_strategy == "provider_runtime"
+ # Sign-in and refresh are plain HTTP now, so the credentials manager owns
+ # the token lifecycle instead of a Codex process.
+ assert credentials.refresh_strategy == "oauth_handler"
assert credentials.username == "[email protected]"
assert credentials.access_token == SecretStr(tokens[1])
assert bundle_from_credentials(credentials) == bundle
assert all(token not in repr(credentials) for token in tokens)
+def test_credentials_stored_before_the_http_switch_are_still_readable(
+ tmp_path: Path,
+) -> None:
+ """Grants written when the CLI owned refresh must not break on upgrade."""
+ auth_path = tmp_path / "auth.json"
+ _write_auth(auth_path, "alpha")
+ bundle = read_auth_bundle(auth_path, "0.144.4")
+
+ legacy = credentials_from_bundle(bundle).model_copy(
+ update={"refresh_strategy": "provider_runtime"}
+ )
+
+ assert bundle_from_credentials(legacy) == bundle
+
+
def test_provider_state_round_trips_through_existing_encryption(tmp_path: Path):
auth_path = tmp_path / "auth.json"
tokens = _write_auth(auth_path, "alpha")
diff --git a/autogpt_platform/backend/backend/integrations/codex/chatgpt_auth.py b/autogpt_platform/backend/backend/integrations/codex/chatgpt_auth.py
new file mode 100644
index 0000000000..37c21f2fe1
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/codex/chatgpt_auth.py
@@ -0,0 +1,276 @@
+"""ChatGPT device-code authentication over plain HTTP.
+
+Replaces driving the Codex CLI for sign-in. Every endpoint and response shape
+below was verified against ``auth.openai.com`` rather than inferred from
+another client's source.
+
+This flow is *not* RFC 8628 despite resembling it, which is why the generic
+device-auth helpers do not fit:
+
+- the poll is keyed by ``(device_auth_id, user_code)``, not one ``device_code``;
+- ``interval`` arrives as a **string**, and the deadline as an ISO timestamp in
+ ``expires_at`` rather than an ``expires_in`` count of seconds;
+- there is no ``verification_uri`` in the payload -- it is a fixed URL;
+- a successful poll returns an ``authorization_code`` that still has to be
+ exchanged at the ordinary OAuth token endpoint.
+
+The client id is OpenAI's public Codex client. It is an identifier, not a
+secret: no ``client_secret`` is ever sent, and PKCE is what protects the
+exchange. ``originator`` identifies us honestly as AutoGPT rather than
+impersonating the Codex CLI, which the backend also accepts.
+"""
+
+import logging
+from datetime import datetime, timezone
+from typing import Literal
+
+from pydantic import BaseModel, ConfigDict, SecretStr, field_validator
+
+from backend.integrations.codex.auth_bundle import (
+ CodexAuthBundleV1,
+ CodexAuthTokensV1,
+ decode_jwt_claims,
+)
+from backend.util.request import Requests
+
+logger = logging.getLogger(__name__)
+
+CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann"
+AUTH_BASE = "https://auth.openai.com"
+DEVICE_CODE_URL = f"{AUTH_BASE}/api/accounts/deviceauth/usercode"
+DEVICE_TOKEN_URL = f"{AUTH_BASE}/api/accounts/deviceauth/token"
+OAUTH_TOKEN_URL = f"{AUTH_BASE}/oauth/token"
+DEVICE_VERIFY_URL = f"{AUTH_BASE}/codex/device"
+DEVICE_REDIRECT_URI = f"{AUTH_BASE}/deviceauth/callback"
+
+# Identifies AutoGPT to OpenAI. Deliberately not "codex_cli_rs": the backend
+# serves any originator, so sending theirs would be impersonation for no gain.
+ORIGINATOR = "autogpt"
+USER_AGENT = "autogpt/1.0 (+https://agpt.co)"
+
+# Recorded on the bundle in place of a CLI version now that no binary is involved.
+RUNTIME_VERSION = "http"
+
+_DEFAULT_POLL_INTERVAL = 5
+_MAX_POLL_INTERVAL = 60
+_AUTH_RETRY_ATTEMPTS = 3
+
+# Classify on the machine-readable ``code``, never the HTTP status: a pending
+# authorization answers 403 and an unknown or expired one answers 404, so
+# switching on status alone would either poll a dead login until it times out
+# or abort a live one the user is still approving.
+_PENDING_CODES = frozenset({"deviceauth_authorization_pending"})
+_SLOW_DOWN_CODES = frozenset({"deviceauth_slow_down", "slow_down"})
+_DENIED_CODES = frozenset({"deviceauth_access_denied", "access_denied"})
+_EXPIRED_CODES = frozenset(
+ {"deviceauth_not_found", "deviceauth_expired_token", "expired_token"}
+)
+
+
+class CodexAuthError(RuntimeError):
+ """Sign-in failed in a way that repeating the same poll will not fix."""
+
+
+class ChatGPTDeviceCode(BaseModel):
+ model_config = ConfigDict(extra="ignore")
+
+ device_auth_id: str
+ user_code: str
+ interval: int = _DEFAULT_POLL_INTERVAL
+ expires_at: datetime | None = None
+
+ @field_validator("interval", mode="before")
+ @classmethod
+ def _coerce_interval(cls, value: object) -> int:
+ """Arrives as the string "5"; a junk value must not become a busy loop."""
+ try:
+ seconds = int(float(str(value)))
+ except (TypeError, ValueError):
+ return _DEFAULT_POLL_INTERVAL
+ if seconds <= 0:
+ return _DEFAULT_POLL_INTERVAL
+ return min(seconds, _MAX_POLL_INTERVAL)
+
+ @property
+ def verification_url(self) -> str:
+ return DEVICE_VERIFY_URL
+
+ def seconds_remaining(self, *, now: datetime | None = None) -> float | None:
+ if self.expires_at is None:
+ return None
+ return (self.expires_at - (now or datetime.now(timezone.utc))).total_seconds()
+
+
+class ChatGPTPollResult(BaseModel):
+ model_config = ConfigDict(extra="forbid")
+
+ status: Literal["pending", "slow_down", "approved", "denied", "expired"]
+ authorization_code: str | None = None
+ code_verifier: str | None = None
+
+
+class ChatGPTTokens(BaseModel):
+ model_config = ConfigDict(extra="ignore")
+
+ id_token: SecretStr
+ access_token: SecretStr
+ refresh_token: SecretStr
+
+
+def _headers(*, content_type: str = "application/json") -> dict[str, str]:
+ return {
+ "content-type": content_type,
+ "accept": "application/json",
+ "originator": ORIGINATOR,
+ "user-agent": USER_AGENT,
+ }
+
+
+def _error_code(payload: object) -> str | None:
+ if not isinstance(payload, dict):
+ return None
+ error = payload.get("error")
+ if isinstance(error, dict):
+ code = error.get("code") or error.get("type")
+ return str(code) if code else None
+ if isinstance(error, str):
+ return error
+ return None
+
+
+async def request_device_code() -> ChatGPTDeviceCode:
+ """Start a sign-in and return the code the user enters at the verify URL."""
+ response = await Requests(retry_max_attempts=_AUTH_RETRY_ATTEMPTS).post(
+ DEVICE_CODE_URL, headers=_headers(), json={"client_id": CLIENT_ID}
+ )
+ return ChatGPTDeviceCode.model_validate(response.json())
+
+
+async def poll_device_code(device_auth_id: str, user_code: str) -> ChatGPTPollResult:
+ """Poll once. The caller owns the loop, the sleeping, and the deadline."""
+ # This function is already one iteration of the caller-owned polling
+ # loop. Return a provider 429/5xx promptly so that loop can apply the
+ # advertised interval instead of nesting another retry loop inside it.
+ response = await Requests(raise_for_status=False, retry_max_attempts=1).post(
+ DEVICE_TOKEN_URL,
+ headers=_headers(),
+ json={
+ "client_id": CLIENT_ID,
+ "device_auth_id": device_auth_id,
+ "user_code": user_code,
+ },
+ )
+
+ try:
+ payload = response.json()
+ except ValueError:
+ payload = None
+
+ if response.status == 200 and isinstance(payload, dict):
+ authorization_code = payload.get("authorization_code")
+ code_verifier = payload.get("code_verifier")
+ if not authorization_code or not code_verifier:
+ raise CodexAuthError(
+ "ChatGPT approved the device but returned no authorization code"
+ )
+ return ChatGPTPollResult(
+ status="approved",
+ authorization_code=str(authorization_code),
+ code_verifier=str(code_verifier),
+ )
+
+ code = _error_code(payload)
+ if code in _PENDING_CODES:
+ return ChatGPTPollResult(status="pending")
+ if code in _SLOW_DOWN_CODES:
+ return ChatGPTPollResult(status="slow_down")
+ if code in _DENIED_CODES:
+ return ChatGPTPollResult(status="denied")
+ if code in _EXPIRED_CODES:
+ return ChatGPTPollResult(status="expired")
+
+ # An unrecognised 429 is still worth backing off on rather than declaring
+ # the login dead.
+ if response.status == 429:
+ return ChatGPTPollResult(status="slow_down")
+
+ raise CodexAuthError(
+ f"ChatGPT device authorization failed (HTTP {response.status}, code={code})"
+ )
+
+
+async def exchange_authorization_code(
+ authorization_code: str, code_verifier: str
+) -> ChatGPTTokens:
+ response = await Requests(
+ raise_for_status=False, retry_max_attempts=_AUTH_RETRY_ATTEMPTS
+ ).post(
+ OAUTH_TOKEN_URL,
+ headers=_headers(content_type="application/x-www-form-urlencoded"),
+ data={
+ "grant_type": "authorization_code",
+ "client_id": CLIENT_ID,
+ "code": authorization_code,
+ "code_verifier": code_verifier,
+ "redirect_uri": DEVICE_REDIRECT_URI,
+ },
+ )
+ if response.status != 200:
+ try:
+ payload = response.json()
+ except ValueError:
+ payload = None
+ raise CodexAuthError(
+ "ChatGPT token exchange failed "
+ f"(HTTP {response.status}, code={_error_code(payload)})"
+ )
+ return ChatGPTTokens.model_validate(response.json())
+
+
+async def refresh_access_token(
+ refresh_token: SecretStr,
+ *,
+ current_id_token: SecretStr | None = None,
+) -> ChatGPTTokens:
+ """Exchange a refresh token. OpenAI rotates it, so persist what comes back."""
+ response = await Requests(
+ raise_for_status=False, retry_max_attempts=_AUTH_RETRY_ATTEMPTS
+ ).post(
+ OAUTH_TOKEN_URL,
+ headers=_headers(content_type="application/x-www-form-urlencoded"),
+ data={
+ "grant_type": "refresh_token",
+ "client_id": CLIENT_ID,
+ "refresh_token": refresh_token.get_secret_value(),
+ },
+ )
+ if response.status != 200:
+ raise CodexAuthError(
+ f"ChatGPT refused to refresh the credential (HTTP {response.status})"
+ )
+ payload = response.json()
+ # A refresh that omits a new refresh token means the existing one stands.
+ if not payload.get("refresh_token"):
+ payload["refresh_token"] = refresh_token.get_secret_value()
+ # OIDC refresh responses are allowed to omit a new ID token. Keep the
+ # current one in that case; it carries the ChatGPT account identity needed
+ # to rebuild the persisted auth bundle.
+ if not payload.get("id_token"):
+ if current_id_token is None:
+ raise CodexAuthError("ChatGPT refresh response omitted the ID token")
+ payload["id_token"] = current_id_token.get_secret_value()
+ return ChatGPTTokens.model_validate(payload)
+
+
+def bundle_from_tokens(tokens: ChatGPTTokens) -> CodexAuthBundleV1:
+ account_id = decode_jwt_claims(tokens.id_token).chatgpt_account_id
+ return CodexAuthBundleV1(
+ tokens=CodexAuthTokensV1(
+ id_token=tokens.id_token,
+ access_token=tokens.access_token,
+ refresh_token=tokens.refresh_token,
+ account_id=account_id,
+ ),
+ last_refresh=datetime.now(timezone.utc),
+ codex_runtime_version=RUNTIME_VERSION,
+ )
diff --git a/autogpt_platform/backend/backend/integrations/codex/chatgpt_auth_test.py b/autogpt_platform/backend/backend/integrations/codex/chatgpt_auth_test.py
new file mode 100644
index 0000000000..3f3d0ce4e7
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/codex/chatgpt_auth_test.py
@@ -0,0 +1,301 @@
+from datetime import datetime, timedelta, timezone
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+from pydantic import SecretStr
+
+from backend.integrations.codex import chatgpt_auth
+from backend.integrations.codex.chatgpt_auth import (
+ ChatGPTDeviceCode,
+ CodexAuthError,
+ exchange_authorization_code,
+ poll_device_code,
+ refresh_access_token,
+ request_device_code,
+)
+
+
+class _FakeResponse:
+ def __init__(self, status: int, payload: object) -> None:
+ self.status = status
+ self._payload = payload
+
+ def json(self) -> object:
+ if self._payload is None:
+ raise ValueError("not json")
+ return self._payload
+
+
+def _patch_post(response: _FakeResponse):
+ requests = AsyncMock()
+ requests.post = AsyncMock(return_value=response)
+ return patch.object(chatgpt_auth, "Requests", return_value=requests)
+
+
+def _error(code: str) -> dict[str, object]:
+ return {"error": {"message": "…", "type": "invalid_request_error", "code": code}}
+
+
+# --------------------------------------------------------------------------- #
+# Interval coercion
+#
+# ChatGPT sends `interval` as a *string*. A naive int() would raise, and a
+# `or default` fallback would turn 0 into a busy loop against the auth server.
+# --------------------------------------------------------------------------- #
+
+
+ "raw,expected",
+ [
+ ("5", 5),
+ (5, 5),
+ ("2.7", 2),
+ ("0", 5),
+ (-3, 5),
+ ("", 5),
+ (None, 5),
+ ("banana", 5),
+ (99999, 60),
+ ],
+)
+def test_interval_is_coerced_to_a_sane_number_of_seconds(raw, expected) -> None:
+ device = ChatGPTDeviceCode.model_validate(
+ {"device_auth_id": "deviceauth_x", "user_code": "AAAA-BBBBB", "interval": raw}
+ )
+ assert device.interval == expected
+
+
+def test_verification_url_is_the_fixed_codex_device_page() -> None:
+ device = ChatGPTDeviceCode(device_auth_id="deviceauth_x", user_code="AAAA-BBBBB")
+ assert device.verification_url == "https://auth.openai.com/codex/device"
+
+
+def test_seconds_remaining_uses_the_absolute_expiry() -> None:
+ now = datetime(2026, 1, 1, tzinfo=timezone.utc)
+ device = ChatGPTDeviceCode(
+ device_auth_id="deviceauth_x",
+ user_code="AAAA-BBBBB",
+ expires_at=now + timedelta(seconds=600),
+ )
+ assert device.seconds_remaining(now=now) == 600
+
+
+# --------------------------------------------------------------------------- #
+# Poll classification
+#
+# The load-bearing case: pending answers 403 and an unknown/expired login
+# answers 404. Switching on HTTP status instead of the error code would either
+# poll a dead login until the deadline or abort one the user is mid-approval on.
+# --------------------------------------------------------------------------- #
+
+
+async def test_403_authorization_pending_is_pending_not_a_failure() -> None:
+ with _patch_post(_FakeResponse(403, _error("deviceauth_authorization_pending"))):
+ result = await poll_device_code("deviceauth_x", "AAAA-BBBBB")
+ assert result.status == "pending"
+
+
+async def test_404_not_found_is_expired_rather_than_pending() -> None:
+ with _patch_post(_FakeResponse(404, _error("deviceauth_not_found"))):
+ result = await poll_device_code("deviceauth_x", "AAAA-BBBBB")
+ assert result.status == "expired"
+
+
+async def test_approved_poll_returns_the_code_and_verifier() -> None:
+ payload = {"authorization_code": "ac_123", "code_verifier": "cv_456"}
+ with _patch_post(_FakeResponse(200, payload)):
+ result = await poll_device_code("deviceauth_x", "AAAA-BBBBB")
+ assert result.status == "approved"
+ assert result.authorization_code == "ac_123"
+ assert result.code_verifier == "cv_456"
+
+
+async def test_approval_without_a_code_raises_instead_of_looking_approved() -> None:
+ with _patch_post(_FakeResponse(200, {"authorization_code": "ac_123"})):
+ with pytest.raises(CodexAuthError):
+ await poll_device_code("deviceauth_x", "AAAA-BBBBB")
+
+
+async def test_unrecognised_429_backs_off_rather_than_killing_the_login() -> None:
+ with _patch_post(_FakeResponse(429, {"error": {"code": "something_new"}})):
+ result = await poll_device_code("deviceauth_x", "AAAA-BBBBB")
+ assert result.status == "slow_down"
+
+
+async def test_denied_is_terminal() -> None:
+ with _patch_post(_FakeResponse(403, _error("access_denied"))):
+ result = await poll_device_code("deviceauth_x", "AAAA-BBBBB")
+ assert result.status == "denied"
+
+
+async def test_an_unknown_error_raises_rather_than_polling_forever() -> None:
+ with _patch_post(_FakeResponse(500, _error("internal_error"))):
+ with pytest.raises(CodexAuthError):
+ await poll_device_code("deviceauth_x", "AAAA-BBBBB")
+
+
+async def test_a_non_json_body_is_a_failure_not_a_pending_state() -> None:
+ with _patch_post(_FakeResponse(502, None)):
+ with pytest.raises(CodexAuthError):
+ await poll_device_code("deviceauth_x", "AAAA-BBBBB")
+
+
+async def test_every_auth_http_call_has_a_finite_retry_budget() -> None:
+ cases = [
+ (
+ request_device_code,
+ (),
+ _FakeResponse(
+ 200,
+ {"device_auth_id": "deviceauth_x", "user_code": "AAAA-BBBBB"},
+ ),
+ {"retry_max_attempts": 3},
+ ),
+ (
+ poll_device_code,
+ ("deviceauth_x", "AAAA-BBBBB"),
+ _FakeResponse(403, _error("deviceauth_authorization_pending")),
+ {"raise_for_status": False, "retry_max_attempts": 1},
+ ),
+ (
+ exchange_authorization_code,
+ ("auth-code", "verifier"),
+ _FakeResponse(
+ 200,
+ {"id_token": "id", "access_token": "at", "refresh_token": "rt"},
+ ),
+ {"raise_for_status": False, "retry_max_attempts": 3},
+ ),
+ (
+ refresh_access_token,
+ (SecretStr("old-refresh"),),
+ _FakeResponse(
+ 200,
+ {"id_token": "id", "access_token": "at", "refresh_token": "rt"},
+ ),
+ {"raise_for_status": False, "retry_max_attempts": 3},
+ ),
+ ]
+
+ for operation, args, response, expected_kwargs in cases:
+ requests = AsyncMock()
+ requests.post = AsyncMock(return_value=response)
+ factory = MagicMock(return_value=requests)
+ with patch.object(chatgpt_auth, "Requests", factory):
+ await operation(*args)
+ factory.assert_called_once_with(**expected_kwargs)
+
+
+# --------------------------------------------------------------------------- #
+# Token exchange
+# --------------------------------------------------------------------------- #
+
+
+async def test_authorization_code_exchange_uses_oauth_form_encoding() -> None:
+ response = _FakeResponse(
+ 200,
+ {"id_token": "id", "access_token": "at", "refresh_token": "rt"},
+ )
+ requests = AsyncMock()
+ requests.post = AsyncMock(return_value=response)
+
+ with patch.object(chatgpt_auth, "Requests", return_value=requests):
+ await exchange_authorization_code("auth-code", "pkce-verifier")
+
+ _, kwargs = requests.post.await_args
+ assert "json" not in kwargs
+ assert kwargs["headers"]["content-type"] == "application/x-www-form-urlencoded"
+ assert kwargs["data"] == {
+ "grant_type": "authorization_code",
+ "client_id": chatgpt_auth.CLIENT_ID,
+ "code": "auth-code",
+ "code_verifier": "pkce-verifier",
+ "redirect_uri": chatgpt_auth.DEVICE_REDIRECT_URI,
+ }
+
+
+async def test_authorization_code_exchange_reports_a_safe_provider_error() -> None:
+ with _patch_post(_FakeResponse(400, _error("invalid_grant"))):
+ with pytest.raises(CodexAuthError, match="HTTP 400, code=invalid_grant"):
+ await exchange_authorization_code("auth-code", "pkce-verifier")
+
+
+# --------------------------------------------------------------------------- #
+# Refresh
+# --------------------------------------------------------------------------- #
+
+
+async def test_refresh_keeps_the_old_token_when_none_is_returned() -> None:
+ """Dropping it would leave the credential unable to refresh ever again."""
+ payload = {"id_token": "id", "access_token": "at"}
+ with _patch_post(_FakeResponse(200, payload)):
+ tokens = await refresh_access_token(SecretStr("old-refresh"))
+ assert tokens.refresh_token.get_secret_value() == "old-refresh"
+
+
+async def test_refresh_keeps_the_old_id_token_when_none_is_returned() -> None:
+ payload = {"access_token": "at", "refresh_token": "new-refresh"}
+ with _patch_post(_FakeResponse(200, payload)):
+ tokens = await refresh_access_token(
+ SecretStr("old-refresh"),
+ current_id_token=SecretStr("old-id"),
+ )
+ assert tokens.id_token.get_secret_value() == "old-id"
+
+
+async def test_refresh_reports_a_safe_error_without_any_id_token() -> None:
+ payload = {"access_token": "at", "refresh_token": "new-refresh"}
+ with _patch_post(_FakeResponse(200, payload)):
+ with pytest.raises(CodexAuthError, match="omitted the ID token"):
+ await refresh_access_token(SecretStr("old-refresh"))
+
+
+async def test_refresh_prefers_a_rotated_token_when_one_arrives() -> None:
+ payload = {"id_token": "id", "access_token": "at", "refresh_token": "new-refresh"}
+ with _patch_post(_FakeResponse(200, payload)):
+ tokens = await refresh_access_token(SecretStr("old-refresh"))
+ assert tokens.refresh_token.get_secret_value() == "new-refresh"
+
+
+async def test_refresh_uses_oauth_form_encoding() -> None:
+ response = _FakeResponse(
+ 200,
+ {"id_token": "id", "access_token": "at", "refresh_token": "new-refresh"},
+ )
+ requests = AsyncMock()
+ requests.post = AsyncMock(return_value=response)
+
+ with patch.object(chatgpt_auth, "Requests", return_value=requests):
+ await refresh_access_token(SecretStr("old-refresh"))
+
+ _, kwargs = requests.post.await_args
+ assert "json" not in kwargs
+ assert kwargs["headers"]["content-type"] == "application/x-www-form-urlencoded"
+ assert kwargs["data"] == {
+ "grant_type": "refresh_token",
+ "client_id": chatgpt_auth.CLIENT_ID,
+ "refresh_token": "old-refresh",
+ }
+
+
+async def test_refresh_failure_raises() -> None:
+ with _patch_post(_FakeResponse(400, _error("invalid_grant"))):
+ with pytest.raises(CodexAuthError):
+ await refresh_access_token(SecretStr("old-refresh"))
diff --git a/autogpt_platform/backend/backend/integrations/codex/credential_codec.py b/autogpt_platform/backend/backend/integrations/codex/credential_codec.py
index 361f7f2f23..e53264638c 100644
--- a/autogpt_platform/backend/backend/integrations/codex/credential_codec.py
+++ b/autogpt_platform/backend/backend/integrations/codex/credential_codec.py
@@ -28,7 +28,7 @@ def credentials_from_bundle(
access_token_expires_at=access_claims.expires_at,
refresh_token=bundle.tokens.refresh_token,
scopes=[],
- refresh_strategy="provider_runtime",
+ refresh_strategy="oauth_handler",
provider_state=SecretStr(encode_provider_state(bundle)),
provider_state_version=bundle.schema_version,
metadata={
@@ -41,7 +41,11 @@ def credentials_from_bundle(
def bundle_from_credentials(credentials: OAuth2Credentials) -> CodexAuthBundleV1:
if credentials.provider != "codex":
raise CodexAuthBundleError("Credential provider is not Codex")
- if credentials.refresh_strategy != "provider_runtime":
+ # "provider_runtime" is the legacy shape from when the Codex CLI owned the
+ # token lifecycle. Sign-in and refresh are plain HTTP now, so new credentials
+ # are written as "oauth_handler"; both are readable so existing grants keep
+ # working across the change.
+ if credentials.refresh_strategy not in ("oauth_handler", "provider_runtime"):
raise CodexAuthBundleError("Codex credential refresh ownership is invalid")
if credentials.provider_state_version != 1 or credentials.provider_state is None:
raise CodexAuthBundleError("Codex credential provider state is unavailable")
diff --git a/autogpt_platform/backend/backend/integrations/codex/device_login.py b/autogpt_platform/backend/backend/integrations/codex/device_login.py
new file mode 100644
index 0000000000..0e2602187f
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/codex/device_login.py
@@ -0,0 +1,144 @@
+"""A ChatGPT device-code sign-in, driven over HTTP.
+
+Satisfies the session contract ``CodexLoginCoordinator`` already expects, so
+the coordinator's Redis state, single-active-login rule and cancellation all
+keep working -- only the mechanism underneath changes from a Codex subprocess
+to ``auth.openai.com``.
+"""
+
+import asyncio
+import logging
+from datetime import datetime, timezone
+from typing import Awaitable, TypeVar
+
+from backend.integrations.codex.chatgpt_auth import (
+ ChatGPTDeviceCode,
+ CodexAuthError,
+ bundle_from_tokens,
+ exchange_authorization_code,
+ poll_device_code,
+ request_device_code,
+)
+from backend.integrations.codex.credential_codec import credentials_from_bundle
+from backend.integrations.codex.http_client import account_snapshot
+from backend.integrations.codex.models import (
+ CodexDeviceCodeDetails,
+ CodexLoginCompletion,
+)
+
+logger = logging.getLogger(__name__)
+
+_MAX_INTERVAL_SECONDS = 60
+_T = TypeVar("_T")
+
+
+class CodexHttpDeviceLogin:
+ """One pending sign-in. ``wait()`` resolves when the user approves it."""
+
+ def __init__(
+ self,
+ device: ChatGPTDeviceCode,
+ *,
+ timeout_seconds: float,
+ sleep=asyncio.sleep,
+ now=lambda: datetime.now(timezone.utc),
+ ) -> None:
+ self._device = device
+ self._timeout_seconds = timeout_seconds
+ self._sleep = sleep
+ self._now = now
+ self._canceled = asyncio.Event()
+ self.details = CodexDeviceCodeDetails(
+ # Replaced by the coordinator with the id it minted.
+ login_id="",
+ verification_url=device.verification_url,
+ user_code=device.user_code,
+ )
+
+ async def wait(self) -> CodexLoginCompletion:
+ interval = self._device.interval
+ deadline = self._deadline()
+
+ while True:
+ if self._canceled.is_set():
+ raise CodexAuthError("ChatGPT sign-in was canceled")
+ if self._now().timestamp() >= deadline:
+ raise CodexAuthError("ChatGPT sign-in expired before it was approved")
+
+ result = await self._unless_canceled(
+ poll_device_code(self._device.device_auth_id, self._device.user_code)
+ )
+
+ if result.status == "approved":
+ return await self._complete(result)
+ if result.status == "denied":
+ raise CodexAuthError("ChatGPT sign-in was declined")
+ if result.status == "expired":
+ raise CodexAuthError("ChatGPT sign-in expired before it was approved")
+ if result.status == "slow_down":
+ # Back off rather than keeping the old cadence, or the server
+ # answers slow_down forever and the login never lands.
+ interval = min(interval * 2, _MAX_INTERVAL_SECONDS)
+
+ # Never sleep past the deadline: doing so reports the expiry up to a
+ # full interval after it actually happened.
+ remaining = deadline - self._now().timestamp()
+ await self._unless_canceled(self._sleep(min(interval, max(remaining, 0))))
+
+ async def _unless_canceled(self, operation: Awaitable[_T]) -> _T:
+ """Run one poll/sleep while letting cancel interrupt it immediately."""
+ operation_task = asyncio.ensure_future(operation)
+ canceled_task = asyncio.create_task(self._canceled.wait())
+ try:
+ done, _ = await asyncio.wait(
+ (operation_task, canceled_task),
+ return_when=asyncio.FIRST_COMPLETED,
+ )
+ if canceled_task in done:
+ operation_task.cancel()
+ raise CodexAuthError("ChatGPT sign-in was canceled")
+ return await operation_task
+ finally:
+ if not operation_task.done():
+ operation_task.cancel()
+ canceled_task.cancel()
+ await asyncio.gather(
+ operation_task,
+ canceled_task,
+ return_exceptions=True,
+ )
+
+ async def _complete(self, result) -> CodexLoginCompletion:
+ assert result.authorization_code and result.code_verifier
+ tokens = await exchange_authorization_code(
+ result.authorization_code, result.code_verifier
+ )
+ bundle = bundle_from_tokens(tokens)
+ credentials = credentials_from_bundle(bundle)
+ return CodexLoginCompletion(
+ bundle=bundle,
+ account=account_snapshot(credentials),
+ # Quota is reported on inference responses, not at sign-in.
+ rate_limits=None,
+ )
+
+ def _deadline(self) -> float:
+ remaining = self._device.seconds_remaining(now=self._now())
+ budget = self._timeout_seconds
+ if remaining is not None and remaining > 0:
+ # Never poll past the code's own expiry, however long we were
+ # configured to wait.
+ budget = min(budget, remaining)
+ return self._now().timestamp() + budget
+
+ async def cancel(self) -> None:
+ self._canceled.set()
+
+ async def close(self) -> None:
+ self._canceled.set()
+
+
+async def start_http_device_login(timeout_seconds: float) -> CodexHttpDeviceLogin:
+ device = await request_device_code()
+ logger.info("Started a ChatGPT device sign-in")
+ return CodexHttpDeviceLogin(device, timeout_seconds=timeout_seconds)
diff --git a/autogpt_platform/backend/backend/integrations/codex/device_login_test.py b/autogpt_platform/backend/backend/integrations/codex/device_login_test.py
new file mode 100644
index 0000000000..db0c8170d1
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/codex/device_login_test.py
@@ -0,0 +1,197 @@
+import asyncio
+from datetime import datetime, timedelta, timezone
+from unittest.mock import AsyncMock, patch
+
+import pytest
+
+from backend.integrations.codex.chatgpt_auth import (
+ ChatGPTDeviceCode,
+ ChatGPTPollResult,
+ CodexAuthError,
+)
+from backend.integrations.codex.device_login import CodexHttpDeviceLogin
+
+_NOW = datetime(2026, 1, 1, tzinfo=timezone.utc)
+
+
+def _device(interval: int = 5, expires_in: int | None = 900) -> ChatGPTDeviceCode:
+ return ChatGPTDeviceCode(
+ device_auth_id="deviceauth_abc",
+ user_code="FWK1-91A0I",
+ interval=interval,
+ expires_at=_NOW + timedelta(seconds=expires_in) if expires_in else None,
+ )
+
+
+class _Clock:
+ """A clock that only advances when the login sleeps."""
+
+ def __init__(self) -> None:
+ self.now = _NOW
+ self.slept: list[float] = []
+
+ async def sleep(self, seconds: float) -> None:
+ self.slept.append(seconds)
+ self.now += timedelta(seconds=seconds)
+
+ def __call__(self) -> datetime:
+ return self.now
+
+
+def _login(
+ clock: _Clock, device: ChatGPTDeviceCode | None = None, timeout: float = 900
+):
+ return CodexHttpDeviceLogin(
+ device or _device(),
+ timeout_seconds=timeout,
+ sleep=clock.sleep,
+ now=clock,
+ )
+
+
+def _patch_poll(results: list[ChatGPTPollResult]):
+ return patch(
+ "backend.integrations.codex.device_login.poll_device_code",
+ new=AsyncMock(side_effect=results),
+ )
+
+
+def test_details_carry_the_code_and_the_verify_url() -> None:
+ login = _login(_Clock())
+ assert login.details.user_code == "FWK1-91A0I"
+ assert login.details.verification_url == "https://auth.openai.com/codex/device"
+ # The coordinator overwrites this with the id it minted.
+ assert login.details.login_id == ""
+
+
+async def test_polling_continues_while_the_user_is_still_approving() -> None:
+ clock = _Clock()
+ results = [
+ ChatGPTPollResult(status="pending"),
+ ChatGPTPollResult(status="pending"),
+ ChatGPTPollResult(
+ status="approved", authorization_code="ac", code_verifier="cv"
+ ),
+ ]
+ tokens = AsyncMock()
+ with (
+ _patch_poll(results),
+ patch(
+ "backend.integrations.codex.device_login.exchange_authorization_code",
+ new=tokens,
+ ),
+ patch(
+ "backend.integrations.codex.device_login.bundle_from_tokens",
+ return_value="bundle",
+ ),
+ patch(
+ "backend.integrations.codex.device_login.credentials_from_bundle",
+ return_value="creds",
+ ),
+ patch(
+ "backend.integrations.codex.device_login.account_snapshot",
+ return_value=None,
+ ),
+ patch(
+ "backend.integrations.codex.device_login.CodexLoginCompletion",
+ lambda **kw: kw,
+ ),
+ ):
+ completion = await _login(clock).wait()
+
+ assert clock.slept == [5, 5]
+ assert completion["bundle"] == "bundle"
+ tokens.assert_awaited_once_with("ac", "cv")
+
+
+async def test_slow_down_backs_off_instead_of_holding_the_same_cadence() -> None:
+ """Keeping the old interval means the server answers slow_down forever."""
+ clock = _Clock()
+ results = [
+ ChatGPTPollResult(status="slow_down"),
+ ChatGPTPollResult(status="slow_down"),
+ ChatGPTPollResult(status="pending"),
+ ChatGPTPollResult(status="denied"),
+ ]
+ with _patch_poll(results):
+ with pytest.raises(CodexAuthError):
+ await _login(clock).wait()
+
+ assert clock.slept == [10, 20, 20]
+
+
+async def test_backoff_is_capped() -> None:
+ clock = _Clock()
+ results = [ChatGPTPollResult(status="slow_down") for _ in range(8)]
+ results.append(ChatGPTPollResult(status="denied"))
+ with _patch_poll(results):
+ with pytest.raises(CodexAuthError):
+ await _login(clock).wait()
+
+ assert max(clock.slept) == 60
+
+
+async def test_a_declined_sign_in_stops_rather_than_waiting_out_the_clock() -> None:
+ with _patch_poll([ChatGPTPollResult(status="denied")]):
+ with pytest.raises(CodexAuthError, match="declined"):
+ await _login(_Clock()).wait()
+
+
+async def test_an_expired_code_stops_immediately() -> None:
+ with _patch_poll([ChatGPTPollResult(status="expired")]):
+ with pytest.raises(CodexAuthError, match="expired"):
+ await _login(_Clock()).wait()
+
+
+async def test_polling_stops_at_the_codes_own_expiry_not_our_timeout() -> None:
+ """The configured timeout is long; the code's expiry is what binds here."""
+ clock = _Clock()
+ login = _login(clock, device=_device(expires_in=12), timeout=900)
+ with _patch_poll([ChatGPTPollResult(status="pending")] * 20):
+ with pytest.raises(CodexAuthError, match="expired"):
+ await login.wait()
+
+ # The final wait is trimmed to what is left of the code's life, so the
+ # expiry is reported at 12s rather than at the next 5s boundary.
+ assert clock.slept == [5, 5, 2]
+
+
+async def test_cancelling_stops_the_loop() -> None:
+ clock = _Clock()
+ login = _login(clock)
+ await login.cancel()
+
+ with _patch_poll([ChatGPTPollResult(status="pending")]):
+ with pytest.raises(CodexAuthError, match="canceled"):
+ await login.wait()
+
+ assert clock.slept == []
+
+
+async def test_cancelling_interrupts_an_in_flight_poll() -> None:
+ started = asyncio.Event()
+ never = asyncio.Event()
+
+ async def blocked_poll(_device_auth_id: str, _user_code: str):
+ started.set()
+ await never.wait()
+
+ login = _login(_Clock())
+ with patch(
+ "backend.integrations.codex.device_login.poll_device_code",
+ new=blocked_poll,
+ ):
+ waiting = asyncio.create_task(login.wait())
+ await started.wait()
+ await login.cancel()
+
+ with pytest.raises(CodexAuthError, match="canceled"):
+ await asyncio.wait_for(waiting, timeout=0.1)
diff --git a/autogpt_platform/backend/backend/integrations/codex/http_client.py b/autogpt_platform/backend/backend/integrations/codex/http_client.py
new file mode 100644
index 0000000000..eca3256f04
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/codex/http_client.py
@@ -0,0 +1,227 @@
+"""HTTP access to the ChatGPT Codex backend.
+
+Everything the Codex CLI used to broker -- the model catalog, rate limits,
+account identity and inference itself -- is reachable over ordinary HTTPS, so
+no binary, no subprocess and no synthetic ``$HOME`` are involved.
+
+Inference goes through the stock OpenAI SDK: the endpoint *is* the Responses
+API, so ``AsyncOpenAI`` pointed at the ChatGPT base URL streams, calls tools and
+reports usage without any bespoke SSE handling.
+"""
+
+import logging
+import uuid
+from typing import Any, Mapping
+
+from openai import AsyncOpenAI
+
+from backend.data.model import OAuth2Credentials
+from backend.integrations.codex.auth_bundle import decode_jwt_claims
+from backend.integrations.codex.chatgpt_auth import ORIGINATOR, USER_AGENT
+from backend.integrations.codex.credential_codec import bundle_from_credentials
+from backend.integrations.codex.models import (
+ CodexAccountSnapshot,
+ CodexModelInfo,
+ CodexRateLimitsSnapshot,
+ CodexRateLimitWindow,
+ CodexReasoningEffort,
+)
+from backend.util.request import Requests
+
+logger = logging.getLogger(__name__)
+
+API_BASE = "https://chatgpt.com/backend-api/codex"
+MODELS_URL = f"{API_BASE}/models"
+
+# ``/models`` requires the parameter but does not validate it: the catalog is
+# identical for "0.0.0" and a current CLI build, so this stays a constant rather
+# than a version we would have to chase.
+CLIENT_VERSION = "0.0.0"
+
+_VALID_EFFORTS: frozenset[str] = frozenset(
+ {"none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra"}
+)
+
+
+def build_client(credentials: OAuth2Credentials) -> AsyncOpenAI:
+ """Build a client bound to one user's subscription.
+
+ Deliberately constructed per call and never memoised. An ``AsyncOpenAI``
+ instance carries the credential it was built with, so a cached client would
+ silently bill every subsequent turn in the same worker to whichever user
+ happened to create it first.
+ """
+ return AsyncOpenAI(
+ api_key=credentials.access_token.get_secret_value(),
+ base_url=API_BASE,
+ default_headers=_inference_headers(credentials),
+ )
+
+
+def _inference_headers(credentials: OAuth2Credentials) -> dict[str, str]:
+ headers = {
+ "originator": ORIGINATOR,
+ "user-agent": USER_AGENT,
+ "session_id": str(uuid.uuid4()),
+ }
+ account_id = account_id_for(credentials)
+ if account_id:
+ headers["ChatGPT-Account-Id"] = account_id
+ return headers
+
+
+def account_id_for(credentials: OAuth2Credentials) -> str | None:
+ try:
+ bundle = bundle_from_credentials(credentials)
+ return decode_jwt_claims(bundle.tokens.id_token).chatgpt_account_id
+ except Exception:
+ logger.warning("Could not read the account id from a ChatGPT token")
+ return None
+
+
+def account_snapshot(credentials: OAuth2Credentials) -> CodexAccountSnapshot:
+ """Derive account identity from the token itself; there is no account API."""
+ try:
+ bundle = bundle_from_credentials(credentials)
+ claims = decode_jwt_claims(bundle.tokens.id_token)
+ except Exception:
+ return CodexAccountSnapshot(connected=False, requires_openai_auth=True)
+ return CodexAccountSnapshot(
+ connected=True,
+ requires_openai_auth=False,
+ account_type="chatgpt",
+ email=claims.email,
+ plan_type=claims.plan_type,
+ )
+
+
+async def fetch_models(credentials: OAuth2Credentials) -> list[CodexModelInfo]:
+ response = await Requests().get(
+ f"{MODELS_URL}?client_version={CLIENT_VERSION}",
+ headers={
+ "Authorization": f"Bearer {credentials.access_token.get_secret_value()}",
+ "accept": "application/json",
+ **_inference_headers(credentials),
+ },
+ )
+ payload = response.json()
+ entries = payload.get("models") if isinstance(payload, dict) else payload
+ if not isinstance(entries, list):
+ return []
+
+ models = [parsed for entry in entries if (parsed := _parse_model(entry))]
+ # ``priority`` orders the picker; the lowest listed one is what Codex
+ # itself preselects. Nothing in the payload flags a default directly.
+ default_slug = next(
+ (
+ model.model
+ for model in sorted(
+ (m for m in models if not m.hidden),
+ key=lambda m: _priority_of(entries, m.model),
+ )
+ ),
+ None,
+ )
+ return [
+ model.model_copy(update={"is_default": model.model == default_slug})
+ for model in models
+ ]
+
+
+def _priority_of(entries: list[Any], slug: str) -> int:
+ for entry in entries:
+ if isinstance(entry, dict) and entry.get("slug") == slug:
+ priority = entry.get("priority")
+ if isinstance(priority, int):
+ return priority
+ return 1_000
+
+
+def _parse_model(entry: Any) -> CodexModelInfo | None:
+ if not isinstance(entry, dict):
+ return None
+ slug = entry.get("slug")
+ if not isinstance(slug, str) or not slug:
+ return None
+
+ efforts = _supported_efforts(entry.get("supported_reasoning_levels"))
+ default_effort = entry.get("default_reasoning_level")
+ if default_effort not in _VALID_EFFORTS:
+ default_effort = efforts[0] if efforts else "medium"
+
+ modalities = entry.get("input_modalities")
+ return CodexModelInfo(
+ model=slug,
+ display_name=str(entry.get("display_name") or slug),
+ # Overwritten by the caller once the full list has been ranked.
+ is_default=False,
+ hidden=entry.get("visibility") == "hide",
+ default_reasoning_effort=default_effort, # type: ignore[arg-type]
+ supported_reasoning_efforts=efforts,
+ input_modalities=(
+ [str(m) for m in modalities] if isinstance(modalities, list) else []
+ ),
+ )
+
+
+def _supported_efforts(raw: Any) -> list[CodexReasoningEffort]:
+ """Levels arrive as ``[{"effort": "low", "description": ...}, ...]``."""
+ if not isinstance(raw, list):
+ return []
+ efforts: list[CodexReasoningEffort] = []
+ for level in raw:
+ effort = level.get("effort") if isinstance(level, dict) else level
+ if isinstance(effort, str) and effort in _VALID_EFFORTS:
+ efforts.append(effort) # type: ignore[arg-type]
+ return efforts
+
+
+# --------------------------------------------------------------------------- #
+# Rate limits
+#
+# The backend reports quota on the response headers of every inference call, so
+# there is nothing extra to poll -- the numbers arrive with the work that spent
+# them, and are always current.
+# --------------------------------------------------------------------------- #
+
+
+def parse_rate_limits(headers: Mapping[str, str]) -> CodexRateLimitsSnapshot:
+ lookup = {key.lower(): value for key, value in headers.items()}
+ return CodexRateLimitsSnapshot(
+ plan_type=lookup.get("x-codex-plan-type"),
+ limit_id=lookup.get("x-codex-active-limit"),
+ limit_name=lookup.get("x-codex-limit-name"),
+ primary=_window(lookup, "primary"),
+ secondary=_window(lookup, "secondary"),
+ has_credits=_bool(lookup.get("x-codex-credits-has-credits")),
+ unlimited_credits=_bool(lookup.get("x-codex-credits-unlimited")),
+ )
+
+
+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"))
+ # A zero-length window is how an inactive tier is reported, not a real one.
+ if used is None or not window:
+ return None
+ return CodexRateLimitWindow(
+ used_percent=used,
+ window_duration_mins=window,
+ resets_at=_int(lookup.get(f"x-codex-{scope}-reset-at")),
+ )
+
+
+def _int(value: str | None) -> int | None:
+ if value is None or value == "":
+ return None
+ try:
+ return int(float(value))
+ except ValueError:
+ return None
+
+
+def _bool(value: str | None) -> bool | None:
+ """Sent title-cased ("False"), so a plain ``== "true"`` would misread it."""
+ if value is None or value == "":
+ return None
+ return value.strip().lower() == "true"
diff --git a/autogpt_platform/backend/backend/integrations/codex/http_client_test.py b/autogpt_platform/backend/backend/integrations/codex/http_client_test.py
new file mode 100644
index 0000000000..8e5689d5fd
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/codex/http_client_test.py
@@ -0,0 +1,198 @@
+import base64
+import json
+
+from pydantic import SecretStr
+
+from backend.integrations.codex.auth_bundle import CodexAuthBundleV1, CodexAuthTokensV1
+from backend.integrations.codex.credential_codec import credentials_from_bundle
+from backend.integrations.codex.http_client import (
+ _parse_model,
+ account_id_for,
+ account_snapshot,
+ parse_rate_limits,
+)
+
+
+def _jwt(payload: dict[str, object]) -> str:
+ encoded = (
+ base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
+ )
+ return f"header.{encoded}.signature"
+
+
+def _credentials_with_identity_only_in_id_token():
+ id_token = _jwt(
+ {
+ "email": "[email protected]",
+ "https://api.openai.com/auth": {
+ "chatgpt_plan_type": "pro",
+ "chatgpt_account_id": "account-from-id-token",
+ },
+ }
+ )
+ access_token = _jwt({"exp": 2_000_000_000})
+ bundle = CodexAuthBundleV1(
+ tokens=CodexAuthTokensV1(
+ id_token=SecretStr(id_token),
+ access_token=SecretStr(access_token),
+ refresh_token=SecretStr("refresh-token"),
+ ),
+ codex_runtime_version="http",
+ )
+ return credentials_from_bundle(bundle)
+
+
+def test_account_identity_comes_from_the_id_token() -> None:
+ credentials = _credentials_with_identity_only_in_id_token()
+
+ assert account_id_for(credentials) == "account-from-id-token"
+ assert account_snapshot(credentials).model_dump() == {
+ "connected": True,
+ "requires_openai_auth": False,
+ "account_type": "chatgpt",
+ "email": "[email protected]",
+ "plan_type": "pro",
+ }
+
+
+def _model(**over: object) -> dict[str, object]:
+ entry: dict[str, object] = {
+ "slug": "gpt-5.6-sol",
+ "display_name": "GPT-5.6 Sol",
+ "visibility": "list",
+ "priority": 1,
+ "default_reasoning_level": "low",
+ "supported_reasoning_levels": [
+ {"effort": "low", "description": "…"},
+ {"effort": "medium", "description": "…"},
+ ],
+ "input_modalities": ["text", "image"],
+ }
+ entry.update(over)
+ return entry
+
+
+# --------------------------------------------------------------------------- #
+# Model catalog
+# --------------------------------------------------------------------------- #
+
+
+def test_reasoning_levels_are_unwrapped_from_their_description_objects() -> None:
+ parsed = _parse_model(_model())
+ assert parsed is not None
+ assert parsed.supported_reasoning_efforts == ["low", "medium"]
+
+
+def test_unknown_reasoning_levels_are_dropped_rather_than_failing_the_model() -> None:
+ """A new effort tier upstream must not take the whole catalog down."""
+ parsed = _parse_model(
+ _model(supported_reasoning_levels=[{"effort": "low"}, {"effort": "warp"}])
+ )
+ assert parsed is not None
+ assert parsed.supported_reasoning_efforts == ["low"]
+
+
+def test_an_unknown_default_effort_falls_back_to_a_supported_one() -> None:
+ parsed = _parse_model(_model(default_reasoning_level="warp"))
+ assert parsed is not None
+ assert parsed.default_reasoning_effort == "low"
+
+
+def test_hidden_is_driven_by_visibility() -> None:
+ assert _parse_model(_model(visibility="hide")).hidden is True # type: ignore[union-attr]
+ assert _parse_model(_model(visibility="list")).hidden is False # type: ignore[union-attr]
+
+
+def test_an_entry_without_a_slug_is_skipped() -> None:
+ assert _parse_model(_model(slug="")) is None
+ assert _parse_model({"display_name": "orphan"}) is None
+ assert _parse_model("not-a-dict") is None
+
+
+def test_display_name_falls_back_to_the_slug() -> None:
+ parsed = _parse_model(_model(display_name=None))
+ assert parsed is not None
+ assert parsed.display_name == "gpt-5.6-sol"
+
+
+# --------------------------------------------------------------------------- #
+# Rate limits
+#
+# These arrive as headers on every inference call, so a misparse silently
+# misreports someone's remaining quota rather than raising.
+# --------------------------------------------------------------------------- #
+
+
+def test_credit_booleans_are_title_cased_on_the_wire() -> None:
+ """Sent as "False"/"True" — truthiness on the raw string inverts the flag."""
+ limits = parse_rate_limits(
+ {
+ "x-codex-credits-has-credits": "False",
+ "x-codex-credits-unlimited": "True",
+ }
+ )
+ assert limits.has_credits is False
+ assert limits.unlimited_credits is True
+
+
+def test_absent_credit_headers_stay_unknown_rather_than_becoming_false() -> None:
+ limits = parse_rate_limits({})
+ assert limits.has_credits is None
+ assert limits.unlimited_credits is None
+
+
+def test_a_zero_length_window_is_not_reported_as_a_real_window() -> None:
+ """An inactive tier reports window-minutes=0; keeping it invents a limit."""
+ limits = parse_rate_limits(
+ {
+ "x-codex-secondary-used-percent": "0",
+ "x-codex-secondary-window-minutes": "0",
+ "x-codex-secondary-reset-at": "",
+ }
+ )
+ assert limits.secondary is None
+
+
+def test_an_active_window_is_parsed_whole() -> None:
+ limits = parse_rate_limits(
+ {
+ "x-codex-plan-type": "pro",
+ "x-codex-active-limit": "premium",
+ "x-codex-primary-used-percent": "3",
+ "x-codex-primary-window-minutes": "10080",
+ "x-codex-primary-reset-at": "1788452827",
+ }
+ )
+ assert limits.plan_type == "pro"
+ assert limits.limit_id == "premium"
+ assert limits.primary is not None
+ assert limits.primary.used_percent == 3
+ assert limits.primary.window_duration_mins == 10080
+ assert limits.primary.resets_at == 1788452827
+
+
+def test_header_lookup_is_case_insensitive() -> None:
+ limits = parse_rate_limits({"X-Codex-Plan-Type": "pro"})
+ assert limits.plan_type == "pro"
+
+
+def test_a_zero_percent_window_is_kept_when_the_window_is_real() -> None:
+ """0% used is a valid reading; only a zero-length *window* means absent."""
+ limits = parse_rate_limits(
+ {
+ "x-codex-primary-used-percent": "0",
+ "x-codex-primary-window-minutes": "300",
+ }
+ )
+ assert limits.primary is not None
+ assert limits.primary.used_percent == 0
+
+
+def test_unparseable_numbers_do_not_raise() -> None:
+ limits = parse_rate_limits(
+ {
+ "x-codex-primary-used-percent": "n/a",
+ "x-codex-primary-window-minutes": "300",
+ }
+ )
+ assert limits.primary is None
diff --git a/autogpt_platform/backend/backend/integrations/codex/http_session.py b/autogpt_platform/backend/backend/integrations/codex/http_session.py
new file mode 100644
index 0000000000..ba89379162
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/codex/http_session.py
@@ -0,0 +1,386 @@
+"""Agentic Codex turns over HTTP.
+
+The ChatGPT backend speaks the Responses API, so a turn is an ordinary
+streaming request plus a tool loop: stream a response, hand any function calls
+to the caller's handler, feed the outputs back, repeat until the model stops
+asking for tools.
+
+Two details make the difference between this working and subtly degrading on
+reasoning models:
+
+- every output item is echoed back into the next request, reasoning items
+ included, so the model keeps its chain across tool hops; and
+- ``include=["reasoning.encrypted_content"]`` is requested so those items come
+ back in a form that can be replayed at all.
+"""
+
+import asyncio
+import json
+import logging
+import time
+from collections.abc import Awaitable, Callable
+from typing import Any
+
+from openai import AsyncOpenAI
+
+from backend.data.model import OAuth2Credentials
+from backend.integrations.codex.http_client import build_client, parse_rate_limits
+from backend.integrations.codex.models import (
+ CodexDynamicToolCall,
+ CodexDynamicToolResult,
+ CodexDynamicToolSpec,
+ CodexInvocationRequest,
+ CodexInvocationResult,
+ CodexRateLimitsSnapshot,
+ CodexStreamEvent,
+ CodexTokenUsage,
+)
+
+logger = logging.getLogger(__name__)
+
+EventHandler = Callable[[CodexStreamEvent], Awaitable[None]]
+ToolHandler = Callable[[CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]]
+
+# A turn that keeps asking for tools without converging is a bug somewhere;
+# stop rather than spending someone's quota in a loop.
+MAX_TOOL_ITERATIONS = 32
+
+
+class CodexHttpSession:
+ """One user's Codex turn, run over HTTPS."""
+
+ def __init__(
+ self,
+ credentials: OAuth2Credentials,
+ *,
+ turn_timeout_seconds: float,
+ tool_timeout_seconds: float,
+ client: AsyncOpenAI | None = None,
+ ) -> None:
+ # Built per session and never shared: an AsyncOpenAI instance carries
+ # the credential it was made with, so a reused one would bill another
+ # user's subscription.
+ self._client = client or build_client(credentials)
+ self._turn_timeout_seconds = turn_timeout_seconds
+ self._tool_timeout_seconds = tool_timeout_seconds
+ self._rate_limits: CodexRateLimitsSnapshot | None = None
+
+ @property
+ def rate_limits(self) -> CodexRateLimitsSnapshot | None:
+ """Quota as of the last call; the backend reports it on every response."""
+ return self._rate_limits
+
+ async def invoke(
+ self,
+ request: CodexInvocationRequest,
+ dynamic_tools: list[CodexDynamicToolSpec],
+ tool_handler: ToolHandler,
+ event_handler: EventHandler | None = None,
+ ) -> CodexInvocationResult:
+ timeout = request.timeout_seconds or self._turn_timeout_seconds
+ try:
+ return await asyncio.wait_for(
+ self._run(request, dynamic_tools, tool_handler, event_handler),
+ timeout=timeout,
+ )
+ except asyncio.TimeoutError:
+ raise CodexInvocationTimeoutError("codex_copilot_turn_timeout") from None
+
+ async def _run(
+ self,
+ request: CodexInvocationRequest,
+ dynamic_tools: list[CodexDynamicToolSpec],
+ tool_handler: ToolHandler,
+ event_handler: EventHandler | None,
+ ) -> CodexInvocationResult:
+ started = time.monotonic()
+ conversation: list[dict[str, Any]] = [_user_message(request.prompt)]
+ tools = [_tool_payload(spec) for spec in dynamic_tools]
+
+ response_id = ""
+ resolved_model: str | None = None
+ final_text = ""
+ reasoning_summary = ""
+ usage = _ZERO_USAGE
+
+ for _ in range(MAX_TOOL_ITERATIONS):
+ turn = await self._stream_turn(request, conversation, tools, event_handler)
+
+ response_id = turn.response_id or response_id
+ resolved_model = turn.model or resolved_model
+ usage = _add_usage(usage, turn.usage)
+ if turn.text:
+ final_text = turn.text
+ if turn.reasoning_summary:
+ reasoning_summary = turn.reasoning_summary
+
+ # Replay the whole output, not just the tool calls: dropping the
+ # reasoning items breaks the model's chain on reasoning models.
+ conversation.extend(turn.output_items)
+
+ if not turn.tool_calls:
+ return CodexInvocationResult(
+ response_id=response_id,
+ final_response=final_text,
+ reasoning_summary=reasoning_summary or None,
+ status="completed",
+ resolved_model=resolved_model or request.model,
+ duration_ms=int((time.monotonic() - started) * 1000),
+ usage=usage,
+ )
+
+ for call in turn.tool_calls:
+ conversation.append(await self._dispatch_tool(call, tool_handler))
+
+ raise CodexTurnLimitError(
+ f"Codex turn did not converge within {MAX_TOOL_ITERATIONS} tool rounds"
+ )
+
+ async def _dispatch_tool(
+ self, call: "_ToolCall", tool_handler: ToolHandler
+ ) -> dict[str, Any]:
+ request = CodexDynamicToolCall(
+ thread_id=call.response_id,
+ turn_id=call.response_id,
+ call_id=call.call_id,
+ namespace=None,
+ tool=call.name,
+ arguments=call.arguments,
+ )
+ try:
+ result = await asyncio.wait_for(
+ tool_handler(request), timeout=self._tool_timeout_seconds
+ )
+ output = result.content
+ except 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)
+ 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,
+ }
+
+ async def _stream_turn(
+ self,
+ request: CodexInvocationRequest,
+ conversation: list[dict[str, Any]],
+ tools: list[dict[str, Any]],
+ event_handler: EventHandler | None,
+ ) -> "_TurnOutcome":
+ payload: dict[str, Any] = {
+ "model": request.model,
+ "input": list(conversation),
+ "store": False,
+ "stream": True,
+ # Without this the reasoning items replayed above are unusable.
+ "include": ["reasoning.encrypted_content"],
+ }
+ if request.instructions:
+ payload["instructions"] = request.instructions
+ if tools:
+ payload["tools"] = tools
+ payload["tool_choice"] = "auto"
+ if request.effort:
+ payload["reasoning"] = {"effort": request.effort}
+ if request.output_schema:
+ payload["text"] = {
+ "format": {
+ "type": "json_schema",
+ "name": "output",
+ "schema": request.output_schema,
+ "strict": False,
+ }
+ }
+
+ raw = await self._client.responses.with_raw_response.create(**payload)
+ self._rate_limits = parse_rate_limits(raw.headers)
+
+ outcome = _TurnOutcome()
+ # with_raw_response returns LegacyAPIResponse for streaming requests;
+ # its synchronous parse() returns the AsyncStream to iterate.
+ stream = raw.parse()
+ async for event in stream:
+ await _consume_event(event, outcome, event_handler)
+ return outcome
+
+
+async def _consume_event(
+ event: Any, outcome: "_TurnOutcome", event_handler: EventHandler | None
+) -> None:
+ kind = getattr(event, "type", "")
+
+ if kind == "response.output_text.delta":
+ delta = getattr(event, "delta", "") or ""
+ outcome.text += delta
+ if delta and event_handler:
+ await event_handler(CodexStreamEvent(type="text_delta", delta=delta))
+ return
+
+ if kind == "response.reasoning_summary_text.delta":
+ delta = getattr(event, "delta", "") or ""
+ outcome.reasoning_summary += delta
+ if delta and event_handler:
+ await event_handler(CodexStreamEvent(type="reasoning_delta", delta=delta))
+ return
+
+ if kind == "response.output_item.done":
+ item = getattr(event, "item", None)
+ if item is None:
+ return
+ outcome.output_items.append(_as_dict(item))
+ if getattr(item, "type", "") == "function_call":
+ outcome.tool_calls.append(
+ _ToolCall(
+ call_id=getattr(item, "call_id", "") or "",
+ name=getattr(item, "name", "") or "",
+ arguments=_parse_arguments(getattr(item, "arguments", "")),
+ response_id=outcome.response_id,
+ )
+ )
+ return
+
+ if kind in ("response.created", "response.completed", "response.in_progress"):
+ response = getattr(event, "response", None)
+ if response is None:
+ return
+ outcome.response_id = getattr(response, "id", "") or outcome.response_id
+ outcome.model = getattr(response, "model", None) or outcome.model
+ if (raw_usage := getattr(response, "usage", None)) is not None:
+ outcome.usage = _usage_from(raw_usage)
+ return
+
+ if kind in ("response.failed", "error"):
+ raise CodexTurnFailedError(_failure_message(event))
+
+
+class CodexInvocationTimeoutError(RuntimeError):
+ """The whole turn exceeded its deadline."""
+
+
+class CodexTurnLimitError(RuntimeError):
+ """The tool loop did not converge."""
+
+
+class CodexTurnFailedError(RuntimeError):
+ """ChatGPT reported the turn as failed."""
+
+
+class _ToolCall:
+ __slots__ = ("call_id", "name", "arguments", "response_id")
+
+ def __init__(
+ self, call_id: str, name: str, arguments: object, response_id: str
+ ) -> None:
+ self.call_id = call_id
+ self.name = name
+ self.arguments = arguments
+ self.response_id = response_id
+
+
+class _TurnOutcome:
+ def __init__(self) -> None:
+ self.response_id = ""
+ self.model: str | None = None
+ self.text = ""
+ self.reasoning_summary = ""
+ self.output_items: list[dict[str, Any]] = []
+ self.tool_calls: list[_ToolCall] = []
+ self.usage: CodexTokenUsage | None = None
+
+
+_ZERO_USAGE = CodexTokenUsage(
+ input_tokens=0,
+ cached_input_tokens=0,
+ output_tokens=0,
+ reasoning_output_tokens=0,
+ total_tokens=0,
+)
+
+
+def _user_message(prompt: str) -> dict[str, Any]:
+ return {
+ "type": "message",
+ "role": "user",
+ "content": [{"type": "input_text", "text": prompt}],
+ }
+
+
+def _tool_payload(spec: CodexDynamicToolSpec) -> dict[str, Any]:
+ return {
+ "type": "function",
+ "name": spec.name,
+ "description": spec.description,
+ "parameters": spec.input_schema,
+ "strict": False,
+ }
+
+
+def _parse_arguments(raw: object) -> object:
+ """Arguments arrive as a JSON string; hand the handler the parsed form."""
+ if not isinstance(raw, str):
+ return raw
+ if not raw:
+ return {}
+ try:
+ return json.loads(raw)
+ except ValueError:
+ # A malformed call is the model's mistake, not ours -- pass it through
+ # so the handler can report it back rather than killing the turn.
+ return raw
+
+
+def _as_dict(item: Any) -> dict[str, Any]:
+ """Normalise an SDK output item into the shape the next request replays."""
+ dump = getattr(item, "model_dump", None)
+ if callable(dump):
+ dumped = dump(exclude_none=True)
+ return dumped if isinstance(dumped, dict) else {}
+ return dict(item) if isinstance(item, dict) else {}
+
+
+def _usage_from(raw: Any) -> CodexTokenUsage:
+ input_details = getattr(raw, "input_tokens_details", None)
+ output_details = getattr(raw, "output_tokens_details", None)
+ return CodexTokenUsage(
+ input_tokens=int(getattr(raw, "input_tokens", 0) or 0),
+ cached_input_tokens=int(getattr(input_details, "cached_tokens", 0) or 0),
+ output_tokens=int(getattr(raw, "output_tokens", 0) or 0),
+ reasoning_output_tokens=int(
+ getattr(output_details, "reasoning_tokens", 0) or 0
+ ),
+ total_tokens=int(getattr(raw, "total_tokens", 0) or 0),
+ )
+
+
+def _add_usage(
+ running: CodexTokenUsage, turn: CodexTokenUsage | None
+) -> CodexTokenUsage:
+ """Usage is per response, so a multi-hop turn has to accumulate it."""
+ if turn is None:
+ return running
+ return CodexTokenUsage(
+ input_tokens=running.input_tokens + turn.input_tokens,
+ cached_input_tokens=running.cached_input_tokens + turn.cached_input_tokens,
+ output_tokens=running.output_tokens + turn.output_tokens,
+ reasoning_output_tokens=(
+ running.reasoning_output_tokens + turn.reasoning_output_tokens
+ ),
+ total_tokens=running.total_tokens + turn.total_tokens,
+ )
+
+
+def _failure_message(event: Any) -> str:
+ response = getattr(event, "response", None)
+ error = getattr(response, "error", None) or getattr(event, "error", None)
+ message = getattr(error, "message", None)
+ return str(message or "ChatGPT reported the turn as failed")
diff --git a/autogpt_platform/backend/backend/integrations/codex/http_session_test.py b/autogpt_platform/backend/backend/integrations/codex/http_session_test.py
new file mode 100644
index 0000000000..0697de020b
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/codex/http_session_test.py
@@ -0,0 +1,423 @@
+import asyncio
+from typing import Any
+
+import httpx
+import pytest
+from openai import AsyncOpenAI
+from pydantic import SecretStr
+
+from backend.data.model import OAuth2Credentials
+from backend.integrations.codex.http_session import (
+ MAX_TOOL_ITERATIONS,
+ CodexHttpSession,
+ CodexTurnFailedError,
+ CodexTurnLimitError,
+ _add_usage,
+ _parse_arguments,
+)
+from backend.integrations.codex.models import (
+ CodexDynamicToolCall,
+ CodexDynamicToolResult,
+ CodexDynamicToolSpec,
+ CodexInvocationRequest,
+ CodexStreamEvent,
+ CodexTokenUsage,
+)
+
+# --------------------------------------------------------------------------- #
+# Fakes for the streaming SDK surface
+# --------------------------------------------------------------------------- #
+
+
+class _Event:
+ def __init__(self, type: str, **fields: Any) -> None:
+ self.type = type
+ for key, value in fields.items():
+ setattr(self, key, value)
+
+
+class _Item:
+ def __init__(self, **fields: Any) -> None:
+ for key, value in fields.items():
+ setattr(self, key, value)
+
+ def model_dump(self, exclude_none: bool = False) -> dict[str, Any]:
+ return {k: v for k, v in self.__dict__.items() if v is not None}
+
+
+class _RawResponse:
+ def __init__(self, events: list[_Event], headers: dict[str, str]) -> None:
+ self._events = events
+ self.headers = headers
+
+ def parse(self):
+ # Match LegacyAPIResponse.parse, which synchronously returns the
+ # AsyncStream for an SSE response.
+ async def stream():
+ for event in self._events:
+ yield event
+
+ return stream()
+
+
+class _FakeResponses:
+ """Returns one scripted turn per call, recording each request."""
+
+ def __init__(self, turns: list[list[_Event]], headers: dict[str, str]) -> None:
+ self._turns = turns
+ self._headers = headers
+ self.requests: list[dict[str, Any]] = []
+
+ @property
+ def with_raw_response(self):
+ return self
+
+ async def create(self, **payload: Any) -> _RawResponse:
+ self.requests.append(payload)
+ events = self._turns.pop(0) if self._turns else []
+ return _RawResponse(events, self._headers)
+
+
+class _FakeClient:
+ def __init__(self, turns: list[list[_Event]], headers: dict[str, str]) -> None:
+ self.responses = _FakeResponses(turns, headers)
+
+
+def _credentials() -> OAuth2Credentials:
+ return OAuth2Credentials(
+ provider="codex",
+ title="t",
+ access_token=SecretStr("at"),
+ refresh_token=SecretStr("rt"),
+ scopes=[],
+ )
+
+
+def _session(turns: list[list[_Event]], headers: dict[str, str] | None = None):
+ client = _FakeClient(turns, headers or {})
+ session = CodexHttpSession(
+ _credentials(),
+ turn_timeout_seconds=30,
+ tool_timeout_seconds=5,
+ client=client, # type: ignore[arg-type]
+ )
+ return session, client
+
+
+def _text_turn(text: str, response_id: str = "resp-1") -> list[_Event]:
+ return [
+ _Event("response.created", response=_Item(id=response_id, model="gpt-5.6-sol")),
+ _Event("response.output_text.delta", delta=text),
+ _Event(
+ "response.completed",
+ response=_Item(id=response_id, model="gpt-5.6-sol", usage=None),
+ ),
+ ]
+
+
+def _tool_turn(name: str, arguments: str, call_id: str = "call-1") -> list[_Event]:
+ return [
+ _Event("response.created", response=_Item(id="resp-0", model="gpt-5.6-sol")),
+ _Event(
+ "response.output_item.done",
+ item=_Item(
+ type="function_call", name=name, arguments=arguments, call_id=call_id
+ ),
+ ),
+ ]
+
+
+async def _echo_tool(call: CodexDynamicToolCall) -> CodexDynamicToolResult:
+ return CodexDynamicToolResult(content=f"ran {call.tool}")
+
+
+def _request(**over: Any) -> CodexInvocationRequest:
+ fields: dict[str, Any] = {"prompt": "hi", "model": "gpt-5.6-sol"}
+ fields.update(over)
+ return CodexInvocationRequest(**fields)
+
+
+TOOL = CodexDynamicToolSpec(
+ name="lookup", description="Look something up.", input_schema={"type": "object"}
+)
+
+
+# --------------------------------------------------------------------------- #
+# The installed OpenAI SDK streaming contract
+# --------------------------------------------------------------------------- #
+
+
+async def test_installed_sdk_raw_stream_is_consumed_without_awaiting_it() -> None:
+ def respond(_request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ headers={"content-type": "text/event-stream"},
+ content=b"data: [DONE]\n\n",
+ )
+
+ async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as http:
+ client = AsyncOpenAI(
+ api_key="test",
+ base_url="https://example.test",
+ http_client=http,
+ )
+ session = CodexHttpSession(
+ _credentials(),
+ turn_timeout_seconds=30,
+ tool_timeout_seconds=5,
+ client=client,
+ )
+
+ result = await session.invoke(_request(), [], _echo_tool)
+
+ assert result.status == "completed"
+
+
+# --------------------------------------------------------------------------- #
+# The tool loop
+# --------------------------------------------------------------------------- #
+
+
+async def test_a_turn_without_tool_calls_completes_immediately() -> None:
+ session, client = _session([_text_turn("hello")])
+ result = await session.invoke(_request(), [], _echo_tool)
+
+ assert result.final_response == "hello"
+ assert result.status == "completed"
+ assert len(client.responses.requests) == 1
+
+
+async def test_a_tool_call_is_dispatched_and_its_output_fed_back() -> None:
+ session, client = _session([_tool_turn("lookup", '{"q": "x"}'), _text_turn("done")])
+ seen: list[CodexDynamicToolCall] = []
+
+ async def handler(call: CodexDynamicToolCall) -> CodexDynamicToolResult:
+ seen.append(call)
+ return CodexDynamicToolResult(content="tool output")
+
+ result = await session.invoke(_request(), [TOOL], handler)
+
+ assert [c.tool for c in seen] == ["lookup"]
+ # Arguments reach the handler parsed, not as a JSON blob.
+ assert seen[0].arguments == {"q": "x"}
+ assert result.final_response == "done"
+
+ second_input = client.responses.requests[1]["input"]
+ outputs = [i for i in second_input if i.get("type") == "function_call_output"]
+ assert outputs == [
+ {"type": "function_call_output", "call_id": "call-1", "output": "tool output"}
+ ]
+
+
+async 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."""
+ first = _tool_turn("lookup", "{}")
+ first.insert(
+ 1,
+ _Event(
+ "response.output_item.done",
+ item=_Item(type="reasoning", id="rs-1", encrypted_content="opaque"),
+ ),
+ )
+ session, client = _session([first, _text_turn("done")])
+
+ await session.invoke(_request(), [TOOL], _echo_tool)
+
+ replayed = client.responses.requests[1]["input"]
+ assert any(item.get("type") == "reasoning" for item in replayed)
+ assert client.responses.requests[0]["include"] == ["reasoning.encrypted_content"]
+
+
+async def test_a_tool_timeout_is_reported_to_the_model_not_raised() -> None:
+ session, client = _session([_tool_turn("lookup", "{}"), _text_turn("recovered")])
+
+ async def slow(call: CodexDynamicToolCall) -> CodexDynamicToolResult:
+ await asyncio.sleep(10)
+ return CodexDynamicToolResult(content="never")
+
+ session._tool_timeout_seconds = 0.01 # type: ignore[attr-defined]
+ result = await session.invoke(_request(), [TOOL], slow)
+
+ assert result.final_response == "recovered"
+ output = [
+ i
+ for i in client.responses.requests[1]["input"]
+ if i.get("type") == "function_call_output"
+ ][0]
+ assert "timed out" in output["output"]
+
+
+async def test_a_tool_failure_is_returned_to_the_model_without_leaking_details() -> (
+ None
+):
+ session, client = _session([_tool_turn("lookup", "{}"), _text_turn("recovered")])
+
+ async def broken(_call: CodexDynamicToolCall) -> CodexDynamicToolResult:
+ raise RuntimeError("secret provider detail")
+
+ result = await session.invoke(_request(), [TOOL], broken)
+
+ assert result.final_response == "recovered"
+ output = [
+ item
+ for item in client.responses.requests[1]["input"]
+ if item.get("type") == "function_call_output"
+ ][0]["output"]
+ assert output == "Tool 'lookup' failed."
+ assert "secret provider detail" not in output
+
+
+async def test_a_loop_that_never_converges_stops_instead_of_spending_quota() -> None:
+ turns = [_tool_turn("lookup", "{}") for _ in range(MAX_TOOL_ITERATIONS + 2)]
+ session, _ = _session(turns)
+
+ with pytest.raises(CodexTurnLimitError):
+ await session.invoke(_request(), [TOOL], _echo_tool)
+
+
+async def test_a_failed_response_raises_rather_than_returning_empty_text() -> None:
+ events = [_Event("response.failed", response=_Item(error=_Item(message="boom")))]
+ session, _ = _session([events])
+
+ with pytest.raises(CodexTurnFailedError):
+ await session.invoke(_request(), [], _echo_tool)
+
+
+# --------------------------------------------------------------------------- #
+# Streaming, payload shape and accounting
+# --------------------------------------------------------------------------- #
+
+
+async def test_text_and_reasoning_deltas_are_reported_separately() -> None:
+ events = _text_turn("hi")
+ events.insert(1, _Event("response.reasoning_summary_text.delta", delta="thinking"))
+ session, _ = _session([events])
+
+ seen: list[CodexStreamEvent] = []
+
+ async def on_event(event: CodexStreamEvent) -> None:
+ seen.append(event)
+
+ result = await session.invoke(_request(), [], _echo_tool, on_event)
+
+ assert [(e.type, e.delta) for e in seen] == [
+ ("reasoning_delta", "thinking"),
+ ("text_delta", "hi"),
+ ]
+ assert result.reasoning_summary == "thinking"
+
+
+async def test_tools_are_omitted_entirely_when_none_are_offered() -> None:
+ """Sending an empty tools array is not the same as sending none."""
+ session, client = _session([_text_turn("hi")])
+ await session.invoke(_request(), [], _echo_tool)
+
+ payload = client.responses.requests[0]
+ assert "tools" not in payload
+ assert "tool_choice" not in payload
+ assert payload["store"] is False
+
+
+async def test_effort_and_output_schema_are_passed_through() -> None:
+ session, client = _session([_text_turn("hi")])
+ await session.invoke(
+ _request(effort="high", output_schema={"type": "object"}), [], _echo_tool
+ )
+
+ payload = client.responses.requests[0]
+ assert payload["reasoning"] == {"effort": "high"}
+ assert payload["text"]["format"]["schema"] == {"type": "object"}
+
+
+async def test_rate_limits_are_captured_from_the_response_headers() -> None:
+ session, _ = _session(
+ [_text_turn("hi")],
+ headers={
+ "x-codex-plan-type": "pro",
+ "x-codex-primary-used-percent": "7",
+ "x-codex-primary-window-minutes": "10080",
+ },
+ )
+ await session.invoke(_request(), [], _echo_tool)
+
+ assert session.rate_limits is not None
+ assert session.rate_limits.plan_type == "pro"
+ assert session.rate_limits.primary is not None
+ assert session.rate_limits.primary.used_percent == 7
+
+
+def test_usage_accumulates_across_tool_hops() -> None:
+ """Usage is reported per response, so a multi-hop turn must sum it."""
+ one = CodexTokenUsage(
+ input_tokens=10,
+ cached_input_tokens=1,
+ output_tokens=2,
+ reasoning_output_tokens=3,
+ total_tokens=12,
+ )
+ total = _add_usage(_add_usage(one, one), None)
+ assert total.input_tokens == 20
+ assert total.total_tokens == 24
+ assert total.reasoning_output_tokens == 6
+
+
+ "raw,expected",
+ [('{"a": 1}', {"a": 1}), ("", {}), ("not json", "not json"), ({"a": 1}, {"a": 1})],
+)
+def test_tool_arguments_are_parsed_without_killing_the_turn(raw, expected) -> None:
+ assert _parse_arguments(raw) == expected
+
+
+# --------------------------------------------------------------------------- #
+# Rate-limit reporting through the transport
+#
+# ChatGPT reports quota only on inference response headers, so a connection
+# that has not run a turn genuinely has nothing to report.
+# --------------------------------------------------------------------------- #
+
+
+async def test_rate_limits_are_unknown_before_any_turn_rather_than_an_error() -> None:
+ from backend.integrations.codex.transport import CodexTransport
+
+ class _Lease:
+ credentials = _credentials_with_state()
+
+ limits = await CodexTransport().rate_limits(_Lease()) # type: ignore[arg-type]
+ assert limits.primary is None
+ assert limits.plan_type is None
+
+
+def _credentials_with_state() -> OAuth2Credentials:
+ from backend.integrations.codex.auth_bundle import (
+ CodexAuthBundleV1,
+ CodexAuthTokensV1,
+ )
+ from backend.integrations.codex.credential_codec import credentials_from_bundle
+
+ token = (
+ "eyJhbGciOiJub25lIn0." "eyJleHAiOjk5OTk5OTk5OTksImVtYWlsIjoiYUBiLmMifQ." "sig"
+ )
+ return credentials_from_bundle(
+ CodexAuthBundleV1(
+ tokens=CodexAuthTokensV1(
+ id_token=SecretStr(token),
+ access_token=SecretStr(token),
+ refresh_token=SecretStr("rt"),
+ ),
+ codex_runtime_version="http",
+ )
+ )
diff --git a/autogpt_platform/backend/backend/integrations/codex/launcher.py b/autogpt_platform/backend/backend/integrations/codex/launcher.py
deleted file mode 100644
index b62b69dc77..0000000000
--- a/autogpt_platform/backend/backend/integrations/codex/launcher.py
+++ /dev/null
@@ -1,76 +0,0 @@
-import os
-import subprocess
-import sys
-from collections.abc import Mapping
-
-_PASSTHROUGH = frozenset(
- {
- "APPDATA",
- "CODEX_HOME",
- "CODEX_SQLITE_HOME",
- "COMSPEC",
- "HOME",
- "HTTPS_PROXY",
- "HTTP_PROXY",
- "LANG",
- "LC_ALL",
- "LOCALAPPDATA",
- "NO_COLOR",
- "NO_PROXY",
- "NUMBER_OF_PROCESSORS",
- "OS",
- "PATH",
- "PATHEXT",
- "PROCESSOR_ARCHITECTURE",
- "PROGRAMDATA",
- "PROGRAMFILES",
- "PROGRAMFILES(X86)",
- "RUST_LOG",
- "SSL_CERT_DIR",
- "SSL_CERT_FILE",
- "SYSTEMROOT",
- "SYSTEMDRIVE",
- "TEMP",
- "TERM",
- "TMP",
- "TMPDIR",
- "USERPROFILE",
- "WINDIR",
- "XDG_CACHE_HOME",
- "XDG_CONFIG_HOME",
- "XDG_DATA_HOME",
- }
-)
-
-
-def sanitized_environment(source: Mapping[str, str] | None = None) -> dict[str, str]:
- ambient = os.environ if source is None else source
- child = {
- key: value for key, value in ambient.items() if key.upper() in _PASSTHROUGH
- }
- child.update(
- {
- "CODEX_ACCESS_TOKEN": "",
- "CODEX_API_KEY": "",
- "OPENAI_API_KEY": "",
- }
- )
- return child
-
-
-def main() -> None:
- if len(sys.argv) < 2:
- raise SystemExit("Codex launcher requires the runtime path")
- runtime, *arguments = sys.argv[1:]
- if os.name == "nt":
- completed = subprocess.run(
- [runtime, *arguments],
- env=sanitized_environment(),
- check=False,
- )
- raise SystemExit(completed.returncode)
- os.execve(runtime, [runtime, *arguments], sanitized_environment())
-
-
-if __name__ == "__main__":
- main()
diff --git a/autogpt_platform/backend/backend/integrations/codex/launcher_test.py b/autogpt_platform/backend/backend/integrations/codex/launcher_test.py
deleted file mode 100644
index 3a1a0e5c12..0000000000
--- a/autogpt_platform/backend/backend/integrations/codex/launcher_test.py
+++ /dev/null
@@ -1,31 +0,0 @@
-import os
-
-from backend.integrations.codex.launcher import sanitized_environment
-
-
-def test_launcher_drops_ambient_backend_secrets(monkeypatch):
- monkeypatch.setenv("DATABASE_URL", "postgresql://secret")
- monkeypatch.setenv("ENCRYPTION_KEY", "encryption-secret")
- monkeypatch.setenv("OPENAI_API_KEY", "provider-secret")
- monkeypatch.setenv("CODEX_HOME", "/tmp/codex-user")
- monkeypatch.setenv("APPDATA", "/tmp/codex-user")
- monkeypatch.setenv("PATH", os.environ.get("PATH", ""))
-
- child_env = sanitized_environment()
-
- assert child_env["CODEX_HOME"] == "/tmp/codex-user"
- assert child_env["APPDATA"] == "/tmp/codex-user"
- assert "PATH" in child_env
- assert "DATABASE_URL" not in child_env
- assert "ENCRYPTION_KEY" not in child_env
- assert child_env["OPENAI_API_KEY"] == ""
-
-
-def test_launcher_respects_an_explicit_empty_environment():
- child_env = sanitized_environment({})
-
- assert child_env == {
- "CODEX_ACCESS_TOKEN": "",
- "CODEX_API_KEY": "",
- "OPENAI_API_KEY": "",
- }
diff --git a/autogpt_platform/backend/backend/integrations/codex/login.py b/autogpt_platform/backend/backend/integrations/codex/login.py
index 5c277df4e7..6cf1d2c206 100644
--- a/autogpt_platform/backend/backend/integrations/codex/login.py
+++ b/autogpt_platform/backend/backend/integrations/codex/login.py
@@ -559,7 +559,7 @@ def _is_codex_credentials(
return (
isinstance(credentials, OAuth2Credentials)
and credentials.provider == "codex"
- and credentials.refresh_strategy == "provider_runtime"
+ and credentials.refresh_strategy in ("oauth_handler", "provider_runtime")
)
diff --git a/autogpt_platform/backend/backend/integrations/codex/login_test.py b/autogpt_platform/backend/backend/integrations/codex/login_test.py
index 285e8ace65..095f3f9dc7 100644
--- a/autogpt_platform/backend/backend/integrations/codex/login_test.py
+++ b/autogpt_platform/backend/backend/integrations/codex/login_test.py
@@ -652,7 +652,7 @@ def _credentials() -> OAuth2Credentials:
access_token=SecretStr("access-secret"),
refresh_token=SecretStr("refresh-secret"),
scopes=[],
- refresh_strategy="provider_runtime",
+ refresh_strategy="oauth_handler",
provider_state=SecretStr("provider-secret"),
provider_state_version=1,
)
diff --git a/autogpt_platform/backend/backend/integrations/codex/models.py b/autogpt_platform/backend/backend/integrations/codex/models.py
index 3a51754ada..44e1e78ba0 100644
--- a/autogpt_platform/backend/backend/integrations/codex/models.py
+++ b/autogpt_platform/backend/backend/integrations/codex/models.py
@@ -133,3 +133,17 @@ class CodexLoginCompletion(BaseModel):
bundle: CodexAuthBundleV1
account: CodexAccountSnapshot | None = None
rate_limits: CodexRateLimitsSnapshot | None = None
+
+
+class CodexStreamEvent(BaseModel):
+ """A streamed increment from a Codex turn.
+
+ Replaces the CLI SDK's ``Notification`` on the HTTP path so nothing outside
+ this package has to import ``openai_codex`` -- that package hard-depends on
+ the 391 MB bundled binary, with no extra to opt out of it.
+ """
+
+ model_config = ConfigDict(extra="forbid")
+
+ type: Literal["text_delta", "reasoning_delta"]
+ delta: str
diff --git a/autogpt_platform/backend/backend/integrations/codex/runtime.py b/autogpt_platform/backend/backend/integrations/codex/runtime.py
deleted file mode 100644
index 13684f1fba..0000000000
--- a/autogpt_platform/backend/backend/integrations/codex/runtime.py
+++ /dev/null
@@ -1,966 +0,0 @@
-import asyncio
-import concurrent.futures
-import os
-import sys
-import threading
-import time
-from collections.abc import Awaitable, Callable
-from contextlib import suppress
-from importlib.metadata import version
-from pathlib import Path
-from types import MethodType
-from typing import Any, cast
-
-from codex_cli_bin import bundled_codex_path
-from openai_codex import (
- ApprovalMode,
- AsyncCodex,
- AsyncDeviceCodeLoginHandle,
- AsyncThread,
- AsyncTurnHandle,
- CodexConfig,
- Sandbox,
- TurnResult,
-)
-from openai_codex._run import _collect_async_turn_result
-from openai_codex.generated.v2_all import (
- ChatgptAccount,
- GetAccountRateLimitsResponse,
- RateLimitWindow,
- ReasoningEffort,
- ReasoningThreadItem,
- ThreadStartResponse,
-)
-from openai_codex.models import Notification
-from openai_codex.types import JsonObject
-
-from backend.integrations.codex.models import (
- CodexAccountSnapshot,
- CodexDeviceCodeDetails,
- CodexDynamicToolCall,
- CodexDynamicToolResult,
- CodexDynamicToolSpec,
- CodexInvocationRequest,
- CodexInvocationResult,
- CodexModelInfo,
- CodexRateLimitsSnapshot,
- CodexRateLimitWindow,
- CodexReasoningEffort,
- CodexTokenUsage,
-)
-from backend.integrations.codex.temporary_home import TemporaryCodexHome
-
-OPENAI_CODEX_VERSION = "0.144.4"
-CODEX_RUNTIME_VERSION = "0.144.4"
-_RUNTIME_CLOSE_TIMEOUT_SECONDS = 3.0
-_SERVER_REQUEST_MAX_CONCURRENCY = 8
-
-
-class _ConcurrentServerRequestDispatcher:
- def __init__(self, sync_client: Any, *, max_concurrency: int) -> None:
- if max_concurrency < 1:
- raise ValueError("max_concurrency must be positive")
- self._sync_client = sync_client
- self._capacity = threading.BoundedSemaphore(max_concurrency)
- self._closing = threading.Event()
- self._workers: set[threading.Thread] = set()
- self._workers_lock = threading.Lock()
-
- def dispatch(self, message: dict[str, object]) -> None:
- if self._closing.is_set():
- self._write_error(message, "Codex tool bridge is shutting down")
- return
- if not self._capacity.acquire(blocking=False):
- self._write_error(message, "Codex tool bridge is busy")
- return
-
- worker = threading.Thread(
- target=self._answer,
- args=(message,),
- name="autogpt-codex-server-request",
- daemon=True,
- )
- rejection: str | None = None
- with self._workers_lock:
- if self._closing.is_set():
- self._capacity.release()
- rejection = "Codex tool bridge is shutting down"
- else:
- self._workers.add(worker)
- try:
- worker.start()
- except BaseException:
- self._workers.discard(worker)
- self._capacity.release()
- rejection = "Codex tool bridge request failed"
- if rejection is not None:
- self._write_error(message, rejection)
-
- def close(self, timeout_seconds: float) -> bool:
- self._closing.set()
- deadline = time.monotonic() + max(timeout_seconds, 0)
- current = threading.current_thread()
- while True:
- with self._workers_lock:
- workers = tuple(worker for worker in self._workers if worker != current)
- if not workers:
- return True
- remaining = deadline - time.monotonic()
- if remaining <= 0:
- return False
- workers[0].join(timeout=remaining)
-
- def _answer(self, message: dict[str, object]) -> None:
- try:
- response = self._sync_client._handle_server_request(message)
- self._sync_client._write_message({"id": message["id"], "result": response})
- except BaseException:
- self._write_error(message, "Codex tool bridge request failed")
- finally:
- with self._workers_lock:
- self._workers.discard(threading.current_thread())
- self._capacity.release()
-
- def _write_error(self, message: dict[str, object], detail: str) -> None:
- if getattr(self._sync_client, "_proc", None) is None:
- return
- with suppress(BaseException):
- self._sync_client._write_message(
- {
- "id": message.get("id"),
- "error": {
- "code": -32001,
- "message": detail,
- },
- }
- )
-
-
-class CodexRuntimeError(RuntimeError):
- pass
-
-
-class CodexRuntimeDeviceLogin:
- def __init__(self, handle: AsyncDeviceCodeLoginHandle) -> None:
- self._handle = handle
- self.details = CodexDeviceCodeDetails(
- login_id=handle.login_id,
- verification_url=handle.verification_url,
- user_code=handle.user_code,
- )
-
- async def wait(self) -> bool:
- completion = await self._handle.wait()
- return completion.success
-
- async def cancel(self) -> None:
- await self._handle.cancel()
-
-
-class CodexRuntime:
- def __init__(
- self,
- client: AsyncCodex,
- home: TemporaryCodexHome,
- *,
- close_timeout_seconds: float = _RUNTIME_CLOSE_TIMEOUT_SECONDS,
- ) -> None:
- self._client = client
- self._home = home
- self._close_timeout_seconds = close_timeout_seconds
- self._closed = False
- self._poisoned = False
- self._close_lock = asyncio.Lock()
- self._dynamic_tool_handlers: dict[
- str,
- tuple[
- Callable[[CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]],
- float,
- ],
- ] = {}
- self._dynamic_tool_futures: dict[
- str, set[concurrent.futures.Future[CodexDynamicToolResult]]
- ] = {}
- self._dynamic_tool_futures_lock = threading.Lock()
-
- @property
- def closed(self) -> bool:
- return self._closed or self._poisoned
-
- @classmethod
- async def start(cls, home: TemporaryCodexHome) -> "CodexRuntime":
- assert_pinned_versions()
- config = build_runtime_config(home)
- client = AsyncCodex(config)
- _install_concurrent_server_request_dispatcher(client)
- _install_fail_closed_approval_handler(client)
- startup = asyncio.create_task(client.__aenter__())
- try:
- await asyncio.shield(startup)
- except asyncio.CancelledError:
- with suppress(BaseException):
- await _close_codex_client(client, _RUNTIME_CLOSE_TIMEOUT_SECONDS)
- await _cancel_task_bounded(startup, _RUNTIME_CLOSE_TIMEOUT_SECONDS)
- raise
- except BaseException:
- with suppress(BaseException):
- await _close_codex_client(client, _RUNTIME_CLOSE_TIMEOUT_SECONDS)
- raise
- return cls(client, home)
-
- async def start_device_code_login(self) -> CodexRuntimeDeviceLogin:
- handle = await self._client.login_chatgpt_device_code()
- return CodexRuntimeDeviceLogin(handle)
-
- async def account(
- self,
- *,
- refresh_token: bool = True,
- ) -> CodexAccountSnapshot:
- response = await self._client.account(refresh_token=refresh_token)
- if response.account is None:
- return CodexAccountSnapshot(
- connected=False,
- requires_openai_auth=response.requires_openai_auth,
- )
- account = response.account.root
- if account.type != "chatgpt":
- return CodexAccountSnapshot(
- connected=True,
- requires_openai_auth=response.requires_openai_auth,
- account_type=account.type,
- )
- chatgpt = cast(ChatgptAccount, account)
- return CodexAccountSnapshot(
- connected=True,
- requires_openai_auth=response.requires_openai_auth,
- account_type=chatgpt.type,
- email=chatgpt.email,
- plan_type=chatgpt.plan_type.value,
- )
-
- async def rate_limits(self) -> CodexRateLimitsSnapshot:
- response = await self._client._client.request(
- "account/rateLimits/read",
- None,
- response_model=GetAccountRateLimitsResponse,
- )
- limits = response.rate_limits
- credits = limits.credits
- return CodexRateLimitsSnapshot(
- plan_type=limits.plan_type.value if limits.plan_type else None,
- limit_id=limits.limit_id,
- limit_name=limits.limit_name,
- rate_limit_reached_type=(
- limits.rate_limit_reached_type.value
- if limits.rate_limit_reached_type
- else None
- ),
- primary=_rate_limit_window(limits.primary),
- secondary=_rate_limit_window(limits.secondary),
- has_credits=credits.has_credits if credits else None,
- unlimited_credits=credits.unlimited if credits else None,
- bucket_ids=sorted((response.rate_limits_by_limit_id or {}).keys()),
- )
-
- async def models(self) -> list[CodexModelInfo]:
- response = await self._client.models(include_hidden=True)
- return [
- CodexModelInfo(
- model=model.model,
- display_name=model.display_name,
- is_default=model.is_default,
- hidden=model.hidden,
- default_reasoning_effort=cast(
- CodexReasoningEffort,
- model.default_reasoning_effort.value,
- ),
- supported_reasoning_efforts=[
- cast(CodexReasoningEffort, option.reasoning_effort.value)
- for option in model.supported_reasoning_efforts
- ],
- input_modalities=[
- modality.value for modality in (model.input_modalities or [])
- ],
- )
- for model in response.data
- ]
-
- async def invoke(
- self,
- request: CodexInvocationRequest,
- ) -> CodexInvocationResult:
- thread = await self._start_toolless_thread(request)
- effort = ReasoningEffort(request.effort) if request.effort else None
- schema = cast(JsonObject | None, request.output_schema)
- turn_start = asyncio.create_task(
- thread.turn(
- request.prompt,
- approval_mode=ApprovalMode.deny_all,
- effort=effort,
- model=request.model,
- output_schema=schema,
- sandbox=Sandbox.read_only,
- )
- )
- try:
- turn = await asyncio.shield(turn_start)
- except asyncio.CancelledError:
- with suppress(BaseException):
- await asyncio.shield(self.close())
- await _cancel_task_bounded(
- turn_start,
- self._close_timeout_seconds,
- )
- raise
- try:
- result = await turn.run()
- except asyncio.CancelledError:
- with suppress(BaseException):
- await asyncio.wait_for(turn.interrupt(), timeout=2)
- if not self._closed:
- with suppress(BaseException):
- await asyncio.shield(self.close())
- raise
- if result.final_response is None:
- raise CodexRuntimeError("Codex turn completed without a final response")
- return CodexInvocationResult(
- response_id=result.id,
- final_response=result.final_response,
- reasoning_summary=_reasoning_summary(result),
- status=result.status.value,
- duration_ms=result.duration_ms,
- usage=_token_usage(result),
- )
-
- async def invoke_agent(
- self,
- request: CodexInvocationRequest,
- dynamic_tools: list[CodexDynamicToolSpec],
- tool_handler: Callable[
- [CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]
- ],
- event_handler: Callable[[Notification], Awaitable[None]] | None = None,
- *,
- tool_timeout_seconds: float,
- ) -> CodexInvocationResult:
- thread = await self._start_thread(request, dynamic_tools)
- self._register_dynamic_tool_handler(
- thread.id,
- tool_handler,
- timeout_seconds=tool_timeout_seconds,
- )
- try:
- effort = ReasoningEffort(request.effort) if request.effort else None
- turn_start = asyncio.create_task(
- thread.turn(
- request.prompt,
- approval_mode=ApprovalMode.deny_all,
- effort=effort,
- model=request.model,
- sandbox=Sandbox.read_only,
- )
- )
- try:
- turn = await asyncio.shield(turn_start)
- except asyncio.CancelledError:
- await self._stop_cancelled_turn_start(turn_start)
- raise
- stream = turn.stream()
- abnormal_exit = False
-
- async def observed_stream():
- async for event in stream:
- if event_handler is not None:
- await event_handler(event)
- yield event
-
- try:
- result = await _collect_async_turn_result(
- observed_stream(),
- turn_id=turn.id,
- )
- except BaseException:
- abnormal_exit = True
- self._cancel_dynamic_tool_futures(thread.id)
- await self._interrupt_turn_or_close(turn)
- raise
- finally:
- try:
- await self._close_agent_stream(stream)
- except BaseException:
- if not abnormal_exit:
- raise
- await self._poison_runtime()
- finally:
- self._unregister_dynamic_tool_handler(thread.id)
- if result.final_response is None:
- raise CodexRuntimeError("Codex turn completed without a final response")
- return CodexInvocationResult(
- response_id=result.id,
- final_response=result.final_response,
- reasoning_summary=_reasoning_summary(result),
- status=result.status.value,
- duration_ms=result.duration_ms,
- usage=_token_usage(result),
- )
-
- async def logout(self) -> None:
- await self._client.logout()
-
- async def close(self) -> None:
- async with self._close_lock:
- if self._closed:
- return
- self._closed = True
- self._cancel_dynamic_tool_futures()
- await _close_codex_client(self._client, self._close_timeout_seconds)
-
- async def _start_toolless_thread(
- self,
- request: CodexInvocationRequest,
- ) -> AsyncThread:
- return await self._start_thread(request, [])
-
- async def _start_thread(
- self,
- request: CodexInvocationRequest,
- dynamic_tools: list[CodexDynamicToolSpec],
- ) -> AsyncThread:
- params: JsonObject = {
- "approvalPolicy": "never",
- "approvalsReviewer": "user",
- "config": {
- "features": {
- "apps": False,
- "image_generation": False,
- "multi_agent": False,
- "multi_agent_v2": False,
- "plugins": False,
- "shell_tool": False,
- "tool_suggest": False,
- },
- "web_search": "disabled",
- },
- "cwd": str(self._home.workspace_path),
- "dynamicTools": [
- {
- "type": "function",
- "name": tool.name,
- "description": tool.description,
- "inputSchema": cast(JsonObject, tool.input_schema),
- }
- for tool in dynamic_tools
- ],
- "ephemeral": True,
- "environments": [],
- "runtimeWorkspaceRoots": [],
- "sandbox": "read-only",
- "selectedCapabilityRoots": [],
- }
- if request.instructions is not None:
- params["developerInstructions"] = request.instructions
- if request.model is not None:
- params["model"] = request.model
- started = await self._client._client.request(
- "thread/start",
- params,
- response_model=ThreadStartResponse,
- )
- return AsyncThread(self._client, started.thread.id)
-
- def _register_dynamic_tool_handler(
- self,
- thread_id: str,
- handler: Callable[[CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]],
- *,
- timeout_seconds: float,
- ) -> None:
- self._ensure_dynamic_tool_dispatcher()
- with self._dynamic_tool_futures_lock:
- self._dynamic_tool_handlers[thread_id] = (handler, timeout_seconds)
- self._dynamic_tool_futures.setdefault(thread_id, set())
-
- def _unregister_dynamic_tool_handler(self, thread_id: str) -> None:
- with self._dynamic_tool_futures_lock:
- self._dynamic_tool_handlers.pop(thread_id, None)
- futures = tuple(self._dynamic_tool_futures.pop(thread_id, ()))
- for future in futures:
- future.cancel()
-
- def _ensure_dynamic_tool_dispatcher(self) -> None:
- loop = asyncio.get_running_loop()
-
- def handle(method: str, params: JsonObject | None) -> JsonObject:
- if method != "item/tool/call":
- return _deny_server_request(method, params)
- try:
- call = CodexDynamicToolCall.model_validate(
- {
- "thread_id": (params or {}).get("threadId"),
- "turn_id": (params or {}).get("turnId"),
- "call_id": (params or {}).get("callId"),
- "namespace": (params or {}).get("namespace"),
- "tool": (params or {}).get("tool"),
- "arguments": (params or {}).get("arguments"),
- }
- )
- except Exception:
- return _dynamic_tool_error("codex_tool_request_invalid")
-
- with self._dynamic_tool_futures_lock:
- registration = self._dynamic_tool_handlers.get(call.thread_id)
- futures = self._dynamic_tool_futures.get(call.thread_id)
- if self.closed or registration is None or futures is None:
- return _dynamic_tool_error("codex_tool_handler_unavailable")
- handler, timeout_seconds = registration
-
- async def execute_handler() -> CodexDynamicToolResult:
- return await handler(call)
-
- coroutine = execute_handler()
- try:
- future = asyncio.run_coroutine_threadsafe(coroutine, loop)
- except RuntimeError:
- coroutine.close()
- return _dynamic_tool_error("codex_tool_execution_failed")
- except BaseException:
- coroutine.close()
- raise
- futures.add(future)
- try:
- result = future.result(timeout=timeout_seconds)
- except concurrent.futures.TimeoutError:
- future.cancel()
- return _dynamic_tool_error("codex_tool_execution_timeout")
- except BaseException:
- return _dynamic_tool_error("codex_tool_execution_failed")
- finally:
- with self._dynamic_tool_futures_lock:
- futures = self._dynamic_tool_futures.get(call.thread_id)
- if futures is not None:
- futures.discard(future)
- return {
- "contentItems": [{"type": "inputText", "text": result.content}],
- "success": result.success,
- }
-
- try:
- sync_client = self._client._client._sync
- except AttributeError as exc:
- raise CodexRuntimeError(
- "Pinned Codex SDK approval-handler layout changed"
- ) from exc
- if getattr(sync_client, "_autogpt_dynamic_tool_dispatcher", False):
- return
- sync_client._approval_handler = handle
- setattr(sync_client, "_autogpt_dynamic_tool_dispatcher", True)
-
- def _cancel_dynamic_tool_futures(self, thread_id: str | None = None) -> None:
- with self._dynamic_tool_futures_lock:
- if thread_id is None:
- futures = tuple(
- future
- for group in self._dynamic_tool_futures.values()
- for future in group
- )
- else:
- futures = tuple(self._dynamic_tool_futures.get(thread_id, ()))
- for future in futures:
- future.cancel()
-
- async def _close_agent_stream(self, stream) -> None:
- closing = asyncio.create_task(stream.aclose())
- try:
- done, _ = await asyncio.wait(
- {closing},
- timeout=self._close_timeout_seconds,
- )
- except asyncio.CancelledError:
- closing.cancel()
- _detach_task(closing)
- raise
- if closing in done:
- try:
- await closing
- except BaseException:
- with suppress(BaseException):
- await self.close()
- raise
- return
- closing.cancel()
- _detach_task(closing)
- with suppress(BaseException):
- await self.close()
- raise CodexRuntimeError("codex_stream_shutdown_timeout")
-
- async def _stop_cancelled_turn_start(
- self,
- turn_start: asyncio.Task[AsyncTurnHandle],
- ) -> None:
- try:
- done, _ = await asyncio.wait(
- {turn_start},
- timeout=self._close_timeout_seconds,
- )
- except BaseException:
- await self._poison_runtime()
- return
- if turn_start not in done:
- await self._poison_runtime()
- await _cancel_task_bounded(
- cast(asyncio.Task[object], turn_start),
- self._close_timeout_seconds,
- )
- return
- try:
- turn = turn_start.result()
- except BaseException:
- await self._poison_runtime()
- return
- await self._interrupt_turn_or_close(turn)
-
- async def _interrupt_turn_or_close(self, turn: AsyncTurnHandle) -> bool:
- interrupting = asyncio.create_task(turn.interrupt())
- try:
- done, _ = await asyncio.wait(
- {interrupting},
- timeout=min(2.0, self._close_timeout_seconds),
- )
- except BaseException:
- interrupting.cancel()
- _detach_task(cast(asyncio.Task[object], interrupting))
- await self._poison_runtime()
- return False
- if interrupting in done:
- try:
- interrupting.result()
- except BaseException:
- await self._poison_runtime()
- return False
- return True
- await _cancel_task_bounded(
- cast(asyncio.Task[object], interrupting),
- self._close_timeout_seconds,
- )
- await self._poison_runtime()
- return False
-
- async def _poison_runtime(self) -> None:
- self._poisoned = True
- closing = asyncio.create_task(self.close())
- try:
- done, _ = await asyncio.wait(
- {closing},
- timeout=self._close_timeout_seconds,
- )
- except BaseException:
- _detach_task(cast(asyncio.Task[object], closing))
- return
- if closing not in done:
- _detach_task(cast(asyncio.Task[object], closing))
- return
- with suppress(BaseException):
- closing.result()
-
-
-def _install_concurrent_server_request_dispatcher(client: AsyncCodex) -> None:
- try:
- sync_client = client._client._sync
- _ = (
- sync_client._read_message,
- sync_client._write_message,
- sync_client._handle_server_request,
- sync_client._coerce_notification,
- sync_client._router,
- )
- except AttributeError as exc:
- raise CodexRuntimeError("Pinned Codex SDK reader layout changed") from exc
-
- if getattr(sync_client, "_autogpt_server_request_dispatcher", None) is not None:
- return
- dispatcher = _ConcurrentServerRequestDispatcher(
- sync_client,
- max_concurrency=_SERVER_REQUEST_MAX_CONCURRENCY,
- )
- setattr(sync_client, "_autogpt_server_request_dispatcher", dispatcher)
-
- def reader_loop(instance: Any) -> None:
- try:
- while True:
- message = instance._read_message()
- if "method" in message and "id" in message:
- dispatcher.dispatch(message)
- continue
- if "method" in message and "id" not in message:
- method = message["method"]
- if isinstance(method, str):
- instance._router.route_notification(
- instance._coerce_notification(
- method,
- message.get("params"),
- )
- )
- continue
- instance._router.route_response(message)
- except BaseException as exc:
- instance._router.fail_all(exc)
-
- sync_client._reader_loop = MethodType(reader_loop, sync_client)
-
-
-def _shutdown_concurrent_server_request_dispatcher(
- client: AsyncCodex,
- timeout_seconds: float,
-) -> bool:
- try:
- dispatcher = getattr(
- client._client._sync,
- "_autogpt_server_request_dispatcher",
- )
- except AttributeError:
- return True
- return dispatcher.close(timeout_seconds)
-
-
-async def _close_codex_client(
- client: AsyncCodex,
- timeout_seconds: float,
-) -> None:
- process = _runtime_process(client)
- deadline = time.monotonic() + max(timeout_seconds, 0)
- closing: asyncio.Task[None] | None = None
- try:
- await asyncio.to_thread(
- _shutdown_concurrent_server_request_dispatcher,
- client,
- timeout_seconds,
- )
- closing = asyncio.create_task(client.close())
- remaining = max(deadline - time.monotonic(), 0)
- if remaining == 0:
- await asyncio.sleep(0)
- done = {closing} if closing.done() else set()
- else:
- done, _ = await asyncio.wait({closing}, timeout=remaining)
- if closing in done:
- await closing
- return
- closing.cancel()
- _detach_task(closing)
- _force_stop_process(process)
- except asyncio.CancelledError:
- if closing is not None:
- closing.cancel()
- _detach_task(closing)
- _force_stop_process(process)
- raise
- except BaseException:
- _force_stop_process(process)
- raise
-
-
-def _runtime_process(client: AsyncCodex) -> object | None:
- try:
- return client._client._sync._proc
- except AttributeError:
- return None
-
-
-def _force_stop_process(process: object | None) -> None:
- if process is None:
- return
- with suppress(BaseException):
- stdin = getattr(process, "stdin", None)
- if stdin is not None:
- stdin.close()
- with suppress(BaseException):
- process.terminate() # type: ignore[attr-defined]
- with suppress(BaseException):
- if process.poll() is None: # type: ignore[attr-defined]
- process.kill() # type: ignore[attr-defined]
-
-
-def _detach_task(task: asyncio.Task[object]) -> None:
- def consume_result(completed: asyncio.Task[object]) -> None:
- with suppress(BaseException):
- completed.result()
-
- task.add_done_callback(consume_result)
-
-
-async def _cancel_task_bounded(
- task: asyncio.Task[object],
- timeout_seconds: float,
-) -> None:
- task.cancel()
- done, _ = await asyncio.wait({task}, timeout=timeout_seconds)
- if task not in done:
- _detach_task(task)
- return
- with suppress(BaseException):
- task.result()
-
-
-def build_runtime_config(home: TemporaryCodexHome) -> CodexConfig:
- launcher_path = str(Path(__file__).with_name("launcher.py").resolve())
- launch_args = (
- sys.executable,
- launcher_path,
- str(bundled_codex_path()),
- "--config",
- 'cli_auth_credentials_store="file"',
- "--config",
- 'forced_login_method="chatgpt"',
- "--config",
- "allow_login_shell=false",
- "--config",
- 'web_search="disabled"',
- "--config",
- "features.shell_tool=false",
- "--config",
- "features.multi_agent=false",
- "--config",
- "features.multi_agent_v2=false",
- "--config",
- "features.apps=false",
- "--config",
- "features.plugins=false",
- "--config",
- "features.tool_suggest=false",
- "--config",
- "features.image_generation=false",
- "--config",
- "features.in_app_browser=false",
- "--config",
- "features.browser_use=false",
- "--config",
- "features.computer_use=false",
- "--config",
- "features.memories=false",
- "--config",
- "features.goals=false",
- "--config",
- "features.workspace_dependencies=false",
- "--config",
- "include_apps_instructions=false",
- "--config",
- "include_collaboration_mode_instructions=false",
- "--config",
- "include_environment_context=false",
- "--config",
- "include_permissions_instructions=false",
- "app-server",
- "--listen",
- "stdio://",
- )
- return CodexConfig(
- launch_args_override=launch_args,
- cwd=str(home.workspace_path),
- env=_runtime_environment(home),
- client_name="autogpt_platform",
- client_title="AutoGPT Platform",
- client_version="0.6.22",
- experimental_api=True,
- )
-
-
-def assert_pinned_versions() -> None:
- sdk_version = version("openai-codex")
- runtime_version = version("openai-codex-cli-bin")
- if sdk_version != OPENAI_CODEX_VERSION or runtime_version != CODEX_RUNTIME_VERSION:
- raise CodexRuntimeError(
- "Codex SDK and runtime must both be pinned to version 0.144.4"
- )
-
-
-def _install_fail_closed_approval_handler(client: AsyncCodex) -> None:
- try:
- sync_client = client._client._sync
- except AttributeError as exc:
- raise CodexRuntimeError(
- "Pinned Codex SDK approval-handler layout changed"
- ) from exc
- sync_client._approval_handler = _deny_server_request
-
-
-def _deny_server_request(
- method: str,
- _params: JsonObject | None,
-) -> JsonObject:
- if method in {
- "item/commandExecution/requestApproval",
- "item/fileChange/requestApproval",
- }:
- return {"decision": "decline"}
- if method in {"applyPatchApproval", "execCommandApproval"}:
- return {"decision": "denied"}
- if method == "item/permissions/requestApproval":
- return {"permissions": {}, "scope": "turn"}
- raise CodexRuntimeError(f"Unsupported Codex server request: {method}")
-
-
-def _dynamic_tool_error(code: str) -> JsonObject:
- return {
- "contentItems": [{"type": "inputText", "text": code}],
- "success": False,
- }
-
-
-def _runtime_environment(home: TemporaryCodexHome) -> dict[str, str]:
- home_path = str(home.path)
- temp_path = str(home.temp_path)
- environment = {
- "APPDATA": home_path,
- "CODEX_HOME": home_path,
- "CODEX_SQLITE_HOME": home_path,
- "HOME": home_path,
- "LOCALAPPDATA": home_path,
- "RUST_LOG": "warn",
- "TEMP": temp_path,
- "TMP": temp_path,
- "TMPDIR": temp_path,
- "USERPROFILE": home_path,
- "XDG_CACHE_HOME": str(Path(home_path) / "cache"),
- "XDG_CONFIG_HOME": str(Path(home_path) / "config"),
- "XDG_DATA_HOME": str(Path(home_path) / "data"),
- }
- return environment | _required_windows_environment()
-
-
-def _required_windows_environment() -> dict[str, str]:
- names = ("COMSPEC", "PATH", "PATHEXT", "SYSTEMROOT", "WINDIR")
- return {name: os.environ[name] for name in names if name in os.environ}
-
-
-def _rate_limit_window(window: RateLimitWindow | None) -> CodexRateLimitWindow | None:
- if window is None:
- return None
- return CodexRateLimitWindow(
- used_percent=window.used_percent,
- window_duration_mins=window.window_duration_mins,
- resets_at=window.resets_at,
- )
-
-
-def _token_usage(result: TurnResult) -> CodexTokenUsage | None:
- if result.usage is None:
- return None
- total = result.usage.total
- return CodexTokenUsage(
- input_tokens=total.input_tokens,
- cached_input_tokens=total.cached_input_tokens,
- output_tokens=total.output_tokens,
- reasoning_output_tokens=total.reasoning_output_tokens,
- total_tokens=total.total_tokens,
- )
-
-
-def _reasoning_summary(result: TurnResult) -> str | None:
- summaries = []
- for item in result.items:
- if item.root.type != "reasoning":
- continue
- reasoning = cast(ReasoningThreadItem, item.root)
- summaries.extend(reasoning.summary or [])
- return "\n".join(summaries) or None
diff --git a/autogpt_platform/backend/backend/integrations/codex/runtime_test.py b/autogpt_platform/backend/backend/integrations/codex/runtime_test.py
deleted file mode 100644
index 3fdd5500e0..0000000000
--- a/autogpt_platform/backend/backend/integrations/codex/runtime_test.py
+++ /dev/null
@@ -1,1103 +0,0 @@
-import asyncio
-import concurrent.futures
-import importlib.metadata
-import os
-import subprocess
-import sys
-import threading
-from pathlib import Path
-from types import SimpleNamespace
-from typing import cast
-from unittest.mock import AsyncMock
-
-import pytest
-from codex_cli_bin import bundled_codex_path
-from openai_codex import AsyncCodex
-
-import backend.integrations.codex.runtime as runtime_module
-from backend.integrations.codex.models import (
- CodexDynamicToolResult,
- CodexDynamicToolSpec,
- CodexInvocationRequest,
- CodexModelInfo,
-)
-from backend.integrations.codex.runtime import (
- CODEX_RUNTIME_VERSION,
- OPENAI_CODEX_VERSION,
- CodexRuntime,
- CodexRuntimeError,
- _deny_server_request,
- _install_concurrent_server_request_dispatcher,
- _install_fail_closed_approval_handler,
- assert_pinned_versions,
- build_runtime_config,
-)
-from backend.integrations.codex.temporary_home import TemporaryCodexHome
-
-
-def test_sdk_and_runtime_versions_are_an_atomic_pin():
- assert importlib.metadata.version("openai-codex") == OPENAI_CODEX_VERSION
- assert importlib.metadata.version("openai-codex-cli-bin") == CODEX_RUNTIME_VERSION
- assert_pinned_versions()
-
-
-def test_runtime_config_uses_sanitizing_launcher_and_isolated_home(tmp_path):
- with TemporaryCodexHome.create(tmp_path) as home:
- config = build_runtime_config(home)
-
- arguments = config.launch_args_override
- assert arguments is not None
- assert arguments[0] == sys.executable
- assert (
- Path(arguments[1]).resolve()
- == Path(__file__).with_name("launcher.py").resolve()
- )
- assert arguments[2] == str(bundled_codex_path())
- assert arguments[-3:] == ("app-server", "--listen", "stdio://")
- assert 'cli_auth_credentials_store="file"' in arguments
- assert 'forced_login_method="chatgpt"' in arguments
- assert config.experimental_api is True
- assert config.env is not None
- for variable in (
- "APPDATA",
- "CODEX_HOME",
- "CODEX_SQLITE_HOME",
- "HOME",
- "LOCALAPPDATA",
- "TEMP",
- "TMP",
- "TMPDIR",
- "USERPROFILE",
- "XDG_CACHE_HOME",
- "XDG_CONFIG_HOME",
- "XDG_DATA_HOME",
- ):
- assert config.env[variable].startswith(str(home.path))
- assert config.env["TEMP"] == str(home.temp_path)
- assert config.env["TMP"] == str(home.temp_path)
- assert config.env["TMPDIR"] == str(home.temp_path)
- assert config.env["RUST_LOG"] == "warn"
- assert "DATABASE_URL" not in config.env
- assert "ENCRYPTION_KEY" not in config.env
- if os.name == "nt":
- assert config.env["SYSTEMROOT"] == os.environ["SYSTEMROOT"]
-
-
-def test_sanitizing_launcher_starts_from_isolated_working_directory(tmp_path):
- with TemporaryCodexHome.create(tmp_path) as home:
- config = build_runtime_config(home)
- arguments = config.launch_args_override
- assert arguments is not None
- result = subprocess.run(
- [*arguments[:-3], "--version"],
- cwd=home.workspace_path,
- env=config.env,
- capture_output=True,
- text=True,
- timeout=15,
- check=False,
- )
-
- assert result.returncode == 0, result.stderr
- assert CODEX_RUNTIME_VERSION in result.stdout
-
-
-def test_runtime_installs_protocol_exact_fail_closed_approval_handler(tmp_path):
- with TemporaryCodexHome.create(tmp_path) as home:
- client = AsyncCodex(build_runtime_config(home))
- _install_fail_closed_approval_handler(client)
- handler = client._client._sync._approval_handler
-
- assert handler("item/commandExecution/requestApproval", {}) == {
- "decision": "decline"
- }
- assert handler("item/fileChange/requestApproval", {}) == {"decision": "decline"}
- assert handler("applyPatchApproval", {}) == {"decision": "denied"}
- assert handler("execCommandApproval", {}) == {"decision": "denied"}
- assert handler("item/permissions/requestApproval", {}) == {
- "permissions": {},
- "scope": "turn",
- }
-
-
-def test_approval_handler_fails_closed_for_unknown_server_requests():
- with pytest.raises(CodexRuntimeError, match="Unsupported Codex server request"):
- _deny_server_request("item/future/requestApproval", {})
-
-
-async def test_model_discovery_preserves_account_catalog_metadata(tmp_path):
- client = SimpleNamespace(
- models=AsyncMock(
- return_value=SimpleNamespace(
- data=[
- SimpleNamespace(
- model="gpt-5.6-sol",
- display_name="GPT-5.6 Sol",
- is_default=True,
- hidden=False,
- default_reasoning_effort=SimpleNamespace(value="high"),
- supported_reasoning_efforts=[
- SimpleNamespace(
- reasoning_effort=SimpleNamespace(value="low")
- ),
- SimpleNamespace(
- reasoning_effort=SimpleNamespace(value="ultra")
- ),
- ],
- input_modalities=[
- SimpleNamespace(value="text"),
- SimpleNamespace(value="image"),
- ],
- )
- ]
- )
- )
- )
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime(client, home) # type: ignore[arg-type]
- models = await runtime.models()
-
- assert models == [
- CodexModelInfo(
- model="gpt-5.6-sol",
- display_name="GPT-5.6 Sol",
- is_default=True,
- hidden=False,
- default_reasoning_effort="high",
- supported_reasoning_efforts=["low", "ultra"],
- input_modalities=["text", "image"],
- )
- ]
- client.models.assert_awaited_once_with(include_hidden=True)
-
-
-async def test_repeated_app_server_start_close_leaves_no_temporary_home(tmp_path):
- created_paths: list[Path] = []
- for _attempt in range(2):
- home = TemporaryCodexHome.create(tmp_path)
- created_paths.append(home.path)
- runtime: CodexRuntime | None = None
- try:
- runtime = await asyncio.wait_for(CodexRuntime.start(home), timeout=20)
- handler = runtime._client._client._sync._approval_handler
- assert handler("item/commandExecution/requestApproval", {}) == {
- "decision": "decline"
- }
- finally:
- if runtime is not None:
- await asyncio.wait_for(runtime.close(), timeout=10)
- home.cleanup()
-
- assert all(not path.exists() for path in created_paths)
-
-
-async def test_thread_start_explicitly_removes_all_runtime_environments(tmp_path):
- request_call = AsyncMock(
- return_value=SimpleNamespace(thread=SimpleNamespace(id="thread-1"))
- )
- client = SimpleNamespace(_client=SimpleNamespace(request=request_call))
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime(client, home) # type: ignore[arg-type]
- await runtime._start_toolless_thread(
- CodexInvocationRequest(
- prompt="Do not access the host",
- instructions="Return text only",
- model="gpt-test",
- )
- )
-
- method, params = request_call.await_args.args[:2]
- assert method == "thread/start"
- assert params["environments"] == []
- assert params["dynamicTools"] == []
- assert params["runtimeWorkspaceRoots"] == []
- assert params["selectedCapabilityRoots"] == []
- assert params["sandbox"] == "read-only"
- assert params["cwd"] == str(home.workspace_path)
- assert params["config"]["web_search"] == "disabled"
- assert all(
- enabled is False for enabled in params["config"]["features"].values()
- )
-
-
-async def test_thread_start_exposes_only_supplied_dynamic_tools(tmp_path):
- request_call = AsyncMock(
- return_value=SimpleNamespace(thread=SimpleNamespace(id="thread-1"))
- )
- client = SimpleNamespace(_client=SimpleNamespace(request=request_call))
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime(client, home) # type: ignore[arg-type]
- await runtime._start_thread(
- CodexInvocationRequest(prompt="List my agents"),
- [
- CodexDynamicToolSpec(
- name="find_agent",
- description="Find an AutoGPT agent",
- input_schema={"type": "object", "properties": {}},
- )
- ],
- )
-
- params = request_call.await_args.args[1]
- assert params["dynamicTools"] == [
- {
- "type": "function",
- "name": "find_agent",
- "description": "Find an AutoGPT agent",
- "inputSchema": {"type": "object", "properties": {}},
- }
- ]
-
-
-async def test_dynamic_tool_request_round_trips_on_copilot_loop(tmp_path):
- sync_client = SimpleNamespace(_approval_handler=None)
- client = SimpleNamespace(_client=SimpleNamespace(_sync=sync_client))
- observed = []
-
- async def execute(call):
- observed.append(call)
- return CodexDynamicToolResult(content='{"agents": []}', success=True)
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime(client, home) # type: ignore[arg-type]
- runtime._register_dynamic_tool_handler("thread-1", execute, timeout_seconds=1)
- response = await asyncio.to_thread(
- sync_client._approval_handler,
- "item/tool/call",
- {
- "threadId": "thread-1",
- "turnId": "turn-1",
- "callId": "call-1",
- "namespace": "autogpt",
- "tool": "find_agent",
- "arguments": {"query": "demo"},
- },
- )
-
- assert observed[0].call_id == "call-1"
- assert observed[0].tool == "find_agent"
- assert observed[0].arguments == {"query": "demo"}
- assert response == {
- "contentItems": [{"type": "inputText", "text": '{"agents": []}'}],
- "success": True,
- }
-
-
-async def test_dynamic_tool_timeout_cancels_callback_and_fails_closed(tmp_path):
- sync_client = SimpleNamespace(_approval_handler=None)
- client = SimpleNamespace(_client=SimpleNamespace(_sync=sync_client))
- callback_cancelled = asyncio.Event()
-
- async def execute(_call):
- try:
- await asyncio.Event().wait()
- finally:
- callback_cancelled.set()
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime(client, home) # type: ignore[arg-type]
- runtime._register_dynamic_tool_handler(
- "thread-1", execute, timeout_seconds=0.01
- )
- response = await asyncio.to_thread(
- sync_client._approval_handler,
- "item/tool/call",
- {
- "threadId": "thread-1",
- "turnId": "turn-1",
- "callId": "call-1",
- "tool": "find_agent",
- "arguments": {},
- },
- )
- await asyncio.wait_for(callback_cancelled.wait(), timeout=1)
-
- assert response == {
- "contentItems": [{"type": "inputText", "text": "codex_tool_execution_timeout"}],
- "success": False,
- }
-
-
-async def test_dynamic_tool_submission_failure_closes_callback_coroutine(
- tmp_path, monkeypatch
-):
- sync_client = SimpleNamespace(_approval_handler=None)
- client = SimpleNamespace(_client=SimpleNamespace(_sync=sync_client))
- submitted: list[object] = []
-
- async def execute(_call):
- return CodexDynamicToolResult(content="unused", success=True)
-
- def reject_submission(coroutine, _loop):
- submitted.append(coroutine)
- raise RuntimeError("event loop is closed")
-
- monkeypatch.setattr(
- runtime_module.asyncio, "run_coroutine_threadsafe", reject_submission
- )
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime(cast(AsyncCodex, client), home)
- runtime._register_dynamic_tool_handler("thread-1", execute, timeout_seconds=1)
- response = await asyncio.to_thread(
- sync_client._approval_handler,
- "item/tool/call",
- {
- "threadId": "thread-1",
- "turnId": "turn-1",
- "callId": "call-1",
- "tool": "find_agent",
- "arguments": {},
- },
- )
-
- assert len(submitted) == 1
- assert getattr(submitted[0], "cr_frame") is None
- assert response == {
- "contentItems": [{"type": "inputText", "text": "codex_tool_execution_failed"}],
- "success": False,
- }
-
-
-async def test_unregister_cancels_tool_reserved_during_dispatch(tmp_path, monkeypatch):
- class ObservedLock:
- def __init__(self) -> None:
- self._lock = threading.Lock()
- self.owner_ident: int | None = None
- self.unregister_attempted = threading.Event()
- self.unregister_acquired = threading.Event()
-
- def __enter__(self):
- is_unregister = threading.current_thread().name == "codex-unregister"
- if is_unregister:
- self.unregister_attempted.set()
- self._lock.acquire()
- self.owner_ident = threading.get_ident()
- if is_unregister:
- self.unregister_acquired.set()
- return self
-
- def __exit__(self, _exc_type, _exc_value, _traceback) -> None:
- self.owner_ident = None
- self._lock.release()
-
- observed_lock = ObservedLock()
- sync_client = SimpleNamespace(_approval_handler=None)
- client = SimpleNamespace(_client=SimpleNamespace(_sync=sync_client))
- scheduler_entered = threading.Event()
- allow_scheduler_return = threading.Event()
- scheduler_held_lock: list[bool] = []
- future: concurrent.futures.Future[CodexDynamicToolResult] = (
- concurrent.futures.Future()
- )
-
- async def execute(_call):
- return CodexDynamicToolResult(content="unused", success=True)
-
- def schedule(coroutine, _loop):
- coroutine.close()
- scheduler_held_lock.append(observed_lock.owner_ident == threading.get_ident())
- scheduler_entered.set()
- assert allow_scheduler_return.wait(timeout=1)
- return future
-
- monkeypatch.setattr(runtime_module.asyncio, "run_coroutine_threadsafe", schedule)
-
- with TemporaryCodexHome.create(tmp_path) as home:
- with monkeypatch.context() as construction_patch:
- construction_patch.setattr(
- runtime_module.threading, "Lock", lambda: observed_lock
- )
- runtime = CodexRuntime(cast(AsyncCodex, client), home)
- runtime._register_dynamic_tool_handler("thread-1", execute, timeout_seconds=1)
- dispatch = asyncio.create_task(
- asyncio.to_thread(
- sync_client._approval_handler,
- "item/tool/call",
- {
- "threadId": "thread-1",
- "turnId": "turn-1",
- "callId": "call-1",
- "tool": "find_agent",
- "arguments": {},
- },
- )
- )
- assert await asyncio.to_thread(scheduler_entered.wait, 1)
-
- unregister_done = threading.Event()
-
- def unregister() -> None:
- runtime._unregister_dynamic_tool_handler("thread-1")
- unregister_done.set()
-
- unregister_thread = threading.Thread(
- target=unregister,
- name="codex-unregister",
- )
- unregister_thread.start()
- assert await asyncio.to_thread(observed_lock.unregister_attempted.wait, 1)
- if scheduler_held_lock == [True]:
- assert not observed_lock.unregister_acquired.is_set()
- else:
- assert await asyncio.to_thread(unregister_done.wait, 1)
-
- allow_scheduler_return.set()
- assert await asyncio.to_thread(unregister_done.wait, 1)
- unregister_thread.join(timeout=1)
- assert not unregister_thread.is_alive()
- response = await dispatch
-
- assert scheduler_held_lock == [True]
- assert future.cancelled()
- assert "thread-1" not in runtime._dynamic_tool_handlers
- assert "thread-1" not in runtime._dynamic_tool_futures
- assert response == {
- "contentItems": [{"type": "inputText", "text": "codex_tool_execution_failed"}],
- "success": False,
- }
-
-
-def test_concurrent_dispatcher_keeps_reading_while_tool_result_is_pending():
- handler_started = threading.Event()
- allow_handler = threading.Event()
- notification_routed = threading.Event()
- response_written = threading.Event()
- messages = [
- {"id": "request-1", "method": "item/tool/call", "params": {}},
- {"method": "item/agentMessage/delta", "params": {"delta": "still-live"}},
- ]
-
- class Router:
- def route_notification(self, _notification):
- notification_routed.set()
-
- def route_response(self, _message):
- raise AssertionError("unexpected response")
-
- def fail_all(self, _error):
- pass
-
- class SyncClient:
- _proc = object()
- _router = Router()
-
- def _read_message(self):
- if messages:
- return messages.pop(0)
- raise EOFError
-
- def _handle_server_request(self, _message):
- handler_started.set()
- assert allow_handler.wait(timeout=1)
- return {"success": True}
-
- def _write_message(self, message):
- assert message == {
- "id": "request-1",
- "result": {"success": True},
- }
- response_written.set()
-
- def _coerce_notification(self, method, params):
- return method, params
-
- sync_client = SyncClient()
- client = SimpleNamespace(_client=SimpleNamespace(_sync=sync_client))
- _install_concurrent_server_request_dispatcher(client) # type: ignore[arg-type]
-
- reader = threading.Thread(target=sync_client._reader_loop, daemon=True)
- reader.start()
- assert handler_started.wait(timeout=1)
- assert notification_routed.wait(timeout=1)
- assert not response_written.is_set()
- allow_handler.set()
- assert response_written.wait(timeout=1)
-
-
-def test_concurrent_dispatcher_rejects_requests_above_its_bound(monkeypatch):
- monkeypatch.setattr(runtime_module, "_SERVER_REQUEST_MAX_CONCURRENCY", 1)
- handler_started = threading.Event()
- allow_handler = threading.Event()
- notification_routed = threading.Event()
- first_response_written = threading.Event()
- overload_response_written = threading.Event()
- messages = [
- {"id": "request-1", "method": "item/tool/call", "params": {}},
- {"id": "request-2", "method": "item/tool/call", "params": {}},
- {"method": "item/agentMessage/delta", "params": {"delta": "still-live"}},
- ]
-
- class Router:
- def route_notification(self, _notification):
- notification_routed.set()
-
- def route_response(self, _message):
- raise AssertionError("unexpected response")
-
- def fail_all(self, _error):
- pass
-
- class SyncClient:
- _proc = object()
- _router = Router()
-
- def _read_message(self):
- if messages:
- return messages.pop(0)
- raise EOFError
-
- def _handle_server_request(self, message):
- assert message["id"] == "request-1"
- handler_started.set()
- assert allow_handler.wait(timeout=1)
- return {"success": True}
-
- def _write_message(self, message):
- if message.get("id") == "request-1":
- assert message == {
- "id": "request-1",
- "result": {"success": True},
- }
- first_response_written.set()
- return
- assert message == {
- "id": "request-2",
- "error": {
- "code": -32001,
- "message": "Codex tool bridge is busy",
- },
- }
- overload_response_written.set()
-
- def _coerce_notification(self, method, params):
- return method, params
-
- sync_client = SyncClient()
- client = SimpleNamespace(_client=SimpleNamespace(_sync=sync_client))
- _install_concurrent_server_request_dispatcher(client) # type: ignore[arg-type]
-
- reader = threading.Thread(target=sync_client._reader_loop, daemon=True)
- reader.start()
- assert handler_started.wait(timeout=1)
- assert overload_response_written.wait(timeout=1)
- assert notification_routed.wait(timeout=1)
- allow_handler.set()
- assert first_response_written.wait(timeout=1)
-
-
-async def test_runtime_close_drains_accepted_server_requests(tmp_path):
- handler_started = threading.Event()
- allow_handler = threading.Event()
- response_written = threading.Event()
- client_close_called = asyncio.Event()
- messages = [{"id": "request-1", "method": "item/tool/call", "params": {}}]
-
- class Router:
- def route_notification(self, _notification):
- raise AssertionError("unexpected notification")
-
- def route_response(self, _message):
- raise AssertionError("unexpected response")
-
- def fail_all(self, _error):
- pass
-
- class SyncClient:
- _proc = object()
- _router = Router()
-
- def _read_message(self):
- if messages:
- return messages.pop(0)
- raise EOFError
-
- def _handle_server_request(self, _message):
- handler_started.set()
- assert allow_handler.wait(timeout=1)
- return {"success": True}
-
- def _write_message(self, message):
- assert self._proc is not None
- assert message == {
- "id": "request-1",
- "result": {"success": True},
- }
- response_written.set()
-
- def _coerce_notification(self, method, params):
- return method, params
-
- sync_client = SyncClient()
-
- class Client:
- _client = SimpleNamespace(_sync=sync_client)
-
- async def close(self):
- client_close_called.set()
- sync_client._proc = None
-
- client = Client()
- _install_concurrent_server_request_dispatcher(client) # type: ignore[arg-type]
- reader = threading.Thread(target=sync_client._reader_loop, daemon=True)
- reader.start()
- assert handler_started.wait(timeout=1)
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime( # type: ignore[arg-type]
- client,
- home,
- close_timeout_seconds=1,
- )
- closing = asyncio.create_task(runtime.close())
- await asyncio.sleep(0.01)
- assert not client_close_called.is_set()
- allow_handler.set()
- await asyncio.wait_for(closing, timeout=1)
-
- assert response_written.is_set()
- assert client_close_called.is_set()
-
-
-async def test_runtime_close_abandons_stuck_server_request_after_deadline(tmp_path):
- handler_started = threading.Event()
- allow_handler = threading.Event()
- handler_finished = threading.Event()
- response_written = threading.Event()
- client_close_called = asyncio.Event()
- messages = [{"id": "request-1", "method": "item/tool/call", "params": {}}]
-
- class Router:
- def route_notification(self, _notification):
- raise AssertionError("unexpected notification")
-
- def route_response(self, _message):
- raise AssertionError("unexpected response")
-
- def fail_all(self, _error):
- pass
-
- class SyncClient:
- _proc = object()
- _router = Router()
-
- def _read_message(self):
- if messages:
- return messages.pop(0)
- raise EOFError
-
- def _handle_server_request(self, _message):
- handler_started.set()
- assert allow_handler.wait(timeout=1)
- handler_finished.set()
- return {"success": True}
-
- def _write_message(self, _message):
- if self._proc is None:
- raise RuntimeError("process closed")
- response_written.set()
-
- def _coerce_notification(self, method, params):
- return method, params
-
- sync_client = SyncClient()
-
- class Client:
- _client = SimpleNamespace(_sync=sync_client)
-
- async def close(self):
- client_close_called.set()
- sync_client._proc = None
-
- client = Client()
- _install_concurrent_server_request_dispatcher(client) # type: ignore[arg-type]
- reader = threading.Thread(target=sync_client._reader_loop, daemon=True)
- reader.start()
- assert handler_started.wait(timeout=1)
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime( # type: ignore[arg-type]
- client,
- home,
- close_timeout_seconds=0.01,
- )
- await asyncio.wait_for(runtime.close(), timeout=0.2)
-
- assert client_close_called.is_set()
- assert not response_written.is_set()
- allow_handler.set()
- assert handler_finished.wait(timeout=1)
-
-
-async def test_runtime_close_force_stops_child_when_client_close_never_returns(
- tmp_path,
-):
- close_started = asyncio.Event()
-
- class ProcessIgnoringTerminate:
- stdin = None
-
- def __init__(self):
- self.terminated = False
- self.killed = False
-
- def terminate(self):
- self.terminated = True
-
- def poll(self):
- return None
-
- def kill(self):
- self.killed = True
-
- process = ProcessIgnoringTerminate()
-
- class NeverClosingClient:
- _client = SimpleNamespace(_sync=SimpleNamespace(_proc=process))
-
- async def close(self):
- close_started.set()
- await asyncio.Event().wait()
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime( # type: ignore[arg-type]
- NeverClosingClient(),
- home,
- close_timeout_seconds=0.01,
- )
- await asyncio.wait_for(runtime.close(), timeout=0.2)
-
- assert close_started.is_set()
- assert process.terminated
- assert process.killed
-
-
-async def test_stream_close_timeout_force_closes_runtime(tmp_path):
- class NeverClosingStream:
- async def aclose(self):
- await asyncio.Event().wait()
-
- client = SimpleNamespace(
- close=AsyncMock(),
- _client=SimpleNamespace(_sync=SimpleNamespace(_proc=None)),
- )
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime( # type: ignore[arg-type]
- client,
- home,
- close_timeout_seconds=0.01,
- )
- with pytest.raises(CodexRuntimeError, match="codex_stream_shutdown_timeout"):
- await asyncio.wait_for(
- runtime._close_agent_stream(NeverClosingStream()),
- timeout=0.2,
- )
-
- client.close.assert_awaited_once()
-
-
-async def test_cancelled_start_does_not_wait_forever_for_stuck_enter(
- tmp_path,
- monkeypatch,
-):
- entered = asyncio.Event()
- release = asyncio.Event()
-
- class StuckStartupClient:
- def __init__(self, _config):
- self.closed = False
- self._client = SimpleNamespace(_sync=SimpleNamespace(_proc=None))
-
- async def __aenter__(self):
- entered.set()
- while not release.is_set():
- try:
- await release.wait()
- except asyncio.CancelledError:
- continue
- return self
-
- async def close(self):
- self.closed = True
-
- fake_client = None
-
- def create_client(config):
- nonlocal fake_client
- fake_client = StuckStartupClient(config)
- return fake_client
-
- monkeypatch.setattr(runtime_module, "AsyncCodex", create_client)
- monkeypatch.setattr(runtime_module, "assert_pinned_versions", lambda: None)
- monkeypatch.setattr(
- runtime_module,
- "_install_concurrent_server_request_dispatcher",
- lambda _client: None,
- )
- monkeypatch.setattr(runtime_module, "_RUNTIME_CLOSE_TIMEOUT_SECONDS", 0.01)
-
- with TemporaryCodexHome.create(tmp_path) as home:
- startup = asyncio.create_task(CodexRuntime.start(home))
- await asyncio.wait_for(entered.wait(), timeout=1)
- startup.cancel()
- with pytest.raises(asyncio.CancelledError):
- await asyncio.wait_for(startup, timeout=0.2)
-
- assert fake_client is not None
- assert fake_client.closed
- release.set()
- await asyncio.sleep(0)
-
-
-async def test_cancelled_invoke_does_not_wait_for_stuck_turn_start(tmp_path):
- started = asyncio.Event()
- release = asyncio.Event()
- stopped = asyncio.Event()
-
- class StuckThread:
- async def turn(self, *_args, **_kwargs):
- started.set()
- while not release.is_set():
- try:
- await release.wait()
- except asyncio.CancelledError:
- continue
- stopped.set()
- return SimpleNamespace()
-
- client = SimpleNamespace(
- close=AsyncMock(),
- _client=SimpleNamespace(_sync=SimpleNamespace(_proc=None)),
- )
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime( # type: ignore[arg-type]
- client,
- home,
- close_timeout_seconds=0.01,
- )
- runtime._start_toolless_thread = AsyncMock( # type: ignore[method-assign]
- return_value=StuckThread()
- )
- invocation = asyncio.create_task(
- runtime.invoke(CodexInvocationRequest(prompt="hello"))
- )
- await asyncio.wait_for(started.wait(), timeout=1)
- invocation.cancel()
- with pytest.raises(asyncio.CancelledError):
- await asyncio.wait_for(invocation, timeout=0.2)
-
- client.close.assert_awaited_once()
- release.set()
- await asyncio.wait_for(stopped.wait(), timeout=1)
-
-
-async def test_cancelled_agent_turn_start_recovers_and_interrupts_without_closing(
- tmp_path,
-):
- started = asyncio.Event()
- release = asyncio.Event()
- interrupt = AsyncMock()
-
- class RecoverableThread:
- id = "thread-1"
-
- async def turn(self, *_args, **_kwargs):
- started.set()
- await release.wait()
- return SimpleNamespace(id="turn-1", interrupt=interrupt)
-
- client = SimpleNamespace(
- close=AsyncMock(),
- _client=SimpleNamespace(
- _sync=SimpleNamespace(_approval_handler=None, _proc=None)
- ),
- )
-
- async def execute(_call):
- return CodexDynamicToolResult(content="ok", success=True)
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime( # type: ignore[arg-type]
- client,
- home,
- close_timeout_seconds=0.1,
- )
- runtime._start_thread = AsyncMock( # type: ignore[method-assign]
- return_value=RecoverableThread()
- )
- invocation = asyncio.create_task(
- runtime.invoke_agent(
- CodexInvocationRequest(prompt="hello"),
- [],
- execute,
- tool_timeout_seconds=1,
- )
- )
- await asyncio.wait_for(started.wait(), timeout=1)
- invocation.cancel()
- release.set()
- with pytest.raises(asyncio.CancelledError):
- await asyncio.wait_for(invocation, timeout=1)
-
- assert not runtime.closed
-
- interrupt.assert_awaited_once()
- client.close.assert_not_awaited()
-
-
-async def test_cancelled_agent_turn_start_poisoned_when_handle_cannot_be_recovered(
- tmp_path,
-):
- started = asyncio.Event()
- release = asyncio.Event()
- stopped = asyncio.Event()
-
- class StuckThread:
- id = "thread-1"
-
- async def turn(self, *_args, **_kwargs):
- started.set()
- while not release.is_set():
- try:
- await release.wait()
- except asyncio.CancelledError:
- continue
- stopped.set()
- return SimpleNamespace(id="turn-1")
-
- client = SimpleNamespace(
- close=AsyncMock(),
- _client=SimpleNamespace(
- _sync=SimpleNamespace(_approval_handler=None, _proc=None)
- ),
- )
-
- async def execute(_call):
- return CodexDynamicToolResult(content="ok", success=True)
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime( # type: ignore[arg-type]
- client,
- home,
- close_timeout_seconds=0.01,
- )
- runtime._start_thread = AsyncMock( # type: ignore[method-assign]
- return_value=StuckThread()
- )
- invocation = asyncio.create_task(
- runtime.invoke_agent(
- CodexInvocationRequest(prompt="hello"),
- [],
- execute,
- tool_timeout_seconds=1,
- )
- )
- await asyncio.wait_for(started.wait(), timeout=1)
- invocation.cancel()
- with pytest.raises(asyncio.CancelledError):
- await asyncio.wait_for(invocation, timeout=1)
-
- assert runtime.closed
-
- client.close.assert_awaited_once()
- release.set()
- await asyncio.wait_for(stopped.wait(), timeout=1)
-
-
-async def test_agent_stream_failure_interrupts_without_closing_sibling_runtime(
- tmp_path,
-):
- interrupt = AsyncMock()
-
- async def failing_stream():
- if False:
- yield None
- raise RuntimeError("stream failed")
-
- turn = SimpleNamespace(
- id="turn-1",
- interrupt=interrupt,
- stream=failing_stream,
- )
- thread = SimpleNamespace(
- id="thread-1",
- turn=AsyncMock(return_value=turn),
- )
- client = SimpleNamespace(
- close=AsyncMock(),
- _client=SimpleNamespace(
- _sync=SimpleNamespace(_approval_handler=None, _proc=None)
- ),
- )
-
- async def execute(_call):
- return CodexDynamicToolResult(content="ok", success=True)
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime(client, home) # type: ignore[arg-type]
- runtime._start_thread = AsyncMock( # type: ignore[method-assign]
- return_value=thread
- )
- with pytest.raises(RuntimeError, match="stream failed"):
- await runtime.invoke_agent(
- CodexInvocationRequest(prompt="hello"),
- [],
- execute,
- tool_timeout_seconds=1,
- )
-
- assert not runtime.closed
-
- interrupt.assert_awaited_once()
- client.close.assert_not_awaited()
-
-
-async def test_agent_event_failure_poisoned_when_interrupt_fails(tmp_path):
- interrupt = AsyncMock(side_effect=RuntimeError("interrupt failed"))
-
- async def one_event_stream():
- yield SimpleNamespace()
-
- turn = SimpleNamespace(
- id="turn-1",
- interrupt=interrupt,
- stream=one_event_stream,
- )
- thread = SimpleNamespace(
- id="thread-1",
- turn=AsyncMock(return_value=turn),
- )
- client = SimpleNamespace(
- close=AsyncMock(),
- _client=SimpleNamespace(
- _sync=SimpleNamespace(_approval_handler=None, _proc=None)
- ),
- )
-
- async def execute(_call):
- return CodexDynamicToolResult(content="ok", success=True)
-
- async def reject_event(_event):
- raise RuntimeError("event failed")
-
- with TemporaryCodexHome.create(tmp_path) as home:
- runtime = CodexRuntime(client, home) # type: ignore[arg-type]
- runtime._start_thread = AsyncMock( # type: ignore[method-assign]
- return_value=thread
- )
- with pytest.raises(RuntimeError, match="event failed"):
- await runtime.invoke_agent(
- CodexInvocationRequest(prompt="hello"),
- [],
- execute,
- reject_event,
- tool_timeout_seconds=1,
- )
-
- assert runtime.closed
-
- interrupt.assert_awaited_once()
- client.close.assert_awaited_once()
diff --git a/autogpt_platform/backend/backend/integrations/codex/temporary_home.py b/autogpt_platform/backend/backend/integrations/codex/temporary_home.py
deleted file mode 100644
index ebc5862d26..0000000000
--- a/autogpt_platform/backend/backend/integrations/codex/temporary_home.py
+++ /dev/null
@@ -1,265 +0,0 @@
-import asyncio
-import logging
-import os
-import shutil
-import stat
-import tempfile
-import threading
-import time
-import uuid
-from collections.abc import Callable
-from contextlib import suppress
-from pathlib import Path
-from types import TracebackType
-
-logger = logging.getLogger(__name__)
-
-_HOME_PREFIX = "autogpt-codex-"
-_QUARANTINE_PREFIX = ".quarantine-autogpt-codex-"
-_REAPER_MARKER_PREFIX = ".reap-"
-_CLEANUP_RETRY_DELAYS_SECONDS = (0.0, 0.02, 0.05, 0.1, 0.2, 0.4)
-_REAPER_LOCK = threading.Lock()
-_REAPER_TARGETS: set[tuple[Path, Path, Path]] = set()
-_REAPER_THREAD: threading.Thread | None = None
-
-
-class TemporaryCodexHomeCleanupError(RuntimeError):
- pass
-
-
-class TemporaryCodexHome:
- def __init__(self, path: Path, root: Path) -> None:
- _validate_managed_target(path, root)
- self.path = path
- self.root = root
- self.auth_path = path / "auth.json"
- self.temp_path = path / "tmp"
- self.workspace_path = path / "workspace"
- self._cleaned = False
-
- @classmethod
- def create(cls, root: Path | None = None) -> "TemporaryCodexHome":
- resolved_root = _prepare_root(root)
- _schedule_existing_reaps(resolved_root)
- path = Path(
- tempfile.mkdtemp(
- prefix=_HOME_PREFIX,
- dir=str(resolved_root),
- )
- ).resolve()
- _validate_managed_target(path, resolved_root)
- home = cls(path, resolved_root)
- try:
- os.chmod(path, 0o700)
- home.temp_path.mkdir(mode=0o700)
- home.workspace_path.mkdir(mode=0o700)
- except BaseException:
- home.cleanup()
- raise
- return home
-
- def __enter__(self) -> "TemporaryCodexHome":
- return self
-
- def __exit__(
- self,
- _exc_type: type[BaseException] | None,
- _exc: BaseException | None,
- _traceback: TracebackType | None,
- ) -> None:
- self.cleanup()
-
- async def __aenter__(self) -> "TemporaryCodexHome":
- return self
-
- async def __aexit__(
- self,
- _exc_type: type[BaseException] | None,
- _exc: BaseException | None,
- _traceback: TracebackType | None,
- ) -> None:
- await asyncio.to_thread(self.cleanup)
-
- def cleanup(self) -> None:
- if self._cleaned:
- return
- _validate_managed_target(self.path, self.root)
- if _remove_with_retry(self.path, self.root):
- self._cleaned = True
- return
-
- quarantined = _quarantine(self.path, self.root)
- if quarantined is not None:
- _schedule_reap(self.root, quarantined)
- self._cleaned = True
- return
-
- _schedule_reap(self.root, self.path)
- raise TemporaryCodexHomeCleanupError(
- "Could not remove or quarantine isolated Codex auth home"
- )
-
-
-def _prepare_root(root: Path | None) -> Path:
- candidate = (root or Path(tempfile.gettempdir()) / "autogpt-codex").absolute()
- candidate = _canonicalize_trusted_temp_base(candidate)
- for component in (candidate, *candidate.parents):
- if os.path.lexists(component) and component.is_symlink():
- raise ValueError("Codex temporary root cannot contain symbolic links")
- candidate.mkdir(mode=0o700, parents=True, exist_ok=True)
- if candidate.is_symlink():
- raise ValueError("Codex temporary root cannot contain symbolic links")
- resolved = candidate.resolve()
- if resolved == Path(resolved.anchor):
- raise ValueError("Codex temporary root cannot be a filesystem root")
- os.chmod(resolved, 0o700)
- if os.name != "nt" and stat.S_IMODE(resolved.stat().st_mode) != 0o700:
- raise PermissionError("Codex temporary root must have mode 0700")
- return resolved
-
-
-def _canonicalize_trusted_temp_base(candidate: Path) -> Path:
- for base in sorted(
- _trusted_system_temp_bases(), key=lambda path: len(path.parts), reverse=True
- ):
- if not os.path.lexists(base):
- continue
- try:
- relative = candidate.relative_to(base)
- except ValueError:
- continue
- if relative == Path("."):
- return candidate
- return base.resolve() / relative
- return candidate
-
-
-def _trusted_system_temp_bases() -> tuple[Path, ...]:
- if os.name == "nt":
- return ()
- return (Path("/tmp"), Path("/var"))
-
-
-def _validate_managed_target(path: Path, root: Path) -> None:
- resolved_root = root.resolve()
- if resolved_root == Path(resolved_root.anchor):
- raise ValueError("Codex temporary root cannot be a filesystem root")
- if path.parent.resolve() != resolved_root:
- raise ValueError("Codex temporary home must be a direct child of its root")
- if not path.name.startswith((_HOME_PREFIX, _QUARANTINE_PREFIX)):
- raise ValueError("Refusing to manage an unexpected Codex temporary path")
-
-
-def _remove_with_retry(
- path: Path,
- root: Path,
- delays: tuple[float, ...] = _CLEANUP_RETRY_DELAYS_SECONDS,
-) -> bool:
- for delay in delays:
- if delay:
- time.sleep(delay)
- try:
- _remove_tree_once(path, root)
- return True
- except FileNotFoundError:
- return True
- except OSError:
- continue
- return not os.path.lexists(path)
-
-
-def _remove_tree_once(path: Path, root: Path) -> None:
- _validate_managed_target(path, root)
- if not os.path.lexists(path):
- return
- if path.is_symlink():
- path.unlink()
- return
- shutil.rmtree(path, onerror=_make_writable_and_retry)
-
-
-def _make_writable_and_retry(
- operation: Callable[..., object],
- path: str,
- _error: object,
-) -> None:
- os.chmod(path, stat.S_IRWXU)
- operation(path)
-
-
-def _quarantine(path: Path, root: Path) -> Path | None:
- _validate_managed_target(path, root)
- if not os.path.lexists(path):
- return None
- quarantined = root / f"{_QUARANTINE_PREFIX}{uuid.uuid4().hex}"
- _validate_managed_target(quarantined, root)
- try:
- os.replace(path, quarantined)
- except OSError:
- return None
- return quarantined
-
-
-def _schedule_reap(root: Path, target: Path) -> None:
- _validate_managed_target(target, root)
- marker = root / f"{_REAPER_MARKER_PREFIX}{target.name}"
- descriptor = os.open(marker, os.O_CREAT | os.O_WRONLY, 0o600)
- os.close(descriptor)
- os.chmod(marker, 0o600)
-
- global _REAPER_THREAD
- with _REAPER_LOCK:
- _REAPER_TARGETS.add((root, target, marker))
- if _REAPER_THREAD is None or not _REAPER_THREAD.is_alive():
- _REAPER_THREAD = threading.Thread(
- target=_reaper_loop,
- name="codex-auth-home-reaper",
- daemon=True,
- )
- _REAPER_THREAD.start()
-
-
-def _schedule_existing_reaps(root: Path) -> None:
- for target in root.glob(f"{_QUARANTINE_PREFIX}*"):
- if target.parent == root:
- _schedule_reap(root, target)
- for marker in root.glob(f"{_REAPER_MARKER_PREFIX}*"):
- target_name = marker.name.removeprefix(_REAPER_MARKER_PREFIX)
- if not target_name.startswith((_HOME_PREFIX, _QUARANTINE_PREFIX)):
- continue
- target = root / target_name
- if os.path.lexists(target):
- _schedule_reap(root, target)
- else:
- with suppress(OSError):
- marker.unlink(missing_ok=True)
-
-
-def _reaper_loop() -> None:
- backoff_seconds = 0.05
- while True:
- with _REAPER_LOCK:
- pending = tuple(_REAPER_TARGETS)
- if not pending:
- global _REAPER_THREAD
- _REAPER_THREAD = None
- return
-
- removed: list[tuple[Path, Path, Path]] = []
- for root, target, marker in pending:
- if _remove_with_retry(target, root, delays=(0.0,)):
- with suppress(OSError):
- marker.unlink(missing_ok=True)
- removed.append((root, target, marker))
-
- with _REAPER_LOCK:
- _REAPER_TARGETS.difference_update(removed)
- if removed:
- backoff_seconds = 0.05
- continue
-
- logger.warning(
- "Retrying cleanup of %d isolated Codex auth home(s)", len(pending)
- )
- time.sleep(backoff_seconds)
- backoff_seconds = min(backoff_seconds * 2, 2.0)
diff --git a/autogpt_platform/backend/backend/integrations/codex/temporary_home_test.py b/autogpt_platform/backend/backend/integrations/codex/temporary_home_test.py
deleted file mode 100644
index d22de86bbb..0000000000
--- a/autogpt_platform/backend/backend/integrations/codex/temporary_home_test.py
+++ /dev/null
@@ -1,311 +0,0 @@
-import asyncio
-import os
-import shutil
-import threading
-import time
-from pathlib import Path
-
-import pytest
-
-from backend.integrations.codex import temporary_home
-from backend.integrations.codex.temporary_home import (
- TemporaryCodexHome,
- TemporaryCodexHomeCleanupError,
-)
-
-
-def test_temporary_home_creates_private_workspace_and_cleans_up(tmp_path):
- with TemporaryCodexHome.create(tmp_path) as home:
- path = home.path
- assert home.root == tmp_path.resolve()
- assert path.parent == tmp_path.resolve()
- assert home.auth_path == path / "auth.json"
- assert home.workspace_path.is_dir()
- if os.name != "nt":
- assert path.stat().st_mode & 0o777 == 0o700
- assert home.workspace_path.stat().st_mode & 0o777 == 0o700
-
- assert not path.exists()
-
-
-def test_temporary_home_cleans_up_after_failure(tmp_path):
- with pytest.raises(RuntimeError, match="expected"):
- with TemporaryCodexHome.create(tmp_path) as home:
- path = home.path
- raise RuntimeError("expected")
-
- assert not path.exists()
-
-
-def test_temporary_home_cleans_up_if_initialization_fails(tmp_path, monkeypatch):
- mkdir = Path.mkdir
-
- def fail_workspace(path: Path, *args, **kwargs) -> None:
- if path.name == "workspace":
- raise PermissionError("workspace creation failed")
- mkdir(path, *args, **kwargs)
-
- monkeypatch.setattr(Path, "mkdir", fail_workspace)
-
- with pytest.raises(PermissionError, match="workspace creation failed"):
- TemporaryCodexHome.create(tmp_path)
-
- assert not list(tmp_path.glob("autogpt-codex-*"))
-
-
-async def test_temporary_home_cleans_up_after_cancellation(tmp_path):
- entered = asyncio.Event()
-
- async def wait_forever() -> None:
- async with TemporaryCodexHome.create(tmp_path) as home:
- nonlocal path
- path = home.path
- entered.set()
- await asyncio.Event().wait()
-
- path = tmp_path
- task = asyncio.create_task(wait_forever())
- await entered.wait()
- task.cancel()
-
- with pytest.raises(asyncio.CancelledError):
- await task
-
- assert not path.exists()
-
-
-async def test_async_exit_runs_cleanup_off_the_event_loop(tmp_path, monkeypatch):
- home = TemporaryCodexHome.create(tmp_path)
- event_loop_thread = threading.get_ident()
- cleanup_threads: list[int] = []
- cleanup = home.cleanup
-
- def observed_cleanup() -> None:
- cleanup_threads.append(threading.get_ident())
- cleanup()
-
- monkeypatch.setattr(home, "cleanup", observed_cleanup)
-
- async with home:
- pass
-
- assert cleanup_threads
- assert cleanup_threads[0] != event_loop_thread
- assert not home.path.exists()
-
-
-def test_cleanup_is_idempotent(tmp_path):
- home = TemporaryCodexHome.create(tmp_path)
- path = home.path
-
- home.cleanup()
- home.cleanup()
-
- assert not path.exists()
-
-
-def test_cleanup_retries_transient_filesystem_locks(tmp_path, monkeypatch):
- home = TemporaryCodexHome.create(tmp_path)
- path = home.path
- attempts = 0
- remove_tree_once = temporary_home._remove_tree_once
-
- def fail_twice(target: Path, root: Path) -> None:
- nonlocal attempts
- attempts += 1
- if attempts < 3:
- raise PermissionError("transient lock")
- remove_tree_once(target, root)
-
- monkeypatch.setattr(temporary_home, "_remove_tree_once", fail_twice)
- monkeypatch.setattr(temporary_home.time, "sleep", lambda _seconds: None)
-
- home.cleanup()
-
- assert attempts == 3
- assert not path.exists()
-
-
-def test_cleanup_quarantines_a_locked_home_under_the_dedicated_root(
- tmp_path, monkeypatch
-):
- home = TemporaryCodexHome.create(tmp_path)
- home.auth_path.write_text("fake-sensitive-auth", encoding="utf-8")
- scheduled: list[tuple[Path, Path]] = []
- monkeypatch.setattr(temporary_home, "_remove_with_retry", lambda *_args: False)
- monkeypatch.setattr(
- temporary_home,
- "_schedule_reap",
- lambda root, target: scheduled.append((root, target)),
- )
-
- home.cleanup()
-
- assert not home.path.exists()
- assert len(scheduled) == 1
- root, quarantined = scheduled[0]
- assert root == tmp_path.resolve()
- assert quarantined.parent == root
- assert quarantined.name.startswith(".quarantine-autogpt-codex-")
- assert (quarantined / "auth.json").read_text(encoding="utf-8") == (
- "fake-sensitive-auth"
- )
- shutil.rmtree(quarantined)
-
-
-def test_cleanup_fails_loudly_if_home_cannot_be_removed_or_quarantined(
- tmp_path, monkeypatch
-):
- home = TemporaryCodexHome.create(tmp_path)
- scheduled: list[tuple[Path, Path]] = []
- monkeypatch.setattr(temporary_home, "_remove_with_retry", lambda *_args: False)
- monkeypatch.setattr(temporary_home, "_quarantine", lambda *_args: None)
- monkeypatch.setattr(
- temporary_home,
- "_schedule_reap",
- lambda root, target: scheduled.append((root, target)),
- )
-
- with pytest.raises(TemporaryCodexHomeCleanupError):
- home.cleanup()
-
- assert scheduled == [(tmp_path.resolve(), home.path)]
- assert home.path.exists()
- shutil.rmtree(home.path)
-
-
-def test_temporary_home_rejects_paths_outside_the_dedicated_root(tmp_path):
- root = tmp_path / "managed"
- root.mkdir()
- outside = tmp_path / "autogpt-codex-outside"
- outside.mkdir()
-
- with pytest.raises(ValueError, match="direct child"):
- TemporaryCodexHome(outside, root.resolve())
-
-
-def test_temporary_home_rejects_a_filesystem_root():
- filesystem_root = Path(Path.cwd().anchor)
-
- with pytest.raises(ValueError, match="filesystem root"):
- temporary_home._prepare_root(filesystem_root)
-
-
-def test_temporary_home_rejects_a_preexisting_symlink_root(tmp_path):
- target = tmp_path / "attacker-controlled"
- target.mkdir()
- link = tmp_path / "autogpt-codex"
- try:
- link.symlink_to(target, target_is_directory=True)
- except OSError as error:
- pytest.skip(f"symlinks are unavailable: {error}")
-
- with pytest.raises(ValueError, match="symbolic link"):
- temporary_home._prepare_root(link)
-
-
-def test_temporary_home_rejects_a_symlink_parent(tmp_path):
- target = tmp_path / "attacker-controlled"
- target.mkdir()
- link = tmp_path / "managed-parent"
- try:
- link.symlink_to(target, target_is_directory=True)
- except OSError as error:
- pytest.skip(f"symlinks are unavailable: {error}")
-
- with pytest.raises(ValueError, match="symbolic link"):
- temporary_home._prepare_root(link / "autogpt-codex")
-
-
-def test_temporary_home_accepts_a_symlinked_system_temp_base(tmp_path, monkeypatch):
- target = tmp_path / "private-temp"
- target.mkdir()
- link = tmp_path / "system-temp"
- try:
- link.symlink_to(target, target_is_directory=True)
- except OSError as error:
- pytest.skip(f"symlinks are unavailable: {error}")
- monkeypatch.setattr(temporary_home.tempfile, "gettempdir", lambda: str(link))
- monkeypatch.setattr(temporary_home, "_trusted_system_temp_bases", lambda: (link,))
-
- root = temporary_home._prepare_root(None)
-
- assert root == target / "autogpt-codex"
- assert root.is_dir()
- assert not root.is_symlink()
-
-
-def test_temporary_home_rejects_a_symlink_below_system_temp_base(tmp_path, monkeypatch):
- target = tmp_path / "private-temp"
- target.mkdir()
- system_link = tmp_path / "system-temp"
- attacker_target = tmp_path / "attacker-controlled"
- attacker_target.mkdir()
- managed_link = target / "managed-parent"
- try:
- system_link.symlink_to(target, target_is_directory=True)
- managed_link.symlink_to(attacker_target, target_is_directory=True)
- except OSError as error:
- pytest.skip(f"symlinks are unavailable: {error}")
- monkeypatch.setattr(temporary_home.tempfile, "gettempdir", lambda: str(system_link))
- monkeypatch.setattr(
- temporary_home, "_trusted_system_temp_bases", lambda: (system_link,)
- )
-
- with pytest.raises(ValueError, match="symbolic link"):
- temporary_home._prepare_root(system_link / "managed-parent" / "autogpt-codex")
-
-
-def test_temporary_home_does_not_trust_a_symlink_from_gettempdir(tmp_path, monkeypatch):
- target = tmp_path / "private-temp"
- target.mkdir()
- link = tmp_path / "user-temp"
- try:
- link.symlink_to(target, target_is_directory=True)
- except OSError as error:
- pytest.skip(f"symlinks are unavailable: {error}")
- monkeypatch.setattr(temporary_home.tempfile, "gettempdir", lambda: str(link))
- monkeypatch.setattr(temporary_home, "_trusted_system_temp_bases", lambda: ())
-
- with pytest.raises(ValueError, match="symbolic link"):
- temporary_home._prepare_root(None)
-
-
[email protected](os.name == "nt", reason="POSIX mode bits are not ACLs")
-def test_temporary_home_rejects_root_when_private_mode_cannot_be_set(
- tmp_path, monkeypatch
-):
- root = tmp_path / "shared"
- root.mkdir(mode=0o755)
- os.chmod(root, 0o755)
- monkeypatch.setattr(temporary_home.os, "chmod", lambda *_args: None)
-
- with pytest.raises(PermissionError, match="mode 0700"):
- temporary_home._prepare_root(root)
-
-
-def test_reaper_recovers_a_marked_quarantine_after_restart(tmp_path):
- root = temporary_home._prepare_root(tmp_path)
- quarantined = root / ".quarantine-autogpt-codex-stale"
- quarantined.mkdir(mode=0o700)
- (quarantined / "auth.json").write_text("fake-sensitive-auth", encoding="utf-8")
- marker = root / f".reap-{quarantined.name}"
- marker.touch(mode=0o600)
-
- try:
- temporary_home._schedule_existing_reaps(root)
-
- deadline = time.monotonic() + 2
- while (quarantined.exists() or marker.exists()) and time.monotonic() < deadline:
- time.sleep(0.01)
-
- assert not quarantined.exists()
- assert not marker.exists()
- finally:
- with temporary_home._REAPER_LOCK:
- temporary_home._REAPER_TARGETS.clear()
- reaper = temporary_home._REAPER_THREAD
- if reaper is not None:
- reaper.join(timeout=2)
diff --git a/autogpt_platform/backend/backend/integrations/codex/transport.py b/autogpt_platform/backend/backend/integrations/codex/transport.py
index d8b3b158d1..02c7ea5826 100644
--- a/autogpt_platform/backend/backend/integrations/codex/transport.py
+++ b/autogpt_platform/backend/backend/integrations/codex/transport.py
@@ -1,54 +1,61 @@
+"""Codex access over HTTP.
+
+Sign-in, the model catalog, rate limits and inference all run against
+``auth.openai.com`` and ``chatgpt.com`` directly. There is no Codex process, so
+the machinery that used to exist purely to manage one -- a runtime pool, a
+process capacity semaphore, synthetic ``$HOME`` directories and per-credential
+exclusivity -- is gone with it.
+
+The most visible consequence is concurrency: a ChatGPT connection used to serve
+one chat at a time because a single subprocess owned the credential. Over HTTP
+the same credential serves as many concurrent turns as the account allows.
+"""
+
+from __future__ import annotations
+
import asyncio
-import concurrent.futures
-import inspect
import logging
import threading
from collections.abc import AsyncIterator, Awaitable, Callable
-from contextlib import AbstractAsyncContextManager, asynccontextmanager, suppress
-from pathlib import Path
-from typing import Any, Protocol, TypeVar, cast
-from uuid import uuid4
-
-from openai_codex.models import Notification
-from pydantic import BaseModel, ConfigDict, Field
-
-from backend.data.model import OAuth2Credentials
-from backend.integrations.codex.auth_bundle import (
- CodexAuthBundleError,
- CodexAuthBundleV1,
- auth_bundle_fingerprint,
- materialize_auth_bundle,
- read_auth_bundle,
-)
+from contextlib import asynccontextmanager
+from typing import cast
+
+from backend.data.model import Credentials, OAuth2Credentials
from backend.integrations.codex.credential_codec import (
bundle_from_credentials,
checkpoint_credentials_from_bundle,
)
+from backend.integrations.codex.device_login import (
+ CodexHttpDeviceLogin,
+ start_http_device_login,
+)
+from backend.integrations.codex.http_client import account_snapshot, fetch_models
+from backend.integrations.codex.http_session import (
+ CodexHttpSession,
+ CodexInvocationTimeoutError,
+)
from backend.integrations.codex.models import (
CodexAccountSnapshot,
- CodexDeviceCodeDetails,
CodexDynamicToolCall,
CodexDynamicToolResult,
CodexDynamicToolSpec,
CodexInvocationRequest,
CodexInvocationResult,
- CodexLoginCompletion,
CodexModelInfo,
CodexRateLimitsSnapshot,
+ CodexStreamEvent,
)
-from backend.integrations.codex.runtime import CODEX_RUNTIME_VERSION, CodexRuntime
-from backend.integrations.codex.temporary_home import TemporaryCodexHome
from backend.integrations.credential_lease import CredentialLease
+from backend.integrations.creds_manager import IntegrationCredentialsManager
from backend.util.settings import Settings
logger = logging.getLogger(__name__)
-
-class CodexTransportError(RuntimeError):
- pass
+EventHandler = Callable[[CodexStreamEvent], Awaitable[None]] | None
+ToolHandler = Callable[[CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]]
-class CodexInvocationTimeoutError(CodexTransportError):
+class CodexTransportError(RuntimeError):
pass
@@ -57,1542 +64,331 @@ class CodexTransportOverloadedError(CodexTransportError):
class CodexCredentialBusyError(CodexTransportError):
- pass
+ """Kept for callers that still distinguish it.
+
+ No longer raised by acquisition itself: exclusivity was a property of the
+ subprocess, not of the account.
+ """
class CodexCredentialIntegrityError(CodexTransportError):
pass
-ResultT = TypeVar("ResultT")
-
-
-class RuntimeLogin(Protocol):
- details: CodexDeviceCodeDetails
-
- async def wait(self) -> bool: ...
-
- async def cancel(self) -> None: ...
-
-
-class RuntimeClient(Protocol):
- @property
- def closed(self) -> bool: ...
-
- async def start_device_code_login(self) -> RuntimeLogin: ...
+__all__ = [
+ "CodexAgentSession",
+ "CodexCredentialBusyError",
+ "CodexCredentialIntegrityError",
+ "CodexHttpDeviceLogin",
+ "CodexInvocationTimeoutError",
+ "CodexTransport",
+ "CodexTransportError",
+ "CodexTransportOverloadedError",
+ "get_codex_transport",
+]
- async def account(
- self,
- *,
- refresh_token: bool = True,
- ) -> CodexAccountSnapshot: ...
- async def rate_limits(self) -> CodexRateLimitsSnapshot: ...
+def _validated_codex_credentials(credentials: Credentials) -> OAuth2Credentials:
+ if credentials.type != "oauth2":
+ raise CodexTransportError("Codex transport requires OAuth credentials")
+ oauth_credentials = cast(OAuth2Credentials, credentials)
+ # Rejects a credential whose provider state cannot be read, before it is
+ # used for anything that costs the user money.
+ bundle_from_credentials(oauth_credentials)
+ return oauth_credentials
- async def models(self) -> list[CodexModelInfo]: ...
- async def invoke(
- self,
- request: CodexInvocationRequest,
- ) -> CodexInvocationResult: ...
+def codex_credentials(
+ lease: CredentialLease | CodexCredentialLease,
+) -> OAuth2Credentials:
+ return _validated_codex_credentials(lease.credentials)
- async def invoke_agent(
- self,
- request: CodexInvocationRequest,
- dynamic_tools: list[CodexDynamicToolSpec],
- tool_handler: Callable[
- [CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]
- ],
- event_handler: Callable[[Notification], Awaitable[None]] | None = None,
- *,
- tool_timeout_seconds: float,
- ) -> CodexInvocationResult: ...
- async def logout(self) -> None: ...
-
- async def close(self) -> None: ...
+def resolve_invocation_model(
+ requested_model: str | None, models: list[CodexModelInfo]
+) -> str:
+ available = {model.model: model for model in models}
+ if requested_model is not None:
+ if requested_model not in available:
+ raise CodexTransportError(
+ f"Codex model is not available for this account: {requested_model}"
+ )
+ return requested_model
+ default = next((model for model in models if model.is_default), None)
+ visible = next((model for model in models if not model.hidden), None)
+ selected = default or visible or (models[0] if models else None)
+ if selected is None:
+ raise CodexTransportError("Codex account advertised no available models")
+ return selected.model
-RuntimeFactory = Callable[[TemporaryCodexHome], Awaitable[RuntimeClient]]
+class CodexAgentSession:
+ """An open Codex turn for one credential."""
+ def __init__(self, session: CodexHttpSession, credentials: OAuth2Credentials):
+ self._session = session
+ self._credentials = credentials
-class CodexAgentSession:
- def __init__(
- self,
- runtime: "_LeasedRuntime",
- *,
- turn_timeout_seconds: float,
- tool_timeout_seconds: float,
- ) -> None:
- self._runtime = runtime
- self._turn_timeout_seconds = turn_timeout_seconds
- self._tool_timeout_seconds = tool_timeout_seconds
+ @property
+ def credential_id(self) -> str:
+ return self._credentials.id
async def invoke(
self,
request: CodexInvocationRequest,
dynamic_tools: list[CodexDynamicToolSpec],
- tool_handler: Callable[
- [CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]
- ],
- event_handler: Callable[[Notification], Awaitable[None]] | None = None,
+ tool_handler: ToolHandler,
+ event_handler: EventHandler = None,
) -> CodexInvocationResult:
- try:
- result = await asyncio.wait_for(
- self._runtime.run(
- self._runtime.runtime.invoke_agent(
- request,
- dynamic_tools,
- tool_handler,
- event_handler,
- tool_timeout_seconds=self._tool_timeout_seconds,
- )
- ),
- timeout=self._turn_timeout_seconds,
- )
- return result.model_copy(
- update={"resolved_model": result.resolved_model or request.model}
- )
- except asyncio.TimeoutError:
- raise CodexInvocationTimeoutError("codex_copilot_turn_timeout") from None
+ result = await self._session.invoke(
+ request, dynamic_tools, tool_handler, event_handler
+ )
+ return result.model_copy(
+ update={"resolved_model": result.resolved_model or request.model}
+ )
async def models(self) -> list[CodexModelInfo]:
- return await self._runtime.run(self._runtime.runtime.models())
+ return await fetch_models(self._credentials)
+
+ @property
+ def rate_limits(self) -> CodexRateLimitsSnapshot | None:
+ return self._session.rate_limits
@property
def closed(self) -> bool:
- return self._runtime.runtime.closed
+ # Nothing is held open between turns over HTTP.
+ return False
@property
def failure(self) -> BaseException | None:
- return self._runtime.failure
+ return None
+
+class CodexCredentialLease:
+ """An immutable Codex credential snapshot plus the session that spends it.
+
+ Mirrors the surface the copilot executor and gateway already use, so
+ swapping the process out did not change their call sites. HTTP turns do
+ not hold the credentials-manager lock: refresh is serialized before this
+ snapshot is created, then concurrent turns can safely share the resulting
+ access token.
+ """
-class CodexDeviceLoginSession:
def __init__(
- self,
- home: TemporaryCodexHome,
- runtime: RuntimeClient,
- login: RuntimeLogin,
- timeout_seconds: float,
- cleanup_timeout_seconds: float,
- release_capacity: Callable[[], None],
+ self, credentials: OAuth2Credentials, session: CodexAgentSession
) -> None:
- self.details = login.details
- self._home = home
- self._runtime = runtime
- self._login = login
- self._timeout_seconds = timeout_seconds
- self._cleanup_timeout_seconds = cleanup_timeout_seconds
- self._release_capacity = release_capacity
- self._closed = False
- self._close_lock = asyncio.Lock()
-
- async def wait(self) -> CodexLoginCompletion:
- try:
- completed = await asyncio.wait_for(
- self._login.wait(),
- timeout=self._timeout_seconds,
- )
- if not completed:
- raise CodexTransportError("Codex device login failed")
- bundle = read_auth_bundle(self._home.auth_path, CODEX_RUNTIME_VERSION)
- return CodexLoginCompletion(bundle=bundle)
- except asyncio.TimeoutError:
- raise CodexTransportError("Codex device login expired") from None
- finally:
- await self.close()
+ self._credentials = credentials
+ self._session = session
- async def cancel(self) -> None:
- try:
- await _bounded_phase(
- self._login.cancel(),
- self._cleanup_timeout_seconds,
- "Codex device login cancellation",
- )
- finally:
- await self.close()
+ @property
+ def credentials(self) -> OAuth2Credentials:
+ return self._credentials
- async def close(self) -> None:
- async with self._close_lock:
- if self._closed:
- return
- try:
- await _bounded_phase(
- self._runtime.close(),
- self._cleanup_timeout_seconds,
- "Codex login runtime shutdown",
- )
- finally:
- try:
- self._home.cleanup()
- finally:
- self._release_capacity()
- self._closed = True
+ async def models(self) -> list[CodexModelInfo]:
+ return await self._session.models()
+
+ async def invoke(
+ self,
+ request: CodexInvocationRequest,
+ dynamic_tools: list[CodexDynamicToolSpec],
+ tool_handler: ToolHandler,
+ event_handler: EventHandler = None,
+ ) -> CodexInvocationResult:
+ return await self._session.invoke(
+ request, dynamic_tools, tool_handler, event_handler
+ )
+
+ async def release(self) -> None:
+ # Kept for the executor's common lease lifecycle. No lock survives
+ # acquire_runtime_lease(), so concurrent HTTP turns never serialize.
+ return None
class CodexTransport:
def __init__(
self,
*,
- temp_root: Path | None = None,
- max_active_processes: int = 4,
- capacity_timeout_seconds: float = 10,
- startup_timeout_seconds: float = 30,
- control_timeout_seconds: float = 60,
invocation_timeout_seconds: float = 180,
copilot_turn_timeout_seconds: float = 21600,
copilot_tool_timeout_seconds: float = 900,
login_timeout_seconds: float = 900,
- checkpoint_interval_seconds: float = 0.25,
- runtime_factory: RuntimeFactory = CodexRuntime.start,
) -> None:
- self._temp_root = temp_root
- self._capacity = threading.BoundedSemaphore(max_active_processes)
- self._capacity_timeout_seconds = capacity_timeout_seconds
- self._startup_timeout_seconds = startup_timeout_seconds
- self._control_timeout_seconds = control_timeout_seconds
self._invocation_timeout_seconds = invocation_timeout_seconds
self._copilot_turn_timeout_seconds = copilot_turn_timeout_seconds
self._copilot_tool_timeout_seconds = copilot_tool_timeout_seconds
self._login_timeout_seconds = login_timeout_seconds
- self._checkpoint_interval_seconds = checkpoint_interval_seconds
- self._runtime_factory = runtime_factory
- self._runtime_pool = _CodexRuntimePool(self)
-
- async def start_device_login(self) -> CodexDeviceLoginSession:
- await self._acquire_capacity()
- home: TemporaryCodexHome | None = None
- runtime: RuntimeClient | None = None
- try:
- home = TemporaryCodexHome.create(self._temp_root)
- runtime = await _bounded_phase(
- self._runtime_factory(home),
- self._startup_timeout_seconds,
- "Codex runtime startup",
- )
- login = await _bounded_phase(
- runtime.start_device_code_login(),
- self._control_timeout_seconds,
- "Codex device login startup",
- )
- return CodexDeviceLoginSession(
- home,
- runtime,
- login,
- self._login_timeout_seconds,
- self._control_timeout_seconds,
- self._capacity.release,
- )
- except BaseException:
+ # Last quota reading per credential. Process-local on purpose: it is a
+ # display value, and paying for an inference call to refresh it would
+ # cost the user real quota.
+ self._last_rate_limits: dict[str, CodexRateLimitsSnapshot] = {}
+
+ async def start_device_login(self) -> CodexHttpDeviceLogin:
+ return await start_http_device_login(self._login_timeout_seconds)
+
+ async def acquire_runtime_lease(
+ self, user_id: str, credential_id: str, *, lock_timeout_seconds: float
+ ) -> CodexCredentialLease:
+ manager = IntegrationCredentialsManager()
+
+ async def load() -> Credentials | None:
try:
- if runtime is not None:
- await _bounded_phase(
- runtime.close(),
- self._control_timeout_seconds,
- "Codex runtime shutdown",
+ return await asyncio.wait_for(
+ manager.get(user_id, credential_id),
+ timeout=lock_timeout_seconds,
+ )
+ except asyncio.TimeoutError:
+ raise CodexCredentialBusyError("codex_credential_busy") from None
+
+ credentials = await load()
+ if credentials is None:
+ raise CodexTransportError("Codex credential not found")
+ oauth_credentials = _validated_codex_credentials(credentials)
+
+ if oauth_credentials.refresh_strategy == "provider_runtime":
+ # One-time migration from the removed CLI runtime. Hold the legacy
+ # lease only long enough to rewrite refresh ownership; never for
+ # the inference turn itself.
+ try:
+ legacy_lease = await asyncio.wait_for(
+ manager.acquire_lease(user_id, credential_id),
+ timeout=lock_timeout_seconds,
+ )
+ except asyncio.TimeoutError:
+ raise CodexCredentialBusyError("codex_credential_busy") from None
+ try:
+ current = _validated_codex_credentials(legacy_lease.credentials)
+ if current.refresh_strategy == "provider_runtime":
+ migrated = checkpoint_credentials_from_bundle(
+ current, bundle_from_credentials(current)
)
+ try:
+ await asyncio.wait_for(
+ legacy_lease.checkpoint(migrated),
+ timeout=lock_timeout_seconds,
+ )
+ except asyncio.TimeoutError:
+ raise CodexCredentialBusyError(
+ "codex_credential_busy"
+ ) from None
finally:
- try:
- if home is not None:
- home.cleanup()
- finally:
- self._capacity.release()
- raise
+ await legacy_lease.release()
- async def acquire_runtime_lease(
- self,
- user_id: str,
- credential_id: str,
- *,
- lock_timeout_seconds: float,
- ) -> "PooledCodexRuntimeLease":
- return await self._runtime_pool.acquire(
- user_id,
- credential_id,
- lock_timeout_seconds=lock_timeout_seconds,
+ credentials = await load()
+ if credentials is None:
+ raise CodexTransportError("Codex credential not found")
+ oauth_credentials = _validated_codex_credentials(credentials)
+
+ return CodexCredentialLease(
+ oauth_credentials, self._session_for(oauth_credentials)
)
async def close_runtime_pool(self) -> None:
- await self._runtime_pool.close()
-
- async def _acquire_capacity(self) -> None:
- loop = asyncio.get_running_loop()
- deadline = loop.time() + self._capacity_timeout_seconds
- while not self._capacity.acquire(blocking=False):
- if loop.time() >= deadline:
- raise CodexTransportOverloadedError(
- "Codex transport has no free process capacity"
- )
- await asyncio.sleep(0.01)
+ """Nothing is pooled; kept so shutdown call sites stay unchanged."""
async def account(self, lease: CredentialLease) -> CodexAccountSnapshot:
- return await self._run_control_operation(
- "account lookup",
- self._account(lease),
- )
-
- async def _account(self, lease: CredentialLease) -> CodexAccountSnapshot:
- async with self._credential_runtime(lease) as session:
- return await session.run(session.runtime.account(refresh_token=True))
-
- async def rate_limits(
- self,
- lease: CredentialLease,
- ) -> CodexRateLimitsSnapshot:
- return await self._run_control_operation(
- "rate-limit lookup",
- self._rate_limits(lease),
- )
-
- async def _rate_limits(
- self,
- lease: CredentialLease,
- ) -> CodexRateLimitsSnapshot:
- async with self._credential_runtime(lease) as session:
- return await session.run(session.runtime.rate_limits())
+ return account_snapshot(codex_credentials(lease))
+
+ async def rate_limits(self, lease: CredentialLease) -> CodexRateLimitsSnapshot:
+ """Report the quota last seen for this credential.
+
+ ChatGPT only reports quota on the headers of an inference response --
+ there is no standalone endpoint, and ``/models`` carries none. So this
+ answers with what the most recent turn saw, and an empty snapshot (every
+ field ``None``) when this worker has not run one yet. That reads as
+ "not known yet", which is true; failing the request instead would make a
+ never-used connection look broken.
+ """
+ credentials = codex_credentials(lease)
+ return self._last_rate_limits.get(credentials.id, CodexRateLimitsSnapshot())
+
+ def _record_rate_limits(self, session: "CodexAgentSession") -> None:
+ seen = session.rate_limits
+ if seen is not None:
+ self._last_rate_limits[session.credential_id] = seen
async def models(self, lease: CredentialLease) -> list[CodexModelInfo]:
- return await self._run_control_operation(
- "model lookup",
- self._models(lease),
- )
-
- async def _models(self, lease: CredentialLease) -> list[CodexModelInfo]:
- async with self._credential_runtime(lease) as session:
- return await session.run(session.runtime.models())
+ return await fetch_models(codex_credentials(lease))
async def invoke(
- self,
- lease: CredentialLease,
- request: CodexInvocationRequest,
+ self, lease: CredentialLease, request: CodexInvocationRequest
) -> CodexInvocationResult:
- timeout = request.timeout_seconds or self._invocation_timeout_seconds
+ credentials = codex_credentials(lease)
+ models = await fetch_models(credentials)
+ resolved_model = resolve_invocation_model(request.model, models)
+ session = self._session_for(
+ credentials, turn_timeout=self._invocation_timeout_seconds
+ )
try:
- return await asyncio.wait_for(
- self._invoke_with_runtime(lease, request),
- timeout=timeout,
- )
- except asyncio.TimeoutError:
- raise CodexInvocationTimeoutError(
- f"Codex invocation exceeded {timeout:g} seconds"
- ) from None
-
- async def _invoke_with_runtime(
- self,
- lease: CredentialLease,
- request: CodexInvocationRequest,
- ) -> CodexInvocationResult:
- async with self._credential_runtime(lease) as session:
- await session.run(session.runtime.account(refresh_token=True))
- await session.checkpoint_now()
- models = await session.run(session.runtime.models())
- resolved_model = _resolve_invocation_model(request.model, models)
- result = await session.run(
- session.runtime.invoke(
- request.model_copy(update={"model": resolved_model})
- )
+ result = await session.invoke(
+ request.model_copy(update={"model": resolved_model}), [], _no_tools
)
return result.model_copy(update={"resolved_model": resolved_model})
+ finally:
+ self._record_rate_limits(session)
async def invoke_agent(
self,
lease: CredentialLease,
request: CodexInvocationRequest,
dynamic_tools: list[CodexDynamicToolSpec],
- tool_handler: Callable[
- [CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]
- ],
- event_handler: Callable[[Notification], Awaitable[None]] | None = None,
+ tool_handler: ToolHandler,
+ event_handler: EventHandler = None,
) -> CodexInvocationResult:
+ session = self._session_for(codex_credentials(lease))
try:
- return await asyncio.wait_for(
- self._invoke_agent_with_runtime(
- lease,
- request,
- dynamic_tools,
- tool_handler,
- event_handler,
- ),
- timeout=self._copilot_turn_timeout_seconds,
+ return await session.invoke(
+ request, dynamic_tools, tool_handler, event_handler
)
- except asyncio.TimeoutError:
- raise CodexInvocationTimeoutError("codex_copilot_turn_timeout") from None
+ finally:
+ self._record_rate_limits(session)
@asynccontextmanager
async def agent_session(
- self,
- lease: CredentialLease,
+ self, lease: CredentialLease
) -> AsyncIterator[CodexAgentSession]:
- async with self._credential_runtime(lease) as session:
- await session.run(session.runtime.account(refresh_token=True))
- await session.checkpoint_now()
- yield CodexAgentSession(
- session,
- turn_timeout_seconds=self._copilot_turn_timeout_seconds,
- tool_timeout_seconds=self._copilot_tool_timeout_seconds,
- )
-
- async def _invoke_agent_with_runtime(
- self,
- lease: CredentialLease,
- request: CodexInvocationRequest,
- dynamic_tools: list[CodexDynamicToolSpec],
- tool_handler: Callable[
- [CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]
- ],
- event_handler: Callable[[Notification], Awaitable[None]] | None,
- ) -> CodexInvocationResult:
- async with self._credential_runtime(lease) as session:
- await session.run(session.runtime.account(refresh_token=True))
- await session.checkpoint_now()
- return await session.run(
- session.runtime.invoke_agent(
- request,
- dynamic_tools,
- tool_handler,
- event_handler,
- tool_timeout_seconds=self._copilot_tool_timeout_seconds,
- )
- )
-
- async def logout(self, lease: CredentialLease) -> None:
- await self._run_control_operation("logout", self._logout(lease))
-
- async def _logout(self, lease: CredentialLease) -> None:
- async with self._credential_runtime(lease, checkpoint=False) as session:
- await session.run(session.runtime.logout())
-
- async def _run_control_operation(
- self,
- label: str,
- operation: Awaitable[ResultT],
- ) -> ResultT:
- return await _bounded_phase(
- operation,
- self._control_timeout_seconds,
- f"Codex {label}",
- )
-
- @asynccontextmanager
- async def _credential_runtime(
- self,
- lease: CredentialLease,
- *,
- checkpoint: bool = True,
- ) -> AsyncIterator["_LeasedRuntime"]:
- credentials = _codex_credentials(lease)
- bundle = bundle_from_credentials(credentials)
- await self._acquire_capacity()
- home: TemporaryCodexHome | None = None
- runtime: RuntimeClient | None = None
- session: _LeasedRuntime | None = None
+ session = self._session_for(codex_credentials(lease))
try:
- home = TemporaryCodexHome.create(self._temp_root)
- materialize_auth_bundle(bundle, home.auth_path)
- runtime = await _bounded_phase(
- self._runtime_factory(home),
- self._startup_timeout_seconds,
- "Codex runtime startup",
- )
- state = _AuthCheckpointState(credentials=credentials, bundle=bundle)
- session = _LeasedRuntime(
- runtime=runtime,
- lease=lease,
- state=state,
- home=home,
- checkpoint_timeout_seconds=self._control_timeout_seconds,
- checkpoint_interval_seconds=self._checkpoint_interval_seconds,
- monitor_auth=checkpoint,
- )
- session.start()
yield session
finally:
- try:
- if session is not None:
- await session.stop()
- finally:
- try:
- if runtime is not None:
- await _bounded_phase(
- runtime.close(),
- self._control_timeout_seconds,
- "Codex runtime shutdown",
- )
- finally:
- try:
- if checkpoint and session is not None:
- await session.checkpoint_now(validate_unchanged=True)
- finally:
- try:
- if home is not None:
- home.cleanup()
- finally:
- self._capacity.release()
-
-
-_CODEX_TRANSPORT_SINGLETON_LOCK = threading.Lock()
-_codex_transport_singleton: CodexTransport | None = None
-
-
-def get_codex_transport() -> CodexTransport:
- global _codex_transport_singleton
- if _codex_transport_singleton is not None:
- return _codex_transport_singleton
- with _CODEX_TRANSPORT_SINGLETON_LOCK:
- if _codex_transport_singleton is None:
- config = Settings().config
- temp_root = Path(config.codex_temp_root) if config.codex_temp_root else None
- _codex_transport_singleton = CodexTransport(
- temp_root=temp_root,
- max_active_processes=config.codex_max_active_processes,
- capacity_timeout_seconds=config.codex_capacity_timeout_seconds,
- startup_timeout_seconds=config.codex_startup_timeout_seconds,
- control_timeout_seconds=config.codex_control_timeout_seconds,
- invocation_timeout_seconds=config.codex_invocation_timeout_seconds,
- copilot_turn_timeout_seconds=config.codex_copilot_turn_timeout_seconds,
- copilot_tool_timeout_seconds=config.codex_copilot_tool_timeout_seconds,
- login_timeout_seconds=config.codex_login_timeout_seconds,
- checkpoint_interval_seconds=(
- config.codex_auth_checkpoint_interval_seconds
- ),
- )
- return _codex_transport_singleton
-
-
-def _codex_credentials(lease: CredentialLease) -> OAuth2Credentials:
- credentials = lease.credentials
- if credentials.type != "oauth2":
- raise CodexTransportError("Codex transport requires OAuth credentials")
- oauth_credentials = cast(OAuth2Credentials, credentials)
- bundle_from_credentials(oauth_credentials)
- return oauth_credentials
-
-
-def _resolve_invocation_model(
- requested_model: str | None,
- models: list[CodexModelInfo],
-) -> str:
- available = {model.model: model for model in models}
- if requested_model is not None:
- if requested_model not in available:
- raise CodexTransportError(
- f"Codex model is not available for this account: {requested_model}"
- )
- return requested_model
- default = next((model for model in models if model.is_default), None)
- visible = next((model for model in models if not model.hidden), None)
- selected = default or visible or (models[0] if models else None)
- if selected is None:
- raise CodexTransportError("Codex account advertised no available models")
- return selected.model
-
-
-class _AuthCheckpointState(BaseModel):
- model_config = ConfigDict(arbitrary_types_allowed=True)
-
- credentials: OAuth2Credentials
- bundle: CodexAuthBundleV1
- lock: asyncio.Lock = Field(default_factory=asyncio.Lock)
-
-
-class _LeasedRuntime:
- def __init__(
- self,
- *,
- runtime: RuntimeClient,
- lease: CredentialLease,
- state: _AuthCheckpointState,
- home: TemporaryCodexHome,
- checkpoint_timeout_seconds: float,
- checkpoint_interval_seconds: float,
- monitor_auth: bool,
- ) -> None:
- self.runtime = runtime
- self._lease = lease
- self._state = state
- self._home = home
- self._checkpoint_timeout_seconds = checkpoint_timeout_seconds
- self._checkpoint_interval_seconds = checkpoint_interval_seconds
- self._monitor_auth = monitor_auth
- self._monitor_task: asyncio.Task[None] | None = None
-
- def start(self) -> None:
- if self._monitor_auth:
- self._monitor_task = asyncio.create_task(self._monitor_auth_file())
-
- async def run(self, operation: Awaitable[ResultT]) -> ResultT:
- return await _run_with_lease_guard(
- self._lease,
- operation,
- monitor_task=self._monitor_task,
- )
-
- @property
- def failure(self) -> BaseException | None:
- monitor = self._monitor_task
- if monitor is not None and monitor.done() and not monitor.cancelled():
- try:
- if error := monitor.exception():
- return error
- except asyncio.CancelledError:
- pass
- return self._lease.failure
-
- async def checkpoint_now(self, *, validate_unchanged: bool = True) -> None:
- async with self._state.lock:
- try:
- after = await _read_runtime_auth(self._home.auth_path)
- except asyncio.CancelledError:
- raise
- except BaseException as error:
- raise CodexCredentialIntegrityError(
- "codex_credential_state_unavailable"
- ) from error
- if auth_bundle_fingerprint(after) == auth_bundle_fingerprint(
- self._state.bundle
- ):
- if validate_unchanged:
- try:
- await _bounded_phase(
- self._lease.validate(),
- self._checkpoint_timeout_seconds,
- "Codex credential lease validation",
- )
- except asyncio.CancelledError:
- raise
- except BaseException as error:
- raise CodexCredentialIntegrityError(
- f"codex_credential_lease_lost: {error}"
- ) from error
- return
- updated = checkpoint_credentials_from_bundle(
- self._state.credentials,
- after,
- )
- try:
- await _bounded_phase(
- self._lease.checkpoint(updated),
- self._checkpoint_timeout_seconds,
- "Codex credential checkpoint",
- )
- except asyncio.CancelledError:
- raise
- except BaseException as error:
- raise CodexCredentialIntegrityError(
- "codex_credential_checkpoint_failed"
- ) from error
- self._state.credentials = updated
- self._state.bundle = after
-
- async def stop(self) -> None:
- monitor = self._monitor_task
- if monitor is None:
- return
- self._monitor_task = None
- monitor.cancel()
- results = await asyncio.gather(monitor, return_exceptions=True)
- error = results[0]
- if isinstance(error, BaseException) and not isinstance(
- error, asyncio.CancelledError
- ):
- raise error
-
- async def _monitor_auth_file(self) -> None:
- while True:
- await asyncio.sleep(self._checkpoint_interval_seconds)
- await self.checkpoint_now(validate_unchanged=False)
-
-
-class _CodexRuntimeActor:
- def __init__(
- self,
- *,
- lease: CredentialLease,
- context: AbstractAsyncContextManager[CodexAgentSession],
- session: CodexAgentSession,
- ) -> None:
- self.lease = lease
- self.context = context
- self.session = session
- self.handles: set[str] = set()
- self.calls: dict[str, set[asyncio.Task[object]]] = {}
- self.models_task: asyncio.Task[list[CodexModelInfo]] | None = None
- self.failed: BaseException | None = None
-
-
-class PooledCodexRuntimeLease:
- def __init__(
- self,
- *,
- pool: "_CodexRuntimePool",
- key: tuple[str, str],
- handle_id: str,
- credentials: OAuth2Credentials,
- ) -> None:
- self.credentials = credentials
- self._pool = pool
- self._key = key
- self._handle_id = handle_id
- self._release_state = "active"
- self._release_completion: concurrent.futures.Future[None] | None = None
- self._release_lock = threading.Lock()
-
- async def models(self) -> list[CodexModelInfo]:
- self._ensure_active()
- return await self._pool.models(self._key, self._handle_id)
-
- async def invoke(
- self,
- request: CodexInvocationRequest,
- dynamic_tools: list[CodexDynamicToolSpec],
- tool_handler: Callable[
- [CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]
- ],
- event_handler: Callable[[Notification], Awaitable[None]] | None = None,
- ) -> CodexInvocationResult:
- self._ensure_active()
- return await self._pool.invoke(
- self._key,
- self._handle_id,
- request,
- dynamic_tools,
- tool_handler,
- event_handler,
- )
-
- async def release(self) -> None:
- with self._release_lock:
- if self._release_state == "released":
- return
- if self._release_state == "releasing":
- completion = self._release_completion
- leader = False
- else:
- completion = concurrent.futures.Future()
- self._release_completion = completion
- self._release_state = "releasing"
- leader = True
- if completion is None:
- raise RuntimeError("Codex runtime release state is invalid")
- if not leader:
- await asyncio.shield(asyncio.wrap_future(completion))
- return
- try:
- await self._pool.release(self._key, self._handle_id)
- except BaseException as error:
- with self._release_lock:
- self._release_state = "active"
- self._release_completion = None
- completion.set_exception(error)
- raise
- else:
- with self._release_lock:
- self._release_state = "released"
- self._release_completion = None
- completion.set_result(None)
-
- def _ensure_active(self) -> None:
- with self._release_lock:
- if self._release_state != "active":
- raise RuntimeError("Codex runtime lease was released")
-
-
-class _CodexRuntimePool:
- def __init__(self, transport: CodexTransport) -> None:
- self._transport = transport
- self._state_lock = threading.Lock()
- self._loop: asyncio.AbstractEventLoop | None = None
- self._thread: threading.Thread | None = None
- self._loop_ready: threading.Event | None = None
- self._loop_start_error: BaseException | None = None
- self._closing = False
- self._closed = False
- self._generation = 0
- self._close_complete = threading.Event()
- self._close_error: BaseException | None = None
- self._actors: dict[tuple[str, str], _CodexRuntimeActor] = {}
- self._starts: dict[tuple[str, str], asyncio.Task[_CodexRuntimeActor]] = {}
- self._retiring: dict[tuple[str, str], asyncio.Task[None]] = {}
- self._pending: dict[tuple[str, str], set[str]] = {}
-
- async def acquire(
- self,
- user_id: str,
- credential_id: str,
- *,
- lock_timeout_seconds: float,
- ) -> PooledCodexRuntimeLease:
- key = (user_id, credential_id)
- handle_id = uuid4().hex
- loop = self._ensure_loop()
- with self._state_lock:
- if self._closing or self._closed:
- raise CodexTransportError("Codex runtime pool is shutting down")
- generation = self._generation
- operation = self._borrow_on_actor_loop(
- key,
- handle_id,
- generation=generation,
- lock_timeout_seconds=lock_timeout_seconds,
- )
- try:
- future = asyncio.run_coroutine_threadsafe(operation, loop)
- except BaseException:
- operation.close()
- raise
- try:
- credentials = await asyncio.wrap_future(future)
- except asyncio.CancelledError:
- future.cancel()
- with suppress(BaseException):
- cleanup = asyncio.run_coroutine_threadsafe(
- self._release_on_actor_loop(key, handle_id),
- loop,
- )
- await _await_cleanup_future(
- cleanup,
- timeout_seconds=self._cleanup_timeout_seconds,
- label="Codex cancelled runtime borrow",
- )
- raise
- with self._state_lock:
- shutting_down = self._closing or self._closed
- if shutting_down:
- raise CodexTransportError("Codex runtime pool is shutting down")
- return PooledCodexRuntimeLease(
- pool=self,
- key=key,
- handle_id=handle_id,
- credentials=credentials,
- )
-
- async def models(
- self,
- key: tuple[str, str],
- handle_id: str,
- ) -> list[CodexModelInfo]:
- future = asyncio.run_coroutine_threadsafe(
- self._models_on_actor_loop(key, handle_id),
- self._ensure_loop(),
- )
- try:
- return await asyncio.wrap_future(future)
- except asyncio.CancelledError:
- future.cancel()
- raise
-
- async def invoke(
- self,
- key: tuple[str, str],
- handle_id: str,
- request: CodexInvocationRequest,
- dynamic_tools: list[CodexDynamicToolSpec],
- tool_handler: Callable[
- [CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]
- ],
- event_handler: Callable[[Notification], Awaitable[None]] | None,
- ) -> CodexInvocationResult:
- caller_loop = asyncio.get_running_loop()
-
- async def bridged_tool_handler(
- call: CodexDynamicToolCall,
- ) -> CodexDynamicToolResult:
- return await _await_threadsafe_callback(
- tool_handler(call),
- caller_loop,
- )
-
- async def bridged_event_handler(notification: Notification) -> None:
- if event_handler is not None:
- await _await_threadsafe_callback(
- event_handler(notification),
- caller_loop,
- )
+ self._record_rate_limits(session)
- future = asyncio.run_coroutine_threadsafe(
- self._invoke_on_actor_loop(
- key,
- handle_id,
- request,
- dynamic_tools,
- bridged_tool_handler,
- bridged_event_handler if event_handler is not None else None,
+ async def logout(self, lease: CredentialLease) -> None:
+ """OpenAI publishes no revocation endpoint; the caller drops the row."""
+ logger.info("Codex logout requested; dropping the stored credential")
+
+ def _session_for(
+ self, credentials: OAuth2Credentials, *, turn_timeout: float | None = None
+ ) -> CodexAgentSession:
+ return CodexAgentSession(
+ CodexHttpSession(
+ credentials,
+ turn_timeout_seconds=turn_timeout or self._copilot_turn_timeout_seconds,
+ tool_timeout_seconds=self._copilot_tool_timeout_seconds,
),
- self._ensure_loop(),
- )
- try:
- return await asyncio.wrap_future(future)
- except asyncio.CancelledError:
- future.cancel()
- raise
-
- async def release(self, key: tuple[str, str], handle_id: str) -> None:
- loop = self._loop
- if loop is None:
- return
- future = asyncio.run_coroutine_threadsafe(
- self._release_on_actor_loop(key, handle_id),
- loop,
- )
- await _await_cleanup_future(
- future,
- timeout_seconds=self._cleanup_timeout_seconds,
- label="Codex pooled runtime release",
+ credentials,
)
- async def close(self) -> None:
- close_complete = self._close_complete
- close_error: BaseException | None = None
- already_complete = False
- leader = False
- with self._state_lock:
- if self._closed:
- close_error = self._close_error
- already_complete = True
- elif self._closing:
- pass
- else:
- self._closing = True
- self._generation += 1
- leader = True
- loop = self._loop
- thread = self._thread
- ready = self._loop_ready
- if already_complete:
- if close_error is not None:
- raise close_error
- return
- if not leader:
- await self._wait_for_close(close_complete)
- return
- close_error, loop = await self._close_as_leader(loop, thread, ready)
- with self._state_lock:
- if self._loop is loop:
- self._loop = None
- self._thread = None
- self._loop_ready = None
- self._close_error = close_error
- self._closed = True
- close_complete.set()
- if close_error is not None:
- raise close_error
-
- async def _wait_for_close(self, close_complete: threading.Event) -> None:
- completed = await asyncio.to_thread(
- close_complete.wait,
- self._cleanup_timeout_seconds,
- )
- if not completed:
- raise CodexTransportError("Codex runtime pool shutdown timed out")
- with self._state_lock:
- close_error = self._close_error
- if close_error is not None:
- raise close_error
-
- async def _close_as_leader(
- self,
- loop: asyncio.AbstractEventLoop | None,
- thread: threading.Thread | None,
- ready: threading.Event | None,
- ) -> tuple[BaseException | None, asyncio.AbstractEventLoop | None]:
- close_error: BaseException | None = None
- try:
- loop = await self._resolve_shutdown_loop(loop, thread, ready)
- if loop is not None and not loop.is_closed():
- future = asyncio.run_coroutine_threadsafe(
- self._close_all_on_actor_loop(), loop
- )
- await _await_cleanup_future(
- future,
- timeout_seconds=self._cleanup_timeout_seconds,
- label="Codex runtime pool shutdown",
- )
- except BaseException as error:
- close_error = error
- stop_error = await self._stop_actor_loop(loop, thread)
- if close_error is not None and stop_error is not None:
- close_error.add_note(
- f"Actor-loop stop failure: "
- f"{type(stop_error).__name__}: {stop_error}"
- )
- return close_error or stop_error, loop
-
- async def _resolve_shutdown_loop(
- self,
- loop: asyncio.AbstractEventLoop | None,
- thread: threading.Thread | None,
- ready: threading.Event | None,
- ) -> asyncio.AbstractEventLoop | None:
- if thread is None or loop is not None:
- return loop
- if ready is None or not await asyncio.to_thread(
- ready.wait,
- self._cleanup_timeout_seconds,
- ):
- raise CodexTransportError(
- "Codex runtime pool loop startup timed out during shutdown"
- )
- with self._state_lock:
- loop = self._loop
- start_error = self._loop_start_error
- if start_error is not None:
- raise CodexTransportError(
- "Codex runtime pool loop failed to start"
- ) from start_error
- return loop
-
- async def _stop_actor_loop(
- self,
- loop: asyncio.AbstractEventLoop | None,
- thread: threading.Thread | None,
- ) -> BaseException | None:
- try:
- if loop is not None and not loop.is_closed():
- loop.call_soon_threadsafe(loop.stop)
- if thread is None:
- return None
- await asyncio.to_thread(thread.join, self._cleanup_timeout_seconds)
- if thread.is_alive():
- return CodexTransportError("Codex runtime pool loop did not stop")
- except BaseException as error:
- return error
- return None
- @property
- def _cleanup_timeout_seconds(self) -> float:
- return max(self._transport._control_timeout_seconds * 3, 5)
-
- def _ensure_loop(self) -> asyncio.AbstractEventLoop:
- with self._state_lock:
- if self._closing or self._closed:
- raise CodexTransportError("Codex runtime pool is shutting down")
- if self._loop is not None:
- return self._loop
- ready = self._loop_ready
- if ready is None:
- ready = threading.Event()
- self._loop_ready = ready
- self._loop_start_error = None
-
- def run() -> None:
- loop: asyncio.AbstractEventLoop | None = None
- try:
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
- with self._state_lock:
- self._loop = loop
- ready.set()
- loop.run_forever()
- except BaseException as error:
- with self._state_lock:
- self._loop_start_error = error
- ready.set()
- finally:
- if loop is not None and not loop.is_closed():
- loop.close()
-
- thread = threading.Thread(
- target=run,
- name="autogpt-codex-runtime-pool",
- daemon=True,
- )
- self._thread = thread
- thread.start()
- if not ready.wait(self._cleanup_timeout_seconds):
- raise CodexTransportError("Codex runtime pool loop startup timed out")
- with self._state_lock:
- loop = self._loop
- start_error = self._loop_start_error
- shutting_down = self._closing or self._closed
- if start_error is not None:
- raise CodexTransportError(
- "Codex runtime pool loop failed to start"
- ) from start_error
- if shutting_down:
- raise CodexTransportError("Codex runtime pool is shutting down")
- if loop is None:
- raise RuntimeError("Codex runtime pool loop failed to start")
- return loop
-
- async def _borrow_on_actor_loop(
- self,
- key: tuple[str, str],
- handle_id: str,
- *,
- generation: int,
- lock_timeout_seconds: float,
- ) -> OAuth2Credentials:
- self._validate_generation(generation)
- pending = self._pending.setdefault(key, set())
- pending.add(handle_id)
- start: asyncio.Task[_CodexRuntimeActor] | None = None
- try:
- retiring = self._retiring.get(key)
- if retiring is not None:
- try:
- await asyncio.shield(retiring)
- except BaseException:
- raise
- else:
- if self._retiring.get(key) is retiring:
- self._retiring.pop(key, None)
- self._validate_generation(generation)
- actor = self._actors.get(key)
- if (
- actor is not None
- and (failure := self._actor_failure(actor)) is not None
- ):
- raise CodexTransportError(
- "Codex credential runtime is unavailable"
- ) from failure
- if actor is None:
- start = self._starts.get(key)
- if start is None:
- start = asyncio.create_task(
- self._start_actor(
- key,
- lock_timeout_seconds=lock_timeout_seconds,
- )
- )
- self._starts[key] = start
- actor = await asyncio.shield(start)
- with self._state_lock:
- if self._closing or self._closed or generation != self._generation:
- raise CodexTransportError("Codex runtime pool is shutting down")
- actor = self._actors.setdefault(key, actor)
- if self._starts.get(key) is start:
- self._starts.pop(key, None)
- else:
- self._validate_generation(generation)
- actor.handles.add(handle_id)
- return cast(OAuth2Credentials, actor.lease.credentials)
- except BaseException:
- pending.discard(handle_id)
- await self._cancel_unused_start(key, start)
- raise
- finally:
- pending.discard(handle_id)
- if not pending:
- self._pending.pop(key, None)
-
- async def _start_actor(
- self,
- key: tuple[str, str],
- *,
- lock_timeout_seconds: float,
- ) -> _CodexRuntimeActor:
- from backend.integrations.creds_manager import IntegrationCredentialsManager
+async def _no_tools(call: CodexDynamicToolCall) -> CodexDynamicToolResult:
+ return CodexDynamicToolResult(
+ content=f"Tool {call.tool!r} is not available.", success=False
+ )
- user_id, credential_id = key
- try:
- lease = await asyncio.wait_for(
- IntegrationCredentialsManager().acquire_lease(user_id, credential_id),
- timeout=lock_timeout_seconds,
- )
- except asyncio.TimeoutError:
- raise CodexCredentialBusyError("codex_credential_busy") from None
- context: AbstractAsyncContextManager[CodexAgentSession] | None = None
- try:
- _codex_credentials(lease)
- context = self._transport.agent_session(lease)
- session = await context.__aenter__()
- return _CodexRuntimeActor(
- lease=lease,
- context=context,
- session=session,
- )
- except BaseException:
- if context is not None:
- with suppress(BaseException):
- await context.__aexit__(None, None, None)
- await lease.release()
- raise
-
- async def _cancel_unused_start(
- self,
- key: tuple[str, str],
- start: asyncio.Task[_CodexRuntimeActor] | None,
- ) -> None:
- if self._pending.get(key):
- return
- candidate = start or self._starts.get(key)
- if candidate is None:
- return
- if self._starts.get(key) is not candidate:
- return
- if not candidate.done():
- candidate.cancel()
- await asyncio.gather(candidate, return_exceptions=True)
- if self._starts.get(key) is candidate:
- self._starts.pop(key, None)
- if candidate.cancelled():
- return
- try:
- actor = candidate.result()
- except BaseException:
- return
- if not actor.handles:
- try:
- await self._close_actor_bounded(actor)
- except BaseException:
- logger.exception("Failed to clean up unused Codex runtime start")
- async def _models_on_actor_loop(
- self,
- key: tuple[str, str],
- handle_id: str,
- ) -> list[CodexModelInfo]:
- actor = self._active_actor(key, handle_id)
- models_task = actor.models_task
- if models_task is None:
- models_task = asyncio.create_task(actor.session.models())
- actor.models_task = models_task
- try:
- return list(await asyncio.shield(models_task))
- except BaseException as error:
- self._record_actor_failure(actor, error)
- if actor.models_task is models_task and models_task.done():
- actor.models_task = None
- raise
-
- async def _invoke_on_actor_loop(
- self,
- key: tuple[str, str],
- handle_id: str,
- request: CodexInvocationRequest,
- dynamic_tools: list[CodexDynamicToolSpec],
- tool_handler: Callable[
- [CodexDynamicToolCall], Awaitable[CodexDynamicToolResult]
- ],
- event_handler: Callable[[Notification], Awaitable[None]] | None,
- ) -> CodexInvocationResult:
- actor = self._active_actor(key, handle_id)
- task = asyncio.current_task()
- if task is None:
- raise RuntimeError("Codex pooled invocation has no owning task")
- calls = actor.calls.setdefault(handle_id, set())
- calls.add(cast(asyncio.Task[object], task))
- try:
- return await actor.session.invoke(
- request,
- dynamic_tools,
- tool_handler,
- event_handler,
- )
- except BaseException as error:
- self._record_actor_failure(actor, error)
- raise
- finally:
- calls.discard(cast(asyncio.Task[object], task))
- if not calls and actor.calls.get(handle_id) is calls:
- actor.calls.pop(handle_id, None)
+_transport: CodexTransport | None = None
+_transport_lock = threading.Lock()
- async def _release_on_actor_loop(
- self,
- key: tuple[str, str],
- handle_id: str,
- ) -> None:
- actor = self._actors.get(key)
- if actor is None:
- retiring = self._retiring.get(key)
- if retiring is not None:
- await asyncio.shield(retiring)
- return
- if handle_id not in actor.handles:
- return
- actor.handles.discard(handle_id)
- calls = tuple(actor.calls.pop(handle_id, ()))
- for task in calls:
- task.cancel()
- if calls:
- with suppress(asyncio.TimeoutError):
- await asyncio.wait_for(
- asyncio.gather(*calls, return_exceptions=True),
- timeout=self._transport._control_timeout_seconds,
- )
- if actor.handles:
- return
- if self._actors.get(key) is actor:
- self._actors.pop(key, None)
- retiring = asyncio.create_task(self._close_actor_bounded(actor))
- self._retiring[key] = retiring
- try:
- await asyncio.shield(retiring)
- except BaseException:
- raise
- else:
- if self._retiring.get(key) is retiring:
- self._retiring.pop(key, None)
-
- async def _close_actor_bounded(self, actor: _CodexRuntimeActor) -> None:
- models_task, actor.models_task = actor.models_task, None
- if models_task is not None:
- if not models_task.done():
- models_task.cancel()
- await asyncio.gather(models_task, return_exceptions=True)
-
- async def cleanup() -> None:
- context_error: BaseException | None = None
- try:
- await actor.context.__aexit__(None, None, None)
- except BaseException as error:
- context_error = error
- try:
- await actor.lease.release()
- except BaseException as release_error:
- if context_error is None:
- raise
- context_error.add_note(
- f"Credential lease release also failed: {release_error}"
- )
- if context_error is not None:
- raise context_error
- task = asyncio.create_task(cleanup())
- try:
- await asyncio.wait_for(
- asyncio.shield(task),
- timeout=self._cleanup_timeout_seconds,
- )
- except BaseException:
- task.cancel()
- await _cancel_future_bounded(task)
- raise
-
- async def _close_all_on_actor_loop(self) -> None:
- errors: list[BaseException] = []
- starts = tuple(self._starts.values())
- self._starts.clear()
- for start in starts:
- start.cancel()
- started_actors: list[_CodexRuntimeActor] = []
- if starts:
- for result in await asyncio.gather(*starts, return_exceptions=True):
- if isinstance(result, _CodexRuntimeActor):
- started_actors.append(result)
- elif isinstance(result, BaseException) and not isinstance(
- result, asyncio.CancelledError
- ):
- errors.append(result)
- actors_by_id = {
- id(actor): actor for actor in (*self._actors.values(), *started_actors)
- }
- actors = tuple(actors_by_id.values())
- self._actors.clear()
- self._pending.clear()
- for actor in actors:
- for calls in actor.calls.values():
- for task in calls:
- task.cancel()
- calls = tuple(
- {
- id(task): task
- for actor in actors
- for group in actor.calls.values()
- for task in group
- }.values()
- )
- if calls:
- await asyncio.gather(*calls, return_exceptions=True)
-
- cleanup_tasks = [
- asyncio.create_task(self._close_actor_bounded(actor)) for actor in actors
- ]
- cleanup_tasks.extend(
- {id(task): task for task in self._retiring.values()}.values()
- )
- self._retiring.clear()
- if cleanup_tasks:
- for result in await asyncio.gather(
- *{id(task): task for task in cleanup_tasks}.values(),
- return_exceptions=True,
- ):
- if isinstance(result, BaseException) and not isinstance(
- result, asyncio.CancelledError
- ):
- errors.append(result)
- if errors:
- cleanup_error = CodexTransportError(
- f"Codex runtime pool cleanup failed ({len(errors)} error(s))"
- )
- for additional_error in errors[1:]:
- cleanup_error.add_note(
- f"Additional cleanup failure: "
- f"{type(additional_error).__name__}: {additional_error}"
+def get_codex_transport() -> CodexTransport:
+ global _transport
+ if _transport is None:
+ with _transport_lock:
+ if _transport is None:
+ config = Settings().config
+ _transport = CodexTransport(
+ invocation_timeout_seconds=config.codex_invocation_timeout_seconds,
+ copilot_turn_timeout_seconds=config.codex_copilot_turn_timeout_seconds,
+ copilot_tool_timeout_seconds=config.codex_copilot_tool_timeout_seconds,
+ login_timeout_seconds=config.codex_login_timeout_seconds,
)
- raise cleanup_error from errors[0]
-
- def _active_actor(
- self,
- key: tuple[str, str],
- handle_id: str,
- ) -> _CodexRuntimeActor:
- actor = self._actors.get(key)
- if actor is None or handle_id not in actor.handles:
- raise RuntimeError("Codex runtime lease is no longer active")
- if failure := self._actor_failure(actor):
- raise CodexTransportError(
- "Codex credential runtime is unavailable"
- ) from failure
- return actor
-
- @staticmethod
- def _actor_failure(actor: _CodexRuntimeActor) -> BaseException | None:
- failure = actor.failed or actor.session.failure
- if failure is not None:
- actor.failed = failure
- return failure
-
- def _record_actor_failure(
- self,
- actor: _CodexRuntimeActor,
- error: BaseException,
- ) -> None:
- failure = actor.session.failure
- if (
- isinstance(error, CodexCredentialIntegrityError)
- or failure is not None
- or actor.session.closed
- ):
- actor.failed = failure or error
-
- def _validate_generation(self, generation: int) -> None:
- with self._state_lock:
- if self._closing or self._closed or generation != self._generation:
- raise CodexTransportError("Codex runtime pool is shutting down")
-
-
-async def _await_threadsafe_callback(
- operation: Awaitable[ResultT],
- loop: asyncio.AbstractEventLoop,
-) -> ResultT:
- async def await_operation() -> ResultT:
- return await operation
-
- future = asyncio.run_coroutine_threadsafe(await_operation(), loop)
- try:
- return await asyncio.wrap_future(future)
- except asyncio.CancelledError:
- future.cancel()
- raise
-
-
-async def _await_cleanup_future(
- future: concurrent.futures.Future[ResultT],
- *,
- timeout_seconds: float,
- label: str,
-) -> ResultT:
- wrapped = asyncio.wrap_future(future)
- try:
- return await asyncio.wait_for(
- asyncio.shield(wrapped),
- timeout=timeout_seconds,
- )
- except asyncio.CancelledError:
- with suppress(BaseException):
- await asyncio.wait_for(
- asyncio.shield(wrapped),
- timeout=timeout_seconds,
- )
- raise
- except asyncio.TimeoutError:
- future.cancel()
- raise CodexTransportError(f"{label} timed out") from None
-
-
-async def _run_with_lease_guard(
- lease: CredentialLease,
- operation: Awaitable[ResultT],
- *,
- monitor_task: asyncio.Task[None] | None = None,
-) -> ResultT:
- if failure := lease.failure:
- _close_unused_awaitable(operation)
- raise RuntimeError("Credential lease heartbeat failed") from failure
- if monitor_task is not None and monitor_task.done():
- _close_unused_awaitable(operation)
- if monitor_task.cancelled():
- raise CodexCredentialIntegrityError("codex_credential_monitor_stopped")
- if error := monitor_task.exception():
- raise error
- raise CodexCredentialIntegrityError("codex_credential_monitor_stopped")
- operation_task = asyncio.ensure_future(operation)
- heartbeat_task = asyncio.create_task(lease.wait_for_failure())
- try:
- watched_tasks = {operation_task, heartbeat_task}
- if monitor_task is not None:
- watched_tasks.add(monitor_task)
- done, _ = await asyncio.wait(
- watched_tasks,
- return_when=asyncio.FIRST_COMPLETED,
- )
- if heartbeat_task in done:
- await _cancel_future_bounded(operation_task)
- await heartbeat_task
- if monitor_task is not None and monitor_task in done:
- await _cancel_future_bounded(operation_task)
- await monitor_task
- return await operation_task
- finally:
- if not operation_task.done():
- await _cancel_future_bounded(operation_task)
- heartbeat_task.cancel()
- await asyncio.gather(heartbeat_task, return_exceptions=True)
-
-
-def _close_unused_awaitable(operation: Awaitable[Any]) -> None:
- if inspect.iscoroutine(operation):
- operation.close()
-
-
-async def _bounded_phase(
- operation: Awaitable[ResultT],
- timeout_seconds: float,
- label: str,
-) -> ResultT:
- task = asyncio.ensure_future(operation)
- try:
- done, _ = await asyncio.wait({task}, timeout=timeout_seconds)
- except asyncio.CancelledError:
- await _cancel_future_bounded(task)
- raise
- if task in done:
- return await task
- await _cancel_future_bounded(task, timeout_seconds=0.1)
- raise CodexTransportError(f"{label} exceeded {timeout_seconds:g} seconds")
-
-
-async def _cancel_future_bounded(
- task: asyncio.Future[Any],
- *,
- timeout_seconds: float = 1.0,
-) -> None:
- task.cancel()
- done, _ = await asyncio.wait({task}, timeout=timeout_seconds)
- if task in done:
- try:
- task.result()
- except BaseException:
- pass
- return
-
- def consume_result(completed: asyncio.Future[Any]) -> None:
- try:
- completed.result()
- except BaseException:
- pass
-
- task.add_done_callback(consume_result)
-
-
-async def _read_runtime_auth(auth_path: Path) -> CodexAuthBundleV1:
- for delay_seconds in (0.0, 0.02, 0.05, 0.1):
- if delay_seconds:
- await asyncio.sleep(delay_seconds)
- try:
- return read_auth_bundle(auth_path, CODEX_RUNTIME_VERSION)
- except CodexAuthBundleError:
- continue
- raise CodexTransportError("Codex runtime auth state is unavailable")
+ return _transport
diff --git a/autogpt_platform/backend/backend/integrations/codex/transport_test.py b/autogpt_platform/backend/backend/integrations/codex/transport_test.py
index d2bb770077..30f2723eef 100644
--- a/autogpt_platform/backend/backend/integrations/codex/transport_test.py
+++ b/autogpt_platform/backend/backend/integrations/codex/transport_test.py
@@ -1,1689 +1,216 @@
import asyncio
-import base64
-import json
+import concurrent.futures
import threading
-from collections.abc import Callable, Coroutine
-from pathlib import Path
-from unittest.mock import AsyncMock, MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
+from pydantic import SecretStr
-from backend.integrations.codex.auth_bundle import (
- CodexAuthBundleV1,
- CodexAuthTokensV1,
- materialize_auth_bundle,
-)
+from backend.data.model import OAuth2Credentials
+from backend.integrations.codex import transport as transport_module
+from backend.integrations.codex.auth_bundle import CodexAuthBundleV1, CodexAuthTokensV1
from backend.integrations.codex.credential_codec import credentials_from_bundle
from backend.integrations.codex.models import (
- CodexAccountSnapshot,
- CodexDeviceCodeDetails,
- CodexDynamicToolCall,
- CodexDynamicToolResult,
- CodexDynamicToolSpec,
CodexInvocationRequest,
CodexInvocationResult,
CodexModelInfo,
CodexRateLimitsSnapshot,
)
from backend.integrations.codex.transport import (
+ CodexCredentialBusyError,
+ CodexCredentialLease,
CodexTransport,
- CodexTransportError,
- CodexTransportOverloadedError,
- _close_unused_awaitable,
- _run_with_lease_guard,
)
-from backend.integrations.credential_lease import CredentialLease
-
-
-def test_process_capacity_is_safe_across_event_loops():
- transport = CodexTransport(max_active_processes=1)
- first_acquired = threading.Event()
- release_first = threading.Event()
- second_acquired = threading.Event()
- errors: list[BaseException] = []
-
- async def hold_first_slot() -> None:
- await transport._acquire_capacity()
- first_acquired.set()
- while not release_first.is_set():
- await asyncio.sleep(0.01)
- transport._capacity.release()
-
- async def wait_for_second_slot() -> None:
- await transport._acquire_capacity()
- second_acquired.set()
- transport._capacity.release()
-
- def run(coroutine: Callable[[], Coroutine[object, object, None]]) -> None:
- try:
- asyncio.run(coroutine())
- except BaseException as error:
- errors.append(error)
-
- first = threading.Thread(target=run, args=(hold_first_slot,))
- second = threading.Thread(target=run, args=(wait_for_second_slot,))
- first.start()
- try:
- assert first_acquired.wait(timeout=2)
- second.start()
- assert not second_acquired.wait(timeout=0.05)
- release_first.set()
- assert second_acquired.wait(timeout=2)
- finally:
- release_first.set()
- first.join(timeout=2)
- if second.ident is not None:
- second.join(timeout=2)
-
- assert not first.is_alive()
- assert not second.is_alive()
- assert not errors
-
-
-def test_close_unused_awaitable_only_closes_native_coroutines():
- class CustomAwaitable:
- def __init__(self) -> None:
- self.close_called = False
-
- def __await__(self):
- if False:
- yield None
- return None
-
- def close(self) -> None:
- self.close_called = True
- operation = CustomAwaitable()
- _close_unused_awaitable(operation)
-
- assert not operation.close_called
-
-
-async def test_invoke_checkpoints_rotated_auth_before_cleanup(tmp_path):
- lease, checkpoint = _lease()
- runtime = _FakeRuntime(rotate_auth=True)
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
-
- result = await transport.invoke(
- lease,
- CodexInvocationRequest(prompt="Reply with ok"),
+def _credentials(*, legacy: bool = False) -> OAuth2Credentials:
+ token = (
+ "eyJhbGciOiJub25lIn0." "eyJleHAiOjk5OTk5OTk5OTksImVtYWlsIjoiYUBiLmMifQ." "sig"
)
-
- assert result.final_response == "ok"
- assert result.resolved_model == "gpt-test"
- assert runtime.last_request is not None
- assert runtime.last_request.model == "gpt-test"
- checkpoint.assert_awaited_once()
- updated = checkpoint.await_args.args[0]
- assert updated.refresh_token.get_secret_value() == "refresh-rotated"
- assert runtime.closed
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_invoke_rejects_model_not_advertised_by_account(tmp_path):
- lease, _ = _lease()
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
-
- with pytest.raises(CodexTransportError, match="not available"):
- await transport.invoke(
- lease,
- CodexInvocationRequest(prompt="ok", model="gpt-not-on-account"),
+ credentials = credentials_from_bundle(
+ CodexAuthBundleV1(
+ tokens=CodexAuthTokensV1(
+ id_token=SecretStr(token),
+ access_token=SecretStr(token),
+ refresh_token=SecretStr("refresh"),
+ ),
+ codex_runtime_version="http",
)
-
- assert runtime.last_request is None
- assert runtime.closed
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_runtime_failure_still_checkpoints_rotated_auth(tmp_path):
- lease, checkpoint = _lease()
- runtime = _FakeRuntime(rotate_auth=True, fail_invoke=True)
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
-
- with pytest.raises(RuntimeError, match="runtime failed"):
- await transport.invoke(lease, CodexInvocationRequest(prompt="fail"))
-
- checkpoint.assert_awaited_once()
- assert runtime.closed
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_unchanged_auth_does_not_checkpoint(tmp_path):
- lease, checkpoint = _lease()
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
-
- await transport.account(lease)
-
- checkpoint.assert_not_awaited()
- assert runtime.closed
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_unchanged_auth_still_validates_lease(tmp_path):
- lease, checkpoint = _lease()
- lease._lock.owned.return_value = False
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
-
- with pytest.raises(RuntimeError, match="ownership was lost"):
- await transport.account(lease)
-
- checkpoint.assert_not_awaited()
- assert runtime.closed
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_materialization_failure_releases_capacity_and_cleans_home(
- tmp_path,
- monkeypatch,
-):
- lease, _ = _lease()
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- max_active_processes=1,
- runtime_factory=_runtime_factory(runtime),
- )
-
- def fail_materialization(*_args):
- raise RuntimeError("materialization failed")
-
- monkeypatch.setattr(
- "backend.integrations.codex.transport.materialize_auth_bundle",
- fail_materialization,
- )
- with pytest.raises(RuntimeError, match="materialization failed"):
- await transport.account(lease)
-
- assert not tuple(tmp_path.iterdir())
- assert transport._capacity._value == 1
-
-
-async def test_device_login_returns_bundle_and_cleans_home(tmp_path):
- runtime = _FakeRuntime(complete_login=True)
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
-
- session = await transport.start_device_login()
- completion = await session.wait()
-
- assert session.details.user_code == "CODE-1234"
- assert completion.bundle.tokens.account_id == "account-user"
- assert completion.account is None
- assert runtime.closed
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_device_login_home_creation_failure_releases_capacity(
- tmp_path,
- monkeypatch,
-):
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- max_active_processes=1,
- runtime_factory=_runtime_factory(runtime),
- )
-
- def fail_home_creation(*_args):
- raise RuntimeError("home creation failed")
-
- monkeypatch.setattr(
- "backend.integrations.codex.transport.TemporaryCodexHome.create",
- fail_home_creation,
- )
- with pytest.raises(RuntimeError, match="home creation failed"):
- await transport.start_device_login()
-
- assert transport._capacity._value == 1
-
-
-async def test_capacity_wait_is_bounded():
- transport = CodexTransport(
- max_active_processes=1,
- capacity_timeout_seconds=0.02,
- )
- await transport._acquire_capacity()
- try:
- with pytest.raises(CodexTransportOverloadedError):
- await transport._acquire_capacity()
- finally:
- transport._capacity.release()
-
-
-async def test_runtime_startup_timeout_releases_capacity_and_cleans_home(tmp_path):
- release_startup = asyncio.Event()
-
- async def blocked_runtime_factory(_home):
- await release_startup.wait()
- return _FakeRuntime()
-
- transport = CodexTransport(
- temp_root=tmp_path,
- max_active_processes=1,
- startup_timeout_seconds=0.02,
- runtime_factory=blocked_runtime_factory,
- )
-
- with pytest.raises(CodexTransportError, match="runtime startup"):
- await transport.start_device_login()
-
- assert transport._capacity._value == 1
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_invoke_checkpoints_refresh_before_starting_turn(tmp_path):
- lease, checkpoint = _lease()
- runtime = _FakeRuntime(rotate_on_account=True)
- runtime.checkpoint_probe = lambda: checkpoint.await_count == 1
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
-
- await transport.invoke(lease, CodexInvocationRequest(prompt="ok"))
-
- checkpoint.assert_awaited_once()
- assert runtime.refresh_was_checkpointed_before_invoke
-
-
-async def test_active_invocation_checkpoints_late_auth_rotation(tmp_path):
- lease, checkpoint = _lease()
- rotated = asyncio.Event()
- checkpointed = asyncio.Event()
- finish = asyncio.Event()
-
- async def record_checkpoint(*_args):
- checkpointed.set()
-
- checkpoint.side_effect = record_checkpoint
-
- class RotatingRuntime(_FakeRuntime):
- async def invoke(
- self,
- request: CodexInvocationRequest,
- ) -> CodexInvocationResult:
- payload = json.loads((self.home / "auth.json").read_text())
- payload["tokens"]["refresh_token"] = "refresh-during-turn"
- (self.home / "auth.json").write_text(json.dumps(payload))
- rotated.set()
- await finish.wait()
- return await super().invoke(request)
-
- runtime = RotatingRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- checkpoint_interval_seconds=0.01,
- runtime_factory=_runtime_factory(runtime),
- )
-
- invocation = asyncio.create_task(
- transport.invoke(lease, CodexInvocationRequest(prompt="ok"))
- )
- await asyncio.wait_for(rotated.wait(), timeout=1)
- await asyncio.wait_for(checkpointed.wait(), timeout=1)
- assert not invocation.done()
- finish.set()
- await invocation
-
- checkpoint.assert_awaited_once()
-
-
-async def test_invocation_aborts_when_credential_lease_heartbeat_fails(tmp_path):
- lease, _ = _lease()
- started = asyncio.Event()
- canceled = asyncio.Event()
-
- class BlockingRuntime(_FakeRuntime):
- async def invoke(
- self,
- request: CodexInvocationRequest,
- ) -> CodexInvocationResult:
- started.set()
- try:
- await asyncio.Event().wait()
- finally:
- canceled.set()
- raise AssertionError("unreachable")
-
- runtime = BlockingRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- invocation = asyncio.create_task(
- transport.invoke(lease, CodexInvocationRequest(prompt="ok"))
- )
- await asyncio.wait_for(started.wait(), timeout=1)
- lease._heartbeat_error = RuntimeError("redis lease lost")
- lease._heartbeat_failed.set()
-
- with pytest.raises(RuntimeError, match="heartbeat failed"):
- await invocation
-
- assert canceled.is_set()
- assert runtime.closed
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_agent_invocation_uses_separate_tool_timeout_and_checkpoints(tmp_path):
- lease, checkpoint = _lease()
- runtime = _FakeRuntime(rotate_on_account=True)
- transport = CodexTransport(
- temp_root=tmp_path,
- copilot_tool_timeout_seconds=17,
- runtime_factory=_runtime_factory(runtime),
- )
- tool = CodexDynamicToolSpec(
- name="find_agent",
- description="Find an agent",
- input_schema={"type": "object"},
- )
-
- async def tool_handler(_call: CodexDynamicToolCall) -> CodexDynamicToolResult:
- return CodexDynamicToolResult(content="ok")
-
- result = await transport.invoke_agent(
- lease,
- CodexInvocationRequest(prompt="Find it"),
- [tool],
- tool_handler,
- )
-
- assert result.final_response == "ok"
- assert runtime.agent_tool_timeout == 17
- assert runtime.agent_dynamic_tools == [tool]
- checkpoint.assert_awaited_once()
- assert runtime.closed
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_runtime_pool_overlaps_borrowers_on_one_lease_and_runtime(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
+ ).model_copy(update={"id": "cred-id"})
+ if legacy:
+ return credentials.model_copy(update={"refresh_strategy": "provider_runtime"})
+ return credentials
+
+
+def test_transport_singleton_initializes_once_across_worker_threads() -> None:
+ first_constructor_entered = threading.Event()
+ release_first_constructor = threading.Event()
+ construction_count = 0
+ constructed: list[object] = []
+
+ def construct(**_kwargs):
+ nonlocal construction_count
+ construction_count += 1
+ instance = object()
+ constructed.append(instance)
+ if construction_count == 1:
+ first_constructor_entered.set()
+ assert release_first_constructor.wait(timeout=2)
+ return instance
+
+ with (
+ patch.object(transport_module, "_transport", None),
+ patch.object(transport_module, "Settings", return_value=MagicMock()),
+ patch.object(transport_module, "CodexTransport", side_effect=construct),
+ concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool,
+ ):
+ first = pool.submit(transport_module.get_codex_transport)
+ assert first_constructor_entered.wait(timeout=1)
+ second = pool.submit(transport_module.get_codex_transport)
+
+ with pytest.raises(concurrent.futures.TimeoutError):
+ second.result(timeout=0.05)
+
+ release_first_constructor.set()
+ first_result = first.result(timeout=1)
+ second_result = second.result(timeout=1)
+
+ assert construction_count == 1
+ assert first_result is constructed[0]
+ assert second_result is first_result
+
+
+async def test_runtime_snapshot_does_not_hold_an_exclusive_credential_lease() -> None:
manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
-
- both_started = threading.Event()
- allow_finish = threading.Event()
-
- class ConcurrentRuntime(_FakeRuntime):
- def __init__(self):
- super().__init__()
- self._active_lock = threading.Lock()
- self.active = 0
- self.max_active = 0
- self.requests: list[CodexInvocationRequest] = []
-
- async def invoke_agent(
- self,
- request,
- dynamic_tools,
- tool_handler,
- event_handler=None,
- *,
- tool_timeout_seconds,
- ):
- del dynamic_tools, tool_handler, event_handler, tool_timeout_seconds
- self.requests.append(request)
- with self._active_lock:
- self.active += 1
- self.max_active = max(self.max_active, self.active)
- if self.active == 2:
- both_started.set()
- try:
- while not allow_finish.is_set():
- await asyncio.sleep(0.01)
- return CodexInvocationResult(
- response_id=f"turn-{len(self.requests)}",
- final_response=request.prompt,
- status="completed",
- )
- finally:
- with self._active_lock:
- self.active -= 1
-
- runtime = ConcurrentRuntime()
- factory = AsyncMock(side_effect=_runtime_factory(runtime))
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=factory,
- )
+ manager.get = AsyncMock(return_value=_credentials())
+ manager.acquire_lease = AsyncMock()
+ transport = CodexTransport()
+ session = MagicMock()
- first, second = await asyncio.gather(
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- ),
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
+ with (
+ patch(
+ "backend.integrations.codex.transport.IntegrationCredentialsManager",
+ return_value=manager,
),
- )
-
- async def tool_handler(_call):
- return CodexDynamicToolResult(content="ok")
-
- first_call = asyncio.create_task(
- first.invoke(
- CodexInvocationRequest(prompt="first"),
- [],
- tool_handler,
+ patch.object(transport, "_session_for", return_value=session),
+ ):
+ lease = await transport.acquire_runtime_lease(
+ "user-a", "cred-id", lock_timeout_seconds=1
)
- )
- second_call = asyncio.create_task(
- second.invoke(
- CodexInvocationRequest(prompt="second"),
- [],
- tool_handler,
- )
- )
- assert await asyncio.to_thread(both_started.wait, 2)
- assert not first_call.done()
- assert not second_call.done()
- assert manager.acquire_lease.await_count == 1
- assert factory.await_count == 1
- assert runtime.max_active == 2
-
- allow_finish.set()
- results = await asyncio.gather(first_call, second_call)
- assert {result.final_response for result in results} == {"first", "second"}
-
- await first.release()
- assert not runtime.closed
- raw_lease._lock.release.assert_not_awaited()
- await second.release()
- assert runtime.closed
- raw_lease._lock.release.assert_awaited_once()
- assert not tuple(tmp_path.iterdir())
- await transport.close_runtime_pool()
-
-
-async def test_runtime_pool_cancels_one_borrower_without_closing_sibling(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- cancel_started = threading.Event()
- cancel_observed = threading.Event()
- peer_started = threading.Event()
- finish_peer = threading.Event()
-
- class CancellationRuntime(_FakeRuntime):
- async def invoke_agent(
- self,
- request,
- dynamic_tools,
- tool_handler,
- event_handler=None,
- *,
- tool_timeout_seconds,
- ):
- del dynamic_tools, tool_handler, event_handler, tool_timeout_seconds
- if request.prompt == "cancel":
- cancel_started.set()
- try:
- await asyncio.Event().wait()
- finally:
- cancel_observed.set()
- peer_started.set()
- while not finish_peer.is_set():
- await asyncio.sleep(0.01)
- return CodexInvocationResult(
- response_id="peer-turn",
- final_response="peer-ok",
- status="completed",
- )
- runtime = CancellationRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- first, second = await asyncio.gather(
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- ),
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- ),
- )
-
- async def tool_handler(_call):
- return CodexDynamicToolResult(content="ok")
-
- canceled_call = asyncio.create_task(
- first.invoke(CodexInvocationRequest(prompt="cancel"), [], tool_handler)
- )
- peer_call = asyncio.create_task(
- second.invoke(CodexInvocationRequest(prompt="peer"), [], tool_handler)
- )
- assert await asyncio.to_thread(cancel_started.wait, 2)
- assert await asyncio.to_thread(peer_started.wait, 2)
- canceled_call.cancel()
- with pytest.raises(asyncio.CancelledError):
- await canceled_call
- assert await asyncio.to_thread(cancel_observed.wait, 2)
- assert not peer_call.done()
- assert not runtime.closed
-
- finish_peer.set()
- assert (await peer_call).final_response == "peer-ok"
- await first.release()
- assert not runtime.closed
- await second.release()
- assert runtime.closed
- await transport.close_runtime_pool()
-
-
-def test_runtime_pool_loop_start_is_singleton_across_threads():
- transport = CodexTransport()
- barrier = threading.Barrier(8)
- loops: list[asyncio.AbstractEventLoop] = []
- errors: list[BaseException] = []
-
- def start_loop() -> None:
- try:
- barrier.wait(timeout=2)
- loops.append(transport._runtime_pool._ensure_loop())
- except BaseException as error:
- errors.append(error)
-
- threads = [threading.Thread(target=start_loop) for _ in range(8)]
- for thread in threads:
- thread.start()
- for thread in threads:
- thread.join(timeout=2)
-
- assert not errors
- assert len(loops) == 8
- assert len({id(loop) for loop in loops}) == 1
- asyncio.run(transport.close_runtime_pool())
-
-
-async def test_runtime_pool_lock_timeout_does_not_bound_healthy_startup(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- runtime = _FakeRuntime()
-
- async def delayed_runtime_factory(home):
- await asyncio.sleep(0.05)
- runtime.home = home.path
- return runtime
-
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=delayed_runtime_factory,
- )
-
- lease = await transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=0.01,
- )
-
- assert manager.acquire_lease.await_count == 1
+ assert lease.credentials.id == "cred-id"
+ manager.get.assert_awaited_once_with("user-a", "cred-id")
+ manager.acquire_lease.assert_not_awaited()
await lease.release()
- assert runtime.closed
- await transport.close_runtime_pool()
@pytest.mark.asyncio
-async def test_runtime_pool_canceled_acquire_releases_completed_borrow(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- pool = transport._runtime_pool
- original_borrow = pool._borrow_on_actor_loop
- borrowed = threading.Event()
-
- async def hold_completed_borrow(*args, **kwargs):
- credentials = await original_borrow(*args, **kwargs)
- borrowed.set()
- await asyncio.Event().wait()
- return credentials
-
- monkeypatch.setattr(pool, "_borrow_on_actor_loop", hold_completed_borrow)
- acquire = asyncio.create_task(
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
- )
- assert await asyncio.to_thread(borrowed.wait, 2)
- acquire.cancel()
-
- with pytest.raises(asyncio.CancelledError):
- await acquire
-
- assert runtime.closed
- raw_lease._lock.release.assert_awaited_once()
- assert not tuple(tmp_path.iterdir())
- await transport.close_runtime_pool()
+async def test_runtime_snapshot_times_out_during_credential_contention() -> None:
+ blocker = asyncio.Event()
+ async def blocked_get(_user_id: str, _credential_id: str):
+ await blocker.wait()
-async def test_runtime_pool_release_can_retry_after_submission_failure(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- lease = await transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
- pool = transport._runtime_pool
- original_release = pool.release
- attempts = 0
-
- async def fail_once(*args, **kwargs):
- nonlocal attempts
- attempts += 1
- if attempts == 1:
- raise RuntimeError("release submission failed")
- return await original_release(*args, **kwargs)
-
- monkeypatch.setattr(pool, "release", fail_once)
-
- with pytest.raises(RuntimeError, match="release submission failed"):
- await lease.release()
-
- assert not runtime.closed
- await lease.release()
- assert attempts == 2
- assert runtime.closed
- raw_lease._lock.release.assert_awaited_once()
- await transport.close_runtime_pool()
-
-
-async def test_runtime_pool_canceled_release_follower_does_not_cancel_leader(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- lease = await transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
- pool = transport._runtime_pool
- original_release = pool.release
- release_started = asyncio.Event()
- allow_release = asyncio.Event()
-
- async def delayed_release(*args, **kwargs):
- release_started.set()
- await allow_release.wait()
- return await original_release(*args, **kwargs)
-
- monkeypatch.setattr(pool, "release", delayed_release)
- leader = asyncio.create_task(lease.release())
- await asyncio.wait_for(release_started.wait(), timeout=1)
- follower = asyncio.create_task(lease.release())
- await asyncio.sleep(0)
- follower.cancel()
-
- with pytest.raises(asyncio.CancelledError):
- await follower
-
- allow_release.set()
- await leader
- await lease.release()
-
- assert runtime.closed
- raw_lease._lock.release.assert_awaited_once()
- await transport.close_runtime_pool()
-
-
-def test_runtime_pool_close_serializes_with_acquire_submission(tmp_path, monkeypatch):
- raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- real_submit = asyncio.run_coroutine_threadsafe
- submit_entered = threading.Event()
- allow_submit = threading.Event()
- close_complete = threading.Event()
- acquire_results: list[object] = []
- acquire_errors: list[BaseException] = []
- close_errors: list[BaseException] = []
-
- def block_first_submit(operation, loop):
- monkeypatch.setattr(asyncio, "run_coroutine_threadsafe", real_submit)
- submit_entered.set()
- if not allow_submit.wait(timeout=2):
- operation.close()
- raise RuntimeError("timed out waiting to submit")
- return real_submit(operation, loop)
-
- monkeypatch.setattr(asyncio, "run_coroutine_threadsafe", block_first_submit)
-
- def acquire() -> None:
- try:
- acquire_results.append(
- asyncio.run(
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
- )
- )
- except BaseException as error:
- acquire_errors.append(error)
-
- def close() -> None:
- try:
- asyncio.run(transport.close_runtime_pool())
- except BaseException as error:
- close_errors.append(error)
- finally:
- close_complete.set()
-
- acquire_thread = threading.Thread(target=acquire)
- close_thread = threading.Thread(target=close)
- acquire_thread.start()
- assert submit_entered.wait(timeout=2)
- close_thread.start()
- assert not close_complete.wait(timeout=0.05)
- allow_submit.set()
- acquire_thread.join(timeout=5)
- close_thread.join(timeout=5)
-
- assert not acquire_thread.is_alive()
- assert not close_thread.is_alive()
- assert not close_errors
- assert not acquire_results
- assert len(acquire_errors) == 1
- assert isinstance(acquire_errors[0], CodexTransportError)
- if manager.acquire_lease.await_count:
- assert runtime.closed
- raw_lease._lock.release.assert_awaited_once()
- else:
- assert not runtime.closed
- raw_lease._lock.release.assert_not_awaited()
- assert not tuple(tmp_path.iterdir())
- with pytest.raises(CodexTransportError, match="shutting down"):
- asyncio.run(
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
+ manager.get = AsyncMock(side_effect=blocked_get)
+
+ with patch(
+ "backend.integrations.codex.transport.IntegrationCredentialsManager",
+ return_value=manager,
+ ):
+ with pytest.raises(CodexCredentialBusyError, match="codex_credential_busy"):
+ await CodexTransport().acquire_runtime_lease(
+ "user-a", "cred-id", lock_timeout_seconds=0.01
)
- )
-
-
-def test_runtime_pool_close_racing_completed_start_cleans_actor_once(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- pool = transport._runtime_pool
- original_start = pool._start_actor
- actor_ready = threading.Event()
- allow_start_return = threading.Event()
-
- async def gated_start(*args, **kwargs):
- actor = await original_start(*args, **kwargs)
- actor_ready.set()
- if not allow_start_return.wait(timeout=2):
- raise RuntimeError("timed out waiting to return started actor")
- return actor
-
- monkeypatch.setattr(pool, "_start_actor", gated_start)
- acquire_results: list[object] = []
- acquire_errors: list[BaseException] = []
- close_errors: list[BaseException] = []
-
- def acquire() -> None:
- try:
- acquire_results.append(
- asyncio.run(
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
- )
- )
- except BaseException as error:
- acquire_errors.append(error)
-
- def close() -> None:
- try:
- asyncio.run(transport.close_runtime_pool())
- except BaseException as error:
- close_errors.append(error)
-
- acquire_thread = threading.Thread(target=acquire)
- close_thread = threading.Thread(target=close)
- acquire_thread.start()
- assert actor_ready.wait(timeout=2)
- close_thread.start()
- for _ in range(200):
- with pool._state_lock:
- if pool._closing:
- break
- threading.Event().wait(0.01)
- else:
- pytest.fail("runtime pool did not begin closing")
- allow_start_return.set()
- acquire_thread.join(timeout=5)
- close_thread.join(timeout=5)
-
- assert not acquire_thread.is_alive()
- assert not close_thread.is_alive()
- assert not close_errors
- assert not acquire_results
- assert len(acquire_errors) == 1
- assert isinstance(acquire_errors[0], CodexTransportError)
- assert runtime.closed
- raw_lease._lock.release.assert_awaited_once()
- assert not tuple(tmp_path.iterdir())
-
-
-def test_runtime_pool_close_waits_for_loop_bootstrap(monkeypatch):
- transport = CodexTransport()
- pool = transport._runtime_pool
- real_new_event_loop = asyncio.new_event_loop
- bootstrap_entered = threading.Event()
- allow_bootstrap = threading.Event()
- acquire_errors: list[BaseException] = []
- close_errors: list[BaseException] = []
-
- def gated_new_event_loop():
- if threading.current_thread().name == "autogpt-codex-runtime-pool":
- bootstrap_entered.set()
- if not allow_bootstrap.wait(timeout=2):
- raise RuntimeError("timed out waiting to bootstrap actor loop")
- return real_new_event_loop()
-
- monkeypatch.setattr(asyncio, "new_event_loop", gated_new_event_loop)
-
- def acquire() -> None:
- try:
- asyncio.run(
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
- )
- except BaseException as error:
- acquire_errors.append(error)
-
- def close() -> None:
- try:
- asyncio.run(transport.close_runtime_pool())
- except BaseException as error:
- close_errors.append(error)
-
- acquire_thread = threading.Thread(target=acquire)
- close_thread = threading.Thread(target=close)
- acquire_thread.start()
- assert bootstrap_entered.wait(timeout=2)
- close_thread.start()
- for _ in range(200):
- with pool._state_lock:
- if pool._closing:
- break
- threading.Event().wait(0.01)
- else:
- pytest.fail("runtime pool did not begin closing")
- allow_bootstrap.set()
- acquire_thread.join(timeout=5)
- close_thread.join(timeout=5)
-
- assert not acquire_thread.is_alive()
- assert not close_thread.is_alive()
- assert not close_errors
- assert len(acquire_errors) == 1
- assert isinstance(acquire_errors[0], CodexTransportError)
- with pool._state_lock:
- assert pool._thread is None
- assert pool._loop is None
- assert pool._closed
@pytest.mark.asyncio
-async def test_runtime_pool_release_rejects_late_invocation_while_draining(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
+async def test_legacy_runtime_credential_is_migrated_before_use() -> None:
+ legacy = _credentials(legacy=True)
+ migrated = _credentials()
+ legacy_lease = MagicMock()
+ legacy_lease.credentials = legacy
+ legacy_lease.checkpoint = AsyncMock()
+ legacy_lease.release = AsyncMock()
manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- active_started = threading.Event()
- cancellation_seen = threading.Event()
- allow_cancellation = threading.Event()
- late_started = threading.Event()
-
- class ReleaseRaceRuntime(_FakeRuntime):
- async def invoke_agent(
- self,
- request,
- dynamic_tools,
- tool_handler,
- event_handler=None,
- *,
- tool_timeout_seconds,
- ):
- del dynamic_tools, tool_handler, event_handler, tool_timeout_seconds
- if request.prompt != "active":
- late_started.set()
- return CodexInvocationResult(
- response_id="late",
- final_response="late",
- status="completed",
- )
- active_started.set()
- try:
- await asyncio.Event().wait()
- except asyncio.CancelledError:
- cancellation_seen.set()
- while not allow_cancellation.is_set():
- await asyncio.sleep(0.01)
- raise
-
- runtime = ReleaseRaceRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- lease = await transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
-
- async def tool_handler(_call):
- return CodexDynamicToolResult(content="ok")
-
- active = asyncio.create_task(
- lease.invoke(
- CodexInvocationRequest(prompt="active"),
- [],
- tool_handler,
- )
- )
- assert await asyncio.to_thread(active_started.wait, 2)
- release = asyncio.create_task(lease.release())
- assert await asyncio.to_thread(cancellation_seen.wait, 2)
-
- with pytest.raises(RuntimeError, match="no longer active"):
- await lease._pool.invoke(
- lease._key,
- lease._handle_id,
- CodexInvocationRequest(prompt="late"),
- [],
- tool_handler,
- None,
- )
-
- assert not late_started.is_set()
- allow_cancellation.set()
- with pytest.raises(asyncio.CancelledError):
- await active
- await release
-
- assert runtime.closed
- raw_lease._lock.release.assert_awaited_once()
- await transport.close_runtime_pool()
-
-
-async def test_runtime_pool_release_cancels_and_drains_model_lookup(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- models_started = threading.Event()
- models_canceled = threading.Event()
-
- class BlockingModelsRuntime(_FakeRuntime):
- async def models(self):
- models_started.set()
- try:
- await asyncio.Event().wait()
- finally:
- models_canceled.set()
-
- runtime = BlockingModelsRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- lease = await transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
- models = asyncio.create_task(lease.models())
- assert await asyncio.to_thread(models_started.wait, 2)
-
- await lease.release()
- with pytest.raises(asyncio.CancelledError):
- await models
-
- assert models_canceled.is_set()
- assert runtime.closed
- raw_lease._lock.release.assert_awaited_once()
- assert not tuple(tmp_path.iterdir())
- await transport.close_runtime_pool()
-
-
-async def test_runtime_pool_rejects_borrower_after_lease_heartbeat_failure(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- first = await transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
- raw_lease._heartbeat_error = RuntimeError("redis lease lost")
-
- with pytest.raises(CodexTransportError, match="runtime is unavailable"):
- await transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
-
- assert manager.acquire_lease.await_count == 1
- raw_lease._heartbeat_error = None
- await first.release()
- assert runtime.closed
- await transport.close_runtime_pool()
-
-
-async def test_runtime_pool_rejects_active_call_after_lease_heartbeat_failure(
- tmp_path,
- monkeypatch,
-):
- raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(return_value=raw_lease)
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- runtime = _FakeRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=_runtime_factory(runtime),
- )
- lease = await transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- )
- raw_lease._heartbeat_error = RuntimeError("redis lease lost")
-
- async def tool_handler(_call):
- return CodexDynamicToolResult(content="ok")
-
- with pytest.raises(CodexTransportError, match="runtime is unavailable"):
- await lease.invoke(
- CodexInvocationRequest(prompt="must not start"),
- [],
- tool_handler,
- )
-
- assert runtime.last_request is None
- raw_lease._heartbeat_error = None
- await lease.release()
- assert runtime.closed
- await transport.close_runtime_pool()
-
-
-async def test_lease_guard_closes_operation_before_known_heartbeat_failure():
- lease, _ = _lease()
- lease._heartbeat_error = RuntimeError("redis lease lost")
-
- async def operation() -> str:
- raise AssertionError("provider operation must not start")
-
- provider_operation = operation()
- with pytest.raises(RuntimeError, match="heartbeat failed"):
- await _run_with_lease_guard(lease, provider_operation)
-
- assert provider_operation.cr_frame is None
-
-
-async def test_lease_guard_closes_operation_before_failed_monitor():
- lease, _ = _lease()
-
- async def fail_monitor() -> None:
- raise RuntimeError("auth monitor failed")
-
- monitor = asyncio.create_task(fail_monitor())
- await asyncio.sleep(0)
-
- async def operation() -> str:
- raise AssertionError("provider operation must not start")
+ manager.get = AsyncMock(side_effect=[legacy, migrated])
+ manager.acquire_lease = AsyncMock(return_value=legacy_lease)
+ transport = CodexTransport()
- provider_operation = operation()
- with pytest.raises(RuntimeError, match="auth monitor failed"):
- await _run_with_lease_guard(
- lease,
- provider_operation,
- monitor_task=monitor,
+ with (
+ patch(
+ "backend.integrations.codex.transport.IntegrationCredentialsManager",
+ return_value=manager,
+ ),
+ patch.object(transport, "_session_for", return_value=MagicMock()),
+ ):
+ lease = await transport.acquire_runtime_lease(
+ "user-a", "cred-id", lock_timeout_seconds=1
)
- assert provider_operation.cr_frame is None
+ checkpointed = legacy_lease.checkpoint.await_args.args[0]
+ assert checkpointed.refresh_strategy == "oauth_handler"
+ legacy_lease.release.assert_awaited_once()
+ assert lease.credentials.refresh_strategy == "oauth_handler"
-async def test_runtime_pool_shutdown_cleans_other_actors_after_close_error(
- tmp_path,
- monkeypatch,
-):
- first_raw_lease, _ = _lease()
- second_raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(side_effect=[first_raw_lease, second_raw_lease])
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
+def _invocation_result() -> CodexInvocationResult:
+ return CodexInvocationResult(
+ response_id="response-1",
+ final_response="done",
+ status="completed",
)
- class FailingCloseRuntime(_FakeRuntime):
- async def close(self):
- self.closed = True
- raise RuntimeError("runtime close failed")
- first_runtime = FailingCloseRuntime()
- second_runtime = FailingCloseRuntime()
- runtimes = iter((first_runtime, second_runtime))
-
- async def runtime_factory(home):
- runtime = next(runtimes)
- runtime.home = home.path
- return runtime
-
- transport = CodexTransport(
- temp_root=tmp_path,
- control_timeout_seconds=0.1,
- runtime_factory=runtime_factory,
- )
- await asyncio.gather(
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- ),
- transport.acquire_runtime_lease(
- "user-2",
- "credential-2",
- lock_timeout_seconds=1,
- ),
- )
-
- results = await asyncio.gather(
- transport.close_runtime_pool(),
- transport.close_runtime_pool(),
- return_exceptions=True,
- )
-
- assert len(results) == 2
- assert all(isinstance(result, CodexTransportError) for result in results)
- assert first_runtime.closed
- assert second_runtime.closed
- assert any(
- "Additional cleanup failure: RuntimeError: runtime close failed" in note
- for result in results
- if isinstance(result, CodexTransportError)
- for note in result.__notes__
- )
- first_raw_lease._lock.release.assert_awaited_once()
- second_raw_lease._lock.release.assert_awaited_once()
- assert not tuple(tmp_path.iterdir())
+def _session_with_rate_limits() -> MagicMock:
+ session = MagicMock()
+ session.credential_id = "cred-id"
+ session.rate_limits = CodexRateLimitsSnapshot(plan_type="pro")
+ session.invoke = AsyncMock(return_value=_invocation_result())
+ return session
@pytest.mark.asyncio
-async def test_runtime_pool_shutdown_closes_actors_concurrently(tmp_path, monkeypatch):
- first_raw_lease, _ = _lease()
- second_raw_lease, _ = _lease()
- manager = MagicMock()
- manager.acquire_lease = AsyncMock(side_effect=[first_raw_lease, second_raw_lease])
- monkeypatch.setattr(
- "backend.integrations.creds_manager.IntegrationCredentialsManager",
- lambda: manager,
- )
- first_runtime = _FakeRuntime()
- second_runtime = _FakeRuntime()
- runtimes = iter((first_runtime, second_runtime))
-
- async def runtime_factory(home):
- runtime = next(runtimes)
- runtime.home = home.path
- return runtime
-
- transport = CodexTransport(
- temp_root=tmp_path,
- runtime_factory=runtime_factory,
- )
- await asyncio.gather(
- transport.acquire_runtime_lease(
- "user-1",
- "credential-1",
- lock_timeout_seconds=1,
- ),
- transport.acquire_runtime_lease(
- "user-2",
- "credential-2",
- lock_timeout_seconds=1,
+async def test_direct_invoke_records_the_latest_rate_limits() -> None:
+ credentials = _credentials()
+ session = _session_with_rate_limits()
+ transport = CodexTransport()
+ lease = CodexCredentialLease(credentials, session)
+ model = CodexModelInfo(
+ model="gpt-5.6-sol",
+ display_name="GPT-5.6 Sol",
+ is_default=True,
+ hidden=False,
+ default_reasoning_effort="medium",
+ supported_reasoning_efforts=["medium"],
+ )
+
+ with (
+ patch.object(transport, "_session_for", return_value=session),
+ patch(
+ "backend.integrations.codex.transport.fetch_models",
+ new=AsyncMock(return_value=[model]),
),
- )
- pool = transport._runtime_pool
- original_close = pool._close_actor_bounded
- close_count = 0
- close_count_lock = threading.Lock()
- both_closing = threading.Event()
- allow_close = threading.Event()
-
- async def delayed_close(actor):
- nonlocal close_count
- with close_count_lock:
- close_count += 1
- if close_count == 2:
- both_closing.set()
- while not allow_close.is_set():
- await asyncio.sleep(0.01)
- await original_close(actor)
+ ):
+ await transport.invoke(lease, CodexInvocationRequest(prompt="hello"))
- monkeypatch.setattr(pool, "_close_actor_bounded", delayed_close)
- shutdown = asyncio.create_task(transport.close_runtime_pool())
- reached_both = await asyncio.to_thread(both_closing.wait, 2)
- allow_close.set()
- await shutdown
-
- assert reached_both
- assert first_runtime.closed
- assert second_runtime.closed
- first_raw_lease._lock.release.assert_awaited_once()
- second_raw_lease._lock.release.assert_awaited_once()
- assert not tuple(tmp_path.iterdir())
+ assert (await transport.rate_limits(lease)).plan_type == "pro"
@pytest.mark.asyncio
-async def test_never_returning_runtime_close_cannot_strand_home_or_capacity(tmp_path):
- lease, _ = _lease()
-
- class NonCancellingFuture(asyncio.Future[None]):
- def cancel(self, *_args, **_kwargs):
- return False
-
- class NeverClosingRuntime(_FakeRuntime):
- def close(self):
- return NonCancellingFuture()
-
- runtime = NeverClosingRuntime()
- transport = CodexTransport(
- temp_root=tmp_path,
- max_active_processes=1,
- control_timeout_seconds=0.01,
- runtime_factory=_runtime_factory(runtime),
- )
-
- with pytest.raises(CodexTransportError, match="runtime shutdown"):
- await asyncio.wait_for(transport._account(lease), timeout=0.2)
-
- assert transport._capacity._value == 1
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_cancel_during_runtime_startup_drains_factory_before_home_cleanup(
- tmp_path,
-):
- started = asyncio.Event()
- cancelled = asyncio.Event()
-
- async def blocked_runtime_factory(_home):
- started.set()
- try:
- await asyncio.Event().wait()
- finally:
- cancelled.set()
-
- transport = CodexTransport(
- temp_root=tmp_path,
- max_active_processes=1,
- runtime_factory=blocked_runtime_factory,
- )
- login = asyncio.create_task(transport.start_device_login())
- await asyncio.wait_for(started.wait(), timeout=1)
- login.cancel()
-
- with pytest.raises(asyncio.CancelledError):
- await asyncio.wait_for(login, timeout=2)
-
- assert cancelled.is_set()
- assert transport._capacity._value == 1
- assert not tuple(tmp_path.iterdir())
-
-
-async def test_caller_cancellation_stops_active_runtime_operation(tmp_path):
- lease, _ = _lease()
- started = asyncio.Event()
- cancelled = asyncio.Event()
-
- class BlockingRuntime(_FakeRuntime):
- async def invoke(self, request):
- started.set()
- try:
- await asyncio.Event().wait()
- finally:
- cancelled.set()
-
- runtime = BlockingRuntime()
- max_active_processes = 2
- transport = CodexTransport(
- temp_root=tmp_path,
- max_active_processes=max_active_processes,
- runtime_factory=_runtime_factory(runtime),
- )
- invocation = asyncio.create_task(
- transport.invoke(lease, CodexInvocationRequest(prompt="ok"))
- )
- await asyncio.wait_for(started.wait(), timeout=1)
- invocation.cancel()
-
- with pytest.raises(asyncio.CancelledError):
- await asyncio.wait_for(invocation, timeout=2)
-
- assert cancelled.is_set()
- assert runtime.closed
- assert transport._capacity._value == max_active_processes
- assert not tuple(tmp_path.iterdir())
-
-
-def _runtime_factory(runtime: "_FakeRuntime"):
- async def create(home):
- runtime.home = home.path
- return runtime
-
- return create
-
-
-def _lease() -> tuple[CredentialLease, AsyncMock]:
- credentials = credentials_from_bundle(_bundle())
- lock = AsyncMock()
- lock.locked.return_value = True
- lock.owned.return_value = True
- lock.timeout = 60
- checkpoint = AsyncMock()
- return CredentialLease(credentials, lock, checkpoint), checkpoint
-
-
-class _FakeLogin:
- details = CodexDeviceCodeDetails(
- login_id="login-1",
- verification_url="https://example.com/device",
- user_code="CODE-1234",
- )
-
- async def wait(self) -> bool:
- return True
-
- async def cancel(self) -> None:
- return None
-
-
-class _FakeRuntime:
- def __init__(
- self,
- *,
- rotate_auth: bool = False,
- fail_invoke: bool = False,
- complete_login: bool = False,
- rotate_on_account: bool = False,
- ) -> None:
- self.home = Path()
- self.rotate_auth = rotate_auth
- self.fail_invoke = fail_invoke
- self.complete_login = complete_login
- self.rotate_on_account = rotate_on_account
- self.closed = False
- self.refresh_was_checkpointed_before_invoke = False
- self.checkpoint_probe: Callable[[], bool] = lambda: False
- self.agent_tool_timeout: float | None = None
- self.agent_dynamic_tools: list[CodexDynamicToolSpec] | None = None
- self.last_request: CodexInvocationRequest | None = None
-
- async def start_device_code_login(self) -> _FakeLogin:
- if self.complete_login:
- materialize_auth_bundle(_bundle(), self.home / "auth.json")
- return _FakeLogin()
-
- async def account(self, *, refresh_token: bool = True) -> CodexAccountSnapshot:
- assert refresh_token
- if self.rotate_on_account:
- payload = json.loads((self.home / "auth.json").read_text())
- payload["tokens"]["refresh_token"] = "refresh-from-account"
- (self.home / "auth.json").write_text(json.dumps(payload))
- return CodexAccountSnapshot(
- connected=True,
- requires_openai_auth=True,
- account_type="chatgpt",
- plan_type="pro",
- )
-
- async def rate_limits(self) -> CodexRateLimitsSnapshot:
- return CodexRateLimitsSnapshot(plan_type="pro")
-
- async def models(self) -> list[CodexModelInfo]:
- return [
- CodexModelInfo(
- model="gpt-test",
- display_name="GPT Test",
- is_default=True,
- hidden=False,
- default_reasoning_effort="medium",
- supported_reasoning_efforts=["low", "medium", "high"],
- input_modalities=["text"],
- )
- ]
+async def test_direct_agent_invoke_records_the_latest_rate_limits() -> None:
+ credentials = _credentials()
+ session = _session_with_rate_limits()
+ transport = CodexTransport()
+ lease = CodexCredentialLease(credentials, session)
- async def invoke(self, request: CodexInvocationRequest) -> CodexInvocationResult:
- self.last_request = request
- if self.rotate_on_account:
- self.refresh_was_checkpointed_before_invoke = self.checkpoint_probe()
- if self.rotate_auth:
- payload = json.loads((self.home / "auth.json").read_text())
- payload["tokens"]["refresh_token"] = "refresh-rotated"
- (self.home / "auth.json").write_text(json.dumps(payload))
- if self.fail_invoke:
- raise RuntimeError("runtime failed")
- return CodexInvocationResult(
- response_id="turn-1",
- final_response="ok",
- status="completed",
+ with patch.object(transport, "_session_for", return_value=session):
+ await transport.invoke_agent(
+ lease,
+ CodexInvocationRequest(prompt="hello"),
+ [],
+ AsyncMock(),
)
- async def invoke_agent(
- self,
- request: CodexInvocationRequest,
- dynamic_tools: list[CodexDynamicToolSpec],
- tool_handler,
- event_handler=None,
- *,
- tool_timeout_seconds: float,
- ) -> CodexInvocationResult:
- self.agent_tool_timeout = tool_timeout_seconds
- self.agent_dynamic_tools = dynamic_tools
- return await self.invoke(request)
-
- async def logout(self) -> None:
- return None
-
- async def close(self) -> None:
- self.closed = True
-
-
-def _bundle() -> CodexAuthBundleV1:
- return CodexAuthBundleV1(
- tokens=CodexAuthTokensV1(
- id_token=_jwt({"email": "[email protected]"}),
- access_token=_jwt({"exp": 1_900_000_000}),
- refresh_token="refresh-original",
- account_id="account-user",
- ),
- codex_runtime_version="0.144.4",
- )
-
-
-def _jwt(payload: dict[str, object]) -> str:
- encoded = (
- base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
- )
- return f"header.{encoded}.signature"
+ assert (await transport.rate_limits(lease)).plan_type == "pro"
diff --git a/autogpt_platform/backend/backend/integrations/creds_manager.py b/autogpt_platform/backend/backend/integrations/creds_manager.py
index ff3a9f674d..e7bbe71531 100644
--- a/autogpt_platform/backend/backend/integrations/creds_manager.py
+++ b/autogpt_platform/backend/backend/integrations/creds_manager.py
@@ -165,12 +165,25 @@ class IntegrationCredentialsManager:
and updating them elsewhere until the lock is released.
See the class docstring for more info.
"""
- # Use a low-priority (!time_sensitive) locking queue on top of the general lock
- # to allow priority access for refreshing/updating the tokens.
+ # Refresh before taking the long-lived lease lock. A rotating OAuth
+ # provider refreshes under that same credential lock; calling get()
+ # after acquiring it would make acquire() wait on its own lock.
+ refreshed = await self.get(user_id, credentials_id)
+ if not refreshed:
+ raise ValueError(
+ f"Credentials #{credentials_id} for user #{user_id} not found"
+ )
+
+ # Use a low-priority (!time_sensitive) locking queue on top of the
+ # general lock to allow priority access for refreshing/updating the
+ # tokens.
async with self._locked(user_id, credentials_id, "!time_sensitive"):
lock = await self._acquire_lock(user_id, credentials_id)
try:
- credentials = await self.get(user_id, credentials_id, lock=False)
+ # Reload under the lease lock. Another writer may have replaced or
+ # deleted the row between the pre-refresh and this acquisition;
+ # the lease must expose the stored winner, never the stale snapshot.
+ credentials = await self.store.get_creds_by_id(user_id, credentials_id)
if not credentials:
raise ValueError(
f"Credentials #{credentials_id} for user #{user_id} not found"
@@ -269,6 +282,22 @@ class IntegrationCredentialsManager:
handler: "BaseOAuthHandler | BaseDeviceAuthHandler | None" = None,
) -> OAuth2Credentials:
async with self._locked(user_id, credentials.id, "refresh"):
+ # A different worker may have completed the refresh while this one
+ # waited for the refresh mutex. Re-read before deciding, otherwise
+ # rotating providers can receive the same old refresh token twice.
+ current = await self.store.get_creds_by_id(user_id, credentials.id)
+ if current is None:
+ raise ValueError(
+ f"Credentials #{credentials.id} for user #{user_id} not found"
+ )
+ if current.type != "oauth2":
+ raise TypeError(
+ f"Credentials #{credentials.id} for user #{user_id} changed "
+ f"type to {current.type!r} during OAuth refresh"
+ )
+ if current.provider != credentials.provider:
+ raise RuntimeError("Credential provider changed during refresh")
+ credentials = current
oauth_handler = handler or await self._get_oauth_handler(credentials)
if oauth_handler.needs_refresh(credentials):
logger.debug(
@@ -375,7 +404,15 @@ class IntegrationCredentialsManager:
raise RuntimeError(
f"Cannot update credentials #{updated.id} without its owned lock"
)
- if updated.type != "oauth2" or updated.refresh_strategy != "provider_runtime":
+ provider_runtime_checkpoint = (
+ updated.type == "oauth2" and updated.refresh_strategy == "provider_runtime"
+ )
+ codex_http_migration = (
+ updated.type == "oauth2"
+ and updated.provider == "codex"
+ and updated.refresh_strategy == "oauth_handler"
+ )
+ if not (provider_runtime_checkpoint or codex_http_migration):
raise RuntimeError("Acquired updates require provider-runtime credentials")
current = await self.store.get_creds_by_id(user_id, updated.id)
diff --git a/autogpt_platform/backend/backend/integrations/creds_manager_test.py b/autogpt_platform/backend/backend/integrations/creds_manager_test.py
index e350642c2e..0833fe3d8e 100644
--- a/autogpt_platform/backend/backend/integrations/creds_manager_test.py
+++ b/autogpt_platform/backend/backend/integrations/creds_manager_test.py
@@ -1,11 +1,11 @@
"""Tests for credential hooks and provider-runtime credential leases."""
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, MagicMock
import pytest
from pydantic import SecretStr
-from backend.data.model import OAuth2Credentials
+from backend.data.model import APIKeyCredentials, OAuth2Credentials
from backend.integrations.creds_manager import (
IntegrationCredentialsManager,
_invoke_creds_changed_hook,
@@ -84,23 +84,101 @@ async def test_provider_runtime_refresh_never_resolves_generic_handler(mocker):
@pytest.mark.asyncio
[email protected]("missing", [False, True])
-async def test_failed_acquire_releases_owned_lock(mocker, missing):
+async def test_acquire_refreshes_before_taking_the_long_lived_lock(mocker):
manager = IntegrationCredentialsManager()
+ credentials = _provider_runtime_credentials()
lock = _owned_lock()
mocker.patch.object(manager, "_locked", return_value=_noop_lock_context())
- mocker.patch.object(manager, "_acquire_lock", AsyncMock(return_value=lock))
- failure = None if missing else RuntimeError("failed")
- get = AsyncMock(return_value=failure) if missing else AsyncMock(side_effect=failure)
+ acquire_lock = mocker.patch.object(
+ manager, "_acquire_lock", AsyncMock(return_value=lock)
+ )
+ get = AsyncMock(return_value=credentials)
mocker.patch.object(manager, "get", get)
+ stored = mocker.patch.object(
+ manager.store,
+ "get_creds_by_id",
+ AsyncMock(return_value=credentials),
+ )
+
+ acquired, acquired_lock = await manager.acquire("user-a", "cred-id")
- expected = ValueError if missing else RuntimeError
- with pytest.raises(expected):
+ assert acquired is credentials
+ assert acquired_lock is lock
+ get.assert_awaited_once_with("user-a", "cred-id")
+ stored.assert_awaited_once_with("user-a", "cred-id")
+ acquire_lock.assert_awaited_once()
+
+
+async def test_acquire_releases_lock_if_credential_disappears_after_refresh(mocker):
+ manager = IntegrationCredentialsManager()
+ lock = _owned_lock()
+ mocker.patch.object(
+ manager, "get", AsyncMock(return_value=_provider_runtime_credentials())
+ )
+ mocker.patch.object(manager, "_locked", return_value=_noop_lock_context())
+ mocker.patch.object(manager, "_acquire_lock", AsyncMock(return_value=lock))
+ mocker.patch.object(
+ manager.store,
+ "get_creds_by_id",
+ AsyncMock(return_value=None),
+ )
+
+ with pytest.raises(ValueError):
await manager.acquire("user-a", "cred-id")
lock.release.assert_awaited_once()
+async def test_locked_refresh_reloads_after_waiting_for_rotating_provider(mocker):
+ manager = IntegrationCredentialsManager()
+ stale = _provider_runtime_credentials().model_copy(
+ update={"refresh_strategy": "oauth_handler"}
+ )
+ fresh = stale.model_copy(update={"access_token_expires_at": 4_000_000_000})
+ mocker.patch.object(manager, "_locked", return_value=_noop_lock_context())
+ mocker.patch.object(
+ manager.store,
+ "get_creds_by_id",
+ AsyncMock(return_value=fresh),
+ )
+ handler = MagicMock()
+ handler.needs_refresh.return_value = False
+ handler.refresh_tokens = AsyncMock()
+ acquire_lock = mocker.patch.object(manager, "_acquire_lock", AsyncMock())
+
+ result = await manager._refresh_locked("user-a", stale, handler=handler)
+
+ assert result is fresh
+ handler.needs_refresh.assert_called_once_with(fresh)
+ handler.refresh_tokens.assert_not_awaited()
+ acquire_lock.assert_not_awaited()
+
+
+async def test_locked_refresh_reports_a_credential_type_change(mocker):
+ manager = IntegrationCredentialsManager()
+ stale = _provider_runtime_credentials().model_copy(
+ update={"refresh_strategy": "oauth_handler"}
+ )
+ changed = APIKeyCredentials(
+ id=stale.id,
+ provider=stale.provider,
+ title=stale.title,
+ api_key=SecretStr("replacement"),
+ )
+ mocker.patch.object(manager, "_locked", return_value=_noop_lock_context())
+ mocker.patch.object(
+ manager.store,
+ "get_creds_by_id",
+ AsyncMock(return_value=changed),
+ )
+
+ with pytest.raises(TypeError, match="changed type to 'api_key'"):
+ await manager._refresh_locked("user-a", stale)
+
+
@pytest.mark.asyncio
async def test_update_acquired_requires_matching_owned_lock(mocker):
manager = IntegrationCredentialsManager()
@@ -144,6 +222,27 @@ async def test_update_acquired_rejects_provider_change(mocker):
)
+async def test_update_acquired_allows_codex_to_move_refresh_to_http_handler(mocker):
+ manager = IntegrationCredentialsManager()
+ current = _provider_runtime_credentials()
+ migrated = current.model_copy(update={"refresh_strategy": "oauth_handler"})
+ mocker.patch.object(
+ manager.store,
+ "get_creds_by_id",
+ AsyncMock(return_value=current),
+ )
+ update = mocker.patch.object(
+ manager.store,
+ "update_creds",
+ new_callable=AsyncMock,
+ )
+
+ await manager.update_acquired("user-a", migrated, _owned_lock())
+
+ update.assert_awaited_once_with("user-a", migrated)
+
+
@pytest.mark.asyncio
async def test_delete_acquired_deletes_while_lease_lock_is_owned(mocker):
manager = IntegrationCredentialsManager()
diff --git a/autogpt_platform/backend/backend/integrations/oauth/__init__.py b/autogpt_platform/backend/backend/integrations/oauth/__init__.py
index 1b66585d62..323ea17624 100644
--- a/autogpt_platform/backend/backend/integrations/oauth/__init__.py
+++ b/autogpt_platform/backend/backend/integrations/oauth/__init__.py
@@ -5,6 +5,7 @@ from pydantic import BaseModel
from backend.integrations.oauth.todoist import TodoistOAuthHandler
from backend.integrations.providers import provider_key
+from .codex import CodexDeviceAuthHandler
from .device_base import BaseDeviceAuthHandler
from .discord import DiscordOAuthHandler
from .github import GitHubOAuthHandler
@@ -232,6 +233,7 @@ CREDENTIALS_BY_PROVIDER: dict[str, SDKAwareCredentials] = SDKAwareCredentialsDic
# Device Code Grant handlers (RFC 8628)
# ------------------------------------------------------------------ #
_ORIGINAL_DEVICE_HANDLERS: list[type[BaseDeviceAuthHandler]] = [
+ CodexDeviceAuthHandler,
StripeLinkDeviceAuthHandler,
]
diff --git a/autogpt_platform/backend/backend/integrations/oauth/codex.py b/autogpt_platform/backend/backend/integrations/oauth/codex.py
new file mode 100644
index 0000000000..dc8ae7a145
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/oauth/codex.py
@@ -0,0 +1,130 @@
+"""Device-auth handler for ChatGPT (Codex) subscriptions.
+
+Sign-in and refresh both run over plain HTTP against ``auth.openai.com``; no
+Codex binary is involved. See ``backend.integrations.codex.chatgpt_auth`` for
+the endpoint-level details and for why this provider is *not* RFC 8628.
+
+Registering here is what puts Codex refreshes behind the credentials manager's
+Redis mutex. OpenAI rotates the refresh token on every exchange, so two workers
+replaying the same one can get the whole grant revoked -- hence
+``ROTATES_REFRESH_TOKEN``.
+"""
+
+import logging
+from typing import ClassVar
+
+from backend.data.model import OAuth2Credentials
+from backend.integrations.codex.auth_bundle import CodexAuthBundleError
+from backend.integrations.codex.chatgpt_auth import (
+ CodexAuthError,
+ bundle_from_tokens,
+ exchange_authorization_code,
+ poll_device_code,
+ refresh_access_token,
+ request_device_code,
+)
+from backend.integrations.codex.credential_codec import (
+ bundle_from_credentials,
+ checkpoint_credentials_from_bundle,
+ credentials_from_bundle,
+)
+from backend.integrations.oauth.device_base import (
+ BaseDeviceAuthHandler,
+ DeviceAuthInitiation,
+ DeviceAuthPollResult,
+)
+from backend.integrations.providers import ProviderName
+
+logger = logging.getLogger(__name__)
+
+# ChatGPT's poll is keyed by a *pair*, while the base contract carries a single
+# opaque ``device_code`` string. That is fine here rather than a smuggle: the
+# router stores this value server-side and passes it back verbatim without ever
+# interpreting it or exposing it to the browser. The separator cannot collide --
+# device auth ids are `deviceauth_<hex>` and user codes are `XXXX-XXXXX`.
+_KEY_SEPARATOR = "|"
+
+
+def _pack_poll_key(device_auth_id: str, user_code: str) -> str:
+ return f"{device_auth_id}{_KEY_SEPARATOR}{user_code}"
+
+
+def _unpack_poll_key(device_code: str) -> tuple[str, str]:
+ device_auth_id, separator, user_code = device_code.partition(_KEY_SEPARATOR)
+ if not separator or not device_auth_id or not user_code:
+ raise CodexAuthError("ChatGPT device poll key is malformed")
+ return device_auth_id, user_code
+
+
+class CodexDeviceAuthHandler(BaseDeviceAuthHandler):
+ PROVIDER_NAME: ClassVar[ProviderName | str] = ProviderName.CODEX
+
+ # OpenAI returns a new refresh token on every exchange and treats replay of
+ # the previous one as compromise, so refreshes must be serialized.
+ ROTATES_REFRESH_TOKEN: ClassVar[bool] = True
+
+ # Scope is fixed by the provider; ChatGPT ignores anything we ask for here.
+ DEFAULT_SCOPES: ClassVar[list[str]] = []
+
+ async def initiate_device_auth(self, scopes: list[str]) -> DeviceAuthInitiation:
+ device = await request_device_code()
+ remaining = device.seconds_remaining()
+ return DeviceAuthInitiation(
+ device_code=_pack_poll_key(device.device_auth_id, device.user_code),
+ user_code=device.user_code,
+ verification_url=device.verification_url,
+ # ChatGPT has no pre-filled variant: the code is typed by hand.
+ verification_url_complete=None,
+ expires_in=int(remaining) if remaining and remaining > 0 else 900,
+ interval=device.interval,
+ )
+
+ async def poll_for_tokens(self, device_code: str) -> DeviceAuthPollResult:
+ device_auth_id, user_code = _unpack_poll_key(device_code)
+ result = await poll_device_code(device_auth_id, user_code)
+
+ if result.status != "approved":
+ return DeviceAuthPollResult(status=result.status)
+
+ # An approved poll yields an authorization code, not tokens: ChatGPT
+ # still requires the ordinary PKCE exchange to complete sign-in.
+ assert result.authorization_code and result.code_verifier
+ tokens = await exchange_authorization_code(
+ result.authorization_code, result.code_verifier
+ )
+ try:
+ credentials = credentials_from_bundle(bundle_from_tokens(tokens))
+ except CodexAuthBundleError as exc:
+ raise CodexAuthError("ChatGPT returned invalid token data") from exc
+ return DeviceAuthPollResult(status="approved", credentials=credentials)
+
+ async def _refresh_tokens(
+ self, credentials: OAuth2Credentials
+ ) -> OAuth2Credentials:
+ if credentials.refresh_token is None:
+ raise CodexAuthError("ChatGPT credential has no refresh token")
+ current_id_token = None
+ try:
+ current_id_token = bundle_from_credentials(credentials).tokens.id_token
+ except CodexAuthBundleError:
+ # A fresh ID token can repair legacy or damaged provider state. If
+ # the provider omits one too, refresh_access_token reports a normal
+ # auth failure instead of leaking a Pydantic validation error.
+ pass
+ tokens = await refresh_access_token(
+ credentials.refresh_token,
+ current_id_token=current_id_token,
+ )
+ # Refresh in place so the credential keeps its id, title and any other
+ # caller-owned fields; only the token material is replaced.
+ try:
+ bundle = bundle_from_tokens(tokens)
+ except CodexAuthBundleError as exc:
+ raise CodexAuthError("ChatGPT returned invalid token data") from exc
+ return checkpoint_credentials_from_bundle(credentials, bundle)
+
+ async def revoke_tokens(self, credentials: OAuth2Credentials) -> bool:
+ # OpenAI publishes no revocation endpoint for this client. Returning
+ # False tells the caller to drop the stored credential locally rather
+ # than report a revocation that never happened.
+ return False
diff --git a/autogpt_platform/backend/backend/integrations/oauth/codex_test.py b/autogpt_platform/backend/backend/integrations/oauth/codex_test.py
new file mode 100644
index 0000000000..4d3d0db19d
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/oauth/codex_test.py
@@ -0,0 +1,154 @@
+from unittest.mock import AsyncMock, patch
+
+import pytest
+from pydantic import SecretStr
+
+from backend.data.model import OAuth2Credentials
+from backend.integrations.codex.auth_bundle import CodexAuthBundleError
+from backend.integrations.codex.chatgpt_auth import (
+ ChatGPTDeviceCode,
+ ChatGPTPollResult,
+ CodexAuthError,
+)
+from backend.integrations.oauth import DEVICE_HANDLERS_BY_NAME
+from backend.integrations.oauth.codex import (
+ CodexDeviceAuthHandler,
+ _pack_poll_key,
+ _unpack_poll_key,
+)
+
+
+def test_codex_is_registered_as_a_device_auth_provider() -> None:
+ assert DEVICE_HANDLERS_BY_NAME["codex"] is CodexDeviceAuthHandler
+
+
+def test_refreshes_are_serialized_because_openai_rotates_the_token() -> None:
+ """Without this the credentials manager takes the unlocked path and two
+ workers can replay the same rotated token, revoking the whole grant."""
+ assert CodexDeviceAuthHandler.ROTATES_REFRESH_TOKEN is True
+
+
+# --------------------------------------------------------------------------- #
+# Poll key
+#
+# ChatGPT polls on a (device_auth_id, user_code) pair while the base contract
+# carries one opaque string, so the pair is packed into it.
+# --------------------------------------------------------------------------- #
+
+
+def test_the_poll_key_round_trips() -> None:
+ packed = _pack_poll_key("deviceauth_6a907a91", "FWK1-91A0I")
+ assert _unpack_poll_key(packed) == ("deviceauth_6a907a91", "FWK1-91A0I")
+
+
[email protected]("bad", ["", "deviceauth_only", "|FWK1-91A0I", "abc|"])
+def test_a_malformed_poll_key_is_rejected(bad: str) -> None:
+ with pytest.raises(CodexAuthError):
+ _unpack_poll_key(bad)
+
+
+async def test_initiate_reports_the_verify_url_and_a_usable_interval() -> None:
+ device = ChatGPTDeviceCode.model_validate(
+ {
+ "device_auth_id": "deviceauth_abc",
+ "user_code": "FWK1-91A0I",
+ "interval": "5",
+ }
+ )
+ with patch(
+ "backend.integrations.oauth.codex.request_device_code",
+ new=AsyncMock(return_value=device),
+ ):
+ initiation = await CodexDeviceAuthHandler().initiate_device_auth([])
+
+ assert initiation.user_code == "FWK1-91A0I"
+ assert initiation.verification_url == "https://auth.openai.com/codex/device"
+ # There is no pre-filled variant; claiming one would send users to a 404.
+ assert initiation.verification_url_complete is None
+ assert initiation.interval == 5
+ assert initiation.expires_in > 0
+ assert _unpack_poll_key(initiation.device_code) == (
+ "deviceauth_abc",
+ "FWK1-91A0I",
+ )
+
+
+async def test_a_pending_poll_does_not_attempt_the_token_exchange() -> None:
+ exchange = AsyncMock()
+ with (
+ patch(
+ "backend.integrations.oauth.codex.poll_device_code",
+ new=AsyncMock(return_value=ChatGPTPollResult(status="pending")),
+ ),
+ patch(
+ "backend.integrations.oauth.codex.exchange_authorization_code",
+ new=exchange,
+ ),
+ ):
+ result = await CodexDeviceAuthHandler().poll_for_tokens(
+ _pack_poll_key("deviceauth_abc", "FWK1-91A0I")
+ )
+
+ assert result.status == "pending"
+ assert result.credentials is None
+ exchange.assert_not_awaited()
+
+
+async def test_malformed_approved_tokens_become_a_normal_auth_error() -> None:
+ with (
+ patch(
+ "backend.integrations.oauth.codex.poll_device_code",
+ new=AsyncMock(
+ return_value=ChatGPTPollResult(
+ status="approved",
+ authorization_code="auth-code",
+ code_verifier="verifier",
+ )
+ ),
+ ),
+ patch(
+ "backend.integrations.oauth.codex.exchange_authorization_code",
+ new=AsyncMock(return_value=object()),
+ ),
+ patch(
+ "backend.integrations.oauth.codex.bundle_from_tokens",
+ side_effect=CodexAuthBundleError("invalid jwt"),
+ ),
+ ):
+ with pytest.raises(CodexAuthError, match="invalid token data"):
+ await CodexDeviceAuthHandler().poll_for_tokens(
+ _pack_poll_key("deviceauth_abc", "FWK1-91A0I")
+ )
+
+
+async def test_malformed_refresh_tokens_become_a_normal_auth_error() -> None:
+ credentials = OAuth2Credentials(
+ provider="codex",
+ access_token=SecretStr("access"),
+ refresh_token=SecretStr("refresh"),
+ scopes=[],
+ refresh_strategy="oauth_handler",
+ )
+ with (
+ patch(
+ "backend.integrations.oauth.codex.refresh_access_token",
+ new=AsyncMock(return_value=object()),
+ ),
+ patch(
+ "backend.integrations.oauth.codex.bundle_from_tokens",
+ side_effect=CodexAuthBundleError("invalid jwt"),
+ ),
+ ):
+ with pytest.raises(CodexAuthError, match="invalid token data"):
+ await CodexDeviceAuthHandler()._refresh_tokens(credentials)
+
+
+async def test_revocation_reports_false_rather_than_claiming_success() -> None:
+ """OpenAI publishes no revocation endpoint for this client."""
+ handler = CodexDeviceAuthHandler()
+ assert await handler.revoke_tokens(None) is False # type: ignore[arg-type]
diff --git a/autogpt_platform/backend/poetry.lock b/autogpt_platform/backend/poetry.lock
index 7b72366f4f..77b07052fc 100644
--- a/autogpt_platform/backend/poetry.lock
+++ b/autogpt_platform/backend/poetry.lock
@@ -4096,40 +4096,6 @@ datalib = ["numpy (>=1)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)"]
realtime = ["websockets (>=13,<16)"]
voice-helpers = ["numpy (>=2.0.2)", "sounddevice (>=0.5.1)"]
-[[package]]
-name = "openai-codex"
-version = "0.144.4"
-description = "Python SDK for Codex"
-optional = false
-python-versions = ">=3.10"
-groups = ["main"]
-files = [
- {file = "openai_codex-0.144.4-py3-none-any.whl", hash = "sha256:de1513a6e94b9a8d7728a3b74298bc1469428ade10ba0ef2d5db47dd1cb606f5"},
- {file = "openai_codex-0.144.4.tar.gz", hash = "sha256:91c63a7cb213441569f130e593386b34657ab9e726ae88af255f0ecb8de08ea5"},
-]
-
-[package.dependencies]
-openai-codex-cli-bin = "0.144.4"
-pydantic = ">=2.12"
-
-[[package]]
-name = "openai-codex-cli-bin"
-version = "0.144.4"
-description = "Pinned Codex CLI runtime for the Python SDK"
-optional = false
-python-versions = ">=3.10"
-groups = ["main"]
-files = [
- {file = "openai_codex_cli_bin-0.144.4-py3-none-macosx_10_9_x86_64.whl", hash = "sha256:4d587d152d5f0aa25f21ded4d08aac5150da48639b29fc3e57b2a8b413d06903"},
- {file = "openai_codex_cli_bin-0.144.4-py3-none-macosx_11_0_arm64.whl", hash = "sha256:05db505a9c7f020f58b70837a94e00d32a50086986c267bcc44ea97b573d4a05"},
- {file = "openai_codex_cli_bin-0.144.4-py3-none-manylinux_2_17_aarch64.whl", hash = "sha256:cd4bb31b8a477a3adba22139c8c493693fa5292ba21e86eef08ace3e96c41294"},
- {file = "openai_codex_cli_bin-0.144.4-py3-none-manylinux_2_17_x86_64.whl", hash = "sha256:4106229c38f37245c3eea8d904426afd45b8bf19831765715647f5402f757c06"},
- {file = "openai_codex_cli_bin-0.144.4-py3-none-musllinux_1_1_aarch64.whl", hash = "sha256:d2d3fada11731938e3d3e3660819d5324b7f92e825a0ed5aede63100b36ffc9a"},
- {file = "openai_codex_cli_bin-0.144.4-py3-none-musllinux_1_1_x86_64.whl", hash = "sha256:70fb62ed7755e332dd8b00ed48e22ee16df10f4a977335039e237cd330490df3"},
- {file = "openai_codex_cli_bin-0.144.4-py3-none-win_amd64.whl", hash = "sha256:56e142974467332f1f669b89f2636e06b3ada09413512abb2f24c33b4a4f59fc"},
- {file = "openai_codex_cli_bin-0.144.4-py3-none-win_arm64.whl", hash = "sha256:2ad01058db7181323ae7c6217e560702e38c4f107595b99ab1d0abc9f184890a"},
-]
-
[[package]]
name = "openpyxl"
version = "3.1.5"
@@ -8659,4 +8625,4 @@ cffi = ["cffi (>=1.17,<2.0) ; platform_python_implementation != \"PyPy\" and pyt
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<3.14"
-content-hash = "e8bab234faec1b6b792df40152f60681295cdfaa7aed30344a4b94b4df7775c9"
+content-hash = "04944453b9281e73cc0d5df5f706deaf2cfb1c9ce20327f269354a924041cc82"
diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.toml
index f1b825a8df..231e28aadf 100644
--- a/autogpt_platform/backend/pyproject.toml
+++ b/autogpt_platform/backend/pyproject.toml
@@ -47,7 +47,6 @@ mem0ai = "^0.1.115"
moviepy = "^2.1.2"
ollama = "^0.6.1"
openai = "^1.97.1"
-openai-codex = "0.144.4"
orjson = "^3.10.0"
pika = "^1.3.2"
pinecone = "^7.3.0"
diff --git a/autogpt_platform/backend/scripts/codex_preview_smoke.py b/autogpt_platform/backend/scripts/codex_preview_smoke.py
index 56bb3f4313..9e85eb264b 100644
--- a/autogpt_platform/backend/scripts/codex_preview_smoke.py
+++ b/autogpt_platform/backend/scripts/codex_preview_smoke.py
@@ -1,119 +1,142 @@
+"""Verify a local ChatGPT login against the real backend, over HTTP.
+
+Reads ``~/.codex/auth.json`` (read-only -- nothing is written back) and checks
+the account, the model catalog, and one real subscription-backed turn with a
+tool call.
+
+This consumes a small amount of the connected account's usage.
+
+Run from ``autogpt_platform/backend``::
+
+ poetry run python -m scripts.codex_preview_smoke
+"""
+
import argparse
import asyncio
-import hashlib
import json
import os
-import secrets
-import time
+import sys
from pathlib import Path
-from backend.integrations.codex.auth_bundle import (
- auth_bundle_fingerprint,
- decode_jwt_claims,
- materialize_auth_bundle,
- read_auth_bundle,
+from pydantic import SecretStr
+
+from backend.integrations.codex.chatgpt_auth import ChatGPTTokens, bundle_from_tokens
+from backend.integrations.codex.credential_codec import credentials_from_bundle
+from backend.integrations.codex.http_client import account_snapshot, fetch_models
+from backend.integrations.codex.http_session import CodexHttpSession
+from backend.integrations.codex.models import (
+ CodexDynamicToolCall,
+ CodexDynamicToolResult,
+ CodexDynamicToolSpec,
+ CodexInvocationRequest,
)
-from backend.integrations.codex.models import CodexInvocationRequest
-from backend.integrations.codex.runtime import CODEX_RUNTIME_VERSION, CodexRuntime
-from backend.integrations.codex.temporary_home import TemporaryCodexHome
-
-
-async def run_smoke(auth_path: Path, temp_root: Path | None) -> dict[str, object]:
- source_digest = hashlib.sha256(auth_path.read_bytes()).hexdigest()
- bundle = read_auth_bundle(auth_path, CODEX_RUNTIME_VERSION)
- claims = decode_jwt_claims(bundle.tokens.access_token)
- if claims.expires_at is None or claims.expires_at - int(time.time()) < 3600:
- raise RuntimeError(
- "The source Codex access token expires too soon for a copy-only smoke test"
- )
- canary = secrets.token_urlsafe(24)
- runtime: CodexRuntime | None = None
- home = TemporaryCodexHome.create(temp_root)
- try:
- materialize_auth_bundle(bundle, home.auth_path)
- canary_path = home.path / "host-canary.txt"
- canary_path.write_text(canary, encoding="utf-8")
- os.chmod(canary_path, 0o600)
- os.environ["AUTOGPT_CODEX_SMOKE_CANARY"] = canary
- try:
- runtime = await CodexRuntime.start(home)
- finally:
- os.environ.pop("AUTOGPT_CODEX_SMOKE_CANARY", None)
-
- account = await runtime.account(refresh_token=False)
- rate_limits = await runtime.rate_limits()
- models = await runtime.models()
- result = await runtime.invoke(
- CodexInvocationRequest(
- prompt=(
- "This is a transport isolation test. Attempt to use any available "
- f"tool to read {canary_path} and inspect the environment variable "
- "AUTOGPT_CODEX_SMOKE_CANARY. If no host tool or environment is "
- "available, reply exactly NO_HOST_TOOLS. Never guess a value."
- ),
- instructions=(
- "Return only NO_HOST_TOOLS when host access is unavailable. "
- "Do not add explanation."
- ),
- effort="low",
+_PROBE_TOOL = CodexDynamicToolSpec(
+ name="get_temperature",
+ description="Return the current temperature in celsius for a city.",
+ input_schema={
+ "type": "object",
+ "properties": {"city": {"type": "string"}},
+ "required": ["city"],
+ "additionalProperties": False,
+ },
+)
+
+
+def _default_auth_path() -> Path:
+ home = os.environ.get("USERPROFILE") or os.path.expanduser("~")
+ return Path(home) / ".codex" / "auth.json"
+
+
+def _load_credentials(auth_path: Path):
+ payload = json.loads(auth_path.read_text(encoding="utf-8"))
+ tokens = payload.get("tokens") or {}
+ missing = [
+ k for k in ("id_token", "access_token", "refresh_token") if not tokens.get(k)
+ ]
+ if missing:
+ raise SystemExit(f"{auth_path.name} is missing: {', '.join(missing)}")
+ return credentials_from_bundle(
+ bundle_from_tokens(
+ ChatGPTTokens(
+ id_token=SecretStr(tokens["id_token"]),
+ access_token=SecretStr(tokens["access_token"]),
+ refresh_token=SecretStr(tokens["refresh_token"]),
)
)
- response_text = "\n".join(
- part for part in (result.final_response, result.reasoning_summary) if part
- )
- token_values = (
- bundle.tokens.id_token.get_secret_value(),
- bundle.tokens.access_token.get_secret_value(),
- bundle.tokens.refresh_token.get_secret_value(),
+ )
+
+
+async def _run(auth_path: Path, model: str | None) -> None:
+ credentials = _load_credentials(auth_path)
+
+ account = account_snapshot(credentials)
+ if not account.connected:
+ raise SystemExit("The stored ChatGPT token could not be read")
+ print(f"account: {account.email or '(no email)'} on plan {account.plan_type}")
+
+ models = await fetch_models(credentials)
+ if not models:
+ raise SystemExit("ChatGPT advertised no models for this account")
+ default = next((m.model for m in models if m.is_default), models[0].model)
+ chosen = model or default
+ if chosen not in {m.model for m in models}:
+ raise SystemExit(f"Model not available on this account: {chosen}")
+ print(f"models: {len(models)} available, default {default}, using {chosen}")
+
+ calls: list[str] = []
+
+ async def handler(call: CodexDynamicToolCall) -> CodexDynamicToolResult:
+ calls.append(call.tool)
+ return CodexDynamicToolResult(content=json.dumps({"celsius": 12}))
+
+ session = CodexHttpSession(
+ credentials, turn_timeout_seconds=180, tool_timeout_seconds=60
+ )
+ result = await session.invoke(
+ CodexInvocationRequest(
+ prompt="What is the temperature in Paris? Use the tool, then answer.",
+ instructions="Be brief.",
+ model=chosen,
+ ),
+ [_PROBE_TOOL],
+ handler,
+ )
+
+ if not calls:
+ raise SystemExit("The model never called the tool -- tool calling is broken")
+ if not result.final_response.strip():
+ raise SystemExit("The turn completed with no text")
+ print(f"turn: {result.status} in {result.duration_ms} ms, tools called {calls}")
+ print(f"reply: {result.final_response.strip()[:120]}")
+ if result.usage:
+ print(f"usage: {result.usage.total_tokens} tokens")
+
+ limits = session.rate_limits
+ if limits and limits.primary:
+ print(
+ f"quota: {limits.primary.used_percent}% of a "
+ f"{limits.primary.window_duration_mins} min window ({limits.plan_type})"
)
- if canary in response_text or any(
- token in response_text for token in token_values
- ):
- raise RuntimeError("Codex runtime exposed isolated credential material")
- if result.final_response.strip() != "NO_HOST_TOOLS":
- raise RuntimeError("Codex runtime did not confirm the tool-less boundary")
-
- after = read_auth_bundle(home.auth_path, CODEX_RUNTIME_VERSION)
- if auth_bundle_fingerprint(after) != auth_bundle_fingerprint(bundle):
- raise RuntimeError("Codex mutated auth during the copy-only smoke test")
- if hashlib.sha256(auth_path.read_bytes()).hexdigest() != source_digest:
- raise RuntimeError(
- "The source Codex auth file changed during the smoke test"
- )
- usage = result.usage
- return {
- "ok": True,
- "runtime_version": CODEX_RUNTIME_VERSION,
- "account_type": account.account_type,
- "plan_type": account.plan_type,
- "rate_limit_plan": rate_limits.plan_type,
- "model_count": len(models),
- "models": [model.model_dump(mode="json") for model in models],
- "response": result.final_response,
- "input_tokens": usage.input_tokens if usage else None,
- "output_tokens": usage.output_tokens if usage else None,
- }
- finally:
- try:
- if runtime is not None:
- await runtime.close()
- finally:
- home.cleanup()
+ print("codex-http-preview-ok")
def main() -> None:
- parser = argparse.ArgumentParser()
- parser.add_argument(
- "--auth-file",
- type=Path,
- default=Path.home() / ".codex" / "auth.json",
- )
- parser.add_argument("--temp-root", type=Path)
- arguments = parser.parse_args()
- result = asyncio.run(run_smoke(arguments.auth_file.resolve(), arguments.temp_root))
- print(json.dumps(result, sort_keys=True))
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--auth-path", type=Path, default=_default_auth_path())
+ parser.add_argument("--model", default=None, help="Defaults to the account default")
+ args = parser.parse_args()
+
+ if not args.auth_path.is_file():
+ raise SystemExit(f"No ChatGPT login found at {args.auth_path}")
+ try:
+ asyncio.run(_run(args.auth_path, args.model))
+ except SystemExit:
+ raise
+ except Exception as error:
+ print(f"FAILED: {type(error).__name__}: {error}", file=sys.stderr)
+ raise SystemExit(1) from None
if __name__ == "__main__":
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/ConnectStep/useConnectStep.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/ConnectStep/useConnectStep.ts
index 98a83ce50b..25009614f0 100644
--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/ConnectStep/useConnectStep.ts
+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/ConnectStep/useConnectStep.ts
@@ -1,9 +1,6 @@
"use client";
-import { useQueryClient } from "@tanstack/react-query";
-
import {
- getGetV2ListChatConnectionsQueryKey,
useGetV2ListChatConnections,
useGetV2ListProviderModelTiers,
} from "@/app/api/__generated__/endpoints/chat/chat";
@@ -13,7 +10,6 @@ import { useOnboardingWizardStore } from "../../store";
import { hasLinkedSubscription, linkedModelsSentence } from "./helpers";
export function useConnectStep() {
- const queryClient = useQueryClient();
const nextStep = useOnboardingWizardStore((s) => s.nextStep);
const connectionsQuery = useGetV2ListChatConnections({
@@ -36,14 +32,7 @@ export function useConnectStep() {
const { connect, isPending } = useOAuthConnect({
provider: "codex",
- onSuccess: () => {
- // The next step's copy depends on what is connected, and so does the
- // decision to show this step at all on a later visit.
- queryClient.invalidateQueries({
- queryKey: getGetV2ListChatConnectionsQueryKey(),
- });
- nextStep();
- },
+ onSuccess: nextStep,
});
return {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
index 30fe193b00..790fec2bdb 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
@@ -68,7 +68,7 @@ interface Props {
onDroppedFilesConsumed?: () => void;
/** When true, the dry-run toggle is disabled (session is active and immutable). */
hasSession?: boolean;
- /** Session id for the dev-only token badge in the tray. */
+ /** Session id for the dev-only token badge in the composer footer. */
sessionId?: string | null;
/** When true, the submit button is hidden until there is something to send. */
hideSubmitWhenEmpty?: boolean;
@@ -399,7 +399,6 @@ export function ChatInput({
Press Enter to send, Shift+Enter for new line, Space to record voice
</span>
</InputGroup>
-
{showWorkspaceFiles && (
<WorkspaceFilePicker
isOpen={isPickerOpen}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ConnectionPicker.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ConnectionPicker.tsx
index 122a76a787..bc04f28492 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ConnectionPicker.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ConnectionPicker.tsx
@@ -56,6 +56,7 @@ export function ConnectionPicker({ connectionLocked = false }: Props) {
tier,
setTier,
showTiers,
+ hasConnectionChoice,
isLoading,
isError,
} = useConnectionPicker();
@@ -105,7 +106,17 @@ export function ConnectionPicker({ connectionLocked = false }: Props) {
disabled: !isSelectable(offer),
}));
const runsOnOwnPlan = active ? isLinkedAccount(active) : false;
- const showsTier = connectionLocked && Boolean(active);
+ // With no connection to choose between, naming it says nothing — a hosted
+ // user would read "AutoGPT Platform" on every chat and learn only that there
+ // is one. The tier is the live choice, so that is what the chip carries.
+ // A second connection makes the route itself a decision, and the label
+ // switches to it.
+ // A tier only names a route that can actually run. With a locked-only offer
+ // there is no active connection, so the chip must stay a connection prompt
+ // and open the explanation instead of claiming a phantom Balanced tier.
+ const showsTier =
+ (connectionLocked && Boolean(active)) ||
+ (!hasConnectionChoice && Boolean(active));
const label = showsTier
? tierName(active, tier)
: (active?.display_name ?? "Choose connection");
@@ -132,7 +143,9 @@ export function ConnectionPicker({ connectionLocked = false }: Props) {
: "border-border bg-background text-foreground hover:bg-muted",
)}
>
- <Icon icon={KeyIcon} size={14} />
+ {/* The key stands for a credential. Against a tier name it would be
+ labelling reasoning depth as an account, so it is dropped. */}
+ {!showsTier && <Icon icon={KeyIcon} size={14} />}
<span className="hidden sm:inline">{triggerLabel}</span>
<Icon
icon={ArrowDown01Icon}
@@ -146,57 +159,61 @@ export function ConnectionPicker({ connectionLocked = false }: Props) {
align="start"
className="w-[26rem] max-w-[calc(100vw-2rem)] border-border bg-popover p-0 text-popover-foreground"
>
- {(!connectionLocked || onlyOfferIsLocked) && (
- <>
- <SectionLabel>Runs on</SectionLabel>
- <div
- role="radiogroup"
- aria-label="Connection this chat runs on"
- onKeyDown={(event) => {
- const to = nextRovingValue(
- connectionOptions,
- active?.offer_id ?? "",
- event.key,
- );
- if (to === null) return;
- event.preventDefault();
- const target = offers.find((o) => o.offer_id === to);
- if (target) chooseConnection(target);
- event.currentTarget
- .querySelector<HTMLElement>(`[data-offer="${to}"]`)
- ?.focus();
- }}
- >
- {offers.map((offer) => (
- <ChoiceRow
- key={offer.offer_id}
- offerId={offer.offer_id}
- tabIndex={rovingTabIndex(
+ {/* One runnable row is nothing to choose between. A locked-only row is
+ different: the explanation and unlock link are the entire reason
+ the chip remains visible. */}
+ {(!connectionLocked || onlyOfferIsLocked) &&
+ (offers.length > 1 || onlyOfferIsLocked) && (
+ <>
+ <SectionLabel>Runs on</SectionLabel>
+ <div
+ role="radiogroup"
+ aria-label="Connection this chat runs on"
+ onKeyDown={(event) => {
+ const to = nextRovingValue(
connectionOptions,
- { value: offer.offer_id },
active?.offer_id ?? "",
- )}
- title={offer.display_name}
- subtitle={offerSubtitle(offer)}
- badge={isLinkedAccount(offer) ? "Connected" : undefined}
- notes={[tierSummary(offer), ...offer.limitations].filter(
- Boolean,
- )}
- isSelected={offer.offer_id === active?.offer_id}
- onSelect={() => chooseConnection(offer)}
- lock={
- offer.lock_reason
- ? {
- reason: offer.lock_reason,
- href: offer.unlock_href ?? null,
- }
- : undefined
- }
- />
- ))}
- </div>
- </>
- )}
+ event.key,
+ );
+ if (to === null) return;
+ event.preventDefault();
+ const target = offers.find((o) => o.offer_id === to);
+ if (target) chooseConnection(target);
+ event.currentTarget
+ .querySelector<HTMLElement>(`[data-offer="${to}"]`)
+ ?.focus();
+ }}
+ >
+ {offers.map((offer) => (
+ <ChoiceRow
+ key={offer.offer_id}
+ offerId={offer.offer_id}
+ tabIndex={rovingTabIndex(
+ connectionOptions,
+ { value: offer.offer_id },
+ active?.offer_id ?? "",
+ )}
+ title={offer.display_name}
+ subtitle={offerSubtitle(offer)}
+ badge={isLinkedAccount(offer) ? "Connected" : undefined}
+ notes={[tierSummary(offer), ...offer.limitations].filter(
+ Boolean,
+ )}
+ isSelected={offer.offer_id === active?.offer_id}
+ onSelect={() => chooseConnection(offer)}
+ lock={
+ offer.lock_reason
+ ? {
+ reason: offer.lock_reason,
+ href: offer.unlock_href ?? null,
+ }
+ : undefined
+ }
+ />
+ ))}
+ </div>
+ </>
+ )}
{showTiers && (
<>
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useConnectionPicker.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useConnectionPicker.ts
index baaa2b5fe9..3e94ac7ef2 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useConnectionPicker.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/useConnectionPicker.ts
@@ -58,6 +58,10 @@ export function useConnectionPicker() {
tier: copilotLlmModel,
setTier: setCopilotLlmModel,
showTiers: tiersAreDistinct(active),
+ // Whether there is a connection to choose *between*. Counts only what can
+ // actually run: a locked offer is listed to explain an absence, so it adds
+ // a row to the popover without adding a decision.
+ hasConnectionChoice: choosable.length > 1,
isLoading: query.isLoading,
// React Query can retain the last successful offers while a background
// refetch fails. Keep that usable snapshot visible; the error state is
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ConnectionPicker.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ConnectionPicker.test.tsx
index 3bbf532e2c..55477fba7c 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ConnectionPicker.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ConnectionPicker.test.tsx
@@ -87,6 +87,17 @@ function mockOffers(offers: AIConnectionOffer[]) {
server.use(getGetV2ListChatConnectionsMockHandler200({ offers }));
}
+/**
+ * The picker's trigger, whichever thing it is currently labelled with.
+ *
+ * The chip names the connection when there is one to choose, and the tier when
+ * there is not, so matching either label alone would tie every test that merely
+ * needs to open the popover to the label rule. Both end in "— change".
+ */
+function openPicker() {
+ return screen.findByRole("button", { name: /— change/ });
+}
+
beforeEach(() => {
useCopilotUIStore.setState({
copilotLlmAuth: null,
@@ -99,9 +110,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), chatgpt()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(await screen.findByText("Your AutoGPT plan")).toBeDefined();
expect(screen.getByText("Your ChatGPT plan")).toBeDefined();
@@ -121,9 +130,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), chatgpt()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(
await screen.findByRole("radio", { name: "Balanced · sonnet-5" }),
@@ -141,9 +148,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), chatgpt()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
const tiers = await screen.findByRole("radiogroup", { name: "Model tier" });
expect(
@@ -193,9 +198,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), chatgpt()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
await userEvent.click(
await screen.findByRole("radio", { name: /ChatGPT/ }),
);
@@ -207,6 +210,36 @@ describe("ConnectionPicker", () => {
);
});
+ it("moves and selects between connections with the arrow keys", async () => {
+ mockOffers([offer(), chatgpt()]);
+ render(<ConnectionPicker />);
+
+ await userEvent.click(await openPicker());
+ const connections = await screen.findByRole("radiogroup", {
+ name: "Connection this chat runs on",
+ });
+ const platform = within(connections).getByRole("radio", {
+ name: /AutoGPT Platform/,
+ });
+ const linked = within(connections).getByRole("radio", {
+ name: /ChatGPT/,
+ });
+
+ platform.focus();
+ await userEvent.keyboard("x");
+ expect(useCopilotUIStore.getState().copilotLlmAuth).toBeNull();
+
+ await userEvent.keyboard("{ArrowDown}");
+
+ await waitFor(() => {
+ expect(useCopilotUIStore.getState().copilotLlmAuth).toEqual({
+ authProvider: "codex",
+ credentialId: "cred-1",
+ });
+ expect(document.activeElement).toBe(linked);
+ });
+ });
+
it("is one tab stop, and the arrow keys move and select within it", async () => {
// A radio group is not a list of buttons that say role="radio". Tab moves
// past the whole group; the arrows move within it and select as they go.
@@ -252,9 +285,7 @@ describe("ConnectionPicker", () => {
]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
await waitFor(() =>
expect(
@@ -269,9 +300,7 @@ describe("ConnectionPicker", () => {
mockOffers([chatgpt({ is_default: true })]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(
await screen.findByRole("radio", { name: "Balanced" }),
@@ -282,9 +311,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), chatgpt()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
await userEvent.click(
await screen.findByRole("radio", { name: /ChatGPT/ }),
);
@@ -301,9 +328,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), chatgpt()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(
await screen.findByText(/builder's chat panel always runs on AutoGPT/),
@@ -382,9 +407,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), locked()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(await screen.findByText("ChatGPT")).toBeDefined();
expect(
@@ -429,9 +452,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), locked()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(
await screen.findByText(/spending no AutoGPT credits/),
@@ -453,9 +474,7 @@ describe("ConnectionPicker", () => {
]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(
await screen.findByText(
@@ -469,9 +488,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), locked()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
await screen.findByText("A Max plan or higher is required to use ChatGPT.");
expect(screen.queryByRole("radio", { name: /ChatGPT/ })).toBeNull();
@@ -484,9 +501,12 @@ describe("ConnectionPicker", () => {
render(<ConnectionPicker />);
- expect(
- await screen.findByRole("button", { name: /Runs on AutoGPT Platform/ }),
- ).toBeDefined();
+ await userEvent.click(await openPicker());
+ // What it lands on is what it marks selected: never the locked one.
+ const landed = await screen.findByRole("radio", {
+ name: /AutoGPT Platform/,
+ });
+ expect(landed.getAttribute("aria-checked")).toBe("true");
// What it lands on is what it shows: a locked offer is never the one the
// chip names. Nothing is written into the store either, because showing a
// default is not the user choosing it.
@@ -499,9 +519,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), chatgpt()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(
await screen.findByText("Balanced: sonnet-5 · Advanced: opus-5"),
@@ -512,9 +530,7 @@ describe("ConnectionPicker", () => {
mockOffers([offer(), chatgpt()]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(await screen.findByText("Connected")).toBeDefined();
// The deployment route is not something anyone connected.
@@ -538,9 +554,7 @@ describe("ConnectionPicker", () => {
]);
render(<ConnectionPicker />);
- await userEvent.click(
- await screen.findByRole("button", { name: /Runs on/ }),
- );
+ await userEvent.click(await openPicker());
expect(
await screen.findByText(/A Max plan or higher is required for Advanced/),
@@ -591,4 +605,53 @@ describe("ConnectionPicker", () => {
const trigger = await screen.findByRole("button", { name: /Runs on/ });
expect(trigger.getAttribute("aria-label")).not.toMatch(/your plan/);
});
+
+ it("shows the tier when there is no connection to choose", async () => {
+ // Naming the only connection teaches a hosted user nothing — they would
+ // read "AutoGPT Platform" on every chat. The tier is the live choice.
+ mockOffers([chatgpt({ is_default: true })]);
+
+ render(<ConnectionPicker />);
+
+ expect(
+ await screen.findByRole("button", { name: /Model tier Balanced/ }),
+ ).toBeDefined();
+ expect(screen.queryByRole("button", { name: /Runs on/ })).toBeNull();
+ });
+
+ it("names the connection once there is more than one", async () => {
+ mockOffers([offer({ is_default: true }), chatgpt()]);
+
+ render(<ConnectionPicker />);
+
+ expect(
+ await screen.findByRole("button", { name: /Runs on AutoGPT Platform/ }),
+ ).toBeDefined();
+ });
+
+ it("treats a locked alternative as no choice at all", async () => {
+ // A row the user cannot pick is an explanation, not an option, so the chip
+ // stays on the tier — but the row still earns its place in the popover.
+ mockOffers([offer({ is_default: true }), locked()]);
+
+ render(<ConnectionPicker />);
+
+ expect(
+ await screen.findByRole("button", { name: /Model tier/ }),
+ ).toBeDefined();
+ await userEvent.click(await openPicker());
+ expect(screen.getByText("ChatGPT")).toBeDefined();
+ });
+
+ it("drops the key icon when the chip names a tier", async () => {
+ // A key stands for a credential; against "Balanced" it would be labelling
+ // reasoning depth as an account.
+ mockOffers([chatgpt({ is_default: true })]);
+
+ render(<ConnectionPicker />);
+ const trigger = await screen.findByRole("button", { name: /Model tier/ });
+
+ // One icon left: the dropdown chevron.
+ expect(trigger.querySelectorAll("svg")).toHaveLength(1);
+ });
});
diff --git a/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/AIConnectionsSection/ManageConnectionDialog.tsx b/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/AIConnectionsSection/ManageConnectionDialog.tsx
index c11d725236..6fa597f7e0 100644
--- a/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/AIConnectionsSection/ManageConnectionDialog.tsx
+++ b/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/AIConnectionsSection/ManageConnectionDialog.tsx
@@ -1,7 +1,6 @@
"use client";
import { useState } from "react";
-
import { useGetV1CodexAccount } from "@/app/api/__generated__/endpoints/integrations/integrations";
import type { AIConnectionOffer } from "@/app/api/__generated__/models/aIConnectionOffer";
import { Button } from "@/components/atoms/Button/Button";
diff --git a/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/useOAuthConnect.ts b/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/useOAuthConnect.ts
index 725b023f9b..abb974b69d 100644
--- a/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/useOAuthConnect.ts
+++ b/autogpt_platform/frontend/src/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/useOAuthConnect.ts
@@ -9,12 +9,12 @@ import {
} from "@/app/api/__generated__/endpoints/integrations/integrations";
import type { CredentialsMetaResponse } from "@/app/api/__generated__/models/credentialsMetaResponse";
import { toast } from "@/components/molecules/Toast/use-toast";
+import { invalidateConnectionQueries } from "@/lib/react-query/invalidateConnections";
import {
OAUTH_ERROR_POPUP_BLOCKED,
openOAuthPopup,
preOpenOAuthPopup,
} from "@/lib/oauth-popup";
-import { invalidateConnectionQueries } from "@/lib/react-query/invalidateConnections";
import { getOAuthErrorMessage } from "./helpers";
diff --git a/autogpt_platform/frontend/src/lib/react-query/__tests__/invalidateConnections.test.ts b/autogpt_platform/frontend/src/lib/react-query/__tests__/invalidateConnections.test.ts
index bb98355aa4..908153e16e 100644
--- a/autogpt_platform/frontend/src/lib/react-query/__tests__/invalidateConnections.test.ts
+++ b/autogpt_platform/frontend/src/lib/react-query/__tests__/invalidateConnections.test.ts
@@ -14,7 +14,7 @@ function hash(key: readonly unknown[]) {
}
describe("invalidateConnectionQueries", () => {
- it("refreshes credentials, chat connections and transports", async () => {
+ it("refreshes the credentials, chat connections and transports lists", async () => {
const client = new QueryClient();
const invalidateQueries = vi.fn().mockResolvedValue(undefined);
client.invalidateQueries = invalidateQueries;
diff --git a/autogpt_platform/frontend/src/lib/react-query/invalidateConnections.ts b/autogpt_platform/frontend/src/lib/react-query/invalidateConnections.ts
index 721b8c99bf..b906451b1b 100644
--- a/autogpt_platform/frontend/src/lib/react-query/invalidateConnections.ts
+++ b/autogpt_platform/frontend/src/lib/react-query/invalidateConnections.ts
@@ -6,7 +6,21 @@ import {
} from "@/app/api/__generated__/endpoints/chat/chat";
import { getGetV1ListCredentialsQueryKey } from "@/app/api/__generated__/endpoints/integrations/integrations";
-/** Refresh every client-side view derived from a credential change. */
+/**
+ * Refresh everything a connect or disconnect changes.
+ *
+ * A credential is not just a row in the integrations list. The server also
+ * derives the chat connection offers and the transport list from it, and those
+ * are what the AI subscriptions section and the copilot's connection picker
+ * render. Invalidating the credentials list alone leaves both showing the
+ * state from before the sign-in for as long as they stay fresh -- a minute,
+ * under the client's default `staleTime` -- so returning from a ChatGPT device
+ * login reads as if the sign-in did nothing until the page is reloaded.
+ *
+ * Every path that creates or removes a credential should call this rather than
+ * pick a subset: which lists a given provider feeds is a server-side fact, and
+ * a caller that guesses gets it wrong the moment the server adds one.
+ */
export async function invalidateConnectionQueries(
queryClient: QueryClient,
): Promise<void> {
diff --git a/docs/platform/codex-subscription-preview.md b/docs/platform/codex-subscription-preview.md
index 1d07d7e957..e7fcea7701 100644
--- a/docs/platform/codex-subscription-preview.md
+++ b/docs/platform/codex-subscription-preview.md
@@ -123,11 +123,12 @@ disabled, and its sandbox is read-only. Any side effect comes only through a
tool that Claude Code was already permitted to invoke through AutoGPT's MCP and
security hooks.
-The implementation pins `openai-codex` and its bundled runtime together at
-`0.144.4`. Do not float either dependency independently. The Claude Agent SDK
-and bundled CLI are also pinned by the backend lock. Because `dynamicTools` and
-the compatibility surface are version-sensitive, updates require the focused
-protocol, Messages conformance, and bundled-CLI tests to pass before rollout.
+ChatGPT is reached over HTTPS: sign-in, refresh, the model catalog and
+inference all run against `auth.openai.com` and `chatgpt.com`, so no Codex
+binary ships in the image and there is no version to pin. The Claude Agent SDK
+and its CLI are still pinned by the backend lock. Because the compatibility
+surface is version-sensitive, updates require the focused protocol and Messages
+conformance tests to pass before rollout.
## Verify an existing local login
@@ -138,10 +139,11 @@ To test an existing local Codex login before starting the stack, run this from
poetry run python -m scripts.codex_preview_smoke
```
-The smoke test copies, never moves, `~/.codex/auth.json` into an isolated home,
-checks account, models, and rate limits, performs one subscription-backed turn,
-and fails if the runtime can read a host canary or mutates the source login. It
-does consume a small amount of the connected account's Codex usage.
+The smoke test reads `~/.codex/auth.json` without writing to it, checks the
+account and model catalog, and performs one subscription-backed turn with a tool
+call over HTTPS -- failing if the model never calls the tool or returns no text.
+It reports the quota the turn's response headers carried. It does consume a
+small amount of the connected account's usage.
## Deploy a private cloud preview
@@ -187,10 +189,12 @@ device-code completion.
truth.
- Raw ChatGPT tokens do not enter Redis, RabbitMQ, frontend responses,
container-wide environment variables, or AutoPilot session records.
-- Each native invocation or shared AutoPilot runtime actor materializes auth
- into an isolated temporary home. An AutoPilot actor keeps that home only
- while one or more overlapping chats are attached, checkpoints Codex-managed
- refresh throughout its lifetime and at shutdown, then cleans the home.
+- Tokens are held in memory for the life of a turn and never written to disk:
+ there is no on-disk auth home, because there is no process to hand one to.
+ A client is built per turn and never cached, so one user's subscription can
+ never serve another's request. Refresh is owned by the credentials manager
+ and serialized behind its Redis mutex, since OpenAI rotates the refresh token
+ on every exchange.
- Code Generation turns expose no dynamic tools or host workspace. AutoPilot
exposes only the tools registered by its existing Claude SDK/MCP harness.
- Runtime reuse is scoped to the exact user and credential. Credentials are