use std::path::PathBuf;
use agent_harness_adapter_core::{Scope, State};
use super::harness::{HarnessDirs, installed_status, shown, tool};
use crate::{
Finding, RepoPath, RepoRoot,
harness::unvouched,
report::{ABSENT_PART, STALE_PART, UNVOUCHED_PART},
};
pub(crate) fn stale_parts(root: &RepoRoot, home: Option<PathBuf>) -> Vec<Finding> {
let dirs = HarnessDirs {
repo: Some(root.clone()),
home,
};
let Ok(tool) = tool(&dirs, Scope::Project) else {
return Vec::new();
};
let mut out = Vec::new();
for scope in [Scope::Project, Scope::Local] {
let Ok(Some(found)) = installed_status(&tool, scope) else {
continue;
};
let local = if scope == Scope::Local {
" --scope local"
} else {
""
};
let doubtful = unvouched(&found);
for h in &found.harnesses {
for p in &h.parts {
let Some(path) = RepoPath::parse(&shown(scope, &p.path)) else {
continue;
};
let install = format!("sokf harness install {}{local}", h.harness);
let (key, message) = match p.state {
State::Stale => (
STALE_PART,
format!(
"the {} part of the {} harness is stale: nobody edited it, and it differs from what sokf now writes; run `{install}` to refresh it",
p.part, h.harness
),
),
State::Absent => (
ABSENT_PART,
format!(
"the {} part of the {} harness is absent; run `{install}` to write it",
p.part, h.harness
),
),
State::Edited => {
let Some(u) = doubtful
.iter()
.find(|u| u.harness == h.harness && u.part == p.part)
else {
continue;
};
let check = format!("check `sokf harness diff {}{local}`", h.harness);
let next = if u.older() {
format!(
"{check}, then run `{install} --force {}` unless the difference holds an edit of the project's",
p.part
)
} else {
check
};
(
UNVOUCHED_PART,
format!(
"the {} part of the {} harness may not be an edit, as {}; {next}",
p.part,
h.harness,
u.cause()
),
)
}
_ => continue,
};
out.push(Finding::warning(path, None, key, message));
}
}
}
out
}