/**
 * Pipeline step: Grader consistency analysis.
 *
 * Calls pipeline/grader-consistency-runner.ts directly with typed options.
 * No env bridge needed — all parameters are passed directly.
 */
import { existsSync } from "fs";
import { resolve } from "path";
import { LiteracyVariant } from "../../pipeline/normalize-mode.js";
import { checkResultsExist } from "../../pipeline/checks.js";
import { resultsFileForMode } from "../../pipeline/eval-constants.js";
import { runGraderConsistency } from "../../pipeline/grader-consistency-runner.js";
export class GraderConsistencyStep {
    name = "grader-consistency";
    optional = true;
    check() {
        return [];
    }
    async execute(ctx) {
        const start = Date.now();
        // Default-on-omit is 5 (matches consistency-analysis-friendly defaults).
        // The dashboard sends 1 by default for cost reasons (see W0283 / new-eval
        // audit S1-E). When the resolved value is <2, the analysis can't compute
        // variance — skip instead of failing so the job doesn't carry a
        // misleading `error.step: "grader-consistency"`.
        const replications = ctx.config.graderReplications ?? 5;
        if (replications < 2) {
            return {
                reason: `graderReplications=${replications} (<2) — consistency analysis requires at least 2 replications`,
                status: "skipped",
            };
        }
        const primaryResultsRun = ctx.config.mode === "literacy"
            ? ctx.config.variant === LiteracyVariant.FULL
                ? LiteracyVariant.STANDARD
                : (ctx.config.variant ?? LiteracyVariant.STANDARD)
            : ctx.config.mode;
        const resultsFile = resultsFileForMode(primaryResultsRun);
        // Precondition: results file exists
        const resultsIssues = checkResultsExist(ctx.config.rootDir, resultsFile);
        const resultsErrors = resultsIssues.filter((i) => i.severity === "error");
        if (resultsErrors.length > 0) {
            return {
                durationMs: Date.now() - start,
                error: `Results missing: ${resultsErrors.map((e) => e.message).join("; ")}. Run eval first.`,
                status: "failed",
            };
        }
        try {
            await runGraderConsistency({
                logger: ctx.logger,
                replications,
                resultsPath: resolve(ctx.config.rootDir, resultsFile),
                rootDir: ctx.config.rootDir,
            });
        }
        catch (err) {
            return {
                durationMs: Date.now() - start,
                error: `grader-consistency failed: ${err instanceof Error ? err.message : String(err)}`,
                status: "failed",
            };
        }
        // Postcondition: output file exists
        const outputPath = resolve(ctx.config.rootDir, "results", "latest", "grader-consistency.json");
        if (!existsSync(outputPath)) {
            return {
                durationMs: Date.now() - start,
                error: "grader-consistency.json was not created",
                status: "failed",
            };
        }
        return {
            durationMs: Date.now() - start,
            status: "success",
            summary: `Grader consistency analysis complete (${replications} replications)`,
        };
    }
}
