Files
adamfandClaude Opus 5.5 cae5ec8e5a
Test and publish the site / test (push) Successful in 2m15s
Test and publish the site / publish (push) Successful in 9m5s
Keep site jobs across restarts, meter Claude spend, harden admin routes
- internal/jobs replaces explain.Jobs: one registry for every kind of
  background job, with records in DATA_DIR/jobs. A job's draft is written
  before its commit, so a failed commit can be saved again (POST
  /jobs/<kind>/<key>/save) and a restart mid-commit resumes it, without
  paying Claude twice.
- Graceful shutdown: SIGTERM stops new jobs and waits SHUTDOWN_GRACE for
  running ones while still serving the pages that poll them.
- internal/usage records every Claude call's tokens and estimated cost in
  DATA_DIR/usage.jsonl; CLAUDE_MONTHLY_BUDGET refuses new jobs (429) once a
  month reaches it. The stats page shows the admin the spend.
- auth.SameOrigin refuses cross-origin writes to admin routes.
- The LeetCode importer runs the clone's script without the server's
  secrets in its environment.
- A sync that leaves HEAD where it was skips the re-index.
- docs_test.go checks README's route table against the routes, and the CLI
  page against the root README's commands.
- The repo is going private: Gitea links on the site are admin-only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-23 22:10:01 +01:00

37 lines
4.9 KiB
Markdown

# CLAUDE.md
Guidance for Claude Code in `site/`. README.md has the full picture (routes, git sync, deployment).
## Commands
```
make check # go vet ./... && go test ./..., then a vite build of web/ into a temp dir
make dev # docker compose up --build: this checkout's problems, AUTH_MODE=dev, GIT_PUSH=false
cd web && npm run dev # SPA hot reload; proxies /usaco/api to localhost:8080
```
## Architecture
One Go binary (`cmd/usacosite`) that serves a REST API and the built Vue SPA, all under `/usaco`. It is deployed as one container inside the `web_server` stack, behind its nginx. It has the same shape as `../job-tracker`, from which `internal/auth` and `handlers.SPA` were copied.
- **Git is the only store** for content. `internal/repo` owns the clone and serializes every git command. A sync is fetch + hard reset (the remote wins). `Mutate` does sync, edit, commit and push, and replays the edit once if the push is rejected. Its `onChange` callback (`Server.Reload`) rebuilds the index under the same lock, when HEAD moved. Beside the clone, `DATA_DIR` holds only operational state: job records (`jobs/`) and the Claude spend ledger (`usage.jsonl`).
- **The index is an immutable snapshot**, swapped in through an `atomic.Pointer`. Handlers read `s.Index()` once per request.
- **Visibility is decided server-side.** Non-admins see only `solved/`, and an unsolved problem is a 404, not a 403. `auth.Forward` returns false for a request with no cookies without a network call. `problem.md` (the judge's statement) is admin-only even on solved problems.
- **Explanations run in the background.** `POST /explain` starts a job (`internal/jobs`, kind "explain", keyed by problem) and returns 202. The job's work calls Claude (`internal/claude`, streamed, JSON schema) and returns a draft; the kind's save func (`saveExplain`) commits `explain.md` through `Mutate`. The draft is on disk before the commit, so a failed commit is retried (`POST /jobs/<kind>/<key>/save`) and one a restart cut off is resumed (`Server.ResumeJobs`) without calling Claude again. Save funcs may therefore run twice for one draft. Tests fake Claude with `explain.Asker` and the judges with `handlers.Fetcher`; nothing in `go test` hits the network.
- **Recording reviews are the same shape.** `POST /review` takes a multipart upload and starts a job of its own kind: transcribe (`internal/transcribe`, OpenAI-compatible, raw HTTP), measure (`Transcript.Metrics`), optionally keep the audio (`internal/audiostore`, S3), have Claude review it (`internal/review`), then `saveReview` commits `reviews/<id>.md`. An unsaved draft keeps its recording; `discardReview` deletes it when a new review replaces the draft. Reviews are admin-only. Tests fake the transcriber and the store with `handlers.Transcriber` and `handlers.AudioStore`.
- **Concepts are the same shape, outside the problems.** `POST /concepts` has Claude explain a technique or pasted code (`internal/concept`) and commits `concepts/<slug>/` (explanation, C++ snippet, the request). The slug is Claude's, so a new concept's job is keyed by an id and returns `{slug}` as its `Result`; rewrites are keyed by slug. The index reads them into `Index.Concepts`, and they are public.
- **Evaluations are the same shape, but commit nothing.** `POST /evaluate` judges the solve page's draft: it runs the saved tests on the runner, then asks Claude (`internal/evaluate`). Its kind has no save func: the answer is the job's `Result`, kept in the job's record, and the page polls for it.
- **Claude calls are metered.** `claude.Client` records every call's tokens in `usage.Ledger`; `Server.claudeReady` refuses a new Claude job with 429 once `CLAUDE_MONTHLY_BUDGET` is spent. A nil ledger tracks nothing, which the tests use.
- **Admin writes are same-origin only.** `auth.SameOrigin` runs before `auth.Require` on the admin group and refuses a cross-origin browser request (403).
- **Code runs in a separate service, never in the site.** The solve page's run/lint handlers (`handlers/run.go`) read the problem from the clone (`Server.Dir`, not the index, which truncates) and post to `cmd/usacorunner` through `runner.Client`; `internal/runner/types.go` is the contract between them. Tests fake it with `handlers.Runner`. The runner's own tests run real g++/python3/clang-tidy/ruff and skip whichever is missing.
## Conventions
- **Comments explain why, not what.** Preserve them, and update them when behaviour changes.
- **Edits look at the disk.** An edit func passed to `Mutate` may run twice, so it must locate the problem with `findOnDisk`, never from the index.
- **Commit subjects match the CLI's** (`lib/cmd/done.sh`): "Solve <name>" and so on.
- **The problem layout belongs to the CLI** in the repo root (`lib/`). When the CLI's layout changes, update `internal/index`.
- **Docs are tested.** `docs_test.go` fails when README.md's route table and `Routes` disagree, or when a `./usaco` command in the root README is missing from `CliPage.vue` (skipped in CI, which only has `site/`).