@@ -891,11 +891,13 @@ export class BatchTriggerV3Service extends BaseService {
891891 }
892892
893893 // FIX for Issue #2965: When a run is cached (duplicate idempotencyKey),
894- // we need to handle it based on whether the cached run is already complete.
894+ // we need to ALWAYS create a BatchTaskRunItem to properly track it.
895+ // This handles cases where cached run may originate from another batch.
896+ // Use unique constraint (batchTaskRunId, taskRunId) to prevent duplicates.
895897 const isAlreadyComplete = isFinalRunStatus ( result . run . status ) ;
896898
897899 logger . debug (
898- "[BatchTriggerV2][processBatchTaskRunItem] Cached run detected" ,
900+ "[BatchTriggerV2][processBatchTaskRunItem] Cached run detected, creating batch item " ,
899901 {
900902 batchId : batch . friendlyId ,
901903 runId : task . runId ,
@@ -906,45 +908,32 @@ export class BatchTriggerV3Service extends BaseService {
906908 }
907909 ) ;
908910
909- // If the cached run is NOT in a final status (still PENDING or EXECUTING),
910- // we should NOT create a BatchTaskRunItem because:
911- // 1. The original item will complete when the run finishes
912- // 2. Creating a duplicate item would cause the batch to hang forever
913- // (as noted in Devin AI review)
914- if ( ! isAlreadyComplete ) {
915- logger . debug (
916- "[BatchTriggerV2][processBatchTaskRunItem] Cached run still in progress, skipping batch item creation" ,
917- {
918- batchId : batch . friendlyId ,
919- cachedRunId : result . run . id ,
920- cachedRunStatus : result . run . status ,
921- }
922- ) ;
923- // Return false to NOT increment expectedCount
924- return false ;
925- }
926-
927- // The cached run is already complete, create a BatchTaskRunItem and increment completedCount
911+ // Always create BatchTaskRunItem for cached runs
912+ // This ensures proper tracking even for cross-batch scenarios
928913 try {
929914 await this . _prisma . batchTaskRunItem . create ( {
930915 data : {
931916 batchTaskRunId : batch . id ,
932917 taskRunId : result . run . id ,
933- status : "COMPLETED" ,
918+ // Use appropriate status based on the cached run's current status
919+ status : isAlreadyComplete ? "COMPLETED" : batchTaskRunItemStatusForRunStatus ( result . run . status ) ,
934920 } ,
935921 } ) ;
936922
937- // Increment completedCount since the cached run is already finished
938- await this . _prisma . batchTaskRun . update ( {
939- where : { id : batch . id } ,
940- data : {
941- completedCount : {
942- increment : 1 ,
923+ // Only increment completedCount if the cached run is already finished
924+ // For in-progress runs, completedCount will be incremented when the run completes
925+ if ( isAlreadyComplete ) {
926+ await this . _prisma . batchTaskRun . update ( {
927+ where : { id : batch . id } ,
928+ data : {
929+ completedCount : {
930+ increment : 1 ,
931+ } ,
943932 } ,
944- } ,
945- } ) ;
933+ } ) ;
934+ }
946935
947- // Return true so expectedCount is incremented (matches completedCount increment above)
936+ // Return true so expectedCount is incremented
948937 return true ;
949938 } catch ( error ) {
950939 if ( isUniqueConstraintError ( error , [ "batchTaskRunId" , "taskRunId" ] ) ) {
0 commit comments