Repository navigation
Description
Bug: cron no_agent script stdout is decoded with the platform default encoding (cp1252 on Windows), mangling non-ASCII output before delivery
Component: cron/scheduler.py → _run_job_script
Version: hermes-agent 0.16.0 (observed on main, HEAD 00c46b8ff)
Platform: Windows 10 (gateway runs as pythonw.exe via the Hermes_Gateway scheduled task). Latent on any host whose locale-preferred encoding for the gateway process is not UTF-8.
Severity: low-to-moderate — output corruption, not a crash. Affects every no_agent (script-only) cron job whose stdout contains non-ASCII characters.
Summary
A no_agent=true cron watchdog that prints any non-ASCII character (a typographic em dash —, accented letters like Finnish ä/ö, an emoji like 🚨, or any user-typed non-English text) has that output delivered to the messaging platform corrupted. Example: a script that prints
Still clocked in (0h01m): … — clock stop to finish.
arrives in Discord as
Still clocked in (0h01m): … â€" clock stop to finish.
i.e. the UTF-8 bytes of — (E2 80 94) are decoded as three cp1252 characters â€".
Root cause
_run_job_script captures the child script's stdout with:
result = subprocess.run(
argv,
capture_output=True,
text=True, # <-- decodes with locale.getpreferredencoding(False)
timeout=script_timeout,
cwd=str(path.parent),
env=run_env,
**popen_kwargs,
)With text=True and no encoding= argument, Python decodes the child's
stdout using the parent process's locale-preferred encoding. Under the Windows
gateway (pythonw.exe launched by a scheduled task) that default is cp1252,
not UTF-8 — even though Hermes_Gateway.cmd sets PYTHONIOENCODING=utf-8 for the
gateway's own stdio, that does not change locale.getpreferredencoding() used
to decode a child process's captured output. The script itself emits perfectly
valid UTF-8; the corruption happens entirely on the capture/decode side in the
scheduler.
Why it's easy to miss
- It does not reproduce from the Hermes terminal tool. The terminal runs
through git-bash with a UTF-8 default, so asubprocess.run(..., text=True)
repro there comes out clean. The bug only manifests under the actual gateway
process. Triggering the live job (cronjob action=run) and reading the
delivered message is the only reliable repro. - A wrapper-side
sys.stdout.reconfigure(encoding="utf-8")does not help —
the mangle is downstream of the child script, in the scheduler's decode step.
Fix
Pin the decode to UTF-8 and tolerate the rare invalid byte:
result = subprocess.run(
argv,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
timeout=script_timeout,
cwd=str(path.parent),
env=run_env,
**popen_kwargs,
)Scripts in the trusted scripts dir are expected to emit UTF-8 (Python's default
output encoding on every modern platform), so forcing UTF-8 on the read side is
correct and matches the producer side. errors="replace" ensures a stray
non-UTF-8 byte degrades to � instead of raising UnicodeDecodeError and
failing the job. Verified: with this change the same watchdog's —, ä/ö, and
🚨 all arrive intact in Discord.
Reproduction
- Create a
no_agent=truecron job whose script prints a line containing a
non-ASCII char (e.g.print("done — ok")), delivering to Discord/Telegram. - On Windows, ensure the job runs under the gateway (the
Hermes_Gateway
scheduled task), not the terminal. cronjob action=run; observe the delivered message showsâ€"instead of—.
Notes
- One-line-equivalent change (two kwargs) in
_run_job_script. - Related ergonomic gotcha worth a docs mention: on Windows, an in-scripts-dir
.sh/.bashcron wrapper fails under the gateway becauseshutil.which("bash")
finds no bash on the gateway's PATH (git-bash is only on the terminal's PATH).
A.pywrapper (run via the scheduler's ownsys.executable) is the reliable
pattern. Not the same bug, but the same class of "works in the terminal, fails
under the gateway" surprise.
Activity
fix(cron): explicit UTF-8 encoding for script stdout capture
GottZ commentedon Jul 19, 2026
Cross-referencing open PRs against this issue: 2 open PRs address it.
encoding="utf-8"anderrors="replace"to thesubprocess.runcall at the site named in the issue (cron/scheduler.py:_run_job_script), fixing the decode vialocale.getpreferredencodinginstead of UTF-8; a new test intests/cron/test_cron_script.pypins the kwargs via monkeypatch as a regression guard.encoding="utf-8"at the same callsite, fixing the cp1252 misdecoding (the core cause), but omitserrors="replace"— an invalid byte would then raiseUnicodeDecodeErrorand fail the job, a case the issue explicitly calls out; also no test, and CI has not run.Duplicates: #42805 and #43857 implement essentially the same change.
Suggested consolidation: merge #42805 — it applies both kwargs derived in the issue at the correct callsite, covering both the misdecoding and the
UnicodeDecodeErrorpath, ships a test anchoring the kwargs, and CI is green. #43857 could be closed as a duplicate: same line, same approach, but incomplete (noerrors="replace", no test, CI not run). After merging #42805 the closes-link will close this issue; what remains open, if anything, is the docs note about the Windows gotcha mentioned in the issue, which neither PR addresses.Cross-PR triage: each diff was read against this issue; verdicts reflect diff content, not PR titles. Part of an automated triage batch.
teknium1 commentedon Jul 25, 2026
Resolved: cron script stdout is decoded utf-8 with errors='replace' on Windows since PR #70875 (
_run_job_script's platform-conditional popen_kwargs), and the surrounding file-I/O class was retired in #71078. Non-ASCII output from no_agent scripts now survives to delivery. Update to latest; reopen with a repro if mangling persists.