From 097d2c02574566235bbab2fc914f8aaf7ba39843 Mon Sep 17 00:00:00 2001 From: Willie Date: Thu, 1 Oct 2026 21:55:41 +0800 Subject: [PATCH] fix(sync): keep unreadable links and report failed agent prune removals prunableLink treated any stat failure as a broken link, so a live link whose referent was temporarily unreadable (for example permission denied) was deleted as if it pointed nowhere. Only a missing referent now counts as broken. PruneOrphanAgentLinks and PruneOrphanAgentCopies ignored os.Remove errors and still listed the entry as removed, so sync reported a prune that did not happen. Failed removals are now returned as errors, which the agent runner already surfaces as warnings. --- internal/sync/agent_sync.go | 21 ++++++--- internal/sync/agent_sync_test.go | 73 ++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/internal/sync/agent_sync.go b/internal/sync/agent_sync.go index c77016c86..5cc4aef8f 100644 --- a/internal/sync/agent_sync.go +++ b/internal/sync/agent_sync.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "io/fs" "os" "path/filepath" "strings" @@ -91,8 +92,10 @@ func linkResolvesToSource(absLink, absSource string) bool { // broken, or it resolves inside sourceDir. Live links to other locations, which // skillshare never creates, survive. func prunableLink(path, sourceDir string) bool { - if _, err := os.Stat(path); err != nil { + if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) { return true + } else if err != nil { + return false // e.g. permission denied: not proven broken } absDir, err := filepath.Abs(sourceDir) if err != nil { @@ -526,6 +529,7 @@ func PruneOrphanAgentLinks(targetDir, sourceDir string, agents []resource.Discov } } + var errs []error for _, entry := range entries { name := entry.Name() @@ -547,12 +551,15 @@ func PruneOrphanAgentLinks(targetDir, sourceDir string, agents []resource.Discov } if !dryRun { - os.Remove(filepath.Join(targetDir, name)) + if err := os.Remove(filepath.Join(targetDir, name)); err != nil { + errs = append(errs, fmt.Errorf("failed to remove orphaned link %s: %w", name, err)) + continue + } } removed = append(removed, name) } - return removed, nil + return removed, errors.Join(errs...) } // PruneOrphanAgentCopies removes copied .md files in targetDir that don't @@ -577,6 +584,7 @@ func PruneOrphanAgentCopies(targetDir string, agents []resource.DiscoveredResour outSuffix = "." + strings.ToLower(outputExt) } + var errs []error for _, entry := range entries { name := entry.Name() lower := strings.ToLower(name) @@ -595,12 +603,15 @@ func PruneOrphanAgentCopies(targetDir string, agents []resource.DiscoveredResour } if !dryRun { - os.Remove(filepath.Join(targetDir, name)) + if err := os.Remove(filepath.Join(targetDir, name)); err != nil { + errs = append(errs, fmt.Errorf("failed to remove orphaned copy %s: %w", name, err)) + continue + } } removed = append(removed, name) } - return removed, nil + return removed, errors.Join(errs...) } // FindLocalAgents finds local (non-symlinked) agent files in a target directory. diff --git a/internal/sync/agent_sync_test.go b/internal/sync/agent_sync_test.go index 369946487..e2db06573 100644 --- a/internal/sync/agent_sync_test.go +++ b/internal/sync/agent_sync_test.go @@ -3,6 +3,7 @@ package sync import ( "os" "path/filepath" + "runtime" "testing" "skillshare/internal/resource" @@ -253,6 +254,61 @@ func TestPruneOrphanAgentLinks_RelativeLinkUnderSymlinkedTarget(t *testing.T) { } } +// skipUnlessPermissionsEnforced skips where chmod cannot deny access. +func skipUnlessPermissionsEnforced(t *testing.T) { + t.Helper() + if runtime.GOOS == "windows" || os.Geteuid() == 0 { + t.Skip("requires POSIX permissions enforced for a non-root user") + } +} + +// A link whose referent cannot be read is not proven broken, so it stays. +func TestPruneOrphanAgentLinks_KeepsLinkItCannotStat(t *testing.T) { + skipUnlessPermissionsEnforced(t) + sourceDir := t.TempDir() + targetDir := t.TempDir() + + locked := filepath.Join(t.TempDir(), "locked") + os.MkdirAll(locked, 0755) + os.WriteFile(filepath.Join(locked, "mine.md"), []byte("# Mine"), 0644) + link := filepath.Join(targetDir, "mine.md") + os.Symlink(filepath.Join(locked, "mine.md"), link) + os.Chmod(locked, 0) + t.Cleanup(func() { os.Chmod(locked, 0755) }) + + removed, err := PruneOrphanAgentLinks(targetDir, sourceDir, nil, false) + if err != nil { + t.Fatalf("PruneOrphanAgentLinks: %v", err) + } + if len(removed) != 0 { + t.Errorf("expected unreadable link kept, removed: %v", removed) + } + if _, err := os.Lstat(link); err != nil { + t.Error("unreadable link should not be removed") + } +} + +// A failed removal is an error, not a reported removal. +func TestPruneOrphanAgentLinks_ReportsRemoveFailure(t *testing.T) { + skipUnlessPermissionsEnforced(t) + sourceDir := t.TempDir() + targetDir := t.TempDir() + + orphan := filepath.Join(sourceDir, "orphan.md") + os.WriteFile(orphan, []byte("# Orphan"), 0644) + os.Symlink(orphan, filepath.Join(targetDir, "orphan.md")) + os.Chmod(targetDir, 0555) + t.Cleanup(func() { os.Chmod(targetDir, 0755) }) + + removed, err := PruneOrphanAgentLinks(targetDir, sourceDir, nil, false) + if err == nil { + t.Error("expected an error for the failed removal") + } + if len(removed) != 0 { + t.Errorf("failed removal reported as removed: %v", removed) + } +} + func TestPruneOrphanAgentLinks_NonExistentDir(t *testing.T) { removed, err := PruneOrphanAgentLinks("/nonexistent/path", "", nil, false) if err != nil { @@ -557,6 +613,23 @@ func TestPruneOrphanAgentCopies(t *testing.T) { } } +func TestPruneOrphanAgentCopies_ReportsRemoveFailure(t *testing.T) { + skipUnlessPermissionsEnforced(t) + targetDir := t.TempDir() + + os.WriteFile(filepath.Join(targetDir, "orphan.md"), []byte("# Orphan"), 0644) + os.Chmod(targetDir, 0555) + t.Cleanup(func() { os.Chmod(targetDir, 0755) }) + + removed, err := PruneOrphanAgentCopies(targetDir, nil, "", false) + if err == nil { + t.Error("expected an error for the failed removal") + } + if len(removed) != 0 { + t.Errorf("failed removal reported as removed: %v", removed) + } +} + // --- Dispatch tests --- func TestSyncAgents_DefaultIsMerge(t *testing.T) {