Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix(grok): decode CLI stdout as UTF-8, not the OS locale codec (#1025)

SNChicago5af840d40e73
diff --git a/changelog.d/+grok-x-windows-utf8-decode.fixed.md b/changelog.d/+grok-x-windows-utf8-decode.fixed.md
new file mode 100644
index 00000000..226cca11
--- /dev/null
+++ b/changelog.d/+grok-x-windows-utf8-decode.fixed.md
@@ -0,0 +1 @@
+The Grok CLI's stdout is now decoded as UTF-8 instead of the OS locale codec. On Windows (cp1252), an emoji or smart quote in an X post's text crashed the decode inside `subprocess.run`, which surfaced as "no items parsed" from the `grok` X backend rather than a real error.
diff --git a/skills/last30days/scripts/lib/grok_x.py b/skills/last30days/scripts/lib/grok_x.py
index 60657ce0..9aad2027 100644
--- a/skills/last30days/scripts/lib/grok_x.py
+++ b/skills/last30days/scripts/lib/grok_x.py
@@ -646,6 +646,12 @@ def _invoke(prompt: str, timeout: int) -> Dict[str, Any]:
[binary, "-p", prompt, "--permission-mode", "bypassPermissions"],
capture_output=True,
text=True,
+ # X posts are not cp1252. Without this, text=True decodes the CLI's
+ # stdout with the locale codec and a reader thread dies on the first
+ # emoji or smart quote, which surfaces as "no items parsed" rather
+ # than an error. Matches the auth-store read above.
+ encoding="utf-8",
+ errors="replace",
timeout=timeout,
cwd=workdir,
env=_subprocess_env(child_home),
diff --git a/tests/test_grok_x.py b/tests/test_grok_x.py
index b25d63c1..7e9bc8a7 100644
--- a/tests/test_grok_x.py
+++ b/tests/test_grok_x.py
@@ -205,6 +205,24 @@ def test_resolved_binary_path_is_used_not_bare_name(monkeypatch):
assert seen["cmd"][0] == "/opt/custom/grok"
+def test_subprocess_decodes_stdout_as_utf8_not_locale(monkeypatch):
+ """text=True with no explicit encoding decodes with the locale codec
+ (cp1252 on Windows). X post text is not cp1252, so a bare emoji or
+ smart quote in the CLI's stdout would otherwise crash the decode and
+ surface as "no items parsed" instead of a real error."""
+ seen = {}
+ monkeypatch.setattr(grok_x, "binary_path", lambda: "/usr/bin/grok")
+
+ def fake_run(cmd, **kwargs):
+ seen["kwargs"] = kwargs
+ return subprocess.CompletedProcess(cmd, 0, _block("2087568620465607078"), "")
+
+ monkeypatch.setattr(grok_x.subprocess, "run", fake_run)
+ grok_x.search_x("steipete", *WINDOW)
+ assert seen["kwargs"].get("encoding") == "utf-8"
+ assert seen["kwargs"].get("errors") == "replace"
+
+
def test_missing_binary_returns_error_not_raises(monkeypatch):
monkeypatch.setattr(grok_x, "binary_path", lambda: None)
result = grok_x.search_x("steipete", *WINDOW)