"""Pure CPU comparison of live-boundary and saved/restored diagnostics. Array APIs require caller-authenticated records and immutable original graph / delta arrays. They do NOT authenticate registrations, process receipts or disk seals. load_record reads existing bytes without writing; its scope tags are not authority. No GPU, learning, membrane replay or physiological inference. """ from __future__ import annotations import hashlib import json from pathlib import Path from types import ModuleType import numpy as np PINS = { 'engineering_compare.py': '5a2403f11bc08d65a9d242ac2cf9686b850e0c3680d65659f785e9b6b50c0238', 'engineering_queue_audit.py': '6ddaec52f2a0b5d0c528d51230d372fe2344160aef5eccea6a95b6c5e61a574e', } AUTHORITY_FIELDS = {'schema', 'boundary_id', 'registration_sha256', 'transition', 'diagnostic_mode', 'input_policy', 'case', 'parent', 'global_tick_offset', 'overlay_before', 'overlay_after', 'expected_effective_weight_before_sha256', 'expected_effective_weight_after_sha256', 'update', 'cpu_validation', 'source_files', 'frozen_inputs'} def _digest(path): h = hashlib.sha256() with Path(path).open('rb') as stream: for block in iter(lambda: stream.read(8 << 20), b''): h.update(block) return h.hexdigest() def _load(filename, expected): path = Path(__file__).resolve().parent.parent / 'olfactory-conditioning-lab-v1' / filename raw = path.read_bytes() if hashlib.sha256(raw).hexdigest() != expected: raise ValueError('Frozen pure helper source differs: ' + filename) module = ModuleType('conditioning_boundary_' + filename[:-3]) module.__file__ = str(path) exec(compile(raw, str(path), 'exec'), module.__dict__) return module C = _load('engineering_compare.py', PINS['engineering_compare.py']) require, bit_equal = C.require, C.bit_equal def _raw(a): h = hashlib.sha256() for start in range(0, len(a), 1 << 18): h.update(np.ascontiguousarray(a[start:start+(1 << 18)], dtype=a.dtype.newbyteorder('<')).tobytes()) return h.hexdigest() def load_record(directory, *, kind): """Read actual inherited schemas; never disguise oracle as fixed-weight. No external SHA pin is accepted or inferred here. The caller independently verifies the complete oracle/new/old checkpoint seal before this reader. """ require(kind in ('oracle', 'updated', 'parent'), 'Explicit record kind required') record = C.load_run(directory) try: summary = record['summary'] if kind == 'oracle': require(summary.get('diagnostic_live_boundary') is True and summary.get('within_trial_weight_updates') is True and summary.get('production_backend') is False and summary.get('restored_from_checkpoint') is False, 'Not a declared live-boundary diagnostic') record['boundary_snapshot'] = C._npz(Path(directory)/'boundary-snapshot.npz', ('v','g','ref','counts','spike_ring','apl_state','apl_pending')) record['boundary_report'] = C._json(Path(directory)/'boundary-comparison.json') record['oracle_provenance'] = C._json(Path(directory)/'oracle-provenance.json') else: require(summary.get('within_trial_weight_updates') is False, 'Checkpoint segment updated during run') if kind == 'updated': require(summary.get('updated_boundary_schema') == 'conditioning-updated-boundary-v1' and summary.get('checkpoint_continuation_used') is True, 'Not an updated checkpoint segment') record['manifest_sha256'] = _digest(Path(directory)/'checkpoint-manifest.json') record['read_scope'] = kind return record except BaseException: C.close_run(record) raise def close_record(record): C.close_run(record) def _checks(): checks = {} def check(name, okay): checks[name] = bool(okay) return checks, check def _complete(record): C.validate_run(record) s=record.get('summary',{}) require(s.get('complete') is True and s.get('pass_all') is True and s.get('all_finite') is True and type(s.get('steps')) is int and s['steps']==record['steps']==s.get('completed_ticks') and type(s.get('nodes')) is int and s['nodes']==len(record['state']['v']) and s.get('precision')=='float64' and s.get('apl_precision')=='float32' and s.get('apl_delay_steps')==18, 'Record summary is incomplete or has wrong numerical scope') def _canonical(value): return json.dumps(value,sort_keys=True,separators=(',',':'),ensure_ascii=False,allow_nan=False).encode('utf-8') def _paired_authorities(pair, oracle_sha, child_sha, child_mode): """Bind actual raw JSON bytes and compare canonical content, not self hashes. File SHA and canonical SHA are intentionally distinct. Only diagnostic_mode and boundary_id may differ; every other byte of canonical JSON must agree. """ require(type(pair) in (tuple,list) and len(pair)==2 and all(type(raw)is bytes for raw in pair), 'paired_authorities must be the two original raw JSON byte strings') def unique(items): result={} for key,value in items: require(key not in result,'Duplicate authority JSON key: '+key);result[key]=value return result values=[json.loads(raw.decode('utf-8'),object_pairs_hook=unique, parse_constant=lambda value:(_ for _ in ()).throw(ValueError('Nonfinite authority JSON'))) for raw in pair] for value in values: require(type(value)is dict and set(value)==AUTHORITY_FIELDS and value.get('schema')=='conditioning-updated-boundary-v1', 'Paired authority must use the exact boundary schema') require(type(value['boundary_id'])is str and bool(value['boundary_id']) and value['boundary_id'].strip()==value['boundary_id'], 'Invalid paired boundary ID') hashes=[hashlib.sha256(raw).hexdigest() for raw in pair] allowed={'diagnostic_mode','boundary_id'} normalized=[{k:v for k,v in value.items() if k not in allowed} for value in values] differences=[key for key in sorted(AUTHORITY_FIELDS-allowed) if _canonical(values[0][key])!=_canonical(values[1][key])] checks=dict(actual_raw_file_SHA_bound=hashes==[oracle_sha,child_sha], explicit_modes=values[0]['diagnostic_mode']=='reference' and values[1]['diagnostic_mode']==child_mode, only_two_allowed_fields_differ=_canonical(normalized[0])==_canonical(normalized[1])) return dict(pass_all=all(checks.values()),checks=checks,raw_sha256=hashes, canonical_sha256=[hashlib.sha256(_canonical(value)).hexdigest() for value in values], common_content_canonical_sha256=[hashlib.sha256(_canonical(value)).hexdigest() for value in normalized], allowed_differences=sorted(allowed),disallowed_different_fields=differences, actual_disk_or_registration_authority_verified_here=False) def compare_boundary(oracle, continuation, *, parent, expected_before_sha256, expected_after_sha256, paired_authorities=None): """Compare full oracle prefix/suffix against an authenticated parent/child. Both reference and light children are supported. A light child's absent intermediate traces/hashes are explicitly unobserved, never called exact. No nonvacuity gate is invented here; changed-edge coverage is separate. """ for record in (oracle, continuation, parent): _complete(record) require(C._sha(expected_before_sha256) and C._sha(expected_after_sha256), 'External expected weight identities required') require(C._sha(parent.get('manifest_sha256')), 'Caller-authenticated parent manifest identity required') require(parent['global_tick_offset'] == oracle['global_tick_offset'] == 0, 'Need original fresh parent/live oracle') cut, steps = parent['steps'], continuation['steps'] require(continuation['global_tick_offset'] == cut and oracle['steps'] == cut+steps, 'Wrong boundary/duration') n = len(parent['state']['v']) require(len(oracle['state']['v']) == len(continuation['state']['v']) == n, 'Population differs') require(oracle.get('traces') and oracle.get('state_hashes') is not None and oracle.get('queues') is not None, 'Live oracle must retain full reference diagnostics') os, cs, ps = oracle['summary'], continuation['summary'], parent['summary'] require(cs.get('diagnostic_mode') in ('reference','light'), 'Child recording mode is undeclared') reference = cs['diagnostic_mode'] == 'reference' if reference: require(continuation.get('traces') and continuation.get('state_hashes') is not None and continuation.get('queues') is not None, 'Reference child diagnostics missing') else: require(not continuation.get('traces') and continuation.get('state_hashes') is None and continuation.get('queues') is None, 'Light scope must not manufacture intermediate histories') if ps.get('diagnostic_mode')=='reference': require(parent.get('traces') and parent.get('state_hashes') is not None and parent.get('queues') is not None, 'Reference parent diagnostics missing') checks, check = _checks() check('oracle.explicit_live_scope', os.get('diagnostic_mode') == 'reference' and os.get('diagnostic_live_boundary') is True and os.get('within_trial_weight_updates') is True and os.get('explicit_weight_switches') == 1 and type(os.get('explicit_weight_switches')) is int and os.get('boundary_tick') == cut and os.get('restored_from_checkpoint') is False and os.get('production_backend') is False) check('child.explicit_updated_scope', cs.get('within_trial_weight_updates') is False and cs.get('checkpoint_continuation_used') is True and cs.get('updated_boundary_schema') == 'conditioning-updated-boundary-v1' and cs.get('parent_checkpoint_manifest_sha256') == parent['manifest_sha256'] and cs.get('global_next_tick') == cut+steps and cs.get('boundary_transition') == 'epoch_update') check('weights.before', ps.get('effective_weights_raw_sha256') == os.get('initial_effective_weights_raw_sha256') == expected_before_sha256) check('weights.after', cs.get('effective_weights_raw_sha256') == os.get('effective_weights_raw_sha256') == expected_after_sha256) ot, on = oracle['state']['step'], oracle['state']['neuron_idx'] split = int(np.searchsorted(ot,cut)) check('prefix.events.tick',bit_equal(ot[:split],parent['state']['step'])) check('prefix.events.neuron_idx',bit_equal(on[:split],parent['state']['neuron_idx'])) check('prefix.counts',np.array_equal(np.bincount(on[:split],minlength=n),parent['state']['counts'])) check('suffix.events.absolute_tick',np.array_equal(continuation['state']['step'].astype(np.int64)+cut,ot[split:].astype(np.int64))) check('suffix.events.neuron_idx',bit_equal(continuation['state']['neuron_idx'],on[split:])) check('suffix.segment_counts',np.array_equal(continuation['state']['counts'],np.bincount(on[split:],minlength=n))) check('prefix_plus_suffix.total_counts',np.array_equal(parent['state']['counts'].astype(np.int64)+continuation['state']['counts'],oracle['state']['counts'])) for name in ('v','g','ref'):check('final.'+name,bit_equal(continuation['state'][name],oracle['state'][name])) check('final.phase_normalized_spike_ring',bit_equal(C.normalized_ring(continuation['terminal']),C.normalized_ring(oracle['terminal']))) for name in ('apl_pending','apl_state'):check('final.'+name,bit_equal(continuation['terminal'][name],oracle['terminal'][name])) snapshot = oracle.get('boundary_snapshot') require(type(snapshot) is dict and set(snapshot) == {'v','g','ref','counts','spike_ring','apl_state','apl_pending'}, 'Missing live boundary snapshot') for name in ('v','g','ref','counts'):check('boundary.live_parent.'+name,bit_equal(snapshot[name],parent['state'][name])) for name in ('spike_ring','apl_state','apl_pending'):check('boundary.live_parent.'+name,bit_equal(snapshot[name],parent['terminal'][name])) report, provenance = oracle.get('boundary_report'), oracle.get('oracle_provenance') require(type(report) is dict and type(provenance) is dict, 'Missing explicit oracle provenance/report') paired=None authority_matches=(C._sha(cs.get('boundary_authority_sha256')) and provenance.get('authority_sha256')==cs['boundary_authority_sha256']) if paired_authorities is not None: paired=_paired_authorities(paired_authorities,provenance.get('authority_sha256'), cs.get('boundary_authority_sha256'),cs['diagnostic_mode']) authority_matches=paired['pass_all'] check('oracle.boundary_metadata',report.get('complete') is True and report.get('boundary_tick') == cut and report.get('parent_checkpoint_manifest_sha256') == parent['manifest_sha256'] and report.get('restore_called') is False and report.get('initial_state_replaced') is False and report.get('queues_replaced') is False and report.get('existing_weights_replaced_once') is True and report.get('before_effective_sha256') == expected_before_sha256 and report.get('after_effective_sha256') == expected_after_sha256) check('oracle.authority_matches_child',provenance.get('schema') == 'conditioning-live-boundary-oracle-v1' and provenance.get('complete') is True and provenance.get('restore_called') is False and provenance.get('prefix_sparse_events_exact') is True and provenance.get('learning_pass_claimed') is False and authority_matches) for label,record,start,stop in (('prefix',parent,0,cut),('suffix',continuation,cut,cut+steps)): req=oracle['requests'];lo,hi=np.searchsorted(req[:,0],(start,stop)) actual=record['requests'].copy();actual[:,0]+=start check(label+'.requests_all_five_fields',bit_equal(actual,req[lo:hi])) if record.get('traces'): for name in C.TRACE_FILES:check(label+'.trace.'+name,bit_equal(record['traces'][name],oracle['traces'][name][start:stop])) if record.get('state_hashes') is not None: check(label+'.state_hashes',all(all(row[key]==oracle['state_hashes'][start+t][key] for key in C.HASH_FIELDS) for t,row in enumerate(record['state_hashes']))) if record.get('queues') is not None: check(label+'.queue_order_acceptance',all(all(row[key]==oracle['queues'][start+t][key] for key in C.QUEUE_FIELDS) for t,row in enumerate(record['queues']))) consume=min(steps,18) if reference:check('boundary.applied_parent_APL',bit_equal(continuation['traces']['applied'][:consume],parent['terminal']['apl_pending'][:consume])) if steps<18:check('boundary.unconsumed_parent_APL',bit_equal(continuation['terminal']['apl_pending'][:18-steps],parent['terminal']['apl_pending'][steps:])) if steps<19:check('boundary.unwritten_parent_spike_slots',bit_equal(continuation['terminal']['spike_ring'][steps:],C.normalized_ring(parent['terminal'])[steps:])) exact=all(checks.values()) return dict(schema='conditioning-live-vs-restored-comparison-v1',pass_all=exact,numerical_exact=exact, status='EXACT-RECORDED-BOUNDARY-SCOPE-NOT-LEARNING' if exact else 'FAIL-BOUNDARY-NUMERICAL',checks=checks, failed_checks=[k for k,v in checks.items() if not v],boundary_tick=cut,suffix_steps=steps,neurons=n, child_mode=cs['diagnostic_mode'],suffix_reference_diagnostics_compared=reference, paired_authority_audit=paired, light_intermediate_states_observed=False,light_per_tick_identity_claimed=False, queue_joint_hash_compared=False,queue_joint_hash_exclusion='Local tick headers differ; independent audit checks each actual header', changed_candidate_pending_arrival_coverage_verified=False,external_file_authority_verified_here=False, independent_membrane_replay_performed=False,learning_pass_claimed=False,candidate_generated=False) def _effective_hash(base, apl, rows, values): h=hashlib.sha256() for start in range(0,len(base),1<<18): stop=min(start+(1<<18),len(base));a=np.array(base[start:stop],copy=True) a[apl[start:stop]]=np.float64(0) lo,hi=np.searchsorted(rows,(start,stop));a[rows[lo:hi]-start]=values[lo:hi] h.update(np.ascontiguousarray(a,dtype='0 and np.all(ids>=0) and np.all(ids[1:]>ids[:-1]),'Candidate IDs must preserve canonical sorted unique order') for name,a in (('before',before),('after',after)): require(isinstance(a,np.ndarray) and a.dtype==np.float32 and a.shape==ids.shape and np.isfinite(a).all() and np.all(a<=0),'Invalid f32 mV delta '+name) require(np.all(after<=before),'This diagnostic admits only existing depression, not potentiation') rows=np.searchsorted(edge_id,ids) require(np.all(rows0),'Candidate overlaps APL or has nonpositive original weight') values=[] for delta in (before,after): a=base[rows].copy();nz=delta!=np.float32(0) a[nz]=np.add(a[nz],np.multiply(delta[nz].astype(np.float64),np.float64(.001))) require(np.isfinite(a).all() and np.all(a>0),'Effective candidate weight changed sign/nonfinite') values.append(a) return n,e,rows,values[0],values[1] def audit_changed_pending(parent,child,*,ptr,src,dst,edge_id,base_weights,apl_mask, candidate_edge_id,delta_before,delta_after,input_pins): """Conditional ref/request/CSR audit plus a changed-edge crossing witness. Post-threshold ref=22 precedes arrivals; requests use prior ref<=1. Every real edge remains in source/CSR order, including rejected/zero weights. Witnesses require an actual numeric effective-weight change and accepted arrival emitted before cut and arriving within the child. This is not a unique-cause claim and does not independently predict membrane spikes. """ require(type(input_pins)is dict and input_pins and all(type(k)is str and C._sha(v) for k,v in input_pins.items()),'Caller input anchors required') Q=_load('engineering_queue_audit.py',PINS['engineering_queue_audit.py']) for record in (parent,child):_complete(record) n,e,rows,before,after=_candidates(ptr,src,dst,edge_id,base_weights,apl_mask,candidate_edge_id,delta_before,delta_after,Q) require(parent['global_tick_offset']==0 and child['global_tick_offset']==parent['steps'],'Fresh parent / exact child boundary required') require(len(parent['state']['v'])==len(child['state']['v'])==n,'Record/graph population mismatch') require(all(type(record['summary'].get('edges')) is int and record['summary']['edges']==e for record in (parent,child)), 'Record/graph edge count differs') cut,total=parent['steps'],parent['steps']+child['steps'] require(total<=np.iinfo(np.int32).max,'Diagnostic clock exceeds exact int32 event range') before_sha=_effective_hash(base_weights,apl_mask,rows,before);after_sha=_effective_hash(base_weights,apl_mask,rows,after) require(parent['summary'].get('effective_weights_raw_sha256')==before_sha and child['summary'].get('effective_weights_raw_sha256')==after_sha,'Delta/base arithmetic mismatches actual runtime effective SHA') changed=(before.view(np.uint64)!=after.view(np.uint64)) & (before!=after) changed_rows=rows[changed];changed_ids=candidate_edge_id[changed] effective=base_weights.copy();effective[apl_mask]=np.float64(0);effective[rows]=after tick=np.concatenate((parent['state']['step'],child['state']['step']+np.int32(cut))) node=np.concatenate((parent['state']['neuron_idx'],child['state']['neuron_idx'])) request=np.concatenate((parent['requests'],child['requests']+np.array([cut,0,0,0,0],np.int64))) ref=np.zeros(n,np.int32);differences=[];difference_count=0 def check(ok,field,t): nonlocal difference_count if not ok: difference_count+=1 if len(differences)<60:differences.append(dict(field=field,global_tick=t)) coverage=dict(changed_candidate_crossing_arrivals=0,accepted_changed_candidate_crossing_arrivals=0, rejected_changed_candidate_crossing_arrivals=0,first_witness=None) full=all(record.get('queues') is not None and record.get('state_hashes') is not None for record in (parent,child)) for t in range(total): record,local=(parent,t) if t=18 else node[:0] qrows,_,_=Q._queue(t,local,sources,ref,ptr,dst,edge_id,effective,[]) expected_queue=qrows[0] if tlen(differences),coverage=coverage,neurons=n,edges=e,boundary_tick=cut, changed_delta_bit_count=int(np.count_nonzero(delta_before.view(np.uint32)!=delta_after.view(np.uint32))), changed_effective_numeric_count=int(changed.sum()),before_effective_sha256=before_sha,after_effective_sha256=after_sha, supplied_input_pins=dict(input_pins),array_raw_sha256={key:_raw(a) for key,a in dict(ptr=ptr,src=src,dst=dst,edge_id=edge_id, base_weights=base_weights,apl_mask=apl_mask,candidate_edge_id=candidate_edge_id,delta_before=delta_before,delta_after=delta_after).items()}, delay_steps=18,refractory_steps=22,actual_membrane_dynamics_independently_replayed=False, observed_spikes_condition_the_audit=True,external_file_authority_verified_here=False,learning_pass_claimed=False)