Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions openhands_aci/editor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,30 +348,32 @@ 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's only {num_lines} lines in this file"
Comment thread
ryanhoangt marked this conversation as resolved.
Outdated
Comment thread
ryanhoangt marked this conversation as resolved.
Outdated
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
output = self._make_output(
'\n'.join(file_content.splitlines()), str(path), start_line
) # Remove extra newlines

# Append warning if we truncated the end_line
if warning_message:
output += f'\nNOTE: {warning_message}\n'

return CLIResult(
path=str(path),
output=output,
Expand Down
7 changes: 2 additions & 5 deletions tests/integration/editor/test_error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,15 @@ 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,
view_range=[1, 10], # File only has 3 lines
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\'s only 3 lines in this file' in result_json['formatted_output_and_error']
Comment thread
ryanhoangt marked this conversation as resolved.
Outdated

# Test invalid range order
result = file_editor(
Expand Down