File size: 5,477 Bytes
9580dce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## What this is
RotNot is a gamified cognitive-fitness web app (Next.js 16 App Router + Prisma 7 + Anthropic). Six exercise categories (Chess, Deep Reading, Feynman, Logic, Critical Thinking, Skill Quests) plus an AI Debate Arena. Single-user, local-first (SQLite); also packaged as a Capacitor iOS/Android app.
## Commands
```bash
npm run dev # next dev (default port 3000; playwright config expects :3101)
npm run build # next build
npm test # vitest run (~2700 tests across ~82 files)
npm run test:watch # vitest in watch mode
npm run typecheck # tsc --noEmit
npm run lint # eslint
# Single test file or pattern
npx vitest run src/lib/__tests__/xp.test.ts
npx vitest run -t "calculateXpGain"
# Database (Prisma 7)
npx prisma migrate deploy
npx prisma db seed # creates Player One, 6 category skills, 55 skill tree nodes
npx prisma migrate dev --name <name>
```
`.env` requires `ANTHROPIC_API_KEY`. `LICHESS_TOKEN` and `CHESSCOM_USERNAME` are optional but enable chess puzzles / Stockfish bot play / profile ratings.
## Architecture quirks worth knowing
**Prisma 7 + libsql adapter.** `prisma/schema.prisma` has no `url` in the datasource β the URL is set in the adapter (`src/lib/db.ts`) via `PrismaLibSql({ url: ... })`. Default resolves to `file:${cwd}/dev.db`. Don't add `url` back to the schema.
**Anthropic SDK.** Singleton in `src/lib/claude.ts`. Always parse Claude responses with `parseClaudeJson(text)` β it strips markdown fences that newer models sometimes wrap JSON in despite explicit prompting. For unstructured β structured conversion, use `formatToJson(rawText, schema)` (Haiku-backed second pass).
**API route pattern.** Every route in `src/app/api/**/route.ts` wraps its handler with `withApiError` from `src/lib/api-error.ts`. Throw `new ApiError(status, message)` for controlled error responses; unknown throws become a generic 500 with details only in stderr.
**Typed API client.** `src/lib/api-client.ts` mirrors every `/api/*` route as a typed function and is what UI components call (not raw `fetch`). It supports `setApiBaseUrl()` so the same client works from a Capacitor app pointing at a remote host. When adding a new API route, add a matching method here.
**Pure XP/progression logic.** `src/lib/xp.ts` and `src/lib/progression.ts` have **no DB side effects** β they take inputs and return values. All XP math, level-ups, crit rolls, and streak logic flows through them and is fully tested. Don't reach for the DB inside these files.
**Skill tree multipliers.** Before any submit route awards XP, it calls `getMultiplierPercent(userId, category)` from `src/lib/multiplier.ts` (parses `+N% Category XP` from unlocked SkillTreeNodes) and passes the result into `calculateXpGain()`. All six exercise submit routes follow this order: fetch user β resolve multiplier β calculate XP β persist. Keep this order when editing.
**Vitest globals.** `vitest.config.ts` has `globals: true` β don't import `describe`/`it`/`expect`. Path alias `@/` β `src/`. Tests live in colocated `__tests__/` dirs (e.g. `src/lib/__tests__/`, `src/lib/puzzles/__tests__/`).
**Grounded teaching (Feynman).** The teach flow in `src/lib/sources/curate.ts` pulls from Wikipedia, Wikidata, and OpenStax, persists them via the `TopicSource` model, then generates citations through Claude with a confidence score (`high` β₯3 sources, `moderate` =2, `low` =1). Cached in `TeachingCache`. When changing the teach pipeline, check the `confidence` thresholds in `curate.ts` against the tests in `src/lib/sources/__tests__/curate.test.ts`.
**AI Debate Arena.** `src/lib/debate/` (core + opponents + topics + few-shot examples + judge). The judge uses few-shot calibration examples inlined into the round-evaluation prompt β when adding examples, the data-integrity tests enforce coverage across dimensions and score values. LangSmith tracing in `src/lib/langsmith.ts` is gated by env vars; the reflector (`src/lib/judge/reflector.ts`) is a separate evaluation loop.
**Single user, no auth.** Everything is keyed by `userId` but seed always creates one user (id 1). Don't add auth scaffolding speculatively.
**Capacitor.** `capacitor.config.ts` + `src/lib/native/` wraps haptics, push notifications, and local notifications. Native bridge calls are no-ops on web; UI components don't need branches.
**Playwright.** `playwright.config.ts` points at `http://192.168.1.166:3101` (LAN address). E2E specs live in `e2e/`. Playwright is not in `package.json` β install separately if running e2e locally.
## When adding a new exercise category
1. Pure generator/validator under `src/lib/puzzles/<name>.ts` (or domain equivalent) with tests in `__tests__/`.
2. `/api/<name>/generate` + `/api/<name>/submit` routes wrapped in `withApiError`.
3. Submit route: fetch user β `getMultiplierPercent()` β `calculateXpGain()` β persist Activity + update User/CategorySkill.
4. Add typed methods to `src/lib/api-client.ts`.
5. UI page under `src/app/<name>/page.tsx`; reuse `XpResults` + `LevelUpOverlay` from `src/components/shared/`.
6. Update seed if new skill tree nodes are required.
## `.planning/STATE.md`
A running development log β useful for "has X already been done" lookups, but it's enormous (>60k tokens). Don't read it whole; grep for the specific feature or commit.
|