Files
2026-11-23 03:28:32 +02:00

68 lines
3.8 KiB
Python

"""Head models back to native HD (owner 2026-11-23).
For every imported Octo head M2 whose path exists natively, point its ROOT cell at the native HD
file's content. Octo's copies are 1.12-head shaped and sit inside HD heads. Path-less Turtle helms
are not touched (refit is separate). Writes root_selector pins, components.json, selected-root.json."""
import hashlib, json, struct, sys
from pathlib import Path
H = Path(__file__).resolve().parent; sys.path.insert(0, str(H))
import frozen_store as fs
C = fs.casc
PAIRS = Path('/mnt/Games/@project/gateway/runtime/continuation-20261113/claude-lane/helm-hd-reinstate-20261123/helm-model-pairs.json')
def md5(b): return hashlib.md5(b).hexdigest()
def sha(b): return hashlib.sha256(b).hexdigest()
def main():
pairs = json.load(open(PAIRS))['pairs']
store = fs.Store(); root = store.root; lay = store.layout
assert lay['root_md5'] == fs.ROOT
ids = {p['imported_fdid'] for p in pairs} | {p['native_fdid'] for p in pairs}
cells = C.root_controls_batch(root, lay, ids)
# one ROOT cell offset per imported FDID (same walk as the cb30 helm alias stage)
want = {p['imported_fdid'] for p in pairs}; offsets = {}
for g in lay['groups']:
f = 0
for i, (delta,) in enumerate(struct.iter_unpack('<I', memoryview(root)[g['ids_offset']:g['ckeys_offset']])):
f += delta
if f in want: offsets.setdefault(f, []).append(g['ckeys_offset'] + i * 16)
f += 1
patched = bytearray(root); comps = []; skipped = []
for p in pairs:
imp, nat = p['imported_fdid'], p['native_fdid']
ic = cells.get(imp, []); nc = cells.get(nat, [])
if len(ic) != 1 or len(offsets.get(imp, [])) != 1:
skipped.append(dict(p, why=f'imported cells {len(ic)}')); continue
nkeys = {c['content_key'] for c in nc}
if len(nkeys) != 1:
skipped.append(dict(p, why=f'native content variants {len(nkeys)}')); continue
before_ck = ic[0]['content_key']; after_ck = nkeys.pop()
if before_ck == after_ck:
skipped.append(dict(p, why='already native')); continue
b = store.read(before_ck); a = store.read(after_ck)
if not b or not a:
skipped.append(dict(p, why='unreadable ' + ('before' if not b else 'after'))); continue
old, new = b[2], a[2]
if old[:4] != b'MD21' or new[:4] != b'MD21':
skipped.append(dict(p, why='not MD21')); continue
off = offsets[imp][0]; assert root[off:off + 16].hex() == before_ck
patched[off:off + 16] = bytes.fromhex(after_ck)
d = H / 'pins' / str(imp); d.mkdir(parents=True, exist_ok=True)
(d / 'before.m2').write_bytes(old); (d / 'after.m2').write_bytes(new)
pin = dict(fdid=imp, parent_root=fs.ROOT, selected_group={},
original_decoded=dict(file='before.m2', md5=md5(old), sha256=sha(old)),
after_decoded=dict(file='after.m2', md5=md5(new), sha256=sha(new)),
reason='head model back to native HD (owner 2026-11-23)', native_fdid=nat,
path=p['path'], octo_archive=p['octo_archive'])
raw = (json.dumps(pin, indent=1) + '\n').encode(); (d / 'pin.json').write_bytes(raw)
comps.append(dict(file=str(d / 'pin.json'), sha256=sha(raw), type='root_selector'))
(H / 'components.json').write_text(json.dumps(comps, indent=2) + '\n')
(H / 'selected-root.json').write_text(json.dumps(dict(
parent_build=fs.BUILD, parent_root=fs.ROOT, decoded_root_sha256=sha(bytes(patched)),
parent_root_sha256=sha(root)), indent=2) + '\n')
(H / 'skipped.json').write_text(json.dumps(skipped, indent=1) + '\n')
print('pins', len(comps), 'skipped', len(skipped))
from collections import Counter; print(Counter(s['why'] for s in skipped))
if __name__ == '__main__': main()