fix: preserve explicit provider startup intent

This commit is contained in:
gnanam1990
2026-04-07 14:50:20 +05:30
parent 65dd19cf87
commit 139610950c
4 changed files with 54 additions and 13 deletions

View File

@@ -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' })
})
})

View File

@@ -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 {

View File

@@ -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',

View File

@@ -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 ||