Templat AGENTS.md / CLAUDE.md (Standard Enterprise)

Salin blok di bawah ke akar mana-mana repo sebagai AGENTS.md (dan/atau CLAUDE.md, .cursor/rules). Ia menukar standard kursus ini menjadi arahan yang dibaca oleh AI pada setiap tugasan. Sunting bahagian stack supaya sepadan dengan projek anda.

# 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

Mengapa setiap bahagian wujud

Bahagian Mencegah Modul
Working rules "Siap" yang tidak disahkan, skop melebar, pakej yang dihalusinasi, pemadaman ala Replit M01, M11
Security Kegagalan OWASP A01 hingga A10 M05
Data Data rosak, race condition, query perlahan, migration yang menyebabkan gangguan M03, M10
Reliability Kegagalan berantai, caj berganda, kerja yang hilang M07, M08
AI features Rampasan matlamat, salah guna alat, kos yang tidak terkawal M06
Observability Kod AI yang tidak boleh di-debug M12