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

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

perf(server): skip non-image results in project icon searches (#32089)

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

Lakshmi Tanmay committed Nov 15, 2026 at 09:37 UTC e8d5c12890ac0cd7b49d91f9d9721ad01e53b4b1
2 files changed +143 -48
apps/server/src/workspace/WorkspaceSearchIndex.test.ts
+93 -38
@@ -9,6 +9,7 @@ import {
9 import * as NodeServices from "@effect/platform-node/NodeServices";
10 import { afterEach, expect, it } from "@effect/vitest";
11 import * as HostProcess from "@t3tools/shared/HostProcess";
12 +import { PROJECT_FAVICON_EXTENSIONS, isProjectFaviconPath } from "@t3tools/shared/projectFavicon";
13 import * as Deferred from "effect/Deferred";
14 import * as Effect from "effect/Effect";
15 import * as Fiber from "effect/Fiber";
@@ -40,44 +41,98 @@ function fileItem(relativePath: string): FileItem {
41 };
42 }
43
43 -it.effect("filters image searches before applying the result limit", () =>
44 - Effect.scoped(
45 - Effect.gen(function* () {
46 - const items = [
47 - ...Array.from({ length: 200 }, (_, index) => fileItem(`src/file-${index}.ts`)),
48 - fileItem("public/icon.svg"),
49 - ];
50 - const fileSearch = vi.fn(async () => ({
51 - ok: true as const,
52 - value: {
53 - items,
54 - scores: [],
55 - totalMatched: items.length,
56 - totalFiles: items.length,
57 - },
58 - }));
59 - const finder = {
60 - destroy: vi.fn(),
61 - waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
62 - getScanProgressAsync: async () => ({
63 - ok: true as const,
64 - value: { isScanning: false },
65 - }),
66 - watch: () => ({ ok: true as const, value: () => undefined }),
67 - fileSearchAsync: fileSearch,
68 - } as unknown as FileFinder;
69 - vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
70 -
71 - const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
72 - const resultWithoutKind = yield* searchIndex.search("", 200, undefined, true);
73 - const resultWithDirectoryKind = yield* searchIndex.search("", 200, "directory", true);
74 -
75 - expect(resultWithoutKind.entries).toEqual([{ kind: "file", path: "public/icon.svg" }]);
76 - expect(resultWithDirectoryKind.entries).toEqual([{ kind: "file", path: "public/icon.svg" }]);
77 - expect(fileSearch).toHaveBeenCalledTimes(2);
78 - expect(fileSearch).toHaveBeenCalledWith("", { pageSize: 25_002 });
79 - }),
80 - ).pipe(Effect.provide(testLayer)),
44 +it.live("image searches match the image subset of a file search without fetching every file", () =>
45 + Effect.gen(function* () {
46 + const fileSystem = yield* FileSystem.FileSystem;
47 + const path = yield* Path.Path;
48 + const vcsProcess = yield* VcsProcess.VcsProcess;
49 + const writeFiles = Effect.fn(function* (root: string, relativePaths: ReadonlyArray<string>) {
50 + for (const relativePath of relativePaths) {
51 + yield* fileSystem.makeDirectory(path.dirname(path.join(root, relativePath)), {
52 + recursive: true,
53 + });
54 + yield* fileSystem.writeFileString(path.join(root, relativePath), "");
55 + yield* fileSystem.utimes(path.join(root, relativePath), 0, 0);
56 + }
57 + });
58 +
59 + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-image-search-" });
60 + yield* vcsProcess.run({ operation: "test.gitInit", command: "git", args: ["init"], cwd });
61 + yield* fileSystem.writeFileString(path.join(cwd, ".git/info/exclude"), "hidden/\n");
62 + yield* writeFiles(cwd, [
63 + ...Array.from({ length: 220 }, (_, index) => `assets/source-${index}.ts`),
64 + ...PROJECT_FAVICON_EXTENSIONS.flatMap((extension) => [
65 + `assets/logo${extension}`,
66 + `other/logo${extension.toUpperCase()}`,
67 + ]),
68 + "assets/logo.ts",
69 + "assets/logotype.png",
70 + "assetss/logotype.png",
71 + "assets/images/logo.PNG",
72 + "assets/foo{bar}.png",
73 + "assets/[id].png",
74 + "hidden/logo.png",
75 + ]);
76 +
77 + const fileSearch = vi.spyOn(FileFinder.prototype, "fileSearchAsync");
78 + const searchIndex = yield* WorkspaceSearchIndex.make(cwd);
79 + yield* searchIndex.search("", 200, undefined, true);
80 + const nativeResult = yield* Effect.promise(
81 + (): ReturnType<FileFinder["fileSearchAsync"]> => fileSearch.mock.results[0]?.value,
82 + );
83 + if (!nativeResult.ok) return yield* Effect.die(nativeResult.error);
84 + expect(nativeResult.value.items.length).toBeLessThanOrEqual(201);
85 +
86 + // The image glob must not change fff's parse of fuzzy paths, globs, negations or locations.
87 + for (const query of [
88 + "",
89 + "logo",
90 + "lgo",
91 + "*.png",
92 + "!*.png",
93 + "assets/**",
94 + "!assets/",
95 + "assets*/",
96 + "/assets",
97 + "assets/",
98 + "logo:12",
99 + "/assets/logotype.png",
100 + "assets/images/",
101 + "logo *.svg !other",
102 + "foo{bar}.png",
103 + "[id].png",
104 + "*.{png,svg}",
105 + "hidden/",
106 + ]) {
107 + const ordinary = yield* searchIndex.search(query, 1000, "file");
108 + const expected = ordinary.entries.filter((entry) => isProjectFaviconPath(entry.path));
109 + for (const limit of [1, 200]) {
110 + const images = yield* searchIndex.search(query, limit, "directory", true);
111 + expect(images, `query=${query} limit=${limit}`).toEqual({
112 + entries: expected.slice(0, limit),
113 + truncated: expected.length > limit,
114 + });
115 + }
116 + }
117 +
118 + // Past 15,000 matches fff drops its filename fallback, which the image glob would otherwise
119 + // reorder: "x/logo/logo.png" must keep its place relative to "a/xxlogo.png".
120 + const largeCwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-image-rank-" });
121 + yield* writeFiles(largeCwd, [
122 + ...Array.from({ length: 14_998 }, (_, index) => `b/logo-${index}.png`),
123 + "x/logo/source.ts",
124 + "a/xxlogo.png",
125 + "x/logo/logo.png",
126 + ]);
127 + const largeIndex = yield* WorkspaceSearchIndex.make(largeCwd);
128 + const pairPaths = ["a/xxlogo.png", "x/logo/logo.png"];
129 + const ordinary = yield* largeIndex.search("logo", 20_000, "file");
130 + const images = yield* largeIndex.search("logo", 20_000, "file", true);
131 + expect(images.entries).toHaveLength(15_000);
132 + expect(images.entries.filter((entry) => pairPaths.includes(entry.path))).toEqual(
133 + ordinary.entries.filter((entry) => pairPaths.includes(entry.path)),
134 + );
135 + }).pipe(Effect.scoped, Effect.provide(testLayer)),
136 );
137
138 it.effect("fetches past a first page of Git-ignored results", () =>
apps/server/src/workspace/WorkspaceSearchIndex.ts
+50 -10
@@ -32,7 +32,7 @@ import type {
32 ProjectSearchEntriesResult,
33 } from "@t3tools/contracts";
34 import * as HostProcess from "@t3tools/shared/HostProcess";
35 -import { isProjectFaviconPath } from "@t3tools/shared/projectFavicon";
35 +import { PROJECT_FAVICON_EXTENSIONS, isProjectFaviconPath } from "@t3tools/shared/projectFavicon";
36 import { scoreSubsequenceMatch } from "@t3tools/shared/searchRanking";
37
38 import * as VcsProcess from "../vcs/VcsProcess.ts";
@@ -42,6 +42,14 @@ import { type IgnoreDecision, type IgnoreFile, parseIgnoreFile } from "./IgnoreP
42 const requireForFff = NodeModule.createRequire(import.meta.url);
43 const { FileFinder } = requireForFff("@ff-labs/fff-node") as typeof import("@ff-labs/fff-node");
44
45 +// A glob intersects existing extension constraints; fff ORs bare `*.ext` constraints together.
46 +const IMAGE_SEARCH_CONSTRAINT = `**/*.{${PROJECT_FAVICON_EXTENSIONS.map((extension) =>
47 + extension.slice(1).replace(/[a-z]/g, (letter) => `[${letter}${letter.toUpperCase()}]`),
48 +).join(",")}}`;
49 +
50 +// fff 0.11 disables filename fallback above this match count per base/overflow segment.
51 +const FFF_FILENAME_FALLBACK_MAX_MATCHES = 15_000;
52 +
53 const WORKSPACE_INDEX_MAX_ENTRIES = 25_000;
54 const WORKSPACE_INDEX_PAGE_SIZE = WORKSPACE_INDEX_MAX_ENTRIES + 2;
55 const WORKSPACE_INDEX_SCAN_TIMEOUT = "15 seconds";
@@ -175,6 +183,14 @@ function toDirectoryEntry(item: DirItem, hostIsWindows: boolean): ProjectEntry |
183 return normalizedPath ? { path: normalizedPath, kind: "directory" } : null;
184 }
185
186 +function isLoneFuzzyPath(query: string): boolean {
187 + const trimmed = query.trim();
188 + return (
189 + !/\s/u.test(trimmed) &&
190 + (trimmed.startsWith("/") || (trimmed.endsWith("/") && !/^[*!\\]/u.test(trimmed)))
191 + );
192 +}
193 +
194 type SearchPage<I> = { readonly items: ReadonlyArray<I>; readonly totalMatched: number };
195
196 function mapFileSearchResult(
@@ -841,13 +857,13 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
857 });
858
859 // Grows the first page until hidden entries can no longer crowd visible ones out of it.
844 - const searchVisible = Effect.fn("WorkspaceSearchIndex.searchVisible")(function* <I>(
860 + const searchVisible = Effect.fn("WorkspaceSearchIndex.searchVisible")(function* <I, Metadata>(
861 query: string,
862 pageSize: number,
863 operation: "directorySearch" | "fileSearch" | "mixedSearch",
848 - execute: (pageSize: number) => Promise<Result<SearchPage<I>>>,
864 + execute: (pageSize: number) => Promise<Result<SearchPage<I> & Metadata>>,
865 pathOf: (item: I) => string,
850 - ): Effect.fn.Return<SearchPage<I>, WorkspaceSearchIndexSearchFailed> {
866 + ): Effect.fn.Return<SearchPage<I> & Metadata, WorkspaceSearchIndexSearchFailed> {
867 let size = pageSize;
868 while (true) {
869 const fetchSize = size;
@@ -862,6 +878,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
878 fetchSize >= WORKSPACE_INDEX_PAGE_SIZE
879 ) {
880 return {
881 + ...result,
882 items,
883 totalMatched: result.totalMatched - (result.items.length - items.length),
884 };
@@ -945,17 +962,40 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
962 );
963 // Index results that are filtered-out links get dropped in the merge, so fetch past them.
964 const indexLimit = limit + linkPaths.size - symlinks.length;
948 - const pageSize = imageOnly ? WORKSPACE_INDEX_PAGE_SIZE : Math.max(1, indexLimit + 1);
965 + const pageSize = Math.max(1, indexLimit + 1);
966 const merge = (indexed: ProjectSearchEntriesResult) =>
967 mergeSymlinkMatches(indexed, linkPaths, symlinks, query, limit);
968 if (kind === "file" || imageOnly) {
952 - const result = yield* searchVisible(
953 - query,
969 + const searchFiles = (fileQuery: string, size: number) =>
970 + searchVisible(
971 + query,
972 + size,
973 + "fileSearch",
974 + (fetchSize) => finder.fileSearchAsync(fileQuery, { pageSize: fetchSize }),
975 + (item: FileItem) => item.relativePath,
976 + );
977 + // Adding a constraint changes fff's lone-path parse; offset pages can skip tied matches.
978 + if (imageOnly && isLoneFuzzyPath(query)) {
979 + const result = yield* searchFiles(query, WORKSPACE_INDEX_PAGE_SIZE);
980 + return merge(mapFileSearchResult(result, indexLimit, hostIsWindows, true));
981 + }
982 + let result = yield* searchFiles(
983 + imageOnly ? `${query} ${IMAGE_SEARCH_CONSTRAINT}` : query,
984 pageSize,
955 - "fileSearch",
956 - (size) => finder.fileSearchAsync(query, { pageSize: size }),
957 - (item) => item.relativePath,
985 );
986 + if (
987 + imageOnly &&
988 + query.trim() &&
989 + result.totalFiles > FFF_FILENAME_FALLBACK_MAX_MATCHES &&
990 + result.totalMatched > 0
991 + ) {
992 + const unfiltered = yield* runSearch(query, 1, "fileSearch", () =>
993 + finder.fileSearchAsync(query, { pageSize: 1 }),
994 + );
995 + if (unfiltered.totalMatched > FFF_FILENAME_FALLBACK_MAX_MATCHES) {
996 + result = yield* searchFiles(query, WORKSPACE_INDEX_PAGE_SIZE);
997 + }
998 + }
999 return merge(mapFileSearchResult(result, indexLimit, hostIsWindows, imageOnly));
1000 }
1001 if (kind === "directory") {