diff --git a/.claude/harness-candidates.md b/.claude/harness-candidates.md index f18b7bd7..bd638778 100644 --- a/.claude/harness-candidates.md +++ b/.claude/harness-candidates.md @@ -1028,3 +1028,16 @@ re-derive from scratch. - [ ] `raise X(...) from exc` where `exc` came from parsing an env/URL value leaks the value through `__cause__` in a traceback (urlsplit put a URL password in its port error). Needs data-flow from env reads; not cheap — caught in the network: llm_only Phase 3 review. - [ ] `await asyncio.to_thread(subprocess.run, ...)` that CREATES a resource inside a try whose `finally` removes it: a cancel leaves the worker thread running, so teardown races the create and leaks it (fixed in `isolation/egress.py` by `_run_to_completion`). Detecting "creates a resource" is not mechanical — caught in the network: llm_only Phase 4 review. - [ ] The single-`asyncio.shield` join in `fs_permissions.set_permissions` does not survive a SECOND cancel of the caller; `isolation/egress._run_to_completion` loops on the shield. Promote that helper to a shared module and use it in both places — caught in the network: llm_only Phase 4 review. + +## From 2026-11-13 Codex app-server provider review + +- [ ] A CI job that runs `tests/test_codex_provider_binary.py` against the latest + openai-codex release, in a venv built with `uv pip install --no-config`. CI runs only the + lock-pinned cli-bin 0.156.1, so the 0.167.x `-32600` rejection reached pip installs of the + `[codex]` extra and no check saw it. Deferred because a floating dependency makes the job + flaky by design; it fits a nightly job, not the PR gate — caught in the code review. +- [ ] A doc-surface check that checked-in Markdown (`.claude/notes/`, `.claude/shared/`, + `docs/`, `plugins/coder-eval/`) never references the git-ignored `c/` plan tree + (`c/YYYY-MM-DD-*.md`) or a plan decision id (`decision D`). A notes section cited + "PI decision D1", which a reader of the repo cannot open. Deferred because there was one + hit and review caught it; promote it on a second hit — caught in the code review. diff --git a/.claude/notes/agents.md b/.claude/notes/agents.md index e196345d..18cde6b8 100644 --- a/.claude/notes/agents.md +++ b/.claude/notes/agents.md @@ -753,6 +753,34 @@ profiles and loses the prepend again. Nested zsh keeps it, because `ZDOTDIR` sta exported. No-op on Windows, where Codex shells through PowerShell (`-NoProfile`) or `cmd /c`, neither of which re-sources a profile chain. +## Codex custom provider on the app-server + +With `CODEX_BASE_URL` set, openai-codex 0.167.0-0.167.2 rejects every turn with JSON-RPC +`-32600 "This chat uses custom, but your current provider is openai"` when only the thread +names the custom provider. The check is in the bundled `openai-codex-cli-bin` binary; the +Python SDK surface (`CodexConfig.config_overrides`) is identical from 0.155.1 to 0.169.0. +An offline probe against a local stub showed thread-only routing works on 0.156.1, 0.168.0 +and 0.169.0 and fails on 0.167.2; app-server plus thread works on all of them. + +`_custom_provider()` builds the provider once. The thread options embed it, and `start()` +passes it to the app-server as two `--config` overrides (`model_provider` and the +`model_providers.custom` inline table). Both seams select the provider: the thread seam is +the path that every other binary version accepts, and both read the one helper, so they +cannot disagree. The TOML value is written with `tomli_w`, a runtime dependency, with quoted +keys, so `api-version` needs no bare-key rules and `"`, `\`, control characters and U+007F +are escaped. `json.dumps` is not valid TOML for U+007F or surrogate escapes. The +`_TomlValue` alias admits only string leaves, because `_toml_inline` renders only those. + +The base URL is on the app-server command line. It is not a secret and also travels in the +thread-start payload; the key stays in the env (`env_key`). A credential embedded in the URL +or a URL that does not parse fails `start()` with a non-retryable `AgentConfigError` +before the app-server spawns, because the URL would go on its command line (visible in +`ps` and `/proc//cmdline`). The message names the variable, never the value. A warning +was rejected: it leaves the credential on argv. CI runs only the lock-pinned binary, +so a binary bump needs a manual live check: a fresh venv per version built with +`uv pip install --no-config` (without it the repo's `[tool.uv]` override pins cli-bin +0.156.1 for every version), then `tests/test_codex_provider_binary.py` and one real turn. + ## Reaping the CLI harnesses `opencode run` leaves a local server child alive after the CLI exits, and it INHERITS the diff --git a/CLAUDE.md b/CLAUDE.md index 6a57fbfd..5dd8c035 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -282,7 +282,7 @@ Tasks are YAML. Full reference: [Task Definition Guide](docs/TASK_DEFINITION_GUI ## Dependencies **Runtime**: pydantic, pydantic-settings, pyyaml, typer, rich, python-dotenv, anthropic, -claude-agent-sdk, anyio, radon, tqdm, jmespath, jsonschema +claude-agent-sdk, anyio, radon, tqdm, jmespath, jsonschema, tomli-w **Runtime (optional, `[uipath]` extra)**: uipath — for local sandbox parity with tasks that invoke `uv run uipath eval ...`. Base installs run end-to-end without it; diff --git a/docs/agents/CODEX.md b/docs/agents/CODEX.md index f434c3ce..367f39a4 100644 --- a/docs/agents/CODEX.md +++ b/docs/agents/CODEX.md @@ -62,6 +62,8 @@ CODEX_API_KEY= ``` This registers a custom Codex model provider (`base_url` + `env_key=CODEX_API_KEY` + `query_params={api-version}` + `wire_api=responses`). The Codex CLI only supports the Responses wire API (it rejects `wire_api=chat` as "no longer supported"), so the protocol is fixed. If your Azure deployment requires the key in an `api-key` header rather than `Authorization: Bearer`, that needs an additional provider `http_headers`/`env_http_headers` entry — open an issue if you hit that. +coder_eval selects this provider on the Codex app-server (`--config`) and on each thread. openai-codex 0.167.x requires the app-server half: without it, every turn fails with JSON-RPC `-32600 "This chat uses custom, but your current provider is openai"`. The URL is on the app-server command line, so coder_eval rejects a `CODEX_BASE_URL` that embeds a credential (`https://user:pass@…`) with a configuration error. Put the key only in `CODEX_API_KEY`. + ## Usage ### Command Line diff --git a/src/coder_eval/agents/codex_agent.py b/src/coder_eval/agents/codex_agent.py index 8171ca98..b57998dd 100644 --- a/src/coder_eval/agents/codex_agent.py +++ b/src/coder_eval/agents/codex_agent.py @@ -16,7 +16,9 @@ from pathlib import Path from types import SimpleNamespace from typing import Any, ClassVar, Literal, NamedTuple -from urllib.parse import urlparse +from urllib.parse import urlparse, urlsplit + +import tomli_w from coder_eval.agent import Agent, AgentState from coder_eval.agents._logging import PrefixedAdapter, log_raw_sdk_event @@ -24,6 +26,7 @@ from coder_eval.agents.watchdog import ThreadedWatchdog from coder_eval.config import settings from coder_eval.errors import ( + AgentConfigError, AgentCrashError, TurnTimeoutError, truncate_crash_message, @@ -81,8 +84,8 @@ # Rationale: .claude/notes/agents.md § Codex runs full-access on every permission mode _CODEX_APPROVAL_MODE = "deny_all" -# Provider id registered in thread config when CODEX_BASE_URL routes to a -# custom endpoint. +# Provider id selected on the app-server and the thread when CODEX_BASE_URL +# routes to a custom endpoint. _CUSTOM_PROVIDER_ID = "custom" # apply_patch statuses meaning the patch did not apply. Used ONLY to classify the @@ -387,6 +390,32 @@ def _message_uncached_input(m: AssistantMessage) -> int: # supported"), so this is a fixed constant, not an operator knob. _CODEX_WIRE_API = "responses" +type _TomlValue = str | dict[str, _TomlValue] + + +def _toml_inline(value: _TomlValue) -> str: + """Render a string or a (nested) string table as one TOML inline value, for ``codex --config``.""" + if isinstance(value, str): + return tomli_w.dumps({"v": value}).removeprefix("v = ").rstrip("\n") + return "{" + ", ".join(f"{_toml_inline(k)} = {_toml_inline(v)}" for k, v in value.items()) + "}" + + +def _require_credential_free_base_url(url: str) -> None: + """Raise ``AgentConfigError`` when CODEX_BASE_URL does not parse or embeds a credential. + + The message names the variable, never its value: the URL goes on the app-server command line. + """ + try: + parts = urlsplit(url) + except ValueError: + raise AgentConfigError("CODEX_BASE_URL is not a valid URL.") from None + if parts.username is not None or parts.password is not None: + raise AgentConfigError( + "CODEX_BASE_URL embeds a credential, which would be visible on the codex app-server " + + "command line; put the key only in CODEX_API_KEY." + ) + + # Login-shell profiles generated into the per-task HOME. ``.bash_profile`` is # what ``bash -l`` reads and ``.profile`` covers sh/dash; the zsh trio covers # macOS, where ``.zshenv`` runs in EVERY zsh, ``.zprofile`` in login shells @@ -1064,13 +1093,12 @@ async def start( self._env_path_prepend = list(env_path_prepend or []) self._setup_login_shell_home() self._state = AgentState.WORKING + config_overrides = self._app_server_config_overrides() try: from openai_codex import Codex, CodexConfig - # Build CodexConfig with environment variables for custom API configuration - env_override = self._build_codex_env() - config = CodexConfig(env=env_override) if env_override else None + config = CodexConfig(env=self._build_codex_env(), config_overrides=config_overrides) # Close any prior client FIRST: start() runs through # execute_with_retry, so a retried start would otherwise orphan the @@ -1488,6 +1516,29 @@ def _resolve_api_version() -> str | None: """ return os.getenv("CODEX_API_VERSION") or None + @staticmethod + def _custom_provider() -> dict[str, _TomlValue] | None: + """The CODEX_BASE_URL model provider definition, or None on the default endpoint. + + The binary has no base-URL env var, so the provider is defined in config and + selected by id; ``env_key`` names the key's variable, so the key itself never + appears here. On Azure, CODEX_API_VERSION adds the required ``api-version`` query + param and CODEX_MODEL is the deployment name. + """ + base_url = CodexAgent._resolve_base_url() + if not base_url: + return None + provider: dict[str, _TomlValue] = { + "name": "Custom", + "base_url": base_url, + "env_key": "CODEX_API_KEY", + "wire_api": _CODEX_WIRE_API, + } + api_version = CodexAgent._resolve_api_version() + if api_version: + provider["query_params"] = {"api-version": api_version} + return provider + def _effective_model(self) -> str | None: """Resolve the model: task/CLI ``agent.model`` wins, else CODEX_MODEL. @@ -1499,8 +1550,7 @@ def _build_codex_env(self) -> dict[str, str] | None: """Build the environment passed to the Codex app-server. Carries ``CODEX_API_KEY`` and, when the sandbox resolved mock dirs, a PATH - with those prepended. The base URL is NOT an env var the binary honors — - it goes through the model provider config instead. + with those prepended. The SDK merges this PARTIAL dict over ``os.environ`` (normalizing the PATH key case-insensitively), so a full PATH value here safely replaces the @@ -1529,6 +1579,25 @@ def _build_codex_env(self) -> dict[str, str] | None: env["CODEX_HOME"] = str(codex_home) return env if env else None + def _app_server_config_overrides(self) -> tuple[str, ...]: + """``--config`` overrides that select the custom provider on the app-server itself. + + The definition is the one the thread options carry; empty on the default endpoint. + + Raises: + AgentConfigError: CODEX_BASE_URL does not parse or embeds a credential. + + Rationale: .claude/notes/agents.md § Codex custom provider on the app-server + """ + provider = self._custom_provider() + if provider is None: + return () + _require_credential_free_base_url(str(provider["base_url"])) + return ( + f"model_provider={_toml_inline(_CUSTOM_PROVIDER_ID)}", + f"model_providers.{_CUSTOM_PROVIDER_ID}={_toml_inline(provider)}", + ) + @staticmethod def _login_shell_profiles_supported() -> bool: """Whether to generate login-shell profile shims (POSIX shells only).""" @@ -1705,35 +1774,18 @@ def _build_thread_options(self) -> dict[str, Any]: + "do not rely on it as a security boundary." ) - # The codex binary has no base-URL env var: a model provider must be - # defined in config and selected, with env_key naming the key's variable. - # For Azure, CODEX_API_VERSION adds the required query param and - # CODEX_MODEL is the deployment name. - base_url = self._resolve_base_url() - if base_url: + provider = self._custom_provider() + if provider is not None: options["model_provider"] = _CUSTOM_PROVIDER_ID if not effective_model: self._log.warning( "CODEX_BASE_URL is set but no model resolved (agent.model / CODEX_MODEL) " + "— the provider may reject the request." ) - provider: dict[str, Any] = { - "name": "Custom", - "base_url": base_url, - "env_key": "CODEX_API_KEY", - # Fixed, not configurable: the pinned binary rejects - # `wire_api = "chat"` as "no longer supported". - "wire_api": _CODEX_WIRE_API, - } - api_version = self._resolve_api_version() - if api_version: - # Azure requires ?api-version=... on every request. - provider["query_params"] = {"api-version": api_version} tool_config["model_providers"] = {_CUSTOM_PROVIDER_ID: provider} - self._log.debug( - f"Codex routed via custom provider (host={urlparse(base_url).hostname or '(unknown)'}, " - + f"api_version={'set' if api_version else 'unset'})" - ) + host = urlparse(str(provider["base_url"])).hostname or "(unknown)" + api_version_state = "set" if "query_params" in provider else "unset" + self._log.debug(f"Codex routed via custom provider (host={host}, api_version={api_version_state})") if self.config.context_window is not None: # Codex then auto-compacts at 90% of this window. diff --git a/tests/test_codex_agent.py b/tests/test_codex_agent.py index f2af4b0d..f266901c 100644 --- a/tests/test_codex_agent.py +++ b/tests/test_codex_agent.py @@ -5,7 +5,9 @@ it isn't installed, so `make test` / CI stay green without the extra. """ +import tomllib from datetime import datetime +from unittest.mock import AsyncMock import pytest @@ -19,9 +21,36 @@ _CODEX_APPROVAL_MODE, CodexAgent, ) +from coder_eval.errors import AgentConfigError from coder_eval.models import AgentConfig, AgentKind, AssistantMessage, ReconciliationMessage, parse_agent_config +class _CodexSpy: + """Stands in for ``openai_codex.Codex``: records the config it was built with.""" + + def __init__(self) -> None: + self.config: object = "never constructed" + + def __call__(self, config: object = None) -> "_CodexSpy": + self.config = config + return self + + def close(self) -> None: + pass + + +@pytest.fixture +def codex_spy(monkeypatch) -> _CodexSpy: + """Clear the Codex routing env and replace ``openai_codex.Codex`` with a recording spy.""" + import openai_codex + + for var in ("CODEX_BASE_URL", "CODEX_API_VERSION", "CODEX_API_KEY"): + monkeypatch.delenv(var, raising=False) + spy = _CodexSpy() + monkeypatch.setattr(openai_codex, "Codex", spy) + return spy + + class TestCodexAgentInitialization: """Test CodexAgent initialization and basic properties.""" @@ -252,6 +281,66 @@ async def test_start_propagates_env_path_prepend(self, monkeypatch, tmp_path): assert agent._env_path_prepend == ["/sandbox/mocks", "/sandbox/bins"] + @pytest.mark.asyncio + async def test_start_passes_provider_overrides_to_codex_config(self, monkeypatch, tmp_path, codex_spy): + """A base URL alone (no key, no prepend) still builds a CodexConfig carrying the overrides.""" + import openai_codex + + monkeypatch.setenv("CODEX_BASE_URL", "https://gw.local/openai/v1") + + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="dep")) + await agent.start(str(tmp_path)) + + config = codex_spy.config + assert isinstance(config, openai_codex.CodexConfig) + assert config.config_overrides[0] == 'model_provider="custom"' + assert config.config_overrides == agent._app_server_config_overrides() + assert config.env is None + + @pytest.mark.asyncio + async def test_start_azure_shape_carries_key_env_and_overrides(self, monkeypatch, tmp_path, codex_spy): + """Key and base URL together: the CodexConfig carries both the key env and the provider overrides.""" + import openai_codex + + monkeypatch.setenv("CODEX_BASE_URL", "https://my-res.openai.azure.com/openai/v1") + monkeypatch.setenv("CODEX_API_VERSION", "2025-04-01-preview") + monkeypatch.setenv("CODEX_API_KEY", "test-key-not-in-argv") + monkeypatch.setattr(CodexAgent, "_run_async", AsyncMock()) + + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="dep")) + await agent.start(str(tmp_path)) + + config = codex_spy.config + assert isinstance(config, openai_codex.CodexConfig) + assert config.env is not None + assert config.env["CODEX_API_KEY"] == "test-key-not-in-argv" + assert config.config_overrides == agent._app_server_config_overrides() + assert "api-version" in config.config_overrides[1] + + @pytest.mark.asyncio + async def test_start_default_endpoint_passes_empty_config(self, tmp_path, codex_spy): + """No base URL, no key, no prepend: the client config carries no env and no overrides.""" + import openai_codex + + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX)) + await agent.start(str(tmp_path)) + + config = codex_spy.config + assert isinstance(config, openai_codex.CodexConfig) + assert config.env is None + assert config.config_overrides == () + + @pytest.mark.asyncio + async def test_start_rejects_credential_in_base_url_before_spawning(self, monkeypatch, tmp_path, codex_spy): + """A credential in CODEX_BASE_URL fails start() with a non-retryable config error; no app-server starts.""" + monkeypatch.setenv("CODEX_BASE_URL", "https://user:s3cret-pw@gw.local/v1") + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="dep")) + + with pytest.raises(AgentConfigError): + await agent.start(str(tmp_path)) + + assert codex_spy.config == "never constructed" + class TestCustomProviderRouting: """Test that CODEX_BASE_URL injects a custom model provider.""" @@ -262,6 +351,7 @@ def test_no_base_url_means_no_provider(self, monkeypatch): opts = agent._build_thread_options() assert "model_provider" not in opts assert "model_providers" not in opts.get("config", {}) + assert agent._app_server_config_overrides() == () def test_base_url_injects_custom_provider(self, monkeypatch): monkeypatch.setenv("CODEX_BASE_URL", "https://gw.local/openai/v1") @@ -329,6 +419,69 @@ def test_empty_api_version_falls_back(self, monkeypatch): assert "query_params" not in provider assert provider["wire_api"] == "responses" + @pytest.mark.parametrize("api_version", [None, "", "2025-04-01-preview"]) + def test_app_server_overrides_match_thread_provider(self, monkeypatch, api_version): + """The app-server overrides select the same provider table the thread options carry.""" + monkeypatch.setenv("CODEX_BASE_URL", "https://my-res.openai.azure.com/openai") + monkeypatch.setenv("CODEX_API_KEY", "test-key-not-in-argv") + if api_version is None: + monkeypatch.delenv("CODEX_API_VERSION", raising=False) + else: + monkeypatch.setenv("CODEX_API_VERSION", api_version) + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="dep")) + + overrides = agent._app_server_config_overrides() + + assert len(overrides) == 2 + assert overrides[0] == 'model_provider="custom"' + assert overrides[1].startswith("model_providers.custom=") + table = tomllib.loads(overrides[1])["model_providers"]["custom"] + assert table == agent._build_thread_options()["config"]["model_providers"]["custom"] + assert not any("test-key-not-in-argv" in o for o in overrides) + if api_version: + assert table["query_params"] == {"api-version": "2025-04-01-preview"} + assert overrides[1] == ( + 'model_providers.custom={"name" = "Custom", ' + + '"base_url" = "https://my-res.openai.azure.com/openai", ' + + '"env_key" = "CODEX_API_KEY", "wire_api" = "responses", ' + + '"query_params" = {"api-version" = "2025-04-01-preview"}}' + ) + else: + assert "query_params" not in table + + def test_app_server_overrides_escape_toml_specials(self, monkeypatch): + """Quotes, backslashes, DEL and newlines in the base URL round-trip on one line.""" + base_url = 'https://gw.local/v1?x="q"\\b\x7f\n' + monkeypatch.setenv("CODEX_BASE_URL", base_url) + monkeypatch.delenv("CODEX_API_VERSION", raising=False) + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="dep")) + + overrides = agent._app_server_config_overrides() + + assert tomllib.loads(overrides[1])["model_providers"]["custom"]["base_url"] == base_url + assert "\n" not in overrides[1] + + @pytest.mark.parametrize( + ("base_url", "reason"), + [ + ("https://user:s3cret-pw@gw.local/v1", "embeds a credential"), + ("https://token-only@gw.local/v1", "embeds a credential"), + ("http://u:s3cret-pw@[::1/v1", "not a valid URL"), + ], + ) + def test_unsafe_base_url_raises_without_echoing_it(self, monkeypatch, base_url, reason): + """Userinfo or an unparseable CODEX_BASE_URL raises a config error naming neither the URL nor the credential.""" + monkeypatch.setenv("CODEX_BASE_URL", base_url) + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="dep")) + + with pytest.raises(AgentConfigError, match=reason) as exc_info: + agent._app_server_config_overrides() + + message = str(exc_info.value) + assert "s3cret-pw" not in message + assert "gw.local" not in message + assert exc_info.value.__cause__ is None + class TestCodexEnvironmentInfo: """get_environment_info surfaces resolved custom-endpoint routing for run artifacts.""" diff --git a/tests/test_codex_provider_binary.py b/tests/test_codex_provider_binary.py new file mode 100644 index 00000000..dda33bf9 --- /dev/null +++ b/tests/test_codex_provider_binary.py @@ -0,0 +1,138 @@ +"""The real bundled codex binary accepts the custom provider and routes a turn through it. + +Starts the app-server against a local HTTP stub, with no network and a throw-away CODEX_HOME. +The ``app-server-only`` case strips the provider from the thread options, so only the +app-server seam can reach the stub; ``app-server-and-thread`` sends the production shape. +Skips when the codex extra or its binary is absent. +""" + +import asyncio +import threading +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from typing import Any, NamedTuple + +import pytest + +from coder_eval.agents.codex_agent import CodexAgent +from coder_eval.models import AgentKind, parse_agent_config + + +codex_client = pytest.importorskip("openai_codex.client") +codex_cli_bin = pytest.importorskip("codex_cli_bin") +try: + codex_cli_bin.bundled_codex_path() +except FileNotFoundError: + pytest.skip("openai-codex-cli-bin is installed without its codex binary", allow_module_level=True) + +pytestmark = pytest.mark.integration + +_STUB_ERROR = b'{"error":{"message":"stub","type":"invalid_request_error"}}' +_SCENARIO_TIMEOUT_S = 60 + + +class _Request(NamedTuple): + path: str + authorization: str | None + + +def _start_stub(requests: list[_Request]) -> ThreadingHTTPServer: + class Handler(BaseHTTPRequestHandler): + def _reply(self) -> None: + requests.append(_Request(self.path, self.headers.get("Authorization"))) + self.rfile.read(int(self.headers.get("Content-Length") or 0)) + self.send_response(400) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(_STUB_ERROR))) + self.end_headers() + self.wfile.write(_STUB_ERROR) + + def do_GET(self) -> None: + self._reply() + + def do_POST(self) -> None: + self._reply() + + def log_message(self, format: str, *args: object) -> None: + pass + + server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + threading.Thread(target=server.serve_forever, daemon=True).start() + return server + + +def _track_clients(monkeypatch) -> list[Any]: + """Record every ``CodexClient`` that spawns an app-server, so a hung one can be reaped.""" + clients: list[Any] = [] + original_start = codex_client.CodexClient.start + + def start(self: Any) -> None: + clients.append(self) + original_start(self) + + monkeypatch.setattr(codex_client.CodexClient, "start", start) + return clients + + +@pytest.mark.parametrize("thread_provider", [False, True], ids=["app-server-only", "app-server-and-thread"]) +def test_provider_routes_turn_to_custom_endpoint(monkeypatch, tmp_path, thread_provider): + requests: list[_Request] = [] + errors: list[BaseException] = [] + server = _start_stub(requests) + try: + codex_home = tmp_path / "codex-home" + codex_home.mkdir() + monkeypatch.setenv("CODEX_HOME", str(codex_home)) + monkeypatch.setenv("CODEX_API_KEY", "dummy") + monkeypatch.setenv("CODEX_BASE_URL", f"http://127.0.0.1:{server.server_address[1]}/v1") + monkeypatch.setenv("CODEX_API_VERSION", "2025-04-01-preview") + monkeypatch.setenv("HTTPS_PROXY", "http://127.0.0.1:9") + monkeypatch.setenv("HTTP_PROXY", "http://127.0.0.1:9") + monkeypatch.setenv("NO_PROXY", "127.0.0.1,localhost") + monkeypatch.delenv("OPENAI_API_KEY", raising=False) + clients = _track_clients(monkeypatch) + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="stub-model")) + if not thread_provider: + build_thread_options = agent._build_thread_options + + def thread_options_without_provider() -> dict: + options = build_thread_options() + options.pop("model_provider") + options["config"].pop("model_providers") + return options + + monkeypatch.setattr(agent, "_build_thread_options", thread_options_without_provider) + + async def scenario() -> None: + await agent.start(str(tmp_path)) + try: + await agent.communicate("say hi", timeout=20) + except Exception as exc: + errors.append(exc) + finally: + await agent.stop() + + def run() -> None: + try: + asyncio.run(scenario()) + except Exception as exc: + errors.append(exc) + + worker = threading.Thread(target=run, daemon=True) + worker.start() + worker.join(timeout=_SCENARIO_TIMEOUT_S) + if worker.is_alive(): + agent.kill_sync() + for client in clients: + client.close() + worker.join(timeout=10) + pytest.fail( + f"codex app-server scenario did not finish within {_SCENARIO_TIMEOUT_S} s; " + + f"stub saw {requests}; errors: {errors!r}" + ) + finally: + server.shutdown() + server.server_close() + + routed = [r for r in requests if r.path.startswith("/v1/responses") and "api-version=2025-04-01-preview" in r.path] + assert routed, f"stub saw {requests}; errors: {errors!r}" + assert all(r.authorization == "Bearer dummy" for r in routed), f"stub saw {requests}" diff --git a/uv.lock b/uv.lock index e74eac31..ffb3a073 100644 --- a/uv.lock +++ b/uv.lock @@ -1896,19 +1896,19 @@ wheels = [ [[package]] name = "opentelemetry-api" -version = "1.45.1" +version = "1.45.2" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "typing-extensions" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/2e/02/6e0ae9cc61bd3169d401077b507b3ebc344745171e1051ab430be012dcd9/opentelemetry_api-1.45.1.tar.gz", hash = "sha256:aa38ed19bcc084ba42782a73255b3582283eced7ad6dddbd6695189e69adfb75", size = 72804, upload-time = "2026-10-06T17:32:58.133Z" } +sdist = { url = "https://files.pythonhosted.org/packages/19/14/cf3a6b6448849656e1e5f4e2101f3988dc6789724050096aba1bc5ccf592/opentelemetry_api-1.45.2.tar.gz", hash = "sha256:d7b0008e21e5fd61cbe7a7f34ca3b6905a78ab677d9bc395e299359c75cda6b4", size = 73277, upload-time = "2026-10-29T17:29:37.888Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/1e/41/f7dcf80b81ee8e71c1a2b59f14208bc723edbd89ed027a73b175abf6348e/opentelemetry_api-1.45.1-py3-none-any.whl", hash = "sha256:b31553efa588ae44bc306f863c785c5333a9ecc091248c6ee68b4b6c87fdedfb", size = 60256, upload-time = "2026-10-06T17:32:33.506Z" }, + { url = "https://files.pythonhosted.org/packages/a9/2d/610db864d7513158cebb8f36cc88e67e73bd6c7aa1905ef18d89e1281c81/opentelemetry_api-1.45.2-py3-none-any.whl", hash = "sha256:2cbd737377e710ee740d261c94e93fa0f08e6d037c18f17513cc0a2d94479a34", size = 60335, upload-time = "2026-10-29T17:29:06.955Z" }, ] [[package]] name = "opentelemetry-instrumentation" -version = "0.66b1" +version = "0.66b2" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "opentelemetry-api" }, @@ -1916,36 +1916,36 @@ dependencies = [ { name = "packaging" }, { name = "wrapt" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/a5/03/89e47ff8d52a4f83b343e6eb9ef1698ff45357216e5b6b2b21e0da5c5c7d/opentelemetry_instrumentation-0.66b1.tar.gz", hash = "sha256:e79a510f7d87c72d95e964ddb42193a0d9a75668c027d980eab032ea1322a5ce", size = 43308, upload-time = "2026-10-06T17:36:10.703Z" } +sdist = { url = "https://files.pythonhosted.org/packages/76/b9/46a136199fa6928eecefec8418867a460d94f01c608a85d22bd2ed4e8d48/opentelemetry_instrumentation-0.66b2.tar.gz", hash = "sha256:02b094150e53b76f5906d75e628dbb3833bbc6e62fa109b5c7ba0c081865e923", size = 43305, upload-time = "2026-10-29T17:38:11.618Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/da/b2/d1413681ff43e13ac9860df27e1226d3199ab0b97b352ceea41abcc660a5/opentelemetry_instrumentation-0.66b1-py3-none-any.whl", hash = "sha256:4c4aa14dc9a24a02325a9d4c42c4d0208dbb1374c2b1b8fe6c9392d59f3e1008", size = 36904, upload-time = "2026-10-06T17:35:11.663Z" }, + { url = "https://files.pythonhosted.org/packages/3b/41/014d40e2b08a5d9d9058ed4967ad992657c203ce0c292ded97364ee7b82a/opentelemetry_instrumentation-0.66b2-py3-none-any.whl", hash = "sha256:fa9ccfdab147a14af8107d19ef0a7b5015675273c5adced7a1fc38f28b42ec4c", size = 36902, upload-time = "2026-10-29T17:37:06.994Z" }, ] [[package]] name = "opentelemetry-sdk" -version = "1.45.1" +version = "1.45.2" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "opentelemetry-api" }, { name = "opentelemetry-semantic-conventions" }, { name = "typing-extensions" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/a1/79/7392e21a1c8f0c61d90b223e31c7e48cb9d452e91a6b820ad24cca5f23c4/opentelemetry_sdk-1.45.1.tar.gz", hash = "sha256:63d24a6ca645019a631e6a51999c73e93adcac1196ca640b8ae78a7cc4762bf3", size = 218324, upload-time = "2026-10-06T17:33:13.26Z" } +sdist = { url = "https://files.pythonhosted.org/packages/7b/1a/74a21d4d6969fb8b432ac397e66112a8167a6deb267ea4559dbaf2fe91ef/opentelemetry_sdk-1.45.2.tar.gz", hash = "sha256:7eec4de13dd21ff3a1670dcb0e02079e692736f94d733dfdce5af461d884248d", size = 221705, upload-time = "2026-10-29T17:29:54.685Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/95/3c/87c42b4bd6dd297536f04cd9383d212ac557ecd49f2cbdcd46da1c9ef5c8/opentelemetry_sdk-1.45.1-py3-none-any.whl", hash = "sha256:c604c11dc429810812348989115fa44bd558772a3d7442afc43d024f2c250ca4", size = 140063, upload-time = "2026-10-06T17:32:55.04Z" }, + { url = "https://files.pythonhosted.org/packages/9f/a5/9a3b793ef48245f2336a16beb2324c2397dbb3f987cd68942f77cca06156/opentelemetry_sdk-1.45.2-py3-none-any.whl", hash = "sha256:ff974ea70ee4f1d51e2ec152db37c2b626c7bf82bb3f7d86114fd8704e7199c6", size = 141756, upload-time = "2026-10-29T17:29:33.722Z" }, ] [[package]] name = "opentelemetry-semantic-conventions" -version = "0.66b1" +version = "0.66b2" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "opentelemetry-api" }, { name = "typing-extensions" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/46/e4/dbbfb2a010c4db2224a5114638acede6fe563d33cc20fb1752cebcbe6298/opentelemetry_semantic_conventions-0.66b1.tar.gz", hash = "sha256:497ca63bf383723411e8eaf60c8779e9877633c936bb641080adab59d0eb6ec8", size = 150250, upload-time = "2026-10-06T17:33:14.073Z" } +sdist = { url = "https://files.pythonhosted.org/packages/37/3f/50f95972b50e8e353f5b9342034c9cd17730f1956f009ce6a3fbbd89f9ce/opentelemetry_semantic_conventions-0.66b2.tar.gz", hash = "sha256:7da5f1a5dc4383eb66711d4d3d3e71824d9403ac1559fea021cc103c1a4c4976", size = 150246, upload-time = "2026-10-29T17:29:55.682Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/bc/14/67f8aa798857f8cf686f515bf93d9bb877ce952ddc8efae0fa25b45ce0d6/opentelemetry_semantic_conventions-0.66b1-py3-none-any.whl", hash = "sha256:d4cddeb4315490b35213f55e2bdc9ac54bb1e4d318927475bed62b35545e581b", size = 206279, upload-time = "2026-10-06T17:32:56.103Z" }, + { url = "https://files.pythonhosted.org/packages/67/bd/6bf33ed31521c565931c037e9e81bac47c1acb000e850c6e24220e2a0c00/opentelemetry_semantic_conventions-0.66b2-py3-none-any.whl", hash = "sha256:4cbe61e5f9275d57ab31708446e7938ef4291ebbe6a7fca6f7a629c5acae87d8", size = 206280, upload-time = "2026-10-29T17:29:35.062Z" }, ] [[package]]