fix(install): persist metadata for tracked repos with root-level SKILL.md

When --track installed a repo whose SKILL.md sits at the repository root
(non-spec layout), the install reported success but:

- .metadata.json gained no entry (cross-machine recovery via metadata broken)
- status --json tracked_repos did not list the repo

Two coupled bugs caused this:

1. cmdInstall gated ReconcileGlobalSkills on len(InstalledSkills) > 0.
   Tracked repos with no discoverable nested skills produced an empty
   InstalledSkills slice, so reconcile never ran and never detected the
   _<name>/.git directory to write a tracked entry.

2. extractTrackedRepos derived repo names from discovered skills, so
   repos with zero discoverable skills were silently dropped from status
   and list output.

Fix both by:

- Making the post-install reconcile unconditional on success. Reconcile is
  idempotent and cheap; gating it on InstalledSkills > 0 created a path
  where successful installs left metadata stale.

- Switching extractTrackedRepos to install.GetTrackedRepos (a directory
  walk for _-prefixed git repos), the same source-of-truth used by check
  and update. Shared across status, status_project, list, and list_project.

Closes #163

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Willie
2026-05-22 10:57:55 +08:00
co-authored by Claude
parent d364edd698
commit 210605ea4e
6 changed files with 132 additions and 27 deletions
+2 -2
View File
@@ -462,7 +462,7 @@ func cmdInstall(args []string) error {
if summary.Source == "" {
summary.Source = parsed.sourceArg
}
if err == nil && !parsed.opts.DryRun && len(summary.InstalledSkills) > 0 {
if err == nil && !parsed.opts.DryRun {
store, storeErr := install.LoadMetadataWithMigration(cfg.EffectiveSkillsSource(), "")
if storeErr != nil {
ui.Warning("Failed to load metadata: %v", storeErr)
@@ -484,7 +484,7 @@ func cmdInstall(args []string) error {
if summary.Source == "" {
summary.Source = parsed.sourceArg
}
if err == nil && !parsed.opts.DryRun && len(summary.InstalledSkills) > 0 {
if err == nil && !parsed.opts.DryRun {
store, storeErr := install.LoadMetadataWithMigration(cfg.EffectiveSkillsSource(), "")
if storeErr != nil {
ui.Warning("Failed to load metadata: %v", storeErr)
+1 -1
View File
@@ -672,7 +672,7 @@ func cmdList(args []string) error {
}
return fmt.Errorf("cannot discover skills: %w", discErr)
}
trackedRepos = extractTrackedRepos(discoveredSkills)
trackedRepos = extractTrackedRepos(cfg.EffectiveSkillsSource())
if sp != nil {
sp.Update(fmt.Sprintf("Reading metadata for %d skills...", len(discoveredSkills)))
}
+1 -1
View File
@@ -107,7 +107,7 @@ func cmdListProject(root string, opts listOptions, kind resourceKindFilter) erro
}
return fmt.Errorf("cannot discover project skills: %w", discErr)
}
trackedRepos = extractTrackedRepos(discoveredSkills)
trackedRepos = extractTrackedRepos(skillsSource)
if sp != nil {
sp.Update(fmt.Sprintf("Reading metadata for %d skills...", len(discoveredSkills)))
}
+11 -21
View File
@@ -11,6 +11,7 @@ import (
"skillshare/internal/audit"
"skillshare/internal/config"
"skillshare/internal/git"
"skillshare/internal/install"
"skillshare/internal/resource"
"skillshare/internal/skillignore"
"skillshare/internal/sync"
@@ -118,7 +119,7 @@ func cmdStatus(args []string) error {
if discoverErr != nil {
discovered = nil
}
trackedRepos := extractTrackedRepos(discovered)
trackedRepos := extractTrackedRepos(cfg.EffectiveSkillsSource())
sp.Stop()
printSourceStatus(cfg, len(discovered), stats)
@@ -146,7 +147,7 @@ func cmdStatus(args []string) error {
}
discovered, stats, _ := sync.DiscoverSourceSkillsWithStats(cfg.EffectiveSkillsSource())
trackedRepos := extractTrackedRepos(discovered)
trackedRepos := extractTrackedRepos(cfg.EffectiveSkillsSource())
output.Source = statusJSONSource{
Path: cfg.EffectiveSkillsSource(),
@@ -301,25 +302,14 @@ func printTrackedReposStatus(cfg *config.Config, discovered []sync.DiscoveredSki
}
}
// extractTrackedRepos derives tracked repo names from discovered skills.
// Note: repos with zero skills will not appear (acceptable trade-off).
func extractTrackedRepos(discovered []sync.DiscoveredSkill) []string {
seen := make(map[string]bool)
var repos []string
for _, d := range discovered {
if !d.IsInRepo {
continue
}
// First path segment is the repo name (e.g. "_team" from "_team/frontend/ui")
idx := strings.Index(d.RelPath, "/")
if idx <= 0 {
continue
}
repo := d.RelPath[:idx]
if !seen[repo] {
seen[repo] = true
repos = append(repos, repo)
}
// extractTrackedRepos walks sourcePath for `_`-prefixed directories that are
// git repositories. Using a directory walk (instead of deriving from discovered
// skills) ensures repos with zero discoverable skills — e.g. those whose only
// SKILL.md sits at the repo root — still appear in status output.
func extractTrackedRepos(sourcePath string) []string {
repos, err := install.GetTrackedRepos(sourcePath)
if err != nil {
return nil
}
sort.Strings(repos)
return repos
+2 -2
View File
@@ -32,7 +32,7 @@ func cmdStatusProject(root string) error {
if discoverErr != nil {
discovered = nil
}
trackedRepos := extractTrackedRepos(discovered)
trackedRepos := extractTrackedRepos(runtime.sourcePath)
sp.Stop()
printProjectSourceStatus(runtime.sourcePath, runtime.agentsSourcePath, len(discovered), stats)
@@ -71,7 +71,7 @@ func cmdStatusProjectJSON(root string) error {
}
discovered, stats, _ := sync.DiscoverSourceSkillsWithStats(runtime.sourcePath)
trackedRepos := extractTrackedRepos(discovered)
trackedRepos := extractTrackedRepos(runtime.sourcePath)
output.Source = statusJSONSource{
Path: runtime.sourcePath,
@@ -0,0 +1,115 @@
//go:build !online
package integration
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"skillshare/internal/install"
"skillshare/internal/testutil"
)
// setupBareRepoWithRootSkill creates a bare git repo whose SKILL.md sits at
// the repository root (non-spec layout). Returns the file:// URL for cloning.
//
// Mirrors real-world repos like op7418/guizang-ppt-skill that put SKILL.md
// alongside README/LICENSE at the repo root instead of in a subdirectory.
func setupBareRepoWithRootSkill(t *testing.T, sb *testutil.Sandbox, name string) string {
t.Helper()
remoteDir := filepath.Join(sb.Root, name+"-remote.git")
run(t, "", "git", "init", "--bare", "--initial-branch=main", remoteDir)
workDir := filepath.Join(sb.Root, name+"-work")
run(t, sb.Root, "git", "clone", remoteDir, workDir)
os.WriteFile(filepath.Join(workDir, "SKILL.md"),
[]byte("---\nname: "+name+"\ndescription: root-level skill\n---\n# "+name+"\n"), 0644)
os.WriteFile(filepath.Join(workDir, "README.md"),
[]byte("# "+name+"\n"), 0644)
run(t, workDir, "git", "add", "-A")
run(t, workDir, "git", "commit", "-m", "initial")
run(t, workDir, "git", "push", "origin", "HEAD:main")
return "file://" + remoteDir
}
// TestInstall_Track_RootSkillMd_WritesMetadata verifies that installing a
// tracked repo whose SKILL.md is at the repo root persists a metadata entry.
// Regression test for issue #163.
func TestInstall_Track_RootSkillMd_WritesMetadata(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
repoURL := setupBareRepoWithRootSkill(t, sb, "root-skill")
result := sb.RunCLI("install", repoURL, "--track", "--name", "root-tracked")
result.AssertSuccess(t)
repoPath := filepath.Join(sb.SourcePath, "_root-tracked")
if !sb.FileExists(repoPath) {
t.Fatalf("tracked repo should be cloned to %s", repoPath)
}
store, err := install.LoadMetadata(sb.SourcePath)
if err != nil {
t.Fatalf("failed to load metadata: %v", err)
}
entry := store.Get("_root-tracked")
if entry == nil {
t.Fatalf("metadata entry '_root-tracked' missing — got keys: %v", store.List())
}
if !entry.Tracked {
t.Errorf("entry.Tracked = false, want true")
}
if entry.Source == "" {
t.Errorf("entry.Source is empty — expected git remote URL")
}
if entry.Branch == "" {
t.Errorf("entry.Branch is empty — expected current branch (main)")
}
}
// TestInstall_Track_RootSkillMd_ShowsInStatus verifies that a tracked repo
// with only a root SKILL.md still appears in `status --json` under
// tracked_repos. Regression test for issue #163.
func TestInstall_Track_RootSkillMd_ShowsInStatus(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
repoURL := setupBareRepoWithRootSkill(t, sb, "root-status")
installResult := sb.RunCLI("install", repoURL, "--track", "--name", "status-tracked")
installResult.AssertSuccess(t)
statusResult := sb.RunCLI("status", "--json")
statusResult.AssertSuccess(t)
var output struct {
TrackedRepos []struct {
Name string `json:"name"`
SkillCount int `json:"skill_count"`
Dirty bool `json:"dirty"`
} `json:"tracked_repos"`
}
if err := json.Unmarshal([]byte(statusResult.Stdout), &output); err != nil {
t.Fatalf("failed to parse status --json: %v\nstdout: %s", err, statusResult.Stdout)
}
found := false
for _, r := range output.TrackedRepos {
if r.Name == "_status-tracked" {
found = true
break
}
}
if !found {
t.Errorf("tracked_repos should contain '_status-tracked', got %+v", output.TrackedRepos)
}
}