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
[mirror] openJiuwen-ai/jiuwenswarm#1085: fix:生成多余-p文件夹
d30029696c254a6233e6f
diff --git a/jiuwenswarm/agents/harness/common/tools/command_tools.py b/jiuwenswarm/agents/harness/common/tools/command_tools.pyindex f26bf157b1..64319e1e38 100644--- a/jiuwenswarm/agents/harness/common/tools/command_tools.py+++ b/jiuwenswarm/agents/harness/common/tools/command_tools.py@@ -209,6 +209,65 @@ _QUOTED_WINDOWS_PATH_PATTERN = re.compile(r"(?P<quote>['\"])(?P<path>[A-Za-z]:\\_UNQUOTED_WINDOWS_PATH_PATTERN = re.compile(r"(?<![\w/])(?P<path>[A-Za-z]:\\[^\s|&;]+)")+def _translate_mkdir_p_to_powershell(segment: str) -> str | None:+ try:+ tokens = shlex.split(segment, posix=False)+ except ValueError:+ return None+ if not tokens:+ return None+ base = _strip_matching_quotes(tokens[0]).rsplit("/", maxsplit=1)[-1].rsplit("\\", maxsplit=1)[-1].lower()+ if base.endswith(".exe"):+ base = base[:-4]+ if base != "mkdir":+ return None+ has_p = False+ paths: list[str] = []+ other_flags: list[str] = []+ for tok in tokens[1:]:+ stripped = _strip_matching_quotes(tok)+ if stripped == "-p" or stripped == "--parents":+ has_p = True+ elif stripped.startswith("-"):+ other_flags.append(stripped)+ else:+ paths.append(tok)+ if not has_p:+ return None+ if other_flags:+ return None+ if not paths:+ return None+ items = []+ for p in paths:+ bare = _strip_matching_quotes(p)+ escaped = bare.replace("'", "''")+ items.append(f"New-Item -ItemType Directory -Path '{escaped}' -Force")+ return "; ".join(items)+++def _translate_posix_segment_for_powershell(segment: str) -> str | None:+ try:+ tokens = shlex.split(segment, posix=False)+ except ValueError:+ return None+ if not tokens:+ return None+ base = _strip_matching_quotes(tokens[0]).rsplit("/", maxsplit=1)[-1].rsplit("\\", maxsplit=1)[-1].lower()+ if base.endswith(".exe"):+ base = base[:-4]+ if base == "mkdir":+ return _translate_mkdir_p_to_powershell(segment)+ return None+++def _translate_posix_for_powershell(command: str) -> str | None:+ segments = _split_shell_segments(command)+ if len(segments) != 1:+ return None+ return _translate_posix_segment_for_powershell(segments[0])++def _clip_text(value: str, max_chars: int) -> str:if max_chars <= 0 or len(value) <= max_chars:return value@@ -560,6 +619,12 @@ def _resolve_execution_plan(command: str, shell_type: str) -> tuple[list[str] |exe = _available_bash(allow_wsl=False)if exe:return [exe, "-lc", _normalize_windows_paths_for_bash(command)], False, "bash"+ translated = _translate_posix_for_powershell(command)+ ps_exe = _available_powershell()+ if translated is not None and ps_exe:+ return [ps_exe, "-NoProfile", "-NonInteractive", "-Command", translated], False, "powershell"+ if ps_exe:+ return [ps_exe, "-NoProfile", "-NonInteractive", "-Command", command], False, "powershell"normalized = "cmd"if normalized == "powershell":exe = _available_powershell()diff --git a/tests/unit_tests/agents/test_command_tools_posix_windows.py b/tests/unit_tests/agents/test_command_tools_posix_windows.pynew file mode 100644index 0000000000..6a01a8fe74--- /dev/null+++ b/tests/unit_tests/agents/test_command_tools_posix_windows.py@@ -0,0 +1,221 @@+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.++from __future__ import annotations++import os++import pytest++from jiuwenswarm.agents.harness.common.tools.command_tools import (+ _looks_like_posix,+ _resolve_execution_plan,+ _split_shell_segments,+ _translate_mkdir_p_to_powershell,+ _translate_posix_for_powershell,+ _translate_posix_segment_for_powershell,+ _available_bash,+ _available_powershell,+)+++# ── _translate_mkdir_p_to_powershell ────────────────────────────────+++class TestTranslateMkdirP:+ @pytest.mark.parametrize(+ "command, expected",+ [+ ("mkdir -p dir", "New-Item -ItemType Directory -Path 'dir' -Force"),+ ("mkdir -p a/b/c", "New-Item -ItemType Directory -Path 'a/b/c' -Force"),+ ("mkdir -p 'path with spaces'", "New-Item -ItemType Directory -Path 'path with spaces' -Force"),+ ("mkdir -p a b c", "New-Item -ItemType Directory -Path 'a' -Force; New-Item -ItemType Directory -Path 'b' -Force; New-Item -ItemType Directory -Path 'c' -Force"),+ ("mkdir --parents dir", "New-Item -ItemType Directory -Path 'dir' -Force"),+ ("mkdir -p dir/subdir", "New-Item -ItemType Directory -Path 'dir/subdir' -Force"),+ ],+ )+ def test_translates_mkdir_p(self, command: str, expected: str) -> None:+ assert _translate_mkdir_p_to_powershell(command) == expected++ @pytest.mark.parametrize(+ "command",+ [+ "mkdir dir",+ "mkdir -p",+ "mkdir -p dir -v",+ "mkdir -p dir -m 755",+ "ls -la",+ "cat file.txt",+ "",+ ],+ )+ def test_returns_none_for_untranslatable(self, command: str) -> None:+ assert _translate_mkdir_p_to_powershell(command) is None++ @pytest.mark.parametrize(+ "command, expected",+ [+ ("mkdir -p 'path with \"quotes\"'", "New-Item -ItemType Directory -Path 'path with \"quotes\"' -Force"),+ ('mkdir -p "$(Start-Process calc)"', "New-Item -ItemType Directory -Path '$(Start-Process calc)' -Force"),+ ('mkdir -p "$env:TEMP"', "New-Item -ItemType Directory -Path '$env:TEMP' -Force"),+ ("mkdir -p \"it's here\"", "New-Item -ItemType Directory -Path 'it''s here' -Force"),+ ],+ )+ def test_injection_vectors_neutralized(self, command: str, expected: str) -> None:+ assert _translate_mkdir_p_to_powershell(command) == expected+++# ── _translate_posix_segment_for_powershell ─────────────────────────+++class TestTranslatePosixSegment:+ def test_translates_mkdir_p_segment(self) -> None:+ assert _translate_posix_segment_for_powershell("mkdir -p dir") is not None++ def test_returns_none_for_ls(self) -> None:+ assert _translate_posix_segment_for_powershell("ls -la") is None++ def test_returns_none_for_empty(self) -> None:+ assert _translate_posix_segment_for_powershell("") is None++ def test_returns_none_for_mkdir_without_p(self) -> None:+ assert _translate_posix_segment_for_powershell("mkdir dir") is None+++# ── _translate_posix_for_powershell ─────────────────────────────────+++class TestTranslatePosixForPowershell:+ def test_translates_single_mkdir_p(self) -> None:+ result = _translate_posix_for_powershell("mkdir -p dir")+ assert result == "New-Item -ItemType Directory -Path 'dir' -Force"++ @pytest.mark.parametrize(+ "command",+ [+ "mkdir -p dir && echo done",+ "mkdir -p dir; ls",+ "ls -la",+ "echo hello",+ ],+ )+ def test_returns_none_for_compound_or_untranslatable(self, command: str) -> None:+ assert _translate_posix_for_powershell(command) is None+++# ── _looks_like_posix ───────────────────────────────────────────────+++class TestLooksLikePosix:+ @pytest.mark.parametrize(+ "command",+ [+ "mkdir -p dir",+ "ls -la",+ "cat file.txt",+ "grep pattern file",+ "find . -name '*.py'",+ "rm -rf dir",+ "touch file.txt",+ "chmod 755 script.sh",+ "ls && cat file.txt",+ "mkdir dir; ls",+ ],+ )+ def test_detects_posix_commands(self, command: str) -> None:+ assert _looks_like_posix(command) is True++ @pytest.mark.parametrize(+ "command",+ [+ "echo hello",+ "python script.py",+ "git status",+ "npm install",+ "dir /s pattern",+ "powershell Get-ChildItem",+ ],+ )+ def test_rejects_non_posix_commands(self, command: str) -> None:+ assert _looks_like_posix(command) is False++ def test_empty_command(self) -> None:+ assert _looks_like_posix("") is False+++# ── _resolve_execution_plan on Windows ──────────────────────────────+++class TestResolveExecutionPlanWindows:+ @pytest.fixture(autouse=True)+ def _require_windows(self) -> None:+ if os.name != "nt":+ pytest.skip("Windows-only tests")++ @pytest.fixture()+ def _no_bash(self, monkeypatch) -> None:+ monkeypatch.setattr(+ "jiuwenswarm.agents.harness.common.tools.command_tools._available_bash",+ lambda **kw: None,+ )++ @pytest.fixture()+ def _with_bash(self, monkeypatch) -> None:+ monkeypatch.setattr(+ "jiuwenswarm.agents.harness.common.tools.command_tools._available_bash",+ lambda **kw: "C:\\Program Files\\Git\\bin\\bash.exe",+ )++ def test_mkdir_p_translated_to_powershell_when_no_bash(self, _no_bash) -> None:+ plan, use_shell, resolved_shell = _resolve_execution_plan("mkdir -p dir", "auto")+ assert resolved_shell == "powershell"+ assert use_shell is False+ assert "New-Item" in plan[-1]+ assert "-ItemType Directory" in plan[-1]++ def test_mkdir_p_uses_bash_when_available(self, _with_bash) -> None:+ plan, use_shell, resolved_shell = _resolve_execution_plan("mkdir -p dir", "auto")+ assert resolved_shell == "bash"+ assert use_shell is False++ def test_ls_routes_to_powershell_when_no_bash(self, _no_bash) -> None:+ plan, use_shell, resolved_shell = _resolve_execution_plan("ls", "auto")+ assert resolved_shell == "powershell"+ assert use_shell is False++ def test_non_posix_stays_on_cmd(self, _no_bash) -> None:+ plan, use_shell, resolved_shell = _resolve_execution_plan("echo hello", "auto")+ assert resolved_shell == "cmd"+ assert use_shell is True++ def test_compound_posix_routes_to_powershell_raw_when_no_bash(self, _no_bash) -> None:+ plan, use_shell, resolved_shell = _resolve_execution_plan("mkdir -p dir && echo done", "auto")+ assert resolved_shell == "powershell"+ assert use_shell is False++ def test_mkdir_without_p_routes_to_powershell_when_no_bash(self, _no_bash) -> None:+ plan, use_shell, resolved_shell = _resolve_execution_plan("mkdir dir", "auto")+ assert resolved_shell == "powershell"+ assert use_shell is False++ def test_mkdir_p_nested_path_translated(self, _no_bash) -> None:+ plan, use_shell, resolved_shell = _resolve_execution_plan("mkdir -p output/slides", "auto")+ assert resolved_shell == "powershell"+ assert "New-Item" in plan[-1]+ assert "output/slides" in plan[-1]++ def test_mkdir_p_with_spaces_translated(self, _no_bash) -> None:+ plan, use_shell, resolved_shell = _resolve_execution_plan("mkdir -p \"path with spaces\"", "auto")+ assert resolved_shell == "powershell"+ assert "New-Item" in plan[-1]+ assert "path with spaces" in plan[-1]+++class TestResolveExecutionPlanNonWindows:+ @pytest.fixture(autouse=True)+ def _require_non_windows(self) -> None:+ if os.name == "nt":+ pytest.skip("Non-Windows-only tests")++ def test_auto_routes_to_bash(self) -> None:+ plan, use_shell, resolved_shell = _resolve_execution_plan("mkdir -p dir", "auto")+ assert resolved_shell in ("bash", "sh")