Files
skillshare/internal/server/handler_agent_diff.go
Willie 7af9224896 fix(diff): list only what sync prunes, and have doctor follow it (#942)
Three previews disagreed with what sync does to a link no skill or
agent maps to.

The agent diff, in the CLI and the dashboard, listed every orphan link
as a removal. Agent sync prunes a link only in merge mode, and only when
it is broken or points into the source, so a live link to another
location was listed forever. Both diffs now ask the function prune uses.

The CLI diff listed no orphan links at all for merge-mode skills, so a
skill removed from the source showed nothing until sync pruned its
link. It now lists the links sync.OrphanLink says prune removes.

doctor called a broken link "did not create, kept" whenever OwnsLink was
false. Prune also removes a broken link under a name the manifest
records in any form once no skill maps to it, so doctor told the user to
delete by hand a link that sync removes. It now reports those as broken
and suggests sync. An empty source counts as "every entry is an orphan";
only a failed discovery turns the check off.

Closes #939
Closes #940
Closes #941
2026-11-17 13:38:31 +08:00

147 lines
3.7 KiB
Go

package server
import (
"os"
"path/filepath"
"strings"
"skillshare/internal/config"
"skillshare/internal/resource"
"skillshare/internal/sync"
"skillshare/internal/utils"
)
// computeAgentTargetDiff computes diff items for agents in a single target directory.
// Returns items with Kind="agent" for each pending action (link, update, prune, local).
// Like sync, it applies the target's include/exclude and the agents' frontmatter
// targets first, and a target with an extension compares the converted outputs
// recorded in its manifest.
func computeAgentTargetDiff(targetName, targetDir, sourceDir string, ac config.ResourceTargetConfig, agents []resource.DiscoveredResource) []diffItem {
var items []diffItem
filtered, err := sync.FilterAgents(agents, ac.Include, ac.Exclude)
if err != nil {
filtered = agents // invalid include/exclude: sync reports it
}
agents = sync.FilterAgentsByTarget(filtered, targetName)
if ac.Extension != "" {
synced, orphans := sync.ExtensionOutputStatus(targetDir, agents)
for i, a := range agents {
if !synced[i] {
items = append(items, diffItem{
Skill: a.FlatName,
Action: "link",
Reason: "output missing or outdated",
Kind: kindAgent,
})
}
}
for _, name := range orphans {
items = append(items, diffItem{
Skill: name,
Action: "prune",
Reason: "orphan output",
Kind: kindAgent,
})
}
return items
}
// Build expected set
expected := make(map[string]resource.DiscoveredResource, len(agents))
for _, a := range agents {
expected[a.FlatName] = a
}
// Read existing .md files in target
existing := make(map[string]os.FileMode)
if entries, err := os.ReadDir(targetDir); err == nil {
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(strings.ToLower(e.Name()), ".md") {
continue
}
if resource.ConventionalExcludes[e.Name()] {
continue
}
existing[e.Name()] = e.Type()
}
}
// Missing agents → link
for flatName, agent := range expected {
fileType, ok := existing[flatName]
if !ok {
items = append(items, diffItem{
Skill: flatName,
Action: "link",
Reason: "source only",
Kind: kindAgent,
})
continue
}
// Exists — check if symlink points to correct source
targetPath := filepath.Join(targetDir, flatName)
if fileType&os.ModeSymlink != 0 || utils.IsSymlinkOrJunction(targetPath) {
absLink, err := utils.ResolveLinkTarget(targetPath)
if err != nil {
items = append(items, diffItem{
Skill: flatName,
Action: "update",
Reason: "link target unreadable",
Kind: kindAgent,
})
continue
}
absSource, _ := filepath.Abs(agent.AbsPath)
if !utils.PathsEqual(absLink, absSource) {
items = append(items, diffItem{
Skill: flatName,
Action: "update",
Reason: "symlink points elsewhere",
Kind: kindAgent,
})
}
continue
}
// Match skills diff semantics: a local file blocks sync unless forced.
items = append(items, diffItem{
Skill: flatName,
Action: "skip",
Reason: "local copy (sync --force to replace)",
Kind: kindAgent,
})
}
// Orphan/local detection
for name, fileType := range existing {
if _, ok := expected[name]; ok {
continue
}
targetPath := filepath.Join(targetDir, name)
if fileType&os.ModeSymlink != 0 || utils.IsSymlinkOrJunction(targetPath) {
// Sync keeps a link it would not prune, so it is not a change.
if !sync.PrunesAgentLink(ac.Mode, targetPath, sourceDir) {
continue
}
items = append(items, diffItem{
Skill: name,
Action: "prune",
Reason: "orphan symlink",
Kind: kindAgent,
})
} else {
items = append(items, diffItem{
Skill: name,
Action: "local",
Reason: "local file",
Kind: kindAgent,
})
}
}
return items
}