"""Tiny synthetic arrays / temporary read fixtures; no brain or GPU execution.""" import copy import csv import json from pathlib import Path import tempfile import unittest import numpy as np import boundary_compare as m Q=m._load('engineering_queue_audit.py',m.PINS['engineering_queue_audit.py']) def raw(a):return __import__('hashlib').sha256(np.ascontiguousarray(a).tobytes()).hexdigest() def save(path,value):path.write_text(json.dumps(value,allow_nan=False),encoding='utf-8') class Fixture: def __init__(self,cut=20,total=61,reject=False,delta=None): self.cut,self.total=cut,total self.graph=dict(ptr=np.array([0,2,4,5,6],np.int64),src=np.array([0,0,1,1,2,3],np.int32), dst=np.array([1,2,2,3,3,0],np.int32),edge_id=np.arange(6,dtype=np.int64), base_weights=np.full(6,.000275,np.float64),apl_mask=np.zeros(6,np.bool_), candidate_edge_id=np.array([0],np.int64),delta_before=np.array([0.],np.float32), delta_after=np.array([-.00275 if delta is None else delta],np.float32)) g=self.graph _,_,rows,before,after=m._candidates(g['ptr'],g['src'],g['dst'],g['edge_id'],g['base_weights'],g['apl_mask'], g['candidate_edge_id'],g['delta_before'],g['delta_after'],Q) self.before_sha=m._effective_hash(g['base_weights'],g['apl_mask'],rows,before) self.after_sha=m._effective_hash(g['base_weights'],g['apl_mask'],rows,after) self.effective=g['base_weights'].copy();self.effective[rows]=after pairs=[(0,3),(cut-2,2),(cut-1,0),(cut+23,0),(total-1,2)] if reject:pairs.append((cut+17,1)) pairs=sorted(pairs);self.tick=np.array([t for t,n in pairs],np.int32);self.node=np.array([n for t,n in pairs],np.int32) self.requests=np.column_stack((self.tick,self.node,np.ones(len(pairs),np.int32),np.ones(len(pairs),np.int32),np.zeros(len(pairs),np.int32))).astype(np.int64) refs=[];ref=np.zeros(4,np.int32);self.queues=[] for t in range(total): ref=np.maximum(ref-1,0).astype(np.int32);ref[self.node[self.tick==t]]=22;refs.append(ref.copy()) sources=self.node[self.tick==t-18] if t>=18 else self.node[:0] qr,_,_=Q._queue(t,t if t=start)&(self.tick=start)&(self.requests[:,0]0, global_tick_offset=start,global_next_tick=stop) return dict(steps=stop-start,global_tick_offset=start,state=state, terminal=dict(spike_ring=ring,apl_state=self.traces['state'][stop-1].copy(),apl_pending=pending,next_tick=np.asarray(stop-start,np.int64)), requests=req,summary=summary,traces={k:v[start:stop].copy() for k,v in self.traces.items()},state_hashes=hashes,queues=queues, terminal_origin='recorded_terminal_queues') def compare(self,paired_authorities=None): return m.compare_boundary(self.oracle,self.child,parent=self.parent, expected_before_sha256=self.before_sha,expected_after_sha256=self.after_sha,paired_authorities=paired_authorities) def audit(self): return m.audit_changed_pending(self.parent,self.child,**self.graph,input_pins={'synthetic-input-authority':'a'*64}) class ComparisonTests(unittest.TestCase): def test_exact_reference_suffix_and_different_raw_ring_phase(self): f=Fixture();result=f.compare() self.assertTrue(result['numerical_exact'],result['failed_checks']) self.assertTrue(result['suffix_reference_diagnostics_compared']) self.assertFalse(np.array_equal(f.child['terminal']['spike_ring'],f.oracle['terminal']['spike_ring'])) self.assertFalse(result['learning_pass_claimed']) self.assertFalse(result['changed_candidate_pending_arrival_coverage_verified']) def test_light_reports_only_observed_scope(self): f=Fixture();f.child.update(traces={},state_hashes=None,queues=None);f.child['summary']['diagnostic_mode']='light' result=f.compare();self.assertTrue(result['numerical_exact']);self.assertFalse(result['suffix_reference_diagnostics_compared']) self.assertFalse(result['light_per_tick_identity_claimed']) self.assertNotIn('suffix.state_hashes',result['checks']) def test_signed_zero_final_change_is_detected(self): f=Fixture();f.child['state']['g'][0]=0.;f.oracle['state']['g'][0]=-0. f.child['state_hashes'][-1]['g_sha256']=raw(f.child['state']['g']) f.oracle['state_hashes'][-1]['g_sha256']=raw(f.oracle['state']['g']) result=f.compare();self.assertFalse(result['pass_all']);self.assertIn('final.g',result['failed_checks']) def test_tick_shift_ref_and_event_counts_are_not_interchangeable(self): f=Fixture();old=int(f.child['state']['step'][0]);f.child['state']['step'][0]-=1 f.child['terminal']['spike_ring'][old%19,0]=0 f.child['terminal']['spike_ring'][(old-1)%19,0]=1 result=f.compare();self.assertFalse(result['numerical_exact']);self.assertIn('suffix.events.absolute_tick',result['failed_checks']) def test_reference_missing_history_is_rejected_not_false_pass(self): f=Fixture();f.child['state_hashes']=None with self.assertRaisesRegex(ValueError,'diagnostics missing'):f.compare() def test_queue_joint_header_exclusion_is_only_comparison_not_audit(self): f=Fixture();f.child['queues'][17]['joint_sha256']='1'*64 self.assertTrue(f.compare()['pass_all']) self.assertFalse(f.audit()['pass_all']) def test_wrong_live_prefix_parent_cannot_pass_suffix_only(self): f=Fixture();f.oracle['boundary_snapshot']['g'][0]+=1e-12 result=f.compare();self.assertFalse(result['pass_all']);self.assertIn('boundary.live_parent.g',result['failed_checks']) def test_original_authority_and_weight_scope_are_checked(self): f=Fixture();f.oracle['oracle_provenance']['authority_sha256']='a'*64 self.assertFalse(f.compare()['pass_all']) def pair(self,f,changed=None): original={key:{'synthetic':1} for key in m.AUTHORITY_FIELDS} original.update(schema='conditioning-updated-boundary-v1',boundary_id='reference-boundary',diagnostic_mode='reference') other=copy.deepcopy(original);other.update(boundary_id='light-boundary',diagnostic_mode='light') if changed:other.update(changed) pair=(json.dumps(original,sort_keys=True,indent=2).encode(),json.dumps(other,sort_keys=True,indent=2).encode()) f.oracle['oracle_provenance']['authority_sha256']=__import__('hashlib').sha256(pair[0]).hexdigest() f.child['summary']['boundary_authority_sha256']=__import__('hashlib').sha256(pair[1]).hexdigest() f.child.update(traces={},state_hashes=None,queues=None);f.child['summary']['diagnostic_mode']='light' return pair def test_separately_pinned_ref_light_authorities_allow_only_mode_and_id(self): f=Fixture();pair=self.pair(f) self.assertFalse(f.compare()['pass_all']) result=f.compare(pair);self.assertTrue(result['pass_all'],result['failed_checks']) audit=result['paired_authority_audit'];self.assertTrue(audit['checks']['actual_raw_file_SHA_bound']) self.assertNotEqual(audit['raw_sha256'][0],audit['canonical_sha256'][0]) self.assertEqual(audit['common_content_canonical_sha256'][0],audit['common_content_canonical_sha256'][1]) def test_paired_authority_cannot_change_parent_after_requests_or_registration(self): for key in ('parent','overlay_after','case','registration_sha256','cpu_validation','source_files','frozen_inputs'): with self.subTest(key=key): f=Fixture();pair=self.pair(f,{key:{'different':True}}) result=f.compare(pair);self.assertFalse(result['pass_all']) self.assertIn(key,result['paired_authority_audit']['disallowed_different_fields']) def test_paired_authority_raw_sha_extra_field_and_duplicate_key_are_rejected(self): f=Fixture();pair=self.pair(f) self.assertFalse(f.compare((pair[0]+b' ',pair[1]))['pass_all']) extra=json.loads(pair[1]);extra['unapproved']=True with self.assertRaisesRegex(ValueError,'exact boundary schema'):f.compare((pair[0],json.dumps(extra).encode())) duplicate=pair[1][:-1]+b',"boundary_id":"duplicate"}' with self.assertRaisesRegex(ValueError,'Duplicate authority'):f.compare((pair[0],duplicate)) f=Fixture();f.child['summary']['effective_weights_raw_sha256']='a'*64 self.assertFalse(f.compare()['pass_all']) class CoverageTests(unittest.TestCase): def test_actual_changed_accepted_crossing_witness(self): f=Fixture();result=f.audit();self.assertTrue(result['pass_all'],result['differences']) w=result['coverage']['first_witness'];self.assertEqual(w['global_edge_id'],0) self.assertEqual(w['source_spike_global_tick'],19);self.assertEqual(w['arrival_global_tick'],37) self.assertEqual(w['arrival_child_local_tick'],17);self.assertEqual(w['post_threshold_target_ref'],0) self.assertNotEqual(w['before_weight_f64_le_hex'],w['after_weight_f64_le_hex']) def test_fixed18_delay_across_all_nearby_ring_phases(self): for cut in (17,18,19,20,21): with self.subTest(cut=cut): result=Fixture(cut=cut,total=cut+41).audit() self.assertTrue(result['pass_all'],result['differences']) self.assertEqual(result['coverage']['first_witness']['arrival_child_local_tick'],17) def test_changed_candidate_without_pending_source_is_incomplete(self): f=Fixture();g=f.graph;g['candidate_edge_id'][0]=2 # source1 never spikes in this fixture _,_,rows,_,after=m._candidates(g['ptr'],g['src'],g['dst'],g['edge_id'],g['base_weights'],g['apl_mask'], g['candidate_edge_id'],g['delta_before'],g['delta_after'],Q) f.child['summary']['effective_weights_raw_sha256']=m._effective_hash(g['base_weights'],g['apl_mask'],rows,after) result=f.audit();self.assertTrue(result['conditional_ref_request_queue_exact']) self.assertEqual(result['changed_effective_numeric_count'],1) self.assertIsNone(result['coverage']['first_witness']);self.assertFalse(result['pass_all']) def test_target_spike_same_arrival_tick_rejects_changed_edge(self): f=Fixture(reject=True);result=f.audit() self.assertTrue(result['conditional_ref_request_queue_exact']);self.assertFalse(result['pass_all']) self.assertEqual(result['coverage']['rejected_changed_candidate_crossing_arrivals'],1) self.assertIsNone(result['coverage']['first_witness']) def test_zero_delta_and_signed_zero_delta_are_not_changed_edge_coverage(self): for delta in (np.float32(0.),np.float32(-0.)): f=Fixture(delta=delta);result=f.audit() self.assertFalse(result['pass_all']);self.assertEqual(result['changed_effective_numeric_count'],0) self.assertEqual(result['status'],'INCOMPLETE-CHANGED-EDGE-COVERAGE') self.assertEqual(result['changed_delta_bit_count'],1) def test_wrong_delta_rejected_against_effective_runtime_hash(self): f=Fixture();f.graph['delta_after'][0]=np.float32(-.003) with self.assertRaisesRegex(ValueError,'effective SHA'):f.audit() def test_source_array_shuffle_and_queue_order_shuffle_rejected(self): f=Fixture();f.graph['src'][0],f.graph['src'][2]=f.graph['src'][2],f.graph['src'][0] with self.assertRaisesRegex(ValueError,'CSR ordering'):f.audit() f=Fixture();f.child['queues'][17]['ordered_edge_sha256']='f'*64 result=f.audit();self.assertFalse(result['pass_all']);self.assertGreater(result['difference_count'],0) def test_wrong_delay_by_one_tick_changes_queue_and_acceptance(self): f=Fixture();f.child['queues'][17],f.child['queues'][18]=f.child['queues'][18],f.child['queues'][17] for t,row in enumerate(f.child['queues']):row['tick']=t self.assertFalse(f.audit()['pass_all']) def test_reference_absence_is_incomplete_even_with_computed_witness(self): f=Fixture();f.child.update(traces={},state_hashes=None,queues=None) result=f.audit();self.assertTrue(result['conditional_ref_request_queue_exact']) self.assertIsNotNone(result['coverage']['first_witness']);self.assertFalse(result['accepted_changed_edge_witness_verified']) def test_duplicate_or_missing_candidate_and_nonfinite_reject(self): f=Fixture();f.graph['candidate_edge_id'][0]=99 with self.assertRaisesRegex(ValueError,'Candidate missing'):f.audit() f=Fixture();f.graph['delta_after'][0]=np.nan with self.assertRaisesRegex(ValueError,'delta after'):f.audit() def test_request_acceptance_reconstructed_not_just_copied(self): f=Fixture();f.child['requests'][0,3:]=[0,1] result=f.audit();self.assertFalse(result['pass_all']) self.assertTrue(any(row['field']=='request.acceptance' for row in result['differences'])) def test_incomplete_summary_is_not_an_array_only_pass(self): f=Fixture();f.child['summary']['complete']=False with self.assertRaisesRegex(ValueError,'summary is incomplete'):f.audit() class LoaderTests(unittest.TestCase): def test_actual_frozen_loader_accepts_declared_oracle_without_fake_schema(self): f=Fixture() with tempfile.TemporaryDirectory() as tmp: root=Path(tmp);r=f.oracle save(root/'summary.json',r['summary']);np.savez_compressed(root/'state.npz',**r['state']) np.savez_compressed(root/'terminal-queues.npz',**r['terminal']) np.save(root/'apl-pending-tail.float32.npy',r['terminal']['apl_pending']) save(root/'apl-timing-audit.json',{'final_apl_state_u32_bits':r['terminal']['apl_state'].view(np.uint32).tolist()}) for name,filename in m.C.TRACE_FILES.items():np.save(root/filename,r['traces'][name]) for name,rows in (('queue.jsonl',r['queues']),('tick-state-hashes.jsonl',r['state_hashes'])): (root/name).write_text(''.join(json.dumps(row)+'\n' for row in rows),encoding='utf-8') with (root/'requests.csv').open('w',newline='') as stream: writer=csv.writer(stream);writer.writerow(m.C.REQUEST_FIELDS);writer.writerows(r['requests']) np.savez_compressed(root/'boundary-snapshot.npz',**r['boundary_snapshot']) save(root/'boundary-comparison.json',r['boundary_report']);save(root/'oracle-provenance.json',r['oracle_provenance']) loaded=m.load_record(root,kind='oracle') try: self.assertTrue(loaded['summary']['within_trial_weight_updates']) self.assertFalse(loaded['summary']['production_backend']) result=m.compare_boundary(loaded,f.child,parent=f.parent, expected_before_sha256=f.before_sha,expected_after_sha256=f.after_sha) self.assertTrue(result['pass_all']) finally:m.close_record(loaded) if __name__=='__main__':unittest.main()