@tangle-network/agent-eval 0.129.0 → 0.130.1

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 (428) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +2 -0
  3. package/dist/active-curriculum-C4mk67HP.js +214 -0
  4. package/dist/active-curriculum-C4mk67HP.js.map +1 -0
  5. package/dist/adversarial-smnADNFS.d.ts +21 -0
  6. package/dist/adversarial-smnADNFS.d.ts.map +1 -0
  7. package/dist/analyst/index.d.ts +81 -2872
  8. package/dist/analyst/index.d.ts.map +1 -0
  9. package/dist/analyst/index.js +319 -360
  10. package/dist/analyst/index.js.map +1 -1
  11. package/dist/analyst-BkTS3C58.d.ts +89 -0
  12. package/dist/analyst-BkTS3C58.d.ts.map +1 -0
  13. package/dist/analyst-LsnNpSkm.js +152 -0
  14. package/dist/analyst-LsnNpSkm.js.map +1 -0
  15. package/dist/analyze-runs-C1CavBMk.js +1067 -0
  16. package/dist/analyze-runs-C1CavBMk.js.map +1 -0
  17. package/dist/analyze-runs-FsgYCinh.d.ts +72 -0
  18. package/dist/analyze-runs-FsgYCinh.d.ts.map +1 -0
  19. package/dist/attribute-vocabulary-DLJ6303h.d.ts +54 -0
  20. package/dist/attribute-vocabulary-DLJ6303h.d.ts.map +1 -0
  21. package/dist/authenticity/index.d.ts +81 -79
  22. package/dist/authenticity/index.d.ts.map +1 -0
  23. package/dist/authenticity/index.js +209 -193
  24. package/dist/authenticity/index.js.map +1 -1
  25. package/dist/baseline-HsBvw_dk.js +550 -0
  26. package/dist/baseline-HsBvw_dk.js.map +1 -0
  27. package/dist/baseline-hG3K85h4.d.ts +125 -0
  28. package/dist/baseline-hG3K85h4.d.ts.map +1 -0
  29. package/dist/belief-state/index.d.ts +448 -1188
  30. package/dist/belief-state/index.d.ts.map +1 -0
  31. package/dist/belief-state/index.js +1617 -1709
  32. package/dist/belief-state/index.js.map +1 -1
  33. package/dist/benchmarks/index.d.ts +2 -891
  34. package/dist/benchmarks/index.js +2 -60
  35. package/dist/benchmarks-BJgDGkAD.js +754 -0
  36. package/dist/benchmarks-BJgDGkAD.js.map +1 -0
  37. package/dist/builder-eval/index.d.ts +150 -662
  38. package/dist/builder-eval/index.d.ts.map +1 -0
  39. package/dist/builder-eval/index.js +356 -345
  40. package/dist/builder-eval/index.js.map +1 -1
  41. package/dist/calibration-CNWWA6K8.js +94 -0
  42. package/dist/calibration-CNWWA6K8.js.map +1 -0
  43. package/dist/campaign/index.d.ts +5 -6381
  44. package/dist/campaign/index.js +3 -213
  45. package/dist/campaign-aKJt6emI.js +3886 -0
  46. package/dist/campaign-aKJt6emI.js.map +1 -0
  47. package/dist/cli.d.ts +1 -1
  48. package/dist/cli.js +164 -175
  49. package/dist/cli.js.map +1 -1
  50. package/dist/client-C97NMzqi.d.ts +581 -0
  51. package/dist/client-C97NMzqi.d.ts.map +1 -0
  52. package/dist/client-CYzbdJOZ.js +637 -0
  53. package/dist/client-CYzbdJOZ.js.map +1 -0
  54. package/dist/code-agent-session-BjkMTQ7H.js +1390 -0
  55. package/dist/code-agent-session-BjkMTQ7H.js.map +1 -0
  56. package/dist/code-agent-session-DqqgOJaz.d.ts +143 -0
  57. package/dist/code-agent-session-DqqgOJaz.d.ts.map +1 -0
  58. package/dist/concurrency-DIxRZF_J.js +85 -0
  59. package/dist/concurrency-DIxRZF_J.js.map +1 -0
  60. package/dist/contract/index.d.ts +645 -5565
  61. package/dist/contract/index.d.ts.map +1 -0
  62. package/dist/contract/index.js +1730 -1938
  63. package/dist/contract/index.js.map +1 -1
  64. package/dist/control.d.ts +3 -1030
  65. package/dist/control.js +2 -33
  66. package/dist/cost-ledger-DIgQUFZZ.js +801 -0
  67. package/dist/cost-ledger-DIgQUFZZ.js.map +1 -0
  68. package/dist/cost-ledger-Dye6jCgg.d.ts +236 -0
  69. package/dist/cost-ledger-Dye6jCgg.d.ts.map +1 -0
  70. package/dist/dataset-BvtnC8Dc.d.ts +115 -0
  71. package/dist/dataset-BvtnC8Dc.d.ts.map +1 -0
  72. package/dist/default-registry-C-vFCSEc.js +2579 -0
  73. package/dist/default-registry-C-vFCSEc.js.map +1 -0
  74. package/dist/default-registry-CNPo-Vsb.d.ts +540 -0
  75. package/dist/default-registry-CNPo-Vsb.d.ts.map +1 -0
  76. package/dist/emitter-CPBAhxum.js +266 -0
  77. package/dist/emitter-CPBAhxum.js.map +1 -0
  78. package/dist/emitter-DGQGoLyj.d.ts +113 -0
  79. package/dist/emitter-DGQGoLyj.d.ts.map +1 -0
  80. package/dist/errors-8YnH8WlF.js +64 -0
  81. package/dist/errors-8YnH8WlF.js.map +1 -0
  82. package/dist/errors-CEk209JS.d.ts +76 -0
  83. package/dist/errors-CEk209JS.d.ts.map +1 -0
  84. package/dist/eval-campaign-DEm6c8ru.js +349 -0
  85. package/dist/eval-campaign-DEm6c8ru.js.map +1 -0
  86. package/dist/execution-tracks-CpgFPpS5.js +93 -0
  87. package/dist/execution-tracks-CpgFPpS5.js.map +1 -0
  88. package/dist/exporters-q9iL-2Jf.js +148 -0
  89. package/dist/exporters-q9iL-2Jf.js.map +1 -0
  90. package/dist/extract-usage-BrQ8mCLX.js +155 -0
  91. package/dist/extract-usage-BrQ8mCLX.js.map +1 -0
  92. package/dist/failure-cluster-CqcvCcdR.d.ts +59 -0
  93. package/dist/failure-cluster-CqcvCcdR.d.ts.map +1 -0
  94. package/dist/feedback-trajectory-CVaeREXV.d.ts +340 -0
  95. package/dist/feedback-trajectory-CVaeREXV.d.ts.map +1 -0
  96. package/dist/fuzz.d.ts +320 -646
  97. package/dist/fuzz.d.ts.map +1 -0
  98. package/dist/fuzz.js +670 -618
  99. package/dist/fuzz.js.map +1 -1
  100. package/dist/hf-dataset-DBJXXoY1.js +763 -0
  101. package/dist/hf-dataset-DBJXXoY1.js.map +1 -0
  102. package/dist/hosted/index.d.ts +11 -831
  103. package/dist/hosted/index.d.ts.map +1 -0
  104. package/dist/hosted/index.js +2 -37
  105. package/dist/index-2JJSA6-r2.d.ts +926 -0
  106. package/dist/index-2JJSA6-r2.d.ts.map +1 -0
  107. package/dist/index-6N0aYmpW.d.ts +217 -0
  108. package/dist/index-6N0aYmpW.d.ts.map +1 -0
  109. package/dist/index-BAvgST_9.d.ts +131 -0
  110. package/dist/index-BAvgST_9.d.ts.map +1 -0
  111. package/dist/index-BvnJuTGD.d.ts +68 -0
  112. package/dist/index-BvnJuTGD.d.ts.map +1 -0
  113. package/dist/index-C61Wi7yg.d.ts +547 -0
  114. package/dist/index-C61Wi7yg.d.ts.map +1 -0
  115. package/dist/index-CD_WZ_Xr.d.ts +2250 -0
  116. package/dist/index-CD_WZ_Xr.d.ts.map +1 -0
  117. package/dist/index-DSC51roc.d.ts +102 -0
  118. package/dist/index-DSC51roc.d.ts.map +1 -0
  119. package/dist/index-Em67JBjs.d.ts +335 -0
  120. package/dist/index-Em67JBjs.d.ts.map +1 -0
  121. package/dist/index.d.ts +3755 -15555
  122. package/dist/index.d.ts.map +1 -0
  123. package/dist/index.js +11182 -11216
  124. package/dist/index.js.map +1 -1
  125. package/dist/integrity-BzRbCHzi.js +424 -0
  126. package/dist/integrity-BzRbCHzi.js.map +1 -0
  127. package/dist/integrity-rmVhXWA7.d.ts +61 -0
  128. package/dist/integrity-rmVhXWA7.d.ts.map +1 -0
  129. package/dist/judge-calibration-DFtEMlde.d.ts +146 -0
  130. package/dist/judge-calibration-DFtEMlde.d.ts.map +1 -0
  131. package/dist/ledger-core/index.d.ts +2 -0
  132. package/dist/ledger-core/index.js +2 -0
  133. package/dist/ledger-core-DtZz1RG0.js +388 -0
  134. package/dist/ledger-core-DtZz1RG0.js.map +1 -0
  135. package/dist/llm-client--GR4JbZE.js +687 -0
  136. package/dist/llm-client--GR4JbZE.js.map +1 -0
  137. package/dist/llm-client-B_nIBlYo.d.ts +290 -0
  138. package/dist/llm-client-B_nIBlYo.d.ts.map +1 -0
  139. package/dist/matrix/index.d.ts +3 -155
  140. package/dist/matrix/index.js +2 -8
  141. package/dist/matrix-BzQnu2S6.js +270 -0
  142. package/dist/matrix-BzQnu2S6.js.map +1 -0
  143. package/dist/meta-eval/index.d.ts +4 -1027
  144. package/dist/meta-eval/index.js +393 -390
  145. package/dist/meta-eval/index.js.map +1 -1
  146. package/dist/metrics-C9YY1OcL.js +239 -0
  147. package/dist/metrics-C9YY1OcL.js.map +1 -0
  148. package/dist/mint-yN2M2eh0.js +201 -0
  149. package/dist/mint-yN2M2eh0.js.map +1 -0
  150. package/dist/multi-layer-verifier-BHY1gWAc.d.ts +138 -0
  151. package/dist/multi-layer-verifier-BHY1gWAc.d.ts.map +1 -0
  152. package/dist/multishot/index.d.ts +273 -480
  153. package/dist/multishot/index.d.ts.map +1 -0
  154. package/dist/multishot/index.js +595 -548
  155. package/dist/multishot/index.js.map +1 -1
  156. package/dist/off-policy-DvgzvtIx.js +220 -0
  157. package/dist/off-policy-DvgzvtIx.js.map +1 -0
  158. package/dist/off-policy-mskQw8Mb.d.ts +153 -0
  159. package/dist/off-policy-mskQw8Mb.d.ts.map +1 -0
  160. package/dist/openapi.json +1 -1
  161. package/dist/opencode-sqlite-BGrHeDu3.js +318 -0
  162. package/dist/opencode-sqlite-BGrHeDu3.js.map +1 -0
  163. package/dist/outcome-store-BYHIuO0e.d.ts +65 -0
  164. package/dist/outcome-store-BYHIuO0e.d.ts.map +1 -0
  165. package/dist/outcome-store-ChBKlTd_.js +75 -0
  166. package/dist/outcome-store-ChBKlTd_.js.map +1 -0
  167. package/dist/paired-arms-D9D0wXj2.js +260 -0
  168. package/dist/paired-arms-D9D0wXj2.js.map +1 -0
  169. package/dist/pipelines/index.d.ts +95 -532
  170. package/dist/pipelines/index.d.ts.map +1 -0
  171. package/dist/pipelines/index.js +497 -478
  172. package/dist/pipelines/index.js.map +1 -1
  173. package/dist/pre-registration-DakwTRXk.js +96 -0
  174. package/dist/pre-registration-DakwTRXk.js.map +1 -0
  175. package/dist/propose-review-control-LhIWGzHl.js +1458 -0
  176. package/dist/propose-review-control-LhIWGzHl.js.map +1 -0
  177. package/dist/query-CJ_DX8vl.d.ts +28 -0
  178. package/dist/query-CJ_DX8vl.d.ts.map +1 -0
  179. package/dist/query-Di7eEQ79.js +83 -0
  180. package/dist/query-Di7eEQ79.js.map +1 -0
  181. package/dist/raw-provider-sink-BQd7mzyT.js +201 -0
  182. package/dist/raw-provider-sink-BQd7mzyT.js.map +1 -0
  183. package/dist/raw-provider-sink-BU29Sh8h.d.ts +134 -0
  184. package/dist/raw-provider-sink-BU29Sh8h.d.ts.map +1 -0
  185. package/dist/redact-7Aq1ukl-.js +107 -0
  186. package/dist/redact-7Aq1ukl-.js.map +1 -0
  187. package/dist/release-report-Cz9NKH39.js +603 -0
  188. package/dist/release-report-Cz9NKH39.js.map +1 -0
  189. package/dist/release-report-mvB2G4_J.d.ts +244 -0
  190. package/dist/release-report-mvB2G4_J.d.ts.map +1 -0
  191. package/dist/replay-GnyotH0J.js +1741 -0
  192. package/dist/replay-GnyotH0J.js.map +1 -0
  193. package/dist/replay-OoidtG1E.d.ts +749 -0
  194. package/dist/replay-OoidtG1E.d.ts.map +1 -0
  195. package/dist/reporting.d.ts +6 -1312
  196. package/dist/reporting.js +6 -51
  197. package/dist/researcher-CwTdwXG1.d.ts +314 -0
  198. package/dist/researcher-CwTdwXG1.d.ts.map +1 -0
  199. package/dist/reward-hacking-eAnOsynk.d.ts +224 -0
  200. package/dist/reward-hacking-eAnOsynk.d.ts.map +1 -0
  201. package/dist/reward-hacking-qipEpKvY.js +596 -0
  202. package/dist/reward-hacking-qipEpKvY.js.map +1 -0
  203. package/dist/reward-nw2xZGZG.js +137 -0
  204. package/dist/reward-nw2xZGZG.js.map +1 -0
  205. package/dist/rl.d.ts +760 -4010
  206. package/dist/rl.d.ts.map +1 -0
  207. package/dist/rl.js +2325 -1958
  208. package/dist/rl.js.map +1 -1
  209. package/dist/rolldown-runtime-8H4AJuhK.js +14 -0
  210. package/dist/rollout/index.d.ts +3 -2087
  211. package/dist/rollout/index.js +8 -168
  212. package/dist/rollout-BOYjemfR.js +624 -0
  213. package/dist/rollout-BOYjemfR.js.map +1 -0
  214. package/dist/rubric-predictive-validity-B3xmbmS1.js +141 -0
  215. package/dist/rubric-predictive-validity-B3xmbmS1.js.map +1 -0
  216. package/dist/rubric-predictive-validity-Ku_clp_1.d.ts +74 -0
  217. package/dist/rubric-predictive-validity-Ku_clp_1.d.ts.map +1 -0
  218. package/dist/run-evidence-ClRX_8A9.d.ts +225 -0
  219. package/dist/run-evidence-ClRX_8A9.d.ts.map +1 -0
  220. package/dist/run-record-BuoE80Dq.js +467 -0
  221. package/dist/{chunk-56TAVBOK.js.map → run-record-BuoE80Dq.js.map} +1 -1
  222. package/dist/run-record-CnZu_gjl.d.ts +357 -0
  223. package/dist/run-record-CnZu_gjl.d.ts.map +1 -0
  224. package/dist/run-score-iEEAWiBY.js +41 -0
  225. package/dist/run-score-iEEAWiBY.js.map +1 -0
  226. package/dist/runtime-trajectory-1gyaTOoC.js +93 -0
  227. package/dist/runtime-trajectory-1gyaTOoC.js.map +1 -0
  228. package/dist/runtime-trajectory-BvSZcCHD.d.ts +50 -0
  229. package/dist/runtime-trajectory-BvSZcCHD.d.ts.map +1 -0
  230. package/dist/schema-BtVldJ3T.d.ts +206 -0
  231. package/dist/schema-BtVldJ3T.d.ts.map +1 -0
  232. package/dist/schema-C6DW4ZHR.js +821 -0
  233. package/dist/schema-C6DW4ZHR.js.map +1 -0
  234. package/dist/schema-CRhEY1SO.js +69 -0
  235. package/dist/schema-CRhEY1SO.js.map +1 -0
  236. package/dist/schema-Cef2cFmb.d.ts +408 -0
  237. package/dist/schema-Cef2cFmb.d.ts.map +1 -0
  238. package/dist/semantic-concept-judge-B6cWNJ2K.js +725 -0
  239. package/dist/semantic-concept-judge-B6cWNJ2K.js.map +1 -0
  240. package/dist/sequential-Br0mAPHA.js +148 -0
  241. package/dist/sequential-Br0mAPHA.js.map +1 -0
  242. package/dist/sequential-CYwq6Ff_.d.ts +141 -0
  243. package/dist/sequential-CYwq6Ff_.d.ts.map +1 -0
  244. package/dist/series-convergence-CjO2QdRW.js +43 -0
  245. package/dist/series-convergence-CjO2QdRW.js.map +1 -0
  246. package/dist/series-convergence-ofsqPWhs.d.ts +35 -0
  247. package/dist/series-convergence-ofsqPWhs.d.ts.map +1 -0
  248. package/dist/server-m5D9cvnG.js +1040 -0
  249. package/dist/server-m5D9cvnG.js.map +1 -0
  250. package/dist/skill-usage-C_pXm7lP.d.ts +534 -0
  251. package/dist/skill-usage-C_pXm7lP.d.ts.map +1 -0
  252. package/dist/skillopt-optimization-method-CUmHkGbI.js +7718 -0
  253. package/dist/skillopt-optimization-method-CUmHkGbI.js.map +1 -0
  254. package/dist/skillopt-optimization-method-CWKVTnks.d.ts +1740 -0
  255. package/dist/skillopt-optimization-method-CWKVTnks.d.ts.map +1 -0
  256. package/dist/statistics-Cmj6nynr.d.ts +514 -0
  257. package/dist/statistics-Cmj6nynr.d.ts.map +1 -0
  258. package/dist/statistics-CnnxdpOg.js +1437 -0
  259. package/dist/statistics-CnnxdpOg.js.map +1 -0
  260. package/dist/store-CT9YIIve.d.ts +117 -0
  261. package/dist/store-CT9YIIve.d.ts.map +1 -0
  262. package/dist/store-CxJry_cs.d.ts +229 -0
  263. package/dist/store-CxJry_cs.d.ts.map +1 -0
  264. package/dist/storyboard/index.d.ts +84 -203
  265. package/dist/storyboard/index.d.ts.map +1 -0
  266. package/dist/storyboard/index.js +609 -542
  267. package/dist/storyboard/index.js.map +1 -1
  268. package/dist/summary-report-BNs5nmXI.js +862 -0
  269. package/dist/summary-report-BNs5nmXI.js.map +1 -0
  270. package/dist/summary-report-Cj9gdw4i.d.ts +407 -0
  271. package/dist/summary-report-Cj9gdw4i.d.ts.map +1 -0
  272. package/dist/supervisor-run/index.d.ts +2 -959
  273. package/dist/supervisor-run/index.js +2 -65
  274. package/dist/supervisor-run-_lnTLM3z.js +1679 -0
  275. package/dist/supervisor-run-_lnTLM3z.js.map +1 -0
  276. package/dist/task-failure-attributes-CQZlB3et.js +311 -0
  277. package/dist/task-failure-attributes-CQZlB3et.js.map +1 -0
  278. package/dist/test-graded-scenario-BsqWLmPt.js +318 -0
  279. package/dist/test-graded-scenario-BsqWLmPt.js.map +1 -0
  280. package/dist/test-graded-scenario-D1TaI2va.d.ts +141 -0
  281. package/dist/test-graded-scenario-D1TaI2va.d.ts.map +1 -0
  282. package/dist/tools-BmuN627J.js +1085 -0
  283. package/dist/tools-BmuN627J.js.map +1 -0
  284. package/dist/trace-attributes.d.ts +2 -52
  285. package/dist/trace-attributes.js +131 -61
  286. package/dist/trace-attributes.js.map +1 -1
  287. package/dist/traces.d.ts +12 -2365
  288. package/dist/traces.js +12 -252
  289. package/dist/trajectory-D_7rLrvE.js +56 -0
  290. package/dist/trajectory-D_7rLrvE.js.map +1 -0
  291. package/dist/types-DGsxbAEd.d.ts +387 -0
  292. package/dist/types-DGsxbAEd.d.ts.map +1 -0
  293. package/dist/types-k9tZGKUg.d.ts +640 -0
  294. package/dist/types-k9tZGKUg.d.ts.map +1 -0
  295. package/dist/verdict-Dps8_okt.d.ts +37 -0
  296. package/dist/verdict-Dps8_okt.d.ts.map +1 -0
  297. package/dist/wire/index.d.ts +702 -1173
  298. package/dist/wire/index.d.ts.map +1 -0
  299. package/dist/wire/index.js +2 -81
  300. package/docs/campaign-proposers.md +1 -0
  301. package/package.json +17 -9
  302. package/dist/benchmarks/index.js.map +0 -1
  303. package/dist/campaign/index.js.map +0 -1
  304. package/dist/chunk-2QU3YOPR.js +0 -7374
  305. package/dist/chunk-2QU3YOPR.js.map +0 -1
  306. package/dist/chunk-3OCR4R5I.js +0 -728
  307. package/dist/chunk-3OCR4R5I.js.map +0 -1
  308. package/dist/chunk-3RF76KTD.js +0 -84
  309. package/dist/chunk-3RF76KTD.js.map +0 -1
  310. package/dist/chunk-56TAVBOK.js +0 -698
  311. package/dist/chunk-5DTSBUL2.js +0 -159
  312. package/dist/chunk-5DTSBUL2.js.map +0 -1
  313. package/dist/chunk-7FO3TNPI.js +0 -232
  314. package/dist/chunk-7FO3TNPI.js.map +0 -1
  315. package/dist/chunk-7ZZMD7UK.js +0 -386
  316. package/dist/chunk-7ZZMD7UK.js.map +0 -1
  317. package/dist/chunk-BOD4O7OF.js +0 -40
  318. package/dist/chunk-BOD4O7OF.js.map +0 -1
  319. package/dist/chunk-BSO5JDQH.js +0 -2335
  320. package/dist/chunk-BSO5JDQH.js.map +0 -1
  321. package/dist/chunk-C6LXANRU.js +0 -1550
  322. package/dist/chunk-C6LXANRU.js.map +0 -1
  323. package/dist/chunk-DODXQREJ.js +0 -752
  324. package/dist/chunk-DODXQREJ.js.map +0 -1
  325. package/dist/chunk-DRYIUNWY.js +0 -622
  326. package/dist/chunk-DRYIUNWY.js.map +0 -1
  327. package/dist/chunk-E7QXT7SX.js +0 -183
  328. package/dist/chunk-E7QXT7SX.js.map +0 -1
  329. package/dist/chunk-EG66UGL4.js +0 -341
  330. package/dist/chunk-EG66UGL4.js.map +0 -1
  331. package/dist/chunk-FXTVJPYD.js +0 -576
  332. package/dist/chunk-FXTVJPYD.js.map +0 -1
  333. package/dist/chunk-G7MGMCZD.js +0 -153
  334. package/dist/chunk-G7MGMCZD.js.map +0 -1
  335. package/dist/chunk-GGE4NNQT.js +0 -65
  336. package/dist/chunk-GGE4NNQT.js.map +0 -1
  337. package/dist/chunk-H23X7XKK.js +0 -181
  338. package/dist/chunk-H23X7XKK.js.map +0 -1
  339. package/dist/chunk-HHWE3POT.js +0 -94
  340. package/dist/chunk-HHWE3POT.js.map +0 -1
  341. package/dist/chunk-HPWUNB47.js +0 -289
  342. package/dist/chunk-HPWUNB47.js.map +0 -1
  343. package/dist/chunk-IYCLP2N2.js +0 -766
  344. package/dist/chunk-IYCLP2N2.js.map +0 -1
  345. package/dist/chunk-JHCHEVET.js +0 -274
  346. package/dist/chunk-JHCHEVET.js.map +0 -1
  347. package/dist/chunk-JQSF5DQT.js +0 -701
  348. package/dist/chunk-JQSF5DQT.js.map +0 -1
  349. package/dist/chunk-K4DBDHLK.js +0 -158
  350. package/dist/chunk-K4DBDHLK.js.map +0 -1
  351. package/dist/chunk-K6N6XJJX.js +0 -306
  352. package/dist/chunk-K6N6XJJX.js.map +0 -1
  353. package/dist/chunk-M4YBQKIJ.js +0 -1040
  354. package/dist/chunk-M4YBQKIJ.js.map +0 -1
  355. package/dist/chunk-MA6HLL3S.js +0 -65
  356. package/dist/chunk-MA6HLL3S.js.map +0 -1
  357. package/dist/chunk-MAZ26DC7.js +0 -99
  358. package/dist/chunk-MAZ26DC7.js.map +0 -1
  359. package/dist/chunk-NPCTHQIO.js +0 -91
  360. package/dist/chunk-NPCTHQIO.js.map +0 -1
  361. package/dist/chunk-NY44NC4A.js +0 -1056
  362. package/dist/chunk-NY44NC4A.js.map +0 -1
  363. package/dist/chunk-OIUOT4QD.js +0 -44
  364. package/dist/chunk-OIUOT4QD.js.map +0 -1
  365. package/dist/chunk-ONWEPEDO.js +0 -57
  366. package/dist/chunk-ONWEPEDO.js.map +0 -1
  367. package/dist/chunk-OWN5NPMC.js +0 -152
  368. package/dist/chunk-OWN5NPMC.js.map +0 -1
  369. package/dist/chunk-P6FYH6K4.js +0 -1161
  370. package/dist/chunk-P6FYH6K4.js.map +0 -1
  371. package/dist/chunk-PC4UYEBM.js +0 -166
  372. package/dist/chunk-PC4UYEBM.js.map +0 -1
  373. package/dist/chunk-PC5DOSM7.js +0 -579
  374. package/dist/chunk-PC5DOSM7.js.map +0 -1
  375. package/dist/chunk-PXE2VKMX.js +0 -140
  376. package/dist/chunk-PXE2VKMX.js.map +0 -1
  377. package/dist/chunk-PZ5AY32C.js +0 -10
  378. package/dist/chunk-PZ5AY32C.js.map +0 -1
  379. package/dist/chunk-QB6BDBP2.js +0 -4464
  380. package/dist/chunk-QB6BDBP2.js.map +0 -1
  381. package/dist/chunk-RXHCETDZ.js +0 -536
  382. package/dist/chunk-RXHCETDZ.js.map +0 -1
  383. package/dist/chunk-RZTMDUO7.js +0 -49
  384. package/dist/chunk-RZTMDUO7.js.map +0 -1
  385. package/dist/chunk-SFLLL76A.js +0 -669
  386. package/dist/chunk-SFLLL76A.js.map +0 -1
  387. package/dist/chunk-SZLVEKMJ.js +0 -1446
  388. package/dist/chunk-SZLVEKMJ.js.map +0 -1
  389. package/dist/chunk-T4SQEITX.js +0 -95
  390. package/dist/chunk-T4SQEITX.js.map +0 -1
  391. package/dist/chunk-T6RLYGAD.js +0 -158
  392. package/dist/chunk-T6RLYGAD.js.map +0 -1
  393. package/dist/chunk-TJVT4QFF.js +0 -911
  394. package/dist/chunk-TJVT4QFF.js.map +0 -1
  395. package/dist/chunk-TQ7LNKZ3.js +0 -136
  396. package/dist/chunk-TQ7LNKZ3.js.map +0 -1
  397. package/dist/chunk-U4L7JRPZ.js +0 -1706
  398. package/dist/chunk-U4L7JRPZ.js.map +0 -1
  399. package/dist/chunk-U4PHLT2N.js +0 -419
  400. package/dist/chunk-U4PHLT2N.js.map +0 -1
  401. package/dist/chunk-VCZ5FQYW.js +0 -928
  402. package/dist/chunk-VCZ5FQYW.js.map +0 -1
  403. package/dist/chunk-VI2UW6B6.js +0 -162
  404. package/dist/chunk-VI2UW6B6.js.map +0 -1
  405. package/dist/chunk-VQMK5FMP.js +0 -247
  406. package/dist/chunk-VQMK5FMP.js.map +0 -1
  407. package/dist/chunk-WGXIEX7P.js +0 -116
  408. package/dist/chunk-WGXIEX7P.js.map +0 -1
  409. package/dist/chunk-WVATSFCP.js +0 -1553
  410. package/dist/chunk-WVATSFCP.js.map +0 -1
  411. package/dist/chunk-X4YIBDER.js +0 -1662
  412. package/dist/chunk-X4YIBDER.js.map +0 -1
  413. package/dist/chunk-YQN4ICPP.js +0 -355
  414. package/dist/chunk-YQN4ICPP.js.map +0 -1
  415. package/dist/chunk-ZET2UAYW.js +0 -89
  416. package/dist/chunk-ZET2UAYW.js.map +0 -1
  417. package/dist/chunk-ZHTZ4EYI.js +0 -1212
  418. package/dist/chunk-ZHTZ4EYI.js.map +0 -1
  419. package/dist/control.js.map +0 -1
  420. package/dist/hosted/index.js.map +0 -1
  421. package/dist/matrix/index.js.map +0 -1
  422. package/dist/reporting.js.map +0 -1
  423. package/dist/rollout/index.js.map +0 -1
  424. package/dist/run-campaign-OJJ7CZF4.js +0 -18
  425. package/dist/run-campaign-OJJ7CZF4.js.map +0 -1
  426. package/dist/supervisor-run/index.js.map +0 -1
  427. package/dist/traces.js.map +0 -1
  428. package/dist/wire/index.js.map +0 -1
@@ -0,0 +1,801 @@
1
+ import { s as ValidationError } from "./errors-8YnH8WlF.js";
2
+ import { i as estimateCost, o as isModelPriced, s as resolveModelPricing } from "./metrics-C9YY1OcL.js";
3
+ import { z } from "zod";
4
+ //#region src/cost-ledger.ts
5
+ var CostCeilingReachedError = class extends ValidationError {
6
+ constructor(ceilingUsd, committedAndReservedUsd, requestedUsd, phase, actor) {
7
+ super(`CostLedger: reserving ${requestedUsd} for '${actor}' during '${phase}' would exceed ceiling ${ceilingUsd} with ${committedAndReservedUsd} already committed or reserved`);
8
+ }
9
+ };
10
+ var CostAccountingIncompleteError = class extends ValidationError {};
11
+ var CostReservationExceededError = class extends ValidationError {
12
+ constructor(actor, actualUsd, maximumUsd) {
13
+ super(`CostLedger: '${actor}' charged ${actualUsd}, exceeding its enforced maximum ${maximumUsd}`);
14
+ }
15
+ };
16
+ var CostCallConflictError = class extends ValidationError {
17
+ callId;
18
+ receipt;
19
+ constructor(message, options = {}) {
20
+ super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
21
+ this.callId = options.callId;
22
+ this.receipt = options.receipt ? cloneReceipt(options.receipt) : void 0;
23
+ }
24
+ };
25
+ var CostLedgerPersistenceError = class extends ValidationError {
26
+ callId;
27
+ receipt;
28
+ constructor(cause, callId, receipt) {
29
+ super(`CostLedger: failed to persist${callId ? ` call '${callId}'` : ""}: ${toError(cause).message}`, { cause });
30
+ this.callId = callId;
31
+ this.receipt = receipt ? cloneReceipt(receipt) : void 0;
32
+ }
33
+ };
34
+ var CostReceiptCaptureError = class extends ValidationError {
35
+ callId;
36
+ receipt;
37
+ receiptError;
38
+ constructor(callId, cause, receiptError, receipt) {
39
+ super(`CostLedger: could not capture the provider receipt for call '${callId}'`, { cause });
40
+ this.callId = callId;
41
+ this.receipt = receipt ? cloneReceipt(receipt) : void 0;
42
+ this.receiptError = toError(receiptError);
43
+ }
44
+ };
45
+ /** Run-wide paid-call admission, durable call state, receipts, and summaries. */
46
+ var CostLedger = class {
47
+ records = /* @__PURE__ */ new Map();
48
+ activeCallIds = /* @__PURE__ */ new Set();
49
+ lateCallIds = /* @__PURE__ */ new Set();
50
+ idleWaiters = /* @__PURE__ */ new Set();
51
+ completedTasks = 0;
52
+ revision = "memory";
53
+ costLimitPersisted = false;
54
+ costCeilingUsd;
55
+ persistence;
56
+ constructor(input) {
57
+ const options = typeof input === "number" ? { costCeilingUsd: input } : input ?? {};
58
+ this.persistence = options.persistence;
59
+ if (options.costCeilingUsd !== void 0) assertNonNegative(options.costCeilingUsd, "costCeilingUsd");
60
+ let persistedCostCeilingUsd;
61
+ if (this.persistence) {
62
+ let stored;
63
+ try {
64
+ stored = this.persistence.read();
65
+ } catch (cause) {
66
+ throw new CostLedgerPersistenceError(cause);
67
+ }
68
+ assertString(stored.revision, "persistence revision");
69
+ this.revision = stored.revision;
70
+ const restored = parseEvents(stored.events);
71
+ for (const record of restored.records) this.records.set(record.callId, record);
72
+ this.completedTasks = restored.completedTasks;
73
+ persistedCostCeilingUsd = restored.costCeilingUsd;
74
+ }
75
+ if (options.costCeilingUsd !== void 0 && persistedCostCeilingUsd !== void 0 && options.costCeilingUsd !== persistedCostCeilingUsd) throw new ValidationError(`CostLedger: requested cost ceiling ${options.costCeilingUsd} does not match persisted ceiling ${persistedCostCeilingUsd}`);
76
+ this.costCeilingUsd = persistedCostCeilingUsd ?? options.costCeilingUsd;
77
+ this.costLimitPersisted = !this.persistence || this.costCeilingUsd === void 0 || persistedCostCeilingUsd !== void 0;
78
+ if (this.costCeilingUsd !== void 0 && [...this.records.values()].some((record) => record.status === "pending" && record.maximumCostUsd === void 0)) throw new ValidationError("CostLedger: capped event log contains an unbounded pending call");
79
+ if (options.receipts?.length) {
80
+ const imported = options.receipts.map((receipt, index) => parseImportedReceipt(receipt, `imported receipt ${index + 1}`));
81
+ this.ensureCostLimitPersisted();
82
+ for (const receipt of imported) this.appendRecord(receipt);
83
+ }
84
+ }
85
+ async runPaidCall(input) {
86
+ let callId;
87
+ let pending;
88
+ try {
89
+ callId = resolveCallId(input.callId);
90
+ validateAttribution(input);
91
+ if (input.signal?.aborted) return {
92
+ succeeded: false,
93
+ callId,
94
+ error: abortError(input.signal)
95
+ };
96
+ if (this.records.has(callId)) return {
97
+ succeeded: false,
98
+ callId,
99
+ error: new CostCallConflictError(`CostLedger: callId '${callId}' already exists`)
100
+ };
101
+ this.ensureCostLimitPersisted(callId);
102
+ const summary = this.summary();
103
+ if (summary.unresolvedCalls > 0) return {
104
+ succeeded: false,
105
+ callId,
106
+ error: new CostAccountingIncompleteError(`CostLedger: ${summary.unresolvedCalls} unresolved call(s) must be reconciled before new paid work`)
107
+ };
108
+ const maximumCostUsd = this.resolveMaximum(input.maximumCharge);
109
+ if (this.costCeilingUsd !== void 0 && this.hasIncompleteSettledCall()) return {
110
+ succeeded: false,
111
+ callId,
112
+ error: new CostAccountingIncompleteError(`CostLedger: accounting is incomplete; refusing paid call '${input.actor}' during '${input.phase}'`)
113
+ };
114
+ if (this.costCeilingUsd !== void 0) {
115
+ const committedAndReserved = summary.totalCostUsd + summary.reservedCostUsd;
116
+ if (committedAndReserved + maximumCostUsd > this.costCeilingUsd) return {
117
+ succeeded: false,
118
+ callId,
119
+ error: new CostCeilingReachedError(this.costCeilingUsd, committedAndReserved, maximumCostUsd, input.phase, input.actor)
120
+ };
121
+ }
122
+ pending = {
123
+ status: "pending",
124
+ callId,
125
+ channel: input.channel,
126
+ phase: input.phase,
127
+ actor: input.actor,
128
+ model: pendingModel(input),
129
+ ...maximumCostUsd === void 0 ? {} : { maximumCostUsd },
130
+ ...input.tags ? { tags: { ...input.tags } } : {},
131
+ timestamp: Date.now()
132
+ };
133
+ this.appendRecord(pending);
134
+ this.activeCallIds.add(callId);
135
+ } catch (error) {
136
+ return {
137
+ succeeded: false,
138
+ ...callId ? { callId } : {},
139
+ error: toError(error)
140
+ };
141
+ }
142
+ try {
143
+ return await this.execute(input, pending);
144
+ } catch (error) {
145
+ return {
146
+ succeeded: false,
147
+ callId: pending.callId,
148
+ error: toError(error)
149
+ };
150
+ } finally {
151
+ if (!this.lateCallIds.has(pending.callId)) this.releaseActiveCall(pending.callId);
152
+ }
153
+ }
154
+ /** Wait until every call started by this ledger has produced a durable outcome. */
155
+ async waitForIdle(options = {}) {
156
+ const hasActiveMatch = () => [...this.activeCallIds].some((callId) => {
157
+ const record = this.records.get(callId);
158
+ return record !== void 0 && matches(record, options.filter);
159
+ });
160
+ if (!hasActiveMatch()) return true;
161
+ const timeoutMs = options.timeoutMs ?? 5e3;
162
+ assertTimeout(timeoutMs, "waitForIdle.timeoutMs");
163
+ return new Promise((resolve) => {
164
+ let timer;
165
+ const finish = (settled) => {
166
+ this.idleWaiters.delete(onIdle);
167
+ if (timer !== void 0) clearTimeout(timer);
168
+ resolve(settled);
169
+ };
170
+ const onIdle = () => {
171
+ if (!hasActiveMatch()) finish(true);
172
+ };
173
+ this.idleWaiters.add(onIdle);
174
+ timer = setTimeout(() => finish(false), timeoutMs);
175
+ onIdle();
176
+ });
177
+ }
178
+ /** Settle a call left pending by a crashed process after reconciling with the provider. */
179
+ reconcile(callId, observed, options = {}) {
180
+ const pending = [...this.records.values()].find((record) => record.callId === callId && record.status === "pending");
181
+ if (!pending) throw new CostCallConflictError(`CostLedger: no pending call '${callId}'`);
182
+ if (this.activeCallIds.has(callId)) throw new CostCallConflictError(`CostLedger: call '${callId}' is still active`);
183
+ this.ensureCostLimitPersisted(callId);
184
+ return this.commitReceipt(pending, observed, options.error);
185
+ }
186
+ list(filter) {
187
+ return [...this.records.values()].filter((record) => record.status === "settled").filter((receipt) => matches(receipt, filter)).map(cloneReceipt);
188
+ }
189
+ /** Read pending calls without exposing mutable ledger state. */
190
+ listPending(filter) {
191
+ return [...this.records.values()].filter((record) => record.status === "pending").filter((record) => matches(record, filter)).map((record) => ({
192
+ ...clonePendingCall(record),
193
+ state: this.lateCallIds.has(record.callId) ? "late" : this.activeCallIds.has(record.callId) ? "active" : "interrupted"
194
+ }));
195
+ }
196
+ summary(filter) {
197
+ const records = [...this.records.values()].filter((record) => matches(record, filter));
198
+ const pending = records.filter((record) => record.status === "pending");
199
+ const receipts = records.filter((record) => record.status === "settled");
200
+ const byChannel = /* @__PURE__ */ new Map();
201
+ const unpriced = /* @__PURE__ */ new Set();
202
+ const incompleteReasons = pending.map((record) => `call '${record.callId}' for '${record.actor}' is pending`);
203
+ let inputTokens = 0;
204
+ let outputTokens = 0;
205
+ let reasoningTokens = 0;
206
+ let cachedTokens = 0;
207
+ let cacheWriteTokens = 0;
208
+ let totalCostUsd = 0;
209
+ for (const receipt of receipts) {
210
+ inputTokens += receipt.inputTokens;
211
+ outputTokens += receipt.outputTokens;
212
+ reasoningTokens += receipt.reasoningTokens ?? 0;
213
+ cachedTokens += receipt.cachedTokens ?? 0;
214
+ cacheWriteTokens += receipt.cacheWriteTokens ?? 0;
215
+ totalCostUsd += receipt.costUsd;
216
+ if (receipt.costUnknown) {
217
+ unpriced.add(receipt.model);
218
+ incompleteReasons.push(receipt.error ?? `cost unknown for '${receipt.actor}' using '${receipt.model}'`);
219
+ }
220
+ if (receipt.usageUnknown) incompleteReasons.push(`token usage unknown for '${receipt.actor}' using '${receipt.model}'`);
221
+ if (receipt.maximumCostUsd !== void 0 && receipt.costUsd > receipt.maximumCostUsd) incompleteReasons.push(`'${receipt.actor}' charged ${receipt.costUsd}, exceeding its enforced maximum ${receipt.maximumCostUsd}`);
222
+ const rollup = byChannel.get(receipt.channel) ?? emptyRollup(receipt.channel);
223
+ rollup.calls += 1;
224
+ rollup.inputTokens += receipt.inputTokens;
225
+ rollup.outputTokens += receipt.outputTokens;
226
+ rollup.reasoningTokens = (rollup.reasoningTokens ?? 0) + (receipt.reasoningTokens ?? 0);
227
+ rollup.cachedTokens += receipt.cachedTokens ?? 0;
228
+ rollup.cacheWriteTokens = (rollup.cacheWriteTokens ?? 0) + (receipt.cacheWriteTokens ?? 0);
229
+ rollup.costUsd += receipt.costUsd;
230
+ if (receipt.costUnknown) rollup.unpricedCalls += 1;
231
+ if (receipt.usageUnknown) rollup.unknownUsageCalls += 1;
232
+ byChannel.set(receipt.channel, rollup);
233
+ }
234
+ return {
235
+ totalCalls: receipts.length,
236
+ pendingCalls: pending.length,
237
+ unresolvedCalls: pending.filter((record) => !this.activeCallIds.has(record.callId) || this.lateCallIds.has(record.callId)).length,
238
+ reservedCostUsd: pending.reduce((sum, record) => sum + (record.maximumCostUsd ?? 0), 0),
239
+ inputTokens,
240
+ outputTokens,
241
+ reasoningTokens,
242
+ cachedTokens,
243
+ cacheWriteTokens,
244
+ totalCostUsd,
245
+ byChannel: [...byChannel.values()].sort((a, b) => a.channel.localeCompare(b.channel)),
246
+ unpricedModels: [...unpriced].sort(),
247
+ fullyPriced: unpriced.size === 0,
248
+ usageComplete: pending.length === 0 && receipts.every((receipt) => !receipt.usageUnknown),
249
+ accountingComplete: incompleteReasons.length === 0,
250
+ incompleteReasons: [...new Set(incompleteReasons)]
251
+ };
252
+ }
253
+ markCompleted(count = 1) {
254
+ if (!Number.isInteger(count) || count < 0) throw new ValidationError(`CostLedger.markCompleted: count must be a non-negative integer, got ${count}`);
255
+ if (count === 0) return;
256
+ this.ensureCostLimitPersisted();
257
+ this.appendEvent({
258
+ version: 1,
259
+ completedTasks: count
260
+ });
261
+ this.completedTasks += count;
262
+ }
263
+ costPerCompletedTask() {
264
+ return this.completedTasks === 0 ? null : this.summary().totalCostUsd / this.completedTasks;
265
+ }
266
+ async execute(input, pending) {
267
+ const signal = input.signal ?? new AbortController().signal;
268
+ if (signal.aborted) return this.commitOutcome(pending, abortError(signal), {
269
+ model: pending.model,
270
+ inputTokens: 0,
271
+ outputTokens: 0,
272
+ actualCostUsd: 0
273
+ });
274
+ const operation = Promise.resolve().then(() => input.execute(signal, pending.callId));
275
+ const settled = await settle(operation, signal);
276
+ if (settled.kind === "aborted") {
277
+ this.lateCallIds.add(pending.callId);
278
+ this.captureLateOutcome(input, pending, operation);
279
+ return paidFailure(pending.callId, abortError(signal));
280
+ }
281
+ if (settled.kind === "error") {
282
+ let observed;
283
+ try {
284
+ observed = input.receiptFromError?.(settled.error);
285
+ } catch (receiptError) {
286
+ return this.captureFailure(pending, settled.error, receiptError);
287
+ }
288
+ return this.commitOutcome(pending, settled.error, observed ?? unknownReceipt(pending.model));
289
+ }
290
+ try {
291
+ const receipt = this.commitReceipt(pending, input.receipt(settled.value));
292
+ if (receipt.maximumCostUsd !== void 0 && receipt.costUsd > receipt.maximumCostUsd) return paidFailure(pending.callId, new CostReservationExceededError(pending.actor, receipt.costUsd, receipt.maximumCostUsd), receipt);
293
+ return {
294
+ succeeded: true,
295
+ callId: pending.callId,
296
+ value: settled.value,
297
+ receipt
298
+ };
299
+ } catch (receiptError) {
300
+ if (receiptError instanceof CostLedgerPersistenceError || receiptError instanceof CostCallConflictError) return paidFailure(pending.callId, receiptError);
301
+ return this.captureFailure(pending, receiptError, receiptError);
302
+ }
303
+ }
304
+ captureLateOutcome(input, pending, operation) {
305
+ operation.then((value) => {
306
+ if (this.records.get(pending.callId)?.status !== "pending") return;
307
+ try {
308
+ this.commitReceipt(pending, input.receipt(value));
309
+ } catch {}
310
+ }, (cause) => {
311
+ if (this.records.get(pending.callId)?.status !== "pending") return;
312
+ const error = toError(cause);
313
+ try {
314
+ const observed = input.receiptFromError?.(error);
315
+ this.commitReceipt(pending, observed ?? unknownReceipt(pending.model), error.message);
316
+ } catch (receiptError) {
317
+ if (receiptError instanceof CostLedgerPersistenceError || receiptError instanceof CostCallConflictError) return;
318
+ this.captureFailure(pending, error, receiptError);
319
+ }
320
+ }).finally(() => {
321
+ this.lateCallIds.delete(pending.callId);
322
+ this.releaseActiveCall(pending.callId);
323
+ });
324
+ }
325
+ releaseActiveCall(callId) {
326
+ this.activeCallIds.delete(callId);
327
+ for (const resolve of [...this.idleWaiters]) resolve();
328
+ }
329
+ commitOutcome(pending, error, observed) {
330
+ try {
331
+ const receipt = this.commitReceipt(pending, observed, error.message);
332
+ return paidFailure(pending.callId, error, receipt);
333
+ } catch (receiptError) {
334
+ if (receiptError instanceof CostLedgerPersistenceError || receiptError instanceof CostCallConflictError) return paidFailure(pending.callId, receiptError);
335
+ return this.captureFailure(pending, error, receiptError);
336
+ }
337
+ }
338
+ captureFailure(pending, cause, receiptError) {
339
+ try {
340
+ const receipt = this.commitReceipt(pending, unknownReceipt(pending.model), toError(receiptError).message);
341
+ return paidFailure(pending.callId, new CostReceiptCaptureError(pending.callId, cause, receiptError, receipt), receipt);
342
+ } catch (error) {
343
+ const typed = error instanceof Error ? error : toError(error);
344
+ return paidFailure(pending.callId, typed);
345
+ }
346
+ }
347
+ commitReceipt(pending, observed, error) {
348
+ const receipt = buildReceipt(pending, observed, error);
349
+ if (this.records.get(pending.callId)?.status !== "pending") throw new CostCallConflictError(`CostLedger: call '${pending.callId}' is not pending`);
350
+ this.appendRecord(receipt);
351
+ return cloneReceipt(receipt);
352
+ }
353
+ resolveMaximum(maximum) {
354
+ if (!maximum) {
355
+ if (this.costCeilingUsd !== void 0) throw new CostAccountingIncompleteError("CostLedger: capped paid calls require a hard maximumCharge before execution");
356
+ return;
357
+ }
358
+ if ("externallyEnforcedMaximumUsd" in maximum) {
359
+ assertNonNegative(maximum.externallyEnforcedMaximumUsd, "maximumCharge.externallyEnforcedMaximumUsd");
360
+ return maximum.externallyEnforcedMaximumUsd;
361
+ }
362
+ if ("customTokenPricing" in maximum) return costForTokenPricing(maximum.customTokenPricing, maximum);
363
+ const priced = costForUsage(maximum.model, maximum);
364
+ if (priced.costUnknown) {
365
+ if (this.costCeilingUsd !== void 0) throw new CostAccountingIncompleteError(`CostLedger: cannot reserve unpriced model '${maximum.model}' in a capped run`);
366
+ return;
367
+ }
368
+ return priced.costUsd;
369
+ }
370
+ hasIncompleteSettledCall() {
371
+ return [...this.records.values()].some((record) => record.status === "settled" && (record.costUnknown || record.usageUnknown || record.maximumCostUsd !== void 0 && record.costUsd > record.maximumCostUsd));
372
+ }
373
+ appendRecord(record) {
374
+ const callId = record.callId;
375
+ const receipt = record.status === "settled" ? record : void 0;
376
+ validateTransition(this.records, record);
377
+ const event = record.status === "settled" ? {
378
+ version: 2,
379
+ record: cloneReceipt(record)
380
+ } : {
381
+ version: 1,
382
+ record: clonePendingCall(record)
383
+ };
384
+ this.appendEvent(event, callId, receipt);
385
+ this.records.set(callId, cloneRecord(record));
386
+ }
387
+ ensureCostLimitPersisted(callId) {
388
+ if (this.costLimitPersisted || this.costCeilingUsd === void 0) return;
389
+ this.appendEvent({
390
+ version: 1,
391
+ costCeilingUsd: this.costCeilingUsd
392
+ }, callId);
393
+ this.costLimitPersisted = true;
394
+ }
395
+ appendEvent(event, callId, receipt) {
396
+ try {
397
+ if (this.persistence) {
398
+ const nextRevision = this.persistence.append(this.revision, `${JSON.stringify(event)}\n`);
399
+ if (nextRevision === void 0) throw new CostCallConflictError(`CostLedger: persisted revision changed while writing call '${callId}'`, {
400
+ callId,
401
+ receipt
402
+ });
403
+ assertString(nextRevision, "persistence revision");
404
+ this.revision = nextRevision;
405
+ }
406
+ } catch (cause) {
407
+ if (cause instanceof CostCallConflictError) throw cause;
408
+ throw new CostLedgerPersistenceError(cause, callId, receipt);
409
+ }
410
+ }
411
+ };
412
+ /** Return the canonical pricing-table key, or null when the model is unpriced. */
413
+ function modelPriceKey(model) {
414
+ return isModelPriced(model) ? model : null;
415
+ }
416
+ function costForUsage(model, usage) {
417
+ assertUsage(usage);
418
+ if (!resolveModelPricing(model)) return {
419
+ costUsd: 0,
420
+ costUnknown: true
421
+ };
422
+ return {
423
+ costUsd: estimateCost(usage.inputTokens + (usage.cachedTokens ?? 0) + (usage.cacheWriteTokens ?? 0), usage.outputTokens, model),
424
+ costUnknown: false
425
+ };
426
+ }
427
+ /** Price token counts with caller-supplied per-million rates. */
428
+ function costForTokenPricing(pricing, usage) {
429
+ assertTokenCount(usage.inputTokens, "usage.inputTokens");
430
+ assertTokenCount(usage.outputTokens, "usage.outputTokens");
431
+ if (usage.cachedTokens !== void 0) assertTokenCount(usage.cachedTokens, "usage.cachedTokens");
432
+ if (usage.cacheWriteTokens !== void 0) assertTokenCount(usage.cacheWriteTokens, "usage.cacheWriteTokens");
433
+ assertNonNegative(pricing.inputUsdPerMillion, "pricing.inputUsdPerMillion");
434
+ if (pricing.cachedInputUsdPerMillion !== void 0) assertNonNegative(pricing.cachedInputUsdPerMillion, "pricing.cachedInputUsdPerMillion");
435
+ if (pricing.cacheWriteUsdPerMillion !== void 0) assertNonNegative(pricing.cacheWriteUsdPerMillion, "pricing.cacheWriteUsdPerMillion");
436
+ assertNonNegative(pricing.outputUsdPerMillion, "pricing.outputUsdPerMillion");
437
+ const cachedInputRate = pricing.cachedInputUsdPerMillion ?? pricing.inputUsdPerMillion;
438
+ const cacheWriteRate = pricing.cacheWriteUsdPerMillion ?? pricing.inputUsdPerMillion;
439
+ return usage.inputTokens / 1e6 * pricing.inputUsdPerMillion + (usage.cachedTokens ?? 0) / 1e6 * cachedInputRate + (usage.cacheWriteTokens ?? 0) / 1e6 * cacheWriteRate + usage.outputTokens / 1e6 * pricing.outputUsdPerMillion;
440
+ }
441
+ async function settle(promise, signal) {
442
+ return await new Promise((resolve) => {
443
+ let done = false;
444
+ const finish = (value) => {
445
+ if (done) return;
446
+ done = true;
447
+ signal.removeEventListener("abort", onAbort);
448
+ resolve(value);
449
+ };
450
+ const onAbort = () => finish({ kind: "aborted" });
451
+ signal.addEventListener("abort", onAbort, { once: true });
452
+ if (signal.aborted) onAbort();
453
+ promise.then((value) => finish({
454
+ kind: "value",
455
+ value
456
+ }), (error) => finish({
457
+ kind: "error",
458
+ error: toError(error)
459
+ }));
460
+ });
461
+ }
462
+ function buildReceipt(pending, observed, error) {
463
+ assertUsage(observed);
464
+ assertString(observed.model, "receipt.model");
465
+ if (observed.actualCostUsd !== void 0 && observed.customTokenPricing !== void 0) throw new ValidationError("CostLedger: a receipt cannot have both actualCostUsd and customTokenPricing");
466
+ const customPricingSnapshot = observed.customTokenPricing === void 0 ? void 0 : pricingSnapshot(observed.customTokenPricing);
467
+ const estimated = customPricingSnapshot ? {
468
+ costUsd: costFromPricing(observed, customPricingSnapshot),
469
+ costUnknown: false
470
+ } : costForUsage(observed.model, observed);
471
+ const hasActual = observed.actualCostUsd !== void 0;
472
+ if (hasActual && observed.costUnknown === true) throw new ValidationError("CostLedger: a receipt cannot have both actualCostUsd and costUnknown=true");
473
+ if (hasActual) assertNonNegative(observed.actualCostUsd, "actualCostUsd");
474
+ const usageUnknown = observed.usageUnknown === true;
475
+ const costUnknown = observed.costUnknown === true || !hasActual && (usageUnknown || estimated.costUnknown);
476
+ const resolvedModelPricing = !customPricingSnapshot && !hasActual && !costUnknown ? resolveModelPricing(observed.model) : null;
477
+ const receiptPricing = !hasActual && !costUnknown ? customPricingSnapshot ?? (resolvedModelPricing ? {
478
+ inputUsdPerThousand: resolvedModelPricing.input,
479
+ outputUsdPerThousand: resolvedModelPricing.output
480
+ } : void 0) : void 0;
481
+ return parseReceipt({
482
+ status: "settled",
483
+ callId: pending.callId,
484
+ channel: pending.channel,
485
+ phase: pending.phase,
486
+ actor: pending.actor,
487
+ model: observed.model,
488
+ inputTokens: observed.inputTokens,
489
+ outputTokens: observed.outputTokens,
490
+ ...observed.reasoningTokens === void 0 ? {} : { reasoningTokens: observed.reasoningTokens },
491
+ ...observed.cachedTokens === void 0 ? {} : { cachedTokens: observed.cachedTokens },
492
+ ...observed.cacheWriteTokens === void 0 ? {} : { cacheWriteTokens: observed.cacheWriteTokens },
493
+ costUsd: costUnknown ? 0 : hasActual ? observed.actualCostUsd : estimated.costUsd,
494
+ costUnknown,
495
+ usageUnknown,
496
+ ...receiptPricing ? { pricing: receiptPricing } : {},
497
+ ...hasActual ? { actualCostUsd: observed.actualCostUsd } : {},
498
+ ...pending.maximumCostUsd === void 0 ? {} : { maximumCostUsd: pending.maximumCostUsd },
499
+ ...error ? { error } : {},
500
+ ...pending.tags ? { tags: { ...pending.tags } } : {},
501
+ timestamp: pending.timestamp
502
+ }, "provider receipt");
503
+ }
504
+ const NonEmptyString = z.string().refine((value) => value.trim().length > 0, "must be non-empty");
505
+ const TokenCount = z.number().int().nonnegative().finite();
506
+ const NonNegative = z.number().nonnegative().finite();
507
+ const Tags = z.record(NonEmptyString, z.string());
508
+ const CostPricingSchema = z.strictObject({
509
+ inputUsdPerThousand: NonNegative,
510
+ cachedInputUsdPerThousand: NonNegative.optional(),
511
+ cacheWriteUsdPerThousand: NonNegative.optional(),
512
+ outputUsdPerThousand: NonNegative
513
+ });
514
+ const CostCallBaseShape = {
515
+ callId: NonEmptyString,
516
+ channel: NonEmptyString,
517
+ phase: NonEmptyString,
518
+ actor: NonEmptyString,
519
+ model: NonEmptyString,
520
+ maximumCostUsd: NonNegative.optional(),
521
+ tags: Tags.optional(),
522
+ timestamp: NonNegative
523
+ };
524
+ const PendingCostCallSchema = z.strictObject({
525
+ status: z.literal("pending"),
526
+ ...CostCallBaseShape
527
+ });
528
+ const CostReceiptBaseShape = {
529
+ status: z.literal("settled"),
530
+ ...CostCallBaseShape,
531
+ inputTokens: TokenCount,
532
+ outputTokens: TokenCount,
533
+ cachedTokens: TokenCount.optional(),
534
+ costUsd: NonNegative,
535
+ costUnknown: z.boolean(),
536
+ usageUnknown: z.boolean().default(false),
537
+ pricing: CostPricingSchema.optional(),
538
+ actualCostUsd: NonNegative.optional(),
539
+ error: z.string().optional()
540
+ };
541
+ const CostReceiptSchema = z.strictObject({
542
+ ...CostReceiptBaseShape,
543
+ reasoningTokens: TokenCount.optional(),
544
+ cacheWriteTokens: TokenCount.optional()
545
+ }).superRefine((receipt, ctx) => validateCostReceipt(receipt, ctx));
546
+ function validateCostReceipt(receipt, ctx) {
547
+ if (receipt.reasoningTokens !== void 0 && receipt.reasoningTokens > receipt.outputTokens) ctx.addIssue({
548
+ code: "custom",
549
+ message: "reasoningTokens must not exceed outputTokens"
550
+ });
551
+ if (receipt.actualCostUsd !== void 0) {
552
+ if (receipt.costUnknown || receipt.costUsd !== receipt.actualCostUsd) ctx.addIssue({
553
+ code: "custom",
554
+ message: "actual cost must be known and equal costUsd"
555
+ });
556
+ if (receipt.pricing !== void 0) ctx.addIssue({
557
+ code: "custom",
558
+ message: "actual cost must not include estimated pricing"
559
+ });
560
+ return;
561
+ }
562
+ if (receipt.costUnknown) {
563
+ if (receipt.costUsd !== 0) ctx.addIssue({
564
+ code: "custom",
565
+ message: "unknown cost must have costUsd 0"
566
+ });
567
+ if (receipt.pricing !== void 0) ctx.addIssue({
568
+ code: "custom",
569
+ message: "unknown cost must not include estimated pricing"
570
+ });
571
+ return;
572
+ }
573
+ if (receipt.usageUnknown) ctx.addIssue({
574
+ code: "custom",
575
+ message: "known estimated cost requires known usage"
576
+ });
577
+ if (!receipt.pricing) {
578
+ ctx.addIssue({
579
+ code: "custom",
580
+ message: "known estimated cost requires a pricing snapshot"
581
+ });
582
+ return;
583
+ }
584
+ const expected = costFromPricing(receipt, receipt.pricing);
585
+ if (receipt.costUsd !== expected) ctx.addIssue({
586
+ code: "custom",
587
+ message: `estimated cost ${receipt.costUsd} does not match pricing snapshot ${expected}`
588
+ });
589
+ }
590
+ const CostLedgerEventSchema = z.union([
591
+ z.strictObject({
592
+ version: z.literal(1),
593
+ record: PendingCostCallSchema
594
+ }),
595
+ z.strictObject({
596
+ version: z.literal(2),
597
+ record: CostReceiptSchema
598
+ }),
599
+ z.strictObject({
600
+ version: z.literal(1),
601
+ completedTasks: TokenCount
602
+ }),
603
+ z.strictObject({
604
+ version: z.literal(1),
605
+ costCeilingUsd: NonNegative
606
+ })
607
+ ]);
608
+ function parseEvents(serialized) {
609
+ const records = /* @__PURE__ */ new Map();
610
+ let completedTasks = 0;
611
+ let costCeilingUsd;
612
+ let lineNumber = 0;
613
+ try {
614
+ for (const line of serialized.split("\n")) {
615
+ if (!line.trim()) continue;
616
+ lineNumber += 1;
617
+ const event = CostLedgerEventSchema.parse(JSON.parse(line));
618
+ if ("record" in event) {
619
+ const record = event.record;
620
+ validateTransition(records, record);
621
+ records.set(record.callId, cloneRecord(record));
622
+ } else if ("completedTasks" in event) {
623
+ completedTasks += event.completedTasks;
624
+ if (!Number.isSafeInteger(completedTasks)) throw new ValidationError("CostLedger: completed task count exceeds safe integer range");
625
+ } else {
626
+ if (costCeilingUsd !== void 0) throw new ValidationError("CostLedger: duplicate persisted cost ceiling");
627
+ costCeilingUsd = event.costCeilingUsd;
628
+ }
629
+ }
630
+ return {
631
+ records: [...records.values()],
632
+ completedTasks,
633
+ ...costCeilingUsd === void 0 ? {} : { costCeilingUsd }
634
+ };
635
+ } catch (cause) {
636
+ throw new ValidationError(`CostLedger: invalid persisted event ${lineNumber || 1}: ${validationMessage(cause)}`, { cause });
637
+ }
638
+ }
639
+ function validateTransition(records, record) {
640
+ const current = records.get(record.callId);
641
+ if (!current) return;
642
+ if (record.status === "pending" || current.status === "settled") throw new CostCallConflictError(`CostLedger: duplicate callId '${record.callId}'`);
643
+ if (!sameAttribution(current, record)) throw new ValidationError(`CostLedger: receipt attribution changed for call '${record.callId}'`);
644
+ }
645
+ function sameAttribution(before, after) {
646
+ return before.channel === after.channel && before.phase === after.phase && before.actor === after.actor && before.maximumCostUsd === after.maximumCostUsd && before.timestamp === after.timestamp && JSON.stringify(before.tags ?? {}) === JSON.stringify(after.tags ?? {});
647
+ }
648
+ function parseReceipt(value, path) {
649
+ try {
650
+ return CostReceiptSchema.parse(value);
651
+ } catch (cause) {
652
+ throw new ValidationError(`CostLedger: invalid ${path}: ${validationMessage(cause)}`, { cause });
653
+ }
654
+ }
655
+ function parseImportedReceipt(value, path) {
656
+ if (typeof value !== "object" || value === null) return parseReceipt(value, path);
657
+ const candidate = { ...value };
658
+ if (candidate.status === "settled" && candidate.actualCostUsd === void 0 && candidate.costUnknown === false && candidate.pricing === void 0 && typeof candidate.model === "string") {
659
+ const pricing = resolveModelPricing(candidate.model);
660
+ if (pricing) candidate.pricing = {
661
+ inputUsdPerThousand: pricing.input,
662
+ outputUsdPerThousand: pricing.output
663
+ };
664
+ }
665
+ return parseReceipt(candidate, path);
666
+ }
667
+ function validateAttribution(input) {
668
+ assertString(input.channel, "channel");
669
+ assertString(input.phase, "phase");
670
+ assertString(input.actor, "actor");
671
+ if (input.model !== void 0) assertString(input.model, "model");
672
+ if (input.tags !== void 0) {
673
+ const parsed = Tags.safeParse(input.tags);
674
+ if (!parsed.success) throw new ValidationError(`CostLedger: invalid tags: ${parsed.error.message}`);
675
+ }
676
+ }
677
+ function matches(record, filter) {
678
+ if (!filter) return true;
679
+ if (filter.channel !== void 0 && record.channel !== filter.channel) return false;
680
+ if (filter.phase !== void 0 && record.phase !== filter.phase) return false;
681
+ return Object.entries(filter.tags ?? {}).every(([key, value]) => record.tags?.[key] === value);
682
+ }
683
+ function cloneRecord(record) {
684
+ if (record.status === "settled") return cloneReceipt(record);
685
+ return clonePendingCall(record);
686
+ }
687
+ function clonePendingCall(record) {
688
+ const { tags, ...rest } = record;
689
+ return {
690
+ ...rest,
691
+ ...tags ? { tags: { ...tags } } : {}
692
+ };
693
+ }
694
+ function cloneReceipt(receipt) {
695
+ const { pricing, tags, ...rest } = receipt;
696
+ return {
697
+ ...rest,
698
+ ...tags ? { tags: { ...tags } } : {},
699
+ ...pricing ? { pricing: { ...pricing } } : {}
700
+ };
701
+ }
702
+ function costFromPricing(usage, pricing) {
703
+ const cachedInputRate = pricing.cachedInputUsdPerThousand ?? pricing.inputUsdPerThousand;
704
+ const cacheWriteRate = pricing.cacheWriteUsdPerThousand ?? pricing.inputUsdPerThousand;
705
+ return usage.inputTokens / 1e3 * pricing.inputUsdPerThousand + (usage.cachedTokens ?? 0) / 1e3 * cachedInputRate + (usage.cacheWriteTokens ?? 0) / 1e3 * cacheWriteRate + usage.outputTokens / 1e3 * pricing.outputUsdPerThousand;
706
+ }
707
+ function pricingSnapshot(pricing) {
708
+ costForTokenPricing(pricing, {
709
+ inputTokens: 0,
710
+ outputTokens: 0
711
+ });
712
+ return {
713
+ inputUsdPerThousand: pricing.inputUsdPerMillion / 1e3,
714
+ ...pricing.cachedInputUsdPerMillion === void 0 ? {} : { cachedInputUsdPerThousand: pricing.cachedInputUsdPerMillion / 1e3 },
715
+ ...pricing.cacheWriteUsdPerMillion === void 0 ? {} : { cacheWriteUsdPerThousand: pricing.cacheWriteUsdPerMillion / 1e3 },
716
+ outputUsdPerThousand: pricing.outputUsdPerMillion / 1e3
717
+ };
718
+ }
719
+ function emptyRollup(channel) {
720
+ return {
721
+ channel,
722
+ calls: 0,
723
+ inputTokens: 0,
724
+ outputTokens: 0,
725
+ reasoningTokens: 0,
726
+ cachedTokens: 0,
727
+ cacheWriteTokens: 0,
728
+ costUsd: 0,
729
+ unpricedCalls: 0,
730
+ unknownUsageCalls: 0
731
+ };
732
+ }
733
+ function pendingModel(input) {
734
+ if (input.model) return input.model;
735
+ if (input.maximumCharge && "model" in input.maximumCharge) return input.maximumCharge.model;
736
+ return "unknown";
737
+ }
738
+ function unknownReceipt(model) {
739
+ return {
740
+ model,
741
+ inputTokens: 0,
742
+ outputTokens: 0,
743
+ costUnknown: true,
744
+ usageUnknown: true
745
+ };
746
+ }
747
+ function resolveCallId(input) {
748
+ if (input !== void 0) {
749
+ assertString(input, "callId");
750
+ return input;
751
+ }
752
+ if (typeof globalThis.crypto?.randomUUID !== "function") throw new ValidationError("CostLedger: crypto.randomUUID is required when callId is omitted");
753
+ return globalThis.crypto.randomUUID();
754
+ }
755
+ function abortError(signal) {
756
+ const reason = signal.reason;
757
+ if (reason instanceof Error) return reason;
758
+ const error = /* @__PURE__ */ new Error("CostLedger: paid call aborted");
759
+ error.name = "AbortError";
760
+ return error;
761
+ }
762
+ function toError(value) {
763
+ return value instanceof Error ? value : new Error(String(value));
764
+ }
765
+ function paidFailure(callId, error, receipt) {
766
+ return {
767
+ succeeded: false,
768
+ callId,
769
+ error,
770
+ ...receipt ? { receipt: cloneReceipt(receipt) } : {}
771
+ };
772
+ }
773
+ function validationMessage(cause) {
774
+ return cause instanceof z.ZodError ? z.prettifyError(cause) : toError(cause).message;
775
+ }
776
+ function assertUsage(usage) {
777
+ assertTokenCount(usage.inputTokens, "inputTokens");
778
+ assertTokenCount(usage.outputTokens, "outputTokens");
779
+ if (usage.reasoningTokens !== void 0) {
780
+ assertTokenCount(usage.reasoningTokens, "reasoningTokens");
781
+ if (usage.reasoningTokens > usage.outputTokens) throw new ValidationError("CostLedger: reasoningTokens must not exceed outputTokens");
782
+ }
783
+ if (usage.cachedTokens !== void 0) assertTokenCount(usage.cachedTokens, "cachedTokens");
784
+ if (usage.cacheWriteTokens !== void 0) assertTokenCount(usage.cacheWriteTokens, "cacheWriteTokens");
785
+ }
786
+ function assertTokenCount(value, name) {
787
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0) throw new ValidationError(`CostLedger: ${name} must be a non-negative integer, got ${String(value)}`);
788
+ }
789
+ function assertNonNegative(value, name) {
790
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) throw new ValidationError(`CostLedger: ${name} must be a non-negative finite number, got ${String(value)}`);
791
+ }
792
+ function assertTimeout(value, name) {
793
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || value > 2147483647) throw new ValidationError(`CostLedger: ${name} must be a non-negative safe integer no greater than 2147483647, got ${String(value)}`);
794
+ }
795
+ function assertString(value, name) {
796
+ if (typeof value !== "string" || value.trim().length === 0) throw new ValidationError(`CostLedger: ${name} must be a non-empty string`);
797
+ }
798
+ //#endregion
799
+ export { CostLedgerPersistenceError as a, costForTokenPricing as c, CostLedger as i, costForUsage as l, CostCallConflictError as n, CostReceiptCaptureError as o, CostCeilingReachedError as r, CostReservationExceededError as s, CostAccountingIncompleteError as t, modelPriceKey as u };
800
+
801
+ //# sourceMappingURL=cost-ledger-DIgQUFZZ.js.map