"""Approved B3/B4 learning-only gates, distinct components, no new models.""" import argparse import hashlib import json import os from pathlib import Path import sys import time import numpy as np import joblib from scipy.sparse import csr_matrix from threadpoolctl import threadpool_limits from phase2_h1_worker_v2 import sha,checked,save from phase2_h1_topology import graph,events,match from phase2_gate_math import monotone_projection,quantize_lower_tie,birth_platforms from r4bc_guard import supervise BASE=Path('/home/frank14f/optane/MeltMind') def read(p):return json.loads(Path(p).read_text()) def prepare(acceptance): a=read(acceptance) if a['status']!='PASS':raise ValueError('Gate synthetic acceptance missing') old=BASE/'artifacts/r4bc-execution-2026-11-16/contract.json';c=read(old) candidates={} for r in c['parts']: role=next(x for x in r['assignments'] if x['fold']==0) if role['role']=='learning' and '100' in role['learning_pools']:candidates.setdefault(r['component_id'],[]).append(r) components=sorted(candidates,key=lambda x:(hashlib.sha256(x.encode()).hexdigest(),x))[:50] rows=[min(candidates[g],key=lambda r:r['case']) for g in components] if len(rows)!=50 or len({r['component_id'] for r in rows})!=50:raise ValueError('Expected 50 distinct learning components') modelpath=BASE/'artifacts/r4bc-execution-2026-11-16/models-terminal.json' model=next(m for m in read(modelpath)['models'] if m['arm']=='A' and m['fold']==0 and m['pool']==100) source={str(old):sha(old),str(modelpath):sha(modelpath),str(acceptance):sha(acceptance)} for p,h in a['code_sha256'].items():checked(p,h) code={f'tools/{n}.py':sha(BASE/f'tools/{n}.py') for n in ['phase2_gates_34','phase2_gate_math','phase2_h1_topology','phase2_h1_worker_v2','phase2_residual_math','r4bc_guard','s2_recovery_supervisor','s2_recovery_monitoring']} for name,cpu in [('B3',16),('B4',18)]: root=BASE/f'artifacts/phase2-{name.lower()}-gate-2026-11-17';root.mkdir(exist_ok=False) save(root/'contract.json',dict(version='phase2-'+name+'-gate-v1',gate=name,authorizing_ruling='1794887072.921849', b1_main_review='1794888554.440209',parts=rows,code_sha256=code,source_sha256=source,baseline_model=model, selection='fold0/pool100 learning; SHA256(component_id UTF8) ascending; lexicographically first case in each; first50', worker_cpu=cpu,cpu_cap_seconds=7200,worker_cap_seconds=7080,reserved_setup_guard_seconds=120, rss_bytes=8*1024**3,disk_bytes=5*1024**3,new_models=0,new_decodes=0,gpu=False,retry=False, thresholds=dict(grid_step=.001,half_grid_offset=.0005,min_precision=.80,min_recall=.80,min_eventful_parts=10), B4_domain='full retained tet nodes; unit positive weights exactly as A training; existing fold0/pool100 A inference on the same learning parts', failure_policy='retain each exception; missing panel parts prevent PASS/pruning conclusions; no replacement', scope='diagnostic learning-side gate; no test targets, no deployable calibration')) print(root,flush=True) def config(root): c=read(root/'contract.json') for p,h in {**c['source_sha256'],**c['code_sha256']}.items():checked(p,h) for r in c['parts']: role=next(a for a in r['assignments'] if a['fold']==0) if role['role']!='learning' or '100' not in role['learning_pools']:raise PermissionError('Gate attempted held-out part') return c def array(row,name): c=row['v5_cache'];return np.load(checked(Path(c['output'])/(name+'.npy'),c['output_sha256'][name+'.npy']),allow_pickle=False) def geometry(row): p=row['preparation'];d=Path(p['preparation_root']);meta=read(checked(d/'result.json',p['result_sha256'])) with np.load(checked(d/'geometry.npz',p['output_sha256']['geometry.npz']),allow_pickle=False) as z: x=z['points_raw'].copy();tet=z['tetra'].copy();raw=z['raw_node_ids'].copy() if not np.array_equal(raw,array(row,'raw_ids')):raise ValueError('Gate raw IDs differ') return x,raw,array(row,'mass'),graph(x,tet),float(meta['scales']['Rg']) def totals(rows,key): items=[r[key] for r in rows];tp=sum(x['TP'] for x in items);fp=sum(x['FP'] for x in items);fn=sum(x['FN'] for x in items) distances=[d for x in items for d in x['matched_distance_over_Rg']];empty={} for item in items:empty[item['empty_class']]=empty.get(item['empty_class'],0)+1 return dict(TP=tp,FP=fp,FN=fn,precision=tp/(tp+fp) if tp+fp else None,recall=tp/(tp+fn) if tp+fn else None, median_distance_over_Rg=float(np.median(distances)) if distances else None,matched=len(distances),empty_classes=empty) def run_b3(root,c): rows=[];failures=[];start=time.process_time();dest=root/'parts';dest.mkdir(exist_ok=False) for r in c['parts']: out=dest/r['case'];out.mkdir() save(out/'access.json',dict(role='fold0_learning100',seconds_sha256=r['v5_cache']['output_sha256']['seconds.npy'])) try: x,raw,m,a,rg=geometry(r);truth=array(r,'seconds');scale=r['preparation']['t_set'];ref=events(truth,m,raw,x,a) save(out/'truth-events.json',ref);save(out/'birth-platforms.json',birth_platforms(truth,m,raw,x,a)) info=dict(case=r['case'],component_id=r['component_id'],nodes=len(raw),edges=a.nnz//2,volume=float(m.sum()),Rg=rg,true_events=len(ref['events'])) for label,offset in [('grid',0.),('half_grid',.0005)]: field=quantize_lower_tie(truth/scale,.001,offset)*scale;pred=events(field,m,raw,x,a) info[label]=match(ref,pred,a,rg);save(out/(label+'-events.json'),pred) np.savez_compressed(out/'geometry.npz',points=x,raw_ids=raw,mass=m,indices=a.indices,indptr=a.indptr,data=a.data,rg=rg) save(out/'result.json',info);rows.append(info) except Exception as exc: failure=dict(case=r['case'],type=type(exc).__name__,message=str(exc));failures.append(failure);save(out/'failure.json',failure) save(root/'progress.json',dict(phase='TRUE_FIELD_STABILITY',complete=len(rows),failures=len(failures),cpu_seconds=time.process_time()-start)) stability={key:totals(rows,key) for key in ('grid','half_grid')};eventful=sum(r['true_events']>0 for r in rows) good=not failures and len(rows)==50 and eventful>=10 and all(t[k] is not None and t[k]>=.8 for t in stability.values() for k in ('precision','recall')) save(root/'stability-gate.json',dict(pass_gate=good,eventful_parts=eventful,planned=50,complete=len(rows),failures=failures,metrics=stability)) eik=[] if good: for r in c['parts']: out=dest/r['case'] with np.load(out/'geometry.npz',allow_pickle=False) as z: raw=z['raw_ids'];x=z['points'];m=z['mass'];rg=float(z['rg']);a=csr_matrix((z['data'],z['indices'],z['indptr']),shape=(len(raw),len(raw))) pred=events(array(r,'Vf')*r['preparation']['t_set'],m,raw,x,a) score=match(read(out/'truth-events.json'),pred,a,rg) save(out/'eik-events.json',dict(events=pred,match=score));eik.append(dict(case=r['case'],eik=score)) save(root/'result.json',dict(status='COMPLETE',gate='B3',decision='METRIC_STABLE_ON_THIS_PANEL' if good else 'PARK_UNIVERSAL_MODEL_JUDGEMENT', eventful_parts=eventful,stability=stability,rows=rows,failures=failures,EIK=totals(eik,'eik') if eik else None, EIK_attempted=good,reopen='independent temporal resolution or remesh reference',cpu_seconds=time.process_time()-start, limitations=['learning panel only','precision perturbation not remesh convergence','no weld/airtrap truth'])) def run_b4(root,c): model=c['baseline_model'];baseline=joblib.load(checked(model['model_path'],model['model_sha256'])) rows=[];failures=[];start=time.process_time();dest=root/'parts';dest.mkdir(exist_ok=False) with threadpool_limits(1): for r in c['parts']: out=dest/r['case'];out.mkdir();save(out/'access.json',dict(role='fold0_learning100',target_sha256=r['v5_cache']['output_sha256']['target.npy'])) try: y=array(r,'target');s=array(r,'Df');x=np.asarray(array(r,'features'),dtype=np.float32) if len(y)!=r['nodes'] or x.shape!=(len(y),19):raise ValueError('A full node domain mismatch') info,curve=monotone_projection(s,y,np.ones(len(y)));pred=np.asarray(baseline.predict(x),dtype=np.float64) denominator=float(np.sum(y*y));ae=float(np.sqrt(np.sum((pred-y)**2)/denominator)) if denominator>0 else None result=dict(case=r['case'],component_id=r['component_id'],A_relative_l2=ae,**info) np.savez_compressed(out/'monotone-map.npz',**curve);save(out/'result.json',result);rows.append(result) except Exception as exc: failure=dict(case=r['case'],type=type(exc).__name__,message=str(exc));failures.append(failure);save(out/'failure.json',failure) save(root/'progress.json',dict(complete=len(rows),failures=len(failures),cpu_seconds=time.process_time()-start)) defined=[r for r in rows if r['relative_l2'] is not None and r['A_relative_l2'] is not None] oracle=float(np.mean([r['relative_l2'] for r in defined])) if defined else None;aa=float(np.mean([r['A_relative_l2'] for r in defined])) if defined else None decision='PARK_COVERAGE' if len(defined)!=50 or failures else 'PRUNE_ONLY_CLOCK_CAN_BEAT_A' if oracle>=aa else 'RETAIN_POSSIBILITY_ONLY' save(root/'result.json',dict(status='COMPLETE',gate='B4',decision=decision,component_equal_oracle_l2=oracle, component_equal_A_l2=aa,rows=rows,failures=failures,cpu_seconds=time.process_time()-start, interpretation='posthoc lower bound for this fixed potential/domain/positive weights/monotone map class; not causal ordering share or deployable calibration', reopen='new potential, different control domain, or additional branch information')) def supervise_gate(root): c=config(root);os.sched_setaffinity(0,[c['worker_cpu']]);start=time.process_time() if (root/'actual-start.json').exists():raise FileExistsError('No duplicate gate') save(root/'actual-start.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json'),main_review=c['authorizing_ruling'])) result=supervise([sys.executable,'tools/phase2_gates_34.py','worker',str(root)],root/'guard', dict(cpu_seconds=c['worker_cap_seconds'],wall_seconds=c['worker_cap_seconds'],rss_bytes=c['rss_bytes'],disk_bytes=c['disk_bytes'])) guard_cpu=time.process_time()-start;total=result['cpu_seconds']+guard_cpu status='PASS' if result['status']=='PASS' and total+60<=7200 and (root/'result.json').exists() else 'STOPPED' save(root/'complete.json',dict(status=status,worker=result,guard_cpu_seconds=guard_cpu,setup_charge_seconds=60, cumulative_charge_seconds=total+60,scientific_gate_pass=None if status!='PASS' else read(root/'result.json')['decision'],retry=False)) return 0 if status=='PASS' else 1 if __name__=='__main__': p=argparse.ArgumentParser();p.add_argument('mode',choices=['prepare','supervise','worker']);p.add_argument('path',type=Path);a=p.parse_args() if a.mode=='prepare':prepare(a.path) elif a.mode=='supervise':raise SystemExit(supervise_gate(a.path)) else: c=config(a.path);(run_b3 if c['gate']=='B3' else run_b4)(a.path,c)