Autocomplete and evaluate solutions on the solve page
Test and publish the site / test (push) Successful in 1m25s
Test and publish the site / publish (push) Successful in 1m51s

The editor's completion popup was on but empty for C++, since
@codemirror/lang-cpp has no completion source. web/src/completions.js
adds one: keywords, the STL, every name in the file (the template's
macros show their expansion, typedefs what they alias), members after
. and ->, and snippets for the loops and lambdas typed over and over.
Python gains the contest standard library and members after ., on top
of lang-python's builtins and local names. Nothing knows types, so
members are the common ones plus those already used in the file.

Evaluate (POST/GET /problems/<name>/evaluate) has Claude judge the
editor's code as a strong competitive programmer would before
submitting. It runs the saved tests on the runner first, as Submit
does, and gives Claude the verdicts and up to three failing cases. The
reply is a verdict, correctness, complexity against the constraints,
line-anchored issues and a traced counterexample (internal/evaluate).
The page shows it in an Evaluation tab of the results panel, marks the
issues in the editor beside the lint's for as long as their lines are
unchanged, and can load the counterexample as the custom input.

It is a background job the page polls, like explanations, but it
commits nothing: the code is a draft. explain.Jobs.StartWith keeps a
finished job's answer in memory until the next one or a restart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 12:46:54 +01:00
co-authored by Claude Opus 5.5
parent 34552f2d81
commit d4849859df
18 changed files with 1566 additions and 36 deletions
+117
View File
@@ -0,0 +1,117 @@
package handlers
import (
"context"
"errors"
"log"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"adam-french.co.uk/usaco-site/internal/claude"
"adam-french.co.uk/usaco-site/internal/evaluate"
"adam-french.co.uk/usaco-site/internal/runner"
)
// The saved tests run first and Claude takes a minute or two after.
const evaluateTimeout = 10 * time.Minute
// startEvaluate starts judging the editor's source and answers at once; the
// page polls evaluateStatus, whose result is the evaluation. Nothing is
// committed: the source is a draft, and the answer is about this version of
// it only.
func (s *Server) startEvaluate(ctx *gin.Context) {
if s.Claude == nil {
ctx.JSON(http.StatusServiceUnavailable, gin.H{"error": "Claude is not configured on this server (CLAUDE_API_KEY)"})
return
}
body, p, l, ok := s.readCode(ctx)
if !ok {
return
}
if strings.TrimSpace(body.Source) == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "the source is empty"})
return
}
if len(body.Note) > maxNote {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "the note is too long"})
return
}
if strings.TrimSpace(p.Statement) == "" && p.URL == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "add the problem statement or a link to it first"})
return
}
in := evaluate.Input{
Name: p.Name, Statement: p.Statement, Examples: examples(p),
Lang: body.Lang, Source: body.Source, LeetCode: l.leetcode, Note: body.Note,
}
link := p.URL
if !s.evalJobs.StartWith(p.Name, func() (any, error) { return s.evaluate(in, link, l) }) {
ctx.JSON(http.StatusConflict, gin.H{"error": "a solution is already being evaluated"})
return
}
ctx.JSON(http.StatusAccepted, s.evalJobs.Get(p.Name))
}
func (s *Server) evaluateStatus(ctx *gin.Context) {
if p, ok := s.lookup(ctx, true); ok {
ctx.JSON(http.StatusOK, s.evalJobs.Get(p.Name))
}
}
// evaluate 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) evaluate(in evaluate.Input, link string, l *layout) (*evaluate.Evaluation, error) {
ctx, cancel := context.WithTimeout(context.Background(), evaluateTimeout)
defer cancel()
name := in.Name
// A fetched statement is not saved: that would be the only commit an
// evaluation makes. Explain or Edit details keep one.
if _, err := s.ensureStatement(ctx, "evaluate", name, &in.Statement, link); err != nil {
return nil, err
}
in.Run, in.RunNote = s.evaluationRun(ctx, name, in.Lang, in.Source, l)
ev, err := evaluate.Generate(ctx, s.Claude, in, time.Now())
if err != nil {
var f *claude.Failure
if errors.As(err, &f) {
log.Printf("[evaluate] %s: %s: %v", name, f.Msg, f.Err)
return nil, f
}
log.Printf("[evaluate] %s: %v", name, err)
return nil, errors.New("the evaluation could not be written")
}
return ev, nil
}
// evaluationRun is Submit's run, for Claude to read. A run that cannot
// happen costs the evaluation its test verdicts, not the evaluation: the
// note says why, and goes to Claude in their place.
func (s *Server) evaluationRun(ctx context.Context, name, lang, source string, l *layout) (*runner.RunResponse, string) {
if s.Runner == nil {
return nil, "The tests were not run: the code runner is not configured."
}
tests, err := readRunTests(l)
if err != nil {
log.Printf("[evaluate] %s: reading the tests: %v", name, err)
return nil, "The tests were not run: they could not be read."
}
files, err := s.headers(l, lang)
if err != nil {
log.Printf("[evaluate] %s: reading the headers: %v", name, err)
return nil, "The tests were not run: the headers could not be read."
}
c, cancel := context.WithTimeout(ctx, runTimeout)
defer cancel()
res, err := s.Runner.Run(c, runner.RunRequest{Lang: lang, Source: source, Files: files, IO: l.io, Tests: tests})
if err != nil {
log.Printf("[evaluate] %s: running the tests: %v", name, err)
return nil, "The tests were not run: the code runner failed."
}
return &res, ""
}