Skip to content
This repository was archived by the owner on Sep 23, 2026. It is now read-only.

fix(print): handle unsupported stdout encodings - #2657

Open
dvd233 wants to merge 2 commits into
MoonshotAI:mainfrom
dvd233:fix/print-output-encoding-2629
Open

dvd233 wants to merge 2 commits into
MoonshotAI:mainfrom
dvd233:fix/print-output-encoding-2629

Conversation

@dvd233

@dvd233 dvd233 commented Sep 20, 2026 •

Copy link
Copy Markdown

Fixes #2629

Problem

Print mode writes streamed messages directly to stdout. On Windows consoles using a legacy encoding such as GBK, characters outside that encoding raise UnicodeEncodeError and terminate the command.

Fix

Sanitize print-mode output according to stdout's encoding before writing it. Rich text output uses the same safe stream wrapper, so text and JSON output both replace unsupported characters instead of crashing.

Verification

  • Reproduced the reported failure with a strict GBK stdout stream.
  • Added regression coverage for text and JSON printers.
  • uv run pytest tests/ui_and_conv/test_print_encoding.py tests/ui_and_conv/test_print_final_only.py tests/ui_and_conv/test_print_notifications.py tests/ui_and_conv/test_print_exit_code.py tests/ui_and_conv/test_replay.py -q
  • uv run ruff check
  • uv run ruff format --check
  • Targeted pyright passes.

Devin Review

Copilot AI lite review requested due to automatic review settings September 20, 2026 22:18

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@devin-ai-integration devin-ai-integration 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.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment thread src/kimi_cli/ui/print/visualize.py Outdated
Comment on lines +65 to +67
def _safe_print(text: str) -> None:
"""Print text while replacing characters unsupported by stdout's encoding."""
builtins.print(_encode_for_stream(text, sys.stdout), flush=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.

🔴 Command echo bypasses safe output

With unsupported command characters, _safe_print does not protect the initial echo. Print.run writes directly, so legacy Windows stdout still aborts before execution.

Learn more

The new encoding-safe path covers messages emitted by the visualizers. Standard non-final text mode first echoes the user's command through a separate direct print call in Print.run. That write occurs before run_soul invokes any printer, so an encoding failure ends the command before the protected code runs. Other direct user-facing writes in Print.run can fail for the same reason when exception text contains unsupported characters.

Example: On a GBK stdout, run non-final text mode with the command explain 🧪. The command echo raises UnicodeEncodeError; no model request runs, although model output now passes through _safe_print.

Recommended fix: Put the safe stream or safe-write helper in a shared print-UI module and route every stdout write in Print.run through it. Add an integration-level regression test covering the command echo, rather than testing only TextPrinter and JsonPrinter.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

This branch has not been deployed

No deployments
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.

UnicodeEncodeError: 'gbk' codec can't encode character '\u0133' in position 1559

2 participants