f1a0ab699a
* fix(tools): preserve tool_search promotions across re-entrant get_available_tools Closes #2884. ``get_available_tools`` used to unconditionally call ``reset_deferred_registry()`` and rebuild a fresh ``DeferredToolRegistry`` on every invocation. That works for the first call of a request (the ContextVar starts at its default of ``None``), but any RE-ENTRANT call during the same async context — e.g. ``task_tool`` building a subagent's toolset, or a custom middleware that rebuilds tools mid-run — wiped any ``tool_search`` promotions the parent agent had already made. The ``DeferredToolFilterMiddleware`` would then re-hide those tools from the next model call, leaving the agent able to see a tool's name (via the prior ``tool_search`` result that's still in conversation history) but unable to invoke it. Fix: when the ContextVar already holds a registry, reuse it instead of rebuilding. Fresh requests still get a fresh registry because each new graph run starts in a new asyncio task with the ContextVar at ``None``. ## Verification - Unit-level reproduction (``test_get_available_tools_resets_registry_wiping_promotion``): promote a tool in the registry, call ``get_available_tools`` again, assert the promotion is preserved. Fails on main, passes on this branch. - Graph-execution reproduction (two tests): drive a real ``langchain.agents.create_agent`` graph with the real ``DeferredToolFilterMiddleware`` through two model turns, including one that issues a re-entrant ``get_available_tools`` call to simulate the task_tool subagent path. - Real-LLM end-to-end (``test_deferred_tool_promotion_real_llm.py``, opt-in via ``ONEAPI_E2E=1``): drives the same flow against a real OpenAI-compatible model (verified on GPT-5.4-mini through the one-api gateway), watches the model call the promoted ``fake_calculator`` through the deferred-filter middleware, and asserts the right arithmetic result. Passes against the fixed branch. - Companion update to ``test_tool_deduplication.py``: dropped the ``@patch("deerflow.tools.tools.reset_deferred_registry")`` decorators because the symbol is no longer imported there. - Test fixtures in the new files patch ``deerflow.tools.tools.get_app_config`` with a minimal ``model_construct``-ed ``AppConfig`` instead of calling the real loader, so they never trigger ``_apply_singleton_configs`` and never leak ``_memory_config``/``_title_config``/… mutations into the rest of the suite. Full backend suite: 3208 passed / 14 skipped / 0 failed. ruff check + format clean. * fix(tools): address Copilot review on #2885 - tools.py: rewrite the reuse-path comment to spell out (a) why we don't reconcile the registry against the current ``mcp_tools`` snapshot — the MCP cache doesn't refresh mid-graph-run, the lead agent's ``ToolNode`` is already bound to the previous tool set anyway, and ``promote()`` drops the entry so a naive re-sync misclassifies promotions as new tools — and (b) why the log uses ``max(0, …)`` to avoid negative counts when the cache shrinks between snapshots. - Replace direct ``ts_mod._registry_var.set(None)`` in test fixtures with the public ``reset_deferred_registry()`` helper so tests don't couple to module internals. - Correct the docstring path in ``test_deferred_tool_registry_promotion.py`` to match the actual monkeypatch target (``deerflow.mcp.cache.get_cached_mcp_tools``). - Rename ``test_get_available_tools_resets_registry_wiping_promotion`` to ``test_get_available_tools_preserves_promotions_across_reentrant_calls`` so the test name describes the contract being asserted, not the bug it originally reproduced. Full backend suite: 3208 passed / 14 skipped. Real-LLM e2e: 1 passed.
221 lines
10 KiB
Python
221 lines
10 KiB
Python
import logging
|
|
|
|
from langchain.tools import BaseTool
|
|
|
|
from deerflow.config import get_app_config
|
|
from deerflow.config.app_config import AppConfig
|
|
from deerflow.reflection import resolve_variable
|
|
from deerflow.sandbox.security import is_host_bash_allowed
|
|
from deerflow.tools.builtins import ask_clarification_tool, present_file_tool, task_tool, view_image_tool
|
|
from deerflow.tools.builtins.tool_search import get_deferred_registry
|
|
from deerflow.tools.sync import make_sync_tool_wrapper
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
BUILTIN_TOOLS = [
|
|
present_file_tool,
|
|
ask_clarification_tool,
|
|
]
|
|
|
|
SUBAGENT_TOOLS = [
|
|
task_tool,
|
|
# task_status_tool is no longer exposed to LLM (backend handles polling internally)
|
|
]
|
|
|
|
|
|
def _is_host_bash_tool(tool: object) -> bool:
|
|
"""Return True if the tool config represents a host-bash execution surface."""
|
|
group = getattr(tool, "group", None)
|
|
use = getattr(tool, "use", None)
|
|
if group == "bash":
|
|
return True
|
|
if use == "deerflow.sandbox.tools:bash_tool":
|
|
return True
|
|
return False
|
|
|
|
|
|
def _ensure_sync_invocable_tool(tool: BaseTool) -> BaseTool:
|
|
"""Attach a sync wrapper to async-only tools used by sync agent callers."""
|
|
if getattr(tool, "func", None) is None and getattr(tool, "coroutine", None) is not None:
|
|
tool.func = make_sync_tool_wrapper(tool.coroutine, tool.name)
|
|
return tool
|
|
|
|
|
|
def get_available_tools(
|
|
groups: list[str] | None = None,
|
|
include_mcp: bool = True,
|
|
model_name: str | None = None,
|
|
subagent_enabled: bool = False,
|
|
*,
|
|
app_config: AppConfig | None = None,
|
|
) -> list[BaseTool]:
|
|
"""Get all available tools from config.
|
|
|
|
Note: MCP tools should be initialized at application startup using
|
|
`initialize_mcp_tools()` from deerflow.mcp module.
|
|
|
|
Args:
|
|
groups: Optional list of tool groups to filter by.
|
|
include_mcp: Whether to include tools from MCP servers (default: True).
|
|
model_name: Optional model name to determine if vision tools should be included.
|
|
subagent_enabled: Whether to include subagent tools (task, task_status).
|
|
|
|
Returns:
|
|
List of available tools.
|
|
"""
|
|
config = app_config or get_app_config()
|
|
tool_configs = [tool for tool in config.tools if groups is None or tool.group in groups]
|
|
|
|
# Do not expose host bash by default when LocalSandboxProvider is active.
|
|
if not is_host_bash_allowed(config):
|
|
tool_configs = [tool for tool in tool_configs if not _is_host_bash_tool(tool)]
|
|
|
|
loaded_tools_raw = [(cfg, resolve_variable(cfg.use, BaseTool)) for cfg in tool_configs]
|
|
|
|
# Warn when the config ``name`` field and the tool object's ``.name``
|
|
# attribute diverge — this mismatch is the root cause of issue #1803 where
|
|
# the LLM receives one name in its tool schema but the runtime router
|
|
# recognises a different name, producing "not a valid tool" errors.
|
|
for cfg, loaded in loaded_tools_raw:
|
|
if cfg.name != loaded.name:
|
|
logger.warning(
|
|
"Tool name mismatch: config name %r does not match tool .name %r (use: %s). The tool's own .name will be used for binding.",
|
|
cfg.name,
|
|
loaded.name,
|
|
cfg.use,
|
|
)
|
|
|
|
loaded_tools = [_ensure_sync_invocable_tool(t) for _, t in loaded_tools_raw]
|
|
|
|
# Conditionally add tools based on config
|
|
builtin_tools = BUILTIN_TOOLS.copy()
|
|
skill_evolution_config = getattr(config, "skill_evolution", None)
|
|
if getattr(skill_evolution_config, "enabled", False):
|
|
from deerflow.tools.skill_manage_tool import skill_manage_tool
|
|
|
|
builtin_tools.append(skill_manage_tool)
|
|
|
|
# Add subagent tools only if enabled via runtime parameter
|
|
if subagent_enabled:
|
|
builtin_tools.extend(SUBAGENT_TOOLS)
|
|
logger.info("Including subagent tools (task)")
|
|
|
|
# If no model_name specified, use the first model (default)
|
|
if model_name is None and config.models:
|
|
model_name = config.models[0].name
|
|
|
|
# Add view_image_tool only if the model supports vision
|
|
model_config = config.get_model_config(model_name) if model_name else None
|
|
if model_config is not None and model_config.supports_vision:
|
|
builtin_tools.append(view_image_tool)
|
|
logger.info(f"Including view_image_tool for model '{model_name}' (supports_vision=True)")
|
|
|
|
# Get cached MCP tools if enabled
|
|
# NOTE: We use ExtensionsConfig.from_file() instead of config.extensions
|
|
# to always read the latest configuration from disk. This ensures that changes
|
|
# made through the Gateway API (which runs in a separate process) are immediately
|
|
# reflected when loading MCP tools.
|
|
mcp_tools = []
|
|
if include_mcp:
|
|
try:
|
|
from deerflow.config.extensions_config import ExtensionsConfig
|
|
from deerflow.mcp.cache import get_cached_mcp_tools
|
|
|
|
extensions_config = ExtensionsConfig.from_file()
|
|
if extensions_config.get_enabled_mcp_servers():
|
|
mcp_tools = get_cached_mcp_tools()
|
|
if mcp_tools:
|
|
logger.info(f"Using {len(mcp_tools)} cached MCP tool(s)")
|
|
|
|
# When tool_search is enabled, register MCP tools in the
|
|
# deferred registry and add tool_search to builtin tools.
|
|
if config.tool_search.enabled:
|
|
from deerflow.tools.builtins.tool_search import DeferredToolRegistry, set_deferred_registry
|
|
from deerflow.tools.builtins.tool_search import tool_search as tool_search_tool
|
|
|
|
# Reuse the existing registry if one is already set for
|
|
# this async context. ``get_available_tools`` is
|
|
# re-entered whenever a subagent is spawned
|
|
# (``task_tool`` calls it to build the child agent's
|
|
# toolset), and previously we used to unconditionally
|
|
# rebuild the registry — wiping out the parent agent's
|
|
# tool_search promotions. The
|
|
# ``DeferredToolFilterMiddleware`` then re-hid those
|
|
# tools from subsequent model calls, leaving the agent
|
|
# able to see a tool's name but unable to invoke it
|
|
# (issue #2884). ``contextvars`` already gives us the
|
|
# lifetime semantics we want: a fresh request / graph
|
|
# run starts in a new asyncio task with the
|
|
# ContextVar at its default of ``None``, so reuse is
|
|
# only triggered for re-entrant calls inside one run.
|
|
#
|
|
# Intentionally NOT reconciling against the current
|
|
# ``mcp_tools`` snapshot. The MCP cache only refreshes
|
|
# on ``extensions_config.json`` mtime changes, which
|
|
# in practice happens between graph runs — not inside
|
|
# one. And even if a refresh did happen mid-run, the
|
|
# already-built lead agent's ``ToolNode`` still holds
|
|
# the *previous* tool set (LangGraph binds tools at
|
|
# graph construction time), so a brand-new MCP tool
|
|
# couldn't actually be invoked anyway. The
|
|
# ``DeferredToolRegistry`` doesn't retain the names
|
|
# of previously-promoted tools (``promote()`` drops
|
|
# the entry entirely), so re-syncing the registry
|
|
# against a fresh ``mcp_tools`` list would
|
|
# mis-classify those promotions as new tools and
|
|
# re-register them as deferred — exactly the bug
|
|
# this fix exists to prevent.
|
|
existing_registry = get_deferred_registry()
|
|
if existing_registry is None:
|
|
registry = DeferredToolRegistry()
|
|
for t in mcp_tools:
|
|
registry.register(t)
|
|
set_deferred_registry(registry)
|
|
logger.info(f"Tool search active: {len(mcp_tools)} tools deferred")
|
|
else:
|
|
mcp_tool_names = {t.name for t in mcp_tools}
|
|
still_deferred = len(existing_registry)
|
|
promoted_count = max(0, len(mcp_tool_names) - still_deferred)
|
|
logger.info(f"Tool search active (preserved promotions): {still_deferred} tools deferred, {promoted_count} already promoted")
|
|
builtin_tools.append(tool_search_tool)
|
|
except ImportError:
|
|
logger.warning("MCP module not available. Install 'langchain-mcp-adapters' package to enable MCP tools.")
|
|
except Exception as e:
|
|
logger.error(f"Failed to get cached MCP tools: {e}")
|
|
|
|
# Add invoke_acp_agent tool if any ACP agents are configured
|
|
acp_tools: list[BaseTool] = []
|
|
try:
|
|
from deerflow.tools.builtins.invoke_acp_agent_tool import build_invoke_acp_agent_tool
|
|
|
|
if app_config is None:
|
|
from deerflow.config.acp_config import get_acp_agents
|
|
|
|
acp_agents = get_acp_agents()
|
|
else:
|
|
acp_agents = getattr(config, "acp_agents", {}) or {}
|
|
if acp_agents:
|
|
acp_tools.append(build_invoke_acp_agent_tool(acp_agents))
|
|
logger.info(f"Including invoke_acp_agent tool ({len(acp_agents)} agent(s): {list(acp_agents.keys())})")
|
|
except Exception as e:
|
|
logger.warning(f"Failed to load ACP tool: {e}")
|
|
|
|
logger.info(f"Total tools loaded: {len(loaded_tools)}, built-in tools: {len(builtin_tools)}, MCP tools: {len(mcp_tools)}, ACP tools: {len(acp_tools)}")
|
|
|
|
# Deduplicate by tool name — config-loaded tools take priority, followed by
|
|
# built-ins, MCP tools, and ACP tools. Duplicate names cause the LLM to
|
|
# receive ambiguous or concatenated function schemas (issue #1803).
|
|
all_tools = loaded_tools + builtin_tools + mcp_tools + acp_tools
|
|
seen_names: set[str] = set()
|
|
unique_tools: list[BaseTool] = []
|
|
for t in all_tools:
|
|
if t.name not in seen_names:
|
|
unique_tools.append(t)
|
|
seen_names.add(t.name)
|
|
else:
|
|
logger.warning(
|
|
"Duplicate tool name %r detected and skipped — check your config.yaml and MCP server registrations (issue #1803).",
|
|
t.name,
|
|
)
|
|
return unique_tools
|