import { R as RunRecord, a as RunSplitTag } from './run-record-DgUVo5pw.js';
import { C as CampaignResult } from './types-Beb6KPqZ.js';
import { V as VerificationReport, R as Researcher, F as FailureMode, S as SteeringChange, E as ExperimentPlan, a as ExperimentResult, b as EvalCampaignResult, c as EvalCampaignOptions } from './researcher-BaVsy0sW.js';
export { r as runEvalCampaign } from './researcher-BaVsy0sW.js';
import { S as Span } from './schema-m0gsnbt3.js';
import { T as TraceStore } from './store-CKUAgsJz.js';
import { O as OutcomeStore } from './outcome-store-D6KWmYvj.js';
export { D as DeploymentOutcome, F as FileSystemOutcomeStore, b as FileSystemOutcomeStoreOptions, I as InMemoryOutcomeStore } from './outcome-store-D6KWmYvj.js';
import { b as RubricPredictiveValidityReport } from './rubric-predictive-validity-DgBHWsh7.js';
import { I as InterimReleaseConfidence } from './sequential-5iSVfzl2.js';
import './errors-Dwqw-T_m.js';
import './llm-client-DbjLfz-K.js';
import './raw-provider-sink-C46HDghv.js';
import './summary-report-BQvXpvaR.js';
import './failure-cluster-CL7IVgkJ.js';
import './emitter-DEZwY14K.js';
import './integrity-CJzrpUua.js';

/**
 * Test-time compute scaling curves.
 *
 * The test-time-compute frontier paper (Snell et al. 2024) and the
 * subsequent o1-style scaling work both show that LLM-agent capability
 * is a function of the compute budget at inference, not just of the
 * training run. The right way to characterize a candidate is therefore
 * a *curve* — score at compute budgets {1×, 4×, 16×, …} — not a single
 * point.
 *
 * This module ships:
 *
 *   1. The compute-curve harness — `runComputeCurve(runner, budgets)` —
 *      that evaluates one candidate at a sequence of compute budgets
 *      and returns the (compute, score) curve.
 *   2. A best-of-N evaluator — `bestOfN(runner, n, scoreFn)` — the
 *      simplest test-time-compute scaling primitive: sample N
 *      independent rollouts, return the best.
 *   3. A self-consistency evaluator — `selfConsistency(runner, n)` —
 *      the majority-vote variant of best-of-N for tasks with a small
 *      categorical answer space.
 *   4. Pareto-frontier extraction over multiple candidates — given
 *      (candidate, compute, score) tuples, return the set of
 *      candidate-compute combinations that aren't dominated.
 *
 * Caveat: "compute" here is the caller's notion of a compute unit. For
 * agent eval that's typically wall-time × parallelism, or token budget,
 * or LLM-call count. We accept whatever the caller provides; the curve
 * is on whatever axis they pick.
 */
interface ComputeCurveBudget {
    /** Identifier — for the report. Common: '1x', '4x', '16x'. */
    id: string;
    /** Numeric value on the chosen axis (tokens, calls, USD, ms — caller picks). */
    cost: number;
    /** Free-form metadata (the caller can carry per-budget config). */
    meta?: Record<string, unknown>;
}
interface ComputeCurvePoint {
    budgetId: string;
    cost: number;
    score: number;
    /** Number of underlying samples used at this budget. */
    samples: number;
    /** Optional spread / variance information. */
    std?: number;
    /** Any extra metrics the runner returned. */
    metrics?: Record<string, number>;
}
interface ComputeCurve {
    candidateId: string;
    points: ComputeCurvePoint[];
    /** Rough exponent fit: score ≈ a + b * log(cost). Useful for "how steep is the curve?" */
    logSlope: number | null;
    /** Best (highest-score) point on the curve. */
    best: ComputeCurvePoint;
}
interface RunComputeCurveOptions {
    candidateId: string;
    budgets: ComputeCurveBudget[];
    /**
     * Run the candidate at one budget. Returns the realized score plus
     * optional spread + extra metrics.
     */
    runAtBudget: (budget: ComputeCurveBudget) => Promise<{
        score: number;
        samples: number;
        std?: number;
        metrics?: Record<string, number>;
    }>;
}
declare function runComputeCurve(opts: RunComputeCurveOptions): Promise<ComputeCurve>;
interface ComputeBestOfNOptions<O> {
    /** Number of independent samples to draw. */
    n: number;
    /** Sampler — produces one rollout. */
    sample: (sampleIdx: number) => Promise<O>;
    /** Score one rollout. */
    scoreFn: (rollout: O) => Promise<number> | number;
}
interface ComputeBestOfNResult<O> {
    best: O;
    bestScore: number;
    scores: number[];
    meanScore: number;
    /** Index of the best rollout, for diagnostics. */
    bestIndex: number;
}
/** The simplest test-time scaling primitive. */
declare function bestOfN<O>(opts: ComputeBestOfNOptions<O>): Promise<ComputeBestOfNResult<O>>;
interface SelfConsistencyOptions<O> {
    n: number;
    sample: (sampleIdx: number) => Promise<O>;
    /** Extract the canonical answer key (string) from a rollout. */
    answerKey: (rollout: O) => string;
}
interface SelfConsistencyResult<O> {
    /** Modal answer (the majority vote). */
    answer: string;
    /** Fraction of samples voting for the modal answer in [0, 1]. */
    agreement: number;
    /** Histogram of all answers. */
    histogram: Record<string, number>;
    /** A representative rollout that voted for the modal answer. */
    representative: O;
    /** All rollouts. */
    rollouts: O[];
}
/**
 * Self-consistency / majority-vote test-time scaling. For tasks with a
 * small categorical answer space (math problems, multiple choice).
 */
declare function selfConsistency<O>(opts: SelfConsistencyOptions<O>): Promise<SelfConsistencyResult<O>>;
/**
 * Pareto frontier over (candidate, compute, score) tuples. A point is on
 * the frontier iff no other point dominates it in both score (higher
 * better) and cost (lower better). Returns the frontier sorted ascending
 * by cost.
 */
interface ParetoPointInput {
    candidateId: string;
    budgetId: string;
    cost: number;
    score: number;
}
declare function paretoFrontier(points: ParetoPointInput[]): ParetoPointInput[];

/**
 * Contamination probe — held-out perturbation tests.
 *
 * The bug class: once a benchmark scenario set is published, models train
 * on it, and your scores become invalid. SWE-Bench-Verified, GPQA, and
 * MMLU-Pro all exist because their predecessors got contaminated within
 * months. The right defense is to keep a held-out *perturbed* version of
 * every scenario — same task, slightly different surface — and check
 * whether scores diverge significantly. Genuine capability transfers; rote
 * memorization doesn't.
 *
 * This module ships the probe contract:
 *
 *   1. A `ScenarioPerturbation` strategy type — function that produces a
 *      perturbed scenario from an original.
 *   2. `runContaminationProbe({ originals, perturbed, scoreFn })` — runs
 *      both halves and reports per-scenario score divergence + a global
 *      contamination verdict via paired Wilcoxon.
 *   3. Several stock perturbations: `renameVariables`, `shuffleOrder`,
 *      `paraphrasePrompt`, `injectIrrelevantClause`. Each preserves the
 *      task's structural difficulty while breaking surface memorization.
 *
 * The verdict is conservative: if the perturbed-vs-original score
 * difference is statistically significant (BH-adjusted p < 0.05) AND
 * the median drop is > 5 percentage points, we flag *contamination
 * suspected*. False positives are possible (the perturbation might
 * actually be harder); the default is to flag for review, not to
 * autoreject.
 */
type ScenarioPerturbationKind = 'rename_variables' | 'shuffle_order' | 'paraphrase' | 'inject_irrelevant_clause' | 'custom';
interface ScenarioPerturbation<S> {
    kind: ScenarioPerturbationKind;
    /** Apply to one scenario, return its perturbed sibling. */
    apply: (scenario: S) => Promise<S> | S;
    /** Optional id — for the report. */
    id?: string;
}
interface ContaminationProbeInput<S> {
    /** Identity of every scenario. The probe's `runFingerprint` keys on these. */
    scenarioId: (s: S) => string;
    /** Original scenarios. */
    originals: S[];
    /**
     * Either pre-computed perturbations (one per original, same order) OR a
     * `perturbation` strategy that synthesizes them on the fly.
     */
    perturbed?: S[];
    perturbation?: ScenarioPerturbation<S>;
    /**
     * Run the policy/agent against one scenario and return a scalar score
     * in [0, 1]. The probe doesn't care what the policy is — that's the
     * caller's contract.
     */
    scoreFn: (s: S) => Promise<number>;
}
interface ContaminationProbeOptions {
    /** Drop scores below this from the probe; treats partial failures separately. Default 0. */
    scoreFloor?: number;
    /**
     * BH-FDR threshold for declaring contamination on each per-scenario
     * delta. Default 0.05.
     */
    fdr?: number;
    /**
     * Minimum median per-scenario drop to flag global contamination. Default
     * 0.05 (5 percentage points). Smaller drops may be noise.
     */
    minMedianDrop?: number;
}
interface ContaminationProbeReport {
    perScenario: Array<{
        scenarioId: string;
        originalScore: number;
        perturbedScore: number;
        delta: number;
        /** Per-scenario q-value (single-test BH for a single scenario). Mainly for display. */
        qValue: number;
    }>;
    /** Wilcoxon paired-test on the deltas. */
    pairedTest: {
        w: number;
        p: number;
    };
    medianDelta: number;
    meanDelta: number;
    contaminationSuspected: boolean;
    reason: string;
    /** Number of scenarios processed. */
    n: number;
}
declare function runContaminationProbe<S>(input: ContaminationProbeInput<S>, opts?: ContaminationProbeOptions): Promise<ContaminationProbeReport>;
/**
 * Identifier-rename perturbation for code/text scenarios. Replaces every
 * occurrence of the listed identifiers with synthesized aliases. Use when
 * the scenario's structural difficulty is independent of variable names
 * (e.g. SWE-Bench-style coding tasks).
 */
declare function renameVariables<S extends {
    prompt: string;
}>(identifiers: string[], rename?: (name: string, idx: number) => string): ScenarioPerturbation<S>;
/**
 * Order-shuffle perturbation. Reshuffles a list-shaped section of the
 * prompt (for QA scenarios that present options A/B/C/D — answer depends
 * on the option labels, not order). Caller provides the section extractor.
 */
declare function shuffleOrder<S extends {
    prompt: string;
}>(shuffleSection: (prompt: string, rng: () => number) => string, seed: number): ScenarioPerturbation<S>;
/**
 * Inject-irrelevant-clause perturbation. Adds a benign sentence that
 * shouldn't change the answer. Tests for "did the model just memorize
 * the input string."
 */
declare function injectIrrelevantClause<S extends {
    prompt: string;
}>(clause: string, position?: 'prefix' | 'suffix'): ScenarioPerturbation<S>;

/**
 * Off-policy evaluation primitives.
 *
 * Standard inverse-probability-weighted (IPS), self-normalized
 * importance-weighted (SNIPS), and doubly-robust (DR) estimators for the
 * value of a *target* policy given trajectories collected under a
 * *behavior* policy. This is the canonical RL eval task: "we have last
 * week's runs, we changed the policy — how would the new one do without
 * re-running?"
 *
 * The math here is textbook (Dudík, Langford, Li 2011 for DR; Swaminathan
 * & Joachims 2015 for SNIPS) but the *application* to LLM-agent
 * evaluation needs care:
 *
 *   - The "policy" is the (prompt, tool config, model snapshot) triple.
 *     Two policies have the same probability over an action *iff* their
 *     LLM call would emit the same token with the same probability —
 *     which is generally unknowable without the model log-probs.
 *   - For LLM agents, propensity scores must be supplied by the caller
 *     (logged in the trace, recovered from token log-probs, or estimated
 *     via a learned propensity model). We do NOT estimate propensity here.
 *   - Doubly-robust requires a Q-function (model-based reward predictor).
 *     We accept any callable; consumers pass either a tabular average,
 *     a regression fit, or a learned reward model.
 *
 * Bias / variance tradeoffs:
 *   - IPS: unbiased; high variance for small overlap, infinite variance
 *     when target has support outside behavior.
 *   - SNIPS: lower variance, slight bias; usually preferred in practice.
 *   - DR: doubly-robust — unbiased if either propensity OR Q-function is
 *     correct. Lowest practical variance when Q is decent. Use this.
 *
 * Caveat the panel will land: on the LLM-agent setting, propensity scores
 * recovered from token log-probs are noisy, the action space is enormous,
 * and overlap is often poor. These estimators are useful but not magic;
 * complement with `replayCampaign` (exact replay where the request hashes
 * match) for high-confidence answers and OPE for the gap.
 */
interface OffPolicyTrajectory {
    /** Stable id, for traceability through the dataset. */
    runId: string;
    /** Reward observed under the behavior policy (the realized outcome). */
    reward: number;
    /**
     * Behavior-policy probability of the action that was taken. For LLM
     * agents this is typically `exp(sum(token_log_probs))` over the chosen
     * trajectory. Must be in (0, 1].
     */
    behaviorProb: number;
    /**
     * Target-policy probability of the same action. For replay-style
     * counterfactual evaluation this is what the *new* policy would have
     * assigned to the *old* trajectory. Must be in [0, 1].
     */
    targetProb: number;
    /**
     * Optional model-based reward prediction at the same context. Used by
     * `doublyRobust`. Set to `null` for IPS-only evaluation.
     */
    qHat?: number | null;
}
interface OffPolicyEstimate {
    /** Estimated value of the target policy. */
    value: number;
    /** Standard error of the estimate. */
    standardError: number;
    /** Effective sample size (Kong 1992). Lower = more reliance on a few high-weight samples. */
    effectiveSampleSize: number;
    /** Number of trajectories used. */
    n: number;
    /**
     * Diagnostic: maximum importance weight observed. Large values (>>10x
     * mean) are a red flag — variance is dominated by a few outliers.
     */
    maxImportanceWeight: number;
}
interface OffPolicyOptions {
    /**
     * Cap importance weights at this value (Ionides 2008 truncated IS) to
     * trade unbiasedness for variance reduction. Default `Infinity` (no cap).
     * Set e.g. `10` for stable estimates when the policies are close.
     */
    weightCap?: number;
    /** Reward clipping range. Default `[0, 1]`. */
    rewardClip?: {
        low: number;
        high: number;
    };
}
/**
 * Inverse Probability Weighting (Horvitz-Thompson). Unbiased estimator
 * of E[reward under target policy]. Variance scales with the spread of
 * target/behavior ratios.
 */
declare function inverseProbabilityWeighting(trajectories: OffPolicyTrajectory[], opts?: OffPolicyOptions): OffPolicyEstimate;
/**
 * Self-Normalized Importance Sampling. Lower variance than vanilla IPS at
 * the cost of small bias (vanishing as N grows). The right default for
 * LLM-agent evaluation where overlap is often poor.
 */
declare function selfNormalizedImportanceWeighting(trajectories: OffPolicyTrajectory[], opts?: OffPolicyOptions): OffPolicyEstimate;
/**
 * Doubly-robust off-policy estimator (Dudík, Langford, Li 2011).
 *
 *     V_DR = (1/N) * sum_i [ q_hat_i + (target_prob_i / behavior_prob_i) * (r_i - q_hat_i) ]
 *
 * Unbiased if EITHER:
 *   - the importance ratios are correct (IPS-style validity), OR
 *   - the Q-hat function is correct (model-based validity).
 *
 * In practice both are imperfect, but the residual bias is the *product*
 * of both errors — much smaller than either alone. This is why DR is the
 * default in production OPE pipelines.
 *
 * Requires `qHat` on every trajectory. If any are `null`, the estimator
 * falls back to SNIPS for those entries (loud-fallback behavior; the
 * report's `n` reflects the full set but `effectiveSampleSize` accounts
 * for the lost variance reduction).
 */
declare function doublyRobust(trajectories: OffPolicyTrajectory[], opts?: OffPolicyOptions): OffPolicyEstimate;
/**
 * Convenience: run all three estimators and return them side-by-side.
 * The recommended diagnostic — agreement across estimators is a much
 * stronger signal than any single one.
 */
declare function offPolicyEstimateAll(trajectories: OffPolicyTrajectory[], opts?: OffPolicyOptions): {
    ips: OffPolicyEstimate;
    snips: OffPolicyEstimate;
    dr: OffPolicyEstimate;
};

/**
 * Preference dataset extraction — bridge from `RunRecord[]` to RL training.
 *
 * Production RLHF / DPO / KTO / SimPO pipelines need preference triples:
 * `(prompt, chosen, rejected)`. The campaign artifact already contains the
 * ingredients — every (variantId, scenarioId, seed) cell is a candidate
 * that ran the same prompt against the same scenario, scored by the same
 * judge — but turning that into a clean preference dataset requires
 * deciding *what counts as a preference*.
 *
 * This module ships three preference-extraction strategies with explicit
 * tradeoffs, plus a unified output type compatible with HuggingFace TRL,
 * Anthropic finetuning JSONL, and OpenAI fine-tuning APIs. The strategies
 * are deliberately not auto-magical — picking the wrong one corrupts the
 * gradient.
 *
 * Strategies:
 *
 *   1. **`paired-by-scenario-and-seed`** — exact-match comparisons. For
 *      each scenario × seed pair, compare every (variantA, variantB) on
 *      that exact (scenario, seed). Matches scenarios so the comparison
 *      isolates variant effects. Highest signal-to-noise; smallest
 *      dataset (only matched pairs count).
 *
 *   2. **`paired-by-scenario`** — looser matching. For each scenario,
 *      compare every (variantA, variantB) where both have ≥ 1 run on the
 *      same scenario. Aggregates across seeds to compute mean scores per
 *      (variant, scenario), then forms preferences from the means. More
 *      data, lower per-pair signal.
 *
 *   3. **`top-vs-bottom`** — coarsest. Within each scenario, the highest-
 *      scoring run is `chosen`, the lowest is `rejected`. Smallest dataset
 *      per scenario but biggest score gap per pair. Useful for early
 *      bootstrapping when you have few variants.
 *
 * The output `PreferenceTriple` is *agent-eval-canonical* but trivially
 * mappable to TRL's `DPODataset` shape (`prompt`, `chosen`, `rejected`)
 * via the `toTRLFormat` helper.
 */

type PreferenceStrategy = 'paired-by-scenario-and-seed' | 'paired-by-scenario' | 'top-vs-bottom';
interface PreferenceTriple {
    /** The scenario (input) the variants were run against. */
    scenarioId: string;
    /** RunRecord ids on each side, for traceability. */
    chosenRunId: string;
    rejectedRunId: string;
    /** Variant ids — load-bearing for the RL update. */
    chosenVariantId: string;
    rejectedVariantId: string;
    /** The score gap between chosen and rejected. Larger = stronger signal. */
    marginScore: number;
    /**
     * Optional `(chosen_score, rejected_score)` pair for soft-margin DPO
     * variants. Omitted for `top-vs-bottom` runs that don't carry meaningful
     * scalar gaps.
     */
    scores?: {
        chosen: number;
        rejected: number;
    };
    /** Tie-breaker — when multiple seeds match this scenario, the one used. */
    seed?: number;
    /**
     * Free-form metadata propagated from the run records — e.g. original
     * prompt-hash, model, etc. Lets the RL trainer reconstruct the prompt.
     */
    meta: {
        chosenPromptHash: string;
        rejectedPromptHash: string;
        chosenConfigHash: string;
        rejectedConfigHash: string;
        chosenModel: string;
        rejectedModel: string;
    };
}
interface ExtractPreferencesOptions {
    strategy?: PreferenceStrategy;
    /**
     * Minimum score gap required to admit a pair. Pairs below this are
     * dropped — they're noise, not signal. Default 0.05 (5% of [0,1]).
     */
    minMargin?: number;
    /**
     * Optional split tag filter — restrict to runs from one split. Default
     * `'holdout'` (the canonical "real" signal).
     */
    splitTag?: RunRecord['splitTag'];
    /**
     * Optional reward extractor that overrides `outcome.holdoutScore` /
     * `outcome.searchScore`. Use to drive preferences off a verifiable
     * reward instead of the headline score.
     */
    rewardOf?: (run: RunRecord) => number | null;
}
interface PreferenceExtractionReport {
    pairs: PreferenceTriple[];
    /** Number of (scenario, seed) cells inspected. */
    cellsInspected: number;
    /** Number of pairs filtered by `minMargin`. */
    pairsBelowMargin: number;
    /** Number of cells with only one variant (no comparison possible). */
    cellsSingleton: number;
    /** Strategy used. */
    strategy: PreferenceStrategy;
}
/**
 * Convert `RunRecord[]` to preference triples for RL training.
 *
 * Returns a structured report so callers can see how much data was
 * dropped and why (low-margin pairs, singleton cells). For production
 * pipelines, you usually want to:
 *
 *   1. Run a campaign producing 5–10 variants × 50–200 scenarios × 3 seeds
 *   2. Call this with `strategy: 'paired-by-scenario-and-seed'` and a
 *      verifiable-reward extractor as `rewardOf`
 *   3. Pass `report.pairs` to `toTRLFormat` and pipe to your DPO trainer
 */
declare function extractPreferences(runs: RunRecord[], opts?: ExtractPreferencesOptions): PreferenceExtractionReport;
/**
 * TRL-compatible export. TRL's `DPODataset` is `{ prompt, chosen, rejected }`
 * but the prompt isn't stored on the RunRecord — only its hash. The caller
 * passes a `promptOf(promptHash)` lookup that the TRL trainer can use.
 */
declare function toTRLFormat(triples: PreferenceTriple[], promptOf: (hash: string) => string): Array<{
    prompt: string;
    chosen: string;
    rejected: string;
}>;
/**
 * Anthropic finetuning JSONL export — `{ system, user, assistant_chosen, assistant_rejected }`
 * shape. Same caveat as TRL: prompt + outputs are content the caller has
 * to map back from the run record / raw event log.
 */
declare function toAnthropicFormat(triples: PreferenceTriple[]): Array<{
    scenarioId: string;
    chosenRunId: string;
    rejectedRunId: string;
    margin: number;
}>;

/**
 * Adapters: convert measurement outputs into the canonical `RunRecord[]`
 * artifact that `replayCache`, `pairedEvalueSequence`, and
 * `rubricPredictiveValidity` consume. Two sources:
 *   - `campaignToRunRecords` — the campaign substrate's per-cell results
 *     (the modern path: `runCampaign` / `runImprovementLoop` → records).
 *   - `verificationReportToRunRecord` — a `MultiLayerVerifier` report.
 *
 * Adapters are thin and explicit — every mandatory `RunRecord` field comes
 * from a caller-supplied context (`commitSha`, `model`, `promptHash`,
 * `configHash`) plus the cell's runtime data. The validator still rejects
 * bare-alias model strings — the caller snapshot-pins.
 */

interface AdapterContext {
    /** Logical experiment id — typically the campaign or sweep identifier. */
    experimentId: string;
    /** Snapshot model id (e.g. `claude-sonnet-4-6@2025-04-15`). */
    model: string;
    /** Git SHA the harness was run from. */
    commitSha: string;
    /** Hash of the effective prompt sent to the model. */
    promptHash: string;
    /** Hash of the effective config (model, temperature, tools, judges, splits). */
    configHash: string;
    /** Default split tag. Default `'search'`. */
    splitTag?: RunSplitTag;
    /** Default cost in USD when the source doesn't record one. Default `0`. */
    defaultCostUsd?: number;
}
/**
 * Convert a `CampaignResult` into canonical `RunRecord[]` — one record per
 * scored cell. The cell's mean judge composite becomes the split score; every
 * judge dimension is carried through to `outcome.raw`. A cell that errored
 * becomes a record with `failureMode: 'cell_error'` (kept, not dropped — an
 * unscored cell is signal). `candidateId` identifies the measured surface
 * (defaults to the campaign manifest hash).
 */
declare function campaignToRunRecords(campaign: CampaignResult, ctx: AdapterContext & {
    candidateId?: string;
}): RunRecord[];
/**
 * Convert a `MultiLayerVerifier` `VerificationReport` into a `RunRecord`.
 * `outcome.searchScore` (or `holdoutScore`) is `report.blendedScore`;
 * `outcome.raw` carries every layer's score + a pass indicator; `failureMode`
 * is the first failing layer's reason.
 */
declare function verificationReportToRunRecord(report: VerificationReport, ctx: AdapterContext & {
    candidateId: string;
    scenarioId?: string;
}, opts?: {
    runId?: string;
}): RunRecord;

/**
 * Bradley-Terry / Elo tournament evaluation.
 *
 * For multi-candidate sweeps, comparing every candidate's score against
 * a fixed comparator wastes information — the comparator becomes a high-
 * variance reference and rank flips between near-tied middle-rank
 * candidates are dominated by noise. Pairwise tournaments fix this:
 * every (i, j) pair contributes a comparison to a Bradley-Terry MLE that
 * estimates each candidate's strength on a unified scale.
 *
 * For online updating (rolling campaigns where new candidates arrive
 * over time), we also ship classical Elo with configurable K-factor.
 *
 * References:
 *   - Bradley, R. A., Terry, M. E. (1952). Rank analysis of incomplete
 *     block designs. Biometrika, 39(3/4), 324–345.
 *   - Hunter, D. R. (2004). MM algorithms for generalized Bradley-Terry
 *     models. Annals of Statistics, 32(1), 384–406. (The MLE algorithm
 *     used here.)
 *   - Elo, A. E. (1978). The Rating of Chess Players, Past and Present.
 *
 * This is a useful primitive because most LLM-eval communities (Chatbot
 * Arena, AlpacaEval, ELO-style ablation) have converged on pairwise
 * tournament eval as the most sample-efficient and most rank-stable
 * method when you have many candidates.
 */
interface PairwiseOutcome {
    /** Winner candidate id. */
    winner: string;
    /** Loser candidate id. */
    loser: string;
    /**
     * Optional draw flag. When true, both candidates get half-credit
     * (Bradley-Terry handles draws as half-wins for each side).
     */
    draw?: boolean;
    /**
     * Optional weight — useful if some pairwise comparisons are stronger
     * signals than others (e.g. a paired test with a wider score gap is
     * a more confident comparison). Default 1.
     */
    weight?: number;
}
interface BradleyTerryRating {
    candidateId: string;
    /** Latent strength θ ≥ 0 from the BT MLE. */
    strength: number;
    /** Log-strength = log(θ) — interpretable on a linear scale. */
    logStrength: number;
    /** Number of pairwise comparisons this candidate appears in. */
    n: number;
    /** Win count (+ 0.5 per draw). */
    wins: number;
}
interface BradleyTerryFit {
    ratings: BradleyTerryRating[];
    /** Iterations of the MM algorithm before convergence. */
    iterations: number;
    /** Final maximum |θ_new - θ_old| / θ_old. */
    finalDelta: number;
    converged: boolean;
}
/**
 * Bradley-Terry MLE via Hunter's MM algorithm.
 *
 * Iteration: θ_i^new = W_i / Σ_{j ≠ i} N_ij / (θ_i + θ_j)
 *   where W_i = wins by i (+ 0.5 per draw), N_ij = total comparisons.
 *
 * Returns log-strengths normalized so the smallest is 0 (any constant
 * offset is unobservable in BT — only differences are identified).
 */
declare function fitBradleyTerry(outcomes: PairwiseOutcome[], opts?: {
    tolerance?: number;
    maxIterations?: number;
    smoothing?: number;
}): BradleyTerryFit;
/**
 * Online Elo updates. Use when comparisons arrive over time and you want
 * a running rating without re-fitting the full BT MLE on every update.
 *
 * Initialize ratings to `defaultRating` (1500 by default). Each call to
 * `applyEloUpdate` mutates the map in place and returns the deltas so
 * the caller can log per-comparison rating changes.
 */
interface EloOptions {
    /** Default rating for unseen candidates. Default 1500. */
    defaultRating?: number;
    /** K-factor controls the step size. Default 32 (FIDE-ish). */
    kFactor?: number;
}
declare function applyEloUpdate(ratings: Map<string, number>, outcome: PairwiseOutcome, opts?: EloOptions): {
    winnerDelta: number;
    loserDelta: number;
};
/**
 * Build pairwise outcomes from the campaign artifact: for every scenario
 * shared by two candidates, the higher-scoring run wins. Useful when you
 * want a tournament view of an existing campaign without an additional
 * pairwise judge call.
 */
interface BuildPairwiseFromCampaignInput {
    runs: Array<{
        candidateId: string;
        /** Stable identifier for the matching unit (typically scenarioId). */
        matchKey: string;
        score: number;
    }>;
    /**
     * Tied-score margin. Below this, the comparison is a draw. Default 0
     * (no ties).
     */
    drawMargin?: number;
}
declare function buildPairwiseFromCampaign(input: BuildPairwiseFromCampaignInput): PairwiseOutcome[];

/**
 * Verifiable reward channel.
 *
 * For RL on coding / math / theorem-proving / structured-output tasks, the
 * reward signal is *decidable* — a test passes or fails, a proof checks or
 * doesn't, an output validates against a schema or doesn't. These rewards
 * are dramatically more useful for RL training than LLM-judge scores
 * because they don't drift, can't be Goodhart-gamed by the policy in the
 * same way, and don't require a separate calibration loop.
 *
 * The `MultiLayerVerifier` already produces this signal — it just doesn't
 * surface it in a shape that's clean enough for RL training. This module
 * wraps the verifier output so consumers can:
 *
 *   1. Extract a clean `VerifiableReward` from a `VerificationReport`
 *   2. Distinguish *deterministic* rewards (compile, test, schema) from
 *      *probabilistic* rewards (judge) so they can be weighted differently
 *      in the RL training step
 *   3. Filter `RunRecord[]` to only those with a verifiable reward,
 *      producing the clean training set that DeepSeek-R1-style GRPO and
 *      AlphaProof-style search both depend on
 *
 * Why this matters: every credible 2025-2026 frontier RL result on coding
 * agents leans on verifiable reward (DeepSeek-R1 GRPO on test pass-rate,
 * o-series RL on math/code, AlphaProof on Lean kernel checking). Mixing
 * judge scores into the reward signal poisons the gradient. This module
 * is the seam.
 */

type VerifiableRewardSource = 'compile' | 'test' | 'schema' | 'sandbox' | 'judge' | 'composite';
interface VerifiableReward {
    /** Scalar in [0, 1]. The RL training signal. */
    value: number;
    /** What produced the reward — different sources have different determinism. */
    source: VerifiableRewardSource;
    /**
     * Determinism class. `'deterministic'` rewards are repeatable byte-for-byte
     * given the same inputs (compile, test, schema validation, sandbox exit code).
     * `'probabilistic'` rewards depend on a stochastic component (LLM judge).
     * Mixing these in the same training batch without separation is a known
     * footgun in production RLHF pipelines.
     */
    determinism: 'deterministic' | 'probabilistic';
    /**
     * Confidence in the reward value. For deterministic sources this is 1.0
     * (the bit either flipped or didn't). For judge sources this is the
     * judge-reported confidence or — when missing — a calibrated prior.
     */
    confidence: number;
    /** The layer / judge id that produced the signal, for provenance. */
    origin: string;
    /**
     * Any per-source breakdown the consumer might want — e.g. `{ tests_passed: 7, tests_total: 10 }`.
     */
    breakdown?: Record<string, number>;
}
interface VerifiableRewardExtractionOptions {
    /**
     * Which layers count as deterministic-reward sources. The verifier doesn't
     * tag layers as "this is verifiable"; the caller declares it via this list
     * (or via the layer name → source mapping). Default treats common names
     * (`install`, `typecheck`, `build`, `lint`, `test`, `compile`, `schema`,
     * `sandbox`) as deterministic.
     */
    deterministicLayers?: string[];
    /**
     * Map layer name → reward source. Defaults to a sensible string-match.
     */
    sourceFor?: (layerName: string) => VerifiableRewardSource;
    /**
     * Whether to fall back to a probabilistic (judge) reward when no
     * deterministic layer produced a numeric score. Default `true`. Set to
     * `false` for "deterministic-only" training pipelines that should
     * discard runs without a verifiable signal.
     */
    fallbackToJudge?: boolean;
    /**
     * Default confidence for probabilistic (judge) rewards when the judge
     * doesn't report one. Default `0.7`.
     */
    judgeConfidenceFloor?: number;
}
/**
 * Extract a `VerifiableReward` from a `VerificationReport`.
 *
 * Strategy: prefer the deterministic layers (in order: test → compile →
 * schema → sandbox), fall back to the judge layer if `fallbackToJudge` is
 * true, return `null` if no signal qualifies. When multiple deterministic
 * layers contribute, return a `'composite'` source with a weighted blend.
 */
declare function extractVerifiableReward(report: VerificationReport, opts?: VerifiableRewardExtractionOptions): VerifiableReward | null;
/**
 * Extract verifiable rewards from `RunRecord[]` produced via the
 * `verificationReportToRunRecord` adapter (which encodes per-layer scores
 * in `outcome.raw['layer.<name>']`). For records that don't carry layer
 * scores, returns `null` for that record.
 *
 * This is the canonical bridge from "campaign-shaped artifacts" to
 * "RL-training-ready reward signals": every record that has a clean
 * verifiable reward becomes a training datum, every record that doesn't
 * gets filtered out (or kept with `'probabilistic'` determinism for
 * separate downstream handling).
 */
declare function extractVerifiableRewardsFromRecords(runs: RunRecord[], opts?: VerifiableRewardExtractionOptions): Array<{
    runId: string;
    reward: VerifiableReward | null;
}>;
/** Filter `RunRecord[]` to those with deterministic verifiable rewards. */
declare function filterDeterministicallyRewarded(runs: RunRecord[], opts?: VerifiableRewardExtractionOptions): Array<{
    run: RunRecord;
    reward: VerifiableReward;
}>;

/**
 * Adaptive curriculum / active scenario selection.
 *
 * Fixed scenario sets waste sample budget on cells the policy already
 * passes (no information left) and cells the policy never passes (no
 * gradient available either). Active learning over scenarios fixes this
 * by allocating the next sample budget to cells where the policy's
 * outcome is *uncertain* — those carry the most decision-relevant signal.
 *
 * This module ships two complementary strategies:
 *
 *   1. **Variance-based** — score each (variant, scenario) cell by the
 *      empirical variance of past observations. Allocate next-round budget
 *      proportional to variance. Standard active-learning-by-uncertainty
 *      heuristic; works well when the policy is non-deterministic and
 *      cells differ in observation noise.
 *
 *   2. **Bandit-based (Thompson sampling)** — model each (variant,
 *      scenario) cell as a Beta-Bernoulli arm; sample a posterior; pick
 *      cells whose posterior mean is closest to the per-scenario decision
 *      threshold. The right primitive when scenarios are
 *      "pass/fail" rather than continuous, and when promotion gates fire
 *      at a known threshold (e.g., 0.5).
 *
 * The output is a *next-round budget allocation* — a list of (variant,
 * scenario, count) triples. The consumer's matrix runner consumes the
 * allocation, runs those cells, feeds the new observations back. Loop.
 *
 * Out of scope (deliberate): scenario *generation* — that's the
 * adversarial primitive's job. This module allocates over an existing
 * scenario pool.
 */

interface CellObservation {
    variantId: string;
    scenarioId: string;
    /** Observed score in [0, 1]. */
    score: number;
    /** For Bernoulli arms — derive from the score with a threshold if needed. */
    pass?: boolean;
}
interface CurriculumAllocation {
    variantId: string;
    scenarioId: string;
    /** How many additional reps to run on this cell. */
    count: number;
    /** Strategy-specific reason for the allocation. */
    reason: string;
}
interface VarianceCurriculumOptions {
    /** Total reps to allocate across all cells. */
    budget: number;
    /**
     * Smoothing prior on variance — keeps the allocator from concentrating
     * on a cell with one observation just because its 1-sample variance is
     * 0. Default 0.05.
     */
    variancePrior?: number;
    /**
     * Minimum reps per cell — even when the variance estimate is low, give
     * every cell at least this many. Default 1.
     */
    floorPerCell?: number;
}
/**
 * Variance-proportional allocation. For each cell, estimate variance from
 * past observations + a prior, then allocate the budget proportional to
 * (sqrt(variance) + 1/sqrt(n)) — a classical optimal-allocation rule
 * (Neyman 1934) that balances "explore noisy cells" with "explore
 * under-sampled cells."
 */
declare function varianceBasedCurriculum(observations: CellObservation[], candidateCells: Array<{
    variantId: string;
    scenarioId: string;
}>, opts: VarianceCurriculumOptions): CurriculumAllocation[];
interface ThompsonCurriculumOptions {
    budget: number;
    /**
     * The per-scenario decision threshold. Cells whose posterior mean is
     * closest to this get the most budget — that's where the next observation
     * has the highest information value for the gate decision. Default 0.5.
     */
    decisionThreshold?: number;
    /** Beta prior parameters. Default α=β=1 (uniform). */
    priorAlpha?: number;
    priorBeta?: number;
    /** Seed the Thompson sampler. Default unset (Math.random). */
    seed?: number;
}
/**
 * Thompson-sampling-style allocation for pass/fail cells. For each cell:
 *
 *   - Maintain Beta(α + passes, β + failures) posterior on pass-rate
 *   - Allocation weight ∝ exp(-((sampledMean - threshold) / σ)^2):
 *     cells whose sampled posterior straddles the decision boundary get
 *     the most weight; cells already clearly above or below get less.
 *
 * This is the right primitive when promotion gates fire at a known
 * threshold and you want to sharpen the posterior near the boundary.
 */
declare function thompsonCurriculum(observations: CellObservation[], candidateCells: Array<{
    variantId: string;
    scenarioId: string;
}>, opts: ThompsonCurriculumOptions): CurriculumAllocation[];
/** Convenience: extract `CellObservation[]` directly from `RunRecord[]`. */
declare function observationsFromRunRecords(runs: RunRecord[], opts?: {
    passThreshold?: number;
    useHoldout?: boolean;
}): CellObservation[];

/**
 * Sample-efficient adaptation evaluation.
 *
 * For foundation-model-based agents, the load-bearing capability isn't
 * raw end-state performance — it's *how fast the agent reaches that
 * performance from cold start*. The same model with a worse prompt that
 * adapts in 5 demonstrations beats the same model with a better prompt
 * that needs 50. Standard meta-learning eval (Finn et al., MAML, RL² lit)
 * reports an *adaptation curve*: score after k=0, 1, 2, 4, 8, 16, …
 * in-context examples or fine-tune steps.
 *
 * This module ships:
 *
 *   1. `runAdaptationCurve` — given a runner that takes k demonstrations
 *      and returns a score, produce the (k, score) curve.
 *   2. `compareAdaptationCurves` — paired comparison across two policies.
 *      Returns per-k delta with bootstrap CIs and an "area-under-curve"
 *      summary statistic.
 *   3. `firstPassK` — for pass/fail evaluation, the minimum k at which
 *      the policy reliably passes (≥ pass-rate threshold over reps).
 *
 * Use cases:
 *   - Compare two prompt designs that have similar end-state performance
 *     but different in-context efficiency.
 *   - Decide between fine-tuning and prompting based on adaptation cost.
 *   - Detect when a policy "memorizes" k=0 inputs vs. genuinely adapts.
 */
interface AdaptationRunner<S> {
    /**
     * Runs the policy on `scenario` with `k` demonstrations. Returns a
     * scalar score in [0, 1]. The runner is responsible for any caching;
     * the harness calls it once per (scenario, k, rep) cell.
     */
    run(args: {
        scenario: S;
        k: number;
        rep: number;
    }): Promise<number>;
}
interface RunAdaptationCurveOptions<S> {
    scenarios: S[];
    /** Number-of-shots to evaluate at. Default `[0, 1, 2, 4, 8, 16]`. */
    ks?: number[];
    /** Reps per (scenario, k) cell. Default 3. */
    reps?: number;
    runner: AdaptationRunner<S>;
    /** Pass-rate threshold for `firstPassK` reporting. Default 0.5. */
    passThreshold?: number;
}
interface AdaptationPoint {
    k: number;
    meanScore: number;
    passRate: number;
    std: number;
    n: number;
    /** Per-scenario means at this k. */
    perScenario: Array<{
        scenarioId: string;
        meanScore: number;
        passes: number;
        total: number;
    }>;
}
interface AdaptationCurve {
    points: AdaptationPoint[];
    /**
     * Smallest `k` at which `passRate ≥ passThreshold`. `null` if no `k`
     * tested reaches it.
     */
    firstPassK: number | null;
    /**
     * Area under the (k, meanScore) curve, normalized by max-k. A
     * single-number summary of "how well does this policy adapt from
     * cold-start to fully-conditioned." Higher = better adapter.
     */
    adaptationArea: number;
}
declare function runAdaptationCurve<S extends {
    scenarioId?: string;
}>(opts: RunAdaptationCurveOptions<S>): Promise<AdaptationCurve>;
interface CompareCurvesResult {
    perK: Array<{
        k: number;
        deltaMean: number;
        aLow: number;
        aHigh: number;
        bLow: number;
        bHigh: number;
    }>;
    areaDelta: number;
    firstPassKDelta: number | null;
    /** Verdict: 'a_better' | 'b_better' | 'similar'. */
    verdict: 'a_better' | 'b_better' | 'similar';
    /** Rationale, ready to render. */
    rationale: string;
}
/**
 * Paired comparison of two adaptation curves. Per-k deltas with 95%
 * bootstrap CIs (constructed from each curve's `perScenario` per-k means
 * — the bootstrap unit is the scenario, not the rep).
 */
declare function compareAdaptationCurves(a: AdaptationCurve, b: AdaptationCurve, opts?: {
    confidence?: number;
    bootstrapResamples?: number;
    seed?: number;
}): CompareCurvesResult;
/** First k at which the curve's per-scenario pass rate reliably hits the threshold. */
declare function firstPassK(curve: AdaptationCurve, threshold?: number): number | null;

/**
 * Adversarial scenario search.
 *
 * Capability evaluation on a fixed scenario set measures performance on
 * the distribution someone curated. Production failure modes live in the
 * tail — inputs the curator didn't think of, or actively avoided. The
 * adversarial-search primitive actively looks for them: starting from a
 * pool of scenarios where the policy already passes, it mutates them
 * (paraphrase, edge-case substitution, compositional combination) and
 * keeps the mutations that *break* the policy.
 *
 * This is not magic. It's the simplest version of the loop that AdA
 * (Open-Ended Adaptation, DeepMind 2023), POET, and Anthropic's
 * auto-jailbreak rigs all run: hill-climb against a failure indicator,
 * keep the survivors, repeat. We ship the harness; consumers supply the
 * mutation strategies and the failure detector.
 *
 * Why ship this in agent-eval and not as a separate red-team tool: every
 * piece of the standard adversarial loop is already in this package
 * (`runEvalCampaign` for the matrix run, `RawProviderSink` for capture,
 * `assertRunCaptured` for integrity, `pairedEvalueSequence` for stop
 * criteria). The adversarial primitive is just the *scenario-mutation
 * meta-loop* on top of that machinery.
 */
interface AdversarialScenario<S> {
    /** Stable id — used for deduplication and lineage tracking. */
    id: string;
    /** Generation index — 0 for seeds, 1 for first round of mutations, etc. */
    generation: number;
    /** Lineage — id of the parent scenario this was mutated from, if any. */
    parentId: string | null;
    scenario: S;
    /** Score on the policy under test. Lower = adversarial signal. */
    score: number | null;
    /** Strategy that produced this mutation, for diagnostics. */
    mutationStrategy: string | null;
}
interface AdversarialMutation<S> {
    id: string;
    /**
     * Mutate one scenario. Return null to skip; return one or more new
     * scenarios. The harness deduplicates by `mutateScenarioId(scenario)`.
     */
    mutate(parent: S, rng: () => number): Promise<S[]> | S[];
}
interface AdversarialSearchOptions<S> {
    /** Initial scenarios — typically those the policy currently passes. */
    seeds: S[];
    /** Stable identifier extraction. */
    mutateScenarioId: (s: S) => string;
    /** Mutation strategies. */
    mutations: AdversarialMutation<S>[];
    /**
     * Run the policy under test against one scenario, return a scalar score
     * in [0, 1]. Lower = adversarial signal.
     */
    scoreFn: (s: S) => Promise<number>;
    /**
     * Threshold below which a scenario counts as a "failure" worth keeping.
     * Default 0.5.
     */
    failureThreshold?: number;
    /** Number of mutation rounds. Default 3. */
    rounds?: number;
    /** Children per parent per round. Default 4. */
    childrenPerParent?: number;
    /** Maximum total scenarios examined. Default Infinity. */
    budget?: number;
    /** Seed for the deterministic RNG. Default 1. */
    seed?: number;
}
interface AdversarialSearchReport<S> {
    scenarios: AdversarialScenario<S>[];
    /** Discovered failures sorted by score ascending. */
    failures: AdversarialScenario<S>[];
    /** Round-by-round counts. */
    byGeneration: Array<{
        generation: number;
        total: number;
        failures: number;
        meanScore: number;
    }>;
    /** Total scoreFn invocations consumed. */
    scoreCalls: number;
}
declare function adversarialScenarioSearch<S>(opts: AdversarialSearchOptions<S>): Promise<AdversarialSearchReport<S>>;

/**
 * Process reward extraction — step-level credit assignment from trace spans.
 *
 * RL on long-horizon agents needs *step-level* rewards, not run-level
 * ones. The classic credit-assignment problem (Sutton & Barto) requires
 * knowing which sub-decisions in a trajectory contributed to the
 * outcome. Modern systems (DeepSeek-R1, OpenAI o-series, Lightman et al.
 * "Let's Verify Step by Step" 2023) train *process reward models* (PRMs)
 * that score every step, then do RL with the PRM as the reward signal.
 *
 * This module extracts `StepReward[]` from trace spans — one per
 * meaningful step — and ships:
 *
 *   1. `extractStepRewards(store, runId, opts)` — span → step-reward
 *      conversion using configurable per-span scorers (LLM judge over the
 *      span output, deterministic checkers, or a learned PRM).
 *   2. `runwiseStepRewardSummary(stepRewards)` — aggregate the per-step
 *      signal into a credit-assignment-aware run-level score.
 *   3. `prmTrainingPairs(stepRewards, options)` — produce the
 *      `(prefix, suffix_chosen, suffix_rejected)` triples that PRM
 *      training pipelines consume.
 *
 * What we ship: the *extraction* and *aggregation* infrastructure plus
 * the data shape PRM training expects. We do NOT ship the actual PRM
 * training (gradient descent over a transformer is out of scope for a
 * TS package). The interface is the contract; downstream consumers wire
 * their preferred trainer.
 *
 * Caveat the panel will land: this is descriptive credit assignment
 * (which steps correlate with outcome), not causal credit assignment
 * (which steps caused outcome). For causal claims you need
 * counterfactual rollouts or a learned dynamics model. Future work; the
 * descriptive version is what production PRM training actually uses.
 */

interface StepReward {
    /** Trace span this reward attaches to. */
    spanId: string;
    runId: string;
    /** Index in the trajectory (0-based, in started-at order). */
    stepIndex: number;
    /** Span kind (typically 'tool', 'llm', 'judge'). */
    kind: Span['kind'];
    /** Span name — for the consumer's downstream filtering. */
    name: string;
    /** Step-level reward in [0, 1]. */
    reward: number;
    /**
     * Determinism class. Mirrors the verifiable-reward distinction:
     * deterministic = test/compile/schema check; probabilistic = LLM judge.
     */
    determinism: 'deterministic' | 'probabilistic';
    /** Optional rationale / evidence — the trainer typically discards. */
    rationale?: string;
    /** Optional weight — how much this step contributes to credit assignment. */
    weight?: number;
}
interface StepScorer {
    /** Span kinds this scorer applies to. */
    appliesTo: Span['kind'][];
    /** Returns null to skip the span; returns a `StepReward` shape (without index/runId/spanId, which are filled in). */
    score(span: Span): Promise<Omit<StepReward, 'spanId' | 'runId' | 'stepIndex'>> | null | undefined;
}
interface ExtractStepRewardsOptions {
    /**
     * Ordered list of scorers. Each span runs through scorers in order;
     * the first non-null result wins. If no scorer applies, the span is
     * skipped (not all spans are training-worthy).
     */
    scorers: StepScorer[];
    /** Optional filter — return null to drop the span entirely before scoring. */
    preFilter?: (span: Span) => boolean;
}
declare function extractStepRewards(store: TraceStore, runId: string, opts: ExtractStepRewardsOptions): Promise<StepReward[]>;
interface RunwiseStepSummary {
    runId: string;
    totalSteps: number;
    meanReward: number;
    /** Sum-of-rewards (weighted by `weight ?? 1`). Use as the run-level proxy. */
    sumWeightedReward: number;
    /** Fraction of steps where reward < 0.5 — proxy for "where the policy was wrong." */
    failureFraction: number;
    /** Maximum drop in reward between consecutive steps — diagnoses a step where things went sideways. */
    worstStepDelta: number;
    worstStepIndex: number | null;
}
declare function runwiseStepRewardSummary(stepRewards: StepReward[]): RunwiseStepSummary;
interface PrmTrainingTriple {
    /** Prefix run-id (or composite key) — the trajectory up to step k-1. */
    prefixRunId: string;
    prefixStepIndex: number;
    /** The step that came next on a high-reward trajectory. */
    chosenSpanId: string;
    chosenReward: number;
    /** A step from a divergent low-reward trajectory at the same prefix length. */
    rejectedSpanId: string;
    rejectedReward: number;
    /** The prefix run came from this run; the rejected step came from `rejectedRunId`. */
    rejectedRunId: string;
    marginScore: number;
}
/**
 * Build PRM training triples. The shape: pair runs that share an early
 * prefix (same scenario, same first N steps) and diverge later — at the
 * point of divergence, the high-reward run's next step is `chosen`, the
 * low-reward run's next step is `rejected`. This is the canonical PRM
 * training data shape from Lightman et al. and DeepSeek-R1 process
 * supervision.
 *
 * Implementation note: we don't have a way to detect "same prefix" in
 * the general agent setting (token-level prefixes require hashing model
 * outputs). The current heuristic groups by `(scenarioId, prefixSpanName
 * sequence)` — runs are paired when their first K span names match. For
 * production use this should be replaced with a proper trajectory-prefix
 * hash; the heuristic is good enough for early-stage scaffolding.
 */
declare function prmTrainingPairs(stepRewardsByRun: Map<string, StepReward[]>, opts?: {
    minMargin?: number;
    minPrefixLength?: number;
}): PrmTrainingTriple[];

/**
 * Trainer-format exporters.
 *
 * agent-eval produces canonical artifacts (`RunRecord[]`, `PreferenceTriple[]`,
 * `StepReward[]`, `PrmTrainingTriple[]`). RL training pipelines consume
 * different shapes — Hugging Face TRL, Prime Intellect's prime-rl, OpenAI
 * fine-tuning, Anthropic finetuning, OpenRLHF, verl. Each has its own
 * JSONL conventions. Rather than ship N adapters, this module ships the
 * canonical formats most production pipelines accept and ergonomic helpers
 * for the rest.
 *
 * Shapes:
 *   - **DPO / IPO / KTO** — `{prompt, chosen, rejected}` JSONL. Consumed
 *     by HuggingFace TRL, prime-rl's offline DPO, OpenRLHF.
 *   - **GRPO offline** — `{prompt, completions[], rewards[]}` JSONL.
 *     Consumed by prime-rl GRPO, verl, OpenRLHF.
 *   - **SFT** — `{messages[]}` JSONL with chosen completion as the final
 *     assistant turn. Consumed by HF SFT trainers, OpenAI fine-tuning,
 *     Anthropic finetuning.
 *   - **PRM** — `{prompt, prefix_steps[], chosen_step, rejected_step}` JSONL.
 *     Consumed by Lightman-style PRM trainers and prime-rl's PRM mode.
 *
 * Why ship this in agent-eval rather than a separate adapter package: the
 * canonical artifacts (`RunRecord[]`, `PreferenceTriple[]`, etc.) are
 * agent-eval's contract; without first-party exporters consumers reverse-
 * engineer the mapping every release. The exporters codify it.
 *
 * The exporters take callbacks for any field that isn't on the canonical
 * artifact (specifically: prompt + completion text, since the package
 * stores only their hashes by design — full text is the consumer's
 * trace store / raw event log).
 */

interface DpoLookups {
    /** Resolve the prompt text for a run (typically from a trace store / raw event sink). */
    promptOf: (runId: string) => string | Promise<string>;
    /** Resolve the assistant completion text for a run. */
    completionOf: (runId: string) => string | Promise<string>;
}
interface DpoExportRow {
    prompt: string;
    chosen: string;
    rejected: string;
    /** Carried-through margin. Some KTO / IPO variants use this. */
    margin?: number;
    /** Free-form metadata for downstream filtering / sharding. */
    meta?: Record<string, unknown>;
}
/**
 * Convert preference triples to TRL-compatible DPO rows. The shape
 * `{prompt, chosen, rejected}` is the canonical HuggingFace DPODataset
 * entry; every major DPO trainer accepts it.
 */
declare function toDpoRows(triples: PreferenceTriple[], lookups: DpoLookups): Promise<DpoExportRow[]>;
/** Serialize DPO rows as JSONL. One line per row. */
declare function toDpoJsonl(rows: DpoExportRow[]): string;
interface GrpoLookups {
    promptOf: (runId: string) => string | Promise<string>;
    completionOf: (runId: string) => string | Promise<string>;
    /** Optional: derive a custom reward from the run. Defaults to score. */
    rewardOf?: (run: RunRecord) => number | null;
}
interface GrpoExportRow {
    prompt: string;
    completions: string[];
    rewards: number[];
    /** runIds in the same order as `completions[]` for traceability. */
    runIds: string[];
    meta?: Record<string, unknown>;
}
/**
 * Convert RunRecord[] grouped by `(scenarioId)` into GRPO offline rows —
 * one row per scenario, with one completion per run on that scenario.
 *
 * GRPO (Shao et al. 2024 / DeepSeek-R1) trains on relative advantages
 * within a group of completions for the same prompt; this is the
 * canonical input format.
 */
declare function toGrpoRows(runs: RunRecord[], lookups: GrpoLookups): Promise<GrpoExportRow[]>;
declare function toGrpoJsonl(rows: GrpoExportRow[]): string;
interface SftLookups {
    promptOf: (runId: string) => string | Promise<string>;
    completionOf: (runId: string) => string | Promise<string>;
    /** Optional system message. Default omits. */
    systemOf?: (run: RunRecord) => string | null | undefined;
    /** Filter — return false to skip the run (e.g., low score, failed cases). */
    include?: (run: RunRecord) => boolean;
}
interface SftExportRow {
    messages: Array<{
        role: 'system' | 'user' | 'assistant';
        content: string;
    }>;
    meta?: Record<string, unknown>;
}
/**
 * Convert RunRecord[] into Hugging Face / OpenAI / Anthropic-style
 * conversational SFT rows. By default every record becomes one row;
 * pass `include` to filter (e.g., keep only `score >= 0.8` for
 * rejection-sampling SFT).
 */
declare function toSftRows(runs: RunRecord[], lookups: SftLookups): Promise<SftExportRow[]>;
declare function toSftJsonl(rows: SftExportRow[]): string;
interface PrmLookups {
    /** Resolve the prompt text for a run. */
    promptOf: (runId: string) => string | Promise<string>;
    /** Resolve the trajectory step text for a (runId, spanId) pair. */
    stepTextOf: (runId: string, spanId: string) => string | Promise<string>;
    /** Optional: sequence of prefix span ids leading up to the divergence. */
    prefixOf?: (runId: string, prefixStepIndex: number) => string[] | Promise<string[]>;
}
interface PrmExportRow {
    prompt: string;
    /** Span ids for the steps before divergence — caller resolves text via `stepTextOf`. */
    prefixSpanIds: string[];
    prefixStepText: string[];
    chosenStep: string;
    rejectedStep: string;
    chosenReward: number;
    rejectedReward: number;
    marginScore: number;
    meta?: Record<string, unknown>;
}
/**
 * Convert PRM training triples to JSONL rows. Caller's `stepTextOf`
 * callback resolves span text from the consumer's trace store.
 */
declare function toPrmRows(triples: PrmTrainingTriple[], lookups: PrmLookups): Promise<PrmExportRow[]>;
declare function toPrmJsonl(rows: PrmExportRow[]): string;
interface StepRewardJsonlRow {
    runId: string;
    spanId: string;
    stepIndex: number;
    reward: number;
    determinism: 'deterministic' | 'probabilistic';
    weight: number;
}
declare function stepRewardsToJsonl(stepRewards: StepReward[]): string;

/**
 * `PredictiveValidityResearcher` — concrete `Researcher` implementation
 * that drives selection from outcome-anchored predictive validity.
 *
 * Each method:
 *
 *   - `inspectFailures(runs)` — synthesizes failure modes from the
 *     bottom-quartile of `RunRecord`s on the configured proxy reward.
 *   - `proposeChange(failures)` — proposes steering changes that target
 *     the rubrics with the lowest predictive validity (decorative ones).
 *     Either reduce their weight in the composite, or recalibrate them.
 *   - `applyChange(changes, baseline)` — merges the proposed steering
 *     into the experiment plan.
 *   - `evaluateChange(plan)` — re-runs the predictive-validity check on
 *     the post-change runs and reports the delta.
 *
 * The result is a closed loop: the rubric weights drift toward the ones
 * that actually predict deployment outcomes, automatically. Pair with
 * `runRLCampaign` for the full auto-research story.
 */

interface PredictiveValidityResearcherOptions {
    outcomes: OutcomeStore;
    outcomeMetrics: string[];
    /** Score threshold below which a run counts as a "failure." Default 0.5. */
    failureThreshold?: number;
    /** Spearman bucket below which a rubric is "decorative." Default 0.4. */
    decorativeThreshold?: number;
    /** Optional steering-namespace prefix for proposed changes. Default `'rubric_weight'`. */
    steeringNamespace?: string;
    /** Override the rubric set the researcher inspects. Default: every numeric `outcome.raw` key seen. */
    rubrics?: string[];
    /**
     * Snapshot stash hook — called with the most recent predictive-validity
     * report. Useful when a downstream system wants to log rubric drift over
     * time. Default no-op.
     */
    onReport?: (report: RubricPredictiveValidityReport) => void | Promise<void>;
}
/**
 * Concrete `Researcher` driven by `rubricPredictiveValidity`. The brain:
 * rubrics that don't predict deployment outcomes don't earn weight.
 */
declare class PredictiveValidityResearcher implements Researcher {
    private opts;
    private lastReport;
    constructor(opts: PredictiveValidityResearcherOptions);
    inspectFailures(runs: RunRecord[]): Promise<FailureMode[]>;
    proposeChange(failures: FailureMode[]): Promise<SteeringChange[]>;
    applyChange(changes: SteeringChange[], baseline: ExperimentPlan): Promise<ExperimentPlan>;
    evaluateChange(plan: ExperimentPlan): Promise<ExperimentResult>;
    /**
     * Run the predictive-validity check explicitly against a fresh RunRecord
     * set. Updates the researcher's cached report so subsequent
     * `proposeChange` calls have evidence to draw from.
     */
    runValidityCheck(runs: RunRecord[]): Promise<RubricPredictiveValidityReport>;
    /**
     * Force-feed a predictive-validity report into the researcher state —
     * useful when the consumer ran the report out-of-band and wants the
     * researcher's later proposals informed by it.
     */
    setReport(report: RubricPredictiveValidityReport): void;
    getLastReport(): RubricPredictiveValidityReport | null;
}

/**
 * Reward hacking / Goodhart detection.
 *
 * Goodhart's Law says: when a measure becomes a target, it ceases to be
 * a good measure. In RLHF and agentic-RL settings this is the dominant
 * failure mode — the policy learns to produce outputs that score well on
 * the proxy reward (judge, rubric, test pass-rate) without producing
 * the underlying capability the proxy was meant to track.
 *
 * Krakovna et al. (2020, "Specification Gaming Examples in AI") and the
 * subsequent RLHF reward-hacking literature (Skalse et al. 2022, Kim et al.
 * 2023) converge on a few diagnostic signatures:
 *
 *   1. **Reward divergence:** the proxy reward grows while the held-out
 *      ground-truth signal stagnates or drops. Predictive validity over
 *      time captures this.
 *   2. **Distributional shift in outputs:** after RL, the policy produces
 *      outputs that no longer match the reference distribution — usually
 *      because it found a high-reward attractor that's degenerate (e.g.
 *      one-token responses, repetition, formatting tricks).
 *   3. **Disagreement between independent rewards:** if you train on
 *      reward A and a held-out independent reward B drops sharply, you're
 *      probably hacking A.
 *   4. **Calibration drift:** the verifiable / deterministic component of
 *      the reward is stable; the probabilistic / judge component drifts up
 *      while the deterministic component doesn't. The judge is being
 *      gamed.
 *
 * This module ships explicit detectors for all four signatures, plus a
 * combined verdict. The output is diagnostic — actionable signals,
 * not autoreject — because each signature has known false positives
 * (e.g., a policy that genuinely improves can show distributional shift).
 *
 * Differs from `rubricPredictiveValidity` (which is a *standing* check on
 * whether rubrics correlate with deployment outcomes) — this is a
 * *temporal* check on whether the reward-vs-truth gap is *widening over
 * time during a training run*.
 */

type RewardHackingSignal = 'reward_divergence' | 'distribution_shift' | 'reward_disagreement' | 'judge_drift';
interface RewardHackingFinding {
    signal: RewardHackingSignal;
    /** Severity in [0, 1]. >0.5 = strong signal. */
    severity: number;
    message: string;
    /** Numeric evidence the consumer can render. */
    detail: Record<string, number>;
}
interface RewardHackingReport {
    findings: RewardHackingFinding[];
    /**
     * Composite verdict. `'clean'` if every signal severity < 0.3;
     * `'suspect'` if at least one ≥ 0.3 but none ≥ 0.6; `'gaming'` if any ≥ 0.6.
     */
    verdict: 'clean' | 'suspect' | 'gaming';
    /** Rationale for the verdict, ready to paste into an audit log. */
    rationale: string[];
    /** Number of paired (proxy, truth) data points the report saw. */
    n: number;
}
interface DetectRewardHackingInput {
    /**
     * Run records ordered by recency (oldest first). The detector segments
     * them into prefix/suffix windows to compute "did the gap widen."
     */
    runs: RunRecord[];
    /**
     * The metric the policy was trained to optimize. Should be present on
     * `outcome.raw` or `outcome.holdoutScore`. Default reads `outcome.holdoutScore`.
     */
    proxyOf?: (run: RunRecord) => number | null;
    /**
     * The held-out ground-truth metric. For RL on coding, this is typically
     * test pass-rate. For RLHF, it's downstream task performance or human
     * preference. For knowledge tasks, it's an independently-graded score.
     */
    truthOf?: (run: RunRecord) => number | null;
    /**
     * Independent secondary reward. Used for the `reward_disagreement`
     * signal. Default uses the verifiable reward extractor (deterministic
     * sources only).
     */
    secondaryRewardOf?: (run: RunRecord) => number | null;
    /**
     * Window size — how many of the most recent runs count as the "after"
     * cohort. Default min(50, half the runs).
     */
    windowSize?: number;
    /**
     * Severity threshold to flag a signal. Default 0.3 (suspect) and 0.6
     * (gaming).
     */
    thresholds?: {
        suspect?: number;
        gaming?: number;
    };
    /**
     * Verifiable-reward options used for the secondary-reward fallback.
     */
    verifiableRewardOptions?: VerifiableRewardExtractionOptions;
}
declare function detectRewardHacking(input: DetectRewardHackingInput): RewardHackingReport;

/**
 * `runRLCampaign` — top-level orchestrator that runs the matrix and
 * produces every RL-ready artifact in one call.
 *
 * Wires:
 *   1. `runEvalCampaign` for the matrix run (capture, integrity, hooks)
 *   2. `extractVerifiableReward` over each run, separating deterministic
 *      from probabilistic reward sources for the trainer
 *   3. `extractPreferences` to produce DPO/PPO/KTO triples
 *   4. `evaluateInterimReleaseConfidence` over paired deltas (anytime-valid)
 *   5. `rubricPredictiveValidity` against an outcome store, when provided
 *   6. `detectRewardHacking` as a standing hygiene check
 *   7. Trainer-format export rows ready for prime-rl / TRL / verl
 *
 * The output `RLCampaignResult` is a single, audit-ready artifact: every
 * stage's output is in there. The consumer's downstream fits in a single
 * line: pass `result.preferences` to their DPO trainer, `result.grpoRows`
 * to GRPO, `result.runs` plus `result.rewardSignals` to a custom RL loop.
 */

interface RunRLCampaignOptions<V> extends EvalCampaignOptions<V> {
    /** Preference-extraction options. Default uses paired-by-scenario-and-seed with min-margin 0.05. */
    preferences?: ExtractPreferencesOptions;
    /** Verifiable-reward extraction options. */
    verifiableReward?: VerifiableRewardExtractionOptions;
    /** Outcome store + metric names — when supplied, runs `rubricPredictiveValidity` post-campaign. */
    outcomeStore?: OutcomeStore;
    outcomeMetrics?: string[];
    /** Anytime-valid sequential evaluation options. */
    sequential?: {
        alpha?: number;
        bound?: number;
        rope?: {
            low: number;
            high: number;
        };
    };
    /** Trainer-format export lookups. When provided, the orchestrator builds the corresponding rows. */
    trainerExport?: {
        dpo?: DpoLookups;
        grpo?: GrpoLookups;
        sft?: SftLookups;
    };
}
interface RLCampaignResult<V> {
    campaign: EvalCampaignResult;
    /** Per-run verifiable reward (deterministic when available, probabilistic fallback otherwise). */
    rewardSignals: Array<{
        runId: string;
        reward: VerifiableReward | null;
    }>;
    /** Preference extraction report. */
    preferences: PreferenceExtractionReport;
    /** Anytime-valid interim verdict over the paired deltas (vs comparator). */
    interimConfidence: InterimReleaseConfidence | null;
    /** Standing reward-hacking hygiene check. */
    rewardHacking: RewardHackingReport;
    /** Predictive validity, when an outcome store was supplied. */
    predictiveValidity: RubricPredictiveValidityReport | null;
    /** Trainer-export rows, populated only for the formats the caller requested via `trainerExport`. */
    trainerRows: {
        dpo?: DpoExportRow[];
        grpo?: GrpoExportRow[];
        sft?: SftExportRow[];
    };
    /**
     * One-line top-level summary the consumer can log.
     */
    summary: string;
    /**
     * Convenience type-tag — consumers can branch on `result.kind`.
     */
    kind: 'agent-eval-rl-campaign';
    unusedVariant?: V;
}
declare function runRLCampaign<V>(opts: RunRLCampaignOptions<V>): Promise<RLCampaignResult<V>>;

export { type AdaptationCurve, type AdaptationPoint, type AdaptationRunner, type AdapterContext, type AdversarialMutation, type AdversarialScenario, type AdversarialSearchOptions, type AdversarialSearchReport, type BradleyTerryFit, type BradleyTerryRating, type BuildPairwiseFromCampaignInput, type CellObservation, type CompareCurvesResult, type ComputeBestOfNOptions, type ComputeBestOfNResult, type ComputeCurve, type ComputeCurveBudget, type ComputeCurvePoint, type ContaminationProbeInput, type ContaminationProbeOptions, type ContaminationProbeReport, type CurriculumAllocation, type DetectRewardHackingInput, type DpoExportRow, type DpoLookups, type EloOptions, type ExtractPreferencesOptions, type ExtractStepRewardsOptions, type GrpoExportRow, type GrpoLookups, type OffPolicyEstimate, type OffPolicyOptions, type OffPolicyTrajectory, OutcomeStore, type PairwiseOutcome, type ParetoPointInput, PredictiveValidityResearcher, type PredictiveValidityResearcherOptions, type PreferenceExtractionReport, type PreferenceStrategy, type PreferenceTriple, type PrmExportRow, type PrmLookups, type PrmTrainingTriple, type RLCampaignResult, type RewardHackingFinding, type RewardHackingReport, type RewardHackingSignal, type RunAdaptationCurveOptions, type RunComputeCurveOptions, type RunRLCampaignOptions, type RunwiseStepSummary, type ScenarioPerturbation, type ScenarioPerturbationKind, type SelfConsistencyOptions, type SelfConsistencyResult, type SftExportRow, type SftLookups, type StepReward, type StepRewardJsonlRow, type StepScorer, type ThompsonCurriculumOptions, type VarianceCurriculumOptions, type VerifiableReward, type VerifiableRewardExtractionOptions, type VerifiableRewardSource, adversarialScenarioSearch, applyEloUpdate, bestOfN, buildPairwiseFromCampaign, campaignToRunRecords, compareAdaptationCurves, detectRewardHacking, doublyRobust, extractPreferences, extractStepRewards, extractVerifiableReward, extractVerifiableRewardsFromRecords, filterDeterministicallyRewarded, firstPassK, fitBradleyTerry, injectIrrelevantClause, inverseProbabilityWeighting, observationsFromRunRecords, offPolicyEstimateAll, paretoFrontier, prmTrainingPairs, renameVariables, runAdaptationCurve, runComputeCurve, runContaminationProbe, runRLCampaign, runwiseStepRewardSummary, selfConsistency, selfNormalizedImportanceWeighting, shuffleOrder, stepRewardsToJsonl, thompsonCurriculum, toAnthropicFormat, toDpoJsonl, toDpoRows, toGrpoJsonl, toGrpoRows, toPrmJsonl, toPrmRows, toSftJsonl, toSftRows, toTRLFormat, varianceBasedCurriculum, verificationReportToRunRecord };
