From 6d97b4b70be031be2fa4d570f5002869ef0c81c4 Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Mon, 10 Nov 2025 18:32:59 +0530 Subject: [PATCH] fix max line range size for zed agent --- crates/agent/src/tools/read_file_tool.rs | 38 +++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/agent/src/tools/read_file_tool.rs b/crates/agent/src/tools/read_file_tool.rs index 52f88aa4db03a2bc01b0fd10fe99f8bad04c24f1..65db6db903a12116754cf6783ec2f32351102dc2 100644 --- a/crates/agent/src/tools/read_file_tool.rs +++ b/crates/agent/src/tools/read_file_tool.rs @@ -14,6 +14,10 @@ use util::markdown::MarkdownCodeBlock; use crate::{AgentTool, ToolCallEventStream, outline}; +/// Maximum allowed size for reading file content with line ranges. +/// If a line range request exceeds this size, it will be rejected. +pub const MAX_LINE_RANGE_SIZE: usize = 262144; // 256 KB + /// Reads the content of the given file in the project. /// /// - Never attempt to read a path that hasn't been previously mentioned. @@ -221,7 +225,39 @@ impl AgentTool for ReadFileTool { log.buffer_read(buffer.clone(), cx); })?; - Ok(result.into()) + if result.len() > MAX_LINE_RANGE_SIZE { + let start_line = input.start_line.unwrap_or(1); + let end_line = input.end_line.unwrap_or(u32::MAX); + let line_count = end_line.saturating_sub(start_line).saturating_add(1); + + if line_count <= 1 { + // Likely minified or single-line file + Ok(formatdoc! {" + The requested line contains {} bytes of content, which exceeds the maximum size of {} bytes. + + This appears to be a minified or single-line file. Use the terminal tool to run: `head -c 4096 {}` + + This will read the first 4096 bytes to help you understand the file structure.", + result.len(), + MAX_LINE_RANGE_SIZE, + abs_path.display() + } + .into()) + } else { + Ok(formatdoc! {" + The requested line range (lines {}-{}) contains {} bytes of content, which exceeds the maximum size of {} bytes. + + Please request a smaller line range, or use the `grep` tool to search for specific content within this section.", + start_line, + end_line, + result.len(), + MAX_LINE_RANGE_SIZE + } + .into()) + } + } else { + Ok(result.into()) + } } else { // No line ranges specified, so check file size to see if it's too big. let buffer_content = outline::get_buffer_content_or_outline(