Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Predicate from "effect/Predicate";
3import * as SqlClient from "effect/sql/SqlClient";
4
5import {
6 indexedMessageCondition,
7 indexedMessageText,
8} from "../../orchestration-v2/messageSearchIndex.ts";
9
10/**
11 * Full-text index for thread message search. The trigram tokenizer matches any
12 * substring of three or more characters, ignoring case and accents, so
13 * identifiers inside code still match, without the full-table scan a
14 * `LIKE '%q%'` needs.
15 *
16 * It holds what the user wrote and what agents answered. User-role messages
17 * that agents or the system wrote (pull request updates, task notices) would
18 * flood results and read as "You".
19 *
20 * The index is contentless and keyed by the message rowid: search reads the
21 * text back from the message row, so the text is not stored twice. `t3 doctor`
22 * rebuilds it after its VACUUM, since SQLite does not promise to keep rowids.
23 * Thread search scans the same messages when the index is missing.
24 *
25 * Triggers keep it in sync with every write to the messages table. Streaming
26 * messages are indexed once they finish, with apostrophes folded as search
27 * folds them.
28 */
29export default Effect.gen(function* () {
30 const sql = yield* SqlClient.SqlClient;
31
32 // Node and Electron bundle a SQLite with these options; a Node built against
33 // an older system SQLite may lack them. Search then scans messages instead
34 // of failing to start. Only SQLITE_ERROR ("no such module", "no such
35 // tokenizer", "unrecognized option") means that: a full disk or I/O error
36 // fails the migration, so the next start retries instead of skipping the
37 // index for good.
38 const created = yield* sql`
39 CREATE VIRTUAL TABLE orchestration_v2_message_search USING fts5(
40 text,
41 content = '',
42 contentless_delete = 1,
43 tokenize = 'trigram remove_diacritics 1'
44 )
45 `.pipe(
46 Effect.as(true),
47 Effect.catchIf(
48 (cause) =>
49 Predicate.hasProperty(cause.reason.cause, "errcode") && cause.reason.cause.errcode === 1,
50 (cause) =>
51 Effect.logWarning(
52 "Message search index unavailable; search will scan messages.",
53 cause,
54 ).pipe(Effect.as(false)),
55 ),
56 );
57 if (!created) return;
58 yield* sql`
59 CREATE TRIGGER orchestration_v2_message_search_insert
60 AFTER INSERT ON orchestration_v2_projection_messages
61 WHEN ${indexedMessageCondition(sql, "new")}
62 BEGIN
63 INSERT INTO orchestration_v2_message_search (rowid, text)
64 SELECT new.rowid, ${indexedMessageText(sql, "new.payload_json")}
65 WHERE json_extract(new.payload_json, '$.text') IS NOT NULL;
66 END
67 `;
68 // Streaming deltas rewrite the row many times a second; skip them until the
69 // message settles. Upserts rewrite every column, so a finished message
70 // re-indexes only when what the index reads changed. Deleting a rowid the
71 // index never held is a no-op.
72 yield* sql`
73 CREATE TRIGGER orchestration_v2_message_search_update
74 AFTER UPDATE OF payload_json, streaming, role ON orchestration_v2_projection_messages
75 WHEN (old.streaming = 0 OR new.streaming = 0)
76 AND (
77 old.streaming IS NOT new.streaming
78 OR old.role IS NOT new.role
79 OR json_extract(old.payload_json, '$.text') IS NOT json_extract(new.payload_json, '$.text')
80 OR json_extract(old.payload_json, '$.createdBy')
81 IS NOT json_extract(new.payload_json, '$.createdBy')
82 )
83 BEGIN
84 DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid AND old.streaming = 0;
85 INSERT INTO orchestration_v2_message_search (rowid, text)
86 SELECT new.rowid, ${indexedMessageText(sql, "new.payload_json")}
87 WHERE ${indexedMessageCondition(sql, "new")}
88 AND json_extract(new.payload_json, '$.text') IS NOT NULL;
89 END
90 `;
91 yield* sql`
92 CREATE TRIGGER orchestration_v2_message_search_delete
93 AFTER DELETE ON orchestration_v2_projection_messages
94 WHEN old.streaming = 0
95 BEGIN
96 DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid;
97 END
98 `;
99 yield* sql`
100 INSERT INTO orchestration_v2_message_search (rowid, text)
101 SELECT messages.rowid, ${indexedMessageText(sql, "messages.payload_json")}
102 FROM orchestration_v2_projection_messages AS messages
103 WHERE ${indexedMessageCondition(sql)}
104 AND json_extract(messages.payload_json, '$.text') IS NOT NULL
105 `;
106});