diff --git a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/architect-mode-prompt.snap b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/architect-mode-prompt.snap index aa928fd50cf3..7eb6e3abf114 100644 --- a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/architect-mode-prompt.snap +++ b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/architect-mode-prompt.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/ask-mode-prompt.snap b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/ask-mode-prompt.snap index 07dc1ff9de14..7dd4d84c3930 100644 --- a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/ask-mode-prompt.snap +++ b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/ask-mode-prompt.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/mcp-server-creation-disabled.snap b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/mcp-server-creation-disabled.snap index 300c7a3565ea..accd834c7128 100644 --- a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/mcp-server-creation-disabled.snap +++ b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/mcp-server-creation-disabled.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/mcp-server-creation-enabled.snap b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/mcp-server-creation-enabled.snap index ec5304a2b2ad..1d6c0dea2e18 100644 --- a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/mcp-server-creation-enabled.snap +++ b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/mcp-server-creation-enabled.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/partial-reads-enabled.snap b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/partial-reads-enabled.snap index bf584f2691cf..65c6b4db1a99 100644 --- a/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/partial-reads-enabled.snap +++ b/src/core/prompts/__tests__/__snapshots__/add-custom-instructions/partial-reads-enabled.snap @@ -27,15 +27,16 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use line ranges to efficiently read specific portions of large files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use line ranges to efficiently read specific portions of large files. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. -By specifying line ranges, you can efficiently read specific portions of large files without loading the entire file into memory. +By specifying line ranges, you can efficiently read specific portions of large files without loading the entire file into memory. Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - line_range: (optional) One or more line range elements in format "start-end" (1-based, inclusive) + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -83,14 +84,56 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes -- You MUST use line ranges to read specific portions of large files, rather than reading entire files when not needed -- You MUST combine adjacent line ranges (<10 lines apart) -- You MUST use multiple ranges for content separated by >10 lines -- You MUST include sufficient line context for planned modifications while keeping ranges minimal +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + +5. Combining pattern and line_range (search within specific range): + + + + src/utils.ts + 100-500 + export const + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context), then use line_range to read full context around matches +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements +- Pattern + range: 100-500 and TODO|FIXME + +**Line Range Features:** +1. **Large files (>300 lines):** System will auto-limit to first 100 lines if you don't specify line_range, with an educational notice showing you how to use line_range for specific sections +2. **Preview unknown files:** Read first without line_range to see file size and structure, then use line_range for specific sections if needed +3. **Multiple sections:** Use multiple tags in ONE request to read non-adjacent sections efficiently + +✅ Line Range Examples: +- Reading large file preview: 1-100 +- Reading specific function: 450-520 +- Reading multiple sections: 1-50 and 200-250 + +📚 How it works: +- Files ≤300 lines: Full content returned +- Files >300 lines without line_range: First 100 lines + notice with usage examples +- Files >300 lines with line_range: Exact ranges you specified + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/system-prompt/consistent-system-prompt.snap b/src/core/prompts/__tests__/__snapshots__/system-prompt/consistent-system-prompt.snap index aa928fd50cf3..7eb6e3abf114 100644 --- a/src/core/prompts/__tests__/__snapshots__/system-prompt/consistent-system-prompt.snap +++ b/src/core/prompts/__tests__/__snapshots__/system-prompt/consistent-system-prompt.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-computer-use-support.snap b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-computer-use-support.snap index 641ec16082ef..b65da3cdbbb8 100644 --- a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-computer-use-support.snap +++ b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-computer-use-support.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-false.snap b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-false.snap index aa928fd50cf3..7eb6e3abf114 100644 --- a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-false.snap +++ b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-false.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-true.snap b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-true.snap index 154e1f08ae8e..bf251bc082bf 100644 --- a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-true.snap +++ b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-true.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-undefined.snap b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-undefined.snap index aa928fd50cf3..7eb6e3abf114 100644 --- a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-undefined.snap +++ b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-diff-enabled-undefined.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-different-viewport-size.snap b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-different-viewport-size.snap index d7feefa7e601..ad62703f7b04 100644 --- a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-different-viewport-size.snap +++ b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-different-viewport-size.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-mcp-hub-provided.snap b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-mcp-hub-provided.snap index ec5304a2b2ad..1d6c0dea2e18 100644 --- a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-mcp-hub-provided.snap +++ b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-mcp-hub-provided.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-undefined-mcp-hub.snap b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-undefined-mcp-hub.snap index aa928fd50cf3..7eb6e3abf114 100644 --- a/src/core/prompts/__tests__/__snapshots__/system-prompt/with-undefined-mcp-hub.snap +++ b/src/core/prompts/__tests__/__snapshots__/system-prompt/with-undefined-mcp-hub.snap @@ -27,15 +27,15 @@ Always use the actual tool name as the XML tag name for proper parsing and execu # Tools ## read_file -Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of one or more files. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code. Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. **IMPORTANT: You can read a maximum of 5 files in a single request.** If you need to read more files, use multiple sequential read_file requests. - + Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory /test/path) - + - pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -82,10 +82,30 @@ Examples: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- You MUST read all related files and implementations together in a single operation (up to 5 files at once) -- You MUST obtain all necessary context before proceeding with changes +4. Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + + + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context) +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements + + +- You MUST read all related files together in a single operation (up to 5 files at once) +- You MUST obtain all necessary context before proceeding with changes - When you need to read more than 5 files, prioritize the most critical files first, then use subsequent read_file requests for additional files ## fetch_instructions diff --git a/src/core/prompts/tools/read-file.ts b/src/core/prompts/tools/read-file.ts index 86f4dc8c640f..0ca4bba15fe3 100644 --- a/src/core/prompts/tools/read-file.ts +++ b/src/core/prompts/tools/read-file.ts @@ -5,15 +5,20 @@ export function getReadFileDescription(args: ToolArgs): string { const isMultipleReadsEnabled = maxConcurrentReads > 1 return `## read_file -Description: Request to read the contents of ${isMultipleReadsEnabled ? "one or more files" : "a file"}. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code.${args.partialReadsEnabled ? " Use line ranges to efficiently read specific portions of large files." : ""} Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. +Description: Request to read the contents of ${isMultipleReadsEnabled ? "one or more files" : "a file"}. The tool outputs line-numbered content (e.g. "1 | const x = 1") for easy reference when creating diffs or discussing code.${args.partialReadsEnabled ? " Use line ranges to efficiently read specific portions of large files." : ""} Use pattern to search for specific content in files. Supports text extraction from PDF and DOCX files, but may not handle other binary files properly. ${isMultipleReadsEnabled ? `**IMPORTANT: You can read a maximum of ${maxConcurrentReads} files in a single request.** If you need to read more files, use multiple sequential read_file requests.` : "**IMPORTANT: Multiple file reads are currently disabled. You can only read one file at a time.**"} -${args.partialReadsEnabled ? `By specifying line ranges, you can efficiently read specific portions of large files without loading the entire file into memory.` : ""} +${args.partialReadsEnabled ? `By specifying line ranges, you can efficiently read specific portions of large files without loading the entire file into memory.` : ""} Use pattern to search for specific content and get lightweight results with match locations - pattern returns line numbers + context, allowing you to then use line_range to read full context around matches. Parameters: - args: Contains one or more file elements, where each file contains: - path: (required) File path (relative to workspace directory ${args.cwd}) - ${args.partialReadsEnabled ? `- line_range: (optional) One or more line range elements in format "start-end" (1-based, inclusive)` : ""} + ${ + args.partialReadsEnabled + ? `- line_range: (optional) One or more line range elements in format "start-end" (1-based, inclusive) + ` + : "" + }- pattern: (optional) Regex pattern to search within the file (lightweight search mode) Usage: @@ -69,17 +74,63 @@ ${isMultipleReadsEnabled ? "3. " : "2. "}Reading an entire file: -IMPORTANT: You MUST use this Efficient Reading Strategy: -- ${isMultipleReadsEnabled ? `You MUST read all related files and implementations together in a single operation (up to ${maxConcurrentReads} files at once)` : "You MUST read files one at a time, as multiple file reads are currently disabled"} -- You MUST obtain all necessary context before proceeding with changes +${isMultipleReadsEnabled ? "4. " : "3. "}Searching for a pattern (lightweight mode): + + + + src/app.ts + async function|TODO + + + +${ + args.partialReadsEnabled + ? ` +${isMultipleReadsEnabled ? "5. " : "4. "}Combining pattern and line_range (search within specific range): + + + + src/utils.ts + 100-500 + export const + + +` + : "" +} + +CRITICAL RULES FOR READING FILES (YOU MUST FOLLOW): + +**Pattern Search (Always Available):** +- **Find specific content:** Use pattern to search within a single file, or use search_files to search across multiple files +- **Pattern search workflow:** Use pattern to find matches (returns line numbers + context)${args.partialReadsEnabled ? `, then use line_range to read full context around matches` : ""} +- **Pattern output:** Returns match locations (line numbers) + 2 lines of context per match (max 20 matches) + +✅ Pattern Examples: +- Searching for pattern: async function|class.*implements +${args.partialReadsEnabled ? `- Pattern + range: 100-500 and TODO|FIXME` : ""} ${ args.partialReadsEnabled - ? `- You MUST use line ranges to read specific portions of large files, rather than reading entire files when not needed -- You MUST combine adjacent line ranges (<10 lines apart) -- You MUST use multiple ranges for content separated by >10 lines -- You MUST include sufficient line context for planned modifications while keeping ranges minimal + ? ` +**Line Range Features:** +1. **Large files (>300 lines):** System will auto-limit to first 100 lines if you don't specify line_range, with an educational notice showing you how to use line_range for specific sections +2. **Preview unknown files:** Read first without line_range to see file size and structure, then use line_range for specific sections if needed +3. **Multiple sections:** Use multiple tags in ONE request to read non-adjacent sections efficiently + +✅ Line Range Examples: +- Reading large file preview: 1-100 +- Reading specific function: 450-520 +- Reading multiple sections: 1-50 and 200-250 + +📚 How it works: +- Files ≤300 lines: Full content returned +- Files >300 lines without line_range: First 100 lines + notice with usage examples +- Files >300 lines with line_range: Exact ranges you specified + ` : "" } +- ${isMultipleReadsEnabled ? `You MUST read all related files together in a single operation (up to ${maxConcurrentReads} files at once)` : "You MUST read files one at a time, as multiple file reads are currently disabled"} +- You MUST obtain all necessary context before proceeding with changes ${isMultipleReadsEnabled ? `- When you need to read more than ${maxConcurrentReads} files, prioritize the most critical files first, then use subsequent read_file requests for additional files` : ""}` } diff --git a/src/core/tools/readFileTool.ts b/src/core/tools/readFileTool.ts index 01427f4d9dc7..eb2d9ef8ebcc 100644 --- a/src/core/tools/readFileTool.ts +++ b/src/core/tools/readFileTool.ts @@ -22,6 +22,21 @@ import { processImageFile, ImageMemoryTracker, } from "./helpers/imageHelpers" +import { regexSearchFiles } from "../../services/ripgrep" + +/** + * Escape XML special characters to prevent XML injection and parsing errors + * @param unsafe - String that may contain XML special characters + * @returns Escaped string safe for XML embedding + */ +function escapeXml(unsafe: string): string { + return unsafe + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'") +} export function getReadFileToolDescription(blockName: string, blockParams: any): string { // Handle both single path and multiple files via args @@ -54,6 +69,117 @@ export function getReadFileToolDescription(blockName: string, blockParams: any): return `[${blockName} with missing path/args]` } } + +/** + * Pattern 搜索結果介面 + */ +interface PatternMatch { + startLine: number + endLine: number + matchLine: number // 實際匹配的行號 + content: string // 帶行號的上下文(前後各 2 行) +} + +/** + * 在單個檔案中搜索 pattern + * @param filePath - 完整檔案路徑 + * @param cwd - 工作目錄 + * @param pattern - Regex pattern + * @param rooIgnoreController - Ignore 控制器 + * @returns 匹配結果數組 + */ +async function searchPatternInFile( + filePath: string, + cwd: string, + pattern: string, + rooIgnoreController?: any, +): Promise { + try { + // 使用 ripgrep 搜索單個檔案 + const searchResults = await regexSearchFiles(cwd, filePath, pattern, undefined, rooIgnoreController) + + // 解析 ripgrep 輸出 + // 輸出格式範例: + // # src/app.ts + // 45 | export async function fetchData() { + // 46 | const response = await fetch(url) + // 47 | return response.json() + // ---- + + const matches: PatternMatch[] = [] + const lines = searchResults.split("\n") + + let currentMatchLines: { line: number; text: string }[] = [] + + for (let i = 0; i < lines.length; i++) { + const line = lines[i] + + // 跳過檔案名行 + if (line.startsWith("#")) { + continue + } + + // 分隔符表示一個匹配塊結束 + if (line.trim() === "----") { + if (currentMatchLines.length > 0) { + // 提取行號範圍 + const lineNumbers = currentMatchLines.map((l) => l.line) + const startLine = Math.min(...lineNumbers) + const endLine = Math.max(...lineNumbers) + + // 組合內容 + const content = currentMatchLines + .map((l) => `${String(l.line).padStart(3, " ")} | ${l.text}`) + .join("\n") + + // 假設匹配行在中間位置 + const matchLine = currentMatchLines[Math.floor(currentMatchLines.length / 2)].line + + matches.push({ + startLine, + endLine, + matchLine, + content, + }) + + currentMatchLines = [] + } + continue + } + + // 解析行號和內容 + // 格式: " 45 | export async function fetchData() {" + const match = line.match(/^\s*(\d+)\s+\|\s+(.*)$/) + if (match) { + const lineNumber = parseInt(match[1], 10) + const lineText = match[2] + currentMatchLines.push({ line: lineNumber, text: lineText }) + } + } + + // 處理最後一個匹配塊(如果沒有結尾的分隔符) + if (currentMatchLines.length > 0) { + const lineNumbers = currentMatchLines.map((l) => l.line) + const startLine = Math.min(...lineNumbers) + const endLine = Math.max(...lineNumbers) + const content = currentMatchLines.map((l) => `${String(l.line).padStart(3, " ")} | ${l.text}`).join("\n") + const matchLine = currentMatchLines[Math.floor(currentMatchLines.length / 2)].line + + matches.push({ + startLine, + endLine, + matchLine, + content, + }) + } + + return matches + } catch (error) { + console.error(`[searchPatternInFile] Error searching pattern in ${filePath}:`, error) + return [] + } +} + // Types interface LineRange { start: number @@ -63,6 +189,7 @@ interface LineRange { interface FileEntry { path?: string lineRanges?: LineRange[] + pattern?: string // 新增:用於在檔案中搜索 regex pattern } // New interface to track file processing state @@ -73,6 +200,7 @@ interface FileResult { error?: string notice?: string lineRanges?: LineRange[] + pattern?: string // Pattern for searching within the file xmlContent?: string // Final XML content for this file imageDataUrl?: string // Image data URL for image files feedbackText?: string // User feedback text from approval/denial @@ -137,6 +265,7 @@ export async function readFileTool( const fileEntry: FileEntry = { path: file.path, lineRanges: [], + pattern: file.pattern, // 解析 pattern 參數 } if (file.line_range) { @@ -196,6 +325,7 @@ export async function readFileTool( path: entry.path || "", status: "pending", lineRanges: entry.lineRanges, + pattern: entry.pattern, })) // Function to update file result status @@ -528,7 +658,81 @@ export async function readFileTool( } } - // Handle range reads (bypass maxReadFileLine) + // Handle pattern + line_range combination + if (fileResult.pattern && fileResult.lineRanges && fileResult.lineRanges.length > 0) { + // Search for pattern within specified line ranges + const matches = await searchPatternInFile( + fullPath, + cline.cwd, + fileResult.pattern, + cline.rooIgnoreController, + ) + + // Filter matches to only include those within specified line ranges + const filteredMatches = matches.filter((match) => { + return fileResult.lineRanges!.some( + (range) => match.matchLine >= range.start && match.matchLine <= range.end, + ) + }) + + // Track file read + await cline.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource) + + if (filteredMatches.length === 0) { + const rangesStr = fileResult.lineRanges.map((r) => `${r.start}-${r.end}`).join(", ") + const xmlInfo = ` +${totalLines} +${escapeXml(fileResult.pattern)} +${rangesStr} +0 + +No matches found for pattern "${escapeXml(fileResult.pattern)}" within line ranges: ${rangesStr}` + + updateFileResult(relPath, { + xmlContent: `${relPath}\n${xmlInfo}\n`, + }) + continue + } + + // Limit results + const limitedMatches = filteredMatches.slice(0, 20) + const hasMore = filteredMatches.length > 20 + const rangesStr = fileResult.lineRanges.map((r) => `${r.start}-${r.end}`).join(", ") + + let xmlInfo = ` +${totalLines} +${escapeXml(fileResult.pattern)} +${rangesStr} +${filteredMatches.length} +${hasMore ? `20` : ""} +\n` + + // Add match results + limitedMatches.forEach((match) => { + const lineAttr = ` lines="${match.startLine}-${match.endLine}"` + xmlInfo += `\n${match.content}\n\n` + }) + + // Add notice + const firstMatch = limitedMatches[0] + const exampleStart = Math.max(1, firstMatch.matchLine - 10) + const exampleEnd = Math.min(totalLines, firstMatch.matchLine + 10) + + let notice = `Found ${filteredMatches.length} match(es) within line ranges: ${rangesStr}${hasMore ? `, showing first 20` : ""}.` + notice += `\n\nTo read full context around a match, use:\n` + notice += `\n\n \n ${relPath}\n` + notice += ` ${exampleStart}-${exampleEnd}\n` + notice += ` \n\n` + + xmlInfo += `${notice}` + + updateFileResult(relPath, { + xmlContent: `${relPath}\n${xmlInfo}\n`, + }) + continue + } + + // Handle range reads only (no pattern search) if (fileResult.lineRanges && fileResult.lineRanges.length > 0) { const rangeResults: string[] = [] for (const range of fileResult.lineRanges) { @@ -545,6 +749,118 @@ export async function readFileTool( continue } + // Handle pattern search only (entire file) + if (fileResult.pattern) { + const matches = await searchPatternInFile( + fullPath, + cline.cwd, + fileResult.pattern, + cline.rooIgnoreController, + ) + + // Track file read + await cline.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource) + + if (matches.length === 0) { + const xmlInfo = ` +${totalLines} +${escapeXml(fileResult.pattern)} +0 + +No matches found for pattern "${escapeXml(fileResult.pattern)}"` + + updateFileResult(relPath, { + xmlContent: `${relPath}\n${xmlInfo}\n`, + }) + continue + } + + // 限制返回的匹配數量(最多 20 個) + const limitedMatches = matches.slice(0, 20) + const hasMore = matches.length > 20 + + let xmlInfo = ` +${totalLines} +${escapeXml(fileResult.pattern)} +${matches.length} +${hasMore ? `20` : ""} +\n` + + // 添加每個匹配的內容 + limitedMatches.forEach((match, idx) => { + const lineAttr = ` lines="${match.startLine}-${match.endLine}"` + xmlInfo += `\n${match.content}\n\n` + }) + + // 添加提示信息 + const firstMatch = limitedMatches[0] + const exampleStart = Math.max(1, firstMatch.matchLine - 10) + const exampleEnd = Math.min(totalLines, firstMatch.matchLine + 10) + + let notice = `Found ${matches.length} match(es)${hasMore ? `, showing first 20` : ""}.` + notice += `\n\nTo read full context around a match, use line_range:\n` + notice += `\n\n \n ${relPath}\n` + notice += ` ${exampleStart}-${exampleEnd} \n` + notice += ` \n\n` + + xmlInfo += `${notice}` + + updateFileResult(relPath, { + xmlContent: `${relPath}\n${xmlInfo}\n`, + }) + continue + } + + // Auto-limit large files without line_range to protect context (Option G) + // This helps weak models learn to use line_range by showing them the correct syntax + if (totalLines > 300 && (!fileResult.lineRanges || fileResult.lineRanges.length === 0)) { + const autoLimitLines = 100 + const content = addLineNumbers(await readLines(fullPath, autoLimitLines - 1, 0), 1) + const lineRangeAttr = ` lines="1-${autoLimitLines}"` + let xmlInfo = `\n${totalLines}\n1-${autoLimitLines}\n\n` + xmlInfo += `\n${content}\n` + + // Try to get code definitions to help model locate specific sections + try { + const defResult = await parseSourceCodeDefinitionsForFile(fullPath, cline.rooIgnoreController) + if (defResult) { + xmlInfo += `${defResult}\n` + } + } catch (error) { + // Silently ignore definition parsing errors for non-supported languages + if (error instanceof Error && !error.message.startsWith("Unsupported language:")) { + console.warn(`[read_file] Warning parsing definitions: ${error.message}`) + } + } + + // Educational notice to teach weak models how to use line_range + const educationalNotice = `⚠️ This file has ${totalLines} lines (exceeds 300-line threshold). +Showing first ${autoLimitLines} lines only to preserve context. + +To read specific sections, use line_range parameter: + + + + ${relPath} + 1-100 + 450-550 + + + + +You can specify multiple elements to read non-adjacent sections efficiently.` + + xmlInfo += `${educationalNotice}\n` + + // Track file read + await cline.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource) + + updateFileResult(relPath, { + xmlContent: `${relPath}\n${xmlInfo}`, + }) + continue + } + // Handle definitions-only mode if (maxReadFileLine === 0) { try {