The best way to control Claude Code, Codex and more from any computer or phone
perf(clients): thread search reuses per-thread fields across keystrokes (#26936)
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Hieu Le committed
Nov 1, 2026 at 22:02 UTC
9504a09f089ad35c87d94a72c7611853dcbc915f
3 files changed
+142
-56
packages/client-runtime/src/state/threadSearch.test.ts
+4
@@ -191,6 +191,10 @@ it("ranks recent threads by this client's own latest open", () => {
191
threadSearchSubject({ ...thread, id: ThreadId.make("thread-unrecorded"), opens: null }, null)
192
.opens,
193
).toEqual({ openedAt: 6_000, score: 1, picks: [] });
194
+ // A subject built before the open does not hide it.
195
+ claimThreadOpen(envA, "thread-local", 7_000);
196
+ expect(threadSearchSubject(thread, null).opens?.openedAt).toBe(7_000);
197
+ expect(threadSearchSubject(thread, "Project").projectTitle).toBe("Project");
198
});
199
200
it("counts recency from what the user wrote, else from when the thread began", () => {
packages/client-runtime/src/state/threadSearch.ts
+37
@@ -29,6 +29,7 @@ const sentThreadOpens = new Map<string, number>();
29
// When this client last opened each thread, sent or not, so recent threads
30
// follow quick switches the server merges.
31
const localThreadOpens = new Map<string, number>();
32
+let localThreadOpensVersion = 0;
33
/**
34
* Records that this client opened a thread, and says whether to send the open
35
* to the server now.
@@ -36,6 +37,7 @@ const localThreadOpens = new Map<string, number>();
37
export function claimThreadOpen(environmentId: EnvironmentId, threadId: string, now: number) {
38
const key = `${environmentId}:${threadId}`;
39
localThreadOpens.set(key, now);
40
+ localThreadOpensVersion += 1;
41
if (now - (sentThreadOpens.get(key) ?? Number.NEGATIVE_INFINITY) < THREAD_OPEN_DEDUPE_MS) {
42
return false;
43
}
@@ -208,6 +210,41 @@ export function threadSearchSubject(
210
thread: ThreadSearchShell,
211
projectTitle: string | null | undefined,
212
environmentLabel?: string | null,
213
+): ThreadSearchSubject {
214
+ // A keystroke ranks the same shells again, so a subject is built once per shell.
215
+ const cached = subjectCache.get(thread);
216
+ if (
217
+ cached !== undefined &&
218
+ cached.projectTitle === projectTitle &&
219
+ cached.environmentLabel === environmentLabel &&
220
+ cached.opensVersion === localThreadOpensVersion
221
+ ) {
222
+ return cached.subject;
223
+ }
224
+ const subject = buildThreadSearchSubject(thread, projectTitle, environmentLabel);
225
+ subjectCache.set(thread, {
226
+ projectTitle,
227
+ environmentLabel,
228
+ opensVersion: localThreadOpensVersion,
229
+ subject,
230
+ });
231
+ return subject;
232
+}
233
+
234
+const subjectCache = new WeakMap<
235
+ ThreadSearchShell,
236
+ {
237
+ readonly projectTitle: string | null | undefined;
238
+ readonly environmentLabel: string | null | undefined;
239
+ readonly opensVersion: number;
240
+ readonly subject: ThreadSearchSubject;
241
+ }
242
+>();
243
+
244
+function buildThreadSearchSubject(
245
+ thread: ThreadSearchShell,
246
+ projectTitle: string | null | undefined,
247
+ environmentLabel: string | null | undefined,
248
): ThreadSearchSubject {
249
// What the user wrote, not agent wakes or task notices in the user role. A
250
// thread the user never wrote in counts from its creation; an older server
packages/shared/src/paletteSearch.ts
+101
-56
@@ -202,14 +202,17 @@ function matchToken(
202
): TokenMatch | null {
203
if (token.length === 0) return null;
204
let best: TokenMatch | null = null;
205
- const ownBoundary = !ALPHANUMERIC.test(token[0]!) && !SYMBOL.test(token[0]!);
205
+ let ownBoundary: boolean | undefined;
206
for (
207
let index = field.text.indexOf(token);
208
index !== -1;
209
index = field.text.indexOf(token, index + 1)
210
) {
211
// A token that opens with punctuation (".ts") brings its own boundary, as in messages.
212
- if (field.wordStarts[index] || ownBoundary) {
212
+ if (
213
+ field.wordStarts[index] ||
214
+ (ownBoundary ??= !ALPHANUMERIC.test(token[0]!) && !SYMBOL.test(token[0]!))
215
+ ) {
216
// Whole when the match ends where a word ends: "t3c" is a prefix of
217
// "t3code", ".ts" whole in "index.ts" but a prefix in "parser.tsx".
218
const end = index + token.length;
@@ -275,9 +278,12 @@ function matchToken(
278
* Allow them at its start ("t3co"), before three letters ("gomod"), or a whole word ("vscode").
279
*/
280
function matchJoinedWords(field: SearchField, token: string): TokenMatch | null {
278
- const firstWord = field.wordStarts.indexOf(1);
279
- for (let start = 0; start < field.text.length; start++) {
280
- if (!field.wordStarts[start] || field.text[start] !== token[0]) continue;
281
+ for (
282
+ let start = field.text.indexOf(token[0]!);
283
+ start !== -1;
284
+ start = field.text.indexOf(token[0]!, start + 1)
285
+ ) {
286
+ if (!field.wordStarts[start]) continue;
287
const runs: SearchRange[] = [];
288
let at = start;
289
let rest = token;
@@ -300,7 +306,7 @@ function matchJoinedWords(field: SearchField, token: string): TokenMatch | null
306
if (
307
last.end === wordEnd(field, last.start) ||
308
last.end - last.start >= 3 ||
303
- start === firstWord ||
309
+ start === field.wordStarts.indexOf(1) ||
310
runs.slice(0, -1).every((run) => run.end - run.start >= 3)
311
) {
312
return { score: JOINED_WORDS, start, end: last.end, letters: runs };
@@ -311,6 +317,8 @@ function matchJoinedWords(field: SearchField, token: string): TokenMatch | null
317
318
interface WeightedSearchField {
319
readonly value: string;
320
+ /** `value` folded; set on first use, so fields kept across keystrokes fold once. */
321
+ prepared?: SearchField;
322
readonly weight: number;
323
/** Only a whole-word match counts, as for a short query against keywords. */
324
readonly wholeWords?: boolean;
@@ -342,6 +350,10 @@ interface SearchFieldsMatch {
350
readonly primaryRanges: ReadonlyArray<SearchRange>;
351
}
352
353
+function preparedField(weighted: WeightedSearchField): SearchField {
354
+ return (weighted.prepared ??= prepareSearchField(weighted.value));
355
+}
356
+
357
const FILLER_WORDS = new Set(["a", "an", "and", "for", "in", "of", "on", "the", "to", "with"]);
358
359
// Sorted, with overlapping ranges joined.
@@ -358,6 +370,25 @@ function mergeRanges(ranges: ReadonlyArray<SearchRange>): SearchRange[] {
370
return merged;
371
}
372
373
+// A keystroke scores every thread for one query, so its tokens are split once.
374
+let lastQuery:
375
+ | { readonly query: string; readonly tokens: string[]; readonly fillerOptional: boolean }
376
+ | undefined;
377
+
378
+function queryTokens(normalizedQuery: string) {
379
+ if (lastQuery?.query !== normalizedQuery) {
380
+ const tokens = normalizedQuery.split(" ").filter((token) => token.length > 0);
381
+ // A phrase-style query ("fix the sidebar") carries filler words a name may
382
+ // leave out; they count where they match but are not required.
383
+ lastQuery = {
384
+ query: normalizedQuery,
385
+ tokens,
386
+ fillerOptional: tokens.some((token) => !FILLER_WORDS.has(token)),
387
+ };
388
+ }
389
+ return lastQuery;
390
+}
391
+
392
/**
393
* Every query token must match some field. Quality is the mean of each token's
394
* best weighted match, plus a bonus when the first field holds the whole query
@@ -369,23 +400,10 @@ function matchSearchFields(
400
options?: { readonly typos?: boolean },
401
): SearchFieldsMatch | null {
402
if (normalizedQuery.length === 0) return null;
372
- const prepared = fields
373
- .filter((field) => field.value.length > 0)
374
- .map((field) => ({
375
- field: prepareSearchField(field.value),
376
- weight: field.weight,
377
- wholeWords: field.wholeWords ?? false,
378
- keyword: field.keyword ?? false,
379
- context: field.context ?? false,
380
- allowMidWord: field.midWord ?? false,
381
- }));
382
- const primary = fields[0]?.value ? prepared[0]?.field : undefined;
403
+ const primary = fields[0]?.value ? preparedField(fields[0]) : undefined;
404
// An empty token would match everywhere, and loop forever in `matchToken`.
384
- const tokens = normalizedQuery.split(" ").filter((token) => token.length > 0);
405
+ const { tokens, fillerOptional } = queryTokens(normalizedQuery);
406
if (tokens.length === 0) return null;
386
- // A phrase-style query ("fix the sidebar") carries filler words a name may
387
- // leave out; they count where they match but are not required.
388
- const fillerOptional = tokens.some((token) => !FILLER_WORDS.has(token));
407
const primaryRanges: SearchRange[] = [];
408
let total = 0;
409
let counted = 0;
@@ -394,11 +412,14 @@ function matchSearchFields(
412
let best = 0;
413
let bestPrimary: TokenMatch | null = null;
414
let atWordStart = false;
397
- for (const { field, weight, wholeWords, keyword, context, allowMidWord } of prepared) {
415
+ for (const weighted of fields) {
416
+ if (weighted.value.length === 0) continue;
417
+ const { weight, wholeWords, keyword, context, midWord } = weighted;
418
+ const field = preparedField(weighted);
419
if (best >= weight && field !== primary && atWordStart) continue;
420
const typos = (options?.typos ?? false) && !keyword;
421
// Joined words only count in the name.
401
- const match = matchToken(field, token, typos, field === primary, allowMidWord);
422
+ const match = matchToken(field, token, typos, field === primary, midWord ?? false);
423
if (match === null || match.score < (wholeWords ? WHOLE_WORD : keyword ? 0.7 : 0)) continue;
424
if (field === primary) bestPrimary = match;
425
if (match.score >= 0.7 && !context) atWordStart = true;
@@ -801,6 +822,9 @@ function threadUsageBoost(thread: ThreadSearchSubject, now: number): number {
822
823
const REFERENCE_PREFIX_MIN = 8;
824
825
+// A number is never cut short: ".../pull/2288" is not pull request 22889.
826
+const notCut = (text: string, end: number) => !DIGIT.test(text[end] ?? "");
827
+
828
// IDs and URLs have many word starts, so field matching would let short queries hit every thread.
829
// Require a long prefix, or any long part containing a digit. Both are ASCII; lowercasing suffices.
830
function matchReference(
@@ -808,14 +832,8 @@ function matchReference(
832
thread: ThreadSearchSubject,
833
): SearchFieldsMatch | null {
834
if (normalizedQuery.length < REFERENCE_PREFIX_MIN) return null;
811
- const references = [
812
- ...(thread.id ? [thread.id] : []),
813
- ...(thread.pullRequests ?? []).flatMap((pullRequest) => pullRequest.references ?? []),
814
- ];
835
const anywhere = DIGIT.test(normalizedQuery);
816
- // A number is never cut short: ".../pull/2288" is not pull request 22889.
817
- const notCut = (text: string, end: number) => !DIGIT.test(text[end] ?? "");
818
- return references.some((reference) => {
836
+ const matches = (reference: string) => {
837
const folded = reference.toLowerCase();
838
const at = folded.indexOf(normalizedQuery);
839
// A pasted link may go on past the reference: ".../pull/22889/files".
@@ -823,7 +841,9 @@ function matchReference(
841
? (at !== -1 && notCut(folded, at + normalizedQuery.length)) ||
842
(normalizedQuery.startsWith(folded) && notCut(normalizedQuery, folded.length))
843
: at === 0;
826
- })
844
+ };
845
+ return (thread.id && matches(thread.id)) ||
846
+ thread.pullRequests?.some((pullRequest) => pullRequest.references?.some(matches))
847
? { quality: 0.6, strong: true, primaryRanges: [] }
848
: null;
849
}
@@ -934,6 +954,45 @@ function matchHint(
954
return rest && rest !== normalizedQuery ? hintFor(rest) : undefined;
955
}
956
957
+// A keystroke scores each thread's fields again, so they are listed once per subject.
958
+const threadFieldsCache = new WeakMap<ThreadSearchSubject, ReadonlyArray<WeightedSearchField>>();
959
+
960
+function threadSearchFields(thread: ThreadSearchSubject): ReadonlyArray<WeightedSearchField> {
961
+ const cached = threadFieldsCache.get(thread);
962
+ if (cached) return cached;
963
+ const fields: WeightedSearchField[] = [{ value: thread.title, weight: 1, midWord: true }];
964
+ for (const pullRequest of thread.pullRequests ?? []) {
965
+ fields.push({ value: `#${pullRequest.number}`, weight: 1 });
966
+ if (pullRequest.title) fields.push({ value: pullRequest.title, weight: 0.5 });
967
+ }
968
+ const branch = searchableBranch(thread.branch);
969
+ if (branch) fields.push({ value: branch, weight: 0.6 });
970
+ if (thread.projectTitle) {
971
+ fields.push({ value: thread.projectTitle, weight: 0.25, context: true });
972
+ }
973
+ if (thread.environmentLabel) {
974
+ fields.push({ value: thread.environmentLabel, weight: 0.25, context: true });
975
+ }
976
+ threadFieldsCache.set(thread, fields);
977
+ return fields;
978
+}
979
+
980
+let lastFieldQuery: { readonly query: string; readonly fieldQuery: string } | undefined;
981
+
982
+// Strip pull request prefixes and the worktree prefix so queries match the stored fields.
983
+function threadFieldQuery(normalizedQuery: string): string {
984
+ if (lastFieldQuery?.query !== normalizedQuery) {
985
+ lastFieldQuery = {
986
+ query: normalizedQuery,
987
+ fieldQuery: normalizedQuery
988
+ .replace(/^(?:pr|pull request|pull)\s*(?=#?\d)/, "")
989
+ .replace(/^#\s*/, "")
990
+ .replace(WORKTREE_BRANCH_PREFIX_PATTERN, ""),
991
+ };
992
+ }
993
+ return lastFieldQuery.fieldQuery;
994
+}
995
+
996
/**
997
* Scores a thread for a normalized query, or null when it does not match.
998
* Titles and pull request numbers count fully, pull request titles and
@@ -952,24 +1011,8 @@ export function scoreThreadSearch(
1011
readonly picks?: boolean;
1012
},
1013
): ThreadSearchMatch | null {
955
- const fields: WeightedSearchField[] = [{ value: thread.title, weight: 1, midWord: true }];
956
- for (const pullRequest of thread.pullRequests ?? []) {
957
- fields.push({ value: `#${pullRequest.number}`, weight: 1 });
958
- if (pullRequest.title) fields.push({ value: pullRequest.title, weight: 0.5 });
959
- }
960
- const branch = searchableBranch(thread.branch);
961
- if (branch) fields.push({ value: branch, weight: 0.6 });
962
- if (thread.projectTitle) {
963
- fields.push({ value: thread.projectTitle, weight: 0.25, context: true });
964
- }
965
- if (thread.environmentLabel) {
966
- fields.push({ value: thread.environmentLabel, weight: 0.25, context: true });
967
- }
968
- // Strip pull request prefixes and the worktree prefix so queries match the stored fields.
969
- const fieldQuery = normalizedQuery
970
- .replace(/^(?:pr|pull request|pull)\s*(?=#?\d)/, "")
971
- .replace(/^#\s*/, "")
972
- .replace(WORKTREE_BRANCH_PREFIX_PATTERN, "");
1014
+ const fields = threadSearchFields(thread);
1015
+ const fieldQuery = threadFieldQuery(normalizedQuery);
1016
const fieldMatch = matchSearchFields(fieldQuery, fields, options);
1017
const referenceMatch = fieldMatch ? null : matchReference(normalizedQuery, thread);
1018
const match = fieldMatch ?? referenceMatch;
@@ -1102,13 +1145,15 @@ export function rankSearchGroups<
1145
score: (item: T, options: { readonly typos: boolean }, group: G) => M | null,
1146
): Array<{ readonly group: G; readonly ranked: ReadonlyArray<RankedSearchItem<T, M>> }> {
1147
const rank = (typos: boolean) =>
1105
- groups.map(({ group, items }) => ({
1106
- group,
1107
- ranked: items.flatMap((item) => {
1108
- const match = score(item, { typos }, group);
1109
- return match === null ? [] : [{ item, match }];
1110
- }),
1111
- }));
1148
+ groups.map(({ group, items }) => {
1149
+ const options = { typos };
1150
+ const ranked: Array<RankedSearchItem<T, M>> = [];
1151
+ for (const item of items) {
1152
+ const match = score(item, options, group);
1153
+ if (match !== null) ranked.push({ item, match });
1154
+ }
1155
+ return { group, ranked };
1156
+ });
1157
let ranked = rank(false);
1158
if (!ranked.some((entry) => entry.ranked.some(({ match }) => match.strong))) {
1159
ranked = rank(true);