ralph-workflow 0.7.18

PROMPT-driven multi-agent orchestrator for git repos
Documentation
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Configuration types and enums.
//!
//! This module defines the core configuration types used throughout Ralph:
//! - [`Config`]: The main configuration struct
//! - [`ReviewDepth`]: Review thoroughness levels
//! - [`Verbosity`]: Output verbosity levels

// Re-export cloud types so existing paths like `config::types::CloudConfig` remain valid.
pub use super::cloud::{
    CloudConfig, CloudStateConfig, GitAuthMethod, GitAuthStateMethod, GitRemoteConfig,
    GitRemoteStateConfig,
};
use super::truncation;
use std::path::PathBuf;

/// Review depth levels for controlling review thoroughness.
///
/// # Variants
///
/// * `Standard` - Balanced review covering functionality, quality, and security
/// * `Comprehensive` - In-depth analysis with priority-ordered checks
/// * `Security` - Security-focused analysis emphasizing OWASP Top 10
/// * `Incremental` - Focused review of changed files only (git diff)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ReviewDepth {
    /// Standard review - balanced coverage of functionality, quality, and security
    #[default]
    Standard,
    /// Comprehensive review - in-depth analysis with priority-ordered checks
    Comprehensive,
    /// Security-focused review - emphasizes security analysis above all else
    Security,
    /// Incremental review - focuses only on changed files (git diff)
    Incremental,
}

impl ReviewDepth {
    /// Parse review depth from string.
    ///
    /// # Arguments
    ///
    /// * `s` - The string to parse
    ///
    /// # Returns
    ///
    /// Returns `Some(ReviewDepth)` if the string matches a known alias,
    /// `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// assert_eq!(ReviewDepth::from_str("standard"), Some(ReviewDepth::Standard));
    /// assert_eq!(ReviewDepth::from_str("security"), Some(ReviewDepth::Security));
    /// ```
    pub(crate) fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "standard" | "default" | "normal" => Some(Self::Standard),
            "comprehensive" | "thorough" | "full" => Some(Self::Comprehensive),
            "security" | "secure" | "security-focused" => Some(Self::Security),
            "incremental" | "diff" | "changed" => Some(Self::Incremental),
            _ => None,
        }
    }

    /// Get a description for display.
    pub(crate) const fn description(self) -> &'static str {
        match self {
            Self::Standard => "Balanced review covering functionality, quality, and security",
            Self::Comprehensive => "In-depth analysis with priority-ordered checks",
            Self::Security => "Security-focused analysis emphasizing OWASP Top 10",
            Self::Incremental => "Focused review of changed files only (git diff)",
        }
    }
}

/// Verbosity levels for output.
///
/// # Variants
///
/// * `Quiet` (0) - Minimal output, aggressive truncation
/// * `Normal` (1) - Balanced output with moderate truncation
/// * `Verbose` (2) - Expanded output limits (default)
/// * `Full` (3) - No truncation, show all content
/// * `Debug` (4) - Maximum verbosity, includes raw JSON and detailed info
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Verbosity {
    /// Quiet - minimal output, aggressive truncation
    Quiet = 0,
    /// Normal - balanced output with moderate truncation
    Normal = 1,
    /// Verbose - expanded output limits (default)
    Verbose = 2,
    /// Full - no truncation, show all content
    Full = 3,
    /// Debug - maximum verbosity, includes raw JSON and detailed info
    Debug = 4,
}

impl From<u8> for Verbosity {
    fn from(v: u8) -> Self {
        match v {
            0 => Self::Quiet,
            1 => Self::Normal,
            2 => Self::Verbose,
            3 => Self::Full,
            _ => Self::Debug,
        }
    }
}

impl Verbosity {
    /// Get truncation limit for content type.
    ///
    /// # Arguments
    ///
    /// * `content_type` - The type of content:
    ///   - "text": Assistant text output
    ///   - "`tool_result"`: Tool execution results
    ///   - "`tool_input"`: Tool input parameters
    ///   - "user": User messages
    ///   - "result": Final result summaries
    ///   - "command": Command execution strings
    ///   - "`agent_msg"`: Agent messages/thinking
    ///
    /// # Returns
    ///
    /// The maximum number of characters to display for the given content type.
    pub(crate) fn truncate_limit(self, content_type: &str) -> usize {
        truncation::get_limit(self as u8, content_type)
    }

    /// Returns true if this verbosity level should show debug information.
    pub(crate) const fn is_debug(self) -> bool {
        matches!(self, Self::Debug)
    }

    /// Returns true if this verbosity level is at least Verbose.
    pub(crate) const fn is_verbose(self) -> bool {
        matches!(self, Self::Verbose | Self::Full | Self::Debug)
    }

    /// Returns true if tool inputs should be shown (Normal and above).
    ///
    /// Tool inputs provide crucial context for understanding what the agent is doing.
    /// They are shown at Normal level and above for better usability.
    pub(crate) const fn show_tool_input(self) -> bool {
        !matches!(self, Self::Quiet)
    }
}

/// Behavioral flags for Ralph configuration.
///
/// Groups user interaction and validation-related boolean settings.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct BehavioralFlags {
    /// Interactive mode (keep agent in foreground)
    pub(crate) interactive: bool,
    /// Whether to auto-detect project stack for review guidelines
    pub(crate) auto_detect_stack: bool,
    /// Whether to run strict PROMPT.md validation
    pub(crate) strict_validation: bool,
}

/// Feature flags for Ralph configuration.
///
/// Groups optional feature toggle settings.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct FeatureFlags {
    /// Whether to enable checkpoint/resume functionality
    pub(crate) checkpoint_enabled: bool,
    /// Force universal review prompt for all agents (default: auto-detect)
    /// When true, the universal/simplified prompt is always used for review
    pub(crate) force_universal_prompt: bool,
}

/// Ralph configuration.
///
/// This struct holds all configuration options for Ralph, populated from
/// environment variables and CLI arguments. Default values are applied
/// via [`Default::default()`].
#[derive(Debug, Clone, PartialEq)]
// Configuration options naturally use many boolean flags. These represent
// independent feature toggles, not a state machine, so bools are appropriate.
pub struct Config {
    /// Developer (driver) agent (set via CLI, env, or `agent_chain`)
    pub(crate) developer_agent: Option<String>,
    /// Reviewer agent (set via CLI, env, or `agent_chain`)
    pub(crate) reviewer_agent: Option<String>,
    /// Developer command override
    pub(crate) developer_cmd: Option<String>,
    /// Reviewer command override
    pub(crate) reviewer_cmd: Option<String>,
    /// Commit command override
    pub(crate) commit_cmd: Option<String>,
    /// Developer model override (e.g., "-m opencode/glm-4.7-free")
    /// Passed to the agent's `model_flag` parameter
    pub(crate) developer_model: Option<String>,
    /// Reviewer model override (e.g., "-m opencode/claude-sonnet-4")
    /// Passed to the agent's `model_flag` parameter
    pub(crate) reviewer_model: Option<String>,
    /// Developer provider override (e.g., "opencode", "anthropic", "openai")
    /// When set, constructs the model flag as "-m {`provider}/{model_name`}"
    pub(crate) developer_provider: Option<String>,
    /// Reviewer provider override (e.g., "opencode", "anthropic", "openai")
    /// When set, constructs the model flag as "-m {`provider}/{model_name`}"
    pub(crate) reviewer_provider: Option<String>,
    /// JSON parser override for the reviewer agent (claude, codex, gemini, opencode, generic)
    /// When set, overrides the agent's configured `json_parser` setting
    pub(crate) reviewer_json_parser: Option<String>,
    /// Feature flags (checkpoint, universal prompt)
    pub(crate) features: FeatureFlags,
    /// Number of developer iterations
    pub(crate) developer_iters: u32,
    /// Number of reviewer re-review passes after fix
    pub(crate) reviewer_reviews: u32,
    /// Fast check command (optional)
    pub(crate) fast_check_cmd: Option<String>,
    /// Full check command (optional)
    pub(crate) full_check_cmd: Option<String>,
    /// Behavioral flags (interactive, auto-detect, strict validation)
    pub(crate) behavior: BehavioralFlags,
    /// Path to save last prompt
    pub(crate) prompt_path: PathBuf,
    /// User templates directory for custom template overrides
    /// When set, templates in this directory take priority over embedded templates
    pub(crate) user_templates_dir: Option<PathBuf>,
    /// Developer context level (0=minimal, 1=normal)
    pub(crate) developer_context: u8,
    /// Reviewer context level (0=minimal/fresh eyes, 1=normal)
    pub(crate) reviewer_context: u8,
    /// Verbosity level
    pub(crate) verbosity: Verbosity,
    /// Review depth level (standard, comprehensive, security, incremental)
    pub(crate) review_depth: ReviewDepth,
    /// Isolation mode: when true, NOTES.md and ISSUES.md are not generated and
    /// any existing ones are deleted at the start of each run. This prevents
    /// context contamination from previous runs. Default: true.
    pub(crate) isolation_mode: bool,
    /// Git user name for commits (optional, falls back to git config)
    pub(crate) git_user_name: Option<String>,
    /// Git user email for commits (optional, falls back to git config)
    pub(crate) git_user_email: Option<String>,
    /// Show streaming quality metrics at the end of agent output.
    ///
    /// This field is `pub(crate)` as streaming metrics are an internal concern.
    /// External access is not required; metrics are displayed via CLI flag.
    pub(crate) show_streaming_metrics: bool,
    /// Maximum number of format correction retries during review output parsing.
    /// When the reviewer agent produces unparseable output, the orchestrator will
    /// retry up to this many times with a format correction prompt. Default: 5.
    pub(crate) review_format_retries: u32,
    /// Maximum continuation attempts when developer returns "partial" or "failed".
    ///
    /// # Semantics
    ///
    /// This value counts *continuation attempts* beyond the initial attempt.
    /// Total valid attempts per iteration is `1 + max_dev_continuations`.
    ///
    /// - `0` = no continuations (1 total attempt)
    /// - `2` = two continuations (3 total attempts)
    ///
    /// # Default Behavior
    ///
    /// **CRITICAL:** The system ALWAYS applies a default of 2 (3 total attempts) when this
    /// field is None. The Option wrapper exists ONLY for backward compatibility with direct
    /// Config construction (`Config::default()`, `Config::test_default()`).
    ///
    /// When loaded via `config_from_unified()`:
    /// - `UnifiedConfig::general.max_dev_continuations` has serde default of 2
    /// - Converted to Some(2) in Config
    /// - Applied unconditionally in `create_initial_state_with_config()`
    ///
    /// This ensures dev loop is ALWAYS bounded, preventing infinite continuation cycles.
    ///
    /// Default: 2 continuations (3 total attempts per iteration).
    pub max_dev_continuations: Option<u32>,
    /// Maximum XSD retry attempts when agent output fails XML validation.
    /// Higher values allow more attempts to fix XML formatting before agent fallback.
    /// Default: 10 (10 retries before falling back to next agent).
    pub max_xsd_retries: Option<u32>,
    /// Maximum same-agent retry attempts for invocation failures that should not
    /// immediately trigger agent fallback (e.g., timeout/internal/unknown and other
    /// non-auth, non-rate-limit failures).
    ///
    /// # Semantics
    ///
    /// This value is a *failure budget* for the current agent: it counts consecutive
    /// failures that are routed through the reducer's same-agent retry path.
    ///
    /// With `max_same_agent_retries = 2`:
    /// - 1st failure → retry the same agent
    /// - 2nd failure → fall back to the next agent
    ///
    /// Default: 2 (one retry before falling back).
    pub max_same_agent_retries: Option<u32>,
    /// Maximum additional residual commit retries after the initial residual-files check.
    ///
    /// This value counts retry passes beyond pass 1.
    /// Default: 10 additional retries (carry forward after pass 11).
    pub max_commit_residual_retries: Option<u32>,
    /// Maximum execution history entries to keep in memory (default: 1000).
    /// Prevents unbounded memory growth by dropping oldest entries when limit is reached.
    pub execution_history_limit: usize,
    /// Cloud runtime configuration (internal).
    pub(crate) cloud: CloudConfig,
}

impl Config {
    /// Get the user templates directory.
    #[must_use]
    pub const fn user_templates_dir(&self) -> Option<&std::path::PathBuf> {
        self.user_templates_dir.as_ref()
    }

    /// Create a test-appropriate Config with safe defaults.
    ///
    /// This function creates a Config suitable for integration tests,
    /// with all agent execution disabled and isolation mode enabled.
    /// It does NOT load from environment variables or config files.
    #[must_use]
    pub fn test_default() -> Self {
        Self {
            developer_agent: Some("codex".to_string()),
            reviewer_agent: Some("codex".to_string()),
            developer_cmd: None,
            reviewer_cmd: None,
            commit_cmd: None,
            developer_model: None,
            reviewer_model: None,
            developer_provider: None,
            reviewer_provider: None,
            reviewer_json_parser: None,
            features: FeatureFlags {
                checkpoint_enabled: true,
                force_universal_prompt: false,
            },
            developer_iters: 0,
            reviewer_reviews: 0,
            fast_check_cmd: None,
            full_check_cmd: None,
            behavior: BehavioralFlags {
                interactive: false,
                auto_detect_stack: false,
                strict_validation: false,
            },
            prompt_path: PathBuf::from(".agent/last_prompt.txt"),
            user_templates_dir: None,
            developer_context: 0,
            reviewer_context: 0,
            verbosity: Verbosity::Quiet,
            review_depth: ReviewDepth::Standard,
            isolation_mode: true,
            git_user_name: Some("Test".to_string()),
            git_user_email: Some("test@example.com".to_string()),
            show_streaming_metrics: false,
            review_format_retries: 5,
            max_dev_continuations: Some(2),
            max_xsd_retries: Some(10),
            max_same_agent_retries: Some(2),
            max_commit_residual_retries: Some(10),
            execution_history_limit: 1000,
            cloud: CloudConfig::disabled(),
        }
    }

    /// Set isolation mode and return new config (functional pattern).
    #[must_use]
    pub fn with_isolation_mode(self, isolation_mode: bool) -> Self {
        Self {
            isolation_mode,
            ..self
        }
    }

    /// Set developer iterations and return new config (functional pattern).
    #[must_use]
    pub fn with_developer_iters(self, iters: u32) -> Self {
        Self {
            developer_iters: iters,
            ..self
        }
    }

    /// Set reviewer reviews and return new config (functional pattern).
    #[must_use]
    pub fn with_reviewer_reviews(self, reviews: u32) -> Self {
        Self {
            reviewer_reviews: reviews,
            ..self
        }
    }

    /// Set `auto_detect_stack` and return new config (functional pattern).
    #[must_use]
    pub fn with_auto_detect_stack(self, auto_detect: bool) -> Self {
        Self {
            behavior: BehavioralFlags {
                auto_detect_stack: auto_detect,
                ..self.behavior
            },
            ..self
        }
    }

    /// Set verbosity and return new config (functional pattern).
    #[must_use]
    pub fn with_verbosity(self, verbosity: Verbosity) -> Self {
        Self { verbosity, ..self }
    }

    /// Set `review_depth` and return new config (functional pattern).
    #[must_use]
    pub fn with_review_depth(self, review_depth: ReviewDepth) -> Self {
        Self {
            review_depth,
            ..self
        }
    }

    /// Set `developer_agent` and return new config (functional pattern).
    #[must_use]
    pub fn with_developer_agent(self, agent: String) -> Self {
        Self {
            developer_agent: Some(agent),
            ..self
        }
    }

    /// Set `reviewer_agent` and return new config (functional pattern).
    #[must_use]
    pub fn with_reviewer_agent(self, agent: String) -> Self {
        Self {
            reviewer_agent: Some(agent),
            ..self
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        super::loader::default_config()
    }
}