Skip to content

Fix the red Cross-platform parity run: decode PowerShell stdout as UTF-8 - #10476

Merged
danielhanchen merged 1 commit into
mainfrom
fix-parity-utf8-stdout
Sep 8, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix-parity-utf8-stdout

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Cross-platform parity has been red on main since #10386 merged, and the failure is in the test harness rather than in install.ps1.

What fails

test_a_non_ascii_marker_survives_the_rollback fails on both shells:

AssertionError: {'After': '...\käffee cache'}
assert '...käffee cache' == '...käffee cache'

käffee is the UTF-8 encoding of käffee read back as cp1252.

Why

_run_powershell calls run_pwsh(..., text = True) without an encoding, and run_pwsh forwards its kwargs to subprocess.run untouched. text = True on its own decodes with the locale codec, which is cp1252 on a Windows runner. Both pwsh and Windows PowerShell 5.1 write stdout as UTF-8, so the JSON the script emits is decoded with the wrong codec and every non-ASCII path in it turns to mojibake.

The assertion is about a value that made a round trip through the marker file, so the mojibake looked like Write-StudioUvCacheMarker or Restore-StudioUvCacheMarker corrupting it. They do not.

Evidence that the product is fine

Driving the three real functions extracted from install.ps1 over the same round trip and reading the marker back as bytes:

BEFORE bytes: b'.../k\xc3\xa4ffee cache\n'
AFTER  bytes: b'.../k\xc3\xa4ffee cache\n'

Byte-identical, still UTF-8. Separately, the mojibake reproduces exactly from the transport alone:

raw bytes : b'{"After":"C:\\\\tmp\\\\k\xc3\xa4ffee cache"}'
as utf-8  : C:\tmp\käffee cache
as cp1252 : C:\tmp\käffee cache

The second line is what the test expects and the third is what CI reported.

The fix

Pass encoding = "utf-8" on that one call, so stdout is decoded as what the shells actually write.

Scoped to this file rather than to run_pwsh itself. Defaulting the shared runner to UTF-8 would change decoding for every caller across the suite, and that is a wider blast radius than this failure justifies.

Verification

Under a non-UTF-8 locale, before and after:

WITHOUT fix, LC_ALL=C:  1 failed   (UnicodeDecodeError in subprocess.py)
WITH    fix, LC_ALL=C:  1 passed

LC_ALL=C raises where cp1252 silently substitutes, but it is the same root cause: the locale codec is not what the shell wrote. Full file passes locally, 45 tests.

The Windows leg of Cross-platform parity on this PR is the real check, since neither locale here is cp1252.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-08T04:23:10.472953Z 39cfde9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

This duplicates #10432, which was opened yesterday for the same root cause and is already green, including parity (windows-latest).

The two diagnoses agree exactly: _run_powershell passes text = True with no encoding, run_pwsh forwards kwargs to subprocess.run untouched, and on a Windows runner the locale codec is cp1252 while both shells write UTF-8. The change here is a strict subset of #10432's, which additionally:

  • sets errors = "strict", so a future mismatch raises at the pipe instead of quietly producing a wrong string;
  • prepends the UTF-8 preamble that install.ps1 itself opens with, pinning the emit side so the result stops depending on the runner's console code page. A script that only borrows install.ps1's functions never executes that preamble, which is exactly why this case was exposed and test_windows_setup_output_encoding.py was not;
  • replaces assert result["After"] == expected, result with a codepoint diff. The old assertion rendered through the very stdout under suspicion, so the expected side came back as U+FFFD and the report read as though the correct half were the broken one. The new message names a cause only when re-encoding through cp1252/latin-1/cp437/cp850 actually reproduces the mojibake, so a genuine restore bug is not explained away as an encoding artefact.

Verified on #10432's run: 672 passed, 9 skipped, against 2 failed, 670 passed, 9 skipped before. Same skip count, both parametrisations flipped, so the powershell 5.1 half is covered too.

Suggest closing this one in favour of #10432. Same fix, already proven on a Windows runner.

For whoever picks this up: four open PRs are currently red on this alone (#10474, #10473, #10471, #10461), all showing the identical 2 failed, 670 passed, 9 skipped, and none of them caused it. Landing #10432 clears all four plus main.

@danielhanchen
danielhanchen merged commit 9fa8d60 into main Sep 8, 2026
18 checks passed
@danielhanchen
danielhanchen deleted the fix-parity-utf8-stdout branch September 8, 2026 04:52
rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Sep 8, 2026
unslothai#10476 fixed the red Cross-platform parity run by decoding PowerShell's stdout
as UTF-8, and its own windows-latest run is green, so the transport bug itself
is closed. This is the part that failure exposed and that fix did not cover.

errors is strict. `encoding = "utf-8"` alone still defaults to errors="strict"
for subprocess text mode today, but the default is not the contract, and the
cost of being wrong here is the exact failure unslothai#10476 just fixed: an undecodable
byte becomes U+FFFD and fails an assertion about a value that was written
correctly. Saying it makes the intent unambiguous at the one place that reads
non-ASCII output.

The assertion is the reason this cost an investigation rather than a glance.
`assert result["After"] == expected, result` renders its diff through the same
stdout under suspicion, so on a cp1252 console the EXPECTED side came back as
U+FFFD while the actual side round-tripped, and the report read as though the
correct half were the broken one. The raw log needed byte-level reading and a
caret count to establish which operand was mangled.

It now prints codepoints, which survive any code page, sliced from the common
prefix so the message stays short. It names a cause only when the bytes show
one: mojibake is the actual reading as expected's UTF-8 bytes back through a
single-byte code page, so that is tested by re-encoding through cp1252,
latin-1, cp437 and cp850 rather than pattern-matched. A genuine restore bug
prints the codepoints and no explanation, because explaining a real bug away as
an encoding artefact would be worse than saying nothing.

Deliberately not included: an earlier revision of this branch also prepended
install.ps1's UTF-8 preamble to pin the emit side. unslothai#10476's green run shows the
runners already emit UTF-8, so that was defence against a condition that does
not hold, and it is dropped rather than carried.

45 passed locally, kwarg-spacing check 62 passed, and both branches of the new
assertion exercised directly: the cp1252 mojibake case names the code page, a
substituted path does not.
danielhanchen added a commit that referenced this pull request Sep 8, 2026
…red (#10432)

* Make the venv-hardening decode strict, and say which codepoints differed

#10476 fixed the red Cross-platform parity run by decoding PowerShell's stdout
as UTF-8, and its own windows-latest run is green, so the transport bug itself
is closed. This is the part that failure exposed and that fix did not cover.

errors is strict. `encoding = "utf-8"` alone still defaults to errors="strict"
for subprocess text mode today, but the default is not the contract, and the
cost of being wrong here is the exact failure #10476 just fixed: an undecodable
byte becomes U+FFFD and fails an assertion about a value that was written
correctly. Saying it makes the intent unambiguous at the one place that reads
non-ASCII output.

The assertion is the reason this cost an investigation rather than a glance.
`assert result["After"] == expected, result` renders its diff through the same
stdout under suspicion, so on a cp1252 console the EXPECTED side came back as
U+FFFD while the actual side round-tripped, and the report read as though the
correct half were the broken one. The raw log needed byte-level reading and a
caret count to establish which operand was mangled.

It now prints codepoints, which survive any code page, sliced from the common
prefix so the message stays short. It names a cause only when the bytes show
one: mojibake is the actual reading as expected's UTF-8 bytes back through a
single-byte code page, so that is tested by re-encoding through cp1252,
latin-1, cp437 and cp850 rather than pattern-matched. A genuine restore bug
prints the codepoints and no explanation, because explaining a real bug away as
an encoding artefact would be worse than saying nothing.

Deliberately not included: an earlier revision of this branch also prepended
install.ps1's UTF-8 preamble to pin the emit side. #10476's green run shows the
runners already emit UTF-8, so that was defence against a condition that does
not hold, and it is dropped rather than carried.

45 passed locally, kwarg-spacing check 62 passed, and both branches of the new
assertion exercised directly: the cp1252 mojibake case names the code page, a
substituted path does not.

* Point the mojibake hint at the script, not the transport

The hint said the shell's stdout had been read with the Windows ANSI code page
and that the script did not lose the path. That is backwards now: _run_powershell
decodes stdout as strict UTF-8, so a transport mis-decode raises there and never
reaches this helper. Mojibake that does reach it was produced inside the script,
which is the product defect these cases exist to catch, and a wrong explanation on
a real restore bug costs more than none.

Same byte test, opposite conclusion, plus where to look: a missing -Encoding utf8
on the marker read/write path.

45 tests in the file pass, and the rendered message on a seeded cp1252 round trip
now reads 'the mis-decode happened inside the script, not in transport'.
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.

1 participant