craftbag 0.1.0

Discover and load Agent Skills (SKILL.md) for CLI and MCP hosts
Documentation
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
//! Skip-kind taxonomy. Frozen after this PR.

use std::fmt::{self, Write};
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::skill::Skill;

/// Why a candidate `SKILL.md` was not loaded.
///
/// v1 set is frozen. Additions are a new versioned variant plus a corpus
/// fixture. Do not add `BudgetOmitted`, `Disabled`, `Ignored`,
/// `VendorDenylist`, or `InvocationOff` until they are real skip rows
/// with fixtures.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SkipKind {
    /// File could not be read.
    Unreadable,
    /// Frontmatter/body failed agentskills parse/validation.
    ParseError,
    /// Frontmatter `name` does not match parent directory.
    NameDirectoryMismatch,
    /// Same name already loaded from a higher-priority path (`winner_path` set).
    NameCollision,
    /// Loose `SKILL.md` in a skills root instead of `dir/<name>/SKILL.md`.
    RootFile,
}

impl SkipKind {
    /// Frozen v1 set, in declaration order.
    pub const fn all() -> [Self; 5] {
        [
            Self::Unreadable,
            Self::ParseError,
            Self::NameDirectoryMismatch,
            Self::NameCollision,
            Self::RootFile,
        ]
    }

    /// Wire name (`snake_case`, Bline parity).
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Unreadable => "unreadable",
            Self::ParseError => "parse_error",
            Self::NameDirectoryMismatch => "name_directory_mismatch",
            Self::NameCollision => "name_collision",
            Self::RootFile => "root_file",
        }
    }
}

impl fmt::Display for SkipKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A skill package that discovery found but did not load.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SkillSkip {
    /// Path to the `SKILL.md` (or unreadable path).
    pub path: PathBuf,
    /// Frontmatter name when parse got far enough; otherwise `None`.
    #[serde(default)]
    pub name: Option<String>,
    pub kind: SkipKind,
    /// Human-readable reason (parse error text, etc.).
    pub detail: String,
    /// Winning path when `kind` is [`SkipKind::NameCollision`].
    ///
    /// Serialize stays `winnerPath`. Deserialize also accepts
    /// `winner_path` so a `SkillMiss` row is not silently winner-less.
    #[serde(default, alias = "winner_path")]
    pub winner_path: Option<PathBuf>,
}

impl Serialize for SkillSkip {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        #[derive(Serialize)]
        #[serde(rename_all = "camelCase")]
        struct Wire<'a> {
            path: &'a Path,
            #[serde(skip_serializing_if = "Option::is_none")]
            name: &'a Option<String>,
            kind: SkipKind,
            code: &'a str,
            detail: &'a str,
            #[serde(skip_serializing_if = "Option::is_none")]
            winner_path: &'a Option<PathBuf>,
        }
        Wire {
            path: &self.path,
            name: &self.name,
            kind: self.kind,
            code: self.kind.as_str(),
            detail: &self.detail,
            winner_path: &self.winner_path,
        }
        .serialize(serializer)
    }
}

impl SkillSkip {
    /// Stable machine code (`kind.as_str()`).
    pub fn code(&self) -> &'static str {
        self.kind.as_str()
    }

    /// Whether `load`/`why` should treat this skip as the requested name.
    ///
    /// A non-blank frontmatter `name` is an identity. The `SKILL.md` parent
    /// directory is also an identity for every skip except [`SkipKind::RootFile`]
    /// (that parent is the skills root, not a package). Blank or
    /// whitespace-only peeked names fall through to the package directory.
    pub fn matches_requested_name(&self, want: &str) -> bool {
        let want = want.trim();
        if want.is_empty() {
            return false;
        }
        if crate::parse::is_path_component_skill_name(want) {
            return false;
        }
        if self.name.as_deref().is_some_and(|n| {
            !crate::parse::is_path_component_skill_name(n)
                && crate::parse::skill_names_equal(n, want)
        }) {
            return true;
        }
        if matches!(self.kind, SkipKind::RootFile) {
            return false;
        }
        skill_md_package_name(&self.path).is_some_and(|n| crate::parse::skill_names_equal(n, want))
    }

    /// True when discover refused a host `--path` / `paths` or
    /// `--user-dir` / `user_dir` token (collapse or line separator).
    ///
    /// That skip is not a package identity. Named `load` / `why`
    /// still peel it so a host sees WHAT and which flag to change.
    pub(crate) fn is_host_token_refuse(&self) -> bool {
        self.kind == SkipKind::Unreadable
            && self.name.is_none()
            && (self.detail.contains("collapses after whitespace trim")
                || self.detail.contains("line separator"))
    }
}

/// Parent directory of a `SKILL.md` / `skill.md` path, when that is the file name.
pub(crate) fn skill_md_package_name(path: &Path) -> Option<&str> {
    let file = path.file_name().and_then(|n| n.to_str())?;
    if !file.eq_ignore_ascii_case("SKILL.md") {
        return None;
    }
    crate::parse::skill_md_package_dir_name(path)
}

/// Result of multi-root skill discovery, including skips for `why`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DiscoveryReport {
    pub skills: Vec<Skill>,
    pub skips: Vec<SkillSkip>,
}

/// Default list TSV rows (`name\tsource\tpath`) for CLI `list`.
///
/// Path goes through [`crate::sanitize_error_token`] so a leftover
/// implicit package cannot split the row (U+2028). Extra-path refuse
/// already rejects a line-separator token; leftover implicit paths
/// do not.
pub fn format_list_tsv(skills: &[Skill]) -> String {
    let mut out = String::new();
    for skill in skills {
        let path = skill
            .source_path
            .as_ref()
            .map(|p| crate::sanitize_error_token(&p.display().to_string()))
            .unwrap_or_default();
        let _ = writeln!(
            out,
            "{}\t{}\t{}",
            crate::sanitize_error_token(&skill.name),
            skill.source.as_str(),
            path
        );
    }
    out
}

/// TSV skip rows (`skip\tkind\tpath\tdetail`) for CLI list stderr,
/// CLI why stdout, and MCP catalog/xml text (stdio has no stderr).
///
/// Path and detail go through [`crate::sanitize_error_token`] so a
/// leftover SKILL.md skip cannot split the row (U+2028) or leak an
/// em dash. Extra-path refuse already sanitizes `skip.path` at
/// construction; leftover implicit paths do not.
pub fn format_skip_tsv(skips: &[SkillSkip]) -> String {
    let mut out = String::new();
    for skip in skips {
        let _ = writeln!(
            out,
            "skip\t{}\t{}\t{}",
            skip.kind.as_str(),
            crate::sanitize_error_token(&skip.path.display().to_string()),
            crate::sanitize_error_token(&skip.detail)
        );
    }
    out
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use proptest::prelude::*;

    use super::{DiscoveryReport, SkillSkip, SkipKind};

    #[test]
    fn as_str_is_snake_case() {
        assert_eq!(SkipKind::Unreadable.as_str(), "unreadable");
        assert_eq!(SkipKind::ParseError.as_str(), "parse_error");
        assert_eq!(
            SkipKind::NameDirectoryMismatch.as_str(),
            "name_directory_mismatch"
        );
        assert_eq!(SkipKind::NameCollision.as_str(), "name_collision");
        assert_eq!(SkipKind::RootFile.as_str(), "root_file");
    }

    #[test]
    fn skill_skip_serializes_code_matching_kind() {
        for kind in SkipKind::all() {
            let skip = SkillSkip {
                path: PathBuf::from("/tmp/x/SKILL.md"),
                name: Some("x".to_owned()),
                kind,
                detail: "d".to_owned(),
                winner_path: None,
            };
            assert_eq!(skip.code(), kind.as_str());
            let json = serde_json::to_string(&skip).expect("ser");
            let v: serde_json::Value = serde_json::from_str(&json).expect("json");
            assert_eq!(v["code"].as_str(), Some(kind.as_str()), "json={json}");
            assert_eq!(v["kind"].as_str(), Some(kind.as_str()), "json={json}");
        }
    }

    #[test]
    fn serde_round_trip_and_wire_names() {
        for kind in SkipKind::all() {
            let json = serde_json::to_string(&kind).expect("ser");
            assert_eq!(json, format!("\"{}\"", kind.as_str()));
            let back: SkipKind = serde_json::from_str(&json).expect("de");
            assert_eq!(back, kind);
        }
    }

    #[test]
    fn taxonomy_is_exactly_five_v1_kinds() {
        let kinds = SkipKind::all();
        assert_eq!(kinds.len(), 5);
        // Exhaustive match is the freeze: a new variant fails to compile.
        for kind in kinds {
            match kind {
                SkipKind::Unreadable
                | SkipKind::ParseError
                | SkipKind::NameDirectoryMismatch
                | SkipKind::NameCollision
                | SkipKind::RootFile => {}
            }
        }
    }

    #[test]
    fn deferred_kinds_are_not_in_v1() {
        for wire in [
            "budget_omitted",
            "disabled",
            "ignored",
            "vendor_denylist",
            "invocation_off",
        ] {
            let json = format!("\"{wire}\"");
            assert!(
                serde_json::from_str::<SkipKind>(&json).is_err(),
                "v1 must reject {wire}"
            );
        }
    }

    #[test]
    fn serde_accepts_snake_winner_path() {
        // SkillMiss JSON uses `winner_path`. SkillSkip serialize uses
        // `winnerPath`. A host that feeds a miss row back into SkillSkip
        // must not stay winner-less (PR 213 leftover on skip wire).
        let json = r#"{"path":"/x/SKILL.md","kind":"name_collision","detail":"d","winner_path":"/a/foo/SKILL.md"}"#;
        let skip: SkillSkip = serde_json::from_str(json).expect("snake winner_path");
        assert_eq!(
            skip.winner_path.as_deref(),
            Some(std::path::Path::new("/a/foo/SKILL.md")),
            "snake winner_path must populate SkillSkip, not stay skipped None"
        );
        let out = serde_json::to_string(&skip).expect("ser");
        assert!(
            out.contains("winnerPath"),
            "SkillSkip serialize must still emit winnerPath: {out}"
        );
        assert!(
            !out.contains("winner_path"),
            "SkillSkip serialize must not switch to snake winner_path: {out}"
        );
    }

    /// SkillMiss `winner_path` must populate SkillSkip for any host string,
    /// not only `/a/foo/SKILL.md`. A path segment named `winner_path` would
    /// false-fail a `contains("winner_path")` serialize check.
    fn host_winner_path() -> impl Strategy<Value = String> {
        prop_oneof![
            Just(String::new()),
            Just("/a/foo/SKILL.md".to_owned()),
            Just("/tmp/winner_path/source_path/SKILL.md".to_owned()),
            Just("/home/user/.agents/skills/foo/SKILL.md".to_owned()),
            Just("/tmp/ünicode dir/技能/SKILL.md".to_owned()),
            Just(r"C:\Users\foo\SKILL.md".to_owned()),
            ".{1,64}",
            prop::collection::vec("[A-Za-z0-9._-]{1,10}", 1usize..5)
                .prop_map(|parts| format!("/{}/SKILL.md", parts.join("/"))),
        ]
    }

    proptest! {
        #[test]
        fn serde_snake_winner_path_for_any_host_path(
            winner in host_winner_path(),
            skip_path in host_winner_path(),
        ) {
            let json = serde_json::json!({
                "path": skip_path,
                "kind": "name_collision",
                "detail": "d",
                "winner_path": winner,
            })
            .to_string();
            let skip: SkillSkip = serde_json::from_str(&json).expect("snake winner_path");
            prop_assert_eq!(
                skip.winner_path.as_deref(),
                Some(std::path::Path::new(&winner))
            );
            prop_assert_eq!(skip.path.as_path(), std::path::Path::new(&skip_path));
            let out = serde_json::to_value(&skip).expect("ser");
            prop_assert_eq!(
                out.get("winnerPath").and_then(|v| v.as_str()),
                Some(winner.as_str())
            );
            prop_assert!(
                out.get("winner_path").is_none(),
                "SkillSkip serialize must still emit winnerPath: {out}"
            );
        }
    }

    #[test]
    fn skill_skip_keeps_winner_path() {
        let skip = SkillSkip {
            path: PathBuf::from("/tmp/b/foo/SKILL.md"),
            name: Some("foo".to_owned()),
            kind: SkipKind::NameCollision,
            detail: "already loaded".to_owned(),
            winner_path: Some(PathBuf::from("/tmp/a/foo/SKILL.md")),
        };
        let json = serde_json::to_string(&skip).expect("ser");
        assert!(json.contains("winnerPath"), "json={json}");
        assert!(json.contains("nameCollision") || json.contains("name_collision"));
        let back: SkillSkip = serde_json::from_str(&json).expect("de");
        assert_eq!(back, skip);
        assert_eq!(
            back.winner_path.as_deref(),
            Some(std::path::Path::new("/tmp/a/foo/SKILL.md"))
        );
    }

    #[test]
    fn host_token_refuse_is_not_a_package_identity() {
        let collapse = SkillSkip {
            path: PathBuf::from(" /.."),
            name: None,
            kind: SkipKind::Unreadable,
            detail: "--path / paths token collapses after whitespace trim".to_owned(),
            winner_path: None,
        };
        assert!(collapse.is_host_token_refuse());
        assert!(
            !collapse.matches_requested_name("demo"),
            "collapse token is not the skill name demo"
        );
        let other = SkillSkip {
            path: PathBuf::from("/tmp/demo/SKILL.md"),
            name: None,
            kind: SkipKind::Unreadable,
            detail: "Is a directory".to_owned(),
            winner_path: None,
        };
        assert!(!other.is_host_token_refuse());
    }

    #[test]
    fn matches_requested_name_uses_package_dir_when_nameless() {
        let parse = SkillSkip {
            path: PathBuf::from("/tmp/demo/SKILL.md"),
            name: None,
            kind: SkipKind::ParseError,
            detail: "missing required field: name".to_owned(),
            winner_path: None,
        };
        assert!(parse.matches_requested_name("DEMO"));
        assert!(!parse.matches_requested_name("other"));
        let root = SkillSkip {
            path: PathBuf::from("/tmp/.agents/skills/SKILL.md"),
            name: None,
            kind: SkipKind::RootFile,
            detail: "put the file in a named subdirectory.".to_owned(),
            winner_path: None,
        };
        assert!(!root.matches_requested_name("skills"));
        assert!(!root.matches_requested_name("   "));
        let named_root = SkillSkip {
            path: PathBuf::from("/tmp/.agents/skills/SKILL.md"),
            name: Some("loose".to_owned()),
            kind: SkipKind::RootFile,
            detail: "put the file in a named subdirectory.".to_owned(),
            winner_path: None,
        };
        assert!(
            named_root.matches_requested_name("loose"),
            "frontmatter name remains an identity on root_file skips"
        );
        assert!(named_root.matches_requested_name("LOOSE"));
        assert!(!named_root.matches_requested_name("skills"));
    }

    #[test]
    fn matches_requested_name_nfkc_dot_components_in_path() {
        let skip = SkillSkip {
            path: PathBuf::from("/tmp/wanted/evil/‥/SKILL.md"),
            name: None,
            kind: SkipKind::ParseError,
            detail: "missing required field: name".to_owned(),
            winner_path: None,
        };
        assert!(
            skip.matches_requested_name("wanted"),
            "package dir must collapse NFKC `..` like extra-path, not treat ‥ as the name"
        );
        assert!(!skip.matches_requested_name("evil"));
        assert!(!skip.matches_requested_name(".."));
        assert!(!skip.matches_requested_name("‥"));
    }

    #[test]
    fn matches_requested_name_nfkc_dot_peek_is_not_identity() {
        let skip = SkillSkip {
            path: PathBuf::from("/tmp/wanted/SKILL.md"),
            name: Some(".".to_owned()),
            kind: SkipKind::ParseError,
            detail: "name must be lowercase alphanumeric and hyphens only".to_owned(),
            winner_path: None,
        };
        assert!(
            skip.matches_requested_name("wanted"),
            "package dir remains the identity when the peek is an NFKC path component"
        );
        assert!(
            !skip.matches_requested_name("."),
            "NFKC `.` is a path component, not a skill name"
        );
        assert!(!skip.matches_requested_name("."));
    }

    #[test]
    fn matches_requested_name_blank_peeked_name_uses_package_dir() {
        let skip = SkillSkip {
            path: PathBuf::from("/tmp/demo/skill.md"),
            name: Some("  ".to_owned()),
            kind: SkipKind::ParseError,
            detail: "name must be lowercase alphanumeric and hyphens only".to_owned(),
            winner_path: None,
        };
        assert!(
            skip.matches_requested_name("demo"),
            "whitespace-only peek name is not an identity"
        );
        assert!(skip.matches_requested_name("DEMO"));
        assert!(!skip.matches_requested_name("   "));
    }

    #[test]
    fn matches_requested_name_trims_padded_peeked_name() {
        let skip = SkillSkip {
            path: PathBuf::from("/tmp/other/SKILL.md"),
            name: Some(" foo ".to_owned()),
            kind: SkipKind::ParseError,
            detail: "name must be lowercase alphanumeric and hyphens only".to_owned(),
            winner_path: None,
        };
        assert!(skip.matches_requested_name("foo"));
        assert!(
            skip.matches_requested_name("other"),
            "package dir remains an identity when the peeked name is different"
        );
    }

    #[test]
    fn matches_requested_name_named_and_nameless_keep_distinct_identities() {
        let named = SkillSkip {
            path: PathBuf::from("/tmp/other/SKILL.md"),
            name: Some("alpha".to_owned()),
            kind: SkipKind::ParseError,
            detail: "invalid YAML: name must be lowercase alphanumeric and hyphens only".to_owned(),
            winner_path: None,
        };
        let nameless = SkillSkip {
            path: PathBuf::from("/tmp/alpha/SKILL.md"),
            name: None,
            kind: SkipKind::ParseError,
            detail: "missing required field: name".to_owned(),
            winner_path: None,
        };
        assert!(named.matches_requested_name("alpha"));
        assert!(nameless.matches_requested_name("alpha"));
        assert!(
            named.matches_requested_name("other"),
            "named skip still matches its package dir so why/load do not call it unknown"
        );
        assert!(!nameless.matches_requested_name("other"));
    }

    #[test]
    fn matches_requested_name_parentdir_skill_md_uses_package_dir() {
        let skip = SkillSkip {
            path: PathBuf::from("/tmp/wanted/other/../SKILL.md"),
            name: None,
            kind: SkipKind::ParseError,
            detail: "missing required field: name".to_owned(),
            winner_path: None,
        };
        assert!(
            skip.matches_requested_name("wanted"),
            "wanted/other/../SKILL.md must be the wanted package, not other"
        );
        assert!(!skip.matches_requested_name("other"));
    }

    #[test]
    fn discovery_report_default_is_empty() {
        let report = DiscoveryReport::default();
        assert!(report.skills.is_empty());
        assert!(report.skips.is_empty());
        let json = serde_json::to_string(&report).expect("ser");
        let back: DiscoveryReport = serde_json::from_str(&json).expect("de");
        assert_eq!(back, report);
    }

    #[test]
    fn format_skip_tsv_leftover_hostile_detail_stays_one_row() {
        // Leftover analog of extra-path line-sep refuse (skip.path is
        // sanitized at construction). A leftover SKILL.md skip still
        // goes through format_skip_tsv raw. U+2028 / U+2014 in path or
        // detail must not split catalog/xml skip TSV (MCP appends the
        // same rows after the prompt fragment).
        assert_eq!(
            super::format_skip_tsv(&[]),
            "",
            "empty skips must not emit a skip row"
        );
        let skip = SkillSkip {
            path: PathBuf::from("/tmp/evil\u{2028}root/SKILL.md"),
            name: Some("loose".to_owned()),
            kind: SkipKind::RootFile,
            detail: "put the file\u{2028}in a named\u{2014}subdirectory.".to_owned(),
            winner_path: None,
        };
        let tsv = super::format_skip_tsv(&[skip]);
        assert!(
            tsv.starts_with("skip\troot_file\t"),
            "leftover skip must stay a TSV row: {tsv:?}"
        );
        assert_eq!(
            tsv.chars().filter(|&c| c == '\n').count(),
            1,
            "leftover skip TSV must stay one row: {tsv:?}"
        );
        assert!(
            !tsv.contains('\u{2028}') && !tsv.contains('\u{2014}'),
            "U+2028 / em dash must not leak into skip TSV: {tsv:?}"
        );
        assert!(
            tsv.contains("evil?root") && tsv.contains("named-subdirectory"),
            "hostile leftover path and detail must be sanitized in place: {tsv:?}"
        );
    }
}