Compare commits

...

4 Commits

Author SHA1 Message Date
gnanam1990
b5dbb71a44 fix: preserve explicit ollama startup intent 2026-04-07 19:40:47 +05:30
gnanam1990
b2cabdd950 fix: preserve explicit provider intent for active profiles 2026-04-07 18:47:35 +05:30
gnanam1990
139610950c fix: preserve explicit provider startup intent 2026-04-07 14:50:20 +05:30
gnanam1990
65dd19cf87 fix: preserve explicit startup provider selection 2026-04-07 10:08:30 +05:30
13 changed files with 352 additions and 14 deletions

1
.gitignore vendored
View File

@@ -10,3 +10,4 @@ GEMINI.md
package-lock.json
/.claude
coverage/
.worktrees/

View File

@@ -238,6 +238,7 @@ import { usePromptsFromClaudeInChrome } from 'src/hooks/usePromptsFromClaudeInCh
import { getTipToShowOnSpinner, recordShownTip } from 'src/services/tips/tipScheduler.js';
import type { Theme } from 'src/utils/theme.js';
import { isPromptTypingSuppressionActive } from './replInputSuppression.js';
import { shouldStartStartupChecks } from './replStartupGates.js';
import { checkAndDisableBypassPermissionsIfNeeded, checkAndDisableAutoModeIfNeeded, useKickOffCheckAndDisableBypassPermissionsIfNeeded, useKickOffCheckAndDisableAutoModeIfNeeded } from 'src/utils/permissions/bypassPermissionsKillswitch.js';
import { SandboxManager } from 'src/utils/sandbox/sandbox-adapter.js';
import { SANDBOX_NETWORK_ACCESS_TOOL_NAME } from 'src/cli/structuredIO.js';
@@ -784,19 +785,6 @@ export function REPL({
});
const tasksV2 = useTasksV2WithCollapseEffect();
// Start background plugin installations
// SECURITY: This code is guaranteed to run ONLY after the "trust this folder" dialog
// has been confirmed by the user. The trust dialog is shown in cli.tsx (line ~387)
// before the REPL component is rendered. The dialog blocks execution until the user
// accepts, and only then is the REPL component mounted and this effect runs.
// This ensures that plugin installations from repository and user settings only
// happen after explicit user consent to trust the current working directory.
useEffect(() => {
if (isRemoteSession) return;
void performStartupChecks(setAppState);
}, [setAppState, isRemoteSession]);
// Allow Claude in Chrome MCP to send prompts through MCP notifications
// and sync permission mode changes to the Chrome extension
usePromptsFromClaudeInChrome(isRemoteSession ? EMPTY_MCP_CLIENTS : mcpClients, toolPermissionContext.mode);
@@ -1337,6 +1325,7 @@ export function REPL({
const [inputValue, setInputValueRaw] = useState(() => consumeEarlyInput());
const inputValueRef = useRef(inputValue);
inputValueRef.current = inputValue;
const startupChecksStartedRef = useRef(false);
const promptTypingSuppressionActive = isPromptTypingSuppressionActive(isPromptInputActive, inputValue);
const insertTextRef = useRef<{
insert: (text: string) => void;
@@ -1344,6 +1333,24 @@ export function REPL({
cursorOffset: number;
} | null>(null);
// Start background plugin installations after the initial input window is idle.
// SECURITY: This still runs only after the "trust this folder" dialog has been
// confirmed because the REPL is not mounted until that dialog completes.
useEffect(() => {
if (
!shouldStartStartupChecks({
isRemoteSession,
promptTypingSuppressionActive,
startupChecksStarted: startupChecksStartedRef.current,
})
) {
return;
}
startupChecksStartedRef.current = true;
void performStartupChecks(setAppState);
}, [isRemoteSession, promptTypingSuppressionActive, setAppState]);
// Wrap setInputValue to co-locate suppression state updates.
// Both setState calls happen in the same synchronous context so React
// batches them into a single render, eliminating the extra render that

View File

@@ -0,0 +1,44 @@
import { describe, expect, test } from 'bun:test'
import { shouldStartStartupChecks } from './replStartupGates.js'
describe('shouldStartStartupChecks', () => {
test('returns false for remote sessions', () => {
expect(
shouldStartStartupChecks({
isRemoteSession: true,
promptTypingSuppressionActive: false,
startupChecksStarted: false,
}),
).toBe(false)
})
test('returns false while prompt typing suppression is active', () => {
expect(
shouldStartStartupChecks({
isRemoteSession: false,
promptTypingSuppressionActive: true,
startupChecksStarted: false,
}),
).toBe(false)
})
test('returns true once local startup is idle and checks have not started', () => {
expect(
shouldStartStartupChecks({
isRemoteSession: false,
promptTypingSuppressionActive: false,
startupChecksStarted: false,
}),
).toBe(true)
})
test('returns false after startup checks have already started', () => {
expect(
shouldStartStartupChecks({
isRemoteSession: false,
promptTypingSuppressionActive: false,
startupChecksStarted: true,
}),
).toBe(false)
})
})

View File

@@ -0,0 +1,11 @@
export function shouldStartStartupChecks(options: {
isRemoteSession: boolean
promptTypingSuppressionActive: boolean
startupChecksStarted: boolean
}): boolean {
return (
!options.isRemoteSession &&
!options.promptTypingSuppressionActive &&
!options.startupChecksStarted
)
}

View File

@@ -8,6 +8,7 @@ import {
} from './managedEnvConstants.js'
import { clearMTLSCache } from './mtls.js'
import { clearProxyCache, configureGlobalAgents } from './proxy.js'
import { filterSettingsEnvForExplicitProvider } from './providerEnvSelection.js'
import { applyActiveProviderProfileFromConfig } from './providerProfiles.js'
import { isSettingSourceEnabled } from './settings/constants.js'
import {
@@ -87,7 +88,9 @@ function filterSettingsEnv(
env: Record<string, string> | undefined,
): Record<string, string> {
return withoutCcdSpawnEnvKeys(
filterSettingsEnvForExplicitProvider(
withoutHostManagedProviderVars(withoutSSHTunnelVars(env)),
),
)
}

View File

@@ -0,0 +1,116 @@
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
import { filterSettingsEnvForExplicitProvider } from './providerEnvSelection.js'
const originalEnv = { ...process.env }
const RESET_KEYS = [
'CLAUDE_CODE_EXPLICIT_PROVIDER',
'CLAUDE_CODE_USE_OPENAI',
'CLAUDE_CODE_USE_GEMINI',
'CLAUDE_CODE_USE_GITHUB',
'CLAUDE_CODE_USE_BEDROCK',
'CLAUDE_CODE_USE_VERTEX',
'CLAUDE_CODE_USE_FOUNDRY',
] as const
beforeEach(() => {
for (const key of RESET_KEYS) {
delete process.env[key]
}
})
afterEach(() => {
for (const key of RESET_KEYS) {
if (originalEnv[key] === undefined) delete process.env[key]
else process.env[key] = originalEnv[key]
}
})
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'
expect(
filterSettingsEnvForExplicitProvider({
CLAUDE_CODE_USE_GITHUB: '1',
CLAUDE_CODE_USE_OPENAI: '1',
OTHER: 'keep-me',
}),
).toEqual({ OTHER: 'keep-me' })
})
test('strips a stale GitHub model when explicit provider is not github', () => {
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'openai'
expect(
filterSettingsEnvForExplicitProvider({
OPENAI_MODEL: 'github:copilot',
OTHER: 'keep-me',
}),
).toEqual({ OTHER: 'keep-me' })
})
test('keeps a normal OpenAI model when explicit provider is openai', () => {
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'openai'
expect(
filterSettingsEnvForExplicitProvider({
OPENAI_MODEL: 'gpt-4o',
OTHER: 'keep-me',
}),
).toEqual({ OPENAI_MODEL: 'gpt-4o', OTHER: 'keep-me' })
})
test('strips a non-GitHub OpenAI model when explicit provider is github', () => {
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'github'
expect(
filterSettingsEnvForExplicitProvider({
OPENAI_MODEL: 'gpt-4o',
OTHER: 'keep-me',
}),
).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' })
})
test('preserves explicit ollama startup intent by stripping OpenAI routing settings', () => {
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'ollama'
expect(
filterSettingsEnvForExplicitProvider({
OPENAI_BASE_URL: 'https://api.openai.com/v1',
OPENAI_MODEL: 'gpt-4o',
OPENAI_API_KEY: 'sk-test',
OTHER: 'keep-me',
}),
).toEqual({ OTHER: 'keep-me' })
})
})

View File

@@ -0,0 +1,63 @@
export const EXPLICIT_PROVIDER_ENV_VAR = 'CLAUDE_CODE_EXPLICIT_PROVIDER'
const PROVIDER_FLAG_KEYS = [
'CLAUDE_CODE_USE_OPENAI',
'CLAUDE_CODE_USE_GEMINI',
'CLAUDE_CODE_USE_GITHUB',
'CLAUDE_CODE_USE_BEDROCK',
'CLAUDE_CODE_USE_VERTEX',
'CLAUDE_CODE_USE_FOUNDRY',
] as const
export function clearProviderSelectionFlags(
env: NodeJS.ProcessEnv = process.env,
): void {
for (const key of PROVIDER_FLAG_KEYS) {
delete env[key]
}
}
function getExplicitProvider(processEnv: NodeJS.ProcessEnv): string | undefined {
return processEnv[EXPLICIT_PROVIDER_ENV_VAR]?.trim() || undefined
}
function isGithubModel(model: string | undefined): boolean {
return (model ?? '').trim().toLowerCase().startsWith('github:')
}
export function filterSettingsEnvForExplicitProvider(
env: Record<string, string> | undefined,
processEnv: NodeJS.ProcessEnv = process.env,
): Record<string, string> {
if (!env) return {}
const explicitProvider = getExplicitProvider(processEnv)
if (!explicitProvider) {
return env
}
const filtered = { ...env }
for (const key of PROVIDER_FLAG_KEYS) {
delete filtered[key]
}
if (explicitProvider === 'ollama') {
delete filtered.OPENAI_BASE_URL
delete filtered.OPENAI_MODEL
delete filtered.OPENAI_API_KEY
return filtered
}
if (explicitProvider === 'github') {
if (!isGithubModel(filtered.OPENAI_MODEL)) {
delete filtered.OPENAI_MODEL
}
return filtered
}
if (isGithubModel(filtered.OPENAI_MODEL)) {
delete filtered.OPENAI_MODEL
}
return filtered
}

View File

@@ -9,11 +9,13 @@ import {
const originalEnv = { ...process.env }
const RESET_KEYS = [
'CLAUDE_CODE_EXPLICIT_PROVIDER',
'CLAUDE_CODE_USE_OPENAI',
'CLAUDE_CODE_USE_GEMINI',
'CLAUDE_CODE_USE_GITHUB',
'CLAUDE_CODE_USE_BEDROCK',
'CLAUDE_CODE_USE_VERTEX',
'CLAUDE_CODE_USE_FOUNDRY',
'OPENAI_BASE_URL',
'OPENAI_API_KEY',
'OPENAI_MODEL',
@@ -83,6 +85,16 @@ describe('applyProviderFlag - openai', () => {
applyProviderFlag('openai', ['--model', 'gpt-4o'])
expect(process.env.OPENAI_MODEL).toBe('gpt-4o')
})
test('clears a previously persisted GitHub flag', () => {
process.env.CLAUDE_CODE_USE_GITHUB = '1'
const result = applyProviderFlag('openai', [])
expect(result.error).toBeUndefined()
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBe('1')
})
})
describe('applyProviderFlag - gemini', () => {
@@ -104,6 +116,16 @@ describe('applyProviderFlag - github', () => {
expect(result.error).toBeUndefined()
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBe('1')
})
test('clears a previously set OpenAI flag', () => {
process.env.CLAUDE_CODE_USE_OPENAI = '1'
const result = applyProviderFlag('github', [])
expect(result.error).toBeUndefined()
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBeUndefined()
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBe('1')
})
})
describe('applyProviderFlag - bedrock', () => {
@@ -151,6 +173,19 @@ describe('applyProviderFlag - invalid provider', () => {
})
})
describe('applyProviderFlag - anthropic', () => {
test('clears third-party provider flags', () => {
process.env.CLAUDE_CODE_USE_GITHUB = '1'
process.env.CLAUDE_CODE_USE_OPENAI = '1'
const result = applyProviderFlag('anthropic', [])
expect(result.error).toBeUndefined()
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBeUndefined()
})
})
describe('applyProviderFlagFromArgs', () => {
test('applies ollama provider and model from argv in one step', () => {
const result = applyProviderFlagFromArgs([

View File

@@ -1,3 +1,8 @@
import {
clearProviderSelectionFlags,
EXPLICIT_PROVIDER_ENV_VAR,
} from './providerEnvSelection.js'
/**
* --provider CLI flag support.
*
@@ -77,6 +82,9 @@ export function applyProviderFlag(
}
}
clearProviderSelectionFlags()
process.env[EXPLICIT_PROVIDER_ENV_VAR] = provider
const model = parseModelFlag(args)
switch (provider as ProviderFlagName) {

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

View File

@@ -9,6 +9,7 @@ async function importFreshProvidersModule() {
const originalEnv = { ...process.env }
const RESTORED_KEYS = [
'CLAUDE_CODE_EXPLICIT_PROVIDER',
'CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED',
'CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED_ID',
'CLAUDE_CODE_USE_OPENAI',
@@ -142,6 +143,29 @@ describe('applyProviderProfileToProcessEnv', () => {
})
describe('applyActiveProviderProfileFromConfig', () => {
test('does not override explicit anthropic startup selection', async () => {
const { applyActiveProviderProfileFromConfig } =
await importFreshProviderProfileModules()
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'anthropic'
const applied = applyActiveProviderProfileFromConfig({
providerProfiles: [
buildProfile({
id: 'saved_github',
baseUrl: 'https://api.githubcopilot.com',
model: 'github:copilot',
}),
],
activeProviderProfileId: 'saved_github',
} as any)
expect(applied).toBeUndefined()
expect(process.env.CLAUDE_CODE_EXPLICIT_PROVIDER).toBe('anthropic')
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBeUndefined()
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
expect(process.env.OPENAI_MODEL).toBeUndefined()
})
test('does not override explicit startup provider selection', async () => {
const { applyActiveProviderProfileFromConfig } =
await importFreshProviderProfileModules()

View File

@@ -5,6 +5,7 @@ import {
type ProviderProfile,
} from './config.js'
import type { ModelOption } from './model/modelOptions.js'
import { EXPLICIT_PROVIDER_ENV_VAR } from './providerEnvSelection.js'
export type ProviderPreset =
| 'anthropic'
@@ -256,6 +257,7 @@ function hasProviderSelectionFlags(
processEnv: NodeJS.ProcessEnv = process.env,
): boolean {
return (
processEnv[EXPLICIT_PROVIDER_ENV_VAR] !== undefined ||
processEnv.CLAUDE_CODE_USE_OPENAI !== undefined ||
processEnv.CLAUDE_CODE_USE_GEMINI !== undefined ||
processEnv.CLAUDE_CODE_USE_GITHUB !== undefined ||