Skip to content

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

fix(reactions): retire superseded answers after delayed queueing

Afloat6f18a375297d
diff --git a/server/reaction-delivery-lifetime.test.ts b/server/reaction-delivery-lifetime.test.ts
new file mode 100644
index 0000000000..9548b9383f
--- /dev/null
+++ b/server/reaction-delivery-lifetime.test.ts
@@ -0,0 +1,82 @@
+import { rmSync } from "node:fs";
+import { beforeEach, expect, it } from "vitest";
+import { DATA_DIR } from "./config.ts";
+import { cancelledChatFollowup } from "./message-db.ts";
+import { createPersonReplies } from "./person-replies.ts";
+import { createReactions } from "./reactions.ts";
+import type { RequestAuth } from "./request-auth.ts";
+import { SendSequencer } from "./send-idempotency.ts";
+import { queueSteeredMessage, queuedSteeredMessage, restoreSteeredMessages } from "./steer-queue.ts";
+import { Store } from "./store.ts";
+
+const OWNER: RequestAuth = { kind: "loopback", scopes: ["admin", "client"] };
+const deferred = () => { let resolve!: () => void; const promise = new Promise<void>((done) => { resolve = done; }); return { promise, resolve }; };
+beforeEach(() => { rmSync(DATA_DIR, { recursive: true, force: true }); restoreSteeredMessages(); });
+
+function harness(queued = true) {
+ const store = new Store(() => ({ instanceId: "fixture", model: "fixture" }));
+ const bot = store.createBot({ name: "Scout" });
+ const asked = store.appendMessage(bot.threadId, { role: "bot", kind: "text", text: "Shall I send the email?" });
+ const entered = deferred(), release = deferred(), sends: string[] = [];
+ const replies = createPersonReplies({
+ store, sendSequencer: new SendSequencer(),
+ assertWithinBudget() {}, messageSender: () => undefined, usageTriggerFor: () => ({ kind: "user" }),
+ groupThreadIsWorking: () => false, startGroupTurn: () => {},
+ async startOrQueueDirectMessage(botId, threadId, text, target, sendId) {
+ sends.push(sendId);
+ if (sends.length === 1) { entered.resolve(); await release.promise; }
+ if (!queued) { store.appendMessage(threadId, { role: "user", kind: "text", text, sendId }); return {}; }
+ queueSteeredMessage(botId, threadId, text, { sendId, replyToId: target.id, reaction: target.reactedWith });
+ return { queued: true };
+ },
+ });
+ const reactions = createReactions({ store, reply: (input) => replies.deliver({ ...input, about: "message" }), answerCard: async () => ({ ok: true }), withdraw: replies.withdraw });
+ const tap = (emoji: "πŸ‘" | "πŸ‘Ž", by = "user") => reactions.react({ auth: OWNER, threadId: bot.threadId, messageId: asked.id, emoji, reactor: { by } });
+ return { bot, store, tap, entered, release, sends };
+}
+
+it("withdraws a queued answer that finishes after its reaction was cleared", async () => {
+ const h = harness();
+ const first = h.tap("πŸ‘");
+ await h.entered.promise;
+ expect((await h.tap("πŸ‘")).delivery).toBe("none");
+ h.release.resolve();
+ const result = await first;
+ expect(queuedSteeredMessage(h.bot.id, h.bot.threadId, h.sends[0]!)).toBeNull();
+ expect(result.delivery).toBe("none");
+ expect(cancelledChatFollowup("bot", h.bot.id, h.bot.threadId, h.sends[0]!)).toBe(true);
+ restoreSteeredMessages();
+ expect(queuedSteeredMessage(h.bot.id, h.bot.threadId, h.sends[0]!)).toBeNull();
+});
+
+it("keeps a newer opposite answer when the older queued answer finishes last", async () => {
+ const h = harness();
+ const first = h.tap("πŸ‘");
+ await h.entered.promise;
+ expect((await h.tap("πŸ‘Ž")).delivery).toBe("queued");
+ h.release.resolve();
+ const result = await first;
+ expect(queuedSteeredMessage(h.bot.id, h.bot.threadId, h.sends[0]!)).toBeNull();
+ expect(result.delivery).toBe("none");
+ expect(queuedSteeredMessage(h.bot.id, h.bot.threadId, h.sends[1]!)?.text).toBe("πŸ‘Ž");
+});
+
+it("does not let another person's reaction retire this person's answer", async () => {
+ const h = harness();
+ const first = h.tap("πŸ‘", "alice");
+ await h.entered.promise;
+ expect((await h.tap("πŸ‘Ž", "bob")).delivery).toBe("queued");
+ h.release.resolve();
+ expect((await first).delivery).toBe("queued");
+ expect(h.sends.map((id) => queuedSteeredMessage(h.bot.id, h.bot.threadId, id)?.text)).toEqual(["πŸ‘", "πŸ‘Ž"]);
+});
+
+it("still reports an answer that already started as started", async () => {
+ const h = harness(false);
+ const first = h.tap("πŸ‘");
+ await h.entered.promise;
+ await h.tap("πŸ‘");
+ h.release.resolve();
+ expect((await first).delivery).toBe("started");
+ expect(h.store.messagesFor(h.bot.threadId).at(-1)?.sendId).toBe(h.sends[0]);
+});
diff --git a/server/reactions.ts b/server/reactions.ts
index 33f75213eb..6747264011 100644
--- a/server/reactions.ts
+++ b/server/reactions.ts
@@ -136,6 +136,14 @@ export function createReactions(deps: ReactionDeps) {
try {
const delivery = await deliver(auth, threadId, message, owner, entry, sent);
withdrawPrevious();
+ if (delivery === "queued") {
+ const current = latest().reactions?.find((reaction) => reaction.by === reactor.by);
+ // Clearing or replacing the reaction may have reached the queue before this answer did.
+ if (!current || current.at !== entry.at || current.emoji !== entry.emoji) {
+ deps.withdraw(threadId, owner, sent.sendId(entry));
+ return { message: latest(), delivery: "none" };
+ }
+ }
return { message: latest(), delivery };
} catch (error) {
const current = latest().reactions?.find((reaction) => reaction.by === reactor.by);