"""Observe genuine saved keeper possessions; physical inputs only, no game writes."""
from pathlib import Path
import json
p=Path(outdir);spec=json.loads((p/'spec.json').read_text());result=dict(eligible=None,passed=None,first_switch=None,queued=False);last=None
def rw(a):return (wram[ram(a)]<<8)|wram[ram(a+1)]
def sw(a):v=rw(a);return v-65536 if v&32768 else v
def test_tick(f):
 global last
 if f<100:return
 g=0xb2ca
 if f==100:result['initial_control']=sw(0xc320)
 if f==100+spec['delay']:
  result['timer_at_press']=sw(g+0x48);result['flags_at_press']=rw(0xc2ea)
  result['queued_events_at_press']=[rw(0xc3a4+2*i) for i in range(32) if rw(0xc3a4+2*i)]
  result['eligible']=rw(0xb7aa)==5 and sw(g+0x48)>=0 and not wram[ram(0xc2ea)]&0x11 and not rw(0xd412) and not wram[ram(0xc2f2)]&4
  held[(0,IDS[spec['button']])]=spec['duration']
 if f>100 and result['passed'] is None and rw(0xb7aa)!=5 and rw(0xbeda)==5 and not wram[ram(0xc2ea)]&1:
  result['passed']=dict(frame=f-100,target=sw(0xbee0),controlled=sw(0xc320),velocity=[sw(0xb772),sw(0xb774)])
 if result['first_switch'] is None and sw(0xc320)!=result['initial_control']:result['first_switch']=dict(frame=f-100,control=sw(0xc320))
 if rw(0xf064):result['queued']=True
 result['final_owner']=sw(0xb7aa);result['final_flags']=rw(0xc2ea);result['final_timer']=sw(g+0x48)
 if result!=last:(p/'live.json').write_text(json.dumps(result,indent=2)+'\n');last=json.loads(json.dumps(result))
