Skip to content

The agent that grows with you

fix(cron): decode the Windows bot-chat lane as the UTF-8 the child writes

liuhao102400ecaf772179
diff --git a/cron/scheduler_delivery.py b/cron/scheduler_delivery.py
index 9cc9dfd4ef2..a71a960a920 100644
--- a/cron/scheduler_delivery.py
+++ b/cron/scheduler_delivery.py
@@ -742,15 +742,25 @@ 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).
+ # Lossy decode everywhere; on Windows also decode as the UTF-8 the child writes.
+ # A 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 the reply and the failure tail with it (#105582; same
+ # errors= hardening as _run_job_script). On win32 the child is guaranteed
+ # UTF-8 — hermes_cli reconfigures its own streams via hermes_bootstrap even
+ # under PYTHONIOENCODING=cp1252 — while the gateway parent is NOT started in
+ # UTF-8 mode (its env overlay sets only PYTHONIOENCODING), so text=True alone
+ # decodes the pipes with the ANSI code page: accented replies come back
+ # mojibake'd, or the reader thread dies on bytes undefined in cp1252 and the
+ # captured reply is silently lost while the delivery still books as delivered
+ # (#115894). On POSIX the child keeps the locale codec, so the locale default
+ # stays correct there (#66566).
+ popen_kwargs: dict = {"errors": "replace"}
+ if sys.platform == "win32":
+ popen_kwargs["encoding"] = "utf-8"
proc = subprocess.Popen(
argv, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
- errors="replace",
- env=env, creationflags=windows_hide_flags())
+ env=env, creationflags=windows_hide_flags(), **popen_kwargs)
streams: dict = {}
def _drain() -> None:
diff --git a/tests/cron/test_cron_bot_chat_delivery.py b/tests/cron/test_cron_bot_chat_delivery.py
index b7194fab8ec..0b663bbbc06 100644
--- a/tests/cron/test_cron_bot_chat_delivery.py
+++ b/tests/cron/test_cron_bot_chat_delivery.py
@@ -311,6 +311,26 @@ def test_bot_chat_turn_failure_tail_decodes_lossily(tmp_path):
assert result.stderr == "boom before \ufffd after\n"
+ getattr(sys.flags, "utf8_mode", 0),
+ reason="parent already in UTF-8 mode; the ANSI-codepage mismatch is not exercisable",
+)
+def test_bot_chat_turn_roundtrips_accented_utf8_reply(tmp_path):
+ """The delivery child writes UTF-8 unconditionally — hermes_cli reconfigures its
+ own streams via hermes_bootstrap on Windows even under PYTHONIOENCODING=cp1252 —
+ while the gateway parent there is NOT started in UTF-8 mode, so text=True alone
+ decoded the pipes with the ANSI code page: the reply came back mojibake'd, or the
+ reader thread died on bytes undefined in cp1252 and the reply was silently lost
+ while the delivery still booked as delivered (#115894)."""
+ text = "AÇÃO ÍNDICE: relatório nº 3\n"
+ child = "import sys; sys.stdout.write({!r})".format(text)
+ 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.stdout == text
+
+
# ── delivery-targets listing (UI pickers) ────────────────────────────────────
def test_delivery_targets_include_local_profiles():