Skip to content

Commit 3540c6e

Browse files
committed
fix(server): remove read-triggered workspace rescans
1 parent ca67542 commit 3540c6e

2 files changed

Lines changed: 18 additions & 56 deletions

File tree

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

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,40 @@ function fileItem(relativePath: string): FileItem {
2929
};
3030
}
3131

32-
it.effect("recovers missed watcher changes on reads without rescanning every read", () =>
32+
it.effect("reads never rescan after the former recovery interval", () =>
3333
Effect.scoped(
3434
Effect.gen(function* () {
35-
let indexed = "old.ts";
36-
let onDisk = indexed;
37-
const scanFiles = vi.fn(() => {
38-
indexed = onDisk;
39-
return { ok: true as const, value: undefined };
40-
});
35+
const scanFiles = vi.fn(() => ({ ok: true as const, value: undefined }));
4136
const finder = {
4237
destroy: vi.fn(),
4338
waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
4439
scanFiles,
40+
mixedSearch: () => ({ ok: true as const, value: { items: [], totalMatched: 0 } }),
4541
fileSearch: () => ({
4642
ok: true as const,
47-
value: { items: [fileItem(indexed)], scores: [], totalMatched: 1, totalFiles: 1 },
43+
value: { items: [fileItem("watched.ts")], scores: [], totalMatched: 1, totalFiles: 1 },
4844
}),
45+
grep: () => ({ ok: true as const, value: { items: [], nextCursor: null } }),
4946
} as unknown as FileFinder;
5047
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
5148
const index = yield* WorkspaceSearchIndex.make("/workspace/project");
52-
onDisk = "new.ts";
49+
yield* TestClock.adjust("10 minutes");
50+
expect(yield* index.list()).toEqual({ entries: [], truncated: false });
5351
expect((yield* index.search("", 10, "file")).entries).toEqual([
54-
{ path: "old.ts", kind: "file" },
52+
{ path: "watched.ts", kind: "file" },
5553
]);
54+
expect(
55+
yield* index.searchContents({
56+
query: "needle",
57+
limit: 1,
58+
caseSensitive: true,
59+
wholeWord: false,
60+
useRegex: false,
61+
}),
62+
).toEqual({ matches: [], truncated: false });
5663
expect(scanFiles).not.toHaveBeenCalled();
57-
yield* TestClock.adjust("5 minutes");
58-
const results = yield* Effect.all(
59-
[index.search("", 10, "file"), index.search("", 10, "file")],
60-
{ concurrency: "unbounded" },
61-
);
62-
for (const result of results) {
63-
expect(result.entries).toEqual([{ path: "new.ts", kind: "file" }]);
64-
}
65-
expect(scanFiles).toHaveBeenCalledTimes(1);
66-
yield* index.search("", 10, "file");
64+
yield* index.refresh();
6765
expect(scanFiles).toHaveBeenCalledTimes(1);
68-
scanFiles.mockImplementationOnce(() => {
69-
throw new Error("scan unavailable");
70-
});
71-
yield* TestClock.adjust("5 minutes");
72-
expect((yield* index.search("", 10, "file")).entries).toEqual([
73-
{ path: "new.ts", kind: "file" },
74-
]);
75-
yield* index.search("", 10, "file");
76-
expect(scanFiles).toHaveBeenCalledTimes(2);
7766
}),
7867
),
7968
);
@@ -157,20 +146,14 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
157146
value: grepResult("needleSuffix", [[0, 6]], nextCursor),
158147
},
159148
);
160-
using now = vi.spyOn(performance, "now").mockReturnValue(0);
161149
const finder = {
162150
destroy: vi.fn(),
163151
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-
},
168152
grep,
169153
} as unknown as FileFinder;
170154
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
171155

172156
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project", "content");
173-
yield* TestClock.adjust("5 minutes");
174157
const result = yield* searchIndex.searchContents({
175158
query: "needle",
176159
limit: 1,

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ import type {
1212
SearchResult,
1313
} from "@ff-labs/fff-node";
1414
import * as Context from "effect/Context";
15-
import * as Clock from "effect/Clock";
1615
import * as Effect from "effect/Effect";
1716
import * as Layer from "effect/Layer";
1817
import * as LayerMap from "effect/LayerMap";
1918
import * as Schema from "effect/Schema";
20-
import * as Semaphore from "effect/Semaphore";
2119

2220
import type {
2321
ProjectEntry,
@@ -38,7 +36,6 @@ const WORKSPACE_INDEX_PAGE_SIZE = WORKSPACE_INDEX_MAX_ENTRIES + 2;
3836
const WORKSPACE_INDEX_SCAN_TIMEOUT = "15 seconds";
3937
const WORKSPACE_INDEX_SCAN_TIMEOUT_MS = 15_000;
4038
const WORKSPACE_INDEX_IDLE_TTL = "15 minutes";
41-
const WORKSPACE_INDEX_RECOVERY_INTERVAL_MS = 5 * 60_000;
4239
const CONTENT_SEARCH_TIME_BUDGET_MS = 250;
4340
const CONTENT_SEARCH_MAX_MATCHES_PER_FILE = 100;
4441

@@ -375,8 +372,6 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
375372
);
376373

377374
const hasIncompleteInitialScan = () => initialScanTimedOut && finder.isScanning();
378-
const refreshPermit = yield* Semaphore.make(1);
379-
let refreshedAt = yield* Clock.currentTimeMillis;
380375

381376
const runSearch = Effect.fn("WorkspaceSearchIndex.runSearch")(function* <A>(
382377
query: string,
@@ -434,24 +429,10 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
434429
cause,
435430
}),
436431
);
437-
refreshedAt = yield* Clock.currentTimeMillis;
438432
});
439433

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-
452434
const list: WorkspaceSearchIndex["Service"]["list"] = Effect.fn("WorkspaceSearchIndex.list")(
453435
function* () {
454-
yield* recover();
455436
const incompleteBeforeQuery = hasIncompleteInitialScan();
456437
const result = yield* runSearch("", WORKSPACE_INDEX_PAGE_SIZE, "mixedSearch", () =>
457438
finder.mixedSearch("", { pageSize: WORKSPACE_INDEX_PAGE_SIZE }),
@@ -472,7 +453,6 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
472453
const search: WorkspaceSearchIndex["Service"]["search"] = Effect.fn(
473454
"WorkspaceSearchIndex.search",
474455
)(function* (query, limit, kind, imageOnly) {
475-
yield* recover();
476456
const pageSize = imageOnly ? WORKSPACE_INDEX_PAGE_SIZE : Math.max(1, limit + 1);
477457
const incompleteBeforeQuery = hasIncompleteInitialScan();
478458
if (kind === "file" || imageOnly) {
@@ -499,7 +479,6 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
499479
const searchContents: WorkspaceSearchIndex["Service"]["searchContents"] = Effect.fn(
500480
"WorkspaceSearchIndex.searchContents",
501481
)(function* (input) {
502-
yield* recover();
503482
const { searchQuery, regexMode } = buildContentSearchQuery(input);
504483
const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS;
505484
// Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing.

0 commit comments

Comments
 (0)