From 94353780cf289dcecee2866322dbbe1f34d51641 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Mon, 26 Oct 2026 01:09:42 -0500 Subject: [PATCH] fix(core): coordinate credential refreshes across locations and processes (#54023) --- packages/core/src/credential.ts | 30 ++++- packages/core/src/integration.ts | 29 +++-- packages/core/test/config/config.test.ts | 2 + packages/core/test/credential-lock.test.ts | 41 ++++++ packages/core/test/fixture/config-nodes.ts | 1 + .../core/test/integration-refresh.test.ts | 118 ++++++++++++++++++ packages/core/test/integration.test.ts | 1 + packages/core/test/mcp-oauth.test.ts | 1 + 8 files changed, 213 insertions(+), 10 deletions(-) create mode 100644 packages/core/test/credential-lock.test.ts create mode 100644 packages/core/test/integration-refresh.test.ts diff --git a/packages/core/src/credential.ts b/packages/core/src/credential.ts index d764fd955e2..f1ca9148ab3 100644 --- a/packages/core/src/credential.ts +++ b/packages/core/src/credential.ts @@ -1,13 +1,14 @@ export * as Credential from "./credential.js" import { asc, desc, eq } from "drizzle-orm" -import { Cause, Context, Effect, Layer, Schema } from "effect" +import { Cause, Context, Effect, Fiber, Layer, Schema } from "effect" import { Credential } from "@opencode/schema/credential" import { CredentialLegacy } from "@opencode/schema/credential-legacy" import { Integration } from "@opencode/schema/integration" import { Database } from "./database/database.js" import { Bus } from "./bus.js" import { makeGlobalNode } from "@opencode/util/effect/app-node" +import { EffectFlock } from "@opencode/util/effect-flock" import { CredentialTable } from "./credential/sql.js" import { ErrorSummary } from "./util/error-summary.js" @@ -42,6 +43,11 @@ export interface Interface { readonly list: (integrationID: Integration.ID) => Effect.Effect /** Returns one stored credential by ID. */ readonly get: (id: ID) => Effect.Effect + /** Concurrent callers for the same credential share the running operation's result, including failures. */ + readonly lock: ( + id: ID, + operation: Effect.Effect, + ) => Effect.Effect /** * Creates a credential for an integration and returns the new record. The new credential becomes the * integration's selection unless `activate` is false and the integration already has a credential. @@ -68,6 +74,9 @@ const layer = Layer.effect( Effect.gen(function* () { const db = (yield* Database.Service).db const bus = yield* Bus.Service + const flock = yield* EffectFlock.Service + const scope = yield* Effect.scope + const pending = new Map>() // Rows written before v2.1 may carry the old key credential shape. const decode = Schema.decodeUnknownSync(CredentialLegacy.Value) const stored = (row: typeof CredentialTable.$inferSelect) => { @@ -86,6 +95,23 @@ const layer = Layer.effect( }) return Service.of({ + lock: Effect.fn("Credential.lock")((id, operation) => + Effect.uninterruptibleMask((restore) => + Effect.gen(function* () { + const current = pending.get(id) + if (current) return yield* restore(Fiber.join(current)) + // The global scope owns the operation, not the first Session or Location waiting for it. + // The file lock extends this to other processes sharing the database. + const fiber = yield* operation.pipe( + flock.withLock(`credential-refresh:${id}`), + Effect.ensuring(Effect.sync(() => pending.delete(id))), + Effect.forkIn(scope), + ) + pending.set(id, fiber) + return yield* restore(Fiber.join(fiber)) + }), + ), + ), all: Effect.fn("Credential.all")(() => db .select() @@ -327,4 +353,4 @@ const layer = Layer.effect( }), ) -export const node = makeGlobalNode({ service: Service, layer, deps: [Database.node, Bus.node] }) +export const node = makeGlobalNode({ service: Service, layer, deps: [Database.node, Bus.node, EffectFlock.node] }) diff --git a/packages/core/src/integration.ts b/packages/core/src/integration.ts index d8fc7ed8f60..22cca5d5a64 100644 --- a/packages/core/src/integration.ts +++ b/packages/core/src/integration.ts @@ -731,16 +731,29 @@ const layer = Layer.effect( const credential = yield* credentials.get(connection.id) if (!credential) return undefined if (credential.value.type !== "oauth") return credential.value - const implementation = state + const refresh = state .get() .integrations.get(credential.integrationID) - ?.implementations.get(credential.value.methodID) - if (!implementation?.refresh) return credential.value - const now = yield* Clock.currentTimeMillis - if (credential.value.expires > now + Duration.toMillis(Duration.minutes(5))) return credential.value - const value = yield* authorize(implementation.refresh(credential.value)) - yield* credentials.update(credential.id, { value }) - return value + ?.implementations.get(credential.value.methodID)?.refresh + const expiring = (value: Credential.OAuth) => + Clock.currentTimeMillis.pipe( + Effect.map((now) => value.expires <= now + Duration.toMillis(Duration.minutes(5))), + ) + if (!refresh || !(yield* expiring(credential.value))) return credential.value + return yield* authorize( + credentials.lock( + credential.id, + Effect.gen(function* () { + // Another Location may have refreshed while this caller waited. + const current = yield* credentials.get(credential.id) + if (!current || current.value.type !== "oauth") return undefined + if (!(yield* expiring(current.value))) return current.value + const value = yield* refresh(current.value).pipe(Effect.timeout("30 seconds")) + yield* credentials.update(current.id, { value }) + return value + }), + ), + ) }), key: Effect.fn("Integration.connection.key")(function* (input) { const method = state diff --git a/packages/core/test/config/config.test.ts b/packages/core/test/config/config.test.ts index bb61cc628de..f85e01aaf97 100644 --- a/packages/core/test/config/config.test.ts +++ b/packages/core/test/config/config.test.ts @@ -802,6 +802,7 @@ describe("Config", () => { layer: Layer.succeed( Credential.Service, Credential.Service.of({ + lock: () => Effect.die("unused Credential.lock"), all: () => Effect.die("unused Credential.all"), list: () => Effect.succeed([ @@ -1051,6 +1052,7 @@ describe("Config", () => { layer: Layer.succeed( Credential.Service, Credential.Service.of({ + lock: () => Effect.die("unused Credential.lock"), all: () => Effect.die("unused Credential.all"), list: () => Effect.succeed([ diff --git a/packages/core/test/credential-lock.test.ts b/packages/core/test/credential-lock.test.ts new file mode 100644 index 00000000000..a3b2592020f --- /dev/null +++ b/packages/core/test/credential-lock.test.ts @@ -0,0 +1,41 @@ +import { expect } from "bun:test" +import { Deferred, Effect, Fiber } from "effect" +import { Credential } from "@opencode/core/credential" +import { Integration } from "@opencode/schema/integration" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { testEffect } from "./lib/effect" + +const it = testEffect(LayerNode.compile(Credential.node)) +const id = Credential.ID.make("cred_refresh") +const value = Credential.OAuth.make({ + type: "oauth", + methodID: Integration.MethodID.make("oauth"), + access: "new-access", + refresh: "new-refresh", + expires: Number.MAX_SAFE_INTEGER, +}) + +it.effect("shares failures but allows a later refresh", () => + Effect.gen(function* () { + const credentials = yield* Credential.Service + const entered = yield* Deferred.make() + const release = yield* Deferred.make() + const error = new Error("token endpoint unavailable") + let calls = 0 + const operation = Effect.gen(function* () { + calls++ + yield* Deferred.succeed(entered, undefined) + yield* Deferred.await(release) + return yield* Effect.fail(error) + }) + const first = yield* credentials.lock(id, operation).pipe(Effect.flip, Effect.forkScoped) + yield* Deferred.await(entered) + const second = yield* credentials.lock(id, operation).pipe(Effect.flip, Effect.forkScoped) + yield* Effect.yieldNow + yield* Deferred.succeed(release, undefined) + expect(yield* Fiber.join(first)).toBe(error) + expect(yield* Fiber.join(second)).toBe(error) + expect(calls).toBe(1) + expect(yield* credentials.lock(id, Effect.succeed(value))).toEqual(value) + }), +) diff --git a/packages/core/test/fixture/config-nodes.ts b/packages/core/test/fixture/config-nodes.ts index 267b2e8a41e..6e9675605fd 100644 --- a/packages/core/test/fixture/config-nodes.ts +++ b/packages/core/test/fixture/config-nodes.ts @@ -8,6 +8,7 @@ export const emptyCredentialNode = makeGlobalNode({ layer: Layer.succeed( Credential.Service, Credential.Service.of({ + lock: () => Effect.die("unused Credential.lock"), all: () => Effect.succeed([]), list: () => Effect.succeed([]), get: () => Effect.undefined, diff --git a/packages/core/test/integration-refresh.test.ts b/packages/core/test/integration-refresh.test.ts new file mode 100644 index 00000000000..2f5c1509ee2 --- /dev/null +++ b/packages/core/test/integration-refresh.test.ts @@ -0,0 +1,118 @@ +import { expect } from "bun:test" +import { Context, Deferred, Effect, Fiber, Layer } from "effect" +import { TestClock } from "effect/testing" +import { Node } from "@opencode/util/effect/app-node" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { Credential } from "@opencode/core/credential" +import { Integration } from "@opencode/core/integration" +import { testEffect } from "./lib/effect" + +const it = testEffect(Layer.empty) + +const locations = Effect.gen(function* () { + const layer = LayerNode.compile(LayerNode.group([Integration.node, Credential.node]), { + shared: Node.tags.values.global, + }) + const memo = yield* Layer.makeMemoMap + const scope = yield* Effect.scope + const first = yield* Layer.buildWithMemoMap(layer, memo, scope) + const second = yield* Layer.buildWithMemoMap(layer, memo, scope) + const a = Context.get(first, Integration.Service) + const b = Context.get(second, Integration.Service) + const credentials = Context.get(first, Credential.Service) + expect(a).not.toBe(b) + expect(credentials).toBe(Context.get(second, Credential.Service)) + return { a, b, credentials } +}) + +for (const cancel of [false, true]) { + it.effect(`shares refresh across Locations${cancel ? " after the first caller is interrupted" : ""}`, () => + Effect.gen(function* () { + const services = yield* locations + const entered = yield* Deferred.make() + const release = yield* Deferred.make() + const integrationID = Integration.ID.make("openai") + const methodID = Integration.MethodID.make("oauth") + let calls = 0 + const registration = { + integrationID, + method: { id: methodID, type: "oauth" as const, label: "OAuth" }, + authorize: () => Effect.die("unused sign-in"), + refresh: (value: Credential.OAuth) => + Effect.gen(function* () { + calls++ + expect(value.refresh).toBe("old-refresh") + yield* Deferred.succeed(entered, undefined) + yield* Deferred.await(release) + return Credential.OAuth.make({ + ...value, + access: "new-access", + refresh: "new-refresh", + expires: Number.MAX_SAFE_INTEGER, + }) + }), + } + yield* services.a.transform((editor) => editor.method.update(registration)) + yield* services.b.transform((editor) => editor.method.update(registration)) + const stored = yield* services.credentials.create({ + integrationID, + value: Credential.OAuth.make({ + type: "oauth", + methodID, + access: "old-access", + refresh: "old-refresh", + expires: 1, + }), + }) + const connection = { type: "credential" as const, id: stored.id, label: stored.label, method: "oauth" as const } + const first = yield* services.a.connection.resolve(connection).pipe(Effect.forkScoped) + yield* Deferred.await(entered) + if (cancel) yield* Fiber.interrupt(first) + const second = yield* services.b.connection.resolve(connection).pipe(Effect.forkScoped) + yield* Effect.yieldNow + expect(calls).toBe(1) + yield* Deferred.succeed(release, undefined) + const value = yield* Fiber.join(second) + if (!cancel) expect(yield* Fiber.join(first)).toEqual(value) + expect((yield* services.credentials.get(stored.id))?.value).toEqual(value) + expect(yield* services.b.connection.resolve(connection)).toEqual(value) + expect(calls).toBe(1) + }), + ) +} + +it.effect("bounds a stuck integration refresh and allows a later attempt", () => + Effect.gen(function* () { + const services = yield* locations + const entered = yield* Deferred.make() + const integrationID = Integration.ID.make("openai") + const methodID = Integration.MethodID.make("oauth") + const stored = yield* services.credentials.create({ + integrationID, + value: Credential.OAuth.make({ type: "oauth", methodID, access: "old", refresh: "refresh", expires: 1 }), + }) + yield* services.a.transform((editor) => + editor.method.update({ + integrationID, + method: { id: methodID, type: "oauth", label: "OAuth" }, + authorize: () => Effect.die("unused sign-in"), + refresh: () => Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)), + }), + ) + const connection = { type: "credential" as const, id: stored.id, label: stored.label, method: "oauth" as const } + const first = yield* services.a.connection.resolve(connection).pipe(Effect.flip, Effect.forkScoped) + yield* Deferred.await(entered) + yield* TestClock.adjust("30 seconds") + expect(yield* Fiber.join(first)).toBeInstanceOf(Integration.AuthorizationError) + yield* services.a.transform((editor) => + editor.method.update({ + integrationID, + method: { id: methodID, type: "oauth", label: "OAuth" }, + authorize: () => Effect.die("unused sign-in"), + refresh: (value) => + Effect.succeed(Credential.OAuth.make({ ...value, access: "new", expires: Number.MAX_SAFE_INTEGER })), + }), + ) + expect(yield* services.a.connection.resolve(connection)).toMatchObject({ access: "new" }) + }), +) diff --git a/packages/core/test/integration.test.ts b/packages/core/test/integration.test.ts index 95ac02c2320..9a2f7749699 100644 --- a/packages/core/test/integration.test.ts +++ b/packages/core/test/integration.test.ts @@ -18,6 +18,7 @@ const failingCredentialNode = makeGlobalNode({ layer: Layer.succeed( Credential.Service, Credential.Service.of({ + lock: () => Effect.die("unused Credential.lock"), all: () => Effect.succeed([]), list: () => Effect.succeed([]), get: () => Effect.undefined, diff --git a/packages/core/test/mcp-oauth.test.ts b/packages/core/test/mcp-oauth.test.ts index 4deeccb8d77..c40ed2e176f 100644 --- a/packages/core/test/mcp-oauth.test.ts +++ b/packages/core/test/mcp-oauth.test.ts @@ -37,6 +37,7 @@ const memoryCredentials = (initial: Credential.Info[]) => { const rows = new Map(initial.map((row) => [row.id, row])) const unused = () => Effect.die("unused credential method") const service = Credential.Service.of({ + lock: unused, all: unused, create: unused, activate: unused,