Repository navigation
fix(agent-core-v2): spawn Windows batch script commands through cmd.exe - #4791
Conversation
🦋 Changeset detectedLatest commit: f830e76 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved Windows command handling defects can cause valid relative batch commands to fail and allow shell metacharacters to execute unintended commands.
3 open issues
What changed in this PR
该 PR 修复 Windows 上通过 npx、npm、uvx 等 .cmd/.bat 脚本启动 stdio MCP 服务失败的问题,并在统一的 node-local 进程派生入口中处理本地及远程 Windows 执行环境。
Changes:
- 新增 Windows PATH/PATHEXT 解析及批处理命令识别逻辑,并通过
cmd.exe /d /s /c启动脚本。 - 在
HostProcessService中接入批处理包装,并统一同步与异步 spawn 失败的错误分类。 - 增加批处理命令解析、扩展名、路径、PATH 优先级和显式 shell 行为测试,并添加 changeset。
| File | Description |
|---|---|
| packages/agent-core-v2/test/os/backends/node-local/hostProcessService.test.ts | 增加 Windows 批处理命令包装、路径和 shell 行为的测试覆盖。 |
| packages/agent-core-v2/src/os/backends/node-local/windowsCommand.ts | 实现 Windows PATH/PATHEXT 解析、批处理脚本识别及 cmd.exe 参数构造。 |
| packages/agent-core-v2/src/os/backends/node-local/hostProcessService.ts | 在中央进程派生入口接入 Windows 批处理包装并归一化 spawn 错误。 |
| .changeset/win32-batch-script-spawn.md | 记录 Windows stdio MCP 脚本命令启动修复。 |
Review effort: Lite
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4cdc791752
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
已在 efe2e5a 中处理全部评审意见:
补充说明:第 4 点 cross-spawn/which 同样是目录优先、会选中前面目录的 .cmd(其面向 npm shim 场景,无需保持 CreateProcess 语义);这里刻意比 cross-spawn 更保守。 测试:hostProcessService 17/17 通过(含 4 个新增评审用例与真实 .cmd 端到端 round-trip),typecheck / check-no-comments 干净。 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: efe2e5aab9
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
复审意见已处理(6bf2300a6):裸命令的原生查找保护从 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6bf2300a66
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
…on in Windows batch spawn
|
第三轮复审意见已处理(743e70dad):
hostProcessService 测试 21/21 通过,typecheck / check-no-comments 干净。 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 743e70dada
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
实现方式调整(2770e276c):按维护者决定,移除自实现的 Windows 解析/转义模块(windowsCommand.ts,净删 363 行),主路径直接改用 cross-spawn。跨目录 exe/com 优先级、换行符拒绝、env 词典序赢家等自实现语义随之移除,PR 描述中已列出与 cross-spawn 语义绑定的取舍表;唯一行为变化是 Windows 上未知裸命令改为经 cmd.exe 运行时报错(退出码 1),不再在 spawn 阶段抛 ENOENT,对应存量测试已按平台分流。测试 8 通过 + 1 平台跳过,typecheck / lint 干净。 |
|
❌ Nix build failed Hash mismatch in
Please update |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5400d8c5de
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
spawn 阶段 ENOENT 分类问题已按方案 B 修复(72f57d1d4):cross-spawn 对未解析到命令的进程会以退出码 1 退出并补发带 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 72f57d1d46
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
关于显式空 PATH 的评审意见,做了真机验证,决定不处理: which 的 残留差异仅在错误分类:此场景 which 用父 PATH 解析到了文件, |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f591b6a6cd
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
显式 shell 命令的退出码问题已修复(1b846c2c9): |
|
exit-error 等待的挂死风险已修复(f830e76a4): |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
…xe (#4791) (#4804) * fix(agent-core-v2): spawn Windows batch script commands through cmd.exe * fix(agent-core-v2): harden Windows batch spawn escaping and resolution * fix(agent-core-v2): preserve native .com precedence in Windows spawn resolution * fix(agent-core-v2): reject line breaks and align env and cwd resolution in Windows batch spawn * refactor(agent-core-v2): replace custom Windows batch spawn handling with cross-spawn * chore: update lockfile for cross-spawn * chore: update nix pnpmDeps hash for cross-spawn * fix(agent-core-v2): classify process-level ENOENT during MCP startup as config error * fix(agent-core-v2): await transport exit error before classifying MCP startup failure * fix(agent-core-v2): keep native spawn for explicit-shell commands on Windows * fix(agent-core-v2): bound the MCP transport exit-error wait
…nfig errors Port upstream MoonshotAI#4791. Capture the transport exit error and pass it to classifyStdioStartupError so a process-level permanent errno during startup surfaces as a configuration error with its cause. The Windows batch-script spawn routing from the same PR is already equivalent via the Bun findWindowsScript path. Record the B4 ledger verdicts.


Requirement or Bug
Windows 上,凡是以脚本型命令(
npx、npm、uvx等,磁盘实体是.cmd/.bat)声明的 stdio MCP 服务器都无法启动,服务器状态为 failed,日志报spawn npx ENOENT。官方文档的mcp.json示例("command": "npx")以及插件清单里声明的 MCP 服务器(如 Chrome DevTools MCP)在 Windows 上全部命中此问题。本 PR 让这些配置在 Windows 上无需改写即可使用。Bug Reproduction Steps
~/.kimi-code/mcp.json配置一个 stdio 服务器:"command": "npx", "args": ["-y", "[email protected]"]。/mcp显示该服务器 failed;kimi-code.log出现mcp server unavailable ... reason="spawn npx ENOENT"。npx.cmd全路径也不行:Node 会同步抛出EINVAL。Root Cause
Windows 上
npx的实体是npx.cmd,而HostProcessService.spawn()直接裸调node:child_process的spawn:npx→ CreateProcess 只在无扩展名时自动补.exe,不做 PATHEXT 查找,因此ENOENT;npx.cmd→ Node ≥ 18.20.2 / 20.12.2(CVE-2024-27980)禁止无 shell 直接执行批处理文件,同步抛EINVAL,且该同步异常此前不在HostProcessError的错误分类链路里。本 PR 是根本修复:在本地进程派生的唯一入口补上 Windows 批处理解析与
cmd.exe包装,而不是在 MCP 调用侧做特例绕过。远程 exec-server 复用同一 node-local 后端,因此远程 Windows 目标机上的 stdio 派生一并修好。Code Changes
核心思路:只在「此前必然失败」的场景里插入一个转换层——Windows 且调用方未指定
shell时,按 PATH + PATHEXT 解析裸命令名,若胜出者是.cmd/.bat,则改写为cmd.exe /d /s /c "<转义后的命令行>"(带windowsVerbatimArguments);解析到.exe/.com或找不到命令时原样放行,现有成功路径与 ENOENT 错误链路完全不变。新增
src/os/backends/node-local/windowsCommand.ts提供纯函数转换;hostProcessService.ts接入并统一同步/异步两类 spawn 失败的错误形态。关键决策:
HostProcessService(中央 spawn 入口)client-stdio.ts里做 MCP 特例lint:imports要求远程执行器只用 node 内置模块,而本文件在其依赖图内.cmd/.bat胜出时包装.exe等现有成功路径保持 CreateProcess 原语义,零回归面cmd.exe /d /s /c+ 逐 token 转义shell: trueshell: true对 args 只做朴素拼接,带空格/引号参数会碎且有注入面已知限制:参数中的 cmd 元字符(
%VAR%展开等)不做逐字符转义,与计划文档中声明的范围一致;.ps1不在处理范围。阅读顺序:先看
windowsCommand.ts理解解析与转义规则,再看hostProcessService.ts的接入点(约 15 行),测试与 changeset 可略读。Behavior Changes and Affected Users
npx)ENOENT失败cmd.exe包装正常启动.cmd/.bat扩展名的命令EINVAL逃逸cmd.exe包装正常启动HostProcessError分类HostProcessError(SpawnFailed).exe/.com命令、POSIX 平台、显式shell选项、找不到的命令McpClassifiedStartupError('config')链路受影响模块与测试覆盖:
os/backends/node-local:新增 6 个 mock 用例(裸名包装、带扩展名包装、路径形式包装、.exe胜出不动、未知命令不动、显式 shell 不动)+ 1 个真实.cmd端到端用例(含带空格/引号参数 round-trip),钉住「不变」人群的旧行为。mcpCore(消费方):逻辑未改,其测试在 Windows 上的存量失败与本 PR 无关(见折叠块)。验证结论:目标测试 13/13 通过,
typecheck干净,check-no-comments通过;lint:imports的 1 处违规为存量问题(src/human/agent/origin.ts)。存量失败的对照验证过程
mcpCore/client-stdio.test.ts等 4 个文件共 44 个失败,报错为 cwd 拼接(/OpenSource/...与D:\OpenSource\...混拼)与 symlink 权限等 Windows 环境问题。git stash -u在干净 main 上重跑:client-stdio.test.ts同样 17 failed / 5 passed,与带本 PR 改动时一致,确认非本次回归。lint:imports违规位于src/human/agent/origin.ts(human kernel import 边界),本次 diff 未触及该文件。Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.