From 2acfec82eca9a3dbeba23cd97edb42fe509e4577 Mon Sep 17 00:00:00 2001 From: Willie Date: Wed, 8 Apr 2026 15:00:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20simplify=20metadata=20=E2=80=94=20e?= =?UTF-8?q?xtract=20helpers,=20remove=20dual-write,=20add=20kind=20constan?= =?UTF-8?q?ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code quality fixes from review: - Extract RemoveByNames() to eliminate copy-paste in uninstall Phase 7 - Add MetadataKindSkill/MetadataKindAgent constants - Remove dual-write shim (findSkillsRoot + writeMetaToCentralized) - install_apply.go uses WriteMetaToStore with SourceDir (no sidecar) - Move LoadMetadata out of walk loops (P0 efficiency fix) - isGroupDir accepts store parameter to avoid redundant loads - Add SourceDir to InstallOptions for --into group support --- cmd/skillshare/check.go | 3 +- cmd/skillshare/install.go | 1 + cmd/skillshare/uninstall.go | 22 +----- cmd/skillshare/uninstall_project.go | 22 +----- cmd/skillshare/update.go | 4 +- cmd/skillshare/update_project.go | 2 +- cmd/skillshare/update_resolve.go | 7 +- cmd/skillshare/update_test.go | 4 +- internal/install/install.go | 1 + internal/install/install_apply.go | 23 ++++-- internal/install/install_local_test.go | 19 +++-- internal/install/meta.go | 66 ----------------- internal/install/metadata.go | 72 +++++++++++++++++++ internal/server/handler_install.go | 2 + .../search_batch_meta_roundtrip_test.go | 19 ++--- 15 files changed, 129 insertions(+), 138 deletions(-) diff --git a/cmd/skillshare/check.go b/cmd/skillshare/check.go index 0e28437b5..70b210dde 100644 --- a/cmd/skillshare/check.go +++ b/cmd/skillshare/check.go @@ -654,13 +654,14 @@ func runCheckFiltered(sourceDir string, opts *checkOptions) error { resolveSpinner = ui.StartSpinner("Resolving skills...") } + checkStore, _ := install.LoadMetadata(sourceDir) var targets []updateTarget seen := map[string]bool{} var resolveWarnings []string for _, name := range opts.names { // Check group directory first (same logic as update) - if isGroupDir(name, sourceDir) { + if isGroupDir(name, sourceDir, checkStore) { groupMatches, groupErr := resolveGroupUpdatable(name, sourceDir) if groupErr != nil { resolveWarnings = append(resolveWarnings, fmt.Sprintf("%s: %v", name, groupErr)) diff --git a/cmd/skillshare/install.go b/cmd/skillshare/install.go index 279091852..d194b64b7 100644 --- a/cmd/skillshare/install.go +++ b/cmd/skillshare/install.go @@ -402,6 +402,7 @@ func cmdInstall(args []string) error { return err } + parsed.opts.SourceDir = cfg.Source source, resolvedFromMeta, err := resolveInstallSource(parsed.sourceArg, parsed.opts, cfg) if err == nil && parsed.opts.Branch != "" { source.Branch = parsed.opts.Branch diff --git a/cmd/skillshare/uninstall.go b/cmd/skillshare/uninstall.go index 11ee4c917..7bce6614d 100644 --- a/cmd/skillshare/uninstall.go +++ b/cmd/skillshare/uninstall.go @@ -1074,27 +1074,7 @@ func cmdUninstall(args []string) error { for _, t := range succeeded { removedNames[t.name] = true } - for _, name := range skillsStore.List() { - // Direct match by bare name or full path (group/name) - entry := skillsStore.Get(name) - fullName := name - if entry != nil && entry.Group != "" { - fullName = entry.Group + "/" + name - } - if removedNames[name] || removedNames[fullName] { - skillsStore.Remove(name) - continue - } - // When a group directory is uninstalled, also remove its member skills by group field - if entry != nil && entry.Group != "" { - for rn := range removedNames { - if entry.Group == rn || strings.HasPrefix(entry.Group, rn+"/") { - skillsStore.Remove(name) - break - } - } - } - } + skillsStore.RemoveByNames(removedNames) if saveErr := skillsStore.Save(cfg.Source); saveErr != nil { ui.Warning("Failed to update metadata after uninstall: %v", saveErr) } diff --git a/cmd/skillshare/uninstall_project.go b/cmd/skillshare/uninstall_project.go index 72c98be7e..819e2da54 100644 --- a/cmd/skillshare/uninstall_project.go +++ b/cmd/skillshare/uninstall_project.go @@ -420,27 +420,7 @@ func cmdUninstallProject(args []string, root string) error { for _, t := range succeeded { removedNames[t.name] = true } - for _, name := range skillsStore.List() { - // Direct match by bare name or full path (group/name) - entry := skillsStore.Get(name) - fullName := name - if entry != nil && entry.Group != "" { - fullName = entry.Group + "/" + name - } - if removedNames[name] || removedNames[fullName] { - skillsStore.Remove(name) - continue - } - // When a group directory is uninstalled, also remove its member skills by group field - if entry != nil && entry.Group != "" { - for rn := range removedNames { - if entry.Group == rn || strings.HasPrefix(entry.Group, rn+"/") { - skillsStore.Remove(name) - break - } - } - } - } + skillsStore.RemoveByNames(removedNames) if saveErr := skillsStore.Save(sourceDir); saveErr != nil { ui.Warning("Failed to update metadata after uninstall: %v", saveErr) } diff --git a/cmd/skillshare/update.go b/cmd/skillshare/update.go index 6f8ffcce0..d8f910189 100644 --- a/cmd/skillshare/update.go +++ b/cmd/skillshare/update.go @@ -260,6 +260,8 @@ func cmdUpdate(args []string) error { return fmt.Errorf("failed to scan skills: %w", err) } } else { + // Load store once for name resolution + nameStore, _ := install.LoadMetadata(cfg.Source) // Resolve by specific names/groups for _, name := range opts.names { // Glob pattern matching (e.g. "core-*", "_team-?") @@ -283,7 +285,7 @@ func cmdUpdate(args []string) error { continue } - if isGroupDir(name, cfg.Source) { + if isGroupDir(name, cfg.Source, nameStore) { groupMatches, groupErr := resolveGroupUpdatable(name, cfg.Source) if groupErr != nil { resolveWarnings = append(resolveWarnings, fmt.Sprintf("%s: %v", name, groupErr)) diff --git a/cmd/skillshare/update_project.go b/cmd/skillshare/update_project.go index 2a47c9d38..6def116ec 100644 --- a/cmd/skillshare/update_project.go +++ b/cmd/skillshare/update_project.go @@ -65,7 +65,7 @@ func cmdUpdateProjectBatch(sourcePath string, opts *updateOptions, projectRoot s // Check group directory first (before repo/skill lookup, // so "feature-radar" expands to all skills rather than // matching a single nested "feature-radar/feature-radar"). - if isGroupDir(name, sourcePath) { + if isGroupDir(name, sourcePath, metaStore) { groupMatches, groupErr := resolveGroupUpdatable(name, sourcePath) if groupErr != nil { resolveWarnings = append(resolveWarnings, fmt.Sprintf("%s: %v", name, groupErr)) diff --git a/cmd/skillshare/update_resolve.go b/cmd/skillshare/update_resolve.go index 3616b9a62..b0d7898a6 100644 --- a/cmd/skillshare/update_resolve.go +++ b/cmd/skillshare/update_resolve.go @@ -99,6 +99,9 @@ func resolveGroupUpdatable(group, sourceDir string) ([]updateTarget, error) { return nil, fmt.Errorf("group '%s' resolves outside source directory", group) } + // Load store once before walk (not per iteration) + store, _ := install.LoadMetadata(resolvedSourceDir) + var matches []updateTarget if walkErr := filepath.Walk(walkRoot, func(path string, fi os.FileInfo, err error) error { if err != nil { @@ -123,7 +126,6 @@ func resolveGroupUpdatable(group, sourceDir string) ([]updateTarget, error) { } // Skill with metadata (centralized store) - store, _ := install.LoadMetadata(resolvedSourceDir) if entry := store.GetByPath(rel); entry != nil && entry.Source != "" { matches = append(matches, updateTarget{name: rel, path: path, isRepo: false, meta: entry}) return filepath.SkipDir @@ -140,7 +142,7 @@ func resolveGroupUpdatable(group, sourceDir string) ([]updateTarget, error) { // isGroupDir checks if a name corresponds to a group directory (a container // for other skills). Returns false for tracked repos, skills with metadata, // and directories that are themselves a skill (have SKILL.md). -func isGroupDir(name, sourceDir string) bool { +func isGroupDir(name, sourceDir string, store *install.MetadataStore) bool { path := filepath.Join(sourceDir, name) info, err := os.Stat(path) if err != nil || !info.IsDir() { @@ -151,7 +153,6 @@ func isGroupDir(name, sourceDir string) bool { return false } // Not a skill with metadata - store, _ := install.LoadMetadata(sourceDir) if entry := store.Get(name); entry != nil && entry.Source != "" { return false } diff --git a/cmd/skillshare/update_test.go b/cmd/skillshare/update_test.go index 9e0c518ce..e62d0bf42 100644 --- a/cmd/skillshare/update_test.go +++ b/cmd/skillshare/update_test.go @@ -90,7 +90,9 @@ func setupUpdatableSkill(t *testing.T, sourceDir, name string) { dir := filepath.Join(sourceDir, name) os.MkdirAll(dir, 0755) os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("# "+name), 0644) - install.WriteMeta(dir, &install.SkillMeta{Source: "github.com/test/" + name, Type: "github"}) + store, _ := install.LoadMetadata(sourceDir) + store.Set(name, &install.MetadataEntry{Source: "github.com/test/" + name, Type: "github"}) + store.Save(sourceDir) } func TestResolveByGlob_MatchesTrackedRepos(t *testing.T) { diff --git a/internal/install/install.go b/internal/install/install.go index 098f2a198..72efbcbdf 100644 --- a/internal/install/install.go +++ b/internal/install/install.go @@ -28,6 +28,7 @@ type InstallOptions struct { AuditProjectRoot string // Project root for project-mode audit rule resolution Quiet bool // Suppress per-skill output in InstallFromConfig Branch string // Git branch to clone from (empty = remote default) + SourceDir string // Skills root dir for centralized metadata (set by caller) } // IsAgentMode returns true if explicitly installing agents. diff --git a/internal/install/install_apply.go b/internal/install/install_apply.go index 805b7e809..5f14031ae 100644 --- a/internal/install/install_apply.go +++ b/internal/install/install_apply.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "skillshare/internal/utils" ) @@ -26,6 +27,20 @@ func buildDiscoverySkillSource(source *Source, skillPath string) string { } func installImpl(source *Source, destPath string, opts InstallOptions) (*InstallResult, error) { + // Derive SourceDir from destPath if not set by caller. + // destPath = sourceDir[/into]/skillName, so strip Into + skillName. + if opts.SourceDir == "" { + dir := filepath.Dir(destPath) + if opts.Into != "" { + // Strip the --into prefix from the parent + dir = filepath.Dir(dir) + for i := strings.Count(opts.Into, "/"); i > 0; i-- { + dir = filepath.Dir(dir) + } + } + opts.SourceDir = dir + } + result := &InstallResult{ SkillName: source.Name, Source: source.Raw, @@ -102,7 +117,7 @@ func installFromLocal(source *Source, destPath string, result *InstallResult, op if hashes, hashErr := ComputeFileHashes(destPath); hashErr == nil { meta.FileHashes = hashes } - if err := WriteMeta(destPath, meta); err != nil { + if err := WriteMetaToStore(opts.SourceDir, destPath, meta); err != nil { result.Warnings = append(result.Warnings, fmt.Sprintf("failed to write metadata: %v", err)) } @@ -144,7 +159,7 @@ func installFromGit(source *Source, destPath string, result *InstallResult, opts if hashes, hashErr := ComputeFileHashes(destPath); hashErr == nil { meta.FileHashes = hashes } - if err := WriteMeta(destPath, meta); err != nil { + if err := WriteMetaToStore(opts.SourceDir, destPath, meta); err != nil { result.Warnings = append(result.Warnings, fmt.Sprintf("failed to write metadata: %v", err)) } @@ -257,7 +272,7 @@ func installFromDiscoveryImpl(discovery *DiscoveryResult, skill SkillInfo, destP if hashes, hashErr := ComputeFileHashes(destPath); hashErr == nil { meta.FileHashes = hashes } - if err := WriteMeta(destPath, meta); err != nil { + if err := WriteMetaToStore(opts.SourceDir, destPath, meta); err != nil { result.Warnings = append(result.Warnings, fmt.Sprintf("failed to write metadata: %v", err)) } @@ -366,7 +381,7 @@ func installFromGitSubdir(source *Source, destPath string, result *InstallResult if hashes, hashErr := ComputeFileHashes(destPath); hashErr == nil { meta.FileHashes = hashes } - if err := WriteMeta(destPath, meta); err != nil { + if err := WriteMetaToStore(opts.SourceDir, destPath, meta); err != nil { result.Warnings = append(result.Warnings, fmt.Sprintf("failed to write metadata: %v", err)) } diff --git a/internal/install/install_local_test.go b/internal/install/install_local_test.go index fb9594b16..f46dc58d7 100644 --- a/internal/install/install_local_test.go +++ b/internal/install/install_local_test.go @@ -46,8 +46,9 @@ func TestInstall_LocalPath_Basic(t *testing.T) { t.Error("expected SKILL.md to exist in destination") } - // Verify metadata was written - if !HasMeta(destDir) { + // Verify metadata was written to centralized store + store, _ := LoadMetadata(filepath.Dir(destDir)) + if !store.Has(filepath.Base(destDir)) { t.Error("expected metadata to be written") } } @@ -162,17 +163,15 @@ func TestInstall_LocalPath_WritesFileHashes(t *testing.T) { t.Fatal(err) } - meta, err := ReadMeta(destDir) - if err != nil { - t.Fatal(err) - } - if meta == nil { + store, _ := LoadMetadata(filepath.Dir(destDir)) + entry := store.Get(filepath.Base(destDir)) + if entry == nil { t.Fatal("expected meta to exist") } - if len(meta.FileHashes) < 2 { - t.Errorf("expected at least 2 file hashes (SKILL.md + helpers.sh), got %d", len(meta.FileHashes)) + if len(entry.FileHashes) < 2 { + t.Errorf("expected at least 2 file hashes (SKILL.md + helpers.sh), got %d", len(entry.FileHashes)) } - for _, hash := range meta.FileHashes { + for _, hash := range entry.FileHashes { if len(hash) < 7 || hash[:7] != "sha256:" { t.Errorf("expected sha256: prefixed hash, got %q", hash) } diff --git a/internal/install/meta.go b/internal/install/meta.go index 3fdc37bcf..1cc3bf8c1 100644 --- a/internal/install/meta.go +++ b/internal/install/meta.go @@ -38,9 +38,6 @@ func (m *SkillMeta) EffectiveKind() string { // Deprecated: WriteMeta writes per-skill sidecar files. // New code should use MetadataStore.Set() + MetadataStore.Save() instead. -// -// For backward compatibility during migration, WriteMeta also writes to the -// centralized .metadata.json in the source root directory. func WriteMeta(skillPath string, meta *SkillMeta) error { metaPath := filepath.Join(skillPath, MetaFileName) @@ -53,72 +50,9 @@ func WriteMeta(skillPath string, meta *SkillMeta) error { return fmt.Errorf("failed to write metadata: %w", err) } - // Dual-write: also update centralized .metadata.json - writeMetaToCentralized(skillPath, meta) - return nil } -// writeMetaToCentralized writes an entry to the centralized .metadata.json store. -// Best-effort: errors are silently ignored since the sidecar is the primary write. -func writeMetaToCentralized(skillPath string, meta *SkillMeta) { - sourceDir := findSkillsRoot(skillPath) - if sourceDir == "" { - return - } - rel, err := filepath.Rel(sourceDir, skillPath) - if err != nil || rel == "." || strings.HasPrefix(rel, "..") { - return - } - - store, loadErr := LoadMetadata(sourceDir) - if loadErr != nil { - return - } - - // Split rel path into group + name (e.g., "frontend/pdf-skill" → group="frontend", name="pdf-skill") - rel = filepath.ToSlash(rel) - name := rel - group := "" - if idx := strings.LastIndex(rel, "/"); idx >= 0 { - group = rel[:idx] - name = rel[idx+1:] - } - - entry := &MetadataEntry{ - Source: meta.Source, - Kind: meta.Kind, - Type: meta.Type, - Group: group, - InstalledAt: meta.InstalledAt, - RepoURL: meta.RepoURL, - Subdir: meta.Subdir, - Version: meta.Version, - TreeHash: meta.TreeHash, - FileHashes: meta.FileHashes, - Branch: meta.Branch, - } - store.Set(name, entry) - _ = store.Save(sourceDir) -} - -// findSkillsRoot walks up from skillPath to find the ancestor directory named "skills". -// Returns "" if not found. -func findSkillsRoot(skillPath string) string { - dir := filepath.Dir(skillPath) // start from parent - for { - base := filepath.Base(dir) - if base == "skills" { - return dir - } - parent := filepath.Dir(dir) - if parent == dir { - return "" - } - dir = parent - } -} - // Deprecated: ReadMeta reads per-skill sidecar files. // New code should use LoadMetadata() + MetadataStore.Get() instead. func ReadMeta(skillPath string) (*SkillMeta, error) { diff --git a/internal/install/metadata.go b/internal/install/metadata.go index 0e94f8c0f..7a586a8f6 100644 --- a/internal/install/metadata.go +++ b/internal/install/metadata.go @@ -13,6 +13,12 @@ import ( // MetadataFileName is the centralized metadata file stored in each directory. const MetadataFileName = ".metadata.json" +// Metadata kind constants for LoadMetadataWithMigration. +const ( + MetadataKindSkill = "" // default kind for skills directories + MetadataKindAgent = "agent" // kind for agents directories +) + // MetadataStore holds all entries for a single directory (skills/ or agents/). type MetadataStore struct { Version int `json:"version"` @@ -115,6 +121,72 @@ func (e *MetadataEntry) FullName() string { return e.Name } +// RemoveByNames removes entries matching the given names, including group members. +// Handles direct key matches, full-path matches (group/name), and group membership. +func (s *MetadataStore) RemoveByNames(names map[string]bool) { + for _, name := range s.List() { + entry := s.Get(name) + fullName := name + if entry != nil && entry.Group != "" { + fullName = entry.Group + "/" + name + } + if names[name] || names[fullName] { + s.Remove(name) + continue + } + // Group directory uninstall: remove member skills + if entry != nil && entry.Group != "" { + for rn := range names { + if entry.Group == rn || strings.HasPrefix(entry.Group, rn+"/") { + s.Remove(name) + break + } + } + } + } +} + +// WriteMetaToStore writes a SkillMeta to the centralized .metadata.json store. +// sourceDir is the skills root (if empty, defaults to parent of destPath). +// destPath is the installed skill path. +func WriteMetaToStore(sourceDir, destPath string, meta *SkillMeta) error { + if sourceDir == "" { + sourceDir = filepath.Dir(destPath) + } + rel, err := filepath.Rel(sourceDir, destPath) + if err != nil { + return fmt.Errorf("relative path: %w", err) + } + rel = filepath.ToSlash(rel) + + name := rel + group := "" + if idx := strings.LastIndex(rel, "/"); idx >= 0 { + group = rel[:idx] + name = rel[idx+1:] + } + + store, loadErr := LoadMetadata(sourceDir) + if loadErr != nil { + store = NewMetadataStore() + } + + store.Set(name, &MetadataEntry{ + Source: meta.Source, + Kind: meta.Kind, + Type: meta.Type, + Group: group, + InstalledAt: meta.InstalledAt, + RepoURL: meta.RepoURL, + Subdir: meta.Subdir, + Version: meta.Version, + TreeHash: meta.TreeHash, + FileHashes: meta.FileHashes, + Branch: meta.Branch, + }) + return store.Save(sourceDir) +} + // LoadMetadata reads .metadata.json from the given directory. // Returns an empty store (version 1) if the file does not exist. func LoadMetadata(dir string) (*MetadataStore, error) { diff --git a/internal/server/handler_install.go b/internal/server/handler_install.go index a23e0483d..0c247c9a2 100644 --- a/internal/server/handler_install.go +++ b/internal/server/handler_install.go @@ -151,6 +151,7 @@ func (s *Server) handleInstallBatch(w http.ResponseWriter, r *http.Request) { SkipAudit: body.SkipAudit, AuditThreshold: s.auditThreshold(), Branch: body.Branch, + SourceDir: s.cfg.Source, } if s.IsProjectMode() { installOpts.AuditProjectRoot = s.projectRoot @@ -327,6 +328,7 @@ func (s *Server) handleInstall(w http.ResponseWriter, r *http.Request) { Into: body.Into, Branch: body.Branch, AuditThreshold: s.auditThreshold(), + SourceDir: s.cfg.Source, } if s.IsProjectMode() { installOpts.AuditProjectRoot = s.projectRoot diff --git a/tests/integration/search_batch_meta_roundtrip_test.go b/tests/integration/search_batch_meta_roundtrip_test.go index 5ea8419a7..220843218 100644 --- a/tests/integration/search_batch_meta_roundtrip_test.go +++ b/tests/integration/search_batch_meta_roundtrip_test.go @@ -79,19 +79,20 @@ func TestSearchBatchGroupedInstall_MetadataSourceParseRoundTrip(t *testing.T) { } } + store, storeErr := install.LoadMetadata(sb.SourcePath) + if storeErr != nil { + t.Fatalf("load metadata: %v", storeErr) + } + for _, name := range []string{"alpha-skill", "beta-skill"} { - skillPath := filepath.Join(sb.SourcePath, name) - meta, err := install.ReadMeta(skillPath) - if err != nil { - t.Fatalf("read meta for %s: %v", name, err) - } - if meta == nil { + entry := store.Get(name) + if entry == nil { t.Fatalf("meta missing for %s", name) } - parsed, err := install.ParseSource(meta.Source) + parsed, err := install.ParseSource(entry.Source) if err != nil { - t.Fatalf("meta source for %s is not parseable: %q (%v)", name, meta.Source, err) + t.Fatalf("meta source for %s is not parseable: %q (%v)", name, entry.Source, err) } if parsed.CloneURL != "https://gitlab.com/team/monorepo.git" { t.Fatalf("unexpected clone URL for %s: got %q", name, parsed.CloneURL) @@ -99,7 +100,7 @@ func TestSearchBatchGroupedInstall_MetadataSourceParseRoundTrip(t *testing.T) { wantSubdir := "skills/" + name if parsed.Subdir != wantSubdir { - t.Fatalf("unexpected subdir for %s: got %q, want %q (source=%q)", name, parsed.Subdir, wantSubdir, meta.Source) + t.Fatalf("unexpected subdir for %s: got %q, want %q (source=%q)", name, parsed.Subdir, wantSubdir, entry.Source) } } }