Skip to content

shell.py stdout print() boundary still mangles non-ASCII paths on Windows (unaudited seam left by #91/#104) #145

Closed

Description

@shenk-ana

Description

save-session.sh reliably fails at the build-prompt step with FileNotFoundError for a %TEMP%-reachable path containing non-ASCII characters (e.g. a CJK Windows username), even though the file in question genuinely exists on disk.

This is the same class of bug as #91/PR #104 (Windows defaulting to the system ANSI codepage instead of UTF-8 at a process boundary) — but a different boundary that audit didn't cover: pipeline/shell.py's own print() calls, which emit KEY=value lines (EXTRACT_FILE=..., HAIKU_TEXT_FILE=..., etc.) for bash to capture via safe_eval. #104's fixes covered sys.stdin.reconfigure() in parse-haiku and subprocess.run(encoding="utf-8") in haiku.py, plus errors="replace" on file reads/writes — but there's no sys.stdout.reconfigure() anywhere in shell.py, so every print() that a downstream $PYTHON call or bash script later consumes as a path/string is still subject to this.

Reproduction

Steps to reproduce (needs a Windows account with a non-ASCII, e.g. CJK, username):

  1. Trigger a save (save-session.sh, called automatically by post-tool-hook.sh, or run directly with --force).
  2. Watch it fail at the build-prompt step.

Expected behavior

save-session.sh should complete the full extract → build-prompt → Haiku → write pipeline regardless of what characters are in the user's Windows profile path.

Actual behavior

The extract step succeeds and creates a temp file that really exists on disk (confirmed independently via ls). The very next step, build-prompt, fails with FileNotFoundError on that same path. Logged only as [error] FAILED at line 166 (exit 1) in hook-errors.log, with a mangled-looking path and no indication that it's an encoding issue rather than a missing/deleted file.

Investigation

EXTRACT_FILE is produced by one $PYTHON -m pipeline.shell extract ... call (cmd_extract, shell.py:94: print(f"EXTRACT_FILE={_shell_escape(extract_file)}")) and captured by bash via safe_eval (command substitution); it's then passed as an argv string to a second $PYTHON -m pipeline.shell build-prompt "$EXTRACT_FILE" ... call. Between those two steps the path's non-ASCII characters get corrupted:

$ cd path/to/plugin && python -m pipeline.shell extract "<session-id>" "<project-dir>"
EXTRACT_FILE=C:\Users\<mojibake>\AppData\Local\Temp\remember-extract-wnuk9p2n.txt

Decoding the raw bytes as cp932 and comparing against the true username's cp932 encoding confirms the mechanism: the username's cp932 bytes end up interpreted one byte per character (a Latin-1-over-multibyte mojibake), not the actual username — i.e. print()'s stdout encoding is the system ANSI codepage (cp932 here) rather than UTF-8, exactly the mechanism #91 documented for the stdin/subprocess boundaries, just on the output side of shell.py this time.

Confirmed by forcing UTF-8 stdout on both invocations:

$ PYTHONIOENCODING=utf-8 python -m pipeline.shell extract "<session-id>" "<project-dir>"
EXTRACT_FILE=C:\Users\<correct-username>\AppData\Local\Temp\remember-extract-sgyms12i.txt   # renders correctly

$ OUT=$(PYTHONIOENCODING=utf-8 python -m pipeline.shell extract ...)
$ EXTRACT_FILE=$(echo "$OUT" | grep '^EXTRACT_FILE=' | cut -d= -f2-)
$ PYTHONIOENCODING=utf-8 python -c "import sys, os; print(os.path.exists(sys.argv[1]))" "$EXTRACT_FILE"
True   # round-trips correctly through a second process now

Re-running save-session.sh --force end-to-end after setting PYTHONIOENCODING=utf-8 completes cleanly: extract → build-prompt → Haiku call → now.md write → NDC compression → today-*.md, no errors.

Suggested fix

Consistent with #104's approach for sys.stdin (shell.py:181-182), add a guarded sys.stdout.reconfigure() at the top of main() so every cmd_* function's print() output is explicit UTF-8, regardless of the ambient console codepage:

def main() -> None:
    if hasattr(sys.stdout, "reconfigure"):
        sys.stdout.reconfigure(encoding="utf-8", errors="replace")
    ...

This is a single, minimal addition at the one place all cmd_* output funnels through, rather than a per-print() fix, and mirrors the existing stdin-reconfigure pattern already in the file. (A blanket PYTHONIOENCODING=utf-8/export PYTHON in scripts/detect-tools.sh also works as a belt-and-braces environment-level fix — that's what I used locally to confirm the diagnosis — but the sys.stdout.reconfigure() change keeps the fix inside the audited boundary list from #104 rather than introducing a new environment-variable convention.)

Environment

  • claude-remember version: 0.8.3
  • Claude Code version: 2.1.220 (Claude Code)
  • Shell: Git Bash (MSYS2)
  • OS: Windows 11
  • Python version: 3.14.3
  • System codepage: cp932 (Japanese Windows), non-ASCII (CJK) Windows username

Activity

  1. added 2 commits that reference this issue on Jul 25, 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