main
John Lauer Record native Kelvin pour transaction and recovered zone creation receipts; advance board 19 3cd9b1c
"""Recover this project's zone ownership from archived native creation receipts.
No board changes, no synthetic gate outcome. Never infer ownership from names.
Run only while no AIFlow command is active.
"""
import json,hashlib,datetime,shutil
from pathlib import Path
P=Path(__file__).resolve().parents[1];run=P/'aiflow/run/run.json'
r=json.loads(run.read_text());read=json.loads((P/'aiflow/run/land-pours-log.json').read_text())['after']
assert read.get('success') and read.get('boardPath','').replace('\\','/')==r['remoteBoard'].replace('\\','/')
assert read['revision']=='87c2e5c602f4156bb4fe3bd6bf3e387dac64b66a8f94c44fcc6a00848c19ef2b'
receipts=['design/ground-zone-add.json','design/top-ground-add.json','design/thermal-regions-live/land.json','design/current-rework-live/60-new-power-spread-zones.json','design/shunt-final-live/04-kicad_apply_zone_plan.json']
live={z['id']for z in read['zones'] if z['type']=='copper'};proof={}
for rel in receipts:
 p=P/rel;d=json.loads(p.read_text())
 assert d.get('success') is True and d.get('mutated') is True and d.get('mechanism')=='ipc' and not d.get('error')
 for ident in d['itemIds']:
  if ident in live:proof[ident]={'receipt':rel,'sha256':hashlib.sha256(p.read_bytes()).hexdigest()}
assert len(proof)==7 and set(proof)==live,'Every adopted zone must have an actual native creation receipt'
assert not r.get('ownedZoneIds'),'Refuse overwriting prior ownership metadata'
backup=P/'design/step10-run-before-ownership.json';assert not backup.exists();shutil.copy2(run,backup)
t=datetime.datetime.now(datetime.timezone.utc).isoformat()
evidence={'scope':'project-native-receipt-reconciliation','t':t,'nativeRevision':read['revision'],'boardSha256':hashlib.sha256((P/'kicad/adom-custom-electronic-load.kicad_pcb').read_bytes()).hexdigest(),'ownershipProof':proof,'boardMutated':False,'gateOutcomesChanged':False}
(P/'design/step10-zone-ownership.json').write_text(json.dumps(evidence,indent=2)+'\n')
r['ownedZoneIds']=sorted(proof);r['zoneOwnershipReconciliation']=evidence
run.with_suffix('.tmp').write_text(json.dumps(r,indent=2)+'\n');run.with_suffix('.tmp').replace(run)
with (P/'aiflow/run/run.jsonl').open('a') as f:f.write(json.dumps({'t':t,'event':'zone-ownership-reconciled','step':'pours','evidence':'design/step10-zone-ownership.json','nativeCreationIds':sorted(proof),'boardMutated':False})+'\n')
print('Recovered7 owned zone IDs from5 native creation receipts; no board or gate outcome changed.')