Project-mode agent sync re-reported every agent as updated on the second
sync when the project lived under a symlinked ancestor (e.g. macOS temp
dirs under /var -> /private/var), failing idempotency on macOS CI.
Two interacting causes:
- shouldUseRelative was unstable: on the first sync the target directory
does not exist yet, so evalOrClean fell back to a non-resolved path and
the under-project-root check failed, producing an absolute link; on the
second sync the directory existed and resolved, so it wanted a relative
link and reformatted the existing one. evalOrClean now resolves the
deepest existing ancestor and re-appends the missing tail, so the answer
is the same whether or not the leaf exists.
- The existing-symlink comparison only did a direct string compare, so a
stable relative link whose resolved target carried the unresolved alias
prefix looked like it pointed elsewhere. It now canonicalizes both sides
via EvalSymlinks on the slow path, mirroring isSymlinkToSource.
Adds a regression test that reproduces the symlinked-ancestor case on any
platform.
Two correctness improvements from Codex review:
1. EvalSymlinks before relative-link decisions: shouldUseRelative and
createLink now resolve symlinked ancestors via EvalSymlinks so that
a project opened through a symlinked workspace, or a target dir that
traverses a symlink, produces correct relative paths (or falls back
to absolute when paths diverge from the project root).
2. Atomic reformat via reformatLink: instead of remove→create (which
loses the link on createLink failure), reformat now creates a temp
link and renames it over the original. On Unix this is atomic; on
Windows it falls back to remove→create when rename fails.
Tests added for symlinked project root, divergent target symlink,
and reformatLink atomic swap.
On Windows without Developer Mode, createLink falls back to an NTFS
junction (always absolute). linkNeedsReformat would then return true
on every sync, causing a remove→recreate loop.
Add build-tagged canCreateRelativeLink(): returns true on Unix, and
on Windows probes once with sync.Once by creating a test relative
symlink in a temp dir. linkNeedsReformat skips reformat when the
platform cannot create relative links.
Also use filepath.Clean instead of filepath.Abs in resolveReadlink
(input is already absolute, avoids unnecessary os.Getwd).
Addresses PR #122 review feedback.
- shouldUseRelative: avoid double separator when projectRoot is /
- symlink_windows.go: use pre-resolved absTarget/absSource for Rel
- extras.go: check os.Remove error on wrong symlink replacement
filepath.Abs(dest) on a relative readlink result resolves against CWD,
not the symlink's parent directory. This broke the 'already correct'
check when comparing relative symlinks to absolute source paths.
Add resolveReadlink() helper that joins relative targets with
filepath.Dir(linkPath) before calling filepath.Abs.
Also check os.Remove errors in all reformat paths instead of ignoring
them silently.
Addresses PR #122 review feedback.
sync previously skipped symlinks that pointed to the correct source,
even if their format (absolute vs relative) didn't match the expected
mode. This meant existing absolute symlinks in project mode were never
converted to relative on subsequent syncs.
Add linkNeedsReformat(dest, wantRelative) helper that checks whether
the raw readlink result has the wrong format. All four 'already
correct' skip points now check format and recreate when mismatched:
- SyncTargetMergeWithSkills (skills merge mode)
- SyncTarget (skills symlink mode)
- syncExtraSymlinkMode (extras directory symlink)
- syncOneExtraFile (extras per-file merge)
Global mode is unaffected: shouldUseRelative returns false, so
linkNeedsReformat never triggers on absolute links.
Also fix macOS /var → /private/var path mismatch in
TestSyncProject_RelativeSymlinks by EvalSymlinks on expected path.
Determines whether both source and target paths are under the project
root, in which case relative symlinks are portable and can be committed
to git.
Ref: #120