Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(src-tauri): 子进程日志按 ANSI 代码页解码,中文 Windows 不再乱码

hairyf7504401c0f94
diff --git a/src-tauri/src/service/plugin/process.rs b/src-tauri/src/service/plugin/process.rs
index d86ed5d1..6e4bc626 100644
--- a/src-tauri/src/service/plugin/process.rs
+++ b/src-tauri/src/service/plugin/process.rs
@@ -14,6 +14,8 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use tauri::{Emitter, WebviewWindow};
+use crate::utils::decode_process_line;
+
#[cfg(windows)]
use crate::service::workflow;
#[cfg(not(windows))]
@@ -326,12 +328,13 @@ fn spawn_line_emitter<R: Read + Send + 'static>(
match buf.read_until(b'\n', &mut acc_buf) {
Ok(0) => break,
Ok(_) => {
- // lossy 兜底:zh-CN Windows 下 python MCP 插件输出 GBK 日志时,
- // 严格 UTF-8 读取会中断本线程并关闭子进程管道(EPIPE)——
- // 安装进程可能因此以非 0 退出码失败,被误判为安装失败。
+ // 非法 UTF-8 不能中断本线程:管道读端一关,子进程写 stderr
+ // 就收到 EPIPE,安装进程可能因此以非 0 退出码失败而被误判为
+ // 安装失败。解码先走 ANSI 代码页(`decode_process_line`),
+ // zh-CN Windows 下 python MCP 插件的 GBK 日志才不会变乱码。
// 行尾剥离与上游 utils.rs 的 #197 修复一致:只剥 \r\n/\n,
// 保留行内尾随空白以对齐 BufRead::lines() 语义。
- let line = String::from_utf8_lossy(&acc_buf);
+ let line = decode_process_line(&acc_buf);
let trimmed = line
.strip_suffix("\r\n")
.or_else(|| line.strip_suffix('\n'))
diff --git a/src-tauri/src/service/workflow/utils.rs b/src-tauri/src/service/workflow/utils.rs
index 728d41b0..d23a02a7 100644
--- a/src-tauri/src/service/workflow/utils.rs
+++ b/src-tauri/src/service/workflow/utils.rs
@@ -5,6 +5,8 @@ use std::sync::{Mutex, OnceLock};
use std::thread;
use std::time::Duration;
+use crate::utils::decode_process_line;
+
const DSH_MAX_LOG_BYTES: u64 = 5 * 1024 * 1024;
const DSH_MAX_BACKUPS: usize = 3;
static DSH_LOG_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
@@ -197,12 +199,13 @@ where
/// 逐行读取子进程输出并写入日志。
///
-/// 任何一行是非法 UTF-8 时**必须**用 lossy 替换继续读下去,不能中断:管道
-/// 读端一旦被关闭,dsh 主进程下一次写 stderr 就会收到 EPIPE,Node 以退出码 1
-/// 静默崩溃(插件子进程——python MCP 服务器等——在中文本地化 Windows 下按
-/// ANSI 代码页输出 GBK 日志是常态),桌面端表现为 Harness 反复崩溃、WebView
-/// 永久卡在 "Loading plugins"。同样的非法字节在日志里以 U+FFFD 呈现,不影响
-/// 其余行的可读性。真正需要停手的只有 EOF 与管道自身的 I/O 错误。
+/// 任何一行是非法 UTF-8 时**必须**继续读下去,不能中断:管道读端一旦被关闭,
+/// dsh 主进程下一次写 stderr 就会收到 EPIPE,Node 以退出码 1 静默崩溃(插件
+/// 子进程——python MCP 服务器等——在中文本地化 Windows 下按 ANSI 代码页输出
+/// GBK 日志是常态),桌面端表现为 Harness 反复崩溃、WebView 永久卡在
+/// "Loading plugins"。解码交给 [`decode_process_line`]:先严格 UTF-8,失败按
+/// ANSI 代码页解,仍失败才以 U+FFFD 呈现。真正需要停手的只有 EOF 与管道自身的
+/// I/O 错误。
fn drain_subprocess_output<R: Read + Send + 'static>(
mut reader: BufReader<R>,
log_path: PathBuf,
@@ -216,7 +219,7 @@ fn drain_subprocess_output<R: Read + Send + 'static>(
// 去除行尾 \n / \r\n(与旧 lines() 行为一致)
let bytes = bytes.strip_suffix(b"\n").unwrap_or(&bytes);
let bytes = bytes.strip_suffix(b"\r").unwrap_or(bytes);
- let line = String::from_utf8_lossy(bytes);
+ let line = decode_process_line(bytes);
match level {
log::Level::Warn => log::warn!(target: "dsh", "{}", line),
_ => log::info!(target: "dsh", "{}", line),
diff --git a/src-tauri/src/utils/mod.rs b/src-tauri/src/utils/mod.rs
index 90d454ca..ce424c30 100644
--- a/src-tauri/src/utils/mod.rs
+++ b/src-tauri/src/utils/mod.rs
@@ -124,3 +124,84 @@ pub fn app_icon_temp_path(app: &tauri::AppHandle) -> Option<std::path::PathBuf>
img.save(&path).ok()?;
Some(path)
}
+
+/// 解码子进程输出的一行。
+///
+/// 中文 Windows 下子进程(cmd.exe、python MCP 服务器等)按 ANSI 代码页输出 GBK,
+/// 图省事的 `from_utf8_lossy` 会把 `系统找不到指定的路径。` 解成
+/// `ϵͳ�Ҳ���ָ����·����`——日志里认不出原话。这里先严格 UTF-8,失败再按 ANSI
+/// 代码页解码,仍失败才回落 lossy。
+///
+/// 任何情况下都不能中断读取:管道读端一关,dsh 主进程写 stderr 就收到 EPIPE
+/// 并静默退出(见 `service::workflow::utils::drain_subprocess_output`)。
+pub fn decode_process_line(bytes: &[u8]) -> String {
+ if let Ok(text) = std::str::from_utf8(bytes) {
+ return text.to_owned();
+ }
+ #[cfg(windows)]
+ if let Some(text) = decode_multibyte(bytes, windows_sys::Win32::Globalization::CP_ACP) {
+ return text;
+ }
+ String::from_utf8_lossy(bytes).into_owned()
+}
+
+/// 按给定代码页把多字节串解成 UTF-16,再取 `String`;转换失败返回 `None`。
+#[cfg(windows)]
+fn decode_multibyte(bytes: &[u8], codepage: u32) -> Option<String> {
+ use windows_sys::Win32::Globalization::MultiByteToWideChar;
+
+ if bytes.is_empty() {
+ return Some(String::new());
+ }
+ if bytes.len() > i32::MAX as usize {
+ return None;
+ }
+ let len = bytes.len() as i32;
+ // SAFETY: 指针与长度同源于 `bytes`;空缓冲 + 0 长度只探测所需宽字符数。
+ let needed =
+ unsafe { MultiByteToWideChar(codepage, 0, bytes.as_ptr(), len, std::ptr::null_mut(), 0) };
+ if needed <= 0 {
+ return None;
+ }
+ let mut wide = vec![0u16; needed as usize];
+ // SAFETY: `wide` 恰有 `needed` 个元素,等于上一次探测出的所需长度。
+ let written =
+ unsafe { MultiByteToWideChar(codepage, 0, bytes.as_ptr(), len, wide.as_mut_ptr(), needed) };
+ if written <= 0 {
+ return None;
+ }
+ wide.truncate(written as usize);
+ Some(String::from_utf16_lossy(&wide))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::decode_process_line;
+ #[cfg(windows)]
+ use super::decode_multibyte;
+
+ #[test]
+ fn keeps_valid_utf8_untouched() {
+ let text = "系统找不到指定的路径。";
+ assert_eq!(decode_process_line(text.as_bytes()), text);
+ }
+
+ #[test]
+ fn invalid_bytes_still_produce_a_line() {
+ assert!(!decode_process_line(&[0xFF, 0xFE, 0x80]).is_empty());
+ }
+
+ /// GBK 字节按显式 936 解码,与 runner 自身的 ANSI 代码页无关。
+ #[cfg(windows)]
+ #[test]
+ fn decodes_gbk_with_an_explicit_codepage() {
+ let gbk = [
+ 0xCF, 0xB5, 0xCD, 0xB3, 0xD5, 0xD2, 0xB2, 0xBB, 0xB5, 0xBD, 0xD6, 0xB8, 0xB6, 0xA8,
+ 0xB5, 0xC4, 0xC2, 0xB7, 0xBE, 0xB6, 0xA1, 0xA3,
+ ];
+ assert_eq!(
+ decode_multibyte(&gbk, 936).as_deref(),
+ Some("系统找不到指定的路径。")
+ );
+ }
+}