Skip to content

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

Merge upstream main 7fb1f79b1 into sharing pause fix

Sebastian B1000cf249259
diff --cc electron/computer-sharing.mjs
index 5735d507f7,d3789af011..0eabe9372f
--- a/electron/computer-sharing.mjs
+++ b/electron/computer-sharing.mjs
@@@ -198,23 -201,8 +201,24 @@@ export function createComputerSharing(
const parsed = JSON.parse(fs.readFileSync(file, "utf8"));
if (parsed?.version === 1 && parsed.records && typeof parsed.records === "object") records = parsed.records;
paused = parsed?.automatic ? parsed.automatic.paused === true : Object.values(records).some(saidNo);
+ if (typeof parsed?.automatic?.terminalAccount === "string") terminalAccount = parsed.automatic.terminalAccount;
} catch {}
+ const sessionMarker = `${file}.lending-session`;
+ let sessionGuarded = false, pauseError;
+ // Reserve recovery state before lending: a failed Pause write must survive an app exit, even on a full disk.
+ const guardSession = () => {
+ if (sessionGuarded) return;
+ fs.mkdirSync(path.dirname(file), { recursive: true });
+ fs.mkdirSync(sessionMarker);
+ sessionGuarded = true;
+ };
+ try { guardSession(); } catch (error) {
+ if (error.code === "EEXIST") {
+ sessionGuarded = true;
+ if (!paused) pauseError = "Lending stays paused because the previous session did not finish saving its state. Retry saving pause or choose Resume.";
+ } else pauseError = "Lending stays paused because restart protection could not be saved. Retry saving pause or choose Resume.";
+ paused = true;
+ }
const running = new Map();
const grantVersions = new Map();
const changeGrant = id => { const version = (grantVersions.get(id) ?? 0) + 1; grantVersions.set(id, version); return version; };
@@@ -229,15 -217,14 +233,16 @@@
const changed = () => { if (!closed) try { onChange(summary()); } catch {} };
const summary = () => {
const browsing = [...inUse.keys()].find(id => linked.has(id));
- const lent = paused ? [] : environments().filter(env => records[env.id]?.cloud && records[env.id]?.enabled === true);
- const using = lent.filter(env => executing?.env === env.id || (inUse.has(env.id) && linked.has(env.id)));
+ const lent = environments().filter(env => records[env.id]?.cloud && records[env.id]?.enabled === true);
+ const clouds = paused ? new Map([...ownClouds(), ...lent].map(env => [env.id, env])).values() : lent;
+ const using = paused ? [] : lent.filter(env => executing?.env === env.id || (inUse.has(env.id) && linked.has(env.id)));
return {
- lending: lent.map(env => env.id),
- clouds: (paused ? ownClouds() : lent).map(env => env.name),
+ lending: paused ? [] : lent.map(env => env.id),
+ clouds: [...clouds].map(env => env.name),
using: using.map(env => env.name),
paused,
+ ...(pauseError ? { pauseError } : {}),
+ terminal: terminalAccount !== null && terminalAccount === signedInAccount(),
busy: executing ?? (browsing ? { env: browsing, action: "internet" } : null),
};
};
diff --cc electron/lending-indicator.mjs
index 87c9abd3b3,2600f377b0..fbc8a604c6
--- a/electron/lending-indicator.mjs
+++ b/electron/lending-indicator.mjs
@@@ -24,14 -24,16 +25,17 @@@ export function createLendingIndicator(
tray.setContextMenu(Menu.buildFromTemplate([
{ label: line, enabled: false },
{ type: "separator" },
+ ...(view.pauseError ? [{ label: "Retry saving pause", click: () => onPause() }] : []),
view.paused ? { label: "Resume", click: () => onResume() } : { label: "Pause", click: () => onPause() },
+ ...view.paused ? [] : [{ label: `Let Clouds run commands on this ${device}`, type: "checkbox", checked: view.terminal,
+ // Electron 43 ticks a checkbox on click by itself: the menu keeps the saved choice until the person confirms one.
+ click: () => { draw(view); onTerminal(!view.terminal); } }],
{ label: "See activity", click: () => onActivity() },
]));
};
return {
- update({ clouds = [], using = [], paused = false, pauseError }) {
- const next = { clouds: [...clouds], using: paused ? [] : [...using], paused: paused === true, ...(pauseError ? { pauseError } : {}) };
- update({ clouds = [], using = [], paused = false, terminal = false }) {
- const next = { clouds: [...clouds], using: paused ? [] : [...using], paused: paused === true, terminal: terminal === true };
++ update({ clouds = [], using = [], paused = false, terminal = false, pauseError }) {
++ const next = { clouds: [...clouds], using: paused ? [] : [...using], paused: paused === true, terminal: terminal === true, ...(pauseError ? { pauseError } : {}) };
if (shown && JSON.stringify(next) === JSON.stringify(shown)) return;
shown = next;
if (!next.clouds.length) { if (tray && !tray.isDestroyed()) tray.destroy(); tray = null; return; }
diff --cc electron/sharing-persistence.node-test.mjs
index c50bf9c6bd,0000000000..4bb0bf1406
mode 100644,000000..100644
--- a/electron/sharing-persistence.node-test.mjs
+++ b/electron/sharing-persistence.node-test.mjs
@@@ -1,253 -1,0 +1,306 @@@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import fs from "node:fs";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import { randomUUID } from "node:crypto";
+import { spawnSync } from "node:child_process";
+import { setImmediate } from "node:timers/promises";
+import { createComputerSharing } from "./computer-sharing.mjs";
+import { createLendingIndicator } from "./lending-indicator.mjs";
+
+function fixture(t) {
+ const dir = fs.mkdtempSync(path.join(tmpdir(), "omb-sharing-persistence-"));
+ const file = path.join(dir, "computer-sharing.json");
+ const env = { id: "fixture-cloud", origin: "https://cloud.fixture.test", name: "Fixture" };
+ const grant = { id: randomUUID(), secret: "a".repeat(64), sessionId: randomUUID(), environmentId: randomUUID(), enabled: true,
+ folders: [], terminal: false, computer: false, internet: true, chrome: true, cloud: { accountId: "fixture-account", origin: env.origin } };
+ fs.writeFileSync(file, JSON.stringify({ version: 1, records: { [env.id]: grant }, automatic: { paused: false } }));
+ const summaries = [], connects = [], polls = [];
+ const polled = Promise.withResolvers();
+ const controllers = [];
+ const options = {
+ file, home: dir, environments: () => [env],
+ cloud: () => ({ status: "connected", accountId: "fixture-account", origin: env.origin }),
+ onChange: summary => summaries.push(summary),
+ async fetch(url, { signal }) {
+ const route = new URL(url).pathname;
+ if (route === "/api/auth/session") return Response.json({ kind: "session", id: grant.sessionId, cloudHome: true, scopes: ["admin"] });
+ if (route === "/.well-known/openmausbot/environment") return Response.json({ environmentId: grant.environmentId, capabilities: { sharedComputers: true } });
+ if (route === "/api/shared-computers/connect") connects.push(url);
+ if (route.endsWith("/poll")) await new Promise((resolve, reject) => {
+ polls.push(signal);
+ polled.resolve();
+ signal.throwIfAborted();
+ signal.addEventListener("abort", () => reject(signal.reason), { once: true });
+ });
+ return Response.json({ ok: true });
+ },
+ };
+ t.after(() => { for (const controller of controllers) controller.close(); fs.rmSync(dir, { recursive: true, force: true }); });
+ return { file, env, summaries, connects, polls, polled: polled.promise,
+ make(overrides = {}) { const controller = createComputerSharing({ ...options, ...overrides }); controllers.push(controller); return controller; },
+ saved: () => JSON.parse(fs.readFileSync(file, "utf8")),
+ block: () => fs.mkdirSync(`${file}.tmp`),
+ unblock: () => fs.rmdirSync(`${file}.tmp`),
+ };
+}
+
+test("a failed Pause save stops active sharing and reports that the pause is not saved", async t => {
+ const f = fixture(t), sharing = f.make();
+ sharing.start();
+ await f.polled;
+ f.block();
+ const write = fs.writeFileSync;
+ let stoppedBeforeSave;
+ t.mock.method(fs, "writeFileSync", (...args) => {
+ if (args[0] === `${f.file}.tmp`) stoppedBeforeSave = f.polls[0].aborted;
+ return write(...args);
+ });
+ sharing.pause();
+ assert.equal(stoppedBeforeSave, true, "work stops before the save is attempted");
+ assert.equal(sharing.paused(), true);
+ assert.equal(f.polls[0].aborted, true);
+ assert.equal(sharing.lendingToCloud(), false);
+ assert.equal(f.saved().automatic.paused, false);
+ assert.match(f.summaries.at(-1).pauseError, /not saved/i);
+ assert.match(f.summaries.at(-1).pauseError, /next start will stay paused/i);
+ assert.deepEqual(f.summaries.at(-1).clouds, ["Fixture"]);
+});
+
+test("Pause retries a failed save while sharing stays paused", async t => {
+ const f = fixture(t), sharing = f.make();
+ f.block();
+ sharing.pause();
+ f.unblock();
+ sharing.pause();
+ assert.equal(f.saved().automatic.paused, true);
+ assert.equal(f.summaries.at(-1).pauseError, undefined);
+ sharing.close();
+ assert.equal(f.make().paused(), true);
+});
+
+test("a failed Pause save cannot silently reconnect after restart", async t => {
+ const f = fixture(t), sharing = f.make();
+ sharing.start();
+ await f.polled;
+ f.block();
+ sharing.pause();
+ sharing.close();
+ f.unblock();
+ const restarted = f.make();
+ restarted.start();
+ restarted.cloudChanged();
+ assert.equal(restarted.paused(), true);
+ assert.match(f.summaries.at(-1).pauseError, /previous session/i);
+ await setImmediate();
+ assert.equal(f.connects.length, 1);
+ restarted.pause();
+ assert.equal(f.saved().automatic.paused, true);
+ assert.equal(f.summaries.at(-1).pauseError, undefined);
+ await restarted.resume();
+ assert.equal(restarted.paused(), false);
+ assert.equal(f.saved().automatic.paused, false);
+});
+
++test("an unsaved Pause keeps automatic terminal lending stopped across restart", async t => {
++ const f = fixture(t);
++ const options = { ownClouds: () => [f.env] };
++ const sharing = f.make(options);
++ await sharing.lendTerminal(true);
++ await f.polled;
++ assert.equal(f.saved().records[f.env.id].terminal, true);
++ f.block();
++ sharing.pause();
++ assert.throws(() => sharing.lendTerminal(false));
++ sharing.start();
++ sharing.cloudChanged();
++ await sharing.screenChanged();
++ assert.equal(sharing.lendingToCloud(), false);
++ sharing.close();
++ f.unblock();
++ const restarted = f.make(options);
++ restarted.start();
++ restarted.cloudChanged();
++ await restarted.lendTerminal(true);
++ await restarted.screenChanged();
++ assert.equal(restarted.paused(), true);
++ assert.equal(restarted.lendingToCloud(), false);
++ assert.equal(f.connects.length, 1);
++ assert.equal(f.summaries.at(-1).terminal, true);
++ assert.match(f.summaries.at(-1).pauseError, /previous session/i);
++ await restarted.resume();
++ assert.equal(restarted.paused(), false);
++ assert.equal(f.saved().records[f.env.id].terminal, true);
++});
++
++test("a terminal confirmation after a failed Pause save cannot resume lending", async t => {
++ const f = fixture(t), sharing = f.make({ ownClouds: () => [f.env] });
++ sharing.start();
++ await f.polled;
++ f.block();
++ sharing.pause();
++ f.unblock();
++ await sharing.lendTerminal(true);
++ assert.equal(sharing.paused(), true);
++ assert.equal(sharing.lendingToCloud(), false);
++ assert.equal(f.connects.length, 1);
++ assert.equal(f.saved().automatic.paused, true);
++ assert.equal(f.saved().automatic.terminalAccount, "fixture-account");
++ sharing.close();
++ const restarted = f.make({ ownClouds: () => [f.env] });
++ restarted.start();
++ await restarted.screenChanged();
++ assert.equal(restarted.paused(), true);
++ assert.equal(f.connects.length, 1);
++});
++
+test("an unclean exit leaves lending paused with a recovery message on the next start", t => {
+ const f = fixture(t);
+ const child = spawnSync(process.execPath, ["--input-type=module", "--eval", `
+ import { createComputerSharing } from ${JSON.stringify(new URL("./computer-sharing.mjs", import.meta.url).href)};
+ createComputerSharing({ file: ${JSON.stringify(f.file)}, environments: () => [] });
+ process.exit(0);
+ `], { encoding: "utf8" });
+ assert.equal(child.status, 0, child.stderr);
+ const restarted = f.make();
+ restarted.start();
+ assert.equal(restarted.paused(), true);
+ assert.match(f.summaries.at(-1).pauseError, /previous session/i);
+});
+
+test("a clean exit keeps saved active lending available on the next start", t => {
+ const f = fixture(t), sharing = f.make();
+ sharing.close();
+ assert.equal(f.make().paused(), false);
+});
+
+test("lending stays paused when restart protection cannot be reserved, until Resume can save it", async t => {
+ const f = fixture(t);
+ const mkdir = fs.mkdirSync;
+ const blocked = t.mock.method(fs, "mkdirSync", (...args) => {
+ if (args[0] === `${f.file}.lending-session`) throw Object.assign(new Error("disk full"), { code: "ENOSPC" });
+ return mkdir(...args);
+ });
+ const sharing = f.make();
+ sharing.start();
+ assert.equal(sharing.paused(), true);
+ assert.match(f.summaries.at(-1).pauseError, /restart protection/);
+ await sharing.resume();
+ assert.equal(sharing.paused(), true);
+ await setImmediate();
+ assert.equal(f.connects.length, 0);
+ blocked.mock.restore();
+ await sharing.resume();
+ assert.equal(sharing.paused(), false);
+ assert.equal(f.summaries.at(-1).pauseError, undefined);
+});
+
+test("a failed Pause save without restart protection warns that restarting can resume lending", async t => {
+ const f = fixture(t);
+ const mkdir = fs.mkdirSync;
+ const blocked = t.mock.method(fs, "mkdirSync", (...args) => {
+ if (args[0] === `${f.file}.lending-session`) throw Object.assign(new Error("disk full"), { code: "ENOSPC" });
+ return mkdir(...args);
+ });
+ const sharing = f.make();
+ sharing.start();
+ f.block();
+ sharing.pause();
+ const error = f.summaries.at(-1).pauseError;
+ assert.equal(sharing.paused(), true);
+ assert.equal(f.saved().automatic.paused, false);
+ assert.equal(fs.existsSync(`${f.file}.lending-session`), false);
+ sharing.close();
+ f.unblock();
+ blocked.mock.restore();
+ const restarted = f.make();
+ restarted.start();
+ await f.polled;
+ assert.equal(restarted.paused(), false);
+ assert.equal(f.connects.length, 1);
+ assert.match(error, /restart.*(?:can|may).*resume lending/i);
+ assert.match(error, /retry saving pause/i);
+ assert.doesNotMatch(error, /next start will stay paused/i);
+});
+
+test("crash recovery exposes Resume when the first own Cloud is restored after start", async t => {
+ const f = fixture(t);
+ fs.writeFileSync(f.file, JSON.stringify({ version: 1, records: {}, automatic: { paused: false } }));
+ fs.mkdirSync(`${f.file}.lending-session`);
+ let own = [], tray = null;
+ class Tray {
+ constructor() { tray = this; }
+ setToolTip(value) { this.tooltip = value; }
+ setTitle() {}
+ setContextMenu(value) { this.menu = value; }
+ isDestroyed() { return false; }
+ on() {}
+ destroy() { tray = null; }
+ }
+ const indicator = createLendingIndicator({ Tray, Menu: { buildFromTemplate: items => items }, nativeImage: { createFromPath: () => ({ resize: () => ({}) }) },
+ iconPath: "fixture.png", onPause() {}, onResume() {}, onActivity() {} });
+ t.after(() => indicator.destroy());
+ const sharing = f.make({ ownClouds: () => own, onChange: summary => { f.summaries.push(summary); indicator.update(summary); } });
+ sharing.start();
+ assert.equal(tray, null);
+ own = [f.env];
+ sharing.cloudChanged();
+ await sharing.screenChanged();
+ assert.equal(sharing.paused(), true);
+ assert.deepEqual(f.connects, []);
+ assert.deepEqual(f.summaries.at(-1).clouds, ["Fixture"]);
+ assert.ok(tray, "Paused lending needs a tray after Cloud restoration");
+ assert.match(tray.tooltip, /previous session/i);
+ assert.ok(tray.menu.some(item => item.label === "Retry saving pause"));
+ assert.ok(tray.menu.some(item => item.label === "Resume"));
+ own = [{ ...f.env, name: "Renamed" }];
+ sharing.cloudChanged();
+ assert.deepEqual(f.summaries.at(-1).clouds, ["Renamed"]);
+ own = [];
+ sharing.cloudChanged();
+ assert.equal(tray, null);
+});
+
+test("Resume stays paused if its save fails and can be retried", async t => {
+ const f = fixture(t), sharing = f.make();
+ sharing.pause();
+ f.block();
+ await sharing.resume();
+ assert.equal(sharing.paused(), true);
+ assert.equal(sharing.lendingToCloud(), false);
+ assert.equal(f.saved().automatic.paused, true);
+ assert.match(f.summaries.at(-1).pauseError, /resume/i);
+ f.unblock();
+ await sharing.resume();
+ assert.equal(sharing.paused(), false);
+ assert.equal(f.saved().automatic.paused, false);
+ assert.equal(f.summaries.at(-1).pauseError, undefined);
+});
+
+test("the tray reports an unsaved pause and offers a save retry alongside Resume", () => {
+ let tray, retries = 0;
+ class Tray {
+ constructor() { tray = this; }
+ setToolTip(value) { this.tooltip = value; }
+ setTitle(value) { this.title = value; }
+ setContextMenu(value) { this.menu = value; }
+ isDestroyed() { return false; }
+ on() {}
+ destroy() {}
+ }
+ const indicator = createLendingIndicator({ Tray, Menu: { buildFromTemplate: items => items }, nativeImage: { createFromPath: () => ({ resize: () => ({}) }) },
+ iconPath: "fixture.png", onPause: () => retries++, onResume() {}, onActivity() {} });
+ try {
+ indicator.update({ clouds: ["Fixture"], using: [], paused: true });
+ indicator.update({ clouds: ["Fixture"], using: [], paused: true, pauseError: "Pause is not saved. Retry saving pause." });
+ assert.match(tray.tooltip, /not saved/);
+ assert.match(tray.title, /save/i);
+ const retry = tray.menu.find(item => item.label === "Retry saving pause");
+ assert.ok(retry);
+ retry.click();
+ assert.equal(retries, 1);
+ assert.ok(tray.menu.some(item => item.label === "Resume"));
++ assert.equal(tray.menu.some(item => item.type === "checkbox"), false);
+ } finally { indicator.destroy(); }
+});