Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Teammates: a bot whose setup failed once reads as available again after a turn of it succeeds (#6998)
Milind Sonieda0c38d8f16
diff --git a/server/host/setup-recovered.test.ts b/server/host/setup-recovered.test.tsnew file mode 100644index 0000000000..88544d7776--- /dev/null+++ b/server/host/setup-recovered.test.ts@@ -0,0 +1,68 @@+import { rmSync } from "node:fs";+import { beforeEach, describe, expect, it } from "vitest";++import { DATA_DIR } from "../config.ts";+import type { ModelSelection } from "../contracts.ts";+import { peerStatus, peerStatusWords } from "../peer-roster.ts";+import { Store } from "../store.ts";+import { clearSetupFailures } from "./setup-recovered.ts";++const selection = (): ModelSelection => ({ instanceId: "claude", model: "claude-sonnet-5" });+const roster = (store: Store, botId: string) => peerStatusWords(peerStatus(store.bot(botId)?.activity, store.bot(botId)?.busy));++describe("a bot whose setup failed once", () => {+ beforeEach(() => { rmSync(DATA_DIR, { recursive: true, force: true }); });++ it("reads as available again once another of its turns succeeds, not until the failed conversation runs again", () => {+ const store = new Store(selection);+ const bot = store.createBot();+ const failed = store.createTask(bot.id, "signed out")!;+ const later = store.createTask(bot.id, "later")!;+ // Its engine's sign-in had expired in one conversation (server/index.ts marks it "dead").+ store.setTaskActivity(bot.id, failed.threadId, "dead");+ expect(roster(store, bot.id)).toBe("unavailable — needs setup");+ // The person signs in again and another conversation of the same bot works.+ store.setTaskActivity(bot.id, later.threadId, "working");+ store.setTaskActivity(bot.id, later.threadId, "idle");+ expect(roster(store, bot.id)).toBe("unavailable — needs setup");+ clearSetupFailures(store, bot.id, later.threadId);+ expect(roster(store, bot.id)).toBe("available");+ expect(store.taskByThread(bot.id, failed.threadId)?.activity).toBe("idle");+ });++ it("keeps a conversation on another engine marked: a turn that works on one engine says nothing of another's sign-in", () => {+ const store = new Store(selection);+ const bot = store.createBot();+ const other = store.createTask(bot.id, "other engine", true, undefined, undefined, undefined, { instanceId: "codex", model: "gpt-5" })!;+ const same = store.createTask(bot.id, "same engine")!;+ store.setTaskActivity(bot.id, other.threadId, "dead");+ clearSetupFailures(store, bot.id, same.threadId);+ expect(store.taskByThread(bot.id, other.threadId)?.activity).toBe("dead");+ expect(roster(store, bot.id)).toBe("unavailable — needs setup");+ });++ it("goes by the engine each conversation last ran on: a turn a stand-in engine carried clears nothing on the signed-out one", () => {+ const store = new Store(selection);+ const bot = store.createBot();+ const failed = store.createTask(bot.id, "signed out")!;+ const carried = store.createTask(bot.id, "carried by a stand-in")!;+ store.markTaskDispatched(bot.id, failed.threadId, "claude");+ store.setTaskActivity(bot.id, failed.threadId, "dead");+ store.markTaskDispatched(bot.id, carried.threadId, "codex");+ clearSetupFailures(store, bot.id, carried.threadId);+ expect(store.taskByThread(bot.id, failed.threadId)?.activity).toBe("dead");+ store.markTaskDispatched(bot.id, carried.threadId, "claude");+ clearSetupFailures(store, bot.id, carried.threadId);+ expect(store.taskByThread(bot.id, failed.threadId)?.activity).toBe("idle");+ });++ it("leaves other activities as they are", () => {+ const store = new Store(selection);+ const bot = store.createBot();+ const waiting = store.createTask(bot.id, "asks")!;+ store.setTaskActivity(bot.id, waiting.threadId, "waiting-on-you");+ clearSetupFailures(store, bot.id, waiting.threadId);+ expect(store.taskByThread(bot.id, waiting.threadId)?.activity).toBe("waiting-on-you");+ clearSetupFailures(store, "no-such-bot", waiting.threadId);+ });+});diff --git a/server/host/setup-recovered.ts b/server/host/setup-recovered.tsnew file mode 100644index 0000000000..f39eb16576--- /dev/null+++ b/server/host/setup-recovered.ts@@ -0,0 +1,23 @@+// A setup failure (an engine signed out or out of credits) marks its conversation "dead", and one dead conversation makes+// the whole bot read "unavailable — needs setup" to its teammates (peer-roster.ts), in the app and in reports, until that+// same conversation runs again. A later turn of the bot that succeeds on the same engine shows that engine's setup works+// now, so the label goes with it. A conversation whose engine is another keeps it: that engine may still be signed out.+import type { Store } from "../store.ts";++/** server/index.ts parks this in lastInstanceId when a conversation was carried in from elsewhere: no engine. */+const EXTERNAL_CONTEXT = "__openmaus_external_context__:";++type Task = { lastInstanceId?: string; modelSelection?: { instanceId: string } };++export function clearSetupFailures(store: Pick<Store, "bot" | "setTaskActivity">, botId: string, threadId: string): void {+ const bot = store.bot(botId);+ const succeeded = bot?.tasks?.find((task) => task.threadId === threadId);+ if (!bot || !succeeded) return;+ // The engine a turn was last sent to (a stand-in after a failover included), else the one the conversation is set to.+ const engineOf = (task: Task) => task.lastInstanceId && !task.lastInstanceId.startsWith(EXTERNAL_CONTEXT)+ ? task.lastInstanceId : (task.modelSelection ?? bot.modelSelection).instanceId;+ const engine = engineOf(succeeded);+ for (const task of bot.tasks ?? []) {+ if (task.activity === "dead" && engineOf(task) === engine) store.setTaskActivity(botId, task.threadId, "idle");+ }+}diff --git a/server/index.ts b/server/index.tsindex 4831fa7cc6..b9ab338495 100644--- a/server/index.ts+++ b/server/index.ts@@ -277,7 +277,7 @@ import { CLOUD_HOME_SECRET_KEYS, CLOUD_IGNORED_KEYS, cloudHomeConfiguration, cloimport { botBox, startBotBoxAtBoot } from "./bot-box/boot.ts"; import { attachmentsForBox } from "./box-attachments.ts"; import { brokerPath } from "./bot-box/broker-probe.ts";import { createCloudSignedHost } from "./host/cloud-signed-host.ts"; import { createDeleteLeftovers, engineSessionFolders, instanceEnvironment } from "./host/delete-leftovers.ts"; import { createBotNameClash } from "./host/bot-name-clash.ts"; import { hiddenThreadRefusal, hideOthersSyncFolders } from "./host/team-private.ts"; import { createTeamMemberLeft, memoryLayerRecords } from "./host/team-member-left.ts"; import { createUploadOwners, uploadsFor } from "./host/member-uploads.ts"; import { createMemberOwnBot } from "./host/member-own-bot.ts"; import { createVisibilityHost } from "./host/visibility-host.ts"; import { createBotAccessHost, refusalFor, relayedSendRefusal } from "./host/bot-access-gate.ts"; import { createBotAccessRoutes, writeAccess } from "./routes/bot-access.ts"; import { createAccessRequests } from "./host/access-requests.ts";import { cloudPowerNow, createCloudPower, registerCloudPower, wokeFromSleep, type CloudPower } from "./cloud-power.ts"; import { createWokeNotifier } from "./cloud-woke.ts"; import { createSharedComputerRoutes } from "./routes/shared-computers.ts";-import { lentCopies, noteSyncBotTurn } from "./routes/cloud-sync.ts"; import { mcpReconnectLine, retryBrokeredMcp } from "./bot-box/broker-mcp.ts";+import { lentCopies, noteSyncBotTurn } from "./routes/cloud-sync.ts"; import { mcpReconnectLine, retryBrokeredMcp } from "./bot-box/broker-mcp.ts"; import { clearSetupFailures } from "./host/setup-recovered.ts";import { type CloudOwnership } from "./cloud-owner.ts"; import { memberLoginRefusal, personalMoveRefusal } from "./cloud-team.ts"; import { createCloudTeamHost, type MemberProfile } from "./host/cloud-team-host.ts"; import { createTeamFilesHost } from "./host/team-files-host.ts"; import { createTeamFilesRoutes } from "./routes/team-files.ts"; import { engineHomes } from "./engine-homes/engine-homes.ts"; import { audienceMember, layerId, memberAudience } from "./audience.ts"; import { createPersonRooms, createRoomReads, type OwnGroupFrames } from "./host/room-reads.ts"; import { createPersonBotOrder } from "./host/person-bot-order.ts"; import { createMeRoutes } from "./routes/me.ts"; import { createPersonNames } from "./host/person-names.ts"; import { createTeamPhoneRoutes } from "./routes/team-phone.ts"; import { settleCloudHome } from "./cloud-home-settle.ts"; import { CLOUD_NOBODY_KEY, CLOUD_OUTSIDE_KEY } from "./cloud-sharing.ts"; import { createTeamSlackHost } from "./host/team-slack-host.ts"; import { createTeamSlackMessages } from "./team-slack.ts"; import { createTeamSlackRoutes } from "./routes/team-slack.ts";import { createCloudMoveRoutes } from "./cloud-move-http.ts";import { RESTART_EXIT_CODE } from "./restart.ts"; import { installCrashGuard, SERVER_ERROR_CAUSE } from "./crash-guard.ts"; import { markRequestArrival } from "./perf/loop-stalls.ts"; import { createThreadMaps, keepThreadEntry } from "./host/thread-maps.ts"; import { botLimitRefusal } from "./bot-limit.ts";@@ -6670,7 +6670,7 @@ bus.subscribe((event: RuntimeEvent) => {const held = facts.limit ?? (signedOutStop(facts.stopReason, failureDetail) ? "signed-out" as const : undefined); // a limit or a sign-out: one incident a stretch, the person told by its own noticeconst fileIncident = () => { if (broken && (!held || limitTakeover.firstIncident(broken.id, broken.modelSelection, held, facts.limitResetsAt))) reportIncident({ kind: "failed", bot: broken, threadId: event.threadId, detail: failureDetail, facts, notified: Boolean(held) }); };let directSettled = () => {}; const settledDirect = new Promise<void>((resolve) => { directSettled = resolve; }); if (failed && bot) void quotaSwitch.turnFailed(bot, event, internal, Promise.all([settledDirect, directTurnDispatchClaims.released(event.threadId)]));- else if (bot && event.ok) quotaSwitch.turnSucceeded(bot, event.threadId);+ else if (bot && event.ok) { quotaSwitch.turnSucceeded(bot, event.threadId); if (!stopped) clearSetupFailures(store, bot.id, event.threadId); }if (bot && event.ok && !stopped && completedTurnId && !routineRun && !coordinator.assignerOf(event.threadId)) askCards.ownQuestion(bot.id, event.threadId, completedTurnId);const lastReported = turnUsage.get(event.threadId);turnUsage.delete(event.threadId);