"""Audit trained masked Transformer using held-out first-stage evidence.

Checks the actual 16-feature input and trained checkpoints at the evidence
boundary. This is not a raw-input 4+4 perturbation test: trained decoder fold
weights for the scored baseline were not saved by run_experiment.py.
"""
import os,json
import numpy as np
import torch
import masked_rate_experiment as M
import nested_schedule_eval as N
import nested_stack_fusion as F
import score as S
from common import DS_KEYS
from common_exp import EXP
from e1_switch import hmm_filter


def main():
 torch.set_num_threads(2);dev=torch.device('cuda' if torch.cuda.is_available() else 'cpu');rng=np.random.default_rng(901)
 clf,_=zip(*[S.load_folds(e,'causal') for e in F.REF]);dec,_=zip(*[S.load_folds(e,'causal') for e in F.DEC]);rows=[]
 for ds in list(DS_KEYS)+['AASD','Carta2026']:
  if ds in DS_KEYS:
   tab=N.original_table([v[ds] for v in dec],[v[ds] for v in clf],ds,True);outer=ds
   examples=[(k,v['x']) for k,v in list(tab.items())[:2]]
  else:
   outer='ALL';dn='aasd_c1_bestP' if ds=='AASD' else f'ext_c1_{ds}_bestP'
   def load(n):return [N.load(os.path.join(EXP,'results','research',n.format(i=i))) for i in range(4)]
   ed=load(dn+'_evidence_s{i}.pkl');ec=load(f'ext_clf_{ds}_w10b3sw2P_evidence_s{{i}}.pkl')
   examples=[(k,F.join(ed,ec,k)[0]) for k in list(ed[0])[:2]]
  for seed in (0,1):
   ck=torch.load(os.path.join(M.OUT,f'model_fusion_{outer}_s{seed}.pt'),map_location='cpu',weights_only=True)
   assert ck['outer']==outer and ck['variant']=='fusion';scale=np.asarray(ck['scale'],dtype='f')
   model=M.MaskedRateNet(16).to(dev);model.load_state_dict(ck['state']);model.eval()
   for key,x in examples:
    a=M.predict(model,x,scale,dev);b=M.predict(model,-x,scale,dev)
    sw=float(np.max(np.abs(a+b)));p=min(40,len(x)//2);assert p>=4
    xx=x.copy();xx[p:]+=rng.normal(size=xx[p:].shape).astype('f')*scale*20
    c=M.predict(model,xx,scale,dev);past=float(np.max(np.abs(a[:p]-c[:p])))
    # The same trial must not retain history from a preceding call.
    M.predict(model,xx,scale,dev);reset=float(np.max(np.abs(a-M.predict(model,x,scale,dev))))
    pp=hmm_filter(-.5*(a-.5)**2,-.5*(a+.5)**2,.998);qq=hmm_filter(-.5*(c-.5)**2,-.5*(c+.5)**2,.998)
    hmm_past=float(np.max(np.abs(pp[:p]-qq[:p])))
    # Negative control: a 4-step future shift must contaminate the past.
    shift=lambda v:np.concatenate((v[4:],np.repeat(v[-1:],4,axis=0)))
    n1=M.predict(model,shift(x),scale,dev);n2=M.predict(model,shift(xx),scale,dev)
    neg=float(np.max(np.abs(n1[:p]-n2[:p])))
    rows.append(dict(ds=ds,seed=seed,trial=key,swap_max=sw,past_change=past,reset_error=reset,hmm_past_change=hmm_past,negative_change=neg))
    assert sw<1e-5 and past<1e-5 and reset<1e-5 and hmm_past<1e-5 and neg>1e-7,rows[-1]
  print(ds,'both seeds and two trials passed',flush=True)
 out=os.path.join(EXP,'results','research','audit_masked_trained_evidence.json')
 with open(out,'w') as f:json.dump(rows,f,indent=2)
 for k in ('swap_max','past_change','reset_error','hmm_past_change'):print(k,'max',max(r[k] for r in rows),flush=True)
 print('negative_change min',min(r['negative_change'] for r in rows),flush=True)
 print('PASS only at held-out evidence boundary; no claim about missing decoder weights or raw preprocessing',flush=True)
if __name__=='__main__':main()
