Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import {
3 EnvironmentId,
4 EventId,
5 MessageId,
6 ProjectId,
7 ProviderInstanceId,
8 ThreadId,
9 type OrchestrationV2DomainEvent,
10 type ThreadLinkedPullRequest,
11} from "@t3tools/contracts";
12import * as NodeServices from "@effect/platform-node/NodeServices";
13import * as DateTime from "effect/DateTime";
14import * as Deferred from "effect/Deferred";
15import * as Effect from "effect/Effect";
16import * as TestClock from "effect/testing/TestClock";
17import * as FileSystem from "effect/FileSystem";
18import * as Fiber from "effect/Fiber";
19import * as Layer from "effect/Layer";
20import * as Path from "effect/Path";
21import * as SqlClient from "effect/sql/SqlClient";
22import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
23import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";
24
25import * as SqlitePersistence from "../persistence/Sqlite.ts";
26import { rebuildMessageSearch } from "./messageSearchIndex.ts";
27import * as ProjectionStore from "./ProjectionStore.ts";
28import * as ProjectStore from "./ProjectStore.ts";
29import * as ThreadSearch from "./ThreadSearch.ts";
30
31const layerTest = Layer.mergeAll(
32 ThreadSearch.layer,
33 ProjectionStore.layer,
34 ProjectStore.layer,
35).pipe(Layer.provideMerge(SqlitePersistence.layerMemory));
36
37const providerInstanceId = ProviderInstanceId.make("codex");
38const at = (minute: number) => DateTime.makeUnsafe(Date.UTC(2026, 8, 27, 0, minute));
39
40const createProject = (projectId: ProjectId) =>
41 Effect.flatMap(ProjectStore.ProjectStoreV2, (projects) =>
42 projects.apply({
43 sequence: 0,
44 eventId: EventId.make(`created:${projectId}`),
45 aggregateKind: "project",
46 aggregateId: projectId,
47 occurredAt: DateTime.formatIso(at(0)),
48 commandId: null,
49 causationEventId: null,
50 correlationId: null,
51 metadata: {},
52 type: "project.created",
53 payload: {
54 projectId,
55 title: projectId,
56 workspaceRoot: `/work/${projectId}`,
57 defaultModelSelection: null,
58 scripts: [],
59 createdAt: DateTime.formatIso(at(0)),
60 updatedAt: DateTime.formatIso(at(0)),
61 },
62 }),
63 );
64
65const thread = (
66 threadId: ThreadId,
67 projectId: ProjectId,
68 overrides: {
69 readonly archivedAt?: DateTime.Utc;
70 readonly deletedAt?: DateTime.Utc;
71 readonly subagentOf?: ThreadId;
72 readonly sideChatOf?: ThreadId;
73 readonly title?: string;
74 readonly settledAt?: DateTime.Utc;
75 readonly linkedPullRequest?: ThreadLinkedPullRequest;
76 readonly branchPullRequest?: ThreadLinkedPullRequest;
77 } = {},
78): OrchestrationV2DomainEvent => ({
79 id: EventId.make(`created:${threadId}`),
80 type: "thread.created",
81 threadId,
82 providerInstanceId,
83 occurredAt: at(0),
84 payload: {
85 createdBy: "user",
86 creationSource: "web",
87 id: threadId,
88 projectId,
89 title: overrides.title ?? threadId,
90 providerInstanceId,
91 modelSelection: { instanceId: providerInstanceId, model: "gpt-5" },
92 runtimeMode: "full-access",
93 interactionMode: "default",
94 branch: null,
95 worktreePath: null,
96 linkedPullRequest: overrides.linkedPullRequest ?? null,
97 branchPullRequest: overrides.branchPullRequest ?? null,
98 activeProviderThreadId: null,
99 lineage: overrides.subagentOf
100 ? {
101 parentThreadId: overrides.subagentOf,
102 relationshipToParent: "subagent",
103 rootThreadId: overrides.subagentOf,
104 }
105 : overrides.sideChatOf
106 ? {
107 parentThreadId: overrides.sideChatOf,
108 relationshipToParent: "fork",
109 rootThreadId: overrides.sideChatOf,
110 sideChat: true,
111 }
112 : { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
113 forkedFrom: null,
114 createdAt: at(0),
115 updatedAt: at(0),
116 archivedAt: overrides.archivedAt ?? null,
117 settledOverride: overrides.settledAt === undefined ? null : "settled",
118 settledAt: overrides.settledAt ?? null,
119 lastVisitedAt: null,
120 deletedAt: overrides.deletedAt ?? null,
121 },
122});
123
124const message = (
125 threadId: ThreadId,
126 id: string,
127 role: "user" | "assistant" | "system",
128 text: string,
129 options: {
130 readonly minute?: number;
131 readonly streaming?: boolean;
132 readonly createdBy?: "user" | "agent";
133 } = {},
134): OrchestrationV2DomainEvent => ({
135 id: EventId.make(`message:${id}`),
136 type: "message.updated",
137 threadId,
138 providerInstanceId,
139 occurredAt: at(options.minute ?? 1),
140 payload: {
141 createdBy: options.createdBy ?? (role === "user" ? "user" : "agent"),
142 creationSource: role === "user" ? "web" : "provider",
143 id: MessageId.make(id),
144 threadId,
145 runId: null,
146 nodeId: null,
147 role,
148 text,
149 attachments: [],
150 streaming: options.streaming ?? false,
151 createdAt: at(options.minute ?? 1),
152 updatedAt: at(options.minute ?? 1),
153 },
154});
155
156// The prefix and suffix are hidden selection context, not text the reader sees.
157const quote = serializeAssistantCitation({
158 version: 1,
159 environmentId: EnvironmentId.make("environment:local"),
160 threadId: ThreadId.make("thread:quoted"),
161 messageId: MessageId.make("quoted-answer"),
162 text: "A lighthouse guides ships safely through the darkness.",
163 comment: "what are you talking about",
164 start: 0,
165 end: 54,
166 prefix: "beacon01",
167 suffix: "harbor",
168});
169
170it.effect.each([true, false])(
171 "ThreadSearch excludes bootstrap text before the hit and candidate limits (indexed: %s)",
172 (indexed) =>
173 Effect.gen(function* () {
174 const projections = yield* ProjectionStore.ProjectionStoreV2;
175 const search = yield* ThreadSearch.ThreadSearch;
176 const sql = yield* SqlClient.SqlClient;
177 const project = ProjectId.make("project:attachments");
178 const threadId = ThreadId.make("thread:attachments");
179 yield* createProject(project);
180 yield* Effect.forEach(
181 [
182 thread(threadId, project),
183 message(threadId, "ordinary", "user", "Review the attached files in context", {
184 minute: 1,
185 }),
186 message(threadId, "attachment", "user", ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, { minute: 2 }),
187 message(
188 threadId,
189 "ultrathink",
190 "user",
191 `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`,
192 { minute: 3 },
193 ),
194 ],
195 projections.apply,
196 { discard: true },
197 );
198 if (!indexed) {
199 yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`;
200 yield* sql`DROP TRIGGER orchestration_v2_message_search_update`;
201 yield* sql`DROP TRIGGER orchestration_v2_message_search_delete`;
202 yield* sql`DROP TABLE orchestration_v2_message_search`;
203 }
204 assert.deepEqual((yield* search.search({ query: "conversation context" })).matches, []);
205 assert.deepEqual((yield* search.search({ query: "ultrathink" })).matches, []);
206 assert.deepEqual(
207 (yield* search.search({ query: "attached files" })).matches.map((match) => [
208 match.messageId,
209 match.snippet,
210 ]),
211 [[MessageId.make("ordinary"), "Review the attached files in context"]],
212 );
213 for (const text of [
214 ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
215 `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`,
216 `uLtRaThInK:\r\n\t\u00a0${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`,
217 ]) {
218 yield* sql`
219 WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${indexed ? 10_000 : 2000})
220 INSERT INTO orchestration_v2_projection_messages
221 (message_id, thread_id, role, streaming, created_at, updated_at, payload_json)
222 SELECT 'attachment-limit:' || i, ${threadId}, 'user', 0,
223 ${DateTime.formatIso(at(5))}, ${DateTime.formatIso(at(5))}, json_object('text', ${text})
224 FROM n
225 `;
226 assert.deepEqual(
227 (yield* search.search({ query: "context" })).matches.map((match) => match.messageId),
228 [MessageId.make("ordinary")],
229 );
230 yield* sql`
231 DELETE FROM orchestration_v2_projection_messages WHERE message_id LIKE 'attachment-limit:%'
232 `;
233 }
234 }).pipe(Effect.provide(layerTest)),
235);
236
237it.effect("ThreadSearch excludes legacy placeholders still in the index", () =>
238 Effect.gen(function* () {
239 const projections = yield* ProjectionStore.ProjectionStoreV2;
240 const search = yield* ThreadSearch.ThreadSearch;
241 const sql = yield* SqlClient.SqlClient;
242 const project = ProjectId.make("project:legacy-attachments");
243 const threadId = ThreadId.make("thread:legacy-attachments");
244 yield* createProject(project);
245 // Simulate the pre-upgrade trigger, which indexed attachment placeholders.
246 yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`;
247 yield* sql`
248 CREATE TRIGGER orchestration_v2_message_search_insert
249 AFTER INSERT ON orchestration_v2_projection_messages
250 BEGIN
251 INSERT INTO orchestration_v2_message_search (rowid, text)
252 VALUES (new.rowid, json_extract(new.payload_json, '$.text'));
253 END
254 `;
255 yield* Effect.forEach(
256 [
257 thread(threadId, project),
258 message(threadId, "ordinary", "user", "Review the attached files in context"),
259 message(threadId, "attachment", "user", ATTACHMENT_ONLY_BOOTSTRAP_PROMPT),
260 message(
261 threadId,
262 "ultrathink",
263 "user",
264 `uLtRaThInK:\r\n\t\u00a0${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`,
265 ),
266 ],
267 projections.apply,
268 { discard: true },
269 );
270 assert.lengthOf(
271 yield* sql`SELECT rowid FROM orchestration_v2_message_search WHERE orchestration_v2_message_search MATCH '"context"'`,
272 3,
273 );
274 assert.deepEqual((yield* search.search({ query: "conversation context" })).matches, []);
275 assert.deepEqual((yield* search.search({ query: "ultrathink" })).matches, []);
276 assert.deepEqual(
277 (yield* search.search({ query: "context" })).matches.map((match) => match.messageId),
278 [MessageId.make("ordinary")],
279 );
280 }).pipe(Effect.provide(layerTest)),
281);
282
283it.layer(layerTest)("ThreadSearch", (it) => {
284 it.effect("reads the newest hits without sorting all of them", () =>
285 Effect.gen(function* () {
286 const sql = yield* SqlClient.SqlClient;
287 for (const match of ['"needle"', null]) {
288 const plan = yield* sql<{
289 readonly detail: string;
290 }>`EXPLAIN QUERY PLAN ${ThreadSearch.messageHitsQuery(sql, { match, substrings: "[]", shortSpellings: "[]", projectId: null, excludedThreadIds: "[]" })}`;
291 assert.isFalse(
292 plan.some((row) => row.detail.includes("TEMP B-TREE")),
293 plan.map((row) => row.detail).join("\n"),
294 );
295 if (match !== null) {
296 assert.lengthOf(
297 plan.filter((row) =>
298 row.detail.startsWith("SEARCH messages USING INTEGER PRIMARY KEY"),
299 ),
300 1,
301 plan.map((row) => row.detail).join("\n"),
302 );
303 }
304 }
305 }),
306 );
307
308 it.effect("returns one finished user or assistant match per active thread", () =>
309 Effect.gen(function* () {
310 const projections = yield* ProjectionStore.ProjectionStoreV2;
311 const search = yield* ThreadSearch.ThreadSearch;
312 const project = ProjectId.make("project:search");
313 const deletedProject = ProjectId.make("project:search-deleted");
314 yield* createProject(project);
315 yield* createProject(deletedProject);
316 yield* Effect.flatMap(ProjectStore.ProjectStoreV2, (projects) =>
317 projects.apply({
318 sequence: 0,
319 eventId: EventId.make("deleted:project"),
320 aggregateKind: "project",
321 aggregateId: deletedProject,
322 occurredAt: DateTime.formatIso(at(2)),
323 commandId: null,
324 causationEventId: null,
325 correlationId: null,
326 metadata: {},
327 type: "project.deleted",
328 payload: { projectId: deletedProject, deletedAt: DateTime.formatIso(at(2)) },
329 }),
330 );
331
332 const both = ThreadId.make("thread:both");
333 const assistantOnly = ThreadId.make("thread:assistant");
334 const events: ReadonlyArray<OrchestrationV2DomainEvent> = [
335 thread(both, project),
336 message(both, "both-assistant", "assistant", "needle from the answer", { minute: 3 }),
337 message(both, "both-user-old", "user", "older needle question", { minute: 1 }),
338 message(both, "both-user-new", "user", "newer needle question", { minute: 2 }),
339 thread(assistantOnly, project),
340 message(assistantOnly, "assistant-only", "assistant", "needle in an answer"),
341 message(assistantOnly, "assistant-streaming", "user", "needle still typing", {
342 streaming: true,
343 }),
344 message(assistantOnly, "assistant-system", "system", "needle system prompt"),
345 thread(ThreadId.make("thread:archived"), project, { archivedAt: at(1) }),
346 message(ThreadId.make("thread:archived"), "archived", "user", "needle archived"),
347 thread(ThreadId.make("thread:deleted"), project, { deletedAt: at(1) }),
348 message(ThreadId.make("thread:deleted"), "deleted", "user", "needle deleted"),
349 thread(ThreadId.make("thread:orphaned"), deletedProject),
350 message(ThreadId.make("thread:orphaned"), "orphaned", "user", "needle orphaned"),
351 ];
352 yield* Effect.forEach(events, projections.apply, { discard: true });
353
354 const result = yield* search.search({ query: "NEEDLE", limit: 20 });
355 assert.deepEqual(
356 result.matches
357 .map((match) => [match.threadId, match.source, match.snippet, match.messageId])
358 .toSorted(([left], [right]) => String(left).localeCompare(String(right))),
359 [
360 [assistantOnly, "assistant", "needle in an answer", "assistant-only"],
361 [both, "user", "newer needle question", "both-user-new"],
362 ],
363 );
364 assert.lengthOf((yield* search.search({ query: "needle", limit: 1 })).matches, 1);
365 assert.deepEqual((yield* search.search({ query: "ne%le" })).matches, []);
366 }),
367 );
368
369 it.effect("matches the readable text of quotes and their comments", () =>
370 Effect.gen(function* () {
371 const projections = yield* ProjectionStore.ProjectionStoreV2;
372 const search = yield* ThreadSearch.ThreadSearch;
373 const project = ProjectId.make("project:search-citation");
374 const threadId = ThreadId.make("thread:citation");
375 yield* createProject(project);
376 yield* Effect.forEach(
377 [
378 thread(threadId, project),
379 message(threadId, "citation", "user", `${quote} quiet silver lantern`),
380 ],
381 projections.apply,
382 { discard: true },
383 );
384 const snippets = (query: string) =>
385 Effect.map(search.search({ query }), (result) =>
386 result.matches
387 .filter((match) => match.threadId === threadId)
388 .map((match) => match.snippet),
389 );
390
391 const readable =
392 "A lighthouse guides ships safely through the darkness. Comment: what are you talking about quiet silver lantern";
393 assert.deepEqual(yield* snippets("what are you talking about"), [
394 `…${readable.slice(readable.indexOf("the darkness"))}`,
395 ]);
396 assert.deepEqual(yield* snippets("ships safely through"), [readable]);
397 assert.deepEqual(yield* snippets("beacon01"), []);
398 assert.deepEqual(yield* snippets("harbor"), []);
399
400 yield* projections.apply(message(threadId, "citation", "user", "quiet silver lantern"));
401 assert.deepEqual(yield* snippets("what are you talking about"), []);
402 assert.deepEqual(yield* snippets("lantern"), ["quiet silver lantern"]);
403 }),
404 );
405
406 it.effect("finds accented, CJK, and apostrophe words of a quote through the index", () =>
407 Effect.gen(function* () {
408 const sql = yield* SqlClient.SqlClient;
409 const projections = yield* ProjectionStore.ProjectionStoreV2;
410 const search = yield* ThreadSearch.ThreadSearch;
411 const project = ProjectId.make("project:search-citation-index");
412 const threadId = ThreadId.make("thread:citation-index");
413 // The stored link percent-encodes these words; the index must hold them as they read.
414 const encodedQuote = serializeAssistantCitation({
415 version: 1,
416 environmentId: EnvironmentId.make("environment:local"),
417 threadId: ThreadId.make("thread:quoted"),
418 messageId: MessageId.make("quoted-answer"),
419 text: "The kittiwake's résumé mentions 灯台の光 twice.",
420 comment: "naïve question в логах",
421 start: 0,
422 end: 44,
423 prefix: "",
424 suffix: "",
425 });
426 yield* createProject(project);
427 yield* Effect.forEach(
428 [thread(threadId, project), message(threadId, "citation-index", "user", encodedQuote)],
429 projections.apply,
430 { discard: true },
431 );
432 const index = yield* sql`
433 SELECT 1 FROM sqlite_master WHERE name = 'orchestration_v2_message_search'
434 `;
435 assert.lengthOf(index, 1);
436 const threadIds = (query: string) =>
437 search.search({ query }).pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
438
439 for (const query of [
440 "kittiwake's",
441 "kittiwake’s",
442 "résumé",
443 "灯台の光",
444 "naïve",
445 "логах в",
446 ]) {
447 assert.deepEqual(yield* threadIds(query), [threadId], query);
448 }
449 // `t3 doctor` rebuilds the index after VACUUM; quotes must still read.
450 yield* rebuildMessageSearch(sql);
451 assert.deepEqual(yield* threadIds("résumé"), [threadId]);
452
453 yield* projections.apply(message(threadId, "citation-index", "user", "plain words"));
454 assert.deepEqual(yield* threadIds("résumé"), []);
455 assert.deepEqual(yield* threadIds("plain"), [threadId]);
456 }),
457 );
458
459 it.effect("indexes a message when it finishes streaming and follows later edits", () =>
460 Effect.gen(function* () {
461 const projections = yield* ProjectionStore.ProjectionStoreV2;
462 const search = yield* ThreadSearch.ThreadSearch;
463 const project = ProjectId.make("project:search-sync");
464 const threadId = ThreadId.make("thread:sync");
465 yield* createProject(project);
466 yield* projections.apply(thread(threadId, project));
467 const threadIds = (query: string) =>
468 search.search({ query }).pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
469
470 yield* projections.apply(
471 message(threadId, "sync", "assistant", "streaming draft", { streaming: true }),
472 );
473 assert.deepEqual(yield* threadIds("draft"), []);
474
475 yield* projections.apply(message(threadId, "sync", "assistant", "finished answer"));
476 assert.deepEqual(yield* threadIds("finished"), [threadId]);
477
478 yield* projections.apply(message(threadId, "sync", "assistant", "edited reply"));
479 assert.deepEqual(yield* threadIds("finished"), []);
480 assert.deepEqual(yield* threadIds("edited"), [threadId]);
481 }),
482 );
483
484 it.effect("matches inside identifiers and skips hidden threads and short tokens", () =>
485 Effect.gen(function* () {
486 const projections = yield* ProjectionStore.ProjectionStoreV2;
487 const search = yield* ThreadSearch.ThreadSearch;
488 const project = ProjectId.make("project:search-tokens");
489 const parent = ThreadId.make("thread:tokens-parent");
490 const subagent = ThreadId.make("thread:tokens-subagent");
491 const sideChat = ThreadId.make("thread:tokens-side-chat");
492 yield* createProject(project);
493 yield* Effect.forEach(
494 [
495 thread(parent, project),
496 message(parent, "parent", "user", "Rename ThreadSearchService before shipping"),
497 thread(subagent, project, { subagentOf: parent }),
498 message(subagent, "subagent", "assistant", "ThreadSearchService renamed"),
499 thread(sideChat, project, { sideChatOf: parent, title: "ThreadSearchService question" }),
500 message(sideChat, "side-chat", "user", "Why rename ThreadSearchService?"),
501 ],
502 projections.apply,
503 { discard: true },
504 );
505
506 const result = yield* search.search({ query: "search servi" });
507 assert.deepEqual(
508 result.matches.map((match) => [match.threadId, match.snippet]),
509 [[parent, "Rename ThreadSearchService before shipping"]],
510 );
511 // "ab" alone cannot use the index; with a longer token it narrows the hits.
512 assert.deepEqual((yield* search.search({ query: "ab" })).matches, []);
513 assert.lengthOf((yield* search.search({ query: "renam be" })).matches, 1);
514 assert.lengthOf((yield* search.search({ query: "renam ab" })).matches, 0);
515 }),
516 );
517
518 it.effect("indexes neither subagent messages nor rewrites that keep the text", () =>
519 Effect.gen(function* () {
520 const sql = yield* SqlClient.SqlClient;
521 const projections = yield* ProjectionStore.ProjectionStoreV2;
522 const project = ProjectId.make("project:search-index-writes");
523 const parent = ThreadId.make("thread:index-parent");
524 const subagent = ThreadId.make("thread:index-subagent");
525 yield* createProject(project);
526 yield* Effect.forEach(
527 [
528 thread(parent, project),
529 message(parent, "index-parent", "assistant", "parent answer"),
530 thread(subagent, project, { subagentOf: parent }),
531 message(subagent, "index-subagent", "assistant", "subagent answer"),
532 ],
533 projections.apply,
534 { discard: true },
535 );
536 const indexedThreads = sql<{ readonly threadId: string }>`
537 SELECT messages.thread_id AS "threadId"
538 FROM orchestration_v2_message_search AS search
539 INNER JOIN orchestration_v2_projection_messages AS messages
540 ON messages.rowid = search.rowid
541 WHERE messages.thread_id IN (${parent}, ${subagent})
542 `.pipe(Effect.map((rows) => rows.map((row) => row.threadId)));
543 assert.deepEqual(yield* indexedThreads, [parent]);
544
545 // total_changes counts trigger writes, so 1 means the index was untouched.
546 const changes = sql<{ readonly n: number }>`SELECT total_changes() AS n`.pipe(
547 Effect.map(([row]) => row?.n ?? 0),
548 );
549 const before = yield* changes;
550 yield* sql`
551 UPDATE orchestration_v2_projection_messages
552 SET payload_json = json_set(payload_json, '$.updatedAt', '2026-09-27T01:00:00.000Z')
553 WHERE message_id = 'index-parent'
554 `;
555 assert.strictEqual((yield* changes) - before, 1);
556 assert.deepEqual(yield* indexedThreads, [parent]);
557 }),
558 );
559});
560
561it.layer(layerTest)("ThreadSearch filters before it ranks", (it) => {
562 it.effect("orders message matches by when they were written, not by row order", () =>
563 Effect.gen(function* () {
564 const projections = yield* ProjectionStore.ProjectionStoreV2;
565 const search = yield* ThreadSearch.ThreadSearch;
566 const project = ProjectId.make("project:written");
567 yield* createProject(project);
568 const recent = ThreadId.make("thread:written-recent");
569 const imported = ThreadId.make("thread:written-imported");
570 // An imported history lands after newer messages in row order.
571 yield* Effect.forEach(
572 [
573 thread(recent, project),
574 message(recent, "written-recent", "user", "kestrel deploy notes", { minute: 30 }),
575 thread(imported, project),
576 message(imported, "written-imported", "user", "kestrel deploy notes", { minute: 1 }),
577 ],
578 projections.apply,
579 { discard: true },
580 );
581 const { matches } = yield* search.search({ query: "kestrel" });
582 assert.deepEqual(
583 matches.map((match) => match.threadId),
584 [recent, imported],
585 );
586 }),
587 );
588
589 it.effect("ranks a thread about the word above a passing mention", () =>
590 Effect.gen(function* () {
591 const projections = yield* ProjectionStore.ProjectionStoreV2;
592 const search = yield* ThreadSearch.ThreadSearch;
593 const project = ProjectId.make("project:about");
594 yield* createProject(project);
595 const about = ThreadId.make("thread:about-osprey");
596 const mention = ThreadId.make("thread:mention-osprey");
597 yield* Effect.forEach(
598 [
599 thread(about, project),
600 ...Array.from({ length: 12 }, (_, index) =>
601 message(about, `about-osprey-${index}`, "assistant", `osprey step ${index}`, {
602 minute: 2,
603 }),
604 ),
605 thread(mention, project),
606 message(mention, "mention-osprey", "user", "also check osprey", { minute: 5 }),
607 ],
608 projections.apply,
609 { discard: true },
610 );
611 const { matches } = yield* search.search({ query: "osprey" });
612 assert.deepEqual(
613 matches.map((match) => [match.threadId, match.hits]),
614 [
615 [about, 12],
616 [mention, 1],
617 ],
618 );
619 }),
620 );
621
622 it.effect("counts a thread's hits at its best match only", () =>
623 Effect.gen(function* () {
624 const projections = yield* ProjectionStore.ProjectionStoreV2;
625 const search = yield* ThreadSearch.ThreadSearch;
626 const project = ProjectId.make("project:best-hits");
627 yield* createProject(project);
628 const clarks = ThreadId.make("thread:clarks");
629 const larks = ThreadId.make("thread:lark-many");
630 yield* Effect.forEach(
631 [
632 thread(clarks, project),
633 message(clarks, "clarks-lark", "user", "Lark once", { minute: 3 }),
634 ...Array.from({ length: 31 }, (_, index) =>
635 message(clarks, `clarks-${index}`, "assistant", "Clark the plan", { minute: 3 }),
636 ),
637 thread(larks, project),
638 ...Array.from({ length: 8 }, (_, index) =>
639 message(larks, `lark-many-${index}`, "assistant", "Lark again", { minute: 3 }),
640 ),
641 ],
642 projections.apply,
643 { discard: true },
644 );
645 const { matches } = yield* search.search({ query: "lark" });
646 assert.deepEqual(
647 matches.map((match) => [match.threadId, match.hits]),
648 [
649 [larks, 8],
650 [clarks, 1],
651 ],
652 );
653 }),
654 );
655
656 it.effect("finds a visible match behind hundreds of hidden or narrower hits", () =>
657 Effect.gen(function* () {
658 const projections = yield* ProjectionStore.ProjectionStoreV2;
659 const search = yield* ThreadSearch.ThreadSearch;
660 const project = ProjectId.make("project:crowded");
661 const parent = ThreadId.make("thread:crowded-parent");
662 const subagent = ThreadId.make("thread:crowded-subagent");
663 const older = ThreadId.make("thread:crowded-older");
664 yield* createProject(project);
665 yield* Effect.forEach(
666 [
667 thread(parent, project),
668 thread(subagent, project, { subagentOf: parent }),
669 thread(older, project),
670 ...Array.from({ length: 450 }, (_, index) =>
671 message(subagent, `subagent-${index}`, "assistant", "needle needle"),
672 ),
673 ...Array.from({ length: 450 }, (_, index) =>
674 message(older, `older-${index}`, "assistant", "pnpm 11 pnpm"),
675 ),
676 message(parent, "parent-needle", "user", "a needle in a long haystack of other words"),
677 // Agent-written notices in user messages are not indexed, so they never read as "You".
678 message(older, "older-notice", "user", "needle update from a watched pull request", {
679 createdBy: "agent",
680 }),
681 message(parent, "parent-pnpm", "user", "moved the repo to pnpm 12 today"),
682 ],
683 projections.apply,
684 { discard: true },
685 );
686
687 const threadIds = (query: string) =>
688 search.search({ query }).pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
689 assert.deepEqual(yield* threadIds("needle"), [parent]);
690 assert.deepEqual(yield* threadIds("pnpm 12"), [parent]);
691 }),
692 );
693
694 it.effect("keeps a thread picked for the query when the limit cuts", () =>
695 Effect.gen(function* () {
696 const projections = yield* ProjectionStore.ProjectionStoreV2;
697 const search = yield* ThreadSearch.ThreadSearch;
698 const sql = yield* SqlClient.SqlClient;
699 const project = ProjectId.make("project:plover");
700 const busy = ThreadId.make("thread:plover-busy");
701 const picked = ThreadId.make("thread:plover-picked");
702 yield* createProject(project);
703 yield* Effect.forEach(
704 [
705 thread(busy, project),
706 message(busy, "plover-1", "user", "plover notes", { minute: 3 }),
707 message(busy, "plover-2", "user", "more plover notes", { minute: 4 }),
708 thread(picked, project),
709 message(picked, "plover-old", "user", "one plover mention", { minute: 1 }),
710 ],
711 projections.apply,
712 { discard: true },
713 );
714 const threadIds = () =>
715 search
716 .search({ query: "plover", limit: 1 })
717 .pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
718 assert.deepEqual(yield* threadIds(), [busy]);
719 // A pick at time 0 would read as no pick.
720 yield* TestClock.adjust("1 day");
721 const at = DateTime.toEpochMillis(yield* DateTime.now);
722 yield* sql`
723 UPDATE orchestration_v2_projection_threads
724 SET payload_json = json_set(payload_json, '$.opens', json(${JSON.stringify({
725 openedAt: at,
726 score: 1,
727 picks: [{ query: "plover", at }],
728 })}))
729 WHERE thread_id = ${picked}
730 `;
731 assert.deepEqual(yield* threadIds(), [picked]);
732 }),
733 );
734
735 it.effect("finds a curly apostrophe for a straight one and back", () =>
736 Effect.gen(function* () {
737 const projections = yield* ProjectionStore.ProjectionStoreV2;
738 const search = yield* ThreadSearch.ThreadSearch;
739 const project = ProjectId.make("project:apostrophes");
740 const curly = ThreadId.make("thread:apostrophe-curly");
741 const straight = ThreadId.make("thread:apostrophe-straight");
742 yield* createProject(project);
743 yield* Effect.forEach(
744 [
745 thread(curly, project),
746 message(curly, "apostrophe-curly", "user", "the gannet\u2019s nest"),
747 thread(straight, project),
748 message(straight, "apostrophe-straight", "user", "a kittiwake's nest"),
749 ],
750 projections.apply,
751 { discard: true },
752 );
753 const threadIds = (query: string) =>
754 search.search({ query }).pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
755 assert.deepEqual(yield* threadIds("gannet's"), [curly]);
756 assert.deepEqual(yield* threadIds("kittiwake\u2019s"), [straight]);
757 }),
758 );
759
760 it.effect("leaves out answers to an agent's question, which show in its card", () =>
761 Effect.gen(function* () {
762 const projections = yield* ProjectionStore.ProjectionStoreV2;
763 const search = yield* ThreadSearch.ThreadSearch;
764 const project = ProjectId.make("project:answers");
765 const asked = ThreadId.make("thread:answers");
766 yield* createProject(project);
767 yield* Effect.forEach(
768 [
769 thread(asked, project),
770 message(asked, "async-answer:request-1", "user", "use the albatross drive"),
771 ],
772 projections.apply,
773 { discard: true },
774 );
775 assert.deepEqual((yield* search.search({ query: "albatross" })).matches, []);
776 }),
777 );
778
779 it.effect("matches messages as they read, with links as their text", () =>
780 Effect.gen(function* () {
781 const projections = yield* ProjectionStore.ProjectionStoreV2;
782 const search = yield* ThreadSearch.ThreadSearch;
783 const project = ProjectId.make("project:heron");
784 const labeled = ThreadId.make("thread:heron-label");
785 const addressOnly = ThreadId.make("thread:heron-address");
786 yield* createProject(project);
787 yield* Effect.forEach(
788 [
789 thread(labeled, project),
790 message(labeled, "heron-label", "assistant", "See [the heron plan](https://x.dev/a) now"),
791 thread(addressOnly, project),
792 message(addressOnly, "heron-address", "user", "Read [this](https://heron.dev/b)"),
793 ],
794 projections.apply,
795 { discard: true },
796 );
797 const { matches } = yield* search.search({ query: "heron" });
798 assert.deepEqual(
799 matches.map((match) => [match.threadId, match.snippet]),
800 [[labeled, "See the heron plan now"]],
801 );
802 }),
803 );
804
805 it.effect("reads at most the newest 10,000 index hits, however many the filters drop", () =>
806 Effect.gen(function* () {
807 const projections = yield* ProjectionStore.ProjectionStoreV2;
808 const search = yield* ThreadSearch.ThreadSearch;
809 const sql = yield* SqlClient.SqlClient;
810 const project = ProjectId.make("project:kestrel");
811 const old = ThreadId.make("thread:kestrel-old");
812 const busy = ThreadId.make("thread:kestrel-busy");
813 yield* createProject(project);
814 yield* Effect.forEach(
815 [
816 thread(old, project),
817 message(old, "kestrel-old", "user", "kestrel ci is red"),
818 thread(busy, project),
819 ],
820 projections.apply,
821 { discard: true },
822 );
823 const insertBusy = (count: number) => sql`
824 WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${count})
825 INSERT INTO orchestration_v2_projection_messages
826 (message_id, thread_id, role, streaming, created_at, updated_at, payload_json)
827 SELECT 'kestrel-busy-' || i, ${busy}, 'assistant', 0, ${DateTime.formatIso(at(5))},
828 ${DateTime.formatIso(at(5))}, json_object('text', 'kestrel packages')
829 FROM n
830 `;
831 const threadIds = (query: string) =>
832 search.search({ query }).pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
833 yield* insertBusy(9_999);
834 assert.deepEqual(yield* threadIds("kestrel ci"), [old]);
835 yield* sql`DELETE FROM orchestration_v2_projection_messages WHERE thread_id = ${busy}`;
836 yield* insertBusy(10_000);
837 assert.deepEqual(yield* threadIds("kestrel ci"), []);
838 }),
839 );
840});
841
842it.effect.each([true, false])(
843 "ThreadSearch narrows Cyrillic and accent-folded Latin short words before the hit limit (indexed: %s)",
844 (indexed) =>
845 Effect.gen(function* () {
846 const projections = yield* ProjectionStore.ProjectionStoreV2;
847 const search = yield* ThreadSearch.ThreadSearch;
848 const sql = yield* SqlClient.SqlClient;
849 const project = ProjectId.make("project:short-limit");
850 const older = ThreadId.make("thread:short-older");
851 const olderFrench = ThreadId.make("thread:short-older-french");
852 const newer = ThreadId.make("thread:short-newer");
853 yield* createProject(project);
854 yield* Effect.forEach(
855 [
856 thread(older, project),
857 message(older, "older", "user", "В логах ошибка"),
858 thread(olderFrench, project),
859 message(olderFrench, "older-french", "user", "École fermée à Lyon"),
860 thread(newer, project),
861 ],
862 projections.apply,
863 { discard: true },
864 );
865 if (!indexed) {
866 yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`;
867 yield* sql`DROP TRIGGER orchestration_v2_message_search_update`;
868 yield* sql`DROP TRIGGER orchestration_v2_message_search_delete`;
869 yield* sql`DROP TABLE orchestration_v2_message_search`;
870 }
871 // More newer hits for the long word than one read holds, none with the short one.
872 yield* sql`
873 WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 2000)
874 INSERT INTO orchestration_v2_projection_messages
875 (message_id, thread_id, role, streaming, created_at, updated_at, payload_json)
876 SELECT 'short-newer-' || i || '-' || word, ${newer}, 'assistant', 0,
877 ${DateTime.formatIso(at(5))}, ${DateTime.formatIso(at(5))}, json_object('text', text)
878 FROM n, (SELECT 'ru' AS word, 'ошибка здесь' AS text UNION ALL SELECT 'fr', 'fermée ici')
879 `;
880 const threadIds = (query: string) =>
881 search.search({ query }).pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
882 assert.deepEqual(yield* threadIds("ошибка в"), [older]);
883 assert.deepEqual(yield* threadIds("fermée ec"), [olderFrench]);
884 }).pipe(Effect.provide(layerTest)),
885);
886
887it.layer(layerTest)("ThreadSearch short words outside ASCII", (it) => {
888 it.effect("finds them in any case or accent, with or without the index", () =>
889 Effect.gen(function* () {
890 const projections = yield* ProjectionStore.ProjectionStoreV2;
891 const search = yield* ThreadSearch.ThreadSearch;
892 const sql = yield* SqlClient.SqlClient;
893 const project = ProjectId.make("project:short-non-ascii");
894 const russian = ThreadId.make("thread:short-russian");
895 const french = ThreadId.make("thread:short-french");
896 yield* createProject(project);
897 yield* Effect.forEach(
898 [
899 thread(russian, project),
900 message(russian, "russian", "user", "В логах ошибка"),
901 thread(french, project),
902 message(french, "french", "user", "École fermée à Lyon"),
903 message(french, "french-shout", "user", "Météo école"),
904 message(french, "romanian", "user", "Școala închisă"),
905 message(french, "vietnamese", "user", "Hồ Chí Minh"),
906 ],
907 projections.apply,
908 { discard: true },
909 );
910 const threadIds = (query: string) =>
911 search.search({ query }).pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
912
913 const assertMatches = Effect.gen(function* () {
914 assert.deepEqual(yield* threadIds("ошибка в"), [russian]);
915 assert.deepEqual(yield* threadIds("fermée éc"), [french]);
916 assert.deepEqual(yield* threadIds("ecole fermée"), [french]);
917 assert.deepEqual(yield* threadIds("école me"), [french]);
918 assert.deepEqual(yield* threadIds("închisă sc"), [french]);
919 assert.deepEqual(yield* threadIds("chí ho"), [french]);
920 // They still narrow: a word the message lacks rules it out.
921 assert.deepEqual(yield* threadIds("ошибка я"), []);
922 assert.deepEqual(yield* threadIds("fermée ce"), []);
923 });
924 yield* assertMatches;
925 yield* sql`DROP TABLE orchestration_v2_message_search`;
926 yield* assertMatches;
927 }),
928 );
929});
930
931it.layer(layerTest)("ThreadSearch across scripts and without the index", (it) => {
932 it.effect(
933 "matches Korean, Japanese, and two-character Chinese words, and whole short words",
934 () =>
935 Effect.gen(function* () {
936 const projections = yield* ProjectionStore.ProjectionStoreV2;
937 const search = yield* ThreadSearch.ThreadSearch;
938 const sql = yield* SqlClient.SqlClient;
939 const project = ProjectId.make("project:scripts");
940 const threadId = ThreadId.make("thread:scripts");
941 const drafts = ThreadId.make("thread:drafts");
942 const addresses = ThreadId.make("thread:addresses");
943 yield* createProject(project);
944 yield* Effect.forEach(
945 [
946 thread(threadId, project),
947 message(threadId, "korean", "user", "검색 기능을 고쳐 주세요"),
948 message(threadId, "japanese", "user", "がんばって検索します"),
949 message(threadId, "chinese", "user", "明天部署到生产环境"),
950 message(threadId, "fts", "assistant", "the FTS index is ready"),
951 thread(drafts, project),
952 message(drafts, "drafts", "assistant", "saved two drafts and shifts"),
953 message(threadId, "camel", "assistant", "Configure enableFTS today"),
954 message(threadId, "accented", "user", "Review Résumé indexing"),
955 thread(addresses, project),
956 message(addresses, "addresses", "user", "This addresses Résumé indexing"),
957 ],
958 projections.apply,
959 { discard: true },
960 );
961 const threadIds = (query: string) =>
962 search
963 .search({ query })
964 .pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
965
966 // Short words must start a word, which camelCase and accented words do.
967 for (const query of ["검색 기능", "がんばって", "部署", "fts", "enable"]) {
968 assert.deepEqual(yield* threadIds(query), [threadId], query);
969 }
970 // "res" inside "addresses" does not hide the accented word after it.
971 assert.sameMembers(yield* threadIds("res"), [threadId, addresses]);
972 // A word beats the same letters inside a longer word, even in an older message.
973 const trust = ThreadId.make("thread:trust");
974 const rust = ThreadId.make("thread:rust");
975 yield* Effect.forEach(
976 [
977 thread(rust, project),
978 message(rust, "rust", "assistant", "Rust rewrite notes"),
979 thread(trust, project),
980 message(trust, "trust", "user", "We trust the build"),
981 ],
982 projections.apply,
983 { discard: true },
984 );
985 const { matches } = yield* search.search({ query: "rust" });
986 assert.deepEqual(
987 matches.map((match) => [match.threadId, (match.quality ?? 1) <= 0.3]),
988 [
989 [rust, false],
990 [trust, true],
991 ],
992 );
993 // Without the index (an SQLite build without FTS5 trigrams) search scans instead.
994 yield* sql`DROP TABLE orchestration_v2_message_search`;
995 assert.deepEqual(yield* threadIds("検索"), [threadId]);
996 assert.deepEqual(yield* threadIds("fts"), [threadId]);
997 }),
998 );
999});
1000
1001it.layer(layerTest)("ThreadSearch.searchOlderSettled", (it) => {
1002 it.effect("returns older settled title matches in scope, then older message hits", () =>
1003 Effect.gen(function* () {
1004 const projections = yield* ProjectionStore.ProjectionStoreV2;
1005 const search = yield* ThreadSearch.ThreadSearch;
1006 const project = ProjectId.make("project:kestrel");
1007 const other = ProjectId.make("project:kestrel-other");
1008 yield* createProject(project);
1009 yield* createProject(other);
1010 const id = (name: string) => ThreadId.make(`thread:kestrel-${name}`);
1011 const old = at(10);
1012 yield* Effect.forEach(
1013 [
1014 thread(id("old"), project, { title: "Kestrel nest", settledAt: old }),
1015 thread(id("recent"), project, { title: "Kestrel nest", settledAt: at(50) }),
1016 thread(id("open"), project, { title: "Kestrel nest" }),
1017 thread(id("child"), project, { title: "Kestrel nest", subagentOf: id("old") }),
1018 thread(id("elsewhere"), other, { title: "Kestrel nest", settledAt: old }),
1019 thread(id("note"), project, { title: "Unrelated", settledAt: old }),
1020 thread(id("new-note"), project, { title: "Unrelated", settledAt: at(50) }),
1021 ],
1022 projections.apply,
1023 { discard: true },
1024 );
1025 const before = DateTime.formatIso(at(30));
1026 const messageThreadIds = [id("new-note"), id("note"), id("old")];
1027 assert.sameMembers(
1028 [...(yield* search.searchOlderSettled({ query: "kestrel", before, messageThreadIds }))],
1029 [id("old"), id("elsewhere"), id("note")],
1030 );
1031 // The scope applies to titles and message hits; message hits follow the titles in order.
1032 assert.deepEqual(
1033 yield* search.searchOlderSettled({
1034 query: "kestrel",
1035 before,
1036 projectIds: [project],
1037 messageThreadIds: [id("elsewhere"), ...messageThreadIds],
1038 }),
1039 [id("old"), id("note")],
1040 );
1041 // A palette also matches the environment's name.
1042 assert.include(
1043 yield* search.searchOlderSettled({
1044 query: "laptop unrelated",
1045 before,
1046 environmentLabel: "Laptop",
1047 messageThreadIds: [],
1048 }),
1049 id("note"),
1050 );
1051 }),
1052 );
1053
1054 it.effect("matches the branch and linked pull requests, like the palette", () =>
1055 Effect.gen(function* () {
1056 const projections = yield* ProjectionStore.ProjectionStoreV2;
1057 const search = yield* ThreadSearch.ThreadSearch;
1058 const project = ProjectId.make("project:heron");
1059 yield* createProject(project);
1060 const pullRequest = (number: number): ThreadLinkedPullRequest => ({
1061 projectId: project,
1062 repository: "acme/heron",
1063 number,
1064 url: `https://github.com/acme/heron/pull/${number}`,
1065 });
1066 const branch = ThreadId.make("thread:heron-branch");
1067 const linked = ThreadId.make("thread:heron-linked");
1068 yield* Effect.forEach(
1069 [
1070 thread(branch, project, {
1071 title: "Heron",
1072 settledAt: at(10),
1073 branchPullRequest: pullRequest(4101),
1074 }),
1075 thread(linked, project, {
1076 title: "Heron",
1077 settledAt: at(10),
1078 linkedPullRequest: pullRequest(4202),
1079 }),
1080 ],
1081 projections.apply,
1082 { discard: true },
1083 );
1084 const find = (query: string) =>
1085 search.searchOlderSettled({
1086 query,
1087 before: DateTime.formatIso(at(30)),
1088 messageThreadIds: [],
1089 });
1090 assert.deepEqual(yield* find("#4101"), [branch]);
1091 assert.deepEqual(yield* find("acme/heron#4202"), [linked]);
1092 }),
1093 );
1094});
1095
1096it.layer(layerTest)("ThreadSearch.searchThreadsWithOlderSettledRows", (it) => {
1097 it.effect("adds older settled rows to message hits, or alone for titles only", () =>
1098 Effect.gen(function* () {
1099 const projections = yield* ProjectionStore.ProjectionStoreV2;
1100 const search = yield* ThreadSearch.ThreadSearch;
1101 const sql = yield* SqlClient.SqlClient;
1102 const project = ProjectId.make("project:plover");
1103 yield* createProject(project);
1104 const old = ThreadId.make("thread:plover-old");
1105 const recent = ThreadId.make("thread:plover-recent");
1106 yield* Effect.forEach(
1107 [
1108 thread(old, project, { title: "Plover nest", settledAt: at(10) }),
1109 thread(recent, project, { title: "Plover nest", settledAt: at(50) }),
1110 message(recent, "plover-note", "user", "a plover note", { minute: 2 }),
1111 ],
1112 projections.apply,
1113 { discard: true },
1114 );
1115 const run = (request: { readonly titlesOnly?: boolean }) =>
1116 ThreadSearch.searchThreadsWithOlderSettledRows({
1117 request: { query: "plover", olderSettledBefore: DateTime.formatIso(at(30)), ...request },
1118 search: search.search,
1119 searchOlderSettled: search.searchOlderSettled,
1120 sql,
1121 readThreads: (threadIds) =>
1122 projections.readShellSnapshot({ location: "active", threadIds }),
1123 latestSequence: Effect.succeed(7),
1124 });
1125 const withMessages = yield* run({});
1126 assert.deepEqual(
1127 withMessages.matches.map((match) => match.threadId),
1128 [recent],
1129 );
1130 assert.equal(withMessages.snapshotSequence, 7);
1131 assert.deepEqual(
1132 withMessages.threads?.map((row) => row.id),
1133 [old],
1134 );
1135 const titles = yield* run({ titlesOnly: true });
1136 assert.deepEqual(titles.matches, []);
1137 assert.deepEqual(
1138 titles.threads?.map((row) => row.id),
1139 [old],
1140 );
1141 // Without the cut it is the plain message search.
1142 assert.isUndefined(
1143 (yield* ThreadSearch.searchThreadsWithOlderSettledRows({
1144 request: { query: "plover" },
1145 search: search.search,
1146 searchOlderSettled: search.searchOlderSettled,
1147 sql,
1148 readThreads: (threadIds) =>
1149 projections.readShellSnapshot({ location: "active", threadIds }),
1150 latestSequence: Effect.succeed(7),
1151 })).threads,
1152 );
1153 }),
1154 );
1155});
1156
1157it.layer(layerTest)("ThreadSearch.searchThreads", (it) => {
1158 it.effect("keeps room for a message-only thread behind many titled ones", () =>
1159 Effect.gen(function* () {
1160 const projections = yield* ProjectionStore.ProjectionStoreV2;
1161 const search = yield* ThreadSearch.ThreadSearch;
1162 const project = ProjectId.make("project:petrel");
1163 yield* createProject(project);
1164 const messageOnly = ThreadId.make("thread:petrel-message");
1165 yield* Effect.forEach(
1166 [
1167 thread(messageOnly, project),
1168 // The oldest message, so a cut taken before titles drop would lose it.
1169 message(messageOnly, "petrel-old", "user", "a petrel note", { minute: 1 }),
1170 ...Array.from({ length: 50 }, (_, index) => {
1171 const id = ThreadId.make(`thread:petrel-${index}`);
1172 return [
1173 thread(id, project, { title: `Petrel ${index}` }),
1174 message(id, `petrel-${index}`, "user", "petrel again", { minute: 2 }),
1175 ];
1176 }).flat(),
1177 ],
1178 projections.apply,
1179 { discard: true },
1180 );
1181 const { matches } = yield* search.searchThreads({ query: "petrel", limit: 5 });
1182 assert.include(
1183 matches.map((match) => match.threadId),
1184 messageOnly,
1185 );
1186 }),
1187 );
1188
1189 it.effect("leaves titled threads out before the hit limit", () =>
1190 Effect.gen(function* () {
1191 const projections = yield* ProjectionStore.ProjectionStoreV2;
1192 const search = yield* ThreadSearch.ThreadSearch;
1193 const sql = yield* SqlClient.SqlClient;
1194 const project = ProjectId.make("project:hit-limit");
1195 const messageOnly = ThreadId.make("thread:grebe-message");
1196 const titled = ThreadId.make("thread:grebe-titled");
1197 yield* createProject(project);
1198 yield* Effect.forEach(
1199 [
1200 thread(messageOnly, project, { title: "Unrelated" }),
1201 message(messageOnly, "grebe-old", "user", "a grebe note", { minute: 1 }),
1202 thread(titled, project, { title: "Grebe" }),
1203 ],
1204 projections.apply,
1205 { discard: true },
1206 );
1207 // Newer hits than one read holds, all in the titled thread.
1208 yield* sql`
1209 WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 2000)
1210 INSERT INTO orchestration_v2_projection_messages
1211 (message_id, thread_id, role, streaming, created_at, updated_at, payload_json)
1212 SELECT 'grebe-titled-' || i, ${titled}, 'assistant', 0, ${DateTime.formatIso(at(5))},
1213 ${DateTime.formatIso(at(5))}, json_object('text', 'grebe again')
1214 FROM n
1215 `;
1216 const { matches } = yield* search.searchThreads({ query: "grebe", limit: 5 });
1217 assert.include(
1218 matches.map((match) => match.threadId),
1219 messageOnly,
1220 );
1221 }),
1222 );
1223
1224 it.effect("ranks title matches like the palette, then message matches, within one project", () =>
1225 Effect.gen(function* () {
1226 const projections = yield* ProjectionStore.ProjectionStoreV2;
1227 const search = yield* ThreadSearch.ThreadSearch;
1228 const project = ProjectId.make("project:titles");
1229 const otherProject = ProjectId.make("project:titles-other");
1230 yield* createProject(project);
1231 yield* createProject(otherProject);
1232 const wordStart = ThreadId.make("thread:word-start");
1233 const midWord = ThreadId.make("thread:mid-word");
1234 const messageOnly = ThreadId.make("thread:message-only");
1235 const elsewhere = ThreadId.make("thread:elsewhere");
1236 yield* Effect.forEach(
1237 [
1238 thread(midWord, project, { title: "Fix oauth redirect" }),
1239 thread(wordStart, project, { title: "Clean up auth tokens" }),
1240 thread(messageOnly, project, { title: "Unrelated" }),
1241 message(messageOnly, "message-only", "user", "the auth flow broke again"),
1242 thread(elsewhere, otherProject, { title: "Auth everywhere" }),
1243 ],
1244 projections.apply,
1245 { discard: true },
1246 );
1247
1248 // Stronger message hits in another project must not use up this project's limit.
1249 yield* Effect.forEach(
1250 Array.from({ length: 55 }, (_, index) => {
1251 const busy = ThreadId.make(`thread:elsewhere-${index}`);
1252 return [
1253 thread(busy, otherProject),
1254 message(busy, `elsewhere-${index}`, "user", "auth auth auth"),
1255 ];
1256 }).flat(),
1257 projections.apply,
1258 { discard: true },
1259 );
1260 const result = yield* search.searchThreads({ query: "auth", projectId: project, limit: 3 });
1261 assert.deepEqual(
1262 result.matches.map((match) => [match.threadId, match.source]),
1263 [
1264 [wordStart, "title"],
1265 [midWord, "title"],
1266 [messageOnly, "user"],
1267 ],
1268 );
1269 // With one slot, an exact title keeps it.
1270 assert.deepEqual(
1271 (yield* search.searchThreads({ query: "auth", projectId: project, limit: 1 })).matches.map(
1272 (match) => match.threadId,
1273 ),
1274 [wordStart],
1275 );
1276 // Exact matching finds nothing, so one typo is forgiven.
1277 assert.deepEqual(
1278 (yield* search.searchThreads({ query: "tokesn", projectId: project })).matches.map(
1279 (match) => match.title,
1280 ),
1281 ["Clean up auth tokens"],
1282 );
1283 }),
1284 );
1285});
1286
1287it.effect("starts snippets a few words before the match so one line shows it", () =>
1288 Effect.sync(() => {
1289 const snippet = (text: string, word: string) =>
1290 ThreadSearch.buildSearchSnippet(text, text.indexOf(word));
1291 const long = `${"lead ".repeat(60)}the NEEDLE sits here ${"tail ".repeat(60)}`;
1292 const cut = snippet(long, "NEEDLE");
1293 assert.isAtMost(cut.length, 240);
1294 assert.match(cut, /^…(lead ){2,4}the NEEDLE sits here/);
1295 assert.isTrue(cut.endsWith("…"));
1296 // Whitespace collapses only in the shown part, so the offset still lands.
1297 assert.match(snippet(`${"word\n\n ".repeat(60)}un café`, "café"), /un café$/);
1298 // A cut never splits an emoji's surrogate pair.
1299 assert.notMatch(
1300 snippet(`x${"🚀".repeat(200)}needle`, "needle"),
1301 /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/,
1302 );
1303 // Emphasis and code marks go, as the thread shows the text.
1304 assert.equal(
1305 ThreadSearch.buildSearchSnippet("Use **bold** and `code` here", 0),
1306 "Use bold and code here",
1307 );
1308 assert.equal(ThreadSearch.buildSearchSnippet("Call __init__ first", 0), "Call __init__ first");
1309 // Short text still moves a late match into view.
1310 assert.equal(snippet(`${"word ".repeat(20)}needle`, "needle"), "…word word word word needle");
1311 }),
1312);
1313
1314it.effect("searches on the reader worker while the writer is busy", () =>
1315 Effect.gen(function* () {
1316 const fileSystem = yield* FileSystem.FileSystem;
1317 const path = yield* Path.Path;
1318 const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-thread-search-" });
1319 const layerFile = Layer.mergeAll(
1320 ThreadSearch.layer,
1321 ProjectionStore.layer,
1322 ProjectStore.layer,
1323 ).pipe(
1324 Layer.provideMerge(SqlitePersistence.layerFromPath(path.join(directory, "state.sqlite"))),
1325 Layer.provide(NodeServices.layer),
1326 );
1327
1328 yield* Effect.gen(function* () {
1329 const projections = yield* ProjectionStore.ProjectionStoreV2;
1330 const search = yield* ThreadSearch.ThreadSearch;
1331 const sql = yield* SqlClient.SqlClient;
1332 const project = ProjectId.make("project:search-reader");
1333 const threadId = ThreadId.make("thread:reader");
1334 yield* createProject(project);
1335 yield* Effect.forEach(
1336 [thread(threadId, project), message(threadId, "reader", "user", "needle on the reader")],
1337 projections.apply,
1338 { discard: true },
1339 );
1340
1341 const writerHeld = yield* Deferred.make<void>();
1342 const releaseWriter = yield* Deferred.make<void>();
1343 const writer = yield* Effect.forkChild(
1344 sql.withTransaction(
1345 Deferred.succeed(writerHeld, undefined).pipe(
1346 Effect.andThen(Deferred.await(releaseWriter)),
1347 ),
1348 ),
1349 );
1350 yield* Deferred.await(writerHeld);
1351 const result = yield* search.search({ query: "needle" });
1352 yield* Deferred.succeed(releaseWriter, undefined);
1353 yield* Fiber.join(writer);
1354
1355 assert.deepEqual(
1356 result.matches.map((match) => match.threadId),
1357 [threadId],
1358 );
1359 }).pipe(Effect.provide(layerFile));
1360 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1361);