- 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]>
4.9 KiB
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/repoowns the clone and serializes every git command. A sync is fetch + hard reset (the remote wins).Mutatedoes sync, edit, commit and push, and replays the edit once if the push is rejected. ItsonChangecallback (Server.Reload) rebuilds the index under the same lock, when HEAD moved. Beside the clone,DATA_DIRholds 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 reads.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.Forwardreturns 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 /explainstarts 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) commitsexplain.mdthroughMutate. 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 withexplain.Askerand the judges withhandlers.Fetcher; nothing ingo testhits the network. -
Recording reviews are the same shape.
POST /reviewtakes 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), thensaveReviewcommitsreviews/<id>.md. An unsaved draft keeps its recording;discardReviewdeletes it when a new review replaces the draft. Reviews are admin-only. Tests fake the transcriber and the store withhandlers.Transcriberandhandlers.AudioStore. -
Concepts are the same shape, outside the problems.
POST /conceptshas Claude explain a technique or pasted code (internal/concept) and commitsconcepts/<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 itsResult; rewrites are keyed by slug. The index reads them intoIndex.Concepts, and they are public. -
Evaluations are the same shape, but commit nothing.
POST /evaluatejudges 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'sResult, kept in the job's record, and the page polls for it. -
Claude calls are metered.
claude.Clientrecords every call's tokens inusage.Ledger;Server.claudeReadyrefuses a new Claude job with 429 onceCLAUDE_MONTHLY_BUDGETis spent. A nil ledger tracks nothing, which the tests use. -
Admin writes are same-origin only.
auth.SameOriginruns beforeauth.Requireon 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 tocmd/usacorunnerthroughrunner.Client;internal/runner/types.gois the contract between them. Tests fake it withhandlers.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
Mutatemay run twice, so it must locate the problem withfindOnDisk, never from the index. - Commit subjects match the CLI's (
lib/cmd/done.sh): "Solve " and so on. - The problem layout belongs to the CLI in the repo root (
lib/). When the CLI's layout changes, updateinternal/index. - Docs are tested.
docs_test.gofails when README.md's route table andRoutesdisagree, or when a./usacocommand in the root README is missing fromCliPage.vue(skipped in CI, which only hassite/).