refactor: simplify metadata — extract helpers, remove dual-write, add kind constants

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
This commit is contained in:
Willie
2026-04-08 15:00:22 +08:00
parent 34b6c2a9e0
commit 2acfec82ec
15 changed files with 129 additions and 138 deletions
+2 -1
View File
@@ -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))
+1
View File
@@ -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
+1 -21
View File
@@ -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)
}
+1 -21
View File
@@ -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)
}
+3 -1
View File
@@ -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))
+1 -1
View File
@@ -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))
+4 -3
View File
@@ -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
}
+3 -1
View File
@@ -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) {
+1
View File
@@ -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.
+19 -4
View File
@@ -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))
}
+9 -10
View File
@@ -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)
}
-66
View File
@@ -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) {
+72
View File
@@ -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) {
+2
View File
@@ -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
@@ -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)
}
}
}