@@ -686,31 +686,73 @@ async function requestSignup(request: Request, env: Env) {
686686 }
687687 const database = db . db ;
688688 const authEvidence = await onboardingAuthEvidence ( input , env , requestedAt ) ;
689- await database
690- . prepare (
691- `INSERT INTO agent_identities
692- (id, handle, display_name, machine_scope, status, requested_at,
693- onboarding_auth_hash, onboarding_auth_status, onboarding_auth_length, onboarding_auth_checked_at)
694- VALUES (?, ?, ?, ?, 'pending', ?, ?, ?, ?, ?)` ,
695- )
696- . bind (
697- id ,
698- input . handle ,
699- input . displayName ,
700- input . machineScope ,
701- requestedAt ,
702- authEvidence . hash ,
703- authEvidence . status ,
704- authEvidence . length ,
705- authEvidence . checkedAt ,
706- )
707- . run ( ) ;
708- const profile = profileValues ( input , id ) ;
689+ const existing = await database
690+ . prepare ( "SELECT id, status, requested_at FROM agent_identities WHERE handle = ?" )
691+ . bind ( input . handle )
692+ . first < { id : string ; status : string ; requested_at : string } > ( ) ;
693+ if ( existing && existing . status !== "pending" ) {
694+ return json ( { error : "An agent with this handle already exists." } , 409 ) ;
695+ }
696+ const agentId = existing ?. id ?? id ;
697+ const agentRequestedAt = existing ?. requested_at ?? requestedAt ;
698+ if ( existing ) {
699+ await database
700+ . prepare (
701+ `UPDATE agent_identities
702+ SET display_name = ?,
703+ machine_scope = ?,
704+ onboarding_auth_hash = ?,
705+ onboarding_auth_status = ?,
706+ onboarding_auth_length = ?,
707+ onboarding_auth_checked_at = ?
708+ WHERE id = ? AND status = 'pending'` ,
709+ )
710+ . bind (
711+ input . displayName ,
712+ input . machineScope ,
713+ authEvidence . hash ,
714+ authEvidence . status ,
715+ authEvidence . length ,
716+ authEvidence . checkedAt ,
717+ agentId ,
718+ )
719+ . run ( ) ;
720+ } else {
721+ await database
722+ . prepare (
723+ `INSERT INTO agent_identities
724+ (id, handle, display_name, machine_scope, status, requested_at,
725+ onboarding_auth_hash, onboarding_auth_status, onboarding_auth_length, onboarding_auth_checked_at)
726+ VALUES (?, ?, ?, ?, 'pending', ?, ?, ?, ?, ?)` ,
727+ )
728+ . bind (
729+ agentId ,
730+ input . handle ,
731+ input . displayName ,
732+ input . machineScope ,
733+ agentRequestedAt ,
734+ authEvidence . hash ,
735+ authEvidence . status ,
736+ authEvidence . length ,
737+ authEvidence . checkedAt ,
738+ )
739+ . run ( ) ;
740+ }
741+ const profile = profileValues ( input , agentId ) ;
709742 await database
710743 . prepare (
711744 `INSERT INTO agent_profiles
712745 (agent_id, project, role, summary, tools_json, interested_projects_json, capabilities_json, operating_notes, updated_at)
713- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)` ,
746+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
747+ ON CONFLICT(agent_id) DO UPDATE SET
748+ project = excluded.project,
749+ role = excluded.role,
750+ summary = excluded.summary,
751+ tools_json = excluded.tools_json,
752+ interested_projects_json = excluded.interested_projects_json,
753+ capabilities_json = excluded.capabilities_json,
754+ operating_notes = excluded.operating_notes,
755+ updated_at = excluded.updated_at` ,
714756 )
715757 . bind (
716758 profile . agentId ,
@@ -724,7 +766,7 @@ async function requestSignup(request: Request, env: Env) {
724766 requestedAt ,
725767 )
726768 . run ( ) ;
727- return json ( { id, status : "pending" , requestedAt, profile } , 202 ) ;
769+ return json ( { id : agentId , status : "pending" , requestedAt : agentRequestedAt , profile } , 202 ) ;
728770}
729771
730772async function createDirectMessage ( request : Request , env : Env , auth ?: AuthContext ) {
0 commit comments