Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(codex): update the app's own Codex copy, not npm's global one
AdityaUmaledb45afce6d55
diff --git a/server/drivers/codex-update.test.ts b/server/drivers/codex-update.test.tsindex 2a9cab90de..54033f5cc8 100644--- a/server/drivers/codex-update.test.ts+++ b/server/drivers/codex-update.test.ts@@ -1,5 +1,9 @@+import { mkdirSync, rmSync, writeFileSync } from "node:fs";+import { join } from "node:path";import { afterEach, expect, it, vi } from "vitest";-import { codexReleaseUpdate, codexUpdateCommand } from "./codex-update.ts";+import { enginesBinDir, enginesPrefix } from "../engine-install.ts";+import { registerPathDir, unregisterPathDir } from "../env-path.ts";+import { codexAppCopyUpdateCommand, codexReleaseUpdate, codexUpdateCommand } from "./codex-update.ts";const releases = vi.hoisted(() => ({ latest: "0.169.0" as string | null, pinned: "0.167.0" as string | undefined }));vi.mock("./codex-release.ts", async (original) => ({@@ -43,3 +47,30 @@ it("leaves an unpinned installer eligible and ignores an unavailable release looreleases.latest = null;expect(await codexReleaseUpdate("codex-cli 0.167.0", "codex", false)).toBeUndefined();});++it("updates the app's own npm copy in the app's folder, not npm's global copy that `codex update` installs into", async () => {+ expect((await codexReleaseUpdate("codex-cli 0.167.0", "codex", false))?.command).toBe("codex update");+ const bin = enginesBinDir();+ const shim = join(bin, process.platform === "win32" ? "codex.cmd" : "codex");+ mkdirSync(bin, { recursive: true });+ writeFileSync(shim, "", { mode: 0o755 });+ registerPathDir(bin);+ try {+ const update = await codexReleaseUpdate("codex-cli 0.167.0", "codex", false);+ expect(update).toMatchObject({ title: "Update Codex to 0.169.0", serverInstall: false, command: codexAppCopyUpdateCommand("0.169.0") });+ expect(update?.command).toContain(enginesPrefix());+ expect(update?.command).toContain("@openai/[email protected]");+ } finally {+ unregisterPathDir(bin);+ rmSync(shim, { force: true });+ }+});++it("quotes the app's folder and package for the shell that runs the copied command", () => {+ expect(codexAppCopyUpdateCommand("0.172.0", "C:\\Users\\O'Neil\\AppData\\Roaming\\OpenMausBot\\tools\\npm", "win32")).toBe(+ "npm install -g --prefix 'C:\\Users\\O''Neil\\AppData\\Roaming\\OpenMausBot\\tools\\npm' --loglevel=error --include=optional '--allow-scripts=@openai/codex' '@openai/[email protected]'",+ );+ expect(codexAppCopyUpdateCommand("0.172.0", "/home/o'neil/My Data/tools/npm", "linux")).toBe(+ "npm install -g --prefix '/home/o'\\''neil/My Data/tools/npm' --loglevel=error --include=optional '--allow-scripts=@openai/codex' '@openai/[email protected]'",+ );+});diff --git a/server/drivers/codex-update.ts b/server/drivers/codex-update.tsindex ba5c62a261..22ab39c202 100644--- a/server/drivers/codex-update.ts+++ b/server/drivers/codex-update.ts@@ -1,11 +1,19 @@// What the engine card offers when a newer stable Codex CLI is out: the update command for the configured executable.import { existsSync } from "node:fs";+import { dirname } from "node:path";import { cloudHomeConfigured } from "../cloud-home.ts";import type { ProviderSnapshot } from "../contracts.ts";-import { splitCliString } from "../env-path.ts";+import { enginesBinDir, enginesPrefix, npmInstallArgs } from "../engine-install.ts";+import { findCliCandidates, splitCliString } from "../env-path.ts";import { pinnedNpmVersion } from "../engine-versions.ts";import { codexVersionBehind, readLatestCodexRelease } from "./codex-release.ts";+const CODEX_NPM_PACKAGE = "@openai/codex";++function shellQuote(token: string, platform: NodeJS.Platform): string {+ return platform === "win32" ? `'${token.replaceAll("'", "''")}'` : `'${token.replaceAll("'", `'\\''`)}'`;+}+/** Ask the configured executable to update itself, so a non-PATH Codex is the one updated. */export function codexUpdateCommand(cli: string, platform: NodeJS.Platform = process.platform): string {if (cli === "codex") return "codex update";@@ -13,13 +21,23 @@ export function codexUpdateCommand(cli: string, platform: NodeJS.Platform = procconst tokens = trimmed.includes(" ") && existsSync(trimmed)? [trimmed]: splitCliString(trimmed);- const quote = platform === "win32"- ? (token: string) => `'${token.replaceAll("'", "''")}'`- : (token: string) => `'${token.replaceAll("'", `'\\''`)}'`;- const command = (tokens.length > 0 ? tokens : [trimmed]).map(quote).join(" ");+ const command = (tokens.length > 0 ? tokens : [trimmed]).map((token) => shellQuote(token, platform)).join(" ");return platform === "win32" ? `& ${command} update` : `${command} update`;}+/** The install the app runs for `codex` from its own npm folder (found ahead of the person's PATH): `codex update` would+ * update npm's global copy instead, so the command installs the release into the app's folder. */+export function codexAppCopyUpdateCommand(version: string, prefix = enginesPrefix(), platform: NodeJS.Platform = process.platform): string {+ const args = npmInstallArgs(prefix, CODEX_NPM_PACKAGE, version);+ return ["npm", ...args.map((arg) => /^[\w.=-]+$/u.test(arg) ? arg : shellQuote(arg, platform))].join(" ");+}++function runsAppCopy(cli: string): boolean {+ if (cli !== "codex") return false;+ const found = findCliCandidates(cli)[0];+ return found !== undefined && dirname(found) === enginesBinDir();+}+export async function codexReleaseUpdate(version: string, cli: string, onCloud = cloudHomeConfigured()): Promise<ProviderSnapshot["update"] | undefined> {const latest = await readLatestCodexRelease();if (!latest || !codexVersionBehind(version, latest)) return undefined;@@ -32,7 +50,7 @@ export async function codexReleaseUpdate(version: string, cli: string, onCloud =message: needsTerminal? `A newer stable Codex CLI is available (installed: ${version}). The app installer supplies tested version ${pinned}. To install ${latest}, use the terminal command below, then refresh models. Model availability also depends on your signed-in account.`: `A newer stable Codex CLI is available (installed: ${version}). Update it, then refresh models. Model availability also depends on your signed-in account.`,- command: codexUpdateCommand(cli),+ command: runsAppCopy(cli) ? codexAppCopyUpdateCommand(latest) : codexUpdateCommand(cli),...(needsTerminal ? { serverInstall: false as const } : {}),};}