_,_=pool.Exec(context.Background(),`DELETE FROM audit_event WHERE request_id='diagnostic-refresh-request'`)
_,_=pool.Exec(context.Background(),`DELETE FROM device_command WHERE tenant_id='10000000-0000-0000-0000-000000000001'::uuid AND idempotency_key=$1`,"ops-refresh:"+key)
_,_=pool.Exec(context.Background(),`DELETE FROM idempotency_record WHERE actor_key='ops:10000000-0000-0000-0000-000000000111' AND idempotency_key=$1`,key)
_,_=pool.Exec(context.Background(),`DELETE FROM conversation_state_history WHERE conversation_id=ANY($1::uuid[])`,internalIDs)
_,_=pool.Exec(context.Background(),`DELETE FROM rtc_task WHERE conversation_id=ANY($1::uuid[])`,internalIDs)
_,_=pool.Exec(context.Background(),`DELETE FROM outbox_event WHERE aggregate_id=ANY($1::uuid[])`,internalIDs)
_,_=pool.Exec(context.Background(),`DELETE FROM audit_event WHERE request_id LIKE 'diagnostic-actions-%'`)
_,_=pool.Exec(context.Background(),`DELETE FROM idempotency_record WHERE actor_key='ops:10000000-0000-0000-0000-000000000111' AND idempotency_key=ANY($1::text[])`,keys)
_,_=pool.Exec(context.Background(),`DELETE FROM web_event WHERE resource_public_id=ANY($1::uuid[])`,conversationIDs)
_,_=pool.Exec(context.Background(),`DELETE FROM conversation WHERE id=ANY($1::uuid[])`,internalIDs)
_,_=pool.Exec(context.Background(),`DELETE FROM context_snapshot WHERE id=$1::uuid`,contextID)
_,_=pool.Exec(context.Background(),`DELETE FROM consent_snapshot WHERE id=$1::uuid`,consentID)
}()
if_,err=pool.Exec(ctx,`INSERT INTO consent_snapshot(id,tenant_id,subject_id,snapshot_version,effective_consents) VALUES($1::uuid,$2::uuid,$3::uuid,9001,'{"voice_chat":true}'::jsonb)`,consentID,tenantID,subjectID);err!=nil{
t.Fatal(err)
}
if_,err=pool.Exec(ctx,`INSERT INTO context_snapshot(id,tenant_id,subject_id,profile_version,consent_snapshot_id,prompt_bundle_version,engine_mode,content_digest) VALUES($1::uuid,$2::uuid,$3::uuid,1,$4::uuid,'diagnostic-test','s2s_hybrid',repeat('9',64))`,contextID,tenantID,subjectID,consentID);err!=nil{
if_,insertErr:=pool.Exec(ctx,`INSERT INTO conversation(id,public_id,tenant_id,household_id,subject_id,device_id,consent_snapshot_id,context_snapshot_id,provider_task_ref,engine_mode,retention_mode,status,allocation_expires_at) VALUES($1::uuid,$2::uuid,$3::uuid,$4::uuid,$5::uuid,$6::uuid,$7::uuid,$8::uuid,$9,'s2s_hybrid','transcript_only',$10,now()+interval '5 minutes')`,internalIDs[index],conversationIDs[index],tenantID,householdID,subjectID,deviceID,consentID,contextID,taskRef,status);insertErr!=nil{
t.Fatal(insertErr)
}
if_,insertErr:=pool.Exec(ctx,`INSERT INTO rtc_task(conversation_id,provider,provider_task_ref,room_id,device_rtc_user_id,agent_rtc_user_id,status) VALUES($1::uuid,'mock',$2,$3,'device-test','agent-test',$4)`,internalIDs[index],taskRef,roomID,rtcStatus);insertErr!=nil{
iferr=pool.QueryRow(ctx,`SELECT status FROM rtc_task WHERE conversation_id=$1::uuid`,internalIDs[0]).Scan(&preStartRTCStatus);err!=nil||preStartRTCStatus!="COMPLETED"{
iferr=pool.QueryRow(ctx,`SELECT count(*) FILTER(WHERE event_type='conversation.agent_stop_requested'),count(*) FILTER(WHERE event_type LIKE 'conversation.agent_%_reconcile_requested') FROM outbox_event WHERE aggregate_id=$1::uuid`,internalIDs[1]).Scan(&stopEvents,&reconcileEvents);err!=nil{
_,_=pool.Exec(context.Background(),`DELETE FROM audit_event WHERE request_id LIKE 'diagnostic-recovery-%'`)
_,_=pool.Exec(context.Background(),`DELETE FROM idempotency_record WHERE actor_key='ops:10000000-0000-0000-0000-000000000111' AND idempotency_key=ANY($1::text[])`,keys)
_,_=pool.Exec(context.Background(),`DELETE FROM outbox_event WHERE id=ANY($1::uuid[]) OR aggregate_id=$2::uuid`,[]string{jobInternalID,unknownJobInternalID},conversationInternalID)
_,_=pool.Exec(context.Background(),`DELETE FROM conversation_state_history WHERE conversation_id=$1::uuid`,conversationInternalID)
_,_=pool.Exec(context.Background(),`DELETE FROM rtc_task WHERE conversation_id=$1::uuid`,conversationInternalID)
_,_=pool.Exec(context.Background(),`DELETE FROM web_event WHERE resource_public_id=$1::uuid`,conversationPublicID)
_,_=pool.Exec(context.Background(),`DELETE FROM conversation WHERE id=$1::uuid`,conversationInternalID)
_,_=pool.Exec(context.Background(),`DELETE FROM context_snapshot WHERE id=$1::uuid`,contextID)
_,_=pool.Exec(context.Background(),`DELETE FROM consent_snapshot WHERE id=$1::uuid`,consentID)
_,_=pool.Exec(context.Background(),`DELETE FROM device_twin WHERE device_id=$1::uuid`,deviceID)
_,_=pool.Exec(context.Background(),`DELETE FROM device WHERE id=$1::uuid`,deviceID)
}()
if_,err=pool.Exec(ctx,`INSERT INTO device(id,public_id,tenant_id,serial_digest,model,status,credential_ref) VALUES($1::uuid,$2::uuid,$3::uuid,$4,'diagnostic-test','registered','test://diagnostic-recovery')`,deviceID,devicePublicID,tenantID,strings.Repeat("8",64));err!=nil{
t.Fatal(err)
}
if_,err=pool.Exec(ctx,`INSERT INTO device_twin(device_id,tenant_id,desired_revision) VALUES($1::uuid,$2::uuid,3)`,deviceID,tenantID);err!=nil{
t.Fatal(err)
}
if_,err=pool.Exec(ctx,`INSERT INTO consent_snapshot(id,tenant_id,subject_id,snapshot_version,effective_consents) VALUES($1::uuid,$2::uuid,'10000000-0000-0000-0000-000000000401'::uuid,9101,'{"voice_chat":true}'::jsonb)`,consentID,tenantID);err!=nil{
t.Fatal(err)
}
if_,err=pool.Exec(ctx,`INSERT INTO context_snapshot(id,tenant_id,subject_id,profile_version,consent_snapshot_id,prompt_bundle_version,engine_mode,content_digest) VALUES($1::uuid,$2::uuid,'10000000-0000-0000-0000-000000000401'::uuid,1,$3::uuid,'diagnostic-revoke','s2s_hybrid',repeat('7',64))`,contextID,tenantID,consentID);err!=nil{
if_,err=pool.Exec(ctx,`INSERT INTO conversation(id,public_id,tenant_id,household_id,subject_id,device_id,consent_snapshot_id,context_snapshot_id,provider_task_ref,engine_mode,retention_mode,status,allocation_expires_at) VALUES($1::uuid,$2::uuid,$3::uuid,'10000000-0000-0000-0000-000000000201'::uuid,'10000000-0000-0000-0000-000000000401'::uuid,$4::uuid,$5::uuid,$6::uuid,$7,'s2s_hybrid','transcript_only','ACTIVE',now()+interval '5 minutes')`,conversationInternalID,conversationPublicID,tenantID,deviceID,consentID,contextID,taskRef);err!=nil{
t.Fatal(err)
}
if_,err=pool.Exec(ctx,`INSERT INTO rtc_task(conversation_id,provider,provider_task_ref,room_id,device_rtc_user_id,agent_rtc_user_id,status) VALUES($1::uuid,'mock',$2,$3,'diagnostic-device','diagnostic-agent','ACTIVE')`,conversationInternalID,taskRef,"diagnostic-revoke-room-"+conversationPublicID);err!=nil{
t.Fatal(err)
}
if_,err=pool.Exec(ctx,`INSERT INTO outbox_event(id,tenant_id,aggregate_type,aggregate_id,event_type,payload,status,attempt_count,last_error_code,dead_at,payload_hash) VALUES($1::uuid,$2::uuid,'platform',$3::uuid,'platform.noop','{}'::jsonb,'dead',8,'TEMPORARY_DEPENDENCY_FAILURE',now(),repeat('0',64))`,jobInternalID,tenantID,aggregateID);err!=nil{
t.Fatal(err)
}
if_,err=pool.Exec(ctx,`INSERT INTO outbox_event(id,tenant_id,aggregate_type,aggregate_id,event_type,payload,status,attempt_count,last_error_code,dead_at,payload_hash) VALUES($1::uuid,$2::uuid,'platform',$3::uuid,'unsupported.diagnostic_job','{}'::jsonb,'dead',8,'HANDLER_NOT_REGISTERED',now(),repeat('1',64))`,unknownJobInternalID,tenantID,aggregateID);err!=nil{
iferr=pool.QueryRow(ctx,`SELECT manual_retry_count FROM outbox_event WHERE id=$1::uuid`,jobInternalID).Scan(&manualRetries);err!=nil||manualRetries!=1{
iferr=pool.QueryRow(ctx,`SELECT status,revoked_at IS NOT NULL FROM device WHERE id=$1::uuid`,deviceID).Scan(&deviceStatus,&revokedAtPresent);err!=nil||deviceStatus!="revoked"||!revokedAtPresent{
iferr=pool.QueryRow(ctx,`SELECT count(*) FROM outbox_event WHERE aggregate_id=$1::uuid AND event_type='conversation.agent_stop_requested'`,conversationInternalID).Scan(&stopEvents);err!=nil||stopEvents!=1{