#!/usr/bin/env python3
"""compare_scores.py PUBLISHED NEW [--report]: two s1_score.py outputs must agree leaf for leaf: same key structure,
same value types, numbers equal exactly (compared by repr), strings equal, except the relocatable path fields
(/run, /slice/lines_dir, /held_patches/eval_set, /anchors_train/records_file, /anchors_held/records_file), which are
listed but not compared. Exit 0 = identical in that sense; --report prints the same but never fails."""
import json, sys
PATHS = {'/run', '/slice/lines_dir', '/held_patches/eval_set', '/anchors_train/records_file', '/anchors_held/records_file'}
def leaves(v, p=''):
    if isinstance(v, (dict, list)) and not v:
        yield p, type(v)()   # empty containers count as leaves
    elif isinstance(v, dict):
        for k in sorted(v): yield from leaves(v[k], f'{p}/{k}')
    elif isinstance(v, list):
        for i, x in enumerate(v): yield from leaves(x, f'{p}[{i}]')
    else:
        yield p, v
a = dict(leaves(json.load(open(sys.argv[1])))); b = dict(leaves(json.load(open(sys.argv[2]))))
structure = sorted(set(a) ^ set(b))
common = sorted(set(a) & set(b))
diff = [(k, a[k], b[k]) for k in common if k not in PATHS and (type(a[k]) is not type(b[k]) or repr(a[k]) != repr(b[k]))]
paths = [(k, a[k], b[k]) for k in common if k in PATHS and a[k] != b[k]]
diff += [(k, a[k], b[k]) for k in common if k in PATHS and not (isinstance(a[k], str) and isinstance(b[k], str) and b[k])]
nnum = sum(1 for k in common if not isinstance(a[k], str))
print(f'leaves compared: {len(common) - len(paths)} ({nnum} numeric); differences: {len(diff)}; structural differences: '
      f'{len(structure)}; relocated path fields: {len(paths)}')
for k, x, y in diff[:40]: print(f'  DIFF {k}: {x!r} -> {y!r}')
for k in structure[:20]: print(f'  KEY {k} only in {"published" if k in a else "new"}')
for k, x, y in paths: print(f'  path {k}: {x!r} -> {y!r}')
sys.exit(0 if '--report' in sys.argv or not (diff or structure) else 1)
