Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Make four release-gate flakes reliable
milind-sonib400065ae1e1
diff --git a/electron/device-link.node-test.mjs b/electron/device-link.node-test.mjsindex 5f1a9c6e2c..8883619620 100644--- a/electron/device-link.node-test.mjs+++ b/electron/device-link.node-test.mjs@@ -247,12 +247,11 @@ test("the link signs in like the poll, takes My Cloud's word for this computer'sconst stop = new AbortController();let opened = 0;const connects = [];- const running = runDeviceLink({ url: end.url, headers: { cookie: "omb_session=s", "x-omb-computer-secret": "a".repeat(64) }, signal: stop.signal, onOpen: () => opened++, openMs: 100,+ const running = runDeviceLink({ url: end.url, headers: { cookie: "omb_session=s", "x-omb-computer-secret": "a".repeat(64) }, signal: stop.signal, onOpen: () => opened++,egress: { interfaces: () => ({}), resolve: async () => [{ address: SEEN_AS, family: 4 }], connect: (options) => { connects.push(options); return tcpConnect(1, "127.0.0.1"); } } });await until(() => end.sessions.length === 1 && opened === 1, "the link to open");assert.equal(end.sessions[0].headers.cookie, "omb_session=s");assert.equal(end.sessions[0].headers["x-omb-computer-secret"], "a".repeat(64));- await delay(250); // past the opening limit: a link that opened is not ended by itassert.deepEqual(await end.sessions[0].mux.open({ host: "home-router.test", port: 443 }), { refused: "home-network" }, "the home's own public address is home");assert.equal(connects.length, 0);stop.abort();@@ -261,6 +260,17 @@ test("the link signs in like the poll, takes My Cloud's word for this computer'sassert.equal(linkUrl("http://127.0.0.1:9", "/x"), "ws://127.0.0.1:9/x");});+test("a link that opened is never ended by its opening limit", async () => {+ class OpensAtOnce { constructor() { queueMicrotask(() => this.onopen?.()); } send() {} close() {} }+ const stop = new AbortController();+ let opened = 0;+ const running = runDeviceLink({ url: "ws://127.0.0.1:9/link", WebSocket: OpensAtOnce, signal: stop.signal, openMs: 20, onOpen: () => opened++, egress: { interfaces: () => ({}) } });+ await delay(100);+ stop.abort();+ assert.equal(opened, 1);+ assert.deepEqual(await running, { code: 1000, reason: "stopped" });+});+test("a My Cloud that stops answering ends the link here within the silence limit, so nothing hangs on it", async (t) => {const end = await cloudEnd(t, { answerPings: false, greet: false });const started = Date.now();diff --git a/server/data/engine.test.ts b/server/data/engine.test.tsindex 91b2e6516f..36297b6feb 100644--- a/server/data/engine.test.ts+++ b/server/data/engine.test.ts@@ -515,41 +515,57 @@ describe("cancellation", () => {expect((await failure(db.run("SELECT 1", { ...bot, signal: early.signal }))).error.code).toBe("cancelled");});- it("cancels a statement whose abort landed before it dispatched", async () => {- let dispatched = false;- let delivered = 0;+ /** A lane whose statement reaches the connection only on `dispatch()`; an interrupt before that is lost, as DuckDB's is,+ * and one that is never delivered after it lets the statement finish half a second later. */+ function undispatched(signal?: AbortSignal) {+ const seen = { dispatched: false, delivered: 0 };let abandon: (() => void) | undefined;- const connection = {+ const lane = new Lane({run: () => {- dispatched = true;+ seen.dispatched = true;return new Promise<unknown>((resolve, reject) => {const finish = setTimeout(() => resolve("finished without an interrupt"), 500);abandon = () => { clearTimeout(finish); reject(new Error("INTERRUPT Error: Interrupted!")); };});},interrupt: () => {- delivered++;+ seen.delivered++;abandon?.();},- };- const lane = new Lane(connection as never);+ } as never);let parked!: () => void;const atWork = new Promise<void>((done) => { parked = done; });- let open!: () => void;- const gate = new Promise<void>((done) => { open = done; });- const controller = new AbortController();- const running = lane.run("SELECT 1", { signal: controller.signal, timeoutMs: 30_000 }, async (conn) => {+ let dispatch!: () => void;+ const gate = new Promise<void>((done) => { dispatch = done; });+ const running = lane.run("SELECT 1", { signal, timeoutMs: 30_000 }, async (conn) => {parked();await gate;return conn.run("SELECT 1");});- await atWork;+ return { seen, lane, atWork, dispatch, running };+ }++ it("cancels a statement whose abort landed before it dispatched", async () => {+ const controller = new AbortController();+ const statement = undispatched(controller.signal);+ await statement.atWork;controller.abort();- open();- const error = await failure(running);- expect(dispatched).toBe(true);+ statement.dispatch();+ const error = await failure(statement.running);+ expect(statement.seen.dispatched).toBe(true);+ expect(error.error.code).toBe("cancelled");+ expect(statement.seen.delivered).toBeGreaterThan(1);+ });++ it("cancels a statement whose interrupt landed before it dispatched", async () => {+ const statement = undispatched();+ await statement.atWork;+ statement.lane.interrupt();+ statement.dispatch();+ const error = await failure(statement.running);+ expect(statement.seen.dispatched).toBe(true);expect(error.error.code).toBe("cancelled");- expect(delivered).toBeGreaterThan(1);+ expect(statement.seen.delivered).toBeGreaterThan(1);});it("interrupts one connection while the other keeps working, and queued statements still run", async () => {diff --git a/server/data/engine.ts b/server/data/engine.tsindex eeefbdb409..d7d873c10c 100644--- a/server/data/engine.ts+++ b/server/data/engine.ts@@ -165,7 +165,7 @@ function missingTableName(message: string): string | null {type InterruptCause = "timeout" | "cancelled";-type RunState = { interrupted: InterruptCause | null; retry?: NodeJS.Timeout };+type RunState = { interrupted: InterruptCause | null; deadline: number; retry?: NodeJS.Timeout };function failureFrom(error: unknown, sql: string, interrupted: InterruptCause | null, timeoutMs: number): DataFailure {if (error instanceof DataFailure) return error;@@ -195,27 +195,10 @@ export class Lane {private async execute<T>(sql: string, options: { timeoutMs?: number; signal?: AbortSignal }, work: (connection: DuckDB.DuckDBConnection) => Promise<T>): Promise<T> {const timeoutMs = options.timeoutMs ?? DATA_LIMITS.sqlTimeoutMs;if (options.signal?.aborted) throw new DataFailure({ code: "cancelled", message: "Cancelled before it ran.", sql });- const state: RunState = { interrupted: null };- const deadline = Date.now() + timeoutMs;+ const state: RunState = { interrupted: null, deadline: Date.now() + timeoutMs };this.running = state;- // DuckDB honours an interrupt only once its statement is dispatched, so a cause keeps re-delivering- // until the statement settles: one that lands first is not lost, and no cause blocks the next.- const interrupt = (cause: InterruptCause) => {- state.interrupted ??= cause;- this.connection.interrupt();- if (state.retry || Date.now() >= deadline) return;- const retry = setInterval(() => {- if (Date.now() >= deadline) {- clearInterval(retry);- state.retry = undefined;- return;- }- this.connection.interrupt();- }, INTERRUPT_DELIVERY_MS);- state.retry = retry;- };- const timer = setTimeout(() => interrupt("timeout"), timeoutMs);- const onAbort = () => interrupt("cancelled");+ const timer = setTimeout(() => this.stop(state, "timeout"), timeoutMs);+ const onAbort = () => this.stop(state, "cancelled");options.signal?.addEventListener("abort", onAbort, { once: true });try {return await work(this.connection);@@ -230,9 +213,24 @@ export class Lane {}interrupt(): void {- if (!this.running) return;- this.running.interrupted ??= "cancelled";+ if (this.running) this.stop(this.running, "cancelled");+ }++ // DuckDB honours an interrupt only once its statement is dispatched, so a cause keeps re-delivering+ // until the statement settles: one that lands first is not lost, and no cause blocks the next.+ private stop(state: RunState, cause: InterruptCause): void {+ state.interrupted ??= cause;this.connection.interrupt();+ if (state.retry || Date.now() >= state.deadline) return;+ const retry = setInterval(() => {+ if (Date.now() >= state.deadline) {+ clearInterval(retry);+ state.retry = undefined;+ return;+ }+ this.connection.interrupt();+ }, INTERRUPT_DELIVERY_MS);+ state.retry = retry;}}diff --git a/server/memory-order.e2e.test.ts b/server/memory-order.e2e.test.tsindex c5c864a595..a46a7995fb 100644--- a/server/memory-order.e2e.test.ts+++ b/server/memory-order.e2e.test.ts@@ -28,10 +28,10 @@ async function fixture(test: (f: any) => Promise<void>) {it("starts the person's own line ahead of a hand-off that was already waiting for memory, and the hand-off right after", () => fixture(async (f) => {const [chief, first, second, person] = [await f.bot("Clive"), await f.bot("First"), await f.bot("Second"), await f.bot("Person's bot")];await f.api(`/api/bots/${chief.id}`, { chiefOfStaff: true, managedSections: ["Team"], acknowledgePeerScope: true }, "PATCH");- const gate = join(f.dataDir, "first-may-finish");+ const gate = join(f.dataDir, "first-may-finish"), firstRunning = join(f.dataDir, "first-is-running");writeFileSync(f.planPath, JSON.stringify({[chief.id]: { steps: [{ arguments: { bot_ids: [first.id], message: "First job" } }, { arguments: { bot_ids: [second.id], message: "Second job" } }], reply: "Sent", resumeReply: "Done" },- [first.id]: { gateFile: gate, reply: "First done" },+ [first.id]: { readyFile: firstRunning, gateFile: gate, reply: "First done" },[second.id]: { reply: "Second done" },[person.id]: { reply: "Yours" },}));@@ -46,6 +46,7 @@ it("starts the person's own line ahead of a hand-off that was already waiting fotry {expect((db.prepare("SELECT kind, detail FROM assignment_events WHERE assignment_id = ? AND kind = 'held'").all(held.id) as Array<{ detail: string }>).map((row) => row.detail)[0]).toMatch(/^memory: \d+ running, \d+ ahead$/);} finally { db.close(); }+ await expect.poll(() => existsSync(firstRunning), { timeout: 30_000 }).toBe(true);await f.cli("send", "--bot", person.id, "--task", person.activeTaskId, "--text", "My own question.");await expect.poll(async () => (await f.messages(person.activeTaskId)).some((message: any) => message.tool?.name?.includes("Waiting for memory")), { timeout: 30_000 }).toBe(true);diff --git a/server/mistral-key-flow.test.ts b/server/mistral-key-flow.test.tsindex a0a81d33d5..88eaa34dd4 100644--- a/server/mistral-key-flow.test.ts+++ b/server/mistral-key-flow.test.ts@@ -4,6 +4,7 @@ import { join } from "node:path";import { expect, it } from "vitest";import { launchVerificationServer } from "../scripts/control-omb.ts";import { fixtureApi } from "../scripts/testing/preview-fixture.ts";+import { hostTimeout } from "./testing/host-timeout.ts";import { openSse } from "./testing/sse.ts";it("Mistral keys persist, rotate, clear and reach real HTTP turns without leaking into public state", async () => {@@ -91,4 +92,4 @@ it("Mistral keys persist, rotate, clear and reach real HTTP turns without leakinawait fixture.close();await new Promise<void>((resolve) => provider.close(() => resolve()));}-});+}, hostTimeout(60_000));