From 43a362719a43f7e96b3e041d0c4307af2b72eed5 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 26 Jun 2026 07:44:55 -0500 Subject: [PATCH] refac --- cptr/utils/agents/codex.py | 165 ++++++++++++++++++++++++++++++------ cptr/utils/agents/events.py | 16 +++- cptr/utils/chat_task.py | 97 ++++++++++++++++++++- 3 files changed, 250 insertions(+), 28 deletions(-) diff --git a/cptr/utils/agents/codex.py b/cptr/utils/agents/codex.py index 91434e4..2801000 100644 --- a/cptr/utils/agents/codex.py +++ b/cptr/utils/agents/codex.py @@ -3,9 +3,11 @@ from __future__ import annotations import asyncio +import codecs import json import os import re +from collections import deque from contextlib import suppress from typing import Any, AsyncIterator @@ -16,9 +18,13 @@ from cptr.utils.agents.events import ( AgentEvent, AgentReasoningDelta, AgentTextDelta, + AgentToolOutputDelta, AgentToolUpdate, ) +CODEX_STDOUT_CHUNK_SIZE = 64 * 1024 +CODEX_MAX_WIRE_MESSAGE_CHARS = 16 * 1024 * 1024 + class CodexAppServer: def __init__(self, command: str, cwd: str, env: dict[str, str]) -> None: @@ -30,6 +36,8 @@ class CodexAppServer: self.stderr_task: asyncio.Task | None = None self.pending: dict[int, asyncio.Future[dict[str, Any]]] = {} self.events: asyncio.Queue[dict[str, Any]] = asyncio.Queue() + self.stderr_tail: deque[str] = deque(maxlen=20) + self.approval_mode = "auto" self.next_id = 1 async def start(self) -> None: @@ -94,31 +102,111 @@ class CodexAppServer: self.proc.stdin.write((json.dumps(payload) + "\n").encode()) await self.proc.stdin.drain() + async def respond(self, request_id: str | int, result: dict[str, Any]) -> None: + await self._send({"id": request_id, "result": result}) + + async def respond_error(self, request_id: str | int, code: int, message: str) -> None: + await self._send({"id": request_id, "error": {"code": code, "message": message}}) + async def _reader_loop(self) -> None: assert self.proc is not None and self.proc.stdout is not None - while True: - line = await self.proc.stdout.readline() - if not line: - break - try: - message = json.loads(line.decode()) - except Exception: - continue - if ( - "id" in message - and "method" not in message - and ("result" in message or "error" in message) - ): - future = self.pending.pop(int(message["id"]), None) - if future and not future.done(): - future.set_result(message) + reader_error: str | None = None + buffer = "" + decoder = codecs.getincrementaldecoder("utf-8")("replace") + try: + while True: + chunk = await self.proc.stdout.read(CODEX_STDOUT_CHUNK_SIZE) + if not chunk: + break + buffer += decoder.decode(chunk) + while "\n" in buffer: + line, buffer = buffer.split("\n", 1) + await self._handle_stdout_line(line.rstrip("\r")) + if len(buffer) > CODEX_MAX_WIRE_MESSAGE_CHARS: + raise RuntimeError("Codex app-server emitted an oversized message") + buffer += decoder.decode(b"", final=True) + if buffer.strip(): + await self._handle_stdout_line(buffer.rstrip("\r")) + except asyncio.CancelledError: + raise + except Exception as exc: # noqa: BLE001 - surfaced in the chat. + reader_error = str(exc) + finally: + if reader_error: + message = f"Codex app-server transport error: {reader_error}" + self._fail_pending(message) + await self.events.put({"method": "transport/error", "params": {"message": message}}) else: - await self.events.put(message) + message = self._exit_message() + self._fail_pending(message) + await self.events.put({"method": "process/exited", "params": {"message": message}}) + + async def _handle_stdout_line(self, line: str) -> None: + if not line.strip(): + return + if len(line) > CODEX_MAX_WIRE_MESSAGE_CHARS: + raise RuntimeError("Codex app-server emitted an oversized message") + message = json.loads(line) + if not isinstance(message, dict): + raise RuntimeError("Codex app-server emitted an invalid message") + await self._route_message(message) + + async def _route_message(self, message: dict[str, Any]) -> None: + if "id" in message and "method" in message: + await self._handle_server_request(message) + return + if ( + "id" in message + and "method" not in message + and ("result" in message or "error" in message) + ): + try: + request_id = int(message["id"]) + except (TypeError, ValueError): + return + future = self.pending.pop(request_id, None) + if future and not future.done(): + future.set_result(message) + return + if "method" in message: + await self.events.put(message) + + async def _handle_server_request(self, message: dict[str, Any]) -> None: + request_id = message["id"] + method = str(message.get("method") or "") + if method in { + "item/commandExecution/requestApproval", + "item/fileChange/requestApproval", + }: + decision = "decline" if self.approval_mode == "ask" else "accept" + await self.respond(request_id, {"decision": decision}) + return + if method in {"applyPatchApproval", "execCommandApproval"}: + decision = "denied" if self.approval_mode == "ask" else "approved" + await self.respond(request_id, {"decision": decision}) + return + await self.respond_error(request_id, -32601, f"Method not found: {method}") async def _stderr_loop(self) -> None: assert self.proc is not None and self.proc.stderr is not None - while await self.proc.stderr.readline(): - pass + while line := await self.proc.stderr.readline(): + text = line.decode(errors="replace").strip() + if text: + self.stderr_tail.append(text[:2000]) + + def _fail_pending(self, message: str) -> None: + for future in self.pending.values(): + if not future.done(): + future.set_exception(RuntimeError(message)) + self.pending.clear() + + def _exit_message(self) -> str: + detail = "\n".join(self.stderr_tail).strip() + if detail: + return f"Codex app-server exited: {detail}" + if self.proc and self.proc.returncode is not None: + return f"Codex app-server exited with code {self.proc.returncode}." + return "Codex app-server exited." def _messages_to_prompt(messages: list[dict[str, Any]]) -> str: @@ -186,7 +274,7 @@ def _tool_from_item_event(method: str, params: dict[str, Any]) -> AgentToolUpdat arguments={"command": detail} if "command" in normalized_type and detail else {"title": title}, - output=detail if method == "item/completed" else None, + output=None, ) @@ -219,6 +307,24 @@ def _item_detail(item: dict[str, Any]) -> str | None: return None +def _tool_output_from_event(method: str, params: dict[str, Any]) -> AgentToolOutputDelta | None: + if method not in {"item/commandExecution/outputDelta", "item/fileChange/outputDelta"}: + return None + call_id = params.get("itemId") + delta = params.get("delta") + if not isinstance(call_id, str) or not call_id.strip(): + return None + if not isinstance(delta, str) or not delta: + return None + return AgentToolOutputDelta( + call_id=call_id.strip(), + delta=delta, + stream_kind="command_output" + if method == "item/commandExecution/outputDelta" + else "file_change_output", + ) + + async def run_codex_agent( *, profile: dict[str, Any], @@ -238,8 +344,9 @@ async def run_codex_agent( thread_id: str | None = None turn_id: str | None = None try: - await client.start() approval_mode = _chat_approval_mode(chat_params) + client.approval_mode = approval_mode + await client.start() thread_params: dict[str, Any] = { "cwd": workspace, "approvalPolicy": _approval_policy(approval_mode), @@ -295,9 +402,13 @@ async def run_codex_agent( if isinstance(delta, str) and delta: yield AgentReasoningDelta(delta) else: - tool = _tool_from_item_event(str(method or ""), params) - if tool: - yield tool + tool_output = _tool_output_from_event(str(method or ""), params) + if tool_output: + yield tool_output + else: + tool = _tool_from_item_event(str(method or ""), params) + if tool: + yield tool if method == "turn/completed": yield AgentDone( resume_state={ @@ -316,6 +427,12 @@ async def run_codex_agent( elif method == "error": yield AgentError(str(params or "Codex app-server error")) return + elif method == "process/exited": + yield AgentError(str(params.get("message") or "Codex app-server exited.")) + return + elif method == "transport/error": + yield AgentError(str(params.get("message") or "Codex app-server transport error")) + return except asyncio.CancelledError: if thread_id and turn_id: with suppress(Exception): diff --git a/cptr/utils/agents/events.py b/cptr/utils/agents/events.py index 88669c2..d0dcd6d 100644 --- a/cptr/utils/agents/events.py +++ b/cptr/utils/agents/events.py @@ -25,6 +25,13 @@ class AgentToolUpdate: output: str | None = None +@dataclass +class AgentToolOutputDelta: + call_id: str + delta: str + stream_kind: str = "tool_output" + + @dataclass class AgentDone: usage: dict[str, Any] | None = None @@ -36,4 +43,11 @@ class AgentError: message: str -AgentEvent = AgentTextDelta | AgentReasoningDelta | AgentToolUpdate | AgentDone | AgentError +AgentEvent = ( + AgentTextDelta + | AgentReasoningDelta + | AgentToolUpdate + | AgentToolOutputDelta + | AgentDone + | AgentError +) diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 6a4b8e8..91beafe 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -11,7 +11,7 @@ import logging import re import uuid -from cptr.env import CHAT_MAX_ITERATIONS, CHAT_TOOL_MAX_CHARS +from cptr.env import CHAT_MAX_ITERATIONS, CHAT_TOOL_COMMAND_MAX_CHARS, CHAT_TOOL_MAX_CHARS from cptr.utils.context import resolve_compact_token_threshold, should_compact from cptr.utils.skills import discover_skills, load_skill, format_skill_content from cptr.utils.summarize import summarize_messages @@ -35,6 +35,7 @@ from cptr.utils.agents.events import ( AgentError, AgentReasoningDelta, AgentTextDelta, + AgentToolOutputDelta, AgentToolUpdate, ) from cptr.utils.agents.attachments import prepare_agent_attachments @@ -1014,6 +1015,19 @@ def _is_native_agent_tool_item(item: dict) -> bool: ) +def _append_capped_output(existing: str, delta: str, max_chars: int) -> str: + text = f"{existing or ''}{delta}" + if len(text) <= max_chars: + return text + marker = "\n\n...(truncated native agent output)...\n\n" + if max_chars <= len(marker) + 2: + return text[:max_chars] + keep = max_chars - len(marker) + head = keep // 2 + tail = keep - head + return f"{text[:head]}{marker}{text[-tail:]}" + + def _append_prompt_suffix(messages: list[dict], suffix: str) -> None: if not suffix: return @@ -1366,16 +1380,93 @@ async def run_chat_task( } _upsert_output_item(output_items, call_item) await emit(output=call_item) - if event.output is not None or event.status == "completed": + if event.output is not None: + capped_event_output = None + output_limit = ( + CHAT_TOOL_COMMAND_MAX_CHARS + if call_item["name"] == "run_command" + else CHAT_TOOL_MAX_CHARS + ) + capped_event_output = _append_capped_output("", event.output, output_limit) + existing_output = next( + ( + item + for item in output_items + if item.get("type") == "function_call_output" + and item.get("call_id") == event.call_id + ), + None, + ) + if ( + existing_output + and existing_output.get("output") + and capped_event_output is not None + ): + _sync_state() + continue output_item = { "type": "function_call_output", "call_id": event.call_id, "native_agent": True, - "output": event.output or "", + "output": capped_event_output or "", } _upsert_output_item(output_items, output_item) await emit(output=output_item) _sync_state() + elif isinstance(event, AgentToolOutputDelta): + flushed_item = _flush_text() + if flushed_item: + await emit(output=flushed_item) + existing_call = next( + ( + item + for item in output_items + if item.get("type") == "function_call" + and item.get("call_id") == event.call_id + ), + None, + ) + if existing_call is None: + existing_call = { + "type": "function_call", + "id": f"agent-{event.call_id}", + "call_id": event.call_id, + "name": "run_command" + if event.stream_kind == "command_output" + else "agent_tool", + "native_agent": True, + "arguments": {}, + "status": "in_progress", + } + _upsert_output_item(output_items, existing_call) + await emit(output=existing_call) + existing_output = next( + ( + item + for item in output_items + if item.get("type") == "function_call_output" + and item.get("call_id") == event.call_id + ), + {}, + ) + max_chars = ( + CHAT_TOOL_COMMAND_MAX_CHARS + if event.stream_kind == "command_output" + else CHAT_TOOL_MAX_CHARS + ) + output_item = { + "type": "function_call_output", + "call_id": event.call_id, + "native_agent": True, + "output": _append_capped_output( + str(existing_output.get("output") or ""), + event.delta, + max_chars, + ), + } + _upsert_output_item(output_items, output_item) + await emit(output=output_item) + _sync_state() elif isinstance(event, AgentError): raise RuntimeError(event.message) elif isinstance(event, AgentDone):