Skip to content

Commit 394f707

Browse files
committed
fix(server): retain search workers after recoverable errors
1 parent 1db36c6 commit 394f707

6 files changed

Lines changed: 77 additions & 11 deletions

File tree

‎apps/server/scripts/workspace-search-mock.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { SearchRequest, SearchResponse } from "../src/workspace/workspaceSearchP
66
import {
77
WorkspaceSearchIndexRefreshFailed,
88
WorkspaceSearchIndexScanTimedOut,
9+
WorkspaceSearchIndexSearchFailed,
910
} from "../src/workspace/WorkspaceSearchIndexService.ts";
1011

1112
const decodeSearchRequest = Schema.decodeUnknownSync(SearchRequest);
@@ -42,6 +43,21 @@ process.on("message", (message) => {
4243
return;
4344
}
4445
if (input.method === "search" && input.query === "crash") process.exit(1);
46+
if (input.method === "search" && input.query === "search-error") {
47+
process.send?.(
48+
encodeSearchResponse(
49+
Exit.fail(
50+
new WorkspaceSearchIndexSearchFailed({
51+
cwd,
52+
queryLength: input.query.length,
53+
pageSize: input.limit + 1,
54+
reason: "search rejected",
55+
}),
56+
),
57+
),
58+
);
59+
return;
60+
}
4561
if (input.method === "search" && input.query === "hold") {
4662
const socket = NodeNet.connect(
4763
Number(process.env.T3_SEARCH_TEST_RECEIPT_PORT),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview";
2121

2222
import {
2323
WorkspaceSearchIndex,
24+
WORKSPACE_INDEX_MAX_ENTRIES,
25+
WORKSPACE_INDEX_PAGE_SIZE,
2426
WorkspaceSearchIndexCreateFailed,
2527
WorkspaceSearchIndexDestroyFailed,
2628
WorkspaceSearchIndexRefreshFailed,
@@ -31,8 +33,6 @@ import {
3133

3234
export * from "./WorkspaceSearchIndexService.ts";
3335

34-
const WORKSPACE_INDEX_MAX_ENTRIES = 25_000;
35-
const WORKSPACE_INDEX_PAGE_SIZE = WORKSPACE_INDEX_MAX_ENTRIES + 2;
3636
const WORKSPACE_INDEX_SCAN_TIMEOUT = "15 seconds";
3737
const WORKSPACE_INDEX_SCAN_TIMEOUT_MS = 15_000;
3838
const CONTENT_SEARCH_TIME_BUDGET_MS = 250;

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import * as Context from "effect/Context";
2+
import * as Cause from "effect/Cause";
23
import * as Effect from "effect/Effect";
34
import * as Layer from "effect/Layer";
5+
import * as Schema from "effect/Schema";
46
import * as Semaphore from "effect/Semaphore";
57
import {
68
startSearchProcess,
79
WorkspaceSearchProcessFailed,
810
type SearchProcess,
911
} from "./WorkspaceSearchProcess.ts";
10-
import type { WorkspaceSearchIndexVariant } from "./WorkspaceSearchIndexService.ts";
12+
import {
13+
WorkspaceSearchIndexSearchFailed,
14+
WorkspaceSearchIndexRefreshFailed,
15+
type WorkspaceSearchIndexVariant,
16+
} from "./WorkspaceSearchIndexService.ts";
1117
import type { SearchOperation } from "./workspaceSearchProtocol.ts";
1218

19+
const isRecoverable = Schema.is(
20+
Schema.Union([WorkspaceSearchIndexSearchFailed, WorkspaceSearchIndexRefreshFailed]),
21+
);
22+
1323
const make = Effect.gen(function* () {
1424
const semaphore = yield* Semaphore.make(1);
1525
let current: { process: SearchProcess; indexes: Set<number> } | undefined;
@@ -58,7 +68,14 @@ const make = Effect.gen(function* () {
5868
? null
5969
: yield* active.process.request({ id, operation });
6070
}).pipe(
61-
Effect.onError(stop),
71+
Effect.onError((cause) =>
72+
cause.reasons.length > 0 &&
73+
cause.reasons.every(
74+
(reason) => Cause.isFailReason(reason) && isRecoverable(reason.error),
75+
)
76+
? Effect.void
77+
: stop(),
78+
),
6279
Effect.ensuring(
6380
Effect.sync(() => {
6481
activeIndex = undefined;

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ it.effect("cancels a queued request without terminating the active search proces
220220
yield* Effect.gen(function* () {
221221
const index = yield* Index.make("workspace");
222222
const other = yield* Index.make("other");
223-
const blocked = yield* index.search("block", 1).pipe(Effect.forkChild);
223+
const before = yield* index.list();
224+
const active = yield* index.search("hold", 1).pipe(Effect.forkChild);
224225
yield* Deferred.await(control.blocked);
225226
const queued = yield* other.list().pipe(Effect.forkChild);
226227
yield* Fiber.interrupt(queued);
227-
expect(yield* Deferred.isDone(control.exited)).toBe(false);
228-
yield* Fiber.interrupt(blocked);
229-
yield* Deferred.await(control.exited);
230-
expect((yield* other.list()).entries).toHaveLength(1);
228+
yield* control.release;
229+
expect(yield* Fiber.join(active)).toEqual(before);
230+
expect((yield* other.list()).entries).toEqual(before.entries);
231231
}).pipe(Effect.provideService(HostProcessEnvironment, control.environment));
232232
}).pipe(withWorker),
233233
),
@@ -255,3 +255,24 @@ it.effect("lets an active workspace finish while an unrelated index expires", ()
255255
}).pipe(withWorker),
256256
),
257257
);
258+
259+
it.effect("preserves the shared process after ordinary search and refresh errors", () =>
260+
Effect.scoped(
261+
Effect.gen(function* () {
262+
const index = yield* Index.make("workspace");
263+
const other = yield* Index.make("other");
264+
const before = yield* other.list();
265+
expect(yield* Effect.flip(index.search("search-error", 1))).toMatchObject({
266+
_tag: "WorkspaceSearchIndexSearchFailed",
267+
reason: "search rejected",
268+
});
269+
expect((yield* other.list()).entries).toEqual(before.entries);
270+
expect(yield* Effect.flip(index.refresh())).toMatchObject({
271+
_tag: "WorkspaceSearchIndexRefreshFailed",
272+
reason: "scan failed",
273+
});
274+
expect((yield* other.list()).entries).toEqual(before.entries);
275+
expect((yield* index.list()).entries).toEqual(before.entries);
276+
}).pipe(withWorker),
277+
),
278+
);

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "@t3tools/contracts";
1010
import {
1111
WorkspaceSearchIndex,
12+
WORKSPACE_INDEX_PAGE_SIZE,
1213
WorkspaceSearchIndexCreateFailed,
1314
WorkspaceSearchIndexRefreshFailed,
1415
WorkspaceSearchIndexScanTimedOut,
@@ -59,13 +60,21 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
5960
list: () =>
6061
remote
6162
.request({ method: "list" })
62-
.pipe(Effect.flatMap(decodeList), Effect.mapError(searchFailure(0, 25_002))),
63+
.pipe(
64+
Effect.flatMap(decodeList),
65+
Effect.mapError(searchFailure(0, WORKSPACE_INDEX_PAGE_SIZE)),
66+
),
6367
search: (query, limit, kind, imageOnly) =>
6468
remote
6569
.request({ method: "search", query, limit, kind, imageOnly })
6670
.pipe(
6771
Effect.flatMap(decodeSearch),
68-
Effect.mapError(searchFailure(query.length, limit + 1)),
72+
Effect.mapError(
73+
searchFailure(
74+
query.length,
75+
imageOnly ? WORKSPACE_INDEX_PAGE_SIZE : Math.max(1, limit + 1),
76+
),
77+
),
6978
),
7079
searchContents: (input) =>
7180
remote

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import type {
99
ProjectSearchEntriesResult,
1010
} from "@t3tools/contracts";
1111

12+
export const WORKSPACE_INDEX_MAX_ENTRIES = 25_000;
13+
export const WORKSPACE_INDEX_PAGE_SIZE = WORKSPACE_INDEX_MAX_ENTRIES + 2;
14+
1215
export class WorkspaceSearchIndexCreateFailed extends Schema.TaggedError<WorkspaceSearchIndexCreateFailed>()(
1316
"WorkspaceSearchIndexCreateFailed",
1417
{

0 commit comments

Comments
 (0)