forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline.ts
More file actions
232 lines (208 loc) · 6.73 KB
/
Copy pathbaseline.ts
File metadata and controls
232 lines (208 loc) · 6.73 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
/**
* Eval baseline management — pull from the production harness, capture from
* local eval runs, and persist/load from disk.
*
* The baseline is a snapshot of probe results that serves as the "expected"
* state for regression detection. Two sources:
*
* 1. **harness-prod** — pulled from the live showcase-harness /api/probes
* endpoint via `pullBaseline`.
* 2. **local-capture** — copied from the most recent local eval result
* file via `captureBaseline`.
*
* Results are keyed by integration slug (e.g. "mastra", "langgraph-python")
* with a `_status` sub-key, matching the format produced by `collectResults`
* in matrix.ts so that `computeRegressions` can compare them correctly.
*/
import fs from "node:fs";
import path from "node:path";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface EvalBaseline {
version: number;
timestamp: string;
source: "harness-prod" | "local-capture";
branch: string;
base: string;
level: string;
results: Record<
string,
Record<
string,
{ status: string; total?: number; passed?: number; failed?: number }
>
>;
summary: { total: number; pass: number; fail: number; skip: number };
}
interface HarnessServiceEntry {
slug: string; // e.g. "e2e-deep:showcase-mastra"
result: string; // "green" or "red"
state: string; // "completed"
}
interface HarnessProbeEntry {
id: string;
kind: string;
lastRun: {
startedAt: string;
finishedAt: string;
durationMs: number;
state: string;
summary: {
total: number;
passed: number;
failed: number;
services?: HarnessServiceEntry[];
} | null;
} | null;
}
export interface HarnessProbesResponse {
probes: HarnessProbeEntry[];
}
// ---------------------------------------------------------------------------
// Pure transform
// ---------------------------------------------------------------------------
/**
* Convert a harness /api/probes response into an EvalBaseline.
*
* Iterates each probe's `summary.services[]` to produce slug-keyed results
* that match the format produced by `collectResults` in matrix.ts. Each
* service slug has the format `<probeId>:showcase-<integrationSlug>` —
* we extract the integration slug via `split(":showcase-")[1]`.
*
* When the same integration slug appears in multiple probes, the worst
* status wins (if any probe shows red, the slug is "fail").
*
* Probes without `lastRun`, without `summary`, or without `services[]`
* are skipped gracefully.
*/
export function transformHarnessResponse(
response: HarnessProbesResponse,
): EvalBaseline {
const results: EvalBaseline["results"] = {};
let pass = 0;
let fail = 0;
for (const probe of response.probes) {
if (!probe.lastRun) continue;
if (!probe.lastRun.summary) continue;
if (!probe.lastRun.summary.services) continue;
for (const service of probe.lastRun.summary.services) {
const integrationSlug = service.slug.split(":showcase-")[1];
if (!integrationSlug) continue;
const status = service.result === "green" ? "pass" : "fail";
if (!results[integrationSlug]) {
results[integrationSlug] = {
_status: { status },
};
if (status === "pass") pass++;
else fail++;
}
// If same slug appears in multiple probes, keep the worst status
else if (
status === "fail" &&
results[integrationSlug]._status.status !== "fail"
) {
results[integrationSlug]._status.status = "fail";
pass--;
fail++;
}
}
}
return {
version: 1,
timestamp: new Date().toISOString(),
source: "harness-prod",
branch: "",
base: "",
level: "deep",
results,
summary: { total: pass + fail, pass, fail, skip: 0 },
};
}
// ---------------------------------------------------------------------------
// Network pull
// ---------------------------------------------------------------------------
const DEFAULT_HARNESS_URL =
process.env["SHOWCASE_HARNESS_URL"] ??
"https://showcase-harness-production.up.railway.app";
/**
* Pull the current probe state from a live harness instance, transform it
* into an EvalBaseline, and save to disk.
*/
export async function pullBaseline(
harnessUrl: string = DEFAULT_HARNESS_URL,
outputPath: string,
): Promise<EvalBaseline> {
const url = `${harnessUrl.replace(/\/+$/, "")}/api/probes`;
const res = await fetch(url, { signal: AbortSignal.timeout(10_000) });
if (!res.ok) {
throw new Error(
`Harness fetch failed: ${res.status} ${res.statusText} (${url})`,
);
}
const body = (await res.json()) as HarnessProbesResponse;
const baseline = transformHarnessResponse(body);
saveBaseline(baseline, outputPath);
return baseline;
}
// ---------------------------------------------------------------------------
// Disk I/O
// ---------------------------------------------------------------------------
/**
* Load a baseline from disk. Returns null when the file doesn't exist.
*/
export function loadBaseline(filePath: string): EvalBaseline | null {
try {
const raw = fs.readFileSync(filePath, "utf-8");
return JSON.parse(raw) as EvalBaseline;
} catch (err) {
if (
err instanceof Error &&
"code" in err &&
(err as NodeJS.ErrnoException).code === "ENOENT"
) {
return null;
}
throw err;
}
}
/**
* Write a baseline to disk as pretty-printed JSON.
*/
export function saveBaseline(baseline: EvalBaseline, filePath: string): void {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(filePath, JSON.stringify(baseline, null, 2) + "\n");
}
// ---------------------------------------------------------------------------
// Local capture
// ---------------------------------------------------------------------------
/**
* Find the most recently modified .json file in `evalResultsDir`, read it
* as an EvalBaseline, override `source` to "local-capture", and write it
* to `baselinePath`.
*/
export function captureBaseline(
evalResultsDir: string,
baselinePath: string,
): void {
const files = fs
.readdirSync(evalResultsDir)
.filter((f) => f.endsWith(".json"))
.map((f) => {
const full = path.join(evalResultsDir, f);
return { path: full, mtime: fs.statSync(full).mtimeMs };
})
.sort((a, b) => b.mtime - a.mtime);
if (files.length === 0) {
throw new Error(
`No .json files found in eval results dir: ${evalResultsDir}`,
);
}
const raw = fs.readFileSync(files[0].path, "utf-8");
const data = JSON.parse(raw) as EvalBaseline;
data.source = "local-capture";
saveBaseline(data, baselinePath);
}