Architecture
Rematch is a monorepo: a Rust backend that does the heavy work, a Next.js web app that shows it, and a small Node sidecar for the optional AI coach. Postgres holds everything, including the job queue. Every choice below has a decision record; the decision log has all of them. This page is the map of the code. How it works is the algorithm, stage by stage, with its constants.
From a game to advice
Section titled “From a game to advice”- Import. The Rust worker fetches your public chess.com games by username, through a rate-limited client with a circuit breaker, and keeps them in sync every 30 minutes. Requests identify the project and never carry an email address (0040, 0043, 0076).
- Queue. Jobs live in Postgres (
FOR UPDATE SKIP LOCKED, leases, retries, dead letters). A tiered scheduler decides what to analyse first and shares the engines fairly between players (0032, ADR-R2-16). - Analyse. A pool of checksum-pinned Stockfish 19 engines evaluates every position under a fixed protocol, with a shared evaluation cache. Enrichment adds MultiPV lines on the critical moves, threat probes, Syzygy endgame tables when installed and, optionally, Maia’s estimate of how hard a move was to find at your rating (0007, ADR-R2-14, ADR-R2-13).
- Profile. The profile engine turns analysed moves into features, twelve weakness families (W1 to W12), rates per chance with 80% intervals, a timeline of change points, KPIs, area scores and opening verdicts. Peers are the chess.com opponents you have faced, rated within 150 points of you; a rating-band reference built from the open Lichess game database is the opt-in alternative (ADR-R2-1, ADR-R2-4, 0080, ADR-R2-11).
- Coaching input. One versioned JSON document per account and time class carries every fact the coach may cite. It is stored as a snapshot per profile build, so a report can always be traced to its input (ADR-R2-2, 0086).
- Coach. Rust builds the prompts and sends them to the AI sidecar, which runs Claude through the Agent SDK. What comes back is validated paragraph by paragraph against the coaching input before anything is stored or streamed (0060, 0062, 0064).
- Show. The web app reads Postgres directly through read models, gets live updates over server-sent events fed by
LISTEN/NOTIFY, and draws everything with one design system (0034, ADR-WEB-8, ADR-WEB-7).
The repository
Section titled “The repository”| Path | What it is |
|---|---|
backend/ |
The Cargo workspace: worker, analysis, engine, enrich, queue, scheduler, platforms, profile, profile-core, coaching-input, ai, db, events, refdata, lichess-db, cli, chess-core, common and testkit. Built and tested only in Docker. |
apps/web/ |
The Next.js app (App Router), with Better Auth, the read models and the API routes. |
packages/ui/ |
@ci/ui: the board, annotations, eval graph, charts, stats, motion, sounds and the design tokens. DESIGN.md is the law for every pixel. |
packages/db/, packages/chess-core/ |
The web’s database client and generated schema types, and the shared TypeScript chess helpers. |
services/ai-sidecar/ |
The Node service that runs Claude through the Agent SDK. It holds no business rules. |
services/maia/ |
The optional Maia-2 difficulty service (Python). |
apps/worker/ |
The earlier TypeScript worker, the reference the Rust port was proven against (0038), and still the image CI runs the TypeScript gate in (0067). |
db/migrations/ |
Plain SQL migrations, applied by sqlx: the schema’s source of truth (0033). |
docs/ |
The decision log, the research and the plans. |
site/ |
This site: Astro and Starlight, built from the same tokens and fonts as the app. |
Running pieces
Section titled “Running pieces”setup.sh starts Postgres 17, Redis, Mailpit, the web app, the Rust worker and the AI sidecar (docker compose up -d --wait postgres redis mailpit web ai-sidecar rust-worker); a plain docker compose up also starts the development containers. The sidecar has no credential until you add one, and the worker then runs computed-only. Maia is an opt-in overlay. The base Compose file publishes no host ports, so several projects can run side by side; .env.example opts the main one into ports 3100 (web), 48025 (Mailpit), 55432 (Postgres) and 56379 (Redis) (0023, 0067).
Why Rust
Section titled “Why Rust”The backend began in TypeScript. Analysis is CPU-bound and runs for every move of every game, so I moved the whole backend to Rust for efficiency, and the port had to match the TypeScript worker’s analysis exactly before it replaced it (0030, 0038). The web stays in TypeScript.