"""Re-run CPU-only examples and tests. Never invokes the GPU execution flag."""
import ast
import json
from pathlib import Path
import subprocess
import sys
import tempfile
from datetime import datetime, timezone

ROOT = Path(__file__).parent.resolve()
def run(args):
    result = subprocess.run([sys.executable, *map(str,args)], cwd=ROOT, text=True, capture_output=True)
    if result.returncode:
        raise RuntimeError(result.stdout+result.stderr)
    return result

suites = {}
for folder in ("toy-examples","training-examples","moe-examples"):
    result = run(["-m","unittest","discover","-s",folder,"-p","test*.py","-v"])
    suites[folder] = {"passed":True,"output":result.stderr}
for path in ROOT.rglob("*.py"):
    ast.parse(path.read_text(), filename=str(path))
toy = json.loads(run(["toy-examples/distill_toy.py"]).stdout)
pruning = json.loads(run(["toy-examples/prune_quantize_toy.py"]).stdout)
plan = json.loads(run(["moe-examples/plan.py","moe-examples/fixtures/config-ornith-observed.json","--prune","0.2","--prune","0.3","--prune","0.5"]).stdout)
with tempfile.TemporaryDirectory() as tmp:
    temp = Path(tmp)
    dataset = json.loads(run(["training-examples/prepare_distill_dataset.py","--input", "training-examples/teacher_candidates.jsonl","--output-dir",temp/"dataset"]).stdout)
    model = temp/"config-fixture-only"
    model.mkdir()
    (model/"config.json").write_text(json.dumps({"architectures":["Qwen3_5ForConditionalGeneration"]}))
    smoke = json.loads(run(["training-examples/train_lora_smoke.py","--model-dir",model,"--data",temp/"dataset/train.jsonl","--output",temp/"adapter"]).stdout)
report = {"checked_at":datetime.now(timezone.utc).isoformat(),"scope":"CPU tests + synthetic fixtures; no Ornith GPU training or serving", "suites":suites,"distillation_toy":toy,"pruning_quantization_toy":pruning,"moe_plan":plan,"synthetic_dataset":dataset,"lora_fixture_plan":smoke,"gpu_executed":False}
(ROOT/"verification.json").write_text(json.dumps(report,ensure_ascii=False,indent=2)+"\n")
print(json.dumps({"test_suites":list(suites),"toy_kl_before":toy["held_out_kl_before"],"toy_kl_after":toy["held_out_kl_after"],"lora":smoke["status"],"gpu_executed":False},indent=2))
