"""Check worker plot/claims against original producer JSON without GPU work.""" import json,os,math from pathlib import Path root=Path(os.environ['SILICO_INPUT_ARTIFACTS_DIR'])/'experiments'/os.environ['SILICO_EXPERIMENT_ID'] work=Path.cwd() # launched after cd "$SILICO_EXPERIMENT_RELATIVE_DIR" base=root/'alg_hodge_27b_01';raw=base/'repaired_v2' def j(p):return json.loads(p.read_text()) p=j(root/'review_contract/plot_ready_worker_v3.json');probe=j(raw/'probe_results.json');h=j(raw/'hodge_results.json');iv=j(raw/'intervention_results.json');freeze=j(raw/'intervention_freeze.json');pair=j(base/'review_v2/paired_contrasts.json');local=j(base/'review/local_hodge_results.json');integrity=j(base/'review/integrity.json');examples=j(base/'review/examples.json');short=j(work/'results/examples_small.json') verified=[];mismatches=[] def same(label,a,b): if isinstance(a,(int,float)) and not isinstance(a,bool): equal=math.isclose(float(a),float(b),rel_tol=1e-8,abs_tol=1e-8) else:equal=(a==b) if equal:verified.append(label) else:mismatches.append({'label':label,'display':a,'source':b}) for name,key in [('posterior','posterior'),('increment','increment'),('advantage','G')]: datum=p['evaluation_mae'][name];src=probe['readouts'][key] for x,y in [('readout','mae'),('constant','constant'),('shuffled_labels','shuffled_labels')]: obj=src[y] if y=='mae' else src['controls_mae'][y] same(f'{name}.{x}',datum[x],obj['mean']) same(f'{name}.n',datum['n'],src['mae']['n_rows']) for idx in range(2):same(f'{name}.ci{idx}',datum['ci95'][idx],src['mae']['ci95'][idx]) for dst,src in [('natural_model',probe['stopping']['receipt_p_continue']['accuracy_at_half']),('advantage_probe',probe['stopping']['accuracy']),('public_features',probe['stopping']['accuracy_controls']['public_state_event']),('near_boundary_abs_G_lt_0_01_probe',probe['stopping']['by_abs_G_margin']['near_0_to_0.01']['accuracy']),('far_abs_G_ge_0_05_probe',probe['stopping']['by_abs_G_margin']['at_least_0.05']['accuracy'])]: for key,source_key in [('value','mean'),('n','n_rows')]:same(dst+'.'+key,p['stopping_accuracy'][dst][key],src[source_key]) for i in range(2):same(dst+f'.ci{i}',p['stopping_accuracy'][dst]['ci95'][i],src['ci95'][i]) for dst,src in [('face_curl_l2','curl_l2'),('nearest_potential_residual_l2','residual_l2'),('six_order_endpoint_spread','endpoint_spread')]: same('hodge.'+dst,p['hodge'][dst]['value'],h[src]['mean']) for i in range(2):same(f'hodge.{dst}.ci{i}',p['hodge'][dst]['ci95'][i],h[src]['ci95'][i]) same('hodge.r',p['hodge']['family_curl_vs_G_probe_stopping_error_pearson'],h['family_curl_stopping_error_pearson']) same('hodge.exact',p['hodge']['exact_bayesian_max_curl_l2'],h['max_bayes_curl_l2']) for dst,src in [('direction','direction'),('random','random'),('shuffled_coordinates','shuffled'),('norm_matched_to_swap','norm_matched'),('opposite_state_swap','swap'),('sham','sham')]: val=p['interventions'][dst];orig=iv['conditions'][src] same('intervention.'+dst,val['directional_p_change'],orig['directional_p_change']['mean']) for i in range(2):same(f'intervention.{dst}.ci{i}',val['ci95'][i],orig['directional_p_change']['ci95'][i]) if 'mean_edit_norm' in val:same('intervention.'+dst+'.norm',val['mean_edit_norm'],orig['edit_norm']['mean']) for key,src in [('direction_delta_curl_l2','direction'),('opposite_swap_delta_curl_l2','swap')]: same('local.'+key,p['local_context_hodge_change'][key],local['conditions'][src]['delta_curl_l2']['mean']) for task in ('posterior','increment','G'): chart_key={'posterior':'posterior_log_odds_nats','increment':'last_event_increment_nats','G':'continuation_advantage_reward_units'}[task] candidates=freeze['calibration'][task]['candidates'] for layer,val in zip(p['layers_zero_based'],p['calibration_mae_alpha_100'][chart_key]): candidate=next(x for x in candidates if x['layer']==layer and x['alpha']==100.) same(f'calibration.{task}.{layer}',val,candidate['calibration_mae']) for case in short['examples']: group=next((g for k in ('best_posterior','worst_posterior','natural_errors','natural_correct') for g in examples[k] if g['id']==case['id']),None) assert group is not None for a,b in [('exact_posterior_log_odds','exact_posterior_log_odds'),('decoded_posterior_log_odds','decoded_posterior_log_odds'),('model_p_continue','natural_p_continue')]:same(case['id']+'.'+a,case[a],group[b]) assert integrity['verified'] and integrity['all_128_shard_checksums'] and pair['original_runtime_call_count_correction']['actual_evaluation_edited_forward_calls']==2048 out=Path(os.environ['SILICO_EXPERIMENT_ARTIFACTS_DIR'])/'alg_hodge_27b_01/review_v3';out.mkdir(parents=True,exist_ok=True) (out/'contract_verification.json').write_text(json.dumps({'verified':not mismatches,'checked_fields':len(verified),'mismatches':mismatches,'source_files':9,'generator_and_shards_verified':True,'note':'Compared compact worker display metrics and examples against immutable full-precision producer and review outputs.'},indent=2)+'\n') print(json.dumps({'verified':not mismatches,'checked_fields':len(verified),'mismatches':mismatches},sort_keys=True)) if mismatches:raise AssertionError('worker plot data differs from source metrics; see contract_verification.json')