skillcask 0.5.0

A lockfile-first package manager for AI agent skills (SKILL.md), backed by Composer repositories
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
//! SKILL.md frontmatter, skill names and the tree hash.

use std::collections::BTreeMap;
use std::path::Path;

use sha2::{Digest, Sha256};

use crate::bail;
use crate::error::{IO, Result};

pub const SKILL_MD: &str = "SKILL.md";

/// Top-level scalar keys of the YAML frontmatter; nested values are skipped. No closing `---`:
/// no frontmatter.
pub fn frontmatter(text: &str) -> BTreeMap<String, String> {
    let text = text.strip_prefix('\u{feff}').unwrap_or(text);
    let lines: Vec<&str> = text.split_inclusive('\n').collect();
    fields(&lines).unwrap_or_default().into_iter().filter_map(|f| Some((f.key.to_string(), f.value?))).collect()
}

/// Sets the frontmatter `name:` value, keeping every other byte. No frontmatter or no `name:`
/// line: unchanged, since the directory name then names the skill. Picks the key `frontmatter`
/// reads (the last top-level `name`) and replaces all lines of its value.
pub fn rename_frontmatter(text: &str, to: &str) -> String {
    let body = text.strip_prefix('\u{feff}').unwrap_or(text);
    let bom = &text[..text.len() - body.len()];
    let lines: Vec<&str> = body.split_inclusive('\n').collect();
    let Some(f) = fields(&lines).and_then(|f| f.into_iter().rev().find(|f| f.key == "name")) else {
        return text.to_string();
    };
    let first = lines[f.lines.start];
    let eol = &first[first.trim_end_matches(['\n', '\r']).len()..];
    format!("{bom}{}name: {to}{eol}{}", lines[..f.lines.start].concat(), lines[f.lines.end..].concat())
}

/// The top-level `requires:` block mapping, key to value as written (quotes and comments
/// removed); empty without one. Values are checked by `deps::check`.
pub fn requires(text: &str) -> std::result::Result<BTreeMap<String, String>, String> {
    let text = text.strip_prefix('\u{feff}').unwrap_or(text);
    let lines: Vec<&str> = text.split_inclusive('\n').collect();
    let Some(f) = fields(&lines).and_then(|f| f.into_iter().rev().find(|f| f.key == "requires")) else {
        return Ok(BTreeMap::new());
    };
    let line = |l: &'_ str| l.trim_end_matches(['\n', '\r']).to_string();
    let inline = line(lines[f.lines.start]).split_once(':').map_or(String::new(), |(_, v)| v.trim().to_string());
    if !inline.is_empty() && !inline.starts_with('#') {
        return Err("requires: must be a block mapping of package to constraint".into());
    }
    let mut out = BTreeMap::new();
    let mut indent = None;
    for l in &lines[f.lines.start + 1..f.lines.end] {
        let l = line(l);
        let t = l.trim_start();
        if t.is_empty() || t.starts_with('#') {
            continue;
        }
        if *indent.get_or_insert(l.len() - t.len()) != l.len() - t.len() {
            return Err(format!("requires: unexpected nesting at {t:?}"));
        }
        let (k, v) = entry(t).ok_or_else(|| format!("requires: expected `package: constraint`, got {t:?}"))?;
        if out.insert(k.clone(), v).is_some() {
            return Err(format!("requires: {k} appears twice"));
        }
    }
    Ok(out)
}

/// `key: value` of one mapping line; either side may be quoted.
fn entry(t: &str) -> Option<(String, String)> {
    let unquote = |s: &str| -> Option<(String, usize)> {
        let q = s.chars().next().filter(|c| matches!(c, '"' | '\''))?;
        let n = closing_quote(&s[1..], q)?;
        Some((s[1..=n].to_string(), n + 2))
    };
    let (key, rest) = match unquote(t) {
        Some((k, n)) => (k, t[n..].strip_prefix(':')?),
        // `github:owner/repo` holds a colon; only `: ` or a final `:` ends a plain key.
        None => match t.find(": ") {
            Some(n) => (t[..n].to_string(), &t[n + 1..]),
            None => (t.strip_suffix(':')?.to_string(), ""),
        },
    };
    let rest = rest.trim();
    let value = match unquote(rest) {
        Some((v, _)) => v,
        None => {
            let cut = rest.char_indices().find(|&(n, c)| c == '#' && rest[..n].ends_with(char::is_whitespace));
            rest[..cut.map_or(rest.len(), |(n, _)| n)].trim().to_string()
        }
    };
    (!key.trim().is_empty() && !value.is_empty()).then(|| (key.trim().to_string(), value))
}

struct Field<'a> {
    key: &'a str,
    /// `None` when empty, nested or an unterminated quote.
    value: Option<String>,
    lines: std::ops::Range<usize>,
}

/// Top-level keys of the frontmatter in `lines` (with line ends); `None` unless it opens and
/// closes with `---`. Understands the YAML that decides which line is a key: quoted scalars
/// spanning lines, block scalars and comments.
fn fields<'a>(lines: &[&'a str]) -> Option<Vec<Field<'a>>> {
    let line = |i: usize| lines.get(i).map(|l| l.trim_end_matches(['\n', '\r']));
    let is_end = |l: &str| l.trim_end() == "---";
    let indented = |l: &str| l.starts_with([' ', '\t']) || l.trim().is_empty();
    if !line(0).is_some_and(is_end) {
        return None;
    }
    let mut out = Vec::new();
    let mut i = 1;
    loop {
        let l = line(i)?;
        if is_end(l) {
            return Some(out);
        }
        let start = i;
        i += 1;
        let Some((key, v)) = l.split_once(':').filter(|_| !l.starts_with([' ', '\t', '#', '-'])) else { continue };
        let v = v.trim_start();
        let mut value = None;
        if let Some(q) = v.chars().next().filter(|c| matches!(c, '"' | '\'')) {
            let (mut parts, mut rest) = (Vec::new(), &v[1..]);
            loop {
                if let Some(n) = closing_quote(rest, q) {
                    parts.push(rest[..n].trim());
                    value = Some(parts.join(" "));
                    break;
                }
                parts.push(rest.trim());
                match line(i) {
                    Some(l) if !is_end(l) => (rest, i) = (l, i + 1),
                    _ => break,
                }
            }
        }
        let body = i;
        while line(i).is_some_and(indented) {
            i += 1;
        }
        while i > body && line(i - 1).is_some_and(|l| l.trim().is_empty()) {
            i -= 1;
        }
        if v.starts_with(['>', '|']) {
            let sep = if v.starts_with('|') { "\n" } else { " " };
            value = Some((body..i).filter_map(line).map(str::trim).collect::<Vec<_>>().join(sep));
        } else if !v.starts_with(['"', '\'']) {
            let plain = |v: &'a str| {
                let cut = v.char_indices().find(|&(n, c)| c == '#' && v[..n].chars().next_back().is_none_or(char::is_whitespace));
                v[..cut.map_or(v.len(), |(n, _)| n)].trim()
            };
            // Indented lines continue a plain scalar, folded with spaces, unless they hold a mapping,
            // a sequence or a comment: then the value is nested.
            let more: Vec<&str> = (body..i).filter_map(line).map(str::trim).filter(|l| !l.is_empty()).collect();
            let nested =
                more.iter().any(|l| l.starts_with('#') || l.starts_with("- ") || *l == "-" || l.contains(": ") || l.ends_with(':'));
            if !nested {
                value = Some(
                    std::iter::once(plain(v)).chain(more.into_iter().map(plain)).filter(|s| !s.is_empty()).collect::<Vec<_>>().join(" "),
                );
            }
        }
        out.push(Field { key: key.trim(), value: value.filter(|v| !v.is_empty()), lines: start..i });
    }
}

fn closing_quote(s: &str, q: char) -> Option<usize> {
    let mut chars = s.char_indices().peekable();
    while let Some((n, c)) = chars.next() {
        if q == '"' && c == '\\' {
            chars.next();
        } else if c == q {
            if q == '\'' && chars.next_if(|&(_, c)| c == '\'').is_some() {
                continue;
            }
            return Some(n);
        }
    }
    None
}

/// `[a-z0-9][a-z0-9-]*`: also the directory name under `.agents/skills/`.
pub fn valid_name(n: &str) -> bool {
    n.starts_with(|c: char| c.is_ascii_lowercase() || c.is_ascii_digit())
        && n.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
}

/// Problems with a SKILL.md under the Agent Skills specification (agentskills.io) for a skill in
/// the directory `dir_name`. Stricter than `valid_name`, which still installs names such as `a--b`.
pub fn spec_problems(text: &str, dir_name: &str) -> Vec<String> {
    let body = text.strip_prefix('\u{feff}').unwrap_or(text);
    if fields(&body.split_inclusive('\n').collect::<Vec<_>>()).is_none() {
        return vec![format!("{SKILL_MD} has no frontmatter (--- ... ---)")];
    }
    let fm = frontmatter(text);
    let mut out = Vec::new();
    match fm.get("name") {
        None => out.push("name is required".into()),
        Some(n) if n.len() > 64 || !valid_name(n) || n.ends_with('-') || n.contains("--") => {
            out.push(format!("name {n:?} must be 1-64 lowercase letters, digits and single hyphens, not starting or ending with a hyphen"))
        }
        Some(n) if n != dir_name => out.push(format!("name {n:?} does not match the directory name {dir_name:?}")),
        Some(_) => {}
    }
    let mut length = |key: &str, max: usize, required: bool| match fm.get(key).map(|v| v.chars().count()) {
        None if required => out.push(format!("{key} is required")),
        Some(n) if n > max => out.push(format!("{key} has {n} characters, at most {max}")),
        _ => {}
    };
    length("description", 1024, true);
    length("compatibility", 500, false);
    out
}

/// Most invisible characters reported per file; the rest are counted.
const INVISIBLE_SHOWN: usize = 5;

/// An invisible or reordering character: the agent reads text a reviewer does not see.
#[derive(Debug, PartialEq)]
pub struct Invisible {
    /// 1-based.
    pub line: usize,
    pub ch: char,
    pub name: &'static str,
}

fn invisible_name(c: char) -> Option<&'static str> {
    Some(match c {
        '\u{061C}' => "ARABIC LETTER MARK",
        '\u{200B}' => "ZERO WIDTH SPACE",
        '\u{200C}' => "ZERO WIDTH NON-JOINER",
        '\u{200D}' => "ZERO WIDTH JOINER",
        '\u{200E}' => "LEFT-TO-RIGHT MARK",
        '\u{200F}' => "RIGHT-TO-LEFT MARK",
        '\u{202A}' => "LEFT-TO-RIGHT EMBEDDING",
        '\u{202B}' => "RIGHT-TO-LEFT EMBEDDING",
        '\u{202C}' => "POP DIRECTIONAL FORMATTING",
        '\u{202D}' => "LEFT-TO-RIGHT OVERRIDE",
        '\u{202E}' => "RIGHT-TO-LEFT OVERRIDE",
        '\u{2060}' => "WORD JOINER",
        '\u{2066}' => "LEFT-TO-RIGHT ISOLATE",
        '\u{2067}' => "RIGHT-TO-LEFT ISOLATE",
        '\u{2068}' => "FIRST STRONG ISOLATE",
        '\u{2069}' => "POP DIRECTIONAL ISOLATE",
        '\u{FEFF}' => "ZERO WIDTH NO-BREAK SPACE",
        '\u{E0001}' => "LANGUAGE TAG",
        '\u{E007F}' => "CANCEL TAG",
        '\u{E0000}'..='\u{E007E}' => "TAG CHARACTER",
        _ => return None,
    })
}

/// Bidi controls, zero-width characters and Unicode tags in `text`, except a leading byte order
/// mark and a zero width joiner between two emoji (`👩‍💻`).
pub fn invisible(text: &str) -> Vec<Invisible> {
    let emoji = |c: Option<char>| c.is_some_and(|c| matches!(c, '\u{2600}'..='\u{27BF}' | '\u{FE0F}' | '\u{1F000}'..='\u{1FAFF}'));
    let mut out = Vec::new();
    let mut line = 1;
    let mut prev = None;
    let mut chars = text.strip_prefix('\u{feff}').unwrap_or(text).chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\n' {
            line += 1;
        } else if let Some(name) = invisible_name(c)
            && !(c == '\u{200D}' && emoji(prev) && emoji(chars.peek().copied()))
        {
            out.push(Invisible { line, ch: c, name });
        }
        prev = Some(c);
    }
    out
}

/// `<path>:<line>: U+XXXX NAME` per invisible character in a file's `bytes`, a few per file, then
/// a count of the rest. Not UTF-8 means binary: nothing.
pub fn invisible_warnings(path: &str, bytes: &[u8]) -> Vec<String> {
    let Ok(text) = std::str::from_utf8(bytes) else { return Vec::new() };
    let found = invisible(text);
    let mut out: Vec<String> =
        found.iter().take(INVISIBLE_SHOWN).map(|i| format!("{}:{}: U+{:04X} {}", shown(path), i.line, u32::from(i.ch), i.name)).collect();
    if found.len() > INVISIBLE_SHOWN {
        out.push(format!("{}: {} more invisible characters", shown(path), found.len() - INVISIBLE_SHOWN));
    }
    out
}

/// Package text for the terminal: control characters (except tab) and the characters `invisible`
/// reports become `<U+XXXX>`, so they are seen instead of moving the cursor or reordering the line.
pub fn shown(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        if (c.is_control() && c != '\t') || invisible_name(c).is_some() {
            out.push_str(&format!("<U+{:04X}>", u32::from(c)));
        } else {
            out.push(c);
        }
    }
    out
}

#[derive(Clone, Debug, PartialEq)]
pub struct FileEntry {
    /// Relative, `/`-separated.
    pub path: String,
    pub exec: bool,
    pub sha256: String,
}

impl FileEntry {
    pub fn new(path: String, exec: bool, content: &[u8]) -> FileEntry {
        FileEntry { path, exec, sha256: hex::encode(Sha256::digest(content)) }
    }
}

/// sha256 over `<path>\0<mode>\0<sha256 of content>\n` per regular file, sorted by path.
pub fn tree_hash(entries: &[FileEntry]) -> String {
    let mut sorted: Vec<&FileEntry> = entries.iter().collect();
    sorted.sort_by(|a, b| a.path.cmp(&b.path));
    let mut h = Sha256::new();
    for e in sorted {
        h.update(format!("{}\0{}\0{}\n", e.path, if e.exec { "755" } else { "644" }, e.sha256));
    }
    hex::encode(h.finalize())
}

/// Lists regular files under `dir`; symlinks, special files and empty directories are errors.
pub fn scan_dir(dir: &Path) -> Result<Vec<FileEntry>> {
    let mut out = Vec::new();
    scan(dir, "", None, &mut out)?;
    Ok(out)
}

/// What `publish` zips: like `scan_dir`, without hidden entries (`.DS_Store`, `.env`, `.git/`),
/// and a relative symlink to a file inside the skill is a copy of that file, as at install.
pub fn scan_publish(dir: &Path) -> Result<Vec<FileEntry>> {
    let mut out = Vec::new();
    scan(dir, "", Some(&dir.canonicalize()?), &mut out)?;
    Ok(out)
}

fn scan(dir: &Path, prefix: &str, publish_root: Option<&Path>, out: &mut Vec<FileEntry>) -> Result<()> {
    let mut any = false;
    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        any = true;
        let name = entry.file_name().into_string().map_err(|n| crate::error::Error::new(IO, format!("non-UTF-8 file name {n:?}")))?;
        if publish_root.is_some() && name.starts_with('.') {
            continue;
        }
        let rel = if prefix.is_empty() { name } else { format!("{prefix}/{name}") };
        let meta = entry.path().symlink_metadata()?;
        let ft = meta.file_type();
        if ft.is_dir() {
            scan(&entry.path(), &rel, publish_root, out)?;
        } else if ft.is_file() {
            out.push(FileEntry::new(rel, is_exec(&meta), &std::fs::read(entry.path())?));
        } else if let Some(root) = publish_root.filter(|_| ft.is_symlink()) {
            let target = std::fs::canonicalize(entry.path())
                .ok()
                .filter(|t| std::fs::read_link(entry.path()).is_ok_and(|l| l.is_relative()) && t.starts_with(root) && t.is_file());
            let Some(target) = target else {
                bail!(IO, "{rel:?}: a symlink in a skill must be relative and point to a file inside the skill");
            };
            out.push(FileEntry::new(rel, is_exec(&target.metadata()?), &std::fs::read(&target)?));
        } else {
            bail!(IO, "{rel:?}: symlinks and special files are not allowed in a skill");
        }
    }
    if !any && !prefix.is_empty() {
        bail!(IO, "{prefix:?}: empty directories are not allowed in a skill");
    }
    Ok(())
}

#[cfg(unix)]
fn is_exec(m: &std::fs::Metadata) -> bool {
    use std::os::unix::fs::PermissionsExt;
    m.permissions().mode() & 0o111 != 0
}

// No exec bit here; installed modes come from the store's `.skillcask-modes` instead.
#[cfg(not(unix))]
fn is_exec(_: &std::fs::Metadata) -> bool {
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn reads_frontmatter() {
        let f = frontmatter("---\nname: agents-md\nversion: \"1.2.0\"\ndescription: >\n  multi\nmetadata:\n  a: b\n---\n# body\nname: no");
        assert_eq!(f.get("name").unwrap(), "agents-md");
        assert_eq!(f.get("version").unwrap(), "1.2.0");
        assert!(!f.contains_key("a"));
        assert!(frontmatter("# no frontmatter").is_empty());
        assert!(valid_name("agents-md") && valid_name("a1") && !valid_name("-a") && !valid_name("A") && !valid_name(""));
    }

    #[test]
    fn reads_requires() {
        let t = "---\nname: mr\nrequires:\n  acme/review: ^1.0 # pinned\n\n  # comment\n  \"github:o/r/pr\": 'main'\n  github:o/x: \">=1, <2\"\ndescription: d\n---\n";
        let r = requires(t).unwrap();
        assert_eq!(r.len(), 3, "{r:?}");
        assert_eq!(r["acme/review"], "^1.0");
        assert_eq!(r["github:o/r/pr"], "main");
        assert_eq!(r["github:o/x"], ">=1, <2");
        assert!(requires("---\nname: a\n---\n").unwrap().is_empty());
        assert!(requires("---\nrequires:\n---\n").unwrap().is_empty());
        assert!(requires("# none").unwrap().is_empty());
        for bad in [
            "---\nrequires: acme/a\n---\n",
            "---\nrequires: {acme/a: ^1}\n---\n",
            "---\nrequires:\n  - acme/a\n---\n",
            "---\nrequires:\n  acme/a:\n    x: y\n---\n",
            "---\nrequires:\n  acme/a:\n---\n",
            "---\nrequires:\n  acme/a: ^1\n  acme/a: ^2\n---\n",
        ] {
            assert!(requires(bad).is_err(), "{bad:?}");
        }
    }

    #[test]
    fn renames_frontmatter_name_only() {
        let t = "---\nname: code-review\ndescription: name: x\n---\n# name: code-review\n";
        assert_eq!(rename_frontmatter(t, "mp-review"), "---\nname: mp-review\ndescription: name: x\n---\n# name: code-review\n");
        assert_eq!(rename_frontmatter("---\r\nname: \"a\"\r\n---\r\n", "b"), "---\r\nname: b\r\n---\r\n");
        assert_eq!(rename_frontmatter("\u{feff}---\nname: a\n---\n", "b"), "\u{feff}---\nname: b\n---\n");
        let no_name = "---\ndescription: d\n---\nname: a\n";
        assert_eq!(rename_frontmatter(no_name, "b"), no_name);
        let nested = "---\nmetadata:\n  name: x\n---\n";
        assert_eq!(rename_frontmatter(nested, "b"), nested);
        assert_eq!(rename_frontmatter("# no frontmatter", "b"), "# no frontmatter");
        assert_eq!(frontmatter(&rename_frontmatter(t, "mp-review"))["name"], "mp-review");
        assert_eq!(rename_frontmatter("---\nname : one\n---\n", "b"), "---\nname: b\n---\n");
        assert_eq!(rename_frontmatter("---\nname: one\nx: y\nname: two\n---\n", "b"), "---\nname: one\nx: y\nname: b\n---\n");
        assert_eq!(rename_frontmatter("---\nname: >\n  Multi Line\nx: y\n---\n", "b"), "---\nname: b\nx: y\n---\n");
        assert_eq!(rename_frontmatter("---\nname:\n  one\n---\n", "b"), "---\nname: b\n---\n");
        for t in ["---\nname : one\n---\n", "---\nname: one\nname: two\n---\n", "---\nname: |-\n  x\n---\n"] {
            assert_eq!(frontmatter(&rename_frontmatter(t, "b"))["name"], "b", "{t:?}");
        }
    }

    #[test]
    fn frontmatter_agrees_with_yaml() {
        // Regression: a key-looking line inside a multi-line quoted scalar was read as a key.
        for q in ['"', '\''] {
            let t = format!("---\nname: code-review\ndescription: {q}x\nname: innocent\n  y{q}\n---\n");
            let f = frontmatter(&t);
            assert_eq!(f["name"], "code-review", "{t:?}");
            assert_eq!(f["description"], "x name: innocent y");
            let r = rename_frontmatter(&t, "b");
            assert_eq!(r, t.replace("name: code-review", "name: b"));
            assert_eq!(frontmatter(&r)["description"], "x name: innocent y");
        }
        let escaped = frontmatter("---\ndescription: \"a \\\" b\nname: c\"\nname: real\n---\n");
        assert_eq!(escaped["name"], "real");
        let doubled = frontmatter("---\ndescription: 'it''s\nname: c'\nname: real\n---\n");
        assert_eq!(doubled["name"], "real");

        let unclosed = "---\nname: a\ndescription: d\n";
        assert!(frontmatter(unclosed).is_empty());
        assert_eq!(rename_frontmatter(unclosed, "b"), unclosed);

        let f = frontmatter("---\nname: a # c\nversion: \"1.0.0\" # c\nurl: x#y\nempty: # c\n---\n");
        assert_eq!((f["name"].as_str(), f["version"].as_str(), f["url"].as_str()), ("a", "1.0.0", "x#y"));
        assert!(!f.contains_key("empty"));
        assert_eq!(rename_frontmatter("---\nname: a # c\n---\n", "b"), "---\nname: b\n---\n");

        let f = frontmatter("---\nd1: >-\n  folded\n  text\nd2: |+\n  lit\n  eral\n\nd3: >\nname: n\n---\n");
        assert_eq!((f["d1"].as_str(), f["d2"].as_str(), f["name"].as_str()), ("folded text", "lit\neral", "n"));
        assert!(!f.contains_key("d3"));

        let f =
            frontmatter("---\ndescription: Reviews code\n  for bugs # c\n\n  and style\nname:\n  one\nmeta:\n  a: b\nlist:\n  - x\n---\n");
        assert_eq!((f["description"].as_str(), f["name"].as_str()), ("Reviews code for bugs and style", "one"));
        assert!(!f.contains_key("meta") && !f.contains_key("list"));
    }

    #[test]
    fn finds_invisible_characters() {
        let found = |t: &str| invisible(t).into_iter().map(|i| (i.line, u32::from(i.ch))).collect::<Vec<_>>();
        assert_eq!(found("a\r\nb\u{202E}c\r\n\u{200B}"), [(2, 0x202E), (3, 0x200B)], "CRLF counts one line");
        assert_eq!(found("\u{feff}x\u{feff}"), [(1, 0xFEFF)], "only a leading BOM is fine");
        assert_eq!(found("\u{1F469}\u{200D}\u{1F4BB} \u{2764}\u{FE0F}\u{200D}\u{1F525}"), [], "emoji ZWJ sequences");
        assert_eq!(found("a\u{200D}b \u{1F469}\u{200D}"), [(1, 0x200D), (1, 0x200D)]);
        assert_eq!(found("\u{E0001}\u{E0041}\u{E007F}\u{E0080}"), [(1, 0xE0001), (1, 0xE0041), (1, 0xE007F)]);
        for c in ['\u{061C}', '\u{200C}', '\u{200E}', '\u{200F}', '\u{202A}', '\u{202D}', '\u{2060}', '\u{2066}', '\u{2069}'] {
            assert_eq!(found(&c.to_string()).len(), 1, "{c:?}");
        }
        assert!(found("zażółć ‘quotes’ — dash\t\u{00A0}").is_empty());

        assert_eq!(invisible_warnings("s/SKILL.md", "x\n\u{202E}".as_bytes()), ["s/SKILL.md:2: U+202E RIGHT-TO-LEFT OVERRIDE"]);
        assert!(invisible_warnings("a.bin", b"\xe2\x80\xae\xff").is_empty(), "binary files are skipped");
        assert_eq!(invisible_warnings("a\u{202E}b.md", "\u{200B}".as_bytes()), ["a<U+202E>b.md:1: U+200B ZERO WIDTH SPACE"]);
        assert_eq!(shown("x\u{1b}]0;t\u{7}\ty\u{E0041}"), "x<U+001B>]0;t<U+0007>\ty<U+E0041>");
        let w = invisible_warnings("f", "\u{200B}".repeat(8).as_bytes());
        assert_eq!((w.len(), w[5].as_str()), (6, "f: 3 more invisible characters"));
    }

    #[test]
    fn checks_the_agent_skills_spec() {
        let md = |fm: &str| format!("---\n{fm}---\nbody\n");
        assert!(spec_problems(&md("name: a-1\ndescription: d\ncompatibility: git\n"), "a-1").is_empty());
        assert!(spec_problems(&format!("\u{feff}{}", md("name: a\ndescription: d\n")), "a").is_empty());
        assert_eq!(spec_problems("# none", "a"), ["SKILL.md has no frontmatter (--- ... ---)"]);
        assert_eq!(spec_problems(&md("x: y\n"), "a"), ["name is required", "description is required"]);
        for bad in ["A", "-a", "a-", "a--b", "a_b", &"a".repeat(65)] {
            let p = spec_problems(&md(&format!("name: {bad}\ndescription: d\n")), bad);
            assert!(p.len() == 1 && p[0].contains("must be 1-64"), "{bad}: {p:?}");
        }
        assert!(spec_problems(&md(&format!("name: {}\ndescription: d\n", "a".repeat(64))), &"a".repeat(64)).is_empty());
        assert_eq!(spec_problems(&md("name: a\ndescription: d\n"), "b"), ["name \"a\" does not match the directory name \"b\""]);
        let long = md(&format!("name: a\ndescription: {}\ncompatibility: {}\n", "ł".repeat(1025), "c".repeat(501)));
        assert_eq!(
            spec_problems(&long, "a"),
            ["description has 1025 characters, at most 1024", "compatibility has 501 characters, at most 500"]
        );
        assert!(spec_problems(&md(&format!("name: a\ndescription: {}\n", "ł".repeat(1024))), "a").is_empty(), "characters, not bytes");
    }

    #[test]
    fn tree_hash_matches_definition() {
        let e = vec![FileEntry::new("b/x.sh".into(), true, b"x"), FileEntry::new("SKILL.md".into(), false, b"s")];
        let mut h = Sha256::new();
        h.update(format!("SKILL.md\0644\0{}\n", hex::encode(Sha256::digest(b"s"))));
        h.update(format!("b/x.sh\0755\0{}\n", hex::encode(Sha256::digest(b"x"))));
        assert_eq!(tree_hash(&e), hex::encode(h.finalize()));

        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("SKILL.md"), b"s").unwrap();
        std::fs::create_dir(dir.path().join("b")).unwrap();
        std::fs::write(dir.path().join("b/x.sh"), b"x").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(dir.path().join("b/x.sh"), std::fs::Permissions::from_mode(0o755)).unwrap();
            assert_eq!(tree_hash(&scan_dir(dir.path()).unwrap()), tree_hash(&e));
            std::os::unix::fs::symlink("SKILL.md", dir.path().join("l")).unwrap();
            assert!(scan_dir(dir.path()).is_err());
            std::fs::remove_file(dir.path().join("l")).unwrap();
        }
        std::fs::create_dir(dir.path().join("empty")).unwrap();
        assert!(scan_dir(dir.path()).is_err());
    }
}