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): 支持关闭 Direct 命令执行 (#5999)
Qin Haojie1d95584c1338
diff --git a/bot/README.md b/bot/README.mdindex 2e65544aa..afbfff322 100644--- a/bot/README.md+++ b/bot/README.md@@ -320,7 +320,8 @@ Restart `vikingbot gateway` after changing the configuration.| `bot.gateway.port` | `18790` | Gateway listen port || `bot.sandbox.backend` | `direct` | Execution backend || `bot.sandbox.mode` | `shared` | Workspace isolation mode |-| `bot.sandbox.backends.direct.allow_compile_exec` | `false` | Compile uses dedicated read tools; set to `true` to allow Compile agents to execute host commands |+| `bot.sandbox.backends.direct.allow_exec` | `true` | Allow agents to execute host commands through the built-in `exec` tool; disabling it also removes the tool from subagents and Compile |+| `bot.sandbox.backends.direct.allow_compile_exec` | `false` | Compile uses dedicated read tools; both this setting and `allow_exec` must be `true` for Compile agents to execute host commands || `bot.heartbeat.enabled` | `true` | Whether to check `HEARTBEAT.md` periodically || `bot.heartbeat.interval_seconds` | `600` | Heartbeat interval || `bot.mode` | `normal` | One of `normal`, `readonly`, or `debug` |@@ -481,7 +482,22 @@ Workspace modes:- `per-session`: every Session has an independent workspace;- `per-channel`: sessions on the same channel instance share a workspace.-DirectBackend defaults to `restrict_to_workspace: false`. For a Gateway exposed to untrusted users, choose an isolated backend and configure channel allowlists and network/file policies.+Direct allows agents to execute host commands by default. To disable this capability, set the following in `ov.conf` and restart Bot:++```json+{+ "bot": {+ "sandbox": {+ "backend": "direct",+ "backends": {+ "direct": { "allow_exec": false }+ }+ }+ }+}+```++Chat, subagents, and Compile can no longer call the built-in `exec` tool when disabled; chat, retrieval, and workspace file tools remain available. `allow_compile_exec=true` cannot override this setting. The switch only controls agent command execution with Direct: it does not affect other sandbox backends or provide file or network isolation. For a Gateway exposed to untrusted users, choose an isolated backend and configure channel allowlists and network/file policies.```json{diff --git a/bot/README_CN.md b/bot/README_CN.mdindex 28fb41ee7..9e997d0c5 100644--- a/bot/README_CN.md+++ b/bot/README_CN.md@@ -317,7 +317,8 @@ export OPENVIKING_CONFIG_FILE=/path/to/ov.conf| `bot.gateway.port` | `18790` | Gateway 监听端口 || `bot.sandbox.backend` | `direct` | 执行后端 || `bot.sandbox.mode` | `shared` | 工作区隔离方式 |-| `bot.sandbox.backends.direct.allow_compile_exec` | `false` | Compile 默认使用专用读取工具;设为 `true` 允许 Compile Agent 在宿主机执行命令 |+| `bot.sandbox.backends.direct.allow_exec` | `true` | 是否允许 Agent 通过内置 `exec` 工具在宿主机执行命令;关闭后也禁止子 Agent 和 Compile 使用该工具 |+| `bot.sandbox.backends.direct.allow_compile_exec` | `false` | Compile 默认使用专用读取工具;只有此项和 `allow_exec` 均为 `true` 时,才允许 Compile Agent 在宿主机执行命令 || `bot.heartbeat.enabled` | `true` | 是否周期检查 `HEARTBEAT.md` || `bot.heartbeat.interval_seconds` | `600` | 心跳间隔 || `bot.mode` | `normal` | 可选 `normal`、`readonly`、`debug` |@@ -478,7 +479,22 @@ Agent 实际使用的活动目录还取决于 `bot.sandbox.mode`:- `per-session`:每个 Session 独立;- `per-channel`:同一渠道实例共享。-DirectBackend 默认 `restrict_to_workspace: false`。对不可信用户开放 Gateway 时,应选择隔离后端,并设置渠道白名单和网络/文件策略。+Direct 默认允许 Agent 在宿主机执行命令。要关闭这项能力,在 `ov.conf` 中设置并重启 Bot:++```json+{+ "bot": {+ "sandbox": {+ "backend": "direct",+ "backends": {+ "direct": { "allow_exec": false }+ }+ }+ }+}+```++关闭后,聊天、子 Agent 和 Compile 均不能调用内置 `exec` 工具;聊天、检索和工作区文件工具仍可使用。`allow_compile_exec=true` 不能覆盖此设置。此开关仅针对 Direct 的 Agent 命令执行,不影响其他沙箱后端,也不提供文件或网络隔离。对不可信用户开放 Gateway 时,应选择隔离后端,并设置渠道白名单和网络/文件策略。```json{diff --git a/bot/tests/test_exec_tool.py b/bot/tests/test_exec_tool.pyindex b395876a3..5ddfad141 100644--- a/bot/tests/test_exec_tool.py+++ b/bot/tests/test_exec_tool.py@@ -1,12 +1,13 @@# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.# SPDX-License-Identifier: AGPL-3.0-"""Regression tests for the exec tool working directory."""+"""Regression tests for agent command access and working directories."""from types import SimpleNamespaceimport pytest-from vikingbot.agent.tools.shell import ExecTool-from vikingbot.config.schema import SessionKey+from vikingbot.agent.tools.factory import register_default_tools+from vikingbot.agent.tools.registry import ToolRegistry+from vikingbot.config.schema import Config, SessionKeyfrom vikingbot.sandbox.backends.direct import DirectBackend@@ -17,40 +18,53 @@ class _SandboxManager:async def get_sandbox(self, _session_key):return self.sandbox+ def to_workspace_id(self, _session_key):+ return "exec-test"+@pytest.mark.asyncio[email protected]("relative_dir", [None, "directory with spaces"])-async def test_exec_tool_runs_in_selected_directory(tmp_path, relative_dir):- class Config:- restrict_workspaces = True+ ("relative_dir", "allow_exec"),+ [(None, True), ("directory with spaces", True), (None, False)],+)+async def test_exec_tool_runs_in_selected_directory(tmp_path, relative_dir, allow_exec):+ config = Config()+ if not allow_exec:+ config.sandbox.backends.direct.allow_exec = False+ registry = ToolRegistry(config)+ register_default_tools(registry, config)workspace = tmp_path / "workspace"working_dir = workspace / relative_dir if relative_dir else workspaceworking_dir.mkdir(parents=True)session_key = SessionKey(type="cli", channel_id="default", chat_id="exec-test")- sandbox = DirectBackend(Config(), session_key, workspace)+ sandbox = DirectBackend(config.sandbox, session_key, workspace)await sandbox.start()try:- context = SimpleNamespace(- sandbox_manager=_SandboxManager(sandbox),+ result = await registry.execute(+ "exec",+ {+ "command": "python3 -c 'import os; print(os.getcwd())'",+ **({"working_dir": relative_dir} if relative_dir else {}),+ },session_key=session_key,- )- result = await ExecTool().execute(- context,- command="python3 -c 'import os; print(os.getcwd())'",- working_dir=relative_dir,+ sandbox_manager=_SandboxManager(sandbox),)finally:await sandbox.stop()- assert result.strip() == str(working_dir)+ if allow_exec:+ assert result.strip() == str(working_dir.resolve())+ else:+ assert result == "Error: Tool 'exec' not found"def test_compile_exec_requires_opt_in():from vikingbot.compile.service import BotCompileService- from vikingbot.config.schema import Configservice = SimpleNamespace(config=Config())assert not BotCompileService._compile_capabilities(service).exec_enabledservice.config.sandbox.backends.direct.allow_compile_exec = Trueassert BotCompileService._compile_capabilities(service).exec_enabled+ service.config.sandbox.backends.direct.allow_exec = False+ assert not BotCompileService._compile_capabilities(service).exec_enableddiff --git a/bot/vikingbot/agent/tools/factory.py b/bot/vikingbot/agent/tools/factory.pyindex 6337d5efa..bc857bbbc 100644--- a/bot/vikingbot/agent/tools/factory.py+++ b/bot/vikingbot/agent/tools/factory.py@@ -20,6 +20,7 @@ from vikingbot.agent.tools.registry import ToolRegistryfrom vikingbot.agent.tools.shell import ExecToolfrom vikingbot.agent.tools.web import WebFetchToolfrom vikingbot.agent.tools.websearch import WebSearchTool+from vikingbot.config.schema import SandboxBackendif TYPE_CHECKING:from vikingbot.agent.subagent import SubagentManager@@ -74,12 +75,13 @@ def register_default_tools(registry.register(EditFileTool())registry.register(ListDirTool())- # Shell tool- registry.register(- ExecTool(- timeout=exec_config.timeout,+ # All agents share this registration, including Compile and subagents.+ if config.sandbox.backend != SandboxBackend.DIRECT or config.sandbox.backends.direct.allow_exec:+ registry.register(+ ExecTool(+ timeout=exec_config.timeout,+ ))- )# Web toolsregistry.register(diff --git a/bot/vikingbot/compile/service.py b/bot/vikingbot/compile/service.pyindex 987dacc28..ab5137a17 100644--- a/bot/vikingbot/compile/service.py+++ b/bot/vikingbot/compile/service.py@@ -421,11 +421,8 @@ class BotCompileService:except (TypeError, ValueError):return CompileCapabilities(exec_enabled=False)if backend == SandboxBackend.DIRECT:- backends = getattr(sandbox, "backends", None)- direct = getattr(backends, "direct", None)- return CompileCapabilities(- exec_enabled=bool(getattr(direct, "allow_compile_exec", False))- )+ direct = sandbox.backends.direct+ return CompileCapabilities(exec_enabled=direct.allow_exec and direct.allow_compile_exec)return CompileCapabilities(exec_enabled=backend in _COMPILE_ISOLATED_EXEC_BACKENDS)async def _admit(self, principal_scope: str) -> None:diff --git a/bot/vikingbot/config/schema.py b/bot/vikingbot/config/schema.pyindex 3fdcd89d5..0c3b4e64d 100644--- a/bot/vikingbot/config/schema.py+++ b/bot/vikingbot/config/schema.py@@ -799,6 +799,7 @@ class DirectBackendConfig(BaseModel):"""Direct backend configuration."""restrict_to_workspace: bool = False # If true, restrict file access to workspace directory+ allow_exec: bool = Field(default=True, description="Allow agents to execute host commands")allow_compile_exec: bool = False