Files
Kaname/src/lib/publications.ts
T
Pascal LinxweilerandClaude Opus 5.5 ee6c623e2a
Release / release (push) Successful in 3m25s
Release 0.11.0: benchmark metadata and system info
Every run records GPUs (PCIe link under load and maximum, ReBAR, P2P),
runtime image/tag/digest and the engine version the server reports,
model, quant, KV cache, context and concurrency, plus a whitelisted
system summary and a description. Shown on the run page, compare and
sweep views; passed to BetterBench as notes and to the launch80 preview
as sections. Settings -> Hardware for manual values.

Also: scripts/intel-probe.sh (read-only diagnostics for Intel hosts),
and --quick is no longer passed together with --passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 19:07:04 +02:00

131 lines
5.9 KiB
TypeScript

import "server-only";
import { readFile, rm } from "node:fs/promises";
import { join } from "node:path";
import { and, desc, eq } from "drizzle-orm";
import { z } from "zod";
import { runDir } from "@/lib/benchmark-paths";
import { db } from "@/lib/db/client";
import { benchmarkPublications, benchmarkRuns, settings } from "@/lib/db/schema";
export class PublicationError extends Error {
constructor(
public code: "invalid" | "not_found" | "busy",
message: string,
public status: number,
) {
super(message);
}
}
/** Limits of the L80 CLI (internal/betterbench: maxTitle, maxSummary). */
export const PreviewBody = z.object({
title: z.string().trim().max(120).nullable().optional(),
summary: z.string().trim().max(600).nullable().optional(),
});
export type L80Status = { version: string | null; tokenSet: boolean; apiBase: string; checkedAt: string } | null;
export async function getL80Status(): Promise<L80Status> {
const row = await db().query.settings.findFirst({ where: eq(settings.key, "l80.status") });
return (row?.value as L80Status) ?? null;
}
export type PublicationRow = typeof benchmarkPublications.$inferSelect;
export async function listPublications(runId: string): Promise<PublicationRow[]> {
return db().select().from(benchmarkPublications).where(eq(benchmarkPublications.runId, runId)).orderBy(desc(benchmarkPublications.createdAt));
}
async function getPublication(runId: string, pubId: string) {
const p = await db().query.benchmarkPublications.findFirst({ where: and(eq(benchmarkPublications.id, pubId), eq(benchmarkPublications.runId, runId)) });
if (!p) throw new PublicationError("not_found", "Publication not found.", 404);
return p;
}
/** Opt-in 1: build the exact payload with a dry run (nothing leaves the machine). */
export async function requestPreview(runId: string, input: z.infer<typeof PreviewBody>) {
const run = await db().query.benchmarkRuns.findFirst({ columns: { state: true }, where: eq(benchmarkRuns.id, runId) });
if (!run) throw new PublicationError("not_found", "Benchmark not found.", 404);
if (run.state !== "completed") throw new PublicationError("invalid", "Only completed runs can be published.", 409);
const status = await getL80Status();
if (!status?.version) throw new PublicationError("invalid", "The L80 CLI isn't available in this installation.", 409);
const [row] = await db()
.insert(benchmarkPublications)
.values({ runId, title: input.title || null, summary: input.summary || null })
.returning({ id: benchmarkPublications.id });
return row;
}
/** Opt-in 2: an explicit confirmation of a previewed payload. */
export async function confirmPublication(runId: string, pubId: string, by: string | null) {
const p = await getPublication(runId, pubId);
if (p.state !== "previewed") throw new PublicationError("invalid", "Only a finished preview can be published.", 409);
const status = await getL80Status();
if (!status?.tokenSet) throw new PublicationError("invalid", "No launch80 token is configured (L80_TOKEN).", 409);
await db()
.update(benchmarkPublications)
.set({ state: "publish", confirmedBy: by, confirmedAt: new Date() })
.where(and(eq(benchmarkPublications.id, pubId), eq(benchmarkPublications.state, "previewed")));
}
/** Throw away a preview (published ones stay as a record). */
export async function discardPublication(runId: string, pubId: string) {
const p = await getPublication(runId, pubId);
if (p.state === "published" || p.state === "publish") throw new PublicationError("busy", "Published pages can't be withdrawn from here; they expire after 5 days.", 409);
await db().delete(benchmarkPublications).where(eq(benchmarkPublications.id, pubId));
await rm(payloadFile(runId, pubId), { force: true });
}
export function payloadFile(runId: string, pubId: string): string {
// Ids come from the URL; only ULIDs become a path.
if (!/^[0-9A-HJKMNP-TV-Z]{26}$/.test(pubId)) throw new PublicationError("not_found", "Publication not found.", 404);
return join(runDir(runId), "publications", `${pubId}.json`);
}
export type PayloadPreview = {
template: string;
bytes: number;
title: string | null;
summary: string | null;
model: string | null;
notes: Record<string, string>;
/** Page sections: the run's description, hardware/runtime facts and system summary. */
sections: { heading: string; body: string }[];
/** Top-level keys that will be sent, and env keys (endpoint/host must be absent). */
keys: string[];
envKeys: string[];
hostRemoved: boolean;
categories: string[];
phases: string[];
};
/** What the dry-run payload contains, read from the exact file that would be sent. */
export async function readPayloadPreview(runId: string, pubId: string): Promise<PayloadPreview | null> {
const p = await getPublication(runId, pubId);
const raw = await readFile(payloadFile(runId, p.id), "utf8").catch(() => null);
if (!raw) return null;
const data = JSON.parse(raw) as Record<string, unknown> & {
$template?: string;
env?: Record<string, unknown>;
l80?: { title?: string; summary?: string; sections?: { heading?: string; body?: string }[] };
single_stream?: Record<string, unknown>;
prefill?: unknown[];
concurrency?: unknown[];
};
const env = data.env ?? {};
return {
template: data.$template ?? p.template,
bytes: Buffer.byteLength(raw),
title: data.l80?.title ?? null,
summary: data.l80?.summary ?? null,
model: typeof env.model === "string" ? env.model : null,
notes: (env.notes as Record<string, string>) ?? {},
sections: (data.l80?.sections ?? []).map((s) => ({ heading: String(s.heading ?? ""), body: String(s.body ?? "") })),
keys: Object.keys(data),
envKeys: Object.keys(env),
hostRemoved: !("endpoint" in env) && !("host" in env),
categories: Object.keys(data.single_stream ?? {}),
phases: [data.single_stream ? "decode" : null, data.prefill?.length ? "prefill" : null, data.concurrency?.length ? "concurrency" : null].filter(Boolean) as string[],
};
}