skill 0.7.0

Blazing-fast Vercel Skills CLI, reborn in Rust. 100% command parity, zero compromises.
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
//! Skill discovery and `SKILL.md` parsing.
//!
//! This module handles finding and parsing skills from the filesystem,
//! including YAML frontmatter extraction and multi-strategy directory
//! scanning.

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

use crate::error::{Result, SkillError};
use crate::types::{DiscoverOptions, Skill};

/// Directories to skip during recursive skill search.
const SKIP_DIRS: &[&str] = &["node_modules", ".git", "dist", "build", "__pycache__"];

/// Maximum recursion depth for skill directory scanning.
const MAX_DEPTH: usize = 5;

/// Check whether `INSTALL_INTERNAL_SKILLS` is enabled.
#[must_use]
pub fn should_install_internal_skills() -> bool {
    std::env::var("INSTALL_INTERNAL_SKILLS")
        .ok()
        .is_some_and(|v| v == "1" || v == "true")
}

/// Parse a `SKILL.md` file and return a [`Skill`] if valid.
///
/// Returns `Ok(None)` when the file exists but lacks required frontmatter
/// fields (`name` and `description`), or when the skill is internal and
/// internal skill installation is not enabled.
///
/// # Errors
///
/// Returns an error if the file cannot be read.
pub async fn parse_skill_md(skill_md_path: &Path, include_internal: bool) -> Result<Option<Skill>> {
    let content = match tokio::fs::read_to_string(skill_md_path).await {
        Ok(c) => c,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(e) => return Err(SkillError::io(skill_md_path, e)),
    };

    let Some((frontmatter, _body)) = extract_frontmatter(&content) else {
        return Ok(None);
    };

    let Ok(data) = serde_yml::from_str::<serde_yml::Value>(frontmatter) else {
        return Ok(None);
    };

    let name = data
        .get("name")
        .and_then(serde_yml::Value::as_str)
        .map(String::from);
    let description = data
        .get("description")
        .and_then(serde_yml::Value::as_str)
        .map(String::from);

    let (Some(name), Some(description)) = (name, description) else {
        return Ok(None);
    };

    let is_internal = data
        .get("metadata")
        .and_then(|m| m.get("internal"))
        .and_then(serde_yml::Value::as_bool)
        .unwrap_or(false);

    if is_internal && !should_install_internal_skills() && !include_internal {
        return Ok(None);
    }

    let metadata = data.get("metadata").and_then(|m| {
        serde_yml::from_value::<std::collections::HashMap<String, serde_yml::Value>>(m.clone()).ok()
    });

    let dir = skill_md_path
        .parent()
        .unwrap_or(skill_md_path)
        .to_path_buf();

    Ok(Some(Skill {
        name,
        description,
        path: dir,
        raw_content: Some(content),
        plugin_name: None,
        metadata,
    }))
}

/// Extract YAML frontmatter delimited by `---`.
///
/// Returns `(frontmatter, body)` or `None` if no valid delimiters are found.
/// This is the public entry point used by providers.
#[must_use]
pub fn extract_frontmatter(content: &str) -> Option<(&str, &str)> {
    let trimmed = content.trim_start();
    if !trimmed.starts_with("---") {
        return None;
    }
    let after_first = &trimmed[3..];
    let end = after_first.find("\n---")?;
    let frontmatter = &after_first[..end];
    let body_start = end + 4;
    let body = after_first.get(body_start..).unwrap_or("");
    Some((frontmatter.trim(), body))
}

/// Check whether a directory contains a `SKILL.md` file.
async fn has_skill_md(dir: &Path) -> bool {
    tokio::fs::try_exists(dir.join("SKILL.md"))
        .await
        .unwrap_or(false)
}

/// Recursively find directories containing `SKILL.md`.
async fn find_skill_dirs(dir: &Path, depth: usize) -> Vec<PathBuf> {
    if depth > MAX_DEPTH {
        return Vec::new();
    }

    let mut results = Vec::new();

    if has_skill_md(dir).await {
        results.push(dir.to_path_buf());
    }

    let Ok(mut entries) = tokio::fs::read_dir(dir).await else {
        return results;
    };

    while let Ok(Some(entry)) = entries.next_entry().await {
        let Ok(file_type) = entry.file_type().await else {
            continue;
        };
        if !file_type.is_dir() {
            continue;
        }
        let name = entry.file_name();
        let name_str = name.to_string_lossy();
        if SKIP_DIRS.contains(&name_str.as_ref()) {
            continue;
        }
        let child_path = entry.path();
        let sub = Box::pin(find_skill_dirs(&child_path, depth + 1));
        results.extend(sub.await);
    }

    results
}

/// Validate that a resolved subpath stays within the base directory.
///
/// Uses lexical normalization (no filesystem access) to ensure consistent
/// behavior across environments where paths may not exist.
#[must_use]
pub fn is_subpath_safe(base_path: &Path, subpath: &str) -> bool {
    // Check for obvious path traversal patterns first
    let normalized_subpath = subpath.replace('\\', "/");
    for segment in normalized_subpath.split('/') {
        if segment == ".." {
            return false;
        }
    }

    // Use lexical normalization for the full path check
    let target = base_path.join(subpath);
    let normalized_base = crate::path_util::normalize_absolute(base_path);
    let normalized_target = crate::path_util::normalize_absolute(&target);
    normalized_target.starts_with(&normalized_base)
}

/// Discover skills under `base_path` (optionally scoped by `subpath`).
///
/// The discovery strategy mirrors the `TypeScript` reference:
/// 1. Check if the search path itself has a `SKILL.md` (single root skill).
/// 2. Scan priority directories (common skill locations for each agent),
///    including paths declared in plugin manifests.
/// 3. Fall back to recursive search if nothing was found, or if `full_depth`
///    is enabled.
///
/// # Errors
///
/// Returns an error if the `subpath` escapes the `base_path`.
#[allow(
    clippy::cognitive_complexity,
    reason = "multi-strategy discovery with fallback logic"
)]
pub async fn discover_skills(
    base_path: &Path,
    subpath: Option<&str>,
    options: &DiscoverOptions,
) -> Result<Vec<Skill>> {
    if let Some(sp) = subpath
        && !is_subpath_safe(base_path, sp)
    {
        return Err(SkillError::PathTraversal {
            context: "subpath",
            path: sp.to_owned(),
        });
    }

    let search_path = subpath.map_or_else(|| base_path.to_path_buf(), |sp| base_path.join(sp));

    let mut skills = Vec::new();
    let mut seen_names: HashSet<String> = HashSet::new();
    let include_internal = options.include_internal;

    // Get plugin groupings to map skills to their parent plugin.
    let plugin_groupings = crate::plugin_manifest::get_plugin_groupings(&search_path).await;

    let enhance_skill = |mut skill: Skill| -> Skill {
        let resolved = std::path::absolute(&skill.path).unwrap_or_else(|_| skill.path.clone());
        if let Some(name) = plugin_groupings.get(&resolved) {
            skill.plugin_name = Some(name.clone());
        }
        skill
    };

    // 1. Root SKILL.md
    if has_skill_md(&search_path).await
        && let Some(skill) = parse_skill_md(&search_path.join("SKILL.md"), include_internal).await?
    {
        let skill = enhance_skill(skill);
        seen_names.insert(skill.name.clone());
        skills.push(skill);
        if !options.full_depth {
            return Ok(skills);
        }
    }

    // 2. Priority search directories + plugin manifest paths
    let mut priority_dirs = build_priority_dirs(&search_path);
    priority_dirs.extend(crate::plugin_manifest::get_plugin_skill_paths(&search_path).await);

    for dir in &priority_dirs {
        let Ok(mut entries) = tokio::fs::read_dir(dir).await else {
            continue;
        };
        while let Ok(Some(entry)) = entries.next_entry().await {
            let Ok(ft) = entry.file_type().await else {
                continue;
            };
            if !ft.is_dir() {
                continue;
            }
            let skill_dir = entry.path();
            if !has_skill_md(&skill_dir).await {
                continue;
            }
            if let Some(skill) =
                parse_skill_md(&skill_dir.join("SKILL.md"), include_internal).await?
                && seen_names.insert(skill.name.clone())
            {
                skills.push(enhance_skill(skill));
            }
        }
    }

    // 3. Recursive fallback
    if skills.is_empty() || options.full_depth {
        for skill_dir in find_skill_dirs(&search_path, 0).await {
            if let Some(skill) =
                parse_skill_md(&skill_dir.join("SKILL.md"), include_internal).await?
                && seen_names.insert(skill.name.clone())
            {
                skills.push(enhance_skill(skill));
            }
        }
    }

    Ok(skills)
}

/// Build the list of priority search directories (matching the TS reference).
fn build_priority_dirs(search_path: &Path) -> Vec<PathBuf> {
    let sp = search_path;
    vec![
        sp.to_path_buf(),
        sp.join("skills"),
        sp.join("skills/.curated"),
        sp.join("skills/.experimental"),
        sp.join("skills/.system"),
        sp.join(".agent/skills"),
        sp.join(".agents/skills"),
        sp.join(".claude/skills"),
        sp.join(".cline/skills"),
        sp.join(".codebuddy/skills"),
        sp.join(".codex/skills"),
        sp.join(".commandcode/skills"),
        sp.join(".continue/skills"),
        sp.join(".github/skills"),
        sp.join(".goose/skills"),
        sp.join(".iflow/skills"),
        sp.join(".junie/skills"),
        sp.join(".kilocode/skills"),
        sp.join(".kiro/skills"),
        sp.join(".mux/skills"),
        sp.join(".neovate/skills"),
        sp.join(".opencode/skills"),
        sp.join(".openhands/skills"),
        sp.join(".pi/skills"),
        sp.join(".qoder/skills"),
        sp.join(".roo/skills"),
        sp.join(".trae/skills"),
        sp.join(".windsurf/skills"),
        sp.join(".zencoder/skills"),
    ]
}

/// Get a display name for a skill (falls back to directory name).
#[must_use]
pub fn get_skill_display_name(skill: &Skill) -> &str {
    if skill.name.is_empty() {
        skill
            .path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unnamed")
    } else {
        &skill.name
    }
}

/// Filter skills by a list of names (case-insensitive match).
#[must_use]
pub fn filter_skills(skills: &[Skill], input_names: &[String]) -> Vec<Skill> {
    let normalized: Vec<String> = input_names.iter().map(|n| n.to_lowercase()).collect();
    skills
        .iter()
        .filter(|skill| {
            let name = skill.name.to_lowercase();
            let display = get_skill_display_name(skill).to_lowercase();
            normalized
                .iter()
                .any(|input| *input == name || *input == display)
        })
        .cloned()
        .collect()
}

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

    #[test]
    fn extract_frontmatter_basic() {
        let content = "---\nname: test\ndescription: hello\n---\n# Body";
        let (fm, body) = extract_frontmatter(content).expect("should parse");
        assert_eq!(fm, "name: test\ndescription: hello");
        assert!(body.contains("# Body"));
    }

    #[test]
    fn extract_frontmatter_missing() {
        assert!(extract_frontmatter("no frontmatter here").is_none());
    }

    #[test]
    fn extract_frontmatter_leading_whitespace() {
        let content = "  \n---\nname: x\n---\nbody";
        let (fm, _body) = extract_frontmatter(content).expect("should handle leading whitespace");
        assert_eq!(fm, "name: x");
    }

    #[test]
    fn extract_frontmatter_no_body() {
        let content = "---\nname: x\n---";
        let (fm, body) = extract_frontmatter(content).expect("should parse without body");
        assert_eq!(fm, "name: x");
        assert!(body.is_empty());
    }

    #[test]
    fn extract_frontmatter_empty_frontmatter() {
        let content = "---\n---\nbody";
        let (fm, _body) = extract_frontmatter(content).expect("should parse empty frontmatter");
        assert!(fm.is_empty());
    }

    #[test]
    fn extract_frontmatter_no_closing() {
        assert!(extract_frontmatter("---\nname: x\nno closing").is_none());
    }

    #[test]
    fn subpath_safe_valid() {
        let base = Path::new("/tmp/repo");
        assert!(is_subpath_safe(base, "skills/my-skill"));
    }

    #[test]
    fn subpath_safe_traversal_blocked() {
        let base = Path::new("/tmp/repo");
        assert!(!is_subpath_safe(base, "../../etc/passwd"));
    }

    #[tokio::test]
    async fn parse_skill_md_valid() {
        let dir = tempfile::tempdir().expect("tempdir");
        let skill_md = dir.path().join("SKILL.md");
        tokio::fs::write(
            &skill_md,
            "---\nname: test-skill\ndescription: A test skill\n---\n# Instructions\nDo things.",
        )
        .await
        .unwrap();

        let result = parse_skill_md(&skill_md, false).await.unwrap();
        let skill = result.expect("should parse valid SKILL.md");
        assert_eq!(skill.name, "test-skill");
        assert_eq!(skill.description, "A test skill");
        assert!(skill.raw_content.is_some());
    }

    #[tokio::test]
    async fn parse_skill_md_missing_name() {
        let dir = tempfile::tempdir().expect("tempdir");
        let skill_md = dir.path().join("SKILL.md");
        tokio::fs::write(&skill_md, "---\ndescription: no name\n---\nbody")
            .await
            .unwrap();

        let result = parse_skill_md(&skill_md, false).await.unwrap();
        assert!(result.is_none(), "should return None when name is missing");
    }

    #[tokio::test]
    async fn parse_skill_md_not_found() {
        let result = parse_skill_md(Path::new("/nonexistent/SKILL.md"), false).await;
        assert!(
            result.unwrap().is_none(),
            "should return Ok(None) for missing file"
        );
    }
}