Skip to content

fix: prevent memory explosion in search_files_content#2983

Merged
dgageot merged 2 commits into
docker:mainfrom
dgageot:board/9e2cba115426f862
Jun 3, 2026
Merged

fix: prevent memory explosion in search_files_content#2983
dgageot merged 2 commits into
docker:mainfrom
dgageot:board/9e2cba115426f862

Conversation

@dgageot
Copy link
Copy Markdown
Member

@dgageot dgageot commented Jun 3, 2026

The search_files_content filesystem tool can produce massive results when searching across large directory trees, causing unbounded memory growth as the output cascades through multiple layers: tool output buffer → in-memory message list → session store/WAL → re-serialization into every subsequent model request for the rest of the session.

This change caps the total output at 1 MiB with a truncation notice and skips files larger than 10 MiB. Output is now built incrementally with strings.Builder instead of accumulating a slice and joining at the end. The file-size check now stats the target (following symlinks, not the symlink itself) and only scans regular files, which also prevents reading forever from device files or FIFOs like /dev/zero.

Tests cover output-cap truncation, large-file skipping, and the symlink-to-large-file case.

dgageot added 2 commits June 3, 2026 10:39
Use stat (following symlinks) instead of lstat when checking file size,
so symlinks to large files are skipped rather than read whole. Also skip
non-regular files to avoid reading forever from devices like /dev/zero.
Remove unreachable early return of fs.SkipAll.
@dgageot dgageot requested a review from a team as a code owner June 3, 2026 09:11
Copy link
Copy Markdown

@docker-agent docker-agent left a comment

Choose a reason for hiding this comment

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

Assessment: 🟢 APPROVE

The changes are correct and well-implemented. The output cap (1 MiB), large-file skip (10 MiB), symlink-aware stat guard, and incremental strings.Builder pattern all work as intended. Tests cover truncation, large-file skipping, and the symlink-to-large-file regression case.

@dgageot dgageot merged commit a885938 into docker:main Jun 3, 2026
8 checks passed
@aheritier aheritier added area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/fix PR fixes a bug (maps to fix: commit prefix) labels Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/fix PR fixes a bug (maps to fix: commit prefix)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants