Deduplicate workspace index builds and stabilize prompt replacement

- Reuse in-flight workspace index builds per cwd and cap `readdir` scan concurrency at 32
- Add server tests for build deduplication and directory read concurrency limits
- Track composer text in a ref so prompt replacements use current text after send/reset
This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-02-17 19:47:09 -08:00
1 parent 1b409c3946
commit 2743e45dbe
3 files changed
+122 -21

No files matched your search

+59 -1
View File
@@ -1,9 +1,10 @@
import fs from "node:fs";
import fsPromises from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { afterEach, assert, describe, it } from "vitest";
import { afterEach, assert, describe, it, vi } from "vitest";
import { searchWorkspaceEntries } from "./workspaceEntries";
@@ -30,6 +31,7 @@ function runGit(cwd: string, args: string[]): void {
describe("searchWorkspaceEntries", () => {
afterEach(() => {
vi.restoreAllMocks();
for (const dir of tempDirs.splice(0, tempDirs.length)) {
fs.rmSync(dir, { recursive: true, force: true });
}
@@ -115,4 +117,60 @@ describe("searchWorkspaceEntries", () => {
assert.include(paths, "src/keep.ts");
assert.isFalse(paths.some((entryPath) => entryPath.startsWith(".convex/")));
});
it("deduplicates concurrent index builds for the same cwd", async () => {
const cwd = makeTempDir("t3code-workspace-concurrent-build-");
writeFile(cwd, "src/components/Composer.tsx");
let rootReadCount = 0;
const originalReaddir = fsPromises.readdir.bind(fsPromises);
vi.spyOn(fsPromises, "readdir").mockImplementation(
(async (...args: Parameters<typeof fsPromises.readdir>) => {
if (args[0] === cwd) {
rootReadCount += 1;
await new Promise((resolve) => setTimeout(resolve, 20));
}
return originalReaddir(...args);
}) as typeof fsPromises.readdir,
);
await Promise.all([
searchWorkspaceEntries({ cwd, query: "", limit: 100 }),
searchWorkspaceEntries({ cwd, query: "comp", limit: 100 }),
searchWorkspaceEntries({ cwd, query: "src", limit: 100 }),
]);
assert.equal(rootReadCount, 1);
});
it("limits concurrent directory reads while walking the filesystem", async () => {
const cwd = makeTempDir("t3code-workspace-read-concurrency-");
for (let index = 0; index < 80; index += 1) {
writeFile(cwd, `group-${index}/entry-${index}.ts`, "export {};");
}
let activeReads = 0;
let peakReads = 0;
const originalReaddir = fsPromises.readdir.bind(fsPromises);
vi.spyOn(fsPromises, "readdir").mockImplementation(
(async (...args: Parameters<typeof fsPromises.readdir>) => {
const target = args[0];
if (typeof target === "string" && target.startsWith(cwd)) {
activeReads += 1;
peakReads = Math.max(peakReads, activeReads);
await new Promise((resolve) => setTimeout(resolve, 4));
try {
return await originalReaddir(...args);
} finally {
activeReads -= 1;
}
}
return originalReaddir(...args);
}) as typeof fsPromises.readdir,
);
await searchWorkspaceEntries({ cwd, query: "", limit: 200 });
assert.isAtMost(peakReads, 32);
});
});
+51 -10
View File
@@ -13,6 +13,7 @@ import {
const WORKSPACE_CACHE_TTL_MS = 15_000;
const WORKSPACE_CACHE_MAX_KEYS = 4;
const WORKSPACE_INDEX_MAX_ENTRIES = 25_000;
const WORKSPACE_SCAN_READDIR_CONCURRENCY = 32;
const IGNORED_DIRECTORY_NAMES = new Set([
".git",
".convex",
@@ -32,6 +33,7 @@ interface WorkspaceIndex {
}
const workspaceIndexCache = new Map<string, WorkspaceIndex>();
const inFlightWorkspaceIndexBuilds = new Map<string, Promise<WorkspaceIndex>>();
function toPosixPath(input: string): string {
return input.split(path.sep).join("/");
@@ -101,6 +103,31 @@ function directoryAncestorsOf(relativePath: string): string[] {
return directories;
}
async function mapWithConcurrency<TInput, TOutput>(
items: readonly TInput[],
concurrency: number,
mapper: (item: TInput, index: number) => Promise<TOutput>,
): Promise<TOutput[]> {
if (items.length === 0) {
return [];
}
const boundedConcurrency = Math.max(1, Math.min(concurrency, items.length));
const results = Array.from({ length: items.length }) as TOutput[];
let nextIndex = 0;
const workers = Array.from({ length: boundedConcurrency }, async () => {
while (nextIndex < items.length) {
const currentIndex = nextIndex;
nextIndex += 1;
results[currentIndex] = await mapper(items[currentIndex] as TInput, currentIndex);
}
});
await Promise.all(workers);
return results;
}
async function isInsideGitWorkTree(cwd: string): Promise<boolean> {
const insideWorkTree = await runProcess("git", ["rev-parse", "--is-inside-work-tree"], {
cwd,
@@ -220,8 +247,10 @@ async function buildWorkspaceIndex(cwd: string): Promise<WorkspaceIndex> {
while (pendingDirectories.length > 0 && !truncated) {
const currentDirectories = pendingDirectories;
pendingDirectories = [];
const directoryEntries = await Promise.all(
currentDirectories.map(async (relativeDir) => {
const directoryEntries = await mapWithConcurrency(
currentDirectories,
WORKSPACE_SCAN_READDIR_CONCURRENCY,
async (relativeDir) => {
const absoluteDir = relativeDir ? path.join(cwd, relativeDir) : cwd;
try {
const dirents = await fs.readdir(absoluteDir, { withFileTypes: true });
@@ -235,7 +264,7 @@ async function buildWorkspaceIndex(cwd: string): Promise<WorkspaceIndex> {
}
return { relativeDir, dirents: null };
}
}),
},
);
const candidateEntriesByDirectory = directoryEntries.map((directoryEntry) => {
@@ -315,14 +344,26 @@ async function getWorkspaceIndex(cwd: string): Promise<WorkspaceIndex> {
return cached;
}
const next = await buildWorkspaceIndex(cwd);
workspaceIndexCache.set(cwd, next);
while (workspaceIndexCache.size > WORKSPACE_CACHE_MAX_KEYS) {
const oldestKey = workspaceIndexCache.keys().next().value;
if (!oldestKey) break;
workspaceIndexCache.delete(oldestKey);
const inFlight = inFlightWorkspaceIndexBuilds.get(cwd);
if (inFlight) {
return inFlight;
}
return next;
const nextPromise = buildWorkspaceIndex(cwd)
.then((next) => {
workspaceIndexCache.set(cwd, next);
while (workspaceIndexCache.size > WORKSPACE_CACHE_MAX_KEYS) {
const oldestKey = workspaceIndexCache.keys().next().value;
if (!oldestKey) break;
workspaceIndexCache.delete(oldestKey);
}
return next;
})
.finally(() => {
inFlightWorkspaceIndexBuilds.delete(cwd);
});
inFlightWorkspaceIndexBuilds.set(cwd, nextPromise);
return nextPromise;
}
export async function searchWorkspaceEntries(
+12 -10
View File
@@ -329,6 +329,7 @@ export default function ChatView() {
gitCreateWorktreeMutationOptions({ api, queryClient }),
);
const [prompt, setPrompt] = useState("");
const promptRef = useRef(prompt);
const [composerImages, setComposerImages] = useState<ComposerImageAttachment[]>([]);
const [isDragOverComposer, setIsDragOverComposer] = useState(false);
const [expandedImage, setExpandedImage] = useState<ExpandedImagePreview | null>(null);
@@ -1126,6 +1127,7 @@ export default function ChatView() {
...(messageAttachments.length > 0 ? { attachments: messageAttachments } : {}),
});
const previousMessages = activeThread.messages;
promptRef.current = "";
setPrompt("");
setComposerImages([]);
setComposerCursor(0);
@@ -1238,16 +1240,15 @@ export default function ChatView() {
const applyPromptReplacement = useCallback(
(rangeStart: number, rangeEnd: number, replacement: string) => {
setPrompt((existing) => {
const next = replaceTextRange(existing, rangeStart, rangeEnd, replacement);
window.requestAnimationFrame(() => {
const textarea = textareaRef.current;
if (!textarea) return;
textarea.focus();
textarea.setSelectionRange(next.cursor, next.cursor);
setComposerCursor(next.cursor);
});
return next.text;
const next = replaceTextRange(promptRef.current, rangeStart, rangeEnd, replacement);
promptRef.current = next.text;
setPrompt(next.text);
window.requestAnimationFrame(() => {
const textarea = textareaRef.current;
if (!textarea) return;
textarea.focus();
textarea.setSelectionRange(next.cursor, next.cursor);
setComposerCursor(next.cursor);
});
},
[],
@@ -1284,6 +1285,7 @@ export default function ChatView() {
workspaceEntriesQuery.isFetching);
const onPromptChange = useCallback((nextPrompt: string, nextCursor: number) => {
promptRef.current = nextPrompt;
setPrompt(nextPrompt);
setComposerCursor(nextCursor);
}, []);