Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lower max file size, char limit #849

Merged
merged 3 commits into from
Jan 29, 2025
Merged
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
27 changes: 20 additions & 7 deletions crates/goose-mcp/src/developer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,19 @@ impl DeveloperRouter {
.map_err(|e| ToolError::ExecutionError(e.to_string()))?;

let output_str = String::from_utf8_lossy(&output.stdout);

// Check the character count of the output
const MAX_CHAR_COUNT: usize = 400_000; // 409600 chars = 400KB
let char_count = output_str.chars().count();
if char_count > MAX_CHAR_COUNT {
return Err(ToolError::ExecutionError(format!(
"Shell output from command '{}' has too many characters ({}). Maximum character count is {}.",
command,
char_count,
MAX_CHAR_COUNT
)));
}

Ok(vec![
Content::text(output_str.clone()).with_audience(vec![Role::Assistant]),
Content::text(output_str)
Expand Down Expand Up @@ -312,9 +325,9 @@ impl DeveloperRouter {

async fn text_editor_view(&self, path: &PathBuf) -> Result<Vec<Content>, ToolError> {
if path.is_file() {
// Check file size first (2MB limit)
const MAX_FILE_SIZE: u64 = 2 * 1024 * 1024; // 2MB in bytes
const MAX_CHAR_COUNT: usize = 1 << 20; // 2^20 characters (1,048,576)
// Check file size first (400KB limit)
const MAX_FILE_SIZE: u64 = 400 * 1024; // 400KB in bytes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's recreate the same limit on shell tool outputs? and same warning/error

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const MAX_CHAR_COUNT: usize = 400_000; // 409600 chars = 400KB

let file_size = std::fs::metadata(path)
.map_err(|e| {
Expand All @@ -324,9 +337,9 @@ impl DeveloperRouter {

if file_size > MAX_FILE_SIZE {
return Err(ToolError::ExecutionError(format!(
"File '{}' is too large ({:.2}MB). Maximum size is 2MB to prevent memory issues.",
"File '{}' is too large ({:.2}KB). Maximum size is 400KB to prevent memory issues.",
path.display(),
file_size as f64 / 1024.0 / 1024.0
file_size as f64 / 1024.0
)));
}

Expand Down Expand Up @@ -778,8 +791,8 @@ mod tests {
let many_chars_path = temp_dir.path().join("many_chars.txt");
let many_chars_str = many_chars_path.to_str().unwrap();

// Create a file with more than 2^20 characters but less than 2MB
let content = "x".repeat((1 << 20) + 1); // 2^20 + 1 characters
// Create a file with more than 400K characters but less than 400KB
let content = "x".repeat(405_000);
std::fs::write(&many_chars_path, content).unwrap();

let result = router
Expand Down
Loading