@@ -34,7 +34,7 @@ import { coerceValue } from '@/executor/utils/start-block'
3434import { subscriptionKeys } from '@/hooks/queries/subscription'
3535import { useExecutionStream } from '@/hooks/use-execution-stream'
3636import { WorkflowValidationError } from '@/serializer'
37- import { useExecutionStore } from '@/stores/execution'
37+ import { useCurrentWorkflowExecution , useExecutionStore } from '@/stores/execution'
3838import { useNotificationStore } from '@/stores/notifications'
3939import { useVariablesStore } from '@/stores/panel'
4040import { useEnvironmentStore } from '@/stores/settings/environment'
@@ -112,12 +112,9 @@ export function useWorkflowExecution() {
112112 useTerminalConsoleStore ( )
113113 const { getAllVariables } = useEnvironmentStore ( )
114114 const { getVariablesByWorkflowId, variables } = useVariablesStore ( )
115+ const { isExecuting, isDebugging, pendingBlocks, executor, debugContext } =
116+ useCurrentWorkflowExecution ( )
115117 const {
116- isExecuting,
117- isDebugging,
118- pendingBlocks,
119- executor,
120- debugContext,
121118 setIsExecuting,
122119 setIsDebugging,
123120 setPendingBlocks,
@@ -158,13 +155,15 @@ export function useWorkflowExecution() {
158155 * Resets all debug-related state
159156 */
160157 const resetDebugState = useCallback ( ( ) => {
161- setIsExecuting ( false )
162- setIsDebugging ( false )
163- setDebugContext ( null )
164- setExecutor ( null )
165- setPendingBlocks ( [ ] )
166- setActiveBlocks ( new Set ( ) )
158+ if ( ! activeWorkflowId ) return
159+ setIsExecuting ( activeWorkflowId , false )
160+ setIsDebugging ( activeWorkflowId , false )
161+ setDebugContext ( activeWorkflowId , null )
162+ setExecutor ( activeWorkflowId , null )
163+ setPendingBlocks ( activeWorkflowId , [ ] )
164+ setActiveBlocks ( activeWorkflowId , new Set ( ) )
167165 } , [
166+ activeWorkflowId ,
168167 setIsExecuting ,
169168 setIsDebugging ,
170169 setDebugContext ,
@@ -312,18 +311,20 @@ export function useWorkflowExecution() {
312311 } = config
313312
314313 const updateActiveBlocks = ( blockId : string , isActive : boolean ) => {
314+ if ( ! workflowId ) return
315315 if ( isActive ) {
316316 activeBlocksSet . add ( blockId )
317317 } else {
318318 activeBlocksSet . delete ( blockId )
319319 }
320- setActiveBlocks ( new Set ( activeBlocksSet ) )
320+ setActiveBlocks ( workflowId , new Set ( activeBlocksSet ) )
321321 }
322322
323323 const markIncomingEdges = ( blockId : string ) => {
324+ if ( ! workflowId ) return
324325 const incomingEdges = workflowEdges . filter ( ( edge ) => edge . target === blockId )
325326 incomingEdges . forEach ( ( edge ) => {
326- setEdgeRunStatus ( edge . id , 'success' )
327+ setEdgeRunStatus ( workflowId , edge . id , 'success' )
327328 } )
328329 }
329330
@@ -459,7 +460,7 @@ export function useWorkflowExecution() {
459460
460461 const onBlockCompleted = ( data : BlockCompletedData ) => {
461462 updateActiveBlocks ( data . blockId , false )
462- setBlockRunStatus ( data . blockId , 'success' )
463+ if ( workflowId ) setBlockRunStatus ( workflowId , data . blockId , 'success' )
463464
464465 executedBlockIds . add ( data . blockId )
465466 accumulatedBlockStates . set ( data . blockId , {
@@ -489,7 +490,7 @@ export function useWorkflowExecution() {
489490
490491 const onBlockError = ( data : BlockErrorData ) => {
491492 updateActiveBlocks ( data . blockId , false )
492- setBlockRunStatus ( data . blockId , 'error' )
493+ if ( workflowId ) setBlockRunStatus ( workflowId , data . blockId , 'error' )
493494
494495 executedBlockIds . add ( data . blockId )
495496 accumulatedBlockStates . set ( data . blockId , {
@@ -547,19 +548,20 @@ export function useWorkflowExecution() {
547548 */
548549 const handleDebugSessionContinuation = useCallback (
549550 ( result : ExecutionResult ) => {
551+ if ( ! activeWorkflowId ) return
550552 logger . info ( 'Debug step completed, next blocks pending' , {
551553 nextPendingBlocks : result . metadata ?. pendingBlocks ?. length || 0 ,
552554 } )
553555
554556 // Update debug context and pending blocks
555557 if ( result . metadata ?. context ) {
556- setDebugContext ( result . metadata . context )
558+ setDebugContext ( activeWorkflowId , result . metadata . context )
557559 }
558560 if ( result . metadata ?. pendingBlocks ) {
559- setPendingBlocks ( result . metadata . pendingBlocks )
561+ setPendingBlocks ( activeWorkflowId , result . metadata . pendingBlocks )
560562 }
561563 } ,
562- [ setDebugContext , setPendingBlocks ]
564+ [ activeWorkflowId , setDebugContext , setPendingBlocks ]
563565 )
564566
565567 /**
@@ -663,11 +665,11 @@ export function useWorkflowExecution() {
663665
664666 // Reset execution result and set execution state
665667 setExecutionResult ( null )
666- setIsExecuting ( true )
668+ setIsExecuting ( activeWorkflowId , true )
667669
668670 // Set debug mode only if explicitly requested
669671 if ( enableDebug ) {
670- setIsDebugging ( true )
672+ setIsDebugging ( activeWorkflowId , true )
671673 }
672674
673675 // Determine if this is a chat execution
@@ -965,9 +967,9 @@ export function useWorkflowExecution() {
965967 controller . close ( )
966968 }
967969 if ( currentChatExecutionIdRef . current === executionId ) {
968- setIsExecuting ( false )
969- setIsDebugging ( false )
970- setActiveBlocks ( new Set ( ) )
970+ setIsExecuting ( activeWorkflowId , false )
971+ setIsDebugging ( activeWorkflowId , false )
972+ setActiveBlocks ( activeWorkflowId , new Set ( ) )
971973 }
972974 }
973975 } ,
@@ -989,16 +991,16 @@ export function useWorkflowExecution() {
989991 'manual'
990992 )
991993 if ( result && 'metadata' in result && result . metadata ?. isDebugSession ) {
992- setDebugContext ( result . metadata . context || null )
994+ setDebugContext ( activeWorkflowId , result . metadata . context || null )
993995 if ( result . metadata . pendingBlocks ) {
994- setPendingBlocks ( result . metadata . pendingBlocks )
996+ setPendingBlocks ( activeWorkflowId , result . metadata . pendingBlocks )
995997 }
996998 } else if ( result && 'success' in result ) {
997999 setExecutionResult ( result )
9981000 // Reset execution state after successful non-debug execution
999- setIsExecuting ( false )
1000- setIsDebugging ( false )
1001- setActiveBlocks ( new Set ( ) )
1001+ setIsExecuting ( activeWorkflowId , false )
1002+ setIsDebugging ( activeWorkflowId , false )
1003+ setActiveBlocks ( activeWorkflowId , new Set ( ) )
10021004
10031005 if ( isChatExecution ) {
10041006 if ( ! result . metadata ) {
@@ -1179,7 +1181,7 @@ export function useWorkflowExecution() {
11791181 logger . error ( 'No trigger blocks found for manual run' , {
11801182 allBlockTypes : Object . values ( filteredStates ) . map ( ( b ) => b . type ) ,
11811183 } )
1182- setIsExecuting ( false )
1184+ if ( activeWorkflowId ) setIsExecuting ( activeWorkflowId , false )
11831185 throw error
11841186 }
11851187
@@ -1195,7 +1197,7 @@ export function useWorkflowExecution() {
11951197 'Workflow Validation'
11961198 )
11971199 logger . error ( 'Multiple API triggers found' )
1198- setIsExecuting ( false )
1200+ if ( activeWorkflowId ) setIsExecuting ( activeWorkflowId , false )
11991201 throw error
12001202 }
12011203
@@ -1220,7 +1222,7 @@ export function useWorkflowExecution() {
12201222 'Workflow Validation'
12211223 )
12221224 logger . error ( 'Trigger has no outgoing connections' , { triggerName, startBlockId } )
1223- setIsExecuting ( false )
1225+ if ( activeWorkflowId ) setIsExecuting ( activeWorkflowId , false )
12241226 throw error
12251227 }
12261228 }
@@ -1251,7 +1253,7 @@ export function useWorkflowExecution() {
12511253 'Workflow Validation'
12521254 )
12531255 logger . error ( 'No startBlockId found after trigger search' )
1254- setIsExecuting ( false )
1256+ if ( activeWorkflowId ) setIsExecuting ( activeWorkflowId , false )
12551257 throw error
12561258 }
12571259
@@ -1457,8 +1459,10 @@ export function useWorkflowExecution() {
14571459 logger . info ( 'Execution aborted by user' )
14581460
14591461 // Reset execution state
1460- setIsExecuting ( false )
1461- setActiveBlocks ( new Set ( ) )
1462+ if ( activeWorkflowId ) {
1463+ setIsExecuting ( activeWorkflowId , false )
1464+ setActiveBlocks ( activeWorkflowId , new Set ( ) )
1465+ }
14621466
14631467 // Return gracefully without error
14641468 return {
@@ -1533,9 +1537,11 @@ export function useWorkflowExecution() {
15331537 }
15341538
15351539 setExecutionResult ( errorResult )
1536- setIsExecuting ( false )
1537- setIsDebugging ( false )
1538- setActiveBlocks ( new Set ( ) )
1540+ if ( activeWorkflowId ) {
1541+ setIsExecuting ( activeWorkflowId , false )
1542+ setIsDebugging ( activeWorkflowId , false )
1543+ setActiveBlocks ( activeWorkflowId , new Set ( ) )
1544+ }
15391545
15401546 let notificationMessage = WORKFLOW_EXECUTION_FAILURE_MESSAGE
15411547 if ( isRecord ( error ) && isRecord ( error . request ) && sanitizeMessage ( error . request . url ) ) {
@@ -1706,21 +1712,21 @@ export function useWorkflowExecution() {
17061712 const handleCancelExecution = useCallback ( ( ) => {
17071713 logger . info ( 'Workflow execution cancellation requested' )
17081714
1709- // Cancel the execution stream (server-side)
1710- executionStream . cancel ( )
1715+ // Cancel the execution stream for this workflow (server-side)
1716+ executionStream . cancel ( activeWorkflowId ?? undefined )
17111717
17121718 // Mark current chat execution as superseded so its cleanup won't affect new executions
17131719 currentChatExecutionIdRef . current = null
17141720
17151721 // Mark all running entries as canceled in the terminal
17161722 if ( activeWorkflowId ) {
17171723 cancelRunningEntries ( activeWorkflowId )
1718- }
17191724
1720- // Reset execution state - this triggers chat stream cleanup via useEffect in chat.tsx
1721- setIsExecuting ( false )
1722- setIsDebugging ( false )
1723- setActiveBlocks ( new Set ( ) )
1725+ // Reset execution state - this triggers chat stream cleanup via useEffect in chat.tsx
1726+ setIsExecuting ( activeWorkflowId , false )
1727+ setIsDebugging ( activeWorkflowId , false )
1728+ setActiveBlocks ( activeWorkflowId , new Set ( ) )
1729+ }
17241730
17251731 // If in debug mode, also reset debug state
17261732 if ( isDebugging ) {
@@ -1833,7 +1839,7 @@ export function useWorkflowExecution() {
18331839 }
18341840 }
18351841
1836- setIsExecuting ( true )
1842+ setIsExecuting ( workflowId , true )
18371843 const executionId = uuidv4 ( )
18381844 const accumulatedBlockLogs : BlockLog [ ] = [ ]
18391845 const accumulatedBlockStates = new Map < string , BlockState > ( )
@@ -1929,8 +1935,8 @@ export function useWorkflowExecution() {
19291935 logger . error ( 'Run-from-block failed:' , error )
19301936 }
19311937 } finally {
1932- setIsExecuting ( false )
1933- setActiveBlocks ( new Set ( ) )
1938+ setIsExecuting ( workflowId , false )
1939+ setActiveBlocks ( workflowId , new Set ( ) )
19341940 }
19351941 } ,
19361942 [
@@ -1962,7 +1968,7 @@ export function useWorkflowExecution() {
19621968 logger . info ( 'Starting run-until-block execution' , { workflowId, stopAfterBlockId : blockId } )
19631969
19641970 setExecutionResult ( null )
1965- setIsExecuting ( true )
1971+ setIsExecuting ( activeWorkflowId ! , true )
19661972
19671973 const executionId = uuidv4 ( )
19681974 try {
@@ -1981,9 +1987,9 @@ export function useWorkflowExecution() {
19811987 const errorResult = handleExecutionError ( error , { executionId } )
19821988 return errorResult
19831989 } finally {
1984- setIsExecuting ( false )
1985- setIsDebugging ( false )
1986- setActiveBlocks ( new Set ( ) )
1990+ setIsExecuting ( activeWorkflowId ! , false )
1991+ setIsDebugging ( activeWorkflowId ! , false )
1992+ setActiveBlocks ( activeWorkflowId ! , new Set ( ) )
19871993 }
19881994 } ,
19891995 [ activeWorkflowId , setExecutionResult , setIsExecuting , setIsDebugging , setActiveBlocks ]
0 commit comments