Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/client/src/effect/api/api.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/client/src/promise/generated/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 32 additions & 4 deletions packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { LayerNode } from "@opencode/util/effect/layer-node"
import type { PersistentPty } from "./persistent-pty.js"
import { Cause, Context, Effect, Exit, Latch, Layer, Logger, Queue, References, Scope, Semaphore } from "effect"
import { Bus } from "./bus.js"
import { Location } from "./location.js"
import { PluginHost } from "./plugin/host.js"
import { type Failure, type Generation, Service } from "./plugin/service.js"
import { State } from "./state.js"
Expand All @@ -19,6 +20,7 @@ const layer = Layer.effect(
Service,
Effect.gen(function* () {
const bus = yield* Bus.Service
const location = yield* Location.Service
const scope = yield* Scope.make()
// One slot per requested definition in activation order, including ones whose setup failed, so
// the prefix diff below stays index-aligned and a failed revision is not retried until it changes.
Expand All @@ -27,16 +29,41 @@ const layer = Layer.effect(
const ready = yield* Latch.make(true)
const pending = new Set<object>()
let closed = false
// `plugin.list` reports readiness, so its flips are announced like inventory changes. A failing listener must not
// keep a hold pending or end the worker that releases one.
const announce = bus
.publish(
Plugin.Event.Updated,
{},
{ location: { directory: location.directory, workspaceID: location.workspaceID } },
)
.pipe(
Effect.asVoid,
Effect.catchCauseIf(
(cause) => !Cause.hasInterrupts(cause),
(cause) => Effect.logError("failed to announce plugin readiness", { cause }),
),
)
const holdUnsafe = () => {
if (closed) return Effect.void
const token = {}
pending.add(token)
ready.closeUnsafe()
return Effect.sync(() => {
if (pending.delete(token) && pending.size === 0) ready.openUnsafe()
return Effect.suspend(() => {
if (!pending.delete(token) || pending.size > 0) return Effect.void
ready.openUnsafe()
return announce
})
}
const hold = () => Effect.sync(holdUnsafe)
const isReady = Effect.sync(() => pending.size === 0)
const hold = () =>
Effect.uninterruptible(
Effect.suspend(() => {
const settled = pending.size === 0
const release = holdUnsafe()
return settled && !closed ? announce.pipe(Effect.as(release)) : Effect.succeed(release)
}),
)
const pendingFailures = yield* Queue.unbounded<PendingFailure>()
let discovered: readonly Failure[] = []
let inventory: Plugin.Info[] = []
Expand Down Expand Up @@ -76,7 +103,7 @@ const layer = Layer.effect(
release: holdUnsafe(),
})
})
const host = yield* PluginHost.make({ list }, plugin.id).pipe(Effect.provideContext(hostContext))
const host = yield* PluginHost.make({ list, ready: isReady }, plugin.id).pipe(Effect.provideContext(hostContext))
const exit = yield* Effect.suspend(() => plugin.effect(host)).pipe(
grouped,
inherit,
Expand Down Expand Up @@ -269,6 +296,7 @@ const layer = Layer.effect(
activate,
close,
awaitActivation: ready.await,
ready: isReady,
hold,
list,
gui,
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/plugin/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const isRpcType = (type: string): type is RpcEvent["type"] => type.startsWith("r
const isRpcEvent = (event: Event.Payload): event is RpcEvent => isRpcType(event.type)
const isPublicEvent = (event: Event.Payload): event is EventManifest.ServerEvent | RpcEvent =>
EventManifest.isServer(event) || isRpcEvent(event)
export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interface, "list">, pluginID: string) {
export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interface, "list" | "ready">, pluginID: string) {
const app = yield* App.Service
const agents = yield* Agent.Service
const artifacts = yield* Artifact.Service
Expand Down Expand Up @@ -466,7 +466,11 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
),
},
plugin: {
list: () => response(plugin.list()),
list: () =>
// Readiness first, so a list read after it is never older than a `true`.
Effect.all([plugin.ready, response(plugin.list())]).pipe(
Effect.map(([ready, listed]) => ({ ...listed, ready })),
),
},
reference: {
list: () => response(reference.list()),
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/plugin/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export interface Interface {
readonly close: (exit: Exit.Exit<unknown, unknown>) => Effect.Effect<void>
/** Wait for announced updates and activation to settle; failures remain in the inventory. */
readonly awaitActivation: Effect.Effect<void>
/**
* Whether activation has settled right now, without waiting. `plugin.updated` is published whenever it flips.
*/
readonly ready: Effect.Effect<boolean>
/** Keep readiness pending while preparing an update. Run the returned Effect to release it. */
readonly hold: () => Effect.Effect<Effect.Effect<void>>
}
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/plugin/supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ export const layer = Layer.effectDiscard(
// Make accepted work visible to awaitActivation before coalescing the burst.
const notify = Effect.gen(function* () {
observed++
if (!release) release = yield* registry.hold()
// Another source can take the hold while this one waits on it; keep one and give the extra back.
if (!release) {
const held = yield* registry.hold()
if (release) yield* held
else release = held
}
yield* Queue.offer(triggers, observed)
})
const watch = <A>(stream: Stream.Stream<A>) =>
Expand Down
30 changes: 29 additions & 1 deletion packages/core/test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,33 @@ it.effect("updates inventory metadata without restarting an unchanged generation
}),
)

it.effect("reports readiness, announcing each flip, and overlapping holds flip it once", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const bus = yield* Bus.Service
const seen: boolean[] = []
yield* Effect.acquireRelease(
bus.listen((event) =>
event.type === Plugin.Event.Updated.type
? plugins.ready.pipe(Effect.map((ready) => void seen.push(ready)))
: Effect.void,
),
(unsubscribe) => unsubscribe,
)
yield* plugins.awaitActivation
expect(yield* plugins.ready).toBe(true)

const first = yield* plugins.hold()
const second = yield* plugins.hold()
expect(yield* plugins.ready).toBe(false)
yield* first
expect(yield* plugins.ready).toBe(false)
yield* second
expect(yield* plugins.ready).toBe(true)
expect(seen).toEqual([false, true])
}),
)

it.live("loads a local plugin with its configured options", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
Expand Down Expand Up @@ -163,7 +190,8 @@ it.live("serves a local plugin's GUI bundle, and reloads it alone when only the

expect(edited.revision).toBe(first.revision)
expect((yield* plugins.gui(id))?.revision).not.toBe(served?.revision)
expect(updates.length).toBe(published + 1)
// The new bundle, plus readiness flipping to false and back to true around the activation.
expect(updates.length).toBe(published + 3)
expect(yield* Effect.promise(() => Bun.file(starts).text())).toBe("x")
}).pipe(Effect.scoped),
)
Expand Down
5 changes: 4 additions & 1 deletion packages/protocol/openapi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions packages/protocol/src/groups/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Location } from "@opencode/schema/location"
import { Plugin } from "@opencode/schema/plugin"
import { optional } from "@opencode/schema/schema"
import { Schema } from "effect"
import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema, OpenApi } from "effect/http-api"
import { InvalidRequestError, PluginNotFoundError, ServiceUnavailableError } from "../errors.js"
Expand All @@ -9,14 +10,23 @@ export const PluginGroup = HttpApiGroup.make("server.plugin")
.add(
HttpApiEndpoint.get("plugin.list", "/api/plugin", {
query: LocationQuery,
success: Location.response(Schema.Array(Plugin.Info)),
success: Schema.Struct({
location: Location.PublicRef,
/**
* False while the location's plugins are still loading; lists such as models and agents may be partial. Servers
* from before this field omit it.
*/
ready: optional(Schema.Boolean),
data: Schema.Array(Plugin.Info),
}),
})
.annotateMerge(locationQueryOpenApi)
.annotateMerge(
OpenApi.annotations({
identifier: "plugin.list",
summary: "List plugins",
description: "Retrieve enabled server plugins and their current status.",
description:
"Retrieve enabled server plugins and their current status. `ready` is false while the location's plugins are still loading, so lists such as models, agents and skills may be partial; `plugin.updated` is published whenever it flips.",
}),
),
)
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/handlers/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export const PluginHandler = HttpApiBuilder.group(Api, "server.plugin", (handler
handlers
.handle("plugin.list", () =>
Effect.gen(function* () {
return yield* response(Plugin.Service.use((plugin) => plugin.list()))
const plugins = yield* Plugin.Service
const ready = yield* plugins.ready
return { ...(yield* response(plugins.list())), ready }
}),
)
.handle("plugin.gui", (ctx) =>
Expand Down
68 changes: 68 additions & 0 deletions packages/server/test/plugin-ready.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expect } from "bun:test"
import { Bus } from "@opencode/core/bus"
import { SdkPlugins } from "@opencode/core/plugin/sdk"
import { Plugin as PluginSchema } from "@opencode/schema/plugin"
import { Plugin } from "@opencode/plugin/effect"
import { Context, Deferred, Effect, Fiber, Layer, Stream } from "effect"
import { HttpEffect, HttpRouter, HttpServer } from "effect/http"
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
import { it } from "../../core/test/lib/effect"
import { createRoutes } from "../src/routes"

it.live(
"plugin.list reports readiness without waiting and plugin.updated announces when it flips",
() =>
Effect.gen(function* () {
const tmp = yield* tmpdirScoped("opencode-plugin-ready-")
const started = yield* Deferred.make<void>()
const release = yield* Deferred.make<void>()
const context = yield* Layer.build(
createRoutes({
password: "secret",
database: { path: ":memory:" },
models: { fetch: false },
fs: { filewatcher: false },
config: { directory: tmp.path, project: false },
}).pipe(Layer.provide(HttpServer.layerServices)),
)
yield* Context.get(context, SdkPlugins.Service).register(
Plugin.define({
id: "slow-plugin",
effect: () =>
Effect.gen(function* () {
yield* Deferred.succeed(started, undefined)
yield* Deferred.await(release)
}),
}),
)
const handler = Context.get(context, HttpRouter.HttpRouter)
.asHttpEffect()
.pipe(HttpEffect.toWebHandlerWith(context))
const ready = Effect.promise(async () => {
const url = new URL("/api/plugin", "http://opencode.local")
url.searchParams.set("location[directory]", tmp.path)
const response = await handler(
new Request(url, { headers: { authorization: `Basic ${btoa("opencode:secret")}` } }),
)
return ((await response.json()) as { ready: boolean }).ready
})

expect(yield* ready.pipe(Effect.timeout("2 seconds"))).toBe(false)
yield* Deferred.await(started)
expect(yield* ready.pipe(Effect.timeout("2 seconds"))).toBe(false)

// A client re-reads plugin.list on each plugin.updated, so the last one must come after it turns true.
const settled = yield* Context.get(context, Bus.Service)
.subscribe([PluginSchema.Event.Updated])
.pipe(
Stream.mapEffect(() => ready),
Stream.takeUntil((value) => value),
Stream.runDrain,
Effect.forkScoped({ startImmediately: true }),
)
yield* Deferred.succeed(release, undefined)
yield* Fiber.join(settled).pipe(Effect.timeout("5 seconds"))
expect(yield* ready).toBe(true)
}),
15_000,
)
Loading