import pathlib,json,sqlite3,sys
for arg in sys.argv[1:]:
 run=pathlib.Path(arg); file=run/'responses.json'; d=json.loads(file.read_text())
 inspector=d['inspectorResult']['threadId']
 db=sqlite3.connect(f'file:{run}/a/userdata/statev2.sqlite?mode=ro',uri=True)
 calls=[]
 for (payload,) in db.execute("select payload_json from orchestration_v2_projection_turn_items where thread_id=? and type='dynamic_tool' order by ordinal",(inspector,)):
  o=json.loads(payload)
  calls.append({'tool':o['toolName'],'status':o['status'],'arguments':o['input'],'response':json.loads(o['output'])})
 d['inspectorToolCalls']=calls
 assert len(calls)==2, [c['tool'] for c in calls]
 for c in calls:
  if c['tool'].endswith('t3_thread_list'): d['listWithoutSubagents']['response']=c['response']
  elif c['tool'].endswith('t3_thread_read'): d['read']['response']=c['response']
  else: raise RuntimeError(c['tool'])
 text=json.dumps(d,indent=2)
 assert '/home/' not in text and '/tmp/' not in text
 file.write_text(text+'\n')
 print(file)
