From 139610950c6f9bf4bab32e7779fa7c4d5d331253 Mon Sep 17 00:00:00 2001 From: gnanam1990 Date: Tue, 7 Apr 2026 14:50:20 +0530 Subject: [PATCH] fix: preserve explicit provider startup intent --- src/utils/providerEnvSelection.test.ts | 29 ++++++++++++++++++++++++++ src/utils/providerEnvSelection.ts | 14 +------------ src/utils/providerProfile.test.ts | 20 ++++++++++++++++++ src/utils/providerProfile.ts | 4 ++++ 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/utils/providerEnvSelection.test.ts b/src/utils/providerEnvSelection.test.ts index 46ee7670..23849269 100644 --- a/src/utils/providerEnvSelection.test.ts +++ b/src/utils/providerEnvSelection.test.ts @@ -27,6 +27,22 @@ afterEach(() => { }) describe('filterSettingsEnvForExplicitProvider', () => { + test('does not treat plain provider flags as an explicit CLI override', () => { + process.env.CLAUDE_CODE_USE_GITHUB = '1' + + expect( + filterSettingsEnvForExplicitProvider({ + CLAUDE_CODE_USE_OPENAI: '1', + OPENAI_MODEL: 'gpt-4o', + OTHER: 'keep-me', + }), + ).toEqual({ + CLAUDE_CODE_USE_OPENAI: '1', + OPENAI_MODEL: 'gpt-4o', + OTHER: 'keep-me', + }) + }) + test('strips settings-sourced provider flags when CLI provider is explicit', () => { process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'openai' @@ -71,4 +87,17 @@ describe('filterSettingsEnvForExplicitProvider', () => { }), ).toEqual({ OTHER: 'keep-me' }) }) + + test('preserves anthropic startup intent by stripping stale GitHub/OpenAI settings', () => { + process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'anthropic' + + expect( + filterSettingsEnvForExplicitProvider({ + CLAUDE_CODE_USE_GITHUB: '1', + CLAUDE_CODE_USE_OPENAI: '1', + OPENAI_MODEL: 'github:copilot', + OTHER: 'keep-me', + }), + ).toEqual({ OTHER: 'keep-me' }) + }) }) diff --git a/src/utils/providerEnvSelection.ts b/src/utils/providerEnvSelection.ts index 6bcd79a5..c11cb9d6 100644 --- a/src/utils/providerEnvSelection.ts +++ b/src/utils/providerEnvSelection.ts @@ -1,5 +1,3 @@ -import { isEnvTruthy } from './envUtils.js' - export const EXPLICIT_PROVIDER_ENV_VAR = 'CLAUDE_CODE_EXPLICIT_PROVIDER' const PROVIDER_FLAG_KEYS = [ @@ -20,17 +18,7 @@ export function clearProviderSelectionFlags( } function getExplicitProvider(processEnv: NodeJS.ProcessEnv): string | undefined { - const explicitProvider = processEnv[EXPLICIT_PROVIDER_ENV_VAR]?.trim() - if (explicitProvider) return explicitProvider - - if (isEnvTruthy(processEnv.CLAUDE_CODE_USE_GEMINI)) return 'gemini' - if (isEnvTruthy(processEnv.CLAUDE_CODE_USE_GITHUB)) return 'github' - if (isEnvTruthy(processEnv.CLAUDE_CODE_USE_OPENAI)) return 'openai' - if (isEnvTruthy(processEnv.CLAUDE_CODE_USE_BEDROCK)) return 'bedrock' - if (isEnvTruthy(processEnv.CLAUDE_CODE_USE_VERTEX)) return 'vertex' - if (isEnvTruthy(processEnv.CLAUDE_CODE_USE_FOUNDRY)) return 'foundry' - - return undefined + return processEnv[EXPLICIT_PROVIDER_ENV_VAR]?.trim() || undefined } function isGithubModel(model: string | undefined): boolean { diff --git a/src/utils/providerProfile.test.ts b/src/utils/providerProfile.test.ts index 39dcc059..9a711199 100644 --- a/src/utils/providerProfile.test.ts +++ b/src/utils/providerProfile.test.ts @@ -485,6 +485,26 @@ test('buildStartupEnvFromProfile leaves explicit provider selections untouched', assert.equal(env.OPENAI_API_KEY, undefined) }) +test('buildStartupEnvFromProfile preserves explicit anthropic startup selection', async () => { + const processEnv = { + CLAUDE_CODE_EXPLICIT_PROVIDER: 'anthropic', + } + + const env = await buildStartupEnvFromProfile({ + persisted: profile('openai', { + CLAUDE_CODE_USE_GITHUB: '1', + OPENAI_MODEL: 'github:copilot', + }), + processEnv, + }) + + assert.equal(env, processEnv) + assert.equal(env.CLAUDE_CODE_EXPLICIT_PROVIDER, 'anthropic') + assert.equal(env.CLAUDE_CODE_USE_OPENAI, undefined) + assert.equal(env.CLAUDE_CODE_USE_GITHUB, undefined) + assert.equal(env.OPENAI_MODEL, undefined) +}) + test('buildStartupEnvFromProfile leaves profile-managed env untouched', async () => { const processEnv = { CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED: '1', diff --git a/src/utils/providerProfile.ts b/src/utils/providerProfile.ts index f327779d..7bd7985e 100644 --- a/src/utils/providerProfile.ts +++ b/src/utils/providerProfile.ts @@ -412,6 +412,10 @@ export function hasExplicitProviderSelection( return true } + if (processEnv.CLAUDE_CODE_EXPLICIT_PROVIDER?.trim()) { + return true + } + return ( processEnv.CLAUDE_CODE_USE_OPENAI !== undefined || processEnv.CLAUDE_CODE_USE_GITHUB !== undefined ||