"""Read only the two synthetic reproduction databases; emit no runtime identifiers."""
import argparse
import datetime
import json
from pathlib import Path
import sqlite3
import subprocess

p=argparse.ArgumentParser()
p.add_argument('--base',type=Path,required=True)
p.add_argument('--head',type=Path,required=True)
p.add_argument('--scratch',type=Path,required=True)
a=p.parse_args()
out=a.scratch/datetime.datetime.now(datetime.timezone.utc).strftime('verify-%Y%m%dT%H%M%S%fZ')
out.mkdir(parents=True)
report={}
for side,root in [('base',a.base),('head',a.head)]:
 db=sqlite3.connect(f'file:{root}/.t3/userdata/statev2.sqlite?mode=ro',uri=True)
 thread=db.execute("select thread_id,runtime_mode,provider_instance_id from orchestration_v2_projection_threads where title='Create Permission Evidence Note'").fetchone()
 if thread is None:
  raise RuntimeError('Synthetic evidence thread missing')
 settings=json.loads((root/'.t3/userdata/settings.json').read_text())
 reqs=db.execute('select kind,status from orchestration_v2_projection_runtime_requests where thread_id=? order by created_at',(thread[0],)).fetchall()
 note=(a.scratch/f'{side}-project/note-30538.txt').read_text()
 report[side]={
  'commit':subprocess.check_output(['git','-C',str(root),'rev-parse','HEAD'],text=True).strip(),
  'newThreadDefault':settings['defaultRuntimeMode'],
  'storedThreadMode':thread[1],
  'provider':thread[2],
  'projectPermissionOverrides':bool(settings.get('projectSettingsOverrides',{})),
  'runtimeApprovalRequests':[{'kind':kind,'status':status} for kind,status in reqs],
  'noteContents':note,
 }
 db.close()
report['interpretation']='Base write was manually approved in the recording. Head write needed no approval. The remaining pending request on each side is the shell command shown in the recordings.'
report['attribution']='AI-assisted; verified with the tests and tools listed above.'
(out/'runtime-observation.json').write_text(json.dumps(report,indent=2)+'\n')
print(json.dumps(report,indent=2))
