"""Independent exact-reference and count-only cost gates; no label access.""" import argparse from pathlib import Path import time import traceback import numpy as np from scipy.stats import kendalltau from s3b_runtime import read,sha,checked,restrict_prediction_io from naive_ordering_math import cdf_interval from s2_recovery_supervisor import save def compare_fields(exact,batch): for key in ['raw_ids','mass']: if not np.array_equal(exact[key],batch[key]):raise ValueError('Full tet support mismatch: '+key) n=len(exact['raw_ids']) for field in [exact,batch]: if (field['arrival'].shape!=(n,) or field['remaining'].shape!=(n,) or not np.isfinite(field['arrival']).all() or np.any(field['remaining']!=0)): raise ValueError('Reference comparison needs all finite full-tet arrivals and zero remaining') tau=float(kendalltau(exact['arrival'],batch['arrival'],variant='b',nan_policy='raise').statistic) left_exact,_=cdf_interval(exact['arrival'],exact['mass']) left_batch,_=cdf_interval(batch['arrival'],batch['mass']) return dict(status='PASS' if np.isfinite(tau) and tau>=.999 else 'FAIL',nodes=n, tau_b=tau if np.isfinite(tau) else None,threshold=.999, max_strict_volume_CDF_delta=float(np.max(abs(left_exact-left_batch))), exact_unique_arrivals=len(np.unique(exact['arrival'])),batch_unique_arrivals=len(np.unique(batch['arrival'])), tau_measure='one vote per full tet node',CDF_measure='strict less-than, original full tet mass', CDF_delta_gate=False,no_solver_switch=True) def query_count(edges,faces,dw): n=len(dw) if not np.isfinite(dw).all() or np.any(dw<0):raise ValueError('Invalid Dw') e=np.asarray(edges);f=np.asarray(faces) wall=np.sort(np.concatenate([f[:,[0,1]],f[:,[0,2]],f[:,[1,2]]]),axis=1) keys=e[:,0].astype(np.int64)*n+e[:,1] wallkeys=np.unique(wall[:,0].astype(np.int64)*n+wall[:,1]) is_wall=np.isin(keys,wallkeys);both=(dw[e]==0).all(axis=1) if not both[is_wall].all():raise ValueError('Surface edge does not have zero-Dw endpoints') return int(np.count_nonzero(both&~is_wall)) def projection_terms(measurements,total_nodes,total_queries,parts): if len(measurements)!=10 or any(r['status']!='COMPLETE' for r in measurements): raise ValueError('Ten complete measurements required') midpoint_rates=[r['costs']['midpoint_CPU_s']/r['exact_midpoint_queries'] for r in measurements if r['exact_midpoint_queries']>0] if total_queries>0 and not midpoint_rates:raise ValueError('No ten-part positive-query rate evidence') event_rate=max(r['costs']['events_CPU_s']/r['nodes'] for r in measurements) midpoint_rate=max(midpoint_rates,default=0.) # Fixed import/verification/geometry/output overhead is conservatively # bounded by the largest measured non-event/non-query overhead per part. overhead=max(max(0.,r['guard_CPU_s']-r['costs']['events_CPU_s']- (r['costs']['midpoint_CPU_s'] if r['exact_midpoint_queries']>0 else 0.)) for r in measurements) return dict(max_event_CPU_per_node=event_rate,max_midpoint_CPU_per_query=midpoint_rate, positive_query_measurements=len(midpoint_rates),max_fixed_overhead_CPU_per_part=overhead, total_nodes=total_nodes,total_queries=total_queries,parts=parts, variable_projection_CPU_s=1.25*(event_rate*total_nodes+midpoint_rate*total_queries), fixed_projection_CPU_s=overhead*parts,margin=1.25, note='Frozen full617 max-rate screening; includes ten in conservative projection, but winning ten are reused, never rerun.') def verify_gate_config(root): c=read(root/'contract.json');f=read(root/'code-freeze.json') if f['contract_sha256']!=sha(root/'contract.json') or not f['git_commit']:raise ValueError('Gates not Git-frozen') for p,h in {**c['code_sha256'],**c['source_sha256']}.items():checked(p,h) return c def measure(root,kind): c=verify_gate_config(root);ten=Path(c['ten_root']);tc=read(ten/'contract.json') terminal=read(ten/'complete.json');chosen=read(ten/'selected-solver.json') if terminal['status']!='TEN_PASS_AWAITING_REFERENCE_AND_COST' or terminal['selected_solver']!=chosen['method']: raise ValueError('Ten not fully passed and frozen') if (Path(c['stage_guard_root'])/'trigger.json').exists():raise ValueError('External stage-cap failure') method=chosen['method'];dest=root/kind/'output';dest.mkdir(exist_ok=False) start=time.process_time();allowed=[tc['roster']] fields=[];results=[];measurements=[] for i in range(10): d=ten/'candidates'/method/'parts'/f'part-{i:02d}' receipt=read(d/'guard-result.json') if receipt['classification']['category']!='PASS':raise ValueError('Ten receipt not PASS') p=d/'output/result.json';checked(p,receipt['files']['result']['sha256']) result=read(p);result['guard_CPU_s']=receipt['worker']['cpu_seconds'];measurements.append(result) if i<3: f=d/'output/event-field.npz';fields.append(f) allowed.append(str(f));results.append(result) if kind=='reference': allowed += [r['field'] for r in tc['exact_references']] restrict_prediction_io(allowed,dest) rows=[] for i,ref in enumerate(tc['exact_references']): row=dict(index=i,case=ref['case'],reference_sha256=ref['field_sha256'], batch_sha256=results[i]['output_sha256']['event-field.npz']) try: checked(ref['field'],ref['field_sha256']);checked(fields[i],row['batch_sha256']) with np.load(ref['field'],allow_pickle=False) as a,np.load(fields[i],allow_pickle=False) as b: row.update(compare_fields(a,b)) except Exception as exc:row.update(status='FAIL',type=type(exc).__name__,reason=str(exc)) rows.append(row) result=dict(status='PASS' if len(rows)==3 and all(r['status']=='PASS' for r in rows) else 'FAIL', solver=method,parts=rows,large_part_accuracy_verified=False,no_solver_switch=True, labels_read=False,worker_CPU_s=time.process_time()-start) save(dest/'result.json',result) return 0 if kind!='census':raise ValueError('Unknown gate stage') # This only counts original edges; it does not query distances or solve. checked(tc['roster'],tc['roster_sha256']);roster=read(tc['roster']) included=sorted([r for r in roster['parts'] if r['included']],key=lambda r:r['case']) if [r['case'] for r in included]!=tc['all_cases'] or len(included)!=617:raise ValueError('Roster mismatch') for r in included: source=Path(r['preparation']['preparation_root']);allowed += [str(source/'geometry.npz'),str(source/'boundary_distance_raw.npy')] restrict_prediction_io(allowed,dest) counts=[] try: for row in included: p=row['preparation'];source=Path(p['preparation_root']) for name in ['geometry.npz','boundary_distance_raw.npy']:checked(source/name,p['output_sha256'][name]) dw=np.load(source/'boundary_distance_raw.npy',allow_pickle=False) with np.load(source/'geometry.npz',allow_pickle=False) as z: edges=z['edges'];faces=z['boundary_triangles'];raw=z['raw_node_ids'] if len(raw)!=row['nodes'] or len(dw)!=len(raw):raise ValueError('Node count differs') count=query_count(edges,faces,dw) counts.append(dict(case=row['case'],nodes=row['nodes'],exact_midpoint_queries=count, geometry_sha256=p['output_sha256']['geometry.npz'],dw_sha256=p['output_sha256']['boundary_distance_raw.npy'])) save(dest/'progress.json',dict(parts=len(counts),worker_CPU_s=time.process_time()-start)) save(dest/'counts.json',dict(parts=counts,labels_read=False,distances_queried=0)) bycase={r['case']:r for r in counts} for r in measurements: if bycase[r['case']]['exact_midpoint_queries']!=r['exact_midpoint_queries']:raise ValueError('Census/ten query count differs') terms=projection_terms(measurements,sum(r['nodes'] for r in counts),sum(r['exact_midpoint_queries'] for r in counts),len(counts)) save(dest/'result.json',dict(status='COMPLETE',projection_terms=terms,counts_sha256=sha(dest/'counts.json'), labels_read=False,distances_queried=0,worker_CPU_s=time.process_time()-start)) return 0 except Exception as exc: save(dest/'failure.json',dict(type=type(exc).__name__,reason=str(exc),traceback=traceback.format_exc(), complete_parts=counts,worker_CPU_s=time.process_time()-start)) raise if __name__=='__main__': p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('kind',choices=['reference','census']) a=p.parse_args();raise SystemExit(measure(a.root,a.kind))