1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Main event loop implementation.
//!
//! This module contains the core orchestrate-handle-reduce cycle that drives
//! the reducer-based pipeline. The event loop coordinates pure reducer logic
//! with impure effect handlers, maintaining strict separation of concerns.
//!
//! ## Event Loop Cycle
//!
//! ```text
//! State → Orchestrate → Effect → Handle → Event → Reduce → Next State
//! (pure) (impure) (pure)
//! ```
//!
//! The loop continues until reaching a terminal state (Interrupted, Completed)
//! or until max iterations is exceeded.
//!
//! ## Architecture
//!
//! The event loop is organized into several modules:
//! - `driver` - Main iteration loop implementing orchestrate→handle→reduce cycle
//! - `recovery` - Defensive completion and max iterations handling
//! - `error_handling` - Panic recovery and error routing
//! - `trace` - Trace buffer and diagnostic dumps
//! - `config` - Configuration and initialization
use crate;
use craterun_event_loop_driver;
use cratePhaseContext;
use crate;
use Result;
/// Trait for handlers that maintain internal state.
///
/// This trait allows the event loop to update the handler's internal state
/// after each event is processed.
/// Run the main event loop for the reducer-based pipeline.
///
/// This function orchestrates pipeline execution by repeatedly:
/// 1. Determining the next effect based on the current state
/// 2. Executing the effect through the effect handler (which performs side effects)
/// 3. Applying the resulting event to state through the reducer (pure function)
/// 4. Repeating until a terminal state is reached or max iterations exceeded
///
/// The entire event loop is wrapped in panic recovery to ensure the pipeline
/// never crashes due to agent failures (panics only; aborts/segfaults cannot be recovered).
///
/// # Arguments
///
/// * `ctx` - Phase context for effect handlers
/// * `initial_state` - Optional initial state (if None, creates a new state)
/// * `config` - Event loop configuration
///
/// # Returns
///
/// Returns the event loop result containing the completion status and final state.
///
/// # Errors
///
/// Returns error if the operation fails.
/// Run the event loop with a custom effect handler.
///
/// This variant allows injecting a custom effect handler for testing.
/// The handler must implement `EffectHandler` and `StatefulHandler` traits.
///
/// # Arguments
///
/// * `ctx` - Phase context for effect handlers
/// * `initial_state` - Optional initial state (if None, creates a new state)
/// * `config` - Event loop configuration
/// * `handler` - Custom effect handler (e.g., `MockEffectHandler` for testing)
///
/// # Returns
///
/// Returns the event loop result containing the completion status and final state.
///
/// # Errors
///
/// Returns error if the operation fails.