Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions openevolve/llm/codex_app_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ def request(self, method: str, params: Optional[Dict[str, Any]], deadline: float
request_id = self._next_id
self._send({"id": request_id, "method": method, "params": params or {}})
while True:
message = self.next_message(deadline)
# Read only fresh messages: next_message() would pop back the
# notification parked below and spin until the deadline.
message = self._read_message(deadline)
if message.get("id") != request_id:
# A notification (e.g. an early item/completed) or a late
# response: park it instead of dropping it.
# response: park it for _collect_response instead of dropping it.
self._pending.append(message)
continue
if "error" in message:
Expand All @@ -147,6 +149,10 @@ def next_message(self, deadline: float) -> Dict[str, Any]:
"""Return the next raw JSON-RPC message, honoring the wall-clock deadline."""
if self._pending:
return self._pending.popleft()
return self._read_message(deadline)

def _read_message(self, deadline: float) -> Dict[str, Any]:
"""Wait for the next new message from the app-server (ignores parked ones)."""
remaining = deadline - time.monotonic()
if remaining <= 0:
raise asyncio.TimeoutError("codex app-server did not respond in time")
Expand Down Expand Up @@ -292,7 +298,7 @@ def _run_app_server(self, prompt: str, timeout: int) -> str:
thread_params: Dict[str, Any] = {
"model": self.model,
"approvalPolicy": "never",
"sandbox": {"mode": "read-only", "networkAccess": False},
"sandbox": "read-only",
}
if self.cwd:
thread_params["cwd"] = self.cwd
Expand All @@ -304,6 +310,7 @@ def _run_app_server(self, prompt: str, timeout: int) -> str:
turn_params: Dict[str, Any] = {
"threadId": thread_id,
"input": [{"type": "text", "text": prompt}],
"sandboxPolicy": {"type": "readOnly", "networkAccess": False},
}
if self.reasoning_effort is not None:
turn_params["effort"] = self.reasoning_effort
Expand All @@ -315,7 +322,9 @@ def _run_app_server(self, prompt: str, timeout: int) -> str:

def _ensure_authenticated(self, session: _AppServerSession, deadline: float) -> None:
result = session.request("account/read", {}, deadline) or {}
if result.get("requiresOpenaiAuth") or result.get("account") is None:
# `requiresOpenaiAuth` is also true for a signed-in ChatGPT account, so
# rely on the account type alone.
if result.get("account") is None:
raise CodexAuthError("Codex app-server reports the account is not authenticated.")
account = result.get("account") or {}
if account.get("type") != "chatgpt":
Expand Down
26 changes: 24 additions & 2 deletions tests/test_codex_app_server_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ def _dispatch(self, method, request_id, params):
elif method == "account/read":
if self.scenario == "unauthenticated":
self._reply(request_id, {"account": None, "requiresOpenaiAuth": True})
elif self.scenario == "chatgpt_requires_openai_auth":
self._reply(
request_id,
{"account": AUTHENTICATED_ACCOUNT, "requiresOpenaiAuth": True},
)
elif self.scenario == "early_notification":
self._notify("configWarning", {"summary": "warn"})
self._reply(
request_id,
{"account": AUTHENTICATED_ACCOUNT, "requiresOpenaiAuth": False},
)
elif self.scenario == "apikey_account":
self._reply(
request_id,
Expand Down Expand Up @@ -252,6 +263,16 @@ def test_authenticated_chatgpt_account_generates(self):
)
self.assertEqual(captured["cmd"], ["codex", "app-server"])

def test_chatgpt_account_with_requires_openai_auth_flag_generates(self):
outcome, _, _ = run_generate(scenario="chatgpt_requires_openai_auth")
self.assertIsNone(outcome["error"])
self.assertEqual(outcome["result"], "hello world")

def test_notification_before_response_does_not_spin(self):
outcome, _, _ = run_generate(scenario="early_notification", cfg_overrides={"timeout": 5})
self.assertIsNone(outcome["error"])
self.assertEqual(outcome["result"], "hello world")

def test_unauthenticated_account_is_rejected_before_any_turn(self):
outcome, fake, _ = run_generate(scenario="unauthenticated")
self.assertIsInstance(outcome["error"], CodexAuthError)
Expand Down Expand Up @@ -287,8 +308,9 @@ def test_thread_is_non_interactive_and_read_only(self):
params = next(p for m, p in fake.requests if m == "thread/start")
self.assertEqual(params["model"], "gpt-5.3-codex")
self.assertEqual(params["approvalPolicy"], "never")
self.assertEqual(params["sandbox"]["mode"], "read-only")
self.assertFalse(params["sandbox"]["networkAccess"])
self.assertEqual(params["sandbox"], "read-only")
turn = next(p for m, p in fake.requests if m == "turn/start")
self.assertEqual(turn["sandboxPolicy"], {"type": "readOnly", "networkAccess": False})

def test_turn_input_and_effort(self):
_, fake, _ = run_generate(
Expand Down
Loading