The Workflow Execution Engine is the core system responsible for orchestrating and executing workflow definitions through a DAG-based (Directed Acyclic Graph) execution model. It manages the entire lifecycle from trigger reception through block-by-block execution to final output delivery, supporting multiple execution modes including streaming responses, background jobs, and human-in-the-loop pause/resume patterns.
The execution engine is structured around three primary layers: the entry layer (API routes and background jobs), the orchestration layer (preprocessing, snapshot management, and core execution), and the execution layer (DAG executor and block handlers).
The following diagram maps high-level execution concepts to the specific classes and functions implemented in the codebase.
Sources: apps/sim/app/api/workflows/[id]/execute/route.ts:46-47, apps/sim/lib/workflows/executor/execution-core.ts22-31 apps/sim/executor/execution/block-executor.ts59-81 apps/sim/executor/index.ts1-7 apps/sim/app/api/workflows/[id]/execute/route.ts:56-56
The execution pipeline processes workflow requests through distinct phases: trigger reception, preprocessing, serialization, DAG execution, and output delivery.
Workflow executions are initiated via several entry points defined in the API and background workers:
| Trigger Type | Entry Point | Authentication | Execution Mode |
|---|---|---|---|
| Manual / API | POST /api/workflows/[id]/execute | checkHybridAuth | Inline/Async |
| Webhook | POST /api/webhooks/trigger/[path] | Provider-specific | Background (Trigger.dev) |
| Schedule | executeWorkflowJob | Cron Trigger | Background (Trigger.dev) |
| Child Workflow | WorkflowBlockHandler | Internal Context | Inline |
Sources: apps/sim/app/api/workflows/[id]/execute/route.ts:10-10, apps/sim/app/api/workflows/[id]/execute/route.ts:157-210, apps/sim/executor/handlers/workflow/workflow-handler.ts46-68 apps/sim/app/api/workflows/[id]/execute/route.ts:56-56
All executions pass through preprocessExecution apps/sim/app/api/workflows/[id]/execute/route.ts:40-40 which validates requests and enforces constraints such as authentication, rate limits, and deployment status. In the API layer, the executeWorkflowBodySchema apps/sim/app/api/workflows/[id]/execute/route.ts:9-9 validates the incoming payload before passing it to executeWorkflowCore.
The execution engine maintains state through ExecutionSnapshot, LoggingSession, and the ExecutionContext.
ExecutionSnapshot apps/sim/app/api/workflows/[id]/execute/route.ts:62-62 encapsulates all data required to execute or resume a workflow. It includes metadata, workflow state, and initial variables.
LoggingSession apps/sim/lib/logs/execution/logging-session.ts132 manages execution log lifecycle and database persistence. It provides methods like safeComplete apps/sim/lib/logs/execution/logging-session.ts204-211 to ensure logs and costs are written even if execution fails. It also tracks real-time progress by persisting markers for the last started apps/sim/lib/logs/execution/logging-session.ts188-201 and completed apps/sim/lib/logs/execution/logging-session.ts203-216 blocks.
The DAGExecutor apps/sim/executor/execution/executor.ts6 (aliased as Executor in apps/sim/executor/index.ts6) is the central orchestrator. It builds a dependency graph and executes nodes in topological order.
The BlockExecutor apps/sim/executor/execution/block-executor.ts59 handles the lifecycle of an individual block's execution:
VariableResolver to resolve inputs apps/sim/executor/execution/block-executor.ts125-147BlockHandler via findHandler apps/sim/executor/execution/block-executor.ts82handler.executeWithNode or handler.execute apps/sim/executor/execution/block-executor.ts169-171NormalizedBlockOutput apps/sim/executor/execution/block-executor.ts191-196BlockStateWriter with results apps/sim/executor/execution/block-executor.ts221The engine supports complex control flow via specialized orchestrators:
Sources: apps/sim/executor/orchestrators/loop.ts1-10 apps/sim/executor/orchestrators/parallel.ts1-10
The execution engine supports real-time output delivery through Server-Sent Events (SSE) and callback functions.
ExecutionContext apps/sim/executor/types.ts330-348 includes hooks for execution events:
onBlockStart: Fired when a block begins apps/sim/executor/execution/block-executor.ts104onBlockComplete: Fired when a block finishes apps/sim/executor/execution/block-executor.ts227-231onStream: Handles streaming content from blocks like Agents apps/sim/executor/execution/block-executor.ts181-190The useWorkflowExecution hook apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts:141-141 manages the frontend execution state. It uses useExecutionStream apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts:46-46 to process SSE events and updates the useTerminalConsoleStore apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts:154-163 to reflect block status and logs in the UI.
The WorkflowBlockHandler apps/sim/executor/handlers/workflow/workflow-handler.ts38 allows workflows to call other workflows.
validateCallChain apps/sim/executor/handlers/workflow/workflow-handler.ts83-90lazyCleanupInputMapping apps/sim/executor/handlers/workflow/workflow-handler.ts123-129Executor instance for the child workflow apps/sim/executor/handlers/workflow/workflow-handler.ts168-195The engine manages "Human-in-the-loop" blocks via the pause/resume infrastructure.
contextId generated via generatePauseContextId apps/sim/executor/execution/block-executor.ts29SerializableExecutionState is saved when a workflow pauses apps/sim/executor/execution/types.ts67-68runFromBlock logic in executeWorkflowCore loads the snapshot and restarts execution from the specified block apps/sim/lib/workflows/executor/execution-core.ts50-53Logs are processed into TraceSpan objects apps/sim/lib/logs/execution/trace-spans/trace-spans.ts49-52 for hierarchical display in the Terminal.
This diagram shows how execution events travel from the backend handlers to the frontend Terminal UI.
Sources: apps/sim/stores/terminal/console/store.ts13-18 apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts:141-163, apps/sim/executor/types.ts225-247 apps/sim/lib/workflows/executor/execution-core.ts172-211
useTerminalConsoleStore apps/sim/stores/terminal/console/store.ts16-18 maintains a list of ConsoleEntry items, which are updated in real-time as SSE events arrive via addConsole apps/sim/stores/terminal/console/store.ts207Sources: apps/sim/executor/types.ts225-247 apps/sim/lib/logs/execution/trace-spans/trace-spans.ts49-66 apps/sim/stores/terminal/console/store.ts12-18
Refresh this wiki