Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(coordinate-bots): a refused hand-off belongs to the run that made it
Flex Builder449009bc0447
diff --git a/server/host/refused-handoffs.test.ts b/server/host/refused-handoffs.test.tsindex ffb2377086..65a527e0fc 100644--- a/server/host/refused-handoffs.test.ts+++ b/server/host/refused-handoffs.test.ts@@ -11,21 +11,25 @@ const books: RosterMember = { id: "books", name: "Bookkeeping", visibility: "admconst mausbot: RosterMember = { id: "maus", name: "MausBot", visibility: { people: ["[email protected]"] }, defaultBot: { person: "[email protected]" } };const bots = [rooms, hours, books, mausbot];+const SAID = "Rooms couldn't hand this to Bookkeeping, because they're shared with different people. Nothing was sent.";+const r1 = { id: "run_1", routineId: "r1", botId: "rooms" }, r2 = { id: "run_2", routineId: "r2", botId: "rooms" };+function fixture(over: { writer?: string; asked?: string | null; run?: boolean; own?: boolean } = {}) {const lines: Array<{ to: string; delivery: Delivery }> = [];+ let current: typeof r1 | undefined = over.run === false ? undefined : r1;const refused = createRefusedHandoffs({- bots: () => bots, runOf: () => (over.run === false ? undefined : { routineId: "r1", botId: "rooms" }), writerOf: () => over.writer,+ bots: () => bots, runOf: () => current, writerOf: () => over.writer,askedPerson: () => over.asked ?? null, theirOwn: () => over.own === true, inbox: () => ({ deliver: (to, delivery) => { lines.push({ to, delivery }); } }), language: () => undefined,});- return { refused, lines };+ return { refused, lines, now: (run: typeof r1 | undefined) => { current = run; } };}describe("a routine's refused hand-off", () => {it("makes the run say so, once, whether the bot was named by id or by name", () => {for (const named of ["books", "Bookkeeping", "@bookkeeping"]) {const { refused } = fixture();- refused.settled("t", rooms, [named], 403);- expect(refused.take("t")).toBe("Rooms couldn't hand this to Bookkeeping, because they're shared with different people. Nothing was sent.");+ refused.settled("t", rooms, [named], 403, r1);+ expect(refused.take("t")).toBe(SAID);expect(refused.take("t")).toBeNull();}});@@ -33,47 +37,76 @@ describe("a routine's refused hand-off", () => {it("stays quiet about anything that wasn't the refusal: a hand-off that went through, a typo, the bot itself, someone's own MausBot", () => {for (const [named, status] of [["Staff Hours", 202], ["Staff Hours", 403], ["Bookkeeper", 403], ["Rooms", 403], ["MausBot", 403], ["books", 202]] as const) {const { refused } = fixture();- refused.settled("t", rooms, [named], status);+ refused.settled("t", rooms, [named], status, r1);expect(refused.take("t"), named).toBeNull();}});it("forgets a refusal once the same bot was reached after all", () => {const { refused } = fixture();- refused.settled("t", rooms, ["Bookkeeping"], 403);- refused.settled("t", rooms, ["books"], 202);+ refused.settled("t", rooms, ["Bookkeeping"], 403, r1);+ refused.settled("t", rooms, ["books"], 202, r1);expect(refused.take("t")).toBeNull();});it("keeps nothing for a conversation that isn't a routine's run", () => {const { refused } = fixture({ run: false });- refused.settled("t", rooms, ["Bookkeeping"], 403);+ refused.settled("t", rooms, ["Bookkeeping"], 403, undefined);expect(refused.take("t")).toBeNull();});+ it("keeps nothing for a call that returned after the run that made it was gone and another had started", () => {+ const { refused, lines, now } = fixture({ writer: "p_olive", asked: "p_mara" });+ const madeIn = r1;+ now(r2);+ refused.settled("t", rooms, ["Bookkeeping"], 403, madeIn);+ expect(refused.take("t"), "the run that started since never made that call").toBeNull();+ expect(lines).toEqual([]);+ });++ it("keeps nothing for a call made outside a routine that returned after a routine's run had started", () => {+ const { refused, lines, now } = fixture({ writer: "p_olive", asked: "p_mara", run: false });+ now(r2);+ refused.settled("t", rooms, ["Bookkeeping"], 403, undefined);+ expect(lines, "the run that started since wrote no routine that was refused").toEqual([]);+ expect(refused.take("t"), "the run that started since never made that call").toBeNull();+ });++ it("still makes the run that made the call say so before the conversation moves on", () => {+ const { refused, lines, now } = fixture({ writer: "p_olive", asked: "p_mara" });+ const madeIn = r1;+ refused.settled("t1", rooms, ["Bookkeeping"], 403, madeIn);+ expect(refused.take("t1")).toBe(SAID);+ now(r2);+ refused.settled("t2", rooms, ["Bookkeeping"], 403, madeIn);+ const later = refused.take("t2");+ expect(lines.map((line) => line.to), "only the run that made the call told its writer").toEqual(["p_olive"]);+ expect(later, "the run that started since never made that call").toBeNull();+ });+it("tells who wrote the routine in their inbox when the bot's failures reach someone else, once for the same words", () => {const { refused, lines } = fixture({ writer: "p_olive", asked: "p_mara" });- refused.settled("t", rooms, ["Bookkeeping"], 403);+ refused.settled("t", rooms, ["Bookkeeping"], 403, r1);refused.take("t");expect(lines).toHaveLength(1);expect(lines[0]!.to).toBe("p_olive");- expect(lines[0]!.delivery).toMatchObject({ kind: "notice", quiet: false, title: "Rooms's routine couldn't hand off its work", text: "Rooms couldn't hand this to Bookkeeping, because they're shared with different people. Nothing was sent." });- refused.settled("t2", rooms, ["Bookkeeping"], 403);+ expect(lines[0]!.delivery).toMatchObject({ kind: "notice", quiet: false, title: "Rooms's routine couldn't hand off its work", text: SAID });+ refused.settled("t2", rooms, ["Bookkeeping"], 403, r1);refused.take("t2");expect(lines, "the next day's run says the same").toHaveLength(1);});it("reaches a person's inbox from a run of their own too, where the failure's own notification already tells them", () => {const { refused, lines } = fixture({ writer: "p_mara", asked: "p_mara", own: true });- refused.settled("t", rooms, ["Bookkeeping"], 403);+ refused.settled("t", rooms, ["Bookkeeping"], 403, r1);refused.take("t");expect(lines.map((line) => [line.to, line.delivery.quiet])).toEqual([["p_mara", true]]);});it("sends no second line when the bot's failures already reach who wrote it", () => {const { refused, lines } = fixture({ writer: "p_olive", asked: "p_olive" });- refused.settled("t", rooms, ["Bookkeeping"], 403);+ refused.settled("t", rooms, ["Bookkeeping"], 403, r1);refused.take("t");expect(lines).toEqual([]);});-});+});\ No newline at end of filediff --git a/server/host/refused-handoffs.ts b/server/host/refused-handoffs.tsindex f0697955dc..a541eaaa5d 100644--- a/server/host/refused-handoffs.ts+++ b/server/host/refused-handoffs.ts@@ -5,14 +5,16 @@ import { namesTeammate, unreachableTeammate, type RosterMember } from "../peer-rimport { serverLine } from "../server-lines.ts";import type { Delivery } from "./person-inbox.ts";-type Refused = { target: RosterMember; from: RosterMember };+/** The run that made a call, as it was before the call went out: a call can outlive it. */+type Run = { id: string; routineId: string; botId: string };+type Refused = { target: RosterMember; from: RosterMember; run: string };export interface RefusedHandoffDeps {bots(): readonly RosterMember[];/** A bot `from` may never be told of (personal-reach.ts hides). */hides?(from: RosterMember, target: RosterMember): boolean;/** The run this conversation is, when it is one. */- runOf(threadId: string): { routineId: string; botId: string } | undefined;+ runOf(threadId: string): Run | undefined;/** The person key of who wrote this routine; absent off a Cloud and for a routine nobody wrote. */writerOf(routineId: string): string | undefined;/** Who this bot's failures in this conversation are for. */@@ -36,25 +38,25 @@ export function createRefusedHandoffs(deps: RefusedHandoffDeps) {}return {- /** A coordinate_bots call in a routine's run came back: remembers the bots it could not reach, and forgets one it has reached since. */- settled(threadId: string, from: RosterMember, named: readonly string[], status: number): void {- if (!deps.runOf(threadId)) return;+ /** A coordinate_bots call came back: remembers the bots the run that made it could not reach, and forgets one it has reached since. */+ settled(threadId: string, from: RosterMember, named: readonly string[], status: number, run: Run | undefined): void {+ if (!run) return;const seen = refused.get(threadId) ?? new Map<string, Refused>();for (const raw of named) {const target = status >= 400 ? unreachableTeammate(deps.bots(), from, raw, (bot) => deps.hides?.(from, bot) === true) : null;- if (target) seen.set(target.id, { target, from });+ if (target) seen.set(target.id, { target, from, run: run.id });if (status < 400) for (const [id, was] of seen) if (namesTeammate(was.target, raw)) seen.delete(id);}if (seen.size) refused.set(threadId, seen);else refused.delete(threadId);},- /** The run in this conversation is ending well: what it must say instead, once; null when nothing was refused. */+ /** The run in this conversation is ending well: what it must say instead, once; null when nothing was refused, or another run was. */take(threadId: string): string | null {- const seen = [...refused.get(threadId)?.values() ?? []];- if (!seen.length) return null;+ const run = deps.runOf(threadId), seen = [...refused.get(threadId)?.values() ?? []].filter((was) => was.run === run?.id);refused.delete(threadId);+ if (!seen.length) return null;const said = words(threadId, seen);- const run = deps.runOf(threadId), writer = run && deps.writerOf(run.routineId);+ const writer = run && deps.writerOf(run.routineId);const asked = run ? deps.askedPerson(run.botId, threadId) : null, inboxedAnyway = writer === asked && !deps.theirOwn(threadId, asked!);if (run && writer && !inboxedAnyway && toldWriter.get(run.routineId) !== said) {toldWriter.set(run.routineId, said);diff --git a/server/index.ts b/server/index.tsindex 36d0d7c3b8..954b81ad1e 100644--- a/server/index.ts+++ b/server/index.ts@@ -13792,8 +13792,8 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {}).safeParse(await readInternalBody());if (!parsed.success) return json(res, 400, { error: coordinateTooLong(parsed.error.issues) ?? `Provide 1-4 distinct botIds and a message of 1-${coordinateMessageMax} characters; thread is standing, new or the id of a thread you opened on that teammate, title one line of at most 80 characters, reply is resume or none, consult is a boolean, done_when at most 1000 characters, files at most 5 paths.` });const { groupId, botIds, message, reason, rework, thread, title, reply, consult, inMyFolder, done_when: doneWhen, answers, files: paths } = parsed.data;- const result = await handoffFiles.coordinate({ paths, answers, senderId: internalSender.id, recipients: botIds.length, saveOne: async (path) => saveTurnFile(internalCapability, internalSender.id, await attachmentVmForTurn(internalCapability), path, undefined, true),- send: (files) => coordinateBots({ groupId, botIds, message, reason: reason || undefined, rework, thread, title: title || undefined, reply, consult, inMyFolder, doneWhen: doneWhen || undefined, ...(answers ? { answers } : {}), ...(files ? { files } : {}) }) }); refusedHandoffs.settled(internalCapability.threadId, internalSender, botIds, result.status);+ const callRun = activeRoutineRunForThread(internalCapability.threadId) ?? undefined, result = await handoffFiles.coordinate({ paths, answers, senderId: internalSender.id, recipients: botIds.length, saveOne: async (path) => saveTurnFile(internalCapability, internalSender.id, await attachmentVmForTurn(internalCapability), path, undefined, true),+ send: (files) => coordinateBots({ groupId, botIds, message, reason: reason || undefined, rework, thread, title: title || undefined, reply, consult, inMyFolder, doneWhen: doneWhen || undefined, ...(answers ? { answers } : {}), ...(files ? { files } : {}) }) }); refusedHandoffs.settled(internalCapability.threadId, internalSender, botIds, result.status, callRun);return json(res, result.status, result.body);} if (method === "POST" && path === "/api/internal/post-to-thread") { const posted = await postToThread(await readInternalBody(), { senderId: internalSender.id, fromThreadId: internalCapability.threadId, store, coordinateBots, waitsOn: (threadId) => threadWaitsOn(coordinator, internalCapability.roomHandoffId ?? internalCapability.generation, threadId) }); return json(res, posted.status, posted.body); }if (method === "GET" && path === "/api/internal/room-targets") {