ralph_workflow/app/event_loop/config.rs
1//! Event loop configuration and initialization.
2//!
3//! This module defines configuration types and initialization logic for the
4//! reducer-based event loop.
5
6use crate::phases::PhaseContext;
7use crate::reducer::event::PipelinePhase;
8use crate::reducer::state::ContinuationState;
9use crate::reducer::PipelineState;
10
11/// Create initial pipeline state with continuation limits from config.
12///
13/// This function creates a `PipelineState` with XSD retry and continuation limits
14/// loaded from the config, ensuring these values are available for the reducer
15/// to make deterministic retry decisions.
16pub(crate) fn create_initial_state_with_config(ctx: &PhaseContext<'_>) -> PipelineState {
17 // Config semantics: max_dev_continuations counts continuation attempts *beyond*
18 // the initial attempt. ContinuationState::max_continue_count semantics are
19 // "maximum total attempts including initial".
20 let max_dev_continuations = ctx.config.max_dev_continuations.unwrap_or(2);
21 let max_continue_count = 1 + max_dev_continuations;
22
23 let continuation = ContinuationState::with_limits(
24 ctx.config.max_xsd_retries.unwrap_or(10),
25 max_continue_count,
26 ctx.config.max_same_agent_retries.unwrap_or(2),
27 );
28 PipelineState::initial_with_continuation(
29 ctx.config.developer_iters,
30 ctx.config.reviewer_reviews,
31 continuation,
32 )
33}
34
35/// Maximum iterations for the main event loop to prevent infinite loops.
36///
37/// This is a safety limit - the pipeline should complete well before this limit
38/// under normal circumstances. If reached, it indicates either a bug in the
39/// reducer logic or an extremely complex project.
40///
41/// NOTE: Even 1_000_000 can still be too low for extremely slow-progress runs.
42/// If this cap is hit in practice, prefer making it configurable and/or
43/// investigating why the reducer is not converging.
44pub const MAX_EVENT_LOOP_ITERATIONS: usize = 1_000_000;
45
46/// Configuration for event loop.
47#[derive(Clone, Debug)]
48pub struct EventLoopConfig {
49 /// Maximum number of iterations to prevent infinite loops.
50 pub max_iterations: usize,
51}
52
53/// Result of event loop execution.
54#[derive(Debug, Clone)]
55pub struct EventLoopResult {
56 /// Whether pipeline completed successfully.
57 pub completed: bool,
58 /// Total events processed.
59 pub events_processed: usize,
60 /// Final reducer phase when the loop stopped.
61 pub final_phase: PipelinePhase,
62 /// Final pipeline state (for metrics and summary).
63 pub final_state: PipelineState,
64}