Files
digital-drosophila/registry/phase4ao-conditioning-interface/source-snapshot/test_engineering_queue_audit.py
T
2026-09-30 17:57:02 +08:00

230 lines
12 KiB
Python

"""Small synthetic CSR/record tests only; no brain, GPU or artifact writes."""
import ast
import copy
import hashlib
import json
from pathlib import Path
import struct
import unittest
import numpy as np
import engineering_queue_audit as audit
def digest(array):
return hashlib.sha256(np.ascontiguousarray(array).tobytes()).hexdigest()
def queue_row(tick, edges, accepted):
# Independent simple whole-payload construction, unlike the chunked audit.
edge_bytes = np.asarray(edges, dtype='<i8').tobytes()
mask_bytes = np.asarray(accepted, dtype=np.uint8).tobytes()
return dict(tick=tick, count=len(edges), accepted_count=int(sum(accepted)),
ordered_edge_sha256=hashlib.sha256(edge_bytes).hexdigest(),
accepted_mask_sha256=hashlib.sha256(mask_bytes).hexdigest(),
joint_sha256=hashlib.sha256(b'phase4af-queue-v1\0' + struct.pack('<qq', tick, len(edges)) + edge_bytes + mask_bytes).hexdigest())
def fixture(*, target_fire=True, weights=None, events=None, cuts=(5, 18, 19, 37)):
ptr = np.array([0, 2, 3, 4, 5], np.int64)
dst = np.array([2, 1, 2, 0, 2], np.int32)
edge_id = np.array([10, 11, 20, 30, 40], np.int64)
weights = np.array([.001, 0., -.002, .003, .004], np.float64) if weights is None else weights
total, n = 60, 4
if events is None:
events = [(0, 0), (0, 1), *(([(18, 2)]) if target_fire else []), (22, 0), (23, 1), (44, 0)]
events = sorted(events)
request_events = sorted(set(events + [(1, 0)]))
ticks, nodes = np.array([x[0] for x in events], np.int32), np.array([x[1] for x in events], np.int32)
requests = {'tick': np.array([x[0] for x in request_events], np.int32),
'neuron_idx': np.array([x[1] for x in request_events], np.int32)}
refs, expanded, masks, request_rows = [], [], [], []
last = np.full(n, -10000, np.int64)
for tick in range(total):
for when, neuron in request_events:
if when == tick:
accepted = int(tick - last[neuron] >= 22)
request_rows.append([tick, neuron, 1, accepted, 1 - accepted])
for when, neuron in events:
if when == tick:
last[neuron] = tick
ref = np.maximum(22 - (tick - last), 0).astype(np.int32)
refs.append(ref)
edges = [j for when, neuron in events if when == tick - 18 for j in range(int(ptr[neuron]), int(ptr[neuron + 1]))]
expanded.append(np.asarray(edges, np.int64))
masks.append((ref[dst[edges]] == 0).astype(np.uint8))
def record(start, stop):
selected = (ticks >= start) & (ticks < stop)
ev_t, ev_n = (ticks[selected] - start).copy(), nodes[selected].copy()
rows = np.array([row for row in request_rows if start <= row[0] < stop], np.int64).reshape(-1, 5)
rows[:, 0] -= start
return {'steps': stop - start, 'global_tick_offset': start,
'summary': {'pass_all': False}, # Never treated as authority.
'state': {'step': ev_t, 'neuron_idx': ev_n, 'counts': np.bincount(ev_n, minlength=n).astype(np.int32),
'v': np.zeros(n, np.float64), 'g': np.zeros(n, np.float64), 'ref': refs[stop - 1].copy()},
'requests': rows,
'queues': [queue_row(t - start, edge_id[expanded[t]], masks[t]) for t in range(start, stop)],
'state_hashes': [{'tick': t - start, 'v_sha256': digest(np.zeros(n, np.float64)),
'g_sha256': digest(np.zeros(n, np.float64)), 'ref_sha256': digest(refs[t])} for t in range(start, stop)]}
intervals = list(zip([0, *cuts], [*cuts, total]))
return {'mono': record(0, total), 'parts': [record(a, b) for a, b in intervals], 'ptr': ptr, 'dst': dst,
'edge_id': edge_id, 'effective_weights': weights, 'requests': requests, 'cuts': list(cuts)}
def run(data):
return audit.audit_queues(data['mono'], data['parts'], **{k: data[k] for k in
('ptr', 'dst', 'edge_id', 'effective_weights', 'requests', 'cuts')})
class QueueAudit(unittest.TestCase):
def test_real_edge_witness_and_local_headers_cross_18_19(self):
data = fixture()
result = run(data)
self.assertTrue(result['pass_all'], result['differences'])
witness = result['first_witness']
self.assertEqual((witness['boundary_tick'], witness['source_spike_tick'], witness['arrival_tick']), (37, 22, 40))
self.assertEqual((witness['source_neuron_idx'], witness['target_neuron_idx'], witness['global_edge_id']), (0, 2, 10))
self.assertEqual(witness['effective_weight_float64_le_hex'], np.asarray(.001, dtype='<f8').tobytes().hex())
self.assertFalse(result['membrane_dynamics_independently_replayed'])
self.assertFalse(result['v_g_hashes_independently_verified'])
self.assertEqual(result['rejected_requests'], 1)
# The tick44 source has arrival62, beyond this 60-tick record; no tail
# simulation or accidental drain is performed by the audit.
self.assertEqual(result['arriving_edges'], 7)
self.assertEqual(result['accepted_arriving_edges'], 2)
json.dumps(result, allow_nan=False)
self.assertNotEqual(data['mono']['queues'][18]['joint_sha256'], data['parts'][2]['queues'][0]['joint_sha256'])
def test_current_threshold_reset_rejects_arrival_and_available_target_accepts(self):
rejected = fixture(); accepted = fixture(target_fire=False)
self.assertEqual(rejected['mono']['queues'][18]['accepted_count'], 0)
self.assertEqual(accepted['mono']['queues'][18]['accepted_count'], 2)
self.assertTrue(run(rejected)['pass_all'])
witness = run(accepted)['first_witness']
self.assertEqual((witness['source_spike_tick'], witness['arrival_tick']), (0, 18))
def test_zero_weight_edges_stay_in_queue_but_cannot_supply_witness(self):
data = fixture(weights=np.zeros(5, np.float64))
result = run(data)
self.assertTrue(result['queue_ref_request_exact'])
self.assertGreater(result['arriving_edges'], 0)
self.assertFalse(result['pass_all'])
self.assertEqual(result['status'], 'INCOMPLETE-NO-ACCEPTED-NONZERO-CROSSING')
self.assertIsNone(result['first_witness'])
def test_target_sorted_or_reversed_queue_fails_even_when_both_outputs_agree(self):
data = fixture()
bad = queue_row(18, [20, 11, 10], [0, 0, 0])
data['mono']['queues'][18] = bad
data['parts'][2]['queues'][0] = queue_row(0, [20, 11, 10], [0, 0, 0])
result = run(data)
self.assertFalse(result['pass_all'])
self.assertTrue(any(d['field'] == 'queue.ordered_edge_sha256' for d in result['differences']))
def test_global_tick_in_segment_joint_header_fails(self):
data = fixture()
data['parts'][2]['queues'][0]['joint_sha256'] = data['mono']['queues'][18]['joint_sha256']
result = run(data)
self.assertEqual(result['difference_count'], 1)
self.assertEqual(result['differences'][0]['field'], 'queue.joint_sha256')
def test_fake_request_acceptance_and_ref_hash_fail_independently(self):
for mutate in ('request', 'hash', 'terminal'):
data = fixture()
if mutate == 'request':
data['parts'][0]['requests'][2, 3:] = [1, 0]
elif mutate == 'hash':
data['parts'][1]['state_hashes'][3]['ref_sha256'] = 'a' * 64
else:
data['parts'][0]['state']['ref'][0] = 0
with self.subTest(mutate=mutate):
self.assertFalse(run(data)['pass_all'])
def test_impossible_spikes_during_ref_fail_even_with_self_consistent_records(self):
data = fixture(events=[(0, 0), (1, 0), (23, 0)])
result = run(data)
self.assertFalse(result['pass_all'])
self.assertTrue(any(d['field'] == 'spike_during_refractory' for d in result['differences']))
def test_original_accepted_request_cannot_disappear_from_all_outputs(self):
data = fixture()
rows = sorted(zip(data['requests']['tick'].tolist(), data['requests']['neuron_idx'].tolist()))
rows.append((4, 3)); rows.sort()
data['requests'] = {'tick': np.array([r[0] for r in rows], np.int32),
'neuron_idx': np.array([r[1] for r in rows], np.int32)}
result = run(data)
self.assertFalse(result['pass_all'])
self.assertTrue(any(d['field'] == 'accepted_request_without_spike' for d in result['differences']))
def test_segment_events_cannot_cancel_or_substitute_nodes(self):
data = fixture()
data['parts'][0]['state']['neuron_idx'][1] = 3
data['parts'][0]['state']['counts'] = np.array([1, 0, 0, 1], np.int32)
result = run(data)
self.assertFalse(result['pass_all'])
self.assertTrue(any(d['field'] == 'events.neuron_idx' for d in result['differences']))
def test_missing_queue_or_wrong_cuts_fail_closed(self):
for mutate in ('missing', 'cuts', 'offset', 'dtype', 'nan'):
data = fixture()
if mutate == 'missing':
data['parts'][0]['queues'] = None
elif mutate == 'cuts':
data['cuts'][1] += 1
elif mutate == 'offset':
data['mono']['global_tick_offset'] = 1
elif mutate == 'dtype':
data['effective_weights'] = data['effective_weights'].astype(np.float32)
else:
data['effective_weights'][0] = np.nan
with self.subTest(mutate=mutate), self.assertRaises(ValueError):
run(data)
def test_negative_nonzero_edge_is_valid_witness_without_valence_claim(self):
data = fixture(target_fire=False)
data['effective_weights'][:2] = 0
result = run(data)
self.assertTrue(result['pass_all'])
self.assertEqual(result['first_witness']['global_edge_id'], 20)
self.assertLess(result['first_witness']['effective_weight_SI_volt'], 0)
self.assertFalse(result['unique_causal_or_necessity_claim'])
def test_input_arrays_are_not_modified(self):
data = fixture(); original = copy.deepcopy(data)
run(data)
for key in ('ptr', 'dst', 'edge_id', 'effective_weights'):
self.assertEqual(data[key].tobytes(), original[key].tobytes())
for key in data['requests']:
self.assertEqual(data['requests'][key].tobytes(), original['requests'][key].tobytes())
self.assertEqual(data['mono']['state']['ref'].tobytes(), original['mono']['state']['ref'].tobytes())
def test_frozen_AF_queue_definition_AST_crosscheck_and_chunk_order(self):
source = Path(__file__).resolve().parents[1] / 'author-fullgraph-lab-v1' / 'oracle_full.py'
raw = source.read_bytes()
self.assertEqual(hashlib.sha256(raw).hexdigest(), audit.AF_QUEUE_SOURCE_SHA256)
tree = ast.parse(raw)
definition = [n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == 'queue_summary']
self.assertEqual(len(definition), 1)
namespace = dict(np=np, hashlib=hashlib, struct=struct, BLOCK_BYTES=8 << 20, QUEUE_HEADER=b'phase4af-queue-v1\0')
exec(compile(ast.Module(body=definition, type_ignores=[]), str(source), 'exec'), namespace)
data = fixture()
saved = audit.CHUNK
try:
audit.CHUNK = 1 # Exercise edge pass then mask pass across boundaries.
for tick, local, sources, ref in [(18, 0, np.array([0, 1]), np.array([4, 4, 22, 0], np.int32)),
(40, 3, np.array([0]), np.zeros(4, np.int32)),
(0, 0, np.empty(0, np.int32), np.zeros(4, np.int32))]:
rows, _, _ = audit._queue(tick, local, sources, ref, data['ptr'], data['dst'], data['edge_id'], data['effective_weights'], [])
indices = np.array([j for s in sources for j in range(data['ptr'][s], data['ptr'][s + 1])], np.int64)
accepted = (ref[data['dst'][indices]] == 0).astype(np.uint8)
for at, row in zip((tick, local), rows):
self.assertEqual(row, namespace['queue_summary'](at, data['edge_id'][indices], accepted, chunk_bytes=8))
finally:
audit.CHUNK = saved
if __name__ == '__main__':
unittest.main()