forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
583 lines (534 loc) · 17.3 KB
/
Copy pathindex.ts
File metadata and controls
583 lines (534 loc) · 17.3 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/**
* Eval orchestrator — registers the `eval` subcommand and implements the
* full evaluation pipeline: scope detection, baseline comparison, tiered
* test execution, result collection, and verdict formatting.
*
* Pipeline:
* 1. Parse CLI options (--d5/--d6, --pr, --scope, --parallel, etc.)
* 2. Optionally create git worktree for PR evaluation
* 3. Detect affected scope via git diff
* 4. Load/pull baseline for comparison
* 5. Start affected services + aimock via lifecycle
* 6. Wait for health
* 7. Run tiered tests
* 8. Collect & format results
* 9. Save results + optional baseline capture
* 10. Cleanup
*/
import type { Command } from "commander";
import { Option } from "commander";
import { execFileSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { loadConfig } from "../config.js";
import { up, down, isRunning } from "../lifecycle.js";
import { createLogger, reloadLogLevel } from "../../logger.js";
// Sibling modules created by other blitz agents — imports written against
// the agreed interfaces. These will not resolve until the sibling branches
// are merged into the integration branch.
import { classifyScope } from "./scope.js";
import type { ScopeResult } from "./scope.js";
import { pullBaseline, loadBaseline, captureBaseline } from "./baseline.js";
import type { EvalBaseline } from "./baseline.js";
import {
collectResults,
formatMatrix,
formatVerdict,
computeRegressions,
saveResults,
} from "./matrix.js";
import type { EvalResults } from "./matrix.js";
import { runTiered } from "./runner.js";
import type { TieredRunResult, RunOptions } from "./runner.js";
const log = createLogger({ component: "eval" });
// ---------------------------------------------------------------------------
// CLI options interface
// ---------------------------------------------------------------------------
interface EvalOptions {
level?: string;
d5?: boolean;
pr?: string;
branch?: string;
scope?: string;
parallel?: string;
baseline?: string;
keep?: boolean;
json?: boolean;
timeout?: string;
slug?: string;
tier?: string;
failFast?: boolean; // commander negates --no-fail-fast to failFast: false
ci?: boolean;
}
// ---------------------------------------------------------------------------
// Command registration
// ---------------------------------------------------------------------------
export function registerEvalCommand(program: Command): void {
program
.command("eval")
.description("Run the D5 evaluation matrix against showcase integrations")
.addOption(
new Option("--level <level>", "probe depth")
.choices(["d5"])
.default("d5"),
)
.option("--d5", "shorthand for --level d5")
.option("--pr <number>", "fetch PR into worktree and eval")
.option("--branch <name>", "eval a specific branch in a worktree")
.option("--scope <mode>", "affected or all", "affected")
.option("--parallel <n>", "max concurrent test runners", "4")
.option("--baseline <action>", "capture or compare")
.option("--keep", "leave containers running after eval")
.option("--json", "JSON output for CI")
.option("--timeout <ms>", "per-test timeout", "45000")
.option("--slug <slugs>", "override scope (comma-separated)")
.option("--tier <n>", "run only tiers 1 through N")
.option("--no-fail-fast", "don't stop on Tier 1 failure")
.option(
"--ci",
"CI mode — skip Docker lifecycle, assume services already running",
)
.action(async (opts: EvalOptions) => {
await runEval(opts);
});
}
// ---------------------------------------------------------------------------
// Orchestrator
// ---------------------------------------------------------------------------
export async function runEval(opts: EvalOptions): Promise<void> {
// In --json mode, redirect all logger output to stderr so it doesn't
// corrupt the JSON payload on stdout.
if (opts.json) {
process.env["LOG_LEVEL"] = "warn";
reloadLogLevel();
}
const config = loadConfig();
// -- 1. Resolve level ------------------------------------------------------
const level = opts.d5 ? "d5" : (opts.level ?? "d5");
const parallel = parseInt(opts.parallel ?? "4", 10);
const timeout = parseInt(opts.timeout ?? "45000", 10);
const maxTier = opts.tier ? parseInt(opts.tier, 10) : undefined;
const failFast = opts.failFast !== false; // default true; --no-fail-fast sets to false
log.info("eval starting", {
level,
parallel,
timeout,
scope: opts.scope,
baseline: opts.baseline,
failFast,
});
// -- 2. PR worktree setup --------------------------------------------------
let worktreeDir: string | null = null;
let originalCwd: string | null = null;
if (opts.pr) {
log.info("setting up PR worktree", { pr: opts.pr });
const prNumber = opts.pr;
const worktreePath = path.join(
config.showcaseDir,
"..",
`.eval-pr-${prNumber}`,
);
worktreeDir = worktreePath;
originalCwd = process.cwd();
try {
execFileSync(
"git",
["fetch", "origin", `pull/${prNumber}/head:eval-pr-${prNumber}`],
{
cwd: config.showcaseDir,
stdio: "pipe",
encoding: "utf-8",
},
);
execFileSync(
"git",
["worktree", "add", worktreePath, `eval-pr-${prNumber}`],
{
cwd: config.showcaseDir,
stdio: "pipe",
encoding: "utf-8",
},
);
process.chdir(worktreePath);
log.info("worktree created", { path: worktreePath });
} catch (err) {
console.error(
`Failed to set up PR worktree: ${err instanceof Error ? err.message : String(err)}`,
);
process.exit(1);
}
} else if (opts.branch) {
log.info("setting up branch worktree", { branch: opts.branch });
const worktreePath = path.join(
config.showcaseDir,
"..",
`.eval-branch-${opts.branch.replace(/\//g, "-")}`,
);
worktreeDir = worktreePath;
originalCwd = process.cwd();
try {
execFileSync("git", ["worktree", "add", worktreePath, opts.branch], {
cwd: config.showcaseDir,
stdio: "pipe",
encoding: "utf-8",
});
process.chdir(worktreePath);
log.info("worktree created", { path: worktreePath });
} catch (err) {
console.error(
`Failed to set up branch worktree: ${err instanceof Error ? err.message : String(err)}`,
);
process.exit(1);
}
}
// Save the original showcaseDir BEFORE any worktree override so that
// cleanup can run `git worktree remove` with cwd OUTSIDE the worktree.
const originalShowcaseDir = config.showcaseDir;
// After worktree setup, override config.showcaseDir to point to the
// worktree's showcase directory so tests run against the PR's code.
if (worktreeDir) {
const worktreeShowcase = path.join(worktreeDir, "showcase");
config.showcaseDir = worktreeShowcase;
config.composeFile = path.join(
worktreeShowcase,
"docker-compose.local.yml",
);
config.localPorts = JSON.parse(
fs.readFileSync(
path.join(worktreeShowcase, "shared/local-ports.json"),
"utf-8",
),
);
log.info("config overridden for worktree", {
showcaseDir: worktreeShowcase,
});
}
// -- 3. Detect scope -------------------------------------------------------
const allSlugs = Object.keys(config.localPorts);
let scopeResult: ScopeResult;
if (opts.slug) {
const rawSlugs = opts.slug
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const unknown = rawSlugs.filter((s) => !allSlugs.includes(s));
if (unknown.length > 0) {
log.warn("unknown slugs in --slug override (skipping)", { unknown });
}
const slugs = rawSlugs.filter((s) => allSlugs.includes(s));
scopeResult = {
slugs,
mode: "all",
reason: `manual override: ${slugs.join(", ")}`,
};
log.info("manual scope override", { slugs });
} else if (opts.scope === "all") {
scopeResult = {
slugs: [...allSlugs],
mode: "all",
reason: "user specified --scope all",
};
log.info("scope: all", { count: allSlugs.length });
} else {
let diffOutput = "";
try {
diffOutput = execFileSync(
"git",
["diff", "--name-only", "origin/main...HEAD"],
{
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
},
).trim();
} catch {
diffOutput = execFileSync(
"git",
["diff", "--name-only", "origin/main", "HEAD"],
{
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
},
).trim();
}
const changedFiles = diffOutput
.split("\n")
.map((f) => f.trim())
.filter(Boolean);
scopeResult = classifyScope(changedFiles, allSlugs);
log.info("scope detected", {
mode: scopeResult.mode,
slugs: scopeResult.slugs,
});
}
if (scopeResult.slugs.length === 0) {
if (opts.json) {
console.log(
JSON.stringify(
{
version: 1,
timestamp: new Date().toISOString(),
branch: "",
base: "",
level,
scope: scopeResult,
results: {},
summary: { total: 0, pass: 0, fail: 0, skip: 0, duration_ms: 0 },
},
null,
2,
),
);
} else {
console.log("\n No showcase integrations affected by this change.\n");
}
await cleanup(worktreeDir, originalCwd, originalShowcaseDir);
return;
}
if (!opts.json) {
console.log(
`\n \x1b[36mEval scope:\x1b[0m ${scopeResult.slugs.join(", ")} (${scopeResult.mode})\n`,
);
}
// -- 4. Baseline -----------------------------------------------------------
const baselinePath = path.join(config.showcaseDir, ".eval-baseline.json");
let baseline: EvalBaseline | null = null;
if (opts.baseline === "compare") {
baseline = loadBaseline(baselinePath);
if (!baseline) {
log.info("no local baseline, pulling from harness");
try {
baseline = await pullBaseline(undefined, baselinePath);
} catch (err) {
log.warn("failed to pull baseline", {
err: err instanceof Error ? err.message : String(err),
});
}
} else {
log.info("loaded local baseline", {
slugCount: Object.keys(baseline.results).length,
});
}
if (!baseline) {
console.warn(
" \x1b[33mWarning: no baseline found for comparison, proceeding without\x1b[0m\n",
);
}
}
// -- 5. Build + start services ---------------------------------------------
let autoStarted: string[] = [];
if (opts.ci) {
log.info("CI mode — skipping Docker lifecycle, assuming services running");
} else {
const slugsToStart = [...scopeResult.slugs];
// Always ensure aimock is running
if (!slugsToStart.includes("aimock")) {
slugsToStart.push("aimock");
}
for (const slug of slugsToStart) {
const running = await isRunning(slug);
if (!running) {
autoStarted.push(slug);
}
}
if (autoStarted.length > 0) {
if (!opts.json) {
console.log(
` \x1b[36mStarting services:\x1b[0m ${autoStarted.join(", ")}`,
);
}
// up() includes health checks internally
await up(autoStarted);
if (!opts.json) {
console.log(" \x1b[32mAll services healthy\x1b[0m\n");
}
}
}
// -- 6-7. Run tiered tests -------------------------------------------------
const healthySlugs = opts.ci
? [...scopeResult.slugs]
: scopeResult.slugs.filter((s) => {
try {
const result = execFileSync(
"docker",
["inspect", "--format={{.State.Health.Status}}", `showcase-${s}`],
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] },
).trim();
return result === "healthy";
} catch {
return false;
}
});
let tieredResult: TieredRunResult;
try {
const runOptions: RunOptions = {
level,
maxParallel: parallel,
timeout,
showcaseDir: config.showcaseDir,
maxTier,
noFailFast: !failFast,
onSlugStart: (slug, tier) => {
if (!opts.json)
console.log(` \x1b[2m[${tier}] testing ${slug}...\x1b[0m`);
},
onSlugComplete: (result, tier) => {
const icon =
result.status === "pass" ? "\x1b[32m✓\x1b[0m" : "\x1b[31m✗\x1b[0m";
if (!opts.json)
console.log(
` ${icon} [${tier}] ${result.slug} (${result.duration_ms}ms)`,
);
},
};
tieredResult = await runTiered(scopeResult.slugs, healthySlugs, runOptions);
} catch (err) {
console.error(
`\x1b[31mEval run failed:\x1b[0m ${err instanceof Error ? err.message : String(err)}`,
);
await teardown(
autoStarted,
opts.keep,
worktreeDir,
originalCwd,
originalShowcaseDir,
);
process.exit(1);
}
// -- 8. Collect results ----------------------------------------------------
const branchName = (() => {
try {
return execFileSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
encoding: "utf-8",
}).trim();
} catch {
return opts.pr ? `PR #${opts.pr}` : (opts.branch ?? "unknown");
}
})();
const evalResults: EvalResults = collectResults(tieredResult.results, {
branch: branchName,
base: "origin/main",
level,
scope: {
mode: scopeResult.mode,
reason: scopeResult.reason,
slugs: scopeResult.slugs,
},
});
// -- 9. Format + print -----------------------------------------------------
// Adapt EvalBaseline to EvalResults for comparison. Both share the
// `.results[slug][test].status` shape that computeRegressions/formatMatrix read.
const baselineAsResults: EvalResults | undefined = baseline
? {
version: baseline.version,
timestamp: baseline.timestamp,
branch: baseline.branch,
base: baseline.base,
level: baseline.level,
scope: {
mode: "all",
reason: "baseline",
slugs: Object.keys(baseline.results),
},
results: Object.fromEntries(
Object.entries(baseline.results).map(([slug, tests]) => [
slug,
Object.fromEntries(
Object.entries(tests).map(([testName, entry]) => [
testName,
{
status:
entry.status as import("./matrix.js").TestResult["status"],
duration_ms: 0,
},
]),
),
]),
),
summary: { ...baseline.summary, duration_ms: 0 },
}
: undefined;
if (opts.json) {
console.log(JSON.stringify(evalResults, null, 2));
} else {
const matrix = formatMatrix(evalResults, baselineAsResults ?? undefined);
console.log(matrix);
if (baseline && opts.baseline === "compare") {
const regressions = computeRegressions(evalResults, baselineAsResults);
if (regressions.count > 0) {
console.log(
`\n \x1b[31mRegressions detected: ${regressions.count}\x1b[0m`,
);
for (const r of regressions.details) {
console.log(` - ${r.slug}: ${r.test}`);
}
}
}
const verdict = formatVerdict(evalResults, baselineAsResults ?? undefined);
console.log(verdict);
}
// -- 10. Save results ------------------------------------------------------
const savedPath = saveResults(evalResults, config.showcaseDir);
log.info("results saved", { path: savedPath });
if (opts.baseline === "capture") {
const evalResultsDir = path.join(config.showcaseDir, ".eval-results");
captureBaseline(evalResultsDir, baselinePath);
log.info("baseline captured");
}
// -- 11. Cleanup -----------------------------------------------------------
await teardown(
autoStarted,
opts.keep,
worktreeDir,
originalCwd,
originalShowcaseDir,
);
// Exit with failure if any tests failed
const totalFailed = evalResults.summary?.fail ?? 0;
if (totalFailed > 0) {
process.exit(1);
}
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
async function teardown(
autoStarted: string[],
keep: boolean | undefined,
worktreeDir: string | null,
originalCwd: string | null,
showcaseDir: string,
): Promise<void> {
if (!keep && autoStarted.length > 0) {
log.info("stopping auto-started services", { services: autoStarted });
try {
await down(autoStarted);
} catch (err) {
log.warn("failed to stop services during teardown", {
err: err instanceof Error ? err.message : String(err),
});
}
}
await cleanup(worktreeDir, originalCwd, showcaseDir);
}
async function cleanup(
worktreeDir: string | null,
originalCwd: string | null,
showcaseDir: string,
): Promise<void> {
if (worktreeDir && originalCwd) {
process.chdir(originalCwd);
try {
execFileSync("git", ["worktree", "remove", "--force", worktreeDir], {
cwd: showcaseDir,
stdio: "pipe",
encoding: "utf-8",
});
log.info("worktree removed", { path: worktreeDir });
} catch (err) {
log.warn("failed to remove worktree", {
path: worktreeDir,
err: err instanceof Error ? err.message : String(err),
});
}
}
}