Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Refuse a backup AI that cannot take over the bot's workspace
erywim248379166c90
diff --git a/server/routes/bot-patch.test.ts b/server/routes/bot-patch.test.tsindex de85acce98..9e06fba33d 100644--- a/server/routes/bot-patch.test.ts+++ b/server/routes/bot-patch.test.ts@@ -634,7 +634,7 @@ describe("PATCH /api/bots/:id unread", () => {describe("PATCH /api/bots/:id fallback", () => {it("checks a backup's reasoning as a model choice is checked, saves what that check returns, and passes plain rows as before", async () => {- const engine = { instanceId: "codex", driverKind: "codex", enabled: true } as unknown as ProviderInstance;+ const engine = { instanceId: "codex", driverKind: "codex", enabled: true, adapter: { capabilities: {} } } as unknown as ProviderInstance;const checkedModelSelection = vi.fn((raw: unknown) => {const row = raw as ModelSelection;return row.effort === ("turbo" as never)@@ -661,4 +661,36 @@ describe("PATCH /api/bots/:id fallback", () => {// A row with no reasoning is saved as it was, without the model check a reasoning choice needs.expect(checkedModelSelection).toHaveBeenCalledTimes(3);});++ it("refuses a new backup that cannot take over the bot's workspace", async () => {+ const claude = { instanceId: "claude", driverKind: "claudeAgent", enabled: true, adapter: { capabilities: {} } } as unknown as ProviderInstance;+ const openai = { instanceId: "openai", driverKind: "openai-compat", enabled: true, adapter: { capabilities: {} } } as unknown as ProviderInstance;+ const { store, bot, patch } = fixture({ registry: { get: (id: string) => id === "claude" ? claude : id === "openai" ? openai : null } } as unknown as Partial<BotPatchRouteDeps>);++ const refused = await patch(bot.id, { fallback: [{ instanceId: "openai", model: "gpt-5" }] });+ expect(refused).toEqual({ status: 400, body: { error: "The backup cannot use the same workspace." } });+ expect(store.bot(bot.id)?.fallback).toBeUndefined();++ expect((await patch(bot.id, { fallback: [{ instanceId: "claude", model: "claude-haiku-5" }] })).status).toBe(200);+ });++ it("refuses a new backup that drops a tool the primary uses", async () => {+ const claude = { instanceId: "claude", driverKind: "claudeAgent", enabled: true, adapter: { capabilities: { composioMcp: true } } } as unknown as ProviderInstance;+ const bare = { instanceId: "other-claude", driverKind: "claudeAgent", enabled: true, adapter: { capabilities: { composioMcp: false } } } as unknown as ProviderInstance;+ const { bot, patch } = fixture({ registry: { get: (id: string) => id === "claude" ? claude : id === "other-claude" ? bare : null } } as unknown as Partial<BotPatchRouteDeps>);++ const refused = await patch(bot.id, { fallback: [{ instanceId: "other-claude", model: "claude-haiku-5" }] });+ expect(refused).toEqual({ status: 400, body: { error: "The backup does not support all of this AI's tools and attachments." } });+ });++ it("keeps an already-saved incompatible backup editable", async () => {+ const claude = { instanceId: "claude", driverKind: "claudeAgent", enabled: true, adapter: { capabilities: {} } } as unknown as ProviderInstance;+ const openai = { instanceId: "openai", driverKind: "openai-compat", enabled: true, adapter: { capabilities: {} } } as unknown as ProviderInstance;+ const { store, bot, patch } = fixture({ registry: { get: (id: string) => id === "claude" ? claude : id === "openai" ? openai : null } } as unknown as Partial<BotPatchRouteDeps>);+ store.patchBot(bot.id, { fallback: [{ instanceId: "openai", model: "gpt-5" }] });++ const saved = await patch(bot.id, { fallback: [{ instanceId: "openai", model: "gpt-5" }, { instanceId: "claude", model: "claude-haiku-5" }] });+ expect(saved.status).toBe(200);+ expect(store.bot(bot.id)?.fallback).toEqual([{ instanceId: "openai", model: "gpt-5" }, { instanceId: "claude", model: "claude-haiku-5" }]);+ });});diff --git a/server/routes/bot-patch.ts b/server/routes/bot-patch.tsindex 393f24fdc8..9a664d9b27 100644--- a/server/routes/bot-patch.ts+++ b/server/routes/bot-patch.ts@@ -10,6 +10,7 @@ import type { MemoryModelSelection } from "../../shared/memory-model.ts";import { parseToolsOff, TOOL_GROUPS } from "../../shared/tool-groups.ts";import { isMausColor, MAUS_COLOR_NAMES, type BotUpdateResponse, type WireBot, type WireGroup } from "../../shared/wire.ts";import { dropRemovedGrants, MAX_ALWAYS_ALLOW } from "../always-allow.ts";+import { recoveryCapabilityError } from "../automatic-recovery.ts";import { checkBrowserProfileChange, settleBrowserChange } from "../bot-browser-profile.ts";import { validateBotCwd } from "../bot-cwd.ts";import { parseBotProfilePatch } from "../bot-profile.ts";@@ -255,15 +256,26 @@ export function createBotPatchRoutes(deps: BotPatchRouteDeps): RouteHandler {if (!Array.isArray(body.fallback) || body.fallback.length > 5) {return json(res, 400, { error: "fallback must be a list of up to five { instanceId, model } entries" });}+ const primarySelection = normalizedSelection ?? existingBot?.modelSelection;+ const primaryInstance = primarySelection ? registry.get(primarySelection.instanceId) : null;+ const primaryEngine = primaryInstance ? { driverKind: primaryInstance.driverKind, capabilities: primaryInstance.adapter.capabilities } : null;+ const saved = new Set((existingBot?.fallback ?? []).map((entry) => `${entry.instanceId}\u0000${entry.model}`));const chain: ModelSelection[] = [];for (const entry of body.fallback as unknown[]) {const row = entry && typeof entry === "object" ? (entry as Partial<Record<keyof ModelSelection, unknown>>) : null;if (!row || typeof row.instanceId !== "string" || !/^[\w.-]+$/.test(row.instanceId) || typeof row.model !== "string" || !row.model.trim() || row.model.length > 500) {return json(res, 400, { error: "fallback must be a list of up to five { instanceId, model } entries" });}- if (!registry.get(row.instanceId)) return json(res, 400, { error: `fallback engine "${row.instanceId}" is not available` });+ const instance = registry.get(row.instanceId);+ if (!instance) return json(res, 400, { error: `fallback engine "${row.instanceId}" is not available` });const model = row.model.trim();if (chain.some((seen) => seen.instanceId === row.instanceId && seen.model === model)) continue;+ // A backup that cannot use the bot's workspace or tools can never take over automatically. An entry saved+ // before this check stays editable, so a chain can still be repaired without deleting it first.+ if (primaryEngine && !saved.has(`${row.instanceId}\u0000${model}`)) {+ const incompatible = recoveryCapabilityError(primaryEngine, { driverKind: instance.driverKind, capabilities: instance.adapter.capabilities });+ if (incompatible) return json(res, 400, { error: incompatible });+ }if (row.effort === undefined && row.variant === undefined) {chain.push({ instanceId: row.instanceId, model });} else {