"""Bind independently verified offline repair and retain historical readouts."""
import hashlib,json,shutil
from pathlib import Path
R=Path(__file__).resolve().parents[1]
def sha(p):return hashlib.sha256(p.read_bytes()).hexdigest()
s=json.loads((R/'artifacts/neowise_flag_repair_analysis_20261014/summary.json').read_text());v=json.loads((R/'artifacts/neowise_flag_repair_verification_20261014/verification.json').read_text());d=json.loads((R/'artifacts/neowise_flag_repair_delta_20261014/verification.json').read_text())
assert v['verified'] and d['verified'] and v['summary_sha256']==d['independent_summary_sha256']==sha(R/'artifacts/neowise_flag_repair_analysis_20261014/summary.json')
reg=json.loads((R/'artifacts/neowise_flag_format_registration_20261014.json').read_text())
for x in reg['inputs']:
 p=R/x['path'];assert p.stat().st_size==x['bytes'] and sha(p)==x['sha256']
paths=['artifacts/neowise_flag_format_registration_20261014.json','docs/NEOWISE_FLAG_FORMAT_AMENDMENT_20261014.md','docs/NEOWISE_FLAG_REPAIR_RESULTS_20261014.md','research/neowise_flag_format.py','research/analyze_native_profile_flag_repair_20261014.py','research/verify_native_profile_flag_repair_20261014.py','research/check_neowise_flag_format_controls_20261014.py','research/verify_neowise_flag_repair_delta_20261014.py','research/prepare_neowise_flag_repair_20261014.py']
stages=['neowise_flag_format_controls_permitted_20261014','neowise_flag_repair_analysis_20261014','neowise_flag_repair_verification_20261014','neowise_flag_repair_delta_20261014']
guards=[]
for name in stages:
 directory=R/'artifacts'/name;g=json.loads((directory/'resource_monitor.json').read_text());assert g['completed'] and g['stop_reason'] is None and g['samples'][-1]['workers']==[];guards.append(g)
 paths += [str(p.relative_to(R)) for p in sorted(directory.iterdir()) if p.is_file()]
c={'schema':'neowise-offline-flag-repair-checkpoint/1','unit_complete':True,'independent_verification_passed':True,'exact_delta_verification_passed':True,'native_rows':874,'corrected_supported':s['fixed_best_supported'],'corrected_denominator':3,'required_supported':2,'corrected_gate_passed':s['primary_gate_passed'],'shape_only_rejections_removed':d['rows_with_shape_only_rejection_removed'],'accepted_rows_added':d['accepted_rows_added'],'native_flags_decoded_without_rewriting':True,'scientific_thresholds_changed':False,'all_six_alternatives_preserved':True,'new_observation_requests':0,'post_read_consumed_data':True,'fresh_physical_validation':False,'physical_validation':False,'new_stars':0,'new_births':0,'models_changed':False,'full_goal_achieved':False,'automatic_successor_authorized':False,'current_interpretation':'Corrected conservative pipeline-quality/cadence gate failed; no physical negative or birth claim','historical_null_gate_preserved':'artifacts/native_profile_resumption_assessment_20261014.json','report_path':'docs/NEOWISE_FLAG_REPAIR_RESULTS_20261014.md','evidence':[{'path':p,'bytes':(R/p).stat().st_size,'sha256':sha(R/p)} for p in paths],'worker_elapsed_seconds':sum(g['elapsed_seconds'] for g in guards),'minimum_observed_free_bytes':min(g['minimum_observed_free_bytes'] for g in guards),'publication_status_at_creation':'local; separate publication receipts required'}
assert c['corrected_supported']==0 and c['corrected_gate_passed'] is False and c['shape_only_rejections_removed']==645 and c['accepted_rows_added']==0
p=R/'artifacts/neowise_flag_repair_checkpoint_20261014.json';assert not p.exists();p.write_text(json.dumps(c,indent=2)+'\n')
prior=[{'path':str(p.relative_to(R)),'sha256':sha(p)} for p in sorted((R/'artifacts/graph').glob('*.json'))]
kg={'schema':'neowise-flag-repair-reviewed-supplement/1','checkpoint_sha256':sha(p),'preserved_prior_graph_blobs':prior,'nodes':[{'id':'F:NFR20261014:repair','type':'finding','status':'verified_post_read_representation_repair','label':'645 shape-only rejections removed without changing native bytes; zero accepted extractions added','sources':['artifacts/neowise_flag_repair_delta_20261014/verification.json']},{'id':'F:NFR20261014:quality','type':'finding','status':'failed_declared_pipeline_quality_cadence_gate','label':'Corrected measurement-quality support 0/3 versus required 2/3','sources':['artifacts/neowise_flag_repair_analysis_20261014/summary.json','artifacts/neowise_flag_repair_verification_20261014/verification.json']},{'id':'C:NFR20261014:physical','type':'qualification','label':'Consumed cases; failed pipeline gate does not establish physical starlessness, ownership, youth or birth','sources':['docs/NEOWISE_FLAG_REPAIR_RESULTS_20261014.md']}],'edges':[{'source':'C:NFR20261014:physical','target':'F:NFR20261014:quality','relation':'qualifies'}],'archive_not_merged':True,'physical_validation':False,'new_stars':0,'new_births':0,'models_changed':False,'original_null_gate_is_historical':True}
(R/'artifacts/graph/neowise_flag_repair_reviewed_supplement_20261014.json').write_text(json.dumps(kg,indent=2)+'\n');print(json.dumps({'corrected_support':c['corrected_supported'],'gate':c['corrected_gate_passed'],'removed':645,'seconds':c['worker_elapsed_seconds'],'free_gib':shutil.disk_usage(R).free/2**30}))
