Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(claude): tie each turn's version flags to the copy it launches
AdityaUmaleace99d74401b
diff --git a/server/drivers/claude.test.ts b/server/drivers/claude.test.tsindex 387f6fca4d..32eed0e848 100644--- a/server/drivers/claude.test.ts+++ b/server/drivers/claude.test.ts@@ -2055,6 +2055,32 @@ describe("ClaudeDriver turns (fake CLI)", () => {resetPathCacheForTests();}});+ it.skipIf(process.platform === "win32")("launches a turn with its own copy's flags when another copy's late version check lands while it waits", async () => {+ // A turn checks 2.1.197 and waits for its --help; snapshot()'s late --version answer from 2.1.321 lands meanwhile and+ // writes 2.1.321's version. The turn must not launch 2.1.197 with 2.1.321's --system-prompt-snapshot.+ const slow = join(scratch, "newer-race-slow"), versionAsked = join(scratch, "newer-race-version"), helpAsked = join(scratch, "older-race-help");+ const newer = join(scratch, "newer-race"), older = join(scratch, "older-race");+ mkdirSync(newer); mkdirSync(older);+ writeFileSync(join(newer, "claude"), `#!/bin/sh\nif [ -e "${slow}" ] && [ "$1" = --version ]; then : > "${versionAsked}"; sleep 1; fi\nFAKE_CLAUDE_VERSION=2.1.321 exec "${FAKE_CLI}" "$@"\n`, { mode: 0o755 });+ // The older copy's first --help is slow, and it refuses --system-prompt-snapshot like 2.1.197 does.+ writeFileSync(join(older, "claude"), `#!/bin/sh\nif [ "$1" = --help ] && [ ! -e "${helpAsked}" ]; then : > "${helpAsked}"; sleep 2; fi\nfor a in "$@"; do [ "$a" = --system-prompt-snapshot ] && { echo "error: unknown option '$a'" >&2; exit 1; }; done\nFAKE_CLAUDE_VERSION=2.1.197 exec "${FAKE_CLI}" "$@"\n`, { mode: 0o755 });+ try {+ resetPathCacheForTests();+ registerPathDir(newer);+ await create(undefined, {}, { cli: "claude" });+ await instance.snapshot();+ writeFileSync(slow, "");+ const snapshot = instance.snapshot();+ await vi.waitFor(() => expect(existsSync(versionAsked)).toBe(true), { timeout: 5000 });+ registerPathDir(older);+ await instance.adapter.sendTurn({ threadId: "t-late-check-race", text: "hi" });+ expect(await recorder.until((e) => e.type === "turn.completed" && e.threadId === "t-late-check-race")).toMatchObject({ ok: true });+ expect(existsSync(helpAsked)).toBe(true);+ await snapshot;+ } finally {+ resetPathCacheForTests();+ }+ });it.skipIf(process.platform === "win32")("keeps the new copy's flags when the old copy's slower --help read finishes last", async () => {// snapshot() is still reading 2.1.321's help when PATH finds 2.1.197 (no --autocompact); that old read must not restore 2.1.321's flags.const refused = join(scratch, "stale-help-autocompact.jsonl"), helpStarted = join(scratch, "newer-help-started");diff --git a/server/drivers/claude.ts b/server/drivers/claude.tsindex 334c9bde98..ece745d704 100644--- a/server/drivers/claude.ts+++ b/server/drivers/claude.ts@@ -1017,7 +1017,9 @@ export const ClaudeDriver: ProviderDriver<ClaudeConfig> = {if (turn.guestConfined) args.push("--restricted", "--tools", GUEST_CLAUDE_TOOLS.join(","));else if (config.tools !== undefined) args.push("--tools", config.tools.join(","));const turnEnvironment = environment();- await ensureCliVersion(turnEnvironment);+ const launchCli = findCliCandidates(config.cli, turnEnvironment.PATH ?? "")[0]; await ensureCliVersion(turnEnvironment);+ for (let tries = 0; tries < 2 && cliVersionChecked && cliProbedAt !== launchCli; tries++) await ensureCliVersion(turnEnvironment); // another copy's late check landed while this turn waited+ const ownCli = cliProbedAt === launchCli; // still another copy's state: none of its version-picked flagsif (turn.guestConfined && !claudeCliSupports(cliVersion, "--restricted")) {throw new Error(withWhy("This Claude Code is too old to run this turn without a shell. Update Claude Code.", turn.confinedWhy));}@@ -1036,9 +1038,9 @@ export const ClaudeDriver: ProviderDriver<ClaudeConfig> = {if (turn.userSkillsDir) args.push("--add-dir", turn.userSkillsDir);}const compactWindow = autoCompactWindow(turnEnvironment);- const passedAutocompact = Boolean(compactWindow) && cliHasAutocompact === true; if (compactWindow && passedAutocompact) args.push("--autocompact", compactWindow);+ const passedAutocompact = Boolean(compactWindow) && ownCli && cliHasAutocompact === true; if (compactWindow && passedAutocompact) args.push("--autocompact", compactWindow);// --resume keeps the recorded system prompt, so every turn refreshes it.- if (cliVersionChecked && claudeCliSupports(cliVersion, "--system-prompt-snapshot")) args.push("--system-prompt-snapshot", "off");+ if (ownCli && cliVersionChecked && claudeCliSupports(cliVersion, "--system-prompt-snapshot")) args.push("--system-prompt-snapshot", "off");const turnModel = config.managed ? turn.model : await resolveClaudeTurnModel(turn.model, turnEnvironment, config.endpointModels);const injected = config.managed || config.endpointModels ? { model: turnModel ?? null, injected: false } : applyClaudeInject({ ...turnEnvironment }, turnModel);if (injected.model) args.push("--model", injected.model);