Files
skillshare/internal/server/handler_sync.go
T
Willie 57919f1b6c fix(server): refuse to replace a conflicting skills symlink without force
In symlink mode the Web API called SyncTarget directly, so a target
symlink pointing somewhere else was handled differently from `skillshare
sync`: no conflict message, and force had no effect. It now checks the
link first like the CLI. Without force the target fails with "conflict -
symlink points to X (use --force to override)" and counts as a failed
target; with force the link is replaced.
2026-09-30 02:04:46 +08:00

642 lines
19 KiB
Go

package server
import (
"encoding/json"
"fmt"
"maps"
"net/http"
"os"
"path/filepath"
"sort"
"time"
"skillshare/internal/backup"
"skillshare/internal/config"
"skillshare/internal/skillignore"
ssync "skillshare/internal/sync"
"skillshare/internal/utils"
)
// ignorePayload builds the common ignored-skills fields for JSON responses.
func ignorePayload(stats *skillignore.IgnoreStats) map[string]any {
skills := []string{}
rootFile := ""
repoFiles := []string{}
if stats != nil {
if len(stats.IgnoredSkills) > 0 {
skills = stats.IgnoredSkills
}
rootFile = stats.RootFile
if stats.RepoFiles != nil {
repoFiles = stats.RepoFiles
}
}
return map[string]any{
"ignored_count": len(skills),
"ignored_skills": skills,
"ignore_root": rootFile,
"ignore_repos": repoFiles,
}
}
type syncTargetResult struct {
Target string `json:"target"`
Linked []string `json:"linked"`
Updated []string `json:"updated"`
Skipped []string `json:"skipped"`
Pruned []string `json:"pruned"`
DirCreated string `json:"dir_created,omitempty"`
}
func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
var body struct {
DryRun bool `json:"dryRun"`
Force bool `json:"force"`
Kind string `json:"kind"`
// Project limits the sync to one declared project's targets.
Project string `json:"project"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
// Default to non-dry-run, non-force, empty kind (both)
}
if body.Kind != "" && body.Kind != kindSkill && body.Kind != kindAgent {
writeError(w, http.StatusBadRequest, "invalid kind: must be 'skill', 'agent', or empty")
return
}
if body.Project != "" {
root := s.declaredProjectRoot(body.Project)
if _, ok := s.cfg.Projects[root]; !ok {
writeError(w, http.StatusBadRequest, "unknown project: "+body.Project)
return
}
body.Project = root
}
out, code, err := s.syncResources(start, body.DryRun, body.Force, body.Kind, body.Project)
if err != nil {
writeError(w, code, err.Error())
return
}
resp := map[string]any{
"results": out.results,
"warnings": out.warnings,
"folder_conflicts": out.folderConflicts,
"path_overlap": out.pathOverlap,
}
if len(out.skills) > 0 {
if contextCost := s.computeContextCost(out.skills); contextCost != nil {
resp["context_cost"] = contextCost
}
}
maps.Copy(resp, ignorePayload(out.ignoreStats))
maps.Copy(resp, agentIgnorePayload(s.agentsSource(), nil))
writeJSON(w, resp)
}
type syncOutcome struct {
results []syncTargetResult
warnings []string
folderConflicts []config.SkillsFolderConflict
pathOverlap int // targets whose path overlap the folder conflicts don't explain
skills []ssync.DiscoveredSkill
ignoreStats *skillignore.IgnoreStats
}
// folderConflicts returns the skills folders whose targets undo each other's
// sync, never nil, plus how many targets overlap in a way they don't explain.
func folderConflicts(targets map[string]config.TargetConfig, isProject bool) ([]config.SkillsFolderConflict, int) {
conflicts := config.SkillsFolderConflicts(targets)
explained := map[string]bool{}
for _, c := range conflicts {
for _, name := range c.Targets {
explained[name] = true
}
}
rest := 0
for _, name := range config.DetectPathOverlap(targets, isProject) {
if !explained[name] {
rest++
}
}
if conflicts == nil {
conflicts = []config.SkillsFolderConflict{}
}
return conflicts, rest
}
// projectTargets returns the targets of the project declared under root.
func (s *Server) projectTargets(root string) map[string]config.TargetConfig {
abs := filepath.Clean(config.ExpandPath(root))
targets := map[string]config.TargetConfig{}
for name, target := range s.cfg.Targets {
if target.ProjectRoot() != "" && filepath.Clean(target.ProjectRoot()) == abs {
targets[name] = target
}
}
return targets
}
// syncResources links skills and agents (kind "" means both) into every target,
// or only the targets of the project declared under project, and logs the sync.
// On failure it returns the HTTP status to report; agent failures only add
// warnings. Callers must hold s.mu.
func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, project string) (*syncOutcome, int, error) {
targets := s.cfg.Targets
if project != "" {
targets = s.projectTargets(project)
}
globalMode := s.cfg.Mode
if globalMode == "" {
globalMode = "merge"
}
// Pre-check warnings via shared config validation
warnings, validErr := config.ValidateConfig(s.cfg)
if validErr != nil {
return nil, http.StatusBadRequest, validErr
}
conflicts, overlap := folderConflicts(s.cfg.Targets, s.IsProjectMode())
if !dryRun {
warnings = append(warnings, s.backupBeforeSync(targets, kind != kindAgent, kind != kindSkill)...)
}
results := make([]syncTargetResult, 0)
skillFailed := 0
var ignoreStats *skillignore.IgnoreStats
var allSkills []ssync.DiscoveredSkill
ignorePatterns := ssync.EffectiveFileIgnorePatterns(s.cfg.Ignore)
// Skill sync (skip when kind == "agent")
if kind != kindAgent {
var err error
allSkills, ignoreStats, err = ssync.DiscoverSourceSkillsWithStatsAndContext(s.cfg.EffectiveSkillsSource())
if err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to discover skills: %w", err)
}
if len(allSkills) == 0 {
warnings = append(warnings, "source directory is empty (0 skills)")
}
// Registry entries are managed by install/uninstall, not sync.
// Sync only manages symlinks — it must not prune registry entries
// for installed skills whose files may be missing from disk.
// A failed target adds a warning and no results row; the rest still sync.
var firstErr error
var firstFailed string
failTarget := func(name string, err error) {
skillFailed++
if firstErr == nil {
firstErr, firstFailed = fmt.Errorf("sync failed for %s: %w", name, err), name
}
warnings = append(warnings, name+": sync failed: "+err.Error())
}
skillRan := 0
for name, target := range targets {
sc := target.SkillsConfig()
if !sc.IsEnabled() {
continue // skills off: agents, extras and MCP below still run
}
skillRan++
mode := sc.Mode
if mode == "" {
mode = globalMode
}
res := syncTargetResult{
Target: name,
Linked: make([]string, 0),
Updated: make([]string, 0),
Skipped: make([]string, 0),
Pruned: make([]string, 0),
}
switch mode {
case "merge":
mergeResult, err := ssync.SyncTargetMergeWithSkills(name, target, allSkills, s.cfg.EffectiveSkillsSource(), dryRun, force, s.projectRoot)
if err != nil {
failTarget(name, err)
continue
}
res.Linked = mergeResult.Linked
res.Updated = mergeResult.Updated
res.Skipped = mergeResult.Skipped
res.DirCreated = mergeResult.DirCreated
pruneResult, err := ssync.PruneOrphanLinksWithSkills(ssync.PruneOptions{
TargetPath: sc.Path, SourcePath: s.cfg.EffectiveSkillsSource(), Skills: allSkills,
Include: sc.Include, Exclude: sc.Exclude, TargetNaming: sc.TargetNaming, TargetName: name,
DryRun: dryRun, Force: force,
})
warnings = append(warnings, skillPruneWarnings(name, pruneResult, err)...)
if pruneResult != nil {
res.Pruned = pruneResult.Removed
}
case "copy":
copyResult, err := ssync.SyncTargetCopyWithSkillsOptions(name, target, allSkills, s.cfg.EffectiveSkillsSource(), dryRun, force, nil, ssync.CopyOptions{IgnorePatterns: ignorePatterns})
if err != nil {
failTarget(name, err)
continue
}
res.Linked = copyResult.Copied
res.Updated = copyResult.Updated
res.Skipped = copyResult.Skipped
res.DirCreated = copyResult.DirCreated
pruneResult, err := ssync.PruneOrphanCopiesWithSkills(sc.Path, allSkills, sc.Include, sc.Exclude, name, sc.TargetNaming, dryRun)
warnings = append(warnings, skillPruneWarnings(name, pruneResult, err)...)
if pruneResult != nil {
res.Pruned = pruneResult.Removed
}
default:
// A symlink pointing elsewhere is replaced only with force, as in the CLI.
if ssync.CheckStatus(sc.Path, s.cfg.EffectiveSkillsSource()) == ssync.StatusConflict {
if !force {
link, err := utils.ResolveLinkTarget(sc.Path)
if err != nil {
link = "(unable to resolve target)"
}
failTarget(name, fmt.Errorf("conflict - symlink points to %s (use --force to override)", link))
continue
}
if !dryRun {
os.Remove(sc.Path)
}
}
err := ssync.SyncTarget(name, target, s.cfg.EffectiveSkillsSource(), dryRun, s.projectRoot)
if err != nil {
failTarget(name, err)
continue
}
res.Linked = []string{"(symlink mode)"}
}
results = append(results, res)
}
// Every skill target failing is an error, so single-target setups
// still see one.
if skillRan > 0 && skillFailed == skillRan {
errArgs := map[string]any{
"targets_total": len(targets),
"targets_failed": skillFailed,
"target": firstFailed,
"dry_run": dryRun,
"force": force,
"scope": "ui",
}
if project != "" {
errArgs["project"] = project
}
s.writeOpsLog("sync", "error", start, errArgs, firstErr.Error())
return nil, http.StatusInternalServerError, firstErr
}
// Clean skillshare entries left in also_scans dirs a target no longer
// writes to, so the runtime stops seeing every skill twice.
if s.IsProjectMode() {
warnings = append(warnings, ssync.CleanMovedProjectDirs(
s.projectRoot, s.cfg.EffectiveSkillsSource(),
ssync.MovedProjectTargets(s.projectCfg, s.cfg.Targets), dryRun)...)
}
}
// Agent sync (skip when kind == "skill")
if kind != kindSkill {
agentsSource := s.agentsSource()
if info, err := os.Stat(agentsSource); err == nil && info.IsDir() {
agents := discoverActiveAgents(agentsSource)
builtinAgents := s.builtinAgentTargets()
agentTargets := make([]ssync.AgentTarget, 0, len(targets))
for name, target := range targets {
agentTargets = append(agentTargets, ssync.AgentTarget{
Name: name,
Path: resolveAgentPath(target, builtinAgents, name, s.IsProjectMode()),
Config: target.AgentsConfig(),
})
}
runs := ssync.RunAgentSync(agentTargets, agents, ssync.AgentRunOptions{
Source: agentsSource,
ProjectRoot: s.projectRoot,
DryRun: dryRun,
Force: force,
ResolveExtension: s.resolveExtensionSpec,
})
for _, run := range runs {
name := run.Name
if run.Path == "" {
continue
}
if run.Err != nil {
warnings = append(warnings, "agent sync failed for "+name+": "+run.Err.Error())
continue
}
warnings = append(warnings, run.Warnings...)
if run.SyncErr != nil {
warnings = append(warnings, "agent sync failed for "+name+": "+run.SyncErr.Error())
}
if !run.Synced {
continue
}
// Find or create result entry for this target
idx := -1
for i := range results {
if results[i].Target == name {
idx = i
break
}
}
if idx < 0 && (len(run.Linked) > 0 || len(run.Updated) > 0 || len(run.Skipped) > 0 || len(run.Pruned) > 0) {
results = append(results, syncTargetResult{
Target: name,
Linked: make([]string, 0),
Updated: make([]string, 0),
Skipped: make([]string, 0),
Pruned: make([]string, 0),
})
idx = len(results) - 1
}
if idx >= 0 {
results[idx].Linked = append(results[idx].Linked, run.Linked...)
results[idx].Updated = append(results[idx].Updated, run.Updated...)
results[idx].Skipped = append(results[idx].Skipped, run.Skipped...)
results[idx].Pruned = append(results[idx].Pruned, run.Pruned...)
}
}
}
}
// Log the sync operation
logArgs := map[string]any{
"targets_total": len(results) + skillFailed,
"targets_failed": skillFailed,
"dry_run": dryRun,
"force": force,
"kind": kind,
"scope": "ui",
}
if project != "" {
logArgs["project"] = project
}
status := "ok"
if skillFailed > 0 {
status = "partial"
}
s.writeOpsLog("sync", status, start, logArgs, "")
return &syncOutcome{results: results, warnings: warnings, folderConflicts: conflicts, pathOverlap: overlap, skills: allSkills, ignoreStats: ignoreStats}, 0, nil
}
// skillPruneWarnings reports a skills prune the way the CLI does: a failure
// as "<target>: prune failed: <err>", then the prune's own warnings.
func skillPruneWarnings(name string, pruneResult *ssync.PruneResult, err error) []string {
var warnings []string
if err != nil {
warnings = append(warnings, fmt.Sprintf("%s: prune failed: %v", name, err))
}
if pruneResult != nil {
warnings = append(warnings, pruneResult.Warnings...)
}
return warnings
}
// backupBeforeSync snapshots the target folders a sync may overwrite, as the
// CLI does, then trims old snapshots. Project mode backs up agents only, like
// `sync -p`. Failures come back as warnings. Callers must hold s.mu.
func (s *Server) backupBeforeSync(targets map[string]config.TargetConfig, skills, agents bool) []string {
var warnings []string
dir := backup.BackupDir()
if s.IsProjectMode() {
dir, skills = backup.ProjectBackupDir(s.projectRoot), false
}
snapshot := func(entry, path string) {
if _, err := backup.CreateInDir(dir, entry, path); err != nil {
warnings = append(warnings, "backup failed for "+entry+": "+err.Error())
}
}
builtinAgents := s.builtinAgentTargets()
for name, target := range targets {
if skills && target.SkillsConfig().IsEnabled() {
snapshot(name, target.SkillsConfig().Path)
}
if p := resolveAgentPath(target, builtinAgents, name, s.IsProjectMode()); agents && p != "" {
snapshot(name+"-agents", resolveExtrasTargetPath(s.projectRoot, p))
}
}
if _, err := backup.CleanupInDir(dir, backup.DefaultCleanupConfig()); err != nil {
warnings = append(warnings, "backup cleanup failed: "+err.Error())
}
return warnings
}
func (s *Server) computeContextCost(skills []ssync.DiscoveredSkill) map[string]any {
type tokenPair struct{ always, onDemand int }
groupMap := make(map[tokenPair][]string)
type skillToken struct {
name string
descTokens int
bodyTokens int
}
var maxAlways, maxOnDemand int
var worstAlwaysSkills, worstOnDemandSkills []skillToken
var worstAlwaysTarget, worstOnDemandTarget string
cfgMode := s.cfg.Mode
if cfgMode == "" {
cfgMode = "merge"
}
for name, target := range s.cfg.Targets {
filtered, fErr := ssync.TargetSkills(name, target, cfgMode, s.cfg.EffectiveSkillsSource(), skills)
if fErr != nil {
filtered = skills
}
var at, ot int
var perSkill []skillToken
for _, sk := range filtered {
at += sk.DescTokens
ot += sk.BodyTokens
perSkill = append(perSkill, skillToken{
name: sk.FlatName,
descTokens: sk.DescTokens,
bodyTokens: sk.BodyTokens,
})
}
groupMap[tokenPair{at, ot}] = append(groupMap[tokenPair{at, ot}], name)
if at > maxAlways {
maxAlways = at
worstAlwaysSkills = perSkill
worstAlwaysTarget = name
} else if at > 0 && at == maxAlways && name < worstAlwaysTarget {
worstAlwaysTarget = name
worstAlwaysSkills = perSkill
}
if ot > maxOnDemand {
maxOnDemand = ot
worstOnDemandSkills = perSkill
worstOnDemandTarget = name
} else if ot > 0 && ot == maxOnDemand && name < worstOnDemandTarget {
worstOnDemandTarget = name
worstOnDemandSkills = perSkill
}
}
type group struct {
Targets []string `json:"targets"`
AlwaysLoadedTokens int `json:"always_loaded_tokens"`
OnDemandTokens int `json:"on_demand_tokens"`
}
groups := make([]group, 0, len(groupMap))
for pair, names := range groupMap {
sort.Strings(names)
groups = append(groups, group{
Targets: names,
AlwaysLoadedTokens: pair.always,
OnDemandTokens: pair.onDemand,
})
}
sort.Slice(groups, func(i, j int) bool { return groups[i].Targets[0] < groups[j].Targets[0] })
budget := s.cfg.ContextBudget
if s.IsProjectMode() && s.projectCfg != nil {
budget = s.projectCfg.ContextBudget
}
result := map[string]any{"groups": groups}
type offender struct {
Name string `json:"name"`
Tokens int `json:"tokens"`
}
type warning struct {
Type string `json:"type"`
Target string `json:"target"`
Actual int `json:"actual"`
Budget int `json:"budget"`
TopOffenders []offender `json:"top_offenders"`
}
topN := func(perSkill []skillToken, n int, byDesc bool) []offender {
type pair struct {
name string
tokens int
}
pairs := make([]pair, 0, len(perSkill))
for _, sk := range perSkill {
t := sk.bodyTokens
if byDesc {
t = sk.descTokens
}
if t > 0 {
pairs = append(pairs, pair{sk.name, t})
}
}
sort.Slice(pairs, func(i, j int) bool { return pairs[i].tokens > pairs[j].tokens })
if len(pairs) > n {
pairs = pairs[:n]
}
out := make([]offender, len(pairs))
for i, p := range pairs {
out[i] = offender{Name: p.name, Tokens: p.tokens}
}
return out
}
var warns []warning
if t := budget.AlwaysLoadedThreshold(); t > 0 && maxAlways > t {
warns = append(warns, warning{
Type: "always_loaded", Target: worstAlwaysTarget,
Actual: maxAlways, Budget: t,
TopOffenders: topN(worstAlwaysSkills, 3, true),
})
}
if t := budget.OnDemandThreshold(); t > 0 && maxOnDemand > t {
warns = append(warns, warning{
Type: "on_demand", Target: worstOnDemandTarget,
Actual: maxOnDemand, Budget: t,
TopOffenders: topN(worstOnDemandSkills, 3, false),
})
}
if len(warns) > 0 {
result["warnings"] = warns
}
return result
}
type diffItem struct {
Skill string `json:"skill"`
Action string `json:"action"` // "link", "update", "skip", "prune", "local"
Reason string `json:"reason"` // human-readable description
Kind string `json:"kind,omitempty"`
}
type diffTarget struct {
Target string `json:"target"`
Items []diffItem `json:"items"`
SkippedCount int `json:"skippedCount,omitempty"`
CollisionCount int `json:"collisionCount,omitempty"`
}
func (s *Server) handleDiff(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before slow I/O.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
agentsSource := s.agentsSource()
globalMode := s.cfg.Mode
ignorePatterns := ssync.EffectiveFileIgnorePatterns(s.cfg.Ignore)
targets := s.cloneTargets()
conflicts, _ := folderConflicts(targets, s.IsProjectMode())
s.mu.RUnlock()
if globalMode == "" {
globalMode = "merge"
}
filterTarget := r.URL.Query().Get("target")
discovered, ignoreStats, err := ssync.DiscoverSourceSkillsWithStats(source)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
diffs := make([]diffTarget, 0)
for name, target := range targets {
if filterTarget != "" && filterTarget != name {
continue
}
diffs = append(diffs, s.computeTargetDiff(name, target, discovered, globalMode, source, ignorePatterns))
}
diffs = s.appendAgentDiffs(diffs, targets, agentsSource, filterTarget)
resp := map[string]any{"diffs": diffs, "folder_conflicts": conflicts}
maps.Copy(resp, ignorePayload(ignoreStats))
maps.Copy(resp, agentIgnorePayload(agentsSource, nil))
writeJSON(w, resp)
}