"""Pass actual recovery contracts through the actual guarded dispatch with mock hardware.""" import ast import resource from pathlib import Path from tempfile import TemporaryDirectory from unittest.mock import patch from phase2_h1_worker_v2 import save from phase3_followup_postfit_recovery_common import read from phase3_followup_postfit_recovery_guard import run_prediction GUARD = Path(__file__).with_name('phase3_followup_postfit_recovery_guard.py') MODULE = 'phase3_followup_postfit_recovery_guard' def run(contract_path): c = read(contract_path) guard_fields = sorted({n.slice.value for n in ast.walk(ast.parse(GUARD.read_text())) if isinstance(n,ast.Subscript) and isinstance(n.value,ast.Name) and n.value.id=='c' and isinstance(n.slice,ast.Constant)}) if set(guard_fields)!= {'jobs','gpu_uuid','idle_wait_seconds'} or set(guard_fields)-set(c): raise ValueError('Actual guard read fields differ from the covered contract fields') if c['idle_wait_seconds']!=20: raise ValueError('Must use the fixed 20-second idle wait') with TemporaryDirectory(prefix='meltmind-guard-synthetic-') as dirname: temp = Path(dirname) passed = [] for fold,job in enumerate(c['jobs']): if job['id']!=fold or type(job['expected_parts']) is not int or job['expected_parts']<1: raise ValueError('Actual nested guard fields missing or invalid') jr = temp/'jobs'/str(fold) jr.mkdir(parents=True) def fake_worker(command, path, mode, limits, *, poll, gpu_uuid): if (Path(command[1]).name!='phase3_followup_postfit_recovery_worker.py' or command[2:]!=['predict',str(temp),str(fold)] or path!=jr/'prediction-guard' or mode!='predict' or gpu_uuid!=c['gpu_uuid'] or limits['job_cpu_seconds']!=240 or not 0