From 75db767bb8de070cdd8a720855c5fde6e3f1edae Mon Sep 17 00:00:00 2001 From: antoniooo12 <57302245+antoniooo12@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:43:34 +0200 Subject: [PATCH 1/2] fix(server): a finished run no longer keeps an unused file index alive Every finished run and new-file write refreshes the project's search index through WorkspaceSearchIndexMap.get, and each release restarts the map's 15-minute idle timer. An index created by one search therefore lived for as long as runs kept finishing in that directory, and every run re-walked the whole tree. WorkspaceEntries now records when each index was last searched, and refresh releases an index nobody searched within the idle timeout instead of rescanning it. Co-Authored-By: Claude Opus 5.5 --- .../src/workspace/WorkspaceEntries.test.ts | 38 ++++++++++++++++ apps/server/src/workspace/WorkspaceEntries.ts | 43 ++++++++++--------- .../src/workspace/WorkspaceSearchIndex.ts | 2 +- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/apps/server/src/workspace/WorkspaceEntries.test.ts b/apps/server/src/workspace/WorkspaceEntries.test.ts index 363764ff70e8d..446f231cff607 100644 --- a/apps/server/src/workspace/WorkspaceEntries.test.ts +++ b/apps/server/src/workspace/WorkspaceEntries.test.ts @@ -8,6 +8,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; import * as PlatformError from "effect/PlatformError"; +import * as TestClock from "effect/testing/TestClock"; import { vi } from "vite-plus/test"; import * as ServerConfig from "../config.ts"; @@ -712,6 +713,43 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { } }), ); + + it.effect("releases an index nobody searched within the idle timeout on refresh", () => + Effect.gen(function* () { + const cwd = yield* makeTempDir({ prefix: "t3code-workspace-idle-refresh-" }); + yield* writeTextFile(cwd, "src/index.ts"); + let rescans = 0; + let destroyed = 0; + const createFinder = FileFinder.create.bind(FileFinder); + vi.spyOn(FileFinder, "create").mockImplementation((options) => { + const created = createFinder(options); + if (created.ok) { + const finder = created.value; + const scanFilesAsync = finder.scanFilesAsync.bind(finder); + finder.scanFilesAsync = () => { + rescans += 1; + return scanFilesAsync(); + }; + const destroy = finder.destroy.bind(finder); + finder.destroy = () => { + destroyed += 1; + destroy(); + }; + } + return created; + }); + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; + yield* TestClock.adjust("1 hour"); + yield* searchWorkspaceEntries({ cwd, query: "index", limit: 10 }); + + for (let run = 0; run < 3; run++) { + yield* TestClock.adjust("6 minutes"); + yield* workspaceEntries.refresh(cwd); + } + + expect({ rescans, destroyed }).toEqual({ rescans: 2, destroyed: 1 }); + }).pipe(Effect.provide(TestClock.layer())), + ); }); describe("searchContents", () => { diff --git a/apps/server/src/workspace/WorkspaceEntries.ts b/apps/server/src/workspace/WorkspaceEntries.ts index ad1d2c2ae0ebe..bd4e336f99f6a 100644 --- a/apps/server/src/workspace/WorkspaceEntries.ts +++ b/apps/server/src/workspace/WorkspaceEntries.ts @@ -1,7 +1,9 @@ // @effect-diagnostics nodeBuiltinImport:off import * as NodeFSP from "node:fs/promises"; +import * as Clock from "effect/Clock"; import * as Context from "effect/Context"; +import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; @@ -165,6 +167,11 @@ export const make = Effect.gen(function* () { return yield* workspacePaths.normalizeWorkspaceRoot(cwd); }); + // Refreshing goes through `get`, which restarts the map's idle timer, so idleness is measured + // from the last search, not the last refresh. + const lastSearchedAt = new Map(); + const indexIdleMillis = Duration.toMillis(WorkspaceSearchIndex.WORKSPACE_INDEX_IDLE_TTL); + const refresh: WorkspaceEntries["Service"]["refresh"] = Effect.fn("WorkspaceEntries.refresh")( function* (cwd) { const normalizedCwd = yield* normalizeWorkspaceRoot(cwd).pipe( @@ -175,6 +182,12 @@ export const make = Effect.gen(function* () { if (!(yield* RcMap.has(workspaceSearchIndexes.rcMap, indexKey))) { continue; } + const searchedAt = lastSearchedAt.get(indexKey) ?? 0; + if ((yield* Clock.currentTimeMillis) - searchedAt >= indexIdleMillis) { + lastSearchedAt.delete(indexKey); + yield* workspaceSearchIndexes.invalidate(indexKey); + continue; + } const recoverRefreshFailure = Effect.fn("WorkspaceEntries.recoverRefreshFailure")( function* ( cause: @@ -258,16 +271,12 @@ export const make = Effect.gen(function* () { const normalizedQuery = normalizeSearchQuery(input.query, { trimLeadingPattern: /^[@./]+/, }); + const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths"); + lastSearchedAt.set(indexKey, yield* Clock.currentTimeMillis); const result = yield* Effect.gen(function* () { const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex; return yield* searchIndex.search(normalizedQuery, input.limit, input.kind, input.imageOnly); - }).pipe( - Effect.provide( - workspaceSearchIndexes.get( - WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths"), - ), - ), - ); + }).pipe(Effect.provide(workspaceSearchIndexes.get(indexKey))); if (input.kind === "directory" || input.imageOnly) return result; const worktreeFiles = yield* findNestedWorktreeFiles( normalizedCwd, @@ -356,16 +365,12 @@ export const make = Effect.gen(function* () { "WorkspaceEntries.searchContents", )(function* (input) { const normalizedCwd = yield* normalizeWorkspaceRoot(input.cwd); + const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "content"); + lastSearchedAt.set(indexKey, yield* Clock.currentTimeMillis); return yield* Effect.gen(function* () { const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex; return yield* searchIndex.searchContents(input); - }).pipe( - Effect.provide( - workspaceSearchIndexes.get( - WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "content"), - ), - ), - ); + }).pipe(Effect.provide(workspaceSearchIndexes.get(indexKey))); }); const list: WorkspaceEntries["Service"]["list"] = Effect.fn("WorkspaceEntries.list")( @@ -540,16 +545,12 @@ export const make = Effect.gen(function* () { truncated: false, }; } + const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths"); + lastSearchedAt.set(indexKey, yield* Clock.currentTimeMillis); return yield* Effect.gen(function* () { const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex; return yield* searchIndex.list(); - }).pipe( - Effect.provide( - workspaceSearchIndexes.get( - WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths"), - ), - ), - ); + }).pipe(Effect.provide(workspaceSearchIndexes.get(indexKey))); }, ); diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.ts b/apps/server/src/workspace/WorkspaceSearchIndex.ts index e862a5d5325aa..13a54f27e40d6 100644 --- a/apps/server/src/workspace/WorkspaceSearchIndex.ts +++ b/apps/server/src/workspace/WorkspaceSearchIndex.ts @@ -54,7 +54,7 @@ const WORKSPACE_INDEX_MAX_ENTRIES = 25_000; const WORKSPACE_INDEX_PAGE_SIZE = WORKSPACE_INDEX_MAX_ENTRIES + 2; const WORKSPACE_INDEX_SCAN_TIMEOUT = "15 seconds"; const WORKSPACE_INDEX_SCAN_TIMEOUT_MS = 15_000; -const WORKSPACE_INDEX_IDLE_TTL = "15 minutes"; +export const WORKSPACE_INDEX_IDLE_TTL = "15 minutes"; const CONTENT_SEARCH_TIME_BUDGET_MS = 250; const CONTENT_SEARCH_MAX_MATCHES_PER_FILE = 100; const GIT_GREP_MAX_OUTPUT_BYTES = 4 * 1024 * 1024; From 0e2ad5c482d203d29b4df7c5f20499674429b577 Mon Sep 17 00:00:00 2001 From: antoniooo12 <57302245+antoniooo12@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:25:53 +0200 Subject: [PATCH 2/2] fix(server): drop search records older than the idle timeout A missing record already counts as idle, so expired ones can go; this keeps the map to the roots searched within the timeout. --- apps/server/src/workspace/WorkspaceEntries.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/apps/server/src/workspace/WorkspaceEntries.ts b/apps/server/src/workspace/WorkspaceEntries.ts index bd4e336f99f6a..0be5321078761 100644 --- a/apps/server/src/workspace/WorkspaceEntries.ts +++ b/apps/server/src/workspace/WorkspaceEntries.ts @@ -171,6 +171,13 @@ export const make = Effect.gen(function* () { // from the last search, not the last refresh. const lastSearchedAt = new Map(); const indexIdleMillis = Duration.toMillis(WorkspaceSearchIndex.WORKSPACE_INDEX_IDLE_TTL); + const markSearched = (indexKey: string, now: number) => { + // A record older than the timeout counts the same as none, so it can go. + for (const [key, searchedAt] of lastSearchedAt) { + if (now - searchedAt >= indexIdleMillis) lastSearchedAt.delete(key); + } + lastSearchedAt.set(indexKey, now); + }; const refresh: WorkspaceEntries["Service"]["refresh"] = Effect.fn("WorkspaceEntries.refresh")( function* (cwd) { @@ -272,7 +279,7 @@ export const make = Effect.gen(function* () { trimLeadingPattern: /^[@./]+/, }); const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths"); - lastSearchedAt.set(indexKey, yield* Clock.currentTimeMillis); + markSearched(indexKey, yield* Clock.currentTimeMillis); const result = yield* Effect.gen(function* () { const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex; return yield* searchIndex.search(normalizedQuery, input.limit, input.kind, input.imageOnly); @@ -366,7 +373,7 @@ export const make = Effect.gen(function* () { )(function* (input) { const normalizedCwd = yield* normalizeWorkspaceRoot(input.cwd); const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "content"); - lastSearchedAt.set(indexKey, yield* Clock.currentTimeMillis); + markSearched(indexKey, yield* Clock.currentTimeMillis); return yield* Effect.gen(function* () { const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex; return yield* searchIndex.searchContents(input); @@ -546,7 +553,7 @@ export const make = Effect.gen(function* () { }; } const indexKey = WorkspaceSearchIndex.workspaceSearchIndexKey(normalizedCwd, "paths"); - lastSearchedAt.set(indexKey, yield* Clock.currentTimeMillis); + markSearched(indexKey, yield* Clock.currentTimeMillis); return yield* Effect.gen(function* () { const searchIndex = yield* WorkspaceSearchIndex.WorkspaceSearchIndex; return yield* searchIndex.list();