|
| 1 | +package skills |
| 2 | + |
| 3 | +import ( |
| 4 | + "cmp" |
| 5 | + "io/fs" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "slices" |
| 9 | + |
| 10 | + "github.com/docker/docker-agent/pkg/paths" |
| 11 | +) |
| 12 | + |
| 13 | +const skillFile = "SKILL.md" |
| 14 | + |
| 15 | +// localSearchPath describes one directory to scan for local skills, and |
| 16 | +// whether subdirectories should be walked recursively (Codex/agents format) |
| 17 | +// or only as immediate children of the search root (Claude format). |
| 18 | +type localSearchPath struct { |
| 19 | + dir string |
| 20 | + recursive bool |
| 21 | +} |
| 22 | + |
| 23 | +// localSearchPaths returns every directory to scan for local skills, in |
| 24 | +// load order. Entries appearing later in the list override skills with the |
| 25 | +// same name from earlier entries: |
| 26 | +// |
| 27 | +// 1. Global directories (under $HOME). |
| 28 | +// 2. .claude/skills under cwd (Claude project format, flat). |
| 29 | +// 3. .agents/skills from the git root down to cwd (closest wins). |
| 30 | +func localSearchPaths() []localSearchPath { |
| 31 | + var searchPaths []localSearchPath |
| 32 | + |
| 33 | + if home := paths.GetHomeDir(); home != "" { |
| 34 | + searchPaths = append(searchPaths, |
| 35 | + localSearchPath{filepath.Join(home, ".codex", "skills"), true}, |
| 36 | + localSearchPath{filepath.Join(home, ".claude", "skills"), false}, |
| 37 | + localSearchPath{filepath.Join(home, ".agents", "skills"), true}, |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + cwd, err := os.Getwd() |
| 42 | + if err != nil { |
| 43 | + return searchPaths |
| 44 | + } |
| 45 | + |
| 46 | + searchPaths = append(searchPaths, localSearchPath{filepath.Join(cwd, ".claude", "skills"), false}) |
| 47 | + for _, dir := range projectSearchDirs(cwd) { |
| 48 | + searchPaths = append(searchPaths, localSearchPath{filepath.Join(dir, ".agents", "skills"), false}) |
| 49 | + } |
| 50 | + return searchPaths |
| 51 | +} |
| 52 | + |
| 53 | +// loadLocalSkillsInto populates skillMap with every local skill, with later |
| 54 | +// search paths overriding earlier ones. |
| 55 | +func loadLocalSkillsInto(skillMap map[string]Skill) { |
| 56 | + for _, p := range localSearchPaths() { |
| 57 | + for _, skill := range loadSkillsFromDir(p.dir, p.recursive) { |
| 58 | + skillMap[skill.Name] = skill |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// projectSearchDirs returns directories from the enclosing git root down to |
| 64 | +// cwd (inclusive), ordered from root to cwd. If cwd is not inside a git |
| 65 | +// repository, it returns just cwd. |
| 66 | +// |
| 67 | +// The ordering matters: later (deeper) entries override skills from parents, |
| 68 | +// so a project subdirectory can shadow a skill defined at the repo root. |
| 69 | +func projectSearchDirs(cwd string) []string { |
| 70 | + abs, err := filepath.Abs(cwd) |
| 71 | + if err != nil { |
| 72 | + return []string{cwd} |
| 73 | + } |
| 74 | + |
| 75 | + // Walk up from cwd, recording each dir, until we either hit a git |
| 76 | + // marker or run out of parents. This collects findGitRoot's result |
| 77 | + // and the dir list in a single traversal. |
| 78 | + var dirs []string |
| 79 | + var gitRoot string |
| 80 | + for current := abs; ; { |
| 81 | + dirs = append(dirs, current) |
| 82 | + if hasGitMarker(current) { |
| 83 | + gitRoot = current |
| 84 | + break |
| 85 | + } |
| 86 | + parent := filepath.Dir(current) |
| 87 | + if parent == current { |
| 88 | + break |
| 89 | + } |
| 90 | + current = parent |
| 91 | + } |
| 92 | + |
| 93 | + if gitRoot == "" { |
| 94 | + return []string{abs} |
| 95 | + } |
| 96 | + |
| 97 | + slices.Reverse(dirs) |
| 98 | + return dirs |
| 99 | +} |
| 100 | + |
| 101 | +// hasGitMarker reports whether dir contains a .git directory or .git file |
| 102 | +// (the latter is used by worktrees and submodules). |
| 103 | +func hasGitMarker(dir string) bool { |
| 104 | + info, err := os.Stat(filepath.Join(dir, ".git")) |
| 105 | + if err != nil { |
| 106 | + return false |
| 107 | + } |
| 108 | + return info.IsDir() || info.Mode().IsRegular() |
| 109 | +} |
| 110 | + |
| 111 | +// findGitRoot finds the enclosing git repository root, or returns "" if dir |
| 112 | +// is not inside a git repository. Kept as a thin wrapper for test coverage |
| 113 | +// and callers that don't need the full directory list. |
| 114 | +func findGitRoot(dir string) string { |
| 115 | + for current := dir; ; { |
| 116 | + if hasGitMarker(current) { |
| 117 | + return current |
| 118 | + } |
| 119 | + parent := filepath.Dir(current) |
| 120 | + if parent == current { |
| 121 | + return "" |
| 122 | + } |
| 123 | + current = parent |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// loadSkillsFromDir loads skills from a directory. |
| 128 | +// If recursive is true, it walks all subdirectories looking for SKILL.md. |
| 129 | +// If recursive is false, it only looks at immediate subdirectories. |
| 130 | +func loadSkillsFromDir(dir string, recursive bool) []Skill { |
| 131 | + if recursive { |
| 132 | + return loadSkillsRecursive(dir) |
| 133 | + } |
| 134 | + return loadSkillsFlat(dir) |
| 135 | +} |
| 136 | + |
| 137 | +// loadSkillsFlat loads skills from immediate subdirectories only (Claude format). |
| 138 | +func loadSkillsFlat(dir string) []Skill { |
| 139 | + entries, err := os.ReadDir(dir) |
| 140 | + if err != nil { |
| 141 | + return nil |
| 142 | + } |
| 143 | + |
| 144 | + var skills []Skill |
| 145 | + for _, entry := range entries { |
| 146 | + if !entry.IsDir() || isHidden(entry) || isSymlink(entry) { |
| 147 | + continue |
| 148 | + } |
| 149 | + path := filepath.Join(dir, entry.Name(), skillFile) |
| 150 | + if skill, ok := loadSkillFile(path, entry.Name()); ok { |
| 151 | + skills = append(skills, skill) |
| 152 | + } |
| 153 | + } |
| 154 | + return skills |
| 155 | +} |
| 156 | + |
| 157 | +// loadSkillsRecursive walks dir for SKILL.md files (Codex format), tracking |
| 158 | +// real directory paths so symlink cycles can't loop forever. |
| 159 | +func loadSkillsRecursive(dir string) []Skill { |
| 160 | + visited := make(map[string]bool) |
| 161 | + if realDir, err := filepath.EvalSymlinks(dir); err == nil { |
| 162 | + visited[realDir] = true |
| 163 | + } |
| 164 | + |
| 165 | + var skills []Skill |
| 166 | + _ = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 167 | + if err != nil { |
| 168 | + return nil |
| 169 | + } |
| 170 | + |
| 171 | + if d.IsDir() { |
| 172 | + if path == dir { |
| 173 | + return nil |
| 174 | + } |
| 175 | + if isHidden(d) { |
| 176 | + return fs.SkipDir |
| 177 | + } |
| 178 | + // De-duplicate by real path so symlink cycles are skipped. |
| 179 | + realPath, err := filepath.EvalSymlinks(path) |
| 180 | + if err == nil { |
| 181 | + if visited[realPath] { |
| 182 | + return fs.SkipDir |
| 183 | + } |
| 184 | + visited[realPath] = true |
| 185 | + } |
| 186 | + return nil |
| 187 | + } |
| 188 | + |
| 189 | + if d.Name() != skillFile { |
| 190 | + return nil |
| 191 | + } |
| 192 | + dirName := filepath.Base(filepath.Dir(path)) |
| 193 | + if skill, ok := loadSkillFile(path, dirName); ok { |
| 194 | + skills = append(skills, skill) |
| 195 | + } |
| 196 | + return nil |
| 197 | + }) |
| 198 | + return skills |
| 199 | +} |
| 200 | + |
| 201 | +// loadSkillFile reads and parses a SKILL.md file. dirName is used as the |
| 202 | +// skill name when the frontmatter does not declare one. |
| 203 | +func loadSkillFile(path, dirName string) (Skill, bool) { |
| 204 | + content, err := os.ReadFile(path) |
| 205 | + if err != nil { |
| 206 | + return Skill{}, false |
| 207 | + } |
| 208 | + |
| 209 | + skill, ok := parseFrontmatter(string(content)) |
| 210 | + if !ok { |
| 211 | + return Skill{}, false |
| 212 | + } |
| 213 | + |
| 214 | + skill.Name = cmp.Or(skill.Name, dirName) |
| 215 | + if skill.Name == "" || skill.Description == "" { |
| 216 | + return Skill{}, false |
| 217 | + } |
| 218 | + |
| 219 | + skill.FilePath = path |
| 220 | + skill.BaseDir = filepath.Dir(path) |
| 221 | + skill.Local = true |
| 222 | + return skill, true |
| 223 | +} |
| 224 | + |
| 225 | +func isHidden(entry fs.DirEntry) bool { |
| 226 | + name := entry.Name() |
| 227 | + return name != "" && name[0] == '.' |
| 228 | +} |
| 229 | + |
| 230 | +func isSymlink(entry fs.DirEntry) bool { |
| 231 | + return entry.Type()&os.ModeSymlink != 0 |
| 232 | +} |
0 commit comments