@tangle-network/agent-eval 0.128.1 → 0.129.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.
Files changed (137) hide show
  1. package/CHANGELOG.md +271 -0
  2. package/README.md +18 -0
  3. package/dist/analyst/index.d.ts +107 -165
  4. package/dist/analyst/index.js +5 -9
  5. package/dist/analyst/index.js.map +1 -1
  6. package/dist/belief-state/index.d.ts +2 -19
  7. package/dist/belief-state/index.js +30 -31
  8. package/dist/belief-state/index.js.map +1 -1
  9. package/dist/benchmarks/index.d.ts +5 -8
  10. package/dist/benchmarks/index.js +12 -11
  11. package/dist/builder-eval/index.js +1 -1
  12. package/dist/campaign/index.d.ts +30 -39
  13. package/dist/campaign/index.js +11 -10
  14. package/dist/{chunk-NKAGIDE2.js → chunk-2QU3YOPR.js} +15 -274
  15. package/dist/chunk-2QU3YOPR.js.map +1 -0
  16. package/dist/{chunk-EJGRPCO3.js → chunk-3OCR4R5I.js} +245 -134
  17. package/dist/chunk-3OCR4R5I.js.map +1 -0
  18. package/dist/{chunk-2JX3CFMB.js → chunk-56TAVBOK.js} +5 -2
  19. package/dist/chunk-56TAVBOK.js.map +1 -0
  20. package/dist/{chunk-DPUHNQLN.js → chunk-7FO3TNPI.js} +2 -2
  21. package/dist/{chunk-DJKY2TSY.js → chunk-BSO5JDQH.js} +27 -120
  22. package/dist/chunk-BSO5JDQH.js.map +1 -0
  23. package/dist/{chunk-EZJEIH2R.js → chunk-C6LXANRU.js} +11 -20
  24. package/dist/chunk-C6LXANRU.js.map +1 -0
  25. package/dist/{chunk-ZUUWPZCV.js → chunk-DODXQREJ.js} +4 -4
  26. package/dist/{chunk-2MKQIFS4.js → chunk-E7QXT7SX.js} +2 -2
  27. package/dist/{chunk-NYLOYM6N.js → chunk-EG66UGL4.js} +37 -28
  28. package/dist/chunk-EG66UGL4.js.map +1 -0
  29. package/dist/{chunk-P5W7RQKK.js → chunk-FXTVJPYD.js} +2 -2
  30. package/dist/{chunk-VZSRQ272.js → chunk-G7MGMCZD.js} +6 -2
  31. package/dist/chunk-G7MGMCZD.js.map +1 -0
  32. package/dist/{chunk-IHQDPH7D.js → chunk-H23X7XKK.js} +85 -75
  33. package/dist/chunk-H23X7XKK.js.map +1 -0
  34. package/dist/{chunk-VBQ3CRKH.js → chunk-HPWUNB47.js} +4 -6
  35. package/dist/chunk-HPWUNB47.js.map +1 -0
  36. package/dist/{chunk-XPRT64IE.js → chunk-IYCLP2N2.js} +3 -3
  37. package/dist/chunk-IYCLP2N2.js.map +1 -0
  38. package/dist/{chunk-XDWDC2MP.js → chunk-JQSF5DQT.js} +11 -5
  39. package/dist/chunk-JQSF5DQT.js.map +1 -0
  40. package/dist/{chunk-NACAGYSY.js → chunk-M4YBQKIJ.js} +11 -11
  41. package/dist/chunk-M4YBQKIJ.js.map +1 -0
  42. package/dist/{chunk-YJBNWCAA.js → chunk-NY44NC4A.js} +3 -3
  43. package/dist/chunk-OIUOT4QD.js +44 -0
  44. package/dist/chunk-OIUOT4QD.js.map +1 -0
  45. package/dist/chunk-OWN5NPMC.js +152 -0
  46. package/dist/chunk-OWN5NPMC.js.map +1 -0
  47. package/dist/chunk-PC5DOSM7.js +579 -0
  48. package/dist/chunk-PC5DOSM7.js.map +1 -0
  49. package/dist/{chunk-UB2LOJ6Q.js → chunk-QB6BDBP2.js} +23 -20
  50. package/dist/chunk-QB6BDBP2.js.map +1 -0
  51. package/dist/chunk-RXHCETDZ.js +536 -0
  52. package/dist/chunk-RXHCETDZ.js.map +1 -0
  53. package/dist/{chunk-PBE2LOSS.js → chunk-SFLLL76A.js} +7 -7
  54. package/dist/chunk-SFLLL76A.js.map +1 -0
  55. package/dist/{chunk-VGRCHJON.js → chunk-T6RLYGAD.js} +3 -8
  56. package/dist/chunk-T6RLYGAD.js.map +1 -0
  57. package/dist/{chunk-VLOATJQ2.js → chunk-TJVT4QFF.js} +21 -18
  58. package/dist/chunk-TJVT4QFF.js.map +1 -0
  59. package/dist/{chunk-S5YLIBFX.js → chunk-TQ7LNKZ3.js} +2 -2
  60. package/dist/{chunk-EOSZT7PL.js → chunk-U4L7JRPZ.js} +2 -297
  61. package/dist/chunk-U4L7JRPZ.js.map +1 -0
  62. package/dist/chunk-U4PHLT2N.js +419 -0
  63. package/dist/chunk-U4PHLT2N.js.map +1 -0
  64. package/dist/{chunk-WS3NZZQQ.js → chunk-VCZ5FQYW.js} +3 -4
  65. package/dist/chunk-VCZ5FQYW.js.map +1 -0
  66. package/dist/{chunk-BYT7ELPS.js → chunk-WVATSFCP.js} +2 -2
  67. package/dist/{chunk-TSN7JT6D.js → chunk-X4YIBDER.js} +21 -5
  68. package/dist/{chunk-TSN7JT6D.js.map → chunk-X4YIBDER.js.map} +1 -1
  69. package/dist/{chunk-TBL77AUT.js → chunk-YQN4ICPP.js} +5 -5
  70. package/dist/{chunk-MHELPNRP.js → chunk-ZHTZ4EYI.js} +1 -1
  71. package/dist/chunk-ZHTZ4EYI.js.map +1 -0
  72. package/dist/cli.js +6 -5
  73. package/dist/cli.js.map +1 -1
  74. package/dist/contract/index.d.ts +47 -87
  75. package/dist/contract/index.js +14 -13
  76. package/dist/contract/index.js.map +1 -1
  77. package/dist/control.js +3 -2
  78. package/dist/fuzz.js +3 -2
  79. package/dist/fuzz.js.map +1 -1
  80. package/dist/index.d.ts +659 -203
  81. package/dist/index.js +145 -117
  82. package/dist/index.js.map +1 -1
  83. package/dist/meta-eval/index.js +2 -2
  84. package/dist/multishot/index.d.ts +3 -4
  85. package/dist/multishot/index.js.map +1 -1
  86. package/dist/openapi.json +1 -1
  87. package/dist/pipelines/index.js +5 -5
  88. package/dist/reporting.d.ts +14 -0
  89. package/dist/reporting.js +7 -6
  90. package/dist/rl.d.ts +652 -82
  91. package/dist/rl.js +415 -171
  92. package/dist/rl.js.map +1 -1
  93. package/dist/rollout/index.d.ts +1071 -32
  94. package/dist/rollout/index.js +68 -10
  95. package/dist/run-campaign-OJJ7CZF4.js +18 -0
  96. package/dist/supervisor-run/index.d.ts +114 -4
  97. package/dist/supervisor-run/index.js +4 -3
  98. package/dist/traces.d.ts +1 -1
  99. package/dist/traces.js +6 -5
  100. package/dist/wire/index.d.ts +10 -11
  101. package/dist/wire/index.js +3 -3
  102. package/docs/feature-guide.md +1 -1
  103. package/docs/rollout.md +116 -2
  104. package/package.json +4 -4
  105. package/dist/chunk-2JX3CFMB.js.map +0 -1
  106. package/dist/chunk-DJKY2TSY.js.map +0 -1
  107. package/dist/chunk-EJGRPCO3.js.map +0 -1
  108. package/dist/chunk-EOSZT7PL.js.map +0 -1
  109. package/dist/chunk-EZJEIH2R.js.map +0 -1
  110. package/dist/chunk-IHQDPH7D.js.map +0 -1
  111. package/dist/chunk-MHELPNRP.js.map +0 -1
  112. package/dist/chunk-NACAGYSY.js.map +0 -1
  113. package/dist/chunk-NKAGIDE2.js.map +0 -1
  114. package/dist/chunk-NYLOYM6N.js.map +0 -1
  115. package/dist/chunk-PBE2LOSS.js.map +0 -1
  116. package/dist/chunk-TT4KNT67.js +0 -124
  117. package/dist/chunk-TT4KNT67.js.map +0 -1
  118. package/dist/chunk-UB2LOJ6Q.js.map +0 -1
  119. package/dist/chunk-UWZZKKU7.js +0 -237
  120. package/dist/chunk-UWZZKKU7.js.map +0 -1
  121. package/dist/chunk-VBQ3CRKH.js.map +0 -1
  122. package/dist/chunk-VGRCHJON.js.map +0 -1
  123. package/dist/chunk-VLOATJQ2.js.map +0 -1
  124. package/dist/chunk-VZSRQ272.js.map +0 -1
  125. package/dist/chunk-WS3NZZQQ.js.map +0 -1
  126. package/dist/chunk-XDWDC2MP.js.map +0 -1
  127. package/dist/chunk-XPRT64IE.js.map +0 -1
  128. package/dist/run-campaign-ISHFZ7FJ.js +0 -17
  129. /package/dist/{chunk-DPUHNQLN.js.map → chunk-7FO3TNPI.js.map} +0 -0
  130. /package/dist/{chunk-ZUUWPZCV.js.map → chunk-DODXQREJ.js.map} +0 -0
  131. /package/dist/{chunk-2MKQIFS4.js.map → chunk-E7QXT7SX.js.map} +0 -0
  132. /package/dist/{chunk-P5W7RQKK.js.map → chunk-FXTVJPYD.js.map} +0 -0
  133. /package/dist/{chunk-YJBNWCAA.js.map → chunk-NY44NC4A.js.map} +0 -0
  134. /package/dist/{chunk-S5YLIBFX.js.map → chunk-TQ7LNKZ3.js.map} +0 -0
  135. /package/dist/{chunk-BYT7ELPS.js.map → chunk-WVATSFCP.js.map} +0 -0
  136. /package/dist/{chunk-TBL77AUT.js.map → chunk-YQN4ICPP.js.map} +0 -0
  137. /package/dist/{run-campaign-ISHFZ7FJ.js.map → run-campaign-OJJ7CZF4.js.map} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/contamination-guard.ts","../src/contract/analyze-runs.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 { 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 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\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 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 for (const [name, delta] of Object.entries(metrics)) {\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 }\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/** Two-sample Welch comparison: unequal-variance t-test + CI on the delta\n * + Cohen's d (pooled stddev). Significance = p < 0.05 AND |d| >= 0.2. */\nfunction welchCompare(baseline: number[], current: number[]): MetricDelta {\n const baselineMean = mean(baseline)\n const currentMean = mean(current)\n const baselineVar = sampleVariance(baseline, baselineMean)\n const currentVar = sampleVariance(current, currentMean)\n const baselineN = baseline.length\n const currentN = current.length\n const delta = currentMean - baselineMean\n\n // Welch standard error\n const se = Math.sqrt(baselineVar / baselineN + currentVar / currentN)\n // For 95% CI we use z=1.96 (large-n approximation). Customers running\n // analyzeRuns will typically have n >= 30; the t-correction is\n // negligible vs the practical noise floor.\n const halfWidth = 1.96 * (se > 0 ? se : 0)\n const ci95: [number, number] = [delta - halfWidth, delta + halfWidth]\n\n // p-value via normal approximation to the t-statistic.\n const t = se > 0 ? delta / se : 0\n const pValue = se > 0 ? 2 * (1 - standardNormalCdf(Math.abs(t))) : 1\n\n // Cohen's d — pooled stddev.\n const pooledStddev = Math.sqrt(\n ((baselineN - 1) * baselineVar + (currentN - 1) * currentVar) /\n Math.max(1, baselineN + currentN - 2),\n )\n const cohensD = pooledStddev > 0 ? delta / pooledStddev : 0\n\n // Significance: BOTH p < 0.05 AND |d| >= 0.2 (small-effect threshold).\n const significant = pValue < 0.05 && Math.abs(cohensD) >= 0.2\n\n return {\n current: currentMean,\n baseline: baselineMean,\n delta,\n ci95,\n pValue,\n cohensD,\n baselineN,\n currentN,\n significant,\n }\n}\n\nfunction sampleVariance(xs: number[], xsMean: number): number {\n if (xs.length < 2) return 0\n let s = 0\n for (const x of xs) s += (x - xsMean) ** 2\n return s / (xs.length - 1)\n}\n\n/** Abramowitz & Stegun approximation to Φ(z). Maximum error ~7.5e-8. */\nfunction standardNormalCdf(z: number): number {\n const a1 = 0.254829592\n const a2 = -0.284496736\n const a3 = 1.421413741\n const a4 = -1.453152027\n const a5 = 1.061405429\n const p = 0.3275911\n const sign = z < 0 ? -1 : 1\n const x = Math.abs(z) / Math.SQRT2\n const t = 1 / (1 + p * x)\n const y = 1 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.exp(-x * x)\n return 0.5 * (1 + sign * y)\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 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.ci95[0] > 0\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}`\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) 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) 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 }\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 const pairedEffect =\n ctx.lift.cohensD === null ? 'undefined (zero delta variance)' : ctx.lift.cohensD.toFixed(2)\n const requiredRuns =\n ctx.lift.requiredN === null ? 'not estimable' : `~${ctx.lift.requiredN} paired runs`\n const decisive = 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=${ctx.lift.pValue.toFixed(4)}, 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 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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BO,SAAS,cAAc,QAAgB,WAA4C;AACxF,QAAM,QAAsB,CAAC;AAC7B,aAAW,KAAK,WAAW;AACzB,QAAI,CAAC,EAAE,OAAQ;AACf,QAAI,OAAO,SAAS,EAAE,MAAM,GAAG;AAC7B,YAAM,KAAK,EAAE,YAAY,EAAE,IAAI,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,UAAU;AAAA,MACV,MAAM;AAAA,MACN,OAAO,8BAA8B,IAAI,WAAW,MAAM,QAAQ,CAAC,CAAC;AAAA,MACpE,QACE;AAAA,MACF,cAAc;AAAA,IAChB,CAAC;AAAA,EACH;AAEA,MAAI,IAAI,mBAAmB,IAAI,gBAAgB,SAAS,SAAS,GAAG;AAClE,UAAM,MAAM,IAAI,gBAAgB,SAAS,CAAC;AAC1C,QAAI,KAAK;AAAA,MACP,UAAU;AAAA,MACV,MAAM;AAAA,MACN,OAAO,wBAAwB,IAAI,IAAI,MAAM,IAAI,QAAQ,KAAK,QAAQ,CAAC,CAAC;AAAA,MACxE,QAAQ,GAAG,IAAI,gBAAgB,aAAa,4CAA4C,IAAI,UAAU,MAAM,qBAAqB,IAAI,IAAI;AAAA,MACzI,cAAc;AAAA,IAChB,CAAC;AAAA,EACH;AAEA,MAAI,IAAI,sBAAsB,KAAK,IAAI,IAAI,mBAAmB,QAAQ,IAAI,KAAK;AAC7E,QAAI,KAAK;AAAA,MACP,UAAU;AAAA,MACV,MAAM;AAAA,MACN,OAAO,+BAA+B,IAAI,mBAAmB,MAAM,qBAAgB,IAAI,mBAAmB,SAAS,QAAQ,CAAC,CAAC;AAAA,MAC7H,QAAQ,yFAAyF,IAAI,mBAAmB,MAAM,2CAA2C,IAAI,mBAAmB,MAAM;AAAA,MACtM,cAAc;AAAA,IAChB,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":["mean","agreement"]}
@@ -5,10 +5,10 @@ import {
5
5
  costReceiptFromLlm,
6
6
  costReceiptFromLlmError,
7
7
  maximumChargeForLlmRequest
8
- } from "./chunk-PBE2LOSS.js";
8
+ } from "./chunk-SFLLL76A.js";
9
9
  import {
10
10
  CostLedger
11
- } from "./chunk-WS3NZZQQ.js";
11
+ } from "./chunk-VCZ5FQYW.js";
12
12
 
13
13
  // src/wire/schemas.ts
14
14
  import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
@@ -1053,4 +1053,4 @@ export {
1053
1053
  startServer,
1054
1054
  startServerAsync
1055
1055
  };
1056
- //# sourceMappingURL=chunk-YJBNWCAA.js.map
1056
+ //# sourceMappingURL=chunk-NY44NC4A.js.map
@@ -0,0 +1,44 @@
1
+ // src/rollout/reward.ts
2
+ function isRealnessGated(record) {
3
+ return record.outcome.realness?.gated === true;
4
+ }
5
+ function observedSplitScore(record, split) {
6
+ return split === "search" ? record.outcome.searchScore : record.outcome.holdoutScore;
7
+ }
8
+ function observedScore(record, prefer = "holdout") {
9
+ const other = prefer === "search" ? "holdout" : "search";
10
+ return observedSplitScore(record, prefer) ?? observedSplitScore(record, other);
11
+ }
12
+ function scoreOrigin(record, prefer = "holdout") {
13
+ const other = prefer === "search" ? "holdout" : "search";
14
+ if (observedSplitScore(record, prefer) !== void 0) return prefer;
15
+ if (observedSplitScore(record, other) !== void 0) return other;
16
+ return "unscored";
17
+ }
18
+ function trainingScore(record, prefer = "holdout") {
19
+ if (isRealnessGated(record)) return 0;
20
+ return observedScore(record, prefer);
21
+ }
22
+ function trainingReward(record) {
23
+ return { reward: trainingScore(record) ?? null, gated: isRealnessGated(record) };
24
+ }
25
+ function rolloutRewardFields(record) {
26
+ const { reward, gated } = trainingReward(record);
27
+ const screened = record.outcome.realness !== void 0;
28
+ return { reward, realness_gated: gated, ...screened ? { realness_screened: true } : {} };
29
+ }
30
+ function unscreenedRewardFields(score) {
31
+ return { reward: score, realness_gated: false, realness_screened: false };
32
+ }
33
+
34
+ export {
35
+ isRealnessGated,
36
+ observedSplitScore,
37
+ observedScore,
38
+ scoreOrigin,
39
+ trainingScore,
40
+ trainingReward,
41
+ rolloutRewardFields,
42
+ unscreenedRewardFields
43
+ };
44
+ //# sourceMappingURL=chunk-OIUOT4QD.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/rollout/reward.ts"],"sourcesContent":["/**\n * The two named score derivations every consumer must choose between.\n *\n * The anti-Goodhart gate (`outcome.realness.gated`) only holds if it is\n * impossible to read a run's score WITHOUT deciding whether the gate applies.\n * A bare `outcome.holdoutScore ?? outcome.searchScore` makes that decision\n * invisible — and silently answers \"no gate\", which is the wrong default on\n * every path that produces training data. So the expression lives here, once,\n * behind two names that force the caller to state the intent:\n *\n * - `trainingScore` / `trainingReward` — GATED. Anything that becomes\n * training data, or a reward a trainer consumes, uses these.\n * - `observedScore` — RAW. Analysis, reporting, and reward-hack DETECTION\n * need the ungated number; that is how a gamed run is visible at all.\n *\n * A leaf module on purpose: it imports only the `RunRecord` type, so gate and\n * reporting code can depend on it without pulling in the trace store that\n * `mint.ts` needs.\n */\n\nimport type { RunRecord } from '../run-record'\n\n/**\n * Which split's score wins when a record carries both. `'holdout'` is the\n * canonical \"real signal\" default; `'search'` exists because some callers\n * deliberately score on the search split when both are present.\n */\nexport type ScorePreference = 'holdout' | 'search'\n\n/** Only the outcome is read, so every accessor here accepts anything carrying one. */\ntype Scored = Pick<RunRecord, 'outcome'>\n\n/** True when the authenticity gate flagged the run as gamed (`realness.gated`). */\nexport function isRealnessGated(record: Scored): boolean {\n return record.outcome.realness?.gated === true\n}\n\n/**\n * The RAW score recorded on ONE split, with no cross-split fallback and no\n * anti-Goodhart gate.\n *\n * The narrowest of the three raw readers, and the one every split-scoped\n * consumer wants: a per-split report, a promotion gate, or a paired comparison\n * asks \"what did this run score on the split I am summarising\", and answering\n * it with the other split's number silently mixes populations. `undefined` =\n * that split was never scored.\n *\n * Same warning as `observedScore`: this INCLUDES runs flagged as gamed. Never\n * feed it into training data.\n */\nexport function observedSplitScore(record: Scored, split: ScorePreference): number | undefined {\n return split === 'search' ? record.outcome.searchScore : record.outcome.holdoutScore\n}\n\n/**\n * The RAW split score the run carries, with NO anti-Goodhart gate applied.\n *\n * INCLUDES RUNS FLAGGED AS GAMED (`outcome.realness.gated === true`); NEVER\n * feed this into training data — a fine-tune that sees it learns from gamed\n * successes. It is exported anyway because analysis, reporting, and\n * reward-hacking detection legitimately need the ungated number: forcing a\n * gamed run to 0 collapses the proxy signal toward ground truth and makes a\n * detector report \"clean\" on exactly the population that is being gamed.\n *\n * Returns `undefined` when the record carries neither score — an unscored run\n * is a labeled gap, not a measured zero, and each caller picks its own\n * sentinel (`?? 0`, `?? null`, skip, throw). Non-finite values are returned\n * as-is; callers that care keep their own `Number.isFinite` guard.\n */\nexport function observedScore(\n record: Scored,\n prefer: ScorePreference = 'holdout',\n): number | undefined {\n const other: ScorePreference = prefer === 'search' ? 'holdout' : 'search'\n return observedSplitScore(record, prefer) ?? observedSplitScore(record, other)\n}\n\n/** Which split actually carried the score, or that none did. */\nexport type ScoreOrigin = 'holdout' | 'search' | 'unscored'\n\n/**\n * Where `observedScore` / `trainingScore` read their number from — the\n * provenance label a rollout line's `reward_source` is built from, and the\n * only supported way to ask \"was this run scored at all\" without respelling\n * the field access.\n */\nexport function scoreOrigin(record: Scored, prefer: ScorePreference = 'holdout'): ScoreOrigin {\n const other: ScorePreference = prefer === 'search' ? 'holdout' : 'search'\n if (observedSplitScore(record, prefer) !== undefined) return prefer\n if (observedSplitScore(record, other) !== undefined) return other\n return 'unscored'\n}\n\n/**\n * The GATED score — the only derivation allowed to reach training data.\n *\n * A realness-gated run scores 0 no matter what it claims, so a fine-tune\n * cannot learn from a gamed success. An unscored run stays `undefined` (a\n * labeled gap), keeping \"we never measured this\" distinct from \"we measured\n * zero\"; callers that need a number apply their own sentinel.\n */\nexport function trainingScore(\n record: Scored,\n prefer: ScorePreference = 'holdout',\n): number | undefined {\n if (isRealnessGated(record)) return 0\n return observedScore(record, prefer)\n}\n\n/**\n * `{reward, gated}` as written onto a minted `RolloutLine` — `trainingScore`\n * plus the flag itself, so the gate travels into the exported row and a\n * downstream filter can drop or down-weight the line.\n *\n * An unscored record yields `reward: null`, matching the schema's \"no verdict\n * exists — a labeled gap, never 0\" rule. It previously collapsed to 0, which\n * made a run nobody graded indistinguishable from one graded as a total\n * failure, and taught any trainer reading the row that the trajectory was bad.\n * A gated run still yields 0, because that IS a verdict: the gate decided.\n */\nexport function trainingReward(record: Scored): { reward: number | null; gated: boolean } {\n return { reward: trainingScore(record) ?? null, gated: isRealnessGated(record) }\n}\n\n/**\n * A reward the CALLER computed, with the gate applied on top.\n *\n * Several published exporters accept a `rewardOf` hook so a consumer can drive\n * training off a verifiable signal instead of the headline judge score. That is\n * a real feature and the gate does not remove it — but a reward derived from a\n * gamed run is a gamed reward whatever its source, so it falls to 0 exactly\n * like the score it replaced.\n *\n * It lives here because the same hook exists under the same name in two\n * modules, and for a while only one of them gated it: `rl/exporters.toGrpoRows`\n * forced a gated run to 0 while `rl/preferences.extractPreferences` handed the\n * caller's number straight through, which put a gamed run on the CHOSEN side of\n * every DPO pair against its honest sibling. Two same-named hooks with\n * different gating behaviour was the defect; one implementation is the fix.\n *\n * `null` means \"no reward\" and is preserved: a non-finite or absent value is a\n * gap, and a gap is not a zero.\n */\nexport function trainingRewardOverride(record: Scored, value: number | null): number | null {\n if (value === null || !Number.isFinite(value)) return null\n return isRealnessGated(record) ? 0 : value\n}\n\n/**\n * The two fields a rollout line's `outcome` has to state TOGETHER — the reward\n * and whether the authenticity gate fired on it.\n *\n * Stating them together is the point. `realness_gated` used to be optional on\n * the wire, so a producer that wrote `reward` and simply never thought about\n * the flag emitted a row on which `isLineRealnessGated` was structurally false\n * — which is how a second minting door existed for every supervisor and worker\n * row without anyone noticing it had never consulted the gate. The wire schema\n * now requires the flag, and this pair is how a producer states it.\n */\nexport interface RolloutRewardFields {\n reward: number | null\n realness_gated: boolean\n /**\n * Whether a screen RAN, as distinct from its verdict. Omitted when the\n * producer cannot tell — see `unscreenedRewardFields` and the field's own doc\n * on `RolloutOutcome`.\n */\n realness_screened?: boolean\n}\n\n/**\n * `trainingReward` in the line's own field names — what `mintRolloutRows`\n * writes onto every row it produces.\n *\n * `realness_screened: true` is written only when the record actually carries an\n * `outcome.realness` verdict, i.e. a screen genuinely ran and reported. A\n * record without one gets the field OMITTED rather than `false`: the record\n * cannot distinguish \"the screen ran elsewhere and this pipeline did not record\n * it\" from \"no screen exists\", and `false` is a load-bearing claim that\n * `assertMinted` refuses on. Absent = unknown, which is the truth here.\n */\nexport function rolloutRewardFields(record: Scored): RolloutRewardFields {\n const { reward, gated } = trainingReward(record)\n const screened = record.outcome.realness !== undefined\n return { reward, realness_gated: gated, ...(screened ? { realness_screened: true } : {}) }\n}\n\n/**\n * The same fields for a producer that has a graded score but NO `RunRecord`\n * behind it — a supervision journal, a harness session store, any second\n * intake. There is no `outcome.realness` to consult, so no authenticity screen\n * has run on this score.\n *\n * `realness_gated: false` alone was the WRONG way to say that. `false` is the\n * gate's VERDICT, and a verdict is a claim that somebody looked; on this path\n * nobody did, so the row asserted \"screened and clean\" about a score that had\n * never been screened at all — indistinguishable on the wire from a genuinely\n * clean one, which is exactly what an adversary needs. The two claims are now\n * carried by two fields:\n *\n * - `realness_screened: false` — no screen ran. Explicit, not inferable from\n * absence, and impossible to read as \"clean\" because it is not the gate's\n * verdict field.\n * - `realness_gated: false` — no gate fired, which is trivially true when no\n * gate ran, and is what a consumer filtering on the flag expects to see.\n *\n * `assertMinted` REFUSES a line carrying `realness_screened: false` with a\n * reward above zero, so these rows can be written, read, reported and analysed,\n * but a positive one cannot become training data until something screens it.\n *\n * A separate function from `rolloutRewardFields` on purpose: picking this one\n * is a producer declaring that its rewards were never screened.\n */\nexport function unscreenedRewardFields(score: number | null): RolloutRewardFields {\n return { reward: score, realness_gated: false, realness_screened: false }\n}\n"],"mappings":";AAiCO,SAAS,gBAAgB,QAAyB;AACvD,SAAO,OAAO,QAAQ,UAAU,UAAU;AAC5C;AAeO,SAAS,mBAAmB,QAAgB,OAA4C;AAC7F,SAAO,UAAU,WAAW,OAAO,QAAQ,cAAc,OAAO,QAAQ;AAC1E;AAiBO,SAAS,cACd,QACA,SAA0B,WACN;AACpB,QAAM,QAAyB,WAAW,WAAW,YAAY;AACjE,SAAO,mBAAmB,QAAQ,MAAM,KAAK,mBAAmB,QAAQ,KAAK;AAC/E;AAWO,SAAS,YAAY,QAAgB,SAA0B,WAAwB;AAC5F,QAAM,QAAyB,WAAW,WAAW,YAAY;AACjE,MAAI,mBAAmB,QAAQ,MAAM,MAAM,OAAW,QAAO;AAC7D,MAAI,mBAAmB,QAAQ,KAAK,MAAM,OAAW,QAAO;AAC5D,SAAO;AACT;AAUO,SAAS,cACd,QACA,SAA0B,WACN;AACpB,MAAI,gBAAgB,MAAM,EAAG,QAAO;AACpC,SAAO,cAAc,QAAQ,MAAM;AACrC;AAaO,SAAS,eAAe,QAA2D;AACxF,SAAO,EAAE,QAAQ,cAAc,MAAM,KAAK,MAAM,OAAO,gBAAgB,MAAM,EAAE;AACjF;AA2DO,SAAS,oBAAoB,QAAqC;AACvE,QAAM,EAAE,QAAQ,MAAM,IAAI,eAAe,MAAM;AAC/C,QAAM,WAAW,OAAO,QAAQ,aAAa;AAC7C,SAAO,EAAE,QAAQ,gBAAgB,OAAO,GAAI,WAAW,EAAE,mBAAmB,KAAK,IAAI,CAAC,EAAG;AAC3F;AA4BO,SAAS,uBAAuB,OAA2C;AAChF,SAAO,EAAE,QAAQ,OAAO,gBAAgB,OAAO,mBAAmB,MAAM;AAC1E;","names":[]}
@@ -0,0 +1,152 @@
1
+ import {
2
+ assertRewardGate
3
+ } from "./chunk-PC5DOSM7.js";
4
+
5
+ // src/rollout/exporters.ts
6
+ function realnessLabels(line) {
7
+ return {
8
+ realness_gated: line.outcome.realness_gated === true,
9
+ // `?? null` rather than `?? false`: absent means the producer never stated
10
+ // it, and `false` is a load-bearing claim ("no screen exists") that would be
11
+ // an invention here.
12
+ realness_screened: line.outcome.realness_screened ?? null
13
+ };
14
+ }
15
+ function toSftRows(lines, options = {}) {
16
+ for (const line of lines) assertRewardGate(line, "SFT export");
17
+ return lines.filter((line) => isTrainingLineEligible(line, options) && line.messages.length > 0).map((line) => ({
18
+ // Dropped, not kept-and-masked: a copied-context turn was authored by
19
+ // another agent, and an SFT trainer has no notion of "present but not a
20
+ // target" — every message it is handed is something to imitate.
21
+ messages: line.messages.filter((message) => message.is_copied_context !== true),
22
+ metadata: {
23
+ rollout_id: line.rollout_id,
24
+ run_id: line.run_id,
25
+ candidate_id: line.candidate_id ?? null,
26
+ instance_id: line.task.instance_id,
27
+ reward: line.outcome.reward,
28
+ ...realnessLabels(line)
29
+ }
30
+ })).filter((row) => row.messages.length > 0);
31
+ }
32
+ function toRewardRows(lines, options = {}) {
33
+ for (const line of lines) assertRewardGate(line, "reward-row export");
34
+ return lines.filter(
35
+ (line) => isTrainingLineEligible(line, options) && line.messages.some(
36
+ (message) => message.role === "user" && typeof message.content === "string" && message.content.length > 0
37
+ )
38
+ ).map((line) => ({
39
+ prompt: line.messages.find((m) => m.role === "user")?.content ?? "",
40
+ steps: line.steps ?? [],
41
+ reward: line.outcome.reward,
42
+ metadata: {
43
+ rollout_id: line.rollout_id,
44
+ run_id: line.run_id,
45
+ candidate_id: line.candidate_id ?? null,
46
+ instance_id: line.task.instance_id,
47
+ split: line.task.split,
48
+ ...realnessLabels(line)
49
+ }
50
+ }));
51
+ }
52
+ function firstAssistantIndex(messages) {
53
+ const index = messages.findIndex((m) => m.role === "assistant");
54
+ return index === -1 ? messages.length : index;
55
+ }
56
+ function toVerifiersRolloutOutput(line) {
57
+ assertRewardGate(line, "verifiers export");
58
+ const split = firstAssistantIndex(line.messages);
59
+ return {
60
+ prompt: line.messages.slice(0, split),
61
+ completion: line.messages.slice(split),
62
+ reward: line.outcome.reward,
63
+ metrics: line.outcome.metrics,
64
+ tool_defs: line.tool_defs,
65
+ token_usage: {
66
+ input_tokens: line.cost.tokens_in,
67
+ output_tokens: line.cost.tokens_out,
68
+ reasoning_tokens: line.cost.tokens_reasoning,
69
+ cache_read_tokens: line.cost.cache_read,
70
+ cache_write_tokens: line.cost.cache_write
71
+ },
72
+ info: {
73
+ task: line.task,
74
+ policy: line.policy,
75
+ rollout_id: line.rollout_id,
76
+ run_id: line.run_id,
77
+ experiment_id: line.experiment_id ?? null,
78
+ candidate_id: line.candidate_id ?? null,
79
+ generation: line.generation,
80
+ candidate_index: line.candidate_index,
81
+ role: line.role,
82
+ ...realnessLabels(line)
83
+ }
84
+ };
85
+ }
86
+ function toVerifiersRolloutOutputs(lines, options = {}) {
87
+ if (options.gatedLines === "zero-and-flag") {
88
+ return lines.filter((line) => isSplitEligible(line, options) && line.messages.length > 0).map(toVerifiersRolloutOutput);
89
+ }
90
+ return lines.filter(
91
+ (line) => isTrainingLineEligible(line, options) && firstAssistantIndex(line.messages) > 0 && firstAssistantIndex(line.messages) < line.messages.length
92
+ ).map(toVerifiersRolloutOutput);
93
+ }
94
+ function toRftItem(line) {
95
+ assertRewardGate(line, "RFT export");
96
+ const split = firstAssistantIndex(line.messages);
97
+ return {
98
+ messages: line.messages.slice(0, split),
99
+ reference: {
100
+ reward: line.outcome.reward,
101
+ reward_source: line.outcome.reward_source,
102
+ verdict: line.outcome.verdict,
103
+ instance_id: line.task.instance_id,
104
+ suite: line.task.suite,
105
+ split: line.task.split,
106
+ rollout_id: line.rollout_id,
107
+ ...realnessLabels(line)
108
+ }
109
+ };
110
+ }
111
+ function toRftItems(lines, options = {}) {
112
+ return lines.filter(
113
+ (line) => (options.gatedLines === "zero-and-flag" ? isSplitEligible(line, options) : isTrainingLineEligible(line, options)) && line.messages.length > 0 && firstAssistantIndex(line.messages) > 0
114
+ ).map(toRftItem);
115
+ }
116
+ function toJsonl(rows) {
117
+ return rows.map((r) => JSON.stringify(r)).join("\n") + (rows.length ? "\n" : "");
118
+ }
119
+ function isSplitEligible(line, options) {
120
+ if (line.task.split === "search") return true;
121
+ return line.task.split === "holdout" && options.allowHeldOutTrainingData === true;
122
+ }
123
+ function isTrainingLineEligible(line, options) {
124
+ const minimumQualityExclusive = options.minimumQualityExclusive ?? 0;
125
+ if (!Number.isFinite(minimumQualityExclusive)) {
126
+ throw new Error("minimumQualityExclusive must be finite");
127
+ }
128
+ const reward = line.outcome.reward;
129
+ if (reward === null) return false;
130
+ if (!Number.isFinite(reward)) {
131
+ throw new Error(`training reward for rollout "${line.rollout_id}" must be finite`);
132
+ }
133
+ if (reward <= minimumQualityExclusive) return false;
134
+ if (!line.outcome.is_completed || line.outcome.is_truncated || line.outcome.error !== null) {
135
+ return false;
136
+ }
137
+ if (line.outcome.realness_gated === true) return false;
138
+ return isSplitEligible(line, options);
139
+ }
140
+
141
+ export {
142
+ realnessLabels,
143
+ toSftRows,
144
+ toRewardRows,
145
+ toVerifiersRolloutOutput,
146
+ toVerifiersRolloutOutputs,
147
+ toRftItem,
148
+ toRftItems,
149
+ toJsonl,
150
+ isSplitEligible
151
+ };
152
+ //# sourceMappingURL=chunk-OWN5NPMC.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/rollout/exporters.ts"],"sourcesContent":["/**\n * Pure exporters over `tangle.rollout.v1` lines → the training-data shapes\n * the improvement loops feed:\n * - SFT chat JSONL (clean trainable successes, {messages, metadata})\n * - reward rows (every scored line, success or failure, with steps)\n * - Prime Intellect verifiers RolloutOutput (prompt/completion split + reward)\n * - OpenAI RFT items (prompt turns + verdict reference fields)\n *\n * All exporters are pure functions of the lines — filtering (never train on\n * holdout, reward thresholds, the realness gate) happens HERE, on inline\n * labels, no joins.\n *\n * Every exporter takes `MintedRolloutLine[]`, not `RolloutLine[]`: the reward\n * on a minted line has been checked against the anti-Goodhart invariant, and\n * the brand is what stops a hand-built object literal claiming a positive\n * reward on a gamed run from being handed to an exporter that copies it\n * verbatim into training data.\n */\n\nimport type {\n ChatMessage,\n MintedRolloutLine,\n RolloutLine,\n RolloutSplit,\n RolloutStep,\n ToolDef,\n} from './schema'\nimport { assertRewardGate } from './schema'\n\n// ---------------------------------------------------------------------------\n// The realness claims every emitted row carries.\n// ---------------------------------------------------------------------------\n\n/**\n * The gate's two claims, which travel TOGETHER on every emitted row.\n *\n * `realness_gated` alone is ambiguous, and the ambiguity is exploitable:\n * `false` reads as \"we screened it and nothing fired\", so a producer that has no\n * screen at all emitted rows indistinguishable from screened-clean ones, and\n * every consumer of the published dataset read them as clean. The second field\n * is what separates the two claims, and it only removes the ambiguity if it\n * reaches the WIRE — for a round it existed on `RolloutOutcome` and on no\n * exported row shape at all, which left the published rows exactly as ambiguous\n * as before.\n *\n * So there is one helper and every row shape spreads it. A row that states one\n * claim without the other is not constructible by copying the pattern, and\n * `exporters.test.ts` walks every emitted shape to prove none does.\n */\nexport interface RealnessLabels {\n /** The screen's VERDICT: the run faked its success signal. */\n realness_gated: boolean\n /**\n * Whether a screen RAN at all. `true` = it ran, so `realness_gated` is its\n * verdict. `false` = the producer declares it has none. `null` = not stated\n * (pre-unification producers), which is \"unknown\" and never \"clean\".\n */\n realness_screened: boolean | null\n}\n\nexport function realnessLabels(line: MintedRolloutLine): RealnessLabels {\n return {\n realness_gated: line.outcome.realness_gated === true,\n // `?? null` rather than `?? false`: absent means the producer never stated\n // it, and `false` is a load-bearing claim (\"no screen exists\") that would be\n // an invention here.\n realness_screened: line.outcome.realness_screened ?? null,\n }\n}\n\n// ---------------------------------------------------------------------------\n// (a) SFT chat JSONL\n// ---------------------------------------------------------------------------\n\nexport interface TrainingExportOptions {\n /** Include held-out evaluation data in training output. Default false. */\n allowHeldOutTrainingData?: boolean\n /** Require reward to be strictly greater than this value. Default 0. */\n minimumQualityExclusive?: number\n}\n\n/**\n * What a signed-signal exporter (verifiers, RFT) does with lines that are not\n * clean trainable successes — realness-gated lines above all.\n *\n * - 'exclude' — the default, the same fail-closed policy as every\n * other training export: positive, completed,\n * non-gated rows on a trainable split.\n * - 'zero-and-flag' — keep them, at their non-positive (or null) reward,\n * with `RealnessLabels` on the row. The dataset release\n * sets this per `FORMAT_GATE_DISPOSITION`: in these\n * formats the reward is a signed learning signal, so a\n * gamed trajectory at reward 0 is a correct negative,\n * and dropping it would bias the negative population\n * toward honest failures and leave a trainer no example\n * of gaming being penalized. The split policy is NOT\n * relaxed: held-out lines still need the named opt-in.\n *\n * SFT deliberately has no such option — an SFT row is an imitation target and\n * a gamed trajectory must never appear in one at any weight.\n */\nexport type GatedLineDisposition = 'exclude' | 'zero-and-flag'\n\nexport interface SignedSignalExportOptions extends TrainingExportOptions {\n /** Disposition for non-trainable lines. Default 'exclude'. */\n gatedLines?: GatedLineDisposition\n}\n\nexport type SftExportOptions = TrainingExportOptions\n\nexport interface SftRow {\n messages: ChatMessage[]\n metadata: {\n rollout_id: string\n run_id: string\n candidate_id: string | null\n instance_id: string\n reward: number\n } & RealnessLabels\n}\n\n/**\n * Supervised fine-tune rows: the completed conversation of each qualifying\n * line. Fail-closed filters: trainable split only (never holdout/canary),\n * reward strictly above `minimumQualityExclusive` (default 0), realness-gated\n * lines never qualify, gap lines carry no trainable content, and\n * copied-context turns are dropped from the transcript (Harbor ATIF RFC 0001\n * rule 7 — see `ChatMessage.is_copied_context`).\n *\n * `realness_gated` is therefore always `false` on an emitted row. It is carried\n * anyway: an SFT row is a pure imitation target, so the row states its realness\n * claims instead of making the reader know the format's policy, and carrying\n * both flags on all four shapes is what lets the release accounting measure\n * every config with one rule rather than skipping the one whose row shape\n * happened to omit the field.\n */\nexport function toSftRows(lines: MintedRolloutLine[], options: SftExportOptions = {}): SftRow[] {\n for (const line of lines) assertRewardGate(line, 'SFT export')\n return lines\n .filter((line) => isTrainingLineEligible(line, options) && line.messages.length > 0)\n .map((line) => ({\n // Dropped, not kept-and-masked: a copied-context turn was authored by\n // another agent, and an SFT trainer has no notion of \"present but not a\n // target\" — every message it is handed is something to imitate.\n messages: line.messages.filter((message) => message.is_copied_context !== true),\n metadata: {\n rollout_id: line.rollout_id,\n run_id: line.run_id,\n candidate_id: line.candidate_id ?? null,\n instance_id: line.task.instance_id,\n reward: line.outcome.reward as number,\n ...realnessLabels(line),\n },\n }))\n .filter((row) => row.messages.length > 0)\n}\n\n// ---------------------------------------------------------------------------\n// (b) Reward rows\n// ---------------------------------------------------------------------------\n\nexport interface RewardRow {\n /** First user turn — the task prompt. */\n prompt: string\n steps: RolloutStep[]\n reward: number\n metadata: {\n rollout_id: string\n run_id: string\n candidate_id: string | null\n instance_id: string\n split: RolloutSplit\n /**\n * The anti-Goodhart verdict AND whether a screen produced it, carried on the\n * row rather than left implicit in a reward of 0. Zeroing alone is lossy: it\n * makes a run that FAKED its success indistinguishable from one that\n * honestly failed, so a consumer can neither drop the gamed population nor\n * mine it (a labeled gamed trajectory is the training signal for a gaming\n * detector). `realness_gated` alone is lossy the same way in the other\n * direction — see `RealnessLabels`.\n */\n } & RealnessLabels\n}\n\n/**\n * Reward-labeled rows for completed, positive-quality training runs.\n */\nexport function toRewardRows(\n lines: MintedRolloutLine[],\n options: TrainingExportOptions = {},\n): RewardRow[] {\n for (const line of lines) assertRewardGate(line, 'reward-row export')\n return lines\n .filter(\n (line) =>\n isTrainingLineEligible(line, options) &&\n line.messages.some(\n (message) =>\n message.role === 'user' &&\n typeof message.content === 'string' &&\n message.content.length > 0,\n ),\n )\n .map((line) => ({\n prompt: line.messages.find((m) => m.role === 'user')?.content ?? '',\n steps: line.steps ?? [],\n reward: line.outcome.reward as number,\n metadata: {\n rollout_id: line.rollout_id,\n run_id: line.run_id,\n candidate_id: line.candidate_id ?? null,\n instance_id: line.task.instance_id,\n split: line.task.split,\n ...realnessLabels(line),\n },\n }))\n}\n\n// ---------------------------------------------------------------------------\n// (c) Prime Intellect verifiers RolloutOutput\n// ---------------------------------------------------------------------------\n\nexport interface VerifiersTokenUsage {\n input_tokens: number | null\n output_tokens: number | null\n reasoning_tokens: number | null\n cache_read_tokens: number | null\n cache_write_tokens: number | null\n}\n\nexport interface VerifiersRolloutOutput {\n /** Messages through the last turn BEFORE the first assistant turn. */\n prompt: ChatMessage[]\n /** The first assistant turn onward — what the policy produced. */\n completion: ChatMessage[]\n reward: number | null\n metrics: Record<string, unknown>\n tool_defs: ToolDef[]\n token_usage: VerifiersTokenUsage\n info: {\n task: RolloutLine['task']\n policy: RolloutLine['policy']\n rollout_id: string\n run_id: string\n experiment_id: string | null\n candidate_id: string | null\n generation: number | null\n candidate_index: number | null\n role: RolloutLine['role']\n } & RealnessLabels\n}\n\n/** Index of the first assistant turn; messages.length when none exists. */\nfunction firstAssistantIndex(messages: ChatMessage[]): number {\n const index = messages.findIndex((m) => m.role === 'assistant')\n return index === -1 ? messages.length : index\n}\n\nexport function toVerifiersRolloutOutput(line: MintedRolloutLine): VerifiersRolloutOutput {\n assertRewardGate(line, 'verifiers export')\n const split = firstAssistantIndex(line.messages)\n return {\n prompt: line.messages.slice(0, split),\n completion: line.messages.slice(split),\n reward: line.outcome.reward,\n metrics: line.outcome.metrics,\n tool_defs: line.tool_defs,\n token_usage: {\n input_tokens: line.cost.tokens_in,\n output_tokens: line.cost.tokens_out,\n reasoning_tokens: line.cost.tokens_reasoning,\n cache_read_tokens: line.cost.cache_read,\n cache_write_tokens: line.cost.cache_write,\n },\n info: {\n task: line.task,\n policy: line.policy,\n rollout_id: line.rollout_id,\n run_id: line.run_id,\n experiment_id: line.experiment_id ?? null,\n candidate_id: line.candidate_id ?? null,\n generation: line.generation,\n candidate_index: line.candidate_index,\n role: line.role,\n ...realnessLabels(line),\n },\n }\n}\n\nexport function toVerifiersRolloutOutputs(\n lines: MintedRolloutLine[],\n options: SignedSignalExportOptions = {},\n): VerifiersRolloutOutput[] {\n if (options.gatedLines === 'zero-and-flag') {\n return lines\n .filter((line) => isSplitEligible(line, options) && line.messages.length > 0)\n .map(toVerifiersRolloutOutput)\n }\n return lines\n .filter(\n (line) =>\n isTrainingLineEligible(line, options) &&\n firstAssistantIndex(line.messages) > 0 &&\n firstAssistantIndex(line.messages) < line.messages.length,\n )\n .map(toVerifiersRolloutOutput)\n}\n\n// ---------------------------------------------------------------------------\n// (d) OpenAI RFT items\n// ---------------------------------------------------------------------------\n\nexport interface RftItem {\n /** Prompt turns only — the graded completion is re-sampled during RFT. */\n messages: ChatMessage[]\n /** Verdict/label fields the grader references as item.reference.* */\n reference: {\n reward: number | null\n reward_source: string | null\n verdict: unknown\n instance_id: string\n suite: string\n split: RolloutSplit\n rollout_id: string\n } & RealnessLabels\n}\n\nexport function toRftItem(line: MintedRolloutLine): RftItem {\n assertRewardGate(line, 'RFT export')\n const split = firstAssistantIndex(line.messages)\n return {\n messages: line.messages.slice(0, split),\n reference: {\n reward: line.outcome.reward,\n reward_source: line.outcome.reward_source,\n verdict: line.outcome.verdict,\n instance_id: line.task.instance_id,\n suite: line.task.suite,\n split: line.task.split,\n rollout_id: line.rollout_id,\n ...realnessLabels(line),\n },\n }\n}\n\n/** RFT needs a real prompt: lines whose transcript starts with prompt turns. */\nexport function toRftItems(\n lines: MintedRolloutLine[],\n options: SignedSignalExportOptions = {},\n): RftItem[] {\n return lines\n .filter(\n (line) =>\n (options.gatedLines === 'zero-and-flag'\n ? isSplitEligible(line, options)\n : isTrainingLineEligible(line, options)) &&\n line.messages.length > 0 &&\n firstAssistantIndex(line.messages) > 0,\n )\n .map(toRftItem)\n}\n\n// ---------------------------------------------------------------------------\n// Serialization — one JSON object per line, the interchange format for\n// every export. `tangle.rollout.v1` lines and export rows alike.\n// ---------------------------------------------------------------------------\n\nexport function toJsonl(rows: ReadonlyArray<unknown>): string {\n return rows.map((r) => JSON.stringify(r)).join('\\n') + (rows.length ? '\\n' : '')\n}\n\n/**\n * The split half of the training policy alone: `search` is trainable, held-out\n * needs the named opt-in, `dev` and `canary` never ship. This is the ONE check\n * `'zero-and-flag'` does not relax — a gated line is shipped as a labeled\n * negative, not as a licence to train on evaluation data.\n *\n * Exported as the single implementation of that rule: `rl/exporters` applies\n * it on its line paths too, so the two waists cannot drift on which splits are\n * trainable.\n */\nexport function isSplitEligible(\n line: RolloutLine,\n options: Pick<TrainingExportOptions, 'allowHeldOutTrainingData'>,\n): boolean {\n if (line.task.split === 'search') return true\n return line.task.split === 'holdout' && options.allowHeldOutTrainingData === true\n}\n\nfunction isTrainingLineEligible(\n line: RolloutLine,\n options: TrainingExportOptions,\n): line is RolloutLine & { outcome: RolloutLine['outcome'] & { reward: number } } {\n const minimumQualityExclusive = options.minimumQualityExclusive ?? 0\n if (!Number.isFinite(minimumQualityExclusive)) {\n throw new Error('minimumQualityExclusive must be finite')\n }\n\n const reward = line.outcome.reward\n if (reward === null) return false\n if (!Number.isFinite(reward)) {\n throw new Error(`training reward for rollout \"${line.rollout_id}\" must be finite`)\n }\n if (reward <= minimumQualityExclusive) return false\n if (!line.outcome.is_completed || line.outcome.is_truncated || line.outcome.error !== null) {\n return false\n }\n if (line.outcome.realness_gated === true) return false\n return isSplitEligible(line, options)\n}\n"],"mappings":";;;;;AA4DO,SAAS,eAAe,MAAyC;AACtE,SAAO;AAAA,IACL,gBAAgB,KAAK,QAAQ,mBAAmB;AAAA;AAAA;AAAA;AAAA,IAIhD,mBAAmB,KAAK,QAAQ,qBAAqB;AAAA,EACvD;AACF;AAoEO,SAAS,UAAU,OAA4B,UAA4B,CAAC,GAAa;AAC9F,aAAW,QAAQ,MAAO,kBAAiB,MAAM,YAAY;AAC7D,SAAO,MACJ,OAAO,CAAC,SAAS,uBAAuB,MAAM,OAAO,KAAK,KAAK,SAAS,SAAS,CAAC,EAClF,IAAI,CAAC,UAAU;AAAA;AAAA;AAAA;AAAA,IAId,UAAU,KAAK,SAAS,OAAO,CAAC,YAAY,QAAQ,sBAAsB,IAAI;AAAA,IAC9E,UAAU;AAAA,MACR,YAAY,KAAK;AAAA,MACjB,QAAQ,KAAK;AAAA,MACb,cAAc,KAAK,gBAAgB;AAAA,MACnC,aAAa,KAAK,KAAK;AAAA,MACvB,QAAQ,KAAK,QAAQ;AAAA,MACrB,GAAG,eAAe,IAAI;AAAA,IACxB;AAAA,EACF,EAAE,EACD,OAAO,CAAC,QAAQ,IAAI,SAAS,SAAS,CAAC;AAC5C;AAgCO,SAAS,aACd,OACA,UAAiC,CAAC,GACrB;AACb,aAAW,QAAQ,MAAO,kBAAiB,MAAM,mBAAmB;AACpE,SAAO,MACJ;AAAA,IACC,CAAC,SACC,uBAAuB,MAAM,OAAO,KACpC,KAAK,SAAS;AAAA,MACZ,CAAC,YACC,QAAQ,SAAS,UACjB,OAAO,QAAQ,YAAY,YAC3B,QAAQ,QAAQ,SAAS;AAA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