Skip to content

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

End a shared mark on an inserted reply and tell a mark with its hold

luishron12363f34211e272
diff --git a/server/held-unread.e2e.test.ts b/server/held-unread.e2e.test.ts
index 0c1e8c9c0b..9854e80815 100644
--- a/server/held-unread.e2e.test.ts
+++ b/server/held-unread.e2e.test.ts
@@ -26,10 +26,16 @@ it("holds a conversation marked unread with it open against being on screen, in
await expect.poll(async () => (await reloaded()).unread, { timeout: 30_000 }).toBe(true);
};
const mark = async () => {
+ const before = otherWindow.frames.length;
await api("PATCH", `/api/bots/${botId}`, { unread: true });
- return (await otherWindow.until((frame) => frame.kind === "bot" && frame.bot?.id === botId &&
- frame.bot.tasks?.some((task: any) => task.threadId === threadId && task.heldUnread !== undefined), 10_000))
- .bot.tasks.find((task: any) => task.threadId === threadId);
+ const held = await otherWindow.until((frame) => otherWindow.frames.indexOf(frame) >= before && frame.kind === "bot" && frame.bot?.id === botId &&
+ frame.bot.tasks?.some((task: any) => task.threadId === threadId && task.heldUnread !== undefined), 10_000);
+ const states = otherWindow.frames.slice(before, otherWindow.frames.indexOf(held) + 1)
+ .filter((frame) => frame.kind === "bot" && frame.bot?.id === botId)
+ .map((frame) => frame.bot.tasks?.find((task: any) => task.threadId === threadId))
+ .map((task) => ({ unread: task?.unread === true, held: task?.heldUnread !== undefined }));
+ expect(states, "never told unread without its mark").not.toContainEqual({ unread: true, held: false });
+ return held.bot.tasks.find((task: any) => task.threadId === threadId);
};
await answered("Summarize the queue.");
await api("POST", `/api/bots/${botId}/read`, { threadId });
diff --git a/server/host/room-reads.test.ts b/server/host/room-reads.test.ts
index a8e79d748f..84d44774ee 100644
--- a/server/host/room-reads.test.ts
+++ b/server/host/room-reads.test.ts
@@ -157,3 +157,35 @@ it("shows a person's mark on a shared chat with no reply before its newest one a
rooms.personMarkThread!(ada, "main", true);
expect(rooms.taskForPerson(main, adaKey), "marked with only that first reply in it").toEqual({ threadId: "main", unread: true, heldUnread: "m1" });
});
+
+it("ends each person's mark on a shared chat when a reply is inserted before the message they marked, keeping their lines apart", () => {
+ const dir = mkdtempSync(join(tmpdir(), "room-reads-"));
+ const thread: Array<{ id: string; role: string; kind?: string; at: number }> = [{ id: "m1", role: "user", at: 1 }, { id: "m2", role: "bot", at: 2 }, { id: "m3", role: "user", at: 3 }, { id: "m4", role: "bot", at: 4 }];
+ const rooms = createPersonRooms({
+ reads: createRoomReads(dir),
+ store: () => ({ activePath: () => thread, messagesFor: () => thread, botByThread: () => ({ id: "scout" }) }),
+ shared: (threadId) => threadId === "main",
+ wireBot: (id) => ({ id }),
+ newestMessageId: () => thread.at(-1)?.id,
+ publicGroupState: (room) => room as never,
+ broadcast: () => {},
+ });
+ const session = (email: string) => ({ kind: "session", session: { id: email, email } }) as never;
+ const ada = session("[email protected]"), ben = session("[email protected]"), cleo = session("[email protected]");
+ const key = (email: string) => personKey({ id: "", email });
+ const main = { threadId: "main", unread: false };
+ rooms.personMarkThread!(ada, "main", true);
+ rooms.personMarkThread!(ben, "main", true);
+ rooms.personMarkThread!(cleo, "main", false);
+ thread.splice(2, 0, { id: "late", role: "bot", at: 5 });
+ expect(rooms.personHeldUnread!(ada, "main"), "a reply newer than the marked message, placed before it").toBe(false);
+ expect(rooms.personHeldUnread!(ben, "main")).toBe(false);
+ expect(rooms.taskForPerson(main, key("[email protected]")), "still unread, read up to where Ada was")
+ .toEqual({ threadId: "main", unread: true, lastReadMessageId: "m3" });
+ expect(rooms.taskForPerson(main, key("[email protected]")), "Cleo's line is her own").toEqual({ threadId: "main", unread: false, lastReadMessageId: "m4" });
+ rooms.personMarkThread!(ada, "main", true);
+ expect(rooms.personHeldUnread!(ada, "main"), "marking again after it holds").toBe(true);
+ expect(rooms.personHeldUnread!(ben, "main"), "without bringing back Ben's").toBe(false);
+ thread.splice(3, 0, { id: "trail", role: "bot", kind: "activity", at: 6 });
+ expect(rooms.personHeldUnread!(ada, "main"), "an inserted working line is no reply").toBe(true);
+});
diff --git a/server/host/room-reads.ts b/server/host/room-reads.ts
index 5f1d6b77f4..b37851591d 100644
--- a/server/host/room-reads.ts
+++ b/server/host/room-reads.ts
@@ -28,12 +28,20 @@ export function roomUnreadFor(path: readonly PathMessage[], readUpTo: string | u
return path.slice(from).some(isReply);
}
-/** A conversation marked unread with `held` its newest message ("" for none) stays held until a reply comes after it. A
- * message no longer on the path (an edit or a retry moved it) ends the mark. */
+/** The message a mark holds from: the path's newest by time, since a reply inserted mid-history is newer than the line
+ * after it; "" for an empty conversation. */
+export function heldFrom(path: readonly PathMessage[]): string {
+ return path.reduce<PathMessage | undefined>((newest, message) => !newest || (message.at ?? 0) >= (newest.at ?? 0) ? message : newest, undefined)?.id ?? "";
+}
+
+/** A conversation marked unread with `held` from {@link heldFrom} stays held until a reply newer than it comes, wherever
+ * on the path it lands. A message no longer on the path (an edit or a retry moved it) ends the mark. */
export function stillHeldUnread(path: readonly PathMessage[], held: string | undefined): boolean {
if (held === undefined) return false;
- const at = held === "" ? -1 : path.findIndex((message) => message.id === held);
- return (held === "" || at >= 0) && !path.slice(at + 1).some(isReply);
+ const index = held === "" ? -1 : path.findIndex((message) => message.id === held), heldAt = path[index]?.at;
+ if (held !== "" && index < 0) return false;
+ const newer = (message: PathMessage, at: number) => heldAt === undefined || message.at === undefined ? at > index : message.at > heldAt || (at > index && message.at === heldAt);
+ return !path.some((message, at) => isReply(message) && newer(message, at));
}
/** `alsoForget`: a person's other marks of their own (their bots' order, their notification reads), which go with
@@ -190,7 +198,7 @@ export function createPersonRooms(deps: PersonRoomsDeps) {
const path = deps.store().activePath(threadId), reply = path.findLastIndex(isReply);
if (reply > 0) reads.read(key, person, path[reply - 1]!.id);
else reads.forget(key, person);
- reads.read(threadHoldKey(threadId), person, newest ?? "");
+ reads.read(threadHoldKey(threadId), person, heldFrom(path));
} else {
reads.forget(threadHoldKey(threadId), person);
if (newest) reads.read(key, person, newest);
diff --git a/server/routes/bot-patch.ts b/server/routes/bot-patch.ts
index 75d8b95e0e..6630e668e8 100644
--- a/server/routes/bot-patch.ts
+++ b/server/routes/bot-patch.ts
@@ -704,6 +704,8 @@ export function createBotPatchRoutes(deps: BotPatchRouteDeps): RouteHandler {
(freshBrowserBot.busy || browserRuntime.heldBy(currentBrowserSession(freshBrowserBot.id, freshBrowserBot.browserProfile)))) {
return json(res, 409, { error: "Stop the bot and release browser control before changing its profile." });
}
+ // A mark goes out with its hold in one frame, below.
+ delete patch.unread;
if (Array.isArray(patch.alwaysAllow)) dropRemovedGrants(store, deps.turnBots?.values() ?? [], m[1], patch.alwaysAllow);
if (profile.patch.soul !== undefined) {
if (freshBrowserBot) assertTeamComputerChangeIdle(freshBrowserBot, { ...freshBrowserBot, ...patch } as BotRecord);
@@ -714,7 +716,7 @@ export function createBotPatchRoutes(deps: BotPatchRouteDeps): RouteHandler {
bot = store.patchBotProfile(m[1], profile.patch);
} else {
if (freshBrowserBot) assertTeamComputerChangeIdle(freshBrowserBot, { ...freshBrowserBot, ...patch } as BotRecord);
- bot = store.patchBot(m[1], patch);
+ bot = Object.keys(patch).length ? store.patchBot(m[1], patch) : store.bot(m[1]) ?? null;
}
if (!bot) return json(res, 404, { error: "no such bot" });
if (typeof body.unread === "boolean") {