fix(bash): 为 Windows Git Bash 增加命令预处理兼容层 - #2717
Conversation
这个 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 detectedLatest commit: 6331752 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 |
There was a problem hiding this comment.
💡 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(); |
There was a problem hiding this comment.
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 👍 / 👎.
| // 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). |
There was a problem hiding this comment.
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.
这个 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 执行前统一改写命令:
必要性:Windows 上模型常输出带反斜杠路径、cmd 风格命令或 Git Bash 缺失的
命令,原 Bash 工具仅做 >nul 正则重写,这些命令会直接失败;本改动在
bash -c 前完成改写,非 Windows 平台行为完全不变。
Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.