"""Synthetic CPU acceptance only; never opens real test targets or invokes CUDA.""" import ast,copy,inspect,json,tempfile,unittest from pathlib import Path import numpy as np from phase2_h1_worker_v2 import sha from phase2_h2_data import load_part from phase2_h2_s1_test_data import load_test_part from phase2_h2_model import edge_attributes from phase2_h2_s1_postfit import query_index,checkpoint_identity,predict,score,h2_execution_command from phase2_h2_s1_common import gate from phase2_residual_math import decode_seconds from phase2_b13_e1b import pooled from phase2_h2_s1_prediction_guard import final_reasons from s2_main_memory_guard import PeakRecords # The exact shared validator is exercised against real temporary Git files. from test_phase2_gate_outcome_postfit import SemanticReview class InputScope(unittest.TestCase): def setUp(self): self.tmp=tempfile.TemporaryDirectory();self.root=Path(self.tmp.name) raw=np.array([10,20,30,40]);mass=np.array([1.,1.,2.,2.]);points=np.array([[0.,0,0],[1,0,0],[0,2,0],[1,2,0]]) receiver=np.arange(4);sender=np.array([1,0,3,2]);_,rg=edge_attributes(points,mass,receiver,sender) geometry=self.root/'geometry.npz';np.savez(geometry,points=points,raw_ids=raw,mass=mass,indptr=np.arange(5),indices=sender,rg=rg) self.F=np.array([.1,.2,.3,.4],np.float64)+1e-10;features=np.arange(76,dtype=float).reshape(4,19) hashes={} for name,array in dict(raw_ids=raw,mass=mass,features=features,Vf=self.F).items(): p=self.root/(name+'.npy');np.save(p,array);hashes[p.name]=sha(p) self.c=dict(folds=[dict(test=['test'],learning=['learn'],validation=['val'])],parts={'test':dict(output=str(self.root),output_sha256=hashes,nodes=4,t_set=.4)},geometry={'test':dict(path=str(geometry),sha256=sha(geometry),directed_edges=4)}) self.stats=dict(mean=[1.]*19,scale=[2.]*19) def tearDown(self):self.tmp.cleanup() def test_test_loader_succeeds_with_no_target_files(self): d=load_test_part(self.c,'test',self.stats);self.assertEqual(d['physical_components'],2);self.assertEqual(d['nodes'],4);self.assertEqual(d['F'].dtype,np.float64);np.testing.assert_array_equal(d['F'],self.F);self.assertNotIn('target',d) np.testing.assert_array_equal(d['x'].numpy(),((np.arange(76).reshape(4,19)-1)/2).astype(np.float32)) def test_reject_learning_validation_unknown_before_file_access(self): for case in ['learn','val','unknown']: with self.assertRaises(PermissionError):load_test_part(self.c,case,self.stats) def test_original_preprocessing_ast_identical(self): old=ast.parse(inspect.getsource(load_part)).body[0].body;new=ast.parse(inspect.getsource(load_test_part)).body[0].body self.assertEqual([ast.dump(x) for x in old[2:-2]+old[-1:]],[ast.dump(x) for x in new[1:]]) def test_fp32_skip_rejected(self): p=self.root/'Vf.npy';np.save(p,self.F.astype(np.float32));self.c['parts']['test']['output_sha256'][p.name]=sha(p) with self.assertRaises(ValueError):load_test_part(self.c,'test',self.stats) def test_full_graph_not_query_subgraph(self): d=load_test_part(self.c,'test',self.stats);q=dict(raw_ids=np.array([20,40]),counts=np.array([3,1]),coordinates_over_Rg=np.zeros((2,3)),near_wall=np.array([True,False]));ix=query_index(d['raw_ids'],q,q) np.testing.assert_array_equal(ix,[1,3]);self.assertEqual(d['directed_edges'],4) class IdentitiesAndScience(unittest.TestCase): def test_query_requires_counts_coordinates_wall_and_raw_identity(self): raw=np.array([1,3,5]);q=dict(raw_ids=np.array([1,5]),counts=np.array([2,3]),coordinates_over_Rg=np.zeros((2,3)),near_wall=np.array([True,False])) for k in q: bad={a:b.copy() for a,b in q.items()};bad[k].flat[0]=not bad[k].flat[0] if k=='near_wall' else bad[k].flat[0]+1 with self.assertRaises(ValueError):query_index(raw,q,bad) with self.assertRaises(ValueError):query_index(raw,dict(q,raw_ids=np.array([1,9])),q) with self.assertRaises(ValueError):query_index(raw[::-1],q,q) def test_final_checkpoint_identity(self): c=dict(training_contract='/train',source_sha256={'/train':'fixed'});blob=dict(completed_epochs=200,optimizer_steps=64000,seed=0,fold=0,role='last_epoch_only',contract_sha256='fixed');checkpoint_identity(blob,c) for k,v in dict(completed_epochs=199,optimizer_steps=63999,seed=1,fold=1,role='best',contract_sha256='other').items(): with self.assertRaises(ValueError):checkpoint_identity(dict(blob,**{k:v}),c) def test_fp64_clock_and_no_negative_clipping(self): f=np.array([.123456789123,.75],np.float64);r=np.array([-1.,.1],np.float32);actual=decode_seconds(r,f,1.026602777749673,.4) np.testing.assert_array_equal(actual,(f*1.026602777749673+r.astype(np.float64))*.4);self.assertLess(actual[0],0) def test_original_strict_or_gate(self): for f,l,expected in [(.005,0,False),(.004999,None,True),(None,-1e-8,True),(.1,-.001,True),(float('nan'),0,False),(None,None,False),(.006,.001,False)]:self.assertEqual(gate(f,l),expected) def test_original_whole_component_and_equal_component_pooling(self): def row(group,new,old):return dict(component_id=group,**{arm:{k:dict(value=v) for k in ['relative_l2','last_location','kendall_tau_b']} for arm,v in [('new',new),('A',old)]}) d=pooled([row('x',2,1),row('x',2,1),row('y',1,1)])['relative_l2'];self.assertEqual(d['new_minus_A']['value'],.5) d=pooled([row('x',2,1),row('x',None,1),row('y',1,1)])['relative_l2'];self.assertEqual(d['components'],dict(new=1,A=2));self.assertEqual(d['new_minus_A']['value'],0) def test_review_precedes_target_reads_in_entry(self): source=inspect.getsource(score);self.assertLess(source.index('validate_review(root)'),source.index("c['A_score']"));self.assertLess(source.index('validate_review(root)'),source.index("cached(row,'seconds')")) for f in [load_test_part,predict]: tree=ast.parse(inspect.getsource(f));calls=[n for n in ast.walk(tree) if isinstance(n,ast.Call) and isinstance(n.func,ast.Name) and n.func.id=='cached'] self.assertFalse(any(isinstance(n.args[-1],ast.Constant) and n.args[-1].value in ['target','seconds'] for n in calls)) class FailureAccounting(unittest.TestCase): def test_process_identity_excludes_preparer_and_tests(self): for script in ['prepare_phase2_h2_s1_postfit.py','test_phase2_h2_s1_postfit.py']: self.assertFalse(h2_execution_command(['python','tools/'+script])) for script in ['phase2_h2_s1_postfit.py','phase2_h2_s1_worker.py','phase2_h2_s1_guard.py']: self.assertTrue(h2_execution_command(['python','tools/'+script])) def test_failed_worker_peak_still_fails_after_result_peak_regresses(self): with tempfile.TemporaryDirectory() as p: root=Path(p);(root/'failure.json').write_text(json.dumps(dict(peak_allocated_bytes=10,peak_reserved_bytes=16*2**30))) t=PeakRecords();self.assertTrue(t.scan(root));(root/'failure.json').unlink();(root/'result.json').write_text(json.dumps(dict(peak_allocated_bytes=10,peak_reserved_bytes=10)));t.scan(root) reasons=final_reasons([],{'status':'FAILED'},dict(status='COMPLETE',parts=121),t,100,100,1200);self.assertIn('WORKER_NOT_PASS',reasons);self.assertIn('RECORDED_VRAM_LIMIT',reasons) def test_missing_peaks_and_incomplete_coverage_fail(self): r=final_reasons([],{'status':'PASS'},dict(status='COMPLETE',parts=120),PeakRecords(),100,100,1200);self.assertIn('MISSING_PEAKS',r);self.assertIn('INCOMPLETE121',r) def test_final_gpu_and_cpu_caps_enforced(self): t=PeakRecords();t.witnesses=dict(peak_allocated_bytes={},peak_reserved_bytes={}) for gpu,cpu in [(600.01,1),(1,1200.01)]:self.assertIn('FINAL_RESOURCE_LIMIT',final_reasons([],{'status':'PASS'},dict(status='COMPLETE',parts=121),t,gpu,cpu,1200)) self.assertEqual(final_reasons([],{'status':'PASS'},dict(status='COMPLETE',parts=121),t,600,1200,1200),[]) if __name__=='__main__':unittest.main()