file_read find_files() traverses hidden directories (.venv, .git, etc.) causing token explosion
Bug
When file_read is called with a directory path, find_files() uses os.walk() and filters hidden files (file.startswith(".")) but does not filter hidden directories. This means os.walk descends into .venv, .git, __pycache__, node_modules, etc.
Impact
A .venv directory (~156MB) caused the tool to collect ~39M tokens worth of file content, crashing the agent with:
prompt is too long: 1069752 tokens > 200000 maximum
Reproduction
- Create a project with a
.venvdirectory (any standard Python venv) - Call
file_read(path=".", mode="find")orfile_read(path="data/", mode="view")where the agent resolves to a directory containing.venv - The tool walks into
.venvand returns thousands of files
Root Cause
In find_files(), the directory branch:
for root, _dirs, files in os.walk(pattern):
if not recursive and root != pattern:
continue
for file in sorted(files):
if not file.startswith("."): # Skip hidden files
matching_files.append(os.path.join(root, file))
Hidden files are skipped, but _dirs is never modified in-place. Per Python docs, modifying dirnames in-place is the standard way to prune os.walk traversal.
Possible Fixes
Several approaches could work — leaving it to maintainers to decide:
- Minimal: Apply the same hidden-item logic to directories:
dirs[:] = [d for d in dirs if not d.startswith(".")]— this alone catches.venv,.git,.mypy_cache, etc. - Configurable: Add a
FILE_READ_SKIP_DIRSenv var (consistent with existingFILE_READ_RECURSIVE_DEFAULTpattern) - Gitignore-aware: Use
.gitignorerules to determine which directories to skip - Guard: Add a max file count or total size limit to prevent runaway traversals
Environment
- strands-tools: latest (main)
- Python 3.12
- macOS
@ranyakhemiri Opened a fix in #440. Filters hidden directories (starting with .) in place during os.walk.