agent-works 0.11.1

Batteries-included Agent toolbox built on agent-base
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
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
//! Filesystem-backed skill discovery and resolution.
//!
//! `SkillResolver` scans SKILL.md directories (later dirs override earlier
//! ones on name collision), resolves `/name args` slash input with fuzzy
//! matching, and serves exact-name lookups for the model-facing `skill` tool.
//! Host apps decide *which* directories to scan (policy); this module only
//! knows *how* to scan and resolve (mechanism).

use std::collections::HashMap;
use std::path::PathBuf;

use super::Skill;
use super::prompt_skill::{PromptSkill, SkillScope};

/// Scanned skill set backing slash lookup and the `skill` tool.
pub struct SkillResolver {
    skills: Vec<PromptSkill>,
}

/// A matched `/name args` slash invocation, with the skill's lifetime scope.
///
/// Hosts dispatch on [`scope`](ResolvedSkill::scope): `Session` skills go to
/// the system prompt (persist for the session), `Turn` skills are injected as
/// ephemeral turn input (one-shot).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedSkill {
    /// Canonical name of the matched skill.
    pub name: String,
    /// Body with `$ARGUMENTS` substituted.
    pub body: String,
    /// Lifetime scope (frontmatter `scope:`; missing/unknown → `session`).
    pub scope: SkillScope,
}

/// Failure reasons for `resolve_by_name` (the `skill` tool maps them to
/// different error messages).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SkillLookupError {
    /// No skill with this name.
    NotFound,
    /// The skill is flagged `disable-model-invocation`: denied for the model
    /// (the user's slash path is unaffected — that flag only constrains
    /// model-side triggering).
    ModelInvocationDenied,
}

impl SkillResolver {
    /// Scan skills from multiple directories (later-scanned dirs win on name
    /// collision).
    pub fn from_dirs(dirs: &[PathBuf]) -> Self {
        let mut by_name: HashMap<String, PromptSkill> = HashMap::new();

        for dir in dirs {
            match PromptSkill::scan_dir(dir) {
                Ok(skills) => {
                    for skill in skills {
                        let name = skill.name().to_string();
                        by_name.insert(name, skill); // later entry overrides earlier one
                    }
                }
                Err(e) => {
                    tracing::warn!(dir = %dir.display(), error = %e, "failed to scan skill directory");
                }
            }
        }

        let mut skills: Vec<PromptSkill> = by_name.into_values().collect();
        skills.sort_by(|a, b| a.name().cmp(b.name()));
        Self { skills }
    }

    /// Parse user input; if it is `/skill-name args`, return the resolved skill body.
    ///
    /// Returns `None` when the input is not a slash command (or no skill
    /// matched); it should then be submitted as-is.
    pub fn resolve(&self, input: &str) -> Option<String> {
        self.resolve_with_meta(input).map(|r| r.body)
    }

    /// Like [`resolve`](Self::resolve), but also returns the matched skill's
    /// canonical name and lifetime scope — hosts need both to dispatch
    /// session-scope skills into their system prompt.
    pub fn resolve_with_meta(&self, input: &str) -> Option<ResolvedSkill> {
        let trimmed = input.trim();
        if !trimmed.starts_with('/') {
            return None;
        }

        // Split: `/skill-name arg1 arg2 ...` → name="skill-name", rest="arg1 arg2 ..."
        let without_slash = &trimmed[1..];
        let (name, raw_args) = match without_slash.split_once(char::is_whitespace) {
            Some((n, rest)) => (n, rest.trim()),
            None => (without_slash, ""),
        };

        // Name must be non-empty and contain no whitespace (prevents "/ " from
        // being treated as a skill command)
        if name.is_empty() || name.contains(char::is_whitespace) {
            return None;
        }

        // Fuzzy match: exact → suffix → contains → word-overlap; within a tier
        // pick the shortest name
        let skill = self.fuzzy_find(name)?;

        if !skill.is_user_invocable() {
            tracing::info!(
                name = name,
                "skill is not user-invocable, passing through as text"
            );
            return None;
        }

        // Named parameters are not parsed yet; only raw_arguments are passed through
        let params = HashMap::new();
        let body = skill.resolve_body(&params, raw_args);

        tracing::info!(
            input = name,
            matched = skill.name(),
            args = raw_args,
            body_len = body.len(),
            scope = ?skill.scope(),
            "resolved /skill command"
        );

        Some(ResolvedSkill {
            name: skill.name().to_string(),
            body,
            scope: skill.scope(),
        })
    }

    /// Fuzzy skill lookup by priority: exact → suffix → contains →
    /// word-overlap. Within a tier, take the shortest name (most specific).
    /// Returns `None` when nothing matches at all.
    fn fuzzy_find(&self, query: &str) -> Option<&PromptSkill> {
        // Candidates: only user_invocable skills participate in matching
        let candidates: Vec<&PromptSkill> = self
            .skills
            .iter()
            .filter(|s| s.is_user_invocable())
            .collect();

        // 1. Exact match
        if let Some(s) = candidates.iter().find(|s| s.name() == query) {
            return Some(s);
        }

        // 2. Suffix match: skill name ends with query (e.g. requesting-code-review
        //    ends with code-review)
        let mut suffix_hits: Vec<&&PromptSkill> = candidates
            .iter()
            .filter(|s| s.name().ends_with(query) && s.name() != query)
            .collect();
        if !suffix_hits.is_empty() {
            suffix_hits.sort_by_key(|s| s.name().len());
            return Some(suffix_hits[0]);
        }

        // 3. Substring match: skill name contains query
        let mut contains_hits: Vec<&&PromptSkill> = candidates
            .iter()
            .filter(|s| s.name().contains(query))
            .collect();
        if !contains_hits.is_empty() {
            contains_hits.sort_by_key(|s| s.name().len());
            return Some(contains_hits[0]);
        }

        // 4. Word-level match: query split into words is a subset of the skill
        //    name's split words
        //    e.g. "review-pr" → ["review", "pr"] ⊆ ["pre", "landing", "pr", "review"]
        let query_words: Vec<&str> = query.split('-').collect();
        let mut word_hits: Vec<(&&PromptSkill, usize)> = candidates
            .iter()
            .filter_map(|s| {
                let name_words: Vec<&str> = s.name().split('-').collect();
                let all_match = query_words.iter().all(|qw| name_words.contains(qw));
                if all_match {
                    Some((s, name_words.len()))
                } else {
                    None
                }
            })
            .collect();
        if !word_hits.is_empty() {
            // Take the one with the fewest words (most specific)
            word_hits.sort_by_key(|(_, len)| *len);
            return Some(word_hits[0].0);
        }

        None
    }

    /// Number of loaded skills (for catalog building and tests).
    pub fn len(&self) -> usize {
        self.skills.len()
    }

    /// Whether empty (for catalog-injection emptiness checks and tests).
    pub fn is_empty(&self) -> bool {
        self.skills.is_empty()
    }

    /// Read-only access to the loaded skills (sorted by name).
    ///
    /// For same-crate consumers such as catalog rendering to iterate; no
    /// `&mut` is exposed — the resolver is a shared snapshot, changes go
    /// through rebuilding (hot reload).
    pub fn entries(&self) -> &[PromptSkill] {
        &self.skills
    }

    /// List the names of all loaded skills (including `user_invocable: false`;
    /// currently only used for host log lines).
    pub fn skill_names(&self) -> Vec<&str> {
        self.skills.iter().map(|s| s.name()).collect()
    }

    /// List (name, description) summaries of all loaded skills (for the `/`
    /// picker display).
    pub fn skill_summaries(&self) -> Vec<(String, String)> {
        self.skills
            .iter()
            .map(|s| (s.name().to_string(), s.brief_description()))
            .collect()
    }

    /// Exact-match by name (skill tool uses this; `/` slash path uses `resolve`).
    ///
    /// Unlike `resolve`, this does NOT filter on `user_invocable` — model tools
    /// can load every skill, and the catalog lists every skill regardless.
    /// Skills flagged `disable-model-invocation` are refused: the trait contract
    /// ("the LLM cannot auto-trigger this skill") is enforced at both model
    /// surfaces — the catalog omits them and this lookup denies them.
    /// Returns `(body, name)` so the caller can trace which skill was served.
    pub fn resolve_by_name(
        &self,
        name: &str,
        args: &str,
    ) -> Result<(String, &str), SkillLookupError> {
        let skill = self.skills.iter().find(|s| s.name() == name);
        let Some(skill) = skill else {
            return Err(SkillLookupError::NotFound);
        };
        if skill.disable_model_invocation() {
            return Err(SkillLookupError::ModelInvocationDenied);
        }
        let params = HashMap::new();
        let body = skill.resolve_body(&params, args);
        Ok((body, skill.name()))
    }

    /// Absolute path of the `SKILL.md` that backs a named skill (for the
    /// overflow fallback: tool returns truncated body + path for read_file).
    pub fn source_path_for(&self, name: &str) -> Option<&std::path::Path> {
        self.skills.iter().find(|s| s.name() == name)?.source_path()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::Path;

    /// Create a temporary skill directory structure:
    ///   tmp_dir/skills/test-skill/SKILL.md
    fn make_skill_dir(tmp: &Path, name: &str, body: &str, user_invocable: bool) {
        let skill_dir = tmp.join("skills").join(name);
        fs::create_dir_all(&skill_dir).unwrap();
        let invocable = if user_invocable { "true" } else { "false" };
        let content = format!(
            "---\nname: {name}\ndescription: test skill\nuser-invocable: {invocable}\n---\n\n{body}"
        );
        fs::write(skill_dir.join("SKILL.md"), content).unwrap();
    }

    #[test]
    fn resolve_slash_command() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(tmp.path(), "commit", "Run tests then commit.", true);

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        assert_eq!(resolver.len(), 1);

        let result = resolver.resolve("/commit");
        assert!(result.is_some());
        assert!(result.unwrap().contains("Run tests then commit."));
    }

    #[test]
    fn resolve_slash_command_with_args() {
        let tmp = tempfile::tempdir().unwrap();
        let skill_dir = tmp.path().join("skills").join("commit");
        fs::create_dir_all(&skill_dir).unwrap();
        fs::write(
            skill_dir.join("SKILL.md"),
            "---\nname: commit\ndescription: d\nuser-invocable: true\n---\n\nRun tests then commit.\nArgs: $ARGUMENTS",
        )
        .unwrap();

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);

        let result = resolver.resolve("/commit --amend");
        assert!(result.is_some());
        let body = result.unwrap();
        assert!(
            body.contains("--amend"),
            "body should contain raw arguments"
        );
    }

    #[test]
    fn non_slash_input_returns_none() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(tmp.path(), "commit", "body", true);

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        assert!(resolver.resolve("hello world").is_none());
        assert!(resolver.resolve("no slash").is_none());
    }

    #[test]
    fn unknown_skill_returns_none() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(tmp.path(), "commit", "body", true);

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        assert!(resolver.resolve("/nonexistent").is_none());
    }

    #[test]
    fn not_user_invocable_returns_none() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(tmp.path(), "internal", "body", false);

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        assert!(resolver.resolve("/internal").is_none());
    }

    #[test]
    fn bare_slash_returns_none() {
        let tmp = tempfile::tempdir().unwrap();
        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        assert!(resolver.resolve("/").is_none());
        assert!(resolver.resolve("/ ").is_none());
    }

    #[test]
    fn project_level_overrides_user_level() {
        let tmp = tempfile::tempdir().unwrap();
        let user_dir = tmp.path().join("user_skills");
        let proj_dir = tmp.path().join("proj_skills");

        // Same skill name, different body
        let sd = user_dir.join("commit").join("SKILL.md");
        fs::create_dir_all(sd.parent().unwrap()).unwrap();
        fs::write(
            &sd,
            "---\nname: commit\ndescription: d\nuser-invocable: true\n---\n\nuser body",
        )
        .unwrap();

        let sd = proj_dir.join("commit").join("SKILL.md");
        fs::create_dir_all(sd.parent().unwrap()).unwrap();
        fs::write(
            &sd,
            "---\nname: commit\ndescription: d\nuser-invocable: true\n---\n\nproject body",
        )
        .unwrap();

        // Later-scanned dirs take priority
        let resolver = SkillResolver::from_dirs(&[user_dir, proj_dir]);
        assert_eq!(resolver.len(), 1);
        let body = resolver.resolve("/commit").unwrap();
        assert!(
            body.contains("project body"),
            "project-level should override user-level"
        );
    }

    #[test]
    fn empty_dir_yields_empty_resolver() {
        let tmp = tempfile::tempdir().unwrap();
        let resolver = SkillResolver::from_dirs(&[tmp.path().join("nonexistent")]);
        assert!(resolver.is_empty());
        assert!(resolver.resolve("/anything").is_none());
    }

    // ── Fuzzy matching tests ──

    #[test]
    fn fuzzy_suffix_match() {
        // Simulate the Claude Code scenario: skill named requesting-code-review,
        // user types /code-review
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(
            tmp.path(),
            "requesting-code-review",
            "review body here",
            true,
        );

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        let result = resolver.resolve("/code-review");
        assert!(
            result.is_some(),
            "suffix match should find requesting-code-review"
        );
        assert!(result.unwrap().contains("review body here"));
    }

    #[test]
    fn fuzzy_contains_match() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(tmp.path(), "design-review", "design body", true);
        make_skill_dir(tmp.path(), "requesting-code-review", "code body", true);

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);

        // "review" is a substring but not a suffix → takes the contains branch
        // Both contain "review"; pick the shortest (design-review)
        let result = resolver.resolve("/review");
        assert!(result.is_some());
        assert!(
            result.unwrap().contains("design body"),
            "contains match should pick shortest name"
        );
    }

    #[test]
    fn fuzzy_exact_takes_priority() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(tmp.path(), "review", "exact body", true);
        make_skill_dir(tmp.path(), "code-review", "suffix body", true);

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);

        // Exact match "review" should win over suffix match "code-review"
        let result = resolver.resolve("/review");
        assert!(result.is_some());
        assert!(
            result.unwrap().contains("exact body"),
            "exact match should win over suffix"
        );
    }

    #[test]
    fn fuzzy_suffix_picks_shortest() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(tmp.path(), "requesting-code-review", "long body", true);
        make_skill_dir(tmp.path(), "code-review", "short body", true);

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);

        // Both end with "code-review"; pick the shortest
        let result = resolver.resolve("/code-review");
        assert!(result.is_some());
        assert!(
            result.unwrap().contains("short body"),
            "suffix match should pick shortest name"
        );
    }

    #[test]
    fn fuzzy_word_overlap() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(
            tmp.path(),
            "finishing-a-development-branch",
            "finish body",
            true,
        );

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);

        // "dev-branch" → ["dev", "branch"]; skill splits to
        // ["finishing","a","development","branch"].
        // "dev" is not among the skill's words (the skill uses "development"),
        // so no match
        let result = resolver.resolve("/dev-branch");
        assert!(result.is_none(), "partial word should not match");

        // "development-branch" → full word match
        let result = resolver.resolve("/development-branch");
        assert!(result.is_some());
        assert!(result.unwrap().contains("finish body"));
    }

    // ── resolve_by_name (`skill` tool path) ──

    #[test]
    fn resolve_by_name_denies_disable_model_invocation() {
        let tmp = tempfile::tempdir().unwrap();
        let deploy_dir = tmp.path().join("skills").join("deploy");
        fs::create_dir_all(&deploy_dir).unwrap();
        fs::write(
            deploy_dir.join("SKILL.md"),
            "---\nname: deploy\ndescription: d\nuser-invocable: true\ndisable-model-invocation: true\n---\n\nship it",
        )
        .unwrap();
        make_skill_dir(tmp.path(), "commit", "commit body", true);

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);

        assert_eq!(
            resolver.resolve_by_name("deploy", ""),
            Err(SkillLookupError::ModelInvocationDenied),
            "disable-model-invocation skill must be denied on the model path"
        );
        assert_eq!(
            resolver.resolve_by_name("nope", ""),
            Err(SkillLookupError::NotFound)
        );
        let (body, name) = resolver.resolve_by_name("commit", "").unwrap();
        assert_eq!(name, "commit");
        assert!(body.contains("commit body"));
    }

    #[test]
    fn skill_summaries_cover_all_skills_sorted() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_dir(tmp.path(), "zeta", "z body", true);
        make_skill_dir(tmp.path(), "internal-helper", "h body", false);

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);

        let summaries = resolver.skill_summaries();
        let names: Vec<&str> = summaries.iter().map(|(n, _)| n.as_str()).collect();
        assert_eq!(
            names,
            vec!["internal-helper", "zeta"],
            "summaries sorted by name"
        );
        // Non-user-invocable skills must also be listed (only the slash path
        // filters; the picker wants everything).
        assert!(names.contains(&"internal-helper"));
        assert!(summaries.iter().all(|(_, d)| !d.is_empty()));
    }

    #[test]
    #[cfg(unix)]
    fn unreadable_dir_does_not_block_later_dirs() {
        use std::os::unix::fs::PermissionsExt;

        let tmp = tempfile::tempdir().unwrap();
        let bad_dir = tmp.path().join("bad_skills");
        let good_dir = tmp.path().join("good_skills");
        fs::create_dir_all(bad_dir.join("broken")).unwrap();
        fs::create_dir_all(bad_dir.join("locked")).unwrap();
        // Put a real skill in the good directory
        let sd = good_dir.join("commit");
        fs::create_dir_all(&sd).unwrap();
        fs::write(
            sd.join("SKILL.md"),
            "---\nname: commit\ndescription: d\nuser-invocable: true\n---\n\nlater dir body",
        )
        .unwrap();

        // Put a skill dir that will end up locked under bad_dir, then chmod
        // 000 bad_dir itself
        fs::write(
            bad_dir.join("broken").join("SKILL.md"),
            "---\nname: broken\ndescription: d\nuser-invocable: true\n---\n\nshould be unreadable",
        )
        .unwrap();
        fs::set_permissions(&bad_dir, fs::Permissions::from_mode(0o000)).unwrap();

        // Regardless of the scan outcome, restore permissions first so tempdir
        // cleanup leaves nothing behind.
        let result = std::panic::catch_unwind(|| {
            SkillResolver::from_dirs(&[bad_dir.clone(), good_dir.clone()])
        });
        fs::set_permissions(&bad_dir, fs::Permissions::from_mode(0o755)).unwrap();

        let resolver = result.expect("from_dirs must not panic on unreadable dir");
        assert_eq!(
            resolver.len(),
            1,
            "later readable dir must still be scanned"
        );
        assert!(resolver.resolve("/commit").is_some());
    }

    // ── scope resolution (skill-lifetime v3) ──

    /// Write a skill with arbitrary extra frontmatter lines.
    fn make_skill_with_frontmatter(tmp: &Path, name: &str, extra: &str) {
        let skill_dir = tmp.join("skills").join(name);
        fs::create_dir_all(&skill_dir).unwrap();
        let content = format!(
            "---\nname: {name}\ndescription: d\nuser-invocable: true\n{extra}\n---\n\nbody of {name}"
        );
        fs::write(skill_dir.join("SKILL.md"), content).unwrap();
    }

    #[test]
    fn scope_defaults_to_session_when_absent() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_with_frontmatter(tmp.path(), "plain", "");

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        let resolved = resolver.resolve_with_meta("/plain").unwrap();
        assert_eq!(resolved.scope, SkillScope::Session);
        assert_eq!(resolved.name, "plain");
        assert!(resolved.body.contains("body of plain"));
    }

    #[test]
    fn scope_turn_is_parsed() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_with_frontmatter(tmp.path(), "oneshot", "scope: turn");

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        assert_eq!(
            resolver.resolve_with_meta("/oneshot").unwrap().scope,
            SkillScope::Turn
        );
    }

    #[test]
    fn scope_session_explicit_and_unknown_fall_back_to_session() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_with_frontmatter(tmp.path(), "explicit", "scope: session");
        // A typo must never fail the whole SKILL.md load — falls back to session.
        make_skill_with_frontmatter(tmp.path(), "typo", "scope: sesion");

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        assert_eq!(resolver.len(), 2, "typo in scope must not fail the load");
        assert_eq!(
            resolver.resolve_with_meta("/explicit").unwrap().scope,
            SkillScope::Session
        );
        assert_eq!(
            resolver.resolve_with_meta("/typo").unwrap().scope,
            SkillScope::Session
        );
    }

    #[test]
    fn resolve_delegates_to_resolve_with_meta_body() {
        let tmp = tempfile::tempdir().unwrap();
        make_skill_with_frontmatter(tmp.path(), "commit", "scope: turn");

        let resolver = SkillResolver::from_dirs(&[tmp.path().join("skills")]);
        let via_meta = resolver.resolve_with_meta("/commit").unwrap();
        assert_eq!(Some(via_meta.body), resolver.resolve("/commit"));
    }
}