"""Build explicit file-bound authorities; does not run a brain or prove a process. The registered coordinator authenticates source/input bytes and actual exits. All output here is a new dictionary; existing authority files are never edited. """ from __future__ import annotations import copy import hashlib import json from pathlib import Path import numpy as np def require(ok, message): if not ok: raise ValueError(message) def digest(path): h=hashlib.sha256() with Path(path).open('rb') as stream: for block in iter(lambda:stream.read(8<<20),b''):h.update(block) return h.hexdigest() def read(path): def unique(items): value={} for k,v in items: require(k not in value,'Duplicate JSON key');value[k]=v return value return json.loads(Path(path).read_text(encoding='utf-8'),object_pairs_hook=unique, parse_constant=lambda x: (_ for _ in ()).throw(ValueError('Nonfinite JSON'))) def pin(path): p=Path(path) require(p.is_absolute() and p.is_file() and not any(x.is_symlink() for x in (p,*p.parents)), 'Absolute unlinked regular source required') return dict(path=str(p.resolve()),sha256=digest(p)) def files_from_authority(value): """Exact direct closure. Parent full payload is additionally checked by loader.""" result={} def add(path,sha): require(path not in result or result[path]==sha,'Conflicting input pin') result[path]=sha def visit(item): if type(item)is dict: if set(item)=={'path','sha256'}:add(item['path'],item['sha256']) else: for key,value in item.items(): if key=='frozen_inputs': for path,sha in value.items():add(path,sha) else:visit(value) elif type(item)is list: for value in item:visit(value) visit(value) if 'parent' in value: p=value['parent'];add(str(Path(p['directory'])/'checkpoint-manifest.json'),p['checkpoint_manifest_sha256']) if 'checkpoint_dir' in value: add(str(Path(value['checkpoint_dir'])/'checkpoint-manifest.json'),value['checkpoint_manifest_sha256']) return result def effective_hash(spec,overlay_file): """Independent original-base bridge, exact SI arithmetic and APL fast mask.""" graph=Path(spec['graph_dir']) weights=np.load(graph/'weights.npy',allow_pickle=False) src=np.load(graph/'src.npy',allow_pickle=False,mmap_mode='r') ids=np.load(graph/'edge_id.npy',allow_pickle=False,mmap_mode='r') try: require(weights.dtype==np.float64 and weights.shape==(25582938,) and src.dtype==np.int32 and src.shape==weights.shape and ids.dtype==np.int64 and ids.shape==weights.shape, 'Full canonical graph required') apl=np.load(Path(spec['apl_bundle_dir'])/'apl_globals.npy',allow_pickle=False) require(apl.dtype==np.int32 and apl.shape==(2,),'Two canonical APL IDs required') fast=np.isin(src,apl);require(int(fast.sum())==5845,'Original fast-APL mask differs') weights[fast]=np.float64(0) with np.load(overlay_file,allow_pickle=False) as f: require(set(f.files)=={'edge_id','plastic_delta_mv'},'Overlay array fields differ') mask=f['edge_id'];delta=f['plastic_delta_mv'] require(mask.dtype==np.int64 and mask.shape==(14232,) and delta.dtype==np.float32 and delta.shape==mask.shape and np.all((mask>=0)&(maskmask[:-1]) and np.array_equal(ids[mask],mask) and not fast[mask].any() and np.isfinite(delta).all() and np.all(delta<=0), 'Original candidate overlay contract differs') nz=delta!=np.float32(0) rows=mask[nz] weights[rows]=np.add(weights[rows],np.multiply(delta[nz].astype(np.float64),np.float64(.001))) require(np.isfinite(weights).all() and np.all(weights[mask]>0),'Effective candidate sign/nonfinite failure') return hashlib.sha256(weights.astype('