Skip to content

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

fix(chat): retain recent queue receipts when history overlaps

Afloat687fb73d8f62
diff --git a/src/state/queue-receipt-order.test.ts b/src/state/queue-receipt-order.test.ts
new file mode 100644
index 0000000000..d0169126e0
--- /dev/null
+++ b/src/state/queue-receipt-order.test.ts
@@ -0,0 +1,28 @@
+import { expect, it } from "vitest";
+
+import { reconcileSnapshotQueues } from "./queue-receipts";
+import { initialState, reducer, type Message } from "./store";
+
+it("keeps live consumed receipts ahead of overlapping loaded history", () => {
+ const recent = reducer(reducer(initialState, { type: "consumePendingQueued", threadId: "live", queueId: "overlap" }),
+ { type: "consumePendingQueued", threadId: "live", queueId: "new-live" });
+ const messages: Message[] = Array.from({ length: 64 }, (_, at) => ({
+ id: `m-${at}`, at, role: "user", kind: "text", text: "Already ran", queueId: at === 0 ? "overlap" : `old-${at}`,
+ }));
+ const hydrated = reconcileSnapshotQueues(recent, [{ messages }]);
+ const late = reducer(hydrated, { type: "pendingQueued", threadId: "old-thread", queueId: "overlap", text: "Already ran" });
+ expect(late.pendingQueued).toEqual({});
+ expect(hydrated.consumedQueueIds["new-live"]).toBe(true);
+ expect(Object.keys(hydrated.consumedQueueIds)).toHaveLength(64);
+ expect(recent.consumedQueueIds).toEqual({ overlap: true, "new-live": true });
+});
+
+it("refreshes a repeated live receipt before evicting the oldest queue ID", () => {
+ let state = initialState;
+ for (let i = 0; i < 64; i++) state = reducer(state, { type: "consumePendingQueued", threadId: "t", queueId: `q-${i}` });
+ state = reducer(state, { type: "consumePendingQueued", threadId: "t", queueId: "q-0" });
+ state = reducer(state, { type: "consumePendingQueued", threadId: "t", queueId: "q-64" });
+ expect(reducer(state, { type: "pendingQueued", threadId: "t", queueId: "q-0", text: "Already ran" }).pendingQueued).toEqual({});
+ expect(state.consumedQueueIds["q-1"]).toBeUndefined();
+ expect(Object.keys(state.consumedQueueIds)).toHaveLength(64);
+});
diff --git a/src/state/queue-receipts.ts b/src/state/queue-receipts.ts
index 744b89e64a..6a37fb0e37 100644
--- a/src/state/queue-receipts.ts
+++ b/src/state/queue-receipts.ts
@@ -8,7 +8,8 @@ export function rememberConsumedQueueId(
consumed: AppState["consumedQueueIds"],
queueId: string,
): AppState["consumedQueueIds"] {
- const next = { ...consumed, [queueId]: true as const };
+ const { [queueId]: _older, ...previous } = consumed;
+ const next = { ...previous, [queueId]: true as const };
const overflow = Object.keys(next).length - MAX_CONSUMED_QUEUE_IDS;
if (overflow > 0) {
for (const id of Object.keys(next).slice(0, overflow)) delete next[id];