Skip to content

Commit 9e2b63d

Browse files
fix(server): ACP OpenCode skill scan follows OpenCode's roots and precedence
Mirror OpenCode 2.1.4's discovery: OPENCODE_CONFIG_DIR and the system config directory, OPENCODE_DISABLE_PROJECT_CONFIG, its ignored directories, later-source-wins precedence, and its frontmatter validity rules. Cap frontmatter before YAML parsing and bound total bytes so pathological SKILL.md files cannot stall the server. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a77c8f1 commit 9e2b63d

3 files changed

Lines changed: 354 additions & 76 deletions

File tree

‎packages/provider-acp-registry/src/server/driver.test.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,12 @@ it.layer(layerDriver)("AcpRegistryDriver workspace discovery", (it) => {
442442
Effect.gen(function* () {
443443
const fileSystem = yield* FileSystem.FileSystem;
444444
const path = yield* Path.Path;
445-
const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-acp-home-" });
446-
const project = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-acp-project-" });
445+
const home = yield* fileSystem.realPath(
446+
yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-acp-home-" }),
447+
);
448+
const project = yield* fileSystem.realPath(
449+
yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-acp-project-" }),
450+
);
447451
const writeSkill = (root: string, name: string) =>
448452
fileSystem
449453
.makeDirectory(path.join(root, ".agents", "skills", name), { recursive: true })
@@ -462,7 +466,11 @@ it.layer(layerDriver)("AcpRegistryDriver workspace discovery", (it) => {
462466
instanceId: identity.instanceId,
463467
displayName: identity.displayName,
464468
accentColor: undefined,
465-
environment: [{ name: "HOME", value: home, sensitive: false }],
469+
environment: [
470+
{ name: "HOME", value: home, sensitive: false },
471+
{ name: "XDG_CONFIG_HOME", value: path.join(home, ".config"), sensitive: false },
472+
{ name: "OPENCODE_CONFIG_DIR", value: path.join(home, "opencode"), sensitive: false },
473+
],
466474
enabled: true,
467475
config: decodeSettings({ agentId: "opencode-v2" }),
468476
}).pipe(
@@ -479,7 +487,11 @@ it.layer(layerDriver)("AcpRegistryDriver workspace discovery", (it) => {
479487

480488
expect(instance.snapshotForCwd).toBeDefined();
481489
const workspace = yield* instance.snapshotForCwd!(project);
482-
expect(workspace.skills.map((skill) => skill.name).toSorted()).toEqual([
490+
// Ancestors of the temp directories are real OpenCode roots too.
491+
const fixtureSkills = workspace.skills.filter(
492+
(skill) => skill.path.startsWith(home) || skill.path.startsWith(project),
493+
);
494+
expect(fixtureSkills.map((skill) => skill.name).toSorted()).toEqual([
483495
"global-skill",
484496
"project-skill",
485497
]);
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { expect, it } from "@effect/vitest";
3+
import * as Effect from "effect/Effect";
4+
import * as FileSystem from "effect/FileSystem";
5+
import * as Path from "effect/Path";
6+
7+
import { discoverOpenCodeAcpSkills } from "./opencodeAcp.ts";
8+
9+
const makeFixture = Effect.gen(function* () {
10+
const fileSystem = yield* FileSystem.FileSystem;
11+
const path = yield* Path.Path;
12+
const root = yield* fileSystem.realPath(
13+
yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-opencode-acp-" }),
14+
);
15+
const write = (relative: string, contents: string) =>
16+
fileSystem
17+
.makeDirectory(path.dirname(path.join(root, relative)), { recursive: true })
18+
.pipe(Effect.andThen(fileSystem.writeFileString(path.join(root, relative), contents)));
19+
const skill = (description: string) => `---\ndescription: ${description}\n---\n\nBody.\n`;
20+
const environment = {
21+
HOME: path.join(root, "home"),
22+
USERPROFILE: path.join(root, "home"),
23+
XDG_CONFIG_HOME: path.join(root, "xdg"),
24+
OPENCODE_CONFIG_DIR: path.join(root, "custom-config"),
25+
};
26+
// Ancestors of the temp directory and the host's system config directory
27+
// are real OpenCode roots too, so only this fixture's skills are compared.
28+
const discover = (cwd: string, overrides: NodeJS.ProcessEnv = {}) =>
29+
discoverOpenCodeAcpSkills(path.join(root, cwd), { ...environment, ...overrides }).pipe(
30+
Effect.map((skills) => skills.filter((skill) => skill.path.startsWith(root))),
31+
);
32+
const descriptions = (cwd: string, overrides: NodeJS.ProcessEnv = {}) =>
33+
discover(cwd, overrides).pipe(
34+
Effect.map((skills) =>
35+
Object.fromEntries(skills.map((skill) => [skill.name, skill.description ?? null])),
36+
),
37+
);
38+
return { root, write, skill, discover, descriptions };
39+
});
40+
41+
it.layer(NodeServices.layer)("discoverOpenCodeAcpSkills", (it) => {
42+
it.effect("resolves skill roots and duplicates the way OpenCode 2.1 does", () =>
43+
Effect.gen(function* () {
44+
const { write, skill, descriptions } = yield* makeFixture;
45+
// Every .claude root loads before any .agents root, config directories
46+
// override both, and nearer project directories override ancestors.
47+
yield* write("home/.claude/skills/shared/SKILL.md", skill("global claude"));
48+
yield* write("home/.agents/skills/shared/SKILL.md", skill("global agents"));
49+
yield* write("project/.claude/skills/compat/SKILL.md", skill("project claude"));
50+
yield* write("home/.agents/skills/compat/SKILL.md", skill("global agents"));
51+
yield* write("project/.claude/skills/claude-only/SKILL.md", skill("project claude"));
52+
yield* write("custom-config/skills/shared/SKILL.md", skill("custom config"));
53+
yield* write(
54+
"xdg/opencode/skills/xdg-only/SKILL.md",
55+
skill("replaced by OPENCODE_CONFIG_DIR"),
56+
);
57+
yield* write("project/.opencode/skill/nested/SKILL.md", skill("singular"));
58+
yield* write("project/.opencode/skills/nested/SKILL.md", skill("plural"));
59+
yield* write("project/app/.opencode/skills/nested/SKILL.md", skill("nearest"));
60+
yield* write("project/.opencode/skills/flat.md", skill("flat file"));
61+
yield* write("project/.opencode/skills/flat/SKILL.md", skill("skill directory"));
62+
yield* write("project/.agents/skills/venv/hidden/SKILL.md", skill("ignored"));
63+
yield* write("project/.agents/skills/__pycache__/hidden/SKILL.md", skill("ignored"));
64+
yield* write("project/.agents/skills/plain/SKILL.md", skill("plain"));
65+
66+
expect(yield* descriptions("project/app")).toEqual({
67+
"claude-only": "project claude",
68+
compat: "global agents",
69+
flat: "skill directory",
70+
nested: "nearest",
71+
plain: "plain",
72+
shared: "custom config",
73+
});
74+
expect(
75+
yield* descriptions("project/app", {
76+
OPENCODE_DISABLE_PROJECT_CONFIG: "1",
77+
OPENCODE_DISABLE_CLAUDE_CODE_SKILLS: "true",
78+
}),
79+
).toEqual({ compat: "global agents", shared: "custom config" });
80+
}).pipe(Effect.scoped),
81+
);
82+
83+
it.effect("skips skills OpenCode rejects and bounds frontmatter parsing", () =>
84+
Effect.gen(function* () {
85+
const { write, discover } = yield* makeFixture;
86+
const skills = "project/.agents/skills";
87+
yield* write(`${skills}/no-frontmatter/SKILL.md`, "Just instructions.\n");
88+
yield* write(
89+
`${skills}/colon/SKILL.md`,
90+
"---\nname: Colon Helper\ndescription: Use when: you need colons\n---\n",
91+
);
92+
yield* write(`${skills}/malformed/SKILL.md`, "---\ndescription: [unclosed\n---\n");
93+
yield* write(`${skills}/numeric-name/SKILL.md`, "---\nname: 3\n---\n");
94+
yield* write(`${skills}/numeric-description/SKILL.md`, "---\ndescription: 3\n---\n");
95+
yield* write(`${skills}/unterminated/SKILL.md`, "---\ndescription: never closed\n");
96+
// `yaml` parses a 200KB mapping in seconds, so it is listed unparsed.
97+
const keys = Array.from({ length: 20_000 }, (_, index) => `key${index}: value`).join("\n");
98+
yield* write(`${skills}/huge/SKILL.md`, `---\ndescription: huge\n${keys}\n---\n`);
99+
100+
const discovered = yield* discover("project");
101+
expect(
102+
discovered.map(({ name, displayName, description }) => ({
103+
name,
104+
displayName,
105+
description,
106+
})),
107+
).toEqual([
108+
{ name: "colon", displayName: "Colon Helper", description: "Use when: you need colons" },
109+
{ name: "huge", displayName: undefined, description: undefined },
110+
{ name: "no-frontmatter", displayName: undefined, description: undefined },
111+
]);
112+
}).pipe(Effect.scoped),
113+
);
114+
});

0 commit comments

Comments
 (0)