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
+17 -4
View File
@@ -52,10 +52,14 @@ type Repo struct {
cfg Config
askpass string
// onChange runs under the lock after every sync or edit, with the clone
// in a consistent state, so the index is never built from a half-reset
// tree.
// onChange runs under the lock after a sync or edit that moved HEAD,
// with the clone in a consistent state, so the index is never built
// from a half-reset tree.
onChange func(dir string) error
// indexed is the HEAD onChange last succeeded on. A sync that finds
// nothing new (the interval's pull, the webhook for the site's own
// push) skips the rebuild, which reads every file and the whole log.
indexed string
mu sync.Mutex
}
@@ -163,7 +167,16 @@ func (r *Repo) changed() error {
if r.onChange == nil {
return nil
}
return r.onChange(r.cfg.Dir)
out, err := r.git(context.Background(), "rev-parse", "HEAD")
head := strings.TrimSpace(out)
if err == nil && head == r.indexed {
return nil
}
if err := r.onChange(r.cfg.Dir); err != nil {
return err
}
r.indexed = head
return nil
}
// Mutate syncs, lets edit change files in the clone, and commits the result
+34
View File
@@ -90,6 +90,40 @@ func TestSyncClonesThenFollowsRemote(t *testing.T) {
if *changes != 2 {
t.Fatalf("onChange ran %d times, want 2", *changes)
}
// Nothing new: no rebuild.
if err := r.Sync(ctx); err != nil {
t.Fatal(err)
}
if *changes != 2 {
t.Fatalf("a sync with nothing new ran onChange (%d times)", *changes)
}
}
// A rebuild that failed is tried again on the next sync, though HEAD has
// not moved since.
func TestFailedRebuildIsRetried(t *testing.T) {
bare, _ := remote(t)
fail := true
calls := 0
r, err := New(Config{Dir: filepath.Join(t.TempDir(), "clone"), Remote: "file://" + bare, Branch: "main", Push: true},
func(string) error {
calls++
if fail {
return os.ErrInvalid
}
return nil
})
if err != nil {
t.Fatal(err)
}
if err := r.Sync(context.Background()); err == nil {
t.Fatal("the failed rebuild was not reported")
}
fail = false
if err := r.Sync(context.Background()); err != nil || calls != 2 {
t.Fatalf("retry: err %v, %d calls, want 2", err, calls)
}
}
func TestMutateCommitsAndPushes(t *testing.T) {