#!/usr/bin/env python3 """Follow-up to normalize.py: artifact.json carries content_sha256 = value_hash(manifest without that key) (embodied_pipeline.artifacts.canonical). Recompute it for every rewritten artifact.json, then propagate the changed raw / content digests to any staged text file embedding them, until nothing changes. Writes new files only.""" import hashlib, json, os, re, sys from pathlib import Path H = Path('${LOCAL}/simulation-workspace/runs/hf-release-20261120') DISK = H / 'stage'; SHM = Path('${LOCAL}/shm/ent400/hf-release-20261120/stage') sha = lambda b: hashlib.sha256(b).hexdigest() canon = lambda v: json.dumps(v, ensure_ascii=False, sort_keys=True, separators=(',', ':'), allow_nan=False).encode() def where(rel): for r in (DISK, SHM): if (r / rel).is_file(): return r / rel def put(rel, nb): root = SHM if rel.startswith('ent400/') else DISK tmp = root / (rel + '.norm-tmp'); tmp.write_bytes(nb); os.replace(tmp, root / rel) scan = {x['rel']: x for x in json.load(open(H / 'work/scan.json'))} texts = [r for r, x in scan.items() if x['text'] and x['size'] < 60_000_000 and where(r) and not (r.startswith('code/evaluation-code/'))] rep = {}; touched = set(); rounds = 0 pending = {r for r in json.load(open(H / 'work/normalize-digests.json'))['order'] if r.endswith('/artifact.json')} while pending: rounds += 1; nxt = set() for rel in sorted(pending): p = where(rel); b = p.read_bytes(); t = b.decode() for o, n in rep.items(): t = t.replace(o, n) if rel.endswith('/artifact.json'): obj = json.loads(t) old = obj.get('content_sha256') if old: new = sha(canon({k: v for k, v in obj.items() if k != 'content_sha256'})) if new != old: assert t.count(old) == 1, rel t = t.replace(old, new); rep[old] = new nb = t.encode() if nb != b: rep[sha(b)] = sha(nb); put(rel, nb); touched.add(rel) # files embedding any changed digest must be revisited keys = [k.encode() for k in rep] for rel in texts: if rel in pending: continue b = where(rel).read_bytes() if any(k in b for k in keys): t = b.decode(); t2 = t for o, n in rep.items(): t2 = t2.replace(o, n) if t2 != t: nxt.add(rel) pending = nxt print('round', rounds, 'touched', len(touched), 'next', len(nxt), flush=True) d = json.load(open(H / 'work/normalize-digests.json')); d['rep'].update(rep) d['order'] += [r for r in sorted(touched) if r not in d['order']] json.dump(d, open(H / 'work/normalize-digests.json', 'w')) print('done: files touched', len(touched), 'digests remapped', len(rep))