Skip to content

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

Bot API retry: a refused turn puts the thread's rewind mark back

milind-sonif7442d3a6333
diff --git a/server/bot-api-host.test.ts b/server/bot-api-host.test.ts
index 3141b638f4..dff8c2d0db 100644
--- a/server/bot-api-host.test.ts
+++ b/server/bot-api-host.test.ts
@@ -616,7 +616,7 @@ describe("Retry in the app on a line a caller sent", () => {
expect(h.turns.at(-1)!.prompt).not.toContain("on their behalf at your level");
});
- it("puts the conversation back on the branch the person was reading when the turn is refused", async () => {
+ it("puts the conversation back on the branch the person was reading when the turn is refused, and the status answers the refused line with no reply", async () => {
const h = await harness(replies("Done"));
const key = (await h.app("POST", "/api/bots/b1/bot-api", { enabled: true })).body.key as string;
const threadId = (await h.call("b1", key, { message: "Hi" })).body.thread as string;
@@ -627,5 +627,7 @@ describe("Retry in the app on a line a caller sent", () => {
hooksBaseUrl: () => "https://bots.example.test", manageRefusal: () => null });
await expect(refusing.retry("b1", threadId, sent)).rejects.toBe(refused);
expect(h.fake.store.activeLeaf(threadId)).toBe(leaf);
+ expect(h.fake.bots.get("b1")!.tasks[0]!).not.toHaveProperty("rewound", true);
+ expect(await h.call("b1", key, undefined, { method: "GET", query: `?thread=${threadId}` })).toEqual({ status: 200, body: { reply: "", thread: threadId } });
});
});
diff --git a/server/bot-api-host.ts b/server/bot-api-host.ts
index 437865b2fd..af869cb699 100644
--- a/server/bot-api-host.ts
+++ b/server/bot-api-host.ts
@@ -223,7 +223,7 @@ export function createBotApiHost(deps: BotApiHostDeps) {
// Never upgraded: a thread opened as the owner's work runs as theirs only while the key still acts as the owner.
const owned = task.botApiAsOwner === true && asOwner(botId);
const message = source.text.trim();
- const leafBefore = store.activeLeaf(threadId);
+ const leafBefore = store.activeLeaf(threadId), rewoundBefore = task.rewound;
const userMessage = store.branchMessage(threadId, source.id, message, sendId, undefined, undefined, { via: "api", ...(owned ? { apiAsOwner: true as const } : {}) });
if (!userMessage) return null;
store.patchTask(botId, threadId, { rewound: true });
@@ -235,9 +235,11 @@ export function createBotApiHost(deps: BotApiHostDeps) {
onDispatchError: () => markRunning(botId, threadId, false),
});
} catch (error) {
- // A turn that never started leaves the conversation on the branch the person was reading.
+ // A turn that never started leaves the conversation on the branch the person was reading. The line stays, as a
+ // refused call's does, so a status read answers it with no reply.
markRunning(botId, threadId, false);
if (leafBefore) store.setActiveLeaf(threadId, leafBefore);
+ store.patchTask(botId, threadId, { rewound: rewoundBefore });
throw error;
}
return userMessage;
diff --git a/server/bot-api.e2e.test.ts b/server/bot-api.e2e.test.ts
index c2f8829c91..7e76025637 100644
--- a/server/bot-api.e2e.test.ts
+++ b/server/bot-api.e2e.test.ts
@@ -3,11 +3,11 @@
// and, on a bot that asks before acting, a call that stops at the approval nobody outside may give.
import { execFile } from "node:child_process";
import { randomUUID } from "node:crypto";
-import { mkdirSync, mkdtempSync, readFileSync, realpathSync } from "node:fs";
+import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { connect, type Socket } from "node:net";
import { join } from "node:path";
-import { afterEach, expect, it } from "vitest";
+import { afterEach, expect, it, onTestFinished } from "vitest";
import { launchVerificationServer, runControlOmb, type VerificationServer } from "../scripts/control-omb.ts";
import { botApiCurl } from "../shared/bot-api.ts";
@@ -264,7 +264,9 @@ it("says it is still working when the reply takes longer than the caller will wa
}, hostTimeout(60_000));
it("a Retry in the app on a caller's line runs it again as the call: the caller's line, framed and fenced, and the status reads the retried turn", async () => {
- const replyState = join(mkdtempSync(join(tmpdir(), "omb-bot-api-retry-")), "replies");
+ const replies = mkdtempSync(join(tmpdir(), "omb-bot-api-retry-"));
+ onTestFinished(() => rmSync(replies, { recursive: true, force: true }));
+ const replyState = join(replies, "replies");
fixture = await launchVerificationServer({ ...process.env, FAKE_CLAUDE_VERSION: "2.1.284", FAKE_CLAUDE_REPLIES: JSON.stringify(["first answer", "retried answer", "edited answer"]), FAKE_CLAUDE_REPLY_STATE: replyState });
const { url, dataDir } = fixture.info;
const api = appApi(url);