Skip to content

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

fix(server): target open main thread when marking bot unread from routine or archived active task

luishron1239d230eca2c7e
diff --git a/server/patch-bot-unread.test.ts b/server/patch-bot-unread.test.ts
new file mode 100644
index 0000000000..0d95fecdd3
--- /dev/null
+++ b/server/patch-bot-unread.test.ts
@@ -0,0 +1,74 @@
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+let home: string | undefined;
+
+async function freshStore() {
+ home = mkdtempSync(join(tmpdir(), "omb-patch-bot-unread-"));
+ vi.resetModules();
+ vi.stubEnv("HOME", home);
+ vi.stubEnv("USERPROFILE", home);
+ const { Store } = await import("./store.ts");
+ return { store: new Store(() => ({ instanceId: "claude", model: "m" })) };
+}
+
+afterEach(async () => {
+ if (!home) return;
+ const { closeMessageDb } = await import("./message-db.ts");
+ closeMessageDb();
+ vi.unstubAllEnvs();
+ rmSync(home, { recursive: true, force: true });
+ home = undefined;
+});
+
+describe("patchBot unread targeting", () => {
+ it("marks the open main conversation unread when activeTask is a routine run, without dirtying the routine run", async () => {
+ const { store } = await freshStore();
+ const bot = store.createBot();
+ const openThreadId = bot.threadId;
+ const routine = store.createTask(bot.id, "Daily check", true)!;
+ store.patchTask(bot.id, routine.threadId, { routineRunId: "run-1", unread: false });
+ expect(store.bot(bot.id)!.threadId).toBe(routine.threadId);
+
+ const patched = store.patchBot(bot.id, { unread: true })!;
+ expect(patched.unread).toBe(true);
+ expect(store.taskByThread(bot.id, routine.threadId)!.unread).toBe(false);
+ expect(store.taskByThread(bot.id, openThreadId)!.unread).toBe(true);
+
+ const { Store } = await import("./store.ts");
+ const reloaded = new Store(() => ({ instanceId: "claude", model: "m" }));
+ expect(reloaded.bot(bot.id)!.unread).toBe(true);
+ expect(reloaded.taskByThread(bot.id, routine.threadId)!.unread).toBe(false);
+ expect(reloaded.taskByThread(bot.id, openThreadId)!.unread).toBe(true);
+ });
+
+ it("marks the open main conversation unread when activeTask is archived, leaving the archived thread untouched", async () => {
+ const { store } = await freshStore();
+ const bot = store.createBot();
+ const firstOpen = bot.threadId;
+ const chosenOpen = store.createTask(bot.id, "Chosen main", false)!;
+ store.patchBot(bot.id, { mainThreadChoice: chosenOpen.threadId });
+ const oldTask = store.createTask(bot.id, "Closed chat", true)!;
+ store.patchTask(bot.id, oldTask.threadId, { archivedAt: 1_000, unread: false });
+ store.switchTask(bot.id, oldTask.threadId);
+ expect(store.bot(bot.id)!.threadId).toBe(oldTask.threadId);
+
+ const patched = store.patchBot(bot.id, { unread: true })!;
+ expect(patched.unread).toBe(true);
+ expect(store.taskByThread(bot.id, oldTask.threadId)!.unread).toBe(false);
+ expect(store.taskByThread(bot.id, firstOpen)!.unread).toBe(false);
+ expect(store.taskByThread(bot.id, chosenOpen.threadId)!.unread).toBe(true);
+ });
+
+ it("leaves bot.unread false when every non-routine conversation is archived", async () => {
+ const { store } = await freshStore();
+ const bot = store.createBot();
+ store.patchTask(bot.id, bot.threadId, { archivedAt: 1_000, unread: false });
+
+ const patched = store.patchBot(bot.id, { unread: true })!;
+ expect(patched.unread).toBe(false);
+ expect(store.taskByThread(bot.id, bot.threadId)!.unread).toBe(false);
+ });
+});
diff --git a/server/store.ts b/server/store.ts
index 3237696369..ae25d7e77c 100644
--- a/server/store.ts
+++ b/server/store.ts
@@ -42,7 +42,7 @@ import { appsFromCreator } from "./inherited-apps.ts"; import { newBotOwner, sha
import { ThreadCache, type ThreadState } from "./thread-cache.ts";
import { lastMessageId, messagesBefore } from "./thread-history.ts";
import { titleFromMessage, UNTITLED_TASK } from "./thread-title.ts";
-import { buildTask, threadTitleFrom, UNTITLED_THREAD, type NewThreadLevel } from "./task-record.ts";
+import { buildTask, threadTitleFrom, unreadTargetTask, UNTITLED_THREAD, type NewThreadLevel } from "./task-record.ts";
import { repairLegacyWorkThreads } from "./legacy-work-threads.ts";
import { createBotFork, createGroupFork } from "./store-forks.ts";
import { branchLeafFrom } from "../shared/conversation-branch.ts";
@@ -1485,14 +1485,14 @@ export class Store {
Object.assign(bot, patch); if (patch.connectorScopes !== undefined) grantScopedApps(bot);
for (const task of toAsk) Object.assign(task, structuredClone(ASK_NOW));
const task = this.activeTask(id);
- if (task) {
- for (const key of ["resumeCursors", "rewound", "pinnedMessageId", "unread"] as const) {
- if (Object.prototype.hasOwnProperty.call(patch, key)) {
- Object.assign(task, { [key]: structuredClone(patch[key]) });
- }
- }
- bot.unread = bot.tasks!.some(taskCountsAsBotUnread);
+ if (task) for (const key of ["resumeCursors", "rewound", "pinnedMessageId"] as const) {
+ if (Object.hasOwn(patch, key)) Object.assign(task, { [key]: structuredClone(patch[key]) });
+ }
+ if (Object.hasOwn(patch, "unread") && bot.tasks?.length) {
+ const target = unreadTargetTask(bot, task, patch.unread);
+ if (target) { target.unread = Boolean(patch.unread); if (target.snoozedUntil === 0 && target.unread) target.snoozedUntil = undefined; }
}
+ if (bot.tasks?.length) bot.unread = bot.tasks.some(taskCountsAsBotUnread);
if (wideningToolScope) {
// New authority goes live only after its durable save succeeds.
this.saveBots(this.bots.map((candidate) => candidate === bot ? { ...bot, toolScope: nextToolScope } : candidate));
diff --git a/server/task-record.ts b/server/task-record.ts
index 3f5f6f0d7b..e64eaf2084 100644
--- a/server/task-record.ts
+++ b/server/task-record.ts
@@ -2,6 +2,7 @@ import { approvalModeFor, type ApprovalMode } from "../shared/approval-mode.ts";
import { sameModelSelection } from "../shared/thread-model.ts";
import type { TaskOpenedBy } from "../shared/wire.ts";
import { newId, type ModelSelection } from "./contracts.ts";
+import { mainThread } from "./main-thread.ts";
import type { BotRecord, TaskRecord } from "./store.ts";
export const UNTITLED_THREAD = "New thread";
@@ -11,6 +12,13 @@ export function threadTitleFrom(title?: string): string {
return title?.trim().slice(0, TASK_TITLE_MAX) || UNTITLED_THREAD;
}
+export function unreadTargetTask(bot: BotRecord, active: TaskRecord | undefined, unread: boolean | undefined): TaskRecord | undefined {
+ if (unread !== true) return active && !active.routineRunId ? active : undefined;
+ if (active && !active.routineRunId && active.archivedAt === undefined) return active;
+ const fallbackId = mainThread(bot.tasks ?? [], bot.threadId, bot.mainThreadChoice);
+ return bot.tasks?.find((task) => task.threadId === fallbackId && !task.routineRunId && task.archivedAt === undefined);
+}
+
/** A new thread starts at its bot's level. A pending grant is not that level: a composer grant belongs to its own
* thread, and a bot-wide one may still roll back, so a thread made meanwhile follows the bot live instead of keeping
* the Ask mask the grant shows (or a Full access that was never confirmed). */