Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Send a follow-up read only for an unread message newer than the open read (#5520)
Milind Soni7a72b8bce5ee
diff --git a/src/state/read-in-flight.test.ts b/src/state/read-in-flight.test.tsindex e30e9b8aee..a31f329d0c 100644--- a/src/state/read-in-flight.test.ts+++ b/src/state/read-in-flight.test.ts@@ -45,13 +45,15 @@ let root: Root;let cursor = 0;const settle = async () => { for (let i = 0; i < 5; i++) await new Promise((resolve) => setTimeout(resolve, 0)); };const stream = () => FixtureEventSource.opened.at(-1)!;-const unreadFrame = (): ServerFrame => ({+// The server stamps a task with its newest message's time; `newer: false` re-announces the newest one already sent.+let newestAt = 1_000;+const unreadFrame = (newer: boolean): ServerFrame => ({kind: "bot",- bot: { ...bot, unread: true, tasks: [{ ...bot.tasks![0]!, unread: true }] },+ bot: { ...bot, unread: true, tasks: [{ ...bot.tasks![0]!, unread: true, updatedAt: newer ? ++newestAt : newestAt }] },} as ServerFrame);// Each frame is applied before the next arrives, as when they come in separate paints.-const sendUnread = () => {- stream().send(unreadFrame(), `run:${++cursor}`);+const sendUnread = (newer = true) => {+ stream().send(unreadFrame(newer), `run:${++cursor}`);flushSync(() => dispatch({ type: "connected", value: true }));};@@ -126,4 +128,29 @@ describe("marking the shown chat read while a read is in flight", () => {await settle();expect(reads).toHaveLength(start + 1);});++ it("sends no follow-up for unread frames that carry no newer message than the open read covers", async () => {+ const start = reads.length;+ sendUnread();+ sendUnread(false);+ sendUnread(false);+ await settle();+ expect(reads).toHaveLength(start + 1);+ reads[start]!.answer();+ await settle();+ expect(reads, "the open read already covered that message").toHaveLength(start + 1);+ });++ it("covers the newest message seen with the follow-up, so a re-announcement of it during the follow-up adds nothing", async () => {+ const start = reads.length;+ sendUnread();+ sendUnread();+ reads[start]!.answer();+ await settle();+ expect(reads).toHaveLength(start + 2);+ sendUnread(false);+ reads[start + 1]!.answer();+ await settle();+ expect(reads).toHaveLength(start + 2);+ });});diff --git a/src/state/store.tsx b/src/state/store.tsxindex c542ec6242..a66fbbb808 100644--- a/src/state/store.tsx+++ b/src/state/store.tsx@@ -3075,13 +3075,14 @@ export function StoreProvider({ children }: { children: ReactNode }) {};const hydrationFallback = setTimeout(hydrate, 1_000);- // One read per conversation at a time; an unread frame meanwhile asks for exactly one more once it finishes.- const readsInFlight = new Map<string, boolean>();- const sendRead = (botId: string, threadId: string | undefined, key: string): void => {- readsInFlight.set(key, false);+ // One read per conversation at a time, covering its newest message (the task's updatedAt). An unread frame meanwhile+ // with a newer message asks for exactly one more once it finishes; one re-announcing what the read covers does not.+ const readsInFlight = new Map<string, { covers: number; again: boolean }>();+ const sendRead = (botId: string, threadId: string | undefined, key: string, covers: number): void => {+ readsInFlight.set(key, { covers, again: false });void fetch(`/api/bots/${botId}/read`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ threadId }) }).catch(() => {}).finally(() => {- const again = readsInFlight.get(key); readsInFlight.delete(key);- if (again && alive) sendRead(botId, threadId, key);+ const open = readsInFlight.get(key); readsInFlight.delete(key);+ if (open?.again && alive) sendRead(botId, threadId, key, open.covers);});};const stopFrames = store.handleFrames((frame) => { handleFrame(frame); stateRef.current = store.getState(); });@@ -3136,9 +3137,9 @@ export function StoreProvider({ children }: { children: ReactNode }) {(selectedTask?.unread || (!bot.tasks && bot.unread))) {if (selectedTask) selectedTask.unread = false;bot.unread = botShowsUnread(bot);- const read = `${bot.id}:${shown}`;- if (readsInFlight.has(read)) readsInFlight.set(read, true);- else sendRead(bot.id, shown, read);+ const read = `${bot.id}:${shown}`, newest = selectedTask?.updatedAt ?? 0, open = readsInFlight.get(read);+ if (!open) sendRead(bot.id, shown, read, newest);+ else if (newest > open.covers) readsInFlight.set(read, { covers: newest, again: true });}rawDispatch({type: "botPatched",