Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
265 changes: 265 additions & 0 deletions server/ui/skills.linkonly.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> }; 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<string, string> }): 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"]);
});
});
Loading