Skip to content

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

fix(cli-version): fall back to a known version only for the same CLI copy

AdityaUmale9322c58520e6
diff --git a/server/cli-version-probe.test.ts b/server/cli-version-probe.test.ts
index ca0bab95db..9025263928 100644
--- a/server/cli-version-probe.test.ts
+++ b/server/cli-version-probe.test.ts
@@ -1,3 +1,6 @@
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { delimiter, join } from "node:path";
import { afterEach, expect, it, vi } from "vitest";
import { cliVersionProbe } from "./cli-version-probe.ts";
import { cliVersionReader, type execCli } from "./procs.ts";
@@ -85,3 +88,26 @@ it("reports an unrecognized failure without claiming the CLI is missing or expos
reason: "`codex` could not report its version. Check that it starts in a terminal, then check again.",
});
});
+
+it("does not report one copy's version for another copy that PATH now finds first", async () => {
+ const root = mkdtempSync(join(tmpdir(), "omb-cli-probe-"));
+ try {
+ const [newer, older] = ["newer", "older"].map((name) => {
+ const dir = join(root, name);
+ mkdirSync(dir);
+ for (const file of ["claude", "claude.cmd"]) writeFileSync(join(dir, file), "");
+ return dir;
+ });
+ const failed = Object.assign(new Error("fixture process failure"), { code: 1 });
+ const run = vi.fn<typeof execCli>()
+ .mockImplementationOnce((_cli, _args, _options, callback) => callback(null, "2.1.321"))
+ .mockImplementationOnce((_cli, _args, _options, callback) => callback(failed, ""))
+ .mockImplementation((_cli, _args, _options, callback) => callback(null, "2.1.197"));
+ const probe = cliVersionProbe("claude", run);
+ expect(await probe({ PATH: newer })).toEqual({ version: "2.1.321" });
+ expect(await probe({ PATH: [older, newer].join(delimiter) })).toEqual({ reason: expect.stringContaining("could not report its version") });
+ expect(await probe({ PATH: [older, newer].join(delimiter) })).toEqual({ version: "2.1.197" });
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
diff --git a/server/cli-version-probe.ts b/server/cli-version-probe.ts
index 20385622e9..8f1483b4b0 100644
--- a/server/cli-version-probe.ts
+++ b/server/cli-version-probe.ts
@@ -1,11 +1,16 @@
+import { findCliCandidates } from "./env-path.ts";
import { CLI_VERSION_TIMEOUT_MS, cliVersionReader, execCli } from "./procs.ts";
-// Shares version checks and retries empty responses.
+// Shares version checks and retries empty responses. A failed check falls back to the last version only while PATH
+// still finds the same copy of the CLI, so a newly found copy is never reported with another copy's version.
export function cliVersionProbe(cli: string, run: typeof execCli = execCli) {
- const read = cliVersionReader(cli);
+ let read = cliVersionReader(cli), readAt: string | undefined;
let pending: { path: string | undefined; probe: Promise<ReturnType<typeof read>> } | undefined; // shared only by callers with the same PATH
return (env: NodeJS.ProcessEnv): Promise<ReturnType<typeof read>> => {
if (pending && pending.path === env.PATH) return pending.probe;
+ const at = findCliCandidates(cli, env.PATH ?? "")[0];
+ if (at !== readAt) [read, readAt] = [cliVersionReader(cli), at];
+ const reader = read;
const deadline = Date.now() + CLI_VERSION_TIMEOUT_MS;
const attempt = () => new Promise<{ error: Error | null; stdout: string }>((resolve) => {
run(cli, ["--version"], { timeout: Math.max(1, deadline - Date.now()), env }, (error, stdout) => resolve({ error, stdout }));
@@ -13,7 +18,7 @@ export function cliVersionProbe(cli: string, run: typeof execCli = execCli) {
const entry: NonNullable<typeof pending> = { path: env.PATH, probe: (async () => {
let result = await attempt();
if (!result.error && !result.stdout.trim() && Date.now() < deadline) result = await attempt();
- return read(result.error, result.stdout);
+ return reader(result.error, result.stdout);
})().finally(() => { if (pending === entry) pending = undefined; }) };
pending = entry;
return entry.probe;