Skip to content

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

Drop the main thread saved for a bot once its choice changes

luishron123b26f9c5a09d5
diff --git a/server/main-thread.e2e.test.ts b/server/main-thread.e2e.test.ts
index d6bcc96a41..23a3f7c234 100644
--- a/server/main-thread.e2e.test.ts
+++ b/server/main-thread.e2e.test.ts
@@ -428,3 +428,56 @@ it("shows the main thread the person chose in an older version, not the one save
await server.close();
}
}, 180_000);
+
+it("drops the thread saved for a bot once an older version chooses another, so clearing that choice does not bring it back", async () => {
+ const server = await launchRestartableServer();
+ const api = async (method: string, path: string, body?: unknown, status = 200) => {
+ const response = await fetch(`${server.url}${path}`, {
+ method, headers: { "content-type": "application/json", origin: server.url },
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
+ });
+ const value = await response.json() as any;
+ expect(response.status, `${method} ${path}: ${JSON.stringify(value)}`).toBe(status);
+ return value;
+ };
+ const cli = (...args: string[]) => runControlOmb([...args, "--url", server.url]) as Promise<any>;
+ try {
+ const { bot } = await cli("new-bot", "--name", "Switcher");
+ const chosen = bot.activeTaskId as string;
+ const main = async () => (await api("GET", "/api/bots")).bots.find((candidate: any) => candidate.id === bot.id).mainThreadId;
+ const saved = () => readRegistry(server.dataDir).bots.find((candidate: any) => candidate.id === bot.id);
+ const file = join(server.dataDir, "bots.json");
+ const rewrite = (change: (record: any) => any) => writeFileSync(file, JSON.stringify((JSON.parse(readFileSync(file, "utf8")) as any[]).map(change)));
+ const writeIn = async (threadId: string) => {
+ await cli("send", "--bot", bot.id, "--task", threadId, "--text", "Note this.");
+ await expect.poll(async () => ((await api("GET", `/api/threads/${threadId}/messages?limit=50`)).messages as any[]).some((message) => message.role !== "user"), { timeout: 20_000 }).toBe(true);
+ await expect.poll(async () => (await api("GET", "/api/bots")).bots.find((candidate: any) => candidate.id === bot.id).busy, { timeout: 20_000 }).toBe(false);
+ };
+ const kept = (await api("POST", `/api/bots/${bot.id}/tasks`, { title: "Kept", approvalMode: "ask" }, 201)).task.threadId as string;
+ const later = (await api("POST", `/api/bots/${bot.id}/tasks`, { title: "Later", approvalMode: "ask" }, 201)).task.threadId as string;
+ await writeIn(kept);
+
+ await server.stop();
+ rewrite(({ mainThreadChoice: _choice, mainThreadKept: _kept, ...rest }) => rest);
+ await server.start();
+ expect(await main()).toBe(kept);
+ await api("POST", `/api/bots/${bot.id}/tasks/${chosen}`);
+ await server.stop();
+ expect(saved().mainThreadKept).toEqual({ id: kept });
+
+ // An older version knows only mainThreadChoice: the person picks another thread there.
+ rewrite((record) => ({ ...record, mainThreadChoice: chosen }));
+ await server.start();
+ expect(await main(), "the person's later choice").toBe(chosen);
+ await writeIn(later);
+ await server.stop();
+ expect(saved().mainThreadKept, "the thread kept under no choice is gone from disk").toBeUndefined();
+
+ // The older version again, clearing the choice.
+ rewrite(({ mainThreadChoice: _choice, ...rest }) => rest);
+ await server.start();
+ expect(await main(), "what a bot with no choice shows: the conversation the person last wrote in").toBe(later);
+ } finally {
+ await server.close();
+ }
+}, 180_000);
diff --git a/server/store.ts b/server/store.ts
index d56dd42095..dda9776ebd 100644
--- a/server/store.ts
+++ b/server/store.ts
@@ -153,7 +153,7 @@ export function titleFromLlm(raw: string): string | null {
export interface BotRecord extends Omit<WireBot, "avatarUrl" | "tasks"> {
tasks?: TaskRecord[];
avatarUrl?: string;
- resumeCursors: Record<string, unknown>; /** Set on a stand-in: the place this bot lives in, and its id there. */ home?: { place: string; placeName: string; botId: string; /** What that place is, as the sidebar's rows say it. */ kind?: "computer" | "cloud"; /** It cannot be reached now: the link is off or its place is unlinked ("off"), or its place no longer has it ("missing"). */ gone?: "off" | "missing" }; mainThreadChoice?: string; mainThreadKept?: { id: string; under?: string }; // Host-only; clients get mainThreadId. mainThreadChoice is the person's "Set as main thread"; mainThreadKept is the one saved for them so it stays put, and no memory follows it. It holds only under the choice it was saved with: a version that does not know it can change the choice and leave it behind.
+ resumeCursors: Record<string, unknown>; /** Set on a stand-in: the place this bot lives in, and its id there. */ home?: { place: string; placeName: string; botId: string; /** What that place is, as the sidebar's rows say it. */ kind?: "computer" | "cloud"; /** It cannot be reached now: the link is off or its place is unlinked ("off"), or its place no longer has it ("missing"). */ gone?: "off" | "missing" }; mainThreadChoice?: string; mainThreadKept?: { id: string; under?: string }; // Host-only; clients get mainThreadId. mainThreadChoice is the person's "Set as main thread"; mainThreadKept is the one saved for them so it stays put, and no memory follows it. It holds only under the choice it was saved with, and is dropped once that choice changes: a version that does not know it can change the choice and leave it behind, then clear it.
/** Elevation journal: Full/Custom runs as Ask until Electron confirms the prepared reply; a marker surviving a restart is revoked on load. */
approvalGrant?: {
requestId: string;
@@ -1459,7 +1459,7 @@ export class Store {
for (const g of rooms) this.emit({ type: "group", groupId: g.id });
return true;
}
- /** A bot's saved main thread, after saving the one `keep` names for it: no save of its own, it goes with the bot's next one. */ keepMainThread(id: string, keep: (chosen?: string) => string | undefined): string | undefined { const saved = (record?: BotRecord | null) => (record?.mainThreadKept?.id && record.mainThreadKept.under === record.mainThreadChoice ? record.mainThreadKept.id : record?.mainThreadChoice), bot = this.own(id), next = bot && keep(saved(bot)); if (!bot || !next) return saved(this.bot(id)); this.registry.changedInPlace({ kind: "bot", id }); bot.mainThreadKept = { id: next, under: bot.mainThreadChoice }; return next; }
+ /** A bot's saved main thread, after saving the one `keep` names for it: no save of its own, it goes with the bot's next one. */ keepMainThread(id: string, keep: (chosen?: string) => string | undefined): string | undefined { const saved = (record?: BotRecord | null) => (record?.mainThreadKept?.id && record.mainThreadKept.under === record.mainThreadChoice ? record.mainThreadKept.id : record?.mainThreadChoice), bot = this.own(id); if (bot?.mainThreadKept && bot.mainThreadKept.under !== bot.mainThreadChoice) { delete bot.mainThreadKept; saveRegistryFor("bots.json", "a main thread saved under an older choice", () => this.saveBotThread(id, undefined, "pass")); } const next = bot && keep(saved(bot)); if (!bot || !next) return saved(this.bot(id)); this.registry.changedInPlace({ kind: "bot", id }); bot.mainThreadKept = { id: next, under: bot.mainThreadChoice }; return next; }
patchBot(id: string, patch: Partial<BotRecord>): BotRecord | null {
const bot = this.own(id);
if (!bot) return null; patch = reshowWhenMoved(bot, patch);