fix: let saved provider profiles win on restart (#513)
Treat profile-managed env as restart state rather than explicit user intent so saved OpenAI-compatible profiles can replace stale Ollama values on startup and persist correctly across restarts. Co-authored-by: Claude Opus 4.6 <noreply@openclaude.dev>
This commit is contained in:
committed by
GitHub
parent
07621a6f8d
commit
cb8f8b7ac2
@@ -485,24 +485,31 @@ test('buildStartupEnvFromProfile leaves explicit provider selections untouched',
|
|||||||
assert.equal(env.OPENAI_API_KEY, undefined)
|
assert.equal(env.OPENAI_API_KEY, undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('buildStartupEnvFromProfile leaves profile-managed env untouched', async () => {
|
test('buildStartupEnvFromProfile lets saved startup profile override profile-managed env', async () => {
|
||||||
const processEnv = {
|
const processEnv = {
|
||||||
CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED: '1',
|
CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED: '1',
|
||||||
ANTHROPIC_BASE_URL: 'https://api.anthropic.com',
|
CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED_ID: 'saved_ollama',
|
||||||
ANTHROPIC_MODEL: 'claude-sonnet-4-6',
|
CLAUDE_CODE_USE_OPENAI: '1',
|
||||||
|
OPENAI_BASE_URL: 'http://localhost:11434/v1',
|
||||||
|
OPENAI_MODEL: 'llama3.1:8b',
|
||||||
}
|
}
|
||||||
|
|
||||||
const env = await buildStartupEnvFromProfile({
|
const env = await buildStartupEnvFromProfile({
|
||||||
persisted: profile('openai', {
|
persisted: profile('openai', {
|
||||||
OPENAI_API_KEY: 'sk-persisted',
|
OPENAI_API_KEY: 'sk-persisted',
|
||||||
OPENAI_MODEL: 'gpt-4o',
|
OPENAI_MODEL: 'Meta-Llama-3.1-70B-Instruct',
|
||||||
|
OPENAI_BASE_URL: 'https://api.sambanova.ai/v1',
|
||||||
}),
|
}),
|
||||||
processEnv,
|
processEnv,
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.equal(env, processEnv)
|
assert.notEqual(env, processEnv)
|
||||||
assert.equal(env.ANTHROPIC_MODEL, 'claude-sonnet-4-6')
|
assert.equal(env.CLAUDE_CODE_USE_OPENAI, '1')
|
||||||
assert.equal(env.OPENAI_MODEL, undefined)
|
assert.equal(env.OPENAI_API_KEY, 'sk-persisted')
|
||||||
|
assert.equal(env.OPENAI_MODEL, 'Meta-Llama-3.1-70B-Instruct')
|
||||||
|
assert.equal(env.OPENAI_BASE_URL, 'https://api.sambanova.ai/v1')
|
||||||
|
assert.equal(env.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED, undefined)
|
||||||
|
assert.equal(env.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED_ID, undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('buildStartupEnvFromProfile treats explicit falsey provider flags as user intent', async () => {
|
test('buildStartupEnvFromProfile treats explicit falsey provider flags as user intent', async () => {
|
||||||
|
|||||||
@@ -667,22 +667,39 @@ export async function buildStartupEnvFromProfile(options?: {
|
|||||||
readGeminiAccessToken?: () => string | undefined
|
readGeminiAccessToken?: () => string | undefined
|
||||||
}): Promise<NodeJS.ProcessEnv> {
|
}): Promise<NodeJS.ProcessEnv> {
|
||||||
const processEnv = options?.processEnv ?? process.env
|
const processEnv = options?.processEnv ?? process.env
|
||||||
if (hasExplicitProviderSelection(processEnv)) {
|
const persisted = options?.persisted ?? loadProfileFile()
|
||||||
|
|
||||||
|
// Saved /provider profiles should still win over provider-manager env that was
|
||||||
|
// auto-applied during startup. Only explicit shell/flag provider selection
|
||||||
|
// should bypass the persisted startup profile.
|
||||||
|
const profileManagedEnv = processEnv.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED === '1'
|
||||||
|
if (hasExplicitProviderSelection(processEnv) && !profileManagedEnv) {
|
||||||
return processEnv
|
return processEnv
|
||||||
}
|
}
|
||||||
|
|
||||||
const persisted = options?.persisted ?? loadProfileFile()
|
|
||||||
if (!persisted) {
|
if (!persisted) {
|
||||||
return processEnv
|
return processEnv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const launchProcessEnv = profileManagedEnv
|
||||||
|
? (() => {
|
||||||
|
const cleanedEnv = { ...processEnv }
|
||||||
|
for (const key of PROFILE_ENV_KEYS) {
|
||||||
|
delete cleanedEnv[key]
|
||||||
|
}
|
||||||
|
delete cleanedEnv.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED
|
||||||
|
delete cleanedEnv.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED_ID
|
||||||
|
return cleanedEnv
|
||||||
|
})()
|
||||||
|
: processEnv
|
||||||
|
|
||||||
return buildLaunchEnv({
|
return buildLaunchEnv({
|
||||||
profile: persisted.profile,
|
profile: persisted.profile,
|
||||||
persisted,
|
persisted,
|
||||||
goal:
|
goal:
|
||||||
options?.goal ??
|
options?.goal ??
|
||||||
normalizeRecommendationGoal(processEnv.OPENCLAUDE_PROFILE_GOAL),
|
normalizeRecommendationGoal(processEnv.OPENCLAUDE_PROFILE_GOAL),
|
||||||
processEnv,
|
processEnv: launchProcessEnv,
|
||||||
getOllamaChatBaseUrl:
|
getOllamaChatBaseUrl:
|
||||||
options?.getOllamaChatBaseUrl ?? getOllamaChatBaseUrl,
|
options?.getOllamaChatBaseUrl ?? getOllamaChatBaseUrl,
|
||||||
resolveOllamaDefaultModel: options?.resolveOllamaDefaultModel,
|
resolveOllamaDefaultModel: options?.resolveOllamaDefaultModel,
|
||||||
|
|||||||
Reference in New Issue
Block a user