Skip to content

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

fix(computer): join a client's Local VM only with a lease this view owns

AdityaUmale1a31c50cce01
diff --git a/src/components/computer-takeover.test.ts b/src/components/computer-takeover.test.ts
index 65ea29b22b..de238b61ec 100644
--- a/src/components/computer-takeover.test.ts
+++ b/src/components/computer-takeover.test.ts
@@ -150,8 +150,9 @@ describe("the app's viewer window", () => {
});
});
-it("the join is told the lease its take holds, so a Local VM join can name it", async () => {
- const join = vi.fn(async (_signal: AbortSignal, _lease: string) => ({ joinUrl: "https://desktop.invalid/scout" }));
+it.each([true, false])("the join is told the lease its take owns (owned: %s), so a Local VM join can name it", async (owned) => {
+ owns = owned;
+ const join = vi.fn(async (_signal: AbortSignal, _lease: string | null) => ({ joinUrl: "https://desktop.invalid/scout" }));
await openProviderDesktop({ botId: "scout", title: "Scout's live desktop", request, current: () => true, join });
- expect(join).toHaveBeenCalledWith(expect.any(AbortSignal), requests[0]!.lease);
+ expect(join).toHaveBeenCalledWith(expect.any(AbortSignal), owned ? requests[0]!.lease : null);
});
diff --git a/src/components/computer-takeover.ts b/src/components/computer-takeover.ts
index 978ed25610..7ea6ca9ba6 100644
--- a/src/components/computer-takeover.ts
+++ b/src/components/computer-takeover.ts
@@ -54,8 +54,8 @@ export async function openProviderDesktop({ botId, title, request, join, current
botId: string;
title: string;
request: Request;
- /** Gets the viewer link; a Local VM's join names the lease its take holds. */
- join: (signal: AbortSignal, controlLeaseId: string) => Promise<{ joinUrl?: unknown }>;
+ /** Gets the viewer link; a Local VM's join names the lease its take owns, null while another view holds the computer. */
+ join: (signal: AbortSignal, controlLeaseId: string | null) => Promise<{ joinUrl?: unknown }>;
/** Still the conversation and computer the person asked for. */
current: () => boolean;
}): Promise<void> {
@@ -73,7 +73,7 @@ export async function openProviderDesktop({ botId, title, request, join, current
const mine = take.owned ? lease : null;
if (mine && window.ogb?.desktopViewer) recordViewerLease(botId, mine);
if (!current()) throw changed();
- const { joinUrl } = await join(AbortSignal.timeout(JOIN_TIMEOUT_MS), lease);
+ const { joinUrl } = await join(AbortSignal.timeout(JOIN_TIMEOUT_MS), mine);
const viewerUrl = typeof joinUrl === "string" && joinUrl ? joinUrl : null;
if (!current()) throw changed();
if (!viewerUrl) throw new LocalizedPanelError("computer.err.noDesktopLink");
diff --git a/src/components/remote-desktop-panel.open.test.ts b/src/components/remote-desktop-panel.open.test.ts
index 475f74426d..26b03810d3 100644
--- a/src/components/remote-desktop-panel.open.test.ts
+++ b/src/components/remote-desktop-panel.open.test.ts
@@ -110,3 +110,21 @@ it("a client whose host runs the bot on its Local VM takes and opens that deskto
expect(fixture.joins).toEqual([`/api/bots/scout/local-computer/join?controlLeaseId=${lease}`]);
expect(open).toHaveBeenCalledWith(new URL("/vps-viewer/relay-1", window.location.origin).toString(), expect.any(String), "scout");
});
+
+it("a client whose host's Local VM is already held in another view is told so, and never joins with a lease it does not own", async () => {
+ fixture.owned = false;
+ const open = vi.fn(async () => true);
+ vi.stubGlobal("ogb", { desktopViewer: { open, close: vi.fn(async () => true), onState: () => () => {}, currentState: async () => ({ open: false, contextId: null }) } });
+ const container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+ flushSync(() => root!.render(createElement(BotEditorStore, { value: {
+ state: { ...initialState, instances: [engine], config: { box: { configured: true } } as AppState["config"] } as AppState,
+ dispatch: vi.fn(), flushBotPatches: async () => null, refreshInstances: async () => {}, refreshModels: async () => {},
+ }, children: createElement(RemoteDesktopPanel, { bot: { ...bot, computer: "vm" } }) })));
+ const take = [...document.querySelectorAll("button")].find((button) => button.textContent?.trim() === "Take control");
+ flushSync(() => take!.click());
+ await vi.waitFor(() => expect(document.querySelector("[role=alert]")?.textContent).toContain("already controlling this desktop in another view"));
+ expect(fixture.joins).toEqual([]);
+ expect(open).not.toHaveBeenCalled();
+});
diff --git a/src/components/remote-desktop-panel.tsx b/src/components/remote-desktop-panel.tsx
index 428d5082b0..f31c03f088 100644
--- a/src/components/remote-desktop-panel.tsx
+++ b/src/components/remote-desktop-panel.tsx
@@ -15,7 +15,7 @@ import { RoutineEditor } from "./RoutinesPage";
import { BrowserPanel, type BrowserSignIn } from "./BrowserPanel";
import { SignInBanner } from "./SignInRequestCard";
import { openProviderDesktop } from "./computer-takeover";
-import { panelErrorText } from "@/lib/computer-messages";
+import { LocalizedPanelError, panelErrorText } from "@/lib/computer-messages";
function viewerAddress(raw: unknown): string {
if (typeof raw !== "string" || !raw) throw new Error(t("remoteDesktop.noLink"));
@@ -189,7 +189,12 @@ export function RemoteDesktopPanel({ bot }: { bot: Bot }) {
method: "POST", body: JSON.stringify({ action, controlLeaseId, ...(target ? { target } : {}) }),
}),
join: async (signal, controlLeaseId) => {
- const path = localVm ? `local-computer/join?${new URLSearchParams({ controlLeaseId })}` : "computer/join";
+ // The host binds a Local VM viewer to the lease holding it; a hold another view owns can't be joined from here.
+ let path = "computer/join";
+ if (localVm) {
+ if (!controlLeaseId) throw new LocalizedPanelError("vm.workspace.alreadyControlled");
+ path = `local-computer/join?${new URLSearchParams({ controlLeaseId })}`;
+ }
return { joinUrl: viewerAddress((await api(`/api/bots/${bot.id}/${path}`, { method: "POST", body: "{}", signal })).joinUrl) };
},
});