mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
fix(core): report duplicate plugin IDs as inventory failures (#46718)
This commit is contained in:
committed by
GitHub
Unverified
parent
519cd8c771
commit
e561431f7c
@@ -80,13 +80,26 @@ const resolve = Effect.fn("PluginSupervisor.resolve")(function* (
|
||||
enabled.add(plugin.id)
|
||||
}
|
||||
|
||||
const ordered = [
|
||||
...pre.filter((plugin) => enabled.has(plugin.id)),
|
||||
...[...packages.values()].filter((plugin) => enabled.has(plugin.id)),
|
||||
...post.filter((plugin) => enabled.has(plugin.id)),
|
||||
]
|
||||
// Registry activation dies on a duplicate ID, which would drop the whole generation including builtins.
|
||||
// Keep the first occurrence in boot order and report later ones like any other plugin setup failure.
|
||||
const duplicate = (plugin: Plugin.Generation, index: number) =>
|
||||
ordered.findIndex((other) => other.id === plugin.id) !== index
|
||||
return {
|
||||
plugins: [
|
||||
...pre.filter((plugin) => enabled.has(plugin.id)),
|
||||
...[...packages.values()].filter((plugin) => enabled.has(plugin.id)),
|
||||
...post.filter((plugin) => enabled.has(plugin.id)),
|
||||
plugins: ordered.filter((plugin, index) => !duplicate(plugin, index)),
|
||||
failures: [
|
||||
...failures.values(),
|
||||
...ordered.filter(duplicate).map((plugin) => ({
|
||||
id: Plugin.ID.make(plugin.id),
|
||||
source: plugin.source ?? { type: "builtin" as const },
|
||||
state: { status: "failed" as const, error: `Duplicate plugin ID: ${plugin.id}` },
|
||||
features: { server: true as const, ...plugin.features },
|
||||
})),
|
||||
],
|
||||
failures: [...failures.values()],
|
||||
pending: [...pending],
|
||||
}
|
||||
})
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Duration, Effect, Layer, LayerMap } from "effect"
|
||||
import { define } from "@opencode-ai/plugin/effect/plugin"
|
||||
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
|
||||
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
|
||||
import { Global } from "@opencode-ai/util/global"
|
||||
import { Command } from "@opencode-ai/core/command"
|
||||
import { Instance } from "@opencode-ai/core/instance"
|
||||
import { LocationServiceMap } from "@opencode-ai/core/location-services"
|
||||
import { Location } from "@opencode-ai/core/location"
|
||||
import { Plugin } from "@opencode-ai/core/plugin"
|
||||
import { SdkPlugins } from "@opencode-ai/core/plugin/sdk"
|
||||
import { AbsolutePath } from "@opencode-ai/core/schema"
|
||||
import { Database } from "../../src/database/database"
|
||||
import { Bus } from "../../src/bus"
|
||||
import { tempGlobalLayer } from "../fixture/global"
|
||||
import { tmpdirScoped } from "../fixture/tmpdir"
|
||||
import { testEffect } from "../lib/effect"
|
||||
|
||||
const id = Plugin.ID.make("account-prompts")
|
||||
|
||||
// Host and instance plugins share one ID; each registers a distinct command so the winner is observable.
|
||||
const greeter = (command: string) =>
|
||||
define({
|
||||
id,
|
||||
effect: (ctx) =>
|
||||
ctx.command.transform((draft) => draft.add({ name: command, execute: () => Effect.void })).pipe(Effect.asVoid),
|
||||
})
|
||||
|
||||
const instances = Layer.effect(
|
||||
LocationServiceMap.Service,
|
||||
Effect.gen(function* () {
|
||||
const map = yield* LayerMap.make(
|
||||
(ref: Location.Ref) =>
|
||||
Instance.layer(ref, { discovery: false, plugins: [greeter("instance-greet")], replacements: bindings }),
|
||||
{ idleTimeToLive: Duration.infinity },
|
||||
)
|
||||
const bindings: LayerNode.Replacements = [
|
||||
Global.node.replace(tempGlobalLayer),
|
||||
LocationServiceMap.node.replace(Layer.succeed(LocationServiceMap.Service, map)),
|
||||
Instance.node.replace(
|
||||
Layer.succeed(Instance.Service, {
|
||||
provide: (session) => Effect.provide(map.get(session.location)),
|
||||
}),
|
||||
),
|
||||
]
|
||||
return map
|
||||
}),
|
||||
)
|
||||
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node, SdkPlugins.node, LocationServiceMap.node]), [
|
||||
Global.node.replace(tempGlobalLayer),
|
||||
LocationServiceMap.node.replace(instances),
|
||||
]),
|
||||
)
|
||||
|
||||
describe("PluginSupervisor", () => {
|
||||
it.live("reports a duplicate plugin ID as a failure without dropping the generation", () =>
|
||||
Effect.gen(function* () {
|
||||
const sdk = yield* SdkPlugins.Service
|
||||
yield* sdk.register(greeter("host-greet"))
|
||||
const directory = yield* tmpdirScoped()
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
const state = yield* Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
yield* plugins.awaitActivation
|
||||
const commands = yield* Command.Service
|
||||
return {
|
||||
inventory: yield* plugins.list(),
|
||||
host: yield* commands.get("host-greet"),
|
||||
instance: yield* commands.get("instance-greet"),
|
||||
}
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory.path) }))),
|
||||
)
|
||||
|
||||
// Earlier boot order wins: the host SDK plugin activates and the instance copy is reported.
|
||||
expect(state.inventory.filter((plugin) => plugin.id === id)).toEqual([
|
||||
{ id, source: { type: "sdk" }, state: { status: "active" }, features: { server: true } },
|
||||
{
|
||||
id,
|
||||
source: { type: "sdk" },
|
||||
state: { status: "failed", error: `Duplicate plugin ID: ${id}` },
|
||||
features: { server: true },
|
||||
},
|
||||
])
|
||||
expect(state.host).toBeDefined()
|
||||
expect(state.instance).toBeUndefined()
|
||||
// Builtins stay active: the duplicate is a plugin failure, not a generation defect.
|
||||
expect(
|
||||
state.inventory.some((plugin) => plugin.id?.startsWith("opencode.") && plugin.state.status === "active"),
|
||||
).toBe(true)
|
||||
}),
|
||||
)
|
||||
})
|
||||
@@ -62,7 +62,7 @@ await opencode.sessions.prompt({ sessionID: session.id, text: "Review the change
|
||||
|
||||
- The same key and Location share one live instance. Different directories or workspace IDs always select separate instances, even with the same application key.
|
||||
- `configure` runs on a cache miss, not on each prompt. Loaded instances live until the host closes; change the application key or restart the host to reconstruct their birth configuration. Plugin transforms and reloads remain available within that lifetime.
|
||||
- Session metadata, message, inbox, and context reads do not initialize plugins; permission and form lists read instance services and therefore acquire the Session's instance. Configuration failure, an instance plugin ID that collides with a host `plugins` entry, or initial setup failure of a supplied plugin prevents capability acquisition, without falling back to another instance. A subsequent request can retry a failed construction.
|
||||
- Session metadata, message, inbox, and context reads do not initialize plugins; permission and form lists read instance services and therefore acquire the Session's instance. Configuration failure or a supplied plugin reported as failed after activation, including initial setup failure or an ID that collides with a host `plugins` entry (the host plugin keeps the ID), prevents capability acquisition, without falling back to another instance. A subsequent request can retry a failed construction.
|
||||
- Existing HTTP prompt middleware also acquires capabilities for an idempotent retry. After restart, that retry can reconstruct plugins before returning the original admission; prompt preparation and hooks do not rerun. Configuration failure can therefore block the retry even when its input was already saved.
|
||||
- Instance selection is not an authorization or storage-isolation boundary. Plugin Session APIs and the existing plugin-ID-based durable storage retain their normal scope.
|
||||
- Omitting `instances` preserves default Location sharing. Host-wide plugins remain separate from Session-selected configuration; retain host-wide catalog policy when locationless generation needs it.
|
||||
|
||||
@@ -4,7 +4,6 @@ import { Instance } from "@opencode-ai/core/instance"
|
||||
import { LocationServiceMap } from "@opencode-ai/core/location-service-map"
|
||||
import { Plugin } from "@opencode-ai/core/plugin"
|
||||
import type { InstancePlugins } from "@opencode-ai/core/plugin/instance"
|
||||
import { SdkPlugins } from "@opencode-ai/core/plugin/sdk"
|
||||
import { Location } from "@opencode-ai/schema/location"
|
||||
import type { Session } from "@opencode-ai/schema/session"
|
||||
import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
|
||||
@@ -27,7 +26,7 @@ export function node(options: Options, replacements: () => LayerNode.Replacement
|
||||
return makeGlobalNode({
|
||||
service: Instance.Service,
|
||||
layer: layer(options, replacements),
|
||||
deps: [LocationServiceMap.node, SdkPlugins.node],
|
||||
deps: [LocationServiceMap.node],
|
||||
})
|
||||
}
|
||||
|
||||
@@ -37,7 +36,6 @@ export function layer(options: Options, replacements: () => LayerNode.Replacemen
|
||||
Effect.gen(function* () {
|
||||
const scope = yield* Effect.scope
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
const sdk = yield* SdkPlugins.Service
|
||||
const key = (session: Session.Info) => ({
|
||||
key: options.key(session),
|
||||
...LocationServiceMap.canonical(session.location),
|
||||
@@ -48,17 +46,6 @@ export function layer(options: Options, replacements: () => LayerNode.Replacemen
|
||||
Layer.unwrap(
|
||||
Effect.gen(function* () {
|
||||
const configuration = yield* options.configure(input.key).pipe(Effect.orDie)
|
||||
// A host/instance ID collision fails the whole plugin generation, which leaves no inventory
|
||||
// trace to check after activation. Reject it before constructing anything.
|
||||
const collisions = configuration.plugins.filter((plugin) =>
|
||||
sdk.all().some((host) => host.id === plugin.id),
|
||||
)
|
||||
if (collisions.length > 0)
|
||||
yield* Effect.die(
|
||||
new Error(
|
||||
`Instance plugin IDs collide with host plugins: ${collisions.map((plugin) => plugin.id).join(", ")}`,
|
||||
),
|
||||
)
|
||||
return Instance.layer(Location.Ref.make({ directory: input.directory, workspaceID: input.workspaceID }), {
|
||||
plugins: configuration.plugins,
|
||||
replacements: [
|
||||
@@ -73,6 +60,7 @@ export function layer(options: Options, replacements: () => LayerNode.Replacemen
|
||||
Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
yield* plugins.awaitActivation
|
||||
// Covers setup failures and IDs colliding with host plugins; Core reports both in the inventory.
|
||||
const failed = (yield* plugins.list()).filter(
|
||||
(plugin) =>
|
||||
plugin.state.status === "failed" &&
|
||||
|
||||
@@ -298,6 +298,7 @@ test("Promise instance plugin ID collisions reject admission and reconstruct on
|
||||
await using directory = await tmpdir("opencode-promise-instance-collision-")
|
||||
const configured: string[] = []
|
||||
const setups: string[] = []
|
||||
const cleanups: string[] = []
|
||||
await using opencode = await OpenCode.create({
|
||||
...hostOptions(directory.path),
|
||||
plugins: [
|
||||
@@ -305,6 +306,9 @@ test("Promise instance plugin ID collisions reject admission and reconstruct on
|
||||
id: "account-prompts",
|
||||
setup() {
|
||||
setups.push("host")
|
||||
return () => {
|
||||
cleanups.push("host")
|
||||
}
|
||||
},
|
||||
},
|
||||
],
|
||||
@@ -342,12 +346,17 @@ test("Promise instance plugin ID collisions reject admission and reconstruct on
|
||||
expect(await opencode.sessions.inbox.list({ sessionID: session.id })).toEqual([])
|
||||
expect((await opencode.message.list({ sessionID: session.id })).data).toEqual([])
|
||||
expect(configured).toEqual(["alpha"])
|
||||
expect(setups).toEqual([])
|
||||
// The host plugin wins the ID and activates inside the instance before the colliding instance plugin
|
||||
// is reported failed; rejecting the instance tears that generation down again.
|
||||
expect(setups).toEqual(["host"])
|
||||
expect(cleanups).toEqual(["host"])
|
||||
|
||||
const admitted = await opencode.sessions.prompt(input)
|
||||
expect(admitted.id).toBe(input.id)
|
||||
expect(admitted.payload.text).toBe("alpha: Retry this input")
|
||||
expect(await opencode.sessions.inbox.list({ sessionID: session.id })).toEqual([admitted])
|
||||
expect(configured).toEqual(["alpha", "alpha"])
|
||||
expect(setups).toEqual(["host", "alpha"])
|
||||
// The reconstructed instance runs a fresh generation: host plugin again, then the renamed instance plugin.
|
||||
expect(setups).toEqual(["host", "host", "alpha"])
|
||||
expect(cleanups).toEqual(["host"])
|
||||
}, 20_000)
|
||||
|
||||
Reference in New Issue
Block a user