import { Dt as caseDetailSchema, Et as getCaseRowCaseKey, It as runWithEvalRegistry, J as runInEvalRuntimeScope, Ot as caseRowSchema, Z as runWithEvalClock, _t as matchesTagsFilter, bt as runManifestSchema, d as loadEvalModule, f as resolveEvalDefaultConfig, g as commitPendingCacheWrites, gt as dedupeEvalTags, ht as deriveStatusFromChildStatuses, i as isCaseChildMessage, m as buildDeclaredColumnDefs, mt as deriveStatusFromCaseRows, n as resolveRunnableEvalCases, o as stripTerminalControlCodes, pt as deriveScopedSummaryFromCases, t as filterEvalCases, u as runWithModuleIsolation, vt as validateEvalTagName, wt as buildCaseKey, xt as runSummarySchema, yt as validateTagsFilterExpression } from "./runExecution-CjWJUUZ5.mjs";
import { Result, resultify } from "t-result";
import { readFile, readdir, rm, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { existsSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { spawn } from "node:child_process";
//#region ../runner/src/chartValidation.ts
function isValidColumnMetric(metric, columnsByKey, evalId, warnings) {
	const columnDef = columnsByKey.get(metric.key);
	if (!columnDef) {
		warnings.push(`[${evalId}] chart metric references unknown column "${metric.key}" — dropped`);
		return false;
	}
	if (metric.aggregate === "passThresholdRate") {
		if (columnDef.isScore !== true || typeof columnDef.passThreshold !== "number") {
			warnings.push(`[${evalId}] chart metric "${metric.key}" uses "passThresholdRate" but the column is not a score with passThreshold — dropped`);
			return false;
		}
	}
	return true;
}
function isValidTooltipExtra(extra, columnsByKey, evalId, warnings) {
	const columnDef = columnsByKey.get(extra.key);
	if (!columnDef) {
		warnings.push(`[${evalId}] chart tooltip extra references unknown column "${extra.key}" — dropped`);
		return false;
	}
	if (extra.aggregate === "passThresholdRate") {
		if (columnDef.isScore !== true || typeof columnDef.passThreshold !== "number") {
			warnings.push(`[${evalId}] chart tooltip extra "${extra.key}" uses "passThresholdRate" but the column is not a score with passThreshold — dropped`);
			return false;
		}
	}
	return true;
}
function sanitizeChart(chart, columnsByKey, evalId, warnings) {
	const metrics = chart.metrics.filter((metric) => {
		if (metric.source === "builtin") return true;
		return isValidColumnMetric(metric, columnsByKey, evalId, warnings);
	});
	if (metrics.length === 0) {
		warnings.push(`[${evalId}] chart had no valid metrics after validation — chart dropped`);
		return null;
	}
	const tooltipExtras = chart.tooltipExtras?.filter((extra) => {
		if (extra.source === "builtin") return true;
		return isValidTooltipExtra(extra, columnsByKey, evalId, warnings);
	});
	return {
		...chart,
		metrics,
		tooltipExtras: tooltipExtras?.length ? tooltipExtras : void 0
	};
}
/**
* Validate and sanitize an authored `charts` config against the eval's
* declared columns. Drops metrics/extras that reference unknown columns or
* misuse `passThresholdRate`, and drops entire charts whose metrics are all
* invalid. Returns `charts: undefined` when nothing valid remains so the UI
* falls back to rendering no chart (matching the opt-in default).
*/
function validateCharts(params) {
	const { charts, columnDefs, evalId } = params;
	if (!charts || charts.length === 0) return {
		charts: void 0,
		warnings: []
	};
	const columnsByKey = new Map(columnDefs.map((def) => [def.key, def]));
	const warnings = [];
	const sanitized = [];
	for (const chart of charts) {
		const result = sanitizeChart(chart, columnsByKey, evalId, warnings);
		if (result) sanitized.push(result);
	}
	return {
		charts: sanitized.length > 0 ? sanitized : void 0,
		warnings
	};
}
//#endregion
//#region ../runner/src/discovery.ts
const evalIdMatchRegex = /\bid\s*:\s*['"]([^'"]+)['"]/;
const evalTitleMatchRegex = /\btitle\s*:\s*['"]([^'"]+)['"]/;
/** Parse static eval metadata and discovery issues from one eval file. */
function parseEvalDiscovery(filePath, content) {
	const metas = [];
	let searchIndex = 0;
	while (searchIndex < content.length) {
		const defineEvalIndex = content.indexOf("defineEval", searchIndex);
		if (defineEvalIndex === -1) break;
		const extracted = extractDefineEvalObject(content, defineEvalIndex);
		if (!extracted) {
			searchIndex = defineEvalIndex + 10;
			continue;
		}
		const id = evalIdMatchRegex.exec(extracted.objectText)?.[1];
		if (id !== void 0) {
			const result = {
				filePath,
				id
			};
			const title = evalTitleMatchRegex.exec(extracted.objectText)?.[1];
			if (title !== void 0) result.title = title;
			metas.push(result);
		}
		searchIndex = extracted.nextIndex;
	}
	const countsById = /* @__PURE__ */ new Map();
	for (const meta of metas) countsById.set(meta.id, (countsById.get(meta.id) ?? 0) + 1);
	const duplicateIds = new Set([...countsById].filter(([, count]) => count > 1).map(([id]) => id));
	const issues = [...duplicateIds].map((evalId) => ({
		type: "duplicate-eval-id",
		severity: "error",
		filePath,
		evalId,
		message: `Duplicate eval id "${evalId}" in ${filePath}. Eval ids must be unique within one file.`
	}));
	return {
		metas: metas.filter((meta) => !duplicateIds.has(meta.id)),
		issues
	};
}
function extractDefineEvalObject(content, defineEvalIndex) {
	const openParenIndex = content.indexOf("(", defineEvalIndex);
	if (openParenIndex === -1) return void 0;
	const objectStartIndex = content.indexOf("{", openParenIndex);
	if (objectStartIndex === -1) return void 0;
	let depth = 0;
	let quote;
	let inBlockComment = false;
	let inLineComment = false;
	let isEscaped = false;
	for (let index = objectStartIndex; index < content.length; index++) {
		const currentChar = content[index];
		const nextChar = content[index + 1];
		if (inLineComment) {
			if (currentChar === "\n") inLineComment = false;
			continue;
		}
		if (inBlockComment) {
			if (currentChar === "*" && nextChar === "/") {
				inBlockComment = false;
				index++;
			}
			continue;
		}
		if (quote) {
			if (isEscaped) {
				isEscaped = false;
				continue;
			}
			if (currentChar === "\\") {
				isEscaped = true;
				continue;
			}
			if (currentChar === quote) quote = void 0;
			continue;
		}
		if (currentChar === "/" && nextChar === "/") {
			inLineComment = true;
			index++;
			continue;
		}
		if (currentChar === "/" && nextChar === "*") {
			inBlockComment = true;
			index++;
			continue;
		}
		if (currentChar === "\"" || currentChar === "'" || currentChar === "`") {
			quote = currentChar;
			continue;
		}
		if (currentChar === "{") {
			depth++;
			continue;
		}
		if (currentChar === "}") {
			depth--;
			if (depth === 0) return {
				nextIndex: index + 1,
				objectText: content.slice(objectStartIndex, index + 1)
			};
		}
	}
}
//#endregion
//#region ../runner/src/evalRegistryLoader.ts
async function loadIsolatedEvalRegistry(params) {
	return await runWithEvalRegistry(async (registry) => {
		await runWithModuleIsolation(params.moduleIsolation, async () => {
			await runInEvalRuntimeScope(params.runtimeScope, async () => {
				await loadEvalModule(params.evalFilePath, params.sourceFingerprint);
			});
		});
		return registry;
	});
}
//#endregion
//#region ../runner/src/freshness.ts
/**
* Derive eval freshness from the latest run, current eval-file fingerprint,
* current git commit, and an age threshold.
*/
function deriveEvalFreshness(params) {
	const { latestRun, gitState, currentEvalSourceFingerprint, staleAfterDays, now = /* @__PURE__ */ new Date() } = params;
	const stale = latestRun?.evalSourceFingerprint !== void 0 && latestRun.evalSourceFingerprint !== null && currentEvalSourceFingerprint !== null && currentEvalSourceFingerprint !== latestRun.evalSourceFingerprint;
	const latestRunCommitSha = latestRun?.commitSha;
	if (latestRunCommitSha === void 0 || latestRunCommitSha === null) return {
		freshnessStatus: stale ? "stale" : "fresh",
		stale,
		outdated: false
	};
	if (gitState.commitSha === null) return {
		freshnessStatus: stale ? "stale" : "fresh",
		stale,
		outdated: false
	};
	if (latestRunCommitSha === gitState.commitSha) return {
		freshnessStatus: stale ? "stale" : "fresh",
		stale,
		outdated: false
	};
	const latestRunStartedAt = new Date(latestRun?.startedAt ?? "").getTime();
	if (!Number.isFinite(latestRunStartedAt)) return {
		freshnessStatus: stale ? "stale" : "fresh",
		stale,
		outdated: false
	};
	const outdated = now.getTime() - latestRunStartedAt >= staleAfterDays * 24 * 60 * 60 * 1e3;
	return {
		freshnessStatus: stale ? "stale" : outdated ? "outdated" : "fresh",
		stale,
		outdated
	};
}
/** Return the timestamp used when ordering and displaying a run recency. */
function getRunFreshnessTimestamp(manifest) {
	return manifest.endedAt ?? manifest.startedAt;
}
//#endregion
//#region ../runner/src/manualInput/walker.ts
function isObject(value) {
	return typeof value === "object" && value !== null;
}
function getZodDef(schema) {
	if (!isObject(schema)) return null;
	const zodHolder = schema._zod;
	if (!isObject(zodHolder)) return null;
	const def = zodHolder.def;
	if (!isObject(def)) return null;
	if (typeof def.type !== "string") return null;
	return {
		...def,
		type: def.type
	};
}
function getDescription(schema) {
	if (!isObject(schema)) return void 0;
	const description = schema.description;
	return typeof description === "string" ? description : void 0;
}
function getInnerSchema(def) {
	return def.innerType;
}
function getChecks(def) {
	const checks = def.checks;
	if (!Array.isArray(checks)) return [];
	const out = [];
	for (const check of checks) {
		if (!isObject(check)) continue;
		const zodHolder = check._zod;
		if (!isObject(zodHolder)) continue;
		const checkDef = zodHolder.def;
		if (!isObject(checkDef)) continue;
		if (typeof checkDef.check !== "string") continue;
		out.push({
			...checkDef,
			check: checkDef.check
		});
	}
	return out;
}
function findCheck(checks, name) {
	return checks.find((check) => check.check === name);
}
function unwrap(schema) {
	let current = schema;
	let required = true;
	let defaultValue = void 0;
	for (let depth = 0; depth < 8; depth += 1) {
		const def = getZodDef(current);
		if (!def) return null;
		if (def.type === "optional" || def.type === "nullable") {
			required = false;
			current = getInnerSchema(def);
			continue;
		}
		if (def.type === "nullish") {
			required = false;
			current = getInnerSchema(def);
			continue;
		}
		if (def.type === "default" || def.type === "prefault") {
			const raw = def.defaultValue;
			if (typeof raw === "function") defaultValue = Reflect.apply(raw, void 0, []);
			else defaultValue = raw;
			current = getInnerSchema(def);
			continue;
		}
		if (def.type === "readonly" || def.type === "pipe") {
			current = getInnerSchema(def) ?? def.in;
			continue;
		}
		return {
			schema: current,
			def,
			required,
			defaultValue
		};
	}
	return null;
}
function humaniseKey(key) {
	const spaced = key.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_-]+/g, " ").trim();
	if (!spaced) return key;
	const lowered = spaced.toLowerCase();
	return lowered.charAt(0).toUpperCase() + lowered.slice(1);
}
function normaliseSelectOptions(raw) {
	if (!raw) return void 0;
	return raw.map((entry) => {
		if (typeof entry === "string") return {
			value: entry,
			label: entry
		};
		return {
			value: entry.value,
			label: entry.label ?? entry.value
		};
	});
}
function enumOptionsFromEntries(def) {
	const entries = def.entries;
	if (!isObject(entries)) return null;
	const out = [];
	for (const [label, value] of Object.entries(entries)) if (typeof value === "string") out.push({
		value,
		label
	});
	else if (typeof value === "number") out.push({
		value: String(value),
		label
	});
	else return null;
	return out;
}
function literalUnionOptions(def) {
	const options = def.options;
	if (!Array.isArray(options)) return null;
	const out = [];
	for (const option of options) {
		const optDef = getZodDef(option);
		if (optDef?.type !== "literal") return null;
		const values = optDef.values;
		if (!Array.isArray(values) || values.length !== 1) return null;
		const value = values[0];
		if (typeof value === "string") out.push({
			value,
			label: value
		});
		else if (typeof value === "number") {
			const stringValue = String(value);
			out.push({
				value: stringValue,
				label: stringValue
			});
		} else return null;
	}
	return out.length > 0 ? out : null;
}
function literalSelectOptions(def) {
	const values = def.values;
	if (!Array.isArray(values)) return null;
	const out = [];
	for (const value of values) if (typeof value === "string") out.push({
		value,
		label: value
	});
	else if (typeof value === "number") {
		const stringValue = String(value);
		out.push({
			value: stringValue,
			label: stringValue
		});
	} else return null;
	return out;
}
function readStringChecks(def) {
	const checks = getChecks(def);
	const out = {};
	const min = findCheck(checks, "min_length");
	if (min && typeof min.minimum === "number") out.minLength = min.minimum;
	const max = findCheck(checks, "max_length");
	if (max && typeof max.maximum === "number") out.maxLength = max.maximum;
	return out;
}
const integerNumberFormats = new Set([
	"int",
	"safeint",
	"int32",
	"uint32",
	"int64",
	"uint64"
]);
function readNumberChecks(def) {
	const checks = getChecks(def);
	const out = {};
	const gt = findCheck(checks, "greater_than");
	if (gt && typeof gt.value === "number" && gt.inclusive === true) out.min = gt.value;
	const lt = findCheck(checks, "less_than");
	if (lt && typeof lt.value === "number" && lt.inclusive === true) out.max = lt.value;
	const format = findCheck(checks, "number_format");
	if (format && typeof format.format === "string" && integerNumberFormats.has(format.format)) out.integer = true;
	return out;
}
function buildField(key, fieldSchema, override) {
	const unwrapped = unwrap(fieldSchema);
	if (!unwrapped) return Result.err(/* @__PURE__ */ new Error(`manualInput: field "${key}" uses an unsupported Zod schema (could not introspect)`));
	const inner = unwrapped.def;
	const description = override?.description ?? getDescription(unwrapped.schema);
	const base = {
		key,
		label: override?.label ?? humaniseKey(key),
		description,
		placeholder: override?.placeholder,
		required: unwrapped.required,
		defaultValue: override?.defaultValue !== void 0 ? override.defaultValue : unwrapped.defaultValue
	};
	if (override?.asJson === true) {
		const rows = override.rows;
		return Result.ok({
			...base,
			kind: "json",
			rows
		});
	}
	if (override?.asFile === true) return Result.ok({
		...base,
		kind: "file",
		accept: override.accept,
		maxSizeBytes: override.maxSizeBytes
	});
	const overrideOptions = normaliseSelectOptions(override?.options);
	if (overrideOptions) return Result.ok({
		...base,
		kind: "select",
		options: overrideOptions
	});
	switch (inner.type) {
		case "string": {
			const checks = readStringChecks(inner);
			if (override?.multiline === true) return Result.ok({
				...base,
				kind: "multiline",
				rows: override.rows,
				minLength: checks.minLength,
				maxLength: checks.maxLength
			});
			return Result.ok({
				...base,
				kind: "text",
				minLength: checks.minLength,
				maxLength: checks.maxLength
			});
		}
		case "number":
		case "int":
		case "bigint": {
			const checks = readNumberChecks(inner);
			return Result.ok({
				...base,
				kind: "number",
				min: checks.min,
				max: checks.max,
				integer: checks.integer
			});
		}
		case "boolean": return Result.ok({
			...base,
			kind: "boolean"
		});
		case "enum": {
			const options = enumOptionsFromEntries(inner);
			if (options) return Result.ok({
				...base,
				kind: "select",
				options
			});
			return Result.ok({
				...base,
				kind: "json",
				rows: override?.rows
			});
		}
		case "literal": {
			const options = literalSelectOptions(inner);
			if (options && options.length > 0) return Result.ok({
				...base,
				kind: "select",
				options
			});
			return Result.ok({
				...base,
				kind: "json",
				rows: override?.rows
			});
		}
		case "union": {
			const options = literalUnionOptions(inner);
			if (options) return Result.ok({
				...base,
				kind: "select",
				options
			});
			return Result.ok({
				...base,
				kind: "json",
				rows: override?.rows
			});
		}
		default: return Result.ok({
			...base,
			kind: "json",
			rows: override?.rows
		});
	}
}
function getObjectShape(schema) {
	const def = getZodDef(schema);
	if (!def) return null;
	if (def.type !== "object") return null;
	const shape = def.shape;
	if (!isObject(shape)) return null;
	return shape;
}
/**
* Walk an eval's `manualInput` configuration and produce the wire-format
* descriptor consumed by the web UI. The schema must resolve to a top-level
* `z.object(...)`; nested objects, arrays, unions, and other unsupported
* shapes inside fields fall back to the JSON textarea widget.
*
* Returns a `Result` so the caller (eval discovery) can surface a discovery
* issue without throwing when the schema is incompatible.
*/
function buildManualInputDescriptor(config) {
	const shape = getObjectShape(config.schema);
	if (!shape) return Result.err(/* @__PURE__ */ new Error("manualInput.schema must be a top-level z.object(...). Wrap nested types in an object schema."));
	const overrides = {};
	const rawOverrides = config.fields;
	if (rawOverrides) {
		for (const [key, override] of Object.entries(rawOverrides)) if (override) overrides[key] = override;
	}
	const fields = [];
	for (const [key, fieldSchema] of Object.entries(shape)) {
		const fieldResult = buildField(key, fieldSchema, overrides[key]);
		if (fieldResult.error) return fieldResult.errorResult();
		fields.push(fieldResult.value);
	}
	return Result.ok({
		title: config.title,
		description: config.description,
		submitLabel: config.submitLabel,
		fields
	});
}
/**
* Resolve an eval's `manualInput` Zod schema against a raw user submission.
* Returns the parsed value typed against the eval's `TInput` generic, or a
* structured `Error` carrying the Zod issues for the caller to surface.
*/
function parseManualInputValues(config, raw) {
	const parsed = config.schema.safeParse(raw);
	if (!parsed.success) return Result.err(new ManualInputValidationError(parsed.error.issues.map(formatIssue)));
	return Result.ok(parsed.data);
}
/**
* Error thrown / returned when manual-input values fail validation against
* the eval's `manualInput.schema`. Carries the structured Zod issues so the
* CLI and HTTP layers can surface them per-field.
*/
var ManualInputValidationError = class extends Error {
	issues;
	constructor(issues) {
		super(issues.length === 0 ? "manualInput validation failed" : `manualInput validation failed: ${issues.map((issue) => issue.path ? `${issue.path}: ${issue.message}` : issue.message).join("; ")}`);
		this.name = "ManualInputValidationError";
		this.issues = issues;
	}
};
function formatIssue(issue) {
	return {
		path: issue.path.map((segment) => typeof segment === "string" || typeof segment === "number" ? String(segment) : "").filter((segment) => segment !== "").join("."),
		message: issue.message
	};
}
//#endregion
//#region ../runner/src/runMaintenance.ts
async function persistRunState(runState) {
	await writeFile(join(runState.runDir, "summary.json"), JSON.stringify(runState.summary, null, 2));
	await writeFile(join(runState.runDir, "run.json"), JSON.stringify(runState.manifest, null, 2));
	const casesJsonl = runState.cases.map((c) => JSON.stringify(c)).join("\n");
	await writeFile(join(runState.runDir, "cases.jsonl"), casesJsonl);
}
/**
* Recompute a persisted case's status after score definitions changed.
*
* Pass/fail gates are per-score: a case fails when any score with a declared
* `passThreshold` reports a numeric value below that threshold. Scores
* without a threshold are informational and never gate. Cancelled and
* errored cases retain their terminal status.
*/
function recomputePersistedCaseStatus(caseRow, caseDetail, scoreThresholds) {
	if (caseRow.status === "cancelled") return "cancelled";
	if (caseDetail?.error !== null && caseDetail?.error !== void 0) return "error";
	if ((caseDetail?.assertionFailures.length ?? 0) > 0) return "fail";
	for (const [key, passThreshold] of scoreThresholds) {
		const rawValue = caseRow.columns[key] ?? caseDetail?.columns[key];
		if (typeof rawValue !== "number") continue;
		if (rawValue < passThreshold) return "fail";
	}
	return caseRow.status === "error" ? "error" : "pass";
}
function runTouchesEval(params) {
	if (params.caseRows.some((caseRow) => caseRow.evalKey === params.evalKey)) return true;
	if (params.target.mode === "all") return params.evalExists;
	if (params.target.mode === "evalIds") return params.target.evalKeys?.includes(params.evalKey) ?? false;
	return false;
}
async function deleteTemporaryRuns(params) {
	let deletedRuns = 0;
	for (const [runId, run] of [...params.runs]) {
		if (run.manifest.temporary !== true) continue;
		if (run.manifest.status === "running") {
			const endedAt = /* @__PURE__ */ new Date();
			run.manifest.status = "cancelled";
			run.manifest.endedAt = endedAt.toISOString();
			run.summary.status = "cancelled";
			run.summary.totalDurationMs = endedAt.getTime() - new Date(run.manifest.startedAt).getTime();
			params.cancelRunningRun(run);
		}
		params.runs.delete(runId);
		await rm(run.runDir, {
			recursive: true,
			force: true
		});
		deletedRuns += 1;
	}
	return deletedRuns;
}
async function recomputeEvalStatusesInRuns(params) {
	let updatedRuns = 0;
	for (const run of params.runs) {
		if (!runTouchesEval({
			target: run.manifest.target,
			caseRows: run.cases,
			evalKey: params.evalKey,
			evalExists: params.evalExists
		})) continue;
		if (run.manifest.status === "running") continue;
		let changed = false;
		for (const caseRow of run.cases) {
			if (caseRow.evalKey !== params.evalKey) continue;
			const caseDetail = run.caseDetails.get(getCaseRowCaseKey(caseRow));
			const nextStatus = recomputePersistedCaseStatus(caseRow, caseDetail, params.scoreThresholds);
			if (caseRow.status === nextStatus) continue;
			caseRow.status = nextStatus;
			if (caseDetail) {
				caseDetail.status = nextStatus;
				await params.persistCaseDetail(run.runDir, caseDetail);
			}
			changed = true;
		}
		if (!changed) continue;
		const derivedSummary = deriveScopedSummaryFromCases({ caseRows: run.cases });
		run.summary.totalCases = derivedSummary.totalCases;
		run.summary.passedCases = derivedSummary.passedCases;
		run.summary.failedCases = derivedSummary.failedCases;
		run.summary.errorCases = derivedSummary.errorCases;
		run.summary.cancelledCases = derivedSummary.cancelledCases;
		await persistRunState(run);
		updatedRuns += 1;
	}
	return updatedRuns;
}
//#endregion
//#region ../runner/src/runPersistence.ts
const SHORT_ID_PATTERN = /^r(\d+)$/;
/**
* Generate a filesystem-safe, sortable run id combining a UTC timestamp
* with a short random suffix.
*/
function generateRunId() {
	const now = /* @__PURE__ */ new Date();
	const pad = (n) => String(n).padStart(2, "0");
	return `${`${String(now.getUTCFullYear())}-${pad(now.getUTCMonth() + 1)}-${pad(now.getUTCDate())}T${pad(now.getUTCHours())}-${pad(now.getUTCMinutes())}-${pad(now.getUTCSeconds())}Z`}_${Math.random().toString(36).slice(2, 8)}`;
}
function parseShortIdNum(shortId) {
	if (shortId === void 0) return null;
	const match = SHORT_ID_PATTERN.exec(shortId);
	if (!match) return null;
	const num = Number(match[1]);
	if (!Number.isFinite(num)) return null;
	return num;
}
/**
* Return the next `shortId` number to assign based on the existing
* loaded snapshots. Legacy runs that don't match the `r\d+` format are
* ignored.
*/
function nextShortIdFromSnapshots(snapshots) {
	let maxNum = -1;
	for (const snapshot of snapshots) {
		const num = parseShortIdNum(snapshot.manifest.shortId);
		if (num !== null && num > maxNum) maxNum = num;
	}
	return maxNum + 1;
}
async function loadPersistedRunSnapshots(localStateDir) {
	const runsDir = join(localStateDir, "runs");
	const entriesResult = await resultify(() => readdir(runsDir, { withFileTypes: true }));
	if (entriesResult.error) return [];
	const snapshots = [];
	const runDirs = entriesResult.value.filter((entry) => entry.isDirectory()).map((entry) => join(runsDir, entry.name)).toSorted();
	for (const runDir of runDirs) {
		const snapshot = await loadPersistedRunSnapshot(runDir);
		if (!snapshot) continue;
		snapshots.push(snapshot);
	}
	return snapshots;
}
async function persistCaseDetail(runDir, caseDetail, fileId = caseDetail.caseId) {
	await writeFile(join(runDir, "case-details", `${encodeCaseDetailFileName(fileId)}.json`), JSON.stringify(caseDetail, null, 2));
}
function getLastRunStatuses(params) {
	const latestRunInfos = getLatestRunInfos(params);
	return new Map([...latestRunInfos].map(([evalId, info]) => [evalId, info.status]));
}
/**
* Return the latest scoped run metadata for each eval based on persisted and
* in-memory runs.
*/
function getLatestRunInfos(params) {
	const { runs, knownEvals } = params;
	const knownEvalMetas = [...knownEvals];
	const manualScoreKeysByEval = new Map(knownEvalMetas.map((evalMeta) => [evalMeta.key, evalMeta.columnDefs.filter((columnDef) => columnDef.isManualScore === true).map((columnDef) => columnDef.key)]));
	const orderedRuns = [...runs].toSorted((a, b) => new Date(getRunFreshnessTimestamp(a.manifest)).getTime() - new Date(getRunFreshnessTimestamp(b.manifest)).getTime());
	const latestRunInfos = /* @__PURE__ */ new Map();
	for (const run of orderedRuns) for (const evalKey of getRunEvalKeys(run, knownEvalMetas)) latestRunInfos.set(evalKey, {
		status: getEvalStatusForRun(run, evalKey, manualScoreKeysByEval.get(evalKey) ?? []),
		startedAt: getRunFreshnessTimestamp(run.manifest),
		commitSha: run.manifest.commitSha ?? null,
		evalSourceFingerprint: run.manifest.evalSourceFingerprints[evalKey] ?? null
	});
	return latestRunInfos;
}
function toLastRunStatus$1(status) {
	return status === "pending" ? null : status;
}
async function loadPersistedRunSnapshot(runDir) {
	const manifest = await readParsedJsonFile(join(runDir, "run.json"), { safeParse: runManifestSchema.safeParse.bind(runManifestSchema) });
	if (!manifest) return null;
	const summary = await readParsedJsonFile(join(runDir, "summary.json"), { safeParse: runSummarySchema.safeParse.bind(runSummarySchema) });
	if (!summary) return null;
	return {
		runDir,
		manifest,
		summary,
		cases: await readCaseRows(runDir),
		caseDetails: await readCaseDetails(runDir)
	};
}
async function readParsedJsonFile(filePath, schema) {
	const fileResult = await resultify(() => readFile(filePath, "utf-8"));
	if (fileResult.error) return null;
	const jsonResult = resultify(() => JSON.parse(fileResult.value));
	if (jsonResult.error) return null;
	const parsed = schema.safeParse(jsonResult.value);
	if (!parsed.success) return null;
	return parsed.data;
}
async function readCaseRows(runDir) {
	const fileResult = await resultify(() => readFile(join(runDir, "cases.jsonl"), "utf-8"));
	if (fileResult.error) return [];
	const rows = [];
	for (const rawLine of fileResult.value.split("\n")) {
		const line = rawLine.trim();
		if (line.length === 0) continue;
		const jsonResult = resultify(() => JSON.parse(line));
		if (jsonResult.error) continue;
		const parsed = caseRowSchema.safeParse(jsonResult.value);
		if (!parsed.success) continue;
		rows.push(parsed.data);
	}
	return rows;
}
async function readCaseDetails(runDir) {
	const detailsDir = join(runDir, "case-details");
	const entriesResult = await resultify(() => readdir(detailsDir, { withFileTypes: true }));
	if (entriesResult.error) return /* @__PURE__ */ new Map();
	const caseDetails = /* @__PURE__ */ new Map();
	for (const entry of entriesResult.value) {
		if (!entry.isFile() || !entry.name.endsWith(".json")) continue;
		const detail = await readParsedJsonFile(join(detailsDir, entry.name), { safeParse: caseDetailSchema.safeParse.bind(caseDetailSchema) });
		if (!detail) continue;
		caseDetails.set(detail.caseKey ?? detail.caseId, detail);
	}
	return caseDetails;
}
function getRunEvalKeys(run, knownEvals) {
	const knownEvalMetas = [...knownEvals];
	const evalKeys = /* @__PURE__ */ new Set();
	for (const caseRow of run.cases) if (caseRow.evalKey !== void 0) evalKeys.add(caseRow.evalKey);
	if (run.manifest.target.mode === "evalIds") for (const evalKey of run.manifest.target.evalKeys ?? []) evalKeys.add(evalKey);
	else if (run.manifest.target.mode === "all" && evalKeys.size === 0) for (const evalMeta of knownEvalMetas) evalKeys.add(evalMeta.key);
	return [...evalKeys];
}
function getEvalStatusForRun(run, evalKey, manualScoreKeys) {
	const evalCases = run.cases.filter((caseRow) => caseRow.evalKey === evalKey);
	if (evalCases.length > 0) {
		if (hasPendingManualScores(evalCases, manualScoreKeys)) return "unscored";
		return toLastRunStatus$1(deriveStatusFromCaseRows({ caseRows: evalCases }));
	}
	return toLastRunStatus$1(deriveStatusFromChildStatuses({
		statuses: [],
		lifecycleStatus: run.manifest.status
	}));
}
function hasPendingManualScores(caseRows, manualScoreKeys) {
	if (manualScoreKeys.length === 0) return false;
	return caseRows.some((caseRow) => manualScoreKeys.some((key) => {
		const value = caseRow.columns[key];
		return typeof value !== "number" || !Number.isFinite(value);
	}));
}
function encodeCaseDetailFileName(caseId) {
	return encodeURIComponent(caseId);
}
//#endregion
//#region ../runner/src/caseChildManager.ts
const moduleMocksFlag = "--experimental-test-module-mocks";
const inspectFlagPrefix = "--inspect";
const inspectBrkFlagPrefix = "--inspect-brk";
const childOutputTailMaxLength = 12e3;
const outputHeadlineMaxLength = 240;
async function executeCaseChild(context) {
	const child = spawn(process.execPath, [...getCaseChildExecArgv(), resolveCaseChildEntrypoint()], {
		cwd: context.workspaceRoot,
		env: process.env,
		serialization: "advanced",
		stdio: [
			"ignore",
			"pipe",
			"pipe",
			"ipc"
		]
	});
	const outputTail = createCaseChildOutputTail(child);
	return await new Promise((resolvePromise, rejectPromise) => {
		let result;
		let childError;
		let settled = false;
		function settleWithError(error) {
			if (settled) return;
			settled = true;
			rejectPromise(error);
		}
		child.once("error", (error) => {
			childError = /* @__PURE__ */ new Error(`Failed to start case child: ${error.message}`);
		});
		child.on("message", (message) => {
			if (!isCaseChildMessage(message)) return;
			if (message.type === "error") {
				childError = new Error(message.message);
				return;
			}
			result = message.result;
		});
		child.once("close", (code, signal) => {
			if (childError !== void 0) {
				settleWithError(childError);
				return;
			}
			if (result !== void 0 && code === 0 && signal === null) {
				if (settled) return;
				settled = true;
				resolvePromise(result);
				return;
			}
			const reason = formatChildExitReason(code, signal);
			settleWithError(new Error(formatUnexpectedCaseChildExit(reason, outputTail)));
		});
		child.send({
			type: "start",
			context
		});
	});
}
function createCaseChildOutputTail(child) {
	const tail = {
		stdout: "",
		stderr: "",
		stdoutTruncated: false,
		stderrTruncated: false
	};
	child.stdout?.on("data", (chunk) => {
		process.stdout.write(chunk);
		const nextTail = appendOutputTail(tail.stdout, chunkToText(chunk));
		tail.stdout = nextTail.text;
		tail.stdoutTruncated = tail.stdoutTruncated || nextTail.truncated;
	});
	child.stderr?.on("data", (chunk) => {
		process.stderr.write(chunk);
		const nextTail = appendOutputTail(tail.stderr, chunkToText(chunk));
		tail.stderr = nextTail.text;
		tail.stderrTruncated = tail.stderrTruncated || nextTail.truncated;
	});
	return tail;
}
function chunkToText(chunk) {
	return typeof chunk === "string" ? chunk : chunk.toString("utf-8");
}
function appendOutputTail(current, next) {
	const combined = current + next;
	if (combined.length <= childOutputTailMaxLength) return {
		text: combined,
		truncated: false
	};
	return {
		text: combined.slice(combined.length - childOutputTailMaxLength),
		truncated: true
	};
}
function formatUnexpectedCaseChildExit(reason, outputTail) {
	const stderr = stripTerminalControlCodes(outputTail.stderr).trim();
	const stdout = stripTerminalControlCodes(outputTail.stdout).trim();
	const headline = getChildStderrHeadline(stderr);
	const sections = [headline === null ? `${reason} before sending a structured case result.` : `Case child exited before sending a structured case result: ${headline}`, reason];
	if (stderr.length > 0) sections.push(formatOutputSection("stderr", stderr, outputTail.stderrTruncated));
	if (stdout.length > 0) sections.push(formatOutputSection("stdout", stdout, outputTail.stdoutTruncated));
	return sections.join("\n\n");
}
function formatChildExitReason(code, signal) {
	if (signal !== null) return `Case child exited with signal ${signal}`;
	return `Case child exited with code ${String(code)}`;
}
function getChildStderrHeadline(stderr) {
	const line = stderr.split("\n").map((entry) => entry.trim()).find((entry) => entry.length > 0);
	if (line === void 0) return null;
	if (line.length <= outputHeadlineMaxLength) return line;
	return `${line.slice(0, outputHeadlineMaxLength)}...`;
}
function formatOutputSection(streamName, output, truncated) {
	return `${truncated ? `Case child ${streamName} (last ${String(output.length)} chars)` : `Case child ${streamName}`}:\n${output}`;
}
function getCaseChildExecArgv() {
	const execArgv = [moduleMocksFlag];
	let skipNext = false;
	for (const arg of process.execArgv) {
		if (skipNext) {
			skipNext = false;
			continue;
		}
		if (arg === "--eval" || arg === "-e" || arg === "--print" || arg === "-p") {
			skipNext = true;
			continue;
		}
		if (arg.startsWith("--eval=") || arg.startsWith("--print=")) continue;
		if (arg === "--input-type" || arg.startsWith("--input-type=")) {
			if (arg === "--input-type") skipNext = true;
			continue;
		}
		if (arg === moduleMocksFlag) continue;
		if (isInspectArg(arg)) continue;
		execArgv.push(arg);
	}
	return execArgv;
}
function isInspectArg(arg) {
	return arg === inspectFlagPrefix || arg.startsWith(`${inspectFlagPrefix}=`) || arg === inspectBrkFlagPrefix || arg.startsWith(`${inspectBrkFlagPrefix}=`);
}
function resolveCaseChildEntrypoint() {
	const currentDir = dirname(fileURLToPath(import.meta.url));
	for (const fileName of [
		"caseChild.ts",
		"caseChild.mjs",
		"caseChild.js"
	]) {
		const candidate = join(currentDir, fileName);
		if (existsSync(candidate)) return candidate;
	}
	throw new Error("Unable to locate the Agent Evals case child entrypoint.");
}
//#endregion
//#region ../runner/src/runQueue.ts
async function executeQueuedCases(params) {
	const { queuedCases, concurrency, globalTraceDisplay } = params;
	let nextCaseIndex = 0;
	let workerError = void 0;
	const workerCount = Math.min(concurrency, queuedCases.length);
	const workers = Array.from({ length: workerCount }, async () => {
		while (workerError === void 0) {
			const queuedCase = queuedCases[nextCaseIndex];
			nextCaseIndex += 1;
			if (queuedCase === void 0) return;
			try {
				await executeQueuedCase({
					queuedCase,
					globalTraceDisplay
				});
			} catch (error) {
				workerError = error instanceof Error ? error : new Error(String(error));
				return;
			}
		}
	});
	await Promise.all(workers);
	if (workerError instanceof Error) throw workerError;
	if (workerError !== void 0) throw new Error(typeof workerError === "string" ? workerError : typeof workerError === "number" || typeof workerError === "boolean" || typeof workerError === "bigint" ? String(workerError) : workerError === null ? "null" : "Unknown queue worker error");
}
async function executeQueuedCase(params) {
	const { queuedCase, globalTraceDisplay } = params;
	const startTime = Date.now();
	await queuedCase.onStart?.();
	const result = await queuedCase.execute({
		globalTraceDisplay,
		startTime
	});
	await queuedCase.onComplete(result);
}
//#endregion
//#region ../runner/src/tags.ts
function getInvalidTagMessages(params) {
	return (params.tags ?? []).flatMap((tag) => {
		const validation = validateEvalTagName(tag);
		return validation.ok ? [] : [`${params.source} tag "${tag}" is invalid: ${validation.message}`];
	});
}
/** Resolve effective eval-level tags and discovery issues for one eval. */
function resolveEvalTags(params) {
	const configTags = params.configTags ?? [];
	const removeTags = params.evalDef.removeTags ?? [];
	const messages = [
		...getInvalidTagMessages({
			tags: configTags,
			source: "config"
		}),
		...getInvalidTagMessages({
			tags: params.evalDef.tags,
			source: "eval"
		}),
		...getInvalidTagMessages({
			tags: removeTags,
			source: "removeTags"
		})
	];
	const globalTagSet = new Set(configTags);
	for (const tag of removeTags) if (!globalTagSet.has(tag)) messages.push(`removeTags tag "${tag}" is not defined in AgentEvalsConfig.tags.`);
	const removeTagSet = new Set(removeTags);
	return {
		tags: dedupeEvalTags([...configTags.filter((tag) => !removeTagSet.has(tag)), ...params.evalDef.tags ?? []]),
		issues: messages.map((message) => ({
			type: "invalid-tags",
			severity: "error",
			filePath: params.filePath,
			evalId: params.evalId,
			message: `Invalid tags for eval "${params.evalId}" in ${params.filePath}: ${message}`
		}))
	};
}
/** Return effective case tags or throw when authored case tags are invalid. */
function resolveCaseTags(params) {
	const messages = getInvalidTagMessages({
		tags: params.evalCase.tags,
		source: `case "${params.evalCase.id}"`
	});
	if (messages.length > 0) throw new Error(`Invalid tags for case "${params.evalCase.id}" in ${params.filePath}#${params.evalId}: ${messages.join("; ")}`);
	return dedupeEvalTags([...params.evalTags, ...params.evalCase.tags ?? []]);
}
/** Validate CLI/API tags filters and return the first error message. */
function validateTagsFilters(filters) {
	for (const filter of filters ?? []) {
		const error = validateTagsFilterExpression(filter);
		if (error !== null) return `Invalid --tags-filter "${filter}": ${error}`;
	}
	return null;
}
/** Filter cases by Vitest-style tag expressions. */
function filterEvalCasesByTags(cases, tagsFilter) {
	if (tagsFilter === void 0 || tagsFilter.length === 0) return [...cases];
	return cases.filter((evalCase) => matchesTagsFilter({
		tags: evalCase.tags,
		filters: tagsFilter
	}));
}
/** Return whether eval-level tags alone satisfy the run's tag filters. */
function evalTagsMatchFilter(params) {
	return matchesTagsFilter({
		tags: params.tags,
		filters: params.tagsFilter
	});
}
//#endregion
//#region ../runner/src/targeting.ts
function escapeRegex(value) {
	return value.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
}
function globToRegex(pattern) {
	const normalized = pattern.replaceAll("\\", "/");
	let regex = "^";
	for (let i = 0; i < normalized.length; i++) {
		const char = normalized[i];
		const next = normalized[i + 1];
		if (char === "*" && next === "*") {
			regex += ".*";
			i++;
		} else if (char === "*") regex += "[^/]*";
		else if (char === "?") regex += "[^/]";
		else regex += escapeRegex(char ?? "");
	}
	regex += "$";
	return new RegExp(regex);
}
function fileMatches(pattern, filePath) {
	const normalizedPattern = pattern.replaceAll("\\", "/");
	if (normalizedPattern === filePath) return true;
	return globToRegex(normalizedPattern).test(filePath);
}
function matchesFiles(evalMeta, files) {
	if (files === void 0 || files.length === 0) return true;
	return files.some((file) => fileMatches(file, evalMeta.filePath));
}
function matchesEvalIds(evalMeta, evalIds) {
	if (evalIds === void 0 || evalIds.length === 0) return true;
	return evalIds.includes(evalMeta.id);
}
function matchesEvalKeys(evalMeta, evalKeys) {
	if (evalKeys === void 0 || evalKeys.length === 0) return true;
	return evalKeys.includes(evalMeta.key);
}
/** Return the discovered evals selected by a run target. */
function getTargetEvals(params) {
	const { target } = params.request;
	return [...params.evals].filter((evalMeta) => matchesEvalKeys(evalMeta, target.evalKeys)).filter((evalMeta) => matchesEvalIds(evalMeta, target.evalIds)).filter((evalMeta) => matchesFiles(evalMeta, target.files)).toSorted((a, b) => a.filePath.localeCompare(b.filePath));
}
/** Resolve which exact eval keys a run request can affect. */
function getTargetEvalKeys(params) {
	return getTargetEvals({
		evals: params.sortedEvals,
		request: params.request
	}).map((evalMeta) => evalMeta.key);
}
//#endregion
//#region ../runner/src/runOrchestration.ts
function toOptionalSourceFingerprint(sourceFingerprint) {
	return sourceFingerprint.length > 0 ? sourceFingerprint : void 0;
}
function buildEvalPreparationModuleIsolation(params) {
	return {
		key: [
			params.runId,
			params.evalKey,
			"prepare"
		].join(":"),
		workspaceRoot: params.workspaceRoot
	};
}
/**
* Ranks case statuses from worst to best. Used to order trial attempts so the
* pessimistic (`lowestScore`) strategy can pick the worst attempt. Any
* non-terminal status outside `pass`/`fail`/`error` is treated as indistinct
* from `fail` for comparison purposes.
*/
function statusRank(status) {
	if (status === "pass") return 2;
	if (status === "error") return 0;
	return 1;
}
/**
* Returns the minimum numeric value across the declared score columns for a
* trial, or `-Infinity` when no score has a numeric value. Used as a
* tiebreaker between trials that share the same status.
*/
function minScoreValue(caseRow, scoreKeys) {
	let min = Number.POSITIVE_INFINITY;
	for (const key of scoreKeys) {
		const v = caseRow.columns[key];
		if (typeof v === "number" && Number.isFinite(v)) {
			if (v < min) min = v;
		}
	}
	return Number.isFinite(min) ? min : Number.NEGATIVE_INFINITY;
}
function compareTrialResults(left, right, scoreKeys) {
	const statusDiff = statusRank(left.caseRow.status) - statusRank(right.caseRow.status);
	if (statusDiff !== 0) return statusDiff;
	const scoreDiff = minScoreValue(left.caseRow, scoreKeys) - minScoreValue(right.caseRow, scoreKeys);
	if (scoreDiff !== 0) return scoreDiff;
	return left.caseRow.trial - right.caseRow.trial;
}
function pickWinningTrial(params) {
	const orderedAttempts = [...params.attempts].toSorted((left, right) => compareTrialResults(left, right, params.scoreKeys));
	if (params.strategy === "lowestScore") {
		const [lowestAttempt] = orderedAttempts;
		if (lowestAttempt === void 0) throw new Error("Expected at least one trial attempt");
		return lowestAttempt;
	}
	const medianAttempt = orderedAttempts[Math.floor((orderedAttempts.length - 1) / 2)];
	if (medianAttempt === void 0) throw new Error("Expected at least one trial attempt");
	return medianAttempt;
}
function formatUnknownErrorDetails(error) {
	if (error instanceof Error) return error.stack ?? error.message;
	if (typeof error === "string") return error;
	return String(error);
}
function findDuplicateCaseIds(cases) {
	const counts = /* @__PURE__ */ new Map();
	for (const evalCase of cases) counts.set(evalCase.id, (counts.get(evalCase.id) ?? 0) + 1);
	return [...counts].filter(([, count]) => count > 1).map(([caseId]) => caseId).toSorted();
}
function throwIfDiscoveryIssues(issues) {
	if (issues.length === 0) return;
	throw new Error(issues.map((issue) => issue.message).join("\n"));
}
function findAmbiguousTargetCaseIds(preparedEvals) {
	const ownersByCaseId = /* @__PURE__ */ new Map();
	for (const preparedEval of preparedEvals) for (const preparedCase of preparedEval.preparedCases) {
		const owners = ownersByCaseId.get(preparedCase.caseId) ?? /* @__PURE__ */ new Set();
		owners.add(`${preparedEval.evalMeta.filePath}#${preparedEval.evalMeta.id}`);
		ownersByCaseId.set(preparedCase.caseId, owners);
	}
	return [...ownersByCaseId].filter(([, owners]) => owners.size > 1).map(([caseId, owners]) => `${caseId} (${[...owners].join(", ")})`);
}
function buildRunErrorMessage(errors) {
	return errors.map((entry) => {
		const [firstLine, ...detailLines] = entry.details.split("\n");
		const messageLine = firstLine?.trim() ?? "Unknown error";
		const details = detailLines.join("\n").trim();
		if (details.length === 0) return `[${entry.evalId}] ${messageLine}`;
		return `[${entry.evalId}] ${messageLine}\n${details}`;
	}).join("\n");
}
function upsertCaseRow(caseRows, nextCaseRow) {
	const existingIndex = caseRows.findIndex((caseRow) => getCaseRowCaseKey(caseRow) === getCaseRowCaseKey(nextCaseRow) && caseRow.trial === nextCaseRow.trial);
	if (existingIndex === -1) {
		caseRows.push(nextCaseRow);
		return;
	}
	caseRows[existingIndex] = nextCaseRow;
}
function removeLiveCaseRows(caseRows, nextCaseRow) {
	const caseKey = getCaseRowCaseKey(nextCaseRow);
	for (let i = caseRows.length - 1; i >= 0; i--) {
		const caseRow = caseRows[i];
		if (caseRow === void 0) continue;
		if (getCaseRowCaseKey(caseRow) !== caseKey) continue;
		if (caseRow.status !== "pending" && caseRow.status !== "running") continue;
		caseRows.splice(i, 1);
	}
}
function emitCaseRowEvent(params) {
	params.emitEvent(params.runState, {
		type: params.type,
		runId: params.runState.manifest.id,
		timestamp: (/* @__PURE__ */ new Date()).toISOString(),
		payload: params.caseRow
	});
}
async function finalizePreparedCase(params) {
	const { runState, runDir, preparedEval, preparedCase, onCaseFinished, emitEvent } = params;
	if (preparedCase.finalized || preparedCase.trialResults.length === 0) return;
	preparedCase.finalized = true;
	const winningTrial = pickWinningTrial({
		strategy: runState.manifest.trialSelection,
		attempts: preparedCase.trialResults,
		scoreKeys: preparedEval.scoreKeys
	});
	if (winningTrial.pendingCacheWrites.length > 0) await commitPendingCacheWrites({
		backingStore: params.cacheStore,
		pendingWrites: winningTrial.pendingCacheWrites
	});
	const artifactFileId = getCaseArtifactFileId(runState, winningTrial.caseRow);
	removeLiveCaseRows(runState.cases, winningTrial.caseRow);
	upsertCaseRow(runState.cases, winningTrial.caseRow);
	runState.caseDetails.set(getCaseRowCaseKey(winningTrial.caseRow), winningTrial.caseDetail);
	if (winningTrial.caseRow.status === "pass") runState.summary.passedCases++;
	else if (winningTrial.caseRow.status === "error") runState.summary.errorCases++;
	else runState.summary.failedCases++;
	await writeFile(join(runDir, "traces", `${encodeURIComponent(artifactFileId)}.json`), JSON.stringify(winningTrial.caseDetail.trace, null, 2));
	await persistCaseDetail(runDir, winningTrial.caseDetail, artifactFileId);
	onCaseFinished?.(winningTrial.caseDetail, winningTrial.caseRow);
	emitCaseRowEvent({
		runState,
		emitEvent,
		type: "case.finished",
		caseRow: winningTrial.caseRow
	});
	preparedEval.evalCaseRows.push(winningTrial.caseRow);
}
function getPreparedCaseOrderKey(caseRow) {
	return `${caseRow.evalKey ?? caseRow.evalId}\u0000${caseRow.caseId}`;
}
function getCaseArtifactFileId(runState, caseRow) {
	const caseKey = getCaseRowCaseKey(caseRow);
	return runState.cases.some((existing) => existing.caseId === caseRow.caseId && getCaseRowCaseKey(existing) !== caseKey) ? caseKey : caseRow.caseId;
}
function sortCaseRowsByPreparedOrder(caseRows, preparedEvals) {
	const orderByCase = /* @__PURE__ */ new Map();
	let order = 0;
	for (const preparedEval of preparedEvals) for (const preparedCase of preparedEval.preparedCases) {
		orderByCase.set(`${preparedEval.evalMeta.key}\u0000${preparedCase.caseId}`, order);
		order++;
	}
	caseRows.sort((left, right) => {
		return (orderByCase.get(getPreparedCaseOrderKey(left)) ?? Number.MAX_SAFE_INTEGER) - (orderByCase.get(getPreparedCaseOrderKey(right)) ?? Number.MAX_SAFE_INTEGER);
	});
}
async function executeRun({ runState, request, runDir, config, cacheStore, lastRunStatusMap, latestRunInfoMap, emitEvent, emitDiscoveryEvent, workspaceRoot, getSourceFingerprint, getConfiguredConcurrency, getSortedEvalMetas, getTargetEvals, onCaseFinished }) {
	try {
		const tagsFilterError = validateTagsFilters(request.target.tagsFilter);
		if (tagsFilterError !== null) throw new Error(tagsFilterError);
		const targetEvals = getTargetEvals(request);
		emitEvent(runState, {
			type: "run.started",
			runId: runState.manifest.id,
			timestamp: (/* @__PURE__ */ new Date()).toISOString(),
			payload: runState.manifest
		});
		const evalErrors = [];
		const queuedCases = [];
		const preparedEvals = [];
		const cacheMode = runState.manifest.cacheMode ?? "use";
		const cacheEnabled = config.cache?.enabled !== false;
		for (const evalMeta of targetEvals) {
			const evalFilePath = evalMeta.sourceFilePath;
			const evalModuleIsolation = buildEvalPreparationModuleIsolation({
				runId: runState.manifest.id,
				evalKey: evalMeta.key,
				workspaceRoot
			});
			let sourceFingerprint = "";
			try {
				sourceFingerprint = getSourceFingerprint(await readFile(evalFilePath, "utf-8"));
			} catch {
				sourceFingerprint = "";
			}
			if (sourceFingerprint.length > 0) {
				runState.manifest.evalSourceFingerprints[evalMeta.key] = sourceFingerprint;
				evalMeta.sourceFingerprint = sourceFingerprint;
			} else {
				delete runState.manifest.evalSourceFingerprints[evalMeta.key];
				evalMeta.sourceFingerprint = null;
			}
			try {
				const entry = (await loadIsolatedEvalRegistry({
					evalFilePath,
					sourceFingerprint: toOptionalSourceFingerprint(sourceFingerprint),
					moduleIsolation: evalModuleIsolation,
					runtimeScope: "env"
				})).get(evalMeta.id);
				if (!entry) {
					evalErrors.push({
						evalId: evalMeta.id,
						details: `Eval "${evalMeta.id}" was not registered after importing ${evalFilePath}`
					});
					continue;
				}
				await runWithModuleIsolation(evalModuleIsolation, async () => {
					await runInEvalRuntimeScope("cases", async () => {
						await entry.use(async (evalDef) => {
							const evalTagsResult = resolveEvalTags({
								configTags: config.tags,
								evalDef,
								evalId: evalMeta.id,
								filePath: evalMeta.filePath
							});
							throwIfDiscoveryIssues(evalTagsResult.issues);
							evalMeta.tags = evalTagsResult.tags;
							if (evalDef.manualInput && evalDef.cases !== void 0) throw new Error(`Eval "${evalMeta.id}" cannot declare both "cases" and "manualInput". Remove one of them.`);
							let manualInputCase = null;
							if (evalDef.manualInput) {
								const manualTags = evalTagsResult.tags;
								if (!filterEvalCasesByTags([{
									id: `${evalMeta.id}-manual`,
									input: {},
									tags: manualTags
								}], request.target.tagsFilter).length) {
									evalMeta.caseCount = 1;
									evalMeta.caseIds = [`${evalMeta.id}-manual`];
									return;
								}
								const rawValue = request.manualInputs?.[evalMeta.key];
								if (rawValue === void 0) throw new Error(`Eval "${evalMeta.id}" requires manual input. Provide it via the run modal in the web UI or "--input" / "--input-file" on the CLI.`);
								const parsed = parseManualInputValues(evalDef.manualInput, rawValue);
								if (parsed.error) {
									const formatted = parsed.error.issues.map((issue) => issue.path ? `${issue.path}: ${issue.message}` : issue.message).join("; ");
									throw new Error(`Invalid manual input for eval "${evalMeta.id}": ${formatted}`);
								}
								manualInputCase = {
									id: `${evalMeta.id}-manual`,
									input: parsed.value,
									tags: manualTags
								};
							}
							const evalCases = manualInputCase ? [manualInputCase] : typeof evalDef.cases === "function" && !evalTagsMatchFilter({
								tags: evalTagsResult.tags,
								tagsFilter: request.target.tagsFilter
							}) ? [] : await runWithEvalClock(evalDef.startTime, async () => typeof evalDef.cases === "function" ? await evalDef.cases() : evalDef.cases ?? [], { freezeTime: evalDef.freezeTime });
							const runnableCases = (manualInputCase ? evalCases : resolveRunnableEvalCases({
								cases: evalCases,
								evalId: evalMeta.id
							})).map((evalCase) => ({
								...evalCase,
								tags: resolveCaseTags({
									evalTags: evalTagsResult.tags,
									evalCase,
									evalId: evalMeta.id,
									filePath: evalMeta.filePath
								})
							}));
							const duplicateCaseIds = findDuplicateCaseIds(runnableCases);
							if (duplicateCaseIds.length > 0) throw new Error(`Duplicate case id${duplicateCaseIds.length === 1 ? "" : "s"} in ${evalMeta.filePath}#${evalMeta.id}: ${duplicateCaseIds.join(", ")}`);
							const cases = filterEvalCasesByTags(filterEvalCases(runnableCases, request.target.caseIds), request.target.tagsFilter);
							evalMeta.caseCount = runnableCases.length;
							evalMeta.caseIds = runnableCases.map((evalCase) => evalCase.id);
							runState.summary.totalCases += cases.length;
							const defaultConfig = resolveEvalDefaultConfig({
								evalDef,
								globalColumns: config.columns,
								globalStats: config.stats,
								globalDefaultStatAggregate: config.defaultStatAggregate,
								globalRemove: config.removeDefaultConfig
							});
							const declaredColumnDefs = buildDeclaredColumnDefs(defaultConfig.columns, evalDef.scores, evalDef.manualScores);
							const validatedCharts = validateCharts({
								charts: defaultConfig.charts,
								columnDefs: declaredColumnDefs,
								evalId: evalMeta.id
							});
							for (const warning of validatedCharts.warnings) console.warn(warning);
							evalMeta.columnDefs = declaredColumnDefs;
							evalMeta.stats = defaultConfig.stats;
							evalMeta.defaultStatAggregate = defaultConfig.defaultStatAggregate;
							evalMeta.charts = validatedCharts.charts;
							const evalCaseRows = [];
							const preparedCases = [];
							const scoreKeys = Object.freeze(Object.keys(evalDef.scores ?? {}));
							const manualScoreKeys = Object.freeze(Object.keys(evalDef.manualScores ?? {}));
							const preparedEval = {
								evalMeta,
								evalCaseRows,
								preparedCases,
								scoreKeys: Object.freeze([...scoreKeys, ...manualScoreKeys])
							};
							preparedEvals.push(preparedEval);
							for (const evalCase of cases) {
								const trialResults = [];
								const liveCaseRow = {
									caseId: evalCase.id,
									evalId: evalMeta.id,
									evalKey: evalMeta.key,
									caseKey: buildCaseKey({
										filePath: evalMeta.filePath,
										evalId: evalMeta.id,
										caseId: evalCase.id
									}),
									tags: evalCase.tags,
									status: "pending",
									durationMs: null,
									cacheHits: 0,
									cacheOperations: 0,
									columns: {},
									trial: 0
								};
								const preparedCase = {
									caseId: evalCase.id,
									liveCaseRow,
									trialResults,
									finalized: false
								};
								preparedCases.push(preparedCase);
								upsertCaseRow(runState.cases, liveCaseRow);
								emitCaseRowEvent({
									runState,
									emitEvent,
									type: "case.updated",
									caseRow: liveCaseRow
								});
								for (let trial = 0; trial < request.trials; trial++) queuedCases.push({
									onStart: () => {
										if (preparedCase.finalized) return;
										preparedCase.liveCaseRow = {
											...preparedCase.liveCaseRow,
											status: "running"
										};
										upsertCaseRow(runState.cases, preparedCase.liveCaseRow);
										emitCaseRowEvent({
											runState,
											emitEvent,
											type: "case.started",
											caseRow: preparedCase.liveCaseRow
										});
									},
									execute: async ({ startTime, globalTraceDisplay }) => await executeCaseChild({
										evalId: evalMeta.id,
										evalKey: evalMeta.key,
										evalFilePath,
										evalFileRelativePath: evalMeta.filePath,
										sourceFingerprint: toOptionalSourceFingerprint(sourceFingerprint),
										evalCase,
										trial,
										startTime,
										cacheMode,
										cacheEnabled,
										globalTraceDisplay,
										workspaceRoot,
										artifactDir: join(runDir, "artifacts"),
										runId: runState.manifest.id
									}),
									onComplete: async ({ caseDetail, caseRow, pendingCacheWrites }) => {
										trialResults.push({
											caseDetail,
											caseRow,
											pendingCacheWrites
										});
										if (trialResults.length !== request.trials) return;
										await finalizePreparedCase({
											runState,
											runDir,
											cacheStore,
											preparedEval,
											preparedCase,
											onCaseFinished,
											emitEvent
										});
									}
								});
							}
						});
					});
				});
			} catch (error) {
				console.error(`Error running eval ${evalMeta.id}:`, error);
				evalErrors.push({
					evalId: evalMeta.id,
					details: formatUnknownErrorDetails(error)
				});
				lastRunStatusMap.set(evalMeta.key, "error");
				latestRunInfoMap.set(evalMeta.key, {
					status: "error",
					startedAt: runState.manifest.endedAt ?? runState.manifest.startedAt,
					commitSha: runState.manifest.commitSha ?? null,
					evalSourceFingerprint: runState.manifest.evalSourceFingerprints[evalMeta.key] ?? null
				});
			}
		}
		const ambiguousCaseTargets = request.target.caseIds && request.target.caseIds.length > 0 ? findAmbiguousTargetCaseIds(preparedEvals) : [];
		if (ambiguousCaseTargets.length > 0) {
			queuedCases.length = 0;
			evalErrors.push({
				evalId: "target",
				details: `Ambiguous --case target. Narrow it with --file and/or --eval: ${ambiguousCaseTargets.join("; ")}`
			});
		} else await executeQueuedCases({
			queuedCases,
			concurrency: getConfiguredConcurrency(),
			globalTraceDisplay: config.traceDisplay
		});
		for (const preparedEval of preparedEvals) {
			for (const preparedCase of preparedEval.preparedCases) await finalizePreparedCase({
				runState,
				runDir,
				cacheStore,
				preparedEval,
				preparedCase,
				onCaseFinished,
				emitEvent
			});
			lastRunStatusMap.set(preparedEval.evalMeta.key, toLastRunStatus(deriveStatusFromCaseRows({ caseRows: preparedEval.evalCaseRows })));
			const latestStatus = lastRunStatusMap.get(preparedEval.evalMeta.key) ?? null;
			latestRunInfoMap.set(preparedEval.evalMeta.key, {
				status: latestStatus,
				startedAt: runState.manifest.endedAt ?? runState.manifest.startedAt,
				commitSha: runState.manifest.commitSha ?? null,
				evalSourceFingerprint: runState.manifest.evalSourceFingerprints[preparedEval.evalMeta.key] ?? null
			});
		}
		sortCaseRowsByPreparedOrder(runState.cases, preparedEvals);
		for (const preparedEval of preparedEvals) sortCaseRowsByPreparedOrder(preparedEval.evalCaseRows, preparedEvals);
		const endTime = /* @__PURE__ */ new Date();
		runState.summary.totalDurationMs = endTime.getTime() - new Date(runState.manifest.startedAt).getTime();
		const finalStatus = evalErrors.length > 0 ? "error" : "completed";
		runState.summary.status = finalStatus;
		runState.manifest.status = finalStatus;
		const completedRunAt = endTime.toISOString();
		runState.manifest.endedAt = completedRunAt;
		runState.summary.errorMessage = evalErrors.length > 0 ? buildRunErrorMessage(evalErrors) : null;
		for (const evalKey of getTargetEvalKeys({
			request,
			sortedEvals: getSortedEvalMetas()
		})) {
			const latestStatus = lastRunStatusMap.get(evalKey) ?? toLastRunStatus(deriveStatusFromCaseRows({
				caseRows: [],
				lifecycleStatus: runState.manifest.status
			}));
			latestRunInfoMap.set(evalKey, {
				status: latestStatus,
				startedAt: completedRunAt,
				commitSha: runState.manifest.commitSha ?? null,
				evalSourceFingerprint: runState.manifest.evalSourceFingerprints[evalKey] ?? null
			});
		}
		await persistRunState(runState);
		emitEvent(runState, {
			type: "run.summary",
			runId: runState.manifest.id,
			timestamp: (/* @__PURE__ */ new Date()).toISOString(),
			payload: runState.summary
		});
		if (finalStatus === "error") emitEvent(runState, {
			type: "run.error",
			runId: runState.manifest.id,
			timestamp: (/* @__PURE__ */ new Date()).toISOString(),
			payload: { message: buildRunErrorMessage(evalErrors) }
		});
		else emitEvent(runState, {
			type: "run.finished",
			runId: runState.manifest.id,
			timestamp: (/* @__PURE__ */ new Date()).toISOString(),
			payload: runState.summary
		});
		emitDiscoveryEvent();
	} catch (error) {
		const message = formatUnknownErrorDetails(error);
		runState.manifest.status = "error";
		runState.manifest.endedAt = (/* @__PURE__ */ new Date()).toISOString();
		runState.summary.status = "error";
		runState.summary.errorMessage = message;
		await persistRunState(runState);
		emitEvent(runState, {
			type: "run.error",
			runId: runState.manifest.id,
			timestamp: (/* @__PURE__ */ new Date()).toISOString(),
			payload: { message }
		});
		emitDiscoveryEvent();
	}
}
function toLastRunStatus(status) {
	return status === "pending" ? null : status;
}
//#endregion
export { validateCharts as C, parseEvalDiscovery as S, runTouchesEval as _, validateTagsFilters as a, deriveEvalFreshness as b, getLatestRunInfos as c, nextShortIdFromSnapshots as d, persistCaseDetail as f, recomputePersistedCaseStatus as g, recomputeEvalStatusesInRuns as h, resolveEvalTags as i, loadPersistedRunSnapshot as l, persistRunState as m, getTargetEvalKeys as n, generateRunId as o, deleteTemporaryRuns as p, getTargetEvals as r, getLastRunStatuses as s, executeRun as t, loadPersistedRunSnapshots as u, buildManualInputDescriptor as v, loadIsolatedEvalRegistry as x, parseManualInputValues as y };
