Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(codex): ask Codex about its login again after a turn finds it gone
CikeSeven150c423fd9a8
diff --git a/server/drivers/codex-login.ts b/server/drivers/codex-login.tsindex 05ae3b29a6..df169baf2b 100644--- a/server/drivers/codex-login.ts+++ b/server/drivers/codex-login.ts@@ -33,15 +33,19 @@ export function codexLoginStatus(cli: string, env: Env): Promise<"signed-in" | "}/** 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. */-export function codexLoginCheck(cli: string): (env: Env) => Promise<"missing" | "present"> {+ * 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. */+export function codexLoginCheck(cli: string): { check: (env: Env) => Promise<"missing" | "present">; forget: () => void } {let confirmedWithout: string | null | undefined;- return async (env) => {- const stored = storedCodexLogin(env);- if (readable(stored) || stored === confirmedWithout) return "present";- const status = await codexLoginStatus(cli, env);- if (status === "signed-in") confirmedWithout = stored;- // Another Codex may be rewriting auth.json: a half-written file is not a missing login.- return status === "signed-out" && !readable(storedCodexLogin(env)) ? "missing" : "present";+ return {+ check: async (env) => {+ const stored = storedCodexLogin(env);+ if (readable(stored) || stored === confirmedWithout) return "present";+ const status = await codexLoginStatus(cli, env);+ if (status === "signed-in") confirmedWithout = stored;+ // Another Codex may be rewriting auth.json: a half-written file is not a missing login.+ return status === "signed-out" && !readable(storedCodexLogin(env)) ? "missing" : "present";+ },+ forget: () => { confirmedWithout = undefined; },};}diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.tsindex 5613b20d6c..95a094fba4 100644--- a/server/drivers/codex.test.ts+++ b/server/drivers/codex.test.ts@@ -3266,6 +3266,19 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)expect(recorder.events.some((e) => e.type === "turn.retrying")).toBe(false);});+ it("asks Codex about its login again once a turn ran without the login Codex reported", async () => {+ const codexHome = join(scratch, ".codex");+ mkdirSync(codexHome, { recursive: true });+ await create({ mode: "api-key-configured", environment: { HOME: scratch, USERPROFILE: scratch, CODEX_HOME: codexHome, FAKE_CODEX_MISSING_BEARER: "1", FAKE_CODEX_STATE: join(scratch, "launches") } });+ await expect(runTurn("t-login-gone")).resolves.toMatchObject({ errors: [expect.objectContaining({ message: codexNotSignedIn() })] });+ process.env.FAKE_CODEX_MODE = "logged-out";+ const spawn = vi.spyOn(procs, "spawnCli");+ const { done, errors } = await runTurn("t-after-login-gone");+ expect(errors).toEqual([expect.objectContaining({ message: codexNotSignedIn(), setup: true })]);+ expect(done).toMatchObject({ ok: false, stopReason: "auth_required" });+ expect(spawn).not.toHaveBeenCalled();+ });+it("runs the turn again once when Codex started without the login it has now", async () => {vi.stubEnv("FAKE_CODEX_RETRY_SCALE", "0.001");await create({ environment: { ...signedIn(), FAKE_CODEX_MISSING_BEARER: "1", FAKE_CODEX_STATE: join(scratch, "launches") } });diff --git a/server/drivers/codex.ts b/server/drivers/codex.tsindex 475853cc21..ad1c391c75 100644--- a/server/drivers/codex.ts+++ b/server/drivers/codex.ts@@ -638,7 +638,7 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {const chatgptTurn = ownLogin &&decodeCodexSelection(turn.model || models.default).modelProvider === OFFICIAL_CODEX_PROVIDER;const route: CodexRoute = plan ? "plan" : chatgptTurn ? "own" : "other";- const signedOut = chatgptTurn && await loginCheck(childEnv()) === "missing";+ const signedOut = chatgptTurn && await loginCheck.check(childEnv()) === "missing";assertPlanCurrent();if (active.has(threadId)) throw new Error("a turn is already running on this thread");const turnId = newId();@@ -1120,7 +1120,7 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {const refused = chatgptTurn && loginMark() !== null && (codexSignInRefused(error) ||(state.authRecovery && !/\b403\b|forbidden/i.test(raw) && /\b401\b|unauthorized/i.test(raw)));if (retryLoginRace(raw, refused)) return;- if (refused) loginRefused();+ if (refused) loginRefused(); else if (chatgptTurn && CODEX_MISSING_CREDENTIALS.test(raw)) loginCheck.forget();const message = refused ? codexSignInExpired(raw) : codexUserError(raw, route, error.codexErrorInfo);const setup = refused || classifyError({ text: raw }).reason === "auth";if (message === state.lastError && (state.lastErrorSetup || !setup)) return;