Run and lint solutions on the site
Test and publish the site / test (push) Successful in 1m23s
Test and publish the site / publish (push) Failing after 4m22s

A new admin-only solve page, /p/<name>/solve, edits a problem's code in
C++ or Python (a dropdown switches between them), runs it on custom
input or against the saved tests with per-test verdicts (AC, WA, RE,
TLE, unchecked) and compile errors, marks lint diagnostics in the
editor, and saves the code back as a commit ("Edit <name>"). Drafts
stay in the browser per problem and language until saved. LeetCode
problems are C++ only, since their tests run through driver.h.

Code never runs in the site, which holds the Claude, Gitea and S3
secrets. A separate runner service (cmd/usacorunner, Dockerfile.runner)
does it in a container meant to have no environment, an internal-only
network, a read-only root apart from /tmp, and memory, CPU and pids
limits, with tini to reap orphans. Runs copy the CLI: the same g++
flags with -DLOCAL and the repo's headers, <io>.in on disk and on
stdin, <io>.out or stdout as the output, and usaco test's comparison.
Unlike the CLI they have CPU, memory, file and wall-clock limits.

C++ lint is g++'s warnings plus clang-tidy with a new .clang-tidy
(bugprone, performance and a few readability checks; template.cpp is
clean). Python lint is a syntax check plus ruff. template.py is the
Python starter, with a stdio() helper like template.rs's.

CI builds, smoke-tests and rolls out the runner image before the site.
The web_server stack needs a usaco-runner service before that rollout
can succeed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 11:24:22 +01:00
co-authored by Claude Opus 5.5
parent 107ecab9da
commit c972fa9fd9
33 changed files with 4477 additions and 23 deletions
+16 -2
View File
@@ -41,6 +41,8 @@ The site commits, so the laptop must pull before pushing. `usaco sync` does `pul
```
cmd/usacosite/ main: reads the environment, wires everything, registers routes
cmd/usacorunner/ the code runner service (its own image, Dockerfile.runner)
internal/runner/ compile, run, compare and lint, with limits; the site's client for it
internal/auth/ is this request the admin? (asks the site backend; copied from job-tracker)
internal/repo/ the clone: sync, and edit-commit-push with one replay on rejection
internal/index/ reads the clone into memory: problems, files, tests, git dates
@@ -73,6 +75,10 @@ Everything is served under `BASE_PATH` (`/usaco`). nginx proxies the prefix thro
| `GET /usaco/api/problems/<name>/reviews/<id>/audio` | admin | the review's recording, with range support; 404 if it was not kept |
| `DELETE /usaco/api/problems/<name>/reviews/<id>` | admin | removes the review and its recording |
| `POST /usaco/api/problems/<name>/status` | admin | `{to: solving\|solved\|skipped}`: moves the directory |
| `GET /usaco/api/problems/<name>/code` | admin | the solve page's start: `{io, leetcode, languages, default, runner, code: {cpp, python}}`, each language's saved source or a starter from `template.cpp`/`template.py` |
| `POST /usaco/api/problems/<name>/run` | admin | `{lang, source, mode: tests\|custom, input?}`: compiles once and runs the saved tests (or the custom input) on the runner; answers its verdicts. 503 without `RUNNER_URL` |
| `POST /usaco/api/problems/<name>/lint` | admin | `{lang, source}`: the runner's diagnostics (g++ and clang-tidy, or python and ruff) |
| `PUT /usaco/api/problems/<name>/code` | admin | `{lang, source}`: commits `main.cpp`, or `main.py` (`sol.py` where that is the existing file), as "Edit <name>" |
| `POST /usaco/api/hooks/gitea` | HMAC (`X-Gitea-Signature`) | queue a pull |
| `GET /usaco/api/health` | open | 200 once the clone is indexed |
@@ -113,8 +119,8 @@ The site's `docker-compose.yml` runs the published image as service `usaco`, wit
A push to `main` that touches `site/` or `.gitea/workflows/` runs `.gitea/workflows/ci.yaml` at the repo root. It does the following:
1. Tests.
2. Builds the image from `site/` and pushes it as `adam-french.co.uk/adamf/usaco:<sha>` and `:latest`.
3. Rolls the site onto the new image and waits for the healthcheck.
2. Builds both images from `site/`, smoke-tests the runner, and pushes them as `adam-french.co.uk/adamf/usaco:<sha>` and `adam-french.co.uk/adamf/usaco-runner:<sha>`, each also as `:latest`.
3. Rolls `usaco-runner` and then the site onto the new images, waiting for each healthcheck.
Problem commits don't match the path filter, so they never rebuild the image. The webhook brings them in.
@@ -164,6 +170,14 @@ The "Review my explanation" panel (admin only) takes a recording of the admin ex
- **The recording.** It is too big for git. With all five `S3_*` variables set it is kept in that bucket as `<name>/<id>.<ext>` and the page gets a player; otherwise it is thrown away once transcribed. In web_server that is a private Garage bucket, never the public `uploads` one. A recording whose review fails is deleted again.
- **Privacy.** A review is a transcript of the admin talking, so reviews and their count are blanked for everyone else, even on solved problems.
## Solve page
`/usaco/p/<name>/solve` (admin only) is a full-width editor: the statement and saved tests on the left; a CodeMirror editor with a C++/Python dropdown, Run (custom input), Submit (the saved tests), Save (commit) and the results on the right. Drafts live in the browser's localStorage per problem and language until saved or reset. LeetCode problems are C++ only, since their tests run through `driver.h`.
- **The runner is its own service.** The site holds the Claude, Gitea and S3 secrets and shares `app-network` with the database, so it never runs code. `cmd/usacorunner` does, in a container with no environment, on an `internal` network that only the site joins, read-only apart from an exec-able `/tmp` tmpfs, with memory, CPU and pids limits, all capabilities dropped and tini as PID 1 to reap orphans. The site posts to it (`runner.Client`, `RUNNER_URL`); the browser never sees it.
- **Runs copy the CLI.** The same g++ flags as `lib/common.sh` (`-DLOCAL`, with the repo root's `debugging.h` and `leetcode.h`, and a LeetCode problem's `driver.h`, sent along), the program runs in a directory holding `<io>.in` with that file also on stdin, `<io>.out` is its output if it wrote one, and outputs compare as `usaco test` does, ignoring trailing whitespace and trailing blank lines. Unlike the CLI there are limits: CPU time (2 s by default; TLE), 512 MB of address space, 64 MB files, a wall clock, and 60 s for a whole request, which keeps it synchronous under nginx's 120 s.
- **Lint.** C++ is g++'s warnings plus clang-tidy with the repo's `.clang-tidy` (bugprone, performance and a few readability checks, tuned so `template.cpp` is clean); the runner embeds a copy, and a test keeps the two equal. Python is `compile()` for syntax errors plus ruff (`E4,E7,E9,F,B`). A missing tool is skipped.
## Things that are easy to get wrong
- **`BASE_PATH` and Vite's `base` must agree.** The SPA's asset URLs and router base are fixed at build time.