mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
fix(sync): prune only agent copies skillshare wrote in copy mode
Copy and extension modes pruned every unexpected .md (or output-extension) file in the agents target, so agent files a user kept there were deleted on the next sync, unlike skills copy mode, which prunes only manifest entries. Copy and transform outputs are now tracked with the same checksum manifest the merge fallback already uses, and prune removes only tracked copies the user has not edited. Existing copies are adopted while their agent is still synced, and an untracked pre-transform .md that still matches its agent is treated as a stale copy, so switching to an extension keeps cleaning it up. Copies already orphaned before upgrading are kept and must be removed by hand. copyTracker.pruneOrphans now continues past a failed removal and joins the errors, so one locked file no longer leaves the other orphans behind.
This commit is contained in:
+48
-44
@@ -369,6 +369,9 @@ func syncAgentsCopy(agents []resource.DiscoveredResource, targetDir string, dryR
|
||||
}
|
||||
}
|
||||
|
||||
// Copies are tracked so prune removes only what skillshare wrote.
|
||||
copies := loadCopyTracker(targetDir)
|
||||
|
||||
for _, agent := range agents {
|
||||
targetPath := filepath.Join(targetDir, agent.FlatName)
|
||||
|
||||
@@ -387,6 +390,7 @@ func syncAgentsCopy(agents []resource.DiscoveredResource, targetDir string, dryR
|
||||
if err := os.WriteFile(targetPath, srcData, 0644); err != nil {
|
||||
return nil, fmt.Errorf("failed to write %s: %w", agent.FlatName, err)
|
||||
}
|
||||
copies.record(agent.FlatName)
|
||||
}
|
||||
result.Updated = append(result.Updated, agent.FlatName)
|
||||
continue
|
||||
@@ -396,6 +400,9 @@ func syncAgentsCopy(agents []resource.DiscoveredResource, targetDir string, dryR
|
||||
// File exists — check if content matches
|
||||
tgtData, readErr := os.ReadFile(targetPath)
|
||||
if readErr == nil && string(tgtData) == string(srcData) && !force {
|
||||
if !dryRun {
|
||||
copies.record(agent.FlatName) // adopts copies made before tracking
|
||||
}
|
||||
result.Linked = append(result.Linked, agent.FlatName)
|
||||
continue
|
||||
}
|
||||
@@ -404,6 +411,7 @@ func syncAgentsCopy(agents []resource.DiscoveredResource, targetDir string, dryR
|
||||
if err := os.WriteFile(targetPath, srcData, 0644); err != nil {
|
||||
return nil, fmt.Errorf("failed to write %s: %w", agent.FlatName, err)
|
||||
}
|
||||
copies.record(agent.FlatName)
|
||||
}
|
||||
result.Updated = append(result.Updated, agent.FlatName)
|
||||
} else {
|
||||
@@ -412,11 +420,17 @@ func syncAgentsCopy(agents []resource.DiscoveredResource, targetDir string, dryR
|
||||
if err := os.WriteFile(targetPath, srcData, 0644); err != nil {
|
||||
return nil, fmt.Errorf("failed to write %s: %w", agent.FlatName, err)
|
||||
}
|
||||
copies.record(agent.FlatName)
|
||||
}
|
||||
result.Linked = append(result.Linked, agent.FlatName)
|
||||
}
|
||||
}
|
||||
|
||||
if !dryRun {
|
||||
if err := copies.save(); err != nil {
|
||||
return nil, fmt.Errorf("failed to update agent manifest: %w", err)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -440,6 +454,8 @@ func SyncAgentsTransform(agents []resource.DiscoveredResource, sourceDir, target
|
||||
}
|
||||
}
|
||||
}
|
||||
// Outputs are tracked so prune removes only what skillshare wrote.
|
||||
copies := loadCopyTracker(targetDir)
|
||||
|
||||
for _, agent := range agents {
|
||||
name := ApplyOutputExt(agent.FlatName, spec.OutputExt)
|
||||
@@ -469,11 +485,13 @@ func SyncAgentsTransform(agents []resource.DiscoveredResource, sourceDir, target
|
||||
// Keeping the old output would leave an unconverted agent active.
|
||||
if readErr == nil {
|
||||
os.Remove(tgtFile)
|
||||
copies.forget(name)
|
||||
}
|
||||
errs = append(errs, fmt.Errorf("%s: %w", agent.FlatName, err))
|
||||
continue
|
||||
}
|
||||
if readErr == nil && bytes.Equal(existing, out) && !force {
|
||||
copies.record(name) // adopts outputs made before tracking
|
||||
result.Linked = append(result.Linked, name)
|
||||
continue
|
||||
}
|
||||
@@ -484,6 +502,7 @@ func SyncAgentsTransform(agents []resource.DiscoveredResource, sourceDir, target
|
||||
errs = append(errs, fmt.Errorf("%s: write target: %w", agent.FlatName, err))
|
||||
continue
|
||||
}
|
||||
copies.record(name)
|
||||
if readErr == nil {
|
||||
result.Updated = append(result.Updated, name)
|
||||
} else {
|
||||
@@ -491,6 +510,11 @@ func SyncAgentsTransform(agents []resource.DiscoveredResource, sourceDir, target
|
||||
}
|
||||
}
|
||||
|
||||
if !dryRun {
|
||||
if err := copies.save(); err != nil {
|
||||
errs = append(errs, fmt.Errorf("failed to update agent manifest: %w", err))
|
||||
}
|
||||
}
|
||||
return result, errors.Join(errs...)
|
||||
}
|
||||
|
||||
@@ -519,17 +543,17 @@ func PruneOrphanAgentLinks(targetDir, sourceDir string, agents []resource.Discov
|
||||
|
||||
// Copies stand in for links when file links are unavailable.
|
||||
copies := loadCopyTracker(targetDir)
|
||||
var errs []error
|
||||
removed, err = copies.pruneOrphans(expected, dryRun)
|
||||
if err != nil {
|
||||
return removed, err
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if !dryRun {
|
||||
if err := copies.save(); err != nil {
|
||||
return removed, fmt.Errorf("failed to update agent manifest: %w", err)
|
||||
return removed, errors.Join(append(errs, fmt.Errorf("failed to update agent manifest: %w", err))...)
|
||||
}
|
||||
}
|
||||
|
||||
var errs []error
|
||||
for _, entry := range entries {
|
||||
name := entry.Name()
|
||||
|
||||
@@ -562,56 +586,36 @@ func PruneOrphanAgentLinks(targetDir, sourceDir string, agents []resource.Discov
|
||||
return removed, errors.Join(errs...)
|
||||
}
|
||||
|
||||
// PruneOrphanAgentCopies removes copied .md files in targetDir that don't
|
||||
// PruneOrphanAgentCopies removes tracked copies in targetDir that don't
|
||||
// correspond to any discovered agent. For copy mode only. outputExt is the
|
||||
// extension a transform renames outputs to ("" keeps .md); files with it are
|
||||
// pruned too, and a stale .md copy of a transformed agent counts as an orphan.
|
||||
// extension a transform renames outputs to ("" keeps .md); a stale tracked .md
|
||||
// copy of a transformed agent counts as an orphan. Files skillshare never wrote,
|
||||
// and copies edited since, are preserved.
|
||||
func PruneOrphanAgentCopies(targetDir string, agents []resource.DiscoveredResource, outputExt string, dryRun bool) (removed []string, _ error) {
|
||||
entries, err := os.ReadDir(targetDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("failed to read agent target directory: %w", err)
|
||||
}
|
||||
|
||||
expected := make(map[string]bool, len(agents))
|
||||
for _, a := range agents {
|
||||
expected[ApplyOutputExt(a.FlatName, outputExt)] = true
|
||||
}
|
||||
outSuffix := ""
|
||||
if outputExt != "" {
|
||||
outSuffix = "." + strings.ToLower(outputExt)
|
||||
|
||||
copies := loadCopyTracker(targetDir)
|
||||
// A copy made before tracking under the pre-transform name is still
|
||||
// identical to its agent; adopt it so the renamed output replaces it.
|
||||
for _, a := range agents {
|
||||
if ApplyOutputExt(a.FlatName, outputExt) == a.FlatName {
|
||||
continue
|
||||
}
|
||||
stale := filepath.Join(targetDir, a.FlatName)
|
||||
if info, err := os.Lstat(stale); err == nil && info.Mode().IsRegular() && contentEqual(a.AbsPath, stale) {
|
||||
copies.record(a.FlatName)
|
||||
}
|
||||
}
|
||||
|
||||
var errs []error
|
||||
for _, entry := range entries {
|
||||
name := entry.Name()
|
||||
lower := strings.ToLower(name)
|
||||
|
||||
if !strings.HasSuffix(lower, ".md") && (outSuffix == "" || !strings.HasSuffix(lower, outSuffix)) {
|
||||
continue
|
||||
removed, err := copies.pruneOrphans(expected, dryRun)
|
||||
if !dryRun {
|
||||
if saveErr := copies.save(); saveErr != nil {
|
||||
err = errors.Join(err, fmt.Errorf("failed to update agent manifest: %w", saveErr))
|
||||
}
|
||||
|
||||
// Skip conventional excludes (user might have README.md etc.)
|
||||
if resource.ConventionalExcludes[name] {
|
||||
continue
|
||||
}
|
||||
|
||||
if expected[name] {
|
||||
continue
|
||||
}
|
||||
|
||||
if !dryRun {
|
||||
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, errors.Join(errs...)
|
||||
return removed, err
|
||||
}
|
||||
|
||||
// FindLocalAgents finds local (non-symlinked) agent files in a target directory.
|
||||
|
||||
@@ -587,18 +587,27 @@ func TestSyncAgents_CopyMode_DifferentContent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPruneOrphanAgentCopies(t *testing.T) {
|
||||
targetDir := t.TempDir()
|
||||
// copyModeFixture syncs the named agents into a fresh target in copy mode.
|
||||
func copyModeFixture(t *testing.T, names ...string) (sourceDir, targetDir string, agents []resource.DiscoveredResource) {
|
||||
t.Helper()
|
||||
sourceDir = t.TempDir()
|
||||
targetDir = t.TempDir()
|
||||
for _, name := range names {
|
||||
src := filepath.Join(sourceDir, name)
|
||||
os.WriteFile(src, []byte("# "+name), 0644)
|
||||
agents = append(agents, resource.DiscoveredResource{FlatName: name, AbsPath: src})
|
||||
}
|
||||
if _, err := SyncAgents(agents, sourceDir, targetDir, "copy", false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return sourceDir, targetDir, agents
|
||||
}
|
||||
|
||||
os.WriteFile(filepath.Join(targetDir, "active.md"), []byte("# Active"), 0644)
|
||||
os.WriteFile(filepath.Join(targetDir, "orphan.md"), []byte("# Orphan"), 0644)
|
||||
func TestPruneOrphanAgentCopies(t *testing.T) {
|
||||
_, targetDir, agents := copyModeFixture(t, "active.md", "orphan.md")
|
||||
os.WriteFile(filepath.Join(targetDir, "README.md"), []byte("# Readme"), 0644) // conventional, skip
|
||||
|
||||
agents := []resource.DiscoveredResource{
|
||||
{FlatName: "active.md"},
|
||||
}
|
||||
|
||||
removed, err := PruneOrphanAgentCopies(targetDir, agents, "", false)
|
||||
removed, err := PruneOrphanAgentCopies(targetDir, agents[:1], "", false)
|
||||
if err != nil {
|
||||
t.Fatalf("PruneOrphanAgentCopies: %v", err)
|
||||
}
|
||||
@@ -613,11 +622,82 @@ func TestPruneOrphanAgentCopies(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// An agent file the user put in a copy-mode target was never copied by skillshare.
|
||||
func TestPruneOrphanAgentCopies_KeepsUntrackedLocalFile(t *testing.T) {
|
||||
_, targetDir, _ := copyModeFixture(t, "active.md")
|
||||
mine := filepath.Join(targetDir, "mine.md")
|
||||
os.WriteFile(mine, []byte("# Mine"), 0644)
|
||||
|
||||
removed, err := PruneOrphanAgentCopies(targetDir, nil, "", false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(removed) != 1 || removed[0] != "active.md" {
|
||||
t.Errorf("expected only the tracked copy removed, got %v", removed)
|
||||
}
|
||||
if _, err := os.Stat(mine); err != nil {
|
||||
t.Error("user's own agent file must be preserved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPruneOrphanAgentCopies_KeepsEditedCopy(t *testing.T) {
|
||||
_, targetDir, _ := copyModeFixture(t, "orphan.md")
|
||||
edited := filepath.Join(targetDir, "orphan.md")
|
||||
os.WriteFile(edited, []byte("# Edited by hand"), 0644)
|
||||
|
||||
if _, err := PruneOrphanAgentCopies(targetDir, nil, "", false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(edited); err != nil {
|
||||
t.Error("an edited copy must be preserved")
|
||||
}
|
||||
}
|
||||
|
||||
// Copies made before tracking existed are adopted while their agent is still synced.
|
||||
func TestSyncAgents_CopyAdoptsExistingCopies(t *testing.T) {
|
||||
sourceDir, targetDir, agents := copyModeFixture(t, "tutor.md")
|
||||
RemoveManifest(targetDir)
|
||||
|
||||
if _, err := SyncAgents(agents, sourceDir, targetDir, "copy", false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
removed, err := PruneOrphanAgentCopies(targetDir, nil, "", false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(removed) != 1 {
|
||||
t.Errorf("expected the adopted copy pruned, got %v", removed)
|
||||
}
|
||||
}
|
||||
|
||||
// A copy made before tracking, still identical to its agent, is a stale
|
||||
// pre-transform output once the target emits a renamed file.
|
||||
func TestPruneOrphanAgentCopies_RemovesUntrackedPreTransformCopy(t *testing.T) {
|
||||
sourceDir, agents, spec := agentTransformFixture(t)
|
||||
spec.OutputExt = "toml"
|
||||
targetDir := t.TempDir()
|
||||
stale := filepath.Join(targetDir, "tutor.md")
|
||||
os.WriteFile(stale, []byte("body"), 0644)
|
||||
mine := filepath.Join(targetDir, "other.md")
|
||||
os.WriteFile(mine, []byte("mine"), 0644)
|
||||
|
||||
if _, err := SyncAgentsTransform(agents, sourceDir, targetDir, "", spec, false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := PruneOrphanAgentCopies(targetDir, agents, "toml", false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(stale); !os.IsNotExist(err) {
|
||||
t.Error("stale tutor.md should be pruned once the target emits tutor.toml")
|
||||
}
|
||||
if _, err := os.Stat(mine); err != nil {
|
||||
t.Error("an unrelated local file must be preserved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPruneOrphanAgentCopies_ReportsRemoveFailure(t *testing.T) {
|
||||
skipUnlessPermissionsEnforced(t)
|
||||
targetDir := t.TempDir()
|
||||
|
||||
os.WriteFile(filepath.Join(targetDir, "orphan.md"), []byte("# Orphan"), 0644)
|
||||
_, targetDir, _ := copyModeFixture(t, "orphan.md")
|
||||
os.Chmod(targetDir, 0555)
|
||||
t.Cleanup(func() { os.Chmod(targetDir, 0755) })
|
||||
|
||||
@@ -978,16 +1058,18 @@ func TestSyncAgentsTransform_FailureRemovesStaleOutput(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPruneOrphanAgentCopies_PrunesTransformedOrphans(t *testing.T) {
|
||||
sourceDir, agents, spec := agentTransformFixture(t)
|
||||
spec.OutputExt = "toml"
|
||||
targetDir := t.TempDir()
|
||||
orphan := filepath.Join(targetDir, "gone.toml")
|
||||
if err := os.WriteFile(orphan, []byte("x"), 0644); err != nil {
|
||||
if _, err := SyncAgentsTransform(agents, sourceDir, targetDir, "", spec, false, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
orphan := filepath.Join(targetDir, "tutor.toml")
|
||||
|
||||
if _, err := PruneOrphanAgentCopies(targetDir, nil, "toml", false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(orphan); !os.IsNotExist(err) {
|
||||
t.Error("orphan gone.toml should be pruned")
|
||||
t.Error("orphan tutor.toml should be pruned")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -100,6 +101,7 @@ func (t *copyTracker) forget(rel string) {
|
||||
// edited ones, and forgets them. It returns the removed names.
|
||||
func (t *copyTracker) pruneOrphans(expected map[string]bool, dryRun bool) ([]string, error) {
|
||||
var removed []string
|
||||
var errs []error
|
||||
for key := range t.m.Managed {
|
||||
rel := filepath.FromSlash(key)
|
||||
if expected[rel] {
|
||||
@@ -108,7 +110,8 @@ func (t *copyTracker) pruneOrphans(expected map[string]bool, dryRun bool) ([]str
|
||||
if t.owns(rel) {
|
||||
if !dryRun {
|
||||
if err := os.Remove(filepath.Join(t.dir, rel)); err != nil {
|
||||
return removed, fmt.Errorf("failed to remove orphaned copy %s: %w", rel, err)
|
||||
errs = append(errs, fmt.Errorf("failed to remove orphaned copy %s: %w", rel, err))
|
||||
continue
|
||||
}
|
||||
}
|
||||
removed = append(removed, rel)
|
||||
@@ -117,7 +120,7 @@ func (t *copyTracker) pruneOrphans(expected map[string]bool, dryRun bool) ([]str
|
||||
t.forget(rel)
|
||||
}
|
||||
}
|
||||
return removed, nil
|
||||
return removed, errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (t *copyTracker) save() error {
|
||||
|
||||
@@ -273,6 +273,29 @@ func TestSyncExtra_MergeWithoutFileLinksPreservesIdenticalLocalFile(t *testing.T
|
||||
}
|
||||
}
|
||||
|
||||
// One failed removal must not stop the remaining orphans from being pruned.
|
||||
func TestCopyTracker_PruneOrphansContinuesAfterRemoveFailure(t *testing.T) {
|
||||
skipUnlessPermissionsEnforced(t)
|
||||
dir := t.TempDir()
|
||||
locked := filepath.Join(dir, "locked")
|
||||
os.MkdirAll(locked, 0755)
|
||||
os.WriteFile(filepath.Join(locked, "a.md"), []byte("a"), 0644)
|
||||
os.WriteFile(filepath.Join(dir, "b.md"), []byte("b"), 0644)
|
||||
tracker := loadCopyTracker(dir)
|
||||
tracker.record(filepath.Join("locked", "a.md"))
|
||||
tracker.record("b.md")
|
||||
os.Chmod(locked, 0555)
|
||||
t.Cleanup(func() { os.Chmod(locked, 0755) })
|
||||
|
||||
removed, err := tracker.pruneOrphans(nil, false)
|
||||
if err == nil {
|
||||
t.Error("expected an error for the locked copy")
|
||||
}
|
||||
if len(removed) != 1 || removed[0] != "b.md" {
|
||||
t.Errorf("expected b.md pruned despite the failure, got %v", removed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyTracker_PruneOrphansPreservesOwnershipOnRemoveFailure(t *testing.T) {
|
||||
if runtime.GOOS != "linux" {
|
||||
t.Skip("requires a read-only proc file")
|
||||
|
||||
@@ -164,12 +164,15 @@ func TestSync_AgentsExtension_PartialFailureStillPrunes(t *testing.T) {
|
||||
sb := testutil.NewSandbox(t)
|
||||
defer sb.Cleanup()
|
||||
|
||||
createAgentSource(t, sb, map[string]string{"good.md": "ok", "bad.md": "FAIL"})
|
||||
agentsDir := createAgentSource(t, sb, map[string]string{"good.md": "ok", "bad.md": "FAIL", "gone.md": "old"})
|
||||
agentsPath := createAgentTarget(t, sb, "codex")
|
||||
orphan := filepath.Join(agentsPath, "gone.md")
|
||||
if err := os.WriteFile(orphan, []byte("old"), 0644); err != nil {
|
||||
// A copy synced earlier whose source agent was then removed.
|
||||
sb.WriteConfig(agentExtensionConfig(sb, agentsPath, " mode: copy\n"))
|
||||
sb.RunCLI("sync", "agents").AssertSuccess(t)
|
||||
if err := os.Remove(filepath.Join(agentsDir, "gone.md")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
orphan := filepath.Join(agentsPath, "gone.md")
|
||||
ext := writeAgentExtension(t, sb, "picky", "run: [\"sh\", \"-c\", \"grep -q FAIL \\\"$SS_SRC_PATH\\\" && exit 1; cat\"]\n")
|
||||
sb.WriteConfig(agentExtensionConfig(sb, agentsPath, " extension: "+ext+"\n"))
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ Agent sync supports all three modes, same as skills:
|
||||
|------|----------|
|
||||
| **merge** (default) | Per-file symlinks. Local agent files in the target are preserved. On Windows without Developer Mode, agents are copied instead and kept updated and pruned like links ([details](/docs/troubleshooting/windows#file-links-need-windows-developer-mode-copying-instead)). |
|
||||
| **symlink** | Entire agents directory symlinked. |
|
||||
| **copy** | Agent files copied as real files. |
|
||||
| **copy** | Agent files copied as real files. Copies are tracked in `.skillshare-manifest.json`, so orphan cleanup removes only copies skillshare wrote and you have not edited; your own agent files in the target are preserved. |
|
||||
|
||||
```bash
|
||||
# Sync everything (skills + agents)
|
||||
|
||||
Reference in New Issue
Block a user