85 lines
7.0 KiB
Python
85 lines
7.0 KiB
Python
"""Independent B17 score-only repair; original fit/predict/coordinator pins untouched."""
|
|
import argparse,json,os,resource,sys,time,fcntl
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from phase2_h1_worker_v2 import checked,sha,save
|
|
from phase2_home_freeze_io import verify,freeze
|
|
from phase2_fixed_io import BASE
|
|
from phase2_b17 import config as original_config,query_identity,decision
|
|
from phase2_b1213_features import cached
|
|
from phase2_b13_e1b import pooled
|
|
from s2_custody_statistics import part_metrics
|
|
from phase2_b16_review_gate import validate_review
|
|
from r4bc_guard import supervise
|
|
|
|
def read(p):return json.loads(Path(p).read_text())
|
|
def config(root):
|
|
c=read(root/'contract.json')
|
|
if c['total_cpu_seconds']!=10800 or c['new_fits']!=0 or c['new_predictions']!=0:raise PermissionError('Score-only scope')
|
|
for p,h in c['source_sha256'].items():checked(p,h)
|
|
for p,h in c['fix_source_sha256'].items():checked(p,h)
|
|
b=read(checked(root/'review-binding.json',c['binding_sha256']))
|
|
if b['fix_source_sha256']!=c['fix_source_sha256']:raise ValueError('Repair code identity')
|
|
old=Path(c['original_root']);prior=read(checked(old/'complete.json',c['original_terminal_sha256']))
|
|
if prior['cumulative_cpu_seconds']!=c['prior_cpu_seconds'] or len(prior['records'])!=131:raise ValueError('Original131 records required')
|
|
fits=[x for x in prior['records'] if x['mode'] in ['train','predict']]
|
|
if len(fits)!=10 or any(x['guard']['status']!='PASS' for x in fits):raise ValueError('Original ten fit/predict jobs not terminal PASS')
|
|
if {x['index'] for x in fits if x['mode']=='train'}!=set(range(5)) or {x['index'] for x in fits if x['mode']=='predict'}!=set(range(5)):raise ValueError('Five distinct fits/prediction jobs required')
|
|
verify(old,'channels');verify(old,'models')
|
|
if (old/'score').exists() or (old/'implementation-review-authorization.json').exists():raise PermissionError('Old scorer must remain unopened')
|
|
verify(root,'implementation-review');validate_review(root/'review.json',b)
|
|
verify(old,'predictions');checked(root/'predictions-manifest.json',c['predictions_manifest_sha256'])
|
|
return c,original_config(old)
|
|
|
|
def part_last_differences(rows):
|
|
values=[]
|
|
for row in rows:
|
|
a=row['new']['last_location']['value'];b=row['G']['last_location']['value']
|
|
if a is not None and b is not None:values.append(float(a)-float(b))
|
|
return values
|
|
|
|
def score(root,c):
|
|
verify(root,'predictions');verify(root,'implementation-review');validate_review(root/'review.json',read(root/'review-binding.json'));dest=root/'score';dest.mkdir(exist_ok=False);m=read(root/'predictions-manifest.json')['entries'];by={r['case']:r for r in c['parts']};G={e['case']:e for e in read(c['G_prediction_manifest'])['entries']};old={r['case']:r for r in read(c['G_score'])['rows']}
|
|
if len(m)!=552 or {e['case'] for e in m}!=set(by):raise ValueError('Complete552 prediction roster required')
|
|
rows=[];access=[]
|
|
for e in m:
|
|
case=e['case'];row=by[case];g=G[case];prior=old[case]
|
|
if e['fold']!=g['fold'] or prior['fold']!=g['fold'] or prior['component_id']!=row['component_id']:raise ValueError('Paired role identity')
|
|
access.append(dict(case=case,role='test',fold=e['fold'],target_sha256=row['v5_cache']['output_sha256']['seconds.npy']));save(dest/'target-access.json',access)
|
|
truth=cached(row,'seconds');raw=cached(row,'raw_ids')
|
|
with np.load(checked(g['path'],g['sha256']),allow_pickle=False) as z,np.load(checked(e['path'],e['sha256']),allow_pickle=False) as v:
|
|
query_identity(z,v);ix=np.searchsorted(raw,v['raw_ids'])
|
|
if np.any(ix>=len(raw)) or not np.array_equal(raw[ix],v['raw_ids']):raise ValueError('Score query IDs')
|
|
metric=part_metrics(v['pred'],truth[ix],v['counts'],v['near_wall'],v['raw_ids'],v['coordinates_over_Rg'],int(case.split('_')[0]))
|
|
rows.append(dict(case=case,component_id=row['component_id'],fold=e['fold'],new=metric,G=prior['new'],A=prior['A'],G_valid=prior['G_valid'],negative_predictions=int(np.sum(v['pred']<0)),true_max_ties=int(np.sum(truth[ix]==truth[ix].max())),candidate_max_ties=int(np.sum(v['pred']==v['pred'].max())),G_max_ties=int(np.sum(z['pred']==z['pred'].max()))))
|
|
save(dest/'progress.json',dict(parts=len(rows)))
|
|
comparisons={a:pooled([dict(r,A=r[a]) for r in rows]) for a in ['G']};result=decision(comparisons['G'])
|
|
differences=part_last_differences(rows)
|
|
save(dest/'result.json',dict(status='COMPLETE',decision=result,parts=552,components=204,new_minus=comparisons,primary='new minus frozen E1b G last-location mean<=-.010 and CI high<0; screening only',stage2_authorized=False,part_last_difference_median=float(np.median(differences)) if differences else None,rows=rows,original_G_metrics_reused=True,invalid_G_parts_retained=sum(not r['G_valid'] for r in rows),negative_predictions=sum(r['negative_predictions'] for r in rows),validation_used=False,new_decodes=0))
|
|
|
|
def worker(root):
|
|
c,science=config(root);score(root,science)
|
|
|
|
def run(root):
|
|
c,science=config(root);lock=(root/'run.lock').open('a');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
|
if (root/'started.json').exists():raise FileExistsError('Single repaired scorer attempt')
|
|
import psutil
|
|
for p in psutil.process_iter(['pid','cmdline']):
|
|
if any(x.endswith('phase2_b17.py') for x in (p.info['cmdline'] or [])):raise RuntimeError('Original coordinator must first reach labelled idle retirement')
|
|
verify(root,'launch');os.sched_setaffinity(0,[8]);start=time.process_time();u0=resource.getrusage(resource.RUSAGE_CHILDREN);guard_cpu=0.
|
|
def total():
|
|
u=resource.getrusage(resource.RUSAGE_CHILDREN);return c['prior_cpu_seconds']+c['repair_setup_cpu_seconds']+time.process_time()-start+max(guard_cpu,u.ru_utime+u.ru_stime-u0.ru_utime-u0.ru_stime)
|
|
cap=min(600.,10800-total()-120)
|
|
if cap<=0:raise RuntimeError('Same total budget exhausted')
|
|
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json'),prior_cpu_seconds=c['prior_cpu_seconds'],new_fits=0,new_predictions=0))
|
|
try:
|
|
g=supervise([sys.executable,'tools/phase2_b17_score_repair.py','worker',str(root)],root/'guard',dict(cpu_seconds=cap,wall_seconds=cap+180,rss_bytes=8*2**30,disk_bytes=3*2**30));guard_cpu=g['cpu_seconds']
|
|
if g['status']!='PASS' or total()>10800:raise RuntimeError('REPAIRED_SCORE_NOT_PASS')
|
|
save(root/'complete.json',dict(status='COMPLETE',decision=read(root/'score/result.json')['decision'],cumulative_cpu_seconds=total(),prior_cpu_seconds=c['prior_cpu_seconds'],guard=g,new_fits=0,new_predictions=0,accounting_complete=True,retry=False))
|
|
freeze(root,'terminal',[root/'contract.json',root/'complete.json',root/'score/result.json',root/'review.json',root/'review-binding.json'])
|
|
save(root/'post-terminal-freeze-cost.json',dict(cumulative_cpu_seconds=total(),within_cap=total()<=10800))
|
|
except BaseException as exc:
|
|
save(root/'complete.json',dict(status='STOPPED',reason=type(exc).__name__+': '+str(exc),cumulative_cpu_seconds=total(),accounting_complete=True,retry=False));raise
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser();p.add_argument('action',choices=['run','worker']);p.add_argument('root',type=Path);a=p.parse_args();(run if a.action=='run' else worker)(a.root)
|