Files
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

120 lines
3.9 KiB
Go

package sync
import (
"fmt"
"skillshare/internal/config"
"skillshare/internal/resource"
)
// AgentTarget is one target an agent sync run visits.
type AgentTarget struct {
Name string
Path string // resolved agents path; "" when the target has none
Config config.ResourceTargetConfig
}
// AgentRunOptions holds what differs between agent sync callers.
type AgentRunOptions struct {
Source string // agents source directory
ProjectRoot string // passed to SyncAgents; "" in global mode
DryRun bool
Force bool
// ResolveExtension turns a target's extension value into a transform
// spec. A nil spec links or copies agents instead of transforming them.
ResolveExtension func(ext string) (*ExtensionSpec, error)
}
// AgentTargetResult is the outcome of an agent sync for one target.
type AgentTargetResult struct {
Name string
Path string // "" when the target has no agents path and was not synced
// Mode is the configured mode (merge when unset), after the file-link
// fallback for syncs that do not transform.
Mode string
Linked []string
Updated []string
Skipped []string
Pruned []string
Synced bool // sync or transform returned results, possibly partial
PruneErr error // also reported in Warnings; the sync itself stands
Warnings []string
Err error // invalid filter or extension; nothing was synced
SyncErr error // sync or transform failure
}
// PrunesAgentLink reports whether an agent sync in mode removes the orphan link
// at path. Only merge mode prunes links; copy mode removes the copies it
// tracks and leaves every link alone.
func PrunesAgentLink(mode, path, sourceDir string) bool {
return EffectiveMode(mode) == "merge" && PrunableLink(path, sourceDir)
}
// RunAgentSync filters agents for each target, syncs or transforms them into
// the target's agents path, and prunes orphans left by earlier syncs.
func RunAgentSync(targets []AgentTarget, agents []resource.DiscoveredResource, opts AgentRunOptions) []AgentTargetResult {
results := make([]AgentTargetResult, 0, len(targets))
for _, t := range targets {
results = append(results, runAgentTarget(t, agents, opts))
}
return results
}
func runAgentTarget(t AgentTarget, agents []resource.DiscoveredResource, opts AgentRunOptions) AgentTargetResult {
res := AgentTargetResult{Name: t.Name, Path: t.Path}
if t.Path == "" {
return res
}
mode := t.Config.Mode
if mode == "" {
mode = "merge"
}
res.Mode = mode
filtered, err := FilterAgents(agents, t.Config.Include, t.Config.Exclude)
if err != nil {
res.Err = fmt.Errorf("invalid agent filter: %w", err)
return res
}
filtered = FilterAgentsByTarget(filtered, t.Name)
spec, err := opts.ResolveExtension(t.Config.Extension)
if err != nil {
res.Err = err
return res
}
var result *AgentSyncResult
outputExt := ""
if spec != nil {
result, res.SyncErr = SyncAgentsTransform(filtered, opts.Source, t.Path, mode, spec, opts.DryRun, opts.Force)
outputExt = spec.OutputExt
} else {
result, res.SyncErr = SyncAgents(filtered, opts.Source, t.Path, mode, opts.DryRun, opts.Force, opts.ProjectRoot)
res.Mode = EffectiveAgentMode(mode)
if res.Mode != mode {
res.Warnings = append(res.Warnings, t.Name+": agents "+FileLinkFallbackWarning)
}
}
// A transform returns partial results when only some agents failed;
// the rest still count and orphans still get pruned.
if result == nil {
return res
}
res.Synced = true
res.Linked, res.Updated, res.Skipped = result.Linked, result.Updated, result.Skipped
// Prune even when the source is empty so uninstall-all clears previously
// synced target entries, matching skills.
switch mode {
case "copy":
res.Pruned, res.PruneErr = PruneOrphanAgentCopies(t.Path, filtered, outputExt, opts.DryRun)
case "merge":
res.Pruned, res.PruneErr = PruneOrphanAgentLinks(t.Path, opts.Source, filtered, opts.DryRun)
}
if res.PruneErr != nil {
res.Warnings = append(res.Warnings, fmt.Sprintf("%s: agents prune failed: %v", t.Name, res.PruneErr))
}
return res
}