AI 学习指南
课程章节
本文目录
项目配置7

AGENTS.md 完整教程:为 Codex 建立持久项目规则

理解 AGENTS.md 的发现顺序、覆盖规则、目录级配置和实用模板,让 Codex 持续遵循项目规范。

进阶 20 分钟 15 2026-07-14 更新

AGENTS.md 完整教程:为 Codex 建立持久项目规则

AGENTS.md 是 Codex 的项目指导文件。它适合保存每次进入仓库都应遵守的规则,例如:

  • 安装、构建和测试命令;
  • 代码风格和目录约定;
  • 禁止修改的文件;
  • Review 标准;
  • 依赖管理策略;
  • 子目录特殊规则。

Codex 在开始工作前读取这些文件,从而减少每个 Prompt 重复描述项目规范。

1. 创建第一个 AGENTS.md

在 Codex 交互界面中:

/init

也可以手工在仓库根目录创建:

touch AGENTS.md

最小示例:

# AGENTS.md

## Commands
- Install: `pnpm install`
- Dev: `pnpm dev`
- Lint: `pnpm lint`
- Typecheck: `pnpm typecheck`
- Test: `pnpm test`

## Working rules
- Keep changes focused on the requested task.
- Do not add production dependencies without approval.
- Do not edit generated files.
- Run the smallest relevant test before reporting completion.

2. Codex 如何发现 AGENTS.md

官方文档描述的核心顺序:

2.1 全局层

Codex Home 默认是:

~/.codex

它会优先读取:

~/.codex/AGENTS.override.md

如果不存在,再读取:

~/.codex/AGENTS.md

全局层只使用第一个非空文件。

2.2 项目层

从项目根目录开始,一直向当前工作目录向下查找。每一级目录优先顺序通常是:

  1. AGENTS.override.md
  2. AGENTS.md
  3. 配置的 fallback 文件名。

每个目录最多取一个文件。

2.3 合并和覆盖

Codex 按“从根目录到当前目录”的顺序拼接指导。离当前目录更近的内容出现在后面,因此可以覆盖上层规则。

示例:

repo/
├── AGENTS.md
├── apps/
│   ├── web/
│   │   └── AGENTS.md
│   └── mobile/
│       └── AGENTS.override.md
└── packages/

apps/mobile 启动时,会读取根规则,再读取 mobile 的 override。

3. 全局 AGENTS.md 应写什么

全局文件保存个人长期偏好,不要写某个项目专属内容。

# ~/.codex/AGENTS.md

## Personal working agreements
- Prefer concise implementation summaries.
- Ask before adding production dependencies.
- Do not run destructive Git commands.
- Report commands that were actually executed.
- Mark unverified claims explicitly.

创建:

mkdir -p ~/.codex
$EDITOR ~/.codex/AGENTS.md

验证:

codex --ask-for-approval never "Summarize the current instructions."

注意:never 只改变批准策略,并不自动给予沙箱外权限。

4. 仓库根 AGENTS.md 推荐结构

# Repository guidance

## Project overview
- This is a Nuxt 3 content website.
- Main app: `apps/web`.
- Shared UI: `packages/ui`.

## Commands
- Install: `pnpm install`
- Dev: `pnpm --filter web dev`
- Lint: `pnpm lint`
- Typecheck: `pnpm typecheck`
- Unit tests: `pnpm test`
- E2E: `pnpm test:e2e`

## Architecture
- Route pages belong in `apps/web/pages`.
- Reusable business logic belongs in `apps/web/composables`.
- Generic UI belongs in `packages/ui`.

## Code style
- Use TypeScript strict mode.
- Prefer named exports for shared utilities.
- Keep public functions documented.

## Constraints
- Do not edit generated files in `.nuxt/` or `dist/`.
- Do not change public API response shapes without approval.
- Do not add dependencies when an existing utility is sufficient.

## Verification
- Run focused tests during implementation.
- Before completion run lint and typecheck for changed packages.
- Report any checks that could not run.

5. 目录级规则

Flutter 项目可能同时有客户端和后端:

repo/
├── AGENTS.md
├── app/
│   └── AGENTS.md
└── server/
    └── AGENTS.md

app/AGENTS.md

## Flutter app rules
- Use Riverpod for new state management.
- Use go_router for navigation.
- Run `flutter analyze` after Dart changes.
- Run the nearest widget/unit tests.
- Do not modify generated `*.g.dart` files directly.

server/AGENTS.md

## Server rules
- Use Spring Boot conventions already present in the module.
- Keep controllers thin; business logic belongs in services.
- Run `./gradlew test` for affected modules.
- Database changes require a migration and rollback notes.

6. AGENTS.override.md 的用途

Override 适合:

  • 某目录完全不同的技术栈;
  • 临时全局规则;
  • 高风险模块的更严格要求;
  • 本地实验覆盖共享规则。

例如支付目录:

# services/payments/AGENTS.override.md

## Payments rules
- Never change idempotency behavior without a regression test.
- Do not rotate or print secrets.
- Use `make test-payments` instead of the repository-wide test command.
- Any schema migration must include a rollback plan.

同一目录存在 override 时,普通 AGENTS.md 会被忽略。

7. 应该写“规则”,不要写“百科”

适合:

- Run `pnpm test -- auth` after changing authentication code.

不适合:

- Authentication is a process by which a system identifies a user...

AGENTS.md 应短、明确、可执行。大段架构文档应放在 docs/,然后在规则中告诉 Codex何时阅读:

- Before changing the payment state machine, read `docs/payments/state-machine.md`.

8. 把重复错误转化为规则

当 Codex反复出现以下问题,可以加入规则:

  • 总是使用错误包管理器;
  • 总是修改生成文件;
  • 总是忘记运行类型检查;
  • 总是在 Controller 中写业务逻辑;
  • 总是新增无必要依赖;
  • 总是忽略某个子模块测试。

例如:

## Common mistakes to avoid
- The repository uses pnpm, not npm or yarn.
- `src/api/generated` is generated; edit the OpenAPI source instead.
- Do not run the full E2E suite during small UI iterations; run the focused spec first.

9. 记录命令时避免过时

不要只写:

- Run tests before completion.

应写真实命令:

- Unit tests: `pnpm test`
- Focused test: `pnpm vitest run path/to/file.test.ts`
- Typecheck: `pnpm typecheck`

当命令变化时,更新 AGENTS.md 应成为项目维护的一部分。

10. Monorepo 模板

# AGENTS.md

## Repository layout
- `apps/web`: customer-facing Nuxt app
- `apps/admin`: internal React app
- `services/api`: FastAPI service
- `packages/ui`: shared design system

## Package manager
- Use `pnpm` only.
- Run commands from repository root unless a section says otherwise.

## Scope
- Avoid cross-package changes unless required by the task.
- If changing a shared package, identify all affected consumers.

## Verification
- Web: `pnpm --filter web test`
- Admin: `pnpm --filter admin test`
- API: `uv run pytest`
- Shared typecheck: `pnpm typecheck`

## Generated content
- Do not edit `openapi/generated` or `*.g.dart` directly.

子目录继续覆盖:

# services/api/AGENTS.md

- Use `uv` for Python dependencies.
- Keep endpoints thin and put domain logic in `app/services`.
- New endpoints require schema tests and authorization tests.

11. 验证 Codex加载了哪些规则

启动对应目录:

codex --cd services/api --ask-for-approval never \
  "List the instruction sources you loaded and summarize the effective rules."

检查结果是否包含:

  • 全局规则;
  • 仓库根规则;
  • 当前目录规则;
  • override 是否正确生效。

如果修改 AGENTS.md 后当前 TUI 没有重新读取,重新启动会话,因为官方说明发现过程通常在每次运行启动时进行。

12. 大小限制与拆分

官方文档说明项目指导合并后存在大小限制,默认上限为 32 KiB。不要把所有文档复制进去。

处理方式:

  • 根目录只写通用规则;
  • 子目录写局部规则;
  • 详细背景放 docs/
  • 使用明确的阅读路由;
  • 删除重复描述。

13. 不要写入敏感信息

禁止放入:

  • API Key;
  • 数据库密码;
  • 生产地址和内部凭据;
  • 私密客户数据;
  • 可直接执行的危险凭据操作。

AGENTS.md 通常会跟随仓库提交,应按代码文档处理。

14. 推荐维护流程

  1. 初次创建只写命令、目录和关键限制;
  2. 使用 Codex完成几次任务;
  3. 把重复错误加入规则;
  4. 定期删除已经过时或重复的规则;
  5. PR Review 中把高频反馈固化;
  6. 复杂流程再升级为 Skill,而不是无限扩大 AGENTS.md

官方参考

资料核对日期:2026-07-14。

继续学习

$ start project✓ context loaded✓ task complete

claude-code

入门

Claude Code 从入门到实战

基于 Anthropic 官方文档,从安装、项目上下文和权限配置,到调试、测试与交付的完整入门课程。

78 分钟开始学习
$ start project✓ context loaded✓ task complete

cursor

开发

Cursor AI 开发指南

深入介绍 Cursor 的智能补全、Agent、规则配置和代码库理解能力。

36 分钟开始学习