From 44e29cbebb50033df3a8be4fb47d69f2e9b93741 Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Fri, 9 Oct 2026 12:12:31 +0800 Subject: [PATCH] fix(agent): keep computer-use tools under a subagent whitelist and log misses A persona tool whitelist on a subagent silently stripped the builtin execution tools. The main agent applies the runtime computer-use tools after the persona whitelist narrowing and never routes them through the whitelist, so a whitelist only narrows plugin/MCP tools there. The subagent handoff built its toolset from the whitelist alone, and since the dashboard checkbox UI only lists plugin tools, builtin names never appear in the list and the subagent lost shell, python and file access entirely. Seed the whitelist-built toolset with the runtime computer-use tools so both paths agree. The seed now covers the shipyard_neo browser tools under the same capabilities gate the main agent uses, skips and logs runtime tools that were deactivated, and an empty whitelist now flows through the same construction instead of returning early, so it keeps the runtime tools and an empty result still reaches the warning. Unknown and inactive whitelist entries keep their warnings too. Signed-off-by: Yufeng He <40085740+he-yufeng@users.noreply.github.com> --- astrbot/core/astr_agent_tool_exec.py | 77 ++++++++++- tests/unit/test_astr_agent_tool_exec.py | 166 ++++++++++++++++++++++++ 2 files changed, 237 insertions(+), 6 deletions(-) diff --git a/astrbot/core/astr_agent_tool_exec.py b/astrbot/core/astr_agent_tool_exec.py index eefa0b912e4..fcd07e0139e 100644 --- a/astrbot/core/astr_agent_tool_exec.py +++ b/astrbot/core/astr_agent_tool_exec.py @@ -32,6 +32,8 @@ from astrbot.core.provider.entites import ProviderRequest from astrbot.core.provider.register import llm_tools from astrbot.core.tools.computer_tools import ( + BrowserBatchExecTool, + BrowserExecTool, CuaKeyboardTypeTool, CuaMouseClickTool, CuaScreenshotTool, @@ -45,6 +47,7 @@ LocalExecuteShellTool, LocalPythonTool, PythonTool, + RunBrowserSkillTool, ShellSessionTool, ) from astrbot.core.tools.message_tools import SendMessageToUserTool @@ -199,6 +202,7 @@ def _get_runtime_computer_tools( runtime: str, tool_mgr, booter: str | None = None, + session_id: str | None = None, ) -> dict[str, FunctionTool]: booter = "" if booter is None else str(booter).lower() if runtime == "sandbox": @@ -231,6 +235,29 @@ def _get_runtime_computer_tools( keyboard_type_tool.name: keyboard_type_tool, } ) + if booter == "shipyard_neo": + # Same gate as the main agent: browser tools ride along when the + # booted sandbox profile supports them, or before the first boot + # when capabilities are still unknown. + from astrbot.core.computer.computer_client import session_booter + + existing_booter = session_booter.get(session_id) if session_id else None + capabilities = ( + getattr(existing_booter, "capabilities", None) + if existing_booter is not None + else None + ) + if capabilities is None or "browser" in capabilities: + browser_exec_tool = tool_mgr.get_builtin_tool(BrowserExecTool) + browser_batch_tool = tool_mgr.get_builtin_tool(BrowserBatchExecTool) + browser_skill_tool = tool_mgr.get_builtin_tool(RunBrowserSkillTool) + tools.update( + { + browser_exec_tool.name: browser_exec_tool, + browser_batch_tool.name: browser_batch_tool, + browser_skill_tool.name: browser_skill_tool, + } + ) return tools if runtime == "local": shell_tool = LocalExecuteShellTool() @@ -271,6 +298,7 @@ def _build_handoff_toolset( runtime, tool_mgr, provider_settings.get("sandbox", {}).get("booter"), + session_id=event.unified_msg_origin, ) # Keep persona semantics aligned with the main agent: tools=None means @@ -288,24 +316,61 @@ def _build_handoff_toolset( if registered_tool.active: toolset.add_tool(registered_tool) for runtime_tool in runtime_computer_tools.values(): - toolset.add_tool(runtime_tool) + if runtime_tool.active: + toolset.add_tool(runtime_tool) return None if toolset.empty() else toolset - if not tools: - return None - + # Mirror the main agent: a persona tool whitelist narrows the plugin + # and MCP tools, but the runtime computer-use tools stay available + # even when the whitelist cannot name them (the dashboard only lists + # plugin tools, so builtin tool names never appear in `tools`). An + # empty whitelist keeps the runtime tools too, same as an empty + # persona whitelist on the main agent. toolset = ToolSet() + unknown_names: list[str] = [] + inactive_names: list[str] = [] + inactive_runtime_names: list[str] = [] + for runtime_tool in runtime_computer_tools.values(): + if runtime_tool.active: + toolset.add_tool(runtime_tool) + else: + inactive_runtime_names.append(runtime_tool.name) for tool_name_or_obj in tools: if isinstance(tool_name_or_obj, str): registered_tool = llm_tools.get_func(tool_name_or_obj) - if registered_tool and registered_tool.active: - toolset.add_tool(registered_tool) + if registered_tool is not None: + if registered_tool.active: + toolset.add_tool(registered_tool) + else: + inactive_names.append(tool_name_or_obj) continue runtime_tool = runtime_computer_tools.get(tool_name_or_obj) if runtime_tool: toolset.add_tool(runtime_tool) + else: + unknown_names.append(tool_name_or_obj) elif isinstance(tool_name_or_obj, FunctionTool): toolset.add_tool(tool_name_or_obj) + if unknown_names: + logger.warning( + "Subagent tool whitelist references unknown tools (skipped): %s", + ", ".join(sorted(unknown_names)), + ) + if inactive_names: + logger.warning( + "Subagent tool whitelist references inactive tools (skipped): %s", + ", ".join(sorted(inactive_names)), + ) + if inactive_runtime_names: + logger.warning( + "Subagent handoff skips deactivated runtime tools: %s", + ", ".join(sorted(inactive_runtime_names)), + ) + if toolset.empty(): + logger.warning( + "Subagent handoff ended up with an empty toolset; the agent " + "will run without any tools." + ) return None if toolset.empty() else toolset @classmethod diff --git a/tests/unit/test_astr_agent_tool_exec.py b/tests/unit/test_astr_agent_tool_exec.py index 139cefcd118..bea97eb866b 100644 --- a/tests/unit/test_astr_agent_tool_exec.py +++ b/tests/unit/test_astr_agent_tool_exec.py @@ -17,6 +17,7 @@ FunctionToolManager, _PermissionGuardedTool, ) +from astrbot.core.tools.computer_tools import ExecuteShellTool from astrbot.core.tools.computer_tools.shell import ShellSessionTool from astrbot.core.utils.active_event_registry import ActiveEventRegistry @@ -126,6 +127,171 @@ def test_build_handoff_toolset_keeps_permission_guards_for_default_tools(runtime ) +def _whitelist_run_context(runtime: str, extra_provider_settings: dict | None = None): + provider_settings = {"computer_use_runtime": runtime} + if extra_provider_settings: + provider_settings.update(extra_provider_settings) + event = _DummyEvent() + context = SimpleNamespace( + get_config=lambda **_kwargs: {"provider_settings": provider_settings}, + get_llm_tool_manager=lambda: FunctionToolManager(), + ) + return ContextWrapper(context=SimpleNamespace(event=event, context=context)) + + +def test_build_handoff_toolset_whitelist_keeps_computer_use_tools(monkeypatch): + # The persona whitelist only lists plugin/MCP tools, but the builtin + # computer-use tools must stay available, mirroring the main agent where + # they are applied after the persona whitelist narrowing and never pass + # through it. + plugin_tool = FunctionTool( + name="plugin_tool", + description="plugin tool", + parameters={"type": "object", "properties": {}}, + ) + monkeypatch.setattr( + "astrbot.core.astr_agent_tool_exec.llm_tools.func_list", [plugin_tool] + ) + + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("sandbox"), tools=["plugin_tool"] + ) + + assert toolset is not None + assert toolset.get_tool("plugin_tool") is not None + assert toolset.get_tool("astrbot_execute_ipython") is not None + assert toolset.get_tool("astrbot_execute_shell") is not None + + +def test_build_handoff_toolset_whitelist_warns_and_skips_unknown_names( + monkeypatch, caplog +): + monkeypatch.setattr("astrbot.core.astr_agent_tool_exec.llm_tools.func_list", []) + + with caplog.at_level("WARNING"): + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("none"), tools=["missing_tool"] + ) + + assert toolset is None + assert any("missing_tool" in rec.message for rec in caplog.records) + assert any("empty toolset" in rec.message for rec in caplog.records) + + +def test_build_handoff_toolset_whitelist_warns_on_inactive_tool(monkeypatch, caplog): + inactive_tool = FunctionTool( + name="off_tool", + description="disabled plugin tool", + parameters={"type": "object", "properties": {}}, + ) + inactive_tool.active = False + monkeypatch.setattr( + "astrbot.core.astr_agent_tool_exec.llm_tools.func_list", [inactive_tool] + ) + + with caplog.at_level("WARNING"): + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("none"), tools=["off_tool", "astrbot_grep_tool"] + ) + + # The inactive plugin tool is skipped with a warning, while an explicit + # builtin name still resolves through llm_tools.get_func. + assert toolset is not None + assert toolset.get_tool("off_tool") is None + assert toolset.get_tool("astrbot_grep_tool") is not None + assert any("off_tool" in rec.message for rec in caplog.records) + + +def test_build_handoff_toolset_empty_whitelist_keeps_runtime_tools(caplog): + # An empty whitelist narrows the plugin tools to nothing but keeps the + # runtime computer-use tools, same as an empty persona whitelist on the + # main agent. + with caplog.at_level("WARNING"): + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("sandbox"), tools=[] + ) + + assert toolset is not None + assert toolset.get_tool("astrbot_execute_shell") is not None + assert toolset.get_tool("astrbot_execute_ipython") is not None + assert not any("empty toolset" in rec.message for rec in caplog.records) + + +def test_build_handoff_toolset_empty_whitelist_without_runtime_warns(caplog): + with caplog.at_level("WARNING"): + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("none"), tools=[] + ) + + assert toolset is None + assert any("empty toolset" in rec.message for rec in caplog.records) + + +def test_build_handoff_toolset_whitelist_skips_deactivated_runtime_tools(caplog): + mgr = FunctionToolManager() + mgr.get_builtin_tool(ExecuteShellTool).active = False + + event = _DummyEvent() + context = SimpleNamespace( + get_config=lambda **_kwargs: { + "provider_settings": {"computer_use_runtime": "sandbox"} + }, + get_llm_tool_manager=lambda: mgr, + ) + run_context = ContextWrapper(context=SimpleNamespace(event=event, context=context)) + + with caplog.at_level("WARNING"): + toolset = FunctionToolExecutor._build_handoff_toolset(run_context, tools=[]) + + assert toolset is not None + assert toolset.get_tool("astrbot_execute_shell") is None + assert toolset.get_tool("astrbot_execute_ipython") is not None + assert any( + "deactivated runtime tools" in rec.message + and "astrbot_execute_shell" in rec.message + for rec in caplog.records + ) + + +@pytest.mark.parametrize( + ("booter_state", "expect_browser"), + [ + pytest.param("not_booted", True, id="capabilities_unknown_before_first_boot"), + pytest.param("browser", True, id="profile_supports_browser"), + pytest.param("shell_only", False, id="profile_without_browser"), + ], +) +def test_build_handoff_toolset_seeds_browser_tools_like_main_agent( + monkeypatch, booter_state, expect_browser +): + from astrbot.core.computer.computer_client import session_booter + + umo = _DummyEvent().unified_msg_origin + if booter_state == "not_booted": + monkeypatch.delitem(session_booter, umo, raising=False) + elif booter_state == "browser": + monkeypatch.setitem( + session_booter, umo, SimpleNamespace(capabilities=["browser", "shell"]) + ) + else: + monkeypatch.setitem( + session_booter, umo, SimpleNamespace(capabilities=["shell", "python"]) + ) + + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("sandbox", {"sandbox": {"booter": "shipyard_neo"}}), + tools=[], + ) + + assert toolset is not None + assert toolset.get_tool("astrbot_execute_shell") is not None + assert (toolset.get_tool("astrbot_execute_browser") is not None) == expect_browser + assert ( + toolset.get_tool("astrbot_execute_browser_batch") is not None + ) == expect_browser + assert (toolset.get_tool("astrbot_run_browser_skill") is not None) == expect_browser + + @pytest.mark.asyncio async def test_collect_handoff_image_urls_normalizes_filters_and_appends_event_image( monkeypatch: pytest.MonkeyPatch,