import sys,json,pathlib,sqlite3
result={}
for side,root in zip(('base','head'),sys.argv[1:]):
 root=pathlib.Path(root)
 db=sqlite3.connect(f'file:{root}/.t3/userdata/statev2.sqlite?mode=ro',uri=True)
 rows=db.execute("select thread_id,payload_json from orchestration_v2_projection_turn_items where type='user_message'").fetchall()
 thread=next(t for t,p in rows if 'Use WaitMsBeforeAsync=45000 for both commands' in json.loads(p).get('text',''))
 sub=db.execute('select status,payload_json from orchestration_v2_projection_subagents where thread_id=?',(thread,)).fetchone()
 child=json.loads(sub[1])
 transcripts=[]
 for f in (root/'.t3/userdata/providers/antigravity').glob('*/antigravity-acp/brain/*/.system_generated/logs/transcript.jsonl'):
  lines=[json.loads(l) for l in f.read_text().splitlines()]
  if not any('WaitMsBeforeAsync=45000' in r.get('content','') for r in lines): continue
  entries=[]
  for r in lines:
   entry={k:r[k] for k in ('step_index','created_at','source','type','status') if k in r}
   calls=[]
   for c in r.get('tool_calls',[]):
    if c['name']=='run_command': calls.append({'name':c['name'],'args':{k:v for k,v in c.get('args',{}).items() if k in ('CommandLine','WaitMsBeforeAsync')}})
    elif c['name'] in ('invoke_subagent','send_message','manage_subagents'): calls.append({'name':c['name']})
   if calls: entry['tools']=calls
   content=r.get('content','')
   if 'All your subagents and background tasks have been stopped due to user force cancellation.' in content:
    entry['notice']='All your subagents and background tasks have been stopped due to user force cancellation.'
   if content.strip()=='STEERED-DONE': entry['reply']=content
   if 'Command exited with code 0.' in content:entry['commandExitCode']=0
   if calls or 'notice' in entry or 'reply' in entry or 'commandExitCode' in entry: entries.append(entry)
  if entries:transcripts.append({'conversation':f.parts[-4],'events':entries})
 spans=[]
 for line in (root/'.t3/userdata/logs/server.trace.ndjson').read_text().splitlines():
  try:r=json.loads(line)
  except:continue
  if any(s in r.get('name','') for s in ('dispatchSteerIntoRun','interruptAndAwaitTerminal','interruptTurn','steerTurn')):
   spans.append({k:r[k] for k in ('name','traceId','startTimeUnixNano','durationMs') if k in r})
 result[side]={'sha':__import__('subprocess').check_output(['git','-C',str(root),'rev-parse','HEAD'],text=True).strip(),'subagent':{k:child.get(k) for k in ('title','status','startedAt','completedAt')},'childFile':(root/'.t3/projects/steering-proof-3/child.txt').read_text(),'transcripts':transcripts,'steerTraceSpans':spans}
print(json.dumps(result,indent=2))
