AgentEvalTool/tests/unit/test_orphan_runs.py
sinohqb 5db0ede4f4
Some checks failed
CI / test (push) Failing after 50s
refactor(judgement): converge case-pass decision into one deep module
「用例是否通过」此前散落 8 处且互相矛盾:engine 权威判定焊死在持久化里
不可单测;report 聚合/compare/markdown 各自从规则结果反推,规则还不一致
(markdown 用 all([]) 把故障用例误渲染成 )。

- 新增纯函数 evaluation/judgement.combine_case_outcome(RuleOutcome/
  CaseOutcome),判定组合脱离通道与 DB 可单测(判定矩阵 14 例)
- engine 调用它一次,逐用例权威结果写入 summary.case_outcomes(JSON,
  零迁移);report/compare/markdown 只读权威值,老 run fallback 反推
- 故障用例判 False(ADR-0002):修正 markdown 的  bug 与 compare 的
  None;顺带修 engine 连通用例无回复也算通过的 bug
- pass_rate 口径改为用例级(CONTEXT.md 词条),规则级保留在
  passed_rules/total_rules;CLI 对比标签同步更正
- 修 RunRepository.update 漏拷 scenario_version/triggered_by 的字段漂移
2026-07-29 19:45:02 +08:00

57 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Startup cleanup of orphan runs (interrupted by server restart)."""
from agenteval.models import EvalRun, RunStatus
from agenteval.storage.repository import RunRepository
def _make_run(session, status: RunStatus) -> str:
run = RunRepository(session).create(EvalRun(
target_id="t-1", scenario_id="s-1", status=status,
))
return run.id
def test_mark_orphans_failed(db_session):
repo = RunRepository(db_session)
running_id = _make_run(db_session, RunStatus.RUNNING)
pending_id = _make_run(db_session, RunStatus.PENDING)
completed_id = _make_run(db_session, RunStatus.COMPLETED)
failed_id = _make_run(db_session, RunStatus.FAILED)
count = repo.mark_orphans_failed()
assert count == 2
for rid in (running_id, pending_id):
run = repo.get(rid)
assert run.status == RunStatus.FAILED
assert run.summary["error"]["code"] == "interrupted"
assert run.completed_at is not None
# 已完结的运行不受影响
assert repo.get(completed_id).status == RunStatus.COMPLETED
assert repo.get(failed_id).status == RunStatus.FAILED
assert repo.get(completed_id).summary is None
def test_mark_orphans_failed_noop_when_clean(db_session):
repo = RunRepository(db_session)
_make_run(db_session, RunStatus.COMPLETED)
assert repo.mark_orphans_failed() == 0
def test_update_preserves_scenario_version_and_triggered_by(db_session):
"""update() 不得丢字段scenario_version / triggered_by 必须回写(漂移回归)。"""
from agenteval.models import RunTrigger
repo = RunRepository(db_session)
run = repo.create(EvalRun(
target_id="t-1", scenario_id="s-1", status=RunStatus.RUNNING,
scenario_version=4, triggered_by=RunTrigger.AI_ASSISTANT,
))
run.scenario_version = 5
run.triggered_by = RunTrigger.CLI
run.status = RunStatus.COMPLETED
updated = repo.update(run)
assert updated.scenario_version == 5
assert updated.triggered_by == RunTrigger.CLI