"""Read-only observation of a real T3 server's migrated SQLite database.
Usage: python3 observe.py BASE_HOME HEAD_HOME OUTPUT_JSONL
Run after starting both servers on identical stopped V1 copies; keep them active.
"""
import sqlite3,sys,time,json,pathlib,hashlib
homes=[pathlib.Path(p) for p in sys.argv[1:3]]
start=time.monotonic()
with open(sys.argv[3],'w') as out:
 for sample in range(73):
  for side,home in zip(['base','head'],homes):
   p=home/'userdata/statev2.sqlite'
   if not p.exists(): continue
   try:
    with sqlite3.connect(f'file:{p}?mode=ro',uri=True) as c:
     row={'side':side,'elapsedSeconds':round(time.monotonic()-start,1)}
     row.update({k:c.execute('pragma '+k).fetchone()[0] for k in ['auto_vacuum','page_count','freelist_count','page_size']})
     row['allocatedBytes']=row['page_count']*row['page_size']
     row['reclaimableBytes']=row['freelist_count']*row['page_size']
     row['v1Sha256']=hashlib.sha256((home/'userdata/state.sqlite').read_bytes()).hexdigest()
     print(json.dumps(row),flush=True);out.write(json.dumps(row)+'\n');out.flush()
   except sqlite3.Error as e: print(type(e).__name__,flush=True)
  time.sleep(5)
