Skip to content

Commit 549fcee

Browse files
fix(extensions): preserve subprocess errors for non-UTF8 stderr (#5186)
1 parent 2b0361d commit 549fcee

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎src/agents/extensions/experimental/codex/exec.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ async def _read_stdout_line() -> bytes:
204204

205205
if process.returncode not in (0, None):
206206
await stderr_task
207-
stderr_text = b"".join(stderr_chunks).decode("utf-8")
207+
stderr_text = b"".join(stderr_chunks).decode("utf-8", errors="replace")
208208
raise RuntimeError(
209209
f"Codex exec exited with code {process.returncode}: {stderr_text}"
210210
)

‎tests/extensions/experiemental/codex/test_codex_exec_thread.py‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,13 @@ async def fake_create_subprocess_exec(*args: Any, **kwargs: Any) -> FakeProcess:
588588

589589

590590
@pytest.mark.asyncio
591+
@pytest.mark.parametrize("stderr_text", ["bad", "找不到命令"])
591592
async def test_codex_exec_run_raises_on_non_zero_exit(
592-
monkeypatch: pytest.MonkeyPatch,
593+
monkeypatch: pytest.MonkeyPatch, stderr_text: str
593594
) -> None:
594-
process = FakeProcess(stdout_lines=[], stderr_chunks=[b"bad"], returncode=2)
595+
process = FakeProcess(
596+
stdout_lines=[], stderr_chunks=[stderr_text.encode("utf-8")], returncode=2
597+
)
595598

596599
async def fake_create_subprocess_exec(*args: Any, **kwargs: Any) -> FakeProcess:
597600
return process
@@ -601,10 +604,44 @@ async def fake_create_subprocess_exec(*args: Any, **kwargs: Any) -> FakeProcess:
601604
exec_client = exec_module.CodexExec(executable_path="/bin/codex")
602605
args = exec_module.CodexExecArgs(input="hello")
603606

604-
with pytest.raises(RuntimeError, match="exited with code 2"):
607+
with pytest.raises(RuntimeError) as exc_info:
605608
async for _ in exec_client.run(args):
606609
pass
607610

611+
assert str(exc_info.value) == f"Codex exec exited with code 2: {stderr_text}"
612+
613+
614+
@pytest.mark.asyncio
615+
@pytest.mark.parametrize("streamed", [False, True])
616+
async def test_codex_thread_preserves_exit_error_with_non_utf8_stderr(
617+
monkeypatch: pytest.MonkeyPatch, streamed: bool
618+
) -> None:
619+
# Captured Windows CP950 diagnostic from the npm Codex shim in issue #5185.
620+
stderr = (
621+
b"'\"node\"' \xa4\xa3\xacO\xa4\xba\xb3\xa1\xa9\xce\xa5~\xb3\xa1\xa9R\xa5O"
622+
b"\xa1B\xa5i\xb0\xf5\xa6\xe6\xaa\xba\xb5{\xa6\xa1\xa9\xce\xa7\xe5\xa6\xb8\xc0\xc9\xa1C\r\n"
623+
)
624+
process = FakeProcess(stdout_lines=[], stderr_chunks=[stderr], returncode=1)
625+
626+
async def fake_create_subprocess_exec(*args: Any, **kwargs: Any) -> FakeProcess:
627+
return process
628+
629+
monkeypatch.setattr(exec_module.asyncio, "create_subprocess_exec", fake_create_subprocess_exec)
630+
thread = Codex(codex_path_override="/bin/codex").start_thread()
631+
632+
with pytest.raises(RuntimeError) as exc_info:
633+
if streamed:
634+
result = await thread.run_streamed("hello")
635+
async for _ in result.events:
636+
pass
637+
else:
638+
await thread.run("hello")
639+
640+
message = str(exc_info.value)
641+
assert message.startswith("Codex exec exited with code 1: '\"node\"' ")
642+
assert "\ufffd" in message
643+
assert message.endswith("\r\n")
644+
608645

609646
@pytest.mark.asyncio
610647
async def test_codex_exec_run_raises_without_stdin(monkeypatch: pytest.MonkeyPatch) -> None:

0 commit comments

Comments
 (0)