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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Reducer-based pipeline architecture.
//!
//! This module implements the event-sourced reducer architecture.
//! It provides:
//! - Pure state reduction with explicit event transitions
//! - Immutable pipeline state that doubles as checkpoint
//! - Event log for debugging and replay
//! - Effect handlers for side effects (git operations, agent execution)
//!
//! # Single Source of Truth
//!
//! The reducer state is the **single source of truth** for all pipeline decisions:
//!
//! - **Phase transitions**: Only happen via reducer events, never via file checks
//! - **Agent selection**: Determined by `state.agent_chain`, not config lookups
//! - **Agent fallback**: Triggered by reducer events (`AgentFallbackTriggered`, `AgentInvocationFailed`)
//! - **XSD retry**: Tracked in `ContinuationState.xsd_retry_count` / `ContinuationState.xsd_retry_pending`, not hidden logic
//! - **Pipeline completion**: Determined by `state.phase == Complete`, not file existence
//!
//! **Invariant**: No phase module or effect handler makes control-flow decisions.
//! All decisions happen via events processed by [`reduce`], which returns new state.
//!
//! All effects are determined by [`determine_next_effect`], a pure function of state.
//! No external file checks or configuration lookups influence effect determination.
//!
//! # Key Types
//!
//! - [`PipelineState`] - Immutable state representing current pipeline progress
//! - [`PipelineEvent`] - Events that trigger state transitions
//! - [`reduce`] - Pure function: `(State, Event) → State`
//! - [`determine_next_effect`] - Pure function: `State → Effect`
//! - [`EffectHandler`] - Trait for executing effects (impure operations)
//!
//! See also: [`CODE_STYLE.md`](https://codeberg.org/mistlight/RalphWithReviewer/src/branch/main/CODE_STYLE.md)
//! for the architecture overview.
//!
//! # Architecture
//!
//! ```text
//! ┌──────────────────────────────────────────────────┐
//! │ Pipeline State │
//! │ (immutable: phase, iteration, agent_chain, history) │
//! └──────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌──────────────────────────────────────────────────┐
//! │ Reducer │
//! │ fn reduce(state: State, event: Event) -> State │
//! │ [Pure, no side effects] │
//! └──────────────────────────────────────────────────┘
//! ▲
//! │
//! ┌──────────────────────────────────────────────────┐
//! │ Events │
//! │ DevelopmentIterationCompleted | AgentFailed | │
//! │ ReviewPassCompleted | RebaseSucceeded | ... │
//! └──────────────────────────────────────────────────┘
//! ▲
//! │
//! ┌──────────────────────────────────────────────────┐
//! │ Effect Handlers │
//! │ (Agent execution, file I/O, git operations) │
//! │ [Side effects isolated here] │
//! └──────────────────────────────────────────────────┘
//! ```
//!
//! # Quick Start
//!
//! Run pipeline with reducer:
//!
//! ```ignore
//! use ralph_workflow::reducer::{run_event_loop, PipelineState};
//!
//! let state = PipelineState::initial(developer_iters, reviewer_reviews);
//! let result = run_event_loop(&mut phase_ctx, Some(state), Default::default())?;
//! ```
//!
//! # State Inspection
//!
//! Inspect pipeline state at any point:
//!
//! ```ignore
//! println!("Current phase: {}", state.phase);
//! println!("Iteration: {}/{}", state.iteration, state.total_iterations);
//! println!("Current agent: {:?}", state.agent_chain.current_agent());
//! ```
//!
//! # Event Replay
//!
//! Replay events from log:
//!
//! ```ignore
//! let final_state = events.into_iter()
//! .fold(initial_state, |s, e| reduce(s, e));
//! ```
//!
//! # Testing Strategy
//!
//! The reducer architecture is designed for extensive testability:
//!
//! ## Unit Tests
//!
//! - **Pure reducer**: `reduce()` function has no side effects, 100% testable
//! - **State transitions**: Each event → state transition tested in `state_reduction.rs` tests
//! - **Agent chain**: Fallback logic tested via `AgentChainState` methods
//! - **Error classification**: All error kinds tested in `fault_tolerant_executor.rs`
//!
//! ## Integration Tests
//!
//! - **State machine**: Real pipeline execution verifies correct phase transitions
//! - **Event replay**: Event logs can reproduce final state deterministically
//!
//! # Testing Reducer Purity
//!
//! Reducer is easy to test - pure function with no side effects:
//!
//! ```ignore
//! #[test]
//! fn test_agent_fallback() {
//! let state = create_test_state();
//! let event = PipelineEvent::AgentInvocationFailed { ... };
//! let new_state = reduce(state, event);
//! assert_eq!(new_state.agent_chain.current_agent_index, 1);
//! }
//! ```
//!
//! # Running Tests
//!
//! ```bash
//! # Unit tests only
//! cargo test -p ralph-workflow --lib --all-features
//!
//! # Integration tests
//! cargo test -p ralph-workflow-tests --all-targets
//!
//! # With coverage
//! cargo test -p ralph-workflow --lib --all-features -- --nocapture
//! ```
pub use crateAgentRole;
pub type MainEffectHandler = MainEffectHandler;
pub use ;
pub use PipelineEvent;
pub use PipelinePhase;
pub use ;
pub use AgentChainState;
pub use CommitState;
pub use PipelineState;
pub use PromptMode;
pub use reduce;
pub use UIEvent;
// Re-export CheckpointTrigger for external use
pub use CheckpointTrigger;
// Re-export category enums for external use
pub use ;
pub use create_test_state;