Skip to content

Commit 25e5190

Browse files
committed
fix(auth): always show the OAuth wizard on first launch
Env-var auto-detect would silently skip the wizard when stray API keys (ANTHROPIC_API_KEY, OPENAI_API_KEY, …) were set in the user's shell or a cwd .env file — even for a brand-new install where the user almost certainly wants the in-house default with free credits rather than spending their own provider budget. Now: only honor the legacy env-var auto-detect when ~/.codebase/ credentials.json exists (the wizard creates it on first sign-in). For first launches, env vars stay set for the user but don't short-circuit the OAuth offer. After they sign in once, the file exists and env-var fallback resumes as a power-user escape hatch.
1 parent 86edaaf commit 25e5190

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

src/agent/config.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,31 @@ export function resolveConfig(envOrOpts: NodeJS.ProcessEnv | ResolveConfigOption
116116
return { model, apiKey, source: "explicit" };
117117
}
118118

119-
for (const provider of AUTO_DETECT_ORDER) {
120-
const apiKey = getEnvApiKey(provider);
121-
if (!apiKey) continue;
122-
123-
const modelId = DEFAULT_MODELS[provider];
124-
if (!modelId) continue;
125-
126-
const model = getModel(provider, modelId as never);
127-
if (!model) continue;
128-
129-
return { model, apiKey, source: "auto" };
119+
// Env-var auto-detect is the legacy power-user path. We only honor it
120+
// for users who have already onboarded — otherwise a stray
121+
// ANTHROPIC_API_KEY or OPENAI_API_KEY in the shell would silently
122+
// skip the OAuth wizard the first time a new user runs `codebase`,
123+
// even though they almost certainly want the in-house default model
124+
// with free credits rather than spending their own API key budget.
125+
//
126+
// "Already onboarded" = credentials.json exists on disk, even if it's
127+
// now expired or empty. The wizard creates that file on first
128+
// sign-in, so its presence is a reliable "this user knows what
129+
// they're doing" signal.
130+
const hasOnboarded = credentials.exists();
131+
if (hasOnboarded) {
132+
for (const provider of AUTO_DETECT_ORDER) {
133+
const apiKey = getEnvApiKey(provider);
134+
if (!apiKey) continue;
135+
136+
const modelId = DEFAULT_MODELS[provider];
137+
if (!modelId) continue;
138+
139+
const model = getModel(provider, modelId as never);
140+
if (!model) continue;
141+
142+
return { model, apiKey, source: "auto" };
143+
}
130144
}
131145

132146
throw new ConfigError(

src/auth/credentials.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ export class CredentialsStore {
5757
return this.path;
5858
}
5959

60+
/**
61+
* True iff a credentials file exists on disk, regardless of whether
62+
* it parses or whether the token is still valid. Used to detect
63+
* "user has gone through the first-run wizard at least once" so we
64+
* don't silently skip the OAuth offer for a brand-new install just
65+
* because some stray API key happens to be in the shell env.
66+
*/
67+
exists(): boolean {
68+
return existsSync(this.path);
69+
}
70+
6071
load(): Credentials | null {
6172
if (!existsSync(this.path)) return null;
6273
let raw: string;

0 commit comments

Comments
 (0)