Skip to content

Commit 8d43a80

Browse files
committed
fix(claude): match native cached plugin skill discovery
1 parent f8b8f5c commit 8d43a80

2 files changed

Lines changed: 132 additions & 42 deletions

File tree

‎apps/server/src/provider/Drivers/ClaudeSkills.test.ts‎

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,107 @@ const writeSkill = Effect.fn(function* (
2020
});
2121

2222
it.layer(NodeServices.layer)("discoverClaudeSkills", (it) => {
23-
it.effect(
24-
"discovers only enabled installed plugin skills with their namespace and metadata",
25-
() =>
23+
it.effect.each(["default", "alternate", "relative alternate", "root named", "root fallback"])(
24+
"discovers enabled installed plugin skills with %s layout",
25+
(layout) =>
2626
Effect.gen(function* () {
2727
const fs = yield* FileSystem.FileSystem;
2828
const path = yield* Path.Path;
2929
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
3030
const configDir = path.join(tempDir, "claude-home");
31-
const plugin = path.join(configDir, "plugins", "cache", "shop", "plugin", "1.0.0");
32-
yield* writeSkill(path.join(plugin, "skills"), "plain", "# Plain");
31+
const workspace = path.join(tempDir, "workspace");
32+
const pluginRoot = layout.includes("alternate")
33+
? path.join(workspace, "alternate")
34+
: path.join(configDir, "plugins");
35+
const environment = layout.includes("alternate")
36+
? { CLAUDE_CODE_PLUGIN_CACHE_DIR: layout === "alternate" ? pluginRoot : "alternate" }
37+
: {};
38+
const plugin = path.join(pluginRoot, "cache", "shop", "plugin", "1.0.0");
39+
const rootSkill = layout.startsWith("root");
40+
const skillsDirectory = rootSkill ? "custom" : "skills";
41+
yield* writeSkill(path.join(plugin, skillsDirectory), "plain", "# Plain");
3342
yield* writeSkill(
34-
path.join(plugin, "skills", "engineering"),
43+
path.join(plugin, skillsDirectory, "engineering"),
3544
"review",
36-
"---\nname: ignored-alias\ndescription: Review changes.\ndisable-model-invocation: yes\nuser-invocable: no\n---\n",
45+
"---\nname: review-alias\ndescription: Review changes.\ndisable-model-invocation: yes\nuser-invocable: no\n---\n",
3746
);
3847
yield* writeSkill(path.join(plugin, "extra"), "deploy", "# Deploy");
48+
yield* writeSkill(plugin, "..safe", "---\nname: safe.name alias_Upper\n---\n");
49+
const outside = path.resolve(plugin, "../outside");
50+
yield* writeSkill(path.dirname(outside), path.basename(outside), "# Outside");
51+
if (path.sep !== "\\") yield* fs.symlink(outside, path.join(plugin, "linked"));
52+
yield* fs.writeFileString(
53+
path.join(plugin, "SKILL.md"),
54+
layout === "root fallback" ? "# Root" : "---\nname: root-alias\n---\n",
55+
);
3956
yield* fs.makeDirectory(path.join(plugin, ".claude-plugin"), { recursive: true });
4057
yield* fs.writeFileString(
4158
path.join(plugin, ".claude-plugin", "plugin.json"),
42-
'{"skills":["./skills/engineering/review","./extra","./skills/plain"]}',
59+
`{"name":"manifest-plugin","skills":[
60+
"./${skillsDirectory}/engineering/review","./extra","./${skillsDirectory}/plain",
61+
"./..safe","./../outside","${outside.replaceAll("\\", "/")}","./linked"
62+
]}`,
4363
);
4464
yield* fs.writeFileString(
45-
path.join(configDir, "plugins", "installed_plugins.json"),
65+
path.join(pluginRoot, "installed_plugins.json"),
4666
`{"version":2,"plugins":{
4767
"plugin@shop":[null,{"scope":"user","installPath":"${plugin.replaceAll("\\", "/")}"}],
4868
"disabled@shop":[{"scope":"user","installPath":"${plugin.replaceAll("\\", "/")}"}],
4969
"unmentioned@shop":[{"scope":"user","installPath":"${plugin.replaceAll("\\", "/")}"}]
5070
}}`,
5171
);
72+
yield* fs.makeDirectory(configDir, { recursive: true });
5273
yield* fs.writeFileString(
5374
path.join(configDir, "settings.json"),
54-
'{ "enabledPlugins": { "plugin@shop": true, "disabled@shop": false, "uninstalled@shop": true }, "skillOverrides": { "plugin:review": "off" } }',
75+
'{ "enabledPlugins": { "plugin@shop": true, "disabled@shop": false, "uninstalled@shop": true }, "skillOverrides": { "manifest-plugin:review-alias": "off" } }',
5576
);
5677

57-
const skills = yield* discoverClaudeSkills({ homePath: configDir });
58-
assert.deepEqual(skills, [
59-
{
60-
name: "plugin:deploy",
61-
path: path.join(plugin, "extra", "deploy", "SKILL.md"),
62-
enabled: true,
63-
scope: "user",
64-
},
65-
{
66-
name: "plugin:plain",
67-
path: path.join(plugin, "skills", "plain", "SKILL.md"),
68-
enabled: true,
69-
scope: "user",
70-
},
71-
{
72-
name: "plugin:review",
73-
path: path.join(plugin, "skills", "engineering", "review", "SKILL.md"),
74-
enabled: false,
75-
scope: "user",
76-
description: "Review changes.",
77-
userInvocationOnly: true,
78-
userInvocable: false,
79-
},
80-
]);
78+
const skills = yield* discoverClaudeSkills({ homePath: configDir }, workspace, environment);
79+
assert.deepEqual(
80+
skills,
81+
[
82+
...(rootSkill
83+
? [
84+
{
85+
name:
86+
layout === "root fallback"
87+
? "manifest-plugin:1-0-0"
88+
: "manifest-plugin:root-alias",
89+
path: path.join(plugin, "SKILL.md"),
90+
enabled: true,
91+
scope: "user",
92+
},
93+
]
94+
: []),
95+
{
96+
name: "manifest-plugin:deploy",
97+
path: path.join(plugin, "extra", "deploy", "SKILL.md"),
98+
enabled: true,
99+
scope: "user",
100+
},
101+
{
102+
name: "manifest-plugin:plain",
103+
path: path.join(plugin, skillsDirectory, "plain", "SKILL.md"),
104+
enabled: true,
105+
scope: "user",
106+
},
107+
{
108+
name: "manifest-plugin:review-alias",
109+
path: path.join(plugin, skillsDirectory, "engineering", "review", "SKILL.md"),
110+
enabled: false,
111+
scope: "user",
112+
description: "Review changes.",
113+
userInvocationOnly: true,
114+
userInvocable: false,
115+
},
116+
{
117+
name: "manifest-plugin:safe.name alias_Upper",
118+
path: path.join(plugin, "..safe", "SKILL.md"),
119+
enabled: true,
120+
scope: "user",
121+
},
122+
].sort((left, right) => left.name.localeCompare(right.name)),
123+
);
81124
}),
82125
);
83126

‎apps/server/src/provider/Drivers/ClaudeSkills.ts‎

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type SkillFrontmatter =
4545
| { readonly kind: "malformed" }
4646
| {
4747
readonly kind: "parsed";
48+
readonly name?: string;
4849
readonly description?: string;
4950
readonly userInvocationOnly?: boolean;
5051
readonly userInvocable?: boolean;
@@ -113,9 +114,11 @@ function parseSkillFrontmatter(contents: string): SkillFrontmatter {
113114
}
114115

115116
const record = parsed as Record<string, unknown>;
117+
const name = typeof record.name === "string" ? record.name.trim() : "";
116118
const description = typeof record.description === "string" ? record.description.trim() : "";
117119
return {
118120
kind: "parsed",
121+
...(name ? { name } : {}),
119122
...(description ? { description } : {}),
120123
...(parseFrontmatterBoolean(record["disable-model-invocation"]) === true
121124
? { userInvocationOnly: true }
@@ -317,6 +320,7 @@ const decodeInstalledPlugin = Schema.decodeUnknownOption(
317320
);
318321
const PluginManifest = Schema.fromJsonString(
319322
Schema.Struct({
323+
name: Schema.optional(Schema.NonEmptyString),
320324
skills: Schema.optional(Schema.Union([Schema.String, Schema.Array(Schema.String)])),
321325
}),
322326
);
@@ -326,11 +330,15 @@ const discoverPluginSkillRoots = Effect.fn("discoverPluginSkillRoots")(function*
326330
configDirPath: string,
327331
cwd: string | undefined,
328332
enabledPlugins: ReadonlyMap<string, boolean>,
333+
environment: NodeJS.ProcessEnv,
329334
): Effect.fn.Return<ReadonlyArray<ClaudeSkillRoot>, never, FileSystem.FileSystem | Path.Path> {
330335
const fileSystem = yield* FileSystem.FileSystem;
331336
const path = yield* Path.Path;
337+
const pluginRoot = environment.CLAUDE_CODE_PLUGIN_CACHE_DIR
338+
? path.resolve(cwd ?? ".", environment.CLAUDE_CODE_PLUGIN_CACHE_DIR)
339+
: path.join(configDirPath, "plugins");
332340
const installed = yield* fileSystem
333-
.readFileString(path.join(configDirPath, "plugins", "installed_plugins.json"))
341+
.readFileString(path.join(pluginRoot, "installed_plugins.json"))
334342
.pipe(
335343
Effect.flatMap(Schema.decodeUnknownEffect(InstalledPlugins)),
336344
Effect.orElseSucceed(() => undefined),
@@ -341,8 +349,6 @@ const discoverPluginSkillRoots = Effect.fn("discoverPluginSkillRoots")(function*
341349

342350
for (const [key, entries] of Object.entries(installed?.plugins ?? {}).sort()) {
343351
if (enabledPlugins.get(key) !== true) continue;
344-
const prefix = key.split("@")[0];
345-
if (!prefix) continue;
346352
const install = entries
347353
.flatMap((entry) => {
348354
const record = Option.getOrUndefined(decodeInstalledPlugin(entry));
@@ -358,7 +364,6 @@ const discoverPluginSkillRoots = Effect.fn("discoverPluginSkillRoots")(function*
358364
.sort((left, right) => scopePriority[right.scope] - scopePriority[left.scope])[0];
359365
if (!install) continue;
360366
const scope = install.scope === "user" ? "user" : "project";
361-
roots.push({ directory: path.join(install.installPath, "skills"), scope, prefix });
362367

363368
// Plugins such as mattpocock-skills declare individual nested folders;
364369
// a declared path can also be a directory containing several skills.
@@ -368,9 +373,41 @@ const discoverPluginSkillRoots = Effect.fn("discoverPluginSkillRoots")(function*
368373
Effect.flatMap(Schema.decodeUnknownEffect(PluginManifest)),
369374
Effect.orElseSucceed(() => undefined),
370375
);
376+
const prefix = manifest?.name ?? key.split("@")[0];
377+
if (!prefix) continue;
378+
const skillsDirectory = path.join(install.installPath, "skills");
379+
roots.push({ directory: skillsDirectory, scope, prefix });
380+
if (
381+
!(yield* fileSystem.exists(skillsDirectory).pipe(Effect.orElseSucceed(() => false))) &&
382+
(yield* fileSystem
383+
.exists(path.join(install.installPath, "SKILL.md"))
384+
.pipe(Effect.orElseSucceed(() => false)))
385+
) {
386+
roots.push({ directory: install.installPath, scope, prefix, singleSkill: true });
387+
}
388+
const installRoot = path.resolve(install.installPath);
389+
const canonicalInstallRoot = yield* fileSystem
390+
.realPath(installRoot)
391+
.pipe(Effect.orElseSucceed(() => undefined));
371392
const declared = manifest?.skills;
372393
for (const relative of typeof declared === "string" ? [declared] : (declared ?? [])) {
373-
const directory = path.resolve(install.installPath, relative);
394+
const directory = path.resolve(installRoot, relative);
395+
const canonicalDirectory = yield* fileSystem
396+
.realPath(directory)
397+
.pipe(Effect.orElseSucceed(() => undefined));
398+
if (!canonicalInstallRoot || !canonicalDirectory) continue;
399+
if (
400+
[
401+
path.relative(installRoot, directory),
402+
path.relative(canonicalInstallRoot, canonicalDirectory),
403+
].some(
404+
(fromInstall) =>
405+
fromInstall === ".." ||
406+
fromInstall.startsWith(`..${path.sep}`) ||
407+
path.isAbsolute(fromInstall),
408+
)
409+
)
410+
continue;
374411
const singleSkill = yield* fileSystem
375412
.exists(path.join(directory, "SKILL.md"))
376413
.pipe(Effect.orElseSucceed(() => false));
@@ -435,7 +472,12 @@ export const discoverClaudeSkills = Effect.fn("discoverClaudeSkills")(function*
435472
const roots: ReadonlyArray<ClaudeSkillRoot> = [
436473
{ directory: path.join(configDirPath, "skills"), scope: "user" },
437474
...(cwd ? [{ directory: path.join(cwd, ".claude", "skills"), scope: "project" as const }] : []),
438-
...(yield* discoverPluginSkillRoots(configDirPath, cwd, enabledPlugins)),
475+
...(yield* discoverPluginSkillRoots(
476+
configDirPath,
477+
cwd,
478+
enabledPlugins,
479+
environment ?? process.env,
480+
)),
439481
];
440482

441483
const skillsByName = new Map<string, ServerProviderSkill>();
@@ -465,7 +507,7 @@ export const discoverClaudeSkills = Effect.fn("discoverClaudeSkills")(function*
465507
continue;
466508
}
467509

468-
// Claude Code identifies a skill by its directory, not by the
510+
// Claude Code identifies a user/project skill by its directory, not by the
469511
// frontmatter `name`: verified against the CLI, a skill in `probe-alias/`
470512
// declaring `name: probe-alias-frontmatter` is published as
471513
// `probe-alias`, and only `skillOverrides["probe-alias"]` switches it
@@ -475,7 +517,12 @@ export const discoverClaudeSkills = Effect.fn("discoverClaudeSkills")(function*
475517
if (!directoryName) {
476518
continue;
477519
}
478-
const name = root.prefix ? `${root.prefix}:${directoryName}` : directoryName;
520+
// Plugin skills use their frontmatter name, with a sanitized directory fallback.
521+
const pluginSkillName =
522+
root.prefix && frontmatter.kind === "parsed" ? frontmatter.name : undefined;
523+
const name = root.prefix
524+
? `${root.prefix}:${pluginSkillName ?? directoryName.replace(/[^a-zA-Z0-9_-]/g, "-")}`
525+
: directoryName;
479526

480527
// First root wins, so a later root never displaces a higher-precedence
481528
// skill of the same name.

0 commit comments

Comments
 (0)