use std::path::{Path, PathBuf};
use trusty_agents_common::agents::skill_root::{SKILLS_ROOT_PLACEHOLDER, resolve_skills_root};
use crate::paths::WriteTargetError;
use crate::paths::write_dir::NativeWriteDir;
pub const SKILL_REFS_DIRNAME: &str = "skill-refs";
pub const REFERENCED_SKILL_FILES: &[(&str, &str)] = &[
(
"condition-based-waiting/SKILL.md",
include_str!("../assets/skill-refs/condition-based-waiting/SKILL.md"),
),
(
"git-workflow/SKILL.md",
include_str!("../assets/skill-refs/git-workflow/SKILL.md"),
),
(
"self-improvement-loop/SKILL.md",
include_str!("../assets/skill-refs/self-improvement-loop/SKILL.md"),
),
(
"verification-before-completion/SKILL.md",
include_str!("../assets/skill-refs/verification-before-completion/SKILL.md"),
),
];
pub fn project_skill_refs_dir(project_root: &Path) -> PathBuf {
crate::paths::native_child(project_root, SKILL_REFS_DIRNAME)
}
pub fn user_skill_refs_dir() -> PathBuf {
crate::paths::private_state::private_state_dir().join(SKILL_REFS_DIRNAME)
}
pub fn embedded_skill_ref(relative: &Path) -> Option<&'static str> {
REFERENCED_SKILL_FILES
.iter()
.find(|(entry, _)| Path::new(entry) == relative)
.map(|(_, content)| *content)
}
pub fn materialize_skill_refs(dir: &NativeWriteDir) -> Result<(), WriteTargetError> {
for (relative, content) in REFERENCED_SKILL_FILES {
let path = Path::new(relative);
let (parent, name) = match (path.parent(), path.file_name().and_then(|n| n.to_str())) {
(Some(parent), Some(name)) => (parent, name),
_ => {
return Err(WriteTargetError::Unpinnable {
path: dir.path().join(relative),
detail: format!("`{relative}` is not a usable skill-ref path"),
});
}
};
let skill = dir.child(parent)?;
if skill
.read(name)?
.is_some_and(|current| current == content.as_bytes())
{
continue;
}
skill.atomic_write(name, content.as_bytes())?;
}
Ok(())
}
pub fn resolve_skill_refs(composed: &str, dir: &Path) -> String {
if !composed.contains(SKILLS_ROOT_PLACEHOLDER) {
return composed.to_string();
}
match resolve_skills_root(composed, dir) {
Ok(resolved) => resolved,
Err(e) => {
tracing::warn!(
path = %dir.display(),
"skill pointers left unresolved in a composed agent: {e}"
);
composed.to_string()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_skill_refs_substitutes_without_touching_disk() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("skill-refs");
let body = format!("Read `{SKILLS_ROOT_PLACEHOLDER}/self-improvement-loop/SKILL.md`.");
let out = resolve_skill_refs(&body, &dir);
let path = dir.join("self-improvement-loop/SKILL.md");
assert_eq!(out, format!("Read `{}`.", path.display()));
assert!(!dir.exists(), "compose must not write the refs dir");
}
#[test]
fn resolve_skill_refs_keeps_the_body_for_a_relative_dir() {
let body = format!("{SKILLS_ROOT_PLACEHOLDER}/x");
assert_eq!(resolve_skill_refs(&body, Path::new("relative")), body);
}
#[test]
fn materialize_skill_refs_writes_readable_files() {
let tmp = tempfile::tempdir().unwrap();
let dir = NativeWriteDir::open(tmp.path(), Path::new(SKILL_REFS_DIRNAME)).unwrap();
materialize_skill_refs(&dir).unwrap();
materialize_skill_refs(&dir).unwrap();
for (relative, content) in REFERENCED_SKILL_FILES {
assert_eq!(
std::fs::read_to_string(dir.path().join(relative)).unwrap(),
*content
);
}
}
#[test]
fn embedded_skill_ref_matches_exact_entries_only() {
let hit = Path::new("self-improvement-loop/SKILL.md");
assert_eq!(embedded_skill_ref(hit), Some(REFERENCED_SKILL_FILES[2].1));
for miss in [
"self-improvement-loop",
"x/../self-improvement-loop/SKILL.md",
"/self-improvement-loop/SKILL.md",
] {
assert_eq!(embedded_skill_ref(Path::new(miss)), None, "{miss}");
}
}
#[test]
fn referenced_copies_match_trusty_mpm_skills() {
let mpm_skills = trusty_common::test_harness::test_repo_root()
.expect("the running checkout's workspace root")
.join("content/skills");
for (relative, content) in REFERENCED_SKILL_FILES {
let skill = relative.trim_end_matches("/SKILL.md");
let source = std::fs::read_to_string(mpm_skills.join(format!("{skill}.md")))
.unwrap_or_else(|e| panic!("trusty-mpm skill `{skill}` must exist: {e}"));
assert_eq!(
*content, source,
"`assets/skill-refs/{relative}` drifted from trusty-mpm's `{skill}.md`"
);
}
}
#[test]
fn every_roster_pointer_names_an_embedded_file() {
let marker = format!("{SKILLS_ROOT_PLACEHOLDER}/");
let root = trusty_common::test_harness::test_repo_root()
.expect("the running checkout's workspace root"); let content =
trusty_agents_common::agent_content::checkout_content(&root).expect("repo content");
let roster =
trusty_agents_common::agent_content::AgentRoster::load(&content).expect("roster");
for (file_name, md) in crate::assets::tm_agent_sources(&roster).expect("catalog") {
for (at, _) in md.match_indices(&marker) {
let rest = &md[at + marker.len()..];
let relative = rest.split('`').next().unwrap_or(rest);
assert!(
REFERENCED_SKILL_FILES.iter().any(|(r, _)| *r == relative),
"{file_name} points at `{relative}`, which tcode does not embed"
);
}
}
}
}