Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(inbox): remove failed optimistic changes before recovery
Sebastian B392bb6753d57
diff --git a/src/state/inbox.test.ts b/src/state/inbox.test.tsindex 9716baa558..0fc99946f9 100644--- a/src/state/inbox.test.ts+++ b/src/state/inbox.test.ts@@ -115,3 +115,95 @@ it("removes a failed mutation's overlay and reconciles from the server", async (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) => {+ 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();+ expect(requests[1]?.path).toBe("/api/inbox");+ requests[1]!.settled = true;+ requests[1]!.reject(new Error("offline"));+ await flush();+ 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" });+});diff --git a/src/state/inbox.ts b/src/state/inbox.tsindex 63cbdbb9d7..cf8eae3ff2 100644--- a/src/state/inbox.ts+++ b/src/state/inbox.ts@@ -29,14 +29,15 @@ function store<T>(initial: T) {const list = store<InboxView | null>(null);export const currentInbox = list.get;export const useInboxView = (): InboxView | null => useSyncExternalStore(list.subscribe, list.get, list.get);-/** Tests and previews show a list of their own. */-export const setInboxView = list.set;let reading: Promise<void> | null = null;let readAgain = false;let revision = 0;+let confirmed: InboxView | null = null;const pending = new Map<number, { id: string; patch: Partial<InboxRow> }>();-const replaceList = (view: InboxView) => {+const publishList = () => {+ const view = confirmed;+ if (!view) { list.set(null); return; }const rows = view.rows.map((row) => {for (const change of pending.values()) {if (change.id === row.id) row = { ...row, ...change.patch };@@ -45,6 +46,9 @@ const replaceList = (view: InboxView) => {});list.set({ ...view, rows });};+const replaceList = (view: InboxView | null) => { confirmed = view; publishList(); };+/** Tests and previews show a list of their own. */+export const setInboxView = replaceList;/** Reads the list now, or once more after the read on its way, so the newest answer always lands last. */export function refreshInbox(): Promise<void> {if (reading) { readAgain = true; return reading; }@@ -67,26 +71,29 @@ export function refreshInbox(): Promise<void> {/** Keeps the list in step with the person's inbox task; nothing off a team. */export function useInboxLoader(version: string | null): void {useEffect(() => {- if (version === null) { list.set(null); return; }+ if (version === null) { replaceList(null); return; }void refreshInbox();}, [version]);}const patchRow = (id: string, patch: Partial<InboxRow>) => {- const view = list.get();- if (view) list.set({ ...view, rows: view.rows.map((row) => (row.id === id ? { ...row, ...patch } : row)) });+ if (confirmed) confirmed = { ...confirmed, rows: confirmed.rows.map((row) => (row.id === id ? { ...row, ...patch } : row)) };};async function mark(id: string, what: "read" | "done", patch: Partial<InboxRow>): Promise<void> {const request = ++revision;pending.set(request, { id, patch });- patchRow(id, patch);+ publishList();const view = await api<InboxView>(`/api/inbox/${id}/${what}`, { method: "POST", background: true }).catch(() => null);const current = request === revision;++revision;pending.delete(request);if (current && view && Array.isArray(view.rows)) replaceList(view);- else void refreshInbox();+ else {+ if (view && Array.isArray(view.rows)) patchRow(id, patch);+ publishList();+ void refreshInbox();+ }}/** The person read a line: it is no longer bold. */