Skip to content

Windows: memory files corrupted with mojibake — Haiku CLI output decoded as cp1252 instead of UTF-8 #91

Closed

Description

@marketechniks

Description

On Windows, any non-ASCII character in a Haiku summary (→, —, ↔, curly quotes — characters Haiku uses routinely) is permanently corrupted into mojibake when written to now.md / today-*.md / recent.md / archive.md. Example from a real memory file:

## 07:43-08:22 | unknown
Migrated 249 ECC skills ... reduced MCPs 6â†'1 (kept exa) ... ~7â€"8k tokens/session savings.

(â†' should be →, â€" should be —.)

Root cause: the pipeline's file I/O is consistently explicit UTF-8 (nice work), but two process-boundary reads rely on Python's platform default encoding, which on Windows is the ANSI code page (cp1252), not UTF-8:

  1. pipeline/shell.py:160 — raw = sys.stdin.read() in parse-haiku. This is fed by save-session.sh:183 (echo "$HAIKU_JSON" | $PYTHON -m pipeline.shell parse-haiku). PEP 528's UTF-8 console only applies to interactive consoles; redirected stdin/pipes use the locale encoding on Windows through at least Python 3.13.
  2. pipeline/haiku.py:69 — subprocess.run(..., text=True) with no encoding= argument decodes the claude CLI's UTF-8 stdout as cp1252 (consolidation path).

So UTF-8 bytes from Haiku are mis-decoded as cp1252 (e.g. → = E2 86 92 → the three characters â†'), then faithfully re-encoded as UTF-8 by the (correct) file writes — making the corruption permanent. Entries that cycle through the pipeline again (NDC, consolidation) get double-mangled (ââ€...).

It can also silently lose data, not just corrupt it: cp1252 has five unmapped bytes (0x81, 0x8D, 0x8F, 0x90, 0x9D). UTF-8 sequences containing them — common in CJK and other non-Latin text — raise UnicodeDecodeError in parse-haiku, the save fails, and that memory entry is never written. Non-English users are disproportionately affected.

Reproduction

Windows + Git Bash (the hook environment), any Python ≤ 3.13 without UTF-8 mode:

printf '\xe2\x86\x92' | python -c "import sys; print(ascii(sys.stdin.read()))"
# Actual:   '\xe2†’'   (the 'â†'' mojibake)
# Expected: '→'             (→)

printf '\xe2\x86\x92' | PYTHONUTF8=1 python -c "import sys; print(ascii(sys.stdin.read()))"
# Correct:  '→'

Or end-to-end: run any session on Windows where the Haiku summary contains → or —, then inspect .remember/now.md.

Expected behavior

Haiku summary text reaches the memory files byte-identical (UTF-8 in → UTF-8 out).

Actual behavior

Every non-ASCII character is mojibake'd on first write, double-mangled on re-compression; some non-Latin content can fail to save entirely (UnicodeDecodeError in parse-haiku).

Suggested fix

Smallest robust change — make UTF-8 explicit at both process boundaries:

# pipeline/shell.py (parse-haiku, before reading)
sys.stdin.reconfigure(encoding="utf-8")   # Python >=3.7
raw = sys.stdin.read()
# or: raw = sys.stdin.buffer.read().decode("utf-8")

# pipeline/haiku.py
result = subprocess.run(..., text=True, encoding="utf-8")

Alternatively (belt-and-braces, one line): export PYTHONUTF8=1 in scripts/resolve-paths.sh (sourced by every entry point) covers all $PYTHON invocations at once.

Workaround for affected users meanwhile: add "PYTHONUTF8": "1" to the env block of ~/.claude/settings.json (takes effect on next session). Existing files need manual cleanup — the corruption is baked in.

Environment

  • claude-remember version: 0.7.3
  • Claude Code version: 2.1.161
  • Shell: Git Bash (hooks), system locale cp1252
  • OS: Windows 11 Pro (10.0.26300)
  • Python version: 3.13.13

Activity

  1. fdaviddpt commented on Jun 18, 2026

    @fdaviddpt
    Contributor

    Fixed in #104 — thank you, this is a textbook bug report. 🙏

    You pinpointed both process boundaries (sys.stdin.read() in parse-haiku, subprocess.run(text=True) in haiku.py), explained why CI missed it (PEP 528's UTF-8 console only covers interactive consoles; redirected pipes use the locale codec), and even flagged the silent-data-loss path via cp1252's unmapped bytes hitting non-Latin text. The printf '\xe2\x86\x92' | python repro made it unambiguous.

    The fix makes both boundaries explicit UTF-8 and — going further — audits every read/write/encode seam, leaving machine-JSON strict while making user-prose and process input resilient. New tests force a non-UTF-8 locale (PYTHONUTF8=0 PYTHONCOERCECLOCALE=0 LC_ALL=C) so they'd have caught this on any OS. Thanks again for the depth here.

  2. added a commit that references this issue on Jun 19, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions