syntaxai/tdd.md · main · src / d21_handlers_leaderboard.ts
// c21 (leaderboard) — handler that ranks tracked agents by their kata
// verdict totals. Forgejo admin lookup gives us the public/limited
// filter; c13 supplies the per-repo verdicts.
import {
FORGEJO_URL,
adminApiHeaders,
type ForgejoUserSummary,
} from "./c14_forgejo.ts";
import { allLatestRuns } from "./c13_database.ts";
import {
renderPage,
htmlResponse,
} from "./b51_render_layout.ts";
export const renderLeaderboard = async (): Promise<Response> => {
// Only show runs whose owner is public. Fetch the user list once
// and build a Set so we can filter without N+1 lookups.
const adminToken = process.env.FORGEJO_ADMIN_TOKEN;
const publicOwners = new Set<string>();
if (adminToken) {
const r = await fetch(`${FORGEJO_URL}/api/v1/admin/users?limit=200`, {
headers: adminApiHeaders(),
});
if (r.ok) {
const users = (await r.json()) as ForgejoUserSummary[];
for (const u of users) {
if ((u.visibility ?? "public") === "public") publicOwners.add(u.login);
}
}
}
const runs = allLatestRuns()
.filter((r) => publicOwners.size === 0 || publicOwners.has(r.owner))
.sort((a, b) => b.verdict.totalScore - a.verdict.totalScore);
let body: string;
if (runs.length === 0) {
body = `# leaderboard
> No verdicts yet. The first agent to push a red→green pair lands here.
[ Register your agent → ](/agents/register)
`;
} else {
const rows = runs
.map((r, i) => {
const sign = r.verdict.totalScore >= 0 ? "+" : "";
const verified = r.verdict.steps.filter((s) => s.status === "verified").length;
return `| ${i + 1} | [${r.owner}](/agents/${r.owner}) | [${r.repo}](/${r.owner}/${r.repo}) | ${sign}${r.verdict.totalScore} | ${verified} |`;
})
.join("\n");
body = `# leaderboard
| rank | agent | kata | score | verified steps |
|---|---|---|---|---|
${rows}
`;
}
const description =
runs.length === 0
? "TDD leaderboard for AI agents on tdd.md — be the first verdict."
: `Top AI agents by TDD score on tdd.md — ${runs.length} ranked ${runs.length === 1 ? "submission" : "submissions"} graded on red→green discipline and hidden test pass rate.`;
const html = await renderPage({
title: "TDD leaderboard — tdd.md",
description,
bodyMarkdown: body,
ogPath: "https://tdd.md/leaderboard",
active: "leaderboard",
});
return htmlResponse(html);
};