import sqlite3,json,sys
from pathlib import Path
checkout,side=sys.argv[1:]
c=sqlite3.connect('file:'+str(Path(checkout)/'.t3/userdata/statev2.sqlite')+'?mode=ro',uri=True)
items=[]
for (payload,) in c.execute("select payload_json from orchestration_v2_projection_turn_items where type='command_execution'"):
 p=json.loads(payload)
 s=p.get('input','')
 if not isinstance(s,str) or not s.startswith('[Earlier output truncated]'):continue
 units=s.encode('utf-16-le',errors='surrogatepass')
 items.append({'source':'Real server persisted command item','status':p['status'],'prefix':ascii(s[:60]),'utf16Length':len(units)//2,'isWellFormed':not any(0xd800<=ord(ch)<=0xdfff for ch in s),'markerCount':s.count('[Earlier output truncated]'),'codeUnitAtBoundary':hex(int.from_bytes(units[56:58],'little')),'rawInputRetainedInProjection':'rawInput' in p})
print(json.dumps({side:items},indent=2))
