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
feat(bot): manage Docker-backed OpenSandbox lifecycle and persistent workspaces (#5269)
DuTao5fef1fbb514e
diff --git a/bot/README.md b/bot/README.mdindex 9f19f0f84..f9f71321d 100644--- a/bot/README.md+++ b/bot/README.md@@ -355,6 +355,9 @@ When `storage.workspace` is omitted, the default is `~/.openviking/data/bot/workvikingbot status```+With managed OpenSandbox (`backend=opensandbox`, `managed=true`), the active workspace root is+`<storage.workspace>/bot/runtime/opensandbox/workspaces`, bind-mounted into containers as described below.+The active directory used by the Agent also depends on `bot.sandbox.mode`:| Mode | Active Workspace |@@ -491,6 +494,82 @@ DirectBackend defaults to `restrict_to_workspace: false`. For a Gateway exposed}```+### Managed Docker sandboxes for Gateway++The default remains `direct`: no Docker checks or OpenSandbox service are started. To opt in,+install and start Docker (Docker Desktop on macOS; Docker Desktop with WSL2 integration on Windows),+then set `bot.sandbox.backend` to `opensandbox` and optionally `bot.sandbox.mode` to `per-session`+in the `ov.conf` used by Gateway.++Both `vikingbot gateway --config /path/to/ov.conf` and OpenViking `--with-bot` check dependencies,+prepare images, generate a private Server configuration and API key, start OpenSandbox, and verify+command execution plus file round trips **before accepting requests**. Failure aborts startup;+there is no fallback to Direct. `--with-bot` waits for readiness rather than process existence.++Generated configuration and logs live in `{storage.workspace}/bot/runtime/opensandbox/gateway-*/`.+The generated credential-bearing configuration is removed on normal shutdown; logs are retained.+There is no need to edit `~/.sandbox.toml`.+The managed service also restricts Docker-published sandbox ports to `127.0.0.1`, adapting+OpenSandbox Server 0.1.6's all-interface bindings without modifying external Servers.++Settings under `bot.sandbox.backends.opensandbox`:++| Setting | Default | Purpose |+|---------|---------|---------|+| `managed` | `true` | Manage a local Docker-backed Server; `false` connects to an external service |+| `server_url` | `http://localhost:18792` | Local HTTP endpoint in managed mode; port is configurable |+| `api_key` | empty | Generated in managed mode; supply the external service's key otherwise |+| `startup_timeout` | `600` | Total startup timeout in seconds, including first-time image pulls |+| `use_server_proxy` | `true` | Access sandbox endpoints through the Server |+| `default_image` | `opensandbox/code-interpreter:v1.0.1` | Workload image; needs shell and Python 3 |+| `execd_image` | `opensandbox/execd:v1.0.6` | Execution daemon image |+| `egress_image` | `opensandbox/egress:v1.0.1` | Network policy sidecar image |+| `pids_limit` | `256` | Managed Docker sandbox process limit |+| `runtime.cpu` / `runtime.memory` | `500m` / `1Gi` | Per-sandbox resource limits |+| `runtime.timeout` | `300` | Sandbox lifetime in seconds, renewed on use |+| `network.allowed_domains` / `network.denied_domains` | `[]` / `[]` | Default-deny egress; deny rules precede allow rules |++Managed sandboxes use bridge networking, dropped capabilities and no-new-privileges. Each container+bind-mounts only its dedicated host workspace at `/workspace`, with read/write access:++The workload container, including execd, runs as the workspace owner's numeric UID/GID with+`HOME=/workspace`. This supports ordinary Linux users' `0755` directories and `0644` files without+relaxing permissions or restoring `CAP_DAC_OVERRIDE`. Egress and image-cache containers keep their+own execution identities.++```text+{storage.workspace}/bot/runtime/opensandbox/+├── gateway-*/ # Server configuration and logs; never mounted+└── workspaces/+ ├── shared/ # shared mode → container /workspace+ ├── <session/channel-key>/ # one workspace per session/channel+ └── compile/<task-id>/ # isolated Compile task workspaces+```++Files are directly visible in Finder and edits from either side affect the same directory.+Chat workspace files survive container destruction and are reused on recreation. Bootstrap files+and enabled local Skills are initialized only when the directory is first created; user edits are+preserved thereafter. Compile task directories retain their existing cleanup lifecycle. Bot config,+Server credentials, the Docker socket and other session directories are not mounted.+Existing `bot/workspace/shared` files are not migrated automatically; customize the new workspace.++`shared` creates and retains one sandbox at startup. Other modes use a disposable startup probe and+create real instances on demand. Compile gets a sandbox per task. Shutdown cancels active work,+cleans up sandboxes, then stops the owned Server. SIGKILL/power loss can leave containers behind;+expiration cleanup cannot run while the managed Server is stopped, so verify recovery on restart.++External mode uses file APIs without local bind mounts, skips local Docker checks and never starts or stops the external service. Its operator+must configure the runtime, egress component and security policy. Automatic management applies to+Gateway / `--with-bot`; standalone `vikingbot chat` requires a prestarted OpenSandbox service.++Run the optional Docker permission regression with the images above available. It uses native+Linux volume storage to exercise UID 1000 ownership, `0755` directories, `0644` files, command/file+API writes and container recreation without Docker Desktop host file-sharing permission translation:++```bash+VIKINGBOT_TEST_DOCKER=1 PYTHONPATH=bot python -m pytest -q -o addopts='' bot/tests/test_opensandbox_docker_permissions.py+```+## HTTP APIThe Gateway Bot API uses the `/bot/v1` prefix:diff --git a/bot/README_CN.md b/bot/README_CN.mdindex 35fdb37e0..6016359e4 100644--- a/bot/README_CN.md+++ b/bot/README_CN.md@@ -352,6 +352,9 @@ Workspace 根目录由 `storage.workspace` 决定:vikingbot status```+使用托管 OpenSandbox(`backend=opensandbox`、`managed=true`)时,活动 Workspace 根目录+改为 `<storage.workspace>/bot/runtime/opensandbox/workspaces`,并挂载进容器,详见下方沙箱配置。+Agent 实际使用的活动目录还取决于 `bot.sandbox.mode`:| 模式 | 活动 Workspace |@@ -488,6 +491,90 @@ DirectBackend 默认 `restrict_to_workspace: false`。对不可信用户开放 G}```+### Gateway 自动管理 Docker 沙箱++默认仍为 `direct`,不会检查 Docker 或启动 OpenSandbox。若希望命令和文件工具在独立+Linux 容器中执行,先安装并启动 Docker(macOS 使用 Docker Desktop;Windows 推荐+Docker Desktop + WSL2,并在 WSL2 内运行 Bot),然后在实际使用的 `ov.conf` 中配置:++```json+{+ "bot": {+ "sandbox": {+ "backend": "opensandbox",+ "mode": "per-session"+ }+ }+}+```++`vikingbot gateway --config /path/to/ov.conf` 和 OpenViking `--with-bot` 共用启动流程:+检查 SDK、Server、Docker 服务及 Linux 容器模式 → 准备镜像 → 生成服务配置和管理密钥 →+启动 OpenSandbox Server → 验证命令执行和文件往返 → Gateway 就绪。初始化失败会退出,+不会回退到 `direct`。`--with-bot` 等待真实就绪,不再把子进程存活当作启动成功。++用户不需要维护 `~/.sandbox.toml`。生成文件及日志位于+`{storage.workspace}/bot/runtime/opensandbox/gateway-*/`;生成的配置文件在正常退出时删除,+日志保留。管理服务默认只监听本机 `127.0.0.1:18792`,密钥不传入工作负载容器。+Bot 托管的服务进程还会将 Docker 发布的沙箱端口限制为 `127.0.0.1`;这是对+OpenSandbox Server 0.1.6 默认绑定所有网卡的适配,不会修改外部 Server。++可选参数位于 `bot.sandbox.backends.opensandbox`:++| 参数 | 默认值 | 说明 |+|------|--------|------|+| `managed` | `true` | Bot 启停本机 OpenSandbox Server;设为 `false` 连接已有服务 |+| `server_url` | `http://localhost:18792` | 托管模式只允许本机 HTTP 地址,可调整端口 |+| `api_key` | 空 | 托管模式自动生成;外部服务填写其管理密钥 |+| `startup_timeout` | `600` | 初始化总超时(秒),首次拉取镜像较慢时可调大 |+| `use_server_proxy` | `true` | 经 Server 访问沙箱,避免客户端必须直连容器 IP |+| `default_image` | `opensandbox/code-interpreter:v1.0.1` | 执行镜像,需包含 shell、Python 3 |+| `execd_image` | `opensandbox/execd:v1.0.6` | 执行服务镜像 |+| `egress_image` | `opensandbox/egress:v1.0.1` | 出站网络策略组件镜像 |+| `pids_limit` | `256` | 托管 Docker 沙箱的进程数限制 |+| `runtime.cpu` / `runtime.memory` | `500m` / `1Gi` | 每个执行沙箱的资源限制 |+| `runtime.timeout` | `300` | 沙箱生命周期(秒),再次使用时续期 |+| `network.allowed_domains` | `[]` | 默认拒绝出站,按需允许依赖下载或业务域名 |+| `network.denied_domains` | `[]` | 在允许规则之前匹配的拒绝域名 |++托管 Docker 使用 bridge 网络、裁剪 capabilities、禁止新增特权。仅将专用工作目录+读写挂载到容器 `/workspace`,不会挂载 Bot 配置、服务密钥、Docker socket 或其他会话目录:++执行容器和其中的 execd 服务使用专用工作目录所有者的数值 UID/GID,`HOME` 指向+`/workspace`。因此在 Linux 上也能读写普通用户拥有的 `0755` 目录和 `0644` 文件,+无需扩大目录权限或恢复 `CAP_DAC_OVERRIDE`;网络组件和镜像缓存容器不使用这个用户覆盖。++```text+{storage.workspace}/bot/runtime/opensandbox/+├── gateway-*/ # 服务配置和日志,不挂载+└── workspaces/+ ├── shared/ # shared 模式 → 容器 /workspace+ ├── <session/channel-key>/ # 按会话或渠道隔离 → 各容器 /workspace+ └── compile/<task-id>/ # Compile 独立任务工作区+```++Mac 上可在 Finder 直接查看这些工作文件,宿主机和容器的修改立即作用于同一目录。+聊天沙箱销毁或过期后文件保留,重建时复用;首次创建目录时初始化引导文件和本地 Skill,+之后不会覆盖用户修改。Compile 仍按原有任务生命周期清理任务目录。+旧的 `bot/workspace/shared` 不自动迁移;启用后应在上面的新工作目录编辑引导文件。++`shared` 在启动时创建并保留一个共享沙箱;`per-session` / `per-channel` 在启动时使用+临时探测沙箱,后续按需创建实例。Compile 单独按任务创建和回收。Gateway 退出时先取消+任务并清理沙箱,再停止自己启动的 Server。SIGKILL 或断电无法保证即时清理;托管 Server+停止期间不会执行到期清理,重启后需确认遗留容器已回收。++`managed=false` 使用文件 API,不挂载 Bot 本机目录、不检查本机 Docker、不启停外部服务;外部服务需自行配置 Docker、egress+组件和权限策略。当前自动托管范围是 Gateway / `--with-bot`,独立 `vikingbot chat` 使用+OpenSandbox 时需要提前启动服务并配置地址。++可显式运行 Docker 权限回归测试(需要 Docker 和上述镜像)。该测试使用 Linux 原生卷,+覆盖 UID 1000 的 `0755` 目录、`0644` 文件、命令与文件 API 写入及容器重建,避免+Docker Desktop 的宿主机文件共享权限转换掩盖 Linux 权限问题:++```bash+VIKINGBOT_TEST_DOCKER=1 PYTHONPATH=bot python -m pytest -q -o addopts='' bot/tests/test_opensandbox_docker_permissions.py+```+## HTTP APIGateway 的 Bot API 前缀为 `/bot/v1`:diff --git a/bot/tests/test_compile.py b/bot/tests/test_compile.pyindex 8666a8843..8846d2cae 100644--- a/bot/tests/test_compile.py+++ b/bot/tests/test_compile.py@@ -1734,6 +1734,8 @@ async def test_execute_skill_target_skips_recursive_catalog_and_completes(monkeypatch, tmp_path: Path, target_uri: str):class TaskConfig:+ uses_managed_opensandbox = False+def __init__(self):self.bot_data_path = tmp_pathself.workspace_path = tmp_path / "host-workspace"@@ -3169,6 +3171,8 @@ async def test_iteration_limit_salvages_before_workspace_cleanup(monkeypatch, tmsandbox = _FakeWorkspaceSandbox(remote_files)class TaskConfig:+ uses_managed_opensandbox = False+def __init__(self):self.bot_data_path = tmp_pathself.workspace_path = tmp_path / "host-workspace"diff --git a/bot/tests/test_opensandbox_docker_permissions.py b/bot/tests/test_opensandbox_docker_permissions.pynew file mode 100644index 000000000..69847ee53--- /dev/null+++ b/bot/tests/test_opensandbox_docker_permissions.py@@ -0,0 +1,147 @@+"""Opt-in regression on native Linux storage, including when run on Docker Desktop.++Run with VIKINGBOT_TEST_DOCKER=1 and the configured OpenSandbox images available.+A named volume avoids macOS file-sharing permission translation masking the bug.+"""++import os+import socket+import subprocess+import uuid+from datetime import timedelta++import pytest+from vikingbot.config.schema import Config, SessionKey+from vikingbot.sandbox.managed_server import WORKSPACE_GID_LABEL, WORKSPACE_UID_LABEL+from vikingbot.sandbox.manager import SandboxManager+from vikingbot.sandbox.runtime import OpenSandboxRuntime++pytestmark = pytest.mark.skipif(+ os.environ.get("VIKINGBOT_TEST_DOCKER") != "1",+ reason="requires explicit Docker integration run",+)+++async def test_uid_1000_linux_workspace_without_dac_override(tmp_path):+ from opensandbox.config import ConnectionConfig+ from opensandbox.models.sandboxes import PVC, NetworkPolicy, Volume+ from opensandbox.sandbox import Sandbox++ with socket.socket() as listener:+ listener.bind(("127.0.0.1", 0))+ port = listener.getsockname()[1]+ config = Config(+ storage_workspace=str(tmp_path),+ sandbox={+ "backend": "opensandbox",+ "backends": {"opensandbox": {"server_url": f"http://127.0.0.1:{port}"}},+ },+ )+ runtime = OpenSandboxRuntime(config)+ manager = SandboxManager(config, config.sandbox_workspace_path, tmp_path / "source")+ volume = "vikingbot-permissions-" + uuid.uuid4().hex+ remote = None++ def docker(*args, check=True):+ return subprocess.run(+ ["docker", *args], capture_output=True, text=True, timeout=60, check=check+ )++ image = config.sandbox.backends.opensandbox.default_image+ docker("volume", "create", volume)+ try:+ docker(+ "run",+ "--rm",+ "--entrypoint",+ "sh",+ "-v",+ f"{volume}:/workspace",+ image,+ "-c",+ "printf original > /workspace/existing; chown -R 1000:1000 /workspace; "+ "chmod 755 /workspace; chmod 644 /workspace/existing",+ )+ before = docker(+ "run",+ "--rm",+ "--entrypoint",+ "sh",+ "--user",+ "0:0",+ "--cap-drop",+ "ALL",+ "--security-opt",+ "no-new-privileges",+ "-v",+ f"{volume}:/workspace",+ image,+ "-c",+ "printf probe > /workspace/root-probe",+ check=False,+ )+ assert before.returncode != 0+ assert "Permission denied" in before.stderr++ # Also exercise the ordinary Bot path with its real host directory owner.+ await runtime.start(manager)+ backend = await manager.get_sandbox(+ SessionKey(type="cli", channel_id="test", chat_id="test")+ )+ await backend.write_file("repeat.txt", "first")+ await backend.write_file("repeat.txt", "second")+ await backend.write_file_bytes("binary.bin", b"\x00\xff")+ workspace = config.opensandbox_workspaces_path / "shared"+ assert (workspace / "repeat.txt").read_text() == "second"+ assert (workspace / "binary.bin").read_bytes() == b"\x00\xff"+ assert (workspace / "repeat.txt").stat().st_mode & 0o777 == 0o644+ assert (workspace / "binary.bin").stat().st_mode & 0o777 == 0o644+ connection = ConnectionConfig(+ domain=runtime.settings.server_url,+ api_key=runtime.settings.api_key,+ use_server_proxy=True,+ )+ for iteration in range(2):+ remote = await Sandbox.create(+ image,+ connection_config=connection,+ timeout=timedelta(seconds=120),+ metadata={WORKSPACE_UID_LABEL: "1000", WORKSPACE_GID_LABEL: "1000"},+ env={"HOME": "/workspace"},+ volumes=[+ Volume(name="workspace", pvc=PVC(claim_name=volume), mount_path="/workspace")+ ],+ network_policy=NetworkPolicy(default_action="deny"),+ )+ execution = await remote.commands.run("id -u; id -g; grep CapEff /proc/self/status")+ assert execution.error is None+ output = "\n".join(chunk.text for chunk in execution.logs.stdout)+ assert output.splitlines()[:2] == ["1000", "1000"]+ assert "0000000000000000" in output+ assert await remote.files.read_file("/workspace/existing") == (+ "original" if iteration == 0 else "updated"+ )+ await remote.files.write_file("/workspace/existing", "updated", mode=644)+ await remote.files.write_file("/workspace/api-probe", "api", mode=644)+ execution = await remote.commands.run(+ "printf shell > /workspace/shell-probe; "+ "stat -c '%u:%g %a' /workspace/existing /workspace/api-probe /workspace/shell-probe"+ )+ assert execution.error is None+ output = "\n".join(chunk.text for chunk in execution.logs.stdout)+ assert output.splitlines() == ["1000:1000 644"] * 3+ await remote.kill()+ await remote.close()+ remote = None+ finally:+ try:+ if remote is not None:+ try:+ await remote.kill()+ finally:+ await remote.close()+ await manager.cleanup_all()+ finally:+ await runtime.stop()+ docker("volume", "rm", volume)diff --git a/bot/tests/test_opensandbox_runtime.py b/bot/tests/test_opensandbox_runtime.pynew file mode 100644index 000000000..896b9a951--- /dev/null+++ b/bot/tests/test_opensandbox_runtime.py@@ -0,0 +1,473 @@+"""Managed startup must prove the sandbox works before serving requests."""++import asyncio+import os+import socket+import sys+from pathlib import Path+from types import SimpleNamespace+from unittest.mock import AsyncMock, Mock++import pytest+from vikingbot.config.schema import Config, SessionKey+from vikingbot.sandbox.backends.opensandbox import OpenSandboxBackend+from vikingbot.sandbox.manager import SandboxManager+from vikingbot.sandbox.runtime import OpenSandboxRuntime++try:+ import tomllib+except ImportError:+ import tomli as tomllib+++def configured(tmp_path, **settings):+ return Config(+ storage_workspace=str(tmp_path),+ sandbox={"backend": "opensandbox", "backends": {"opensandbox": settings}},+ )+++async def test_direct_never_checks_dependencies(tmp_path, monkeypatch):+ runtime = OpenSandboxRuntime(Config(storage_workspace=str(tmp_path)))+ check = Mock(side_effect=AssertionError("must not inspect dependencies"))+ monkeypatch.setattr("vikingbot.sandbox.runtime.importlib.util.find_spec", check)+ await runtime.start(None)+ check.assert_not_called()+++async def test_missing_docker_is_actionable(tmp_path, monkeypatch):+ monkeypatch.setattr("vikingbot.sandbox.runtime.shutil.which", lambda _: None)+ runtime = OpenSandboxRuntime(configured(tmp_path))+ with pytest.raises(RuntimeError, match="Docker is not installed.*https://"):+ await runtime._docker_environment()+++async def test_unreachable_docker_is_not_missing_docker(tmp_path):+ runtime = OpenSandboxRuntime(configured(tmp_path))+ runtime._command = AsyncMock(side_effect=RuntimeError("permission denied"))+ with pytest.MonkeyPatch.context() as patch:+ patch.setattr("vikingbot.sandbox.runtime.shutil.which", lambda _: "/bin/docker")+ with pytest.raises(RuntimeError, match="Docker is not reachable.*permissions"):+ await runtime._docker_environment()+++async def test_docker_desktop_context_is_forwarded_to_server(tmp_path, monkeypatch):+ monkeypatch.delenv("DOCKER_HOST", raising=False)+ monkeypatch.delenv("DOCKER_CONTEXT", raising=False)+ monkeypatch.setattr("vikingbot.sandbox.runtime.shutil.which", lambda _: "/bin/docker")+ runtime = OpenSandboxRuntime(configured(tmp_path))+ runtime._command = AsyncMock(side_effect=["linux", "unix:///desktop/docker.sock"])+ env = await runtime._docker_environment()+ assert env["DOCKER_HOST"] == "unix:///desktop/docker.sock"+++async def test_native_windows_containers_rejected(tmp_path, monkeypatch):+ monkeypatch.setattr("vikingbot.sandbox.runtime.shutil.which", lambda _: "/bin/docker")+ runtime = OpenSandboxRuntime(configured(tmp_path))+ runtime._command = AsyncMock(return_value="windows")+ with pytest.raises(RuntimeError, match="Linux containers"):+ await runtime._docker_environment()+++def test_generated_config_is_private_and_uses_actual_settings(tmp_path):+ with socket.socket() as listener:+ listener.bind(("127.0.0.1", 0))+ port = listener.getsockname()[1]+ config = configured(tmp_path, server_url=f"http://localhost:{port}", pids_limit=64)+ runtime = OpenSandboxRuntime(config)+ path = runtime._write_config()+ parsed = tomllib.loads(path.read_text())+ assert parsed["server"]["port"] == port+ assert parsed["server"]["api_key"] == runtime.settings.api_key+ assert len(runtime.settings.api_key) >= 32+ assert parsed["runtime"]["type"] == "docker"+ assert parsed["docker"]["network_mode"] == "bridge"+ assert parsed["docker"]["drop_capabilities"] == ["ALL"]+ assert parsed["docker"]["pids_limit"] == 64+ assert parsed["storage"]["allowed_host_paths"] == [+ str(config.opensandbox_workspaces_path.resolve())+ ]+ assert not path.resolve().is_relative_to(config.opensandbox_workspaces_path.resolve())+ assert path.is_relative_to(config.bot_data_path)+ assert not path.is_relative_to(config.workspace_path)+ if os.name != "nt":+ assert path.stat().st_mode & 0o777 == 0o600+++def test_occupied_port_is_not_reused_or_killed(tmp_path):+ with socket.socket() as listener:+ listener.bind(("127.0.0.1", 0))+ listener.listen()+ runtime = OpenSandboxRuntime(+ configured(+ tmp_path,+ server_url=f"http://127.0.0.1:{listener.getsockname()[1]}",+ )+ )+ with pytest.raises(RuntimeError, match="occupied"):+ runtime._write_config()+ assert runtime.process is None+++def test_managed_server_restricts_workload_and_sidecar_published_ports(monkeypatch):+ import docker+ from vikingbot.sandbox.managed_server import restrict_published_ports++ # Use the real Docker SDK's serialization without connecting to a daemon.+ monkeypatch.setattr(docker.APIClient, "create_host_config", docker.APIClient.create_host_config)+ restrict_published_ports(docker.APIClient)+ client = docker.APIClient(base_url="unix:///unused.sock", version="1.41")+ try:+ config = client.create_host_config(+ network_mode="bridge",+ port_bindings={"44772": ("0.0.0.0", 50123), "8080": ("::", 50124)},+ cap_add=["NET_ADMIN"],+ )+ assert config["PortBindings"] == {+ "44772/tcp": [{"HostIp": "127.0.0.1", "HostPort": "50123"}],+ "8080/tcp": [{"HostIp": "127.0.0.1", "HostPort": "50124"}],+ }+ assert config["CapAdd"] == ["NET_ADMIN"]+ finally:+ client.close()+++def test_managed_server_sets_workspace_owner_only_for_workloads():+ from vikingbot.sandbox.managed_server import (+ WORKSPACE_GID_LABEL,+ WORKSPACE_UID_LABEL,+ use_workspace_owner,+ )++ class Client:+ def create_container(self, **kwargs):+ return kwargs++ use_workspace_owner(Client)+ client = Client()+ workload = client.create_container(+ labels={+ "opensandbox.io/id": "test",+ WORKSPACE_UID_LABEL: "1000",+ WORKSPACE_GID_LABEL: "1001",+ },+ user="root",+ )+ assert workload["user"] == "1000:1001"+ assert "user" not in client.create_container(labels={"opensandbox.io/egress": "test"})+ assert "user" not in client.create_container(name="sandbox-execd-cache")+ for value in ("", "root", "1000:wheel", "-1:1000"):+ with pytest.raises(ValueError, match="UID:GID"):+ client.create_container(+ labels={+ "opensandbox.io/id": "test",+ WORKSPACE_UID_LABEL: value,+ WORKSPACE_GID_LABEL: "1000",+ }+ )+++async def test_binary_files_use_remote_api_and_sdk_errors_are_reported(tmp_path):+ from opensandbox.models.execd import Execution, ExecutionError++ config = configured(tmp_path)+ backend = OpenSandboxBackend(config.sandbox, "shared", tmp_path / "work")+ backend._sandbox = SimpleNamespace(+ files=SimpleNamespace(write_file=AsyncMock()),+ commands=SimpleNamespace(+ run=AsyncMock(+ return_value=Execution(+ error=ExecutionError(+ name="CommandExecError", value="exit status 2", timestamp=0+ )+ )+ )+ ),+ )+ await backend.write_file_bytes("nested/image.png", b"\x00\xff")+ backend._sandbox.files.write_file.assert_awaited_once_with(+ "/workspace/nested/image.png", b"\x00\xff", mode=644+ )+ assert not (tmp_path / "work" / "nested" / "image.png").exists()+ output = await backend.execute("false")+ assert "exit status 2" in output+ assert "Exit code: 1" in output+++def probe_manager():+ files = {}++ async def write(path, content):+ files[path] = content++ async def execute(command):+ if command.startswith("cat "):+ return files[command.removeprefix("cat /workspace/")]+ return ""++ sandbox = SimpleNamespace(+ write_file=AsyncMock(side_effect=write),+ read_file=AsyncMock(side_effect=lambda path: files[path]),+ execute=AsyncMock(side_effect=execute),+ )+ return SimpleNamespace(+ get_sandbox=AsyncMock(return_value=sandbox),+ cleanup_session=AsyncMock(),+ cleanup_all=AsyncMock(),+ )++[email protected]("mode", ["shared", "per-session", "per-channel"])+async def test_external_server_probes_without_local_docker(tmp_path, monkeypatch, mode):+ config = configured(tmp_path, managed=False)+ config.sandbox.mode = mode+ runtime = OpenSandboxRuntime(config)+ runtime._wait_for_server = AsyncMock()+ runtime._docker_environment = AsyncMock(side_effect=AssertionError("external server"))+ manager = probe_manager()+ await runtime.start(manager)+ runtime._docker_environment.assert_not_called()+ assert manager.cleanup_session.await_count == (0 if mode == "shared" else 1)+ await runtime.stop()+++async def test_failed_probe_cleans_up_and_does_not_fallback(tmp_path):+ config = configured(tmp_path, managed=False)+ runtime = OpenSandboxRuntime(config)+ runtime._wait_for_server = AsyncMock()+ manager = probe_manager()+ manager.get_sandbox.return_value.execute.side_effect = None+ manager.get_sandbox.return_value.execute.return_value = "Exit code: 1"+ runtime.stop = AsyncMock()+ with pytest.raises(RuntimeError, match="command probe failed"):+ await runtime.start(manager)+ manager.cleanup_all.assert_awaited_once()+ runtime.stop.assert_awaited_once()+ assert config.sandbox.backend == "opensandbox"+++async def test_cancelled_startup_stops_owned_server(tmp_path):+ runtime = OpenSandboxRuntime(configured(tmp_path, managed=False))+ runtime._wait_for_server = AsyncMock(side_effect=asyncio.CancelledError)+ runtime.stop = AsyncMock()+ manager = probe_manager()+ with pytest.raises(asyncio.CancelledError):+ await runtime.start(manager)+ manager.cleanup_all.assert_awaited_once()+ runtime.stop.assert_awaited_once()++[email protected]("managed", [True, False])+async def test_backend_creation_mounts_only_managed_workspace(tmp_path, monkeypatch, managed):+ from opensandbox.sandbox import Sandbox++ remote = SimpleNamespace(commands=SimpleNamespace(run=AsyncMock()))+ create = AsyncMock(return_value=remote)+ monkeypatch.setattr(Sandbox, "create", create)+ config = configured(tmp_path, managed=managed)+ settings = config.sandbox.backends.opensandbox+ settings.network.allowed_domains = ["pypi.org"]+ settings.network.denied_domains = ["internal.example"]+ workspace = config.sandbox_workspace_path / "shared"+ backend = OpenSandboxBackend(+ config.sandbox, "shared", workspace, host_workspace=workspace if managed else None+ )+ await backend.start()+ kwargs = create.call_args.kwargs+ if managed:+ from vikingbot.sandbox.managed_server import WORKSPACE_GID_LABEL, WORKSPACE_UID_LABEL++ assert len(kwargs["volumes"]) == 1+ volume = kwargs["volumes"][0]+ assert volume.host.path == str(workspace.resolve())+ assert volume.mount_path == "/workspace"+ assert volume.read_only is False+ owner = workspace.stat()+ assert kwargs["metadata"][WORKSPACE_UID_LABEL] == str(owner.st_uid)+ assert kwargs["metadata"][WORKSPACE_GID_LABEL] == str(owner.st_gid)+ assert kwargs["env"]["HOME"] == "/workspace"+ else:+ assert "volumes" not in kwargs+ assert "metadata" not in kwargs+ assert kwargs["resource"] == {"cpu": "500m", "memory": "1Gi"}+ assert kwargs["network_policy"].default_action == "deny"+ assert [rule.action for rule in kwargs["network_policy"].egress] == ["deny", "allow"]+ assert kwargs["connection_config"].use_server_proxy is True+ assert backend.local_file_path("secret") is None+++async def test_concurrent_first_use_creates_one_sandbox(tmp_path):+ config = Config(storage_workspace=str(tmp_path))+ manager = SandboxManager(config, tmp_path / "work", tmp_path / "source")+ sandbox = object()++ async def create(*_):+ await asyncio.sleep(0)+ return sandbox++ manager._create_sandbox = AsyncMock(side_effect=create)+ key = SessionKey(type="cli", channel_id="test", chat_id="test")+ results = await asyncio.gather(*(manager.get_sandbox(key) for _ in range(8)))+ assert results == [sandbox] * 8+ manager._create_sandbox.assert_awaited_once()+++async def test_managed_workspace_survives_container_recreation_without_overwrite(tmp_path):+ config = configured(tmp_path)+ source = tmp_path / "source"+ source.mkdir()+ (source / "SOUL.md").write_text("initial")+ manager = SandboxManager(config, config.sandbox_workspace_path, source)++ class MountedBackend:+ def __init__(self, config, workspace_id, workspace, *, host_workspace):+ assert workspace == host_workspace+ self.workspace = workspace++ async def start(self):+ self.workspace.mkdir(parents=True, exist_ok=True)++ async def stop(self):+ pass++ manager._backend_cls = MountedBackend+ key = SessionKey(type="cli", channel_id="test", chat_id="test")+ first = await manager.get_sandbox(key)+ assert first.workspace == config.opensandbox_workspaces_path / "shared"+ assert (first.workspace / "SOUL.md").read_text() == "initial"+ (first.workspace / "SOUL.md").write_text("customized")+ (first.workspace / "result.txt").write_text("keep me")+ await manager.cleanup_all()+ second = await manager.get_sandbox(key)+ assert second is not first+ assert (second.workspace / "SOUL.md").read_text() == "customized"+ assert (second.workspace / "result.txt").read_text() == "keep me"++[email protected]("escape", ["parent", "symlink"])+async def test_managed_workspace_cannot_mount_server_credentials(tmp_path, escape):+ config = configured(tmp_path)+ root = config.opensandbox_workspaces_path+ root.mkdir(parents=True)+ credentials = root.parent / "gateway-private"+ credentials.mkdir()+ if escape == "symlink":+ workspace = root / "shared"+ workspace.symlink_to(credentials, target_is_directory=True)+ else:+ workspace = credentials+ manager = SandboxManager(config, root, tmp_path / "source")+ with pytest.raises(ValueError, match="host workspace"):+ await manager._create_sandbox("shared", workspace)+++async def test_managed_service_uses_generated_config_and_is_stopped(tmp_path, monkeypatch):+ config = configured(tmp_path)+ runtime = OpenSandboxRuntime(config)+ runtime._docker_environment = AsyncMock(return_value={"DOCKER_HOST": "unix:///docker.sock"})+ runtime._command = AsyncMock(return_value="image exists")+ runtime._wait_for_server = AsyncMock()+ process = SimpleNamespace(returncode=None, terminate=Mock(), kill=Mock(), wait=AsyncMock())+ spawn = AsyncMock(return_value=process)+ monkeypatch.setattr(+ "vikingbot.sandbox.runtime.shutil.which", lambda _: "/bin/opensandbox-server"+ )+ monkeypatch.setattr("vikingbot.sandbox.runtime.asyncio.create_subprocess_exec", spawn)+ await runtime.start(probe_manager())+ argv = spawn.call_args.args+ assert argv[:4] == (sys.executable, "-m", "vikingbot.sandbox.managed_server", "--config")+ assert spawn.call_args.kwargs["start_new_session"] == (os.name != "nt")+ if os.name == "nt":+ import subprocess++ assert spawn.call_args.kwargs["creationflags"] == subprocess.CREATE_NEW_PROCESS_GROUP+ path = Path(argv[4])+ assert path.exists()+ assert config.sandbox.backends.opensandbox.api_key+ await runtime.stop()+ process.terminate.assert_called_once()+ process.wait.assert_awaited_once()+ assert not path.exists()+ assert (runtime.runtime_dir / "server.log").exists()+++async def test_health_probe_requires_authenticated_lifecycle_api(tmp_path, monkeypatch):+ import httpx++ requests = []++ def handler(request):+ requests.append(request)+ return httpx.Response(200 if request.url.path == "/health" else 401)++ client = httpx.AsyncClient(transport=httpx.MockTransport(handler))+ monkeypatch.setattr("vikingbot.sandbox.runtime.httpx.AsyncClient", lambda **_: client)+ runtime = OpenSandboxRuntime(configured(tmp_path, managed=False, api_key="test-key"))+ with pytest.raises(RuntimeError, match="authentication failed"):+ await runtime._wait_for_server()+ assert requests[-1].headers["OPEN-SANDBOX-API-KEY"] == "test-key"++[email protected]("fail", [False, True])+def test_gateway_preflights_before_serving_and_always_cleans_up(tmp_path, monkeypatch, fail):+ import typer+ from vikingbot.cli import commands++ config = configured(tmp_path)+ events = []+ manager = SimpleNamespace(cleanup_all=AsyncMock())+ agent = SimpleNamespace(sandbox_manager=manager, run=AsyncMock(), close_mcp=AsyncMock())+ server = SimpleNamespace(started=True)++ async def start(_):+ events.append("sandbox")+ if fail:+ raise RuntimeError("Docker unavailable")++ async def serve():+ events.append("http")++ runtime = SimpleNamespace(start=AsyncMock(side_effect=start), stop=AsyncMock())+ server.serve = AsyncMock(side_effect=serve)+ monkeypatch.setattr(commands, "ensure_config", lambda _: config)+ monkeypatch.setattr(commands, "validate_openviking_auth", lambda _: None)+ monkeypatch.setattr(commands, "_abort_if_port_in_use", lambda *_: None)+ monkeypatch.setattr(commands, "_init_bot_data", lambda _: None)+ monkeypatch.setattr(commands, "prepare_cron", lambda *_: None)+ monkeypatch.setattr(commands, "prepare_agent_loop", lambda *_: agent)+ monkeypatch.setattr(+ commands, "prepare_channel", lambda *_, **__: SimpleNamespace(start_all=AsyncMock())+ )+ monkeypatch.setattr(+ commands, "prepare_heartbeat", lambda *_: SimpleNamespace(start=AsyncMock(), stop=Mock())+ )+ monkeypatch.setattr("uvicorn.Server", lambda _: server)+ monkeypatch.setattr("vikingbot.sandbox.runtime.OpenSandboxRuntime", lambda _: runtime)+ monkeypatch.setattr(+ "vikingbot.compile.service.BotCompileService",+ lambda **_: SimpleNamespace(start=AsyncMock(), close=AsyncMock()),+ )+ if fail:+ with pytest.raises(typer.Exit):+ commands.gateway(port=None, host="127.0.0.1", verbose=False, config_path=None)+ assert events == ["sandbox"]+ else:+ commands.gateway(port=None, host="127.0.0.1", verbose=False, config_path=None)+ assert events == ["sandbox", "http"]+ runtime.stop.assert_awaited_once()+ manager.cleanup_all.assert_awaited_once()diff --git a/bot/tests/test_sandbox_file_access.py b/bot/tests/test_sandbox_file_access.pyindex 31ed70522..463db0d7d 100644--- a/bot/tests/test_sandbox_file_access.py+++ b/bot/tests/test_sandbox_file_access.py@@ -5,6 +5,7 @@import jsonfrom pathlib import Pathfrom types import SimpleNamespace+from unittest.mock import AsyncMockimport pytestfrom vikingbot.sandbox.backends.aiosandbox import AioSandboxBackend@@ -225,3 +226,46 @@ async def test_opensandbox_vke_inventory_stops_at_the_remote_limit(tmp_path: Patwith pytest.raises(ValueError, match="inventory exceeds 1 entries"):await backend.list_files(max_entries=1)assert "limit = 1" in commands.calls[0][0]++[email protected]("count,chunk_size", [(0, 10000), (500, 20000), (500, 257)])+async def test_opensandbox_directory_json_is_never_display_truncated(tmp_path, count, chunk_size):+ expected = [(f"document-{index:04d}.txt", False) for index in range(count)]+ payload = json.dumps(expected)+ if count == 500:+ assert len(payload) == 15000+ execution = SimpleNamespace(+ error=None,+ logs=SimpleNamespace(+ stdout=[+ SimpleNamespace(text=payload[offset : offset + chunk_size])+ for offset in range(0, len(payload), chunk_size)+ ],+ stderr=[SimpleNamespace(text="unrelated diagnostic")],+ ),+ )+ commands = SimpleNamespace(run=AsyncMock(return_value=execution))+ backend = _opensandbox_vke_backend(tmp_path, _OpenSandboxFiles(), commands)+ assert await backend.list_dir(".") == expected+++async def test_opensandbox_directory_command_error_is_reported_before_json_parsing(tmp_path):+ execution = SimpleNamespace(error=SimpleNamespace(value="Permission denied"))+ commands = SimpleNamespace(run=AsyncMock(return_value=execution))+ backend = _opensandbox_vke_backend(tmp_path, _OpenSandboxFiles(), commands)+ with pytest.raises(IOError, match="directory listing failed: Permission denied"):+ await backend.list_dir("private")+++async def test_opensandbox_exec_retains_display_output_limit(tmp_path):+ execution = SimpleNamespace(+ error=None,+ logs=SimpleNamespace(stdout=[SimpleNamespace(text="x" * 15000)], stderr=[]),+ )+ commands = SimpleNamespace(run=AsyncMock(return_value=execution))+ backend = _opensandbox_vke_backend(tmp_path, _OpenSandboxFiles(), commands)+ output = await backend.execute("generate-long-output")+ assert output == "x" * 10000 + "\n... (truncated, 5000 more chars)"diff --git a/bot/vikingbot/cli/commands.py b/bot/vikingbot/cli/commands.pyindex b7fc57888..57b27bae3 100644--- a/bot/vikingbot/cli/commands.py+++ b/bot/vikingbot/cli/commands.py@@ -4,6 +4,7 @@ import asyncioimport jsonimport osimport select+import signalimport socketimport sysimport time@@ -466,6 +467,13 @@ def gateway(bus = MessageBus()path = Path(config_path).expanduser() if config_path is not None else Noneconfig = ensure_config(path)+ from vikingbot.utils.startup import report_startup++ startup_timeout = getattr(getattr(config, "sandbox", None), "backends", None)+ report_startup(+ "starting",+ timeout=(startup_timeout.opensandbox.startup_timeout + 60) if startup_timeout else 600,+ )effective_host = host if host is not None else config.gateway.hosteffective_port = port if port is not None else config.gateway.portconfig.gateway.host = effective_host@@ -511,6 +519,8 @@ def gateway(async def run():import uvicorn+ from vikingbot.sandbox.runtime import OpenSandboxRuntime+# Start uvicorn server for OpenAPIconfig_uvicorn = uvicorn.Config(fastapi_app,@@ -520,25 +530,73 @@ def gateway()server = uvicorn.Server(config_uvicorn)- tasks = [- heartbeat.start(),- compile_service.start(),- channels.start_all(),- agent_loop.run(),- server.serve(),- ]- if cron is not None:- tasks.append(cron.start())+ runtime = OpenSandboxRuntime(config)+ manager = getattr(agent_loop, "sandbox_manager", None)+ loop = asyncio.get_running_loop()+ current = asyncio.current_task()+ previous_sigterm = signal.signal(+ signal.SIGTERM, lambda *_: loop.call_soon_threadsafe(current.cancel)+ )+ tasks = []++ async def report_ready(server_task):+ while not getattr(server, "started", False):+ if server_task.done():+ return+ await asyncio.sleep(0.05)+ report_startup("ready")+try:- await asyncio.gather(*tasks)+ await runtime.start(manager)+ await compile_service.start()+ server_task = asyncio.create_task(server.serve())+ tasks = [+ server_task,+ asyncio.create_task(report_ready(server_task)),+ asyncio.create_task(heartbeat.start()),+ asyncio.create_task(channels.start_all()),+ asyncio.create_task(agent_loop.run()),+ ]+ if cron is not None:+ tasks.append(asyncio.create_task(cron.start()))+ combined = asyncio.gather(*tasks)+ await asyncio.wait({combined, server_task}, return_when=asyncio.FIRST_COMPLETED)+ if combined.done():+ await combined+ else:+ await server_taskfinally:- await agent_loop.close_mcp()+ for task in tasks:+ task.cancel()+ await asyncio.gather(*tasks, return_exceptions=True)+ if tasks:+ await asyncio.gather(combined, return_exceptions=True)+ try:+ if hasattr(compile_service, "close"):+ await compile_service.close()+ if hasattr(heartbeat, "stop"):+ heartbeat.stop()+ if cron is not None and hasattr(cron, "stop"):+ cron.stop()+ await agent_loop.close_mcp()+ finally:+ try:+ if manager is not None:+ await manager.cleanup_all()+ finally:+ await runtime.stop()+ signal.signal(signal.SIGTERM, previous_sigterm)- asyncio.run(run())+ try:+ asyncio.run(run())+ except (Exception, asyncio.CancelledError) as exc:+ report_startup("failed", error=str(exc) or "Gateway startup cancelled")+ console.print(f"[red]Gateway failed:[/red] {exc}")+ raise typer.Exit(1) from excdef prepare_agent_loop(config, bus, session_manager, cron, quiet: bool = False, eval: bool = False):- sandbox_parent_path = config.workspace_path+ sandbox_parent_path = config.sandbox_workspace_pathsource_workspace_path = get_source_workspace_path()sandbox_manager = SandboxManager(config, sandbox_parent_path, source_workspace_path)if config.sandbox.backend == "direct":@@ -570,7 +628,7 @@ def prepare_agent_loop(config, bus, session_manager, cron, quiet: bool = False,agent = AgentLoop(bus=bus,provider=provider,- workspace=config.workspace_path,+ workspace=config.sandbox_workspace_path,model=config.agents.model,temperature=config.agents.temperature,max_iterations=config.agents.max_tool_iterations,@@ -718,7 +776,7 @@ def prepare_heartbeat(config, agent_loop, session_manager) -> HeartbeatService:)heartbeat = HeartbeatService(- workspace=config.workspace_path,+ workspace=config.sandbox_workspace_path,on_heartbeat=on_heartbeat,interval_s=config.heartbeat.interval_seconds,enabled=config.heartbeat.enabled,@@ -1264,7 +1322,7 @@ def status():config_path = get_config_path()config = load_config()- workspace = config.workspace_path+ workspace = config.sandbox_workspace_pathconsole.print(f"{__logo__} vikingbot Status\n")diff --git a/bot/vikingbot/compile/service.py b/bot/vikingbot/compile/service.pyindex 61914fc8e..b30949259 100644--- a/bot/vikingbot/compile/service.py+++ b/bot/vikingbot/compile/service.py@@ -377,6 +377,13 @@ class BotCompileService:await self._prune_terminal_tasks()self._started = True+ async def close(self) -> None:+ """Cancel active work and let task finally blocks release their sandboxes."""+ tasks = list(self._tasks)+ for task in tasks:+ task.cancel()+ await asyncio.gather(*tasks, return_exceptions=True)+async def create_task(self,request: CompileRequest,@@ -826,6 +833,8 @@ class BotCompileService:)task_config.sandbox.mode = SandboxMode.PER_SESSIONworkspace_parent = self.config.bot_data_path / "compile_workspaces" / task_id+ if task_config.uses_managed_opensandbox:+ workspace_parent = task_config.opensandbox_workspaces_path / "compile" / task_idsandbox_manager = SandboxManager(task_config, workspace_parent, task_config.workspace_path)workspace = sandbox_manager.get_workspace_path(session_key)client: VikingClient | None = Nonediff --git a/bot/vikingbot/config/schema.py b/bot/vikingbot/config/schema.pyindex 189f28232..00e93ad0a 100644--- a/bot/vikingbot/config/schema.py+++ b/bot/vikingbot/config/schema.py@@ -789,21 +789,22 @@ class OpenSandboxNetworkConfig(BaseModel):class OpenSandboxRuntimeConfig(BaseModel):"""OpenSandbox runtime configuration."""- timeout: int = 300+ timeout: int = Field(default=300, gt=0)cpu: str = "500m"memory: str = "1Gi"class OpenSandboxBackendConfig(BaseModel):- """OpenSandbox backend configuration.-- Auto-detects runtime environment:- - Local: uses configured server_url (default http://localhost:18792)- - VKE: auto-detects KUBERNETES_SERVICE_HOST, uses http://opensandbox-server:8080- """+ """Docker-backed OpenSandbox; manage a local server or connect to an external one."""server_url: str = "http://localhost:18792"api_key: str = ""+ managed: bool = True+ startup_timeout: int = Field(default=600, ge=10)+ use_server_proxy: bool = True+ execd_image: str = "opensandbox/execd:v1.0.6"+ egress_image: str = "opensandbox/egress:v1.0.1"+ pids_limit: int = Field(default=256, gt=0)default_image: str = "opensandbox/code-interpreter:v1.0.1"network: OpenSandboxNetworkConfig = Field(default_factory=OpenSandboxNetworkConfig)runtime: OpenSandboxRuntimeConfig = Field(default_factory=OpenSandboxRuntimeConfig)@@ -931,6 +932,21 @@ class Config(BaseSettings):"""Get expanded workspace path: {storage_workspace}/bot/workspace."""return self.bot_data_path / "workspace"+ @property+ def opensandbox_workspaces_path(self) -> Path:+ """Dedicated host bind mounts, separate from Server credentials and logs."""+ return self.bot_data_path / "runtime" / "opensandbox" / "workspaces"++ @property+ def uses_managed_opensandbox(self) -> bool:+ return self.sandbox.backend == "opensandbox" and self.sandbox.backends.opensandbox.managed++ @property+ def sandbox_workspace_path(self) -> Path:+ if self.uses_managed_opensandbox:+ return self.opensandbox_workspaces_path+ return self.workspace_path+@propertydef ov_data_path(self) -> Path:return self.bot_data_path / "ov_data"diff --git a/bot/vikingbot/sandbox/backends/opensandbox.py b/bot/vikingbot/sandbox/backends/opensandbox.pyindex 25e79a691..7bd889473 100644--- a/bot/vikingbot/sandbox/backends/opensandbox.py+++ b/bot/vikingbot/sandbox/backends/opensandbox.py@@ -1,158 +1,46 @@"""OpenSandbox backend implementation using official SDK."""-import asyncio-import atexit-import base64import json-import osimport posixpathimport shlex-import subprocess-import timefrom datetime import timedeltafrom pathlib import Pathfrom typing import Any-import httpxfrom loguru import loggerfrom vikingbot.config.schema import SandboxConfig, SessionKeyfrom vikingbot.sandbox.backends import register_backendfrom vikingbot.sandbox.base import SandboxBackend, SandboxFileInfo, SandboxNotStartedError--# Global to track the opensandbox-server process-_OSB_SERVER_PROCESS: "subprocess.Popen | None" = None---def _is_kubernetes_env() -> bool:- if "KUBERNETES_SERVICE_HOST" in os.environ:- return True- if Path("/var/run/secrets/kubernetes.io/serviceaccount").exists():- return True- return False---async def _wait_for_server(url: str, timeout: int = 60) -> bool:- logger.info("Waiting for OpenSandbox server at {}...", url)- start_time = time.time()- while time.time() - start_time < timeout:- try:- async with httpx.AsyncClient(timeout=5.0) as client:- health_url = f"{url}/health"- logger.debug("Checking health at: {}", health_url)- response = await client.get(health_url)- logger.debug("Health check response: {} - {}", response.status_code, response.text)- if response.status_code == 200:- logger.info("OpenSandbox server is ready!")- return True- except Exception as e:- logger.debug("Health check failed: {}", e)- await asyncio.sleep(1)- logger.warning("OpenSandbox server not ready after {}s", timeout)- return False---def _start_opensandbox_server() -> "subprocess.Popen | None":- global _OSB_SERVER_PROCESS-- if _OSB_SERVER_PROCESS is not None:- if _OSB_SERVER_PROCESS.poll() is None:- logger.info("OpenSandbox server already running")- return _OSB_SERVER_PROCESS- else:- logger.warning("OpenSandbox server process died, restarting")- _OSB_SERVER_PROCESS = None-- try:- config_path = Path.home() / ".sandbox.toml"- if not config_path.exists():- logger.info("Initializing OpenSandbox config at {}", config_path)- try:- result = subprocess.run(- ["opensandbox-server", "init-config", str(config_path), "--example", "docker"],- capture_output=True,- text=True,- )- if result.returncode != 0:- logger.warning("Failed to init config with --example, trying without...")- result = subprocess.run(- ["opensandbox-server", "init-config", str(config_path)],- capture_output=True,- text=True,- )- if result.returncode != 0:- logger.warning("Failed to init config, stderr: {}", result.stderr)- except Exception as e:- logger.warning("Failed to run init-config: {}", e)-- logger.info("Starting OpenSandbox server...")- _OSB_SERVER_PROCESS = subprocess.Popen(- ["opensandbox-server"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True- )-- return _OSB_SERVER_PROCESS- except FileNotFoundError:- logger.error("opensandbox-server command not found.")- logger.error("Please start it manually first: opensandbox-server")- return None- except Exception as e:- logger.error("Failed to start OpenSandbox server: {}", e)- return None---def cleanup_opensandbox_server():- global _OSB_SERVER_PROCESS- if _OSB_SERVER_PROCESS is not None and _OSB_SERVER_PROCESS.poll() is None:- logger.info("Stopping OpenSandbox server...")- _OSB_SERVER_PROCESS.terminate()- try:- _OSB_SERVER_PROCESS.wait(timeout=5)- except subprocess.TimeoutExpired:- _OSB_SERVER_PROCESS.kill()- _OSB_SERVER_PROCESS = None---# Register cleanup on exit-atexit.register(cleanup_opensandbox_server)+from vikingbot.sandbox.managed_server import WORKSPACE_GID_LABEL, WORKSPACE_UID_LABEL@register_backend("opensandbox")class OpenSandboxBackend(SandboxBackend):- def __init__(self, config: "SandboxConfig", session_key: SessionKey, workspace: Path):- # OpenSandbox has built-in isolation, restrict_to_workspace is not needed+ def __init__(+ self,+ config: "SandboxConfig",+ session_key: SessionKey,+ workspace: Path,+ *,+ host_workspace: Path | None = None,+ ):+ # Paths belong to the remote container, not the Bot host filesystem.super().__init__()self.config = configself.session_key = session_keyself._workspace = workspace+ self._host_workspace = host_workspaceself._sandbox = Noneself._connection_config = Noneself._osb_config = config.backends.opensandbox- self._is_vke = _is_kubernetes_env()- if self._is_vke:- self._server_url = "http://opensandbox-server:8080"- logger.info(- "Detected VKE environment, using OpenSandbox server at: {}", self._server_url- )- else:- self._server_url = self._osb_config.server_url- logger.info(- "Detected local environment, using OpenSandbox server at: {}", self._server_url- )+ self._server_url = self._osb_config.server_urlasync def start(self) -> None:self._workspace.mkdir(parents=True, exist_ok=True)- if not self._is_vke:- server_process = _start_opensandbox_server()- if server_process:- ready = await _wait_for_server(self._server_url, timeout=10)- if not ready:- logger.info(- "OpenSandbox server not ready. Please start it manually: opensandbox-server"- )-try:from opensandbox.config import ConnectionConfigfrom opensandbox.sandbox import Sandbox@@ -160,55 +48,64 @@ class OpenSandboxBackend(SandboxBackend):self._connection_config = ConnectionConfig(domain=self._server_url,api_key=self._osb_config.api_key,- request_timeout=timedelta(seconds=300),+ request_timeout=timedelta(seconds=self._osb_config.startup_timeout),+ use_server_proxy=self._osb_config.use_server_proxy,)timeout_seconds = self._osb_config.runtime.timeout- # Configure volumes- volumes = None- if not self._is_vke:- # Local environment: mount host volume- from opensandbox.models.sandboxes import Host, Volume-- volumes = [+ from opensandbox.models.sandboxes import Host, NetworkPolicy, NetworkRule, Volume++ volume_options = {}+ if self._osb_config.managed:+ if self._host_workspace is None:+ raise ValueError("Managed OpenSandbox requires a dedicated host workspace")+ owner = self._host_workspace.stat()+ volume_options["metadata"] = {+ WORKSPACE_UID_LABEL: str(owner.st_uid),+ WORKSPACE_GID_LABEL: str(owner.st_gid),+ }+ # Numeric users may not have a passwd entry or access to image /root.+ volume_options["env"] = {"HOME": "/workspace"}+ volume_options["volumes"] = [Volume(name="workspace",- host=Host(path=str(self._workspace.resolve())),- mountPath="/workspace",+ host=Host(path=str(self._host_workspace.resolve())),+ mount_path="/workspace",+ read_only=False,)]- else:- # VKE environment: always mount TOS PVC to /workspace- try:- from opensandbox.models.sandboxes import Volume-- # Build Volume with PVC using dictionary approach for compatibility- volume_dict = {- "name": "tos-workspace",- "persistentVolumeClaim": {"claimName": "vikingbot-data"},- "mountPath": "/workspace",- }-- # Try to create Volume object- volumes = [Volume(**volume_dict)]- logger.info("Configured TOS PVC mount: vikingbot-data -> /workspace")- except Exception as e:- logger.warning("Failed to create Volume object with PVC, falling back: {}", e)- volumes = None+ network = self._osb_config.network+ policy = NetworkPolicy(+ default_action="deny",+ egress=[+ NetworkRule(action="deny", target=domain) for domain in network.denied_domains+ ]+ + [+ NetworkRule(action="allow", target=domain) for domain in network.allowed_domains+ ],+ )self._sandbox = await Sandbox.create(self._osb_config.default_image,connection_config=self._connection_config,timeout=timedelta(seconds=timeout_seconds),- volumes=volumes,+ ready_timeout=timedelta(seconds=self._osb_config.startup_timeout),+ resource={+ "cpu": self._osb_config.runtime.cpu,+ "memory": self._osb_config.runtime.memory,+ },+ network_policy=policy,+ **volume_options,)+ # External services still use an API-only workspace without local mounts.+ await self._sandbox.commands.run("mkdir -p /workspace")logger.info("OpenSandbox created successfully")except ImportError:logger.error(- "opensandbox SDK not installed. Install with: uv pip install opensandbox>=0.1.0 opensandbox-server>=0.1.0"+ 'OpenSandbox SDK missing or incompatible. Install: pip install "openviking[bot]"')raiseexcept Exception as e:@@ -225,13 +122,13 @@ class OpenSandboxBackend(SandboxBackend):logger.info("[OpenSandbox] Executing: {}", repr(command))if command.strip() == "pwd":- return "/workspace" if self._is_vke else "/"+ return "/workspace"try:from opensandbox.models.execd import RunCommandOptsopts = RunCommandOpts(timeout=timedelta(seconds=timeout))- execution = await self._sandbox.commands.run(command, opts=opts)+ execution = await self._sandbox.commands.run(f"cd /workspace && {command}", opts=opts)output_parts = []@@ -247,7 +144,11 @@ class OpenSandboxBackend(SandboxBackend):[chunk.text for chunk in execution.logs.stderr if chunk.text])- exit_code = execution.exit_code if hasattr(execution, "exit_code") else 0+ error = getattr(execution, "error", None)+ exit_code = getattr(execution, "exit_code", 0)+ if error:+ stderr_text = "\n".join(filter(None, [stderr_text, f"{error.name}: {error.value}"]))+ exit_code = exit_code or 1if stdout_text:output_parts.append(stdout_text)@@ -277,11 +178,11 @@ class OpenSandboxBackend(SandboxBackend):try:if hasattr(self._sandbox, "kill"):await self._sandbox.kill()- if hasattr(self._sandbox, "close"):- await self._sandbox.close()logger.info("OpenSandbox stopped")except Exception as e:logger.warning("Error stopping sandbox: {}", e)+ finally:+ await self._sandbox.close()self._sandbox = Noneself._connection_config = None@@ -289,6 +190,14 @@ class OpenSandboxBackend(SandboxBackend):def is_running(self) -> bool:return self._sandbox is not None+ async def is_healthy(self) -> bool:+ if self._sandbox is None:+ return False+ healthy = await self._sandbox.is_healthy()+ if healthy:+ await self._sandbox.renew(timedelta(seconds=self._osb_config.runtime.timeout))+ return healthy+@propertydef workspace(self) -> Path:return self._workspace@@ -304,103 +213,51 @@ class OpenSandboxBackend(SandboxBackend):return self.sandbox_cwd if not relative else f"{self.sandbox_cwd}/{relative}"def local_file_path(self, path: str) -> Path | None:- if self._is_vke:- return None- return super().local_file_path(path)+ return Noneasync def read_file(self, path: str) -> str:- """Read file from OpenSandbox."""if not self._sandbox:raise SandboxNotStartedError()-- # In VKE environment, use SDK API; in local, use base implementation (host mount)- if self._is_vke:- try:- return await self._sandbox.files.read_file(self._sandbox_path(path))- except Exception as e:- logger.error(f"[OpenSandbox] Failed to read file {path}: {e}")- raise- else:- return await super().read_file(path)+ return await self._sandbox.files.read_file(self._sandbox_path(path))async def write_file(self, path: str, content: str) -> None:- """Write file to OpenSandbox."""if not self._sandbox:raise SandboxNotStartedError()-- # In VKE environment, use SDK API; in local, use base implementation (host mount)- if self._is_vke:- try:- await self._sandbox.files.write_file(self._sandbox_path(path), content, mode=0o644)- except Exception as e:- logger.error(f"[OpenSandbox] Failed to write file {path}: {e}")- raise- else:- await super().write_file(path, content)+ # Execd interprets mode as octal digits (644), not Python's 0o644 (=420).+ await self._sandbox.files.write_file(self._sandbox_path(path), content, mode=644)async def write_file_bytes(self, path: str, content: bytes) -> None:- """Write binary content to OpenSandbox, including remote VKE sandboxes."""if not self._sandbox:raise SandboxNotStartedError()- if not self._is_vke:- await super().write_file_bytes(path, content)- return- sandbox_path = path if path.startswith("/") else f"/workspace/{path}"- try:- await self._sandbox.files.write_file(sandbox_path, content, mode=0o644)- except TypeError:- encoded_path = f"{sandbox_path}.vikingbot-b64"- encoded = base64.b64encode(content).decode("ascii")- await self._sandbox.files.write_file(encoded_path, encoded, mode=0o600)- output = await self.execute(- f"base64 -d {shlex.quote(encoded_path)} > {shlex.quote(sandbox_path)} "- f"&& rm -f {shlex.quote(encoded_path)}"- )- self._ensure_command_succeeded(output, "binary file decode")+ await self._sandbox.files.write_file(self._sandbox_path(path), content, mode=644)async def remove_tree(self, path: str) -> None:if not self._sandbox:raise SandboxNotStartedError()if not path or path.startswith("/") or ".." in Path(path).parts:raise PermissionError("remove_tree requires a safe sandbox-relative path")- if not self._is_vke:- await super().remove_tree(path)- return- sandbox_path = f"/workspace/{path}"- output = await self.execute(f"rm -rf -- {shlex.quote(sandbox_path)}")+ output = await self.execute(f"rm -rf -- {shlex.quote(self._sandbox_path(path))}")self._ensure_command_succeeded(output, "sandbox tree removal")async def list_dir(self, path: str) -> list[tuple[str, bool]]:- """List directory in OpenSandbox."""if not self._sandbox:raise SandboxNotStartedError()+ from opensandbox.models.execd import RunCommandOpts- # In VKE environment, use SDK API; in local, use base implementation (host mount)- if self._is_vke:- try:- sandbox_path = path- if not path.startswith("/"):- sandbox_path = f"/workspace/{path}"-- # Use execute to list directory as fallback- result = await self.execute(f"ls -la {sandbox_path}")-- items = []- lines = result.strip().split("\n")- for line in lines[1:]:- parts = line.split()- if len(parts) >= 9:- name = " ".join(parts[8:])- is_dir = parts[0].startswith("d")- if name not in (".", ".."):- items.append((name, is_dir))-- return items- except Exception as e:- logger.error(f"[OpenSandbox] Failed to list directory {path}: {e}")- raise- else:- return await super().list_dir(path)+ script = (+ "import json, os; "+ f"print(json.dumps([(e.name, e.is_dir()) for e in os.scandir({self._sandbox_path(path)!r})]))"+ )+ # Structured data must not go through execute()'s display truncation or+ # stderr formatting. SDK stdout chunks may split a JSON token anywhere.+ execution = await self._sandbox.commands.run(+ f"python3 -c {shlex.quote(script)}",+ opts=RunCommandOpts(timeout=timedelta(seconds=30)),+ )+ if execution.error:+ raise IOError(f"Sandbox directory listing failed: {execution.error.value}")+ output = "".join(message.text for message in execution.logs.stdout)+ return [(name, is_dir) for name, is_dir in json.loads(output)]async def list_files(self,@@ -409,8 +266,6 @@ class OpenSandboxBackend(SandboxBackend):max_entries: int,) -> list[SandboxFileInfo]:"""List remote VKE files without materializing an unbounded search response."""- if not self._is_vke:- return await super().list_files(path, max_entries=max_entries)if not self._sandbox:raise SandboxNotStartedError()self._validate_max_entries(max_entries)@@ -497,8 +352,6 @@ class OpenSandboxBackend(SandboxBackend):return sorted(files, key=lambda item: item.path)async def read_file_bytes(self, path: str, *, max_bytes: int | None = None) -> bytes:- if not self._is_vke:- return await super().read_file_bytes(path, max_bytes=max_bytes)if not self._sandbox:raise SandboxNotStartedError()self._validate_max_bytes(max_bytes)@@ -516,8 +369,6 @@ class OpenSandboxBackend(SandboxBackend):*,max_bytes: int | None = None,) -> int:- if not self._is_vke:- return await super().export_file(path, destination, max_bytes=max_bytes)if not self._sandbox:raise SandboxNotStartedError()self._validate_max_bytes(max_bytes)diff --git a/bot/vikingbot/sandbox/managed_server.py b/bot/vikingbot/sandbox/managed_server.pynew file mode 100644index 000000000..acba6c9af--- /dev/null+++ b/bot/vikingbot/sandbox/managed_server.py@@ -0,0 +1,68 @@+"""Launch OpenSandbox with loopback ports and workspace-owner execution identities.++OpenSandbox Server 0.1.6 hardcodes 0.0.0.0 for workload and egress port mappings;+its docker.host_ip setting only controls advertised URLs. It also lacks a Docker+execution-user option. Apply these restrictions at the Docker SDK boundary in+this owned subprocess, never in an external server.+"""++from functools import wraps+from importlib.metadata import distribution++WORKSPACE_UID_LABEL = "vikingbot.io/workspace-uid"+WORKSPACE_GID_LABEL = "vikingbot.io/workspace-gid"+++def use_workspace_owner(api_client_class) -> None:+ """Set Docker User before execd starts, so commands and file APIs share one UID.++ The trusted Bot supplies the dedicated workspace owner's numeric UID/GID. Do not+ change ownership or permissions of user files, and do not alter egress/cache+ containers, which have different runtime requirements.+ """+ original = api_client_class.create_container++ @wraps(original)+ def create_container(self, *args, **kwargs):+ labels = kwargs.get("labels") or {}+ if "opensandbox.io/id" in labels:+ parts = [labels.get(key, "") for key in (WORKSPACE_UID_LABEL, WORKSPACE_GID_LABEL)]+ if not all(+ isinstance(part, str) and part.isascii() and part.isdecimal() for part in parts+ ):+ raise ValueError("Managed sandbox requires a numeric workspace UID:GID")+ kwargs["user"] = ":".join(parts)+ return original(self, *args, **kwargs)++ api_client_class.create_container = create_container+++def restrict_published_ports(api_client_class) -> None:+ original = api_client_class.create_host_config++ @wraps(original)+ def create_host_config(self, *args, **kwargs):+ config = original(self, *args, **kwargs)+ for bindings in config.get("PortBindings", {}).values():+ for binding in bindings or []:+ binding["HostIp"] = "127.0.0.1"+ return config++ api_client_class.create_host_config = create_host_config+++def main() -> None:+ import docker++ restrict_published_ports(docker.APIClient)+ use_workspace_owner(docker.APIClient)+ entrypoint = next(+ entry+ for entry in distribution("opensandbox-server").entry_points+ if entry.group == "console_scripts" and entry.name == "opensandbox-server"+ )+ entrypoint.load()()+++if __name__ == "__main__":+ main()diff --git a/bot/vikingbot/sandbox/manager.py b/bot/vikingbot/sandbox/manager.pyindex 07c1902aa..29ba4db90 100644--- a/bot/vikingbot/sandbox/manager.py+++ b/bot/vikingbot/sandbox/manager.py@@ -1,5 +1,6 @@"""Sandbox manager for creating and managing sandbox instances."""+import asynciofrom pathlib import Pathfrom loguru import logger@@ -22,6 +23,7 @@ class SandboxManager:self.workspace = sandbox_parent_pathself.source_workspace = source_workspace_pathself._sandboxes: dict[str, SandboxBackend] = {}+ self._create_lock = asyncio.Lock()self.remote_skill_cache = (RemoteSkillSnapshotCache(config)if getattr(config, "remote_skills", None) is not None@@ -39,22 +41,57 @@ class SandboxManager:async def _get_or_create_sandbox(self, session_key: SessionKey) -> SandboxBackend:"""Get or create session-specific sandbox."""workspace_id = self.to_workspace_id(session_key)- if workspace_id not in self._sandboxes:- sandbox = await self._create_sandbox(- workspace_id,- self.get_workspace_path(session_key),- )- self._sandboxes[workspace_id] = sandbox- return self._sandboxes[workspace_id]+ async with self._create_lock:+ sandbox = self._sandboxes.get(workspace_id)+ if sandbox is not None and self.config.sandbox.backend == "opensandbox":+ if not await sandbox.is_healthy():+ await self.cleanup_session(session_key)+ sandbox = None+ if sandbox is None:+ sandbox = await self._create_sandbox(+ workspace_id,+ self.get_workspace_path(session_key),+ )+ self._sandboxes[workspace_id] = sandbox+ return sandboxasync def _create_sandbox(self, workspace_id: str, workspace: Path) -> SandboxBackend:"""Create new sandbox instance."""- instance = self._backend_cls(self.config.sandbox, workspace_id, workspace)+ backend_options = {}+ managed = self.config.uses_managed_opensandbox+ if managed:+ root = self.config.opensandbox_workspaces_path+ # Never mount credentials, the parent runtime directory, or a symlink escape.+ if root.is_symlink() or workspace.resolve() == root.resolve():+ raise ValueError("Invalid OpenSandbox host workspace")+ if not workspace.resolve().is_relative_to(root.resolve()):+ raise ValueError(+ "OpenSandbox host workspace must be under its workspaces directory"+ )+ backend_options["host_workspace"] = workspace+ instance = self._backend_cls(+ self.config.sandbox, workspace_id, workspace, **backend_options+ )+ needs_bootstrap = not workspace.exists()try:+ if needs_bootstrap and self.config.sandbox.backend == "opensandbox":+ await self._copy_bootstrap_files(workspace)await instance.start()if not workspace.exists():await self._copy_bootstrap_files(workspace)- except Exception:+ if self.config.sandbox.backend == "opensandbox" and not managed:+ # External services have no local mount; upload bootstrap inputs via APIs.+ for name in [*self.COPY_BOOTSTRAP_FILES, "skills"]:+ root = workspace / name+ paths = root.rglob("*") if root.is_dir() else [root]+ for path in paths:+ if path.is_file() and not path.is_symlink():+ if path.resolve().is_relative_to(workspace.resolve()):+ await instance.write_file_bytes(+ path.relative_to(workspace).as_posix(),+ path.read_bytes(),+ )+ except BaseException:logger.exception(f"Failed to start sandbox for workspace {workspace_id}")try:await instance.stop()@@ -102,15 +139,19 @@ class SandboxManager:async def cleanup_session(self, session_key: SessionKey) -> None:"""Clean up sandbox for a session."""workspace_id = self.to_workspace_id(session_key)- if workspace_id in self._sandboxes:- await self._sandboxes[workspace_id].stop()- del self._sandboxes[workspace_id]+ sandbox = self._sandboxes.pop(workspace_id, None)+ if sandbox is not None:+ await sandbox.stop()async def cleanup_all(self) -> None:"""Clean up all sandboxes."""- for sandbox in self._sandboxes.values():- await sandbox.stop()+ sandboxes = list(self._sandboxes.values())self._sandboxes.clear()+ for sandbox in sandboxes:+ try:+ await sandbox.stop()+ except Exception:+ logger.exception("Failed to clean up sandbox")def get_workspace_path(self, session_key: SessionKey) -> Path:return resolve_workspace_path(diff --git a/bot/vikingbot/sandbox/runtime.py b/bot/vikingbot/sandbox/runtime.pynew file mode 100644index 000000000..3ca251846--- /dev/null+++ b/bot/vikingbot/sandbox/runtime.py@@ -0,0 +1,276 @@+"""Gateway-owned OpenSandbox lifecycle. Never starts a container runtime itself."""++import asyncio+import importlib.util+import json+import os+import platform+import secrets+import shutil+import socket+import subprocess+import sys+import tempfile+from importlib.metadata import PackageNotFoundError, distribution+from pathlib import Path+from urllib.parse import urlsplit++import httpx+from loguru import logger++from vikingbot.config.schema import Config, SessionKey+++def docker_install_hint() -> str:+ system = platform.system()+ if system == "Darwin":+ return "Install and start Docker Desktop for Mac: https://docs.docker.com/desktop/setup/install/mac-install/"+ if system == "Windows" or "microsoft" in platform.release().lower():+ return "Install Docker Desktop, enable WSL2/Linux containers and WSL integration: https://docs.docker.com/desktop/setup/install/windows-install/"+ return "Install and start Docker Engine: https://docs.docker.com/engine/install/"+++class OpenSandboxRuntime:+ """One server per Gateway; session and Compile backends only create sandboxes."""++ def __init__(self, config: Config):+ self.config = config+ self.settings = config.sandbox.backends.opensandbox+ self.process = None+ self.log_file = None+ self.runtime_dir: Path | None = None++ async def _command(self, *argv: str, env=None, timeout=30, output_file=None) -> str:+ process = await asyncio.create_subprocess_exec(+ *argv,+ env=env,+ stdin=asyncio.subprocess.DEVNULL,+ stdout=output_file or asyncio.subprocess.PIPE,+ stderr=asyncio.subprocess.STDOUT,+ )+ try:+ stdout, _ = await asyncio.wait_for(process.communicate(), timeout)+ except BaseException:+ if process.returncode is None:+ process.kill()+ await process.wait()+ raise+ output = (stdout or b"").decode(errors="replace").strip()+ if process.returncode:+ raise RuntimeError(output[-2000:] or f"Command failed: {argv[0]} (see OpenSandbox log)")+ return output++ async def _docker_environment(self) -> dict[str, str]:+ if not shutil.which("docker"):+ raise RuntimeError(f"Docker is not installed. {docker_install_hint()}")+ env = os.environ.copy()+ try:+ kind = await self._command("docker", "info", "--format", "{{.OSType}}", env=env)+ except (RuntimeError, asyncio.TimeoutError) as exc:+ raise RuntimeError(+ "Docker is not reachable. Start Docker and check socket permissions / Docker context "+ f"with `docker info`. {docker_install_hint()}\n{exc}"+ ) from exc+ if kind != "linux":+ raise RuntimeError(+ "OpenSandbox requires Linux containers; switch Docker to Linux containers."+ )+ # docker-py does not read the CLI's selected context (notably Docker Desktop).+ if env.get("DOCKER_CONTEXT") or not env.get("DOCKER_HOST"):+ endpoint = await self._command(+ "docker",+ "context",+ "inspect",+ "--format",+ "{{.Endpoints.docker.Host}}",+ env=env,+ )+ env["DOCKER_HOST"] = endpoint+ if not env["DOCKER_HOST"].startswith(("unix://", "npipe://")):+ raise RuntimeError(+ "Managed OpenSandbox requires a local Docker socket. For remote Docker, run "+ "OpenSandbox on that host and set managed=false with its server_url."+ )+ return env++ def _write_config(self) -> Path:+ url = urlsplit(self.settings.server_url)+ if (+ url.scheme != "http"+ or url.hostname not in {"localhost", "127.0.0.1"}+ or url.path not in {"", "/"}+ or url.query+ or url.fragment+ or url.username+ ):+ raise ValueError(+ "Managed OpenSandbox server_url must be http://127.0.0.1:<port> or localhost."+ )+ port = url.port or 80+ with socket.socket() as probe:+ try:+ probe.bind(("127.0.0.1", port))+ except OSError as exc:+ raise RuntimeError(+ f"OpenSandbox port {port} is occupied; choose another server_url port."+ ) from exc+ parent = self.config.bot_data_path / "runtime" / "opensandbox"+ parent.mkdir(parents=True, exist_ok=True)+ mount_root = self.config.opensandbox_workspaces_path+ if mount_root.is_symlink():+ raise ValueError("OpenSandbox workspaces directory must not be a symlink")+ mount_root.mkdir(parents=True, exist_ok=True)+ self.runtime_dir = Path(tempfile.mkdtemp(prefix="gateway-", dir=parent))+ self.settings.api_key = self.settings.api_key or secrets.token_urlsafe(32)+ self.settings.server_url = f"http://127.0.0.1:{port}"++ # JSON string encoding is also valid TOML basic-string encoding.+ def q(value):+ return json.dumps(str(value), ensure_ascii=True)++ content = (+ f'[server]\nhost = "127.0.0.1"\nport = {port}\napi_key = {q(self.settings.api_key)}\n'+ f'[runtime]\ntype = "docker"\nexecd_image = {q(self.settings.execd_image)}\n'+ '[docker]\nnetwork_mode = "bridge"\nhost_ip = "127.0.0.1"\n'+ 'no_new_privileges = true\ndrop_capabilities = ["ALL"]\n'+ f"pids_limit = {self.settings.pids_limit}\n"+ f"[egress]\nimage = {q(self.settings.egress_image)}\n"+ # Newer servers can explicitly use host-mapped ports when proxying.+ "[proxy]\nresolve_internal = false\n"+ # Only dedicated workspaces are mountable; Server credentials are siblings.+ f"[storage]\nallowed_host_paths = [{q(mount_root.resolve())}]\n"+ )+ path = self.runtime_dir / "sandbox.toml"+ with open(+ path, "x", encoding="utf-8", opener=lambda p, flags: os.open(p, flags, 0o600)+ ) as f:+ f.write(content)+ return path++ async def _wait_for_server(self) -> None:+ async with httpx.AsyncClient(timeout=3, trust_env=False) as client:+ while True:+ if self.process is not None and self.process.returncode is not None:+ raise RuntimeError(+ f"OpenSandbox Server exited; inspect {self.runtime_dir / 'server.log'}"+ )+ try:+ response = await client.get(self.settings.server_url.rstrip("/") + "/health")+ if response.status_code == 200:+ # Do not accept an unauthenticated health response as proof the key works.+ response = await client.get(+ self.settings.server_url.rstrip("/") + "/v1/sandboxes",+ headers={"OPEN-SANDBOX-API-KEY": self.settings.api_key},+ )+ if response.status_code in {401, 403}:+ raise RuntimeError("OpenSandbox authentication failed; check api_key.")+ if response.status_code == 200:+ return+ except httpx.HTTPError:+ pass+ await asyncio.sleep(0.25)++ async def start(self, manager) -> None:+ if self.config.sandbox.backend != "opensandbox":+ return+ logger.info("[SANDBOX] Checking OpenSandbox dependencies")+ if importlib.util.find_spec("opensandbox") is None:+ raise RuntimeError(+ 'Missing OpenSandbox SDK. Install Bot dependencies: pip install "openviking[bot]"'+ )+ try:+ await asyncio.wait_for(self._initialize(manager), self.settings.startup_timeout)+ except BaseException as exc:+ try:+ await manager.cleanup_all()+ finally:+ await self.stop()+ if isinstance(exc, asyncio.TimeoutError):+ raise RuntimeError(+ f"OpenSandbox startup exceeded {self.settings.startup_timeout}s. "+ f"Check Docker, image pulls and server logs under {self.runtime_dir}; "+ "increase bot.sandbox.backends.opensandbox.startup_timeout if needed."+ ) from exc+ raise++ async def _initialize(self, manager) -> None:+ if self.settings.managed:+ try:+ distribution("opensandbox-server")+ except PackageNotFoundError as exc:+ raise RuntimeError(+ 'Missing opensandbox-server. Install: pip install "openviking[bot]"'+ ) from exc+ env = await self._docker_environment()+ path = self._write_config()+ self.log_file = (self.runtime_dir / "server.log").open("ab", buffering=0)+ for image in (+ self.settings.default_image,+ self.settings.execd_image,+ self.settings.egress_image,+ ):+ logger.info(+ "[SANDBOX] Preparing image {} (first pull can take several minutes)",+ image,+ )+ try:+ await self._command("docker", "image", "inspect", image, env=env)+ except RuntimeError:+ await self._command(+ "docker",+ "pull",+ image,+ env=env,+ timeout=self.settings.startup_timeout,+ output_file=self.log_file,+ )+ logger.info("[SANDBOX] Starting managed server at {}", self.settings.server_url)+ self.process = await asyncio.create_subprocess_exec(+ sys.executable,+ "-m",+ "vikingbot.sandbox.managed_server",+ "--config",+ str(path),+ env=env,+ stdin=asyncio.subprocess.DEVNULL,+ stdout=self.log_file,+ stderr=asyncio.subprocess.STDOUT,+ # Ctrl+C targets the foreground process group. Keep this service+ # alive until Gateway has deleted its sandboxes through the API.+ start_new_session=os.name != "nt",+ creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if os.name == "nt" else 0,+ )+ await self._wait_for_server()+ logger.info("[SANDBOX] Creating sandbox and verifying command/file operations")+ key = SessionKey(type="startup", channel_id="probe", chat_id=secrets.token_hex(8))+ sandbox = await manager.get_sandbox(key)+ marker = secrets.token_hex(16)+ filename = f".bot-startup-{marker}"+ try:+ await sandbox.write_file(filename, marker)+ if (await sandbox.read_file(filename)).strip() != marker:+ raise RuntimeError("OpenSandbox file read/write probe failed")+ if (await sandbox.execute(f"cat /workspace/{filename}")).strip() != marker:+ raise RuntimeError("OpenSandbox command probe failed")+ await sandbox.execute(f"rm /workspace/{filename}")+ finally:+ if self.config.sandbox.mode != "shared":+ await manager.cleanup_session(key)+ logger.info("[SANDBOX] OpenSandbox ready")++ async def stop(self) -> None:+ if self.process is not None:+ if self.process.returncode is None:+ self.process.terminate()+ try:+ await asyncio.wait_for(self.process.wait(), 10)+ except asyncio.TimeoutError:+ self.process.kill()+ await self.process.wait()+ self.process = None+ if self.log_file is not None:+ self.log_file.close()+ self.log_file = None+ # Preserve diagnostics, remove the generated credential-bearing config.+ if self.runtime_dir is not None:+ (self.runtime_dir / "sandbox.toml").unlink(missing_ok=True)diff --git a/bot/vikingbot/utils/startup.py b/bot/vikingbot/utils/startup.pynew file mode 100644index 000000000..be488a674--- /dev/null+++ b/bot/vikingbot/utils/startup.py@@ -0,0 +1,20 @@+"""Readiness handshake with the parent OpenViking process."""++import json+import os+from pathlib import Path+++def report_startup(status: str, *, timeout: int = 600, error: str = "") -> None:+ destination = os.environ.get("VIKINGBOT_STARTUP_STATUS")+ if not destination:+ return+ path = Path(destination)+ if not path.parent.exists():+ return # The parent removes the handshake directory after readiness.+ temporary = path.with_suffix(".tmp")+ temporary.write_text(+ json.dumps({"pid": os.getpid(), "status": status, "timeout": timeout, "error": error}),+ encoding="utf-8",+ )+ temporary.replace(path)diff --git a/openviking/server/bootstrap.py b/openviking/server/bootstrap.pyindex 6b857a4fa..fc3331549 100644--- a/openviking/server/bootstrap.py+++ b/openviking/server/bootstrap.py@@ -10,6 +10,7 @@ import shutilimport socketimport subprocessimport sys+import tempfileimport timefrom dataclasses import dataclassfrom pathlib import Path@@ -237,6 +238,7 @@ def main():# 🔍 Authentication health check - CRITICAL: will exit if check failstry:from openviking.server.auth.health_check import run_startup_health_check_or_exit+asyncio.run(run_startup_health_check_or_exit(config))except Exception as e:# Don't fail startup if health check itself has issues@@ -370,6 +372,29 @@ def _handle_vikingbot_failure(output: str, returncode: int) -> None:print(f"\nDetailed error:\n{output}", file=sys.stderr)+def _wait_for_bot_ready(process, status_path: Path) -> None:+ """A running child is not ready until its sandbox and HTTP server are ready."""+ started = time.monotonic()+ timeout = 900+ while time.monotonic() - started < timeout:+ if process.poll() is not None:+ raise RuntimeError(+ f"VikingBot exited before becoming ready (code {process.returncode})."+ )+ try:+ status = json.loads(status_path.read_text(encoding="utf-8"))+ except (FileNotFoundError, ValueError):+ status = {}+ if status.get("pid") == process.pid:+ timeout = max(timeout, int(status.get("timeout", timeout)))+ if status.get("status") == "failed":+ raise RuntimeError(status.get("error") or "VikingBot initialization failed")+ if status.get("status") == "ready":+ return+ time.sleep(0.1)+ raise TimeoutError("VikingBot readiness timed out; check Docker/image pulls and the Bot log.")++def _start_vikingbot_gateway(enable_logging: bool,log_dir: str,@@ -402,11 +427,14 @@ def _start_vikingbot_gateway(return Nonevikingbot_cmd.extend(["--host", VIKINGBOT_DEFAULT_HOST, "--port", str(port)])+ resolved_config = resolve_config_path(config_path, OPENVIKING_CONFIG_ENV, DEFAULT_OV_CONF)+ if resolved_config is not None:+ vikingbot_cmd.extend(["--config", str(resolved_config)])# Prepare logginglog_file = None- stdout_handler = subprocess.PIPE- stderr_handler = subprocess.PIPE+ stdout_handler = None+ stderr_handler = Nonelog_file_path = Noneif enable_logging:@@ -423,13 +451,18 @@ def _start_vikingbot_gateway(if log_file:log_file.close()log_file = None- stdout_handler = subprocess.PIPE- stderr_handler = subprocess.PIPE+ stdout_handler = None+ stderr_handler = None+ # Keep the handshake outside the sandbox workspace and unique to this child.+ startup_directory = tempfile.TemporaryDirectory(prefix="vikingbot-startup-")+ status_path = Path(startup_directory.name) / "status.json"+ process = None# Start vikingbot gateway processtry:# Set environment to ensure it uses the same Python environmentenv = os.environ.copy()+ env["VIKINGBOT_STARTUP_STATUS"] = str(status_path)cli_config_path = _resolve_cli_config_for_bot(config_path)if cli_config_path is not None:env[OPENVIKING_CLI_CONFIG_ENV] = cli_config_path@@ -443,32 +476,36 @@ def _start_vikingbot_gateway(stderr=stderr_handler,text=True,env=env,+ # Parent handles terminal signals and then shuts Gateway down once.+ # Avoid Ctrl+C cancelling child cleanup before its sandbox is released.+ start_new_session=os.name != "nt",+ creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if os.name == "nt" else 0,)- # Wait a moment to check if it started successfully- time.sleep(2)- if process.poll() is not None:- # Process exited early- if log_file:- log_file.close()- if log_file_path:- with open(log_file_path, "r") as f:- output = f.read()- _handle_vikingbot_failure(output, process.returncode)- else:- stdout, stderr = process.communicate(timeout=1)- _handle_vikingbot_failure(stderr, process.returncode)- sys.exit(1)+ _wait_for_bot_ready(process, status_path)print(f"Vikingbot gateway started (PID: {process.pid})")return BotProcess(process=process, log_file=log_file)- except Exception as e:+ except BaseException as e:+ if process is not None and process.poll() is None:+ process.terminate()+ try:+ process.wait(timeout=30)+ except subprocess.TimeoutExpired:+ process.kill()+ process.wait()if log_file:log_file.close()- print(f"Warning: Failed to start vikingbot gateway: {e}")+ print(f"Failed to start vikingbot gateway: {e}")+ if log_file_path:+ print(f"VikingBot startup details: {log_file_path}")+ if isinstance(e, (KeyboardInterrupt, SystemExit)):+ raisereturn None+ finally:+ startup_directory.cleanup()def _stop_vikingbot_gateway(bot_process: BotProcess) -> None:@@ -482,7 +519,7 @@ def _stop_vikingbot_gateway(bot_process: BotProcess) -> None:# Try graceful termination firstbot_process.process.terminate()try:- bot_process.process.wait(timeout=5)+ bot_process.process.wait(timeout=30)print("Vikingbot gateway stopped gracefully.")except subprocess.TimeoutExpired:# Force kill if it doesn't stop in timediff --git a/pyproject.toml b/pyproject.tomlindex 722d91bac..0157bb9bb 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -168,8 +168,8 @@ bot = ["dingtalk-stream>=0.4.0","slack-sdk>=3.26.0","qq-botpy>=1.0.0",- "opensandbox>=0.1.0",- "opensandbox-server>=0.1.0",+ "opensandbox>=0.1.5",+ "opensandbox-server>=0.1.6","agent-sandbox>=0.0.23","fusepy>=3.0.1","opencode-ai>=0.1.0a0",diff --git a/tests/unit/test_server_bootstrap_bot_gateway.py b/tests/unit/test_server_bootstrap_bot_gateway.pyindex cc972f867..11e75925c 100644--- a/tests/unit/test_server_bootstrap_bot_gateway.py+++ b/tests/unit/test_server_bootstrap_bot_gateway.py@@ -2,6 +2,11 @@# SPDX-License-Identifier: AGPL-3.0+import json+from types import SimpleNamespace++import pytest+import openviking.server.bootstrap as bootstrapfrom openviking_cli.utils.config.consts import OPENVIKING_CLI_CONFIG_ENV@@ -19,18 +24,20 @@ def test_start_vikingbot_gateway_forces_localhost_host(monkeypatch):monkeypatch.setattr(bootstrap.shutil, "which", lambda name: "/usr/bin/vikingbot")monkeypatch.delenv(OPENVIKING_CLI_CONFIG_ENV, raising=False)- def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None):+ def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None, **kwargs):captured["cmd"] = cmdcaptured["env"] = env+ captured["process_options"] = kwargsreturn _FakeProcess()monkeypatch.setattr(bootstrap.subprocess, "Popen", _fake_popen)- monkeypatch.setattr(bootstrap.time, "sleep", lambda _: None)+ monkeypatch.setattr(bootstrap, "_wait_for_bot_ready", lambda *_: None)process = bootstrap._start_vikingbot_gateway(enable_logging=False, log_dir="/tmp/logs")assert process is not Noneassert captured["cmd"][:2] == ["vikingbot", "gateway"]+ assert captured["process_options"]["start_new_session"] == (bootstrap.os.name != "nt")assert "--host" in captured["cmd"]assert captured["cmd"][captured["cmd"].index("--host") + 1] == "127.0.0.1"assert "--port" in captured["cmd"]@@ -45,13 +52,14 @@ def test_start_vikingbot_gateway_uses_custom_port(monkeypatch):monkeypatch.setattr(bootstrap.shutil, "which", lambda name: "/usr/bin/vikingbot")monkeypatch.delenv(OPENVIKING_CLI_CONFIG_ENV, raising=False)- def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None):+ def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None, **kwargs):captured["cmd"] = cmdcaptured["env"] = env+ captured["process_options"] = kwargsreturn _FakeProcess()monkeypatch.setattr(bootstrap.subprocess, "Popen", _fake_popen)- monkeypatch.setattr(bootstrap.time, "sleep", lambda _: None)+ monkeypatch.setattr(bootstrap, "_wait_for_bot_ready", lambda *_: None)process = bootstrap._start_vikingbot_gateway(enable_logging=False,@@ -73,13 +81,14 @@ def test_start_vikingbot_gateway_prefers_colocated_ovcli_conf(monkeypatch, tmp_pmonkeypatch.setattr(bootstrap.shutil, "which", lambda name: "/usr/bin/vikingbot")- def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None):+ def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None, **kwargs):captured["cmd"] = cmdcaptured["env"] = env+ captured["process_options"] = kwargsreturn _FakeProcess()monkeypatch.setattr(bootstrap.subprocess, "Popen", _fake_popen)- monkeypatch.setattr(bootstrap.time, "sleep", lambda _: None)+ monkeypatch.setattr(bootstrap, "_wait_for_bot_ready", lambda *_: None)monkeypatch.delenv(OPENVIKING_CLI_CONFIG_ENV, raising=False)process = bootstrap._start_vikingbot_gateway(@@ -90,6 +99,7 @@ def test_start_vikingbot_gateway_prefers_colocated_ovcli_conf(monkeypatch, tmp_passert process is not Noneassert captured["env"][OPENVIKING_CLI_CONFIG_ENV] == str(cli_config_path)+ assert captured["cmd"][captured["cmd"].index("--config") + 1] == str(config_path)def test_start_vikingbot_gateway_preserves_explicit_cli_config_env(monkeypatch, tmp_path):@@ -103,13 +113,14 @@ def test_start_vikingbot_gateway_preserves_explicit_cli_config_env(monkeypatch,monkeypatch.setattr(bootstrap.shutil, "which", lambda name: "/usr/bin/vikingbot")- def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None):+ def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None, **kwargs):captured["cmd"] = cmdcaptured["env"] = env+ captured["process_options"] = kwargsreturn _FakeProcess()monkeypatch.setattr(bootstrap.subprocess, "Popen", _fake_popen)- monkeypatch.setattr(bootstrap.time, "sleep", lambda _: None)+ monkeypatch.setattr(bootstrap, "_wait_for_bot_ready", lambda *_: None)monkeypatch.setenv(OPENVIKING_CLI_CONFIG_ENV, str(explicit_cli_config))process = bootstrap._start_vikingbot_gateway(@@ -127,12 +138,13 @@ def test_start_vikingbot_gateway_passes_managed_server_runtime(monkeypatch):monkeypatch.setattr(bootstrap.shutil, "which", lambda name: "/usr/bin/vikingbot")- def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None):+ def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None, **kwargs):captured["env"] = env+ captured["process_options"] = kwargsreturn _FakeProcess()monkeypatch.setattr(bootstrap.subprocess, "Popen", _fake_popen)- monkeypatch.setattr(bootstrap.time, "sleep", lambda _: None)+ monkeypatch.setattr(bootstrap, "_wait_for_bot_ready", lambda *_: None)process = bootstrap._start_vikingbot_gateway(enable_logging=False,@@ -155,13 +167,13 @@ def test_start_vikingbot_gateway_allows_slow_module_probe(monkeypatch):captured["probe_timeout"] = timeoutreturn type("Result", (), {"returncode": 0})()- def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None):+ def _fake_popen(cmd, stdout=None, stderr=None, text=None, env=None, **kwargs):captured["cmd"] = cmdreturn _FakeProcess()monkeypatch.setattr(bootstrap.subprocess, "run", _fake_run)monkeypatch.setattr(bootstrap.subprocess, "Popen", _fake_popen)- monkeypatch.setattr(bootstrap.time, "sleep", lambda _: None)+ monkeypatch.setattr(bootstrap, "_wait_for_bot_ready", lambda *_: None)process = bootstrap._start_vikingbot_gateway(enable_logging=False, log_dir="/tmp/logs")@@ -169,3 +181,50 @@ def test_start_vikingbot_gateway_allows_slow_module_probe(monkeypatch):assert captured["probe_cmd"][1:] == ["-m", "vikingbot", "--help"]assert captured["probe_timeout"] == 15assert captured["cmd"][1:4] == ["-m", "vikingbot", "gateway"]+++def test_readiness_waits_for_matching_child_pid(monkeypatch, tmp_path):+ status = tmp_path / "status.json"+ process = SimpleNamespace(pid=123, poll=lambda: None)+ status.write_text(json.dumps({"pid": 999, "status": "ready"}))+ polls = []++ def advance(_):+ polls.append(1)+ state = "starting" if len(polls) == 1 else "ready"+ status.write_text(json.dumps({"pid": 123, "status": state}))++ monkeypatch.setattr(bootstrap.time, "sleep", advance)+ bootstrap._wait_for_bot_ready(process, status)+ assert len(polls) == 2+++def test_readiness_reports_sandbox_failure(tmp_path):+ status = tmp_path / "status.json"+ status.write_text(json.dumps({"pid": 123, "status": "failed", "error": "Docker missing"}))+ process = SimpleNamespace(pid=123, poll=lambda: None)+ with pytest.raises(RuntimeError, match="Docker missing"):+ bootstrap._wait_for_bot_ready(process, status)+++def test_readiness_rejects_early_exit(tmp_path):+ process = SimpleNamespace(pid=123, poll=lambda: 1, returncode=1)+ with pytest.raises(RuntimeError, match="exited before becoming ready"):+ bootstrap._wait_for_bot_ready(process, tmp_path / "status.json")+++def test_failed_readiness_terminates_owned_child(monkeypatch, tmp_path):+ from unittest.mock import Mock++ process = Mock(pid=123)+ process.poll.return_value = None+ monkeypatch.setattr(bootstrap.shutil, "which", lambda _: "/bin/vikingbot")+ monkeypatch.setattr(bootstrap.subprocess, "Popen", lambda *_, **__: process)++ def fail(*_):+ raise TimeoutError("startup timeout")++ monkeypatch.setattr(bootstrap, "_wait_for_bot_ready", fail)+ assert bootstrap._start_vikingbot_gateway(False, str(tmp_path)) is None+ process.terminate.assert_called_once()+ process.wait.assert_called_once()diff --git a/uv.lock b/uv.lockindex 24539213f..35c01e2b3 100644--- a/uv.lock+++ b/uv.lock@@ -3626,8 +3626,8 @@ requires-dist = [{ name = "myst-parser", marker = "extra == 'doc'", specifier = ">=2.0.0" },{ name = "openai", specifier = ">=1.0.0" },{ name = "opencode-ai", marker = "extra == 'bot'", specifier = ">=0.1.0a0" },- { name = "opensandbox", marker = "extra == 'bot'", specifier = ">=0.1.0" },- { name = "opensandbox-server", marker = "extra == 'bot'", specifier = ">=0.1.0" },+ { name = "opensandbox", marker = "extra == 'bot'", specifier = ">=0.1.5" },+ { name = "opensandbox-server", marker = "extra == 'bot'", specifier = ">=0.1.6" },{ name = "opentelemetry-api", specifier = ">=1.14" },{ name = "opentelemetry-exporter-otlp-proto-grpc", specifier = ">=1.14" },{ name = "opentelemetry-exporter-otlp-proto-http", specifier = ">=1.14" },