forked from apache/maka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.ts
More file actions
489 lines (466 loc) · 15.2 KB
/
Copy pathrunner.ts
File metadata and controls
489 lines (466 loc) · 15.2 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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import {
expandExperiment,
type ExperimentCell,
type ExperimentSpec,
type JsonObject,
} from './experiment.js';
import {
decodeEvalResult,
type CellAttempt,
type EvalResult,
isReplaceableAttempt,
type NormalizedUsage,
} from './result.js';
export interface SubjectExecutionResult {
readonly output?: string;
readonly usage: NormalizedUsage | null;
readonly costUsd: number | null;
readonly durationMs: number;
readonly status: 'completed' | 'failed' | 'infra_failed' | 'indeterminate';
readonly failureReason: string | null;
readonly artifacts: readonly JsonObject[];
}
export interface SubjectExecutionContext {
readonly cwd: string;
readonly taskInput: string;
readonly metadata: JsonObject;
readonly signal?: AbortSignal;
readonly execute: (input: {
readonly command: string;
readonly args: readonly string[];
readonly environment?: Readonly<Record<string, string>>;
readonly credentialEnvironment: Readonly<Record<string, string>>;
readonly captureStdout?: boolean;
}) => Promise<{
readonly termination: 'exited' | 'framework_timeout';
readonly exitCode: number;
readonly stdout: string;
readonly diagnostic?: {
readonly category:
| 'none'
| 'unstructured-output'
| 'result-frame-missing'
| 'result-frame-invalid'
| 'result-frame-ambiguous'
| 'result-frame-oversize'
| 'execution-scope-unavailable';
readonly bytes?: number;
readonly sha256?: string;
};
}>;
}
export interface SubjectAdapter {
readonly kind: ExperimentCell['subject']['kind'];
validate?(cell: ExperimentCell): void;
prepare?(input: {
readonly spec: ExperimentSpec;
readonly cells: readonly ExperimentCell[];
}): Promise<void>;
canReuse?(input: { readonly cell: ExperimentCell; readonly attempt: CellAttempt }): boolean;
execute(input: {
readonly cell: ExperimentCell;
readonly context: SubjectExecutionContext;
}): Promise<SubjectExecutionResult>;
}
export interface ExecutorVerification {
readonly status: 'completed' | 'subject_failed' | 'infra_failed';
readonly score: number | null;
readonly failureReason: string | null;
readonly artifacts: readonly JsonObject[];
}
export type ExecutorPreparationCode =
| 'cancelled'
| 'preparation-failed'
| 'spawn-failed'
| 'exit-before-ready'
| 'invalid-ready'
| 'framework-version-mismatch';
export type ExecutorAttemptOutcome =
| { readonly kind: 'settled'; readonly value: EvalResult }
| {
readonly kind: 'indeterminate';
readonly cause: 'host-cancelled' | 'cleanup-unconfirmed';
readonly value?: EvalResult;
}
| {
readonly kind: 'not_started';
readonly code: ExecutorPreparationCode;
readonly artifacts: readonly JsonObject[];
};
export interface ExperimentExecutor {
readonly kind: string;
validate?(cell: ExperimentCell): void;
runAttempt(
input: {
readonly cell: ExperimentCell;
readonly subjectCredentialNames: readonly string[];
readonly signal?: AbortSignal;
},
operation: (attempt: {
readonly context: SubjectExecutionContext;
verify(): Promise<ExecutorVerification>;
}) => Promise<EvalResult>,
): Promise<ExecutorAttemptOutcome>;
}
export interface AttemptStore {
list(cellId: string): Promise<readonly CellAttempt[]>;
append(attempt: CellAttempt): Promise<void>;
runExclusive<T>(operation: () => Promise<T>): Promise<T>;
}
export async function runExperiment(input: {
readonly spec: ExperimentSpec;
readonly store: AttemptStore;
readonly executor: ExperimentExecutor;
readonly subjects: readonly SubjectAdapter[];
readonly cellIds?: readonly string[];
readonly signal?: AbortSignal;
readonly now?: () => number;
}): Promise<ReadonlyMap<string, CellAttempt>> {
return input.store.runExclusive(async () => {
const cells = expandExperiment(input.spec);
const selected = selectCells(cells, input.cellIds);
const subjects = new Map(input.subjects.map((subject) => [subject.kind, subject]));
const subjectCredentialNames = [
...new Set(input.spec.subjects.flatMap((subject) => subject.credentials)),
];
if (input.executor.kind !== input.spec.executor.kind) throw new Error('executor kind mismatch');
for (const cell of selected) {
input.executor.validate?.(cell);
const subject = subjects.get(cell.subject.kind);
if (!subject) throw new Error(`missing subject adapter: ${cell.subject.kind}`);
subject.validate?.(cell);
}
for (const subject of subjects.values()) {
const cellsForSubject = selected.filter((cell) => cell.subject.kind === subject.kind);
if (cellsForSubject.length > 0) {
await subject.prepare?.({ spec: input.spec, cells: cellsForSubject });
}
}
await runTaskGroups(
groupTaskCells(selected),
input.spec.execution.maxConcurrentTaskGroups,
input.signal,
async (group, fail) => {
if (input.signal?.aborted) return;
const operations = group.map(async (cell) => {
if (input.signal?.aborted) return;
const attempts = await input.store.list(cell.id);
if (input.signal?.aborted) return;
const subject = subjects.get(cell.subject.kind)!;
if (selectSubjectResult(attempts, subject, cell)) return;
const startedAt = (input.now ?? Date.now)();
const result = await executeCell(
input.executor,
subject,
cell,
subjectCredentialNames,
input.signal,
);
await input.store.append({
cellId: cell.id,
sequence: (attempts.at(-1)?.sequence ?? 0) + 1,
startedAt,
completedAt: (input.now ?? Date.now)(),
result,
});
});
await Promise.allSettled(
operations.map((operation) =>
operation.catch((error: unknown) => {
fail(error);
throw error;
}),
),
);
},
);
return new Map(
(
await Promise.all(
cells.map(
async (cell) =>
[
cell.id,
selectSubjectResult(
await input.store.list(cell.id),
subjects.get(cell.subject.kind)!,
cell,
),
] as const,
),
)
).flatMap(([cellId, result]) => (result ? [[cellId, result] as const] : [])),
);
});
}
function selectSubjectResult(
attempts: readonly CellAttempt[],
subject: SubjectAdapter,
cell: ExperimentCell,
): CellAttempt | undefined {
return [...attempts]
.sort((left, right) => left.sequence - right.sequence)
.find(
(attempt) =>
!isReplaceableAttempt(attempt) && (subject.canReuse?.({ cell, attempt }) ?? true),
);
}
function groupTaskCells(cells: readonly ExperimentCell[]): ExperimentCell[][] {
const groups = new Map<string, ExperimentCell[]>();
for (const cell of cells) {
const key = `${cell.task.id}\u0000${cell.repetition}`;
const group = groups.get(key);
if (group) group.push(cell);
else groups.set(key, [cell]);
}
return [...groups.values()];
}
async function runTaskGroups<T>(
groups: readonly T[],
maximum: number,
signal: AbortSignal | undefined,
run: (group: T, fail: (error: unknown) => void) => Promise<void>,
): Promise<void> {
let next = 0;
let failed = false;
let failure: unknown;
const fail = (error: unknown) => {
if (failed) return;
failed = true;
failure = error;
};
const worker = async () => {
for (;;) {
if (failed || signal?.aborted) return;
const group = groups[next];
next += 1;
if (group === undefined) return;
try {
await run(group, fail);
} catch (error) {
fail(error);
}
}
};
await Promise.all(
Array.from({ length: Math.min(maximum, groups.length) }, async () => await worker()),
);
if (failed) throw failure;
}
async function executeCell(
executor: ExperimentExecutor,
subject: SubjectAdapter,
cell: ExperimentCell,
subjectCredentialNames: readonly string[],
signal?: AbortSignal,
): Promise<EvalResult> {
try {
const attempt = await executor.runAttempt(
{ cell, subjectCredentialNames, ...(signal ? { signal } : {}) },
async ({ context, verify }) => {
let execution: SubjectExecutionResult;
try {
execution = decodeSubjectExecution(
await subject.execute({
cell,
context: { ...context, ...(signal ? { signal } : {}) },
}),
);
} catch {
return failure('infra_failed', 'subject execution failed');
}
if (
signal?.aborted ||
execution.status === 'infra_failed' ||
execution.status === 'indeterminate'
) {
return fromUncertainSubject(execution, signal?.aborted === true);
}
try {
const verified = decodeVerification(await verify());
return {
score: verified.score,
usage: execution.usage,
costUsd: execution.costUsd,
durationMs: execution.durationMs,
status: settledStatus(execution.status, verified.status),
failureReason:
verified.status === 'infra_failed'
? verified.failureReason
: (execution.failureReason ?? verified.failureReason),
artifacts: [...execution.artifacts, ...verified.artifacts],
};
} catch {
return failure('infra_failed', 'verification failed', execution);
}
},
);
if (attempt.kind === 'not_started') {
const cancelled = attempt.code === 'cancelled';
return failure(
cancelled ? 'indeterminate' : 'infra_failed',
cancelled ? 'executor preparation cancelled' : 'executor preparation failed',
undefined,
attempt.artifacts,
);
}
if (attempt.kind === 'settled') return decodeEvalResult(attempt.value);
const failureReason =
attempt.cause === 'host-cancelled'
? 'executor cancelled before verification completed'
: 'executor cleanup did not settle';
if (!attempt.value) return failure('indeterminate', failureReason);
const partial = decodeEvalResult(attempt.value);
return {
...partial,
score: null,
status: 'indeterminate',
failureReason,
};
} catch {
return failure('infra_failed', 'executor preparation failed');
}
}
function decodeSubjectExecution(value: unknown): SubjectExecutionResult {
const subject = exactRecord(
value,
['usage', 'costUsd', 'durationMs', 'status', 'failureReason', 'artifacts'],
['output'],
);
if (
subject.status !== 'completed' &&
subject.status !== 'failed' &&
subject.status !== 'infra_failed' &&
subject.status !== 'indeterminate'
) {
throw new Error('subject status is invalid');
}
if (subject.output !== undefined && typeof subject.output !== 'string') {
throw new Error('subject output is invalid');
}
const decoded = decodeEvalResult({
score: null,
usage: subject.usage,
costUsd: subject.costUsd,
durationMs: subject.durationMs,
status: subject.status === 'failed' ? 'subject_failed' : subject.status,
failureReason: subject.failureReason,
artifacts: subject.artifacts,
});
return {
...(subject.output === undefined ? {} : { output: subject.output }),
usage: decoded.usage,
costUsd: decoded.costUsd,
durationMs: decoded.durationMs,
status: subject.status,
failureReason: decoded.failureReason,
artifacts: decoded.artifacts,
};
}
function decodeVerification(value: unknown): ExecutorVerification {
const verification = exactRecord(value, ['status', 'score', 'failureReason', 'artifacts']);
if (
verification.status !== 'completed' &&
verification.status !== 'subject_failed' &&
verification.status !== 'infra_failed'
) {
throw new Error('verification status is invalid');
}
const decoded = decodeEvalResult({
score: verification.score,
usage: null,
costUsd: null,
durationMs: 0,
status: verification.status,
failureReason: verification.failureReason,
artifacts: verification.artifacts,
});
return {
status: verification.status,
score: decoded.score,
failureReason: decoded.failureReason,
artifacts: decoded.artifacts,
};
}
function settledStatus(
subject: SubjectExecutionResult['status'],
verification: ExecutorVerification['status'],
): EvalResult['status'] {
if (verification === 'infra_failed') return 'infra_failed';
if (subject === 'failed' || verification === 'subject_failed') return 'subject_failed';
return 'completed';
}
function exactRecord(
value: unknown,
required: readonly string[],
optional: readonly string[] = [],
): Record<string, unknown> {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
throw new Error('result envelope must be an object');
}
const record = value as Record<string, unknown>;
const allowed = new Set([...required, ...optional]);
if (
required.some((field) => !Object.hasOwn(record, field)) ||
Object.keys(record).some((field) => !allowed.has(field))
) {
throw new Error('result envelope fields are invalid');
}
return record;
}
function fromUncertainSubject(subject: SubjectExecutionResult, cancelled: boolean): EvalResult {
return {
score: null,
usage: subject.usage,
costUsd: subject.costUsd,
durationMs: subject.durationMs,
status: cancelled
? 'indeterminate'
: subject.status === 'infra_failed'
? 'infra_failed'
: 'indeterminate',
failureReason: subject.failureReason,
artifacts: subject.artifacts,
};
}
function failure(
status: 'infra_failed' | 'indeterminate',
failureReason: string,
subject?: SubjectExecutionResult,
artifacts: readonly JsonObject[] = subject?.artifacts ?? [],
): EvalResult {
return {
score: null,
usage: subject?.usage ?? null,
costUsd: subject?.costUsd ?? null,
durationMs: subject?.durationMs ?? 0,
status,
failureReason,
artifacts,
};
}
function selectCells(cells: readonly ExperimentCell[], ids?: readonly string[]): ExperimentCell[] {
if (!ids) return [...cells];
const selected = new Set(ids);
const known = new Set(cells.map(({ id }) => id));
for (const id of selected) if (!known.has(id)) throw new Error(`unknown experiment cell: ${id}`);
return cells.filter(({ id }) => selected.has(id));
}