From e469e85a2907ca59135d9d30c74e500d1ba9d642 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:57:47 -0500 Subject: [PATCH] perf(core): rebuild providers on account switch only when a plugin's state changed (#60787) --- packages/core/src/plugin/provider/chatgpt.ts | 6 ++- packages/core/src/plugin/provider/gitlab.ts | 11 +++-- .../plugin/provider/openai-context-caps.ts | 4 +- packages/core/src/plugin/provider/openai.ts | 7 ++- .../src/plugin/provider/snowflake-cortex.ts | 4 +- .../core/test/plugin/provider-openai.test.ts | 46 +++++++++++++++++++ 6 files changed, 69 insertions(+), 9 deletions(-) diff --git a/packages/core/src/plugin/provider/chatgpt.ts b/packages/core/src/plugin/provider/chatgpt.ts index c1dbe339b43..6821a0bb1e3 100644 --- a/packages/core/src/plugin/provider/chatgpt.ts +++ b/packages/core/src/plugin/provider/chatgpt.ts @@ -2,7 +2,7 @@ import type { IntegrationOAuthMethodRegistration } from "@opencode/plugin/effect import type { Context } from "@opencode/plugin/effect/plugin" import { define } from "@opencode/plugin/effect/plugin" // import { Deferred, Duration, Effect, Option, Schema, Semaphore, Stream } from "effect" -import { Deferred, Effect, Option, Schema, Semaphore, Stream } from "effect" +import { Deferred, Effect, Equal, Option, Schema, Semaphore, Stream } from "effect" import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http" import type { Server, ServerResponse } from "node:http" import { App } from "../../app.js" @@ -224,12 +224,14 @@ export const ChatGPTPlugin = define({ ? yield* ctx.integration.connection.resolve(connection).pipe(Effect.orElseSucceed(() => undefined)) : undefined chatgpt = credential?.type === "oauth" && credential.methodID === methodID ? credential : undefined + const previous = source source = chatgpt ? connection : undefined // if (previous !== IntegrationConnection.key(source)) { // const metadata = Option.getOrUndefined(decodeMetadata(chatgpt?.metadata)) // const stored = metadata ? yield* ctx.storage.get(modelCacheKey(metadata.clientID)) : undefined // available = Option.getOrUndefined(decodeCachedModels(stored)) // } + return !Equal.equals(previous, source) }) yield* ctx.integration.transform((editor) => { @@ -357,7 +359,7 @@ export const ChatGPTPlugin = define({ // }), // ) // }) - const reload = () => loading.withPermit(load().pipe(Effect.andThen(ctx.provider.reload()))) + const reload = () => loading.withPermit(load().pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void)))) // .pipe(Effect.andThen(refreshModels().pipe(Effect.forkScoped, Effect.asVoid))) yield* bus.subscribe(Credential.Event.Switched).pipe( Stream.filter((event) => event.data.integrationID === integrationID), diff --git a/packages/core/src/plugin/provider/gitlab.ts b/packages/core/src/plugin/provider/gitlab.ts index 8821788fde9..e009f67d12a 100644 --- a/packages/core/src/plugin/provider/gitlab.ts +++ b/packages/core/src/plugin/provider/gitlab.ts @@ -1,6 +1,6 @@ import os from "os" import { App } from "../../app.js" -import { Clock, Deferred, Effect, Exit, Option, Schema, Semaphore, Stream } from "effect" +import { Clock, Deferred, Effect, Equal, Exit, Option, Schema, Semaphore, Stream } from "effect" import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http" import type { Server, ServerResponse } from "node:http" import { define } from "@opencode/plugin/effect/plugin" @@ -71,9 +71,10 @@ export const GitLabPlugin = define({ ? yield* ctx.integration.connection.resolve(stored).pipe(Effect.orElseSucceed(() => undefined)) : undefined if (!stored || !credential || credential.type === "external") { + const changed = loaded.models !== undefined || loaded.connection !== undefined loaded.models = undefined loaded.connection = undefined - return + return changed } const provider = yield* providers.get(providerID) @@ -118,9 +119,11 @@ export const GitLabPlugin = define({ IntegrationConnection.key(connection) !== IntegrationConnection.key(yield* ctx.integration.connection.active(integrationID)) ) - return + return false + const changed = !Equal.equals(remote?.models, loaded.models) || !Equal.equals(connection, loaded.connection) loaded.models = remote?.models loaded.connection = connection + return changed }) yield* ctx.integration.transform((editor) => { @@ -291,7 +294,7 @@ export const GitLabPlugin = define({ ], }) }) - const refresh = () => loading.withPermit(load().pipe(Effect.andThen(ctx.provider.reload()))) + const refresh = () => loading.withPermit(load().pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void)))) yield* bus.subscribe(Credential.Event.Switched).pipe( Stream.filter((event) => event.data.integrationID === integrationID), Stream.runForEach(refresh), diff --git a/packages/core/src/plugin/provider/openai-context-caps.ts b/packages/core/src/plugin/provider/openai-context-caps.ts index dcdbed6f083..45c13e5d839 100644 --- a/packages/core/src/plugin/provider/openai-context-caps.ts +++ b/packages/core/src/plugin/provider/openai-context-caps.ts @@ -25,7 +25,9 @@ export const OpenAIContextCapsPlugin = define({ const credential = connection ? yield* ctx.integration.connection.resolve(connection).pipe(Effect.orElseSucceed(() => undefined)) : undefined + const previous = signedIn signedIn = credential?.type === "oauth" && methodIDs.has(credential.methodID) + return signedIn !== previous }) yield* load() @@ -41,7 +43,7 @@ export const OpenAIContextCapsPlugin = define({ }) yield* bus.subscribe(Credential.Event.Switched).pipe( Stream.filter((event) => event.data.integrationID === integrationID), - Stream.runForEach(() => load().pipe(Effect.andThen(ctx.provider.reload()))), + Stream.runForEach(() => load().pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void)))), Effect.forkScoped({ startImmediately: true }), ) }), diff --git a/packages/core/src/plugin/provider/openai.ts b/packages/core/src/plugin/provider/openai.ts index d005eb17ada..413329b3fff 100644 --- a/packages/core/src/plugin/provider/openai.ts +++ b/packages/core/src/plugin/provider/openai.ts @@ -243,11 +243,16 @@ export const OpenAIPlugin = define({ const credential = connection ? yield* ctx.integration.connection.resolve(connection).pipe(Effect.orElseSucceed(() => undefined)) : undefined + const previous = chatgpt chatgpt = credential?.type === "oauth" && (credential.methodID === browserMethodID || credential.methodID === headlessMethodID) ? credential : undefined + return ( + (previous === undefined) !== (chatgpt === undefined) || + previous?.metadata?.accountID !== chatgpt?.metadata?.accountID + ) }) yield* ctx.integration.transform((editor) => { @@ -326,7 +331,7 @@ export const OpenAIPlugin = define({ }) for (const name of ["context", "compaction"] as const) yield* ctx.session.hook(name, omitOutputLimit, { providerID: Provider.ID.openai }) - const refresh = () => loading.withPermit(load().pipe(Effect.andThen(ctx.provider.reload()))) + const refresh = () => loading.withPermit(load().pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void)))) yield* bus.subscribe(Credential.Event.Switched).pipe( Stream.filter((event) => event.data.integrationID === Integration.ID.make("openai")), Stream.runForEach(refresh), diff --git a/packages/core/src/plugin/provider/snowflake-cortex.ts b/packages/core/src/plugin/provider/snowflake-cortex.ts index 0249123e018..564dfba3fa0 100644 --- a/packages/core/src/plugin/provider/snowflake-cortex.ts +++ b/packages/core/src/plugin/provider/snowflake-cortex.ts @@ -172,7 +172,9 @@ export const SnowflakeCortexPlugin = define({ const load = Effect.gen(function* () { const value = (yield* saved)?.value + const previous = account.value account.value = normalizeAccount(process.env.SNOWFLAKE_ACCOUNT ?? value?.metadata?.account ?? configured?.account) + return account.value !== previous }) yield* load yield* ctx.provider.transform((providers) => { @@ -201,7 +203,7 @@ export const SnowflakeCortexPlugin = define({ }) yield* bus.subscribe(Credential.Event.Switched).pipe( Stream.filter((event) => event.data.integrationID === integrationID), - Stream.runForEach(() => load.pipe(Effect.andThen(ctx.provider.reload()))), + Stream.runForEach(() => load.pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void)))), Effect.forkScoped({ startImmediately: true }), ) yield* ctx.session.hook( diff --git a/packages/core/test/plugin/provider-openai.test.ts b/packages/core/test/plugin/provider-openai.test.ts index 29957e56526..694fcb2d1ae 100644 --- a/packages/core/test/plugin/provider-openai.test.ts +++ b/packages/core/test/plugin/provider-openai.test.ts @@ -19,6 +19,7 @@ import { AbsolutePath } from "@opencode/core/schema" import { SessionModelRequest } from "@opencode/core/session/model-request" import { SessionModelTransport } from "@opencode/core/session/model-transport" import { SessionRunnerModel } from "@opencode/core/session/runner/model" +import { drain } from "../lib/clock" import { testEffect } from "../lib/effect" import { PluginTestLayer } from "./fixture" @@ -243,6 +244,51 @@ describe("OpenAIPlugin", () => { }), ) + it.effect("reloads providers on a switch only when the ChatGPT login changes", () => + Effect.gen(function* () { + const catalog = yield* Provider.Service + const credentials = yield* Credential.Service + yield* catalog.transform((catalog) => { + catalog.update(Provider.ID.openai, () => {}) + }) + const first = yield* credentials.create({ + integrationID: Integration.ID.make("openai"), + value: Credential.Key.make({ type: "key", key: "sk-first" }), + }) + yield* addPlugin() + const folds = { count: 0 } + yield* catalog.transform(() => { + folds.count++ + }) + const before = folds.count + + yield* credentials.create({ + integrationID: Integration.ID.make("openai"), + value: Credential.Key.make({ type: "key", key: "sk-second" }), + }) + yield* credentials.activate(first.id) + yield* drain + expect(folds.count).toBe(before) + + yield* credentials.create({ + integrationID: Integration.ID.make("openai"), + value: Credential.OAuth.make({ + type: "oauth", + methodID: Integration.MethodID.make("chatgpt-browser"), + access: "chatgpt-token", + refresh: "refresh", + expires: Date.now() + 60_000, + metadata: { accountID: "acct_123" }, + }), + }) + yield* drain + expect(folds.count).toBeGreaterThan(before) + expect(required(yield* catalog.get(Provider.ID.openai)).headers).toMatchObject({ + "chatgpt-account-id": "acct_123", + }) + }), + ) + it.effect("omits the default output limit from OpenAI steps and compaction", () => Effect.gen(function* () { yield* addPlugin()