Plantilla de AGENTS.md / CLAUDE.md (estándar enterprise)
Copia el bloque de abajo en la raíz de cualquier repo como AGENTS.md (y/o CLAUDE.md, .cursor/rules). Convierte los estándares del curso en instrucciones que la IA lee en cada tarea. Edita la sección del stack para que coincida con el proyecto.
# 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
Por qué existe cada sección
| Sección | Evita | Módulo |
|---|---|---|
| Working rules | "Terminado" sin verificar, scope creep, paquetes alucinados, borrados al estilo Replit | M01, M11 |
| Security | Fallos de OWASP A01 a A10 | M05 |
| Data | Corrupción, condiciones de carrera, consultas lentas, migraciones que provocan caídas | M03, M10 |
| Reliability | Fallos en cascada, cobros dobles, trabajo perdido | M07, M08 |
| AI features | Secuestro de objetivos, mal uso de herramientas, costos desbocados | M06 |
| Observability | Código generado por IA imposible de depurar | M12 |