fix(sync): stop linking single files with junctions on Windows

createLink always tried `mklink /J` first. mklink does not check the
source type, so agents, single-file extras such as a shared AGENTS.md, and
per-file extras links became directory junctions that point at a file:
Explorer shows a folder and tools cannot read it. Sync then compared only
the link destination and kept the broken junction forever.

Files now get real symlinks only. When the OS cannot create them (Windows
without Developer Mode, non-elevated), file-based link modes fall back to
copies at the mode level, so status, doctor, the API and later syncs all
see an effective `copy` mode. Fallback copies are tracked by hash so they
keep updating and pruning without touching the user's own files, and turn
back into links once links work. Single-file extras remember what they last
wrote, so an unedited copy is replaced silently instead of producing a
drift backup on every source edit, and switching a target back to import
restores the user's attach-time content plus the import block.

Includes a Windows runbook and script, verified on a Windows 11 ARM64 VM
with and without symlink rights.

Refs #299
This commit is contained in:
Willie
2026-09-28 13:54:52 +08:00
parent 97499ebcc4
commit c7eb59a78f
25 changed files with 1216 additions and 99 deletions
@@ -0,0 +1,91 @@
# Windows Runbook: File Links and Copy Fallback
Manual runbook (not mdproof): it runs on a real Windows machine, driven from a macOS
host through UTM's `utmctl`. It checks that file-based link modes never produce a
junction to a file, that they fall back to copies without file-symlink rights, and
that skills stay junctions recognised as links.
## Scope
- Skills (merge): directory junction, tag `0xa0000003`, not re-linked on resync.
- Agents (merge), directory extra (merge), single-file extra `AGENTS.md` (merge):
- with file-symlink rights: symlink, tag `0xa000000c`, readable;
- without: regular file with source content and the warning
`file links need Windows Developer Mode; copying instead`.
- `-Extended`: two source edits propagate without drift backups or a "backed up"
line, removed source agent is pruned while an
untracked agent stays, a junction-to-file `AGENTS.md` is replaced, and
`status`/`doctor`/`list`/`extras list` output is captured.
## Environment
- Windows guest in UTM with the guest agent (runs as SYSTEM). Everything lives under
`C:\Users\Public\sstest\`. The script overrides `USERPROFILE`, `HOME`, `APPDATA`,
`LOCALAPPDATA` and `TEMP`, and aborts unless `doctor` reports the config under the
test root.
- `utmctl exec` is asynchronous and returns no output. The script writes a report
and ends it with `DONE`; poll it with `utmctl file pull`.
- SYSTEM and administrators (including an admin with UAC off, where
`-RunLevel Limited` still yields a full token) can create file symlinks. For the
fallback path run through `runas /trustlevel:0x20000` (basic-user token); the
report's `SeCreateSymbolicLinkPrivilege: absent` and `file symlink probe: FAILED`
lines confirm it.
## Steps
1. Build in the devcontainer and copy out (`GOARCH=amd64` for x64 guests):
```bash
docker exec -w /workspace "$CONTAINER" bash -lc \
'GOOS=windows GOARCH=arm64 go build -o /tmp/ss.exe ./cmd/skillshare'
docker cp "$CONTAINER":/tmp/ss.exe ./ss.exe
```
For a baseline, build `git archive HEAD` extracted to a temp dir the same way.
2. Push files (create `C:\Users\Public\sstest` first and grant the desktop user
access, e.g. `icacls C:\Users\Public\sstest /grant '<user>:(OI)(CI)F'`):
```bash
U=~/Applications/UTM.app/Contents/MacOS/utmctl
$U file push Windows 'C:\Users\Public\sstest\ss.exe' < ss.exe
$U file push Windows 'C:\Users\Public\sstest\e2e-file-links.ps1' < scripts/windows/e2e-file-links.ps1
```
3. Run as the desktop user through a scheduled task registered from SYSTEM (push
this as `launch.ps1` and `utmctl exec` it with `powershell.exe -File`):
```powershell
$ps = 'powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File C:\Users\Public\sstest\e2e-file-links.ps1 -Exe C:\Users\Public\sstest\ss.exe -Root C:\Users\Public\sstest\run -Out C:\Users\Public\sstest\out.txt -Extended'
$a = New-ScheduledTaskAction -Execute 'runas.exe' -Argument "/trustlevel:0x20000 `"$ps`"" # or -Execute powershell.exe for a full token
$p = New-ScheduledTaskPrincipal -UserId '<user>' -LogonType Interactive -RunLevel Limited
Register-ScheduledTask -TaskName sstest-run -Action $a -Principal $p -Force
Start-ScheduledTask -TaskName sstest-run
```
4. Poll until the last line is `DONE`:
```bash
$U file pull Windows 'C:\Users\Public\sstest\out.txt'
```
5. Clean up from SYSTEM: `Unregister-ScheduledTask -TaskName sstest-* -Confirm:$false`
and `cmd /c rmdir /s /q C:\Users\Public\sstest` (`rmdir` does not follow junctions).
## Pass Criteria
- Baseline (before the fix) reproduces the bug: agent, rule and `AGENTS.md` entries
are `LinkType=Junction`, tag `0xa0000003`, `read=NO`.
- Fixed, full token: those entries are `LinkType=SymbolicLink`, tag `0xa000000c`,
`read=yes` with the source content.
- Fixed, basic-user token: those entries have `reparseTag=none`, `read=yes`; the
warning line appears for agents and each file extra; `.skillshare-manifest.json`
exists in the agents and rules targets.
- Both fixed runs: `demo-skill` is a junction and `skill junction recreated: False`;
(a) target reads `shared v2`, `drift backups before/after source edits: 0/0` and
`backed-up message after source edits: False`; (b) `demo-agent.md : MISSING`,
`my-own.md` kept; (c) the junction becomes a readable file or symlink after sync.
- Fixed, basic-user token: `status agents line` shows `copied … [copy] 1/1 linked` and
`doctor agents line` shows `[copy] synced (1/1 linked)`.
- Note: `utmctl file pull` exits 0 even when the file is missing; check its output
for `failed to open` when verifying cleanup.
+4 -3
View File
@@ -8,6 +8,7 @@ import (
"skillshare/internal/config"
"skillshare/internal/sync"
"skillshare/internal/ui"
"skillshare/internal/utils"
)
// extraDiffResult holds diff for one extra → one target.
@@ -60,7 +61,7 @@ func collectExtrasDiff(extras []config.ExtraConfig, sourceResolver func(config.E
results = append(results, extraDiffResult{
extraName: extra.Name,
targetPath: t.Path,
mode: sync.EffectiveMode(t.Mode),
mode: sync.ExtraTargetMode(t.Mode, extra.File != ""),
errMsg: "source directory not found",
})
}
@@ -68,7 +69,7 @@ func collectExtrasDiff(extras []config.ExtraConfig, sourceResolver func(config.E
}
for _, t := range extra.Targets {
mode := sync.EffectiveMode(t.Mode)
mode := sync.ExtraTargetMode(t.Mode, extra.File != "")
r := extraDiffResult{
extraName: extra.Name,
targetPath: t.Path,
@@ -107,7 +108,7 @@ func collectExtrasDiff(extras []config.ExtraConfig, sourceResolver func(config.E
switch mode {
case "merge", "symlink":
if tInfo.Mode()&os.ModeSymlink != 0 {
if utils.IsLinkMode(targetFile, tInfo.Mode()) {
link, _ := os.Readlink(targetFile)
if link != sourceFile {
r.items = append(r.items, extraDiffItem{
+6 -6
View File
@@ -10,6 +10,7 @@ import (
"skillshare/internal/resource"
"skillshare/internal/sync"
"skillshare/internal/ui"
"skillshare/internal/utils"
)
// checkAgentTargetInline validates the agent target for a single target,
@@ -22,10 +23,7 @@ func checkAgentTargetInline(name string, target config.TargetConfig, builtinAgen
}
ac := target.AgentsConfig()
mode := ac.Mode
if mode == "" {
mode = "merge"
}
mode, _ := agentStatusLabel(ac)
// Apply per-target include/exclude filters to get expected agent count
filtered, filterErr := sync.FilterAgents(allAgents, ac.Include, ac.Exclude)
@@ -36,7 +34,8 @@ func checkAgentTargetInline(name string, target config.TargetConfig, builtinAgen
fmt.Sprintf("Agent target %s: invalid filter: %v", name, filterErr), nil)
return
}
agentCount := len(sync.FilterAgentsByTarget(filtered, name))
expected := sync.FilterAgentsByTarget(filtered, name)
agentCount := len(expected)
// Build details for JSON output
var details []string
@@ -73,6 +72,7 @@ func checkAgentTargetInline(name string, target config.TargetConfig, builtinAgen
}
linked, broken := countAgentLinksAndBroken(agentPath)
linked += sync.SyncedAgentCopies(agentPath, expected)
if broken > 0 {
msg := fmt.Sprintf("[%s] %d linked, %d broken", mode, linked, broken)
fmt.Printf(" agents %s%s%s\n", ui.Yellow, msg, ui.Reset)
@@ -108,7 +108,7 @@ func countAgentLinksAndBroken(dir string) (linked, broken int) {
if !strings.HasSuffix(strings.ToLower(e.Name()), ".md") {
continue
}
if e.Type()&os.ModeSymlink == 0 {
if !utils.IsLinkMode(filepath.Join(dir, e.Name()), e.Type()) {
continue
}
// It's a symlink — check if target exists (os.Stat follows symlinks)
+1 -1
View File
@@ -53,7 +53,7 @@ func buildExtrasListEntries(extras []config.ExtraConfig, extrasSource, extension
}
for _, t := range extra.Targets {
m := sync.EffectiveMode(t.Mode)
m := sync.ExtraTargetMode(t.Mode, extra.File != "")
resolvedPath := config.ExpandPath(t.Path)
ti := extrasTargetInfo{
Path: t.Path,
+6 -5
View File
@@ -210,7 +210,7 @@ func printExtrasStatus(extras []config.ExtraConfig, sourceDirFn func(config.Extr
continue
}
for _, t := range extra.Targets {
detail := fmt.Sprintf("[%s] %s (%d files)", sync.EffectiveMode(t.Mode), t.Path, len(files))
detail := fmt.Sprintf("[%s] %s (%d files)", sync.ExtraTargetMode(t.Mode, extra.File != ""), t.Path, len(files))
ui.Status(extra.Name, "has files", detail)
}
}
@@ -380,8 +380,9 @@ func printTargetsStatus(cfg *config.Config, discovered []sync.DiscoveredSkill) e
agentsSource := cfg.EffectiveAgentsSource()
agentsExist := dirExists(agentsSource)
var agentCount int
var agents []resource.DiscoveredResource
if agentsExist {
agents, _ := resource.AgentKind{}.Discover(agentsSource)
agents, _ = resource.AgentKind{}.Discover(agentsSource)
agentCount = len(agents)
}
@@ -418,14 +419,14 @@ func printTargetsStatus(cfg *config.Config, discovered []sync.DiscoveredSkill) e
if agentsExist {
agentPath := resolveAgentTargetPath(target, builtinAgents, name)
if agentPath != "" {
linked := countLinkedAgents(agentPath)
agentStatus := "merged"
linked := countLinkedAgents(agentPath, agents)
agentMode, agentStatus := agentStatusLabel(target.AgentsConfig())
driftLabel := ""
if linked != agentCount && agentCount > 0 {
agentStatus = "drift"
driftLabel = ui.Yellow + " (drift)" + ui.Reset
}
printTargetSubItem("agents", agentStatus, fmt.Sprintf("[merge] %d/%d linked%s", linked, agentCount, driftLabel))
printTargetSubItem("agents", agentStatus, fmt.Sprintf("[%s] %d/%d linked%s", agentMode, linked, agentCount, driftLabel))
}
}
}
+22 -4
View File
@@ -3,6 +3,7 @@ package main
import (
"skillshare/internal/config"
"skillshare/internal/resource"
"skillshare/internal/sync"
)
// statusJSONAgents is the agent section of status --json output.
@@ -45,7 +46,7 @@ func buildAgentStatusJSON(cfg *config.Config) *statusJSONAgents {
continue
}
linked := countLinkedAgents(agentPath)
linked := countLinkedAgents(agentPath, agents)
result.Targets = append(result.Targets, statusJSONAgentTarget{
Name: name,
Path: agentPath,
@@ -58,8 +59,25 @@ func buildAgentStatusJSON(cfg *config.Config) *statusJSONAgents {
return result
}
// countLinkedAgents counts healthy .md symlinks in the target agent directory.
func countLinkedAgents(targetDir string) int {
// countLinkedAgents counts healthy .md symlinks in the target agent directory,
// plus up-to-date copies made where file links are unavailable.
func countLinkedAgents(targetDir string, agents []resource.DiscoveredResource) int {
linked, _ := countAgentLinksAndBroken(targetDir)
return linked
return linked + sync.SyncedAgentCopies(targetDir, agents)
}
// agentStatusLabel returns the effective agent mode and the sub-item status
// shown for a target in sync.
func agentStatusLabel(ac config.ResourceTargetConfig) (mode, status string) {
mode = sync.EffectiveMode(ac.Mode)
if ac.Extension == "" {
mode = sync.EffectiveAgentMode(ac.Mode)
}
switch mode {
case "copy":
return mode, "copied"
case "symlink":
return mode, "linked"
}
return mode, "merged"
}
+27
View File
@@ -0,0 +1,27 @@
package main
import (
"os"
"path/filepath"
"testing"
"skillshare/internal/config"
"skillshare/internal/resource"
"skillshare/internal/sync"
)
func TestCountLinkedAgents_CountsFallbackCopiesAsCopyMode(t *testing.T) {
t.Cleanup(sync.SetFileLinksForTest(false))
src, tgt := t.TempDir(), t.TempDir()
os.WriteFile(filepath.Join(src, "tutor.md"), []byte("# Tutor"), 0644)
agents := []resource.DiscoveredResource{{FlatName: "tutor.md", AbsPath: filepath.Join(src, "tutor.md")}}
if _, err := sync.SyncAgents(agents, src, tgt, "merge", false, false); err != nil {
t.Fatal(err)
}
mode, status := agentStatusLabel(config.ResourceTargetConfig{})
if n := countLinkedAgents(tgt, agents); n != 1 || mode != "copy" || status != "copied" {
t.Errorf("linked = %d, mode = %q, status = %q; want 1 copy in sync", n, mode, status)
}
}
+6 -5
View File
@@ -139,7 +139,7 @@ func buildProjectAgentStatusJSON(rt *projectRuntime) *statusJSONAgents {
continue
}
linked := countLinkedAgents(agentPath)
linked := countLinkedAgents(agentPath, agents)
result.Targets = append(result.Targets, statusJSONAgentTarget{
Name: entry.Name,
Path: agentPath,
@@ -221,8 +221,9 @@ func printProjectTargetsStatus(runtime *projectRuntime, discovered []sync.Discov
builtinAgents := config.ProjectAgentTargets()
agentsExist := dirExists(runtime.agentsSourcePath)
var agentCount int
var agents []resource.DiscoveredResource
if agentsExist {
agents, _ := (resource.AgentKind{}).Discover(runtime.agentsSourcePath)
agents, _ = (resource.AgentKind{}).Discover(runtime.agentsSourcePath)
agentCount = len(agents)
}
@@ -269,14 +270,14 @@ func printProjectTargetsStatus(runtime *projectRuntime, discovered []sync.Discov
if agentsExist {
agentPath := resolveProjectAgentTargetPath(entry, builtinAgents, runtime.root)
if agentPath != "" {
linked := countLinkedAgents(agentPath)
agentStatus := "merged"
linked := countLinkedAgents(agentPath, agents)
agentMode, agentStatus := agentStatusLabel(entry.AgentsConfig())
driftLabel := ""
if linked != agentCount && agentCount > 0 {
agentStatus = "drift"
driftLabel = ui.Yellow + " (drift)" + ui.Reset
}
printTargetSubItem("agents", agentStatus, fmt.Sprintf("[merge] %d/%d linked%s", linked, agentCount, driftLabel))
printTargetSubItem("agents", agentStatus, fmt.Sprintf("[%s] %d/%d linked%s", agentMode, linked, agentCount, driftLabel))
}
}
}
+8 -1
View File
@@ -332,7 +332,14 @@ func syncAgentTarget(name, agentPath, modeOverride string, spec *sync.ExtensionS
}
if !jsonOutput {
reportAgentSyncResult(name, mode, stats, dryRun)
shownMode := mode
if spec == nil {
shownMode = sync.EffectiveAgentMode(mode)
if shownMode != mode {
ui.Warning("%s: agents %s", name, sync.FileLinkFallbackWarning)
}
}
reportAgentSyncResult(name, shownMode, stats, dryRun)
}
return stats, err
+11 -9
View File
@@ -189,10 +189,11 @@ func cmdSyncExtrasGlobal(dryRun, force, jsonOutput bool, start time.Time) error
result, syncErr := syncExtraTarget(extra, target, extraSource, targetPath, mode, dryRun, force, "", spec)
shortTarget := shortenPath(targetPath)
shownMode := sync.ExtraTargetMode(mode, extra.File != "")
jsonTarget := syncExtrasJSONTarget{
Path: target.Path,
Mode: mode,
Mode: shownMode,
}
if syncErr != nil {
@@ -221,17 +222,17 @@ func cmdSyncExtrasGlobal(dryRun, force, jsonOutput bool, start time.Time) error
if !jsonOutput {
// Report result
verb := syncVerb(mode)
verb := syncVerb(shownMode)
if result.Synced > 0 {
parts := []string{fmt.Sprintf("%d files %s", result.Synced, verb)}
if result.Pruned > 0 {
parts = append(parts, fmt.Sprintf("%d pruned", result.Pruned))
}
ui.Success("%s %s (%s)", shortTarget, strings.Join(parts, ", "), mode)
ui.Success("%s %s (%s)", shortTarget, strings.Join(parts, ", "), shownMode)
} else if result.Skipped > 0 {
ui.Warning("%s %d files skipped (use --force to override)", shortTarget, result.Skipped)
} else {
ui.Success("%s up to date (%s)", shortTarget, mode)
ui.Success("%s up to date (%s)", shortTarget, shownMode)
}
for _, e := range result.Errors {
@@ -390,10 +391,11 @@ func cmdSyncExtrasProject(cwd string, dryRun, force, jsonOutput bool, start time
result, syncErr := syncExtraTarget(extra, target, extraSource, targetPath, mode, dryRun, force, cwd, spec)
shortTarget := shortenPath(targetPath)
shownMode := sync.ExtraTargetMode(mode, extra.File != "")
jsonTarget := syncExtrasJSONTarget{
Path: targetPath,
Mode: mode,
Mode: shownMode,
}
if syncErr != nil {
@@ -421,17 +423,17 @@ func cmdSyncExtrasProject(cwd string, dryRun, force, jsonOutput bool, start time
jsonEntry.Targets = append(jsonEntry.Targets, jsonTarget)
if !jsonOutput {
verb := syncVerb(mode)
verb := syncVerb(shownMode)
if result.Synced > 0 {
parts := []string{fmt.Sprintf("%d files %s", result.Synced, verb)}
if result.Pruned > 0 {
parts = append(parts, fmt.Sprintf("%d pruned", result.Pruned))
}
ui.Success("%s %s (%s)", shortTarget, strings.Join(parts, ", "), mode)
ui.Success("%s %s (%s)", shortTarget, strings.Join(parts, ", "), shownMode)
} else if result.Skipped > 0 {
ui.Warning("%s %d files skipped (use --force to override)", shortTarget, result.Skipped)
} else {
ui.Success("%s up to date (%s)", shortTarget, mode)
ui.Success("%s up to date (%s)", shortTarget, shownMode)
}
for _, e := range result.Errors {
@@ -570,7 +572,7 @@ func runExtrasSyncEntries(extras []config.ExtraConfig, sourceFunc func(config.Ex
}
result, syncErr := syncExtraTarget(extra, target, extraSource, targetPath, mode, dryRun, force, projectRoot, spec)
jt := syncExtrasJSONTarget{Path: targetPath, Mode: mode}
jt := syncExtrasJSONTarget{Path: targetPath, Mode: sync.ExtraTargetMode(mode, extra.File != "")}
if syncErr != nil {
jt.Error = syncErr.Error()
} else {
+5 -4
View File
@@ -13,6 +13,7 @@ import (
"skillshare/internal/config"
syncpkg "skillshare/internal/sync"
"skillshare/internal/utils"
)
// resolveExtensionSpec resolves a target's extension value into a transform
@@ -132,7 +133,7 @@ func (s *Server) handleExtras(w http.ResponseWriter, r *http.Request) {
entry.Targets = make([]extrasTargetInfo, 0, len(extra.Targets))
for _, t := range extra.Targets {
m := syncpkg.EffectiveMode(t.Mode)
m := syncpkg.ExtraTargetMode(t.Mode, extra.File != "")
targetPath := resolveExtrasTargetPath(projectRoot, t.Path)
ti := extrasTargetInfo{
Path: t.Path,
@@ -280,7 +281,7 @@ func (s *Server) handleExtrasDiff(w http.ResponseWriter, r *http.Request) {
}
for _, t := range extra.Targets {
m := syncpkg.EffectiveMode(t.Mode)
m := syncpkg.ExtraTargetMode(t.Mode, extra.File != "")
// Transform extensions use copy semantics; resolve through the shared
// resolver so the diff isn't computed against the merge default. On an
// invalid (non-copy) mode, leave m as-is — sync surfaces that error.
@@ -366,7 +367,7 @@ func buildExtrasDiffItems(sourceFiles []string, sourceDir, targetDir, mode strin
switch mode {
case "symlink", "merge":
if info.Mode()&os.ModeSymlink != 0 {
if utils.IsLinkMode(targetFile, info.Mode()) {
link, readErr := os.Readlink(targetFile)
if readErr != nil || filepath.Clean(resolveExtrasTargetPath(filepath.Dir(targetFile), link)) != filepath.Clean(sourceFile) {
items = append(items, extrasDiffItem{
@@ -608,7 +609,7 @@ func (s *Server) syncExtras(name string, dryRun, force bool) []extraSyncResult {
tr := extraTargetSyncResult{
Target: t.Path,
Mode: m,
Mode: syncpkg.ExtraTargetMode(t.Mode, extra.File != ""),
Errors: []string{},
}
@@ -11,6 +11,7 @@ import (
"skillshare/internal/config"
"skillshare/internal/instructions"
ssync "skillshare/internal/sync"
)
// newInstructionsServer creates a global-mode server whose HOME is a temp dir
@@ -452,3 +453,23 @@ func TestSharedInstructions_AssignAndRestoreRider(t *testing.T) {
t.Errorf("restore: %v, codex = %q", res, readFile(t, codex))
}
}
func TestSharedInstructions_ReportsCopyWithoutFileLinks(t *testing.T) {
t.Cleanup(ssync.SetFileLinksForTest(false))
s, home := newInstructionsServer(t, "codex")
if rr := instructionsRequest(t, s, http.MethodPost, "/api/instructions", `{"name":"team","content":"team\n"}`); rr.Code != http.StatusOK {
t.Fatalf("create: %d %s", rr.Code, rr.Body.String())
}
if res := decodeBody[map[string]any](t, instructionsRequest(t, s, http.MethodPost, "/api/instructions/assign", `{"targets":["codex"],"extras":["team"]}`)); res["success"] != true {
t.Fatalf("assign: %v", res)
}
list := decodeBody[struct {
Targets []sharedInstructionsTarget `json:"targets"`
}](t, instructionsRequest(t, s, http.MethodGet, "/api/instructions", ""))
info, err := os.Lstat(filepath.Join(home, ".codex", "AGENTS.md"))
if a := list.Targets[0].Assigned; len(a) != 1 || a[0].Mode != "copy" || a[0].Status != "synced" || err != nil || !info.Mode().IsRegular() {
t.Errorf("codex assigned = %+v, want a synced copy", a)
}
}
+3
View File
@@ -290,6 +290,9 @@ func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, projec
outputExt = spec.OutputExt
} else {
agentResult, err = ssync.SyncAgents(filteredAgents, agentsSource, agentPath, agentMode, dryRun, force, s.projectRoot)
if ssync.EffectiveAgentMode(agentMode) != agentMode {
warnings = append(warnings, name+": agents "+ssync.FileLinkFallbackWarning)
}
}
if err != nil {
warnings = append(warnings, "agent sync failed for "+name+": "+err.Error())
+132 -9
View File
@@ -68,6 +68,9 @@ func SyncAgents(agents []resource.DiscoveredResource, sourceDir, targetDir, mode
case "copy":
return syncAgentsCopy(agents, targetDir, dryRun, force)
default: // "merge" or ""
if !canCreateFileLink() {
return syncAgentsMergeCopy(agents, targetDir, dryRun, force)
}
return syncAgentsMerge(agents, sourceDir, targetDir, dryRun, force, root)
}
}
@@ -96,19 +99,22 @@ func syncAgentsMerge(agents []resource.DiscoveredResource, sourceDir, targetDir
}
}
// Copies made while file links were unavailable are relinked.
copies := loadCopyTracker(targetDir)
for _, agent := range agents {
targetPath := filepath.Join(targetDir, agent.FlatName)
info, err := os.Lstat(targetPath)
_, err := os.Lstat(targetPath)
if err == nil {
if info.Mode()&os.ModeSymlink != 0 {
if utils.IsSymlinkOrJunction(targetPath) {
absLink, linkErr := utils.ResolveLinkTarget(targetPath)
if linkErr != nil {
return nil, fmt.Errorf("failed to resolve link for %s: %w", agent.FlatName, linkErr)
}
absSource, _ := filepath.Abs(agent.AbsPath)
if linkResolvesToSource(absLink, absSource) {
if linkResolvesToSource(absLink, absSource) && fileLinkUsable(targetPath) {
dest, _ := os.Readlink(targetPath)
if !linkNeedsReformat(dest, relative) {
result.Linked = append(result.Linked, agent.FlatName)
@@ -131,12 +137,13 @@ func syncAgentsMerge(agents []resource.DiscoveredResource, sourceDir, targetDir
}
result.Updated = append(result.Updated, agent.FlatName)
} else {
if force {
if force || copies.owns(agent.FlatName) {
if !dryRun {
os.Remove(targetPath)
if err := createLink(targetPath, agent.AbsPath, relative); err != nil {
return nil, fmt.Errorf("failed to create symlink for %s: %w", agent.FlatName, err)
}
copies.forget(agent.FlatName)
}
result.Updated = append(result.Updated, agent.FlatName)
} else {
@@ -155,9 +162,97 @@ func syncAgentsMerge(agents []resource.DiscoveredResource, sourceDir, targetDir
}
}
if !dryRun {
if err := copies.save(); err != nil {
return nil, fmt.Errorf("failed to update agent manifest: %w", err)
}
}
return result, nil
}
// syncAgentsMergeCopy is merge mode where file links are unavailable: each
// agent is copied and tracked, so later syncs keep the copies updated and
// pruned while local agent files stay preserved as in merge mode.
func syncAgentsMergeCopy(agents []resource.DiscoveredResource, targetDir string, dryRun, force bool) (*AgentSyncResult, error) {
result := &AgentSyncResult{}
if !dryRun {
if err := os.MkdirAll(targetDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create agent target directory: %w", err)
}
}
copies := loadCopyTracker(targetDir)
for _, agent := range agents {
name := agent.FlatName
targetPath := filepath.Join(targetDir, name)
info, err := os.Lstat(targetPath)
exists := err == nil
switch {
case os.IsNotExist(err):
case err != nil:
return nil, fmt.Errorf("failed to check target path for %s: %w", name, err)
case utils.IsSymlinkOrJunction(targetPath):
// A merge-mode link, or a junction to a file that cannot be read.
case info.IsDir():
result.Skipped = append(result.Skipped, name)
continue
case contentEqual(agent.AbsPath, targetPath):
if !dryRun {
copies.record(name)
}
result.Linked = append(result.Linked, name)
continue
case !force && !copies.owns(name):
result.Skipped = append(result.Skipped, name)
continue
}
if !dryRun {
if exists {
if err := os.Remove(targetPath); err != nil {
return nil, fmt.Errorf("failed to replace %s: %w", name, err)
}
}
if err := copyFile(agent.AbsPath, targetPath); err != nil {
return nil, fmt.Errorf("failed to copy %s: %w", name, err)
}
copies.record(name)
}
if exists {
result.Updated = append(result.Updated, name)
} else {
result.Linked = append(result.Linked, name)
}
}
if !dryRun {
if err := copies.save(); err != nil {
return nil, fmt.Errorf("failed to update agent manifest: %w", err)
}
}
return result, nil
}
// SyncedAgentCopies counts the agents whose target file is a copy made in
// place of a link that still matches its source. Status and doctor count them
// as in sync alongside healthy links; a stale copy is drift.
func SyncedAgentCopies(targetDir string, agents []resource.DiscoveredResource) int {
copies := loadCopyTracker(targetDir)
n := 0
for _, a := range agents {
if _, tracked := copies.m.Managed[a.FlatName]; !tracked {
continue
}
target := filepath.Join(targetDir, a.FlatName)
if info, err := os.Lstat(target); err == nil && info.Mode().IsRegular() && contentEqual(a.AbsPath, target) {
n++
}
}
return n
}
// syncAgentsSymlink creates a single directory symlink from targetDir to sourceDir.
// If targetDir already exists as a real directory, it's replaced only with force.
func syncAgentsSymlink(sourceDir, targetDir string, dryRun, force bool, projectRoot string) (*AgentSyncResult, error) {
@@ -170,7 +265,7 @@ func syncAgentsSymlink(sourceDir, targetDir string, dryRun, force bool, projectR
info, err := os.Lstat(targetDir)
if err == nil {
if info.Mode()&os.ModeSymlink != 0 {
if utils.IsLinkMode(targetDir, info.Mode()) {
// Already a symlink — check if correct
absLink, linkErr := utils.ResolveLinkTarget(targetDir)
if linkErr != nil {
@@ -248,6 +343,21 @@ func syncAgentsCopy(agents []resource.DiscoveredResource, targetDir string, dryR
return nil, fmt.Errorf("failed to read source %s: %w", agent.FlatName, err)
}
// A link left by merge mode (or an unreadable junction) is replaced;
// writing through it would edit the source.
if utils.IsSymlinkOrJunction(targetPath) {
if !dryRun {
if err := os.Remove(targetPath); err != nil {
return nil, fmt.Errorf("failed to replace %s: %w", agent.FlatName, err)
}
if err := os.WriteFile(targetPath, srcData, 0644); err != nil {
return nil, fmt.Errorf("failed to write %s: %w", agent.FlatName, err)
}
}
result.Updated = append(result.Updated, agent.FlatName)
continue
}
if _, statErr := os.Stat(targetPath); statErr == nil {
// File exists — check if content matches
tgtData, readErr := os.ReadFile(targetPath)
@@ -308,7 +418,7 @@ func SyncAgentsTransform(agents []resource.DiscoveredResource, sourceDir, target
tgtFile := filepath.Join(targetDir, name)
// Merge-mode leftovers link to the source; drop the link, never write through it.
if info, err := os.Lstat(tgtFile); err == nil && info.Mode()&os.ModeSymlink != 0 {
if info, err := os.Lstat(tgtFile); err == nil && utils.IsLinkMode(tgtFile, info.Mode()) {
if err := os.Remove(tgtFile); err != nil {
errs = append(errs, fmt.Errorf("%s: remove symlink: %w", agent.FlatName, err))
continue
@@ -372,6 +482,15 @@ func PruneOrphanAgentLinks(targetDir string, agents []resource.DiscoveredResourc
expected[a.FlatName] = true
}
// Copies stand in for links when file links are unavailable.
copies := loadCopyTracker(targetDir)
removed = copies.pruneOrphans(expected, dryRun)
if !dryRun {
if err := copies.save(); err != nil {
return removed, fmt.Errorf("failed to update agent manifest: %w", err)
}
}
for _, entry := range entries {
name := entry.Name()
@@ -384,7 +503,7 @@ func PruneOrphanAgentLinks(targetDir string, agents []resource.DiscoveredResourc
continue
}
if info.Mode()&os.ModeSymlink == 0 {
if !utils.IsLinkMode(filepath.Join(targetDir, name), info.Mode()) {
continue
}
@@ -462,7 +581,7 @@ func FindLocalAgents(targetDir, sourcePath string) ([]LocalAgentInfo, error) {
return nil, fmt.Errorf("failed to read agent target directory: %w", err)
}
if info.Mode()&os.ModeSymlink != 0 {
if utils.IsLinkMode(targetDir, info.Mode()) {
absLink, err := utils.ResolveLinkTarget(targetDir)
if err != nil {
return nil, err
@@ -485,6 +604,7 @@ func FindLocalAgents(targetDir, sourcePath string) ([]LocalAgentInfo, error) {
return nil, fmt.Errorf("failed to read agent target directory: %w", err)
}
copies := loadCopyTracker(targetDir)
for _, entry := range entries {
name := entry.Name()
@@ -494,13 +614,16 @@ func FindLocalAgents(targetDir, sourcePath string) ([]LocalAgentInfo, error) {
if utils.IsHidden(name) || resource.ConventionalExcludes[name] {
continue
}
if copies.owns(name) {
continue // a copy standing in for a link, not a local agent
}
info, err := entry.Info()
if err != nil {
continue
}
if info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
if info.IsDir() || utils.IsLinkMode(filepath.Join(targetDir, name), info.Mode()) {
continue
}
+55 -14
View File
@@ -8,6 +8,8 @@ import (
"path/filepath"
"sort"
"strings"
"skillshare/internal/utils"
)
// ExtraResult holds the result of an extras sync operation.
@@ -151,7 +153,7 @@ func syncExtraTransform(sourcePath, targetPath string, spec *ExtensionSpec, dryR
// leftover symlink, or --force would be (re)written and counts as synced.
if dryRun {
if info, lstatErr := os.Lstat(tgtFile); lstatErr == nil &&
info.Mode()&os.ModeSymlink == 0 && !force {
!utils.IsLinkMode(tgtFile, info.Mode()) && !force {
result.Skipped++
} else {
result.Synced++
@@ -177,7 +179,7 @@ func syncExtraTransform(sourcePath, targetPath string, spec *ExtensionSpec, dryR
// never destroyed without --force.
if info, lstatErr := os.Lstat(tgtFile); lstatErr == nil {
switch {
case info.Mode()&os.ModeSymlink != 0:
case utils.IsLinkMode(tgtFile, info.Mode()):
// Leftover symlink from a different mode: safe to replace.
// os.Remove drops the link itself without following it.
if rmErr := os.Remove(tgtFile); rmErr != nil {
@@ -236,7 +238,7 @@ func syncExtraSymlinkMode(sourcePath, targetPath string, dryRun, force bool, pro
info, lstatErr := os.Lstat(targetPath)
if lstatErr == nil {
// Something exists at targetPath
if info.Mode()&os.ModeSymlink != 0 {
if utils.IsLinkMode(targetPath, info.Mode()) {
// Already a symlink — check if correct
dest, readErr := os.Readlink(targetPath)
if readErr == nil {
@@ -298,9 +300,20 @@ func syncExtraSymlinkMode(sourcePath, targetPath string, dryRun, force bool, pro
}
// syncExtraPerFile handles merge (symlink) and copy modes on a per-file basis.
// Merge copies instead when file links are unavailable; those copies are
// tracked so later syncs keep them updated and pruned like links.
func syncExtraPerFile(sourcePath, targetPath, mode string, dryRun, force, flatten bool, projectRoot string) (*ExtraResult, error) {
result := &ExtraResult{}
var copies *copyTracker
if mode == "merge" {
copies = loadCopyTracker(targetPath)
if !canCreateFileLink() {
mode = "copy"
result.Warnings = append(result.Warnings, FileLinkFallbackWarning)
}
}
files, err := DiscoverExtraFiles(sourcePath)
if err != nil {
return nil, err
@@ -332,7 +345,7 @@ func syncExtraPerFile(sourcePath, targetPath, mode string, dryRun, force, flatte
}
tgtFile := filepath.Join(targetPath, tgtRel)
synced, skipped, syncErr := syncOneExtraFile(srcFile, tgtFile, mode, dryRun, force, relative)
synced, skipped, syncErr := syncOneExtraFile(srcFile, tgtFile, mode, dryRun, force, relative, copies, tgtRel)
if syncErr != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", rel, syncErr))
continue
@@ -360,13 +373,24 @@ func syncExtraPerFile(sourcePath, targetPath, mode string, dryRun, force, flatte
result.Pruned = pruned
result.Errors = append(result.Errors, pruneErrors...)
}
if copies != nil {
for _, rel := range copies.pruneOrphans(sourceSet, false) {
cleanEmptyParents(filepath.Dir(filepath.Join(targetPath, rel)), targetPath)
result.Pruned++
}
if err := copies.save(); err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("failed to update manifest: %v", err))
}
}
}
return result, nil
}
// syncOneExtraFile syncs a single file. Returns (synced, skipped, error).
func syncOneExtraFile(srcFile, tgtFile, mode string, dryRun, force, relative bool) (int, int, error) {
// copies, when set, tracks copies made in place of merge-mode links under
// the target-relative name tgtRel.
func syncOneExtraFile(srcFile, tgtFile, mode string, dryRun, force, relative bool, copies *copyTracker, tgtRel string) (int, int, error) {
// Ensure parent directory exists
if !dryRun {
if err := os.MkdirAll(filepath.Dir(tgtFile), 0755); err != nil {
@@ -377,10 +401,10 @@ func syncOneExtraFile(srcFile, tgtFile, mode string, dryRun, force, relative boo
// Check if target already exists
info, lstatErr := os.Lstat(tgtFile)
if lstatErr == nil {
isSymlink := info.Mode()&os.ModeSymlink != 0
isSymlink := utils.IsSymlinkOrJunction(tgtFile)
// Already correct? → skip (idempotent)
if mode == "merge" && isSymlink {
if mode == "merge" && isSymlink && fileLinkUsable(tgtFile) {
dest, readErr := os.Readlink(tgtFile)
if readErr == nil {
absDest := resolveReadlink(dest, tgtFile)
@@ -401,12 +425,16 @@ func syncOneExtraFile(srcFile, tgtFile, mode string, dryRun, force, relative boo
if mode == "copy" && !isSymlink && !info.IsDir() {
srcInfo, srcErr := os.Stat(srcFile)
if srcErr == nil && srcInfo.Size() == info.Size() && contentEqual(srcFile, tgtFile) {
if copies != nil && !dryRun {
copies.record(tgtRel)
}
return 1, 0, nil
}
}
// Symlinks left over from a different mode are safe to replace
autoReplace := isSymlink
// Symlinks left over from a different mode are safe to replace, and so
// are unedited copies made in place of merge-mode links.
autoReplace := isSymlink || (copies != nil && !info.IsDir() && copies.owns(tgtRel))
if !autoReplace && !force {
return 0, 1, nil
}
@@ -427,10 +455,16 @@ func syncOneExtraFile(srcFile, tgtFile, mode string, dryRun, force, relative boo
if err := createLink(tgtFile, srcFile, relative); err != nil {
return 0, 0, fmt.Errorf("failed to create symlink: %w", err)
}
if copies != nil {
copies.forget(tgtRel)
}
case "copy":
if err := copyFile(srcFile, tgtFile); err != nil {
return 0, 0, fmt.Errorf("failed to copy file: %w", err)
}
if copies != nil {
copies.record(tgtRel)
}
}
return 1, 0, nil
@@ -479,7 +513,7 @@ func pruneExtraOrphans(targetPath string, sourceFiles map[string]bool, mode stri
// Source doesn't exist — candidate for pruning
if mode == "merge" {
// In merge mode, only prune symlinks (don't delete user's local files)
if info.Mode()&os.ModeSymlink == 0 {
if !utils.IsLinkMode(path, info.Mode()) {
return nil
}
}
@@ -597,7 +631,7 @@ func pruneExtraSymlinkTarget(targetPath string) (pruned int, errors []string) {
}
return 0, []string{fmt.Sprintf("prune %s: %v", targetPath, err)}
}
if info.Mode()&os.ModeSymlink == 0 {
if !utils.IsLinkMode(targetPath, info.Mode()) {
return 0, []string{fmt.Sprintf("prune %s: target is not a symlink", targetPath)}
}
if err := os.Remove(targetPath); err != nil {
@@ -682,9 +716,9 @@ func CheckSyncStatus(sourceFiles []string, sourceDir, targetDir, mode string, fl
switch mode {
case "symlink", "merge":
if tInfo.Mode()&os.ModeSymlink != 0 {
if utils.IsSymlinkOrJunction(targetFile) {
link, readErr := os.Readlink(targetFile)
if readErr != nil || filepath.Clean(resolveReadlink(link, targetFile)) != filepath.Clean(sourceFile) {
if readErr != nil || filepath.Clean(resolveReadlink(link, targetFile)) != filepath.Clean(sourceFile) || !fileLinkUsable(targetFile) {
return "drift"
}
} else {
@@ -732,6 +766,10 @@ type ExtraCollectResult struct {
// (basename only) rather than preserving the target subdirectory structure.
func CollectExtraFiles(sourceDir, targetDir, mode string, dryRun, force, flatten bool, projectRoot string) (*ExtraCollectResult, error) {
result := &ExtraCollectResult{}
// Without file links the collected files stay in place, as copies.
if !canCreateFileLink() {
mode = "copy"
}
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
return nil, fmt.Errorf("target directory does not exist: %s", targetDir)
@@ -766,10 +804,13 @@ func CollectExtraFiles(sourceDir, targetDir, mode string, dryRun, force, flatten
if err != nil {
return nil
}
if linfo.Mode()&os.ModeSymlink != 0 {
if utils.IsLinkMode(path, linfo.Mode()) {
result.Skipped++
return nil
}
if info.Name() == ManifestFile {
return nil // skillshare's copy tracking, not user content
}
rel, err := filepath.Rel(targetDir, path)
if err != nil {
+45 -1
View File
@@ -11,6 +11,7 @@ import (
"time"
"skillshare/internal/config"
"skillshare/internal/utils"
)
// keepExtraBackups bounds the backups kept per replaced file.
@@ -220,11 +221,54 @@ func fileExists(path string) bool {
// clearExtraAttach drops the attach-time record of path.
func clearExtraAttach(path string) {
dir := extraBackupDir(path)
for _, name := range []string{attachCreated, attachRestore, attachRestoreLink} {
for _, name := range []string{attachCreated, attachRestore, attachRestoreLink, extraWritten} {
_ = os.Remove(filepath.Join(dir, name))
}
}
// extraWritten holds the hash of the copy skillshare last wrote at a target,
// so a later sync can tell its own unedited copy from a user edit.
const extraWritten = "written"
// recordExtraWritten remembers the file now at path as skillshare's copy.
func recordExtraWritten(path string) error {
h, err := utils.FileHash(path)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(extraBackupDir(path), extraWritten), []byte(h), 0644)
}
func clearExtraWritten(path string) {
_ = os.Remove(filepath.Join(extraBackupDir(path), extraWritten))
}
func hasExtraWritten(path string) bool {
return fileExists(filepath.Join(extraBackupDir(path), extraWritten))
}
// extraRestoreBase returns the file content recorded at attach time, or ""
// when no file existed or a link was replaced (a link's content is not the
// user's to rebuild).
func extraRestoreBase(path string) string {
data, err := os.ReadFile(filepath.Join(extraBackupDir(path), attachRestore))
if err != nil {
return ""
}
return string(data)
}
// isOurExtraCopy reports whether the file at path is still exactly the copy
// skillshare last wrote there.
func isOurExtraCopy(path string) bool {
want, err := os.ReadFile(filepath.Join(extraBackupDir(path), extraWritten))
if err != nil {
return false
}
h, err := utils.FileHash(path)
return err == nil && h == string(want)
}
func extraCreated(path string) bool {
_, err := os.Stat(filepath.Join(extraBackupDir(path), "created"))
return err == nil
+96 -26
View File
@@ -8,6 +8,7 @@ import (
"strings"
"skillshare/internal/config"
"skillshare/internal/utils"
)
// Managed import block markers. Claude strips block-level HTML comments before
@@ -22,10 +23,13 @@ type ExtraFile struct {
Source string // absolute path of <source dir>/<file>
Target string // <target path>/<as or file>
Mode string // merge (default), symlink, copy, or import
linkFallback bool // Mode is copy because file links are unavailable
}
// NewExtraFile resolves the source and target files of a single-file extra.
// merge and symlink both link the one file.
// merge and symlink both link the one file, or copy it when file links are
// unavailable (see ExtraTargetMode).
func NewExtraFile(sourceDir, file, targetDir, as, mode string) ExtraFile {
if as == "" {
as = file
@@ -34,7 +38,8 @@ func NewExtraFile(sourceDir, file, targetDir, as, mode string) ExtraFile {
if abs, err := filepath.Abs(src); err == nil {
src = abs
}
return ExtraFile{Source: src, Target: filepath.Join(targetDir, as), Mode: EffectiveMode(mode)}
m := ExtraTargetMode(mode, true)
return ExtraFile{Source: src, Target: filepath.Join(targetDir, as), Mode: m, linkFallback: m != EffectiveMode(mode)}
}
// DiscoverExtraSource returns the source files of an extra relative to
@@ -82,7 +87,11 @@ func SyncExtraFile(f ExtraFile, dryRun bool, projectRoot string) (*ExtraResult,
case "import":
return syncExtraImport(f, dryRun)
case "merge", "symlink", "copy":
return syncExtraFileReplace(f, dryRun, projectRoot)
result, err := syncExtraFileReplace(f, dryRun, projectRoot)
if err == nil && f.linkFallback {
result.Warnings = append(result.Warnings, FileLinkFallbackWarning)
}
return result, err
default:
return nil, fmt.Errorf("unsupported extras sync mode: %q", f.Mode)
}
@@ -99,8 +108,8 @@ func syncExtraFileReplace(f ExtraFile, dryRun bool, projectRoot string) (*ExtraR
case os.IsNotExist(err):
case err != nil:
return nil, fmt.Errorf("failed to inspect target: %w", err)
case info.Mode()&os.ModeSymlink != 0:
if !copyMode && f.isOurLink() {
case utils.IsSymlinkOrJunction(f.Target):
if !copyMode && f.isOurLink() && fileLinkUsable(f.Target) {
dest, _ := os.Readlink(f.Target)
if linkNeedsReformat(dest, relative) && !dryRun {
if err := reformatLink(f.Target, f.Source, relative); err != nil {
@@ -130,9 +139,14 @@ func syncExtraFileReplace(f ExtraFile, dryRun bool, projectRoot string) (*ExtraR
default:
same := contentEqual(f.Source, f.Target)
if copyMode && same {
if !dryRun && attached && !isOurExtraCopy(f.Target) {
_ = recordExtraWritten(f.Target) // best effort: only saves a later drift backup
}
result.Synced = 1
return result, nil
}
// skillshare's own earlier copy, left unedited, is replaced silently.
edited := !same && !isOurExtraCopy(f.Target)
if !dryRun {
// On first attach, back up even an identical file: restoring the
// target later has nothing else to put back once the link is
@@ -143,12 +157,12 @@ func syncExtraFileReplace(f ExtraFile, dryRun bool, projectRoot string) (*ExtraR
return nil, err
}
attached = true
} else if !same {
} else if edited {
if err := backupExtraDrift(f.Target); err != nil {
return nil, err
}
}
if !same {
if edited {
result.Warnings = append(result.Warnings, fmt.Sprintf("backed up %s before replacing it", f.Target))
}
if err := os.Remove(f.Target); err != nil {
@@ -176,6 +190,13 @@ func syncExtraFileReplace(f ExtraFile, dryRun bool, projectRoot string) (*ExtraR
return nil, fmt.Errorf("failed to record created file: %w", err)
}
}
if copyMode {
if err := recordExtraWritten(f.Target); err != nil {
return nil, fmt.Errorf("failed to record copied file: %w", err)
}
} else {
clearExtraWritten(f.Target)
}
result.Synced = 1
return result, nil
}
@@ -207,32 +228,60 @@ func BackupFile(path string) error { return backupExtraFile(path) }
func syncExtraImport(f ExtraFile, dryRun bool) (*ExtraResult, error) {
result := &ExtraResult{Synced: 1}
attached := extraAttached(f.Target)
// A link to the source is left over from symlink mode; writing through it
// would edit the source, so drop it and start a new file.
// would edit the source, so it is replaced by a new file.
ourLink := f.isOurLink()
if ourLink && !dryRun {
if err := os.Remove(f.Target); err != nil {
return nil, fmt.Errorf("failed to remove leftover symlink: %w", err)
var data []byte
var err error
if !ourLink {
data, err = os.ReadFile(f.Target)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to read target: %w", err)
}
}
// An attached copy of the source is left over from copy mode; replace it
// rather than keep it, so restore can put back the attach-time state.
leftoverCopy := !ourLink && extraAttached(f.Target) && contentEqual(f.Source, f.Target)
exists := ourLink || err == nil
rest, others := splitImportBlock(string(data))
data, err := os.ReadFile(f.Target)
created := os.IsNotExist(err) || ourLink || leftoverCopy
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to read target: %w", err)
// A whole-file copy is left over from copy mode: unedited when it is what
// skillshare wrote or, apart from other extras' import lines, the source.
// An edited one still came from copy mode when a written record remains.
var leftoverCopy, editedCopy bool
if !ourLink && exists && attached {
src, _ := os.ReadFile(f.Source)
leftoverCopy = isOurExtraCopy(f.Target) || rest == string(src)
editedCopy = !leftoverCopy && hasExtraWritten(f.Target)
}
if created {
data = nil
var updated string
changed := true
if ourLink || leftoverCopy || editedCopy {
// Rebuild the attach-time file plus the managed import block, so the
// target reads as it did right after the original import attach.
updated = extraRestoreBase(f.Target)
for _, line := range others {
updated, _ = addImportLine(updated, line)
}
updated, _ = addImportLine(updated, f.importLine())
} else {
updated, changed = addImportLine(string(data), f.importLine())
}
updated, changed := addImportLine(string(data), f.importLine())
if !changed || dryRun {
return result, nil
}
if editedCopy {
if err := backupExtraDrift(f.Target); err != nil {
return nil, err
}
result.Warnings = append(result.Warnings, fmt.Sprintf("backed up %s before replacing it", f.Target))
}
if ourLink {
if err := os.Remove(f.Target); err != nil {
return nil, fmt.Errorf("failed to remove leftover symlink: %w", err)
}
}
perm := os.FileMode(0644)
if info, statErr := os.Stat(f.Target); statErr == nil {
perm = info.Mode().Perm()
@@ -243,8 +292,9 @@ func syncExtraImport(f ExtraFile, dryRun bool) (*ExtraResult, error) {
if err := os.WriteFile(f.Target, []byte(updated), perm); err != nil {
return nil, fmt.Errorf("failed to write target: %w", err)
}
clearExtraWritten(f.Target)
// A switch from another mode keeps the restore point recorded then.
if created && !extraAttached(f.Target) {
if (!exists || ourLink) && !attached {
if err := markExtraCreated(f.Target); err != nil {
return nil, fmt.Errorf("failed to record created file: %w", err)
}
@@ -252,6 +302,26 @@ func syncExtraImport(f ExtraFile, dryRun bool) (*ExtraResult, error) {
return result, nil
}
// splitImportBlock returns content without its managed import block (and the
// blank line addImportLine puts after it), plus the block's import lines.
func splitImportBlock(content string) (rest string, lines []string) {
all := strings.Split(content, "\n")
begin, end := findImportBlock(all)
if begin == -1 {
return content, nil
}
for _, l := range all[begin+1 : end] {
if t := strings.TrimSpace(l); t != "" {
lines = append(lines, t)
}
}
tail := all[end+1:]
if len(tail) > 1 && strings.TrimSpace(tail[0]) == "" {
tail = tail[1:]
}
return strings.Join(append(append([]string{}, all[:begin]...), tail...), "\n"), lines
}
// findImportBlock returns the line indexes of the managed block markers, or
// (-1, -1) when the block is absent.
func findImportBlock(lines []string) (begin, end int) {
@@ -363,13 +433,13 @@ func ExtraFileStatus(f ExtraFile) string {
}
return "drift"
}
if info.Mode()&os.ModeSymlink != 0 {
if f.isOurLink() {
if utils.IsSymlinkOrJunction(f.Target) {
if f.isOurLink() && fileLinkUsable(f.Target) {
return "synced"
}
return "drift"
}
if info.Mode().IsRegular() && !contentEqual(f.Source, f.Target) {
if info.Mode().IsRegular() && !contentEqual(f.Source, f.Target) && !isOurExtraCopy(f.Target) {
return "modified"
}
return "drift"
@@ -391,7 +461,7 @@ func RestoreExtraTarget(f ExtraFile) (bool, error) {
case os.IsNotExist(err):
case err != nil:
return false, fmt.Errorf("failed to inspect target: %w", err)
case f.isOurLink(), f.Mode == "copy" && info.Mode().IsRegular() && contentEqual(f.Source, f.Target):
case f.isOurLink(), f.Mode == "copy" && info.Mode().IsRegular() && (contentEqual(f.Source, f.Target) || isOurExtraCopy(f.Target)):
if err := os.Remove(f.Target); err != nil {
return false, fmt.Errorf("failed to remove target: %w", err)
}
+70
View File
@@ -440,3 +440,73 @@ func TestRestoreExtraTarget_ModeSwitchKeepsUserFile(t *testing.T) {
})
}
}
func TestSyncExtraFile_SwitchBackToImportRestoresUserLines(t *testing.T) {
for _, via := range []string{"copy", "symlink"} {
t.Run("import->"+via+"->import", func(t *testing.T) {
src, tgt := setupExtraFileTest(t, "# agents")
target := filepath.Join(tgt, "CLAUDE.md")
os.WriteFile(target, []byte("mine\n"), 0644)
imp := NewExtraFile(src, "AGENTS.md", tgt, "CLAUDE.md", "import")
SyncExtraFile(imp, false, "")
attached := readFile(t, target)
SyncExtraFile(NewExtraFile(src, "AGENTS.md", tgt, "CLAUDE.md", via), false, "")
if _, err := SyncExtraFile(imp, false, ""); err != nil {
t.Fatal(err)
}
if got := readFile(t, target); got != attached {
t.Fatalf("after switching back = %q, want %q", got, attached)
}
if _, err := RestoreExtraTarget(imp); err != nil {
t.Fatal(err)
}
if got := readFile(t, target); got != "mine\n" {
t.Errorf("restored %q, want the original file", got)
}
})
}
}
func TestSyncExtraFile_SwitchBackToImportBacksUpEditedCopy(t *testing.T) {
src, tgt := setupExtraFileTest(t, "# agents")
target := filepath.Join(tgt, "CLAUDE.md")
os.WriteFile(target, []byte("mine\n"), 0644)
imp := NewExtraFile(src, "AGENTS.md", tgt, "CLAUDE.md", "import")
SyncExtraFile(imp, false, "")
SyncExtraFile(NewExtraFile(src, "AGENTS.md", tgt, "CLAUDE.md", "copy"), false, "")
os.WriteFile(target, []byte("edited in copy mode"), 0644)
if _, err := SyncExtraFile(imp, false, ""); err != nil {
t.Fatal(err)
}
if got := driftBackups(t, target); len(got) != 1 || got[0] != "edited in copy mode" {
t.Errorf("drift = %v, want the edit", got)
}
if got := readFile(t, target); !strings.HasSuffix(got, "\nmine\n") {
t.Errorf("target = %q, want the user lines back", got)
}
}
func TestSyncExtraFile_SwitchBackToImportKeepsOtherImports(t *testing.T) {
src, tgt := setupExtraFileTest(t, "# agents")
os.WriteFile(filepath.Join(src, "TEAM.md"), []byte("# team"), 0644)
target := filepath.Join(tgt, "CLAUDE.md")
os.WriteFile(target, []byte("mine\n"), 0644)
a := NewExtraFile(src, "AGENTS.md", tgt, "CLAUDE.md", "import")
b := NewExtraFile(src, "TEAM.md", tgt, "CLAUDE.md", "import")
SyncExtraFile(a, false, "")
SyncExtraFile(b, false, "")
SyncExtraFile(NewExtraFile(src, "AGENTS.md", tgt, "CLAUDE.md", "copy"), false, "")
SyncExtraFile(b, false, "")
if _, err := SyncExtraFile(a, false, ""); err != nil {
t.Fatal(err)
}
got := readFile(t, target)
if len(ManagedImportLines(got)) != 4 || !strings.Contains(got, a.ImportLine()) || !strings.Contains(got, b.ImportLine()) || !strings.HasSuffix(got, "\nmine\n") {
t.Errorf("target = %q, want both imports in one block and the user lines", got)
}
}
+128
View File
@@ -0,0 +1,128 @@
package sync
import (
"os"
"path/filepath"
"skillshare/internal/utils"
)
// FileLinkFallbackWarning is reported when a mode that links single files
// syncs copies instead.
const FileLinkFallbackWarning = "file links need Windows Developer Mode; copying instead"
// canCreateFileLink reports whether this system can create symlinks to files.
// Windows junctions only work for directories, so without Developer Mode file
// link modes fall back to copy. A variable so tests can simulate that.
var canCreateFileLink = platformCanCreateFileLink
// CanCreateFileLink reports whether this system can create symlinks to files
// (false on Windows without Developer Mode). The probe runs once.
func CanCreateFileLink() bool { return canCreateFileLink() }
// SetFileLinksForTest overrides whether file links can be created and returns
// a func that restores the probe. Only for tests in other packages.
func SetFileLinksForTest(ok bool) (restore func()) {
prev := canCreateFileLink
canCreateFileLink = func() bool { return ok }
return func() { canCreateFileLink = prev }
}
// EffectiveAgentMode returns the mode agents actually sync with. Merge links
// each agent file, so it copies when file links are unavailable; symlink mode
// links the whole directory and is unaffected.
func EffectiveAgentMode(mode string) string {
m := EffectiveMode(mode)
if m == "merge" && !canCreateFileLink() {
return "copy"
}
return m
}
// ExtraTargetMode returns the mode an extras target actually syncs with.
// Merge links each file, and symlink mode of a single-file extra links that
// file, so both copy when file links are unavailable. Symlink mode of a
// directory extra links the directory and is unaffected.
func ExtraTargetMode(mode string, singleFile bool) string {
m := EffectiveMode(mode)
if !canCreateFileLink() && (m == "merge" || (singleFile && m == "symlink")) {
return "copy"
}
return m
}
// copyTracker records files copied in place of file links in the target's
// manifest, so later syncs can tell them from the user's own files. A tracked
// file that still matches its recorded hash belongs to skillshare and may be
// updated, relinked, or pruned; an edited one is left alone.
type copyTracker struct {
dir string
m *Manifest
changed bool
}
func loadCopyTracker(dir string) *copyTracker {
m, err := ReadManifest(dir)
if err != nil {
m = &Manifest{Managed: make(map[string]string), Mtimes: make(map[string]int64)}
}
return &copyTracker{dir: dir, m: m}
}
// owns reports whether rel is a tracked copy the user has not edited.
func (t *copyTracker) owns(rel string) bool {
sum, ok := t.m.Managed[filepath.ToSlash(rel)]
if !ok {
return false
}
h, err := utils.FileHash(filepath.Join(t.dir, rel))
return err == nil && h == sum
}
func (t *copyTracker) record(rel string) {
key := filepath.ToSlash(rel)
if h, err := utils.FileHash(filepath.Join(t.dir, rel)); err == nil && t.m.Managed[key] != h {
t.m.Managed[key] = h
t.changed = true
}
}
func (t *copyTracker) forget(rel string) {
key := filepath.ToSlash(rel)
if _, ok := t.m.Managed[key]; ok {
delete(t.m.Managed, key)
t.changed = true
}
}
// pruneOrphans removes tracked copies whose name is not in expected, keeping
// edited ones, and forgets them. It returns the removed names.
func (t *copyTracker) pruneOrphans(expected map[string]bool, dryRun bool) []string {
var removed []string
for key := range t.m.Managed {
rel := filepath.FromSlash(key)
if expected[rel] {
continue
}
if t.owns(rel) {
if !dryRun {
os.Remove(filepath.Join(t.dir, rel))
}
removed = append(removed, rel)
}
if !dryRun {
t.forget(rel)
}
}
return removed
}
func (t *copyTracker) save() error {
if !t.changed {
return nil
}
if len(t.m.Managed) == 0 {
return RemoveManifest(t.dir)
}
return WriteManifest(t.dir, t.m)
}
+239
View File
@@ -0,0 +1,239 @@
package sync
import (
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/resource"
)
// withoutFileLinks simulates Windows without Developer Mode.
func withoutFileLinks(t *testing.T) {
t.Helper()
t.Cleanup(SetFileLinksForTest(false))
}
func isRegularFile(t *testing.T, path string) bool {
t.Helper()
info, err := os.Lstat(path)
return err == nil && info.Mode().IsRegular()
}
func agentFixture(t *testing.T) (src, tgt string, agents []resource.DiscoveredResource) {
t.Helper()
src, tgt = t.TempDir(), t.TempDir()
os.WriteFile(filepath.Join(src, "tutor.md"), []byte("v1"), 0644)
agents = []resource.DiscoveredResource{{FlatName: "tutor.md", AbsPath: filepath.Join(src, "tutor.md")}}
return src, tgt, agents
}
func TestSyncAgents_MergeCopiesAndUpdatesWithoutFileLinks(t *testing.T) {
withoutFileLinks(t)
src, tgt, agents := agentFixture(t)
target := filepath.Join(tgt, "tutor.md")
if _, err := SyncAgents(agents, src, tgt, "merge", false, false); err != nil {
t.Fatal(err)
}
os.WriteFile(agents[0].AbsPath, []byte("v2"), 0644)
res, err := SyncAgents(agents, src, tgt, "merge", false, false)
if err != nil {
t.Fatal(err)
}
if !isRegularFile(t, target) || readFile(t, target) != "v2" || len(res.Updated) != 1 {
t.Errorf("target = %q regular=%v, result = %+v; want an updated copy", readFile(t, target), isRegularFile(t, target), res)
}
}
func TestSyncAgents_MergeWithoutFileLinksReplacesLinkWithCopy(t *testing.T) {
withoutFileLinks(t)
src, tgt, agents := agentFixture(t)
target := filepath.Join(tgt, "tutor.md")
os.Symlink(agents[0].AbsPath, target)
if _, err := SyncAgents(agents, src, tgt, "merge", false, false); err != nil {
t.Fatal(err)
}
if !isRegularFile(t, target) || readFile(t, agents[0].AbsPath) != "v1" {
t.Error("link to the source file was not replaced by a copy")
}
}
func TestSyncAgents_MergeWithoutFileLinksKeepsLocalAgents(t *testing.T) {
withoutFileLinks(t)
src, tgt, agents := agentFixture(t)
os.WriteFile(filepath.Join(tgt, "tutor.md"), []byte("mine"), 0644)
os.WriteFile(filepath.Join(tgt, "local.md"), []byte("local"), 0644)
res, err := SyncAgents(agents, src, tgt, "merge", false, false)
if err != nil {
t.Fatal(err)
}
PruneOrphanAgentLinks(tgt, agents, false)
if readFile(t, filepath.Join(tgt, "tutor.md")) != "mine" || readFile(t, filepath.Join(tgt, "local.md")) != "local" || len(res.Skipped) != 1 {
t.Errorf("local agents changed; result = %+v", res)
}
}
func TestPruneOrphanAgentLinks_RemovesTrackedCopies(t *testing.T) {
withoutFileLinks(t)
src, tgt, agents := agentFixture(t)
SyncAgents(agents, src, tgt, "merge", false, false)
removed, _ := PruneOrphanAgentLinks(tgt, nil, false)
if _, err := os.Stat(filepath.Join(tgt, "tutor.md")); len(removed) != 1 || !os.IsNotExist(err) {
t.Errorf("removed = %v, want the orphaned copy pruned", removed)
}
}
func TestSyncAgents_MergeRelinksCopiesOnceFileLinksWork(t *testing.T) {
src, tgt, agents := agentFixture(t)
restore := SetFileLinksForTest(false)
SyncAgents(agents, src, tgt, "merge", false, false)
restore()
if _, err := SyncAgents(agents, src, tgt, "merge", false, false); err != nil {
t.Fatal(err)
}
if isRegularFile(t, filepath.Join(tgt, "tutor.md")) {
t.Error("copy was not relinked after file links became available")
}
}
func TestSyncExtraFile_LinkModesCopyWithoutFileLinks(t *testing.T) {
withoutFileLinks(t)
for _, mode := range []string{"merge", "symlink"} {
t.Run(mode, func(t *testing.T) {
src, tgt := setupExtraFileTest(t, "# agents")
f := NewExtraFile(src, "AGENTS.md", tgt, "", mode)
res, err := SyncExtraFile(f, false, "")
if err != nil {
t.Fatal(err)
}
target := filepath.Join(tgt, "AGENTS.md")
if f.Mode != "copy" || !isRegularFile(t, target) || readFile(t, target) != "# agents" || len(res.Warnings) != 1 {
t.Errorf("mode = %q, regular = %v, warnings = %v; want a copy", f.Mode, isRegularFile(t, target), res.Warnings)
}
})
}
}
func TestSyncExtraFile_WithoutFileLinksReplacesLinkWithCopy(t *testing.T) {
withoutFileLinks(t)
src, tgt := setupExtraFileTest(t, "# agents")
target := filepath.Join(tgt, "AGENTS.md")
os.Symlink(filepath.Join(src, "AGENTS.md"), target)
f := NewExtraFile(src, "AGENTS.md", tgt, "", "merge")
if _, err := SyncExtraFile(f, false, ""); err != nil {
t.Fatal(err)
}
if !isRegularFile(t, target) || ExtraFileStatus(f) != "synced" {
t.Errorf("regular = %v, status = %q; want a synced copy", isRegularFile(t, target), ExtraFileStatus(f))
}
}
func TestSyncExtra_MergeCopiesUpdatesAndPrunesWithoutFileLinks(t *testing.T) {
withoutFileLinks(t)
src, tgt := setupExtrasTest(t, map[string]string{"a.md": "v1", "b.md": "b"})
if _, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(src, "a.md"), []byte("v2"), 0644)
os.Remove(filepath.Join(src, "b.md"))
res, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
if err != nil {
t.Fatal(err)
}
_, bErr := os.Stat(filepath.Join(tgt, "b.md"))
if readFile(t, filepath.Join(tgt, "a.md")) != "v2" || !os.IsNotExist(bErr) || res.Pruned != 1 {
t.Errorf("a.md = %q, b.md err = %v, result = %+v; want updated and pruned copies", readFile(t, filepath.Join(tgt, "a.md")), bErr, res)
}
}
func TestRestoreExtraTarget_WithoutFileLinksPutsBackUserFile(t *testing.T) {
withoutFileLinks(t)
src, tgt := setupExtraFileTest(t, "# agents")
target := filepath.Join(tgt, "AGENTS.md")
os.WriteFile(target, []byte("mine"), 0644)
f := NewExtraFile(src, "AGENTS.md", tgt, "", "merge")
SyncExtraFile(f, false, "")
if _, err := RestoreExtraTarget(f); err != nil {
t.Fatal(err)
}
if got := readFile(t, target); got != "mine" {
t.Errorf("restored %q, want the pre-attach file", got)
}
}
func TestSyncExtraFile_CopyReplacesOwnStaleCopySilently(t *testing.T) {
for _, mode := range []string{"copy", "merge"} { // merge copies without file links
t.Run(mode, func(t *testing.T) {
withoutFileLinks(t)
src, tgt := setupExtraFileTest(t, "v1")
target := filepath.Join(tgt, "AGENTS.md")
f := NewExtraFile(src, "AGENTS.md", tgt, "", mode)
SyncExtraFile(f, false, "")
os.WriteFile(filepath.Join(src, "AGENTS.md"), []byte("v2"), 0644)
res, err := SyncExtraFile(f, false, "")
if err != nil {
t.Fatal(err)
}
for _, w := range res.Warnings {
if strings.Contains(w, "backed up") {
t.Errorf("unexpected warning %q", w)
}
}
if readFile(t, target) != "v2" || len(driftBackups(t, target)) != 0 {
t.Errorf("target = %q, drift = %v; want v2 and no drift backup", readFile(t, target), driftBackups(t, target))
}
})
}
}
func TestSyncExtraFile_CopyStillBacksUpUserEdit(t *testing.T) {
src, tgt := setupExtraFileTest(t, "v1")
target := filepath.Join(tgt, "AGENTS.md")
f := NewExtraFile(src, "AGENTS.md", tgt, "", "copy")
SyncExtraFile(f, false, "")
os.WriteFile(target, []byte("edited"), 0644)
if _, err := SyncExtraFile(f, false, ""); err != nil {
t.Fatal(err)
}
if got := driftBackups(t, target); len(got) != 1 || got[0] != "edited" {
t.Errorf("drift = %v, want the edit", got)
}
}
func TestSyncedAgentCopies_CountsOnlyCurrentCopies(t *testing.T) {
withoutFileLinks(t)
src, tgt, agents := agentFixture(t)
SyncAgents(agents, src, tgt, "merge", false, false)
if n := SyncedAgentCopies(tgt, agents); n != 1 {
t.Fatalf("after sync = %d, want 1", n)
}
os.WriteFile(agents[0].AbsPath, []byte("v2"), 0644)
if n := SyncedAgentCopies(tgt, agents); n != 0 {
t.Errorf("stale copy counted: %d, want 0", n)
}
}
+7
View File
@@ -27,6 +27,13 @@ func createLink(linkPath, sourcePath string, relative bool) error {
// canCreateRelativeLink returns true on Unix where os.Symlink always works.
func canCreateRelativeLink() bool { return true }
// platformCanCreateFileLink returns true on Unix where os.Symlink always works.
func platformCanCreateFileLink() bool { return true }
// fileLinkUsable reports whether a link to a file source can be read through.
// Unix symlinks always can; only Windows junctions cannot.
func fileLinkUsable(string) bool { return true }
// isJunctionOrSymlink checks if path is a symlink
func isJunctionOrSymlink(path string) bool {
info, err := os.Lstat(path)
+50 -7
View File
@@ -11,11 +11,14 @@ import (
"path/filepath"
"strings"
gosync "sync"
"skillshare/internal/utils"
)
// createLink creates a directory junction on Windows (no admin required).
// If relative is true, first tries os.Symlink with a relative path
// (requires Developer Mode). Falls back to junction with absolute paths.
// A file source only gets a symlink: a junction to a file cannot be read.
func createLink(linkPath, sourcePath string, relative bool) error {
absSource, err := filepath.Abs(sourcePath)
if err != nil {
@@ -26,12 +29,14 @@ func createLink(linkPath, sourcePath string, relative bool) error {
return fmt.Errorf("failed to resolve target path: %w", err)
}
if _, err := os.Stat(absSource); os.IsNotExist(err) {
srcInfo, statErr := os.Stat(absSource)
if os.IsNotExist(statErr) {
return fmt.Errorf("source directory does not exist: %s", absSource)
}
isFile := statErr == nil && !srcInfo.IsDir()
if info, err := os.Lstat(absTarget); err == nil {
if info.Mode()&os.ModeSymlink != 0 {
if utils.IsLinkMode(absTarget, info.Mode()) {
return fmt.Errorf("target already exists as a junction/symlink: %s", absTarget)
}
return fmt.Errorf("target already exists: %s", absTarget)
@@ -51,6 +56,13 @@ func createLink(linkPath, sourcePath string, relative bool) error {
}
}
if isFile {
if symlinkErr := os.Symlink(absSource, absTarget); symlinkErr == nil {
return nil
}
return fmt.Errorf("failed to create file link\n symlink: requires Administrator or Developer Mode\n target: %s\n source: %s", absTarget, absSource)
}
// Try junction (no admin required, but requires absolute paths)
var stderr bytes.Buffer
cmd := exec.Command("cmd", "/c", "mklink", "/J", absTarget, absSource)
@@ -103,12 +115,43 @@ func canCreateRelativeLink() bool {
return relativeProbe.ok
}
// isJunctionOrSymlink checks if path is a junction or symlink
func isJunctionOrSymlink(path string) bool {
// platformCanCreateFileLink probes whether the OS can create file symlinks,
// which requires Developer Mode (or Administrator) on Windows.
var fileLinkProbe struct {
once gosync.Once
ok bool
}
func platformCanCreateFileLink() bool {
fileLinkProbe.once.Do(func() {
dir, err := os.MkdirTemp("", "ss-fileprobe-*")
if err != nil {
return
}
defer os.RemoveAll(dir)
target := filepath.Join(dir, "t")
if err := os.WriteFile(target, nil, 0644); err != nil {
return
}
fileLinkProbe.ok = os.Symlink(target, filepath.Join(dir, "l")) == nil
})
return fileLinkProbe.ok
}
// fileLinkUsable reports whether a link to a file source can be read through:
// it must be a real symlink resolving to a file. A junction (never a symlink
// on Go 1.23+) to a file looks right to Readlink but cannot be opened.
func fileLinkUsable(path string) bool {
info, err := os.Lstat(path)
if err != nil {
if err != nil || info.Mode()&os.ModeSymlink == 0 {
return false
}
// Both junctions and symlinks have ModeSymlink on Windows
return info.Mode()&os.ModeSymlink != 0
target, err := os.Stat(path)
return err == nil && !target.IsDir()
}
// isJunctionOrSymlink checks if path is a junction or symlink. Since Go 1.23
// only symlinks have ModeSymlink; junctions are reported as ModeIrregular.
func isJunctionOrSymlink(path string) bool {
return utils.IsSymlinkOrJunction(path)
}
+8 -4
View File
@@ -8,6 +8,7 @@ import (
"skillshare/internal/config"
"skillshare/internal/resource"
ssync "skillshare/internal/sync"
"skillshare/internal/utils"
)
const defaultAgentMode = "merge"
@@ -108,6 +109,9 @@ func (b *Builder) buildSummary(targetName, path, displayPath string, ac config.R
if mode == "" {
mode = defaultAgentMode
}
if ac.Extension == "" {
mode = ssync.EffectiveAgentMode(mode)
}
summary := &AgentSummary{
Path: path,
@@ -130,7 +134,7 @@ func (b *Builder) buildSummary(targetName, path, displayPath string, ac config.R
if b.sourceExists {
summary.ExpectedCount = len(expectedAgents)
}
summary.ManagedCount = countManagedAgents(path, mode, b.sourcePath, summary.ExpectedCount)
summary.ManagedCount = countManagedAgents(path, mode, b.sourcePath, summary.ExpectedCount, expectedAgents)
// Extension output is managed, not local, even when it keeps the .md name.
if ac.Extension == "" {
summary.LocalCount = countLocalAgents(path, b.sourcePath)
@@ -139,11 +143,11 @@ func (b *Builder) buildSummary(targetName, path, displayPath string, ac config.R
return summary, nil
}
func countManagedAgents(targetPath, mode, sourcePath string, expectedCount int) int {
func countManagedAgents(targetPath, mode, sourcePath string, expectedCount int, agents []resource.DiscoveredResource) int {
switch mode {
case "copy":
_, managed, _ := ssync.CheckStatusCopy(targetPath)
return managed
return managed + ssync.SyncedAgentCopies(targetPath, agents)
case "symlink":
if ssync.CheckStatus(targetPath, sourcePath) == ssync.StatusLinked {
return expectedCount
@@ -168,7 +172,7 @@ func countHealthyAgentLinks(dir string) int {
if !strings.HasSuffix(strings.ToLower(entry.Name()), ".md") {
continue
}
if entry.Type()&os.ModeSymlink == 0 {
if !utils.IsLinkMode(filepath.Join(dir, entry.Name()), entry.Type()) {
continue
}
if _, err := os.Stat(filepath.Join(dir, entry.Name())); err == nil {
+162
View File
@@ -0,0 +1,162 @@
# Windows E2E for file links (skills = junctions, file modes = symlink or copy fallback).
# Runs one skillshare binary in an isolated home under -Root and writes a report to -Out.
# Usage: powershell -NoProfile -ExecutionPolicy Bypass -File e2e-file-links.ps1 `
# -Exe C:\Users\Public\sstest\ss.exe -Root C:\Users\Public\sstest\run1 -Out C:\Users\Public\sstest\run1.txt [-Extended]
# -Extended adds: source edit, agent prune, broken-junction repair, idempotent resync, status/doctor/list.
# The last line of -Out is "DONE" when the script finishes.
param(
[Parameter(Mandatory)] [string]$Exe,
[Parameter(Mandatory)] [string]$Root,
[Parameter(Mandatory)] [string]$Out,
[switch]$Extended
)
$ErrorActionPreference = 'Continue'
try { [Console]::OutputEncoding = [Text.Encoding]::UTF8 } catch {}
$OutputEncoding = [Text.Encoding]::UTF8
function Log([string]$s) { $s | Out-File -Encoding utf8 -Append $Out }
function Section([string]$s) { Log ""; Log "=== $s ===" }
function Run([string[]]$CliArgs) {
Log "> skillshare $($CliArgs -join ' ')"
$res = & $Exe @CliArgs 2>&1 | ForEach-Object { "$_" }
$script:LastOut = ($res | Out-String)
Log $script:LastOut.TrimEnd()
Log "rc=$LASTEXITCODE"
}
function DriftCount {
@(Get-ChildItem -Recurse -File -Filter *.bak "$env:APPDATA\skillshare\extras\backups" -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like '*\drift\*' }).Count
}
function Inspect([string]$p) {
$i = Get-Item -LiteralPath $p -Force -ErrorAction SilentlyContinue
if (-not $i) { Log "$p : MISSING"; return }
$fs = (cmd /c "fsutil reparsepoint query `"$p`" 2>&1") | Out-String
$tag = if ($fs -match '0x[0-9a-fA-F]{8}') { $Matches[0] } else { 'none' }
$read = 'n/a'
if ($p -like '*.md') {
try { $c = Get-Content -LiteralPath $p -Raw -ErrorAction Stop; $read = "yes: " + ($c -replace "`r?`n", ' | ').Trim() }
catch { $read = "NO: " + $_.Exception.Message }
}
Log ("{0}`n LinkType={1} Target={2} PSIsContainer={3} Attributes={4}`n reparseTag={5} read={6}" -f `
$p, $i.LinkType, ($i.Target -join ','), $i.PSIsContainer, $i.Attributes, $tag, $read)
}
# --- isolation ---
if (Test-Path $Root) { Remove-Item -LiteralPath $Root -Recurse -Force }
$H = Join-Path $Root 'home'
$env:USERPROFILE = $H; $env:HOME = $H
$env:APPDATA = "$H\AppData\Roaming"; $env:LOCALAPPDATA = "$H\AppData\Local"
$env:TEMP = "$Root\tmp"; $env:TMP = "$Root\tmp"
foreach ($v in 'XDG_CONFIG_HOME','XDG_DATA_HOME','XDG_STATE_HOME','XDG_CACHE_HOME','SKILLSHARE_CONFIG') { Remove-Item "env:$v" -ErrorAction SilentlyContinue }
$env:NO_COLOR = '1'
New-Item -ItemType Directory -Force $env:APPDATA, $env:LOCALAPPDATA, $env:TEMP | Out-Null
Set-Location $H
Section 'environment'
Log "exe=$Exe"
Log "whoami=$(whoami)"
Log "arch=$env:PROCESSOR_ARCHITECTURE os=$([Environment]::OSVersion.VersionString)"
$priv = (whoami /priv | Select-String 'SeCreateSymbolicLinkPrivilege') -join ''
Log ("SeCreateSymbolicLinkPrivilege: " + $(if ($priv) { $priv.Trim() } else { 'absent' }))
$pt = "$Root\tmp\probe-target.txt"; 'x' | Out-File $pt
try { New-Item -ItemType SymbolicLink -Path "$Root\tmp\probe-link.txt" -Target $pt -ErrorAction Stop | Out-Null; Log 'file symlink probe: CREATED' }
catch { Log "file symlink probe: FAILED ($($_.Exception.Message.Trim()))" }
# --- fixture ---
$S = "$H\src"
New-Item -ItemType Directory -Force "$S\skills\demo-skill", "$S\agents", "$S\extras\rules", "$S\extras\personal", "$H\.claude", "$H\.codex", "$env:APPDATA\skillshare" | Out-Null
"---`nname: demo-skill`ndescription: Demo skill`n---`nskill body`n" | Set-Content -NoNewline "$S\skills\demo-skill\SKILL.md"
"---`nname: demo-agent`ndescription: Demo agent`n---`nagent v1`n" | Set-Content -NoNewline "$S\agents\demo-agent.md"
"rule v1`n" | Set-Content -NoNewline "$S\extras\rules\style.md"
"shared v1`n" | Set-Content -NoNewline "$S\extras\personal\AGENTS.md"
@"
source: '$S\skills'
agents_source: '$S\agents'
extras_source: '$S\extras'
mode: merge
targets:
claude:
skills:
path: '$H\.claude\skills'
agents:
path: '$H\.claude\agents'
extras:
- name: rules
targets:
- path: '$H\.claude\rules'
- name: personal
file: AGENTS.md
targets:
- path: '$H\.codex'
"@ | Set-Content "$env:APPDATA\skillshare\config.yaml"
Section 'isolation check (doctor Config line)'
$doc = & $Exe doctor 2>&1 | ForEach-Object { "$_" }
$cfgLine = ($doc | Select-String 'Config:' | Select-Object -First 1) -join ''
Log $cfgLine.Trim()
if ($cfgLine -notlike "*$Root*") { Log 'ABORT: config is not under the test root'; Log 'DONE'; exit 1 }
$paths = @("$H\.claude\skills\demo-skill", "$H\.claude\agents\demo-agent.md", "$H\.claude\rules\style.md", "$H\.codex\AGENTS.md")
$manifests = @("$H\.claude\skills", "$H\.claude\agents", "$H\.claude\rules", "$H\.codex")
function InspectAll {
foreach ($p in $paths) { Inspect $p }
foreach ($d in $manifests) { Log ("manifest {0}: {1}" -f $d, (Test-Path "$d\.skillshare-manifest.json")) }
}
Section 'sync --all'
Run @('sync', '--all')
Section 'state after first sync'
InspectAll
if ($Extended) {
Section '(a) edit shared AGENTS.md source twice, sync each time'
$driftBefore = DriftCount
$backedUp = $false
foreach ($v in 'shared v1b', 'shared v2') {
"$v`n" | Set-Content -NoNewline "$S\extras\personal\AGENTS.md"
Run @('sync', '--all')
if ($script:LastOut -match 'backed up') { $backedUp = $true }
}
Inspect "$H\.codex\AGENTS.md"
Log "drift backups before/after source edits: $driftBefore/$(DriftCount)"
Log "backed-up message after source edits: $backedUp"
Section '(b) remove source agent; untracked user agent must stay'
"user agent`n" | Set-Content -NoNewline "$H\.claude\agents\my-own.md"
Remove-Item "$S\agents\demo-agent.md"
Run @('sync', '--all')
Inspect "$H\.claude\agents\demo-agent.md"
Inspect "$H\.claude\agents\my-own.md"
Log ("manifest agents: " + $(if (Test-Path "$H\.claude\agents\.skillshare-manifest.json") { Get-Content -Raw "$H\.claude\agents\.skillshare-manifest.json" } else { 'absent' }))
Section '(c) broken junction repair'
Remove-Item -LiteralPath "$H\.codex\AGENTS.md" -Force
cmd /c "mklink /J `"$H\.codex\AGENTS.md`" `"$S\extras\personal\AGENTS.md`"" 2>&1 | ForEach-Object { Log "$_" }
Inspect "$H\.codex\AGENTS.md"
Run @('sync', '--all')
Inspect "$H\.codex\AGENTS.md"
Section '(d) skills resync is a no-op'
$before = (Get-Item -LiteralPath "$H\.claude\skills\demo-skill" -Force).CreationTimeUtc.Ticks
Run @('sync')
Run @('sync')
$after = (Get-Item -LiteralPath "$H\.claude\skills\demo-skill" -Force).CreationTimeUtc.Ticks
Log "skill junction recreated: $($before -ne $after)"
Inspect "$H\.claude\skills\demo-skill"
Section '(e) re-add agent, then status / doctor / list / extras list'
"---`nname: demo-agent`ndescription: Demo agent`n---`nagent v3`n" | Set-Content -NoNewline "$S\agents\demo-agent.md"
Run @('sync', 'agents')
Inspect "$H\.claude\agents\demo-agent.md"
Log "extras backups:"
Get-ChildItem -Recurse -File "$env:APPDATA\skillshare\extras\backups" -ErrorAction SilentlyContinue | ForEach-Object { Log " $($_.FullName)" }
Run @('status')
Log ("status agents line: " + (($script:LastOut -split "`r?`n" | Select-String '^\s*agents\s') -join ' || ').Trim())
Run @('doctor')
Log ("doctor agents line: " + (($script:LastOut -split "`r?`n" | Select-String '^\s*agents\s') -join ' || ').Trim())
Run @('list', '--all', '--no-tui')
Run @('extras', 'list', '--no-tui')
}
Log ''
Log 'DONE'
+13
View File
@@ -101,6 +101,19 @@ Do not abort the whole runbook after the first failed step. Preserve every step
- Clean exact `/tmp` targets at the beginning of a step.
- Never assume a repository name equals an installed skill name; verify with `ss list --json`.
## Windows Verification
Linux tests cannot show Windows link behavior (junctions, file symlinks, Developer Mode). For changes to links, sync modes, or paths on Windows, run `ai_docs/tests/windows_file_links_runbook.md` on a real Windows machine; its script is `scripts/windows/e2e-file-links.ps1`.
- Cross-compile in the devcontainer (`GOOS=windows`, `GOARCH` matching the guest). The host drives a UTM guest with `utmctl` (`file push`, `exec`, `file pull`); this is the one allowed host-side execution path, and it never runs the product on macOS.
- `utmctl exec` is asynchronous and runs as SYSTEM. Scripts write a report ending in `DONE`, and the host polls it with `file pull`, which exits 0 even when the file is missing.
- SYSTEM and administrators can create file symlinks. Run as the desktop user through a scheduled task, and use `runas /trustlevel:0x20000` to test without symlink rights.
- Isolate under `C:\Users\Public\sstest\` by overriding `USERPROFILE`, `HOME`, `APPDATA`, `LOCALAPPDATA`, and `TEMP`; confirm the config path before any mutating command.
- Inspect results with `fsutil reparsepoint query` (`0xa0000003` junction, `0xa000000c` symlink) and by reading the file.
- Clean up with `rmdir /s /q`, which does not follow junctions, and unregister the scheduled tasks.
Report which token (full or basic-user) and architecture each result came from.
## Frontend and Website Verification
Start the dashboard through the devcontainer `ui` command. Run website package commands according to `website/AGENTS.md`. Visual changes require inspected screenshots in addition to builds and tests. Load `frontend` for design-specific checks.