use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use vitrify_core::CoreError;
use vitrify_core::cascade::{self, Candidate, Closures, Owner, Ownership};
use vitrify_core::facf::{self, Hypothesis, Search, Verdict};
use vitrify_core::graph::Node;
use super::blame::{Prepare, Runner};
use crate::git::Git;
pub fn probe_checkout(worktrees: &Path, slug: &str) -> PathBuf {
worktrees.join(format!("probe-{slug}"))
}
#[derive(Debug, Clone)]
pub struct Rotten<'a> {
pub node: &'a Node,
pub row_number: usize,
pub row: &'a str,
pub anchor: &'a str,
pub base: &'a str,
}
pub struct Probe<'a> {
pub git: &'a Git,
pub ownership: &'a Ownership,
pub closures: &'a Closures,
pub setup: &'a [String],
pub prepare: &'a dyn Prepare,
pub runner: &'a dyn Runner,
pub config: facf::Config,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Culprit {
pub sha: String,
pub short: String,
pub subject: String,
pub upstream: Vec<Node>,
pub integrated: bool,
pub probability: f64,
}
impl Culprit {
pub fn describe(&self) -> String {
let units = self
.upstream
.iter()
.map(|n| n.to_string())
.collect::<Vec<_>>()
.join(", ");
match (self.upstream.is_empty(), self.integrated) {
(true, _) => format!("\"{}\"", self.subject),
(false, true) => format!("{units} integrated"),
(false, false) => format!("changed {units}"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Finding {
Culprit(Culprit),
Flake { probability: f64 },
Inconclusive {
best: Option<Culprit>,
probability: f64,
},
Interrupted,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Report {
pub node: Node,
pub row_number: usize,
pub row: String,
pub finding: Finding,
pub candidates: usize,
pub touched: usize,
pub probes: usize,
pub notes: Vec<String>,
}
impl Report {
pub fn line(&self) -> String {
format!("{} {}", self.node, self.finding_line())
}
pub fn finding_line(&self) -> String {
let who = format!("row {}", self.row_number);
match &self.finding {
Finding::Culprit(c) => {
format!(
"{who} broke at {} ({}) — culprit found",
c.short,
c.describe()
)
}
Finding::Flake { .. } => format!(
"{who} is flaky: no commit since its last check broke it — fix the row (`{}`); the unit is re-checked again",
self.row
),
Finding::Inconclusive { best, .. } => match best {
Some(c) => format!(
"{who} broke, culprit not found after {} probes (most likely {}, {}) — inconclusive",
self.probes,
c.short,
c.describe()
),
None if self.candidates == 0 && !self.notes.is_empty() => format!(
"{who} broke, culprit not searched ({}) — inconclusive",
self.notes.join("; ")
),
None if self.candidates == 0 => format!(
"{who} fails at its own last check — no commit to search — inconclusive"
),
None => format!(
"{who} broke, culprit not found after {} probes (most likely flaky) — inconclusive",
self.probes
),
},
Finding::Interrupted => format!("{who}: culprit search interrupted"),
}
}
}
pub fn find(rotten: &Rotten<'_>, probe: &Probe<'_>) -> Result<Report, CoreError> {
let git = probe.git;
let mut report = Report {
node: rotten.node.clone(),
row_number: rotten.row_number,
row: rotten.row.to_string(),
finding: Finding::Inconclusive {
best: None,
probability: 0.0,
},
candidates: 0,
touched: 0,
probes: 0,
notes: Vec::new(),
};
let Some(anchor) = git.resolve_commit(rotten.anchor).map_err(CoreError::from)? else {
report.notes.push(format!(
"its recorded anchor {} names no commit here",
rotten.anchor
));
return Ok(report);
};
let base = git.rev_parse(rotten.base).map_err(CoreError::from)?;
let commits = git
.first_parent_range(&anchor, &base)
.map_err(CoreError::from)?;
report.candidates = commits.len();
if commits.is_empty() {
return Ok(report);
}
let closure: BTreeSet<Node> = probe.closures.get(rotten.node).cloned().unwrap_or_default();
let closures: Closures = [(rotten.node.clone(), closure)].into();
let mut diffs: Vec<Vec<String>> = Vec::with_capacity(commits.len());
let mut touched: Vec<bool> = Vec::with_capacity(commits.len());
for c in &commits {
let parent = format!("{c}^1");
let paths = git.changed_paths(&parent, c).map_err(CoreError::from)?;
let changes: cascade::Changes = [(parent.clone(), paths.clone())].into();
let cand = [Candidate {
node: rotten.node.clone(),
anchor: Some(parent),
}];
touched.push(!cascade::dirty_set(&cand, &changes, &closures, probe.ownership).is_empty());
diffs.push(paths);
}
report.touched = touched.iter().filter(|&&t| t).count();
let mut search = Search::from_touched(&touched, &probe.config)
.map_err(|e| CoreError::refusal(1, format!("error: culprit search: {e}")))?;
let mut interrupted = false;
while let Some(version) = search.next_probe() {
let commit = &commits[version];
let dir = probe.prepare.prepare(commit)?;
let Some(passed) = run_probe(probe, &dir, rotten.row, commit, &mut report.notes) else {
interrupted = true;
break;
};
search
.observe(version, passed)
.map_err(|e| CoreError::refusal(1, format!("error: culprit search: {e}")))?;
}
report.probes = search.probes_used();
probe.prepare.prepare(&base)?;
let culprit = |index: usize, probability: f64| -> Result<Culprit, CoreError> {
describe(
git,
probe.ownership,
&commits[index],
&diffs[index],
probability,
)
};
report.finding = if interrupted {
Finding::Interrupted
} else {
match search.verdict() {
Verdict::Culprit { index, probability } => {
Finding::Culprit(culprit(index, probability)?)
}
Verdict::Flake { probability } => Finding::Flake { probability },
Verdict::Inconclusive { best, probability } => Finding::Inconclusive {
best: match best {
Hypothesis::Commit(index) => Some(culprit(index, probability)?),
Hypothesis::NoCulprit => None,
},
probability,
},
Verdict::Searching => Finding::Inconclusive {
best: None,
probability: 0.0,
},
}
};
Ok(report)
}
fn run_probe(
probe: &Probe<'_>,
dir: &Path,
row: &str,
commit: &str,
notes: &mut Vec<String>,
) -> Option<bool> {
for cmd in probe.setup {
let ran = probe.runner.run(cmd, dir);
if ran.interrupted {
return None;
}
if !ran.ok() {
notes.push(format!(
"`{cmd}` failed at {} (exit {}): counted as a failure of the row",
&commit[..7.min(commit.len())],
ran.code
));
return Some(false);
}
}
let ran = probe.runner.run(row, dir);
if ran.interrupted {
return None;
}
Some(ran.ok())
}
fn describe(
git: &Git,
ownership: &Ownership,
sha: &str,
paths: &[String],
probability: f64,
) -> Result<Culprit, CoreError> {
let subject = git.subject(sha).map_err(CoreError::from)?;
let short = git.short_sha(sha).map_err(CoreError::from)?;
let mut owners: BTreeSet<Node> = BTreeSet::new();
let mut spec_slugs: BTreeSet<String> = BTreeSet::new();
for p in paths {
match ownership.classify(p) {
Owner::Unit(n) | Owner::Test { unit: Some(n), .. } => {
owners.insert(n);
}
Owner::Spec(slug) => {
spec_slugs.insert(slug);
}
_ => {}
}
}
let integrated = integrated_units(&subject);
let (upstream, from_message) = if integrated.is_empty() {
(owners.iter().cloned().collect::<Vec<_>>(), false)
} else {
let slugs: BTreeSet<String> = owners
.iter()
.map(|n| n.slug.clone())
.chain(spec_slugs)
.collect();
let mut nodes: Vec<Node> = Vec::new();
for unit in &integrated {
let mut found = false;
for n in owners.iter().filter(|n| &n.unit == unit) {
nodes.push(n.clone());
found = true;
}
if !found
&& slugs.len() == 1
&& let Some(slug) = slugs.first()
{
nodes.push(Node::new(slug.as_str(), unit.as_str()));
}
}
nodes.sort();
nodes.dedup();
if nodes.is_empty() {
(owners.into_iter().collect(), false)
} else {
(nodes, true)
}
};
Ok(Culprit {
sha: sha.to_string(),
short,
subject,
upstream,
integrated: from_message,
probability,
})
}
fn integrated_units(subject: &str) -> Vec<String> {
let Some(rest) = subject
.strip_prefix("sdd: integrate ")
.or_else(|| subject.strip_prefix("sdd: verify "))
else {
return Vec::new();
};
rest.split(", ")
.filter_map(|part| part.split_whitespace().next())
.filter(|id| !id.is_empty())
.map(str::to_string)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn integration_subjects_name_their_units() {
assert_eq!(integrated_units("sdd: integrate U-004 (c3)"), vec!["U-004"]);
assert_eq!(
integrated_units("sdd: verify U-001 (c1), U-002 (c4)"),
vec!["U-001", "U-002"]
);
assert!(integrated_units("fix the parser").is_empty());
}
#[test]
fn a_culprit_is_described_by_what_it_integrated_or_changed() {
let mut c = Culprit {
sha: "a1b2c3d4".into(),
short: "a1b2c3d".into(),
subject: "sdd: integrate U-004 (c3)".into(),
upstream: vec![Node::new("core", "U-004")],
integrated: true,
probability: 0.97,
};
assert_eq!(c.describe(), "core U-004 integrated");
c.integrated = false;
assert_eq!(c.describe(), "changed core U-004");
c.upstream.clear();
assert_eq!(c.describe(), "\"sdd: integrate U-004 (c3)\"");
let report = Report {
node: Node::new("api", "U-002"),
row_number: 2,
row: "make check".into(),
finding: Finding::Culprit(Culprit {
upstream: vec![Node::new("core", "U-004")],
integrated: true,
..c
}),
candidates: 3,
touched: 1,
probes: 2,
notes: Vec::new(),
};
assert_eq!(
report.line(),
"api U-002 row 2 broke at a1b2c3d (core U-004 integrated) — culprit found"
);
}
}