Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Inbox: hand-off results file no line (#5872, made automatic)
milind-soni9759c44067bf
diff --git a/server/host/place-inbox.test.ts b/server/host/place-inbox.test.tsindex e3bcca95eb..253304d04a 100644--- a/server/host/place-inbox.test.ts+++ b/server/host/place-inbox.test.ts@@ -1,4 +1,4 @@-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { afterEach, describe, expect, it } from "vitest";@@ -66,67 +66,49 @@ function setup(over: Partial<PlaceInboxDeps> = {}, dataDir = mkdtempSync(join(tm}describe("finished work reaches the inbox once", () => {- const work = (threadId: string, text = "Pricing page updated.") => ({ botId: "mira", threadId, text });- const asked = { botId: "eli", threadId: "eli-main" };const summary = { kind: "done", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Done", body: "All three pages are live." } as Notification;+ const routine = { kind: "done", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Done", body: "Weekly report." } as Notification;+ const failure = (threadId: string) => ({ kind: "turn-failed", botId: "eli", botName: "Eli", threadId, title: "Failed", body: `It stopped in ${threadId}.` }) as Notification;const infoRows = (f: ReturnType<typeof setup>) => f.inbox.view(OWNER).rows.filter((row) => row.tab === "info");- it("files handed-off work as an unread line, opening its own thread, and sends nothing", () => {+ it("files the asking bot's summary and a routine's report, and nothing of its own for work handed on", () => {const f = setup();- f.inbox.handedResult(work("mira-pricing"), asked);- expect(infoRows(f)).toMatchObject([{ kind: "result", from: { name: "Mira", botId: "mira" }, workingFor: { name: "Eli" }, text: "Pricing page updated.", unread: true,- origin: { botId: "mira", threadId: "mira-pricing" } }]);+ f.inbox.route(routine, { routine: true });+ f.tick(1);+ f.inbox.route(summary, { summary: true });+ expect(infoRows(f).map((row) => row.text)).toEqual(["All three pages are live.", "Weekly report."]);+ expect(f.inbox.count(OWNER)).toBe(2);expect(f.sent).toEqual([]);- expect(f.inbox.count(OWNER)).toBe(1);});- it("keeps one row per job: the next teammate's result and then the summary replace it", () => {+ it("keeps one row per job: the next summary replaces the last one the person has not read", () => {const f = setup();- f.inbox.handedResult(work("mira-pricing"), asked);- f.tick(1);- f.inbox.handedResult(work("mira-launch", "Launch post drafted."), asked);- expect(infoRows(f).map((row) => row.text)).toEqual(["Launch post drafted."]);+ f.inbox.route({ kind: "delegation-settled", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Results", body: "Results from Mira" } as Notification, { summary: true });+ f.inbox.route({ kind: "delegation-settled", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Results", body: "ignored" } as Notification);+ expect(infoRows(f).map((row) => row.text)).toEqual(["Results from Mira"]);f.tick(1);f.inbox.route(summary, { summary: true });expect(infoRows(f)).toMatchObject([{ kind: "result", from: { name: "Eli" }, text: "All three pages are live.", origin: { threadId: "eli-main" } }]);});- it("leaves what the person already read, other jobs and routine reports alone", () => {+ it("still files a failure in work another bot handed on, saying whose work it was", () => {const f = setup();- f.inbox.handedResult(work("mira-pricing"), asked);- f.inbox.markRead(OWNER, infoRows(f)[0]!.id);- f.inbox.handedResult(work("mira-launch", "Second."), { botId: "eli", threadId: "eli-launch" });- f.inbox.route({ kind: "done", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Done", body: "Weekly report." } as Notification, { routine: true });- f.tick(1);- f.inbox.route(summary, { summary: true });- expect(infoRows(f).map((row) => row.text).sort()).toEqual(["All three pages are live.", "Pricing page updated.", "Second.", "Weekly report."]);+ f.handed.set("eli-pricing", "mira");+ f.inbox.route(failure("eli-pricing"));+ expect(infoRows(f)).toMatchObject([{ kind: "notice", text: "It stopped in eli-pricing.", workingFor: { botId: "mira", name: "Mira" } }]);});- it("adds no line for a muted bot, an empty reply or a plain turn's done, and a restart keeps the row", () => {+ it("drops a hand-off result filed before hand-offs stopped filing lines, and keeps the rest", () => {const f = setup();- f.bots.mira!.notifications = false;- f.inbox.handedResult(work("mira-pricing"), asked);- f.bots.mira!.notifications = true;- f.inbox.handedResult(work("mira-pricing", " "), asked);- f.inbox.route({ kind: "done", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Done", body: "Plain." } as Notification);- expect(infoRows(f)).toEqual([]);-- f.inbox.handedResult(work("mira-pricing"), asked);+ f.handed.set("eli-pricing", "mira");+ f.inbox.route(failure("eli-pricing"));+ f.inbox.route(summary, { summary: true });+ const file = join(f.dataDir, PLACE_INBOX_FILE);+ const saved = JSON.parse(readFileSync(file, "utf8")) as { lines: Array<Record<string, unknown>> };+ saved.lines.push({ id: "old-hand-off", botId: "mira", threadId: "mira-pricing", kind: "result", from: "Mira", workingFor: { botId: "eli", name: "Eli" }, text: "Pricing page updated.", at: 1_000, job: "eli-main" });+ writeFileSync(file, JSON.stringify(saved));const reopened = setup({}, f.dataDir);- expect(infoRows(reopened).map((row) => row.text)).toEqual(["Pricing page updated."]);- reopened.inbox.route(summary, { summary: true });- expect(infoRows(reopened).map((row) => row.text)).toEqual(["All three pages are live."]);- });-- it("files a resumed answer's results notice under the same job, and keeps a guest's work theirs", () => {- const f = setup();- f.inbox.route({ kind: "delegation-settled", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Results", body: "Results from Mira" } as Notification, { summary: true });- f.inbox.route({ kind: "delegation-settled", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Results", body: "ignored" } as Notification);- expect(infoRows(f).map((row) => row.text)).toEqual(["Results from Mira"]);- f.guests.set("guest-chat", "p_guest");- f.inbox.handedResult(work("mira-pricing", "For the guest."), { botId: "eli", threadId: "guest-chat" });- expect(f.inbox.view(GUEST).rows.map((row) => row.text)).toEqual(["For the guest."]);- expect(infoRows(f).map((row) => row.text)).toEqual(["Results from Mira"]);+ expect(infoRows(reopened).map((row) => row.text).sort()).toEqual(["All three pages are live.", "It stopped in eli-pricing."]);});});diff --git a/server/host/place-inbox.ts b/server/host/place-inbox.tsindex ed93352248..f868f58a88 100644--- a/server/host/place-inbox.ts+++ b/server/host/place-inbox.ts@@ -1,7 +1,8 @@// The inbox of a place that is no team's: this computer, or a Cloud of one person. There is one person here, so nothing is// copied and nothing routed. An ask is the open card itself, read where it was asked, so one tap answers the one card// however many surfaces show it, and whatever was waiting before this inbox existed is listed. What a bot sent the person-// is a line of its conversation, and a reply there is an ordinary line of theirs.+// is a line of its conversation, and a reply there is an ordinary line of theirs. Work one bot handed another files no line+// when it finishes: that is the asking bot's news, and its own answer to the person is what lands here.// Security: a guest's conversation stays out of the owner's lines, and the owner's stays out of a guest's. An ask is listed// only for someone who may answer it, so a guest's questions reach the owner labelled with who asked, never as their own.import { readFileSync } from "node:fs";@@ -90,7 +91,8 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {let seenAsks = new Set<string>();try {const parsed = fileSchema.safeParse(JSON.parse(readFileSync(file, "utf8")));- if (parsed.success) { lines = parsed.data.lines; seenAsks = new Set(parsed.data.seenAsks); }+ // A hand-off result filed before hand-offs stopped filing lines (its job is the asking conversation, not its own) goes.+ if (parsed.success) { lines = parsed.data.lines.filter((line) => !(line.kind === "result" && line.workingFor && line.job && line.job !== line.threadId)); seenAsks = new Set(parsed.data.seenAsks); }else console.warn(`place inbox: ${file} is not an inbox file; starting fresh`);} catch (error) {if ((error as NodeJS.ErrnoException).code !== "ENOENT") console.warn(`place inbox: ${file} is unreadable; starting fresh`);@@ -235,18 +237,6 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {return notification;}- /** Work handed on finished: one line, so the result is here even if the conversation that asked never reports back. It- * sends nothing, and that conversation's next result or its summary replaces it. */- function handedResult(work: { botId: string; threadId: string; groupId?: string; text: string }, requester: { botId: string; threadId: string }): void {- const bot = store.bot(work.botId);- const text = work.text.trim();- if (!bot || bot.notifications === false || !text) return;- const asked = store.bot(requester.botId);- const guest = deps.guestOf(requester.threadId);- addLine({ id: crypto.randomUUID(), botId: bot.id, threadId: work.threadId, ...(work.groupId ? { groupId: work.groupId } : {}), kind: "result", from: bot.name.slice(0, 200),- ...(asked ? { workingFor: { botId: asked.id, name: asked.name } } : {}), ...(guest ? { guest } : {}), text: clip(text, NOTICE_TEXT_MAX), at: now(), job: requester.threadId });- }-const unsubscribe = store.onChange((change: StoreChange) => {if (change.type !== "thread.deleted") return;const before = lines.length;@@ -258,7 +248,6 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {view,message,route,- handedResult,/** What waits on this viewer: every open ask, and news they have not read. */count(auth: RequestAuth): number {return view(auth).rows.filter((row) => row.tab === "needs-you" || (row.tab === "info" && row.unread)).length;diff --git a/server/host/team-messages.ts b/server/host/team-messages.tsindex 3f1c0b47c2..16b4b43190 100644--- a/server/host/team-messages.ts+++ b/server/host/team-messages.ts@@ -428,10 +428,6 @@ export function createTeamMessages(deps: TeamMessagesDeps) {route(notification: Notification, how: { routine?: boolean; summary?: boolean } = {}): Notification | null {return !team && deps.place ? deps.place.route(notification, how) : inbox.route(notification, how);},- /** Work handed on finished: on a place that is no team's, a line in the inbox (place-inbox.ts). */- handedResult(work: { botId: string; threadId: string; groupId?: string; text: string }, requester: { botId: string; threadId: string }): void {- if (!team) deps.place?.handedResult(work, requester);- },stop(): void {unsubscribe();inbox.stop();diff --git a/server/index.ts b/server/index.tsindex e5c9bd32d2..0c8da34057 100644--- a/server/index.ts+++ b/server/index.ts@@ -3970,7 +3970,7 @@ const handoffTicks = createHandoffTicks(), coordinator: Coordinator = createCoorif (parent.groupId ? !group || !store.groupTaskByThread(group.id, parent.threadId) : !store.taskByThread(parent.botId, parent.threadId)) return;const bot = store.bot(child.botId);if (store.messagesFor(parent.threadId).some(m => m.roomRequest?.id === child.id && m.roomRequest.phase === "result")) return;- const problem = roomHandoffProblem(child, parent); if (child.status === "completed" && !problem && child.mode === "work" && !child.statusOnly) teamMessages.handedResult({ botId: child.botId, threadId: child.threadId, ...(child.groupId ? { groupId: child.groupId } : {}), text: redactSecretsInText(child.result) }, { botId: parent.botId, threadId: parent.threadId });+ const problem = roomHandoffProblem(child, parent);store.appendMessage(parent.threadId, {role: "bot", kind: "activity",roomRequest: { id: child.id, phase: "result" },diff --git a/server/place-inbox.e2e.test.ts b/server/place-inbox.e2e.test.tsindex c8dbfa8951..9ab0ff6a40 100644--- a/server/place-inbox.e2e.test.ts+++ b/server/place-inbox.e2e.test.ts@@ -167,11 +167,12 @@ describe.each([expect((await call("POST", `/api/threads/${row.origin!.threadId}/respond`, { requestId: row.card!.requestId, behavior: "answer", message: "Home" })).status).toBe(200);await until(async () => (await h.messages(row.origin!.threadId)).some((line) => line.text === "W1-E: Home"), "the work to go on");expect((await h.listed()).some((one) => one.id === row.id)).toBe(false);- // The finished work leaves one news line, which Mira's summary replaces once she reports back; reading it clears it.+ // Eli's finished work for Mira files no line of its own: Mira's summary to the person is the one news line; reading it clears it.const results = async () => (await h.listed()).filter((one) => one.kind === "result" && (one.from.name === mira.name || one.from.name === eli.name));await until(async () => (await results()).some((one) => one.from.name === mira.name), "Mira's summary in the inbox");const news = await results();expect(news).toHaveLength(1);+ expect(news[0]!.from.name).toBe(mira.name);expect(news[0]!.unread).toBe(true);expect((await call("POST", `/api/inbox/${news[0]!.id}/read`)).status).toBe(200);expect(await h.count()).toBe(0);