mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
fix(core): check plugins for updates every 24 hours, not every 48 (#65056)
This commit is contained in:
committed by
GitHub
Unverified
parent
d643c87587
commit
2c9060237f
@@ -1,7 +1,7 @@
|
||||
export * as PluginSupervisor from "./supervisor.js"
|
||||
|
||||
import { Event } from "@opencode/schema/config"
|
||||
import { Cause, Clock, Effect, Layer, Queue, Stream } from "effect"
|
||||
import { Cause, Effect, Layer, Queue, Stream } from "effect"
|
||||
import { ConfigPluginSource } from "../config/plugin/source.js"
|
||||
import { makeLocationNode } from "@opencode/util/effect/app-node"
|
||||
import { Bus } from "../bus.js"
|
||||
@@ -136,9 +136,10 @@ export const layer = Layer.effectDiscard(
|
||||
let running = new Map<string, Plugin.Generation>()
|
||||
let outdated = new Set<string>()
|
||||
const updating = new Set<string>()
|
||||
// When each `@latest` plugin last updated on its own. One attempt per check interval, so an update that doesn't
|
||||
// take, or a new version that fails to load, isn't retried on every reload.
|
||||
// The check interval in which each `@latest` plugin last updated on its own. One attempt per interval, so an update
|
||||
// that doesn't take, or a new version that fails to load, isn't retried on every reload.
|
||||
const attempted = new Map<string, number>()
|
||||
let cycle = 0
|
||||
let generation = 0
|
||||
let observed = 0
|
||||
|
||||
@@ -205,18 +206,17 @@ export const layer = Layer.effectDiscard(
|
||||
Effect.flatMap((checked) =>
|
||||
Effect.gen(function* () {
|
||||
if (current !== generation) return
|
||||
const now = yield* Clock.currentTimeMillis
|
||||
// Only a plugin written as `name@latest` updates on its own; the update reloads it.
|
||||
yield* Effect.forEach(
|
||||
checked.filter(
|
||||
([target, available]) =>
|
||||
available &&
|
||||
target.endsWith("@latest") &&
|
||||
now - (attempted.get(target) ?? Number.NEGATIVE_INFINITY) >= PluginUpdate.interval,
|
||||
attempted.get(target) !== cycle,
|
||||
),
|
||||
([target]) =>
|
||||
// Recorded first: the update itself triggers reloads, which check again while it runs.
|
||||
Effect.sync(() => attempted.set(target, now)).pipe(
|
||||
Effect.sync(() => attempted.set(target, cycle)).pipe(
|
||||
Effect.andThen(updates.update(target)),
|
||||
Effect.catchCause((cause) => Effect.logWarning("failed to update plugin", { target, cause })),
|
||||
),
|
||||
@@ -247,7 +247,7 @@ export const layer = Layer.effectDiscard(
|
||||
)
|
||||
yield* watch(sources.changes())
|
||||
yield* watch(modules.changes())
|
||||
yield* watch(Stream.fromEffectRepeat(Effect.sleep(PluginUpdate.interval)))
|
||||
yield* watch(updates.intervals().pipe(Stream.tap((next) => Effect.sync(() => (cycle = next)))))
|
||||
yield* watch(bus.subscribe([Event.Updated, SdkPlugins.Updated]))
|
||||
yield* watch(
|
||||
updates.changes().pipe(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export * as PluginUpdate from "./update.js"
|
||||
|
||||
import { Clock, Context, Effect, Exit, Layer, Option, PubSub, Stream } from "effect"
|
||||
import { Context, Effect, Exit, Layer, Option, PubSub, Stream } from "effect"
|
||||
import { Npm } from "@opencode/util/npm"
|
||||
import { EffectFlock } from "@opencode/util/effect-flock"
|
||||
import { makeGlobalNode } from "@opencode/util/effect/app-node"
|
||||
@@ -19,6 +19,8 @@ export interface Interface {
|
||||
readonly check: (target: string, options?: { readonly refresh?: boolean }) => Effect.Effect<boolean>
|
||||
readonly update: (target: string) => Effect.Effect<void, Npm.InstallFailedError | EffectFlock.LockError>
|
||||
readonly changes: () => Stream.Stream<Change>
|
||||
/** Emits the number of each new check interval. Checks after it ask the registry or Git remote again. */
|
||||
readonly intervals: () => Stream.Stream<number>
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Interface>()("@opencode/PluginUpdate") {}
|
||||
@@ -28,23 +30,32 @@ const layer = Layer.effect(
|
||||
Effect.gen(function* () {
|
||||
const npm = yield* Npm.Service
|
||||
const locks = KeyedMutex.makeUnsafe<string>()
|
||||
const status = new Map<string, { readonly outdated: boolean; readonly checkedAt: number }>()
|
||||
// A result stays fresh until its interval ends, however late in the interval it was checked. A fixed age from the
|
||||
// check would still be fresh when the next interval starts, skipping that interval's check.
|
||||
const status = new Map<string, { readonly outdated: boolean; readonly cycle: number }>()
|
||||
const changes = yield* PubSub.unbounded<Change>()
|
||||
const intervals = yield* PubSub.unbounded<number>()
|
||||
let cycle = 0
|
||||
yield* Effect.sleep(interval).pipe(
|
||||
Effect.andThen(Effect.suspend(() => PubSub.publish(intervals, ++cycle))),
|
||||
Effect.forever,
|
||||
Effect.forkScoped,
|
||||
)
|
||||
|
||||
return Service.of({
|
||||
check: (target, options) =>
|
||||
locks.withLock(target)(
|
||||
Effect.gen(function* () {
|
||||
const checkedAt = yield* Clock.currentTimeMillis
|
||||
const current = status.get(target)
|
||||
if (!options?.refresh && current && checkedAt - current.checkedAt < interval) return current.outdated
|
||||
const checking = cycle
|
||||
const previous = status.get(target)
|
||||
if (!options?.refresh && previous?.cycle === checking) return previous.outdated
|
||||
const outdated = yield* npm.check(target).pipe(
|
||||
Effect.tapCause((cause) => Effect.logWarning("failed to check plugin update", { target, cause })),
|
||||
Effect.option,
|
||||
)
|
||||
const value = Option.getOrElse(outdated, () => current?.outdated ?? false)
|
||||
status.set(target, { outdated: value, checkedAt })
|
||||
if ((current?.outdated ?? false) !== value)
|
||||
const value = Option.getOrElse(outdated, () => previous?.outdated ?? false)
|
||||
status.set(target, { outdated: value, cycle: checking })
|
||||
if ((previous?.outdated ?? false) !== value)
|
||||
yield* PubSub.publish(changes, { target, outdated: value, updating: false })
|
||||
return value
|
||||
}),
|
||||
@@ -68,6 +79,7 @@ const layer = Layer.effect(
|
||||
}),
|
||||
),
|
||||
changes: () => Stream.fromPubSub(changes),
|
||||
intervals: () => Stream.fromPubSub(intervals),
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Deferred, Duration, Effect, Layer, LayerMap, Stream } from "effect"
|
||||
import { Clock, Deferred, Duration, Effect, Layer, LayerMap, Stream } from "effect"
|
||||
import { TestClock } from "effect/testing"
|
||||
import type { LanguageModelV3 } from "@ai-sdk/provider"
|
||||
import { define } from "@opencode/plugin/effect/plugin"
|
||||
@@ -17,13 +17,14 @@ import { Model } from "@opencode/core/model"
|
||||
import { Plugin } from "@opencode/core/plugin"
|
||||
import { Provider } from "@opencode/core/provider"
|
||||
import { SdkPlugins } from "@opencode/core/plugin/sdk"
|
||||
import { PluginUpdate } from "@opencode/core/plugin/update"
|
||||
import { AbsolutePath } from "@opencode/core/schema"
|
||||
import { Database } from "../../src/database/database"
|
||||
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 { advance, drain } from "../lib/clock"
|
||||
import { testEffect } from "../lib/effect"
|
||||
|
||||
const id = Plugin.ID.make("account-prompts")
|
||||
@@ -50,8 +51,8 @@ const sourceLayer = Layer.succeed(
|
||||
}),
|
||||
)
|
||||
|
||||
// Package checks report `available`; updates are recorded instead of installed.
|
||||
const packages = { available: false, updated: [] as string[] }
|
||||
// Package checks report `available` after `delay`; updates are recorded instead of installed.
|
||||
const packages = { available: false, delay: Duration.zero, checks: 0, updated: [] as string[] }
|
||||
|
||||
const instances = Layer.effect(
|
||||
LocationServiceMap.Service,
|
||||
@@ -78,7 +79,11 @@ const instances = Layer.effect(
|
||||
}),
|
||||
),
|
||||
resolve: (name) => Effect.succeed({ directory: "", name }),
|
||||
check: () => Effect.succeed(packages.available),
|
||||
check: () =>
|
||||
Effect.sleep(packages.delay).pipe(
|
||||
Effect.andThen(Effect.sync(() => packages.checks++)),
|
||||
Effect.as(packages.available),
|
||||
),
|
||||
update: (pkg) =>
|
||||
packages.available
|
||||
? Effect.sync(() => {
|
||||
@@ -227,6 +232,55 @@ describe("PluginSupervisor", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("checks and updates once per interval, 24 hours after a slow startup", () =>
|
||||
Effect.gen(function* () {
|
||||
source.operations = [{ type: "add", target: "latest-plugin@latest", options: {} }]
|
||||
packages.available = true
|
||||
// Checks take a while, so the first ones finish well after startup.
|
||||
packages.delay = Duration.seconds(10)
|
||||
packages.checks = 0
|
||||
packages.updated = []
|
||||
source.activations = 0
|
||||
const sdk = yield* SdkPlugins.Service
|
||||
const directory = yield* tmpdirScoped()
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
const started = yield* Clock.currentTimeMillis
|
||||
yield* Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
yield* advance(() => packages.updated.length === 1)
|
||||
// The update reloads and checks again; the newer version is still reported but isn't installed twice.
|
||||
yield* drain
|
||||
yield* plugins.awaitActivation
|
||||
expect(packages.updated).toEqual(["latest-plugin@latest"])
|
||||
// Other reloads in the same interval reuse the checks.
|
||||
const checks = packages.checks
|
||||
const activations = source.activations
|
||||
yield* sdk.register(greeter("interval-greet", "interval"))
|
||||
yield* advance(() => source.activations > activations)
|
||||
yield* drain
|
||||
yield* plugins.awaitActivation
|
||||
expect(packages.checks).toBe(checks)
|
||||
expect(packages.updated).toHaveLength(1)
|
||||
// The scheduled check exactly one interval after startup checks and installs again.
|
||||
yield* TestClock.adjust(started + PluginUpdate.interval - (yield* Clock.currentTimeMillis))
|
||||
yield* advance(() => packages.updated.length === 2)
|
||||
yield* drain
|
||||
expect(packages.checks).toBeGreaterThan(checks)
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory.path) }))),
|
||||
Effect.ensuring(
|
||||
Effect.sync(() => {
|
||||
source.operations = []
|
||||
packages.available = false
|
||||
packages.delay = Duration.zero
|
||||
}),
|
||||
),
|
||||
)
|
||||
expect(packages.updated).toEqual(["latest-plugin@latest", "latest-plugin@latest"])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("reloads for a trigger published while the initial generation is activating", () =>
|
||||
Effect.gen(function* () {
|
||||
source.activations = 0
|
||||
|
||||
Reference in New Issue
Block a user