diff --git a/CHANGELOG.md b/CHANGELOG.md index 5299e266..352ced06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to `sessionpipe-gui`. The format follows ## [Unreleased] +### Fixed + +- Link-only skill installs and harness operations preserve the library source folder when paths resolve to the same skill. + ## [0.8.18] - 2026-12-04 ### Fixed diff --git a/server/ui/skills.linkonly.test.ts b/server/ui/skills.linkonly.test.ts new file mode 100644 index 00000000..a482698e --- /dev/null +++ b/server/ui/skills.linkonly.test.ts @@ -0,0 +1,265 @@ +// SPDX-License-Identifier: Apache-2.0 +import { + existsSync, + lstatSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { + adoptLocalSkill, + discoverLocalSkills, + installLocalSkillFromDir, + removeLocalSkill, + skillsStateFile, + unlinkLocalSkill, +} from "./skillsCore.js"; + +const tmps: string[] = []; +function makeTmp(): string { + const d = mkdtempSync(path.join(os.tmpdir(), "sp-gui-skills-link-")); + tmps.push(d); + return d; +} + +afterEach(() => { + for (const d of tmps.splice(0)) rmSync(d, { recursive: true, force: true }); +}); + +function writeSkill(dir: string, slug: string): string { + mkdirSync(dir, { recursive: true }); + writeFileSync(path.join(dir, "SKILL.md"), `---\nname: ${slug}\ndescription: Desc for ${slug}\n---\nBody.\n`); + return dir; +} + +// Symlinks need extra rights on Windows, so these run on macOS and Linux only. +describe.skipIf(process.platform === "win32")("skillsCore link-only install onto the library copy", () => { + function library(): { env: { home: string; env: Record }; canon: string } { + const home = makeTmp(); + const canon = writeSkill(path.join(home, ".agents", "skills", "foo"), "foo"); + writeFileSync(path.join(canon, "notes.md"), "keep me\n"); + return { env: { home, env: {} }, canon }; + } + function expectIntact(canon: string): void { + expect(lstatSync(canon).isSymbolicLink()).toBe(false); + expect(readFileSync(path.join(canon, "notes.md"), "utf8")).toBe("keep me\n"); + expect(existsSync(path.join(canon, "SKILL.md"))).toBe(true); + } + const linkOnly = { harnesses: [], linkOnly: true, overwrite: true } as const; + // State an earlier release or run could leave: the skill recorded as a managed copy in Claude Code. + function writeClaudeCopyRecord(env: { home: string; env: Record }): void { + const stateFile = skillsStateFile(env); + mkdirSync(path.dirname(stateFile), { recursive: true }); + const now = new Date().toISOString(); + const rec = { + slug: "foo", + source: "managed", + installedAt: now, + updatedAt: now, + hash: "x", + managedCopies: { "claude-code": "x" }, + }; + writeFileSync(stateFile, JSON.stringify({ v: 1, collections: [], skills: { foo: rec } })); + } + + it("refuses the library folder itself and keeps its files", () => { + const { env, canon } = library(); + expect(() => installLocalSkillFromDir(canon, { ...env, ...linkOnly })).toThrow( + /refusing to link skill "foo" to itself/, + ); + expectIntact(canon); + }); + + it("refuses a harness link that points at the library folder", () => { + const { env, canon } = library(); + const harnessLink = path.join(env.home, ".claude", "skills", "foo"); + mkdirSync(path.dirname(harnessLink), { recursive: true }); + symlinkSync(canon, harnessLink, "dir"); + expect(() => installLocalSkillFromDir(harnessLink, { ...env, ...linkOnly })).toThrow(/to itself/); + expectIntact(canon); + expect(discoverLocalSkills(env).skills.map((s) => s.slug)).toEqual(["foo"]); + }); + + it("refuses a folder inside the library folder", () => { + const { env, canon } = library(); + const inner = writeSkill(path.join(canon, "inner"), "foo"); + expect(() => installLocalSkillFromDir(inner, { ...env, slug: "foo", ...linkOnly })).toThrow(/to itself/); + expectIntact(canon); + }); + + it("refuses the library folder spelled in another case on a case-insensitive disk", (ctx) => { + const { env, canon } = library(); + const upper = path.join(path.dirname(canon), "FOO"); + if (!existsSync(upper)) ctx.skip(); + expect(() => installLocalSkillFromDir(upper, { ...env, ...linkOnly })).toThrow(/to itself/); + expectIntact(canon); + }); + + it("links the real folder when the library entry is already a link and the source goes through it", () => { + const { env, canon } = library(); + const outside = writeSkill(path.join(env.home, "src", "foo"), "foo"); + writeFileSync(path.join(outside, "notes.md"), "outside\n"); + installLocalSkillFromDir(outside, { ...env, ...linkOnly }); + const harnessLink = path.join(env.home, ".claude", "skills", "foo"); + mkdirSync(path.dirname(harnessLink), { recursive: true }); + symlinkSync(canon, harnessLink, "dir"); + for (const source of [canon, harnessLink]) { + installLocalSkillFromDir(source, { ...env, ...linkOnly }); + expect(readFileSync(path.join(canon, "notes.md"), "utf8")).toBe("outside\n"); + expect(discoverLocalSkills(env).skills.map((s) => s.slug)).toEqual(["foo"]); + } + }); + + it("refuses a source that holds the skills library", () => { + const { env, canon } = library(); + const agents = writeSkill(path.join(env.home, ".agents"), "foo"); + expect(() => installLocalSkillFromDir(agents, { ...env, slug: "foo", ...linkOnly })).toThrow( + /holds the skills library/, + ); + expectIntact(canon); + }); + + it("keeps a harness folder that the library entry links to when harnesses are forced", () => { + const home = makeTmp(); + const env = { home, env: {} }; + const harnessFolder = writeSkill(path.join(home, ".claude", "skills", "foo"), "foo"); + writeFileSync(path.join(harnessFolder, "notes.md"), "keep me\n"); + installLocalSkillFromDir(harnessFolder, { + ...env, + harnesses: ["claude-code"], + linkOnly: true, + forceHarnesses: true, + }); + expect(lstatSync(harnessFolder).isSymbolicLink()).toBe(false); + expect(readFileSync(path.join(harnessFolder, "notes.md"), "utf8")).toBe("keep me\n"); + expect(discoverLocalSkills(env).skills.map((s) => s.slug)).toEqual(["foo"]); + }); + + it("matches a harness folder typed in another case, and unlinking it later keeps its files", (ctx) => { + const home = makeTmp(); + const env = { home, env: {} }; + const harnessFolder = writeSkill(path.join(home, ".claude", "skills", "foo"), "foo"); + writeFileSync(path.join(harnessFolder, "notes.md"), "keep me\n"); + const typed = path.join(home, ".Claude", "skills", "foo"); + if (!existsSync(typed)) ctx.skip(); + installLocalSkillFromDir(typed, { ...env, harnesses: ["claude-code"], linkOnly: true, forceHarnesses: true }); + expect(readFileSync(path.join(harnessFolder, "notes.md"), "utf8")).toBe("keep me\n"); + unlinkLocalSkill("foo", ["claude-code"], env); + expect(readFileSync(path.join(harnessFolder, "notes.md"), "utf8")).toBe("keep me\n"); + }); + + it("drops an old managed-copy record for the harness folder it links to, so unlinking keeps its files", () => { + const home = makeTmp(); + const env = { home, env: {} }; + const harnessFolder = writeSkill(path.join(home, ".claude", "skills", "foo"), "foo"); + writeFileSync(path.join(harnessFolder, "notes.md"), "keep me\n"); + writeClaudeCopyRecord(env); + installLocalSkillFromDir(harnessFolder, { + ...env, + harnesses: ["claude-code"], + linkOnly: true, + forceHarnesses: true, + }); + const state = JSON.parse(readFileSync(skillsStateFile(env), "utf8")); + expect(state.skills.foo.managedCopies?.["claude-code"]).toBeUndefined(); + unlinkLocalSkill("foo", ["claude-code"], env); + expect(readFileSync(path.join(harnessFolder, "notes.md"), "utf8")).toBe("keep me\n"); + }); + + it("keeps the harness folder it links to when that harness is not linked and the skill is removed", () => { + const home = makeTmp(); + const env = { home, env: {} }; + const harnessFolder = writeSkill(path.join(home, ".claude", "skills", "foo"), "foo"); + writeFileSync(path.join(harnessFolder, "notes.md"), "keep me\n"); + writeClaudeCopyRecord(env); + installLocalSkillFromDir(harnessFolder, { ...env, harnesses: ["cursor"], linkOnly: true }); + unlinkLocalSkill("foo", ["claude-code"], env); + expect(readFileSync(path.join(harnessFolder, "notes.md"), "utf8")).toBe("keep me\n"); + removeLocalSkill("foo", { ...env, removeCanonical: true }); + expect(lstatSync(harnessFolder).isSymbolicLink()).toBe(false); + expect(readFileSync(path.join(harnessFolder, "notes.md"), "utf8")).toBe("keep me\n"); + }); + + it("keeps the library folder when a harness skills folder is a link to the library", () => { + const { env, canon } = library(); + mkdirSync(path.join(env.home, ".claude"), { recursive: true }); + symlinkSync(path.dirname(canon), path.join(env.home, ".claude", "skills"), "dir"); + writeClaudeCopyRecord(env); + unlinkLocalSkill("foo", ["claude-code"], env); + expectIntact(canon); + }); + + it("keeps a harness skill folder that holds the linked source, when linking and when removing", () => { + const home = makeTmp(); + const env = { home, env: {} }; + const harnessFolder = writeSkill(path.join(home, ".claude", "skills", "foo"), "foo"); + const inner = writeSkill(path.join(harnessFolder, "inner"), "foo"); + writeFileSync(path.join(inner, "notes.md"), "keep me\n"); + writeClaudeCopyRecord(env); + installLocalSkillFromDir(inner, { + ...env, + slug: "foo", + harnesses: ["claude-code"], + linkOnly: true, + forceHarnesses: true, + }); + expect(readFileSync(path.join(inner, "notes.md"), "utf8")).toBe("keep me\n"); + removeLocalSkill("foo", { ...env, removeCanonical: true }); + expect(readFileSync(path.join(inner, "notes.md"), "utf8")).toBe("keep me\n"); + }); + + it("keeps a library link when a harness skills folder is a link to the library", () => { + const { env } = library(); + const outside = writeSkill(path.join(env.home, "src", "foo"), "foo"); + installLocalSkillFromDir(outside, { ...env, ...linkOnly }); + mkdirSync(path.join(env.home, ".claude"), { recursive: true }); + symlinkSync(path.join(env.home, ".agents", "skills"), path.join(env.home, ".claude", "skills"), "dir"); + unlinkLocalSkill("foo", ["claude-code"], env); + expect(lstatSync(path.join(env.home, ".agents", "skills", "foo")).isSymbolicLink()).toBe(true); + expect(discoverLocalSkills(env).skills.map((s) => s.slug)).toEqual(["foo"]); + }); + + it("adopting a harness folder linked in another case keeps the folder", (ctx) => { + const home = makeTmp(); + const env = { home, env: {} }; + const harnessFolder = writeSkill(path.join(home, ".claude", "skills", "foo"), "foo"); + mkdirSync(path.join(harnessFolder, ".git")); + writeFileSync(path.join(harnessFolder, ".git", "HEAD"), "ref: refs/heads/main\n"); + const typed = path.join(home, ".Claude", "skills", "foo"); + if (!existsSync(typed)) ctx.skip(); + installLocalSkillFromDir(typed, { ...env, harnesses: ["claude-code"], linkOnly: true, forceHarnesses: true }); + adoptLocalSkill("foo", { ...env, fromHarness: "claude-code", harnesses: ["claude-code"], force: true }); + expect(lstatSync(harnessFolder).isSymbolicLink()).toBe(false); + expect(readFileSync(path.join(harnessFolder, ".git", "HEAD"), "utf8")).toBe("ref: refs/heads/main\n"); + }); + + it("adopting a harness folder that holds the linked source keeps the folder", () => { + const home = makeTmp(); + const env = { home, env: {} }; + const harnessFolder = writeSkill(path.join(home, ".claude", "skills", "foo"), "foo"); + mkdirSync(path.join(harnessFolder, ".git")); + writeFileSync(path.join(harnessFolder, ".git", "HEAD"), "ref: refs/heads/main\n"); + const inner = writeSkill(path.join(harnessFolder, "inner"), "foo"); + installLocalSkillFromDir(inner, { ...env, slug: "foo", harnesses: [], linkOnly: true }); + adoptLocalSkill("foo", { ...env, fromHarness: "claude-code", harnesses: ["claude-code"], force: true }); + expect(lstatSync(harnessFolder).isSymbolicLink()).toBe(false); + expect(readFileSync(path.join(harnessFolder, ".git", "HEAD"), "utf8")).toBe("ref: refs/heads/main\n"); + }); + + it("still replaces a library folder with a link to a folder outside it", () => { + const { env, canon } = library(); + const outside = writeSkill(path.join(env.home, "src", "foo"), "foo"); + installLocalSkillFromDir(outside, { ...env, ...linkOnly }); + expect(lstatSync(canon).isSymbolicLink()).toBe(true); + expect(existsSync(path.join(canon, "notes.md"))).toBe(false); + // Linking again to the same outside folder through the existing link still works. + installLocalSkillFromDir(outside, { ...env, ...linkOnly }); + expect(discoverLocalSkills(env).skills.map((s) => s.slug)).toEqual(["foo"]); + }); +}); diff --git a/server/ui/skillsCore.ts b/server/ui/skillsCore.ts index 018146a5..66088e6b 100644 --- a/server/ui/skillsCore.ts +++ b/server/ui/skillsCore.ts @@ -642,7 +642,7 @@ export function linkLocalSkill( const state = readState(opts); const rec = ensureRecord(state, slug, readSymlinkTarget(canonPath) ? "symlink" : "managed", scanned.meta.hash); const platform = opts?.platform ?? process.platform; - const canonReal = safeRealpath(canonPath); + const canonReal = nativeRealpath(canonPath); const defaultCanon = path.join(resolveHome(opts), ".agents", "skills"); const isDefaultCanon = path.resolve(canonDir) === path.resolve(defaultCanon); @@ -655,8 +655,15 @@ export function linkLocalSkill( } const dest = path.join(target.skillsDir, slug); const destSym = readSymlinkTarget(dest); - const destReal = safeRealpath(dest); - if (destSym && canonReal && destReal === canonReal) continue; + const destReal = nativeRealpath(dest); + // Already in place: the harness entry is a link to the library folder, or the library entry is a + // link to this harness folder or a folder inside it (a link-only install from there). Removing + // the folder would delete the skill. + if (canonReal && destReal && (destReal === canonReal || (!destSym && isInsideRoot(destReal, canonReal)))) { + // A real folder here holds the library's own source, not a copy this harness manages. + if (!destSym && rec.managedCopies?.[h]) delete rec.managedCopies[h]; + continue; + } if (existsSync(dest) || destSym !== null) { if (!rec.managedCopies?.[h] && !opts?.force) { throw new Error(`refusing to overwrite unmanaged skill at ${dest} (pass force to replace)`); @@ -680,7 +687,9 @@ export function unlinkLocalSkill( opts?: SkillsEnv, ): LocalSkillsSnapshot { if (!isValidSlug(slug)) throw new Error(`invalid skill slug "${slug}"`); - const canonReal = safeRealpath(path.join(canonicalSkillsDir(opts), slug)); + const canonDir = canonicalSkillsDir(opts); + const canonDirReal = nativeRealpath(canonDir); + const canonReal = nativeRealpath(path.join(canonDir, slug)); const targets = harnessTargets(opts); const state = readState(opts); const rec = state.skills[slug]; @@ -691,7 +700,19 @@ export function unlinkLocalSkill( const dest = path.join(target.skillsDir, slug); const destSym = readSymlinkTarget(dest); if (destSym === null && !existsSync(dest)) continue; - const isLinkedToCanon = !!destSym && !!canonReal && safeRealpath(dest) === canonReal; + // The harness skills folder is the library folder itself (a link to it): this is the library entry. + if (canonDirReal && nativeRealpath(target.skillsDir) === canonDirReal) { + if (rec?.managedCopies?.[h]) delete rec.managedCopies[h]; + continue; + } + const destReal = nativeRealpath(dest); + // A real folder the library entry resolves to, or that holds it, is the skill itself (the source + // of a link-only install), not a copy: keep it, and forget a managed-copy record left for it. + if (!destSym && canonReal && destReal && isInsideRoot(destReal, canonReal)) { + if (rec?.managedCopies?.[h]) delete rec.managedCopies[h]; + continue; + } + const isLinkedToCanon = !!destSym && !!canonReal && destReal === canonReal; const isManagedCopy = !!rec?.managedCopies?.[h]; if (!isLinkedToCanon && !isManagedCopy) continue; rmSync(dest, { recursive: true, force: true }); @@ -743,9 +764,11 @@ export function adoptLocalSkill( const canonRoot = canonicalSkillsDir(opts); const canonPath = path.join(canonRoot, slug); mkdirSync(canonRoot, { recursive: true }); - const canonReal = safeRealpath(canonPath); - const foundReal = safeRealpath(foundPath); - const alreadyCanonical = symTarget === canonPath || (canonReal !== null && foundReal === canonReal); + const canonReal = nativeRealpath(canonPath); + const foundReal = nativeRealpath(foundPath); + // The library entry already resolves to this folder or into it (a link-only install from there). + const alreadyCanonical = + symTarget === canonPath || (canonReal !== null && foundReal !== null && isInsideRoot(foundReal, canonReal)); let recordSource: "managed" | "symlink"; let recordTarget: string | undefined; @@ -797,6 +820,16 @@ export function adoptLocalSkill( return discoverLocalSkills(opts); } +// The real path as the OS spells it, for comparing folders. On macOS and Windows that is the case as +// stored on disk; Linux's realpath keeps the case as typed. +function nativeRealpath(p: string): string | null { + try { + return realpathSync.native(p); + } catch { + return safeRealpath(p); + } +} + export function installLocalSkillFromDir( sourceDir: string, opts?: SkillsEnv & { @@ -822,16 +855,38 @@ export function installLocalSkillFromDir( if (destExists && !opts?.overwrite) { throw new Error(`skill "${slug}" already exists (pass overwrite to replace)`); } + let linkTarget = absSource; if (opts?.linkOnly) { + // A link-only install removes the library entry, then links it to the source. When the source + // resolves into that entry (the library folder itself, a folder inside it, or a harness link to + // it), removing a real folder deletes the source, and linking the path as typed leaves a link + // to itself. A source holding the whole library would link a folder into itself. The native + // real path spells each name as the disk does on macOS and Windows, so FOO still matches foo there. + const sourceReal = nativeRealpath(absSource); + const rootReal = nativeRealpath(canonRoot); + const destReal = destExists ? nativeRealpath(dest) : null; + if (sourceReal && rootReal && isInsideRoot(sourceReal, rootReal)) { + throw new Error(`refusing to link skill "${slug}" to ${absSource}: it holds the skills library ${canonRoot}`); + } + if (sourceReal && destReal && isInsideRoot(destReal, sourceReal)) { + if (readSymlinkTarget(dest) === null) { + throw new Error( + `refusing to link skill "${slug}" to itself: ${absSource} resolves to ${sourceReal}, the library folder ${dest} or a folder inside it; link a folder outside the library`, + ); + } + // The entry is already a link to the folder the source resolves to: link that real folder, + // not a path that leads back through the entry. + linkTarget = sourceReal; + } if (destExists) rmSync(dest, { recursive: true, force: true }); - symlinkSync(absSource, dest, (opts?.platform ?? process.platform) === "win32" ? "junction" : "dir"); + symlinkSync(linkTarget, dest, (opts?.platform ?? process.platform) === "win32" ? "junction" : "dir"); } else { writeValidatedFiles(scanned.files, dest); } const state = readState(opts); ensureRecord(state, slug, opts?.linkOnly ? "symlink" : "managed", scanned.meta.hash, { - ...(opts?.linkOnly ? { linkedTarget: absSource } : {}), + ...(opts?.linkOnly ? { linkedTarget: linkTarget } : {}), ...(opts?.collectionId ? { collectionId: opts.collectionId } : {}), }); if (opts?.collectionId) {