Skip to content

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

fix(memory): keep history paging usable with long thread notes

Afloat30de19076bba
diff --git a/server/thread-notes.test.ts b/server/thread-notes.test.ts
index 898a4194e3..d725f35942 100644
--- a/server/thread-notes.test.ts
+++ b/server/thread-notes.test.ts
@@ -3,9 +3,11 @@ import { join } from "node:path";
import { describe, expect, it } from "vitest";
-import { memoryOverview, parseMemoryPath, readMemoryDoc, threadNotesPath } from "./memory-store.ts";
+import { memoryOverview, parseMemoryPath, readMemoryDoc, threadNotesPath, writeMemoryDoc } from "./memory-store.ts";
import {
THREAD_NOTES_MAX_LINES,
+ THREAD_NOTES_MAX_BYTES,
+ THREAD_READ_BUDGET,
createThreadScope,
reachThreads,
readThreadNotes,
@@ -194,6 +196,44 @@ describe("reaching another conversation on request", () => {
expect(reach(other, { query: "liability" })).toEqual({ total: 0, threads: [] });
});
+ it("keeps a cursor through every message when legal notes exceed the page budget", () => {
+ const botId = freshBot(), notes = "A clause for this conversation. ".repeat(300).trim();
+ expect(notes.length).toBeGreaterThan(THREAD_READ_BUDGET);
+ expect(Buffer.byteLength(notes, "utf8")).toBeLessThan(THREAD_NOTES_MAX_BYTES);
+ writeMemoryDoc(botId, threadNotesPath("sm"), notes);
+ const messages = [line("m1", "user", "first ".repeat(100)), line("m2", "bot", "second ".repeat(100)), line("m3", "user", "third ".repeat(100))];
+ const seen: string[] = [];
+ let before: string | undefined;
+ for (let index = 0; index <= messages.length; index += 1) {
+ const page = reach(botId, { threadId: "sm", before, messages: () => messages });
+ if (!("messages" in page) || !page.messages) throw new Error("expected a conversation page");
+ expect(page.messages.length).toBeGreaterThan(0);
+ expect(page.notesTruncated).toBe(true);
+ expect(page.notes!.length + page.messages.reduce((sum, message) => sum + message.text.length, 0)).toBeLessThanOrEqual(THREAD_READ_BUDGET);
+ seen.push(...page.messages.map((message) => message.messageId!));
+ if (page.earlier === null) break;
+ before = page.earlier!;
+ }
+ expect(seen).toEqual(["m3", "m2", "m1"]);
+ expect(readThreadNotes(botId, "sm")).toBe(notes);
+ });
+
+ it("does not consume an older message when the remaining budget cannot hold its first character", () => {
+ const botId = freshBot();
+ writeMemoryDoc(botId, threadNotesPath("sm"), "n".repeat(7_500));
+ const messages = [line("m1", "user", "πŸ™‚"), line("m2", "bot", "x".repeat(499) + "πŸ™‚")];
+ const page = reach(botId, { threadId: "sm", messages: () => messages });
+ expect(page).toMatchObject({ messages: [{ messageId: "m2", text: "x".repeat(499), truncated: true }], earlier: "m2", notesTruncated: false });
+ expect(reach(botId, { threadId: "sm", before: "m2", messages: () => messages })).toMatchObject({ messages: [{ messageId: "m1", text: "πŸ™‚", truncated: false }], earlier: null });
+ });
+
+ it("bounds the notes even when there are no messages to show", () => {
+ const botId = freshBot();
+ writeMemoryDoc(botId, threadNotesPath("sm"), "n".repeat(9_000));
+ const page = reach(botId, { threadId: "sm", messages: () => [] });
+ expect(page).toMatchObject({ notes: "n".repeat(THREAD_READ_BUDGET), notesTruncated: true, messages: [], earlier: null });
+ });
+
it("reads no notes while the bot's memory is off", () => {
const botId = freshBot();
updateThreadNotes(botId, "sm", { action: "append", text: "Clause 7 agreed" });
diff --git a/server/thread-notes.ts b/server/thread-notes.ts
index 5462c63d98..db6fa10334 100644
--- a/server/thread-notes.ts
+++ b/server/thread-notes.ts
@@ -209,18 +209,22 @@ export function reachThreads(input: {
const lines = input.messages(thread.threadId).filter((line) => (line.role === "user" || line.role === "bot") && line.kind === "text" && line.text?.trim());
const end = input.before ? lines.findIndex((line) => line.id === input.before) : lines.length;
const limit = Math.min(Math.max(1, input.limit ?? THREAD_READ_LIMIT), THREAD_LIST_MAX);
- let budget = THREAD_READ_BUDGET - notes.text.length;
+ const first = (end === -1 ? 0 : end) - 1;
+ const reserved = Math.min(THREAD_LINE_CHARS, lines[first]?.text?.trim().length ?? 0);
+ const shownNotes = cut(notes.text, THREAD_READ_BUDGET - reserved);
+ let budget = THREAD_READ_BUDGET - shownNotes.text.length;
const page: Array<{ messageId?: string; by: "person" | "bot"; at?: number; text: string; truncated: boolean }> = [];
- for (let index = (end === -1 ? 0 : end) - 1; index >= 0 && page.length < limit && budget > 0; index -= 1) {
+ for (let index = first; index >= 0 && page.length < limit && budget > 0; index -= 1) {
const line = lines[index]!;
const shown = cut(line.text!.trim(), Math.min(THREAD_LINE_CHARS, budget));
+ if (!shown.text) break;
budget -= shown.text.length;
page.push({ ...(line.id ? { messageId: line.id } : {}), by: line.role === "user" ? "person" : "bot", ...(line.at ? { at: line.at } : {}), ...shown });
}
const oldest = (end === -1 ? 0 : end) - page.length;
return {
thread: { threadId: thread.threadId, ...(thread.title ? { title: thread.title } : {}) },
- notes: notes.text, notesTruncated: notes.truncated,
+ notes: shownNotes.text, notesTruncated: notes.truncated || shownNotes.truncated,
messages: page,
earlier: oldest > 0 ? page.at(-1)?.messageId ?? null : null,
};