Skip to main content

ralph_workflow/git_helpers/
review_baseline.rs

1//! Per-review-cycle baseline tracking.
2//!
3//! This module manages the baseline commit for each review cycle, ensuring that
4//! reviewers only see changes from the current cycle rather than cumulative changes
5//! from previous fix commits.
6//!
7//! # Overview
8//!
9//! During the review-fix phase, each cycle should:
10//! 1. Capture baseline before review (current HEAD)
11//! 2. Review sees diff from that baseline
12//! 3. Fixer makes changes
13//! 4. Baseline is updated after fix pass
14//! 5. Next review cycle sees only new changes
15//!
16//! This prevents "diff scope creep" where previous fix commits pollute
17//! subsequent review passes.
18
19use std::fs;
20use std::io;
21use std::path::Path;
22
23#[cfg(any(test, feature = "test-utils"))]
24use crate::workspace::Workspace;
25
26use super::start_commit::get_current_head_oid;
27
28/// Path to the review baseline file.
29///
30/// Stored in `.agent/review_baseline.txt`, this file contains the OID (SHA) of the
31/// commit that serves as the baseline for the current review cycle.
32const REVIEW_BASELINE_FILE: &str = ".agent/review_baseline.txt";
33
34/// Sentinel value when review baseline is not set.
35const BASELINE_NOT_SET: &str = "__BASELINE_NOT_SET__";
36
37/// Review baseline state.
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub enum ReviewBaseline {
40    /// A concrete commit OID to diff from.
41    Commit(git2::Oid),
42    /// Baseline not set (first review cycle).
43    NotSet,
44}
45
46/// Update the review baseline to current HEAD.
47///
48/// This should be called AFTER each fix pass to update the baseline so
49/// the next review cycle sees only new changes.
50///
51/// # Errors
52///
53/// Returns an error if:
54/// - The current HEAD cannot be determined
55/// - The file cannot be written
56///
57/// **Note:** This function uses the current working directory to discover the repo.
58/// For explicit path control, use [`update_review_baseline_at`] instead.
59pub fn update_review_baseline() -> io::Result<()> {
60    let oid = get_current_head_oid()?;
61    write_review_baseline_cwd(&oid)
62}
63
64/// Load the review baseline.
65///
66/// Returns the baseline commit for the current review cycle.
67///
68/// # Errors
69///
70/// Returns an error if:
71/// - The file cannot be read
72/// - The file content is invalid
73///
74pub fn load_review_baseline() -> io::Result<ReviewBaseline> {
75    let path = Path::new(REVIEW_BASELINE_FILE);
76    load_review_baseline_impl(path)
77}
78
79/// Implementation of load_review_baseline.
80fn load_review_baseline_impl(path: &Path) -> io::Result<ReviewBaseline> {
81    if !path.exists() {
82        return Ok(ReviewBaseline::NotSet);
83    }
84
85    let content = fs::read_to_string(path)?;
86    let raw = content.trim();
87
88    if raw.is_empty() || raw == BASELINE_NOT_SET {
89        return Ok(ReviewBaseline::NotSet);
90    }
91
92    // Parse the OID
93    let oid = git2::Oid::from_str(raw).map_err(|_| {
94        io::Error::new(
95            io::ErrorKind::InvalidData,
96            format!("Invalid OID format in {}: '{}'. The review baseline will be reset. Run 'ralph --reset-start-commit' if this persists.", REVIEW_BASELINE_FILE, raw),
97        )
98    })?;
99
100    Ok(ReviewBaseline::Commit(oid))
101}
102
103/// Get information about the current review baseline.
104///
105/// Returns a tuple of (baseline_oid, commits_since_baseline, is_stale).
106/// - `baseline_oid`: The OID of the baseline commit (or None if not set)
107/// - `commits_since_baseline`: Number of commits since baseline
108/// - `is_stale`: true if baseline is old (>10 commits behind)
109///
110pub fn get_review_baseline_info() -> io::Result<(Option<String>, usize, bool)> {
111    let repo = git2::Repository::discover(".").map_err(|e| to_io_error(&e))?;
112    get_review_baseline_info_impl(&repo, load_review_baseline()?)
113}
114
115/// Implementation of get_review_baseline_info.
116fn get_review_baseline_info_impl(
117    repo: &git2::Repository,
118    baseline: ReviewBaseline,
119) -> io::Result<(Option<String>, usize, bool)> {
120    let baseline_oid = match baseline {
121        ReviewBaseline::Commit(oid) => Some(oid.to_string()),
122        ReviewBaseline::NotSet => None,
123    };
124
125    let commits_since = if let Some(ref oid) = baseline_oid {
126        count_commits_since(repo, oid)?
127    } else {
128        0
129    };
130
131    let is_stale = commits_since > 10;
132
133    Ok((baseline_oid, commits_since, is_stale))
134}
135
136/// Write the review baseline to disk (CWD-based, for backward compatibility).
137fn write_review_baseline_cwd(oid: &str) -> io::Result<()> {
138    let path = Path::new(REVIEW_BASELINE_FILE);
139    if let Some(parent) = path.parent() {
140        fs::create_dir_all(parent)?;
141    }
142    fs::write(path, oid)?;
143    Ok(())
144}
145
146/// Load the review baseline using workspace abstraction.
147///
148/// This is the workspace-aware version for pipeline code.
149#[cfg(any(test, feature = "test-utils"))]
150pub fn load_review_baseline_with_workspace(
151    workspace: &dyn Workspace,
152) -> io::Result<ReviewBaseline> {
153    let path = Path::new(REVIEW_BASELINE_FILE);
154
155    if !workspace.exists(path) {
156        return Ok(ReviewBaseline::NotSet);
157    }
158
159    let content = workspace.read(path)?;
160    let raw = content.trim();
161
162    if raw.is_empty() || raw == BASELINE_NOT_SET {
163        return Ok(ReviewBaseline::NotSet);
164    }
165
166    // Parse the OID
167    let oid = git2::Oid::from_str(raw).map_err(|_| {
168        io::Error::new(
169            io::ErrorKind::InvalidData,
170            format!(
171                "Invalid OID format in {}: '{}'. The review baseline will be reset. \
172                 Run 'ralph --reset-start-commit' if this persists.",
173                REVIEW_BASELINE_FILE, raw
174            ),
175        )
176    })?;
177
178    Ok(ReviewBaseline::Commit(oid))
179}
180
181/// Count commits since a given baseline.
182fn count_commits_since(repo: &git2::Repository, baseline_oid: &str) -> io::Result<usize> {
183    let oid = git2::Oid::from_str(baseline_oid).map_err(|_| {
184        io::Error::new(
185            io::ErrorKind::InvalidInput,
186            format!("Invalid baseline OID: {baseline_oid}"),
187        )
188    })?;
189
190    let baseline = repo.find_commit(oid).map_err(|e| to_io_error(&e))?;
191
192    // Try to get HEAD and count commits
193    match repo.head() {
194        Ok(head) => {
195            let head_commit = head.peel_to_commit().map_err(|e| to_io_error(&e))?;
196
197            // Use revwalk to count commits
198            let mut revwalk = repo.revwalk().map_err(|e| to_io_error(&e))?;
199            revwalk
200                .push(head_commit.id())
201                .map_err(|e| to_io_error(&e))?;
202
203            let mut count = 0;
204            for commit_id in revwalk {
205                let commit_id = commit_id.map_err(|e| to_io_error(&e))?;
206                if commit_id == baseline.id() {
207                    break;
208                }
209                count += 1;
210                // Safety limit to prevent infinite loops
211                if count > 1000 {
212                    break;
213                }
214            }
215            Ok(count)
216        }
217        Err(_) => Ok(0),
218    }
219}
220
221/// Diff statistics for the changes since baseline.
222#[derive(Debug, Clone, Default)]
223pub struct DiffStats {
224    /// Number of files changed.
225    pub files_changed: usize,
226    /// Number of lines added.
227    pub lines_added: usize,
228    /// Number of lines deleted.
229    pub lines_deleted: usize,
230    /// List of changed file paths (up to 10 for display).
231    pub changed_files: Vec<String>,
232}
233
234/// Baseline summary information for display.
235#[derive(Debug, Clone)]
236pub struct BaselineSummary {
237    /// The baseline OID (short form).
238    pub baseline_oid: Option<String>,
239    /// Number of commits since baseline.
240    pub commits_since: usize,
241    /// Whether the baseline is stale (>10 commits behind).
242    pub is_stale: bool,
243    /// Diff statistics for changes since baseline.
244    pub diff_stats: DiffStats,
245}
246
247impl BaselineSummary {
248    /// Format a compact version for inline display.
249    pub fn format_compact(&self) -> String {
250        match &self.baseline_oid {
251            Some(oid) => {
252                let short_oid = &oid[..8.min(oid.len())];
253                if self.is_stale {
254                    format!(
255                        "Baseline: {} (+{} commits since, {} files changed)",
256                        short_oid, self.commits_since, self.diff_stats.files_changed
257                    )
258                } else if self.commits_since > 0 {
259                    format!(
260                        "Baseline: {} ({} commits since, {} files changed)",
261                        short_oid, self.commits_since, self.diff_stats.files_changed
262                    )
263                } else {
264                    format!(
265                        "Baseline: {} ({} files: +{}/-{} lines)",
266                        short_oid,
267                        self.diff_stats.files_changed,
268                        self.diff_stats.lines_added,
269                        self.diff_stats.lines_deleted
270                    )
271                }
272            }
273            None => {
274                format!(
275                    "Baseline: start_commit ({} files: +{}/-{} lines)",
276                    self.diff_stats.files_changed,
277                    self.diff_stats.lines_added,
278                    self.diff_stats.lines_deleted
279                )
280            }
281        }
282    }
283
284    /// Format a detailed version for verbose display.
285    pub fn format_detailed(&self) -> String {
286        let mut lines = Vec::new();
287
288        lines.push("Review Baseline Summary:".to_string());
289        lines.push("─".repeat(40));
290
291        match &self.baseline_oid {
292            Some(oid) => {
293                let short_oid = &oid[..8.min(oid.len())];
294                lines.push(format!("  Commit: {}", short_oid));
295                if self.commits_since > 0 {
296                    lines.push(format!("  Commits since baseline: {}", self.commits_since));
297                }
298            }
299            None => {
300                lines.push("  Commit: start_commit (initial baseline)".to_string());
301            }
302        }
303
304        lines.push(format!(
305            "  Files changed: {}",
306            self.diff_stats.files_changed
307        ));
308        lines.push(format!("  Lines added: {}", self.diff_stats.lines_added));
309        lines.push(format!(
310            "  Lines deleted: {}",
311            self.diff_stats.lines_deleted
312        ));
313
314        if !self.diff_stats.changed_files.is_empty() {
315            lines.push(String::new());
316            lines.push("  Changed files:".to_string());
317            for file in &self.diff_stats.changed_files {
318                lines.push(format!("    - {}", file));
319            }
320            if self.diff_stats.changed_files.len() < self.diff_stats.files_changed {
321                let remaining = self.diff_stats.files_changed - self.diff_stats.changed_files.len();
322                lines.push(format!("    ... and {} more", remaining));
323            }
324        }
325
326        if self.is_stale {
327            lines.push(String::new());
328            lines.push(
329                "  ⚠ WARNING: Baseline is stale. Consider updating with --reset-start-commit."
330                    .to_string(),
331            );
332        }
333
334        lines.join("\n")
335    }
336}
337
338/// Get a summary of the baseline state for display.
339///
340/// Returns a `BaselineSummary` containing information about the current
341/// baseline, commits since baseline, staleness, and diff statistics.
342///
343pub fn get_baseline_summary() -> io::Result<BaselineSummary> {
344    let repo = git2::Repository::discover(".").map_err(|e| to_io_error(&e))?;
345    get_baseline_summary_impl(&repo, load_review_baseline()?)
346}
347
348/// Implementation of get_baseline_summary.
349fn get_baseline_summary_impl(
350    repo: &git2::Repository,
351    baseline: ReviewBaseline,
352) -> io::Result<BaselineSummary> {
353    let baseline_oid = match baseline {
354        ReviewBaseline::Commit(oid) => Some(oid.to_string()),
355        ReviewBaseline::NotSet => None,
356    };
357
358    let commits_since = if let Some(ref oid) = baseline_oid {
359        count_commits_since(repo, oid)?
360    } else {
361        0
362    };
363
364    let is_stale = commits_since > 10;
365
366    // Get diff statistics
367    let diff_stats = get_diff_stats(repo, &baseline_oid)?;
368
369    Ok(BaselineSummary {
370        baseline_oid,
371        commits_since,
372        is_stale,
373        diff_stats,
374    })
375}
376
377/// Count lines in a blob content.
378///
379/// Returns the number of lines, matching the behavior of counting
380/// newlines and adding 1 (so empty content returns 0, but any content
381/// returns at least 1).
382fn count_lines_in_blob(content: &[u8]) -> usize {
383    if content.is_empty() {
384        return 0;
385    }
386    // Count newlines and add 1 to get the line count
387    // This matches the previous behavior and ensures that even files
388    // without trailing newlines are counted correctly
389    content.iter().filter(|&&c| c == b'\n').count() + 1
390}
391
392/// Get diff statistics for changes since the baseline.
393fn get_diff_stats(repo: &git2::Repository, baseline_oid: &Option<String>) -> io::Result<DiffStats> {
394    let baseline_tree = match baseline_oid {
395        Some(oid) => {
396            let oid = git2::Oid::from_str(oid).map_err(|_| {
397                io::Error::new(
398                    io::ErrorKind::InvalidInput,
399                    format!("Invalid baseline OID: {}", oid),
400                )
401            })?;
402            let commit = repo.find_commit(oid).map_err(|e| to_io_error(&e))?;
403            commit.tree().map_err(|e| to_io_error(&e))?
404        }
405        None => {
406            // No baseline set, use empty tree
407            repo.find_tree(git2::Oid::zero())
408                .map_err(|e| to_io_error(&e))?
409        }
410    };
411
412    // Get the current HEAD tree
413    let head_tree = match repo.head() {
414        Ok(head) => {
415            let commit = head.peel_to_commit().map_err(|e| to_io_error(&e))?;
416            commit.tree().map_err(|e| to_io_error(&e))?
417        }
418        Err(_) => {
419            // No HEAD yet, use empty tree
420            repo.find_tree(git2::Oid::zero())
421                .map_err(|e| to_io_error(&e))?
422        }
423    };
424
425    // Generate diff
426    let diff = repo
427        .diff_tree_to_tree(Some(&baseline_tree), Some(&head_tree), None)
428        .map_err(|e| to_io_error(&e))?;
429
430    // Collect statistics
431    let mut stats = DiffStats::default();
432    let mut delta_ids = Vec::new();
433
434    diff.foreach(
435        &mut |delta, _progress| {
436            use git2::Delta;
437
438            stats.files_changed += 1;
439
440            if let Some(path) = delta.new_file().path() {
441                let path_str = path.to_string_lossy().to_string();
442                if stats.changed_files.len() < 10 {
443                    stats.changed_files.push(path_str);
444                }
445            } else if let Some(path) = delta.old_file().path() {
446                let path_str = path.to_string_lossy().to_string();
447                if stats.changed_files.len() < 10 {
448                    stats.changed_files.push(path_str);
449                }
450            }
451
452            match delta.status() {
453                Delta::Added => {
454                    delta_ids.push((delta.new_file().id(), true));
455                }
456                Delta::Deleted => {
457                    delta_ids.push((delta.old_file().id(), false));
458                }
459                Delta::Modified => {
460                    delta_ids.push((delta.new_file().id(), true));
461                }
462                _ => {}
463            }
464
465            true
466        },
467        None,
468        None,
469        None,
470    )
471    .map_err(|e| to_io_error(&e))?;
472
473    // Count lines added/deleted
474    for (blob_id, is_new_or_modified) in delta_ids {
475        if let Ok(blob) = repo.find_blob(blob_id) {
476            let line_count = count_lines_in_blob(blob.content());
477
478            if is_new_or_modified {
479                stats.lines_added += line_count;
480            } else {
481                stats.lines_deleted += line_count;
482            }
483        }
484    }
485
486    Ok(stats)
487}
488
489/// Convert git2 error to `io::Error`.
490fn to_io_error(err: &git2::Error) -> io::Error {
491    io::Error::other(err.to_string())
492}
493
494#[cfg(test)]
495mod tests {
496    use super::*;
497
498    #[test]
499    fn test_review_baseline_file_path_defined() {
500        assert_eq!(REVIEW_BASELINE_FILE, ".agent/review_baseline.txt");
501    }
502
503    #[test]
504    fn test_load_review_baseline_returns_result() {
505        let result = load_review_baseline();
506        assert!(result.is_ok() || result.is_err());
507    }
508
509    #[test]
510    fn test_get_review_baseline_info_returns_result() {
511        let result = get_review_baseline_info();
512        assert!(result.is_ok() || result.is_err());
513    }
514
515    // =========================================================================
516    // Workspace-aware function tests
517    // =========================================================================
518
519    #[test]
520    fn test_load_review_baseline_with_workspace_not_set() {
521        use crate::workspace::MemoryWorkspace;
522
523        let workspace = MemoryWorkspace::new_test();
524
525        let result = load_review_baseline_with_workspace(&workspace).unwrap();
526        assert_eq!(result, ReviewBaseline::NotSet);
527    }
528
529    #[test]
530    fn test_load_review_baseline_with_workspace_sentinel() {
531        use crate::workspace::MemoryWorkspace;
532
533        let workspace =
534            MemoryWorkspace::new_test().with_file(".agent/review_baseline.txt", BASELINE_NOT_SET);
535
536        let result = load_review_baseline_with_workspace(&workspace).unwrap();
537        assert_eq!(result, ReviewBaseline::NotSet);
538    }
539
540    #[test]
541    fn test_load_review_baseline_with_workspace_empty() {
542        use crate::workspace::MemoryWorkspace;
543
544        let workspace = MemoryWorkspace::new_test().with_file(".agent/review_baseline.txt", "");
545
546        let result = load_review_baseline_with_workspace(&workspace).unwrap();
547        assert_eq!(result, ReviewBaseline::NotSet);
548    }
549
550    #[test]
551    fn test_load_review_baseline_with_workspace_valid_oid() {
552        use crate::workspace::MemoryWorkspace;
553
554        let workspace = MemoryWorkspace::new_test().with_file(
555            ".agent/review_baseline.txt",
556            "abcd1234abcd1234abcd1234abcd1234abcd1234",
557        );
558
559        let result = load_review_baseline_with_workspace(&workspace).unwrap();
560        let expected_oid = git2::Oid::from_str("abcd1234abcd1234abcd1234abcd1234abcd1234").unwrap();
561        assert_eq!(result, ReviewBaseline::Commit(expected_oid));
562    }
563
564    #[test]
565    fn test_load_review_baseline_with_workspace_invalid_oid() {
566        use crate::workspace::MemoryWorkspace;
567
568        let workspace =
569            MemoryWorkspace::new_test().with_file(".agent/review_baseline.txt", "invalid");
570
571        let result = load_review_baseline_with_workspace(&workspace);
572        assert!(result.is_err());
573        assert_eq!(result.unwrap_err().kind(), io::ErrorKind::InvalidData);
574    }
575}