-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun-eval.ts
More file actions
661 lines (596 loc) · 19.6 KB
/
run-eval.ts
File metadata and controls
661 lines (596 loc) · 19.6 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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import { constants } from 'node:fs';
import { access } from 'node:fs/promises';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import {
type EvalCase,
type EvaluationCache,
type EvaluationResult,
type ProviderResponse,
runEvaluation as defaultRunEvaluation,
ensureVSCodeSubagents,
loadEvalCases,
subscribeToCodexLogEntries,
subscribeToPiLogEntries,
} from '@agentv/core';
import { loadEnvFromHierarchy } from './env.js';
import {
type OutputFormat,
type OutputWriter,
createOutputWriter,
getDefaultExtension,
} from './output-writer.js';
import { ProgressDisplay, type WorkerProgress } from './progress-display.js';
import { calculateEvaluationSummary, formatEvaluationSummary } from './statistics.js';
import { type TargetSelection, selectTarget } from './targets.js';
const DEFAULT_WORKERS = 3;
interface RunEvalCommandInput {
readonly testFiles: readonly string[];
readonly rawOptions: Record<string, unknown>;
}
interface NormalizedOptions {
readonly target?: string;
readonly targetsPath?: string;
readonly evalId?: string;
readonly workers?: number;
readonly outPath?: string;
readonly format: OutputFormat;
readonly dryRun: boolean;
readonly dryRunDelay: number;
readonly dryRunDelayMin: number;
readonly dryRunDelayMax: number;
readonly agentTimeoutSeconds: number;
readonly maxRetries: number;
readonly workspaceRoot?: string;
readonly cache: boolean;
readonly verbose: boolean;
}
function normalizeBoolean(value: unknown): boolean {
return value === true;
}
function normalizeString(value: unknown): string | undefined {
if (typeof value !== 'string') {
return undefined;
}
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
function normalizeNumber(value: unknown, fallback: number): number {
if (typeof value === 'number' && Number.isFinite(value)) {
return value;
}
if (typeof value === 'string') {
const parsed = Number.parseInt(value, 10);
if (!Number.isNaN(parsed)) {
return parsed;
}
}
return fallback;
}
function normalizeOptions(rawOptions: Record<string, unknown>): NormalizedOptions {
const formatStr = normalizeString(rawOptions.outputFormat) ?? 'jsonl';
const format: OutputFormat = formatStr === 'yaml' ? 'yaml' : 'jsonl';
const workers = normalizeNumber(rawOptions.workers, 0);
return {
target: normalizeString(rawOptions.target),
targetsPath: normalizeString(rawOptions.targets),
evalId: normalizeString(rawOptions.evalId),
workers: workers > 0 ? workers : undefined,
outPath: normalizeString(rawOptions.out),
format,
dryRun: normalizeBoolean(rawOptions.dryRun),
dryRunDelay: normalizeNumber(rawOptions.dryRunDelay, 0),
dryRunDelayMin: normalizeNumber(rawOptions.dryRunDelayMin, 0),
dryRunDelayMax: normalizeNumber(rawOptions.dryRunDelayMax, 0),
agentTimeoutSeconds: normalizeNumber(rawOptions.agentTimeout, 120),
maxRetries: normalizeNumber(rawOptions.maxRetries, 2),
workspaceRoot: normalizeString(rawOptions.workspaceRoot),
cache: normalizeBoolean(rawOptions.cache),
verbose: normalizeBoolean(rawOptions.verbose),
} satisfies NormalizedOptions;
}
async function ensureFileExists(filePath: string, description: string): Promise<void> {
try {
await access(filePath, constants.F_OK);
} catch {
throw new Error(`${description} not found: ${filePath}`);
}
}
async function findRepoRoot(start: string): Promise<string> {
const fallback = path.resolve(start);
let current: string | undefined = fallback;
while (current !== undefined) {
const candidate = path.join(current, '.git');
try {
await access(candidate, constants.F_OK);
return current;
} catch {
const parent = path.dirname(current);
if (parent === current) {
break;
}
current = parent;
}
}
return fallback;
}
function buildDefaultOutputPath(cwd: string, format: OutputFormat): string {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const baseName = 'eval';
const extension = getDefaultExtension(format);
return path.join(cwd, '.agentv', 'results', `${baseName}_${timestamp}${extension}`);
}
function createEvaluationCache(): EvaluationCache {
const store = new Map<string, ProviderResponse>();
return {
async get(key: string) {
return store.get(key);
},
async set(key: string, value: ProviderResponse) {
store.set(key, value);
},
} satisfies EvaluationCache;
}
type ProgressReporter = {
readonly isInteractive: boolean;
start(): void;
setTotal(total: number): void;
update(workerId: number, progress: WorkerProgress): void;
finish(): void;
addLogPaths(paths: readonly string[], provider?: 'codex' | 'pi'): void;
};
function createProgressReporter(
maxWorkers: number,
options?: { verbose?: boolean },
): ProgressReporter {
const display = new ProgressDisplay(maxWorkers, options);
return {
isInteractive: display.isInteractiveMode(),
start: () => display.start(),
setTotal: (total: number) => display.setTotalTests(total),
update: (workerId: number, progress: WorkerProgress) =>
display.updateWorker({ ...progress, workerId }),
finish: () => display.finish(),
addLogPaths: (paths: readonly string[], provider?: 'codex' | 'pi') =>
display.addLogPaths(paths, provider),
};
}
function makeEvalKey(testFilePath: string, evalId: string): string {
return `${path.resolve(testFilePath)}::${evalId}`;
}
function createDisplayIdTracker(): { getOrAssign(evalKey: string): number } {
const map = new Map<string, number>();
let nextId = 1;
return {
getOrAssign(evalKey: string): number {
const existing = map.get(evalKey);
if (existing !== undefined) {
return existing;
}
const assigned = nextId++;
map.set(evalKey, assigned);
return assigned;
},
};
}
/**
* Override CLI provider verbose setting based on CLI --verbose flag.
* CLI provider logs should only appear when --verbose is passed.
*/
function applyVerboseOverride(selection: TargetSelection, cliVerbose: boolean): TargetSelection {
const { resolvedTarget } = selection;
// Only CLI providers have a verbose setting in their config
if (resolvedTarget.kind !== 'cli') {
return selection;
}
// Set verbose to match CLI --verbose flag
return {
...selection,
resolvedTarget: {
...resolvedTarget,
config: {
...resolvedTarget.config,
verbose: cliVerbose,
},
},
};
}
export function applyWorkspaceRootOverride(
selection: TargetSelection,
workspaceRoot?: string,
): TargetSelection {
const root = workspaceRoot?.trim();
if (!root) {
return selection;
}
const { resolvedTarget } = selection;
if (resolvedTarget.kind === 'vscode' || resolvedTarget.kind === 'vscode-insiders') {
const current = resolvedTarget.config.workspaceTemplate;
if (typeof current === 'string' && current.trim().length > 0) {
return selection;
}
return {
...selection,
resolvedTarget: {
...resolvedTarget,
config: {
...resolvedTarget.config,
workspaceTemplate: root,
},
},
};
}
if (resolvedTarget.kind === 'cli') {
const current = resolvedTarget.config.cwd;
if (typeof current === 'string' && current.trim().length > 0) {
return selection;
}
return {
...selection,
resolvedTarget: {
...resolvedTarget,
config: {
...resolvedTarget.config,
cwd: root,
},
},
};
}
if (resolvedTarget.kind === 'codex') {
const current = resolvedTarget.config.cwd;
if (typeof current === 'string' && current.trim().length > 0) {
return selection;
}
return {
...selection,
resolvedTarget: {
...resolvedTarget,
config: {
...resolvedTarget.config,
cwd: root,
},
},
};
}
if (resolvedTarget.kind === 'pi-coding-agent') {
const current = resolvedTarget.config.cwd;
if (typeof current === 'string' && current.trim().length > 0) {
return selection;
}
return {
...selection,
resolvedTarget: {
...resolvedTarget,
config: {
...resolvedTarget.config,
cwd: root,
},
},
};
}
if (resolvedTarget.kind === 'claude-code') {
const current = resolvedTarget.config.cwd;
if (typeof current === 'string' && current.trim().length > 0) {
return selection;
}
return {
...selection,
resolvedTarget: {
...resolvedTarget,
config: {
...resolvedTarget.config,
cwd: root,
},
},
};
}
return selection;
}
async function prepareFileMetadata(params: {
readonly testFilePath: string;
readonly repoRoot: string;
readonly cwd: string;
readonly workspaceRoot?: string;
readonly options: NormalizedOptions;
}): Promise<{
readonly evalIds: readonly string[];
readonly evalCases: readonly EvalCase[];
readonly selection: TargetSelection;
readonly inlineTargetLabel: string;
}> {
const { testFilePath, repoRoot, cwd, options, workspaceRoot } = params;
await ensureFileExists(testFilePath, 'Test file');
await loadEnvFromHierarchy({
testFilePath,
repoRoot,
verbose: options.verbose,
});
const selection = await selectTarget({
testFilePath,
repoRoot,
cwd,
explicitTargetsPath: options.targetsPath,
cliTargetName: options.target,
dryRun: options.dryRun,
dryRunDelay: options.dryRunDelay,
dryRunDelayMin: options.dryRunDelayMin,
dryRunDelayMax: options.dryRunDelayMax,
env: process.env,
});
const selectionWithWorkspaceRoot = applyWorkspaceRootOverride(selection, workspaceRoot);
const providerLabel = options.dryRun
? `${selectionWithWorkspaceRoot.resolvedTarget.kind} (dry-run)`
: selectionWithWorkspaceRoot.resolvedTarget.kind;
const inlineTargetLabel = `${selectionWithWorkspaceRoot.targetName} [provider=${providerLabel}]`;
const evalCases = await loadEvalCases(testFilePath, repoRoot, {
verbose: options.verbose,
evalId: options.evalId,
});
const filteredIds = options.evalId
? evalCases.filter((value) => value.id === options.evalId).map((value) => value.id)
: evalCases.map((value) => value.id);
return { evalIds: filteredIds, evalCases, selection, inlineTargetLabel };
}
async function runWithLimit<T>(
items: readonly T[],
limit: number,
task: (item: T) => Promise<void>,
): Promise<void> {
const safeLimit = Math.max(1, limit);
let index = 0;
const workers = Array.from({ length: safeLimit }, async () => {
while (index < items.length) {
const current = items[index];
index += 1;
await task(current);
}
});
await Promise.all(workers);
}
async function runSingleEvalFile(params: {
readonly testFilePath: string;
readonly cwd: string;
readonly repoRoot: string;
readonly options: NormalizedOptions;
readonly outputWriter: OutputWriter;
readonly cache?: EvaluationCache;
readonly evaluationRunner: typeof defaultRunEvaluation;
readonly workersOverride?: number;
readonly progressReporter: ProgressReporter;
readonly seenEvalCases: Set<string>;
readonly displayIdTracker: { getOrAssign(evalKey: string): number };
readonly selection: TargetSelection;
readonly inlineTargetLabel: string;
readonly evalCases: readonly EvalCase[];
}): Promise<{ results: EvaluationResult[] }> {
const {
testFilePath,
cwd,
repoRoot,
options,
outputWriter,
cache,
evaluationRunner,
workersOverride,
progressReporter,
seenEvalCases,
displayIdTracker,
selection,
inlineTargetLabel,
evalCases,
} = params;
await ensureFileExists(testFilePath, 'Test file');
// CLI provider verbose logging should only be enabled when --verbose flag is passed
const resolvedTargetSelection = applyVerboseOverride(selection, options.verbose);
const providerLabel = options.dryRun
? `${resolvedTargetSelection.resolvedTarget.kind} (dry-run)`
: resolvedTargetSelection.resolvedTarget.kind;
const targetMessage = options.verbose
? `Using target (${resolvedTargetSelection.targetSource}): ${resolvedTargetSelection.targetName} [provider=${providerLabel}] via ${resolvedTargetSelection.targetsFilePath}`
: `Using target: ${inlineTargetLabel}`;
if (!progressReporter.isInteractive || options.verbose) {
console.log(targetMessage);
}
const agentTimeoutMs = Math.max(0, options.agentTimeoutSeconds) * 1000;
// Resolve workers: CLI flag (adjusted per-file) > target setting > default (1)
const workerPreference = workersOverride ?? options.workers;
let resolvedWorkers =
workerPreference ?? resolvedTargetSelection.resolvedTarget.workers ?? DEFAULT_WORKERS;
if (resolvedWorkers < 1 || resolvedWorkers > 50) {
throw new Error(`Workers must be between 1 and 50, got: ${resolvedWorkers}`);
}
// VSCode providers require window focus, so only 1 worker is allowed
const isVSCodeProvider = ['vscode', 'vscode-insiders'].includes(
resolvedTargetSelection.resolvedTarget.kind,
);
if (isVSCodeProvider && resolvedWorkers > 1) {
console.warn(
`Warning: VSCode providers require window focus. Limiting workers from ${resolvedWorkers} to 1 to prevent race conditions.`,
);
resolvedWorkers = 1;
}
// Auto-provision subagents for VSCode targets
if (isVSCodeProvider && !options.dryRun) {
await ensureVSCodeSubagents({
kind: resolvedTargetSelection.resolvedTarget.kind as 'vscode' | 'vscode-insiders',
count: resolvedWorkers,
verbose: options.verbose,
});
}
const results = await evaluationRunner({
testFilePath,
repoRoot,
target: resolvedTargetSelection.resolvedTarget,
targets: resolvedTargetSelection.definitions,
env: process.env,
maxRetries: Math.max(0, options.maxRetries),
agentTimeoutMs,
cache,
useCache: options.cache,
evalId: options.evalId,
evalCases,
verbose: options.verbose,
maxConcurrency: resolvedWorkers,
onResult: async (result: EvaluationResult) => {
await outputWriter.append(result);
},
onProgress: async (event) => {
const evalKey = makeEvalKey(testFilePath, event.evalId);
if (event.status === 'pending' && !seenEvalCases.has(evalKey)) {
seenEvalCases.add(evalKey);
progressReporter.setTotal(seenEvalCases.size);
}
const displayId = displayIdTracker.getOrAssign(evalKey);
progressReporter.update(displayId, {
workerId: displayId,
evalId: event.evalId,
status: event.status,
startedAt: event.startedAt,
completedAt: event.completedAt,
error: event.error,
targetLabel: inlineTargetLabel,
});
},
});
return { results: [...results] };
}
export async function runEvalCommand(input: RunEvalCommandInput): Promise<void> {
const options = normalizeOptions(input.rawOptions);
const workspaceRoot = options.workspaceRoot ? path.resolve(options.workspaceRoot) : undefined;
const cwd = process.cwd();
const repoRoot = await findRepoRoot(cwd);
if (options.verbose) {
console.log(`Repository root: ${repoRoot}`);
}
const outputPath = options.outPath
? path.resolve(options.outPath)
: buildDefaultOutputPath(cwd, options.format);
console.log(`Output path: ${outputPath}`);
const outputWriter = await createOutputWriter(outputPath, options.format);
const cache = options.cache ? createEvaluationCache() : undefined;
const evaluationRunner = await resolveEvaluationRunner();
const allResults: EvaluationResult[] = [];
const seenEvalCases = new Set<string>();
const resolvedTestFiles = input.testFiles.map((file) => path.resolve(file));
const displayIdTracker = createDisplayIdTracker();
// Derive file-level concurrency from worker count (global) when provided
const totalWorkers = options.workers ?? DEFAULT_WORKERS;
const fileConcurrency = Math.min(
Math.max(1, totalWorkers),
Math.max(1, resolvedTestFiles.length),
);
const perFileWorkers = options.workers
? Math.max(1, Math.floor(totalWorkers / fileConcurrency))
: undefined;
const fileMetadata = new Map<
string,
{
readonly evalIds: readonly string[];
readonly evalCases: readonly EvalCase[];
readonly selection: TargetSelection;
readonly inlineTargetLabel: string;
}
>();
for (const testFilePath of resolvedTestFiles) {
const meta = await prepareFileMetadata({
testFilePath,
repoRoot,
cwd,
workspaceRoot,
options,
});
fileMetadata.set(testFilePath, meta);
}
const totalEvalCount = Array.from(fileMetadata.values()).reduce(
(sum, meta) => sum + meta.evalIds.length,
0,
);
if (totalEvalCount === 0) {
throw new Error('No eval cases matched the provided filters.');
}
const progressReporter = createProgressReporter(totalWorkers, { verbose: options.verbose });
progressReporter.start();
progressReporter.setTotal(totalEvalCount);
const seenCodexLogPaths = new Set<string>();
const unsubscribeCodexLogs = subscribeToCodexLogEntries((entry) => {
if (!entry.filePath || seenCodexLogPaths.has(entry.filePath)) {
return;
}
seenCodexLogPaths.add(entry.filePath);
progressReporter.addLogPaths([entry.filePath], 'codex');
});
const seenPiLogPaths = new Set<string>();
const unsubscribePiLogs = subscribeToPiLogEntries((entry) => {
if (!entry.filePath || seenPiLogPaths.has(entry.filePath)) {
return;
}
seenPiLogPaths.add(entry.filePath);
progressReporter.addLogPaths([entry.filePath], 'pi');
});
for (const [testFilePath, meta] of fileMetadata.entries()) {
for (const evalId of meta.evalIds) {
const evalKey = makeEvalKey(testFilePath, evalId);
seenEvalCases.add(evalKey);
const displayId = displayIdTracker.getOrAssign(evalKey);
progressReporter.update(displayId, {
workerId: displayId,
evalId,
status: 'pending',
targetLabel: meta.inlineTargetLabel,
});
}
}
try {
await runWithLimit(resolvedTestFiles, fileConcurrency, async (testFilePath) => {
const targetPrep = fileMetadata.get(testFilePath);
if (!targetPrep) {
throw new Error(`Missing metadata for ${testFilePath}`);
}
const result = await runSingleEvalFile({
testFilePath,
cwd,
repoRoot,
options,
outputWriter,
cache,
evaluationRunner,
workersOverride: perFileWorkers,
progressReporter,
seenEvalCases,
displayIdTracker,
selection: targetPrep.selection,
inlineTargetLabel: targetPrep.inlineTargetLabel,
evalCases: targetPrep.evalCases,
});
allResults.push(...result.results);
});
progressReporter.finish();
const summary = calculateEvaluationSummary(allResults);
console.log(formatEvaluationSummary(summary));
if (allResults.length > 0) {
console.log(`\nResults written to: ${outputPath}`);
}
} finally {
unsubscribeCodexLogs();
unsubscribePiLogs();
await outputWriter.close().catch(() => undefined);
}
}
async function resolveEvaluationRunner(): Promise<typeof defaultRunEvaluation> {
const overridePath = process.env.AGENTEVO_CLI_EVAL_RUNNER;
if (!overridePath) {
return defaultRunEvaluation;
}
const resolved = path.isAbsolute(overridePath)
? overridePath
: path.resolve(process.cwd(), overridePath);
const moduleUrl = pathToFileURL(resolved).href;
const mod = await import(moduleUrl);
const candidate = mod.runEvaluation;
if (typeof candidate !== 'function') {
throw new Error(
`Module '${resolved}' must export a 'runEvaluation' function to override the default implementation`,
);
}
return candidate as typeof defaultRunEvaluation;
}