Skip to content

Commit e9adc6c

Browse files
only21milclaude
andcommitted
fix(server): keep live workspace indexes when a refresh outlasts its scan wait
A rescan that runs past the 15 second wait keeps going on the live finder, which still answers from its previous index. Treat that the way index creation already does instead of failing the refresh, so WorkspaceEntries.refresh no longer invalidates a working index after a file write and forces a rebuild that can time out again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a8bb769 commit e9adc6c

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @effect-diagnostics nodeBuiltinImport:off
22
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import { FileFinder } from "@ff-labs/fff-node";
34
import { it, afterEach, describe, expect } from "@effect/vitest";
45
import * as Effect from "effect/Effect";
56
import * as FileSystem from "effect/FileSystem";
@@ -569,6 +570,43 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
569570
expect(match.lineContent.slice(range.start, range.end)).toBe("wörld");
570571
}),
571572
);
573+
574+
it.effect("keeps answering from the live index when a refresh outlasts the scan wait", () =>
575+
Effect.gen(function* () {
576+
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-refresh-" });
577+
yield* writeTextFile(cwd, "src/notes.ts", "const needle = 1;\n");
578+
let scansOutlastWait = false;
579+
const createFinder = FileFinder.create.bind(FileFinder);
580+
vi.spyOn(FileFinder, "create").mockImplementation((options) => {
581+
const created = createFinder(options);
582+
if (created.ok) {
583+
const finder = created.value;
584+
const waitForIndexReady = finder.waitForIndexReady.bind(finder);
585+
finder.waitForIndexReady = (timeoutMs) =>
586+
scansOutlastWait
587+
? Promise.resolve({ ok: true, value: false })
588+
: waitForIndexReady(timeoutMs);
589+
}
590+
return created;
591+
});
592+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
593+
const searchNeedle = workspaceEntries.searchContents({
594+
cwd,
595+
query: "needle",
596+
limit: 100,
597+
caseSensitive: true,
598+
wholeWord: false,
599+
useRegex: false,
600+
});
601+
yield* searchNeedle;
602+
603+
scansOutlastWait = true;
604+
yield* workspaceEntries.refresh(cwd);
605+
const result = yield* searchNeedle;
606+
607+
expect(result.matches.map((match) => match.path)).toEqual(["src/notes.ts"]);
608+
}),
609+
);
572610
});
573611

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

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ export class WorkspaceSearchIndex extends Context.Service<
120120
readonly searchContents: (
121121
input: Omit<ProjectSearchContentsInput, "cwd">,
122122
) => Effect.Effect<ProjectSearchContentsResult, WorkspaceSearchIndexSearchFailed>;
123-
readonly refresh: () => Effect.Effect<
124-
void,
125-
WorkspaceSearchIndexRefreshFailed | WorkspaceSearchIndexScanTimedOut
126-
>;
123+
readonly refresh: () => Effect.Effect<void, WorkspaceSearchIndexRefreshFailed>;
127124
}
128125
>()("t3/workspace/WorkspaceSearchIndex") {}
129126

@@ -428,6 +425,8 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
428425
reason: result.error,
429426
});
430427
}
428+
// A rescan that outlasts the wait keeps running, and the finder answers
429+
// from its previous index until it finishes, so the index stays usable.
431430
yield* waitForIndexReady(
432431
cwd,
433432
finder,
@@ -437,7 +436,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
437436
reason,
438437
cause,
439438
}),
440-
);
439+
).pipe(Effect.catchTags({ WorkspaceSearchIndexScanTimedOut: () => Effect.void }));
441440
});
442441

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

0 commit comments

Comments
 (0)