From f028f9ab7a386da347e9da440c1d6f03c396feb8 Mon Sep 17 00:00:00 2001 From: CarlesUIPath Date: Fri, 13 Nov 2026 13:01:35 +0000 Subject: [PATCH 1/6] =?UTF-8?q?fix(codex):=201/4=20=E2=80=94=20give=20the?= =?UTF-8?q?=20app-server=20the=20custom=20provider=20so=20openai-codex=200?= =?UTF-8?q?.167+=20accepts=20turns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit openai-codex 0.167.x rejects turn/start with JSON-RPC -32600 when only the thread names the CODEX_BASE_URL provider. Build the provider once (_custom_provider) and send it to the app-server as --config overrides (serialised with tomli_w) as well as to the thread. The default endpoint still starts the client with config=None. Co-Authored-By: Claude Opus 5.5 --- .claude/notes/agents.md | 24 ++++++ src/coder_eval/agents/codex_agent.py | 85 +++++++++++++++------ tests/test_codex_agent.py | 87 +++++++++++++++++++++ tests/test_codex_provider_binary.py | 110 +++++++++++++++++++++++++++ 4 files changed, 282 insertions(+), 24 deletions(-) create mode 100644 tests/test_codex_provider_binary.py diff --git a/.claude/notes/agents.md b/.claude/notes/agents.md index e196345d..60ef8458 100644 --- a/.claude/notes/agents.md +++ b/.claude/notes/agents.md @@ -753,6 +753,30 @@ 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 as before, and +`start()` passes it to the app-server as two `--config` overrides (`model_provider` and the +`model_providers.custom` inline table). Both seams are kept (PI decision D1): the thread +seam is the tested production path, and both read the one helper. The TOML value is +written with `tomli_w`, already 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 base URL is now on the app-server command line. It is not a secret and already +travels in the thread-start payload; the key stays in the env (`env_key`), and a +credential embedded in the URL is unsupported. 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/src/coder_eval/agents/codex_agent.py b/src/coder_eval/agents/codex_agent.py index 8171ca98..94c805e4 100644 --- a/src/coder_eval/agents/codex_agent.py +++ b/src/coder_eval/agents/codex_agent.py @@ -18,6 +18,8 @@ from typing import Any, ClassVar, Literal, NamedTuple from urllib.parse import urlparse +import tomli_w + from coder_eval.agent import Agent, AgentState from coder_eval.agents._logging import PrefixedAdapter, log_raw_sdk_event from coder_eval.agents.registry import AgentRegistry @@ -81,8 +83,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 +389,14 @@ def _message_uncached_input(m: AssistantMessage) -> int: # supported"), so this is a fixed constant, not an operator knob. _CODEX_WIRE_API = "responses" + +def _toml_inline(value: str | dict[str, Any]) -> 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()) + "}" + + # 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 @@ -1068,9 +1078,13 @@ async def start( 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_overrides = self._app_server_config_overrides() + config = ( + CodexConfig(env=env_override, config_overrides=config_overrides) + if env_override or config_overrides + else None + ) # Close any prior client FIRST: start() runs through # execute_with_retry, so a retried start would otherwise orphan the @@ -1488,6 +1502,29 @@ def _resolve_api_version() -> str | None: """ return os.getenv("CODEX_API_VERSION") or None + @staticmethod + def _custom_provider() -> dict[str, Any] | 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, Any] = { + "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. @@ -1529,6 +1566,22 @@ 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. + + openai-codex 0.167.x rejects ``turn/start`` (-32600) when only the thread names the + provider, so the app-server carries the same definition as the thread options. + + Rationale: .claude/notes/agents.md § Codex custom provider on the app-server + """ + provider = self._custom_provider() + if provider is None: + return () + 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,34 +1758,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'})" + f"Codex routed via custom provider (host={urlparse(provider['base_url']).hostname or '(unknown)'}, " + + f"api_version={'set' if 'query_params' in provider else 'unset'})" ) if self.config.context_window is not None: diff --git a/tests/test_codex_agent.py b/tests/test_codex_agent.py index f2af4b0d..6ff00fbb 100644 --- a/tests/test_codex_agent.py +++ b/tests/test_codex_agent.py @@ -5,6 +5,7 @@ it isn't installed, so `make test` / CI stay green without the extra. """ +import tomllib from datetime import datetime import pytest @@ -252,6 +253,49 @@ 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): + """A base URL alone (no key, no prepend) still builds a CodexConfig carrying the overrides.""" + from types import SimpleNamespace + + import openai_codex + + for var in ("CODEX_BASE_URL", "CODEX_API_VERSION", "CODEX_API_KEY"): + monkeypatch.delenv(var, raising=False) + monkeypatch.setenv("CODEX_BASE_URL", "https://gw.local/openai/v1") + captured: dict = {} + monkeypatch.setattr( + openai_codex, "Codex", lambda **kw: captured.update(kw) or SimpleNamespace(close=lambda: None) + ) + + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="dep")) + await agent.start(str(tmp_path)) + + config = captured["config"] + assert isinstance(config, openai_codex.CodexConfig) + assert config.config_overrides == agent._app_server_config_overrides() + assert config.config_overrides + assert config.env is None + + @pytest.mark.asyncio + async def test_start_default_endpoint_passes_no_config(self, monkeypatch, tmp_path): + """No base URL, no key, no prepend: the client starts with config=None, as before.""" + from types import SimpleNamespace + + import openai_codex + + for var in ("CODEX_BASE_URL", "CODEX_API_VERSION", "CODEX_API_KEY"): + monkeypatch.delenv(var, raising=False) + captured: dict = {} + monkeypatch.setattr( + openai_codex, "Codex", lambda **kw: captured.update(kw) or SimpleNamespace(close=lambda: None) + ) + + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX)) + await agent.start(str(tmp_path)) + + assert captured["config"] is None + class TestCustomProviderRouting: """Test that CODEX_BASE_URL injects a custom model provider.""" @@ -262,6 +306,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 +374,48 @@ 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] + 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..355b4b91 --- /dev/null +++ b/tests/test_codex_provider_binary.py @@ -0,0 +1,110 @@ +"""The real bundled codex binary accepts the app-server provider overrides and routes a turn through them. + +Starts the app-server against a local HTTP stub, with no network and a throw-away CODEX_HOME. The +thread options lose their provider here, so only the app-server seam can reach the stub. +Skips when the codex extra or its binary is absent. +""" + +import asyncio +import contextlib +import threading +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +import pytest + +from coder_eval.agents.codex_agent import CodexAgent +from coder_eval.models import AgentKind, parse_agent_config + + +pytest.importorskip("openai_codex") +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"}}' + + +def _start_stub(paths: list[str]) -> ThreadingHTTPServer: + class Handler(BaseHTTPRequestHandler): + def _reply(self) -> None: + paths.append(self.path) + 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 test_app_server_overrides_route_turn_to_custom_endpoint(monkeypatch, tmp_path): + paths: list[str] = [] + server = _start_stub(paths) + 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) + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="stub-model")) + 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: + with contextlib.suppress(Exception): + await agent.communicate("say hi", timeout=20) + finally: + await agent.stop() + + errors: list[BaseException] = [] + + def run() -> None: + try: + asyncio.run(scenario()) + except BaseException as exc: + errors.append(exc) + + worker = threading.Thread(target=run, daemon=True) + worker.start() + worker.join(timeout=60) + if worker.is_alive(): + agent.kill_sync() + worker.join(timeout=5) + pytest.fail("codex app-server scenario did not finish within 60 s") + finally: + server.shutdown() + server.server_close() + + assert any(p.startswith("/v1/responses") and "api-version=2025-04-01-preview" in p for p in paths), ( + f"stub saw {paths}; scenario errors: {errors}" + ) From 2fb31fe8aa6c8fcf135d5e30ddfb2c8a4e67908a Mon Sep 17 00:00:00 2001 From: CarlesUIPath Date: Fri, 13 Nov 2026 13:02:27 +0000 Subject: [PATCH 2/6] =?UTF-8?q?docs(codex):=202/4=20=E2=80=94=20document?= =?UTF-8?q?=20the=20app-server=20provider=20seam=20and=20the=20-32600=20sy?= =?UTF-8?q?mptom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- docs/agents/CODEX.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/agents/CODEX.md b/docs/agents/CODEX.md index f434c3ce..27a804a1 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"`. Do not embed a credential in `CODEX_BASE_URL`, because the URL is on the app-server command line. Put the key only in `CODEX_API_KEY`. + ## Usage ### Command Line From 936abb6a68be917776d724b2ddeffc53e09406e1 Mon Sep 17 00:00:00 2001 From: CarlesUIPath Date: Fri, 13 Nov 2026 13:19:40 +0000 Subject: [PATCH 3/6] fix(codex): address review findings on the app-server provider seam - Type the provider as a recursive _TomlValue alias (str leaves only), so pyright rejects a non-str leaf before _toml_inline crashes start(). - Warn (do not raise) when CODEX_BASE_URL carries userinfo, because the URL reaches the app-server argv; the warning names neither URL nor credential. - Debug log reads the resolved base URL and api-version, not the dict shape. - Docstrings state the contract only; the base-URL env fact lives in one place. - Unit tests: a named Codex spy fixture replaces the duplicated setup; add a URL-credential warning test. - Binary test: parametrize app-server-only and app-server-and-thread, assert the Bearer key arrives, surface communicate() errors, and reap a client that hangs in Codex() construction on timeout. - Notes: drop the plan decision id and change-history wording. - CLAUDE.md: list tomli-w as a runtime dependency. - harness-candidates: latest-openai-codex CI job; no c/ plan refs in docs. Co-Authored-By: Claude Opus 5.5 --- .claude/harness-candidates.md | 13 +++++ .claude/notes/agents.md | 26 +++++---- CLAUDE.md | 2 +- docs/agents/CODEX.md | 2 +- src/coder_eval/agents/codex_agent.py | 40 +++++++++----- tests/test_codex_agent.py | 77 ++++++++++++++++++-------- tests/test_codex_provider_binary.py | 82 +++++++++++++++++++--------- 7 files changed, 164 insertions(+), 78 deletions(-) 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 60ef8458..821b7454 100644 --- a/.claude/notes/agents.md +++ b/.claude/notes/agents.md @@ -762,18 +762,20 @@ Python SDK surface (`CodexConfig.config_overrides`) is identical from 0.155.1 to 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 as before, and -`start()` passes it to the app-server as two `--config` overrides (`model_provider` and the -`model_providers.custom` inline table). Both seams are kept (PI decision D1): the thread -seam is the tested production path, and both read the one helper. The TOML value is -written with `tomli_w`, already 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 base URL is now on the app-server command line. It is not a secret and already -travels in the thread-start payload; the key stays in the env (`env_key`), and a -credential embedded in the URL is unsupported. CI runs only the lock-pinned binary, so a -binary bump needs a manual live check: a fresh venv per version built with +`_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 +is unsupported: `_custom_provider()` logs a warning that names neither the URL nor the +credential, and does not raise, so the run continues. 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. 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 27a804a1..94a4d198 100644 --- a/docs/agents/CODEX.md +++ b/docs/agents/CODEX.md @@ -62,7 +62,7 @@ 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"`. Do not embed a credential in `CODEX_BASE_URL`, because the URL is on the app-server command line. Put the key only in `CODEX_API_KEY`. +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"`. Do not embed a credential in `CODEX_BASE_URL`, because the URL is on the app-server command line; coder_eval logs a warning if the URL contains one. Put the key only in `CODEX_API_KEY`. ## Usage diff --git a/src/coder_eval/agents/codex_agent.py b/src/coder_eval/agents/codex_agent.py index 94c805e4..be2c3c6b 100644 --- a/src/coder_eval/agents/codex_agent.py +++ b/src/coder_eval/agents/codex_agent.py @@ -16,7 +16,7 @@ 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 @@ -389,14 +389,25 @@ 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: str | dict[str, Any]) -> str: + +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 _url_has_userinfo(url: str) -> bool: + """Whether ``url`` carries a username or password; False for a URL that does not parse.""" + try: + parts = urlsplit(url) + except ValueError: + return False + return parts.username is not None or parts.password is not None + + # 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 @@ -1503,18 +1514,24 @@ def _resolve_api_version() -> str | None: return os.getenv("CODEX_API_VERSION") or None @staticmethod - def _custom_provider() -> dict[str, Any] | None: + 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. + param and CODEX_MODEL is the deployment name. A credential embedded in the URL + logs a warning, because the URL reaches the app-server command line. """ base_url = CodexAgent._resolve_base_url() if not base_url: return None - provider: dict[str, Any] = { + if _url_has_userinfo(base_url): + logger.warning( + "CODEX_BASE_URL embeds a credential, which is visible on the codex app-server " + + "command line; put the key only in CODEX_API_KEY." + ) + provider: dict[str, _TomlValue] = { "name": "Custom", "base_url": base_url, "env_key": "CODEX_API_KEY", @@ -1536,8 +1553,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 @@ -1569,8 +1585,7 @@ def _build_codex_env(self) -> dict[str, str] | None: def _app_server_config_overrides(self) -> tuple[str, ...]: """``--config`` overrides that select the custom provider on the app-server itself. - openai-codex 0.167.x rejects ``turn/start`` (-32600) when only the thread names the - provider, so the app-server carries the same definition as the thread options. + The definition is the one the thread options carry; empty on the default endpoint. Rationale: .claude/notes/agents.md § Codex custom provider on the app-server """ @@ -1767,10 +1782,9 @@ def _build_thread_options(self) -> dict[str, Any]: + "— the provider may reject the request." ) tool_config["model_providers"] = {_CUSTOM_PROVIDER_ID: provider} - self._log.debug( - f"Codex routed via custom provider (host={urlparse(provider['base_url']).hostname or '(unknown)'}, " - + f"api_version={'set' if 'query_params' in provider else 'unset'})" - ) + host = urlparse(self._resolve_base_url() or "").hostname or "(unknown)" + api_version = "set" if self._resolve_api_version() else "unset" + self._log.debug(f"Codex routed via custom provider (host={host}, api_version={api_version})") 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 6ff00fbb..63444e96 100644 --- a/tests/test_codex_agent.py +++ b/tests/test_codex_agent.py @@ -23,6 +23,32 @@ 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.""" @@ -254,47 +280,28 @@ 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): + 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.""" - from types import SimpleNamespace - import openai_codex - for var in ("CODEX_BASE_URL", "CODEX_API_VERSION", "CODEX_API_KEY"): - monkeypatch.delenv(var, raising=False) monkeypatch.setenv("CODEX_BASE_URL", "https://gw.local/openai/v1") - captured: dict = {} - monkeypatch.setattr( - openai_codex, "Codex", lambda **kw: captured.update(kw) or SimpleNamespace(close=lambda: None) - ) agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="dep")) await agent.start(str(tmp_path)) - config = captured["config"] + config = codex_spy.config assert isinstance(config, openai_codex.CodexConfig) assert config.config_overrides == agent._app_server_config_overrides() assert config.config_overrides assert config.env is None @pytest.mark.asyncio - async def test_start_default_endpoint_passes_no_config(self, monkeypatch, tmp_path): - """No base URL, no key, no prepend: the client starts with config=None, as before.""" - from types import SimpleNamespace - - import openai_codex - - for var in ("CODEX_BASE_URL", "CODEX_API_VERSION", "CODEX_API_KEY"): - monkeypatch.delenv(var, raising=False) - captured: dict = {} - monkeypatch.setattr( - openai_codex, "Codex", lambda **kw: captured.update(kw) or SimpleNamespace(close=lambda: None) - ) - + async def test_start_default_endpoint_passes_no_config(self, tmp_path, codex_spy): + """No base URL, no key, no prepend: the client starts with config=None.""" agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX)) await agent.start(str(tmp_path)) - assert captured["config"] is None + assert codex_spy.config is None class TestCustomProviderRouting: @@ -416,6 +423,28 @@ def test_app_server_overrides_escape_toml_specials(self, monkeypatch): assert tomllib.loads(overrides[1])["model_providers"]["custom"]["base_url"] == base_url assert "\n" not in overrides[1] + @pytest.mark.parametrize( + ("base_url", "warns"), + [ + ("https://user:s3cret-pw@gw.local/v1", True), + ("https://token-only@gw.local/v1", True), + ("https://gw.local/v1", False), + ("http://[::1/v1", False), + ], + ) + def test_credential_in_base_url_warns_without_echoing_it(self, monkeypatch, caplog, base_url, warns): + """Userinfo in CODEX_BASE_URL logs a warning that names neither the URL nor the credential.""" + monkeypatch.setenv("CODEX_BASE_URL", base_url) + agent = CodexAgent(parse_agent_config(type=AgentKind.CODEX, model="dep")) + + with caplog.at_level("WARNING", logger="coder_eval.agents.codex_agent"): + overrides = agent._app_server_config_overrides() + + assert len(overrides) == 2 + warnings = [r.getMessage() for r in caplog.records if "embeds a credential" in r.getMessage()] + assert bool(warnings) is warns + assert not any("s3cret-pw" in w or "gw.local" in w for w in warnings) + 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 index 355b4b91..71e83ed8 100644 --- a/tests/test_codex_provider_binary.py +++ b/tests/test_codex_provider_binary.py @@ -1,14 +1,15 @@ -"""The real bundled codex binary accepts the app-server provider overrides and routes a turn through them. +"""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 -thread options lose their provider here, so only the app-server seam can reach the stub. +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 contextlib import threading from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from typing import Any, NamedTuple import pytest @@ -16,7 +17,7 @@ from coder_eval.models import AgentKind, parse_agent_config -pytest.importorskip("openai_codex") +codex_client = pytest.importorskip("openai_codex.client") codex_cli_bin = pytest.importorskip("codex_cli_bin") try: codex_cli_bin.bundled_codex_path() @@ -26,12 +27,18 @@ pytestmark = pytest.mark.integration _STUB_ERROR = b'{"error":{"message":"stub","type":"invalid_request_error"}}' +_SCENARIO_TIMEOUT_S = 60 -def _start_stub(paths: list[str]) -> ThreadingHTTPServer: +class _Request(NamedTuple): + path: str + authorization: str | None + + +def _start_stub(requests: list[_Request]) -> ThreadingHTTPServer: class Handler(BaseHTTPRequestHandler): def _reply(self) -> None: - paths.append(self.path) + 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") @@ -53,9 +60,24 @@ def log_message(self, format: str, *args: object) -> None: return server -def test_app_server_overrides_route_turn_to_custom_endpoint(monkeypatch, tmp_path): - paths: list[str] = [] - server = _start_stub(paths) +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() @@ -67,27 +89,28 @@ def test_app_server_overrides_route_turn_to_custom_endpoint(monkeypatch, tmp_pat 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")) - build_thread_options = agent._build_thread_options + 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 + 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) + monkeypatch.setattr(agent, "_build_thread_options", thread_options_without_provider) async def scenario() -> None: await agent.start(str(tmp_path)) try: - with contextlib.suppress(Exception): - await agent.communicate("say hi", timeout=20) + await agent.communicate("say hi", timeout=20) + except Exception as exc: + errors.append(exc) finally: await agent.stop() - errors: list[BaseException] = [] - def run() -> None: try: asyncio.run(scenario()) @@ -96,15 +119,20 @@ def run() -> None: worker = threading.Thread(target=run, daemon=True) worker.start() - worker.join(timeout=60) + worker.join(timeout=_SCENARIO_TIMEOUT_S) if worker.is_alive(): agent.kill_sync() - worker.join(timeout=5) - pytest.fail("codex app-server scenario did not finish within 60 s") + 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() - assert any(p.startswith("/v1/responses") and "api-version=2025-04-01-preview" in p for p in paths), ( - f"stub saw {paths}; scenario errors: {errors}" - ) + 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}" From ad435e1ee9430ee3f808a5fd078aaba446357145 Mon Sep 17 00:00:00 2001 From: CarlesUIPath Date: Fri, 13 Nov 2026 15:37:39 +0000 Subject: [PATCH 4/6] test(codex): catch Exception, not BaseException, in the binary-test worker CodeQL flags the BaseException handler. Exception covers every error the scenario can raise; a failure still surfaces in the assertion message. Co-Authored-By: Claude Opus 5.5 --- tests/test_codex_provider_binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_codex_provider_binary.py b/tests/test_codex_provider_binary.py index 71e83ed8..dda33bf9 100644 --- a/tests/test_codex_provider_binary.py +++ b/tests/test_codex_provider_binary.py @@ -114,7 +114,7 @@ async def scenario() -> None: def run() -> None: try: asyncio.run(scenario()) - except BaseException as exc: + except Exception as exc: errors.append(exc) worker = threading.Thread(target=run, daemon=True) From e78a968221504b1ca72ac0e82c658ee56ffdfd45 Mon Sep 17 00:00:00 2001 From: CarlesUIPath Date: Fri, 13 Nov 2026 17:12:44 +0000 Subject: [PATCH 5/6] fix(codex): reject a credential in CODEX_BASE_URL and address PR review nits - A credential in CODEX_BASE_URL, or a URL that does not parse, now fails start() with a non-retryable AgentConfigError before the app-server spawns, instead of a warning that left the credential on argv. The check runs once, on the argv seam only; the message never echoes the value. - _custom_provider() is a pure builder again. - start() always builds CodexConfig: the SDK treats None and a default CodexConfig the same. - The routing debug log reads the provider dict, not the env again. - Tests: Azure shape (key env plus overrides), default endpoint config fields, rejection before spawn. Co-Authored-By: Claude Opus 5.5 --- .claude/notes/agents.md | 6 ++- docs/agents/CODEX.md | 2 +- src/coder_eval/agents/codex_agent.py | 43 +++++++++-------- tests/test_codex_agent.py | 71 +++++++++++++++++++++------- 4 files changed, 81 insertions(+), 41 deletions(-) diff --git a/.claude/notes/agents.md b/.claude/notes/agents.md index 821b7454..18cde6b8 100644 --- a/.claude/notes/agents.md +++ b/.claude/notes/agents.md @@ -773,8 +773,10 @@ are escaped. `json.dumps` is not valid TOML for U+007F or surrogate escapes. The 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 -is unsupported: `_custom_provider()` logs a warning that names neither the URL nor the -credential, and does not raise, so the run continues. CI runs only the lock-pinned binary, +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. diff --git a/docs/agents/CODEX.md b/docs/agents/CODEX.md index 94a4d198..367f39a4 100644 --- a/docs/agents/CODEX.md +++ b/docs/agents/CODEX.md @@ -62,7 +62,7 @@ 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"`. Do not embed a credential in `CODEX_BASE_URL`, because the URL is on the app-server command line; coder_eval logs a warning if the URL contains one. Put the key only in `CODEX_API_KEY`. +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 diff --git a/src/coder_eval/agents/codex_agent.py b/src/coder_eval/agents/codex_agent.py index be2c3c6b..b57998dd 100644 --- a/src/coder_eval/agents/codex_agent.py +++ b/src/coder_eval/agents/codex_agent.py @@ -26,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, @@ -399,13 +400,20 @@ def _toml_inline(value: _TomlValue) -> str: return "{" + ", ".join(f"{_toml_inline(k)} = {_toml_inline(v)}" for k, v in value.items()) + "}" -def _url_has_userinfo(url: str) -> bool: - """Whether ``url`` carries a username or password; False for a URL that does not parse.""" +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: - return False - return parts.username is not None or parts.password is not None + 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 @@ -1085,17 +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 - env_override = self._build_codex_env() - config_overrides = self._app_server_config_overrides() - config = ( - CodexConfig(env=env_override, config_overrides=config_overrides) - if env_override or config_overrides - 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 @@ -1520,17 +1523,11 @@ def _custom_provider() -> dict[str, _TomlValue] | None: 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. A credential embedded in the URL - logs a warning, because the URL reaches the app-server command line. + param and CODEX_MODEL is the deployment name. """ base_url = CodexAgent._resolve_base_url() if not base_url: return None - if _url_has_userinfo(base_url): - logger.warning( - "CODEX_BASE_URL embeds a credential, which is visible on the codex app-server " - + "command line; put the key only in CODEX_API_KEY." - ) provider: dict[str, _TomlValue] = { "name": "Custom", "base_url": base_url, @@ -1587,11 +1584,15 @@ def _app_server_config_overrides(self) -> tuple[str, ...]: 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)}", @@ -1782,9 +1783,9 @@ def _build_thread_options(self) -> dict[str, Any]: + "— the provider may reject the request." ) tool_config["model_providers"] = {_CUSTOM_PROVIDER_ID: provider} - host = urlparse(self._resolve_base_url() or "").hostname or "(unknown)" - api_version = "set" if self._resolve_api_version() else "unset" - self._log.debug(f"Codex routed via custom provider (host={host}, api_version={api_version})") + 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 63444e96..f266901c 100644 --- a/tests/test_codex_agent.py +++ b/tests/test_codex_agent.py @@ -7,6 +7,7 @@ import tomllib from datetime import datetime +from unittest.mock import AsyncMock import pytest @@ -20,6 +21,7 @@ _CODEX_APPROVAL_MODE, CodexAgent, ) +from coder_eval.errors import AgentConfigError from coder_eval.models import AgentConfig, AgentKind, AssistantMessage, ReconciliationMessage, parse_agent_config @@ -291,17 +293,53 @@ async def test_start_passes_provider_overrides_to_codex_config(self, monkeypatch 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.config_overrides assert config.env is None @pytest.mark.asyncio - async def test_start_default_endpoint_passes_no_config(self, tmp_path, codex_spy): - """No base URL, no key, no prepend: the client starts with config=None.""" + 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)) - assert codex_spy.config is None + 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: @@ -424,26 +462,25 @@ def test_app_server_overrides_escape_toml_specials(self, monkeypatch): assert "\n" not in overrides[1] @pytest.mark.parametrize( - ("base_url", "warns"), + ("base_url", "reason"), [ - ("https://user:s3cret-pw@gw.local/v1", True), - ("https://token-only@gw.local/v1", True), - ("https://gw.local/v1", False), - ("http://[::1/v1", False), + ("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_credential_in_base_url_warns_without_echoing_it(self, monkeypatch, caplog, base_url, warns): - """Userinfo in CODEX_BASE_URL logs a warning that names neither the URL nor the credential.""" + 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 caplog.at_level("WARNING", logger="coder_eval.agents.codex_agent"): - overrides = agent._app_server_config_overrides() + with pytest.raises(AgentConfigError, match=reason) as exc_info: + agent._app_server_config_overrides() - assert len(overrides) == 2 - warnings = [r.getMessage() for r in caplog.records if "embeds a credential" in r.getMessage()] - assert bool(warnings) is warns - assert not any("s3cret-pw" in w or "gw.local" in w for w in warnings) + 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: From 1c768ec6bedf63bca6bfddb60ac2c648f33d355b Mon Sep 17 00:00:00 2001 From: CarlesUIPath Date: Fri, 13 Nov 2026 17:21:24 +0000 Subject: [PATCH 6/6] chore(deps): bump opentelemetry to 1.45.2 for CVE-2026-126360 pip-audit fails the Quality Gate on opentelemetry-api 1.45.1 (ReDoS in TraceContextTextMapPropagator.extract, fixed in 1.45.2). opentelemetry-sdk pins the api in lockstep, so api, sdk, instrumentation and semantic-conventions move together. Lock-only change. Co-Authored-By: Claude Opus 5.5 --- uv.lock | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) 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]]