1use std::collections::BTreeMap;
8use std::env;
9use std::fmt;
10use std::fs;
11use std::io;
12use std::path::{Path, PathBuf};
13use std::sync::OnceLock;
14
15pub const HARN_SKILLS_DIR_ENV: &str = "HARN_SKILLS_DIR";
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct SkillFrontmatter {
21 pub name: &'static str,
22 pub short: &'static str,
23 pub description: &'static str,
24 pub when_to_use: Option<&'static str>,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub struct EmbeddedSkill {
30 pub name: &'static str,
31 pub frontmatter: SkillFrontmatter,
32 pub body: &'static str,
33 pub source: &'static str,
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct DiskSkillFrontmatter {
43 pub name: String,
44 pub short: String,
45 pub description: String,
46 pub when_to_use: Option<String>,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct DiskSkill {
52 pub name: String,
53 pub frontmatter: DiskSkillFrontmatter,
54 pub body: String,
55 pub source: String,
56 pub path: PathBuf,
57}
58
59#[derive(Debug, Clone, PartialEq, Eq)]
61pub enum SkillCorpus {
62 Embedded(&'static [EmbeddedSkill]),
63 Disk(Vec<DiskSkill>),
64}
65
66impl SkillCorpus {
67 pub fn is_disk(&self) -> bool {
68 matches!(self, Self::Disk(_))
69 }
70
71 pub fn len(&self) -> usize {
72 match self {
73 Self::Embedded(skills) => skills.len(),
74 Self::Disk(skills) => skills.len(),
75 }
76 }
77
78 pub fn is_empty(&self) -> bool {
79 self.len() == 0
80 }
81}
82
83#[derive(Debug)]
85pub enum SkillDiscoveryError {
86 Io {
87 path: PathBuf,
88 source: io::Error,
89 },
90 MissingFrontmatter {
91 path: PathBuf,
92 },
93 MissingField {
94 path: PathBuf,
95 field: &'static str,
96 },
97 DuplicateName {
98 name: String,
99 first: PathBuf,
100 second: PathBuf,
101 },
102}
103
104impl fmt::Display for SkillDiscoveryError {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 match self {
107 Self::Io { path, source } => write!(f, "{}: {source}", path.display()),
108 Self::MissingFrontmatter { path } => {
109 write!(f, "{}: missing SKILL.md frontmatter", path.display())
110 }
111 Self::MissingField { path, field } => {
112 write!(f, "{}: missing `{field}` frontmatter field", path.display())
113 }
114 Self::DuplicateName {
115 name,
116 first,
117 second,
118 } => write!(
119 f,
120 "duplicate skill `{name}` in {} and {}",
121 first.display(),
122 second.display()
123 ),
124 }
125 }
126}
127
128impl std::error::Error for SkillDiscoveryError {}
129
130const SOURCES: &[&str] = &[
131 include_str!("corpus/harn-agent/SKILL.md"),
132 include_str!("corpus/harn-diagnostics/SKILL.md"),
133 include_str!("corpus/harn-language/SKILL.md"),
134 include_str!("corpus/harn-orchestration/SKILL.md"),
135 include_str!("corpus/harn-providers/SKILL.md"),
136 include_str!("corpus/harn-testing/SKILL.md"),
137 include_str!("corpus/harn-tracing/SKILL.md"),
138];
139
140static EMBEDDED_SKILLS: OnceLock<Box<[EmbeddedSkill]>> = OnceLock::new();
141
142pub fn list_embedded_skills() -> &'static [EmbeddedSkill] {
144 EMBEDDED_SKILLS
145 .get_or_init(|| SOURCES.iter().map(|source| parse_skill(source)).collect())
146 .as_ref()
147}
148
149pub fn get_embedded_skill(name: &str) -> Option<&'static EmbeddedSkill> {
151 list_embedded_skills()
152 .iter()
153 .find(|skill| skill.name == name)
154}
155
156pub fn resolve_skill_corpus_from_env() -> Result<SkillCorpus, SkillDiscoveryError> {
160 let Ok(dir) = env::var(HARN_SKILLS_DIR_ENV) else {
161 return Ok(SkillCorpus::Embedded(list_embedded_skills()));
162 };
163 if dir.trim().is_empty() {
164 return Ok(SkillCorpus::Embedded(list_embedded_skills()));
165 }
166
167 let skills = list_disk_skills(dir)?;
168 if skills.is_empty() {
169 Ok(SkillCorpus::Embedded(list_embedded_skills()))
170 } else {
171 Ok(SkillCorpus::Disk(skills))
172 }
173}
174
175pub fn list_disk_skills(root: impl AsRef<Path>) -> Result<Vec<DiskSkill>, SkillDiscoveryError> {
180 let root = root.as_ref();
181 if !root.exists() {
182 return Ok(Vec::new());
183 }
184
185 let mut paths = Vec::new();
186 collect_skill_paths(root, &mut paths)?;
187 paths.sort();
188
189 let mut by_name: BTreeMap<String, DiskSkill> = BTreeMap::new();
190 for path in paths {
191 let skill = parse_disk_skill(&path)?;
192 if let Some(first) = by_name.get(&skill.name) {
193 return Err(SkillDiscoveryError::DuplicateName {
194 name: skill.name,
195 first: first.path.clone(),
196 second: path,
197 });
198 }
199 by_name.insert(skill.name.clone(), skill);
200 }
201
202 Ok(by_name.into_values().collect())
203}
204
205fn parse_skill(source: &'static str) -> EmbeddedSkill {
206 let (frontmatter, body) = split_frontmatter(source);
207 let frontmatter = parse_frontmatter(frontmatter);
208 EmbeddedSkill {
209 name: frontmatter.name,
210 frontmatter,
211 body,
212 source,
213 }
214}
215
216fn collect_skill_paths(dir: &Path, out: &mut Vec<PathBuf>) -> Result<(), SkillDiscoveryError> {
217 let entries = fs::read_dir(dir).map_err(|source| SkillDiscoveryError::Io {
218 path: dir.to_path_buf(),
219 source,
220 })?;
221 for entry in entries {
222 let entry = entry.map_err(|source| SkillDiscoveryError::Io {
223 path: dir.to_path_buf(),
224 source,
225 })?;
226 let path = entry.path();
227 let file_type = entry
228 .file_type()
229 .map_err(|source| SkillDiscoveryError::Io {
230 path: path.clone(),
231 source,
232 })?;
233 if file_type.is_dir() {
234 collect_skill_paths(&path, out)?;
235 } else if file_type.is_file() && entry.file_name() == "SKILL.md" {
236 out.push(path);
237 }
238 }
239 Ok(())
240}
241
242fn parse_disk_skill(path: &Path) -> Result<DiskSkill, SkillDiscoveryError> {
243 let source = fs::read_to_string(path).map_err(|source| SkillDiscoveryError::Io {
244 path: path.to_path_buf(),
245 source,
246 })?;
247 let (frontmatter, body) =
248 split_disk_frontmatter(&source).ok_or_else(|| SkillDiscoveryError::MissingFrontmatter {
249 path: path.to_path_buf(),
250 })?;
251 let frontmatter = parse_disk_frontmatter(path, frontmatter)?;
252 Ok(DiskSkill {
253 name: frontmatter.name.clone(),
254 frontmatter,
255 body: body.to_string(),
256 source,
257 path: path.to_path_buf(),
258 })
259}
260
261fn split_disk_frontmatter(source: &str) -> Option<(&str, &str)> {
262 split_frontmatter_parts(source)
263}
264
265fn parse_disk_frontmatter(
266 path: &Path,
267 frontmatter: &str,
268) -> Result<DiskSkillFrontmatter, SkillDiscoveryError> {
269 let mut name = None;
270 let mut short = None;
271 let mut description = None;
272 let mut when_to_use = None;
273
274 for line in frontmatter.lines() {
275 let Some((key, value)) = line.split_once(':') else {
276 continue;
277 };
278 let value = value.trim().to_string();
279 match key {
280 "name" => name = Some(value),
281 "short" => short = Some(value),
282 "description" => description = Some(value),
283 "when_to_use" => when_to_use = Some(value),
284 _ => {}
285 }
286 }
287
288 Ok(DiskSkillFrontmatter {
289 name: require_disk_field(path, name, "name")?,
290 short: short.unwrap_or_default(),
291 description: require_disk_field(path, description, "description")?,
292 when_to_use,
293 })
294}
295
296fn require_disk_field(
297 path: &Path,
298 value: Option<String>,
299 field: &'static str,
300) -> Result<String, SkillDiscoveryError> {
301 value.ok_or_else(|| SkillDiscoveryError::MissingField {
302 path: path.to_path_buf(),
303 field,
304 })
305}
306
307fn split_frontmatter(source: &'static str) -> (&'static str, &'static str) {
308 let Some((after_open, line_ending)) = split_opening_frontmatter(source) else {
309 panic!("embedded skill source is missing opening frontmatter delimiter");
310 };
311 let Some((frontmatter, body)) = split_closing_frontmatter(after_open, line_ending) else {
312 panic!("embedded skill source is missing closing frontmatter delimiter");
313 };
314 (frontmatter, body)
315}
316
317fn split_frontmatter_parts(source: &str) -> Option<(&str, &str)> {
318 let (after_open, line_ending) = split_opening_frontmatter(source)?;
319 split_closing_frontmatter(after_open, line_ending)
320}
321
322fn split_opening_frontmatter(source: &str) -> Option<(&str, &str)> {
323 if let Some(after_open) = source.strip_prefix("---\n") {
324 Some((after_open, "\n"))
325 } else if let Some(after_open) = source.strip_prefix("---\r\n") {
326 Some((after_open, "\r\n"))
327 } else {
328 None
329 }
330}
331
332fn split_closing_frontmatter<'a>(
333 after_open: &'a str,
334 line_ending: &str,
335) -> Option<(&'a str, &'a str)> {
336 let close = format!("{line_ending}---{line_ending}");
337 let close_offset = after_open.find(&close)?;
338 Some((
339 &after_open[..close_offset],
340 &after_open[close_offset + close.len()..],
341 ))
342}
343
344fn parse_frontmatter(frontmatter: &'static str) -> SkillFrontmatter {
345 let mut name = None;
346 let mut short = None;
347 let mut description = None;
348 let mut when_to_use = None;
349
350 for line in frontmatter.lines() {
351 let Some((key, value)) = line.split_once(':') else {
352 continue;
353 };
354 let value = value.trim();
355 match key {
356 "name" => name = Some(value),
357 "short" => short = Some(value),
358 "description" => description = Some(value),
359 "when_to_use" => when_to_use = Some(value),
360 _ => {}
361 }
362 }
363
364 SkillFrontmatter {
365 name: name.expect("embedded skill frontmatter is missing `name`"),
366 short: short.expect("embedded skill frontmatter is missing `short`"),
367 description: description.expect("embedded skill frontmatter is missing `description`"),
368 when_to_use,
369 }
370}
371
372#[cfg(test)]
373mod tests {
374 use super::*;
375 use std::collections::BTreeSet;
376 use tempfile::TempDir;
377
378 #[test]
379 fn lists_expected_initial_corpus() {
380 let skills = list_embedded_skills();
381 let names: Vec<&str> = skills.iter().map(|skill| skill.name).collect();
382 assert_eq!(
383 names,
384 [
385 "harn-agent",
386 "harn-diagnostics",
387 "harn-language",
388 "harn-orchestration",
389 "harn-providers",
390 "harn-testing",
391 "harn-tracing",
392 ]
393 );
394 assert_eq!(skills.len(), SOURCES.len());
395 }
396
397 #[test]
398 fn can_fetch_harn_language_skill() {
399 let skill = get_embedded_skill("harn-language").expect("harn-language skill is embedded");
400 assert_eq!(skill.frontmatter.name, "harn-language");
401 assert!(skill.body.contains("Harn language"));
402 }
403
404 #[test]
405 fn skills_have_unique_names_and_body_only_content() {
406 let mut names = BTreeSet::new();
407 for skill in list_embedded_skills() {
408 assert_eq!(skill.name, skill.frontmatter.name);
409 assert!(names.insert(skill.name), "duplicate skill {}", skill.name);
410 assert!(
411 !skill.body.trim().is_empty(),
412 "{} body is empty",
413 skill.name
414 );
415 assert!(
416 !skill.body.trim_start().starts_with("---"),
417 "{} body includes frontmatter",
418 skill.name
419 );
420 }
421 }
422
423 #[test]
424 fn skills_are_sorted_by_name() {
425 let names: Vec<&str> = list_embedded_skills()
426 .iter()
427 .map(|skill| skill.name)
428 .collect();
429 let mut sorted = names.clone();
430 sorted.sort_unstable();
431 assert_eq!(names, sorted);
432 }
433
434 #[test]
435 fn source_round_trips_to_frontmatter_and_body() {
436 for skill in list_embedded_skills() {
437 assert!(
438 split_frontmatter_parts(skill.source).is_some(),
439 "{} source missing opening fence",
440 skill.name
441 );
442 assert!(
443 skill.source.ends_with(skill.body),
444 "{} source must end with the body so dump output is byte-stable",
445 skill.name
446 );
447 assert!(
448 skill.source.contains(&format!("name: {}\n", skill.name)),
449 "{} source missing canonical name field",
450 skill.name
451 );
452 }
453 }
454
455 #[test]
456 fn frontmatter_split_accepts_crlf_sources() {
457 let source = "---\r\nname: crlf\r\n---\r\n# Body\r\n";
458 let (frontmatter, body) = split_frontmatter_parts(source).expect("CRLF frontmatter");
459 assert_eq!(frontmatter, "name: crlf");
460 assert_eq!(body, "# Body\r\n");
461 }
462
463 #[test]
464 fn frontmatter_split_rejects_missing_closing_fence() {
465 assert!(split_frontmatter_parts("---\nname: missing\n# Body\n").is_none());
466 }
467
468 #[test]
469 fn embedded_corpus_stays_within_binary_budget() {
470 let bytes: usize = SOURCES.iter().map(|source| source.len()).sum();
471 assert!(
472 bytes <= 200 * 1024,
473 "embedded corpus is {bytes} bytes, expected <= 200 KiB"
474 );
475 }
476
477 #[test]
478 fn skill_bodies_are_focused_and_not_placeholders() {
479 let expectations = [
480 ("harn-agent", ["agent_loop", "session id", "approval"]),
481 ("harn-diagnostics", ["diagnostic", "repair", "conformance"]),
482 ("harn-language", ["quickref", "type", "conformance"]),
483 ("harn-orchestration", ["agent_loop", "workflow", "host"]),
484 ("harn-providers", ["llm_call", "provider", "schema"]),
485 (
486 "harn-testing",
487 ["conformance", "deterministic", "mock_time"],
488 ),
489 ("harn-tracing", ["replay", "receipts", "transcript"]),
490 ];
491
492 for (name, terms) in expectations {
493 let skill = get_embedded_skill(name).expect("expected embedded skill");
494 let body = skill.body.to_ascii_lowercase();
495 assert!(
496 !body.contains("embedded stub") && !body.contains("placeholder"),
497 "{name} should contain real guidance, not stub wording"
498 );
499 for term in terms {
500 assert!(
501 body.contains(term),
502 "{name} body should mention focused term `{term}`"
503 );
504 }
505 }
506 }
507
508 #[test]
509 fn skill_bodies_match_split_skill_contract() {
510 for skill in list_embedded_skills() {
511 let lines = skill.body.lines().count();
512 assert!(
513 lines >= 80,
514 "{} body is {lines} lines, expected at least 80",
515 skill.name
516 );
517 assert!(
518 lines <= 300,
519 "{} body is {lines} lines, expected at most 300",
520 skill.name
521 );
522 }
523 }
524
525 #[test]
526 fn skill_cross_links_resolve_to_embedded_skills() {
527 let names: BTreeSet<&str> = list_embedded_skills()
528 .iter()
529 .map(|skill| skill.name)
530 .collect();
531 for skill in list_embedded_skills() {
532 for reference in bracketed_skill_references(skill.body) {
533 assert!(
534 names.contains(reference),
535 "{} links to unknown embedded skill [[{}]]",
536 skill.name,
537 reference
538 );
539 }
540 }
541 }
542
543 #[test]
544 fn diagnostics_skill_mentions_all_code_categories() {
545 let skill = get_embedded_skill("harn-diagnostics").expect("diagnostics skill");
546 for category in [
547 "TYP", "PAR", "NAM", "CAP", "LLM", "ORC", "STD", "PRM", "MOD", "LNT", "FMT", "IMP",
548 "OWN", "RCV", "MAT",
549 ] {
550 assert!(
551 skill.body.contains(&format!("`{category}`")),
552 "harn-diagnostics should mention diagnostic category `{category}`"
553 );
554 }
555 }
556
557 #[test]
558 fn disk_discovery_finds_recursive_skill_files_sorted_by_name() {
559 let temp = TempDir::new().expect("temp dir");
560 write_skill(
561 &temp.path().join("zeta").join("SKILL.md"),
562 "zeta-skill",
563 "Zeta",
564 );
565 write_skill(
566 &temp.path().join("nested").join("alpha").join("SKILL.md"),
567 "alpha-skill",
568 "Alpha",
569 );
570
571 let skills = list_disk_skills(temp.path()).expect("discover disk skills");
572 let names: Vec<&str> = skills.iter().map(|skill| skill.name.as_str()).collect();
573 assert_eq!(names, ["alpha-skill", "zeta-skill"]);
574 assert_eq!(skills[0].frontmatter.description, "Alpha description");
575 assert!(skills[0].body.contains("Alpha body"));
576 }
577
578 #[test]
579 fn disk_discovery_treats_missing_root_as_empty() {
580 let temp = TempDir::new().expect("temp dir");
581 let skills = list_disk_skills(temp.path().join("missing")).expect("discover disk skills");
582 assert!(skills.is_empty());
583 }
584
585 #[test]
586 fn disk_discovery_rejects_duplicate_skill_names() {
587 let temp = TempDir::new().expect("temp dir");
588 write_skill(
589 &temp.path().join("one").join("SKILL.md"),
590 "same-skill",
591 "One",
592 );
593 write_skill(
594 &temp.path().join("two").join("SKILL.md"),
595 "same-skill",
596 "Two",
597 );
598
599 let error = list_disk_skills(temp.path()).expect_err("duplicate name should fail");
600 assert!(
601 error.to_string().contains("duplicate skill `same-skill`"),
602 "unexpected error: {error}"
603 );
604 }
605
606 fn write_skill(path: &Path, name: &str, label: &str) {
607 fs::create_dir_all(path.parent().expect("skill parent")).expect("create skill parent");
608 fs::write(
609 path,
610 format!(
611 "---\nname: {name}\nshort: {label} short\ndescription: {label} description\n---\n# {label}\n\n{label} body\n"
612 ),
613 )
614 .expect("write SKILL.md");
615 }
616
617 fn bracketed_skill_references(body: &str) -> Vec<&str> {
618 let mut references = Vec::new();
619 let mut rest = body;
620 while let Some(start) = rest.find("[[") {
621 rest = &rest[start + 2..];
622 let Some(end) = rest.find("]]") else {
623 break;
624 };
625 references.push(&rest[..end]);
626 rest = &rest[end + 2..];
627 }
628 references
629 }
630}