Skip to content

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

perf(chat): index newest children when switching branches

Afloatc2f7b18b8c0a
diff --git a/server/branch-leaf-index.test.ts b/server/branch-leaf-index.test.ts
new file mode 100644
index 0000000000..2005a287ae
--- /dev/null
+++ b/server/branch-leaf-index.test.ts
@@ -0,0 +1,31 @@
+import { afterEach, expect, it } from "vitest";
+import { closeMessageDb, importThread, readStoredActiveLeaf } from "./message-db.ts";
+import { Store, type Message } from "./store.ts";
+
+afterEach(() => closeMessageDb());
+
+const message = (id: string, parentId: string | null, at: number): Message => ({ id, parentId, at, role: "user", kind: "text", text: id });
+const selection = () => ({ instanceId: "fixture", model: "fixture" });
+
+it("chooses the newest child at every branch and persists its final descendant", () => {
+ const threadId = "newest-child";
+ importThread(threadId, [message("root", null, 1), message("older", "root", 2), message("older-tail", "older", 100), message("newer", "root", 5), message("newer-tail", "newer", 6), message("last-but-older", "root", 3)], "last-but-older");
+ const store = new Store(selection);
+ const leaves: string[] = [];
+ store.onChange((event) => { if (event.type === "thread" && event.activeLeafId) leaves.push(event.activeLeafId); });
+ expect(store.setActiveLeaf(threadId, "root")).toBe("newer-tail");
+ expect(leaves).toEqual(["newer-tail"]);
+ expect(readStoredActiveLeaf(threadId)).toBe("newer-tail");
+ closeMessageDb();
+ expect(new Store(selection).activeLeaf(threadId)).toBe("newer-tail");
+});
+
+it("uses the later stored sibling to break equal timestamps and keeps other roots separate", () => {
+ const threadId = "equal-time";
+ importThread(threadId, [message("root", null, 1), message("first", "root", 5), message("second", "root", 5), message("other-root", null, 100), message("other-tail", "other-root", 200)], "other-tail");
+ const store = new Store(selection);
+ expect(store.setActiveLeaf(threadId, "root")).toBe("second");
+ expect(store.setActiveLeaf(threadId, "other-root")).toBe("other-tail");
+ expect(store.setActiveLeaf(threadId, "missing")).toBeNull();
+ expect(readStoredActiveLeaf(threadId)).toBe("other-tail");
+});
diff --git a/server/store.ts b/server/store.ts
index 3604c07b3e..629972eb57 100644
--- a/server/store.ts
+++ b/server/store.ts
@@ -1229,11 +1229,17 @@ export class Store {
setActiveLeaf(threadId: string, messageId: string): string | null {
const t = this.thread(threadId);
if (!t.messages.some((m) => m.id === messageId)) return null;
+ const newestChild = new Map<string, Message>();
+ for (const message of t.messages) {
+ if (typeof message.parentId !== "string") continue;
+ const previous = newestChild.get(message.parentId);
+ if (!previous || message.at >= previous.at) newestChild.set(message.parentId, message);
+ }
let cur = messageId;
for (;;) {
- const children = t.messages.filter((m) => m.parentId === cur);
- if (!children.length) break;
- cur = children.reduce((a, b) => (b.at >= a.at ? b : a)).id;
+ const child = newestChild.get(cur);
+ if (!child) break;
+ cur = child.id;
}
t.activeLeafId = cur;
mdb.setActiveLeaf(threadId, cur);