Skip to content

Commit e03b696

Browse files
only21milclaude
andauthored
fix(server): keep the live search index when a refresh times out (#23182)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e8b877c commit e03b696

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

‎apps/server/src/workspace/WorkspaceEntries.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @effect-diagnostics nodeBuiltinImport:off
22
import * as NodeFSP from "node:fs/promises";
33
import * as NodeServices from "@effect/platform-node/NodeServices";
4+
import { FileFinder } from "@ff-labs/fff-node";
45
import { it, afterEach, describe, expect } from "@effect/vitest";
56
import * as Effect from "effect/Effect";
67
import * as FileSystem from "effect/FileSystem";
@@ -673,6 +674,53 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
673674
expect(match.lineContent.slice(range.start, range.end)).toBe("wörld");
674675
}),
675676
);
677+
678+
it.effect("keeps answering from the live index when a refresh outlasts the scan wait", () =>
679+
Effect.gen(function* () {
680+
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-refresh-" });
681+
yield* writeTextFile(cwd, "src/notes.ts", "const needle = 1;\n");
682+
let scansOutlastWait = false;
683+
let finders = 0;
684+
let rescans = 0;
685+
const createFinder = FileFinder.create.bind(FileFinder);
686+
vi.spyOn(FileFinder, "create").mockImplementation((options) => {
687+
const created = createFinder(options);
688+
if (created.ok) {
689+
const finder = created.value;
690+
finders += 1;
691+
const scanFiles = finder.scanFiles.bind(finder);
692+
finder.scanFiles = () => {
693+
rescans += 1;
694+
return scanFiles();
695+
};
696+
const waitForIndexReady = finder.waitForIndexReady.bind(finder);
697+
finder.waitForIndexReady = (timeoutMs) =>
698+
scansOutlastWait
699+
? Promise.resolve({ ok: true, value: false })
700+
: waitForIndexReady(timeoutMs);
701+
}
702+
return created;
703+
});
704+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
705+
const searchNeedle = workspaceEntries.searchContents({
706+
cwd,
707+
query: "needle",
708+
limit: 100,
709+
caseSensitive: true,
710+
wholeWord: false,
711+
useRegex: false,
712+
});
713+
yield* searchNeedle;
714+
715+
scansOutlastWait = true;
716+
yield* workspaceEntries.refresh(cwd);
717+
const result = yield* searchNeedle;
718+
719+
expect(result.matches.map((match) => match.path)).toEqual(["src/notes.ts"]);
720+
// The second search ran on the rescanned live finder, not a rebuild.
721+
expect({ finders, rescans }).toEqual({ finders: 1, rescans: 1 });
722+
}),
723+
);
676724
});
677725

678726
describe("browse", () => {

‎apps/server/src/workspace/WorkspaceSearchIndex.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ export class WorkspaceSearchIndex extends Context.Service<
117117
readonly searchContents: (
118118
input: Omit<ProjectSearchContentsInput, "cwd">,
119119
) => Effect.Effect<ProjectSearchContentsResult, WorkspaceSearchIndexSearchFailed>;
120-
readonly refresh: () => Effect.Effect<
121-
void,
122-
WorkspaceSearchIndexRefreshFailed | WorkspaceSearchIndexScanTimedOut
123-
>;
120+
readonly refresh: () => Effect.Effect<void, WorkspaceSearchIndexRefreshFailed>;
124121
}
125122
>()("t3/workspace/WorkspaceSearchIndex") {}
126123

@@ -419,6 +416,8 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
419416
reason: result.error,
420417
});
421418
}
419+
// A rescan that outlasts the wait keeps running, and the finder answers
420+
// from its previous index until it finishes, so the index stays usable.
422421
yield* waitForIndexReady(
423422
cwd,
424423
finder,
@@ -428,7 +427,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
428427
reason,
429428
cause,
430429
}),
431-
);
430+
).pipe(Effect.catchTags({ WorkspaceSearchIndexScanTimedOut: () => Effect.void }));
432431
});
433432

434433
const list: WorkspaceSearchIndex["Service"]["list"] = Effect.fn("WorkspaceSearchIndex.list")(

0 commit comments

Comments
 (0)