The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): decode cron child output lossily (script + bot-chat delivery)
liuhao10248be901ac0f8b
diff --git a/cron/scheduler_delivery.py b/cron/scheduler_delivery.pyindex bcc0285551b..9cc9dfd4ef2 100644--- a/cron/scheduler_delivery.py+++ b/cron/scheduler_delivery.py@@ -742,8 +742,14 @@ def _run_bot_chat_turn(argv: list, env: dict, report_path: str, timeout: float)"""from hermes_cli.quiet_single_query import read_turn_report+ # Lossy decode: this child's stdout/stderr only feed the reply capture and the+ # failure tail, so one stray non-UTF-8 byte (e.g. a grandchild sharing the pipe+ # interleaving a partial multi-byte write) must not raise UnicodeDecodeError in+ # the drain thread and take both stream tails with it (#105582; same errors=+ # hardening as _run_job_script).proc = subprocess.Popen(argv, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,+ errors="replace",env=env, creationflags=windows_hide_flags())streams: dict = {}diff --git a/cron/scheduler_script.py b/cron/scheduler_script.pyindex 60b28770422..55a5faf61ea 100644--- a/cron/scheduler_script.py+++ b/cron/scheduler_script.py@@ -344,7 +344,15 @@ def _run_job_script(try:from tools.environments.local import build_subprocess_env- popen_kwargs: dict[str, Any] = {"start_new_session": True}+ # Lossy decode only: keep the platform-default (locale) encoding — gating ``encoding=``+ # to win32 was deliberate (#66566: unconditional UTF-8 leaked into POSIX) — but+ # ``errors=`` must not stay 'strict': one stray non-UTF-8 byte in the script's stdout+ # or stderr raises UnicodeDecodeError in communicate() and fails the whole run,+ # discarding the output (#105582; the Windows branch decodes lossily per #45099).+ popen_kwargs: dict[str, Any] = {+ "start_new_session": True,+ "errors": "replace",+ }if sys.platform == "win32":popen_kwargs = {"creationflags": windows_hide_flags()diff --git a/tests/cron/test_cron_bot_chat_delivery.py b/tests/cron/test_cron_bot_chat_delivery.pyindex bf80f9632e2..b7194fab8ec 100644--- a/tests/cron/test_cron_bot_chat_delivery.py+++ b/tests/cron/test_cron_bot_chat_delivery.py@@ -285,6 +285,32 @@ def test_turn_that_never_ends_is_still_killed_at_the_cap(tmp_path):assert time.monotonic() - started < 8[email protected]_only+def test_bot_chat_turn_decodes_stray_stderr_bytes_lossily(tmp_path):+ """A stray non-UTF-8 byte on the delivery child's stderr (e.g. a grandchild+ sharing the pipe interleaving a partial multi-byte write) must not raise+ UnicodeDecodeError in the drain thread — the turn is booked and the stream+ tail survives with U+FFFD (#105582)."""+ child = "import os, sys; os.write(2, b'noise before \\x80 after\\n'); sys.exit(0)"+ result = sched_delivery._run_bot_chat_turn(+ [sys.executable, "-c", child], _child_env(), str(tmp_path / "turn.json"), timeout=15)++ assert result.returncode == 0+ assert result.stderr == "noise before \ufffd after\n"++[email protected]_only+def test_bot_chat_turn_failure_tail_decodes_lossily(tmp_path):+ """The exit-1 tail is still surfaced (with U+FFFD for the bad byte) instead of+ vanishing when the drain thread dies at the first undecodable byte (#105582)."""+ child = "import os, sys; os.write(2, b'boom before \\x80 after\\n'); sys.exit(1)"+ result = sched_delivery._run_bot_chat_turn(+ [sys.executable, "-c", child], _child_env(), str(tmp_path / "turn.json"), timeout=15)++ assert result.returncode == 1+ assert result.stderr == "boom before \ufffd after\n"++# ── delivery-targets listing (UI pickers) ────────────────────────────────────def test_delivery_targets_include_local_profiles():diff --git a/tests/cron/test_cron_script.py b/tests/cron/test_cron_script.pyindex 29bb3b1fa92..7ca29fa83b3 100644--- a/tests/cron/test_cron_script.py+++ b/tests/cron/test_cron_script.py@@ -97,6 +97,27 @@ class TestRunJobScript:assert success is Trueassert output == "hello from script"+ def test_script_stdout_non_utf8_decoded_lossily(self, cron_env):+ """A stray non-UTF-8 byte in script stdout must not fail the run (#105582).++ The POSIX decode path used text=True without errors= (i.e. errors='strict'), so a+ single bad byte raised UnicodeDecodeError in communicate() and the whole run failed+ with "Script execution failed: 'utf-8' codec can't decode ...", discarding the+ output. The Windows branch already decoded lossily (#45099).+ """+ from cron.scheduler_script import _run_job_script++ script = cron_env / "scripts" / "binary_stdout.py"+ script.write_text(+ "import sys\n"+ 'sys.stdout.buffer.write(b"alert before \\x80 after\\n")\n'+ )++ success, output = _run_job_script(str(script))+ assert success is True+ assert "alert before" in output+ assert "\ufffd" in output+def test_script_relative_path(self, cron_env):from cron.scheduler_script import _run_job_script@@ -292,7 +313,13 @@ class TestRunJobScript:sys.platform == "win32",reason="Windows always takes the overlay/creationflags branch",)- def test_non_windows_script_preserves_default_text_decoding(self, cron_env, monkeypatch):+ def test_non_windows_script_keeps_locale_encoding_with_lossy_errors(self, cron_env, monkeypatch):+ """POSIX keeps the platform-default (locale) encoding — gating ``encoding=`` to win32+ was deliberate (#66566: unconditional UTF-8 leaked into POSIX) — but decoding must be+ lossy: ``errors='replace'`` so a stray non-UTF-8 byte in script output cannot raise+ UnicodeDecodeError in communicate() and fail the whole run (#105582). Supersedes+ ``test_non_windows_script_preserves_default_text_decoding``, which pinned strict+ decoding as a side effect of the win32 encoding gate."""# No platform patching: the Linux CI host already takes this branch.from cron import scheduler as sched_modfrom cron import scheduler_script as sched_script@@ -331,7 +358,7 @@ class TestRunJobScript:assert captured["kwargs"]["text"] is Trueassert "creationflags" not in captured["kwargs"]assert "encoding" not in captured["kwargs"]- assert "errors" not in captured["kwargs"]+ assert captured["kwargs"]["errors"] == "replace"def test_non_overlay_branch_keeps_plain_argv(self, cron_env, monkeypatch):"""When the Windows uv-venv overlay is NOT active, the invocation must