Added recursive AGENTS.md loading from child dirs
Summary
I've successfully implemented recursive loading of AGENTS.md files from child folders. Here's what I did:
Changes Made:
-
Added
findDownfunction in/home/runner/work/opencode/opencode/packages/opencode/src/util/filesystem.ts:69-84:- Searches for files recursively in child directories using Bun's glob functionality
- Includes a configurable
maxDepthparameter (default: 3) to prevent performance issues - Uses the pattern
**/${target}to find files at any depth
-
Updated
custom()function in/home/runner/work/opencode/opencode/packages/opencode/src/session/system.ts:53-84:- Now searches both upward (existing functionality) and downward (new functionality) for AGENTS.md files
- Maintains backward compatibility with existing behavior
- Limits search depth to 3 levels to avoid performance issues in large codebases
Key Features:
- Backward Compatible: Existing upward search functionality remains unchanged
- Performance Optimized: Limited to 3 levels of depth by default to prevent scanning entire large codebases
-
Comprehensive Coverage: Now finds AGENTS.md files in:
- Current directory and parent directories (existing)
- Child directories up to 3 levels deep (new)
- Global config directory (existing)
- Home directory
.claudefolder (existing)
Testing:
The implementation was tested and verified to correctly find AGENTS.md files at different directory levels, respecting the depth limit parameter.
This should resolve the issue where AGENTS.md files in child folders were being ignored, allowing for more granular project-specific instructions at different directory levels.
Closes #847
view session | view log
@adamdotdevin @thdxr @opencode-agent Great work on implementing this! I'd like to propose an alternative strategy that might offer better performance and more intuitive behavior by integrating directly with the ReadTool.
What if the ReadTool was updated to discover AGENTS.md files based on the directory tree being read?
if read path = "src/app/api/login/route.ts"
then tree = [
"src",
"src/app",
"src/app/api",
"src/app/login",
]
and search = for (const file of CUSTOM_FILES) [
"src/{file}",
"src/app/{file}",
"src/app/api/{file}",
"src/app/login/{file}",
]
1. Context Relevance:
- Reduces context bloat, only loads rules relevant files
- Avoids loading rules from unrelated project areas
2. Performance:
- Lazy loading based on actual file access patterns
- Leverages existing ReadTool caching mechanisms
3. Predictable Behavior:
- Mirrors established patterns from TypeScript (
tsconfig.jsonresolution) and Node.js (module resolution) - Developers already understand this "walk up the tree" mental model
Implementation Considerations
- Could reuse existing filesystem utilities and caching
- Maintains backward compatibility with current AGENTS.md patterns
- Integrates naturally with ReadTool's existing file access workflow
Comparison with Current PR
The recursive implementation scans child directories proactively, which is excellent for comprehensive discovery. The tree-walking approach would be more reactive, discovering context only when relevant files are accessed.
Both approaches have merit - curious what you think about the trade-offs between comprehensive upfront loading vs. on-demand contextual loading?
This suggestion is inspired by how Codex CLI / Gemini CLI / Claude Code handle dynamic (AGENTS|CLAUDE).md discovery, and how established tools like TypeScript handle configuration file resolution