mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
A repository-root .agents/skills now counts as a skill anchor only when the repository has no other SKILL.md outside .git and target directories, so tracked checkouts that author skills there are discovered, while repos that commit .agents/skills copies keep discovering only their authored skills. Fixes #800
418 lines
13 KiB
Go
418 lines
13 KiB
Go
package sync
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
|
|
"skillshare/internal/config"
|
|
"skillshare/internal/install"
|
|
"skillshare/internal/skillignore"
|
|
"skillshare/internal/sourcewalk"
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// discoverOptions controls the behavior of discoverSourceSkillsInternal.
|
|
type discoverOptions struct {
|
|
parseFrontmatter bool // parse SKILL.md frontmatter for targets
|
|
collectIgnored bool // collect ignored skill paths into IgnoreStats
|
|
collectTracked bool // collect tracked repo paths (for Lite mode)
|
|
collectContext bool // compute DescChars/BodyChars during walk (for analyze)
|
|
includeIgnored bool // include ignored skills in results with Disabled=true
|
|
walk sourcewalk.Options
|
|
}
|
|
|
|
var (
|
|
knownTargetNameSet map[string]bool
|
|
knownTargetNameSetOnce sync.Once
|
|
projectTargetPaths []projectTargetPath
|
|
projectTargetPathsOnce sync.Once
|
|
)
|
|
|
|
type projectTargetPath struct {
|
|
name string
|
|
path string
|
|
}
|
|
|
|
func getKnownTargetNameSet() map[string]bool {
|
|
knownTargetNameSetOnce.Do(func() {
|
|
targetNames := config.KnownTargetNames()
|
|
knownTargetNameSet = make(map[string]bool, len(targetNames))
|
|
for _, name := range targetNames {
|
|
knownTargetNameSet[name] = true
|
|
}
|
|
})
|
|
return knownTargetNameSet
|
|
}
|
|
|
|
func getProjectTargetPaths() []projectTargetPath {
|
|
projectTargetPathsOnce.Do(func() {
|
|
groupedTargets := config.GroupedProjectTargets()
|
|
projectTargetPaths = make([]projectTargetPath, 0, len(groupedTargets)*2)
|
|
for _, target := range groupedTargets {
|
|
if target.Name == "" || target.Path == "" {
|
|
continue
|
|
}
|
|
p := filepath.ToSlash(target.Path)
|
|
projectTargetPaths = append(projectTargetPaths, projectTargetPath{
|
|
name: target.Name,
|
|
path: p,
|
|
})
|
|
for _, member := range target.Members {
|
|
projectTargetPaths = append(projectTargetPaths, projectTargetPath{
|
|
name: member,
|
|
path: p,
|
|
})
|
|
}
|
|
}
|
|
sort.Slice(projectTargetPaths, func(i, j int) bool {
|
|
if projectTargetPaths[i].path == projectTargetPaths[j].path {
|
|
return projectTargetPaths[i].name < projectTargetPaths[j].name
|
|
}
|
|
return projectTargetPaths[i].path < projectTargetPaths[j].path
|
|
})
|
|
})
|
|
return projectTargetPaths
|
|
}
|
|
|
|
func inferSkillTargetsFromRelPath(relPath string) []string {
|
|
// Prefer explicit project paths from known targets (e.g. ".factory/skills").
|
|
targets := inferTargetsFromProjectPaths(relPath)
|
|
if len(targets) > 0 {
|
|
return targets
|
|
}
|
|
|
|
parts := strings.Split(relPath, "/")
|
|
if len(parts) < 3 || parts[1] != "skills" {
|
|
return nil
|
|
}
|
|
|
|
// Fallback to top-level host-style paths that match known target names
|
|
// (e.g. "openclaw/skills/*" for openclaw project path "skills").
|
|
host := strings.TrimPrefix(parts[0], ".")
|
|
if host == "" {
|
|
return nil
|
|
}
|
|
if getKnownTargetNameSet()[host] {
|
|
return []string{host}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func inferTargetsFromProjectPaths(relPath string) []string {
|
|
targets := make([]string, 0, 2)
|
|
|
|
for _, target := range getProjectTargetPaths() {
|
|
if matchesHostSkillPath(relPath, target.name, target.path) {
|
|
targets = append(targets, target.name)
|
|
}
|
|
}
|
|
|
|
return targets
|
|
}
|
|
|
|
func matchesHostSkillPath(relPath, targetName, targetPath string) bool {
|
|
switch {
|
|
case targetPath == "skills":
|
|
parts := strings.Split(relPath, "/")
|
|
return len(parts) >= 2 && parts[1] == "skills" && strings.TrimPrefix(parts[0], ".") == targetName
|
|
case relPath == targetPath:
|
|
return false
|
|
case strings.HasPrefix(relPath, targetPath+"/"):
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// discoverSourceSkillsInternal is the shared walk implementation used by all
|
|
// public discovery functions. It returns:
|
|
// - discovered skills
|
|
// - tracked repo paths (only when opts.collectTracked is true; nil otherwise)
|
|
// - ignore stats (only when opts.collectIgnored is true; nil otherwise)
|
|
// - error
|
|
func discoverSourceSkillsInternal(sourcePath string, opts discoverOptions) ([]DiscoveredSkill, []string, *skillignore.IgnoreStats, error) {
|
|
var skills []DiscoveredSkill
|
|
var trackedRepos []string
|
|
ignoreMatchers := make(map[string]*skillignore.Matcher) // tracked repo slash rel path → .skillignore matcher
|
|
repoRoots := make(map[string]bool) // slash rel paths of _-prefixed git repos seen so far, e.g. org/_team from --into
|
|
|
|
// repoOf returns the tracked repo root containing slash relPath, or "". A
|
|
// _-prefixed first segment always counts; a deeper one only when the walk
|
|
// saw it as a git repo, so a plain org/_drafts folder is not mistaken for a repo.
|
|
repoOf := func(relPath string) string {
|
|
first, _, _ := strings.Cut(relPath, "/")
|
|
if utils.IsTrackedRepoDir(first) {
|
|
return first
|
|
}
|
|
for i := len(first) + 1; i <= len(relPath); i++ {
|
|
if (i == len(relPath) || relPath[i] == '/') && repoRoots[relPath[:i]] {
|
|
return relPath[:i]
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
walkRoot := utils.ResolveSymlink(sourcePath)
|
|
rootMatcher := skillignore.ReadMatcher(walkRoot)
|
|
|
|
// Stats collection (only allocated when needed)
|
|
var stats *skillignore.IgnoreStats
|
|
if opts.collectIgnored {
|
|
stats = &skillignore.IgnoreStats{}
|
|
// Record root .skillignore if it exists
|
|
rootIgnorePath := filepath.Join(walkRoot, ".skillignore")
|
|
if _, err := os.Stat(rootIgnorePath); err == nil {
|
|
stats.RootFile = rootIgnorePath
|
|
}
|
|
// Record root .skillignore.local if it exists
|
|
if rootMatcher.HasLocal {
|
|
stats.RootLocalFile = filepath.Join(walkRoot, ".skillignore.local")
|
|
}
|
|
if pats := rootMatcher.Patterns(); len(pats) > 0 {
|
|
stats.Patterns = append(stats.Patterns, pats...)
|
|
}
|
|
}
|
|
|
|
visit := func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return nil // Skip inaccessible paths
|
|
}
|
|
|
|
// Skip .git and nested target dotdirs (.claude, .cursor, ...) so a skill
|
|
// that ships its own target copies is not re-discovered as nested skills.
|
|
if (info.IsDir() && info.Name() == ".git") || sourcewalk.SkipTargetDotDir(walkRoot, path, info) {
|
|
return filepath.SkipDir
|
|
}
|
|
|
|
// Skip directories matching root-level .skillignore.
|
|
// When collectIgnored is true, disable CanSkipDir so the walk
|
|
// descends into ignored directories and the file-level Match
|
|
// check can record each ignored SKILL.md path.
|
|
if info.IsDir() && !opts.collectIgnored && !opts.includeIgnored {
|
|
relPath, relErr := filepath.Rel(walkRoot, path)
|
|
if relErr == nil && relPath != "." {
|
|
relPath = strings.ReplaceAll(relPath, "\\", "/")
|
|
if rootMatcher.CanSkipDir(relPath) {
|
|
return filepath.SkipDir
|
|
}
|
|
}
|
|
}
|
|
|
|
// Collect tracked repos: _-prefixed directories that are git repos
|
|
if info.IsDir() && info.Name() != "." && utils.IsTrackedRepoDir(info.Name()) {
|
|
if install.IsGitRepo(path) {
|
|
relPath, relErr := filepath.Rel(walkRoot, path)
|
|
repoRel := ""
|
|
if relErr == nil && relPath != "." {
|
|
repoRel = filepath.ToSlash(relPath)
|
|
repoRoots[repoRel] = true
|
|
if opts.collectTracked {
|
|
trackedRepos = append(trackedRepos, relPath)
|
|
}
|
|
}
|
|
m := skillignore.ReadMatcher(path)
|
|
if m.HasRules() {
|
|
ignoreMatchers[repoRel] = m
|
|
// Record repo-level .skillignore in stats
|
|
if opts.collectIgnored {
|
|
repoIgnorePath := filepath.Join(path, ".skillignore")
|
|
stats.RepoFiles = append(stats.RepoFiles, repoIgnorePath)
|
|
if m.HasLocal {
|
|
stats.RepoLocalFiles = append(stats.RepoLocalFiles, filepath.Join(path, ".skillignore.local"))
|
|
}
|
|
if pats := m.Patterns(); len(pats) > 0 {
|
|
stats.Patterns = append(stats.Patterns, pats...)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Skip directories matching repo-level .skillignore inside tracked repos.
|
|
// Same CanSkipDir bypass as above when collectIgnored is true.
|
|
if info.IsDir() && !opts.collectIgnored && !opts.includeIgnored {
|
|
relPath, relErr := filepath.Rel(walkRoot, path)
|
|
if relErr == nil && relPath != "." {
|
|
relPath = strings.ReplaceAll(relPath, "\\", "/")
|
|
if repo := repoOf(relPath); repo != "" && repo != relPath {
|
|
if m, ok := ignoreMatchers[repo]; ok {
|
|
if m.CanSkipDir(strings.TrimPrefix(relPath, repo+"/")) {
|
|
return filepath.SkipDir
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Look for SKILL.md files
|
|
if !info.IsDir() && info.Name() == "SKILL.md" {
|
|
skillDir := filepath.Dir(path)
|
|
relPath, err := filepath.Rel(walkRoot, skillDir)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
if relPath == "." {
|
|
return nil
|
|
}
|
|
|
|
relPath = strings.ReplaceAll(relPath, "\\", "/")
|
|
|
|
repoRelPath := repoOf(relPath)
|
|
isInRepo := repoRelPath != ""
|
|
logicalID, variantAgent := skillVariant(relPath, repoRelPath)
|
|
|
|
// Root-level .skillignore fallback (for files in non-skipped dirs),
|
|
// then the repo-level .skillignore inside tracked repos. With
|
|
// includeIgnored the skill is kept, flagged Disabled, and measured
|
|
// like any other so analyze can price it for symlink-mode targets.
|
|
disabled := rootMatcher.Match(relPath, false) || (isInRepo && isSkillIgnored(relPath, repoRelPath, ignoreMatchers))
|
|
if disabled {
|
|
if opts.collectIgnored {
|
|
stats.IgnoredSkills = append(stats.IgnoredSkills, relPath)
|
|
}
|
|
if !opts.includeIgnored {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
skillFile := filepath.Join(skillDir, "SKILL.md")
|
|
|
|
var targets []string
|
|
var ctx skillContext
|
|
var lintIssues []LintIssue
|
|
|
|
if opts.collectContext {
|
|
// Single read: parse targets + context from one os.ReadFile
|
|
content, readErr := os.ReadFile(skillFile)
|
|
if readErr == nil {
|
|
targets = utils.ParseFrontmatterListFromBytes(content, "targets")
|
|
var yamlErr error
|
|
ctx, yamlErr = calcContextFromContent(content)
|
|
if yamlErr != nil {
|
|
lintIssues = append(lintIssues, LintIssue{
|
|
Rule: "malformed-frontmatter",
|
|
Severity: LintError,
|
|
Category: "structure",
|
|
Message: fmt.Sprintf("frontmatter YAML is malformed: %v", yamlErr),
|
|
})
|
|
}
|
|
lintResults, err := LintSkill(ctx.Name, ctx.Description, ctx.BodyChars)
|
|
if err != nil {
|
|
return fmt.Errorf("lint skill %s: %w", relPath, err)
|
|
}
|
|
lintIssues = append(lintIssues, lintResults...)
|
|
}
|
|
} else if opts.parseFrontmatter {
|
|
targets = utils.ParseFrontmatterList(skillFile, "targets")
|
|
}
|
|
|
|
variantRouting := false
|
|
if len(targets) == 0 && (opts.parseFrontmatter || opts.collectContext) {
|
|
if variantAgent != "" {
|
|
targets = []string{variantAgent}
|
|
variantRouting = true
|
|
} else {
|
|
targets = inferSkillTargetsFromRelPath(relPath)
|
|
}
|
|
}
|
|
|
|
// Use original sourcePath (not walkRoot) so SourcePath preserves
|
|
// the caller's logical path, even if sourcePath is a symlink.
|
|
skills = append(skills, DiscoveredSkill{
|
|
SourcePath: filepath.Join(sourcePath, relPath),
|
|
RelPath: relPath,
|
|
FlatName: utils.PathToFlatName(relPath),
|
|
IsInRepo: isInRepo,
|
|
RepoRelPath: repoRelPath,
|
|
LogicalID: logicalID,
|
|
VariantAgent: variantAgent,
|
|
variantRouting: variantRouting,
|
|
Targets: targets,
|
|
DescChars: ctx.DescChars,
|
|
BodyChars: ctx.BodyChars,
|
|
DescTokens: ctx.DescTokens,
|
|
BodyTokens: ctx.BodyTokens,
|
|
Description: ctx.Description,
|
|
LintIssues: lintIssues,
|
|
Disabled: disabled,
|
|
})
|
|
}
|
|
|
|
return nil
|
|
}
|
|
err := sourcewalk.Walk(walkRoot, opts.walk, visit)
|
|
if err == nil {
|
|
err = walkRepoAgentsSkills(walkRoot, repoRoots, skills, visit)
|
|
}
|
|
if err != nil {
|
|
return nil, nil, nil, fmt.Errorf("failed to walk source directory: %w", err)
|
|
}
|
|
|
|
confirmSkillVariants(skills, walkRoot)
|
|
if opts.parseFrontmatter || opts.collectContext {
|
|
applyTargetOverrides(skills, walkRoot)
|
|
}
|
|
|
|
return skills, trackedRepos, stats, nil
|
|
}
|
|
|
|
// walkRepoAgentsSkills walks the .agents/skills directory of each tracked repo
|
|
// that yielded no skills, for repositories that author their skills only there.
|
|
func walkRepoAgentsSkills(walkRoot string, repoRoots map[string]bool, skills []DiscoveredSkill, visit filepath.WalkFunc) error {
|
|
found := make(map[string]bool, len(repoRoots))
|
|
for _, s := range skills {
|
|
found[s.RepoRelPath] = true
|
|
}
|
|
roots := make([]string, 0, len(repoRoots))
|
|
for root := range repoRoots {
|
|
if !found[root] {
|
|
roots = append(roots, root)
|
|
}
|
|
}
|
|
sort.Strings(roots)
|
|
for _, root := range roots {
|
|
dir, ok := sourcewalk.AgentsSkillsDir(filepath.Join(walkRoot, filepath.FromSlash(root)), sourcewalk.TargetDotDirs)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if err := filepath.Walk(dir, visit); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// applyTargetOverrides replaces the frontmatter targets of tracked-repo skills
|
|
// with the overrides stored in the source's .metadata.json, so every consumer
|
|
// of discovery sees the same effective targets. An empty override means all
|
|
// targets.
|
|
func applyTargetOverrides(skills []DiscoveredSkill, sourceDir string) {
|
|
var overrides map[string][]string
|
|
loaded := false
|
|
for i := range skills {
|
|
if !skills[i].IsInRepo {
|
|
continue
|
|
}
|
|
if !loaded {
|
|
overrides = install.LoadTargetOverrides(sourceDir)
|
|
loaded = true
|
|
}
|
|
targets, ok := overrides[skills[i].RelPath]
|
|
if !ok {
|
|
continue
|
|
}
|
|
skills[i].variantRouting = false
|
|
if len(targets) == 0 {
|
|
skills[i].Targets = nil
|
|
} else {
|
|
skills[i].Targets = append([]string(nil), targets...)
|
|
}
|
|
}
|
|
}
|