import ast import json from pathlib import Path import sys import unittest from unittest.mock import patch from phase2_h1_recovery import failure_scope,FAILED from phase2_h1_recovery_gate import verified_gate,scoring_gate def function(path,name): source=Path(path).read_text();node=next(n for n in ast.parse(source).body if isinstance(n,ast.FunctionDef) and n.name==name) return ast.get_source_segment(source,node) class RecoveryTests(unittest.TestCase): def test_prediction_arithmetic_is_byte_identical(self): old=function('tools/phase2_h1_worker_v2.py','predict');new=function('tools/phase2_h1_recovery_worker.py','predict') token=' terminal = json.loads' self.assertEqual(old[old.index(token):],new[new.index(token):]) self.assertNotIn('subprocess.',new) def test_score_and_statistics_bodies_are_identical(self): for name in ['score_part','intervals','summarize']: self.assertEqual(function('tools/phase2_h1_postfit.py',name),function('tools/phase2_h1_recovery_postfit.py',name)) self.assertNotIn('subprocess.',function('tools/phase2_h1_recovery_postfit.py','scoring_gate')) def test_only_exact_five_failed_predictions(self): rows=[dict(job_index=i,guard=dict(status='FAILED' if i in FAILED else 'PASS',stop_reason='process_count_limit' if i in FAILED else None)) for i in range(20)] self.assertEqual(sorted(failure_scope(dict(attempts=rows))),FAILED) rows[2]['guard']['status']='FAILED' with self.assertRaises(ValueError):failure_scope(dict(attempts=rows)) def worker(root): with patch('subprocess.check_output',side_effect=AssertionError('Worker must not spawn Git')): verified_gate(root,'models');c,p=scoring_gate(root) assert p['all_prediction_attempts_terminal'] and c['main_review']=='synthetic' auth=root/'models-authorization.json';old=auth.read_bytes();j=json.loads(old);j['verified_git_commit']='tampered';auth.write_text(json.dumps(j)) try: try:verified_gate(root,'models') except PermissionError:pass else:raise AssertionError('Tampered coordinator authorization accepted') finally:auth.write_bytes(old) print('PASS actual one-process worker: coordinator Git freeze accepted; tampering rejected; no subprocess') if __name__=='__main__': if len(sys.argv)>1 and sys.argv[1]=='worker':worker(Path(sys.argv[2])) else:unittest.main()