@devries-cameron20 / t3code-1 / commits / 32c069eed1
Fork 0

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

fix(server): content search recovers after a slow first index (#33138)

Co-authored-by: ItsThierry <274141289+ItsThierry@users.noreply.github.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Boban Kostadinoski committed Nov 15, 2026 at 02:29 UTC 32c069eed1d22ca4b4bbaa52ec8d34bb3f4a9ff3
4 files changed +106 -10
apps/server/src/workspace/WorkspaceEntries.test.ts
+62
@@ -1014,6 +1014,68 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
1014 expect({ finders, rescans }).toEqual({ finders: 1, rescans: 1 });
1015 }),
1016 );
1017 +
1018 + it.effect("serves content search once a first scan that outlasted the wait finishes", () =>
1019 + Effect.gen(function* () {
1020 + const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-slow-scan-" });
1021 + yield* writeTextFile(cwd, "src/notes.ts", "const needle = 1;\n");
1022 + // Real finders; only the initial wait and the progress it reads report a scan still running.
1023 + let scanning = true;
1024 + let finders = 0;
1025 + let settle = (): Promise<unknown> => Promise.resolve();
1026 + const createFinder = FileFinder.create.bind(FileFinder);
1027 + vi.spyOn(FileFinder, "create").mockImplementation((options) => {
1028 + const created = createFinder(options);
1029 + if (!created.ok) return created;
1030 + const finder = created.value;
1031 + finders += 1;
1032 + const waitForIndexReady = finder.waitForIndexReady.bind(finder);
1033 + settle = () => waitForIndexReady(15_000);
1034 + finder.waitForIndexReady = (timeoutMs) =>
1035 + scanning ? Promise.resolve({ ok: true, value: false }) : waitForIndexReady(timeoutMs);
1036 + const getScanProgress = finder.getScanProgressAsync.bind(finder);
1037 + finder.getScanProgressAsync = async () => {
1038 + const progress = await getScanProgress();
1039 + return scanning && progress.ok
1040 + ? {
1041 + ok: true,
1042 + value: { ...progress.value, isScanning: true, isWarmupComplete: false },
1043 + }
1044 + : progress;
1045 + };
1046 + return created;
1047 + });
1048 + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
1049 + const searchNeedle = workspaceEntries
1050 + .searchContents({
1051 + cwd,
1052 + query: "needle",
1053 + limit: 100,
1054 + caseSensitive: true,
1055 + wholeWord: false,
1056 + useRegex: false,
1057 + })
1058 + .pipe(
1059 + Effect.map((result): unknown => result.matches.map((match) => match.path)),
1060 + Effect.catchTags({
1061 + WorkspaceSearchIndexScanTimedOut: (error) => Effect.succeed<unknown>(error._tag),
1062 + }),
1063 + );
1064 +
1065 + const whileScanning = yield* Effect.all([searchNeedle, searchNeedle], {
1066 + concurrency: "unbounded",
1067 + });
1068 + scanning = false;
1069 + yield* Effect.promise(() => settle());
1070 + const afterScan = yield* searchNeedle;
1071 +
1072 + expect({ whileScanning, afterScan, finders }).toEqual({
1073 + whileScanning: ["WorkspaceSearchIndexScanTimedOut", "WorkspaceSearchIndexScanTimedOut"],
1074 + afterScan: ["src/notes.ts"],
1075 + finders: 1,
1076 + });
1077 + }),
1078 + );
1079 });
1080
1081 describe("browse", () => {
apps/server/src/workspace/WorkspaceEntries.ts
-2
@@ -179,7 +179,6 @@ export const make = Effect.gen(function* () {
179 function* (
180 cause:
181 | WorkspaceSearchIndex.WorkspaceSearchIndexCreateFailed
182 - | WorkspaceSearchIndex.WorkspaceSearchIndexScanTimedOut
182 | WorkspaceSearchIndex.WorkspaceSearchIndexRefreshFailed,
183 ) {
184 yield* Effect.logWarning("Failed to refresh workspace search index", {
@@ -197,7 +196,6 @@ export const make = Effect.gen(function* () {
196 Effect.provide(workspaceSearchIndexes.get(indexKey)),
197 Effect.catchTags({
198 WorkspaceSearchIndexCreateFailed: recoverRefreshFailure,
200 - WorkspaceSearchIndexScanTimedOut: recoverRefreshFailure,
199 WorkspaceSearchIndexRefreshFailed: recoverRefreshFailure,
200 }),
201 );
apps/server/src/workspace/WorkspaceSearchIndex.ts
+24 -8
@@ -142,7 +142,10 @@ export class WorkspaceSearchIndex extends Context.Service<
142 ) => Effect.Effect<ProjectSearchEntriesResult, WorkspaceSearchIndexSearchFailed>;
143 readonly searchContents: (
144 input: Omit<ProjectSearchContentsInput, "cwd">,
145 - ) => Effect.Effect<ProjectSearchContentsResult, WorkspaceSearchIndexSearchFailed>;
145 + ) => Effect.Effect<
146 + ProjectSearchContentsResult,
147 + WorkspaceSearchIndexScanTimedOut | WorkspaceSearchIndexSearchFailed
148 + >;
149 readonly refresh: () => Effect.Effect<void, WorkspaceSearchIndexRefreshFailed>;
150 }
151 >()("t3/workspace/WorkspaceSearchIndex") {}
@@ -585,13 +588,12 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
588 cause,
589 }),
590 ).pipe(
591 + // The scan keeps running on this finder; failing here would destroy it and cache the failure.
592 Effect.catchTags({
589 - WorkspaceSearchIndexScanTimedOut: (error) =>
590 - variant === "paths"
591 - ? Effect.sync(() => {
592 - initialScanTimedOut = true;
593 - })
594 - : Effect.fail(error),
593 + WorkspaceSearchIndexScanTimedOut: () =>
594 + Effect.sync(() => {
595 + initialScanTimedOut = true;
596 + }),
597 }),
598 );
599
@@ -609,7 +611,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
611 const runSearch = Effect.fn("WorkspaceSearchIndex.runSearch")(function* <A>(
612 query: string,
613 pageSize: number,
612 - operation: "directorySearch" | "fileSearch" | "grep" | "mixedSearch",
614 + operation: "directorySearch" | "fileSearch" | "getScanProgress" | "grep" | "mixedSearch",
615 execute: () => Promise<Result<A>>,
616 ): Effect.fn.Return<A, WorkspaceSearchIndexSearchFailed> {
617 // fff's searches wait on its picker lock, which its watcher can hold for minutes.
@@ -1036,6 +1038,20 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
1038 const searchContents: WorkspaceSearchIndex["Service"]["searchContents"] = Effect.fn(
1039 "WorkspaceSearchIndex.searchContents",
1040 )(function* (input) {
1041 + if (initialScanTimedOut) {
1042 + const progress = yield* runSearch(input.query, input.limit, "getScanProgress", () =>
1043 + finder.getScanProgressAsync(),
1044 + );
1045 + // Files the first scan has not indexed yet would read as "No results found".
1046 + if (!progress.isWarmupComplete) {
1047 + return yield* new WorkspaceSearchIndexScanTimedOut({
1048 + cwd,
1049 + timeout: WORKSPACE_INDEX_SCAN_TIMEOUT,
1050 + });
1051 + }
1052 + initialScanTimedOut = false;
1053 + }
1054 +
1055 let { searchQuery, regexMode } = buildContentSearchQuery(input);
1056 const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS;
1057 // Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing.
apps/web/src/state/queries.ts
+20
@@ -13,6 +13,7 @@ import {
13 type ThreadSearchScope,
14 } from "@t3tools/client-runtime/state/thread-search";
15 import {
16 + ProjectSearchContentsError,
17 PROJECT_SEARCH_CONTENTS_QUERY_MAX_LENGTH,
18 THREAD_SEARCH_QUERY_MAX_LENGTH,
19 } from "@t3tools/contracts";
@@ -30,6 +31,7 @@ import type {
31 VcsRef,
32 } from "@t3tools/contracts";
33 import * as Cause from "effect/Cause";
34 +import * as Schema from "effect/Schema";
35 import { AsyncResult, Atom } from "effect/reactivity";
36 import { useEffect, useState } from "react";
37
@@ -46,6 +48,8 @@ const PROJECT_PATH_SEARCH_DEBOUNCE_MS = 120;
48 const COMPOSER_PATH_SEARCH_LIMIT = 80;
49 const PROJECT_CONTENT_SEARCH_DEBOUNCE_MS = 120;
50 const PROJECT_CONTENT_SEARCH_LIMIT = 500;
51 +const PROJECT_CONTENT_SEARCH_SCAN_RETRY_MS = 30_000;
52 +const isProjectSearchContentsError = Schema.is(ProjectSearchContentsError);
53 const THREAD_SEARCH_DEBOUNCE_MS = 120;
54 const VCS_REF_LIST_LIMIT = 100;
55 const EMPTY_REFS: ReadonlyArray<VcsRef> = [];
@@ -390,6 +394,22 @@ export function useProjectContentSearch(target: ProjectContentSearchTarget) {
394 })
395 : null,
396 );
397 + // Only once the timeout has come back: a refresh restarts a search still in flight.
398 + const settledScanTimeout =
399 + !result.isPending &&
400 + isProjectSearchContentsError(result.failure) &&
401 + result.failure.failure === "search_index_scan_timed_out"
402 + ? result.failure
403 + : null;
404 + const { refresh } = result;
405 + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- retry timer: nothing tells the client when the server's first scan ends
406 + useEffect(() => {
407 + if (settledScanTimeout === null) return;
408 + const timer = window.setTimeout(refresh, PROJECT_CONTENT_SEARCH_SCAN_RETRY_MS);
409 + return () => {
410 + window.clearTimeout(timer);
411 + };
412 + }, [refresh, settledScanTimeout]);
413
414 return {
415 canReadFiles,