From 7195a29c33084a2824ede59477479c4bb91c92f6 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 11 Sep 2026 07:08:48 +0800 Subject: [PATCH] fix(evaluation): scope skill file reads to temp agent --- scripts/evaluation/runtime.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/scripts/evaluation/runtime.py b/scripts/evaluation/runtime.py index 43322e815..ef632364d 100644 --- a/scripts/evaluation/runtime.py +++ b/scripts/evaluation/runtime.py @@ -13,6 +13,9 @@ from unittest.mock import patch from urllib.parse import unquote, urlsplit from uuid import uuid4 +from pydantic import PrivateAttr + +from app.agent.tools.impl.read_file import ReadFileTool from scripts.evaluation.world import EvaluationWorld if TYPE_CHECKING: @@ -112,6 +115,21 @@ class _MemoryPort: self.messages = messages +class _EvaluationReadFileTool(ReadFileTool): + """评测专用文件读取工具,只允许访问本次运行的临时 Agent 根目录。""" + + _evaluation_allowed_root: Path = PrivateAttr() + + def __init__(self, *, allowed_root: Path, **kwargs: Any) -> None: + """绑定隔离根目录后复用生产文件读取、大小限制和行范围合同。""" + super().__init__(**kwargs) + self._evaluation_allowed_root = allowed_root.resolve() + + def _get_non_admin_local_file_roots(self) -> list[Path]: + """返回评测临时 Agent 根,避免回退到宿主 CONFIG_PATH/agent。""" + return [self._evaluation_allowed_root] + + class _McpDirectory: """受控目录不配置任何外部 MCP,禁止发现阶段启动服务或联网。""" @@ -262,7 +280,6 @@ async def _run_isolated( from app.agent.contracts import ReplyMode from app.agent.memory import MemoryManager from app.agent.tools.impl.api import MoviePilotApiTool - from app.agent.tools.impl.read_file import ReadFileTool from app.db.adapters.invocation import TransactionalInvocationRepository from app.db.models.agentinvocation import AgentInvocation from app.schemas.types import NotificationChannel @@ -299,7 +316,9 @@ async def _run_isolated( # Skill 主体只返回相对 supporting_files;评测必须提供与生产一致的 # read_file 能力,才能验证模型是否按需加载 api/*.md 合同。工具使用 # 非管理员上下文,只能读取当前临时 CONFIG_DIR/agent 隔离目录。 - skill_file_tool = ReadFileTool(session_id=agent.session_id, user_id="1") + skill_file_tool = _EvaluationReadFileTool( + allowed_root=directory / "agent", session_id=agent.session_id, user_id="1", + ) skill_file_tool.set_message_attr(agent.channel, agent.source, agent.username) skill_file_tool.set_agent_context({ "is_admin": False,