Skip to content

Commit 630aac9

Browse files
authored
feat(onboard): label CLI candidates with subscription vs API-key auth (#122140)
Detected Codex/Claude Code CLI candidates now say whether they ride a flat-rate subscription login (ChatGPT, Claude) or a metered API key, so picking one is an informed billing decision for an always-on assistant. Codex classification parses `codex login status` output per codex-rs/cli/src/login.rs; Claude uses prompt-free credential-file and env/apiKeyHelper signals only (never the keychain). Unknown states keep the plain 'logged in'. Display-only: ranking and defaults unchanged.
1 parent 2a1eba7 commit 630aac9

2 files changed

Lines changed: 124 additions & 25 deletions

File tree

‎src/commands/onboard-inference.test.ts‎

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,49 @@ describe("detectInferenceBackends", () => {
131131
"anthropic-api-key",
132132
"claude-cli",
133133
]);
134-
expect(candidates[1]).toMatchObject({ credentials: true, detail: "logged in" });
134+
expect(candidates[1]).toMatchObject({
135+
credentials: true,
136+
detail: "logged in · API key (usage-billed)",
137+
});
135138
});
136139

140+
it("labels a Claude CLI environment key as usage-billed", async () => {
141+
const candidates = await detectInferenceBackends({
142+
env: { ANTHROPIC_API_KEY: "sk-y" },
143+
platform: "linux",
144+
deps: {
145+
probeLocalCommand: probeDeps({ claude: true }),
146+
readClaudeCliCredentials: () => null,
147+
},
148+
});
149+
150+
expect(candidates.find((candidate) => candidate.kind === "claude-cli")?.detail).toBe(
151+
"logged in · API key (usage-billed)",
152+
);
153+
});
154+
155+
it.each(["oauth", "token"])(
156+
"labels parsed Claude CLI %s credentials as a subscription",
157+
async (type) => {
158+
const candidates = await detectInferenceBackends({
159+
env: {},
160+
platform: "linux",
161+
deps: {
162+
probeLocalCommand: probeDeps({ claude: true }),
163+
readClaudeCliCredentials: () => ({ type }),
164+
},
165+
});
166+
167+
expect(candidates).toMatchObject([
168+
{
169+
kind: "claude-cli",
170+
credentials: true,
171+
detail: "logged in · Claude subscription",
172+
},
173+
]);
174+
},
175+
);
176+
137177
it("keeps an Anthropic environment key ahead of unknown Claude credentials", async () => {
138178
const candidates = await detectInferenceBackends({
139179
env: { ANTHROPIC_API_KEY: "sk-y" },
@@ -176,7 +216,7 @@ describe("detectInferenceBackends", () => {
176216
kind: "claude-cli",
177217
credentials: true,
178218
detail:
179-
"logged in; Claude Code 2.1.206 is the first published build known to advertise msg_lifecycle_v1; found 2.1.205. OpenClaw verifies this capability at runtime. If this build is rejected, run `claude update`, restart OpenClaw, and retry.",
219+
"logged in · Claude subscription; Claude Code 2.1.206 is the first published build known to advertise msg_lifecycle_v1; found 2.1.205. OpenClaw verifies this capability at runtime. If this build is rejected, run `claude update`, restart OpenClaw, and retry.",
180220
},
181221
]);
182222
});
@@ -320,11 +360,19 @@ describe("detectInferenceBackends", () => {
320360
).toBeUndefined();
321361
});
322362

323-
it("recognizes Codex login status across native credential stores", async () => {
363+
it.each([
364+
["ChatGPT", "Logged in using ChatGPT", "logged in · ChatGPT subscription"],
365+
[
366+
"API key",
367+
"Logged in using an API key - sk-proj-1***23456",
368+
"logged in · API key (usage-billed)",
369+
],
370+
["unrecognized auth", "Logged in using access token", "logged in"],
371+
])("classifies Codex %s login status", async (_auth, loginOutput, expectedDetail) => {
324372
const probe = async (command: string, args: string[] = ["--version"]) => ({
325373
command,
326374
found: command === "codex",
327-
...(args[0] === "login" ? {} : { version: "codex 1.0" }),
375+
version: args[0] === "login" ? loginOutput : "codex 1.0",
328376
});
329377
const candidates = await detectInferenceBackends({
330378
env: {},
@@ -335,7 +383,7 @@ describe("detectInferenceBackends", () => {
335383
});
336384

337385
expect(candidates).toMatchObject([
338-
{ kind: "codex-cli", credentials: true, detail: "logged in" },
386+
{ kind: "codex-cli", credentials: true, detail: expectedDetail },
339387
]);
340388
});
341389

‎src/commands/onboard-inference.ts‎

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,75 @@ function detectCliCredentialState(params: {
8383
return params.platform === "darwin" ? undefined : false;
8484
}
8585

86-
function describeCliDetail(credentials: boolean | undefined, loginHint: string): string {
87-
if (credentials === true) {
86+
type CliAuthKind = "api-key" | "chatgpt-subscription" | "claude-subscription";
87+
type CliLoginState = { credentials: boolean | undefined; authKind?: CliAuthKind };
88+
89+
const CLI_AUTH_KIND_LABEL: Record<CliAuthKind, string> = {
90+
"api-key": "API key (usage-billed)",
91+
"chatgpt-subscription": "ChatGPT subscription",
92+
"claude-subscription": "Claude subscription",
93+
};
94+
95+
function describeCliDetail(state: CliLoginState, loginHint: string): string {
96+
if (state.authKind) {
97+
return `logged in · ${CLI_AUTH_KIND_LABEL[state.authKind]}`;
98+
}
99+
if (state.credentials === true) {
88100
return "logged in";
89101
}
90-
if (credentials === false) {
102+
if (state.credentials === false) {
91103
return `installed, not logged in — ${loginHint}, then check again`;
92104
}
93105
return "installed";
94106
}
95107

108+
function classifyClaudeCliAuth(
109+
credential: { type: string } | null,
110+
env: NodeJS.ProcessEnv,
111+
): CliAuthKind | undefined {
112+
if (env.ANTHROPIC_API_KEY?.trim() || credential?.type === "api_key_helper") {
113+
return "api-key";
114+
}
115+
if (credential?.type === "oauth" || credential?.type === "token") {
116+
return "claude-subscription";
117+
}
118+
return undefined;
119+
}
120+
96121
function describeGeminiCliDetail(credentials: boolean | undefined): string {
97122
return credentials === true
98123
? "installed; credentials found"
99124
: "installed; login status unavailable";
100125
}
101126

102-
async function detectCodexLoginState(
127+
async function classifyCodexLoginStatus(
103128
probe: typeof probeLocalCommand,
104129
command: string,
105-
): Promise<boolean | undefined> {
130+
): Promise<CliLoginState> {
106131
const status = await probe(command, ["login", "status"], { timeoutMs: 3_000 });
107-
if (!status.error) {
108-
return true;
132+
if (status.error) {
133+
// Codex login status covers its own auth store, not custom model-provider
134+
// credentials. Keep failures indeterminate so the live probe decides usability.
135+
return { credentials: undefined };
109136
}
110-
// Codex login status covers its own auth store, not custom model-provider
111-
// credentials. Keep failures indeterminate so the live probe decides usability.
112-
return undefined;
137+
if (status.version === "Logged in using ChatGPT") {
138+
return { credentials: true, authKind: "chatgpt-subscription" };
139+
}
140+
if (/^Logged in using an API key - .+$/u.test(status.version ?? "")) {
141+
return { credentials: true, authKind: "api-key" };
142+
}
143+
return { credentials: true };
144+
}
145+
146+
// Deliberately boolean-shaped: this signature is reachable from the exported
147+
// detectInferenceBackends options type and therefore part of the plugin-sdk
148+
// agent-harness API contract. Widening it would bump the contract hash — the
149+
// rich classification stays module-local in classifyCodexLoginStatus.
150+
async function detectCodexLoginState(
151+
probe: typeof probeLocalCommand,
152+
command: string,
153+
): Promise<boolean | undefined> {
154+
return (await classifyCodexLoginStatus(probe, command)).credentials;
113155
}
114156

115157
function randomizeClaudeCodexTie(
@@ -241,7 +283,10 @@ export async function detectInferenceBackends(
241283
if (credentials === true && claudeCredential?.type === "oauth") {
242284
subscriptionPromotionEligibleCliKinds.add("claude-cli");
243285
}
244-
const detail = describeCliDetail(credentials, "run `claude auth login`");
286+
const detail = describeCliDetail(
287+
{ credentials, authKind: classifyClaudeCliAuth(claudeCredential, env) },
288+
"run `claude auth login`",
289+
);
245290
// Only the live init record can prove capability support. Keep backports and
246291
// wrappers selectable here even when their version predates the known release.
247292
cliCandidates.push({
@@ -261,15 +306,21 @@ export async function detectInferenceBackends(
261306
}
262307
if (codexProbe.found && !codexProbe.timedOut) {
263308
const codexCredential = readCodex();
264-
const credentials = options.deps?.detectCodexLoginState
265-
? await options.deps.detectCodexLoginState(probe, codexProbe.command)
309+
const loginState: CliLoginState = options.deps?.detectCodexLoginState
310+
? { credentials: await options.deps.detectCodexLoginState(probe, codexProbe.command) }
266311
: options.deps?.readCodexCliCredentials
267-
? detectCliCredentialState({
268-
probe: codexProbe,
269-
hasStoredCredentials: codexCredential !== null,
270-
platform,
271-
})
272-
: await detectCodexLoginState(probe, codexProbe.command);
312+
? {
313+
credentials: detectCliCredentialState({
314+
probe: codexProbe,
315+
hasStoredCredentials: codexCredential !== null,
316+
platform,
317+
}),
318+
...(codexCredential?.type === "oauth"
319+
? { authKind: "chatgpt-subscription" as const }
320+
: {}),
321+
}
322+
: await classifyCodexLoginStatus(probe, codexProbe.command);
323+
const credentials = loginState.credentials;
273324
// Promote only prompt-free ChatGPT OAuth tokens. Status-only logins may be metered;
274325
// keychain-only ChatGPT users conservatively stay usable in the fallback tier.
275326
if (credentials === true && codexCredential?.type === "oauth") {
@@ -279,7 +330,7 @@ export async function detectInferenceBackends(
279330
kind: "codex-cli",
280331
modelRef: CODEX_APP_SERVER_DEFAULT_MODEL_REF,
281332
label: "Codex",
282-
detail: describeCliDetail(credentials, "run `codex login`"),
333+
detail: describeCliDetail(loginState, "run `codex login`"),
283334
...(credentials === undefined ? {} : { credentials }),
284335
});
285336
}

0 commit comments

Comments
 (0)