The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; |
| 3 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as SqlClient from "effect/sql/SqlClient"; |
| 6 | |
| 7 | import { rebuildMessageSearch } from "../../orchestration-v2/messageSearchIndex.ts"; |
| 8 | import { runMigrations } from "../Migrations.ts"; |
| 9 | |
| 10 | const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); |
| 11 | |
| 12 | layer("069_ThreadMessageSearch", (it) => { |
| 13 | it.effect("backfills what users wrote and agents answered, then follows deletes", () => |
| 14 | Effect.gen(function* () { |
| 15 | const sql = yield* SqlClient.SqlClient; |
| 16 | yield* runMigrations({ toMigrationInclusive: 68 }); |
| 17 | const insert = ( |
| 18 | id: string, |
| 19 | role: string, |
| 20 | streaming: number, |
| 21 | text: string, |
| 22 | createdBy: string | undefined = role === "user" ? "user" : "agent", |
| 23 | ) => sql` |
| 24 | INSERT INTO orchestration_v2_projection_messages ( |
| 25 | message_id, thread_id, run_id, node_id, role, streaming, created_at, updated_at, payload_json |
| 26 | ) |
| 27 | VALUES ( |
| 28 | ${id}, 'thread-1', NULL, NULL, ${role}, ${streaming}, |
| 29 | '2026-09-01T00:00:00.000Z', '2026-09-01T00:00:00.000Z', ${JSON.stringify({ text, createdBy })} |
| 30 | ) |
| 31 | `; |
| 32 | yield* insert("user", "user", 0, "needle from the user"); |
| 33 | yield* insert("assistant", "assistant", 0, "needle from the agent"); |
| 34 | yield* insert("streaming", "assistant", 1, "needle still streaming"); |
| 35 | yield* insert("system", "system", 0, "needle in a system prompt"); |
| 36 | yield* insert("notice", "user", 0, "needle in a pull request update", "agent"); |
| 37 | // Older rows carry no author; they were the user's. |
| 38 | yield* insert("legacy", "user", 0, "needle from before authors", undefined); |
| 39 | |
| 40 | yield* runMigrations(); |
| 41 | |
| 42 | const indexed = () => |
| 43 | sql<{ readonly messageId: string }>` |
| 44 | SELECT messages.message_id AS "messageId" |
| 45 | FROM orchestration_v2_message_search AS search |
| 46 | INNER JOIN orchestration_v2_projection_messages AS messages |
| 47 | ON messages.rowid = search.rowid |
| 48 | WHERE orchestration_v2_message_search MATCH '"needle"' |
| 49 | ORDER BY messages.message_id |
| 50 | `.pipe(Effect.map((rows) => rows.map((row) => row.messageId))); |
| 51 | assert.deepEqual(yield* indexed(), ["assistant", "legacy", "user"]); |
| 52 | |
| 53 | yield* sql`DELETE FROM orchestration_v2_projection_messages WHERE message_id = 'user'`; |
| 54 | assert.deepEqual(yield* indexed(), ["assistant", "legacy"]); |
| 55 | }), |
| 56 | ); |
| 57 | }); |
| 58 | |
| 59 | it.effect.each(["inserted", "updated", "backfilled", "rebuilt"])( |
| 60 | "keeps normal messages indexed and excludes %s placeholders", |
| 61 | (path) => |
| 62 | Effect.gen(function* () { |
| 63 | const sql = yield* SqlClient.SqlClient; |
| 64 | yield* runMigrations({ toMigrationInclusive: path === "backfilled" ? 68 : 73 }); |
| 65 | if (path !== "backfilled") { |
| 66 | // Pre-upgrade triggers indexed user text even when it was an attachment placeholder. |
| 67 | yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`; |
| 68 | yield* sql`DROP TRIGGER orchestration_v2_message_search_update`; |
| 69 | yield* sql` |
| 70 | CREATE TRIGGER orchestration_v2_message_search_insert |
| 71 | AFTER INSERT ON orchestration_v2_projection_messages |
| 72 | BEGIN |
| 73 | INSERT INTO orchestration_v2_message_search (rowid, text) |
| 74 | VALUES (new.rowid, json_extract(new.payload_json, '$.text')); |
| 75 | END |
| 76 | `; |
| 77 | yield* sql` |
| 78 | CREATE TRIGGER orchestration_v2_message_search_update |
| 79 | AFTER UPDATE OF payload_json ON orchestration_v2_projection_messages |
| 80 | BEGIN |
| 81 | DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid; |
| 82 | INSERT INTO orchestration_v2_message_search (rowid, text) |
| 83 | VALUES (new.rowid, json_extract(new.payload_json, '$.text')); |
| 84 | END |
| 85 | `; |
| 86 | if (path !== "rebuilt") yield* runMigrations(); |
| 87 | } |
| 88 | const placeholders = [ |
| 89 | ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, |
| 90 | ...[ |
| 91 | "", |
| 92 | " ", |
| 93 | "\n", |
| 94 | "\t\r\n", |
| 95 | "\v\f", |
| 96 | "\u00a0", |
| 97 | "\u1680", |
| 98 | "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a", |
| 99 | "\u2028\u2029\u202f\u205f\u3000\ufeff", |
| 100 | ].map((whitespace) => `uLtRaThInK:${whitespace}${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`), |
| 101 | ]; |
| 102 | const normal = [ |
| 103 | "normal context", |
| 104 | ATTACHMENT_ONLY_BOOTSTRAP_PROMPT.toLowerCase(), |
| 105 | `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT.toLowerCase()}`, |
| 106 | `quoted ${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, |
| 107 | `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT} extra`, |
| 108 | ` ${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, |
| 109 | `Ultrathink:\u0085${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, |
| 110 | ]; |
| 111 | for (const [index, text] of [...normal, ...placeholders].entries()) { |
| 112 | const payload = JSON.stringify({ text, createdBy: "user" }); |
| 113 | yield* sql` |
| 114 | INSERT INTO orchestration_v2_projection_messages |
| 115 | (message_id, thread_id, role, streaming, created_at, updated_at, payload_json) |
| 116 | VALUES (${String(index)}, 'thread-1', 'user', 0, |
| 117 | '2026-09-01T00:00:00.000Z', '2026-09-01T00:00:00.000Z', |
| 118 | ${path === "updated" ? JSON.stringify({ text: "normal context" }) : payload}) |
| 119 | `; |
| 120 | if (path === "updated") { |
| 121 | yield* sql` |
| 122 | UPDATE orchestration_v2_projection_messages SET payload_json = ${payload} |
| 123 | WHERE message_id = ${String(index)} |
| 124 | `; |
| 125 | } |
| 126 | } |
| 127 | yield* runMigrations({ toMigrationInclusive: path === "backfilled" ? 69 : undefined }); |
| 128 | if (path === "rebuilt") yield* rebuildMessageSearch(sql); |
| 129 | const indexed = yield* sql<{ readonly messageId: string }>` |
| 130 | SELECT messages.message_id AS "messageId" |
| 131 | FROM orchestration_v2_message_search AS search |
| 132 | INNER JOIN orchestration_v2_projection_messages AS messages ON messages.rowid = search.rowid |
| 133 | ORDER BY messages.rowid |
| 134 | `; |
| 135 | assert.deepEqual( |
| 136 | indexed.map((row) => row.messageId), |
| 137 | normal.map((_, index) => String(index)), |
| 138 | ); |
| 139 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), |
| 140 | ); |