mod entries;
mod git;
mod installation;
mod manifest;
mod removal;
mod transaction;
mod update;
pub use update::update;
use anyhow::{bail, Context, Result};
use serde::Serialize;
use std::{
collections::{BTreeMap, BTreeSet},
fs,
path::{Path, PathBuf},
};
use super::{
error::AppError, installer, path_guard, skill_store::SkillRecord, tool_adapters::ToolAdapter,
};
pub use manifest::LIBRARY as SKILLS_DIRECTORY;
use manifest::{fingerprint, ProjectEntry, ProjectManifest, ProjectSource, LIBRARY, MANIFEST};
pub use removal::{remove_target, remove_target_with_adapter};
use transaction::{ProjectLock, Publication};
#[derive(Serialize)]
pub struct ProjectLibraryStatus {
pub skills: Vec<ProjectLibrarySkill>,
pub files_to_commit: Vec<String>,
pub management_files: Vec<String>,
pub ignored_paths: Vec<String>,
}
#[derive(Serialize)]
pub struct ProjectLibrarySkill {
pub name: String,
pub directory: String,
pub modified: bool,
pub disabled: bool,
pub targets: Vec<String>,
pub stale_targets: Vec<String>,
pub target_issues: Vec<ProjectTargetIssue>,
}
#[derive(Serialize)]
pub struct ProjectTargetIssue {
pub agent: String,
pub agent_name: String,
pub path: String,
pub reason: &'static str,
}
pub fn install(
root: &Path,
skill: &SkillRecord,
targets: &[ToolAdapter],
) -> Result<ProjectLibraryStatus, AppError> {
install_with_mode(root, skill, targets, false)
}
pub fn install_with_mode(
root: &Path,
skill: &SkillRecord,
targets: &[ToolAdapter],
copy: bool,
) -> Result<ProjectLibraryStatus, AppError> {
mutate(root, |root, lock| {
let name = super::sync_engine::target_dir_name(Path::new(&skill.central_path), &skill.name);
safe_name(&name)?;
let mut manifest = manifest::read(root)?;
if manifest.skills.contains_key(&name) {
return deploy_unlocked(root, lock, &mut manifest, &name, targets, copy);
}
installation::install_unlocked(root, lock, skill, targets, &mut manifest, &name, copy)
})
}
pub fn deploy(
root: &Path,
name: &str,
targets: &[ToolAdapter],
) -> Result<ProjectLibraryStatus, AppError> {
deploy_with_mode(root, name, targets, false)
}
pub fn deploy_with_mode(
root: &Path,
name: &str,
targets: &[ToolAdapter],
copy: bool,
) -> Result<ProjectLibraryStatus, AppError> {
mutate(root, |root, lock| {
safe_name(name)?;
let mut manifest = manifest::read(root)?;
deploy_unlocked(root, lock, &mut manifest, name, targets, copy)
})
}
fn deploy_unlocked(
root: &Path,
lock: &ProjectLock,
manifest: &mut ProjectManifest,
name: &str,
targets: &[ToolAdapter],
copy: bool,
) -> Result<()> {
let canonical = format!("{LIBRARY}/{name}");
let content = safe_path(root, &canonical)?;
if !super::skill_metadata::is_valid_skill_dir(&content) {
bail!("Project library Skill not found");
}
let current = fingerprint(&content)?.context("Project library Skill not found")?;
let entry = manifest
.skills
.entry(name.into())
.or_insert_with(|| ProjectEntry {
id: uuid::Uuid::new_v4().to_string(),
name: name.into(),
content_hash: current.clone(),
targets: BTreeMap::new(),
source: None,
disabled: false,
disabled_targets: BTreeSet::new(),
linked_targets: BTreeSet::new(),
extra: BTreeMap::new(),
});
if entry.disabled {
bail!("Project Skill is disabled; enable it before adding targets");
}
let paths = target_paths(root, name, targets)?;
let snapshot = team_snapshot(&content, ¤t)?;
let content = snapshot.path().join("content");
let mut publication = Publication::new(root)?;
publication.observe(canonical.clone(), Some(current.clone()));
for path in unique_paths(paths.values()) {
if path == canonical {
continue;
}
let recorded = entry.targets.values().any(|existing| existing == &path);
let disabled = entry
.targets
.iter()
.any(|(key, existing)| existing == &path && entry.disabled_targets.contains(key));
for (key, added_path) in &paths {
if added_path == &path && disabled {
entry.disabled_targets.insert(key.clone());
}
}
let path = actual_target(&path, disabled);
let baseline = entries::fingerprint_entry(&entries::path(root, &path)?)?;
check_replace(
root,
&path,
recorded.then_some(entry.content_hash.as_str()),
Some(¤t),
)?;
let linked = if recorded {
installation::linked_path(entry, &path) || entries::path(root, &path)?.is_symlink()
} else {
!copy
};
if linked {
let created = publication.add_link(
root,
path.clone(),
&canonical,
&content,
baseline.as_deref(),
)?;
for (agent, added_path) in &paths {
if actual_target(added_path, disabled) == path {
if created {
entry.linked_targets.insert(agent.clone());
} else {
entry.linked_targets.remove(agent);
}
}
}
} else {
publication.add(root, path.clone(), Some(&content), baseline.as_deref())?;
for (agent, added_path) in &paths {
if actual_target(added_path, disabled) == path {
entry.linked_targets.remove(agent);
}
}
}
}
entry.targets.extend(paths);
publish_manifest(root, lock, publication, manifest)
}
pub fn repair(root: &Path, name: &str) -> Result<ProjectLibraryStatus, AppError> {
mutate(root, |root, lock| {
let mut manifest = manifest::read(root)?;
let entry = manifest
.skills
.get_mut(name)
.context("Project Skill is not managed")?;
let canonical = canonical_path(name, entry.disabled);
let baseline = entry.content_hash.clone();
let content = safe_path(root, &canonical)?;
let current = fingerprint(&content)?
.context("Project library content missing; restore it from project Git")?;
let snapshot = team_snapshot(&content, ¤t)?;
let content = snapshot.path().join("content");
let mut publication = Publication::new(root)?;
publication.observe(canonical.clone(), Some(current.clone()));
for path in all_entry_paths(name, entry) {
if path == canonical || !entries::is_present(root, &path)? {
continue;
}
check_replace(root, &path, Some(&baseline), Some(¤t))?;
installation::publish_target(
&mut publication,
root,
entry,
path,
&canonical,
&content,
Some(&baseline),
)?;
}
entry.content_hash = current;
publish_manifest(root, lock, publication, &manifest)
})
}
pub fn set_enabled(
root: &Path,
name: &str,
enabled: bool,
) -> Result<ProjectLibraryStatus, AppError> {
mutate(root, |root, lock| {
let mut manifest = manifest::read(root)?;
let entry = manifest
.skills
.get_mut(name)
.context("Project Skill is not managed")?;
if entry.disabled != enabled {
return Ok(());
}
let old_paths = all_entry_paths(name, entry);
let source = safe_path(root, &canonical_path(name, entry.disabled))?;
let current = fingerprint(&source)?.context("Project library content missing")?;
for path in &old_paths {
check_replace(root, path, Some(¤t), None)?;
}
let snapshot = team_snapshot(&source, ¤t)?;
let source = snapshot.path().join("content");
entry.disabled = !enabled;
entry.disabled_targets.clear();
let new_paths = all_entry_paths(name, entry);
let mut publication = Publication::new(root)?;
let unchanged: Vec<_> = old_paths
.iter()
.filter(|path| new_paths.contains(path))
.cloned()
.collect();
for path in old_paths {
if !unchanged.contains(&path) {
publication.add(root, path, None, Some(¤t))?;
}
}
for path in new_paths {
if unchanged.contains(&path) {
continue;
}
check_replace(root, &path, None, Some(¤t))?;
installation::publish_target(
&mut publication,
root,
entry,
path,
&canonical_path(name, entry.disabled),
&source,
None,
)?;
}
entry.content_hash = current;
publish_manifest(root, lock, publication, &manifest)
})
}
pub fn set_target_enabled(
root: &Path,
name: &str,
agent: &str,
enabled: bool,
) -> Result<ProjectLibraryStatus, AppError> {
let manifest = manifest::read(root).map_err(AppError::io_with_context)?;
if manifest
.skills
.get(name)
.and_then(|entry| entry.targets.get(agent))
.is_some_and(|path| path == &format!("{LIBRARY}/{name}"))
{
return set_enabled(root, name, enabled);
}
mutate(root, |root, lock| {
let mut manifest = manifest::read(root)?;
let entry = manifest
.skills
.get_mut(name)
.context("Project Skill is not managed")?;
if entry.disabled {
if !enabled {
return Ok(());
}
bail!("Shared project Skill is disabled; enable the project Skill first");
}
let path = entry
.targets
.get(agent)
.context("Team target not found")?
.clone();
let disabled = entry.disabled_targets.contains(agent);
if disabled != enabled {
return Ok(());
}
let old = actual_target(&path, disabled);
let new = actual_target(&path, !enabled);
let source = entries::path(root, &old)?;
let current = entries::content_fingerprint(root, &old)?
.context("Team entry missing; repair it first")?;
check_replace(root, &new, None, Some(¤t))?;
let mut publication = Publication::new(root)?;
publication.add(root, old, None, Some(¤t))?;
publication.add(root, new, Some(&source), None)?;
let aliases: Vec<_> = entry
.targets
.iter()
.filter(|(_, other)| *other == &path)
.map(|(key, _)| key.clone())
.collect();
for key in aliases {
if enabled {
entry.disabled_targets.remove(&key);
} else {
entry.disabled_targets.insert(key);
}
}
publish_manifest(root, lock, publication, &manifest)
})
}
pub fn remove(root: &Path, name: &str) -> Result<ProjectLibraryStatus, AppError> {
mutate(root, |root, lock| {
let mut manifest = manifest::read(root)?;
let entry = manifest
.skills
.get(name)
.context("Project Skill is not managed")?;
let mut publication = Publication::new(root)?;
for path in all_entry_paths(name, entry) {
check_replace(root, &path, Some(&entry.content_hash), None)?;
publication.add(root, path, None, Some(&entry.content_hash))?;
}
manifest.skills.remove(name);
publish_manifest(root, lock, publication, &manifest)
})
}
pub fn is_managed(root: &Path, name: &str) -> Result<bool, AppError> {
manifest::read(root)
.map(|manifest| manifest.skills.contains_key(name))
.map_err(AppError::io_with_context)
}
pub fn status(root: &Path) -> Result<ProjectLibraryStatus, AppError> {
let root = root.canonicalize().map_err(AppError::io)?;
status_inner(&root).map_err(AppError::io_with_context)
}
fn status_inner(root: &Path) -> Result<ProjectLibraryStatus> {
let manifest = manifest::read(root)?;
let mut files = Vec::new();
let mut management_files = Vec::new();
let mut skills = Vec::new();
if root.join(MANIFEST).is_file() {
files.push(MANIFEST.into());
management_files.push(MANIFEST.into());
}
for (directory, entry) in &manifest.skills {
let canonical = canonical_path(directory, entry.disabled);
let current = fingerprint(&safe_path(root, &canonical)?)?;
let mut stale = Vec::new();
let mut target_issues = Vec::new();
for (agent, path) in &entry.targets {
let path = actual_target(
path,
entry.disabled || entry.disabled_targets.contains(agent),
);
let target = entries::path(root, &path)?;
if !entries::is_present(root, &path)? {
continue;
}
let is_linked = entry.linked_targets.contains(agent);
let reason = if is_linked && !target.is_symlink() {
Some("link_replaced")
} else if (is_linked || target.is_symlink())
&& !entries::points_to(root, &path, &canonical)
{
Some("link_mismatch")
} else if entries::content_fingerprint(root, &path)? != current {
Some("content_mismatch")
} else {
None
};
if let Some(reason) = reason {
stale.push(agent.clone());
target_issues.push(ProjectTargetIssue {
agent: agent.clone(),
agent_name: super::tool_adapters::find_adapter(agent)
.map(|adapter| adapter.display_name)
.unwrap_or_else(|| agent.clone()),
path,
reason,
});
}
}
files.extend(all_entry_paths(directory, entry));
skills.push(ProjectLibrarySkill {
name: entry.name.clone(),
directory: directory.clone(),
disabled: entry.disabled,
modified: current.as_deref() != Some(&entry.content_hash),
targets: entry.targets.keys().cloned().collect(),
stale_targets: stale,
target_issues,
});
}
if let Ok(repo) = git2::Repository::discover(root) {
if let (Some(workdir), Ok(changes)) = (repo.workdir(), repo.statuses(None)) {
for change in changes.iter().filter(|change| {
change
.status()
.intersects(git2::Status::WT_DELETED | git2::Status::INDEX_DELETED)
}) {
if let Some(path) = change.path().and_then(|path| {
workdir
.join(path)
.strip_prefix(root)
.ok()
.map(Path::to_path_buf)
}) {
let path = path
.to_string_lossy()
.replace(std::path::MAIN_SEPARATOR, "/");
if path.ends_with("/SKILL.md") || path.ends_with("/skill.md") {
files.push(path);
}
}
}
}
}
files.sort();
files.dedup();
let ignored_paths = git::ignored_paths(root, &files);
Ok(ProjectLibraryStatus {
skills,
files_to_commit: files,
management_files,
ignored_paths,
})
}
fn mutate(
root: &Path,
action: impl FnOnce(&Path, &ProjectLock) -> Result<()>,
) -> Result<ProjectLibraryStatus, AppError> {
let _guard = super::project_skill_mutation::lock_project_skill_mutation()?;
let root = root
.canonicalize()
.map_err(|err| AppError::io_with_context(err.into()))?;
let result = (|| {
let lock = ProjectLock::acquire(&root)?;
transaction::recover(&root, &lock)?;
action(&root, &lock)?;
status_inner(&root)
})();
result.map_err(|error| match error.downcast::<AppError>() {
Ok(error) => error,
Err(error) => AppError::io_with_context(error),
})
}
pub(crate) fn safe_name(name: &str) -> Result<()> {
path_guard::ensure_safe_skill_relative_path(name).map_err(|e| anyhow::anyhow!(e.message))?;
if name.contains(['/', '\\', ':']) || name.starts_with('.') {
bail!("Invalid project Skill directory name");
}
Ok(())
}
pub(crate) fn safe_path(root: &Path, relative: &str) -> Result<PathBuf> {
let target = entries::path(root, relative)?;
if !path_guard::is_path_safe(root, &target) {
bail!("Project path escapes its workspace");
}
if target.is_symlink() {
bail!("Project path contains a symbolic link; import or migrate explicitly");
}
Ok(target)
}
fn check_replace(
root: &Path,
path: &str,
baseline: Option<&str>,
desired: Option<&str>,
) -> Result<()> {
let current = entries::content_fingerprint(root, path)?;
if current.is_none() || current.as_deref() == desired || current.as_deref() == baseline {
return Ok(());
}
bail!("Project content changed or is not managed: {path}. Existing files were preserved; review the differences first");
}
fn target_paths(
root: &Path,
name: &str,
targets: &[ToolAdapter],
) -> Result<BTreeMap<String, String>> {
if targets.is_empty() {
bail!("Select at least one team Agent");
}
let mut paths: BTreeMap<String, String> = BTreeMap::new();
for target in targets {
let directory = target.project_relative_skills_dir();
if directory.is_empty() {
bail!("Agent has no project Skills directory");
}
let path = entries::target_path(root, &format!("{directory}/{name}"))?;
validate_target(root, name, &path)?;
for existing in paths.values() {
if existing != &path {
path_guard::ensure_disjoint_paths(
&entries::path(root, existing)?,
&entries::path(root, &path)?,
)
.map_err(|err| anyhow::anyhow!(err.message))?;
}
}
paths.insert(target.key.clone(), path);
}
Ok(paths)
}
pub(crate) fn validate_target(root: &Path, name: &str, path: &str) -> Result<()> {
let target = entries::path(root, path)?;
for relative in [
canonical_path(name, false),
canonical_path(name, true),
MANIFEST.into(),
] {
let reference = entries::path(root, &relative)?;
if reference != target {
path_guard::ensure_disjoint_paths(&reference, &target)
.map_err(|err| anyhow::anyhow!(err.message))?;
}
}
Ok(())
}
fn unique_paths<'a>(paths: impl Iterator<Item = &'a String>) -> Vec<String> {
let mut result: Vec<_> = paths.cloned().collect();
result.sort();
result.dedup();
result
}
fn canonical_path(name: &str, disabled: bool) -> String {
format!(
"{LIBRARY}{}/{name}",
if disabled { "-disabled" } else { "" }
)
}
fn actual_target(path: &str, disabled: bool) -> String {
if !disabled {
return path.into();
}
let (parent, name) = path.rsplit_once('/').expect("validated relative target");
format!("{parent}-disabled/{name}")
}
fn all_entry_paths(name: &str, entry: &ProjectEntry) -> Vec<String> {
let mut paths = vec![canonical_path(name, entry.disabled)];
paths.extend(entry.targets.iter().map(|(agent, path)| {
actual_target(
path,
entry.disabled || entry.disabled_targets.contains(agent),
)
}));
paths.sort();
paths.dedup();
paths
}
fn team_snapshot(source: &Path, expected: &str) -> Result<tempfile::TempDir> {
let snapshot = tempfile::tempdir()?;
let content = snapshot.path().join("content");
transaction::copy_project_content(source, &content)?;
if fingerprint(source)?.as_deref() != Some(expected)
|| fingerprint(&content)?.as_deref() != Some(expected)
{
bail!("Project source changed while staging; no team files were replaced");
}
Ok(snapshot)
}
fn portable_source(skill: &SkillRecord) -> Option<ProjectSource> {
if !matches!(skill.source_type.as_str(), "git" | "skillssh") {
return None;
}
let url = skill.source_ref_resolved.as_ref()?;
let parsed = reqwest::Url::parse(url).ok()?;
if !matches!(parsed.scheme(), "https" | "http")
|| !parsed.username().is_empty()
|| parsed.password().is_some()
|| parsed.query().is_some()
|| parsed.fragment().is_some()
{
return None;
}
Some(ProjectSource {
source_type: skill.source_type.clone(),
url: url.clone(),
subpath: skill
.source_subpath
.clone()
.filter(|path| path_guard::ensure_safe_skill_relative_path(path).is_ok()),
branch: skill.source_branch.clone(),
revision: if super::content_hash::hash_directory(Path::new(&skill.central_path))
.ok()
.as_ref()
== skill.content_hash.as_ref()
{
skill.source_revision.clone()
} else {
None
},
})
}
fn publish_manifest(
root: &Path,
lock: &ProjectLock,
mut publication: Publication,
manifest: &ProjectManifest,
) -> Result<()> {
let file = tempfile::NamedTempFile::new()?;
let mut bytes = serde_json::to_vec_pretty(manifest)?;
bytes.push(b'\n');
fs::write(file.path(), bytes)?;
publication.add(
root,
MANIFEST.into(),
Some(file.path()),
manifest.baseline.as_deref(),
)?;
publication.apply(root, lock)
}
#[cfg(test)]
mod compatibility_tests;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod deployment_tests;
#[cfg(test)]
mod removal_tests;
#[cfg(test)]
mod force_restore_tests;
#[cfg(test)]
mod directory_alias_tests;