"""Pure temporary preparation integration; all old context/Git/biology mocked. No historical files, sampling, brain, source-module execution or GPU is used. Real plan/anatomy/complete-source authentication belongs to separate validation. """ import copy import json from pathlib import Path import sys import tempfile from types import SimpleNamespace import unittest from unittest.mock import Mock,patch import numpy as np import prepare_execution as P def save(path,value): path.parent.mkdir(parents=True,exist_ok=True);path.write_text(json.dumps(value),encoding='utf-8') class PrepareFixture: def __init__(self,root): self.root=root;self.model=root/'models/conditioning-execution-v1';self.model.mkdir(parents=True) (self.model/'synthetic.py').write_text('# synthetic source only\n') self.exp=root/'experiments/phase4aq-fixture';self.exp.mkdir(parents=True) self.ap=root/'experiments/phase4ap-fixture';self.ap.mkdir() self.ao=root/'experiments/phase4ao-fixture';self.ao.mkdir() self.checkpoint=self.ap/'runs/D1--reference--epoch-001';self.checkpoint.mkdir(parents=True) save(self.checkpoint/'checkpoint-manifest.json',{'synthetic':True}) self.before=root/'before.npz';self.bm=root/'before.json' np.savez_compressed(self.before,edge_id=np.array([0],np.int64),plastic_delta_mv=np.array([0.],np.float32));save(self.bm,{'synthetic':'before'}) self.mapping=root/'mapping.npz' np.savez_compressed(self.mapping,edge_id=np.array([0],np.int64),source_neuron_idx=np.array([1],np.int32), target_neuron_idx=np.array([2],np.int32),target_compartment_verbatim=np.array(["y5B'2a"])) self.authority=self.ap/'jobs/D1--reference--epoch-001.authority.json' save(self.authority,dict(checkpoint_dir=str(self.checkpoint),checkpoint_manifest_sha256=P.digest(self.checkpoint/'checkpoint-manifest.json'), files={'candidate_map':P.A.pin(self.mapping),'overlay_before':P.A.pin(self.before),'overlay_manifest_before':P.A.pin(self.bm)})) update=self.ap/'updates/D1--reference--epoch-001';update.mkdir(parents=True) for name in ('chain-receipt.json','update-receipt.json','delta-after.npz','overlay-manifest.json'):(update/name).write_bytes(b'synthetic') save(self.ap/'cases/D1--reference--epoch-001/spec.json',{'synthetic':'pinned-template'}) static=self.ao/'inputs/static';static.mkdir(parents=True) self.populations=static/'populations.npz' np.savez_compressed(self.populations,PAM15=np.array([3,4,5,6],np.int32),Kenyon_Cell=np.array([1],np.int32)) self.tables={} for cue in ('A','B'): self.tables[cue]={'targets':np.array([10,11],np.int32),'rates_hz':np.array([1.,2.],np.float32)} np.savez_compressed(static/(cue+'-rates.npz'),**self.tables[cue]) self.fullplan=self.ao/'protocol/full-trial-plan.json';save(self.fullplan,{'synthetic':'not sampled'}) pins={str(p):P.digest(p) for p in (self.before,self.bm,self.mapping,self.populations,self.fullplan)} self.reg=dict(input_sha256=pins,source_sha256={},prepared_file_sha256={}, initial_overlay={'overlay_file':str(self.before),'overlay_manifest':str(self.bm)}) self.plan=dict(imported_parents={'D1':dict(role='AP.D1.reference.epoch001',checkpoint_manifest=str(self.checkpoint/'checkpoint-manifest.json'), sha256=P.digest(self.checkpoint/'checkpoint-manifest.json'))},initial_overlay={'overlay_file':P.A.pin(self.before),'overlay_manifest':P.A.pin(self.bm)}, calls=[{'call_id':'ref','case_group':'shared'},{'call_id':'light','case_group':'shared'}]) self.old=SimpleNamespace(C=SimpleNamespace(load_checkpoint=Mock(return_value={'fresh':True,'steps':1200})), helper=lambda:SimpleNamespace(verify_seal=Mock(return_value={'synthetic_verified':True})), previous_integrity=Mock(return_value={'synthetic_old_checksum':True})) self.inputs=SimpleNamespace(load_rate_tables=Mock(return_value={'tables':copy.deepcopy(self.tables),'audit':{'synthetic_no_sampler':True}})) self.mechanical=Mock(return_value=({'tick':np.array([1199],np.int32),'neuron_idx':np.array([1],np.int32)}, {'synthetic':True})) self.planobj=SimpleNamespace(build_plan=lambda:copy.deepcopy(self.plan),validate_plan=Mock(return_value=True), derive_case_spec=lambda template,call_id,**kw:{'steps':1000,'case_group':'shared'},mechanical_requests=self.mechanical) def git(self,command): if command[-2:]==['rev-parse','HEAD']:return b'a'*40+b'\n' if command[-2]=='show':return (self.root/command[-1].split(':',1)[1]).read_bytes() raise AssertionError('Unexpected external command') def run(self,git=None): with patch.object(P,'ROOT',self.root),patch.object(P,'MODEL',self.model),patch.object(P,'EXP',self.exp), \ patch.object(P,'AP',self.ap),patch.object(P,'AO',self.ao),patch.object(P,'PLAN',self.planobj), \ patch.object(P,'old_context',return_value=(self.old,self.reg,{})), \ patch.object(P.subprocess,'check_output',side_effect=self.git if git is None else git), \ patch.object(P.shutil,'disk_usage',return_value=SimpleNamespace(free=100*1024**3)), \ patch.dict(sys.modules,{'conditioning_preflight_inputs':self.inputs}): return P.prepare() class PreparationTests(unittest.TestCase): def setUp(self): self.tmp=tempfile.TemporaryDirectory();self.f=PrepareFixture(Path(self.tmp.name).resolve()) def tearDown(self):self.tmp.cleanup() def test_exclusive_registration_source_snapshot_and_shared_case(self): sha=self.f.run();reg=P.read(self.f.exp/'protocol/preregistered.json') self.assertEqual(sha,P.digest(self.f.exp/'protocol/preregistered.json')) self.assertEqual(set(reg['case_templates']),{'shared'}) self.assertEqual((self.f.exp/'protocol/source-snapshot/synthetic.py').read_bytes(),(self.f.model/'synthetic.py').read_bytes()) self.assertFalse(reg['brain_or_sampler_run_during_preparation']);self.assertFalse(reg['future_hashes_fabricated']) self.assertFalse(reg['scientific_acquisition_authorized']);self.assertFalse(reg['learning_success_claimed']) self.f.inputs.load_rate_tables.assert_called_once_with(self.f.root) self.f.old.C.load_checkpoint.assert_called_once_with(self.f.checkpoint,self.f.plan['imported_parents']['D1']['sha256']) with self.assertRaisesRegex(ValueError,'empty experiment'):self.f.run() def test_uncommitted_source_blob_rejected_before_new_outputs(self): def mismatch(command):return b'a'*40+b'\n' if command[-2:]==['rev-parse','HEAD'] else b'wrong committed source' with self.assertRaisesRegex(ValueError,'committed blob'):self.f.run(git=mismatch) self.assertEqual(list(self.f.exp.iterdir()),[]) def test_shared_template_must_be_fully_equal(self): self.f.planobj.derive_case_spec=lambda template,call_id,**kw:{'steps':1000,'wrong_mode_leak':call_id} with self.assertRaisesRegex(ValueError,'Shared comparison case templates differ'):self.f.run() self.assertFalse((self.f.exp/'protocol/preregistered.json').exists()) def test_pinned_rate_arrays_need_dtype_and_bits_not_json_similarity(self): self.f.inputs.load_rate_tables.return_value['tables']['A']['rates_hz']=self.f.tables['A']['rates_hz'].astype(np.float64) with self.assertRaisesRegex(ValueError,'PN mapping differs'):self.f.run() self.assertFalse((self.f.exp/'protocol/preregistered.json').exists()) def test_candidate_source_outside_KC_is_not_silently_selected(self): np.savez_compressed(self.f.populations,PAM15=np.array([3,4,5,6],np.int32),Kenyon_Cell=np.array([99],np.int32)) self.f.reg['input_sha256'][str(self.f.populations)]=P.digest(self.f.populations) with self.assertRaisesRegex(ValueError,'outside original KC'):self.f.run() def test_imported_parent_authority_must_match_selected_checkpoint(self): value=P.read(self.f.authority);value['checkpoint_manifest_sha256']='f'*64;save(self.f.authority,value) with self.assertRaisesRegex(ValueError,'selected parent'):self.f.run() self.assertFalse((self.f.exp/'protocol/preregistered.json').exists()) def test_no_bytecode_or_nested_source_dirs_in_inventory(self): with patch.object(P,'MODEL',self.f.model): self.assertEqual(len(P.source_inventory()),1) (self.f.model/'__pycache__').mkdir() with self.assertRaisesRegex(ValueError,'source files'):P.source_inventory() if __name__=='__main__':unittest.main()