Skip to content

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

Merged
danielhanchen merged 2 commits into
mainfrom
fix/windows-parity-pwsh-stdout-utf8
Sep 8, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/windows-parity-pwsh-stdout-utf8

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Rebased and narrowed. #10476 landed the decode fix and its parity (windows-latest) is green, so the transport bug is closed and this PR no longer carries it. What remains is the part that failure exposed and that fix did not cover.

errors = "strict"

encoding = "utf-8" on its own already defaults to errors="strict" in subprocess text mode today, so this changes no behaviour. But the default is not the contract, and the cost of being wrong here is precisely the failure #10476 just fixed: an undecodable byte becomes U+FFFD and fails an assertion about a value that was written correctly. Worth stating at the one helper in this file that reads non-ASCII output.

The assertion

This is the substantive half. The old line was:

assert result["After"] == expected, result

which renders its diff through the same stdout under suspicion. On a cp1252 console the expected side came back as U+FFFD while the actual side round-tripped, so the report read as though the correct half were the broken one. Establishing which operand was mangled took byte-level reading of the raw log plus a caret count: one caret under expected, two under actual.

It now prints codepoints, which survive any code page, sliced from the common prefix so the message stays short:

the rollback did not restore the marker it saved
  first 56 characters match, then:
    actual   'A~A€ffee cache'  [U+00C3 U+00A4 U+0066 U+0066 U+0065 U+0065 ...]
    expected 'affee cache'   [U+00E4 U+0066 U+0066 U+0065 U+0065 ...]
  actual is expected's UTF-8 bytes decoded as a single-byte code page, i.e. this shell's
  stdout was read with the Windows ANSI code page. The script did not lose the path.

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/cp850 rather than pattern-matched. A genuine restore bug prints the codepoints and no explanation:

the rollback did not restore the marker it saved
  first 55 characters match, then:
    actual   'WRONG cache'   [U+0057 U+0052 U+004F U+004E U+0047 ...]
    expected 'kaffee cache'  [U+006B U+00E4 U+0066 U+0066 U+0065 U+0065 ...]

Explaining a real restore bug away as an encoding artefact would be worse than saying nothing, which is why the claim is earned rather than asserted.

Deliberately dropped

The earlier revision of this branch also prepended install.ps1's UTF-8 preamble to _run_powershell, pinning the emit side so the result would not depend on the runner's console code page. #10476's green run shows the runners already emit UTF-8, so that was defence against a condition that does not hold. Dropped rather than carried.

Verification

  • pytest tests/python/test_windows_python_venv_hardening.py -> 45 passed
  • pytest tests/test_enforce_kwargs_spacing.py -> 62 passed
  • no line over the repo's 119 limit
  • both branches of the new assertion exercised directly: the cp1252 mojibake case names the code page, a substituted path does not

Net delta against main is 50 insertions, 1 deletion, one file.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 7, 2026 •

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 🔄 Running since 2026-09-08T06:06:37.149898Z 66dc5dc Manual request
ℹ️ 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.

#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.
@danielhanchen
danielhanchen force-pushed the fix/windows-parity-pwsh-stdout-utf8 branch from e8db458 to a608876 Compare September 8, 2026 05:20
@danielhanchen danielhanchen changed the title Read PowerShell's stdout as UTF-8, not as the Windows ANSI code page Make the venv-hardening decode strict, and say which codepoints differed Sep 8, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a608876ab9

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +101 to +105
if _looks_like_a_mis_decode(actual[head:], expected[head:]):
lines.append(
" actual is expected's UTF-8 bytes decoded as a single-byte code page, i.e. this "
"shell's stdout was read with the Windows ANSI code page. The script did not lose "
"the path."

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid blaming stdout for marker corruption

If Write-StudioUvCacheMarker regresses to reading the BOM-less marker with Windows PowerShell's ANSI codec—the exact product failure this test covers—the restored value is also expected.encode("utf-8").decode("cp1252"). This predicate therefore matches both transport corruption and corruption inside the script, then misleadingly states that the script preserved the path. Since _run_powershell now decodes stdout as strict UTF-8, the helper cannot infer where the mojibake occurred from the final strings alone; keep the codepoint diff but do not claim a transport-only cause without raw-byte evidence.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and the conclusion was backwards rather than merely unprovable. _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 defect these cases exist to catch. Fixed in 66dc5dc: same byte test, opposite conclusion, plus where to look (a missing -Encoding utf8 on the marker read/write path). Rendered on a seeded cp1252 round trip it now reads 'the mis-decode happened inside the script, not in 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'.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 66dc5dca46

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 6dcfd43 into main Sep 8, 2026
17 checks passed
@danielhanchen
danielhanchen deleted the fix/windows-parity-pwsh-stdout-utf8 branch September 8, 2026 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant