From 712b7bcfd57305e7516b793d64b94b0a5179d699 Mon Sep 17 00:00:00 2001 From: Jazzcort Date: Thu, 28 Aug 2025 15:34:26 -0400 Subject: [PATCH] Fix: Refactor dependency injection for settings and clients This commit refactors how IdeSettings and ControlPlaneClient are accessed throughout the configuration system to improve state management and reduce coupling. Previously, ideSettingsPromise was passed down from the top-level ConfigHandler through various profile loaders and clients. This created a rigid dependency chain and required manual promise replacement when settings were updated. Similarly, downstream components could hold onto stale instances of ControlPlaneClient after a change in authentication state. The key changes are: - Removed ideSettingsPromise prop drilling: Components that require IDE settings now fetch them directly from the IDE object (ide.getIdeSettings()) when needed. - Updated globalLocalProfileManager during every cascadeInit: This ensures it always holds the most recent ControlPlaneClient, preventing issues with stale client objects after re-authentication. - Simplified constructors: The constructors for ConfigHandler, ControlPlaneClient, and various loaders have been simplified to reflect these changes, primarily by passing the IDE object directly instead of its derived promises. --- core/config/ConfigHandler.ts | 32 ++++++++----------- core/config/profile/LocalProfileLoader.ts | 4 +-- core/config/profile/PlatformProfileLoader.ts | 12 ++----- core/config/profile/doLoadConfig.ts | 7 ++-- .../providers/_context-providers.vitest.ts | 2 -- core/control-plane/client.ts | 9 +++--- core/core.ts | 5 ++- core/test/fixtures.ts | 4 +-- 8 files changed, 25 insertions(+), 50 deletions(-) diff --git a/core/config/ConfigHandler.ts b/core/config/ConfigHandler.ts index 837bb2a39..ffce8a53d 100644 --- a/core/config/ConfigHandler.ts +++ b/core/config/ConfigHandler.ts @@ -59,27 +59,17 @@ export class ConfigHandler { constructor( private readonly ide: IDE, - private ideSettingsPromise: Promise, private llmLogger: ILLMLogger, - sessionInfoPromise: Promise, + initialSessionInfoPromise: Promise, ) { - this.ide = ide; - this.ideSettingsPromise = ideSettingsPromise; - this.controlPlaneClient = new ControlPlaneClient( - sessionInfoPromise, - ideSettingsPromise, - this.ide.getIdeInfo(), + initialSessionInfoPromise, + this.ide, ); // This profile manager will always be available this.globalLocalProfileManager = new ProfileLifecycleManager( - new LocalProfileLoader( - ide, - ideSettingsPromise, - this.controlPlaneClient, - this.llmLogger, - ), + new LocalProfileLoader(ide, this.controlPlaneClient, this.llmLogger), this.ide, ); @@ -97,6 +87,7 @@ export class ConfigHandler { } private workspaceDirs: string[] | null = null; + async getWorkspaceId() { if (!this.workspaceDirs) { this.workspaceDirs = await this.ide.getWorkspaceDirs(); @@ -113,6 +104,13 @@ export class ConfigHandler { const signal = this.cascadeAbortController.signal; this.workspaceDirs = null; // forces workspace dirs reload + // Always update globalLocalProfileManager before recreating all the loaders + // during every cascadeInit so it holds the most recent controlPlaneClient. + this.globalLocalProfileManager = new ProfileLifecycleManager( + new LocalProfileLoader(this.ide, this.controlPlaneClient, this.llmLogger), + this.ide, + ); + try { const { orgs, errors } = await this.getOrgs(); @@ -246,7 +244,6 @@ export class ConfigHandler { versionSlug: assistant.configResult.config?.version ?? "latest", controlPlaneClient: this.controlPlaneClient, ide: this.ide, - ideSettingsPromise: this.ideSettingsPromise, llmLogger: this.llmLogger, rawYaml: assistant.rawYaml, orgScopeId: orgScopeId, @@ -367,7 +364,6 @@ export class ConfigHandler { const profiles = [...assistantFiles, ...agentFiles].map((assistant) => { return new LocalProfileLoader( this.ide, - this.ideSettingsPromise, this.controlPlaneClient, this.llmLogger, assistant, @@ -392,7 +388,6 @@ export class ConfigHandler { // Ide settings change: refresh session and cascade refresh from the top async updateIdeSettings(ideSettings: IdeSettings) { - this.ideSettingsPromise = Promise.resolve(ideSettings); this.abortCascade(); await this.cascadeInit("IDE settings update"); } @@ -430,8 +425,7 @@ export class ConfigHandler { if (reload) { this.controlPlaneClient = new ControlPlaneClient( Promise.resolve(sessionInfo), - this.ideSettingsPromise, - this.ide.getIdeInfo(), + this.ide, ); this.abortCascade(); await this.cascadeInit("Control plane session info update"); diff --git a/core/config/profile/LocalProfileLoader.ts b/core/config/profile/LocalProfileLoader.ts index e7a9608f1..9c5345fbd 100644 --- a/core/config/profile/LocalProfileLoader.ts +++ b/core/config/profile/LocalProfileLoader.ts @@ -1,7 +1,7 @@ import { ConfigResult, parseConfigYaml } from "@continuedev/config-yaml"; import { ControlPlaneClient } from "../../control-plane/client.js"; -import { ContinueConfig, IDE, IdeSettings, ILLMLogger } from "../../index.js"; +import { ContinueConfig, IDE, ILLMLogger } from "../../index.js"; import { ProfileDescription } from "../ProfileLifecycleManager.js"; import { getPrimaryConfigFilePath } from "../../util/paths.js"; @@ -15,7 +15,6 @@ export default class LocalProfileLoader implements IProfileLoader { constructor( private ide: IDE, - private ideSettingsPromise: Promise, private controlPlaneClient: ControlPlaneClient, private llmLogger: ILLMLogger, private overrideAssistantFile?: @@ -57,7 +56,6 @@ export default class LocalProfileLoader implements IProfileLoader { async doLoadConfig(): Promise> { const result = await doLoadConfig({ ide: this.ide, - ideSettingsPromise: this.ideSettingsPromise, controlPlaneClient: this.controlPlaneClient, llmLogger: this.llmLogger, profileId: this.description.id, diff --git a/core/config/profile/PlatformProfileLoader.ts b/core/config/profile/PlatformProfileLoader.ts index 01de0066d..0540d924a 100644 --- a/core/config/profile/PlatformProfileLoader.ts +++ b/core/config/profile/PlatformProfileLoader.ts @@ -2,7 +2,7 @@ import { AssistantUnrolled, ConfigResult } from "@continuedev/config-yaml"; import { ControlPlaneClient } from "../../control-plane/client.js"; import { getControlPlaneEnv } from "../../control-plane/env.js"; -import { ContinueConfig, IDE, IdeSettings, ILLMLogger } from "../../index.js"; +import { ContinueConfig, IDE, ILLMLogger } from "../../index.js"; import { ProfileDescription } from "../ProfileLifecycleManager.js"; import doLoadConfig from "./doLoadConfig.js"; @@ -28,7 +28,6 @@ export default class PlatformProfileLoader implements IProfileLoader { private readonly versionSlug: string; private readonly controlPlaneClient: ControlPlaneClient; private readonly ide: IDE; - private ideSettingsPromise: Promise; private llmLogger: ILLMLogger; readonly description: ProfileDescription; private readonly orgScopeId: string | null; @@ -41,7 +40,6 @@ export default class PlatformProfileLoader implements IProfileLoader { versionSlug, controlPlaneClient, ide, - ideSettingsPromise, llmLogger, description, orgScopeId, @@ -53,7 +51,6 @@ export default class PlatformProfileLoader implements IProfileLoader { versionSlug: string; controlPlaneClient: ControlPlaneClient; ide: IDE; - ideSettingsPromise: Promise; llmLogger: ILLMLogger; description: ProfileDescription; orgScopeId: string | null; @@ -65,7 +62,6 @@ export default class PlatformProfileLoader implements IProfileLoader { this.versionSlug = versionSlug; this.controlPlaneClient = controlPlaneClient; this.ide = ide; - this.ideSettingsPromise = ideSettingsPromise; this.llmLogger = llmLogger; this.description = description; this.orgScopeId = orgScopeId; @@ -79,7 +75,6 @@ export default class PlatformProfileLoader implements IProfileLoader { versionSlug, controlPlaneClient, ide, - ideSettingsPromise, llmLogger, rawYaml, orgScopeId, @@ -91,12 +86,11 @@ export default class PlatformProfileLoader implements IProfileLoader { versionSlug: string; controlPlaneClient: ControlPlaneClient; ide: IDE; - ideSettingsPromise: Promise; llmLogger: ILLMLogger; rawYaml: string; orgScopeId: string | null; }): Promise { - const controlPlaneEnv = await getControlPlaneEnv(ideSettingsPromise); + const controlPlaneEnv = await getControlPlaneEnv(ide.getIdeSettings()); const description: ProfileDescription = { id: `${ownerSlug}/${packageSlug}`, @@ -121,7 +115,6 @@ export default class PlatformProfileLoader implements IProfileLoader { versionSlug, controlPlaneClient, ide, - ideSettingsPromise, llmLogger, description, orgScopeId, @@ -139,7 +132,6 @@ export default class PlatformProfileLoader implements IProfileLoader { const results = await doLoadConfig({ ide: this.ide, - ideSettingsPromise: this.ideSettingsPromise, controlPlaneClient: this.controlPlaneClient, llmLogger: this.llmLogger, overrideConfigYaml: this.configResult.config, diff --git a/core/config/profile/doLoadConfig.ts b/core/config/profile/doLoadConfig.ts index 4b0f64500..a188b0e02 100644 --- a/core/config/profile/doLoadConfig.ts +++ b/core/config/profile/doLoadConfig.ts @@ -11,7 +11,6 @@ import { import { ContinueConfig, IDE, - IdeSettings, ILLMLogger, RuleWithSource, SerializedContinueConfig, @@ -70,7 +69,6 @@ async function loadRules(ide: IDE) { } export default async function doLoadConfig(options: { ide: IDE; - ideSettingsPromise: Promise; controlPlaneClient: ControlPlaneClient; llmLogger: ILLMLogger; overrideConfigJson?: SerializedContinueConfig; @@ -82,7 +80,6 @@ export default async function doLoadConfig(options: { }): Promise> { const { ide, - ideSettingsPromise, controlPlaneClient, llmLogger, overrideConfigJson, @@ -95,7 +92,7 @@ export default async function doLoadConfig(options: { const ideInfo = await ide.getIdeInfo(); const uniqueId = await ide.getUniqueId(); - const ideSettings = await ideSettingsPromise; + const ideSettings = await ide.getIdeSettings(); const workOsAccessToken = await controlPlaneClient.getAccessToken(); const isSignedIn = await controlPlaneClient.isSignedIn(); @@ -370,7 +367,7 @@ export default async function doLoadConfig(options: { const useOnPremProxy = controlPlane?.useContinueForTeamsProxy === false && controlPlane?.proxyUrl; - const env = await getControlPlaneEnv(ideSettingsPromise); + const env = await getControlPlaneEnv(Promise.resolve(ideSettings)); let controlPlaneProxyUrl: string = useOnPremProxy ? controlPlane?.proxyUrl : env.DEFAULT_CONTROL_PLANE_PROXY_URL; diff --git a/core/context/providers/_context-providers.vitest.ts b/core/context/providers/_context-providers.vitest.ts index bde33cec1..40020e700 100644 --- a/core/context/providers/_context-providers.vitest.ts +++ b/core/context/providers/_context-providers.vitest.ts @@ -28,11 +28,9 @@ async function getContextProviderExtras( ): Promise { setUpTestDir(); const ide = new FileSystemIde(TEST_DIR); - const ideSettingsPromise = ide.getIdeSettings(); const llmLogger = new LLMLogger(); const configHandler = new ConfigHandler( ide, - ideSettingsPromise, llmLogger, Promise.resolve(undefined), ); diff --git a/core/control-plane/client.ts b/core/control-plane/client.ts index 1f049e1fd..a8607386f 100644 --- a/core/control-plane/client.ts +++ b/core/control-plane/client.ts @@ -11,7 +11,7 @@ import { import fetch, { RequestInit, Response } from "node-fetch"; import { OrganizationDescription } from "../config/ProfileLifecycleManager.js"; -import { IdeInfo, IdeSettings, ModelDescription } from "../index.js"; +import { IDE, ModelDescription } from "../index.js"; import { Logger } from "../util/Logger.js"; import { ControlPlaneSessionInfo, isOnPremSession } from "./AuthTypes.js"; @@ -44,8 +44,7 @@ export const TRIAL_PROXY_URL = export class ControlPlaneClient { constructor( readonly sessionInfoPromise: Promise, - private readonly ideSettingsPromise: Promise, - private readonly ideInfoPromise: Promise, + private readonly ide: IDE, ) {} async resolveFQSNs( @@ -90,9 +89,9 @@ export class ControlPlaneClient { throw new Error("No access token"); } - const env = await getControlPlaneEnv(this.ideSettingsPromise); + const env = await getControlPlaneEnv(this.ide.getIdeSettings()); const url = new URL(path, env.CONTROL_PLANE_URL).toString(); - const ideInfo = await this.ideInfoPromise; + const ideInfo = await this.ide.getIdeInfo(); const resp = await fetch(url, { ...init, diff --git a/core/core.ts b/core/core.ts index 760466eb2..b49f36378 100644 --- a/core/core.ts +++ b/core/core.ts @@ -138,7 +138,7 @@ export class Core { const ideInfoPromise = messenger.request("getIdeInfo", undefined); const ideSettingsPromise = messenger.request("getIdeSettings", undefined); - const sessionInfoPromise = messenger.request( + const initialSessionInfoPromise = messenger.request( "getControlPlaneSessionInfo", { silent: true, @@ -148,9 +148,8 @@ export class Core { this.configHandler = new ConfigHandler( this.ide, - ideSettingsPromise, this.llmLogger, - sessionInfoPromise, + initialSessionInfoPromise, ); this.docsService = DocsService.createSingleton( diff --git a/core/test/fixtures.ts b/core/test/fixtures.ts index 0b98bb256..51c90d9ef 100644 --- a/core/test/fixtures.ts +++ b/core/test/fixtures.ts @@ -12,13 +12,11 @@ export const ideSettingsPromise = testIde.getIdeSettings(); export const testControlPlaneClient = new ControlPlaneClient( Promise.resolve(undefined), - ideSettingsPromise, - testIde.getIdeInfo(), + testIde, ); export const testConfigHandler = new ConfigHandler( testIde, - ideSettingsPromise, new LLMLogger(), Promise.resolve(undefined), );