#!/usr/bin/env python3 """Read-only audit of a daytime U5 output folder or share.zip. NumPy only. Usage: python3 audit_day5.py PATH Checks all counters and tracked unresolved IDs, generating source hashes, every retained start/endpoint, all saved spectra and curvature witnesses. Does not repeat searches or reconstruct unretained matrices. """ from functools import lru_cache from collections import Counter import hashlib import importlib.util import json from pathlib import Path import sqlite3 import sys import tempfile import zipfile import numpy as np ROOT=Path(__file__).resolve().parent def eigenbasis_start_error(p, seed, idx, family, u): """Check a saved start up to signs/rotations in repeated eigenspaces. This checks numerical compatibility with the stated start protocol, not identical regeneration on a different NumPy/LAPACK build. The original campaign source and the actual saved start are never changed. """ assert family in ('root_soft','ascent_weak') bid,k=divmod(idx,p.BATCH);cache=p.catalogue_cache() rng=np.random.default_rng(np.random.SeedSequence([seed,bid])) indices=(bid//len(p.DAY_POLICIES)*p.BATCH+np.arange(p.BATCH))%len(p.CATALOGUE) if family=='ascent_weak': indices=cache['weak'][rng.integers(0,len(cache['weak']),p.BATCH)] j=int(indices[k]);base=p.CATALOGUE[j] z,v=np.linalg.eig(base.conj().T@u) b=v@np.diag(np.angle(z))@np.linalg.inv(v);b=(b+b.conj().T)/2 errors=[float(np.linalg.norm(u.conj().T@u-np.eye(p.N))), float(np.max(abs(p.expstep(base,b)-u)))] values=cache['spectra'][j];directions=cache['directions'][j] if family=='root_soft': coeff=rng.normal(size=(p.BATCH,3))[k] scale=(10**rng.uniform(-4,0,p.BATCH))[k] a=b/scale;coord=np.einsum('ij,kji->k',a,directions).real order=np.argsort(abs(values))[:3] errors.extend([abs(np.linalg.norm(b)/scale-1), np.linalg.norm(a-np.einsum('k,kij->ij',coord,directions))]) unseen=set(range(len(values))) while unseen: first=min(unseen) group=np.flatnonzero(abs(values-values[first])<1e-7) unseen.difference_update(group.tolist()) expected=np.sum(coeff[np.isin(order,group)]**2)/np.sum(coeff**2) errors.append(abs(np.sum(coord[group]**2)-expected)) else: rng.choice([-1,1],size=p.BATCH) # Its sign is part of the unknown eigenvector sign. noise=np.einsum('bk,kij->bij',rng.normal(size=(p.BATCH,p.DIM)),p.BASIS) noise/=np.linalg.norm(noise,axis=(1,2))[:,None,None] scale=(10**rng.uniform(-4,-.3,p.BATCH))[k];a=b/scale top=directions[abs(values-values[-1])<1e-7] def project(x): return np.einsum('k,kij->ij',np.einsum('ij,kji->k',x,top).real,top) ap=a-project(a);np_=noise[k]-project(noise[k]) assert np.linalg.norm(ap)>1e-10 alpha=.1*np.vdot(ap,np_).real/np.linalg.norm(ap)**2 assert .89=?',(total,)).fetchone()[0]==0 stationary=sum(d['n'] for _,s,d in aggregates if s in ('candidate','nonflat_uphill')) assert db.execute('select coalesce(sum(n),0) from signature_counts').fetchone()[0]==stationary flags=sum(d['n'] for _,s,d in aggregates if s in ('candidate','hessian_uncertain')) assert db.execute("select count(*) from slots where pool='flags'").fetchone()[0]==flags assert db.execute('select count(*) from slots s left join kept k on k.id=s.run_id where k.id is null').fetchone()[0]==0 @lru_cache(maxsize=32) def starts(bid): return p.starts_for(cfg['seed'],bid)[1] if mode=='day-replay' else p.day_starts(cfg['seed'],bid)[1] count=spectra=exact_starts=basis_starts=0 worst_metric=worst_spec=worst_q=worst_direct=worst_basis=0. basis_families=Counter() products=p.BASIS[:,None]@p.BASIS[None,:] for idx,txt,initial,raw,endpoint,full,hor,witness in db.execute('select * from kept order by id'): m=json.loads(txt);count+=1 u0=np.frombuffer(initial,dtype='p.GRAD_TOL and sp['top']>p.CURV_TOL if m['status'] in ('candidate','nonflat_uphill'): sig,error=p.signature_match(u,sp) assert sig==m['known_signature'] and abs(error-m['signature_error'])<1e-8 if m['status']=='small_entry_excluded':assert m['min_modulus']? or top<=?)", (p.GRAD_TOL,p.CURV_TOL)).fetchone()[0]==0 assert db.execute("select count(*) from followups where status='small_entry_excluded' and min_modulus>=?", (p.BOUNDARY_CUTOFF,)).fetchone()[0]==0 result=dict(mode=mode,counted_starts=total,retained_endpoints=count,spectrum_pairs=spectra, final_counts=dict(counts),followup_transitions=transitions, starts_regenerated_within_1e_11=exact_starts, starts_checked_up_to_eigenbasis=basis_starts,eigenbasis_families=dict(basis_families), max_eigenbasis_compatibility_error=worst_basis, start_check_scope='Exact regeneration where possible; otherwise numerical eigenspace compatibility, not identical regeneration.', tracked_initial_unresolved=before,tracked_final_unresolved=after, all_candidate_flags_retained=flags,max_scalar_discrepancy=worst_metric, max_spectrum_discrepancy=worst_spec,max_witness_discrepancy=worst_q, max_direct_hessian_discrepancy=worst_direct,result='passed') db.close();return result if __name__=='__main__': if len(sys.argv)!=2:raise SystemExit(__doc__) path=Path(sys.argv[1]) if path.is_dir():result=audit(path) else: with tempfile.TemporaryDirectory() as tmp: with zipfile.ZipFile(path) as z: assert all('/' not in n and '\\' not in n and n not in ('.','..') for n in z.namelist());z.extractall(tmp) result=audit(Path(tmp)) print(json.dumps(result,indent=2))