@devries-cameron20 / t3code-1 / commits / acad1d5a08
Fork 0

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

perf(shared): stop single-token message search at the best hit (#28930)

Hieu Le committed Nov 4, 2026 at 00:11 UTC acad1d5a089875a52b710f2bf6558de7d96bf8de
2 files changed +39 -3
packages/shared/src/paletteSearch.test.ts
+29
@@ -661,6 +661,26 @@ describe("scoreMessageMatch", () => {
661 expect(scoreMessageMatch("We trust the build", "rust")).toEqual({ quality: 0.3, offset: 4 });
662 expect(scoreMessageMatch("Rust and trust", "rust")).toEqual({ quality: 1, offset: 0 });
663 expect(scoreMessageMatch("Use the sidebar", '"sidebar"')).toEqual({ quality: 1, offset: 8 });
664 + for (const [text, query, expected] of [
665 + ["model mode mode", "mode", { quality: 1, offset: 6 }],
666 + ["xmode model models", "mode", { quality: 0.8, offset: 6 }],
667 + ["model models", "mode", { quality: 0.8, offset: 0 }],
668 + ["xmode ymode", "mode", { quality: 0.3, offset: 1 }],
669 + ["mode mode", "mode", { quality: 1, offset: 0 }],
670 + ["fts fts", "fts", { quality: 1, offset: 0 }],
671 + ["the theme", "he", null],
672 + ["he theme", "he", { quality: 1, offset: 0 }],
673 + ["notes", "", { quality: 1, offset: 0 }],
674 + ["", "notes", null],
675 + ["same same same", "same same", { quality: 1.25, offset: 0 }],
676 + ["7cafe52bb8 cafe cafe", "cafe", { quality: 1, offset: 11 }],
677 + ["7cafe52bb8 7cafe52bb8", "7cafe52", { quality: 1, offset: 0 }],
678 + ["明天部署部署到生产环境", "部署", { quality: 1, offset: 2 }],
679 + ["gannet’s gannet's", "gannet's", { quality: 1, offset: 9 }],
680 + ["gannet's gannet’s", "gannet’s", { quality: 1, offset: 0 }],
681 + ] as const) {
682 + expect(scoreMessageMatch(text, query), `${query} in ${text}`).toEqual(expected);
683 + }
684 // Words written together as typed rank above the same words apart, and
685 // the snippet starts where they are together.
686 const together = "Run the tests, then vp run dev to check";
@@ -736,5 +756,14 @@ describe("scoreMessageMatch", () => {
756 expect(scoreMessageMatch("İ Enable fts and model", "fts")).toMatchObject({ offset: 9 });
757 expect(scoreMessageMatch("İ Enable fts and model", "model")?.quality).toBe(1);
758 expect(scoreMessageMatch("Trip to İstanbul", "İstanbul")).toEqual({ quality: 1, offset: 8 });
759 + for (const [text, query, expected] of [
760 + ["İ cafeteria Café CAFÉ", "cafe", { quality: 1, offset: 12 }],
761 + ["Café cafe", "cafe", { quality: 1, offset: 5 }],
762 + ["İ Café Café", "café", { quality: 1, offset: 2 }],
763 + ["İ Résumé Résumé", "resume", { quality: 1, offset: 2 }],
764 + ["İ résuméprefix résumé", "resume", { quality: 1, offset: 15 }],
765 + ] as const) {
766 + expect(scoreMessageMatch(text, query), `${query} in ${text}`).toEqual(expected);
767 + }
768 });
769 });
packages/shared/src/paletteSearch.ts
+10 -3
@@ -535,6 +535,7 @@ function findMessageToken(
535 text: string,
536 folded: string,
537 token: string,
538 + firstOnly: boolean,
539 ): { readonly at: ReadonlyArray<number>; readonly quality: number } | null {
540 let quality = 0;
541 let at: number[] = [];
@@ -559,7 +560,8 @@ function findMessageToken(
560 if (found > quality) {
561 quality = found;
562 at = [index];
562 - } else if (found === quality) {
563 + if (firstOnly && quality === 1) return { at, quality };
564 + } else if (!firstOnly && found === quality) {
565 at.push(index);
566 }
567 }
@@ -611,10 +613,15 @@ export function messageMatcher(query: string): (text: string) => MessageMatch |
613 let quality = 1;
614 const positions: Array<ReadonlyArray<number>> = [];
615 for (const { token, folded: foldedToken, nonAscii, mustStartWord, grades } of tokens) {
614 - let found = findMessageToken(text, plain, token);
616 + let found = findMessageToken(text, plain, token, tokens.length === 1);
617 // Accents typed or left out: "cafe" finds "Café", also after "cafeteria".
618 if (found?.quality !== 1 && foldedToken.length > 0 && (nonAscii || NON_ASCII.test(text))) {
617 - found = findMessageToken(text, (folded ??= foldInPlace(text)), foldedToken);
619 + found = findMessageToken(
620 + text,
621 + (folded ??= foldInPlace(text)),
622 + foldedToken,
623 + tokens.length === 1,
624 + );
625 }
626 if (found === null) return null;
627 if (found.quality === MID_WORD && mustStartWord) return null;