Files
2026-09-30 13:18:18 +08:00

410 lines
23 KiB
Python

"""CPU-only synthetic tests: no Torch/Brian import, GPU, remote, or brain run."""
import copy
import hashlib
import importlib.util
import json
from pathlib import Path
import sys
import tempfile
import types
import unittest
from unittest import mock
import numpy as np
import runtime_bridge as bridge
HERE = Path(__file__).absolute().parent
AJ = HERE.parent / 'apl-timing-lab-v1'
def write_json(path, data):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data), encoding='utf-8')
def spec(root, name='cal-physiological-00-s51101'):
return dict(name=name, kind='calibration', steps=1000, dt_ms=.1, duration_ms=100.,
model={'fixed': True}, graph_dir=str(root / 'graph'), apl_bundle_dir=str(root / 'bundle'),
coefficients_path=str(root / 'coefficients.json'), science_python=str(root / 'venv/python'),
legacy_apl_worker=str(root / 'legacy_apl_worker.py'),
apl={'tau_ms': 20., 'gain': .01, 'normalization': 1.}, apl_delay_steps=18,
record_history=False, training=False, precision='float64', apl_precision='float32',
budget_root=str(root / 'old-exp'), output_budget_bytes=1024)
def inventory(root):
cfg = dict(control_cases=['control-phys', 'control-matched'], validation={
'cohorts': list(bridge.COHORTS), 'seeds': [101, 102, 103, 104, 105],
'modes': ['physiological', 'matched_energy'], 'odor_count': 6,
'repeat_first_seed_each_odor': True})
controls = [dict(name=name, source_case=str(root / 'old-cases' / name),
source_output=str(root / 'old-gpu' / name)) for name in cfg['control_cases']]
rows = [dict(cohort=cohort, mode=mode, odor_index=oi, seed=seed,
name=f'{cohort}-{mode}-{oi:02d}-s{seed}') for cohort in bridge.COHORTS
for mode in cfg['validation']['modes'] for oi in range(6) for seed in cfg['validation']['seeds']]
write_json(root / 'protocol/control-cases.json', controls)
write_json(root / 'protocol/validation-cases.json', rows)
return cfg, rows
def gate(root, stage, **values):
write_json(root / 'audit' / (stage + '-gate.json'), dict(stage=stage, pass_all=True, **values))
def seed_summary(root, cfg, rows):
base = bridge.cohort_rows(rows, cfg, 'seed_validation')
repeated = [row for row in base if row['seed'] == cfg['validation']['seeds'][0]]
pairs = [(row['name'], 0) for row in base] + [(row['name'], 1) for row in repeated]
return dict(phase='4AK', cohort='seed_validation', apl_delay_steps=18, duration_ms=100.,
seeds=cfg['validation']['seeds'], pass_all=True, execution_gate_pass=True,
representation_gate_pass=True, numerical_integrity_pass=True, exact_repeat_pass=True,
expected_base_trials=60, valid_base_trials=60, expected_repeats=12, checked_repeats=12,
checked_timing_trials=72, runs=[dict(case=row['name'], cohort='seed_validation',
validation_cohort='seed_validation') for row in base],
repeats=[dict(case=row['name'], pass_all=True) for row in repeated],
timing_checks=[dict(case=name, repeat=repeat, pass_all=True, apl_delay_steps=18, steps=1000)
for name, repeat in pairs], input_output_provenance=[dict(case=name, repeat=repeat) for name, repeat in pairs],
modes={mode: dict(gate=True) for mode in cfg['validation']['modes']},
prerequisite_input_sha256={'validation_inventory': bridge.digest(root / 'protocol/validation-cases.json')})
def bind_seed_gate(root, summary):
path = root / 'validation/seed_validation/summary.json'
write_json(path, summary)
gate(root, 'seed-validation', cohort='seed_validation', execution_gate_pass=True,
representation_gate_pass=True, summary='validation/seed_validation/summary.json',
summary_sha256=bridge.digest(path), validation_inventory_sha256=bridge.digest(root / 'protocol/validation-cases.json'))
class LoaderTests(unittest.TestCase):
def test_verified_import_preserves_file_and_globals_without_pyc(self):
with tempfile.TemporaryDirectory() as temporary:
path = Path(temporary) / 'sealed.py'
data = b'VALUE = 37\n'
path.write_bytes(data)
name = '_test_phase4ak_sealed'
try:
module = bridge.load_checked(name, path, hashlib.sha256(data).hexdigest())
self.assertEqual(module.VALUE, 37)
self.assertEqual(Path(module.__file__), path)
self.assertIs(bridge.load_checked(name, path, hashlib.sha256(data).hexdigest()), module)
self.assertNotIn('__phase4ak_loaded_sha256__', module.__dict__)
self.assertFalse((path.parent / '__pycache__').exists())
finally:
sys.modules.pop(name, None); bridge._LOADED.pop(name, None)
def test_sha_checked_before_execution(self):
with tempfile.TemporaryDirectory() as temporary:
path = Path(temporary) / 'sealed.py'
path.write_text('raise RuntimeError("must not execute")', encoding='utf-8')
with self.assertRaisesRegex(ValueError, 'SHA mismatch'):
bridge.load_checked('_test_bad_pin', path, '0' * 64)
self.assertNotIn('_test_bad_pin', sys.modules)
def test_existing_name_collision_is_not_silently_replaced(self):
with tempfile.TemporaryDirectory() as temporary:
path = Path(temporary) / 'sealed.py'
path.write_text('VALUE = 1', encoding='utf-8')
old = types.ModuleType('_test_collision')
sys.modules[old.__name__] = old
try:
with self.assertRaisesRegex(ValueError, 'unverified module'):
bridge.load_checked(old.__name__, path, bridge.digest(path))
self.assertIs(sys.modules[old.__name__], old)
finally:
sys.modules.pop(old.__name__, None)
def test_real_pure_sealed_module_load_does_not_import_torch_or_brian(self):
self.assertNotIn('torch', sys.modules)
self.assertNotIn('brian2', sys.modules)
compare = bridge.sealed('compare_timing.py', AJ)
self.assertEqual(bridge.digest(compare.__file__), bridge.AJ_PINS['compare_timing.py'])
self.assertTrue(callable(compare.compare))
self.assertNotIn('torch', sys.modules)
self.assertNotIn('brian2', sys.modules)
class InputTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory()
self.root = Path(self.temporary.name)
self.old, self.new = self.root / 'old', self.root / 'new'
self.old.mkdir(); self.new.mkdir()
self.old_spec = spec(self.root)
self.new_spec = dict(self.old_spec, budget_root=str(self.root / 'new-exp'), output_budget_bytes=2048)
write_json(self.old / 'spec.json', self.old_spec)
write_json(self.new / 'spec.json', self.new_spec)
for name in ('requests.npz', 'pn-drive.npz'):
for directory in (self.old, self.new):
(directory / name).write_bytes(b'identical synthetic saved input bytes')
def tearDown(self):
self.temporary.cleanup()
def test_control_allows_only_budget_difference(self):
fresh, old, evidence = bridge.control_inputs(self.new, self.old)
self.assertEqual(fresh['apl_delay_steps'], old['apl_delay_steps'])
self.assertEqual(len(evidence), 6)
changed = dict(self.new_spec, mode='changed')
write_json(self.new / 'spec.json', changed)
with self.assertRaisesRegex(ValueError, 'beyond the two budget fields'):
bridge.control_inputs(self.new, self.old)
def test_drive_and_request_bytes_both_checked(self):
for name in ('requests.npz', 'pn-drive.npz'):
with self.subTest(name=name):
original = (self.new / name).read_bytes()
(self.new / name).write_bytes(original + b'changed')
with self.assertRaisesRegex(ValueError, name):
bridge.control_inputs(self.new, self.old)
(self.new / name).write_bytes(original)
def test_real_d18_required_no_fake_d0(self):
for value in (0, True, 18.0):
with self.subTest(value=value):
write_json(self.new / 'spec.json', dict(self.new_spec, apl_delay_steps=value))
with self.assertRaisesRegex(ValueError, 'D18'):
bridge.control_inputs(self.new, self.old)
def test_initial_state_presence_and_bytes_checked(self):
(self.new / 'arrays.npz').write_bytes(b'a')
with self.assertRaisesRegex(ValueError, 'presence'):
bridge.control_inputs(self.new, self.old)
(self.old / 'arrays.npz').write_bytes(b'b')
with self.assertRaisesRegex(ValueError, 'arrays.npz'):
bridge.control_inputs(self.new, self.old)
def test_exclusive_output_and_new_budget_containment(self):
root = Path(self.new_spec['budget_root'])
root.mkdir()
out = root / 'gpu' / 'run'
bridge._new_output(self.new, out)
out.mkdir(parents=True)
with self.assertRaisesRegex(ValueError, 'exclusive'):
bridge._new_output(self.new, out)
with self.assertRaisesRegex(ValueError, 'exclusive'):
bridge._new_output(self.new, self.root / 'elsewhere')
def test_control_refuses_overwrite_before_reading_outputs(self):
report = self.root / 'report.json'
report.write_text('keep me', encoding='utf-8')
with self.assertRaisesRegex(ValueError, 'existing control'):
bridge.control_identity('missing-new', 'missing-old', self.new, self.old, report)
self.assertEqual(report.read_text(encoding='utf-8'), 'keep me')
class GateTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory()
self.root = Path(self.temporary.name)
self.cfg, self.rows = inventory(self.root)
def tearDown(self):
self.temporary.cleanup()
def test_exact_120_cells_and_fixed_two_probes(self):
controls, rows = bridge.inventories(self.root, self.cfg)
self.assertEqual(len(controls), 2)
for cohort in bridge.COHORTS:
probes = bridge.probe_rows(rows, self.cfg, cohort)
self.assertEqual(len(probes), 2)
self.assertTrue(all(row['odor_index'] == 0 and row['seed'] == 101 for row in probes))
def test_duplicate_or_replaced_cell_rejected(self):
rows = copy.deepcopy(self.rows)
rows[-1] = rows[0]
write_json(self.root / 'protocol/validation-cases.json', rows)
with self.assertRaises(ValueError):
bridge.inventories(self.root, self.cfg)
def test_controls_do_not_run_brian_or_repeats(self):
name = self.cfg['control_cases'][0]
bridge.authorize_case(self.root, self.cfg, 'controls', name, 'gpu', 0)
for backend, repeat in (('oracle', 0), ('gpu', 1)):
with self.assertRaises(ValueError):
bridge.authorize_case(self.root, self.cfg, 'controls', name, backend, repeat)
def test_probes_before_other_seed_runs(self):
gate(self.root, 'controls')
probe = bridge.probe_rows(self.rows, self.cfg, 'seed_validation')[0]
bridge.authorize_case(self.root, self.cfg, 'seed_validation', probe['name'], 'oracle', 0)
later = next(row for row in self.rows if row['cohort'] == 'seed_validation' and row['seed'] == 102)
with self.assertRaises(FileNotFoundError):
bridge.authorize_case(self.root, self.cfg, 'seed_validation', later['name'], 'gpu', 0)
gate(self.root, 'seed-probes')
bridge.authorize_case(self.root, self.cfg, 'seed_validation', later['name'], 'gpu', 0)
with self.assertRaises(ValueError):
bridge.authorize_case(self.root, self.cfg, 'seed_validation', later['name'], 'oracle', 0)
with self.assertRaises(ValueError):
bridge.authorize_case(self.root, self.cfg, 'seed_validation', later['name'], 'gpu', 1)
def test_novel_denied_on_representation_failure_even_execution_passes(self):
gate(self.root, 'controls')
gate(self.root, 'seed-validation', execution_gate_pass=True, representation_gate_pass=False)
probe = bridge.probe_rows(self.rows, self.cfg, 'novel_to_aj')[0]
with self.assertRaisesRegex(ValueError, 'representation'):
bridge.authorize_case(self.root, self.cfg, 'novel_to_aj', probe['name'], 'gpu', 0)
def test_novel_requires_both_gate_and_underlying_summary(self):
gate(self.root, 'controls')
probe = bridge.probe_rows(self.rows, self.cfg, 'novel_to_aj')[0]
summary = seed_summary(self.root, self.cfg, self.rows)
summary['representation_gate_pass'] = False
bind_seed_gate(self.root, summary)
with self.assertRaisesRegex(ValueError, 'summary'):
bridge.authorize_case(self.root, self.cfg, 'novel_to_aj', probe['name'], 'gpu', 0)
summary['representation_gate_pass'] = True
bind_seed_gate(self.root, summary)
bridge.authorize_case(self.root, self.cfg, 'novel_to_aj', probe['name'], 'gpu', 0)
def test_novel_rejects_summary_hash_or_cohort_misbinding(self):
gate(self.root, 'controls')
summary = seed_summary(self.root, self.cfg, self.rows)
bind_seed_gate(self.root, summary)
probe = bridge.probe_rows(self.rows, self.cfg, 'novel_to_aj')[0]
summary['cohort'] = 'novel_to_aj'
write_json(self.root / 'validation/seed_validation/summary.json', summary)
with self.assertRaisesRegex(ValueError, 'exact summary'):
bridge.authorize_case(self.root, self.cfg, 'novel_to_aj', probe['name'], 'gpu', 0)
bind_seed_gate(self.root, summary)
with self.assertRaisesRegex(ValueError, 'identity'):
bridge.authorize_case(self.root, self.cfg, 'novel_to_aj', probe['name'], 'gpu', 0)
def test_novel_rejects_replaced_case_repeat_or_timing_even_with_correct_counts(self):
gate(self.root, 'controls')
probe = bridge.probe_rows(self.rows, self.cfg, 'novel_to_aj')[0]
for key in ('runs', 'repeats', 'timing_checks', 'input_output_provenance'):
with self.subTest(key=key):
summary = seed_summary(self.root, self.cfg, self.rows)
summary[key][0]['case'] = 'not-a-registered-case'
bind_seed_gate(self.root, summary)
with self.assertRaises(ValueError):
bridge.authorize_case(self.root, self.cfg, 'novel_to_aj', probe['name'], 'gpu', 0)
def test_any_stop_marker_prevents_all_scientific_actions(self):
for kind in ('representation_failure', 'execution_or_numerical_failure'):
with self.subTest(kind=kind):
write_json(self.root / 'audit/stopped-seed-validation.json', {'stop_kind': kind})
for backend in ('gpu', 'oracle', 'component'):
with self.assertRaisesRegex(ValueError, 'stop marker'):
bridge.authorize_case(self.root, self.cfg, 'controls', self.cfg['control_cases'][0], backend, 0)
def test_standalone_action_failure_persists_stop_and_never_retries(self):
action = mock.Mock(side_effect=RuntimeError('synthetic failure; no science executed'))
with self.assertRaisesRegex(RuntimeError, 'synthetic failure'):
bridge.guarded_action(self.root, 'gpu', 'controls', 'control-phys', 0, action)
marker = self.root / 'audit/stopped-case-gpu-controls-control-phys-r0.json'
report = bridge.read(marker)
self.assertEqual(report['stop_kind'], 'execution_or_numerical_failure')
self.assertFalse(report['numerical_instability_inferred_from_execution_error'])
before = marker.read_bytes()
with self.assertRaisesRegex(ValueError, 'stop marker'):
bridge.guarded_action(self.root, 'gpu', 'controls', 'control-phys', 0, action)
self.assertEqual(action.call_count, 1)
self.assertEqual(marker.read_bytes(), before)
def test_successful_guarded_action_adds_no_stop(self):
expected = {'pass_all': True}
self.assertIs(bridge.guarded_action(self.root, 'gpu', 'controls', 'control-phys', 0, lambda: expected), expected)
bridge.ensure_unstopped(self.root)
class InvocationTests(unittest.TestCase):
def test_original_gpu_function_receives_new_paths_unchanged(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
case, output, build = root / 'case', root / 'new-exp/gpu/run', root / 'new-exp/build'
data = spec(root)
data['budget_root'] = str(root / 'new-exp')
write_json(case / 'spec.json', data)
runtime = types.SimpleNamespace(run_gpu=mock.Mock(return_value={'pass_all': True}))
with mock.patch.object(bridge, 'sealed', return_value=runtime), mock.patch.object(bridge, '_receipt'):
result = bridge.run_gpu(case, output, build)
self.assertTrue(result['pass_all'])
runtime.run_gpu.assert_called_once_with(case, output, build)
def test_original_oracle_function_is_not_given_candidate_trace(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
case, output = root / 'case', root / 'new-exp/oracle/run'
data = spec(root); data['budget_root'] = str(root / 'new-exp')
write_json(case / 'spec.json', data)
runtime = types.SimpleNamespace(run_case=mock.Mock(return_value={'pass_all': True}))
with mock.patch.object(bridge, 'sealed', return_value=runtime), mock.patch.object(bridge, '_receipt'):
bridge.run_oracle(case, output)
runtime.run_case.assert_called_once_with(case, output)
class ControlEvidenceTests(unittest.TestCase):
def test_real_sealed_comparator_accepts_complete_synthetic_d18_and_rejects_signed_zero_tail(self):
# Artifact construction only: these zero traces were not produced by any model.
aj = bridge.sealed('compare_timing.py', AJ)
timing = bridge.sealed('timing_queue.py', AJ)
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
cases, outputs = [root / name for name in ('old-case', 'new-case')], [root / name for name in ('old-out', 'new-out')]
base = spec(root)
(root / 'graph').mkdir()
np.save(root / 'graph/weights.npy', np.zeros(1, np.float64), allow_pickle=False)
write_json(root / 'coefficients.json', {'synthetic_only': True})
states = dict(v=np.zeros(166700, np.float64), g=np.zeros(166700, np.float64),
ref=np.zeros(166700, np.int32), counts=np.zeros(166700, np.int32),
step=np.empty(0, np.int32), neuron_idx=np.empty(0, np.int32))
generated = np.zeros((1000, 4064), np.float32)
pending = np.zeros((18, 4064), np.float32)
audit = timing.audit_sequence(generated, generated, pending, 18)
audit.update(generated_rows=1000, applied_generated_rows=982, pending_shape=[18, 4064],
pending_application_tick_start=1000, pending_application_tick_stop_exclusive=1018,
pending_generated_tick_start=982, pending_generated_tick_stop_exclusive=1000,
pending_raw_sha256=aj.raw_hash(pending), final_apl_state_float32=[0., 0.],
final_apl_state_u32_bits=[0, 0], final_apl_state_nonzero=False,
finite_generated_sequence_only=True, future_apl_decay_or_feedback_generated=False,
closed_loop_branch_integrals_claimed_equal=False, biological_delay_measurement_claimed=False)
hashes = [{**{name + '_sha256': aj.raw_hash(states[name]) for name in ('v', 'g', 'ref')}, 'tick': tick}
for tick in range(1000)]
queues = [aj._queue_digest(tick, np.empty(0, np.int64), np.empty(0, np.uint8)) for tick in range(1000)]
for index, (case, output) in enumerate(zip(cases, outputs)):
data = dict(base, budget_root=str(root / ('budget-' + str(index))), output_budget_bytes=10000 + index)
write_json(case / 'spec.json', data)
for name in ('requests.npz', 'pn-drive.npz'):
(case / name).write_bytes(b'synthetic-identical-input')
output.mkdir()
np.savez_compressed(output / 'state.npz', **states)
for name, value in (('drive', np.zeros((1000, 2), np.float32)), ('state', np.zeros((1000, 2), np.float32)),
('inhibition', generated), ('applied', generated), ('pending-tail', pending)):
np.save(output / ('apl-' + name + '.float32.npy'), value, allow_pickle=False)
for name, rows in (('tick-state-hashes.jsonl', hashes), ('queue.jsonl', queues)):
(output / name).write_text(''.join(json.dumps(row) + '\n' for row in rows), encoding='utf-8')
(output / 'requests.csv').write_text('tick,neuron_idx,attempted,accepted,rejected\n', encoding='utf-8')
component = {'synthetic_fixture': True}
write_json(output / 'apl-component.json', component)
write_json(output / 'apl-timing-audit.json', audit)
inputs = {name: bridge.digest(case / name) for name in ('spec.json', 'requests.npz')}
inputs.update({'base_weights.npy': bridge.digest(root / 'graph/weights.npy'),
'coefficients.json': bridge.digest(root / 'coefficients.json')})
summary = dict(case=base['name'], complete=True, pass_all=True, steps=1000, completed_ticks=1000,
nodes=166700, edges=25582938, precision='float64', apl_precision='float32', state_units='SI volts',
total_spikes=0, requested=0, accepted_requests=0, rejected_requests=0, arriving_edges=0,
accepted_arriving_edges=0, counts_match_sparse_events=True, all_finite=True, record_history=False,
apl_fast_edges_zeroed=5845, kc_targets_present=4064, effective_weights_raw_sha256='0' * 64,
apl_schedule='same synthetic schedule', apl_si_conversion='same synthetic conversion',
sealed_ae_backend_sha256='1' * 64, learning=False, apl_delay_steps=18, apl_timing=audit,
source_provenance={'same_sealed_source': True}, pending_generated_count=18,
pending_initial_zero_prefix=0, pending_tail_shape=[18, 4064], future_generated_decay_tail_included=False,
apl_component=component, input_sha256=inputs, build={'path': str(root / ('build-' + str(index)))})
write_json(output / 'summary.json', summary)
result = bridge.control_identity(outputs[1], outputs[0], cases[1], cases[0], aj_model=AJ)
self.assertTrue(result['pass_all'], result.get('error', result['failed_checks']))
pending[0, 0] = np.float32(-0.)
np.save(outputs[1] / 'apl-pending-tail.float32.npy', pending, allow_pickle=False)
failed = bridge.control_identity(outputs[1], outputs[0], cases[1], cases[0], aj_model=AJ)
self.assertFalse(failed['pass_all'])
self.assertIn('apl.pending.bitwise_exact', failed['failed_checks'])
self.assertIn('fresh_gpu.fixed_delay_pending.bitwise_exact', failed['failed_checks'])
if __name__ == '__main__':
unittest.main()