mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-07-29 01:15:59 +00:00
* feat(uploads): lazy-load historical files via list_uploaded_files tool
Replace per-turn injection of all historical upload metadata with on-demand
discovery via a new `list_uploaded_files` built-in tool, following the same
deferred-discovery pattern used by skills.
- Rename <uploaded_files> block to <current_uploads> (current-run files only)
- Add list_uploaded_files tool with include_outline: bool|list[str]
- Extract outline helpers to shared deerflow/utils/file_outline.py
- Update system prompt to reflect lazy-loading behaviour
- Historical file scan removed from UploadsMiddleware.before_agent()
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(uploads): clear uploaded_files state when no new files in current turn
When before_agent() returns None on empty turns, the LastValue
uploaded_files field retains the previous turn's filenames.
list_uploaded_files then incorrectly excludes those files as
"current-run" files, making them invisible until the next upload.
Fix: return {"uploaded_files": []} instead of None to explicitly
clear state. Add two-turn regression test covering the exact
scenario from review feedback.
Co-Authored-By: Claude <noreply@anthropic.com>
* fix: resolve CI lint errors and stale test assertion from merge
- Split long prompt line to fit 240-char limit
- Add missing `Any` import in list_uploaded_files_tool
- Remove unused `re` import in file_conversion (outline code moved)
- Remove unused `os` import in middleware test
- Fix test assertion: <uploaded_files> → <current_uploads> after main merge
Co-Authored-By: Claude <noreply@anthropic.com>
* fix: resolve CI lint errors and stale test assertion from merge
- Split long prompt line to fit 240-char limit
- Add missing `Any` import in list_uploaded_files_tool
- Remove unused `re` import in file_conversion (outline code moved)
- Remove unused `os` import in middleware test
- Fix test assertion: <uploaded_files> → <current_uploads> after main merge
Co-Authored-By: Claude <noreply@anthropic.com>
* fix: add current_uploads to input sanitization exempt tags
The lazy-loading PR renamed <uploaded_files> to <current_uploads>.
The anti-drift guard scans all framework XML blocks and requires each
to be either blocked or explicitly exempted. current_uploads wraps
trusted server-generated file metadata, not user input, so it belongs
in the exempt set.
Co-Authored-By: Claude <noreply@anthropic.com>
* test: regenerate replay golden after uploaded_files state change
before_agent now returns {"uploaded_files": []} instead of None,
adding uploaded_files to SSE values events. Regenerated via
DEERFLOW_WRITE_GOLDEN=1.
Co-Authored-By: Claude <noreply@anthropic.com>
* fix: review feedback — memory pipeline, stale tags, state clearing, nits
- Match both tags in memory stripping pipeline (uploaded_files|current_uploads)
- Remove stale uploaded_files from _BLOCKED_TAG_NAMES
- Clear uploaded_files on all before_agent early-return paths
- Fix ponytail: stray word in file_conversion re-export comment
- Remove dead total_omitted branch in _format_omitted_summary
- ruff format fixes
Co-Authored-By: Claude <noreply@anthropic.com>
* fix: block current_uploads, sanitize only original user content
Per review feedback: instead of exempting <current_uploads> (which
allows user forgery), move it to _BLOCKED_TAG_NAMES and change
InputSanitizationMiddleware._process_request to scan only the
original user content (ORIGINAL_USER_CONTENT_KEY) when available.
Server-injected trusted blocks are no longer checked against the
blocked-tag denylist.
Co-Authored-By: Claude <noreply@anthropic.com>
* docs: clarify fallback reason in input sanitization comment
Co-Authored-By: Claude <noreply@anthropic.com>
* @
fix: third-round review feedback — state visibility, sanitization, regex, nits
- list_uploaded_files_tool: logger.warning instead of silent try/except
on runtime.state read failure (High)
- input_sanitization_middleware: _extract_text_from_content skips empty
text blocks to match message_content_to_text behaviour; rfind fallback
path logs warning for observability (Medium)
- memory pipeline regexes: backreference (?P<tag>)(?P=tag) in
message_processing.py and prompt.py (Low)
- file_conversion.py: re-export moved to top of file (Low)
- Tests: middleware→tool state bridge test; integrated forged-tag +
multimodal sanitization tests
PR #4174 — Follow-up issues: #4212, #4213, #4214
Co-Authored-By: Claude <noreply@anthropic.com>
@
* @
fix: 4th-round review — denylist, sanitization, scandir, nits
- Add "uploaded_files" back to _BLOCKED_TAG_NAMES (old tag still processed by
deermem; user forgery must be escaped) (consistency)
- Fix inaccurate rfind-fallback comment: UploadsMiddleware keeps string as
string, fallback is unreachable for strings (doc fix)
- Distinguish "empty string key" (upload without text) from "non-string key"
(caller forgery) so empty-text uploads never escape the server block (edge)
- Merge dual os.scandir(uploads_dir) calls into one list re-use (minor)
- Add comment on .md sibling skip known limitation: user-uploaded .md files
whose stem collides with a converted doc are hidden (boundary, no code change)
Co-Authored-By: Claude <noreply@anthropic.com>
@
* @
fix: tighten rfind-failure fallback — distinguish server blocks from user blocks
When _extract_text_from_content and message_content_to_text disagree on
multimodal list content and rfind fails, use content[0] (server-injected
<current_uploads> block) vs content[1:] (user blocks) to sanitize only
user blocks. Raw strings and non-standard dict blocks that
_extract_text_from_content misses are now also sanitized.
Non-distinguishable paths (< 2 text blocks, non-list content) still
degrade to full sanitization (safe — server block may be escaped but
user forgery never leaks). All fallback paths log via logger.warning.
Decision 18 / willem-bd 4th-round comment #3
Co-Authored-By: Claude <noreply@anthropic.com>
@
* @
fix: correct comments referencing text_blocks → content in rfind fallback
Co-Authored-By: Claude <noreply@anthropic.com>
@
* fix: 5th-round review — dead code, subagent gating, integration test, perf, consistency
- Delete unreachable ORIGINAL_USER_CONTENT_KEY guard in rfind fallback
branch (original_user_content guaranteed non-empty str at that point)
- Remove list_uploaded_files from BUILTIN_TOOLS; add include_upload_tool
param to get_available_tools(), default True; task_tool.py passes False
so subagents no longer receive a tool whose state exclusion is broken
- Add integration test exercising real create_agent graph (not mocked
runtime.state) to verify LangGraph propagates before_agent state writes
into ToolRuntime.state during same-turn tool calls
- Cache DirEntry.stat() st_size in candidates tuple to avoid second
per-file syscall in the rendering loop
- Make the upload-tag pre-check case-insensitive (content_str.lower())
to match _UPLOAD_BLOCK_RE re.IGNORECASE
PR #4174 — willem-bd 5th-round review items #1-#5
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(channels): pass files metadata through _human_input_message() for IM uploads
_human_input_message() was not passing additional_kwargs.files to the
downstream message. UploadsMiddleware read no files, wrote
uploaded_files=[], and list_uploaded_files reported same-run IM
attachments as historical files (fancyboi999 repro).
Fix: add files parameter to _human_input_message(), call site passes
files=uploaded. Regression test locks the contract.
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(channels): remove legacy <uploaded_files> manual prepend to fix double-injection regression
Commit 8d86dbf6 added files= pass-through to UploadsMiddleware but
left the manual _format_uploaded_files_block() prepend in place.
Every IM attachment reached the model twice — once via the legacy
<uploaded_files> block and once via <current_uploads>.
This commit removes the manual prepend and the now-dead
_format_uploaded_files_block() function. UploadsMiddleware is the
sole upload-context producer for both IM and web paths.
Reported-by: fancyboi999 (PR review)
Co-Authored-By: Claude <noreply@anthropic.com>
* docs: update #4212 issue body to reflect completed fixes and narrowed remaining scope
* chore: remove temporary scratch file
* fix(middleware): neutralize user-derived values inside <current_uploads> block
Upload-derived filenames, paths, outline titles, and preview text are
interpolated verbatim inside the trusted <current_uploads> wrapper,
which InputSanitizationMiddleware exempts from sanitization. A crafted
filename or document heading containing blocked authority tags would
bypass the guardrail and enter model context as trusted framework data.
Fix: call neutralize_untrusted_tags() on all four user-derived values
inside _format_file_entry(), preserving the outer <current_uploads>
wrapper untouched.
Reported-by: fancyboi999 (P1 security review)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(middleware): neutralize extension labels in omitted-file summary
Files exceeding the 10-item context cap bypass _format_file_entry().
Their extensions, derived from user-controlled filenames via
_extension_label(), were interpolated verbatim into the trusted
<current_uploads> wrapper — another path for blocked authority tags
to escape the guardrail.
Fix: neutralize extension values inside _extension_label(), the
single extraction point for all extension labels.
Reported-by: fancyboi999 (P1 security review)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(tools): neutralize user-derived values in list_uploaded_files tool result
Apply neutralize_untrusted_tags() to every model-visible user-derived value
returned by list_uploaded_files: filename, virtual path, extension, outline
titles, outline preview lines, and omitted-file extension summary.
This closes the last remaining injection bypass in the upload lazy-loading
path - the <current_uploads> block and its omitted summary were already
neutralized (previous commits), but the list_uploaded_files tool produced
a second exit for the same attacker-controlled metadata that
ToolResultSanitizationMiddleware did not cover.
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(tests): add missing include_upload_tool=False to task_tool mock assertions
PR #4174 added include_upload_tool parameter to get_available_tools().
task_tool.py correctly passes include_upload_tool=False for subagents
but 5 existing tests' assert_called_once_with expectations were not
updated, causing CI failures.
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>
184 lines
7.7 KiB
Python
184 lines
7.7 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, list_uploaded_files, present_file_tool, review_skill_package, task_tool, view_image_tool
|
|
from deerflow.tools.mcp_metadata import tag_mcp_tool
|
|
from deerflow.tools.sync import make_sync_tool_wrapper
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
BUILTIN_TOOLS = [
|
|
present_file_tool,
|
|
ask_clarification_tool,
|
|
review_skill_package,
|
|
]
|
|
|
|
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,
|
|
*,
|
|
include_upload_tool: bool = True,
|
|
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).
|
|
include_upload_tool: Whether to include ``list_uploaded_files`` (default: True).
|
|
Set to False for subagent tool assembly — subagents have independent
|
|
ThreadState and cannot exclude current-run files.
|
|
|
|
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()
|
|
if include_upload_tool:
|
|
builtin_tools.append(list_uploaded_files)
|
|
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)")
|
|
|
|
# Tag MCP-sourced tools so deferred-tool assembly at each
|
|
# agent construction site can identify them. Lead agents
|
|
# assemble their full configured MCP catalog and apply active
|
|
# skill policy at runtime; subagents may pass an already
|
|
# policy-filtered list because their skills load at startup.
|
|
for t in mcp_tools:
|
|
tag_mcp_tool(t)
|
|
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 = [_ensure_sync_invocable_tool(t) for t in 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
|