Skip to content

fix(windows): 修复长路径启动崩溃与文件搜索失败 - #4825

Merged
sailist merged 2 commits into
MoonshotAI:mainfrom
pvzheroes125:fix/windows-spawn-long-cwd-20261111
Nov 11, 2026
Merged

sailist merged 2 commits into
MoonshotAI:mainfrom
pvzheroes125:fix/windows-spawn-long-cwd-20261111

Conversation

@pvzheroes125

@pvzheroes125 pvzheroes125 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

需求或缺陷

在 Windows 上,即使工作目录真实存在,较长的 cwd 仍可能导致 Core 子进程启动失败,随后触发未处理的 read ENOTCONN,使整个宿主退出。本次修复让启动失败正常返回给调用方,并让 Glob 能够搜索长目录。

关联 issue:#4749。以下复现使用隔离的合成文件,验证范围覆盖真实 Core 工具类,不包含原应用的完整生命周期。该 issue 目前尚无维护者的 /approve 评论。

缺陷复现步骤

  1. 创建长度分别为 57、285、288 字符的普通 ASCII 绝对目录,每个路径组件均短于 100 字符。在每个目录中放置 fixture.txt,以及仅包含 print("CWD_PROBE_OK") 的 fixture.py。
  2. 通过进程启动参数传入 cwd。在短目录和 285 字符目录中,通过 HostProcessService 执行 cmd.exe /d /c echo CWD_PROBE_OK;再通过真实 Bash 工具执行 PYTHONDONTWRITEBYTECODE=1 python -B "fixture.py" verify。
  3. 通过真实 Glob 工具,在短目录和 288 字符搜索根中搜索 fixture.*。每次长路径调用后,在同一个运行时进程中继续执行短路径探针。

改前和改后使用同一套 harness 与原始 fixture 目录。harness 调用真实 Core 工具类,仅为环境、上下文等依赖提供最小接线;验证覆盖到工具层,不代表完整 CLI 或桌面应用会话验证。主实验传入的路径均不带扩展长度前缀。

Windows 场景与执行入口 改前 改后
57 字符 cwd:HostProcess、Bash、Glob 全部成功 全部成功
285 字符 cwd:HostProcess spawn ENOENT 后触发未处理的 read ENOTCONN,宿主退出码为 1 返回 SpawnFailed,保留原始 ChildProcess 错误为 cause;同一 PID 的下一条短 cwd 探针成功
285 字符 cwd:Bash spawn ENOENT 后触发未处理的 read ENOTCONN,宿主退出码为 1 将启动错误正常返回为工具错误;同一 PID 的下一条短 cwd Python 探针成功
288 字符搜索根:Glob rg 启动失败,随后因 read ENOTCONN 导致宿主退出码为 1 从较短的祖先目录启动 rg,以参数传入长搜索根,返回两个预期 fixture 文件;同一 PID 随后完成短根搜索

两次构建均基于 f26481d2bf20fe35fdce4ea6546953fc0552e6b4。实测环境:Windows 11 10.0.26300、Node 24.15.0、Core 0.6.0、CLI 源码包 2.5.3、pnpm 10.33.0、Git Bash 5.2.37、Git for Windows 2.47.1.windows.2、ripgrep 15.0.0、Python 3.10.11。系统长路径支持原本已启用,本次没有修改该设置。

根因

HostProcessService 等待 ChildProcess 启动后,才构建现有流封装。Windows 拒绝长 cwd 时,ChildProcess 会发出启动错误,其尚未连接的管道也可能在流处理者建立前发出错误。仅捕获 ChildProcess 错误,仍会留下未处理的管道错误。

Glob 原本使用 cwd=searchRoot 启动 rg。一个目录可以被 rg 作为路径参数读取,却不能作为 Win32 进程的 cwd。直接改用其他 cwd,又会改变 anchored 模式和包含路径的 --glob 模式的匹配基准。

管道错误处理修复了宿主崩溃;Glob 的改动绕过 Windows cwd 长度限制,并不使原生进程支持任意长度的 cwd。

代码改动

 HostProcessService 启动过程
+  立即接住 stdin/stdout/stderr 的 error
   等待 ChildProcess 的 spawn 或 error
+  失败:保留原始错误为 cause,管道关闭前继续接住错误
+  成功:先建立现有 HostProcess 处理,再移除启动阶段监听

 Glob 搜索
-  始终使用 cwd=searchRoot,搜索参数为 "."
+  Windows 长根改用较短的祖先目录作为 cwd,唯一搜索路径参数仍为 searchRoot
+  按新 cwd 重定位内部 glob 模式

启动阶段监听只属于单次启动尝试,在成功移交或管道关闭后移除。同步 spawn 异常继续使用现有 SpawnFailed 转换。

Glob 的 rg 可执行文件探测也处理了 workspace cwd 本身过长的情况。搜索范围仍限于指定根目录,内部模式转换保留普通、anchored、含路径、否定、brace 等匹配语义,并转义根路径中的 glob 元字符。现有 rg 查找与回退、EAGAIN 重试、ignore 参数、VCS 排除、敏感文件过滤、排序、分页和返回格式继续沿用原实现。普通短目录仍使用原 cwd 和 . 搜索参数。

行为变化与受影响用户

行为 改前 改后 受影响用户与调用方 退路或兼容性
子进程启动失败后的管道错误 可能终止整个宿主 返回原有启动失败及原始 cause,安全清理管道 Windows Core 调用方,包括 TUI、print、desktop/web、VS Code、ACP 和 SDK 宿主 保留现有错误契约,无需额外配置
Windows 长根 Glob,包含 workspace 根也过长的情况 无法从该 cwd 启动 rg 从短祖先 cwd 启动,以参数指定原搜索根 通过上述 Core 调用方搜索深层目录的 Windows 用户 保留搜索范围及匹配、输出语义,无需额外配置
成功启动和普通目录 Glob 现有进程、流处理及根目录相对搜索 启动监听移交给现有处理;短根保持原搜索路径 Windows 及其他平台的现有调用方 继续支持原有输入

第一行由 hostProcessService.test.ts 覆盖:原始 cause 身份、三个管道的早期与后续错误、失败清理,以及成功后的监听移交。Glob 相关行为由 tools/glob.test.ts 覆盖:长搜索根、长 workspace 根、anchored 模式、ignore、输出和现有短根集成场景。另外将一处现有外部根测试的断言统一为生产输出使用的 / 分隔符,使其能在 Windows 上运行;未为此改变生产输出。

验证结果:两份聚焦测试 79 通过/1 跳过,包类型检查和 git diff --check 通过。独立改前/改后 harness 的 43 组匹配、ignore、输出对照全部通过,覆盖普通及 anchored 模式、否定、brace、递归、目录模式、两种 include_ignored 值、嵌套 ignore、敏感文件过滤、排序、外部根输出,以及根路径包含 glob 元字符的情况。

完整 pnpm run lint 已通过(0 errors,现有 warnings 不阻断),pnpm run sherif 通过。测试中的进程清理调用均等待 dispose() 完成。

lint:imports 在未改动文件中报告 4 条诊断。对同一基线导出的未修改源码运行检查,得到完全相同的诊断。本轮未运行完整应用生命周期及无关包的测试。

验证命令与基线已有的导入诊断
pnpm --filter @moonshot-ai/agent-core-v2 test test/os/backends/node-local/hostProcessService.test.ts test/os/backends/node-local/tools/glob.test.ts
  79 通过,1 跳过
pnpm --filter @moonshot-ai/agent-core-v2 typecheck
  退出码 0
git diff --check
  退出码 0
pnpm run lint
  退出码 0,0 errors
pnpm run sherif
  退出码 0
pnpm --filter @moonshot-ai/agent-core-v2 lint:imports
  基线与修复后均为相同的 4 条诊断,退出码 1

基线已有诊断位于 agent/fullCompaction/fullCompactionService.ts、agent/task/completionRestore.ts、agent/task/taskOutput.ts 和 human/agent/origin.ts。

检查清单

@changeset-bot

changeset-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4ade11c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T06:48:21.666242Z d2e3b21 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@4ade11c
npx https://pkg.pr.new/@moonshot-ai/kimi-code@4ade11c

commit: 4ade11c

@pvzheroes125 pvzheroes125 changed the title fix(windows): contain spawn pipe errors and search long directories fix(windows): 修复长路径启动崩溃与文件搜索失败 Nov 11, 2026
@sailist
sailist merged commit 41b7acc into MoonshotAI:main Nov 11, 2026
17 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 11, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 11, 2026
…pgrep cwd

Port upstream MoonshotAI#4825. When the glob search root exceeds the Windows path
limit, run ripgrep from a shorter parent directory and prefix the glob
patterns with the relative path so matches and ordering are unchanged.
The hostProcessService pipe-error containment from the same upstream PR
is already equivalent under the Bun spawn translation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants