TypeScript
fix(server): OpenCode no longer drops a $skill read before its directory scan ends (#29535)
Julius Marminge78aee8a6706e
diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.tsindex 327841d1627..f39f2dcf98e 100644--- a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts@@ -2298,6 +2298,76 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {}).pipe(Effect.scoped),);+ it.effect("sends a $skill that appears once OpenCode finishes scanning the directory", () =>+ Effect.gen(function* () {+ const prompts: Array<unknown> = [];+ const greet = {+ id: "greet",+ name: "greet",+ path: `${WORK}/.opencode/skills/greet/SKILL.md`,+ content: "Say hello.",+ };+ const { runtime, thread } = yield* resumed(+ [+ out("skill.list", { "location[directory]": WORK }),+ reply("skill.list", { location: { directory: WORK }, data: [] }),+ // OpenCode answers the model list only once the directory scan has ended.+ ...directoryModels(WORK),+ out("skill.list", { "location[directory]": WORK }),+ reply("skill.list", { location: { directory: WORK }, data: [greet] }),+ out("session.prompt", { sessionID: SESSION, text: "<any>" }),+ promptAccepted,+ event("session.execution.succeeded", { sessionID: SESSION }),+ ],+ { onPrompt: (input) => prompts.push(input) },+ );+ const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);+ const started = yield* runtime+ .startTurn({+ ...turnInput(thread),+ message: { ...turnInput(thread).message, text: "$greet again please" },+ })+ .pipe(Effect.exit);+ assert.deepInclude(prompts[0], { skills: [{ id: "greet" }] });+ assert.isTrue(Exit.isSuccess(started));+ assert.equal((yield* Fiber.join(terminal))?.status, "completed");+ }).pipe(Effect.scoped),+ );++ it.effect(+ "keeps a $skill found on the first read when the reread after the scan comes back empty",+ () =>+ Effect.gen(function* () {+ const prompts: Array<unknown> = [];+ const greet = {+ id: "greet",+ name: "greet",+ path: `${WORK}/.opencode/skills/greet/SKILL.md`,+ content: "Say hello.",+ };+ const { runtime, thread } = yield* resumed(+ [+ out("skill.list", { "location[directory]": WORK }),+ reply("skill.list", { location: { directory: WORK }, data: [greet] }),+ ...directoryModels(WORK),+ out("skill.list", { "location[directory]": WORK }),+ reply("skill.list", { location: { directory: WORK }, data: [] }),+ out("session.prompt", { sessionID: SESSION, text: "<any>" }),+ promptAccepted,+ event("session.execution.succeeded", { sessionID: SESSION }),+ ],+ { onPrompt: (input) => prompts.push(input) },+ );+ const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);+ yield* runtime.startTurn({+ ...turnInput(thread),+ message: { ...turnInput(thread).message, text: "$greet then echo $HOME" },+ });+ assert.equal((yield* Fiber.join(terminal))?.status, "completed");+ assert.deepInclude(prompts[0], { skills: [{ id: "greet" }] });+ }).pipe(Effect.scoped),+ );+it.effect("keeps a turn still running after a reconnect open for its next events", () =>Effect.gen(function* () {const { runtime, thread } = yield* resumed([diff --git a/packages/provider-opencode/src/server/v2/adapter.ts b/packages/provider-opencode/src/server/v2/adapter.tsindex 95700d31c83..71bceb59475 100644--- a/packages/provider-opencode/src/server/v2/adapter.ts+++ b/packages/provider-opencode/src/server/v2/adapter.ts@@ -630,14 +630,8 @@ const commandOf = (text: string) => {return match === null ? undefined : { name: match[1]!, text: match[2] ?? "" };};-const SKILL_MENTION = new RegExp(SKILL_MENTION_PATTERN.source, "u");--const skillsNamed = (text: string, known: ReadonlySet<string>) => [- ...new Set(- [...text.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) =>- known.has(match[2] ?? "") ? [match[2]!] : [],- ),- ),+const skillMentions = (text: string) => [+ ...new Set([...text.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => match[2] ?? [])),];const turnTokenUsage = (turn: ActiveTurn, status: OrchestrationV2ProviderTurn["status"]) => {@@ -3322,6 +3316,27 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pryield* stopStrays(state);});+ const listSkills = (location: { readonly directory: string }) =>+ client.skill.list({ location }).pipe(+ Effect.timeout(INVENTORY_TIMEOUT),+ Effect.map((list) => new Set<string>(list.data.map((skill) => skill.id))),+ Effect.orElseSucceed(() => new Set<string>()),+ );++ // A directory OpenCode is still scanning lists none or some of its skills.+ // OpenCode 2.2.5 answers its model list only once the scan has ended (observed, not documented).+ const skillsMentioned = Effect.fnUntraced(function* (+ location: { readonly directory: string },+ mentions: ReadonlyArray<string>,+ ) {+ const known = yield* listSkills(location);+ if (mentions.some((name) => !known.has(name))) {+ yield* readModels(location.directory);+ for (const name of yield* listSkills(location)) known.add(name);+ }+ return mentions.filter((name) => known.has(name));+ });+const submit = Effect.fnUntraced(function* (sessionId: string,turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,@@ -3357,16 +3372,8 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Prreturn yield* client.session.command({ sessionID, ...command });}}- const skills = SKILL_MENTION.test(text)- ? skillsNamed(- text,- yield* client.skill.list({ location }).pipe(- Effect.timeout(INVENTORY_TIMEOUT),- Effect.map((list) => new Set(list.data.map((skill) => skill.id))),- Effect.orElseSucceed(() => new Set<string>()),- ),- )- : [];+ const mentions = skillMentions(text);+ const skills = mentions.length === 0 ? [] : yield* skillsMentioned(location, mentions);const files = yield* promptFiles(turnInput.message.attachments);const messageText = yield* promptText(turnInput);if (!sending()) return;