diff --git a/openhands_aci/editor/editor.py b/openhands_aci/editor/editor.py index 97ffc61..91d4334 100644 --- a/openhands_aci/editor/editor.py +++ b/openhands_aci/editor/editor.py @@ -348,23 +348,21 @@ def view(self, path: Path, view_range: list[int] | None = None) -> CLIResult: f'Its first element `{start_line}` should be within the range of lines of the file: {[1, num_lines]}.', ) - if end_line > num_lines: - raise EditorToolParameterInvalidError( - 'view_range', - view_range, - f'Its second element `{end_line}` should be smaller than the number of lines in the file: `{num_lines}`.', - ) + # Normalize end_line and provide a warning if it exceeds file length + warning_message: str | None = None + if end_line == -1: + end_line = num_lines + elif end_line > num_lines: + warning_message = f"We only show up to {num_lines} since there're only {num_lines} lines in this file." + end_line = num_lines - if end_line != -1 and end_line < start_line: + if end_line < start_line: raise EditorToolParameterInvalidError( 'view_range', view_range, f'Its second element `{end_line}` should be greater than or equal to the first element `{start_line}`.', ) - if end_line == -1: - end_line = num_lines - file_content = self.read_file(path, start_line=start_line, end_line=end_line) # Get the detected encoding @@ -372,6 +370,10 @@ def view(self, path: Path, view_range: list[int] | None = None) -> CLIResult: '\n'.join(file_content.splitlines()), str(path), start_line ) # Remove extra newlines + # Prepend warning if we truncated the end_line + if warning_message: + output = f'NOTE: {warning_message}\n{output}' + return CLIResult( path=str(path), output=output, diff --git a/pyproject.toml b/pyproject.toml index 0598d25..50e71e0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "openhands-aci" -version = "0.3.1" +version = "0.3.2" description = "An Agent-Computer Interface (ACI) designed for software development agents OpenHands." authors = ["OpenHands"] license = "MIT" diff --git a/tests/integration/editor/test_error_handling.py b/tests/integration/editor/test_error_handling.py index 215d20d..9d8ac1a 100644 --- a/tests/integration/editor/test_error_handling.py +++ b/tests/integration/editor/test_error_handling.py @@ -86,7 +86,7 @@ def test_view_range_validation(temp_file): 'should be a list of two integers' in result_json['formatted_output_and_error'] ) - # Test out of bounds range + # Test out of bounds range: should clamp to file end and show a warning result = file_editor( command='view', path=temp_file, @@ -94,10 +94,7 @@ def test_view_range_validation(temp_file): enable_linting=False, ) result_json = parse_result(result) - assert ( - 'should be smaller than the number of lines' - in result_json['formatted_output_and_error'] - ) + assert 'NOTE: We only show up to 3 since there\'re only 3 lines in this file.' in result_json['formatted_output_and_error'] # Test invalid range order result = file_editor(