fix(relink): never delete or link over a skill gstack does not own (#2119)

gstack-relink runs on every ./setup. Its cleanup did `rm -rf` on any same-name
entry whose SKILL.md was a symlink, with no readlink check, and its link step
did `mkdir -p` then `ln -snf` onto any existing SKILL.md — on Linux that
replaces a user's real file with a symlink into gstack (macOS refused by
accident). setup's Windows mode-flip cleanup deleted any real dir whose name
matched a gstack skill. A personal `qa` skill, or a fork installed under
another path, was destroyed by the installer of a tool it never asked for.

Ownership is now proven, never assumed. An entry is ours when it is a symlink
resolving into INSTALL_DIR or RENDER_DIR, a real dir whose SKILL.md is such a
symlink, or a real dir carrying the .gstack-owned marker setup now writes for
Windows copy installs (legacy copies count when byte-identical to the source
or carrying gen-skill-docs' AUTO-GENERATED header). Anything else — including
an entry whose readlink fails — is foreign: left untouched, reported on
stderr, and listed in relink's summary line. The same rule replaces setup's
Windows name-match deletion; setup:1040 and gstack-uninstall:204 already
gated on readlink, so this closes the last unguarded deleter of the class.

Tests: foreign real dir in flat mode, foreign flat entry on a prefix flip,
foreign directory symlink, RENDER_DIR-targeted entry (ours), marker-carrying
copy (ours), marker-less copy (foreign); the Windows cleanup test now proves
provenance three ways and keeps the user's own same-name skill.

Idea and two regression cases from PR #2119 (@smblight); implemented on the
destination entry, not only the symlink target.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-09-03 01:18:18 +00:00
co-authored by Claude Fable 5.1
parent 17733302cc
commit 584c2a44fb
4 changed files with 212 additions and 13 deletions
+15 -5
View File
@@ -960,6 +960,10 @@ link_claude_skill_dirs() {
_skill_md_src="$_render_dir/$dir_name/SKILL.md"
fi
_link_or_copy "$_skill_md_src" "$target/SKILL.md"
# Ownership marker for Windows COPY installs (#2119): there is no symlink
# to readlink, so gstack-relink and the mode-flip cleanup below prove
# provenance by this marker instead of by name.
if [ "$IS_WINDOWS" -eq 1 ]; then : > "$target/.gstack-owned" 2>/dev/null || true; fi
# Link every runtime asset the skill ships next to its SKILL.md (#2317,
# #2454): sections/ for carved skills, review's checklist.md +
# specialists/, qa's templates/ + references/, gstack-upgrade's
@@ -1062,10 +1066,13 @@ cleanup_old_claude_symlinks() {
fi
done
# Windows install pattern: real dir with real-file SKILL.md (no symlink
# available, so we can't readlink to verify provenance). Iterate known
# gstack skill names from "$gstack_dir"/*, so a name match plus IS_WINDOWS
# is safe to treat as gstack-managed during a mode flip. When the payload
# is gone this branch is a no-op — a real file has no proven owner.
# available, so we can't readlink to verify provenance). A bare name match
# deleted a user's own same-name skill (#2119); ownership is now proven by
# the .gstack-owned marker link_claude_skill_dirs writes, or — for copies
# made before the marker existed — by the copy being byte-identical to the
# gstack source SKILL.md or carrying gen-skill-docs' AUTO-GENERATED header
# (every generated SKILL.md does; a hand-written skill does not). Anything
# else is foreign and is left alone.
if [ "${IS_WINDOWS:-0}" -eq 1 ] && [ -d "$gstack_dir" ]; then
for skill_dir in "$gstack_dir"/*/; do
if [ -f "$skill_dir/SKILL.md" ]; then
@@ -1074,7 +1081,10 @@ cleanup_old_claude_symlinks() {
case "$skill_name" in gstack-*) continue ;; esac
old_target="$skills_dir/$skill_name"
if [ -d "$old_target" ] && [ ! -L "$old_target" ] \
&& [ -f "$old_target/SKILL.md" ] && [ ! -L "$old_target/SKILL.md" ]; then
&& [ -f "$old_target/SKILL.md" ] && [ ! -L "$old_target/SKILL.md" ] \
&& { [ -f "$old_target/.gstack-owned" ] \
|| cmp -s "$old_target/SKILL.md" "$skill_dir/SKILL.md" \
|| grep -q 'AUTO-GENERATED from SKILL.md.tmpl' "$old_target/SKILL.md" 2>/dev/null; }; then
rm -rf "$old_target"
removed+=("$skill_name")
fi