The best way to control Claude Code, Codex and more from any computer or phone
fix(server): a finished run no longer keeps an unused file index alive (#38585)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Anton Omelianenko committed
Nov 18, 2026 at 16:25 UTC
8cf97b5f06f30a872ce30ab8a888f2ff012365a2
3 files changed
+68
-22
apps/server/src/workspace/WorkspaceEntries.test.ts
+38
@@ -8,6 +8,7 @@ import * as FileSystem from "effect/FileSystem";
8
import * as Layer from "effect/Layer";
9
import * as Path from "effect/Path";
10
import * as PlatformError from "effect/PlatformError";
11
+import * as TestClock from "effect/testing/TestClock";
12
import { vi } from "vite-plus/test";
13
14
import * as ServerConfig from "../config.ts";
@@ -712,6 +713,43 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
713
}
714
}),
715
);
716
+
717
+ it.effect("releases an index nobody searched within the idle timeout on refresh", () =>
718
+ Effect.gen(function* () {
719
+ const cwd = yield* makeTempDir({ prefix: "t3code-workspace-idle-refresh-" });
720
+ yield* writeTextFile(cwd, "src/index.ts");
721
+ let rescans = 0;
722
+ let destroyed = 0;
723
+ const createFinder = FileFinder.create.bind(FileFinder);
724
+ vi.spyOn(FileFinder, "create").mockImplementation((options) => {
725
+ const created = createFinder(options);
726
+ if (created.ok) {
727
+ const finder = created.value;
728
+ const scanFilesAsync = finder.scanFilesAsync.bind(finder);
729
+ finder.scanFilesAsync = () => {
730
+ rescans += 1;
731
+ return scanFilesAsync();
732
+ };
733
+ const destroy = finder.destroy.bind(finder);
734
+ finder.destroy = () => {
735
+ destroyed += 1;
736
+ destroy();
737
+ };
738
+ }
739
+ return created;
740
+ });
741
+ const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
742
+ yield* TestClock.adjust("1 hour");
743
+ yield* searchWorkspaceEntries({ cwd, query: "index", limit: 10 });
744
+
745
+ for (let run = 0; run < 3; run++) {
746
+ yield* TestClock.adjust("6 minutes");
747
+ yield* workspaceEntries.refresh(cwd);
748
+ }
749
+
750
+ expect({ rescans, destroyed }).toEqual({ rescans: 2, destroyed: 1 });
751
+ }).pipe(Effect.provide(TestClock.layer())),
752
+ );
753
});
754
755
describe("searchContents", () => {
apps/server/src/workspace/WorkspaceEntries.ts
+29
-21
@@ -1,7 +1,9 @@
1
// @effect-diagnostics nodeBuiltinImport:off
2
import * as NodeFSP from "node:fs/promises";
3
4
+import * as Clock from "effect/Clock";
5
import * as Context from "effect/Context";
6
+import * as Duration from "effect/Duration";
7
import * as Effect from "effect/Effect";
8
import * as Layer from "effect/Layer";
9
import * as Path from "effect/Path";
@@ -165,6 +167,18 @@ export const make = Effect.gen(function* () {
167
return yield* workspacePaths.normalizeWorkspaceRoot(cwd);
168
});
169
170
+ // Refreshing goes through `get`, which restarts the map's idle timer, so idleness is measured
171
+ // from the last search, not the last refresh.
172
+ const lastSearchedAt = new Map<string, number>();
173
+ const indexIdleMillis = Duration.toMillis(WorkspaceSearchIndex.WORKSPACE_INDEX_IDLE_TTL);
174
+ const markSearched = (indexKey: string, now: number) => {
175
+ // A record older than the timeout counts the same as none, so it can go.
176
+ for (const [key, searchedAt] of lastSearchedAt) {
177
+ if (now - searchedAt >= indexIdleMillis) lastSearchedAt.delete(key);
178
+ }
179
+ lastSearchedAt.set(indexKey, now);
180
+ };
181
+
182
const refresh: WorkspaceEntries["Service"]["refresh"] = Effect.fn("WorkspaceEntries.refresh")(
183
function* (cwd) {
184
const normalizedCwd = yield* normalizeWorkspaceRoot(cwd).pipe(
@@ -175,6 +189,12 @@ export const make = Effect.gen(function* () {
189
if (!(yield* RcMap.has(workspaceSearchIndexes.rcMap, indexKey))) {
190
continue;
191
}
192
+ const searchedAt = lastSearchedAt.get(indexKey) ?? 0;
193
+ if ((yield* Clock.currentTimeMillis) - searchedAt >= indexIdleMillis) {
194
+ lastSearchedAt.delete(indexKey);
195
+ yield* workspaceSearchIndexes.invalidate(indexKey);
196
+ continue;
197
+ }
198
const recoverRefreshFailure = Effect.fn("WorkspaceEntries.recoverRefreshFailure")(
199
function* (
200
cause:
@@ -258,16 +278,12 @@ export const make = Effect.gen(function* () {
278
const normalizedQuery = normalizeSearchQuery(input.query, {
279
trimLeadingPattern: /^[@./]+/,
280
});
281
+ const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths");
282
+ markSearched(indexKey, yield* Clock.currentTimeMillis);
283
const result = yield* Effect.gen(function* () {
284
const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex;
285
return yield* searchIndex.search(normalizedQuery, input.limit, input.kind, input.imageOnly);
264
- }).pipe(
265
- Effect.provide(
266
- workspaceSearchIndexes.get(
267
- WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths"),
268
- ),
269
- ),
270
- );
286
+ }).pipe(Effect.provide(workspaceSearchIndexes.get(indexKey)));
287
if (input.kind === "directory" || input.imageOnly) return result;
288
const worktreeFiles = yield* findNestedWorktreeFiles(
289
normalizedCwd,
@@ -356,16 +372,12 @@ export const make = Effect.gen(function* () {
372
"WorkspaceEntries.searchContents",
373
)(function* (input) {
374
const normalizedCwd = yield* normalizeWorkspaceRoot(input.cwd);
375
+ const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "content");
376
+ markSearched(indexKey, yield* Clock.currentTimeMillis);
377
return yield* Effect.gen(function* () {
378
const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex;
379
return yield* searchIndex.searchContents(input);
362
- }).pipe(
363
- Effect.provide(
364
- workspaceSearchIndexes.get(
365
- WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "content"),
366
- ),
367
- ),
368
- );
380
+ }).pipe(Effect.provide(workspaceSearchIndexes.get(indexKey)));
381
});
382
383
const list: WorkspaceEntries["Service"]["list"] = Effect.fn("WorkspaceEntries.list")(
@@ -540,16 +552,12 @@ export const make = Effect.gen(function* () {
552
truncated: false,
553
};
554
}
555
+ const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths");
556
+ markSearched(indexKey, yield* Clock.currentTimeMillis);
557
return yield* Effect.gen(function* () {
558
const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex;
559
return yield* searchIndex.list();
546
- }).pipe(
547
- Effect.provide(
548
- workspaceSearchIndexes.get(
549
- WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths"),
550
- ),
551
- ),
552
- );
560
+ }).pipe(Effect.provide(workspaceSearchIndexes.get(indexKey)));
561
},
562
);
563
apps/server/src/workspace/WorkspaceSearchIndex.ts
+1
-1
@@ -54,7 +54,7 @@ const WORKSPACE_INDEX_MAX_ENTRIES = 25_000;
54
const WORKSPACE_INDEX_PAGE_SIZE = WORKSPACE_INDEX_MAX_ENTRIES + 2;
55
const WORKSPACE_INDEX_SCAN_TIMEOUT = "15 seconds";
56
const WORKSPACE_INDEX_SCAN_TIMEOUT_MS = 15_000;
57
-const WORKSPACE_INDEX_IDLE_TTL = "15 minutes";
57
+export const WORKSPACE_INDEX_IDLE_TTL = "15 minutes";
58
const CONTENT_SEARCH_TIME_BUDGET_MS = 250;
59
const CONTENT_SEARCH_MAX_MATCHES_PER_FILE = 100;
60
const GIT_GREP_MAX_OUTPUT_BYTES = 4 * 1024 * 1024;