feat(memory): hierarchical AGENTS.md discovery — walk cwd up to repo root#233
Merged
emal-avala merged 1 commit intomainfrom Apr 23, 2026
Merged
Conversation
…root Previously `MemoryContext::load(cwd)` only checked `AGENTS.md` at the session cwd + user global. If a monorepo sub-package had its own AGENTS.md and the agent was invoked from inside that sub-package, the package-local instructions were loaded — but sibling root-level AGENTS.md at the repo root was missed unless invoked from there. Conversely, if invoked from the repo root, no sub-package AGENTS.md in a deeper directory ever applied. Fix: add `hierarchical_project_files(start)` which walks from `start` up to the nearest `.git` directory (repo root) — or stops at `start` if no `.git` is found — collecting every AGENTS.md / .agent/AGENTS.md / CLAUDE.md / .claude/CLAUDE.md along the way. Returns paths outermost-first so deeper (more specific) files override broader ones in the composed prompt. Safety: never escapes the repo root — if no `.git` anywhere in the ancestor chain, the walk stops at `start` and never reads from a random parent dir. Called from `load_project_context` in place of the old two-point (cwd + .agent/) lookup. Rules and local overrides (layers 3 & 4) are unchanged. Tests: 6 new, including: - walk from deeply nested cwd up through 3 AGENTS.md tiers - ordering guarantee (outermost first) - never escapes the repo root (walk stays inside tmpdir) - handles missing intermediate files (collects only what exists) - graceful without `.git` (stops at start, no escape) - .agent/AGENTS.md at each level is picked up alongside AGENTS.md
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extends project-context discovery to walk from the session cwd up to the repo root, loading every
AGENTS.md/.agent/AGENTS.md/CLAUDE.md/.claude/CLAUDE.mdalong the way in outermost→innermost order.Before: only
AGENTS.mdat the cwd level (plus user-global) was loaded, so in a monorepo like~/repo/packages/foo/the config at~/repo/AGENTS.mdwas invisible to a session launched insidefoo/.After: the walk stops at the nearest ancestor containing
.git, never escaping the repo. Deeper files are loaded last so inner configs override outer ones when composed into the system prompt.Behavior
repo/AGENTS.md,repo/pkg/AGENTS.md, cwd=repo/pkg/repo/AGENTS.md, cwd=repo/a/b/c/(no intermediate files)repo/.agent/AGENTS.md,repo/pkg/AGENTS.md, cwd=repo/pkg/.gitanywhere, cwd=/tmp/foo//tmp/foo/AGENTS.md(no parent escape)Tests
Six new tests in
memory::testsexercise:.gitroot (inclusive of both ends).git— no escape beyond repo root.gitfallback: walk collapses tostart.agent/AGENTS.mdsubdir honored at each levelAll 8 memory tests pass,
cargo clippy --workspace --tests --no-deps -- -D warningsclean.Test plan
cargo test -p agent-code-lib --lib memory::tests(8/8 pass)cargo clippy --workspace --tests --no-deps -- -D warningscargo fmt --all --check