-
Notifications
You must be signed in to change notification settings - Fork 22.8k
fix(grep): stream ripgrep output to prevent memory exhaustion #5432
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import DESCRIPTION from "./grep.txt" | |
| import { Instance } from "../project/instance" | ||
|
|
||
| const MAX_LINE_LENGTH = 2000 | ||
| const MATCH_LIMIT = 100 | ||
|
|
||
| export const GrepTool = Tool.define("grep", { | ||
| description: DESCRIPTION, | ||
|
|
@@ -33,51 +34,95 @@ export const GrepTool = Tool.define("grep", { | |
| stderr: "pipe", | ||
| }) | ||
|
|
||
| const output = await new Response(proc.stdout).text() | ||
| const reader = proc.stdout.getReader() | ||
| const decoder = new TextDecoder() | ||
| let buffer = "" | ||
| const matches: Array<{ | ||
| path: string | ||
| modTime: number | ||
| lineNum: number | ||
| lineText: string | ||
| }> = [] | ||
| let hitCollectLimit = false | ||
|
|
||
| try { | ||
| while (true) { | ||
| const { done, value } = await reader.read() | ||
| if (done) break | ||
|
|
||
| buffer += decoder.decode(value, { stream: true }) | ||
| const lines = buffer.split("\n") | ||
| buffer = lines.pop() || "" | ||
|
|
||
| for (const line of lines) { | ||
| if (!line) continue | ||
| if (matches.length >= MATCH_LIMIT) { | ||
| hitCollectLimit = true | ||
| break | ||
| } | ||
|
|
||
| const [filePath, lineNumStr, ...lineTextParts] = line.split("|") | ||
| if (!filePath || !lineNumStr || lineTextParts.length === 0) continue | ||
|
|
||
| const lineNum = parseInt(lineNumStr, 10) | ||
| const lineText = lineTextParts.join("|") | ||
|
|
||
| const file = Bun.file(filePath) | ||
| const stats = await file.stat().catch(() => null) | ||
| if (!stats) continue | ||
|
|
||
| matches.push({ | ||
| path: filePath, | ||
| modTime: stats.mtime.getTime(), | ||
| lineNum, | ||
| lineText, | ||
| }) | ||
| } | ||
|
Comment on lines
+56
to
+71
|
||
|
|
||
| if (hitCollectLimit) break | ||
| } | ||
|
|
||
| if (!hitCollectLimit && buffer) { | ||
| const [filePath, lineNumStr, ...lineTextParts] = buffer.split("|") | ||
| if (filePath && lineNumStr && lineTextParts.length > 0) { | ||
| const lineNum = parseInt(lineNumStr, 10) | ||
| const lineText = lineTextParts.join("|") | ||
| const file = Bun.file(filePath) | ||
| const stats = await file.stat().catch(() => null) | ||
| if (stats) { | ||
| matches.push({ | ||
| path: filePath, | ||
| modTime: stats.mtime.getTime(), | ||
| lineNum, | ||
| lineText, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
||
| } finally { | ||
| if (hitCollectLimit) proc.kill() | ||
| reader.releaseLock() | ||
| } | ||
|
|
||
| const errorOutput = await new Response(proc.stderr).text() | ||
|
||
| const exitCode = await proc.exited | ||
|
|
||
| if (exitCode === 1) { | ||
| if (exitCode === 1 && matches.length === 0) { | ||
|
||
| return { | ||
| title: params.pattern, | ||
| metadata: { matches: 0, truncated: false }, | ||
| output: "No files found", | ||
| } | ||
| } | ||
|
|
||
| if (exitCode !== 0) { | ||
| if (exitCode !== 0 && exitCode !== 1 && !hitCollectLimit) { | ||
| throw new Error(`ripgrep failed: ${errorOutput}`) | ||
| } | ||
|
|
||
| const lines = output.trim().split("\n") | ||
| const matches = [] | ||
|
|
||
| for (const line of lines) { | ||
| if (!line) continue | ||
|
|
||
| const [filePath, lineNumStr, ...lineTextParts] = line.split("|") | ||
| if (!filePath || !lineNumStr || lineTextParts.length === 0) continue | ||
|
|
||
| const lineNum = parseInt(lineNumStr, 10) | ||
| const lineText = lineTextParts.join("|") | ||
|
|
||
| const file = Bun.file(filePath) | ||
| const stats = await file.stat().catch(() => null) | ||
| if (!stats) continue | ||
|
|
||
| matches.push({ | ||
| path: filePath, | ||
| modTime: stats.mtime.getTime(), | ||
| lineNum, | ||
| lineText, | ||
| }) | ||
| } | ||
|
|
||
| matches.sort((a, b) => b.modTime - a.modTime) | ||
|
|
||
| const limit = 100 | ||
| const truncated = matches.length > limit | ||
| const finalMatches = truncated ? matches.slice(0, limit) : matches | ||
| const truncated = matches.length > MATCH_LIMIT | ||
| const finalMatches = truncated ? matches.slice(0, MATCH_LIMIT) : matches | ||
|
||
|
|
||
| if (finalMatches.length === 0) { | ||
| return { | ||
|
|
@@ -103,7 +148,7 @@ export const GrepTool = Tool.define("grep", { | |
| outputLines.push(` Line ${match.lineNum}: ${truncatedLineText}`) | ||
| } | ||
|
|
||
| if (truncated) { | ||
| if (truncated || hitCollectLimit) { | ||
| outputLines.push("") | ||
| outputLines.push("(Results are truncated. Consider using a more specific path or pattern.)") | ||
| } | ||
|
|
@@ -112,7 +157,7 @@ export const GrepTool = Tool.define("grep", { | |
| title: params.pattern, | ||
| metadata: { | ||
| matches: finalMatches.length, | ||
| truncated, | ||
| truncated: truncated || hitCollectLimit, | ||
| }, | ||
| output: outputLines.join("\n"), | ||
| } | ||
|
|
||
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.
Calling
file.stat()for every single match result can be a performance bottleneck, especially when processing many results. Each stat call is an I/O operation that waits for file system access. Since ripgrep already provides the file path, consider whether the modification time is truly necessary for sorting, or if there's a way to batch or optimize these stat calls.