From 1516924e995978e535f76b3c1911f95fbc2691dd Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 5 Aug 2026 01:59:39 -0500 Subject: [PATCH] refac --- cptr/utils/agents/acp.py | 8 ++++++-- cptr/utils/agents/codex.py | 19 +++++++++++++++---- cptr/utils/agents/opencode.py | 8 ++++++-- cptr/utils/agents/pi.py | 17 +++++++++++++++-- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/cptr/utils/agents/acp.py b/cptr/utils/agents/acp.py index 747b6d3..349bc43 100644 --- a/cptr/utils/agents/acp.py +++ b/cptr/utils/agents/acp.py @@ -188,8 +188,12 @@ class AcpClient: 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 True: + try: + if not await self.proc.stderr.readline(): + break + except ValueError: + continue async def _handle_message(self, message: dict[str, Any]) -> None: if ( diff --git a/cptr/utils/agents/codex.py b/cptr/utils/agents/codex.py index 3f7c9c0..96c3182 100644 --- a/cptr/utils/agents/codex.py +++ b/cptr/utils/agents/codex.py @@ -190,10 +190,21 @@ class CodexAppServer: async def _stderr_loop(self) -> None: assert self.proc is not None and self.proc.stderr is not None - while line := await self.proc.stderr.readline(): - text = line.decode(errors="replace").strip() - if text: - self.stderr_tail.append(text[:2000]) + while True: + try: + line = await self.proc.stderr.readline() + except ValueError as exc: + self._append_stderr(f"stderr line too long: {exc}", truncated=True) + continue + if not line: + break + self._append_stderr(line.decode(errors="replace")) + + def _append_stderr(self, text: str, *, truncated: bool = False) -> None: + text = text.strip() + if text: + suffix = " [truncated]" if truncated else "" + self.stderr_tail.append(f"{text[:2000]}{suffix}") def _fail_pending(self, message: str) -> None: for future in self.pending.values(): diff --git a/cptr/utils/agents/opencode.py b/cptr/utils/agents/opencode.py index 4d80a80..b588236 100644 --- a/cptr/utils/agents/opencode.py +++ b/cptr/utils/agents/opencode.py @@ -108,8 +108,12 @@ async def _opencode_server(profile: dict[str, Any], workspace: str): async def _drain_stderr(proc: asyncio.subprocess.Process) -> None: assert proc.stderr is not None - while await proc.stderr.readline(): - pass + while True: + try: + if not await proc.stderr.readline(): + break + except ValueError: + continue def _headers(profile: dict[str, Any]) -> dict[str, str]: diff --git a/cptr/utils/agents/pi.py b/cptr/utils/agents/pi.py index fa1618c..166e714 100644 --- a/cptr/utils/agents/pi.py +++ b/cptr/utils/agents/pi.py @@ -223,8 +223,21 @@ class PiRpcClient: async def _read_stderr(self) -> None: assert self.proc and self.proc.stderr - while line := await self.proc.stderr.readline(): - self.stderr_tail.append(line.decode(errors="replace").rstrip()) + while True: + try: + line = await self.proc.stderr.readline() + except ValueError as exc: + self._append_stderr(f"stderr line too long: {exc}", truncated=True) + continue + if not line: + break + self._append_stderr(line.decode(errors="replace")) + + def _append_stderr(self, text: str, *, truncated: bool = False) -> None: + text = text.strip() + if text: + suffix = " [truncated]" if truncated else "" + self.stderr_tail.append(f"{text[:2000]}{suffix}") def _fail_pending(self, message: str) -> None: for future in self.pending.values():