Skip to content

cron no_agent script stdout decoded with platform default (cp1252 on Windows), mangling non-ASCII output before delivery #42785

Closed

Description

@dertzi

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 a subprocess.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

  1. Create a no_agent=true cron job whose script prints a line containing a
    non-ASCII char (e.g. print("done — ok")), delivering to Discord/Telegram.
  2. On Windows, ensure the job runs under the gateway (the Hermes_Gateway
    scheduled task), not the terminal.
  3. 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/.bash cron wrapper fails under the gateway because shutil.which("bash")
    finds no bash on the gateway's PATH (git-bash is only on the terminal's PATH).
    A .py wrapper (run via the scheduler's own sys.executable) is the reliable
    pattern. Not the same bug, but the same class of "works in the terminal, fails
    under the gateway" surprise.

Activity

  1. added
    type/bugSomething isn't working
    P3Low — cosmetic, nice to have
    comp/cronCron scheduler and job management
    on Jun 9, 2026
  2. GottZ commented on Jul 19, 2026

    @GottZ

    Cross-referencing open PRs against this issue: 2 open PRs address it.

    • fix(cron): decode no-agent script output as UTF-8 #42805 (+30/-0) — fixes the root cause: adds exactly the proposed kwargs encoding="utf-8" and errors="replace" to the subprocess.run call at the site named in the issue (cron/scheduler.py:_run_job_script), fixing the decode via locale.getpreferredencoding instead of UTF-8; a new test in tests/cron/test_cron_script.py pins the kwargs via monkeypatch as a regression guard.
    • fix(cron): explicit UTF-8 encoding for script stdout capture #43857 (+1/-0) — partial: the one-line diff sets encoding="utf-8" at the same callsite, fixing the cp1252 misdecoding (the core cause), but omits errors="replace" — an invalid byte would then raise UnicodeDecodeError and 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 UnicodeDecodeError path, ships a test anchoring the kwargs, and CI is green. #43857 could be closed as a duplicate: same line, same approach, but incomplete (no errors="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.

  3. teknium1 commented on Jul 25, 2026

    @teknium1
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low — cosmetic, nice to havecomp/cronCron scheduler and job managementtype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions