Skip to content

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

fix(memory): retain short identifying terms in conversation search

Afloat33f06c6bcf81
diff --git a/server/thread-notes.ts b/server/thread-notes.ts
index b5760cdbab..5462c63d98 100644
--- a/server/thread-notes.ts
+++ b/server/thread-notes.ts
@@ -225,7 +225,7 @@ export function reachThreads(input: {
earlier: oldest > 0 ? page.at(-1)?.messageId ?? null : null,
};
}
- const words = (input.query ?? "").toLowerCase().split(/[^\p{L}\p{N}]+/u).filter((word) => word.length > 1);
+ const words = (input.query ?? "").toLowerCase().split(/[^\p{L}\p{N}]+/u).filter((word) => word.length > 0);
const found = others.flatMap((thread) => {
const notes = notesOf(thread.threadId).text;
const title = (thread.title ?? "").toLowerCase();
diff --git a/server/thread-search-short-terms.e2e.test.ts b/server/thread-search-short-terms.e2e.test.ts
new file mode 100644
index 0000000000..ade01c6743
--- /dev/null
+++ b/server/thread-search-short-terms.e2e.test.ts
@@ -0,0 +1,41 @@
+import { randomUUID } from "node:crypto";
+import { expect, it } from "vitest";
+
+import { launchVerificationServer, runControlOmb } from "../scripts/control-omb.ts";
+
+it("filters thread titles and notes by short identifying terms through the real search route", async () => {
+ const key = randomUUID();
+ const fixture = await launchVerificationServer({ ...process.env, OMB_TEST_INTERNAL_CAPABILITY_KEY: key });
+ const api = async (path: string, method = "GET", body?: unknown, headers: Record<string, string> = {}) => {
+ const response = await fetch(fixture.info.url + path, {
+ method, headers: { "content-type": "application/json", origin: fixture.info.url, ...headers },
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }), signal: AbortSignal.timeout(20_000),
+ });
+ expect(response.ok, `${method} ${path}: ${response.status}`).toBe(true);
+ return await response.json() as any;
+ };
+ try {
+ expect(await runControlOmb(["doctor", "--url", fixture.info.url])).toMatchObject({ ok: true });
+ const { bot } = await runControlOmb(["new-bot", "--name", "Search Counsel", "--url", fixture.info.url]) as any;
+ const ids: string[] = [];
+ for (const title of ["Contract 7", "Contract 8", "李先生的租约", "张先生的租约"]) {
+ const created = await api(`/api/bots/${bot.id}/tasks`, "POST", { title });
+ ids.push(String(created.task?.threadId ?? created.threadId));
+ }
+ await api(`/api/bots/${bot.id}/memory/thread/${ids[0]}`, "PUT", { text: "Agreed clause 7" });
+ await api(`/api/bots/${bot.id}/memory/thread/${ids[1]}`, "PUT", { text: "Agreed clause 8" });
+ const { token } = await api("/api/testing/internal-capability", "POST", {
+ botId: bot.id, threadId: bot.activeTaskId, kind: "agents",
+ }, { "x-openmausbot-test-capability": key });
+ const search = (query: string) => api(`/api/internal/session-search?${new URLSearchParams({
+ fromBotId: bot.id, fromThreadId: bot.activeTaskId, scope: "threads", q: query,
+ })}`, "GET", undefined, { authorization: `Bearer ${token}` });
+ expect(await search("Contract 7")).toMatchObject({ total: 1, threads: [{ threadId: ids[0] }] });
+ expect(await search("clause 7")).toMatchObject({ total: 1, threads: [{ threadId: ids[0] }] });
+ expect(await search("李")).toMatchObject({ total: 1, threads: [{ threadId: ids[2] }] });
+ expect(await search("Contract 9")).toMatchObject({ total: 0, threads: [] });
+ expect(await search("")).toMatchObject({ total: 4 });
+ } finally {
+ await fixture.close();
+ }
+}, 90_000);
diff --git a/server/thread-search-short-terms.test.ts b/server/thread-search-short-terms.test.ts
new file mode 100644
index 0000000000..38e57f0a18
--- /dev/null
+++ b/server/thread-search-short-terms.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, it } from "vitest";
+
+import { writeMemoryDoc, threadNotesPath } from "./memory-store.ts";
+import { reachThreads } from "./thread-notes.ts";
+import { ensureWorkspace } from "./workspace.ts";
+
+let nextBot = 0;
+function fixture(titles: string[], notes: string[] = []) {
+ const botId = `short-search-${process.pid}-${nextBot++}`;
+ ensureWorkspace(botId, "en");
+ const threads = titles.map((title, index) => ({ threadId: `matter-${index}`, title }));
+ notes.forEach((text, index) => writeMemoryDoc(botId, threadNotesPath(threads[index]!.threadId), text));
+ return (query?: string) => reachThreads({ botId, current: "here", threads, query, messages: () => [] });
+}
+
+describe("short terms in conversation search", () => {
+ it("uses a single digit to distinguish otherwise identical matter names", () => {
+ const search = fixture(["Contract 7", "Contract 8"]);
+ expect(search("Contract 7")).toMatchObject({ total: 1, threads: [{ threadId: "matter-0" }] });
+ expect(search("Contract 9")).toEqual({ total: 0, threads: [] });
+ });
+
+ it("matches a single-character surname without returning every conversation", () => {
+ const search = fixture(["李先生的租约", "张先生的租约"]);
+ expect(search("李")).toMatchObject({ total: 1, threads: [{ threadId: "matter-0" }] });
+ expect(search("王")).toEqual({ total: 0, threads: [] });
+ });
+
+ it("applies every term when the identifying number occurs only in notes", () => {
+ const search = fixture(["Acme NDA", "Other NDA"], ["Agreed clause 7", "Agreed clause 8"]);
+ expect(search("clause 7")).toMatchObject({ total: 1, threads: [{ threadId: "matter-0" }] });
+ });
+
+ it("retains the unfiltered listing for an omitted or blank query", () => {
+ const search = fixture(["Contract A", "Contract B"]);
+ expect(search()).toMatchObject({ total: 2 });
+ expect(search(" ")).toMatchObject({ total: 2 });
+ });
+});