forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.ts
More file actions
294 lines (250 loc) · 8.94 KB
/
Copy pathmatrix.ts
File metadata and controls
294 lines (250 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Eval matrix reporting module — aggregates per-slug eval results into a
* consolidated report, formats a coloured terminal matrix with delta markers
* (FIXED / NEW) against a baseline, and computes regression counts.
*
* Uses the same ANSI colour constants as ../results.ts.
*/
import fs from "node:fs";
import path from "node:path";
// ---------------------------------------------------------------------------
// ANSI helpers (mirrored from ../results.ts)
// ---------------------------------------------------------------------------
const RESET = "\x1b[0m";
const GREEN = "\x1b[32m";
const RED = "\x1b[31m";
const YELLOW = "\x1b[33m";
const DIM = "\x1b[2m";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface EvalResults {
version: number;
timestamp: string;
branch: string;
base: string;
level: string;
scope: { mode: string; reason: string; slugs: string[] };
results: Record<string, Record<string, TestResult>>;
summary: {
total: number;
pass: number;
fail: number;
skip: number;
duration_ms: number;
};
}
export interface TestResult {
status: "pass" | "fail" | "skip" | "error" | "build_failed" | "unhealthy";
duration_ms?: number;
error?: string;
}
export interface SlugResult {
slug: string;
status: "pass" | "fail" | "error" | "build_failed" | "unhealthy" | "skipped";
tests: Record<string, TestResult>;
duration_ms: number;
}
export interface EvalMetadata {
branch: string;
base: string;
level: string;
scope: { mode: string; reason: string; slugs: string[] };
}
// ---------------------------------------------------------------------------
// collectResults
// ---------------------------------------------------------------------------
/**
* Aggregate per-slug results into consolidated EvalResults format.
* Slugs whose status is build_failed or unhealthy with no tests get a
* synthetic test entry so they still appear in the matrix.
*/
export function collectResults(
slugResults: SlugResult[],
metadata: EvalMetadata,
): EvalResults {
const results: Record<string, Record<string, TestResult>> = {};
let totalDuration = 0;
for (const sr of slugResults) {
const tests = { ...sr.tests };
// Synthetic entry for slugs that never ran tests
if (Object.keys(tests).length === 0) {
const syntheticStatus = sr.status === "skipped" ? "skip" : sr.status;
tests["_status"] = {
status: syntheticStatus as TestResult["status"],
duration_ms: sr.duration_ms,
};
}
results[sr.slug] = tests;
totalDuration += sr.duration_ms;
}
// Compute summary from all test entries
const allTests = Object.values(results).flatMap((t) => Object.values(t));
const pass = allTests.filter((t) => t.status === "pass").length;
const skip = allTests.filter((t) => t.status === "skip").length;
const fail = allTests.length - pass - skip;
return {
version: 1,
timestamp: new Date().toISOString(),
branch: metadata.branch,
base: metadata.base,
level: metadata.level,
scope: metadata.scope,
results,
summary: {
total: allTests.length,
pass,
fail,
skip,
duration_ms: totalDuration,
},
};
}
// ---------------------------------------------------------------------------
// computeRegressions
// ---------------------------------------------------------------------------
/**
* Count pass->fail transitions between baseline and current results.
* Only pass->fail in the baseline is counted as a regression. fail->fail,
* skip->fail, and new tests are not regressions.
*/
export function computeRegressions(
results: EvalResults,
baseline?: EvalResults,
): { count: number; details: Array<{ slug: string; test: string }> } {
if (!baseline) {
return { count: 0, details: [] };
}
const details: Array<{ slug: string; test: string }> = [];
for (const [slug, tests] of Object.entries(results.results)) {
const baselineTests = baseline.results[slug];
if (!baselineTests) continue;
for (const [testName, testResult] of Object.entries(tests)) {
const baselineTest = baselineTests[testName];
if (!baselineTest) continue;
// Only pass->fail is a regression
if (baselineTest.status === "pass" && testResult.status !== "pass") {
details.push({ slug, test: testName });
}
}
}
return { count: details.length, details };
}
// ---------------------------------------------------------------------------
// formatMatrix
// ---------------------------------------------------------------------------
function statusColor(status: TestResult["status"]): string {
if (status === "pass") return GREEN;
if (status === "skip") return YELLOW;
return RED;
}
function statusIcon(status: TestResult["status"]): string {
if (status === "pass") return "✓";
if (status === "skip") return "-";
return "✗";
}
/**
* Format a coloured terminal matrix showing per-slug results with optional
* delta markers against a baseline.
*/
export function formatMatrix(
results: EvalResults,
baseline?: EvalResults,
): string {
const lines: string[] = [];
lines.push("");
lines.push(
` ${DIM}Eval Matrix${RESET} ${results.branch} vs ${results.base} ${DIM}(${results.level})${RESET}`,
);
lines.push(` ${DIM}${"─".repeat(60)}${RESET}`);
const slugs = Object.keys(results.results).sort();
for (const slug of slugs) {
const tests = results.results[slug];
const testNames = Object.keys(tests).sort();
const slugPass = Object.values(tests).filter(
(t) => t.status === "pass",
).length;
const slugTotal = testNames.length;
const slugColor = slugPass === slugTotal ? GREEN : RED;
lines.push(
` ${slugColor}${slug}${RESET} ${DIM}(${slugPass}/${slugTotal})${RESET}`,
);
for (const testName of testNames) {
const test = tests[testName];
const icon = statusIcon(test.status);
const color = statusColor(test.status);
let delta = "";
if (baseline) {
const baselineTest = baseline.results[slug]?.[testName];
if (baselineTest) {
if (baselineTest.status !== "pass" && test.status === "pass") {
delta = ` ${GREEN}[FIXED]${RESET}`;
} else if (baselineTest.status === "pass" && test.status !== "pass") {
delta = ` ${RED}[NEW]${RESET}`;
}
}
}
const duration = test.duration_ms
? ` ${DIM}(${(test.duration_ms / 1000).toFixed(1)}s)${RESET}`
: "";
const errorStr = test.error ? ` ${RED}${test.error}${RESET}` : "";
lines.push(
` ${color}${icon}${RESET} ${testName} ${color}${test.status}${RESET}${duration}${delta}${errorStr}`,
);
}
}
// Summary
lines.push(` ${DIM}${"─".repeat(60)}${RESET}`);
const { pass, fail, skip } = results.summary;
const parts: string[] = [];
parts.push(`${GREEN}${pass} passed${RESET}`);
if (fail > 0) parts.push(`${RED}${fail} failed${RESET}`);
if (skip > 0) parts.push(`${YELLOW}${skip} skipped${RESET}`);
const durationStr = `${DIM}(${(results.summary.duration_ms / 1000).toFixed(1)}s)${RESET}`;
lines.push(` ${parts.join(", ")} ${durationStr}`);
// If no failures, still show "0 passed" for empty case
if (pass === 0 && fail === 0 && skip === 0) {
lines[lines.length - 1] = ` ${GREEN}0 passed${RESET} ${durationStr}`;
}
return lines.join("\n");
}
// ---------------------------------------------------------------------------
// formatVerdict
// ---------------------------------------------------------------------------
/**
* Return a coloured verdict line — SAFE TO MERGE or REGRESSIONS DETECTED.
*/
export function formatVerdict(
results: EvalResults,
baseline?: EvalResults,
): string {
const { count, details } = computeRegressions(results, baseline);
if (count === 0) {
return `\n ${GREEN}✓ SAFE TO MERGE${RESET} ${DIM}(0 regressions)${RESET}\n`;
}
const lines: string[] = [];
lines.push(
`\n ${RED}✗ REGRESSIONS DETECTED${RESET} ${RED}(${count} regression${count > 1 ? "s" : ""})${RESET}`,
);
for (const d of details) {
lines.push(` ${RED}✗${RESET} ${d.slug} / ${d.test}`);
}
lines.push("");
return lines.join("\n");
}
// ---------------------------------------------------------------------------
// saveResults
// ---------------------------------------------------------------------------
/**
* Write eval results to .eval-results/<timestamp>.json under the given
* showcase directory. Returns the absolute path to the written file.
*/
export function saveResults(results: EvalResults, showcaseDir: string): string {
const dir = path.join(showcaseDir, ".eval-results");
fs.mkdirSync(dir, { recursive: true });
// Timestamp-based filename, safe for filesystem
const ts = results.timestamp.replace(/[:.]/g, "-");
const filePath = path.join(dir, `${ts}.json`);
fs.writeFileSync(filePath, JSON.stringify(results, null, 2) + "\n", "utf-8");
return filePath;
}