Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(post_to_thread): reserve the delivery id while a post is under way
AdityaUmalefc49d2650815
diff --git a/server/host/thread-posts.test.ts b/server/host/thread-posts.test.tsindex 93ac0c65dc..911b0ce5ce 100644--- a/server/host/thread-posts.test.ts+++ b/server/host/thread-posts.test.ts@@ -77,6 +77,28 @@ describe("thread posts", () => {expect(calls).toHaveLength(1);});+ it("posts concurrent calls with one delivery id once, and frees the id when delivery fails", async () => {+ const { calls, deps: d } = deps("sender-race");+ let release!: () => void;+ const ok = { status: 200, body: { receipts: [{ outcome: "delivered", requestId: "r1" }] } };+ d.coordinateBots = async (call) => { calls.push(call); await new Promise<void>((done) => { release = done; }); return ok; };+ const first = postToThread({ toThreadId: "t-1", message: "hi", deliveryId: "race" }, d);+ const same = await postToThread({ toThreadId: "t-1", message: "hi", deliveryId: "race" }, d);+ const other = await postToThread({ toThreadId: "t-2", message: "hi", deliveryId: "race" }, d);+ release();+ expect((await first).status).toBe(200);+ expect(same.status).toBe(409);+ expect(same.body.duplicate).toBeUndefined();+ expect(other.status).toBe(409);+ expect(calls).toHaveLength(1);++ d.coordinateBots = async (call) => { calls.push(call); throw new Error("down"); };+ await expect(postToThread({ toThreadId: "t-1", message: "hi", deliveryId: "fails" }, d)).rejects.toThrow("down");+ d.coordinateBots = async (call) => { calls.push(call); return ok; };+ expect((await postToThread({ toThreadId: "t-1", message: "hi", deliveryId: "fails" }, d)).status).toBe(200);+ expect(calls).toHaveLength(3);+ });+it("refuses a call made from a room", async () => {const { calls, deps: d } = deps("sender-room", ["from-run"]);expect((await postToThread({ toThreadId: "t-1", message: "hi" }, d)).status).toBe(409);diff --git a/server/host/thread-posts.ts b/server/host/thread-posts.tsindex bc955f182c..a07c260cc5 100644--- a/server/host/thread-posts.ts+++ b/server/host/thread-posts.ts@@ -75,6 +75,8 @@ const PostBody = z.object({});/** Thread each delivery went to, by sender and delivery id, so a retry is not posted twice (a no-wake post is also found in its thread). */const deliveries = new Map<string, string>();+/** Same key while its delivery is still under way, so a concurrent retry neither posts twice nor reuses the id elsewhere. */+const pending = new Map<string, string>();/** POST /api/internal/post-to-thread: checks the call and sends it through coordinate_bots as a post, so its reach rules hold. */export async function postToThread(body: unknown, deps: { senderId: string; fromThreadId: string; waitsOn: (threadId: string) => boolean;@@ -90,12 +92,19 @@ export async function postToThread(body: unknown, deps: { senderId: string; fromif (!owner || deps.store.groupByThread(threadId)) return refuse(404, "No open thread with that id: use a thread id list_threads shows. Rooms take post_to_room.");if (threadId === deps.fromThreadId) return refuse(409, "That is this conversation: say it in your reply instead.");if (deps.waitsOn(threadId)) return refuse(403, "That thread is waiting on this one: end your turn and your reply reaches it.");- const key = `${deps.senderId}\u0000${deliveryId}`, sentTo = deliveries.get(key);- if (sentTo !== undefined && sentTo !== threadId) return refuse(409, "That delivery_id was already used for another thread: use a new delivery_id.");+ const key = `${deps.senderId}\u0000${deliveryId}`, sentTo = deliveries.get(key), sending = pending.get(key), usedFor = sentTo ?? sending;+ if (usedFor !== undefined && usedFor !== threadId) return refuse(409, "That delivery_id was already used for another thread: use a new delivery_id.");+ if (sending !== undefined) return refuse(409, "A post with this delivery_id is still being delivered: try again shortly with the same delivery_id.");if (sentTo !== undefined || findThreadPost(deps.store.messagesFor(threadId), deps.senderId, deliveryId)) {return { status: 200, body: { deliveryId, threadId, botName: owner.name, duplicate: true, message: "Already delivered with this delivery_id; not posted again." } };}- const result = await deps.coordinateBots({ botIds: [owner.id], message, rework: false, thread: threadId, reply: "none", consult: false, post: { deliveryId, wake } });+ pending.set(key, threadId);+ let result: Reply;+ try {+ result = await deps.coordinateBots({ botIds: [owner.id], message, rework: false, thread: threadId, reply: "none", consult: false, post: { deliveryId, wake } });+ } finally {+ pending.delete(key);+ }const receipt = (result.body.receipts as PeerDeliveryReceipt[] | undefined)?.[0];if (result.status !== 200 || !receipt || receipt.outcome === "failed") {return refuse(result.status === 200 ? 409 : result.status, String(receipt?.detail ?? result.body.error ?? "Not delivered."));