@devries-cameron20 / t3code-1 / commits / 314f1e2976
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): message search finds smart apostrophes when scanning (#36832)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 14, 2026 at 18:15 UTC 314f1e2976d9ee238af416b591822de9eb644a35
4 files changed +72 -28
apps/server/src/orchestration-v2/ThreadSearch.test.ts
+39 -25
@@ -736,31 +736,6 @@ it.layer(layerTest)("ThreadSearch filters before it ranks", (it) => {
736 }),
737 );
738
739 - it.effect("finds a curly apostrophe for a straight one and back", () =>
740 - Effect.gen(function* () {
741 - const projections = yield* ProjectionStore.ProjectionStoreV2;
742 - const search = yield* ThreadSearch.ThreadSearch;
743 - const project = ProjectId.make("project:apostrophes");
744 - const curly = ThreadId.make("thread:apostrophe-curly");
745 - const straight = ThreadId.make("thread:apostrophe-straight");
746 - yield* createProject(project);
747 - yield* Effect.forEach(
748 - [
749 - thread(curly, project),
750 - message(curly, "apostrophe-curly", "user", "the gannet\u2019s nest"),
751 - thread(straight, project),
752 - message(straight, "apostrophe-straight", "user", "a kittiwake's nest"),
753 - ],
754 - projections.apply,
755 - { discard: true },
756 - );
757 - const threadIds = (query: string) =>
758 - search.search({ query }).pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
759 - assert.deepEqual(yield* threadIds("gannet's"), [curly]);
760 - assert.deepEqual(yield* threadIds("kittiwake\u2019s"), [straight]);
761 - }),
762 - );
763 -
739 it.effect("leaves out answers in their question card and turns the thread hides", () =>
740 Effect.gen(function* () {
741 const projections = yield* ProjectionStore.ProjectionStoreV2;
@@ -986,6 +961,45 @@ it.effect.each([true, false])(
961 }).pipe(Effect.provide(layerTest)),
962 );
963
964 +// A two-character CJK word is too short for the index, so it scans even with the index.
965 +it.effect.each([true, false])(
966 + "ThreadSearch finds a curly apostrophe for a straight one and back (indexed: %s)",
967 + (indexed) =>
968 + Effect.gen(function* () {
969 + const projections = yield* ProjectionStore.ProjectionStoreV2;
970 + const search = yield* ThreadSearch.ThreadSearch;
971 + const sql = yield* SqlClient.SqlClient;
972 + const project = ProjectId.make("project:apostrophes");
973 + const curly = ThreadId.make("thread:apostrophe-curly");
974 + const straight = ThreadId.make("thread:apostrophe-straight");
975 + yield* createProject(project);
976 + yield* Effect.forEach(
977 + [
978 + thread(curly, project),
979 + message(curly, "apostrophe-curly", "user", "部署 the gannet\u2019s nest"),
980 + thread(straight, project),
981 + message(straight, "apostrophe-straight", "user", "部署 a kittiwake's nest"),
982 + ],
983 + projections.apply,
984 + { discard: true },
985 + );
986 + if (!indexed) {
987 + yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`;
988 + yield* sql`DROP TRIGGER orchestration_v2_message_search_update`;
989 + yield* sql`DROP TRIGGER orchestration_v2_message_search_delete`;
990 + yield* sql`DROP TABLE orchestration_v2_message_search`;
991 + }
992 + const threadIds = (query: string) =>
993 + search.search({ query }).pipe(Effect.map(({ matches }) => matches.map((m) => m.threadId)));
994 + for (const query of ["gannet's", "部署 gannet's"]) {
995 + assert.deepEqual(yield* threadIds(query), [curly], query);
996 + }
997 + for (const query of ["kittiwake\u2019s", "部署 kittiwake\u2019s"]) {
998 + assert.deepEqual(yield* threadIds(query), [straight], query);
999 + }
1000 + }).pipe(Effect.provide(layerTest)),
1001 +);
1002 +
1003 it.layer(layerTest)("ThreadSearch short words outside ASCII", (it) => {
1004 it.effect("finds them in any case or accent, with or without the index", () =>
1005 Effect.gen(function* () {
apps/server/src/orchestration-v2/ThreadSearch.ts
+3 -2
@@ -34,6 +34,7 @@ import {
34 } from "@t3tools/shared/paletteSearch";
35 import { threadSearchPullRequests } from "@t3tools/shared/threadPullRequests";
36 import {
37 + foldedMessageSearchText,
38 indexedMessageCondition,
39 messageSearchText,
40 nonPlaceholderMessageCondition,
@@ -299,7 +300,7 @@ export const messageHitsQuery = (
300 )
301 AND NOT EXISTS (
302 -- The text read once, not once per check.
302 - SELECT 1 FROM (SELECT ${messageSearchText(sql)} AS text) AS message,
303 + SELECT 1 FROM (SELECT ${foldedMessageSearchText(sql)} AS text) AS message,
304 json_each(${substrings}) AS substring
305 WHERE instr(lower(message.text), json_extract(substring.value, '$.token')) = 0
306 AND NOT (
@@ -311,7 +312,7 @@ export const messageHitsQuery = (
312 SELECT 1 FROM json_each(${shortSpellings}) AS token
313 WHERE NOT EXISTS (
314 SELECT 1 FROM json_each(token.value) AS spelling
314 - WHERE instr(${messageSearchText(sql)}, spelling.value) > 0
315 + WHERE instr(${foldedMessageSearchText(sql)}, spelling.value) > 0
316 )
317 )
318 -- The index's own rowid, so FTS5 returns hits newest first and stops at
apps/server/src/orchestration-v2/messageSearchIndex.ts
+5 -1
@@ -62,6 +62,10 @@ export const readableSearchText = (text: string) => {
62 export const messageSearchText = (sql: SqlClient.SqlClient) =>
63 sql.literal(readableText("messages"));
64
65 +/** What the index stores for a message, so a scan matches the folded query as the index does. */
66 +export const foldedMessageSearchText = (sql: SqlClient.SqlClient, row = "messages") =>
67 + sql.literal(foldApostrophes(readableText(row)));
68 +
69 /**
70 * A message's stored text with apostrophes folded, what the index held before
71 * migration 81. Migration 69's backfill uses it. `payload` is the message's
@@ -82,7 +86,7 @@ const indexedText = Effect.fn("MessageSearchIndex.indexedText")(function* (
86 WHERE name = 'search_text'
87 `;
88 return column.length > 0
85 - ? (row: string) => sql.literal(foldApostrophes(readableText(row)))
89 + ? (row: string) => foldedMessageSearchText(sql, row)
90 : (row: string) => indexedMessageText(sql, `${row}.payload_json`);
91 });
92
scripts/scenarios/thread-search-finds-curly-apostrophe.e2e.ts new
+25
@@ -0,0 +1,25 @@
1 +import { expect } from "e2e";
2 +
3 +import { test, WEB } from "./t3.ts";
4 +
5 +test(
6 + "Thread search finds a curly apostrophe next to a two-character Chinese word",
7 + WEB,
8 + async ({ t3, app, screen }) => {
9 + const project = await t3.project("demo");
10 + const thread = await t3.thread({ project, title: "Weekly notes", messages: ["Start"] });
11 + await t3.open(thread);
12 +
13 + // Search reads what the user wrote; messages the CLI sends count as an agent's.
14 + const composer = screen.getByRole("textbox");
15 + await composer.pressSequentially("部署 John’s service after migration");
16 + await composer.press("Enter");
17 + await expect(screen.getByText("second fixture turn complete", { exact: false })).toBeVisible();
18 +
19 + await screen.getByRole("combobox", "Search threads").fill("部署 John’s");
20 + await expect(screen.getByRole("listbox", "Thread search results")).toContainText(
21 + "John’s service",
22 + );
23 + await app.screenshot("search");
24 + },
25 +);