Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(own home): ask a saved server who owns it once at a time, and leave one that did not say alone for a minute (#4058)
Milind Soni42c179030aac
diff --git a/electron/own-home-main.d.mts b/electron/own-home-main.d.mtsindex 20456a3bdb..f733f3b1e6 100644--- a/electron/own-home-main.d.mts+++ b/electron/own-home-main.d.mts@@ -7,6 +7,7 @@ export interface SavedPlace { id: string; kind: string; name: string; origin?: stype Answer = { ok: boolean; status: number; body: any };export declare const OWNER_KNOWN_MS: number;+export declare const OWNER_RETRY_MS: number;export function parseChoice(value: unknown): OwnHomeChoice | null;export function handSaved(entry: unknown): entry is SavedPlace & { kind: "remote"; origin: string };export function serverRequests(place: { origin: string }, fetchImpl: typeof fetch): {diff --git a/electron/own-home-main.mjs b/electron/own-home-main.mjsindex 5dc0e5b651..645c24542c 100644--- a/electron/own-home-main.mjs+++ b/electron/own-home-main.mjs@@ -15,6 +15,8 @@ const ORIGIN = /^https?:\/\/[^/\s]+$/u;const ENVIRONMENT_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu;/** How long a server's word on who owns it is kept for the rail. */export const OWNER_KNOWN_MS = 10 * 60_000;+/** How long a server that did not say who owns it is left alone before it is asked again. */+export const OWNER_RETRY_MS = 60_000;/** A chosen move that could not begin (this computer's server did not answer) looks again this often. */const START_RETRY_MS = 30_000;@@ -159,20 +161,33 @@ export function installOwnHome({ localRequest, fetch: fetchImpl, environments, hlog,});- /** The server's own word on whether this window's sign-in there is its one owner's, kept OWNER_KNOWN_MS. */- async function owner(place) {- try {- const who = await serverRequests(place, fetchImpl).who();- const known = { own: who.own && who.takesMove && who.environmentId !== null, environmentId: who.environmentId, at: now() };- const before = owners.get(place.id);- owners.set(place.id, known);- if (before?.own !== known.own) onChange();- return known;- } catch (error) {- log(`own home: ${place.name} did not say who owns it (${error?.message ?? error})`);- owners.delete(place.id);- return { own: false, environmentId: null, at: now() };- }+ /** The asks under way, one per server. */+ const asking = new Map();+ /** The server's own word on whether this window's sign-in there is its one owner's, kept OWNER_KNOWN_MS. A server that+ * did not say is left alone for OWNER_RETRY_MS. */+ function owner(place) {+ const pending = asking.get(place.id);+ if (pending) return pending;+ const ask = (async () => {+ try {+ const who = await serverRequests(place, fetchImpl).who();+ const known = { own: who.own && who.takesMove && who.environmentId !== null, environmentId: who.environmentId, at: now() };+ const before = owners.get(place.id);+ owners.set(place.id, known);+ if (before?.own !== known.own) onChange();+ return known;+ } catch (error) {+ log(`own home: ${place.name} did not say who owns it (${error?.message ?? error})`);+ const before = owners.get(place.id);+ owners.set(place.id, { own: false, environmentId: null, at: now(), unanswered: true });+ if (before?.own) onChange();+ return { own: false, environmentId: null, at: now() };+ } finally {+ asking.delete(place.id);+ }+ })();+ asking.set(place.id, ask);+ return ask;}const owns = (place) => {const known = handSaved(place) ? owners.get(place.id) : null;@@ -226,7 +241,7 @@ export function installOwnHome({ localRequest, fetch: fetchImpl, environments, hrefresh() {for (const place of environments().filter(handSaved)) {const known = owners.get(place.id);- if (!known || now() - known.at >= OWNER_KNOWN_MS) void owner(place);+ if (!known || now() - known.at >= (known.unanswered ? OWNER_RETRY_MS : OWNER_KNOWN_MS)) void owner(place);}},/** The person chose this server as their home: asked once more, then the move starts. */diff --git a/electron/own-home-main.node-test.mjs b/electron/own-home-main.node-test.mjsindex a489fc185a..9b1800876b 100644--- a/electron/own-home-main.node-test.mjs+++ b/electron/own-home-main.node-test.mjs@@ -1,7 +1,7 @@import assert from "node:assert/strict";import test from "node:test";-import { handSaved, installOwnHome, openServerSession, OWNER_KNOWN_MS, parseChoice } from "./own-home-main.mjs";+import { handSaved, installOwnHome, openServerSession, OWNER_KNOWN_MS, OWNER_RETRY_MS, parseChoice } from "./own-home-main.mjs";const ID = "7c1d2e3f-4a5b-4c6d-8e7f-9a0b1c2d3e4f";const garage = { id: "g", kind: "remote", name: "Garage server", origin: "https://garage.example.test" };@@ -19,6 +19,8 @@ function servers(answers) {const { origin, pathname } = new URL(url);asked.push({ origin, path: pathname, headers: init?.headers ?? {} });const server = answers[origin];+ if (server?.silent) return new Promise(() => {});+ if (server?.down) throw new TypeError("fetch failed");if (!server) return new Response("", { status: 502 });if (pathname === "/api/auth/session") return Response.json(server.signedIn === false ? { error: "unauthorized" } : { kind: "session", scopes: ["admin"], ...(server.own ? { own: true } : {}) }, { status: server.signedIn === false ? 401 : 200 });if (pathname === "/.well-known/openmausbot/environment") return Response.json({ environmentId: server.environmentId ?? ID, capabilities: server.move === false ? {} : { move: 1 } });@@ -135,3 +137,23 @@ test("while the move goes, this computer's screen names the server; once home isassert.equal(done.own.offers(garage), false);done.own.close();});++test("a server that does not say who owns it is asked once at a time, and again only after OWNER_RETRY_MS", async () => {+ let at = 1_000;+ const { own, asked } = install({ answers: { [garage.origin]: { down: true }, [family.origin]: { silent: true } }, now: () => at });+ const to = (origin) => asked.filter((ask) => ask.origin === origin).length;+ own.refresh(); own.refresh(); own.refresh();+ await new Promise((resolve) => setTimeout(resolve, 20));+ assert.equal(to(garage.origin), 2, "its session and its environment, once");+ assert.equal(to(family.origin), 2, "a server still being asked is not asked again");+ own.refresh();+ await new Promise((resolve) => setTimeout(resolve, 20));+ assert.equal(to(garage.origin), 2, "a server that did not say is left alone for a while");+ assert.equal(own.offers(garage), false);+ at += OWNER_RETRY_MS;+ own.refresh();+ await new Promise((resolve) => setTimeout(resolve, 20));+ assert.equal(to(garage.origin), 4, "then asked again");+ assert.equal(to(family.origin), 2, "while its first ask still waits");+ own.close();+});