diff --git a/backend/app/hands/capabilities.py b/backend/app/hands/capabilities.py index 82b4a276..12f49304 100644 --- a/backend/app/hands/capabilities.py +++ b/backend/app/hands/capabilities.py @@ -91,6 +91,24 @@ def _can_launch_local_cdp_browser() -> bool: return False +def _has_terminal_shell() -> bool: + """Return whether the runtime has a shell suitable for terminal tools.""" + if os.name == "nt": + candidates = ( + "bash", + "pwsh", + "pwsh.exe", + "powershell", + "powershell.exe", + "cmd", + "cmd.exe", + ) + else: + candidates = ("bash", "sh") + + return any(shutil.which(candidate) is not None for candidate in candidates) + + @dataclass class BrainCapabilities: """ @@ -151,7 +169,7 @@ def detect_capabilities(config: dict | None = None) -> BrainCapabilities: logger.info("Brain running in Docker, using limited capabilities") deployment = "docker" caps = BrainCapabilities( - has_terminal=shutil.which("bash") is not None, + has_terminal=_has_terminal_shell(), has_browser=False, filesystem_scope="workspace_only", mcp_mode="all", # MCP available in all deployment modes @@ -174,7 +192,7 @@ def detect_capabilities(config: dict | None = None) -> BrainCapabilities: "not running under Electron host, and no launchable browser found." ) caps = BrainCapabilities( - has_terminal=shutil.which("bash") is not None, + has_terminal=_has_terminal_shell(), has_browser=has_browser, filesystem_scope="full", mcp_mode="all", @@ -188,7 +206,7 @@ def detect_capabilities(config: dict | None = None) -> BrainCapabilities: else: # sandbox / docker / container -> limited capabilities caps = BrainCapabilities( - has_terminal=shutil.which("bash") is not None, + has_terminal=_has_terminal_shell(), has_browser=False, filesystem_scope="workspace_only", mcp_mode="all", # MCP available in all deployment modes diff --git a/backend/tests/app/hands/test_capabilities.py b/backend/tests/app/hands/test_capabilities.py new file mode 100644 index 00000000..1926c468 --- /dev/null +++ b/backend/tests/app/hands/test_capabilities.py @@ -0,0 +1,68 @@ +# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= + +import pytest + +from app.hands import capabilities +from app.hands.environment_hands import EnvironmentHands + + +@pytest.mark.unit +def test_has_terminal_shell_accepts_windows_powershell(monkeypatch): + monkeypatch.setattr(capabilities.os, "name", "nt") + + def fake_which(command: str) -> str | None: + if command == "powershell.exe": + return ( + "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\" + "powershell.exe" + ) + return None + + monkeypatch.setattr(capabilities.shutil, "which", fake_which) + + assert capabilities._has_terminal_shell() is True + + +@pytest.mark.unit +def test_has_terminal_shell_accepts_posix_sh(monkeypatch): + monkeypatch.setattr(capabilities.os, "name", "posix") + + def fake_which(command: str) -> str | None: + if command == "sh": + return "/bin/sh" + return None + + monkeypatch.setattr(capabilities.shutil, "which", fake_which) + + assert capabilities._has_terminal_shell() is True + + +@pytest.mark.unit +def test_detect_capabilities_uses_terminal_shell_probe(monkeypatch): + monkeypatch.setattr(capabilities, "_is_running_in_docker", lambda: False) + monkeypatch.setattr(capabilities, "_probe_cdp_browser", lambda: False) + monkeypatch.setattr(capabilities, "_is_electron_runtime", lambda: False) + monkeypatch.setattr( + capabilities, "_can_launch_local_cdp_browser", lambda: False + ) + monkeypatch.setattr(capabilities, "env", lambda _, default=None: default) + monkeypatch.setattr(capabilities.shutil, "which", lambda _: None) + monkeypatch.setattr(capabilities, "_has_terminal_shell", lambda: True) + + caps = capabilities.detect_capabilities() + hands = EnvironmentHands(caps) + + assert caps.has_terminal is True + assert hands.can_execute_terminal() is True