@devries-cameron20 / t3code Mirror · read-only / commits / 7b17bdbcef
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 19:45 UTC

fix(server): file index refreshes no longer block the server on fff's lock (#35460)

Julius Marminge committed Nov 12, 2026 at 10:16 UTC 7b17bdbcef739444d8e50f4d59872d897734ed9c
4 files changed +196 -32
apps/server/src/workspace/WorkspaceEntries.test.ts
+6 -2
@@ -813,10 +813,14 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
813 if (created.ok) {
814 const finder = created.value;
815 finders += 1;
816 - const scanFiles = finder.scanFiles.bind(finder);
816 + // The synchronous rescan waits on fff's picker lock on the event loop.
817 finder.scanFiles = () => {
818 + throw new Error("refresh must not rescan on the JS thread");
819 + };
820 + const scanFilesAsync = finder.scanFilesAsync.bind(finder);
821 + finder.scanFilesAsync = () => {
822 rescans += 1;
819 - return scanFiles();
823 + return scanFilesAsync();
824 };
825 const waitForIndexReady = finder.waitForIndexReady.bind(finder);
826 finder.waitForIndexReady = (timeoutMs) =>
apps/server/src/workspace/WorkspaceSearchIndex.ts
+4 -3
@@ -399,12 +399,13 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
399 const refresh: WorkspaceSearchIndex["Service"]["refresh"] = Effect.fn(
400 "WorkspaceSearchIndex.refresh",
401 )(function* () {
402 - const result = yield* Effect.try({
403 - try: () => finder.scanFiles(),
402 + // scanFiles waits on fff's picker lock, which its watcher can hold for seconds.
403 + const result = yield* Effect.tryPromise({
404 + try: () => finder.scanFilesAsync(),
405 catch: (cause) =>
406 new WorkspaceSearchIndexRefreshFailed({
407 cwd,
407 - reason: "FileFinder.scanFiles threw unexpectedly.",
408 + reason: "FileFinder.scanFilesAsync rejected unexpectedly.",
409 cause,
410 }),
411 });
patches/@ff-labs__fff-node@0.11.0.patch
+183 -24
@@ -1,16 +1,5 @@
1 -diff --git a/package.json b/package.json
2 ---- a/package.json
3 -+++ b/package.json
4 -@@ -11,7 +11,7 @@
5 - ".": {
6 - "types": "./dist/index.d.ts",
7 - "import": "./dist/index.js",
8 -- "require": "./dist/index.cjs",
9 -+ "require": "./dist/index.js",
10 - "default": "./dist/index.js"
11 - },
12 - "./package.json": "./package.json"
1 diff --git a/dist/fff-api.d.ts b/dist/fff-api.d.ts
2 +index 0f90eedca20655ccd30b8f28e433b2094341fbdf..c2ef86927d6e3ab0e8584330fa0ac3fdacc06cf5 100644
3 --- a/dist/fff-api.d.ts
4 +++ b/dist/fff-api.d.ts
5 @@ -199,7 +199,7 @@ export type Location = {
@@ -40,7 +29,34 @@ diff --git a/dist/fff-api.d.ts b/dist/fff-api.d.ts
29 scores: Score[];
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
34 +--- a/dist/finder.d.ts
35 ++++ b/dist/finder.d.ts
36 +@@ -242,6 +242,10 @@ export declare class FileFinder implements FileFinderApi {
37 + * background watcher might have missed.
38 + */
39 + scanFiles(): Result<void>;
40 ++ /**
41 ++ * Trigger a rescan without blocking the JS thread. Patched in T3 Code.
42 ++ */
43 ++ scanFilesAsync(): Promise<Result<void>>;
44 + /**
45 + * Check if a scan is currently in progress.
46 + */
47 +@@ -254,6 +258,10 @@ export declare class FileFinder implements FileFinderApi {
48 + * Get the current scan progress.
49 + */
50 + getScanProgress(): Result<ScanProgress>;
51 ++ /**
52 ++ * Read scan progress without blocking the JS thread. Patched in T3 Code.
53 ++ */
54 ++ getScanProgressAsync(): Promise<Result<ScanProgress>>;
55 + /**
56 + * Wait for the initial file scan to complete.
57 + * Non-blocking: polls `isScanning` and yields to the event loop between checks.
58 diff --git a/dist/index.js b/dist/index.js
59 +index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..138e590bdcb1bc4fd33f7d195b8d588ac7f86cc9 100644
60 --- a/dist/index.js
61 +++ b/dist/index.js
62 @@ -1,7 +1,7 @@
@@ -86,7 +102,45 @@ diff --git a/dist/index.js b/dist/index.js
102 if (isNullPointer(ptr)) return null;
103 try {
104 const [str] = restorePointer({
89 -@@ -426,9 +437,9 @@ function ffiDestroy(handle) {
105 +@@ -303,6 +314,26 @@ function freeResult(resultPtr) {
106 + function readResultEnvelope(funcName, paramsType, paramsValue) {
107 + loadLibrary();
108 + const { rawPtr, struct: structData } = callRaw(funcName, paramsType, paramsValue);
109 ++ return envelopeFromRaw(rawPtr, structData);
110 ++}
111 ++async function readResultEnvelopeOnNativeThread(funcName, handle) {
112 ++ loadLibrary();
113 ++ const rawPtr = await load({
114 ++ library: LIBRARY_KEY,
115 ++ funcName,
116 ++ retType: DataType.External,
117 ++ paramsType: [DataType.External],
118 ++ paramsValue: [handle],
119 ++ freeResultMemory: false,
120 ++ runInNewThread: true
121 ++ });
122 ++ const [structData] = restorePointer({
123 ++ retType: [FFF_RESULT_STRUCT],
124 ++ paramsValue: wrapPointer([rawPtr])
125 ++ });
126 ++ return envelopeFromRaw(rawPtr, structData);
127 ++}
128 ++function envelopeFromRaw(rawPtr, structData) {
129 + if (structData.success === 0) {
130 + const errorStr = readCString(structData.error);
131 + freeResult(rawPtr);
132 +@@ -311,7 +342,9 @@ function readResultEnvelope(funcName, paramsType, paramsValue) {
133 + return { rawPtr, struct: structData };
134 + }
135 + function callVoidResult(funcName, paramsType, paramsValue) {
136 +- const res = readResultEnvelope(funcName, paramsType, paramsValue);
137 ++ return voidResultFromEnvelope(readResultEnvelope(funcName, paramsType, paramsValue));
138 ++}
139 ++function voidResultFromEnvelope(res) {
140 + if ("ok" in res) return res;
141 + freeResult(res.rawPtr);
142 + return { ok: true, value: void 0 };
143 +@@ -426,9 +459,9 @@ function ffiDestroy(handle) {
144 });
145 }
146 var FFF_FILE_ITEM_STRUCT = {
@@ -99,7 +153,7 @@ diff --git a/dist/index.js b/dist/index.js
153 size: DataType.U64,
154 modified: DataType.U64,
155 access_frecency_score: DataType.I64,
102 -@@ -445,8 +456,9 @@ var FFF_SCORE_STRUCT = {
156 +@@ -445,8 +478,9 @@ var FFF_SCORE_STRUCT = {
157 distance_penalty: DataType.I32,
158 current_file_penalty: DataType.I32,
159 combo_match_boost: DataType.I32,
@@ -110,7 +164,7 @@ diff --git a/dist/index.js b/dist/index.js
164 };
165 var FFF_SEARCH_RESULT_STRUCT = {
166 items: DataType.External,
113 -@@ -462,8 +474,8 @@ var FFF_SEARCH_RESULT_STRUCT = {
167 +@@ -462,8 +496,8 @@ var FFF_SEARCH_RESULT_STRUCT = {
168 location_end_col: DataType.I32
169 };
170 var FFF_DIR_ITEM_STRUCT = {
@@ -121,7 +175,7 @@ diff --git a/dist/index.js b/dist/index.js
175 max_access_frecency: DataType.I32
176 };
177 var FFF_DIR_SEARCH_RESULT_STRUCT = {
124 -@@ -475,9 +487,9 @@ var FFF_DIR_SEARCH_RESULT_STRUCT = {
178 +@@ -475,9 +509,9 @@ var FFF_DIR_SEARCH_RESULT_STRUCT = {
179 };
180 var FFF_MIXED_ITEM_STRUCT = {
181 item_type: DataType.U8,
@@ -134,7 +188,7 @@ diff --git a/dist/index.js b/dist/index.js
188 size: DataType.U64,
189 modified: DataType.U64,
190 access_frecency_score: DataType.I64,
137 -@@ -500,10 +512,10 @@ var FFF_MIXED_SEARCH_RESULT_STRUCT = {
191 +@@ -500,10 +534,10 @@ var FFF_MIXED_SEARCH_RESULT_STRUCT = {
192 location_end_col: DataType.I32
193 };
194 var FFF_GREP_MATCH_STRUCT = {
@@ -149,7 +203,7 @@ diff --git a/dist/index.js b/dist/index.js
203 match_ranges: DataType.External,
204 context_before: DataType.External,
205 context_after: DataType.External,
152 -@@ -598,18 +610,13 @@ function readMixedItemFromRaw(raw) {
206 +@@ -598,18 +632,13 @@ function readMixedItemFromRaw(raw) {
207 }
208 function callAccessor(funcName, resultPtr, index, structDef) {
209 loadLibrary();
@@ -170,7 +224,7 @@ diff --git a/dist/index.js b/dist/index.js
224 }
225 function ptrOffset(base, bytes) {
226 return load({
173 -@@ -766,6 +773,7 @@ function parseSearchResult(rawPtr) {
227 +@@ -766,6 +795,7 @@ function parseSearchResult(rawPtr) {
228 }
229 const items = [];
230 const scores = [];
@@ -178,7 +232,7 @@ diff --git a/dist/index.js b/dist/index.js
232 for (let i = 0; i < count; i++) {
233 const rawItem = callAccessor(
234 "fff_search_result_get_item",
181 -@@ -774,13 +782,15 @@ function parseSearchResult(rawPtr) {
235 +@@ -774,13 +804,15 @@ function parseSearchResult(rawPtr) {
236 FFF_FILE_ITEM_STRUCT
237 );
238 items.push(readFileItemFromRaw(rawItem));
@@ -201,7 +255,7 @@ diff --git a/dist/index.js b/dist/index.js
255 }
256 load({
257 library: LIBRARY_KEY,
204 -@@ -824,6 +834,7 @@ function parseDirSearchResult(rawPtr) {
258 +@@ -824,6 +856,7 @@ function parseDirSearchResult(rawPtr) {
259 const count = sr.count;
260 const items = [];
261 const scores = [];
@@ -209,7 +263,7 @@ diff --git a/dist/index.js b/dist/index.js
263 for (let i = 0; i < count; i++) {
264 const rawItem = callAccessor(
265 "fff_dir_search_result_get_item",
212 -@@ -832,13 +843,15 @@ function parseDirSearchResult(rawPtr) {
266 +@@ -832,13 +865,15 @@ function parseDirSearchResult(rawPtr) {
267 FFF_DIR_ITEM_STRUCT
268 );
269 items.push(readDirItemFromRaw(rawItem));
@@ -232,7 +286,7 @@ diff --git a/dist/index.js b/dist/index.js
286 }
287 load({
288 library: LIBRARY_KEY,
235 -@@ -897,6 +910,7 @@ function parseMixedSearchResult(rawPtr) {
289 +@@ -897,6 +932,7 @@ function parseMixedSearchResult(rawPtr) {
290 }
291 const items = [];
292 const scores = [];
@@ -240,7 +294,7 @@ diff --git a/dist/index.js b/dist/index.js
294 for (let i = 0; i < count; i++) {
295 const rawItem = callAccessor(
296 "fff_mixed_search_result_get_item",
243 -@@ -905,13 +919,15 @@ function parseMixedSearchResult(rawPtr) {
297 +@@ -905,13 +941,15 @@ function parseMixedSearchResult(rawPtr) {
298 FFF_MIXED_ITEM_STRUCT
299 );
300 items.push(readMixedItemFromRaw(rawItem));
@@ -263,3 +317,108 @@ diff --git a/dist/index.js b/dist/index.js
317 }
318 load({
319 library: LIBRARY_KEY,
320 +@@ -1188,7 +1226,11 @@ var FFF_SCAN_PROGRESS_STRUCT = {
321 + };
322 + function ffiGetScanProgress(handle) {
323 + loadLibrary();
324 +- const res = readResultEnvelope("fff_get_scan_progress", [DataType.External], [handle]);
325 ++ return scanProgressFromEnvelope(
326 ++ readResultEnvelope("fff_get_scan_progress", [DataType.External], [handle])
327 ++ );
328 ++}
329 ++function scanProgressFromEnvelope(res) {
330 + if ("ok" in res) return res;
331 + const handlePtr = res.struct.handle;
332 + freeResult(res.rawPtr);
333 +@@ -1429,6 +1471,8 @@ var FileFinder = class _FileFinder {
334 + handle;
335 + /** Native ids of this instance's active watch subscriptions. */
336 + watchers = /* @__PURE__ */ new Set();
337 ++ /** Patched in T3 Code: in-flight native-thread calls by function name. */
338 ++ nativeThreadCalls = /* @__PURE__ */ new Map();
339 + constructor(handle) {
340 + this.handle = handle;
341 + }
342 +@@ -1485,12 +1529,37 @@ var FileFinder = class _FileFinder {
343 + destroy() {
344 + if (this.handle !== null) {
345 + const handle = this.handle;
346 +- ffiDestroy(handle);
347 ++ const watchers = this.watchers;
348 + this.handle = null;
349 +- ffiWatchCleanupAfterDestroy(handle, this.watchers);
350 +- this.watchers.clear();
351 ++ this.watchers = /* @__PURE__ */ new Set();
352 ++ const release = () => {
353 ++ ffiDestroy(handle);
354 ++ 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);
359 ++ } else {
360 ++ release();
361 ++ }
362 + }
363 + }
364 ++ /**
365 ++ * Patched in T3 Code: runs a handle-only call on a libuv pool thread so waiting on fff's
366 ++ * picker lock never blocks the JS thread. Concurrent callers share one call, so one finder
367 ++ * holds at most one pool thread per function.
368 ++ */
369 ++ callOnNativeThread(funcName, fromEnvelope) {
370 ++ const guard = this.ensureAlive();
371 ++ if (!guard.ok) return Promise.resolve(guard);
372 ++ const pending = this.nativeThreadCalls.get(funcName);
373 ++ if (pending) return pending;
374 ++ const call = readResultEnvelopeOnNativeThread(funcName, guard.value).then(fromEnvelope, (cause) => err(String(cause))).finally(() => {
375 ++ this.nativeThreadCalls.delete(funcName);
376 ++ });
377 ++ this.nativeThreadCalls.set(funcName, call);
378 ++ return call;
379 ++ }
380 + /**
381 + * Check if this instance has been destroyed.
382 + */
383 +@@ -1757,6 +1826,18 @@ var FileFinder = class _FileFinder {
384 + if (!guard.ok) return guard;
385 + return ffiScanFiles(guard.value);
386 + }
387 ++ /**
388 ++ * Trigger a rescan without blocking the JS thread. Patched in T3 Code.
389 ++ */
390 ++ scanFilesAsync() {
391 ++ return this.callOnNativeThread("fff_scan_files", voidResultFromEnvelope);
392 ++ }
393 ++ /**
394 ++ * Read scan progress without blocking the JS thread. Patched in T3 Code.
395 ++ */
396 ++ getScanProgressAsync() {
397 ++ return this.callOnNativeThread("fff_get_scan_progress", scanProgressFromEnvelope);
398 ++ }
399 + /**
400 + * Check if a scan is currently in progress.
401 + */
402 +@@ -1838,7 +1919,8 @@ var FileFinder = class _FileFinder {
403 + if (!guard.ok) return guard;
404 + const deadline = Date.now() + timeoutMs;
405 + while (true) {
406 +- const progress = this.getScanProgress();
407 ++ // Patched in T3 Code: the progress read takes the picker lock.
408 ++ const progress = await this.getScanProgressAsync();
409 + if (!progress.ok) return progress;
410 + if (!progress.value.isScanning && progress.value.isWarmupComplete) {
411 + return { ok: true, value: true };
412 +diff --git a/package.json b/package.json
413 +index e000a78b3f79826200979ebb3fe297b599bcea67..84325461c2d963cb42cf72267079d36a419c1398 100644
414 +--- a/package.json
415 ++++ b/package.json
416 +@@ -11,7 +11,7 @@
417 + ".": {
418 + "types": "./dist/index.d.ts",
419 + "import": "./dist/index.js",
420 +- "require": "./dist/index.cjs",
421 ++ "require": "./dist/index.js",
422 + "default": "./dist/index.js"
423 + },
424 + "./package.json": "./package.json"
pnpm-lock.yaml
+3 -3
@@ -363,7 +363,7 @@ patchedDependencies:
363 '@clerk/expo-native-components@0.4.4': 7a808b92da8460750b85818552e13be3fb73d111691c7db76965970351e124e4
364 '@cursor/sdk@1.2.0': 57f502128bf6accd976963b126e227c86a45d815a53a8dc450461beae3bed4a4
365 '@effect/vitest@4.1.2': 299510b71c36537a832a67e9d160a5f112a5f326a29e95c41cb45fc8234e47c7
366 - '@ff-labs/fff-node@0.11.0': cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c
366 + '@ff-labs/fff-node@0.11.0': a71dc3a5544fc47afacf0fe9da04dccec3018e625a76c96087ebfdf35e293768
367 '@legendapp/list@3.7.2': 3c68607b8bb7a531873bcbe0b197d163ef1fdb4d4cf52a4ddf9d2b3804671658
368 '@muse-code/sdk@1.4.6': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101
369 '@pierre/diffs@1.7.0-beta.3': 4a64f63a0def94c5add9d86d70560d26d6857e1f5150133862e42e1fd77bc4e8
@@ -856,7 +856,7 @@ importers:
856 version: 4.1.2(bufferutil@4.1.0)(effect@4.1.2)(utf-8-validate@6.0.6)
857 '@ff-labs/fff-node':
858 specifier: 0.11.0
859 - version: 0.11.0(patch_hash=cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c)
859 + version: 0.11.0(patch_hash=a71dc3a5544fc47afacf0fe9da04dccec3018e625a76c96087ebfdf35e293768)
860 '@muse-code/sdk':
861 specifier: 1.4.6
862 version: 1.4.6(patch_hash=7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101)(@types/node@24.12.4)
@@ -15144,7 +15144,7 @@ snapshots:
15144 '@ff-labs/fff-bin-win32-x64@0.11.0':
15145 optional: true
15146
15147 - '@ff-labs/fff-node@0.11.0(patch_hash=cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c)':
15147 + '@ff-labs/fff-node@0.11.0(patch_hash=a71dc3a5544fc47afacf0fe9da04dccec3018e625a76c96087ebfdf35e293768)':
15148 dependencies:
15149 ffi-rs: 1.3.2
15150 optionalDependencies: