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
455
456
457
458
459
460
461
462
463
464
465
466
//! Per-review-cycle baseline tracking.
//!
//! This module manages the baseline commit for each review cycle, ensuring that
//! reviewers only see changes from the current cycle rather than cumulative changes
//! from previous fix commits.
//!
//! # Overview
//!
//! During the review-fix phase, each cycle should:
//! 1. Capture baseline before review (current HEAD)
//! 2. Review sees diff from that baseline
//! 3. Fixer makes changes (reviewer agent by default)
//! 4. Baseline is updated after fix pass
//! 5. Next review cycle sees only new changes
//!
//! This prevents "diff scope creep" where previous fix commits pollute
//! subsequent review passes.

use std::path::Path;

use crate::workspace::{Workspace, WorkspaceFs};

mod iot {
    pub type Result<T> = std::io::Result<T>;
    pub type Error = std::io::Error;
    pub type ErrorKind = std::io::ErrorKind;
}

// Boundary module for libgit2 revwalk operations.
include!("review_baseline/io.rs");

use super::start_commit::get_current_head_oid;

// =============================================================================
// Baseline Persistence (from review_baseline/baseline_persistence.rs)
// =============================================================================

pub const REVIEW_BASELINE_FILE: &str = ".agent/review_baseline.txt";
pub const BASELINE_NOT_SET: &str = "__BASELINE_NOT_SET__";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReviewBaseline {
    Commit(git2::Oid),
    NotSet,
}

pub fn load_review_baseline() -> iot::Result<ReviewBaseline> {
    let repo = git2::Repository::discover(".").map_err(|e| to_io_error(&e))?;
    let repo_root = repo
        .workdir()
        .ok_or_else(|| iot::Error::new(iot::ErrorKind::NotFound, "No workdir for repository"))?;
    let workspace = WorkspaceFs::new(repo_root.to_path_buf());
    load_review_baseline_with_workspace(&workspace)
}

pub fn load_review_baseline_with_workspace(
    workspace: &dyn Workspace,
) -> iot::Result<ReviewBaseline> {
    let path = Path::new(REVIEW_BASELINE_FILE);
    if !workspace.exists(path) {
        return Ok(ReviewBaseline::NotSet);
    }

    let content = workspace.read(path)?;
    let raw = content.trim();

    if raw.is_empty() || raw == BASELINE_NOT_SET {
        return Ok(ReviewBaseline::NotSet);
    }

    let oid = git2::Oid::from_str(raw).map_err(|_| {
        iot::Error::new(
            iot::ErrorKind::InvalidData,
            format!("Invalid baseline OID in {REVIEW_BASELINE_FILE}: '{raw}'"),
        )
    })?;

    Ok(ReviewBaseline::Commit(oid))
}

pub fn update_review_baseline() -> iot::Result<()> {
    let repo = git2::Repository::discover(".").map_err(|e| to_io_error(&e))?;
    let repo_root = repo
        .workdir()
        .ok_or_else(|| iot::Error::new(iot::ErrorKind::NotFound, "No workdir for repository"))?;
    let workspace = WorkspaceFs::new(repo_root.to_path_buf());
    update_review_baseline_with_workspace(&workspace)
}

pub fn update_review_baseline_with_workspace(workspace: &dyn Workspace) -> iot::Result<()> {
    let path = Path::new(REVIEW_BASELINE_FILE);
    match get_current_head_oid() {
        Ok(oid) => workspace.write(path, oid.trim()),
        Err(e) if e.kind() == iot::ErrorKind::NotFound => workspace.write(path, BASELINE_NOT_SET),
        Err(e) => Err(e),
    }
}

pub fn get_review_baseline_info() -> iot::Result<(Option<String>, usize, bool)> {
    let repo = git2::Repository::discover(".").map_err(|e| to_io_error(&e))?;
    match load_review_baseline()? {
        ReviewBaseline::Commit(oid) => {
            let oid_str = oid.to_string();
            let commits_since = count_commits_since(&repo, &oid_str)?;
            let is_stale = commits_since > 10;
            Ok((Some(oid_str), commits_since, is_stale))
        }
        ReviewBaseline::NotSet => Ok((None, 0, false)),
    }
}

fn count_commits_since(repo: &git2::Repository, baseline_oid: &str) -> iot::Result<usize> {
    let baseline = git2::Oid::from_str(baseline_oid).map_err(|_| {
        iot::Error::new(
            iot::ErrorKind::InvalidInput,
            format!("Invalid baseline OID: {baseline_oid}"),
        )
    })?;

    let head_oid = match repo.head() {
        Ok(head) => head.peel_to_commit().map_err(|e| to_io_error(&e))?.id(),
        Err(ref e) if e.code() == git2::ErrorCode::UnbornBranch => return Ok(0),
        Err(e) => return Err(to_io_error(&e)),
    };

    if let Ok((ahead, _behind)) = repo.graph_ahead_behind(head_oid, baseline) {
        return Ok(ahead);
    }

    revwalk_count_commits(repo, head_oid, baseline)
}

fn to_io_error(err: &git2::Error) -> iot::Error {
    iot::Error::other(err.to_string())
}

// =============================================================================
// Diff Stats (from review_baseline/diff_stats.rs)
// =============================================================================

#[derive(Debug, Clone, Default)]
pub struct DiffStats {
    pub files_changed: usize,
    pub lines_added: usize,
    pub lines_deleted: usize,
    pub changed_files: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct BaselineSummary {
    pub baseline_oid: Option<String>,
    pub commits_since: usize,
    pub is_stale: bool,
    pub diff_stats: DiffStats,
}

impl BaselineSummary {
    pub fn format_compact(&self) -> String {
        self.baseline_oid.as_ref().map_or_else(
            || {
                format!(
                    "Baseline: start_commit ({} files: +{}/-{} lines)",
                    self.diff_stats.files_changed,
                    self.diff_stats.lines_added,
                    self.diff_stats.lines_deleted
                )
            },
            |oid| {
                let short_oid = &oid[..8.min(oid.len())];
                if self.is_stale {
                    format!(
                        "Baseline: {} (+{} commits since, {} files changed)",
                        short_oid, self.commits_since, self.diff_stats.files_changed
                    )
                } else if self.commits_since > 0 {
                    format!(
                        "Baseline: {} ({} commits since, {} files changed)",
                        short_oid, self.commits_since, self.diff_stats.files_changed
                    )
                } else {
                    format!(
                        "Baseline: {} ({} files: +{}/-{} lines)",
                        short_oid,
                        self.diff_stats.files_changed,
                        self.diff_stats.lines_added,
                        self.diff_stats.lines_deleted
                    )
                }
            },
        )
    }

    pub fn format_detailed(&self) -> String {
        let baseline_info: Vec<String> = match &self.baseline_oid {
            Some(oid) => {
                let short_oid = &oid[..8.min(oid.len())];
                let lines = vec![format!("  Commit: {short_oid}")];
                if self.commits_since > 0 {
                    lines
                        .into_iter()
                        .chain(std::iter::once(format!(
                            "  Commits since baseline: {}",
                            self.commits_since
                        )))
                        .collect()
                } else {
                    lines
                }
            }
            None => vec!["  Commit: start_commit (initial baseline)".to_string()],
        };

        let file_info: Vec<String> = if !self.diff_stats.changed_files.is_empty() {
            let file_lines: Vec<String> = self
                .diff_stats
                .changed_files
                .iter()
                .map(|file| format!("    - {file}"))
                .collect();
            let remaining = self.diff_stats.files_changed - self.diff_stats.changed_files.len();
            let remaining_line = (remaining > 0).then(|| format!("    ... and {remaining} more"));
            std::iter::once(String::new())
                .chain(std::iter::once("  Changed files:".to_string()))
                .chain(file_lines)
                .chain(remaining_line)
                .collect()
        } else {
            Vec::new()
        };

        let stale_warning: Vec<String> = if self.is_stale {
            vec![
                String::new(),
                "  WARNING: Baseline is stale. Consider updating with --reset-start-commit."
                    .to_string(),
            ]
        } else {
            Vec::new()
        };

        let lines: Vec<String> = std::iter::once("Review Baseline Summary:".to_string())
            .chain(std::iter::once("".to_string()))
            .chain(baseline_info)
            .chain(std::iter::once(format!(
                "  Files changed: {}",
                self.diff_stats.files_changed
            )))
            .chain(std::iter::once(format!(
                "  Lines added: {}",
                self.diff_stats.lines_added
            )))
            .chain(std::iter::once(format!(
                "  Lines deleted: {}",
                self.diff_stats.lines_deleted
            )))
            .chain(file_info)
            .chain(stale_warning)
            .collect();

        lines.join("\n")
    }
}

pub fn get_baseline_summary() -> iot::Result<BaselineSummary> {
    let repo = git2::Repository::discover(".").map_err(|e| to_io_error(&e))?;
    get_baseline_summary_impl(&repo, load_review_baseline()?)
}

fn get_baseline_summary_impl(
    repo: &git2::Repository,
    baseline: ReviewBaseline,
) -> iot::Result<BaselineSummary> {
    let baseline_oid = match baseline {
        ReviewBaseline::Commit(oid) => Some(oid.to_string()),
        ReviewBaseline::NotSet => None,
    };

    let commits_since = if let Some(ref oid) = baseline_oid {
        count_commits_since(repo, oid)?
    } else {
        0
    };

    let is_stale = commits_since > 10;

    let diff_stats = get_diff_stats(repo, baseline_oid.as_ref())?;

    Ok(BaselineSummary {
        baseline_oid,
        commits_since,
        is_stale,
        diff_stats,
    })
}

fn count_lines_in_blob(content: &[u8]) -> usize {
    if content.is_empty() {
        return 0;
    }
    content.iter().copied().filter(|&c| c == b'\n').count() + 1
}

fn get_diff_stats(
    repo: &git2::Repository,
    baseline_oid: Option<&String>,
) -> iot::Result<DiffStats> {
    let baseline_tree = match baseline_oid {
        Some(oid) => {
            let oid = git2::Oid::from_str(oid).map_err(|_| {
                iot::Error::new(
                    iot::ErrorKind::InvalidInput,
                    format!("Invalid baseline OID: {oid}"),
                )
            })?;
            let commit = repo.find_commit(oid).map_err(|e| to_io_error(&e))?;
            commit.tree().map_err(|e| to_io_error(&e))?
        }
        None => repo
            .find_tree(git2::Oid::zero())
            .map_err(|e| to_io_error(&e))?,
    };

    let head_tree = match repo.head() {
        Ok(head) => {
            let commit = head.peel_to_commit().map_err(|e| to_io_error(&e))?;
            commit.tree().map_err(|e| to_io_error(&e))?
        }
        Err(_) => repo
            .find_tree(git2::Oid::zero())
            .map_err(|e| to_io_error(&e))?,
    };

    let diff = repo
        .diff_tree_to_tree(Some(&baseline_tree), Some(&head_tree), None)
        .map_err(|e| to_io_error(&e))?;

    #[derive(Debug, Clone)]
    struct DeltaInfo {
        path: Option<String>,
        is_added_or_modified: bool,
        blob_id: git2::Oid,
    }

    let deltas: Vec<DeltaInfo> = diff
        .deltas()
        .filter_map(|delta| {
            use git2::Delta;

            let path = delta
                .new_file()
                .path()
                .or(delta.old_file().path())
                .map(|p: &std::path::Path| p.to_string_lossy().to_string());

            let (is_new_or_modified, blob_id) = match delta.status() {
                Delta::Added | Delta::Modified => (true, delta.new_file().id()),
                Delta::Deleted => (false, delta.old_file().id()),
                _ => return None,
            };

            Some(DeltaInfo {
                path,
                is_added_or_modified: is_new_or_modified,
                blob_id,
            })
        })
        .collect();

    let files_changed = deltas.len();
    let changed_files: Vec<String> = deltas
        .iter()
        .filter_map(|d| d.path.clone())
        .take(10)
        .collect();

    let (lines_added, lines_deleted) = deltas
        .iter()
        .filter_map(|d| {
            repo.find_blob(d.blob_id)
                .ok()
                .map(|blob| (d.is_added_or_modified, count_lines_in_blob(blob.content())))
        })
        .fold((0usize, 0usize), |(add, del), (is_new, count)| {
            if is_new {
                (add.saturating_add(count), del)
            } else {
                (add, del.saturating_add(count))
            }
        });

    let stats = DiffStats {
        files_changed,
        lines_added,
        lines_deleted,
        changed_files,
    };

    Ok(stats)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_review_baseline_file_path_defined() {
        assert_eq!(REVIEW_BASELINE_FILE, ".agent/review_baseline.txt");
    }

    #[test]
    fn test_load_review_baseline_with_workspace_not_set() {
        use crate::workspace::MemoryWorkspace;

        let workspace = MemoryWorkspace::new_test();

        let result = load_review_baseline_with_workspace(&workspace).unwrap();
        assert_eq!(result, ReviewBaseline::NotSet);
    }

    #[test]
    fn test_load_review_baseline_with_workspace_sentinel() {
        use crate::workspace::MemoryWorkspace;

        let workspace =
            MemoryWorkspace::new_test().with_file(".agent/review_baseline.txt", BASELINE_NOT_SET);

        let result = load_review_baseline_with_workspace(&workspace).unwrap();
        assert_eq!(result, ReviewBaseline::NotSet);
    }

    #[test]
    fn test_load_review_baseline_with_workspace_empty() {
        use crate::workspace::MemoryWorkspace;

        let workspace = MemoryWorkspace::new_test().with_file(".agent/review_baseline.txt", "");

        let result = load_review_baseline_with_workspace(&workspace).unwrap();
        assert_eq!(result, ReviewBaseline::NotSet);
    }

    #[test]
    fn test_load_review_baseline_with_workspace_valid_oid() {
        use crate::workspace::MemoryWorkspace;

        let workspace = MemoryWorkspace::new_test().with_file(
            ".agent/review_baseline.txt",
            "abcd1234abcd1234abcd1234abcd1234abcd1234",
        );

        let result = load_review_baseline_with_workspace(&workspace).unwrap();
        let expected_oid = git2::Oid::from_str("abcd1234abcd1234abcd1234abcd1234abcd1234").unwrap();
        assert_eq!(result, ReviewBaseline::Commit(expected_oid));
    }

    #[test]
    fn test_load_review_baseline_with_workspace_invalid_oid() {
        use crate::workspace::MemoryWorkspace;

        let workspace =
            MemoryWorkspace::new_test().with_file(".agent/review_baseline.txt", "invalid");

        let result = load_review_baseline_with_workspace(&workspace);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().kind(), iot::ErrorKind::InvalidData);
    }
}