@@ -61,11 +61,13 @@ export class ParallelOrchestrator {
6161
6262 let items : any [ ] | undefined
6363 let branchCount : number
64+ let isEmpty = false
6465
6566 try {
66- const resolved = this . resolveBranchCount ( ctx , parallelConfig )
67+ const resolved = this . resolveBranchCount ( ctx , parallelConfig , parallelId )
6768 branchCount = resolved . branchCount
6869 items = resolved . items
70+ isEmpty = resolved . isEmpty ?? false
6971 } catch ( error ) {
7072 const errorMessage = `Parallel Items did not resolve: ${ error instanceof Error ? error . message : String ( error ) } `
7173 logger . error ( errorMessage , { parallelId, distribution : parallelConfig . distribution } )
@@ -91,6 +93,34 @@ export class ParallelOrchestrator {
9193 throw new Error ( branchError )
9294 }
9395
96+ // Handle empty distribution - skip parallel body
97+ if ( isEmpty || branchCount === 0 ) {
98+ const scope : ParallelScope = {
99+ parallelId,
100+ totalBranches : 0 ,
101+ branchOutputs : new Map ( ) ,
102+ completedCount : 0 ,
103+ totalExpectedNodes : 0 ,
104+ items : [ ] ,
105+ isEmpty : true ,
106+ }
107+
108+ if ( ! ctx . parallelExecutions ) {
109+ ctx . parallelExecutions = new Map ( )
110+ }
111+ ctx . parallelExecutions . set ( parallelId , scope )
112+
113+ // Set empty output for the parallel
114+ this . state . setBlockOutput ( parallelId , { results : [ ] } )
115+
116+ logger . info ( 'Parallel scope initialized with empty distribution, skipping body' , {
117+ parallelId,
118+ branchCount : 0 ,
119+ } )
120+
121+ return scope
122+ }
123+
94124 const { entryNodes } = this . expander . expandParallel ( this . dag , parallelId , branchCount , items )
95125
96126 const scope : ParallelScope = {
@@ -127,15 +157,17 @@ export class ParallelOrchestrator {
127157
128158 private resolveBranchCount (
129159 ctx : ExecutionContext ,
130- config : SerializedParallel
131- ) : { branchCount : number ; items ?: any [ ] } {
160+ config : SerializedParallel ,
161+ parallelId : string
162+ ) : { branchCount : number ; items ?: any [ ] ; isEmpty ?: boolean } {
132163 if ( config . parallelType === 'count' ) {
133164 return { branchCount : config . count ?? 1 }
134165 }
135166
136167 const items = this . resolveDistributionItems ( ctx , config )
137168 if ( items . length === 0 ) {
138- return { branchCount : config . count ?? 1 }
169+ logger . info ( 'Parallel has empty distribution, skipping parallel body' , { parallelId } )
170+ return { branchCount : 0 , items : [ ] , isEmpty : true }
139171 }
140172
141173 return { branchCount : items . length , items }
0 commit comments