rho-coding-agent 1.48.0

A lightweight agent harness inspired by Pi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//! Agent Skills discovery and SKILL.md parsing.
//!
//! Frontmatter follows the Agent Skills specification (name, description,
//! license, compatibility, metadata, allowed-tools) and is parsed with a real
//! YAML parser. `disable-model-invocation` is a Rho extension outside the
//! Agent Skills field set; it stays opt-in per skill and is enforced only by
//! prompt metadata injection.
//!
//! Precedence, highest first; the first source for a name wins and conflicts
//! are reported instead of hidden:
//!
//! 1. built-in skills
//! 2. loose user skills: `~/.rho/skills`, then `~/.agents/skills`
//! 3. loose project skills: nearest `.agents/skills` ancestor first
//! 4. project Agent Plugins skills: nearest `.agents/plugins` ancestor first
//! 5. user Agent Plugins skills: `~/.agents/plugins`

use std::path::{Path, PathBuf};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SkillSource {
    BuiltIn,
    Filesystem {
        skill_file: PathBuf,
        owner: Option<String>,
    },
}

impl SkillSource {
    fn file(skill_file: PathBuf) -> Self {
        Self::Filesystem {
            skill_file,
            owner: None,
        }
    }

    pub(crate) fn plugin(skill_file: PathBuf, plugin: String) -> Self {
        Self::Filesystem {
            skill_file,
            owner: Some(plugin),
        }
    }
}

impl std::fmt::Display for SkillSource {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BuiltIn => formatter.write_str("built in to rho"),
            Self::Filesystem {
                skill_file,
                owner: None,
            } => formatter.write_str(&crate::paths::display(skill_file)),
            Self::Filesystem {
                skill_file,
                owner: Some(owner),
            } => write!(
                formatter,
                "plugin {owner} ({})",
                crate::paths::display(skill_file.parent().unwrap_or(skill_file))
            ),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Skill {
    pub name: String,
    pub description: String,
    pub disable_model_invocation: bool,
    pub source: SkillSource,
    pub contents: String,
}

const BUILTIN_SKILLS: &[&str] = &[
    include_str!("builtin_skills/rho-config/SKILL.md"),
    include_str!("builtin_skills/rho-diagnostics/SKILL.md"),
    include_str!("builtin_skills/rho-agent-creator/SKILL.md"),
    include_str!("builtin_skills/rho-workflow-authoring/SKILL.md"),
];

pub fn discover(cwd: &Path) -> Vec<Skill> {
    let home = crate::paths::home_dir();
    discover_with_home(cwd, home.as_deref())
}

pub(crate) fn find_builtin(name: &str) -> Option<Skill> {
    builtin_skills()
        .into_iter()
        .find(|skill| skill.name == name)
}

pub fn discover_with_home(cwd: &Path, home: Option<&Path>) -> Vec<Skill> {
    let plugin_skills = crate::plugins::skills_by_precedence(cwd, home);
    discover_with_plugin_skills(cwd, home, plugin_skills)
}

pub(crate) fn discover_with_plugin_skills(
    cwd: &Path,
    home: Option<&Path>,
    plugin_skills: Vec<Skill>,
) -> Vec<Skill> {
    let mut roots = Vec::new();
    if let Some(home) = home {
        roots.extend(crate::paths::user_skill_dirs(home));
    }
    roots.extend(
        crate::workspace::project_ancestor_dirs(cwd)
            .into_iter()
            .rev()
            .map(|path| path.join(".agents").join("skills")),
    );

    // Candidates arrive in precedence order: built-ins, loose user, loose
    // project, then plugin skills (project plugins before user plugins).
    let mut candidates = builtin_skills();
    candidates.extend(
        roots
            .into_iter()
            .flat_map(|root| skill_paths(&root))
            .filter_map(|path| match read_skill(&path) {
                Ok(skill) => Some(skill),
                Err(error) => {
                    tracing::warn!(skill = %path.display(), error = %error, "skipping invalid skill");
                    None
                }
            }),
    );
    candidates.extend(plugin_skills);

    let mut skills: Vec<Skill> = Vec::with_capacity(candidates.len());
    for candidate in candidates {
        if let Some(winner) = skills.iter().find(|skill| skill.name == candidate.name) {
            tracing::warn!(
                skill = %candidate.name,
                selected = %winner.source,
                ignored = %candidate.source,
                "duplicate skill name; keeping the higher-precedence source"
            );
            continue;
        }
        skills.push(candidate);
    }
    // Sort after precedence/dedup so every presentation surface shares one order.
    skills.sort_by(|left, right| {
        left.name
            .to_ascii_lowercase()
            .cmp(&right.name.to_ascii_lowercase())
    });
    skills
}

fn skill_paths(root: &Path) -> Vec<PathBuf> {
    let Ok(entries) = std::fs::read_dir(root) else {
        return Vec::new();
    };

    let mut paths: Vec<_> = entries
        .filter_map(Result::ok)
        .filter_map(|entry| {
            let path = entry.path();
            if path.is_dir() {
                Some(path.join("SKILL.md"))
            } else {
                None
            }
        })
        .collect();
    paths.sort();
    paths
}

fn read_skill(path: &Path) -> anyhow::Result<Skill> {
    let contents = std::fs::read_to_string(path)?;
    parse_skill(&contents, SkillSource::file(path.to_path_buf()), Some(path))
}

fn builtin_skills() -> Vec<Skill> {
    BUILTIN_SKILLS
        .iter()
        .map(|contents| read_builtin_skill(contents).expect("embedded skills must be valid"))
        .collect()
}

fn read_builtin_skill(contents: &str) -> anyhow::Result<Skill> {
    parse_skill(contents, SkillSource::BuiltIn, None)
}

/// Parse and validate one SKILL.md document.
///
/// `skill_path` enables the Agent Skills rule that `name` must match the
/// containing directory; built-in skills have no directory and skip it.
pub(crate) fn parse_skill(
    contents: &str,
    source: SkillSource,
    skill_path: Option<&Path>,
) -> anyhow::Result<Skill> {
    let block = frontmatter_block(contents)?;
    let frontmatter: SkillFrontmatter = serde_yaml_ng::from_str(&block)
        .map_err(|error| anyhow::anyhow!("invalid SKILL.md frontmatter: {error}"))?;
    validate_frontmatter(&frontmatter)?;
    if let Some(path) = skill_path {
        let directory_name = path
            .parent()
            .and_then(Path::file_name)
            .and_then(|name| name.to_str())
            .ok_or_else(|| anyhow::anyhow!("missing skill directory name"))?;
        if frontmatter.name != directory_name {
            anyhow::bail!("skill name must match directory name");
        }
    }

    Ok(Skill {
        name: frontmatter.name,
        description: frontmatter.description,
        disable_model_invocation: frontmatter
            .disable_model_invocation
            .map(|flag| flag.0)
            .unwrap_or(false),
        source,
        contents: contents.into(),
    })
}

/// The YAML block between the opening and closing `---` frontmatter fences.
fn frontmatter_block(contents: &str) -> anyhow::Result<String> {
    let mut lines = contents.lines();
    if lines.next() != Some("---") {
        anyhow::bail!("SKILL.md must start with YAML frontmatter");
    }
    let mut block = Vec::new();
    for line in lines {
        if line == "---" {
            return Ok(block.join("\n"));
        }
        block.push(line.to_string());
    }
    anyhow::bail!("unterminated YAML frontmatter")
}

/// Typed Agent Skills frontmatter plus Rho's `disable-model-invocation`
/// extension. Unknown fields are ignored, matching prior behavior and the
/// ecosystem convention that clients may add their own frontmatter fields.
#[derive(serde::Deserialize)]
struct SkillFrontmatter {
    name: String,
    description: String,
    #[expect(dead_code, reason = "type-validated only")]
    #[serde(default)]
    license: Option<String>,
    #[serde(default)]
    compatibility: Option<String>,
    #[expect(dead_code, reason = "type-validated only")]
    #[serde(rename = "allowed-tools", default)]
    allowed_tools: Option<String>,
    #[serde(rename = "disable-model-invocation", default)]
    disable_model_invocation: Option<TolerantBool>,
}

/// Accepts YAML booleans plus the legacy string forms `"true"`/`"false"`
/// (case-insensitive) that the earlier hand-written parser tolerated.
#[derive(Clone, Copy)]
struct TolerantBool(bool);

impl<'de> serde::Deserialize<'de> for TolerantBool {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct Visitor;

        impl<'de> serde::de::Visitor<'de> for Visitor {
            type Value = TolerantBool;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("a boolean or the string \"true\"/\"false\"")
            }

            fn visit_bool<E: serde::de::Error>(self, value: bool) -> Result<TolerantBool, E> {
                Ok(TolerantBool(value))
            }

            fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<TolerantBool, E> {
                match value.to_ascii_lowercase().as_str() {
                    "true" => Ok(TolerantBool(true)),
                    "false" => Ok(TolerantBool(false)),
                    _ => Err(serde::de::Error::custom(
                        "disable-model-invocation must be true or false",
                    )),
                }
            }
        }

        deserializer.deserialize_any(Visitor)
    }
}

/// Agent Skills field constraints, separate from YAML syntax parsing.
fn validate_frontmatter(frontmatter: &SkillFrontmatter) -> anyhow::Result<()> {
    validate_name(&frontmatter.name)?;
    let description_chars = frontmatter.description.chars().count();
    if description_chars == 0 || description_chars > 1024 {
        anyhow::bail!("skill description must be 1-1024 characters");
    }
    if let Some(compatibility) = &frontmatter.compatibility {
        let compatibility_chars = compatibility.chars().count();
        if compatibility_chars == 0 || compatibility_chars > 500 {
            anyhow::bail!("skill compatibility must be 1-500 characters");
        }
    }
    // `license` and `allowed-tools` (space-separated tool list) are constrained
    // by their deserialized types. `metadata` and other client fields are ignored.
    Ok(())
}

fn validate_name(name: &str) -> anyhow::Result<()> {
    if name.is_empty() || name.len() > 64 {
        anyhow::bail!("skill name must be 1-64 characters");
    }
    let bytes = name.as_bytes();
    if bytes.first() == Some(&b'-') || bytes.last() == Some(&b'-') || name.contains("--") {
        anyhow::bail!("skill name must use single hyphen separators");
    }
    if !bytes
        .iter()
        .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || *byte == b'-')
    {
        anyhow::bail!("skill name must be lowercase alphanumeric with hyphen separators");
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use tempfile::TempDir;

    use super::*;

    #[test]
    fn discovers_embedded_rho_config_skill() {
        let root = TempDir::new().unwrap();

        let skills = discover_with_home(root.path(), None);
        let skill = skills
            .iter()
            .find(|skill| skill.name == "rho-config")
            .unwrap();

        assert_eq!(skill.source, SkillSource::BuiltIn);
        assert!(skill.contents.contains("config.toml"));
    }

    #[test]
    fn discovers_embedded_rho_diagnostics_skill() {
        let root = TempDir::new().unwrap();

        let skills = discover_with_home(root.path(), None);
        let skill = skills
            .iter()
            .find(|skill| skill.name == "rho-diagnostics")
            .unwrap();

        assert_eq!(skill.source, SkillSource::BuiltIn);
        assert!(skill.contents.contains("Available actions:"));
    }

    #[test]
    fn discovers_embedded_rho_agent_creator_skill() {
        let root = TempDir::new().unwrap();

        let skills = discover_with_home(root.path(), None);
        let skill = skills
            .iter()
            .find(|skill| skill.name == "rho-agent-creator")
            .unwrap();

        assert_eq!(skill.source, SkillSource::BuiltIn);
        assert!(skill.contents.contains("questionnaire"));
    }

    #[test]
    fn parses_disable_model_invocation() {
        let root = TempDir::new().unwrap();
        let skill_dir = root.path().join(".agents/skills/manual-skill");
        std::fs::create_dir_all(&skill_dir).unwrap();
        std::fs::write(
            skill_dir.join("SKILL.md"),
            "---\nname: manual-skill\ndescription: manual skill\ndisable-model-invocation: true\n---\nrules\n",
        )
        .unwrap();

        let skill = discover_with_home(root.path(), None)
            .into_iter()
            .find(|skill| skill.name == "manual-skill")
            .unwrap();

        assert!(skill.disable_model_invocation);
    }

    #[test]
    fn discovers_valid_skills_in_order() {
        let home = TempDir::new().unwrap();
        let project = TempDir::new().unwrap();
        write_skill(
            home.path(),
            ".rho/skills/rho-skill",
            "rho-skill",
            "rho desc",
        );
        write_skill(
            home.path(),
            ".agents/skills/agent-skill",
            "agent-skill",
            "agent desc",
        );
        write_skill(
            project.path(),
            ".agents/skills/project-skill",
            "project-skill",
            "project desc",
        );

        let skills = discover_with_home(project.path(), Some(home.path()));

        let names: Vec<_> = skills.iter().map(|skill| skill.name.as_str()).collect();
        assert_eq!(
            names,
            [
                "agent-skill",
                "project-skill",
                "rho-agent-creator",
                "rho-config",
                "rho-diagnostics",
                "rho-skill",
                "rho-workflow-authoring",
            ]
        );
    }

    #[test]
    fn discovers_project_skills_from_ancestor_directories() {
        let home = TempDir::new().unwrap();
        let project = TempDir::new().unwrap();
        let child = project.path().join("src/nested");
        std::fs::create_dir_all(&child).unwrap();
        std::fs::create_dir(project.path().join(".git")).unwrap();
        write_skill(
            project.path(),
            ".agents/skills/project-skill",
            "project-skill",
            "project desc",
        );

        let skills = discover_with_home(&child, Some(home.path()));

        assert!(skills.iter().any(|skill| skill.name == "project-skill"));
    }

    #[test]
    fn prefers_nearest_project_skill_when_names_duplicate() {
        let home = TempDir::new().unwrap();
        let project = TempDir::new().unwrap();
        let child = project.path().join("src/nested");
        std::fs::create_dir_all(&child).unwrap();
        std::fs::create_dir(project.path().join(".git")).unwrap();
        write_skill(
            project.path(),
            ".agents/skills/dup-skill",
            "dup-skill",
            "parent desc",
        );
        write_skill(
            &child,
            ".agents/skills/dup-skill",
            "dup-skill",
            "child desc",
        );

        let skills = discover_with_home(&child, Some(home.path()));

        let skill = skills
            .iter()
            .find(|skill| skill.name == "dup-skill")
            .unwrap();
        assert_eq!(skill.description, "child desc");
    }

    #[test]
    fn rejects_invalid_discovered_skills() {
        let cases = [
            ("bad-skill", "# bad", "bad-skill"),
            (
                "dir-name",
                "---\nname: other-name\ndescription: desc\n---\n",
                "other-name",
            ),
            (
                "bad--skill",
                "---\nname: bad--skill\ndescription: desc\n---\n",
                "bad--skill",
            ),
            (
                "bad-skill",
                "---\nname: bad-skill\ndescription: \n---\n",
                "bad-skill",
            ),
        ];

        for (directory, contents, rejected) in cases {
            let root = TempDir::new().unwrap();
            let skill_dir = root.path().join(".rho/skills").join(directory);
            std::fs::create_dir_all(&skill_dir).unwrap();
            std::fs::write(skill_dir.join("SKILL.md"), contents).unwrap();

            let skills = discover_with_home(root.path(), Some(root.path()));
            assert_only_builtins_excluding(&skills, rejected);
        }
    }

    #[test]
    fn skips_duplicate_skill_names_after_first_match() {
        let home = TempDir::new().unwrap();
        let project = TempDir::new().unwrap();
        write_skill(
            home.path(),
            ".rho/skills/dup-skill",
            "dup-skill",
            "first desc",
        );
        write_skill(
            home.path(),
            ".agents/skills/dup-skill",
            "dup-skill",
            "second desc",
        );

        let skills = discover_with_home(project.path(), Some(home.path()));

        let duplicates: Vec<_> = skills
            .iter()
            .filter(|skill| skill.name == "dup-skill")
            .collect();
        assert_eq!(duplicates.len(), 1);
        assert_eq!(duplicates[0].description, "first desc");
    }

    /// Table-driven frontmatter coverage for the YAML parser and the Agent
    /// Skills validation pass.
    #[test]
    fn parses_frontmatter_variants() {
        #[derive(Debug)]
        struct Case {
            name: &'static str,
            frontmatter: &'static str,
            expected: Result<Expected, &'static str>,
        }

        #[derive(Debug)]
        struct Expected {
            name: &'static str,
            description: &'static str,
            disable_model_invocation: bool,
        }

        let cases = [
            Case {
                name: "double quoted with escapes",
                frontmatter: r#"name: quote-skill
description: "a \"quoted\" description"
"#,
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "a \"quoted\" description",
                    disable_model_invocation: false,
                }),
            },
            Case {
                name: "single quoted",
                frontmatter: "name: 'quote-skill'\ndescription: 'plain'\n",
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "plain",
                    disable_model_invocation: false,
                }),
            },
            Case {
                name: "trailing comments",
                frontmatter: "name: quote-skill # the name\ndescription: desc # the desc\n",
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "desc",
                    disable_model_invocation: false,
                }),
            },
            Case {
                name: "folded multiline description",
                frontmatter: "name: quote-skill\ndescription: >-\n  first\n  second\n",
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "first second",
                    disable_model_invocation: false,
                }),
            },
            Case {
                name: "literal multiline description",
                frontmatter: "name: quote-skill\ndescription: |-\n  first\n  second\n",
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "first\nsecond",
                    disable_model_invocation: false,
                }),
            },
            Case {
                name: "nested metadata and optional fields",
                frontmatter: "name: quote-skill\ndescription: desc\nlicense: MIT\ncompatibility: needs git\nmetadata:\n  author: example-org\n  version: \"1.0\"\nallowed-tools: Bash(git:*) Read\n",
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "desc",
                    disable_model_invocation: false,
                }),
            },
            Case {
                name: "disable-model-invocation string legacy form",
                frontmatter: "name: quote-skill\ndescription: desc\ndisable-model-invocation: \"True\"\n",
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "desc",
                    disable_model_invocation: true,
                }),
            },
            Case {
                name: "unknown fields ignored",
                frontmatter: "name: quote-skill\ndescription: desc\nsome-client-field: 1\n",
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "desc",
                    disable_model_invocation: false,
                }),
            },
            Case {
                name: "missing description",
                frontmatter: "name: quote-skill\n",
                expected: Err("description"),
            },
            Case {
                name: "nested metadata mapping is accepted",
                frontmatter: "name: quote-skill\ndescription: desc\nmetadata:\n  requires:\n    bins: [\"bitbucket-cli\"]\n",
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "desc",
                    disable_model_invocation: false,
                }),
            },
            Case {
                name: "numeric metadata scalar is accepted",
                frontmatter: "name: quote-skill\ndescription: desc\nmetadata:\n  version: 1.0\n",
                expected: Ok(Expected {
                    name: "quote-skill",
                    description: "desc",
                    disable_model_invocation: false,
                }),
            },
            Case {
                name: "invalid disable-model-invocation type",
                frontmatter: "name: quote-skill\ndescription: desc\ndisable-model-invocation: 3\n",
                expected: Err("invalid type"),
            },
            Case {
                name: "invalid compatibility type",
                frontmatter: "name: quote-skill\ndescription: desc\ncompatibility: [git]\n",
                expected: Err("invalid type"),
            },
        ];
        for case in cases {
            let contents = format!("---\n{}---\n# body\n", case.frontmatter);
            let result = parse_skill(&contents, SkillSource::BuiltIn, None);
            match &case.expected {
                Ok(expected) => {
                    let skill = result.unwrap_or_else(|error| {
                        panic!("{}: expected success, got {error}", case.name)
                    });
                    assert_eq!(skill.name, expected.name, "{}", case.name);
                    assert_eq!(skill.description, expected.description, "{}", case.name);

                    assert_eq!(
                        skill.disable_model_invocation, expected.disable_model_invocation,
                        "{}",
                        case.name
                    );
                }
                Err(fragment) => {
                    let error = result
                        .err()
                        .unwrap_or_else(|| panic!("{}: expected failure", case.name));
                    assert!(
                        error.to_string().contains(fragment),
                        "{}: error `{error}` should mention `{fragment}`",
                        case.name
                    );
                }
            }
        }
    }

    #[test]
    fn rejects_overlong_compatibility() {
        let compatibility = "aaaaaaaaaa ".repeat(60);
        assert!(compatibility.chars().count() > 500);
        let contents = format!(
            "---\nname: quote-skill\ndescription: desc\ncompatibility: {compatibility}\n---\n"
        );
        let error = parse_skill(&contents, SkillSource::BuiltIn, None).unwrap_err();
        assert!(error.to_string().contains("compatibility"));
    }

    /// Asserts the discovered skills are exactly the built-ins, minus the one
    /// rejected skill. Derived from `builtin_skills()` so this stays correct
    /// as built-ins are added or removed without editing every rejection test.
    fn assert_only_builtins_excluding(skills: &[Skill], rejected: &str) {
        let mut expected: Vec<_> = builtin_skills()
            .into_iter()
            .filter(|skill| skill.name != rejected)
            .map(|skill| skill.name)
            .collect();
        expected.sort_by_key(|a| a.to_ascii_lowercase());

        let mut actual: Vec<_> = skills.iter().map(|skill| skill.name.as_str()).collect();
        actual.sort_by_key(|a| a.to_ascii_lowercase());

        assert_eq!(actual, expected);
    }

    fn write_skill(root: &Path, relative_dir: &str, name: &str, description: &str) {
        let skill_dir = root.join(relative_dir);
        std::fs::create_dir_all(&skill_dir).unwrap();
        std::fs::write(
            skill_dir.join("SKILL.md"),
            format!("---\nname: {name}\ndescription: {description}\n---\n# {name}\n"),
        )
        .unwrap();
    }
}