Skip to content

Commit 7f97059

Browse files
committed
fix(server): keep index recovery outside the content search budget
1 parent 6ef99ea commit 7f97059

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,20 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
157157
value: grepResult("needleSuffix", [[0, 6]], nextCursor),
158158
},
159159
);
160+
using now = vi.spyOn(performance, "now").mockReturnValue(0);
160161
const finder = {
161162
destroy: vi.fn(),
162163
waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
164+
scanFiles: () => {
165+
now.mockReturnValue(1_000);
166+
return { ok: true as const, value: undefined };
167+
},
163168
grep,
164169
} as unknown as FileFinder;
165170
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
166171

167172
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project", "content");
173+
yield* TestClock.adjust("5 minutes");
168174
const result = yield* searchIndex.searchContents({
169175
query: "needle",
170176
limit: 1,

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,6 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
384384
operation: "directorySearch" | "fileSearch" | "grep" | "mixedSearch",
385385
execute: () => Result<A>,
386386
): Effect.fn.Return<A, WorkspaceSearchIndexSearchFailed> {
387-
// Watchers can lose events; repair hot indexes occasionally, rather than after every turn.
388-
yield* Effect.gen(function* () {
389-
const now = yield* Clock.currentTimeMillis;
390-
if (now - refreshedAt < WORKSPACE_INDEX_RECOVERY_INTERVAL_MS) return;
391-
refreshedAt = now;
392-
yield* refresh().pipe(
393-
Effect.catch((cause) =>
394-
Effect.logWarning("Periodic workspace index recovery failed", { cwd, cause }),
395-
),
396-
);
397-
}).pipe(refreshPermit.withPermits(1));
398387
const result = yield* Effect.try({
399388
try: execute,
400389
catch: (cause) =>
@@ -448,8 +437,21 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
448437
refreshedAt = yield* Clock.currentTimeMillis;
449438
});
450439

440+
// Watchers can lose events; repair hot indexes occasionally, rather than after every turn.
441+
const recover = Effect.fn("WorkspaceSearchIndex.recover")(function* () {
442+
const now = yield* Clock.currentTimeMillis;
443+
if (now - refreshedAt < WORKSPACE_INDEX_RECOVERY_INTERVAL_MS) return;
444+
refreshedAt = now;
445+
yield* refresh().pipe(
446+
Effect.catch((cause) =>
447+
Effect.logWarning("Periodic workspace index recovery failed", { cwd, cause }),
448+
),
449+
);
450+
}, refreshPermit.withPermits(1));
451+
451452
const list: WorkspaceSearchIndex["Service"]["list"] = Effect.fn("WorkspaceSearchIndex.list")(
452453
function* () {
454+
yield* recover();
453455
const incompleteBeforeQuery = hasIncompleteInitialScan();
454456
const result = yield* runSearch("", WORKSPACE_INDEX_PAGE_SIZE, "mixedSearch", () =>
455457
finder.mixedSearch("", { pageSize: WORKSPACE_INDEX_PAGE_SIZE }),
@@ -470,6 +472,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
470472
const search: WorkspaceSearchIndex["Service"]["search"] = Effect.fn(
471473
"WorkspaceSearchIndex.search",
472474
)(function* (query, limit, kind, imageOnly) {
475+
yield* recover();
473476
const pageSize = imageOnly ? WORKSPACE_INDEX_PAGE_SIZE : Math.max(1, limit + 1);
474477
const incompleteBeforeQuery = hasIncompleteInitialScan();
475478
if (kind === "file" || imageOnly) {
@@ -496,6 +499,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
496499
const searchContents: WorkspaceSearchIndex["Service"]["searchContents"] = Effect.fn(
497500
"WorkspaceSearchIndex.searchContents",
498501
)(function* (input) {
502+
yield* recover();
499503
const { searchQuery, regexMode } = buildContentSearchQuery(input);
500504
const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS;
501505
// Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing.

0 commit comments

Comments
 (0)