diff --git a/internal/server/handler_sync.go b/internal/server/handler_sync.go index 2192a4769..f635c104d 100644 --- a/internal/server/handler_sync.go +++ b/internal/server/handler_sync.go @@ -170,6 +170,7 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec } results := make([]syncTargetResult, 0) + skillFailed := 0 var ignoreStats *skillignore.IgnoreStats var allSkills []ssync.DiscoveredSkill @@ -191,11 +192,23 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec // 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 @@ -209,24 +222,12 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec Pruned: make([]string, 0), } - syncErrArgs := map[string]any{ - "targets_total": len(targets), - "targets_failed": 1, - "target": name, - "dry_run": dryRun, - "force": force, - "scope": "ui", - } - if project != "" { - syncErrArgs["project"] = project - } - switch mode { case "merge": mergeResult, err := ssync.SyncTargetMergeWithSkills(name, target, allSkills, s.cfg.EffectiveSkillsSource(), dryRun, force, s.projectRoot) if err != nil { - s.writeOpsLog("sync", "error", start, syncErrArgs, err.Error()) - return nil, http.StatusInternalServerError, fmt.Errorf("sync failed for %s: %w", name, err) + failTarget(name, err) + continue } res.Linked = mergeResult.Linked res.Updated = mergeResult.Updated @@ -245,8 +246,8 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec case "copy": copyResult, err := ssync.SyncTargetCopyWithSkillsOptions(name, target, allSkills, s.cfg.EffectiveSkillsSource(), dryRun, force, nil, ssync.CopyOptions{IgnorePatterns: ignorePatterns}) if err != nil { - s.writeOpsLog("sync", "error", start, syncErrArgs, err.Error()) - return nil, http.StatusInternalServerError, fmt.Errorf("sync failed for %s: %w", name, err) + failTarget(name, err) + continue } res.Linked = copyResult.Copied res.Updated = copyResult.Updated @@ -261,8 +262,8 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec default: err := ssync.SyncTarget(name, target, s.cfg.EffectiveSkillsSource(), dryRun, s.projectRoot) if err != nil { - s.writeOpsLog("sync", "error", start, syncErrArgs, err.Error()) - return nil, http.StatusInternalServerError, fmt.Errorf("sync failed for %s: %w", name, err) + failTarget(name, err) + continue } res.Linked = []string{"(symlink mode)"} } @@ -270,6 +271,24 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec 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() { @@ -350,8 +369,8 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec // Log the sync operation logArgs := map[string]any{ - "targets_total": len(results), - "targets_failed": 0, + "targets_total": len(results) + skillFailed, + "targets_failed": skillFailed, "dry_run": dryRun, "force": force, "kind": kind, @@ -360,7 +379,11 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec if project != "" { logArgs["project"] = project } - s.writeOpsLog("sync", "ok", start, logArgs, "") + 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 } diff --git a/internal/server/handler_sync_test.go b/internal/server/handler_sync_test.go index c2bc18f6b..8c8a8201e 100644 --- a/internal/server/handler_sync_test.go +++ b/internal/server/handler_sync_test.go @@ -496,3 +496,73 @@ func TestHandleSync_ReportsFolderConflicts(t *testing.T) { } } } + +func TestHandleSync_SkillTargetFailureKeepsSyncingOthers(t *testing.T) { + s, src, _ := newAgentSyncServer(t) + addSkill(t, src, "alpha") + s.cfg.Targets["broken"] = config.TargetConfig{ + Skills: &config.ResourceTargetConfig{Path: filepath.Join(t.TempDir(), "broken-skills"), Include: []string{"["}}, + } + s.cfg.Targets["claude"] = config.TargetConfig{ + Skills: &config.ResourceTargetConfig{Path: filepath.Join(t.TempDir(), "claude-skills")}, + Agents: &config.ResourceTargetConfig{Path: filepath.Join(t.TempDir(), "claude-agents")}, + } + if err := s.cfg.Save(); err != nil { + t.Fatal(err) + } + + req := httptest.NewRequest(http.MethodPost, "/api/sync", strings.NewReader(`{}`)) + rr := httptest.NewRecorder() + s.handler.ServeHTTP(rr, req) + + if rr.Code != http.StatusOK { + t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String()) + } + var resp struct { + Results []syncTargetResult `json:"results"` + Warnings []string `json:"warnings"` + } + if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil { + t.Fatal(err) + } + if len(resp.Results) != 1 || resp.Results[0].Target != "claude" || !slices.Equal(resp.Results[0].Linked, []string{"alpha", "tutor.md"}) { + t.Fatalf("expected only the claude row with alpha and tutor.md, got %+v", resp.Results) + } + if !slices.ContainsFunc(resp.Warnings, func(w string) bool { + return strings.HasPrefix(w, "broken: sync failed: ") + }) { + t.Fatalf("expected sync failed warning for broken, got %v", resp.Warnings) + } + entries, err := oplog.Read(config.ConfigPath(), oplog.OpsFile, 1) + if err != nil || len(entries) != 1 { + t.Fatalf("read ops log: %v (%d entries)", err, len(entries)) + } + if e := entries[0]; e.Status != "partial" || e.Args["targets_failed"] != float64(1) { + t.Fatalf("expected partial sync with targets_failed 1, got status %q args %v", e.Status, e.Args) + } +} + +func TestHandleSync_EverySkillTargetFailingIsAnError(t *testing.T) { + s, _ := newTestServer(t) + s.cfg.Targets["broken"] = config.TargetConfig{ + Skills: &config.ResourceTargetConfig{Path: filepath.Join(t.TempDir(), "broken-skills"), Include: []string{"["}}, + } + if err := s.cfg.Save(); err != nil { + t.Fatal(err) + } + + req := httptest.NewRequest(http.MethodPost, "/api/sync", strings.NewReader(`{}`)) + rr := httptest.NewRecorder() + s.handler.ServeHTTP(rr, req) + + if rr.Code != http.StatusInternalServerError || !strings.Contains(rr.Body.String(), "sync failed for broken") { + t.Fatalf("expected 500 naming broken, got %d: %s", rr.Code, rr.Body.String()) + } + entries, err := oplog.Read(config.ConfigPath(), oplog.OpsFile, 1) + if err != nil || len(entries) != 1 { + t.Fatalf("read ops log: %v (%d entries)", err, len(entries)) + } + if e := entries[0]; e.Status != "error" || e.Args["targets_failed"] != float64(1) { + t.Fatalf("expected error sync with targets_failed 1, got status %q args %v", e.Status, e.Args) + } +}