fix(core): coordinate credential refreshes across locations and processes (#54023)

This commit is contained in:
Aiden Cline
2026-10-26 01:09:42 -05:00
committed by GitHub
Unverified
parent 74556a25c7
commit 94353780cf
8 changed files with 213 additions and 10 deletions
+28 -2
View File
@@ -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<Info[]>
/** Returns one stored credential by ID. */
readonly get: (id: ID) => Effect.Effect<Info | undefined>
/** Concurrent callers for the same credential share the running operation's result, including failures. */
readonly lock: (
id: ID,
operation: Effect.Effect<OAuth | undefined, unknown>,
) => Effect.Effect<OAuth | undefined, unknown>
/**
* 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<ID, Fiber.Fiber<OAuth | undefined, unknown>>()
// 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] })
+21 -8
View File
@@ -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
+2
View File
@@ -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([
@@ -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<void>()
const release = yield* Deferred.make<void>()
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)
}),
)
@@ -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,
@@ -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<void>()
const release = yield* Deferred.make<void>()
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<void>()
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" })
}),
)
+1
View File
@@ -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,
+1
View File
@@ -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,