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
+35 -20
View File
@@ -2,6 +2,7 @@ package handlers
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
@@ -82,8 +83,7 @@ func (s *Server) startExplain(ctx *gin.Context) {
if !ok {
return
}
if s.Claude == nil {
ctx.JSON(http.StatusServiceUnavailable, gin.H{"error": "Claude is not configured on this server (CLAUDE_API_KEY)"})
if !s.claudeReady(ctx) {
return
}
var body explainBody
@@ -105,28 +105,33 @@ func (s *Server) startExplain(ctx *gin.Context) {
in.Code = mainFile(p)
}
link := p.URL
if !s.jobs.Start(p.Name, func() error { return s.explain(in, link) }) {
ctx.JSON(http.StatusConflict, gin.H{"error": "an explanation is already being written"})
return
if startJob(ctx, s.explainJobs, p.Name, "an explanation is already being written", func() (any, error) { return s.explain(in, link) }) {
ctx.JSON(http.StatusAccepted, s.explainJobs.Get(p.Name))
}
ctx.JSON(http.StatusAccepted, s.jobs.Get(p.Name))
}
func (s *Server) explainStatus(ctx *gin.Context) {
if p, ok := s.lookup(ctx, true); ok {
ctx.JSON(http.StatusOK, s.jobs.Get(p.Name))
ctx.JSON(http.StatusOK, s.explainJobs.Get(p.Name))
}
}
// explain is one job. Its error is shown to the admin, so every path returns
// a message meant for them and logs the detail.
func (s *Server) explain(in explain.Input, link string) error {
// explainDraft is a written explanation waiting to be committed.
type explainDraft struct {
Name string `json:"name"`
Fetched string `json:"fetched,omitempty"`
Markdown string `json:"markdown"`
}
// explain is one job's work. Its error is shown to the admin, so every path
// returns a message meant for them and logs the detail.
func (s *Server) explain(in explain.Input, link string) (any, error) {
ctx, cancel := context.WithTimeout(context.Background(), explainTimeout)
defer cancel()
fetched, err := s.ensureStatement(ctx, "explain", in.Name, &in.Statement, link)
if err != nil {
return err
return nil, err
}
md, err := explain.Generate(ctx, s.Claude, in, time.Now())
@@ -134,25 +139,35 @@ func (s *Server) explain(in explain.Input, link string) error {
var f *claude.Failure
if errors.As(err, &f) {
log.Printf("[explain] %s: %s: %v", in.Name, f.Msg, f.Err)
return f
return nil, f
}
log.Printf("[explain] %s: %v", in.Name, err)
return errors.New("the explanation could not be written")
return nil, errors.New("the explanation could not be written")
}
return explainDraft{Name: in.Name, Fetched: fetched, Markdown: md}, nil
}
name := in.Name
err = s.Repo.Mutate(ctx, "Explain "+name, func(dir string) error {
status, err := findOnDisk(dir, name)
// saveExplain commits an explanation. Running it again for a draft already
// pushed changes nothing, so commits nothing.
func (s *Server) saveExplain(raw json.RawMessage) (any, error) {
var d explainDraft
if err := json.Unmarshal(raw, &d); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), saveTimeout)
defer cancel()
err := s.Repo.Mutate(ctx, "Explain "+d.Name, func(dir string) error {
status, err := findOnDisk(dir, d.Name)
if err != nil {
return err
}
pdir := filepath.Join(dir, status, name)
if err := saveFetched(pdir, fetched); err != nil {
pdir := filepath.Join(dir, status, d.Name)
if err := saveFetched(pdir, d.Fetched); err != nil {
return err
}
return writeOrRemove(filepath.Join(pdir, "explain.md"), md)
return writeOrRemove(filepath.Join(pdir, "explain.md"), d.Markdown)
})
return jobSaveError("explain", name, "the explanation was written but could not be saved to git", err)
return nil, jobSaveError("explain", d.Name, "the explanation was written but could not be saved to git", err)
}
// ensureStatement fetches the statement when none is saved, so a job can