"""verify_held.py PARENT_CKPT HX_RUN_DIR EXPECTED_ITERS : hard checks after a held-out pass (exit 1 on any failure).
- the Hx checkpoint is the parent model unchanged: same model_state_sha256, same completed_iterations == EXPECTED_ITERS;
- every frozen evaluation patch is present in satisfied_fitted.json (after held_ids) and scored (missing == 0);
- the scores were computed on that same model, and every reported metric is finite;
- the Hx log reports no sanitised non-finite-gradient steps (also checked for the parent's log by the chain).
Prints one TSCORE-ready line on success."""
import json, math, sys
import torch

parent_ckpt, run, expected = sys.argv[1], sys.argv[2], int(sys.argv[3])
fail = []
load = lambda p: torch.load(p, map_location='cpu', weights_only=False)
par, hx = load(parent_ckpt), load(f'{run}/checkpoint_fitted.ckpt')
for name, c in (('parent', par), ('Hx', hx)):
    if c['completed_iterations'] != expected:
        fail.append(f'{name} completed_iterations {c["completed_iterations"]} != {expected}')
if par['model_state_sha256'] != hx['model_state_sha256']:
    fail.append(f'model changed: parent {par["model_state_sha256"][:16]} Hx {hx["model_state_sha256"][:16]}')

s = json.load(open(f'{run}/scores_trim.json'))
eval_set = json.load(open(s['held_patches']['eval_set']))
names = eval_set['patches'] if isinstance(eval_set, dict) and 'patches' in eval_set else eval_set
names = set(names if not isinstance(names, dict) else names.keys())
present = {e['id'] for e in json.load(open(f'{run}/satisfied_fitted.json'))['patches']}
lost = sorted(n for n in names if n not in present)
if lost:
    fail.append(f'{len(lost)} of {len(names)} evaluation patches absent from satisfied_fitted.json (e.g. {lost[:3]})')
if not s['held_patches'].get('satisfied_file_present') or s['held_patches']['missing'] != 0:
    fail.append(f'held_patches missing={s["held_patches"]["missing"]}')
if s['model_state_sha256'] != hx['model_state_sha256']:
    fail.append('scores computed on a different model state')
a = s['slice']['windows']['all']
vals = {'f025': a['f025'], 'f05': a['f05'], 'held_patches': s['held_patches']['fraction'],
        'held_off': s['anchors_held']['equal_fiber_off_rate'], 'train_off': s['anchors_train']['equal_fiber_off_rate']}
for k, v in vals.items():
    if not isinstance(v, (int, float)) or not math.isfinite(v):
        fail.append(f'{k} not finite: {v}')
for k in ('anchors_held', 'anchors_train'):
    if s[k].get('nonfinite', 0):
        fail.append(f'{k} nonfinite={s[k]["nonfinite"]}')
if fail:
    print('VERIFY FAILED: ' + '; '.join(fail))
    sys.exit(1)
print('f025 %.4f f05 %.4f held_patches %.4f (%d eval patches, missing 0) held_off %.4f train_off %.4f; model %s at %d'
      % (vals['f025'], vals['f05'], vals['held_patches'], len(names), vals['held_off'], vals['train_off'],
         hx['model_state_sha256'][:12], expected))
