Skip to content

fix(process): keep multibyte characters intact across pipe reads - #50298

Merged
nwparker merged 1 commit into
stablyai:mainfrom
Afloat16:fix/batch-command-text-decoding
Nov 27, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
Afloat16:fix/batch-command-text-decoding

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When Orca runs a Windows .cmd/.bat command and reads what it prints, non-English characters sometimes came back as �. This happened when one character landed half in one piece of output and half in the next. Orca now holds the unfinished half until the rest arrives.

What Changed

  • Before: spawnCommandCapture turned each stdout/stderr Buffer chunk into a string separately (chunk.toString(encoding)). If a pipe read ended partway through a multibyte UTF-8 character, both halves decoded to U+FFFD, even though the child wrote valid UTF-8.
  • After: stdout and stderr each get their own StringDecoder (in the requested encoding, default utf-8). Each chunk goes through decoder.write(chunk), which keeps an incomplete trailing sequence for the next chunk. Both decoders are flushed with decoder.end() when capture settles, including on error, timeout and abort rejections, so the attached stdout/stderr are complete.
  • maxBuffer still counts raw bytes before decoding. Cancellation, timeout and listener cleanup are unchanged.

Why

commandExecFileAsync sends .cmd/.bat commands, and its Windows shim fallback, through spawnCommandCapture. Corrupted characters there can change JSON output, paths and diagnostics before the caller parses them. A streaming decoder fixes this without buffering all raw output until exit, and it is how Node's own stream setEncoding handles text.

Linked Issue

Fixes #50286

Original fix by @Afloat16 in #50298; final version reworked during the bug bash.

Visual Proof

N/A: this changes subprocess text capture, not any UI.

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

New spawn-command-capture.test.ts starts a real Node child that writes the first byte of é中😀 (stdout) and ø界🚀 (stderr), then the rest 50 ms later, so the first character is split across two pipe reads. It fails on main with U+FFFD and passes with this change. Extra probes, not committed, also checked that timeout rejections carry decoded partial output.

Ran on macOS: targeted vitest for process-capture/, oxlint, oxfmt, anti-slop, code-quality native plugins, ast-grep and the changed-code quality gate. StringDecoder is cross-platform Node, and this path only runs on Windows in production. The helper behaves the same on every OS, so the test runs everywhere.

Performance

None that matters, because this path is a one-shot capture for Windows batch shims, not a hot path (no terminal stream, polling or render path). Microbenchmark per chunk, Node 24, old toString vs new decoder.write:

  • 1 KB JSON: 0.18 µs → 0.14 µs
  • 64 KB ASCII: 3.6 µs → 7.0 µs
  • 64 KB all-multibyte (adversarial): ~26 µs → ~150 µs

The worst case is about 0.17 ms per 64 KB pipe read, spread across separate data events. That is far below the 16 ms stall budget. The only allocation is two small decoder objects per process. No new subprocesses, I/O or polling.

Risk

Level: Low. Only text decoding changes. Byte limits, error shapes and settlement are unchanged. A plain revert restores the old behavior.

Reported by @Afloat16

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

spawnCommandCapture now uses separate StringDecoder instances for stdout and stderr. It decodes chunks while preserving multibyte characters split across reads, then flushes both decoders when capture finishes. A test checks the complete captured output for both streams.

Severity of issue fixed: Medium

Merge Risk: Minimal · up to f7ee8

Multibyte output from captured commands is no longer corrupted when a character is split across pipe reads. No behavioral or performance risk is evident in the change, so it is ready to merge once CI passes.

Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed [#50286] The issue requires four coding behaviors in spawnCommandCapture. (1) Independent decoder state per stream: at head, stdoutDecoder and stderrDecoder are separate StringDecoder instances …
Out of Scope Changes check Passed The whole-PR diff against the merge base changes two files. spawn-command-capture.ts adds the StringDecoder import, the two per-stream decoders, the write calls that replace per-chunk toString…
Title check Passed The title "fix(process): keep multibyte characters intact across pipe reads" names the main change. The PR switches stdout and stderr capture in spawnCommandCapture to per-stream StringDecoder instanc…
Description check Passed The description follows the repository template and covers the required sections. It has an ELI5, a before/after account of What Changed, a Why section that explains the approach, a linked issue (Fixe…
Full details: Linked Issues check

Explanation

[#50286] The issue requires four coding behaviors in spawnCommandCapture. (1) Independent decoder state per stream: at head, stdoutDecoder and stderrDecoder are separate StringDecoder instances created before spawn, and onStdoutData/onStderrData each write to their own decoder. (2) Requested encoding: both decoders use options.encoding ?? 'utf-8'. (3) Flush on settlement: finish() calls end() on both decoders after cleanupListeners() and before the reject and resolve branches, so the error path also flushes. (4) Limits, errors, cancellation and cleanup: the maxBuffer byte counters are still checked before write, and the abort, timeout and cleanup code is unchanged in the whole-PR diff against the merge base. The PR description says there are two regression cases for exit 0 and exit 7. The head test file contains only one case, a success-path test with a split first byte and a 5-second timeout. No error-path test exists at head. The issue does not state a test requirement for the error path, so this does not block the verdict. The error-path flush is supported by source only, not by an automated test. Native Windows routing was not executed, as the issue also states.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes two files. spawn-command-capture.ts adds the StringDecoder import, the two per-stream decoders, the write calls that replace per-chunk toString, and the end() flush in finish. spawn-command-capture.test.ts is a new Vitest case for multibyte text split across pipe reads on stdout and stderr. Both changes support the objective of #50286. Command construction, process launching, SSH capture and Codex file polling are not changed. The head test file has one case, not the two described in the PR description, but the extra case in the description is not a code change, so it does not affect scope.

Full details: Title check

Explanation

The title "fix(process): keep multibyte characters intact across pipe reads" names the main change. The PR switches stdout and stderr capture in spawnCommandCapture to per-stream StringDecoder instances, so multibyte UTF-8 characters split across pipe reads decode correctly. The title is specific and matches the changeset and linked objectives.

Full details: Description check

Explanation

The description follows the repository template and covers the required sections. It has an ELI5, a before/after account of What Changed, a Why section that explains the approach, a linked issue (Fixes #50286), N/A for Visual Proof with a reason, a Testing section with checked items and a described regression test, a Performance line with measurements, and a Risk section with a level and revert note. Gaps: the Risk section does not name a specific revert path beyond a plain revert, the AI Disclosure section is left blank, the Review and Agent skill upstream boundary sections are not filled in, and the Notes checklist items are not addressed. The objectives also say the full gate did not pass locally, but the description checks the local testing boxes and does not state this. These are minor and non-critical, so the description is mostly complete.

  • 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

spawnCommandCapture decoded each stdout/stderr chunk on its own, so a
UTF-8 character split across two pipe reads became U+FFFD. Decode each
stream through its own StringDecoder and flush it when capture settles.
maxBuffer still counts raw bytes.

Fixes stablyai#50286

Co-authored-by: Neil <[email protected]>
@nwparker
nwparker force-pushed the fix/batch-command-text-decoding branch from 60e348e to f7ee80c Compare November 27, 2026 03:52
@nwparker nwparker changed the title fix(process): preserve multibyte command capture across pipe reads fix(process): keep multibyte characters intact across pipe reads 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.

Nitpick comments: 1

src/main/git/command-runner/process-capture/spawn-command-capture.test.ts: 1

9-9: Trivial · Maintainability & Code Quality · Quick win

Make the split-read test deterministic.

If the parent is delayed for more than 50 ms, it can receive both writes in one pipe read. The assertion can then pass with the old per-chunk decoder. Add a controlled-stream test that emits the partial character in separate data events. Keep the real-child test as a smoke test.

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: 46376bfa-a28c-421e-a395-f0d267155614
Commits

Reviewing files that changed from the base of the PR and between 60e348e and f7ee80c.

Files selected for processing: 2

  • src/main/git/command-runner/process-capture/spawn-command-capture.test.ts
  • src/main/git/command-runner/process-capture/spawn-command-capture.ts

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

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]: Windows command-shim capture corrupts multibyte output split across pipe reads

2 participants