Skip to content

Preserve UTF-8 across terminal output chunks - #1741

Open
VIOLETee0 wants to merge 3 commits into
CodebuffAI:mainfrom
VIOLETee0:fix/terminal-output-utf8-chunks
Open

VIOLETee0 wants to merge 3 commits into
CodebuffAI:mainfrom
VIOLETee0:fix/terminal-output-utf8-chunks

Conversation

@VIOLETee0

@VIOLETee0 VIOLETee0 commented Nov 27, 2026 •

Copy link
Copy Markdown

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

  • 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.

@codebuff-team

Copy link
Copy Markdown
Contributor

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.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants