mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-05-19 16:12:30 +00:00
This commit implements a progressive refactoring of the tool system to support plugin discovery, scoped loading, and protocol-driven runtime context injection. Key changes: - Add Tool ABC metadata (tool_name, _scopes) and ToolContext dataclass for dependency injection. - Introduce ToolLoader with pkgutil-based builtin discovery and entry_points-based third-party plugin loading. - Add scope filtering (core/subagent/memory) so different contexts load appropriate tool sets. - Introduce ContextAware protocol and RequestContext dataclass to replace hardcoded per-tool context injection in AgentLoop. - Add RuntimeState / MutableRuntimeState protocols to decouple MyTool from AgentLoop. - Migrate all built-in tools to declare scopes and implement create()/enabled() hooks. - Migrate MessageTool, SpawnTool, CronTool, and MyTool to ContextAware. - Refactor AgentLoop to use ToolLoader and protocol-driven context injection. - Refactor SubagentManager to use ToolLoader(scope="subagent") with per-run FileStates isolation. - Register all built-in tools via pyproject.toml entry_points. - Add comprehensive tests for loader scopes, entry_points, ContextAware, subagent tools, and runtime state sync.
30 lines
849 B
Python
30 lines
849 B
Python
"""Focused tests for MyTool runtime sync side effects."""
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from nanobot.agent.tools.self import MyTool
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_my_tool_max_iterations_syncs_subagent_limit() -> None:
|
|
loop = MagicMock()
|
|
loop.max_iterations = 40
|
|
loop._runtime_vars = {}
|
|
loop.subagents = MagicMock()
|
|
loop.subagents.max_iterations = loop.max_iterations
|
|
|
|
def _sync_subagent_runtime_limits() -> None:
|
|
loop.subagents.max_iterations = loop.max_iterations
|
|
|
|
loop._sync_subagent_runtime_limits = _sync_subagent_runtime_limits
|
|
|
|
tool = MyTool(runtime_state=loop)
|
|
|
|
result = await tool.execute(action="set", key="max_iterations", value=80)
|
|
|
|
assert "Set max_iterations = 80" in result
|
|
assert loop.max_iterations == 80
|
|
assert loop.subagents.max_iterations == 80
|