Skip to content

fix: add encoding="utf-8" to prompt file open() calls in script_runner (Windows CP950)#607

Open
edenfunf wants to merge 1 commit intomicrosoft:mainfrom
edenfunf:fix/cp950-file-encoding
Open

fix: add encoding="utf-8" to prompt file open() calls in script_runner (Windows CP950)#607
edenfunf wants to merge 1 commit intomicrosoft:mainfrom
edenfunf:fix/cp950-file-encoding

Conversation

@edenfunf
Copy link
Copy Markdown
Contributor

@edenfunf edenfunf commented Apr 7, 2026

Summary

Fixes a UnicodeDecodeError crash when reading or writing .prompt.md files on Windows systems with a non-UTF-8 locale encoding.

Closes #604.

Root Cause

Three open() calls in script_runner.py rely on the platform default encoding. On Windows with CP950/CP936/CP932, Python cannot decode UTF-8 multi-byte sequences (any CJK character, emoji, or non-ASCII content in a prompt file triggers the error).

Changes

src/apm_cli/core/script_runner.py — 3 lines changed

Location Before After
ScriptRunner._execute_script — read compiled file open(compiled_path, "r") open(compiled_path, "r", encoding="utf-8")
PromptCompiler.compile — read source prompt open(prompt_path, "r") open(prompt_path, "r", encoding="utf-8")
PromptCompiler.compile — write compiled output open(output_path, "w") open(output_path, "w", encoding="utf-8")

All other open() and read_text() calls in the codebase already carry an explicit encoding parameter; this brings the remaining three into line.

Test Plan

  • uv run pytest tests/unit/test_script_runner.py -x -v — all tests pass
  • On Windows CP950: create a .prompt.md containing CJK characters, run apm run start — no UnicodeDecodeError

…r (Windows CP950)

PromptCompiler.compile() and _resolve_prompt_file() open .prompt.md files
with plain open() which defaults to the system locale encoding. On Windows
systems set to CP950/CP936/CP932 (Chinese/Japanese/Korean), a UTF-8 encoded
prompt file containing any multibyte character causes:

  UnicodeDecodeError: 'cp950' codec can't decode byte 0x8b in position 12

Three open() calls were missing explicit encoding:
- compiled_path read in ScriptRunner._execute_script
- prompt_path read in PromptCompiler.compile
- output_path write in PromptCompiler.compile

Adding encoding="utf-8" to all three matches the behaviour of the rest of
the codebase and fixes the crash for any non-UTF-8 Windows locale.
@edenfunf edenfunf requested a review from danielmeppiel as a code owner April 7, 2026 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: UnicodeDecodeError reading .prompt.md on Windows CP950 — open() missing encoding parameter

2 participants