Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(rooms): log the passed-over backups when none can take a room turn
laijsg1182762f38d6d
diff --git a/server/host/limit-takeover.test.ts b/server/host/limit-takeover.test.tsindex 2a8d601141..aee4382403 100644--- a/server/host/limit-takeover.test.ts+++ b/server/host/limit-takeover.test.ts@@ -363,6 +363,10 @@ describe("a new conversation once an AI is known to be at its limit", () => {expect(instance.takeoverAtStart(KAI, on("claude", "claude-opus-5-5"), own), "nothing holds Claude yet").toBeNull();refused(clock, "t1", NOW + 5 * HOUR);expect(instance.takeoverAtStart(KAI, on("claude", "claude-opus-5-5"), []), "only the backups given, never the installation's").toBeNull();+ const lines: string[] = [];+ const logged = takeover({ blockedUntil: (id) => clock.blockedUntil(id), log: (line) => lines.push(line) }).instance;+ expect(logged.takeoverAtStart(KAI, on("claude", "claude-opus-5-5"), [on("opencode", "go-gone")]), "none usable").toBeNull();+ expect(lines, "still says why each was left out").toEqual([expect.stringContaining("passed over backup opencode/go-gone: unknown-model")]);expect(instance.heldAtStart(KAI, on("claude", "claude-opus-5-5"), false, []), "nothing to carry on with").toBeNull();const taken = instance.takeoverAtStart(KAI, on("claude", "claude-opus-5-5"), own);expect(taken).toEqual({ to: on("codex", "gpt-6"), scope: "engine", resetsAt: NOW + 5 * HOUR });@@ -389,6 +393,7 @@ describe("a new conversation once an AI is known to be at its limit", () => {const off = takeover({ blockedUntil: (id) => clock.blockedUntil(id), enabled: () => false }).instance;expect(off.heldAtStart(KAI, on("claude", "claude-opus-5-5"))).toBeNull();expect(off.heldAtStart(KAI, on("claude", "claude-opus-5-5"), true)).toEqual({ scope: "engine" });+ expect(off.takeoverAtStart(KAI, on("claude", "claude-opus-5-5"), [on("codex", "gpt-6")]), "a room member's own backup too").toBeNull();});it("keeps one model's own limit through a restart, and a turn that works on another model leaves it held", () => {diff --git a/server/host/limit-takeover.ts b/server/host/limit-takeover.tsindex 47f45e0a41..1fe2f05b2b 100644--- a/server/host/limit-takeover.ts+++ b/server/host/limit-takeover.ts@@ -269,17 +269,22 @@ export function createLimitTakeover(deps: LimitTakeoverDeps) {signedIn(selection.instanceId);};- function heldAtStart(bot: ThreadBot, selection: ModelSelection, recovering = false, chosen?: readonly ModelSelection[]): { scope: TakeoverScope; resetsAt?: number } | null {+ function holdOn(selection: ModelSelection): { scope: TakeoverScope; resetsAt?: number } | null {const engineUntil = deps.blockedUntil(selection.instanceId, selection.model);const model = engineUntil === null ? modelHeld(selection) : null;const scope: TakeoverScope | null = engineUntil !== null ? "engine" : model ? model.scope : signedOutHeld(selection.instanceId) ? "signed-out" : null;if (!scope) return null;- const carries = (deps.enabled?.() ?? true) && backups(bot.id, selection, scope, approvalModeFor(bot), chosen).length > 0;- if (!carries && !(recovering && scope !== "signed-out")) return null;const resetsAt = scope === "engine" ? deps.resetAt?.(selection.instanceId, selection.model) ?? undefined : model?.resetsAt;return { scope, ...(resetsAt && resetsAt > now() ? { resetsAt } : {}) };}+ function heldAtStart(bot: ThreadBot, selection: ModelSelection, recovering = false, chosen?: readonly ModelSelection[]): { scope: TakeoverScope; resetsAt?: number } | null {+ const held = holdOn(selection);+ if (!held) return null;+ const carries = (deps.enabled?.() ?? true) && backups(bot.id, selection, held.scope, approvalModeFor(bot), chosen).length > 0;+ return carries || (recovering && held.scope !== "signed-out") ? held : null;+ }+function carryOnRows(bot: Bot, from: ModelSelection, to: ModelSelection, facts: { scope: TakeoverScope; resetsAt?: number }): CarriedOnRow[] {const told = tell(bot, from, { ...facts, backup: to });const line = facts.scope === "signed-out" ? signedOutLine(refusedName(from, facts.scope), label(to, from))@@ -331,8 +336,8 @@ export function createLimitTakeover(deps: LimitTakeoverDeps) {/** Before a turn on `selection`: when what holds that AI is already known and one of `chosen` can take the turn, that* backup and what held it; the backups passed over are logged. Null when the turn goes to `selection` as usual. */takeoverAtStart(bot: ThreadBot, selection: ModelSelection, chosen: readonly ModelSelection[]): { to: ModelSelection; scope: TakeoverScope; resetsAt?: number } | null {- const held = heldAtStart(bot, selection, false, chosen);- if (!held) return null;+ const held = holdOn(selection);+ if (!held || !(deps.enabled?.() ?? true)) return null;const { backups: usable, skipped } = weighBackups(bot.id, selection, held.scope, approvalModeFor(bot), chosen);passedOver(bot, selection, held.scope, skipped);return usable[0] ? { to: usable[0], ...held } : null;diff --git a/server/room-limit-takeover.e2e.test.ts b/server/room-limit-takeover.e2e.test.tsindex 72d2e50365..5a60860f58 100644--- a/server/room-limit-takeover.e2e.test.ts+++ b/server/room-limit-takeover.e2e.test.ts@@ -12,7 +12,7 @@ const BACKUP_REPLY = "done from fake codex";const jsonLines = (path: string): any[] => existsSync(path)? readFileSync(path, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line)) : [];-type Room = { api: (method: string, path: string, body?: unknown) => Promise<any>; send: (text: string, settles?: string) => Promise<void>; messages: () => Promise<any[]>; claudeTurns: () => any[]; usage: () => any[]; bot: any };+type Room = { api: (method: string, path: string, body?: unknown) => Promise<any>; send: (text: string, settles?: string) => Promise<void>; messages: () => Promise<any[]>; claudeTurns: () => any[]; usage: () => any[]; serverLog: () => string; bot: any };/** One room with one member on fake Claude, whose every turn its usage limit refuses, and fake Codex beside it. */async function withLimitedRoom(choose: (f: { api: (method: string, path: string, body?: unknown) => Promise<any>; bot: any; codex: { instanceId: string; model: string } }) => Promise<void>,@@ -49,12 +49,14 @@ async function withLimitedRoom(choose: (f: { api: (method: string, path: string,await choose({ api, bot, codex });const send = async (text: string, settles = "settled") => {await cli("send-channel", "--channel", channel.id, "--text", `@Analyst ${text}`);- expect((await cli("wait", "--channel", channel.id, "--timeout", "60")).status).toBe(settles);+ const waited = await cli("wait", "--channel", channel.id, "--timeout", "60");+ evidence.push({ sent: text, wait: waited });+ expect(waited.status).toBe(settles);};const messages = async () => (await api("GET", `/api/threads/${channel.activeTaskId}/messages?limit=200`)).messages as any[];try {const usage = () => readUsage(dataDir, { from: new Date(Date.now() - 3_600_000), to: new Date(Date.now() + 3_600_000) }).filter((row) => row.threadId === channel.activeTaskId && row.botId === bot.id);- await check({ api, send, messages, claudeTurns: () => jsonLines(`${planPath}.evidence.jsonl`).filter((turn) => turn.botId === bot.id), usage, bot });+ await check({ api, send, messages, claudeTurns: () => jsonLines(`${planPath}.evidence.jsonl`).filter((turn) => turn.botId === bot.id), usage, serverLog: () => readFileSync(logPath, "utf8"), bot });} finally {evidence.push({ messages: await messages() });}@@ -104,3 +106,17 @@ it("never sends a room member's turn to the installation's backup, which leavesexpect(usage().some((row) => row.instanceId === "codex")).toBe(false);});}, 180_000);++it("keeps a room member on its own AI when none of its own backups can take the turn, and logs why each was passed over", async () => {+ await withLimitedRoom(async ({ api, bot }) => {+ await api("PATCH", `/api/bots/${bot.id}`, { fallback: [{ instanceId: "codex", model: "gpt-gone" }] });+ }, async ({ send, messages, claudeTurns, usage, serverLog }) => {+ await send("How did October close?", "failed");+ await send("And November so far?", "failed");+ expect(claudeTurns(), "each turn is tried on its own AI").toHaveLength(2);+ const rows = await messages();+ expect(rows.some((row) => row.kind === "activity" && /took over/.test(row.tool?.name ?? ""))).toBe(false);+ expect(usage().some((row) => row.instanceId === "codex")).toBe(false);+ await expect.poll(serverLog, { timeout: 15_000 }).toMatch(/limit takeover: Analyst \([^)]+\) on claude\/\S+ \(engine\) passed over backup codex\/gpt-gone: unknown-model/);+ });+}, 180_000);