|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Create a GitHub Check Run for eval results based on score statistics. |
| 5 | + * |
| 6 | + * This script: |
| 7 | + * 1. Reads eval-results.json from the evals package |
| 8 | + * 2. Calculates overall statistics and score distribution |
| 9 | + * 3. Creates a GitHub Check Run via the Checks API |
| 10 | + * 4. Sets conclusion to 'success' if avg score >= 0.5, 'failure' otherwise |
| 11 | + * |
| 12 | + * Environment variables required: |
| 13 | + * - GITHUB_TOKEN: GitHub token with checks:write permission |
| 14 | + * - GITHUB_REPOSITORY: Repository in format 'owner/repo' |
| 15 | + * - GITHUB_SHA: Commit SHA to create the check for |
| 16 | + */ |
| 17 | + |
| 18 | +import { readFileSync } from "node:fs"; |
| 19 | +import { resolve } from "node:path"; |
| 20 | + |
| 21 | +// GitHub API helper |
| 22 | +async function createCheckRun({ |
| 23 | + owner, |
| 24 | + repo, |
| 25 | + token, |
| 26 | + sha, |
| 27 | + name, |
| 28 | + conclusion, |
| 29 | + title, |
| 30 | + summary, |
| 31 | + text, |
| 32 | +}) { |
| 33 | + const url = `https://api.github.com/repos/${owner}/${repo}/check-runs`; |
| 34 | + |
| 35 | + const response = await fetch(url, { |
| 36 | + method: "POST", |
| 37 | + headers: { |
| 38 | + Authorization: `Bearer ${token}`, |
| 39 | + Accept: "application/vnd.github+json", |
| 40 | + "X-GitHub-Api-Version": "2022-11-28", |
| 41 | + "Content-Type": "application/json", |
| 42 | + }, |
| 43 | + body: JSON.stringify({ |
| 44 | + name, |
| 45 | + head_sha: sha, |
| 46 | + status: "completed", |
| 47 | + conclusion, |
| 48 | + output: { |
| 49 | + title, |
| 50 | + summary, |
| 51 | + text, |
| 52 | + }, |
| 53 | + }), |
| 54 | + }); |
| 55 | + |
| 56 | + if (!response.ok) { |
| 57 | + const error = await response.text(); |
| 58 | + throw new Error(`Failed to create check run: ${response.status} ${error}`); |
| 59 | + } |
| 60 | + |
| 61 | + return response.json(); |
| 62 | +} |
| 63 | + |
| 64 | +// Format score with color emoji |
| 65 | +function formatScore(score) { |
| 66 | + if (score >= 0.75) return `🟢 ${score.toFixed(2)}`; |
| 67 | + if (score >= 0.5) return `🟡 ${score.toFixed(2)}`; |
| 68 | + return `🔴 ${score.toFixed(2)}`; |
| 69 | +} |
| 70 | + |
| 71 | +// Main execution |
| 72 | +async function main() { |
| 73 | + // Validate environment |
| 74 | + const token = process.env.GITHUB_TOKEN; |
| 75 | + const repository = process.env.GITHUB_REPOSITORY; |
| 76 | + const sha = process.env.GITHUB_SHA; |
| 77 | + |
| 78 | + if (!token || !repository || !sha) { |
| 79 | + throw new Error( |
| 80 | + "Missing required environment variables: GITHUB_TOKEN, GITHUB_REPOSITORY, GITHUB_SHA", |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + const [owner, repo] = repository.split("/"); |
| 85 | + |
| 86 | + // Read eval results (vitest JSON format) |
| 87 | + const resultsPath = resolve( |
| 88 | + process.cwd(), |
| 89 | + "packages/mcp-server-evals/eval-results.json", |
| 90 | + ); |
| 91 | + console.log(`Reading eval results from: ${resultsPath}`); |
| 92 | + |
| 93 | + let vitestResults; |
| 94 | + try { |
| 95 | + vitestResults = JSON.parse(readFileSync(resultsPath, "utf-8")); |
| 96 | + } catch (error) { |
| 97 | + if (error.code === "ENOENT") { |
| 98 | + throw new Error( |
| 99 | + `Eval results file not found at ${resultsPath}. The eval run likely failed before producing results. Check the "Run evals" step logs for errors.`, |
| 100 | + ); |
| 101 | + } |
| 102 | + throw new Error(`Failed to read/parse eval results: ${error.message}`); |
| 103 | + } |
| 104 | + |
| 105 | + // Extract eval results from vitest format |
| 106 | + const evalResults = []; |
| 107 | + for (const testFile of vitestResults.testResults || []) { |
| 108 | + for (const test of testFile.assertionResults || []) { |
| 109 | + if (test.meta?.eval) { |
| 110 | + evalResults.push({ |
| 111 | + name: test.fullName || test.title, |
| 112 | + file: testFile.name, |
| 113 | + avgScore: test.meta.eval.avgScore ?? null, |
| 114 | + scores: test.meta.eval.scores || [], |
| 115 | + passed: test.status === "passed", |
| 116 | + duration: test.duration, |
| 117 | + }); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Calculate statistics |
| 123 | + const totalTests = evalResults.length; |
| 124 | + const validScores = evalResults |
| 125 | + .map((r) => r.avgScore) |
| 126 | + .filter((score) => score !== null); |
| 127 | + |
| 128 | + const avgScore = |
| 129 | + validScores.length > 0 |
| 130 | + ? validScores.reduce((sum, score) => sum + score, 0) / validScores.length |
| 131 | + : 0; |
| 132 | + |
| 133 | + const green = validScores.filter((s) => s >= 0.75).length; |
| 134 | + const yellow = validScores.filter((s) => s >= 0.5 && s < 0.75).length; |
| 135 | + const red = validScores.filter((s) => s < 0.5).length; |
| 136 | + const scoreDistribution = { green, yellow, red }; |
| 137 | + |
| 138 | + // Determine conclusion based on 0.5 threshold |
| 139 | + const conclusion = avgScore >= 0.5 ? "success" : "failure"; |
| 140 | + |
| 141 | + // Format title |
| 142 | + const title = `Eval Score: ${avgScore.toFixed(2)} (${green} green, ${yellow} yellow, ${red} red)`; |
| 143 | + |
| 144 | + // Format summary |
| 145 | + const summary = [ |
| 146 | + `## Overall Statistics`, |
| 147 | + ``, |
| 148 | + `- **Total Evaluations**: ${totalTests}`, |
| 149 | + `- **Average Score**: ${formatScore(avgScore)}`, |
| 150 | + `- **Pass Threshold**: 0.50 (catastrophic failure)`, |
| 151 | + ``, |
| 152 | + `### Score Distribution`, |
| 153 | + `- 🟢 Green (≥0.75): ${green} evals`, |
| 154 | + `- 🟡 Yellow (0.50-0.74): ${yellow} evals`, |
| 155 | + `- 🔴 Red (<0.50): ${red} evals`, |
| 156 | + ``, |
| 157 | + ].join("\n"); |
| 158 | + |
| 159 | + // Format detailed results |
| 160 | + const detailsByScore = [...evalResults].sort( |
| 161 | + (a, b) => (b.avgScore || 0) - (a.avgScore || 0), |
| 162 | + ); |
| 163 | + |
| 164 | + const details = [ |
| 165 | + `## Individual Eval Scores`, |
| 166 | + ``, |
| 167 | + ...detailsByScore.map((result) => { |
| 168 | + const score = result.avgScore !== null ? result.avgScore : 0; |
| 169 | + const statusIcon = result.passed ? "✅" : "❌"; |
| 170 | + const scoreDisplay = formatScore(score); |
| 171 | + |
| 172 | + let line = `${statusIcon} **${result.name}**: ${scoreDisplay}`; |
| 173 | + |
| 174 | + // Add rationale for failed or low-scoring tests |
| 175 | + if (!result.passed || score < 0.75) { |
| 176 | + const firstScore = result.scores[0]; |
| 177 | + if (firstScore?.metadata?.rationale) { |
| 178 | + line += `\n - ${firstScore.metadata.rationale}`; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return line; |
| 183 | + }), |
| 184 | + ``, |
| 185 | + `---`, |
| 186 | + ``, |
| 187 | + `### Conclusion`, |
| 188 | + ``, |
| 189 | + conclusion === "success" |
| 190 | + ? `✅ **Passed**: Average score (${avgScore.toFixed(2)}) is above the catastrophic failure threshold (0.50)` |
| 191 | + : `❌ **Failed**: Average score (${avgScore.toFixed(2)}) is below the catastrophic failure threshold (0.50)`, |
| 192 | + ].join("\n"); |
| 193 | + |
| 194 | + // Create check run |
| 195 | + console.log(`Creating check run with conclusion: ${conclusion}`); |
| 196 | + const checkRun = await createCheckRun({ |
| 197 | + owner, |
| 198 | + repo, |
| 199 | + token, |
| 200 | + sha, |
| 201 | + name: "Evaluation Results", |
| 202 | + conclusion, |
| 203 | + title, |
| 204 | + summary, |
| 205 | + text: details, |
| 206 | + }); |
| 207 | + |
| 208 | + console.log(`✅ Check run created: ${checkRun.html_url}`); |
| 209 | + console.log(` Conclusion: ${conclusion}`); |
| 210 | + console.log(` Average Score: ${avgScore.toFixed(2)}`); |
| 211 | +} |
| 212 | + |
| 213 | +main().catch((error) => { |
| 214 | + console.error("❌ Error creating check run:", error); |
| 215 | + process.exit(1); |
| 216 | +}); |
0 commit comments