Skip to content

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

The move to My Cloud only ever uses this computer's own data folder (#5020)

Milind Sonia7cf31902456
diff --git a/electron/clean-workspace.mjs b/electron/clean-workspace.mjs
index 1694e3b159..ac0dabc66b 100644
--- a/electron/clean-workspace.mjs
+++ b/electron/clean-workspace.mjs
@@ -11,10 +11,12 @@
// profile, one folder per profile under one folder beside the data folder, named by its stable id. It is fresh, the same
// swap opens it, and this computer stays lent to their Cloud while it runs, since that account is the one lending answers to.
//
-// Security: the move's code knows one server, this computer's own copy. A top-up, a send or a lift pointed at the clean
-// workspace would upload it over the person's Cloud or report a freeze lifted that wasn't, so while the clean workspace runs
-// `realCopy` answers only what the copy's own files can say, and refuses the rest; `holding` brings the copy back first for
-// the two things that must reach it (lifting the freeze, forgetting the copy).
+// Security: the move's code knows one server, this computer's own copy, the one on its own data folder. A move, a top-up, a
+// send or a lift pointed at any other folder's server (the clean workspace, a profile, a named local environment) would
+// carry and freeze that folder instead, upload it over the person's Cloud, or report a freeze lifted that wasn't, so while
+// any other folder runs `realCopy` answers only what the copy's own files can say, and refuses the rest. `holding` brings
+// the copy back first from the clean workspace or a profile for the two things that must reach it (lifting the freeze,
+// forgetting the copy); a named local environment the person chose stays, and those wait until they open the copy again.
import nodeFs from "node:fs";
import path from "node:path";
@@ -63,8 +65,11 @@ export function createCleanWorkspace({ dataDir, running, swap, allowed = () => t
};
const active = () => stranded() || profile() !== null;
let held = 0, swapping = false;
- /** This computer's own copy is the server that answers now: not the clean workspace, and not on its way between them. */
- const ownCopy = () => !active() && !swapping;
+ /** `dir` is this computer's own data folder, however it is spelled; no folder at all (no server) is not. */
+ const own = (dir) => typeof dir === "string" && dir !== "" && path.resolve(dir) === path.resolve(dataDir());
+ /** This computer's own copy is the server that answers now: its server runs on the data folder, and isn't on its way to
+ * or from another. */
+ const ownCopy = () => !swapping && own(running());
let queue = Promise.resolve();
/** One swap at a time: a second would stop the server the first just started. */
const inTurn = (work) => {
@@ -113,7 +118,7 @@ export function createCleanWorkspace({ dataDir, running, swap, allowed = () => t
enter: () => inTurn(async () => {
if (active()) return true;
if (!allowed() || held > 0) return false;
- if (running() !== dataDir()) return true;
+ if (!own(running())) return true;
try { seed(folder()); } catch (error) { log(`clean workspace: could not set it up (${error?.message ?? error})`); return false; }
return swapTo(folder());
}),
@@ -135,7 +140,7 @@ export function createCleanWorkspace({ dataDir, running, swap, allowed = () => t
return await work();
} finally { held--; }
},
- /** `request` (this computer's server) as the move may use it: while the clean workspace runs, what the frozen copy's own
+ /** `request` (this computer's server) as the move may use it: while any other folder runs, what the frozen copy's own
* files say, and everything else refused. */
realCopy(request) {
return async (route, init) => {
diff --git a/electron/clean-workspace.node-test.mjs b/electron/clean-workspace.node-test.mjs
index a976a3d8d9..8d9765e37d 100644
--- a/electron/clean-workspace.node-test.mjs
+++ b/electron/clean-workspace.node-test.mjs
@@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { dirname, join, sep } from "node:path";
import test from "node:test";
import { cleanWorkspaceConfig, cleanWorkspaceDir, createCleanWorkspace, DEFAULT_PROFILE, profileDir, profilesDir } from "./clean-workspace.mjs";
@@ -341,3 +341,62 @@ test("lending goes on from a profile: the sign-in alone decides, and only the wo
state = { status: "signed-out", account: null, machine: null };
assert.notDeepEqual(cloudLendingVerdict(grant, target.cloud(env), env), { allow: true }, "signed out: nothing is lent, profile or not");
});
+
+/** A named local environment's folder: anywhere the person chose, neither the clean workspace nor under the profiles' folder. */
+const workFolder = (w) => join(dirname(w.dataDir), "work-environment");
+const onWork = (w) => w.requests.filter(([folder]) => folder === workFolder(w));
+
+test("a named local environment is not this computer's own copy: the move gets the copy's own marker and identity, nothing else", async (t) => {
+ const w = computer(t);
+ const request = w.clean.realCopy(w.request);
+ w.running = workFolder(w);
+ assert.deepEqual([w.clean.active(), w.clean.profile(), w.clean.ownCopy()], [false, null, false]);
+ assert.deepEqual(await (await request("/api/move/freeze")).json(), { frozen: true, cloud: CLOUD });
+ assert.deepEqual(await (await request("/.well-known/openmausbot/environment")).json(), { environmentId: "own-copy-id" }, "the Admin never hears the environment's identity as this computer's");
+ for (const [route, init] of [["/api/bots?messages=0"], ["/api/cloud-send?move=1"], ["/api/move/freeze", { method: "POST" }], ["/api/move/freeze", { method: "DELETE" }], ["/api/move/forget", { method: "POST" }]]) {
+ await assert.rejects(request(route, init), { code: "clean-workspace" }, `${init?.method ?? "GET"} ${route}`);
+ }
+ await assert.rejects(w.clean.holding(() => request("/api/move/forget", { method: "POST" })), { code: "clean-workspace" });
+ assert.deepEqual(w.swaps, [], "the environment the person chose stays open");
+ assert.deepEqual(w.requests, [], "nothing reached its server");
+});
+
+test("only the data folder itself is this computer's own copy: however it is spelled, and never with no server at all", (t) => {
+ const w = computer(t);
+ w.running = `${w.dataDir}${sep}`;
+ assert.equal(w.clean.ownCopy(), true, "a trailing separator is the same folder");
+ w.running = join(w.dataDir, "..", ".openmausbot");
+ assert.equal(w.clean.ownCopy(), true);
+ w.running = null;
+ assert.equal(w.clean.ownCopy(), false);
+ w.running = `${w.dataDir}-work`;
+ assert.equal(w.clean.ownCopy(), false, "a folder beside it is another folder");
+});
+
+test("no first move from a named local environment: nothing is carried or frozen, the Admin hears why, and it starts from the data folder", async (t) => {
+ const w = moved(t, { state: ready(), journal: null, marker: null });
+ w.running = workFolder(w);
+ await w.main.kick();
+ assert.equal(w.journal, null, "no move started");
+ assert.deepEqual(onWork(w), []);
+ assert.equal(w.main.oneHome.facts().noted, "local_unavailable");
+ assert.deepEqual([w.main.moveStatus.read().phase, w.main.moveStatus.read().reason], ["none", "local_unavailable"]);
+ assert.equal(w.main.view(), null, "no line says the bots are moving");
+ w.running = w.dataDir;
+ await w.main.kick();
+ assert.equal(w.journal?.v, 1, "this computer's own copy running, the move starts");
+ assert.deepEqual(w.requests.find(([, , route]) => route.startsWith("/api/cloud-send")), [w.dataDir, "GET", "/api/cloud-send?move=1"]);
+ assert.deepEqual(onWork(w), []);
+});
+
+test("a named local environment is never forgotten in place of this computer's copy, nor topped up from", async (t) => {
+ const w = moved(t, { state: ready(), journal: done({ doneAt: 2 }) });
+ w.running = workFolder(w);
+ await w.main.kick();
+ assert.deepEqual(onWork(w), []);
+ assert.equal(w.journal.forgot, undefined, "deleted later, from the copy itself");
+ assert.equal(w.journal.version, "0.1.140", "the top-up waits too");
+ w.running = w.dataDir;
+ await w.main.kick();
+ assert.deepEqual(w.requests.filter(([, , route]) => route === "/api/move/forget"), [[w.dataDir, "POST", "/api/move/forget"]]);
+});
diff --git a/electron/one-home.d.mts b/electron/one-home.d.mts
index 142f03feb5..96725c7d56 100644
--- a/electron/one-home.d.mts
+++ b/electron/one-home.d.mts
@@ -86,7 +86,8 @@ export function createOneHome(deps: {
openHome?(): void;
openLocal?(): void;
lastInput?(): number;
- /** Whether this computer's own copy is the server answering now. */
+ /** Whether this computer's own copy, the server on its own data folder, is the one answering now: while it isn't, no first
+ * move starts and no top-up goes. */
ownCopy?(): boolean;
again?: { bring(): Promise<{ restored: string; switches: Record<string, boolean> } | { deleted: true } | { later: number }> };
/** The person's choice to keep this computer's bots here. */
diff --git a/electron/one-home.mjs b/electron/one-home.mjs
index 7a8a5325c3..50247a46e4 100644
--- a/electron/one-home.mjs
+++ b/electron/one-home.mjs
@@ -194,8 +194,9 @@ export function createOneHome(deps) {
const log = deps.log ?? (() => {});
const t = deps.t ?? nativeText.englishText.t;
let running = null, again = false, timer = null, closed = false, waitingFor = null, waitingWhat = null, wasReady = null, reportFailedAt = -Infinity, keptFailedAt = -Infinity;
- /** What the last run found holding the move that only a run can find out. */
- let noted = null;
+ /** What the last run found holding the move that only a run can find out; `heldOff`, a first move waits for this
+ * computer's own copy, said once in the log. */
+ let noted = null, heldOff = false;
const read = () => parseJournal(deps.journal.read());
const kept = () => { try { return parseKeep(deps.keep?.read()).on; } catch { return false; } };
@@ -658,6 +659,10 @@ export function createOneHome(deps) {
// A frozen server with no move under way is one this app no longer knows about: it is never lifted blindly.
noted = await deps.local.frozen().then((frozen) => (frozen ? "frozen_without_move" : null), () => "local_unavailable");
if (noted) { log(`one home: ${noted === "frozen_without_move" ? "this computer is frozen with no move under way" : "this computer's server did not say whether it is frozen"}`); return; }
+ // Only from this computer's own copy: while another folder's server runs (the clean workspace, a profile, a named
+ // local environment) nothing starts or counts as failing, and it looks again in a minute.
+ if (deps.ownCopy?.() === false) { noted = "local_unavailable"; if (!heldOff) log("one home: the move waits until this computer's own copy runs"); heldOff = true; later(MINUTE); return; }
+ heldOff = false;
journal = write({ v: 1, phase: "copy", cloud: { origin: cloud.origin, account: cloud.account, machine: cloud.machine, ...(cloud.name ? { name: cloud.name } : {}) }, startedAt: now() });
}
if (journal.thaw && !(await thaw(journal))) return;