From 55c7a11b084b19ca9affc65ecec06df58d1173c4 Mon Sep 17 00:00:00 2001 From: ikumiyo <481023055@qq.com> Date: Sat, 18 Apr 2026 14:47:05 +0800 Subject: [PATCH] fix(codex): preserve proxy env when creating OpenAI client --- run_agent.py | 7 +++++- ...t_create_openai_client_kwargs_isolation.py | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/run_agent.py b/run_agent.py index e8d23d39cac36..82ee1d03bda08 100644 --- a/run_agent.py +++ b/run_agent.py @@ -4568,7 +4568,12 @@ def _create_openai_client(self, client_kwargs: dict, *, reason: str, shared: boo # constructs a fresh one — no stale closed transport can be reused. # Tests in ``tests/run_agent/test_create_openai_client_reuse.py`` and # ``tests/run_agent/test_sequential_chats_live.py`` pin this invariant. - if "http_client" not in client_kwargs: + proxy_env_keys = ( + "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", + "http_proxy", "https_proxy", "all_proxy", + ) + has_proxy_env = any(os.getenv(key) for key in proxy_env_keys) + if "http_client" not in client_kwargs and not has_proxy_env: try: import httpx as _httpx import socket as _socket diff --git a/tests/run_agent/test_create_openai_client_kwargs_isolation.py b/tests/run_agent/test_create_openai_client_kwargs_isolation.py index 98b7ff480c6bf..0cb06f6ad8a9f 100644 --- a/tests/run_agent/test_create_openai_client_kwargs_isolation.py +++ b/tests/run_agent/test_create_openai_client_kwargs_isolation.py @@ -35,3 +35,27 @@ def test_create_openai_client_does_not_mutate_input_kwargs(mock_openai): assert kwargs == snapshot, ( f"_create_openai_client mutated input kwargs; expected {snapshot}, got {kwargs}" ) + + +@patch("run_agent.OpenAI") +def test_create_openai_client_preserves_proxy_env_transport(mock_openai, monkeypatch): + mock_openai.return_value = MagicMock() + monkeypatch.setenv("https_proxy", "http://127.0.0.1:7897") + agent = AIAgent( + api_key="test-key", + base_url="https://chatgpt.com/backend-api/codex", + provider="openai-codex", + model="gpt-5.4", + quiet_mode=True, + skip_context_files=True, + skip_memory=True, + ) + + kwargs = { + "api_key": "test-key", + "base_url": "https://chatgpt.com/backend-api/codex", + } + + agent._create_openai_client(kwargs, reason="test", shared=False) + + assert "http_client" not in mock_openai.call_args.kwargs