Files
MeltMind/tools/test_phase2_gate_continued_postfit.py
T

75 lines
6.0 KiB
Python

import ast,copy,inspect,json,tempfile,unittest
from pathlib import Path
import numpy as np
from unittest.mock import patch
import phase2_gate_continued_postfit_common as m
import phase2_gate_continued_postfit as runner
from test_phase2_gate_outcome_postfit import Statistics,SemanticReview
class CanonicalInputs(unittest.TestCase):
def fixture(self):
cont=dict(new_indices=list(range(333,493)),original_complete_sha256='old-hash',prior_cpu_seconds=66936.020178167,setup_seconds=600,total_cpu_seconds=m.TOTAL,geometry_cpu_cap=m.TOTAL-3600)
old=dict(cumulative_cpu_seconds=cont['prior_cpu_seconds']);new=[dict(index=i,guard=dict(status='PASS',returncode=0,cpu_seconds=10),guard_cpu_seconds=1) for i in cont['new_indices']]
done=dict(status='CANONICAL_FEATURES_COMPLETE_POSTFIT_REVIEW_PENDING',accounting_complete=True,original_reused_terminal=333,canonical_terminal=493,new_records=new,original_complete_sha256='old-hash',prior_cpu_once=cont['prior_cpu_seconds'],postfit_started=False,new_fits=0,new_predictions=0,new_v2_labels_read=0,cumulative_cpu_seconds=90000,total_cpu_seconds=m.TOTAL)
cost=dict(cumulative_cpu_seconds=90001,total_cpu_seconds=m.TOTAL,geometry_cpu_cap=m.TOTAL-3600,postfit_reserve=3600,within_cap=True)
return cont,old,done,cost
def test_actual_two_source_ledger_adopts_total_once(self):
c,o,d,k=self.fixture();self.assertEqual(m.ledger_identity(c,o,d,k),90001)
def test_old493_shape_or_partial160_rejected(self):
c,o,d,k=self.fixture();d['status']='FEATURES_COMPLETE_PREDICTIONS_AND_COMPARISON_PENDING'
with self.assertRaises(PermissionError):m.ledger_identity(c,o,d,k)
c,o,d,k=self.fixture();d['new_records'].pop()
with self.assertRaises(ValueError):m.ledger_identity(c,o,d,k)
def test_duplicate_failed_or_replayed_old_job_rejected(self):
for mode in ['duplicate','failed','old']:
c,o,d,k=self.fixture()
if mode=='duplicate':d['new_records'][-1]=copy.deepcopy(d['new_records'][0])
elif mode=='failed':d['new_records'][0]['guard']['status']='FAILED'
else:d['new_records'][0]['index']=332
with self.assertRaises(ValueError):m.ledger_identity(c,o,d,k)
def test_double_prior_missing_guard_cost_and_no_postfit_reserve(self):
for mode in ['prior','under','reserve','nonfinite']:
c,o,d,k=self.fixture()
if mode=='prior':d['prior_cpu_once']*=2
elif mode=='under':d['cumulative_cpu_seconds']=c['prior_cpu_seconds']+600
elif mode=='reserve':k['cumulative_cpu_seconds']=m.TOTAL-3599
else:k['cumulative_cpu_seconds']=float('nan')
with self.assertRaises(ValueError):m.ledger_identity(c,o,d,k)
def test_remaining_jobs_reserved_under_both_caps(self):
c=dict(total_cpu_seconds=m.TOTAL);m.require_budget(c,m.TOTAL-3600,120,5)
with self.assertRaises(RuntimeError):m.require_budget(c,m.TOTAL-1500,120,5)
with self.assertRaises(RuntimeError):m.require_budget(c,1000,3000,0)
m.require_budget(c,m.TOTAL-3600,3400,0,False)
def test_live_missing_features_cannot_be_adopted(self):
with tempfile.TemporaryDirectory() as t:
with self.assertRaises(FileNotFoundError):m.terminal_inputs(Path(t),dict(preparation_root=t,original_preparation_root=t),{})
def manifests(self):
original=Path('/old');new=Path('/new');parts=[dict(row=dict(case=str(i))) for i in range(493)]
entries=[dict(index=i,case=str(i),path=str((original if i<333 else new)/'parts'/f'{i:04d}'),result_sha256=str(i),guard_result_sha256=str(i),output_sha256={}) for i in range(493)]
reused=copy.deepcopy(entries[:333])
for e in entries:e['origin']='ORIGINAL_TERMINAL_HASH_REUSE' if e['index']<333 else 'NEVER_ATTEMPTED_CONTINUATION'
prep=dict(parts=parts,exact_reuse=[dict(case='exact'+str(i)) for i in range(52)],invalid_cases=['invalid'+str(i) for i in range(7)])
manifest=dict(recomputed=entries,exact_reuse=prep['exact_reuse'],invalid_cases=prep['invalid_cases'],parts_total=552,original333_reused=True,new160_completed=True,original_stop_preserved=True,new_labels_read=False,new_fits=0,new_predictions=0)
return manifest,reused,prep,original,new
def test_manifest333_reused160_new_and52_7_unchanged(self):
args=self.manifests();self.assertEqual(len(m.manifest_identity(*args)),493)
def test_reused_hash_path_origin_and_invalid_mutations_rejected(self):
for mode in ['hash','path','origin','invalid']:
a=self.manifests();e=a[0]['recomputed']
if mode=='hash':e[0]['result_sha256']='changed'
elif mode=='path':e[333]['path']='/old/parts/0333'
elif mode=='origin':e[333]['origin']='ORIGINAL_TERMINAL_HASH_REUSE'
else:a[0]['invalid_cases']=[]
with self.assertRaises(ValueError):m.manifest_identity(*a)
def test_prediction_reuse_flags_cannot_skip_new_targets(self):
old={str(i):dict(case=str(i),fold=i%5,component_id='g',path='/baseline/'+str(i),sha256=str(i)) for i in range(545)};a=dict(exact_reuse=[dict(case=str(i),prediction=old[str(i)]) for i in range(52)],recomputed=[dict(case=str(i)) for i in range(52,545)])
entries=[dict(case=str(i),fold=i%5,component_id='g',original=old[str(i)],path=old[str(i)]['path'] if i<52 else '/postfit/predict/'+str(i%5)+'/'+str(i)+'.npz',sha256=str(i),exact_reuse=i<52) for i in range(545)];c=dict(postfit_root='/postfit')
runner.prediction_identity(entries,c,old,a);entries[52]['exact_reuse']=True
with self.assertRaises(ValueError):runner.prediction_identity(entries,c,old,a)
def test_new_pass_precedes_model_and_target_reads(self):
predict=inspect.getsource(runner.predict);score=inspect.getsource(runner.score);run=inspect.getsource(runner.run)
self.assertLess(predict.index('validate_review(root)'),predict.index('joblib.load'));self.assertLess(score.index('validate_review(root)'),score.index("read(checked(c['original_score']"));self.assertLess(score.index('validate_review(root)'),score.index("cached(row,'seconds')"));self.assertLess(run.index('terminal_inputs(root,c,prep)'),run.index("root/'started.json'"))
tree=ast.parse(score);branches=[n for n in ast.walk(tree) if isinstance(n,ast.If) and ast.unparse(n.test)=="e['exact_reuse']"]
self.assertEqual(len(branches),1);self.assertNotIn('cached(',ast.unparse(ast.Module(body=branches[0].body,type_ignores=[])))
if __name__=='__main__':unittest.main()