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
+1 -1
View File
@@ -147,7 +147,7 @@ func Prompt(in Input) string {
// Generate asks Claude and returns explain.md.
func Generate(ctx context.Context, a Asker, in Input, now time.Time) (string, error) {
var r Result
err := a.JSON(ctx, claude.Request{System: System, User: Prompt(in), Schema: Schema, MaxTokens: maxTokens}, &r)
err := a.JSON(ctx, claude.Request{Tag: "explain", System: System, User: Prompt(in), Schema: Schema, MaxTokens: maxTokens}, &r)
if err != nil {
return "", err
}
-34
View File
@@ -105,37 +105,3 @@ func TestGenerate(t *testing.T) {
t.Error("System is missing the rules it shares with concepts")
}
}
func TestJobsOneAtATime(t *testing.T) {
var j Jobs
if s := j.Get("a"); s.State != Idle {
t.Fatalf("fresh state = %q, want idle", s.State)
}
release := make(chan struct{})
if !j.Start("a", func() error { <-release; return nil }) {
t.Fatal("first start refused")
}
if j.Start("a", func() error { return nil }) {
t.Fatal("second start while running accepted")
}
if !j.Start("b", func() error { return context.Canceled }) {
t.Fatal("another problem's job refused")
}
close(release)
wait := func(name string) Status {
for range 200 {
if s := j.Get(name); s.State != Running {
return s
}
time.Sleep(5 * time.Millisecond)
}
t.Fatalf("%s still running", name)
return Status{}
}
if s := wait("a"); s.State != Done || s.Finished == nil {
t.Errorf("a = %+v, want done", s)
}
if s := wait("b"); s.State != Failed || s.Error != context.Canceled.Error() {
t.Errorf("b = %+v, want the error", s)
}
}
-78
View File
@@ -1,78 +0,0 @@
package explain
import (
"sync"
"time"
)
// A job's states. Idle means nothing has run since the server started.
const (
Idle = "idle"
Running = "running"
Done = "done"
Failed = "error"
)
// Status is what the page polls for.
type Status struct {
State string `json:"state"`
Error string `json:"error,omitempty"`
Started *time.Time `json:"started,omitempty"`
Finished *time.Time `json:"finished,omitempty"`
// Result is a finished StartWith job's answer.
Result any `json:"result,omitempty"`
}
// Jobs runs at most one explanation per problem at a time. An explanation
// takes a minute or more, longer than nginx waits for a response, so the
// request only starts it and the page polls. State is in memory: a restart
// mid-job loses the job, and nothing is committed until it finishes.
type Jobs struct {
mu sync.Mutex
m map[string]*Status
}
// Start runs fn in the background unless a job for name is running, and
// reports whether it started. fn's error is shown to the admin, so it must
// be a message meant for them.
func (j *Jobs) Start(name string, fn func() error) bool {
return j.StartWith(name, func() (any, error) { return nil, fn() })
}
// StartWith is Start for a job whose answer is kept here rather than
// committed: a done job's Status carries it until the next job for name
// starts, or the server restarts.
func (j *Jobs) StartWith(name string, fn func() (any, error)) bool {
j.mu.Lock()
defer j.mu.Unlock()
if j.m == nil {
j.m = map[string]*Status{}
}
if s := j.m[name]; s != nil && s.State == Running {
return false
}
now := time.Now()
j.m[name] = &Status{State: Running, Started: &now}
go func() {
res, err := fn()
j.mu.Lock()
defer j.mu.Unlock()
end := time.Now()
s := &Status{State: Done, Started: &now, Finished: &end, Result: res}
if err != nil {
s.State, s.Error, s.Result = Failed, err.Error(), nil
}
j.m[name] = s
}()
return true
}
// Get is name's latest job.
func (j *Jobs) Get(name string) Status {
j.mu.Lock()
defer j.mu.Unlock()
if s := j.m[name]; s != nil {
return *s
}
return Status{State: Idle}
}