Skip to content

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

Backup reasoning: a saved level the AI no longer offers falls back to its default at takeover

milind-sonifc83da25b2ec
diff --git a/server/automatic-recovery.ts b/server/automatic-recovery.ts
index 0586676aa9..356ad81d3a 100644
--- a/server/automatic-recovery.ts
+++ b/server/automatic-recovery.ts
@@ -1,6 +1,6 @@
import type { ProviderAdapter } from "./contracts.ts";
import { supportsWorkspaceFiles } from "./workspace.ts";
-import type { ModelSelection } from "../shared/wire.ts";
+import { offeredEffortLevels, type ModelSelection } from "../shared/wire.ts";
type Engine = { driverKind: string; capabilities: ProviderAdapter["capabilities"] };
type Backup = ModelSelection;
@@ -18,6 +18,16 @@ export function chosenBackups(bot: { fallback?: Backup[] } | null | undefined, r
return [...(bot?.fallback ?? []), ...(recovery?.enabled && recovery.backup ? [recovery.backup] : [])];
}
+/** A backup as `engine` takes it now: a saved reasoning level or variant the AI no longer offers is left to its default,
+ * so the switch to the backup is not refused for a setting the person chose while it was offered. */
+export function withOfferedReasoning(backup: Backup, engine: (Parameters<typeof offeredEffortLevels>[0] & { capabilities?: { modelVariants?: boolean } }) | undefined): Backup {
+ if (!engine || (backup.effort === undefined && backup.variant === undefined)) return backup;
+ const { effort, variant, ...rest } = backup;
+ if (effort !== undefined && offeredEffortLevels(engine, backup.model).includes(effort)) return { ...rest, effort };
+ if (variant !== undefined && engine.capabilities?.modelVariants === true) return { ...rest, variant };
+ return rest;
+}
+
/** An automatic switch must not strand the task without its tools or move its filesystem to another machine. A USB
* phone is not one of those tools: only Codex and Pi drive one, Claude offers it on every desktop, and no Cloud has a
* USB port, so counting it left a limited Claude bot with no backup but those two. */
diff --git a/server/host/limit-takeover.test.ts b/server/host/limit-takeover.test.ts
index 3514814da9..a41c1e79f5 100644
--- a/server/host/limit-takeover.test.ts
+++ b/server/host/limit-takeover.test.ts
@@ -64,14 +64,43 @@ function takeover(overrides: Partial<LimitTakeoverDeps> = {}, dir = freshDir())
return { instance, told, record, now };
}
+const REASONING = { ...TOOLS, effortLevels: ["low", "medium", "high"], modelVariants: true } as TakeoverEngine["capabilities"];
+const REASONING_ENGINES = () => [CLAUDE, CODEX, GO].map((one) => ({ ...one, capabilities: REASONING }));
+
describe("which AI carries on while one is limited", () => {
it("keeps the chosen reasoning when the first backup is also the same account's default model", () => {
const sameAccount = { ...on("codex", "gpt-6"), effort: "medium" } as const;
const other = { ...on("opencode", "go-default"), variant: "thinking" };
- const { instance } = takeover({ chosen: () => [other, sameAccount] });
+ const { instance } = takeover({ engines: REASONING_ENGINES, chosen: () => [other, sameAccount] });
expect(instance.backups("kai", on("codex", "gpt-6-mini"), "model")).toEqual([sameAccount, other]);
});
+ it("preserves configured reasoning effort and model variant on chosen backups", () => {
+ const effortBackup: ModelSelection = { instanceId: "claude", model: "claude-opus-5-5", effort: "medium" };
+ const variantBackup: ModelSelection = { instanceId: "opencode", model: "go-default", variant: "high" };
+ const { instance } = takeover({ engines: REASONING_ENGINES, chosen: () => [effortBackup, variantBackup] });
+ expect(instance.weighBackups("kai", on("codex", "gpt-6")).backups).toEqual([effortBackup, variantBackup]);
+ expect(instance.backups("kai", on("codex", "gpt-6"))).toEqual([effortBackup, variantBackup]);
+
+ const clock = new LimitResetClock({ accounts: () => [{ id: "codex", name: "Codex", driver: "codex", environment: {} }], now: () => NOW });
+ clock.noteLimit("codex", NOW + HOUR);
+ const room = takeover({ engines: REASONING_ENGINES, blockedUntil: (id) => clock.blockedUntil(id), resetAt: (id) => clock.resetAt(id) }).instance;
+ expect(room.takeoverAtStart(KAI, on("codex", "gpt-6"), [effortBackup])).toEqual({ to: effortBackup, scope: "engine", resetsAt: NOW + HOUR });
+ });
+
+ it("leaves a backup's reasoning the AI no longer offers to its default, so the switch is not refused", () => {
+ // gpt-6-mini now offers only "low", and this Codex has no named variants.
+ const reasoningCodex: TakeoverEngine = { ...CODEX, capabilities: { ...TOOLS, effortLevels: ["low", "medium", "high"] } as TakeoverEngine["capabilities"],
+ models: { ...CODEX.models, options: [CODEX.models.options[0]!, { id: "gpt-6-mini", label: "gpt-6-mini", effortLevels: ["low"] }] } };
+ const { instance } = takeover({
+ engines: () => [CLAUDE, reasoningCodex, GO],
+ chosen: () => [{ ...on("codex", "gpt-6-mini"), effort: "high" }, { ...on("codex", "gpt-6"), effort: "medium" }, { ...on("opencode", "go-default"), variant: "high" }],
+ });
+ expect(instance.backups("kai", on("claude", "claude-opus-5-5"))).toEqual([
+ on("codex", "gpt-6-mini"), { ...on("codex", "gpt-6"), effort: "medium" }, on("opencode", "go-default"),
+ ]);
+ });
+
it("keeps a bot on OpenCode Zen's free models while only the Go subscription's meter is full", () => {
const openCode = engine("opencode", "opencode", ["opencode-go/kimi-k2", "opencode/big-pickle"]);
const clock = new LimitResetClock({ accounts: () => [{ id: "opencode", name: "OpenCode", driver: "opencode", environment: {} }], now: () => NOW });
diff --git a/server/host/limit-takeover.ts b/server/host/limit-takeover.ts
index f22230c624..440ceeabf2 100644
--- a/server/host/limit-takeover.ts
+++ b/server/host/limit-takeover.ts
@@ -7,7 +7,7 @@ import { readFileSync } from "node:fs";
import { approvalModeFor, modelSwitchNeedsAsk, type ApprovalMode } from "../../shared/approval-mode.ts";
import { writeFileAtomic } from "../atomic.ts";
-import { recoveryCapabilityError } from "../automatic-recovery.ts";
+import { recoveryCapabilityError, withOfferedReasoning } from "../automatic-recovery.ts";
import type { InstanceConfigMap, ProviderAdapter, RuntimeEvent } from "../contracts.ts";
import { readyToRun, signedOut as statusSignedOut, type DefaultSelectionContext, type SelectableInstance } from "../default-model-selection.ts";
import { engineFailureKind } from "../drivers/engine-errors.ts";
@@ -201,7 +201,8 @@ export function createLimitTakeover(deps: LimitTakeoverDeps) {
if (!current) return { backups: [], skipped: [] };
const out: ModelSelection[] = [];
const left: SkippedBackup[] = [];
- const add = (pick: ModelSelection) => {
+ const add = (chosenPick: ModelSelection) => {
+ const pick = withOfferedReasoning(chosenPick, engines.find((engine) => engine.instanceId === chosenPick.instanceId));
if (pick.instanceId === from.instanceId && pick.model === from.model) return;
if ([...out, ...left.map((one) => one.selection)].some((known) => known.instanceId === pick.instanceId && known.model === pick.model)) return;
if (pick.instanceId === from.instanceId && (scope === "engine" || scope === "signed-out")) {
diff --git a/server/quota-switch.test.ts b/server/quota-switch.test.ts
index 7ee2c15047..4dc5028bcb 100644
--- a/server/quota-switch.test.ts
+++ b/server/quota-switch.test.ts
@@ -277,6 +277,16 @@ describe("quota switch", () => {
}
});
+ it("carries the backup model's configured reasoning effort or variant on automatic failover", async () => {
+ const backupWithEffort: ModelSelection = { instanceId: "codex", model: "codex-default", effort: "medium" };
+ backups = [backupWithEffort];
+ const turn = begin(bot.threadId, { messageId: said("use backup reasoning") });
+ coordinator.endTurn(turn.id, { ok: false, text: QUOTA });
+ expect(await ranOut(turn)).toBeNull();
+ expect(thread().modelSelection).toEqual(backupWithEffort);
+ expect(resend).toHaveBeenCalledTimes(1);
+ });
+
it("moves a request whose turn had already run something to the backup without sending it again", async () => {
for (const ran of ["tool", "reply"] as const) {
const turn = begin(bot.threadId, { messageId: said(`file the ticket (${ran})`) });
diff --git a/server/routes/bot-patch.test.ts b/server/routes/bot-patch.test.ts
index 5183396976..f9865b0173 100644
--- a/server/routes/bot-patch.test.ts
+++ b/server/routes/bot-patch.test.ts
@@ -584,3 +584,34 @@ describe("PATCH /api/bots/:id unread", () => {
expect(store.bot(bot.id)?.unread).toBe(true);
});
});
+
+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 checkedModelSelection = vi.fn((raw: unknown) => {
+ const row = raw as ModelSelection;
+ return row.effort === ("turbo" as never)
+ ? { ok: false as const, status: 400, error: `effort "turbo" is not recognized` }
+ : { ok: true as const, selection: { instanceId: row.instanceId, model: row.model.trim(), ...(row.effort ? { effort: row.effort } : {}), ...(row.variant ? { variant: row.variant } : {}) } };
+ });
+ const { store, bot, patch } = fixture({ registry: { get: () => engine }, checkedModelSelection } as unknown as Partial<BotPatchRouteDeps>);
+
+ const refused = await patch(bot.id, { fallback: [{ instanceId: "codex", model: "gpt-6", effort: "turbo" }] });
+ expect(refused.status).toBe(400);
+ expect(store.bot(bot.id)?.fallback).toBeUndefined();
+
+ const saved = await patch(bot.id, { fallback: [
+ { instanceId: "codex", model: "gpt-6-mini", effort: "low" },
+ { instanceId: "codex", model: "gpt-6", variant: "medium" },
+ { instanceId: "codex", model: "gpt-6-nano" },
+ ] });
+ expect(saved.status).toBe(200);
+ expect(store.bot(bot.id)?.fallback).toEqual([
+ { instanceId: "codex", model: "gpt-6-mini", effort: "low" },
+ { instanceId: "codex", model: "gpt-6", variant: "medium" },
+ { instanceId: "codex", model: "gpt-6-nano" },
+ ]);
+ // A row with no reasoning is saved as it was, without the model check a reasoning choice needs.
+ expect(checkedModelSelection).toHaveBeenCalledTimes(3);
+ });
+});