Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(pane): run one magmux --version probe for panes that start together

Jack Rudenko378b85448d88
diff --git a/packages/cli/src/pane/pane-launch-version-probe.test.ts b/packages/cli/src/pane/pane-launch-version-probe.test.ts
new file mode 100644
index 00000000..6abd5f25
--- /dev/null
+++ b/packages/cli/src/pane/pane-launch-version-probe.test.ts
@@ -0,0 +1,232 @@
+import { afterAll, describe, expect, test } from "bun:test";
+import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import {
+ MIN_MAGMUX_VERSION,
+ MagmuxUnavailableError,
+ assertMagmuxAvailable,
+} from "./pane-launch.js";
+
+const tempDirs: string[] = [];
+const testTimeout = 4_500;
+
+function shellQuote(value: string): string {
+ return `'${value.replaceAll("'", "'\\''")}'`;
+}
+
+function fakeMagmux(body: string): { binary: string; runs: () => number } {
+ const directory = mkdtempSync(join(tmpdir(), "pane-launch-version-probe-"));
+ tempDirs.push(directory);
+ const binary = join(directory, "magmux");
+ const counter = join(directory, "runs");
+ writeFileSync(
+ binary,
+ `#!/bin/sh
+counter=${shellQuote(counter)}
+printf '%s\\n' run >> "$counter"
+if [ "$#" -ne 1 ] || [ "$1" != "--version" ]; then
+ printf '%s\\n' 'expected exactly --version' >&2
+ exit 64
+fi
+${body}
+`
+ );
+ chmodSync(binary, 0o755);
+ return {
+ binary,
+ runs: () => {
+ if (!existsSync(counter)) return 0;
+ return readFileSync(counter, "utf8").trim().split("\n").length;
+ },
+ };
+}
+
+async function expectUnavailable(binary: string, ...messageParts: string[]): Promise<void> {
+ try {
+ await assertMagmuxAvailable(binary);
+ } catch (error) {
+ expect(error).toBeInstanceOf(MagmuxUnavailableError);
+ if (!(error instanceof MagmuxUnavailableError)) {
+ throw error;
+ }
+ expect(error.code).toBe("magmux_unavailable");
+ for (const part of messageParts) {
+ expect(error.message).toContain(part);
+ }
+ return;
+ }
+ throw new Error("Expected assertMagmuxAvailable to reject");
+}
+
+afterAll(() => {
+ for (const directory of tempDirs) {
+ rmSync(directory, { recursive: true, force: true });
+ }
+});
+
+describe("magmux version probe", () => {
+ test(
+ "B1: six overlapping callers share exactly one probe",
+ async () => {
+ const fake = fakeMagmux(`sleep 0.3
+printf '%s\\n' 'magmux 0.17.0 (test-build)'`);
+
+ const results = await Promise.all(
+ Array.from({ length: 6 }, () => assertMagmuxAvailable(fake.binary))
+ );
+
+ expect(results).toHaveLength(6);
+ for (const result of results) {
+ expect(result).toEqual({ binary: fake.binary, version: "0.17.0" });
+ }
+ expect(fake.runs()).toBe(1);
+ },
+ testTimeout
+ );
+
+ test(
+ "B2: accepts a version printed only on stderr",
+ async () => {
+ const fake = fakeMagmux("printf '%s\\n' 'magmux 0.17.0' >&2");
+
+ expect(await assertMagmuxAvailable(fake.binary)).toEqual({
+ binary: fake.binary,
+ version: "0.17.0",
+ });
+ expect(fake.runs()).toBe(1);
+ },
+ testTimeout
+ );
+
+ test(
+ "B3: retries once when a successful first run prints nothing",
+ async () => {
+ const fake = fakeMagmux(`if [ "$(wc -l < "$counter")" -eq 1 ]; then
+ exit 0
+fi
+printf '%s\\n' 'magmux 0.17.0'`);
+
+ expect(await assertMagmuxAvailable(fake.binary)).toEqual({
+ binary: fake.binary,
+ version: "0.17.0",
+ });
+ expect(fake.runs()).toBe(2);
+ },
+ testTimeout
+ );
+
+ test(
+ "B4: rejects an unknown version after exactly two runs",
+ async () => {
+ const fake = fakeMagmux(`printf '%s\\n' 'magmux development build'
+printf '%s\\n' 'version metadata unavailable' >&2`);
+
+ await expectUnavailable(fake.binary, "is version unknown", ">= 0.17.0");
+ expect(fake.runs()).toBe(2);
+ },
+ testTimeout
+ );
+
+ test(
+ "B5: rejects a version below the floor without retrying",
+ async () => {
+ const fake = fakeMagmux("printf '%s\\n' 'magmux 0.16.0'");
+
+ expect(MIN_MAGMUX_VERSION).toBe("0.17.0");
+ await expectUnavailable(fake.binary, "is version 0.16.0", ">= 0.17.0");
+ expect(fake.runs()).toBe(1);
+ },
+ testTimeout
+ );
+
+ test(
+ "B6: a rejected probe is not cached for the next call",
+ async () => {
+ const fake = fakeMagmux(`if [ "$(wc -l < "$counter")" -eq 1 ]; then
+ printf '%s\\n' 'magmux 0.16.0'
+else
+ printf '%s\\n' 'magmux 0.17.0'
+fi`);
+
+ await expectUnavailable(fake.binary, "is version 0.16.0");
+ expect(fake.runs()).toBe(1);
+
+ expect(await assertMagmuxAvailable(fake.binary)).toEqual({
+ binary: fake.binary,
+ version: "0.17.0",
+ });
+ expect(fake.runs()).toBe(2);
+ },
+ testTimeout
+ );
+
+ test(
+ "B7: a successful probe is cached for subsequent calls",
+ async () => {
+ const fake = fakeMagmux("printf '%s\\n' 'magmux 0.17.0'");
+ const expected = { binary: fake.binary, version: "0.17.0" };
+
+ expect(await assertMagmuxAvailable(fake.binary)).toEqual(expected);
+ expect(fake.runs()).toBe(1);
+
+ for (let index = 0; index < 3; index++) {
+ expect(await assertMagmuxAvailable(fake.binary)).toEqual(expected);
+ expect(fake.runs()).toBe(1);
+ }
+ },
+ testTimeout
+ );
+
+ test(
+ "B8: a non-zero exit rejects with a version-command failure",
+ async () => {
+ const fake = fakeMagmux(`printf '%s\\n' 'magmux probe failed' >&2
+exit 7`);
+
+ await expectUnavailable(fake.binary, "--version failed");
+ expect(fake.runs()).toBeGreaterThanOrEqual(1);
+ },
+ testTimeout
+ );
+
+ test(
+ "B8: a non-executable binary rejects with a version-command failure",
+ async () => {
+ const fake = fakeMagmux("printf '%s\\n' 'magmux 0.17.0'");
+ chmodSync(fake.binary, 0o644);
+
+ await expectUnavailable(fake.binary, "--version failed");
+ expect(fake.runs()).toBe(0);
+ },
+ testTimeout
+ );
+
+ test(
+ "B9: accepts a release candidate and returns only its version triple",
+ async () => {
+ const fake = fakeMagmux("printf '%s\\n' 'magmux 0.17.0-rc1'");
+
+ expect(await assertMagmuxAvailable(fake.binary)).toEqual({
+ binary: fake.binary,
+ version: "0.17.0",
+ });
+ expect(fake.runs()).toBe(1);
+ },
+ testTimeout
+ );
+
+ test(
+ "B9: accepts a newer version",
+ async () => {
+ const fake = fakeMagmux("printf '%s\\n' 'magmux 0.18.2'");
+
+ expect(await assertMagmuxAvailable(fake.binary)).toEqual({
+ binary: fake.binary,
+ version: "0.18.2",
+ });
+ expect(fake.runs()).toBe(1);
+ },
+ testTimeout
+ );
+});
diff --git a/packages/cli/src/pane/pane-launch.ts b/packages/cli/src/pane/pane-launch.ts
index 42b8aa1e..bd252fd1 100644
--- a/packages/cli/src/pane/pane-launch.ts
+++ b/packages/cli/src/pane/pane-launch.ts
@@ -512,38 +512,66 @@ export function versionAtLeast(v: string, min: string): boolean {
return true;
}
-const magmuxCache = new Map<string, { binary: string; version: string }>();
+type MagmuxBinary = { binary: string; version: string };
+
+/** Successes only: a failed probe is never cached, so the next caller probes again. */
+const magmuxCache = new Map<string, MagmuxBinary>();
+/** The probe in flight per key: six slots starting at once run `--version` once, not six times. */
+const magmuxProbes = new Map<string, Promise<MagmuxBinary>>();
const execFileAsync = promisify(execFile);
/** The magmux binary and its version (≥ MIN_MAGMUX_VERSION), cached per process. Never a `-p` fallback. */
-export async function assertMagmuxAvailable(
- binary?: string
-): Promise<{ binary: string; version: string }> {
- const cached = magmuxCache.get(binary ?? "");
+export async function assertMagmuxAvailable(binary?: string): Promise<MagmuxBinary> {
+ const key = binary ?? "";
+ const cached = magmuxCache.get(key);
if (cached) return cached;
+ const inFlight = magmuxProbes.get(key);
+ if (inFlight) return inFlight;
+ const probe = (async () => {
+ try {
+ const result = await probeMagmux(binary);
+ magmuxCache.set(key, result);
+ return result;
+ } finally {
+ magmuxProbes.delete(key);
+ }
+ })();
+ magmuxProbes.set(key, probe);
+ return probe;
+}
+
+async function probeMagmux(binary?: string): Promise<MagmuxBinary> {
const found = binary ?? findMagmuxBinaryOrNull();
if (!found)
throw new MagmuxUnavailableError(
"magmux_unavailable: magmux not found (bundled package or PATH); install it: brew install MadAppGang/tap/magmux"
);
+ let version = await readMagmuxVersion(found);
+ // Under load a run has exited 0 with no version in its output, and six concurrent probes
+ // refused a good 0.17.0 as "version unknown". Ask once more before refusing; a real version
+ // below the floor is an answer and is not retried.
+ if (!version) version = await readMagmuxVersion(found);
+ if (!version || !versionAtLeast(version, MIN_MAGMUX_VERSION))
+ throw new MagmuxUnavailableError(
+ `magmux_unavailable: ${found} is version ${version || "unknown"}; panes need >= ${MIN_MAGMUX_VERSION} (the Claude Code input gate bound to a named transcript); upgrade: brew upgrade MadAppGang/tap/magmux`
+ );
+ return { binary: found, version };
+}
+
+/** The `X.Y.Z` from one `<binary> --version` run, read from stdout and stderr together; "" when absent. */
+async function readMagmuxVersion(found: string): Promise<string> {
let out = "";
try {
// async: the first create_session or team run must not stall the MCP server's event
// loop for up to 5 s (every other session's frames and socket share it)
- out = (await execFileAsync(found, ["--version"], { encoding: "utf8", timeout: 5000 })).stdout;
+ const r = await execFileAsync(found, ["--version"], { encoding: "utf8", timeout: 5000 });
+ out = `${r.stdout}\n${r.stderr}`;
} catch (e) {
throw new MagmuxUnavailableError(
`magmux_unavailable: ${found} --version failed: ${e instanceof Error ? e.message : String(e)}`
);
}
- const version = out.match(/(\d+\.\d+\.\d+)/)?.[1] ?? "";
- if (!version || !versionAtLeast(version, MIN_MAGMUX_VERSION))
- throw new MagmuxUnavailableError(
- `magmux_unavailable: ${found} is version ${version || "unknown"}; panes need >= ${MIN_MAGMUX_VERSION} (the Claude Code input gate bound to a named transcript); upgrade: brew upgrade MadAppGang/tap/magmux`
- );
- const result = { binary: found, version };
- magmuxCache.set(binary ?? "", result);
- return result;
+ return out.match(/(\d+\.\d+\.\d+)/)?.[1] ?? "";
}
/* ───────────────────────────── launcher ───────────────────────────── */