fix(provider): saved profile ignored when stale CLAUDE_CODE_USE_* in shell (#807)
* fix(provider): saved profile ignored when stale CLAUDE_CODE_USE_* in shell Users reported "my saved /provider profile isn't picked up at startup — the banner shows gpt-4o / api.openai.com even though I saved Moonshot". Root cause: applyActiveProviderProfileFromConfig() bailed out whenever hasProviderSelectionFlags(processEnv) was true — i.e. whenever ANY CLAUDE_CODE_USE_* flag was present. But a bare `CLAUDE_CODE_USE_OPENAI=1` with no paired OPENAI_BASE_URL / OPENAI_MODEL is almost always a stale shell export left over from a prior manual setup, not genuine startup intent. Respecting it skipped the saved profile and let StartupScreen.ts fall through to the hardcoded `gpt-4o` / `https://api.openai.com/v1` defaults — the exact symptom users see. Fix: narrow the guard from "any flag set" to "flag set AND at least one concrete config value (BASE_URL, MODEL, or API_KEY)". A bare stale flag no longer blocks the saved profile. A real shell selection (flag + URL or flag + model) still wins, preserving the "explicit startup intent overrides saved profile" contract. New helper: hasCompleteProviderSelection(env). Per-provider check for a paired concrete value. Bedrock/Vertex/Foundry keep the flag-alone semantic since they rely on ambient AWS/GCP credentials rather than env config. Three new tests cover the bug and the two counter-cases: - bare USE flag → profile applies (fixes the bug) - USE flag + BASE_URL → profile blocked (preserves explicit intent) - USE flag + MODEL → profile blocked (preserves explicit intent) Co-Authored-By: OpenClaude <openclaude@gitlawb.com> * fix(provider): don't overlay stale legacy profile on plural-managed env Second half of the "saved profile not picked up in banner" bug. The prior commit fixed the guard that prevented applyActiveProviderProfileFromConfig() from firing when a stale CLAUDE_CODE_USE_* flag was in the shell. But even when the plural system applies correctly, buildStartupEnvFromProfile() was then loading the legacy .openclaude-profile.json AND overwriting the plural-managed env with whatever that file contained. addProviderProfile() (the call path the /provider preset picker uses) does NOT sync the legacy file, so a user who went: manual setup: CLAUDE_CODE_USE_OPENAI=1 + OPENAI_MODEL=gpt-4o → writes .openclaude-profile.json as { openai, gpt-4o, ... } /provider: add Moonshot preset, mark active → writes plural config; legacy file UNCHANGED would see startup reliably apply Moonshot env first, then get it clobbered by the stale legacy file. Banner shows gpt-4o / api.openai.com while runtime ends up with the correct env via a different code path — exactly the user-reported symptom. Fix: in buildStartupEnvFromProfile, when the plural system has already set env (CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED === '1'), skip the legacy-file overlay entirely and return processEnv unchanged. Legacy is now strictly a first-run / fallback path for users who haven't adopted the plural system. Also removes the stripped-then-rebuilt env construction that was part of the old overlay path — no longer needed. Test updates: - Replaced "lets saved startup profile override profile-managed env" (encoded the old broken behavior) with a regression test that pins the new semantic: plural env survives when legacy is stale. - Added "falls back to legacy when plural hasn't applied" to pin the first-run path still works. Co-Authored-By: OpenClaude <openclaude@gitlawb.com> --------- Co-authored-by: OpenClaude <openclaude@gitlawb.com>
This commit is contained in:
@@ -841,43 +841,35 @@ export async function buildStartupEnvFromProfile(options?: {
|
||||
const processEnv = options?.processEnv ?? process.env
|
||||
const persisted = options?.persisted ?? loadProfileFile()
|
||||
|
||||
// Saved /provider profiles should still win over provider-manager env that was
|
||||
// auto-applied during startup. Only an explicit shell/flag provider selection
|
||||
// should bypass the persisted startup profile.
|
||||
//
|
||||
const profileManagedEnv = processEnv.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED === '1'
|
||||
|
||||
// If the user explicitly selected a provider via env, allow it to bypass
|
||||
// the persisted profile only when we can prove it was managed by the
|
||||
// persisted profile env itself.
|
||||
// The legacy single-profile file (~/.openclaude-profile.json) is a
|
||||
// first-run / fallback mechanism. The newer plural provider-profile
|
||||
// system (`/provider` presets + activeProviderProfileId in config) is
|
||||
// applied earlier in the bootstrap via applyActiveProviderProfileFromConfig
|
||||
// and signals completion with CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED=1.
|
||||
//
|
||||
// Practically: on initial startup, provider routing env vars can already
|
||||
// be present due to earlier auto-application steps. We should still apply
|
||||
// the persisted profile rather than returning early.
|
||||
// If the plural system has already set env, trust it — do NOT overlay the
|
||||
// legacy file. addProviderProfile() does not sync the legacy file, so a
|
||||
// stale legacy file (e.g. OpenAI defaults from an earlier manual setup)
|
||||
// would otherwise overwrite the correct plural env and surface as the
|
||||
// "banner shows gpt-4o / api.openai.com even though my saved profile is
|
||||
// Moonshot" bug.
|
||||
if (profileManagedEnv) {
|
||||
return processEnv
|
||||
}
|
||||
|
||||
if (!persisted) {
|
||||
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({
|
||||
profile: persisted.profile,
|
||||
persisted,
|
||||
goal:
|
||||
options?.goal ??
|
||||
normalizeRecommendationGoal(processEnv.OPENCLAUDE_PROFILE_GOAL),
|
||||
processEnv: launchProcessEnv,
|
||||
processEnv,
|
||||
getOllamaChatBaseUrl:
|
||||
options?.getOllamaChatBaseUrl ?? getOllamaChatBaseUrl,
|
||||
resolveOllamaDefaultModel: options?.resolveOllamaDefaultModel,
|
||||
|
||||
Reference in New Issue
Block a user