From 63e9ee796f4a3297db8cbda08083ad2f404ad82c Mon Sep 17 00:00:00 2001 From: jxxghp Date: Mon, 23 Nov 2026 10:32:06 +0800 Subject: [PATCH] fix(agent): align locale, evaluation harness, complexity, and dependency baseline --- app/agent/tools/base.py | 60 ++++----- app/agent/tools/impl/api.py | 125 +++++++++++------- app/locales/en-US.json | 3 +- app/locales/zh-CN.json | 3 +- app/locales/zh-TW.json | 3 +- scripts/evaluation/runtime.py | 17 ++- .../architecture/dependency-baseline.json | 18 ++- 7 files changed, 148 insertions(+), 81 deletions(-) diff --git a/app/agent/tools/base.py b/app/agent/tools/base.py index c75a24a79..9fe363a17 100644 --- a/app/agent/tools/base.py +++ b/app/agent/tools/base.py @@ -898,6 +898,31 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta): roots.append(resolved_dir) return roots + def _check_non_admin_memory_access( + self, + resolved_path: Path, + memory_root: Path, + operation: str, + ) -> Optional[str]: + """ + 校验普通用户访问记忆目录时的归属与写权限边界。 + + :param resolved_path: 归一化后的目标路径 + :param memory_root: Agent 记忆根目录 + :param operation: 当前操作名称 + :return: 拒绝原因;允许时返回 None + """ + user_memory_root = memory_root / "users" + if self._is_path_relative_to(resolved_path, user_memory_root): + user_key = build_user_memory_key(str(self._agent_context.get("memory_owner_id") or self._user_id)) + own_memory_root = user_memory_root / user_key if user_key else None + if not own_memory_root or not self._is_path_relative_to(resolved_path, own_memory_root): + return "抱歉,普通用户只能访问自己的用户级记忆,不能读取或修改其他用户的记忆。" + return None + if operation != "读取": + return "抱歉,全局公共记忆只有系统管理员才能写入;请将个人偏好写入当前用户的记忆目录。" + return None + async def _check_local_file_access( self, path: str, operation: str = "访问" ) -> tuple[Optional[Path], Optional[str]]: @@ -937,6 +962,9 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta): return resolved_path, "抱歉,Agent 后台任务配置仅允许系统管理员访问。" memory_root = agent_root / "memory" + if self._is_path_relative_to(resolved_path, memory_root): + return resolved_path, self._check_non_admin_memory_access(resolved_path, memory_root, operation) + runtime_root = agent_root / "runtime" for protected in (runtime_root / 'learning' / 'users', runtime_root / 'history' / 'users', runtime_root / 'code' / 'users'): if self._is_path_relative_to(resolved_path, protected): @@ -944,39 +972,11 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta): if not user_key or not self._is_path_relative_to(resolved_path, protected / user_key): return resolved_path, '抱歉,不能读取或修改其他用户的 Agent 消息和个人技能。' if self._is_path_relative_to(resolved_path, runtime_root) and operation != "读取": - return ( - resolved_path, - "抱歉,Agent 人格及运行时配置只有系统管理员才能修改。", - ) - + return resolved_path, "抱歉,Agent 人格及运行时配置只有系统管理员才能修改。" if self._is_path_relative_to(resolved_path, agent_root / 'skills') and operation != '读取': return resolved_path, '抱歉,公共技能只有系统管理员才能修改;个人学习请使用 skill_manage。' - - if not self._is_path_relative_to(resolved_path, memory_root): - if operation != "读取": - return ( - resolved_path, - "抱歉,普通用户只能修改自己的用户级记忆,不能修改 Agent 公共或系统文件。", - ) - return resolved_path, None - - user_memory_root = memory_root / "users" - if self._is_path_relative_to(resolved_path, user_memory_root): - user_key = build_user_memory_key(str(self._agent_context.get("memory_owner_id") or self._user_id)) - own_memory_root = user_memory_root / user_key if user_key else None - if not own_memory_root or not self._is_path_relative_to(resolved_path, own_memory_root): - return ( - resolved_path, - "抱歉,普通用户只能访问自己的用户级记忆,不能读取或修改其他用户的记忆。", - ) - return resolved_path, None - if operation != "读取": - return ( - resolved_path, - "抱歉,全局公共记忆只有系统管理员才能写入;请将个人偏好写入当前用户的记忆目录。", - ) - + return resolved_path, "抱歉,普通用户只能修改自己的用户级记忆,不能修改 Agent 公共或系统文件。" return resolved_path, None async def _check_local_storage_access( diff --git a/app/agent/tools/impl/api.py b/app/agent/tools/impl/api.py index e04282e49..7aebb3027 100644 --- a/app/agent/tools/impl/api.py +++ b/app/agent/tools/impl/api.py @@ -310,6 +310,76 @@ class MoviePilotApiTool(MoviePilotTool): ensure_ascii=False, ) + def _sync_direct_web_user_context(self, user: Any, *, is_direct_user_channel: bool) -> None: + """ + 将实时查询到的网页用户状态与权限同步回当前工具上下文。 + + :param user: 实时数据库用户记录 + :param is_direct_user_channel: 是否为直接网页或 WebAgent 渠道 + """ + if not is_direct_user_channel: + return + if not user.is_active: + self._agent_context["web_user_active"] = False + return + normalized_permissions = normalize_web_permissions(getattr(user, "permissions", None)) + self._resolved_web_permissions = normalized_permissions + self._agent_context["web_user_active"] = True + self._agent_context["web_user_permissions"] = normalized_permissions + self._agent_context["is_admin"] = bool(user.is_superuser) + + def _check_operation_authorization( + self, + operation_id: str, + *, + requires_system_admin: bool, + is_admin: bool, + query: Optional[Dict[str, Any]], + body: Any, + ) -> Optional[str]: + """ + 按当前会话来源校验操作权限;拒绝时返回结构化 JSON 错误字符串。 + + :param operation_id: 白名单业务 API 操作标识 + :param requires_system_admin: 操作在非网页渠道下是否要求系统管理员 + :param is_admin: 当前解析出的主体是否为超级管理员 + :param query: 查询参数 + :param body: 请求体 + :return: 拒绝时的 JSON 错误响应;允许时返回 None + """ + if self._is_direct_web_session(): + if self._agent_context.get("web_user_active") is False: + message = "当前 MoviePilot 用户已停用,请联系管理员启用该用户后重试。" + self._record_identity_error(message) + return json.dumps( + {"success": False, "error": "permission_denied", "message": message}, + ensure_ascii=False, + ) + allowed, denial_reason = authorize_web_operation( + is_superuser=is_admin, + permissions=self._get_resolved_web_permissions(), + operation_id=operation_id, + media_type=self._extract_request_media_type(query=query, body=body), + ) + if not allowed: + self._record_identity_error(denial_reason) + return json.dumps( + {"success": False, "error": "permission_denied", "message": denial_reason}, + ensure_ascii=False, + ) + return None + if requires_system_admin and not is_admin: + self._record_identity_error("任务执行账号不具备此操作需要的系统管理员权限。") + return json.dumps( + { + "success": False, + "error": "permission_denied", + "message": "This MoviePilot API operation requires a system administrator.", + }, + ensure_ascii=False, + ) + return None + async def _resolve_superuser_integration_identity( self, ) -> tuple[str, Optional[str], bool]: @@ -396,16 +466,9 @@ class MoviePilotApiTool(MoviePilotTool): user = await self._resolve_channel_api_user(raw_user_id) if user is None: raise ApiExecutionError(self._identity_unavailable_message()) + self._sync_direct_web_user_context(user, is_direct_user_channel=is_direct_user_channel) if not user.is_active: - if is_direct_user_channel: - self._agent_context["web_user_active"] = False raise ApiExecutionError("当前 MoviePilot 用户已停用,请联系管理员启用该用户后重试。重新绑定不能解除停用。") - if is_direct_user_channel: - normalized_permissions = normalize_web_permissions(getattr(user, "permissions", None)) - self._resolved_web_permissions = normalized_permissions - self._agent_context["web_user_active"] = True - self._agent_context["web_user_permissions"] = normalized_permissions - self._agent_context["is_admin"] = bool(user.is_superuser) return str(user.id), user.name, bool(user.is_superuser) def _identity_unavailable_message(self) -> str: @@ -553,43 +616,15 @@ class MoviePilotApiTool(MoviePilotTool): require_system_admin=requires_system_admin, public_read_only=operation_id in API_UNBOUND_READ_OPERATIONS, ) - if self._is_direct_web_session(): - if self._agent_context.get("web_user_active") is False: - self._record_identity_error("当前 MoviePilot 用户已停用,请联系管理员启用该用户后重试。") - return json.dumps( - { - "success": False, - "error": "permission_denied", - "message": "当前 MoviePilot 用户已停用,请联系管理员启用该用户后重试。", - }, - ensure_ascii=False, - ) - allowed, denial_reason = authorize_web_operation( - is_superuser=is_admin, - permissions=self._get_resolved_web_permissions(), - operation_id=operation_id, - media_type=self._extract_request_media_type(query=query, body=body), - ) - if not allowed: - self._record_identity_error(denial_reason) - return json.dumps( - { - "success": False, - "error": "permission_denied", - "message": denial_reason, - }, - ensure_ascii=False, - ) - elif requires_system_admin and not is_admin: - self._record_identity_error("任务执行账号不具备此操作需要的系统管理员权限。") - return json.dumps( - { - "success": False, - "error": "permission_denied", - "message": "This MoviePilot API operation requires a system administrator.", - }, - ensure_ascii=False, - ) + denied_response = self._check_operation_authorization( + operation_id, + requires_system_admin=requires_system_admin, + is_admin=is_admin, + query=query, + body=body, + ) + if denied_response is not None: + return denied_response result = await executor.execute( operation_id, path_params=path_params, diff --git a/app/locales/en-US.json b/app/locales/en-US.json index 0d70c9a2e..95b7fac48 100644 --- a/app/locales/en-US.json +++ b/app/locales/en-US.json @@ -680,7 +680,8 @@ "渠道授权服务尚未就绪": "The channel authorization service is not ready", "请使用 MoviePilot 系统管理员账号管理渠道授权": "Use a MoviePilot system administrator account to manage channel authorization", "目标渠道不存在或尚未启用": "The target channel does not exist or is disabled", - "渠道授权服务不可用": "The channel authorization service is unavailable" + "渠道授权服务不可用": "The channel authorization service is unavailable", + "用户未激活": "User is inactive" }, "message_patterns": [ { diff --git a/app/locales/zh-CN.json b/app/locales/zh-CN.json index ea09a3148..b7506e66d 100644 --- a/app/locales/zh-CN.json +++ b/app/locales/zh-CN.json @@ -206,7 +206,8 @@ "渠道授权服务尚未就绪": "渠道授权服务尚未就绪", "请使用 MoviePilot 系统管理员账号管理渠道授权": "请使用 MoviePilot 系统管理员账号管理渠道授权", "目标渠道不存在或尚未启用": "目标渠道不存在或尚未启用", - "渠道授权服务不可用": "渠道授权服务不可用" + "渠道授权服务不可用": "渠道授权服务不可用", + "用户未激活": "用户未激活" }, "message_patterns": [ { diff --git a/app/locales/zh-TW.json b/app/locales/zh-TW.json index 20a7be287..992cef484 100644 --- a/app/locales/zh-TW.json +++ b/app/locales/zh-TW.json @@ -621,7 +621,8 @@ "渠道授权服务尚未就绪": "渠道授權服務尚未就緒", "请使用 MoviePilot 系统管理员账号管理渠道授权": "請使用 MoviePilot 系統管理員帳號管理渠道授權", "目标渠道不存在或尚未启用": "目標渠道不存在或尚未啟用", - "渠道授权服务不可用": "渠道授權服務無法使用" + "渠道授权服务不可用": "渠道授權服務無法使用", + "用户未激活": "使用者未啟用" }, "message_patterns": [ { diff --git a/scripts/evaluation/runtime.py b/scripts/evaluation/runtime.py index 58bdaebc4..438a14525 100644 --- a/scripts/evaluation/runtime.py +++ b/scripts/evaluation/runtime.py @@ -833,10 +833,25 @@ async def _run_isolated( output: list[str] = [] command_root = directory / "command-workspace" command_root.mkdir() + eval_user = SimpleNamespace( + id=1, + name="evaluation", + is_active=True, + is_superuser=True, + permissions={}, + ) + + async def _eval_get_user(_key: Any) -> Any: + return eval_user + + eval_users = SimpleNamespace( + async_get_by_id=_eval_get_user, + async_get_by_name=_eval_get_user, + ) agent = _agent_type()( session_id=session_id, user_id="1", username="evaluation", channel=NotificationChannel.WebAgent.value, source="evaluation", is_channel_admin=True, replay_mode=ReplyMode.CAPTURE_ONLY, allow_message_tools=False, - output_callback=output.append, data=SimpleNamespace(invocations=invocation_repository), + output_callback=output.append, data=SimpleNamespace(invocations=invocation_repository, users=eval_users), memory=MemoryManager(chat=memory_port, persistence=memory_port), model=model, model_name=model_name, context_window=context_window, max_iterations=max_iterations, ) diff --git a/tests/fixtures/architecture/dependency-baseline.json b/tests/fixtures/architecture/dependency-baseline.json index e10a8d6f8..934a89090 100644 --- a/tests/fixtures/architecture/dependency-baseline.json +++ b/tests/fixtures/architecture/dependency-baseline.json @@ -978,8 +978,8 @@ "runtime_only": true } }, - "edge_count": 10510, - "edge_sha256": "3a971f618840e8dcf079a7e8af829f269d27350c5910443ea9d34f792d9c56be", + "edge_count": 10524, + "edge_sha256": "51067d3a051ddd91b2c89819b815f71a4e9dbeb95e2572a3fa6315263ca2e39e", "edges": [ "app -> app.foundation", "app -> app.foundation.environment", @@ -1683,6 +1683,8 @@ "app.agent.middleware.policy -> app.application", "app.agent.middleware.policy -> app.application.security", "app.agent.middleware.policy -> app.application.security.authorization", + "app.agent.middleware.policy -> app.schemas", + "app.agent.middleware.policy -> app.schemas.types", "app.agent.middleware.recall -> app.agent", "app.agent.middleware.recall -> app.agent.history", "app.agent.middleware.recall -> app.agent.history.message", @@ -1821,6 +1823,8 @@ "app.agent.orchestrator -> app.application.security.authorization", "app.agent.orchestrator -> app.chain", "app.agent.orchestrator -> app.chain.agent", + "app.agent.orchestrator -> app.domain", + "app.agent.orchestrator -> app.domain.authorization", "app.agent.orchestrator -> app.foundation", "app.agent.orchestrator -> app.foundation.identity", "app.agent.orchestrator -> app.runtime", @@ -2052,6 +2056,8 @@ "app.agent.tools.impl.api -> app.application.security", "app.agent.tools.impl.api -> app.application.security.auth", "app.agent.tools.impl.api -> app.application.security.authorization", + "app.agent.tools.impl.api -> app.domain", + "app.agent.tools.impl.api -> app.domain.authorization", "app.agent.tools.impl.api -> app.schemas", "app.agent.tools.impl.api -> app.schemas.common", "app.agent.tools.impl.api -> app.schemas.types", @@ -2268,6 +2274,8 @@ "app.agent.web -> app.agent", "app.agent.web -> app.agent.callback", "app.agent.web -> app.agent.loader", + "app.agent.web -> app.agent.tools", + "app.agent.web -> app.agent.tools.base", "app.agent.web -> app.application", "app.agent.web -> app.application.security", "app.agent.web -> app.application.security.user", @@ -2314,6 +2322,8 @@ "app.api.dependencies.auth -> app.application.security.passkey", "app.api.dependencies.auth -> app.application.security.user", "app.api.dependencies.auth -> app.application.security.userconfig", + "app.api.dependencies.auth -> app.domain", + "app.api.dependencies.auth -> app.domain.authorization", "app.api.dependencies.auth -> app.runtime", "app.api.dependencies.auth -> app.runtime.execution", "app.api.dependencies.auth -> app.schemas", @@ -2674,6 +2684,7 @@ "app.api.endpoints.download -> app.chain.download", "app.api.endpoints.download -> app.chain.media", "app.api.endpoints.download -> app.domain", + "app.api.endpoints.download -> app.domain.authorization", "app.api.endpoints.download -> app.domain.context", "app.api.endpoints.download -> app.domain.media", "app.api.endpoints.download -> app.domain.meta", @@ -2727,9 +2738,12 @@ "app.api.endpoints.history -> app.application.history.registration", "app.api.endpoints.history -> app.application.security", "app.api.endpoints.history -> app.application.security.authorization", + "app.api.endpoints.history -> app.application.security.user", "app.api.endpoints.history -> app.application.transfer", "app.api.endpoints.history -> app.application.transfer.execution", "app.api.endpoints.history -> app.application.transfer.recovery", + "app.api.endpoints.history -> app.domain", + "app.api.endpoints.history -> app.domain.authorization", "app.api.endpoints.history -> app.runtime", "app.api.endpoints.history -> app.runtime.execution", "app.api.endpoints.history -> app.runtime.log",