use std::{fmt::Write as _, path::PathBuf};
use agent_harness_adapter_core::{Scope, State};
use super::harness::{HarnessDirs, installed_status, shown, tool};
use crate::{
Finding, RepoPath, RepoRoot,
report::{ABSENT_PART, STALE_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 {
""
};
for h in &found.harnesses {
for p in &h.parts {
let Some(path) = RepoPath::parse(&shown(scope, &p.path)) else {
continue;
};
let (key, mut message, verb) = 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; ",
p.part, h.harness
),
"refresh",
),
State::Absent => (
ABSENT_PART,
format!(
"the {} part of the {} harness is absent; ",
p.part, h.harness
),
"write",
),
_ => continue,
};
let _ = write!(
message,
"run `sokf harness install {}{local}` to {verb} it",
h.harness
);
out.push(Finding::warning(path, None, key, message));
}
}
}
out
}