Repository navigation
Description
Summary
The official Hermes plugin (hooks/hermes/__init__.py, installed via rtk init --agent hermes) calls subprocess.run(["rtk", "rewrite", command], capture_output=True, text=True) without an explicit encoding=. On Windows, text=True decodes the child's output with the ANSI code page (cp936/GBK on zh-CN systems) — but rtk rewrite emits UTF-8. Any command containing characters that don't round-trip through GBK (non-ASCII paths, curly quotes, emoji — i.e. most real-world commands on CJK Windows) makes the reader thread throw UnicodeDecodeError, so result.stdout is left as None, and the plugin then crashes on result.stdout.strip().
The exception is swallowed by the plugin's wide except, so the command fails open and runs unrewritten. No data is corrupted, but the plugin silently loses 100% of its rewriting benefit for those commands, plus emits one stderr noise line per call. The user sees nothing except "rtk gain never goes up".
Environment
- rtk 0.50.0 (winget
rtk-ai.rtk); same defect confirmed on 0.48.0 - Windows 11, locale zh-CN (
locale.getpreferredencoding()→cp936) - Python 3.14.7 / 3.13 (both affected); plugin as shipped by
rtk init --agent hermes(upstream file, sha256f687dcfd…, no local modification)
Minimal repro (outside the plugin, same call the plugin makes)
import subprocess, locale
print(locale.getpreferredencoding(False)) # cp936
r = subprocess.run(["rtk", "rewrite", "cat 中文测试.md"],
shell=False, capture_output=True, text=True, timeout=2)
print(r.returncode, repr(r.stdout))Actual output:
cp936
Exception in thread Thread-1 (_readerthread):
Traceback (most recent call last):
File "...\\Lib\\threading.py", line 1082, in _bootstrap_inner
...
File "...\\Lib\\subprocess.py", line 1614, in _readerthread
buffer.append(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 11: illegal multibyte sequence
3 None
Then inside the plugin (0.50 file, line 71): rewritten = result.stdout.strip() →
AttributeError: 'NoneType' object has no attribute 'strip'
→ caught by the plugin's except Exception → hook returns None → command runs as-is.
Note the NoneType error points at result.stdout, not result.stderr — on the rc=3 rewrite path the stderr.strip() line is never reached, so any fix limited to stderr does not help.
Suggested fix (3 lines, working locally since September)
RTK's output is always UTF-8 regardless of console code page, so decode it as UTF-8 and guard both streams:
result = subprocess.run(
["rtk", "rewrite", command],
shell=False, timeout=2, capture_output=True, text=True,
encoding="utf-8", errors="replace", # RTK emits UTF-8; Windows otherwise decodes as cp936
)
...
rewritten = (result.stdout or "").strip()
...
stderr = (result.stderr or "").strip()Verified on this machine after the patch: rtk rewrite "cat 中文测试.md" → rc=3, stdout = 'rtk read 中文测试.md', rewrite chain end-to-end confirmed via rtk gain counters.
Happy to send a PR if you'll take one — the same one-line encoding= consideration may apply to the other hook adapters that capture subprocess output on Windows.
Activity
rtk-wshm-sync-bot commentedon Oct 12, 2026
🔍 Automated Triage
bughighSummary
Hermes plugin's subprocess.run uses text=True without encoding=utf-8 on Windows, so non-GBK output triggers UnicodeDecodeError, leaving stdout=None and silently disabling rewriting.
📁 Relevant files
hooks/hermes/__init__.pySuggested Actions
or ""Triaged automatically by wshm · This is an automated analysis, not a human review.
laolitongxue commentedon Nov 23, 2026
Still reproduces on every current ref — could this be fixed upstream?
hooks/hermes/rtk-rewrite/__init__.pyv1.2.1(latest stable, 2026-11-19)0b39e5a4…developHEADdev-1.2.2-rc.670subprocess.run(..., text=True)still passes noencoding=, so on a zh-CN Windows (ANSI codepage 936) Python decodes rtk's UTF-8 stdout as GBK. Two silent outcomes:1. The mangled rewrite is applied anyway. When the bytes happen to be valid GBK, the corrupted command is still handed to the agent:
rtk rewritestill exits 0/3, so nothing is reported. The command then fails with "file not found", or — if the mangled name happens to exist — operates on the wrong path.2. Fail-open. When the bytes are not valid GBK:
UnicodeDecodeErrorin the reader thread →stdout is None→result.stdout.strip()raisesAttributeError→ the wideexceptswallows it and no rewrite happens. Observed asrtk: hermes plugin warning: 'NoneType' object has no attribute 'strip'and a flatrtk gain.Either way, on a zh-CN machine any command with non-ASCII paths loses the savings silently, and a subset gets corrupted.
The fix is three lines and is already proposed in #4527 (which now shows as conflicting):
The
or ""guards are needed even after the encoding fix:stdout/stderrcan still beNonewhen the reader thread dies, and the plugin otherwise raises a second error while trying to report the first.Repro without Hermes (run under a cp936 interpreter, e.g.
PYTHONUTF8=0):Happy to open a rebased PR against
developwith those three lines plus the two tests from #4527, if that is easier than reviving #4527.Environment: rtk 1.2.1, Windows 11 zh-CN (codepage 936), Python 3.14 as bundled with Hermes.
acalexanderac commentedon Nov 24, 2026
I can work on this one
let's take a look