Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(inbox): an answer from before the inbox was cleared never brings it back

Sebastian B0fcdb992bc41
diff --git a/src/state/inbox.test.ts b/src/state/inbox.test.ts
index 0fc99946f9..ed08f49090 100644
--- a/src/state/inbox.test.ts
+++ b/src/state/inbox.test.ts
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, expect, it, vi } from "vitest";
import type { InboxView } from "../../shared/inbox";
import { inboxView, messageRow, reportRow } from "@/testing/inbox-fixture";
import { api } from "./api";
-import { currentInbox, doneInboxRow, readInboxRow, refreshInbox, replyToInboxRow, setInboxView } from "./inbox";
+import { currentInbox, doneInboxRow, forgetInbox, readInboxRow, refreshInbox, replyToInboxRow, setInboxView } from "./inbox";
vi.mock("./api", () => ({ api: vi.fn() }));
@@ -207,3 +207,33 @@ it("removes failed Done from the latest confirmed snapshot", async () => {
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.ts
index cf8eae3ff2..c59394054a 100644
--- a/src/state/inbox.ts
+++ b/src/state/inbox.ts
@@ -33,6 +33,7 @@ export const useInboxView = (): InboxView | null => useSyncExternalStore(list.su
let reading: Promise<void> | null = null;
let readAgain = false;
let revision = 0;
+let generation = 0;
let confirmed: InboxView | null = null;
const pending = new Map<number, { id: string; patch: Partial<InboxRow> }>();
const publishList = () => {
@@ -56,8 +57,10 @@ export function refreshInbox(): Promise<void> {
try {
do {
readAgain = false;
+ const era = generation;
const request = ++revision;
const view = await api<InboxView>("/api/inbox", { background: true }).catch(() => null);
+ if (era !== generation) continue;
if (request !== revision) { readAgain = true; continue; }
if (view && Array.isArray(view.rows)) replaceList(view);
} while (readAgain);
@@ -68,10 +71,17 @@ export function refreshInbox(): Promise<void> {
return reading;
}
+/** Off a team or signed out: the list goes, and no answer still on its way can bring it back. */
+export function forgetInbox(): void {
+ generation++;
+ pending.clear();
+ replaceList(null);
+}
+
/** 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) { replaceList(null); return; }
+ if (version === null) { forgetInbox(); return; }
void refreshInbox();
}, [version]);
}
@@ -81,6 +91,7 @@ const patchRow = (id: string, patch: Partial<InboxRow>) => {
};
async function mark(id: string, what: "read" | "done", patch: Partial<InboxRow>): Promise<void> {
+ const era = generation;
const request = ++revision;
pending.set(request, { id, patch });
publishList();
@@ -88,6 +99,7 @@ async function mark(id: string, what: "read" | "done", patch: Partial<InboxRow>)
const current = request === revision;
++revision;
pending.delete(request);
+ if (era !== generation) return;
if (current && view && Array.isArray(view.rows)) replaceList(view);
else {
if (view && Array.isArray(view.rows)) patchRow(id, patch);
@@ -109,12 +121,13 @@ export function doneInboxRow(row: Pick<InboxRow, "id">): void {
/** 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. */
export async function replyToInboxRow(view: Pick<InboxView, "botId" | "threadId">, row: Pick<InboxRow, "id">, text: string): Promise<void> {
+ const era = generation;
++revision;
try {
await api(`/api/bots/${view.botId}/messages`, { method: "POST", body: JSON.stringify({ text, threadId: view.threadId, replyToId: row.id, sendId: crypto.randomUUID() }) });
} finally {
++revision;
- void refreshInbox();
+ if (era === generation) void refreshInbox();
}
}