CDX-301a · Module 3

Conditional Rules & Context-Aware Configs

4 min read

Conditional rules adapt AGENTS.md behavior based on context — the current directory, the active branch, the time of day, or the developer's role. While AGENTS.md itself is static text, you can achieve conditional behavior through directory scoping (different rules per directory), override files (developer-specific rules), and CI environment detection (rules that only apply in pipeline context). The combination of these mechanisms gives you a dynamic config system built from static files.

The most powerful conditional pattern is branch-based rule switching. Your CI pipeline can copy a branch-specific AGENTS.md into place before running Codex. The main branch gets strict rules ("never skip tests, always lint"). Feature branches get relaxed rules ("allow TODO comments, skip integration tests"). Release branches get compliance rules ("all changes require security review comment"). This turns your AGENTS.md into a policy engine that adapts to your release process.

name: Codex Branch-Aware Review
on:
  pull_request:
    types: [opened, synchronize]

jobs:
  review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Select AGENTS.md by target branch
        run: |
          TARGET=${{ github.base_ref }}
          if [ -f ".codex/agents-${TARGET}.md" ]; then
            cp ".codex/agents-${TARGET}.md" AGENTS.md
            echo "Using branch-specific config: agents-${TARGET}.md"
          else
            echo "Using default AGENTS.md"
          fi
      - name: Run Codex
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: codex review --base origin/${{ github.base_ref }} --json
  1. Define branch policies List the rules that should differ by branch: main (strict), feature (relaxed), release (compliance). Write each as a separate AGENTS.md variant.
  2. Create the .codex/ directory Store branch-specific configs in .codex/agents-{branch}.md. Add a README explaining the naming convention.
  3. Wire into CI Add a CI step that copies the correct config into place based on the target branch. Log which config was selected for audit.