Keep site jobs across restarts, meter Claude spend, harden admin routes
Test and publish the site / test (push) Successful in 2m15s
Test and publish the site / publish (push) Successful in 9m5s

- internal/jobs replaces explain.Jobs: one registry for every kind of
  background job, with records in DATA_DIR/jobs. A job's draft is written
  before its commit, so a failed commit can be saved again (POST
  /jobs/<kind>/<key>/save) and a restart mid-commit resumes it, without
  paying Claude twice.
- Graceful shutdown: SIGTERM stops new jobs and waits SHUTDOWN_GRACE for
  running ones while still serving the pages that poll them.
- internal/usage records every Claude call's tokens and estimated cost in
  DATA_DIR/usage.jsonl; CLAUDE_MONTHLY_BUDGET refuses new jobs (429) once a
  month reaches it. The stats page shows the admin the spend.
- auth.SameOrigin refuses cross-origin writes to admin routes.
- The LeetCode importer runs the clone's script without the server's
  secrets in its environment.
- A sync that leaves HEAD where it was skips the re-index.
- docs_test.go checks README's route table against the routes, and the CLI
  page against the root README's commands.
- The repo is going private: Gitea links on the site are admin-only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-23 22:10:01 +01:00
co-authored by Claude Opus 5.5
parent 7638d88570
commit cae5ec8e5a
42 changed files with 1849 additions and 264 deletions
-1
View File
@@ -20,7 +20,6 @@ const route = useRoute();
<RouterLink to="/concepts" :class="{ on: route.name === 'concept' }" active-class="on">Concepts</RouterLink>
<RouterLink to="/stats" active-class="on">Stats</RouterLink>
<RouterLink to="/cli" active-class="on">CLI</RouterLink>
<a href="/gitea/adamf/usaco">Source</a>
<a href="/">adam-french.co.uk</a>
</nav>
</header>
+10
View File
@@ -83,3 +83,13 @@ export const conceptJob = (id) => request("GET", `/concept-jobs/${encodeURICompo
export const rewriteConcept = (slug, body) => request("POST", `/concepts/${encodeURIComponent(slug)}/explain`, body);
export const rewriteStatus = (slug) => request("GET", `/concepts/${encodeURIComponent(slug)}/explain`);
export const deleteConcept = (slug) => request("DELETE", `/concepts/${encodeURIComponent(slug)}`);
// Every job status carries its kind and key. One that ends with unsaved:
// true wrote its answer but could not commit it; this commits that answer
// again, without asking Claude again, and answers the job (running), which
// is then polled at its usual status route.
export const saveJobAgain = (job) =>
request("POST", `/jobs/${encodeURIComponent(job.kind)}/${encodeURIComponent(job.key)}/save`);
// The admin's Claude spend: { tracked, budget, month, months, kinds }.
export const getUsage = () => request("GET", "/usage");
+19 -6
View File
@@ -2,13 +2,23 @@
/**
* How to use ./usaco, the workspace CLI. A condensed copy of the repo
* README's Commands section: the Docker build context is site/, so the README
* itself can't be imported. Keep the two in step when the CLI changes.
* itself can't be imported. Keep the two in step when the CLI changes;
* site/docs_test.go fails when a command in the README is missing here.
*/
import { ref } from "vue";
import { getMe } from "@/api";
// The repo on the site's Gitea, behind the same nginx, so a root-relative
// link works.
// link works. It is private (it holds the judges' statements and the
// admin's review transcripts), so only the admin gets links into it; for
// anyone else they would be 404s, and the names render as plain text.
const repo = "/gitea/adamf/usaco";
const src = (path) => `${repo}/src/branch/main/${path}`;
const admin = ref(false);
getMe()
.then((me) => (admin.value = me.admin))
.catch(() => {});
const src = (path) => (admin.value ? (path ? `${repo}/src/branch/main/${path}` : repo) : undefined);
// Every command that takes a problem name accepts a unique prefix, so the
// examples use the full name only where it reads better.
@@ -91,7 +101,7 @@ usaco [watermelon:solved]> q`;
<h1>The usaco CLI</h1>
<p class="muted">
Every problem here is written in a workspace managed by <a :href="src('usaco')"><code>./usaco</code></a>, a small bash script at
the root of <a :href="repo">the repo on Gitea</a> (the commands themselves are in
the root of <a :href="src('')">the repo on Gitea</a> (the commands themselves are in
<a :href="src('lib/cmd')"><code>lib/cmd/</code></a>). Each problem lives in <code>solving/</code>, <code>solved/</code> or
<code>skipped/</code> as a directory with <code>main.cpp</code> or <code>main.rs</code> and its <code>.in</code>/<code>.out</code>
files. Wherever a command takes a problem name, a unique prefix works too:
@@ -102,8 +112,11 @@ usaco [watermelon:solved]> q`;
<section>
<h2>Setup</h2>
<div class="card body">
<p>Clone the repo and run <code>./usaco</code> from its root:</p>
<pre><code>git clone https://www.adam-french.co.uk{{ repo }}.git</code></pre>
<template v-if="admin">
<p>Clone the repo and run <code>./usaco</code> from its root:</p>
<pre><code>git clone https://www.adam-french.co.uk{{ repo }}.git</code></pre>
</template>
<p v-else>The repo is private. In a clone of it, run <code>./usaco</code> from its root.</p>
<p>Or add this to <code>~/.zshrc</code>:</p>
<pre><code>source ~/Coding/USACO/<a :href="src('usaco.zsh')">usaco.zsh</a></code></pre>
<p>
+16 -2
View File
@@ -11,7 +11,7 @@
*/
import { computed, onMounted, onUnmounted, ref } from "vue";
import { RouterLink, useRoute, useRouter } from "vue-router";
import { conceptJob, listConcepts, startConcept } from "@/api";
import { conceptJob, listConcepts, saveJobAgain, startConcept } from "@/api";
import { formatDate } from "@/format";
const route = useRoute();
@@ -84,6 +84,17 @@ async function ask() {
}
}
// Written but not committed: commit the same answer again.
async function saveAgain() {
try {
job.value = await saveJobAgain(job.value);
remember(job.value.key);
poll = setTimeout(() => check(job.value.key), 3000);
} catch (e) {
job.value = { state: "error", error: e.message };
}
}
const canAsk = computed(
() => job.value.state !== "running" && (topic.value.trim() || (showCode.value && code.value.trim())),
);
@@ -147,7 +158,10 @@ onUnmounted(() => clearTimeout(poll));
</button>
<span v-if="job.state === 'running'" class="muted">This takes a minute or two; the concept opens when it is saved.</span>
</div>
<p v-if="job.state === 'error'" class="error">{{ job.error }}</p>
<p v-if="job.state === 'error'" class="error">
{{ job.error }}
<button v-if="job.unsaved" class="btn" type="button" @click="saveAgain">Save again</button>
</p>
</form>
<div class="controls">
+14 -2
View File
@@ -7,7 +7,7 @@
*/
import { onUnmounted, ref, watch } from "vue";
import { RouterLink, useRouter } from "vue-router";
import { deleteConcept, getConcept, getMe, rewriteConcept, rewriteStatus } from "@/api";
import { deleteConcept, getConcept, getMe, rewriteConcept, rewriteStatus, saveJobAgain } from "@/api";
import { formatDate } from "@/format";
import CodeBlock from "@/components/CodeBlock.vue";
import Explanation from "@/components/Explanation.vue";
@@ -81,6 +81,15 @@ async function rewrite() {
}
}
async function saveAgain() {
try {
job.value = await saveJobAgain(job.value);
poll = setTimeout(checkRewrite, 3000);
} catch (e) {
job.value = { state: "error", error: e.message };
}
}
async function forget() {
if (!confirm(`Delete “${concept.value.title}” and its snippet?`)) return;
actionError.value = "";
@@ -130,7 +139,10 @@ async function forget() {
<button class="btn" @click="panel = ''">Cancel</button>
</div>
</div>
<p v-if="job.state === 'error'" class="error">Rewrite: {{ job.error }}</p>
<p v-if="job.state === 'error'" class="error">
Rewrite: {{ job.error }}
<button v-if="job.unsaved" class="btn" @click="saveAgain">Save again</button>
</p>
<p v-if="actionError" class="error">{{ actionError }}</p>
</section>
+28 -2
View File
@@ -21,6 +21,7 @@ import {
getMe,
getProblem,
reviewStatus,
saveJobAgain,
setStatus,
startExplain,
startReview,
@@ -186,6 +187,16 @@ async function explain() {
}
}
// The explanation was written but not committed: commit it again.
async function saveExplainAgain() {
try {
job.value = await saveJobAgain(job.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 ----
@@ -258,6 +269,15 @@ async function checkReview() {
}
}
async function saveReviewAgain() {
try {
reviewJob.value = await saveJobAgain(reviewJob.value);
reviewPoll = setTimeout(checkReview, 3000);
} catch (e) {
reviewJob.value = { state: "error", error: e.message };
}
}
async function submitReview() {
const form = new FormData();
form.append("audio", recording.value);
@@ -409,8 +429,14 @@ const moves = computed(() => {
<!-- 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>
<p v-if="job.state === 'error'" class="error">
Explain: {{ job.error }}
<button v-if="job.unsaved" class="btn" @click="saveExplainAgain">Save again</button>
</p>
<p v-if="reviewJob.state === 'error'" class="error">
Review: {{ reviewJob.error }}
<button v-if="reviewJob.unsaved" class="btn" @click="saveReviewAgain">Save again</button>
</p>
</section>
<section v-if="notesHtml" class="notes card prose" v-html="notesHtml"></section>
+47 -1
View File
@@ -4,13 +4,15 @@
* headline counts, a year of solves as a calendar heatmap, and the same data
* by month as a table (the heatmap's accessible equivalent).
*
* The admin also sees what Claude has cost (GET /usage, the server's ledger).
*
* Days are the viewer's local days (format.dayKey). The solve date is the
* commit that moved a problem into solved/, so a batch of problems committed
* together lands on one day.
*/
import { computed, nextTick, onMounted, ref } from "vue";
import { RouterLink } from "vue-router";
import { listProblems } from "@/api";
import { getMe, getUsage, listProblems } from "@/api";
import { dayKey, formatDate, title } from "@/format";
const problems = ref([]);
@@ -18,7 +20,16 @@ const loading = ref(true);
const error = ref("");
const scroller = ref(null);
// The Claude spend; null for anyone but the admin.
const spend = ref(null);
const money = (n) => `$${n.toFixed(2)}`;
const KIND_LABELS = { explain: "Explanations", concept: "Concepts", review: "Reviews", evaluate: "Evaluations" };
onMounted(async () => {
getMe()
.then((me) => (me.admin ? getUsage() : null))
.then((u) => (spend.value = u?.tracked ? u : null))
.catch(() => {});
try {
problems.value = (await listProblems()).problems;
} catch (e) {
@@ -224,6 +235,38 @@ const monthly = computed(() => {
</table>
<p v-else class="muted">Nothing solved yet.</p>
</section>
<section v-if="spend">
<h2>Claude spend</h2>
<p class="muted">
{{ money(spend.month) }} this month<template v-if="spend.budget"> of a {{ money(spend.budget) }} budget</template>.
Estimated from token counts at list prices; transcription is not included.
</p>
<table v-if="spend.months.length" class="card">
<thead>
<tr><th scope="col">Month</th><th scope="col" class="num">Calls</th><th scope="col" class="num">Cost</th></tr>
</thead>
<tbody>
<tr v-for="m in spend.months" :key="m.name">
<td>{{ formatDate(m.name + "-01T00:00:00", { month: "long", year: "numeric" }) }}</td>
<td class="num">{{ m.calls }}</td>
<td class="num">{{ money(m.cost) }}</td>
</tr>
</tbody>
</table>
<table v-if="spend.kinds.length" class="card">
<thead>
<tr><th scope="col">This month, by feature</th><th scope="col" class="num">Calls</th><th scope="col" class="num">Cost</th></tr>
</thead>
<tbody>
<tr v-for="k in spend.kinds" :key="k.name">
<td>{{ KIND_LABELS[k.name] || k.name }}</td>
<td class="num">{{ k.calls }}</td>
<td class="num">{{ money(k.cost) }}</td>
</tr>
</tbody>
</table>
</section>
</div>
</template>
@@ -259,6 +302,9 @@ const monthly = computed(() => {
.stats > section {
min-width: 0;
}
table + table {
margin-top: 12px;
}
h2 {
font-size: 1.6rem;
margin-bottom: 8px;