You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Preserve UTF-8 across terminal output chunks - #1741
Decoding each stdout/stderr chunk separately corrupts UTF-8 characters split between chunks. This affects foreground tool output and brokered background log files.
Changes
Use an independent StringDecoder for each stream, flushing incomplete bytes at EOF without changing the stream encoding seen by other consumers. Preserve the current upstream first-output timing on the first raw chunk, even when that chunk is only part of a character. The branch incorporates main through fef9b86; background exit reporting and process ownership are retained.
Verification
The existing regression fails against current main and passes with this branch.
Terminal-related tests: 28 pass / 7 platform skips / 0 fail, including UTF-8 output, cancellation, process cleanup and timing.
A deterministic broker probe sends a partial character at 10 ms and completes it at 100 ms: output is correct and first_output_ms remains 10.
SDK typecheck passes with native TypeScript 7.0.2.
ESLint: 0 errors / 15 warnings. Whole-file Prettier reports checkout/source differences; normalized comparison introduces no new formatting differences.
SDK build is still blocked by the pre-existing declaration-generator ts.sys error; binary build and smoke were not run in this follow-up.
Scope / Tradeoffs
Existing regression unchanged; no new test cases added. Windows/Bun 1.3.14 local validation. POSIX-only background-exit tests remain skipped; no Linux/macOS CI claim.
Good find and a clean fix. The root cause is real: data.toString() per-chunk silently mangles any multi-byte UTF-8 character that straddles a chunk boundary, since Buffer.toString() has no memory of partial sequences. Using string_decoder's StringDecoder per stream is exactly the right tool here — it's the same mechanism Node's own Readable.setEncoding('utf8') uses internally, so this isn't exotic.
The change in run-terminal-command.ts is small and targeted: collectDecodedOutput wraps the existing data/end listener pattern, keeps the stream's raw Buffer semantics intact for other consumers (verified in the test via brokerChunks.every(Buffer.isBuffer)), and is applied consistently to both foreground stdout/stderr and the background-command log writer in startBackgroundCommand. That symmetry matters — fixing only one path would've left a mismatch between live and logged output.
The regression test is thorough: byte-by-byte writes to force splits, mixed 2/3/4-byte characters, an incomplete trailing byte to confirm the expected � replacement still happens at genuine truncation, and ANSI escape sequences left untouched around the decoded text. Running it against both SYNC and BACKGROUND process types is a nice touch since the two code paths diverge.
One thing worth checking before porting: collectDecodedOutput only flushes the decoder on 'end'. If a stream errors out or is destroyed without reaching EOF, any buffered partial bytes in the decoder are dropped silently rather than being flushed as-is. That was arguably true before too, so it's not a regression, but might be worth a comment or a stream.once('close', ...) fallback if it matters in practice.
Scope is clean — only sdk/src/tools/run-terminal-command.ts and a new test file, no forbidden paths touched.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem / Why
Decoding each stdout/stderr chunk separately corrupts UTF-8 characters split between chunks. This affects foreground tool output and brokered background log files.
Changes
Use an independent StringDecoder for each stream, flushing incomplete bytes at EOF without changing the stream encoding seen by other consumers. Preserve the current upstream first-output timing on the first raw chunk, even when that chunk is only part of a character. The branch incorporates main through fef9b86; background exit reporting and process ownership are retained.
Verification
Scope / Tradeoffs
Existing regression unchanged; no new test cases added. Windows/Bun 1.3.14 local validation. POSIX-only background-exit tests remain skipped; no Linux/macOS CI claim.