Skip to content

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

fix(claude): wait for a running help read and keep version probes per PATH

AdityaUmale19f664b314a4
diff --git a/server/cli-version-probe.test.ts b/server/cli-version-probe.test.ts
index f6c8faf951..ca0bab95db 100644
--- a/server/cli-version-probe.test.ts
+++ b/server/cli-version-probe.test.ts
@@ -18,6 +18,24 @@ it("shares overlapping slow probes and accepts a version after the old eight-sec
expect(run).toHaveBeenCalledTimes(1);
});
+it("never hands a caller the version of a probe started with a different PATH", async () => {
+ vi.useFakeTimers();
+ const run = vi.fn<typeof execCli>((_cli, _args, options, callback) => {
+ const version = options.env?.PATH === "nvm" ? "2.1.197" : "2.1.321";
+ setTimeout(() => callback(null, version), options.env?.PATH === "nvm" ? 1_000 : 2_000);
+ });
+ const probe = cliVersionProbe("claude", run);
+ const homebrew = probe({ PATH: "homebrew" });
+ const nvm = probe({ PATH: "nvm" });
+ expect(nvm).not.toBe(homebrew);
+ expect(probe({ PATH: "nvm" })).toBe(nvm);
+ await vi.advanceTimersByTimeAsync(1_000);
+ expect(await nvm).toEqual({ version: "2.1.197" });
+ await vi.advanceTimersByTimeAsync(1_000);
+ expect(await homebrew).toEqual({ version: "2.1.321" });
+ expect(run).toHaveBeenCalledTimes(2);
+});
+
it("retries an empty success once within the original thirty-second budget", async () => {
vi.useFakeTimers();
const run = vi.fn<typeof execCli>()
diff --git a/server/cli-version-probe.ts b/server/cli-version-probe.ts
index 40b5faee73..20385622e9 100644
--- a/server/cli-version-probe.ts
+++ b/server/cli-version-probe.ts
@@ -3,18 +3,19 @@ import { CLI_VERSION_TIMEOUT_MS, cliVersionReader, execCli } from "./procs.ts";
// Shares version checks and retries empty responses.
export function cliVersionProbe(cli: string, run: typeof execCli = execCli) {
const read = cliVersionReader(cli);
- let pending: Promise<ReturnType<typeof read>> | 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) return pending;
+ if (pending && pending.path === env.PATH) return pending.probe;
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 }));
});
- pending = (async () => {
+ 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);
- })().finally(() => { pending = undefined; });
- return pending;
+ })().finally(() => { if (pending === entry) pending = undefined; }) };
+ pending = entry;
+ return entry.probe;
};
}
diff --git a/server/drivers/claude.ts b/server/drivers/claude.ts
index a59dbb15f2..a8fc1bfa9b 100644
--- a/server/drivers/claude.ts
+++ b/server/drivers/claude.ts
@@ -806,7 +806,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeConfig> = {
const readCliVersion = cliVersionProbe(config.cli);
const ensureCliVersion = async (env: NodeJS.ProcessEnv): Promise<void> => { // and again once PATH finds another copy (an nvm one ahead of Homebrew's)
const at = findCliCandidates(config.cli, env.PATH ?? "")[0], reprobed = cliVersionChecked;
- if (reprobed && at === cliProbedAt) return;
+ if (reprobed && at === cliProbedAt) { await helpRead?.done; return; } // a help read for this copy may still be running
const probe = await readCliVersion(env);
if ("version" in probe) [cliVersion, cliVersionChecked, cliProbedAt] = [parseClaudeCliVersion(probe.version), true, at];
if (reprobed && "version" in probe && probe.version !== cliHelpVersion) await readCliHelp(probe.version, env);