Files
MeltMind/tools/audit_o1_v2_panel.py

84 lines
5.0 KiB
Python

"""Read-only receipt/hash audit and report for the approved sixty-panel gate."""
import argparse
import collections
import csv
from pathlib import Path
import time
from o1_prepare import read, contract
from o1_partition import sha, checked
from s2_recovery_supervisor import save
def audit(root):
start=time.process_time(); c=contract(root)
panel=read(root/'panel-feasibility.json'); manifest=read(root/'panel-manifest.json')
expected=[p['case'] for p in manifest['parts']]
assert len(expected)==len(set(expected))==60
assert expected==[c['parts'][i]['case'] for i in c['panel_indices']]
rows=[]; records=[]; hashes=0
for case in expected:
folder=root/'parts'/case; rec=read(folder/'guard-result.json');records.append(rec)
assert rec['case']==case
if rec['result_path']:
r=read(checked(rec['result_path'],rec['result_sha256']))
for name,digest in r.get('output_sha256',{}).items():
checked(folder/'output'/name,digest); hashes+=1
else:r=dict(status='WORKER_FAILED',reason=rec['worker']['stop_reason'])
d=r.get('diagnostics',{}); m=r.get('matching') or {}; w=r.get('weight_diagnostics',{})
arms=r.get('arms',{})
row=dict(case=case,component_id=c['parts'][rec['index']]['component_id'],
worker_status=rec['worker']['status'],status=r['status'],reason=r.get('reason'),
arm_failures='; '.join(a+': '+v.get('reason','') for a,v in arms.items() if v['status']!='PASS'),
calls=r.get('partition_calls'),g_p=w.get('g_p'),
TG=d.get('T/G',{}).get('interface',{}).get('value'),
TC=d.get('T/C',{}).get('interface',{}).get('value'),
GC=d.get('G/C',{}).get('interface',{}).get('value'),
cut_area_ratio=m.get('cut_area_max_min'),
diameter_ratio_max=max(m['sorted_diameter_max_min']) if m else None,
volume_pair_scaled_max=max(m['volume_pair_scaled_max'].values()) if m else None,
old_five_percent_match=m.get('old_five_percent_matching_pass'),
worker_cpu_seconds=rec['worker']['cpu_seconds'],result_sha256=rec['result_sha256'])
rows.append(row)
if r.get('protocol'):
assert r.get('labels_decoded')==[] and not r.get('label_cache_opened',False) and not r.get('model_fit',False)
if arms:assert all(v['seed']==0 for v in arms.values())
e=sum(r['status']=='PASS' for r in rows)
h=sum(r['status']=='PASS' and r['TG']>=.1 and r['TC']>=.1 for r in rows)
assert (e,h)==(panel['eligible_parts'],panel['strong_eligible_parts'])
assert panel['total_parts']==60
if not panel['passed']:
done=read(root/'complete.json')
assert done['status']=='V2_PANEL_IDENTIFIABILITY_STOP'
assert len(done['records'])==60 and not done['global_stop'] and done['accounting_complete']
assert len(done['never_attempted'])==len(c['dispatch'])-60
with (root/'panel-parts.csv').open('w',newline='') as f:
writer=csv.DictWriter(f,fieldnames=list(rows[0]));writer.writeheader();writer.writerows(rows)
save(root/'panel-parts.json',rows)
matching=[r for r in rows if r['cut_area_ratio'] is not None]
matching_failure_dimensions={
'volume_pair_scaled_above_0025':sum(r['volume_pair_scaled_max']>.025 for r in matching),
'cut_area_max_min_above_125':sum(r['cut_area_ratio']>1.25 for r in matching),
'sorted_diameter_max_min_above_125':sum(r['diameter_ratio_max']>1.25 for r in matching)}
result=dict(status='PASS',panel_passed=panel['passed'],panel_sha256=sha(root/'panel-feasibility.json'),
manifest_sha256=sha(root/'panel-manifest.json'),source_pins='PASS',
all_60_guard_statuses=dict(collections.Counter(r['worker_status'] for r in rows)),
scientific_statuses=dict(collections.Counter(r['status'] for r in rows)),
reasons=dict(collections.Counter(str(r['reason']) for r in rows)),
arm_failure_causes=dict(collections.Counter(r['arm_failures'] for r in rows if r['arm_failures'])),
matching_failure_dimensions_overlapping=matching_failure_dimensions,
all_three_arm_metrics_available=len(matching),
all_three_arm_interface_differences_available=sum(r['TG'] is not None and r['TC'] is not None for r in rows),
old_five_percent_match_parts=sum(r['old_five_percent_match'] is True for r in rows),
partition_calls=sum(r['calls'] or 0 for r in rows),verified_output_arrays=hashes,
panel_worker_cpu_seconds=sum(r['worker']['cpu_seconds'] for r in records),
initial_preparation_cpu_seconds=c['initial_preparation_cpu_seconds'],
panel_cumulative_cpu_seconds=c['initial_preparation_cpu_seconds']+sum(r['worker']['cpu_seconds'] for r in records),
audit_cpu_seconds=time.process_time()-start,labels_decoded=0,new_partitions=0,new_models=0)
save(root/'panel-independent-audit.json',result)
print(result)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);a=p.parse_args();audit(a.root)