Keep site jobs across restarts, meter Claude spend, harden admin routes
- 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:
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
@@ -62,8 +63,7 @@ func (s *Server) startReview(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
|
||||
}
|
||||
if s.Transcriber == nil {
|
||||
@@ -125,11 +125,9 @@ func (s *Server) startReview(ctx *gin.Context) {
|
||||
id := time.Now().UTC().Format(reviewIDLayout)
|
||||
rec := recording{name: id + ext, contentType: contentType, data: audio}
|
||||
link := p.URL
|
||||
if !s.reviewJobs.Start(p.Name, func() error { return s.review(in, link, id, rec) }) {
|
||||
ctx.JSON(http.StatusConflict, gin.H{"error": "a recording is already being reviewed"})
|
||||
return
|
||||
if startJob(ctx, s.reviewJobs, p.Name, "a recording is already being reviewed", func() (any, error) { return s.review(in, link, id, rec) }) {
|
||||
ctx.JSON(http.StatusAccepted, s.reviewJobs.Get(p.Name))
|
||||
}
|
||||
ctx.JSON(http.StatusAccepted, s.reviewJobs.Get(p.Name))
|
||||
}
|
||||
|
||||
func (s *Server) reviewStatus(ctx *gin.Context) {
|
||||
@@ -143,16 +141,26 @@ type recording struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
// review 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) review(in review.Input, link, id string, rec recording) (err error) {
|
||||
// reviewDraft is a written review waiting to be committed. Audio is the
|
||||
// recording's key in the store, kept until the review is saved or replaced.
|
||||
type reviewDraft struct {
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
Fetched string `json:"fetched,omitempty"`
|
||||
Markdown string `json:"markdown"`
|
||||
Audio string `json:"audio,omitempty"`
|
||||
}
|
||||
|
||||
// review 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) review(in review.Input, link, id string, rec recording) (draft any, err error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), reviewTimeout)
|
||||
defer cancel()
|
||||
name := in.Name
|
||||
|
||||
fetched, err := s.ensureStatement(ctx, "review", name, &in.Statement, link)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tr, err := s.Transcriber.Transcribe(ctx, rec.name, rec.data)
|
||||
@@ -160,17 +168,19 @@ func (s *Server) review(in review.Input, link, id string, rec recording) (err er
|
||||
var f *transcribe.Failure
|
||||
if errors.As(err, &f) {
|
||||
log.Printf("[review] %s: %s: %v", name, f.Msg, f.Err)
|
||||
return f
|
||||
return nil, f
|
||||
}
|
||||
log.Printf("[review] %s: transcribing: %v", name, err)
|
||||
return errors.New("the recording could not be transcribed")
|
||||
return nil, errors.New("the recording could not be transcribed")
|
||||
}
|
||||
in.Transcript, in.Metrics = tr, tr.Metrics()
|
||||
|
||||
// The recording is kept only if there is somewhere to keep it, and a
|
||||
// failure to keep it costs the playback, not the review. It is stored
|
||||
// before Claude runs so the review can point at it, and removed again
|
||||
// if the review never gets saved.
|
||||
// if Claude fails. Once there is a review it stays with the draft: a
|
||||
// failed commit can be retried, and discardReview removes it if a new
|
||||
// recording replaces the draft instead.
|
||||
key := ""
|
||||
if s.Audio != nil {
|
||||
k := name + "/" + rec.name
|
||||
@@ -194,27 +204,51 @@ func (s *Server) review(in review.Input, link, id string, rec recording) (err er
|
||||
var f *claude.Failure
|
||||
if errors.As(err, &f) {
|
||||
log.Printf("[review] %s: %s: %v", name, f.Msg, f.Err)
|
||||
return f
|
||||
return nil, f
|
||||
}
|
||||
log.Printf("[review] %s: %v", name, err)
|
||||
return errors.New("the review could not be written")
|
||||
return nil, errors.New("the review could not be written")
|
||||
}
|
||||
return reviewDraft{Name: name, ID: id, Fetched: fetched, Markdown: md, Audio: key}, nil
|
||||
}
|
||||
|
||||
err = s.Repo.Mutate(ctx, "Review "+name, func(dir string) error {
|
||||
status, err := findOnDisk(dir, name)
|
||||
// saveReview commits a review. The id was chosen when the recording came
|
||||
// in, so running it again for a draft already pushed rewrites the same
|
||||
// file and commits nothing.
|
||||
func (s *Server) saveReview(raw json.RawMessage) (any, error) {
|
||||
var d reviewDraft
|
||||
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, "Review "+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
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(pdir, "reviews"), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return writeOrRemove(filepath.Join(pdir, "reviews", id+".md"), md)
|
||||
return writeOrRemove(filepath.Join(pdir, "reviews", d.ID+".md"), d.Markdown)
|
||||
})
|
||||
return jobSaveError("review", name, "the review was written but could not be saved to git", err)
|
||||
return nil, jobSaveError("review", d.Name, "the review was written but could not be saved to git", err)
|
||||
}
|
||||
|
||||
// discardReview removes the recording of a review that was never saved and
|
||||
// has been replaced by a new one.
|
||||
func (s *Server) discardReview(raw json.RawMessage) {
|
||||
var d reviewDraft
|
||||
if json.Unmarshal(raw, &d) != nil || d.Audio == "" || s.Audio == nil {
|
||||
return
|
||||
}
|
||||
if err := s.Audio.Delete(context.Background(), d.Audio); err != nil {
|
||||
log.Printf("[review] %s: removing the unsaved review's recording: %v", d.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// findReview is the problem's review with the URL's id.
|
||||
|
||||
Reference in New Issue
Block a user