"""held_ids.py RUN_DIR : rewrite RUN_DIR/satisfied_fitted.json patch ids from object-store UUIDs to the patch names the
frozen eval set uses (store_m6/holdout_uuids.json maps name -> uuid). The original is kept as satisfied_fitted.uuid.json."""
import json, os, sys
run = sys.argv[1]; here = os.path.dirname(os.path.abspath(__file__))
name_of = {u: n for n, u in json.load(open(f'{here}/holdout_uuids.json'))['patches'].items()}
p = f'{run}/satisfied_fitted.json'; orig = f'{run}/satisfied_fitted.uuid.json'
if not os.path.exists(orig):  # idempotent: a rerun maps from the preserved UUID original, never from mapped names
    os.replace(p, orig)
d = json.load(open(orig))
hit = 0
for e in d['patches']:
    if e['id'] in name_of: e['id'] = name_of[e['id']]; hit += 1
json.dump(d, open(p, 'w'))
print(f'held_ids: {hit} of {len(name_of)} held-out patches found among {len(d["patches"])} entries')
