use std::path::PathBuf;
use agent_harness_adapter_core::{DeclinedStore, TomlDeclined};
pub const OFF_BY_DEFAULT: &[&str] = &["hooks"];
pub fn bad_with(harness: &str, parts: &[String]) -> Option<String> {
parts
.iter()
.find(|p| !OFF_BY_DEFAULT.contains(&p.as_str()))
.map(|p| {
let allowed: Vec<String> = OFF_BY_DEFAULT.iter().map(|p| format!("`{p}`")).collect();
format!(
"harness.{harness}.with lists `{p}`, which isn't off by default; it can list only {}",
allowed.join(", ")
)
})
}
pub struct SokfDeclined {
inner: TomlDeclined,
path: PathBuf,
shown: String,
}
impl SokfDeclined {
pub fn new(path: PathBuf, shown: String) -> Self {
SokfDeclined {
inner: TomlDeclined::new(path.clone(), shown.clone()),
path,
shown,
}
}
pub fn with(&self, harness: &str) -> agent_harness_adapter_core::Result<Vec<String>> {
let refuse = |m: String| agent_harness_adapter_core::Error::file(&self.shown, m);
let text = match std::fs::read_to_string(&self.path) {
Ok(text) => text,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(e) => return Err(refuse(e.to_string())),
};
let doc = text
.parse::<toml::Table>()
.map_err(|e| refuse(e.message().to_string()))?;
let Some(list) = doc
.get("harness")
.and_then(|h| h.get(harness))
.and_then(|t| t.get("with"))
else {
return Ok(Vec::new());
};
let not_list = || {
refuse(format!(
"`harness.{harness}.with` is not a list of part names"
))
};
let parts = list
.as_array()
.ok_or_else(not_list)?
.iter()
.map(|v| v.as_str().map(str::to_string).ok_or_else(not_list))
.collect::<agent_harness_adapter_core::Result<Vec<_>>>()?;
if let Some(m) = bad_with(harness, &parts) {
return Err(refuse(m));
}
Ok(parts)
}
pub fn off_by_default(&self, harness: &str) -> agent_harness_adapter_core::Result<Vec<String>> {
let with = self.with(harness)?;
Ok(OFF_BY_DEFAULT
.iter()
.filter(|p| !with.iter().any(|w| w == *p))
.map(|p| (*p).to_string())
.collect())
}
pub fn without(&self, harness: &str) -> agent_harness_adapter_core::Result<Vec<String>> {
self.inner.declined(harness)
}
pub fn shown(&self) -> &str {
&self.shown
}
}
impl DeclinedStore for SokfDeclined {
fn declined(&self, harness: &str) -> agent_harness_adapter_core::Result<Vec<String>> {
let mut declined = self.without(harness)?;
for part in self.off_by_default(harness)? {
if !declined.contains(&part) {
declined.push(part);
}
}
Ok(declined)
}
fn location(&self) -> String {
self.inner.location()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_hooks_are_declined_unless_with_lists_them() {
let dir = crate::test_support::fresh_dir("harness-declined");
let path = dir.join("config.toml");
let store = SokfDeclined::new(path.clone(), "/config.toml".into());
assert_eq!(store.declined("claude").unwrap(), ["hooks"]);
std::fs::write(&path, "[harness.claude]\nwithout = [\"skills\"]\n").unwrap();
assert_eq!(store.declined("claude").unwrap(), ["skills", "hooks"]);
std::fs::write(&path, "[harness.claude]\nwith = [\"hooks\"]\n").unwrap();
assert_eq!(store.declined("claude").unwrap(), Vec::<String>::new());
assert_eq!(store.declined("codex").unwrap(), ["hooks"]);
std::fs::write(
&path,
"[harness.claude]\nwith = [\"hooks\"]\nwithout = [\"hooks\"]\n",
)
.unwrap();
assert_eq!(store.declined("claude").unwrap(), ["hooks"]);
std::fs::write(&path, "[harness.claude]\nwith = [\"skills\"]\n").unwrap();
let e = store.declined("claude").unwrap_err().to_string();
assert!(e.contains("harness.claude.with lists `skills`"), "{e}");
std::fs::remove_dir_all(&dir).unwrap();
}
}