Skip to main content
The Execution Engine is responsible for running workflows, managing node execution, propagating data, and handling errors.

Overview

CrystalFlow’s execution engine provides:

Plan-Based Execution

Build execution plan, then execute with full control

Conditional Flow

Support for if/else and switch/case branching

Event System

Comprehensive events for monitoring execution

Error Handling

Graceful error handling with detailed context

Cancellation

Support for cancelling long-running executions

The Executor

The Executor class orchestrates workflow execution:

Execution Options

number
Maximum execution time in milliseconds (default: no timeout)
Record<string, any>
Global variables accessible to all nodes during execution
AbortSignal
Optional AbortSignal for cancelling execution

Execution Plan Architecture

CrystalFlow uses a plan-based execution architecture that enables advanced control flow patterns:

How It Works

  1. Build Phase: The workflow graph is analyzed and converted into an execution plan
  2. Execution Phase: The plan is executed step-by-step with full control

Benefits

Conditional Execution

Native support for if/else, switch/case branching

Loop Support

Future support for for/while loops (planned)

Debugging

Step-by-step debugging with breakpoints (planned)

Inspectability

Full visibility into execution plan structure

Step Types

The execution plan consists of different step types:
  • NodeExecutionStep: Execute a single node
  • ConditionalStep: Evaluate condition and execute matching branch
  • LoopStep: Execute steps repeatedly (planned)
  • ParallelStep: Execute multiple steps in parallel (planned)
The plan-based architecture is transparent to users - workflows execute smoothly while supporting advanced control flow patterns like conditionals and future loop support.
For a deep dive into the architecture, see the execution-plan-architecture.md document.

Execution Flow

The execution follows these steps:
1

Validation

Workflow is validated (connections, required inputs)
2

Context Creation

ExecutionContext is created with unique execution ID
3

Build Execution Plan

Workflow graph is analyzed and converted to execution plan with steps and branches
4

Step-by-Step Execution

Each step executes in order:
  • Node Step: Execute node, transfer data, validate, store results
  • Conditional Step: Evaluate condition, select and execute matching branch
  • Check for cancellation between steps
5

Branch Evaluation

When a conditional step is encountered:
  • Execute the conditional node to update its state
  • Call evaluateCondition() to get active branch
  • Execute steps in the matching branch
  • Skip other branches
6

Result Collection

All node results are collected and returned

Event System

The Executor emits events throughout execution:

Before Execution

Node Events

After Execution

Error Events

Step Events

Monitor execution plan step execution:
Step types include:
  • ExecutionStepType.Node - Regular node execution
  • ExecutionStepType.Conditional - Conditional branch selection
  • ExecutionStepType.Loop - Loop execution (planned)
  • ExecutionStepType.Parallel - Parallel execution (planned)

Branch Events

Track conditional branch execution:
Example with IfNode:
Branch events are useful for:
  • Debugging conditional logic
  • Visualizing execution paths
  • Tracking which branches execute in complex workflows
  • Performance profiling of different branches

Async Event Listeners

All event listeners support async functions - the executor waits for them to complete:

Execution Result

The executor returns an ExecutionResult object:

Using Results

Data Propagation

Data flows automatically between connected nodes:

How It Works

  1. Source node executes and sets output values
  2. Executor identifies connections from source outputs
  3. Target node inputs are populated with output values
  4. Target node executes with received data

Error Handling

The execution engine provides robust error handling:

Node Execution Errors

Validation Errors

Timeout Errors

Cancellation

Workflows can be cancelled during execution:

User-Initiated Cancellation

External Signal Cancellation

Timeout-Based Cancellation

Mid-Node Cancellation

Nodes can check for cancellation during long operations:

Execution Context

The ExecutionContext provides runtime information to nodes:

Context Properties

string
Unique identifier for this execution
string
Identifier of the workflow being executed
Record<string, any>
Global variables passed to execution
Date
When execution started

Performance Monitoring

Track execution performance with timing data:

Best Practices

Wrap execution in try-catch and handle different error types appropriately.
Use timeouts to prevent workflows from hanging indefinitely.
Use events for logging, monitoring, and debugging execution flow.
For long-running nodes, periodically check for cancellation.
Pass runtime configuration through variables rather than hardcoding.

Example: Complete Execution

Next Steps

Decorators

Learn about decorator syntax

Execution & Events Guide

Master the event system

Cancellation

Advanced cancellation patterns

Executor API

Complete Executor API docs