diff --git a/packages/core/src/credential.ts b/packages/core/src/credential.ts index b6090aae1fd..ffddc4c6965 100644 --- a/packages/core/src/credential.ts +++ b/packages/core/src/credential.ts @@ -1,7 +1,7 @@ export * as Credential from "./credential.js" import { asc, desc, eq, type SQL } from "drizzle-orm" -import { Cause, Context, Effect, Fiber, Layer, Schema } from "effect" +import { Cause, Clock, Context, Duration, 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" @@ -29,6 +29,10 @@ export type Value = Credential.Value export const Event = Credential.Event +/** Whether an access token is due for a refresh. `expires` 0 means the issuer gave no lifetime. */ +export const expiring = (value: OAuth, now: number) => + value.expires !== 0 && value.expires <= now + Duration.toMillis(Duration.minutes(5)) + export class Info extends Schema.Class("Credential.Info")({ id: ID, integrationID: Integration.ID, @@ -43,10 +47,19 @@ 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: ( + /** + * Refreshes a stored OAuth credential and saves the result. Every process and Location sharing the database + * refreshes a credential one at a time, and concurrent callers share the running refresh, including its + * failure. The row is read again under the lock: if `stale` is false, another caller already refreshed it and + * that row is returned. If the row changes while `refresh` runs, the stored row wins. Resolves undefined when + * the credential is gone. + */ + readonly refresh: ( id: ID, - operation: Effect.Effect, + input: { + readonly stale: (current: OAuth, now: number) => boolean + readonly refresh: (current: OAuth) => Effect.Effect + }, ) => Effect.Effect /** * Creates a credential for an integration and returns the new record. The new credential becomes the @@ -126,21 +139,46 @@ const layer = Layer.effect( Effect.orDie, ) - return Service.of({ - lock: Effect.fn("Credential.lock")((id, operation) => - Effect.uninterruptibleMask((restore) => + const lock = (id: ID, operation: Effect.Effect) => + 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)) + }), + ) + + const service: Interface = Service.of({ + refresh: Effect.fn("Credential.refresh")((id, input) => + lock( + id, 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)) + const current = yield* service.get(id) + if (current?.value.type !== "oauth") return undefined + if (!input.stale(current.value, yield* Clock.currentTimeMillis)) { + yield* Effect.logInfo("credential refresh skipped", { credentialID: id, reason: "already_refreshed" }) + return current.value + } + const value = yield* input.refresh(current.value).pipe(Effect.timeout("30 seconds")) + const latest = yield* service.get(id) + if (latest?.value.type !== "oauth") return undefined + if (latest.value.access !== current.value.access || latest.value.refresh !== current.value.refresh) { + yield* Effect.logWarning("credential refresh discarded", { + credentialID: id, + reason: "changed_meanwhile", + }) + return latest.value + } + yield* service.update(id, { value }) + return value }), ), ), @@ -305,6 +343,7 @@ const layer = Layer.effect( ) }), }) + return service }), ) diff --git a/packages/core/src/integration.ts b/packages/core/src/integration.ts index fe4b8fd824b..842f59e2752 100644 --- a/packages/core/src/integration.ts +++ b/packages/core/src/integration.ts @@ -708,25 +708,9 @@ const layer = Layer.effect( .get() .integrations.get(credential.integrationID) ?.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 - }), - ), - ) + if (!refresh || !Credential.expiring(credential.value, yield* Clock.currentTimeMillis)) + return credential.value + return yield* authorize(credentials.refresh(credential.id, { stale: Credential.expiring, refresh })) }), key: Effect.fn("Integration.connection.key")(function* (input) { const method = state diff --git a/packages/core/src/mcp/oauth.ts b/packages/core/src/mcp/oauth.ts index b8eb208e7ec..815fbfbcb9d 100644 --- a/packages/core/src/mcp/oauth.ts +++ b/packages/core/src/mcp/oauth.ts @@ -337,52 +337,43 @@ export const connectProvider = Effect.fnUntraced(function* (input: { } // Refresh tokens rotate and the row is shared across connections: only drop it while it still holds ours. let presented = found.value.refresh - // Every process and Location sharing the database refreshes this row under one lock, and the lock is held - // until the rotated tokens are saved. A caller holding a refresh token the row no longer has gets the - // stored tokens instead of spending it: servers that rotate refresh tokens revoke the grant on reuse. + // Every process and Location sharing the database refreshes this row one at a time. A caller holding a + // refresh token the row no longer has gets the stored tokens instead of spending it: servers that rotate + // refresh tokens revoke the grant on reuse. const refresh: Refresh = (send, url, init, token) => run( credentials - .lock( - id, - Effect.gen(function* () { - const current = yield* credentials.get(id) - if (current?.value.type !== "oauth") return undefined - if (current.value.refresh !== token) { - yield* Effect.logInfo("mcp oauth refresh skipped", { credentialID: id, reason: "already_rotated" }) - return current.value - } - // Bounds reading the body too, so a server that stalls after its headers cannot hold the lock. - const { response, body } = yield* Effect.tryPromise({ - try: async (signal) => { - const response = await send(url, { ...init, signal }) - return { response, body: await response.text() } - }, - catch: (error) => error, - }).pipe(Effect.timeout("30 seconds")) - const parsed = OAuthTokensSchema.safeParse( - response.ok ? Option.getOrUndefined(decodeJson(body)) : undefined, - ) - if (!parsed.success) - return yield* new RefreshRejected({ status: response.status, headers: response.headers, body }) - const latest = yield* credentials.get(id) - if (latest?.value.type !== "oauth") return undefined - if (latest.value.refresh !== token) { - yield* Effect.logWarning("mcp oauth refresh discarded", { credentialID: id, reason: "rotated_meanwhile" }) - return latest.value - } - // The SDK ignores an issuer in the token response; keep the one this credential was stamped with. - const issuer = latest.value.metadata?.issuer - const value = toCredential({ - methodID, - serverUrl: input.config.url, - tokens: { refresh_token: token, ...parsed.data, issuer: typeof issuer === "string" ? issuer : undefined }, - client: clientFromCredential(latest.value), - }) - yield* credentials.update(id, { value }) - return value - }), - ) + .refresh(id, { + stale: (current) => current.refresh === token, + refresh: (current) => + Effect.gen(function* () { + // The body is read here so the refresh time limit also covers a server that stalls after its headers. + const { response, body } = yield* Effect.tryPromise({ + try: async (signal) => { + const response = await send(url, { ...init, signal }) + return { response, body: await response.text() } + }, + catch: (error) => error, + }) + const parsed = OAuthTokensSchema.safeParse( + response.ok ? Option.getOrUndefined(decodeJson(body)) : undefined, + ) + if (!parsed.success) + return yield* new RefreshRejected({ status: response.status, headers: response.headers, body }) + // The SDK ignores an issuer in the token response; keep the one this credential was stamped with. + const issuer = current.metadata?.issuer + return toCredential({ + methodID, + serverUrl: input.config.url, + tokens: { + refresh_token: token, + ...parsed.data, + issuer: typeof issuer === "string" ? issuer : undefined, + }, + client: clientFromCredential(current), + }) + }), + }) .pipe( Effect.map((value) => value diff --git a/packages/core/test/config/config.test.ts b/packages/core/test/config/config.test.ts index 0eb688c5415..b85604282bb 100644 --- a/packages/core/test/config/config.test.ts +++ b/packages/core/test/config/config.test.ts @@ -910,7 +910,7 @@ describe("Config", () => { layer: Layer.succeed( Credential.Service, Credential.Service.of({ - lock: () => Effect.die("unused Credential.lock"), + refresh: () => Effect.die("unused Credential.refresh"), all: () => Effect.die("unused Credential.all"), list: () => Effect.succeed([ @@ -1153,7 +1153,7 @@ describe("Config", () => { layer: Layer.succeed( Credential.Service, Credential.Service.of({ - lock: () => Effect.die("unused Credential.lock"), + refresh: () => Effect.die("unused Credential.refresh"), 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 deleted file mode 100644 index a3b2592020f..00000000000 --- a/packages/core/test/credential-lock.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -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/credential-refresh.test.ts b/packages/core/test/credential-refresh.test.ts new file mode 100644 index 00000000000..66154877f2e --- /dev/null +++ b/packages/core/test/credential-refresh.test.ts @@ -0,0 +1,113 @@ +import { describe, expect, test } 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 oauth = (access: string, refresh: string, expires: number) => + Credential.OAuth.make({ type: "oauth", methodID: Integration.MethodID.make("oauth"), access, refresh, expires }) +const create = Effect.gen(function* () { + const credentials = yield* Credential.Service + const created = yield* credentials.create({ + integrationID: Integration.ID.make("test"), + value: oauth("old-access", "old-refresh", 1), + }) + return created.id +}) +const always = () => true + +describe("Credential.refresh", () => { + it.effect("saves the refreshed value", () => + Effect.gen(function* () { + const credentials = yield* Credential.Service + const id = yield* create + const value = oauth("new-access", "new-refresh", Number.MAX_SAFE_INTEGER) + expect(yield* credentials.refresh(id, { stale: always, refresh: () => Effect.succeed(value) })).toEqual(value) + expect((yield* credentials.get(id))?.value).toEqual(value) + }), + ) + + it.effect("returns the stored row when another caller already refreshed it", () => + Effect.gen(function* () { + const credentials = yield* Credential.Service + const id = yield* create + let calls = 0 + const result = yield* credentials.refresh(id, { + stale: () => false, + refresh: () => Effect.sync(() => calls++).pipe(Effect.as(oauth("new-access", "new-refresh", 2))), + }) + expect(result?.access).toBe("old-access") + expect(calls).toBe(0) + }), + ) + + it.effect("keeps a row that changed while the refresh ran", () => + Effect.gen(function* () { + const credentials = yield* Credential.Service + const id = yield* create + const replaced = oauth("other-access", "other-refresh", 3) + const result = yield* credentials.refresh(id, { + stale: always, + refresh: () => + credentials.update(id, { value: replaced }).pipe(Effect.as(oauth("new-access", "new-refresh", 2))), + }) + expect(result).toEqual(replaced) + expect((yield* credentials.get(id))?.value).toEqual(replaced) + }), + ) + + it.effect("resolves undefined for a missing credential", () => + Effect.gen(function* () { + const credentials = yield* Credential.Service + const result = yield* credentials.refresh(Credential.ID.make("cred_missing"), { + stale: always, + refresh: () => Effect.die("unreachable"), + }) + expect(result).toBeUndefined() + }), + ) + + it.effect("shares failures but allows a later refresh", () => + Effect.gen(function* () { + const credentials = yield* Credential.Service + const id = yield* create + const entered = yield* Deferred.make() + const release = yield* Deferred.make() + const error = new Error("token endpoint unavailable") + let calls = 0 + const failing = { + stale: always, + refresh: () => + Effect.gen(function* () { + calls++ + yield* Deferred.succeed(entered, undefined) + yield* Deferred.await(release) + return yield* Effect.fail(error) + }), + } + const first = yield* credentials.refresh(id, failing).pipe(Effect.flip, Effect.forkScoped) + yield* Deferred.await(entered) + const second = yield* credentials.refresh(id, failing).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) + const value = oauth("new-access", "new-refresh", 2) + expect(yield* credentials.refresh(id, { stale: always, refresh: () => Effect.succeed(value) })).toEqual(value) + }), + ) +}) + +describe("Credential.expiring", () => { + const now = 1_000_000_000 + test("is due within five minutes of expiry", () => { + expect(Credential.expiring(oauth("a", "r", now + 4 * 60_000), now)).toBe(true) + expect(Credential.expiring(oauth("a", "r", now + 6 * 60_000), now)).toBe(false) + }) + test("never expires a token without a lifetime", () => { + expect(Credential.expiring(oauth("a", "r", 0), now)).toBe(false) + }) +}) diff --git a/packages/core/test/fixture/config-nodes.ts b/packages/core/test/fixture/config-nodes.ts index 8e4524a1d20..0b9b7267d0a 100644 --- a/packages/core/test/fixture/config-nodes.ts +++ b/packages/core/test/fixture/config-nodes.ts @@ -8,7 +8,7 @@ export const emptyCredentialNode = makeGlobalNode({ layer: Layer.succeed( Credential.Service, Credential.Service.of({ - lock: () => Effect.die("unused Credential.lock"), + refresh: () => Effect.die("unused Credential.refresh"), all: () => Effect.succeed([]), list: () => Effect.succeed([]), get: () => Effect.undefined, diff --git a/packages/core/test/fixture/policy.ts b/packages/core/test/fixture/policy.ts index f57bfb9a1e8..09c6387a756 100644 --- a/packages/core/test/fixture/policy.ts +++ b/packages/core/test/fixture/policy.ts @@ -75,7 +75,7 @@ export const registerIntegrationPolicy = Effect.fn(function* (input: { all: () => Effect.succeed([]), list: () => Effect.succeed([]), get: () => Effect.succeed(undefined), - lock: () => Effect.die("unused Credential.lock"), + refresh: () => Effect.die("unused Credential.refresh"), create: () => Effect.die("unused Credential.create"), activate: () => Effect.die("unused Credential.activate"), update: () => Effect.die("unused Credential.update"), diff --git a/packages/core/test/integration.test.ts b/packages/core/test/integration.test.ts index c6d06e41ea0..ebd303a71a7 100644 --- a/packages/core/test/integration.test.ts +++ b/packages/core/test/integration.test.ts @@ -20,7 +20,7 @@ const failingCredentialNode = makeGlobalNode({ layer: Layer.succeed( Credential.Service, Credential.Service.of({ - lock: () => Effect.die("unused Credential.lock"), + refresh: () => Effect.die("unused Credential.refresh"), 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 c23839dd215..6ed86ddddad 100644 --- a/packages/core/test/mcp-oauth.test.ts +++ b/packages/core/test/mcp-oauth.test.ts @@ -42,7 +42,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, + refresh: unused, all: unused, create: unused, activate: unused, diff --git a/packages/core/test/plugin/provider-opencode.test.ts b/packages/core/test/plugin/provider-opencode.test.ts index 650f7756b2d..f504a171351 100644 --- a/packages/core/test/plugin/provider-opencode.test.ts +++ b/packages/core/test/plugin/provider-opencode.test.ts @@ -327,7 +327,7 @@ describe("OpencodePlugin", () => { methodID: Integration.MethodID.make("device"), access: "expired-access", refresh: "old-refresh", - expires: 0, + expires: 1, metadata: { server: server.url.origin, orgID: "org-a", orgName: "Alpha", custom: "preserved" }, }), }) @@ -1094,7 +1094,7 @@ describe("OpencodePlugin", () => { methodID: Integration.MethodID.make("device"), access: "expired", refresh: "revoked", - expires: 0, + expires: 1, metadata: { server: server.url.origin, orgID: "org_acme", orgName: "Acme" }, }), })