Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(bot): translate POSIX compile sandbox commands on Windows (#6372)
Zhanran Wangfeb051c70666
diff --git a/bot/tests/test_direct_windows_posix.py b/bot/tests/test_direct_windows_posix.pynew file mode 100644index 000000000..407f12b68--- /dev/null+++ b/bot/tests/test_direct_windows_posix.py@@ -0,0 +1,131 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0+"""Regression tests for the Windows POSIX shim used by Compile sandboxes."""++import asyncio+import shlex++import pytest+from vikingbot.compile.sandbox import _CompileSandbox+from vikingbot.config.schema import Config+from vikingbot.sandbox.backends import direct+++class _FakeProcess:+ returncode = 0++ async def communicate(self):+ return b"ok", b""+++async def _acl_ok(path, *, directory):+ return None+++def windows_backend(tmp_path, monkeypatch):+ """A DirectBackend forced through the Windows code path with a stubbed shell."""+ monkeypatch.setattr(direct.os, "name", "nt")+ calls = []++ async def fake_create_subprocess_shell(command, **kwargs):+ calls.append((command, kwargs.get("env")))+ return _FakeProcess()++ monkeypatch.setattr(asyncio, "create_subprocess_shell", fake_create_subprocess_shell)+ backend = direct.DirectBackend(Config().sandbox, "test", tmp_path)+ backend._running = True+ return backend, calls+++async def test_initialize_handles_posix_quoted_paths(tmp_path, monkeypatch):+ workspace = tmp_path / "Anne O'Brien & R&D work"+ workspace.mkdir()++ class Backend:+ sandbox_cwd = str(workspace)++ async def execute(self, command, timeout=60, **kwargs):+ return await direct._translate_windows_posix(command, self.sandbox_cwd)++ async def write_file(self, path, content):+ (workspace / path).write_text(content, encoding="utf-8")++ @staticmethod+ def _ensure_command_succeeded(output, operation):+ direct.DirectBackend._ensure_command_succeeded(output, operation)++ monkeypatch.setattr(direct, "_restrict_windows_acl", _acl_ok)+ sandbox = _CompileSandbox(Backend())+ await sandbox.initialize({"url": "http://127.0.0.1:1933"})++ credential_dir = workspace / sandbox._directory+ assert credential_dir.is_dir()+ assert (credential_dir / "ovcli.conf").is_file()+++async def test_initialize_and_execute_use_child_env(windows_backend, monkeypatch):+ backend, calls = windows_backend+ monkeypatch.setenv("OPENVIKING_URL", "http://host-override")+ monkeypatch.setattr(direct, "_restrict_windows_acl", _acl_ok)+ sandbox = _CompileSandbox(backend)++ await sandbox.initialize({"url": "http://127.0.0.1:1933"})+ result = await sandbox.execute('python -c "print(123)"')++ assert result == "ok"+ command, env = calls[-1]+ assert command == 'python -c "print(123)"'+ assert "unset" not in command and "OPENVIKING_CLI_CONFIG_FILE=" not in command+ assert env["OPENVIKING_CLI_CONFIG_FILE"] == sandbox._config_path+ assert "OPENVIKING_URL" not in env+++async def test_direct_execute_moves_env_prefix_into_child_env(windows_backend):+ backend, calls = windows_backend+ command = (+ "unset OPENVIKING_URL OPENVIKING_API_KEY; "+ "export OPENVIKING_CLI_CONFIG_FILE='C:\\work\\ovcli.conf'; "+ 'python -c "print(123)"'+ )++ result = await backend.execute(command)++ assert result == "ok"+ captured, env = calls[-1]+ assert captured == 'python -c "print(123)"'+ assert env["OPENVIKING_CLI_CONFIG_FILE"] == r"C:\work\ovcli.conf"+ assert "OPENVIKING_URL" not in env and "OPENVIKING_API_KEY" not in env+++async def test_chmod_only_supports_exact_form(tmp_path, monkeypatch):+ target = tmp_path / "ovcli.conf"+ target.write_text("{}", encoding="utf-8")+ monkeypatch.setattr(direct, "_restrict_windows_acl", _acl_ok)+ quoted = shlex.quote(str(target))++ result = await direct._translate_windows_posix(f"chmod 600 {quoted}", str(tmp_path))+ assert result == "(no output)"+ assert await direct._translate_windows_posix(f"chmod 000 {quoted}", str(tmp_path)) is None+ assert (+ await direct._translate_windows_posix(f"chmod 600 {quoted} {quoted}", str(tmp_path)) is None+ )+ assert await direct._translate_windows_posix(f"chmod -R 600 {quoted}", str(tmp_path)) is None+++async def test_mkdir_rejects_unsupported_flags(tmp_path):+ target = shlex.quote(str(tmp_path / "credentials"))++ result = await direct._translate_windows_posix(f"mkdir -p {target}", str(tmp_path))+ assert result == "(no output)"+ assert await direct._translate_windows_posix(f"mkdir -m 755 -- {target}", str(tmp_path)) is None+ assert (+ await direct._translate_windows_posix(f"mkdir -p {target} && echo ok", str(tmp_path))+ is None+ )+ assert await direct._translate_windows_posix("echo hi", str(tmp_path)) is Nonediff --git a/bot/vikingbot/sandbox/backends/direct.py b/bot/vikingbot/sandbox/backends/direct.pyindex 6c8714922..b6ce49b8b 100644--- a/bot/vikingbot/sandbox/backends/direct.py+++ b/bot/vikingbot/sandbox/backends/direct.py@@ -2,6 +2,8 @@import asyncioimport os+import re+import shleximport signalfrom pathlib import Pathfrom typing import Any@@ -12,6 +14,175 @@ from vikingbot.config.schema import SandboxConfig, SessionKeyfrom vikingbot.sandbox.backends import register_backendfrom vikingbot.sandbox.base import SandboxBackend+# On Windows the shell path uses COMSPEC (cmd.exe), which cannot parse the POSIX+# commands Compile runs: `mkdir -m 700 --`, `chmod 600`, and the+# `unset ...; export OPENVIKING_CLI_CONFIG_FILE=...;` prefix. The prefix is moved+# into a per-invocation child environment for every command; only the fixed+# mkdir/chmod forms are executed natively. Every other command still runs through+# the shell with that child environment.+_ENV_PREFIX = re.compile(+ r"^unset\s+([^;]*);\s*export\s+([A-Za-z_][A-Za-z0-9_]*)=(.*?);\s*(.+)$",+ re.DOTALL,+)+_CHMOD_MODE = "600"+_MKDIR_RESTRICTED_MODE = "700"+++def _decode_posix_value(raw: str) -> str:+ """Decode a single POSIX-quoted shell value emitted by ``shlex.quote``."""+ try:+ parts = shlex.split(raw, posix=True)+ except ValueError:+ return raw.strip()+ return parts[0] if len(parts) == 1 else raw.strip()+++def _prepare_windows_shell(command: str) -> tuple[str, dict[str, str] | None]:+ """Move the Compile env prefix into a child env; return (command, child_env)."""+ match = _ENV_PREFIX.match(command)+ if match is None:+ return command, None+ unset_names, name, raw_value, remaining = match.groups()+ child_env = dict(os.environ)+ for key in unset_names.split():+ child_env.pop(key, None)+ child_env[name] = _decode_posix_value(raw_value)+ return remaining.strip(), child_env+++def _has_unquoted_shell_control(command: str) -> bool:+ """Return True when a shell control character appears outside quotes."""+ quote = ""+ index = 0+ while index < len(command):+ char = command[index]+ if not quote:+ if char in "'\"":+ quote = char+ elif char in ";&|<>`":+ return True+ elif char == "$" and index + 1 < len(command) and command[index + 1] == "(":+ return True+ elif quote == "'":+ if char == "'":+ quote = ""+ elif char == "\\":+ index += 2+ continue+ elif char == '"':+ quote = ""+ index += 1+ return False+++def _split_posix_command(command: str) -> list[str] | None:+ """Split a POSIX-quoted command while rejecting unquoted shell operators."""+ if not command or _has_unquoted_shell_control(command):+ return None+ try:+ tokens = shlex.split(command, posix=True)+ except ValueError:+ return None+ return tokens or None+++def _resolve_windows_path(target: str, base: str) -> str:+ """Resolve a sandbox path against the command working directory."""+ return target if os.path.isabs(target) else os.path.join(base, target)+++async def _restrict_windows_acl(path: str, *, directory: bool) -> str | None:+ """Approximate POSIX 0700/0600 with an owner/SYSTEM/Administrators NTFS ACL.++ Inherited entries are removed first (for example sandbox helper groups), then+ explicit grants are applied. Returns error text on failure, None on success.+ """+ user = os.environ.get("USERNAME") or ""+ domain = os.environ.get("USERDOMAIN") or ""+ principal = f"{domain}\\{user}" if domain and user else user+ if not principal:+ return "STDERR:\nUSERNAME is not set\nExit code: 1"+ suffix = ":(OI)(CI)F" if directory else ":F"+ grants = [principal + suffix, "*S-1-5-18" + suffix, "*S-1-5-32-544" + suffix]+ arguments = ["icacls", path, "/inheritance:r"]+ for grant in grants:+ arguments += ["/grant:r", grant]+ process = await asyncio.create_subprocess_exec(+ *arguments,+ stdin=asyncio.subprocess.DEVNULL,+ stdout=asyncio.subprocess.PIPE,+ stderr=asyncio.subprocess.PIPE,+ )+ _stdout, stderr = await process.communicate()+ if process.returncode != 0:+ detail = stderr.decode("utf-8", errors="replace")+ return f"STDERR:\n{detail}Exit code: {process.returncode}"+ return None+++def _mkdir_operands(tokens: list[str]) -> tuple[list[str], bool] | None:+ """Validate ``mkdir`` flags and return (paths, restricted); None if unsupported."""+ paths: list[str] = []+ restricted = False+ index = 1+ while index < len(tokens):+ token = tokens[index]+ if token == "--":+ paths.extend(tokens[index + 1 :])+ break+ if token == "-p":+ index += 1+ continue+ if token == "-m":+ if index + 1 >= len(tokens) or tokens[index + 1] != _MKDIR_RESTRICTED_MODE:+ return None+ restricted = True+ index += 2+ continue+ if token.startswith("-"):+ return None+ paths.append(token)+ index += 1+ if not paths:+ return None+ return paths, restricted+++async def _translate_windows_posix(command: str, cwd: str) -> str | None:+ """Run the fixed POSIX forms Compile needs; return None to fall back to the shell."""+ tokens = _split_posix_command(command)+ if tokens is None:+ return None+ try:+ if tokens[0] == "chmod":+ # Only the exact `chmod 600 <file>` form is implemented.+ if len(tokens) != 3 or tokens[1] != _CHMOD_MODE:+ return None+ target = _resolve_windows_path(tokens[2], cwd)+ if not os.path.isfile(target):+ return (+ f"STDERR:\nchmod: cannot access '{target}': "+ "No such file or directory\nExit code: 1"+ )+ error = await _restrict_windows_acl(target, directory=False)+ return error if error else "(no output)"+ if tokens[0] == "mkdir":+ operands = _mkdir_operands(tokens)+ if operands is None:+ return None+ paths, restricted = operands+ for path in paths:+ absolute = _resolve_windows_path(path, cwd)+ os.makedirs(absolute, exist_ok=True)+ if restricted:+ error = await _restrict_windows_acl(absolute, directory=True)+ if error:+ return error+ return "(no output)"+ except OSError as exc:+ return f"STDERR:\n{exc}\nExit code: 1"+ return None+@register_backend("direct")class DirectBackend(SandboxBackend):@@ -40,6 +211,14 @@ class DirectBackend(SandboxBackend):cwd = kwargs.get("working_dir", str(self._workspace))+ child_env = None+ if os.name == "nt":+ command, child_env = _prepare_windows_shell(command)+ translated = await _translate_windows_posix(command, cwd)+ if translated is not None:+ logger.info("[Direct] Windows POSIX command translated: {}", repr(command))+ return translated+try:process = await asyncio.create_subprocess_shell(command,@@ -47,6 +226,7 @@ class DirectBackend(SandboxBackend):stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE,cwd=cwd,+ env=child_env,start_new_session=os.name == "posix",)