From ba6a72cf1592bd809ec2cf2d9adb27f0ca879d91 Mon Sep 17 00:00:00 2001 From: Sarah Fortune Date: Wed, 22 Oct 2025 08:31:15 -0700 Subject: [PATCH] Update the remote config when the user logs in (#7025) * Update the remote config when the user logs in Subscribe to changes in the auth state, and fetch the remote config when the user logs in. Move the error handling into `fetchRemoteConfig` to remove duplication. * Update src/core/storage/remote-config/fetch.ts Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> --------- Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> --- .../controller/account/setUserOrganization.ts | 11 ++------ src/core/controller/index.ts | 26 +++++++++---------- src/core/storage/remote-config/fetch.ts | 11 +++++--- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/core/controller/account/setUserOrganization.ts b/src/core/controller/account/setUserOrganization.ts index 31074b2a..2c419b8d 100644 --- a/src/core/controller/account/setUserOrganization.ts +++ b/src/core/controller/account/setUserOrganization.ts @@ -14,17 +14,10 @@ export async function setUserOrganization(controller: Controller, request: UserO if (!controller.accountService) { throw new Error("Account service not available") } - // Switch to the specified organization using the account service await controller.accountService.switchAccount(request.organizationId) - - try { - await fetchRemoteConfig(controller) - } catch (error) { - console.error("Failed to fetch remote config after org switch:", error) - } - - return Empty.create({}) + await fetchRemoteConfig(controller) + return {} } catch (error) { throw error } diff --git a/src/core/controller/index.ts b/src/core/controller/index.ts index 3e0de1ef..7a8a7ca9 100644 --- a/src/core/controller/index.ts +++ b/src/core/controller/index.ts @@ -34,6 +34,7 @@ import { featureFlagsService } from "@/services/feature-flags" import { getDistinctId } from "@/services/logging/distinctId" import { telemetryService } from "@/services/telemetry" import { ShowMessageType } from "@/shared/proto/host/window" +import { AuthState } from "@/shared/proto/index.cline" import { getLatestAnnouncementId } from "@/utils/announcements" import { getCwd, getDesktopDir } from "@/utils/path" import { PromptRegistry } from "../prompts/system-prompt" @@ -47,6 +48,7 @@ import { import { fetchRemoteConfig } from "../storage/remote-config/fetch" import { PersistenceErrorEvent, StateManager } from "../storage/StateManager" import { Task } from "../task" +import { StreamingResponseHandler } from "./grpc-handler" import { sendMcpMarketplaceCatalogEvent } from "./mcp/subscribeToMcpMarketplaceCatalog" import { appendClineStealthModels } from "./models/refreshOpenRouterModels" import { checkCliInstallation } from "./state/checkCliInstallation" @@ -108,16 +110,9 @@ export class Controller { */ private startRemoteConfigTimer() { // Initial fetch - fetchRemoteConfig(this).catch((error) => { - console.error("Failed to fetch remote config:", error) - }) - + fetchRemoteConfig(this) // Set up 30-second interval - this.remoteConfigTimer = setInterval(() => { - fetchRemoteConfig(this).catch((error) => { - console.error("Failed to fetch remote config:", error) - }) - }, 30000) // 30 seconds + this.remoteConfigTimer = setInterval(() => fetchRemoteConfig(this), 30000) // 30 seconds } constructor(readonly context: vscode.ExtensionContext) { @@ -150,6 +145,13 @@ export class Controller { this.ocaAuthService = OcaAuthService.initialize(this) this.accountService = ClineAccountService.getInstance() + const authStatusHandler: StreamingResponseHandler = async (response, _isLast, _seqNumber): Promise => { + if (response.user) { + fetchRemoteConfig(this) + } + } + this.authService.subscribeToAuthStatusUpdate(this, {}, authStatusHandler, undefined) + this.authService.restoreRefreshTokenAndRetrieveAuthInfo().then(() => { this.startRemoteConfigTimer() }) @@ -244,11 +246,7 @@ export class Controller { historyItem?: HistoryItem, taskSettings?: Partial, ) { - try { - await fetchRemoteConfig(this) - } catch (error) { - console.error("Failed to fetch remote config on task init:", error) - } + await fetchRemoteConfig(this) await this.clearTask() // ensures that an existing task doesn't exist before starting a new one, although this shouldn't be possible since user must clear task before starting a new one diff --git a/src/core/storage/remote-config/fetch.ts b/src/core/storage/remote-config/fetch.ts index 582f1a11..effa3887 100644 --- a/src/core/storage/remote-config/fetch.ts +++ b/src/core/storage/remote-config/fetch.ts @@ -177,12 +177,17 @@ async function ensureUserInOrgWithRemoteConfig(controller: Controller): Promise< * Scans all user organizations, switches to the one with remote config if found, * and applies the configuration. * + * It catches any exceptions, logs them and does not propagate them to the caller. + * * This function is called periodically to ensure users stay in * organizations with remote configuration enabled. * * @param controller The controller instance - * @returns Promise resolving to the RemoteConfig object, or undefined if no organization has remote config */ -export async function fetchRemoteConfig(controller: Controller): Promise { - return ensureUserInOrgWithRemoteConfig(controller) +export async function fetchRemoteConfig(controller: Controller) { + try { + await ensureUserInOrgWithRemoteConfig(controller) + } catch (error) { + console.error("Failed to fetch remote config", error) + } }