diff --git a/.changeset/trusted-plugin-skills.md b/.changeset/trusted-plugin-skills.md new file mode 100644 index 000000000000..cb82e1141684 --- /dev/null +++ b/.changeset/trusted-plugin-skills.md @@ -0,0 +1,5 @@ +--- +"@kilocode/cli": patch +--- + +Load skills registered by plugin config hooks reliably. diff --git a/packages/opencode/src/kilocode/config/plugin-skill-origins.ts b/packages/opencode/src/kilocode/config/plugin-skill-origins.ts new file mode 100644 index 000000000000..c3d7dcac6f22 --- /dev/null +++ b/packages/opencode/src/kilocode/config/plugin-skill-origins.ts @@ -0,0 +1,21 @@ +import type { Config } from "@/config/config" + +export function mark(cfg: Config.Info, before: ReadonlySet) { + const paths = cfg.skills?.paths + if (!paths?.length) return + + // Only paths added after plugin hooks began are trusted. Config-file paths + // already have an origin recorded by Config.load; limiting this to the + // observed delta keeps missing provenance fail-closed for future callers. + const origins = { ...cfg.skill_path_origins } + for (const path of paths) { + if (before.has(path) || origins[path]) continue + origins[path] = { + trusted: true, + source: "plugin config hook", + } + } + cfg.skill_path_origins = origins +} + +export * as PluginSkillOrigins from "./plugin-skill-origins" diff --git a/packages/opencode/src/plugin/index.ts b/packages/opencode/src/plugin/index.ts index 8e6ebb9f8374..9a260afb58be 100644 --- a/packages/opencode/src/plugin/index.ts +++ b/packages/opencode/src/plugin/index.ts @@ -34,6 +34,7 @@ import type { WorkspaceAdapter } from "@/control-plane/types" import { RuntimeFlags } from "@/effect/runtime-flags" import { EventV2Bridge } from "@/event-v2-bridge" import { InstallationChannel } from "@opencode-ai/core/installation/version" +import { PluginSkillOrigins } from "@/kilocode/config/plugin-skill-origins" // kilocode_change type State = { hooks: Hooks[] @@ -248,6 +249,7 @@ export const layer = Layer.effect( } // Notify plugins of current config + const before = new Set(cfg.skills?.paths ?? []) // kilocode_change for (const hook of hooks) { yield* Effect.tryPromise({ try: () => Promise.resolve((hook as any).config?.(cfg)), @@ -257,6 +259,7 @@ export const layer = Layer.effect( Effect.ignore, ) } + PluginSkillOrigins.mark(cfg, before) // kilocode_change - retain provenance for skill paths added by config hooks const unsubscribe = yield* events.listen((event) => { if (event.location?.directory !== ctx.directory) return Effect.void diff --git a/packages/opencode/test/kilocode/fixtures/plugin-skill.ts b/packages/opencode/test/kilocode/fixtures/plugin-skill.ts new file mode 100644 index 000000000000..51c54ec9c1d5 --- /dev/null +++ b/packages/opencode/test/kilocode/fixtures/plugin-skill.ts @@ -0,0 +1,9 @@ +export default async (input: { directory: string }) => ({ + config: async (cfg: { skills?: { paths?: string[] } }) => { + cfg.skills = cfg.skills ?? {} + // Keep the fixture outside the project root so the test exercises the trust + // boundary used by plugin-provided skill directories, while retaining a + // unique path for each test run. + cfg.skills.paths = [...(cfg.skills.paths ?? []), `${input.directory}-plugin-skills`] + }, +}) diff --git a/packages/opencode/test/kilocode/plugin-skill-path.test.ts b/packages/opencode/test/kilocode/plugin-skill-path.test.ts new file mode 100644 index 000000000000..7737668b2c63 --- /dev/null +++ b/packages/opencode/test/kilocode/plugin-skill-path.test.ts @@ -0,0 +1,83 @@ +import { expect } from "bun:test" +import { FSUtil } from "@opencode-ai/core/fs-util" +import { Global } from "@opencode-ai/core/global" +import { Effect, Layer } from "effect" +import { FetchHttpClient } from "effect/unstable/http" +import fs from "node:fs/promises" +import path from "node:path" +import { pathToFileURL } from "node:url" +import { AccountTest } from "../fake/account" +import { AuthTest } from "../fake/auth" +import { NpmTest } from "../fake/npm" +import { Config } from "../../src/config/config" +import { Discovery } from "../../src/skill/discovery" +import { Env } from "../../src/env" +import { EventV2Bridge } from "../../src/event-v2-bridge" +import { Git } from "../../src/git" +import { Plugin } from "../../src/plugin" +import { RuntimeFlags } from "../../src/effect/runtime-flags" +import { Skill } from "../../src/skill" +import { testEffect } from "../lib/effect" + +const plugin = pathToFileURL(path.join(import.meta.dir, "fixtures", "plugin-skill.ts")).href +const flags = RuntimeFlags.layer({ disableDefaultPlugins: true }) +const config = Config.layer.pipe( + Layer.provide(Git.defaultLayer), + Layer.provide(flags), + Layer.provide(FSUtil.defaultLayer), + Layer.provide(Env.defaultLayer), + Layer.provide(AuthTest.empty), + Layer.provide(AccountTest.empty), + Layer.provide(NpmTest.noop), + Layer.provide(FetchHttpClient.layer), +) +const events = EventV2Bridge.defaultLayer +const plugins = Plugin.layer.pipe(Layer.provide(events), Layer.provide(flags), Layer.provide(config)) +const skills = Skill.layer.pipe( + Layer.provide(Git.defaultLayer), + Layer.provide(Discovery.defaultLayer), + Layer.provide(config), + Layer.provide(events), + Layer.provide(FSUtil.defaultLayer), + Layer.provide(Global.layer), + Layer.provide(flags), +) +const layer = Layer.mergeAll(config, plugins, skills) +const it = testEffect(layer) + +it.instance( + "loads skills registered by a plugin config hook", + () => + Effect.gen(function* () { + yield* Plugin.Service.use((service) => service.init()) + const list = yield* Skill.Service.use((service) => service.all()) + + expect(list.find((item) => item.name === "plugin-skill")).toMatchObject({ + description: "Registered by a plugin config hook.", + content: expect.stringContaining("plugin payload"), + }) + }), + { + config: { plugin: [plugin] }, + init: (dir) => + Effect.gen(function* () { + const root = `${dir}-plugin-skills` + yield* Effect.addFinalizer(() => Effect.promise(() => fs.rm(root, { recursive: true, force: true }))) + yield* Effect.promise(() => + Bun.write( + path.join(root, "example", "SKILL.md"), + `--- +name: plugin-skill +description: Registered by a plugin config hook. +--- + +# Plugin skill + +{file:../payload.txt} +`, + ), + ) + yield* Effect.promise(() => Bun.write(path.join(root, "payload.txt"), "plugin payload")) + }), + }, +)