Skip to content

Commit da8451b

Browse files
author
cellwebb
committed
fix(tools): improve read_lines out-of-bounds handling
- Return error only when entire requested range is outside file bounds - Clamp to file end when only upper boundary exceeds file length - Store original values for accurate error messages - Apply same behavior for both direct calls and executor usage - Improves robustness for agents requesting line ranges Examples: - '3-10' in 5-line file → returns lines 3-5 (clamped) - '8-12' in 5-line file → returns error (entire range outside) - '10' in 5-line file → returns error (single line outside)
1 parent 7210308 commit da8451b

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/clippy/tools/read_lines.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,19 @@ def parse_line_range(
169169
except ValueError:
170170
return True, "", 1, total_lines
171171

172-
# Check if range is out of bounds (before clamping)
173-
if start < 1 or end > total_lines:
172+
# Store original values before clamping to check bounds
173+
original_start = start
174+
original_end = end
175+
176+
# Clamp to file bounds
177+
start = max(1, start)
178+
end = min(total_lines, end)
179+
180+
# Check if the entire requested range is outside file bounds
181+
if original_end < 1 or original_start > total_lines:
174182
return (
175183
False,
176-
f"Line range {range_spec} (resolved to {start}-{end}) is outside "
184+
f"Line range {range_spec} (resolved to {original_start}-{original_end}) is outside "
177185
f"file bounds (file has {total_lines} lines)",
178186
0,
179187
0,

0 commit comments

Comments
 (0)