{"record":{"id":"72eb4a78472fe021","repo":"alibaba/open-code-review","slug":"file-q-has-only-d-lines-requested-range-d-d","errorCode":null,"errorMessage":"file %q has only %d lines, requested range %d-%d","messagePattern":"file %q has only (.+?) lines, requested range (.+?)-(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/tool/file_read.go","lineNumber":55,"sourceCode":"\n\tmaxLines := fileReadMaxLines\n\tif endLine > 0 {\n\t\trequested := int(endLine) - int(startLine) + 1\n\t\tif requested <= 0 {\n\t\t\treturn \"\", fmt.Errorf(\"invalid line range: start_line %d is greater than end_line %d\", int(startLine), int(endLine))\n\t\t}\n\t\tif requested < maxLines {\n\t\t\tmaxLines = requested\n\t\t}\n\t}\n\n\tlines, totalLines, err := p.FileReader.ReadLines(ctx, filePath, int(startLine), maxLines)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"file %q not found: %w\", filePath, err)\n\t}\n\n\tif totalLines > 0 && int(startLine)-1 >= totalLines {\n\t\treturn \"\", fmt.Errorf(\"file %q has only %d lines, requested range %d-%d\", filePath, totalLines, int(startLine), int(endLine))\n\t}\n\n\teffectiveEnd := totalLines\n\tif endLine > 0 && int(endLine) < effectiveEnd {\n\t\teffectiveEnd = int(endLine)\n\t}\n\tfullRange := effectiveEnd - (int(startLine) - 1)\n\ttruncated := fullRange > fileReadMaxLines\n\n\tdisplayEnd := int(startLine) - 1 + len(lines)\n\n\tvar sb strings.Builder\n\tsb.WriteString(fmt.Sprintf(\"File: %s (Total lines: %d)\\n\", filePath, totalLines))\n\tsb.WriteString(fmt.Sprintf(\"IS_TRUNCATED: %t\\n\", truncated))\n\tsb.WriteString(fmt.Sprintf(\"LINE_RANGE: %d-%d\\n\", int(startLine), displayEnd))\n\tfor i, line := range lines {\n\t\tsb.WriteString(fmt.Sprintf(\"%d|%s\\n\", int(startLine)+i, line))\n\t}","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/internal/tool/file_read.go#L37-L73","documentation":"Range error from FileReadProvider.Execute: the requested start_line lies at or beyond the end of the file. The tool knows the file's total line count after reading (totalLines) and rejects the request when start_line-1 >= totalLines, reporting the actual line count and the requested range instead of returning empty content.","triggerScenarios":"Executing file_read with start_line greater than the file's total number of lines (e.g. start_line=500 on a 120-line file). Only triggers when totalLines > 0; empty files (totalLines == 0) are not rejected by this branch.","commonSituations":"Agent using line numbers from a previous (longer) version of the file; paging past EOF with a fixed page size; stale index or search results referencing deleted lines.","solutions":["Clamp start_line to at most the file's total line count reported in the error.","Re-read from line 1 or re-list the file to get its current size before paging.","If you were searching for content, re-run code_search — the file may have changed."],"exampleFix":"// before: file has 120 lines\n{\"file_path\": \"a.go\", \"start_line\": 500}\n// after\n{\"file_path\": \"a.go\", \"start_line\": 1, \"end_line\": 120}","handlingStrategy":"validation","validationCode":"total, err := lineCount(filePath) // e.g. via ReadLines first page\nif err != nil { return err }\nif startLine > total {\n    return fmt.Errorf(\"clamping start_line %d to total %d\", startLine, total)\n}","typeGuard":"func inBounds(start, total int) bool { return total == 0 || start >= 1 && start <= total }","tryCatchPattern":"out, err := provider.Execute(ctx, args)\nif err != nil && strings.Contains(err.Error(), \"has only\") {\n    var total int\n    fmt.Sscanf(err.Error(), \"file %q has only %d lines\", new(string), &total)\n    args[\"start_line\"] = float64(total)\n    out, err = provider.Execute(ctx, args)\n}","preventionTips":["Parse the total line count from the tool's 'Total lines:' header on the first read and page within it.","Refresh your view of the file after any edit; line numbers shift.","Clamp start_line client-side to the last known total before paging."],"tags":["file-read","line-range","bounds-check","tool-execution"],"backgroundTag":"out-of-range-read","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}