From 9fec8669bbba9aa9dc6431f4f06e24a4408317fb Mon Sep 17 00:00:00 2001 From: Willie Date: Thu, 26 Feb 2026 19:58:24 +0800 Subject: [PATCH] fix(sync): resolve race condition for targets sharing the same path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Multiple targets (e.g. universal/amp/kimi/replit) share ~/.config/agents/skills. Parallel sync caused concurrent goroutines to race on os.Symlink for the same destination, producing "file exists" errors. Fix: group entries by resolved target path so same-path targets run sequentially within one goroutine. Different-path targets remain fully parallel. Also addresses PR #52 review feedback: - audit: replace O(N²) findElapsed with O(1) index-aligned elapsed slice - test-brew-upgrade: explicit go check + mktemp -d (replaces || true and /tmp) Add 6 integration tests covering shared-path race condition, idempotency, --force mode, mixed paths, filters, and output verification. --- .devcontainer/bin/test-brew-upgrade | 8 +- cmd/skillshare/audit.go | 22 +- cmd/skillshare/sync_parallel.go | 35 ++- tests/integration/sync_shared_path_test.go | 280 +++++++++++++++++++++ 4 files changed, 322 insertions(+), 23 deletions(-) create mode 100644 tests/integration/sync_shared_path_test.go diff --git a/.devcontainer/bin/test-brew-upgrade b/.devcontainer/bin/test-brew-upgrade index 5a6eefa7d..d4f40f0f6 100755 --- a/.devcontainer/bin/test-brew-upgrade +++ b/.devcontainer/bin/test-brew-upgrade @@ -7,10 +7,14 @@ set -euo pipefail WORKSPACE_ROOT="${SKILLSHARE_DEV_WORKSPACE_ROOT:-/workspace}" GO_BIN="${SKILLSHARE_DEV_GO_BIN:-/usr/local/go/bin/go}" -[ -x "$GO_BIN" ] || GO_BIN="$(command -v go || true)" +[ -x "$GO_BIN" ] || GO_BIN="$(command -v go)" +if [ -z "$GO_BIN" ]; then + echo "Error: 'go' command not found. Please ensure Go is installed and in your PATH." >&2 + exit 1 +fi CELLAR="/opt/homebrew/Cellar/skillshare/dev/bin" -FAKE_BIN="/tmp/fake-brew-bin" +FAKE_BIN="$(mktemp -d)" BINARY="$CELLAR/skillshare" cleanup() { diff --git a/cmd/skillshare/audit.go b/cmd/skillshare/audit.go index 869210689..803a48fab 100644 --- a/cmd/skillshare/audit.go +++ b/cmd/skillshare/audit.go @@ -326,16 +326,6 @@ func toAuditInputs(skills []struct { return inputs } -// findElapsed returns the elapsed time for a result from the scan outputs. -func findElapsed(outputs []audit.ScanOutput, result *audit.Result) time.Duration { - for _, o := range outputs { - if o.Result == result { - return o.Elapsed - } - } - return 0 -} - func scanPathTarget(targetPath, projectRoot string) (*audit.Result, error) { info, err := os.Stat(targetPath) if err != nil { @@ -418,8 +408,9 @@ func auditInstalled(sourcePath, mode, projectRoot, threshold string, opts auditO fmt.Println() } - // Collect results. + // Collect results and their elapsed times together. results := make([]*audit.Result, 0, len(skillPaths)) + elapsed := make([]time.Duration, 0, len(skillPaths)) scanErrors := 0 for i, sp := range skillPaths { sr := scanResults[i] @@ -433,6 +424,7 @@ func auditInstalled(sourcePath, mode, projectRoot, threshold string, opts auditO sr.Result.Threshold = threshold sr.Result.IsBlocked = sr.Result.HasSeverityAtOrAbove(threshold) results = append(results, sr.Result) + elapsed = append(elapsed, sr.Elapsed) } summary := summarizeAuditResults(len(skillPaths), results, threshold) @@ -444,7 +436,7 @@ func auditInstalled(sourcePath, mode, projectRoot, threshold string, opts auditO if !jsonOutput { for i, r := range results { if !opts.Quiet || len(r.Findings) > 0 { - printSkillResultLine(i+1, len(results), r, findElapsed(scanResults, r)) + printSkillResultLine(i+1, len(results), r, elapsed[i]) } } fmt.Println() @@ -554,8 +546,9 @@ func auditFiltered(sourcePath string, names, groups []string, mode, projectRoot, fmt.Println() } - // Collect results. + // Collect results and their elapsed times together. results := make([]*audit.Result, 0, len(matched)) + elapsed := make([]time.Duration, 0, len(matched)) scanErrors := 0 for i, sp := range matched { sr := scanResults[i] @@ -569,6 +562,7 @@ func auditFiltered(sourcePath string, names, groups []string, mode, projectRoot, sr.Result.Threshold = threshold sr.Result.IsBlocked = sr.Result.HasSeverityAtOrAbove(threshold) results = append(results, sr.Result) + elapsed = append(elapsed, sr.Elapsed) } summary := summarizeAuditResults(len(matched), results, threshold) @@ -580,7 +574,7 @@ func auditFiltered(sourcePath string, names, groups []string, mode, projectRoot, if !jsonOutput { for i, r := range results { if !opts.Quiet || len(r.Findings) > 0 { - printSkillResultLine(i+1, len(results), r, findElapsed(scanResults, r)) + printSkillResultLine(i+1, len(results), r, elapsed[i]) } } fmt.Println() diff --git a/cmd/skillshare/sync_parallel.go b/cmd/skillshare/sync_parallel.go index cd336fa26..c1ebacd0d 100644 --- a/cmd/skillshare/sync_parallel.go +++ b/cmd/skillshare/sync_parallel.go @@ -348,6 +348,8 @@ func renderSyncResults(results []syncTargetResult) { } // runParallelSync executes sync for multiple targets in parallel using bounded concurrency. +// Targets sharing the same resolved path are grouped and run sequentially within one +// goroutine to avoid race conditions (e.g., two targets trying to create the same symlink). // Returns results (indexed by position) and count of failed targets. func runParallelSync(entries []syncTargetEntry, source string, skills []sync.DiscoveredSkill, dryRun, force bool) ([]syncTargetResult, int) { names := make([]string, len(entries)) @@ -356,21 +358,40 @@ func runParallelSync(entries []syncTargetEntry, source string, skills []sync.Dis } progress := newSyncProgress(names) + // Group entries by resolved target path so shared-path targets run sequentially. + type indexedEntry struct { + idx int + entry syncTargetEntry + } + groups := make(map[string][]indexedEntry) + var groupOrder []string + for i, e := range entries { + p := e.target.Path + if _, seen := groups[p]; !seen { + groupOrder = append(groupOrder, p) + } + groups[p] = append(groups[p], indexedEntry{idx: i, entry: e}) + } + results := make([]syncTargetResult, len(entries)) sem := make(chan struct{}, syncMaxWorkers) var wg gosync.WaitGroup - for i, e := range entries { + for _, p := range groupOrder { + group := groups[p] wg.Add(1) sem <- struct{}{} - go func(idx int, entry syncTargetEntry) { + go func(members []indexedEntry) { defer wg.Done() defer func() { <-sem }() - progress.startTarget(entry.name) - r := collectSyncResult(entry.name, entry.target, source, entry.mode, skills, dryRun, force, progress) - progress.doneTarget(entry.name, r) - results[idx] = r - }(i, e) + // Run all members of this path group sequentially. + for _, m := range members { + progress.startTarget(m.entry.name) + r := collectSyncResult(m.entry.name, m.entry.target, source, m.entry.mode, skills, dryRun, force, progress) + progress.doneTarget(m.entry.name, r) + results[m.idx] = r + } + }(group) } wg.Wait() progress.stop() diff --git a/tests/integration/sync_shared_path_test.go b/tests/integration/sync_shared_path_test.go new file mode 100644 index 000000000..c1f50e2fc --- /dev/null +++ b/tests/integration/sync_shared_path_test.go @@ -0,0 +1,280 @@ +//go:build !online + +package integration + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "skillshare/internal/testutil" +) + +// TestSync_SharedPath_NoRaceCondition verifies that multiple targets sharing +// the same resolved directory path do not cause "file exists" errors during +// parallel sync. This was a regression introduced when sync moved from +// sequential to parallel execution — concurrent goroutines would race on +// os.Symlink for the same destination path. +// +// The fix groups targets by resolved path so that same-path targets run +// sequentially within one goroutine. +func TestSync_SharedPath_NoRaceCondition(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + // Create multiple skills to increase race window. + for i := 0; i < 10; i++ { + sb.CreateSkill(fmt.Sprintf("skill-%02d", i), map[string]string{ + "SKILL.md": fmt.Sprintf("---\nname: skill-%02d\n---\n# Skill %d", i, i), + }) + } + + // All 4 targets share the same physical directory — mimics the real-world + // universal/amp/kimi/replit pattern in targets.yaml. + sharedDir := filepath.Join(sb.Home, ".config", "agents", "skills") + if err := os.MkdirAll(sharedDir, 0755); err != nil { + t.Fatalf("create shared dir: %v", err) + } + + sb.WriteConfig(fmt.Sprintf(`source: %s +mode: merge +targets: + target-a: + path: %s + target-b: + path: %s + target-c: + path: %s + target-d: + path: %s +`, sb.SourcePath, sharedDir, sharedDir, sharedDir, sharedDir)) + + result := sb.RunCLI("sync") + result.AssertSuccess(t) + + // Verify all 10 skills exist as correct symlinks. + for i := 0; i < 10; i++ { + name := fmt.Sprintf("skill-%02d", i) + link := filepath.Join(sharedDir, name) + if !sb.IsSymlink(link) { + t.Errorf("symlink not created for %s", name) + continue + } + expected := filepath.Join(sb.SourcePath, name) + if got := sb.SymlinkTarget(link); got != expected { + t.Errorf("skill %s: symlink target = %q, want %q", name, got, expected) + } + } +} + +// TestSync_SharedPath_WithSameFilters verifies that shared-path targets +// with identical include filters both succeed without race conditions. +func TestSync_SharedPath_WithSameFilters(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + sb.CreateSkill("alpha", map[string]string{ + "SKILL.md": "---\nname: alpha\n---\n# Alpha", + }) + sb.CreateSkill("beta", map[string]string{ + "SKILL.md": "---\nname: beta\n---\n# Beta", + }) + + sharedDir := filepath.Join(sb.Home, ".config", "shared-skills") + if err := os.MkdirAll(sharedDir, 0755); err != nil { + t.Fatalf("create shared dir: %v", err) + } + + // Both targets share the same path and same filter — no prune conflict. + sb.WriteConfig(fmt.Sprintf(`source: %s +mode: merge +targets: + target-a: + path: %s + include: [alpha] + target-b: + path: %s + include: [alpha] +`, sb.SourcePath, sharedDir, sharedDir)) + + result := sb.RunCLI("sync") + result.AssertSuccess(t) + + // alpha should be symlinked; beta excluded by both filters. + if !sb.IsSymlink(filepath.Join(sharedDir, "alpha")) { + t.Error("symlink not created for alpha") + } + if sb.IsSymlink(filepath.Join(sharedDir, "beta")) { + t.Error("beta should not be symlinked (excluded by both targets)") + } +} + +// TestSync_SharedPath_RepeatedRuns verifies that running sync multiple times +// with shared-path targets is idempotent and never produces errors. +func TestSync_SharedPath_RepeatedRuns(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + for i := 0; i < 5; i++ { + sb.CreateSkill(fmt.Sprintf("skill-%d", i), map[string]string{ + "SKILL.md": fmt.Sprintf("---\nname: skill-%d\n---\n# Skill", i), + }) + } + + sharedDir := filepath.Join(sb.Home, ".config", "agents", "skills") + if err := os.MkdirAll(sharedDir, 0755); err != nil { + t.Fatalf("create shared dir: %v", err) + } + + sb.WriteConfig(fmt.Sprintf(`source: %s +mode: merge +targets: + alpha: + path: %s + beta: + path: %s + gamma: + path: %s +`, sb.SourcePath, sharedDir, sharedDir, sharedDir)) + + // Run sync 3 times — must be idempotent. + for run := 1; run <= 3; run++ { + result := sb.RunCLI("sync") + result.AssertSuccess(t) + } + + // Verify final state. + for i := 0; i < 5; i++ { + link := filepath.Join(sharedDir, fmt.Sprintf("skill-%d", i)) + if !sb.IsSymlink(link) { + t.Errorf("run 3: symlink not created for skill-%d", i) + } + } +} + +// TestSync_SharedPath_ForceMode verifies that --force with shared-path targets +// does not fail when replacing local copies concurrently. +func TestSync_SharedPath_ForceMode(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + sb.CreateSkill("my-skill", map[string]string{ + "SKILL.md": "---\nname: my-skill\n---\n# My Skill", + }) + + sharedDir := filepath.Join(sb.Home, ".config", "agents", "skills") + if err := os.MkdirAll(sharedDir, 0755); err != nil { + t.Fatalf("create shared dir: %v", err) + } + + // Pre-create a local copy (directory, not symlink) to trigger --force path. + localSkill := filepath.Join(sharedDir, "my-skill") + if err := os.MkdirAll(localSkill, 0755); err != nil { + t.Fatalf("create local skill dir: %v", err) + } + if err := os.WriteFile(filepath.Join(localSkill, "SKILL.md"), []byte("local"), 0644); err != nil { + t.Fatalf("write local SKILL.md: %v", err) + } + + sb.WriteConfig(fmt.Sprintf(`source: %s +mode: merge +targets: + target-a: + path: %s + target-b: + path: %s +`, sb.SourcePath, sharedDir, sharedDir)) + + result := sb.RunCLI("sync", "--force") + result.AssertSuccess(t) + + // Should be a symlink now, not a directory. + if !sb.IsSymlink(filepath.Join(sharedDir, "my-skill")) { + t.Error("my-skill should be a symlink after --force sync") + } +} + +// TestSync_SharedPath_MixedWithUniquePaths ensures that targets with unique +// paths still run in parallel alongside shared-path target groups. +func TestSync_SharedPath_MixedWithUniquePaths(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + sb.CreateSkill("the-skill", map[string]string{ + "SKILL.md": "---\nname: the-skill\n---\n# The Skill", + }) + + sharedDir := filepath.Join(sb.Home, ".config", "agents", "skills") + uniqueDir1 := filepath.Join(sb.Home, ".cursor", "skills") + uniqueDir2 := filepath.Join(sb.Home, ".claude", "skills") + + for _, d := range []string{sharedDir, uniqueDir1, uniqueDir2} { + if err := os.MkdirAll(d, 0755); err != nil { + t.Fatalf("create dir %s: %v", d, err) + } + } + + sb.WriteConfig(fmt.Sprintf(`source: %s +mode: merge +targets: + shared-a: + path: %s + shared-b: + path: %s + unique-cursor: + path: %s + unique-claude: + path: %s +`, sb.SourcePath, sharedDir, sharedDir, uniqueDir1, uniqueDir2)) + + result := sb.RunCLI("sync") + result.AssertSuccess(t) + + // Verify all three directories got the symlink. + for _, dir := range []string{sharedDir, uniqueDir1, uniqueDir2} { + link := filepath.Join(dir, "the-skill") + if !sb.IsSymlink(link) { + t.Errorf("symlink not created in %s", dir) + } + } +} + +// TestSync_SharedPath_OutputShowsAllTargets verifies that sync output includes +// status lines for each target name, even when they share a physical path. +func TestSync_SharedPath_OutputShowsAllTargets(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + sb.CreateSkill("test-skill", map[string]string{ + "SKILL.md": "---\nname: test-skill\n---\n# Test", + }) + + sharedDir := filepath.Join(sb.Home, ".config", "agents", "skills") + if err := os.MkdirAll(sharedDir, 0755); err != nil { + t.Fatalf("create shared dir: %v", err) + } + + sb.WriteConfig(fmt.Sprintf(`source: %s +mode: merge +targets: + universal: + path: %s + replit: + path: %s + kimi: + path: %s +`, sb.SourcePath, sharedDir, sharedDir, sharedDir)) + + result := sb.RunCLI("sync") + result.AssertSuccess(t) + + // All target names should appear in output, even though they share a path. + output := strings.ToLower(result.Output()) + for _, name := range []string{"universal", "replit", "kimi"} { + if !strings.Contains(output, name) { + t.Errorf("output should mention target %q, got:\n%s", name, result.Output()) + } + } +}