import { mkdir, writeFile } from "node:fs/promises"; import { dirname } from "node:path"; import { analyzeSessions, compareBenchmark, type SessionMetrics, type TaskSuccess, } from "../src/session-benchmark.js"; const RECORDED_BASELINE: SessionMetrics = { sessions: 1, requests: 0, failedRequests: 0, failureRate: 1.57, totalTokens: 24_780_823, cost: 7.5739, }; interface Arguments { candidate: string[]; baseline: string[]; taskSuccess?: TaskSuccess; output?: string; } function parseArguments(values: string[]): Arguments { const result: Arguments = { candidate: [], baseline: [] }; for (let index = 0; index < values.length; index++) { const option = values[index]; const value = values[index + 1]; if (option === "--candidate" && value) { result.candidate.push(value); index += 1; } else if (option === "--baseline" && value) { result.baseline.push(value); index += 1; } else if (option === "--task-success" && value) { if (value !== "equal" && value !== "better" && value !== "worse") { throw new Error("--task-success must be equal, better, or worse"); } result.taskSuccess = value; index += 1; } else if (option === "--output" && value) { result.output = value; index += 1; } else { throw new Error(`Unknown or incomplete option: ${option}`); } } if (result.candidate.length === 0) { throw new Error("At least one --candidate file or directory is required"); } if (!result.taskSuccess) throw new Error("--task-success is required"); if (!result.output) throw new Error("--output is required"); return result; } function metric(value: number, digits = 2): string { return value.toLocaleString("en-US", { minimumFractionDigits: digits, maximumFractionDigits: digits, }); } function reportMarkdown(input: { baseline: SessionMetrics; candidate: SessionMetrics; baselineLabel: string; taskSuccess: TaskSuccess; }): string { const comparison = compareBenchmark(input); const gate = (passed: boolean) => passed ? "PASS" : "FAIL"; return `# Context-cost benchmark Baseline: ${input.baselineLabel} | Metric | Baseline | Candidate | Change | Gate | |---|---:|---:|---:|---| | Spend | $${metric(input.baseline.cost, 4)} | $${metric(input.candidate.cost, 4)} | ${metric(comparison.costReductionPercent)}% lower | ${gate(comparison.gates.cost)} | | Total tokens | ${metric(input.baseline.totalTokens, 0)} | ${metric(input.candidate.totalTokens, 0)} | ${metric(comparison.tokenReductionPercent)}% lower | ${gate(comparison.gates.tokens)} | | Failure rate | ${metric(input.baseline.failureRate)}% | ${metric(input.candidate.failureRate)}% | — | ${gate(comparison.gates.failureRate)} | | Task success | — | ${input.taskSuccess} | — | ${gate(comparison.gates.taskSuccess)} | Candidate sessions: ${input.candidate.sessions} Overall: **${comparison.passed ? "PASS" : "FAIL"}** `; } async function main(): Promise { const args = parseArguments(process.argv.slice(2).filter((arg) => arg !== "--")); const candidate = await analyzeSessions(args.candidate); const baseline = args.baseline.length > 0 ? await analyzeSessions(args.baseline) : RECORDED_BASELINE; const baselineLabel = args.baseline.length > 0 ? `${baseline.sessions} analyzed session(s)` : "recorded specification baseline"; const report = reportMarkdown({ baseline, candidate, baselineLabel, taskSuccess: args.taskSuccess!, }); await mkdir(dirname(args.output!), { recursive: true }); await writeFile(args.output!, report, "utf8"); process.stdout.write(report); process.exitCode = compareBenchmark({ baseline, candidate, taskSuccess: args.taskSuccess!, }).passed ? 0 : 1; } main().catch((error) => { process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); process.exitCode = 2; });