graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Windows: charmap codec crash in _call_claude_cli subprocess on common markdown chars (→, ✅, ≥)

Open cbeaulieu-gt opened this issue 6 months ago • 0 comments

Summary

On Windows with the default cp1252 console codepage, _call_claude_cli (graphify/llm.py:426) and the claude-cli branch of _call_llm (graphify/llm.py:959) both call subprocess.run(..., text=True) without an explicit encoding=. Python defaults to the locale codec (cp1252 on Windows), which can't encode common markdown characters like → (U+2192), ✅ (U+2705), ≥ (U+2265). The subprocess stdin write raises UnicodeEncodeError before the claude process receives any input.

Behavior is silent partial failure: affected chunks fail mid-extract, the overall command exits 0, and the chunk content is dropped from the final graph. CI and downstream graph consumers cannot tell that data was lost.

Prior UTF-8 fixes (#204 file I/O, #248 git-hooks PYTHONIOENCODING, #788 console _safe(), #832 read_text / write_text in skill.md) do not cover this surface — the _call_claude_cli subprocess was the remaining gap.

Repro

Corpus: glitchwerks/rsl-siege-manager @ 6085fd66. Several markdown files in docs/ contain →, ✅, ≥.

Fresh Windows shell — no PYTHONUTF8 / PYTHONIOENCODING set in env. Run on v8 @ b4c0f01:

graphify extract I:/games/raid/siege-web/.worktrees/graphify-eval-6085fd66 --backend claude-cli

Observed

[graphify] chunk 1/3 failed: 'charmap' codec can't encode character '→' in position 2101: character maps to <undefined>
[graphify] chunk 2/3 failed: 'charmap' codec can't encode character '✅' in position 2118: character maps to <undefined>
[graphify] chunk 3/3 failed: 'charmap' codec can't encode character '≥' in position 2981: character maps to <undefined>
[graphify extract] wrote graph.json: 1107 nodes, 1842 edges, 128 communities

Exit code: 0 — silent. All 3 chunks containing those characters are dropped.

Environment:

  • Windows 11
  • Python 3.12 (uv tool managed)
  • Default console codepage cp1252
  • --backend claude-cli

Expected

Semantic-extraction chunks containing common markdown characters should succeed. The subprocess.run calls invoking the claude CLI need an explicit encoding="utf-8" so stdin/stdout are not subject to the locale codec.

Additionally, silent partial failure should become loud: the extract should emit a summary line at end of run when any chunk failed, so CI / downstream consumers can detect dropped data.

Audit — all I/O sites on the chunk/LLM-request path on v8

Site File Status
_call_claude_cli subprocess.run llm.py:426 NEEDS FIX
_call_llm claude-cli branch subprocess.run llm.py:959 NEEDS FIX
_read_files p.read_text(encoding="utf-8") llm.py:148 OK
_estimate_file_tokens path.read_text(encoding="utf-8") llm.py:594 OK
cache.py read/write paths cache.py:94,102,126 OK
prs.py subprocess.run (gh/git) prs.py:137,159,226,295 Not on LLM/chunk path; CLI output is ASCII-safe

Proposed fix

  1. Add encoding="utf-8" to both subprocess.run calls in _call_claude_cli (llm.py:426) and the claude-cli branch of _call_llm (llm.py:959).
  2. Loud failure: in extract_corpus_parallel (llm.py), track failed_chunks in the merged result dict and print [graphify] WARNING: N/M semantic chunk(s) failed — see errors above. Partial results returned. on stderr at end of run when any chunk failed. Exit code preserved at 0 for backward compat; the WARNING line is the visibility hook.

_safe(...) ASCII fallback from #788 is for stdout printing only — wrapping LLM-bound chunk content with it would corrupt the data, so this fix has to be UTF-8 enforcement at the write boundary, not ASCII fallback.

Verification

After the patch, re-running the same corpus and shell produces:

[graphify extract] wrote graph.json: 1263 nodes, 2037 edges, 154 communities

Zero charmap errors. +156 nodes / +195 edges / +26 communities recovered from the previously-failing chunks. Without PYTHONUTF8=1 or PYTHONIOENCODING=utf-8 in env (i.e., default Windows shell).

PR

#906 — patch + 10 regression tests across 3 classes (subprocess encoding kwarg on both call sites, loud-failure summary, substitution validation); live-validated against the repro corpus above; no-regression checks against worked/example, worked/mixed-corpus, worked/httpx (byte-identical outputs since none contain bug-triggering unicode).

🤖 Generated by Claude Code on behalf of @cbeaulieu-gt

cbeaulieu-gt avatar May 17 '26 01:05 cbeaulieu-gt