@tangle-network/agent-eval 0.147.0 → 0.148.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +7 -0
- package/dist/analyst/index.d.ts +1 -1
- package/dist/analyst/index.js +1 -1
- package/dist/{benchmark-command-BTiysiYU.js → benchmark-command-Cz4fDmsk.js} +2 -2
- package/dist/{benchmark-command-BTiysiYU.js.map → benchmark-command-Cz4fDmsk.js.map} +1 -1
- package/dist/benchmarks/index.d.ts +2 -2
- package/dist/benchmarks/index.js +2 -2
- package/dist/campaign/index.d.ts +4 -4
- package/dist/campaign/index.js +4 -4
- package/dist/{campaign-DCDdhuv2.js → campaign-BcfXzmPM.js} +4 -4
- package/dist/{campaign-DCDdhuv2.js.map → campaign-BcfXzmPM.js.map} +1 -1
- package/dist/cli.js +1 -1
- package/dist/contract/index.d.ts +3 -3
- package/dist/contract/index.js +3 -3
- package/dist/{define-agent-eval-BqWFz3sK.js → define-agent-eval-CvZQW4u9.js} +2 -2
- package/dist/{define-agent-eval-BqWFz3sK.js.map → define-agent-eval-CvZQW4u9.js.map} +1 -1
- package/dist/{define-agent-eval-D52ClbX2.d.ts → define-agent-eval-hXLUBKtb.d.ts} +2 -2
- package/dist/{define-agent-eval-D52ClbX2.d.ts.map → define-agent-eval-hXLUBKtb.d.ts.map} +1 -1
- package/dist/experiment/index.d.ts +117 -1
- package/dist/experiment/index.d.ts.map +1 -1
- package/dist/experiment/index.js +217 -1
- package/dist/experiment/index.js.map +1 -1
- package/dist/{index-BjBjxiVv.d.ts → index-Dzn8Q3C2.d.ts} +12 -358
- package/dist/index-Dzn8Q3C2.d.ts.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/{llm-judge-DrzsVS5k.js → llm-judge-DWq1Ptco.js} +259 -5
- package/dist/llm-judge-DWq1Ptco.js.map +1 -0
- package/dist/{matrix-C-2Qx1Zr.d.ts → matrix-Bc111FKv.d.ts} +17 -1
- package/dist/{matrix-C-2Qx1Zr.d.ts.map → matrix-Bc111FKv.d.ts.map} +1 -1
- package/dist/multishot/golden/index.d.ts +1 -1
- package/dist/multishot/index.d.ts +1 -1
- package/dist/multishot/index.js +16 -0
- package/dist/multishot/index.js.map +1 -1
- package/dist/openapi.json +1 -1
- package/dist/{produced-state-Dtx60bUQ.js → produced-state-C0oJ4vr-.js} +2 -2
- package/dist/{produced-state-Dtx60bUQ.js.map → produced-state-C0oJ4vr-.js.map} +1 -1
- package/dist/{provenance-LrOEOHQb.d.ts → provenance-DA-Pmyfv.d.ts} +441 -2
- package/dist/provenance-DA-Pmyfv.d.ts.map +1 -0
- package/dist/{skillopt-optimization-method-BoC1Qccx.d.ts → skillopt-optimization-method-CrY0OG17.d.ts} +2 -2
- package/dist/{skillopt-optimization-method-BoC1Qccx.d.ts.map → skillopt-optimization-method-CrY0OG17.d.ts.map} +1 -1
- package/dist/{skillopt-optimization-method-C_UrqZs2.js → skillopt-optimization-method-Donmq4sq.js} +2 -2
- package/dist/{skillopt-optimization-method-C_UrqZs2.js.map → skillopt-optimization-method-Donmq4sq.js.map} +1 -1
- package/docs/multishot-golden-records.md +12 -1
- package/docs/search-history-receipts.md +139 -0
- package/package.json +2 -2
- package/dist/index-BjBjxiVv.d.ts.map +0 -1
- package/dist/llm-judge-DrzsVS5k.js.map +0 -1
- package/dist/provenance-LrOEOHQb.d.ts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"define-agent-eval-BqWFz3sK.js","names":[],"sources":["../src/contamination-guard.ts","../src/contract/analyze-runs.ts","../src/contract/self-improve.ts","../src/contract/define-agent-eval.ts"],"sourcesContent":["/**\n * ContaminationGuard — ensures held-out scenarios don't leak into\n * training/prompt paths, and flags model memorization.\n *\n * Three probes:\n * 1. `checkCanaries(output, scenario)` — if the scenario carries a\n * canary token, it MUST NOT appear in the agent's output.\n * Canaries are strings that are statistically impossible to\n * reconstruct from the scenario description alone — so if they\n * echo back, the model memorized them.\n * 2. `canaryLeakView(store)` — cross-corpus view of every run whose\n * output contained a canary, with the offending scenario + run.\n * 3. `HoldoutAuditor` — wraps a Dataset and emits a structured error\n * on any code path that reads holdout scenarios but doesn't flag\n * `purpose: 'evaluation'`. Keeps engineers honest.\n */\n\nimport type { DatasetScenario } from './dataset'\nimport { llmSpans } from './trace/query'\nimport type { TraceStore } from './trace/store'\n\nexport interface CanaryLeak {\n scenarioId: string\n canary: string\n runId?: string\n evidence: string\n}\n\nexport function checkCanaries(output: string, scenarios: DatasetScenario[]): CanaryLeak[] {\n const leaks: CanaryLeak[] = []\n for (const s of scenarios) {\n if (!s.canary) continue\n if (output.includes(s.canary)) {\n leaks.push({ scenarioId: s.id, canary: s.canary, evidence: excerpt(output, s.canary) })\n }\n }\n return leaks\n}\n\n/**\n * Behavioral canary: tests the AGENT, not the eval grep.\n *\n * - `forbiddenPattern` PRESENT in output ⇒ AGENT EMITTED BAD CONTENT ⇒ LEAK.\n * - `forbiddenPattern` ABSENT from output ⇒ AGENT HELD THE LINE ⇒ PASS.\n *\n * Use when running known-bad-prompt scenarios against the agent under\n * test and you want to know if the agent misbehaved. The classical\n * {@link checkCanaries} / {@link import('./canary').runCanaries | runCanaries}\n * test whether the eval check fires when the bad output is forced\n * into the eval flow — different question, different answer.\n *\n * Pattern resolution order (first match wins):\n * 1. `scenario.forbiddenPattern` — if it parses as `/body/flags`,\n * treated as a regex; otherwise a literal substring.\n * 2. `scenario.canary` — literal substring fallback so the helper\n * works on existing scenario fixtures.\n *\n * Returns `null` when nothing forbidden was found OR the scenario\n * declared no pattern.\n */\nexport function checkBehavioralCanary(\n output: string,\n scenario: DatasetScenario,\n): CanaryLeak | null {\n const pattern = scenario.forbiddenPattern ?? scenario.canary\n if (!pattern) return null\n const hit = matchForbidden(output, pattern)\n if (!hit) return null\n return {\n scenarioId: scenario.id,\n canary: pattern,\n evidence: excerpt(output, hit),\n }\n}\n\n/**\n * Behavioral canary over many (scenario, output) pairs. Sibling to\n * {@link import('./canary').runCanaries | runCanaries} — same idea\n * (run-many → report) but the question being answered is \"did the\n * AGENT misbehave?\" rather than \"did the EVAL grep fire?\".\n *\n * Returns one `CanaryLeak` per pair where the agent's output\n * contained its scenario's `forbiddenPattern` (or `canary` fallback).\n */\nexport function runBehavioralCanaries(\n cases: Array<{ scenario: DatasetScenario; output: string; runId?: string }>,\n): CanaryLeak[] {\n const leaks: CanaryLeak[] = []\n for (const c of cases) {\n const leak = checkBehavioralCanary(c.output, c.scenario)\n if (leak) leaks.push({ ...leak, runId: c.runId ?? leak.runId })\n }\n return leaks\n}\n\n/**\n * Resolve a forbidden-pattern string to the matched substring inside\n * `output`. `/body/flags` notation is interpreted as a regex; anything\n * else is a literal substring.\n */\nfunction matchForbidden(output: string, pattern: string): string | null {\n const re = tryParseRegex(pattern)\n if (re) {\n const m = output.match(re)\n return m && m[0].length > 0 ? m[0] : null\n }\n return output.includes(pattern) ? pattern : null\n}\n\nfunction tryParseRegex(pattern: string): RegExp | null {\n if (pattern.length < 2 || pattern[0] !== '/') return null\n const last = pattern.lastIndexOf('/')\n if (last <= 0) return null\n const body = pattern.slice(1, last)\n const flags = pattern.slice(last + 1)\n if (!/^[gimsuy]*$/.test(flags)) return null\n try {\n return new RegExp(body, flags)\n } catch {\n return null\n }\n}\n\n/**\n * Scan the LLM-output history in a corpus; returns every case where a\n * canary from a known scenario appeared in agent output. Pass the full\n * set of scenarios whose canaries you care about (typically the whole\n * held-out slice).\n */\nexport async function canaryLeakView(\n store: TraceStore,\n scenarios: DatasetScenario[],\n): Promise<CanaryLeak[]> {\n const targets = scenarios.filter((s) => !!s.canary)\n if (targets.length === 0) return []\n const spans = await llmSpans(store)\n const leaks: CanaryLeak[] = []\n for (const span of spans) {\n const output = span.output ?? ''\n for (const s of targets) {\n if (s.canary && output.includes(s.canary)) {\n leaks.push({\n scenarioId: s.id,\n canary: s.canary,\n runId: span.runId,\n evidence: excerpt(output, s.canary),\n })\n }\n }\n }\n return leaks\n}\n\nexport class HoldoutAuditor {\n private scenarios: DatasetScenario[]\n private accessLog: Array<{ scenarioId: string; purpose: string; at: number }> = []\n\n constructor(scenarios: DatasetScenario[]) {\n this.scenarios = scenarios\n }\n\n /** Retrieve a holdout scenario for a declared purpose. Non-'evaluation' throws. */\n get(scenarioId: string, purpose: 'evaluation' | 'debugging'): DatasetScenario {\n if (purpose !== 'evaluation' && purpose !== 'debugging') {\n throw new Error(\n `HoldoutAuditor.get: purpose must be 'evaluation' or 'debugging', got ${purpose}`,\n )\n }\n const s = this.scenarios.find((x) => x.id === scenarioId)\n if (!s) throw new Error(`holdout scenario \"${scenarioId}\" not found`)\n this.accessLog.push({ scenarioId, purpose, at: Date.now() })\n return s\n }\n\n getAccessLog(): ReadonlyArray<{ scenarioId: string; purpose: string; at: number }> {\n return this.accessLog\n }\n}\n\nfunction excerpt(source: string, needle: string): string {\n const at = source.indexOf(needle)\n if (at < 0) return ''\n const start = Math.max(0, at - 30)\n const end = Math.min(source.length, at + needle.length + 30)\n return (start > 0 ? '…' : '') + source.slice(start, end) + (end < source.length ? '…' : '')\n}\n","/**\n * # `analyzeRuns()` — turn a set of agent runs into an actionable decision packet.\n *\n * Wires the substrate's statistical, calibration, clustering, Pareto, and\n * release-confidence primitives into one `InsightReport`. Two top-level\n * entry points use this function:\n *\n * - `selfImprove()` calls it on the campaign output to attach a packet\n * to every run.\n * - Consumers with observed `RunRecord[]` (production traces, gold\n * corpora, approve/reject tables) call it directly via `analyzeRuns()`\n * for analysis without a closed loop.\n *\n * Every section is opt-in based on what the input data supports — the\n * function never invents signal. If runs carry no judge scores, `judges`\n * is empty. If there's no baseline/candidate split, `lift` is undefined.\n * If no `analyst` is wired, `failureClusters` is undefined.\n *\n * The `recommendations` array is the human-readable layer; everything\n * else is the evidence backing each recommendation.\n */\n\nimport type { AnalystRegistry } from '../analyst/registry'\nimport type { AnalystFinding } from '../analyst/types'\nimport { welchsTTest } from '../baseline'\nimport { checkCanaries } from '../contamination-guard'\nimport type { DatasetScenario } from '../dataset'\nimport { continuousAgreement } from '../judge-calibration'\nimport { pairRunRecords } from '../paired-arms'\nimport { observedSplitScore } from '../rollout/reward'\nimport {\n type RunRecord,\n type RunTerminalOutcome,\n type RunTokenUsage,\n validateRunRecord,\n} from '../run-record'\nimport {\n BOOTSTRAP_GATE_MIN_N,\n pairedBootstrap,\n pairedCohensDz,\n pairedMde,\n pairedTTest,\n pearsonR,\n requiredPairedSampleSize,\n spearmanR,\n} from '../statistics'\nimport { type ParetoFigureSpec, paretoChart } from '../summary-report'\nimport type { FailureClass } from '../trace/schema'\n\nimport type {\n CostProvenanceSummary,\n ExecutionInsight,\n FailureClassTally,\n FailureClusterInsight,\n InsightReport,\n InterRaterInsight,\n JudgeInsight,\n LiftInsight,\n MetricDelta,\n OutcomeCorrelationInsight,\n PriorPeriodComparison,\n Recommendation,\n ScalarDistribution,\n TokenUsageInsight,\n} from './insight-report'\n\n// ── Public API ───────────────────────────────────────────────────────\n\nexport interface AnalyzeRunsOptions {\n /** The runs to analyze. */\n runs: RunRecord[]\n /** Which split to score against when reading composite from RunOutcome.\n * Default: holdout when ANY run has a `holdoutScore`, else search. */\n split?: 'search' | 'holdout' | 'auto'\n /** Pairwise analysis configuration. When both `baselineCandidateId` and\n * `candidateCandidateId` are present, lift is computed on paired\n * (experimentId, scenarioId, seed) identities shared between the two sides.\n * Unmatched rows remain visible in the lift result. */\n baselineCandidateId?: string\n candidateCandidateId?: string\n /** Canary scenarios — checked against every run's raw output for\n * holdout contamination. */\n canaryScenarios?: DatasetScenario[]\n /** Analyst registry for failure clustering. When omitted, the\n * `failureClusters` section is left undefined. */\n analyst?: AnalystRegistry\n /** Downstream outcome metric per run (e.g. engagement rate, approval\n * rate, downstream pass rate). When present, the report includes\n * `outcomeCorrelation` + a simple linear reward model fit. */\n outcomeSignal?: {\n metric: string\n valueByRunId: Record<string, number>\n }\n /** Multi-rater feedback for inter-rater agreement. Each entry is one\n * rater's score for one run. Two or more raters → kappa + disagreement\n * triage list. */\n raterScores?: Array<{ runId: string; rater: string; score: number }>\n /** Number of histogram bins for distributional summaries. Default 12. */\n histogramBins?: number\n /** Decision threshold — the smallest composite lift the caller cares\n * about. Used by the recommendations engine to call ship vs hold.\n * Default 0.02. */\n decisionThreshold?: number\n /** Optional prior-period runs. When set, the report includes\n * `priorPeriodComparison` with per-metric Welch-CI deltas and\n * recommendations fire on statistically significant regressions.\n * The two windows do NOT have to share scenarios — the comparison\n * is two-sample unpaired (the substrate's `lift` field uses paired\n * bootstrap on shared (experimentId, scenarioId, seed) identities; this is the\n * shape for \"this week vs last week\" rather than \"candidate vs\n * baseline within a campaign\"). */\n baselineRuns?: RunRecord[]\n /** Human-readable label for the baseline window, e.g. \"vs prior 7\n * days\", \"vs v3.1 release\". Surfaces in recommendations + UI. */\n baselineLabel?: string\n}\n\nexport interface SummarizeExecutionOptions {\n runs: RunRecord[]\n histogramBins?: number\n}\n\nexport interface ExecutionReport {\n execution: ExecutionInsight\n costProvenance: CostProvenanceSummary\n}\n\n/** Summarize runtime facts without interpreting task quality or promotion readiness. */\nexport function summarizeExecution(opts: SummarizeExecutionOptions): ExecutionReport {\n const runs = opts.runs.map(validateRunRecord)\n const bins = opts.histogramBins ?? 12\n return {\n execution: computeExecutionInsight(runs, bins),\n costProvenance: summarizeCostProvenance(runs),\n }\n}\n\n/** A bootstrap interval with no spread: every resample landed on the same\n * value, so the interval carries no information about how far the point\n * estimate could be wrong and cannot support a directional claim. */\nfunction zeroWidth(ci: readonly [number, number]): boolean {\n return !Number.isFinite(ci[0]) || !Number.isFinite(ci[1]) || ci[0] === ci[1]\n}\n\nexport async function analyzeRuns(opts: AnalyzeRunsOptions): Promise<InsightReport> {\n const runs = opts.runs.map(validateRunRecord)\n const bins = opts.histogramBins ?? 12\n const threshold = opts.decisionThreshold ?? 0.02\n if (!Number.isFinite(threshold)) {\n throw new Error(`analyzeRuns: decisionThreshold must be finite, got ${threshold}`)\n }\n const split = resolveSplit(runs, opts.split ?? 'auto')\n\n const compositeWithIds = runs\n .map((r) => ({ runId: r.runId, score: compositeOf(r, split) }))\n .filter((p) => Number.isFinite(p.score))\n const composite = distributionOf(\n compositeWithIds.map((p) => p.score),\n bins,\n compositeWithIds,\n )\n\n const perDimension = computePerDimension(runs, bins)\n const { execution, costProvenance: provenance } = summarizeExecution({\n runs,\n histogramBins: bins,\n })\n const knownCostRuns = runs.filter((run) => run.costProvenance.kind !== 'uncaptured')\n const costs = knownCostRuns.map((r) => r.costUsd).filter(isFiniteNumber)\n const costDist = distributionOf(costs, bins)\n const pareto = paretoChart(knownCostRuns, { split })\n const degraded: { cost?: string; pareto?: string } = {}\n if (provenance.uncaptured.n > 0) {\n degraded.cost = diagnoseCostCoverage(runs, provenance)\n } else if (costs.length === 0 || costs.every((c) => c === 0)) {\n degraded.cost = `all ${runs.length} explicitly observed or estimated USD values are $0`\n }\n if (pareto.points.length < 2) {\n degraded.pareto =\n pareto.points.length === 0\n ? 'no candidates — Pareto unavailable'\n : 'single candidate — Pareto is a single point, not a frontier'\n }\n const costQuality = {\n cost: costDist,\n pareto,\n provenance,\n ...(degraded.cost || degraded.pareto ? { degraded } : {}),\n }\n\n const judges = computeJudgeInsights(runs)\n\n const interRater = opts.raterScores ? computeInterRater(opts.raterScores) : undefined\n\n const lift = computeLift(runs, opts.baselineCandidateId, opts.candidateCandidateId, split)\n\n const failureClusters = opts.analyst\n ? await computeFailureClusters(runs, opts.analyst, split)\n : undefined\n\n const failureClasses = computeFailureClasses(runs, split)\n\n const contamination = opts.canaryScenarios\n ? computeContamination(runs, opts.canaryScenarios)\n : undefined\n\n const outcomeCorrelation = opts.outcomeSignal\n ? computeOutcomeCorrelation(runs, opts.outcomeSignal, split)\n : undefined\n\n const release = buildReleaseScorecard(composite, lift, contamination)\n\n const priorPeriodComparison = opts.baselineRuns\n ? computePriorPeriodComparison(runs, opts.baselineRuns, split, opts.baselineLabel)\n : undefined\n\n const recommendations = buildRecommendations({\n composite,\n judges,\n interRater,\n lift,\n failureClusters,\n failureClasses,\n contamination,\n outcomeCorrelation,\n priorPeriodComparison,\n threshold,\n })\n\n return {\n n: runs.length,\n execution,\n composite,\n perDimension,\n costQuality,\n judges,\n interRater,\n lift,\n failureClusters,\n contamination,\n outcomeCorrelation,\n release,\n ...(failureClasses ? { failureClasses } : {}),\n ...(priorPeriodComparison ? { priorPeriodComparison } : {}),\n recommendations,\n }\n}\n\nfunction computeExecutionInsight(runs: RunRecord[], bins: number): ExecutionInsight {\n const aggregateRows = runs.flatMap((run) => {\n const usage = aggregateTokenUsage(run)\n return usage ? [{ usage, costUsd: finiteRaw(run, 'aggregate_cost_usd') }] : []\n })\n const aggregateCosts = aggregateRows.flatMap((row) =>\n row.costUsd !== undefined ? [row.costUsd] : [],\n )\n const modelCounts = new Map<string, number>()\n let executionErrorRuns = 0\n let executionErrorEvents = 0\n let errorReportingRuns = 0\n let errorSpanEvents = 0\n let errorSpanReportingRuns = 0\n const terminalOutcomes: Record<RunTerminalOutcome, number> = {\n succeeded: 0,\n failed: 0,\n cancelled: 0,\n incomplete: 0,\n unknown: 0,\n }\n const errorsByTerminalOutcome: ExecutionInsight['executionErrors']['byTerminalOutcome'] = {\n succeeded: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n failed: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n cancelled: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n incomplete: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n unknown: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n }\n let modelCallRuns = 0\n let modelCallEvents = 0\n let modelCallReportingRuns = 0\n\n for (const run of runs) {\n modelCounts.set(run.model, (modelCounts.get(run.model) ?? 0) + 1)\n const terminalOutcome = run.terminalOutcome\n terminalOutcomes[terminalOutcome] += 1\n const modelCalls = nonNegativeCountRaw(run, 'llm_span_count')\n if (modelCalls !== undefined) {\n modelCallEvents += modelCalls\n modelCallReportingRuns += 1\n }\n const usage = run.tokenUsage\n if (\n (modelCalls ?? 0) > 0 ||\n usage.input > 0 ||\n usage.output > 0 ||\n (usage.cached ?? 0) > 0 ||\n (usage.cacheWrite ?? 0) > 0\n ) {\n modelCallRuns += 1\n }\n const errorEvents = reportedExecutionErrorEvents(run)\n if (errorEvents !== undefined) {\n executionErrorEvents += errorEvents\n errorReportingRuns += 1\n if (errorEvents > 0) {\n executionErrorRuns += 1\n errorsByTerminalOutcome[terminalOutcome].withErrors += 1\n } else errorsByTerminalOutcome[terminalOutcome].withoutErrors += 1\n } else errorsByTerminalOutcome[terminalOutcome].unreported += 1\n const reportedErrorSpans = nonNegativeCountRaw(run, 'error_span_count')\n if (reportedErrorSpans !== undefined) {\n errorSpanEvents += reportedErrorSpans\n errorSpanReportingRuns += 1\n }\n }\n\n return {\n durationMs: distributionOf(\n runs.map((run) => run.wallMs),\n bins,\n ),\n queueMs: distributionOf(\n runs.filter((run) => run.queueMs !== undefined).map((run) => run.queueMs!),\n bins,\n ),\n tokenUsage: summarizeTokenUsage(\n runs.map((run) => run.tokenUsage),\n bins,\n ),\n aggregateUsage: {\n runs: aggregateRows.length,\n tokenUsage: summarizeTokenUsage(\n aggregateRows.map((row) => row.usage),\n bins,\n ),\n costUsd: distributionOf(aggregateCosts, bins),\n totalCostUsd: aggregateCosts.reduce((total, value) => total + value, 0),\n },\n models: [...modelCounts.entries()]\n .map(([model, count]) => ({ model, runs: count }))\n .sort((left, right) => right.runs - left.runs || left.model.localeCompare(right.model)),\n modelCalls: {\n runs: modelCallRuns,\n events: modelCallEvents,\n reportingRuns: modelCallReportingRuns,\n },\n executionErrors: {\n runs: executionErrorRuns,\n fraction: errorReportingRuns > 0 ? executionErrorRuns / errorReportingRuns : null,\n events: executionErrorEvents,\n reportingRuns: errorReportingRuns,\n errorSpanEvents,\n errorSpanReportingRuns,\n byTerminalOutcome: errorsByTerminalOutcome,\n },\n terminalOutcomes,\n }\n}\n\nfunction reportedExecutionErrorEvents(run: RunRecord): number | undefined {\n return nonNegativeCountRaw(run, 'execution_error_count')\n}\n\nfunction nonNegativeCountRaw(run: RunRecord, key: string): number | undefined {\n const value = finiteRaw(run, key)\n return value !== undefined && Number.isInteger(value) && value >= 0 ? value : undefined\n}\n\nfunction summarizeTokenUsage(usages: RunTokenUsage[], bins: number): TokenUsageInsight {\n const reasoning = usages.flatMap((usage) =>\n usage.reasoning !== undefined ? [usage.reasoning] : [],\n )\n const cached = usages.flatMap((usage) => (usage.cached !== undefined ? [usage.cached] : []))\n const cacheWrite = usages.flatMap((usage) =>\n usage.cacheWrite !== undefined ? [usage.cacheWrite] : [],\n )\n return {\n input: distributionOf(\n usages.map((usage) => usage.input),\n bins,\n ),\n output: distributionOf(\n usages.map((usage) => usage.output),\n bins,\n ),\n reasoning: distributionOf(reasoning, bins),\n cached: distributionOf(cached, bins),\n cacheWrite: distributionOf(cacheWrite, bins),\n totals: {\n input: usages.reduce((total, usage) => total + usage.input, 0),\n output: usages.reduce((total, usage) => total + usage.output, 0),\n reasoning: reasoning.reduce((total, value) => total + value, 0),\n cached: cached.reduce((total, value) => total + value, 0),\n cacheWrite: cacheWrite.reduce((total, value) => total + value, 0),\n },\n }\n}\n\nfunction aggregateTokenUsage(run: RunRecord): RunTokenUsage | undefined {\n const input = finiteRaw(run, 'aggregate_prompt_tokens')\n const output = finiteRaw(run, 'aggregate_completion_tokens')\n const reasoning = finiteRaw(run, 'aggregate_reasoning_tokens')\n const cached = finiteRaw(run, 'aggregate_cached_tokens')\n const cacheWrite = finiteRaw(run, 'aggregate_cache_write_tokens')\n if (\n input === undefined &&\n output === undefined &&\n reasoning === undefined &&\n cached === undefined &&\n cacheWrite === undefined\n )\n return undefined\n return {\n input: input ?? 0,\n output: output ?? 0,\n ...(reasoning !== undefined ? { reasoning } : {}),\n ...(cached !== undefined ? { cached } : {}),\n ...(cacheWrite !== undefined ? { cacheWrite } : {}),\n }\n}\n\nfunction finiteRaw(run: RunRecord, key: string): number | undefined {\n const value = run.outcome.raw[key]\n return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : undefined\n}\n\nfunction summarizeCostProvenance(runs: RunRecord[]): CostProvenanceSummary {\n const summary: CostProvenanceSummary = {\n observed: { n: 0, totalUsd: 0 },\n estimated: { n: 0, totalUsd: 0 },\n uncaptured: { n: 0 },\n knownFraction: 0,\n }\n for (const run of runs) {\n const cost = run.costProvenance\n if (cost.kind === 'uncaptured') {\n summary.uncaptured.n += 1\n } else {\n summary[cost.kind].n += 1\n summary[cost.kind].totalUsd += cost.usd\n }\n }\n const known = summary.observed.n + summary.estimated.n\n summary.knownFraction = runs.length > 0 ? known / runs.length : 0\n return summary\n}\n\nfunction diagnoseCostCoverage(runs: RunRecord[], provenance: CostProvenanceSummary): string {\n const uncaptured = provenance.uncaptured.n\n const known = provenance.observed.n + provenance.estimated.n\n if (uncaptured === runs.length) {\n return `USD cost uncaptured for all ${runs.length} runs — no observed or estimated USD values; token and wall-time metrics remain available.`\n }\n return `USD cost uncaptured for ${uncaptured}/${runs.length} runs; excluded those rows from cost statistics (${known}/${runs.length} retained: ${provenance.observed.n} observed, ${provenance.estimated.n} estimated).`\n}\n\n/**\n * Model-free task-failure tally.\n *\n * Explicit non-success classes are task-failure evidence.\n * A low task score without a class is counted as `unknown`.\n */\nfunction computeFailureClasses(\n runs: RunRecord[],\n split: 'search' | 'holdout',\n): FailureClassTally[] | undefined {\n const counts = new Map<FailureClass, number>()\n for (const r of runs) {\n if (!isTaskFailure(r, split)) continue\n const key =\n r.failureClass !== undefined && r.failureClass !== 'success' ? r.failureClass : 'unknown'\n counts.set(key, (counts.get(key) ?? 0) + 1)\n }\n if (counts.size === 0) return undefined\n const n = runs.length\n return [...counts.entries()]\n .map(([failureClass, count]) => ({\n failureClass,\n count,\n share: n > 0 ? count / n : 0,\n }))\n .sort((a, b) => b.count - a.count || a.failureClass.localeCompare(b.failureClass))\n}\n\n// ── Prior-period comparison ─────────────────────────────────────────\n\n/** Direction of the metric — does \"higher current\" mean better or worse?\n * Composite + judge dimensions: higher is better. Cost + duration: lower\n * is better. The recommendations engine flips the sign before judging\n * regressed vs improved. */\ntype MetricDirection = 'higher-is-better' | 'lower-is-better'\n\nfunction computePriorPeriodComparison(\n current: RunRecord[],\n baseline: RunRecord[],\n split: 'search' | 'holdout',\n windowLabel: string | undefined,\n): PriorPeriodComparison | undefined {\n if (current.length === 0 || baseline.length === 0) return undefined\n\n const metrics: Record<string, MetricDelta> = {}\n const directions: Record<string, MetricDirection> = {}\n\n const compositeCurrent = current\n .map((r) => compositeOf(r, split))\n .filter(Number.isFinite) as number[]\n const compositeBaseline = baseline\n .map((r) => compositeOf(r, split))\n .filter(Number.isFinite) as number[]\n if (compositeCurrent.length > 0 && compositeBaseline.length > 0) {\n metrics.composite = welchCompare(compositeBaseline, compositeCurrent)\n directions.composite = 'higher-is-better'\n }\n\n const costCurrent = knownCostValues(current)\n const costBaseline = knownCostValues(baseline)\n if (costCurrent.length > 0 && costBaseline.length > 0) {\n metrics.cost = welchCompare(costBaseline, costCurrent)\n directions.cost = 'lower-is-better'\n }\n\n const durCurrent = current.map((r) => r.wallMs).filter(Number.isFinite)\n const durBaseline = baseline.map((r) => r.wallMs).filter(Number.isFinite)\n if (durCurrent.length > 0 && durBaseline.length > 0) {\n metrics.duration = welchCompare(durBaseline, durCurrent)\n directions.duration = 'lower-is-better'\n }\n\n const tokCurrent = current\n .map((r) => (r.tokenUsage.input ?? 0) + (r.tokenUsage.output ?? 0))\n .filter(Number.isFinite)\n const tokBaseline = baseline\n .map((r) => (r.tokenUsage.input ?? 0) + (r.tokenUsage.output ?? 0))\n .filter(Number.isFinite)\n if (tokCurrent.length > 0 && tokBaseline.length > 0) {\n metrics.tokenUsage = welchCompare(tokBaseline, tokCurrent)\n directions.tokenUsage = 'lower-is-better'\n }\n\n // Per-dimension judge comparisons — only for dimensions present in BOTH\n // windows. We use perDimMean since per-judge nesting is finicky for\n // two-sample comparisons across different judge configurations.\n const dimsCurrent = collectPerDimension(current)\n const dimsBaseline = collectPerDimension(baseline)\n for (const dim of Object.keys(dimsCurrent)) {\n const b = dimsBaseline[dim]\n const c = dimsCurrent[dim]\n if (!b || b.length === 0 || !c || c.length === 0) continue\n metrics[`dim.${dim}`] = welchCompare(b, c)\n directions[`dim.${dim}`] = 'higher-is-better'\n }\n\n const regressedMetrics: string[] = []\n const improvedMetrics: string[] = []\n const inconclusiveMetrics: string[] = []\n for (const [name, delta] of Object.entries(metrics)) {\n if (delta.status !== 'ok') {\n inconclusiveMetrics.push(name)\n continue\n }\n if (!delta.significant) continue\n const dir = directions[name] ?? 'higher-is-better'\n const better = dir === 'higher-is-better' ? delta.delta > 0 : delta.delta < 0\n if (better) improvedMetrics.push(name)\n else regressedMetrics.push(name)\n }\n\n return {\n baselineN: baseline.length,\n currentN: current.length,\n ...(windowLabel ? { windowLabel } : {}),\n metrics,\n regressedMetrics,\n improvedMetrics,\n inconclusiveMetrics,\n }\n}\n\nfunction knownCostValues(runs: RunRecord[]): number[] {\n return runs\n .filter((run) => run.costProvenance.kind !== 'uncaptured')\n .map((run) => run.costUsd)\n .filter(isFiniteNumber)\n}\n\nfunction isFiniteNumber(value: unknown): value is number {\n return typeof value === 'number' && Number.isFinite(value)\n}\n\n/** Collect per-dimension values across runs (from outcome.judgeScores.perDimMean). */\nfunction collectPerDimension(runs: RunRecord[]): Record<string, number[]> {\n const out: Record<string, number[]> = {}\n for (const r of runs) {\n const perDim = r.outcome.judgeScores?.perDimMean\n if (!perDim) continue\n for (const [dim, value] of Object.entries(perDim)) {\n if (!Number.isFinite(value)) continue\n if (!out[dim]) out[dim] = []\n out[dim].push(value as number)\n }\n }\n return out\n}\n\n/** Adapt the shared two-sample Welch result to the report contract. */\nfunction welchCompare(baseline: number[], current: number[]): MetricDelta {\n const result = welchsTTest(baseline, current)\n const base = {\n current: result.meanB,\n baseline: result.meanA,\n delta: result.delta,\n baselineN: baseline.length,\n currentN: current.length,\n }\n if (result.status !== 'ok') {\n return {\n ...base,\n status: result.status,\n ci95: null,\n pValue: null,\n cohensD: null,\n significant: false,\n }\n }\n return {\n ...base,\n status: 'ok',\n ci95: result.ci95,\n pValue: result.p,\n cohensD: result.cohensD,\n significant: result.p < 0.05 && Math.abs(result.cohensD) >= 0.2,\n }\n}\n\n// ── Composite + split selection ─────────────────────────────────────\n\nfunction resolveSplit(\n runs: RunRecord[],\n pref: 'search' | 'holdout' | 'auto',\n): 'search' | 'holdout' {\n if (pref !== 'auto') return pref\n const hasHoldout = runs.some((r) => Number.isFinite(observedSplitScore(r, 'holdout')))\n return hasHoldout ? 'holdout' : 'search'\n}\n\n/**\n * RAW (`observedSplitScore`): `analyzeRuns` describes what a set of runs\n * reported, and every downstream reader of this composite — distributions,\n * per-candidate summaries, the reward-hacking correlation — needs the ungated\n * number to see an inflated run at all.\n */\nfunction compositeOf(run: RunRecord, split: 'search' | 'holdout'): number {\n // Split-exact, no cross-split fallthrough: answering \"what did this run\n // score on the split I am summarising\" with the other split's number\n // silently mixes populations.\n const score = observedSplitScore(run, split)\n return Number.isFinite(score) ? (score as number) : Number.NaN\n}\n\n// ── Distribution helpers ────────────────────────────────────────────\n\nfunction distributionOf(\n values: number[],\n bins: number,\n withIds?: Array<{ runId: string; score: number }>,\n): ScalarDistribution {\n if (values.length === 0) {\n return {\n n: 0,\n mean: null,\n p50: null,\n p95: null,\n stddev: null,\n min: null,\n max: null,\n histogram: [],\n }\n }\n const sorted = [...values].sort((a, b) => a - b)\n const n = sorted.length\n const mean = sorted.reduce((s, v) => s + v, 0) / n\n const variance = sorted.reduce((s, v) => s + (v - mean) ** 2, 0) / n\n const stddev = Math.sqrt(variance)\n const tailRuns = withIds\n ? [...withIds].sort((a, b) => a.score - b.score).slice(0, Math.min(5, withIds.length))\n : undefined\n return {\n n,\n mean,\n p50: percentile(sorted, 0.5),\n p95: percentile(sorted, 0.95),\n stddev,\n min: sorted[0]!,\n max: sorted[n - 1]!,\n histogram: histogram(sorted, bins),\n ...(tailRuns ? { tailRuns } : {}),\n }\n}\n\nfunction percentile(sorted: number[], q: number): number {\n if (sorted.length === 0) return 0\n if (sorted.length === 1) return sorted[0]!\n const idx = (sorted.length - 1) * q\n const lo = Math.floor(idx)\n const hi = Math.ceil(idx)\n if (lo === hi) return sorted[lo]!\n const w = idx - lo\n return sorted[lo]! * (1 - w) + sorted[hi]! * w\n}\n\n/** Even-width histogram over the value range. Returns inclusive-lo /\n * exclusive-hi bins (closed on right for the last bin) compatible with\n * the substrate's `GainDistributionBin` shape. */\nfunction histogram(sorted: number[], bins: number): ScalarDistribution['histogram'] {\n if (sorted.length === 0 || bins < 1) return []\n const min = sorted[0]!\n const max = sorted[sorted.length - 1]!\n if (min === max) return [{ lo: min, hi: max, count: sorted.length }]\n const width = (max - min) / bins\n const out: ScalarDistribution['histogram'] = []\n for (let i = 0; i < bins; i++) {\n const lo = min + i * width\n const hi = i === bins - 1 ? max : lo + width\n out.push({ lo, hi, count: 0 })\n }\n for (const v of sorted) {\n const idx = Math.min(bins - 1, Math.floor((v - min) / width))\n out[idx]!.count++\n }\n return out\n}\n\nfunction computePerDimension(runs: RunRecord[], bins: number): Record<string, ScalarDistribution> {\n // JudgeScoresRecord pre-aggregates `perDimMean` (mean across judges per\n // dimension). We collect those means across runs to produce a per-dim\n // distribution at the corpus level. Consumers who want per-judge\n // dimension values reach into `perJudge[judgeId][dim]` themselves.\n const byDim = new Map<string, number[]>()\n for (const run of runs) {\n const scores = run.outcome.judgeScores\n if (!scores) continue\n for (const [dim, value] of Object.entries(scores.perDimMean ?? {})) {\n if (!Number.isFinite(value)) continue\n const arr = byDim.get(dim) ?? []\n arr.push(value)\n byDim.set(dim, arr)\n }\n }\n const out: Record<string, ScalarDistribution> = {}\n for (const [dim, values] of byDim) out[dim] = distributionOf(values, bins)\n return out\n}\n\n// ── Judge insights ──────────────────────────────────────────────────\n\nfunction computeJudgeInsights(runs: RunRecord[]): Record<string, JudgeInsight> {\n // Each judge's per-run mean is the average of its per-dimension scores\n // for that run. We aggregate those means across all runs each judge\n // scored — giving consumers a \"this judge's typical verdict\" reading.\n const out: Record<string, JudgeInsight> = {}\n const byJudge = new Map<string, number[]>()\n for (const run of runs) {\n const scores = run.outcome.judgeScores\n if (!scores?.perJudge) continue\n for (const [judgeId, dims] of Object.entries(scores.perJudge)) {\n const dimValues = Object.values(dims).filter(Number.isFinite) as number[]\n if (dimValues.length === 0) continue\n const judgeMean = dimValues.reduce((s, v) => s + v, 0) / dimValues.length\n const arr = byJudge.get(judgeId) ?? []\n arr.push(judgeMean)\n byJudge.set(judgeId, arr)\n }\n }\n for (const [judgeId, values] of byJudge) {\n out[judgeId] = {\n n: values.length,\n meanScore: values.reduce((s, v) => s + v, 0) / values.length,\n }\n }\n return out\n}\n\n// ── Inter-rater agreement ───────────────────────────────────────────\n\nfunction computeInterRater(\n ratings: Array<{ runId: string; rater: string; score: number }>,\n): InterRaterInsight | undefined {\n const byRun = new Map<string, Array<{ rater: string; score: number }>>()\n for (const r of ratings) {\n if (!Number.isFinite(r.score)) continue\n const list = byRun.get(r.runId) ?? []\n list.push({ rater: r.rater, score: r.score })\n byRun.set(r.runId, list)\n }\n const raters = new Set(ratings.map((r) => r.rater))\n const jointlyRated: string[] = []\n for (const [runId, ratersForRun] of byRun) {\n const seen = new Set(ratersForRun.map((r) => r.rater))\n let all = true\n for (const r of raters) if (!seen.has(r)) all = false\n if (all) jointlyRated.push(runId)\n }\n if (raters.size < 2 || jointlyRated.length === 0) return undefined\n\n const raterList = [...raters].sort()\n const perPair: Record<string, number> = {}\n for (let i = 0; i < raterList.length; i++) {\n for (let j = i + 1; j < raterList.length; j++) {\n const a = raterList[i]!\n const b = raterList[j]!\n const aScores: number[] = []\n const bScores: number[] = []\n for (const runId of jointlyRated) {\n const ratersForRun = byRun.get(runId)!\n const sa = ratersForRun.find((r) => r.rater === a)?.score\n const sb = ratersForRun.find((r) => r.rater === b)?.score\n if (sa !== undefined && sb !== undefined) {\n aScores.push(sa)\n bScores.push(sb)\n }\n }\n const agreement = continuousAgreement(\n aScores.map((score, index) => [score, bScores[index]!]),\n { bootstrap: 0 },\n )\n perPair[`${a}::${b}`] = agreement.weightedKappa\n }\n }\n const matrix = jointlyRated.map((runId) => {\n const ratingsByRater = new Map(byRun.get(runId)!.map((rating) => [rating.rater, rating.score]))\n return raterList.map((rater) => ratingsByRater.get(rater)!)\n })\n const agreement = continuousAgreement(matrix, { bootstrap: 0 })\n\n const disagreementCases = jointlyRated\n .map((runId) => {\n const ratersForRun = byRun.get(runId)!\n const scores = ratersForRun.map((r) => r.score)\n const range = Math.max(...scores) - Math.min(...scores)\n return { runId, ratings: ratersForRun, range }\n })\n .sort((a, b) => b.range - a.range)\n .slice(0, 20)\n\n return {\n raters: raters.size,\n jointlyRated: jointlyRated.length,\n kappa: Number.isFinite(agreement.weightedKappa) ? agreement.weightedKappa : 0,\n icc: agreement.icc,\n pearson: agreement.pearson,\n spearman: agreement.spearman,\n perPair,\n disagreementCases,\n }\n}\n\n// ── Lift ────────────────────────────────────────────────────────────\n\nfunction computeLift(\n runs: RunRecord[],\n baselineId: string | undefined,\n candidateId: string | undefined,\n split: 'search' | 'holdout',\n): LiftInsight | undefined {\n let bId = baselineId\n let cId = candidateId\n if (!bId || !cId) {\n // Auto-detect: when exactly two distinct candidateIds appear, treat the\n // lower-mean side as baseline.\n const ids = [...new Set(runs.map((r) => r.candidateId))]\n if (ids.length !== 2) return undefined\n const [idA, idB] = ids as [string, string]\n const scoresA = finiteCompositeScores(\n runs.filter((run) => run.candidateId === idA),\n split,\n )\n const scoresB = finiteCompositeScores(\n runs.filter((run) => run.candidateId === idB),\n split,\n )\n if (scoresA.length === 0 || scoresB.length === 0) return undefined\n const meanA = mean(scoresA)\n const meanB = mean(scoresB)\n bId = meanA <= meanB ? idA : idB\n cId = meanA <= meanB ? idB : idA\n }\n\n const baseline = runs.filter((r) => r.candidateId === bId)\n const candidate = runs.filter((r) => r.candidateId === cId)\n if (baseline.length === 0 || candidate.length === 0) return undefined\n\n const scoredBaseline = baseline.filter((run) => Number.isFinite(compositeOf(run, split)))\n const scoredCandidate = candidate.filter((run) => Number.isFinite(compositeOf(run, split)))\n const pairing = pairRunRecords(scoredBaseline, scoredCandidate)\n const pairedBaseline = pairing.pairs.map((pair) => compositeOf(pair.baseline, split))\n const pairedCandidate = pairing.pairs.map((pair) => compositeOf(pair.treatment, split))\n if (pairedBaseline.length === 0) return undefined\n\n const baselineMean = mean(pairedBaseline)\n const candidateMean = mean(pairedCandidate)\n const delta = candidateMean - baselineMean\n\n const bootstrap = pairedBootstrap(pairedBaseline, pairedCandidate, {\n confidence: 0.95,\n resamples: 2000,\n statistic: 'mean',\n })\n const tTest = pairedTTest(pairedBaseline, pairedCandidate)\n const d = pairedCohensDz(pairedBaseline, pairedCandidate)\n const mde = pairedMde({ nPaired: pairedBaseline.length, power: 0.8, alpha: 0.05 })\n const requiredN =\n d === null || d === 0\n ? null\n : requiredPairedSampleSize({\n effect: Math.abs(d),\n power: 0.8,\n alpha: 0.05,\n })\n\n return {\n baselineMean,\n candidateMean,\n delta,\n ci95: [bootstrap.low, bootstrap.high],\n pValue: tTest.p,\n n: pairedBaseline.length,\n minimumRequired: BOOTSTRAP_GATE_MIN_N,\n decisionEligible: bootstrap.gateEligible,\n unpairedBaseline: pairing.unpairedBaseline.length,\n unpairedCandidate: pairing.unpairedTreatment.length,\n cohensD: d,\n mde,\n requiredN,\n }\n}\n\nfunction mean(arr: number[]): number {\n return arr.length === 0 ? 0 : arr.reduce((s, v) => s + v, 0) / arr.length\n}\n\n// ── Failure clustering ──────────────────────────────────────────────\n\nasync function computeFailureClusters(\n runs: RunRecord[],\n analyst: AnalystRegistry,\n split: 'search' | 'holdout',\n): Promise<FailureClusterInsight | undefined> {\n const failed = runs.filter((run) => isTaskFailure(run, split))\n if (failed.length === 0) return { clusters: [], totalFailures: 0 }\n\n const clusters = new Map<string, { exemplars: string[]; share: number }>()\n for (const run of failed) {\n try {\n // AnalystRunInputs routes by field name: run-record analysts read\n // `runRecord`. Any other shape makes every analyst skip with\n // \"missing input\" and the clusters come back silently empty.\n const result = await analyst.run(run.runId, { runRecord: run })\n for (const finding of result.findings as AnalystFinding[]) {\n const key = finding.area || finding.analyst_id || 'unclassified'\n const c = clusters.get(key) ?? { exemplars: [], share: 0 }\n if (c.exemplars.length < 5) c.exemplars.push(run.runId)\n clusters.set(key, c)\n }\n } catch {\n const c = clusters.get('analyst-error') ?? { exemplars: [], share: 0 }\n if (c.exemplars.length < 5) c.exemplars.push(run.runId)\n clusters.set('analyst-error', c)\n }\n }\n const clusterList = [...clusters.entries()].map(([id, c]) => ({\n id,\n name: id,\n share: c.exemplars.length / failed.length,\n exemplars: c.exemplars,\n }))\n clusterList.sort((a, b) => b.share - a.share)\n return { clusters: clusterList, totalFailures: failed.length }\n}\n\nfunction finiteCompositeScores(runs: readonly RunRecord[], split: 'search' | 'holdout'): number[] {\n return runs.map((run) => compositeOf(run, split)).filter(Number.isFinite)\n}\n\nfunction isTaskFailure(run: RunRecord, split: 'search' | 'holdout'): boolean {\n if (run.failureClass !== undefined && run.failureClass !== 'success') return true\n const score = compositeOf(run, split)\n return Number.isFinite(score) && score < 0.5\n}\n\n// ── Contamination ──────────────────────────────────────────────────\n\nfunction computeContamination(\n runs: RunRecord[],\n canaries: DatasetScenario[],\n): InsightReport['contamination'] {\n let leaks = 0\n const details: Array<{ runId: string; canary: string; matched: string }> = []\n for (const run of runs) {\n const output = stringifyOutput(run)\n if (!output) continue\n const leaksHere = checkCanaries(output, canaries)\n for (const leak of leaksHere) {\n leaks++\n details.push({ runId: run.runId, canary: leak.canary, matched: leak.evidence })\n }\n }\n return { leaks, holdoutAuditPassed: leaks === 0, details }\n}\n\nfunction stringifyOutput(run: RunRecord): string | undefined {\n // RunRecord doesn't fix where \"the agent's output\" lives — different\n // consumers stash it differently. We probe the common shapes: the\n // outcome.raw map (numeric only by design — unlikely to contain text),\n // and any string-valued fields tucked under metadata via type casting.\n // Consumers with bespoke shapes pass canaryScenarios only when they\n // know their runs carry a stringifiable surface.\n const metadata = (run as unknown as { metadata?: Record<string, unknown> }).metadata\n if (typeof metadata?.output === 'string') return metadata.output\n if (typeof metadata?.text === 'string') return metadata.text\n return undefined\n}\n\n// ── Outcome correlation + linear reward model ──────────────────────\n\nfunction computeOutcomeCorrelation(\n runs: RunRecord[],\n outcome: { metric: string; valueByRunId: Record<string, number> },\n split: 'search' | 'holdout',\n): OutcomeCorrelationInsight | undefined {\n const xs: number[] = []\n const ys: number[] = []\n for (const run of runs) {\n const y = outcome.valueByRunId[run.runId]\n if (y === undefined || !Number.isFinite(y)) continue\n const x = compositeOf(run, split)\n if (!Number.isFinite(x)) continue\n xs.push(x)\n ys.push(y)\n }\n if (xs.length < 3) return undefined\n\n const p = pearsonR(xs, ys)\n const s = spearmanR(xs, ys)\n const meanX = mean(xs)\n const meanY = mean(ys)\n let num = 0\n let denom = 0\n for (let i = 0; i < xs.length; i++) {\n num += (xs[i]! - meanX) * (ys[i]! - meanY)\n denom += (xs[i]! - meanX) ** 2\n }\n const slope = denom === 0 ? 0 : num / denom\n const intercept = meanY - slope * meanX\n const ssTot = ys.reduce((a, y) => a + (y - meanY) ** 2, 0)\n const ssRes = ys.reduce((a, y, i) => a + (y - (intercept + slope * xs[i]!)) ** 2, 0)\n const r2 = ssTot === 0 ? 0 : 1 - ssRes / ssTot\n\n return {\n metric: outcome.metric,\n n: xs.length,\n pearson: p,\n spearman: s,\n rewardModel: { intercept, slope, r2 },\n }\n}\n\n// ── Release confidence scorecard ───────────────────────────────────\n\nfunction buildReleaseScorecard(\n composite: ScalarDistribution,\n lift: LiftInsight | undefined,\n contamination: InsightReport['contamination'],\n): InsightReport['release'] {\n // Synthesise a minimal scorecard from the rolled-up signal. The\n // substrate's `evaluateReleaseConfidence` primitive consumes a richer\n // input shape that callers can produce by wiring SLO definitions; the\n // shape here is the contract `selfImprove`/`analyzeRuns` consumers\n // receive automatically. They can call `evaluateReleaseConfidence`\n // directly when they want SLO-based axis evaluation.\n const axes: InsightReport['release']['axes'] = []\n const liftPass =\n lift === undefined\n ? ('not_evaluated' as const)\n : !lift.decisionEligible\n ? ('not_evaluated' as const)\n : lift.ci95[0] > 0 && !zeroWidth(lift.ci95)\n ? ('pass' as const)\n : lift.delta > 0\n ? ('warn' as const)\n : ('fail' as const)\n axes.push({\n name: 'quality-lift',\n status: liftPass,\n detail: lift\n ? `delta=${lift.delta.toFixed(3)}, CI95=[${lift.ci95[0].toFixed(3)}, ${lift.ci95[1].toFixed(3)}], n=${lift.n}${lift.decisionEligible ? '' : ` (descriptive only; ${lift.minimumRequired} required)`}`\n : 'no baseline/candidate pair available',\n })\n const contamPass =\n contamination === undefined\n ? ('not_evaluated' as const)\n : contamination.leaks === 0\n ? ('pass' as const)\n : ('fail' as const)\n axes.push({\n name: 'contamination',\n status: contamPass,\n detail: contamination ? `${contamination.leaks} canary leak(s)` : 'no canaries supplied',\n })\n axes.push(\n composite.n === 0\n ? {\n name: 'composite-distribution',\n status: 'not_evaluated',\n detail: 'no task-quality scores available',\n }\n : {\n name: 'composite-distribution',\n status:\n composite.mean !== null && composite.mean >= 0.5\n ? 'pass'\n : composite.mean !== null && composite.mean >= 0.3\n ? 'warn'\n : 'fail',\n detail:\n composite.mean === null || composite.p50 === null || composite.p95 === null\n ? 'task-quality distribution is internally incomplete'\n : `mean=${composite.mean.toFixed(3)}, p50=${composite.p50.toFixed(3)}, p95=${composite.p95.toFixed(3)} over n=${composite.n}`,\n },\n )\n const status = axes.some((a) => a.status === 'fail')\n ? 'fail'\n : axes.some((a) => a.status === 'warn' || a.status === 'not_evaluated')\n ? 'warn'\n : 'pass'\n return {\n status,\n axes,\n issues: [],\n }\n}\n\n// ── Recommendations engine ─────────────────────────────────────────\n\ninterface RecommendationContext {\n composite: ScalarDistribution\n judges: Record<string, JudgeInsight>\n interRater?: InterRaterInsight\n lift?: LiftInsight\n failureClusters?: FailureClusterInsight\n failureClasses?: FailureClassTally[]\n contamination?: InsightReport['contamination']\n outcomeCorrelation?: OutcomeCorrelationInsight\n priorPeriodComparison?: PriorPeriodComparison\n threshold: number\n}\n\nfunction buildRecommendations(ctx: RecommendationContext): Recommendation[] {\n const out: Recommendation[] = []\n\n // Prior-period regressions — highest customer-impact signal when present.\n // \"Did my last change help?\" with a falsifiable answer.\n if (ctx.priorPeriodComparison) {\n const ppc = ctx.priorPeriodComparison\n const label = ppc.windowLabel ?? 'baseline period'\n for (const name of ppc.regressedMetrics) {\n const d = ppc.metrics[name]\n if (d?.status !== 'ok') continue\n out.push({\n priority: 'critical',\n kind: 'investigate',\n title: `${name} regressed from ${d.baseline.toFixed(3)} → ${d.current.toFixed(3)} vs ${label}`,\n detail: `Welch CI95 = [${d.ci95[0].toFixed(3)}, ${d.ci95[1].toFixed(3)}], p=${d.pValue.toFixed(4)}, Cohen's d=${d.cohensD.toFixed(2)} (n_current=${d.currentN}, n_baseline=${d.baselineN}). The regression is statistically significant at p<0.05 with at-least-small effect size.`,\n evidencePath: `priorPeriodComparison.metrics.${name}`,\n })\n }\n for (const name of ppc.improvedMetrics) {\n const d = ppc.metrics[name]\n if (d?.status !== 'ok') continue\n out.push({\n priority: 'low',\n kind: 'ship',\n title: `${name} improved from ${d.baseline.toFixed(3)} → ${d.current.toFixed(3)} vs ${label}`,\n detail: `Welch CI95 = [${d.ci95[0].toFixed(3)}, ${d.ci95[1].toFixed(3)}], p=${d.pValue.toFixed(4)}, Cohen's d=${d.cohensD.toFixed(2)} (n_current=${d.currentN}, n_baseline=${d.baselineN}). Statistically significant improvement worth flagging.`,\n evidencePath: `priorPeriodComparison.metrics.${name}`,\n })\n }\n for (const name of ppc.inconclusiveMetrics) {\n const d = ppc.metrics[name]\n if (!d || d.status === 'ok' || d.delta === 0) continue\n const reason =\n d.status === 'zero-variance'\n ? 'both periods have zero observed variance'\n : 'one or both periods have fewer than two observations'\n out.push({\n priority: 'high',\n kind: 'investigate',\n title: `${name} changed from ${d.baseline.toFixed(3)} → ${d.current.toFixed(3)} vs ${label}; inference unavailable`,\n detail: `Observed delta ${d.delta.toFixed(3)} across n_current=${d.currentN} and n_baseline=${d.baselineN}, but ${reason}. The report does not fabricate a p-value, confidence interval, or effect size; inspect independence and data capture before acting.`,\n evidencePath: `priorPeriodComparison.metrics.${name}`,\n })\n }\n }\n\n // Composite-distribution branch. Fires when the overall quality signal is\n // poor regardless of lift / contamination / clusters — the customer needs\n // to know they have a problem AND which specific runs to inspect.\n if (\n ctx.composite.n > 0 &&\n ctx.composite.mean !== null &&\n ctx.composite.p50 !== null &&\n ctx.composite.p95 !== null\n ) {\n if (ctx.composite.mean < 0.3) {\n const tail = ctx.composite.tailRuns ?? []\n const names = tail\n .slice(0, 5)\n .map((t) => `${t.runId}=${t.score.toFixed(3)}`)\n .join(', ')\n out.push({\n priority: 'critical',\n kind: 'investigate',\n title: `Composite mean ${ctx.composite.mean.toFixed(3)} is below the 0.3 floor — the agent is broken on this corpus`,\n detail:\n tail.length > 0\n ? `Worst ${tail.length} run${tail.length === 1 ? '' : 's'} to inspect first: ${names}. Histogram p50=${ctx.composite.p50.toFixed(3)}, p95=${ctx.composite.p95.toFixed(3)}.`\n : `Histogram p50=${ctx.composite.p50.toFixed(3)}, p95=${ctx.composite.p95.toFixed(3)}.`,\n evidencePath: 'composite.tailRuns',\n })\n } else if (ctx.composite.mean < 0.5) {\n const tail = ctx.composite.tailRuns ?? []\n const names = tail\n .slice(0, 3)\n .map((t) => `${t.runId}=${t.score.toFixed(3)}`)\n .join(', ')\n out.push({\n priority: 'high',\n kind: 'investigate',\n title: `Composite mean ${ctx.composite.mean.toFixed(3)} is below 0.5 — investigate the lower tail before claiming the agent is healthy`,\n detail:\n tail.length > 0\n ? `Worst ${tail.length} run${tail.length === 1 ? '' : 's'}: ${names}. Histogram p50=${ctx.composite.p50.toFixed(3)}, p95=${ctx.composite.p95.toFixed(3)}.`\n : `Histogram p50=${ctx.composite.p50.toFixed(3)}, p95=${ctx.composite.p95.toFixed(3)}.`,\n evidencePath: 'composite.tailRuns',\n })\n }\n }\n\n // A healthy-looking mean can hide a group of failed tasks sharing one\n // producer-reported cause. This path does not require an analyst.\n if (ctx.failureClasses && ctx.failureClasses.length > 0) {\n const top = ctx.failureClasses[0]!\n if (top.count >= 3 && top.share >= 0.15) {\n out.push({\n priority: top.share >= 0.25 ? 'high' : 'medium',\n kind: 'investigate',\n title: `'${top.failureClass}' is the dominant failure class — ${top.count} runs (${(top.share * 100).toFixed(0)}% of the corpus)`,\n detail: `The mean composite can look acceptable while one failure class dominates the lower tail. ${top.count} of ${ctx.composite.n} runs failed with '${top.failureClass}'${ctx.failureClasses.length > 1 ? ` (next: '${ctx.failureClasses[1]!.failureClass}' ×${ctx.failureClasses[1]!.count})` : ''}. Fix this cause first.`,\n evidencePath: 'failureClasses',\n })\n }\n }\n\n // Missing-judges branch. The report can't surface per-dimension or\n // calibration signal when `outcome.judgeScores` is empty across the\n // corpus. Tell the customer how to enrich.\n if (Object.keys(ctx.judges).length === 0 && ctx.composite.n > 0) {\n out.push({\n priority: 'medium',\n kind: 'expand-corpus',\n title: 'No judge scores recorded — per-dimension + calibration insights unavailable',\n detail:\n 'Records have no `outcome.judgeScores`. To unlock perDimension, judges, and calibration, attach a Judge run during your eval pass and populate `outcome.judgeScores.perJudge[judgeName][dimension] = score`. See `docs/insight-report.md` for the expected shape.',\n evidencePath: 'judges',\n })\n }\n\n if (ctx.lift) {\n if (!ctx.lift.decisionEligible) {\n out.push({\n priority: 'high',\n kind: 'expand-corpus',\n title: `Inconclusive — ${ctx.lift.n} paired runs; ${ctx.lift.minimumRequired} required`,\n detail: `The bootstrap interval is descriptive below ${ctx.lift.minimumRequired} paired observations and cannot support a ship decision.`,\n evidencePath: 'lift',\n })\n } else {\n const pairedEffect =\n ctx.lift.cohensD === null ? 'undefined (zero delta variance)' : ctx.lift.cohensD.toFixed(2)\n const pairedP =\n ctx.lift.pValue === null ? 'undefined (zero delta variance)' : ctx.lift.pValue.toFixed(4)\n const requiredRuns =\n ctx.lift.requiredN === null ? 'not estimable' : `~${ctx.lift.requiredN} paired runs`\n // A ZERO-WIDTH interval never reads as \"ship\": n identical paired deltas\n // make every resample identical, so `[g, g]` clears any threshold below g\n // and `[0, 0]` clears any negative `decisionThreshold`, on no spread at\n // all. It falls through to the inconclusive/hold arms, which is where a\n // sample carrying no information about its own error belongs.\n const decisive = !zeroWidth(ctx.lift.ci95) && ctx.lift.ci95[0] > ctx.threshold\n const inconclusive = ctx.lift.ci95[0] <= ctx.threshold && ctx.lift.ci95[1] > ctx.threshold\n if (decisive) {\n out.push({\n priority: 'critical',\n kind: 'ship',\n title: `Ship — lift ${ctx.lift.delta.toFixed(3)} (95% CI ${ctx.lift.ci95[0].toFixed(3)}..${ctx.lift.ci95[1].toFixed(3)})`,\n detail: `Holdout lift exceeds threshold ${ctx.threshold} with 95% bootstrap confidence (n=${ctx.lift.n}, p=${pairedP}, paired d=${pairedEffect}).`,\n evidencePath: 'lift',\n })\n } else if (inconclusive) {\n out.push({\n priority: 'high',\n kind: 'expand-corpus',\n title: `Inconclusive — required sample is ${requiredRuns} (have ${ctx.lift.n}) at current effect size`,\n detail: `CI straddles threshold. Current MDE at 80% power is ${ctx.lift.mde.toFixed(3)}; observed delta is ${ctx.lift.delta.toFixed(3)}.`,\n evidencePath: 'lift',\n })\n } else {\n out.push({\n priority: 'critical',\n kind: 'hold',\n title: `Hold — lift CI lower bound ${ctx.lift.ci95[0].toFixed(3)} is at or below threshold ${ctx.threshold}`,\n detail: `Bootstrap CI provides no statistical evidence the candidate is better. Consider tightening the mutation or expanding the holdout.`,\n evidencePath: 'lift',\n })\n }\n }\n }\n\n if (ctx.contamination && ctx.contamination.leaks > 0) {\n out.push({\n priority: 'critical',\n kind: 'fix',\n title: `${ctx.contamination.leaks} canary leak${ctx.contamination.leaks === 1 ? '' : 's'} detected`,\n detail: `Holdout integrity is compromised. The lift number is unreliable until you investigate.`,\n evidencePath: 'contamination',\n })\n }\n\n if (ctx.interRater && ctx.interRater.kappa < 0.5) {\n out.push({\n priority: 'high',\n kind: 'recalibrate',\n title: `Inter-rater weighted kappa ${ctx.interRater.kappa.toFixed(2)} is below 0.5`,\n detail:\n 'Raters disagree on what good looks like. Review the largest disagreement cases and refine the rubric before automating these decisions.',\n evidencePath: 'interRater',\n })\n }\n\n if (ctx.failureClusters && ctx.failureClusters.clusters.length > 0) {\n const top = ctx.failureClusters.clusters[0]!\n out.push({\n priority: 'high',\n kind: 'investigate',\n title: `Top failure cluster: ${top.name} (${(top.share * 100).toFixed(0)}% of failures)`,\n detail: `${ctx.failureClusters.totalFailures} runs failed. The largest cluster groups ${top.exemplars.length} exemplars under '${top.name}'.`,\n evidencePath: 'failureClusters.clusters[0]',\n })\n }\n\n if (ctx.outcomeCorrelation && Math.abs(ctx.outcomeCorrelation.spearman) < 0.3) {\n out.push({\n priority: 'medium',\n kind: 'recalibrate',\n title: `Judge scores decoupled from ${ctx.outcomeCorrelation.metric} (Spearman ρ=${ctx.outcomeCorrelation.spearman.toFixed(2)})`,\n detail: `Your judges score what they were trained to score, but it isn't predicting downstream ${ctx.outcomeCorrelation.metric}. Consider retraining the judge against ${ctx.outcomeCorrelation.metric} as the gold signal.`,\n evidencePath: 'outcomeCorrelation',\n })\n }\n\n return out\n}\n\n// ── Re-export pareto figure spec for hosted-side rendering ─────────\n\nexport type { ParetoFigureSpec }\n","/**\n * Run one complete improvement job.\n *\n * A caller-owned `proposer` can generate candidates across local generations.\n * An external `method`, such as official GEPA or SkillOpt, owns its complete\n * search and returns one candidate. Both paths remeasure the selected candidate\n * against cases that candidate generation never receives.\n */\n\nimport type { ProposalFinding } from '../analyst/types'\nimport { defaultProductionGate } from '../campaign/gates/default-production-gate'\nimport { type PowerPreflight, powerPreflight } from '../campaign/gates/power-preflight'\nimport {\n assertOptimizationResult,\n type OptimizationMethod,\n type OptimizationMethodProvenance,\n type OptimizationMethodResult,\n} from '../campaign/presets/compare-optimization-methods'\nimport {\n type RunImprovementLoopResult,\n runImprovementLoop,\n} from '../campaign/presets/run-improvement-loop'\nimport type {\n PremeasuredOptimizationBaseline,\n RunOptimizationOptions,\n} from '../campaign/presets/run-optimization'\nimport {\n emitLoopProvenance,\n type LoopProvenanceRecord,\n loopProvenanceArgsFromResult,\n} from '../campaign/provenance'\nimport { resolveRunDir } from '../campaign/run-dir'\nimport {\n campaignCellExecutionEvidence,\n campaignCellJudgeDimensions,\n campaignCellTaskScore,\n campaignCellToRunRecord,\n} from '../campaign/run-record'\nimport {\n type CampaignStorage,\n createRunCostLedger,\n fsCampaignStorage,\n inMemoryCampaignStorage,\n} from '../campaign/storage'\nimport { surfaceContentHash, surfaceHash } from '../campaign/surface-identity'\nimport type {\n CampaignCellResult,\n DispatchContext,\n Gate,\n JudgeConfig,\n LabeledScenarioStore,\n MutableSurface,\n Scenario,\n SurfaceProposer,\n} from '../campaign/types'\nimport type { CostLedgerHandle, CostLedgerSummary, CostReceipt } from '../cost-ledger'\nimport { ValidationError } from '../errors'\nimport { createHostedClient, type HostedTenant } from '../hosted/client'\nimport type { EvalRunCellScore, EvalRunEvent, EvalRunGenerationSnapshot } from '../hosted/types'\nimport { modelHasSnapshot, type RunRecord, type RunSplitTag } from '../run-record'\nimport { analyzeRuns } from './analyze-runs'\nimport type { InsightReport } from './insight-report'\n\nexport interface SelfImproveBudget {\n /** Hard spend cap across the full run. Each paid call reserves its enforced\n * maximum before dispatch, so completed spend cannot cross this amount. */\n dollars?: number\n /** Proposer generations. Default: 3. External methods own their rounds and\n * require this value to be omitted or set to 1. Set 0 only for a\n * proposer-free baseline run. */\n generations?: number\n /** Candidates the proposer emits per generation. Default 2. */\n populationSize?: number\n /** Max concurrent cells across the loop. Default 2. */\n maxConcurrency?: number\n /** Candidate campaigns scored in parallel. Default 1. Total concurrent\n * cells are bounded by `candidateConcurrency * maxConcurrency`. */\n candidateConcurrency?: number\n /** Fraction of `scenarios` held out from training, used for the gate.\n * Default 0.25. Ignored when `holdoutScenarios` is set explicitly. */\n holdoutFraction?: number\n /** Fraction of the non-final cases reserved for method selection.\n * Default 0.25. Used only with `method` and ignored when\n * `selectionScenarios` is supplied explicitly. */\n selectionFraction?: number\n /** Explicit held-out scenarios; overrides `holdoutFraction`. */\n holdoutScenarios?: Scenario[]\n /** Holdout policy. Default `'measured'`: split, re-score baseline vs winner\n * on the held-out set, gate on that comparison. `'deferred'`: run the\n * improvement-set campaigns + search promotion, dispatch ZERO holdout cells,\n * force the gate to `'hold'`, return `lift: undefined`, and record\n * `holdout: 'deferred'` in the provenance record — for callers that measure\n * the held-out comparison in a separate later run instead of faking a\n * static holdout scenario and recording a meaningless lift. Unless\n * `holdoutScenarios` reserves an explicit set, ALL scenarios train. */\n holdout?: 'measured' | 'deferred'\n /** Per-scenario replicates per cell — raises bootstrap-CI tightness. Default 1. */\n reps?: number\n /** DEPTH dial forwarded to the proposer's `propose()` as\n * `ctx.maxImprovementShots` — max iterations an agentic candidate generator\n * may take per candidate (verify-in-session retries). Unset ⇒ the\n * proposer's own default. */\n maxImprovementShots?: number\n}\n\nexport type SelfImproveProgressEvent =\n | { kind: 'baseline.started'; scenarios: number }\n | { kind: 'baseline.completed'; compositeMean: number; durationMs: number }\n | { kind: 'generation.started'; index: number; populationSize: number }\n | { kind: 'generation.completed'; index: number; bestComposite: number; durationMs: number }\n // `lift` is absent when `budget.holdout === 'deferred'` — no held-out\n // measurement ran, and the search-split delta must not masquerade as one.\n | { kind: 'gate.decided'; decision: string; lift?: number }\n | { kind: 'power.estimated'; n: number; sd: number; mde: number; underpowered: boolean }\n\nexport interface SelfImproveOptions<TScenario extends Scenario, TArtifact> {\n /**\n * Your agent — a function that takes the current `MutableSurface`\n * (typically a system prompt the loop is optimizing) plus the\n * scenario + cell ctx, and returns the artifact your judge scores.\n *\n * Same shape as `RunOptimizationOptions.dispatchWithSurface`. Wrap a\n * plain `Dispatch` if you don't have a surface seam:\n *\n * agent: (_surface, scenario, ctx) => yourPlainDispatch(scenario, ctx)\n *\n * That mode evaluates without mutating any surface — useful as a\n * baseline-only run (set `budget.generations = 0`).\n */\n agent: (surface: MutableSurface, scenario: TScenario, ctx: DispatchContext) => Promise<TArtifact>\n\n /**\n * Snapshot-bearing model identity for agents that do not report a paid-call\n * receipt through `ctx.cost.runPaidCall()`.\n *\n * Omit this when every cell reports its concrete model in a receipt.\n */\n model?: string\n\n /** Scenarios to evaluate against. Train/holdout split is computed from\n * these unless `budget.holdoutScenarios` is set explicitly. */\n scenarios: TScenario[]\n\n /** Judge that scores artifacts. Bring your own; use `langchainJudge`\n * from `/adapters/langchain` for a Runnable-shaped one. */\n judge: JudgeConfig<TArtifact, TScenario>\n\n /** Starting surface — system prompt, JSON config, anything `MutableSurface`\n * accepts. The proposer mutates this each generation. */\n baselineSurface: MutableSurface\n\n /** Budget + loop shape. All fields optional. */\n budget?: SelfImproveBudget\n\n /**\n * Complete prior measurement of `baselineSurface` over the TRAIN split.\n * Forwarded to the loop body, which validates its surface hash, scenario\n * split, seed (42), reps, and coverage, then skips the baseline search\n * campaign entirely — no baseline dispatch, no resumability lookup. The\n * train split is `scenarios` minus the holdout split, so premeasure with\n * exactly that scenario set (explicit `budget.holdoutScenarios`, or\n * `budget.holdout: 'deferred'` with no reserved set, makes the train split\n * deterministic). Prior spend stays in the imported campaign aggregates and\n * is not re-added to this run's cost ledger.\n */\n premeasuredBaseline?: PremeasuredOptimizationBaseline<TArtifact, TScenario>\n\n /**\n * Candidate generator for this local generation loop.\n * Required when `budget.generations` is greater than zero.\n */\n proposer?: SurfaceProposer<ProposalFinding>\n\n /**\n * Complete optimization method, such as official GEPA or SkillOpt.\n * The method receives disjoint train and selection cases and never receives\n * the final comparison cases. Mutually exclusive with `proposer`.\n */\n method?: OptimizationMethod<TScenario, TArtifact>\n\n /** Explicit method-selection cases. They must also appear in `scenarios`\n * and must not overlap the final comparison cases. */\n selectionScenarios?: TScenario[]\n\n /** Custom gate. Default is `defaultProductionGate` with\n * `deltaThreshold: 0.05` on the held-out split. */\n gate?: Gate<TArtifact, TScenario>\n\n /** Placebo control. When supplied AND the winner differs from baseline, the\n * loop scores a THIRD held-out arm: the winner surface with its content\n * footprint-matched-blanked by this fn (typically via `neutralizeText`). Its\n * scores reach the gate as `ctx.neutralizedJudgeScores`, letting a\n * `neutralizationGate` reject a win whose lift survives blanking the content\n * (decorative — driven by footprint, not content). Costs one extra held-out\n * campaign; omit to skip. Compose `neutralizationGate` into `gate` to act on it. */\n neutralize?: (winnerSurface: MutableSurface, baselineSurface: MutableSurface) => MutableSurface\n\n /** Storage backend. A filesystem run directory uses `fsCampaignStorage()`;\n * a `mem://` directory uses in-memory storage. External methods default to\n * a filesystem directory because their official state must survive. */\n storage?: CampaignStorage\n\n /** Run directory. Proposer mode defaults to\n * `mem://selfImprove-<timestamp>`. External method mode defaults to\n * `.agent-eval/runs/self-improve-<timestamp>`. */\n runDir?: string\n\n /** Fires once the durable provenance record + OTel spans are emitted.\n * Receives the structured record for inline assertions / custom routing. */\n onProvenance?: (record: LoopProvenanceRecord) => void\n\n /** Distributed execution seam — same as `RunCampaignOptions.cellPlacement`.\n * Returns an opaque placement key the substrate forwards to your agent\n * as `ctx.placement`. Combined with `httpDispatch` from\n * `/adapters/http`, fans cells across regions. */\n cellPlacement?: (input: {\n scenario: TScenario\n rep: number\n generation?: number\n }) => string | undefined\n\n /** Per-cell agent dispatch deadline, applied to baseline, candidate, and\n * held-out campaigns. Default 600_000 ms. Set 0 to disable. */\n dispatchTimeoutMs?: number\n\n /** Streaming hook — fires on baseline + each generation + gate decision.\n * Consumer routes events wherever (UI, dashboard, logs). */\n onProgress?: (event: SelfImproveProgressEvent) => void\n\n /** Auto-promotion behavior on a ship decision. Default `'none'` — we\n * return the winner; you ship it however you ship. `'pr'` opens a\n * GitHub PR via `openAutoPr`; requires `ghOwner` + `ghRepo`. */\n autoOnPromote?: 'pr' | 'none'\n ghOwner?: string\n ghRepo?: string\n\n /**\n * Opt-in: ship eval-run events to a hosted orchestrator (ours, your\n * self-hosted one, or any compatible implementation of the\n * `docs/hosted-ingest-spec.md` wire format). When set, the substrate\n * POSTs the final `EvalRunEvent` to `${endpoint}/v1/ingest/eval-runs`\n * after the loop completes. Failures are logged but do not fail the\n * loop — local result is always returned.\n *\n * For our orchestrator: `{ endpoint: 'https://orchestrator.tangle.tools/v1', apiKey, tenantId }`.\n *\n * For your self-hosted: any URL serving the wire format. See\n * `examples/hosted-ingest-server/` for the reference receiver.\n */\n hostedTenant?: HostedTenant\n\n /** Free-form labels attached to the hosted event (env, branch, model id,\n * etc.). Ignored when `hostedTenant` is unset. */\n hostedLabels?: Record<string, string>\n\n /** Capture every search artifact and judge score to this store.\n * The store is output only and is never exposed to candidate generation.\n * Pass `'off'` to disable. Default: off. */\n labeledStore?: LabeledScenarioStore | 'off'\n\n /** Capture-source tag for `labeledStore`. Default `'eval-run'`. */\n captureSource?: 'production-trace' | 'eval-run' | 'manual' | 'red-team' | 'synthetic'\n\n /**\n * Per-cell backend-integrity expectation — the fail-loud guard. A cell that\n * produced an artifact but reported `costUsd === 0` AND zero tokens is a\n * stub. Modes: `'assert'` throws on the first such cell, `'warn'` logs it,\n * `'off'` skips the check (offline/replay). Default `'assert'` — `selfImprove`\n * is the real-run path, so a stub fails loud rather than scoring a clean 0.\n */\n expectUsage?: 'assert' | 'warn' | 'off'\n\n /**\n * Per-generation findings producer. Runs once on the baseline campaign (as\n * `generation: -1`) before generation 0 proposes — so single-generation runs\n * propose with trace context — and again after each generation is scored;\n * whatever it returns REPLACES the proposer's `findings` for the next\n * `propose()`. Plug a trace-analyst registry / HALO here. When absent,\n * findings stay `opts.findings`.\n */\n analyzeGeneration?: RunOptimizationOptions<TScenario, TArtifact>['analyzeGeneration']\n\n /** Static findings forwarded to the proposer's `propose()` as `ctx.findings`\n * (a findings-grounded proposer consumes them). Default: none. */\n findings?: ProposalFinding[]\n\n /** Override how the WINNER is selected among coverage-complete candidates.\n * Defaults to the scalar mean composite (historical behavior). A binary-with-\n * replicates consumer whose ship-gate counts an instance resolved only when\n * every replicate resolved passes a fail-closed lexicographic key here so that\n * winner-selection and the ship-gate rank on the identical metric and cannot\n * invert. See `RunOptimizationOptions.selectionRankKey`. */\n selectionRankKey?: RunOptimizationOptions<TScenario, TArtifact>['selectionRankKey']\n}\n\nexport interface SelfImproveResult<TScenario extends Scenario, TArtifact> {\n /** Composite mean across all scenarios, baseline run. When\n * `budget.holdout === 'deferred'` this is measured on the improvement\n * (search) split — no holdout campaign ran. */\n baseline: {\n compositeMean: number\n perScenario: Record<string, number>\n }\n /** Composite mean on the held-out set, winner run. When\n * `budget.holdout === 'deferred'` this is the winner's improvement-set\n * (search) measurement — no holdout campaign ran. */\n winner: {\n compositeMean: number\n perScenario: Record<string, number>\n surface: MutableSurface\n /** Proposer label for the promoted change. Absent ⇒ winner == baseline or\n * a bare-surface mutator. */\n label?: string\n /** Proposer rationale — the \"because Z\" that motivated the promoted change.\n * Threaded from the proposer's `ProposedCandidate` through the loop.\n * Absent ⇒ winner == baseline. */\n rationale?: string\n }\n /** `winner.compositeMean - baselineOnHoldout.compositeMean`. Positive\n * means the gate observed improvement. Absent iff\n * `budget.holdout === 'deferred'` — no held-out measurement ran, so there\n * is no lift to report (never a fabricated 0). */\n lift?: number\n /** The explicit baseline→winner unified diff. Always present (empty string\n * when winner == baseline). */\n diff: string\n /** Durable, queryable provenance record: candidate→cell→gate→promote chain +\n * rationale + diff + backend provenance. The artifact the hosted ingest\n * path stores; the +lift RECOMPUTES from `record.heldOutLift`. */\n provenance: LoopProvenanceRecord\n /** `defaultProductionGate.decide()` result. */\n gateDecision: 'ship' | 'hold' | 'need_more_work' | 'model_ceiling' | 'arch_ceiling'\n /** Number of generations actually explored (may be less than the\n * budget if the proposer gave up early). */\n generationsExplored: number\n /** Wall-clock total. */\n durationMs: number\n /** Total newly observed cost across the full run. */\n totalCostUsd: number\n /** Canonical run-wide spend summary. */\n cost: CostLedgerSummary\n /** Run-wide receipts across proposal, search, holdout, judging, analysis,\n * and promotion work, with phase and actor attribution. */\n receipts: CostReceipt[]\n /** Exact external method and source identity, when `method` was used. */\n optimization?: {\n name: string\n cost: OptimizationMethodResult['cost']\n durationMs?: number\n provenance?: OptimizationMethodProvenance\n }\n /**\n * Rigor packet: distributional summary, paired-bootstrap lift CI,\n * judge stats, contamination check, recommendations. Wired through\n * `analyzeRuns()` on the baseline + winner cells of the campaign.\n * Hosted-tier dashboards render this as the v3-vs-v4 decision view.\n */\n insight: InsightReport\n /** Minimum-detectable-lift analysis from the baseline holdout cells: could this\n * budget have shipped ANY plausible effect? Absent when the baseline produced\n * fewer than 3 scored holdout cells. See `powerPreflight` for the standalone\n * pre-run version (run `gate: 'none'` first, budget the real search after). */\n power?: PowerPreflight\n /**\n * Raw substrate result for advanced inspection — full per-generation\n * candidates, full campaign artifacts, all judge scores. Useful for\n * debugging or reporting beyond the summary.\n */\n raw: RunImprovementLoopResult<TArtifact, TScenario>\n}\n\n/** Failed self-improvement run with an immutable receipt snapshot. */\nexport class SelfImproveRunError extends Error {\n readonly cost: CostLedgerSummary\n readonly receipts: CostReceipt[]\n\n constructor(cause: unknown, ledger: CostLedgerHandle) {\n const original = cause instanceof Error ? cause : new Error(String(cause))\n super(original.message, { cause: original })\n this.name = 'SelfImproveRunError'\n this.cost = ledger.summary()\n this.receipts = ledger.list()\n }\n}\n\nfunction assertSelfImproveSearchMode<TScenario extends Scenario, TArtifact>(\n opts: SelfImproveOptions<TScenario, TArtifact>,\n): void {\n if (opts.method && opts.proposer) {\n throw new Error('selfImprove: method and proposer are mutually exclusive')\n }\n if (!opts.method) {\n if (opts.selectionScenarios !== undefined) {\n throw new Error('selfImprove: selectionScenarios requires method')\n }\n return\n }\n if (\n typeof opts.method.name !== 'string' ||\n !opts.method.name.trim() ||\n opts.method.name.trim() !== opts.method.name ||\n typeof opts.method.optimize !== 'function'\n ) {\n throw new Error('selfImprove: method must have a trimmed name and optimize(input)')\n }\n const budget = opts.budget\n if (budget?.generations !== undefined && budget.generations !== 1) {\n throw new Error('selfImprove: method owns its rounds; budget.generations must be 1 when set')\n }\n if (budget?.populationSize !== undefined && budget.populationSize !== 1) {\n throw new Error(\n 'selfImprove: method owns its candidates; budget.populationSize must be 1 when set',\n )\n }\n if (\n budget?.candidateConcurrency !== undefined ||\n budget?.maxImprovementShots !== undefined ||\n opts.analyzeGeneration !== undefined ||\n opts.findings !== undefined\n ) {\n throw new Error(\n 'selfImprove: candidateConcurrency, maxImprovementShots, analyzeGeneration, and findings apply only to proposer mode',\n )\n }\n}\n\nfunction splitMethodPartitions<TScenario extends Scenario>(\n searchScenarios: TScenario[],\n explicitSelection: TScenario[] | undefined,\n fraction: number,\n): { train: TScenario[]; selection: TScenario[] } {\n if (!Number.isFinite(fraction) || fraction <= 0 || fraction >= 1) {\n throw new Error('selfImprove: budget.selectionFraction must be in (0, 1)')\n }\n const byId = new Map<string, TScenario>()\n for (const scenario of searchScenarios) {\n if (byId.has(scenario.id)) {\n throw new Error(`selfImprove: duplicate scenario id '${scenario.id}'`)\n }\n byId.set(scenario.id, scenario)\n }\n if (explicitSelection) {\n if (explicitSelection.length === 0) {\n throw new Error('selfImprove: selectionScenarios must not be empty')\n }\n const selectionIds = new Set<string>()\n for (const scenario of explicitSelection) {\n if (!byId.has(scenario.id)) {\n throw new Error(\n `selfImprove: selection scenario '${scenario.id}' is absent from the non-final cases`,\n )\n }\n if (selectionIds.has(scenario.id)) {\n throw new Error(`selfImprove: duplicate selection scenario id '${scenario.id}'`)\n }\n selectionIds.add(scenario.id)\n }\n const train = searchScenarios.filter((scenario) => !selectionIds.has(scenario.id))\n if (train.length === 0) {\n throw new Error('selfImprove: method train split is empty')\n }\n return {\n train,\n selection: explicitSelection.map((scenario) => byId.get(scenario.id)!),\n }\n }\n if (searchScenarios.length < 2) {\n throw new Error('selfImprove: method requires at least two non-final scenarios')\n }\n const sorted = [...searchScenarios].sort(\n (a, b) => stableScenarioHash(a.id) - stableScenarioHash(b.id),\n )\n const count = Math.max(1, Math.min(sorted.length - 1, Math.round(sorted.length * fraction)))\n return {\n selection: sorted.slice(0, count),\n train: sorted.slice(count),\n }\n}\n\nfunction safeRunComponent(value: string): string {\n return value.replace(/[^a-zA-Z0-9._-]/g, '_')\n}\n\nfunction stableScenarioHash(value: string): number {\n let hash = 2166136261 >>> 0\n for (let index = 0; index < value.length; index++) {\n hash ^= value.charCodeAt(index)\n hash = Math.imul(hash, 16777619) >>> 0\n }\n return hash\n}\n\n/**\n * Deterministic train/holdout split by a stable hash of `scenario.id`,\n * so the same scenario set always splits the same way across runs.\n */\nfunction splitTrainHoldout<TScenario extends Scenario>(\n scenarios: TScenario[],\n fraction: number,\n): { train: TScenario[]; holdout: TScenario[] } {\n const sorted = [...scenarios].sort((a, b) => stableScenarioHash(a.id) - stableScenarioHash(b.id))\n const nHoldout = Math.max(1, Math.min(sorted.length - 1, Math.round(sorted.length * fraction)))\n return {\n holdout: sorted.slice(0, nHoldout),\n train: sorted.slice(nHoldout),\n }\n}\n\nfunction meanComposite(byScenario: Record<string, { meanComposite: number }>): {\n compositeMean: number\n perScenario: Record<string, number>\n} {\n const perScenario: Record<string, number> = {}\n const values: number[] = []\n for (const [id, agg] of Object.entries(byScenario)) {\n perScenario[id] = agg.meanComposite\n values.push(agg.meanComposite)\n }\n return {\n compositeMean: values.length === 0 ? 0 : values.reduce((s, v) => s + v, 0) / values.length,\n perScenario,\n }\n}\n\n/**\n * Latest search campaign measured for the winner surface; the baseline search\n * campaign when the winner IS the baseline. Used by the deferred-holdout\n * summary, where no holdout campaign exists to summarize.\n */\nfunction winnerSearchCampaign<TScenario extends Scenario, TArtifact>(\n result: RunImprovementLoopResult<TArtifact, TScenario>,\n): RunImprovementLoopResult<TArtifact, TScenario>['baselineCampaign'] {\n for (let i = result.generations.length - 1; i >= 0; i--) {\n const measured = result.generations[i]?.surfaces.find(\n (s) => s.surfaceHash === result.winnerSurfaceHash,\n )\n if (measured) return measured.campaign\n }\n return result.baselineCampaign\n}\n\n/**\n * One-shot self-improvement loop. See module docstring for defaults +\n * extension points.\n *\n * @example Minimum:\n *\n * const result = await selfImprove({\n * agent: (surface, scenario, ctx) => myAgent(surface, scenario, ctx.signal),\n * scenarios,\n * judge,\n * baselineSurface: DEFAULT_PROMPT,\n * proposer,\n * })\n * console.log(`lift: ${result.lift.toFixed(3)} (${result.gateDecision})`)\n *\n * @example Distributed (workers in three regions):\n *\n * await selfImprove({\n * agent: httpDispatch({ resolveUrl: ({ placement }) => REGION_URLS[placement!] }),\n * scenarios,\n * judge,\n * baselineSurface: DEFAULT_PROMPT,\n * cellPlacement: ({ scenario }) => scenario.region,\n * budget: { maxConcurrency: 12 },\n * })\n */\nexport async function selfImprove<TScenario extends Scenario, TArtifact>(\n opts: SelfImproveOptions<TScenario, TArtifact>,\n): Promise<SelfImproveResult<TScenario, TArtifact>> {\n const startedAt = Date.now()\n const requestedRunDir =\n opts.runDir ??\n (opts.method ? `.agent-eval/runs/self-improve-${startedAt}` : `mem://selfImprove-${startedAt}`)\n const runDir = resolveRunDir(requestedRunDir)\n const storage =\n opts.storage ?? (runDir.startsWith('mem://') ? inMemoryCampaignStorage() : fsCampaignStorage())\n const costLedger = createRunCostLedger({\n storage,\n runDir,\n costCeilingUsd: opts.budget?.dollars,\n })\n try {\n return await runSelfImprove(opts, costLedger, startedAt, runDir, storage)\n } catch (error) {\n throw new SelfImproveRunError(error, costLedger)\n }\n}\n\nasync function runSelfImprove<TScenario extends Scenario, TArtifact>(\n opts: SelfImproveOptions<TScenario, TArtifact>,\n costLedger: CostLedgerHandle,\n startedAt: number,\n runDir: string,\n storage: CampaignStorage,\n): Promise<SelfImproveResult<TScenario, TArtifact>> {\n const budget = opts.budget ?? {}\n assertSelfImproveSearchMode(opts)\n const generations = opts.method ? 1 : (budget.generations ?? 3)\n const populationSize = opts.method ? 1 : (budget.populationSize ?? 2)\n const maxConcurrency = budget.maxConcurrency ?? 2\n const holdoutFraction = budget.holdoutFraction ?? 0.25\n const holdoutMode = budget.holdout ?? 'measured'\n const holdoutDeferred = holdoutMode === 'deferred'\n const expectUsage = opts.expectUsage ?? 'assert'\n\n // Deferred holdout without an explicitly reserved set trains on EVERYTHING:\n // there is no held-out measurement in this run, so carving out a fraction\n // would waste scenarios. An explicit `holdoutScenarios` set stays reserved\n // (excluded from training) even when deferred, for the later measured run.\n const explicitHoldout = budget.holdoutScenarios\n const { train, holdout } = explicitHoldout\n ? {\n train: opts.scenarios.filter((s) => !explicitHoldout.some((h) => h.id === s.id)),\n holdout: explicitHoldout as TScenario[],\n }\n : holdoutDeferred\n ? { train: opts.scenarios, holdout: [] as TScenario[] }\n : splitTrainHoldout(opts.scenarios, holdoutFraction)\n\n if (train.length === 0) {\n throw new Error(\n 'selfImprove: train split is empty. Reduce holdoutFraction or pass more scenarios.',\n )\n }\n if (holdout.length === 0 && !holdoutDeferred) {\n throw new Error('selfImprove: holdout split is empty. Pass more scenarios.')\n }\n\n if (generations > 0 && !opts.proposer && !opts.method) {\n throw new Error(\n 'selfImprove: method or proposer is required when budget.generations is greater than zero',\n )\n }\n let optimizationResult: OptimizationMethodResult | undefined\n const methodPartitions = opts.method\n ? splitMethodPartitions(train, opts.selectionScenarios, budget.selectionFraction ?? 0.25)\n : undefined\n const proposer: SurfaceProposer<ProposalFinding> = opts.method\n ? {\n kind: `method:${opts.method.name}`,\n propose: async (context) => {\n if (context.generation > 0) return []\n const result = await opts.method!.optimize(\n Object.freeze({\n baselineSurface: structuredClone(context.currentSurface),\n trainScenarios: Object.freeze(\n methodPartitions!.train.map((scenario) => structuredClone(scenario)),\n ),\n selectionScenarios: Object.freeze(\n methodPartitions!.selection.map((scenario) => structuredClone(scenario)),\n ),\n dispatchWithSurface: opts.agent,\n judges: Object.freeze([opts.judge]),\n runDir: `${runDir}/optimization/${safeRunComponent(opts.method!.name)}`,\n seed: 42,\n runOptions: Object.freeze({\n storage,\n maxConcurrency,\n reps: budget.reps,\n dispatchTimeoutMs: opts.dispatchTimeoutMs,\n expectUsage,\n costCeiling: budget.dollars,\n }),\n costLedger,\n }),\n )\n assertOptimizationResult(opts.method!.name, result)\n optimizationResult = structuredClone(result)\n return [\n {\n surface: structuredClone(result.winnerSurface),\n label: opts.method!.name,\n rationale: `${opts.method!.name} selected this surface without final cases.`,\n },\n ]\n },\n }\n : (opts.proposer ?? {\n kind: 'baseline-only',\n propose: async () => [],\n })\n\n const gate: Gate<TArtifact, TScenario> =\n opts.gate ??\n defaultProductionGate<TArtifact, TScenario>({\n holdoutScenarios: holdout,\n deltaThreshold: 0.05,\n })\n\n if (opts.onProgress) {\n opts.onProgress({ kind: 'baseline.started', scenarios: opts.scenarios.length })\n }\n\n const result = await runImprovementLoop<TScenario, TArtifact>({\n scenarios: train,\n baselineSurface: opts.baselineSurface,\n premeasuredBaseline: opts.premeasuredBaseline,\n dispatchWithSurface: opts.agent,\n proposer,\n judges: [opts.judge],\n populationSize,\n maxGenerations: generations,\n candidateConcurrency: budget.candidateConcurrency,\n reps: budget.reps,\n maxImprovementShots: budget.maxImprovementShots,\n holdoutScenarios: holdout,\n holdout: holdoutMode,\n gate,\n neutralize: opts.neutralize,\n autoOnPromote: opts.autoOnPromote ?? 'none',\n ghOwner: opts.ghOwner,\n ghRepo: opts.ghRepo,\n storage,\n runDir,\n maxConcurrency,\n cellPlacement: opts.cellPlacement,\n dispatchTimeoutMs: opts.dispatchTimeoutMs,\n costLedger,\n expectUsage,\n labeledStore: opts.labeledStore,\n captureSource: opts.captureSource,\n analyzeGeneration: opts.analyzeGeneration,\n findings: opts.findings,\n selectionRankKey: opts.selectionRankKey,\n })\n\n // Deferred holdout ran zero holdout cells, so the summary stats come from\n // the improvement-set (search) campaigns — labeled as such on the result\n // type — and `lift` is omitted rather than fabricated from empty campaigns.\n const reportSplit: RunSplitTag = holdoutDeferred ? 'search' : 'holdout'\n const reportBaselineCampaign = holdoutDeferred\n ? result.baselineCampaign\n : result.baselineOnHoldout\n const reportWinnerCampaign = holdoutDeferred\n ? winnerSearchCampaign(result)\n : result.winnerOnHoldout\n const baseline = meanComposite(reportBaselineCampaign.aggregates.byScenario)\n const winnerStats = meanComposite(reportWinnerCampaign.aggregates.byScenario)\n\n // Power analysis from the baseline holdout cells — the number that says whether\n // this budget could ship ANY effect. Attached to every result; loud when the\n // search was structurally unable to promote (that spend should not repeat).\n let power: PowerPreflight | undefined\n const baselineHoldoutComposites = result.baselineOnHoldout.cells\n .filter((cell) => !cell.error)\n .map((cell) => {\n const scores = Object.values(cell.judgeScores)\n return scores.length === 0\n ? Number.NaN\n : scores.reduce((sum, s) => sum + s.composite, 0) / scores.length\n })\n .filter((v) => Number.isFinite(v))\n if (baselineHoldoutComposites.length >= 3) {\n // selfImprove's holdout is scored by the SAME judge as the gate — the\n // shared-channel case by construction (S1c): flag it so the MDE reads as a\n // lower bound and nobody buys reps expecting them to fix judge bias.\n power = powerPreflight({\n baselineComposites: baselineHoldoutComposites,\n sharedScorerChannel: true,\n })\n if (opts.onProgress) {\n opts.onProgress({\n kind: 'power.estimated',\n n: power.n,\n sd: power.sd,\n mde: power.mde,\n underpowered: power.underpowered,\n })\n }\n if (power.underpowered && generations > 0) {\n console.warn(`[selfImprove] ${power.recommendation}`)\n }\n }\n\n if (opts.onProgress) {\n opts.onProgress({\n kind: 'baseline.completed',\n compositeMean: baseline.compositeMean,\n durationMs: Date.now() - startedAt,\n })\n opts.onProgress({\n kind: 'gate.decided',\n decision: result.gateResult.decision,\n // Deferred holdout has no held-out measurement: in that mode the summary\n // stats are search-split numbers, and emitting their delta as `lift`\n // would misreport a train-split delta as a held-out one. Omit instead.\n ...(holdoutDeferred ? {} : { lift: winnerStats.compositeMean - baseline.compositeMean }),\n })\n }\n\n const cost = result.cost\n const totalCost = cost.totalCostUsd\n\n // Rigor packet: feed baseline + winner cells through analyzeRuns().\n // The two candidates (`baseline` / `winner`) give the lift section a\n // clean paired comparison; per-judge / per-dimension / cost-quality\n // sections populate from the cells' judgeScores.\n const insight = await analyzeRuns({\n runs: [\n ...cellsToRunRecords(\n reportBaselineCampaign.cells,\n 'baseline',\n runDir,\n opts.baselineSurface,\n reportSplit,\n opts.model,\n ),\n ...cellsToRunRecords(\n reportWinnerCampaign.cells,\n 'winner',\n runDir,\n result.winnerSurface,\n reportSplit,\n opts.model,\n ),\n ],\n baselineCandidateId: 'baseline',\n candidateCandidateId: 'winner',\n })\n\n // ── Durable provenance: candidate→cell→gate→promote chain + rationale +\n // diff + backend provenance. Always emitted; the +lift recomputes from it.\n const durationMs = Date.now() - startedAt\n const { record: provenance } = await emitLoopProvenance<TArtifact, TScenario>({\n ...loopProvenanceArgsFromResult({\n runId: `${runDir}#${startedAt}`,\n runDir,\n timestamp: new Date(startedAt).toISOString(),\n baselineSurface: opts.baselineSurface,\n result,\n costReceipts: costLedger.list(),\n totalCostUsd: totalCost,\n totalDurationMs: durationMs,\n }),\n ...(optimizationResult\n ? {\n optimizationMethod: {\n name: opts.method!.name,\n cost: structuredClone(optimizationResult.cost),\n ...(optimizationResult.durationMs === undefined\n ? {}\n : { durationMs: optimizationResult.durationMs }),\n ...(optimizationResult.provenance === undefined\n ? {}\n : { provenance: structuredClone(optimizationResult.provenance) }),\n },\n }\n : {}),\n storage,\n hostedClient: opts.hostedTenant ? createHostedClient(opts.hostedTenant) : undefined,\n })\n if (opts.onProvenance) opts.onProvenance(provenance)\n\n const summary: SelfImproveResult<TScenario, TArtifact> = {\n baseline,\n winner: {\n ...winnerStats,\n surface: result.winnerSurface,\n ...(result.winnerLabel ? { label: result.winnerLabel } : {}),\n ...(result.winnerRationale ? { rationale: result.winnerRationale } : {}),\n },\n ...(holdoutDeferred ? {} : { lift: winnerStats.compositeMean - baseline.compositeMean }),\n diff: result.promotedDiff,\n provenance,\n gateDecision: result.gateResult.decision,\n generationsExplored: result.generations.length,\n durationMs,\n totalCostUsd: totalCost,\n cost,\n receipts: costLedger.list(),\n ...(optimizationResult\n ? {\n optimization: {\n name: opts.method!.name,\n cost: structuredClone(optimizationResult.cost),\n ...(optimizationResult.durationMs === undefined\n ? {}\n : { durationMs: optimizationResult.durationMs }),\n ...(optimizationResult.provenance === undefined\n ? {}\n : { provenance: structuredClone(optimizationResult.provenance) }),\n },\n }\n : {}),\n insight,\n ...(power ? { power } : {}),\n raw: result,\n }\n\n // Opt-in hosted ingest. Failures are logged but never fail the loop: the\n // local result is always returned.\n if (opts.hostedTenant) {\n try {\n await shipEvalRunToHosted(opts.hostedTenant, opts, summary, result, runDir)\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err)\n // eslint-disable-next-line no-console -- intentional: hosted-ingest is best-effort\n console.warn(`[agent-eval] hosted ingest failed (continuing): ${msg}`)\n }\n }\n\n return summary\n}\n\nasync function shipEvalRunToHosted<TScenario extends Scenario, TArtifact>(\n tenant: HostedTenant,\n opts: SelfImproveOptions<TScenario, TArtifact>,\n summary: SelfImproveResult<TScenario, TArtifact>,\n raw: RunImprovementLoopResult<TArtifact, TScenario>,\n runDir: string,\n): Promise<void> {\n const client = createHostedClient(tenant)\n\n function snapshotFromCampaign(\n index: number,\n surface: MutableSurface,\n campaign: RunImprovementLoopResult<TArtifact, TScenario>['baselineCampaign'],\n durationMs: number,\n ): EvalRunGenerationSnapshot {\n const cells: EvalRunCellScore[] = campaign.cells.map((cell) => {\n const execution = campaignCellExecutionEvidence(cell)\n return {\n scenarioId: cell.scenarioId,\n rep: cell.rep,\n compositeMean: campaignCellTaskScore(cell) ?? null,\n dimensions: campaignCellJudgeDimensions(cell),\n terminalOutcome: execution.terminalOutcome,\n executionErrorCount: execution.executionErrorCount ?? null,\n errorMessage: cell.error ?? undefined,\n }\n })\n const scoredCells = cells.flatMap((cell) =>\n cell.compositeMean === null ? [] : [cell.compositeMean],\n )\n const compositeMean =\n scoredCells.length === 0\n ? null\n : scoredCells.reduce((sum, score) => sum + score, 0) / scoredCells.length\n return {\n index,\n surfaceHash: surfaceHash(surface),\n surface,\n cells,\n compositeMean,\n costUsd: campaign.aggregates.cost.totalCostUsd,\n durationMs,\n }\n }\n\n const generations: EvalRunGenerationSnapshot[] = []\n // Baseline as generation 0.\n generations.push(snapshotFromCampaign(0, opts.baselineSurface, raw.baselineCampaign, 0))\n // Improvement generations as 1..N. Substrate stores per-surface campaigns\n // per generation — we summarize the WINNING surface per generation here.\n for (const gen of raw.generations) {\n const winner = gen.surfaces.reduce(\n (best, s) =>\n s.campaign.aggregates.cellsExecuted > 0 &&\n (best === undefined || averageComposite(s.campaign) > averageComposite(best.campaign))\n ? s\n : best,\n gen.surfaces[0],\n )\n if (!winner) continue\n generations.push(\n snapshotFromCampaign(gen.record.generationIndex + 1, winner.surface, winner.campaign, 0),\n )\n }\n\n const event: EvalRunEvent = {\n runId: `${runDir}#${Date.now()}`,\n runDir,\n timestamp: new Date().toISOString(),\n status: 'finished',\n labels: opts.hostedLabels ?? {},\n baseline: generations[0],\n generations,\n gateDecision: summary.gateDecision,\n holdoutLift: summary.lift,\n totalCostUsd: summary.totalCostUsd,\n totalDurationMs: summary.durationMs,\n insightReport: summary.insight,\n }\n\n await client.ingestEvalRun(event)\n}\n\nfunction averageComposite(\n campaign: RunImprovementLoopResult<unknown, Scenario>['baselineCampaign'],\n): number {\n const aggs = Object.values(campaign.aggregates.byScenario)\n return aggs.length === 0 ? 0 : aggs.reduce((s, a) => s + a.meanComposite, 0) / aggs.length\n}\n\nfunction hashString(s: string): string {\n let h = 2166136261 >>> 0\n for (let i = 0; i < s.length; i++) {\n h ^= s.charCodeAt(i)\n h = Math.imul(h, 16777619) >>> 0\n }\n return h.toString(16).padStart(8, '0')\n}\n\n/**\n * Adapt campaign cells into the `RunRecord` shape `analyzeRuns()` consumes.\n * Each cell becomes one run; `candidateId` is the caller-supplied label so\n * baseline + winner pair cleanly on `(experimentId, scenarioId, seed)`.\n *\n * `promptHash` is the REAL sha256 content hash of the surface this cell ran\n * (baseline vs winner are byte-distinguishable + byte-identical-verifiable);\n * `configHash` is the sha256 of the candidate label so the two candidates'\n * config rows differ. Both were previously the literal `'sha256:cell'`, which\n * made baseline and winner indistinguishable in every downstream record.\n */\nfunction cellsToRunRecords<TArtifact>(\n cells: ReadonlyArray<CampaignCellResult<TArtifact>>,\n candidateId: 'baseline' | 'winner',\n runId: string,\n surface: MutableSurface,\n splitTag: RunSplitTag,\n fallbackModel?: string,\n): RunRecord[] {\n const promptHash = surfaceContentHash(surface)\n const configHash = surfaceContentHash(candidateId)\n return cells.map((cell) => {\n const receiptModels = cell.resolvedModels ?? (cell.resolvedModel ? [cell.resolvedModel] : [])\n if (receiptModels.length > 1) {\n throw new ValidationError(\n `selfImprove cell ${cell.cellId} used multiple agent models: ${receiptModels.join(', ')}`,\n )\n }\n const model = receiptModels[0] ?? fallbackModel\n if (!model) {\n throw new ValidationError(\n `selfImprove.model is required when cell ${cell.cellId} has no paid-call model receipt`,\n )\n }\n if (!modelHasSnapshot(model)) {\n throw new ValidationError(\n `selfImprove model \"${model}\" lacks a snapshot version for cell ${cell.cellId}`,\n )\n }\n return campaignCellToRunRecord(cell, {\n runId: `${runId}::${candidateId}::${cell.cellId}`,\n experimentId: runId,\n candidateId,\n // scenarioId is explicit; seed keeps repeated runs distinct.\n seed:\n cell.rep * 1_000_000 +\n hashString(cell.scenarioId)\n .slice(0, 6)\n .split('')\n .reduce((a, c) => (a * 31 + c.charCodeAt(0)) >>> 0, 0),\n model,\n promptHash,\n configHash,\n commitSha: 'cell',\n splitTag,\n })\n })\n}\n","import type { RunEvalOptions } from '../campaign/presets/run-eval'\nimport { runEval } from '../campaign/presets/run-eval'\nimport { inMemoryCampaignStorage } from '../campaign/storage'\nimport type {\n CampaignResult,\n DispatchContext,\n JudgeConfig,\n MutableSurface,\n Scenario,\n} from '../campaign/types'\nimport type { HostedTenant } from '../hosted/client'\nimport {\n type SelfImproveBudget,\n type SelfImproveOptions,\n type SelfImproveResult,\n selfImprove,\n} from './self-improve'\n\nexport type AgentEvalAgent<TScenario extends Scenario, TArtifact> = (\n surface: MutableSurface,\n scenario: TScenario,\n ctx: DispatchContext,\n) => Promise<TArtifact>\n\nexport type DefineAgentEvalOptions<TScenario extends Scenario, TArtifact> = SelfImproveOptions<\n TScenario,\n TArtifact\n>\n\nexport interface AgentEvalEvaluateOptions<TScenario extends Scenario, TArtifact>\n extends Omit<\n RunEvalOptions<TScenario, TArtifact>,\n 'dispatch' | 'judges' | 'runDir' | 'scenarios'\n > {\n /** Scenario set to evaluate. Defaults to the scenarios passed to `defineAgentEval`. */\n scenarios?: TScenario[]\n /** Surface to evaluate. Defaults to the baseline surface passed to `defineAgentEval`. */\n surface?: MutableSurface\n /** Agent to evaluate. Defaults to the agent passed to `defineAgentEval`. */\n agent?: AgentEvalAgent<TScenario, TArtifact>\n /** Single judge override. Ignored when `judges` is set. */\n judge?: JudgeConfig<TArtifact, TScenario>\n /** Full judge list override. Defaults to the single judge passed to `defineAgentEval`. */\n judges?: JudgeConfig<TArtifact, TScenario>[]\n /** Logical or filesystem run directory. Defaults to an in-memory run. */\n runDir?: string\n}\n\nexport type AgentEvalImproveOptions<TScenario extends Scenario, TArtifact> = Omit<\n Partial<SelfImproveOptions<TScenario, TArtifact>>,\n 'budget' | 'hostedTenant'\n> & {\n budget?: Partial<SelfImproveBudget>\n hostedTenant?: Partial<HostedTenant>\n}\n\nexport interface DefinedAgentEval<TScenario extends Scenario, TArtifact> {\n /** The default scenarios used by `evaluate()` and `improve()`. */\n readonly scenarios: readonly TScenario[]\n /** The default baseline surface used by `evaluate()` and `improve()`. */\n readonly baselineSurface: MutableSurface\n /**\n * Run one scored evaluation. Use this for a baseline score or to score one\n * candidate surface without running an improvement loop.\n */\n evaluate(\n opts?: AgentEvalEvaluateOptions<TScenario, TArtifact>,\n ): Promise<CampaignResult<TArtifact, TScenario>>\n /**\n * Run the closed improvement loop. Per-call overrides replace the definition\n * except for nested config objects (`budget`, `hostedTenant`), which\n * are merged field-by-field so callers can override one knob without\n * repeating secrets or budget defaults.\n */\n improve(\n opts?: AgentEvalImproveOptions<TScenario, TArtifact>,\n ): Promise<SelfImproveResult<TScenario, TArtifact>>\n}\n\n/**\n * Define an agent eval once, then either score a surface with `evaluate()` or\n * run the closed loop with `improve()`.\n *\n * This is a DX wrapper only: it delegates to `runEval()` and `selfImprove()` and\n * returns their native result shapes.\n */\nexport function defineAgentEval<TScenario extends Scenario, TArtifact>(\n defaults: DefineAgentEvalOptions<TScenario, TArtifact>,\n): DefinedAgentEval<TScenario, TArtifact> {\n const defaultEvaluateOptions = evaluateDefaults(defaults)\n\n return {\n scenarios: defaults.scenarios,\n baselineSurface: defaults.baselineSurface,\n\n async evaluate(opts = {}) {\n const { agent, judge, judges, runDir, scenarios, surface, ...campaignOpts } = opts\n const selectedAgent = agent ?? defaults.agent\n const selectedSurface = surface ?? defaults.baselineSurface\n const selectedRunDir = runDir ?? defaults.runDir ?? `mem://defineAgentEval-${Date.now()}`\n const selectedStorage =\n campaignOpts.storage ??\n defaultEvaluateOptions.storage ??\n (selectedRunDir.startsWith('mem://') ? inMemoryCampaignStorage() : undefined)\n const evalOptions: RunEvalOptions<TScenario, TArtifact> = {\n ...defaultEvaluateOptions,\n ...campaignOpts,\n ...(selectedStorage ? { storage: selectedStorage } : {}),\n runDir: selectedRunDir,\n scenarios: scenarios ?? defaults.scenarios,\n dispatch: (scenario, ctx) => selectedAgent(selectedSurface, scenario, ctx),\n judges: evaluateJudges(judges, judge ?? defaults.judge),\n }\n if (evalOptions.reps !== undefined)\n evalOptions.reps = requirePositiveInteger(evalOptions.reps, 'reps')\n return runEval<TScenario, TArtifact>(evalOptions)\n },\n\n async improve(opts = {}) {\n const {\n budget: budgetOverride,\n hostedTenant: hostedTenantOverride,\n ...topLevelOverrides\n } = opts\n const merged = mergeDefined(defaults, topLevelOverrides)\n const budget = mergeBudget(defaults.budget, budgetOverride)\n const hostedTenant = mergeHostedTenant(defaults.hostedTenant, hostedTenantOverride)\n return selfImprove<TScenario, TArtifact>({\n ...merged,\n ...(budget ? { budget } : {}),\n ...(hostedTenant ? { hostedTenant } : {}),\n })\n },\n }\n}\n\ntype SharedEvaluateDefaults<TScenario extends Scenario, TArtifact> = Omit<\n RunEvalOptions<TScenario, TArtifact>,\n 'dispatch' | 'judges' | 'runDir' | 'scenarios'\n>\n\nfunction evaluateDefaults<TScenario extends Scenario, TArtifact>(\n defaults: DefineAgentEvalOptions<TScenario, TArtifact>,\n): SharedEvaluateDefaults<TScenario, TArtifact> {\n const out: SharedEvaluateDefaults<TScenario, TArtifact> = {}\n if (defaults.storage) out.storage = defaults.storage\n if (defaults.labeledStore) out.labeledStore = defaults.labeledStore\n if (defaults.captureSource) out.captureSource = defaults.captureSource\n if (defaults.cellPlacement) out.cellPlacement = defaults.cellPlacement\n if (defaults.expectUsage) out.expectUsage = defaults.expectUsage\n if (defaults.budget?.dollars !== undefined) out.costCeiling = defaults.budget.dollars\n if (defaults.budget?.maxConcurrency !== undefined)\n out.maxConcurrency = defaults.budget.maxConcurrency\n if (defaults.budget?.reps !== undefined)\n out.reps = requirePositiveInteger(defaults.budget.reps, 'budget.reps')\n return out\n}\n\nfunction mergeBudget(\n defaults: SelfImproveBudget | undefined,\n overrides: Partial<SelfImproveBudget> | undefined,\n): SelfImproveBudget | undefined {\n const merged = mergeOptionalObject(defaults, overrides)\n if (merged?.reps !== undefined) merged.reps = requirePositiveInteger(merged.reps, 'budget.reps')\n return merged\n}\n\nfunction mergeHostedTenant(\n defaults: HostedTenant | undefined,\n overrides: Partial<HostedTenant> | undefined,\n): HostedTenant | undefined {\n const merged = mergeOptionalObject(defaults, overrides)\n if (!merged) return undefined\n if (!merged.endpoint?.trim() || !merged.apiKey?.trim() || !merged.tenantId?.trim()) {\n throw new Error(\n 'defineAgentEval.improve: hostedTenant requires endpoint, apiKey, and tenantId after merging defaults and overrides',\n )\n }\n return merged\n}\n\nfunction mergeDefined<T extends object>(defaults: T, overrides: Partial<T> | undefined): T {\n if (!overrides) return defaults\n const merged = { ...defaults } as Record<string, unknown>\n for (const [key, value] of Object.entries(overrides)) {\n if (value !== undefined) merged[key] = value\n }\n return merged as T\n}\n\nfunction mergeOptionalObject<T extends object>(\n defaults: T | undefined,\n overrides: Partial<T> | undefined,\n): T | undefined {\n if (!defaults && !overrides) return undefined\n return mergeDefined(defaults ?? ({} as T), overrides)\n}\n\nfunction evaluateJudges<TArtifact, TScenario extends Scenario>(\n judges: JudgeConfig<TArtifact, TScenario>[] | undefined,\n defaultJudge: JudgeConfig<TArtifact, TScenario>,\n): JudgeConfig<TArtifact, TScenario>[] {\n if (judges !== undefined) {\n if (judges.length === 0) {\n throw new Error('defineAgentEval.evaluate: judges must not be empty')\n }\n return judges\n }\n return [defaultJudge]\n}\n\nfunction requirePositiveInteger(value: number, field: string): number {\n if (!Number.isInteger(value) || value < 1) {\n throw new Error(`defineAgentEval: ${field} must be a positive integer`)\n }\n return value\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA4BA,SAAgB,cAAc,QAAgB,WAA4C;CACxF,MAAM,QAAsB,CAAC;CAC7B,KAAK,MAAM,KAAK,WAAW;EACzB,IAAI,CAAC,EAAE,QAAQ;EACf,IAAI,OAAO,SAAS,EAAE,MAAM,GAC1B,MAAM,KAAK;GAAE,YAAY,EAAE;GAAI,QAAQ,EAAE;GAAQ,UAAU,QAAQ,QAAQ,EAAE,MAAM;EAAE,CAAC;CAE1F;CACA,OAAO;AACT;AA8IA,SAAS,QAAQ,QAAgB,QAAwB;CACvD,MAAM,KAAK,OAAO,QAAQ,MAAM;CAChC,IAAI,KAAK,GAAG,OAAO;CACnB,MAAM,QAAQ,KAAK,IAAI,GAAG,KAAK,EAAE;CACjC,MAAM,MAAM,KAAK,IAAI,OAAO,QAAQ,KAAK,OAAO,SAAS,EAAE;CAC3D,QAAQ,QAAQ,IAAI,MAAM,MAAM,OAAO,MAAM,OAAO,GAAG,KAAK,MAAM,OAAO,SAAS,MAAM;AAC1F;;;;ACzDA,SAAgB,mBAAmB,MAAkD;CACnF,MAAM,OAAO,KAAK,KAAK,IAAI,iBAAiB;CAE5C,OAAO;EACL,WAAW,wBAAwB,MAFxB,KAAK,iBAAiB,EAEY;EAC7C,gBAAgB,wBAAwB,IAAI;CAC9C;AACF;;;;AAKA,SAAS,UAAU,IAAwC;CACzD,OAAO,CAAC,OAAO,SAAS,GAAG,EAA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AS,GACjD,IAAI,UAAU,KAAA,GAAW,OAAO,OAAO;CAEzC,OAAO;AACT;AAEA,SAAS,oBACP,UACA,WACe;CACf,IAAI,CAAC,YAAY,CAAC,WAAW,OAAO,KAAA;CACpC,OAAO,aAAa,YAAa,CAAC,GAAS,SAAS;AACtD;AAEA,SAAS,eACP,QACA,cACqC;CACrC,IAAI,WAAW,KAAA,GAAW;EACxB,IAAI,OAAO,WAAW,GACpB,MAAM,IAAI,MAAM,oDAAoD;EAEtE,OAAO;CACT;CACA,OAAO,CAAC,YAAY;AACtB;AAEA,SAAS,uBAAuB,OAAe,OAAuB;CACpE,IAAI,CAAC,OAAO,UAAU,KAAK,KAAK,QAAQ,GACtC,MAAM,IAAI,MAAM,oBAAoB,MAAM,4BAA4B;CAExE,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"define-agent-eval-CvZQW4u9.js","names":[],"sources":["../src/contamination-guard.ts","../src/contract/analyze-runs.ts","../src/contract/self-improve.ts","../src/contract/define-agent-eval.ts"],"sourcesContent":["/**\n * ContaminationGuard — ensures held-out scenarios don't leak into\n * training/prompt paths, and flags model memorization.\n *\n * Three probes:\n * 1. `checkCanaries(output, scenario)` — if the scenario carries a\n * canary token, it MUST NOT appear in the agent's output.\n * Canaries are strings that are statistically impossible to\n * reconstruct from the scenario description alone — so if they\n * echo back, the model memorized them.\n * 2. `canaryLeakView(store)` — cross-corpus view of every run whose\n * output contained a canary, with the offending scenario + run.\n * 3. `HoldoutAuditor` — wraps a Dataset and emits a structured error\n * on any code path that reads holdout scenarios but doesn't flag\n * `purpose: 'evaluation'`. Keeps engineers honest.\n */\n\nimport type { DatasetScenario } from './dataset'\nimport { llmSpans } from './trace/query'\nimport type { TraceStore } from './trace/store'\n\nexport interface CanaryLeak {\n scenarioId: string\n canary: string\n runId?: string\n evidence: string\n}\n\nexport function checkCanaries(output: string, scenarios: DatasetScenario[]): CanaryLeak[] {\n const leaks: CanaryLeak[] = []\n for (const s of scenarios) {\n if (!s.canary) continue\n if (output.includes(s.canary)) {\n leaks.push({ scenarioId: s.id, canary: s.canary, evidence: excerpt(output, s.canary) })\n }\n }\n return leaks\n}\n\n/**\n * Behavioral canary: tests the AGENT, not the eval grep.\n *\n * - `forbiddenPattern` PRESENT in output ⇒ AGENT EMITTED BAD CONTENT ⇒ LEAK.\n * - `forbiddenPattern` ABSENT from output ⇒ AGENT HELD THE LINE ⇒ PASS.\n *\n * Use when running known-bad-prompt scenarios against the agent under\n * test and you want to know if the agent misbehaved. The classical\n * {@link checkCanaries} / {@link import('./canary').runCanaries | runCanaries}\n * test whether the eval check fires when the bad output is forced\n * into the eval flow — different question, different answer.\n *\n * Pattern resolution order (first match wins):\n * 1. `scenario.forbiddenPattern` — if it parses as `/body/flags`,\n * treated as a regex; otherwise a literal substring.\n * 2. `scenario.canary` — literal substring fallback so the helper\n * works on existing scenario fixtures.\n *\n * Returns `null` when nothing forbidden was found OR the scenario\n * declared no pattern.\n */\nexport function checkBehavioralCanary(\n output: string,\n scenario: DatasetScenario,\n): CanaryLeak | null {\n const pattern = scenario.forbiddenPattern ?? scenario.canary\n if (!pattern) return null\n const hit = matchForbidden(output, pattern)\n if (!hit) return null\n return {\n scenarioId: scenario.id,\n canary: pattern,\n evidence: excerpt(output, hit),\n }\n}\n\n/**\n * Behavioral canary over many (scenario, output) pairs. Sibling to\n * {@link import('./canary').runCanaries | runCanaries} — same idea\n * (run-many → report) but the question being answered is \"did the\n * AGENT misbehave?\" rather than \"did the EVAL grep fire?\".\n *\n * Returns one `CanaryLeak` per pair where the agent's output\n * contained its scenario's `forbiddenPattern` (or `canary` fallback).\n */\nexport function runBehavioralCanaries(\n cases: Array<{ scenario: DatasetScenario; output: string; runId?: string }>,\n): CanaryLeak[] {\n const leaks: CanaryLeak[] = []\n for (const c of cases) {\n const leak = checkBehavioralCanary(c.output, c.scenario)\n if (leak) leaks.push({ ...leak, runId: c.runId ?? leak.runId })\n }\n return leaks\n}\n\n/**\n * Resolve a forbidden-pattern string to the matched substring inside\n * `output`. `/body/flags` notation is interpreted as a regex; anything\n * else is a literal substring.\n */\nfunction matchForbidden(output: string, pattern: string): string | null {\n const re = tryParseRegex(pattern)\n if (re) {\n const m = output.match(re)\n return m && m[0].length > 0 ? m[0] : null\n }\n return output.includes(pattern) ? pattern : null\n}\n\nfunction tryParseRegex(pattern: string): RegExp | null {\n if (pattern.length < 2 || pattern[0] !== '/') return null\n const last = pattern.lastIndexOf('/')\n if (last <= 0) return null\n const body = pattern.slice(1, last)\n const flags = pattern.slice(last + 1)\n if (!/^[gimsuy]*$/.test(flags)) return null\n try {\n return new RegExp(body, flags)\n } catch {\n return null\n }\n}\n\n/**\n * Scan the LLM-output history in a corpus; returns every case where a\n * canary from a known scenario appeared in agent output. Pass the full\n * set of scenarios whose canaries you care about (typically the whole\n * held-out slice).\n */\nexport async function canaryLeakView(\n store: TraceStore,\n scenarios: DatasetScenario[],\n): Promise<CanaryLeak[]> {\n const targets = scenarios.filter((s) => !!s.canary)\n if (targets.length === 0) return []\n const spans = await llmSpans(store)\n const leaks: CanaryLeak[] = []\n for (const span of spans) {\n const output = span.output ?? ''\n for (const s of targets) {\n if (s.canary && output.includes(s.canary)) {\n leaks.push({\n scenarioId: s.id,\n canary: s.canary,\n runId: span.runId,\n evidence: excerpt(output, s.canary),\n })\n }\n }\n }\n return leaks\n}\n\nexport class HoldoutAuditor {\n private scenarios: DatasetScenario[]\n private accessLog: Array<{ scenarioId: string; purpose: string; at: number }> = []\n\n constructor(scenarios: DatasetScenario[]) {\n this.scenarios = scenarios\n }\n\n /** Retrieve a holdout scenario for a declared purpose. Non-'evaluation' throws. */\n get(scenarioId: string, purpose: 'evaluation' | 'debugging'): DatasetScenario {\n if (purpose !== 'evaluation' && purpose !== 'debugging') {\n throw new Error(\n `HoldoutAuditor.get: purpose must be 'evaluation' or 'debugging', got ${purpose}`,\n )\n }\n const s = this.scenarios.find((x) => x.id === scenarioId)\n if (!s) throw new Error(`holdout scenario \"${scenarioId}\" not found`)\n this.accessLog.push({ scenarioId, purpose, at: Date.now() })\n return s\n }\n\n getAccessLog(): ReadonlyArray<{ scenarioId: string; purpose: string; at: number }> {\n return this.accessLog\n }\n}\n\nfunction excerpt(source: string, needle: string): string {\n const at = source.indexOf(needle)\n if (at < 0) return ''\n const start = Math.max(0, at - 30)\n const end = Math.min(source.length, at + needle.length + 30)\n return (start > 0 ? '…' : '') + source.slice(start, end) + (end < source.length ? '…' : '')\n}\n","/**\n * # `analyzeRuns()` — turn a set of agent runs into an actionable decision packet.\n *\n * Wires the substrate's statistical, calibration, clustering, Pareto, and\n * release-confidence primitives into one `InsightReport`. Two top-level\n * entry points use this function:\n *\n * - `selfImprove()` calls it on the campaign output to attach a packet\n * to every run.\n * - Consumers with observed `RunRecord[]` (production traces, gold\n * corpora, approve/reject tables) call it directly via `analyzeRuns()`\n * for analysis without a closed loop.\n *\n * Every section is opt-in based on what the input data supports — the\n * function never invents signal. If runs carry no judge scores, `judges`\n * is empty. If there's no baseline/candidate split, `lift` is undefined.\n * If no `analyst` is wired, `failureClusters` is undefined.\n *\n * The `recommendations` array is the human-readable layer; everything\n * else is the evidence backing each recommendation.\n */\n\nimport type { AnalystRegistry } from '../analyst/registry'\nimport type { AnalystFinding } from '../analyst/types'\nimport { welchsTTest } from '../baseline'\nimport { checkCanaries } from '../contamination-guard'\nimport type { DatasetScenario } from '../dataset'\nimport { continuousAgreement } from '../judge-calibration'\nimport { pairRunRecords } from '../paired-arms'\nimport { observedSplitScore } from '../rollout/reward'\nimport {\n type RunRecord,\n type RunTerminalOutcome,\n type RunTokenUsage,\n validateRunRecord,\n} from '../run-record'\nimport {\n BOOTSTRAP_GATE_MIN_N,\n pairedBootstrap,\n pairedCohensDz,\n pairedMde,\n pairedTTest,\n pearsonR,\n requiredPairedSampleSize,\n spearmanR,\n} from '../statistics'\nimport { type ParetoFigureSpec, paretoChart } from '../summary-report'\nimport type { FailureClass } from '../trace/schema'\n\nimport type {\n CostProvenanceSummary,\n ExecutionInsight,\n FailureClassTally,\n FailureClusterInsight,\n InsightReport,\n InterRaterInsight,\n JudgeInsight,\n LiftInsight,\n MetricDelta,\n OutcomeCorrelationInsight,\n PriorPeriodComparison,\n Recommendation,\n ScalarDistribution,\n TokenUsageInsight,\n} from './insight-report'\n\n// ── Public API ───────────────────────────────────────────────────────\n\nexport interface AnalyzeRunsOptions {\n /** The runs to analyze. */\n runs: RunRecord[]\n /** Which split to score against when reading composite from RunOutcome.\n * Default: holdout when ANY run has a `holdoutScore`, else search. */\n split?: 'search' | 'holdout' | 'auto'\n /** Pairwise analysis configuration. When both `baselineCandidateId` and\n * `candidateCandidateId` are present, lift is computed on paired\n * (experimentId, scenarioId, seed) identities shared between the two sides.\n * Unmatched rows remain visible in the lift result. */\n baselineCandidateId?: string\n candidateCandidateId?: string\n /** Canary scenarios — checked against every run's raw output for\n * holdout contamination. */\n canaryScenarios?: DatasetScenario[]\n /** Analyst registry for failure clustering. When omitted, the\n * `failureClusters` section is left undefined. */\n analyst?: AnalystRegistry\n /** Downstream outcome metric per run (e.g. engagement rate, approval\n * rate, downstream pass rate). When present, the report includes\n * `outcomeCorrelation` + a simple linear reward model fit. */\n outcomeSignal?: {\n metric: string\n valueByRunId: Record<string, number>\n }\n /** Multi-rater feedback for inter-rater agreement. Each entry is one\n * rater's score for one run. Two or more raters → kappa + disagreement\n * triage list. */\n raterScores?: Array<{ runId: string; rater: string; score: number }>\n /** Number of histogram bins for distributional summaries. Default 12. */\n histogramBins?: number\n /** Decision threshold — the smallest composite lift the caller cares\n * about. Used by the recommendations engine to call ship vs hold.\n * Default 0.02. */\n decisionThreshold?: number\n /** Optional prior-period runs. When set, the report includes\n * `priorPeriodComparison` with per-metric Welch-CI deltas and\n * recommendations fire on statistically significant regressions.\n * The two windows do NOT have to share scenarios — the comparison\n * is two-sample unpaired (the substrate's `lift` field uses paired\n * bootstrap on shared (experimentId, scenarioId, seed) identities; this is the\n * shape for \"this week vs last week\" rather than \"candidate vs\n * baseline within a campaign\"). */\n baselineRuns?: RunRecord[]\n /** Human-readable label for the baseline window, e.g. \"vs prior 7\n * days\", \"vs v3.1 release\". Surfaces in recommendations + UI. */\n baselineLabel?: string\n}\n\nexport interface SummarizeExecutionOptions {\n runs: RunRecord[]\n histogramBins?: number\n}\n\nexport interface ExecutionReport {\n execution: ExecutionInsight\n costProvenance: CostProvenanceSummary\n}\n\n/** Summarize runtime facts without interpreting task quality or promotion readiness. */\nexport function summarizeExecution(opts: SummarizeExecutionOptions): ExecutionReport {\n const runs = opts.runs.map(validateRunRecord)\n const bins = opts.histogramBins ?? 12\n return {\n execution: computeExecutionInsight(runs, bins),\n costProvenance: summarizeCostProvenance(runs),\n }\n}\n\n/** A bootstrap interval with no spread: every resample landed on the same\n * value, so the interval carries no information about how far the point\n * estimate could be wrong and cannot support a directional claim. */\nfunction zeroWidth(ci: readonly [number, number]): boolean {\n return !Number.isFinite(ci[0]) || !Number.isFinite(ci[1]) || ci[0] === ci[1]\n}\n\nexport async function analyzeRuns(opts: AnalyzeRunsOptions): Promise<InsightReport> {\n const runs = opts.runs.map(validateRunRecord)\n const bins = opts.histogramBins ?? 12\n const threshold = opts.decisionThreshold ?? 0.02\n if (!Number.isFinite(threshold)) {\n throw new Error(`analyzeRuns: decisionThreshold must be finite, got ${threshold}`)\n }\n const split = resolveSplit(runs, opts.split ?? 'auto')\n\n const compositeWithIds = runs\n .map((r) => ({ runId: r.runId, score: compositeOf(r, split) }))\n .filter((p) => Number.isFinite(p.score))\n const composite = distributionOf(\n compositeWithIds.map((p) => p.score),\n bins,\n compositeWithIds,\n )\n\n const perDimension = computePerDimension(runs, bins)\n const { execution, costProvenance: provenance } = summarizeExecution({\n runs,\n histogramBins: bins,\n })\n const knownCostRuns = runs.filter((run) => run.costProvenance.kind !== 'uncaptured')\n const costs = knownCostRuns.map((r) => r.costUsd).filter(isFiniteNumber)\n const costDist = distributionOf(costs, bins)\n const pareto = paretoChart(knownCostRuns, { split })\n const degraded: { cost?: string; pareto?: string } = {}\n if (provenance.uncaptured.n > 0) {\n degraded.cost = diagnoseCostCoverage(runs, provenance)\n } else if (costs.length === 0 || costs.every((c) => c === 0)) {\n degraded.cost = `all ${runs.length} explicitly observed or estimated USD values are $0`\n }\n if (pareto.points.length < 2) {\n degraded.pareto =\n pareto.points.length === 0\n ? 'no candidates — Pareto unavailable'\n : 'single candidate — Pareto is a single point, not a frontier'\n }\n const costQuality = {\n cost: costDist,\n pareto,\n provenance,\n ...(degraded.cost || degraded.pareto ? { degraded } : {}),\n }\n\n const judges = computeJudgeInsights(runs)\n\n const interRater = opts.raterScores ? computeInterRater(opts.raterScores) : undefined\n\n const lift = computeLift(runs, opts.baselineCandidateId, opts.candidateCandidateId, split)\n\n const failureClusters = opts.analyst\n ? await computeFailureClusters(runs, opts.analyst, split)\n : undefined\n\n const failureClasses = computeFailureClasses(runs, split)\n\n const contamination = opts.canaryScenarios\n ? computeContamination(runs, opts.canaryScenarios)\n : undefined\n\n const outcomeCorrelation = opts.outcomeSignal\n ? computeOutcomeCorrelation(runs, opts.outcomeSignal, split)\n : undefined\n\n const release = buildReleaseScorecard(composite, lift, contamination)\n\n const priorPeriodComparison = opts.baselineRuns\n ? computePriorPeriodComparison(runs, opts.baselineRuns, split, opts.baselineLabel)\n : undefined\n\n const recommendations = buildRecommendations({\n composite,\n judges,\n interRater,\n lift,\n failureClusters,\n failureClasses,\n contamination,\n outcomeCorrelation,\n priorPeriodComparison,\n threshold,\n })\n\n return {\n n: runs.length,\n execution,\n composite,\n perDimension,\n costQuality,\n judges,\n interRater,\n lift,\n failureClusters,\n contamination,\n outcomeCorrelation,\n release,\n ...(failureClasses ? { failureClasses } : {}),\n ...(priorPeriodComparison ? { priorPeriodComparison } : {}),\n recommendations,\n }\n}\n\nfunction computeExecutionInsight(runs: RunRecord[], bins: number): ExecutionInsight {\n const aggregateRows = runs.flatMap((run) => {\n const usage = aggregateTokenUsage(run)\n return usage ? [{ usage, costUsd: finiteRaw(run, 'aggregate_cost_usd') }] : []\n })\n const aggregateCosts = aggregateRows.flatMap((row) =>\n row.costUsd !== undefined ? [row.costUsd] : [],\n )\n const modelCounts = new Map<string, number>()\n let executionErrorRuns = 0\n let executionErrorEvents = 0\n let errorReportingRuns = 0\n let errorSpanEvents = 0\n let errorSpanReportingRuns = 0\n const terminalOutcomes: Record<RunTerminalOutcome, number> = {\n succeeded: 0,\n failed: 0,\n cancelled: 0,\n incomplete: 0,\n unknown: 0,\n }\n const errorsByTerminalOutcome: ExecutionInsight['executionErrors']['byTerminalOutcome'] = {\n succeeded: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n failed: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n cancelled: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n incomplete: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n unknown: { withErrors: 0, withoutErrors: 0, unreported: 0 },\n }\n let modelCallRuns = 0\n let modelCallEvents = 0\n let modelCallReportingRuns = 0\n\n for (const run of runs) {\n modelCounts.set(run.model, (modelCounts.get(run.model) ?? 0) + 1)\n const terminalOutcome = run.terminalOutcome\n terminalOutcomes[terminalOutcome] += 1\n const modelCalls = nonNegativeCountRaw(run, 'llm_span_count')\n if (modelCalls !== undefined) {\n modelCallEvents += modelCalls\n modelCallReportingRuns += 1\n }\n const usage = run.tokenUsage\n if (\n (modelCalls ?? 0) > 0 ||\n usage.input > 0 ||\n usage.output > 0 ||\n (usage.cached ?? 0) > 0 ||\n (usage.cacheWrite ?? 0) > 0\n ) {\n modelCallRuns += 1\n }\n const errorEvents = reportedExecutionErrorEvents(run)\n if (errorEvents !== undefined) {\n executionErrorEvents += errorEvents\n errorReportingRuns += 1\n if (errorEvents > 0) {\n executionErrorRuns += 1\n errorsByTerminalOutcome[terminalOutcome].withErrors += 1\n } else errorsByTerminalOutcome[terminalOutcome].withoutErrors += 1\n } else errorsByTerminalOutcome[terminalOutcome].unreported += 1\n const reportedErrorSpans = nonNegativeCountRaw(run, 'error_span_count')\n if (reportedErrorSpans !== undefined) {\n errorSpanEvents += reportedErrorSpans\n errorSpanReportingRuns += 1\n }\n }\n\n return {\n durationMs: distributionOf(\n runs.map((run) => run.wallMs),\n bins,\n ),\n queueMs: distributionOf(\n runs.filter((run) => run.queueMs !== undefined).map((run) => run.queueMs!),\n bins,\n ),\n tokenUsage: summarizeTokenUsage(\n runs.map((run) => run.tokenUsage),\n bins,\n ),\n aggregateUsage: {\n runs: aggregateRows.length,\n tokenUsage: summarizeTokenUsage(\n aggregateRows.map((row) => row.usage),\n bins,\n ),\n costUsd: distributionOf(aggregateCosts, bins),\n totalCostUsd: aggregateCosts.reduce((total, value) => total + value, 0),\n },\n models: [...modelCounts.entries()]\n .map(([model, count]) => ({ model, runs: count }))\n .sort((left, right) => right.runs - left.runs || left.model.localeCompare(right.model)),\n modelCalls: {\n runs: modelCallRuns,\n events: modelCallEvents,\n reportingRuns: modelCallReportingRuns,\n },\n executionErrors: {\n runs: executionErrorRuns,\n fraction: errorReportingRuns > 0 ? executionErrorRuns / errorReportingRuns : null,\n events: executionErrorEvents,\n reportingRuns: errorReportingRuns,\n errorSpanEvents,\n errorSpanReportingRuns,\n byTerminalOutcome: errorsByTerminalOutcome,\n },\n terminalOutcomes,\n }\n}\n\nfunction reportedExecutionErrorEvents(run: RunRecord): number | undefined {\n return nonNegativeCountRaw(run, 'execution_error_count')\n}\n\nfunction nonNegativeCountRaw(run: RunRecord, key: string): number | undefined {\n const value = finiteRaw(run, key)\n return value !== undefined && Number.isInteger(value) && value >= 0 ? value : undefined\n}\n\nfunction summarizeTokenUsage(usages: RunTokenUsage[], bins: number): TokenUsageInsight {\n const reasoning = usages.flatMap((usage) =>\n usage.reasoning !== undefined ? [usage.reasoning] : [],\n )\n const cached = usages.flatMap((usage) => (usage.cached !== undefined ? [usage.cached] : []))\n const cacheWrite = usages.flatMap((usage) =>\n usage.cacheWrite !== undefined ? [usage.cacheWrite] : [],\n )\n return {\n input: distributionOf(\n usages.map((usage) => usage.input),\n bins,\n ),\n output: distributionOf(\n usages.map((usage) => usage.output),\n bins,\n ),\n reasoning: distributionOf(reasoning, bins),\n cached: distributionOf(cached, bins),\n cacheWrite: distributionOf(cacheWrite, bins),\n totals: {\n input: usages.reduce((total, usage) => total + usage.input, 0),\n output: usages.reduce((total, usage) => total + usage.output, 0),\n reasoning: reasoning.reduce((total, value) => total + value, 0),\n cached: cached.reduce((total, value) => total + value, 0),\n cacheWrite: cacheWrite.reduce((total, value) => total + value, 0),\n },\n }\n}\n\nfunction aggregateTokenUsage(run: RunRecord): RunTokenUsage | undefined {\n const input = finiteRaw(run, 'aggregate_prompt_tokens')\n const output = finiteRaw(run, 'aggregate_completion_tokens')\n const reasoning = finiteRaw(run, 'aggregate_reasoning_tokens')\n const cached = finiteRaw(run, 'aggregate_cached_tokens')\n const cacheWrite = finiteRaw(run, 'aggregate_cache_write_tokens')\n if (\n input === undefined &&\n output === undefined &&\n reasoning === undefined &&\n cached === undefined &&\n cacheWrite === undefined\n )\n return undefined\n return {\n input: input ?? 0,\n output: output ?? 0,\n ...(reasoning !== undefined ? { reasoning } : {}),\n ...(cached !== undefined ? { cached } : {}),\n ...(cacheWrite !== undefined ? { cacheWrite } : {}),\n }\n}\n\nfunction finiteRaw(run: RunRecord, key: string): number | undefined {\n const value = run.outcome.raw[key]\n return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : undefined\n}\n\nfunction summarizeCostProvenance(runs: RunRecord[]): CostProvenanceSummary {\n const summary: CostProvenanceSummary = {\n observed: { n: 0, totalUsd: 0 },\n estimated: { n: 0, totalUsd: 0 },\n uncaptured: { n: 0 },\n knownFraction: 0,\n }\n for (const run of runs) {\n const cost = run.costProvenance\n if (cost.kind === 'uncaptured') {\n summary.uncaptured.n += 1\n } else {\n summary[cost.kind].n += 1\n summary[cost.kind].totalUsd += cost.usd\n }\n }\n const known = summary.observed.n + summary.estimated.n\n summary.knownFraction = runs.length > 0 ? known / runs.length : 0\n return summary\n}\n\nfunction diagnoseCostCoverage(runs: RunRecord[], provenance: CostProvenanceSummary): string {\n const uncaptured = provenance.uncaptured.n\n const known = provenance.observed.n + provenance.estimated.n\n if (uncaptured === runs.length) {\n return `USD cost uncaptured for all ${runs.length} runs — no observed or estimated USD values; token and wall-time metrics remain available.`\n }\n return `USD cost uncaptured for ${uncaptured}/${runs.length} runs; excluded those rows from cost statistics (${known}/${runs.length} retained: ${provenance.observed.n} observed, ${provenance.estimated.n} estimated).`\n}\n\n/**\n * Model-free task-failure tally.\n *\n * Explicit non-success classes are task-failure evidence.\n * A low task score without a class is counted as `unknown`.\n */\nfunction computeFailureClasses(\n runs: RunRecord[],\n split: 'search' | 'holdout',\n): FailureClassTally[] | undefined {\n const counts = new Map<FailureClass, number>()\n for (const r of runs) {\n if (!isTaskFailure(r, split)) continue\n const key =\n r.failureClass !== undefined && r.failureClass !== 'success' ? r.failureClass : 'unknown'\n counts.set(key, (counts.get(key) ?? 0) + 1)\n }\n if (counts.size === 0) return undefined\n const n = runs.length\n return [...counts.entries()]\n .map(([failureClass, count]) => ({\n failureClass,\n count,\n share: n > 0 ? count / n : 0,\n }))\n .sort((a, b) => b.count - a.count || a.failureClass.localeCompare(b.failureClass))\n}\n\n// ── Prior-period comparison ─────────────────────────────────────────\n\n/** Direction of the metric — does \"higher current\" mean better or worse?\n * Composite + judge dimensions: higher is better. Cost + duration: lower\n * is better. The recommendations engine flips the sign before judging\n * regressed vs improved. */\ntype MetricDirection = 'higher-is-better' | 'lower-is-better'\n\nfunction computePriorPeriodComparison(\n current: RunRecord[],\n baseline: RunRecord[],\n split: 'search' | 'holdout',\n windowLabel: string | undefined,\n): PriorPeriodComparison | undefined {\n if (current.length === 0 || baseline.length === 0) return undefined\n\n const metrics: Record<string, MetricDelta> = {}\n const directions: Record<string, MetricDirection> = {}\n\n const compositeCurrent = current\n .map((r) => compositeOf(r, split))\n .filter(Number.isFinite) as number[]\n const compositeBaseline = baseline\n .map((r) => compositeOf(r, split))\n .filter(Number.isFinite) as number[]\n if (compositeCurrent.length > 0 && compositeBaseline.length > 0) {\n metrics.composite = welchCompare(compositeBaseline, compositeCurrent)\n directions.composite = 'higher-is-better'\n }\n\n const costCurrent = knownCostValues(current)\n const costBaseline = knownCostValues(baseline)\n if (costCurrent.length > 0 && costBaseline.length > 0) {\n metrics.cost = welchCompare(costBaseline, costCurrent)\n directions.cost = 'lower-is-better'\n }\n\n const durCurrent = current.map((r) => r.wallMs).filter(Number.isFinite)\n const durBaseline = baseline.map((r) => r.wallMs).filter(Number.isFinite)\n if (durCurrent.length > 0 && durBaseline.length > 0) {\n metrics.duration = welchCompare(durBaseline, durCurrent)\n directions.duration = 'lower-is-better'\n }\n\n const tokCurrent = current\n .map((r) => (r.tokenUsage.input ?? 0) + (r.tokenUsage.output ?? 0))\n .filter(Number.isFinite)\n const tokBaseline = baseline\n .map((r) => (r.tokenUsage.input ?? 0) + (r.tokenUsage.output ?? 0))\n .filter(Number.isFinite)\n if (tokCurrent.length > 0 && tokBaseline.length > 0) {\n metrics.tokenUsage = welchCompare(tokBaseline, tokCurrent)\n directions.tokenUsage = 'lower-is-better'\n }\n\n // Per-dimension judge comparisons — only for dimensions present in BOTH\n // windows. We use perDimMean since per-judge nesting is finicky for\n // two-sample comparisons across different judge configurations.\n const dimsCurrent = collectPerDimension(current)\n const dimsBaseline = collectPerDimension(baseline)\n for (const dim of Object.keys(dimsCurrent)) {\n const b = dimsBaseline[dim]\n const c = dimsCurrent[dim]\n if (!b || b.length === 0 || !c || c.length === 0) continue\n metrics[`dim.${dim}`] = welchCompare(b, c)\n directions[`dim.${dim}`] = 'higher-is-better'\n }\n\n const regressedMetrics: string[] = []\n const improvedMetrics: string[] = []\n const inconclusiveMetrics: string[] = []\n for (const [name, delta] of Object.entries(metrics)) {\n if (delta.status !== 'ok') {\n inconclusiveMetrics.push(name)\n continue\n }\n if (!delta.significant) continue\n const dir = directions[name] ?? 'higher-is-better'\n const better = dir === 'higher-is-better' ? delta.delta > 0 : delta.delta < 0\n if (better) improvedMetrics.push(name)\n else regressedMetrics.push(name)\n }\n\n return {\n baselineN: baseline.length,\n currentN: current.length,\n ...(windowLabel ? { windowLabel } : {}),\n metrics,\n regressedMetrics,\n improvedMetrics,\n inconclusiveMetrics,\n }\n}\n\nfunction knownCostValues(runs: RunRecord[]): number[] {\n return runs\n .filter((run) => run.costProvenance.kind !== 'uncaptured')\n .map((run) => run.costUsd)\n .filter(isFiniteNumber)\n}\n\nfunction isFiniteNumber(value: unknown): value is number {\n return typeof value === 'number' && Number.isFinite(value)\n}\n\n/** Collect per-dimension values across runs (from outcome.judgeScores.perDimMean). */\nfunction collectPerDimension(runs: RunRecord[]): Record<string, number[]> {\n const out: Record<string, number[]> = {}\n for (const r of runs) {\n const perDim = r.outcome.judgeScores?.perDimMean\n if (!perDim) continue\n for (const [dim, value] of Object.entries(perDim)) {\n if (!Number.isFinite(value)) continue\n if (!out[dim]) out[dim] = []\n out[dim].push(value as number)\n }\n }\n return out\n}\n\n/** Adapt the shared two-sample Welch result to the report contract. */\nfunction welchCompare(baseline: number[], current: number[]): MetricDelta {\n const result = welchsTTest(baseline, current)\n const base = {\n current: result.meanB,\n baseline: result.meanA,\n delta: result.delta,\n baselineN: baseline.length,\n currentN: current.length,\n }\n if (result.status !== 'ok') {\n return {\n ...base,\n status: result.status,\n ci95: null,\n pValue: null,\n cohensD: null,\n significant: false,\n }\n }\n return {\n ...base,\n status: 'ok',\n ci95: result.ci95,\n pValue: result.p,\n cohensD: result.cohensD,\n significant: result.p < 0.05 && Math.abs(result.cohensD) >= 0.2,\n }\n}\n\n// ── Composite + split selection ─────────────────────────────────────\n\nfunction resolveSplit(\n runs: RunRecord[],\n pref: 'search' | 'holdout' | 'auto',\n): 'search' | 'holdout' {\n if (pref !== 'auto') return pref\n const hasHoldout = runs.some((r) => Number.isFinite(observedSplitScore(r, 'holdout')))\n return hasHoldout ? 'holdout' : 'search'\n}\n\n/**\n * RAW (`observedSplitScore`): `analyzeRuns` describes what a set of runs\n * reported, and every downstream reader of this composite — distributions,\n * per-candidate summaries, the reward-hacking correlation — needs the ungated\n * number to see an inflated run at all.\n */\nfunction compositeOf(run: RunRecord, split: 'search' | 'holdout'): number {\n // Split-exact, no cross-split fallthrough: answering \"what did this run\n // score on the split I am summarising\" with the other split's number\n // silently mixes populations.\n const score = observedSplitScore(run, split)\n return Number.isFinite(score) ? (score as number) : Number.NaN\n}\n\n// ── Distribution helpers ────────────────────────────────────────────\n\nfunction distributionOf(\n values: number[],\n bins: number,\n withIds?: Array<{ runId: string; score: number }>,\n): ScalarDistribution {\n if (values.length === 0) {\n return {\n n: 0,\n mean: null,\n p50: null,\n p95: null,\n stddev: null,\n min: null,\n max: null,\n histogram: [],\n }\n }\n const sorted = [...values].sort((a, b) => a - b)\n const n = sorted.length\n const mean = sorted.reduce((s, v) => s + v, 0) / n\n const variance = sorted.reduce((s, v) => s + (v - mean) ** 2, 0) / n\n const stddev = Math.sqrt(variance)\n const tailRuns = withIds\n ? [...withIds].sort((a, b) => a.score - b.score).slice(0, Math.min(5, withIds.length))\n : undefined\n return {\n n,\n mean,\n p50: percentile(sorted, 0.5),\n p95: percentile(sorted, 0.95),\n stddev,\n min: sorted[0]!,\n max: sorted[n - 1]!,\n histogram: histogram(sorted, bins),\n ...(tailRuns ? { tailRuns } : {}),\n }\n}\n\nfunction percentile(sorted: number[], q: number): number {\n if (sorted.length === 0) return 0\n if (sorted.length === 1) return sorted[0]!\n const idx = (sorted.length - 1) * q\n const lo = Math.floor(idx)\n const hi = Math.ceil(idx)\n if (lo === hi) return sorted[lo]!\n const w = idx - lo\n return sorted[lo]! * (1 - w) + sorted[hi]! * w\n}\n\n/** Even-width histogram over the value range. Returns inclusive-lo /\n * exclusive-hi bins (closed on right for the last bin) compatible with\n * the substrate's `GainDistributionBin` shape. */\nfunction histogram(sorted: number[], bins: number): ScalarDistribution['histogram'] {\n if (sorted.length === 0 || bins < 1) return []\n const min = sorted[0]!\n const max = sorted[sorted.length - 1]!\n if (min === max) return [{ lo: min, hi: max, count: sorted.length }]\n const width = (max - min) / bins\n const out: ScalarDistribution['histogram'] = []\n for (let i = 0; i < bins; i++) {\n const lo = min + i * width\n const hi = i === bins - 1 ? max : lo + width\n out.push({ lo, hi, count: 0 })\n }\n for (const v of sorted) {\n const idx = Math.min(bins - 1, Math.floor((v - min) / width))\n out[idx]!.count++\n }\n return out\n}\n\nfunction computePerDimension(runs: RunRecord[], bins: number): Record<string, ScalarDistribution> {\n // JudgeScoresRecord pre-aggregates `perDimMean` (mean across judges per\n // dimension). We collect those means across runs to produce a per-dim\n // distribution at the corpus level. Consumers who want per-judge\n // dimension values reach into `perJudge[judgeId][dim]` themselves.\n const byDim = new Map<string, number[]>()\n for (const run of runs) {\n const scores = run.outcome.judgeScores\n if (!scores) continue\n for (const [dim, value] of Object.entries(scores.perDimMean ?? {})) {\n if (!Number.isFinite(value)) continue\n const arr = byDim.get(dim) ?? []\n arr.push(value)\n byDim.set(dim, arr)\n }\n }\n const out: Record<string, ScalarDistribution> = {}\n for (const [dim, values] of byDim) out[dim] = distributionOf(values, bins)\n return out\n}\n\n// ── Judge insights ──────────────────────────────────────────────────\n\nfunction computeJudgeInsights(runs: RunRecord[]): Record<string, JudgeInsight> {\n // Each judge's per-run mean is the average of its per-dimension scores\n // for that run. We aggregate those means across all runs each judge\n // scored — giving consumers a \"this judge's typical verdict\" reading.\n const out: Record<string, JudgeInsight> = {}\n const byJudge = new Map<string, number[]>()\n for (const run of runs) {\n const scores = run.outcome.judgeScores\n if (!scores?.perJudge) continue\n for (const [judgeId, dims] of Object.entries(scores.perJudge)) {\n const dimValues = Object.values(dims).filter(Number.isFinite) as number[]\n if (dimValues.length === 0) continue\n const judgeMean = dimValues.reduce((s, v) => s + v, 0) / dimValues.length\n const arr = byJudge.get(judgeId) ?? []\n arr.push(judgeMean)\n byJudge.set(judgeId, arr)\n }\n }\n for (const [judgeId, values] of byJudge) {\n out[judgeId] = {\n n: values.length,\n meanScore: values.reduce((s, v) => s + v, 0) / values.length,\n }\n }\n return out\n}\n\n// ── Inter-rater agreement ───────────────────────────────────────────\n\nfunction computeInterRater(\n ratings: Array<{ runId: string; rater: string; score: number }>,\n): InterRaterInsight | undefined {\n const byRun = new Map<string, Array<{ rater: string; score: number }>>()\n for (const r of ratings) {\n if (!Number.isFinite(r.score)) continue\n const list = byRun.get(r.runId) ?? []\n list.push({ rater: r.rater, score: r.score })\n byRun.set(r.runId, list)\n }\n const raters = new Set(ratings.map((r) => r.rater))\n const jointlyRated: string[] = []\n for (const [runId, ratersForRun] of byRun) {\n const seen = new Set(ratersForRun.map((r) => r.rater))\n let all = true\n for (const r of raters) if (!seen.has(r)) all = false\n if (all) jointlyRated.push(runId)\n }\n if (raters.size < 2 || jointlyRated.length === 0) return undefined\n\n const raterList = [...raters].sort()\n const perPair: Record<string, number> = {}\n for (let i = 0; i < raterList.length; i++) {\n for (let j = i + 1; j < raterList.length; j++) {\n const a = raterList[i]!\n const b = raterList[j]!\n const aScores: number[] = []\n const bScores: number[] = []\n for (const runId of jointlyRated) {\n const ratersForRun = byRun.get(runId)!\n const sa = ratersForRun.find((r) => r.rater === a)?.score\n const sb = ratersForRun.find((r) => r.rater === b)?.score\n if (sa !== undefined && sb !== undefined) {\n aScores.push(sa)\n bScores.push(sb)\n }\n }\n const agreement = continuousAgreement(\n aScores.map((score, index) => [score, bScores[index]!]),\n { bootstrap: 0 },\n )\n perPair[`${a}::${b}`] = agreement.weightedKappa\n }\n }\n const matrix = jointlyRated.map((runId) => {\n const ratingsByRater = new Map(byRun.get(runId)!.map((rating) => [rating.rater, rating.score]))\n return raterList.map((rater) => ratingsByRater.get(rater)!)\n })\n const agreement = continuousAgreement(matrix, { bootstrap: 0 })\n\n const disagreementCases = jointlyRated\n .map((runId) => {\n const ratersForRun = byRun.get(runId)!\n const scores = ratersForRun.map((r) => r.score)\n const range = Math.max(...scores) - Math.min(...scores)\n return { runId, ratings: ratersForRun, range }\n })\n .sort((a, b) => b.range - a.range)\n .slice(0, 20)\n\n return {\n raters: raters.size,\n jointlyRated: jointlyRated.length,\n kappa: Number.isFinite(agreement.weightedKappa) ? agreement.weightedKappa : 0,\n icc: agreement.icc,\n pearson: agreement.pearson,\n spearman: agreement.spearman,\n perPair,\n disagreementCases,\n }\n}\n\n// ── Lift ────────────────────────────────────────────────────────────\n\nfunction computeLift(\n runs: RunRecord[],\n baselineId: string | undefined,\n candidateId: string | undefined,\n split: 'search' | 'holdout',\n): LiftInsight | undefined {\n let bId = baselineId\n let cId = candidateId\n if (!bId || !cId) {\n // Auto-detect: when exactly two distinct candidateIds appear, treat the\n // lower-mean side as baseline.\n const ids = [...new Set(runs.map((r) => r.candidateId))]\n if (ids.length !== 2) return undefined\n const [idA, idB] = ids as [string, string]\n const scoresA = finiteCompositeScores(\n runs.filter((run) => run.candidateId === idA),\n split,\n )\n const scoresB = finiteCompositeScores(\n runs.filter((run) => run.candidateId === idB),\n split,\n )\n if (scoresA.length === 0 || scoresB.length === 0) return undefined\n const meanA = mean(scoresA)\n const meanB = mean(scoresB)\n bId = meanA <= meanB ? idA : idB\n cId = meanA <= meanB ? idB : idA\n }\n\n const baseline = runs.filter((r) => r.candidateId === bId)\n const candidate = runs.filter((r) => r.candidateId === cId)\n if (baseline.length === 0 || candidate.length === 0) return undefined\n\n const scoredBaseline = baseline.filter((run) => Number.isFinite(compositeOf(run, split)))\n const scoredCandidate = candidate.filter((run) => Number.isFinite(compositeOf(run, split)))\n const pairing = pairRunRecords(scoredBaseline, scoredCandidate)\n const pairedBaseline = pairing.pairs.map((pair) => compositeOf(pair.baseline, split))\n const pairedCandidate = pairing.pairs.map((pair) => compositeOf(pair.treatment, split))\n if (pairedBaseline.length === 0) return undefined\n\n const baselineMean = mean(pairedBaseline)\n const candidateMean = mean(pairedCandidate)\n const delta = candidateMean - baselineMean\n\n const bootstrap = pairedBootstrap(pairedBaseline, pairedCandidate, {\n confidence: 0.95,\n resamples: 2000,\n statistic: 'mean',\n })\n const tTest = pairedTTest(pairedBaseline, pairedCandidate)\n const d = pairedCohensDz(pairedBaseline, pairedCandidate)\n const mde = pairedMde({ nPaired: pairedBaseline.length, power: 0.8, alpha: 0.05 })\n const requiredN =\n d === null || d === 0\n ? null\n : requiredPairedSampleSize({\n effect: Math.abs(d),\n power: 0.8,\n alpha: 0.05,\n })\n\n return {\n baselineMean,\n candidateMean,\n delta,\n ci95: [bootstrap.low, bootstrap.high],\n pValue: tTest.p,\n n: pairedBaseline.length,\n minimumRequired: BOOTSTRAP_GATE_MIN_N,\n decisionEligible: bootstrap.gateEligible,\n unpairedBaseline: pairing.unpairedBaseline.length,\n unpairedCandidate: pairing.unpairedTreatment.length,\n cohensD: d,\n mde,\n requiredN,\n }\n}\n\nfunction mean(arr: number[]): number {\n return arr.length === 0 ? 0 : arr.reduce((s, v) => s + v, 0) / arr.length\n}\n\n// ── Failure clustering ──────────────────────────────────────────────\n\nasync function computeFailureClusters(\n runs: RunRecord[],\n analyst: AnalystRegistry,\n split: 'search' | 'holdout',\n): Promise<FailureClusterInsight | undefined> {\n const failed = runs.filter((run) => isTaskFailure(run, split))\n if (failed.length === 0) return { clusters: [], totalFailures: 0 }\n\n const clusters = new Map<string, { exemplars: string[]; share: number }>()\n for (const run of failed) {\n try {\n // AnalystRunInputs routes by field name: run-record analysts read\n // `runRecord`. Any other shape makes every analyst skip with\n // \"missing input\" and the clusters come back silently empty.\n const result = await analyst.run(run.runId, { runRecord: run })\n for (const finding of result.findings as AnalystFinding[]) {\n const key = finding.area || finding.analyst_id || 'unclassified'\n const c = clusters.get(key) ?? { exemplars: [], share: 0 }\n if (c.exemplars.length < 5) c.exemplars.push(run.runId)\n clusters.set(key, c)\n }\n } catch {\n const c = clusters.get('analyst-error') ?? { exemplars: [], share: 0 }\n if (c.exemplars.length < 5) c.exemplars.push(run.runId)\n clusters.set('analyst-error', c)\n }\n }\n const clusterList = [...clusters.entries()].map(([id, c]) => ({\n id,\n name: id,\n share: c.exemplars.length / failed.length,\n exemplars: c.exemplars,\n }))\n clusterList.sort((a, b) => b.share - a.share)\n return { clusters: clusterList, totalFailures: failed.length }\n}\n\nfunction finiteCompositeScores(runs: readonly RunRecord[], split: 'search' | 'holdout'): number[] {\n return runs.map((run) => compositeOf(run, split)).filter(Number.isFinite)\n}\n\nfunction isTaskFailure(run: RunRecord, split: 'search' | 'holdout'): boolean {\n if (run.failureClass !== undefined && run.failureClass !== 'success') return true\n const score = compositeOf(run, split)\n return Number.isFinite(score) && score < 0.5\n}\n\n// ── Contamination ──────────────────────────────────────────────────\n\nfunction computeContamination(\n runs: RunRecord[],\n canaries: DatasetScenario[],\n): InsightReport['contamination'] {\n let leaks = 0\n const details: Array<{ runId: string; canary: string; matched: string }> = []\n for (const run of runs) {\n const output = stringifyOutput(run)\n if (!output) continue\n const leaksHere = checkCanaries(output, canaries)\n for (const leak of leaksHere) {\n leaks++\n details.push({ runId: run.runId, canary: leak.canary, matched: leak.evidence })\n }\n }\n return { leaks, holdoutAuditPassed: leaks === 0, details }\n}\n\nfunction stringifyOutput(run: RunRecord): string | undefined {\n // RunRecord doesn't fix where \"the agent's output\" lives — different\n // consumers stash it differently. We probe the common shapes: the\n // outcome.raw map (numeric only by design — unlikely to contain text),\n // and any string-valued fields tucked under metadata via type casting.\n // Consumers with bespoke shapes pass canaryScenarios only when they\n // know their runs carry a stringifiable surface.\n const metadata = (run as unknown as { metadata?: Record<string, unknown> }).metadata\n if (typeof metadata?.output === 'string') return metadata.output\n if (typeof metadata?.text === 'string') return metadata.text\n return undefined\n}\n\n// ── Outcome correlation + linear reward model ──────────────────────\n\nfunction computeOutcomeCorrelation(\n runs: RunRecord[],\n outcome: { metric: string; valueByRunId: Record<string, number> },\n split: 'search' | 'holdout',\n): OutcomeCorrelationInsight | undefined {\n const xs: number[] = []\n const ys: number[] = []\n for (const run of runs) {\n const y = outcome.valueByRunId[run.runId]\n if (y === undefined || !Number.isFinite(y)) continue\n const x = compositeOf(run, split)\n if (!Number.isFinite(x)) continue\n xs.push(x)\n ys.push(y)\n }\n if (xs.length < 3) return undefined\n\n const p = pearsonR(xs, ys)\n const s = spearmanR(xs, ys)\n const meanX = mean(xs)\n const meanY = mean(ys)\n let num = 0\n let denom = 0\n for (let i = 0; i < xs.length; i++) {\n num += (xs[i]! - meanX) * (ys[i]! - meanY)\n denom += (xs[i]! - meanX) ** 2\n }\n const slope = denom === 0 ? 0 : num / denom\n const intercept = meanY - slope * meanX\n const ssTot = ys.reduce((a, y) => a + (y - meanY) ** 2, 0)\n const ssRes = ys.reduce((a, y, i) => a + (y - (intercept + slope * xs[i]!)) ** 2, 0)\n const r2 = ssTot === 0 ? 0 : 1 - ssRes / ssTot\n\n return {\n metric: outcome.metric,\n n: xs.length,\n pearson: p,\n spearman: s,\n rewardModel: { intercept, slope, r2 },\n }\n}\n\n// ── Release confidence scorecard ───────────────────────────────────\n\nfunction buildReleaseScorecard(\n composite: ScalarDistribution,\n lift: LiftInsight | undefined,\n contamination: InsightReport['contamination'],\n): InsightReport['release'] {\n // Synthesise a minimal scorecard from the rolled-up signal. The\n // substrate's `evaluateReleaseConfidence` primitive consumes a richer\n // input shape that callers can produce by wiring SLO definitions; the\n // shape here is the contract `selfImprove`/`analyzeRuns` consumers\n // receive automatically. They can call `evaluateReleaseConfidence`\n // directly when they want SLO-based axis evaluation.\n const axes: InsightReport['release']['axes'] = []\n const liftPass =\n lift === undefined\n ? ('not_evaluated' as const)\n : !lift.decisionEligible\n ? ('not_evaluated' as const)\n : lift.ci95[0] > 0 && !zeroWidth(lift.ci95)\n ? ('pass' as const)\n : lift.delta > 0\n ? ('warn' as const)\n : ('fail' as const)\n axes.push({\n name: 'quality-lift',\n status: liftPass,\n detail: lift\n ? `delta=${lift.delta.toFixed(3)}, CI95=[${lift.ci95[0].toFixed(3)}, ${lift.ci95[1].toFixed(3)}], n=${lift.n}${lift.decisionEligible ? '' : ` (descriptive only; ${lift.minimumRequired} required)`}`\n : 'no baseline/candidate pair available',\n })\n const contamPass =\n contamination === undefined\n ? ('not_evaluated' as const)\n : contamination.leaks === 0\n ? ('pass' as const)\n : ('fail' as const)\n axes.push({\n name: 'contamination',\n status: contamPass,\n detail: contamination ? `${contamination.leaks} canary leak(s)` : 'no canaries supplied',\n })\n axes.push(\n composite.n === 0\n ? {\n name: 'composite-distribution',\n status: 'not_evaluated',\n detail: 'no task-quality scores available',\n }\n : {\n name: 'composite-distribution',\n status:\n composite.mean !== null && composite.mean >= 0.5\n ? 'pass'\n : composite.mean !== null && composite.mean >= 0.3\n ? 'warn'\n : 'fail',\n detail:\n composite.mean === null || composite.p50 === null || composite.p95 === null\n ? 'task-quality distribution is internally incomplete'\n : `mean=${composite.mean.toFixed(3)}, p50=${composite.p50.toFixed(3)}, p95=${composite.p95.toFixed(3)} over n=${composite.n}`,\n },\n )\n const status = axes.some((a) => a.status === 'fail')\n ? 'fail'\n : axes.some((a) => a.status === 'warn' || a.status === 'not_evaluated')\n ? 'warn'\n : 'pass'\n return {\n status,\n axes,\n issues: [],\n }\n}\n\n// ── Recommendations engine ─────────────────────────────────────────\n\ninterface RecommendationContext {\n composite: ScalarDistribution\n judges: Record<string, JudgeInsight>\n interRater?: InterRaterInsight\n lift?: LiftInsight\n failureClusters?: FailureClusterInsight\n failureClasses?: FailureClassTally[]\n contamination?: InsightReport['contamination']\n outcomeCorrelation?: OutcomeCorrelationInsight\n priorPeriodComparison?: PriorPeriodComparison\n threshold: number\n}\n\nfunction buildRecommendations(ctx: RecommendationContext): Recommendation[] {\n const out: Recommendation[] = []\n\n // Prior-period regressions — highest customer-impact signal when present.\n // \"Did my last change help?\" with a falsifiable answer.\n if (ctx.priorPeriodComparison) {\n const ppc = ctx.priorPeriodComparison\n const label = ppc.windowLabel ?? 'baseline period'\n for (const name of ppc.regressedMetrics) {\n const d = ppc.metrics[name]\n if (d?.status !== 'ok') continue\n out.push({\n priority: 'critical',\n kind: 'investigate',\n title: `${name} regressed from ${d.baseline.toFixed(3)} → ${d.current.toFixed(3)} vs ${label}`,\n detail: `Welch CI95 = [${d.ci95[0].toFixed(3)}, ${d.ci95[1].toFixed(3)}], p=${d.pValue.toFixed(4)}, Cohen's d=${d.cohensD.toFixed(2)} (n_current=${d.currentN}, n_baseline=${d.baselineN}). The regression is statistically significant at p<0.05 with at-least-small effect size.`,\n evidencePath: `priorPeriodComparison.metrics.${name}`,\n })\n }\n for (const name of ppc.improvedMetrics) {\n const d = ppc.metrics[name]\n if (d?.status !== 'ok') continue\n out.push({\n priority: 'low',\n kind: 'ship',\n title: `${name} improved from ${d.baseline.toFixed(3)} → ${d.current.toFixed(3)} vs ${label}`,\n detail: `Welch CI95 = [${d.ci95[0].toFixed(3)}, ${d.ci95[1].toFixed(3)}], p=${d.pValue.toFixed(4)}, Cohen's d=${d.cohensD.toFixed(2)} (n_current=${d.currentN}, n_baseline=${d.baselineN}). Statistically significant improvement worth flagging.`,\n evidencePath: `priorPeriodComparison.metrics.${name}`,\n })\n }\n for (const name of ppc.inconclusiveMetrics) {\n const d = ppc.metrics[name]\n if (!d || d.status === 'ok' || d.delta === 0) continue\n const reason =\n d.status === 'zero-variance'\n ? 'both periods have zero observed variance'\n : 'one or both periods have fewer than two observations'\n out.push({\n priority: 'high',\n kind: 'investigate',\n title: `${name} changed from ${d.baseline.toFixed(3)} → ${d.current.toFixed(3)} vs ${label}; inference unavailable`,\n detail: `Observed delta ${d.delta.toFixed(3)} across n_current=${d.currentN} and n_baseline=${d.baselineN}, but ${reason}. The report does not fabricate a p-value, confidence interval, or effect size; inspect independence and data capture before acting.`,\n evidencePath: `priorPeriodComparison.metrics.${name}`,\n })\n }\n }\n\n // Composite-distribution branch. Fires when the overall quality signal is\n // poor regardless of lift / contamination / clusters — the customer needs\n // to know they have a problem AND which specific runs to inspect.\n if (\n ctx.composite.n > 0 &&\n ctx.composite.mean !== null &&\n ctx.composite.p50 !== null &&\n ctx.composite.p95 !== null\n ) {\n if (ctx.composite.mean < 0.3) {\n const tail = ctx.composite.tailRuns ?? []\n const names = tail\n .slice(0, 5)\n .map((t) => `${t.runId}=${t.score.toFixed(3)}`)\n .join(', ')\n out.push({\n priority: 'critical',\n kind: 'investigate',\n title: `Composite mean ${ctx.composite.mean.toFixed(3)} is below the 0.3 floor — the agent is broken on this corpus`,\n detail:\n tail.length > 0\n ? `Worst ${tail.length} run${tail.length === 1 ? '' : 's'} to inspect first: ${names}. Histogram p50=${ctx.composite.p50.toFixed(3)}, p95=${ctx.composite.p95.toFixed(3)}.`\n : `Histogram p50=${ctx.composite.p50.toFixed(3)}, p95=${ctx.composite.p95.toFixed(3)}.`,\n evidencePath: 'composite.tailRuns',\n })\n } else if (ctx.composite.mean < 0.5) {\n const tail = ctx.composite.tailRuns ?? []\n const names = tail\n .slice(0, 3)\n .map((t) => `${t.runId}=${t.score.toFixed(3)}`)\n .join(', ')\n out.push({\n priority: 'high',\n kind: 'investigate',\n title: `Composite mean ${ctx.composite.mean.toFixed(3)} is below 0.5 — investigate the lower tail before claiming the agent is healthy`,\n detail:\n tail.length > 0\n ? `Worst ${tail.length} run${tail.length === 1 ? '' : 's'}: ${names}. Histogram p50=${ctx.composite.p50.toFixed(3)}, p95=${ctx.composite.p95.toFixed(3)}.`\n : `Histogram p50=${ctx.composite.p50.toFixed(3)}, p95=${ctx.composite.p95.toFixed(3)}.`,\n evidencePath: 'composite.tailRuns',\n })\n }\n }\n\n // A healthy-looking mean can hide a group of failed tasks sharing one\n // producer-reported cause. This path does not require an analyst.\n if (ctx.failureClasses && ctx.failureClasses.length > 0) {\n const top = ctx.failureClasses[0]!\n if (top.count >= 3 && top.share >= 0.15) {\n out.push({\n priority: top.share >= 0.25 ? 'high' : 'medium',\n kind: 'investigate',\n title: `'${top.failureClass}' is the dominant failure class — ${top.count} runs (${(top.share * 100).toFixed(0)}% of the corpus)`,\n detail: `The mean composite can look acceptable while one failure class dominates the lower tail. ${top.count} of ${ctx.composite.n} runs failed with '${top.failureClass}'${ctx.failureClasses.length > 1 ? ` (next: '${ctx.failureClasses[1]!.failureClass}' ×${ctx.failureClasses[1]!.count})` : ''}. Fix this cause first.`,\n evidencePath: 'failureClasses',\n })\n }\n }\n\n // Missing-judges branch. The report can't surface per-dimension or\n // calibration signal when `outcome.judgeScores` is empty across the\n // corpus. Tell the customer how to enrich.\n if (Object.keys(ctx.judges).length === 0 && ctx.composite.n > 0) {\n out.push({\n priority: 'medium',\n kind: 'expand-corpus',\n title: 'No judge scores recorded — per-dimension + calibration insights unavailable',\n detail:\n 'Records have no `outcome.judgeScores`. To unlock perDimension, judges, and calibration, attach a Judge run during your eval pass and populate `outcome.judgeScores.perJudge[judgeName][dimension] = score`. See `docs/insight-report.md` for the expected shape.',\n evidencePath: 'judges',\n })\n }\n\n if (ctx.lift) {\n if (!ctx.lift.decisionEligible) {\n out.push({\n priority: 'high',\n kind: 'expand-corpus',\n title: `Inconclusive — ${ctx.lift.n} paired runs; ${ctx.lift.minimumRequired} required`,\n detail: `The bootstrap interval is descriptive below ${ctx.lift.minimumRequired} paired observations and cannot support a ship decision.`,\n evidencePath: 'lift',\n })\n } else {\n const pairedEffect =\n ctx.lift.cohensD === null ? 'undefined (zero delta variance)' : ctx.lift.cohensD.toFixed(2)\n const pairedP =\n ctx.lift.pValue === null ? 'undefined (zero delta variance)' : ctx.lift.pValue.toFixed(4)\n const requiredRuns =\n ctx.lift.requiredN === null ? 'not estimable' : `~${ctx.lift.requiredN} paired runs`\n // A ZERO-WIDTH interval never reads as \"ship\": n identical paired deltas\n // make every resample identical, so `[g, g]` clears any threshold below g\n // and `[0, 0]` clears any negative `decisionThreshold`, on no spread at\n // all. It falls through to the inconclusive/hold arms, which is where a\n // sample carrying no information about its own error belongs.\n const decisive = !zeroWidth(ctx.lift.ci95) && ctx.lift.ci95[0] > ctx.threshold\n const inconclusive = ctx.lift.ci95[0] <= ctx.threshold && ctx.lift.ci95[1] > ctx.threshold\n if (decisive) {\n out.push({\n priority: 'critical',\n kind: 'ship',\n title: `Ship — lift ${ctx.lift.delta.toFixed(3)} (95% CI ${ctx.lift.ci95[0].toFixed(3)}..${ctx.lift.ci95[1].toFixed(3)})`,\n detail: `Holdout lift exceeds threshold ${ctx.threshold} with 95% bootstrap confidence (n=${ctx.lift.n}, p=${pairedP}, paired d=${pairedEffect}).`,\n evidencePath: 'lift',\n })\n } else if (inconclusive) {\n out.push({\n priority: 'high',\n kind: 'expand-corpus',\n title: `Inconclusive — required sample is ${requiredRuns} (have ${ctx.lift.n}) at current effect size`,\n detail: `CI straddles threshold. Current MDE at 80% power is ${ctx.lift.mde.toFixed(3)}; observed delta is ${ctx.lift.delta.toFixed(3)}.`,\n evidencePath: 'lift',\n })\n } else {\n out.push({\n priority: 'critical',\n kind: 'hold',\n title: `Hold — lift CI lower bound ${ctx.lift.ci95[0].toFixed(3)} is at or below threshold ${ctx.threshold}`,\n detail: `Bootstrap CI provides no statistical evidence the candidate is better. Consider tightening the mutation or expanding the holdout.`,\n evidencePath: 'lift',\n })\n }\n }\n }\n\n if (ctx.contamination && ctx.contamination.leaks > 0) {\n out.push({\n priority: 'critical',\n kind: 'fix',\n title: `${ctx.contamination.leaks} canary leak${ctx.contamination.leaks === 1 ? '' : 's'} detected`,\n detail: `Holdout integrity is compromised. The lift number is unreliable until you investigate.`,\n evidencePath: 'contamination',\n })\n }\n\n if (ctx.interRater && ctx.interRater.kappa < 0.5) {\n out.push({\n priority: 'high',\n kind: 'recalibrate',\n title: `Inter-rater weighted kappa ${ctx.interRater.kappa.toFixed(2)} is below 0.5`,\n detail:\n 'Raters disagree on what good looks like. Review the largest disagreement cases and refine the rubric before automating these decisions.',\n evidencePath: 'interRater',\n })\n }\n\n if (ctx.failureClusters && ctx.failureClusters.clusters.length > 0) {\n const top = ctx.failureClusters.clusters[0]!\n out.push({\n priority: 'high',\n kind: 'investigate',\n title: `Top failure cluster: ${top.name} (${(top.share * 100).toFixed(0)}% of failures)`,\n detail: `${ctx.failureClusters.totalFailures} runs failed. The largest cluster groups ${top.exemplars.length} exemplars under '${top.name}'.`,\n evidencePath: 'failureClusters.clusters[0]',\n })\n }\n\n if (ctx.outcomeCorrelation && Math.abs(ctx.outcomeCorrelation.spearman) < 0.3) {\n out.push({\n priority: 'medium',\n kind: 'recalibrate',\n title: `Judge scores decoupled from ${ctx.outcomeCorrelation.metric} (Spearman ρ=${ctx.outcomeCorrelation.spearman.toFixed(2)})`,\n detail: `Your judges score what they were trained to score, but it isn't predicting downstream ${ctx.outcomeCorrelation.metric}. Consider retraining the judge against ${ctx.outcomeCorrelation.metric} as the gold signal.`,\n evidencePath: 'outcomeCorrelation',\n })\n }\n\n return out\n}\n\n// ── Re-export pareto figure spec for hosted-side rendering ─────────\n\nexport type { ParetoFigureSpec }\n","/**\n * Run one complete improvement job.\n *\n * A caller-owned `proposer` can generate candidates across local generations.\n * An external `method`, such as official GEPA or SkillOpt, owns its complete\n * search and returns one candidate. Both paths remeasure the selected candidate\n * against cases that candidate generation never receives.\n */\n\nimport type { ProposalFinding } from '../analyst/types'\nimport { defaultProductionGate } from '../campaign/gates/default-production-gate'\nimport { type PowerPreflight, powerPreflight } from '../campaign/gates/power-preflight'\nimport {\n assertOptimizationResult,\n type OptimizationMethod,\n type OptimizationMethodProvenance,\n type OptimizationMethodResult,\n} from '../campaign/presets/compare-optimization-methods'\nimport {\n type RunImprovementLoopResult,\n runImprovementLoop,\n} from '../campaign/presets/run-improvement-loop'\nimport type {\n PremeasuredOptimizationBaseline,\n RunOptimizationOptions,\n} from '../campaign/presets/run-optimization'\nimport {\n emitLoopProvenance,\n type LoopProvenanceRecord,\n loopProvenanceArgsFromResult,\n} from '../campaign/provenance'\nimport { resolveRunDir } from '../campaign/run-dir'\nimport {\n campaignCellExecutionEvidence,\n campaignCellJudgeDimensions,\n campaignCellTaskScore,\n campaignCellToRunRecord,\n} from '../campaign/run-record'\nimport {\n type CampaignStorage,\n createRunCostLedger,\n fsCampaignStorage,\n inMemoryCampaignStorage,\n} from '../campaign/storage'\nimport { surfaceContentHash, surfaceHash } from '../campaign/surface-identity'\nimport type {\n CampaignCellResult,\n DispatchContext,\n Gate,\n JudgeConfig,\n LabeledScenarioStore,\n MutableSurface,\n Scenario,\n SurfaceProposer,\n} from '../campaign/types'\nimport type { CostLedgerHandle, CostLedgerSummary, CostReceipt } from '../cost-ledger'\nimport { ValidationError } from '../errors'\nimport { createHostedClient, type HostedTenant } from '../hosted/client'\nimport type { EvalRunCellScore, EvalRunEvent, EvalRunGenerationSnapshot } from '../hosted/types'\nimport { modelHasSnapshot, type RunRecord, type RunSplitTag } from '../run-record'\nimport { analyzeRuns } from './analyze-runs'\nimport type { InsightReport } from './insight-report'\n\nexport interface SelfImproveBudget {\n /** Hard spend cap across the full run. Each paid call reserves its enforced\n * maximum before dispatch, so completed spend cannot cross this amount. */\n dollars?: number\n /** Proposer generations. Default: 3. External methods own their rounds and\n * require this value to be omitted or set to 1. Set 0 only for a\n * proposer-free baseline run. */\n generations?: number\n /** Candidates the proposer emits per generation. Default 2. */\n populationSize?: number\n /** Max concurrent cells across the loop. Default 2. */\n maxConcurrency?: number\n /** Candidate campaigns scored in parallel. Default 1. Total concurrent\n * cells are bounded by `candidateConcurrency * maxConcurrency`. */\n candidateConcurrency?: number\n /** Fraction of `scenarios` held out from training, used for the gate.\n * Default 0.25. Ignored when `holdoutScenarios` is set explicitly. */\n holdoutFraction?: number\n /** Fraction of the non-final cases reserved for method selection.\n * Default 0.25. Used only with `method` and ignored when\n * `selectionScenarios` is supplied explicitly. */\n selectionFraction?: number\n /** Explicit held-out scenarios; overrides `holdoutFraction`. */\n holdoutScenarios?: Scenario[]\n /** Holdout policy. Default `'measured'`: split, re-score baseline vs winner\n * on the held-out set, gate on that comparison. `'deferred'`: run the\n * improvement-set campaigns + search promotion, dispatch ZERO holdout cells,\n * force the gate to `'hold'`, return `lift: undefined`, and record\n * `holdout: 'deferred'` in the provenance record — for callers that measure\n * the held-out comparison in a separate later run instead of faking a\n * static holdout scenario and recording a meaningless lift. Unless\n * `holdoutScenarios` reserves an explicit set, ALL scenarios train. */\n holdout?: 'measured' | 'deferred'\n /** Per-scenario replicates per cell — raises bootstrap-CI tightness. Default 1. */\n reps?: number\n /** DEPTH dial forwarded to the proposer's `propose()` as\n * `ctx.maxImprovementShots` — max iterations an agentic candidate generator\n * may take per candidate (verify-in-session retries). Unset ⇒ the\n * proposer's own default. */\n maxImprovementShots?: number\n}\n\nexport type SelfImproveProgressEvent =\n | { kind: 'baseline.started'; scenarios: number }\n | { kind: 'baseline.completed'; compositeMean: number; durationMs: number }\n | { kind: 'generation.started'; index: number; populationSize: number }\n | { kind: 'generation.completed'; index: number; bestComposite: number; durationMs: number }\n // `lift` is absent when `budget.holdout === 'deferred'` — no held-out\n // measurement ran, and the search-split delta must not masquerade as one.\n | { kind: 'gate.decided'; decision: string; lift?: number }\n | { kind: 'power.estimated'; n: number; sd: number; mde: number; underpowered: boolean }\n\nexport interface SelfImproveOptions<TScenario extends Scenario, TArtifact> {\n /**\n * Your agent — a function that takes the current `MutableSurface`\n * (typically a system prompt the loop is optimizing) plus the\n * scenario + cell ctx, and returns the artifact your judge scores.\n *\n * Same shape as `RunOptimizationOptions.dispatchWithSurface`. Wrap a\n * plain `Dispatch` if you don't have a surface seam:\n *\n * agent: (_surface, scenario, ctx) => yourPlainDispatch(scenario, ctx)\n *\n * That mode evaluates without mutating any surface — useful as a\n * baseline-only run (set `budget.generations = 0`).\n */\n agent: (surface: MutableSurface, scenario: TScenario, ctx: DispatchContext) => Promise<TArtifact>\n\n /**\n * Snapshot-bearing model identity for agents that do not report a paid-call\n * receipt through `ctx.cost.runPaidCall()`.\n *\n * Omit this when every cell reports its concrete model in a receipt.\n */\n model?: string\n\n /** Scenarios to evaluate against. Train/holdout split is computed from\n * these unless `budget.holdoutScenarios` is set explicitly. */\n scenarios: TScenario[]\n\n /** Judge that scores artifacts. Bring your own; use `langchainJudge`\n * from `/adapters/langchain` for a Runnable-shaped one. */\n judge: JudgeConfig<TArtifact, TScenario>\n\n /** Starting surface — system prompt, JSON config, anything `MutableSurface`\n * accepts. The proposer mutates this each generation. */\n baselineSurface: MutableSurface\n\n /** Budget + loop shape. All fields optional. */\n budget?: SelfImproveBudget\n\n /**\n * Complete prior measurement of `baselineSurface` over the TRAIN split.\n * Forwarded to the loop body, which validates its surface hash, scenario\n * split, seed (42), reps, and coverage, then skips the baseline search\n * campaign entirely — no baseline dispatch, no resumability lookup. The\n * train split is `scenarios` minus the holdout split, so premeasure with\n * exactly that scenario set (explicit `budget.holdoutScenarios`, or\n * `budget.holdout: 'deferred'` with no reserved set, makes the train split\n * deterministic). Prior spend stays in the imported campaign aggregates and\n * is not re-added to this run's cost ledger.\n */\n premeasuredBaseline?: PremeasuredOptimizationBaseline<TArtifact, TScenario>\n\n /**\n * Candidate generator for this local generation loop.\n * Required when `budget.generations` is greater than zero.\n */\n proposer?: SurfaceProposer<ProposalFinding>\n\n /**\n * Complete optimization method, such as official GEPA or SkillOpt.\n * The method receives disjoint train and selection cases and never receives\n * the final comparison cases. Mutually exclusive with `proposer`.\n */\n method?: OptimizationMethod<TScenario, TArtifact>\n\n /** Explicit method-selection cases. They must also appear in `scenarios`\n * and must not overlap the final comparison cases. */\n selectionScenarios?: TScenario[]\n\n /** Custom gate. Default is `defaultProductionGate` with\n * `deltaThreshold: 0.05` on the held-out split. */\n gate?: Gate<TArtifact, TScenario>\n\n /** Placebo control. When supplied AND the winner differs from baseline, the\n * loop scores a THIRD held-out arm: the winner surface with its content\n * footprint-matched-blanked by this fn (typically via `neutralizeText`). Its\n * scores reach the gate as `ctx.neutralizedJudgeScores`, letting a\n * `neutralizationGate` reject a win whose lift survives blanking the content\n * (decorative — driven by footprint, not content). Costs one extra held-out\n * campaign; omit to skip. Compose `neutralizationGate` into `gate` to act on it. */\n neutralize?: (winnerSurface: MutableSurface, baselineSurface: MutableSurface) => MutableSurface\n\n /** Storage backend. A filesystem run directory uses `fsCampaignStorage()`;\n * a `mem://` directory uses in-memory storage. External methods default to\n * a filesystem directory because their official state must survive. */\n storage?: CampaignStorage\n\n /** Run directory. Proposer mode defaults to\n * `mem://selfImprove-<timestamp>`. External method mode defaults to\n * `.agent-eval/runs/self-improve-<timestamp>`. */\n runDir?: string\n\n /** Fires once the durable provenance record + OTel spans are emitted.\n * Receives the structured record for inline assertions / custom routing. */\n onProvenance?: (record: LoopProvenanceRecord) => void\n\n /** Distributed execution seam — same as `RunCampaignOptions.cellPlacement`.\n * Returns an opaque placement key the substrate forwards to your agent\n * as `ctx.placement`. Combined with `httpDispatch` from\n * `/adapters/http`, fans cells across regions. */\n cellPlacement?: (input: {\n scenario: TScenario\n rep: number\n generation?: number\n }) => string | undefined\n\n /** Per-cell agent dispatch deadline, applied to baseline, candidate, and\n * held-out campaigns. Default 600_000 ms. Set 0 to disable. */\n dispatchTimeoutMs?: number\n\n /** Streaming hook — fires on baseline + each generation + gate decision.\n * Consumer routes events wherever (UI, dashboard, logs). */\n onProgress?: (event: SelfImproveProgressEvent) => void\n\n /** Auto-promotion behavior on a ship decision. Default `'none'` — we\n * return the winner; you ship it however you ship. `'pr'` opens a\n * GitHub PR via `openAutoPr`; requires `ghOwner` + `ghRepo`. */\n autoOnPromote?: 'pr' | 'none'\n ghOwner?: string\n ghRepo?: string\n\n /**\n * Opt-in: ship eval-run events to a hosted orchestrator (ours, your\n * self-hosted one, or any compatible implementation of the\n * `docs/hosted-ingest-spec.md` wire format). When set, the substrate\n * POSTs the final `EvalRunEvent` to `${endpoint}/v1/ingest/eval-runs`\n * after the loop completes. Failures are logged but do not fail the\n * loop — local result is always returned.\n *\n * For our orchestrator: `{ endpoint: 'https://orchestrator.tangle.tools/v1', apiKey, tenantId }`.\n *\n * For your self-hosted: any URL serving the wire format. See\n * `examples/hosted-ingest-server/` for the reference receiver.\n */\n hostedTenant?: HostedTenant\n\n /** Free-form labels attached to the hosted event (env, branch, model id,\n * etc.). Ignored when `hostedTenant` is unset. */\n hostedLabels?: Record<string, string>\n\n /** Capture every search artifact and judge score to this store.\n * The store is output only and is never exposed to candidate generation.\n * Pass `'off'` to disable. Default: off. */\n labeledStore?: LabeledScenarioStore | 'off'\n\n /** Capture-source tag for `labeledStore`. Default `'eval-run'`. */\n captureSource?: 'production-trace' | 'eval-run' | 'manual' | 'red-team' | 'synthetic'\n\n /**\n * Per-cell backend-integrity expectation — the fail-loud guard. A cell that\n * produced an artifact but reported `costUsd === 0` AND zero tokens is a\n * stub. Modes: `'assert'` throws on the first such cell, `'warn'` logs it,\n * `'off'` skips the check (offline/replay). Default `'assert'` — `selfImprove`\n * is the real-run path, so a stub fails loud rather than scoring a clean 0.\n */\n expectUsage?: 'assert' | 'warn' | 'off'\n\n /**\n * Per-generation findings producer. Runs once on the baseline campaign (as\n * `generation: -1`) before generation 0 proposes — so single-generation runs\n * propose with trace context — and again after each generation is scored;\n * whatever it returns REPLACES the proposer's `findings` for the next\n * `propose()`. Plug a trace-analyst registry / HALO here. When absent,\n * findings stay `opts.findings`.\n */\n analyzeGeneration?: RunOptimizationOptions<TScenario, TArtifact>['analyzeGeneration']\n\n /** Static findings forwarded to the proposer's `propose()` as `ctx.findings`\n * (a findings-grounded proposer consumes them). Default: none. */\n findings?: ProposalFinding[]\n\n /** Override how the WINNER is selected among coverage-complete candidates.\n * Defaults to the scalar mean composite (historical behavior). A binary-with-\n * replicates consumer whose ship-gate counts an instance resolved only when\n * every replicate resolved passes a fail-closed lexicographic key here so that\n * winner-selection and the ship-gate rank on the identical metric and cannot\n * invert. See `RunOptimizationOptions.selectionRankKey`. */\n selectionRankKey?: RunOptimizationOptions<TScenario, TArtifact>['selectionRankKey']\n}\n\nexport interface SelfImproveResult<TScenario extends Scenario, TArtifact> {\n /** Composite mean across all scenarios, baseline run. When\n * `budget.holdout === 'deferred'` this is measured on the improvement\n * (search) split — no holdout campaign ran. */\n baseline: {\n compositeMean: number\n perScenario: Record<string, number>\n }\n /** Composite mean on the held-out set, winner run. When\n * `budget.holdout === 'deferred'` this is the winner's improvement-set\n * (search) measurement — no holdout campaign ran. */\n winner: {\n compositeMean: number\n perScenario: Record<string, number>\n surface: MutableSurface\n /** Proposer label for the promoted change. Absent ⇒ winner == baseline or\n * a bare-surface mutator. */\n label?: string\n /** Proposer rationale — the \"because Z\" that motivated the promoted change.\n * Threaded from the proposer's `ProposedCandidate` through the loop.\n * Absent ⇒ winner == baseline. */\n rationale?: string\n }\n /** `winner.compositeMean - baselineOnHoldout.compositeMean`. Positive\n * means the gate observed improvement. Absent iff\n * `budget.holdout === 'deferred'` — no held-out measurement ran, so there\n * is no lift to report (never a fabricated 0). */\n lift?: number\n /** The explicit baseline→winner unified diff. Always present (empty string\n * when winner == baseline). */\n diff: string\n /** Durable, queryable provenance record: candidate→cell→gate→promote chain +\n * rationale + diff + backend provenance. The artifact the hosted ingest\n * path stores; the +lift RECOMPUTES from `record.heldOutLift`. */\n provenance: LoopProvenanceRecord\n /** `defaultProductionGate.decide()` result. */\n gateDecision: 'ship' | 'hold' | 'need_more_work' | 'model_ceiling' | 'arch_ceiling'\n /** Number of generations actually explored (may be less than the\n * budget if the proposer gave up early). */\n generationsExplored: number\n /** Wall-clock total. */\n durationMs: number\n /** Total newly observed cost across the full run. */\n totalCostUsd: number\n /** Canonical run-wide spend summary. */\n cost: CostLedgerSummary\n /** Run-wide receipts across proposal, search, holdout, judging, analysis,\n * and promotion work, with phase and actor attribution. */\n receipts: CostReceipt[]\n /** Exact external method and source identity, when `method` was used. */\n optimization?: {\n name: string\n cost: OptimizationMethodResult['cost']\n durationMs?: number\n provenance?: OptimizationMethodProvenance\n }\n /**\n * Rigor packet: distributional summary, paired-bootstrap lift CI,\n * judge stats, contamination check, recommendations. Wired through\n * `analyzeRuns()` on the baseline + winner cells of the campaign.\n * Hosted-tier dashboards render this as the v3-vs-v4 decision view.\n */\n insight: InsightReport\n /** Minimum-detectable-lift analysis from the baseline holdout cells: could this\n * budget have shipped ANY plausible effect? Absent when the baseline produced\n * fewer than 3 scored holdout cells. See `powerPreflight` for the standalone\n * pre-run version (run `gate: 'none'` first, budget the real search after). */\n power?: PowerPreflight\n /**\n * Raw substrate result for advanced inspection — full per-generation\n * candidates, full campaign artifacts, all judge scores. Useful for\n * debugging or reporting beyond the summary.\n */\n raw: RunImprovementLoopResult<TArtifact, TScenario>\n}\n\n/** Failed self-improvement run with an immutable receipt snapshot. */\nexport class SelfImproveRunError extends Error {\n readonly cost: CostLedgerSummary\n readonly receipts: CostReceipt[]\n\n constructor(cause: unknown, ledger: CostLedgerHandle) {\n const original = cause instanceof Error ? cause : new Error(String(cause))\n super(original.message, { cause: original })\n this.name = 'SelfImproveRunError'\n this.cost = ledger.summary()\n this.receipts = ledger.list()\n }\n}\n\nfunction assertSelfImproveSearchMode<TScenario extends Scenario, TArtifact>(\n opts: SelfImproveOptions<TScenario, TArtifact>,\n): void {\n if (opts.method && opts.proposer) {\n throw new Error('selfImprove: method and proposer are mutually exclusive')\n }\n if (!opts.method) {\n if (opts.selectionScenarios !== undefined) {\n throw new Error('selfImprove: selectionScenarios requires method')\n }\n return\n }\n if (\n typeof opts.method.name !== 'string' ||\n !opts.method.name.trim() ||\n opts.method.name.trim() !== opts.method.name ||\n typeof opts.method.optimize !== 'function'\n ) {\n throw new Error('selfImprove: method must have a trimmed name and optimize(input)')\n }\n const budget = opts.budget\n if (budget?.generations !== undefined && budget.generations !== 1) {\n throw new Error('selfImprove: method owns its rounds; budget.generations must be 1 when set')\n }\n if (budget?.populationSize !== undefined && budget.populationSize !== 1) {\n throw new Error(\n 'selfImprove: method owns its candidates; budget.populationSize must be 1 when set',\n )\n }\n if (\n budget?.candidateConcurrency !== undefined ||\n budget?.maxImprovementShots !== undefined ||\n opts.analyzeGeneration !== undefined ||\n opts.findings !== undefined\n ) {\n throw new Error(\n 'selfImprove: candidateConcurrency, maxImprovementShots, analyzeGeneration, and findings apply only to proposer mode',\n )\n }\n}\n\nfunction splitMethodPartitions<TScenario extends Scenario>(\n searchScenarios: TScenario[],\n explicitSelection: TScenario[] | undefined,\n fraction: number,\n): { train: TScenario[]; selection: TScenario[] } {\n if (!Number.isFinite(fraction) || fraction <= 0 || fraction >= 1) {\n throw new Error('selfImprove: budget.selectionFraction must be in (0, 1)')\n }\n const byId = new Map<string, TScenario>()\n for (const scenario of searchScenarios) {\n if (byId.has(scenario.id)) {\n throw new Error(`selfImprove: duplicate scenario id '${scenario.id}'`)\n }\n byId.set(scenario.id, scenario)\n }\n if (explicitSelection) {\n if (explicitSelection.length === 0) {\n throw new Error('selfImprove: selectionScenarios must not be empty')\n }\n const selectionIds = new Set<string>()\n for (const scenario of explicitSelection) {\n if (!byId.has(scenario.id)) {\n throw new Error(\n `selfImprove: selection scenario '${scenario.id}' is absent from the non-final cases`,\n )\n }\n if (selectionIds.has(scenario.id)) {\n throw new Error(`selfImprove: duplicate selection scenario id '${scenario.id}'`)\n }\n selectionIds.add(scenario.id)\n }\n const train = searchScenarios.filter((scenario) => !selectionIds.has(scenario.id))\n if (train.length === 0) {\n throw new Error('selfImprove: method train split is empty')\n }\n return {\n train,\n selection: explicitSelection.map((scenario) => byId.get(scenario.id)!),\n }\n }\n if (searchScenarios.length < 2) {\n throw new Error('selfImprove: method requires at least two non-final scenarios')\n }\n const sorted = [...searchScenarios].sort(\n (a, b) => stableScenarioHash(a.id) - stableScenarioHash(b.id),\n )\n const count = Math.max(1, Math.min(sorted.length - 1, Math.round(sorted.length * fraction)))\n return {\n selection: sorted.slice(0, count),\n train: sorted.slice(count),\n }\n}\n\nfunction safeRunComponent(value: string): string {\n return value.replace(/[^a-zA-Z0-9._-]/g, '_')\n}\n\nfunction stableScenarioHash(value: string): number {\n let hash = 2166136261 >>> 0\n for (let index = 0; index < value.length; index++) {\n hash ^= value.charCodeAt(index)\n hash = Math.imul(hash, 16777619) >>> 0\n }\n return hash\n}\n\n/**\n * Deterministic train/holdout split by a stable hash of `scenario.id`,\n * so the same scenario set always splits the same way across runs.\n */\nfunction splitTrainHoldout<TScenario extends Scenario>(\n scenarios: TScenario[],\n fraction: number,\n): { train: TScenario[]; holdout: TScenario[] } {\n const sorted = [...scenarios].sort((a, b) => stableScenarioHash(a.id) - stableScenarioHash(b.id))\n const nHoldout = Math.max(1, Math.min(sorted.length - 1, Math.round(sorted.length * fraction)))\n return {\n holdout: sorted.slice(0, nHoldout),\n train: sorted.slice(nHoldout),\n }\n}\n\nfunction meanComposite(byScenario: Record<string, { meanComposite: number }>): {\n compositeMean: number\n perScenario: Record<string, number>\n} {\n const perScenario: Record<string, number> = {}\n const values: number[] = []\n for (const [id, agg] of Object.entries(byScenario)) {\n perScenario[id] = agg.meanComposite\n values.push(agg.meanComposite)\n }\n return {\n compositeMean: values.length === 0 ? 0 : values.reduce((s, v) => s + v, 0) / values.length,\n perScenario,\n }\n}\n\n/**\n * Latest search campaign measured for the winner surface; the baseline search\n * campaign when the winner IS the baseline. Used by the deferred-holdout\n * summary, where no holdout campaign exists to summarize.\n */\nfunction winnerSearchCampaign<TScenario extends Scenario, TArtifact>(\n result: RunImprovementLoopResult<TArtifact, TScenario>,\n): RunImprovementLoopResult<TArtifact, TScenario>['baselineCampaign'] {\n for (let i = result.generations.length - 1; i >= 0; i--) {\n const measured = result.generations[i]?.surfaces.find(\n (s) => s.surfaceHash === result.winnerSurfaceHash,\n )\n if (measured) return measured.campaign\n }\n return result.baselineCampaign\n}\n\n/**\n * One-shot self-improvement loop. See module docstring for defaults +\n * extension points.\n *\n * @example Minimum:\n *\n * const result = await selfImprove({\n * agent: (surface, scenario, ctx) => myAgent(surface, scenario, ctx.signal),\n * scenarios,\n * judge,\n * baselineSurface: DEFAULT_PROMPT,\n * proposer,\n * })\n * console.log(`lift: ${result.lift.toFixed(3)} (${result.gateDecision})`)\n *\n * @example Distributed (workers in three regions):\n *\n * await selfImprove({\n * agent: httpDispatch({ resolveUrl: ({ placement }) => REGION_URLS[placement!] }),\n * scenarios,\n * judge,\n * baselineSurface: DEFAULT_PROMPT,\n * cellPlacement: ({ scenario }) => scenario.region,\n * budget: { maxConcurrency: 12 },\n * })\n */\nexport async function selfImprove<TScenario extends Scenario, TArtifact>(\n opts: SelfImproveOptions<TScenario, TArtifact>,\n): Promise<SelfImproveResult<TScenario, TArtifact>> {\n const startedAt = Date.now()\n const requestedRunDir =\n opts.runDir ??\n (opts.method ? `.agent-eval/runs/self-improve-${startedAt}` : `mem://selfImprove-${startedAt}`)\n const runDir = resolveRunDir(requestedRunDir)\n const storage =\n opts.storage ?? (runDir.startsWith('mem://') ? inMemoryCampaignStorage() : fsCampaignStorage())\n const costLedger = createRunCostLedger({\n storage,\n runDir,\n costCeilingUsd: opts.budget?.dollars,\n })\n try {\n return await runSelfImprove(opts, costLedger, startedAt, runDir, storage)\n } catch (error) {\n throw new SelfImproveRunError(error, costLedger)\n }\n}\n\nasync function runSelfImprove<TScenario extends Scenario, TArtifact>(\n opts: SelfImproveOptions<TScenario, TArtifact>,\n costLedger: CostLedgerHandle,\n startedAt: number,\n runDir: string,\n storage: CampaignStorage,\n): Promise<SelfImproveResult<TScenario, TArtifact>> {\n const budget = opts.budget ?? {}\n assertSelfImproveSearchMode(opts)\n const generations = opts.method ? 1 : (budget.generations ?? 3)\n const populationSize = opts.method ? 1 : (budget.populationSize ?? 2)\n const maxConcurrency = budget.maxConcurrency ?? 2\n const holdoutFraction = budget.holdoutFraction ?? 0.25\n const holdoutMode = budget.holdout ?? 'measured'\n const holdoutDeferred = holdoutMode === 'deferred'\n const expectUsage = opts.expectUsage ?? 'assert'\n\n // Deferred holdout without an explicitly reserved set trains on EVERYTHING:\n // there is no held-out measurement in this run, so carving out a fraction\n // would waste scenarios. An explicit `holdoutScenarios` set stays reserved\n // (excluded from training) even when deferred, for the later measured run.\n const explicitHoldout = budget.holdoutScenarios\n const { train, holdout } = explicitHoldout\n ? {\n train: opts.scenarios.filter((s) => !explicitHoldout.some((h) => h.id === s.id)),\n holdout: explicitHoldout as TScenario[],\n }\n : holdoutDeferred\n ? { train: opts.scenarios, holdout: [] as TScenario[] }\n : splitTrainHoldout(opts.scenarios, holdoutFraction)\n\n if (train.length === 0) {\n throw new Error(\n 'selfImprove: train split is empty. Reduce holdoutFraction or pass more scenarios.',\n )\n }\n if (holdout.length === 0 && !holdoutDeferred) {\n throw new Error('selfImprove: holdout split is empty. Pass more scenarios.')\n }\n\n if (generations > 0 && !opts.proposer && !opts.method) {\n throw new Error(\n 'selfImprove: method or proposer is required when budget.generations is greater than zero',\n )\n }\n let optimizationResult: OptimizationMethodResult | undefined\n const methodPartitions = opts.method\n ? splitMethodPartitions(train, opts.selectionScenarios, budget.selectionFraction ?? 0.25)\n : undefined\n const proposer: SurfaceProposer<ProposalFinding> = opts.method\n ? {\n kind: `method:${opts.method.name}`,\n propose: async (context) => {\n if (context.generation > 0) return []\n const result = await opts.method!.optimize(\n Object.freeze({\n baselineSurface: structuredClone(context.currentSurface),\n trainScenarios: Object.freeze(\n methodPartitions!.train.map((scenario) => structuredClone(scenario)),\n ),\n selectionScenarios: Object.freeze(\n methodPartitions!.selection.map((scenario) => structuredClone(scenario)),\n ),\n dispatchWithSurface: opts.agent,\n judges: Object.freeze([opts.judge]),\n runDir: `${runDir}/optimization/${safeRunComponent(opts.method!.name)}`,\n seed: 42,\n runOptions: Object.freeze({\n storage,\n maxConcurrency,\n reps: budget.reps,\n dispatchTimeoutMs: opts.dispatchTimeoutMs,\n expectUsage,\n costCeiling: budget.dollars,\n }),\n costLedger,\n }),\n )\n assertOptimizationResult(opts.method!.name, result)\n optimizationResult = structuredClone(result)\n return [\n {\n surface: structuredClone(result.winnerSurface),\n label: opts.method!.name,\n rationale: `${opts.method!.name} selected this surface without final cases.`,\n },\n ]\n },\n }\n : (opts.proposer ?? {\n kind: 'baseline-only',\n propose: async () => [],\n })\n\n const gate: Gate<TArtifact, TScenario> =\n opts.gate ??\n defaultProductionGate<TArtifact, TScenario>({\n holdoutScenarios: holdout,\n deltaThreshold: 0.05,\n })\n\n if (opts.onProgress) {\n opts.onProgress({ kind: 'baseline.started', scenarios: opts.scenarios.length })\n }\n\n const result = await runImprovementLoop<TScenario, TArtifact>({\n scenarios: train,\n baselineSurface: opts.baselineSurface,\n premeasuredBaseline: opts.premeasuredBaseline,\n dispatchWithSurface: opts.agent,\n proposer,\n judges: [opts.judge],\n populationSize,\n maxGenerations: generations,\n candidateConcurrency: budget.candidateConcurrency,\n reps: budget.reps,\n maxImprovementShots: budget.maxImprovementShots,\n holdoutScenarios: holdout,\n holdout: holdoutMode,\n gate,\n neutralize: opts.neutralize,\n autoOnPromote: opts.autoOnPromote ?? 'none',\n ghOwner: opts.ghOwner,\n ghRepo: opts.ghRepo,\n storage,\n runDir,\n maxConcurrency,\n cellPlacement: opts.cellPlacement,\n dispatchTimeoutMs: opts.dispatchTimeoutMs,\n costLedger,\n expectUsage,\n labeledStore: opts.labeledStore,\n captureSource: opts.captureSource,\n analyzeGeneration: opts.analyzeGeneration,\n findings: opts.findings,\n selectionRankKey: opts.selectionRankKey,\n })\n\n // Deferred holdout ran zero holdout cells, so the summary stats come from\n // the improvement-set (search) campaigns — labeled as such on the result\n // type — and `lift` is omitted rather than fabricated from empty campaigns.\n const reportSplit: RunSplitTag = holdoutDeferred ? 'search' : 'holdout'\n const reportBaselineCampaign = holdoutDeferred\n ? result.baselineCampaign\n : result.baselineOnHoldout\n const reportWinnerCampaign = holdoutDeferred\n ? winnerSearchCampaign(result)\n : result.winnerOnHoldout\n const baseline = meanComposite(reportBaselineCampaign.aggregates.byScenario)\n const winnerStats = meanComposite(reportWinnerCampaign.aggregates.byScenario)\n\n // Power analysis from the baseline holdout cells — the number that says whether\n // this budget could ship ANY effect. Attached to every result; loud when the\n // search was structurally unable to promote (that spend should not repeat).\n let power: PowerPreflight | undefined\n const baselineHoldoutComposites = result.baselineOnHoldout.cells\n .filter((cell) => !cell.error)\n .map((cell) => {\n const scores = Object.values(cell.judgeScores)\n return scores.length === 0\n ? Number.NaN\n : scores.reduce((sum, s) => sum + s.composite, 0) / scores.length\n })\n .filter((v) => Number.isFinite(v))\n if (baselineHoldoutComposites.length >= 3) {\n // selfImprove's holdout is scored by the SAME judge as the gate — the\n // shared-channel case by construction (S1c): flag it so the MDE reads as a\n // lower bound and nobody buys reps expecting them to fix judge bias.\n power = powerPreflight({\n baselineComposites: baselineHoldoutComposites,\n sharedScorerChannel: true,\n })\n if (opts.onProgress) {\n opts.onProgress({\n kind: 'power.estimated',\n n: power.n,\n sd: power.sd,\n mde: power.mde,\n underpowered: power.underpowered,\n })\n }\n if (power.underpowered && generations > 0) {\n console.warn(`[selfImprove] ${power.recommendation}`)\n }\n }\n\n if (opts.onProgress) {\n opts.onProgress({\n kind: 'baseline.completed',\n compositeMean: baseline.compositeMean,\n durationMs: Date.now() - startedAt,\n })\n opts.onProgress({\n kind: 'gate.decided',\n decision: result.gateResult.decision,\n // Deferred holdout has no held-out measurement: in that mode the summary\n // stats are search-split numbers, and emitting their delta as `lift`\n // would misreport a train-split delta as a held-out one. Omit instead.\n ...(holdoutDeferred ? {} : { lift: winnerStats.compositeMean - baseline.compositeMean }),\n })\n }\n\n const cost = result.cost\n const totalCost = cost.totalCostUsd\n\n // Rigor packet: feed baseline + winner cells through analyzeRuns().\n // The two candidates (`baseline` / `winner`) give the lift section a\n // clean paired comparison; per-judge / per-dimension / cost-quality\n // sections populate from the cells' judgeScores.\n const insight = await analyzeRuns({\n runs: [\n ...cellsToRunRecords(\n reportBaselineCampaign.cells,\n 'baseline',\n runDir,\n opts.baselineSurface,\n reportSplit,\n opts.model,\n ),\n ...cellsToRunRecords(\n reportWinnerCampaign.cells,\n 'winner',\n runDir,\n result.winnerSurface,\n reportSplit,\n opts.model,\n ),\n ],\n baselineCandidateId: 'baseline',\n candidateCandidateId: 'winner',\n })\n\n // ── Durable provenance: candidate→cell→gate→promote chain + rationale +\n // diff + backend provenance. Always emitted; the +lift recomputes from it.\n const durationMs = Date.now() - startedAt\n const { record: provenance } = await emitLoopProvenance<TArtifact, TScenario>({\n ...loopProvenanceArgsFromResult({\n runId: `${runDir}#${startedAt}`,\n runDir,\n timestamp: new Date(startedAt).toISOString(),\n baselineSurface: opts.baselineSurface,\n result,\n costReceipts: costLedger.list(),\n totalCostUsd: totalCost,\n totalDurationMs: durationMs,\n }),\n ...(optimizationResult\n ? {\n optimizationMethod: {\n name: opts.method!.name,\n cost: structuredClone(optimizationResult.cost),\n ...(optimizationResult.durationMs === undefined\n ? {}\n : { durationMs: optimizationResult.durationMs }),\n ...(optimizationResult.provenance === undefined\n ? {}\n : { provenance: structuredClone(optimizationResult.provenance) }),\n },\n }\n : {}),\n storage,\n hostedClient: opts.hostedTenant ? createHostedClient(opts.hostedTenant) : undefined,\n })\n if (opts.onProvenance) opts.onProvenance(provenance)\n\n const summary: SelfImproveResult<TScenario, TArtifact> = {\n baseline,\n winner: {\n ...winnerStats,\n surface: result.winnerSurface,\n ...(result.winnerLabel ? { label: result.winnerLabel } : {}),\n ...(result.winnerRationale ? { rationale: result.winnerRationale } : {}),\n },\n ...(holdoutDeferred ? {} : { lift: winnerStats.compositeMean - baseline.compositeMean }),\n diff: result.promotedDiff,\n provenance,\n gateDecision: result.gateResult.decision,\n generationsExplored: result.generations.length,\n durationMs,\n totalCostUsd: totalCost,\n cost,\n receipts: costLedger.list(),\n ...(optimizationResult\n ? {\n optimization: {\n name: opts.method!.name,\n cost: structuredClone(optimizationResult.cost),\n ...(optimizationResult.durationMs === undefined\n ? {}\n : { durationMs: optimizationResult.durationMs }),\n ...(optimizationResult.provenance === undefined\n ? {}\n : { provenance: structuredClone(optimizationResult.provenance) }),\n },\n }\n : {}),\n insight,\n ...(power ? { power } : {}),\n raw: result,\n }\n\n // Opt-in hosted ingest. Failures are logged but never fail the loop: the\n // local result is always returned.\n if (opts.hostedTenant) {\n try {\n await shipEvalRunToHosted(opts.hostedTenant, opts, summary, result, runDir)\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err)\n // eslint-disable-next-line no-console -- intentional: hosted-ingest is best-effort\n console.warn(`[agent-eval] hosted ingest failed (continuing): ${msg}`)\n }\n }\n\n return summary\n}\n\nasync function shipEvalRunToHosted<TScenario extends Scenario, TArtifact>(\n tenant: HostedTenant,\n opts: SelfImproveOptions<TScenario, TArtifact>,\n summary: SelfImproveResult<TScenario, TArtifact>,\n raw: RunImprovementLoopResult<TArtifact, TScenario>,\n runDir: string,\n): Promise<void> {\n const client = createHostedClient(tenant)\n\n function snapshotFromCampaign(\n index: number,\n surface: MutableSurface,\n campaign: RunImprovementLoopResult<TArtifact, TScenario>['baselineCampaign'],\n durationMs: number,\n ): EvalRunGenerationSnapshot {\n const cells: EvalRunCellScore[] = campaign.cells.map((cell) => {\n const execution = campaignCellExecutionEvidence(cell)\n return {\n scenarioId: cell.scenarioId,\n rep: cell.rep,\n compositeMean: campaignCellTaskScore(cell) ?? null,\n dimensions: campaignCellJudgeDimensions(cell),\n terminalOutcome: execution.terminalOutcome,\n executionErrorCount: execution.executionErrorCount ?? null,\n errorMessage: cell.error ?? undefined,\n }\n })\n const scoredCells = cells.flatMap((cell) =>\n cell.compositeMean === null ? [] : [cell.compositeMean],\n )\n const compositeMean =\n scoredCells.length === 0\n ? null\n : scoredCells.reduce((sum, score) => sum + score, 0) / scoredCells.length\n return {\n index,\n surfaceHash: surfaceHash(surface),\n surface,\n cells,\n compositeMean,\n costUsd: campaign.aggregates.cost.totalCostUsd,\n durationMs,\n }\n }\n\n const generations: EvalRunGenerationSnapshot[] = []\n // Baseline as generation 0.\n generations.push(snapshotFromCampaign(0, opts.baselineSurface, raw.baselineCampaign, 0))\n // Improvement generations as 1..N. Substrate stores per-surface campaigns\n // per generation — we summarize the WINNING surface per generation here.\n for (const gen of raw.generations) {\n const winner = gen.surfaces.reduce(\n (best, s) =>\n s.campaign.aggregates.cellsExecuted > 0 &&\n (best === undefined || averageComposite(s.campaign) > averageComposite(best.campaign))\n ? s\n : best,\n gen.surfaces[0],\n )\n if (!winner) continue\n generations.push(\n snapshotFromCampaign(gen.record.generationIndex + 1, winner.surface, winner.campaign, 0),\n )\n }\n\n const event: EvalRunEvent = {\n runId: `${runDir}#${Date.now()}`,\n runDir,\n timestamp: new Date().toISOString(),\n status: 'finished',\n labels: opts.hostedLabels ?? {},\n baseline: generations[0],\n generations,\n gateDecision: summary.gateDecision,\n holdoutLift: summary.lift,\n totalCostUsd: summary.totalCostUsd,\n totalDurationMs: summary.durationMs,\n insightReport: summary.insight,\n }\n\n await client.ingestEvalRun(event)\n}\n\nfunction averageComposite(\n campaign: RunImprovementLoopResult<unknown, Scenario>['baselineCampaign'],\n): number {\n const aggs = Object.values(campaign.aggregates.byScenario)\n return aggs.length === 0 ? 0 : aggs.reduce((s, a) => s + a.meanComposite, 0) / aggs.length\n}\n\nfunction hashString(s: string): string {\n let h = 2166136261 >>> 0\n for (let i = 0; i < s.length; i++) {\n h ^= s.charCodeAt(i)\n h = Math.imul(h, 16777619) >>> 0\n }\n return h.toString(16).padStart(8, '0')\n}\n\n/**\n * Adapt campaign cells into the `RunRecord` shape `analyzeRuns()` consumes.\n * Each cell becomes one run; `candidateId` is the caller-supplied label so\n * baseline + winner pair cleanly on `(experimentId, scenarioId, seed)`.\n *\n * `promptHash` is the REAL sha256 content hash of the surface this cell ran\n * (baseline vs winner are byte-distinguishable + byte-identical-verifiable);\n * `configHash` is the sha256 of the candidate label so the two candidates'\n * config rows differ. Both were previously the literal `'sha256:cell'`, which\n * made baseline and winner indistinguishable in every downstream record.\n */\nfunction cellsToRunRecords<TArtifact>(\n cells: ReadonlyArray<CampaignCellResult<TArtifact>>,\n candidateId: 'baseline' | 'winner',\n runId: string,\n surface: MutableSurface,\n splitTag: RunSplitTag,\n fallbackModel?: string,\n): RunRecord[] {\n const promptHash = surfaceContentHash(surface)\n const configHash = surfaceContentHash(candidateId)\n return cells.map((cell) => {\n const receiptModels = cell.resolvedModels ?? (cell.resolvedModel ? [cell.resolvedModel] : [])\n if (receiptModels.length > 1) {\n throw new ValidationError(\n `selfImprove cell ${cell.cellId} used multiple agent models: ${receiptModels.join(', ')}`,\n )\n }\n const model = receiptModels[0] ?? fallbackModel\n if (!model) {\n throw new ValidationError(\n `selfImprove.model is required when cell ${cell.cellId} has no paid-call model receipt`,\n )\n }\n if (!modelHasSnapshot(model)) {\n throw new ValidationError(\n `selfImprove model \"${model}\" lacks a snapshot version for cell ${cell.cellId}`,\n )\n }\n return campaignCellToRunRecord(cell, {\n runId: `${runId}::${candidateId}::${cell.cellId}`,\n experimentId: runId,\n candidateId,\n // scenarioId is explicit; seed keeps repeated runs distinct.\n seed:\n cell.rep * 1_000_000 +\n hashString(cell.scenarioId)\n .slice(0, 6)\n .split('')\n .reduce((a, c) => (a * 31 + c.charCodeAt(0)) >>> 0, 0),\n model,\n promptHash,\n configHash,\n commitSha: 'cell',\n splitTag,\n })\n })\n}\n","import type { RunEvalOptions } from '../campaign/presets/run-eval'\nimport { runEval } from '../campaign/presets/run-eval'\nimport { inMemoryCampaignStorage } from '../campaign/storage'\nimport type {\n CampaignResult,\n DispatchContext,\n JudgeConfig,\n MutableSurface,\n Scenario,\n} from '../campaign/types'\nimport type { HostedTenant } from '../hosted/client'\nimport {\n type SelfImproveBudget,\n type SelfImproveOptions,\n type SelfImproveResult,\n selfImprove,\n} from './self-improve'\n\nexport type AgentEvalAgent<TScenario extends Scenario, TArtifact> = (\n surface: MutableSurface,\n scenario: TScenario,\n ctx: DispatchContext,\n) => Promise<TArtifact>\n\nexport type DefineAgentEvalOptions<TScenario extends Scenario, TArtifact> = SelfImproveOptions<\n TScenario,\n TArtifact\n>\n\nexport interface AgentEvalEvaluateOptions<TScenario extends Scenario, TArtifact>\n extends Omit<\n RunEvalOptions<TScenario, TArtifact>,\n 'dispatch' | 'judges' | 'runDir' | 'scenarios'\n > {\n /** Scenario set to evaluate. Defaults to the scenarios passed to `defineAgentEval`. */\n scenarios?: TScenario[]\n /** Surface to evaluate. Defaults to the baseline surface passed to `defineAgentEval`. */\n surface?: MutableSurface\n /** Agent to evaluate. Defaults to the agent passed to `defineAgentEval`. */\n agent?: AgentEvalAgent<TScenario, TArtifact>\n /** Single judge override. Ignored when `judges` is set. */\n judge?: JudgeConfig<TArtifact, TScenario>\n /** Full judge list override. Defaults to the single judge passed to `defineAgentEval`. */\n judges?: JudgeConfig<TArtifact, TScenario>[]\n /** Logical or filesystem run directory. Defaults to an in-memory run. */\n runDir?: string\n}\n\nexport type AgentEvalImproveOptions<TScenario extends Scenario, TArtifact> = Omit<\n Partial<SelfImproveOptions<TScenario, TArtifact>>,\n 'budget' | 'hostedTenant'\n> & {\n budget?: Partial<SelfImproveBudget>\n hostedTenant?: Partial<HostedTenant>\n}\n\nexport interface DefinedAgentEval<TScenario extends Scenario, TArtifact> {\n /** The default scenarios used by `evaluate()` and `improve()`. */\n readonly scenarios: readonly TScenario[]\n /** The default baseline surface used by `evaluate()` and `improve()`. */\n readonly baselineSurface: MutableSurface\n /**\n * Run one scored evaluation. Use this for a baseline score or to score one\n * candidate surface without running an improvement loop.\n */\n evaluate(\n opts?: AgentEvalEvaluateOptions<TScenario, TArtifact>,\n ): Promise<CampaignResult<TArtifact, TScenario>>\n /**\n * Run the closed improvement loop. Per-call overrides replace the definition\n * except for nested config objects (`budget`, `hostedTenant`), which\n * are merged field-by-field so callers can override one knob without\n * repeating secrets or budget defaults.\n */\n improve(\n opts?: AgentEvalImproveOptions<TScenario, TArtifact>,\n ): Promise<SelfImproveResult<TScenario, TArtifact>>\n}\n\n/**\n * Define an agent eval once, then either score a surface with `evaluate()` or\n * run the closed loop with `improve()`.\n *\n * This is a DX wrapper only: it delegates to `runEval()` and `selfImprove()` and\n * returns their native result shapes.\n */\nexport function defineAgentEval<TScenario extends Scenario, TArtifact>(\n defaults: DefineAgentEvalOptions<TScenario, TArtifact>,\n): DefinedAgentEval<TScenario, TArtifact> {\n const defaultEvaluateOptions = evaluateDefaults(defaults)\n\n return {\n scenarios: defaults.scenarios,\n baselineSurface: defaults.baselineSurface,\n\n async evaluate(opts = {}) {\n const { agent, judge, judges, runDir, scenarios, surface, ...campaignOpts } = opts\n const selectedAgent = agent ?? defaults.agent\n const selectedSurface = surface ?? defaults.baselineSurface\n const selectedRunDir = runDir ?? defaults.runDir ?? `mem://defineAgentEval-${Date.now()}`\n const selectedStorage =\n campaignOpts.storage ??\n defaultEvaluateOptions.storage ??\n (selectedRunDir.startsWith('mem://') ? inMemoryCampaignStorage() : undefined)\n const evalOptions: RunEvalOptions<TScenario, TArtifact> = {\n ...defaultEvaluateOptions,\n ...campaignOpts,\n ...(selectedStorage ? { storage: selectedStorage } : {}),\n runDir: selectedRunDir,\n scenarios: scenarios ?? defaults.scenarios,\n dispatch: (scenario, ctx) => selectedAgent(selectedSurface, scenario, ctx),\n judges: evaluateJudges(judges, judge ?? defaults.judge),\n }\n if (evalOptions.reps !== undefined)\n evalOptions.reps = requirePositiveInteger(evalOptions.reps, 'reps')\n return runEval<TScenario, TArtifact>(evalOptions)\n },\n\n async improve(opts = {}) {\n const {\n budget: budgetOverride,\n hostedTenant: hostedTenantOverride,\n ...topLevelOverrides\n } = opts\n const merged = mergeDefined(defaults, topLevelOverrides)\n const budget = mergeBudget(defaults.budget, budgetOverride)\n const hostedTenant = mergeHostedTenant(defaults.hostedTenant, hostedTenantOverride)\n return selfImprove<TScenario, TArtifact>({\n ...merged,\n ...(budget ? { budget } : {}),\n ...(hostedTenant ? { hostedTenant } : {}),\n })\n },\n }\n}\n\ntype SharedEvaluateDefaults<TScenario extends Scenario, TArtifact> = Omit<\n RunEvalOptions<TScenario, TArtifact>,\n 'dispatch' | 'judges' | 'runDir' | 'scenarios'\n>\n\nfunction evaluateDefaults<TScenario extends Scenario, TArtifact>(\n defaults: DefineAgentEvalOptions<TScenario, TArtifact>,\n): SharedEvaluateDefaults<TScenario, TArtifact> {\n const out: SharedEvaluateDefaults<TScenario, TArtifact> = {}\n if (defaults.storage) out.storage = defaults.storage\n if (defaults.labeledStore) out.labeledStore = defaults.labeledStore\n if (defaults.captureSource) out.captureSource = defaults.captureSource\n if (defaults.cellPlacement) out.cellPlacement = defaults.cellPlacement\n if (defaults.expectUsage) out.expectUsage = defaults.expectUsage\n if (defaults.budget?.dollars !== undefined) out.costCeiling = defaults.budget.dollars\n if (defaults.budget?.maxConcurrency !== undefined)\n out.maxConcurrency = defaults.budget.maxConcurrency\n if (defaults.budget?.reps !== undefined)\n out.reps = requirePositiveInteger(defaults.budget.reps, 'budget.reps')\n return out\n}\n\nfunction mergeBudget(\n defaults: SelfImproveBudget | undefined,\n overrides: Partial<SelfImproveBudget> | undefined,\n): SelfImproveBudget | undefined {\n const merged = mergeOptionalObject(defaults, overrides)\n if (merged?.reps !== undefined) merged.reps = requirePositiveInteger(merged.reps, 'budget.reps')\n return merged\n}\n\nfunction mergeHostedTenant(\n defaults: HostedTenant | undefined,\n overrides: Partial<HostedTenant> | undefined,\n): HostedTenant | undefined {\n const merged = mergeOptionalObject(defaults, overrides)\n if (!merged) return undefined\n if (!merged.endpoint?.trim() || !merged.apiKey?.trim() || !merged.tenantId?.trim()) {\n throw new Error(\n 'defineAgentEval.improve: hostedTenant requires endpoint, apiKey, and tenantId after merging defaults and overrides',\n )\n }\n return merged\n}\n\nfunction mergeDefined<T extends object>(defaults: T, overrides: Partial<T> | undefined): T {\n if (!overrides) return defaults\n const merged = { ...defaults } as Record<string, unknown>\n for (const [key, value] of Object.entries(overrides)) {\n if (value !== undefined) merged[key] = value\n }\n return merged as T\n}\n\nfunction mergeOptionalObject<T extends object>(\n defaults: T | undefined,\n overrides: Partial<T> | undefined,\n): T | undefined {\n if (!defaults && !overrides) return undefined\n return mergeDefined(defaults ?? ({} as T), overrides)\n}\n\nfunction evaluateJudges<TArtifact, TScenario extends Scenario>(\n judges: JudgeConfig<TArtifact, TScenario>[] | undefined,\n defaultJudge: JudgeConfig<TArtifact, TScenario>,\n): JudgeConfig<TArtifact, TScenario>[] {\n if (judges !== undefined) {\n if (judges.length === 0) {\n throw new Error('defineAgentEval.evaluate: judges must not be empty')\n }\n return judges\n }\n return [defaultJudge]\n}\n\nfunction requirePositiveInteger(value: number, field: string): number {\n if (!Number.isInteger(value) || value < 1) {\n throw new Error(`defineAgentEval: ${field} must be a positive integer`)\n }\n return value\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA4BA,SAAgB,cAAc,QAAgB,WAA4C;CACxF,MAAM,QA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