import type {
RunnerE2EHistoryCampaign,
RunnerE2EHistoryIndex,
} from "./types.js";
function html(value: unknown) {
return String(value ?? "")
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
function number(value: number) {
return new Intl.NumberFormat("en-US").format(value);
}
function usd(value: number) {
return `$${value.toFixed(value < 0.01 ? 6 : 2)}`;
}
function duration(durationMs: number) {
const seconds = Math.round(durationMs / 1_000);
if (seconds < 60) return `${seconds}s`;
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return `${minutes}m ${seconds % 60}s`;
return `${Math.floor(minutes / 60)}h ${minutes % 60}m`;
}
function date(value: string) {
return new Intl.DateTimeFormat("en-US", {
dateStyle: "medium",
timeStyle: "short",
timeZone: "UTC",
}).format(new Date(value));
}
function safeRelativeAssetHref(relative: string | undefined) {
if (!relative || /^(?:[a-z]+:|\/\/|\/)/i.test(relative)) return null;
const segments = relative.split("/");
if (
segments.some((segment) => !segment || segment === "." || segment === "..")
) {
return null;
}
return segments.map(encodeURIComponent).join("/");
}
function campaignStatus(campaign: RunnerE2EHistoryCampaign) {
return campaign.failed === 0 &&
campaign.passed === campaign.selected &&
campaign.executed === campaign.selected &&
campaign.cleanupPassed
? "passed"
: "failed";
}
function sourceCell(campaign: RunnerE2EHistoryCampaign) {
const sha = campaign.source.sha;
const shortSha = sha?.slice(0, 8) ?? "Unknown";
const shaLabel = /^[0-9a-f]{40}$/i.test(sha ?? "")
? `${html(shortSha)} `
: html(shortSha);
const workflow = campaign.source.workflowRunUrl
? `Workflow run `
: "";
return `${shaLabel}${html(campaign.source.ref ?? "Unknown ref")}${workflow} `;
}
function campaignRow(campaign: RunnerE2EHistoryCampaign) {
const status = campaignStatus(campaign);
const suites = campaign.suites
.map(
(suite) =>
`${html(suite.suiteId)} · ${suite.passed}/${suite.selected} `,
)
.join("");
const billing = campaign.billing;
return `
${html(campaign.campaignId)}
${html(date(campaign.generatedAt))} UTC
${status}
${campaign.complete ? "Complete campaign" : "Partial campaign"}${campaign.retries > 0 ? ` · ${campaign.retries} retries` : ""}
${sourceCell(campaign)}
${suites}
${campaign.passed}/${campaign.selected} passed
${campaign.executed} executed · ${campaign.failed} failed
${html(number(billing.llm.totalTokens))}
${html(number(billing.llm.inputTokens))} in · ${html(number(billing.llm.outputTokens))} out · ${html(number(billing.llm.cachedInputTokens))} cached
${html(usd(billing.observedAndEstimatedCostUsd))}
${html(usd(billing.reportedLlmCostUsd))} LLM · ${html(usd(billing.estimatedRuntimeCostUsd))} runtime
${html(duration(billing.agentRunDurationMs))}
${html(duration(billing.leaseDurationMs))} Daytona lease
Open report →
`;
}
export function renderRunnerHistoryIndex(
history: RunnerE2EHistoryIndex,
options: { latestSummaryImageHref?: string } = {},
) {
const campaigns = [...history.campaigns].sort((left, right) =>
right.generatedAt.localeCompare(left.generatedAt),
);
const passed = campaigns.filter(
(campaign) => campaignStatus(campaign) === "passed",
).length;
const latest = campaigns.find(
(campaign) => campaign.campaignId === history.latestCampaignId,
);
const latestGreen = campaigns.find(
(campaign) => campaign.campaignId === history.latestGreenCampaignId,
);
const totalCost = campaigns.reduce(
(sum, campaign) => sum + campaign.billing.observedAndEstimatedCostUsd,
0,
);
const rows =
campaigns.length > 0
? campaigns.map(campaignRow).join("")
: `No campaigns have been published yet. `;
const latestSummaryImageHref = safeRelativeAssetHref(
options.latestSummaryImageHref,
);
return `
Runner E2E Campaigns · Paperclip
Paperclip
Quality engineering · Runner acceptance
Historical test reporting
Runner E2E campaigns
Each row is one workflow campaign against a Paperclip revision. Open a report for its configuration matrices, matchers, per-test billing, declared screenshots, and sanitized structured evidence. Additional diagnostic evidence remains in access-controlled workflow artifacts.
${campaigns.length} Campaigns
${passed} Passed
${html(usd(totalCost))} Recorded cost
${latestSummaryImageHref ? ` ` : ""}
${latest ? `Latest run · ${html(latest.campaignId)} ` : ""}
${latestGreen ? `Latest complete green · ${html(latestGreen.campaignId)} ` : ""}
Campaign Status Source Suites Tests Tokens Cost Agent time
${rows}
Updated ${html(date(history.updatedAt))} UTC Immutable campaign reports · Declared screenshots and inert structured evidence
`;
}