Merge pull request #7050 from DDSRem-Dev/codex-comment-migration

docs: 统一全项目中文注释和结构化契约说明
This commit is contained in:
DDSRem
2026-10-17 19:26:29 +08:00
committed by GitHub
parent 3c7431b7da
commit 2de976f6b3
2345 changed files with 143990 additions and 61534 deletions

View File

@@ -1,4 +1,6 @@
"""用真实 Agent 图和 Python RPC 验证宿主策略、Skill 范围及完整结果聚合。"""
"""
用真实 Agent 图和 Python RPC 验证宿主策略、Skill 范围及完整结果聚合。
"""
import asyncio
import json
@@ -26,37 +28,53 @@ from app.agent.tools.impl.execute_code import ExecuteCodeTool
class CodeModel(FakeMessagesListChatModel):
"""只有模型输出固定,真实工具节点、Python 和 RPC 都参与执行。"""
"""
只有模型输出固定,真实工具节点、Python 和 RPC 都参与执行。
"""
def bind_tools(self, _tools, **_kwargs):
"""不访问外部模型供应商。"""
"""
不访问外部模型供应商。
"""
return self
class RecordedPolicy(AgentToolPolicyOrchestrator):
"""保留真实宿主策略,只额外记录每次独立工具回执。"""
"""
保留真实宿主策略,只额外记录每次独立工具回执。
"""
def __init__(self):
"""为当前测试建立独立记录。"""
"""
为当前测试建立独立记录。
"""
super().__init__()
self.receipts = []
def finish(self, observation, result):
"""确认内部调用走完整策略结束路径。"""
"""
确认内部调用走完整策略结束路径。
"""
receipt = super().finish(observation, result)
self.receipts.append(receipt)
return receipt
class PageExecutor:
"""回放分页列表,每页正文超过模型工具预览上限,最后一条仍须参与 Python 聚合。"""
"""
回放分页列表,每页正文超过模型工具预览上限,最后一条仍须参与 Python 聚合。
"""
def __init__(self):
"""记录真实 API 网关交给执行边界的参数。"""
"""
记录真实 API 网关交给执行边界的参数。
"""
self.calls = []
async def execute(self, operation_id, **kwargs):
"""返回确定性的分页数据,不访问主库、HTTP 或下载器。"""
"""
返回确定性的分页数据,不访问主库、HTTP 或下载器。
"""
self.calls.append((operation_id, kwargs))
page = kwargs['query']['page']
return json.dumps({'success': True, 'data': [
@@ -67,7 +85,9 @@ class PageExecutor:
@pytest_asyncio.fixture
async def environment(tmp_path, monkeypatch):
"""只替换模型和外部 API 执行边界,管理器及工具实例使用真实实现。"""
"""
只替换模型和外部 API 执行边界,管理器及工具实例使用真实实现。
"""
manager = CodeSessionManager(tmp_path / 'code')
monkeypatch.setattr(code_manager, 'code_session_manager', manager)
context = ToolPolicyContext(session_id='code', user_id='alice', origin=ToolOrigin.AGENT_INTERACTIVE,
@@ -101,12 +121,16 @@ async def environment(tmp_path, monkeypatch):
def _call(identifier, name, arguments):
"""每个模型调用使用唯一 ID,内部 RPC 也必须独立生成 ID。"""
"""
每个模型调用使用唯一 ID,内部 RPC 也必须独立生成 ID。
"""
return AIMessage(content='', tool_calls=[{'name': name, 'args': arguments, 'id': identifier}])
def _program():
"""三页完整数据在 Python 内聚合,只打印最终数字。"""
"""
三页完整数据在 Python 内聚合,只打印最终数字。
"""
return ('from moviepilot_tools import moviepilot_api\n'
'rows = []\nfor page in range(1, 4):\n'
' rows += moviepilot_api("subscription.list", query={"page": page, "count": 100})["data"]\n'
@@ -115,7 +139,9 @@ def _program():
@pytest.mark.asyncio
async def test_real_graph_pages_full_results_and_uses_each_policy_receipt(environment):
"""大结果没有被模型预览截断,三次 API 均有真实回执,内层调用不污染循环检测器。"""
"""
大结果没有被模型预览截断,三次 API 均有真实回执,内层调用不污染循环检测器。
"""
scope, executor, api, tool, _, guard, recorded, middlewares = environment
model = CodeModel(responses=[_call('skill', 'read_skill', {'name': 'pages'}),
_call('code', 'execute_code', {'code': _program()}), AIMessage(content='分析完成')])
@@ -136,7 +162,9 @@ async def test_real_graph_pages_full_results_and_uses_each_policy_receipt(enviro
@pytest.mark.asyncio
async def test_code_cannot_bypass_unloaded_skill(environment):
"""直接在 Python 调用 API 仍被真实 Skill 中间件拒绝,不能靠代码工具隐式加载权限。"""
"""
直接在 Python 调用 API 仍被真实 Skill 中间件拒绝,不能靠代码工具隐式加载权限。
"""
scope, executor, api, tool, _, _, _, middlewares = environment
model = CodeModel(responses=[_call('code', 'execute_code', {'code': _program()}), AIMessage(content='范围未加载')])
graph = create_agent(model=model, tools=[api, tool], middleware=middlewares)
@@ -151,16 +179,22 @@ async def test_code_cannot_bypass_unloaded_skill(environment):
class DropApiMiddleware(AgentMiddleware):
"""模拟真实工具筛选,证明 RPC 集合取自最终请求而非完整目录。"""
"""
模拟真实工具筛选,证明 RPC 集合取自最终请求而非完整目录。
"""
async def awrap_model_call(self, request, handler):
"""移除 API 后的 Python 环境不能继续导入 API 存根。"""
"""
移除 API 后的 Python 环境不能继续导入 API 存根。
"""
return await handler(request.override(tools=[tool for tool in request.tools if tool.name != 'moviepilot_api']))
@pytest.mark.asyncio
async def test_selected_tools_intersection_is_enforced(environment):
"""目录中存在但本次请求被筛掉的 API 不可从 Python 使用。"""
"""
目录中存在但本次请求被筛掉的 API 不可从 Python 使用。
"""
scope, executor, api, tool, _, _, _, middlewares = environment
middlewares.insert(-1, DropApiMiddleware())
model = CodeModel(responses=[_call('code', 'execute_code', {'code': _program()}), AIMessage(content='工具不可用')])
@@ -175,14 +209,18 @@ async def test_selected_tools_intersection_is_enforced(environment):
@pytest.mark.asyncio
async def test_direct_tool_invocation_cannot_create_host_authority(environment):
"""仅传入管理员标志不足以建立内部执行窗口,外部工具直接调用保持拒绝。"""
"""
仅传入管理员标志不足以建立内部执行窗口,外部工具直接调用保持拒绝。
"""
_, _, _, tool, _, _, _, _ = environment
result = json.loads(await tool.ainvoke({'code': 'print("must not execute")'}))
assert result['success'] is False and not code_manager.code_session_manager._entries
def test_code_tool_not_published_to_unbound_external_or_nonadmin_catalog(monkeypatch):
"""外部工具目录和普通用户不暴露任意主机 Python;管理员会话默认可用。"""
"""
外部工具目录和普通用户不暴露任意主机 Python;管理员会话默认可用。
"""
monkeypatch.setattr('app.agent.tools.factory._get_plugin_agent_tools', lambda: [])
for context, external, expected in (({}, False, False), ({'is_admin': True}, True, False), ({'is_admin': True}, False, True)):
tools = MoviePilotToolFactory.create_tools(session_id='code', user_id='alice', agent_context=context,