diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index f4a3de1f5e9..48d6e054e96 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -183,11 +183,10 @@ export const layer = (options?: Options) => const auth = entry.manifest.auth const credential = (yield* credentials.list(entry.integrationID)).at(-1) const variables = auth && credential?.value.type === "key" ? { [auth.env]: credential.value.key } : undefined - // A failed refresh, including a missing credential, keeps the last resolved config so remote providers - // and allowlists do not disappear; only removing the source clears it. - const configs = yield* ( - variables ? wellknown.resolve(entry, variables) : Effect.fail(new Error("No usable wellknown credential")) - ).pipe( + // Logging out drops the saved remote config, so the source supplies nothing until a fresh fetch succeeds. + if (!variables) return yield* wellknown.clearCached(entry.origin).pipe(Effect.as([])) + // A failed refresh keeps the last resolved config so remote providers and allowlists do not disappear. + const configs = yield* wellknown.resolve(entry, variables).pipe( Effect.catch((error) => Effect.logWarning("failed to load wellknown config", { source: entry.origin, error }).pipe( Effect.andThen(wellknown.cached(entry.origin)), diff --git a/packages/core/src/wellknown.ts b/packages/core/src/wellknown.ts index a01b0bea1d0..80f10aed70a 100644 --- a/packages/core/src/wellknown.ts +++ b/packages/core/src/wellknown.ts @@ -50,8 +50,10 @@ export interface Interface { readonly remove: (origin: string) => Effect.Effect /** Resolves and caches the configuration for a registered source. */ readonly resolve: (entry: Entry, variables: Readonly>) => Effect.Effect - /** Returns the last configuration resolved for an origin, kept until the source is removed. */ + /** Returns the last configuration resolved for an origin, kept until it is cleared or the source is removed. */ readonly cached: (origin: string) => Effect.Effect + /** Drops the last configuration resolved for an origin while keeping the source registered. */ + readonly clearCached: (origin: string) => Effect.Effect } export class Service extends Context.Service()("@opencode/WellKnown") {} @@ -210,6 +212,9 @@ const layer = Layer.effect( const value = yield* kv.get(configKey(origin)) return Schema.is(Configs)(value) ? [...value] : [] }), + clearCached: Effect.fn("WellKnown.clearCached")(function* (origin) { + yield* kv.remove(configKey(origin)) + }), }) }), ) diff --git a/packages/core/test/config/config.test.ts b/packages/core/test/config/config.test.ts index 31e9f231480..0eb688c5415 100644 --- a/packages/core/test/config/config.test.ts +++ b/packages/core/test/config/config.test.ts @@ -574,7 +574,15 @@ describe("Config", () => { const credentialNode = makeGlobalNode({ service: Credential.Service, layer: Layer.mock(Credential.Service)({ - list: () => Effect.succeed([]), + list: (integrationID) => + Effect.succeed([ + new Credential.Info({ + id: Credential.ID.create(), + integrationID, + label: "default", + value: Credential.Key.make({ type: "key", key: "secret" }), + }), + ]), get: (id) => Effect.succeed( new Credential.Info({ @@ -587,13 +595,17 @@ describe("Config", () => { }), deps: [], }) - const entry: WellKnown.Entry = { origin, integrationID: Integration.ID.make(origin), manifest: {} } + const entry: WellKnown.Entry = { + origin, + integrationID: Integration.ID.make(origin), + manifest: { auth: { command: ["login"], env: "TOKEN" } }, + } const wellknownNode = makeGlobalNode({ service: WellKnown.Service, layer: Layer.mock(WellKnown.Service)({ entries: () => Effect.succeed([entry]), snapshot: () => [entry], - cached: () => + resolve: () => Effect.succeed([ { mcp: { servers: { wiki: { type: "remote", url: "https://wiki.acme.com/mcp" } } }, @@ -936,6 +948,7 @@ describe("Config", () => { resolve: (_entry, variables) => available ? Effect.succeed([{ shell: variables.TOKEN }]) : Effect.fail(new Error("expired credential")), cached: () => Effect.succeed([]), + clearCached: () => Effect.void, }), ), deps: [], @@ -971,7 +984,7 @@ describe("Config", () => { ), ) - it.live("keeps the last resolved wellknown config across failures and restarts until its source is removed", () => + it.live("keeps the last resolved wellknown config across failures and restarts while a credential exists", () => withTempDir((tmp) => { let manifestStatus = 200 let configStatus = 200 @@ -1079,7 +1092,14 @@ describe("Config", () => { expect(requests).toBeGreaterThan(before) key = undefined - expect(yield* warm.reload("logged-out", warm.switched)).toEqual(loaded) + expect(yield* warm.reload("logged-out", warm.switched)).toEqual(removed) + + // Logging out drops the saved config, so logging back in while the remote is down supplies nothing. + key = "secret" + expect(yield* warm.reload("logged-in-unavailable", warm.switched)).toEqual(removed) + configStatus = 200 + expect(yield* warm.reload("logged-in", warm.switched)).toEqual(loaded) + configStatus = 503 manifestStatus = 503 const updated = yield* warm.bus @@ -1090,7 +1110,6 @@ describe("Config", () => { expect(updated.pollUnsafe()).toBeUndefined() }).pipe(Effect.scoped, Effect.provide(instance())) - key = "secret" yield* Effect.gen(function* () { const cold = yield* services expect(remote(yield* cold.config.entries())).toEqual(loaded) @@ -1167,6 +1186,7 @@ describe("Config", () => { // Exercise the loader boundary against a malformed implementation response. resolve: () => Effect.succeed([null as unknown as WellKnown.Config]), cached: () => Effect.succeed([]), + clearCached: () => Effect.void, }), ), deps: [], diff --git a/packages/core/test/fixture/config-nodes.ts b/packages/core/test/fixture/config-nodes.ts index 6e9675605fd..8e4524a1d20 100644 --- a/packages/core/test/fixture/config-nodes.ts +++ b/packages/core/test/fixture/config-nodes.ts @@ -33,6 +33,7 @@ export const emptyWellknownNode = makeGlobalNode({ remove: () => Effect.die("unused Wellknown.remove"), resolve: () => Effect.die("unused Wellknown.resolve"), cached: () => Effect.succeed([]), + clearCached: () => Effect.void, }), ), deps: [],