mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-06-09 17:12:01 +00:00
Follow-up to #3342 (deferred MCP tool loading). Maintainability cleanup plus hardening of malformed/empty tool_search queries; no change to the deferral mechanism or search ranking. - Add deerflow/tools/mcp_metadata.py as the single source of truth for the "deerflow_mcp" tag (MCP_TOOL_METADATA_KEY + tag_mcp_tool + public is_mcp_tool). Removes the duplicated magic string and the private, cross-module _is_mcp_tool import. - tool_search.search: never raise on model-generated input. Extract _compile_catalog_regex (shared compile-with-literal-fallback); return empty for empty/whitespace queries and a bare "+" instead of matching everything or raising IndexError. - DeferredToolSetup: document the empty-vs-populated invariant. - build_deferred_tool_setup: comment the two distinct empty-return branches. - _assemble_deferred: add return type, rename local to deferred_setup, build the final list with an explicit append. - Tests: use tag_mcp_tool instead of per-file tag helpers; cover empty and bare-"+" queries.
84 lines
2.6 KiB
Python
84 lines
2.6 KiB
Python
import pytest
|
|
from langchain_core.tools import tool as as_tool
|
|
|
|
from deerflow.tools.builtins.tool_search import DeferredToolCatalog
|
|
|
|
|
|
@as_tool
|
|
def alpha_search(query: str) -> str:
|
|
"Search alpha records by query."
|
|
return query
|
|
|
|
|
|
@as_tool
|
|
def beta_translate(text: str) -> str:
|
|
"Translate beta text."
|
|
return text
|
|
|
|
|
|
@pytest.fixture
|
|
def catalog() -> DeferredToolCatalog:
|
|
return DeferredToolCatalog((alpha_search, beta_translate))
|
|
|
|
|
|
def test_names(catalog):
|
|
assert catalog.names == frozenset({"alpha_search", "beta_translate"})
|
|
|
|
|
|
def test_search_select(catalog):
|
|
got = catalog.search("select:alpha_search")
|
|
assert [t.name for t in got] == ["alpha_search"]
|
|
|
|
|
|
def test_search_plus_keyword(catalog):
|
|
got = catalog.search("+beta translate")
|
|
assert [t.name for t in got] == ["beta_translate"]
|
|
|
|
|
|
def test_search_regex_on_description(catalog):
|
|
got = catalog.search("translate")
|
|
assert "beta_translate" in [t.name for t in got]
|
|
|
|
|
|
def test_search_invalid_regex_falls_back_to_literal():
|
|
@as_tool
|
|
def calc(expr: str) -> str:
|
|
"Compute sum(a, b) style expressions."
|
|
return expr
|
|
|
|
cat = DeferredToolCatalog((calc, alpha_search))
|
|
# "sum(" is an invalid regex (unbalanced paren). search() must not raise; it
|
|
# falls back to a literal match, which finds calc's "sum(" in its description.
|
|
assert [t.name for t in cat.search("sum(")] == ["calc"]
|
|
# A literal with no match is deterministically empty (and still must not raise).
|
|
assert cat.search("zzz(") == []
|
|
|
|
|
|
def test_search_empty_query_returns_empty(catalog):
|
|
# An empty / whitespace-only query is meaningless; rather than let the empty
|
|
# regex match every tool, search() returns nothing so the model gets a clear
|
|
# "no match" signal and re-queries instead of acting on noise.
|
|
assert catalog.search("") == []
|
|
assert catalog.search(" ") == []
|
|
|
|
|
|
def test_search_bare_plus_returns_empty(catalog):
|
|
# A "+" prefix with no required token is malformed model input. It must
|
|
# return no matches, not raise IndexError on parts[0]. " + " strips to "+",
|
|
# so it routes here too and must be handled the same way.
|
|
assert catalog.search("+") == []
|
|
assert catalog.search(" + ") == []
|
|
assert catalog.search("+ ") == []
|
|
|
|
|
|
def test_hash_stable_across_instances():
|
|
c1 = DeferredToolCatalog((alpha_search, beta_translate))
|
|
c2 = DeferredToolCatalog((beta_translate, alpha_search))
|
|
assert c1.hash == c2.hash
|
|
|
|
|
|
def test_hash_changes_with_membership():
|
|
c1 = DeferredToolCatalog((alpha_search, beta_translate))
|
|
c2 = DeferredToolCatalog((alpha_search,))
|
|
assert c1.hash != c2.hash
|