GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(core): recover Console models after startup failures by kitlangton · Pull Request #45759 · anomalyco/opencode

fix(core): recover Console models after startup failures #45759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kitlangton wants to merge 3 commits into v2 from console-recovery

Uh oh!

There was an error while loading. Please reload this page.

Closed

fix(core): recover Console models after startup failures #45759

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
b70bdad
fix(core): recover Console models after startup failures
kitlangton Aug 27, 2026
79f5080
fix(core): keep Console inventory recovery plugin-local
kitlangton Aug 27, 2026
5b67d64
refactor(core): simplify Console recovery lifecycle
kitlangton Aug 28, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
refactor(core): simplify Console recovery lifecycle
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@kitlangton
kitlangton committed Aug 28, 2026
commit 5b67d64f9451b47d12c876174a6cb899a28892e0

There are no files selected for viewing

152 changes: 63 additions & 89 deletions 152 packages/core/src/plugin/provider/opencode.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
import {
Cause,
Duration,
Effect,
Exit,
FiberHandle,
Latch,
Option,
Schedule,
Schema,
Stream,
SynchronizedRef,
} from "effect"
import { Cause, Duration, Effect, Exit, Latch, Option, Schedule, Schema, Stream } from "effect"
import type { Scope } from "effect"
import type { IntegrationOAuthMethodRegistration } from "@opencode-ai/plugin/effect/integration"
import { define } from "@opencode-ai/plugin/effect/plugin"
Expand Down Expand Up @@ -127,71 +115,59 @@ export const OpencodePlugin = define<HttpClient.HttpClient | Bus.Service | Scope
draft.method.update({ integrationID: "opencode", method: { type: "key", label: "API key (service account)" } })
})

// Activation batches transforms; stored OAuth credentials need the refresh implementation before resolution.
if ((yield* ctx.integration.connection.active("opencode"))?.type === "credential") {
const registered = yield* ctx.integration
.get({ integrationID: Integration.ID.make("opencode") })
.pipe(Effect.orElseSucceed(() => undefined))
if (!registered?.data?.methods.some((method) => method.type === "oauth" && method.id === methodID))
yield* ctx.integration.reload()
}

const read = Effect.fn("OpencodePlugin.readCache")(function* () {
const connection = yield* ctx.integration.connection.active("opencode")
// Stored connection IDs survive token refresh and separate accounts, servers, and organizations.
const cached =
connection?.type === "credential"
? yield* ctx.storage.get(`inventory:${connection.id}`).pipe(
Effect.catchCauseIf(
(cause) => !Cause.hasInterruptsOnly(cause),
(cause) =>
? yield* ctx.storage
.get(`inventory:${connection.id}`)
.pipe(
Effect.catchDefect((cause) =>
Effect.logWarning("failed to read Console inventory cache", { cause }).pipe(Effect.as(undefined)),
),
)
),
)
: undefined
return { connection, providers: Option.getOrUndefined(Schema.decodeUnknownOption(CachedInventory)(cached)) }
})
const inventory = yield* read().pipe(Effect.flatMap(SynchronizedRef.make))
let inventory = yield* read()
const ready = yield* Latch.make()
const worker = yield* FiberHandle.make<void>()

const refresh = Effect.fn("OpencodePlugin.refresh")(function* () {
const result = yield* SynchronizedRef.modifyEffect(inventory, (current) =>
Effect.gen(function* () {
const providers = current.connection
? yield* ctx.integration.connection.resolve(current.connection).pipe(
Effect.flatMap((credential) =>
credential
? fetchProviders(http, credential).pipe(Effect.map((providers) => providers ?? {}))
: Effect.undefined,
),
Effect.retry({ while: retryable, times: 2, schedule: Schedule.exponential(200) }),
Effect.timeout("5 seconds"),
)
: undefined
const next = { connection: current.connection, providers }
return [{ changed: !isDeepStrictEqual(current.providers ?? {}, providers ?? {}), next }, next] as const
}),
)
if (result.changed) yield* ctx.catalog.reload()
if (result.next.connection?.type !== "credential" || result.next.providers === undefined) return
const cached = Schema.encodeOption(CachedInventory)(result.next.providers)
// Activation batches transforms; materialize OAuth refresh before resolution, but not before cache restore.
if (inventory.connection?.type === "credential") {
const registered = yield* ctx.integration
.get({ integrationID: Integration.ID.make("opencode") })
.pipe(Effect.orElseSucceed(() => undefined))
if (!registered?.data?.methods.some((method) => method.type === "oauth" && method.id === methodID))
yield* ctx.integration.reload()
}
const providers = inventory.connection
? yield* ctx.integration.connection.resolve(inventory.connection).pipe(
Effect.flatMap((credential) =>
credential
? fetchProviders(http, credential).pipe(Effect.map((providers) => providers ?? {}))
: Effect.undefined,
),
Effect.retry({ while: retryable, times: 2, schedule: Schedule.exponential(200) }),
Effect.timeout("5 seconds"),
)
: undefined
if (isDeepStrictEqual(inventory.providers, providers)) return
inventory = { connection: inventory.connection, providers }
yield* ctx.catalog.reload()
if (inventory.connection?.type !== "credential" || providers === undefined) return
const cached = Schema.encodeOption(CachedInventory)(providers)
yield* (
Option.isSome(cached)
? ctx.storage.set(`inventory:${result.next.connection.id}`, cached.value)
: ctx.storage.remove(`inventory:${result.next.connection.id}`)
).pipe(
Effect.catchCauseIf(
(cause) => !Cause.hasInterruptsOnly(cause),
(cause) => Effect.logWarning("failed to persist Console inventory cache", { cause }),
),
)
? ctx.storage.set(`inventory:${inventory.connection.id}`, cached.value)
: ctx.storage.remove(`inventory:${inventory.connection.id}`)
).pipe(Effect.catchDefect((cause) => Effect.logWarning("failed to persist Console inventory cache", { cause })))
})

yield* ctx.catalog.transform((catalog) => {
const current = SynchronizedRef.getUnsafe(inventory)
// Later transforms may mutate nested settings; keep the source inventory independent of catalog policy.
for (const [providerID, item] of Object.entries(structuredClone(current.providers ?? {}))) {
for (const [providerID, item] of Object.entries(structuredClone(inventory.providers ?? {}))) {
catalog.provider.update(providerID, (provider) => {
provider.integrationID = Integration.ID.make("opencode")
if (item.name !== undefined) provider.name = item.name
Expand Down Expand Up @@ -251,7 +227,7 @@ export const OpencodePlugin = define<HttpClient.HttpClient | Bus.Service | Scope

const item = catalog.provider.get(Provider.ID.opencode)
if (!item) return
const hasKey = Boolean(process.env.OPENCODE_API_KEY || current.connection || item.provider.settings?.apiKey)
const hasKey = Boolean(process.env.OPENCODE_API_KEY || inventory.connection || item.provider.settings?.apiKey)
catalog.provider.update(item.provider.id, (provider) => {
if (!hasKey) {
provider.activation = "enabled"
Expand All @@ -267,36 +243,35 @@ export const OpencodePlugin = define<HttpClient.HttpClient | Bus.Service | Scope
}
})

const start = Effect.fn("OpencodePlugin.startRefresh")(function* () {
if (SynchronizedRef.getUnsafe(inventory).providers !== undefined) yield* ready.open
yield* FiberHandle.run(
worker,
refresh().pipe(
Effect.tapError((cause) => Effect.logWarning("failed to load OpenCode provider config", { cause })),
Effect.onExit((exit) =>
Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause) ? Effect.void : ready.open,
),
Effect.retry({
while: retryable,
schedule: Schedule.min([Schedule.exponential("5 seconds"), Schedule.spaced("30 seconds")]),
}),
Effect.catch(() => Effect.void),
),
)
})
// Switching waits for the previous refresh to stop, so only one worker writes the captured inventory.
yield* bus.subscribe(Credential.Event.Switched).pipe(
Stream.filter((event) => event.data.integrationID === Integration.ID.make("opencode")),
Stream.runForEach(() =>
Effect.gen(function* () {
yield* FiberHandle.clear(worker)
yield* read().pipe(Effect.flatMap((next) => SynchronizedRef.set(inventory, next)))
yield* ctx.catalog.reload()
yield* start()
}),
Stream.prepend([undefined]),
Stream.switchMap((event) =>
Stream.fromEffect(
Effect.gen(function* () {
if (event) {
inventory = yield* read()
yield* ctx.catalog.reload()
}
if (inventory.providers !== undefined) yield* ready.open
yield* refresh().pipe(
Effect.tapError((cause) => Effect.logWarning("failed to load OpenCode provider config", { cause })),
Effect.onExit((exit) =>
Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause) ? Effect.void : ready.open,
),
Effect.retry({
while: retryable,
schedule: Schedule.min([Schedule.exponential("5 seconds"), Schedule.spaced("30 seconds")]),
}),
Effect.ignore,
)
}),
),
),
Stream.runDrain,
Effect.forkScoped({ startImmediately: true }),
)
yield* start()
yield* ready.await
}),
})
Expand Down Expand Up @@ -337,9 +312,8 @@ function cacheable(value: unknown): boolean {

function cacheableURL(value: string | undefined) {
if (value === undefined) return true
if (!URL.canParse(value)) return false
const url = new URL(value)
return !url.username && !url.password && !url.search && !url.hash
const url = URL.parse(value)
return url !== null && !url.username && !url.password && !url.search && !url.hash
}

function retryable(cause: unknown): boolean {
Expand Down
5 changes: 3 additions & 2 deletions 5 packages/core/test/plugin/provider-opencode-recovery.test.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Plugin } from "@opencode-ai/core/plugin"
import { PluginHost } from "@opencode-ai/core/plugin/host"
import { OpencodePlugin } from "@opencode-ai/core/plugin/provider/opencode"
import { Provider } from "@opencode-ai/core/provider"
import { State } from "@opencode-ai/core/state"
import { Effect, Fiber, Option, Stream } from "effect"
import { testEffect } from "../lib/effect"
import { PluginTestLayer } from "./fixture"
Expand All @@ -19,8 +20,8 @@ const hiddenID = Model.ID.make("hidden")

const addPlugin = Effect.fn(function* () {
const plugin = yield* Plugin.Service
const host = yield* PluginHost.make(plugin)
yield* OpencodePlugin.effect(host)
const host = yield* PluginHost.make(plugin, OpencodePlugin.id)
yield* State.batch(OpencodePlugin.effect(host))
})

const connect = Effect.fn(function* (respond: (request: Request, attempt: number) => Response | Promise<Response>) {
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.