Skip to content

fix(shell): use EncodedCommand for PowerShell to fix Windows encoding issues - #11148

Merged
catrielmuller merged 3 commits into
Kilo-Org:mainfrom
senguangd:fix/powershell-encoding
Jun 12, 2026
Merged

fix(shell): use EncodedCommand for PowerShell to fix Windows encoding issues#11148
catrielmuller merged 3 commits into
Kilo-Org:mainfrom
senguangd:fix/powershell-encoding

Conversation

@senguangd

Copy link
Copy Markdown
Contributor

Issue

Fixes #4138
Fixes #2422
Fixes #2939

Also related to #9440, #8616

Context

On Windows, PowerShell commands containing or producing non-ASCII characters (Chinese, Japanese, Cyrillic, etc.) result in garbled output (mojibake). This happens because the console encoding defaults to the system OEM code page (e.g., CP936 for zh-CN) rather than UTF-8.

Multiple users have reported this issue:

All three issues were closed as stale/not-planned without a code fix. The underlying encoding problem remains.

Implementation

Switch PowerShell invocation from -Command to -EncodedCommand with a Base64-encoded UTF-16LE payload. The encoded payload explicitly sets [Console]::InputEncoding, [Console]::OutputEncoding, and $OutputEncoding to UTF-8 before executing the original command. This ensures both input and output are properly encoded as UTF-8 regardless of the system locale.

New file: packages/core/src/shell/powershell.ts

  • args(command) — returns PowerShell arguments using -EncodedCommand
  • encoded(command) — wraps the command in a UTF-8 console setup preamble and encodes as Base64 (UTF-16LE)

Modified: packages/opencode/src/shell/shell.ts

  • Import the new PowerShell module
  • Replace ["-NoProfile", "-Command", command] with PowerShell.args(command)

Modified: packages/opencode/src/tool/shell.ts

  • Replace hardcoded ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", command] with Shell.args(shell, command, cwd), reusing the same encoding logic

This approach is inspired by how VS Code's own shell execution handles PowerShell encoding (using -EncodedCommand to avoid encoding mismatches).

Screenshots

N/A, non-visual change. The fix affects command output encoding in the shell execution layer.

How to Test

Manual/local verification

  1. On a Windows machine with a non-English locale (e.g., zh-CN, ja-JP, ru-RU), launch Kilo Code
  2. Ask the agent to run a PowerShell command that produces localized output, e.g.:
    (Get-CimInstance -ClassName Win32_OperatingSystem).caption
    
  3. Before fix: output contains mojibake/garbled characters
  4. After fix: output displays correctly in the expected language

Reviewer test steps

  1. Check out this branch on a Windows machine
  2. Build and run Kilo Code
  3. Execute any command via the agent that triggers PowerShell (e.g., "What OS am I running?")
  4. Verify the output is correctly encoded (no mojibake)
  5. Also verify on an English-locale Windows machine that existing functionality is not broken

Checklist

  • Issue linked above, or exception explained
  • Tests/verification described
  • Screenshots/video included for visual changes, or marked N/A
  • // kilocode_change markers added for changes in packages/opencode/
  • I personally reviewed the diff and can explain the changes

Copilot AI review requested due to automatic review settings June 12, 2026 04:35

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR centralizes PowerShell command invocation and switches PowerShell execution to use -EncodedCommand (with a UTF-8 wrapper) instead of passing raw -Command strings.

Changes:

  • Added a core powershell.ts helper to generate PowerShell CLI args using -EncodedCommand.
  • Updated shell argument selection to delegate PowerShell arg construction to the new helper.
  • Updated Windows PowerShell process creation to reuse Shell.args(...) rather than hardcoding flags.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
packages/opencode/src/tool/shell.ts Uses Shell.args(...) for Windows PowerShell process creation instead of inline PowerShell flags.
packages/opencode/src/shell/shell.ts Routes PowerShell argument generation through @opencode-ai/core/shell/powershell.
packages/core/src/shell/powershell.ts Introduces PowerShell arg builder that encodes commands and enforces UTF-8 I/O settings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/opencode/src/kilocode/shell/shell.ts
Comment thread packages/opencode/src/shell/shell.ts Outdated
Comment thread packages/opencode/src/shell/shell.ts Outdated
Comment thread packages/opencode/src/tool/shell.ts Outdated
Comment thread packages/opencode/src/kilocode/shell/shell.ts
Comment thread packages/core/src/shell/powershell.ts Outdated
@kilo-code-bot

kilo-code-bot Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: 1 Observation (no blocking issues) | Recommendation: Merge

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 1
Previously Flagged Issues — All Resolved

All issues from prior reviews have been addressed:

Issue Status
Missing Buffer import in powershell.ts ✅ Fixed — import { Buffer } from "node:buffer" added
// kilocode_change start/end block markers in shell.ts ✅ Fixed — replaced with descriptive inline comment
Standalone // kilocode_change in shell.ts ✅ Fixed — folded into line with description
Standalone // kilocode_change in tool/shell.ts ✅ Fixed — folded into line with description
Leading newline in template literal (powershell.ts) ✅ Fixed — template literal now starts with [Console] directly
Fork hygiene — new file in upstream-shared packages/core/ ✅ Fixed — file moved to packages/opencode/src/kilocode/shell/shell.ts
No test coverage ✅ Fixed — packages/opencode/test/kilocode/shell/shell.test.ts added
Remaining Observation (not in diff)

Changeset missing: This PR fixes a real user-facing bug (garbled output on non-English Windows locales) and still has no .changeset/*.md entry. A patch changeset with a user-facing description like "Fix garbled PowerShell output on Windows with non-UTF-8 system locales" would be appropriate per AGENTS.md.

Files Reviewed (3 files)
  • packages/opencode/src/kilocode/shell/shell.ts — new file, moved from packages/core/src/shell/powershell.ts; all prior issues resolved
  • packages/opencode/src/shell/shell.ts — all prior issues resolved
  • packages/opencode/src/tool/shell.ts — all prior issues resolved
  • packages/opencode/test/kilocode/shell/shell.test.ts — new test file added

Reviewed by claude-4.6-sonnet-20260217 · 676,910 tokens

Review guidance: REVIEW.md from base branch main

… issues

On Windows, PowerShell commands with non-ASCII characters (e.g., Chinese, Japanese, Cyrillic) produce garbled output (mojibake) because the console encoding defaults to the system OEM code page rather than UTF-8.

Switch from `-Command` to `-EncodedCommand` with a Base64-encoded UTF-16LE payload that explicitly sets `[Console]::InputEncoding`, `[Console]::OutputEncoding`, and `$OutputEncoding` to UTF-8 before executing the original command. This ensures both input and output are properly encoded as UTF-8 regardless of the system locale.

Fixes #4138
Fixes #2422
Fixes #2939
@senguangd
senguangd force-pushed the fix/powershell-encoding branch from cf890dd to 6e0639b Compare June 12, 2026 04:56
@johnnyeric
johnnyeric requested a review from catrielmuller June 12, 2026 11:23
@catrielmuller
catrielmuller merged commit 91eb3ed into Kilo-Org:main Jun 12, 2026
17 checks passed
@johnnyeric

Copy link
Copy Markdown
Contributor

Hey @senguangd, could you please link your GitHub account to the Kilo account? This way, we can grant you credits for the merged PRs.
Once you have done that, please message johnny[at]kilocode.ai

t7tran pushed a commit to t7tran/kilocode that referenced this pull request Aug 14, 2026
fix(shell): use EncodedCommand for PowerShell to fix Windows encoding issues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants