Skip to content

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

Notify VPS server switch as soon as the alias commits

Ada8f812a76fcfc
diff --git a/server/routes/bot-patch.test.ts b/server/routes/bot-patch.test.ts
index fd2b0ac4cc..68b580b71e 100644
--- a/server/routes/bot-patch.test.ts
+++ b/server/routes/bot-patch.test.ts
@@ -301,6 +301,17 @@ describe("PATCH /api/bots/:id vpsAlias relocation guard", () => {
expect((await patch(bot.id, { vpsAlias: null })).status).toBe(200);
expect(switched).toEqual(["gamma-vps", undefined]);
});
+ it("notifies vpsServerSwitched for a committed alias even when the profile save that follows throws", async () => {
+ const switched: Array<string | undefined> = [];
+ const { store, bot, patch } = fixture({
+ vpsServerSwitched: (updated) => { switched.push(updated.vpsAlias); },
+ });
+ vi.spyOn(store, "patchBotProfile").mockImplementation(() => { throw new Error("saveBots failed"); });
+
+ await expect(patch(bot.id, { vpsAlias: "gamma-vps", soul: "New instructions" })).rejects.toThrow("saveBots failed");
+ expect(store.bot(bot.id)?.vpsAlias).toBe("gamma-vps");
+ expect(switched).toEqual(["gamma-vps"]);
+ });
});
describe("PATCH /api/bots/:id defaultBot: null", () => {
diff --git a/server/routes/bot-patch.ts b/server/routes/bot-patch.ts
index 0735e07e1b..87a527e8ee 100644
--- a/server/routes/bot-patch.ts
+++ b/server/routes/bot-patch.ts
@@ -729,6 +729,13 @@ export function createBotPatchRoutes(deps: BotPatchRouteDeps): RouteHandler {
return json(res, 409, { error: "Stop the bot and release browser control before changing its profile." });
}
const beforeVpsAlias = freshBrowserBot ? vpsSshAliasForBot(cfg, freshBrowserBot) : null;
+ // Fires once the alias commits, so a later profile save that throws cannot strand the old tunnel and idle timer.
+ let vpsSwitchNotified = false;
+ const notifyVpsSwitch = (next: BotRecord | null | undefined) => {
+ if (!existingBot || !next || vpsSwitchNotified || vpsSshAliasForBot(cfg, next) === beforeVpsAlias) return;
+ vpsSwitchNotified = true;
+ deps.vpsServerSwitched?.(next);
+ };
// A mark goes out with its hold in one frame, below.
delete patch.unread;
if (Array.isArray(patch.alwaysAllow)) dropRemovedGrants(store, deps.turnBots?.values() ?? [], m[1], patch.alwaysAllow);
@@ -737,7 +744,7 @@ export function createBotPatchRoutes(deps: BotPatchRouteDeps): RouteHandler {
// Runtime fields keep their fail-closed order; only the profile fields commit atomically.
const runtimePatch = { ...patch };
for (const field of Object.keys(profile.patch)) delete runtimePatch[field];
- if (Object.keys(runtimePatch).length) store.patchBot(m[1], runtimePatch);
+ if (Object.keys(runtimePatch).length) notifyVpsSwitch(store.patchBot(m[1], runtimePatch));
bot = store.patchBotProfile(m[1], profile.patch);
} else {
if (freshBrowserBot) assertTeamComputerChangeIdle(freshBrowserBot, { ...freshBrowserBot, ...patch } as BotRecord);
@@ -759,7 +766,7 @@ export function createBotPatchRoutes(deps: BotPatchRouteDeps): RouteHandler {
if (normalizedSelection && existingBot) deps.modelSwitched?.(bot.id, movedTask?.threadId ?? existingBot.threadId, movedTask?.modelSelection ?? existingBot.modelSelection, normalizedSelection, true);
if (existingBot) {
settleBrowserChange(bot, { browserProfile: beforeBrowserProfile, browser: beforeBrowserEnabled }, { closeForBot: (id) => browserLive.closeForBot(id), forgetTemporaryBrowser });
- if (vpsSshAliasForBot(cfg, bot) !== beforeVpsAlias) deps.vpsServerSwitched?.(bot);
+ notifyVpsSwitch(bot);
}
const chiefChanges =
body.chiefOfStaff === true || chiefMovedSections