202 lines
10 KiB
Python
202 lines
10 KiB
Python
"""同一版画面不得反复看图:质检结论要按产物指纹复用。
|
||
|
||
项目 9《自检-异步调度验证》run #41 的实测事故:镜头 03 的新画面在 10:08:39
|
||
被判 **100 分合格**,六分钟后同一条调度里的 quality_check 又对**同一个文件**
|
||
重新看了一遍图,这次判 94 分不合格 → 把已经合格的镜头推回返工循环,
|
||
多烧两趟四分钟的出图,最后以「自动返工 2 轮仍未通过」把整条 LLM 调度判失败。
|
||
用户看到的就是「指令下了,项目卡住不动」。
|
||
看图模型本身有随机性,复判同一个文件不是「更准」,而是重新摇一次骰子。
|
||
|
||
现在:产物指纹(output_checksum)没变且上一轮合格 → 沿用结论并留痕;
|
||
只有画面真的换了新版本(重出、返工、人工替换)才重新质检。
|
||
顺带修掉两个同源问题:过肩镜头画出不属于任何人的第三只手;
|
||
以及 SQLite 抢写锁把正在跑的任务误判成 runtime_error。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from app.constants import ShotStatus, TaskKind
|
||
from app.models import EventLog, Project, Shot, Task
|
||
from app.services import agent, prompt, quality, visual_qc
|
||
|
||
|
||
def _shots(db, pid):
|
||
project = db.get(Project, pid)
|
||
return sorted(project.shots or [], key=lambda row: (row.order_index, row.id))
|
||
|
||
|
||
def _generated(api, db, pid):
|
||
api.post(f"/api/projects/{pid}/import",
|
||
data={"script_text": "1. 紫小慧求助,俊杰安抚。", "default_duration": 2},
|
||
files=[("images", ("s01.png", None, "image/png"))])
|
||
return _shots(db, pid)
|
||
|
||
|
||
def _qc_task_count(db, pid, shot_id):
|
||
return db.query(Task).filter(Task.project_id == pid, Task.kind == TaskKind.QC_SHOT,
|
||
Task.shot_id == shot_id).count()
|
||
|
||
|
||
# ------------------------------------------------------------------ 结论复用
|
||
def test_a_passed_artifact_is_not_judged_a_second_time(api, db, studio, make_image_png):
|
||
"""画面没换版本时,quality_check 不得再排一次看图任务(复判 = 抽奖 + 白烧 GPU)。"""
|
||
pid = studio["project_id"]
|
||
api.post(f"/api/projects/{pid}/import",
|
||
data={"script_text": "1. 紫小慧求助。\n2. 俊杰安抚。", "default_duration": 2},
|
||
files=[("images", ("s01.png", make_image_png(480, 270, "0x226699"), "image/png")),
|
||
("images", ("s02.png", make_image_png(480, 270, "0x993355"), "image/png"))])
|
||
api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": False})
|
||
api.drain(pid)
|
||
db.expire_all()
|
||
shot = _shots(db, pid)[0]
|
||
assert bool((shot.qc_json or {}).get("passed")) is True
|
||
# 模拟生产里的真实窗口:镜头状态还被上一步当成「刚出图待质检」
|
||
shot.status = ShotStatus.GENERATED
|
||
db.commit()
|
||
|
||
before = _qc_task_count(db, pid, shot.id)
|
||
project = db.get(Project, pid)
|
||
_, task_ids, failed, outcome = agent._qc_pass(db, project, [shot], 120)
|
||
|
||
assert task_ids == [], "同一个文件不该再排一次质检任务"
|
||
assert failed == [], failed
|
||
assert _qc_task_count(db, pid, shot.id) == before
|
||
assert outcome.get("reused_shot_ids") == [shot.id]
|
||
actions = [row.action for row in db.query(EventLog).filter(EventLog.project_id == pid)]
|
||
assert "qc.reused" in actions, "沿用旧结论必须留痕,否则审核人不知道为什么没跑质检"
|
||
|
||
|
||
def test_a_new_artifact_is_judged_again(api, db, studio, make_image_png):
|
||
"""画面重出后产物指纹变了:旧结论必须作废,否则返工等于没检。"""
|
||
pid = studio["project_id"]
|
||
api.post(f"/api/projects/{pid}/import",
|
||
data={"script_text": "1. 紫小慧求助。\n2. 俊杰安抚。", "default_duration": 2},
|
||
files=[("images", ("s01.png", make_image_png(480, 270, "0x226699"), "image/png")),
|
||
("images", ("s02.png", make_image_png(480, 270, "0x993355"), "image/png"))])
|
||
api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": False})
|
||
api.drain(pid)
|
||
db.expire_all()
|
||
shot = _shots(db, pid)[0]
|
||
before = _qc_task_count(db, pid, shot.id)
|
||
shot.output_checksum = "0" * 32 # 换了一版画面
|
||
shot.status = ShotStatus.GENERATED
|
||
db.commit()
|
||
|
||
_, task_ids, failed, _ = agent._qc_pass(db, db.get(Project, pid), [shot], 180)
|
||
assert task_ids, "产物变了必须重新看图"
|
||
assert failed == [] and _qc_task_count(db, pid, shot.id) == before + 1
|
||
|
||
|
||
def test_force_recheck_ignores_the_cached_verdict(api, db, studio, make_image_png):
|
||
"""质检规则升级后人工要求「全部复查」时,缓存结论要让路。"""
|
||
pid = studio["project_id"]
|
||
api.post(f"/api/projects/{pid}/import",
|
||
data={"script_text": "1. 紫小慧求助。\n2. 俊杰安抚。", "default_duration": 2},
|
||
files=[("images", ("s01.png", make_image_png(480, 270, "0x226699"), "image/png")),
|
||
("images", ("s02.png", make_image_png(480, 270, "0x993355"), "image/png"))])
|
||
api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": False})
|
||
api.drain(pid)
|
||
db.expire_all()
|
||
shot = _shots(db, pid)[0]
|
||
before = _qc_task_count(db, pid, shot.id)
|
||
_, task_ids, _, _ = agent._qc_pass(db, db.get(Project, pid), [shot], 180, reuse_passed=False)
|
||
assert task_ids and _qc_task_count(db, pid, shot.id) == before + 1
|
||
|
||
|
||
def test_qc_record_stamps_which_artifact_it_judged(api, db, studio, make_image_png):
|
||
"""质检结论要写清「查的是哪一版画面」,否则复用无从判断。"""
|
||
pid = studio["project_id"]
|
||
api.post(f"/api/projects/{pid}/import",
|
||
data={"script_text": "1. 紫小慧求助。\n2. 俊杰安抚。", "default_duration": 2},
|
||
files=[("images", ("s01.png", make_image_png(480, 270, "0x226699"), "image/png")),
|
||
("images", ("s02.png", make_image_png(480, 270, "0x993355"), "image/png"))])
|
||
api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": False})
|
||
api.drain(pid)
|
||
db.expire_all()
|
||
for shot in _shots(db, pid):
|
||
assert shot.qc_json["artifact_path"] == shot.output_path
|
||
assert shot.qc_json["artifact_checksum"] == shot.output_checksum
|
||
assert agent.qc_verdict_is_current(shot) is True
|
||
|
||
|
||
def test_verdict_is_current_falls_back_to_the_file_path():
|
||
"""老数据里没有产物指纹:退化成按画面文件路径判断(重出画面必然换文件名)。"""
|
||
old = Shot(id=1, project_id=1, order_index=1, code="s01",
|
||
output_path="9/generated/shot-01-task464.mp4", output_checksum="abc",
|
||
qc_json={"passed": True, "qc_ruleset": quality.QC_RULESET,
|
||
"probe": {"path": "/abs/media/9/generated/shot-01-task464.mp4"}})
|
||
assert agent.qc_verdict_is_current(old) is True
|
||
old.output_path = "9/generated/shot-01-task480.mp4"
|
||
assert agent.qc_verdict_is_current(old) is False
|
||
|
||
stamped = Shot(id=2, project_id=1, order_index=1, code="s02",
|
||
output_path="9/generated/x.mp4", output_checksum="new",
|
||
qc_json={"passed": True, "qc_ruleset": quality.QC_RULESET,
|
||
"artifact_checksum": "old"})
|
||
assert agent.qc_verdict_is_current(stamped) is False
|
||
stamped.output_checksum = "old"
|
||
assert agent.qc_verdict_is_current(stamped) is True
|
||
stamped.qc_json["passed"] = False
|
||
assert agent.qc_verdict_is_current(stamped) is False
|
||
|
||
|
||
def test_stale_ruleset_invalidates_a_reused_verdict():
|
||
"""画面没换、但**质检规则升级了**,旧结论是旧尺子量的,必须重新查。
|
||
|
||
「台词听写」这项上线时,上一轮已判 passed 的镜头全被当成仍然有效而跳过,
|
||
带着起手式的画面直接进了成片(project 12 shot39 实测)。
|
||
"""
|
||
fresh = Shot(id=3, project_id=1, order_index=1, code="s03",
|
||
output_path="9/generated/y.mp4", output_checksum="abc",
|
||
qc_json={"passed": True, "artifact_checksum": "abc",
|
||
"qc_ruleset": quality.QC_RULESET})
|
||
assert agent.qc_verdict_is_current(fresh) is True
|
||
fresh.qc_json["qc_ruleset"] = quality.QC_RULESET - 1 # 上一版规则集的结论
|
||
assert agent.qc_verdict_is_current(fresh) is False
|
||
del fresh.qc_json["qc_ruleset"] # 加字段之前的老数据
|
||
assert agent.qc_verdict_is_current(fresh) is False
|
||
|
||
|
||
# ------------------------------------------------------------- 过肩镜头多出的手
|
||
def _shot(**kw) -> Shot:
|
||
base = dict(id=1, project_id=1, order_index=1, code="s03", title="演示",
|
||
description="俊杰单手在键盘上轻敲,指向屏幕", dialogue="",
|
||
params_json={}, style_note="", duration=5)
|
||
base.update(kw)
|
||
return Shot(**base)
|
||
|
||
|
||
def _project() -> Project:
|
||
return Project(id=1, name="自检", style_guide="", aspect_ratio="9:16", fps=24)
|
||
|
||
|
||
def test_over_shoulder_prompt_pins_every_hand_to_a_body():
|
||
"""过肩机位让模型把主角的手留成前景,再补一只手 → 同一镜头三只手。"""
|
||
text = prompt.compose_prompt(_project(), _shot(camera_angle="over_shoulder",
|
||
camera_movement="push_in"),
|
||
characters=[], scenes=[])
|
||
assert "过肩镜头的前景肩膀与手臂必须属于画面中已出镜的人物之一" in text
|
||
assert "严禁出现第三只手" in text
|
||
|
||
|
||
def test_plain_camera_language_gets_no_extra_clause():
|
||
text = prompt.compose_prompt(_project(), _shot(camera_angle="eye_level",
|
||
camera_movement="static"),
|
||
characters=[], scenes=[])
|
||
assert "第三只手" not in text
|
||
assert prompt.hand_ownership_note(_shot(camera_angle="eye_level")) == ""
|
||
|
||
|
||
def test_descriptions_that_say_over_shoulder_are_enough():
|
||
"""镜头参数没填、但画面描述写了过肩,同样要带上约束。"""
|
||
shot = _shot(camera_angle="eye_level", description="过肩镜头拍到屏幕前的两人")
|
||
assert "第三只手" in prompt.compose_prompt(_project(), shot, characters=[], scenes=[])
|
||
|
||
|
||
def test_qc_discipline_says_two_hands_are_normal():
|
||
"""质检也得会区分:一人两手是正常姿态,多出来的那只才算畸变。"""
|
||
text = visual_qc.build_prompt(_shot(camera_angle="over_shoulder"),
|
||
[{"id": 1, "name": "俊杰", "gender": "男",
|
||
"appearance": "深蓝西装", "speaker": True}], 4, [])
|
||
assert "一只手必须有主人" in text
|
||
assert "第三只手" in text
|