Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions apps/sim/lib/logs/execution/logging-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
59 changes: 54 additions & 5 deletions apps/sim/lib/workflows/executor/execution-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -914,6 +910,58 @@ 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',
blocks: [{ id: 'paused-block' }],
connections: [],
loops: {},
parallels: {},
}
executorExecuteMock.mockResolvedValue({
success: true,
status: 'completed',
output: {},
logs: [],
metadata: { duration: 1, startTime: 'start', endTime: 'end' },
})

await executeWorkflowCore({
snapshot: createDraftResumeSnapshot(pausedWorkflow),
callbacks: {},
loggingSession: loggingSession as unknown as LoggingSession,
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 () => {
await expect(
executeWorkflowCore({
snapshot: createDraftResumeSnapshot(undefined),
callbacks: {},
loggingSession: loggingSession as unknown as LoggingSession,
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: '<EMAIL_ADDRESS>', count: 7 }]
Expand Down Expand Up @@ -953,6 +1001,7 @@ describe('executeWorkflowCore terminal finalization sequencing', () => {
decisions: { router: {}, condition: {} },
completedLoops: [],
activeExecutionPath: [],
pendingQueue: [],
trustedLargeValueAccess: { executionIds: [], largeValueKeys: [], fileKeys: [] },
}
}
Expand Down
62 changes: 42 additions & 20 deletions apps/sim/lib/workflows/executor/execution-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -905,14 +919,21 @@ 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')
: triggerType === 'webhook' || triggerType === 'schedule'
? '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.')
Expand All @@ -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<string>()
processedInput =
resumeFromSnapshot || runFromBlock
Expand Down
Loading