use agent_harness_adapter_core::{InstallResult, State};
const OLDER: &str = "its record is from an older version";
const CAUSES: [(&str, &str); 4] = [
(OLDER, "an older sokf recorded it"),
(
"its record is from a later version",
"a later sokf recorded it",
),
(
"its record was made under other rules",
"another version of sokf recorded it",
),
(
"its record doesn't fit the part",
"the record of the harnesses doesn't fit it",
),
];
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Unvouched {
pub harness: String,
pub part: String,
pub path: String,
pub reason: String,
pub warning: usize,
}
impl Unvouched {
pub fn cause(&self) -> &str {
CAUSES
.iter()
.find(|(reason, _)| *reason == self.reason)
.map_or(self.reason.as_str(), |(_, cause)| cause)
}
pub fn older(&self) -> bool {
self.reason == OLDER
}
pub fn words(&self, shown: &str) -> String {
format!(
"{}: {} in {shown} may not be an edit, as {}",
self.harness,
self.part,
self.cause()
)
}
}
pub(crate) fn unvouched(result: &InstallResult) -> Vec<Unvouched> {
let mut out = Vec::new();
for h in &result.harnesses {
for p in &h.parts {
if p.state != State::Edited || p.action.is_some() {
continue;
}
let prefix = format!(
"{}: {} in {} may not be an edit, as ",
h.harness, p.part, p.path
);
let found = result
.warnings
.iter()
.enumerate()
.find_map(|(i, w)| w.strip_prefix(&prefix).map(|rest| (i, rest)));
if let Some((warning, rest)) = found {
let reason = rest.split_once("; ").map_or(rest, |(reason, _)| reason);
out.push(Unvouched {
harness: h.harness.clone(),
part: p.part.clone(),
path: p.path.clone(),
reason: reason.to_string(),
warning,
});
}
}
}
out
}
#[cfg(test)]
mod tests {
use std::{fs, path::Path};
use agent_harness_adapter_core::Scope;
use super::*;
use crate::harness::SokfTool;
fn status(dir: &Path) -> InstallResult {
let tool = SokfTool::new(
Some(dir.to_path_buf()),
None,
std::collections::BTreeMap::default(),
);
agent_harness_adapter_core::status(&tool, ["claude"], Scope::Project).unwrap()
}
#[test]
fn finds_the_edited_part_whose_record_is_of_an_older_sokf() {
let dir = crate::test_support::fresh_dir("harness-unvouched");
fs::create_dir_all(dir.join(".claude/skills/sokf")).unwrap();
fs::write(
dir.join(".claude/skills/sokf/SKILL.md"),
"---\nname: sokf\n---\nold\n",
)
.unwrap();
fs::create_dir_all(dir.join(".sokf")).unwrap();
fs::write(
dir.join(".sokf/harness.toml"),
"[claude]\nskills = \"fnv1a64:0000000000000001\"\n",
)
.unwrap();
let result = status(&dir);
let found = unvouched(&result);
assert_eq!(found.len(), 1, "{result:?}");
let u = &found[0];
assert_eq!(
(u.harness.as_str(), u.part.as_str(), u.path.as_str()),
("claude", "skills", ".claude/skills")
);
assert_eq!(u.reason, "its record is from an older version");
assert_eq!(
u.words("/.claude/skills"),
"claude: skills in /.claude/skills may not be an edit, as an older sokf recorded it"
);
assert!(u.older());
let other = Unvouched {
reason: "the moon is full".into(),
..u.clone()
};
assert_eq!(other.cause(), "the moon is full");
assert!(!other.older());
assert!(result.warnings[u.warning].starts_with("claude: skills in .claude/skills "));
fs::remove_file(dir.join(".sokf/harness.toml")).unwrap();
assert_eq!(unvouched(&status(&dir)), []);
fs::remove_dir_all(&dir).unwrap();
}
}