Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Try again draws at once, and a long Retry-After keeps the minute read while someone looks
milind-soni859be5c5051b
diff --git a/electron/cloud-background.node-test.mjs b/electron/cloud-background.node-test.mjsindex 60209fd889..dfb73eb891 100644--- a/electron/cloud-background.node-test.mjs+++ b/electron/cloud-background.node-test.mjs@@ -189,9 +189,9 @@ test("coming to the front reads the session at once when its answer is a minute});// The session's own waits, against the loopback fixture (fixtures/cloud-account.mjs).-test("a failing check never asks before the Admin's Retry-After, held to five minutes and spread only later, with a last try before the answer stops counting", async t => {+test("nobody looking, a failing check never asks before the Admin's Retry-After, held to five minutes and spread only later, with a last try before the answer stops counting", async t => {let draw = 0.5;- const f = await fixture(t, { random: () => draw }); await f.connect();+ const f = await fixture(t, { random: () => draw, attended: () => false }); await f.connect();f.sessionError = { status: 429, body: { error: "rate_limited" }, headers: { "retry-after": "60" } };for (const [drawn, delay] of [[0, 60_000], [0.5, 66_000], [1, 72_000]]) { draw = drawn; await f.client.refresh(); assert.equal(f.delay, delay, "the Admin's own wait, not 15 s, spread up to a fifth later"); }f.sessionError.headers["retry-after"] = "3600"; draw = 0;@@ -265,3 +265,38 @@ test("nobody looking and nothing under way: the session every 5 minutes, spreadf.now += 120_000; f.client.freshen();assert.equal(f.requests.length, ended);});++test("while someone looks, a Retry-After an hour long still leaves a read each minute, as before", async t => {+ const f = await fixture(t, { attended: () => true }); await f.connect();+ f.sessionError = { status: 429, body: { error: "rate_limited" }, headers: { "retry-after": "3600" } };+ await f.client.refresh();+ assert.equal(f.delay, 60_000);+});++test("coming to the front never moves the next read later than it was", async t => {+ const f = await fixture(t, { attended: () => true, random: () => 0 }); await f.connect();+ assert.equal(f.delay, 48_000, "in front, spread sooner");+ f.now += 20_000;+ assert.equal(f.client.freshen(), null);+ assert.equal(f.delay, 48_000, "already due before a minute after the last read: left as it is");+});++test("triggers at the same moment (focus, show, wake, Try again) make one session read", async t => {+ const f = await fixture(t); await f.connect();+ f.now += 61_000;+ const before = f.requests.filter((row) => row.route.endsWith("/session")).length;+ await Promise.all([0, 1, 2, 3, 4].map(() => f.client.freshen()));+ assert.equal(f.requests.filter((row) => row.route.endsWith("/session")).length, before + 1);+});++test("while another account takes over, neither token is asked for with the sign-in before", async t => {+ const f = await fixture(t); await f.connect();+ f.pages = { status: 200, body: { cloudContractVersion: 1, url: `${f.origin}/api/pages/token`, token: `omb_pd_${"P".repeat(43)}` } };+ f.x = { status: 200, body: { cloudContractVersion: 1, url: `${f.origin}/api/x`, token: `omb_xd_${"X".repeat(43)}` } };+ assert.ok(await f.client.pagesAccess(), "signed in: the token comes");+ const before = f.requests.length;+ f.client.quiesce();+ await assert.rejects(f.client.pagesAccess(), /another account/i);+ await assert.rejects(f.client.xResearchAccess(), /another account/i);+ assert.equal(f.requests.length, before, "nothing reached the Admin");+});diff --git a/electron/cloud-cadence.mjs b/electron/cloud-cadence.mjsindex 91d45cc51a..b03ff28ebe 100644--- a/electron/cloud-cadence.mjs+++ b/electron/cloud-cadence.mjs@@ -54,11 +54,12 @@ export function retryWait(failures, { steps = ASK_RETRY_MS, retryAfter, retryAft/** The wait before a failed session read is tried again: 15 s, 30 s, then each minute while someone is looking (`attended`),* every 15 s while a sleeping Cloud is awaited (`waking`), else longer each time up to five minutes. Never before the Admin's- * Retry-After (held to five minutes), never past the sign-in's expiry (`expiresIn`), and always with a last try 20 s before- * the verified answer stops counting (`verifiedIn`). */+ * Retry-After, held to five minutes, and to a minute while someone is looking; never past the sign-in's expiry (`expiresIn`),+ * and always with a last try 20 s before the verified answer stops counting (`verifiedIn`). */export function sessionRetryWait(failures, { retryAfter, expiresIn = Infinity, verifiedIn = 0, attended = false, waking = false, random = Math.random } = {}) {const left = verifiedIn - 20_000, steps = waking ? WAKE_RETRY_MS : attended ? ATTENDED_RETRY_MS : SESSION_RETRY_MS;- return Math.min(retryWait(failures, { steps, retryAfter, retryAfterMax: SESSION_RETRY_AFTER_MAX_MS, below: waking || attended, random }), expiresIn, left > 0 ? left : Infinity);+ const wait = retryWait(failures, { steps, retryAfter, retryAfterMax: SESSION_RETRY_AFTER_MAX_MS, below: waking || attended, random });+ return Math.min(attended ? Math.min(wait, REFRESH_MS) : wait, expiresIn, left > 0 ? left : Infinity);}/**diff --git a/electron/cloud-cadence.node-test.mjs b/electron/cloud-cadence.node-test.mjsindex fd344d8a6e..f6d2adf15d 100644--- a/electron/cloud-cadence.node-test.mjs+++ b/electron/cloud-cadence.node-test.mjs@@ -21,6 +21,7 @@ test("a Retry-After is kept, spread only later, and held to its cap (an hour, fifor (const bad of [0, -5, Number.NaN, undefined, "600"]) assert.equal(retryWait(1, { retryAfter: bad, random: () => 0.5 }), 60_000, String(bad));assert.equal(sessionRetryWait(1, { retryAfter: 3_600, verifiedIn: 900_000, random: low }), SESSION_RETRY_AFTER_MAX_MS, "the session's is held to five minutes");assert.equal(sessionRetryWait(1, { retryAfter: 3_600, verifiedIn: 140_000, random: low }), 120_000, "and never past 20 s before the verified answer stops counting");+ assert.equal(sessionRetryWait(1, { retryAfter: 3_600, verifiedIn: 900_000, attended: true, random: () => 1 }), 60_000, "and to a minute while someone looks");});test("the session's failure waits: who is looking decides how long", () => {diff --git a/electron/cloud-operation.node-test.mjs b/electron/cloud-operation.node-test.mjsindex f89265c343..b5db411957 100644--- a/electron/cloud-operation.node-test.mjs+++ b/electron/cloud-operation.node-test.mjs@@ -152,3 +152,20 @@ test("a team's Cloud's own page is told its name with what has it down", async (test("a rollback is its own kind", () => {assert.deepEqual(parseCloudOperation({ kind: "rollback", since: 5, until: 9, expectedAt: 7 }), { kind: "rollback", since: 5, until: 9, expectedAt: 7 });});++test("Try again draws the screen at once, never after a session read that hangs, and again when that read lands", async () => {+ const w = world({ state: { status: "signed-out" } });+ await w.home.show();+ await w.home.retry();+ assert.equal(w.freshens ?? 0, 0, "only a press reads the sign-in");+ let land, timer;+ w.freshRead = new Promise((resolve) => { land = resolve; });+ const drawn = await Promise.race([w.home.retry({ pressed: true }).then(() => "drawn"), new Promise((resolve) => { timer = setTimeout(resolve, 5_000, "waited for the read"); })]);+ clearTimeout(timer);+ assert.equal(drawn, "drawn");+ assert.equal(w.freshens, 1);+ const reads = w.stateReads;+ land(w.state);+ for (let i = 0; i < 20; i++) await new Promise((resolve) => setImmediate(resolve));+ assert.ok(w.stateReads > reads, "drawn again once the read lands");+});diff --git a/electron/fixtures/home-window-world.mjs b/electron/fixtures/home-window-world.mjsindex 9551ea027f..4e263976ff 100644--- a/electron/fixtures/home-window-world.mjs+++ b/electron/fixtures/home-window-world.mjs@@ -46,14 +46,14 @@ export function world({ clean = null, journal = done(), marker = null, state = cw.contents = contents;w.window = { isDestroyed: () => false, webContents: contents };const account = {- state: () => w.state,+ state: () => { w.stateReads = (w.stateReads ?? 0) + 1; return w.state; },/** The session read again (cloud-account.mjs `refresh`): `w.refreshed`, when set, is what the Admin says then. */refresh: async () => { w.refreshes++; if (w.refreshed) w.state = w.refreshed; return w.state; },/** While the Admin's session read keeps failing (`w.failingUntil`), when the account tries it again by itself. */failingUntil: () => w.failingUntil ?? 0,/** When the last session read began (`w.lastAttemptAt`); `freshen`, as the account's: read when that is a minute old. */lastAttemptAt: () => w.lastAttemptAt ?? 0,- freshen: () => { w.freshens = (w.freshens ?? 0) + 1; return null; },+ freshen: () => { w.freshens = (w.freshens ?? 0) + 1; return w.freshRead ?? null; },power: () => w.power,powerOf: (origin) => (origin === CLOUD ? w.power : (w.state?.clouds ?? []).find((cloud) => cloud.origin === origin)?.power ?? null),async wakeHome() { w.wakes++; await w.cloud.wakeGate; if (w.cloud.wakes) w.power = { ...w.power, state: "awake" }; return "awake"; },diff --git a/electron/home-window.mjs b/electron/home-window.mjsindex ccf7154762..083108e718 100644--- a/electron/home-window.mjs+++ b/electron/home-window.mjs@@ -768,9 +768,10 @@ export function createHomeWindow(deps) {/** The place the window was last sent to: My Cloud, or a team's or another own Cloud (`{ kind: "team" | "own", id, name, origin }`). */shown: () => shown,/** The screen asked again (as it opens, back online, or the window focused); `pressed`: the person pressed Try again, which- * no wait holds except the Admin's own 429, and which reads the session again first when its last read began a minute ago. */+ * no wait holds except the Admin's own 429, and which also reads the session when its last read began a minute ago. */async retry({ pressed = false } = {}) {- if (pressed) { waits.pressed(); await Promise.resolve(account()?.freshen?.()).catch(() => {}); }+ // The screen is drawn at once; a session read that was due draws it again when it lands, however long it takes.+ if (pressed) { waits.pressed(); const reading = account()?.freshen?.(); if (reading) void Promise.resolve(reading).then(() => show(), () => {}).catch(() => {}); }return show();},/** The screen's one button: this device's MausBot Cloud sign-in, again. A sign-in already open in the browser opens