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
444
445
446
447
448
449
450
451
452
453
454
//! Banner and UI output utilities.
//!
//! This module contains presentation logic for the pipeline's visual output,
//! including the welcome banner and the final summary display.

use crate::logger::Colors;
use crate::logger::Loggable;

/// Summary data for pipeline completion display.
///
/// All metrics MUST derive from the final `PipelineState.metrics` to ensure
/// consistency and prevent drift between runtime counters and actual progress.
///
/// # Single Source of Truth
///
/// The reducer is the authoritative source for all execution statistics.
/// This struct is purely a presentation layer that receives reducer-derived
/// metrics and formats them for display.
///
/// Decouples the banner presentation logic from the actual pipeline types.
pub struct PipelineSummary {
    /// Total elapsed time formatted as "Xm YYs"
    pub total_time: String,
    /// Number of developer iterations completed (from reducer metrics)
    pub dev_runs_completed: usize,
    /// Total configured developer iterations (from reducer metrics)
    pub dev_runs_total: usize,
    /// Number of review passes completed (from reducer metrics)
    pub review_passes_completed: usize,
    /// Total configured review passes (from reducer metrics)
    pub review_passes_total: usize,
    /// Number of reviewer runs completed (from reducer metrics)
    pub review_runs: usize,
    /// Number of commits created during pipeline (from reducer metrics)
    pub changes_detected: usize,
    /// Whether isolation mode is enabled
    pub isolation_mode: bool,
    /// Whether to show verbose output
    pub verbose: bool,
    /// Optional review metrics summary
    pub review_summary: Option<ReviewSummary>,
    /// Number of connectivity interruptions (offline windows) during the run.
    /// None means no connectivity interruptions occurred.
    pub connectivity_interruptions: Option<usize>,
}

/// Review metrics summary for display.
pub struct ReviewSummary {
    /// One-line summary of review results
    pub summary: String,
    /// Number of unresolved issues
    pub unresolved_count: usize,
    /// Number of unresolved blocking issues
    pub blocking_count: usize,
    /// Optional detailed breakdown (for verbose mode)
    pub detailed_breakdown: Option<String>,
    /// Optional sample unresolved issues (for verbose mode)
    pub samples: Vec<String>,
}

/// Print the welcome banner for the Ralph pipeline.
///
/// Displays a styled ASCII box with the pipeline name and agent information.
///
/// # Arguments
///
/// * `colors` - Color configuration for terminal output
/// * `developer_agent` - Name of the developer agent
/// * `reviewer_agent` - Name of the reviewer agent
pub fn print_welcome_banner(colors: Colors, developer_agent: &str, reviewer_agent: &str) {
    let _ = print_welcome_banner_to(colors, developer_agent, reviewer_agent, std::io::stdout());
}

pub fn print_welcome_banner_to<W: std::io::Write>(
    colors: Colors,
    developer_agent: &str,
    reviewer_agent: &str,
    output: W,
) -> std::io::Result<()> {
    let content = build_welcome_banner_content(colors, developer_agent, reviewer_agent);
    write_banner_to(output, &content)
}

fn build_welcome_banner_content(
    colors: Colors,
    developer_agent: &str,
    reviewer_agent: &str,
) -> String {
    let lines = [
        "",
        &format!(
            "{}{}โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ{}",
            colors.bold(),
            colors.cyan(),
            colors.reset()
        ),
        &format!(
            "{}{}โ”‚{}  {}{}๐Ÿค– Ralph{} {}โ”€ PROMPT-driven agent orchestrator{}              {}{}โ”‚{}",
            colors.bold(),
            colors.cyan(),
            colors.reset(),
            colors.bold(),
            colors.white(),
            colors.reset(),
            colors.dim(),
            colors.reset(),
            colors.bold(),
            colors.cyan(),
            colors.reset()
        ),
        &format!(
            "{}{}โ”‚{}  {}{} ร— {} pipeline for autonomous development{}                 {}{}โ”‚{}",
            colors.bold(),
            colors.cyan(),
            colors.reset(),
            colors.dim(),
            developer_agent,
            reviewer_agent,
            colors.reset(),
            colors.bold(),
            colors.cyan(),
            colors.reset()
        ),
        &format!(
            "{}{}โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ{}",
            colors.bold(),
            colors.cyan(),
            colors.reset()
        ),
        "",
        "",
    ];
    lines.join("\n")
}

/// Print the final summary after pipeline completion.
///
/// Displays statistics about the pipeline run including timing, run counts,
/// and review metrics if available.
///
/// # Arguments
///
/// * `colors` - Color configuration for terminal output
/// * `summary` - Pipeline summary data
/// * `logger` - Logger for final success message (via Loggable trait)
pub fn print_final_summary<L: Loggable>(colors: Colors, summary: &PipelineSummary, logger: &L) {
    let _ = print_final_summary_to(colors, summary, logger, std::io::stdout());
}

pub fn print_final_summary_to<L: Loggable, W: std::io::Write>(
    colors: Colors,
    summary: &PipelineSummary,
    logger: &L,
    output: W,
) -> std::io::Result<()> {
    logger.header("Pipeline Complete", crate::logger::Colors::green);

    let content = build_final_summary_content(colors, summary);

    write_banner_to(output, &content)?;

    // Use the Loggable trait's success method
    logger.success("Ralph pipeline completed successfully!");

    // Log additional status messages via Loggable trait
    if summary.review_runs > 0 {
        logger.info(&format!("Completed {} review run(s)", summary.review_runs));
    }
    if summary.changes_detected > 0 {
        logger.info(&format!("Detected {} change(s)", summary.changes_detected));
    }
    if summary.isolation_mode {
        logger.info("Running in isolation mode");
    }

    // Log warnings for unresolved issues if present
    if let Some(ref review) = summary.review_summary {
        if review.unresolved_count > 0 {
            logger.warn(&format!(
                "{} unresolved issue(s) remaining",
                review.unresolved_count
            ));
        }
        if review.blocking_count > 0 {
            logger.error(&format!(
                "{} blocking issue(s) unresolved",
                review.blocking_count
            ));
        }
    }

    // Log connectivity interruptions if any occurred
    if let Some(interruptions) = summary.connectivity_interruptions {
        if interruptions > 0 {
            logger.info(&format!(
                "Connectivity interruptions: {} (workflow paused {} times due to offline detection; \
                 no continuation or retry budget was consumed during offline windows)",
                interruptions, interruptions
            ));
        }
    }

    Ok(())
}

fn write_banner_to<W: std::io::Write>(mut output: W, content: &str) -> std::io::Result<()> {
    output.write_all(content.as_bytes())
}

fn build_final_summary_content(colors: Colors, summary: &PipelineSummary) -> String {
    let base_lines: Vec<String> = vec![
        "".to_string(),
        format!(
            "{}{}๐Ÿ“Š Summary{}",
            colors.bold(),
            colors.white(),
            colors.reset()
        ),
        format!(
            "{}โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€{}",
            colors.dim(),
            colors.reset()
        ),
        format!(
            "  {}โฑ{}  Total time:      {}{}{}",
            colors.cyan(),
            colors.reset(),
            colors.bold(),
            summary.total_time,
            colors.reset()
        ),
        format!(
            "  {}๐Ÿ”„{}  Dev runs:        {}{}{}/{}",
            colors.blue(),
            colors.reset(),
            colors.bold(),
            summary.dev_runs_completed,
            colors.reset(),
            summary.dev_runs_total
        ),
        format!(
            "  {}๐Ÿ”{}  Review passes:   {}{}{}/{}",
            colors.magenta(),
            colors.reset(),
            colors.bold(),
            summary.review_passes_completed,
            colors.reset(),
            summary.review_passes_total
        ),
        format!(
            "  {}๐Ÿ“{}  Changes detected: {}{}{}",
            colors.green(),
            colors.reset(),
            colors.bold(),
            summary.changes_detected,
            colors.reset()
        ),
    ];

    let verbose_line: Vec<String> = if summary.verbose {
        vec![format!(
            "  {}  {}  (Total runs:     {}{}{}){}",
            colors.dim(),
            colors.magenta(),
            colors.bold(),
            summary.review_runs,
            colors.reset(),
            colors.reset()
        )]
    } else {
        Vec::new()
    };

    let review_lines: Vec<String> = summary
        .review_summary
        .as_ref()
        .map_or(Vec::new(), |review| {
            build_review_summary_content(colors, summary.verbose, review)
                .lines()
                .map(String::from)
                .collect()
        });

    let output_files_lines: Vec<String> =
        build_output_files_content(colors, summary.isolation_mode)
            .lines()
            .map(String::from)
            .collect();

    base_lines
        .into_iter()
        .chain(verbose_line)
        .chain(std::iter::once("".to_string()))
        .chain(review_lines)
        .chain(std::iter::once("".to_string()))
        .chain(output_files_lines)
        .collect::<Vec<_>>()
        .join("\n")
}

fn build_review_summary_content(colors: Colors, verbose: bool, review: &ReviewSummary) -> String {
    if review.unresolved_count == 0 && review.blocking_count == 0 {
        return [format!(
            "  {}โœ“{}   Review result:   {}{}{}",
            colors.green(),
            colors.reset(),
            colors.bold(),
            review.summary,
            colors.reset()
        )]
        .join("\n");
    }

    let base_line: Vec<String> = vec![format!(
        "  {}๐Ÿ”Ž{}  Review summary:  {}{}{}",
        colors.yellow(),
        colors.reset(),
        colors.bold(),
        review.summary,
        colors.reset()
    )];

    let unresolved_line: Vec<String> = if review.unresolved_count > 0 {
        vec![format!(
            "  {}โš {}   Unresolved:      {}{}{} issues remaining",
            colors.red(),
            colors.reset(),
            colors.bold(),
            review.unresolved_count,
            colors.reset()
        )]
    } else {
        Vec::new()
    };

    let verbose_lines: Vec<String> = if verbose {
        let breakdown_lines: Vec<String> =
            review
                .detailed_breakdown
                .as_ref()
                .map_or_else(Vec::<String>::new, |breakdown| {
                    let line_strs: Vec<&str> = breakdown.lines().collect();
                    let dimmed: Vec<String> = line_strs
                        .iter()
                        .map(|line| {
                            format!("      {}{}{}", colors.dim(), line.trim(), colors.reset())
                        })
                        .collect();
                    std::iter::once(format!(
                        "  {}๐Ÿ“Š{}  Breakdown:",
                        colors.dim(),
                        colors.reset()
                    ))
                    .chain(dimmed)
                    .collect()
                });

        let sample_lines: Vec<String> = if !review.samples.is_empty() {
            std::iter::once(format!(
                "  {}๐Ÿงพ{}  Unresolved samples:",
                colors.dim(),
                colors.reset()
            ))
            .chain(
                review
                    .samples
                    .iter()
                    .map(|s| format!("      {}- {}{}", colors.dim(), s, colors.reset())),
            )
            .collect()
        } else {
            Vec::new()
        };

        breakdown_lines.into_iter().chain(sample_lines).collect()
    } else {
        Vec::new()
    };

    let blocking_line: Vec<String> = if review.blocking_count > 0 {
        vec![format!(
            "  {}๐Ÿšจ{}  BLOCKING:        {}{}{} critical/high issues unresolved",
            colors.red(),
            colors.reset(),
            colors.bold(),
            review.blocking_count,
            colors.reset()
        )]
    } else {
        Vec::new()
    };

    base_line
        .into_iter()
        .chain(unresolved_line)
        .chain(verbose_lines)
        .chain(blocking_line)
        .collect::<Vec<_>>()
        .join("\n")
}

fn build_output_files_content(colors: Colors, isolation_mode: bool) -> String {
    let base_lines: Vec<String> = vec![
        format!(
            "{}{}๐Ÿ“ Output Files{}",
            colors.bold(),
            colors.white(),
            colors.reset()
        ),
        format!(
            "{}โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€{}",
            colors.dim(),
            colors.reset()
        ),
        format!(
            "  โ†’ {}PROMPT.md{}           Goal definition",
            colors.cyan(),
            colors.reset()
        ),
        format!(
            "  โ†’ {}.agent/STATUS.md{}    Current status",
            colors.cyan(),
            colors.reset()
        ),
    ];

    let isolation_lines: Vec<String> = if !isolation_mode {
        vec![
            format!(
                "  โ†’ {}.agent/ISSUES.md{}    Review findings",
                colors.cyan(),
                colors.reset()
            ),
            format!(
                "  โ†’ {}.agent/NOTES.md{}     Progress notes",
                colors.cyan(),
                colors.reset()
            ),
        ]
    } else {
        Vec::new()
    };

    base_lines
        .into_iter()
        .chain(isolation_lines)
        .chain(std::iter::once(format!(
            "  โ†’ {}.agent/logs/{}        Detailed logs",
            colors.cyan(),
            colors.reset()
        )))
        .collect::<Vec<_>>()
        .join("\n")
}