skills: load: auto|always|manual frontmatter (on-demand vs inlined vs command-only)
ci / check (pull_request) Canceled after 0s
ci / check (push) Canceled after 0s

This commit is contained in:
hermes committed 2026-10-01 14:27:28 +00:00
1 parent fe40594deb
commit 8712e5f9ca
2 files changed
+76 -4

No files matched your search

+72 -4
View File
@@ -40,6 +40,20 @@ pub struct Skill {
pub name: String,
pub description: String,
pub path: PathBuf,
pub load: Load,
/// Text after the frontmatter.
pub body: String,
}
/// Frontmatter `load:` key. `auto` (default): name + description in the system prompt, the model reads
/// the file when the task matches. `always`: the body is inlined in the system prompt. `manual`: only
/// reachable as a `/<name>` command, never mentioned to the model.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Load {
#[default]
Auto,
Always,
Manual,
}
/// Skill dirs, lowest priority first: `~/.agents/skills` (shared with other agents), `<hx home>/skills`,
@@ -78,10 +92,12 @@ pub fn skills_in(dirs: &[PathBuf]) -> Vec<Skill> {
let Ok(s) = std::fs::read_to_string(&p) else {
continue;
};
let fm = s
let (fm, body) = s
.strip_prefix("---")
.and_then(|r| r.split_once("\n---"))
.map_or("", |(f, _)| f);
.map_or(("", s.as_str()), |(f, b)| {
(f, b.split_once('\n').map_or("", |x| x.1))
});
let get = |k: &str| {
fm.lines().find_map(|l| {
l.strip_prefix(k)?
@@ -96,8 +112,15 @@ pub fn skills_in(dirs: &[PathBuf]) -> Vec<Skill> {
.to_string_lossy()
.into()
});
let load = match get("load").as_deref() {
Some("always") => Load::Always,
Some("manual") => Load::Manual,
_ => Load::Auto,
};
let sk = Skill {
description: get("description").unwrap_or_default(),
load,
body: body.trim().to_string(),
name,
path: p,
};
@@ -112,9 +135,13 @@ pub fn system_prompt(base: Option<&str>, home: &Path, cwd: &Path) -> String {
let mut s = base.unwrap_or(DEFAULT_PROMPT).to_string();
s.push_str(&format!("\n\nWorking directory: {}", cwd.display()));
let sk = skills(home, cwd);
if !sk.is_empty() {
for k in sk.iter().filter(|k| k.load == Load::Always) {
s.push_str(&format!("\n\n# Skill: {}\n{}", k.name, k.body));
}
let auto: Vec<_> = sk.iter().filter(|k| k.load == Load::Auto).collect();
if !auto.is_empty() {
s.push_str("\n\n# Skills\nRead a skill's file with `read` when the task matches it.\n");
for k in sk {
for k in auto {
s.push_str(&format!(
"- {}: {} ({})\n",
k.name,
@@ -168,6 +195,47 @@ mod tests {
assert!(system_prompt(Some("CUSTOM"), &home, &proj).starts_with("CUSTOM"));
}
#[test]
fn skill_load_modes() {
let t = tempfile::tempdir().unwrap();
for (n, l) in [("a", "always"), ("m", "manual"), ("d", "")] {
let d = t.path().join(n);
std::fs::create_dir_all(&d).unwrap();
let load = if l.is_empty() {
String::new()
} else {
format!("load: {l}\n")
};
std::fs::write(
d.join("SKILL.md"),
format!("---\nname: {n}\ndescription: use when {n}\n{load}---\nBODY-{n}\n"),
)
.unwrap();
}
let sk = skills_in(&[t.path().to_path_buf()]);
let loads: Vec<_> = sk.iter().map(|s| (s.name.as_str(), s.load)).collect();
assert_eq!(
loads,
[("a", Load::Always), ("d", Load::Auto), ("m", Load::Manual)]
);
assert_eq!(sk[0].body, "BODY-a");
// prompt: always inlined, auto listed only, manual absent
let home = t.path().join("h");
std::fs::create_dir_all(home.join("skills")).unwrap();
for n in ["a", "d", "m"] {
std::fs::create_dir_all(home.join("skills").join(n)).unwrap();
std::fs::copy(
t.path().join(n).join("SKILL.md"),
home.join("skills").join(n).join("SKILL.md"),
)
.unwrap();
}
let p = system_prompt(None, &home, &t.path().join("proj"));
assert!(p.contains("BODY-a") && !p.contains("- a:"));
assert!(p.contains("- d: use when d") && !p.contains("BODY-d"));
assert!(!p.contains("BODY-m") && !p.contains("- m:"));
}
#[test]
fn agent_skills_dir() {
let t = tempfile::tempdir().unwrap();