#!/usr/bin/env python3
"""LABELLED SYNTHETIC PROJECTION HARNESS, not provider execution.
Run only with the isolated server stopped, after native New task sent T50.
Preserves native-created persisted title and user message. Adds raw 200-unit
activity inputs to its completed run, through the documented SQLite helper.
"""
import json,pathlib,sqlite3,subprocess,sys
checkout=pathlib.Path(sys.argv[1]);tid=sys.argv[2];root=pathlib.Path(__file__).parent
con=sqlite3.connect(f'file:{checkout}/.t3/userdata/statev2.sqlite?mode=ro',uri=True)
items=[json.loads(p) for p, in con.execute('SELECT payload_json FROM orchestration_v2_projection_turn_items WHERE thread_id=?',(tid,))]
user=next(x for x in items if x['type']=='user_message' and x['creationSource']=='mobile')
assert user['text']=='Text '+'\u200b'*44+'😀 tail'
run=json.loads(con.execute('SELECT payload_json FROM orchestration_v2_projection_runs WHERE run_id=?',(user['runId'],)).fetchone()[0])
assert run['status']=='completed',run
timestamp='2026-10-27T14:10:00.000Z'
def lit(v):
 if v is None:return 'NULL'
 if isinstance(v,int):return str(v)
 return "'"+str(v).replace("'","''")+"'"
sql=[];seed=[]
for n,(kind,text) in enumerate([('command_execution','Cmd '+'\u200b'*195+'😀 tail'),('system_notice','Note '+'\u200b'*194+'😀 tail'),('system_notice','Note '+'\u200b'*194+'😀 tail'),('system_notice','Keep '+'\u200b'*193+'😀 tail')],1):
 item={k:user[k] for k in ['threadId','runId','nodeId','providerThreadId','providerTurnId','nativeItemRef','parentItemId']}
 item.update(id=f'evidence-24611-native-{n}',ordinal=user['ordinal']+n,status='completed',title=None,startedAt=timestamp,completedAt=timestamp,updatedAt=timestamp,type=kind)
 if kind=='command_execution':item.update(input=text,output='ok',exitCode=0)
 else:item.update(message=text)
 seed.append(item)
 vals=[item[k] for k in ['id','threadId','runId','nodeId','providerThreadId','providerTurnId','parentItemId','ordinal','type','status','updatedAt']]+[json.dumps(item,ensure_ascii=True,separators=(',',':'))]
 sql.append('INSERT OR REPLACE INTO orchestration_v2_projection_turn_items VALUES ('+','.join(map(lit,vals))+');')
# The actual provider result stays in event history. This harness claims only the
# native renderer of these synthetic projection rows, with an identical settled tail.
for item in items:
 if item['type']!='user_message':
  sql.insert(0,'DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id='+lit(item['id'])+';')
path=root/(checkout.name+'-native.sql');path.write_text('\n'.join(sql)+'\n')
(root/(checkout.name+'-native-raw.json')).write_text(json.dumps(seed,ensure_ascii=True,indent=2))
subprocess.run(['node','apps/server/scripts/t3-sqlite-state.ts','exec','--base-dir',str(checkout/'.t3'),'--file',str(path)],cwd=checkout,check=True)
print('Seeded raw C200, compact M200, expanded-candidate M200 and E200. Command was not executed.')
