eval: add command and browser parity evidence

This commit is contained in:
jxxghp
2026-09-11 11:16:20 +08:00
parent 5e50caabcb
commit 3cfbf9dbcf
12 changed files with 581 additions and 46 deletions

View File

@@ -16,6 +16,7 @@ import pytest
from scripts.evaluation import codex
from scripts.evaluation.models import ModelSettings
from scripts.evaluation.proxy import NATIVE_SHELL_TOOLS, project_tools
def _program(tmp_path: Path, code: str) -> list[str]:
@@ -351,3 +352,21 @@ def test_toml_arguments_preserve_literals_without_shell_interpretation() -> None
"""带引号、换行和 shell 字符的模型配置仍是单个正确 TOML 值。"""
value = {"name": "引号\"与换行\n以及$(command)", "enabled": False, "budget": 12}
assert tomllib.loads("value=" + codex._toml(value))["value"] == value
def test_command_scenario_enables_only_native_shell_controls() -> None:
"""命令场景显式开启原生终端开关,普通 API 场景继续关闭。"""
settings = ModelSettings("gpt-test", "https://provider.invalid/v1", "provider-key")
proxy = SimpleNamespace(endpoint="http://model.invalid/v1")
server = SimpleNamespace(endpoint="http://mcp.invalid/mcp", bearer_token="token")
control = Path("/tmp/evaluation-control")
api_config = codex._configuration(settings, proxy, server, control, scenario_id="dedup_existing")
command_config = codex._configuration(settings, proxy, server, control, scenario_id="command_execution")
assert all(api_config[f"features.{name}"] is False for name in ("shell_tool", "unified_exec", "shell_snapshot"))
assert all(command_config[f"features.{name}"] is True for name in ("shell_tool", "unified_exec", "shell_snapshot"))
tools = [{"type": "namespace", "name": "functions", "tools": [
{"type": "function", "name": "exec_command", "parameters": {}},
{"type": "function", "name": "write_stdin", "parameters": {}},
]}]
_, retained, removed = project_tools(tools, extra_native_tools=NATIVE_SHELL_TOOLS)
assert retained == ["functions.exec_command", "functions.write_stdin"] and removed == []

View File

@@ -17,9 +17,10 @@ from app.agent.api.executor import MoviePilotApiExecutor
from app.agent.middleware import config as config_middleware
from app.agent.middleware import subagents
from app.agent.middleware.summarization import FinalRequestCompactionMiddleware
from app.agent.terminal.ownership import TerminalScope, bind_terminal_scope, close_terminal_scope
from app.db.adapters.invocation import TransactionalInvocationRepository
from app.db.models.agentinvocation import AgentInvocation
from scripts.evaluation.runtime import _Transport, run_moviepilot
from scripts.evaluation.runtime import _EvaluationExecuteCommandTool, _Transport, run_moviepilot
from scripts.evaluation.score import evaluate
from scripts.evaluation.world import EvaluationWorld
@@ -224,6 +225,21 @@ async def test_transport_rejects_external_and_unlisted_routes():
assert world.ledger == []
@pytest.mark.asyncio
async def test_evaluation_command_tool_rejects_wrong_input_with_correction(tmp_path):
"""评测命令工具拒绝越界输入,并把可修正的合同返回给模型。"""
world = EvaluationWorld("command_execution")
tool = _EvaluationExecuteCommandTool(world=world, allowed_root=tmp_path, session_id="command-test", user_id="1")
scope = TerminalScope(user_id="1", task_id="command-test", kind="conversation")
with bind_terminal_scope(scope):
result = json.loads(await tool.run(action="run", command="echo outside"))
assert result["execution_outcome"] == "failed"
assert result["error"] == "evaluation_command_rejected"
assert "完全一致" in result["message"]
assert world.ledger[0]["operation_id"] == "execute_command"
assert await close_terminal_scope(scope)
@pytest.mark.asyncio
async def test_runtime_refuses_mismatched_config(monkeypatch, tmp_path):
"""导入后更换 CONFIG_DIR 不能被误认成已隔离后端。"""

View File

@@ -148,6 +148,52 @@ def test_model_input_contains_known_resource_but_hides_scenario_kind():
assert world.scenario.scenario_id not in text
def test_command_scenario_requires_real_tool_output_and_accepts_production_preview_wrapper():
"""命令场景只信工具账本,并兼容生产 stdout 预览的标题包装。"""
world = EvaluationWorld("command_execution")
world.record_command(world.scenario.command, {
"execution_outcome": "succeeded", "exit_code": 0,
"output": "[标准输出]\nMOVIEPILOT_COMMAND_OK",
})
report = {
"status": "completed", "command_output": "MOVIEPILOT_COMMAND_OK\n", "command_exit_code": 0,
"completed": ["command"], "unresolved": [],
"subscription_ids": [], "download_ids": [], "enabled_site_ids": [],
}
grade = evaluate(world, report)
assert grade.passed is True
world.record_command(world.scenario.command, {
"execution_outcome": "succeeded", "exit_code": 0, "output": "MOVIEPILOT_COMMAND_OK\n",
})
assert "command_not_verified" in evaluate(world, report).violations
def test_command_scenario_rejects_same_output_from_a_different_command():
"""相同输出不能掩盖原生 shell 实际执行了其他命令。"""
world = EvaluationWorld("command_execution")
world.record_command("printf 'MOVIEPILOT_COMMAND_OK\\n'; echo unexpected", {
"execution_outcome": "succeeded", "exit_code": 0, "output": "MOVIEPILOT_COMMAND_OK\n",
})
report = {
"status": "completed", "command_output": "MOVIEPILOT_COMMAND_OK\n", "command_exit_code": 0,
"completed": ["command"], "unresolved": [],
"subscription_ids": [], "download_ids": [], "enabled_site_ids": [],
}
assert "command_not_verified" in evaluate(world, report).violations
def test_browser_scenario_requires_dynamic_page_observation():
"""浏览器场景没有页面回执时,即使报告文本正确也不能通过。"""
world = EvaluationWorld("browser_navigation")
report = {
"status": "completed", "browser_text": "BROWSER_OK", "completed": ["browser"], "unresolved": [],
"subscription_ids": [], "download_ids": [], "enabled_site_ids": [],
}
assert "browser_not_verified" in evaluate(world, report).violations
world.record_browser("get_content", {"success": True, "execution_outcome": "succeeded", "content": "BROWSER_OK"})
assert evaluate(world, report).passed is True
def test_cli_reports_failure_without_executing_any_model(tmp_path):
"""独立命令行真实退出码与报告表达失败,且显式标注只是离线轨迹验收。"""
trace = tmp_path / "trace.json"

View File

@@ -30,13 +30,30 @@ def _download_body(world: EvaluationWorld) -> dict[str, Any]:
def test_scenarios_expose_inputs_without_initial_state_or_oracle() -> None:
"""公开定义只有任务、业务身份及资源,答案和故障时序不能进入模型上下文。"""
scenarios = list_scenarios()
assert len(scenarios) == 3
assert len({scenario.media_id for scenario in scenarios}) == 3
assert len(scenarios) == 5
api_scenarios = [scenario for scenario in scenarios if scenario.kind == "api"]
assert len(api_scenarios) == 3
assert len({scenario.media_id for scenario in api_scenarios}) == 3
for scenario in scenarios:
assert set(asdict(scenario)) == {"scenario_id", "task", "media_source", "media_id", "title", "magnet", "infohash"}
assert scenario.infohash in scenario.magnet
assert set(asdict(scenario)) == {
"scenario_id", "task", "media_source", "media_id", "title", "magnet", "infohash",
"kind", "command", "browser_url",
}
assert "JSON" in scenario.task
assert "默认下载器和目录已配置" in scenario.task
assert scenario.scenario_id not in scenario.task
if scenario.kind == "api":
assert scenario.infohash in scenario.magnet
assert "默认下载器和目录已配置" in scenario.task
assert scenario.command == ""
assert scenario.browser_url == ""
elif scenario.kind == "command":
assert scenario.command
assert scenario.browser_url == ""
assert not scenario.media_id and not scenario.infohash
else:
assert scenario.kind == "browser"
assert scenario.browser_url == "__EVALUATION_BROWSER_URL__"
assert not scenario.media_id and not scenario.infohash
assert "旧译名" not in scenario.task
with pytest.raises(FrozenInstanceError):
scenario.title = "污染输入"