#!/usr/bin/env python3
"""Suite28: prequential Dynamic Readiness–Requirement Model, comparison and audit."""
from __future__ import annotations
import argparse,copy,datetime,hashlib,json,os,pathlib,platform,random,sys,time,traceback,warnings
import numpy as np,pandas as pd,torch,joblib
from sklearn.ensemble import HistGradientBoostingClassifier
from suite28_data import discover,load_fold,valid_context_features,context_audit,CourseBank,Rows
from suite28_engine import prepare,batches,device_batch,evaluate_model,train_epoch,episodes
from suite28_model import ReadinessModel,ordinal_probabilities,ordinal_score_fisher
from suite28_diagnostics import course_geometry,filter_health
from suite28_metrics import set_seed,safe_ap,safe_auc,metric_dict,print_table,save_json
from suite28_reporting import references,rows_metrics,summaries,paired,bootstrap,stratified,operational

VARIANTS={
 'suite28_context_only':dict(kind='bottleneck',context_only=True,binary_only=False),
 'suite28_mirt':dict(kind='mirt',context_only=False,binary_only=False),
 'suite28_drrm':dict(kind='bottleneck',context_only=False,binary_only=False),
}
OPTIONAL={'suite28_binary_only':dict(kind='bottleneck',context_only=False,binary_only=True),
          'suite28_k1':dict(kind='bottleneck',context_only=False,binary_only=False,k=1)}

def arg_parser():
    p=argparse.ArgumentParser(description=__doc__)
    p.add_argument('--root',default='/root/disk_3tb/earlywarning/30072026')
    p.add_argument('--cache-dir',default=None);p.add_argument('--structured-dir',default=None);p.add_argument('--out',default=None)
    p.add_argument('--faculties',default='all');p.add_argument('--folds',default='1,2,3,4,5,6,7');p.add_argument('--dev-folds',default='4,5')
    p.add_argument('--max-epochs',type=int,default=7);p.add_argument('--batch-students',type=int,default=48)
    p.add_argument('--lr',type=float,default=.001);p.add_argument('--weight-decay',type=float,default=.0002)
    p.add_argument('--steps-per-epoch',type=int,default=0,help='0=full epoch; a nonzero budget is an EXPLICIT approximation')
    p.add_argument('--k',type=int,default=4);p.add_argument('--tau',type=float,default=.65)
    p.add_argument('--discount',type=float,default=.7);p.add_argument('--damping',type=float,default=.65)
    p.add_argument('--seed',type=int,default=42);p.add_argument('--device',default='cuda' if torch.cuda.is_available() else 'cpu')
    p.add_argument('--extended',action='store_true',help='Also fit binary-only and K=1 controls')
    p.add_argument('--bootstrap',type=int,default=200);p.add_argument('--skip-checkpoints',action='store_true')
    p.add_argument('--min-dev-rows',type=int,default=30);p.add_argument('--min-dev-positive',type=int,default=3)
    p.add_argument('--min-dev-negative',type=int,default=20);p.add_argument('--min-dev-coverage',type=float,default=1.)
    p.add_argument('--preflight-only',action='store_true')
    p.add_argument('--skip-flat-control',action='store_true',help='Disable matched standalone full-136-feature HGB control')
    return p

def roots(base):
    names={
        'suite22':'suite22_sftr_tabm','suite25':'suite25_structure_aware_chronological_retrieval',
        'suite23':'suite23_cs_hagt','suite24':'suite24_target_conditioned_structural_residual',
        'suite17':'suite17_robust_forward_specialist_boosting','suite18':'suite18_fmm_forward_specialist_boosting',
        'suite21':'suite21_guarded_temporal_residual','suite14':'suite14_authority_safe_collaborative_moe',
        'suite26':'suite26_tree_native_student_memory',
        'council':'suite4/2_competence_council','v2':'final_train_v2'}
    return {k:(base/'results'/v) for k,v in names.items()}

def manifest(args,faculties,features):
    home=pathlib.Path(__file__).parent
    sources={}
    for path in sorted(home.glob('*.py')):
        sources[path.name]=hashlib.sha256(path.read_bytes()).hexdigest()
    for name in ['README.md','DESIGN.md','reference_council_13_faculties.csv']:
        path=home/name
        if path.exists():sources[name]=hashlib.sha256(path.read_bytes()).hexdigest()
    input_meta=[]
    for fold in range(1,8):
        for fac in faculties:
            for root in [args.cache_dir,args.structured_dir]:
                path=pathlib.Path(root)/f'{fac}_fold_{fold}.joblib'
                if not path.exists():raise FileNotFoundError(path)
                st=path.stat()
                input_meta.append({'file':str(path),'size_bytes':st.st_size,'mtime_ns':st.st_mtime_ns})
    return {'created':datetime.datetime.now(datetime.timezone.utc).isoformat(),'python':platform.python_version(),
            'torch':torch.__version__,'cuda':torch.cuda.is_available(),'device':str(args.device),'seed':args.seed,
            'faculties':faculties,'context_fields':features,'config':vars(args),'sources_sha256':sources,
            'input_file_metadata_not_content_hash':input_meta,
            'historical_holdout_status':'F6/F7 were reused during Suites22–27 design; not a new prospective confirmation.'}

def selected_common_fields(args,faculty_names):
    # Feature ownership uses only covariates, NEVER outcomes or their predictions.
    print('[PRECHECK] auditing student-semester context invariance across all historical folds...',flush=True)
    shared=None;audits=[]
    for fold in range(1,8):
        hist,tgt=load_fold(args.cache_dir,args.structured_dir,faculty_names,fold)
        audit=context_audit(hist)
        audits.append({'fold':fold,'groups_checked':audit['groups_checked'],'inconsistent':audit['inconsistent']})
        good=set(audit['allowed'])
        if fold<=5:
            shared=good if shared is None else (shared & good)
        else:
            if shared is not None and not shared.issubset(good):
                raise RuntimeError(f'Holdout F{fold} input feature contract violation: student-context fields declared invariant on F1-F5 become course-dependent: {sorted(shared-good)}. Never tune feature choice on F6/F7.')
        print(f'[CONTEXT AUDIT] F{fold} candidate={len(hist.raw_names)} invariant={len(good)} groups={audit["groups_checked"]} unstable={audit["inconsistent"]}',flush=True)
        del hist,tgt
    if not shared:raise RuntimeError('No common student-semester invariant context features across F1–F5')
    # Fix deterministic order from the canonical whitelisted first fold.
    hist,tgt=load_fold(args.cache_dir,args.structured_dir,faculty_names,5)
    names=[n for n in hist.raw_names if n in shared]
    print('[CONTEXT OWNERSHIP] frozen',len(names),'fields:',names,flush=True)
    print('[FEATURE PROVENANCE WARNING] original 136 common upstream features cannot be fully provenance-certified from these caches. Suite28 rebuilds course outcome aggregates as-of and never passes query grade or failure to inference.')
    return names,{'by_fold':audits,'frozen_names':names,'selection_boundary':'F1-F5 only; F6/F7 contract checks are fail-only'}

def trim_to_names(hist,tgt,names):
    inds=[hist.raw_names.index(n) for n in names]
    hist.raw=hist.raw[:,inds].copy();tgt.raw=tgt.raw[:,inds].copy()
    hist.raw_names=list(names);tgt.raw_names=list(names)
    return hist,tgt

def load_origin(args,faculties,fold,names):
    h,t=load_fold(args.cache_dir,args.structured_dir,faculties,fold,context_names=names)
    return h,t

def audit_support(h,t,faculties,fold,strict=False,args=None):
    out=[]
    duplicates=pd.DataFrame({'faculty':h.faculty,'student':h.student,'course':h.course,'semester':h.semester}).duplicated(keep=False)
    print(f'[EVENT IDENTITY AUDIT] F{fold} coarse student-course-semester duplicate rows={int(duplicates.sum())} / {len(h)} (may represent separate registrations; do not silently deduplicate)')
    print(f'[GRADE SUPPORT] F{fold} training numeric-grade coverage={float(np.isfinite(h.grade).mean()):.5f}; test-grade coverage (EVALUATION ONLY)={float(np.isfinite(t.grade).mean()):.5f}')
    print(f'[INPUT] F{fold} train_rows={len(h):,} target_rows={len(t):,} contexts={h.raw.shape[1]} common_raw_full={h.raw_all.shape[1] if h.raw_all is not None else None}')
    historical={(int(f),str(s)) for f,s in zip(h.faculty,h.student)}
    historical_courses={(int(f),str(c)) for f,c in zip(h.faculty,h.course)}
    for fi,f in enumerate(faculties):
        mm=t.faculty==fi;n=int(mm.sum());p=int(t.y[mm].sum());seen=np.mean([(fi,str(s)) in historical for s in t.student[mm]]) if n else np.nan
        known=np.mean([(fi,str(c)) in historical_courses for c in t.course[mm]]) if n else np.nan
        eligible=bool(n>=args.min_dev_rows and p>=args.min_dev_positive and (n-p)>=args.min_dev_negative) if args else True
        out.append(dict(fold=fold,faculty=f,rows=n,positive=p,negative=n-p,prevalence=p/max(n,1),seen_student_fraction=seen,known_course_fraction=known,dev_support=eligible))
    df=pd.DataFrame(out);print_table(f'FOLD {fold} SUPPORT / DEVELOPMENT ELIGIBILITY',df)
    if strict:
        coverage=float(df.dev_support.mean())
        if coverage<args.min_dev_coverage:raise RuntimeError(f'Development F{fold} supported faculty coverage={coverage:.3f} < {args.min_dev_coverage}; see audit above')
    return df

def make_model(args,variant,train_course,train_context,faculties):
    cfg=VARIANTS.get(variant,OPTIONAL.get(variant));k=int(cfg.get('k',args.k))
    return ReadinessModel(context_dim=train_context.shape[1],course_dim=30,faculty_count=len(faculties),
                          id_vocab=train_course.vocab_size,k=k,kind=cfg['kind'],
                          context_only=cfg['context_only'],binary_only=cfg['binary_only'],tau=args.tau,
                          discount=args.discount,damping=args.damping,seed=args.seed).to(args.device)

def per_fac_ap(rows,scores,eligible=None):
    values=[]
    for i,f in enumerate(rows.faculty_names):
        mask=rows.faculty==i
        if eligible is not None and not eligible[i]:continue
        values.append(safe_ap(rows.y[mask],scores[mask]))
    return np.nanmean(values)

def run_selection(args,faculties,names,variants,output):
    # Each development origin has an independently fitted model based only on its earlier history.
    # Only F4/F5 (or explicitly named earlier development folds) may select recipe epochs.
    results=[];support=[]
    devfolds=[int(x) for x in args.dev_folds.split(',')]
    if any(x>=6 for x in devfolds):raise ValueError('Development folds must be <=5; F6/F7 cannot select.')
    for fold in devfolds:
        print(f'\n{"="*130}\n[DEVELOPMENT ORIGIN] F{fold} — independent earlier-only fitting\n{"="*130}',flush=True)
        h,t=load_origin(args,faculties,fold,names)
        dd=audit_support(h,t,faculties,fold,strict=True,args=args);support.extend(dd.to_dict('records'))
        train,pp,cb=prepare(h,fit=True)
        infer,_,_=prepare(h,t,processor=pp,course_bank=cb)
        eligible=dd.dev_support.to_numpy(bool)
        for name in variants:
            set_seed(args.seed+fold)
            model=make_model(args,name,cb,train.x,faculties)
            opt=torch.optim.AdamW(model.parameters(),lr=args.lr,weight_decay=args.weight_decay)
            for epoch in range(1,args.max_epochs+1):
                t0=time.time();tr=train_epoch(model,train,opt,args.device,args.batch_students,
                        seed=args.seed+epoch*100+fold,max_steps=args.steps_per_epoch or None)
                scores,_=evaluate_model(model,infer,args.batch_students,args.device)
                ap=per_fac_ap(t,scores,eligible)
                f2=np.nanmean([metric_dict(t.y[t.faculty==i],scores[t.faculty==i])['fbeta_at_20'] for i in range(len(faculties)) if eligible[i]])
                results.append({'fold':fold,'model':name,'epoch':epoch,'macro_ap':ap,'macro_f2_20':f2,'train_loss':tr['total_loss'],'epoch_steps':tr['steps'],'seconds':time.time()-t0,**{f'grad_{k}':v for k,v in tr['last_batch_grad_norms'].items()}})
                print(f'[SELECT] fold={fold} model={name} epoch={epoch}/{args.max_epochs} loss={tr["total_loss"]:.5f} AP={ap:.5f} F2@20={f2:.5f} steps={tr["steps"]} sec={time.time()-t0:.1f} gradients={tr["last_batch_grad_norms"]}',flush=True)
        del h,t,train,infer,pp,cb
        if torch.cuda.is_available():torch.cuda.empty_cache()
    pd.DataFrame(results).to_csv(output/'development_epoch_grid.csv',index=False)
    pd.DataFrame(support).to_csv(output/'development_support.csv',index=False)
    d=pd.DataFrame(results);choice={}
    for name in variants:
        x=d[d.model==name].groupby('epoch').agg(AP=('macro_ap','mean'),F2=('macro_f2_20','mean'),origins=('fold','nunique')).reset_index()
        if x.origins.min()!=len(devfolds):raise RuntimeError('Partial model-selection coverage')
        x=x.sort_values(['AP','F2','epoch'],ascending=[False,False,True]);row=x.iloc[0]
        choice[name]=int(row.epoch)
        print_table(f'[DEVELOPMENT FROZEN CHOICE] {name}',x)
    save_json(choice,output/'frozen_epochs.json')
    return choice

def _save_preds(out,fold,rows,scores):
    p=pd.DataFrame({'fold':fold,'faculty':[rows.faculty_names[int(i)] for i in rows.faculty],
         'student_id':rows.student,'course_id':rows.course,'semester':rows.semester,'source_row':rows.source_row,
         'actual_failure_lt5':rows.y})
    for name,z in scores.items():p[name]=z
    p.to_csv(out/f'fold_{fold}_scores.csv',index=False)
    # Legacy per-faculty positional score files compatible with older analysis scripts.
    for i,fac in enumerate(rows.faculty_names):
        mask=rows.faculty==i;d=p.loc[mask].reset_index(drop=True)
        d.rename(columns={'actual_failure_lt5':'actual'}).to_csv(out/f'{fac}_fold_{fold}_scores.csv',index=False)

def fulltab_hgb(train_rows,query_rows,course_bank,seed=42):
    """Independent current-only reference: 136 common raw + static/course-as-of features, no memory model."""
    if train_rows.raw_all is None:raise RuntimeError('Full tabular comparator requires the full common feature matrix')
    hist_h,_,_=course_bank.query(train_rows);target_h,_,_=course_bank.query(query_rows)
    def matrix(rows,history):
        hs=np.stack([np.log1p(history[:,0])/8,history[:,1]/10,history[:,2]/5,history[:,3],history[:,4]],1)
        faculty_onehot=np.eye(len(rows.faculty_names),dtype=np.float32)[np.asarray(rows.faculty,int)]
        x=np.concatenate([rows.raw_all,rows.static,hs,faculty_onehot],1)
        return np.clip(np.nan_to_num(x,nan=0.,posinf=0.,neginf=0.),-1e5,1e5).astype(np.float32)
    x=matrix(train_rows,hist_h);xt=matrix(query_rows,target_h)
    w=np.zeros(len(train_rows),np.float32)
    for fi in np.unique(train_rows.faculty):
        m=train_rows.faculty==fi;w[m]=len(train_rows)/max(np.sum(m),1)/len(np.unique(train_rows.faculty))
    m=HistGradientBoostingClassifier(learning_rate=.05,max_iter=180,max_leaf_nodes=31,min_samples_leaf=20,l2_regularization=1.,early_stopping=False,random_state=seed)
    m.fit(x,train_rows.y,sample_weight=w)
    p=np.asarray(m.predict_proba(xt))[:,list(m.classes_).index(1)]
    return p,m

def fold_production(args,faculties,names,variants,epochs,fold,out,refs):
    print(f'\n{"="*140}\n[PRODUCTION HISTORICAL EVALUATION] F{fold}\n{"="*140}',flush=True)
    h,t=load_origin(args,faculties,fold,names)
    audit=audit_support(h,t,faculties,fold,args=args)
    train,pp,cb=prepare(h,fit=True);infer,_,_=prepare(h,t,processor=pp,course_bank=cb)
    course_prior=infer.counts[len(h):]
    scores={'committee':t.committee.astype(float)};ref,refstatus=references(refs,fold,t);scores.update(ref)
    if not args.skip_flat_control:
        now=time.time();ppred,base=fulltab_hgb(h,t,cb,seed=args.seed+fold)
        scores['suite28_fulltab_hgb']=ppred
        print(f'[FULL FEATURE CONTROL] F{fold} HGB uses {h.raw_all.shape[1]} common raw +12 static+5 rebuilt as-of features+faculty onehot; macroAP={per_fac_ap(t,ppred):.5f} secs={time.time()-now:.1f}')
        if not args.skip_checkpoints:
            cp=out/f'fold_{fold}';cp.mkdir(exist_ok=True);joblib.dump(base,cp/'fulltab_hgb.joblib',compress=3)
        del base
    geometry=[];interventions=[];times=[];requirements=[];health=[]
    for name in variants:
        print(f'\n[PRODUCTION VARIANT] {name} epochs={epochs[name]}',flush=True)
        start=time.time();set_seed(args.seed+fold)
        model=make_model(args,name,cb,train.x,faculties)
        opt=torch.optim.AdamW(model.parameters(),lr=args.lr,weight_decay=args.weight_decay)
        for ep in range(epochs[name]):
            r=train_epoch(model,train,opt,args.device,args.batch_students,seed=args.seed+fold*37+ep,max_steps=args.steps_per_epoch or None)
            print(f'[TRAIN] F{fold} {name} epoch={ep+1}/{epochs[name]} loss={r["total_loss"]:.5f} steps={r["steps"]} sec={r["sec"]:.1f} gradients={r["last_batch_grad_norms"]}',flush=True)
        if name=='suite28_drrm':
            p,geo,row_diag=evaluate_model(model,infer,args.batch_students,args.device,debug=True,return_row_debug=True)
            rowdf=pd.DataFrame({'faculty':[faculties[int(i)] for i in t.faculty],'fold':fold,'student_id':t.student,'course_id':t.course,
                               'semester':t.semester,'actual':t.y,'prediction':p,**row_diag})
            rowdf.to_csv(out/f'fold_{fold}_readiness_uncertainty.csv',index=False)
            mass=rowdf[[f'gradebin_{i}' for i in range(6)]].to_numpy()
            if not np.allclose(mass.sum(-1),1.,rtol=1e-5,atol=1e-4):raise RuntimeError('Incoherent grade probabilities in holdout!')
            if not np.allclose(mass[:,0],p,rtol=0,atol=1e-7):raise RuntimeError('Grade bin 0 does not match failure probability!')
            if np.isfinite(t.grade).any():
                gradeidx=np.digitize(np.nan_to_num(t.grade,nan=0.),[5,6,7,8,9],right=False)
                valid=np.isfinite(t.grade)
                grade_nll=-np.log(np.maximum(mass[np.arange(len(p)),gradeidx],1e-9))
                print('[GRADE LIKELIHOOD] observed_grade_rows=',int(valid.sum()),' NLL=',float(grade_nll[valid].mean()),' grade_present_rate=',float(valid.mean()))
                grade_csv=pd.DataFrame({'fold':fold,'faculty':[faculties[int(i)] for i in t.faculty],
                    'grade_bin_actual':np.where(valid,gradeidx,-1),'grade_nll':np.where(valid,grade_nll,np.nan),
                    'predicted_failure':p})
                grade_csv.to_csv(out/f'fold_{fold}_grade_likelihood.csv',index=False)
            print('[GRADE-BIN COHERENCE] all six bins sum to 1; first bin equals failure marginal, PASS')
            print_table(f'F{fold} UNCERTAINTY / SUPPORT BY FACULTY',rowdf.groupby('faculty',as_index=False).agg(
                    rows=('prediction','count'),failures=('actual','sum'),history_events=('history_count','mean'),
                    projected_variance=('projected_variance','mean'),cov_trace=('cov_trace','mean'),readiness_norm=('mu_norm','mean'),retention=('retention','mean')),max_rows=25)
        else:p,geo=evaluate_model(model,infer,args.batch_students,args.device)
        scores[name]=p;times.append(dict(fold=fold,model=name,seconds=time.time()-start,params=sum(x.numel() for x in model.parameters()),epochs=epochs[name]))
        print(f'[PREDICTION] F{fold} {name} macro AP={per_fac_ap(t,p):.5f} AUC={np.nanmean([safe_auc(t.y[t.faculty==i],p[t.faculty==i]) for i in range(len(faculties))]):.5f}')
        if name=='suite28_drrm':
            geometry=[{'fold':fold,**x} for x in geo]
            requirements=[{'fold':fold,**x} for x in course_geometry(model,faculties,infer,h,args.device)]
            health=[{'fold':fold,**filter_health(model)}]
            print_table(f'F{fold} LEARNED COURSE REQUIREMENT GEOMETRY',requirements,max_rows=25)
            print('[FILTER HEALTH]',health)
            methods=['no_history','no_grade','mean_only','no_course_id','shuffle_historical_outcomes']
            for method in methods:
                if method=='no_history':
                    altered=copy.copy(infer);altered.update=infer.update.copy();altered.update[:len(h)]=False
                    diag,_=evaluate_model(model,altered,args.batch_students,args.device)
                elif method=='no_course_id':
                    altered=copy.copy(infer);altered.cid=np.zeros_like(infer.cid)
                    diag,_=evaluate_model(model,altered,args.batch_students,args.device)
                elif method=='shuffle_historical_outcomes':
                    altered=copy.copy(infer);altered.grade=infer.grade.copy();altered.y=infer.y.copy()
                    rng=np.random.default_rng(args.seed+fold*173)
                    # Jointly permute grades and failure outcomes ONLY among older bank rows
                    # of the same faculty and semester. No future or target outcomes are introduced.
                    for fi in np.unique(infer.fac[:len(h)]):
                        for semester in np.unique(infer.sem[:len(h)][infer.fac[:len(h)]==fi]):
                            ix=np.flatnonzero((infer.fac[:len(h)]==fi)&(infer.sem[:len(h)]==semester))
                            if len(ix)<2:continue
                            perm=rng.permutation(ix)
                            altered.grade[ix]=infer.grade[perm];altered.y[ix]=infer.y[perm]
                    diag,_=evaluate_model(model,altered,args.batch_students,args.device)
                else:diag,_=evaluate_model(model,infer,args.batch_students,args.device,intervention=method)
                for fi,fac in enumerate(faculties):
                    m=t.faculty==fi;d=safe_ap(t.y[m],diag[m])-safe_ap(t.y[m],p[m]);interventions.append(dict(fold=fold,faculty=fac,intervention=method,delta_ap=d,mean_abs_score_change=float(np.mean(np.abs(diag[m]-p[m]))),changed_fraction=float(np.mean(np.abs(diag[m]-p[m])>1e-5))))
                print(f'[INTERVENTION] F{fold} {method} mean_delta_ap={np.nanmean([r["delta_ap"] for r in interventions if r["fold"]==fold and r["intervention"]==method]):+.5f}',flush=True)
            if not args.skip_checkpoints:
                cp=out/f'fold_{fold}';cp.mkdir(exist_ok=True)
                torch.save({'state_dict':model.state_dict(),'variant':name,'k':args.k,'epochs':epochs[name],'faculty_names':faculties},cp/'drrm_model.pt')
                joblib.dump({'processor':pp,'course_bank':cb,'context_names':names},cp/'training_only_preprocessing.joblib',compress=3)
        else:
            if not args.skip_checkpoints:
                cp=out/f'fold_{fold}';cp.mkdir(exist_ok=True)
                torch.save({'state_dict':model.state_dict(),'variant':name,'k':model.k,'epochs':epochs[name]},cp/f'{name}.pt')
        del model,opt
    _save_preds(out,fold,t,scores)
    return t,scores,refstatus,geometry,interventions,times,course_prior,requirements,health

def run(args):
    start=time.time();set_seed(args.seed)
    base=pathlib.Path(args.root)
    args.cache_dir=args.cache_dir or str(base/'results'/'cross_faculty_residual_moe'/'cache')
    args.structured_dir=args.structured_dir or str(base/'results'/'suite12_structured_cache')
    output=pathlib.Path(args.out or base/'results'/'suite28_dynamic_readiness_requirement');output.mkdir(parents=True,exist_ok=True)
    folds=[int(x) for x in args.folds.split(',')]
    faculty_names=discover(args.cache_dir,min(folds)) if args.faculties in ('all','auto','') else [x.strip() for x in args.faculties.split(',')]
    if any(f<=0 or f>7 for f in folds):raise ValueError('Only historical folds 1..7 supported')
    names,ctxaudit=selected_common_fields(args,faculty_names)
    save_json(manifest(args,faculty_names,names),output/'manifest.json');save_json(ctxaudit,output/'context_field_audit.json')
    print(f'[ENV] Python={platform.python_version()} torch={torch.__version__} device={args.device} faculties={len(faculty_names)} folds={folds}')
    print('[SCIENTIFIC CONTRACT] B primary=DRRM; A=compensatory MIRT; contextual-only is RETRAINED; all learn without Committee input.')
    print('[HOLDOUT STATUS] Historical F6/F7 have informed prior research and are not fresh prospective confirmation.')
    print('[GRADES] Consistent ordinal [<5,5-6,6-7,7-8,8-9,9-10]; failure equals first bin. No same-semester outcomes before prediction.')
    if args.preflight_only:
        for fold in folds:
            h,t=load_origin(args,faculty_names,fold,names);audit_support(h,t,faculty_names,fold,args=args)
            del h,t
        return
    variants=dict(VARIANTS)
    if args.extended:variants.update(OPTIONAL)
    freeze=run_selection(args,faculty_names,names,variants,output)
    print('[FROZEN DEV EPOCHS]',freeze)
    metrics=[];reference_status=[];geometry=[];interventions=[];timings=[];strata=[];scored={};fold_values={};reqs=[];healths=[]
    ref_roots=roots(base)
    for fold in folds:
        t,s,rm,geo,ab,times,c_prior,rq,hh=fold_production(args,faculty_names,names,variants,freeze,fold,output,ref_roots)
        metrics.extend(rows_metrics(fold,t,s));reference_status.extend(rm);geometry.extend(geo);interventions.extend(ab);timings.extend(times);reqs.extend(rq);healths.extend(hh)
        # Per-student prior count measured independently from TRAIN data; zero can reflect cache coverage.
        seen=set((int(a),str(b)) for a,b in zip(t.faculty,t.student))
        # target student-history counts are not generated from target outcomes.
        h,_=load_origin(args,faculty_names,fold,names)
        counts={};
        for f,st in zip(h.faculty,h.student):counts[(int(f),str(st))]=counts.get((int(f),str(st)),0)+1
        count=np.asarray([counts.get((int(f),str(st)),0) for f,st in zip(t.faculty,t.student)])
        strata.extend(stratified(fold,t,s['suite28_drrm'],s['committee'],c_prior,count))
        for fi,fac in enumerate(faculty_names):
            m=t.faculty==fi
            for name,p in s.items():
                if np.isfinite(p[m]).all():scored[(fold,fac,name)]=(t.y[m],p[m])
        fold_values[fold]={'t':t,'scores':s}
        pd.DataFrame(metrics).to_csv(output/'all_fold_metrics.csv',index=False)
        pd.DataFrame(reference_status).to_csv(output/'reference_coverage.csv',index=False)
        pd.DataFrame(geometry).to_csv(output/'state_geometry_samples.csv',index=False)
        pd.DataFrame(interventions).to_csv(output/'postfit_interventions.csv',index=False)
        pd.DataFrame(timings).to_csv(output/'timings.csv',index=False)
        pd.DataFrame(strata).to_csv(output/'support_strata.csv',index=False)
        pd.DataFrame(reqs).to_csv(output/'requirement_geometry.csv',index=False)
        save_json(healths,output/'filter_parameters.json')
        del h,t,s;torch.cuda.empty_cache() if torch.cuda.is_available() else None
    df=pd.DataFrame(metrics)
    print_table('F1–F7 MACRO PERFORMANCE',summaries(df,folds),max_rows=100)
    if {6,7}.issubset(folds):
        print_table('F6/F7 HISTORICAL HOLDOUT MACRO PERFORMANCE',summaries(df,(6,7)),max_rows=100)
        faculty=df[df.fold.isin([6,7])].pivot_table(index='faculty',columns='model',values='ap',aggfunc='mean')
        print_table('F6/F7 ALL FACULTY AP',faculty.reset_index(),max_rows=50)
        delta=[]
        for b in ['suite28_context_only','suite28_mirt','committee','suite22','suite25','council']:
            pair=paired(df,'suite28_drrm',b)
            if len(pair):delta.append(dict(comparison=f'drrm minus {b}',cells=len(pair),mean_ap_delta=float(pair.delta_ap.mean()),wins=int((pair.delta_ap>0).sum()),mean_f2_delta=float(pair.delta_f2.mean())))
        print_table('ALIGNED PAIRED INCREMENTS',delta)
        pd.DataFrame(delta).to_csv(output/'paired_comparisons.csv',index=False)
        bootstrap_rows=[]
        for fi,fac in enumerate(faculty_names):
            yy=[];aa=[];xx={name:[] for name in ['committee','suite22','suite25','suite28_mirt','suite28_context_only','council']};cid=[]
            for fold in (6,7):
                r=fold_values[fold];t=r['t'];mask=t.faculty==fi;s=r['scores'];yy.extend(t.y[mask]);aa.extend(s['suite28_drrm'][mask]);cid.extend([f'{fac}:{z}' for z in t.student[mask]])
                for name in xx:
                    if name in s:xx[name].extend(s[name][mask])
            yy=np.asarray(yy);aa=np.asarray(aa);cid=np.asarray(cid)
            for name,v in xx.items():
                if len(v)!=len(yy) or len(yy)==0 or not np.isfinite(v).all():continue
                d,lo,hi,n=bootstrap(yy,aa,np.asarray(v),cid,n=args.bootstrap,seed=args.seed+fi)
                bootstrap_rows.append(dict(faculty=fac,reference=name,delta_ap=d,ci_low=lo,ci_high=hi,valid_bootstraps=n))
        print_table('STUDENT-CLUSTER BOOTSTRAP (POOLED F6/F7 PER FACULTY)',bootstrap_rows,max_rows=150)
        pd.DataFrame(bootstrap_rows).to_csv(output/'student_cluster_bootstrap.csv',index=False)
    # Frozen historical Council summary: reference is 13-faculty coverage, not a row-aligned score.
    council_csv=pathlib.Path(__file__).parent/'reference_council_13_faculties.csv'
    if council_csv.exists():
        archived=pd.read_csv(council_csv)
        ours=df[(df.model=='suite28_drrm') & (df.fold.isin(folds))][['fold','faculty','ap','fbeta_at_20']]
        matched=ours.merge(archived[['fold','faculty','ap','fixed_f2']].rename(columns={'ap':'council_historical_ap','fixed_f2':'council_historical_fixed_f2'}),on=['fold','faculty'])
        if len(matched):
            matched['delta_ap']=matched.ap-matched.council_historical_ap
            matched.to_csv(output/'historical_council_overlap_not_row_aligned.csv',index=False)
            print_table('FROZEN HISTORICAL COUNCIL OVERLAP (NOT ROW-ALIGNED)',matched[matched.fold.isin([6,7])],max_rows=100)
            print('[COUNCIL CAUTION] F6/F7 archive has 13 faculty cells, English absent; archival values may be rounded; no cluster CI is possible from this CSV alone.')
    ops_threshold,ops=operational(scored,df)
    ops_threshold.to_csv(output/'frozen_operational_thresholds.csv',index=False);ops.to_csv(output/'operational_holdout.csv',index=False)
    if len(ops):
        print_table('F6/F7 FROZEN OPERATIONAL POLICY, FACULTY-MEAN',ops.groupby(['model','policy'],as_index=False)[['precision','recall','fbeta','flag_rate']].mean().sort_values(['policy','fbeta'],ascending=[True,False]),max_rows=100)
        print_table('IT OPERATIONAL CONFUSION MATRICES',ops[ops.faculty=='khoa_cong_nghe_thong_tin'],max_rows=100)
    if len(interventions):
        print_table('POSTFIT MECHANISM INTERVENTIONS BY HOLDOUT',pd.DataFrame(interventions).query('fold in [6,7]').groupby('intervention',as_index=False).agg(mean_delta_ap=('delta_ap','mean'),harmed_cells=('delta_ap',lambda x:int((x<0).sum())),cells=('delta_ap','count'),score_mae=('mean_abs_score_change','mean')))
    if len(strata):print_table('HOLDOUT SUPPORT / COLD-START STRATA (EXCERPT)',pd.DataFrame(strata).query('fold in [6,7]').head(80),max_rows=80)
    macro=summaries(df,(6,7)) if {6,7}.issubset(folds) else summaries(df,folds)
    amap=dict(zip(macro.model,macro.ap))
    audit={'primary':amap.get('suite28_drrm'),'contextual':amap.get('suite28_context_only'),'fulltab_hgb':amap.get('suite28_fulltab_hgb'),'mirt':amap.get('suite28_mirt'),
         'committee':amap.get('committee'),'suite22_blend':amap.get('suite22'),'suite25_blend':amap.get('suite25'),
         'contextual_increment':amap.get('suite28_drrm',np.nan)-amap.get('suite28_context_only',np.nan),
         'bottleneck_increment':amap.get('suite28_drrm',np.nan)-amap.get('suite28_mirt',np.nan),
         'beats_2026_reference_by_0p02':bool(amap.get('suite28_drrm',-1)>=max(amap.get('suite22',-1),amap.get('suite25',-1))+.02),
         'reference_coverage_note':'Compare aligned cells, not unmatched aggregates. Historical F6/F7 previously used for design.'}
    save_json(audit,output/'frozen_premise_audit.json');print_table('FROZEN HYPOTHESIS AUDIT',pd.DataFrame([audit]))
    print(f'[DONE] Suite28 finished elapsed_hours={(time.time()-start)/3600:.3f} results={output}')

def main():
    args=arg_parser().parse_args()
    try:run(args)
    except Exception:
        traceback.print_exc();sys.exit(1)
if __name__=='__main__':main()
