Refactor: Improve file tool context formatting and diff error guidance#2278
Refactor: Improve file tool context formatting and diff error guidance#2278
Conversation
Modifies how the result of the `read_file` tool is presented
in the conversation history sent to the AI model.
Previously, the format was:
[read_file for 'path/to/file'] Result:
{content_or_error}
This commit changes the format to use XML tags for better
structure and potentially easier parsing by the model:
<file>
<path>path/to/file</path>
<content>
{content_or_error}
</content>
</file>
This change only affects the `read_file` tool result formatting
within the user context message constructed in `src/core/Cline.ts`.
Other tool result formats remain unchanged.
…strategy Refines the error message returned when no sufficiently similar match is found during the multi-search-replace operation. The message now includes a clearer instruction to use the read_file tool for obtaining the latest file content before attempting to apply the diff again.
|
|
I think we could just make this change by adjusting what we pass into pushToolResult in readFileTool.ts |
|
Ok get it done! :) :P |
Modifies the `readFileTool` function to format the output as XML, enhancing the structure of the returned file content. This change aligns with previous updates to ensure consistent result formatting across tools.
This update refines the handling of tool responses in the `Cline` class by removing the XML formatting for `read_file` results and consolidating the logic for pushing results to the user message content. The changes ensure that all tool results are processed uniformly, improving code clarity and maintainability.
This commit modifies the assertions in the `read_file` tool tests to check for the expected XML format in the results. The changes ensure that the output structure aligns with recent updates to the tool's response formatting, enhancing test accuracy and reliability.
…able This commit updates the `read_file` tool tests to utilize a predefined variable for the expected XML output, improving readability and maintainability of the test code. The changes ensure consistency in the expected results across multiple test cases.
|
IMHO, this was merged prematurely. I have reiterated the most important items (from the review above) that should be addressed before merging: 1. program tail output is incorrect because of unnecessary XML indentation:
2. Errors are shown as file content which could confuse the model We do not want the model to think that the file contains the error text, but the structured markup does imply that as it is currently implemented.
|
|
Yeah that's good feedback. I like your suggestions. |
|
I am going to make a pr for this stay tuned ... |
* fix: addLineNumbers handling of empty content Empty files should not have line numbers, but non-empty files with empty content at a specific line offset should. - If content is empty, return empty string for empty files - If content is empty but startLine > 1, return line number for empty content at that offset This ensures that the model does not think the file contains a single empty line. Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> * refactor: improve readFileTool XML output format - Remove unnecessary XML indentation that could confuse the model - Separate file content from notices and errors using dedicated tags - Add line range information to content tags - Handle empty files properly with self-closing tags - Add comprehensive test coverage Fixes #2278 Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> * fix: always show line numbers in read_file XML output - Always display line numbers in non-range reads - Improve XML formatting with consistent newlines for better readability Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> * test: update tests to match new XML format with line numbers - Update test expectations to match the new XML format with newlines - Update tests to expect line numbers attribute in content tags - Modify test assertions to check for the correct line range values Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> * fix: consistent blank line handling in addLineNumbers - Add newline to all output - Handle trailing newlines and empty lines consistently - Add test cases for blank lines: - Multiple blank lines within content - Multiple trailing blank lines - Only blank lines with offset - Trailing newlines Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> * test: use actual addLineNumbers in read-file-xml tests - Modified extract-text mock to preserve actual addLineNumbers implementation - Removed mock implementation of addLineNumbers - Updated test data to account for trailing newline - Removed unnecessary mock verification Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> * test: ensure actual addLineNumbers function is called in tests - Replace direct mocking of addLineNumbers with spy on actual implementation - Add verification to ensure the real function is called when appropriate - Add skipAddLineNumbersCheck option for cases where function should not be called - Update test cases to use appropriate verification options - Fix numberedFileContent to include trailing newline for consistency Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> * fix: modify readLines to process data directly instead of line by line - Direct data processing provides more accurate results by preserving exact content with carriage returns - Improved performance through minimal buffering and efficient string operations - Use string indexes to find newlines while maintaining their original format - Handle all edge cases correctly with preserved line endings - Add tests for various edge cases including empty files, single lines, and different line endings Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> * test: remove unused mockInputContent variable Remove unused variable declaration to appease ellipsis-dev linter requirements. Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> --------- Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org> Co-authored-by: Eric Wheeler <roo-code@z.ewheeler.org>
This PR includes two related improvements to tool interactions and context formatting:
Refactor
read_fileTool Result Formatting (src/core/Cline.ts):read_filetool are formatted when added to the AI model's conversation history (user context).[read_file for 'path/to/file'] Result:\n{content}read_fileresults; other tool formats are unchanged.Improve
apply_diffError Message Clarity (src/core/diff/strategies/multi-search-replace.ts- Commit73da4d03):multi-search-replacediff strategy when no sufficiently similar match is found.read_filetool to get the latest file content before retrying theapply_difftool, explicitly mentioning both tools.Reasoning and Benefits:
read_fileresults provides a clearer, more structured representation of file path and content within the AI's context. This may improve the model's ability to parse and utilize file information reliably.read_fileandapply_diff, it helps steer the AI towards the correct recovery action (refreshing its knowledge of the file before trying the diff again).Important
Refactor
read_filetool to use XML formatting and improveapply_differror guidance for better tool interaction.read_filetool result formatting inCline.tsto XML format for structured representation.apply_differror message inmulti-search-replace.tsto guide usingread_filebefore retrying.read-file-maxReadFileLine.test.tsto expect XML formatted results.readFileTool.tsto push XML formatted results.This description was created by
for 8cd6c20. It will automatically update as commits are pushed.