Keep site jobs across restarts, meter Claude spend, harden admin routes
Test and publish the site / test (push) Successful in 2m15s
Test and publish the site / publish (push) Successful in 9m5s

- 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]>
This commit is contained in:
2026-10-23 22:10:01 +01:00
co-authored by Claude Opus 5.5
parent 7638d88570
commit cae5ec8e5a
42 changed files with 1849 additions and 264 deletions
+9 -9
View File
@@ -18,8 +18,8 @@ import (
"adam-french.co.uk/usaco-site/internal/audiostore"
"adam-french.co.uk/usaco-site/internal/claude"
"adam-french.co.uk/usaco-site/internal/explain"
"adam-french.co.uk/usaco-site/internal/index"
"adam-french.co.uk/usaco-site/internal/jobs"
"adam-french.co.uk/usaco-site/internal/review"
"adam-french.co.uk/usaco-site/internal/transcribe"
)
@@ -121,18 +121,18 @@ func upload(r *gin.Engine, name, filename string, size int, fields ...string) *h
return do(r, "POST", "/usaco/api/problems/"+name+"/review", body.String(), true, "Content-Type", w.FormDataContentType())
}
func waitReview(t *testing.T, r *gin.Engine, name string) explain.Status {
func waitReview(t *testing.T, r *gin.Engine, name string) jobs.Status {
t.Helper()
for range 400 {
var s explain.Status
var s jobs.Status
json.Unmarshal(do(r, "GET", "/usaco/api/problems/"+name+"/review", "", true).Body.Bytes(), &s)
if s.State != explain.Running {
if s.State != jobs.Running {
return s
}
time.Sleep(5 * time.Millisecond)
}
t.Fatal("review still running")
return explain.Status{}
return jobs.Status{}
}
func withStatement(t *testing.T, r *gin.Engine) {
@@ -183,7 +183,7 @@ func TestReviewWritesAndCommits(t *testing.T) {
if rec := upload(r, "moocast", "Take 1.M4A", 10, "note", "mock interview", "includeCode", "true"); rec.Code != 202 {
t.Fatalf("start = %d: %s", rec.Code, rec.Body)
}
if s := waitReview(t, r, "moocast"); s.State != explain.Done {
if s := waitReview(t, r, "moocast"); s.State != jobs.Done {
t.Fatalf("job = %+v, want done", s)
}
if !strings.HasSuffix(ft.file, ".m4a") || string(ft.audio[:3]) != "ID3" {
@@ -241,7 +241,7 @@ func TestReviewKeepsAndRemovesTheRecording(t *testing.T) {
withStatement(t, r)
upload(r, "moocast", "take.mp3", 100)
if s := waitReview(t, r, "moocast"); s.State != explain.Done {
if s := waitReview(t, r, "moocast"); s.State != jobs.Done {
t.Fatalf("job = %+v, want done", s)
}
var p index.Problem
@@ -288,7 +288,7 @@ func TestReviewFailures(t *testing.T) {
commits := len(repo.msg)
upload(r, "moocast", "a.mp3", 10)
if s := waitReview(t, r, "moocast"); s.State != explain.Failed || s.Error != "no speech was found in the recording" {
if s := waitReview(t, r, "moocast"); s.State != jobs.Failed || s.Error != "no speech was found in the recording" {
t.Fatalf("transcription failure: job = %+v", s)
}
@@ -296,7 +296,7 @@ func TestReviewFailures(t *testing.T) {
// behind when the review fails.
ft.err, fr.fail = nil, true
upload(r, "moocast", "a.mp3", 10)
if s := waitReview(t, r, "moocast"); s.State != explain.Failed || s.Error != "Claude declined to answer" {
if s := waitReview(t, r, "moocast"); s.State != jobs.Failed || s.Error != "Claude declined to answer" {
t.Fatalf("Claude failure: job = %+v", s)
}
if len(repo.msg) != commits || len(store.keys()) != 0 {