"""Held-out next-token NLL gate for this synthetic fixture only. Not a safety gate.
Unexecuted optional Transformer path. Uses CPU; no paid APIs or uploaded data.
"""
import argparse,hashlib,json,math,os,shutil
from pathlib import Path
import torch
from transformers import AutoTokenizer, GPT2LMHeadModel
p=argparse.ArgumentParser();p.add_argument('--root',default='/work');p.add_argument('--max-nll',type=float,required=True);a=p.parse_args()
r=Path(a.root);src=r/'transformer-candidate';dst=r/'approved'
if dst.exists(): raise ValueError('use a fresh release directory; never overwrite a served artifact')
data=json.loads((r/'dataset.json').read_text());lineage=json.loads((src/'lineage.json').read_text())
assert lineage['data_sha256']==hashlib.sha256((r/'dataset.json').read_bytes()).hexdigest()
tok=AutoTokenizer.from_pretrained(src,local_files_only=True);model=GPT2LMHeadModel.from_pretrained(src,local_files_only=True).eval()
total=0.;n=0
with torch.no_grad():
    for row in data['validation']:
        ids=tok(row['text'],max_length=126,truncation=True)['input_ids']+[tok.eos_token_id]
        x=torch.tensor([ids]);loss=model(input_ids=x,labels=x).loss.item()
        total+=loss*(len(ids)-1);n+=len(ids)-1
nll=total/n
print(json.dumps({'validation_nll':nll,'tokens':n,'threshold':a.max_nll}))
if not math.isfinite(nll) or nll>=a.max_nll: raise ValueError('candidate rejected; approved version unchanged')
files={f.name:hashlib.sha256(f.read_bytes()).hexdigest() for f in src.iterdir() if f.is_file()}
staging=r/'approval-staging'
if staging.exists(): shutil.rmtree(staging)
shutil.copytree(src,staging)
(staging/'release-manifest.json').write_text(json.dumps({'files':files,'validation_nll':nll,'gate':'synthetic fixture only','data_sha256':lineage['data_sha256']},indent=2))
os.replace(staging,dst) # Atomic directory rename on this one filesystem, not object-store transaction.
