Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(cloud): a refused send keeps no typing hint; queued-conflict test queues
Bugfixer 24e2409d0b4e9
diff --git a/server/routes/bot-messages.test.ts b/server/routes/bot-messages.test.tsindex d3af38e638..07c6f2e563 100644--- a/server/routes/bot-messages.test.ts+++ b/server/routes/bot-messages.test.ts@@ -78,6 +78,7 @@ async function fixture(over: Partial<Record<keyof BotMessageRouteDeps, unknown>>if (match.kind === "conflict") throw Object.assign(new Error("sendId already belongs to another message"), { status: 409 });if (match.kind === "match") return { ok: true as const, threadId: input.threadId, message: match.message };const waiting = queuedSteeredMessage(input.botId, input.threadId, input.sendId);+ if (waiting && (waiting.text !== input.text || waiting.replyToId !== input.replyTo?.id)) throw Object.assign(new Error("sendId already belongs to another message"), { status: 409 });if (waiting) return { ok: true as const, queued: true as const, queueId: waiting.id, threadId: input.threadId };}if (state.busy) {@@ -353,13 +354,26 @@ describe("POST /api/bots/:id/messages and who typed it", () => {});it("keeps the first sender's session when a conflicting send reuses a queued line's sendId", async () => {- const { threadId, post } = await fixture();- expect((await post({ threadId, sendId: SEND, text: words }, "", { "x-test-session": "pc-session" })).status).toBe(202);+ const { threadId, post, state } = await fixture();+ state.busy = true;+ const first = await post({ threadId, sendId: SEND, text: words }, "", { "x-test-session": "pc-session" });+ expect(first.status).toBe(202);+ expect(first.body).toMatchObject({ queued: true });const conflict = await post({ threadId, sendId: SEND, text: "Something else" }, "", { "x-test-session": "laptop-session" });expect(conflict.status, JSON.stringify(conflict.body)).toBe(409);expect(sendingSession(threadId, SEND)).toBe("pc-session");});+ it("notes no session for a refused send, so an accepted retry from another session names its own", async () => {+ const { threadId, post, state } = await fixture();+ state.refuse = new DirectSendRefused(409, { error: "This workspace is at its spend limit.", code: "spend_cap" });+ expect((await post({ threadId, sendId: SEND, text: words }, "", { "x-test-session": "pc-session" })).status).toBe(409);+ expect(sendingSession(threadId, SEND)).toBeUndefined();+ state.refuse = null;+ expect((await post({ threadId, sendId: SEND, text: words }, "", { "x-test-session": "laptop-session" })).status).toBe(202);+ expect(sendingSession(threadId, SEND)).toBe("laptop-session");+ });+it("does not take a bare loopback send as a person's on a headless server, where it may be a bot's shell", async () => {const { threadId, post } = await fixture({}, false);const plain = await post({ threadId, sendId: SEND, text: words });diff --git a/server/routes/bot-messages.ts b/server/routes/bot-messages.tsindex 473ad55c18..65d54c2145 100644--- a/server/routes/bot-messages.ts+++ b/server/routes/bot-messages.ts@@ -35,7 +35,7 @@ import type { BotRecord, GroupRecord, Message, Store } from "../store.ts";import { extractSteerImages } from "../turn-images.ts";import { followupFacts, helperFollowup, helperFollowupBody, mayFollowUp } from "../helper-followup.ts";import type { UsageTrigger } from "../usage-ledger.ts";-import { noteSendingSession } from "../sending-sessions.ts";+import { forgetSendingSession, noteSendingSession } from "../sending-sessions.ts";import { noteTypedSend } from "../typed-sends.ts";import { engineSessionOutdated, startFresh } from "../fresh-start.ts";import { parseMentionPicks } from "../../shared/room-mentions.ts";@@ -342,7 +342,7 @@ export function createBotMessageRoutes(deps: BotMessageRouteDeps): RouteHandler// paired session or the desktop capability proves a person and lifts the webhook block.const personPresent = auth.kind === "session" || DESKTOP_MANAGED;if (personPresent && sendId && !guarded && !press && !appSent) noteTypedSend(threadId, sendId, text);- if (auth.kind === "session" && sendId) noteSendingSession(threadId, sendId, auth.session.id);+ const notedSession = auth.kind === "session" && sendId ? noteSendingSession(threadId, sendId, auth.session.id) : false;if (pressKey) pressesInFlight.set(pressKey, sendId);try {const receipt = await acceptDirectSend({@@ -356,6 +356,7 @@ export function createBotMessageRoutes(deps: BotMessageRouteDeps): RouteHandlerreturn json(res, 202, receipt);} catch (error) {slot?.release();+ if (notedSession) forgetSendingSession(threadId, sendId!);if (error instanceof DirectSendRefused) return json(res, error.status, error.body);throw error;} finally {diff --git a/server/sending-sessions.ts b/server/sending-sessions.tsindex 32394a1f44..14eecbb522 100644--- a/server/sending-sessions.ts+++ b/server/sending-sessions.ts@@ -7,12 +7,18 @@ const sessions = new Map<string, string>();const key = (threadId: string, sendId: string): string => `${threadId}\n${sendId}`;/** The first send of a line names its session: a later send reusing that sendId is a conflict the route refuses, and- * must not take over the hint of the line already queued. */-export function noteSendingSession(threadId: string, sendId: string, session: string): void {+ * must not take over the hint of the line already queued. True when this call noted it. */+export function noteSendingSession(threadId: string, sendId: string, session: string): boolean {const at = key(threadId, sendId);- if (sessions.has(at)) return;+ if (sessions.has(at)) return false;sessions.set(at, session);if (sessions.size > LIMIT) sessions.delete(sessions.keys().next().value!);+ return true;+}++/** Drops the session a refused send noted, so an accepted retry of that line names its own. */+export function forgetSendingSession(threadId: string, sendId: string): void {+ sessions.delete(key(threadId, sendId));}/** The session that sent this line, when the send route noted one. */