diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..68add0f --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,221 @@ +# AGENTS.md + +Instructions for AI coding tools in this repo (Northstar CRM). Read this file +before editing. It follows the lab guides (`docs/lab48/` through `docs/lab51/`, +`STARTER.md`, `README.md`) and [docs/ai-usage-plan.md](docs/ai-usage-plan.md). + +When a lab guide and the code disagree, trust the code and `README.md` for how +to run the app, and trust this file plus `docs/ai-usage-plan.md` for what you +may draft and what you must not commit. + +## Product + +Agents sign in, look up a customer, and record interactions (calls, emails, +notes, meetings). PostgreSQL is the source of truth. After a successful +commit, the API publishes a Kafka event. Admins can close and reopen a +customer. Angular hides buttons; the API enforces auth. + +Non-goals: Oracle, React, SOAP, Bitbucket Pipelines, OpenShift. The labs use +PostgreSQL, Angular, and k3s. + +## Stack and versions + +| Layer | Version | Where | +| --- | --- | --- | +| Frontend | Angular 19.2 (`@angular/*` ^19.2.0), CLI ^19.2.27, TypeScript ~5.7.2, RxJS ~7.8.0, Zone.js ~0.15.0 | `frontend/` (`package.json`). Project name `lab50-crm-ui`, prefix `app`. | +| Node | 20 in CI (`node-version: "20"`). Local installs need Node 20+. | `.github/workflows/capstone-ci.yml` | +| Backend | Spring Boot **3.3.5**, Java **21** | `backend/pom.xml` (`spring-boot-starter-parent` 3.3.5, `java.version` 21) | +| Maven | `com.northstar:crm-backend` | `backend/pom.xml` | +| Database | PostgreSQL **16**, schema owned by Flyway (V1–V3). Hibernate validates only. Tests use Testcontainers Postgres, not H2. | `compose.yaml`, `backend/src/main/resources/db/migration/` | +| Events | Kafka image `apache/kafka:3.8.0` (single node). Topics `crm.customer.interactions.v1` and `crm.customer.lifecycle.v1`, plus `.DLT`. | `compose.yaml`, `docs/architecture.md` | +| Security | Spring Security JWT resource server, HS256, issuer `northstar-crm`, roles `AGENT` and `ADMIN`, access tokens expire in 15 minutes | `docs/lab51/security-and-observability.md` | +| Local runtime | Docker Compose. API on port **8081** with the `local` profile. UI on **4200** (`ng serve`). CORS allows only 4200 and 4201. | `README.md` | +| CI | GitHub Actions on `ubuntu-latest`. Temurin 21, Node 20, Postgres 16. | `.github/workflows/capstone-ci.yml` | +| Images | Dockerfiles copy a prebuilt JAR and `frontend/dist`. CI builds those first, then images. Deploy promotes digests; it does not rebuild. | `backend/Dockerfile`, `frontend/Dockerfile`, `docs/adrs/003-github-actions-digest-promotion.md` | +| Deploy / IaC | k3s manifests under `deploy/k3s/`. Terraform `>= 1.5.0`, `hashicorp/kubernetes` `~> 2.35`. Ansible only validates promotion inputs. | `infra/terraform/versions.tf`, `infra/ansible/site.yml`, `docs/terraform-ansible-plan.md` | + +`ng` is not required on `PATH`. Use `npx ng`. + +## Naming: `com.northstar.crm` + +- Java package root is `com.northstar.crm`. Put new types under that root: + `api`, `api.dto`, `domain`, `repo`, `service`, `messaging`, `security`, + `observability`. Do not add another root package. +- Maven groupId stays `com.northstar`. Artifact stays `crm-backend`. +- Kafka trusted packages stay `com.northstar.crm.messaging` + (`backend/src/main/resources/application.yml`). +- The API identity of a customer is the public id (`CUS-1001`), never the + database `customer_id`. +- HTTP API: `/api/v1/customers`, `/api/v1/interactions`, + `POST /api/v1/auth/login`. Send `Authorization: Bearer `. +- Correlation header: `X-Correlation-Id`. +- Customer status values: `PROSPECT`, `ACTIVE`, `CLOSED`. Interaction types: + `CALL`, `EMAIL`, `NOTE`, `MEETING`. +- Moving a customer to or from `CLOSED` is `ADMIN` only (403 for `AGENT`). +- Event records carry ids and metadata only. `CustomerInteractionRecordedV1` + must not include the interaction summary or other note text. +- Diagrams in markdown are Mermaid, not image exports. + +## Official fixtures + +Use only these synthetic fixtures in code, tests, docs, demos, commits, and +`reports/`. **No secrets or real data.** + +| Fixture | Use | +| --- | --- | +| `CUS-1001` | Amina Khan, `ACTIVE`. Flyway seed. Happy path: search, profile, create a note. | +| `CUS-1002` | Ravi Singh, `PROSPECT`. Flyway seed. Close and reopen checks. | +| `CUS-9999` | Not a customer. Expect **404** `Unknown customer: CUS-9999`. No row, no event. | +| `lab-request-001` | Default correlation id for lab, demo, and smoke calls. | + +`CUS-1001` and `CUS-1002` are inserted by +`backend/src/main/resources/db/migration/V1__crm_schema.sql`. `CUS-9999` is +not seeded. Do not add real people, change these seed rows, or invent extra +seed customers. +Synthetic demo users are `agent` (`AGENT`) and `admin` (`AGENT`, `ADMIN`). + +## No secrets or real data + +From [docs/ai-usage-plan.md](docs/ai-usage-plan.md) and `reports/README.md`: + +- Do not commit secrets, kubeconfig, `.env`, JWTs, `Authorization` headers, or + Secret manifests. +- Do not put passwords, tokens, cluster hostnames, or real personal data in + source, tests, docs, screenshots, or `reports/`. +- `.env` is gitignored. Only `.env.example` is committed. + `cp .env.example .env` is local only. +- Outside the `local` profile the API refuses to start without + `CRM_JWT_SECRET` (at least 32 bytes). That value, and + `CRM_AGENT_PASSWORD` / `CRM_ADMIN_PASSWORD`, come from the environment or a + k3s Secret. Never from git. +- The `local` profile (`application-local.yml`) exists so a laptop can boot. + Do not copy its values into new files, logs, PR text, or evidence. +- `scripts/smoke-crm.sh` reads credentials from the environment and must not + be changed to print them. +- Sanitize evidence before committing: strip `eyJ...` tokens and + `Authorization` headers. Projector-safe fixtures only. +- Do not apply Terraform or Ansible to prod without approval. +- Do not copy proprietary third-party code. + +## What you may draft, and what you must verify + +Allowed ([docs/ai-usage-plan.md](docs/ai-usage-plan.md)): + +- Boilerplate (DTOs, Angular services and components) +- First drafts of tests +- Sketches of Terraform, workflows, and k8s manifests +- Explaining errors and unfamiliar code + +Verify before you finish: + +- Angular changes compile (`npx ng build`) and match the backend DTOs. +- Flyway SQL is PostgreSQL (`UUID`, `TIMESTAMPTZ`, `BIGSERIAL`), not Oracle + (`NUMBER`, `VARCHAR2`). Add a new `V*.sql`. Do not edit an applied migration. +- No secrets were introduced. + +## Build and test + +Needs Docker (Docker Desktop on a Mac), JDK 21, Maven, and Node 20+. +Integration tests start Postgres and Kafka with Testcontainers, so Docker must +be running. Surefire pins the Docker API to `1.41` in `backend/pom.xml`; leave +that property in place. Surefire runs both `*Test.java` and `*IT.java`. + +```bash +cp .env.example .env # local only, never commit .env +docker compose up -d # Postgres 16 on :5432, Kafka 3.8.0 on :9092 +``` + +Backend, from `backend/` (or `mvn -f backend/pom.xml` from the repo root): + +```bash +mvn -B test # Lab 49 smoke +mvn -B clean verify # Lab 50 and CI. Unit, MockMvc, security, JPA, API, Kafka +SPRING_PROFILES_ACTIVE=local SERVER_PORT=8081 mvn spring-boot:run + # API on http://localhost:8081 +``` + +Frontend, from `frontend/`: + +```bash +npm ci +npx ng build +npx ng test --watch=false --browsers=ChromeHeadless +npx ng serve # http://localhost:4200 +``` + +Lab 50 checklist (Docker running): + +```bash +cp .env.example .env +docker compose up -d +cd backend && mvn -B clean verify +cd ../frontend && npm ci && npx ng build +npx ng test --watch=false --browsers=ChromeHeadless +``` + +On a Mac, headless Chrome may need +`CHROME_BIN='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'`. + +CI (`.github/workflows/capstone-ci.yml`, `ubuntu-latest`) runs the same +checks: frontend `npm ci`, `npx ng test --watch=false --browsers=ChromeHeadless`, +`npx ng build --configuration=production`; backend `mvn -B clean verify`. + +Selenium (`frontend/e2e/`) is not part of `ng test`, `mvn test`, or default CI. +It needs Compose, the API on 8081, and Chrome. The e2e proxy defaults to +8080, so set the target: + +```bash +# API already on 8081 with the local profile +cd frontend +CRM_E2E_API_TARGET=http://localhost:8081 npm run e2e:serve # :4300 +npm run e2e +``` + +Laptop smoke against a backend that is already up. Export `CRM_SMOKE_PASSWORD` +(or `CRM_SMOKE_TOKEN`) in the shell first. The script never prints it. The +local-profile user is documented in `README.md`; do not paste the password +into git. + +```bash +CRM_BASE_URL=http://localhost:8081 CRM_SMOKE_SKIP_FRONTEND=true \ + bash scripts/smoke-crm.sh +``` + +Expect `Smoke passed for CUS-1001 with correlation lab-request-001`. + +Images only copy build output. Before `docker build`, run `mvn clean verify` +in `backend/` and `npx ng build` in `frontend/`. + +Windows uses the same steps with PowerShell env vars (`$env:NAME = "value"`). +See `README.md`. + +Stop Compose with `docker compose down` (`-v` drops the database). + +## Where code goes + +```text +backend/src/main/java/com/northstar/crm/ API, domain, services, Kafka, security +backend/src/main/resources/db/migration/ Flyway V1–V3 (new changes = next V) +backend/src/test/java/com/northstar/crm/ *Test.java and *IT.java +frontend/src/app/core/ auth, interceptors, API URL and errors +frontend/src/app/features/ customers, interactions, login +frontend/e2e/ Selenium happy path (not default CI) +deploy/k3s/ base + dev/test/stage/prod-like overlays +docs/ architecture, ADRs, lab notes, AI plan +``` + +Angular specs live next to the file they cover (`*.spec.ts`). Selectors that +e2e uses are `data-testid` only. + +## Working on a change + +- Branch off `main`. One issue per branch. Open a PR back into `main`. +- Do not refactor unrelated code, and do not change the official fixtures, + while doing a focused task. +- Do not commit `target/`, `node_modules/`, `dist/`, `.angular/`, or `.env`. +- New schema is a new Flyway file. PostgreSQL types only. +- Publish Kafka events after the database commit. Consumers apply each + `eventId` once (`ProcessedEvent`). After the bounded retry, or immediately + for a non-retryable failure, the record goes to `.DLT`. +- Keep the "Must verify" checks in `docs/ai-usage-plan.md` in mind on every + AI-assisted change: compile, PostgreSQL review, no secrets.