codewhale-tui 0.9.4

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

use async_trait::async_trait;
use serde_json::{Value, json};

use crate::skills::{
    Skill, SkillDiscoveryMode, SkillSource, discover_for_workspace_and_dir_with_mode_and_plugins,
    discover_in_workspace_with_mode_and_plugins, skill_directories_for_workspace_and_dir,
    skills_directories_for_mode,
};

use super::spec::{
    ApprovalRequirement, ToolCapability, ToolContext, ToolError, ToolResult, ToolSpec,
};

pub struct LoadSkillTool;

#[async_trait]
impl ToolSpec for LoadSkillTool {
    fn name(&self) -> &'static str {
        "load_skill"
    }

    fn description(&self) -> &'static str {
        "Load a skill (SKILL.md body + companion file list) into the next turn's context. \
         Use name=\"list\" to discover the complete enabled catalogue, then load an exact \
         skill when the user names it or the task clearly matches its description. Faster \
         than File action=\"read\" plus File action=\"list\"."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "Skill id to load. Omit or pass \"list\" to see all available skills."
                }
            },
            "additionalProperties": false
        })
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        vec![ToolCapability::ReadOnly]
    }

    fn approval_requirement(&self) -> ApprovalRequirement {
        ApprovalRequirement::Auto
    }

    fn supports_parallel(&self) -> bool {
        true
    }

    async fn execute(&self, input: Value, context: &ToolContext) -> Result<ToolResult, ToolError> {
        let name = input
            .get("name")
            .and_then(Value::as_str)
            .unwrap_or("")
            .trim();

        // #432: walk every candidate skill directory (workspace
        // .agents/skills, skills, .opencode/skills, .claude/skills,
        // .cursor/skills, ~/.agents/skills, global default), merging with
        // first-wins precedence. The
        // tool's lookup mirrors what the system-prompt skills block
        // already lists, so the model never asks for a name it
        // can't find.
        let discovery_mode =
            SkillDiscoveryMode::from_codewhale_only(context.skills_scan_codewhale_only);
        let registry = if let Some(skills_dir) = context.skills_dir.as_deref() {
            discover_for_workspace_and_dir_with_mode_and_plugins(
                &context.workspace,
                skills_dir,
                discovery_mode,
                context.plugin_registry.as_deref(),
            )
        } else {
            discover_in_workspace_with_mode_and_plugins(
                &context.workspace,
                discovery_mode,
                context.plugin_registry.as_deref(),
            )
        }
        .into_enabled();

        // Listing mode: empty name, "*", or "list" returns the full registry (#4651).
        if name.is_empty() || name == "*" || name == "list" {
            let skills = registry.list();
            if skills.is_empty() {
                return Ok(ToolResult::success("No skills installed."));
            }
            let mut listing = format!("Available skills ({}):\n", skills.len());
            for skill in skills {
                if skill.description.trim().is_empty() {
                    listing.push_str(&format!("  - {}\n", skill.name));
                } else {
                    listing.push_str(&format!("  - {} — {}\n", skill.name, skill.description));
                }
            }
            return Ok(ToolResult::success(listing));
        }

        let Some(skill) = registry.get(name) else {
            let available: Vec<&str> = registry.list().iter().map(|s| s.name.as_str()).collect();
            let hint = if available.is_empty() {
                let dirs: Vec<String> = context
                    .skills_dir
                    .as_deref()
                    .map(|skills_dir| {
                        skill_directories_for_workspace_and_dir(
                            &context.workspace,
                            skills_dir,
                            discovery_mode,
                        )
                    })
                    .unwrap_or_else(|| {
                        skills_directories_for_mode(&context.workspace, discovery_mode)
                    })
                    .iter()
                    .map(|p| p.display().to_string())
                    .collect();
                if dirs.is_empty() {
                    if context.skills_scan_codewhale_only {
                        "no skills directories found; install skills under `<workspace>/.codewhale/skills/<name>/SKILL.md` or `~/.codewhale/skills/<name>/SKILL.md`"
                            .to_string()
                    } else {
                        "no skills directories found; install skills under `<workspace>/.agents/skills/<name>/SKILL.md`, `~/.codewhale/skills/<name>/SKILL.md`, or `~/.deepseek/skills/<name>/SKILL.md`"
                            .to_string()
                    }
                } else {
                    format!("no skills installed. Searched: {}", dirs.join(", "))
                }
            } else {
                format!(
                    "skill `{name}` not found. Available: {}",
                    available.join(", ")
                )
            };
            return Err(ToolError::execution_failed(hint));
        };

        ensure_reviewed_plugin_skill_is_current(skill, &context.workspace)?;
        ensure_native_skill_file_present(skill)?;
        let body = format_skill_body(skill);
        let (skill_path, skill_source) = match &skill.source {
            SkillSource::Native => (Some(skill.path.display().to_string()), "native".to_string()),
            SkillSource::Plugin {
                plugin_id,
                plugin_name,
                ..
            } => (
                None,
                format!("reviewed-plugin-snapshot:{plugin_name}:{plugin_id}"),
            ),
        };
        Ok(ToolResult::success(body).with_metadata(json!({
            "skill_name": skill.name,
            "skill_path": skill_path,
            "skill_source": skill_source,
            "companion_files": collect_companion_files(skill)
                .into_iter()
                .map(|p| p.display().to_string())
                .collect::<Vec<String>>(),
        })))
    }
}

/// A native registry entry whose SKILL.md vanished from disk after discovery
/// (deleted, or resolved under a wrong home directory) must fail loudly with
/// the exact path — never silently serve the stale cached body while the user
/// believes the skill loaded (§2.5).
fn ensure_native_skill_file_present(skill: &Skill) -> Result<(), ToolError> {
    if !matches!(skill.source, SkillSource::Native) || skill.path.is_file() {
        return Ok(());
    }
    let message = format!(
        "Skill `{}` is registered at {} but that file no longer exists on disk, \
         so the skill did not load. Restore the file, or fix the skills directory it \
         came from (`skills_dir` in config.toml, `$CODEWHALE_HOME`, or the OS home) — \
         the path above shows exactly where the runtime looked.",
        skill.name,
        skill.path.display()
    );
    crate::logging::warn(&message);
    Err(ToolError::execution_failed(message))
}

fn ensure_reviewed_plugin_skill_is_current(
    skill: &Skill,
    workspace: &std::path::Path,
) -> Result<(), ToolError> {
    let SkillSource::Plugin {
        plugin_name,
        authority,
        ..
    } = &skill.source
    else {
        return Ok(());
    };

    if authority.workspace != workspace {
        return Err(ToolError::execution_failed(format!(
            "Plugin skill `{}` belongs to a different workspace and was denied",
            skill.name
        )));
    }

    crate::plugins::registry::verify_plugin_authority(authority).map_err(|reason| {
        ToolError::execution_failed(format!(
            "Plugin skill `{}` was denied: {reason}. Run `/plugin reload`, inspect `/plugin show {plugin_name}`, then repeat the displayed trust command and enable it before retrying",
            skill.name
        ))
    })
}

/// Render the skill body the model will see. Includes the description
/// up top so a single tool result is self-contained — no need to
/// cross-reference the system-prompt catalogue. Companion-file paths
/// land at the bottom under a clearly-named heading so the model can
/// open them with `read_file` if they're relevant to the task.
fn format_skill_body(skill: &Skill) -> String {
    let mut out = String::new();
    out.push_str(&format!("# Skill: {}\n\n", skill.name));
    if !skill.description.trim().is_empty() {
        out.push_str(&format!("> {}\n\n", skill.description.trim()));
    }
    let invocation = match skill.invocation {
        crate::skills::SkillInvocation::ModelAndUser => "model+user",
        crate::skills::SkillInvocation::ExplicitOnly => "explicit-only",
    };
    out.push_str(&format!("Invocation: `{invocation}`\n"));
    if !skill.aliases.is_empty() {
        out.push_str(&format!("Aliases: `{}`\n", skill.aliases.join("`, `")));
    }
    out.push('\n');
    match &skill.source {
        SkillSource::Native => out.push_str(&format!("Source: `{}`\n\n", skill.path.display())),
        SkillSource::Plugin {
            plugin_id,
            plugin_name,
            ..
        } => out.push_str(&format!(
            "Source: reviewed in-memory plugin snapshot `{plugin_name}` ({plugin_id})\n\n"
        )),
    }
    out.push_str("## SKILL.md\n\n");
    out.push_str(skill.body.trim());
    out.push('\n');

    let companions = collect_companion_files(skill);
    if !companions.is_empty() {
        out.push_str("\n## Companion files\n\n");
        out.push_str(
            "Sibling files in the skill directory. Open one with File action=\"read\" when the task requires it; a skill stored outside the workspace has to be read through Bash instead.\n\n",
        );
        for path in &companions {
            out.push_str(&format!("- `{}`\n", path.display()));
        }
    }
    out
}

/// List sibling files of `SKILL.md` in the skill's own directory.
/// Skips the `SKILL.md` itself and any nested directories so the
/// listing stays focused on at-hand resources. Sorted lexically for
/// deterministic output (matters for transcript diffing in tests).
fn collect_companion_files(skill: &Skill) -> Vec<std::path::PathBuf> {
    if matches!(&skill.source, SkillSource::Plugin { .. }) {
        // Companion files remain hashed, but exposing their mutable on-disk
        // paths would let content change after review and bypass the snapshot.
        return Vec::new();
    }
    let Some(dir) = skill.path.parent() else {
        return Vec::new();
    };
    let mut entries: Vec<std::path::PathBuf> = match std::fs::read_dir(dir) {
        Ok(rd) => rd
            .flatten()
            .filter_map(|entry| {
                let path = entry.path();
                let is_file = entry.file_type().is_ok_and(|ft| ft.is_file());
                let is_skill_md = path.file_name().and_then(|s| s.to_str()) == Some("SKILL.md");
                if is_file && !is_skill_md {
                    Some(path)
                } else {
                    None
                }
            })
            .collect(),
        Err(_) => Vec::new(),
    };
    entries.sort();
    entries
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::skills::SkillRegistry;
    use std::fs;
    use tempfile::tempdir;

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

    #[test]
    fn load_skill_returns_skill_body_with_description_header() {
        let tmp = tempdir().unwrap();
        write_skill(
            tmp.path(),
            "review-pr",
            "Run a focused PR review",
            "# Steps\n1. Read the diff.\n2. Comment.\n",
        );
        let skill = SkillRegistry::discover(tmp.path())
            .get("review-pr")
            .unwrap()
            .clone();
        let body = format_skill_body(&skill);
        assert!(body.contains("# Skill: review-pr"));
        assert!(body.contains("Run a focused PR review"));
        assert!(body.contains("# Steps"));
        assert!(body.contains("Read the diff."));
    }

    #[test]
    fn collect_companion_files_lists_siblings_excluding_skill_md() {
        let tmp = tempdir().unwrap();
        let skill_dir = tmp.path().join("rich-skill");
        fs::create_dir_all(&skill_dir).unwrap();
        fs::write(
            skill_dir.join("SKILL.md"),
            "---\nname: rich-skill\ndescription: x\n---\nbody\n",
        )
        .unwrap();
        fs::write(skill_dir.join("script.py"), "print('hi')").unwrap();
        fs::write(skill_dir.join("data.json"), "{}").unwrap();
        // Nested directory — skipped by collect_companion_files.
        fs::create_dir_all(skill_dir.join("subdir")).unwrap();

        let registry = SkillRegistry::discover(tmp.path());
        let skill = registry.get("rich-skill").unwrap();
        let files = collect_companion_files(skill);
        let names: Vec<String> = files
            .iter()
            .filter_map(|p| p.file_name().and_then(|s| s.to_str().map(str::to_string)))
            .collect();
        assert_eq!(
            names,
            vec!["data.json".to_string(), "script.py".to_string()]
        );
    }

    #[test]
    fn native_skill_with_vanished_file_fails_loudly_with_the_path() {
        // §2.5: a registry entry pointing at a SKILL.md that no longer exists
        // must surface the exact path instead of silently serving the stale
        // cached body — this is the "delegate skill silently never loads"
        // symptom class.
        let tmp = tempdir().unwrap();
        let missing = tmp.path().join("delegate").join("SKILL.md");
        let skill = Skill {
            name: "delegate".to_string(),
            description: "delegate work".to_string(),
            localized_descriptions: std::collections::HashMap::new(),
            invocation: crate::skills::SkillInvocation::ModelAndUser,
            aliases: Vec::new(),
            body: "cached body".to_string(),
            path: missing.clone(),
            source: SkillSource::Native,
        };
        let err = ensure_native_skill_file_present(&skill)
            .expect_err("a vanished SKILL.md must fail loudly");
        let message = err.to_string();
        assert!(
            message.contains(&missing.display().to_string()),
            "error names the exact path: {message}"
        );
        assert!(
            message.contains("did not load"),
            "error says the skill did not load: {message}"
        );

        // An existing file passes, and plugin skills are untouched (their
        // content-bound snapshot never consults the mutable path).
        let present_dir = tempdir().unwrap();
        let present = present_dir.path().join("SKILL.md");
        fs::write(&present, "body").unwrap();
        let mut on_disk = skill.clone();
        on_disk.path = present;
        ensure_native_skill_file_present(&on_disk).expect("present file loads");
        let mut plugin = skill;
        plugin.source = SkillSource::Plugin {
            plugin_id: "workspace/1/demo".to_string(),
            plugin_name: "demo".to_string(),
            authority: Box::new(crate::plugins::types::PluginAuthority {
                plugin_id: crate::plugins::types::PluginId("workspace/1/demo".to_string()),
                plugin_name: "demo".to_string(),
                workspace: tmp.path().to_path_buf(),
                state_path: tmp.path().join("state.json"),
                source_manifest: tmp.path().join("plugin.toml"),
                staged_manifest: tmp.path().join("staged/plugin.toml"),
                content_hash: "0".repeat(64),
                capability_hash: "0".repeat(64),
                state_generation: 0,
            }),
        };
        ensure_native_skill_file_present(&plugin).expect("plugin snapshot skips the disk check");
    }

    #[test]
    fn plugin_skill_body_uses_reviewed_snapshot_without_mutable_file_paths() {
        let tmp = tempdir().unwrap();
        let skill_path = tmp.path().join("SKILL.md");
        fs::write(&skill_path, "changed on disk").unwrap();
        fs::write(tmp.path().join("companion.txt"), "changed companion").unwrap();
        let skill = Skill {
            name: "demo:hello".to_string(),
            description: "hello".to_string(),
            localized_descriptions: std::collections::HashMap::new(),
            invocation: crate::skills::SkillInvocation::ModelAndUser,
            aliases: Vec::new(),
            body: "reviewed body".to_string(),
            path: skill_path.clone(),
            source: SkillSource::Plugin {
                plugin_id: "workspace/123/demo".to_string(),
                plugin_name: "demo".to_string(),
                authority: Box::new(crate::plugins::types::PluginAuthority {
                    plugin_id: crate::plugins::types::PluginId("workspace/123/demo".to_string()),
                    plugin_name: "demo".to_string(),
                    workspace: tmp.path().to_path_buf(),
                    state_path: tmp.path().join("state.json"),
                    source_manifest: tmp.path().join("plugin.toml"),
                    staged_manifest: tmp.path().join("staged/plugin.toml"),
                    content_hash: "0".repeat(64),
                    capability_hash: "0".repeat(64),
                    state_generation: 0,
                }),
            },
        };

        let rendered = format_skill_body(&skill);
        assert!(rendered.contains("reviewed body"));
        assert!(rendered.contains("reviewed in-memory plugin snapshot"));
        assert!(!rendered.contains(&skill_path.display().to_string()));
        assert!(collect_companion_files(&skill).is_empty());
    }

    #[test]
    fn plugin_skill_load_fails_closed_when_reviewed_bundle_drifts() {
        let _lock = crate::test_support::lock_test_env();
        let tmp = tempdir().unwrap();
        let home = tmp.path().join("home");
        let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", &home);
        let bundle = tmp.path().join(".codewhale/plugins/demo");
        let skill_dir = bundle.join("skills/hello");
        fs::create_dir_all(&skill_dir).unwrap();
        fs::write(
            bundle.join("plugin.toml"),
            "schema_version = 1\n[plugin]\nname = \"demo\"\nversion = \"1.0.0\"\n[skills]\npath = \"skills\"\n",
        )
        .unwrap();
        fs::write(
            skill_dir.join("SKILL.md"),
            "---\nname: hello\ndescription: hello\n---\nreviewed body\n",
        )
        .unwrap();
        fs::write(skill_dir.join("companion.txt"), "reviewed companion").unwrap();

        let discovery = crate::plugins::PluginDiscoveryContext::capture_pre_dotenv();
        let mut plugins = discovery.registry_for_workspace(tmp.path());
        std::sync::Arc::make_mut(&mut plugins)
            .trust("demo")
            .unwrap();
        std::sync::Arc::make_mut(&mut plugins)
            .enable("demo")
            .unwrap();
        let registry = crate::skills::discover_in_workspace_with_mode_and_plugins(
            tmp.path(),
            SkillDiscoveryMode::CodeWhaleOnly,
            Some(plugins.as_ref()),
        );
        let skill = registry.get("demo:hello").expect("active plugin skill");
        ensure_reviewed_plugin_skill_is_current(skill, tmp.path())
            .expect("stable reviewed snapshot");

        fs::write(skill_dir.join("companion.txt"), "changed after review").unwrap();
        let error = ensure_reviewed_plugin_skill_is_current(skill, tmp.path())
            .expect_err("bundle drift must deny the reviewed skill snapshot");
        assert!(error.to_string().contains("changed after review"));
    }

    #[test]
    fn collect_companion_files_returns_empty_for_solo_skill() {
        let tmp = tempdir().unwrap();
        write_skill(tmp.path(), "solo", "Just a skill", "body");
        let registry = SkillRegistry::discover(tmp.path());
        let skill = registry.get("solo").unwrap();
        assert!(collect_companion_files(skill).is_empty());
    }

    #[test]
    fn format_skill_body_emits_companion_files_section_when_present() {
        let tmp = tempdir().unwrap();
        let skill_dir = tmp.path().join("skill-with-friends");
        fs::create_dir_all(&skill_dir).unwrap();
        fs::write(
            skill_dir.join("SKILL.md"),
            "---\nname: skill-with-friends\ndescription: x\n---\nbody\n",
        )
        .unwrap();
        fs::write(skill_dir.join("helper.sh"), "#!/bin/sh\necho hi").unwrap();

        let registry = SkillRegistry::discover(tmp.path());
        let skill = registry.get("skill-with-friends").unwrap();
        let body = format_skill_body(skill);
        assert!(body.contains("## Companion files"));
        assert!(body.contains("helper.sh"));
    }

    #[test]
    fn format_skill_body_skips_companion_section_when_solo() {
        let tmp = tempdir().unwrap();
        write_skill(tmp.path(), "solo", "x", "body");
        let registry = SkillRegistry::discover(tmp.path());
        let skill = registry.get("solo").unwrap();
        let body = format_skill_body(skill);
        assert!(
            !body.contains("## Companion files"),
            "solo skills shouldn't emit an empty Companion files section"
        );
    }

    #[tokio::test]
    async fn execute_lists_available_skills_for_empty_star_and_list_names() {
        let _lock = crate::test_support::lock_test_env();
        let tmp = tempdir().unwrap();
        // Pin home-based global skill roots to the tempdir so host skills
        // never leak into the listing count.
        let _home = crate::test_support::EnvVarGuard::set("HOME", tmp.path().join("home"));
        let _cw_home =
            crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", tmp.path().join("cw-home"));
        let workspace = tmp.path().to_path_buf();
        let skills_dir = workspace.join(".codewhale").join("skills");
        write_skill(&skills_dir, "alpha-skill", "First demo skill", "Body A.");
        write_skill(&skills_dir, "beta-skill", "", "Body B.");

        let context = ToolContext::new(workspace);
        let tool = LoadSkillTool;

        // #4651: listing is an action inside the single load_skill tool —
        // empty name, "*", and "list" all enumerate the reviewed registry.
        for listing_name in [json!({}), json!({"name": "*"}), json!({"name": "list"})] {
            let result = tool
                .execute(listing_name.clone(), &context)
                .await
                .expect("listing should succeed");
            assert!(result.success);
            assert!(
                result.content.contains("Available skills (2)"),
                "listing for {listing_name} should count skills: {}",
                result.content
            );
            assert!(
                result.content.contains("alpha-skill — First demo skill"),
                "listing should include name and description: {}",
                result.content
            );
            assert!(
                result.content.contains("- beta-skill"),
                "listing should include description-less skills: {}",
                result.content
            );
        }
    }

    #[tokio::test]
    async fn execute_listing_reports_empty_registry_plainly() {
        let _lock = crate::test_support::lock_test_env();
        let tmp = tempdir().unwrap();
        let _home = crate::test_support::EnvVarGuard::set("HOME", tmp.path().join("home"));
        let _cw_home =
            crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", tmp.path().join("cw-home"));
        let context = ToolContext::new(tmp.path().to_path_buf());
        let result = LoadSkillTool
            .execute(json!({"name": "list"}), &context)
            .await
            .expect("empty listing should still succeed");
        assert!(result.success);
        assert!(
            result.content.contains("No skills installed."),
            "{}",
            result.content
        );
    }

    #[tokio::test]
    async fn execute_finds_skills_in_opencode_dir_via_workspace_discovery() {
        let tmp = tempdir().unwrap();
        let workspace = tmp.path().to_path_buf();
        // Skill installed under workspace `.opencode/skills` (#432).
        let opencode_dir = workspace.join(".opencode").join("skills");
        std::fs::create_dir_all(&opencode_dir).unwrap();
        write_skill(
            &opencode_dir,
            "from-opencode",
            "Skill installed under .opencode/skills",
            "Body content marker.",
        );

        let mut context = ToolContext::new(workspace);
        // The skill tool reads $HOME for the global default; pin it to a
        // tempdir so the test is hermetic regardless of the host's
        // ~/.deepseek/skills.
        context.workspace = tmp.path().to_path_buf();

        let tool = LoadSkillTool;
        let result = tool
            .execute(json!({"name": "from-opencode"}), &context)
            .await
            .expect("load_skill should succeed");
        assert!(result.success);
        assert!(
            result.content.contains("# Skill: from-opencode"),
            "body header missing: {}",
            result.content
        );
        assert!(result.content.contains("Body content marker."));

        let metadata = result.metadata.expect("metadata stamped");
        assert_eq!(
            metadata
                .get("skill_name")
                .and_then(serde_json::Value::as_str),
            Some("from-opencode")
        );
        let path_str = metadata
            .get("skill_path")
            .and_then(serde_json::Value::as_str)
            .expect("skill_path stamped");
        assert!(
            path_str.contains(".opencode"),
            "skill_path should point at the .opencode dir: {path_str}"
        );
    }

    #[tokio::test]
    async fn execute_respects_codewhale_only_skill_discovery() {
        let tmp = tempdir().unwrap();
        let workspace = tmp.path().to_path_buf();
        write_skill(
            &workspace.join(".claude").join("skills"),
            "claude-only",
            "Claude skill",
            "Body content marker.",
        );
        let codewhale_dir = workspace.join(".codewhale").join("skills");
        write_skill(
            &codewhale_dir,
            "codewhale-only",
            "CodeWhale skill",
            "Body content marker.",
        );

        let context = ToolContext::new(workspace).with_skills_config(codewhale_dir, true);
        let tool = LoadSkillTool;

        let result = tool
            .execute(json!({"name": "codewhale-only"}), &context)
            .await
            .expect("CodeWhale skill should load");
        assert!(result.success);

        let err = tool
            .execute(json!({"name": "claude-only"}), &context)
            .await
            .expect_err("Claude skill should be hidden in CodeWhale-only mode");
        let msg = err.to_string();
        assert!(
            msg.contains("claude-only") && msg.contains("codewhale-only"),
            "error should name the missing skill and available strict catalog: {msg}"
        );
    }

    #[tokio::test]
    async fn execute_loads_configured_external_skill_without_workspace_trust() {
        let tmp = tempdir().unwrap();
        let workspace = tmp.path().join("workspace");
        let home = tmp.path().join("home");
        let global_skills = home.join(".codewhale/skills");
        fs::create_dir_all(&workspace).unwrap();
        write_skill(
            &global_skills,
            "global-helper",
            "Global helper",
            "Global body marker.",
        );

        // Keep this test independent of the process-native home directory:
        // `crate::config::effective_home_dir()` cannot be redirected reliably after process start
        // on Windows. The injected-home discovery test in `skills::tests`
        // separately proves that ~/.codewhale/skills enters the default catalog.
        let context = ToolContext::new(&workspace).with_skills_config(global_skills.clone(), false);
        assert!(!context.trust_mode);
        assert!(
            context
                .resolve_path(
                    global_skills
                        .join("global-helper/SKILL.md")
                        .to_str()
                        .unwrap()
                )
                .is_err(),
            "ordinary file tools must retain the workspace boundary"
        );

        let result = LoadSkillTool
            .execute(json!({"name": "global-helper"}), &context)
            .await
            .expect("load_skill host lookup should open a configured external skill root");
        assert!(result.success);
        assert!(result.content.contains("Global body marker."));
    }

    #[tokio::test]
    async fn execute_returns_helpful_error_for_unknown_skill() {
        let tmp = tempdir().unwrap();
        let workspace = tmp.path().to_path_buf();
        // One real skill so the available list is non-empty.
        write_skill(
            &workspace.join(".agents").join("skills"),
            "real-one",
            "x",
            "body",
        );

        let context = ToolContext::new(workspace);
        let tool = LoadSkillTool;
        let err = tool
            .execute(json!({"name": "imaginary"}), &context)
            .await
            .expect_err("unknown skill should error");
        let msg = err.to_string();
        assert!(
            msg.contains("imaginary") && msg.contains("real-one"),
            "error must name the missing skill and list available ones: {msg}"
        );
    }
}