Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(steer-queue): preserve first insertion order across restart
erywimc639a6fa2098
diff --git a/server/steer-queue.test.ts b/server/steer-queue.test.tsindex b17433db08..6e148a408c 100644--- a/server/steer-queue.test.ts+++ b/server/steer-queue.test.ts@@ -295,6 +295,52 @@ describe("steer-queue module", () => {expect(run.mock.calls.map((call) => [call[2], call[6].sender?.name])).toEqual([["and the PR", "Priya"], ["what is due", "Tom"]]);});+ it("restores a first insertion ahead of an unattended report queued before it", () => {+ const bot = fakeBot("bot-first-restore", "thread-first-restore", true);+ const store = fakeStore([bot]);+ const run = vi.fn();+ const report = { botId: "bot-reporter", name: "Reporter", incident: { threadId: "thread-reporter" } };+ queueSteeredMessage(bot.id, bot.threadId, "the report", { unattended: true, peerAsk: report });+ queueSteeredMessage(bot.id, bot.threadId, "my own request", { first: true });+ restoreSteeredMessages();+ bot.busy = false;+ drainSteeredMessages(store, run);+ expect(run.mock.calls[0]![2]).toBe("my own request");+ drainSteeredMessages(store, run);+ expect(run.mock.calls[1]![2]).toBe("the report");+ });++ it("restores the asker's own addition ahead of another person's line", () => {+ const bot = fakeBot("bot-ahead-restore", "thread-ahead-restore", true);+ const store = fakeStore([bot]);+ const run = vi.fn();+ const tom = { name: "Tom", id: "p_tom" }, priya = { name: "Priya", id: "p_priya" };+ queueSteeredMessage(bot.id, bot.threadId, "what is due", { sender: tom, first: true });+ queueSteeredMessage(bot.id, bot.threadId, "and the PR", { sender: priya, first: true, aheadOfOthers: "p_priya" });+ restoreSteeredMessages();+ bot.busy = false;+ drainSteeredMessages(store, run);+ drainSteeredMessages(store, run);+ expect(run.mock.calls.map((call) => call[2])).toEqual(["and the PR", "what is due"]);+ });++ it("restores rows written before place hints existed in their rowid order, and still replays a newer first", () => {+ const bot = fakeBot("bot-legacy-order", "thread-legacy-order", true);+ const store = fakeStore([bot]);+ const run = vi.fn();+ saveChatFollowup({ id: "legacy-order-report", kind: "bot", ownerId: bot.id, threadId: bot.threadId,+ payload: { text: "older report", prompt: "older report", peerAsk: { botId: "bot-reporter", name: "Reporter" } } });+ saveChatFollowup({ id: "legacy-order-note", kind: "bot", ownerId: bot.id, threadId: bot.threadId,+ payload: { text: "older note", prompt: "older note" } });+ queueSteeredMessage(bot.id, bot.threadId, "my own request", { first: true });+ restoreSteeredMessages();+ bot.busy = false;+ drainSteeredMessages(store, run);+ drainSteeredMessages(store, run);+ drainSteeredMessages(store, run);+ expect(run.mock.calls.map((call) => call[2])).toEqual(["my own request", "older report", "older note"]);+ });+it("drains one call's spoken lines as one turn, and the next call's as its own", () => {const bot = fakeBot("bot-call-groups", "thread-call-groups", true);const store = fakeStore([bot]);diff --git a/server/steer-queue.ts b/server/steer-queue.tsindex eb5a1aa35b..d32ccf5533 100644--- a/server/steer-queue.ts+++ b/server/steer-queue.ts@@ -1,7 +1,7 @@// Mid-turn sends wait here off the transcript (else they become the active leaf mid-turn) and survive Stop: stop-then-steer is the feature.import { newId } from "./contracts.ts";-import { chatFollowups, saveChatFollowup, settleChatFollowups } from "./message-db.ts";+import { chatFollowups, saveChatFollowup, settleChatFollowups, type FollowupPayload } from "./message-db.ts";import { drainCoalesceHead } from "./admission.ts";import { spokenLineFields } from "./live-call-record.ts";import { workOriginSchema, type WorkOrigin } from "./peer-provenance.ts";@@ -58,22 +58,35 @@ export interface HeldSteerQueue {items: QueueEntry["items"];}+/** Placement hints stored beside a follow-up payload; rows from older builds do not carry them. */+type PlacedPayload = FollowupPayload & { first?: boolean; aheadOfOthers?: string };++/** `first`: ahead of everything queued but the person's own messages sent before it; with `aheadOfOthers` (the person+ * whose request runs, where several people write), ahead of everyone else's too. Live enqueue and restore share this, so+ * the two orders cannot drift. */+function insertQueuedItem(entry: QueueEntry, item: QueueEntry["items"][number], first?: boolean, aheadOfOthers?: string): void {+ const ahead = first ? entry.items.findIndex((other) => other.peerAsk !== undefined || other.unattended === true || other.queuedAt > item.queuedAt+ || (aheadOfOthers !== undefined && other.sender?.id !== aheadOfOthers)) : -1;+ if (ahead < 0) entry.items.push(item);+ else entry.items.splice(ahead, 0, item);+}+export function restoreSteeredMessages(): void {queues.clear();for (const row of chatFollowups("bot")) {if (row.status !== "pending") continue;const entry = queues.get(row.threadId) ?? { botId: row.ownerId, items: [] };if (entry.botId !== row.ownerId) throw new Error("queued task belongs to another bot");- const { via, callId, origin: storedOrigin, ...payload } = row.payload;+ const { via, callId, origin: storedOrigin, first, aheadOfOthers, ...payload } = row.payload as PlacedPayload;const origin = workOriginSchema.safeParse(storedOrigin);- entry.items.push({+ insertQueuedItem(entry, {...payload,...spokenLineFields(via, callId),...(origin.success ? { origin: origin.data } : {}),messageId: row.id,prompt: row.payload.prompt ?? row.payload.text,queuedAt: row.payload.queuedAt ?? Date.now(),- });+ }, first, aheadOfOthers);queues.set(row.threadId, entry);}}@@ -137,13 +150,13 @@ export function queueSteeredMessage(...(options.rowId ? { rowId: options.rowId } : {}),...(options.mentions?.length ? { mentions: options.mentions } : {}),};- saveChatFollowup({ id, kind: "bot", ownerId: botId, threadId, payload: item });- // `first`: ahead of everything queued but the person's own messages sent before it; with `aheadOfOthers` (the person- // whose request runs, where several people write), ahead of everyone else's too.- const ahead = options.first ? entry.items.findIndex((other) => other.peerAsk !== undefined || other.unattended === true || other.queuedAt > item.queuedAt- || (options.aheadOfOthers !== undefined && other.sender?.id !== options.aheadOfOthers)) : -1;- if (ahead < 0) entry.items.push(item);- else entry.items.splice(ahead, 0, item);+ const placed: PlacedPayload = {+ ...item,+ ...(options.first ? { first: true } : {}),+ ...(options.aheadOfOthers !== undefined ? { aheadOfOthers: options.aheadOfOthers } : {}),+ };+ saveChatFollowup({ id, kind: "bot", ownerId: botId, threadId, payload: placed });+ insertQueuedItem(entry, item, options.first, options.aheadOfOthers);queues.set(threadId, entry);changed();return { id };