-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: Add pattern search support to read_file tool #8739
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
base: main
Are you sure you want to change the base?
feat: Add pattern search support to read_file tool #8739
Conversation
Implemented lightweight pattern search functionality for the read_file tool, enabling models to efficiently search for specific content within files without reading entire files. This feature provides a "search then read" workflow to save context window space. Key changes: - Added pattern parameter to read_file tool - Returns match locations (line numbers) + 2 lines of context per match - Maximum 20 matches to prevent context overflow - Complements existing line_range functionality - Updated tool schema with pattern examples and usage guidelines Files modified: - src/core/tools/readFileTool.ts (~170 lines added) - New PatternMatch interface and searchPatternInFile() helper - Pattern search logic with XML output formatting - Integrated with ripgrep service - src/core/prompts/tools/read-file.ts (~50 lines updated) - Added pattern parameter documentation - New usage examples for pattern search - Updated critical rules for pattern workflow 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Pattern search functionality was fully implemented but hidden by default because its documentation was conditional on partialReadsEnabled flag. Since partialReadsEnabled defaults to false (maxReadFileLine: -1), models couldn't discover or use the pattern search feature. Changes: - Separated pattern documentation from partialReadsEnabled condition - Pattern parameter, examples, and rules now always visible - Line range documentation remains conditional as intended - No changes to actual implementation (already working) Impact: Models can now discover and use pattern search for efficient file content searching, as demonstrated in JIRA pattern search tests. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Code Review SummaryI've reviewed the pattern search implementation for the Issues to Resolve
|
src/core/tools/readFileTool.ts
Outdated
const lines = searchResults.split("\n") | ||
|
||
let currentMatchLines: { line: number; text: string }[] = [] | ||
let inMatchBlock = false |
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.
Unused variable 'inMatchBlock' is declared and updated but never used. Consider removing it.
let inMatchBlock = false |
src/core/tools/readFileTool.ts
Outdated
const lines = searchResults.split("\n") | ||
|
||
let currentMatchLines: { line: number; text: string }[] = [] | ||
let inMatchBlock = false |
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.
The variable inMatchBlock
is set but never used in searchPatternInFile
. Removing it would simplify the code.
let inMatchBlock = false |
src/core/tools/readFileTool.ts
Outdated
if (matches.length === 0) { | ||
const xmlInfo = `<metadata> | ||
<total_lines>${totalLines}</total_lines> | ||
<pattern>${fileResult.pattern}</pattern> |
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.
User input (fileResult.pattern
) is embedded directly into XML. For security and well-formed XML, please sanitize or escape XML special characters.
} | ||
|
||
// Handle pattern search (lightweight search mode) | ||
if (fileResult.pattern) { |
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.
The implementation doesn't support combining pattern
with line_range
as documented in example 5 of the tool description. When both parameters are provided, line_range
is processed first (line 651) and returns early with continue
(line 664), preventing this pattern logic from executing. This means users cannot search for patterns within a specific line range as the documentation suggests. To fix this, the pattern search logic would need to either: (1) be executed before line_range handling and filter matches to the specified ranges, or (2) read only the specified line ranges first and then search within them.
…ML escaping, update snapshots This commit addresses all review feedback for PR RooCodeInc#8739: 1. **Fix pattern + line_range combination** (roomote bot critical issue) - Refactored logic to support searching patterns within specified line ranges - Added dedicated handling for combined pattern+line_range mode - Filters pattern matches to only show results within specified ranges - Previously line_range would return early, preventing pattern search 2. **Remove unused variable** (ellipsis-dev bot) - Removed unused `inMatchBlock` variable from searchPatternInFile() - Variable was declared and set but never read 3. **Add XML escaping** (ellipsis-dev bot security issue) - Added escapeXml() helper function to prevent XML injection - Escaped all pattern parameter values before embedding in XML output - Prevents parsing errors and potential injection attacks 4. **Update snapshot tests** (CI failures) - Updated 13 snapshot files to reflect new pattern search documentation - Tests now pass with updated system prompts including pattern feature Files changed: - src/core/tools/readFileTool.ts: All code fixes - src/core/prompts/__tests__/__snapshots__/*: Updated snapshots 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Summary
This PR adds regex pattern search functionality to the
read_file
tool, enabling efficient in-file content search without reading entire files. This "search then read" workflow significantly reduces token usage and improves LLM efficiency.Key Features
pattern
parameter for regex-based content search within filesline_range
functionalityImplementation Details
Files Modified:
src/core/tools/readFileTool.ts
(+231 lines)PatternMatch
interface for search resultssearchPatternInFile()
function with ripgrep integrationsrc/core/prompts/tools/read-file.ts
(+61 lines, -10 lines)Usage Examples
Simple pattern search:
Pattern + line_range combination:
Benefits
Test Plan
Related Commits
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]
Important
Adds regex pattern search to
read_file
tool, allowing efficient in-file searches with context and match location output.read_file
tool with a newpattern
parameter.line_range
functionality.readFileTool.ts
: ImplementssearchPatternInFile()
using ripgrep, addsPatternMatch
interface, and integrates pattern search logic.read-file.ts
: Updates documentation to include pattern search usage and examples.line_range
is specified.This description was created by
for 8c5f9c3. You can customize this summary. It will automatically update as commits are pushed.