Add an Explain button that has Claude explain a problem
Test and publish the site / publish (push) Successful in 8m15s
Test and publish the site / test (push) Successful in 5m51s

When stuck, the admin can ask Claude for an intuitive explanation of a
problem. It is committed as explain.md and shown as folded sections, so
the reader opens only as much as they need: the key observation, hints
that get stronger, the approach, a walkthrough with an SVG diagram per
step, complexity, pitfalls and a pseudocode sketch.

- internal/claude: streamed call with a JSON-schema reply, high effort,
  the server-side refusal fallback, fixed error messages and logged
  token usage. CLAUDE_API_KEY and CLAUDE_MODEL (default claude-opus-5-5);
  without a key only this feature is off.
- internal/explain: the prompt, the schema, rendering to markdown, and
  a per-problem job tracker. The request starts a job and the page polls,
  since a call outlasts nginx's 120s timeout.
- internal/statement: fetches statements from LeetCode's GraphQL API and
  USACO's problem pages. Others (Codeforces is behind Cloudflare) are
  pasted.
- problem.md holds the statement, editable from the meta editor and
  shown only to the admin. Diagrams are sanitized with DOMPurify.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-19 20:38:15 +01:00
co-authored by Claude Opus 5.5
parent f0e10d2a58
commit 995b44a57a
27 changed files with 2144 additions and 67 deletions
+33 -19
View File
@@ -3,9 +3,11 @@
//
// The layout is the CLI's (README.md at the repo root): solving/, solved/
// and skipped/<name>/, each with a solution (main.cpp, sol.py...), an <io>.in
// the CLI wires up, tests/N.in + N.out, an optional .url and an optional
// notes.md. The repo is small, so everything, file contents included, is
// read on each rebuild, and a rebuild only happens when the clone changes.
// the CLI wires up, tests/N.in + N.out, and optionally a .url, notes.md,
// problem.md (the statement, pasted or fetched from the judge by the site)
// and explain.md (an explanation the site had Claude write). The repo is
// small, so everything, file contents included, is read on each rebuild,
// and a rebuild only happens when the clone changes.
package index
import (
@@ -61,27 +63,32 @@ type Commit struct {
// Summary is what the list shows.
type Summary struct {
Name string `json:"name"`
Status string `json:"status"`
URL string `json:"url,omitempty"`
Source string `json:"source,omitempty"`
Langs []string `json:"langs"`
Tests int `json:"tests"`
HasNotes bool `json:"hasNotes"`
FirstSeen *time.Time `json:"firstSeen,omitempty"`
SolvedAt *time.Time `json:"solvedAt,omitempty"`
LastTouched *time.Time `json:"lastTouched,omitempty"`
Name string `json:"name"`
Status string `json:"status"`
URL string `json:"url,omitempty"`
Source string `json:"source,omitempty"`
Langs []string `json:"langs"`
Tests int `json:"tests"`
HasNotes bool `json:"hasNotes"`
HasExplanation bool `json:"hasExplanation"`
FirstSeen *time.Time `json:"firstSeen,omitempty"`
SolvedAt *time.Time `json:"solvedAt,omitempty"`
LastTouched *time.Time `json:"lastTouched,omitempty"`
}
// Problem is everything known about one problem.
type Problem struct {
Summary
IO string `json:"io,omitempty"`
Notes string `json:"notes"`
Files []File `json:"files"`
Sample *File `json:"sample,omitempty"`
TestSet []Test `json:"testCases"`
History []Commit `json:"history"`
IO string `json:"io,omitempty"`
Notes string `json:"notes"`
// Statement is the judge's text, so the handlers send it to the admin
// only: the site should not republish it.
Statement string `json:"statement,omitempty"`
Explanation string `json:"explanation"`
Files []File `json:"files"`
Sample *File `json:"sample,omitempty"`
TestSet []Test `json:"testCases"`
History []Commit `json:"history"`
}
// Index is one snapshot of the clone.
@@ -157,6 +164,12 @@ func readProblem(pdir, status string) (*Problem, error) {
case name == "notes.md":
b, _ := os.ReadFile(filepath.Join(pdir, name))
p.Notes = string(b)
case name == "problem.md":
b, _ := os.ReadFile(filepath.Join(pdir, name))
p.Statement = string(b)
case name == "explain.md":
b, _ := os.ReadFile(filepath.Join(pdir, name))
p.Explanation = string(b)
case strings.HasSuffix(name, ".in") && p.IO == "":
// The first *.in, as detect_io in lib/common.sh picks it.
p.IO = strings.TrimSuffix(name, ".in")
@@ -195,6 +208,7 @@ func readProblem(pdir, status string) (*Problem, error) {
p.TestSet = tests
p.Tests = len(tests)
p.HasNotes = strings.TrimSpace(p.Notes) != ""
p.HasExplanation = strings.TrimSpace(p.Explanation) != ""
p.Source = Source(p.URL)
return p, nil
}
+8
View File
@@ -70,6 +70,8 @@ func fixture(t *testing.T) string {
write("solved/moocast/tests/3.in", "e\n")
write("solved/moocast/notes.md", "union find\n")
write("solved/moocast/problem.md", "# Moocast\n")
write("solved/moocast/explain.md", "## Key observation\n")
commit(4, "Add a test")
return dir
}
@@ -87,6 +89,12 @@ func TestBuild(t *testing.T) {
if m.Status != Solved || m.IO != "moocast" || m.Source != "USACO" || !m.HasNotes {
t.Fatalf("moocast summary wrong: %+v", m.Summary)
}
if m.Statement != "# Moocast\n" || m.Explanation != "## Key observation\n" || !m.HasExplanation {
t.Fatalf("moocast statement/explanation = %q / %q", m.Statement, m.Explanation)
}
if w := idx.Problems["watermelon"]; w.HasExplanation || w.Statement != "" {
t.Fatal("watermelon has no problem.md or explain.md")
}
if len(m.Files) != 2 || m.Files[0].Name != "main.cpp" || m.Files[1].Lang != "python" {
t.Fatalf("moocast files = %+v, want main.cpp then sol.py", m.Files)
}