Files
MeltMind/tools/phase2_b14_gate.py

82 lines
7.2 KiB
Python

"""Independent B14 channel gate, original B12 scientific fitting/scoring reused."""
import argparse,fcntl,json,os,resource,sys,time,traceback
from pathlib import Path
import numpy as np
from phase2_b1213_gate import config,read,coverage,train,predict,score
from phase2_b1213_features import input_geometry,cached,flow_mesh,cell_flow_speed,exterior_faces,InputViolation
from phase2_h1_worker_v2 import checked,sha,save
from phase2_fixed_io import BASE,freeze
from r4bc_guard import supervise
from s2_recovery_monitoring import disk_usage
from phase2_b14_math import channels
def field(root,c,index):
row=c['parts'][index];dest=root/'parts'/f'{index:04d}';start=time.process_time()
try:
x,t,raw,old=input_geometry(row);g=flow_mesh(x,t);F=cached(row,'Vf');df=cached(row,'Df');dw=cached(row,'Dw')
source=np.searchsorted(raw,old['features']['gate_raw_ids'])
if np.any(source>=len(raw)) or not np.array_equal(raw[source],old['features']['gate_raw_ids']):raise InputViolation('Gate identity')
speed,allwall=cell_flow_speed(g,dw,exterior_faces(t))
if not np.allclose([speed.min(),speed.max()],[old['features']['speed_min'],old['features']['speed_max']],rtol=1e-12,atol=0):raise InputViolation('Shared R1 speed differs')
if not np.isfinite(F).all() or not np.isfinite(df).all() or np.any(F<0) or np.any(F>1) or np.any(df<0):raise ValueError('Invalid frozen F/Df')
receipts=[]
def emit(record,arrays):
p=dest/f'layer-{len(receipts):03d}.npz';np.savez_compressed(p,**arrays);receipts.append(dict(**record,path=str(p),sha256=sha(p)))
save(dest/'progress.json',dict(layers_completed=len(receipts),last_layer=record,cpu_seconds=time.process_time()-start))
extra,report,dg=channels(x,t,F,df,g['mass'],g['volume'],speed,source,emit)
np.save(dest/'extra.npy',extra,allow_pickle=False);np.savez_compressed(dest/'distances.npz',dG=dg,Df=df,F=F,raw_ids=raw)
complete=report['valid_nonroot_volume_fraction']>=.8
save(dest/'result.json',dict(case=row['case'],component_id=row['component_id'],index=index,nodes=row['nodes'],status='GEOMETRY_COMPLETE' if complete else 'SCIENTIFIC_MISSING',reason=None if complete else 'NONROOT_VOLUME_WITNESS_COVERAGE_BELOW_80PCT',
extra_sha256=sha(dest/'extra.npy'),distances_sha256=sha(dest/'distances.npz'),layer_receipts=receipts,labels_read=False,all_families_attempted=True,**report,cpu_seconds=time.process_time()-start))
except BaseException as exc:
save(dest/'failure.json',dict(type=type(exc).__name__,reason=str(exc),traceback=traceback.format_exc(),cpu_seconds=time.process_time()-start,partial_is_not_complete=True));raise
def run(root):
c=config(root);os.sched_setaffinity(0,[26]);lock=(root/'run.lock').open('a');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
if (root/'started.json').exists():raise FileExistsError('Single launch; zero retries')
start=time.process_time();u0=resource.getrusage(resource.RUSAGE_CHILDREN);charged=0.;records=[];entries=[];results={}
def total():
u=resource.getrusage(resource.RUSAGE_CHILDREN);return 60+max(charged,u.ru_utime+u.ru_stime-u0.ru_utime-u0.ru_stime)+time.process_time()-start
def guard(mode,dest,cap,wall,index=None):
nonlocal charged
remaining=c['disk_bytes']-disk_usage(root)
cap=min(cap,7200-total()-120)
if cap<=0 or remaining<=0:raise RuntimeError('Resource reserve exhausted')
command=[sys.executable,'tools/phase2_b14_gate.py',mode,str(root)]+([] if index is None else [str(index)])
g=supervise(command,dest,dict(cpu_seconds=cap,wall_seconds=wall,rss_bytes=c['rss_bytes'],disk_bytes=remaining))
charged+=g['cpu_seconds'];records.append(dict(mode=mode,index=index,guard=g));save(root/'phase-ledger.json',dict(records=records,cumulative_cpu_seconds=total()))
if g['status']!='PASS' or total()>7200 or disk_usage(root)>c['disk_bytes']:raise RuntimeError('ENGINEERING_OR_RESOURCE_STOP')
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json')))
try:
for i,row in enumerate(c['parts']):
if total()-60+c['part_cpu_seconds']>c['geometry_cpu_cap']:raise RuntimeError('PARK_RESOURCE_GEOMETRY_CAP')
dest=root/'parts'/f'{i:04d}';dest.mkdir(parents=True,exist_ok=False)
save(root/'live-progress.json',dict(stage='GEOMETRY_ONLY',index=i,case=row['case'],cumulative_cpu_seconds=total(),unix=time.time()))
guard('field',dest,c['part_cpu_seconds'],c['part_wall_seconds'],i)
result=read(dest/'result.json');results[row['case']]=result;entries.append(dict(case=row['case'],index=i,result_sha256=sha(dest/'result.json')))
if i+1==c['cost_checkpoint']:
projected=1.25*(total()-60)*sum(r['nodes'] for r in c['parts'])/sum(r['nodes'] for r in c['parts'][:i+1])
save(root/'first-ten-cost-check.json',dict(projected_geometry_cpu_seconds=projected,cap=c['geometry_cpu_cap'],passed=projected<=c['geometry_cpu_cap'],actual_cpu_seconds=total()))
if projected>c['geometry_cpu_cap']:raise RuntimeError('PARK_RESOURCE_FIRST_TEN_PROJECTION')
cov={role:coverage(rows,results) for role,rows in c['rows'].items()};save(root/'coverage.json',cov)
save(root/'features-manifest.json',dict(entries=entries,all431_terminal=True,labels_read=False))
freeze(root,'features',[root/'contract.json',root/'features-manifest.json',root/'coverage.json',*[BASE/p for p in c['code_sha256']]])
if c['kind']=='B14' and any(v['fraction']<.8 for v in cov.values()):
save(root/'complete.json',dict(status='COMPLETE',scientific_gate='PARK_CHANNEL_COVERAGE',coverage=cov,cumulative_cpu_seconds=total(),records=records,models_fit=0,retry=False));return
for mode,cap in [('train',c['fit_cpu_cap']),('predict',c['predict_cpu_cap']),('score',c['score_cpu_cap'])]:
if mode=='predict':freeze(root,'model',[root/'contract.json',root/'phases/train/model.ubj',root/'phases/train/result.json',*[BASE/p for p in c['code_sha256']]])
if mode=='score':freeze(root,'predictions',[root/'contract.json',root/'phases/predict/manifest.json',*[BASE/p for p in c['code_sha256']]])
dest=root/'phases'/mode;dest.mkdir(parents=True,exist_ok=False);save(root/'live-progress.json',dict(stage=mode,cumulative_cpu_seconds=total(),unix=time.time()))
guard(mode,dest,cap,cap+60)
save(root/'complete.json',dict(status='COMPLETE',scientific_gate=read(root/'phases/score/result.json')['decision'],cumulative_cpu_seconds=total(),records=records,models_fit=1,retry=False))
except BaseException as exc:
save(root/'complete.json',dict(status='STOPPED',reason=type(exc).__name__+': '+str(exc),cumulative_cpu_seconds=total(),records=records,completed_geometry_parts=len(entries),retry=False));raise
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('mode',choices=['run','field','train','predict','score']);p.add_argument('root',type=Path);p.add_argument('index',nargs='?',type=int);a=p.parse_args()
if a.mode=='run':run(a.root)
elif a.mode=='field':field(a.root,config(a.root),a.index)
else:dict(train=train,predict=predict,score=score)[a.mode](a.root,config(a.root))