"""每个Qwen分组重新准入;已开始响应完成后才阻止下一次模型请求。""" import asyncio import json import pytest from app.adapters.provider.client import Provider from app.core.config import Settings from app.domain.execution import current_phase_callback from app.services.orchestration.coordinator import PhaseDeferred @pytest.mark.parametrize( "task_type,phase_key", [ ("text.story.plan", "story_plan"), ("text.script.generate", "script"), ], ) @pytest.mark.parametrize("close_during_response", [True, False]) async def test_each_qwen_group_rechecks_window_after_current_response_finishes( tmp_path, task_type, phase_key, close_during_response ): settings = Settings( mode="test", database_url="sqlite+aiosqlite:///:memory:", workspace_root=tmp_path, inference_api_key_file=None, ) provider = Provider(settings) work = settings.job_work_root / "grouped-qwen" work.mkdir(parents=True) shot_ids = [f"{index:032x}" for index in range(1, 10)] shots = [ { "shot_id": shot_id, "name": "海边", "visual_prompt": "旅人站在沙滩上", "motion_prompt": "朝海边走", "duration_frames": 120, } for shot_id in shot_ids ] if task_type == "text.story.plan": inputs = { "spec": { "schema_version": 4, "scenes": [{"id": "c" * 32, "name": "海边"}], "shots": [ { "id": shot["shot_id"], "scene_id": "c" * 32, "visual_prompt": shot["visual_prompt"], "motion_prompt": shot["motion_prompt"], "continuity": {"group_id": "g"}, } for shot in shots ], } } else: inputs = {"shots": shots} response_started, release_response = asyncio.Event(), asyncio.Event() window_open = True admitted = [] requested_groups, completed_groups = [], [] async def admission(phase, pool): assert (phase, pool) == (phase_key, "qwen_http") if not window_open: raise PhaseDeferred() admitted.append(phase) async def chat(messages, *, max_tokens, thinking): assert admitted assert max_tokens == 4096 and thinking is False group = json.loads(messages[-1]["content"])["shots"] ids = [row["id" if task_type == "text.story.plan" else "shot_id"] for row in group] requested_groups.append(ids) if len(requested_groups) == 1: response_started.set() await release_response.wait() completed_groups.append(ids) if task_type == "text.story.plan": return json.dumps( { "shots": [ { "shot_id": shot_id, "state_in": {"summary": "旅人走向海边"}, "state_out": {"summary": "旅人到达海边"}, "end_prompt": "旅人面朝大海", } for shot_id in ids ] } ) return json.dumps( {"segments": [{"shot_id": shot_id, "text": "旅人走向海边。"} for shot_id in ids]} ) provider._chat = chat token = current_phase_callback.set(admission) operation = asyncio.create_task(provider.run_cpu(task_type, inputs, {}, {}, work)) try: await asyncio.wait_for(response_started.wait(), timeout=2) window_open = not close_during_response await asyncio.sleep(0) assert not operation.done() assert completed_groups == [] release_response.set() if close_during_response: with pytest.raises(PhaseDeferred): await operation assert requested_groups == [shot_ids[:8]] assert completed_groups == [shot_ids[:8]] assert list(work.iterdir()) == [] else: result = await operation assert requested_groups == [shot_ids[:8], shot_ids[8:]] assert completed_groups == requested_groups if task_type == "text.story.plan": assert [shot["id"] for shot in result.result["spec"]["shots"]] == shot_ids else: assert [segment["shot_id"] for segment in result.result["segments"]] == shot_ids assert json.loads(result.outputs[0].read_text(encoding="utf-8")) == result.result finally: release_response.set() if not operation.done(): operation.cancel() await asyncio.gather(operation, return_exceptions=True) current_phase_callback.reset(token) await provider.aclose()