@@ -11,7 +11,6 @@ import { createLogger } from '@sim/logger'
1111import { getErrorMessage , redactBoundParameters , toError } from '@sim/utils/errors'
1212import { filterUndefined , isPlainRecord , isRecordLike } from '@sim/utils/object'
1313import { mergeSubblockStateWithValues } from '@sim/workflow-persistence/subblocks'
14- import type { Edge } from '@xyflow/react'
1514import { eq } from 'drizzle-orm'
1615import { z } from 'zod'
1716import { type EffectivePiiRedaction , resolveEffectivePiiRedaction } from '@/lib/billing/retention'
@@ -79,6 +78,7 @@ import {
7978 buildParallelSentinelEndId ,
8079} from '@/executor/utils/subflow-node-id-codec'
8180import { Serializer } from '@/serializer'
81+ import type { SerializedWorkflow } from '@/serializer/types'
8282
8383const logger = createLogger ( 'ExecutionCore' )
8484
@@ -686,6 +686,16 @@ async function executeWorkflowCoreImpl(
686686 if ( resumeFromSnapshot && ! useDraftState && ! resumeDeploymentVersionId ) {
687687 throw new Error ( 'Deployed resume requires its admitted deployment version' )
688688 }
689+ /**
690+ * A draft resume runs the graph it paused on, never the live draft. Any `write`
691+ * member can edit the draft while the resumed run keeps its starter's identity
692+ * (personal variables, credential access), so reloading it would run another
693+ * member's later edits as the starter.
694+ */
695+ const resumesPausedDraft = resumeFromSnapshot && useDraftState
696+ if ( resumesPausedDraft && ! Array . isArray ( snapshot . workflow ?. blocks ) ) {
697+ throw new Error ( 'Draft resume is missing the workflow graph it paused on' )
698+ }
689699 const pinnedDeploymentVersionId = metadata . pinnedDeploymentVersionId
690700 if ( pinnedDeploymentVersionId && ( useDraftState || metadata . workflowStateOverride ) ) {
691701 throw new Error ( 'A pinned deployment version cannot run draft or overridden workflow state' )
@@ -797,16 +807,18 @@ async function executeWorkflowCoreImpl(
797807 }
798808
799809 const [ workflowState , env , piiPolicyRow ] = await Promise . all ( [
800- withDatabaseReadRetry ( loadWorkflowState , { label : 'loadWorkflowState' } ) ,
810+ resumesPausedDraft
811+ ? undefined
812+ : withDatabaseReadRetry ( loadWorkflowState , { label : 'loadWorkflowState' } ) ,
801813 reads . environment ,
802814 reads . piiPolicyRow ,
803815 ] )
804816
805- const { blocks , loops , parallels } = workflowState
806- const edges : Edge [ ] = workflowState . edges
807- deploymentVersionId = workflowState . deploymentVersionId
808-
809- const mergedStates = mergeSubblockStateWithValues ( blocks )
817+ deploymentVersionId = workflowState ?. deploymentVersionId
818+ const loadedWorkflow = workflowState && {
819+ ... workflowState ,
820+ mergedStates : mergeSubblockStateWithValues ( workflowState . blocks ) ,
821+ }
810822
811823 const {
812824 personalEncrypted,
@@ -872,7 +884,12 @@ async function executeWorkflowCoreImpl(
872884 triggerData : metadata . correlation ? { correlation : metadata . correlation } : undefined ,
873885 skipLogCreation,
874886 deploymentVersionId,
875- workflowState : { blocks, edges, loops, parallels } ,
887+ workflowState : loadedWorkflow && {
888+ blocks : loadedWorkflow . blocks ,
889+ edges : loadedWorkflow . edges ,
890+ loops : loadedWorkflow . loops ,
891+ parallels : loadedWorkflow . parallels ,
892+ } ,
876893 } )
877894 /**
878895 * A start that failed because the run's workspace or workflow is gone ends the run here,
@@ -881,9 +898,6 @@ async function executeWorkflowCoreImpl(
881898 const deletedScope = loggingSession . getDeletedScope ( )
882899 if ( deletedScope ) throw new ExecutionScopeDeletedError ( executionId , deletedScope )
883900
884- // Use edges directly - trigger-to-trigger edges are prevented at creation time
885- const filteredEdges = edges
886-
887901 // Check if this is a resume execution before trigger resolution
888902 const resumePendingQueue = snapshot . state ?. pendingQueue
889903 const resumeRemainingEdges = snapshot . state ?. remainingEdges
@@ -905,14 +919,21 @@ async function executeWorkflowCoreImpl(
905919 resumeTerminalNoop,
906920 } )
907921 } else if ( ! triggerBlockId ) {
922+ if ( ! loadedWorkflow ) {
923+ throw new Error ( 'Draft resume is missing its pending queue' )
924+ }
908925 const executionKind =
909926 triggerType === 'api' || triggerType === 'chat'
910927 ? ( triggerType as 'api' | 'chat' )
911928 : triggerType === 'webhook' || triggerType === 'schedule'
912929 ? 'external'
913930 : 'manual'
914931
915- const startBlock = TriggerUtils . findStartBlock ( mergedStates , executionKind , false )
932+ const startBlock = TriggerUtils . findStartBlock (
933+ loadedWorkflow . mergedStates ,
934+ executionKind ,
935+ false
936+ )
916937
917938 if ( ! startBlock ) {
918939 throw new UserFailure ( 'No start block found. Add a start block to this workflow.' )
@@ -927,14 +948,15 @@ async function executeWorkflowCoreImpl(
927948 } )
928949 }
929950
930- // Serialize workflow
931- const serializedWorkflow = new Serializer ( ) . serializeWorkflow (
932- mergedStates ,
933- filteredEdges ,
934- loops ,
935- parallels ,
936- true
937- )
951+ const serializedWorkflow : SerializedWorkflow = loadedWorkflow
952+ ? new Serializer ( ) . serializeWorkflow (
953+ loadedWorkflow . mergedStates ,
954+ loadedWorkflow . edges ,
955+ loadedWorkflow . loops ,
956+ loadedWorkflow . parallels ,
957+ true
958+ )
959+ : snapshot . workflow
938960 const inputFileKeys = new Set < string > ( )
939961 processedInput =
940962 resumeFromSnapshot || runFromBlock
0 commit comments