Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(runtime): let a bot ask back from another conversation while the asker waits on it
AdityaUmale16eabc3ac804
diff --git a/server/crossed-handoffs.e2e.test.ts b/server/crossed-handoffs.e2e.test.tsindex d038c9422e..4f69e19b0f 100644--- a/server/crossed-handoffs.e2e.test.ts+++ b/server/crossed-handoffs.e2e.test.ts@@ -1,9 +1,9 @@// Two bots that ask each other at once, end to end on the scripted engine (docs/runtime-contract.md §2). Modelled on a real// day: the mail bot asked the chief of staff for yesterday's mailbox details; before that was answered, the person told the// chief of staff in its own chat that mail had worked yesterday, and the chief of staff asked the mail bot why it failed.-// Both then waited on each other, both worked the same problem in two places, and each came back with its own plan.-// After: the second request is refused at once, naming the request it would answer, so the chief of staff answers the-// person itself and the mail bot's question still gets its answer. Once that answer is in, the same hand-off is ordinary.+// Each conversation answers its own request: the chief of staff's question from its own chat goes through while the mail+// bot waits on it, and both get their answers. A request from or into the conversation that holds the waiting one is still+// refused (server/runtime-coordinator.test.ts).import { existsSync, readFileSync, writeFileSync } from "node:fs";import { join } from "node:path";import { expect, it } from "vitest";@@ -13,7 +13,7 @@ import { readHandoffs } from "./testing/handoff-rows.ts";const SETTLED = "Your downstream room requests have settled.";-it("refuses a request back to a bot that is waiting on you, and the same hand-off once it has its answer", async () => {+it("lets the chief of staff ask the mail bot from its own chat while the mail bot waits on it, and both get their answers", async () => {const session = await launchVerificationServer(process.env, undefined, undefined, undefined, undefined, { scripted: true });const cli = (...args: string[]) => runControlOmb(args, { env: { OPENMAUSBOT_URL: session.info.url } }) as Promise<any>;const api = (path: string, body?: unknown, method = "POST") =>@@ -29,14 +29,12 @@ it("refuses a request back to a bot that is waiting on you, and the same hand-of{ key: "P1", match: ["P1-JOB"], steps: [{ arguments: { bot_ids: [cos.id], message: "P1-ASK: do you still have yesterday's mailbox details?", consult: true } }],reply: "P1: asked the chief of staff." },{ key: "P1-DONE", match: [SETTLED, "P1-JOB"], reply: "P1-DONE: nothing stored, so I need the mailbox password from you." },- { key: "C2-WORK", match: ["C2-ASK"], reply: "C2-WORK: the server answers on port 993." },+ { key: "C1-WORK", match: ["C1-ASK"], reply: "C1-WORK: the mail server refuses the saved sign-in since this morning." },] },[cos.id]: { script: [{ key: "P1-ASK", match: ["P1-ASK"], readyFile: ready, gateFile: gate, reply: "P1-ASK: no mailbox details are stored here." },- { key: "C1", match: ["C1-JOB"], steps: [{ arguments: { bot_ids: [post.id], message: "C1-ASK: why can't you fetch mail?" }, expectError: true }],- reply: "C1: Post is already asking me about this; I'll answer it there." },- { key: "C2", match: ["C2-JOB"], steps: [{ arguments: { bot_ids: [post.id], message: "C2-ASK: which port does the mail server use?" } }], reply: "C2: asked Post." },- { key: "C2-DONE", match: [SETTLED, "C2-JOB"], reply: "C2-DONE: port 993." },+ { key: "C1", match: ["C1-JOB"], steps: [{ arguments: { bot_ids: [post.id], message: "C1-ASK: why can't you fetch mail?" } }], reply: "C1: asked Post." },+ { key: "C1-DONE", match: [SETTLED, "C1-JOB"], reply: "C1-DONE: the mail server refuses the saved sign-in." },] },}));const evidence = (): any[] => existsSync(`${planPath}.evidence.jsonl`)@@ -49,22 +47,15 @@ it("refuses a request back to a bot that is waiting on you, and the same hand-ofawait expect.poll(() => existsSync(ready), { timeout: 20_000 }).toBe(true);// The chief of staff is answering the mail bot in its work thread when the person writes to it in its own chat.await cli("send", "--bot", cos.id, "--task", cos.activeTaskId, "--text", "C1-JOB: mail worked yesterday, find out why it fails");- await expect.poll(() => said(cos.activeTaskId, "C1: Post is already asking me about this; I'll answer it there."), { timeout: 20_000 }).toBe(true);- const refusal = evidence().find((turn) => turn.planKey === "C1").evidence.find((entry: any) => entry.step).response.result;- expect(refusal.isError).toBe(true);- expect(JSON.stringify(refusal)).toContain("@Post is already waiting on you: it handed you a request");- expect(JSON.stringify(refusal), "the waiting request's words stay in its own thread").not.toContain("P1-ASK");+ await expect.poll(() => said(cos.activeTaskId, "C1-DONE: the mail server refuses the saved sign-in."), { timeout: 20_000 }).toBe(true);+ const sent = evidence().find((turn) => turn.planKey === "C1").evidence.find((entry: any) => entry.step).response.result;+ expect(sent.isError, JSON.stringify(sent)).not.toBe(true);+ expect(keys(post.id), "the mail bot answered the chief of staff while it still waited on its own question").toEqual(["P1", "C1-WORK"]);writeFileSync(gate, "answer");await expect.poll(() => said(post.activeTaskId, "P1-DONE: nothing stored, so I need the mailbox password from you."), { timeout: 20_000 }).toBe(true);- expect(keys(post.id), "the mail bot asked once and heard back once; nothing was handed back to it").toEqual(["P1", "P1-DONE"]);- expect(keys(cos.id).sort(), "the chief of staff answered the person and the mail bot, one turn each").toEqual(["C1", "P1-ASK"]);- expect(readHandoffs(session.info.dataDir).filter((node) => node.parentId)).toHaveLength(1);-- // A legitimate hand-off the other way, once nothing waits on it any more, goes through as before.- await cli("send", "--bot", cos.id, "--task", cos.activeTaskId, "--text", "C2-JOB: ask Post for the mail server's port");- await expect.poll(() => said(cos.activeTaskId, "C2-DONE: port 993."), { timeout: 20_000 }).toBe(true);- expect(keys(post.id)).toEqual(["P1", "P1-DONE", "C2-WORK"]);+ expect(keys(post.id)).toEqual(["P1", "C1-WORK", "P1-DONE"]);+ expect(keys(cos.id).sort(), "the chief of staff answered the person and the mail bot").toEqual(["C1", "C1-DONE", "P1-ASK"]);expect(readHandoffs(session.info.dataDir).filter((node) => node.parentId).map((node) => node.status)).toEqual(["completed", "completed"]);} finally {await session.close();diff --git a/server/parallel-threads.e2e.test.ts b/server/parallel-threads.e2e.test.tsindex afd7f0f3c9..b36beb3721 100644--- a/server/parallel-threads.e2e.test.ts+++ b/server/parallel-threads.e2e.test.ts@@ -462,7 +462,7 @@ describe("a Starter Cloud", () => {f.finish(b.threads[0]!);await send(f, c.botId, c.threads[0]!, "Cyd's own job");await whenStarted(c.threads[0]!);- // From a third bot: Ben asking Ada back while Ada waits on Ben would be refused as a loop (crossed-handoffs.e2e).+ // From a third bot: Ben asking Ada back from the thread that holds Ada's request would be refused as a loop.const toAda = await startThread(c.threads[0]!, "For Ada", a.botId);expect(toAda.status, JSON.stringify(toAda.body)).toBeLessThan(300);await whenStarted(await ownThread(a.botId, "For Ada"));diff --git a/server/runtime-coordinator.test.ts b/server/runtime-coordinator.test.tsindex 292cfa84a0..5fca0be14f 100644--- a/server/runtime-coordinator.test.ts+++ b/server/runtime-coordinator.test.ts@@ -557,8 +557,8 @@ describe("RuntimeCoordinator: refusals in the legacy words", () => {});// Modelled on a real day: the mail bot asked the chief of staff for yesterday's mailbox details, and before that was answered-// the chief of staff, asked by the person in its own chat, asked the mail bot why mail failed. Both waited on each other and-// worked the same problem in two places, each with its own plan.+// the chief of staff, asked by the person in its own chat, asked the mail bot why mail failed. Each conversation answers its+// own request; only a request from or into the conversation that holds the waiting one would leave both waiting on each other.describe("RuntimeCoordinator: a request back to a bot that is waiting on you", () => {const mail: RoomAddress = { botId: "mail", threadId: "mail-chat" };const cos: RoomAddress = { botId: "cos", threadId: "cos-chat" };@@ -566,27 +566,42 @@ describe("RuntimeCoordinator: a request back to a bot that is waiting on you", (const mailWork: RoomAddress = { botId: "mail", threadId: "mail-work-from-cos" };const holdCos: Script = (node) => node.botId === "cos" && node.parentId !== undefined ? "hold" : undefined;- it("refuses it while the request it would answer is open, says which, and writes nothing", async () => {+ it("lets a request from another of your conversations through while the asker waits, and both get their answers", async () => {const w = world(holdCos);const asked = send(w, mail, "gen-mail", undefined, cosWork, "Do you have the mailbox details from yesterday?");- expect(() => send(w, cos, "gen-cos", undefined, mailWork, "Why can't you fetch mail?"))+ const back = send(w, cos, "gen-cos", undefined, mailWork, "Why can't you fetch mail?");+ expect(back.node).toMatchObject({ botId: "mail", status: "queued" });+ w.coordinator.tick(); await flush();+ expect(w.coordinator.node(asked.node.id)?.status).toBe("running");+ expect(w.coordinator.node(back.node.id)?.status).toBe("completed");+ await w.host.release(asked.node.id, { ok: true, text: "No details stored" });+ expect(w.coordinator.node(asked.node.id)?.status).toBe("completed");+ });++ it("refuses it from the conversation that holds the request it would answer, or into the one that waits, says which, and writes nothing", async () => {+ const w = world(holdCos);+ const asked = send(w, mail, "gen-mail", undefined, cosWork, "Do you have the mailbox details from yesterday?");+ expect(() => send(w, cosWork, "gen-cos-work", undefined, mailWork, "Why can't you fetch mail?")).toThrow(`@mail is already waiting on you: it handed you a request (assignment ${asked.node.id}), and that has not reached you yet.`);+ expect(w.coordinator.children("gen-cos-work")).toEqual([]);+ expect(() => send(w, cos, "gen-cos", undefined, mail, "Why can't you fetch mail?")).toThrow(/@mail is already waiting on you/);expect(w.coordinator.children("gen-cos")).toEqual([]);+ ended(w, "gen-cos-work");w.coordinator.tick();expect(w.coordinator.node(asked.node.id)?.status).toBe("running");- expect(() => send(w, cos, "gen-cos", undefined, mailWork, "Why can't you fetch mail?")).toThrow(/you are answering it in another conversation/);+ expect(() => send(w, cos, "gen-cos", undefined, mail, "Why can't you fetch mail?")).toThrow(/you are answering it in another conversation/);await w.host.release(asked.node.id, { ok: true, text: "No details stored" });expect(w.coordinator.node(asked.node.id)?.status).toBe("completed");- expect(send(w, cos, "gen-cos", undefined, mailWork, "Why can't you fetch mail?").node).toMatchObject({ botId: "mail", status: "queued" });+ expect(send(w, cos, "gen-cos", undefined, mail, "Why can't you fetch mail?").node).toMatchObject({ botId: "mail", status: "queued" });});- it("refuses a consultation the same way, and while the request waits on a question", async () => {+ it("refuses a consultation the same way while the request waits on a question in that conversation", async () => {const w = world((node) => node.botId === "cos" && node.parentId !== undefined ? { ok: true, text: "NEEDS INPUT: Which mailbox?" } : undefined);const asked = send(w, mail, "gen-mail", undefined, cosWork, "Do you have the mailbox details from yesterday?");w.coordinator.tick(); await flush();expect(w.coordinator.node(asked.node.id)?.status).toBe("waiting");- begun(w, cos, "gen-cos");- expect(() => w.coordinator.enqueue(cos, "gen-cos", undefined, mailWork, "Which mailbox do you mean?", { mode: "consult" }))+ begun(w, cosWork, "gen-cos-work");+ expect(() => w.coordinator.enqueue(cosWork, "gen-cos-work", undefined, mailWork, "Which mailbox do you mean?", { mode: "consult" })).toThrow(/@mail is already waiting on you/);});@@ -597,7 +612,7 @@ describe("RuntimeCoordinator: a request back to a bot that is waiting on you", (// The chief of staff's own request is another line of the person's than the one they stopped.begun(w, cos, "gen-cos", "Mail worked yesterday", { origin: { kind: "user", request: "Mail worked yesterday, find out why" } });w.coordinator.stopAwaitingDirect("mail-chat");- expect(send(w, cos, "gen-cos", undefined, mailWork, "Why can't you fetch mail?").node).toMatchObject({ botId: "mail", status: "queued" });+ expect(send(w, cos, "gen-cos", undefined, mail, "Why can't you fetch mail?").node).toMatchObject({ botId: "mail", status: "queued" });if (w.host.held.has(asked.node.id)) await w.host.release(asked.node.id, { ok: true, text: "No details stored" });});diff --git a/server/runtime-coordinator.ts b/server/runtime-coordinator.tsindex 18f90abbab..7d710a1ad7 100644--- a/server/runtime-coordinator.ts+++ b/server/runtime-coordinator.ts@@ -142,9 +142,10 @@ function announcingWrites(store: RuntimeStore, wrote: () => void): RuntimeStoremarkDelivered: announced(store.markDelivered), prune: announced(store.prune) };}-/** A request back to a bot that waits on the asker's answer would leave each waiting on the other, as two bots did on a real- * day, each then working the same problem with its own plan. The ancestor rule covers this within one request; this covers- * two requests. The waiting request's words are not repeated: it may come from another person's private chat. */+/** A request back to a bot that waits on the asker's answer in the same conversation would leave each waiting on the other.+ * The ancestor rule covers this within one request; this covers two requests. A request from another of the asker's+ * conversations goes through: each conversation answers its own. The waiting request's words are not repeated: it may come+ * from another person's private chat. */const crossed = (asker: string, request: Assignment): string => `@${asker} is already waiting on you: it handed you a request (assignment ${request.id}), and ${request.status === "queued" ? "that has not reached you yet" : request.status === "running" ? "you are answering it in another conversation now": "you have not finished it yet"}. Asking @${asker} for something now would leave each of you waiting on the other, so this was not sent. Answer the person here from what you know; @${@@ -319,7 +320,7 @@ export class RuntimeCoordinator implements Coordinator {c.assignee.destination.groupId === target.groupId && c.assignee.botId === target.botId && c.status === "completed")) throw new Error(ALREADY_COMPLETED);if (kind === "work" && target.groupId && lineage.some(n => n.assignee.destination.groupId === target.groupId)) throw new Error(ROOM_ANCESTOR);// A bot's own threads wait on it by design and can never ask it back (the ancestor rule above), so no two bots can cross.- const waiting = own || mode === "notify" || target.groupId !== undefined ? undefined : this.waitingOn(source.botId, target.botId);+ const waiting = own || mode === "notify" || target.groupId !== undefined ? undefined : this.waitingOn(source, target);if (waiting) throw new Error(crossed(this.host.names(this.project(waiting.request), this.project(waiting.parent)).requester, waiting.request));const requester: Requester = { kind: "bot_turn", botId: parent.assignee.botId, threadId: parent.assignee.destination.threadId,chief: this.host.chiefOf(address(parent)), ...(parent.id === root.id && root.requester.runId !== undefined ? { runId: root.requester.runId } : {}) };@@ -771,10 +772,12 @@ export class RuntimeCoordinator implements Coordinator {}}- /** Direct work `asker` handed to `botId` and still waits on: an open request whose requesting turn is still open. */- private waitingOn(botId: string, asker: string): { request: Assignment; parent: Assignment } | undefined {+ /** Direct work `asker` handed to `bot` and still waits on: an open request whose requesting turn is still open, held in the+ * conversation `bot` asks from or waiting in the one the new request goes to. */+ private waitingOn(bot: RoomAddress, asker: RoomAddress): { request: Assignment; parent: Assignment } | undefined {for (const request of this.live()) {- if (request.assignee.botId !== botId || request.requester.botId !== asker || request.reply === "none" || request.assignee.destination.groupId !== undefined) continue;+ if (request.assignee.botId !== bot.botId || request.requester.botId !== asker.botId || request.reply === "none" || request.assignee.destination.groupId !== undefined) continue;+ if (request.assignee.destination.threadId !== bot.threadId && request.requester.threadId !== asker.threadId) continue;const parent = request.parentId ? this.store.get(request.parentId) : undefined;if (parent && !isTerminal(parent)) return { request, parent };}