"""Claude1794900929: scoring-only marked amendment, unchanged score functions.""" import argparse import fcntl import json import os from pathlib import Path import resource import shutil import sys import time import psutil from phase2_h1_execution import Runner,task,read,BASE from phase2_h1_worker_v2 import contract,checked,sha,save from phase2_fixed_io import freeze,verify OLD=BASE/'artifacts/phase2-h1-labelled-recovery-2026-11-17' ORIGINAL=BASE/'artifacts/phase2-h1-execution-2026-11-17' ROOT=BASE/'artifacts/phase2-h1-score-extension-2026-11-17' CPUS=[4,6,8,10,12,14,16,18] def scope(frozen,records): pairs=sorted({(e['protocol'],e['case']) for e in frozen['entries']}) scored={} for r in records: if not r['name'].startswith('score-'):continue key=(r['protocol'],r['case']) if key in scored:raise ValueError('Duplicate original score job') scored[key]=r complete=[p for p in pairs if p in scored and scored[p]['guard']['status']=='PASS'] unfinished=[p for p in pairs if p not in complete] causes={p:('NEVER_ATTEMPTED' if p not in scored else scored[p]['guard']['stop_reason']) for p in unfinished} if any(c not in ['NEVER_ATTEMPTED','GUARD_SIGNAL_15'] for c in causes.values()):raise ValueError('Unapproved failure cause in scoring continuation') return complete,unfinished,causes def engineering(root): e=read(root/'engineering-contract.json') if e['approval']!='1794900929.164719':raise PermissionError('No scoring-only amendment') for p,h in {**e['source_sha256'],**e['code_sha256']}.items():checked(BASE/p,h) return e def gate(root): engineering(root);verify(root,'scoring');c=contract(root);f=read(root/'predictions-freeze.json') if not f['all_prediction_attempts_terminal'] or not f['accounting_complete']:raise ValueError('Missing frozen canonical predictions') return c,f def worker(root,mode,protocol=None,case=None): e=engineering(root) import phase2_h1_recovery_postfit as scorer scorer.scoring_gate=gate # Only the local authorization hook changes; arithmetic source stays pinned. if mode=='part': if [protocol,case] not in e['unfinished']:raise PermissionError('Completed score cannot rerun') scorer.score_part(root,protocol,case) elif mode=='summary': timing=dict(calls=0,cpu_seconds=0.) original=scorer.paired_difference def timed(*args,**kwargs): start=time.process_time() try:return original(*args,**kwargs) finally:timing['calls']+=1;timing['cpu_seconds']+=time.process_time()-start scorer.paired_difference=timed scorer.summarize(root);save(root/'bootstrap-cpu.json',dict(**timing,method='process_time around original paired_difference, arguments/results unchanged')) else:raise ValueError('No fitting or predictions in this module') def prepare(acceptance): a=read(acceptance);assert a['status']=='PASS' for p,h in a['code_sha256'].items():checked(BASE/p,h) stopped=read(OLD/'complete.json');original=read(ORIGINAL/'complete.json');assert stopped['status']=='STOPPED' and stopped['accounting_complete'] prior_total=stopped['cumulative_total_cpu_seconds'];prior_post=original['charges']['postfit']+prior_total-original['total_cpu_charge'] frozen=read(OLD/'predictions-freeze.json');done,todo,causes=scope(frozen,stopped['records']) assert len(done)==1109 and len(todo)==29 ROOT.mkdir(exist_ok=False) for name in ['contract.json','predictions-freeze.json','geometry/manifest.json','models-terminal.json']: p=ROOT/name;p.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(OLD/name,p) reused=[] for protocol,case in done: source=OLD/'scores'/protocol/case;files={str(p.relative_to(BASE)):sha(p) for p in source.iterdir() if p.is_file()} assert (source/'core.json').exists() and (source/'topology.json').exists() dest=ROOT/'scores'/protocol/case;dest.parent.mkdir(parents=True,exist_ok=True);dest.symlink_to(source,target_is_directory=True) rec=next(r for r in stopped['records'] if r.get('protocol')==protocol and r.get('case')==case) for name in ['tasks/'+rec['name']+'.json','guards/'+rec['name']+'/guard-result.json']: p=ROOT/name;p.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(OLD/name,p) reused.append(dict(protocol=protocol,case=case,source=str(source),files=files)) save(ROOT/'reused-score-manifest.json',dict(entries=reused,completed_jobs=len(done),recompute=False)) sources={str(p.relative_to(BASE)):sha(p) for p in [OLD/'complete.json',OLD/'predictions-freeze.json',ORIGINAL/'complete.json',acceptance]} save(ROOT/'engineering-contract.json',dict(approval='1794900929.164719',version='B1-v2-scoring-only-budget-amendment',source_sha256=sources, code_sha256={p:sha(BASE/p) for p in ['tools/phase2_h1_score_extension.py','tools/phase2_h1_recovery_postfit.py','tools/phase2_h1_execution.py','tools/phase2_fixed_io.py']}, old_root=str(OLD),prior_total_cpu_seconds=prior_total,prior_postfit_cpu_seconds=prior_post,prior_postfit_worker_guard_column=22020.475399839972, old_cap=25200,new_postfit_cap=61200,increment=36000,total_cpu_cap=72000,setup_seconds=60,coordinator_reserve_seconds=600,summary_reserve_seconds=120, unfinished=[list(p) for p in todo],scope=[dict(protocol=p[0],case=p[1],cause=causes[p]) for p in todo],reuse_completed=len(done),worker_cpus=CPUS, per_job_cpu_seconds=300,parallel=8,refit=False,new_predictions=False,new_decodes=0,retry=False,old_failures_preserved=True)) print(json.dumps(dict(done=len(done),todo=len(todo),prior_postfit=prior_post,contract_sha256=sha(ROOT/'engineering-contract.json')))) class ExtensionRunner(Runner): def __init__(self,root,c,e): super().__init__(root,c);self.e=e;u=resource.getrusage(resource.RUSAGE_CHILDREN);self.child_start=u.ru_utime+u.ru_stime def new(self): u=resource.getrusage(resource.RUSAGE_CHILDREN) return 60+max(sum(self.charges.values()),u.ru_utime+u.ru_stime-self.child_start)+time.process_time()-self.start_cpu def total(self): new=self.new() if self.e['prior_postfit_cpu_seconds']+new>=self.e['new_postfit_cap']-120:self.stop=self.stop or 'EXTENDED_POSTFIT_CAP' return self.e['prior_total_cpu_seconds']+new def run(root): e=engineering(root);os.sched_setaffinity(0,[30,31]) for p in psutil.process_iter(['pid','cmdline']): if str(OLD) in (p.info['cmdline'] or []):raise RuntimeError('Old scoring still live') with (root/'run.lock').open('a') as lock: fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB) if (root/'started.json').exists():raise FileExistsError('Unique extension already attempted') c=contract(root);c['budget']['postfit']=e['new_postfit_cap']-e['prior_postfit_cpu_seconds']-e['coordinator_reserve_seconds'];runner=ExtensionRunner(root,c,e) save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),engineering_sha256=sha(root/'engineering-contract.json'))) try: for entry in read(root/'reused-score-manifest.json')['entries']: for p,h in entry['files'].items():checked(BASE/p,h) freeze(root,'scoring',[root/'contract.json',root/'predictions-freeze.json',root/'geometry/manifest.json',root/'reused-score-manifest.json',root/'engineering-contract.json',*[BASE/p for p in e['code_sha256']]]) tasks=[task(f'extension-score-{i:04d}',['tools/phase2_h1_score_extension.py','part',str(root),protocol,case],[CPUS[i%len(CPUS)]],300.,protocol=protocol,case=case) for i,(protocol,case) in enumerate(e['unfinished'])] # Summary's original task glob is score-*.json; new names continue original ID space. for i,t in enumerate(tasks):t['name']=f'score-{2000+i:04d}' runner.jobs('postfit',tasks,parallel=8) if runner.stop:raise RuntimeError('Extended scoring stopped') result=runner.jobs('postfit',[task('summary',['tools/phase2_h1_score_extension.py','summary',str(root)],[4],120.)]) if runner.stop or result[0]['guard']['status']!='PASS':raise RuntimeError('Extended summary failed') new=runner.new();post=e['prior_postfit_cpu_seconds']+new;total=runner.total() if post>e['new_postfit_cap'] or total>e['total_cpu_cap']:raise RuntimeError('Final cumulative cap') save(root/'complete.json',dict(status='COMPLETE',accounting_complete=runner.accounting_complete,new_cpu_seconds=new,cumulative_postfit_cpu_seconds=post,cumulative_total_cpu_seconds=total,records=runner.records,reused_completed=e['reuse_completed'],new_score_jobs=len(e['unfinished']),old_stop_preserved=True)) except BaseException as exc: save(root/'complete.json',dict(status='STOPPED',reason=type(exc).__name__+': '+str(exc),accounting_complete=runner.accounting_complete,new_cpu_seconds=runner.new(),cumulative_postfit_cpu_seconds=e['prior_postfit_cpu_seconds']+runner.new(),cumulative_total_cpu_seconds=runner.total(),records=runner.records,old_stop_preserved=True));raise if __name__=='__main__': p=argparse.ArgumentParser();p.add_argument('mode',choices=['prepare','run','part','summary']);p.add_argument('root',type=Path);p.add_argument('protocol',nargs='?');p.add_argument('case',nargs='?');a=p.parse_args() if a.mode=='prepare':prepare(a.root.resolve()) elif a.mode=='run':run(a.root) else:worker(a.root,a.mode,a.protocol,a.case)