/**
 * chronic-failures command — query recent reports to find tasks that
 * consistently error above a configurable threshold.
 *
 * @see docs/exec-plans/eval-pipeline-timeout-resilience.md — Phase 5
 */
import { Command } from "commander";
import { detectChronicFailures, formatChronicFailuresConsole, } from "../pipeline/chronic-failures.js";
import { ReportStore } from "../report-store.js";
export function createChronicFailuresCommand() {
    return new Command("chronic-failures")
        .description("Identify tasks that error in >50% of recent evaluation runs")
        .option("--lookback <n>", "Number of recent reports to analyze", (v) => parseInt(v, 10), 10)
        .option("--error-rate <n>", "Error rate threshold (0-1) for chronic classification", (v) => parseFloat(v), 0.5)
        .option("-f, --format <fmt>", "Output format: console or json", "console")
        .action(async (opts) => {
        const reportStore = new ReportStore();
        const report = await detectChronicFailures(reportStore, {
            lookback: opts.lookback,
            threshold: opts.errorRate,
        });
        if (opts.format === "json") {
            console.log(JSON.stringify(report, null, 2));
        }
        else {
            console.log(formatChronicFailuresConsole(report));
        }
        // Exit with non-zero if chronic failures detected
        if (report.failures.length > 0) {
            process.exit(1);
        }
    });
}
