Files
superlist/AGENTS.md
2026-10-15 15:30:22 -03:00

141 lines
8.2 KiB
Markdown

# AGENTS.md
## Status
Scaffold is in place: Turborepo monorepo with `apps/api`, `apps/web` and `packages/shared`.
No CI, no database migrations and no product features yet.
## Stack (source of truth: `STACK.md`)
- Monorepo orchestrated with **Turborepo**
- Backend: **Bun** + **Hono**, **Prisma** against **Postgres**, **Zod** for validation, **Pino** for logging
- Frontend: **React + Vite + TypeScript**, UI components from **shadcn/ui**
- Auth: **BetterAuth**
## Conventions
- **Package manager: Bun** — `bun install`, `bun run <script>`. Do not introduce npm/pnpm/yarn lockfiles.
- Bun is configured with `minimumReleaseAge` (see `~/.bunfig.toml`), so brand new releases are rejected during
install. If a dependency fails to resolve, pin the newest version that is at least a few days old instead of
reaching for the very latest.
- Workspaces:
- `apps/api` — Bun backend (Prisma schema + migrations live here)
- `apps/web` — React/Vite frontend
- `packages/shared` — shared code (`@superlist/shared`: Result pattern, Problem Details helpers/types)
- Verification commands, run from the repo root:
- `bun run build`
- `bun run lint` → `bun run typecheck` → `bun run test` (in that order)
- **Lint with Biome** (`biome.json` at the repo root, one config for every workspace): `bun run lint`
(per-package via turbo), `bun run lint:fix` (apply safe fixes), `bun run lint:ci` (whole repo, for CI).
- **Format with Prettier** (`bun run format`). Biome's formatter and assist are deliberately disabled in
`biome.json` so the two tools never fight each other.
- `apps/web/src/components/ui/**` is excluded from Biome's recommended preset through an `overrides` entry:
those files are vendored shadcn/ui source and get regenerated by the CLI.
- Generated files are gitignored and must never be edited by hand: `apps/web/src/routeTree.gen.ts`,
`apps/api/src/generated/`.
## Backend architecture (Hono)
Endpoints are grouped into **modules**, and each module into **features**. Example: an `alerts` module with
features `list`, `create`, `update`, `delete`:
```
apps/api/src/modules/alerts/
list/
route.ts # Hono route declaration + Zod validation + call to the use case
use-case.ts # class with a single execute() method
create/
route.ts
use-case.ts
```
- `route.ts` holds **only** the Hono route declaration, input validation (Zod), and the call to the use case's
`execute`. No business logic, no DB access.
- Business logic lives in a **use case class with a single `execute` method** (one class per feature), named
after the action: `ListAlerts`, `CreateAlert`. Dependencies (e.g. the Prisma client) are injected through the
constructor and instantiated in the route.
- Use cases return the **Result** pattern (`Ok` / `Failure` from `@superlist/shared`). Never throw for expected
outcomes; reserve exceptions for truly unexpected failures.
- Failures are Problem Details payloads built with the `problems` factories from `@superlist/shared`
(`problems.notFound(...)`, `problems.validation(...)`, `problems.conflict(...)`, …).
- **Routes never check `result.ok` by hand**: every route ends with `resultJson(c, result)`, which serializes
`Ok` as JSON (200 by default, override with `{ status: 201 }`) and maps `Failure` to the matching RFC 9457
`application/problem+json` response.
```ts
// apps/api/src/modules/alerts/list/route.ts
import { validate } from '@/lib/validate'
import { resultJson } from '@/lib/result-json'
import { AlertQuerySchema } from './schema'
import { ListAlerts } from './use-case'
export const listAlertsRoute = new Hono<AppEnv>().get(
'/',
validate.query(AlertQuerySchema),
async (c) => {
const query = c.req.valid('query') // typed straight from the schema, no cast
const result = await new ListAlerts(prisma).execute(query)
return resultJson(c, result)
},
)
```
- Validation always goes through `validate.*` (`validate.query`, `validate.json`, `validate.param`,
`validate.header`, `validate.cookie`, `validate.form`). It answers invalid input with a 422 Problem Details
payload containing per-field `errors`.
- **All responses follow RFC 9457 (Problem Details)** — errors use the `application/problem+json` media type
with `type`, `title`, `status`, `detail` fields. Problem builders live in
`packages/shared/src/problem-details.ts`; the HTTP helpers in `apps/api/src/lib/`.
- Zod is the only validation library. Define schemas next to the feature that uses them; share them through
`@superlist/shared` only when more than one module needs them.
- **Logging: Pino** — one shared logger in `apps/api/src/lib/logger.ts`, reachable from handlers as
`c.get('logger')`; never `console.log` in the backend.
- Unexpected throws are turned into Problem Details by `app.onError` in `apps/api/src/app.ts`.
- Prisma: after changing the schema, run `bun run db:migrate` (from `apps/api`, or `bun run db:migrate` at the
root) and commit the generated migration — never edit migration files by hand.
## Frontend architecture (React + Vite)
```
apps/web/src/
routes/ # file-based TanStack Router routes; routeTree.gen.ts is generated
components/ui/ # shadcn/ui components (checked in, editable source)
components/layout # app shell: sidebar, header, user menu
hooks/ # query hooks (e.g. use-health.ts)
lib/ # api-client, auth-client, query-client, utils
```
- **Routing: TanStack Router with file-based routes.** Add a file under `src/routes` and the route tree is
regenerated (`bun run routes:generate`, also run by `dev`, `build` and `typecheck`). Never edit
`routeTree.gen.ts`.
- `(auth)/sign-in.tsx` — public routes.
- `_authenticated/route.tsx` — pathless layout for signed-in pages; it guards on the BetterAuth session and
redirects to `/sign-in` when there is none.
- **Server state: TanStack Query only.** Never call `fetch` from a component; add a hook under `src/hooks` that
exports `queryOptions` (`<entity>QueryOptions()`) plus a `use<Entity>()` wrapper, and use `apiFetch` from
`src/lib/api-client.ts` as the `queryFn`. Exporting the options lets route loaders prefetch with
`context.queryClient.ensureQueryData(...)` (the client is passed through the router context from `main.tsx`).
- **Forms: React Hook Form + Zod** (`zodResolver`). Render errors with the shadcn `Field`/`FieldError`
components — this registry version ships `field` instead of the older `form` component.
- **shadcn/ui**: components live in `src/components/ui` and are source, not generated output — edit them
directly. Add new ones with
`cd apps/web && ../../node_modules/.bin/shadcn add <component>` (this registry needs the `@shadcn/` prefix for
some items, e.g. `@shadcn/field`).
- `cn` comes from the `cn` package; `src/lib/utils.ts` re-exports it for local imports.
- Prefer small feature components over one large route file.
- Biome does not flag the `Route` export of route files, so route files stay clean; only the vendored
`components/ui` files are exempted from the recommended preset.
<!-- BEGIN:turborepo-agent-rules -->
# This is NOT the Turborepo you know
Turborepo configuration, task behavior, and CLI commands can vary between installed versions and may differ from your training data. Resolve the `turbo` package from this file's directory or relevant workspace; in monorepos, it may not be visible from the repository root. For example, run `node -p "require.resolve('turbo/package.json')"` from a workspace that depends on `turbo`.
Read `docs/README.md` inside that installed package first, then read the relevant pages from its `docs/` directory before changing Turborepo configuration or commands. Heed deprecation notices. These bundled docs match the installed package version and are available without network access.
This block is written and re-added by `turbo` before repository-scoped commands when an AI agent is detected. In the Turborepo source repository, its template is defined in `crates/turborepo-cli/src/cli/agent_guidance.rs`. Removing the managed block while updates are enabled means a later qualifying invocation will add it again. Set `"agentGuidance": false` in the root `turbo.json` or `turbo.jsonc` to opt out; this does not remove an existing block. Keep the block committed with your work to avoid an uncommitted change on the next agent invocation.
<!-- END:turborepo-agent-rules -->