@@ -5,6 +5,30 @@ import { useTerminalConsoleStore } from '@/stores/terminal'
55import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
66import { useWorkflowStore } from '@/stores/workflows/workflow/store'
77
8+ /**
9+ * Updates the active blocks set and ref counts for a single block.
10+ * Ref counting ensures a block stays active until all parallel branches for it complete.
11+ */
12+ export function updateActiveBlockRefCount (
13+ refCounts : Map < string , number > ,
14+ activeSet : Set < string > ,
15+ blockId : string ,
16+ isActive : boolean
17+ ) : void {
18+ if ( isActive ) {
19+ refCounts . set ( blockId , ( refCounts . get ( blockId ) ?? 0 ) + 1 )
20+ activeSet . add ( blockId )
21+ } else {
22+ const next = ( refCounts . get ( blockId ) ?? 1 ) - 1
23+ if ( next <= 0 ) {
24+ refCounts . delete ( blockId )
25+ activeSet . delete ( blockId )
26+ } else {
27+ refCounts . set ( blockId , next )
28+ }
29+ }
30+ }
31+
832export interface WorkflowExecutionOptions {
933 workflowInput ?: any
1034 onStream ?: ( se : StreamingExecution ) => Promise < void >
@@ -104,9 +128,12 @@ export async function executeWorkflowWithFullLogging(
104128
105129 switch ( event . type ) {
106130 case 'block:started' : {
107- const startCount = activeBlockRefCounts . get ( event . data . blockId ) ?? 0
108- activeBlockRefCounts . set ( event . data . blockId , startCount + 1 )
109- activeBlocksSet . add ( event . data . blockId )
131+ updateActiveBlockRefCount (
132+ activeBlockRefCounts ,
133+ activeBlocksSet ,
134+ event . data . blockId ,
135+ true
136+ )
110137 setActiveBlocks ( wfId , new Set ( activeBlocksSet ) )
111138
112139 const incomingEdges = workflowEdges . filter (
@@ -119,13 +146,12 @@ export async function executeWorkflowWithFullLogging(
119146 }
120147
121148 case 'block:completed' : {
122- const completeCount = activeBlockRefCounts . get ( event . data . blockId ) ?? 1
123- if ( completeCount <= 1 ) {
124- activeBlockRefCounts . delete ( event . data . blockId )
125- activeBlocksSet . delete ( event . data . blockId )
126- } else {
127- activeBlockRefCounts . set ( event . data . blockId , completeCount - 1 )
128- }
149+ updateActiveBlockRefCount (
150+ activeBlockRefCounts ,
151+ activeBlocksSet ,
152+ event . data . blockId ,
153+ false
154+ )
129155 setActiveBlocks ( wfId , new Set ( activeBlocksSet ) )
130156
131157 setBlockRunStatus ( wfId , event . data . blockId , 'success' )
@@ -156,13 +182,12 @@ export async function executeWorkflowWithFullLogging(
156182 }
157183
158184 case 'block:error' : {
159- const errorCount = activeBlockRefCounts . get ( event . data . blockId ) ?? 1
160- if ( errorCount <= 1 ) {
161- activeBlockRefCounts . delete ( event . data . blockId )
162- activeBlocksSet . delete ( event . data . blockId )
163- } else {
164- activeBlockRefCounts . set ( event . data . blockId , errorCount - 1 )
165- }
185+ updateActiveBlockRefCount (
186+ activeBlockRefCounts ,
187+ activeBlocksSet ,
188+ event . data . blockId ,
189+ false
190+ )
166191 setActiveBlocks ( wfId , new Set ( activeBlocksSet ) )
167192
168193 setBlockRunStatus ( wfId , event . data . blockId , 'error' )
0 commit comments