perf(update): skip unchanged skills in grouped repo updates

When update --all processes many skills from the same repo, avoid reinstall/copy when installed metadata already matches the fetched repo state.

For root skills, skip when meta.Version matches repo commit. For subdir skills, require both meta.Version and meta.TreeHash to match. Added unit tests for match/mismatch cases.
This commit is contained in:
Willie
2026-02-28 02:03:55 +08:00
parent a923a4957c
commit ec81fc11b0
2 changed files with 182 additions and 0 deletions
+47
View File
@@ -2,6 +2,8 @@ package install
import (
"fmt"
"path/filepath"
"strings"
)
// BatchUpdateResult holds results for a batch of skills from the same repository
@@ -41,12 +43,19 @@ func UpdateSkillsFromRepo(repoURL string, skillTargets map[string]string, opts I
Results: make(map[string]*InstallResult),
Errors: make(map[string]error),
}
repoPath := filepath.Join(discovery.RepoPath, "repo")
if result.CommitHash == "" {
if hash, hashErr := getGitCommit(repoPath); hashErr == nil {
result.CommitHash = hash
}
}
// Create a map for quick lookup in discovery
discoveryMap := make(map[string]SkillInfo)
for _, s := range discovery.Skills {
discoveryMap[s.Path] = s
}
treeHashCache := make(map[string]string)
// 2. Install/Update each requested skill from the discovered repo
for repoSubdir, destPath := range skillTargets {
@@ -59,6 +68,13 @@ func UpdateSkillsFromRepo(repoURL string, skillTargets map[string]string, opts I
result.Errors[repoSubdir] = fmt.Errorf("skill path %q not found in repository", repoSubdir)
continue
}
// Fast path for update --all on huge inventories:
// if installed metadata already points at this exact repo commit/tree,
// skip reinstall/copy entirely.
if opts.Update && !opts.DryRun &&
isSkillCurrentAtRepoState(destPath, repoSubdir, result.CommitHash, repoPath, treeHashCache) {
continue
}
// Use a local copy of options to avoid side effects
skillOpts := opts
@@ -74,3 +90,34 @@ func UpdateSkillsFromRepo(repoURL string, skillTargets map[string]string, opts I
return result, nil
}
// isSkillCurrentAtRepoState returns true when installed metadata already
// matches the latest fetched repo state for this skill path.
func isSkillCurrentAtRepoState(destPath, repoSubdir, commitHash, repoPath string, treeHashCache map[string]string) bool {
if commitHash == "" || repoPath == "" {
return false
}
meta, err := ReadMeta(destPath)
if err != nil || meta == nil {
return false
}
if strings.TrimSpace(meta.Version) != strings.TrimSpace(commitHash) {
return false
}
subdir := strings.TrimSpace(repoSubdir)
if subdir == "" || subdir == "." {
// Root installs don't have a stable tree hash field; commit match is enough.
return true
}
if strings.TrimSpace(meta.TreeHash) == "" {
return false
}
treeHash, ok := treeHashCache[subdir]
if !ok {
treeHash = getSubdirTreeHash(repoPath, subdir)
treeHashCache[subdir] = treeHash
}
return treeHash != "" && meta.TreeHash == treeHash
}
+135
View File
@@ -0,0 +1,135 @@
package install
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestIsSkillCurrentAtRepoState_RootSkillCommitMatch(t *testing.T) {
repo := initTestRepo(t)
if err := os.WriteFile(filepath.Join(repo, "SKILL.md"), []byte("# Root"), 0644); err != nil {
t.Fatal(err)
}
gitAdd(t, repo, ".")
gitCommit(t, repo, "add root skill")
commit, err := getGitCommit(repo)
if err != nil || commit == "" {
t.Fatalf("getGitCommit failed: %v, commit=%q", err, commit)
}
dest := t.TempDir()
if err := WriteMeta(dest, &SkillMeta{
Source: "https://example.com/repo",
Type: "github",
InstalledAt: time.Now(),
Version: commit,
}); err != nil {
t.Fatalf("WriteMeta failed: %v", err)
}
if !isSkillCurrentAtRepoState(dest, ".", commit, repo, map[string]string{}) {
t.Fatal("expected root skill to be up-to-date when commit matches")
}
}
func TestIsSkillCurrentAtRepoState_SubdirTreeHashMatch(t *testing.T) {
repo := initTestRepo(t)
subdir := filepath.Join(repo, "skills", "my-skill")
if err := os.MkdirAll(subdir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(subdir, "SKILL.md"), []byte("# Skill"), 0644); err != nil {
t.Fatal(err)
}
gitAdd(t, repo, ".")
gitCommit(t, repo, "add subdir skill")
commit, err := getGitCommit(repo)
if err != nil || commit == "" {
t.Fatalf("getGitCommit failed: %v, commit=%q", err, commit)
}
tree := getSubdirTreeHash(repo, "skills/my-skill")
if tree == "" {
t.Fatal("expected non-empty tree hash")
}
dest := t.TempDir()
if err := WriteMeta(dest, &SkillMeta{
Source: "https://example.com/repo/skills/my-skill",
Type: "github",
InstalledAt: time.Now(),
Version: commit,
TreeHash: tree,
Subdir: "skills/my-skill",
}); err != nil {
t.Fatalf("WriteMeta failed: %v", err)
}
if !isSkillCurrentAtRepoState(dest, "skills/my-skill", commit, repo, map[string]string{}) {
t.Fatal("expected subdir skill to be up-to-date when commit/tree hash match")
}
}
func TestIsSkillCurrentAtRepoState_SubdirMissingTreeHashDoesNotSkip(t *testing.T) {
repo := initTestRepo(t)
subdir := filepath.Join(repo, "skills", "my-skill")
if err := os.MkdirAll(subdir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(subdir, "SKILL.md"), []byte("# Skill"), 0644); err != nil {
t.Fatal(err)
}
gitAdd(t, repo, ".")
gitCommit(t, repo, "add subdir skill")
commit, err := getGitCommit(repo)
if err != nil || commit == "" {
t.Fatalf("getGitCommit failed: %v, commit=%q", err, commit)
}
dest := t.TempDir()
if err := WriteMeta(dest, &SkillMeta{
Source: "https://example.com/repo/skills/my-skill",
Type: "github",
InstalledAt: time.Now(),
Version: commit,
Subdir: "skills/my-skill",
}); err != nil {
t.Fatalf("WriteMeta failed: %v", err)
}
if isSkillCurrentAtRepoState(dest, "skills/my-skill", commit, repo, map[string]string{}) {
t.Fatal("expected subdir skill without tree hash to require reinstall")
}
}
func TestIsSkillCurrentAtRepoState_CommitMismatchDoesNotSkip(t *testing.T) {
repo := initTestRepo(t)
if err := os.WriteFile(filepath.Join(repo, "SKILL.md"), []byte("# Root"), 0644); err != nil {
t.Fatal(err)
}
gitAdd(t, repo, ".")
gitCommit(t, repo, "add root skill")
commit, err := getGitCommit(repo)
if err != nil || commit == "" {
t.Fatalf("getGitCommit failed: %v, commit=%q", err, commit)
}
dest := t.TempDir()
if err := WriteMeta(dest, &SkillMeta{
Source: "https://example.com/repo",
Type: "github",
InstalledAt: time.Now(),
Version: "deadbeef",
}); err != nil {
t.Fatalf("WriteMeta failed: %v", err)
}
if isSkillCurrentAtRepoState(dest, ".", commit, repo, map[string]string{}) {
t.Fatal("expected commit mismatch to require reinstall")
}
}