Skip to content

fix(bash): 为 Windows Git Bash 增加命令预处理兼容层 - #2717

Open
MaxwellGengYF wants to merge 3 commits into
MoonshotAI:mainfrom
Sikao-Engine:main
Open

fix(bash): 为 Windows Git Bash 增加命令预处理兼容层#2717
MaxwellGengYF wants to merge 3 commits into
MoonshotAI:mainfrom
Sikao-Engine:main

Conversation

@MaxwellGengYF

@MaxwellGengYF MaxwellGengYF commented Aug 7, 2026

Copy link
Copy Markdown

这个 Commit 完全由人类 review 过,并非直接 vibe coding。

Related Issue

#2718

Resolve #(issue_number)

Problem

这个 PR 解决的是 Windows 上 Bash 工具执行命令经常失败 的问题:

模型在 Windows 上常常输出带反斜杠的 Windows 路径(C:\x\y)、cmd 风格命令(copy、tasklist、cd /d、>nul)或 Git Bash 缺失的 POSIX 命令(rev、tree、zip 等),而旧的 Bash 工具只做 >nul 的正则重写,这些命令会直接报错。

PR 在 bash -c 执行前统一改写命令:把反斜杠路径转成 Git Bash 风格、去掉 cd /d、补上 79 条缺失命令的 fallback、保留 >nul 重写,并注入 MSYS_NO_PATHCONV 等环境变量,避免原生工具参数被 MSYS 路径转换破坏。

What changed

在 Bash 工具中(agent-core 与 agent-core-v2)实现 Windows Git Bash 兼容层,以纯 TypeScript 的 @moonshot-ai/tree-sitter-bash 解析器为 shell 感知前端。
在 bash -c 执行前统一改写命令:

  • 预处理:引号感知的反斜杠路径 -> 正斜杠整体转换(heredoc/注释/命令名 除外),再基于语法树做保守路径重写、缺失命令 fallback、cd /d 去旗标;
  • 内置 79 条fallback 定义(rev/tree/zip/tasklist/ copy 等),仅当 command -v 失败时生效,pip3/python3 带 WindowsApps 桩防护;
  • 子进程注入 MSYS_NO_PATHCONV / MSYS2_ARG_CONV_EXCL,避免 /FO 等原生工具 参数被 MSYS 路径转换破坏;Git Bash 下前置 export MSYSTEM=;,开发环境 检测真实的 Windows MSVC 环境而非 mingw;>nul -> /dev/null 重写保留;
  • 性能优化:无可改写命令时跳过解析,MSYSTEM 标记探测按安装根目录缓存。

必要性:Windows 上模型常输出带反斜杠路径、cmd 风格命令或 Git Bash 缺失的
命令,原 Bash 工具仅做 >nul 正则重写,这些命令会直接失败;本改动在
bash -c 前完成改写,非 Windows 平台行为完全不变。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

这个 Commit 完全由人类 review 过,并非直接 vibe coding。
在 Bash 工具中(agent-core 与 agent-core-v2)实现 Windows Git Bash 兼容层,以纯 TypeScript 的
@moonshot-ai/tree-sitter-bash 解析器为 shell 感知前端。
在 bash -c 执行前统一改写命令:

- 预处理:引号感知的反斜杠路径 -> 正斜杠整体转换(heredoc/注释/命令名
  除外),再基于语法树做保守路径重写、缺失命令 fallback、cd /d 去旗标;
- 内置 79 条fallback 定义(rev/tree/zip/tasklist/
  copy 等),仅当 command -v 失败时生效,pip3/python3 带 WindowsApps 桩防护;
- 子进程注入 MSYS_NO_PATHCONV / MSYS2_ARG_CONV_EXCL,避免 /FO 等原生工具
  参数被 MSYS 路径转换破坏;Git Bash 下前置 export MSYSTEM=;,开发环境
  检测真实的 Windows MSVC 环境而非 mingw;>nul -> /dev/null 重写保留;
- 性能优化:无可改写命令时跳过解析,MSYSTEM
  标记探测按安装根目录缓存。

必要性:Windows 上模型常输出带反斜杠路径、cmd 风格命令或 Git Bash 缺失的
命令,原 Bash 工具仅做 >nul 正则重写,这些命令会直接失败;本改动在
bash -c 前完成改写,非 Windows 平台行为完全不变。
@changeset-bot

changeset-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6331752

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 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 803b746064

ℹ️ 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".

// byte-for-byte for exactly these commands.
if (!needsFullPipeline(command)) return unchanged();
const parsed = parse(command);
if (!parsed.ok) return unchanged();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Degrade when bash parsing reports errors

When a Windows command parses as ok but hasError (for example a partially recovered malformed command), this code still walks the recovered tree and can rewrite paths or prepend fallback functions before Bash sees the original syntax error. The repository contract for @moonshot-ai/tree-sitter-bash says callers must treat both aborted and hasError trees as “cannot analyze” and degrade, so this should return unchanged() when parsed.hasError is true.

AGENTS.md reference: AGENTS.md:L31-L31

Useful? React with 👍 / 👎.

Comment on lines +41 to +43
// Fallback function bodies — static bash strings ported byte-for-byte from
// kimi-agent `bash_fix.py` `_FALLBACK_BODIES` (including the embedded perl /
// PowerShell delegate snippets, which must stay exact).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove implementation comments from v2 source

This new v2 implementation adds non-header implementation comments throughout the file, but the scoped v2 guide only permits comments in the top-of-file block and explicitly forbids comments beside functions, methods, or statements. Please fold any necessary external-role context into the header or remove these line-level narratives so the new helper follows the package convention.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L36-L40

Useful? React with 👍 / 👎.

…he Windows fixer

Return commands byte-for-byte unchanged when the tree-sitter-bash parse
reports hasError, per the package caller contract (aborted and hasError
trees are 'cannot analyze'): a partially recovered malformed command must
not get path rewrites or fallback function prepends before Bash reports
the original syntax error. Add a regression test asserting
unterminated-quote commands are passed through unchanged.

Also fold the external-role context (fallback-body provenance from
kimi-agent bash_fix.py, WindowsApps stub guard, parser degradation
contract) into the top-of-file header and remove all non-header comments,
per the package header-only comment convention.
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.

1 participant