Review recorded explanations of a solution on the site
Test and publish the site / test (push) Successful in 2m4s
Test and publish the site / publish (push) Successful in 1m44s

The admin can upload a recording of themselves explaining a problem's
solution out loud (mp3, m4a, wav, webm, ogg or flac, up to 25 MB) and
get interview-style feedback, committed as reviews/<id>.md so every
attempt is kept.

Claude can't take audio, so the recording is transcribed first by an
OpenAI-compatible endpoint (whisper-1 by default; TRANSCRIBE_API_KEY,
with TRANSCRIBE_URL for a self-hosted server). Pace, pauses, filler
words and when complexity first comes up are measured from the word
timestamps and given to Claude with the timestamped transcript, the
statement and optionally the code. The reply covers the verdict, scores
in five areas, the approach and its complexity, missed edge cases,
delivery and what to work on.

Recordings are kept for playback in an S3 bucket (Garage) when the
S3_* settings are given, and are thrown away otherwise. Reviews are
admin-only. The job runs in the background like Explain, and the
statement fetch and save-error handling are now shared between the two.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 01:56:55 +01:00
co-authored by Claude Opus 5.5
parent 33027429c5
commit 63608f130c
25 changed files with 2381 additions and 54 deletions
+127
View File
@@ -0,0 +1,127 @@
// Package audiostore keeps the review recordings in an S3 bucket, so they
// can be played back next to their feedback. In production that is a
// private bucket on web_server's Garage.
//
// It is optional. Git is the site's store and a recording is about a
// megabyte a minute, too big to commit, so without the S3_* settings the
// audio is transcribed and thrown away and only the review is kept.
package audiostore
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)
// ErrNotFound is a key with no object.
var ErrNotFound = errors.New("no such recording")
// Config is the S3_* block of the environment.
type Config struct {
Endpoint string // e.g. http://garage:3900
Region string // must match garage.toml's s3_region
Bucket string
AccessKeyID string
SecretAccessKey string
}
// Store is one bucket.
type Store struct {
client *s3.Client
bucket string
}
// New returns nil, and the audio is thrown away, unless every setting is
// given: half a config is more likely a typo than a wish to store nothing.
func New(cfg Config) (*Store, error) {
set := 0
for _, v := range []string{cfg.Endpoint, cfg.Region, cfg.Bucket, cfg.AccessKeyID, cfg.SecretAccessKey} {
if v != "" {
set++
}
}
switch set {
case 0:
return nil, nil
case 5:
default:
return nil, errors.New("S3_ENDPOINT, S3_REGION, S3_BUCKET, S3_ACCESS_KEY_ID and S3_SECRET_ACCESS_KEY must be set together")
}
// The same options as web_server's backend uses for Garage.
client := s3.New(s3.Options{
BaseEndpoint: aws.String(cfg.Endpoint),
Region: cfg.Region,
Credentials: credentials.NewStaticCredentialsProvider(cfg.AccessKeyID, cfg.SecretAccessKey, ""),
// Garage addresses buckets by path (/bucket/key); virtual-host style
// would need a DNS name per bucket.
UsePathStyle: true,
// Recent SDKs add CRC32 checksums to every PUT by default. Only send
// them where S3 requires them, which is what third-party servers
// handle reliably.
RequestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
ResponseChecksumValidation: aws.ResponseChecksumValidationWhenRequired,
})
return &Store{client: client, bucket: cfg.Bucket}, nil
}
// Bucket is where everything goes.
func (s *Store) Bucket() string { return s.bucket }
// Garage is on the same Docker network, so anything near this is a hang.
const timeout = 2 * time.Minute
// Put stores one recording.
func (s *Store) Put(ctx context.Context, key, contentType string, b []byte) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
_, err := s.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
Body: bytes.NewReader(b),
ContentLength: aws.Int64(int64(len(b))),
ContentType: aws.String(contentType),
})
if err != nil {
return fmt.Errorf("s3 put %s/%s: %w", s.bucket, key, err)
}
return nil
}
// Object is a recording being read; the caller closes Body.
type Object struct {
Body io.ReadCloser
ContentType string
Size int64
}
// Get opens one recording.
func (s *Store) Get(ctx context.Context, key string) (*Object, error) {
out, err := s.client.GetObject(ctx, &s3.GetObjectInput{Bucket: aws.String(s.bucket), Key: aws.String(key)})
if err != nil {
var nk *types.NoSuchKey
if errors.As(err, &nk) {
return nil, ErrNotFound
}
return nil, fmt.Errorf("s3 get %s/%s: %w", s.bucket, key, err)
}
return &Object{Body: out.Body, ContentType: aws.ToString(out.ContentType), Size: aws.ToInt64(out.ContentLength)}, nil
}
// Delete removes one recording. A missing one is not an error: S3 does not
// report it either.
func (s *Store) Delete(ctx context.Context, key string) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
if _, err := s.client.DeleteObject(ctx, &s3.DeleteObjectInput{Bucket: aws.String(s.bucket), Key: aws.String(key)}); err != nil {
return fmt.Errorf("s3 delete %s/%s: %w", s.bucket, key, err)
}
return nil
}
@@ -0,0 +1,73 @@
package audiostore
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewNeedsAllOrNothing(t *testing.T) {
if s, err := New(Config{}); s != nil || err != nil {
t.Errorf("empty config = %v, %v; want nil, nil", s, err)
}
if _, err := New(Config{Endpoint: "http://garage:3900"}); err == nil {
t.Error("half a config should be an error")
}
}
// A fake S3 that holds objects in a map, addressed by path as Garage is.
func TestPutGetDelete(t *testing.T) {
objs := map[string][]byte{}
types := map[string]string{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPut:
b, _ := io.ReadAll(r.Body)
objs[r.URL.Path], types[r.URL.Path] = b, r.Header.Get("Content-Type")
case http.MethodGet:
b, ok := objs[r.URL.Path]
if !ok {
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(404)
io.WriteString(w, `<?xml version="1.0"?><Error><Code>NoSuchKey</Code><Message>gone</Message></Error>`)
return
}
w.Header().Set("Content-Type", types[r.URL.Path])
w.Write(b)
case http.MethodDelete:
delete(objs, r.URL.Path)
w.WriteHeader(204)
}
}))
defer srv.Close()
s, err := New(Config{Endpoint: srv.URL, Region: "garage", Bucket: "usaco-audio", AccessKeyID: "GK1", SecretAccessKey: "s"})
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
if err := s.Put(ctx, "moocast/2026-10-20-153000.mp3", "audio/mpeg", []byte("ID3")); err != nil {
t.Fatal(err)
}
if string(objs["/usaco-audio/moocast/2026-10-20-153000.mp3"]) != "ID3" {
t.Fatalf("objects = %v", objs)
}
o, err := s.Get(ctx, "moocast/2026-10-20-153000.mp3")
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(o.Body)
o.Body.Close()
if string(b) != "ID3" || o.ContentType != "audio/mpeg" {
t.Errorf("get = %q %q", b, o.ContentType)
}
if err := s.Delete(ctx, "moocast/2026-10-20-153000.mp3"); err != nil {
t.Fatal(err)
}
if _, err := s.Get(ctx, "moocast/2026-10-20-153000.mp3"); !errors.Is(err, ErrNotFound) {
t.Errorf("get after delete: %v, want ErrNotFound", err)
}
}