Windows (GBK) 上 cs upgrade 因 UnicodeDecodeError 失败,并弄坏现有安装
现象
在中文 Windows(系统代码页 GBK / cp936)上运行 cs upgrade,升级失败,并且把现有安装也弄坏了:之后所有 cs 命令都报 ModuleNotFoundError: No module named 'claude_statusbar',状态栏直接消失。
File "...\Lib\subprocess.py", line 1614, in _readerthread
buffer.append(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0x82 in position 287: illegal multibyte sequence
Upgrade failed. Run manually: C:\...\uv.EXE tool install --upgrade claude-statusbar
$ cs --version
File "C:\Users\<user>\.local\bin\cs.exe\__main__.py", line 4, in <module>
from claude_statusbar.cli import main
ModuleNotFoundError: No module named 'claude_statusbar'
按提示手动运行 uv tool install --upgrade claude-statusbar 可以恢复(装上 3.43.3 → 3.43.5)。
原因
claude_statusbar/updater.py:293 用 subprocess.run(cmd, capture_output=True, text=True, ...) 调用 uv tool install --upgrade。text=True 且没指定 encoding 时,Python 会用区域设置的编码(这里是 GBK)解码子进程输出,而 uv 输出的是 UTF-8(进度条 / 符号),于是读取线程抛出 UnicodeDecodeError,异常中断了 cs 对升级结果的处理。
此时 uv 大概已经把旧环境拆掉了,结果留下一个装了一半的 tool 环境,入口还在,包却不见了。
建议修复
- 所有
capture_output=True, text=True的调用都加上encoding="utf-8", errors="replace"。其中updater.py:293最关键,另外updater.py:357、ocs.py:198、core.py:592、daemon.py:387/428、party.py:192、scoped_usage.py:49、_git_refresh.py:97、cleanup.py:75、service.py:139也有同样的写法(ocs whoami输出里有非 ASCII 的会话名时同样会出问题)。 - 在升级子进程的环境里设置
PYTHONIOENCODING=utf-8/PYTHONUTF8=1。 - 升级失败后做一次自检(例如运行新入口的
cs --version);如果已经坏了,就自动重跑一次uv tool install --upgrade,或者至少用醒目的方式提示用户安装已经损坏。注意每日自动升级是在后台静默跑的,同样的崩溃会让用户的状态栏莫名消失。
环境
- cs 3.43.3 → 3.43.5(uv tool 安装,uv 来自 WinGet)
- Python 3.14.7
- Windows 11 中文版(ANSI 代码页 936)
Fixed in v3.43.6 (af7c732): upgrade output is captured as bytes (uv runs with PYTHONUTF8=1), so a GBK locale can no longer crash the upgrade mid-install. If an upgrade fails and leaves cs unrunnable, both cs upgrade and the daily background upgrade retry once; if cs is still broken, cs upgrade says so and prints the repair command. The other text-mode subprocess reads (ocs whoami, git, …) now decode UTF-8 with replacement. Upgrade with uv tool install --upgrade claude-statusbar.
Follow-up in v3.43.7 (0479ffb). After 3.43.6 fixed the decode crash, cs upgrade still broke the install on Windows. Windows won't delete a file a process has open, and the daemon and every render keep cs.exe open. uv tool install --upgrade rebuilds the env and died deleting Scripts\\.
On Windows, cs now runs uv tool upgrade (an in-place sync) after renaming the in-use launchers aside. Any launcher uv didn't rewrite is moved back, and success is judged by the installed version. Reproduced and verified on Windows 11 with the daemon, the upgrading process and a 50ms render loop all running: both cs upgrade and the background auto-upgrade went 3.43.0 → 3.43.6 with cs working throughout. Auto-upgrade also never ran on Windows before (copied launchers made every uv install look like a shadow install); uv's receipt now settles ownership.
To get onto 3.43.7 from an older version, use uv tool upgrade claude-statusbar, not cs upgrade. If it reports cs.exe in use (os error 32), the package is still upgraded.