Skip to content

feat(memory): hierarchical AGENTS.md discovery — walk cwd up to repo root#233

Merged
emal-avala merged 1 commit intomainfrom
feat/memory-hierarchical-agents-md
Apr 23, 2026
Merged

feat(memory): hierarchical AGENTS.md discovery — walk cwd up to repo root#233
emal-avala merged 1 commit intomainfrom
feat/memory-hierarchical-agents-md

Conversation

@emal-avala
Copy link
Copy Markdown
Member

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.md along the way in outermost→innermost order.

Before: only AGENTS.md at the cwd level (plus user-global) was loaded, so in a monorepo like ~/repo/packages/foo/ the config at ~/repo/AGENTS.md was invisible to a session launched inside foo/.

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

Layout Loaded (in order)
repo/AGENTS.md, repo/pkg/AGENTS.md, cwd=repo/pkg/ repo → repo/pkg
repo/AGENTS.md, cwd=repo/a/b/c/ (no intermediate files) repo only
repo/.agent/AGENTS.md, repo/pkg/AGENTS.md, cwd=repo/pkg/ repo/.agent → repo/pkg
no .git anywhere, cwd=/tmp/foo/ only /tmp/foo/AGENTS.md (no parent escape)

Tests

Six new tests in memory::tests exercise:

  • Walk range from cwd up to .git root (inclusive of both ends)
  • Stop-at-.git — no escape beyond repo root
  • Outermost-first ordering
  • No-.git fallback: walk collapses to start
  • Handles missing intermediate files gracefully
  • .agent/AGENTS.md subdir honored at each level

All 8 memory tests pass, cargo clippy --workspace --tests --no-deps -- -D warnings clean.

Test plan

  • cargo test -p agent-code-lib --lib memory::tests (8/8 pass)
  • cargo clippy --workspace --tests --no-deps -- -D warnings
  • cargo fmt --all --check

…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
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@emal-avala emal-avala merged commit fa6d033 into main Apr 23, 2026
14 checks passed
@emal-avala emal-avala deleted the feat/memory-hierarchical-agents-md branch April 23, 2026 23:46
@emal-avala emal-avala mentioned this pull request Apr 24, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant