27 lines
1.5 KiB
Python
27 lines
1.5 KiB
Python
"""Fixed B17 training weights and semantic pre-score review gate."""
|
|
import json
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from phase2_h1_worker_v2 import sha
|
|
|
|
def tail_weights(seconds, mass):
|
|
t=np.asarray(seconds,dtype=np.float64);m=np.asarray(mass,dtype=np.float64)
|
|
if t.ndim!=1 or m.shape!=t.shape or not len(t) or not np.isfinite(t).all() or not np.isfinite(m).all() or np.any(m<=0):
|
|
raise ValueError('Invalid training time/mass')
|
|
order=np.argsort(-t,kind='stable');budget=.1*m.sum();cumulative=np.cumsum(m[order]);cut=t[order[np.searchsorted(cumulative,budget,side='left')]]
|
|
above=t>cut;ties=t==cut;fraction=(budget-m[above].sum())/m[ties].sum()
|
|
if not -1e-12<=fraction<=1+1e-12:raise ValueError('Tail mass fraction outside unit interval')
|
|
fraction=min(1.,max(0.,fraction));a=above.astype(np.float64);a[ties]=fraction
|
|
if not np.isclose(np.dot(m,a),budget,rtol=1e-12,atol=0):raise ValueError('Tail mass not ten percent')
|
|
w=1+3*a;return w/w.mean()
|
|
|
|
def validate_review(path,binding,base):
|
|
d=json.loads(Path(path).read_text())
|
|
if d.get('verdict')!='PASS':raise PermissionError('Review must explicitly PASS')
|
|
for k in ['reviewed_commit','contract_sha256','launch_proof_sha256','code_sha256']:
|
|
if d.get(k)!=binding[k]:raise PermissionError('Review identity mismatch: '+k)
|
|
if not binding['code_sha256']:raise PermissionError('Empty reviewed sources')
|
|
for name,digest in binding['code_sha256'].items():
|
|
if sha(Path(base)/name)!=digest:raise PermissionError('Reviewed source bytes changed')
|
|
return d
|