sync update from LAN repo (source ji @ 85078a31)

This commit is contained in:
gozeros committed 2026-10-21 15:09:16 +08:00
1 parent 5d6627af50
commit e602d88695
864 files changed
+99150 -6346

No files matched your search

@@ -98,35 +98,69 @@ def _get_skills_container_path() -> str:
return _DEFAULT_SKILLS_CONTAINER_PATH
def _get_skills_host_path() -> str | None:
"""Get the skills host filesystem path from config.
Returns None if the skills directory does not exist or config cannot be
loaded. Only successful lookups are cached; failures are retried on the
next call so that a transiently unavailable skills directory does not
permanently disable skills access.
"""
cached = getattr(_get_skills_host_path, "_cached", None)
if cached is not None:
return cached
try:
from deerflow.config import get_app_config
config = get_app_config()
skills_path = config.skills.get_skills_path()
if skills_path.exists():
value = str(skills_path)
_get_skills_host_path._cached = value # type: ignore[attr-defined]
return value
except Exception:
pass
return None
def _get_skills_host_path() -> str | None:
"""Get the refreshed, online-only model-facing skills path.
Never return the canonical administrative skills tree. Offline skills
intentionally remain there and would otherwise be reachable from bash by
constructing a path dynamically instead of spelling it in the tool call.
"""
try:
from deerflow.sandbox.skill_view import refresh_skill_sandbox_view
return str(refresh_skill_sandbox_view().root)
except Exception:
pass
return None
def _refresh_skill_capability() -> None:
"""Refresh the filesystem capability used by local and AIO sandboxes."""
from deerflow.sandbox.skill_view import refresh_skill_sandbox_view
refresh_skill_sandbox_view()
def _is_skills_path(path: str) -> bool:
"""Check if a path is under the skills container path."""
skills_prefix = _get_skills_container_path()
return path == skills_prefix or path.startswith(f"{skills_prefix}/")
def _require_enabled_skill_path(path: str) -> None:
"""Fail closed unless ``path`` belongs to a currently enabled skill."""
from deerflow.tools.builtins.skill_tools import require_enabled_skill_path
require_enabled_skill_path(path, container_path=_get_skills_container_path())
def _validate_enabled_skill_paths_in_command(command: str) -> None:
"""Give a clear error for explicit unavailable skill paths.
This is a usability policy, not the security boundary. Shell expansion
can hide paths from string inspection, so the enforceable boundary is the
online-only filesystem view refreshed before every bash invocation.
"""
skills_container = _get_skills_container_path()
boundary = re.compile(rf"{re.escape(skills_container)}(?=$|[/\s\"'`;&|<>()])")
if boundary.search(command) is None:
return
matched = False
url_spans = _non_file_url_spans(command)
for match in _ABSOLUTE_PATH_PATTERN.finditer(command):
if _is_in_spans(match.start(), url_spans):
continue
path = match.group()
if not _is_skills_path(path):
continue
matched = True
_require_enabled_skill_path(path)
# Quoting/concatenation that hides the concrete skill directory is not a
# valid way around the gate. Exact, discoverable paths are required.
if not matched:
raise PermissionError("Skill path is unavailable")
def _resolve_skills_path(path: str) -> str:
@@ -649,6 +683,7 @@ def validate_local_tool_path(path: str, thread_data: ThreadDataState | None, *,
# Skills paths — read-only access only
if _is_skills_path(path):
_require_enabled_skill_path(path)
if not read_only:
raise PermissionError(f"Write access to skills path is not allowed: {path}")
return
@@ -1268,7 +1303,7 @@ def _truncate_ls_output(output: str, max_chars: int) -> str:
@tool("bash", parse_docstring=True)
def bash_tool(runtime: ToolRuntime[ContextT, ThreadState], description: str, command: str) -> str:
def bash_tool(runtime: ToolRuntime[ContextT, ThreadState], description: str, command: str) -> str:
"""Execute a bash command in a Linux environment.
@@ -1279,8 +1314,10 @@ def bash_tool(runtime: ToolRuntime[ContextT, ThreadState], description: str, com
Args:
description: Explain why you are running this command in short words. ALWAYS PROVIDE THIS PARAMETER FIRST.
command: The bash command to execute. Always use absolute paths for files and directories.
"""
try:
"""
try:
_refresh_skill_capability()
_validate_enabled_skill_paths_in_command(command)
sandbox = ensure_sandbox_initialized(runtime)
if is_local_sandbox(runtime):
if not is_host_bash_allowed():
@@ -1317,17 +1354,20 @@ def bash_tool(runtime: ToolRuntime[ContextT, ThreadState], description: str, com
@tool("ls", parse_docstring=True)
def ls_tool(runtime: ToolRuntime[ContextT, ThreadState], description: str, path: str) -> str:
def ls_tool(runtime: ToolRuntime[ContextT, ThreadState], description: str, path: str) -> str:
"""List the contents of a directory up to 2 levels deep in tree format.
Args:
description: Explain why you are listing this directory in short words. ALWAYS PROVIDE THIS PARAMETER FIRST.
path: The **absolute** path to the directory to list.
"""
try:
requested_path = path
try:
if _is_skills_path(path):
_refresh_skill_capability()
_require_enabled_skill_path(path)
sandbox = ensure_sandbox_initialized(runtime)
ensure_thread_directories_exist(runtime)
requested_path = path
thread_data = None
if is_local_sandbox(runtime):
thread_data = get_thread_data(runtime)
@@ -1364,7 +1404,7 @@ def ls_tool(runtime: ToolRuntime[ContextT, ThreadState], description: str, path:
@tool("glob", parse_docstring=True)
def glob_tool(
def glob_tool(
runtime: ToolRuntime[ContextT, ThreadState],
description: str,
pattern: str,
@@ -1381,10 +1421,13 @@ def glob_tool(
include_dirs: Whether matching directories should also be returned. Default is False.
max_results: Maximum number of paths to return. Default is 200.
"""
try:
requested_path = path
try:
if _is_skills_path(path):
_refresh_skill_capability()
_require_enabled_skill_path(path)
sandbox = ensure_sandbox_initialized(runtime)
ensure_thread_directories_exist(runtime)
requested_path = path
effective_max_results = _resolve_max_results(
"glob",
max_results,
@@ -1414,7 +1457,7 @@ def glob_tool(
@tool("grep", parse_docstring=True)
def grep_tool(
def grep_tool(
runtime: ToolRuntime[ContextT, ThreadState],
description: str,
pattern: str,
@@ -1435,10 +1478,13 @@ def grep_tool(
case_sensitive: Whether matching is case-sensitive. Default is False.
max_results: Maximum number of matching lines to return. Default is 100.
"""
try:
requested_path = path
try:
if _is_skills_path(path):
_refresh_skill_capability()
_require_enabled_skill_path(path)
sandbox = ensure_sandbox_initialized(runtime)
ensure_thread_directories_exist(runtime)
requested_path = path
effective_max_results = _resolve_max_results(
"grep",
max_results,
@@ -1484,7 +1530,7 @@ def grep_tool(
@tool("read_file", parse_docstring=True)
def read_file_tool(
def read_file_tool(
runtime: ToolRuntime[ContextT, ThreadState],
description: str,
path: str,
@@ -1498,18 +1544,30 @@ def read_file_tool(
path: The **absolute** path to the file to read.
start_line: Optional starting line number (1-indexed, inclusive). Use with end_line to read a specific range.
end_line: Optional ending line number (1-indexed, inclusive). Use with start_line to read a specific range.
"""
try:
requested_path = path
if _is_skills_path(requested_path):
from deerflow.tools.builtins.skill_tools import (
"""
try:
requested_path = path
if start_line is not None and (not isinstance(start_line, int) or isinstance(start_line, bool) or start_line < 1):
return "Error: start_line must be a positive integer"
if end_line is not None and (not isinstance(end_line, int) or isinstance(end_line, bool) or end_line < 1):
return "Error: end_line must be a positive integer"
effective_start = start_line or 1
if end_line is not None and end_line < effective_start:
return "Error: end_line must be greater than or equal to start_line"
if _is_skills_path(requested_path):
_refresh_skill_capability()
from deerflow.tools.builtins.skill_tools import (
_resolve_active_agent_id,
can_access_coordinator_only_skill,
is_coordinator_only_skill_path,
)
if is_coordinator_only_skill_path(requested_path) and _resolve_active_agent_id(runtime) != "roundtable-coordinator":
if is_coordinator_only_skill_path(requested_path) and not can_access_coordinator_only_skill(
_resolve_active_agent_id(runtime), "agent_orchestration"
):
# Match skill_view's non-leaking behavior: ordinary agents must not discover whether it exists.
return f"Error: File not found: {requested_path}"
_require_enabled_skill_path(requested_path)
sandbox = ensure_sandbox_initialized(runtime)
ensure_thread_directories_exist(runtime)
if is_local_sandbox(runtime):
@@ -1522,22 +1580,63 @@ def read_file_tool(
elif not _is_custom_mount_path(path):
path = _resolve_and_validate_user_data_path(path, thread_data)
# Custom mount paths are resolved by LocalSandbox._resolve_path()
content = sandbox.read_file(path)
# 读成功即记一次技能使用 —— requested_path 仍是虚拟路径(本地/容器沙箱都一样),
# 非技能路径会被 _record_skill_read 自行忽略。
_record_skill_read(requested_path)
if not content:
return "(empty)"
if start_line is not None and end_line is not None:
content = "\n".join(content.splitlines()[start_line - 1 : end_line])
try:
from deerflow.config.app_config import get_app_config
sandbox_cfg = get_app_config().sandbox
max_chars = sandbox_cfg.read_file_output_max_chars if sandbox_cfg else 50000
except Exception:
max_chars = 50000
return _truncate_read_file_output(content, max_chars)
try:
from deerflow.config.app_config import get_app_config
from deerflow.uploads.policy import get_runtime_large_file_policy
sandbox_cfg = get_app_config().sandbox
configured_max = sandbox_cfg.read_file_output_max_chars if sandbox_cfg else 50000
large_file_policy = get_runtime_large_file_policy()
except Exception:
configured_max = 50000
large_file_policy = {
"threshold_bytes": 1024 * 1024,
"max_result_chars": 12000,
"hard_max_result_chars": 32000,
}
policy_max = max(256, int(large_file_policy.get("max_result_chars", 12000)))
hard_max = max(256, min(32000, int(large_file_policy.get("hard_max_result_chars", 32000))))
max_chars = min(policy_max, hard_max, configured_max if configured_max > 0 else hard_max)
# A range-free read of a large local file returns deterministic
# head/middle/tail samples plus whole-file streaming statistics. The
# raw body is never materialized in memory or sent to the model.
if start_line is None and end_line is None and is_local_sandbox(runtime):
try:
physical_path = Path(path)
threshold = max(1, int(large_file_policy.get("threshold_bytes", 1024 * 1024)))
if physical_path.is_file() and physical_path.stat().st_size >= threshold:
from deerflow.uploads.inspection import inspect_text_file
inspected = inspect_text_file(
physical_path,
display_path=requested_path,
max_result_chars=max_chars,
hard_max_result_chars=hard_max,
)
_record_skill_read(requested_path)
return inspected["rendered"]
except OSError:
pass
# Reserve room for a continuation marker so the complete tool result,
# not just its body, remains under the hard cap.
body_budget = max(64, max_chars - 180)
content, next_line, truncated = sandbox.read_file_range(
path,
start_line=effective_start,
end_line=end_line,
max_chars=body_budget,
)
# 读成功即记一次技能使用 —— requested_path 仍是虚拟路径(本地/容器沙箱都一样),
# 非技能路径会被 _record_skill_read 自行忽略。
_record_skill_read(requested_path)
if not content and not truncated:
return "(empty)"
if truncated:
continuation = next_line or effective_start
content = f"{content}\n... [bounded read; continue with start_line={continuation}]"
return _truncate_read_file_output(content, max_chars)
except SandboxError as e:
return f"Error: {e}"
except FileNotFoundError: