diff --git a/cmd/skillshare/install_handlers.go b/cmd/skillshare/install_handlers.go index de93b0507..f4df660f2 100644 --- a/cmd/skillshare/install_handlers.go +++ b/cmd/skillshare/install_handlers.go @@ -440,7 +440,7 @@ func installSelectedSkills(selected []install.SkillInfo, discovery *install.Disc } } - // Reorder: install root skill first so children can nest under it + // Reorder: install root skill first so children nest under it on disk. orderedSkills := selected if rootIdx > 0 { orderedSkills = make([]install.SkillInfo, 0, len(selected)) @@ -449,9 +449,6 @@ func installSelectedSkills(selected []install.SkillInfo, discovery *install.Disc orderedSkills = append(orderedSkills, selected[rootIdx+1:]...) } - // Track if root was installed (children are already included in root) - rootInstalled := false - for i, skill := range orderedSkills { if installSpinner != nil { installSpinner.NextStep(fmt.Sprintf("Installing %s...", skill.Name)) @@ -464,6 +461,10 @@ func installSelectedSkills(selected []install.SkillInfo, discovery *install.Disc } // Determine destination path and effective --into for force hints. + // Orchestrator repos: when the root skill is also selected, children + // install as siblings under / so they remain independent + // skills discoverable by sync (issue #124). The root copy excludes + // child skill directories so they do not duplicate. var destPath string skillOpts := opts if skill.Path == "." { @@ -482,15 +483,6 @@ func installSelectedSkills(selected []install.SkillInfo, discovery *install.Disc destPath = destWithInto(cfg.Source, opts, skill.Name) } - // If root was installed, children are already included - skip reinstall - if rootInstalled && skill.Path != "." { - results = append(results, skillInstallResult{skill: skill, success: true, message: fmt.Sprintf("included in %s", parentName)}) - if progressBar != nil { - progressBar.Increment() - } - continue - } - installResult, err := install.InstallFromDiscovery(discovery, skill, destPath, skillOpts) if err != nil { r := skillInstallResult{skill: skill, success: false, message: err.Error(), err: err} @@ -504,9 +496,6 @@ func installSelectedSkills(selected []install.SkillInfo, discovery *install.Disc continue } - if skill.Path == "." { - rootInstalled = true - } message := "installed" if len(installResult.Warnings) > 0 { message = fmt.Sprintf("installed (%d warning(s))", len(installResult.Warnings)) diff --git a/internal/install/copy.go b/internal/install/copy.go index a3d71bbe9..ca6496c41 100644 --- a/internal/install/copy.go +++ b/internal/install/copy.go @@ -6,7 +6,17 @@ import ( "path/filepath" ) +// copyDir recursively copies src into dst, skipping any `.git` directory. func copyDir(src, dst string) error { + return copyDirExcluding(src, dst, nil) +} + +// copyDirExcluding recursively copies src into dst, skipping any `.git` +// directory and any subdirectory whose slash-normalized path relative to src +// appears in excludes. The excludes keys must use forward slashes (e.g. +// "skills/officecli-pptx"). Passing a nil or empty map is equivalent to +// copyDir. +func copyDirExcluding(src, dst string, excludes map[string]bool) error { return filepath.Walk(src, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -20,6 +30,15 @@ func copyDir(src, dst string) error { return filepath.SkipDir } + // Skip excluded subdirectories (e.g. child skill dirs when copying + // the root of an orchestrator repo so they do not duplicate into the + // root install). + if len(excludes) > 0 && info.IsDir() && relPath != "." { + if excludes[filepath.ToSlash(relPath)] { + return filepath.SkipDir + } + } + if info.IsDir() { return os.MkdirAll(dstPath, info.Mode()) } diff --git a/internal/install/copy_test.go b/internal/install/copy_test.go index 491d9c7af..b816ebc4a 100644 --- a/internal/install/copy_test.go +++ b/internal/install/copy_test.go @@ -61,6 +61,78 @@ func TestCopyDir_SkipsGit(t *testing.T) { } } +func TestCopyDirExcluding_SkipsChildSkillDirs(t *testing.T) { + src := t.TempDir() + dst := filepath.Join(t.TempDir(), "dest") + + // Orchestrator-style repo layout (issue #124): + // SKILL.md (root skill) + // README.md + // src/helper.go (root-owned asset, must remain) + // skills/child-a/SKILL.md (child skill, must be excluded) + // skills/child-b/SKILL.md (child skill, must be excluded) + // skills/shared/README.md (NOT a skill dir, must remain) + os.WriteFile(filepath.Join(src, "SKILL.md"), []byte("# Root"), 0644) + os.WriteFile(filepath.Join(src, "README.md"), []byte("root readme"), 0644) + os.MkdirAll(filepath.Join(src, "src"), 0755) + os.WriteFile(filepath.Join(src, "src", "helper.go"), []byte("package main"), 0644) + os.MkdirAll(filepath.Join(src, "skills", "child-a"), 0755) + os.WriteFile(filepath.Join(src, "skills", "child-a", "SKILL.md"), []byte("# A"), 0644) + os.MkdirAll(filepath.Join(src, "skills", "child-b"), 0755) + os.WriteFile(filepath.Join(src, "skills", "child-b", "SKILL.md"), []byte("# B"), 0644) + os.MkdirAll(filepath.Join(src, "skills", "shared"), 0755) + os.WriteFile(filepath.Join(src, "skills", "shared", "README.md"), []byte("shared"), 0644) + + excludes := map[string]bool{ + "skills/child-a": true, + "skills/child-b": true, + } + if err := copyDirExcluding(src, dst, excludes); err != nil { + t.Fatal(err) + } + + // Files that MUST exist in the copy + mustExist := []string{ + "SKILL.md", + "README.md", + filepath.Join("src", "helper.go"), + filepath.Join("skills", "shared", "README.md"), + } + for _, rel := range mustExist { + if _, err := os.Stat(filepath.Join(dst, rel)); err != nil { + t.Errorf("expected %q to be copied, got %v", rel, err) + } + } + + // Files that MUST NOT exist (excluded child skill dirs) + mustNotExist := []string{ + filepath.Join("skills", "child-a"), + filepath.Join("skills", "child-b"), + filepath.Join("skills", "child-a", "SKILL.md"), + } + for _, rel := range mustNotExist { + if _, err := os.Stat(filepath.Join(dst, rel)); !os.IsNotExist(err) { + t.Errorf("expected %q to be excluded, but it exists (err=%v)", rel, err) + } + } +} + +func TestCopyDirExcluding_NilMapEqualsCopyDir(t *testing.T) { + src := t.TempDir() + dst := filepath.Join(t.TempDir(), "dest") + + os.WriteFile(filepath.Join(src, "a.txt"), []byte("a"), 0644) + os.MkdirAll(filepath.Join(src, "sub"), 0755) + os.WriteFile(filepath.Join(src, "sub", "b.txt"), []byte("b"), 0644) + + if err := copyDirExcluding(src, dst, nil); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(dst, "sub", "b.txt")); err != nil { + t.Errorf("expected sub/b.txt to be copied, got %v", err) + } +} + func TestCopyDir_PreservesPermissions(t *testing.T) { src := t.TempDir() dst := filepath.Join(t.TempDir(), "dest") diff --git a/internal/install/install_apply.go b/internal/install/install_apply.go index 5be65c4fb..5b5adbf7b 100644 --- a/internal/install/install_apply.go +++ b/internal/install/install_apply.go @@ -32,6 +32,50 @@ func discoverySourceRoot(discovery *DiscoveryResult) string { return filepath.Join(discovery.RepoPath, "repo") } +// descendantSkillPaths returns the set of slash-normalized paths (relative to +// the source root of `skill`) of every other discovered skill that lives +// strictly inside `skill`. The returned map is suitable as the excludes +// argument to copyDirExcluding. +// +// For the root skill (Path="."), every other discovered skill counts as a +// descendant. For a non-root skill, only nested sub-skills count. Returns nil +// when there is nothing to exclude so callers hit the fast path in +// copyDirExcluding. +func descendantSkillPaths(discovery *DiscoveryResult, skill SkillInfo) map[string]bool { + if discovery == nil || len(discovery.Skills) <= 1 { + return nil + } + current := filepath.ToSlash(skill.Path) + var prefix string + if current != "." { + prefix = current + "/" + } + excludes := make(map[string]bool) + for _, other := range discovery.Skills { + otherPath := filepath.ToSlash(other.Path) + if otherPath == current { + continue + } + if current == "." { + // Root skill: every other skill path is a strict descendant. + if otherPath != "." { + excludes[otherPath] = true + } + continue + } + // Non-root: only paths strictly under current count. + if rel, ok := strings.CutPrefix(otherPath, prefix); ok { + // rel is the path relative to `skill` (which is how + // copyDirExcluding walks `srcPath`). + excludes[rel] = true + } + } + if len(excludes) == 0 { + return nil + } + return excludes +} + func installAgentRelativePath(agent AgentInfo) string { return strings.TrimPrefix(filepath.ToSlash(agent.Path), "agents/") } @@ -330,7 +374,13 @@ func installFromDiscoveryImpl(discovery *DiscoveryResult, skill SkillInfo, destP srcPath = filepath.Join(sourceRoot, skill.Path) } - if err := copyDir(srcPath, destPath); err != nil { + // In an orchestrator-style repo (root SKILL.md + children), copying the + // root would otherwise drag every child skill directory into the root + // install. Exclude any other discovered skill whose path is a strict + // descendant of the skill currently being installed (issue #124). + excludes := descendantSkillPaths(discovery, skill) + + if err := copyDirExcluding(srcPath, destPath, excludes); err != nil { return nil, fmt.Errorf("failed to copy skill: %w", err) } diff --git a/internal/install/install_local_test.go b/internal/install/install_local_test.go index 48f937a95..131f1d4af 100644 --- a/internal/install/install_local_test.go +++ b/internal/install/install_local_test.go @@ -497,3 +497,107 @@ func TestCheckSkillFile_Missing(t *testing.T) { t.Errorf("expected 1 warning for missing SKILL.md, got %d", len(result.Warnings)) } } + +// TestInstallFromDiscovery_Orchestrator_RootExcludesChildren verifies issue +// #124: installing a root skill of an orchestrator repo must not drag child +// skill directories into the root install. Each child must install to its own +// directory as an independent skill. +func TestInstallFromDiscovery_Orchestrator_RootExcludesChildren(t *testing.T) { + tmp := t.TempDir() + repoDir := filepath.Join(tmp, "orchestrator") + os.MkdirAll(repoDir, 0755) + + // Layout: + // SKILL.md (root skill) + // README.md + // src/helper.go (root-owned asset) + // skills/child-a/SKILL.md (child skill) + // skills/child-b/SKILL.md (child skill) + os.WriteFile(filepath.Join(repoDir, "SKILL.md"), + []byte("---\nname: orchestrator\n---\n# Root"), 0644) + os.WriteFile(filepath.Join(repoDir, "README.md"), []byte("readme"), 0644) + os.MkdirAll(filepath.Join(repoDir, "src"), 0755) + os.WriteFile(filepath.Join(repoDir, "src", "helper.go"), []byte("package main"), 0644) + os.MkdirAll(filepath.Join(repoDir, "skills", "child-a"), 0755) + os.WriteFile(filepath.Join(repoDir, "skills", "child-a", "SKILL.md"), + []byte("---\nname: child-a\n---\n# A"), 0644) + os.MkdirAll(filepath.Join(repoDir, "skills", "child-b"), 0755) + os.WriteFile(filepath.Join(repoDir, "skills", "child-b", "SKILL.md"), + []byte("---\nname: child-b\n---\n# B"), 0644) + + source := &Source{ + Type: SourceTypeLocalPath, + Raw: repoDir, + Path: repoDir, + Name: "orchestrator", + } + + discovery, err := DiscoverLocal(source) + if err != nil { + t.Fatalf("DiscoverLocal failed: %v", err) + } + if len(discovery.Skills) != 3 { + t.Fatalf("expected 3 discovered skills (root + 2 children), got %d: %+v", + len(discovery.Skills), discovery.Skills) + } + + // Locate root and children in the discovery result. + var root *SkillInfo + var children []SkillInfo + for i := range discovery.Skills { + if discovery.Skills[i].Path == "." { + root = &discovery.Skills[i] + } else { + children = append(children, discovery.Skills[i]) + } + } + if root == nil { + t.Fatal("expected root skill with Path=\".\"") + } + if len(children) != 2 { + t.Fatalf("expected 2 children, got %d", len(children)) + } + + // Install the root skill first. + rootDest := filepath.Join(tmp, "dest", "orchestrator") + if _, err := InstallFromDiscovery(discovery, *root, rootDest, + InstallOptions{SourceDir: filepath.Join(tmp, "dest"), SkipAudit: true}); err != nil { + t.Fatalf("root install failed: %v", err) + } + + // Root dest MUST contain its own files and non-skill subdirs. + for _, rel := range []string{ + "SKILL.md", + "README.md", + filepath.Join("src", "helper.go"), + } { + if _, err := os.Stat(filepath.Join(rootDest, rel)); err != nil { + t.Errorf("expected %q in root install, got %v", rel, err) + } + } + + // Root dest MUST NOT contain child skill directories or their SKILL.md. + for _, rel := range []string{ + filepath.Join("skills", "child-a"), + filepath.Join("skills", "child-b"), + filepath.Join("skills", "child-a", "SKILL.md"), + filepath.Join("skills", "child-b", "SKILL.md"), + } { + if _, err := os.Stat(filepath.Join(rootDest, rel)); !os.IsNotExist(err) { + t.Errorf("expected %q to be excluded from root install, but it exists (err=%v)", + rel, err) + } + } + + // Install each child as an independent skill under the root dir. + for _, child := range children { + childDest := filepath.Join(rootDest, child.Name) + if _, err := InstallFromDiscovery(discovery, child, childDest, + InstallOptions{SourceDir: filepath.Join(tmp, "dest"), SkipAudit: true}); err != nil { + t.Fatalf("child %q install failed: %v", child.Name, err) + } + if _, err := os.Stat(filepath.Join(childDest, "SKILL.md")); err != nil { + t.Errorf("expected child %q SKILL.md at %q, got %v", child.Name, childDest, err) + } + } +}