Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 7 additions & 4 deletions internal/tool/file_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ func (p *FileReadProvider) Execute(ctx context.Context, args map[string]any) (st

maxLines := fileReadMaxLines
if endLine > 0 {
requested := int(endLine) - int(startLine) + 1
if requested <= 0 {
return "", fmt.Errorf("invalid line range: start_line %d is greater than end_line %d", int(startLine), int(endLine))
if int(endLine) < int(startLine) {
// Models sometimes derive start_line from the old side of a diff
// hunk while end_line comes from the new file, or simply swap the
// two. Serve the swapped range instead of failing the call; the
// total-line count in the response lets the caller recover.
startLine, endLine = endLine, startLine
}
if requested < maxLines {
if requested := int(endLine) - int(startLine) + 1; requested < maxLines {
maxLines = requested
}
}
Expand Down
15 changes: 10 additions & 5 deletions internal/tool/file_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,20 +353,25 @@ func TestExecute_EmptyFilePath(t *testing.T) {
}
}

func TestExecute_InvalidLineRange(t *testing.T) {
func TestExecute_SwappedLineRange(t *testing.T) {
dir := t.TempDir()
writeTestFile(t, dir, "test.txt", "a\nb\nc\n")

fr := &FileReader{RepoDir: dir, Mode: ModeWorkspace}
p := NewFileRead(fr)

_, err := p.Execute(context.Background(), map[string]any{
// An inverted range is a common model slip (start from the old hunk side,
// end from the new file). The tool swaps it instead of erroring.
got, err := p.Execute(context.Background(), map[string]any{
"file_path": "test.txt",
"start_line": float64(5),
"start_line": float64(3),
"end_line": float64(2),
})
if err == nil {
t.Error("expected error for invalid line range")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(got, "2|b\n3|c\n") {
t.Errorf("Execute() = %q, want swapped range 2-3", got)
}
}

Expand Down
Loading