import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { bundledSkillsDir, EXTENSION_PREAMBLE, LEGACY_SKILL_NAMES, loadSkill, resolveSkills, type SkillEntries, type SkillInputs } from "./skills-manifest.js"; function writeSkill(root: string, name: string, description: string, body = "body", extra = ""): string { const dir = join(root, name); mkdirSync(dir, { recursive: true }); writeFileSync(join(dir, "SKILL.md"), `---\nname: ${name}\ndescription: ${description}\n${extra}---\n\n${body}\n`); return dir; } describe("skills", () => { let root: string; let cwd: string; let global: string; const saved: Record = {}; beforeEach(() => { root = realpathSync(mkdtempSync(join(tmpdir(), "pi-pi-skills-"))); cwd = join(root, "project"); global = join(root, "global"); mkdirSync(join(cwd, ".pi", "skills"), { recursive: true }); mkdirSync(global, { recursive: true }); for (const key of ["PI_SKILLS_DIR", "HOME", "PI_CODING_AGENT_DIR"]) saved[key] = process.env[key]; process.env.PI_SKILLS_DIR = global; }); afterEach(() => { for (const [key, value] of Object.entries(saved)) { if (value === undefined) delete process.env[key]; else process.env[key] = value; } rmSync(root, { recursive: true, force: true }); }); const skills = (options: Partial & { global?: SkillEntries; project?: SkillEntries } = {}) => resolveSkills({ cwd, trusted: options.trusted ?? true, toggles: options.toggles, entries: { global: options.global, project: options.project }, }); const catalogNames = (set: ReturnType) => set.catalog.map((skill) => skill.name); const projectSkills = () => join(cwd, ".pi", "skills"); // Every bundled skill's description sits in the system prompt of every // session, so the catalog is deliberately short and this list is the gate: // adding to it should be a decision, not a side effect of adding a file. it("ships a focused general-purpose catalog under pi-pi's own names", () => { const set = skills(); const bundled = set.catalog.filter((skill) => skill.layer === "bundled"); expect(bundled.map((skill) => skill.name)).toEqual([ "pp-bug-work", "pp-code-rules", "pp-feature-work", "pp-git-work", "pp-investigate", "pp-ops-work", "pp-review", "pp-skill-work", ]); // Only guidance a worker can follow is shareable: the rest tells its reader // to delegate, ask the user or commission reviews. expect(bundled.filter((skill) => skill.audience === "any").map((skill) => skill.name)).toEqual(["pp-code-rules"]); expect(bundled.every((skill) => skill.base.filePath.startsWith(bundledSkillsDir()))).toBe(true); const text = (name: string) => loadSkill(set, name).document; expect(text("pp-code-rules")).toContain("## After every edit"); expect(text("pp-git-work")).toContain("conventional-commit type"); expect(text("pp-skill-work")).toContain("Never recommend, adapt, or merge a skill whose body you have not fetched"); expect(text("pp-review")).toContain("Zero findings is a valid result"); expect(text("pp-feature-work")).toContain("Most features are problems someone has already solved"); expect(text("pp-bug-work")).toContain("Reproduce before you theorise"); expect(text("pp-ops-work")).toContain("Ask before anything destructive"); expect(text("pp-investigate")).toContain("Read the source, not your memory of it"); }); // A skill that names another must name one that exists, under its new name: // a stale reference sends the agent after a skill it cannot load. it("names only skills that exist, in pi-pi's skills and prompts", () => { const names = new Set(skills().catalog.map((skill) => skill.name)); const files: string[] = []; const walk = (dir: string) => { for (const entry of readdirSync(dir)) { const path = join(dir, entry); if (statSync(path).isDirectory()) walk(path); else if (path.endsWith(".md")) files.push(path); } }; walk(bundledSkillsDir()); const sources = join(dirname(bundledSkillsDir()), "agents", "tool-routing.ts"); for (const file of [...files, sources, join(dirname(bundledSkillsDir()), "event-handlers.ts")]) { const text = readFileSync(file, "utf8"); for (const [, name] of text.matchAll(/`(pp-[a-z-]+)`/g)) expect(names, `${file}: ${name}`).toContain(name); for (const [, list] of text.matchAll(/skills: \[([^\]]*)\]/g)) { for (const [, name] of list!.matchAll(/"([a-z-]+)"/g)) expect(names, `${file}: ${name}`).toContain(name); } for (const legacy of Object.keys(LEGACY_SKILL_NAMES)) { expect(text, `${file} still names \`${legacy}\``).not.toMatch(new RegExp(`\`${legacy}\`|"${legacy}"\\]`)); } } }); it("resolves project over global over pi-pi's, and a layer turned off hides nothing", () => { writeSkill(global, "same", "global version"); writeSkill(projectSkills(), "same", "project version"); const set = skills(); expect(set.byName.get("same")?.description).toBe("project version"); expect(set.found.get("same")?.shadows).toEqual([{ layer: "global", filePath: join(global, "same", "SKILL.md") }]); expect(set.issues).toContainEqual(expect.objectContaining({ kind: "shadowed", skill: "same" })); expect(skills({ toggles: { loadBundled: true, loadGlobal: true, loadProject: false } }).byName.get("same")?.description).toBe("global version"); }); it("reads pi's skill folders: the agent folder, ~/.agents/skills and the old ~/.pi/skills, nearest project folder first", () => { delete process.env.PI_SKILLS_DIR; const home = join(root, "home"); process.env.HOME = home; process.env.PI_CODING_AGENT_DIR = join(home, ".pi", "agent"); writeSkill(join(home, ".pi", "agent", "skills"), "a", "agent folder"); writeSkill(join(home, ".agents", "skills"), "a", "shared folder"); writeSkill(join(home, ".agents", "skills"), "b", "shared folder"); writeSkill(join(home, ".pi", "skills"), "c", "old folder"); // A project's .agents folders count up to the repository's root. mkdirSync(join(root, ".git")); cwd = join(root, "repo", "sub"); writeSkill(join(root, ".agents", "skills"), "d", "repo root"); writeSkill(join(root, "repo", ".agents", "skills"), "d", "nearer"); const set = skills(); expect(set.byName.get("a")?.description).toBe("agent folder"); expect(set.byName.get("b")?.layer).toBe("global"); expect(set.byName.get("c")?.description).toBe("old folder"); expect(set.byName.get("d")).toMatchObject({ layer: "project", description: "nearer" }); }); it("reads only /SKILL.md in .agents folders, and keeps ~/.agents/skills global when working in the home directory", () => { delete process.env.PI_SKILLS_DIR; const home = join(root, "home"); process.env.HOME = home; process.env.PI_CODING_AGENT_DIR = join(home, ".pi", "agent"); writeSkill(join(home, ".agents", "skills"), "mine", "mine"); writeFileSync(join(home, ".agents", "skills", "README.md"), "---\nname: readme\ndescription: not a skill here\n---\n"); writeSkill(join(home, ".pi", "skills"), "old", "old folder"); cwd = home; const set = skills({ trusted: false }); expect(set.byName.get("mine")?.layer).toBe("global"); expect(set.byName.get("old")?.layer).toBe("global"); expect(set.byName.has("readme")).toBe(false); expect(set.issues.some((issue) => issue.kind === "untrusted")).toBe(false); }); it("keeps a project's link to a shared folder a project folder", () => { delete process.env.PI_SKILLS_DIR; const home = join(root, "home"); process.env.HOME = home; process.env.PI_CODING_AGENT_DIR = join(home, ".pi", "agent"); writeSkill(join(home, ".agents", "skills"), "shared", "from the shared folder"); writeSkill(join(home, ".pi", "agent", "skills"), "shared", "from the agent folder"); rmSync(projectSkills(), { recursive: true }); symlinkSync(join(home, ".agents", "skills"), projectSkills()); expect(skills().byName.get("shared")).toMatchObject({ layer: "project", description: "from the shared folder" }); expect(skills({ toggles: { loadBundled: true, loadGlobal: false, loadProject: true } }).byName.has("shared")).toBe(true); }); it("reads only regular files, whatever a SKILL.md links to", () => { mkdirSync(join(global, "device")); symlinkSync("/dev/zero", join(global, "device", "SKILL.md")); symlinkSync("/dev/zero", join(global, "zero.md")); expect(skills().found.has("device")).toBe(false); }); it("stops walking a folder that links to a huge tree, and says so", () => { const big = join(root, "big"); for (let i = 0; i < 2100; i++) mkdirSync(join(big, `d${i}`), { recursive: true }); symlinkSync(big, join(global, "big")); writeSkill(global, "after", "after"); const set = skills(); expect(set.issues).toContainEqual(expect.objectContaining({ kind: "limit" })); }); it("says why a SKILL.md was ignored", () => { writeFileSync(join(global, "SKILL.md.bak"), ""); mkdirSync(join(global, "Bad_Name")); writeFileSync(join(global, "Bad_Name", "SKILL.md"), "---\ndescription: d\n---\n"); mkdirSync(join(global, "empty")); writeFileSync(join(global, "empty", "SKILL.md"), "---\nname: empty\n---\n"); mkdirSync(join(global, "broken")); writeFileSync(join(global, "broken", "SKILL.md"), "---\nname: broken\ndescription: a: b: c\n---\n"); const issues = skills().issues; const messages = issues.filter((issue) => issue.kind === "invalid-skill").map((issue) => issue.message); expect(messages).toEqual(expect.arrayContaining([expect.stringContaining('"Bad_Name" is not a valid skill name'), expect.stringContaining("it has no description")])); expect(issues).toContainEqual(expect.objectContaining({ kind: "unreadable", message: expect.stringMatching(/broken\/SKILL\.md: .+/) })); }); it("stops at the working directory outside a repository", () => { writeSkill(join(root, ".agents", "skills"), "outside", "above the project, no repository"); writeSkill(join(cwd, ".agents", "skills"), "inside", "in the project"); const set = skills(); expect(set.byName.has("outside")).toBe(false); expect(set.byName.has("inside")).toBe(true); }); it("finds skills as pi does: flat files, nested folders, not hidden ones or node_modules", () => { writeFileSync(join(global, "flat.md"), "---\nname: flat\ndescription: flat skill\n---\n\nFlat.\n"); writeSkill(join(global, "vendor"), "nested", "nested skill"); writeSkill(join(global, ".hidden"), "hidden-dir", "hidden"); writeSkill(join(global, "node_modules"), "dep", "dependency"); const names = catalogNames(skills()); expect(names).toEqual(expect.arrayContaining(["flat", "nested"])); expect(names).not.toContain("hidden-dir"); expect(names).not.toContain("dep"); }); it("follows skills linked in from elsewhere, once each, with their references where they live", () => { const shared = join(root, "shared"); const real = writeSkill(shared, "linked", "linked skill"); symlinkSync(real, join(global, "linked")); symlinkSync(real, join(projectSkills(), "linked-again")); // A link back up must not loop. symlinkSync(global, join(real, "loop")); const set = skills(); const skill = set.byName.get("linked")!; expect(skill.layer).toBe("project"); expect(set.found.get("linked")?.shadows).toEqual([]); expect(loadSkill(set, "linked").document).toContain(`dir="${real}"`); }); it("reads a folder reached twice through a link once", () => { delete process.env.PI_SKILLS_DIR; const home = join(root, "home"); process.env.HOME = home; process.env.PI_CODING_AGENT_DIR = join(home, ".pi", "agent"); writeSkill(join(home, ".agents", "skills"), "mine", "mine"); mkdirSync(join(home, ".pi"), { recursive: true }); symlinkSync(join(home, ".agents", "skills"), join(home, ".pi", "skills")); const set = skills(); expect(set.found.get("mine")?.shadows).toEqual([]); expect(set.issues.filter((issue) => issue.kind === "shadowed")).toEqual([]); }); it("keeps the pp- prefix for pi-pi's own skills", () => { writeSkill(global, "pp-review", "an impostor"); const set = skills(); expect(set.byName.get("pp-review")?.layer).toBe("bundled"); expect(set.issues).toContainEqual(expect.objectContaining({ kind: "reserved-name", skill: "pp-review" })); }); it("ignores a project's skills and skill settings while pi does not trust it", () => { writeSkill(projectSkills(), "local", "project skill"); writeSkill(global, "mine", "global skill"); const set = skills({ trusted: false, project: { mine: { extends: "pp-review" } } }); expect(set.byName.has("local")).toBe(false); expect(set.byName.get("mine")?.base.name).toBe("mine"); expect(set.issues.filter((issue) => issue.kind === "untrusted")).toHaveLength(2); }); it("adds extensions to the end of a pi-pi skill, the user's marked as winning, global settings first", () => { writeSkill(global, "zz-global", "global addition", "Global rules."); writeSkill(global, "aa-project", "project addition", "Project rules."); const set = skills({ global: { "zz-global": { extends: "pp-review" } }, project: { "aa-project": { extends: "pp-review" } } }); expect(catalogNames(set)).not.toContain("zz-global"); expect(catalogNames(set)).not.toContain("aa-project"); const document = loadSkill(set, "pp-review").document; expect(document.indexOf("Zero findings")).toBeLessThan(document.indexOf("Global rules.")); expect(document.indexOf("Global rules.")).toBeLessThan(document.indexOf("Project rules.")); expect(document).toContain(`\n${EXTENSION_PREAMBLE}`); // Asked for by an extension's own name, the agent gets the whole skill. expect(loadSkill(set, "aa-project").document).toBe(document); }); it("puts a replacement in pi-pi's place, under its own name, with the extensions on top", () => { writeSkill(global, "my-git", "my git rules", "My git."); writeSkill(global, "more-git", "more git", "More git."); const set = skills({ global: { "my-git": { replaces: "pp-git-work" }, "more-git": { extends: "pp-git-work" } } }); expect(catalogNames(set)).toContain("my-git"); expect(catalogNames(set)).not.toContain("pp-git-work"); expect(set.byName.get("my-git")).toMatchObject({ replaces: "pp-git-work", description: "my git rules" }); const document = loadSkill(set, "pp-git-work").document; expect(document).toMatch(/^ { writeSkill(global, "one", "one"); writeSkill(global, "two", "two"); const projectWins = skills({ global: { one: { replaces: "pp-git-work" } }, project: { two: { replaces: "pp-git-work" } } }); expect(projectWins.byName.get("pp-git-work")?.base.name).toBe("two"); expect(projectWins.issues).toContainEqual(expect.objectContaining({ kind: "replacement-conflict", skill: "one" })); const tie = skills({ global: { one: { replaces: "pp-git-work" }, two: { replaces: "pp-git-work" } } }); expect(tie.byName.get("pp-git-work")?.base.name).toBe("pp-git-work"); expect(tie.issues).toContainEqual(expect.objectContaining({ kind: "replacement-conflict", skill: "pp-git-work" })); // The skills that lost are skills of their own again. expect(catalogNames(tie)).toEqual(expect.arrayContaining(["one", "two"])); }); it("replaces a skill's global entry whole with the project's, and null cancels it", () => { writeSkill(global, "mine", "mine"); expect(skills({ global: { mine: { extends: "pp-review", standalone: true } }, project: { mine: { enabled: true } } }).byName.get("mine")?.base.name).toBe("mine"); const cancelled = skills({ global: { mine: { extends: "pp-review" } }, project: { mine: null } }); expect(cancelled.entries.has("mine")).toBe(false); expect(catalogNames(cancelled)).toContain("mine"); }); it("turns single skills off: a pi-pi skill with its extensions, or a replacement, which brings pi-pi's back", () => { writeSkill(global, "addition", "addition"); writeSkill(global, "stand-in", "stand-in"); const off = skills({ global: { "pp-review": { enabled: false }, addition: { extends: "pp-review" } } }); expect(catalogNames(off)).not.toContain("pp-review"); expect(off.issues).toContainEqual(expect.objectContaining({ kind: "disabled-target", skill: "addition" })); expect(() => loadSkill(off, "pp-review")).toThrow("turned off"); expect(() => loadSkill(off, "review")).toThrow("turned off"); expect(() => loadSkill(off, "addition")).toThrow(/not in use: addition extends pp-review, which is turned off/); const back = skills({ global: { "stand-in": { replaces: "pp-git-work", enabled: false } } }); expect(back.byName.get("pp-git-work")?.base.name).toBe("pp-git-work"); expect(back.byName.has("stand-in")).toBe(false); // Off means off: a replacement does not bring a turned-off skill back. const offReplaced = skills({ global: { "pp-git-work": { enabled: false }, "stand-in": { replaces: "pp-git-work" } } }); expect(offReplaced.byName.has("pp-git-work")).toBe(false); expect(offReplaced.byName.has("stand-in")).toBe(false); expect(offReplaced.issues).toContainEqual(expect.objectContaining({ kind: "disabled-target", skill: "stand-in" })); }); it("does not let a link into pi-pi's own folder take a skill away", () => { symlinkSync(join(bundledSkillsDir(), "pp-review"), join(global, "pp-review")); symlinkSync(bundledSkillsDir(), join(projectSkills(), "all")); const set = skills(); expect(set.byName.get("pp-review")?.layer).toBe("bundled"); expect(catalogNames(set).filter((name) => name.startsWith("pp-"))).toHaveLength(8); // Nor a whole global folder that is pi-pi's, and it is not reported as impostors. process.env.PI_SKILLS_DIR = bundledSkillsDir(); const whole = skills(); expect(catalogNames(whole).filter((name) => name.startsWith("pp-"))).toHaveLength(8); expect(whole.issues.filter((issue) => issue.kind === "reserved-name")).toEqual([]); }); it("keeps a hidden replacement out of the catalog, still standing in for pi-pi's", () => { writeSkill(global, "quiet-git", "quiet git", "Quiet git.", "disable-model-invocation: true\n"); const set = skills({ global: { "quiet-git": { replaces: "pp-git-work" } } }); expect(catalogNames(set)).not.toContain("quiet-git"); expect(catalogNames(set)).not.toContain("pp-git-work"); expect(loadSkill(set, "pp-git-work").document).toContain("Quiet git."); }); it("reports settings it cannot apply, and applies none of them", () => { writeSkill(global, "both", "both"); writeSkill(global, "other", "other"); writeSkill(global, "chain", "chain"); const set = skills({ global: { both: { extends: "pp-review", replaces: "pp-git-work" }, chain: { extends: "other" }, "pp-review": { extends: "pp-git-work" }, } }); expect(set.issues.filter((issue) => issue.kind === "invalid-entry").map((issue) => issue.skill).sort()).toEqual(["both", "chain", "pp-review"]); expect(catalogNames(set)).toEqual(expect.arrayContaining(["both", "chain", "pp-review"])); // With pi-pi's skills off the link is not wrong, just unused. const off = skills({ toggles: { loadBundled: false, loadGlobal: true, loadProject: true }, global: { other: { extends: "pp-review" } } }); expect(off.issues).toContainEqual(expect.objectContaining({ kind: "disabled-target", skill: "other", message: expect.stringContaining("pi-pi's skills are turned off") })); }); it("shares a linked skill with subagents as pi-pi's skill is, and an unlinked one as set", () => { writeSkill(global, "house-rules", "house rules"); writeSkill(global, "notes", "notes"); const set = skills({ global: { "house-rules": { extends: "pp-code-rules" }, notes: { audience: "any" } } }); expect(set.byName.get("house-rules")).toMatchObject({ name: "pp-code-rules", audience: "any" }); expect(set.byName.get("notes")?.audience).toBe("any"); }); it("keeps old names working until a skill of the user's takes one", () => { const set = skills(); expect(loadSkill(set, "code-rules")).toMatchObject({ alias: "code-rules", skill: { name: "pp-code-rules" } }); writeSkill(global, "review", "my own review"); const taken = skills(); expect(taken.aliases.has("review")).toBe(false); expect(loadSkill(taken, "review").document).toContain(' { writeSkill(global, "review", "my own review"); expect(skills().issues).toContainEqual(expect.objectContaining({ kind: "legacy-name", skill: "review" })); expect(skills({ global: { review: { standalone: true } } }).issues.some((issue) => issue.kind === "legacy-name")).toBe(false); expect(skills({ global: { review: { extends: "pp-review" } }, project: { review: null } }).issues.some((issue) => issue.kind === "legacy-name")).toBe(false); }); it("keeps a skill marked disable-model-invocation out of the catalog, loadable by name and as an extension", () => { writeSkill(global, "quiet", "quiet one", "Quiet.", "disable-model-invocation: true\n"); const set = skills(); expect(catalogNames(set)).not.toContain("quiet"); expect(loadSkill(set, "quiet").document).toContain("Quiet."); expect(loadSkill(skills({ global: { quiet: { extends: "pp-review" } } }), "pp-review").document).toContain("Quiet."); }); it("reloads edited guidance, ignores invalid metadata, and lists names for an unknown one", () => { writeSkill(global, "edited", "edited", "First."); expect(loadSkill(skills(), "edited").document).toContain("First."); writeSkill(global, "edited", "edited", "Second."); expect(loadSkill(skills(), "edited").document).toContain("Second."); writeFileSync(join(global, "Bad Name.md"), "---\nname: Bad Name\ndescription: invalid\n---\n"); writeFileSync(join(global, "nodesc.md"), "---\nname: nodesc\n---\n"); const set = skills(); expect(catalogNames(set)).not.toContain("Bad Name"); expect(catalogNames(set)).not.toContain("nodesc"); expect(() => loadSkill(set, "missing")).toThrow(/Unknown skill "missing"\. Available skills: .*pp-review/); }); });