Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Codex: mark an API-key login as an API key so Settings offers no failing sign-out

AdityaUmale48a6925d5c3a
diff --git a/server/drivers/codex-login.ts b/server/drivers/codex-login.ts
index 353722c91e..8ca547a48c 100644
--- a/server/drivers/codex-login.ts
+++ b/server/drivers/codex-login.ts
@@ -24,14 +24,27 @@ export function codexReportsLogin(said: string): boolean {
return /^(?:logged in|api key configured)\b/im.test(said);
}
-export function codexLoginStatus(cli: string, env: Env): Promise<"signed-in" | "signed-out" | "unknown"> {
+/** 0.160.1 says "Logged in using an API key - …", 0.165.0 "API key configured - …": a key, not a ChatGPT sign-in. */
+const API_KEY_LOGIN = /^(?:api key configured|logged in using an api key)\b/im;
+
+export type CodexLoginStatus = "signed-in" | "signed-out" | "unknown";
+
+/** `apiKey` is true for an API-key login, which `codex logout` as a ChatGPT sign-out cannot confirm. */
+export function readCodexLogin(cli: string, env: Env): Promise<{ status: CodexLoginStatus; apiKey: boolean }> {
return new Promise((resolve) => execCli(cli, ["login", "status"], { timeout: 8000, env }, (err, stdout, stderr) => {
const said = `${stdout}\n${stderr ?? ""}`;
- resolve(!err && codexReportsLogin(said) ? "signed-in"
- : err && /^(?:not logged in|error checking login status)\b/im.test(said) ? "signed-out" : "unknown");
+ const signedIn = !err && codexReportsLogin(said);
+ resolve({
+ status: signedIn ? "signed-in" : err && /^(?:not logged in|error checking login status)\b/im.test(said) ? "signed-out" : "unknown",
+ apiKey: signedIn && API_KEY_LOGIN.test(said),
+ });
}));
}
+export async function codexLoginStatus(cli: string, env: Env): Promise<CodexLoginStatus> {
+ return (await readCodexLogin(cli, env)).status;
+}
+
/** Without a login Codex still runs a turn and fails only after a minute of reconnects. Only Codex's own "not
* logged in" counts, and a login it reads from elsewhere is remembered so later turns skip the check, until
* `forget` is called because a turn found that login gone. */
diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.ts
index f37d5230cf..a98adfe863 100644
--- a/server/drivers/codex.test.ts
+++ b/server/drivers/codex.test.ts
@@ -3506,14 +3506,20 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)
});
});
- it("counts an API-key login as signed in", async () => {
+ it("counts an API-key login as signed in, marked as an API key so Settings offers no ChatGPT sign-out", async () => {
await create({ mode: "api-key-configured" });
await expect(instance.snapshot()).resolves.toMatchObject({
state: "available",
authenticated: true,
+ account: { method: "api-key" },
});
});
+ it("does not mark a ChatGPT login as an API key", async () => {
+ await create({ mode: "logged-in-stdout" });
+ expect((await instance.snapshot()).account?.method).toBeUndefined();
+ });
+
it("also accepts login status from older Codex versions that used stdout", async () => {
await create({ mode: "logged-in-stdout" });
await expect(instance.snapshot()).resolves.toMatchObject({
diff --git a/server/drivers/codex.ts b/server/drivers/codex.ts
index e07da37d92..a3f4282151 100644
--- a/server/drivers/codex.ts
+++ b/server/drivers/codex.ts
@@ -57,7 +57,7 @@ import { codexAccountEmail, codexHome } from "./codex-identity.ts";
import { linkSharedCodexHome } from "./codex-shared-home.ts";
import { containsControlCharacter, mcpAppApprovalForm, mcpAppApprovalText, mcpElicitationDeclined, plainRecord } from "./codex-app-approval.ts";
import { archivedNativeCodexThread, CODEX_MISSING_CREDENTIALS, codexProviderMismatch, CODEX_SIGN_IN_EXPIRED, codexLimitFacts, codexNotSignedIn, CodexRpcError, codexSignInExpired, codexPlanModelRefused, codexSignInRefused, codexUserError, codexUserFailure, missingNativeCodexThread, type CodexRoute } from "./codex-errors.ts";
-import { codexLoginCheck, codexLoginStatus, storedCodexLogin } from "./codex-login.ts";
+import { codexLoginCheck, readCodexLogin, storedCodexLogin } from "./codex-login.ts";
import { keyRejected, noteKeyAccepted, noteKeyRejected } from "../key-rejections.ts";
import { classifyResumeFailure, mayReplay, rebuiltSessionNotice, recoveryPromptFor } from "../resume-recovery.ts";
import { extractMcpImages } from "../mcp-tool-images.ts"; import { codexImageInputs, withoutInlineImages } from "./codex-images.ts";
@@ -1545,17 +1545,17 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {
const previousCliVersion = modelsCliVersion;
modelsCliVersion = version;
if (previousCliVersion !== null && previousCliVersion !== version) await refreshModels();
- const authenticated = await codexLoginStatus(config.cli, env) === "signed-in";
+ const login = await readCodexLogin(config.cli, env), authenticated = login.status === "signed-in";
if (authenticated && loginRejected()) {
return { state: "available", version, authenticated: false, reason: CODEX_SIGN_IN_EXPIRED,
update: await codexReleaseUpdate(version, config.cli), billing: "subscription" };
}
- const email = authenticated ? await codexAccountEmail(config.cli, env) : null;
+ const email = authenticated && !login.apiKey ? await codexAccountEmail(config.cli, env) : null;
return {
state: "available",
version,
authenticated,
- ...(email ? { account: { email } } : {}),
+ ...(login.apiKey ? { account: { method: "api-key" as const } } : email ? { account: { email } } : {}), // no ChatGPT sign-out
update: await codexReleaseUpdate(version, config.cli),
billing: "subscription",
};
diff --git a/src/components/EnginesSettings.test.ts b/src/components/EnginesSettings.test.ts
index ba20942ae7..97351297e0 100644
--- a/src/components/EnginesSettings.test.ts
+++ b/src/components/EnginesSettings.test.ts
@@ -48,6 +48,16 @@ describe("Settings → Engines → ChatGPT accounts", () => {
expect(render(true)).toContain(">Signed in<");
});
+ it("names a Codex API-key login as an API key and offers no ChatGPT sign-out it cannot confirm", () => {
+ render(true, { signOut: true });
+ const keyed = fixture.instances[0]!;
+ keyed.snapshot.account = { method: "api-key" };
+ const html = renderToStaticMarkup(createElement(EnginesSettings));
+ expect(html).toContain("Uses an API key");
+ expect(html).not.toContain(">Signed in<");
+ expect(accountActions("chatgpt", keyed).signOut).toBe(false);
+ });
+
it("offers sign-out only for a signed-in account the server can sign out, and removal only for added accounts", () => {
const account = (instanceId: string, authenticated: boolean, signOut: boolean): InstanceInfo => ({
instanceId, driverKind: "codex", displayName: "Plan", snapshot: { state: "available", authenticated, chatgptPlan: true },