codex icon indicating copy to clipboard operation
codex copied to clipboard

Deduplicate AGENTS.md when CODEX_HOME is also the project root

Open grig2277 opened this issue 4 months ago • 1 comments

Summary

When CODEX_HOME is also the project root and current working directory, Codex loads the same canonical AGENTS.md file twice:

  1. as the global $CODEX_HOME/AGENTS.md;
  2. again through project instruction discovery.

The second copy is wrapped as --- project-doc ---.

Environment

  • codex-cli 0.144.6
  • Linux x86_64
  • zsh

Minimal reproduction

tmp="$(mktemp -d)"
export CODEX_HOME="$tmp"
cd "$tmp"
git init -q
printf '# CANONICAL_DEDUPE_MARKER\n' > AGENTS.md

codex debug prompt-input DEDUPE_CHECK |
  jq -c '[.. | strings] as $strings |
    {marker_count:([$strings[] | scan("# CANONICAL_DEDUPE_MARKER")]|length),
     project_doc_wrappers:([$strings[] | scan("--- project-doc ---")]|length)}'

Observed: the marker appears twice and one project-doc wrapper is present.

The same behavior was measured in a real Codex home repository:

{"agents_headers":2,"project_doc_wrappers":1,"marker_count":1,"total_string_bytes":45422}

With the one-run override -c project_doc_max_bytes=0:

{"agents_headers":1,"project_doc_wrappers":0,"marker_count":1,"total_string_bytes":31123}

Expected behavior

Instruction assembly should deduplicate identical canonical source paths across global and project discovery. If $CODEX_HOME/AGENTS.md was already loaded globally, project discovery should not append the same file again.

Impact

  • duplicate durable instructions in every turn;
  • unnecessary input-token consumption;
  • additional prompt processing latency;
  • misleading provenance because one physical file appears as two logical instruction layers.

Configuration limitations

  • Named profiles require explicit --profile selection.
  • Project config cannot select a profile.
  • A global project_doc_max_bytes=0 would incorrectly disable project instructions in every repository.
  • A conditional -c project_doc_max_bytes=0 suppresses the duplicate locally, but also suppresses the entire project instruction chain and is only a workaround.

Related work

  • #23416 tracks incorrect instruction provenance when both global and project instructions are loaded.
  • #17091 reports global and project AGENTS.md as separate layers in /status, but does not deduplicate identical paths.

grig2277 avatar Jul 19 '26 16:07 grig2277

The same AGENTS.md is currently injected twice when CODEX_HOME is also the local project root.

Confirmed against current main at 678157acaa.

  • Observable sequence: load $CODEX_HOME/AGENTS.md as global instructions, then run project instruction discovery from that same directory.
  • Expected: the physical source appears once as global instructions.
  • Actual: the same source is appended again as a project entry and rendered behind a --- project-doc --- separator.

The prepared patch is limited to instruction assembly:

  • convert the global instruction source to its PathUri once;
  • when merging locally discovered project entries, discard only an entry with that exact source path;
  • leave remote-environment entries untouched, since a matching path string there may identify a different physical file;
  • add a regression test comparing the complete assembled LoadedAgentsMd object and requiring an empty project-entry list.

Actual screenshots

Before — the focused regression exposes the duplicate project entry

Before: VS Code terminal shows the focused AGENTS.md regression failing because the assembled instructions contain a duplicate Project entry

After — the same focused regression passes

After: VS Code terminal shows the same focused AGENTS.md deduplication regression passing

Validation:

  • agents_md::tests: 42/42 passed
  • just fix -p codex-core: passed
  • just fmt: passed

I have a PR ready if issue accepted

Kevinjohn avatar Jul 19 '26 22:07 Kevinjohn

Still reproducible on codex-cli 0.146.0 via codex debug prompt-input. This is especially annoying when $CODEX_HOME is itself version-controlled: opening the settings/config repo is a normal workflow, but every turn duplicates the global AGENTS.md and burns context unless the user remembers a bespoke profile or one-off override.

PaulRBerg avatar Aug 01 '26 11:08 PaulRBerg

Confirmed still reproducible on codex-cli 0.153.4, macOS arm64.

When cwd == CODEX_HOME (/Users/.../.codex), the same physical AGENTS.md appears twice in the model-visible instructions:

  1. global instructions
  2. project instructions after "--- project-doc ---"

I also verified that sessions with cwd outside CODEX_HOME do not show this duplicate.

The two injected AGENTS.md sections are byte-for-byte identical.

b-y-f avatar Sep 06 '26 01:09 b-y-f

Confirmed with the app-bundled codex-cli 0.154.0-alpha.6.2 on macOS 26.6.2 arm64, using local debug prompt-input checks on 2026-09-13.

Working directory Copies of the same global policy version marker
$CODEX_HOME (the local .codex directory) 2
An ordinary project outside $CODEX_HOME 1
/private/tmp 1

The .codex task also receives global and --- project-doc --- copies of the same policy. The global policy's SHA-256 matched the deployed baseline before relying on the comparison. The duplicated policy has not been removed or replaced, and no extra instruction layer or project-instruction suppression was introduced.

This corroborates the existing self-root issue on a newer bundled CLI; it does not establish duplication across all projects. The checks inspect locally assembled prompt input, not model inference. No credentials, private source material, local policy text, machine-specific home paths, or session transcripts are attached.

Reztahla avatar Sep 13 '26 20:09 Reztahla

Additional source-level observation against codex-rs/core/src/agents_md.rs on main inspected 2026-09-14 (main identity read during this check: 3abbf9fe2c6b6910e9de61f6a0c5bb468f74b5c8). This is source inspection, not a compiled regression pass.

load_project_instructions initializes the host user instructions, then appends every project entry returned by read_agents_md, without comparing the source of the host instruction. read_agents_md also consumes the project byte budget before those entries reach the assembler.

A correction should therefore avoid both duplicate injection and premature budget consumption. Dropping the duplicate only after discovery has applied the byte budget can still truncate or omit a later distinct project instruction. Preserve environment identity: matching path strings in a remote environment need not refer to the host file. Distinct files with equal text must remain distinct, and override-file precedence must remain intact.

Suggested acceptance cases: (1) local CODEX_HOME equals project root; (2) that root plus a child project instruction under a tight budget; (3) distinct global/project files with identical contents; (4) AGENTS.override.md precedence; (5) remote same-looking paths; (6) documented symlink/canonical identity behavior. The full assembled provenance should be checked, not only a text marker count.

The installed app-bundled CLI remains 0.154.0-alpha.6.2; PATH CLI and latest stable release are both 0.154.0. No custom runtime was installed and no global policy was removed or project discovery suppressed. Following the current contribution policy, this is analysis for maintainers rather than an unsolicited PR.

Reztahla avatar Sep 14 '26 02:09 Reztahla

Source-level recheck on 2026-09-22: this still appears consistent with current main at 94174e44cbc54cece45f6052328ca0c2cd7a8a2a.

The global loader in codex-rs/codex-home/src/instructions/mod.rs still loads the CODEX_HOME instruction source independently, while codex-rs/core/src/agents_md.rs appends project-discovered instruction entries without a cross-source canonical-path deduplication step.

I added an independent deterministic conformance fixture in codex-scope PR #13: https://github.com/kodlbegiko/codex-scope/pull/13

The fixture asserts that when CODEX_HOME == project root == cwd, the same AGENTS.md path currently appears twice in the modeled active sources (global + project), and CI is green with that expectation. The fixture is explicitly marked implementation parity / current discrepancy, not desired behavior, so an upstream dedupe change is intended to produce behavior drift and require an evidence update.

Important limitation: this is a source-level recheck against current main plus the existing 0.144.6 reproduction in this issue; I did not run a fresh current Codex binary reproduction in this environment.

kodlbegiko avatar Sep 22 '26 08:09 kodlbegiko

Confirmed on macOS Codex desktop (bundled codex-cli 0.155.0-alpha.9.2) with cwd under $CODEX_HOME/.chatgpt-projects/<project-id> and a .git marker at $CODEX_HOME.

Read-only reproduction using codex debug prompt-input DEDUPE_CHECK in that cwd: the model-visible instruction text contains two # Global Codex Rules (RU Legal Workbench) headers and one --- project-doc --- separator. The second block is an exact 23,099-character prefix of $CODEX_HOME/AGENTS.md, then truncates at the project-doc byte budget. The separate cwd AGENTS.md has unique project instructions, but its # ChatGPT project context header is absent from the assembled prompt. The global file is 109,949 bytes; the cwd file is 11,427 bytes.

This is the same canonical-file duplication in a nested cwd. Deduplicating by canonical source path before consuming the project-doc budget should both save context and allow the actual project instructions to load. A global project_doc_max_bytes=0 is unsuitable because it also suppresses those project instructions. No user rule text or personal path is included here.

nikkon6-beep avatar Sep 23 '26 10:09 nikkon6-beep