Skip to content

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

fix(lending): an update keeps the terminal a Cloud had, and a room bot can ask for it (#4017)

Aditya Umale3e72997a1458
diff --git a/docs/cloud-pro.md b/docs/cloud-pro.md
index dca2578f68..c4dc90b754 100644
--- a/docs/cloud-pro.md
+++ b/docs/cloud-pro.md
@@ -696,7 +696,9 @@ internet connection then, and becomes automatic like anyone else's.
Anyone else gets the automatic grant at the next start. A grant kept from before
keeps its folders, gains the home folder, the internet connection and Chrome, and
-loses a terminal it had; a **Settings → Servers → Computer access** grant kept for
+keeps a terminal it had: when every grant of one account lent the terminal and
+nobody said no to lending, **Let Clouds run commands on this Mac** starts on for
+that account and no other; a **Settings → Servers → Computer access** grant kept for
a Cloud's own entry gives way to it. Only a kept grant that already lends what it
should runs as it is: one that doesn't never connects, even for a moment.
diff --git a/electron/computer-sharing.mjs b/electron/computer-sharing.mjs
index a1c7971487..9ec41e6dba 100644
--- a/electron/computer-sharing.mjs
+++ b/electron/computer-sharing.mjs
@@ -145,6 +145,16 @@ export function saidNo(record) {
return record.internet === false;
}
+/** Security: the menu's switch lends the terminal to every Cloud of one account, so grants from before it stand for it
+ * only when one account's grants all lent the terminal, as the person confirmed then, and nobody said no to lending. */
+function terminalLentBeforeAutomatic(records) {
+ const grants = Object.values(records).filter(record => record?.enabled === true && typeof record.cloud?.accountId === "string" && !saidNo(record));
+ const accounts = new Set(grants.filter(grant => grant.terminal === true).map(grant => grant.cloud.accountId));
+ if (accounts.size !== 1) return null;
+ const [account] = accounts;
+ return grants.every(grant => grant.cloud.accountId !== account || grant.terminal === true) ? account : null;
+}
+
/** What each Cloud of the person's own gets by itself: the internet connection, their Chrome, apps and screen once
* computer control works here (`screen`), and the home folder with editing (`home`, null when it can't be lent), on top
* of the folders lent to it already (`kept`, which Files may keep copies of) and the project folders the move adds. The
@@ -210,6 +220,7 @@ export function createComputerSharing({ file, fetch: fetchImpl, environments, cu
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;
+ else if (!parsed?.automatic) terminalAccount = terminalLentBeforeAutomatic(records);
} catch {}
const running = new Map();
const grantVersions = new Map();
diff --git a/electron/lend-automatically.node-test.mjs b/electron/lend-automatically.node-test.mjs
index a45fabf41f..6a292a930a 100644
--- a/electron/lend-automatically.node-test.mjs
+++ b/electron/lend-automatically.node-test.mjs
@@ -260,7 +260,7 @@ test("someone who had switched lending off, or unticked the internet connection,
}
});
-test("a grant kept from before keeps its folders, which Files keeps copies of; the terminal it had is not lent any more", async t => {
+test("a grant kept from before keeps its folders, which Files keeps copies of, and the terminal its account lent, as the menu's switch", async t => {
const homes = { [HOME]: cloudHome() };
const mac = await computer(t, { homes, places: [myCloud] });
const plans = path.join(mac.dir, "Plans");
@@ -273,12 +273,45 @@ test("a grant kept from before keeps its folders, which Files keeps copies of; t
sharing.start();
await until(() => homes[HOME].connects.length === 1, "the automatic grant to reach My Cloud");
await delay(100);
- assert.equal(homes[HOME].connects.length, 1, "the grant with the terminal never connected, not even for a moment");
+ assert.equal(homes[HOME].connects.length, 1, "the grant from before never connected, not even for a moment");
assert.deepEqual(homes[HOME].connects[0].folders.map(folder => [folder.id, folder.name, folder.write]), [[id, "Plans", false]], "the same folder, by the same id");
- assert.equal(homes[HOME].connects[0].terminal, false);
- homes[HOME].push({ action: "run_command", command: "id" });
+ assert.equal(homes[HOME].connects[0].terminal, true);
+ assert.equal(mac.summaries.at(-1).terminal, true, "the menu shows it on");
+ homes[HOME].push({ action: "run_command", command: "echo kept" });
await until(() => homes[HOME].results.length === 1, "the command's answer");
- assert.equal(homes[HOME].results[0].isError, true, "no command runs here");
+ assert.notEqual(homes[HOME].results[0].isError, true, "the command ran here");
+ const again = mac.restart();
+ again.start();
+ await until(() => homes[HOME].connects.length === 2, "the kept grant to reach My Cloud after another restart");
+ assert.equal(homes[HOME].connects[1].terminal, true, "still lent once saved the new way");
+ mac.signedIn.signInAs("acct_2");
+ await again.screenChanged();
+ await until(() => homes[HOME].connects.length === 3, "the other account's grant to reach My Cloud");
+ assert.equal(homes[HOME].connects[2].terminal, false, "only the account that lent it");
+});
+
+test("grants from before give no terminal when one didn't lend it, the person said no to lending, or two accounts lent it, after Resume too", async t => {
+ const grant = (origin, extra) => ({ sessionId: null, environmentId: null, id: randomUUID(), secret: "a".repeat(64), enabled: true, folders: [], terminal: true,
+ computer: false, internet: true, cloud: { accountId: "acct_1", origin }, ...extra });
+ const cases = {
+ "it didn't lend the terminal": { [myCloud.id]: grant(HOME, { terminal: false }) },
+ "it is off": { [myCloud.id]: grant(HOME, { enabled: false }) },
+ "the internet connection was unticked": { [myCloud.id]: grant(HOME, { internet: false }) },
+ "another Cloud of the account didn't lend it": { [myCloud.id]: grant(HOME), [clientA.id]: grant(OTHER, { terminal: false }) },
+ "two accounts lent it": { [myCloud.id]: grant(HOME), [clientA.id]: grant(OTHER, { cloud: { accountId: "acct_2", origin: OTHER } }) },
+ };
+ for (const [why, records] of Object.entries(cases)) {
+ const homes = { [HOME]: cloudHome(), [OTHER]: cloudHome() };
+ const mac = await computer(t, { homes, places: [myCloud, clientA] });
+ await mkdir(path.dirname(mac.file), { recursive: true });
+ await writeFile(mac.file, JSON.stringify({ version: 1, records }));
+ const sharing = mac.restart();
+ sharing.start();
+ if (sharing.paused()) await sharing.resume();
+ await until(() => homes[HOME].connects.length === 1, `the automatic grant to reach My Cloud when ${why}`);
+ assert.equal(homes[HOME].connects[0].terminal, false, why);
+ assert.equal(mac.summaries.at(-1).terminal, false, why);
+ }
});
test("the terminal is lent only once the person turns it on: every own Cloud gets it, it lasts across restarts, and turning it off takes it back", async t => {
diff --git a/electron/use-my-mac.node-test.mjs b/electron/use-my-mac.node-test.mjs
index d86737a529..d815b6e250 100644
--- a/electron/use-my-mac.node-test.mjs
+++ b/electron/use-my-mac.node-test.mjs
@@ -384,7 +384,7 @@ test("a grant kept from before lending was automatic becomes the automatic one,
await until(() => state.home.connects.at(-1)?.internet === true && sharing.cloudState(env).link === "on", "the automatic grant to connect");
const last = state.home.connects.at(-1);
assert.deepEqual(last.folders.map((entry) => entry.name), ["Plans"], "its folders as they were");
- assert.equal(last.terminal, false, "the terminal is no part of it");
+ assert.equal(last.terminal, true, "the terminal it lent, kept as the menu's switch");
sharing.cloudChanged();
await delay(100);
assert.equal(state.home.connects.at(-1), last, "nothing else is lent on top");
diff --git a/server/host/computer-asks-host.test.ts b/server/host/computer-asks-host.test.ts
index d13688a9eb..5cf3953cc3 100644
--- a/server/host/computer-asks-host.test.ts
+++ b/server/host/computer-asks-host.test.ts
@@ -5,17 +5,18 @@ import type { Message } from "../store.ts";
const turn = { botId: "scout", threadId: "t1", generation: "g1" };
-function fixture({ computer = "on" as string, refusal = null as string | null, active = true } = {}) {
+function fixture({ computer = "on" as string, refusal = null as string | null, active = true, room = null as { id: string; name: string; memberIds: string[] } | null } = {}) {
const lines: Message[] = [{ id: "u1", at: 1, role: "user", kind: "text", text: "Open the invoice in Excel on my PC." } as Message];
const store = {
appendMessage: (_threadId: string, message: Partial<Message>) => { const added = { id: `m${lines.length + 1}`, at: Date.now(), ...message } as Message; lines.push(added); return added; },
patchMessage: (_threadId: string, id: string, patch: Partial<Message>) => Object.assign(lines.find((line) => line.id === id) ?? {}, patch),
messagesFor: () => lines,
activePath: () => lines,
- taskByThread: () => ({ id: "scout", threadId: "t1" }),
- projectBotForTask: () => ({ id: "scout", name: "Scout", color: "blue", computer, busy: true, activity: "working" }),
+ taskByThread: () => room ? undefined : { id: "scout", threadId: "t1" },
+ projectBotForTask: () => room ? null : { id: "scout", name: "Scout", color: "blue", computer, busy: true, activity: "working" },
+ bot: (id: string) => id === "scout" ? { id: "scout", name: "Scout", color: "blue", computer } : undefined,
setTaskActivity: () => {},
- groupByThread: () => null,
+ groupByThread: () => room,
};
const lending = { lendingNow: vi.fn(() => [] as Array<{ id: string; name: string; computer: boolean; terminal: boolean }>), known: () => [{ platform: "win32" as const }], changed: () => {}, askTool: null as string | null };
const continued: unknown[] = [];
@@ -44,6 +45,14 @@ describe("ask_for_computer on the person's own Cloud", () => {
.toEqual({ status: 403, body: { error: "Someone else wrote in this conversation, so it can't use your PC." } });
});
+ it("raises the card for a bot in a room, whose turn runs on the room's thread, and refuses a bot that isn't in it", async () => {
+ const room = { id: "brand", name: "Brand", memberIds: ["scout", "writer"] };
+ const f = fixture({ room });
+ f.lending.lendingNow.mockReturnValue([{ id: "mac-1", name: "Mac", computer: false, terminal: true }]);
+ expect(await f.host.call(turn, { need: "terminal", why: "Run the scan" })).toEqual({ status: 200, body: { state: "ready", text: expect.stringContaining("computer_id mac-1") } });
+ expect(await fixture({ room: { ...room, memberIds: ["writer"] } }).host.call(turn, { need: "terminal" })).toMatchObject({ status: 404 });
+ });
+
it("tells the shared computer texts to point at the ask, and answers ready at once when the computer lends what's needed", async () => {
const f = fixture();
expect(f.lending.askTool).toBe("ask_for_computer");
diff --git a/server/host/computer-asks-host.ts b/server/host/computer-asks-host.ts
index a8a6c31b6e..d99d570af6 100644
--- a/server/host/computer-asks-host.ts
+++ b/server/host/computer-asks-host.ts
@@ -49,13 +49,15 @@ export function createComputerAsksHost(deps: ComputerAsksHostDeps) {
deps.lending.changed = () => asks.lendingChanged();
deps.lending.askTool = ASK_FOR_COMPUTER_TOOL;
+ const roomMember = (turn: Turn) => store.groupByThread(turn.threadId)?.memberIds.includes(turn.botId) ? store.bot(turn.botId) ?? null : null;
+
/** POST /api/internal/computer-asks: { need, why } from a bot's turn. */
async function call(turn: Turn, body: unknown): Promise<{ status: number; body: Record<string, unknown> }> {
const input = body && typeof body === "object" ? body as { need?: unknown; why?: unknown } : {};
if (!deps.active(turn)) return { status: 401, body: { error: "This turn has ended." } };
const need = COMPUTER_NEEDS.find((candidate) => candidate === input.need);
if (!need) return { status: 400, body: { error: `need must be one of ${COMPUTER_NEEDS.join(", ")}` } };
- const bot = store.projectBotForTask(turn.botId, turn.threadId);
+ const bot = store.projectBotForTask(turn.botId, turn.threadId) ?? roomMember(turn);
if (!bot) return { status: 404, body: { error: "This bot no longer exists." } };
if (need === "screen" && bot.computer === "off") {
return { status: 403, body: { error: "This bot has no computer. Change its Computer setting to use their computer's apps and screen." } };