JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(tools): windows bash tool
weichenhao46b95477656b
diff --git a/jiuwenswarm/agents/harness/common/prompt/prompt_builder.py b/jiuwenswarm/agents/harness/common/prompt/prompt_builder.pyindex 2e14cad9ca..5ecacd4dac 100644--- a/jiuwenswarm/agents/harness/common/prompt/prompt_builder.py+++ b/jiuwenswarm/agents/harness/common/prompt/prompt_builder.py@@ -1,9 +1,10 @@# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.+import sysfrom enum import IntEnumfrom typing import Optional-import sysfrom openjiuwen.harness.prompts import SystemPromptBuilder, PromptSection, resolve_language+from jiuwenswarm.agents.harness.common.prompt.shell_environment import build_shell_environment_promptfrom jiuwenswarm.common.utils import loggerfrom jiuwenswarm.common.utils import (@@ -115,6 +116,7 @@ def _identity_prompt(language: str) -> PromptSection:skills_dir = get_agent_skills_dir()todo_dir = get_deepagent_todo_dir()os_type = sys.platform+ shell_env_prompt = build_shell_environment_prompt(language, os_type)if language == "cn":content = f"""你是一个私人智能体,由 JiuwenSwarm 创建。像一个有温度的人类助手一样与用户互动。@@ -146,6 +148,8 @@ def _identity_prompt(language: str) -> PromptSection:当前运行平台:`{os_type}`+{shell_env_prompt}+**重要提示**:必须严格使用与当前平台匹配的命令语法,切勿使用其他平台的命令格式。常见命令差异对照:@@ -159,7 +163,7 @@ def _identity_prompt(language: str) -> PromptSection:| 删除目录 | `rmdir folder` 或 PowerShell `Remove-Item -Recurse folder` | `rm -rf folder` || 查找文件 | `dir /s pattern` 或 PowerShell `Get-ChildItem -Recurse -Filter pattern` | `find . -name pattern` |-**特别注意**:Windows 的 `mkdir` 不支持 `-p` 参数!在 Windows 上使用 `mkdir -p folder` 会错误创建名为 `-p` 的目录。如需创建嵌套目录,请使用 PowerShell `New-Item -ItemType Directory -Path "parent/child" -Force`,或使用 cmd 分步创建 `mkdir parent && mkdir parent\\child`。+**特别注意**:Windows 的 cmd/PowerShell `mkdir` 不支持 `-p` 参数;只有在 Shell 能力显示 Git Bash/PATH bash 可用且实际使用 bash/Git Bash 时,`mkdir -p` 才是合适的。如需在 cmd/PowerShell 中创建嵌套目录,请使用 PowerShell `New-Item -ItemType Directory -Path "parent/child" -Force`,或使用 cmd 分步创建 `mkdir parent && mkdir parent\\child`。## 输出文件放置规范执行用户任务时产生的生成产物(如代码文件、文档、数据文件等),若用户未指定存放位置,请遵循以下规则:@@ -206,6 +210,8 @@ Be careful with your configuration. If changes are required, remember to restartCurrent platform: `{os_type}`+{shell_env_prompt}+**Important**: You MUST strictly use command syntax matching the current platform. Never use command formats from other platforms.Common command differences:@@ -219,7 +225,7 @@ Common command differences:| Delete directory | `rmdir folder` or PowerShell `Remove-Item -Recurse folder` | `rm -rf folder` || Find file | `dir /s pattern` or PowerShell `Get-ChildItem -Recurse -Filter pattern` | `find . -name pattern` |-**WARNING**: Windows `mkdir` does NOT support the `-p` flag! Using `mkdir -p folder` on Windows will incorrectly create a directory named `-p`. To create nested directories on Windows, use either PowerShell `New-Item -ItemType Directory -Path "parent/child" -Force` or cmd with step-by-step creation `mkdir parent && mkdir parent\\child`.+**WARNING**: Windows cmd/PowerShell `mkdir` does NOT support the `-p` flag; `mkdir -p` is appropriate only when Shell capabilities show Git Bash/PATH bash is available and you are actually using bash/Git Bash. To create nested directories in cmd/PowerShell, use either PowerShell `New-Item -ItemType Directory -Path "parent/child" -Force` or cmd with step-by-step creation `mkdir parent && mkdir parent\\child`.## Output File PlacementGenerated artifacts (code files, documents, data files, etc.) produced during user task execution should follow these placement rules unless the user specifies otherwise:diff --git a/jiuwenswarm/agents/harness/common/prompt/shell_environment.py b/jiuwenswarm/agents/harness/common/prompt/shell_environment.pynew file mode 100644index 0000000000..20c8dbe233--- /dev/null+++ b/jiuwenswarm/agents/harness/common/prompt/shell_environment.py@@ -0,0 +1,157 @@+# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.+"""Build dynamic shell environment prompt fragments."""++from __future__ import annotations++import os+import shutil+from pathlib import Path+from typing import Iterable, Optional+++def _is_wsl_bash_path(path: str) -> bool:+ normalized = path.replace("/", "\\").lower()+ return (+ normalized.endswith("\\system32\\bash.exe")+ or normalized.endswith("\\sysnative\\bash.exe")+ or "\\windowsapps\\bash.exe" in normalized+ )+++def _existing_executable(path: Path) -> Optional[str]:+ try:+ if path.is_file():+ return str(path)+ except OSError:+ return None+ return None+++def _dedupe_paths(paths: Iterable[Path]) -> list[Path]:+ seen = set()+ result = []+ for path in paths:+ key = str(path).lower()+ if key in seen:+ continue+ seen.add(key)+ result.append(path)+ return result+++def _git_bash_candidates() -> list[Path]:+ candidates = []++ for env_name in ("GIT_BASH", "GIT_BASH_PATH"):+ env_value = os.environ.get(env_name)+ if env_value:+ candidates.append(Path(os.path.expandvars(env_value.strip('"'))).expanduser())++ for base_env in ("ProgramFiles", "ProgramFiles(x86)", "LocalAppData"):+ base_path = os.environ.get(base_env)+ if base_path:+ git_root = Path(base_path) / "Git"+ candidates.extend([+ git_root / "bin" / "bash.exe",+ git_root / "usr" / "bin" / "bash.exe",+ ])++ git_exe = shutil.which("git")+ if git_exe:+ git_path = Path(git_exe)+ for parent in git_path.parents:+ if parent.name.lower() == "git":+ candidates.extend([+ parent / "bin" / "bash.exe",+ parent / "usr" / "bin" / "bash.exe",+ ])+ break++ return _dedupe_paths(candidates)+++def _available_git_bash() -> Optional[str]:+ for candidate in _git_bash_candidates():+ executable = _existing_executable(candidate)+ if executable:+ return executable+ return None+++def _available_powershell() -> Optional[str]:+ for command in ("pwsh", "powershell"):+ executable = shutil.which(command)+ if executable:+ return executable++ system_root = os.environ.get("SystemRoot")+ if system_root:+ executable = _existing_executable(+ Path(system_root)+ / "System32"+ / "WindowsPowerShell"+ / "v1.0"+ / "powershell.exe"+ )+ if executable:+ return executable+ return None+++def _status(language: str, path: Optional[str]) -> str:+ if path:+ return f"可用,路径 `{path}`" if language == "cn" else f"available at `{path}`"+ return "不可用" if language == "cn" else "unavailable"+++def _path_bash_status(language: str, path: Optional[str]) -> str:+ if not path:+ return "不可用" if language == "cn" else "unavailable"+ if _is_wsl_bash_path(path):+ if language == "cn":+ return f"`{path}`(WSL stub,不作为 Git Bash 使用)"+ return f"`{path}` (WSL stub; do not treat it as Git Bash)"+ return f"可用,路径 `{path}`" if language == "cn" else f"available at `{path}`"+++def build_shell_environment_prompt(language: str, os_type: str) -> str:+ """Return shell capability and selection guidance for the current host."""+ path_bash = shutil.which("bash")++ if os_type.startswith("win"):+ powershell = _available_powershell()+ git_bash = _available_git_bash()+ if language == "cn":+ return f"""Shell 能力:+- PowerShell:{_status(language, powershell)}+- Git Bash:{_status(language, git_bash)}+- PATH bash:{_path_bash_status(language, path_bash)}++Shell 选择规则:+- Windows 且 Git Bash 可用,或 PATH bash 明确不是 WSL stub 时,可以使用 bash/Git Bash 执行 POSIX 命令,例如 `ls`、`grep`、`cat`、`mkdir -p`、bash 脚本。+- Windows 且 Git Bash 不可用、PATH bash 也不可用或只是 WSL stub 时,不要使用 POSIX 命令;优先使用 PowerShell 或 cmd。+- PowerShell cmdlet 不要包在 bash 里执行,应直接使用 PowerShell。"""+ return f"""Shell capabilities:+- PowerShell: {_status(language, powershell)}+- Git Bash: {_status(language, git_bash)}+- PATH bash: {_path_bash_status(language, path_bash)}++Shell selection rules:+- On Windows, when Git Bash is available, or PATH bash is clearly not a WSL stub, use bash/Git Bash for POSIX commands such as `ls`, `grep`, `cat`, `mkdir -p`, and bash scripts.+- On Windows, when Git Bash is unavailable and PATH bash is unavailable or only a WSL stub, do not use POSIX commands; prefer PowerShell or cmd.+- Do not wrap PowerShell cmdlets in bash; invoke PowerShell directly."""++ shell_path = shutil.which("bash") or shutil.which("sh")+ if language == "cn":+ return f"""Shell 能力:+- Bash/sh:{_status(language, shell_path)}++Shell 选择规则:+- Linux/macOS 通常使用 bash/sh 风格命令。+- 如果 bash/sh 不可用,使用当前平台可用的 shell 或专用工具。"""+ return f"""Shell capabilities:+- Bash/sh: {_status(language, shell_path)}++Shell selection rules:+- Linux/macOS usually use bash/sh-style commands.+- If bash/sh is unavailable, use the current platform's available shell or dedicated tools."""diff --git a/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.py b/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.pyindex d417fe3bed..3e215d5bd4 100644--- a/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.py@@ -17,6 +17,7 @@ import yamlfrom openjiuwen.core.single_agent.rail.base import AgentCallbackContextfrom openjiuwen.harness.prompts import PromptSectionfrom openjiuwen.harness.rails.base import DeepAgentRail+from jiuwenswarm.agents.harness.common.prompt.shell_environment import build_shell_environment_promptfrom jiuwenswarm.common.utils import get_config_dir, loggerfrom jiuwenswarm.common.utils import get_agent_workspace_dir@@ -210,6 +211,8 @@ class RuntimePromptRail(DeepAgentRail):shell_name = os.path.basename(shell_path) if shell_path else "unknown"import platform as platos_version = f"{plat.system()} {plat.release()}"+ env_language = "cn" if not self._force_english and self._language == "cn" else "en"+ shell_env_prompt = build_shell_environment_prompt(env_language, os_type)if not self._force_english and self._language == "cn":env_content = (@@ -217,6 +220,7 @@ class RuntimePromptRail(DeepAgentRail):f"- 当前运行平台:`{os_type}`\n"f"- Shell:{shell_name}\n"f"- OS 版本:{os_version}\n\n"+ f"{shell_env_prompt}\n\n""## 平台命令差异(仅在必须使用 shell 时参考)\n\n""以下命令差异仅适用于测试、构建、git、包管理、运行脚本等必须调用 shell 的场景。""文件读取、编辑、搜索仍应优先使用专用工具。\n\n"@@ -230,9 +234,11 @@ class RuntimePromptRail(DeepAgentRail):"| 查找文件 | `dir /s pattern` 或 PowerShell ""`Get-ChildItem -Recurse -Filter pattern` ""| `find . -name pattern` |\n\n"- "**特别注意**:Windows 的 `mkdir` 不支持 `-p` 参数!"- "在 Windows 上使用 `mkdir -p folder` 会错误创建名为 `-p` 的目录。"- "如需创建嵌套目录,请使用 PowerShell `New-Item -ItemType Directory -Path \"parent/child\" -Force`,"+ "**特别注意**:Windows 的 cmd/PowerShell `mkdir` 不支持 `-p` 参数;"+ "只有在 Shell 能力显示 Git Bash/PATH bash 可用且实际使用 bash/Git Bash 时,"+ "`mkdir -p` 才是合适的。"+ "如需在 cmd/PowerShell 中创建嵌套目录,请使用 PowerShell "+ "`New-Item -ItemType Directory -Path \"parent/child\" -Force`,""或使用 cmd 分步创建 `mkdir parent && mkdir parent\\child`。")else:@@ -241,6 +247,7 @@ class RuntimePromptRail(DeepAgentRail):f"- Current platform: `{os_type}`\n"f"- Shell: {shell_name}\n"f"- OS Version: {os_version}\n\n"+ f"{shell_env_prompt}\n\n""## Platform Command Differences (only when shell is required)\n\n""The following command differences apply only to scenarios where shell execution is required ""(testing, builds, git, package management, running scripts). "@@ -255,9 +262,10 @@ class RuntimePromptRail(DeepAgentRail):"| Find file | `dir /s pattern` or PowerShell ""`Get-ChildItem -Recurse -Filter pattern` ""| `find . -name pattern` |\n\n"- "**WARNING**: Windows `mkdir` does NOT support the `-p` flag! "- "Using `mkdir -p folder` on Windows will incorrectly create a directory named `-p`. "- "To create nested directories on Windows, use either PowerShell "+ "**WARNING**: Windows cmd/PowerShell `mkdir` does NOT support the `-p` flag; "+ "`mkdir -p` is appropriate only when Shell capabilities show Git Bash/PATH bash "+ "is available and you are actually using bash/Git Bash. "+ "To create nested directories in cmd/PowerShell, use either PowerShell ""`New-Item -ItemType Directory -Path \"parent/child\" -Force` ""or cmd with step-by-step creation `mkdir parent && mkdir parent\\\\child`.")