"""Capture actual tool replies from a completed drive.py run, using read-only SQLite."""
import argparse,json,pathlib,sqlite3
p=argparse.ArgumentParser();p.add_argument('run');p.add_argument('sha');p.add_argument('destination');a=p.parse_args()
run=pathlib.Path(a.run);dest=pathlib.Path(a.destination)
c=sqlite3.connect('file:'+str(run/'A/userdata/statev2.sqlite')+'?mode=ro',uri=True)
tools=[]
for (raw,) in c.execute('select payload_json from orchestration_v2_projection_turn_items order by ordinal'):
 item=json.loads(raw)
 if item.get('toolName','').split('__')[-1] in ['delegate_task','task_status','t3_thread_read']:
  tools.append({k:item[k] for k in ['toolName','status','startedAt','completedAt','input','output'] if k in item})
c.close()
result={'commit':a.sha,'environment':'Two real Node T3 servers on Linux, loopback HTTP, separate fresh databases; Linked environments enabled; full-access link; real Claude Haiku 5.5 provider through configured gateway. Synthetic projects created through CLI.','tools':tools}
# Keep the provider's tool outputs intact. Only provider thread identifiers and run IDs are synthetic.
dest.write_text(json.dumps(result,indent=2)+'\n')
print('Commit',a.sha)
for tool in tools:
 out=tool['output'].get('structuredContent')
 if out is None:
  try:out=json.loads(tool['output'].get('content','{}'))
  except:out={}
 print(tool['toolName'],tool['status'])
 print(json.dumps({k:out[k] for k in ['status','model','executedModel','summary','waitTimedOut','recentRuns'] if k in out},indent=2))
