use std::path::PathBuf;
use agent_harness_adapter_core::{Item, Scope, State};
use super::{
harness::{HarnessDirs, installed_status, shown, tool},
harness_packs::conflicted_fragments,
};
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 conflicted: Option<Vec<String>> = None;
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 conflicts = if p.part == Item::Instructions.as_str() {
conflicted
.get_or_insert_with(|| conflicted_fragments(root).unwrap_or_default())
.as_slice()
} else {
&[]
};
if !conflicts.is_empty() {
message.push_str(&format!(
"resolve the conflict in {} first, then ",
conflicts.join(", ")
));
}
message.push_str(&format!(
"run `sokf harness install {}{local}` to {verb} it",
h.harness
));
out.push(Finding::warning(path, None, key, message));
}
}
}
out
}