Skip to content

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

fix(codex): count Codex 0.165's API-key login as signed in

CikeSeven410278389dd3
diff --git a/server/drivers/codex-device-auth.test.ts b/server/drivers/codex-device-auth.test.ts
index 421b5afbd3..f151b41015 100644
--- a/server/drivers/codex-device-auth.test.ts
+++ b/server/drivers/codex-device-auth.test.ts
@@ -23,6 +23,8 @@ if (args.join(' ') === 'login status') {
process.stderr.write('Logged in using ChatGPT\\n'); process.exit(0);
} else if (mode === 'api') {
process.stderr.write('Logged in using an API key - sk-private-token\\n'); process.exit(0);
+ } else if (mode === 'api-configured') {
+ process.stderr.write('API key configured - sk-private-token\\nAuthentication is checked when you send a request.\\n'); process.exit(0);
} else if (mode === 'unknown-status') {
process.stderr.write('Unrecognized login status secret-token\\n'); process.exit(0);
} else { process.stderr.write('Not logged in\\n'); process.exit(1); }
@@ -249,6 +251,11 @@ describe("Codex server-owned device authentication", () => {
expect(calls()).toHaveLength(1);
});
+ it("does not replace an API key that newer Codex reports as configured", async () => {
+ await expect(create("api-configured").start()).rejects.toThrow("different sign-in method");
+ expect(calls()).toHaveLength(1);
+ });
+
it("fails closed when login status cannot establish the existing account", async () => {
await expect(create("unknown-status").start()).rejects.toThrow("could not confirm the existing sign-in");
expect(calls()).toHaveLength(1);
diff --git a/server/drivers/codex-device-auth.ts b/server/drivers/codex-device-auth.ts
index a74f061f13..5944649d73 100644
--- a/server/drivers/codex-device-auth.ts
+++ b/server/drivers/codex-device-auth.ts
@@ -2,6 +2,7 @@
import { stripVTControlCharacters } from "node:util";
import { deviceSignInPrompt } from "../../shared/device-sign-in.ts";
import { DeviceAuthController, type DeviceAuthOptions, type DeviceSignIn } from "./device-auth.ts";
+import { codexReportsLogin } from "./codex-login.ts";
export function codexDevicePrompt(output: string): { authorizationUrl: string; userCode: string } | null {
return deviceSignInPrompt("codex", stripVTControlCharacters(output));
@@ -33,7 +34,7 @@ export const CODEX_DEVICE_SIGN_IN: DeviceSignIn = {
args: ["login", "status"],
read: (code, output) =>
code === 0 && /^logged in.*chatgpt\b/im.test(output) ? "signed-in"
- : code === 0 && /^logged in\b/im.test(output) ? "other"
+ : code === 0 && codexReportsLogin(output) ? "other"
: code !== 0 && /^not logged in\b/im.test(output) ? "signed-out"
: "unknown",
},
diff --git a/server/drivers/codex-login.ts b/server/drivers/codex-login.ts
index 7c3711f4cd..05ae3b29a6 100644
--- a/server/drivers/codex-login.ts
+++ b/server/drivers/codex-login.ts
@@ -18,11 +18,16 @@ const readable = (stored: string | null): boolean => {
try { JSON.parse(stored); return true; } catch { return false; }
};
-/** codex-cli 0.160.1 says "Logged in using …", "Not logged in", or "Error checking login status: …" for an auth.json it cannot read. */
+/** codex-cli 0.160.1 says "Logged in using …", "Not logged in", or "Error checking login status: …" for an auth.json it
+ * cannot read; 0.165.0 says "API key configured - …" for an API-key login. */
+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"> {
return new Promise((resolve) => execCli(cli, ["login", "status"], { timeout: 8000, env }, (err, stdout, stderr) => {
const said = `${stdout}\n${stderr ?? ""}`;
- resolve(!err && /^logged in\b/im.test(said) ? "signed-in"
+ resolve(!err && codexReportsLogin(said) ? "signed-in"
: err && /^(?:not logged in|error checking login status)\b/im.test(said) ? "signed-out" : "unknown");
}));
}
diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.ts
index 9fe548db79..5613b20d6c 100644
--- a/server/drivers/codex.test.ts
+++ b/server/drivers/codex.test.ts
@@ -2988,6 +2988,14 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)
});
});
+ it("counts an API-key login as signed in", async () => {
+ await create({ mode: "api-key-configured" });
+ await expect(instance.snapshot()).resolves.toMatchObject({
+ state: "available",
+ authenticated: true,
+ });
+ });
+
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/testing/fake-codex-app-server.ts b/server/testing/fake-codex-app-server.ts
index 41929842c9..96a0a645bc 100755
--- a/server/testing/fake-codex-app-server.ts
+++ b/server/testing/fake-codex-app-server.ts
@@ -97,6 +97,10 @@ if (process.argv[2] === "login" && process.argv[3] === "status") {
process.stderr.write("Not logged in\n");
process.exit(1);
}
+ if (mode === "api-key-configured") {
+ process.stderr.write("API key configured - sk-fake-****\nAuthentication is checked when you send a request.\n");
+ process.exit(0);
+ }
// Codex 0.147.0 reports a successful login on stderr; older versions wrote it on stdout.
const statusStream = mode === "logged-in-stdout" ? process.stdout : process.stderr;
statusStream.write("Logged in using ChatGPT\n");