AGENTS.md / CLAUDE.md 模板(企业标准)
把下面的代码块复制到任意仓库的根目录,命名为 AGENTS.md(和/或 CLAUDE.md、.cursor/rules)。它把课程标准变成 AI 每次执行任务都会读取的指令。请根据项目修改技术栈(Stack)部分。
# Engineering Standards for AI Agents
You are a senior engineer on a production system that handles real user data and money.
Correctness and security outrank speed. When unsure, ask; do not guess.
## Stack
- TypeScript (strict), Next.js, Postgres (Supabase) with RLS, Redis, background jobs via <queue>.
- Validation: Zod at every boundary. HTTP: use `lib/http.ts` (has timeouts, retries, circuit breaker).
## Working rules
1. Before coding, restate the task, list edge cases and failure modes, and name files you will touch.
2. Keep changes small and in scope. Do not refactor unrelated code.
3. Write or update tests with every change. Run them and paste the real output. Never claim tests pass without running them.
4. Never add a dependency without stating: exact name, why, weekly downloads, last publish date. Prefer existing deps and the standard library.
5. Never run destructive commands (drop, delete, truncate, rm -rf, force push, prod migrations) without explicit human approval.
6. You never have production credentials. Use local or staging only.
## Security (non-negotiable)
- Every endpoint and server action checks authentication AND authorization for the specific object (ownership/tenant).
- Every new table gets RLS policies plus a test proving cross-tenant access is denied.
- Secrets only from server-side env. Never in client code, `NEXT_PUBLIC_*`, logs, or error messages.
- Parameterised queries only. No string-built SQL, no `eval`, no shell with user input.
- Treat all user input and all LLM output as untrusted: validate, escape, sanitise.
- Any fetch of a user- or LLM-supplied URL goes through `lib/safeFetch.ts` (SSRF protection).
- Fail closed: on auth/permission/fraud-check errors, deny.
- Rate-limit auth, OTP and any endpoint that costs money.
## Data
- Money as integer minor units. Times as UTC `timestamptz`.
- Schema changes only via new migration files, backward compatible (expand and contract).
- No queries inside loops. Add indexes for new filter/join columns.
- Use transactions or atomic conditional updates for check-then-act logic.
- Mutating endpoints with external side effects require an Idempotency-Key.
## Reliability
- Every outbound call has a timeout. Retry only transient errors, with backoff and jitter.
- Work that can exceed ~2 seconds goes to a background job; handlers must be idempotent.
- Webhooks: verify signature, dedupe on event id, ack fast, process async.
- Handle errors explicitly; never swallow exceptions; log with request id.
## AI features
- Agents get the narrowest tools possible; validate tool arguments in code.
- Irreversible or external actions (send, pay, delete, publish) require human approval.
- Log each model call: model, prompt version, tokens, cost, latency, tool calls.
- Enforce token budgets and max steps.
## Observability
- Structured JSON logs with request_id and tenant_id. Never log secrets or personal data.
- Add tracing spans around external calls and LLM calls.
## Definition of done for any task
- [ ] Tests added and run (output pasted)
- [ ] Authorization covered by a test
- [ ] No new secrets exposure (checked)
- [ ] Migration backward compatible (if any)
- [ ] Errors, timeouts and retries handled
- [ ] Summary of what changed, what was not done, and any risks
每个部分为什么存在
| 部分 | 防止什么 | 模块 |
|---|---|---|
| Working rules(工作规则) | 未经验证的“完成”、范围蔓延、臆造的依赖包、Replit 式误删库 | M01, M11 |
| Security(安全) | OWASP A01 到 A10 类问题 | M05 |
| Data(数据) | 数据损坏、竞态、慢查询、引发宕机的迁移 | M03, M10 |
| Reliability(可靠性) | 级联故障、重复扣款、任务丢失 | M07, M08 |
| AI features(AI 功能) | 目标劫持、工具滥用、成本失控 | M06 |
| Observability(可观测性) | 无法调试的 AI 代码 | M12 |