use std::collections::BTreeMap;
use agent_harness_adapter_core::{InstallOptions, Item, Scope, State};
use super::harness::{
HarnessDirs, HarnessOptions, HarnessReport, harness_ids, refusal, report, shown, tool,
};
use crate::{Error, RepoRoot, Result, harness::SokfTool, pack};
pub(super) fn conflicted_fragments(repo: &RepoRoot) -> Result<Vec<String>> {
Ok(pack::fragments(repo, &pack::read_lock(repo)?)?
.into_iter()
.map(|(name, text)| (pack::instructions_of(&name), text))
.filter(|(path, text)| !crate::check::conflict_lines(path, text).is_empty())
.map(|(path, _)| path.as_str().to_string())
.collect())
}
pub(super) fn install_held(
dirs: &HarnessDirs,
tool: &SokfTool,
opts: &HarnessOptions,
) -> Result<Option<HarnessReport>> {
let Some(repo) = &dirs.repo else {
return Ok(None);
};
if opts.scope == Scope::User {
return Ok(None);
}
let conflicted = conflicted_fragments(repo)?;
if conflicted.is_empty() {
return Ok(None);
}
let scope = opts.scope;
let ids = harness_ids(&opts.harnesses);
let part = Item::Instructions.as_str();
let before =
report(agent_harness_adapter_core::status(tool, &ids, scope).map_err(refusal(scope))?);
let declined = before.harnesses.iter().any(|h| {
h.parts
.iter()
.any(|p| p.part == part && p.state == State::Skipped)
});
if declined && opts.force.iter().any(|p| p == part) {
return Ok(None);
}
let force = opts.force.iter().filter(|p| *p != part).cloned();
let held = tool.clone().hold_instructions();
let mut out = report(
agent_harness_adapter_core::install(
&held,
&InstallOptions::new(ids, scope).force_parts(force),
)
.map_err(refusal(scope))?,
);
let local = match scope {
Scope::Project => String::new(),
other => format!(" --scope {other}"),
};
for h in &mut out.harnesses {
let Some(was) = before
.harness(&h.harness)
.and_then(|b| b.parts.iter().find(|p| p.part == part))
else {
continue;
};
for p in h.parts.iter_mut().filter(|p| p.part == part) {
*p = was.clone();
}
if !matches!(was.state, State::Skipped | State::Shared) {
h.notes.push(format!(
"kept {} ({part}): {} holds a conflict; resolve it, then run `sokf harness install {}{local}`",
was.path,
conflicted.join(", "),
h.harness
));
}
}
Ok(Some(out))
}
pub(super) fn refuse_pack_files(
dirs: &HarnessDirs,
tool: &SokfTool,
opts: &HarnessOptions,
) -> Result<()> {
let Some(repo) = &dirs.repo else {
return Ok(());
};
if opts.scope == Scope::User {
return Ok(());
}
let lock = pack::read_lock(repo)?;
if lock.packs.is_empty() {
return Ok(());
}
let found = agent_harness_adapter_core::status(tool, harness_ids(&opts.harnesses), opts.scope)
.map_err(refusal(opts.scope))?;
for h in &found.harnesses {
for p in &h.parts {
let written = part_path(opts.scope, p);
let owned = lock.packs.iter().find_map(|(name, pack)| {
pack.files
.values()
.find(|f| pack::covers(&written, &f.path))
.map(|f| (name, &f.path))
});
if let Some((pack, path)) = owned {
return Err(Error::Harness(format!(
"{path}: the {} part of {} would write a file of the pack {pack}; remove the file from the pack, or decline the part in /.sokf/config.toml",
p.part, h.harness
)));
}
}
}
Ok(())
}
fn part_path(scope: Scope, p: &agent_harness_adapter_core::PartResult) -> String {
let path = shown(scope, &p.path);
if p.part == Item::Skills.as_str() {
format!("{}/sokf/", path.trim_end_matches('/'))
} else {
path
}
}
pub(crate) fn written_paths(dirs: &HarnessDirs) -> Result<BTreeMap<String, String>> {
let mut out = BTreeMap::new();
if dirs.repo.is_none() {
return Ok(out);
}
for scope in [Scope::Project, Scope::Local] {
let tool = tool(dirs, scope)?;
let installed =
agent_harness_adapter_core::installed(&tool, scope).map_err(refusal(scope))?;
if installed.is_empty() {
continue;
}
let found =
agent_harness_adapter_core::status(&tool, &installed, scope).map_err(refusal(scope))?;
for h in found.harnesses {
for p in &h.parts {
let path = part_path(scope, p);
if path.starts_with('/') {
out.insert(
path,
format!("the {} part of the {} harness", p.part, h.harness),
);
}
}
}
}
Ok(out)
}