Skip to content

Fix: Improve text encoding for shell output in VSCode preview (#6178) - #6182

Merged
etraut-openai merged 10 commits into
openai:mainfrom
LIHUA919:fix/text-encoding-6178
Nov 20, 2025
Merged

etraut-openai merged 10 commits into
openai:mainfrom
LIHUA919:fix/text-encoding-6178

Conversation

@LIHUA919

@LIHUA919 LIHUA919 commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

🐛 Problem

Users running commands with non-ASCII characters (like Russian text "пример") in Windows/WSL environments experience garbled text in VSCode's shell preview window, with Unicode replacement characters (�) appearing instead of the actual text.

Issue: #6178

🔧 Root Cause

The issue was in StreamOutput<Vec<u8>>::from_utf8_lossy() method in codex-rs/core/src/exec.rs, which used String::from_utf8_lossy() to convert shell output bytes to strings. This function immediately replaces any invalid UTF-8 byte sequences with replacement characters, without attempting to decode using other common encodings.

In Windows/WSL environments, shell output often uses encodings like:

  • Windows-1252 (common Windows encoding)
  • Latin-1/ISO-8859-1 (extended ASCII)

🛠️ Solution

Replaced the simple String::from_utf8_lossy() call with intelligent encoding detection via a new bytes_to_string_smart() function that tries multiple encoding strategies:

  1. UTF-8 (fast path for valid UTF-8)
  2. Windows-1252 (handles Windows-specific characters in 0x80-0x9F range)
  3. Latin-1 (fallback for extended ASCII)
  4. Lossy UTF-8 (final fallback, same as before)

📁 Changes

New Files

  • codex-rs/core/src/text_encoding.rs - Smart encoding detection module
  • codex-rs/core/tests/suite/text_encoding_fix.rs - Integration tests

Modified Files

  • codex-rs/core/src/lib.rs - Added text_encoding module
  • codex-rs/core/src/exec.rs - Updated StreamOutput::from_utf8_lossy()
  • codex-rs/core/tests/suite/mod.rs - Registered new test module

✅ Testing

All tests pass:

cargo test -p codex-core text_encoding
cargo test -p codex-core test_shell_output_encoding_issue_6178

🎯 Impact

  • ✅ Eliminates garbled text in VSCode shell preview for non-ASCII content
  • ✅ Supports Windows/WSL environments with proper encoding detection
  • ✅ Zero performance impact for UTF-8 text (fast path)
  • ✅ Backward compatible - UTF-8 content works exactly as before
  • ✅ Handles edge cases with robust fallback mechanism

🧪 Test Scenarios

The fix has been tested with:

  • Russian text ("пример")
  • Windows-1252 quotation marks (""test")
  • Latin-1 accented characters ("café")
  • Mixed encoding content
  • Invalid byte sequences (graceful fallback)

📋 Checklist

  • Addresses the reported issue
  • Includes comprehensive tests
  • Maintains backward compatibility
  • Follows project coding conventions
  • No breaking changes

…#6178)

   Replace String::from_utf8_lossy() with intelligent encoding detection
   in StreamOutput::from_utf8_lossy() to fix garbled text in VSCode shell
   preview when running commands with non-ASCII characters in Windows/WSL.

   The new bytes_to_string_smart() function tries multiple encoding strategies:
   1. UTF-8 (fast path for valid UTF-8)
   2. Windows-1252 (common in Windows environments)
   3. Latin-1 (fallback for extended ASCII)
   4. Lossy UTF-8 (final fallback)
@github-actions

github-actions Bot commented Nov 4, 2025 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@etraut-openai

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull 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".

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 thread codex-rs/core/src/text_encoding.rs Resolved
@etraut-openai

Copy link
Copy Markdown
Contributor

Thanks for the contribution!

Please sign the CLA, fix the CI failures (looks like a simple formatting issue), and take a look at the codex code review feedback.

@etraut-openai etraut-openai added the needs-response Additional information is requested label Nov 4, 2025
@LIHUA919

LIHUA919 commented Nov 4, 2025

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 4, 2025
   Apply cargo fmt to fix formatting issues:
   - Reorder module declarations alphabetically
   - Fix line length and spacing in text_encoding.rs
   - Add newline at end of file
  Allow ESC (\x1b) character in Windows-1252 and Latin-1 detection
  to support colored shell output with ANSI escape sequences.

  This addresses the code review feedback that typical shell output
  embeds ANSI colour sequences alongside Windows-1252 characters,
  and the previous strict control character check was causing these
  to fall back to lossy UTF-8 conversion.

@joshka-oai joshka-oai 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.

General approval with the approach and code changes from a visual inspection. I haven't yet tested this however, but will later this week.

Comment thread codex-rs/core/src/text_encoding.rs Resolved
Comment thread codex-rs/core/src/text_encoding.rs Resolved
@etraut-openai etraut-openai removed the needs-response Additional information is requested label Nov 5, 2025
youta7 added a commit to youta7/ta-codex that referenced this pull request Nov 10, 2025
We already have encoding_rs in our dependency list, and it's a widely
used package (275M downloads). Chardetng is by the same developer.
@joshka-oai

Copy link
Copy Markdown
Contributor

@codex review

I've modified this to use encoding_rs and chardetng (another crate by the same developer as encoding_rs that detects the character sets). Codex rewrote most of the unit tests and added some explicit handling for codepage 866 / Windows 1252 oddness - I tried to get it to find a canonical link to some page that documents the problem it was solving here, but it could only really say that there was a problem and point at the two codepages as evidence. The logic seems reasonable here though. I'm not an expert however on those things.

@LIHUA919 @nos1609 would you mind taking a quick look at this. I can see that unit tests are reasonably detailed and seemingly cover the previous concerns, but this is one of those places where the real world tends to have some edge cases that are difficult to anticipate.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull 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".

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 thread codex-rs/core/src/text_encoding.rs Resolved
@etraut-openai etraut-openai added the needs-response Additional information is requested label Nov 18, 2025
@joshka-oai

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull 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".

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 thread codex-rs/core/src/text_encoding.rs Resolved
@joshka-oai

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull 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".

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 +33 to +46
// is to only coerce IBM866 to Windows-1252 when (a) the high bytes are exclusively the punctuation
// values listed below and (b) we spot adjacent ASCII. This targets the real failure case without
// clobbering legitimate Cyrillic text. If another code page has a similar collision, introduce a
// dedicated allowlist (like this one) plus unit tests that capture the actual shell output we want
// to preserve. Windows-1252 byte values for smart punctuation.
const WINDOWS_1252_PUNCT_BYTES: [u8; 8] = [
0x91, // ‘ (left single quotation mark)
0x92, // ’ (right single quotation mark)
0x93, // “ (left double quotation mark)
0x94, // ” (right double quotation mark)
0x95, // • (bullet)
0x96, // – (en dash)
0x97, // — (em dash)
0x99, // ™ (trade mark sign)

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.

P1 Badge Add missing CP1252 punct bytes

Right now WINDOWS_1252_PUNCT_BYTES only contains {0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x99}. That still leaves other CP1252-only punctuation (e.g. the ellipsis … at 0x85, single/double low quotes at 0x82/0x84, dagger 0x86, etc.) outside the allowlist. The chardetng issue you are working around happens for any byte in the 0x80–0x9F window once the detector guesses IBM866, so inputs such as b"\x85 test" (VSCode rendering … test) still come back as "Е test" because the heuristic refuses to flip the encoding away from IBM866. That’s the same garbled output we set out to fix. Please extend the allowlist (and add a regression test) so these common characters also coerce to Windows-1252 instead of slipping through as Cyrillic.

Useful? React with 👍 / 👎.

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.

I'm going to push back against this one for now - we could chase all the various characters here. Let's wait for an actual problem to occur to re-review which characters should be in this list.

@nos1609

nos1609 commented Nov 18, 2025 •

Copy link
Copy Markdown

@joshka-oai The only doubt actually from my point of view is the lack of Windows 1251 codepage. The more kind-of "universal" would be to handle the Linux's LatArCyrHeb approch to cover non Latin-charaters output, especially cyrillic ones.

The Windows 1252 looks like not covering the ArCyrHeb segments of the caracter maps.

Just a small "hello world" example
The word "Привет" in Windows 1252 would be rendered as "Привет" which is not really human-legible as "Hello" because of lack of cyrillic characters from Windows 1251.

https://learn.microsoft.com/windows/win32/intl/code-page-identifiers

PS if got it right, this covers the case
https://github.com/openai/codex/pull/6182/files#diff-6603ce56d5786a455399c6dd3791b120a4e22c65ae19e0f924d1f8a4901cd60cR132-R144

@joshka-oai

Copy link
Copy Markdown
Contributor

@nos1609 disregard the main PR comment here - I got codex to reimplement this from the manual detection into a more automated one using https://crates.io/crates/chardetng and https://crates.io/crates/encoding_rs. These should handle detecting 1251 and a large variety of other codepages. I'll add your test here as a new unit test to make sure that scenario is covered correctly though.


To test this in your VS Code extension:

Rebuild codex locally (cargo build) and edit your settings.json file to point to it. Something like this (adjust the path as appropriate):

"chatgpt.cliExecutable": "/Users/<user name>/code/codex/codex-rs/target/debug/codex",

Then when you restart the extension, it will use this CLI executable. You'll see a little orange icon in the lower right corner of the extension panel to remind you that you're using a custom CLI. Make sure to remove this when you're done testing to ensure that you get up to date versions of the CLI when updating the extention.

@etraut-openai etraut-openai removed the needs-response Additional information is requested label Nov 20, 2025
@etraut-openai
etraut-openai merged commit 397279d into openai:main Nov 20, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants