Repository navigation
perf: batch remote filesystem walks #4539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Speed up file completion in remote environments without ripgrep. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { join, relative, sep } from 'node:path'; | ||
|
|
||
| import ignore from 'ignore'; | ||
|
|
||
| import type { HostDirEntry, HostWalkOptions, HostWalkResult } from './hostFileSystem'; | ||
| import { HostFsError, OsFsErrors } from './hostFsErrors'; | ||
|
|
||
| export const HOST_WALK_MAX_DEPTH = 64; | ||
| export const HOST_WALK_MAX_DIRECTORIES = 10_000; | ||
| export const HOST_WALK_MAX_ENTRIES = 100_000; | ||
| export const HOST_WALK_MAX_RESPONSE_BYTES = 8 * 1024 * 1024; | ||
| export const HOST_WALK_MAX_IGNORE_BYTES = 1024 * 1024; | ||
| export const HOST_WALK_DEFAULT_TIMEOUT_MS = 55_000; | ||
| export const HOST_WALK_MAX_TIMEOUT_MS = 60_000; | ||
| export const HOST_WALK_MAX_DIRECTORY_ENTRIES = 50_000; | ||
|
|
||
| export const EMPTY_HOST_WALK_RESULT: HostWalkResult = { | ||
| entries: [], | ||
| errors: [], | ||
| truncated: true, | ||
| responseBytes: 0, | ||
| }; | ||
|
|
||
| interface HostWalkerFs { | ||
| readdir(path: string): Promise<readonly HostDirEntry[]>; | ||
| } | ||
|
|
||
| interface HostWalkPath { | ||
| readonly separator: string; | ||
| join(...paths: readonly string[]): string; | ||
| relative(from: string, to: string): string; | ||
| } | ||
|
|
||
| interface HostWalkHooks { | ||
| readonly path?: HostWalkPath; | ||
| readonly serializeError?: (path: string, error: Error) => unknown; | ||
| } | ||
|
|
||
| export async function walkHostDirectory( | ||
| fs: HostWalkerFs, | ||
| root: string, | ||
| options: HostWalkOptions, | ||
| hooks: HostWalkHooks = {}, | ||
| readDirectory: (path: string) => Promise<readonly HostDirEntry[]> = fs.readdir.bind(fs), | ||
| ): Promise<HostWalkResult> { | ||
| validateLimit('maxDepth', options.maxDepth, 0, HOST_WALK_MAX_DEPTH); | ||
| validateLimit('maxDirectories', options.maxDirectories, 1, HOST_WALK_MAX_DIRECTORIES); | ||
| validateLimit('maxEntries', options.maxEntries, 1, HOST_WALK_MAX_ENTRIES); | ||
| validateLimit('maxResponseBytes', options.maxResponseBytes, 1, HOST_WALK_MAX_RESPONSE_BYTES); | ||
| if (options.timeoutMs !== undefined) { | ||
| validateLimit('timeoutMs', options.timeoutMs, 1, HOST_WALK_MAX_TIMEOUT_MS); | ||
| } | ||
| const matcher = options.ignore === undefined ? undefined : ignore().add(options.ignore); | ||
| const serializeError = hooks.serializeError ?? ((_path, error) => ({ | ||
| name: error.name, | ||
| message: error.message, | ||
| })); | ||
| const pathApi = hooks.path ?? { join, relative, separator: sep }; | ||
| const timeout = AbortSignal.timeout(options.timeoutMs ?? HOST_WALK_DEFAULT_TIMEOUT_MS); | ||
| const signal = options.signal === undefined | ||
| ? timeout | ||
| : AbortSignal.any([options.signal, timeout]); | ||
| const entries: HostDirEntry[] = []; | ||
| const errors: { path: string; error: Error }[] = []; | ||
| const pending = [{ path: root, depth: 0 }]; | ||
| let responseBytes = 64; | ||
| let truncated = false; | ||
| for (let index = 0; index < pending.length && !truncated; index += 1) { | ||
| if (signal?.aborted === true) { | ||
| truncated = true; | ||
| break; | ||
| } | ||
| const { path: directory, depth } = pending[index]!; | ||
| let children: readonly HostDirEntry[]; | ||
| try { | ||
| children = await readDirectory(directory); | ||
| if (children.length > HOST_WALK_MAX_DIRECTORY_ENTRIES) { | ||
| throw new HostFsError( | ||
| OsFsErrors.codes.OS_FS_DIRECTORY_TOO_LARGE, | ||
| `readdir ${directory} exceeds the ${HOST_WALK_MAX_DIRECTORY_ENTRIES}-entry limit`, | ||
| { details: { path: directory, op: 'readdir', limit: HOST_WALK_MAX_DIRECTORY_ENTRIES } }, | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| const errorBytes = Buffer.byteLength(JSON.stringify({ | ||
| path: directory, | ||
| error: serializeError(directory, asError(error)), | ||
| })) + 1; | ||
| if (responseBytes + errorBytes > options.maxResponseBytes) { | ||
| truncated = true; | ||
| break; | ||
| } | ||
| responseBytes += errorBytes; | ||
| errors.push({ path: directory, error: asError(error) }); | ||
| continue; | ||
| } | ||
| for (const child of children) { | ||
| if (signal?.aborted) { | ||
| truncated = true; | ||
| break; | ||
| } | ||
| const path = pathApi.join(directory, child.name); | ||
| const isDir = child.isDirectory && child.isSymbolicLink !== true; | ||
| const relPath = pathApi.relative(root, path).split(pathApi.separator).join('/'); | ||
| if (matcher?.ignores(isDir ? `${relPath}/` : relPath) === true) continue; | ||
| const entryBytes = Buffer.byteLength(JSON.stringify({ ...child, name: path })) + 1; | ||
| if (entries.length >= options.maxEntries || responseBytes + entryBytes > options.maxResponseBytes) { | ||
| truncated = true; | ||
| break; | ||
| } | ||
| responseBytes += entryBytes; | ||
| entries.push({ ...child, name: path }); | ||
| if (isDir && depth < options.maxDepth) { | ||
| if (pending.length >= options.maxDirectories) { | ||
| truncated = true; | ||
| break; | ||
| } | ||
| pending.push({ path, depth: depth + 1 }); | ||
| } | ||
| } | ||
| } | ||
| return { entries, errors, truncated, responseBytes }; | ||
| } | ||
|
|
||
| function validateLimit(name: string, value: number, min: number, max: number): void { | ||
| if (!Number.isInteger(value) || value < min || value > max) { | ||
| throw new RangeError(`${name} must be an integer in [${min}, ${max}]`); | ||
| } | ||
| } | ||
|
|
||
| function asError(error: unknown): Error { | ||
| return error instanceof Error ? error : new Error(String(error)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a nonempty search traverses more than 10,000 directories, 100,000 entries, about 8 MiB of inventory, or 55 seconds, this commit now returns partial results even for local and ACP workspaces, whereas the previous DFS had no corresponding global bounds. The changeset mentions only faster remote completion, so release notes omit the completeness users lose and the lack of an escape hatch; name that truncation explicitly.
AGENTS.md reference: AGENTS.md:L115-L117
Useful? React with 👍 / 👎.