Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(codex): read the model catalog once more before falling back to the static list (#7637)
Ooi Pin Jun521d09ac551d
diff --git a/server/drivers/codex-catalog.test.ts b/server/drivers/codex-catalog.test.tsindex 5244ea792a..33e46e55de 100644--- a/server/drivers/codex-catalog.test.ts+++ b/server/drivers/codex-catalog.test.ts@@ -1,4 +1,4 @@-import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { dirname, isAbsolute, join, relative } from "node:path";import { fileURLToPath } from "node:url";@@ -102,6 +102,40 @@ describe("readCodexModelCatalog", () => {});});+ it("reads the app-server catalog again when the first listing fails", async () => {+ chmodSync(FAKE_CLI, 0o755);+ const home = scratchHome({});+ const attempts = join(home, "catalog-attempts");+ const catalog = await readCodexModelCatalog(+ { HOME: home, PATH: process.env.PATH, FAKE_CODEX_CATALOG_FAILURES: "1", FAKE_CODEX_STATE: attempts },+ fetch,+ FAKE_CLI,+ );++ expect(catalog).toEqual({+ default: "gpt-fake-default",+ options: [+ { id: "gpt-fake-default", label: "GPT Fake Default" },+ { id: "gpt-page-two", label: "GPT Page Two" },+ ],+ });+ expect(readFileSync(attempts, "utf8")).toBe("2");+ });++ it("falls back to the static catalog when the second listing also fails", async () => {+ chmodSync(FAKE_CLI, 0o755);+ const home = scratchHome({});+ const attempts = join(home, "catalog-attempts");+ const catalog = await readCodexModelCatalog(+ { HOME: home, PATH: process.env.PATH, FAKE_CODEX_CATALOG_FAILURES: "2", FAKE_CODEX_STATE: attempts },+ fetch,+ FAKE_CLI,+ );++ expect(catalog).toEqual(STATIC_CODEX_MODELS);+ expect(readFileSync(attempts, "utf8")).toBe("2");+ });+it("sends a model_catalog_json listing to the provider that serves it", async () => {chmodSync(FAKE_CLI, 0o755);const home = scratchHome({diff --git a/server/drivers/codex-catalog.ts b/server/drivers/codex-catalog.tsindex 44840e0854..a0291bc717 100644--- a/server/drivers/codex-catalog.ts+++ b/server/drivers/codex-catalog.ts@@ -422,7 +422,8 @@ export async function readCodexModelCatalog(fetchImpl: typeof fetch = fetch,cli?: string,): Promise<ModelCatalog> {- const official = cli ? await readCodexAppServerModelCatalog(cli, env) : null;+ let official = cli ? await readCodexAppServerModelCatalog(cli, env) : null;+ if (cli && official === null) official = await readCodexAppServerModelCatalog(cli, env);const catalog = await catalogAround(official ?? STATIC_CODEX_MODELS, official !== null, env, fetchImpl);if (cli && !official) unconfirmedModelCatalog(catalog);return catalog;diff --git a/server/testing/fake-codex-app-server.ts b/server/testing/fake-codex-app-server.tsindex 23b8d86d00..cd096755b7 100755--- a/server/testing/fake-codex-app-server.ts+++ b/server/testing/fake-codex-app-server.ts@@ -59,6 +59,7 @@// FAKE_CODEX_TEXT_MODE `codex exec`: ok (default) | fail | exit | hang | tool | noisy// FAKE_CODEX_TEXT_REFUSE `codex exec`: comma-separated models answered with the 400 a ChatGPT account gets for gpt-5.4-mini// FAKE_CODEX_TEXT_BLOCK `codex exec`: comma-separated models answered with the 403 OpenAI sends a region it does not serve+// FAKE_CODEX_CATALOG_FAILURES the first N null-cursor model/list requests fail (count in FAKE_CODEX_STATE)// FAKE_CODEX_LUNA "1": model/list also offers gpt-6-luna// FAKE_CODEX_TIERS "1": model/list rows list a Fast service tier, as codex-cli 0.167.2 does; gpt-page-two// already runs on it by default. Set FAKE_CODEX_ULTRAFAST="1" alongside it to also include Ultrafast.@@ -659,7 +660,19 @@ process.stdin.on("data", (chunk) => {out({ jsonrpc: "2.0", id: msg.id, result: { account, requiresOpenaiAuth: true } });break;}- case "model/list":+ case "model/list": {+ if ((msg.params?.cursor ?? null) === null && process.env.FAKE_CODEX_CATALOG_FAILURES && process.env.FAKE_CODEX_STATE) {+ let listed = 0;+ try {+ listed = Number(readFileSync(process.env.FAKE_CODEX_STATE, "utf8")) || 0;+ } catch {}+ const quota = Number(process.env.FAKE_CODEX_CATALOG_FAILURES) || 0;+ writeFileSync(process.env.FAKE_CODEX_STATE, String(listed + 1));+ if (listed < quota) {+ out({ jsonrpc: "2.0", id: msg.id, error: { code: -32603, message: "model catalog unavailable" } });+ break;+ }+ }if (msg.params?.cursor === "page-2") {out({jsonrpc: "2.0",@@ -699,6 +712,7 @@ process.stdin.on("data", (chunk) => {});}break;+ }case "config/read":logStartup("config/read");if (process.env.FAKE_CODEX_CONFIG_HOLD_FILE && existsSync(process.env.FAKE_CODEX_CONFIG_HOLD_FILE) && !msg.released) {