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
.venv directory (any standard Python venv)
- Call
file_read(path=".", mode="find") or file_read(path="data/", mode="view") where the agent resolves to a directory containing .venv
- The tool walks into
.venv and 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_DIRS env var (consistent with existing FILE_READ_RECURSIVE_DEFAULT pattern)
- Gitignore-aware: Use
.gitignore rules 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
Bug
When
file_readis called with a directory path,find_files()usesos.walk()and filters hidden files (file.startswith(".")) but does not filter hidden directories. This meansos.walkdescends into.venv,.git,__pycache__,node_modules, etc.Impact
A
.venvdirectory (~156MB) caused the tool to collect ~39M tokens worth of file content, crashing the agent with:Reproduction
.venvdirectory (any standard Python venv)file_read(path=".", mode="find")orfile_read(path="data/", mode="view")where the agent resolves to a directory containing.venv.venvand returns thousands of filesRoot Cause
In
find_files(), the directory branch:Hidden files are skipped, but
_dirsis never modified in-place. Per Python docs, modifyingdirnamesin-place is the standard way to pruneos.walktraversal.Possible Fixes
Several approaches could work — leaving it to maintainers to decide:
dirs[:] = [d for d in dirs if not d.startswith(".")]— this alone catches.venv,.git,.mypy_cache, etc.FILE_READ_SKIP_DIRSenv var (consistent with existingFILE_READ_RECURSIVE_DEFAULTpattern).gitignorerules to determine which directories to skipEnvironment