iferr=tx.QueryRow(ctx,`SELECT EXISTS(SELECT 1 FROM device_binding WHERE device_id=$1::uuid AND generation=$2 AND unbound_at IS NULL) OR EXISTS(SELECT 1 FROM device_account_binding WHERE device_id=$1::uuid AND generation=$2 AND released_at IS NULL)`,d.ID,d.Generation).Scan(&bound);err!=nil{
tag,err:=tx.Exec(ctx,`UPDATE device_activation_attempt SET status='UNKNOWN' WHERE activation_id=$1::uuid AND status='CONTACT_POSSIBLE' AND permit_expires_at < $2 AND executor IN ('DEVICE','SERVER')`,j.ID,s.now().Add(-90*time.Second))
_,err=tx.Exec(ctx,`UPDATE device_activation_job SET status='RECONCILE_REQUIRED',last_error_code='DEVICE_RESULT_UNCERTAIN',version=version+1,updated_at=now() WHERE id=$1::uuid`,j.ID)
// Never rotate the configuration under a potentially running SDK call.
varbusybool
iferr:=tx.QueryRow(ctx,`SELECT EXISTS(SELECT 1 FROM device_activation_attempt WHERE activation_id=$1::uuid AND status IN ('CONTACT_POSSIBLE','UNKNOWN'))`,j.ID).Scan(&busy);err!=nil{
_,err=tx.Exec(ctx,`UPDATE device_activation_job SET provider_instance_id=$2,provider_product_key=$3,provider_bot_id=$4,config_version=$5,config_ciphertext=$6,config_digest=$7,config_expires_at=$8,config_ack_at=NULL,engine_ready_at=$9,status=$10,version=version+1,updated_at=now() WHERE id=$1::uuid`,j.ID,j.Instance,j.Product,j.Bot,j.ConfigVersion,j.Config,j.Digest,expires,j.Engine,j.Status)
iferr=tx.QueryRow(ctx,`SELECT key_version,ciphertext FROM device_onboarding_key WHERE device_id=$1::uuid AND status='ACTIVE'`,d.ID).Scan(&version,&encrypted);err!=nil{
iferr=tx.QueryRow(ctx,`SELECT CASE WHEN config_ack_boot_id=$2::uuid THEN COALESCE(config_local_generation,0) ELSE 0 END FROM device_activation_job WHERE id=$1::uuid`,j.ID,current.BootID).Scan(&previous);err!=nil{
_,err=tx.Exec(ctx,`UPDATE device_activation_job SET config_ack_at=COALESCE(config_ack_at,now()),engine_ready_at=now(),config_local_generation=$2,config_ack_boot_id=$3::uuid,updated_at=now() WHERE id=$1::uuid`,j.ID,in.LocalGeneration,current.BootID)
err=tx.QueryRow(ctx,`SELECT status,config_version,binding_generation,permit_expires_at FROM device_activation_attempt WHERE id=$1::uuid AND activation_id=$2::uuid AND executor='DEVICE'`,in.AttemptID,j.ID).Scan(&status,&version,&gen,&expires)
iferr=tx.QueryRow(ctx,`SELECT EXISTS(SELECT 1 FROM device_activation_attempt WHERE activation_id=$1::uuid AND status IN ('CONTACT_POSSIBLE','UNKNOWN')),count(*) FILTER(WHERE created_at>now()-interval '1 hour') FROM device_activation_attempt WHERE activation_id=$1::uuid AND executor='DEVICE'`,j.ID).Scan(&busy,&recent);err!=nil{
_,err=tx.Exec(ctx,`INSERT INTO device_activation_attempt(id,activation_id,executor,stage,binding_generation,config_version,status,permit_expires_at) VALUES($1::uuid,$2::uuid,'DEVICE',$3,$4,$5,'CONTACT_POSSIBLE',$6)`,in.AttemptID,j.ID,stage,current.Generation,j.ConfigVersion,expires)
iferr!=nil{
returnnil,err
}
_,err=tx.Exec(ctx,`UPDATE device_activation_job SET status='CONTACT_POSSIBLE',current_attempt_id=$2::uuid,version=version+1,updated_at=now() WHERE id=$1::uuid`,j.ID,in.AttemptID)
err=tx.QueryRow(ctx,`SELECT stage,status,binding_generation,config_version,result_digest,conversation_id::text,call_session_id::text FROM device_activation_attempt WHERE id=$1::uuid AND activation_id=$2::uuid AND executor='DEVICE' FOR UPDATE`,in.AttemptID,in.ActivationID).Scan(&stage,&status,&gen,&version,&stored,&conversation,&callSession)
err=tx.QueryRow(ctx,`SELECT EXISTS(SELECT 1 FROM call_session c JOIN call_event a ON a.session_id=c.id AND a.event_type='MEDIA_CONNECTED' JOIN call_event b ON b.session_id=c.id AND b.event_type='FIRST_AUDIO_RECEIVED' JOIN call_event p ON p.session_id=c.id AND p.event_type='FIRST_PLAYBACK_STARTED' WHERE c.id=$1::uuid AND c.device_id=$2::uuid AND c.binding_generation=$3 AND c.purpose='FIRST_TEST' AND c.call_state='ENDED' AND c.local_audio_off AND c.sdk_stopped AND a.seq<b.seq AND b.seq<=p.seq AND c.ended_at-c.created_at<=interval '90 seconds')`,*callSession,current.ID,current.Generation).Scan(&valid)
_,err=tx.Exec(ctx,`UPDATE device_activation_attempt SET status=$2,sdk_result=$3,result_digest=$4,completed_at=now() WHERE id=$1::uuid`,in.AttemptID,in.Outcome,in.SDKResult,digest(raw))
iferr!=nil{
returnnil,err
}
target:="RESERVED"
ifj.Registered!=nil{
target="REGISTERED_OBSERVED"
}
ifin.Outcome=="UNKNOWN"{
target="RECONCILE_REQUIRED"
}
ifin.Outcome=="SUCCESS_OBSERVED"{
ifin.Stage=="SDK_REGISTER"{
target="REGISTERED_OBSERVED"
_,err=tx.Exec(ctx,`UPDATE device_activation_job SET sdk_registered_at=COALESCE(sdk_registered_at,now()),engine_ready_at=now() WHERE id=$1::uuid`,j.ID)
}else{
target="READY"
_,err=tx.Exec(ctx,`UPDATE device_activation_job SET ready_binding_generation=$2 WHERE id=$1::uuid`,j.ID,current.Generation)
_,err=tx.Exec(ctx,`UPDATE device_activation_job SET status=$2,last_error_code=CASE WHEN $2='RECONCILE_REQUIRED' THEN 'DEVICE_RESULT_UNCERTAIN' ELSE NULL END,version=version+1,updated_at=now() WHERE id=$1::uuid`,j.ID,target)
err:=tx.QueryRow(ctx,`SELECT EXISTS(SELECT 1 FROM conversation c JOIN hardware_agent_event a ON a.conversation_id=c.id AND a.event_type='session_started' JOIN hardware_agent_event b ON b.conversation_id=c.id AND b.event_type='first_audio_received' JOIN hardware_agent_event z ON z.conversation_id=c.id AND z.event_type='session_ended' WHERE c.id=$1::uuid AND c.device_id=$2::uuid AND c.onboarding_generation=$3 AND c.status='COMPLETED' AND a.sequence_no<b.sequence_no AND b.sequence_no<z.sequence_no AND z.created_at-a.created_at<=interval '90 seconds')`,conversation,d.ID,d.Generation).Scan(&valid)