"""Read-only, privacy-filtered evidence of the real server's persisted state."""
import json,pathlib,sqlite3,sys
root=pathlib.Path(sys.argv[1])
db=sqlite3.connect(f'file:{root}/.t3/userdata/statev2.sqlite?mode=ro',uri=True)
subs=[json.loads(r[0]) for r in db.execute('select payload_json from orchestration_v2_projection_subagents')]
child=next(s['childThreadId'] for s in subs if s['origin']=='app_owned')
pts=[json.loads(r[0]) for r in db.execute('select payload_json from orchestration_v2_projection_provider_threads')]
p=next(p for p in pts if p['appThreadId']==child)
runs=[json.loads(r[0]) for r in db.execute('select payload_json from orchestration_v2_projection_runs where thread_id=? order by ordinal',(child,))]
print(json.dumps({'side':sys.argv[2],'commit':sys.argv[3],'childProviderStatus':p['status'],'pendingBackgroundTasks':[{'command':t.get('command'),'awaited':t.get('awaited',False)} for t in p['pendingBackgroundTasks']], 'completedParentContinuation':any(s['origin']=='app_owned' and 'provider-continuation' in s['id'] and s['status']=='completed' and s.get('providerThreadId')==p['id'] for s in subs),'childRuns':[{'ordinal':r['ordinal'],'status':r['status'],'restartCancelledBackgroundWork':r.get('restartCancelledBackgroundWork')} for r in runs]},indent=2))
