Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Keep the inbox in order with one sequence number
milind-soni18fd6a365d70
diff --git a/src/state/inbox.test.ts b/src/state/inbox.test.tsindex ed08f49090..248ec24dd2 100644--- a/src/state/inbox.test.ts+++ b/src/state/inbox.test.ts@@ -1,8 +1,8 @@import { afterEach, beforeEach, expect, it, vi } from "vitest";import type { InboxView } from "../../shared/inbox";-import { inboxView, messageRow, reportRow } from "@/testing/inbox-fixture";+import { inboxView, messageRow } from "@/testing/inbox-fixture";import { api } from "./api";-import { currentInbox, doneInboxRow, forgetInbox, readInboxRow, refreshInbox, replyToInboxRow, setInboxView } from "./inbox";+import { currentInbox, doneInboxRow, readInboxRow, refreshInbox, setInboxView } from "./inbox";vi.mock("./api", () => ({ api: vi.fn() }));@@ -15,6 +15,11 @@ const reply = async (index: number, view: InboxView) => {requests[index]!.resolve(view);await flush();};+const fail = async (index: number) => {+ requests[index]!.settled = true;+ requests[index]!.reject(new Error("offline"));+ await flush();+};beforeEach(() => {requests = [];@@ -31,209 +36,55 @@ afterEach(async () => {vi.clearAllMocks();});-it("keeps Done when an older read response arrives last, then reconciles the discarded snapshot", async () => {+it("keeps Done when the read sent before it answers last", async () => {readInboxRow(messageRow);doneInboxRow(messageRow);await reply(1, done);await reply(0, inboxView([{ ...messageRow, unread: false }]));- expect(currentInbox()?.rows[0]?.tab).toBe("done");- expect(requests[2]?.path).toBe("/api/inbox");- await reply(2, inboxView([{ ...done.rows[0]!, text: "Fresh server text" }]));- expect(currentInbox()?.rows[0]?.text).toBe("Fresh server text");+ expect(currentInbox()?.rows[0]).toMatchObject({ tab: "done", unread: false });+ expect(requests.map((request) => request.path)).toEqual(["/api/inbox/line-message/read", "/api/inbox/line-message/done"]);});-it("discards a GET started before a successful mutation and reads again", async () => {+it("drops a list read before Done, whichever answers first", async () => {const loading = refreshInbox();doneInboxRow(messageRow);- await reply(1, done);await reply(0, inboxView([messageRow]));expect(currentInbox()?.rows[0]?.tab).toBe("done");- expect(requests[2]?.path).toBe("/api/inbox");- await reply(2, done);+ await reply(1, done);await loading;+ expect(currentInbox()?.rows[0]?.tab).toBe("done");++ setInboxView(inboxView([messageRow]));+ const again = refreshInbox();+ doneInboxRow(messageRow);+ await reply(3, done);+ await reply(2, inboxView([messageRow]));+ await again;+ expect(currentInbox()?.rows[0]?.tab).toBe("done");});-it("keeps a newer refresh when an older mutation response arrives afterward", async () => {+it("keeps a newer list when an older read answers after it", async () => {readInboxRow(messageRow);const loading = refreshInbox();await reply(1, inboxView([{ ...messageRow, unread: false, text: "Newer text" }]));await loading;await reply(0, inboxView([{ ...messageRow, unread: false }]));expect(currentInbox()?.rows[0]?.text).toBe("Newer text");- expect(requests[2]?.path).toBe("/api/inbox");-});--it("overlays pending Done on a GET and refreshes after that mutation settles", async () => {- doneInboxRow(messageRow);- const loading = refreshInbox();- await reply(1, inboxView([messageRow]));- await loading;- expect(currentInbox()?.rows[0]).toMatchObject({ tab: "done", unread: false });- await reply(0, done);- expect(requests[2]?.path).toBe("/api/inbox");-});--it("keeps another row's pending read when a Done snapshot lands", async () => {- setInboxView(inboxView([messageRow, reportRow]));- readInboxRow(messageRow);- doneInboxRow(reportRow);- await reply(1, inboxView([messageRow, { ...reportRow, tab: "done" }]));- expect(currentInbox()?.rows[0]?.unread).toBe(false);- expect(currentInbox()?.rows[1]?.tab).toBe("done");});-it("discards a GET started during a mutation that completed before the GET landed", async () => {- doneInboxRow(messageRow);- const loading = refreshInbox();- await reply(0, done);- await reply(1, inboxView([messageRow]));- expect(currentInbox()?.rows[0]?.tab).toBe("done");- expect(requests[2]?.path).toBe("/api/inbox");- await reply(2, done);- await loading;-});--it("discards a GET that predates a successful message reply", async () => {- const loading = refreshInbox();- const sending = replyToInboxRow(inboxView(), messageRow, "Thanks");- await reply(1, done);- await sending;- await reply(0, inboxView([{ ...messageRow, text: "Old snapshot" }]));- expect(currentInbox()?.rows[0]?.text).toBe(messageRow.text);- expect(requests[2]?.path).toBe("/api/inbox");- await reply(2, done);- await loading;- expect(currentInbox()?.rows[0]?.tab).toBe("done");-});--it("removes a failed mutation's overlay and reconciles from the server", async () => {- doneInboxRow(messageRow);- requests[0]!.settled = true;- requests[0]!.reject(new Error("offline"));- await flush();- expect(requests[1]?.path).toBe("/api/inbox");- await reply(1, inboxView([messageRow]));- expect(currentInbox()?.rows[0]).toMatchObject({ tab: "info", unread: true });-});--it.each(["done", "read"] as const)("removes failed %s before recovery and reconciles after a failed GET", async (what) => {+it.each(["done", "read"] as const)("puts the row back when %s fails and the read after it fails too", async (what) => {if (what === "done") doneInboxRow(messageRow);else readInboxRow(messageRow);expect(currentInbox()?.rows[0]?.unread).toBe(false);- requests[0]!.settled = true;- requests[0]!.reject(new Error("offline"));- await flush();- const beforeRecovery = currentInbox();+ await fail(0);+ expect(currentInbox()?.rows[0]).toMatchObject({ tab: "info", unread: true });expect(requests[1]?.path).toBe("/api/inbox");- requests[1]!.settled = true;- requests[1]!.reject(new Error("offline"));- await flush();+ await fail(1);expect(requests).toHaveLength(2);expect(currentInbox()?.rows[0]).toMatchObject({ tab: "info", unread: true });- expect(beforeRecovery?.rows[0]).toMatchObject({ tab: "info", unread: true });+const loading = refreshInbox();await reply(2, inboxView([{ ...messageRow, text: "Reconciled text", unread: false }]));await loading;expect(currentInbox()?.rows[0]).toMatchObject({ tab: "info", unread: false, text: "Reconciled text" });});--it("keeps another row's pending read when Done and recovery fail", async () => {- setInboxView(inboxView([messageRow, { ...reportRow, unread: true }]));- doneInboxRow(messageRow);- readInboxRow({ ...reportRow, unread: true });- requests[0]!.settled = true;- requests[0]!.reject(new Error("offline"));- await flush();- expect(currentInbox()?.rows[0]).toMatchObject({ tab: "info", unread: true });- expect(currentInbox()?.rows[1]?.unread).toBe(false);- requests[2]!.settled = true;- requests[2]!.reject(new Error("offline"));- await flush();- expect(currentInbox()?.rows[0]?.tab).toBe("info");- expect(currentInbox()?.rows[1]?.unread).toBe(false);-});--it("keeps confirmed Done when an overlapping read and recovery fail", async () => {- doneInboxRow(messageRow);- readInboxRow(messageRow);- await reply(0, done);- requests[1]!.settled = true;- requests[1]!.reject(new Error("offline"));- await flush();- expect(currentInbox()?.rows[0]).toMatchObject({ tab: "done", unread: false });- requests[2]!.settled = true;- requests[2]!.reject(new Error("offline"));- await flush();- await reply(3, done);- expect(currentInbox()?.rows[0]).toMatchObject({ tab: "done", unread: false });-});--it("keeps confirmed state and pending changes when message reply and recovery fail", async () => {- setInboxView(inboxView([messageRow, { ...reportRow, unread: true }]));- const sending = replyToInboxRow(inboxView(), messageRow, "Thanks").catch((error: unknown) => error);- readInboxRow({ ...reportRow, unread: true });- requests[0]!.settled = true;- requests[0]!.reject(new Error("offline"));- await flush();- expect(await sending).toBeInstanceOf(Error);- requests[2]!.settled = true;- requests[2]!.reject(new Error("offline"));- await flush();- expect(currentInbox()?.rows[0]).toMatchObject({ tab: "info", unread: true, text: messageRow.text });- expect(currentInbox()?.rows[1]?.unread).toBe(false);- const loading = refreshInbox();- await reply(3, inboxView([{ ...messageRow, text: "Reconciled reply" }, { ...reportRow, unread: true }]));- await loading;- expect(currentInbox()?.rows[0]?.text).toBe("Reconciled reply");- expect(currentInbox()?.rows[1]?.unread).toBe(false);-});--it("keeps the same row's pending read when Done fails", async () => {- doneInboxRow(messageRow);- readInboxRow(messageRow);- requests[0]!.settled = true;- requests[0]!.reject(new Error("offline"));- await flush();- expect(currentInbox()?.rows[0]).toMatchObject({ tab: "info", unread: false });-});--it("removes failed Done from the latest confirmed snapshot", async () => {- doneInboxRow(messageRow);- const loading = refreshInbox();- await reply(1, inboxView([{ ...messageRow, text: "Other client's edit" }]));- await loading;- requests[0]!.settled = true;- requests[0]!.reject(new Error("offline"));- await flush();- expect(currentInbox()?.rows[0]).toMatchObject({ tab: "info", unread: true, text: "Other client's edit" });-});--it("drops a GET that lands after the inbox is forgotten, and reads nothing more", async () => {- const loading = refreshInbox();- forgetInbox();- await reply(0, inboxView([messageRow]));- await loading;- expect(currentInbox()).toBeNull();- expect(requests).toHaveLength(1);-});--it("drops a read or Done answer that lands after the inbox is forgotten, and reads nothing more", async () => {- readInboxRow(messageRow);- doneInboxRow(messageRow);- forgetInbox();- await reply(0, inboxView([{ ...messageRow, unread: false }]));- requests[1]!.settled = true;- requests[1]!.reject(new Error("offline"));- await flush();- expect(currentInbox()).toBeNull();- expect(requests).toHaveLength(2);-});--it("reads nothing after a message reply when the inbox was forgotten meanwhile", async () => {- const sending = replyToInboxRow(inboxView(), messageRow, "Thanks");- forgetInbox();- await reply(0, done);- await sending;- expect(currentInbox()).toBeNull();- expect(requests).toHaveLength(1);-});diff --git a/src/state/inbox.ts b/src/state/inbox.tsindex 73196d39aa..866808666c 100644--- a/src/state/inbox.ts+++ b/src/state/inbox.ts@@ -41,6 +41,16 @@ export const setInboxSupport = support.set;/** Tests and previews show a list of their own. */export const setInboxView = (view: InboxView | null): void => { list.set(view); support.set(view ? "yes" : "unknown"); };+// Every read and every change here takes the next number; a list from the server lands only if nothing later was shown,+// so an older answer never moves a row back.+let sent = 0, shown = 0;+const show = (seq: number, view: InboxView) => {+ if (seq < shown) return;+ shown = seq;+ list.set(view);+ support.set("yes");+};+let reading: Promise<void> | null = null;let readAgain = false;/** Reads the list now, or once more after the read on its way, so the newest answer always lands last. */@@ -50,11 +60,12 @@ export function refreshInbox(): Promise<void> {try {do {readAgain = false;+ const seq = ++sent;const view = await api<InboxView>("/api/inbox", { background: true }).catch((error: unknown) => {if ((error as { status?: number }).status === 404) { list.set(null); support.set("no"); }return null;});- if (view && Array.isArray(view.rows)) { list.set(view); support.set("yes"); }+ if (view && Array.isArray(view.rows)) show(seq, view);} while (readAgain);} finally {reading = null;@@ -77,21 +88,25 @@ export function useInboxLoader(version: string): void {}const patchRow = (id: string, patch: Partial<InboxRow>) => {+ shown = ++sent;const view = list.get();if (view) list.set({ ...view, rows: view.rows.map((row) => (row.id === id ? { ...row, ...patch } : row)) });};-async function mark(id: string, what: "read" | "done"): Promise<void> {+/** Tells the server; if it can't be told, the row goes back to how it was (`undo`) and the list is read again. */+async function mark(id: string, what: "read" | "done", undo: Partial<InboxRow>): Promise<void> {+ const seq = ++sent;const view = await api<InboxView>(`/api/inbox/${id}/${what}`, { method: "POST", background: true }).catch(() => null);- if (view && Array.isArray(view.rows)) list.set(view);- else void refreshInbox();+ if (view && Array.isArray(view.rows)) return show(seq, view);+ patchRow(id, undo);+ void refreshInbox();}/** The person read a line: it is no longer bold. */export function readInboxRow(row: Pick<InboxRow, "id" | "unread">): void {if (!row.unread) return;patchRow(row.id, { unread: false });- void mark(row.id, "read");+ void mark(row.id, "read", { unread: true });}/** An ask the person just answered here: it leaves the list at once, before the server's next read says so. */@@ -99,8 +114,9 @@ export const settleInboxRow = (row: Pick<InboxRow, "id">): void => patchRow(row./** The person is done with a line of news: it moves to Done. */export function doneInboxRow(row: Pick<InboxRow, "id">): void {+ const before = list.get()?.rows.find((one) => one.id === row.id);patchRow(row.id, { tab: "done", unread: false });- void mark(row.id, "done");+ void mark(row.id, "done", before ? { tab: before.tab, unread: before.unread } : {});}/** A reply to a line of news that waits on one: it goes back where the line came from, as a Reply in the chat does. */