Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Lending: a pass under way when the account is left lends nothing more
milind-soni716ced132de0
diff --git a/electron/computer-sharing.mjs b/electron/computer-sharing.mjsindex 468ad6752c..7dc05ef80d 100644--- a/electron/computer-sharing.mjs+++ b/electron/computer-sharing.mjs@@ -621,8 +621,11 @@ export function createComputerSharing({ file, fetch: fetchImpl, environments, curun(env, grant);changed();};+ /** Moves on each time an account is left: a pass that began before that lends nothing more. */+ let accountTurn = 0;const automaticPass = async (projects, target) => {if (closed || paused || !ownClouds().some(verifiedCloud)) return;+ const turn = accountTurn;const [[homeFolder = null], screenLent, projectFolders] = await Promise.all([lendable([{ id: randomUUID(), path: home, write: true, home: true }]),readScreen(),@@ -630,11 +633,11 @@ export function createComputerSharing({ file, fetch: fetchImpl, environments, cu]);for (const env of ownClouds()) {const kept = records[env.id], current = verifiedCloud(env);- if (closed || paused || !current) continue;+ if (closed || paused || turn !== accountTurn || !current) continue;const keptFolders = kept?.cloud?.accountId === current.accountId && Array.isArray(kept.folders) ? await lendable(kept.folders) : [];const wanted = automaticGrant({ kept: keptFolders, home: homeFolder, screen: screenLent, projects: env.id === (target ?? myCloud())?.id ? projectFolders : [],terminal: terminalAccount === current.accountId });- if (closed || paused || records[env.id] !== kept) continue;+ if (closed || paused || turn !== accountTurn || records[env.id] !== kept) continue;if (!lendsAsWanted(kept, wanted)) lendNow(env, current, kept, wanted);else if (!running.has(env.id) && validRecord(kept) && cloudLendingVerdict(kept.cloud, cloud(env), env).allow) run(env, kept);}@@ -826,6 +829,7 @@ export function createComputerSharing({ file, fetch: fetchImpl, environments, cu* told to them (one asleep stays asleep) and what was lent is kept, so lending goes on if it comes back. */leaveAccount(accountId) {if (!accountId) return;+ accountTurn++;for (const [id, grant] of Object.entries(records)) {if (grant?.cloud?.accountId !== accountId) continue;stop(id);diff --git a/electron/lending-accounts.node-test.mjs b/electron/lending-accounts.node-test.mjsindex c87d6f5b63..bf78a7d205 100644--- a/electron/lending-accounts.node-test.mjs+++ b/electron/lending-accounts.node-test.mjs@@ -8,7 +8,7 @@ import { createComputerSharing } from "./computer-sharing.mjs";const until = async (check, what) => { for (let i = 0; i < 400; i++) { if (check()) return; await new Promise((resolve) => setTimeout(resolve, 5)); } assert.fail(`never: ${what}`); };-async function fixture(t) {+async function fixture(t, { screen = () => false } = {}) {const dir = await mkdtemp(path.join(tmpdir(), "omb-lending-accounts-"));const env = { id: "cloud-a", origin: "https://a.fixture.test", name: "A's Cloud", cloud: { id: "c" } };const f = { env, account: "acct-a", status: "connected", origin: env.origin, places: [env], polls: [], asks: [], routes: [], live: () => [...f.polls, ...f.asks].filter((signal) => !signal.aborted).length };@@ -17,7 +17,7 @@ async function fixture(t) {f.sharing = createComputerSharing({file: path.join(dir, "sharing.json"), environments: () => f.places, enabled: async () => false, home: path.join(dir, "home"),cloud: () => ({ status: f.status, accountId: f.account, origin: f.origin }), myCloud: () => f.places[0] ?? null, ownClouds: () => f.places,- hostControl: async () => ({ renew: async () => {}, release: async () => {} }), screen: () => false,+ hostControl: async () => ({ renew: async () => {}, release: async () => {} }), screen,async fetch(url, { signal }) {const route = new URL(url).pathname;f.routes.push(route);@@ -32,11 +32,18 @@ async function fixture(t) {return f;}+/** The automatic pass has finished, and the connection it left is the one polling. */+async function settled(f) {+ await f.sharing.lendProjects([]);+ await until(() => f.polls.some((signal) => !signal.aborted), "the lent connection");+ await new Promise((resolve) => setTimeout(resolve, 20));+}+test("switching: this computer stops working for the account left, its Cloud is told nothing and can go to sleep, and it comes back with that account", async (t) => {const f = await fixture(t);await f.sharing.saveCloud(f.env, { folders: [], internet: true });f.sharing.cloudChanged();- await until(() => f.polls.length > 0, "the lent connection");+ await settled(f);assert.equal(f.sharing.lendingToCloud(), true);f.routes.length = 0;@@ -68,3 +75,22 @@ test("leaving another account, or none, stops nothing", async (t) => {f.sharing.leaveAccount(null);assert.equal(f.sharing.lendingToCloud(), true);});++test("a lending pass already under way when the account is left lends nothing more, and tells that Cloud nothing", async (t) => {+ const screen = Promise.withResolvers();+ let held = false;+ const f = await fixture(t, { screen: () => (held ? screen.promise : false) });+ await f.sharing.saveCloud(f.env, { folders: [], internet: true });+ await settled(f);+ held = true;+ f.sharing.cloudChanged();+ await new Promise((resolve) => setTimeout(resolve, 5));+ f.sharing.leaveAccount("acct-a");+ f.routes.length = 0;+ const polling = f.polls.length;+ screen.resolve(true);+ await new Promise((resolve) => setTimeout(resolve, 60));+ assert.deepEqual(f.routes.filter((route) => route.endsWith("/disconnect")), [], "no request that could wake a sleeping Cloud");+ assert.equal(f.polls.length, polling, "nothing dials the account left");+ assert.equal(f.sharing.lendingToCloud(), false);+});