mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(core): activate the initial plugin generation without the reload debounce (#46922)
This commit is contained in:
committed by
GitHub
Unverified
parent
e402600d92
commit
c992716523
@@ -1,7 +1,7 @@
|
||||
export * as PluginSupervisor from "./supervisor.js"
|
||||
|
||||
import { Event } from "@opencode-ai/schema/config"
|
||||
import { Cause, Effect, Layer, PubSub, Stream } from "effect"
|
||||
import { Cause, Effect, Layer, Queue, Stream } from "effect"
|
||||
import path from "path"
|
||||
import { ConfigPluginSource } from "../config/plugin/source.js"
|
||||
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
|
||||
@@ -131,7 +131,6 @@ export const layer = Layer.effectDiscard(
|
||||
const updating = new Set<string>()
|
||||
let generation = 0
|
||||
let observed = 0
|
||||
const refresh = yield* PubSub.unbounded<void>()
|
||||
|
||||
const activate = Effect.fn("PluginSupervisor.activate")(function* () {
|
||||
const current = ++generation
|
||||
@@ -189,11 +188,24 @@ export const layer = Layer.effectDiscard(
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
})
|
||||
const reloads = Stream.merge(
|
||||
Stream.merge(
|
||||
Stream.merge(sources.changes(), Stream.fromPubSub(refresh)),
|
||||
bus.subscribe([Event.Updated, SdkPlugins.Updated]),
|
||||
),
|
||||
// Start source consumers before activation, without an extra merge/debounce boundary delaying them.
|
||||
// Each source owns its upstream subscriptions; the queue retains the latest observed request.
|
||||
const triggers = yield* Queue.sliding<number>(1)
|
||||
// Make accepted work visible to awaitActivation before coalescing the burst.
|
||||
const notify = Effect.gen(function* () {
|
||||
observed++
|
||||
if (!release) release = yield* registry.hold()
|
||||
yield* Queue.offer(triggers, observed)
|
||||
})
|
||||
const watch = <A>(stream: Stream.Stream<A>) =>
|
||||
stream.pipe(
|
||||
Stream.runForEach(() => notify),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
yield* watch(sources.changes())
|
||||
yield* watch(Stream.fromEffectRepeat(Effect.sleep("24 hours")))
|
||||
yield* watch(bus.subscribe([Event.Updated, SdkPlugins.Updated]))
|
||||
yield* watch(
|
||||
updates.changes().pipe(
|
||||
Stream.filter((update) => packages.has(update.target)),
|
||||
Stream.tap((update) =>
|
||||
@@ -202,22 +214,10 @@ export const layer = Layer.effectDiscard(
|
||||
update.updating ? updating.add(update.target) : updating.delete(update.target)
|
||||
}),
|
||||
),
|
||||
Stream.map(() => undefined),
|
||||
),
|
||||
).pipe(
|
||||
// Make accepted work visible to awaitActivation before coalescing the burst.
|
||||
Stream.mapEffect(() =>
|
||||
Effect.gen(function* () {
|
||||
observed++
|
||||
if (!release) release = yield* registry.hold()
|
||||
return observed
|
||||
}),
|
||||
),
|
||||
)
|
||||
yield* Stream.concat(Stream.succeed(0), reloads).pipe(
|
||||
// Keep observing updates while activation runs, retaining only the latest generation request.
|
||||
Stream.buffer({ capacity: 1, strategy: "sliding" }),
|
||||
Stream.debounce("100 millis"),
|
||||
// Run initial activation immediately; debounce only later requests. One consumer serializes both.
|
||||
yield* Stream.concat(Stream.succeed(0), Stream.fromQueue(triggers).pipe(Stream.debounce("100 millis"))).pipe(
|
||||
Stream.runForEach((target) =>
|
||||
Effect.gen(function* () {
|
||||
yield* activate().pipe(Effect.catchCause((cause) => Effect.logError("failed to reload plugins", { cause })))
|
||||
@@ -229,12 +229,6 @@ export const layer = Layer.effectDiscard(
|
||||
),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
// The periodic refresh joins the reload feed above so it never activates concurrently with a change.
|
||||
yield* Effect.sleep("24 hours").pipe(
|
||||
Effect.andThen(PubSub.publish(refresh, undefined)),
|
||||
Effect.forever,
|
||||
Effect.forkScoped,
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { describe, expect, setDefaultTimeout } from "bun:test"
|
||||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
import { Deferred, Duration, Effect, Layer, LayerMap, Schedule } from "effect"
|
||||
import { Deferred, Duration, Effect, Fiber, Layer, LayerMap, Schedule } from "effect"
|
||||
import { TestClock } from "effect/testing"
|
||||
import { define } from "@opencode-ai/plugin/effect/plugin"
|
||||
import { Event } from "@opencode-ai/schema/config"
|
||||
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
|
||||
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
|
||||
@@ -21,6 +22,7 @@ import { AbsolutePath } from "@opencode-ai/core/schema"
|
||||
import { tempGlobalLayer } from "../fixture/global"
|
||||
import { offlineModels } from "../fixture/models"
|
||||
import { tmpdirScoped } from "../fixture/tmpdir"
|
||||
import { advance } from "../lib/clock"
|
||||
import { testEffect } from "../lib/effect"
|
||||
|
||||
// Real Location boot with plugin-directory discovery, so local plugin files are loaded and reloaded.
|
||||
@@ -55,6 +57,7 @@ const npmLayer = Layer.succeed(
|
||||
const instances = Layer.effect(
|
||||
LocationServiceMap.Service,
|
||||
Effect.gen(function* () {
|
||||
const watcher = yield* Watcher.Test
|
||||
const map = yield* LayerMap.make((ref: Location.Ref) => Instance.layer(ref, { replacements: bindings }), {
|
||||
idleTimeToLive: Duration.infinity,
|
||||
})
|
||||
@@ -62,7 +65,7 @@ const instances = Layer.effect(
|
||||
Global.node.replace(tempGlobalLayer),
|
||||
offlineModels,
|
||||
Npm.node.replace(npmLayer),
|
||||
Watcher.node.replace(Watcher.configured({ enabled: false })),
|
||||
Watcher.node.replace(Layer.succeed(Watcher.Service, watcher)),
|
||||
LocationServiceMap.node.replace(Layer.succeed(LocationServiceMap.Service, map)),
|
||||
Instance.node.replace(
|
||||
Layer.succeed(Instance.Service, {
|
||||
@@ -72,14 +75,14 @@ const instances = Layer.effect(
|
||||
]
|
||||
return map
|
||||
}),
|
||||
)
|
||||
).pipe(Layer.provide(Watcher.testLayer))
|
||||
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node, SdkPlugins.node, LocationServiceMap.node]), [
|
||||
Global.node.replace(tempGlobalLayer),
|
||||
offlineModels,
|
||||
LocationServiceMap.node.replace(instances),
|
||||
]),
|
||||
]).pipe(Layer.provideMerge(Watcher.testLayer)),
|
||||
)
|
||||
|
||||
const greeter = (command: string) => `export default {
|
||||
@@ -107,6 +110,61 @@ const failed = (plugins: Plugin.Interface) =>
|
||||
)
|
||||
|
||||
describe("PluginSupervisor reload", () => {
|
||||
;(["discovered", "configured"] as const).forEach((mode) => {
|
||||
it.effect(`retains a ${mode} plugin change during initial activation`, () =>
|
||||
Effect.gen(function* () {
|
||||
const directory = yield* tmpdirScoped()
|
||||
const file = path.join(
|
||||
directory.path,
|
||||
mode === "discovered" ? ".opencode/plugins/greeter.ts" : "external/greeter/index.ts",
|
||||
)
|
||||
yield* Effect.promise(async () => {
|
||||
await Bun.write(file, greeter("greet-v1"))
|
||||
await fs.utimes(file, new Date(0), new Date(0))
|
||||
if (mode === "configured") {
|
||||
await Bun.write(
|
||||
path.join(directory.path, ".opencode/opencode.json"),
|
||||
JSON.stringify({ plugins: [path.dirname(file)] }),
|
||||
)
|
||||
}
|
||||
})
|
||||
const entered = yield* Deferred.make<void>()
|
||||
const gate = yield* Deferred.make<void>()
|
||||
const sdk = yield* SdkPlugins.Service
|
||||
yield* sdk.register(
|
||||
define({
|
||||
id: "gated",
|
||||
effect: () => Deferred.succeed(entered, undefined).pipe(Effect.andThen(Deferred.await(gate))),
|
||||
}),
|
||||
)
|
||||
const watcher = yield* Watcher.Test
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
yield* Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
const commands = yield* Command.Service
|
||||
yield* Deferred.await(entered)
|
||||
// The real ConfigPluginSource merges config-root changes and configured-path watches.
|
||||
// Emit while setup is blocked, without a bus event that could mask a lost source trigger.
|
||||
yield* Effect.promise(async () => {
|
||||
await Bun.write(file, greeter("greet-v2"))
|
||||
await fs.utimes(file, new Date(), new Date())
|
||||
})
|
||||
yield* watcher.emit({ path: file, type: "update" })
|
||||
const ready = yield* plugins.awaitActivation.pipe(Effect.forkScoped({ startImmediately: true }))
|
||||
yield* Deferred.succeed(gate, undefined)
|
||||
yield* advance(() => ready.pollUnsafe() !== undefined)
|
||||
yield* Fiber.join(ready)
|
||||
|
||||
expect(yield* commands.get("greet-v1")).toBeUndefined()
|
||||
expect(yield* commands.get("greet-v2")).toBeDefined()
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory.path) }))),
|
||||
)
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it.live("keeps the running generation when an updated local plugin fails to import", () =>
|
||||
Effect.gen(function* () {
|
||||
const directory = yield* tmpdirScoped()
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Duration, Effect, Layer, LayerMap } from "effect"
|
||||
import { Deferred, Duration, Effect, Layer, LayerMap, Stream } from "effect"
|
||||
import { TestClock } from "effect/testing"
|
||||
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 { ConfigPluginSource } from "@opencode-ai/core/config/plugin/source"
|
||||
import { Instance } from "@opencode-ai/core/instance"
|
||||
import { LocationServiceMap } from "@opencode-ai/core/location-services"
|
||||
import { Location } from "@opencode-ai/core/location"
|
||||
@@ -16,18 +18,33 @@ import { Bus } from "../../src/bus"
|
||||
import { tempGlobalLayer } from "../fixture/global"
|
||||
import { offlineModels } from "../fixture/models"
|
||||
import { tmpdirScoped } from "../fixture/tmpdir"
|
||||
import { advance } from "../lib/clock"
|
||||
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) =>
|
||||
const greeter = (command: string, plugin: string = id) =>
|
||||
define({
|
||||
id,
|
||||
id: plugin,
|
||||
effect: (ctx) =>
|
||||
ctx.command.transform((editor) => editor.add({ name: command, execute: () => Effect.void })).pipe(Effect.asVoid),
|
||||
})
|
||||
|
||||
// Every supervisor activation scans the config plugin operations once, so counting scans counts activations.
|
||||
const source = { activations: 0 }
|
||||
const sourceLayer = Layer.succeed(
|
||||
ConfigPluginSource.Service,
|
||||
ConfigPluginSource.Service.of({
|
||||
operations: () =>
|
||||
Effect.sync(() => {
|
||||
source.activations++
|
||||
return []
|
||||
}),
|
||||
changes: () => Stream.never,
|
||||
}),
|
||||
)
|
||||
|
||||
const instances = Layer.effect(
|
||||
LocationServiceMap.Service,
|
||||
Effect.gen(function* () {
|
||||
@@ -39,6 +56,7 @@ const instances = Layer.effect(
|
||||
const bindings: LayerNode.Replacements = [
|
||||
Global.node.replace(tempGlobalLayer),
|
||||
offlineModels,
|
||||
ConfigPluginSource.node.replace(sourceLayer),
|
||||
LocationServiceMap.node.replace(Layer.succeed(LocationServiceMap.Service, map)),
|
||||
Instance.node.replace(
|
||||
Layer.succeed(Instance.Service, {
|
||||
@@ -97,4 +115,117 @@ describe("PluginSupervisor", () => {
|
||||
).toBe(true)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("activates the initial generation without waiting for the reload debounce", () =>
|
||||
Effect.gen(function* () {
|
||||
source.activations = 0
|
||||
const directory = yield* tmpdirScoped()
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
yield* Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
const commands = yield* Command.Service
|
||||
// The TestClock never advances here, so any timer between boot and the first activation would hang this.
|
||||
yield* plugins.awaitActivation
|
||||
expect(yield* commands.get("instance-greet")).toBeDefined()
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory.path) }))),
|
||||
)
|
||||
expect(source.activations).toBe(1)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("refreshes every 24 hours without adding an immediate reload", () =>
|
||||
Effect.gen(function* () {
|
||||
source.activations = 0
|
||||
const directory = yield* tmpdirScoped()
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
yield* Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
yield* plugins.awaitActivation
|
||||
yield* TestClock.adjust("23 hours")
|
||||
expect(source.activations).toBe(1)
|
||||
|
||||
yield* TestClock.adjust("1 hour")
|
||||
yield* advance(() => source.activations === 2)
|
||||
yield* plugins.awaitActivation
|
||||
|
||||
yield* TestClock.adjust("24 hours")
|
||||
yield* advance(() => source.activations === 3)
|
||||
yield* plugins.awaitActivation
|
||||
expect(source.activations).toBe(3)
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory.path) }))),
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("reloads for a trigger published while the initial generation is activating", () =>
|
||||
Effect.gen(function* () {
|
||||
source.activations = 0
|
||||
const sdk = yield* SdkPlugins.Service
|
||||
const entered = yield* Deferred.make<void>()
|
||||
const gate = yield* Deferred.make<void>()
|
||||
yield* sdk.register(
|
||||
define({
|
||||
id: "gated",
|
||||
effect: () => Deferred.succeed(entered, undefined).pipe(Effect.andThen(Deferred.await(gate))),
|
||||
}),
|
||||
)
|
||||
let late = false
|
||||
const directory = yield* tmpdirScoped()
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
yield* Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
const commands = yield* Command.Service
|
||||
yield* Deferred.await(entered)
|
||||
// Generation 0 is mid-setup: the reload feed must already be subscribed for this update to count.
|
||||
yield* sdk.register(
|
||||
define({
|
||||
id: "late",
|
||||
effect: (ctx) =>
|
||||
ctx.command
|
||||
.transform((editor) => editor.add({ name: "late-greet", execute: () => Effect.void }))
|
||||
.pipe(Effect.tap(() => Effect.sync(() => (late = true)))),
|
||||
}),
|
||||
)
|
||||
yield* Deferred.succeed(gate, undefined)
|
||||
yield* advance(() => late)
|
||||
yield* plugins.awaitActivation
|
||||
expect(yield* commands.get("late-greet")).toBeDefined()
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory.path) }))),
|
||||
)
|
||||
expect(source.activations).toBe(2)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("coalesces a burst of reload triggers after the initial generation into one activation", () =>
|
||||
Effect.gen(function* () {
|
||||
source.activations = 0
|
||||
const sdk = yield* SdkPlugins.Service
|
||||
const directory = yield* tmpdirScoped()
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
yield* Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
const commands = yield* Command.Service
|
||||
yield* plugins.awaitActivation
|
||||
expect(source.activations).toBe(1)
|
||||
|
||||
yield* sdk.register(greeter("greet-a", "a"))
|
||||
yield* sdk.register(greeter("greet-b", "b"))
|
||||
yield* sdk.register(greeter("greet-c", "c"))
|
||||
yield* advance(() => source.activations > 1)
|
||||
yield* plugins.awaitActivation
|
||||
expect(yield* commands.get("greet-a")).toBeDefined()
|
||||
expect(yield* commands.get("greet-c")).toBeDefined()
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory.path) }))),
|
||||
)
|
||||
expect(source.activations).toBe(2)
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -195,7 +195,8 @@ it.live(
|
||||
expect(yield* Effect.promise<unknown>(() => response.json())).toEqual({ data: [] })
|
||||
}
|
||||
}
|
||||
expect(boots).toEqual([])
|
||||
// Reading permissions or forms builds the Session's Instance and starts its plugin activation at once; the
|
||||
// ordering assertion after the prompts is what proves each Session boots exactly once.
|
||||
|
||||
for (const config of configs) {
|
||||
const session = yield* sessions.get(config.id)
|
||||
|
||||
Reference in New Issue
Block a user