import json, sqlite3, sys
from pathlib import Path
from datetime import datetime, timezone
root, thread = Path(sys.argv[1]), sys.argv[2]
db=sqlite3.connect(f"file:{root / '.t3/userdata/statev2.sqlite'}?mode=ro", uri=True)
db.row_factory=sqlite3.Row
runs=[dict(r) for r in db.execute("SELECT ordinal,status,requested_at,completed_at FROM orchestration_v2_projection_runs WHERE thread_id=?",(thread,))]
children=[dict(r) for r in db.execute("SELECT status,started_at,completed_at FROM orchestration_v2_projection_subagents WHERE thread_id=?",(thread,))]
notices=[]
records=[]
for f in (root / '.t3/userdata/providers/antigravity').rglob('*.json'):
 if f.parent.name not in ('messages','subagents'): continue
 value=json.loads(f.read_text())
 if f.parent.name=='messages' and value.get('sender')=='system' and 'has gone idle' in value.get('content',''):
  notices.append({k:value[k] for k in ('sender','priority','timestamp','content','sourceMetadata') if k in value})
 if f.parent.name=='subagents': records.append({k:value[k] for k in ('conversationId','subagentDescriptor','state') if k in value})
print(json.dumps(dict(observedAt=datetime.now(timezone.utc).isoformat(),runs=runs,children=children,nativeIdleNotices=notices,nativeChildRecords=records),indent=2))
