"""Independent APL scheduling hypothesis; every earlier artifact is read-only.""" import hashlib import importlib.util import json import sys from pathlib import Path ROOT = Path('/data/digital-drosophila') MODEL = Path(__file__).resolve().parent EXP = ROOT / 'experiments/phase4aj-apl-timing' AI_MODEL = ROOT / 'models/male-cns-lif/pn-recruitment-audit-v1' AI = ROOT / 'experiments/phase4ai-pn-recruitment-audit' AH_MODEL = ROOT / 'models/male-cns-lif/kc-drive-audit-v1' AG_MODEL = ROOT / 'models/male-cns-lif/author-apl-lab-v2' AG = ROOT / 'experiments/phase4ag-author-apl-r2' META = ROOT / 'datasets/male-cns-v1.0/processed/neuron-only/neuron-index/unified-metadata.parquet' COEFF = ROOT / 'experiments/phase4af-author-fullgraph/oracle/no_input/coefficients.json' CFG = json.loads((MODEL / 'config.json').read_text(encoding='utf-8')) def load_module(name, path): spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) sys.modules[name] = module spec.loader.exec_module(module) return module def digest(path): h = hashlib.sha256() with Path(path).open('rb') as f: for block in iter(lambda: f.read(8 << 20), b''): h.update(block) return h.hexdigest() def read(path): return json.loads(Path(path).read_text(encoding='utf-8')) def save(path, value): path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) with path.open('x', encoding='utf-8') as f: json.dump(value, f, indent=2, ensure_ascii=False, allow_nan=False) f.write('\n') def sources(): return {p.relative_to(MODEL).as_posix(): digest(p) for p in sorted(MODEL.rglob('*')) if p.is_file() and '__pycache__' not in p.parts and p.name not in ('manifest.json', 'checksums.sha256')} def integrity(): previous = load_module('sealed_ai_context_aj', AI_MODEL / 'context.py') result = previous.integrity() sys.path.insert(0, str(ROOT / 'models/male-cns-lif/terminal-scope-diagnostic-v1')) from diagnostic import manifest_check for path in (AI_MODEL, AI): result[str(path.relative_to(ROOT))] = manifest_check(path, digest) assert all(r['pass'] for r in result.values()) return result def check(): registered = read(EXP / 'protocol/preregistered.json') assert registered['sources'] == sources(), 'Registered sources changed' for row in registered['inputs']: assert digest(ROOT / row['path']) == row['sha256'], row['path'] for row in read(EXP / 'protocol/prepared-hashes.json'): assert digest(EXP / row['path']) == row['sha256'], row['path'] return registered def budget(): size = sum(p.stat().st_size for p in EXP.rglob('*') if p.is_file()) assert size < CFG['output_budget_bytes'], 'Output budget exceeded; preserve partial work' return size def seal(root): rows = [dict(path=str(p.relative_to(root)), bytes=p.stat().st_size, sha256=digest(p)) for p in sorted(root.rglob('*')) if p.is_file() and '__pycache__' not in p.parts] save(root / 'manifest.json', dict(root=str(root), files=rows)) with (root / 'checksums.sha256').open('x') as f: for row in rows: f.write(row['sha256'] + ' ' + row['path'] + '\n') f.write(digest(root / 'manifest.json') + ' manifest.json\n') def bit_equal(a, b): return a.dtype == b.dtype and a.shape == b.shape and a.tobytes() == b.tobytes()