google-gemini / google-gemini/gemini-cli

Harden ReadManyFilesTool: Concurrency Control and Defensive Guards

Open
#21,841 11 comments 0 reactions 0 assignees View on GitHub
area/core effort/medium kind/enhancement priority/p2 Stale status/bot-triaged
Dominant language
TypeScript
Stars
107k
Forks
14.6k
Avg merge
2d 3h
Merged PRs (30d)
45

Description

### Problem

`ReadManyFilesTool` currently relies on optimistic execution and assumes small, targeted file reads. Since the tool is LLM-driven, it can receive overly broad glob patterns (e.g., `**/*`, `src/**/*`), which may trigger large-scale file ingestion.

The current implementation lacks several defensive safeguards:

1. **Unbounded Parallelism (EMFILE Risk)**
All discovered files are mapped to `readFile` promises and executed using `Promise.allSettled`.
In large repositories this can exhaust OS file descriptors, resulting in errors such as:
```
EMFILE: too many open files
```

2. **No File Count Guard**
There is no limit on the number of files processed in a single request. Reading thousands of files can cause large memory spikes and may lead to Gemini API request failures (e.g., `400 Bad Request`) due to context size overflow.

3. **Missing Bulk File Size Protection**
Although a global 20MB limit exists, bulk reads should enforce a much smaller per-file limit to prevent large files from consuming the model's context unnecessarily.

4. **Inefficient Binary Handling**
The tool performs binary detection by reading file headers for every matched file, which results in unnecessary I/O during large glob operations.

---

### Proposed Solution

Introduce a **Defensive Read architecture** for `ReadManyFilesTool`:

- **Concurrency Control**
- **`max_files` Parameter**
- **Bulk File Size Guard**
- **Standard Directory Exclusions**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.