"""编导明确说「重新生成」时,已交付项目必须真的重跑,而不是回一句「已完成」。 背景:项目一旦有了成片,LLM 倾向于返回 outcome=complete + 空 steps, 界面全绿、零个任务、盘上新文件一个都没有(实测 agent_run #36)。 """ from __future__ import annotations import pytest from app.constants import AssetKind, TaskKind from app.models import Task from app.services import agent, storage COMPLETE_PLAN = { "intent": "complete", "outcome": "complete", "reply": "项目已交付成片,无需重复生成。", "summary": "已交付", "steps": [], "blockers": [], "warnings": [], } REDO_MESSAGE = ("用现在资产库里的定妆照和音色,重新生成三个镜头的画面与配音," "然后重新合成成片") @pytest.fixture def delivered(api, studio, script_text, make_image_png, install_llm, install_image): """先跑通一整条交付链路,返回项目 id 与第一版的产物路径。""" install_llm({ "intent": "full_delivery", "outcome": "execute", "reply": "完整交付。", "summary": "生成并导出", "steps": [ {"action": "generate_shots", "reason": "出片", "shot_ids": None}, {"action": "quality_check", "reason": "质检", "shot_ids": None}, {"action": "approve_passed", "reason": "授权", "shot_ids": None}, {"action": "build_timeline", "reason": "排布", "shot_ids": None}, {"action": "compose", "reason": "导出", "shot_ids": None}, ], "blockers": [], "warnings": [], }) pid = studio["project_id"] api.post(f"/api/projects/{pid}/import", data={"script_text": script_text, "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"))]) first = api.post(f"/api/projects/{pid}/agent/plan", json={"message": "完整交付", "auto_approve": True})["run"] api.post(f"/api/projects/{pid}/agent/runs/{first['id']}/execute", json={"wait": True, "timeout_seconds": 600}) shots = api.get(f"/api/projects/{pid}/shots")["shots"] exports = api.get(f"/api/projects/{pid}/exports")["exports"] assert exports and all(row["output_path"] for row in shots) return {"project_id": pid, "outputs": {row["id"]: row["output_path"] for row in shots}, "exports": [row["id"] for row in exports]} def test_redo_instruction_on_delivered_project_is_not_swallowed(api, delivered, install_llm, install_image): """模型回「已完成」也必须被重做指令兜住:计划里有步骤,执行后有新的镜头与成片。""" install_llm(COMPLETE_PLAN) install_image() pid = delivered["project_id"] run = api.post(f"/api/projects/{pid}/agent/plan", json={"message": REDO_MESSAGE, "auto_approve": True})["run"] assert run["status"] == "planned", run actions = [step["action"] for step in run["steps"]] assert actions == ["generate_keyframes", "generate_shots", "quality_check", "approve_passed", "build_timeline", "compose"], actions assert all(step.get("regenerate", {}).get("video") for step in run["steps"]) assert any("强制排定" in note for note in run["warnings"]), run["warnings"] executed = api.post(f"/api/projects/{pid}/agent/runs/{run['id']}/execute", json={"wait": True, "timeout_seconds": 900})["run"] assert executed["status"] == "succeeded", executed["error_message"] by_action = {step["action"]: step for step in executed["steps"]} assert by_action["generate_shots"]["status"] == "succeeded" assert by_action["generate_shots"]["result"]["created"], "重做必须重新入队视频任务" assert by_action["build_timeline"]["status"] == "succeeded" assert by_action["compose"]["result"].get("export_id") shots = api.get(f"/api/projects/{pid}/shots")["shots"] assert {row["id"]: row["output_path"] for row in shots} != delivered["outputs"], \ "镜头产物仍是旧文件,说明重做没有真正出片" exports = api.get(f"/api/projects/{pid}/exports")["exports"] assert len(exports) == len(delivered["exports"]) + 1 assert api.get(f"/api/projects/{pid}")["summary"]["status"] == "delivered" def test_voice_only_redo_requeues_voiceover_without_rerendering_video(api, delivered, install_llm, install_image, db): """只说「重新配音」时不该顺手烧一遍视频,但配音任务必须重新排队。""" install_llm(COMPLETE_PLAN) install_image() pid = delivered["project_id"] before = {row["id"]: row["output_path"] for row in api.get(f"/api/projects/{pid}/shots")["shots"]} video_tasks = db.query(Task).filter(Task.project_id == pid, Task.kind == TaskKind.GENERATE_SHOT).count() voice_tasks = db.query(Task).filter(Task.project_id == pid, Task.kind == TaskKind.VOICEOVER).count() run = api.post(f"/api/projects/{pid}/agent/plan", json={"message": "音色换过了,重新配一下音就行", "auto_approve": True})["run"] assert run["status"] == "planned", run assert [step["action"] for step in run["steps"]] == ["generate_shots", "build_timeline", "compose"] executed = api.post(f"/api/projects/{pid}/agent/runs/{run['id']}/execute", json={"wait": True, "timeout_seconds": 900})["run"] assert executed["status"] == "succeeded", executed["error_message"] db.expire_all() assert db.query(Task).filter(Task.project_id == pid, Task.kind == TaskKind.GENERATE_SHOT).count() == video_tasks, \ "只重做配音时不应重新渲染视频" assert db.query(Task).filter(Task.project_id == pid, Task.kind == TaskKind.VOICEOVER).count() > voice_tasks after = {row["id"]: row["output_path"] for row in api.get(f"/api/projects/{pid}/shots")["shots"]} assert after == before assert len(api.get(f"/api/projects/{pid}/exports")["exports"]) == len(delivered["exports"]) + 1 def test_uploaded_first_frame_is_never_overwritten_by_redo(db, delivered, install_image): """编导自己上传的首帧是原始素材:重做只允许覆盖 AI 生成的关键帧。""" from app.models import Shot pid = delivered["project_id"] install_image() shot = db.query(Shot).filter(Shot.project_id == pid).order_by(Shot.id).first() uploaded = storage.first_asset(db, "shot", shot.id, AssetKind.KEYFRAME) assert uploaded is not None and not (uploaded.metadata_json or {}).get("generated") assert agent._shot_needs_keyframe(db, shot, redo=True) is False uploaded.path = "9/keyframe/uploaded-by-author.png" db.commit() assert agent._shot_needs_keyframe(db, shot, redo=False) is True @pytest.mark.parametrize("message,expected", [ (REDO_MESSAGE, {"keyframes", "video", "voice", "export"}), ("重新跑一遍", {"video", "voice", "export"}), ("音色换过了,重新配一下音就行", {"voice", "export"}), ("把成片重新合成一次", {"export"}), ("检查是否完成", set()), ("项目已交付,无需重复生成", set()), ("把镜头重新排个序", set()), ("重新审核后交付", set()), ("按现在的分镜继续", set()), ("regenerate the film", {"video", "voice", "export"}), ]) def test_regeneration_scope_only_fires_on_real_redo_requests(message, expected): assert set(agent.regeneration_scope(message)) == expected