Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(chief): a retried question stays a question; a Chief in a room retries as a turn
AdityaUmalee5b6e12df695
diff --git a/server/host/chief-notices-retry.test.ts b/server/host/chief-notices-retry.test.tsindex f2b54a26e5..5f7fb51980 100644--- a/server/host/chief-notices-retry.test.ts+++ b/server/host/chief-notices-retry.test.ts@@ -357,7 +357,7 @@ describe("a run the server had no room to start", () => {describe("a Chief's retry of a thread that failed the Chief's own request", () => {const sent = { status: 200, body: { receipts: [{ botId: "ada", botName: "Ada", outcome: "queued", threadId: "ada-job" }] } };- const node = (over: Record<string, unknown>) => ({ id: "child", parentId: "chief-turn", threadId: "ada-job", botId: "ada", status: "failed", reply: "resume", ...over });+ const node = (over: Record<string, unknown>) => ({ id: "child", parentId: "chief-turn", threadId: "ada-job", botId: "ada", status: "failed", reply: "resume", mode: "work", ...over });const coordinator = (last: Record<string, unknown> | undefined) => ({inThread: () => (last ? [node({ id: "older", status: "completed" }), last] : []) as never,node: (id: string) => (id === "chief-turn" ? { id, botId: "chief", threadId: "chief-main" } : undefined) as never,@@ -365,7 +365,8 @@ describe("a Chief's retry of a thread that failed the Chief's own request", () =const requester = (last: Record<string, unknown> | undefined) => failedRequester(coordinator(last), "ada-job");it("knows which bot asked, with reply resume, for the failed work, and nobody for anything else", () => {- expect(requester(node({}))).toBe("chief");+ expect(requester(node({}))).toEqual({ botId: "chief", consult: false });+ expect(requester(node({ mode: "consult" }))).toEqual({ botId: "chief", consult: true });expect(requester(node({ status: "completed" }))).toBeUndefined();expect(requester(node({ reply: "none" }))).toBeUndefined();expect(requester(node({ parentId: undefined }))).toBeUndefined();@@ -374,7 +375,7 @@ describe("a Chief's retry of a thread that failed the Chief's own request", () =it("asks again from the Chief's own turn, so the result wakes the Chief, and says so", async () => {const coordinateBots = vi.fn(async () => sent);- const f = fixture({ failedRequester: () => "chief" });+ const f = fixture({ failedRequester: () => ({ botId: "chief", consult: false }) });const how = await f.notices.retryThread({ chief: f.chief as never, bot: f.ada as never, threadId: "ada-job", unattended: true, note: "The service was down.", coordinateBots });expect(f.startTurn).not.toHaveBeenCalled();expect(coordinateBots).toHaveBeenCalledWith(expect.objectContaining({ botIds: ["ada"], thread: "ada-job", reply: "resume", consult: false }));@@ -382,9 +383,16 @@ describe("a Chief's retry of a thread that failed the Chief's own request", () =expect(retriedMessage("Ada", "Invoices", "request")).toContain("resumes you");});+ it("asks a failed question again as a question, so the teammate still acts on nothing", async () => {+ const coordinateBots = vi.fn(async () => sent);+ const f = fixture({ failedRequester: () => ({ botId: "chief", consult: true }) });+ expect(await f.notices.retryThread({ chief: f.chief as never, bot: f.ada as never, threadId: "ada-job", unattended: false, coordinateBots })).toBe("request");+ expect(coordinateBots).toHaveBeenCalledWith(expect.objectContaining({ reply: "resume", consult: true }));+ });+it("tells the Chief why when that request could not be sent", async () => {const coordinateBots = vi.fn(async () => ({ status: 403, body: { error: "not reachable" } }));- const f = fixture({ failedRequester: () => "chief" });+ const f = fixture({ failedRequester: () => ({ botId: "chief", consult: false }) });await expect(f.notices.retryThread({ chief: f.chief as never, bot: f.ada as never, threadId: "ada-job", unattended: true, coordinateBots })).rejects.toThrow("not reachable");expect(f.startTurn).not.toHaveBeenCalled();});@@ -392,7 +400,7 @@ describe("a Chief's retry of a thread that failed the Chief's own request", () =it("retries every other thread as before: another bot's request, the person's own work, or OpenMausBot's automatic retry", async () => {for (const [requested, automatic] of [["someone-else", false], [undefined, false], ["chief", true]] as const) {const coordinateBots = vi.fn(async () => sent);- const f = fixture({ failedRequester: () => requested });+ const f = fixture({ failedRequester: () => (requested ? { botId: requested, consult: false } : undefined) });const how = await f.notices.retryThread({ chief: f.chief as never, bot: f.ada as never, threadId: "ada-job", unattended: true, coordinateBots, ...(automatic ? { automatic: true as const } : {}) });expect(coordinateBots).not.toHaveBeenCalled();expect(f.turnsOf("ada")).toHaveLength(1);diff --git a/server/host/chief-notices.ts b/server/host/chief-notices.tsindex 984a85d13b..3e8ea16685 100644--- a/server/host/chief-notices.ts+++ b/server/host/chief-notices.ts@@ -22,8 +22,8 @@ type PeerAsk = NonNullable<Message["peerAsk"]>;export interface ChiefNoticeDeps {store: Pick<Store, "bots" | "bot" | "tasks" | "taskByThread" | "groupByThread" | "createTask" | "appendMessage" | "messagesFor" | "activePath"> & Partial<Pick<Store, "patchMessage">>;assignerOf(threadId: string): unknown;- /** The bot whose reply-resume request this thread's newest assignment was, when that assignment failed (`failedRequester`). */- failedRequester?(threadId: string): string | undefined;+ /** Who asked, with reply resume, for this thread's newest assignment, and whether it was a question, when it failed (`failedRequester`). */+ failedRequester?(threadId: string): FailedRequest | undefined;notify(notification: Notification | null): void;threadBusy(botId: string, threadId: string): boolean;botAtThreadCapacity(botId: string): boolean;@@ -79,10 +79,14 @@ export const SERVER_MEMORY_RETRY_TEXT = "[OpenMausBot started this again by itseexport const ROOM_RETRY_TEXT = "[OpenMausBot started this again by itself, once: the last run could not start because this server had no room for another program. "+ "Continue the request above from where it stopped and finish it. If the same problem comes back, say exactly what is blocking and stop.]";-/** The bot that asked, with reply resume, for the work this thread's newest assignment was, when that assignment failed. */-export function failedRequester(coordinator: Pick<Coordinator, "inThread" | "node">, threadId: string): string | undefined {+/** The bot that asked, with reply resume, for this thread's newest assignment, and whether it asked a question (consult). */+export interface FailedRequest { botId: string; consult: boolean }++/** Who asked for the work this thread's newest assignment was, when that assignment failed. */+export function failedRequester(coordinator: Pick<Coordinator, "inThread" | "node">, threadId: string): FailedRequest | undefined {const last = coordinator.inThread(threadId).at(-1);- return last?.status === "failed" && last.reply === "resume" && last.parentId ? coordinator.node(last.parentId)?.botId : undefined;+ const botId = last?.status === "failed" && last.reply === "resume" && last.parentId ? coordinator.node(last.parentId)?.botId : undefined;+ return last && botId ? { botId, consult: last.mode === "consult" } : undefined;}/** What retry_thread tells the Chief: a retry of its own failed request comes back to it; any other result stays there. */@@ -139,14 +143,16 @@ export function createChiefNotices(deps: ChiefNoticeDeps) {/** The one way a teammate's stopped thread runs again: a Chief's retry_thread and OpenMausBot's automatic retry both* start it here, after the same refusal checks. Throws the refusal, or why the start failed. */async function retryThread(input: { chief: BotRecord; bot: BotRecord; threadId: string; unattended: boolean; note?: string; automatic?: true;- /** The retrying Chief's own coordinate_bots, from its current turn. */+ /** The retrying Chief's own coordinate_bots, from its current turn; left out from a room, where it would address the room, not this thread. */coordinateBots?: (call: CoordinateBotsCall) => Promise<{ status: number; body: Record<string, unknown> }> }): Promise<"request" | "turn"> {const refused = retryRefusal(input.bot.id, input.threadId);if (refused) throw new Error(refused);// The Chief's own request failed here: it asks again from its current turn, so the result wakes it as the first would have.- if (!input.automatic && input.coordinateBots && deps.failedRequester?.(input.threadId) === input.chief.id) {+ const failed = input.automatic || !input.coordinateBots ? undefined : deps.failedRequester?.(input.threadId);+ if (input.coordinateBots && failed?.botId === input.chief.id) {const note = input.note ? ` Note: ${quoteInNote(input.note, 300)}` : "";- const sent = await input.coordinateBots({ botIds: [input.bot.id], thread: input.threadId, reply: "resume", consult: false, rework: false,+ // A question stays a question: asked again, the teammate still acts on nothing.+ const sent = await input.coordinateBots({ botIds: [input.bot.id], thread: input.threadId, reply: "resume", consult: failed.consult, rework: false,message: `Your last run in this thread stopped before it finished my request, so I am asking again.${note} Continue the request above from where it stopped and finish it. If the same problem comes back, say exactly what is blocking and stop.` });const receipt = (sent.body.receipts as PeerDeliveryReceipt[] | undefined)?.[0];if (sent.status !== 200 || !receipt || receipt.outcome === "failed") throw new Error(String(receipt?.detail ?? sent.body.error ?? "Not delivered."));diff --git a/server/index.ts b/server/index.tsindex 35f3ba0777..00df8a1211 100644--- a/server/index.ts+++ b/server/index.ts@@ -13728,7 +13728,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {requireActiveInternalCapability();let retried: "request" | "turn";try {- retried = await retryThread({ chief: from, bot: target, threadId, note, unattended: isUnattended(from.id, fromThreadId), coordinateBots });+ retried = await retryThread({ chief: from, bot: target, threadId, note, unattended: isUnattended(from.id, fromThreadId), ...(store.groupByThread(fromThreadId) ? {} : { coordinateBots }) });} catch (error) {return json(res, 409, { error: error instanceof Error ? error.message : String(error) });}