-
Notifications
You must be signed in to change notification settings - Fork 39
feat(skills): validation and shadowing signals #2607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import type { SkillInfo } from "@posthog/shared"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { analyzeSkills, OVERSIZED_SKILL_MD_BYTES } from "./analyzeSkills"; | ||
|
|
||
| function makeSkill(overrides: Partial<SkillInfo>): SkillInfo { | ||
| return { | ||
| name: "my-skill", | ||
| description: "Does a thing", | ||
| source: "user", | ||
| path: "/home/.claude/skills/my-skill", | ||
| editable: true, | ||
| skillMdBytes: 1024, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe("analyzeSkills", () => { | ||
| it("returns no issues for a healthy skill", () => { | ||
| expect(analyzeSkills([makeSkill({})])).toEqual({}); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["empty description", ""], | ||
| ["whitespace description", " "], | ||
| ])("flags a missing description: %s", (_label, description) => { | ||
| const skill = makeSkill({ description }); | ||
|
|
||
| const analysis = analyzeSkills([skill]); | ||
|
|
||
| expect(analysis[skill.path]).toEqual([ | ||
| expect.objectContaining({ type: "missing-description" }), | ||
| ]); | ||
| }); | ||
|
|
||
| it("flags a frontmatter name that differs from the directory name", () => { | ||
| const skill = makeSkill({ | ||
| name: "Pretty Name", | ||
| path: "/home/.claude/skills/my-skill", | ||
| }); | ||
|
|
||
| const analysis = analyzeSkills([skill]); | ||
|
|
||
| expect(analysis[skill.path]).toEqual([ | ||
| expect.objectContaining({ type: "name-mismatch" }), | ||
| ]); | ||
| }); | ||
|
|
||
| it("flags an oversized SKILL.md", () => { | ||
| const skill = makeSkill({ skillMdBytes: OVERSIZED_SKILL_MD_BYTES + 1 }); | ||
|
|
||
| const analysis = analyzeSkills([skill]); | ||
|
|
||
| expect(analysis[skill.path]).toEqual([ | ||
| expect.objectContaining({ type: "oversized-manifest" }), | ||
| ]); | ||
| }); | ||
|
|
||
| it("does not flag SKILL.md exactly at the size limit", () => { | ||
| const skill = makeSkill({ skillMdBytes: OVERSIZED_SKILL_MD_BYTES }); | ||
|
|
||
| expect(analyzeSkills([skill])).toEqual({}); | ||
| }); | ||
|
|
||
| describe("shadowing", () => { | ||
| it.each([ | ||
| ["repo", "user"], | ||
| ["repo", "marketplace"], | ||
| ["repo", "bundled"], | ||
| ["user", "marketplace"], | ||
| ["user", "bundled"], | ||
| ["marketplace", "bundled"], | ||
| ] as const)("%s shadows %s", (winnerSource, loserSource) => { | ||
| const winner = makeSkill({ | ||
| source: winnerSource, | ||
| path: `/roots/${winnerSource}/my-skill`, | ||
| }); | ||
| const loser = makeSkill({ | ||
| source: loserSource, | ||
| path: `/roots/${loserSource}/my-skill`, | ||
| }); | ||
|
|
||
| const analysis = analyzeSkills([loser, winner]); | ||
|
|
||
| expect(analysis[winner.path]).toBeUndefined(); | ||
| expect(analysis[loser.path]).toEqual([ | ||
| expect.objectContaining({ type: "shadowed" }), | ||
| ]); | ||
| }); | ||
|
|
||
| it("does not flag skills with distinct names", () => { | ||
| const a = makeSkill({ name: "alpha", path: "/roots/user/alpha" }); | ||
| const b = makeSkill({ | ||
| name: "beta", | ||
| path: "/roots/repo/beta", | ||
| source: "repo", | ||
| }); | ||
|
|
||
| expect(analyzeSkills([a, b])).toEqual({}); | ||
| }); | ||
|
|
||
| it("flags the later repo when two open repos share a skill name", () => { | ||
| const first = makeSkill({ | ||
| source: "repo", | ||
| repoName: "repo-a", | ||
| path: "/roots/repo-a/.claude/skills/my-skill", | ||
| }); | ||
| const second = makeSkill({ | ||
| source: "repo", | ||
| repoName: "repo-b", | ||
| path: "/roots/repo-b/.claude/skills/my-skill", | ||
| }); | ||
|
|
||
| const analysis = analyzeSkills([first, second]); | ||
|
|
||
| expect(analysis[first.path]).toBeUndefined(); | ||
| expect(analysis[second.path]?.[0]?.message).toContain("repo-a"); | ||
| }); | ||
|
|
||
| it("marks every loser when three sources collide", () => { | ||
| const repo = makeSkill({ source: "repo", path: "/roots/repo/my-skill" }); | ||
| const user = makeSkill({ source: "user", path: "/roots/user/my-skill" }); | ||
| const bundled = makeSkill({ | ||
| source: "bundled", | ||
| path: "/roots/bundled/my-skill", | ||
| editable: false, | ||
| }); | ||
|
|
||
| const analysis = analyzeSkills([bundled, user, repo]); | ||
|
|
||
| expect(analysis[repo.path]).toBeUndefined(); | ||
| expect(analysis[user.path]?.[0]?.message).toContain("repository"); | ||
| expect(analysis[bundled.path]?.[0]?.message).toContain("repository"); | ||
| }); | ||
| }); | ||
|
|
||
| it("accumulates multiple issues on one skill", () => { | ||
| const skill = makeSkill({ | ||
| name: "Other Name", | ||
| description: "", | ||
| skillMdBytes: OVERSIZED_SKILL_MD_BYTES * 2, | ||
| }); | ||
|
|
||
| const analysis = analyzeSkills([skill]); | ||
|
|
||
| expect(analysis[skill.path]?.map((i) => i.type)).toEqual([ | ||
| "missing-description", | ||
| "name-mismatch", | ||
| "oversized-manifest", | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import type { SkillInfo, SkillSource } from "@posthog/shared"; | ||
|
|
||
| export type SkillIssueType = | ||
| | "missing-description" | ||
| | "name-mismatch" | ||
| | "oversized-manifest" | ||
| | "shadowed"; | ||
|
|
||
| export interface SkillIssue { | ||
| type: SkillIssueType; | ||
| message: string; | ||
| } | ||
|
|
||
| /** Issues keyed by skill path. Paths without issues are absent. */ | ||
| export type SkillAnalysis = Record<string, SkillIssue[]>; | ||
|
|
||
| /** SKILL.md is injected into agent context; warn when it gets expensive. */ | ||
| export const OVERSIZED_SKILL_MD_BYTES = 32 * 1024; | ||
|
|
||
| /** | ||
| * Precedence when two skills share a name: the most specific source wins. | ||
| * Repo skills beat user skills beat marketplace plugins beat bundled ones. | ||
| */ | ||
| const SOURCE_PRECEDENCE: SkillSource[] = [ | ||
| "repo", | ||
| "user", | ||
| "marketplace", | ||
| "bundled", | ||
| ]; | ||
|
|
||
| const SOURCE_LABEL: Record<SkillSource, string> = { | ||
| repo: "repository", | ||
| user: "user", | ||
| marketplace: "marketplace", | ||
| bundled: "bundled", | ||
| }; | ||
|
|
||
| function precedence(source: SkillSource): number { | ||
| const index = SOURCE_PRECEDENCE.indexOf(source); | ||
| return index === -1 ? SOURCE_PRECEDENCE.length : index; | ||
| } | ||
|
|
||
| function directoryName(skillPath: string): string { | ||
| return skillPath.split(/[/\\]/).filter(Boolean).pop() ?? skillPath; | ||
| } | ||
|
|
||
| /** Pure health analysis over the discovered skills. No I/O. */ | ||
| export function analyzeSkills(skills: SkillInfo[]): SkillAnalysis { | ||
| const analysis: SkillAnalysis = {}; | ||
| const push = (skill: SkillInfo, issue: SkillIssue) => { | ||
| const issues = analysis[skill.path] ?? []; | ||
| issues.push(issue); | ||
| analysis[skill.path] = issues; | ||
| }; | ||
|
|
||
| for (const skill of skills) { | ||
| if (!skill.description.trim()) { | ||
| push(skill, { | ||
| type: "missing-description", | ||
| message: | ||
| "No description — agents rely on it to decide when to use this skill", | ||
| }); | ||
| } | ||
|
|
||
| const dirName = directoryName(skill.path); | ||
| if (skill.name !== dirName) { | ||
| push(skill, { | ||
| type: "name-mismatch", | ||
| message: `Frontmatter name "${skill.name}" does not match the directory name "${dirName}"`, | ||
| }); | ||
| } | ||
|
|
||
| if (skill.skillMdBytes > OVERSIZED_SKILL_MD_BYTES) { | ||
| push(skill, { | ||
| type: "oversized-manifest", | ||
| message: `SKILL.md is ${Math.round(skill.skillMdBytes / 1024)} kB — large manifests add context cost every time the skill is used`, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const byName = new Map<string, SkillInfo[]>(); | ||
| for (const skill of skills) { | ||
| const group = byName.get(skill.name); | ||
| if (group) group.push(skill); | ||
| else byName.set(skill.name, [skill]); | ||
| } | ||
|
|
||
| for (const [name, group] of byName) { | ||
| if (group.length < 2) continue; | ||
| const sorted = [...group].sort( | ||
| (a, b) => precedence(a.source) - precedence(b.source), | ||
| ); | ||
| const winner = sorted[0]; | ||
| if (!winner) continue; | ||
| const winnerLabel = winner.repoName | ||
| ? `${SOURCE_LABEL[winner.source]} (${winner.repoName})` | ||
| : SOURCE_LABEL[winner.source]; | ||
| for (const shadowed of sorted.slice(1)) { | ||
| push(shadowed, { | ||
| type: "shadowed", | ||
| message: `Shadowed by the ${winnerLabel} skill named "${name}"`, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return analysis; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.