Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(desktop): stuck on "Opening My Cloud…" after moving bots to a server
Dantebb8ec81b4292
diff --git a/electron/fixtures/home-window-world.mjs b/electron/fixtures/home-window-world.mjsindex 26fd30a4ac..9c073f13a0 100644--- a/electron/fixtures/home-window-world.mjs+++ b/electron/fixtures/home-window-world.mjs@@ -27,6 +27,9 @@ export function world({ clean = null, journal = done(), marker = null, state = c/** Another Cloud of the person's own, answering at CLIENT; `gone`: the Admin's pairing says it is no longer theirs. */client: { down: false, signedIn: true, version: "0.1.130", health: 200, gone: false, ...client },went: [], calls: [], fetched: [], pairs: 0, teamPairs: [], ownPairs: [], wakes: 0, waking: [], splash: [], published: [], local: [], reloads: 0, signIns: [],+ /** The servers this computer's bots moved to, asked for again (`reopen`), and what main answers. `reopenGate`: a+ * recovery whose navigation is still loading. */+ reopened: [], reopenAnswer: true, reopenGate: null,remembered, remembers: [], left: [], dropped: [], redeemed: [], rehomes: [], rehomeThrows: false,url, timers: [], intervals: [],/** Remove from list on the Admin: each Cloud asked about, and what the Admin answers (`removed` takes it off the session's list). */@@ -102,6 +105,7 @@ export function world({ clean = null, journal = done(), marker = null, state = cpower: { asleep: () => w.power?.state === "asleep", hold: () => { w.power = { state: "asleep", since: 1 }; }, woke: () => w.calls.push("woke") },go: async (url, origin, place) => { w.went.push(place?.kind === "team" || place?.kind === "own" ? [url, origin, place.id] : [url, origin]); w.url = url; if (w.stall(url)) await new Promise(() => {}); },showLocal: (leaving) => { w.local.push(leaving); if (leaving) w.url = `${LOCAL}/`; },+ reopen: async (place) => { w.reopened.push(place); if (w.reopenGate) await w.reopenGate; if (!w.reopenAnswer) return false; w.url = `${place.origin}/`; return true; },remembered: () => w.remembered,remember: (place) => { w.remembers.push(place); w.remembered = place ? { ...place, name: ACME.name, origin: TEAM } : null; },left: (name) => w.left.push(name),diff --git a/electron/home-window.d.mts b/electron/home-window.d.mtsindex 50a1439265..da6147ba4b 100644--- a/electron/home-window.d.mts+++ b/electron/home-window.d.mts@@ -31,7 +31,10 @@ export interface HomeBridge {updating?: boolean;}-export type Home = { kind: "computer" } | { kind: "cloud"; origin: string } | { kind: "signin"; other?: true };+export type Home = ({ kind: "computer" } | { kind: "cloud"; origin: string } | { kind: "signin"; other?: true })+ /** This computer's copy is frozen by a move to a server of the person's own, whose marker names it: that server, and+ * its name. The copy's own page has nothing to show, so the window goes back to it (`reopen`). */+ & { frozenPlace?: { origin: string | null; place: string } };/** A place the window shows: My Cloud, or a team's Cloud or another Cloud of the person's own (`own`), from the verified session. */export type HomePlace = { kind: "cloud"; origin: string } | { kind: "team" | "own"; id: string; name: string; role: string | null; origin: string };/** What to open: My Cloud, a team's Cloud, another Cloud of the person's own, or (null) the place remembered from last time. */@@ -99,7 +102,7 @@ export interface HomeWindow {export function createHomeWindow(deps: {journal(): unknown;- frozen(): Promise<{ frozen: boolean; cloud?: string } | null>;+ frozen(): Promise<{ frozen: boolean; cloud?: string; place?: string } | null>;account(): { state(): any; power(): any; wakeHome(): Promise<unknown>; setWaking(on: boolean): void; pairHome(cloudId?: string | null): Promise<{ origin: string; code: string; expiresAt: number }>;pairTeam?(teamId: string): Promise<{ origin: string; code: string; expiresAt: number }>; signInAgain(): Promise<unknown>; begin(): Promise<unknown>; signOut?(): Promise<unknown>; reopen?(): Promise<unknown>;hideCloud?(cloudId: string): Promise<{ outcome: import("./cloud-account.mjs").CloudRemoveOutcome }> } | null;@@ -110,6 +113,7 @@ export function createHomeWindow(deps: {cookieFetch: (url: string, init?: RequestInit) => Promise<Response>;power: { asleep(): boolean; hold(): void; woke(): void };go(url: string, origin: string | null, place: HomePlace | null): unknown;+ reopen?(place: { origin: string | null; place: string }, still?: () => boolean): Promise<boolean>;showLocal(leaving: boolean): unknown;clean?: import("./clean-workspace.mjs").CleanWorkspace | null;remembered?(): RememberedPlace;diff --git a/electron/home-window.mjs b/electron/home-window.mjsindex f42b21a5db..6d60c9137c 100644--- a/electron/home-window.mjs+++ b/electron/home-window.mjs@@ -210,6 +210,8 @@ export function routeOf(url, origin) {* became ready (createReadyClock). `go(url, origin, place)`: main's one way to move the window, to My Cloud or a team's Cloud (`origin`, with the* `place` it is) or this computer's screen (null). `showLocal(leaving)`: the window as it was before the move (this computer's page, or a saved server it* shows); `leaving`: it showed a place or this computer's screen until now, so it goes back to this computer's page.+ * `reopen(place, still)`: this computer's copy moved to a server of the person's own, so the window goes back to that+ * server (`origin`, `place`); false when it isn't saved here any more, doesn't answer, or `still()` says a newer `show()` took over.* `frozen()`: this computer's server's freeze marker. `splash()`: the startup screen while it is up. `remembered()`: the* team or other Cloud shown last time, with its name and origin as saved; `remember(place)`: what to open next time;* `left(name)`: the team shown is no longer this person's, so the window says so once; `drop(place)`: the Admin said so@@ -218,6 +220,7 @@ export function routeOf(url, origin) {*/export function createHomeWindow(deps) {const { journal, frozen, account, restored = async () => {}, window, ensureWindow, localOrigin, cookieFetch, power, go, showLocal, clean = null,+ reopen = async () => false,remembered = () => null, remember = () => {}, left = () => {}, drop = (place) => { if (place.kind === "team") left(place.name); remember(null); },splash = () => null, lastInput = () => 0, publish = () => {}, rehomed = () => {}, versionEveryMs = VERSION_EVERY_MS, quietMs = QUIET_MS,now = Date.now, timers = DEFAULT_TIMERS, hosts = () => null, readyStore = null, ownMove = () => null, log = () => {} } = deps;@@ -247,17 +250,19 @@ export function createHomeWindow(deps) {async function refresh() {clock.seen(state());const kept = parseJournal(journal());- let frozenCloud = null;+ let frozenCloud = null, frozenPlace = null;if (!kept) {const marker = await frozen().catch(() => null);// A freeze for a server of the person's own names it (`place`): home is that server, shown as any saved server is.if (marker?.frozen && !marker.place) frozenCloud = typeof marker.cloud === "string" ? marker.cloud : true;+ else if (marker?.frozen && marker.place) frozenPlace = { origin: typeof marker.cloud === "string" ? marker.cloud : null, place: String(marker.place) };}if (frozenCloud || awayIn(kept)) await restored();const before = home;const worked = homeOf({ journal: kept, frozenCloud, state: state() });// Home is this computer then, in its clean workspace, so a Cloud of this account's own opens from it as for anyone.home = clean && noWayHome(worked, state()) ? { kind: "computer", clean: true, ...(worked.other ? { other: true } : {}) } : worked;+ if (frozenPlace) home = { ...home, frozenPlace };if (home.kind !== before.kind || home.origin !== before.origin || home.clean !== before.clean) {try { rehomed(home); } catch {}}@@ -530,6 +535,22 @@ export function createHomeWindow(deps) {asked = null; naming = null;}if (current.kind === "computer") {+ // A copy frozen by a move to a server of the person's own is never shown; a move under way keeps its own screen.+ const parkedAt = current.frozenPlace;+ if (!current.clean && parkedAt && !ownMove() && !(parkedAt.origin && shows(parkedAt.origin))) {+ clearRetry(); screen = null; status = null; shown = null; away = false;+ ensureWindow();+ let slow = null;+ const back = await Promise.race([+ reopen(parkedAt, () => wanted(token)).catch(() => false),+ new Promise((done) => { slow = timers.set(() => done(false), OPEN_MS); }),+ ]);+ timers.clear(slow);+ if (!wanted(token)) return;+ if (back) return;+ toScreen({ screen: "offline", reason: `Your bots are on ${parkedAt.place}.` });+ return later();+ }if (current.clean && !(await clean.enter())) { if (wanted(token)) { ensureWindow(); clearRetry(); toScreen({ screen: "signin", ...(current.other ? { other: true } : {}) }); } return; }if (!wanted(token)) return;clearRetry(); screen = null; status = null; shown = null;@@ -697,6 +718,15 @@ export function createHomeWindow(deps) {* computer's screen says so and the window tries again by itself. False for any other server. */failed(url, code = 0) {const at = originOf(url), place = placeOrigin(), mine = readyCloudOrigin(state());+ // The window's own way back to a frozen copy's server (a recovery at launch): its page didn't load, so this+ // computer's screen says where the bots are and looks again, never the generic dialog and the way to Local.+ const parkedAt = home.frozenPlace;+ if (!place && parkedAt?.origin && at === parkedAt.origin) {+ ++opens;+ toScreen({ screen: "offline", reason: `Your bots are on ${parkedAt.place}.` });+ later();+ return true;+ }if (!at || (at !== place && (home.kind !== "computer" || at !== mine))) return false;if (at !== place) asked ??= { open: null };const token = ++opens;diff --git a/electron/home-window.node-test.mjs b/electron/home-window.node-test.mjsindex bfe1e502aa..5e3bd2d096 100644--- a/electron/home-window.node-test.mjs+++ b/electron/home-window.node-test.mjs@@ -9,9 +9,12 @@ import { cloudPageSenderAllowed, myCloudOrigin } from "./cloud-home.mjs";import { otherOwnClouds } from "./cloud-list.mjs";import environments from "./environments.cjs";import localOrigin from "./local-origin.cjs";-import { HOME_SCREEN_PATH, homeOf, installHomeWindowIpc, LOOK_MS, placeOf, QUIET_MS, routeOf, SPLASH_TEXT, splashText, TEAM_RENEW_EARLY_MS, TEAM_RENEW_MS, TEAM_RENEW_RETRY_MS, UPDATING_MS } from "./home-window.mjs";+import { HOME_SCREEN_PATH, homeOf, installHomeWindowIpc, LOOK_MS, OPEN_MS, placeOf, QUIET_MS, routeOf, SPLASH_TEXT, splashText, TEAM_RENEW_EARLY_MS, TEAM_RENEW_MS, TEAM_RENEW_RETRY_MS, UPDATING_MS } from "./home-window.mjs";import { ACCOUNT, ACME, CLIENT, CLIENT_A, CLOUD, connected, done, LOCAL, TEAM, world } from "./fixtures/home-window-world.mjs";+/** Everything already queued runs before this comes back: what the fake clock's callbacks left to flush. */+const tick = () => new Promise((resolve) => setImmediate(resolve));+test("home is My Cloud only once the move switched and the same account is signed in; anything else is this computer or the sign-in screen", () => {assert.deepEqual(homeOf({}), { kind: "computer" }, "no Cloud: nothing changes");assert.deepEqual(homeOf({ state: connected() }), { kind: "computer" }, "a Cloud the move hasn't reached is still not home");@@ -49,6 +52,70 @@ test("a server of the person's own their bots moved to is no Cloud: no sign-in,assert.deepEqual(moving.home.view(), { screen: "opening" }, "still home here: nothing is stopped yet");});+test("a frozen copy left on this computer's own page goes back to the server it names, or says where the bots are", async () => {+ const garage = "https://garage.example.test";+ const marker = { frozen: true, cloud: garage, place: "Garage server" };+ // The window opens on this computer's page, and that server is frozen: its page is the home screen, which waits on a+ // home that is not here, so it spins there for ever. The window goes back to the server instead, as any saved server does.+ const parked = world({ journal: null, marker, state: { status: "signed-out" }, url: `${LOCAL}${HOME_SCREEN_PATH}` });+ await parked.home.show();+ assert.deepEqual(parked.reopened, [{ origin: garage, place: "Garage server" }], "the server is asked for again, never the frozen page");+ assert.equal(parked.url, `${garage}/`, "the window is on the server");+ assert.deepEqual(parked.local, [], "this computer's frozen page is never shown");+ assert.deepEqual(parked.calls, ["ensure"], "a window is there to open it in");+ assert.deepEqual(parked.home.view(), { screen: "opening" }, "no screen of its own while the server opens");+ // A server forgotten here, or one that doesn't answer: the screen says where the bots are, and looks again by itself.+ const forgotten = world({ journal: null, marker, state: { status: "signed-out" }, url: `${LOCAL}${HOME_SCREEN_PATH}` });+ forgotten.reopenAnswer = false;+ await forgotten.home.show();+ assert.deepEqual(forgotten.home.view(), { screen: "offline", reason: "Your bots are on Garage server." });+ assert.equal(forgotten.timers.length, 1, "it looks again by itself");+ // The second try lands on the server, and the next show() leaves the window there, never back on the frozen page.+ forgotten.reopenAnswer = true;+ await forgotten.home.show();+ assert.equal(forgotten.url, `${garage}/`, "the retry opened the server");+ await forgotten.home.show();+ assert.deepEqual(forgotten.local, [false], "already on the server: nothing moves the window back");+ // A show() that overlaps the recovery, while its page is still loading, leaves the window where it is going.+ const racing = world({ journal: null, marker, state: { status: "signed-out" }, url: `${LOCAL}${HOME_SCREEN_PATH}` });+ racing.reopenAnswer = false;+ await racing.home.show();+ const before = racing.reopened.length;+ racing.reopenAnswer = true;+ let release = () => {};+ racing.reopenGate = new Promise((done) => { release = done; });+ const recovering = racing.home.show();+ for (let i = 0; i < 200 && racing.reopened.length === before; i++) await tick();+ racing.url = `${garage}/`;+ await racing.home.show();+ release();+ await recovering;+ assert.equal(racing.url, `${garage}/`, "an overlapping show() never pulls the window back to the frozen page");+ assert.deepEqual(racing.local, [false], "it leaves the window where the recovery is taking it");+ // The recovery's own page failing to load is this app's to answer, not main's generic "not reachable" dialog.+ const loading = world({ journal: null, marker, state: { status: "signed-out" }, url: `${LOCAL}${HOME_SCREEN_PATH}` });+ await loading.home.show();+ assert.equal(loading.home.failed(`${garage}/chats`, -105), true, "the way back's load failure is answered here");+ assert.deepEqual(loading.home.view(), { screen: "offline", reason: "Your bots are on Garage server." });+ // A recovery whose page never commits is bounded: the screen says where the bots are, with a retry, within OPEN_MS.+ const stuck = world({ journal: null, marker, state: { status: "signed-out" }, url: `${LOCAL}${HOME_SCREEN_PATH}` });+ stuck.reopenGate = new Promise(() => {});+ let back = false;+ const waiting = stuck.home.show().then(() => { back = true; });+ for (let i = 0; i < 200 && stuck.timers.length === 0; i++) await tick();+ await stuck.later(OPEN_MS);+ await tick();+ assert.equal(back, true, "the deadline hands show() back");+ await waiting;+ assert.deepEqual(stuck.home.view(), { screen: "offline", reason: "Your bots are on Garage server." }, "the screen says where they are");+ assert.equal(stuck.timers.length, 1, "and looks again by itself");+ // A move still under way is that screen's to say, not this one's.+ const moving = world({ journal: null, marker, state: { status: "signed-out" }, ownMove: { phase: "final", name: "Garage server" }, url: `${LOCAL}${HOME_SCREEN_PATH}` });+ await moving.home.show();+ assert.deepEqual(moving.reopened, [], "an unfinished move is not opened");+ assert.deepEqual(moving.home.view(), { screen: "moving", name: "Garage server" });+});+test("a sign-in keeps only a path of My Cloud's own", () => {assert.equal(routeOf(`${CLOUD}/chats/b1?view=2#x`, CLOUD), "/chats/b1?view=2");assert.equal(routeOf(`${CLOUD}/`, CLOUD), null);diff --git a/electron/main.mjs b/electron/main.mjsindex 182e4e4a5f..9499ba5cca 100644--- a/electron/main.mjs+++ b/electron/main.mjs@@ -104,7 +104,7 @@ import { cloudPageSenderAllowed, cloudPlanDisk, cloudUnreachableText, createClouimport { installCloudFiles } from "./cloud-sync-main.mjs";import { createCloudEntry } from "./cloud-entry.mjs";import { createLendingTarget } from "./lending-target.mjs"; import { createOpenAtLogin, installOpenAtLogin } from "./open-at-login.mjs";-import { createCleanWorkspace } from "./clean-workspace.mjs"; import { installOneHome } from "./one-home-main.mjs"; import { handSaved, installOwnHome } from "./own-home-main.mjs"; import { serverOpens } from "./saved-server.mjs"; import { installTwoPlacesMain } from "./two-places-main.mjs";+import { createCleanWorkspace } from "./clean-workspace.mjs"; import { installOneHome } from "./one-home-main.mjs"; import { handSaved, installOwnHome } from "./own-home-main.mjs"; import { reopenSavedServer, serverOpens } from "./saved-server.mjs"; import { installTwoPlacesMain } from "./two-places-main.mjs";import { askForMicrophone, openPrivacySettings } from "./privacy-settings.mjs";import { createHomeWindow, installHomeWindowIpc } from "./home-window.mjs";import { createTeamProfileSeeds } from "./team-profile-seed.mjs";@@ -2667,12 +2667,8 @@ const homeWindow = createHomeWindow({ clean: cleanWorkspace, journal: oneHome.joremembered: homePlaces.remembered, remember: homePlaces.remember, left: homePlaces.left, drop: homePlaces.drop,publish: view => { if (mainWindow && !mainWindow.isDestroyed() && senderIsLocal({ sender: mainWindow.webContents })) mainWindow.webContents.send("home:state-changed", view); },go: (url, origin, place) => { if (place?.kind === "team" || place?.kind === "own") { activatePlace(place); if (place.kind === "team") teamProfileSeeds.opened(place.origin); } else if (origin) activateCloud(origin); else if (activeEnvironment(environmentsState)) persistEnvironments(withActive(environmentsState, LOCAL_ID)); return navigateMainWindow(url); },- showLocal: leaving => {- if (!mainWindow || mainWindow.isDestroyed()) return createWindow();- if (!leaving) return;- persistEnvironments(withActive(environmentsState, LOCAL_ID));- return navigateMainWindow(rendererOrigin());- } });+ reopen: (place, still) => reopenSavedServer(place, { still, saved: () => environmentsState.environments, fetch: (...args) => session.defaultSession.fetch(...args), opened: async (entry) => { homePlaces.remember(null); persistEnvironments(withActive(environmentsState, entry.id)); return navigateMainWindow(activeOrigin()); } }),+ showLocal: leaving => { if (!mainWindow || mainWindow.isDestroyed()) return createWindow(); if (!leaving) return; persistEnvironments(withActive(environmentsState, LOCAL_ID)); return navigateMainWindow(rendererOrigin()); } });installHomeWindowIpc({ ipcMain, homeWindow, cloudPage: cloudPageAsking });/** Every path that shows the window: My Cloud once it is the person's home, else as before. */function showHome(options) {diff --git a/electron/saved-server.mjs b/electron/saved-server.mjsindex a267297cec..fb42785ccf 100644--- a/electron/saved-server.mjs+++ b/electron/saved-server.mjs@@ -34,3 +34,18 @@ export async function serverOpens(entry, { fetch: fetchImpl, dialog, forget, timif (response !== 0) return false;}}++/**+ * The window's way back to a server this computer's bots moved to: opens it again as the Server menu does, without that+ * menu's dialog, because this is the app putting itself right and the screen it lands on has Try again. False when the+ * server isn't saved here any more, nothing answers at it, or `still()` says a newer `show()` took over.+ * @param {{ origin: string | null }} place+ * @param {{ saved(): Array<{ id: string, origin: string }>, fetch: typeof fetch, still?(): boolean, opened(entry: object): unknown }} deps+ */+export async function reopenSavedServer({ origin }, { saved, fetch: fetchImpl, still = () => true, opened }) {+ const entry = origin ? saved().find((candidate) => candidate.origin === origin) ?? null : null;+ if (!entry) return false;+ if (!(await serverAnswers(entry.origin, fetchImpl))) return false;+ if (!still()) return false;+ return Boolean(await opened(entry));+}