Files
adamfandClaude Opus 5.5 c972fa9fd9
Test and publish the site / test (push) Successful in 1m23s
Test and publish the site / publish (push) Failing after 4m22s
Run and lint solutions on the site
A new admin-only solve page, /p/<name>/solve, edits a problem's code in
C++ or Python (a dropdown switches between them), runs it on custom
input or against the saved tests with per-test verdicts (AC, WA, RE,
TLE, unchecked) and compile errors, marks lint diagnostics in the
editor, and saves the code back as a commit ("Edit <name>"). Drafts
stay in the browser per problem and language until saved. LeetCode
problems are C++ only, since their tests run through driver.h.

Code never runs in the site, which holds the Claude, Gitea and S3
secrets. A separate runner service (cmd/usacorunner, Dockerfile.runner)
does it in a container meant to have no environment, an internal-only
network, a read-only root apart from /tmp, and memory, CPU and pids
limits, with tini to reap orphans. Runs copy the CLI: the same g++
flags with -DLOCAL and the repo's headers, <io>.in on disk and on
stdin, <io>.out or stdout as the output, and usaco test's comparison.
Unlike the CLI they have CPU, memory, file and wall-clock limits.

C++ lint is g++'s warnings plus clang-tidy with a new .clang-tidy
(bugprone, performance and a few readability checks; template.cpp is
clean). Python lint is a syntax check plus ruff. template.py is the
Python starter, with a stdio() helper like template.rs's.

CI builds, smoke-tests and rolls out the runner image before the site.
The web_server stack needs a usaco-runner service before that rollout
can succeed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-20 11:24:22 +01:00

75 lines
1.9 KiB
Go

package runner
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// Client is the site's side: it posts to the runner service at BaseURL.
type Client struct {
BaseURL string
HTTP *http.Client
}
// NewClient talks to the runner at baseURL (RUNNER_URL). The timeout is
// above the service's own per-request deadline, so a slow run comes back as
// the service's answer rather than a client timeout.
func NewClient(baseURL string) *Client {
return &Client{
BaseURL: strings.TrimRight(baseURL, "/"),
HTTP: &http.Client{Timeout: 90 * time.Second},
}
}
// Run compiles req.Source once and runs it on every test.
func (c *Client) Run(ctx context.Context, req RunRequest) (RunResponse, error) {
var out RunResponse
err := c.post(ctx, "/run", req, &out)
return out, err
}
// Lint returns the source's diagnostics.
func (c *Client) Lint(ctx context.Context, req LintRequest) (LintResponse, error) {
var out LintResponse
err := c.post(ctx, "/lint", req, &out)
return out, err
}
func (c *Client) post(ctx context.Context, path string, in, out any) error {
body, err := json.Marshal(in)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL+path, bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTP.Do(req)
if err != nil {
return fmt.Errorf("runner: %w", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(io.LimitReader(resp.Body, 16<<20))
if err != nil {
return fmt.Errorf("runner: %w", err)
}
if resp.StatusCode != http.StatusOK {
// The service answers errors as {"error": "..."}, meant for the admin.
var e struct {
Error string `json:"error"`
}
if json.Unmarshal(data, &e) == nil && e.Error != "" {
return fmt.Errorf("runner: %s", e.Error)
}
return fmt.Errorf("runner: %s", resp.Status)
}
return json.Unmarshal(data, out)
}