Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Grok signals: random prompt ids, a late reply after the notification ends nothing twice, admission test through the engine wrapper
milind-soni8de7eb436d36
diff --git a/server/drivers/acp/grok-signals.test.ts b/server/drivers/acp/grok-signals.test.tsindex df0f367274..8d08417ab9 100644--- a/server/drivers/acp/grok-signals.test.ts+++ b/server/drivers/acp/grok-signals.test.ts@@ -61,7 +61,7 @@ describe("Grok's prompt_complete", () => {expect(of(events, "turn.completed")).toEqual([expect.objectContaining({ ok: true, stopReason: null })]);expect(of(events, "thread.token-usage.updated")).toEqual([expect.objectContaining({ input: 20, output: 7 })]);const prompt = calls().find((call) => call.method === "session/prompt")!;- expect(prompt.meta).toEqual({ promptId: expect.stringMatching(/^omb-prompt-\d+$/), requestId: prompt.meta?.promptId });+ expect(prompt.meta).toEqual({ promptId: expect.stringMatching(/^omb-prompt-[\w-]+$/), requestId: prompt.meta?.promptId });});it("ends the turn on the notification alone when no reply comes", async () => {@@ -71,6 +71,18 @@ describe("Grok's prompt_complete", () => {expect(replyText(events)).toBe("answered");});+ it("ends the turn once when the prompt's reply comes after the notification already ended it, and the thread goes on", async () => {+ const { ask, calls, recorder } = await start({ FAKE_ACP_MODE: "grok-complete", FAKE_ACP_COMPLETE_REPLY: "late-bare", FAKE_ACP_COMPLETE_REPLY_MS: "3000" });+ const [firstEnd] = of(await ask(), "turn.completed");+ expect(firstEnd).toMatchObject({ ok: true });+ // The second turn starts as the first ends and itself waits out the notification's grace, past the first reply's arrival.+ const second = await ask("and now?");+ expect(of(second, "turn.completed")).toEqual([expect.objectContaining({ ok: true })]);+ expect(replyText(second)).toBe("answered");+ expect(of(recorder.events, "turn.completed").map((event) => event.turnId)).toEqual([firstEnd!.turnId, second[0]!.turnId]);+ expect(new Set(calls().filter((call) => call.method === "session/prompt").map((call) => call.meta?.promptId)).size).toBe(2);+ });+it("takes a notification that names no prompt for its session's one prompt out", async () => {const { ask } = await start({ FAKE_ACP_MODE: "grok-complete", FAKE_ACP_COMPLETE_REPLY: "never", FAKE_ACP_COMPLETE_NAMES: "none" });expect(of(await ask(), "turn.completed")).toEqual([expect.objectContaining({ ok: true })]);@@ -101,11 +113,10 @@ describe("Grok's rate-limit stop", () => {expect(error!.message).toMatch(/^This Grok account has reached its usage limit/);};- it("is the account's usage limit, so another AI can carry on: from the notification, the error reply or the reply's own stop", async () => {- usageLimit(await (await start({ FAKE_ACP_MODE: "grok-complete", FAKE_ACP_COMPLETE_STOP: "rate_limit", FAKE_ACP_COMPLETE_REPLY: "never" })).ask());- usageLimit(await (await start({ FAKE_ACP_MODE: "grok-complete", FAKE_ACP_COMPLETE_STOP: "rate_limit", FAKE_ACP_COMPLETE_REPLY: "rate-limit-error" })).ask());- usageLimit(await (await start({ FAKE_ACP_MODE: "grok-complete", FAKE_ACP_COMPLETE_STOP: "rate_limit", FAKE_ACP_COMPLETE_REPLY: "after" })).ask());- });+ it.each([["the notification alone", "never"], ["the JSON-RPC error reply", "rate-limit-error"], ["the reply's own stop", "after"]])(+ "is the account's usage limit, so another AI can carry on: from %s", async (_from, reply) => {+ usageLimit(await (await start({ FAKE_ACP_MODE: "grok-complete", FAKE_ACP_COMPLETE_STOP: "rate_limit", FAKE_ACP_COMPLETE_REPLY: reply })).ask());+ });it("keeps its own words when they name another cause, such as the service being down", async () => {const events = await (await start({ FAKE_ACP_MODE: "grok-complete", FAKE_ACP_COMPLETE_STOP: "rate_limit", FAKE_ACP_COMPLETE_REPLY: "never",diff --git a/server/drivers/acp/host.ts b/server/drivers/acp/host.tsindex 19a76846ab..3f890b2848 100644--- a/server/drivers/acp/host.ts+++ b/server/drivers/acp/host.ts@@ -2,6 +2,7 @@// sessions in one process (ACP keys every session request and update by sessionId), so a sharing driver links many// threads to one host; every other driver links exactly one.import type { ChildProcessWithoutNullStreams } from "node:child_process";+import { randomUUID } from "node:crypto";import { engineMemory } from "../../engine-memory.ts";import { engineOwners } from "../../engine-owners.ts";@@ -255,7 +256,7 @@ export class AcpHost {const id = this.#nextId++;if (method === "_x.ai/mcp/list") this.#privateResponses.add(id);const completion = method === "session/prompt" ? this.options.promptCompletion : undefined;- const prompt = completion ? { id: `omb-prompt-${id}`, sessionId: (params as { sessionId?: unknown } | null)?.sessionId } : null;+ const prompt = completion ? { id: `omb-prompt-${randomUUID()}`, sessionId: (params as { sessionId?: unknown } | null)?.sessionId } : null;if (completion && prompt) params = { ...params as object, _meta: { ...(params as { _meta?: object } | null)?._meta, ...completion.meta(prompt.id) } };let timer: ReturnType<typeof setTimeout> | null = null;if (timeoutMs) {diff --git a/server/process-room.test.ts b/server/process-room.test.tsindex 45c8f427a5..078139f435 100644--- a/server/process-room.test.ts+++ b/server/process-room.test.ts@@ -694,6 +694,29 @@ describe("a new engine that never comes up", () => {expect(s.memory.stoppedForRoom("t1", 1000)).toBe(false);expect(s.memory.stoppedForRoom("t1")).toBe(false);});+ it("admits each engine at its own size: a Grok start waits where an engine of the usual size fits", async () => {+ // Under pids.max 256, 120 and a busy engine's 44 running: one more of the usual 60 reaches the 224 below the margin,+ // Grok's runtime threads pass it.+ const send = async (driverKind: string) => {+ const s = server({ pids: 256, turns: 100 });+ s.state.base = 120;+ expect(await s.start("busy")).toBe(true);+ // Its processes are all there by now, so nothing more is held for it.+ s.advance(6_000);+ const decorated = withEngineMemory(fakeInstance(driverKind).instance, s.memory), notices: string[] = [];+ decorated.adapter.onEvent((event) => { if (event.type === "runtime.notice") notices.push(event.message); });+ return { decorated, notices, turn: decorated.adapter.sendTurn({ threadId: "t1", text: "hi" } as SendTurnInput) };+ };+ const usual = await send("claudeAgent");+ expect(await usual.turn).toEqual({ turnId: "turn-t1" });+ expect(usual.notices).toEqual([]);+ const grok = await send("grokAgent");+ const caught = grok.turn.catch((error: unknown) => error);+ await settle();+ expect(grok.notices).toHaveLength(1);+ await grok.decorated.adapter.interruptTurn("t1", "turn-t1");+ expect(await caught).toBeInstanceOf(TurnNotStartedError);+ });});describe("a spawn refused during a turn", () => {diff --git a/server/testing/fake-acp-cli.ts b/server/testing/fake-acp-cli.tsindex 7c3fc01e46..8d9c20081e 100755--- a/server/testing/fake-acp-cli.ts+++ b/server/testing/fake-acp-cli.ts@@ -105,7 +105,7 @@// notification {sessionId, promptId: the prompt's _meta.promptId, stopReason: FAKE_ACP_COMPLETE_STOP (end_turn),// agentResult: FAKE_ACP_COMPLETE_RESULT (null)}, then answers per FAKE_ACP_COMPLETE_REPLY: "after" (default) at once// with that stopReason and usage, "never", "late" after FAKE_ACP_COMPLETE_REPLY_MS (3500) with "late words" and usage,-// or "rate-limit-error" (JSON-RPC -32003). FAKE_ACP_COMPLETE_NAMES (own) lists the prompts the notifications name:+// "late-bare" the same reply with no words, or "rate-limit-error" (JSON-RPC -32003). FAKE_ACP_COMPLETE_NAMES (own) lists the prompts the notifications name:// own, none (no promptId), task (a background task's task-completed-… id), other-session.// FAKE_ACP_START_FAILURE this line on stderr at startup, then exit 1 before reading anything, as grok 1.0.x does when it// cannot start its threads@@ -801,7 +801,10 @@ function grokCompletePrompt(msg: any) {switch (process.env.FAKE_ACP_COMPLETE_REPLY ?? "after") {case "after": result(msg.id, { stopReason, _meta: usage }); break;case "never": hangKeepAlive = setInterval(() => {}, 1_000); break;- case "late": setTimeout(() => { say(" late words"); result(msg.id, { stopReason, _meta: usage }); }, Number(process.env.FAKE_ACP_COMPLETE_REPLY_MS ?? 3_500)); break;+ case "late": case "late-bare": setTimeout(() => {+ if (process.env.FAKE_ACP_COMPLETE_REPLY === "late") say(" late words");+ result(msg.id, { stopReason, _meta: usage });+ }, Number(process.env.FAKE_ACP_COMPLETE_REPLY_MS ?? 3_500)); break;case "rate-limit-error": out({ jsonrpc: "2.0", id: msg.id, error: { code: -32003, message: "Rate limited", data: { message: process.env.FAKE_ACP_COMPLETE_RESULT ?? "Rate limited" } } }); break;}}