"""Role isolation, full-row learning, early stopping and real guard receipts.""" import json from pathlib import Path import resource import sys import tempfile import unittest import numpy as np from gre24_models import xgb_model,fnn_model,eligible_roles,require_training_role,early_stop_update from gre24_job_guard import supervise class ModelChecks(unittest.TestCase): def test_role_isolation_and_missing_no_replacement(self): s=dict(train=['a','b'],test=['c','d'],fnn_learning=['a'],fnn_validation=['b']) roles,missing=eligible_roles(s,['a','c','d']) self.assertEqual(roles['train'],['a']);self.assertEqual(missing['train'],['b']) with self.assertRaises(PermissionError):require_training_role('c',s,'train') with self.assertRaises(PermissionError):require_training_role('a',s,'test') def test_earlystop_strict_improvement_patience(self): best=1.;stale=0 for i in range(5): best,stale,improved,stop=early_stop_update(best,stale,1.) self.assertFalse(improved);self.assertEqual(stop,i==4) best,stale,improved,stop=early_stop_update(best,stale,.9) self.assertTrue(improved);self.assertEqual(stale,0);self.assertFalse(stop) with self.assertRaises(ValueError):early_stop_update(best,stale,float('nan')) def test_xgboost_fixed_recipe_learns_all_rows(self): rng=np.random.default_rng(42);x=rng.normal(size=(512,19)).astype(np.float32);y=x[:,0]+.5*x[:,1] m=xgb_model(42,threads=1);m.fit(x,y) self.assertLess(np.mean((m.predict(x)-y)**2),.02) self.assertEqual(m.n_estimators,100);self.assertEqual(m.max_depth,8) def test_fnn_architecture_gradients_and_checkpoint_restore(self): import torch torch.set_num_threads(1);torch.manual_seed(42);m=fnn_model() linear=[a for a in m if isinstance(a,torch.nn.Linear)] self.assertEqual([(a.in_features,a.out_features) for a in linear],[(19,100),(100,100),(100,100),(100,100),(100,1)]) x=torch.randn(64,19);y=torch.ones(64,1);o=torch.optim.Adam(m.parameters(),lr=.001) before=float(((m(x)-y)**2).mean()) for _ in range(15): o.zero_grad();loss=((m(x)-y)**2).mean();loss.backward();o.step() self.assertLess(float(((m(x)-y)**2).mean()),before) with tempfile.TemporaryDirectory() as tmp: p=Path(tmp)/'m.pt';torch.save(m.state_dict(),p);n=fnn_model();n.load_state_dict(torch.load(p,weights_only=True)) torch.testing.assert_close(m(x),n(x)) def test_guard_records_failure_cpu_and_refuses_repeat(self): limits=dict(total_cpu_seconds=10,job_cpu_seconds=5,job_wall_seconds=5,tree_rss_bytes=256*2**20,disk_bytes=2**20,max_processes=1) with tempfile.TemporaryDirectory() as tmp: r=supervise([sys.executable,'-c','raise SystemExit(7)'],Path(tmp),'fail',limits,poll=.02) self.assertEqual(r['returncode'],7);self.assertGreater(r['cpu_seconds'],0) self.assertEqual(r['status'],'STOPPED_OR_FAILED') with self.assertRaises(FileExistsError):supervise([sys.executable,'-c','pass'],Path(tmp),'fail',limits) if __name__=='__main__': result=unittest.TextTestRunner(verbosity=2).run(unittest.defaultTestLoader.loadTestsFromTestCase(ModelChecks)) u=resource.getrusage(resource.RUSAGE_SELF);ch=resource.getrusage(resource.RUSAGE_CHILDREN) Path('artifacts/gre24-reproduction-2026-11-15/model-synthetic-receipt.json').write_text(json.dumps(dict( tests=result.testsRun,passed=result.wasSuccessful(),failures=len(result.failures),errors=len(result.errors), CPU_s=u.ru_utime+u.ru_stime+ch.ru_utime+ch.ru_stime,GPU_used=False),indent=2)) raise SystemExit(0 if result.wasSuccessful() else 1)