Skip to content

feat(context-files): merged AGENTS.md directory chain from git root to cwd (port of grok-cli) - #80781

Merged
teknium1 merged 1 commit into
mainfrom
grok-cli-port/agentsmd-chain
Aug 8, 2026
Merged

feat(context-files): merged AGENTS.md directory chain from git root to cwd (port of grok-cli)#80781
teknium1 merged 1 commit into
mainfrom
grok-cli-port/agentsmd-chain

Conversation

@teknium1

@teknium1 teknium1 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Sessions launched deep inside a monorepo now see the whole AGENTS.md chain — git root down through every intermediate directory to cwd — merged into the system prompt at session build, instead of only the single cwd file. Port of superagent-ai/grok-cli's directory-chain instructions loading (src/utils/instructions.ts, directoryChain + loadAgentsSegments).

What changed

  • agent/prompt_builder.py
    • New _agents_md_directory_chain(cwd): computes git root → cwd directory list (root first, cwd last). Without a git root — or when cwd sits outside it — the chain is [cwd] only, so parents are never consulted and a stray AGENTS.md in /tmp/$HOME can't leak in (mirrors the existing _find_hermes_md safety rationale).
    • _load_agents_md now walks that chain: each directory contributes its AGENTS.md/agents.md (first name wins per dir) as a provenance-labelled section (## ../../AGENTS.md, …, ## AGENTS.md), deeper files last so more specific guidance takes precedence.
    • Identical content encountered again along the chain (copied/symlinked files) is deduplicated.
    • Budgets: each file passes through the existing _scan_context_content security scan and per-file _truncate_content budget; a multi-file merge is additionally capped by the same total budget so a deep monorepo can't multiply context-file spend unbounded.
    • Zero regression by construction: with a single match the returned section is byte-identical to the old single-file output (asserted in tests). Discovery still happens exactly once at session build — the system prompt stays byte-stable for the life of a conversation (prompt-cache safe).
  • website/docs/user-guide/features/context-files.md — documents the chain with a monorepo diagram; existing progressive-subdirectory-hints docs unchanged (that mechanism already dedupes by content digest, so files injected at startup that hints later re-discover are the same bytes and its own digest guard plus install-tree exclusions still apply).
  • tests/agent/test_prompt_builder.py — 5 new tests (chain merge + ordering + provenance headers, gap skipping, dedup, byte-identical single-file output, no-git-root cwd-only isolation).

Complementary, not duplicate (dupe sweep done)

Validation

Check Result
scripts/run_tests.sh tests/agent/test_prompt_builder.py ✅ 61/61 passed (56 existing + 5 new)
ruff check on touched files ✅ All checks passed
scripts/audit_pr_attribution.py --fix ✅ all emails mapped
Single-file behavior ✅ byte-identical (test_agents_md_single_file_output_unchanged)
No-git-root isolation ✅ parents never consulted (test_agents_md_no_git_root_stays_cwd_only)

Infographic

Directory-chain AGENTS.md loading

@alt-glitch alt-glitch added type/feature New feature or request comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have labels Aug 7, 2026
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

૮ >ﻌ< ა ci review

ran on 162778b

⚠️ Warnings

OSV vulnerability scan · View job

50 known vulnerabilities found in pinned dependencies.

How to fix:

Review the findings in the Security tab. Update the affected dependencies if a patched version is available.

@spfcraze

spfcraze commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

This was generated by AI during triage.

Summary:
The description's dupe sweep lists four complementary items, but not #39061 or #21686 — both feature requests covering the AGENTS.md directory-chain loading this PR adds.

Problems:

Solution:
Add #39061 and #21686 to the complementary list in the description.


Checked against 162778b — the tip of grok-cli-port/agentsmd-chain when this was written.

@teknium1
teknium1 merged commit 2e2fcc0 into main Aug 8, 2026
45 checks passed
@teknium1
teknium1 deleted the grok-cli-port/agentsmd-chain branch August 8, 2026 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants