Files
MeltMind/artifacts/meltassist-e1b-audit-2026-11-17/audit.py
T

93 lines
8.6 KiB
Python

"""Read-only evidence audit: no model import, array decoding or scoring."""
import collections
import hashlib
import json
from pathlib import Path
import subprocess
import time
import zipfile
B = Path('/home/frank14f/optane/MeltMind')
R = B/'artifacts/phase2-b13-e1b-2026-11-17'
OUT = Path(__file__).parent
checks = []
def read(p): return json.loads(Path(p).read_text())
def digest(p):
h = hashlib.sha256()
with Path(p).open('rb') as f:
for block in iter(lambda: f.read(4*1024**2), b''): h.update(block)
return h.hexdigest()
def check(name, ok, detail=None):
checks.append(dict(name=name, passed=bool(ok), detail=detail))
def git(repo, *args):
return subprocess.check_output(['git','-C',repo,*args])
c=read(R/'contract.json'); complete=read(R/'complete.json'); score=read(R/'score/result.json')
manifest=read(R/'predictions-manifest.json')['entries']; by={r['case']:r for r in c['parts']}
old=read(Path(c['prior_E1_root'])/'complete.json')
check('original_E1_retained',old['decision']=='PARK_G_COVERAGE' and old['new_fits']==0)
check('prior_CPU_once',c['prior_E1_cpu_seconds']==old['cumulative_cpu_seconds']==838.804720921)
check('budget',complete['cumulative_cpu_seconds']<=c['total_cpu_cap'],dict(total=complete['cumulative_cpu_seconds'],prior=c['prior_E1_cpu_seconds'],new=complete['cumulative_cpu_seconds']-c['prior_E1_cpu_seconds']))
check('source_pins',all(digest(B/p)==h for p,h in {**c['source_sha256'],**c['code_sha256']}.items()))
testrows=[r for f in c['folds'] for r in f['rows']['test']]
check('552_unique_tests',len(testrows)==len({r['case'] for r in testrows})==552 and {r['case'] for r in testrows}==set(by))
component_folds=collections.defaultdict(set)
fold_counts=[]
for f in c['folds']:
learn=f['rows']['learning'];test=f['rows']['test'];fold=f['fold']
check('roles_'+str(fold),all(next(a for a in r['assignments'] if a['fold']==fold)['role']==role for role,rows in f['rows'].items() for r in rows))
check('component_isolation_'+str(fold),not ({r['component_id'] for r in learn}&{r['component_id'] for r in test}))
for r in test:component_folds[r['component_id']].add(fold)
fold_counts.append(len({r['component_id'] for r in test}))
if fold:
access=read(R/f'folds/{fold}/phases/train/target-access.json')
check('actual_training_access_'+str(fold),[r['case'] for r in access]==[r['case'] for r in learn] and all(a['target_sha256']==r['v5_cache']['output_sha256']['target.npy'] for a,r in zip(access,learn)),dict(rows=len(access),logged_role_strings=sorted({a['role'] for a in access})))
check('204_components_once',len(component_folds)==204 and all(len(v)==1 for v in component_folds.values()),fold_counts)
old_fold0=read(B/'artifacts/phase2-b13-gate-2026-11-17/contract.json')
check('fold0_exact_learning',old_fold0['rows']['learning']==c['folds'][0]['rows']['learning'])
check('fold0_model_reuse',digest(c['fold0_model']['path'])==digest(R/'folds/0/phases/train/model.ubj')==c['fold0_model']['sha256'])
check('new_fit_dispatch',[r['index'] for r in complete['records'] if r['mode']=='train'] and sorted(r['index'] for r in complete['records'] if r['mode']=='train')==[1,2,3,4])
check('ten_guards',len(complete['records'])==10 and all(r['guard']['status']=='PASS' and r==read(R/f"guards/{r['mode']}-{r['index']:04d}/guard-result.json") for r in complete['records']))
features=read(R/'features-manifest.json')['entries'];invalid=[];feature_hash_ok=True;finite_metadata=[]
for e,reused in zip(features,c['reused_geometry']):
p=R/f"parts/{e['index']:04d}";r=read(p/'result.json')
feature_hash_ok &= e['case']==reused['case'] and digest(p/'result.json')==e['result_sha256']==reused['result_sha256'] and digest(p/'extra.npy')==r['extra_sha256']==reused['extra_sha256']
if r['status']!='GEOMETRY_COMPLETE':
invalid.append(e['case']);finite_metadata.append(r.get('finite_nodes_per_column'))
check('552_feature_hashes',len(features)==552 and feature_hash_ok)
check('invalid_42_retained',len(invalid)==42 and set(invalid)==set(score['invalid_G_parts'])=={e['case'] for e in manifest if not e['G_valid']},dict(finite_metadata_counts=dict(collections.Counter(map(str,finite_metadata)))))
check('552_prediction_hashes',len(manifest)==552 and all(digest(e['path'])==e['sha256'] for e in manifest))
check('552_A_hashes_roles',all(digest(c['A_predictions'][e['case']]['path'])==e['A_source_sha256']==c['A_predictions'][e['case']]['sha256'] and c['A_predictions'][e['case']]['fold']==e['fold'] and c['A_predictions'][e['case']]['pool']==100 and c['A_predictions'][e['case']]['protocol']=='grouped' for e in manifest))
check('score_roster_same',len(score['rows'])==552 and {r['case'] for r in score['rows']}==set(by) and all({k:e[k] for k in ['case','component_id','fold','G_valid']}=={k:r[k] for k in ['case','component_id','fold','G_valid']} for e,r in zip(manifest,score['rows'])))
metrics={}
for name,m in score['metrics'].items():
ci=m['new_minus_A'];metrics[name]=dict(means=m['means'],**{k:v for k,v in ci.items() if k!='component_ids'})
check('metric_roster_'+name,m['components']==dict(new=204,A=204) and len(ci['component_ids'])==204 and set(ci['component_ids'])==set(component_folds) and ci['undefined_replicates']==0)
check('precommitted_gate_matches',score['gate']=='pooled field CI high<0 only' and score['metrics']['relative_l2']['new_minus_A']['high']<0 and score['decision']==complete['decision']=='CONFIRMED_FIXED_GENERIC_CONTEXT_INCREMENT')
proofs=list(R.glob('folds/*/features-git-freeze.json'))+list(R.glob('folds/*/model-git-freeze.json'))+[R/'launch-git-freeze.json',R/'predictions-git-freeze.json',R/'terminal-result-git-freeze.json']
barriers=[]
for p in proofs:
d=read(p);repo=d['local_repository'];commit=d['commit'];ok=True
for path,h in d['sha256'].items():
key=str(Path(path).relative_to(B)) if Path(path).is_absolute() else path
ok &= hashlib.sha256(git(repo,'show',commit+':'+key)).hexdigest()==h and digest(B/path)==h
check('git_objects_'+str(p.relative_to(R)),ok)
barriers.append(dict(proof=str(p.relative_to(R)),commit=commit,unix=int(git(repo,'show','-s','--format=%ct',commit)),remote_published=d['remote_published']))
pred=read(R/'predictions-git-freeze.json');repo=pred['local_repository'];score_start=read(R/'guards/score-0000/worker.started.json')['unix']
check('prediction_freeze_before_score',int(git(repo,'show','-s','--format=%ct',pred['commit']))<score_start)
for fold in range(5):
feature=read(R/f'folds/{fold}/features-git-freeze.json')['commit'];model=read(R/f'folds/{fold}/model-git-freeze.json')['commit']
for earlier,later in [(feature,model),(model,pred['commit'])]:
check('ancestor_'+earlier[:8]+'_'+later[:8],subprocess.run(['git','-C',repo,'merge-base','--is-ancestor',earlier,later]).returncode==0)
predict_start=read(R/f'guards/predict-{fold:04d}/worker.started.json')['unix']
check('model_before_predict_'+str(fold),int(git(repo,'show','-s','--format=%ct',model))<predict_start)
if fold:check('feature_before_fit_'+str(fold),int(git(repo,'show','-s','--format=%ct',feature))<read(R/f'guards/train-{fold:04d}/worker.started.json')['unix'])
zp=B/'artifacts/phase2-e1b-review-2026-11-17.zip'
check('review_zip_sha',digest(zp)=='04a9fc8e8b27274c2e88b968173b99dbf49f86bfa5fd5d1032c72d27d64978c9')
with zipfile.ZipFile(zp) as z:
names=z.namelist();check('zip_members_match_local',len(names)==10 and all(hashlib.sha256(z.read(n)).hexdigest()==digest(B/n) for n in names),names)
findings=[dict(severity='metadata',finding='Four fresh-fit target-access logs use hardcoded fold0_learning. Actual case lists and target hashes match each own fold learning contract; no test-role substitution found. Preserve originals and add erratum; no rerun needed.'),dict(severity='interpretation',finding='Last-fill CI crosses zero: inconclusive, not proof of unchanged/equivalent performance. Pooled five-fold confirmation is not every-fold improvement.')]
result=dict(status='PASS_WITH_METADATA_ERRATUM' if all(x['passed'] for x in checks) else 'CHECK_FAILURE',scope='Read-only local JSON/source/Git objects and byte hashes; no array/label decoding, model loading, fitting, predictions, metrics or bootstrap recomputation; no Git writes or remote verification.',unix=time.time(),checks=checks,metrics=metrics,barriers=barriers,findings=findings,remote_publication='Not independently checked; freeze receipts record false, no remote-completion claim.',source_result_commit=read(R/'terminal-result-git-freeze.json')['commit'])
(OUT/'audit.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n')
print(json.dumps(dict(status=result['status'],checks=len(checks),failures=[x for x in checks if not x['passed']],findings=findings,metrics=metrics),ensure_ascii=False,indent=2))