fix(read_file): clamp end position instead of erroring when it exceeds file length

When read_file is called with an end position beyond the file length,
instead of returning an error that forces a retry, now clamps to the
actual file length and returns the content with an informative message.

This eliminates wasteful retry cycles where the LLM had to make a
second request with the corrected end position.
This commit is contained in:
Dhanji R. Prasanna
2026-01-12 05:11:09 +05:30
parent da63e79a13
commit ac17b95b24

View File

@@ -110,13 +110,13 @@ pub async fn execute_read_file<W: UiWriter>(
} }
let user_end = end_char.unwrap_or(total_file_len); let user_end = end_char.unwrap_or(total_file_len);
if user_end > total_file_len { // Clamp end position to file length (don't error, just read what's available)
return Ok(format!( let (user_end, end_was_clamped) = if user_end > total_file_len {
"❌ End position {} exceeds file length {}", (total_file_len, true)
user_end, } else {
total_file_len (user_end, false)
)); };
}
if user_start > user_end { if user_start > user_end {
return Ok(format!( return Ok(format!(
"❌ Start position {} is greater than end position {}", "❌ Start position {} is greater than end position {}",
@@ -164,6 +164,12 @@ pub async fn execute_read_file<W: UiWriter>(
"{}\n🔍 {} lines read (truncated, chars {}-{} of {}, context {}%)", "{}\n🔍 {} lines read (truncated, chars {}-{} of {}, context {}%)",
partial_content, line_count, start_boundary, end_boundary, total_file_len, context_pct partial_content, line_count, start_boundary, end_boundary, total_file_len, context_pct
)) ))
} else if end_was_clamped {
// End position exceeded file length, clamped to actual length
Ok(format!(
"{}\n🔍 {} lines read (chars {}-{}, end clamped from {} to file length {})",
partial_content, line_count, start_boundary, end_boundary, end_char.unwrap(), total_file_len
))
} else if start_char.is_some() || end_char.is_some() { } else if start_char.is_some() || end_char.is_some() {
Ok(format!( Ok(format!(
"{}\n🔍 {} lines read (chars {}-{})", "{}\n🔍 {} lines read (chars {}-{})",