Skip to content

Commit ea7b7cf

Browse files
committed
perf(server): find in a long thread no longer re-parses every message on each keystroke
1 parent b38aa02 commit ea7b7cf

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/ThreadFind.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,28 @@ describe("V2 thread find", () => {
456456
}).pipe(Effect.provide(layerTest)),
457457
);
458458

459+
it.effect("reuses parses across queries in a thread of a few hundred messages", () =>
460+
Effect.gen(function* () {
461+
const projection = yield* setup;
462+
const total = 600;
463+
yield* putItems(
464+
Array.from({ length: total }, (_, i) => item(`long:${i}`, i + 1, `message ${i} needle`)),
465+
);
466+
const parse = vi.spyOn(ThreadFindText, "searchableMessageSegments");
467+
try {
468+
const first = yield* projection.searchThread({ threadId, query: "needle" });
469+
assert.equal(first.totalMatches, total);
470+
assert.equal(parse.mock.calls.length, total);
471+
parse.mockClear();
472+
const typed = yield* projection.searchThread({ threadId, query: "message 5" });
473+
assert.equal(typed.totalMatches, 111);
474+
assert.equal(parse.mock.calls.length, 0);
475+
} finally {
476+
parse.mockRestore();
477+
}
478+
}).pipe(Effect.provide(layerTest)),
479+
);
480+
459481
it.effect("searches visible rendered messages and plans in canonical item order", () =>
460482
Effect.gen(function* () {
461483
const projection = yield* setup;

‎apps/server/src/orchestration-v2/ThreadFind.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,10 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
263263
});
264264
});
265265
});
266+
// Each scan visits every message, so a cache smaller than the thread is cycled through and
267+
// never hits. Real threads reach a few thousand messages of a few hundred characters.
266268
const textCache = yield* Cache.make({
267-
capacity: 512,
269+
capacity: 8192,
268270
timeToLive: "1 minute",
269271
lookup: (key: TextKey) => Effect.sync(() => parseText(key)),
270272
});

0 commit comments

Comments
 (0)