Skip to content

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

fix(rooms): keep queued reply targets in separate batches

Afloat45fdc09f9d6d
diff --git a/server/channel-queue.test.ts b/server/channel-queue.test.ts
index 2aba37face..9ecd3fc9c7 100644
--- a/server/channel-queue.test.ts
+++ b/server/channel-queue.test.ts
@@ -4,6 +4,7 @@ import {
_queuedChannelCount,
cancelChannelMessage,
drainChannelMessages,
+ headChannelGroup,
holdChannelQueue,
queuedChannelMessage,
queueChannelMessage,
@@ -11,9 +12,50 @@ import {
restoreHeldChannelQueue,
resolveHeldReplyTarget,
settleHeldChannelQueueHead,
+ type ChannelQueueItem,
} from "./channel-queue.ts";
import { saveChatFollowup } from "./message-db.ts";
+describe("channel queue batches", () => {
+ const item = (id: string, options: Partial<ChannelQueueItem> = {}): ChannelQueueItem => ({
+ id, text: id, mode: "chat", queuedAt: 1_000, ...options,
+ });
+
+ it.each([
+ ["quoted then unquoted", "reply-a", undefined, 1],
+ ["unquoted then quoted", undefined, "reply-a", 1],
+ ["different replies", "reply-a", "reply-b", 1],
+ ["the same reply", "reply-a", "reply-a", 2],
+ ["both unquoted", undefined, undefined, 2],
+ ] as const)("batches %s messages by their reply target", (_name, firstReply, secondReply, count) => {
+ const first = item("first", { replyToId: firstReply });
+ const second = item("second", { replyToId: secondReply, queuedAt: 1_001 });
+ expect(headChannelGroup([first, second])).toEqual([first, second].slice(0, count));
+ });
+
+ it.each([
+ ["API", { via: "api" }],
+ ["goal", { mode: "goal" }],
+ ["pointed", { pointed: { refs: [], blocks: { atlas: "picked page" } } }],
+ ] satisfies Array<[string, Partial<ChannelQueueItem>]>)("keeps %s sends separate even with the same reply target", (_name, options) => {
+ const first = item("first", { replyToId: "reply-a", ...options });
+ const second = item("second", { replyToId: "reply-a", ...options });
+ expect(headChannelGroup([first, second])).toEqual([first]);
+ });
+
+ it("keeps different people separate even with the same reply target", () => {
+ const first = item("first", { replyToId: "reply-a", sender: { id: "priya", name: "Priya" } });
+ const second = item("second", { replyToId: "reply-a", sender: { id: "alex", name: "Alex" } });
+ expect(headChannelGroup([first, second])).toEqual([first]);
+ });
+
+ it("still coalesces one person's replies after their display name changes", () => {
+ const first = item("first", { replyToId: "reply-a", sender: { id: "priya", name: "Priya" } });
+ const second = item("second", { replyToId: "reply-a", sender: { id: "priya", name: "Priya Shah" } });
+ expect(headChannelGroup([first, second])).toEqual([first, second]);
+ });
+});
+
describe("channel queue", () => {
it("keeps readable citation prompts byte-for-byte through room queue and steer hold", () => {
const prompt = '<!--omb-citation-v1:fixture-->\n> Quoted message:\n> multiline 🐭\n> code\n\nComment:\nCheck it';
@@ -209,6 +251,20 @@ describe("channel queue", () => {
expect(_queuedChannelCount("thread-e")).toBe(0);
});
+ it("keeps a different reply target queued after steering the head", () => {
+ const head = queueChannelMessage("group-replies", "thread-replies", "first reply", { replyToId: "reply-a" });
+ const tail = queueChannelMessage("group-replies", "thread-replies", "second reply", { replyToId: "reply-b" });
+ const held = holdChannelQueue("group-replies", "thread-replies", head.id)!;
+
+ settleHeldChannelQueueHead(held);
+ const run = vi.fn();
+ drainChannelMessages(() => false, run);
+ expect(run).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({
+ items: [expect.objectContaining({ id: tail.id, replyToId: "reply-b" })],
+ }));
+ expect(_queuedChannelCount("thread-replies")).toBe(0);
+ });
+
it("drains an idle thread queue while another thread in the same group is working", () => {
const item1 = queueChannelMessage("group-multi", "thread-busy", "waits for busy thread to settle");
const item2 = queueChannelMessage("group-multi", "thread-idle", "runs immediately in idle thread");
diff --git a/server/channel-queue.ts b/server/channel-queue.ts
index d8319247ba..a9d50d1d4a 100644
--- a/server/channel-queue.ts
+++ b/server/channel-queue.ts
@@ -209,13 +209,13 @@ export function headChannelGroup(items: readonly ChannelQueueItem[]): ChannelQue
return drainCoalesceHead(items, coalesceIdentity, (item) => item.queuedAt, DRAIN_COALESCE_MAX_ITEMS);
}
-/** An API send has no person behind it and never merges, nor does a goal; unattributed local sends are one identity. */
+/** API, goal and pointed sends never merge; a chat burst shares one sender and reply target. */
function coalesceIdentity(item: ChannelQueueItem): string {
if (item.via === "api") return `api:${item.id}`;
if (item.mode !== "chat") return `mode:${item.mode}:${item.id}`;
if (item.pointed) return `pointed:${item.id}`;
- if (item.sender) return `person:${item.sender.id ?? item.sender.name}`;
- return "person:local";
+ const sender = item.sender ? `person:${item.sender.id ?? item.sender.name}` : "person:local";
+ return JSON.stringify([sender, item.replyToId ?? null]);
}
export function queuedChannelSends(threadId: string): number {
diff --git a/server/room-quoted-replies.e2e.test.ts b/server/room-quoted-replies.e2e.test.ts
index 39d7bb5fcb..b38949a9c5 100644
--- a/server/room-quoted-replies.e2e.test.ts
+++ b/server/room-quoted-replies.e2e.test.ts
@@ -11,6 +11,7 @@ const bots: Bot[] = [];
let routingCalls = 0;
let routingOptions: string[] = [];
let keyChecks = 0;
+let replyGate: { started: boolean; release: Promise<void> } | undefined;
const api = async (method: string, path: string, body?: unknown) => {
const response = await fetch(fixture.info.url + path, {
@@ -43,6 +44,13 @@ beforeAll(async () => {
return json({ answers: { answer: { type: "choice", choice, confidence: 0.99, probabilities: { [choice]: 0.99 } } } });
}
res.writeHead(200, { "content-type": "text/event-stream" });
+ const gate = replyGate;
+ replyGate = undefined;
+ if (gate) {
+ res.flushHeaders();
+ gate.started = true;
+ await gate.release;
+ }
res.end(`data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "I received your message." }, finish_reason: "stop" }] })}\n\ndata: [DONE]\n\n`);
});
await new Promise<void>((resolve) => upstream.listen(0, "127.0.0.1", resolve));
@@ -106,6 +114,40 @@ it("lets an explicit mention override the quoted bot", async () => {
expect(routingCalls).toBe(before);
}, 120_000);
+it.each([
+ ["quoted then unquoted", "atlas", undefined, [0, 1]],
+ ["unquoted then quoted", undefined, "atlas", [1, 0]],
+ ["different quoted bots", "atlas", "boreal", [0, 1]],
+ ["the same quoted bot", "atlas", "atlas", [0]],
+ ["both unquoted", undefined, undefined, [1]],
+] as const)("routes queued %s messages without changing their reply targets", async (_name, firstTarget, secondTarget, responders) => {
+ const { room, quote } = await quotedRoom();
+ const secondQuote = (await round(room, "@Boreal please post your status.")).replies[0]!;
+ expect(secondQuote.from?.botId).toBe(bots[1]!.id);
+ const quoteIds = { atlas: quote.id, boreal: secondQuote.id };
+ const before = new Set((await messages(room)).map((message) => message.id));
+ let release!: () => void;
+ const gate = { started: false, release: new Promise<void>((resolve) => { release = resolve; }) };
+ replyGate = gate;
+ try {
+ await api("POST", `/api/groups/${room.id}/messages`, { text: "@Cyra keep this room busy.", threadId: room.threadId });
+ await expect.poll(() => gate.started, { timeout: 30_000 }).toBe(true);
+ for (const [index, target] of [firstTarget, secondTarget].entries()) {
+ const sent = await api("POST", `/api/groups/${room.id}/messages`, {
+ text: `Please review detail ${index + 1}.`, threadId: room.threadId,
+ ...(target ? { replyToId: quoteIds[target] } : {}),
+ });
+ expect(sent.queued).toBe(true);
+ }
+ } finally {
+ replyGate = undefined;
+ release();
+ }
+ expect((await control(["wait", "--channel", room.id, "--timeout", "30"])).status).toBe("settled");
+ const replies = (await messages(room)).filter((message) => !before.has(message.id) && message.kind === "text" && message.role === "bot");
+ expect(replies.map((message) => message.from?.botId)).toEqual([bots[2]!.id, ...responders.map((index) => bots[index]!.id)]);
+}, 120_000);
+
it("still asks the decision model for a reply to a person's message", async () => {
const { room, person } = await quotedRoom();
const before = routingCalls;