//! The Review Loop's end report: one markdown page that says why the run //! stopped and what every round found, fixed, committed and tested. Pure, like //! `loop_driver::summary_markdown()`: the driver hands it the run record and //! writes the text where it goes (the page, the report list and, for a spec //! launch, the spec folder). use crate::review_loop::{BatchRecord, Mode, ReviewRun, ReviewScope, RoundRecord, Shape, Squash}; use crate::round_logic::{Finding, StopReason, Verdict}; /// The stop reason in the words the report and the end screen lead with. pub fn reason_words(reason: Option<&StopReason>) -> &'static str { match reason { Some(StopReason::Clean) => "Clean", Some(StopReason::Cap) => "Stopped at the round cap", Some(StopReason::Person) => "Stopped by you", Some(StopReason::Failed(_)) => "Failed", None => "Not finished", } } /// A commit id as people read it: the first seven characters. pub fn short_id(id: &str) -> &str { id.get(..7).unwrap_or(id) } /// `title (file:line)`, leaving out what a finding does not name. pub fn finding_text(finding: &Finding) -> String { let title = one_line(&finding.title); match (finding.file.is_empty(), finding.line) { (true, _) => title, (false, None) => format!("{title} (`{}`)", finding.file), (false, Some(line)) => format!("{title} (`{}:{line}`)", finding.file), } } fn one_line(text: &str) -> String { text.split_whitespace().collect::>().join(" ") } fn plural(n: usize, one: &str, many: &str) -> String { if n == 1 { format!("1 {one}") } else { format!("{n} {many}") } } fn scope_words(run: &ReviewRun) -> String { match (run.scope, run.setup.spec.as_deref()) { (ReviewScope::Spec, Some(spec)) => format!("the spec {spec}"), (ReviewScope::Spec, None) => "a spec".to_string(), (ReviewScope::Branch, _) => "the branch against its base".to_string(), (ReviewScope::Uncommitted, _) => "the uncommitted changes".to_string(), } } fn verdict_words(verdict: Option<&Verdict>, critical: u32, warning: u32) -> String { match verdict { Some(Verdict::Clean) => "clean".to_string(), Some(Verdict::Findings { .. }) => format!("{critical} Critical, {warning} Warning"), Some(Verdict::Failed(reason)) => format!("did not finish ({})", one_line(reason)), None => "not checked".to_string(), } } fn strength_words(round: &RoundRecord) -> String { let word = match round.strength.as_deref() { Some(word) if !word.is_empty() => word.to_string(), _ => "as launched".to_string(), }; let model = match (round.model.as_deref(), round.effort.as_deref()) { (Some(model), Some(effort)) => Some(format!("{model}, {effort}")), (Some(model), None) => Some(model.to_string()), _ => None, }; match model { Some(model) => format!("{word} ({model})"), None => word, } } fn tests_words(round: &RoundRecord) -> String { match &round.tests { None => "not reported".to_string(), Some(t) => { let result = if !t.ran { "did not run" } else if t.passed { "passed" } else { "failed" }; let summary = one_line(&t.summary); if summary.is_empty() { result.to_string() } else { format!("{summary} ({result})") } } } } fn commit_words(round_commit: Option<&str>, mode: Mode) -> String { match (round_commit, mode) { (Some(id), _) => format!("`{}`", short_id(id)), (None, Mode::Snapshot) => "not committed (snapshot mode)".to_string(), (None, Mode::Commit) => "not committed (nothing changed)".to_string(), } } fn round_section(md: &mut String, round: &RoundRecord, mode: Mode) { md.push_str(&format!("\n## Round {}\n\n", round.n)); md.push_str(&format!( "- Found: {} Critical, {} Warning, {} Suggestion ({})\n", round.critical, round.warning, round.suggestion, verdict_words(round.verdict.as_ref(), round.critical, round.warning) )); if round.fixed.is_empty() { md.push_str("- Fixed: nothing\n"); } else { let fixed: Vec = round.fixed.iter().map(finding_text).collect(); md.push_str(&format!("- Fixed: {}\n", fixed.join("; "))); } if !round.unfixed.is_empty() { let open: Vec = round.unfixed.iter().map(finding_text).collect(); md.push_str(&format!("- Left open: {}\n", open.join("; "))); } md.push_str(&format!( "- Commit: {}\n", commit_words(round.commit.as_deref(), mode) )); md.push_str(&format!("- Targeted tests: {}\n", tests_words(round))); let time = match (round.started_at.is_empty(), round.ended_at.is_empty()) { (false, false) => format!("{} to {}", round.started_at, round.ended_at), (false, true) => format!("started {}", round.started_at), _ => "not recorded".to_string(), }; md.push_str(&format!("- Time: {time}\n")); md.push_str(&format!("- Strength: {}\n", strength_words(round))); if round.retried { if round.could_not_step_up { md.push_str( "- Retry: one fresh session after a failure; it could not step up, so it ran at the same level\n", ); } else { md.push_str("- Retry: one fresh session after a failure, one strength up\n"); } } if !round.summary.trim().is_empty() { md.push_str(&format!("- Summary: {}\n", one_line(&round.summary))); } } fn batch_section(md: &mut String, batch: &BatchRecord, mode: Mode, title: &str) { md.push_str(&format!("\n## {title}\n\n")); md.push_str(&format!( "- Briefed with {}\n", plural(batch.briefed.len(), "Suggestion", "Suggestions") )); md.push_str(&format!( "- Commit: {}\n", commit_words(batch.commit.as_deref(), mode) )); match &batch.final_check { Some(verdict) => md.push_str(&format!( "- Final check by a fresh reviewer: {}\n", verdict_words(Some(verdict), batch.final_critical, batch.final_warning) )), None => md.push_str("- Final check by a fresh reviewer: not run\n"), } if !batch.summary.trim().is_empty() { md.push_str(&format!("- Summary: {}\n", one_line(&batch.summary))); } } /// The whole report for `run`, as it stands: after a stop, and again after /// sign-off with what was picked and whether the squash ran. pub fn review_report_markdown(run: &ReviewRun) -> String { let reason = run.stop_reason.as_ref(); let mut md = format!("# Review Loop: {}\n\n", reason_words(reason)); if let Some(StopReason::Failed(why)) = reason { md.push_str(&format!("Why it failed: {}\n\n", one_line(why))); } let rounds_run = run.rounds.len(); md.push_str(&format!( "- Rounds run: {} of at most {}\n", rounds_run, run.cap )); md.push_str(&format!("- Reviewed: {}\n", scope_words(run))); md.push_str(&format!( "- Mode: {}\n", match run.mode { Mode::Commit => "each round committed", Mode::Snapshot => "snapshots only, nothing committed", } )); md.push_str(&format!( "- Shape: {}\n", match run.shape { Shape::OneSession => "one session per round, with a helper reviewing", Shape::TwoSession => "two sessions per round, a reviewer and then a fixer", } )); if let Some(id) = &run.pre_run_commit { md.push_str(&format!( "- Work before the run was committed first: `{}`\n", short_id(id) )); } if let Some(evidence) = run .stop_evidence .as_deref() .filter(|e| !e.trim().is_empty()) { md.push_str(&format!("- Evidence: {}\n", one_line(evidence))); } for round in &run.rounds { round_section(&mut md, round, run.mode); } for batch in run .earlier_batches .iter() .chain(run.suggestions_batch.iter()) { batch_section(&mut md, batch, run.mode, "Suggestions batch"); } md.push_str("\n## Still open\n\n"); let open = run.open_findings(); if open.is_empty() { md.push_str("No Critical or Warning findings are open.\n"); } else { md.push_str("Critical or Warning findings no round fixed:\n\n"); for finding in &open { md.push_str(&format!("- {}\n", finding_text(finding))); } } md.push_str("\n## Suggestions\n\n"); match &run.signoff { Some(signoff) => { if signoff.picked.is_empty() { md.push_str("Nothing was picked at sign-off.\n"); } else { md.push_str("Picked at sign-off:\n\n"); for finding in &signoff.picked { md.push_str(&format!("- {}\n", finding_text(finding))); } } if let Some(problem) = &signoff.problem { md.push_str(&format!("\nNot all were fixed: {}\n", one_line(problem))); } if !signoff.left.is_empty() { md.push_str("\nLeft alone:\n\n"); for finding in &signoff.left { md.push_str(&format!("- {}\n", finding_text(finding))); } } } None => { let applied = run .earlier_batches .iter() .chain(run.suggestions_batch.iter()); let applied_count: usize = applied.map(|batch| batch.briefed.len()).sum(); if applied_count > 0 { md.push_str(&format!( "{} fixed in the batch above, with its final check.\n", plural(applied_count, "Suggestion was", "Suggestions were") )); } if run.collected.is_empty() { if applied_count == 0 { md.push_str("No Suggestions were collected.\n"); } } else { md.push_str("Waiting for you to pick at sign-off:\n\n"); for finding in &run.collected { md.push_str(&format!("- {}\n", finding_text(finding))); } } } } if let Some(signoff) = &run.signoff { md.push_str("\n## Sign-off\n\n"); let squash = match (&signoff.squash, signoff.squash_asked) { (Squash::Done { commit, commits }, _) => format!( "squashed {} into `{}`", plural(*commits, "commit", "commits"), short_id(commit) ), (Squash::Refused(why), _) => format!("not squashed: {}", one_line(why)), (Squash::Off, _) => "not asked for".to_string(), }; md.push_str(&format!("- Squash of the round commits: {squash}\n")); } if let Some(partial) = run.partial.as_ref().filter(|p| p.pending) { md.push_str(&format!( "\n## Partial changes\n\nStop now left {} changed (+{} -{}) that are not committed, waiting for Keep or Discard.\n", plural(partial.files as usize, "file", "files"), partial.added, partial.removed )); } md } #[cfg(test)] mod tests { use super::*; use crate::loop_driver::tests::fake_agent_launch; use crate::review_loop::{ReviewSetup, SignOff}; use crate::round_boundary::Boundary; use crate::round_logic::RoundTests; use std::collections::BTreeMap; use std::path::PathBuf; fn run_with(reason: Option) -> ReviewRun { let mut run = ReviewRun::new( PathBuf::from("."), ReviewSetup::default(), fake_agent_launch(), Mode::Commit, Shape::OneSession, Boundary::Commit("0000000000000000000000000000000000000000".to_string()), PathBuf::new(), BTreeMap::new(), ); run.stop_reason = reason; run.running = false; run } fn finding(title: &str, file: &str, line: Option) -> Finding { Finding { title: title.to_string(), file: file.to_string(), line, } } fn round(n: u32, critical: u32, warning: u32) -> RoundRecord { RoundRecord { n, critical, warning, suggestion: 1, verdict: Some(if critical + warning == 0 { Verdict::Clean } else { Verdict::Findings { critical, warning } }), commit: Some("1234567890abcdef".to_string()), started_at: "2026-11-28 10:00".to_string(), ended_at: "2026-11-28 10:09".to_string(), tests: Some(RoundTests { ran: true, passed: true, summary: "14 pass".to_string(), }), ..RoundRecord::default() } } #[test] fn every_stop_reason_leads_the_report_in_words() { let cases = [ (Some(StopReason::Clean), "# Review Loop: Clean"), ( Some(StopReason::Cap), "# Review Loop: Stopped at the round cap", ), (Some(StopReason::Person), "# Review Loop: Stopped by you"), ( Some(StopReason::Failed("tests failed".to_string())), "# Review Loop: Failed", ), ]; for (reason, heading) in cases { let md = review_report_markdown(&run_with(reason)); assert!(md.starts_with(heading), "{md}"); } let failed = review_report_markdown(&run_with(Some(StopReason::Failed( "tests failed".to_string(), )))); assert!(failed.contains("Why it failed: tests failed"), "{failed}"); let clean = review_report_markdown(&run_with(Some(StopReason::Clean))); assert!(clean.contains("Rounds run: 0 of at most 3"), "{clean}"); } #[test] fn a_round_says_its_findings_fixes_commit_tests_time_and_strength() { let mut run = run_with(Some(StopReason::Cap)); let mut first = round(1, 2, 1); first.fixed = vec![finding("Close the handle", "src/a.rs", Some(12))]; first.strength = Some("Balanced".to_string()); first.model = Some("sonnet".to_string()); first.effort = Some("low".to_string()); run.rounds.push(first); let md = review_report_markdown(&run); assert!(md.contains("## Round 1"), "{md}"); assert!( md.contains("Found: 2 Critical, 1 Warning, 1 Suggestion"), "{md}" ); assert!( md.contains("Fixed: Close the handle (`src/a.rs:12`)"), "{md}" ); assert!(md.contains("Commit: `1234567`"), "{md}"); assert!(md.contains("Targeted tests: 14 pass (passed)"), "{md}"); assert!( md.contains("Time: 2026-11-28 10:00 to 2026-11-28 10:09"), "{md}" ); assert!(md.contains("Strength: Balanced (sonnet, low)"), "{md}"); } #[test] fn an_agent_without_strengths_reads_as_launched_and_a_same_level_retry_says_so() { let mut run = run_with(Some(StopReason::Cap)); let mut r = round(1, 1, 0); r.retried = true; r.could_not_step_up = true; run.rounds.push(r); let md = review_report_markdown(&run); assert!(md.contains("Strength: as launched"), "{md}"); assert!( md.contains("it could not step up, so it ran at the same level"), "{md}" ); let mut stepped = run_with(Some(StopReason::Cap)); let mut r = round(1, 1, 0); r.retried = true; r.strength = Some("Best".to_string()); stepped.rounds.push(r); let md = review_report_markdown(&stepped); assert!(md.contains("one strength up"), "{md}"); assert!(!md.contains("could not step up"), "{md}"); } #[test] fn open_findings_show_their_file_and_line_and_a_clean_run_has_none() { let mut run = run_with(Some(StopReason::Cap)); let mut last = round(2, 1, 1); last.unfixed = vec![ finding("Missing check", "src/b.rs", Some(40)), finding("Wrong default", "src/c.rs", None), ]; run.rounds.push(last); let md = review_report_markdown(&run); assert!(md.contains("- Missing check (`src/b.rs:40`)"), "{md}"); assert!(md.contains("- Wrong default (`src/c.rs`)"), "{md}"); let mut clean = run_with(Some(StopReason::Clean)); clean.rounds.push(round(1, 0, 0)); let md = review_report_markdown(&clean); assert!( md.contains("No Critical or Warning findings are open."), "{md}" ); } #[test] fn suggestions_are_listed_to_pick_or_reported_as_applied_with_the_final_check() { let mut waiting = run_with(Some(StopReason::Clean)); waiting.collected = vec![finding("Name the timer", "src/d.rs", Some(3))]; let md = review_report_markdown(&waiting); assert!(md.contains("Waiting for you to pick at sign-off"), "{md}"); assert!(md.contains("- Name the timer (`src/d.rs:3`)"), "{md}"); let mut applied = run_with(Some(StopReason::Clean)); applied.suggestions_batch = Some(BatchRecord { briefed: vec![finding("Name the timer", "src/d.rs", None)], commit: Some("abcdef0123456789".to_string()), final_check: Some(Verdict::Clean), ..BatchRecord::default() }); let md = review_report_markdown(&applied); assert!( md.contains("1 Suggestion was fixed in the batch above"), "{md}" ); assert!( md.contains("Final check by a fresh reviewer: clean"), "{md}" ); assert!(md.contains("Commit: `abcdef0`"), "{md}"); } #[test] fn the_sign_off_records_the_picks_and_whether_the_squash_ran_or_was_refused() { let mut run = run_with(Some(StopReason::Cap)); run.signoff = Some(SignOff { picked: vec![finding("Name the timer", "src/d.rs", None)], left: vec![finding("Tidy import", "src/e.rs", None)], squash_asked: true, squash: Squash::Done { commit: "fedcba9876543210".to_string(), commits: 3, }, problem: None, }); let md = review_report_markdown(&run); assert!(md.contains("Picked at sign-off"), "{md}"); assert!(md.contains("Left alone"), "{md}"); assert!(md.contains("squashed 3 commits into `fedcba9`"), "{md}"); run.signoff.as_mut().unwrap().squash = Squash::Refused( "Something else was committed during the run, so the rounds were not squashed." .to_string(), ); let md = review_report_markdown(&run); assert!( md.contains("not squashed: Something else was committed"), "{md}" ); } }