-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.ts
More file actions
370 lines (320 loc) · 10.5 KB
/
runner.ts
File metadata and controls
370 lines (320 loc) · 10.5 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
import { spawn } from "node:child_process";
import { writeFile, mkdir, rm } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { randomBytes } from "node:crypto";
import type { ValidatedConfig, StudyConfig, Scenario } from "./config.js";
import { loadScenario } from "./config.js";
import type {
TrialOutput,
TrialMeta,
SuiteResult,
StudyResult,
ContractResult,
SPRTState,
SPRTConfig,
} from "./types.js";
import { CerberusError } from "./errors.js";
import { EXIT_CODE } from "./types.js";
import {
sprtConfigFromContract,
createSPRT,
updateSPRT,
wilsonScoreInterval,
bonferroniCorrection,
benjaminiHochbergCorrection,
} from "./stats.js";
import { evaluateContract } from "./contracts.js";
import { displayProgress } from "./output.js";
const MAX_STDOUT_BYTES = 1024 * 1024; // 1MB
export interface RunOptions {
readonly json: boolean;
}
// ── Adapter: spawn + capture ─────────────────────────────────
async function executeTrial(
config: ValidatedConfig,
scenarioPath: string,
): Promise<TrialOutput> {
const { executable, args, scenarioPlaceholderIndex } = config.parsedCommand;
const timeout = config.raw.adapter.timeout;
// Replace {{scenario}} placeholder with actual path
const resolvedArgs = args.map((arg, i) =>
i === scenarioPlaceholderIndex
? arg.replace("{{scenario}}", scenarioPath)
: arg,
);
const start = performance.now();
return new Promise<TrialOutput>((resolve) => {
const child = spawn(executable, resolvedArgs, {
shell: false,
stdio: ["ignore", "pipe", "pipe"],
timeout: 0, // we handle timeout manually
});
const stdoutChunks: Buffer[] = [];
const stderrChunks: Buffer[] = [];
let stdoutBytes = 0;
let killed = false;
child.stdout.on("data", (chunk: Buffer) => {
stdoutBytes += chunk.length;
if (stdoutBytes <= MAX_STDOUT_BYTES) {
stdoutChunks.push(chunk);
} else if (!killed) {
killed = true;
child.kill("SIGTERM");
}
});
child.stderr.on("data", (chunk: Buffer) => {
stderrChunks.push(chunk);
});
// Timeout handling: SIGTERM -> 5s -> SIGKILL
const timer = setTimeout(() => {
killed = true;
child.kill("SIGTERM");
setTimeout(() => {
try {
child.kill("SIGKILL");
} catch {
// already dead
}
}, 5000);
}, timeout);
child.on("close", (code) => {
clearTimeout(timer);
const durationMs = performance.now() - start;
const raw = Buffer.concat(stdoutChunks).toString("utf-8");
const stderr = Buffer.concat(stderrChunks).toString("utf-8");
const exitCode = code ?? 1;
let parsed: Record<string, unknown> = {};
let jsonParsed = false;
try {
const obj: unknown = JSON.parse(raw);
if (typeof obj === "object" && obj !== null && !Array.isArray(obj)) {
parsed = obj as Record<string, unknown>;
jsonParsed = true;
}
} catch {
// not valid JSON — that's fine, raw is always available
}
const meta: TrialMeta = {
raw,
stderr,
exitCode,
durationMs,
jsonParsed,
};
resolve({ meta, parsed });
});
child.on("error", (err) => {
clearTimeout(timer);
const durationMs = performance.now() - start;
const meta: TrialMeta = {
raw: "",
stderr: err.message,
exitCode: 1,
durationMs,
jsonParsed: false,
};
resolve({ meta, parsed: {} });
});
});
}
// ── Scenario temp file management ────────────────────────────
async function createTempDir(): Promise<string> {
const id = randomBytes(8).toString("hex");
const dir = join(tmpdir(), `cerberus-${id}`);
await mkdir(dir, { recursive: true });
return dir;
}
async function writeScenarioFile(
tempDir: string,
scenario: Scenario,
): Promise<string> {
const filePath = join(tempDir, "scenario.json");
await writeFile(filePath, JSON.stringify(scenario), "utf-8");
return filePath;
}
// ── Study runner ─────────────────────────────────────────────
interface ContractState {
readonly sprtConfig: SPRTConfig;
sprtState: SPRTState;
successes: number;
failures: number;
}
async function runStudy(
config: ValidatedConfig,
study: StudyConfig,
isCI: boolean,
): Promise<StudyResult> {
const start = performance.now();
// Load scenario
const scenarioPath = join(config.configDir, study.scenario);
const scenario = await loadScenario(scenarioPath);
// Create temp dir for scenario file
const tempDir = await createTempDir();
const scenarioFilePath = await writeScenarioFile(tempDir, scenario);
// Initialize SPRT state per contract
const contractStates = new Map<string, ContractState>();
for (const contract of study.contracts) {
const sprtConfig = sprtConfigFromContract(contract.threshold, contract.confidence);
contractStates.set(contract.name, {
sprtConfig,
sprtState: createSPRT(sprtConfig),
successes: 0,
failures: 0,
});
}
// Find max trials across all contracts
const maxTrials = Math.max(...study.contracts.map((c) => c.trials));
let errorCount = 0;
let totalTrials = 0;
let aborted = false;
try {
for (let trial = 0; trial < maxTrials; trial++) {
// Check if all contracts have decided
const allDecided = [...contractStates.values()].every(
(s) => s.sprtState.decision !== "continue",
);
if (allDecided) break;
// Execute trial
const output = await executeTrial(config, scenarioFilePath);
totalTrials++;
// Check for crash/error — abort takes priority over SPRT
const isError = output.meta.exitCode !== 0;
if (isError) {
errorCount++;
}
if (totalTrials >= 5) {
const errorRate = errorCount / totalTrials;
if (errorRate > study.max_error_rate) {
aborted = true;
break;
}
}
// Evaluate each contract
for (const contract of study.contracts) {
const state = contractStates.get(contract.name)!;
if (state.sprtState.decision !== "continue") continue;
if (trial >= contract.trials) continue;
const verdict = await evaluateContract(output, contract, scenario, config.raw.judges);
const success = verdict.status === "pass";
if (success) {
state.successes++;
} else {
state.failures++;
}
state.sprtState = updateSPRT(state.sprtState, state.sprtConfig, success);
}
// Display progress
if (!isCI) {
displayProgress(study.name, trial + 1, maxTrials, contractStates);
}
}
} finally {
// Cleanup temp dir
await rm(tempDir, { recursive: true, force: true }).catch(() => {});
}
// Build contract results with confidence intervals
const contractResults: ContractResult[] = [];
for (const contract of study.contracts) {
const state = contractStates.get(contract.name)!;
const total = state.successes + state.failures;
const ci = wilsonScoreInterval(state.successes, total, contract.confidence);
let status: "pass" | "fail" | "inconclusive";
if (aborted) {
status = "fail";
} else if (state.sprtState.decision === "accept") {
status = "pass";
} else if (state.sprtState.decision === "reject") {
status = "fail";
} else {
status = "inconclusive";
}
contractResults.push({
contractName: contract.name,
status,
observedRate: total > 0 ? state.successes / total : 0,
ci,
trialsEvaluated: total,
sprtStoppedEarly: state.sprtState.decision !== "continue" && total < contract.trials,
});
}
return {
studyName: study.name,
contractResults,
totalTrials,
errorCount,
aborted,
durationMs: performance.now() - start,
};
}
// ── Suite runner ─────────────────────────────────────────────
export async function runSuite(
config: ValidatedConfig,
_options: RunOptions,
): Promise<SuiteResult> {
const start = performance.now();
const isCI = !!process.env.CI;
const studies: StudyResult[] = [];
for (const studyConfig of config.raw.studies) {
try {
const result = await runStudy(config, studyConfig, isCI);
studies.push(result);
} catch (e) {
if (e instanceof CerberusError) throw e;
const msg = e instanceof Error ? e.message : String(e);
throw new CerberusError(
`Study "${studyConfig.name}" failed: ${msg}`,
EXIT_CODE.RUNTIME_ERROR,
);
}
}
// Apply multiple testing correction
applyCorrection(config, studies);
// Determine suite status
const allResults = studies.flatMap((s) => s.contractResults);
let status: SuiteResult["status"];
if (allResults.some((r) => r.status === "fail")) {
status = "fail";
} else if (allResults.some((r) => r.status === "inconclusive")) {
status = "inconclusive";
} else {
status = "pass";
}
return {
status,
studies,
durationMs: performance.now() - start,
};
}
// ── Multiple testing correction ──────────────────────────────
function applyCorrection(
config: ValidatedConfig,
studies: StudyResult[],
): void {
const correction = config.raw.correction;
if (correction === "none") return;
const allResults = studies.flatMap((s) => s.contractResults);
const n = allResults.length;
if (n <= 1) return;
if (correction === "bonferroni") {
const corrected = bonferroniCorrection(
allResults[0]!.ci.n > 0 ? 0.05 : 0.05, // base alpha
n,
);
for (let i = 0; i < n; i++) {
(allResults[i] as { correctedAlpha?: number }).correctedAlpha = corrected[i];
}
} else {
// BH correction: use 1 - observedRate as a proxy p-value
// (this is a simplification; true p-values would require more computation)
const pValues = allResults.map((r) => {
if (r.status === "pass") return 0.001; // clearly passing
if (r.status === "fail") return 0.999; // clearly failing
return 0.5; // inconclusive
});
const result = benjaminiHochbergCorrection(pValues, 0.05);
for (let i = 0; i < n; i++) {
(allResults[i] as { correctedAlpha?: number }).correctedAlpha = result.correctedAlphas[i];
}
}
}