agent-Specialization/modules/terminal_ops/misc.py
JOJO 42cd99d2d8 feat(windows): WSL2 沙箱执行环境与执行环境提示词平台拆分
- Windows 宿主机模式基于 WSL2 实现沙箱执行:只读/批准/自动审核识别、
  工作区可写+区外只读挂载、网络档位(受限/开放/禁止)、敏感目录屏蔽
- 新增 scripts/setup-wsl-sandbox.ps1 一键准备 WSL 沙箱发行版
- 执行环境提示词按平台拆分骨架:prompts/execution_mode/macos.txt
  (原 execution_mode.txt,mac/dockerweb 沿用)与 windows.txt
  (骨架+动态注入环境与路径、当前规则、网络档位、切换一致性说明)
- Windows 执行环境切换通知改为完整命令写法说明(双向),mac 保持原样
- 附调研与 PoC 验证报告:wsl2-sandbox-research.md、
  windows-sandbox-research.md、wsl2-sandbox-poc-report.md
2026-07-30 13:06:34 +08:00

77 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# modules/terminal_ops.py - 终端操作模块修复Python命令检测
import os
import sys
import asyncio
import subprocess
import shutil
import time
import signal
import re
from pathlib import Path
from typing import Dict, Optional, Tuple, TYPE_CHECKING
from types import SimpleNamespace
try:
from config import (
TERMINAL_COMMAND_TIMEOUT,
FORBIDDEN_COMMANDS,
OUTPUT_FORMATS,
MAX_RUN_COMMAND_CHARS,
TOOLBOX_TERMINAL_IDLE_SECONDS,
HOST_SANDBOX_NETWORK_PERMISSION,
)
except ImportError:
project_root = Path(__file__).resolve().parents[1]
if str(project_root) not in sys.path:
sys.path.insert(0, str(project_root))
from config import (
TERMINAL_COMMAND_TIMEOUT,
FORBIDDEN_COMMANDS,
OUTPUT_FORMATS,
MAX_RUN_COMMAND_CHARS,
TOOLBOX_TERMINAL_IDLE_SECONDS,
HOST_SANDBOX_NETWORK_PERMISSION,
)
from modules.toolbox_container import ToolboxContainer
from modules.host_sandbox_runner import (
HostSandboxError,
NETWORK_PERMISSION_RESTRICTED,
build_host_sandbox_plan,
build_host_sandbox_readonly_plan,
host_sandbox_enabled,
)
if TYPE_CHECKING:
from modules.user_container_manager import ContainerHandle
from modules.terminal_manager import TerminalManager
class MiscMixin:
"""TerminalOperator misc 能力 mixin。"""
def kill_process(self):
"""终止当前运行的进程"""
if self.process and self.process.returncode is None:
self.process.kill()
print(f"{OUTPUT_FORMATS['warning']} 进程已终止")
@staticmethod
def _materialize_seccomp_fd(plan_command: list[str], seccomp_path: Optional[str]) -> tuple[list[str], tuple[int, ...], Optional[int]]:
if not seccomp_path:
return plan_command, tuple(), None
seccomp_fd = os.open(seccomp_path, os.O_RDONLY)
fd_num = seccomp_fd
cmd = [str(fd_num) if token == "__SECCOMP_FD__" else token for token in plan_command]
return cmd, (fd_num,), seccomp_fd
@staticmethod
def _filter_ignored_stderr_lines(text: str, patterns: list[str]) -> str:
"""按行过滤 stderr 中匹配指定模式的噪音(如 wsl.exe 的 localhost 代理警告)。"""
if not text or not patterns:
return text
kept = [
line for line in text.splitlines(keepends=True)
if not any(re.search(pattern, line) for pattern in patterns)
]
return "".join(kept)