Files
usaco/site/web/src/views/ProblemPage.vue
T
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

735 lines
21 KiB
Vue

<script setup>
/**
* One problem: its solution files, sample input, saved tests, notes,
* explanation and git history. The admin also gets the status buttons, the
* link/statement/notes editor, the Explain panel and recording reviews; each
* edit is a commit the server pushes, and the response is the problem as it
* is afterwards, so the page just takes it. An explanation or a review takes
* minutes, so starting one only queues it and the page polls until it is
* committed.
*/
import { computed, onUnmounted, ref, watch } from "vue";
import { RouterLink } from "vue-router";
import AudioRecorder from "@/components/AudioRecorder.vue";
import CodeBlock from "@/components/CodeBlock.vue";
import Explanation from "@/components/Explanation.vue";
import ReviewList from "@/components/ReviewList.vue";
import { markdown } from "@/markdown";
import {
explainStatus,
fetchStatement,
getMe,
getProblem,
reviewStatus,
setStatus,
startExplain,
startReview,
updateMeta,
} from "@/api";
import { LANG_LABELS, STATUS_LABELS, formatDate, title } from "@/format";
const props = defineProps({ name: { type: String, required: true } });
const md = markdown();
const problem = ref(null);
const admin = ref(false);
const loading = ref(true);
const error = ref("");
const activeFile = ref(0);
// The editor, Explain and Review forms fold into the admin bar, one open at
// a time, so the solution is not pushed below the fold. A running job is
// reported in the bar, so a panel can be closed while it works. Declared
// here because load() closes it and runs as soon as the watch below is set.
const panel = ref("");
async function load() {
loading.value = true;
error.value = "";
activeFile.value = 0;
panel.value = "";
try {
const [p, me] = await Promise.all([getProblem(props.name), getMe().catch(() => ({ admin: false }))]);
problem.value = p;
admin.value = me.admin;
if (me.admin) {
checkExplain();
checkReview();
}
} catch (e) {
problem.value = null;
error.value = e.status === 404 ? "No such problem." : e.message;
} finally {
loading.value = false;
}
}
watch(() => props.name, load, { immediate: true });
const notesHtml = computed(() => (problem.value?.notes ? md.render(problem.value.notes) : ""));
const statementHtml = computed(() => (problem.value?.statement ? md.render(problem.value.statement) : ""));
const file = computed(() => problem.value?.files[activeFile.value]);
// ---- admin tools ----
const tools = [
{ id: "edit", label: "Edit details" },
{ id: "explain", label: "Explain" },
{ id: "review", label: "Review a recording" },
];
function toggle(id) {
// The file input and recorder go with the panel, so the chosen recording
// must too.
if (panel.value === "review") recording.value = null;
if (panel.value === id) {
panel.value = "";
return;
}
if (id === "edit") startEdit();
panel.value = id;
}
const busy = computed(() => {
const out = [];
if (job.value.state === "running") out.push("Claude is writing an explanation…");
if (reviewJob.value.state === "uploading") out.push("Uploading the recording…");
if (reviewJob.value.state === "running") out.push("Transcribing and reviewing the recording… this takes a minute or two.");
return out;
});
// ---- admin edits ----
const saving = ref(false);
const editError = ref("");
const draftUrl = ref("");
const draftNotes = ref("");
const draftStatement = ref("");
const fetching = ref(false);
function startEdit() {
draftUrl.value = problem.value.url || "";
draftNotes.value = problem.value.notes || "";
draftStatement.value = problem.value.statement || "";
editError.value = "";
}
async function run(action) {
saving.value = true;
editError.value = "";
try {
problem.value = await action();
return true;
} catch (e) {
editError.value = e.message;
return false;
} finally {
saving.value = false;
}
}
async function saveMeta() {
const meta = { url: draftUrl.value, notes: draftNotes.value, statement: draftStatement.value };
if (await run(() => updateMeta(props.name, meta))) {
panel.value = "";
}
}
// Fills the statement box for review; nothing is saved until Save.
async function fillStatement() {
fetching.value = true;
editError.value = "";
try {
draftStatement.value = (await fetchStatement(props.name, draftUrl.value)).statement;
} catch (e) {
editError.value = e.message;
} finally {
fetching.value = false;
}
}
// ---- explanations ----
const stuckNote = ref("");
const includeCode = ref(true);
const job = ref({ state: "idle" });
let poll = null;
function stopPolling() {
clearTimeout(poll);
poll = null;
}
onUnmounted(stopPolling);
watch(() => props.name, stopPolling);
async function checkExplain() {
stopPolling();
const name = props.name;
try {
const s = await explainStatus(name);
if (name !== props.name) return;
const finished = job.value.state === "running" && s.state === "done";
job.value = s;
if (s.state === "running") {
poll = setTimeout(checkExplain, 3000);
} else if (finished) {
problem.value = await getProblem(name);
}
} catch (e) {
job.value = { state: "error", error: e.message };
}
}
async function explain() {
try {
job.value = await startExplain(props.name, { note: stuckNote.value, includeCode: includeCode.value });
panel.value = "";
poll = setTimeout(checkExplain, 3000);
} catch (e) {
job.value = { state: "error", error: e.message };
}
}
const canExplain = computed(() => !!(problem.value?.statement || problem.value?.url));
// ---- recording reviews ----
// The same shape as explanations, with its own job and timer: both can run
// at once.
const recording = ref(null);
const reviewNote = ref("");
const reviewCode = ref(true);
const reviewJob = ref({ state: "idle" });
let reviewPoll = null;
function stopReviewPolling() {
clearTimeout(reviewPoll);
reviewPoll = null;
}
onUnmounted(stopReviewPolling);
watch(() => props.name, stopReviewPolling);
// 25 MB is the transcriber's limit; checking here saves uploading it first.
const MAX_RECORDING = 25 << 20;
function useRecording(f) {
recording.value = f;
if (f && f.size > MAX_RECORDING) {
reviewJob.value = { state: "error", error: "That recording is over 25 MB; export it at a lower bitrate." };
} else if (reviewJob.value.state === "error") {
reviewJob.value = { state: "idle" };
}
}
function pickRecording(e) {
useRecording(e.target.files[0] || null);
}
// A take recorded here replaces a picked file, so the file input must not
// go on naming one.
const fileInput = ref(null);
function tookRecording(f) {
if (fileInput.value) fileInput.value.value = "";
useRecording(f);
}
function recorderError(message) {
reviewJob.value = { state: "error", error: message };
}
// Whichever recording is chosen gets a player, to check it before sending.
const recordingUrl = ref("");
watch(recording, (f) => {
URL.revokeObjectURL(recordingUrl.value);
recordingUrl.value = f ? URL.createObjectURL(f) : "";
});
onUnmounted(() => URL.revokeObjectURL(recordingUrl.value));
async function checkReview() {
stopReviewPolling();
const name = props.name;
try {
const s = await reviewStatus(name);
if (name !== props.name) return;
const finished = reviewJob.value.state === "running" && s.state === "done";
reviewJob.value = s;
if (s.state === "running") {
reviewPoll = setTimeout(checkReview, 3000);
} else if (finished) {
problem.value = await getProblem(name);
}
} catch (e) {
reviewJob.value = { state: "error", error: e.message };
}
}
async function submitReview() {
const form = new FormData();
form.append("audio", recording.value);
form.append("note", reviewNote.value);
form.append("includeCode", String(reviewCode.value));
reviewJob.value = { state: "uploading" };
try {
reviewJob.value = await startReview(props.name, form);
recording.value = null;
panel.value = "";
reviewPoll = setTimeout(checkReview, 3000);
} catch (e) {
reviewJob.value = { state: "error", error: e.message };
}
}
const canReview = computed(
() =>
canExplain.value &&
!!recording.value &&
recording.value.size <= MAX_RECORDING &&
!["uploading", "running"].includes(reviewJob.value.state),
);
const moves = computed(() => {
const s = problem.value?.status;
return [
{ to: "solved", label: "Mark solved" },
{ to: "skipped", label: "Skip" },
{ to: "solving", label: "Reopen" },
].filter((m) => m.to !== s);
});
</script>
<template>
<p v-if="loading" class="muted">Loading…</p>
<div v-else-if="error">
<p class="error">{{ error }}</p>
<RouterLink to="/">Back to the list</RouterLink>
</div>
<article v-else-if="problem" class="page">
<header class="head">
<RouterLink to="/" class="back">← All problems</RouterLink>
<h1>{{ title(problem.name) }}</h1>
<div class="facts">
<span class="badge" :class="problem.status">{{ STATUS_LABELS[problem.status] }}</span>
<a v-if="problem.url" :href="problem.url" target="_blank" rel="noopener noreferrer">
{{ problem.source || "Problem statement" }} ↗
</a>
<span v-if="problem.solvedAt" class="muted">Solved {{ formatDate(problem.solvedAt) }}</span>
<span v-else-if="problem.firstSeen" class="muted">Started {{ formatDate(problem.firstSeen) }}</span>
<span v-if="problem.io" class="muted">File I/O: <code>{{ problem.io }}.in</code></span>
</div>
</header>
<section v-if="admin" class="admin card">
<div class="admin-row">
<span class="muted">Admin</span>
<button v-for="m in moves" :key="m.to" class="btn" :disabled="saving" @click="run(() => setStatus(problem.name, m.to))">
{{ m.label }}
</button>
<span class="sep" aria-hidden="true"></span>
<button
v-for="t in tools"
:key="t.id"
class="btn tool"
:class="{ on: panel === t.id }"
:aria-expanded="panel === t.id"
:disabled="t.id === 'edit' && saving"
@click="toggle(t.id)"
>
{{ t.label }}
</button>
<RouterLink :to="`/p/${problem.name}/solve`" class="btn solve">Solve</RouterLink>
</div>
<div v-if="saving || busy.length" class="admin-row status" role="status">
<span v-if="saving" class="muted">Saving to git…</span>
<span v-for="b in busy" :key="b" class="working">{{ b }}</span>
</div>
<form v-if="panel === 'edit'" class="editor panel" @submit.prevent="saveMeta">
<label>
Problem link
<input v-model="draftUrl" type="url" placeholder="https://usaco.org/index.php?page=viewproblem2&cpid=…" />
</label>
<label>
<span class="label-row">
Problem statement (markdown, shown only to you and Claude)
<button class="btn small" type="button" :disabled="fetching || !draftUrl" @click="fillStatement">
{{ fetching ? "Fetching…" : "Fetch from link" }}
</button>
</span>
<textarea v-model="draftStatement" rows="8" placeholder="Paste the statement, or fetch it from a LeetCode or USACO link"></textarea>
</label>
<label>
Notes (markdown)
<textarea v-model="draftNotes" rows="8" placeholder="Approach, complexity, what tripped you up…"></textarea>
</label>
<div class="admin-row">
<button class="btn primary" type="submit" :disabled="saving">Save</button>
<button class="btn" type="button" :disabled="saving" @click="panel = ''">Cancel</button>
</div>
</form>
<div v-if="panel === 'explain'" class="panel">
<h2>Stuck?</h2>
<p class="muted">
Claude writes an intuitive explanation with diagrams, laid out as hints you open one at a time. It takes a
minute or two and is committed as <code>explain.md</code>.
</p>
<textarea v-model="stuckNote" rows="3" placeholder="Optional: what you've tried and where you're stuck"></textarea>
<div class="admin-row">
<label class="check"><input v-model="includeCode" type="checkbox" /> Include my current code</label>
<button class="btn primary" :disabled="job.state === 'running' || !canExplain" @click="explain">
{{ problem.explanation ? "Explain again" : "Explain" }}
</button>
<span v-if="!canExplain" class="muted">Add a link or the statement first.</span>
</div>
</div>
<div v-if="panel === 'review'" class="panel">
<h2>Review my explanation</h2>
<p class="muted">
Record yourself explaining your solution as you would to an interviewer, here or elsewhere and upload it
(mp3, m4a, wav, webm, ogg or flac, up to 25 MB). It is transcribed, and Claude gives feedback on the approach
and on your delivery. Each review is committed under <code>reviews/</code>.
</p>
<div class="source">
<AudioRecorder
:max-bytes="MAX_RECORDING"
@start="useRecording(null)"
@recorded="tookRecording"
@error="recorderError"
/>
<span class="muted">or</span>
<input ref="fileInput" type="file" accept="audio/*,.mp3,.m4a,.wav,.webm,.ogg,.flac" @change="pickRecording" />
</div>
<audio v-if="recordingUrl" :src="recordingUrl" controls preload="metadata"></audio>
<textarea v-model="reviewNote" rows="2" placeholder="Optional context: a mock interview, a time limit, what to focus on"></textarea>
<div class="admin-row">
<label class="check"><input v-model="reviewCode" type="checkbox" /> Include my code</label>
<button class="btn primary" :disabled="!canReview" @click="submitReview">Review</button>
<span v-if="!canExplain" class="muted">Add a link or the statement first.</span>
</div>
</div>
<!-- Outside the panels, so a job that fails after its panel was
closed still says so. -->
<p v-if="editError" class="error">{{ editError }}</p>
<p v-if="job.state === 'error'" class="error">Explain: {{ job.error }}</p>
<p v-if="reviewJob.state === 'error'" class="error">Review: {{ reviewJob.error }}</p>
</section>
<section v-if="notesHtml" class="notes card prose" v-html="notesHtml"></section>
<section v-if="problem.explanation">
<h2>Explanation</h2>
<Explanation :source="problem.explanation" />
</section>
<section v-if="admin && problem.reviews.length">
<h2>Reviews</h2>
<ReviewList :name="problem.name" :reviews="problem.reviews" @changed="(p) => (problem = p)" />
</section>
<details v-if="admin && statementHtml" class="statement card">
<summary>Problem statement</summary>
<div class="prose" v-html="statementHtml"></div>
</details>
<section>
<h2>Solution</h2>
<p v-if="!problem.files.length" class="muted">No solution file yet.</p>
<template v-else>
<div v-if="problem.files.length > 1" class="file-tabs" role="tablist">
<button
v-for="(f, i) in problem.files"
:key="f.name"
role="tab"
:aria-selected="i === activeFile"
:class="{ on: i === activeFile }"
@click="activeFile = i"
>
{{ f.name }} <span class="muted">{{ LANG_LABELS[f.lang] || f.lang }}</span>
</button>
</div>
<p v-else class="filename muted">{{ file.name }}</p>
<CodeBlock :key="file.name" :content="file.content" :lang="file.lang" :truncated="file.truncated" />
</template>
</section>
<section v-if="problem.sample">
<h2>Sample input</h2>
<p class="filename muted">{{ problem.sample.name }}</p>
<CodeBlock :content="problem.sample.content" :truncated="problem.sample.truncated" />
</section>
<section v-if="problem.testCases.length">
<h2>Tests</h2>
<details v-for="t in problem.testCases" :key="t.name" class="test card">
<summary>Test {{ t.name }}</summary>
<div class="io">
<div>
<p class="filename muted">Input</p>
<CodeBlock :content="t.input.content" :truncated="t.input.truncated" />
</div>
<div>
<p class="filename muted">Expected output</p>
<CodeBlock v-if="t.output" :content="t.output.content" :truncated="t.output.truncated" />
<p v-else class="muted">Not saved.</p>
</div>
</div>
</details>
</section>
<section v-if="problem.history.length">
<h2>History</h2>
<ol class="history">
<li v-for="c in problem.history" :key="c.hash">
<time :datetime="c.date" class="muted">{{ formatDate(c.date) }}</time>
<span>{{ c.subject }}</span>
<code class="muted">{{ c.hash.slice(0, 7) }}</code>
</li>
</ol>
</section>
</article>
</template>
<style scoped>
.page {
display: grid;
gap: 28px;
min-width: 0;
}
.page > section {
min-width: 0;
}
.back {
font-size: 0.875rem;
color: var(--color-ink-muted);
text-decoration: none;
}
.back:hover {
color: var(--color-ink);
}
h1 {
font-size: 2.75rem;
margin: 6px 0 10px;
overflow-wrap: anywhere;
}
h2 {
font-size: 1.6rem;
margin-bottom: 8px;
}
.facts {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 6px 16px;
font-size: 0.9rem;
}
.facts code,
.history code {
font-family: var(--font-mono);
font-size: 0.85em;
}
.admin {
padding: 12px 16px;
display: grid;
gap: 12px;
}
.admin-row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.editor {
display: grid;
gap: 12px;
}
.editor label {
display: grid;
gap: 4px;
font-size: 0.875rem;
color: var(--color-ink-muted);
}
.editor input,
.editor textarea {
padding: 8px 10px;
border: 1px solid var(--color-line);
border-radius: 4px;
font-family: var(--font-mono);
font-size: 0.85rem;
color: var(--color-ink);
background: var(--color-paper);
}
.notes {
padding: 16px 20px;
}
.label-row {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.btn.small {
padding: 2px 10px;
font-size: 0.8rem;
}
.sep {
width: 1px;
align-self: stretch;
margin: 0 4px;
background: var(--color-line-soft);
}
/* On a phone the buttons wrap, and the divider would end a line. */
@media (max-width: 639px) {
.sep {
display: none;
}
}
.btn.solve {
text-decoration: none;
}
.btn.tool.on {
border-color: var(--color-ink);
background: var(--color-paper-shade);
}
.status {
font-size: 0.9rem;
}
/* A pulsing dot: the job is still going, and the page is polling for it. */
.working::before {
content: "";
display: inline-block;
width: 8px;
height: 8px;
margin-right: 8px;
border-radius: 50%;
background: var(--color-solving);
animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.25;
}
}
@media (prefers-reduced-motion: reduce) {
.working::before {
animation: none;
}
}
.panel {
display: grid;
gap: 10px;
padding-top: 12px;
border-top: 1px solid var(--color-line-soft);
}
.panel h2 {
margin: 0;
}
.panel p {
margin: 0;
font-size: 0.9rem;
}
.panel code {
font-family: var(--font-mono);
font-size: 0.85em;
}
.panel textarea {
padding: 8px 10px;
border: 1px solid var(--color-line);
border-radius: 4px;
font-size: 0.9rem;
color: var(--color-ink);
background: var(--color-paper);
}
.source {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 10px;
}
.panel audio {
width: 100%;
max-width: 480px;
}
.panel input[type="file"] {
font-size: 0.9rem;
max-width: 100%;
}
.panel input[type="file"]::file-selector-button {
margin-right: 10px;
padding: 6px 14px;
border: 1px solid var(--color-line);
border-radius: 4px;
background: var(--color-paper);
color: var(--color-ink);
cursor: pointer;
}
.panel input[type="file"]::file-selector-button:hover {
border-color: var(--color-ink-muted);
}
.check {
display: flex;
align-items: center;
gap: 6px;
font-size: 0.9rem;
}
.statement summary {
padding: 8px 14px;
cursor: pointer;
}
.statement .prose {
padding: 4px 20px 16px;
overflow-wrap: anywhere;
}
.file-tabs {
display: flex;
flex-wrap: wrap;
gap: 4px;
margin-bottom: 8px;
}
.file-tabs button {
padding: 4px 12px;
border: 1px solid var(--color-line-soft);
border-radius: 4px;
font-family: var(--font-mono);
font-size: 0.8rem;
cursor: pointer;
}
.file-tabs button.on {
border-color: var(--color-ink);
background: var(--color-paper);
}
.filename {
font-family: var(--font-mono);
font-size: 0.8rem;
margin-bottom: 4px;
}
.test {
margin-bottom: 8px;
}
.test summary {
padding: 8px 14px;
cursor: pointer;
}
.io {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 12px;
padding: 0 14px 14px;
min-width: 0;
}
.io > div {
min-width: 0;
}
.history {
display: grid;
gap: 4px;
font-size: 0.9rem;
}
.history li {
display: flex;
flex-wrap: wrap;
gap: 4px 12px;
}
.history time {
min-width: 7.5em;
}
</style>