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: 1 addition & 5 deletions 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: 4 additions & 32 deletions packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 @@ -20,7 +19,6 @@ 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 @@ -29,41 +27,16 @@ 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.suspend(() => {
if (!pending.delete(token) || pending.size > 0) return Effect.void
ready.openUnsafe()
return announce
return Effect.sync(() => {
if (pending.delete(token) && pending.size === 0) ready.openUnsafe()
})
}
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 hold = () => Effect.sync(holdUnsafe)
const pendingFailures = yield* Queue.unbounded<PendingFailure>()
let discovered: readonly Failure[] = []
let inventory: Plugin.Info[] = []
Expand Down Expand Up @@ -103,7 +76,7 @@ const layer = Layer.effect(
release: holdUnsafe(),
})
})
const host = yield* PluginHost.make({ list, ready: isReady }, plugin.id).pipe(Effect.provideContext(hostContext))
const host = yield* PluginHost.make({ list }, plugin.id).pipe(Effect.provideContext(hostContext))
const exit = yield* Effect.suspend(() => plugin.effect(host)).pipe(
grouped,
inherit,
Expand Down Expand Up @@ -296,7 +269,6 @@ const layer = Layer.effect(
activate,
close,
awaitActivation: ready.await,
ready: isReady,
hold,
list,
gui,
Expand Down
8 changes: 2 additions & 6 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" | "ready">, pluginID: string) {
export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interface, "list">, pluginID: string) {
const app = yield* App.Service
const agents = yield* Agent.Service
const artifacts = yield* Artifact.Service
Expand Down Expand Up @@ -466,11 +466,7 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
),
},
plugin: {
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 })),
),
list: () => response(plugin.list()),
},
reference: {
list: () => response(reference.list()),
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/plugin/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ 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: 1 addition & 6 deletions packages/core/src/plugin/supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,7 @@ export const layer = Layer.effectDiscard(
// Make accepted work visible to awaitActivation before coalescing the burst.
const notify = Effect.gen(function* () {
observed++
// 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
}
if (!release) release = yield* registry.hold()
yield* Queue.offer(triggers, observed)
})
const watch = <A>(stream: Stream.Stream<A>) =>
Expand Down
30 changes: 1 addition & 29 deletions packages/core/test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,6 @@ 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 @@ -190,8 +163,7 @@ 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)
// The new bundle, plus readiness flipping to false and back to true around the activation.
expect(updates.length).toBe(published + 3)
expect(updates.length).toBe(published + 1)
expect(yield* Effect.promise(() => Bun.file(starts).text())).toBe("x")
}).pipe(Effect.scoped),
)
Expand Down
5 changes: 1 addition & 4 deletions packages/protocol/openapi.json

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

14 changes: 2 additions & 12 deletions packages/protocol/src/groups/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -10,23 +9,14 @@ export const PluginGroup = HttpApiGroup.make("server.plugin")
.add(
HttpApiEndpoint.get("plugin.list", "/api/plugin", {
query: LocationQuery,
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),
}),
success: Location.response(Schema.Array(Plugin.Info)),
})
.annotateMerge(locationQueryOpenApi)
.annotateMerge(
OpenApi.annotations({
identifier: "plugin.list",
summary: "List plugins",
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.",
description: "Retrieve enabled server plugins and their current status.",
}),
),
)
Expand Down
4 changes: 1 addition & 3 deletions packages/server/src/handlers/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export const PluginHandler = HttpApiBuilder.group(Api, "server.plugin", (handler
handlers
.handle("plugin.list", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const ready = yield* plugins.ready
return { ...(yield* response(plugins.list())), ready }
return yield* response(Plugin.Service.use((plugin) => plugin.list()))
}),
)
.handle("plugin.gui", (ctx) =>
Expand Down
68 changes: 0 additions & 68 deletions packages/server/test/plugin-ready.test.ts

This file was deleted.

Loading