Seal AO interface failure and verified isolated postprocessing diagnosis

This commit is contained in:
Digital Drosophila Archive
2026-09-30 17:57:02 +08:00
parent 4ff241bdc9
commit 8e9f8f4d56
126 changed files with 92124 additions and 3 deletions
@@ -0,0 +1,246 @@
"""Synthetic artifact comparisons only; no neural equation or GPU execution."""
import copy
import hashlib
import json
from pathlib import Path
import tempfile
import unittest
import numpy as np
import engineering_compare as c
def sha(value):
return hashlib.sha256(np.ascontiguousarray(value).tobytes()).hexdigest()
def synthetic_parts(lengths, *, active=True, traces=True, queues=True):
n, k = 3, 2
total = sum(lengths)
all_ticks = np.array([value for value in (0, 7, 19, 21, 41, 61) if value < total], np.int32) if active else np.empty(0, np.int32)
all_nodes = np.array([0, 1, 2, 0, 2, 1][:len(all_ticks)], np.int32)
ring, apl_ring = np.zeros((19, n), np.uint8), np.zeros((18, k), np.float32)
phase, offset, result = 0, 0, []
for length in lengths:
ring = ring[(np.arange(19) + phase % 19) % 19].copy()
head = phase % 18
apl_ring = np.concatenate((apl_ring[head:], apl_ring[:head]))
event_tick, event_node, requested, hashes, queue_rows = [], [], [], [], []
generated, applied, drives, apl_states = [], [], [], []
for tick in range(length):
absolute = offset + tick
fired = all_nodes[all_ticks == absolute]
ring[tick % 19] = 0; ring[tick % 19, fired] = 1
for neuron in fired:
event_tick.append(tick); event_node.append(neuron)
requested.append([tick, int(neuron), 1, 1, 0])
due = ring[(tick - 18) % 19] if absolute >= 18 else np.zeros(n, np.uint8)
edges = np.array([int(node) * 2 + i for node in np.flatnonzero(due) for i in (0, 1)], np.int64)
accepted = np.ones(len(edges), np.uint8)
queue_rows.append({'tick': tick, 'count': len(edges), 'accepted_count': len(edges),
'ordered_edge_sha256': sha(edges), 'accepted_mask_sha256': sha(accepted),
'joint_sha256': hashlib.sha256(str(tick).encode() + edges.tobytes() + accepted.tobytes()).hexdigest()})
v = np.array([-.05, -.04, -.03], np.float64)
g = np.array([-0., float(absolute), -.001], np.float64)
ref = np.zeros(n, np.int32)
hashes.append({'tick': tick, 'v_sha256': sha(v), 'g_sha256': sha(g), 'ref_sha256': sha(ref)})
h = np.full(k, np.float32(absolute + 1 if active else 0), np.float32)
application = apl_ring[tick % 18].copy(); apl_ring[tick % 18] = h
generated.append(h); applied.append(application)
drives.append(np.full(2, np.float32(absolute if active else 0), np.float32))
apl_states.append(np.full(2, np.float32(absolute + 2 if active else 0), np.float32))
local_ticks, nodes = np.array(event_tick, np.int32), np.array(event_node, np.int32)
state = {'step': local_ticks, 'neuron_idx': nodes, 'counts': np.bincount(nodes, minlength=n).astype(np.int32), 'v': v, 'g': g, 'ref': ref}
head = length % 18
pending = np.concatenate((apl_ring[head:], apl_ring[:head]))
terminal = {'spike_ring': ring.copy(), 'apl_state': apl_states[-1].copy(), 'apl_pending': pending,
'next_tick': np.asarray(length, np.int64)}
summary = {'complete': True, 'pass_all': True, 'all_finite': True, 'steps': length, 'completed_ticks': length,
'nodes': n, 'precision': 'float64', 'apl_precision': 'float32', 'apl_delay_steps': 18,
'checkpoint_continuation_used': offset > 0, 'global_tick_offset': offset, 'global_next_tick': offset + length,
'effective_weights_raw_sha256': 'e' * 64, 'diagnostic_mode': 'reference' if traces else 'light'}
result.append({'source': 'synthetic', 'steps': length, 'global_tick_offset': offset, 'state': state,
'requests': np.asarray(requested, np.int64).reshape(-1, 5), 'terminal': terminal,
'summary': summary, 'terminal_origin': 'synthetic_recorded',
'traces': {'generated': np.asarray(generated), 'applied': np.asarray(applied),
'drive': np.asarray(drives), 'state': np.asarray(apl_states)} if traces else {},
'state_hashes': hashes if traces else None, 'queues': queue_rows if queues else None})
offset += length; phase = length
return result
def write_output(path, run, *, legacy=False):
path.mkdir()
for name, value in (('summary.json', run['summary']), ('apl-timing-audit.json',
{'final_apl_state_u32_bits': run['terminal']['apl_state'].view(np.uint32).tolist()})):
(path / name).write_text(json.dumps(value), encoding='utf-8')
np.savez_compressed(path / 'state.npz', **run['state'])
np.save(path / 'apl-pending-tail.float32.npy', run['terminal']['apl_pending'])
if legacy:
(path / 'adapter-provenance.json').write_text('{}', encoding='utf-8')
else:
np.savez_compressed(path / 'terminal-queues.npz', **run['terminal'])
with (path / 'requests.csv').open('w', encoding='utf-8', newline='') as stream:
import csv
writer = csv.writer(stream); writer.writerow(c.REQUEST_FIELDS); writer.writerows(run['requests'].tolist())
for name, file in c.TRACE_FILES.items():
if name in run['traces']:
np.save(path / file, run['traces'][name])
for name, file in (('state_hashes', 'tick-state-hashes.jsonl'), ('queues', 'queue.jsonl')):
if run[name] is not None:
(path / file).write_text(''.join(json.dumps(row) + '\n' for row in run[name]), encoding='utf-8')
class Comparisons(unittest.TestCase):
def test_fresh_light_reference_exact_without_fabricating_light_traces(self):
reference = synthetic_parts([64])[0]
light = synthetic_parts([64], traces=False, queues=False)[0]
report = c.compare_fresh(light, reference)
self.assertTrue(report['pass_all'])
self.assertFalse(report['reference_comparisons_available']['APL_traces'])
self.assertFalse(report['temporal_validation_complete'])
def test_light_does_not_hide_reference_vs_AM_interior_trace_mismatch(self):
light = synthetic_parts([64], traces=False, queues=False)[0]
reference = synthetic_parts([64])[0]
legacy = copy.deepcopy(reference)
reference['traces']['drive'][10, 0] += np.float32(1.)
report = c.compare_fresh(light, reference, legacy)
self.assertFalse(report['pass_all'])
self.assertIn('reference_vs_AM.trace.drive', report['failed_checks'])
def test_nonzero_origin_monolithic_and_split_require_same_parent(self):
mono = synthetic_parts([20, 44])[1]
parts = synthetic_parts([20, 5, 39])[1:]
with self.assertRaisesRegex(ValueError, 'same starting parent'):
c.compare_segments(mono, parts)
mono['summary']['parent_checkpoint_manifest_sha256'] = 'a' * 64
parts[0]['summary']['parent_checkpoint_manifest_sha256'] = 'a' * 64
report = c.compare_segments(mono, parts)
self.assertTrue(report['pass_all'], report['failed_checks'])
self.assertEqual(report['global_tick_offset'], 20)
parts[0]['summary']['parent_checkpoint_manifest_sha256'] = 'b' * 64
with self.assertRaisesRegex(ValueError, 'same starting parent'):
c.compare_segments(mono, parts)
def test_monolithic_segments_exact_and_cross_boundary_coverage(self):
mono = synthetic_parts([64])[0]
parts = synthetic_parts([5, 12, 1, 1, 18, 19, 8])
report = c.compare_segments(mono, parts)
self.assertTrue(report['pass_all'], report['failed_checks'])
self.assertTrue(report['reference_comparisons_available']['APL_traces'])
self.assertTrue(report['reference_comparisons_available']['state_hashes'])
self.assertGreater(report['coverage']['observed_carried_arriving_edges'], 0)
self.assertGreater(report['coverage']['carried_APL_nonzero_applied_values'], 0)
self.assertFalse(report['queue_joint_hash_compared'])
self.assertNotEqual(mono['queues'][18]['joint_sha256'], parts[3]['queues'][0]['joint_sha256'])
def test_raw_ring_phase_must_be_normalized(self):
mono = synthetic_parts([64])[0]
parts = synthetic_parts([20, 44])
self.assertFalse(c.bit_equal(mono['terminal']['spike_ring'], parts[-1]['terminal']['spike_ring']))
self.assertTrue(c.bit_equal(c.normalized_ring(mono['terminal']), c.normalized_ring(parts[-1]['terminal'])))
self.assertTrue(c.compare_segments(mono, parts)['pass_all'])
def test_signed_zero_final_state_difference_is_not_equal(self):
left = synthetic_parts([64], traces=False, queues=False)[0]
right = copy.deepcopy(left); right['state']['g'][0] = 0.
report = c.compare_fresh(left, right)
self.assertFalse(report['numerical_exact'])
self.assertIn('pair.state.g', report['failed_checks'])
def test_zero_activity_is_not_a_complete_temporal_pass(self):
mono = synthetic_parts([64], active=False)[0]
parts = synthetic_parts([20, 44], active=False)
report = c.compare_segments(mono, parts)
self.assertTrue(report['numerical_exact'])
self.assertFalse(report['pass_all'])
self.assertFalse(report['temporal_validation_complete'])
self.assertEqual(report['status'], 'INCOMPLETE-COVERAGE')
self.assertFalse(c.compare_fresh(mono, copy.deepcopy(mono))['pass_all'])
def test_missing_observed_queue_coverage_is_reported_unknown_not_zero_or_pass(self):
mono = synthetic_parts([64], traces=False, queues=False)[0]
parts = synthetic_parts([20, 44], traces=False, queues=False)
report = c.compare_segments(mono, parts)
self.assertTrue(report['numerical_exact'])
self.assertIsNone(report['coverage']['observed_carried_arriving_edges'])
self.assertFalse(report['temporal_validation_complete'])
def test_request_acceptance_difference_fails_even_if_events_are_equal(self):
left = synthetic_parts([64])[0]; right = copy.deepcopy(left)
right['requests'][0, 3:] = [0, 1]
report = c.compare_fresh(left, right)
self.assertIn('pair.requests_five_fields', report['failed_checks'])
def test_clock_gaps_and_empty_segment_list_rejected(self):
mono = synthetic_parts([64])[0]
with self.assertRaises(ValueError):
c.compare_segments(mono, [])
parts = synthetic_parts([20, 44]); parts[1]['global_tick_offset'] += 1
with self.assertRaisesRegex(ValueError, 'clock gap'):
c.compare_segments(mono, parts)
def test_partial_traces_hash_coverage_and_bad_dtype_rejected(self):
for mutation in ('trace', 'hash', 'dtype', 'duplicate'):
run = synthetic_parts([64])[0]
if mutation == 'trace':
run['traces'].pop('applied')
elif mutation == 'hash':
run['state_hashes'].pop()
elif mutation == 'dtype':
run['state']['v'] = run['state']['v'].astype(np.float32)
else:
run['state']['step'][1] = run['state']['step'][0]
run['state']['neuron_idx'][1] = run['state']['neuron_idx'][0]
with self.subTest(mutation=mutation), self.assertRaises(ValueError):
c.validate_run(run)
def test_per_step_state_hash_and_queue_mask_mismatch_fail(self):
mono = synthetic_parts([64])[0]; parts = synthetic_parts([20, 44])
parts[1]['state_hashes'][1]['g_sha256'] = '0' * 64
parts[1]['queues'][1]['accepted_mask_sha256'] = '1' * 64
report = c.compare_segments(mono, parts)
self.assertFalse(report['pass_all'])
self.assertIn('concatenated_per_tick_state_hashes', report['failed_checks'])
self.assertIn('concatenated_queue_order_and_acceptance', report['failed_checks'])
def test_effective_weight_change_fails(self):
mono = synthetic_parts([64])[0]; parts = synthetic_parts([20, 44])
parts[1]['summary']['effective_weights_raw_sha256'] = 'f' * 64
self.assertFalse(c.compare_segments(mono, parts)['checks']['unchanged_effective_weights'])
class FileSchemas(unittest.TestCase):
def test_actual_five_request_fields_and_AM_derived_ring_are_labeled(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
run = synthetic_parts([64])[0]
write_output(root / 'AN', run)
write_output(root / 'AM', run, legacy=True)
actual, legacy = c.load_run(root / 'AN'), c.load_run(root / 'AM', allow_legacy_am=True)
try:
self.assertIn('derived_from_fresh_events', legacy['terminal_origin'])
self.assertTrue(c.compare_fresh(actual, copy.deepcopy(actual), legacy)['pass_all'])
with self.assertRaisesRegex(ValueError, 'missing'):
c.load_run(root / 'AM')
finally:
c.close_run(actual); c.close_run(legacy)
def test_missing_reference_file_or_wrong_request_header_rejected(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
run = synthetic_parts([64])[0]
write_output(root / 'partial', run)
(root / 'partial' / 'apl-applied.float32.npy').unlink()
with self.assertRaisesRegex(ValueError, 'incomplete'):
c.load_run(root / 'partial')
write_output(root / 'header', run)
(root / 'header' / 'requests.csv').write_text('tick,neuron_idx,accepted\n', encoding='utf-8')
with self.assertRaisesRegex(ValueError, 'five AJ'):
c.load_run(root / 'header')
if __name__ == '__main__':
unittest.main()