@@ -798,6 +798,9 @@ export async function reviewPullRequest(pullRequestId: string, input: {
798798 if ( ! pullRequest ) {
799799 throw new Error ( `Pull request ${ pullRequestId } not found.` ) ;
800800 }
801+ if ( pullRequest . authorId === input . agentId ) {
802+ throw new Error ( "Self-review is not allowed. A different agent must review this PR." ) ;
803+ }
801804 const repository = state . repositories . find ( ( item ) => item . id === pullRequest . repositoryId ) ;
802805 if ( ! repository ) {
803806 throw new Error ( `Repository ${ pullRequest . repositoryId } not found.` ) ;
@@ -821,14 +824,18 @@ export async function reviewPullRequest(pullRequestId: string, input: {
821824 const approvals = reviews . filter ( ( review ) => review . decision === ReviewDecision . APPROVE ) . length ;
822825 const rejections = reviews . filter ( ( review ) => review . decision === ReviewDecision . REJECT ) . length ;
823826 if ( pullRequest . status === "OPEN" && approvals >= forgeConfig . minApprovals && approvals > rejections ) {
824- const merge = await mergePullRequestOnDisk ( { repoPath : repository . repoPath , sourceBranch : pullRequest . sourceBranch , targetBranch : pullRequest . targetBranch } ) ;
825- pullRequest . status = "MERGED" ;
826- pullRequest . mergeCommitHash = merge . hash ;
827- state . commits . unshift ( { id : createId ( ) , repositoryId : repository . id , authorId : input . agentId , branch : pullRequest . targetBranch , hash : merge . hash , message : `Merge PR ${ pullRequest . title } ` , createdAt : new Date ( ) . toISOString ( ) } ) ;
828- repository . updatedAt = new Date ( ) . toISOString ( ) ;
829- await writeStore ( state ) ;
830- await createAuditEvent ( { repositoryId : repository . id , actorId : input . agentId , pullRequestId, eventType : "pr.merged" , summary : `Autonomously merged '${ pullRequest . title } '` , metadata : { mergeCommitHash : merge . hash } } ) ;
831- await incrementAgentScore ( pullRequest . authorId , 15 ) ;
827+ try {
828+ const merge = await mergePullRequestOnDisk ( { repoPath : repository . repoPath , sourceBranch : pullRequest . sourceBranch , targetBranch : pullRequest . targetBranch } ) ;
829+ pullRequest . status = "MERGED" ;
830+ pullRequest . mergeCommitHash = merge . hash ;
831+ state . commits . unshift ( { id : createId ( ) , repositoryId : repository . id , authorId : input . agentId , branch : pullRequest . targetBranch , hash : merge . hash , message : `Merge PR ${ pullRequest . title } ` , createdAt : new Date ( ) . toISOString ( ) } ) ;
832+ repository . updatedAt = new Date ( ) . toISOString ( ) ;
833+ await writeStore ( state ) ;
834+ await createAuditEvent ( { repositoryId : repository . id , actorId : input . agentId , pullRequestId, eventType : "pr.merged" , summary : `Autonomously merged '${ pullRequest . title } '` , metadata : { mergeCommitHash : merge . hash } } ) ;
835+ await incrementAgentScore ( pullRequest . authorId , 15 ) ;
836+ } catch {
837+ await createAuditEvent ( { repositoryId : repository . id , actorId : input . agentId , pullRequestId, eventType : "pr.merge_conflict" , summary : `Merge conflict in '${ pullRequest . title } ' — manual resolution needed` , metadata : { } } ) ;
838+ }
832839 }
833840 return { reviewer, pullRequest, decision : input . decision , comment : input . comment , id : existingReview ?. id ?? state . reviews [ 0 ] ?. id ?? createId ( ) } ;
834841 }
@@ -856,6 +863,10 @@ export async function reviewPullRequest(pullRequestId: string, input: {
856863 } ,
857864 } ) ;
858865
866+ if ( review . pullRequest . authorId === input . agentId ) {
867+ throw new Error ( "Self-review is not allowed. A different agent must review this PR." ) ;
868+ }
869+
859870 await createAuditEvent ( {
860871 repositoryId : review . pullRequest . repositoryId ,
861872 actorId : input . agentId ,
@@ -880,37 +891,48 @@ export async function reviewPullRequest(pullRequestId: string, input: {
880891 approvals >= forgeConfig . minApprovals &&
881892 approvals > rejections
882893 ) {
883- const merge = await mergePullRequestOnDisk ( {
884- repoPath : updatedPullRequest . repository . repoPath ,
885- sourceBranch : updatedPullRequest . sourceBranch ,
886- targetBranch : updatedPullRequest . targetBranch ,
887- } ) ;
888-
889- await db . pullRequest . update ( {
890- where : { id : pullRequestId } ,
891- data : { status : PullRequestStatus . MERGED , mergeCommitHash : merge . hash } ,
892- } ) ;
894+ try {
895+ const merge = await mergePullRequestOnDisk ( {
896+ repoPath : updatedPullRequest . repository . repoPath ,
897+ sourceBranch : updatedPullRequest . sourceBranch ,
898+ targetBranch : updatedPullRequest . targetBranch ,
899+ } ) ;
900+
901+ await db . pullRequest . update ( {
902+ where : { id : pullRequestId } ,
903+ data : { status : PullRequestStatus . MERGED , mergeCommitHash : merge . hash } ,
904+ } ) ;
905+
906+ await db . gitCommit . create ( {
907+ data : {
908+ repositoryId : updatedPullRequest . repositoryId ,
909+ authorId : input . agentId ,
910+ branch : updatedPullRequest . targetBranch ,
911+ hash : merge . hash ,
912+ message : `Merge PR ${ updatedPullRequest . title } ` ,
913+ } ,
914+ } ) ;
893915
894- await db . gitCommit . create ( {
895- data : {
916+ await createAuditEvent ( {
896917 repositoryId : updatedPullRequest . repositoryId ,
897- authorId : input . agentId ,
898- branch : updatedPullRequest . targetBranch ,
899- hash : merge . hash ,
900- message : `Merge PR ${ updatedPullRequest . title } ` ,
901- } ,
902- } ) ;
903-
904- await createAuditEvent ( {
905- repositoryId : updatedPullRequest . repositoryId ,
906- actorId : input . agentId ,
907- pullRequestId,
908- eventType : "pr.merged" ,
909- summary : `Autonomously merged '${ updatedPullRequest . title } '` ,
910- metadata : { mergeCommitHash : merge . hash } ,
911- } ) ;
918+ actorId : input . agentId ,
919+ pullRequestId,
920+ eventType : "pr.merged" ,
921+ summary : `Autonomously merged '${ updatedPullRequest . title } '` ,
922+ metadata : { mergeCommitHash : merge . hash } ,
923+ } ) ;
912924
913- await incrementAgentScore ( updatedPullRequest . authorId , 15 ) ;
925+ await incrementAgentScore ( updatedPullRequest . authorId , 15 ) ;
926+ } catch {
927+ await createAuditEvent ( {
928+ repositoryId : updatedPullRequest . repositoryId ,
929+ actorId : input . agentId ,
930+ pullRequestId,
931+ eventType : "pr.merge_conflict" ,
932+ summary : `Merge conflict in '${ updatedPullRequest . title } ' — manual resolution needed` ,
933+ metadata : { } ,
934+ } ) ;
935+ }
914936 }
915937
916938 return review ;
0 commit comments