feat(search): Cmd+K finds the right thread first, with indexed message search #24343
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 66 commits into
main fromrefs/pull/24343/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- packages/shared/src/paletteSearch.ts line 616OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +596,21 @@596+ return (text) => {597+ const lower = text.toLowerCase();598+ // "İ" lowercases to two units, which would shift every offset after it.599+ const plain = lower.length === text.length ? lower : foldInPlace(text);600+ let folded: string | null = null;601+ let quality = 1;602+ const positions: Array<ReadonlyArray<number>> = [];603+ for (const { token, folded: foldedToken, nonAscii, mustStartWord, grades } of tokens) {604+ let found = findMessageToken(text, plain, token);605+ // Accents typed or left out: "cafe" finds "Café", also after "cafeteria".606+ if (found?.quality !== 1 && foldedToken.length > 0 && (nonAscii || NON_ASCII.test(text))) {607+ found = findMessageToken(text, (folded ??= foldInPlace(text)), foldedToken);608+ }609+ if (found === null) return null;610+ if (found.quality === MID_WORD && mustStartWord) return null;611+ if (grades) quality = Math.min(quality, found.quality);612+ positions.push(found.at);613+ }614+ // Written together as typed: the snippet starts there. Apostrophes are615+ // straight in the query, and straightening keeps offsets.616+ const together = phrase?.exec(plain.replace(CURLY_APOSTROPHES, "'"));packages/shared/src/paletteSearch.ts line 65Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +45,21 @@45+46+function foldCharacter(character: string): string {47+ // A typed straight apostrophe finds a curly one: iOS keyboards type "’".48+ return character49+ .normalize("NFKD")50+ .replace(COMBINING_MARKS, "")51+ .toLowerCase()52+ .replace("ς", "σ")53+ .replace(/[‘’]/, "'");54+}55+56+/**57+ * `text` with case and accents folded one UTF-16 unit at a time, so offsets58+ * and word starts still read from `text` and highlights need no mapping. A59+ * letter that folds to more or less than one unit ("ß", "fi") stays as it is.60+ */61+function foldInPlace(text: string): string {62+ return text63+ .replace(/[\u0080-\uffff]/g, (character) => {64+ const folded = foldCharacter(character);65+ return folded.length === 1 ? folded : character;apps/server/src/orchestration-v2/ThreadSearch.ts line 225Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -59,0 +202,21 @@202+ json_extract(messages.payload_json, '$.text') AS text,203+ messages.created_at AS "messageCreatedAt"204+ FROM orchestration_v2_message_search205+ INNER JOIN orchestration_v2_projection_messages AS messages206+ ON messages.rowid = orchestration_v2_message_search.rowid207+ INNER JOIN eligible ON eligible.thread_id = messages.thread_id208+ WHERE orchestration_v2_message_search MATCH ${match}209+ AND orchestration_v2_message_search.rowid >= coalesce((210+ SELECT rowid FROM orchestration_v2_message_search211+ WHERE orchestration_v2_message_search MATCH ${match}212+ ORDER BY rowid DESC213+ LIMIT 1 OFFSET ${MESSAGE_CANDIDATE_LIMIT - 1}214+ ), 0)215+ `216+ }217+ -- Answers to an agent's question show inside its question card, where218+ -- neither find nor a jump can reach them.219+ AND messages.message_id NOT LIKE 'async-answer:%'220+ AND NOT EXISTS (221+ SELECT 1 FROM json_each(${substrings}) AS token222+ WHERE instr(lower(json_extract(messages.payload_json, '$.text')), token.value) = 0apps/server/src/orchestration-v2/ThreadSearch.ts line 364OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -162,0 +344,21 @@344+ newer(left, right);345+ const rows = yield* read(346+ Effect.gen(function* () {347+ const index = yield* sql`348+ SELECT 1 FROM sqlite_master WHERE name = 'orchestration_v2_message_search'349+ `;350+ const messageSearch = buildMessageSearch(input.query, index.length > 0);351+ return messageSearch === null352+ ? []353+ : yield* searchRows({ ...messageSearch, projectId: input.projectId });354+ }),355+ );356+ const best = new Map<ThreadId, Hit>();357+ // Matching messages per thread at its best quality: a thread that keeps358+ // using the word ranks above a passing mention, and weaker hits ("Trust"359+ // for "rust") never add to a whole word's count.360+ const hitCounts = new Map<ThreadId, number>();361+ const hitCount = (threadId: ThreadId) => hitCounts.get(threadId) ?? 1;362+ const matchMessage = messageMatcher(input.query);363+ for (const row of rows) {364+ if (input.excludedThreadIds?.has(row.threadId)) continue;
- packages/shared/src/paletteSearch.ts line 532OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +512,21 @@512+ return isWordStart(513+ text[index - 2] ?? "",514+ text[index - 1] ?? "",515+ character,516+ text[index + 1] ?? "",517+ );518+}519+520+// Whether a word ends right before `end` in `text`.521+function endsWordAt(text: string, end: number): boolean {522+ return end >= text.length || !ALPHANUMERIC.test(text[end]!) || startsWordAt(text, end);523+}524+525+// A commit SHA or UUID part: seven or more hex characters with a digit.526+const HEX_ID = /^(?=[a-f]*\d)[0-9a-f]{7,}$/i;527+528+// Whether `index` sits past the start of a hex ID: its letters after digits529+// are not words ("cafe" in "7cafe52bb8"), though a pasted SHA prefix is.530+function insideHexId(text: string, index: number, length: number): boolean {531+ let start = index;532+ while (start > 0 && ALPHANUMERIC.test(text[start - 1]!)) start--;
- apps/mobile/src/features/threads/ThreadDetailScreen.tsx lines 429 to 432Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -404,3 +419,14 @@404419 const wakeIsLatestUpdate = selectedThreadUpdatedAt === selectedThreadWokeAt;405420 const selectedThreadLastVisitedAt = thread.lastVisitedAt;406421 const selectedThreadCompletedAt = thread.latestRun?.completedAt;422+ // Opening a thread is one visit that also counts toward search ranking; a423+ // palette pick already sent it with the query. The watermark effect below424+ // skips it.425+ const recordedOpenRef = useRef<string | null>(null);426+ useEffect(() => {427+ if (!showContent) return;428+ if (recordedOpenRef.current === selectedThreadKey) return;429+ recordedOpenRef.current = selectedThreadKey;430+ // Recorded here even when the server cannot take opens, so recents follow it.431+ const claimed = claimThreadOpen(props.environmentId, selectedThreadId, Date.now());432+ if (!claimed || selectedThreadLastVisitedAt === undefined) return;apps/server/src/orchestration-v2/ThreadSearch.ts line 364OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -162,0 +344,21 @@344+ newer(left, right);345+ const rows = yield* read(346+ Effect.gen(function* () {347+ const index = yield* sql`348+ SELECT 1 FROM sqlite_master WHERE name = 'orchestration_v2_message_search'349+ `;350+ const messageSearch = buildMessageSearch(input.query, index.length > 0);351+ return messageSearch === null352+ ? []353+ : yield* searchRows({ ...messageSearch, projectId: input.projectId });354+ }),355+ );356+ const best = new Map<ThreadId, Hit>();357+ // Matching messages per thread at its best quality: a thread that keeps358+ // using the word ranks above a passing mention, and weaker hits ("Trust"359+ // for "rust") never add to a whole word's count.360+ const hitCounts = new Map<ThreadId, number>();361+ const hitCount = (threadId: ThreadId) => hitCounts.get(threadId) ?? 1;362+ const matchMessage = messageMatcher(input.query);363+ for (const row of rows) {364+ if (input.excludedThreadIds?.has(row.threadId)) continue;
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed