import sqlite3,json,time,sys,pathlib,re
side,db,out=sys.argv[1:]
p=pathlib.Path(out);p.mkdir(parents=True,exist_ok=True)
con=sqlite3.connect('file:'+db+'?mode=ro',uri=True)
start=time.monotonic(); seen=set(); rows=[]
cast=open(p/(side+'.cast'),'w');txt=open(p/(side+'.txt'),'w')
cast.write(json.dumps({'version':2,'width':120,'height':40,'timestamp':int(time.time()),'title':side+' real T3 server Monitor capture'})+'\n')
def emit(s):
 print(s,flush=True);txt.write(s+'\n');txt.flush();cast.write(json.dumps([round(time.monotonic()-start,3),'o',s+'\r\n'])+'\n');cast.flush()
emit(side+' | Claude Code 2.1.323 / Claude Haiku 5.5 | fresh T3 database')
while time.monotonic()-start<130:
 for r in con.execute("select m.message_id,m.created_at,m.role,m.payload_json,t.title from orchestration_v2_projection_messages m join orchestration_v2_projection_threads t on t.thread_id=m.thread_id order by m.created_at"):
  if r[0] in seen:continue
  seen.add(r[0]);j=json.loads(r[3]);s=j.get('text','')
  # Include actual monitor/parent output and notifications, not prompts or workspace details.
  if j.get('notification') or (r[2]=='assistant' and any(x in s for x in ['EVENT_','MONITOR_DONE','PARENT_','STARTED'])) or s.startswith('Delegated task '):
   s=re.sub(r'/tmp/[^`\s]+','<scratch project>',s)
   emit(r[1]+' '+r[4]+' '+r[2]+' '+s)
   if j.get('notification'):emit('notification='+json.dumps(j['notification']))
   rows.append({'at':r[1],'thread':r[4],'role':r[2],'text':s,'notification':j.get('notification')})
 time.sleep(1)
emit('Final delegated task rows:')
for r in con.execute("select origin,status,completion_delivery_state,started_at,completed_at from orchestration_v2_projection_subagents"):
 emit(json.dumps(r))
(p/(side+'.json')).write_text(json.dumps(rows,indent=2))
