use std::{
fs,
path::{Path, PathBuf},
};
use serde::Serialize;
use super::pack::{edited_files, harness_dirs, remove, write};
use crate::{
Error, RepoPath, RepoRoot, Result, check,
pack::{self, EMBEDDED, EmbeddedPack},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum PackFileState {
Edited,
Conflicted,
Deleted,
}
impl PackFileState {
pub fn label(self) -> &'static str {
match self {
PackFileState::Edited => "edited",
PackFileState::Conflicted => "conflicted",
PackFileState::Deleted => "deleted",
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct PackState {
pub name: String,
pub version: String,
pub source: String,
pub git_ref: Option<String>,
pub stale: Option<String>,
pub files: Vec<(String, PackFileState)>,
}
#[derive(Debug, Clone, Serialize)]
pub struct PackStatus {
pub packs: Vec<PackState>,
}
impl PackStatus {
pub fn to_text(&self) -> String {
if self.packs.is_empty() {
return "the lock holds no pack\n".into();
}
let mut out = String::new();
for p in &self.packs {
let source = match &p.git_ref {
Some(r) => format!("{}@{r}", p.source),
None => p.source.clone(),
};
out.push_str(&format!("{} {} from {source}", p.name, p.version));
match &p.stale {
Some(why) => out.push_str(&format!(": stale, {why}\n")),
None => out.push('\n'),
}
for (path, state) in &p.files {
out.push_str(&format!("{} {path}\n", state.label()));
}
}
out
}
}
pub fn pack_status(root: &RepoRoot) -> Result<PackStatus> {
let lock = pack::read_lock(root)?;
let tool = pack::tool_version();
let packs = lock
.packs
.iter()
.map(|(name, p)| {
let edited = edited_files(root, p);
let files = edited
.into_iter()
.map(|path| {
let text = RepoPath::parse(&path)
.and_then(|rp| fs::read_to_string(rp.to_fs(root)).ok().map(|t| (rp, t)));
let state = match text {
None => PackFileState::Deleted,
Some((rp, t)) if !check::conflict_lines(&rp, &t).is_empty() => {
PackFileState::Conflicted
}
Some(_) => PackFileState::Edited,
};
(path, state)
})
.collect();
let builtin = p.source.starts_with(pack::BUILTIN);
let stale = (builtin && p.version != tool).then(|| {
format!(
"this is sokf {tool}; run `sokf pack sync` to sync the pack of this release"
)
});
PackState {
name: name.clone(),
version: p.version.clone(),
source: p.source.clone(),
git_ref: p.git_ref.clone(),
stale,
files,
}
})
.collect();
Ok(PackStatus { packs })
}
#[derive(Debug, Clone, Serialize)]
pub struct ListedPack {
pub name: String,
pub version: String,
pub description: String,
pub locked: bool,
}
pub fn list_text(packs: &[ListedPack]) -> String {
if packs.is_empty() {
return "the binary embeds no pack\n".into();
}
packs
.iter()
.map(|p| {
let mark = if p.locked { " (synced)" } else { "" };
format!("{} {}{mark}: {}\n", p.name, p.version, p.description)
})
.collect()
}
pub fn pack_list(root: Option<&RepoRoot>) -> Result<Vec<ListedPack>> {
pack_list_with(root, EMBEDDED)
}
pub fn pack_list_with(
root: Option<&RepoRoot>,
embedded: &[EmbeddedPack],
) -> Result<Vec<ListedPack>> {
let lock = match root {
Some(r) => pack::read_lock(r)?,
None => pack::Lock::default(),
};
Ok(pack::embedded_packs(embedded)?
.into_iter()
.map(|m| ListedPack {
locked: lock
.packs
.get(&m.name)
.is_some_and(|p| p.source == format!("{}{}", pack::BUILTIN, m.name)),
name: m.name,
version: m.version,
description: m.description,
})
.collect())
}
#[derive(Debug, Clone, Serialize)]
pub struct PackRemoved {
pub name: String,
pub removed: Vec<String>,
pub kept: Vec<String>,
pub instructions: Vec<String>,
}
impl PackRemoved {
pub fn to_text(&self) -> String {
let mut out = String::new();
for p in &self.removed {
out.push_str(&format!("removed {p}\n"));
}
for p in &self.kept {
out.push_str(&format!("kept {p}: edited\n"));
}
out.push_str(&format!("removed the pack {} from the lock\n", self.name));
for line in &self.instructions {
out.push_str(&format!("{line}\n"));
}
out.push_str("note: run `sokf validate --fix` to bring the store's indexes up to date\n");
out
}
}
fn locked(root: &RepoRoot, name: &str) -> Result<(pack::Lock, pack::LockedPack)> {
let lock = pack::read_lock(root)?;
let entry = lock.packs.get(name).cloned().ok_or_else(|| {
let names: Vec<&str> = lock.packs.keys().map(String::as_str).collect();
Error::Pack(format!(
"the lock holds no pack named `{name}`; it holds {}",
if names.is_empty() {
"none".to_string()
} else {
names.join(", ")
}
))
})?;
Ok((lock, entry))
}
pub fn pack_remove(root: &RepoRoot, name: &str, home: Option<PathBuf>) -> Result<PackRemoved> {
let (mut lock, entry) = locked(root, name)?;
let edited = edited_files(root, &entry);
let mut report = PackRemoved {
name: name.to_string(),
removed: Vec::new(),
kept: Vec::new(),
instructions: Vec::new(),
};
let fragment = pack::instructions_of(name);
for (rel, f) in &entry.files {
if edited.contains(&f.path) && f.path != fragment.as_str() {
if RepoPath::parse(&f.path).is_some_and(|p| p.to_fs(root).exists()) {
report.kept.push(f.path.clone());
}
} else if let Some(p) = RepoPath::parse(&f.path) {
let existed = p.to_fs(root).exists();
remove(root, &p)?;
if existed {
report.removed.push(f.path.clone());
}
}
remove(root, &pack::base_path(name, rel))?;
}
lock.packs.remove(name);
if lock.packs.is_empty() {
remove(root, &pack::lock_path())?;
let _ = fs::remove_dir(root.as_path().join(pack::PACKS_DIR));
} else {
write(root, &pack::lock_path(), pack::lock_text(&lock)?.as_bytes())?;
}
report.instructions = super::harness::refresh_instructions(&harness_dirs(root, &home))?;
Ok(report)
}
pub fn pack_diff(root: &RepoRoot, name: &str) -> Result<String> {
let (_, entry) = locked(root, name)?;
let edited = edited_files(root, &entry);
let mut out = String::new();
for (rel, f) in &entry.files {
if !edited.contains(&f.path) {
continue;
}
let base_path = pack::base_path(name, rel).to_fs(root);
let base = fs::read(&base_path).map_err(|e| Error::io(&base_path, e))?;
let current = RepoPath::parse(&f.path).and_then(|p| fs::read(p.to_fs(root)).ok());
let current = current.as_deref().map(pack::normalise);
out.push_str(&pack::diff(
rel,
&pack::normalise(&base),
current.as_deref(),
)?);
}
Ok(out)
}
pub fn pack_export(name: &str, dir: &Path) -> Result<Vec<String>> {
pack_export_with(name, dir, EMBEDDED)
}
pub fn pack_export_with(name: &str, dir: &Path, embedded: &[EmbeddedPack]) -> Result<Vec<String>> {
let found = embedded
.iter()
.find(|p| p.read().is_ok_and(|f| f.manifest.name == name));
let Some(found) = found else {
pack::find_embedded(embedded, name)?;
return Err(Error::Internal(format!("no embedded pack {name}")));
};
if let Ok(mut entries) = fs::read_dir(dir)
&& entries.next().is_some()
{
return Err(Error::Pack(format!(
"{}: the directory is not empty",
dir.display()
)));
}
let mut written = Vec::new();
for (rel, text) in found.files {
let path = dir.join(rel);
agent_harness_adapter_core::fs::write_atomic(&path, text.as_bytes())?;
written.push(rel.to_string());
}
Ok(written)
}