fix(update): repair skill sources recorded before the per-skill source fix (#866)

Before #863, `install <repo> -s <skill>` recorded "<repo>/<path>" as the
skill's source. GitLab, Bitbucket and file:// sources parse that string as
another repository, so every path that parsed the recorded source failed
for those skills: `update` of a single skill, `install <name> --update`,
the dashboard update, and the local-changes diff. #863 only changed what
new installs record.

The record still holds the repository and the path, so RecordedSource
rebuilds the source from them when the written string parses as a
different repository, and the reinstall writes the record back in the
form that parses.

In project mode the config entry that declared the old string follows the
rewritten record, because teammates install from the config. An entry a
teammate changed stays as it is, and so does a record a batch update only
respelled. The config is reloaded first: `update --prune` saves it through
another copy, and saving the stale one brought pruned skills back.

`hub draft` and `hub index` published the old string as it was written;
they now publish the source that installs the skill.

Closes #862
This commit is contained in:
Willie
2026-11-11 23:34:43 +08:00
committed by GitHub
parent 8abf3f50ed
commit 9f43310a3d
13 changed files with 319 additions and 10 deletions
+1 -1
View File
@@ -335,7 +335,7 @@ func resolveSkillFromName(skillName string, cfg *config.Config) (*install.Source
return nil, fmt.Errorf("skill '%s' has no metadata, cannot update", skillName)
}
source, err := install.ParseSourceWithOptions(entry.Source, parseOptsFromConfig(cfg))
source, err := install.RecordedSource(entry, parseOptsFromConfig(cfg))
if err != nil {
return nil, fmt.Errorf("invalid source in metadata: %w", err)
}
+2 -2
View File
@@ -214,7 +214,7 @@ func updateRegularSkill(uc *updateContext, skillName string) (updateResult, erro
startUpdate := time.Now()
// Parse source and reinstall
source, err := install.ParseSourceWithOptions(meta.Source, uc.parseOpts)
source, err := install.RecordedSource(meta, uc.parseOpts)
if err != nil {
return updateResult{skipped: 1}, fmt.Errorf("invalid source in metadata: %w", err)
}
@@ -317,7 +317,7 @@ func updateSkillFromMeta(uc *updateContext, skillPath string, cachedMeta *instal
}
}
source, err := install.ParseSourceWithOptions(meta.Source, uc.parseOpts)
source, err := install.RecordedSource(meta, uc.parseOpts)
if err != nil {
return false, nil, nil
}
+12
View File
@@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"skillshare/internal/config"
"skillshare/internal/install"
"skillshare/internal/sourcewalk"
ssync "skillshare/internal/sync"
@@ -44,6 +45,17 @@ func cmdUpdateProject(args []string, root string) (*updateResult, error) {
// An update is what moves the lockfile forward.
if !opts.dryRun {
defer trackProjectLock(runtime)()
// Runs before the lockfile is written, which pins by the config's source.
before := install.LoadMetadataOrNew(runtime.sourcePath)
defer func() {
// The update may have saved the config through another copy (--prune).
if fresh, err := config.LoadProject(root); err == nil {
*runtime.config = *fresh
}
if err := config.FollowRepairedSources(root, runtime.config, before, install.LoadMetadataOrNew(runtime.sourcePath), parseOptsFromProjectConfig(runtime.config)); err != nil {
ui.Warning("Could not update the project config: %v", err)
}
}()
}
if opts.all {
+28
View File
@@ -80,6 +80,34 @@ func followInstalledSources(cfg *ProjectConfig, before, store *install.MetadataS
return changed
}
// FollowRepairedSources points config entries at the records an update
// rewrote because their source did not parse back to what was installed
// (#862): teammates install from the config, and that source fails for them.
// before is the store from before the update. An entry that no longer
// declares the old record is left alone.
func FollowRepairedSources(projectRoot string, cfg *ProjectConfig, before, store *install.MetadataStore, opts install.ParseOptions) error {
changed := false
for i := range cfg.Skills {
skill := &cfg.Skills[i]
was, now := before.GetExact(skill.FullName()), store.GetExact(skill.FullName())
if skill.EffectiveKind() != "skill" || was == nil || now == nil || skill.Source != was.Source || now.Source == was.Source {
continue
}
if install.RecordedSourceString(was, opts) == was.Source {
continue
}
skill.Source = now.Source
changed = true
}
if !changed {
return nil
}
if err := cfg.Save(projectRoot); err != nil {
return fmt.Errorf("failed to save project config: %w", err)
}
return nil
}
// ForgetProjectSkills drops the config entries and lock pins of uninstalled
// skills, so a later `install -p`, here or on a teammate's machine, does not
// bring them back. A name is a skill, or a group folder with every skill
+38
View File
@@ -592,6 +592,44 @@ func TestReconcileProjectInstall_ConfigFollowsReinstalledSource(t *testing.T) {
}
}
// Regression for #862: when an update rewrites a record whose source did not
// parse back, the config entry that declared that source follows it. A
// declaration a teammate changed, and a record that only changed its spelling,
// stay as they are.
func TestFollowRepairedSources(t *testing.T) {
const old, repaired = "file:///srv/skills/x", "file:///srv/skills//x"
for _, tt := range []struct {
name string
declared string
was install.MetadataEntry
now string
want string
}{
{"declares the old record", old, install.MetadataEntry{Source: old, RepoURL: "file:///srv/skills", Subdir: "x"}, repaired, repaired},
{"changed by a teammate", "other/repo/x", install.MetadataEntry{Source: old, RepoURL: "file:///srv/skills", Subdir: "x"}, repaired, "other/repo/x"},
{"record respelled by a batch update", "org/skills/x", install.MetadataEntry{Source: "org/skills/x", RepoURL: "https://github.com/org/skills.git", Subdir: "x"}, "https://github.com/org/skills.git/x", "org/skills/x"},
} {
t.Run(tt.name, func(t *testing.T) {
root := t.TempDir()
cfg := &ProjectConfig{
Targets: []ProjectTargetEntry{{Name: "claude"}},
Skills: []SkillEntry{{Name: "x", Source: tt.declared}},
}
before := install.NewMetadataStore()
before.Set("x", &tt.was)
store := install.NewMetadataStore()
store.Set("x", &install.MetadataEntry{Source: tt.now, RepoURL: tt.was.RepoURL, Subdir: "x"})
if err := FollowRepairedSources(root, cfg, before, store, install.ParseOptions{}); err != nil {
t.Fatalf("FollowRepairedSources: %v", err)
}
if got := cfg.Skills[0].Source; got != tt.want {
t.Errorf("config source = %q, want %q", got, tt.want)
}
})
}
}
// A teammate's change to config.yaml must survive an install of another skill
// on a machine whose copy still comes from the old source.
func TestReconcileProjectInstall_KeepsDeclarationOfUntouchedSkill(t *testing.T) {
+5 -4
View File
@@ -69,7 +69,7 @@ func DraftCandidates(sourcePath string, walk ...sourcewalk.Options) ([]DraftEntr
// parseInstalled parses the entry's source as it was installed, so a web URL
// whose ref contains "/" splits into ref and subdir as the install did.
func parseInstalled(entry *install.MetadataEntry) (*install.Source, error) {
parsed, err := install.ParseSource(entry.Source)
parsed, err := install.RecordedSource(entry, install.ParseOptions{})
if err == nil && entry.Branch != "" {
parsed.ApplyRecordedBranch(entry.Branch)
}
@@ -79,15 +79,16 @@ func parseInstalled(entry *install.MetadataEntry) (*install.Source, error) {
// installedSource returns the entry's source pinned at the ref it was
// installed from, so the hub entry installs the same version.
func installedSource(entry *install.MetadataEntry) string {
source := install.RecordedSourceString(entry, install.ParseOptions{})
if entry.Branch == "" {
return entry.Source
return source
}
parsed, err := parseInstalled(entry)
if err != nil {
return entry.Source
return source
}
if pinned, err := parsed.AtRef(entry.Branch); err == nil {
return pinned
}
return entry.Source
return source
}
+26
View File
@@ -186,6 +186,32 @@ func TestDraftCandidatesLocalPathsAndMetadata(t *testing.T) {
}
}
// Regression for #862: a record written before #859 is published with the
// source that installs it, by the draft and by the index.
func TestPublishedSourceOfRecordBefore859(t *testing.T) {
root := t.TempDir()
os.MkdirAll(filepath.Join(root, "x"), 0755)
os.WriteFile(filepath.Join(root, "x", "SKILL.md"), []byte("---\nname: x\ndescription: d\n---\n# x"), 0644)
os.WriteFile(filepath.Join(root, ".metadata.json"), []byte(`{"version":1,"entries":{"x":{"source":"https://gitlab.com/team/skills/x","repo_url":"https://gitlab.com/team/skills.git","subdir":"x"}}}`), 0644)
const want = "https://gitlab.com/team/skills.git/x"
entries, err := DraftCandidates(root)
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 || field(entries[0].Data, "source") != want || field(entries[0].Data, "skill") != "" {
t.Errorf("draft source=%q skill=%q, want source %q and no skill", field(entries[0].Data, "source"), field(entries[0].Data, "skill"), want)
}
idx, err := BuildIndex(root, false, false)
if err != nil {
t.Fatal(err)
}
if len(idx.Skills) != 1 || idx.Skills[0].Source != want {
t.Errorf("index skills = %+v, want source %q", idx.Skills, want)
}
}
func TestDraftCandidatesTrackedRepository(t *testing.T) {
root := t.TempDir()
dir := filepath.Join(root, "_team", "skills", "review")
+1 -1
View File
@@ -79,7 +79,7 @@ func BuildIndex(sourcePath string, full bool, auditSkills bool, walk ...sourcewa
source := d.RelPath
if entry := store.GetByPath(d.RelPath); entry != nil {
if entry.Source != "" {
source = entry.Source
source = install.RecordedSourceString(entry, install.ParseOptions{})
}
if full {
item.Type = entry.Type
+39
View File
@@ -58,6 +58,45 @@ func isDiscoverySkillSource(entry *MetadataEntry, source *Source, skillPath stri
repoURLsMatch(entry.RepoURL, source.CloneURL) && entry.Subdir == filepath.ToSlash(skillPath)
}
// RecordedSource parses the source a skill's entry was installed from.
// Installs from before #859 recorded a skill picked out of a repository as
// "<repo>/<path>", which GitLab, Bitbucket and file:// sources parse as a
// repository of its own. The entry's recorded repository and path then say
// what was installed, and the source is rebuilt in the form that parses back.
func RecordedSource(entry *MetadataEntry, opts ParseOptions) (*Source, error) {
source, _, err := recordedSource(entry, opts)
return source, err
}
// RecordedSourceString is the source string that installs what entry
// records: entry.Source, or the rebuilt one when RecordedSource repairs it.
func RecordedSourceString(entry *MetadataEntry, opts ParseOptions) string {
if source, repaired, err := recordedSource(entry, opts); err == nil && repaired {
return source.Raw
}
return entry.Source
}
func recordedSource(entry *MetadataEntry, opts ParseOptions) (source *Source, repaired bool, err error) {
source, err = ParseSourceWithOptions(entry.Source, opts)
if err != nil || entry.Subdir == "" || source.Subdir != "" || repoURLsMatch(source.CloneURL, entry.RepoURL) {
return source, false, err
}
repoRaw, ok := strings.CutSuffix(entry.Source, "/"+entry.Subdir)
if !ok {
return source, false, nil
}
repo, err := ParseSourceWithOptions(repoRaw, opts)
if err != nil || repo.HasSubdir() || !repoURLsMatch(repo.CloneURL, entry.RepoURL) {
return source, false, nil
}
rebuilt, err := ParseSourceWithOptions(buildDiscoverySkillSource(repo, entry.Subdir), opts)
if err != nil || rebuilt.Subdir != entry.Subdir {
return source, false, nil
}
return rebuilt, true, nil
}
func discoverySourceRoot(discovery *DiscoveryResult) string {
if discovery.Source != nil && discovery.Source.Type == SourceTypeLocalPath {
return discovery.RepoPath
+1 -1
View File
@@ -63,7 +63,7 @@ func LocalChanges(entry *MetadataEntry, parseOpts ParseOptions, dir string, foll
if commit == "" {
return nil, errors.New("no recorded commit to compare with")
}
source, err := ParseSourceWithOptions(entry.Source, parseOpts)
source, err := RecordedSource(entry, parseOpts)
if err != nil {
return nil, fmt.Errorf("invalid source: %w", err)
}
+27
View File
@@ -123,6 +123,33 @@ func TestIsDiscoverySkillSource_FormBefore859(t *testing.T) {
}
}
// Regression for #862: a record written before #859 holds a source that parses
// as another repository; its recorded repository and path rebuild it.
func TestRecordedSource(t *testing.T) {
for _, tt := range []struct {
name string
entry MetadataEntry
wantRaw string
}{
{"file source in the old form", MetadataEntry{Source: "file:///srv/skills/pack/x", RepoURL: "file:///srv/skills", Subdir: "pack/x"}, "file:///srv/skills//pack/x"},
{"gitlab source in the old form", MetadataEntry{Source: "https://gitlab.com/team/skills/x", RepoURL: "https://gitlab.com/team/skills.git", Subdir: "x"}, "https://gitlab.com/team/skills.git/x"},
{"bitbucket source in the old form", MetadataEntry{Source: "https://bitbucket.org/team/skills/x", RepoURL: "https://bitbucket.org/team/skills.git", Subdir: "x"}, "https://bitbucket.org/team/skills.git/x"},
{"repository of its own at that URL", MetadataEntry{Source: "https://gitlab.com/team/skills/x", RepoURL: "https://gitlab.com/team/skills/x.git"}, "https://gitlab.com/team/skills/x"},
{"source that already parses", MetadataEntry{Source: "github.com/org/skills/x", RepoURL: "https://github.com/org/skills.git", Subdir: "x"}, "github.com/org/skills/x"},
{"record that names another repository", MetadataEntry{Source: "https://gitlab.com/team/skills/x", RepoURL: "https://gitlab.com/other/repo.git", Subdir: "x"}, "https://gitlab.com/team/skills/x"},
} {
t.Run(tt.name, func(t *testing.T) {
source, err := RecordedSource(&tt.entry, ParseOptions{})
if err != nil {
t.Fatalf("RecordedSource: %v", err)
}
if source.Raw != tt.wantRaw {
t.Errorf("source = %q, want %q", source.Raw, tt.wantRaw)
}
})
}
}
func TestBuildDiscoverySkillSource(t *testing.T) {
tests := []struct {
name string
+19 -1
View File
@@ -58,6 +58,7 @@ func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()
defer s.trackProjectLock()()
defer s.followRepairedSources()()
var body updateRequest
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
@@ -441,7 +442,7 @@ func (s *Server) reinstallRegularSkill(name, skillPath, branch string, force, sk
if !force && entry.LocallyModified(skillPath, follow...) {
return updateResultItem{Name: name, Action: "skipped", Message: "has local changes since install (try force update)"}
}
source, err := install.ParseSourceWithOptions(entry.Source, s.parseOpts())
source, err := install.RecordedSource(entry, s.parseOpts())
if err != nil {
return updateResultItem{
Name: name,
@@ -565,6 +566,23 @@ func getServerUpdatableSkills(sourceDir string, store *install.MetadataStore, wa
return skills, nil
}
// followRepairedSources returns a function for defer: the project config
// follows the records the request rewrote because their source did not parse
// (config.FollowRepairedSources). Defer it after trackProjectLock, which pins
// by the config's source.
func (s *Server) followRepairedSources() func() {
if !s.IsProjectMode() {
return func() {}
}
sourceDir := s.cfg.EffectiveSkillsSource()
before := install.LoadMetadataOrNew(sourceDir)
return func() {
if err := config.FollowRepairedSources(s.projectRoot, s.projectCfg, before, install.LoadMetadataOrNew(sourceDir), s.parseOpts()); err != nil {
log.Printf("warning: failed to update project config: %v", err)
}
}
}
// trackProjectLock returns a function for defer: it moves the lockfile pins of
// the skills the request changed. Global mode has no lockfile.
func (s *Server) trackProjectLock() func() {
+120
View File
@@ -7,6 +7,7 @@ import (
"path/filepath"
"testing"
"skillshare/internal/config"
"skillshare/internal/install"
"skillshare/internal/testutil"
)
@@ -470,3 +471,122 @@ func TestUpdateProject_NamedCheckoutUsesTrackedRule(t *testing.T) {
result.AssertAnyOutputContains(t, want)
}
}
// Regression for #862: a skill installed with -s before #859 recorded a source
// that file:// (and GitLab, Bitbucket) parse as another repository. Update
// reinstalls it from the recorded repository and path, and the config entry
// teammates install from follows.
func TestUpdateProject_RepairsSourceRecordedBefore859(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
remoteRepo := filepath.Join(sb.Root, "old-form.git")
workClone := filepath.Join(sb.Root, "old-form-work")
gitInit(t, remoteRepo, true)
gitClone(t, remoteRepo, workClone)
skillFile := filepath.Join(workClone, "x", "SKILL.md")
os.MkdirAll(filepath.Dir(skillFile), 0755)
os.WriteFile(skillFile, []byte("---\nname: x\n---\n# x v1"), 0644)
gitAddCommit(t, workClone, "add x")
gitPush(t, workClone)
repoURL := "file://" + remoteRepo
sb.RunCLIInDir(projectRoot, "install", repoURL, "-s", "x", "-p", "--skip-audit").AssertSuccess(t)
// What 0.30.5 wrote for this install.
oldSource := repoURL + "/x"
skillsDir := filepath.Join(projectRoot, ".skillshare", "skills")
store, err := install.LoadMetadata(skillsDir)
if err != nil {
t.Fatal(err)
}
store.Get("x").Source = oldSource
if err := store.Save(skillsDir); err != nil {
t.Fatal(err)
}
cfg, err := config.LoadProject(projectRoot)
if err != nil {
t.Fatal(err)
}
cfg.Skills[0].Source = oldSource
if err := cfg.Save(projectRoot); err != nil {
t.Fatal(err)
}
os.WriteFile(skillFile, []byte("---\nname: x\n---\n# x v2"), 0644)
gitAddCommit(t, workClone, "update x")
gitPush(t, workClone)
sb.RunCLIInDir(projectRoot, "update", "x", "-p", "--skip-audit").AssertSuccess(t)
if content := sb.ReadFile(filepath.Join(skillsDir, "x", "SKILL.md")); !contains(content, "x v2") {
t.Errorf("skill was not updated: %q", content)
}
cfg, err = config.LoadProject(projectRoot)
if err != nil {
t.Fatal(err)
}
if got, want := cfg.Skills[0].Source, repoURL+"//x"; got != want {
t.Errorf("config source = %q, want %q", got, want)
}
}
// The config update that follows a repaired source must not bring back the
// entry of a skill the same update pruned.
func TestUpdateProject_RepairedSourceKeepsPrunedSkillOut(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
remoteRepo := filepath.Join(sb.Root, "old-form-prune.git")
workClone := filepath.Join(sb.Root, "old-form-prune-work")
gitInit(t, remoteRepo, true)
gitClone(t, remoteRepo, workClone)
for _, name := range []string{"x", "gone"} {
os.MkdirAll(filepath.Join(workClone, name), 0755)
os.WriteFile(filepath.Join(workClone, name, "SKILL.md"), []byte("---\nname: "+name+"\n---\n# "+name+" v1"), 0644)
}
gitAddCommit(t, workClone, "add skills")
gitPush(t, workClone)
repoURL := "file://" + remoteRepo
sb.RunCLIInDir(projectRoot, "install", repoURL, "-s", "x,gone", "-p", "--skip-audit").AssertSuccess(t)
// What 0.30.5 wrote for these installs.
skillsDir := filepath.Join(projectRoot, ".skillshare", "skills")
store, err := install.LoadMetadata(skillsDir)
if err != nil {
t.Fatal(err)
}
cfg, err := config.LoadProject(projectRoot)
if err != nil {
t.Fatal(err)
}
for i := range cfg.Skills {
name := cfg.Skills[i].Name
store.Get(name).Source = repoURL + "/" + name
cfg.Skills[i].Source = repoURL + "/" + name
}
if err := store.Save(skillsDir); err != nil {
t.Fatal(err)
}
if err := cfg.Save(projectRoot); err != nil {
t.Fatal(err)
}
os.RemoveAll(filepath.Join(workClone, "gone"))
os.WriteFile(filepath.Join(workClone, "x", "SKILL.md"), []byte("---\nname: x\n---\n# x v2"), 0644)
gitAddCommit(t, workClone, "remove gone, update x")
gitPush(t, workClone)
sb.RunCLIInDir(projectRoot, "update", "--all", "--prune", "--skip-audit", "-p").AssertSuccess(t)
cfg, err = config.LoadProject(projectRoot)
if err != nil {
t.Fatal(err)
}
if len(cfg.Skills) != 1 || cfg.Skills[0].Name != "x" || cfg.Skills[0].Source != repoURL+"//x" {
t.Errorf("config skills = %+v, want only x from %s", cfg.Skills, repoURL+"//x")
}
}