from copy import deepcopy import pytest from sqlalchemy import select from app.models import Job,Asset from app.v2_models import Execution,AssetReference from app.coordinator import Coordinator async def queued_job(system): response=await system.client.post("/api/jobs",json={"task_type":"image.generate","input":{"prompt":"x"}}) job_id=response.json()["jobs"][0]["id"] await system.app.state.coordinator.run_once(force=True) async with system.app.state.db.session() as session: job=await session.get(Job,job_id) return job_id,job.upstream_id @pytest.mark.asyncio async def test_gpu_attempt_observation_preempt_restart_stale_counter_and_publication(system): job_id,upstream=await queued_job(system) c=system.app.state.coordinator running={"id":upstream,"execution_id":upstream+":1","attempt_count":1,"state":"running","stage":"executing","runner_pid":77} await c._apply_remote(job_id,running) async with system.app.state.db.session() as session: first=(await session.scalars(select(Execution).where(Execution.job_id==job_id))).one() assert first.state=="running" and first.manifest_json["upstream_attempt"]==1 first_id=first.id # A queued state without a cleanup ACK cannot claim the runner is gone. await c._apply_remote(job_id,{**running,"state":"queued","stage":"queued"}) async with system.app.state.db.session() as session:assert (await session.get(Execution,first_id)).state=="running" await c._apply_remote(job_id,{**running,"state":"queued","stage":"queued","runner_pid":None,"preemptions":1}) async with system.app.state.db.session() as session:assert (await session.get(Execution,first_id)).state=="exited" # Same closed attempt cannot be revived by a late running callback. await c._apply_remote(job_id,running) async with system.app.state.db.session() as session:assert (await session.get(Job,job_id)).state=="queued" running2={**running,"execution_id":upstream+":2","attempt_count":2,"runner_pid":88} await c._apply_remote(job_id,running2) restarted=Coordinator(system.settings,system.app.state.db,system.provider) await restarted._apply_remote(job_id,running2) stale={**running,"state":"succeeded","result":{"outputs":[{"path":"old-attempt.png"}]}} await restarted._apply_remote(job_id,stale) assert system.provider.download_count==0 success={**running2,"state":"succeeded","result":{"outputs":[{"path":"attempt-2/image.png"}],"metadata":{}}} await restarted._apply_remote(job_id,success) async with system.app.state.db.session() as session: job=await session.get(Job,job_id) rows=list((await session.scalars(select(Execution).where(Execution.job_id==job_id).order_by(Execution.ordinal))).all()) assert len(rows)==2 and [row.state for row in rows]==["exited","results_ready"] assert job.state=="succeeded" and job.current_execution_id==rows[1].id assert rows[1].manifest_json["upstream_execution_id"]==upstream+":2" asset=(await session.scalars(select(Asset).where(Asset.source_job_id==job_id))).one() assert asset.ready binding=await session.scalar(select(AssetReference).where(AssetReference.asset_id==asset.id,AssetReference.ref_kind=="execution")) assert binding.ref_id==rows[1].id @pytest.mark.asyncio async def test_unlaunched_deferred_claim_does_not_invent_execution(system): job_id,upstream=await queued_job(system) c=system.app.state.coordinator claim={"id":upstream,"execution_id":upstream+":1","attempt_count":1,"state":"running","stage":"launching","runner_pid":None,"started_at":123} await c._apply_remote(job_id,claim) await c._apply_remote(job_id,{**claim,"state":"queued","stage":"batch_window_closed","started_at":None}) await c._apply_remote(job_id,{**claim,"state":"cancelled","stage":"cancelled","started_at":None}) async with system.app.state.db.session() as session: assert not (await session.scalars(select(Execution).where(Execution.job_id==job_id))).all() @pytest.mark.asyncio async def test_gpu_collection_rechecks_current_fence_before_asset_commit(system,monkeypatch): from app.executions import begin_execution job_id,upstream=await queued_job(system) c=system.app.state.coordinator download=system.provider.download async def late_download(*args): await download(*args) async with system.app.state.db.session() as session: await begin_execution(session,job_id,"gpu:2","new_gpu") await session.commit() monkeypatch.setattr(system.provider,"download",late_download) remote={"id":upstream,"execution_id":upstream+":1","attempt_count":1,"state":"succeeded","result":{"outputs":[{"path":"attempt-1.png"}]}} with pytest.raises(ValueError,match="Stale GPU result import"): await c._apply_remote(job_id,remote) async with system.app.state.db.session() as session: assert not (await session.scalars(select(Asset).where(Asset.source_job_id==job_id))).all() assert (await session.get(Job,job_id)).state!="succeeded" @pytest.mark.asyncio async def test_gpu_terminal_observation_does_not_fabricate_missed_attempts(system): job_id,upstream=await queued_job(system) await system.app.state.coordinator._apply_remote(job_id,{"id":upstream,"execution_id":upstream+":3","attempt_count":3,"state":"failed"}) async with system.app.state.db.session() as session: rows=list((await session.scalars(select(Execution).where(Execution.job_id==job_id))).all()) assert len(rows)==1 and rows[0].ordinal==1 and rows[0].manifest_json["upstream_attempt"]==3 assert rows[0].state=="exited" @pytest.mark.asyncio async def test_gpu_final_publication_rechecks_fence_after_import(system,monkeypatch): from app.executions import begin_execution job_id,upstream=await queued_job(system) c=system.app.state.coordinator complete=c._complete async def replaced_before_publish(job_id,result,**kwargs): async with system.app.state.db.session() as session: await begin_execution(session,job_id,"gpu:2","new_gpu");await session.commit() await complete(job_id,result,**kwargs) monkeypatch.setattr(c,"_complete",replaced_before_publish) await c._apply_remote(job_id,{"id":upstream,"execution_id":upstream+":1","attempt_count":1,"state":"succeeded","result":{"outputs":[{"path":"attempt-1.png"}]}}) async with system.app.state.db.session() as session: assets=list((await session.scalars(select(Asset).where(Asset.source_job_id==job_id))).all()) assert assets and all(not asset.ready for asset in assets) assert (await session.get(Job,job_id)).state!="succeeded" @pytest.mark.asyncio async def test_gpu_same_attempt_wrong_remote_fence_cannot_publish(system): job_id,upstream=await queued_job(system) c=system.app.state.coordinator running={"id":upstream,"execution_id":upstream+":1","attempt_count":1,"state":"running","stage":"executing","fence_token":"current"} await c._apply_remote(job_id,running) await c._apply_remote(job_id,{**running,"fence_token":"stale","state":"succeeded","result":{"outputs":[{"path":"wrong.png"}]}}) assert system.provider.download_count==0 async with system.app.state.db.session() as session:assert (await session.get(Job,job_id)).state=="running"