Skip to content

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

Let go of an archived main thread when only a bot's thread is left open

luishron123ceae66002aa2
diff --git a/server/main-thread.e2e.test.ts b/server/main-thread.e2e.test.ts
index 814ab39a72..5eba468b40 100644
--- a/server/main-thread.e2e.test.ts
+++ b/server/main-thread.e2e.test.ts
@@ -285,3 +285,39 @@ it("keeps the selected thread as the main one across an update for a bot nobody
await server.close();
}
}, 120_000);
+
+it("moves an archived or deleted main thread to the only open one, a thread the bot opened, and keeps it there", async () => {
+ const { fixture, dataDir, api, cli, wire, thread, replied } = await launch();
+ try {
+ const { bot } = await cli("new-bot", "--name", "Keeper");
+ writeFileSync(join(dataDir, "room-plan.json"), JSON.stringify({
+ [bot.id]: { turns: [
+ { steps: [{ tool: "start_thread", arguments: { title: "Side job", message: "Check the side job." } }], reply: "Opened a side job." },
+ { reply: "Side job checked." },
+ ] },
+ }));
+ const first = bot.activeTaskId as string;
+ await cli("send", "--bot", bot.id, "--task", first, "--text", "Open a side job.");
+ const side = await thread(bot.id, "Side job");
+ await replied(bot.id, side.threadId, "Side job checked.");
+ expect(side.openedBy).toMatchObject({ botId: bot.id });
+ const main = async () => (await wire(bot.id)).mainThreadId;
+ const open = async (title: string) => (await api("POST", `/api/bots/${bot.id}/tasks`, { title, approvalMode: "ask" }, 201)).task.threadId as string;
+
+ await api("PATCH", `/api/bots/${bot.id}`, { mainThreadChoice: first });
+ expect(await main()).toBe(first);
+ await api("PATCH", `/api/bots/${bot.id}/tasks/${first}`, { archivedAt: Date.now() });
+ expect(await main(), "the thread the bot opened is the only one open").toBe(side.threadId);
+ await api("PATCH", `/api/bots/${bot.id}/tasks/${first}`, { archivedAt: null });
+ expect(await main(), "reopening the old main thread moves nothing").toBe(side.threadId);
+
+ await api("PATCH", `/api/bots/${bot.id}`, { mainThreadChoice: first });
+ expect(await main()).toBe(first);
+ await api("DELETE", `/api/bots/${bot.id}/tasks/${first}`);
+ expect(await main()).toBe(side.threadId);
+ await open("Later");
+ expect(await main(), "a thread the person opens afterwards moves nothing").toBe(side.threadId);
+ } finally {
+ await fixture.close();
+ }
+}, 120_000);
diff --git a/server/main-thread.test.ts b/server/main-thread.test.ts
index e6c718c020..8833aac84f 100644
--- a/server/main-thread.test.ts
+++ b/server/main-thread.test.ts
@@ -80,6 +80,14 @@ describe("the main thread a bot keeps", () => {
expect(mainThreadToKeep([latest, later, { ...first, archivedAt: 40 }], "first", "latest")).toBe("later");
expect(mainThreadToKeep([latest, { ...later, openedBy: { botId: "teammate" } }], "first", "later")).toBe("latest");
});
+
+ it("is the oldest open thread a bot opened once the saved thread is archived or deleted and the person has no other open", () => {
+ const handed = { ...later, openedBy: { botId: "teammate" } };
+ expect(mainThreadToKeep([{ ...latest, openedBy: { botId: "teammate" } }, handed, { ...first, archivedAt: 40 }], "first")).toBe("later");
+ expect(mainThreadToKeep([handed], "first")).toBe("later");
+ expect(mainThreadToKeep([handed, first], "later")).toBeUndefined();
+ expect(mainThreadToKeep([{ ...handed, archivedAt: 41 }, { ...first, archivedAt: 40 }], "first")).toBeUndefined();
+ });
});
describe("the oldest thread", () => {
diff --git a/server/main-thread.ts b/server/main-thread.ts
index 3acd86eb3f..702efbc980 100644
--- a/server/main-thread.ts
+++ b/server/main-thread.ts
@@ -33,10 +33,10 @@ export function mainThread(tasks: readonly MainThreadCandidate[], chosenId?: str
/** The thread to save as a bot's choice so its main thread stays put: nothing while the saved one is open. A bot that
* never had one keeps the thread it showed until now: the open one a person last wrote in, else the selected one, else
* the oldest open one. One whose saved thread was archived or deleted moves to the oldest open conversation a person
- * started, and reopening the old one moves nothing. */
+ * started, else the oldest open one, and reopening the old one moves nothing. */
export function mainThreadToKeep(tasks: readonly MainThreadCandidate[], chosenId?: string, selectedId?: string): string | undefined {
- if (chosenId) return tasks.some((task) => task.threadId === chosenId && open(task)) ? undefined : oldestThread(tasks.filter((task) => !task.openedBy && open(task)));
const live = tasks.filter(open);
+ if (chosenId) return live.some((task) => task.threadId === chosenId) ? undefined : oldestThread(live.filter((task) => !task.openedBy)) ?? oldestThread(live);
const asked = live.reduce<MainThreadCandidate | undefined>(
(latest, task) => ((task.askedAt ?? -1) > (latest?.askedAt ?? -1) ? task : latest), undefined,
);