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
TheExecutor 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
- Build Phase: The workflow graph is analyzed and converted into an execution plan
- 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.
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:ExecutionStepType.Node- Regular node executionExecutionStepType.Conditional- Conditional branch selectionExecutionStepType.Loop- Loop execution (planned)ExecutionStepType.Parallel- Parallel execution (planned)
Branch Events
Track conditional branch execution:Async Event Listeners
All event listeners support async functions - the executor waits for them to complete:Execution Result
The executor returns anExecutionResult object:
Using Results
Data Propagation
Data flows automatically between connected nodes:How It Works
- Source node executes and sets output values
- Executor identifies connections from source outputs
- Target node inputs are populated with output values
- 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
TheExecutionContext 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
Always Handle Errors
Always Handle Errors
Wrap execution in try-catch and handle different error types appropriately.
Set Reasonable Timeouts
Set Reasonable Timeouts
Use timeouts to prevent workflows from hanging indefinitely.
Monitor Execution Events
Monitor Execution Events
Use events for logging, monitoring, and debugging execution flow.
Check Cancellation in Long Nodes
Check Cancellation in Long Nodes
For long-running nodes, periodically check for cancellation.
Use Variables for Configuration
Use Variables for Configuration
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