@devries-cameron20 / t3code-1 / commits / 8cd8a3ca5a
Fork 0

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

fix(server): file search no longer freezes the server while fff's watcher reads a large file (#36273)

Julius Marminge committed Nov 13, 2026 at 11:08 UTC 8cd8a3ca5ab36c5ce79d166f5a226f7aa644e093
5 files changed +274 -63
apps/server/src/workspace/WorkspaceEntries.test.ts
+4 -1
@@ -963,10 +963,13 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
963 if (created.ok) {
964 const finder = created.value;
965 finders += 1;
966 - // The synchronous rescan waits on fff's picker lock on the event loop.
966 + // The synchronous rescan and grep wait on fff's picker lock on the event loop.
967 finder.scanFiles = () => {
968 throw new Error("refresh must not rescan on the JS thread");
969 };
970 + finder.grep = () => {
971 + throw new Error("search must not grep on the JS thread");
972 + };
973 const scanFilesAsync = finder.scanFilesAsync.bind(finder);
974 finder.scanFilesAsync = () => {
975 rescans += 1;
apps/server/src/workspace/WorkspaceSearchIndex.test.ts
+35 -16
@@ -47,7 +47,7 @@ it.effect("filters image searches before applying the result limit", () =>
47 ...Array.from({ length: 200 }, (_, index) => fileItem(`src/file-${index}.ts`)),
48 fileItem("public/icon.svg"),
49 ];
50 - const fileSearch = vi.fn(() => ({
50 + const fileSearch = vi.fn(async () => ({
51 ok: true as const,
52 value: {
53 items,
@@ -59,9 +59,12 @@ it.effect("filters image searches before applying the result limit", () =>
59 const finder = {
60 destroy: vi.fn(),
61 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
62 - isScanning: () => false,
62 + getScanProgressAsync: async () => ({
63 + ok: true as const,
64 + value: { isScanning: false },
65 + }),
66 watch: () => ({ ok: true as const, value: () => undefined }),
64 - fileSearch,
67 + fileSearchAsync: fileSearch,
68 } as unknown as FileFinder;
69 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
70
@@ -95,7 +98,7 @@ it.effect("fetches past a first page of Git-ignored results", () =>
98 fileItem("hidden/c.ts"),
99 fileItem("src/a.ts"),
100 ];
98 - const fileSearch = vi.fn((_query: string, options?: { pageSize?: number }) => ({
101 + const fileSearch = vi.fn(async (_query: string, options?: { pageSize?: number }) => ({
102 ok: true as const,
103 value: {
104 items: items.slice(0, options?.pageSize),
@@ -107,9 +110,12 @@ it.effect("fetches past a first page of Git-ignored results", () =>
110 const finder = {
111 destroy: vi.fn(),
112 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
110 - isScanning: () => false,
113 + getScanProgressAsync: async () => ({
114 + ok: true as const,
115 + value: { isScanning: false },
116 + }),
117 watch: () => ({ ok: true as const, value: () => undefined }),
112 - fileSearch,
118 + fileSearchAsync: fileSearch,
119 } as unknown as FileFinder;
120 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
121
@@ -131,9 +137,12 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
137 const finder = {
138 destroy: vi.fn(),
139 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
134 - isScanning: () => false,
140 + getScanProgressAsync: async () => ({
141 + ok: true as const,
142 + value: { isScanning: false },
143 + }),
144 watch: () => ({ ok: true as const, value: () => undefined }),
136 - fileSearch: vi.fn(() => ({
145 + fileSearchAsync: vi.fn(async () => ({
146 ok: true as const,
147 value: { items, scores: [], totalMatched: 1, totalFiles: 1 },
148 })),
@@ -184,7 +193,7 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
193 filteredFileCount: 1,
194 nextCursor: cursor,
195 });
187 - const grep = vi.fn((_query: string, options?: GrepOptions) =>
196 + const grep = vi.fn(async (_query: string, options?: GrepOptions) =>
197 options?.cursor
198 ? { ok: true as const, value: grepResult("needle", [[0, 6]], null) }
199 : {
@@ -195,9 +204,12 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
204 const finder = {
205 destroy: vi.fn(),
206 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
198 - isScanning: () => false,
207 + getScanProgressAsync: async () => ({
208 + ok: true as const,
209 + value: { isScanning: false },
210 + }),
211 watch: () => ({ ok: true as const, value: () => undefined }),
200 - grep,
212 + grepAsync: grep,
213 } as unknown as FileFinder;
214 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
215
@@ -241,7 +253,7 @@ function fakeSymlinkFinder(
253 let watchFailures = options.watchFailures ?? 0;
254 // fff's pageIndex is an offset into the results, not a page number.
255 const directorySearch = vi.fn(
244 - (_query: string, searchOptions?: { pageIndex?: number; pageSize?: number }) => {
256 + async (_query: string, searchOptions?: { pageIndex?: number; pageSize?: number }) => {
257 const offset = searchOptions?.pageIndex ?? 0;
258 const items = directories.slice(offset, offset + (searchOptions?.pageSize ?? 100));
259 return {
@@ -260,7 +272,14 @@ function fakeSymlinkFinder(
272 const finder = {
273 destroy: vi.fn(),
274 waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
263 - isScanning: () => false,
275 + // fff's isScanning waits on the picker lock on the JS thread.
276 + isScanning: () => {
277 + throw new Error("isScanning must not run on the JS thread");
278 + },
279 + getScanProgressAsync: async () => ({
280 + ok: true as const,
281 + value: { isScanning: false },
282 + }),
283 watch: (callback: (events: ReadonlyArray<WatchEvent>) => void) => {
284 if (watchFailures > 0) {
285 watchFailures -= 1;
@@ -269,12 +288,12 @@ function fakeSymlinkFinder(
288 watchers.push(callback);
289 return { ok: true as const, value: () => undefined };
290 },
272 - directorySearch,
273 - fileSearch: () => {
291 + directorySearchAsync: directorySearch,
292 + fileSearchAsync: async () => {
293 const items = indexedItems.flatMap((entry) => (entry.type === "file" ? [entry.item] : []));
294 return { ok: true as const, value: { items, scores: [], totalMatched: items.length } };
295 },
277 - mixedSearch: () => ({
296 + mixedSearchAsync: async () => ({
297 ok: true as const,
298 value: { items: indexedItems, totalMatched: indexedItems.length },
299 }),
apps/server/src/workspace/WorkspaceSearchIndex.ts
+28 -16
@@ -579,22 +579,32 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
579 }),
580 );
581
582 - const hasIncompleteInitialScan = () => initialScanTimedOut && finder.isScanning();
582 + // fff's isScanning takes the picker lock too, so progress is read off the JS thread.
583 + const isScanning = Effect.promise(async () => {
584 + try {
585 + const progress = await finder.getScanProgressAsync();
586 + return !progress.ok || progress.value.isScanning;
587 + } catch {
588 + return true;
589 + }
590 + });
591 + const hasIncompleteInitialScan = () => (initialScanTimedOut ? isScanning : Effect.succeed(false));
592
593 const runSearch = Effect.fn("WorkspaceSearchIndex.runSearch")(function* <A>(
594 query: string,
595 pageSize: number,
596 operation: "directorySearch" | "fileSearch" | "grep" | "mixedSearch",
588 - execute: () => Result<A>,
597 + execute: () => Promise<Result<A>>,
598 ): Effect.fn.Return<A, WorkspaceSearchIndexSearchFailed> {
590 - const result = yield* Effect.try({
599 + // fff's searches wait on its picker lock, which its watcher can hold for minutes.
600 + const result = yield* Effect.tryPromise({
601 try: execute,
602 catch: (cause) =>
603 new WorkspaceSearchIndexSearchFailed({
604 cwd,
605 queryLength: query.length,
606 pageSize,
597 - reason: `FileFinder.${operation} threw unexpectedly.`,
607 + reason: `FileFinder.${operation} rejected unexpectedly.`,
608 cause,
609 }),
610 });
@@ -616,9 +626,12 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
626 const directories: string[] = [];
627 // fff's pageIndex is an offset into the results, not a page number.
628 for (let pageIndex = 0; ; pageIndex += SYMLINK_DIRECTORY_PAGE_SIZE) {
619 - const result = yield* Effect.sync(() => {
629 + const result = yield* Effect.promise(async () => {
630 try {
621 - return finder.directorySearch("", { pageIndex, pageSize: SYMLINK_DIRECTORY_PAGE_SIZE });
631 + return await finder.directorySearchAsync("", {
632 + pageIndex,
633 + pageSize: SYMLINK_DIRECTORY_PAGE_SIZE,
634 + });
635 } catch {
636 return undefined;
637 }
@@ -629,7 +642,6 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
642 if (directory) directories.push(directory);
643 }
644 if (result.value.items.length < SYMLINK_DIRECTORY_PAGE_SIZE) return directories;
632 - yield* Effect.yieldNow;
645 }
646 },
647 );
@@ -682,7 +694,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
694 });
695
696 const scanSymlinkEntries = Effect.fn("WorkspaceSearchIndex.scanSymlinkEntries")(function* () {
685 - const indexComplete = !finder.isScanning();
697 + const indexComplete = !(yield* isScanning);
698 const directories = yield* listIndexedDirectories();
699 const scanned = yield* Effect.forEach(
700 ["", ...directories],
@@ -833,7 +845,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
845 query: string,
846 pageSize: number,
847 operation: "directorySearch" | "fileSearch" | "mixedSearch",
836 - execute: (pageSize: number) => Result<SearchPage<I>>,
848 + execute: (pageSize: number) => Promise<Result<SearchPage<I>>>,
849 pathOf: (item: I) => string,
850 ): Effect.fn.Return<SearchPage<I>, WorkspaceSearchIndexSearchFailed> {
851 let size = pageSize;
@@ -895,12 +907,12 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
907
908 const list: WorkspaceSearchIndex["Service"]["list"] = Effect.fn("WorkspaceSearchIndex.list")(
909 function* () {
898 - const incompleteBeforeQuery = hasIncompleteInitialScan();
910 + const incompleteBeforeQuery = yield* hasIncompleteInitialScan();
911 const result = yield* searchVisible(
912 "",
913 WORKSPACE_INDEX_PAGE_SIZE,
914 "mixedSearch",
903 - (pageSize) => finder.mixedSearch("", { pageSize }),
915 + (pageSize) => finder.mixedSearchAsync("", { pageSize }),
916 mixedItemPath,
917 );
918 const mapped = mapMixedSearchResult(result, WORKSPACE_INDEX_MAX_ENTRIES, hostIsWindows);
@@ -941,7 +953,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
953 query,
954 pageSize,
955 "fileSearch",
944 - (size) => finder.fileSearch(query, { pageSize: size }),
956 + (size) => finder.fileSearchAsync(query, { pageSize: size }),
957 (item) => item.relativePath,
958 );
959 return merge(mapFileSearchResult(result, indexLimit, hostIsWindows, imageOnly));
@@ -951,7 +963,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
963 query,
964 pageSize,
965 "directorySearch",
954 - (size) => finder.directorySearch(query, { pageSize: size }),
966 + (size) => finder.directorySearchAsync(query, { pageSize: size }),
967 (item) => item.relativePath,
968 );
969 return merge(mapDirectorySearchResult(result, indexLimit, hostIsWindows));
@@ -960,7 +972,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
972 query,
973 pageSize,
974 "mixedSearch",
963 - (size) => finder.mixedSearch(query, { pageSize: size }),
975 + (size) => finder.mixedSearchAsync(query, { pageSize: size }),
976 mixedItemPath,
977 );
978 return merge(mapMixedSearchResult(result, indexLimit, hostIsWindows));
@@ -973,7 +985,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
985 // matching is subsequence-based, so dropping them still matches "[id]" and "a{b}" paths.
986 // "{" is dropped only when the query finds nothing as a glob, so "*.{ts,tsx}" keeps working.
987 const query = rawQuery.replaceAll("[", "");
976 - const incompleteBeforeQuery = hasIncompleteInitialScan();
988 + const incompleteBeforeQuery = yield* hasIncompleteInitialScan();
989 let mapped = yield* searchNames(query, limit, kind, imageOnly);
990 if (mapped.entries.length === 0 && query.includes("{")) {
991 mapped = yield* searchNames(query.replaceAll("{", ""), limit, kind, imageOnly);
@@ -998,7 +1010,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
1010 do {
1011 const remainingTimeBudgetMs = Math.max(1, Math.ceil(deadline - performance.now()));
1012 const result = yield* runSearch(input.query, input.limit, "grep", () =>
1001 - finder.grep(searchQuery, {
1013 + finder.grepAsync(searchQuery, {
1014 mode: regexMode ? "regex" : "plain",
1015 smartCase: !input.caseSensitive && !regexMode,
1016 maxMatchesPerFile: Math.min(CONTENT_SEARCH_MAX_MATCHES_PER_FILE, rawPageSize),
patches/@ff-labs__fff-node@0.11.0.patch
+204 -27
@@ -30,10 +30,10 @@ index 0f90eedca20655ccd30b8f28e433b2094341fbdf..c2ef86927d6e3ab0e8584330fa0ac3fd
30 /** Total number of items (files + dirs) that matched */
31 totalMatched: number;
32 diff --git a/dist/finder.d.ts b/dist/finder.d.ts
33 -index 452102d720cc19b0560cfc9e2f1a320687047c92..3d3211c70a7de4b97792edb32cc9466c8ba89547 100644
33 +index 452102d720cc19b0560cfc9e2f1a320687047c92..490dba5277c8d751b53ed239c0ce77f3e11f9c43 100644
34 --- a/dist/finder.d.ts
35 +++ b/dist/finder.d.ts
36 -@@ -242,6 +242,10 @@ export declare class FileFinder implements FileFinderApi {
36 +@@ -242,6 +242,18 @@ export declare class FileFinder implements FileFinderApi {
37 * background watcher might have missed.
38 */
39 scanFiles(): Result<void>;
@@ -41,10 +41,18 @@ index 452102d720cc19b0560cfc9e2f1a320687047c92..3d3211c70a7de4b97792edb32cc9466c
41 + * Trigger a rescan without blocking the JS thread. Patched in T3 Code.
42 + */
43 + scanFilesAsync(): Promise<Result<void>>;
44 ++ /** `fileSearch` without blocking the JS thread. Patched in T3 Code. */
45 ++ fileSearchAsync(query: string, options?: SearchOptions): Promise<Result<SearchResult>>;
46 ++ /** `directorySearch` without blocking the JS thread. Patched in T3 Code. */
47 ++ directorySearchAsync(query: string, options?: DirSearchOptions): Promise<Result<DirSearchResult>>;
48 ++ /** `mixedSearch` without blocking the JS thread. Patched in T3 Code. */
49 ++ mixedSearchAsync(query: string, options?: SearchOptions): Promise<Result<MixedSearchResult>>;
50 ++ /** `grep` without blocking the JS thread. Patched in T3 Code. */
51 ++ grepAsync(query: string, options?: GrepOptions): Promise<Result<GrepResult>>;
52 /**
53 * Check if a scan is currently in progress.
54 */
47 -@@ -254,6 +258,10 @@ export declare class FileFinder implements FileFinderApi {
55 +@@ -254,6 +266,10 @@ export declare class FileFinder implements FileFinderApi {
56 * Get the current scan progress.
57 */
58 getScanProgress(): Result<ScanProgress>;
@@ -56,7 +64,7 @@ index 452102d720cc19b0560cfc9e2f1a320687047c92..3d3211c70a7de4b97792edb32cc9466c
64 * Wait for the initial file scan to complete.
65 * Non-blocking: polls `isScanning` and yields to the event loop between checks.
66 diff --git a/dist/index.js b/dist/index.js
59 -index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588ac7f86cc9 100644
67 +index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..5d9fbf45095bd2b2ff1c321c03b85414498bde57 100644
68 --- a/dist/index.js
69 +++ b/dist/index.js
70 @@ -1,7 +1,7 @@
@@ -140,7 +148,20 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
148 if ("ok" in res) return res;
149 freeResult(res.rawPtr);
150 return { ok: true, value: void 0 };
143 -@@ -426,9 +459,9 @@ function ffiDestroy(handle) {
151 +@@ -417,18 +450,20 @@ function ffiCreate(basePath, frecencyDbPath, historyDbPath, _useUnsafeNoLock, en
152 + }
153 + function ffiDestroy(handle) {
154 + loadLibrary();
155 +- load({
156 ++ return load({
157 + library: LIBRARY_KEY,
158 + funcName: "fff_destroy",
159 + retType: DataType.Void,
160 + paramsType: [DataType.External],
161 +- paramsValue: [handle]
162 ++ paramsValue: [handle],
163 ++ runInNewThread: true
164 ++ }).catch(() => {
165 });
166 }
167 var FFF_FILE_ITEM_STRUCT = {
@@ -153,7 +174,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
174 size: DataType.U64,
175 modified: DataType.U64,
176 access_frecency_score: DataType.I64,
156 -@@ -445,8 +478,9 @@ var FFF_SCORE_STRUCT = {
177 +@@ -445,8 +480,9 @@ var FFF_SCORE_STRUCT = {
178 distance_penalty: DataType.I32,
179 current_file_penalty: DataType.I32,
180 combo_match_boost: DataType.I32,
@@ -164,7 +185,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
185 };
186 var FFF_SEARCH_RESULT_STRUCT = {
187 items: DataType.External,
167 -@@ -462,8 +496,8 @@ var FFF_SEARCH_RESULT_STRUCT = {
188 +@@ -462,8 +498,8 @@ var FFF_SEARCH_RESULT_STRUCT = {
189 location_end_col: DataType.I32
190 };
191 var FFF_DIR_ITEM_STRUCT = {
@@ -175,7 +196,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
196 max_access_frecency: DataType.I32
197 };
198 var FFF_DIR_SEARCH_RESULT_STRUCT = {
178 -@@ -475,9 +509,9 @@ var FFF_DIR_SEARCH_RESULT_STRUCT = {
199 +@@ -475,9 +511,9 @@ var FFF_DIR_SEARCH_RESULT_STRUCT = {
200 };
201 var FFF_MIXED_ITEM_STRUCT = {
202 item_type: DataType.U8,
@@ -188,7 +209,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
209 size: DataType.U64,
210 modified: DataType.U64,
211 access_frecency_score: DataType.I64,
191 -@@ -500,10 +534,10 @@ var FFF_MIXED_SEARCH_RESULT_STRUCT = {
212 +@@ -500,10 +536,10 @@ var FFF_MIXED_SEARCH_RESULT_STRUCT = {
213 location_end_col: DataType.I32
214 };
215 var FFF_GREP_MATCH_STRUCT = {
@@ -203,7 +224,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
224 match_ranges: DataType.External,
225 context_before: DataType.External,
226 context_after: DataType.External,
206 -@@ -598,18 +632,13 @@ function readMixedItemFromRaw(raw) {
227 +@@ -598,18 +634,13 @@ function readMixedItemFromRaw(raw) {
228 }
229 function callAccessor(funcName, resultPtr, index, structDef) {
230 loadLibrary();
@@ -224,7 +245,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
245 }
246 function ptrOffset(base, bytes) {
247 return load({
227 -@@ -766,6 +795,7 @@ function parseSearchResult(rawPtr) {
248 +@@ -766,6 +797,7 @@ function parseSearchResult(rawPtr) {
249 }
250 const items = [];
251 const scores = [];
@@ -232,7 +253,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
253 for (let i = 0; i < count; i++) {
254 const rawItem = callAccessor(
255 "fff_search_result_get_item",
235 -@@ -774,13 +804,15 @@ function parseSearchResult(rawPtr) {
256 +@@ -774,13 +806,15 @@ function parseSearchResult(rawPtr) {
257 FFF_FILE_ITEM_STRUCT
258 );
259 items.push(readFileItemFromRaw(rawItem));
@@ -255,7 +276,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
276 }
277 load({
278 library: LIBRARY_KEY,
258 -@@ -824,6 +856,7 @@ function parseDirSearchResult(rawPtr) {
279 +@@ -824,6 +858,7 @@ function parseDirSearchResult(rawPtr) {
280 const count = sr.count;
281 const items = [];
282 const scores = [];
@@ -263,7 +284,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
284 for (let i = 0; i < count; i++) {
285 const rawItem = callAccessor(
286 "fff_dir_search_result_get_item",
266 -@@ -832,13 +865,15 @@ function parseDirSearchResult(rawPtr) {
287 +@@ -832,13 +867,15 @@ function parseDirSearchResult(rawPtr) {
288 FFF_DIR_ITEM_STRUCT
289 );
290 items.push(readDirItemFromRaw(rawItem));
@@ -286,7 +307,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
307 }
308 load({
309 library: LIBRARY_KEY,
289 -@@ -897,6 +932,7 @@ function parseMixedSearchResult(rawPtr) {
310 +@@ -897,6 +934,7 @@ function parseMixedSearchResult(rawPtr) {
311 }
312 const items = [];
313 const scores = [];
@@ -294,7 +315,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
315 for (let i = 0; i < count; i++) {
316 const rawItem = callAccessor(
317 "fff_mixed_search_result_get_item",
297 -@@ -905,13 +941,15 @@ function parseMixedSearchResult(rawPtr) {
318 +@@ -905,13 +943,15 @@ function parseMixedSearchResult(rawPtr) {
319 FFF_MIXED_ITEM_STRUCT
320 );
321 items.push(readMixedItemFromRaw(rawItem));
@@ -317,7 +338,83 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
338 }
339 load({
340 library: LIBRARY_KEY,
320 -@@ -1188,7 +1226,11 @@ var FFF_SCAN_PROGRESS_STRUCT = {
341 +@@ -932,7 +972,7 @@ function parseMixedSearchResult(rawPtr) {
342 + }
343 + return { ok: true, value: result };
344 + }
345 +-function ffiSearch(handle, query, currentFile, maxThreads, pageIndex, pageSize, comboBoostMultiplier, minComboCount) {
346 ++function ffiSearch(handle, query, currentFile, maxThreads, pageIndex, pageSize, comboBoostMultiplier, minComboCount, onNativeThread = false) {
347 + loadLibrary();
348 + const rawPtr = load({
349 + library: LIBRARY_KEY,
350 +@@ -966,9 +1006,10 @@ function ffiSearch(handle, query, currentFile, maxThreads, pageIndex, pageSize,
351 + comboBoostMultiplier,
352 + minComboCount
353 + ],
354 +- freeResultMemory: false
355 ++ freeResultMemory: false,
356 ++ runInNewThread: onNativeThread
357 + });
358 +- return parseSearchResult(rawPtr);
359 ++ return onNativeThread ? rawPtr.then(parseSearchResult) : parseSearchResult(rawPtr);
360 + }
361 + function ffiGlob(handle, pattern, currentFile, maxThreads, pageIndex, pageSize) {
362 + loadLibrary();
363 +@@ -995,7 +1036,7 @@ function ffiGlob(handle, pattern, currentFile, maxThreads, pageIndex, pageSize)
364 + });
365 + return parseSearchResult(rawPtr);
366 + }
367 +-function ffiSearchDirectories(handle, query, currentFile, maxThreads, pageIndex, pageSize) {
368 ++function ffiSearchDirectories(handle, query, currentFile, maxThreads, pageIndex, pageSize, onNativeThread = false) {
369 + loadLibrary();
370 + const rawPtr = load({
371 + library: LIBRARY_KEY,
372 +@@ -1016,11 +1057,12 @@ function ffiSearchDirectories(handle, query, currentFile, maxThreads, pageIndex,
373 + // page_size
374 + ],
375 + paramsValue: [handle, query, currentFile ?? "", maxThreads, pageIndex, pageSize],
376 +- freeResultMemory: false
377 ++ freeResultMemory: false,
378 ++ runInNewThread: onNativeThread
379 + });
380 +- return parseDirSearchResult(rawPtr);
381 ++ return onNativeThread ? rawPtr.then(parseDirSearchResult) : parseDirSearchResult(rawPtr);
382 + }
383 +-function ffiSearchMixed(handle, query, currentFile, maxThreads, pageIndex, pageSize, comboBoostMultiplier, minComboCount) {
384 ++function ffiSearchMixed(handle, query, currentFile, maxThreads, pageIndex, pageSize, comboBoostMultiplier, minComboCount, onNativeThread = false) {
385 + loadLibrary();
386 + const rawPtr = load({
387 + library: LIBRARY_KEY,
388 +@@ -1054,11 +1096,12 @@ function ffiSearchMixed(handle, query, currentFile, maxThreads, pageIndex, pageS
389 + comboBoostMultiplier,
390 + minComboCount
391 + ],
392 +- freeResultMemory: false
393 ++ freeResultMemory: false,
394 ++ runInNewThread: onNativeThread
395 + });
396 +- return parseMixedSearchResult(rawPtr);
397 ++ return onNativeThread ? rawPtr.then(parseMixedSearchResult) : parseMixedSearchResult(rawPtr);
398 + }
399 +-function ffiLiveGrep(handle, query, mode, maxFileSize, maxMatchesPerFile, smartCase, fileOffset, pageLimit, timeBudgetMs, enforceTimeBudget, beforeContext, afterContext, classifyDefinitions) {
400 ++function ffiLiveGrep(handle, query, mode, maxFileSize, maxMatchesPerFile, smartCase, fileOffset, pageLimit, timeBudgetMs, enforceTimeBudget, beforeContext, afterContext, classifyDefinitions, onNativeThread = false) {
401 + loadLibrary();
402 + const rawPtr = load({
403 + library: LIBRARY_KEY,
404 +@@ -1107,9 +1150,10 @@ function ffiLiveGrep(handle, query, mode, maxFileSize, maxMatchesPerFile, smartC
405 + afterContext,
406 + classifyDefinitions
407 + ],
408 +- freeResultMemory: false
409 ++ freeResultMemory: false,
410 ++ runInNewThread: onNativeThread
411 + });
412 +- return parseGrepResult(rawPtr);
413 ++ return onNativeThread ? rawPtr.then(parseGrepResult) : parseGrepResult(rawPtr);
414 + }
415 + function ffiMultiGrep(handle, patternsJoined, constraints, maxFileSize, maxMatchesPerFile, smartCase, fileOffset, pageLimit, timeBudgetMs, enforceTimeBudget, beforeContext, afterContext, classifyDefinitions) {
416 + loadLibrary();
417 +@@ -1188,7 +1232,11 @@ var FFF_SCAN_PROGRESS_STRUCT = {
418 };
419 function ffiGetScanProgress(handle) {
420 loadLibrary();
@@ -330,16 +427,18 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
427 if ("ok" in res) return res;
428 const handlePtr = res.struct.handle;
429 freeResult(res.rawPtr);
333 -@@ -1429,6 +1471,8 @@ var FileFinder = class _FileFinder {
430 +@@ -1429,6 +1477,10 @@ var FileFinder = class _FileFinder {
431 handle;
432 /** Native ids of this instance's active watch subscriptions. */
433 watchers = /* @__PURE__ */ new Set();
434 + /** Patched in T3 Code: in-flight native-thread calls by function name. */
435 + nativeThreadCalls = /* @__PURE__ */ new Map();
436 ++ /** Patched in T3 Code: the last search queued on a native thread. */
437 ++ nativeSearchTail = null;
438 constructor(handle) {
439 this.handle = handle;
440 }
342 -@@ -1485,12 +1529,37 @@ var FileFinder = class _FileFinder {
441 +@@ -1485,12 +1537,115 @@ var FileFinder = class _FileFinder {
442 destroy() {
443 if (this.handle !== null) {
444 const handle = this.handle;
@@ -349,13 +448,15 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
448 - ffiWatchCleanupAfterDestroy(handle, this.watchers);
449 - this.watchers.clear();
450 + this.watchers = /* @__PURE__ */ new Set();
352 -+ const release = () => {
353 -+ ffiDestroy(handle);
451 ++ // Patched in T3 Code: freeing the handle waits on the picker lock, so it runs on a
452 ++ // native thread, after any native-thread call still using the handle.
453 ++ const release = () => ffiDestroy(handle).finally(() => {
454 + ffiWatchCleanupAfterDestroy(handle, watchers);
355 -+ };
356 -+ // Patched in T3 Code: a native-thread call may still be using the handle.
357 -+ if (this.nativeThreadCalls.size > 0) {
358 -+ Promise.allSettled(this.nativeThreadCalls.values()).then(release);
455 ++ });
456 ++ const pending = [...this.nativeThreadCalls.values()];
457 ++ if (this.nativeSearchTail) pending.push(this.nativeSearchTail);
458 ++ if (pending.length > 0) {
459 ++ Promise.allSettled(pending).then(release);
460 + } else {
461 + release();
462 + }
@@ -376,11 +477,87 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
477 + });
478 + this.nativeThreadCalls.set(funcName, call);
479 + return call;
480 ++ }
481 ++ /**
482 ++ * Patched in T3 Code: runs a search on a libuv pool thread. Searches on one finder run one at
483 ++ * a time, so while fff's watcher holds the picker lock a finder holds at most one pool thread.
484 ++ */
485 ++ searchOnNativeThread(search) {
486 ++ const guard = this.ensureAlive();
487 ++ if (!guard.ok) return Promise.resolve(guard);
488 ++ const call = (this.nativeSearchTail ?? Promise.resolve()).then(() => {
489 ++ const alive = this.ensureAlive();
490 ++ return alive.ok ? search(alive.value) : alive;
491 ++ }).catch((cause) => err(String(cause)));
492 ++ this.nativeSearchTail = call;
493 ++ call.finally(() => {
494 ++ if (this.nativeSearchTail === call) this.nativeSearchTail = null;
495 ++ });
496 ++ return call;
497 ++ }
498 ++ /** Patched in T3 Code: `fileSearch` without blocking the JS thread. */
499 ++ fileSearchAsync(query, options) {
500 ++ return this.searchOnNativeThread((handle) => ffiSearch(
501 ++ handle,
502 ++ query,
503 ++ options?.currentFile ?? "",
504 ++ options?.maxThreads ?? 0,
505 ++ options?.pageIndex ?? 0,
506 ++ options?.pageSize ?? 0,
507 ++ options?.comboBoostMultiplier ?? 0,
508 ++ options?.minComboCount ?? 0,
509 ++ true
510 ++ ));
511 ++ }
512 ++ /** Patched in T3 Code: `directorySearch` without blocking the JS thread. */
513 ++ directorySearchAsync(query, options) {
514 ++ return this.searchOnNativeThread((handle) => ffiSearchDirectories(
515 ++ handle,
516 ++ query,
517 ++ options?.currentFile ?? null,
518 ++ options?.maxThreads ?? 0,
519 ++ options?.pageIndex ?? 0,
520 ++ options?.pageSize ?? 0,
521 ++ true
522 ++ ));
523 ++ }
524 ++ /** Patched in T3 Code: `mixedSearch` without blocking the JS thread. */
525 ++ mixedSearchAsync(query, options) {
526 ++ return this.searchOnNativeThread((handle) => ffiSearchMixed(
527 ++ handle,
528 ++ query,
529 ++ options?.currentFile ?? "",
530 ++ options?.maxThreads ?? 0,
531 ++ options?.pageIndex ?? 0,
532 ++ options?.pageSize ?? 0,
533 ++ options?.comboBoostMultiplier ?? 0,
534 ++ options?.minComboCount ?? 0,
535 ++ true
536 ++ ));
537 ++ }
538 ++ /** Patched in T3 Code: `grep` without blocking the JS thread. */
539 ++ grepAsync(query, options) {
540 ++ return this.searchOnNativeThread((handle) => ffiLiveGrep(
541 ++ handle,
542 ++ query,
543 ++ options?.mode ?? "plain",
544 ++ options?.maxFileSize ?? 0,
545 ++ options?.maxMatchesPerFile ?? 0,
546 ++ options?.smartCase ?? true,
547 ++ options?.cursor?._offset ?? 0,
548 ++ options?.pageSize ?? 0,
549 ++ options?.timeBudgetMs ?? 0,
550 ++ options?.enforceTimeBudget ?? false,
551 ++ options?.beforeContext ?? 0,
552 ++ options?.afterContext ?? 0,
553 ++ options?.classifyDefinitions ?? false,
554 ++ true
555 ++ ));
556 + }
557 /**
558 * Check if this instance has been destroyed.
559 */
383 -@@ -1757,6 +1826,18 @@ var FileFinder = class _FileFinder {
560 +@@ -1757,6 +1912,18 @@ var FileFinder = class _FileFinder {
561 if (!guard.ok) return guard;
562 return ffiScanFiles(guard.value);
563 }
@@ -399,7 +576,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588a
576 /**
577 * Check if a scan is currently in progress.
578 */
402 -@@ -1838,7 +1919,8 @@ var FileFinder = class _FileFinder {
579 +@@ -1838,7 +2005,8 @@ var FileFinder = class _FileFinder {
580 if (!guard.ok) return guard;
581 const deadline = Date.now() + timeoutMs;
582 while (true) {
pnpm-lock.yaml
+3 -3
@@ -364,7 +364,7 @@ patchedDependencies:
364 '@cursor/sdk@1.2.0': 57f502128bf6accd976963b126e227c86a45d815a53a8dc450461beae3bed4a4
365 '@effect/vitest@4.1.2': 299510b71c36537a832a67e9d160a5f112a5f326a29e95c41cb45fc8234e47c7
366 '@expo/ui@58.0.27': 9f2547678939f4ec78fe45163b6455aad63d3c80353d7e0cc783f6a61a7ccc2f
367 - '@ff-labs/fff-node@0.11.0': a71dc3a5544fc47afacf0fe9da04dccec3018e625a76c96087ebfdf35e293768
367 + '@ff-labs/fff-node@0.11.0': 3be7107340e5f388114da259637828cd47f2b32eb8ae572440715c4e38cffe6e
368 '@legendapp/list@3.7.2': 3c68607b8bb7a531873bcbe0b197d163ef1fdb4d4cf52a4ddf9d2b3804671658
369 '@muse-code/sdk@1.4.6': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101
370 '@pierre/diffs@1.7.0-beta.3': 4a64f63a0def94c5add9d86d70560d26d6857e1f5150133862e42e1fd77bc4e8
@@ -857,7 +857,7 @@ importers:
857 version: 4.1.2(bufferutil@4.1.0)(effect@4.1.3)(utf-8-validate@6.0.6)
858 '@ff-labs/fff-node':
859 specifier: 0.11.0
860 - version: 0.11.0(patch_hash=a71dc3a5544fc47afacf0fe9da04dccec3018e625a76c96087ebfdf35e293768)
860 + version: 0.11.0(patch_hash=3be7107340e5f388114da259637828cd47f2b32eb8ae572440715c4e38cffe6e)
861 '@muse-code/sdk':
862 specifier: 1.4.6
863 version: 1.4.6(patch_hash=7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101)(@types/node@24.12.4)
@@ -15148,7 +15148,7 @@ snapshots:
15148 '@ff-labs/fff-bin-win32-x64@0.11.0':
15149 optional: true
15150
15151 - '@ff-labs/fff-node@0.11.0(patch_hash=a71dc3a5544fc47afacf0fe9da04dccec3018e625a76c96087ebfdf35e293768)':
15151 + '@ff-labs/fff-node@0.11.0(patch_hash=3be7107340e5f388114da259637828cd47f2b32eb8ae572440715c4e38cffe6e)':
15152 dependencies:
15153 ffi-rs: 1.3.2
15154 optionalDependencies: