import sqlite3,json,collections
c=sqlite3.connect('file:/home/ubuntu/wt/issue-25157/.t3/userdata/statev2.sqlite?mode=ro',uri=True)
for typ in ['message.updated','turn-item.updated']:
 rows=c.execute('select payload_json from orchestration_events where event_type=?',(typ,)).fetchall()
 groups=collections.defaultdict(list)
 for (raw,) in rows:
  p=json.loads(raw)
  if p.get('text'):groups[p['id']].append((len(p['text'].encode()),p.get('updatedAt')))
 print(typ,'committed text updates:',sum(map(len,groups.values())), 'per-item lengths and timestamps:',list(groups.values()))
for table in ['messages','turn_items']:
 rows=c.execute(f"select payload_json,updated_at from orchestration_v2_projection_{table}").fetchall()
 mismatches=sum(json.loads(p)['updatedAt']!=t for p,t in rows)
 print(table,'rows:',len(rows),'timestamp mismatches:',mismatches)
 if table=='messages':
  print('message created_at mismatches:',c.execute("select count(*) from orchestration_v2_projection_messages where created_at != json_extract(payload_json,'$.createdAt')").fetchone()[0])
