@tangle-network/agent-eval 0.180.0 → 0.182.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 (247) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/README.md +119 -159
  3. package/dist/adapters/http.d.ts +2 -2
  4. package/dist/{agent-profile-B7yErX0q.d.ts → agent-profile-CivaSsSy.d.ts} +4 -4
  5. package/dist/{agent-profile-B7yErX0q.d.ts.map → agent-profile-CivaSsSy.d.ts.map} +1 -1
  6. package/dist/{agent-profile-cell-0gSi5ffD.js → agent-profile-cell-Cv6UA-W_.js} +20 -57
  7. package/dist/agent-profile-cell-Cv6UA-W_.js.map +1 -0
  8. package/dist/{agent-profile-cell-CTOZJUuE.d.ts → agent-profile-cell-s__adRnK.d.ts} +3 -3
  9. package/dist/agent-profile-cell-s__adRnK.d.ts.map +1 -0
  10. package/dist/analyst/index.d.ts +11 -11
  11. package/dist/analyst/index.js +4 -4
  12. package/dist/ast-CP9ae9B0.js +557 -0
  13. package/dist/ast-CP9ae9B0.js.map +1 -0
  14. package/dist/ast-hI-vjW6J.d.ts +457 -0
  15. package/dist/ast-hI-vjW6J.d.ts.map +1 -0
  16. package/dist/{benchmark-command-D4vpnAdO.js → benchmark-command-7X7HPNHh.js} +8 -7
  17. package/dist/{benchmark-command-D4vpnAdO.js.map → benchmark-command-7X7HPNHh.js.map} +1 -1
  18. package/dist/benchmarks/index.d.ts +4 -4
  19. package/dist/benchmarks/index.js +3 -3
  20. package/dist/campaign/index.d.ts +6 -6
  21. package/dist/campaign/index.js +8 -8
  22. package/dist/{campaign-BGEurASO.js → campaign-B2U7WcFz.js} +13 -13
  23. package/dist/{campaign-BGEurASO.js.map → campaign-B2U7WcFz.js.map} +1 -1
  24. package/dist/{campaign-evidence-D8DBLqLI.js → campaign-evidence-PYPUftRy.js} +515 -471
  25. package/dist/campaign-evidence-PYPUftRy.js.map +1 -0
  26. package/dist/cli.js +5 -8
  27. package/dist/cli.js.map +1 -1
  28. package/dist/{client-BlLY6o2w.js → client-CXE-U1SA.js} +3 -1
  29. package/dist/client-CXE-U1SA.js.map +1 -0
  30. package/dist/{client-CuQgX33c.d.ts → client-kh2jOjTK.d.ts} +4 -4
  31. package/dist/{client-CuQgX33c.d.ts.map → client-kh2jOjTK.d.ts.map} +1 -1
  32. package/dist/contract/index.d.ts +13 -13
  33. package/dist/contract/index.js +11 -10
  34. package/dist/contract/index.js.map +1 -1
  35. package/dist/{default-registry-IGDE9XIC.d.ts → default-registry-BwDSWVzg.d.ts} +6 -6
  36. package/dist/{default-registry-IGDE9XIC.d.ts.map → default-registry-BwDSWVzg.d.ts.map} +1 -1
  37. package/dist/{default-registry-aL7xUrUz.js → default-registry-DHWwIwa8.js} +2 -2
  38. package/dist/{default-registry-aL7xUrUz.js.map → default-registry-DHWwIwa8.js.map} +1 -1
  39. package/dist/{define-agent-eval-Cx4Ls9ta.d.ts → define-agent-eval-CwOWWQt_.d.ts} +33 -12
  40. package/dist/define-agent-eval-CwOWWQt_.d.ts.map +1 -0
  41. package/dist/{define-agent-eval-Dzidv34q.js → define-agent-eval-DSIPB4hi.js} +136 -69
  42. package/dist/define-agent-eval-DSIPB4hi.js.map +1 -0
  43. package/dist/{dspy-rlm-engine-xKiWmj_G.js → dspy-rlm-engine-S53V0HhE.js} +2 -2
  44. package/dist/{dspy-rlm-engine-xKiWmj_G.js.map → dspy-rlm-engine-S53V0HhE.js.map} +1 -1
  45. package/dist/{engine-CX8ReXkn.d.ts → engine-DS1cysJy.d.ts} +10 -7
  46. package/dist/engine-DS1cysJy.d.ts.map +1 -0
  47. package/dist/{eval-campaign-Cs-7MiCs.js → eval-campaign-CCEkwzoH.js} +4 -4
  48. package/dist/{eval-campaign-Cs-7MiCs.js.map → eval-campaign-CCEkwzoH.js.map} +1 -1
  49. package/dist/{exact-types-B7LC1EyX.d.ts → exact-types-BZDe0W2D.d.ts} +2 -2
  50. package/dist/{exact-types-B7LC1EyX.d.ts.map → exact-types-BZDe0W2D.d.ts.map} +1 -1
  51. package/dist/experiment/index.d.ts +27 -477
  52. package/dist/experiment/index.d.ts.map +1 -1
  53. package/dist/experiment/index.js +97 -561
  54. package/dist/experiment/index.js.map +1 -1
  55. package/dist/{experiment-tracker-B3TiF5-u.d.ts → experiment-tracker-C7PfnF4b.d.ts} +2 -2
  56. package/dist/{experiment-tracker-B3TiF5-u.d.ts.map → experiment-tracker-C7PfnF4b.d.ts.map} +1 -1
  57. package/dist/{external-optimizer-process-Dlz8YxrT.js → external-optimizer-process-QDRURJAM.js} +3 -3
  58. package/dist/{external-optimizer-process-Dlz8YxrT.js.map → external-optimizer-process-QDRURJAM.js.map} +1 -1
  59. package/dist/{external-optimizer-subprocess-q3VzlGAO.js → external-optimizer-subprocess-D4dzUBZI.js} +3 -2
  60. package/dist/{external-optimizer-subprocess-q3VzlGAO.js.map → external-optimizer-subprocess-D4dzUBZI.js.map} +1 -1
  61. package/dist/{feedback-trajectory-eHWNv5Aj.d.ts → feedback-trajectory-CXmtITBo.d.ts} +3 -3
  62. package/dist/{feedback-trajectory-eHWNv5Aj.d.ts.map → feedback-trajectory-CXmtITBo.d.ts.map} +1 -1
  63. package/dist/hosted/index.d.ts +2 -2
  64. package/dist/hosted/index.d.ts.map +1 -1
  65. package/dist/hosted/index.js +1 -1
  66. package/dist/{index-BxWvILU8.d.ts → index-Bp_6sj3x.d.ts} +109 -56
  67. package/dist/index-Bp_6sj3x.d.ts.map +1 -0
  68. package/dist/{index-e7LXeRVa.d.ts → index-CJ3LhKIX.d.ts} +2 -2
  69. package/dist/{index-e7LXeRVa.d.ts.map → index-CJ3LhKIX.d.ts.map} +1 -1
  70. package/dist/{index-CiUjjEIa.d.ts → index-DbG8QbH9.d.ts} +8 -8
  71. package/dist/{index-CiUjjEIa.d.ts.map → index-DbG8QbH9.d.ts.map} +1 -1
  72. package/dist/{index-DxNYmx4a.d.ts → index-DoykkxW0.d.ts} +11 -11
  73. package/dist/{index-DxNYmx4a.d.ts.map → index-DoykkxW0.d.ts.map} +1 -1
  74. package/dist/index.d.ts +28 -28
  75. package/dist/index.js +27 -18
  76. package/dist/index.js.map +1 -1
  77. package/dist/{insight-report-DETqPc_A.d.ts → insight-report-D1qa0HWs.d.ts} +9 -5
  78. package/dist/{insight-report-DETqPc_A.d.ts.map → insight-report-D1qa0HWs.d.ts.map} +1 -1
  79. package/dist/{integrity-DH5ng72x.js → integrity-CgQce0FV.js} +2 -2
  80. package/dist/{integrity-DH5ng72x.js.map → integrity-CgQce0FV.js.map} +1 -1
  81. package/dist/{integrity-BKTcA-HP.d.ts → integrity-rGOfSUle.d.ts} +2 -2
  82. package/dist/{integrity-BKTcA-HP.d.ts.map → integrity-rGOfSUle.d.ts.map} +1 -1
  83. package/dist/{ledger-core-Cs9f7385.js → journal-Cs9f7385.js} +1 -1
  84. package/dist/journal-Cs9f7385.js.map +1 -0
  85. package/dist/{judge-calibration-C5CbMYce.d.ts → judge-calibration-DFtEMlde.d.ts} +31 -2
  86. package/dist/judge-calibration-DFtEMlde.d.ts.map +1 -0
  87. package/dist/{judge-calibration-BnpVKtnb.js → judge-calibration-DYmaBtJr.js} +48 -2
  88. package/dist/{judge-calibration-BnpVKtnb.js.map → judge-calibration-DYmaBtJr.js.map} +1 -1
  89. package/dist/ledger-core/index.d.ts +1 -1
  90. package/dist/ledger-core/index.js +1 -1
  91. package/dist/{llm-judge-v80Kmu9g.js → llm-judge-BPFR12VJ.js} +645 -456
  92. package/dist/llm-judge-BPFR12VJ.js.map +1 -0
  93. package/dist/{matrix-DeMmnWrP.d.ts → matrix-CyhW-vgJ.d.ts} +2 -2
  94. package/dist/{matrix-DeMmnWrP.d.ts.map → matrix-CyhW-vgJ.d.ts.map} +1 -1
  95. package/dist/meta-eval/index.d.ts +138 -7
  96. package/dist/meta-eval/index.d.ts.map +1 -1
  97. package/dist/meta-eval/index.js +245 -97
  98. package/dist/meta-eval/index.js.map +1 -1
  99. package/dist/{mint-Cc1_zwRQ.js → mint-ySIIkKlV.js} +2 -2
  100. package/dist/{mint-Cc1_zwRQ.js.map → mint-ySIIkKlV.js.map} +1 -1
  101. package/dist/multishot/golden/index.d.ts +1 -1
  102. package/dist/multishot/index.d.ts +2 -2
  103. package/dist/openapi.json +1 -1
  104. package/dist/outcome-store-BXlkwMPR.js +131 -0
  105. package/dist/outcome-store-BXlkwMPR.js.map +1 -0
  106. package/dist/{outcome-store-BYHIuO0e.d.ts → outcome-store-CNt4iZ67.d.ts} +18 -25
  107. package/dist/outcome-store-CNt4iZ67.d.ts.map +1 -0
  108. package/dist/{paired-arms-D4aeIHUy.js → paired-arms-JSDcs2zx.js} +16 -3
  109. package/dist/paired-arms-JSDcs2zx.js.map +1 -0
  110. package/dist/{paired-promotion-decision-CGzg0cI_.d.ts → paired-promotion-decision-DPsMQm-0.d.ts} +13 -7
  111. package/dist/{paired-promotion-decision-CGzg0cI_.d.ts.map → paired-promotion-decision-DPsMQm-0.d.ts.map} +1 -1
  112. package/dist/pipelines/index.js +1 -1
  113. package/dist/{power-and-mde-B8F2RdcD.js → power-and-mde-BJOpt6pq.js} +2 -2
  114. package/dist/{power-and-mde-B8F2RdcD.js.map → power-and-mde-BJOpt6pq.js.map} +1 -1
  115. package/dist/{produced-state-Cv0kJJuP.js → produced-state-CkoG43gI.js} +3 -3
  116. package/dist/{produced-state-Cv0kJJuP.js.map → produced-state-CkoG43gI.js.map} +1 -1
  117. package/dist/profile-cell.d.ts +1 -1
  118. package/dist/profile-cell.js +1 -1
  119. package/dist/{promotion-policy-DWOm70gx.js → promotion-policy-BKFrl7CC.js} +28 -40
  120. package/dist/promotion-policy-BKFrl7CC.js.map +1 -0
  121. package/dist/{registry-ByVld1-5.d.ts → registry-BRbB6Y0v.d.ts} +4 -4
  122. package/dist/{registry-ByVld1-5.d.ts.map → registry-BRbB6Y0v.d.ts.map} +1 -1
  123. package/dist/{release-confidence-BAcNYOf1.d.ts → release-confidence-BcqeQTHW.d.ts} +3 -3
  124. package/dist/{release-confidence-BAcNYOf1.d.ts.map → release-confidence-BcqeQTHW.d.ts.map} +1 -1
  125. package/dist/{release-confidence-BcGCclTB.js → release-confidence-DMg8n18l.js} +2 -2
  126. package/dist/{release-confidence-BcGCclTB.js.map → release-confidence-DMg8n18l.js.map} +1 -1
  127. package/dist/{report-command-V1ecVgAv.js → report-command-BvqaxlVG.js} +32 -201
  128. package/dist/report-command-BvqaxlVG.js.map +1 -0
  129. package/dist/reporting.d.ts +4 -4
  130. package/dist/reporting.js +5 -5
  131. package/dist/{researcher-jsW1X94L.d.ts → researcher-64T49THL.d.ts} +6 -6
  132. package/dist/{researcher-jsW1X94L.d.ts.map → researcher-64T49THL.d.ts.map} +1 -1
  133. package/dist/{reward-hacking-ZXEi9VCq.d.ts → reward-hacking-uzO_ihep.d.ts} +2 -2
  134. package/dist/{reward-hacking-ZXEi9VCq.d.ts.map → reward-hacking-uzO_ihep.d.ts.map} +1 -1
  135. package/dist/rl.d.ts +53 -99
  136. package/dist/rl.d.ts.map +1 -1
  137. package/dist/rl.js +183 -170
  138. package/dist/rl.js.map +1 -1
  139. package/dist/rollout/index.d.ts +1 -1
  140. package/dist/rollout/index.js +2 -3
  141. package/dist/{rollout-DmoJVqrF.js → rollout-BsN6pOU7.js} +154 -13
  142. package/dist/rollout-BsN6pOU7.js.map +1 -0
  143. package/dist/rubric-predictive-validity-Bmj2_cll.d.ts +79 -0
  144. package/dist/rubric-predictive-validity-Bmj2_cll.d.ts.map +1 -0
  145. package/dist/rubric-predictive-validity-CCK-1B7w.js +178 -0
  146. package/dist/rubric-predictive-validity-CCK-1B7w.js.map +1 -0
  147. package/dist/{run-record-DTv1MdjK.d.ts → run-record-BiTWauyO.d.ts} +2 -2
  148. package/dist/{run-record-DTv1MdjK.d.ts.map → run-record-BiTWauyO.d.ts.map} +1 -1
  149. package/dist/run-record-DPa2PeHx.js +464 -0
  150. package/dist/run-record-DPa2PeHx.js.map +1 -0
  151. package/dist/{run-record-DQpSf7t-.js → run-record-DualPTn2.js} +2 -2
  152. package/dist/{run-record-DQpSf7t-.js.map → run-record-DualPTn2.js.map} +1 -1
  153. package/dist/{semantic-concept-judge-Bi6_iGqg.js → semantic-concept-judge-Bm5JDEKO.js} +3 -3
  154. package/dist/{semantic-concept-judge-Bi6_iGqg.js.map → semantic-concept-judge-Bm5JDEKO.js.map} +1 -1
  155. package/dist/{sequential-B5gXgcyp.js → sequential-D_xHrBZe.js} +42 -25
  156. package/dist/sequential-D_xHrBZe.js.map +1 -0
  157. package/dist/{series-convergence-DeG33RpC.d.ts → series-convergence-BnMs_uAr.d.ts} +3 -3
  158. package/dist/{series-convergence-DeG33RpC.d.ts.map → series-convergence-BnMs_uAr.d.ts.map} +1 -1
  159. package/dist/{skillopt-optimization-method-C3oYul8v.js → skillopt-optimization-method-B2IEaCeF.js} +5 -5
  160. package/dist/{skillopt-optimization-method-C3oYul8v.js.map → skillopt-optimization-method-B2IEaCeF.js.map} +1 -1
  161. package/dist/{statistical-heldout-0La5ZTlv.d.ts → statistical-heldout-CpVd6FmY.d.ts} +207 -144
  162. package/dist/statistical-heldout-CpVd6FmY.d.ts.map +1 -0
  163. package/dist/{store-tool-spans-4J1EDElP.d.ts → store-tool-spans-Dt-YdAuE.d.ts} +6 -6
  164. package/dist/{store-tool-spans-4J1EDElP.d.ts.map → store-tool-spans-Dt-YdAuE.d.ts.map} +1 -1
  165. package/dist/{summary-report-gMrbYawB.d.ts → summary-report-D1h4dlrK.d.ts} +3 -3
  166. package/dist/{summary-report-gMrbYawB.d.ts.map → summary-report-D1h4dlrK.d.ts.map} +1 -1
  167. package/dist/{summary-report-B16xy9Kd.js → summary-report-IGAOtgPd.js} +4 -4
  168. package/dist/{summary-report-B16xy9Kd.js.map → summary-report-IGAOtgPd.js.map} +1 -1
  169. package/dist/supervisor-run/index.d.ts +20 -44
  170. package/dist/supervisor-run/index.d.ts.map +1 -1
  171. package/dist/supervisor-run/index.js +8 -8
  172. package/dist/supervisor-run/index.js.map +1 -1
  173. package/dist/{terminal-record-BtPwKTSr.js → terminal-record-BHocwFw1.js} +5 -22
  174. package/dist/terminal-record-BHocwFw1.js.map +1 -0
  175. package/dist/{tool-groups-2QA0S7dK.d.ts → tool-groups-B2bSNaJB.d.ts} +3 -3
  176. package/dist/tool-groups-B2bSNaJB.d.ts.map +1 -0
  177. package/dist/{tool-waste-B9tdWV6g.js → tool-waste-C7MU9u1e.js} +2 -2
  178. package/dist/{tool-waste-B9tdWV6g.js.map → tool-waste-C7MU9u1e.js.map} +1 -1
  179. package/dist/trace-repair/index.d.ts +2 -2
  180. package/dist/traces.d.ts +6 -6
  181. package/dist/traces.js +1 -1
  182. package/dist/{types-lPkDQNqJ.d.ts → types-BlIJ5URG.d.ts} +17 -26
  183. package/dist/types-BlIJ5URG.d.ts.map +1 -0
  184. package/dist/{types-BmlkCrg0.d.ts → types-BvZoPTGa.d.ts} +3 -3
  185. package/dist/{types-BmlkCrg0.d.ts.map → types-BvZoPTGa.d.ts.map} +1 -1
  186. package/dist/{types-gvRsyJLh.d.ts → types-CBbLtr2J.d.ts} +38 -3
  187. package/dist/{types-gvRsyJLh.d.ts.map → types-CBbLtr2J.d.ts.map} +1 -1
  188. package/dist/{types-C34V4Vto.d.ts → types-CS0qk_Yp.d.ts} +4 -4
  189. package/dist/{types-C34V4Vto.d.ts.map → types-CS0qk_Yp.d.ts.map} +1 -1
  190. package/dist/{types-DzuaM493.d.ts → types-D7gEdPoQ.d.ts} +3 -3
  191. package/dist/{types-DzuaM493.d.ts.map → types-D7gEdPoQ.d.ts.map} +1 -1
  192. package/dist/wire/index.d.ts +2 -2
  193. package/docs/adapters-observability.md +14 -0
  194. package/docs/campaign-proposers.md +86 -128
  195. package/docs/charter.md +108 -112
  196. package/docs/concepts.md +157 -69
  197. package/docs/design/mlbenchmarks-book-review.md +440 -0
  198. package/docs/design/mlbenchmarks-review/observations.json +713 -0
  199. package/docs/design/mlbenchmarks-review/probes.mts +476 -0
  200. package/docs/design/mlbenchmarks-review/sources.json +200 -0
  201. package/docs/design/self-improvement-evidence-audit.md +263 -0
  202. package/docs/design.md +2 -1
  203. package/docs/eval-surface-map.md +95 -42
  204. package/docs/evaluation-integrity.md +220 -0
  205. package/docs/experiment.md +111 -55
  206. package/docs/feature-guide.md +5 -6
  207. package/docs/hosted-ingest-spec.md +4 -11
  208. package/docs/insight-report.md +187 -455
  209. package/docs/outcome-validity.md +182 -0
  210. package/docs/product-eval-adoption.md +1 -2
  211. package/docs/public-api.md +5 -8
  212. package/docs/research-report-methodology.md +7 -7
  213. package/docs/search-history-receipts.md +8 -0
  214. package/docs/statistical-evidence.md +129 -0
  215. package/docs/verdicts.md +76 -49
  216. package/package.json +1 -1
  217. package/dist/agent-profile-cell-0gSi5ffD.js.map +0 -1
  218. package/dist/agent-profile-cell-CTOZJUuE.d.ts.map +0 -1
  219. package/dist/campaign-evidence-D8DBLqLI.js.map +0 -1
  220. package/dist/client-BlLY6o2w.js.map +0 -1
  221. package/dist/define-agent-eval-Cx4Ls9ta.d.ts.map +0 -1
  222. package/dist/define-agent-eval-Dzidv34q.js.map +0 -1
  223. package/dist/engine-CX8ReXkn.d.ts.map +0 -1
  224. package/dist/index-BxWvILU8.d.ts.map +0 -1
  225. package/dist/judge-calibration-C5CbMYce.d.ts.map +0 -1
  226. package/dist/ledger-core-Cs9f7385.js.map +0 -1
  227. package/dist/llm-judge-v80Kmu9g.js.map +0 -1
  228. package/dist/opencode-sqlite-CNw3vubS.js +0 -145
  229. package/dist/opencode-sqlite-CNw3vubS.js.map +0 -1
  230. package/dist/outcome-store-BYHIuO0e.d.ts.map +0 -1
  231. package/dist/outcome-store-ChBKlTd_.js +0 -75
  232. package/dist/outcome-store-ChBKlTd_.js.map +0 -1
  233. package/dist/paired-arms-D4aeIHUy.js.map +0 -1
  234. package/dist/promotion-policy-DWOm70gx.js.map +0 -1
  235. package/dist/report-command-V1ecVgAv.js.map +0 -1
  236. package/dist/rollout-DmoJVqrF.js.map +0 -1
  237. package/dist/rubric-predictive-validity-2D5Gw9z9.js +0 -131
  238. package/dist/rubric-predictive-validity-2D5Gw9z9.js.map +0 -1
  239. package/dist/rubric-predictive-validity-Dl1dvKCv.d.ts +0 -75
  240. package/dist/rubric-predictive-validity-Dl1dvKCv.d.ts.map +0 -1
  241. package/dist/run-record-CR63CpHK.js +0 -216
  242. package/dist/run-record-CR63CpHK.js.map +0 -1
  243. package/dist/sequential-B5gXgcyp.js.map +0 -1
  244. package/dist/statistical-heldout-0La5ZTlv.d.ts.map +0 -1
  245. package/dist/terminal-record-BtPwKTSr.js.map +0 -1
  246. package/dist/tool-groups-2QA0S7dK.d.ts.map +0 -1
  247. package/dist/types-lPkDQNqJ.d.ts.map +0 -1
@@ -1,145 +0,0 @@
1
- import { createRequire } from "node:module";
2
- import { join } from "node:path";
3
- import { homedir } from "node:os";
4
- //#region src/rollout/readers/opencode-sqlite.ts
5
- /**
6
- * Read-only backfill reader over the opencode sqlite store
7
- * (~/.local/share/opencode/opencode.db) → canonical chat-with-tools messages.
8
- *
9
- * Schema consumed (observed, 2026-07): `session` rows carry directory /
10
- * parent_id / agent / model / cost / tokens_*; `message` rows carry a JSON
11
- * `data` blob ({role, modelID, providerID, tokens, cost, finish}); `part`
12
- * rows carry the actual content ({type: text|reasoning|tool|step-start|
13
- * step-finish|snapshot…}). Tool parts hold {callID, state:{input, output,
14
- * status}} — both the call and its result, which we split into an assistant
15
- * tool_call plus a role:"tool" result message.
16
- *
17
- * The store is mutable and can be corrupt (a `.corrupt-bak` sibling ships
18
- * next to it in the wild), so `openOpencodeDb` returns null instead of
19
- * throwing — callers record a gap line, never crash the backfill.
20
- */
21
- const DEFAULT_OPENCODE_DB = join(homedir(), ".local", "share", "opencode", "opencode.db");
22
- /** Open the store read-only; null = unavailable/corrupt (caller records a gap). */
23
- async function openOpencodeDb(path = DEFAULT_OPENCODE_DB) {
24
- try {
25
- const { DatabaseSync } = createRequire(import.meta.url)("node:sqlite");
26
- const db = new DatabaseSync(path, { readOnly: true });
27
- db.prepare("SELECT id FROM session LIMIT 1").get();
28
- return db;
29
- } catch {
30
- return null;
31
- }
32
- }
33
- const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
34
- function parseSessionRow(row) {
35
- let model = null;
36
- if (typeof row.model === "string" && row.model.length > 0) try {
37
- const parsed = JSON.parse(row.model);
38
- if (isRecord(parsed)) model = parsed;
39
- } catch {
40
- model = null;
41
- }
42
- return {
43
- id: String(row.id),
44
- parentId: row.parent_id === null || row.parent_id === void 0 ? null : String(row.parent_id),
45
- directory: String(row.directory),
46
- agent: row.agent === null || row.agent === void 0 ? null : String(row.agent),
47
- model,
48
- costUsd: Number(row.cost ?? 0),
49
- tokensInput: Number(row.tokens_input ?? 0),
50
- tokensOutput: Number(row.tokens_output ?? 0),
51
- tokensReasoning: Number(row.tokens_reasoning ?? 0),
52
- tokensCacheRead: Number(row.tokens_cache_read ?? 0),
53
- tokensCacheWrite: Number(row.tokens_cache_write ?? 0),
54
- timeCreated: Number(row.time_created ?? 0),
55
- timeUpdated: Number(row.time_updated ?? 0)
56
- };
57
- }
58
- const SESSION_COLUMNS = "id, parent_id, directory, agent, model, cost, tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write, time_created, time_updated";
59
- /** Sessions whose cwd is `directory` (the worker-clone join key). */
60
- function findOpencodeSessionsByDirectory(db, directory) {
61
- return db.prepare(`SELECT ${SESSION_COLUMNS} FROM session WHERE directory = ? ORDER BY time_created`).all(directory).map(parseSessionRow);
62
- }
63
- function toolResultContent(output) {
64
- if (typeof output === "string") return output;
65
- if (output === null || output === void 0) return "";
66
- return JSON.stringify(output);
67
- }
68
- /**
69
- * Convert one session's message+part rows into canonical messages.
70
- * An opencode assistant message row spans several model steps; each step's
71
- * parts (reasoning → text → tool …) become one assistant message followed by
72
- * the role:"tool" results of its calls, preserving order.
73
- */
74
- function readOpencodeSessionMessages(db, sessionId) {
75
- const messageRows = db.prepare("SELECT id, data FROM message WHERE session_id = ? ORDER BY time_created, id").all(sessionId);
76
- const partsStmt = db.prepare("SELECT data FROM part WHERE message_id = ? ORDER BY id");
77
- const messages = [];
78
- for (const messageRow of messageRows) {
79
- let data;
80
- try {
81
- const parsed = JSON.parse(messageRow.data);
82
- if (!isRecord(parsed)) continue;
83
- data = parsed;
84
- } catch {
85
- continue;
86
- }
87
- const parts = [];
88
- for (const row of partsStmt.all(messageRow.id)) try {
89
- const parsed = JSON.parse(row.data);
90
- if (isRecord(parsed)) parts.push(parsed);
91
- } catch {}
92
- if (data.role === "user") {
93
- const text = parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("\n");
94
- messages.push({
95
- role: "user",
96
- content: text
97
- });
98
- continue;
99
- }
100
- if (data.role !== "assistant") continue;
101
- const steps = [];
102
- let current = [];
103
- for (const part of parts) {
104
- if (part.type === "step-start") {
105
- if (current.length > 0) steps.push(current);
106
- current = [];
107
- continue;
108
- }
109
- if (part.type === "step-finish" || part.type === "snapshot" || part.type === "patch") continue;
110
- current.push(part);
111
- }
112
- if (current.length > 0) steps.push(current);
113
- for (const step of steps) {
114
- const reasoning = step.filter((p) => p.type === "reasoning" && typeof p.text === "string" && p.text.length > 0).map((p) => p.text).join("\n");
115
- const text = step.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("\n");
116
- const toolParts = step.filter((p) => p.type === "tool" && typeof p.callID === "string");
117
- const toolCalls = toolParts.map((p) => ({
118
- id: p.callID,
119
- type: "function",
120
- function: {
121
- name: p.tool ?? "unknown",
122
- arguments: JSON.stringify(p.state?.input ?? {})
123
- }
124
- }));
125
- if (reasoning.length === 0 && text.length === 0 && toolCalls.length === 0) continue;
126
- messages.push({
127
- role: "assistant",
128
- content: text.length > 0 ? text : null,
129
- ...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
130
- ...toolCalls.length > 0 ? { tool_calls: toolCalls } : {}
131
- });
132
- for (const p of toolParts) messages.push({
133
- role: "tool",
134
- tool_call_id: p.callID,
135
- name: p.tool ?? "unknown",
136
- content: toolResultContent(p.state?.output)
137
- });
138
- }
139
- }
140
- return messages;
141
- }
142
- //#endregion
143
- export { readOpencodeSessionMessages as i, findOpencodeSessionsByDirectory as n, openOpencodeDb as r, DEFAULT_OPENCODE_DB as t };
144
-
145
- //# sourceMappingURL=opencode-sqlite-CNw3vubS.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"opencode-sqlite-CNw3vubS.js","names":[],"sources":["../src/rollout/readers/opencode-sqlite.ts"],"sourcesContent":["/**\n * Read-only backfill reader over the opencode sqlite store\n * (~/.local/share/opencode/opencode.db) → canonical chat-with-tools messages.\n *\n * Schema consumed (observed, 2026-07): `session` rows carry directory /\n * parent_id / agent / model / cost / tokens_*; `message` rows carry a JSON\n * `data` blob ({role, modelID, providerID, tokens, cost, finish}); `part`\n * rows carry the actual content ({type: text|reasoning|tool|step-start|\n * step-finish|snapshot…}). Tool parts hold {callID, state:{input, output,\n * status}} — both the call and its result, which we split into an assistant\n * tool_call plus a role:\"tool\" result message.\n *\n * The store is mutable and can be corrupt (a `.corrupt-bak` sibling ships\n * next to it in the wild), so `openOpencodeDb` returns null instead of\n * throwing — callers record a gap line, never crash the backfill.\n */\n\nimport { createRequire } from 'node:module'\nimport { homedir } from 'node:os'\nimport { join } from 'node:path'\nimport type { DatabaseSync } from 'node:sqlite'\nimport type { ChatMessage, ChatToolCall } from '../schema'\n\nexport const DEFAULT_OPENCODE_DB = join(homedir(), '.local', 'share', 'opencode', 'opencode.db')\n\nexport interface OpencodeSessionRow {\n id: string\n parentId: string | null\n directory: string\n agent: string | null\n /** Raw session.model JSON: {id, providerID, variant} where present. */\n model: { id?: string; providerID?: string } | null\n costUsd: number\n tokensInput: number\n tokensOutput: number\n tokensReasoning: number\n tokensCacheRead: number\n tokensCacheWrite: number\n timeCreated: number\n timeUpdated: number\n}\n\n// `node:sqlite` is loaded through CommonJS `require`, not `import()`. esbuild\n// (bundling) and Vite (tests) both rewrite a dynamic import and strip the\n// `node:` prefix under an es20xx target, turning this builtin into a bogus\n// \"sqlite\" package lookup; composing the specifier at runtime does not reliably\n// defeat that (it still resolved through Vite's transform in some workers, so\n// the failure moved around as test files were added). A require obtained from\n// `createRequire` is not an analyzable module reference in either tool, so\n// neither can rewrite it.\n/** Open the store read-only; null = unavailable/corrupt (caller records a gap). */\nexport async function openOpencodeDb(\n path: string = DEFAULT_OPENCODE_DB,\n): Promise<DatabaseSync | null> {\n try {\n // Keep Node-only module initialization inside the Node-only operation.\n // Root imports are shared with edge consumers that do not define import.meta.url.\n const nodeRequire = createRequire(import.meta.url)\n const { DatabaseSync } = nodeRequire('node:sqlite') as typeof import('node:sqlite')\n const db = new DatabaseSync(path, { readOnly: true })\n // Probe: a corrupt store can open() fine and fail on first page read.\n db.prepare('SELECT id FROM session LIMIT 1').get()\n return db\n } catch {\n return null\n }\n}\n\nconst isRecord = (v: unknown): v is Record<string, unknown> =>\n typeof v === 'object' && v !== null && !Array.isArray(v)\n\nfunction parseSessionRow(row: Record<string, unknown>): OpencodeSessionRow {\n let model: OpencodeSessionRow['model'] = null\n if (typeof row.model === 'string' && row.model.length > 0) {\n try {\n const parsed: unknown = JSON.parse(row.model)\n if (isRecord(parsed)) model = parsed as { id?: string; providerID?: string }\n } catch {\n model = null\n }\n }\n return {\n id: String(row.id),\n parentId: row.parent_id === null || row.parent_id === undefined ? null : String(row.parent_id),\n directory: String(row.directory),\n agent: row.agent === null || row.agent === undefined ? null : String(row.agent),\n model,\n costUsd: Number(row.cost ?? 0),\n tokensInput: Number(row.tokens_input ?? 0),\n tokensOutput: Number(row.tokens_output ?? 0),\n tokensReasoning: Number(row.tokens_reasoning ?? 0),\n tokensCacheRead: Number(row.tokens_cache_read ?? 0),\n tokensCacheWrite: Number(row.tokens_cache_write ?? 0),\n timeCreated: Number(row.time_created ?? 0),\n timeUpdated: Number(row.time_updated ?? 0),\n }\n}\n\nconst SESSION_COLUMNS =\n 'id, parent_id, directory, agent, model, cost, tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write, time_created, time_updated'\n\n/** Sessions whose cwd is `directory` (the worker-clone join key). */\nexport function findOpencodeSessionsByDirectory(\n db: DatabaseSync,\n directory: string,\n): OpencodeSessionRow[] {\n const rows = db\n .prepare(`SELECT ${SESSION_COLUMNS} FROM session WHERE directory = ? ORDER BY time_created`)\n .all(directory) as Array<Record<string, unknown>>\n return rows.map(parseSessionRow)\n}\n\ninterface OpencodePart {\n type?: string\n text?: string\n tool?: string\n callID?: string\n state?: { status?: string; input?: unknown; output?: unknown }\n}\n\nfunction toolResultContent(output: unknown): string {\n if (typeof output === 'string') return output\n if (output === null || output === undefined) return ''\n return JSON.stringify(output)\n}\n\n/**\n * Convert one session's message+part rows into canonical messages.\n * An opencode assistant message row spans several model steps; each step's\n * parts (reasoning → text → tool …) become one assistant message followed by\n * the role:\"tool\" results of its calls, preserving order.\n */\nexport function readOpencodeSessionMessages(db: DatabaseSync, sessionId: string): ChatMessage[] {\n const messageRows = db\n .prepare('SELECT id, data FROM message WHERE session_id = ? ORDER BY time_created, id')\n .all(sessionId) as Array<{ id: string; data: string }>\n const partsStmt = db.prepare('SELECT data FROM part WHERE message_id = ? ORDER BY id')\n\n const messages: ChatMessage[] = []\n for (const messageRow of messageRows) {\n let data: Record<string, unknown>\n try {\n const parsed: unknown = JSON.parse(messageRow.data)\n if (!isRecord(parsed)) continue\n data = parsed\n } catch {\n continue\n }\n const parts: OpencodePart[] = []\n for (const row of partsStmt.all(messageRow.id) as Array<{ data: string }>) {\n try {\n const parsed: unknown = JSON.parse(row.data)\n if (isRecord(parsed)) parts.push(parsed as OpencodePart)\n } catch {\n // Malformed part payload: skip the part, keep the message.\n }\n }\n\n if (data.role === 'user') {\n const text = parts\n .filter((p) => p.type === 'text' && typeof p.text === 'string')\n .map((p) => p.text as string)\n .join('\\n')\n messages.push({ role: 'user', content: text })\n continue\n }\n if (data.role !== 'assistant') continue\n\n // Split the row into steps at step-start boundaries; parts before the\n // first step-start (none observed, but tolerated) form an implicit step.\n const steps: OpencodePart[][] = []\n let current: OpencodePart[] = []\n for (const part of parts) {\n if (part.type === 'step-start') {\n if (current.length > 0) steps.push(current)\n current = []\n continue\n }\n if (part.type === 'step-finish' || part.type === 'snapshot' || part.type === 'patch') continue\n current.push(part)\n }\n if (current.length > 0) steps.push(current)\n\n for (const step of steps) {\n const reasoning = step\n .filter((p) => p.type === 'reasoning' && typeof p.text === 'string' && p.text.length > 0)\n .map((p) => p.text as string)\n .join('\\n')\n const text = step\n .filter((p) => p.type === 'text' && typeof p.text === 'string')\n .map((p) => p.text as string)\n .join('\\n')\n const toolParts = step.filter((p) => p.type === 'tool' && typeof p.callID === 'string')\n const toolCalls: ChatToolCall[] = toolParts.map((p) => ({\n id: p.callID as string,\n type: 'function',\n function: {\n name: p.tool ?? 'unknown',\n arguments: JSON.stringify(p.state?.input ?? {}),\n },\n }))\n if (reasoning.length === 0 && text.length === 0 && toolCalls.length === 0) continue\n messages.push({\n role: 'assistant',\n content: text.length > 0 ? text : null,\n ...(reasoning.length > 0 ? { reasoning_content: reasoning } : {}),\n ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),\n })\n for (const p of toolParts) {\n messages.push({\n role: 'tool',\n tool_call_id: p.callID as string,\n name: p.tool ?? 'unknown',\n content: toolResultContent(p.state?.output),\n })\n }\n }\n }\n return messages\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAuBA,MAAa,sBAAsB,KAAK,QAAQ,GAAG,UAAU,SAAS,YAAY,aAAa;;AA4B/F,eAAsB,eACpB,OAAe,qBACe;CAC9B,IAAI;EAIF,MAAM,EAAE,iBADY,cAAc,OAAO,KAAK,GACX,CAAC,CAAC,aAAa;EAClD,MAAM,KAAK,IAAI,aAAa,MAAM,EAAE,UAAU,KAAK,CAAC;EAEpD,GAAG,QAAQ,gCAAgC,CAAC,CAAC,IAAI;EACjD,OAAO;CACT,QAAQ;EACN,OAAO;CACT;AACF;AAEA,MAAM,YAAY,MAChB,OAAO,MAAM,YAAY,MAAM,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KAAK,SAAQ,gBAAqB,QAAQ;;UAKjC;EACf;EACA;;cAGW,kCAAkC;UACrC;UACA;UACA;UACA;EAER,YAAY,SAAS;UAKP;EAKR,OAAO,SAAS,oBAAoB;UAiB5B;EAuBR,OAAO,gBAAgB,QAAQ;EAI/B,KAAK,SAAS,gBAAgB,QAAQ"}
@@ -1,75 +0,0 @@
1
- //#region src/meta-eval/outcome-store.ts
2
- var InMemoryOutcomeStore = class {
3
- items = [];
4
- async append(outcome) {
5
- this.items.push({ ...outcome });
6
- }
7
- async forRun(runId) {
8
- return this.items.filter((o) => o.runId === runId).map((o) => ({ ...o }));
9
- }
10
- async list(filter = {}) {
11
- return this.items.filter((o) => matches(o, filter)).map((o) => ({ ...o }));
12
- }
13
- };
14
- var FileSystemOutcomeStore = class {
15
- dir;
16
- maxBytes;
17
- memo;
18
- loaded = false;
19
- constructor(options) {
20
- this.dir = options.dir;
21
- this.maxBytes = options.maxBytes ?? 32 * 1024 * 1024;
22
- }
23
- async ensureDir() {
24
- await (await import("node:fs/promises")).mkdir(this.dir, { recursive: true });
25
- }
26
- async append(outcome) {
27
- await this.ensureDir();
28
- const fs = await import("node:fs/promises");
29
- const path = await import("node:path");
30
- const active = path.join(this.dir, "outcomes.ndjson");
31
- try {
32
- if ((await fs.stat(active)).size >= this.maxBytes) await fs.rename(active, path.join(this.dir, `outcomes.${Date.now()}.ndjson`));
33
- } catch {}
34
- await fs.appendFile(active, `${JSON.stringify(outcome)}\n`, "utf8");
35
- if (this.memo) await this.memo.append(outcome);
36
- }
37
- async load() {
38
- if (this.loaded && this.memo) return this.memo;
39
- const fs = await import("node:fs/promises");
40
- const path = await import("node:path");
41
- const memo = new InMemoryOutcomeStore();
42
- try {
43
- const entries = await fs.readdir(this.dir);
44
- for (const file of entries) {
45
- if (!file.endsWith(".ndjson")) continue;
46
- const content = await fs.readFile(path.join(this.dir, file), "utf8");
47
- for (const line of content.split("\n")) {
48
- if (!line.trim()) continue;
49
- await memo.append(JSON.parse(line));
50
- }
51
- }
52
- } catch {}
53
- this.memo = memo;
54
- this.loaded = true;
55
- return memo;
56
- }
57
- async forRun(runId) {
58
- return (await this.load()).forRun(runId);
59
- }
60
- async list(filter) {
61
- return (await this.load()).list(filter);
62
- }
63
- };
64
- function matches(o, f) {
65
- if (f.runIds && !f.runIds.includes(o.runId)) return false;
66
- if (f.since !== void 0 && o.capturedAt < f.since) return false;
67
- if (f.until !== void 0 && o.capturedAt > f.until) return false;
68
- if (f.source && o.source !== f.source) return false;
69
- if (f.label && o.labels?.[f.label.key] !== f.label.value) return false;
70
- return true;
71
- }
72
- //#endregion
73
- export { InMemoryOutcomeStore as n, FileSystemOutcomeStore as t };
74
-
75
- //# sourceMappingURL=outcome-store-ChBKlTd_.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"outcome-store-ChBKlTd_.js","names":[],"sources":["../src/meta-eval/outcome-store.ts"],"sourcesContent":["/**\n * OutcomeStore — deployment outcomes attached to Run IDs.\n *\n * Outcomes arrive asynchronously from production telemetry after the\n * eval run completed: user ratings, retention flags, conversion events,\n * revenue, support-ticket rate, anything a product team can measure.\n * The store is a peer to TraceStore — separate lifecycle, same runId\n * foreign key.\n *\n * The whole point of this module is to make the meta-eval correlation\n * question computable: `correlate(evalMetric, outcomeMetric) → r, ρ, n, CI`.\n */\n\nexport interface DeploymentOutcome {\n runId: string\n capturedAt: number\n /** Numeric outcomes keyed by name — retention_7d, csat, revenue_usd, etc. */\n metrics: Record<string, number>\n /** Dimensions for stratified analysis — cohort, region, user_segment. */\n labels?: Record<string, string>\n /** Free-form provenance (source system, pipeline version). */\n source?: string\n}\n\nexport interface OutcomeFilter {\n runIds?: string[]\n since?: number\n until?: number\n label?: { key: string; value: string }\n source?: string\n}\n\nexport interface OutcomeStore {\n append(outcome: DeploymentOutcome): Promise<void>\n /** All outcomes attached to this run (a single run can have many — multiple\n * capture windows over deployment time). */\n forRun(runId: string): Promise<DeploymentOutcome[]>\n list(filter?: OutcomeFilter): Promise<DeploymentOutcome[]>\n}\n\nexport class InMemoryOutcomeStore implements OutcomeStore {\n private items: DeploymentOutcome[] = []\n\n async append(outcome: DeploymentOutcome): Promise<void> {\n this.items.push({ ...outcome })\n }\n\n async forRun(runId: string): Promise<DeploymentOutcome[]> {\n return this.items.filter((o) => o.runId === runId).map((o) => ({ ...o }))\n }\n\n async list(filter: OutcomeFilter = {}): Promise<DeploymentOutcome[]> {\n return this.items.filter((o) => matches(o, filter)).map((o) => ({ ...o }))\n }\n}\n\nexport interface FileSystemOutcomeStoreOptions {\n dir: string\n maxBytes?: number\n}\n\nexport class FileSystemOutcomeStore implements OutcomeStore {\n private dir: string\n private maxBytes: number\n private memo?: InMemoryOutcomeStore\n private loaded = false\n\n constructor(options: FileSystemOutcomeStoreOptions) {\n this.dir = options.dir\n this.maxBytes = options.maxBytes ?? 32 * 1024 * 1024\n }\n\n private async ensureDir(): Promise<void> {\n const fs = await import('node:fs/promises')\n await fs.mkdir(this.dir, { recursive: true })\n }\n\n async append(outcome: DeploymentOutcome): Promise<void> {\n await this.ensureDir()\n const fs = await import('node:fs/promises')\n const path = await import('node:path')\n const active = path.join(this.dir, 'outcomes.ndjson')\n try {\n const stat = await fs.stat(active)\n if (stat.size >= this.maxBytes) {\n await fs.rename(active, path.join(this.dir, `outcomes.${Date.now()}.ndjson`))\n }\n } catch {\n /* first write */\n }\n await fs.appendFile(active, `${JSON.stringify(outcome)}\\n`, 'utf8')\n if (this.memo) await this.memo.append(outcome)\n }\n\n private async load(): Promise<InMemoryOutcomeStore> {\n if (this.loaded && this.memo) return this.memo\n const fs = await import('node:fs/promises')\n const path = await import('node:path')\n const memo = new InMemoryOutcomeStore()\n try {\n const entries = await fs.readdir(this.dir)\n for (const file of entries) {\n if (!file.endsWith('.ndjson')) continue\n const content = await fs.readFile(path.join(this.dir, file), 'utf8')\n for (const line of content.split('\\n')) {\n if (!line.trim()) continue\n await memo.append(JSON.parse(line))\n }\n }\n } catch {\n /* empty */\n }\n this.memo = memo\n this.loaded = true\n return memo\n }\n\n async forRun(runId: string): Promise<DeploymentOutcome[]> {\n return (await this.load()).forRun(runId)\n }\n\n async list(filter?: OutcomeFilter): Promise<DeploymentOutcome[]> {\n return (await this.load()).list(filter)\n }\n}\n\nfunction matches(o: DeploymentOutcome, f: OutcomeFilter): boolean {\n if (f.runIds && !f.runIds.includes(o.runId)) return false\n if (f.since !== undefined && o.capturedAt < f.since) return false\n if (f.until !== undefined && o.capturedAt > f.until) return false\n if (f.source && o.source !== f.source) return false\n if (f.label && o.labels?.[f.label.key] !== f.label.value) return false\n return true\n}\n"],"mappings":";AAwCA,IAAa,uBAAb,MAA0D;CACxD,QAAqC,CAAC;CAEtC,MAAM,OAAO,SAA2C;EACtD,KAAK,MAAM,KAAK,EAAE,GAAG,QAAQ,CAAC;CAChC;CAEA,MAAM,OAAO,OAA6C;EACxD,OAAO,KAAK,MAAM,QAAQ,MAAM,EAAE,UAAU,KAAK,CAAC,CAAC,KAAK,OAAO,EAAE,GAAG,EAAE,EAAE;CAC1E;CAEA,MAAM,KAAK,SAAwB,CAAC,GAAiC;EACnE,OAAO,KAAK,MAAM,QAAQ,MAAM,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,OAAO,EAAE,GAAG,EAAE,EAAE;CAC3E;AACF;AAOA,IAAa,yBAAb,MAA4D;CAC1D;CACA;CACA;CACA,SAAiB;CAEjB,YAAY,SAAwC;EAClD,KAAK,MAAM,QAAQ;EACnB,KAAK,WAAW,QAAQ,YAAY,KAAK,OAAO;CAClD;CAEA,MAAc,YAA2B;EAEvC,OAAM,MADW,OAAO,oBAAA,CACf,MAAM,KAAK,KAAK,EAAE,WAAW,KAAK,CAAC;CAC9C;CAEA,MAAM,OAAO,SAA2C;EACtD,MAAM,KAAK,UAAU;EACrB,MAAM,KAAK,MAAM,OAAO;EACxB,MAAM,OAAO,MAAM,OAAO;EAC1B,MAAM,SAAS,KAAK,KAAK,KAAK,KAAK,iBAAiB;EACpD,IAAI;GAEF,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O,OAAO;CAC5D,IAAI,EAAE,UAAU,KAAA,KAAa,EAAE,aAAa,EAAE,OAAO,OAAO;CAC5D,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,OAAO;CAC9C,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,SAAS,EAAE,MAAM,OAAO,OAAO;CACjE,OAAO;AACT"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"paired-arms-D4aeIHUy.js","names":[],"sources":["../src/statistics/paired-binary.ts","../src/math/normal.ts","../src/statistics/rank-tests.ts","../src/paired-arms.ts"],"sourcesContent":["import { regularizedIncompleteBeta } from '../math/special-functions'\nimport { binomialSignTwoSided, zQuantile } from './internal'\n\n// ── Binomial proportion + paired-binary + coding-eval estimators ─────\n//\n// The paired family above (pairedBootstrap/pairedTTest/wilcoxonSignedRank)\n// operates on continuous scores. Pass/fail A/B comparisons — \"does treatment\n// X raise the success RATE vs control\" — are binary and paired, so they need\n// their own correct estimators: McNemar for significance (only the discordant\n// pairs carry signal), the paired risk difference for effect size, Wilson for\n// a single-arm proportion CI, and pass@k for the standard k-sample coding-eval\n// metric. The normal approximation is wrong for proportions near 0/1 and for\n// the small discordant counts typical of eval runs, so these are exact /\n// Wilson-based, not Wald.\n\n/** A binomial proportion estimate with a confidence interval. */\nexport interface ProportionInterval {\n /** Point estimate successes / n (0 when n = 0). */\n estimate: number\n /** Lower bound, clamped to [0, 1]. */\n lower: number\n /** Upper bound, clamped to [0, 1]. */\n upper: number\n}\n\n/**\n * Wilson score interval for a binomial proportion. Correct at small n and near\n * 0/1, where the normal (Wald) approximation produces bounds outside [0, 1] and\n * understates coverage. Use this for any pass-rate / hit-rate / realness-rate\n * CI — the continuous `confidenceInterval` assumes the wrong distribution for a\n * proportion. `n = 0 ⇒ {0, 0, 0}`.\n */\nexport function wilson(successes: number, n: number, confidence = 0.95): ProportionInterval {\n if (n <= 0) return { estimate: 0, lower: 0, upper: 0 }\n if (successes < 0 || successes > n) {\n throw new Error(`wilson: successes (${successes}) must be in [0, ${n}]`)\n }\n const z = zQuantile(1 - (1 - confidence) / 2)\n const p = successes / n\n const z2 = z * z\n const denom = 1 + z2 / n\n const center = (p + z2 / (2 * n)) / denom\n const half = (z * Math.sqrt((p * (1 - p) + z2 / (4 * n)) / n)) / denom\n return {\n estimate: p,\n lower: Math.max(0, center - half),\n upper: Math.min(1, center + half),\n }\n}\n\n/**\n * Are these per-item outcomes binary (every value exactly 0 or 1)?\n *\n * The discriminator a promotion gate needs before choosing a paired statistic.\n * On binary outcomes the paired delta vector lives in {-1, 0, +1} and is\n * normally dominated by zeros (both arms solve, or both arms miss, most items),\n * so its MEDIAN is pinned at exactly 0 no matter how large the real shift in\n * success rate is — and a bootstrap CI on that median collapses to [0, 0].\n * A gate keying on `ci.low > threshold` is then structurally unable to see\n * either a gain or a regression. Detect this shape and switch to the\n * paired-binary estimators ({@link mcnemar}, {@link pairedRiskDifference})\n * instead of silently answering \"no\" forever.\n *\n * Empty input is NOT binary: there is no evidence of the outcome's shape, and\n * defaulting an empty vector into the binary branch would pick a statistic on\n * no data at all.\n *\n * NOT the right discriminator for a gate. It recognises the literal {0, 1}\n * encoding and nothing else, so a pass/fail dimension emitted on 0-100 — which\n * judges in this codebase do routinely — reads as non-binary, and a single\n * partial-credit score in an otherwise pass/fail vector flips it to false while\n * leaving the median just as blind. Gates want {@link pairedBinaryScale} (any\n * two-point encoding). This predicate remains for callers that specifically\n * mean \"literally 0/1\".\n */\nexport function isBinaryOutcomeVector(values: ArrayLike<number>): boolean {\n if (values.length === 0) return false\n for (let i = 0; i < values.length; i++) {\n const v = values[i]!\n if (v !== 0 && v !== 1) return false\n }\n return true\n}\n\n/** Result of a McNemar paired-binary significance test. */\nexport interface McNemarResult {\n /** Total paired observations. */\n n: number\n /** Discordant pairs (b + c) — the only ones that carry signal. */\n nDiscordant: number\n /** Pairs where treatment succeeded and control failed (\"newly correct\"). */\n b: number\n /** Pairs where control succeeded and treatment failed (\"newly wrong\"). */\n c: number\n /** Continuity-corrected chi-square statistic (reference; exact p drives the call). */\n statistic: number\n /** Two-sided p-value. Exact (binomial sign test on discordant pairs). */\n pValue: number\n}\n\n/**\n * McNemar's test for paired binary outcomes — the correct significance test for\n * \"does treatment change the success rate vs control on the SAME items\". Only\n * discordant pairs (one arm right, the other wrong) carry information; concordant\n * pairs are uninformative, so a paired t-test / two-proportion z-test on the raw\n * rates is wrong here. The p-value is exact: under H0 the b \"treatment-wins\" are\n * Binomial(b + c, 0.5), so the two-sided p is the doubled binomial tail — correct\n * at the small discordant counts typical of eval runs (no continuity-corrected\n * chi-square approximation needed, though it is returned as `statistic` for\n * reference). Inputs are paired 0/1 (or boolean) arrays, control first to match\n * the module's (before, after) convention. Throws on unequal lengths.\n */\nexport function mcnemar(\n control: ArrayLike<number | boolean>,\n treatment: ArrayLike<number | boolean>,\n): McNemarResult {\n if (control.length !== treatment.length) {\n throw new Error(`mcnemar: unequal sample sizes (${control.length} vs ${treatment.length})`)\n }\n const n = control.length\n let b = 0 // treatment 1, control 0\n let c = 0 // treatment 0, control 1\n for (let i = 0; i < n; i++) {\n const ctrl = control[i] ? 1 : 0\n const treat = treatment[i] ? 1 : 0\n if (treat === 1 && ctrl === 0) b++\n else if (treat === 0 && ctrl === 1) c++\n }\n const nDiscordant = b + c\n const statistic = nDiscordant === 0 ? 0 : (Math.abs(b - c) - 1) ** 2 / nDiscordant\n return { n, nDiscordant, b, c, statistic, pValue: binomialSignTwoSided(b, c) }\n}\n\n/** A paired binary effect size (treatment rate − control rate) with a CI. */\nexport interface RiskDifferenceResult {\n /** Total paired observations. */\n n: number\n /** Discordant pairs: treatment-win count. */\n b: number\n /** Discordant pairs: control-win count. */\n c: number\n /** Paired risk difference p(treatment) − p(control) = (b − c) / n. */\n riskDifference: number\n /** Lower bound of the CI, clamped to [-1, 1]. */\n lower: number\n /** Upper bound of the CI, clamped to [-1, 1]. */\n upper: number\n /** Confidence level used. */\n confidence: number\n}\n\n/**\n * Paired risk difference (the effect-size companion to {@link mcnemar}): the\n * change in success rate p(treatment) − p(control) on matched items, which for\n * paired binary data equals (b − c) / n. The CI uses the paired variance from\n * the discordant counts, not the independent-samples formula (which overstates\n * the interval by ignoring the pairing). Inputs are paired 0/1 (or boolean)\n * arrays, control first. Throws on unequal lengths.\n *\n * REPORTING ONLY — do NOT decide a promotion on this interval. The CI is a Wald\n * normal approximation, which badly UNDERCOVERS when only a handful of pairs are\n * discordant: at n = 3 with b = 2, c = 0 it returns [0.133, 1.000], excluding 0,\n * while McNemar's exact test on the same data gives p = 0.50. A gate keying on\n * `lower > 0` would promote noise. Use {@link pairedRiskDifferenceExact}, whose\n * interval is dual to the exact test by construction, for any decision.\n */\nexport function pairedRiskDifference(\n control: ArrayLike<number | boolean>,\n treatment: ArrayLike<number | boolean>,\n confidence = 0.95,\n): RiskDifferenceResult {\n if (control.length !== treatment.length) {\n throw new Error(\n `pairedRiskDifference: unequal sample sizes (${control.length} vs ${treatment.length})`,\n )\n }\n const n = control.length\n if (n === 0) return { n: 0, b: 0, c: 0, riskDifference: 0, lower: 0, upper: 0, confidence }\n let b = 0\n let c = 0\n for (let i = 0; i < n; i++) {\n const ctrl = control[i] ? 1 : 0\n const treat = treatment[i] ? 1 : 0\n if (treat === 1 && ctrl === 0) b++\n else if (treat === 0 && ctrl === 1) c++\n }\n const rd = (b - c) / n\n const variance = (b + c - (b - c) ** 2 / n) / (n * n)\n const z = zQuantile(1 - (1 - confidence) / 2)\n const half = z * Math.sqrt(Math.max(0, variance))\n return {\n n,\n b,\n c,\n riskDifference: rd,\n lower: Math.max(-1, rd - half),\n upper: Math.min(1, rd + half),\n confidence,\n }\n}\n\n/** A paired binary effect size with an EXACT interval and the exact test that\n * bounds it — one object so a caller cannot read the estimate without the\n * significance it is entitled to. */\nexport interface ExactRiskDifferenceResult {\n /** Total paired observations. */\n n: number\n /** Discordant pairs: treatment-win count. */\n b: number\n /** Discordant pairs: control-win count. */\n c: number\n /** Discordant pairs (b + c) — the only ones carrying information. */\n nDiscordant: number\n /** Paired risk difference p(treatment) − p(control) = (b − c) / n. */\n riskDifference: number\n /** Exact conditional CI lower bound. 0 when there are no discordant pairs. */\n lower: number\n /** Exact conditional CI upper bound. 0 when there are no discordant pairs. */\n upper: number\n /** Confidence level used. */\n confidence: number\n /** McNemar's exact two-sided p-value on the same discordant counts. */\n pValue: number\n}\n\n/**\n * Paired risk difference with the EXACT CONDITIONAL interval — the estimator a\n * promotion gate may decide on.\n *\n * Conditional on the number of discordant pairs m = b + c, the treatment-win\n * count b is Binomial(m, π) with π = P(treatment wins | discordant), and the\n * risk difference is an exact reparameterisation: RD = (2π − 1)·m/n. So a\n * Clopper-Pearson exact interval for π maps straight onto RD. This buys the\n * property the Wald interval in {@link pairedRiskDifference} does not have:\n *\n * **`lower > 0` ⟺ McNemar's exact test rejects at α = 1 − confidence.**\n *\n * Clopper-Pearson excludes π = 0.5 exactly when the two-sided exact binomial\n * test of π = 0.5 rejects, and that test IS {@link mcnemar}'s p-value — so the\n * interval and the test can never disagree, and a gate keyed on `lower` cannot\n * promote what the exact test refuses. The exact p is returned in the same\n * object so the two are impossible to compute apart.\n *\n * The interval is conservative (exact intervals over-cover; conditioning on m\n * discards the concordant pairs' information about m itself). That is the\n * correct direction for a promotion gate: it refuses more often, never less.\n *\n * With m = 0 there are no discordant pairs and π is not identified: the result\n * is the degenerate [0, 0] with p = 1. That is NOT evidence of equivalence —\n * callers must treat a zero-width interval as \"cannot decide\", not as \"no\n * difference\". Inputs are paired 0/1 (or boolean) arrays, control first.\n * Throws on unequal lengths.\n */\nexport function pairedRiskDifferenceExact(\n control: ArrayLike<number | boolean>,\n treatment: ArrayLike<number | boolean>,\n confidence = 0.95,\n): ExactRiskDifferenceResult {\n if (control.length !== treatment.length) {\n throw new Error(\n `pairedRiskDifferenceExact: unequal sample sizes (${control.length} vs ${treatment.length})`,\n )\n }\n if (confidence <= 0 || confidence >= 1) {\n throw new Error(`pairedRiskDifferenceExact: confidence must be in (0,1), got ${confidence}`)\n }\n const n = control.length\n if (n === 0) {\n return {\n n: 0,\n b: 0,\n c: 0,\n nDiscordant: 0,\n riskDifference: 0,\n lower: 0,\n upper: 0,\n confidence,\n pValue: 1,\n }\n }\n let b = 0\n let c = 0\n for (let i = 0; i < n; i++) {\n const ctrl = control[i] ? 1 : 0\n const treat = treatment[i] ? 1 : 0\n if (treat === 1 && ctrl === 0) b++\n else if (treat === 0 && ctrl === 1) c++\n }\n const m = b + c\n const riskDifference = (b - c) / n\n const pValue = binomialSignTwoSided(b, c)\n if (m === 0) {\n return { n, b, c, nDiscordant: 0, riskDifference: 0, lower: 0, upper: 0, confidence, pValue }\n }\n const alpha = 1 - confidence\n const piLow = b === 0 ? 0 : betaQuantile(alpha / 2, b, m - b + 1)\n const piHigh = b === m ? 1 : betaQuantile(1 - alpha / 2, b + 1, m - b)\n const scale = m / n\n return {\n n,\n b,\n c,\n nDiscordant: m,\n riskDifference,\n lower: Math.max(-1, (2 * piLow - 1) * scale),\n upper: Math.min(1, (2 * piHigh - 1) * scale),\n confidence,\n pValue,\n }\n}\n\n/** Inverse regularized incomplete beta by bisection on\n * {@link regularizedIncompleteBeta}, which is monotone increasing in x. 80\n * halvings of [0,1] resolve to ~8e-25, far past the continued fraction's own\n * 3e-15 tolerance, so the quantile is as exact as the CDF it inverts. */\nfunction betaQuantile(p: number, a: number, b: number): number {\n if (p <= 0) return 0\n if (p >= 1) return 1\n let lo = 0\n let hi = 1\n for (let i = 0; i < 80; i++) {\n const mid = (lo + hi) / 2\n if (regularizedIncompleteBeta(mid, a, b) < p) lo = mid\n else hi = mid\n }\n return (lo + hi) / 2\n}\n\n/** A paired binary effect size with an interval that is valid at a NONZERO\n * margin — the estimator a noninferiority decision may be made on. */\nexport interface ScoreRiskDifferenceResult {\n /** Total paired observations. */\n n: number\n /** Discordant pairs: treatment-win count. */\n b: number\n /** Discordant pairs: control-win count. */\n c: number\n /** Discordant pairs (b + c). */\n nDiscordant: number\n /** Paired risk difference p(treatment) − p(control) = (b − c) / n. */\n riskDifference: number\n /** Score-interval lower bound on the population risk difference. */\n lower: number\n /** Score-interval upper bound on the population risk difference. */\n upper: number\n /** Confidence level used. */\n confidence: number\n}\n\n/**\n * Constrained MLE of q = P(treatment loses) under the hypothesis RD = `delta`.\n *\n * Profiling the two concordant cells out of the multinomial leaves\n * `L(q) = b·log(q+delta) + c·log(q) + e·log(1 − 2q − delta)` with `e = n − b − c`,\n * whose stationary point is the positive root of\n * `2n·q² − [(b + c) − delta·(b + 3c + 2e)]·q − c·delta·(1 − delta) = 0`.\n * At `delta = 0` this returns `(b + c) / 2n`, the familiar null.\n */\nfunction constrainedLossRate(b: number, c: number, n: number, delta: number): number {\n const e = n - b - c\n const quadratic = 2 * n\n const linear = -(b + c - delta * (b + 3 * c + 2 * e))\n const constant = -c * delta * (1 - delta)\n const discriminant = linear * linear - 4 * quadratic * constant\n const root = discriminant > 0 ? Math.sqrt(discriminant) : 0\n const q = (-linear + root) / (2 * quadratic)\n // Clamp into the region where all four cell probabilities stay non-negative.\n return Math.min(Math.max(q, Math.max(0, -delta)), Math.max(0, (1 - delta) / 2))\n}\n\n/** Tango's score statistic for H0: RD = `delta`. `Var(b − c) = n·(2q + delta −\n * delta²)` under that hypothesis, evaluated at the constrained MLE of q. */\nfunction tangoScore(b: number, c: number, n: number, delta: number): number {\n const numerator = b - c - n * delta\n const q = constrainedLossRate(b, c, n, delta)\n const variance = n * (2 * q + delta * (1 - delta))\n if (!(variance > 0)) {\n if (numerator === 0) return 0\n return numerator > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY\n }\n return numerator / Math.sqrt(variance)\n}\n\n/**\n * Paired risk difference with TANGO'S (1998) SCORE INTERVAL — the estimator a\n * promotion gate may decide on **at a nonzero margin**.\n *\n * {@link pairedRiskDifferenceExact} conditions on the observed discordant count\n * `m = b + c`, builds a Clopper-Pearson interval for the win share among those\n * `m` pairs, and multiplies by the observed `m/n`. That is exact for testing\n * RD = 0 — it is dual to McNemar — but it is NOT a confidence interval for the\n * population risk difference at a nonzero margin, because the sampling\n * variability of `m/n` itself is discarded. The gap is not academic: with the\n * production caller's `pairedDeltaThreshold: -0.05`, a process whose true risk\n * difference sits exactly on that margin clears a nominal-95 % `lower > margin`\n * check 24.75 % of the time at n = 40 and 43.95 % at n = 76 (2000 replicates\n * each) when the conditional interval decides.\n *\n * Tango's interval inverts the score test of RD = delta, which estimates the\n * nuisance loss rate under each hypothesised delta instead of fixing it at the\n * observed value, so `m` contributes its own uncertainty. It is the method\n * `ratesci::scorepairci` uses for paired risk-difference noninferiority, and it\n * is not conditional, so it stays valid as the margin moves away from zero.\n *\n * The bounds are found by bisecting `tangoScore(delta) = ±z` — the score is\n * monotone decreasing in delta, so each crossing is unique. Inputs are paired\n * 0/1 (or boolean) arrays, control first. Throws on unequal lengths.\n */\nexport function pairedRiskDifferenceScore(\n control: ArrayLike<number | boolean>,\n treatment: ArrayLike<number | boolean>,\n confidence = 0.95,\n): ScoreRiskDifferenceResult {\n if (control.length !== treatment.length) {\n throw new Error(\n `pairedRiskDifferenceScore: unequal sample sizes (${control.length} vs ${treatment.length})`,\n )\n }\n if (confidence <= 0 || confidence >= 1) {\n throw new Error(`pairedRiskDifferenceScore: confidence must be in (0,1), got ${confidence}`)\n }\n const n = control.length\n if (n === 0) {\n return { n: 0, b: 0, c: 0, nDiscordant: 0, riskDifference: 0, lower: -1, upper: 1, confidence }\n }\n let b = 0\n let c = 0\n for (let i = 0; i < n; i++) {\n const ctrl = control[i] ? 1 : 0\n const treat = treatment[i] ? 1 : 0\n if (treat === 1 && ctrl === 0) b++\n else if (treat === 0 && ctrl === 1) c++\n }\n const riskDifference = (b - c) / n\n const z = zQuantile(1 - (1 - confidence) / 2)\n\n // Lower bound: the smallest delta still inside the interval, i.e. the root of\n // score(delta) = +z on [-1, riskDifference]. score(riskDifference) = 0 < z, so\n // the right endpoint is always inside and the bisection is well posed.\n let lo = -1\n let hi = riskDifference\n for (let i = 0; i < 200; i++) {\n const mid = (lo + hi) / 2\n if (tangoScore(b, c, n, mid) > z) lo = mid\n else hi = mid\n }\n const lower = (lo + hi) / 2\n\n // Upper bound: root of score(delta) = -z on [riskDifference, 1].\n let ulo = riskDifference\n let uhi = 1\n for (let i = 0; i < 200; i++) {\n const mid = (ulo + uhi) / 2\n if (tangoScore(b, c, n, mid) > -z) ulo = mid\n else uhi = mid\n }\n const upper = (ulo + uhi) / 2\n\n return {\n n,\n b,\n c,\n nDiscordant: b + c,\n riskDifference,\n lower: Math.max(-1, lower),\n upper: Math.min(1, upper),\n confidence,\n }\n}\n\n/**\n * The common positive level `s` such that EVERY value across both paired arms is\n * exactly 0 or `s` — i.e. the outcome is pass/fail, whatever encoding it arrived\n * in. Returns null when the outcomes are not two-point, when the two arms use\n * different levels, or when no positive value was observed at all (all-zero\n * arms: the level is not identified, and there is nothing to decide anyway).\n *\n * This is the scale-aware successor to {@link isBinaryOutcomeVector}, which only\n * recognises literal {0, 1}. Judges in this codebase emit dimensions on 0-100 as\n * well as [0,1] (see `detectScale` in `campaign/gates/statistical-heldout.ts`),\n * so a pass/fail dimension routinely arrives as {0, 100} and a {0,1}-only test\n * silently sends it down the median path that cannot see it. Any positive level\n * is accepted, not just 1 and 100: for a two-point {0, s} outcome the mean paired\n * delta is exactly s·(b − c)/n, so the binary estimators apply after dividing by\n * s and rescaling the result back into the caller's native units.\n *\n * Non-finite values ⇒ null: an unusable outcome must not be classified as a\n * clean pass/fail shape.\n */\nexport function pairedBinaryScale(\n before: ArrayLike<number>,\n after: ArrayLike<number>,\n): number | null {\n let level: number | null = null\n for (const arm of [before, after]) {\n for (let i = 0; i < arm.length; i++) {\n const v = arm[i]!\n if (!Number.isFinite(v)) return null\n if (v === 0) continue\n if (v < 0) return null\n if (level === null) level = v\n else if (v !== level) return null\n }\n }\n return level\n}\n\n/**\n * Unbiased pass@k for code generation (Chen et al. 2021, \"Evaluating Large\n * Language Models Trained on Code\"). Given `n` independent samples for one\n * problem of which `c` pass, the probability that at least one of a random k of\n * them passes is 1 − C(n−c, k) / C(n, k). Estimating pass@k as \"did any of the\n * first k pass\" is biased high at small n; this is the variance-reduced estimator\n * averaged implicitly over all k-subsets. Average the per-problem values across\n * the suite for the corpus pass@k. Computed in the numerically stable product\n * form. Requires 1 ≤ k ≤ n and 0 ≤ c ≤ n.\n */\nexport function passAtK(n: number, c: number, k: number): number {\n if (!Number.isInteger(n) || !Number.isInteger(c) || !Number.isInteger(k)) {\n throw new Error(`passAtK: n, c, k must be integers (got n=${n}, c=${c}, k=${k})`)\n }\n if (k < 1 || k > n || c < 0 || c > n) {\n throw new Error(`passAtK: require 1 ≤ k ≤ n and 0 ≤ c ≤ n (got n=${n}, c=${c}, k=${k})`)\n }\n if (n - c < k) return 1\n let prob = 1\n for (let i = n - c + 1; i <= n; i++) prob *= 1 - k / i\n return 1 - prob\n}\n","/**\n * Standard normal cumulative distribution using Abramowitz and Stegun 7.1.26.\n *\n * The approximation is evaluated as erf(x / sqrt(2)). Computing the negative\n * tail from the complementary term avoids cancellation when x is far below 0.\n * The maximum absolute CDF error is approximately 7.5e-8.\n */\nexport function normalCdf(x: number): number {\n if (x === 0) return 0.5\n\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\n const scaled = Math.abs(x) / Math.SQRT2\n const t = 1 / (1 + p * scaled)\n const complement = ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.exp(-scaled * scaled)\n\n return x < 0 ? complement / 2 : 1 - complement / 2\n}\n","import { ValidationError } from '../errors'\nimport { normalCdf } from '../math/normal'\nimport { lnGamma } from '../math/special-functions'\nimport { assertFiniteSample, makeRng, symmetricTwoSampleSeed } from './internal'\n\n// ── Rank tests: exact by default ─────────────────────────────────────\n//\n// At 3–10 repetitions per arm the binding constraint on a rank test is\n// combinatorial, not numerical. Three versus three admits only 20 splits, so\n// the attainable two-sided p-grid starts at 0.1000 and α = 0.05 is out of\n// reach at that design; a normal approximation reports 0.0495, a p-value that\n// describes no attainable outcome. No better approximation fixes this — adding\n// the tie correction moves `[0,0,0]` versus `[1,1,1]` from 0.0495 to 0.0469,\n// further from its exact 0.1000, not closer.\n//\n// So both null distributions are enumerated exactly inside bounded state and\n// work budgets, by convolution over the observed midranks (identical to\n// enumerating every split / sign pattern, and cheaper), which conditions on the\n// realised tie pattern for free. Above those budgets the default is a seeded\n// Monte Carlo permutation. The asymptotic path is never chosen automatically,\n// and asking for it inside the exact-feasible range throws.\n//\n// Every result carries `method` and `pFloor` so a downstream gate can SEE the\n// discreteness rather than infer it: a gate handed data whose `pFloor` exceeds\n// its alpha is underpowered by construction, which is a true statement about\n// the experiment, not a false one about the effect.\n\n/** How a rank test's p-value was actually computed. */\nexport type RankTestMethod = 'exact' | 'permutation' | 'asymptotic'\n\n/**\n * What the caller asks for. `'auto'` selects `'exact'` inside the enumeration\n * threshold and `'permutation'` above it, and never selects `'asymptotic'`.\n */\nexport type RankTestMethodRequest = 'auto' | 'exact' | 'asymptotic'\n\nexport interface RankTestOptions {\n /** Default `'auto'`. `'asymptotic'` inside the exact-feasible range throws. */\n method?: RankTestMethodRequest\n /** Resamples on the Monte Carlo permutation path. Default 100000. */\n permutations?: number\n /** Seed for the permutation path. Omitted ⇒ derived from the data itself, so\n * the result is reproducible either way. */\n seed?: number\n}\n\n/** Maximum dynamic-programming cells used by an exact two-sample rank test. */\nexport const MANN_WHITNEY_EXACT_MAX_STATES = 8_192\n/** Maximum inner-loop transitions used by an exact two-sample rank test. */\nexport const MANN_WHITNEY_EXACT_MAX_WORK = 250_000\n/** Non-zero differences up to which the signed-rank null is enumerated exactly. */\nexport const WILCOXON_EXACT_MAX_N = 20\n/** Resamples used when a rank test falls back to Monte Carlo permutation. */\nexport const DEFAULT_PERMUTATIONS = 100_000\n\nexport interface MannWhitneyResult {\n /** `min(U_a, U_b)` — the conventional reported statistic. */\n u: number\n /** U for sample `a`. Carries the direction of the effect, which `u` discards. */\n uA: number\n /** Two-sided p-value. */\n p: number\n /** How `p` was computed. */\n method: RankTestMethod\n /** Smallest two-sided p this design can produce. `p` can never be below it. */\n pFloor: number\n}\n\n/**\n * Mann-Whitney U — two independent samples, no distributional assumption.\n *\n * Exact conditional (permutation) p by default when the dynamic program fits\n * {@link MANN_WHITNEY_EXACT_MAX_STATES} cells and\n * {@link MANN_WHITNEY_EXACT_MAX_WORK} transitions, seeded Monte Carlo\n * permutation above those limits. This keeps imbalanced designs such as 1+24\n * exact without admitting expensive balanced designs merely because they have\n * the same total size. Throws on non-finite input and on `method:\n * 'asymptotic'` where an exact answer is available. Empty input yields `p = 1,\n * pFloor = 1` — no design, no attainable evidence.\n */\nexport function mannWhitneyU(\n a: number[],\n b: number[],\n opts: RankTestOptions = {},\n): MannWhitneyResult {\n assertFiniteSample('mannWhitneyU', 'a', a)\n assertFiniteSample('mannWhitneyU', 'b', b)\n\n const n1 = a.length\n const n2 = b.length\n if (n1 === 0 || n2 === 0) return { u: 0, uA: 0, p: 1, method: 'exact', pFloor: 1 }\n\n const total = n1 + n2\n const combined = [\n ...a.map((v) => ({ v, fromA: true })),\n ...b.map((v) => ({ v, fromA: false })),\n ].sort((x, y) => x.v - y.v)\n\n const { midranks, tieTerm } = midranksWithTieTerm(combined.map((entry) => entry.v))\n let rankSumA = 0\n for (let k = 0; k < total; k++) {\n if (combined[k]!.fromA) rankSumA += midranks[k]!\n }\n\n const uA = rankSumA - (n1 * (n1 + 1)) / 2\n const u = Math.min(uA, n1 * n2 - uA)\n // Midranks are integers or halves, so doubling makes the null convolution\n // integral. U is centred on n₁n₂/2, hence a doubled centre of n₁n₂.\n const doubled = midranks.map((rank) => Math.round(rank * 2))\n const doubledDeviation = Math.abs(2 * uA - n1 * n2)\n const selectedN = Math.min(n1, n2)\n const otherN = total - selectedN\n const exactCost = exactTwoSampleCost(doubled, selectedN)\n\n const designFloor = exactTwoSampleFloor(doubled, selectedN)\n const method = selectRankTestMethod(\n 'mannWhitneyU',\n opts.method ?? 'auto',\n `n1=${n1}, n2=${n2}`,\n exactCost.states <= MANN_WHITNEY_EXACT_MAX_STATES &&\n exactCost.work <= MANN_WHITNEY_EXACT_MAX_WORK,\n designFloor,\n `${MANN_WHITNEY_EXACT_MAX_STATES.toLocaleString('en-US')} states and ` +\n `${MANN_WHITNEY_EXACT_MAX_WORK.toLocaleString('en-US')} transitions`,\n )\n\n if (method === 'exact') {\n const { p, pFloor } = exactTwoSampleP(doubled, selectedN, otherN, doubledDeviation)\n return { u, uA, p, method, pFloor }\n }\n\n if (method === 'asymptotic') {\n return {\n u,\n uA,\n p: asymptoticTwoSidedP(doubledDeviation / 2, twoSampleSigma(n1, n2, total, tieTerm)),\n method,\n pFloor: designFloor,\n }\n }\n\n const permutations = resolvePermutations('mannWhitneyU', opts.permutations)\n const rng = opts.seed === undefined ? makeRng(symmetricTwoSampleSeed(a, b)) : makeRng(opts.seed)\n let atLeastAsExtreme = 0\n const pool = [...doubled]\n for (let iteration = 0; iteration < permutations; iteration++) {\n let doubledRankSum = 0\n for (let k = 0; k < selectedN; k++) {\n const pick = k + Math.floor(rng() * (total - k))\n const swapped = pool[pick]!\n pool[pick] = pool[k]!\n pool[k] = swapped\n doubledRankSum += swapped\n }\n if (\n Math.abs(doubledRankSum - selectedN * (selectedN + 1) - selectedN * otherN) >=\n doubledDeviation\n ) {\n atLeastAsExtreme++\n }\n }\n const pFloor = Math.max(1 / (permutations + 1), designFloor)\n return {\n u,\n uA,\n p: Math.max((1 + atLeastAsExtreme) / (permutations + 1), pFloor),\n method,\n pFloor,\n }\n}\n\nexport interface WilcoxonSignedRankResult {\n /** W⁺, the rank sum of the positive differences. (scipy reports\n * `min(W⁺, W⁻)`; compare statistics only after converting.) */\n w: number\n /** Two-sided p-value. */\n p: number\n /** How `p` was computed. */\n method: RankTestMethod\n /** Smallest two-sided p this design can produce. */\n pFloor: number\n /** Non-zero differences — zero differences are dropped and carry no rank. */\n nNonZero: number\n}\n\n/**\n * Wilcoxon signed-rank — paired, no distributional assumption on the deltas.\n *\n * Exact conditional (sign-flip) p by default at `n ≤\n * {@link WILCOXON_EXACT_MAX_N}` non-zero differences, seeded Monte Carlo\n * permutation above it. Throws on non-finite input and on `method:\n * 'asymptotic'` where an exact answer is available.\n *\n * `n` is the count of NON-ZERO differences: exact ties are dropped before\n * ranking, so a run of tied pairs shrinks the design and raises `pFloor`.\n * All-tied input yields `p = 1, pFloor = 1` — no attainable evidence, which\n * `pFloor` states rather than leaving `p = 1` to be read as a measured null.\n */\nexport function wilcoxonSignedRank(\n before: number[],\n after: number[],\n opts: RankTestOptions = {},\n): WilcoxonSignedRankResult {\n if (before.length !== after.length) {\n throw new ValidationError(\n `wilcoxonSignedRank: unequal sample sizes (${before.length} vs ${after.length})`,\n )\n }\n assertFiniteSample('wilcoxonSignedRank', 'before', before)\n assertFiniteSample('wilcoxonSignedRank', 'after', after)\n\n const diffs = before.map((b, i) => after[i]! - b).filter((d) => d !== 0)\n const n = diffs.length\n if (n === 0) return { w: 0, p: 1, method: 'exact', pFloor: 1, nNonZero: 0 }\n\n const order = diffs.map((d, i) => ({ abs: Math.abs(d), i })).sort((x, y) => x.abs - y.abs)\n const { midranks, tieTerm } = midranksWithTieTerm(order.map((entry) => entry.abs))\n const ranks: number[] = new Array(n)\n for (let k = 0; k < n; k++) ranks[order[k]!.i] = midranks[k]!\n\n let wPlus = 0\n for (let k = 0; k < n; k++) if (diffs[k]! > 0) wPlus += ranks[k]!\n\n // Midranks are integers or halves; doubling makes the convolution integral.\n // Σ midranks = n(n+1)/2 whatever the tie pattern, so E[W⁺] = n(n+1)/4 and\n // the doubled centre is n(n+1)/2.\n const doubled = midranks.map((rank) => Math.round(rank * 2))\n const doubledDeviation = Math.abs(2 * wPlus - (n * (n + 1)) / 2)\n\n const designFloor = Math.min(1, 2 ** (1 - n))\n const method = selectRankTestMethod(\n 'wilcoxonSignedRank',\n opts.method ?? 'auto',\n `n=${n} non-zero differences`,\n n <= WILCOXON_EXACT_MAX_N,\n designFloor,\n `${WILCOXON_EXACT_MAX_N} non-zero differences`,\n )\n\n if (method === 'exact') {\n const { p, pFloor } = exactSignedRankP(doubled, doubledDeviation)\n return { w: wPlus, p, method, pFloor, nNonZero: n }\n }\n\n if (method === 'asymptotic') {\n const variance = (n * (n + 1) * (2 * n + 1)) / 24 - tieTerm / 48\n return {\n w: wPlus,\n p: asymptoticTwoSidedP(doubledDeviation / 2, Math.sqrt(variance)),\n method,\n pFloor: designFloor,\n nNonZero: n,\n }\n }\n\n const permutations = resolvePermutations('wilcoxonSignedRank', opts.permutations)\n const rng = makeRng(opts.seed, before, after)\n const doubledCentre = (n * (n + 1)) / 2\n let atLeastAsExtreme = 0\n for (let iteration = 0; iteration < permutations; iteration++) {\n let doubledWPlus = 0\n for (let k = 0; k < n; k++) if (rng() < 0.5) doubledWPlus += doubled[k]!\n if (Math.abs(doubledWPlus - doubledCentre) >= doubledDeviation) atLeastAsExtreme++\n }\n return {\n w: wPlus,\n p: (1 + atLeastAsExtreme) / (permutations + 1),\n method,\n pFloor: Math.max(1 / (permutations + 1), designFloor),\n nNonZero: n,\n }\n}\n\n/**\n * Average ranks over an ASCENDING-sorted array, plus `Σ(t³ − t)` over tie\n * groups of size `t` — the correction term both asymptotic rank-test variances\n * need.\n */\nfunction midranksWithTieTerm(sorted: readonly number[]): { midranks: number[]; tieTerm: number } {\n const midranks = new Array<number>(sorted.length)\n let tieTerm = 0\n let i = 0\n while (i < sorted.length) {\n let j = i\n while (j < sorted.length && sorted[j] === sorted[i]) j++\n const average = (i + 1 + j) / 2\n for (let k = i; k < j; k++) midranks[k] = average\n const groupSize = j - i\n if (groupSize > 1) tieTerm += groupSize ** 3 - groupSize\n i = j\n }\n return { midranks, tieTerm }\n}\n\nfunction selectRankTestMethod(\n fn: string,\n request: RankTestMethodRequest,\n design: string,\n exactFeasible: boolean,\n designFloor: number,\n threshold: string,\n): RankTestMethod {\n if (request === 'auto') return exactFeasible ? 'exact' : 'permutation'\n if (request === 'exact') {\n if (exactFeasible) return 'exact'\n throw new ValidationError(\n `${fn}: method 'exact' is out of range at ${design} — enumeration is bounded by ` +\n `${threshold}. Use 'auto' for the seeded Monte Carlo permutation, which converges ` +\n 'to the same answer.',\n )\n }\n if (exactFeasible) {\n throw new ValidationError(\n `${fn}: method 'asymptotic' is refused at ${design} — the exact p-grid at this design ` +\n `starts at ${formatProbability(designFloor)}, so an asymptotic p below it describes no ` +\n `attainable outcome. Use method 'exact' (the default) or add repetitions past ` +\n `${threshold}.`,\n )\n }\n return 'asymptotic'\n}\n\nfunction resolvePermutations(fn: string, permutations: number | undefined): number {\n if (permutations === undefined) return DEFAULT_PERMUTATIONS\n if (!Number.isInteger(permutations) || permutations < 1) {\n throw new ValidationError(`${fn}: permutations must be a positive integer, got ${permutations}`)\n }\n return permutations\n}\n\n/** Two-sided normal-approximation tail with the continuity correction. */\nfunction asymptoticTwoSidedP(deviation: number, sigma: number): number {\n if (!(sigma > 0)) return 1\n return Math.min(1, 2 * (1 - normalCdf(Math.max(0, deviation - 0.5) / sigma)))\n}\n\n/** SD of U under the permutation null, corrected for the realised ties. The\n * tie term reduces (N+1) and reaches it exactly when every value is tied, so\n * the variance floors at 0 rather than going negative. */\nfunction twoSampleSigma(n1: number, n2: number, total: number, tieTerm: number): number {\n if (total < 2) return 0\n const variance = ((n1 * n2) / 12) * (total + 1 - tieTerm / (total * (total - 1)))\n return Math.sqrt(Math.max(0, variance))\n}\n\nfunction logChoose(n: number, k: number): number {\n return lnGamma(n + 1) - lnGamma(k + 1) - lnGamma(n - k + 1)\n}\n\nfunction formatProbability(value: number): string {\n return value >= 1e-4 || value === 0 ? value.toFixed(4) : value.toExponential(3)\n}\n\n/**\n * Exact DP allocation and loop count for this observed rank vector.\n *\n * The smaller arm is sufficient because selecting its complement produces the\n * same two-sided U deviation while using fewer rows in the state table.\n */\nfunction exactTwoSampleCost(\n doubledRanks: readonly number[],\n selectedN: number,\n): { states: number; work: number } {\n const maxSum = doubledRanks.reduce((sum, rank) => sum + rank, 0)\n let work = 0\n for (let placed = 0; placed < doubledRanks.length; placed++) {\n work += Math.min(selectedN, placed + 1) * (maxSum - doubledRanks[placed]! + 1)\n }\n return {\n states: (selectedN + 1) * (maxSum + 1),\n work,\n }\n}\n\n/**\n * Smallest attainable two-sided p under the observed ties.\n *\n * Only subsets with the minimum or maximum rank sum can attain the largest\n * deviation. Their multiplicity is the number of ways to choose within the\n * tie group at each boundary, so this calculation is exact without allocating\n * the full null distribution.\n */\nfunction exactTwoSampleFloor(doubledRanks: readonly number[], selectedN: number): number {\n const total = doubledRanks.length\n const otherN = total - selectedN\n const minimumSum = doubledRanks.slice(0, selectedN).reduce((sum, rank) => sum + rank, 0)\n const maximumSum = doubledRanks.slice(total - selectedN).reduce((sum, rank) => sum + rank, 0)\n if (minimumSum === maximumSum) return 1\n\n const centre = selectedN * (selectedN + 1) + selectedN * otherN\n const minimumDeviation = Math.abs(minimumSum - centre)\n const maximumDeviation = Math.abs(maximumSum - centre)\n const totalLogWays = logChoose(total, selectedN)\n const minimumMass = Math.exp(\n logExtremeSubsetWays(doubledRanks, selectedN, 'minimum') - totalLogWays,\n )\n const maximumMass = Math.exp(\n logExtremeSubsetWays(doubledRanks, selectedN, 'maximum') - totalLogWays,\n )\n\n if (minimumDeviation > maximumDeviation) return minimumMass\n if (maximumDeviation > minimumDeviation) return maximumMass\n return Math.min(1, minimumMass + maximumMass)\n}\n\nfunction logExtremeSubsetWays(\n sortedRanks: readonly number[],\n selectedN: number,\n side: 'minimum' | 'maximum',\n): number {\n const boundaryIndex = side === 'minimum' ? selectedN - 1 : sortedRanks.length - selectedN\n const boundary = sortedRanks[boundaryIndex]!\n let first = boundaryIndex\n let afterLast = boundaryIndex + 1\n while (first > 0 && sortedRanks[first - 1] === boundary) first--\n while (afterLast < sortedRanks.length && sortedRanks[afterLast] === boundary) afterLast++\n\n const tieSize = afterLast - first\n const fixed = side === 'minimum' ? first : sortedRanks.length - afterLast\n return logChoose(tieSize, selectedN - fixed)\n}\n\n/**\n * Exact conditional two-sided p for the two-sample rank test.\n *\n * Convolves the observed doubled midranks into the null distribution of group\n * a's rank sum over every `C(n₁+n₂, n₁)` split — identical to enumerating the\n * splits, but `O(N·n₁·ΣR)` instead of `O(C(N,n₁)·n₁n₂)`. Conditioning on the\n * realised multiset makes the tie handling exact rather than a correction.\n *\n * The null is symmetric about `n₁n₂/2` (negating every value maps `U → n₁n₂ −\n * U` and permutes the split set onto itself), so the two-sided p is the mass\n * at least as far from the centre as the observation.\n */\nfunction exactTwoSampleP(\n doubledRanks: readonly number[],\n n1: number,\n n2: number,\n doubledDeviation: number,\n): { p: number; pFloor: number } {\n const maxSum = doubledRanks.reduce((sum, rank) => sum + rank, 0)\n const width = maxSum + 1\n // ways[k][s] = number of size-k subsets whose doubled rank sum is s.\n const ways: Float64Array[] = Array.from({ length: n1 + 1 }, () => new Float64Array(width))\n ways[0]![0] = 1\n let placed = 0\n for (const rank of doubledRanks) {\n for (let k = Math.min(n1, placed + 1); k >= 1; k--) {\n const from = ways[k - 1]!\n const into = ways[k]!\n for (let sum = maxSum - rank; sum >= 0; sum--) {\n const count = from[sum]!\n if (count !== 0) into[sum + rank]! += count\n }\n }\n placed++\n }\n\n // U = rankSum − n₁(n₁+1)/2, so doubled U = s − n₁(n₁+1), and the doubled\n // centre 2·(n₁n₂/2) is n₁n₂.\n const shift = n1 * (n1 + 1) + n1 * n2\n const chosen = ways[n1]!\n let totalWays = 0\n let extremeWays = 0\n let tailWays = 0\n let maxDeviation = -1\n for (let sum = 0; sum < width; sum++) {\n const count = chosen[sum]!\n if (count === 0) continue\n totalWays += count\n const deviation = Math.abs(sum - shift)\n if (deviation >= doubledDeviation) tailWays += count\n if (deviation > maxDeviation) {\n maxDeviation = deviation\n extremeWays = count\n } else if (deviation === maxDeviation) {\n extremeWays += count\n }\n }\n return { p: tailWays / totalWays, pFloor: extremeWays / totalWays }\n}\n\n/**\n * Exact conditional two-sided p for the paired signed-rank test.\n *\n * Convolves the observed doubled absolute midranks over all `2ⁿ` sign\n * assignments in `O(n·ΣR)`. Probabilities rather than counts keep `2ⁿ` off the\n * arithmetic. The null is symmetric about `n(n+1)/4`.\n */\nfunction exactSignedRankP(\n doubledRanks: readonly number[],\n doubledDeviation: number,\n): { p: number; pFloor: number } {\n const maxSum = doubledRanks.reduce((sum, rank) => sum + rank, 0)\n const width = maxSum + 1\n let mass = new Float64Array(width)\n mass[0] = 1\n for (const rank of doubledRanks) {\n const next = new Float64Array(width)\n for (let sum = 0; sum < width; sum++) {\n const probability = mass[sum]!\n if (probability === 0) continue\n next[sum]! += probability * 0.5\n next[sum + rank]! += probability * 0.5\n }\n mass = next\n }\n\n const centre = maxSum / 2\n let tail = 0\n let extreme = 0\n let maxDeviation = -1\n for (let sum = 0; sum < width; sum++) {\n const probability = mass[sum]!\n if (probability === 0) continue\n const deviation = Math.abs(sum - centre)\n if (deviation >= doubledDeviation) tail += probability\n if (deviation > maxDeviation) {\n maxDeviation = deviation\n extreme = probability\n } else if (deviation === maxDeviation) {\n extreme += probability\n }\n }\n return { p: Math.min(1, tail), pFloor: Math.min(1, extreme) }\n}\n","/**\n * Matched-pair arm comparison — \"did the treatment arm beat the baseline arm\n * on the SAME work items?\"\n *\n * An arm A/B over run records is only trustworthy when it is PAIRED: the same\n * task/scenario/seed evaluated under both arms, compared item-by-item, so\n * inter-item difficulty variance cancels instead of masquerading as an arm\n * effect. This module owns the two error-prone steps every consumer otherwise\n * hand-rolls:\n *\n * 1. Pairing — matching rows across arms by `pairKey` (and by `repKey`\n * within multi-rep items), with leftovers REPORTED rather than silently\n * dropped (a silently unbalanced pairing biases every paired statistic\n * downstream). Pairing never keys on outcome content: matching reps by\n * their outcomes deflates discordant-pair counts and makes McNemar\n * anti-conservative, so reps pair only by (`pairKey`, `repKey`) identity.\n * 2. Composition — feeding the matched pairs to the correct paired\n * estimators that already live in `statistics`: `mcnemar` +\n * `pairedRiskDifference` for pass/fail, `pairedBootstrap` +\n * `wilcoxonSignedRank` for continuous metrics. No statistic is\n * re-implemented here.\n *\n * The row shape is deliberately structural — callers project a `RunRecord`\n * (or any record) into `{ pairKey, arm, pass?, metrics? }`. Arm names are\n * caller-supplied parameters; the module ships no domain literal.\n */\n\nimport { ValidationError } from './errors'\nimport type { RunRecord } from './run-record'\nimport type { McNemarResult, PairedBootstrapOptions, PairedBootstrapResult } from './statistics'\nimport {\n mcnemar,\n pairedBootstrap,\n pairedRiskDifference,\n type RiskDifferenceResult,\n wilcoxonSignedRank,\n} from './statistics'\n\n/** One arm observation of one work item. Structural on purpose: callers\n * project their own record type (e.g. a `RunRecord`) into this shape. */\nexport interface PairedArmRow {\n /** Matching key — rows sharing a `pairKey` across both arms form pairs\n * (typically the task/scenario/seed identity). */\n pairKey: string\n /** Rep identity within a `pairKey` (e.g. a seed or rep number). Required on\n * every row of a `pairKey` that has more than one rep in either arm; reps\n * then pair only on exact (`pairKey`, `repKey`) match, never on outcome\n * content. Optional when each arm has at most one rep of the item. */\n repKey?: string\n /** Arm label this row was produced under. */\n arm: string\n /** Binary outcome; omit when the comparison has no pass/fail notion. */\n pass?: boolean\n /** Named numeric measurements (score, cost, latency, …). */\n metrics?: Record<string, number>\n}\n\nexport interface PairArmsOptions {\n /** Arm treated as the control side of every pair. */\n baselineArm: string\n /** Arm treated as the treatment side of every pair. */\n treatmentArm: string\n}\n\n/** One matched (baseline, treatment) observation of the same work item. */\nexport interface MatchedPair {\n pairKey: string\n /** 0-based position of this pair within its `pairKey`, ordered by sorted\n * `repKey` (always 0 for a single-rep item). The rep identity itself is on\n * the rows (`baseline.repKey` / `treatment.repKey`). */\n repIndex: number\n baseline: PairedArmRow\n treatment: PairedArmRow\n}\n\nexport interface PairArmsResult {\n /** Matched pairs, ordered by (`pairKey`, `repIndex`). */\n pairs: MatchedPair[]\n /** Baseline rows left without a treatment counterpart — reported, never\n * silently dropped. */\n unpairedBaseline: PairedArmRow[]\n /** Treatment rows left without a baseline counterpart. */\n unpairedTreatment: PairedArmRow[]\n}\n\n/**\n * Match rows across two arms into (baseline, treatment) pairs by `pairKey`.\n *\n * A `pairKey` with at most one row per arm pairs directly, no `repKey`\n * needed. A `pairKey` with multiple reps in either arm requires `repKey` on\n * every one of its rows, and reps pair only on exact (`pairKey`, `repKey`)\n * match — pairing is keyed purely on row identity, never on outcome content\n * (outcome-keyed matching deflates discordant counts and biases McNemar), and\n * is therefore independent of input order. Reps whose `repKey` has no\n * counterpart in the other arm, and items present in only one arm, land in\n * the unpaired lists — reported, never truncated.\n *\n * Fail-loud: throws when either named arm has zero rows (an unknown arm\n * name would otherwise read as \"everything unpaired\"), when the two arm\n * names are equal, when a multi-rep `pairKey` has a row without `repKey`, or\n * when a (`pairKey`, arm) group repeats a `repKey` (the match would be\n * ambiguous).\n */\nexport function pairArms(rows: readonly PairedArmRow[], opts: PairArmsOptions): PairArmsResult {\n const { baselineArm, treatmentArm } = opts\n if (baselineArm === treatmentArm) {\n throw new ValidationError(\n `pairArms: baselineArm and treatmentArm are both '${baselineArm}' — an arm cannot be compared to itself`,\n )\n }\n\n // arm → pairKey → rows\n const byArm = new Map<string, Map<string, PairedArmRow[]>>()\n const armsSeen = new Set<string>()\n for (const row of rows) {\n armsSeen.add(row.arm)\n if (row.arm !== baselineArm && row.arm !== treatmentArm) continue\n const byKey = byArm.get(row.arm) ?? new Map<string, PairedArmRow[]>()\n const group = byKey.get(row.pairKey) ?? []\n group.push(row)\n byKey.set(row.pairKey, group)\n byArm.set(row.arm, byKey)\n }\n\n for (const arm of [baselineArm, treatmentArm]) {\n if (!byArm.has(arm)) {\n const seen = [...armsSeen].sort().join(', ') || '<none>'\n throw new ValidationError(`pairArms: no rows for arm '${arm}' (arms present: ${seen})`)\n }\n }\n\n const baselineByKey = byArm.get(baselineArm)!\n const treatmentByKey = byArm.get(treatmentArm)!\n\n const allKeys = [...new Set([...baselineByKey.keys(), ...treatmentByKey.keys()])].sort()\n const pairs: MatchedPair[] = []\n const unpairedBaseline: PairedArmRow[] = []\n const unpairedTreatment: PairedArmRow[] = []\n for (const pairKey of allKeys) {\n const b = baselineByKey.get(pairKey) ?? []\n const t = treatmentByKey.get(pairKey) ?? []\n\n if (b.length <= 1 && t.length <= 1) {\n if (b.length === 1 && t.length === 1) {\n const baseline = b[0]!\n const treatment = t[0]!\n if (baseline.repKey !== undefined || treatment.repKey !== undefined) {\n if (\n baseline.repKey === undefined ||\n treatment.repKey === undefined ||\n baseline.repKey !== treatment.repKey\n ) {\n unpairedBaseline.push(baseline)\n unpairedTreatment.push(treatment)\n continue\n }\n }\n pairs.push({ pairKey, repIndex: 0, baseline, treatment })\n } else {\n unpairedBaseline.push(...b)\n unpairedTreatment.push(...t)\n }\n continue\n }\n\n const bByRep = indexByRepKey(b, pairKey, baselineArm)\n const tByRep = indexByRepKey(t, pairKey, treatmentArm)\n const repKeys = [...new Set([...bByRep.keys(), ...tByRep.keys()])].sort()\n let repIndex = 0\n for (const repKey of repKeys) {\n const baseline = bByRep.get(repKey)\n const treatment = tByRep.get(repKey)\n if (baseline !== undefined && treatment !== undefined) {\n pairs.push({ pairKey, repIndex: repIndex++, baseline, treatment })\n } else if (baseline !== undefined) {\n unpairedBaseline.push(baseline)\n } else if (treatment !== undefined) {\n unpairedTreatment.push(treatment)\n }\n }\n }\n\n return { pairs, unpairedBaseline, unpairedTreatment }\n}\n\n/** Index a multi-rep (pairKey, arm) group by `repKey`, enforcing that every\n * row carries one and that no repKey repeats within the group. */\nfunction indexByRepKey(\n group: readonly PairedArmRow[],\n pairKey: string,\n arm: string,\n): Map<string, PairedArmRow> {\n const byRep = new Map<string, PairedArmRow>()\n for (const row of group) {\n if (row.repKey === undefined) {\n throw new ValidationError(\n `pairArms: pairKey '${pairKey}' has multiple reps in an arm, but a row in arm '${arm}' ` +\n `is missing repKey — multi-rep items require an explicit repKey on every row so reps ` +\n `pair by identity (pairing reps by outcome or by index would bias the paired statistics)`,\n )\n }\n if (byRep.has(row.repKey)) {\n throw new ValidationError(\n `pairArms: duplicate repKey '${row.repKey}' for pairKey '${pairKey}' in arm '${arm}' — ` +\n `(pairKey, repKey) must uniquely identify a rep within an arm`,\n )\n }\n byRep.set(row.repKey, row)\n }\n return byRep\n}\n\n/** Paired pass/fail comparison over the pairs where BOTH sides carry `pass`. */\nexport interface PairedCorrectness {\n /** Discordant pairs where the treatment passed and the baseline failed. */\n b10: number\n /** Discordant pairs where the baseline passed and the treatment failed. */\n b01: number\n /** Exact McNemar significance over the paired outcomes (`b === b10`, `c === b01`). */\n mcnemar: McNemarResult\n /** Paired effect size: p(treatment) − p(baseline) with a paired-variance CI. */\n riskDifference: RiskDifferenceResult\n}\n\n/** Paired delta summary for one named metric (delta = treatment − baseline). */\nexport interface PairedMetricDelta {\n name: string\n /** Pairs where BOTH sides carry a finite value for this metric. */\n n: number\n /** Pairs where at least one side does not carry the metric. */\n nMissing: number\n /** Median paired delta, or null when `n === 0`. */\n medianDelta: number | null\n /** Mean paired delta, or null when `n === 0`. */\n meanDelta: number | null\n /** Bootstrap CI on the paired delta (`pairedBootstrap`); null when\n * `n === 0` — a zero-width [0, 0] interval on no data would read as a\n * measured tight null. */\n bootstrapCi: PairedBootstrapResult | null\n /** Wilcoxon signed-rank test on the paired deltas; null when `n === 0`. */\n wilcoxon: { w: number; p: number } | null\n}\n\nexport interface ComparePairedArmsOptions extends PairArmsOptions {\n /** Metrics to compare. Default: every metric name observed on any matched\n * pair, sorted. A name that appears on no pair is still reported (with\n * `n = 0`) so a misspelled metric is visible instead of vanishing. */\n metricNames?: string[]\n /** Passed through to `pairedBootstrap` — set `seed` for reproducible CIs. */\n bootstrap?: PairedBootstrapOptions\n}\n\nexport interface PairedArmsComparison {\n nPairs: number\n nUnpairedBaseline: number\n nUnpairedTreatment: number\n /** null when no matched pair carries `pass` on both sides — a pass/fail\n * verdict over rows that never measured pass/fail would be fabricated. */\n correctness: PairedCorrectness | null\n metricDeltas: PairedMetricDelta[]\n}\n\n/**\n * Full matched-pair arm comparison: pair via {@link pairArms}, then compose\n * the paired estimators from `statistics` over the matched pairs.\n *\n * Correctness uses only the pairs where both sides carry `pass` (`mcnemar.n`\n * is that subset's size); each metric uses only the pairs where both sides\n * carry a finite value for it, with the remainder counted in `nMissing`.\n * Deltas are treatment − baseline throughout.\n *\n * Fail-loud: inherits {@link pairArms}'s unknown-arm throw, and throws on a\n * non-finite metric value — silently treating corrupt telemetry as \"metric\n * absent\" would misreport it as missing coverage.\n */\nexport function comparePairedArms(\n rows: readonly PairedArmRow[],\n opts: ComparePairedArmsOptions,\n): PairedArmsComparison {\n const { pairs, unpairedBaseline, unpairedTreatment } = pairArms(rows, opts)\n\n let correctness: PairedCorrectness | null = null\n const baselinePass: number[] = []\n const treatmentPass: number[] = []\n for (const pair of pairs) {\n if (pair.baseline.pass === undefined || pair.treatment.pass === undefined) continue\n baselinePass.push(pair.baseline.pass ? 1 : 0)\n treatmentPass.push(pair.treatment.pass ? 1 : 0)\n }\n if (baselinePass.length > 0) {\n const mc = mcnemar(baselinePass, treatmentPass)\n correctness = {\n b10: mc.b,\n b01: mc.c,\n mcnemar: mc,\n riskDifference: pairedRiskDifference(baselinePass, treatmentPass),\n }\n }\n\n const metricNames =\n opts.metricNames ??\n [\n ...new Set(\n pairs.flatMap((p) => [\n ...Object.keys(p.baseline.metrics ?? {}),\n ...Object.keys(p.treatment.metrics ?? {}),\n ]),\n ),\n ].sort()\n\n const metricDeltas: PairedMetricDelta[] = metricNames.map((name) => {\n const before: number[] = []\n const after: number[] = []\n let nMissing = 0\n for (const pair of pairs) {\n const b = metricValue(pair.baseline, name)\n const t = metricValue(pair.treatment, name)\n if (b === undefined || t === undefined) {\n nMissing++\n continue\n }\n before.push(b)\n after.push(t)\n }\n const bootstrapCi = before.length === 0 ? null : pairedBootstrap(before, after, opts.bootstrap)\n return {\n name,\n n: before.length,\n nMissing,\n medianDelta: bootstrapCi?.median ?? null,\n meanDelta: bootstrapCi?.mean ?? null,\n bootstrapCi,\n wilcoxon: before.length === 0 ? null : wilcoxonSignedRank(before, after),\n }\n })\n\n return {\n nPairs: pairs.length,\n nUnpairedBaseline: unpairedBaseline.length,\n nUnpairedTreatment: unpairedTreatment.length,\n correctness,\n metricDeltas,\n }\n}\n\nexport interface MatchedRunRecordPair {\n pairKey: string\n repKey: string\n baseline: RunRecord\n treatment: RunRecord\n}\n\nexport interface PairRunRecordsResult {\n pairs: MatchedRunRecordPair[]\n unpairedBaseline: RunRecord[]\n unpairedTreatment: RunRecord[]\n}\n\ninterface RunRecordArmRow extends PairedArmRow {\n run: RunRecord\n repKey: string\n}\n\n/**\n * Pair two RunRecord arms by the identity of the evaluated work:\n * `(experimentId, scenarioId, seed)`.\n *\n * Falling back to array order, candidate id, or experiment id can compare\n * different tasks and fabricate lift. Duplicate identities throw.\n */\nexport function pairRunRecords(\n baselineRuns: readonly RunRecord[],\n treatmentRuns: readonly RunRecord[],\n): PairRunRecordsResult {\n const baselineRows = runRecordArmRows(baselineRuns, 'baseline')\n const treatmentRows = runRecordArmRows(treatmentRuns, 'treatment')\n validateRunRecordArmRows(baselineRows, 'baseline')\n validateRunRecordArmRows(treatmentRows, 'treatment')\n if (baselineRows.length === 0 || treatmentRows.length === 0) {\n return {\n pairs: [],\n unpairedBaseline: baselineRows.map((row) => row.run),\n unpairedTreatment: treatmentRows.map((row) => row.run),\n }\n }\n\n const result = pairArms([...baselineRows, ...treatmentRows], {\n baselineArm: 'baseline',\n treatmentArm: 'treatment',\n })\n return {\n pairs: result.pairs.map((pair) => {\n const baseline = pair.baseline as RunRecordArmRow\n const treatment = pair.treatment as RunRecordArmRow\n return {\n pairKey: pair.pairKey,\n repKey: baseline.repKey,\n baseline: baseline.run,\n treatment: treatment.run,\n }\n }),\n unpairedBaseline: result.unpairedBaseline.map((row) => (row as RunRecordArmRow).run),\n unpairedTreatment: result.unpairedTreatment.map((row) => (row as RunRecordArmRow).run),\n }\n}\n\nfunction runRecordArmRows(runs: readonly RunRecord[], arm: string): RunRecordArmRow[] {\n return runs.map((run) => {\n const scenarioId = run.scenarioId.trim()\n if (!scenarioId) {\n throw new ValidationError(\n `pairRunRecords: run '${run.runId}' is missing scenarioId; paired comparisons require explicit scenario identity`,\n )\n }\n return {\n pairKey: JSON.stringify([run.experimentId, scenarioId]),\n repKey: String(run.seed),\n arm,\n run,\n }\n })\n}\n\nfunction validateRunRecordArmRows(rows: readonly RunRecordArmRow[], arm: string): void {\n const byPairKey = new Map<string, RunRecordArmRow[]>()\n for (const row of rows) {\n const group = byPairKey.get(row.pairKey) ?? []\n group.push(row)\n byPairKey.set(row.pairKey, group)\n }\n for (const [pairKey, group] of byPairKey) {\n if (group.length > 1) indexByRepKey(group, pairKey, arm)\n }\n}\n\nfunction metricValue(row: PairedArmRow, name: string): number | undefined {\n const v = row.metrics?.[name]\n if (v === undefined) return undefined\n if (!Number.isFinite(v)) {\n throw new ValidationError(\n `comparePairedArms: non-finite value for metric '${name}' on pairKey '${row.pairKey}' (arm '${row.arm}'): ${v}`,\n )\n }\n return v\n}\n"],"mappings":";;;;;;;;;;;AAgCA,SAAgB,OAAO,WAAmB,GAAW,aAAa,KAA0B;CAC1F,IAAI,KAAK,GAAG,OAAO;EAAE,UAAU;EAAG,OAAO;EAAG,OAAO;CAAE;CACrD,IAAI,YAAY,KAAK,YAAY,GAC/B,MAAM,IAAI,MAAM,sBAAsB,UAAU,mBAAmB,EAAE,EAAE;CAEzE,MAAM,IAAI,UAAU,KAAK,IAAI,cAAc,CAAC;CAC5C,MAAM,IAAI,YAAY;CACtB,MAAM,KAAK,IAAI;CACf,MAAM,QAAQ,IAAI,KAAK;CACvB,MAAM,UAAU,IAAI,MAAM,IAAI,MAAM;CACpC,MAAM,OAAQ,IAAI,KAAK,MAAM,KAAK,IAAI,KAAK,MAAM,IAAI,MAAM,CAAC,IAAK;CACjE,OAAO;EACL,UAAU;EACV,OAAO,KAAK,IAAI,GAAG,SAAS,IAAI;EAChC,OAAO,KAAK,IAAI,GAAG,SAAS,IAAI;CAClC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;AA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wBAAwB,IAAI,MAAM,+EACpC;EAEF,OAAO;GACL,SAAS,KAAK,UAAU,CAAC,IAAI,cAAc,UAAU,CAAC;GACtD,QAAQ,OAAO,IAAI,IAAI;GACvB;GACA;EACF;CACF,CAAC;AACH;AAEA,SAAS,yBAAyB,MAAkC,KAAmB;CACrF,MAAM,4BAAY,IAAI,IAA+B;CACrD,KAAK,MAAM,OAAO,MAAM;EACtB,MAAM,QAAQ,UAAU,IAAI,IAAI,OAAO,KAAK,CAAC;EAC7C,MAAM,KAAK,GAAG;EACd,UAAU,IAAI,IAAI,SAAS,KAAK;CAClC;CACA,KAAK,MAAM,CAAC,SAAS,UAAU,WAC7B,IAAI,MAAM,SAAS,GAAG,cAAc,OAAO,SAAS,GAAG;AAE3D;AAEA,SAAS,YAAY,KAAmB,MAAkC;CACxE,MAAM,IAAI,IAAI,UAAU;CACxB,IAAI,MAAM,KAAA,GAAW,OAAO,KAAA;CAC5B,IAAI,CAAC,OAAO,SAAS,CAAC,GACpB,MAAM,IAAI,gBACR,mDAAmD,KAAK,gBAAgB,IAAI,QAAQ,UAAU,IAAI,IAAI,MAAM,GAC9G;CAEF,OAAO;AACT"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"promotion-policy-DWOm70gx.js","names":[],"sources":["../src/campaign/gates/promotion-policy.ts"],"sourcesContent":["/**\n * Promotion policy over the evidence VECTOR — the substrate's answer to \"never\n * collapse the multi-objective promotion decision into one scalar.\" A\n * `defaultProductionGate` is one opinionated composition; this module factors\n * the decision into two reusable pieces so MANY policies can compete over the\n * SAME evidence (the quant-desk pattern: one evidence bus, plural strategies):\n *\n * buildEvidenceVector(ctx, objectives, opts) -> EvidenceVector // the bus\n * PromotionPolicy = (ev: EvidenceVector) => GateResult // a strategy\n * paretoPolicy(ev) // the default strategy\n * paretoSignificanceGate(options): Gate // bus + policy as a Gate\n *\n * The Pareto policy is SYMMETRIC multi-objective: every objective is BOTH a\n * potential gain source AND a safety floor (unlike `defaultProductionGate`,\n * where only `composite` can win and `criticalDimensions` are pure floors). A\n * candidate ships iff it weakly DOMINATES the baseline at the confidence level —\n * no objective credibly worse (CI floor breach) AND at least one objective\n * credibly better (CI gain). Insufficient evidence on ANY axis -> need_more_work\n * (NOT folded into hold: \"gather more reps\" and \"reject\" are different actions).\n *\n * Cost/latency are NOT CI axes here — `GateContext` carries only an aggregate\n * per-side cost, no per-cell observation vector to bootstrap. Treat them as hard\n * constraints (compose with a budget gate via `composeGate`), not faked CIs.\n */\n\nimport {\n decidePairedPromotion,\n type PairedDecisionMethod,\n type PairedDecisionStatistic,\n type PairedMcNemarEvidence,\n} from '../../paired-promotion-decision'\nimport type { Direction } from '../../pareto'\nimport {\n DECISION_PAIRED_DELTA_STATISTIC,\n type PairedBootstrapResult,\n pairedBootstrap,\n} from '../../statistics'\nimport type { Gate, GateContext, GateDecision, GateResult, JudgeScore, Scenario } from '../types'\nimport { detectScale, pairHoldout } from './statistical-heldout'\n\n/** Where an objective's per-cell scalar comes from. `composite` reads the\n * judge's composite; `dimension` reads a named per-dimension score. */\nexport type ObjectiveSource = { kind: 'composite' } | { kind: 'dimension'; dimension: string }\n\nexport interface PromotionObjective {\n /** Stable label used in reports + `contributingGates`. */\n name: string\n source: ObjectiveSource\n /** 'maximize' (quality dims) or 'minimize' (error/risk/length dims). Orients\n * the paired delta so a positive bootstrap always means \"candidate better\". */\n direction: Direction\n /** The good-direction paired-delta CI lower bound must EXCEED this to count\n * as a significant gain on this axis. Interpreted in the judge's native\n * scale. Default 0 (⇒ \"confidently better\"). */\n gainThreshold?: number\n /** A floor breach (regression) is declared when the good-direction CI lower\n * bound is below −floorTolerance, or when the exact small-sample test proves\n * a drop past it. When omitted it auto-scales off observed magnitudes\n * (0.05 on [0,1], 5 on 0-100), matching `dimensionRegressions`. */\n floorTolerance?: number\n}\n\n/** Per-axis verdict from the good-direction paired bootstrap. */\nexport type AxisVerdict = 'improved' | 'regressed' | 'flat' | 'few_runs'\n\nexport interface AxisEvidence {\n name: string\n source: ObjectiveSource\n direction: Direction\n /** Paired bootstrap on the GOOD-DIRECTION delta (oriented by `direction`):\n * a positive value means the candidate is better on this axis.\n *\n * DIAGNOSTIC on a pass/fail axis: there the verdict is decided on Tango's\n * score interval instead, because a percentile bootstrap over a three-atom\n * delta lattice is not a valid interval at the nonzero margin `floorTolerance`\n * and `gainThreshold` create. `ci` carries the interval that decided. */\n bootstrap: PairedBootstrapResult\n /** Which paired statistic `bootstrap.low`/`.high` bracket. `'mean'` unless the\n * caller asked for the median — on a pass/fail axis the median and its whole\n * CI are pinned at 0 by tie domination and can see neither a gain nor a\n * regression. `bootstrap.median` still carries the median point estimate. */\n bootstrapStatistic: 'median' | 'mean'\n /** The interval the axis verdict was actually decided on, good-direction and\n * in the axis's native units. */\n ci: { low: number; high: number }\n /** Which estimator produced `ci`. */\n decisionStatistic: PairedDecisionStatistic\n /** McNemar's exact evidence on a pass/fail axis; null otherwise. */\n mcnemar: PairedMcNemarEvidence | null\n /** `ci` has zero width — no evidence in either direction, so the axis is\n * neither improved nor regressed however the point estimate sits. */\n indeterminate: boolean\n /** Paired observations contributing to this axis. */\n n: number\n minimumRequired: number\n decisionMethod: PairedDecisionMethod\n gainThreshold: number\n floorTolerance: number\n verdict: AxisVerdict\n}\n\nexport interface EvidenceVector {\n /** One entry per objective — NOTHING averaged across axes. */\n axes: AxisEvidence[]\n /** Smallest paired n across axes that produced observations — the binding\n * evidence-sufficiency constraint. 0 when no axis produced observations. */\n minN: number\n /** Aggregate per-side cost from the gate context (a constraint input, not a\n * CI axis — see the module header). */\n cost: { candidate: number; baseline: number }\n}\n\n/** A promotion strategy: a pure function from the evidence vector to a verdict.\n * Many policies can run over the same `EvidenceVector` and disagree — that's\n * the point (competing strategies, shared evidence). */\nexport type PromotionPolicy = (ev: EvidenceVector) => GateResult\n\nexport interface BuildEvidenceVectorOptions {\n /** Minimum paired observations before an axis can claim significance; below\n * it the axis is `few_runs`. The exact small-sample test may require more\n * observations at the selected confidence. */\n minProductiveRuns?: number\n /** Confidence level for every axis bootstrap. Default 0.95. */\n confidence?: number\n /** Bootstrap resamples. Default 2000. */\n resamples?: number\n /** Fixed bootstrap seed for a deterministic, reproducible verdict. Default 1337. */\n seed?: number\n /** Paired statistic every axis CI is computed on. Default `'mean'` — see\n * {@link DECISION_PAIRED_DELTA_STATISTIC} for why the median is not. */\n statistic?: 'mean' | 'median'\n}\n\n/**\n * The Evidence Bus. For each objective, pair candidate vs baseline by full\n * cellId and bootstrap a CI on the good-direction paired delta. Reuses the\n * exact `pairHoldout` + `pairedBootstrap` machinery the held-out gate uses, so\n * a single source of truth governs pairing granularity + scale handling.\n */\nexport function buildEvidenceVector<TArtifact, TScenario extends Scenario>(\n ctx: GateContext<TArtifact, TScenario>,\n objectives: PromotionObjective[],\n opts: BuildEvidenceVectorOptions = {},\n): EvidenceVector {\n if (objectives.length === 0) {\n throw new Error('buildEvidenceVector: at least 1 objective required')\n }\n const confidence = opts.confidence ?? 0.95\n const resamples = opts.resamples ?? 2000\n const seed = opts.seed ?? 1337\n const baseline = ctx.baselineJudgeScores ?? ctx.judgeScores\n const scenarioIds = new Set(ctx.scenarios.map((s) => s.id))\n\n const axes: AxisEvidence[] = []\n for (const obj of objectives) {\n let select: (s: JudgeScore) => number | undefined\n if (obj.source.kind === 'composite') {\n select = (s) => s.composite\n } else {\n const dim = obj.source.dimension\n select = (s) => s.dimensions[dim]\n }\n const paired = pairHoldout(ctx.judgeScores, baseline, scenarioIds, select)\n // Orient to the good direction: maximize ⇒ bootstrap (candidate − baseline);\n // minimize ⇒ bootstrap (baseline − candidate) by swapping args, so a\n // positive bootstrap always reads as \"candidate better on this axis\".\n const before = obj.direction === 'maximize' ? paired.before : paired.after\n const after = obj.direction === 'maximize' ? paired.after : paired.before\n const n = paired.before.length\n const floorTolerance =\n obj.floorTolerance ?? 0.05 * detectScale([...paired.before, ...paired.after])\n const gainThreshold = obj.gainThreshold ?? 0\n // Axes are decided on the MEAN paired delta — which for a pass/fail axis is\n // exactly the change in success rate. The median is structurally blind on\n // the shapes eval data lands in: with most pairs tied its bootstrap CI\n // collapses to [0,0] and the axis reads 'flat', hiding real gains AND real\n // regressions — pass/fail axes on ANY encoding ({0,1} and the 0-100 one\n // `detectScale` exists for), and low-cardinality axes even below half ties.\n // Orthogonal to `pairedDeltaTest`'s own small-sample switch: that picks the\n // TEST (bootstrap CI at n ≥ 20, exact sign test below), this picks the\n // ESTIMATOR the test is applied to. Both are needed — an exact sign test on\n // a tie-pinned median is still blind.\n const bootstrapStatistic = opts.statistic ?? DECISION_PAIRED_DELTA_STATISTIC\n // Both burdens of proof route through the ONE shared rule\n // (`decidePairedPromotion`), so a pass/fail axis is judged on Tango's score\n // interval — the only paired-binary construction valid at the nonzero\n // margins `gainThreshold` / `floorTolerance` create — and a zero-width\n // interval cannot buy a verdict in either direction.\n const improvement = decidePairedPromotion(before, after, {\n confidence,\n resamples,\n statistic: bootstrapStatistic,\n seed,\n threshold: gainThreshold,\n minPairs: opts.minProductiveRuns,\n })\n const regression = decidePairedPromotion(after, before, {\n confidence,\n resamples,\n statistic: bootstrapStatistic,\n seed,\n threshold: floorTolerance,\n minPairs: opts.minProductiveRuns,\n })\n const bootstrap =\n improvement.bootstrap ??\n pairedBootstrap(before, after, {\n confidence,\n resamples,\n statistic: bootstrapStatistic,\n seed,\n })\n // A floor breach fires on EITHER burden of proof, because they cover\n // different failures and the floor is the anti-Goodhart guard:\n // - `improvement.low < -floorTolerance` — the CREDIBLE WORST CASE exceeds\n // the tolerance. This is the contract `AxisEvidence.floorTolerance` and\n // `paretoPolicy`'s own reason string state, and it is the conservative\n // posture a safety axis needs: block unless the data can rule the\n // breach out, rather than waiting for the breach to be proven. Read off\n // the DECIDING interval, so a pass/fail axis is not screened by a\n // bootstrap that is pinned wherever ties dominate.\n // - `regression.promote` — a PROVEN drop past the tolerance. Adds the\n // small-sample path, where the decision is an exact sign test because\n // the bootstrap interval is descriptive only.\n // The credible-worst-case arm stays on the BOOTSTRAP, deliberately. Reading\n // it off the score interval instead would change what the floor MEANS on a\n // pass/fail axis: with every pair concordant the score interval is\n // ±z²/(n+z²) — ±0.39 at n=6, ±0.16 at n=20 — so a completely unchanged\n // safety axis would breach a 0.05 floor at any realistic n, and the gate\n // would refuse everything. That the bootstrap arm is instead fail-OPEN on a\n // tied pass/fail axis is a real and separate weakness: the honest fix is a\n // minimum-power requirement on the floor, not a wider interval, because the\n // data genuinely cannot rule a 5pp drop out at n=20 and a gate that says so\n // by blocking every candidate is not usable. `regression.promote` — the\n // PROVEN-drop arm — does route through the shared rule, so a real pass/fail\n // regression is now caught on an interval valid at the nonzero tolerance.\n const floorBreached = bootstrap.low < -floorTolerance || regression.promote\n // Floor check precedes the gain check: a credible regression must never be\n // masked as \"improved\". With the defaults (gainThreshold 0, positive floor)\n // the regions are disjoint and order is moot, but a consumer who sets a\n // negative gainThreshold (\"accept small dips\") could otherwise have a real\n // floor breach classified as a gain — anti-Goodhart wins the tie.\n const verdict: AxisVerdict = !improvement.sufficient\n ? 'few_runs'\n : floorBreached\n ? 'regressed'\n : improvement.promote\n ? 'improved'\n : 'flat'\n axes.push({\n name: obj.name,\n source: obj.source,\n direction: obj.direction,\n bootstrap,\n bootstrapStatistic,\n ci: { low: improvement.low, high: improvement.high },\n decisionStatistic: improvement.statistic,\n mcnemar: improvement.mcnemar,\n indeterminate: improvement.indeterminate,\n n,\n minimumRequired: improvement.minimumPairs,\n decisionMethod: improvement.method,\n gainThreshold,\n floorTolerance,\n verdict,\n })\n }\n const ns = axes.map((a) => a.n).filter((n) => n > 0)\n const minN = ns.length > 0 ? Math.min(...ns) : 0\n return { axes, minN, cost: { candidate: ctx.cost.candidate, baseline: ctx.cost.baseline } }\n}\n\n/**\n * The default strategy: symmetric multi-objective Pareto significance. Ship iff\n * the candidate weakly dominates the baseline at the confidence level — no axis\n * credibly worse AND ≥1 axis credibly better. Floor breach on any axis → hold\n * (anti-Goodhart, dominates everything). Insufficient evidence on any axis →\n * need_more_work. Statistically equivalent → hold (never ship noise).\n */\nexport const paretoPolicy: PromotionPolicy = (ev) => {\n const contributingGates = ev.axes.map((ax) => ({\n name: `objective:${ax.name}`,\n status:\n ax.verdict === 'regressed'\n ? ('fail' as const)\n : ax.verdict === 'few_runs'\n ? ('not_evaluated' as const)\n : ('pass' as const),\n detail: {\n direction: ax.direction,\n source: ax.source,\n verdict: ax.verdict,\n n: ax.n,\n deltaMedian: ax.bootstrap.median,\n ciLow: ax.ci.low,\n ciHigh: ax.ci.high,\n decisionStatistic: ax.decisionStatistic,\n decisionMethod: ax.decisionMethod,\n mcnemar: ax.mcnemar,\n indeterminate: ax.indeterminate,\n bootstrapCiLow: ax.bootstrap.low,\n bootstrapCiHigh: ax.bootstrap.high,\n confidence: ax.bootstrap.confidence,\n gainThreshold: ax.gainThreshold,\n floorTolerance: ax.floorTolerance,\n },\n }))\n\n const regressed = ev.axes.filter((a) => a.verdict === 'regressed')\n const fewRuns = ev.axes.filter((a) => a.verdict === 'few_runs')\n const improved = ev.axes.filter((a) => a.verdict === 'improved')\n\n let decision: GateDecision\n const reasons: string[] = []\n if (regressed.length > 0) {\n // Floor breach dominates: a credible regression on ANY axis blocks ship even\n // if another axis improved. This makes the +gain/−safety false positive\n // structurally impossible whenever the safety dim is an objective.\n decision = 'hold'\n for (const a of regressed) {\n reasons.push(\n `objective '${a.name}' regressed: good-direction CI.low ${a.ci.low.toFixed(3)} < -${a.floorTolerance} (n=${a.n})`,\n )\n }\n } else if (fewRuns.length > 0) {\n // No credible regression on the scored axes, but ≥1 axis lacks the evidence\n // to claim a gain ⇒ gather more reps, do NOT reject.\n decision = 'need_more_work'\n for (const a of fewRuns) {\n reasons.push(\n `objective '${a.name}' has only n=${a.n} paired runs — insufficient evidence to claim significance`,\n )\n }\n } else if (improved.length > 0) {\n // Weakly dominates (no axis worse) AND strictly better on ≥1 axis ⇒ a Pareto\n // improvement at the confidence level.\n decision = 'ship'\n reasons.push(\n `Pareto improvement at the confidence level: ${improved\n .map(\n (a) =>\n `'${a.name}' +${a.ci.low > 0 ? a.ci.low.toFixed(3) : a.bootstrap.mean.toFixed(3)} (CI.low ${a.ci.low.toFixed(3)})`,\n )\n .join(', ')}; no objective regressed`,\n )\n } else {\n // Enough evidence, nothing credibly better or worse ⇒ statistically\n // equivalent. Do NOT ship a no-op.\n decision = 'hold'\n reasons.push(\n 'no Pareto improvement: candidate statistically equivalent to baseline on every objective',\n )\n }\n\n // `delta` surfaces the composite axis if present, else the first axis — a\n // single convenience scalar; the vector lives in `contributingGates`.\n const composite = ev.axes.find((a) => a.source.kind === 'composite') ?? ev.axes[0]\n return { decision, reasons, contributingGates, delta: composite?.bootstrap.median }\n}\n\nexport interface ParetoSignificanceGateOptions extends BuildEvidenceVectorOptions {\n /** The objective vector. Every axis is both a gain source and a safety floor. */\n objectives: PromotionObjective[]\n /** Strategy applied to the evidence vector. Default `paretoPolicy`. Override\n * to run a stricter/looser strategy over the SAME bus (competing policies). */\n policy?: PromotionPolicy\n /** Override the gate name in reports. */\n name?: string\n}\n\n/**\n * Wrap the bus + a policy as a `Gate`. Plugs into the existing\n * `runImprovementLoop({ gate })` slot and composes via `composeGate`; default\n * loop behavior is unchanged because consumers opt in by passing this gate.\n */\nexport function paretoSignificanceGate<TArtifact = unknown, TScenario extends Scenario = Scenario>(\n options: ParetoSignificanceGateOptions,\n): Gate<TArtifact, TScenario> {\n if (options.objectives.length === 0) {\n throw new Error('paretoSignificanceGate: at least 1 objective required')\n }\n const policy = options.policy ?? paretoPolicy\n return {\n name: options.name ?? 'paretoSignificanceGate',\n async decide(ctx: GateContext<TArtifact, TScenario>): Promise<GateResult> {\n const ev = buildEvidenceVector(ctx, options.objectives, options)\n return policy(ev)\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2IA,SAAgB,oBACd,KACA,YACA,OAAmC,CAAC,GACpB;CAChB,IAAI,WAAW,WAAW,GACxB,MAAM,IAAI,MAAM,oDAAoD;CAEtE,MAAM,aAAa,KA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