From 5c75a54bb7b948ba09f3d4fc43964873c6bb6d8b Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 14 Nov 2026 14:10:45 -0800 Subject: [PATCH 1/2] fix(executor): resume a paused draft run on the graph it paused on --- .../sim/lib/logs/execution/logging-session.ts | 11 ++-- .../workflows/executor/execution-core.test.ts | 62 +++++++++++++++++-- .../lib/workflows/executor/execution-core.ts | 62 +++++++++++++------ 3 files changed, 104 insertions(+), 31 deletions(-) diff --git a/apps/sim/lib/logs/execution/logging-session.ts b/apps/sim/lib/logs/execution/logging-session.ts index 4824fb92a26..90125642ca5 100644 --- a/apps/sim/lib/logs/execution/logging-session.ts +++ b/apps/sim/lib/logs/execution/logging-session.ts @@ -839,13 +839,12 @@ export class LoggingSession { workspaceId, variables ) - this.workflowState = - workflowState ?? - (deploymentVersionId - ? await loadDeployedWorkflowStateForLogging(this.workflowId) - : await loadWorkflowStateForExecution(this.workflowId)) - if (!skipLogCreation) { + this.workflowState = + workflowState ?? + (deploymentVersionId + ? await loadDeployedWorkflowStateForLogging(this.workflowId) + : await loadWorkflowStateForExecution(this.workflowId)) await executionLogger.startWorkflowExecution({ workflowId: this.workflowId, workspaceId, diff --git a/apps/sim/lib/workflows/executor/execution-core.test.ts b/apps/sim/lib/workflows/executor/execution-core.test.ts index f4d9ee65de1..bc3556b03b4 100644 --- a/apps/sim/lib/workflows/executor/execution-core.test.ts +++ b/apps/sim/lib/workflows/executor/execution-core.test.ts @@ -222,11 +222,7 @@ describe('executeWorkflowCore terminal finalization sequencing', () => { enforceCredentialAccess: false, startTime: new Date().toISOString(), }, - workflow: { - id: 'workflow-1', - userId: 'workflow-owner', - variables: {}, - }, + workflow: { version: '1.0', blocks: [], connections: [], loops: {}, parallels: {} }, input: { hello: 'world' }, workflowVariables: {}, selectedOutputs: [], @@ -914,6 +910,61 @@ describe('executeWorkflowCore terminal finalization sequencing', () => { expect(loadWorkflowDeploymentVersionStateMock).not.toHaveBeenCalled() }) + it('runs a draft resume on the graph it paused on, never the live draft', async () => { + const pausedWorkflow = { + version: '1.0', + blocks: [{ id: 'paused-block' }], + connections: [], + loops: {}, + parallels: {}, + } + executorExecuteMock.mockResolvedValue({ + success: true, + status: 'completed', + output: {}, + logs: [], + metadata: { duration: 1, startTime: 'start', endTime: 'end' }, + }) + const resumedSnapshot = createSnapshot() + resumedSnapshot.metadata = { + ...resumedSnapshot.metadata, + resumeFromSnapshot: true, + resumeTerminalNoop: true, + } as any + ;(resumedSnapshot as any).workflow = pausedWorkflow + + await executeWorkflowCore({ + snapshot: resumedSnapshot as any, + callbacks: {}, + loggingSession: loggingSession as any, + skipLogCreation: true, + }) + + expect(loadWorkflowFromNormalizedTablesMock).not.toHaveBeenCalled() + expect(executorConstructorMock).toHaveBeenCalledWith( + expect.objectContaining({ workflow: pausedWorkflow }) + ) + }) + + it('fails a draft resume whose snapshot lost its paused graph', async () => { + const resumedSnapshot = createSnapshot() + resumedSnapshot.metadata = { + ...resumedSnapshot.metadata, + resumeFromSnapshot: true, + } as any + ;(resumedSnapshot as any).workflow = undefined + + await expect( + executeWorkflowCore({ + snapshot: resumedSnapshot as any, + callbacks: {}, + loggingSession: loggingSession as any, + skipLogCreation: true, + }) + ).rejects.toThrow('Draft resume is missing the workflow graph it paused on') + expect(loadWorkflowFromNormalizedTablesMock).not.toHaveBeenCalled() + }) + describe('PII redaction of restored large values', () => { const sourceItems = [{ email: 'alice@example.com', count: 7 }] const maskedItems = [{ email: '', count: 7 }] @@ -953,6 +1004,7 @@ describe('executeWorkflowCore terminal finalization sequencing', () => { decisions: { router: {}, condition: {} }, completedLoops: [], activeExecutionPath: [], + pendingQueue: [], trustedLargeValueAccess: { executionIds: [], largeValueKeys: [], fileKeys: [] }, } } diff --git a/apps/sim/lib/workflows/executor/execution-core.ts b/apps/sim/lib/workflows/executor/execution-core.ts index 6ffa938f2c0..b9331e41705 100644 --- a/apps/sim/lib/workflows/executor/execution-core.ts +++ b/apps/sim/lib/workflows/executor/execution-core.ts @@ -11,7 +11,6 @@ import { createLogger } from '@sim/logger' import { getErrorMessage, redactBoundParameters, toError } from '@sim/utils/errors' import { filterUndefined, isPlainRecord, isRecordLike } from '@sim/utils/object' import { mergeSubblockStateWithValues } from '@sim/workflow-persistence/subblocks' -import type { Edge } from '@xyflow/react' import { eq } from 'drizzle-orm' import { z } from 'zod' import { type EffectivePiiRedaction, resolveEffectivePiiRedaction } from '@/lib/billing/retention' @@ -79,6 +78,7 @@ import { buildParallelSentinelEndId, } from '@/executor/utils/subflow-node-id-codec' import { Serializer } from '@/serializer' +import type { SerializedWorkflow } from '@/serializer/types' const logger = createLogger('ExecutionCore') @@ -686,6 +686,16 @@ async function executeWorkflowCoreImpl( if (resumeFromSnapshot && !useDraftState && !resumeDeploymentVersionId) { throw new Error('Deployed resume requires its admitted deployment version') } + /** + * A draft resume runs the graph it paused on, never the live draft. Any `write` + * member can edit the draft while the resumed run keeps its starter's identity + * (personal variables, credential access), so reloading it would run another + * member's later edits as the starter. + */ + const resumesPausedDraft = resumeFromSnapshot && useDraftState + if (resumesPausedDraft && !Array.isArray(snapshot.workflow?.blocks)) { + throw new Error('Draft resume is missing the workflow graph it paused on') + } const pinnedDeploymentVersionId = metadata.pinnedDeploymentVersionId if (pinnedDeploymentVersionId && (useDraftState || metadata.workflowStateOverride)) { throw new Error('A pinned deployment version cannot run draft or overridden workflow state') @@ -797,16 +807,18 @@ async function executeWorkflowCoreImpl( } const [workflowState, env, piiPolicyRow] = await Promise.all([ - withDatabaseReadRetry(loadWorkflowState, { label: 'loadWorkflowState' }), + resumesPausedDraft + ? undefined + : withDatabaseReadRetry(loadWorkflowState, { label: 'loadWorkflowState' }), reads.environment, reads.piiPolicyRow, ]) - const { blocks, loops, parallels } = workflowState - const edges: Edge[] = workflowState.edges - deploymentVersionId = workflowState.deploymentVersionId - - const mergedStates = mergeSubblockStateWithValues(blocks) + deploymentVersionId = workflowState?.deploymentVersionId + const loadedWorkflow = workflowState && { + ...workflowState, + mergedStates: mergeSubblockStateWithValues(workflowState.blocks), + } const { personalEncrypted, @@ -872,7 +884,12 @@ async function executeWorkflowCoreImpl( triggerData: metadata.correlation ? { correlation: metadata.correlation } : undefined, skipLogCreation, deploymentVersionId, - workflowState: { blocks, edges, loops, parallels }, + workflowState: loadedWorkflow && { + blocks: loadedWorkflow.blocks, + edges: loadedWorkflow.edges, + loops: loadedWorkflow.loops, + parallels: loadedWorkflow.parallels, + }, }) /** * A start that failed because the run's workspace or workflow is gone ends the run here, @@ -881,9 +898,6 @@ async function executeWorkflowCoreImpl( const deletedScope = loggingSession.getDeletedScope() if (deletedScope) throw new ExecutionScopeDeletedError(executionId, deletedScope) - // Use edges directly - trigger-to-trigger edges are prevented at creation time - const filteredEdges = edges - // Check if this is a resume execution before trigger resolution const resumePendingQueue = snapshot.state?.pendingQueue const resumeRemainingEdges = snapshot.state?.remainingEdges @@ -905,6 +919,9 @@ async function executeWorkflowCoreImpl( resumeTerminalNoop, }) } else if (!triggerBlockId) { + if (!loadedWorkflow) { + throw new Error('Draft resume is missing its pending queue') + } const executionKind = triggerType === 'api' || triggerType === 'chat' ? (triggerType as 'api' | 'chat') @@ -912,7 +929,11 @@ async function executeWorkflowCoreImpl( ? 'external' : 'manual' - const startBlock = TriggerUtils.findStartBlock(mergedStates, executionKind, false) + const startBlock = TriggerUtils.findStartBlock( + loadedWorkflow.mergedStates, + executionKind, + false + ) if (!startBlock) { throw new UserFailure('No start block found. Add a start block to this workflow.') @@ -927,14 +948,15 @@ async function executeWorkflowCoreImpl( }) } - // Serialize workflow - const serializedWorkflow = new Serializer().serializeWorkflow( - mergedStates, - filteredEdges, - loops, - parallels, - true - ) + const serializedWorkflow: SerializedWorkflow = loadedWorkflow + ? new Serializer().serializeWorkflow( + loadedWorkflow.mergedStates, + loadedWorkflow.edges, + loadedWorkflow.loops, + loadedWorkflow.parallels, + true + ) + : snapshot.workflow const inputFileKeys = new Set() processedInput = resumeFromSnapshot || runFromBlock From 1dcfbdd6fbd9bd6a40c3b96d1a305df70a8bf097 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 14 Nov 2026 14:19:57 -0800 Subject: [PATCH 2/2] test(executor): type the draft resume snapshots instead of casting to any --- .../workflows/executor/execution-core.test.ts | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/apps/sim/lib/workflows/executor/execution-core.test.ts b/apps/sim/lib/workflows/executor/execution-core.test.ts index bc3556b03b4..83841247726 100644 --- a/apps/sim/lib/workflows/executor/execution-core.test.ts +++ b/apps/sim/lib/workflows/executor/execution-core.test.ts @@ -910,6 +910,17 @@ describe('executeWorkflowCore terminal finalization sequencing', () => { expect(loadWorkflowDeploymentVersionStateMock).not.toHaveBeenCalled() }) + function createDraftResumeSnapshot(workflow: unknown) { + const base = createSnapshot() + return new ExecutionSnapshot( + { ...base.metadata, resumeFromSnapshot: true, resumeTerminalNoop: true }, + workflow, + {}, + {}, + [] + ) + } + it('runs a draft resume on the graph it paused on, never the live draft', async () => { const pausedWorkflow = { version: '1.0', @@ -925,18 +936,11 @@ describe('executeWorkflowCore terminal finalization sequencing', () => { logs: [], metadata: { duration: 1, startTime: 'start', endTime: 'end' }, }) - const resumedSnapshot = createSnapshot() - resumedSnapshot.metadata = { - ...resumedSnapshot.metadata, - resumeFromSnapshot: true, - resumeTerminalNoop: true, - } as any - ;(resumedSnapshot as any).workflow = pausedWorkflow await executeWorkflowCore({ - snapshot: resumedSnapshot as any, + snapshot: createDraftResumeSnapshot(pausedWorkflow), callbacks: {}, - loggingSession: loggingSession as any, + loggingSession: loggingSession as unknown as LoggingSession, skipLogCreation: true, }) @@ -947,18 +951,11 @@ describe('executeWorkflowCore terminal finalization sequencing', () => { }) it('fails a draft resume whose snapshot lost its paused graph', async () => { - const resumedSnapshot = createSnapshot() - resumedSnapshot.metadata = { - ...resumedSnapshot.metadata, - resumeFromSnapshot: true, - } as any - ;(resumedSnapshot as any).workflow = undefined - await expect( executeWorkflowCore({ - snapshot: resumedSnapshot as any, + snapshot: createDraftResumeSnapshot(undefined), callbacks: {}, - loggingSession: loggingSession as any, + loggingSession: loggingSession as unknown as LoggingSession, skipLogCreation: true, }) ).rejects.toThrow('Draft resume is missing the workflow graph it paused on')