Skip to content

fix(automations): decode cmd.exe precheck output in the Windows OEM code page - #50960

Merged
nwparker merged 1 commit into
stablyai:mainfrom
Pr1p:fix/precheck-windows-output-codepage
Nov 27, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
Pr1p:fix/precheck-windows-output-codepage

Conversation

@Pr1p

@Pr1p Pr1p commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

On Windows, an automation precheck runs through cmd.exe. When cmd.exe prints its own error, for example "this command does not exist", it uses the legacy code page for the system language instead of UTF-8. Orca read everything as UTF-8. On Chinese, Japanese, Korean, Russian, Thai or Vietnamese Windows, the message that explains why a precheck skipped a run came out as garbage. Orca now decodes those lines in the right code page.

What Changed

  • Before: runLocalPrecheck called setEncoding('utf8') on stdout/stderr. When cmd.exe wrote a message in the OEM page (GBK, Shift_JIS, etc.), the stored precheck stderr was 'x' ����.
  • After: that line reads 'x' 中文… (the localized cmd.exe message). Lines that are valid UTF-8, such as node or git output, look the same as before.
  • Mechanism:
    • src/main/automations/dispatch/precheck-runner.ts keeps a bounded raw byte tail for each stream with the existing createOutputSink(…, 'tail'). The cap is 4 bytes per allowed character. The runner decodes once at settle and then applies the existing character cap. When the tail was truncated, the runner first drops up to 3 leading UTF-8 continuation bytes, so a character cut by the cap does not push its whole line into the OEM decoder.
    • New src/main/windows/windows-shell-output-decoding.ts, with decodeWindowsShellOutput(bytes):
      • Output that is all valid UTF-8 returns bytes.toString('utf8') straight away and never loads the registry addon.
      • Otherwise, on win32, it reads HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage\OEMCP once per process (cached, failures included) through the existing loadWindowsNativeRegistry. Then it decodes each line as UTF-8 when the line is valid, and with the OEM decoder when it is not. It works per line because one pipe can carry both, for example nosuchcmd || node ….
      • A UTF-8 character cut at the very end (timeout kill, block flush) still counts as UTF-8 and shows one U+FFFD, as StringDecoder did before.
    • Supported OEM pages: 866, 874, 932, 936, 949, 950, 1258. Off Windows, without the addon (server slots), or on any other page, output decodes as UTF-8 as before. SSH prechecks are unchanged.

Why

Adding chcp 65001 or chcp 936 in front of the command does not help. Probed on a Windows host: cmd.exe still writes its own messages to the pipe in OEMCP, and the bytes do not change. Decoding on Orca's side is the only fix that does not depend on what the user's command prints.

Linked Issue

Fixes #50959

Visual Proof

N/A. The change is in the text stored for a precheck result. It only shows up on Windows with a CJK/Cyrillic/Thai/Vietnamese OEM code page, and no such GUI host was available (changing a system locale needs a reboot). The before/after strings are in What Changed and Testing.

Testing

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Every added test fails without this change and avoids the test anti-patterns

  • Windows host (OEMCP 437, node 24): a probe of spawn(cmd, { shell: true }) with nosuchcmd_é showed cmd.exe writing 0x82 (é in cp437). main decodes that to 'nosuchcmd_�' is not recognized…. Putting chcp 936/chcp 65001 in front did not change the bytes.

  • Contributor (Windows 10 19045, OEMCP 936): ran the real runAutomationPrecheck with nosuchcmd_xyz || node -e …. On main, stderr was U+FFFD garbage. With the fix, it is the localized "not recognized" message, and node's stdout stays readable.

  • Scratch test with the real runAutomationPrecheck: OEMCP 936 mocked, win32 set at settle, a child writing one GBK line and one UTF-8 line. main fails. This branch passes, including a truncated 中×10000 tail with suffixes ''/a/ab, which stays all 中.

  • Electron ICU (Electron 43, ICU 78): ibm866, windows-874, shift_jis, gbk, euc-kr and big5 all construct and decode. If a page is not supported, the TextDecoder constructor throws and the catch falls back to UTF-8.

  • windows-shell-output-decoding.test.ts (codec test):

    • It covers a mixed GBK cmd.exe line, a UTF-8 line and an unterminated tail.
    • It also covers a UTF-8 line cut mid-character at the end, alone and after a GBK line.
    • It fails on main because the module does not exist. The cut-character cases fail when the trailing-cut handling is removed (mutation checked).
  • vitest run src/main/windows/ src/main/automations/ bounded-output-sink.test.ts: 14 files passed, 43 tests.

  • oxlint, oxfmt, anti-slop oxlint, native code-quality oxlint, ast-grep and the changed-code quality gate all pass on the touched files.

  • macOS/Linux behavior is unchanged: decoding stays UTF-8.

Performance

Cold path: main process, once per local automation precheck. No renderer, store, polling or terminal-stream changes, and no new subprocesses. The registry read is one cached sync read and only happens when output is not valid UTF-8 on win32. Benchmarks (node 24) compare old and new:

  • Decode at settle, per stream:
    • Typical 200 B ASCII: 0.0004 ms.
    • Dir-like 16 KB GBK: 0.09 ms.
    • Adversarial 8000 invalid 1-byte lines: 1.2 ms.
    • Adversarial 16000 invalid bytes on one line: 0.2 ms.
    • Everything stays well under 16 ms.
  • Ingest with createOutputSink vs the previous string tail:
    • Typical: 0.0003 ms.
    • 1 B × 200k flood: 5.5 ms vs 33 ms on main.
    • 64 KB × 1600 (100 MB): about 2–3 ms.
    • Max event-loop gap is about 2 ms, the same as main.

Risk

Level: Low

  • Who is affected: only Windows machines with OEM page 866/874/932/936/949/950/1258, and only for lines that are not valid UTF-8. Those lines were already garbled on main.
  • Trade-off 1: a line that is mostly UTF-8 but has one stray invalid byte in the middle now decodes entirely in the OEM page instead of showing one U+FFFD. Such output is already corrupt. A character cut at either end of the tail is handled.
  • Trade-off 2: Russian tools that write ANSI cp1251 now show wrong Cyrillic letters instead of U+FFFD. That output was unreadable on main too.
  • Not covered: TextDecoder has no cp437/cp850, so accented letters in cmd.exe messages on Western-European OEM pages stay garbled, as on main.
  • Back out: plain revert.

Review

  • Possible follow-ups, out of scope here: the SSH precheck path, and ephemeral-vm-recipe-process.ts, which has the same shell: true + UTF-8 pattern.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

macOS/Linux and SSH prechecks keep the UTF-8 decode. Older remote runtimes are not involved: this runs only on the local main process.

Original fix by @Pr1p in #50960. Maintainer review changes are squashed into this commit, and Chen is kept as the author.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

Local precheck stdout and stderr are retained as bounded byte buffers and decoded when the process settles. On Windows, the decoder uses the OEM code page when available and decodes valid UTF-8 lines as UTF-8. It uses UTF-8 decoding when no OEM decoder is available. The character cap applies after decoding. Tests cover mixed GBK and UTF-8 output and UTF-8 output cut off mid-character.

Severity of issue fixed: Medium

Merge Risk: Low · up to 8616c

Windows precheck output is now decoded with the OEM code page where possible, which fixes the garbled text. In one narrow case, an unterminated final line ending in a byte that is not a valid UTF-8 start can still show a replacement character instead of the OEM text. This is cosmetic and low risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed Issue #50959 requires three things. First, cmd.exe messages from local Windows prechecks must decode in the OEM code page. Second, UTF-8 tool output must stay readable. Third, the change must not alte…
Out of Scope Changes check Passed All changed code supports the decoding objective of #50959. The changes are: the new windows-shell-output-decoding module, the byte-tail buffering and single decode in runLocalPrecheck, and the new un…
Title check Passed The title names the main change: cmd.exe precheck output is now decoded in the Windows OEM code page. It matches the changeset, which adds OEM decoding for local Windows precheck output. The title fol…
Description check Passed The description follows the repository template. It includes ELI5, What Changed, Why, Linked Issue (Fixes #50959), Visual Proof (N/A with a reason), Testing with platform details, Performance, Risk wi…
Full details: Linked Issues check

Explanation

Issue #50959 requires three things. First, cmd.exe messages from local Windows prechecks must decode in the OEM code page. Second, UTF-8 tool output must stay readable. Third, the change must not alter other paths. At head 8616ce7, runLocalPrecheck (src/main/automations/dispatch/precheck-runner.ts) collects raw byte tails through createOutputSink. It calls decodeByteTail once in settle, which calls decodeWindowsShellOutput. decodeWindowsShellOutput (src/main/windows/windows-shell-output-decoding.ts) reads OEMCP from the registry once per process. It maps the code page to a TextDecoder encoding. decodeMixedShellOutputLines decodes each LF-delimited line as UTF-8 when valid, and otherwise with the OEM decoder. This handles a pipe that carries both a GBK cmd.exe line and a UTF-8 node line. A final line cut mid-character is checked without treating the partial character as invalid. The SSH path (runSshChannelPrecheck) still decodes as UTF-8. Non-Windows platforms return bytes.toString('utf8'). The PR reports a manual before/after run on zh-CN (OEM 936). Its stderr changes from U+FFFD to the localized message. The changes since the previous review add windows-1258 to the mapping and the cut-character handling in decodeByteTail. Both keep the same behavior for the reported case. Limit: the new test file was not read in this review. Test behavior is taken from the summary and the PR description.

Full details: Out of Scope Changes check

Explanation

All changed code supports the decoding objective of #50959. The changes are: the new windows-shell-output-decoding module, the byte-tail buffering and single decode in runLocalPrecheck, and the new unit test file. The byte cap of four bytes per character supports the existing character cap. The mid-character cut handling in decodeByteTail and isUtf8BeforeCutCharacter prevents a split character from turning a valid UTF-8 line into OEM text. This supports the UTF-8 readability requirement. The entry for windows-1258 is a minor addition. It is a Windows code page, not a true OEM page, and the PR description does not list it. It is in the same locale mapping and does not change other behavior, so it is not an unrelated change. The SSH path and ephemeral-vm recipe path are not changed. The PR description states that the ephemeral-vm recipe path was left out on purpose. The PR description should be updated to list 1258 in the supported pages.

Full details: Title check

Explanation

The title names the main change: cmd.exe precheck output is now decoded in the Windows OEM code page. It matches the changeset, which adds OEM decoding for local Windows precheck output. The title follows the conventional commit form and is 77 characters long, which is within a reasonable range for a title.

Full details: Description check

Explanation

The description follows the repository template. It includes ELI5, What Changed, Why, Linked Issue (Fixes #50959), Visual Proof (N/A with a reason), Testing with platform details, Performance, Risk with level and back-out plan, Review, Agent skill boundary, Notes, and Checklist. The AI Disclosure section is left blank, which the template allows for internal contributors, and the author has not stated whether it applies. The Risk section says how to back out the change. Remaining gaps are minor: the Performance section explains the trade-off, and the Testing section names the platforms tested.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7847dcb9-f127-42de-8378-f22903b4adaa
Commits

Reviewing files that changed from the base of the PR and between eafbf1a and 5fe4ebb.

Files selected for processing: 3

  • src/main/automations/dispatch/precheck-runner.ts
  • src/main/windows/windows-shell-output-decoding.test.ts
  • src/main/windows/windows-shell-output-decoding.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

const bytes = Buffer.concat([tail.bytes, chunk])
return bytes.length <= MAX_PRECHECK_OUTPUT_BYTES
? { ...tail, bytes }
: { bytes: bytes.subarray(-MAX_PRECHECK_OUTPUT_BYTES), truncated: true }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Preserve encoding boundaries when trimming the byte tail.

On Windows with OEMCP 936, UTF-8 output containing '中'.repeat(6000) occupies 18000 bytes. This cut starts the retained buffer inside a character. The strict UTF-8 decoder then throws, so decodeMixedShellOutputLines decodes the entire retained line as GBK. The final 4000-character slice remains unreadable. Fatal decoding throws on invalid input as documented for Node 24. (nodejs.org)

Preserve boundary context when trimming so the cut cannot change the retained line's encoding selection. Keep genuine OEM fallback working. Add a regression case through byte-tail collection and final decoding.

Based on learnings, validate complete encoding units rather than arbitrary byte fragments.

Source: Learnings

On Windows, local automation prechecks run through cmd.exe, which writes
its own messages (such as "is not recognized as an internal or external
command") to the pipe in the OEM code page. The runner decoded every byte
as UTF-8, so on zh-CN, ja, ko, ru, th and vi Windows the stored precheck
output, which tells the user why a run was skipped, showed U+FFFD garbage.

The runner now keeps bounded raw byte tails per stream (createOutputSink)
and decodes once at settle. decodeWindowsShellOutput reads OEMCP from the
registry once per process and decodes each line as UTF-8 when it is valid,
otherwise with the OEM decoder, because one pipe can mix UTF-8 tool output
with cmd.exe messages. A UTF-8 character split by the byte cap at the start
or by a kill at the end stays UTF-8. Off Windows, without the registry
addon, or for unsupported pages, output is decoded as UTF-8 as before. SSH
prechecks are unchanged.

Fixes stablyai#50959

Co-authored-by: Neil <[email protected]>
@nwparker
nwparker force-pushed the fix/precheck-windows-output-codepage branch from 5fe4ebb to 8616ce7 Compare November 27, 2026 15:55
@nwparker nwparker changed the title fix(automations): decode cmd.exe precheck output in the OEM code page fix(automations): decode cmd.exe precheck output in the Windows OEM code page Nov 27, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 41813f70-94b1-403f-acea-d4947303dde9
Commits

Reviewing files that changed from the base of the PR and between 5fe4ebb and 8616ce7.

Files selected for processing: 3

  • src/main/automations/dispatch/precheck-runner.ts
  • src/main/windows/windows-shell-output-decoding.test.ts
  • src/main/windows/windows-shell-output-decoding.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

for (let length = 1; length <= Math.min(3, bytes.length); length++) {
const byte = bytes[bytes.length - length]
if ((byte & 0xc0) !== 0x80) {
const characterLength = byte >= 0xf0 ? 4 : byte >= 0xe0 ? 3 : byte >= 0xc0 ? 2 : 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Reject impossible UTF-8 lead bytes before treating a suffix as cut.

On Windows with OEMCP 866, Buffer.from([0x32, 0x30, 0xf8]) represents the unterminated text 20°. The IBM866 mapping assigns 0xf8 to °. (encoding.spec.whatwg.org)

Line 40 treats 0xf8 as a four-byte UTF-8 lead. The helper then validates only 20, so the exported decoder bypasses OEM decoding and returns 20�.

Only accept a suffix that can actually begin a valid UTF-8 character. Reject impossible leads such as 0xf8 and invalid continuation combinations. This preserves the valid E4 B8 cutoff case while allowing OEM fallback for this input. UTF-8 lead bytes cannot exceed 0xf4. (encoding.spec.whatwg.org)

@nwparker
nwparker merged commit 3381099 into stablyai:main Nov 27, 2026
55 of 56 checks passed
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]: Automation precheck output from cmd.exe is mojibake on non-English Windows

2 participants