read_file reports an impossible line range as empty, including end_line=-1
read_file returns an empty string for a line range that cannot exist, including the case where a caller applies the documented negative-index idiom to end_line.
The tool documents start_line as "the 0-based index of the first line to be retrieved, negative values count from the end of the file", and end_line as "the 0-based index of the last line to be retrieved (inclusive)". The implementation is a bare Python slice:
# src/serena/repl/api/fs_api.py:196-197
lines = TextUtils.split_lines(project.read_file(relative_path))
lines = lines[start_line:] if end_line is None else lines[start_line : end_line + 1]so every range a slice cannot satisfy comes back empty rather than as an error. Against a four-line file line0..line3:
| call | result | what a caller would conclude |
|---|---|---|
start_line=-2, end_line=None |
line2\nline3\n |
correct, and matches the documented negative start_line |
start_line=0, end_line=-1 |
"" |
the region is empty — but it is the whole file |
start_line=-3, end_line=-1 |
"" |
"the last three lines" is empty |
start_line=2, end_line=0 |
"" |
the region is empty — the range is reversed |
start_line=99, end_line=100 |
"" |
the region is empty — the file has no such lines |
start_line=0, end_line=99 |
whole file | truncates silently, which seems fine |
The second and third rows are the ones I would fix: end_line=-1 is what a caller writes when they have internalised the documented negative-index behaviour for start_line, and "the last three lines" returning "" is not a distinguishable answer.
Why I think it is worth raising rather than documenting
TextUtils already has the opposite behaviour one method away, in the same class:
# src/solidlsp/ls_utils.py:291-302
def get_index_from_line_col(text: str, line: int, col: int) -> int:
while text_stepper.line < line:
if not text_stepper.step_line():
raise InvalidTextLocationError(f"{line=}, {col=}")so an out-of-range line raises InvalidTextLocationError there and returns "" here. And the test module states the coupling that makes an empty read consequential: "delete_lines/replace_lines require the same range of lines to have been read via read_file beforehand, so the line indices reported by read_file must be the ones the editing tools apply to." A caller that reads "" and concludes the region is empty may then act on that conclusion.
What I could not check
I could not exercise the editing side. CodeEditor._open_file_context is abstract in practice — it raises NotImplementedError: This method must be overridden for each subclass — so delete_lines needs a SolidLSP or JetBrains backend, i.e. a running language server, which this environment does not have. So I am not claiming that a bad range corrupts a file; I only know that the read reports an impossible range as an empty result. If the editing tools do validate their range, then this is purely a misleading read and the fix belongs in read_file's reporting.
What I would change, if the intent is to keep the slice
Either is defensible, and the choice is yours:
- validate the range in
fs_api.read_fileand raise —InvalidTextLocationErroralready exists and is the right shape; or - keep the lenient slice and say so in the docstring, so that
""is documented as "this range does not exist" rather than "this region is empty".
I have a branch with a test for the current behaviour if that is useful for pinning it either way, but I did not want to guess which of the two you want, so I have not attempted a fix.
Reproduction is a few lines against a temporary project, using the same harness as test/serena/test_file_tools.py (Project.load plus a stub agent); no language server is needed for the read path.
Source: oraios/serena