mirror of
https://github.com/openai/codex.git
synced 2026-10-30 19:18:49 +00:00
Address Python goal review feedback
This commit is contained in:
@@ -78,6 +78,8 @@ from .generated.v2_all import (
|
||||
)
|
||||
from .models import InitializeResponse, JsonObject, Notification
|
||||
|
||||
_MAX_GOAL_OBJECTIVE_CHARS = 4_000
|
||||
|
||||
|
||||
def _normalize_goal_objective(objective: str) -> str:
|
||||
if not isinstance(objective, str):
|
||||
@@ -85,6 +87,8 @@ def _normalize_goal_objective(objective: str) -> str:
|
||||
objective = objective.strip()
|
||||
if not objective:
|
||||
raise ValueError("goal objective must not be empty")
|
||||
if len(objective) > _MAX_GOAL_OBJECTIVE_CHARS:
|
||||
raise ValueError(f"goal objective must be at most {_MAX_GOAL_OBJECTIVE_CHARS} characters")
|
||||
return objective
|
||||
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ class AsyncCodexClient:
|
||||
def __init__(self, config: CodexConfig | None = None) -> None:
|
||||
"""Create the wrapped sync client that owns the transport process."""
|
||||
self._sync = CodexClient(config=config)
|
||||
self._background_tasks: set[asyncio.Task[None]] = set()
|
||||
|
||||
async def __aenter__(self) -> "AsyncCodexClient":
|
||||
"""Start the Codex process when entering an async context."""
|
||||
@@ -90,6 +91,9 @@ class AsyncCodexClient:
|
||||
async def close(self) -> None:
|
||||
"""Close the wrapped sync client in a worker thread."""
|
||||
await self._call_sync(self._sync.close)
|
||||
if self._background_tasks:
|
||||
await asyncio.gather(*tuple(self._background_tasks), return_exceptions=True)
|
||||
self._background_tasks.clear()
|
||||
|
||||
async def initialize(self) -> InitializeResponse:
|
||||
"""Initialize the Codex session."""
|
||||
@@ -247,17 +251,23 @@ class AsyncCodexClient:
|
||||
try:
|
||||
return await asyncio.shield(operation)
|
||||
except asyncio.CancelledError:
|
||||
try:
|
||||
state, _ = await operation
|
||||
except BaseException:
|
||||
pass
|
||||
else:
|
||||
|
||||
async def cleanup_cancelled_start() -> None:
|
||||
try:
|
||||
state, _ = await operation
|
||||
except BaseException:
|
||||
return
|
||||
try:
|
||||
await self.pause_goal(thread_id)
|
||||
except Exception:
|
||||
pass
|
||||
state.finish()
|
||||
self.unregister_goal_operation(state)
|
||||
finally:
|
||||
state.finish()
|
||||
self.unregister_goal_operation(state)
|
||||
|
||||
cleanup = asyncio.create_task(cleanup_cancelled_start())
|
||||
self._background_tasks.add(cleanup)
|
||||
cleanup.add_done_callback(self._background_tasks.discard)
|
||||
raise
|
||||
|
||||
async def turn_start(
|
||||
|
||||
Reference in New Issue
Block a user