Files
MeltMind/tools/prepare_phase3_followup_recovery.py
T

70 lines
3.3 KiB
Python

"""Build the two independently reviewed, no-launch idle-field recovery bindings."""
import ast
from pathlib import Path
import sys
from check_phase3_followup_recovery_guard import run as check_guard
from phase2_h1_worker_v2 import save, sha
from phase2_home_freeze_io import freeze, verify
from phase3_followup_postfit_common import budget, read, root_for
from phase3_followup_postfit_recovery_common import accounting, config, contract_delta, recovery_root
from track_a_h2_prepare import local_code_closure
TOOLS = Path(__file__).resolve().parent
def scorer_identity():
old_path = TOOLS/'phase3_followup_postfit_worker.py'
new_path = TOOLS/'phase3_followup_postfit_recovery_worker.py'
old = ast.parse(old_path.read_text())
new = ast.parse(new_path.read_text())
def functions(module):
return {n.name:ast.dump(n,include_attributes=False) for n in module.body if isinstance(n,ast.FunctionDef)}
if functions(old)!=functions(new):
raise ValueError('Worker function bodies must be identical to the reviewed entry')
return dict(original=sha(old_path), recovery=sha(new_path), function_bodies_identical=True)
def prepare(item, *, resume_metadata=False):
root = recovery_root(item)
if root.exists() and not resume_metadata:
raise FileExistsError('Inspect existing recovery binding')
original = root_for(item)
old = read(original/'contract.json')
model_budget = accounting(item)
paths = [*TOOLS.glob('phase3_followup_postfit_recovery*.py'),
TOOLS/'check_phase3_followup_recovery_guard.py',Path(__file__)]
codes=local_code_closure([*paths,*map(Path,old['code_sha256'])])
for p,d in old['code_sha256'].items():
if codes[p]!=d:
raise ValueError('Original reviewed code changed')
c=dict(old,idle_wait_seconds=20,code_sha256=codes)
contract_delta(old,c)
budget(c,model_budget['prior_gpu_seconds'],model_budget['prior_cpu_seconds']+180,5)
contract=root/'contract.json'
ledger=root/'recovery-accounting.json'
if resume_metadata:
if ({p.name for p in root.iterdir()}!={'contract.json','recovery-accounting.json'}
or read(ledger)!=model_budget or read(contract)!=dict(c,code_sha256=read(contract)['code_sha256'])):
raise ValueError('Only the original unfrozen metadata-only attempt can resume')
else:
root.mkdir()
save(contract,c)
save(ledger,model_budget)
test=root/'guard-contract-synthetic.json'
save(test,dict(**check_guard(contract),contract_sha256=sha(contract),scorer_identity=scorer_identity()))
proof_file=root/'implementation-git-freeze.json'
freeze(root,'implementation',[contract,ledger,test,*map(Path,codes)])
proof=verify(root,'implementation')
binding=dict(reviewed_commit=proof['commit'],contract_sha256=sha(contract),
implementation_proof_sha256=sha(proof_file),
comparison_contract_sha256=old['comparison_contract_sha256'],code_sha256=codes)
save(root/'expected-review-binding.json',binding)
config(root,review=False)
print(item, root, {k:v for k,v in binding.items() if k!='code_sha256'},
'binding',sha(root/'expected-review-binding.json'),'synthetic',sha(test),'codes',len(codes))
if __name__=='__main__':
prepare(sys.argv[1], resume_metadata=sys.argv[2:]==['--resume-metadata'])