@tangle-network/agent-eval 0.128.2 → 0.130.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 (424) hide show
  1. package/CHANGELOG.md +279 -0
  2. package/README.md +19 -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 +83 -2932
  8. package/dist/analyst/index.d.ts.map +1 -0
  9. package/dist/analyst/index.js +319 -364
  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 -1205
  30. package/dist/belief-state/index.d.ts.map +1 -0
  31. package/dist/belief-state/index.js +1617 -1710
  32. package/dist/belief-state/index.js.map +1 -1
  33. package/dist/benchmarks/index.d.ts +2 -894
  34. package/dist/benchmarks/index.js +2 -59
  35. package/dist/benchmarks-DviOvUNr.js +754 -0
  36. package/dist/benchmarks-DviOvUNr.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 -6390
  44. package/dist/campaign/index.js +3 -212
  45. package/dist/campaign-CBKZvQ1H.js +3885 -0
  46. package/dist/campaign-CBKZvQ1H.js.map +1 -0
  47. package/dist/cli.d.ts +1 -1
  48. package/dist/cli.js +164 -174
  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 -5605
  61. package/dist/contract/index.d.ts.map +1 -0
  62. package/dist/contract/index.js +1730 -1937
  63. package/dist/contract/index.js.map +1 -1
  64. package/dist/control.d.ts +3 -1030
  65. package/dist/control.js +2 -32
  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 -617
  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-CAPUUKaM.d.ts +335 -0
  116. package/dist/index-CAPUUKaM.d.ts.map +1 -0
  117. package/dist/index-DE5fb3EC.d.ts +2244 -0
  118. package/dist/index-DE5fb3EC.d.ts.map +1 -0
  119. package/dist/index-DSC51roc.d.ts +102 -0
  120. package/dist/index-DSC51roc.d.ts.map +1 -0
  121. package/dist/index.d.ts +3776 -15120
  122. package/dist/index.d.ts.map +1 -0
  123. package/dist/index.js +11185 -11191
  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 -481
  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 -1298
  196. package/dist/reporting.js +6 -50
  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 +916 -3596
  206. package/dist/rl.d.ts.map +1 -0
  207. package/dist/rl.js +2362 -1751
  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 -1048
  211. package/dist/rollout/index.js +8 -110
  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/run-record-BuoE80Dq.js.map +1 -0
  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-B7wX7XkF.d.ts +1739 -0
  253. package/dist/skillopt-optimization-method-B7wX7XkF.d.ts.map +1 -0
  254. package/dist/skillopt-optimization-method-D4ODwFVV.js +7712 -0
  255. package/dist/skillopt-optimization-method-D4ODwFVV.js.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 -849
  273. package/dist/supervisor-run/index.js +2 -64
  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 -251
  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 -1174
  298. package/dist/wire/index.d.ts.map +1 -0
  299. package/dist/wire/index.js +2 -81
  300. package/docs/feature-guide.md +1 -1
  301. package/docs/rollout.md +116 -2
  302. package/package.json +18 -10
  303. package/dist/benchmarks/index.js.map +0 -1
  304. package/dist/campaign/index.js.map +0 -1
  305. package/dist/chunk-2JX3CFMB.js +0 -695
  306. package/dist/chunk-2JX3CFMB.js.map +0 -1
  307. package/dist/chunk-2MKQIFS4.js +0 -183
  308. package/dist/chunk-2MKQIFS4.js.map +0 -1
  309. package/dist/chunk-3RF76KTD.js +0 -84
  310. package/dist/chunk-3RF76KTD.js.map +0 -1
  311. package/dist/chunk-5DTSBUL2.js +0 -159
  312. package/dist/chunk-5DTSBUL2.js.map +0 -1
  313. package/dist/chunk-7ZZMD7UK.js +0 -386
  314. package/dist/chunk-7ZZMD7UK.js.map +0 -1
  315. package/dist/chunk-BOD4O7OF.js +0 -40
  316. package/dist/chunk-BOD4O7OF.js.map +0 -1
  317. package/dist/chunk-BYT7ELPS.js +0 -1553
  318. package/dist/chunk-BYT7ELPS.js.map +0 -1
  319. package/dist/chunk-DJKY2TSY.js +0 -2428
  320. package/dist/chunk-DJKY2TSY.js.map +0 -1
  321. package/dist/chunk-DPUHNQLN.js +0 -232
  322. package/dist/chunk-DPUHNQLN.js.map +0 -1
  323. package/dist/chunk-DRYIUNWY.js +0 -622
  324. package/dist/chunk-DRYIUNWY.js.map +0 -1
  325. package/dist/chunk-EJGRPCO3.js +0 -617
  326. package/dist/chunk-EJGRPCO3.js.map +0 -1
  327. package/dist/chunk-EOSZT7PL.js +0 -2001
  328. package/dist/chunk-EOSZT7PL.js.map +0 -1
  329. package/dist/chunk-EZJEIH2R.js +0 -1559
  330. package/dist/chunk-EZJEIH2R.js.map +0 -1
  331. package/dist/chunk-GGE4NNQT.js +0 -65
  332. package/dist/chunk-GGE4NNQT.js.map +0 -1
  333. package/dist/chunk-HHWE3POT.js +0 -94
  334. package/dist/chunk-HHWE3POT.js.map +0 -1
  335. package/dist/chunk-IHQDPH7D.js +0 -171
  336. package/dist/chunk-IHQDPH7D.js.map +0 -1
  337. package/dist/chunk-JHCHEVET.js +0 -274
  338. package/dist/chunk-JHCHEVET.js.map +0 -1
  339. package/dist/chunk-K4DBDHLK.js +0 -158
  340. package/dist/chunk-K4DBDHLK.js.map +0 -1
  341. package/dist/chunk-K6N6XJJX.js +0 -306
  342. package/dist/chunk-K6N6XJJX.js.map +0 -1
  343. package/dist/chunk-MA6HLL3S.js +0 -65
  344. package/dist/chunk-MA6HLL3S.js.map +0 -1
  345. package/dist/chunk-MAZ26DC7.js +0 -99
  346. package/dist/chunk-MAZ26DC7.js.map +0 -1
  347. package/dist/chunk-MHELPNRP.js +0 -1212
  348. package/dist/chunk-MHELPNRP.js.map +0 -1
  349. package/dist/chunk-NACAGYSY.js +0 -1040
  350. package/dist/chunk-NACAGYSY.js.map +0 -1
  351. package/dist/chunk-NKAGIDE2.js +0 -7633
  352. package/dist/chunk-NKAGIDE2.js.map +0 -1
  353. package/dist/chunk-NPCTHQIO.js +0 -91
  354. package/dist/chunk-NPCTHQIO.js.map +0 -1
  355. package/dist/chunk-NYLOYM6N.js +0 -332
  356. package/dist/chunk-NYLOYM6N.js.map +0 -1
  357. package/dist/chunk-ONWEPEDO.js +0 -57
  358. package/dist/chunk-ONWEPEDO.js.map +0 -1
  359. package/dist/chunk-P5W7RQKK.js +0 -576
  360. package/dist/chunk-P5W7RQKK.js.map +0 -1
  361. package/dist/chunk-P6FYH6K4.js +0 -1161
  362. package/dist/chunk-P6FYH6K4.js.map +0 -1
  363. package/dist/chunk-PBE2LOSS.js +0 -669
  364. package/dist/chunk-PBE2LOSS.js.map +0 -1
  365. package/dist/chunk-PC4UYEBM.js +0 -166
  366. package/dist/chunk-PC4UYEBM.js.map +0 -1
  367. package/dist/chunk-PXE2VKMX.js +0 -140
  368. package/dist/chunk-PXE2VKMX.js.map +0 -1
  369. package/dist/chunk-PZ5AY32C.js +0 -10
  370. package/dist/chunk-PZ5AY32C.js.map +0 -1
  371. package/dist/chunk-RZTMDUO7.js +0 -49
  372. package/dist/chunk-RZTMDUO7.js.map +0 -1
  373. package/dist/chunk-S5YLIBFX.js +0 -136
  374. package/dist/chunk-S5YLIBFX.js.map +0 -1
  375. package/dist/chunk-SZLVEKMJ.js +0 -1446
  376. package/dist/chunk-SZLVEKMJ.js.map +0 -1
  377. package/dist/chunk-T4SQEITX.js +0 -95
  378. package/dist/chunk-T4SQEITX.js.map +0 -1
  379. package/dist/chunk-TBL77AUT.js +0 -355
  380. package/dist/chunk-TBL77AUT.js.map +0 -1
  381. package/dist/chunk-TSN7JT6D.js +0 -1646
  382. package/dist/chunk-TSN7JT6D.js.map +0 -1
  383. package/dist/chunk-TT4KNT67.js +0 -124
  384. package/dist/chunk-TT4KNT67.js.map +0 -1
  385. package/dist/chunk-UB2LOJ6Q.js +0 -4461
  386. package/dist/chunk-UB2LOJ6Q.js.map +0 -1
  387. package/dist/chunk-UWZZKKU7.js +0 -237
  388. package/dist/chunk-UWZZKKU7.js.map +0 -1
  389. package/dist/chunk-VBQ3CRKH.js +0 -291
  390. package/dist/chunk-VBQ3CRKH.js.map +0 -1
  391. package/dist/chunk-VGRCHJON.js +0 -163
  392. package/dist/chunk-VGRCHJON.js.map +0 -1
  393. package/dist/chunk-VI2UW6B6.js +0 -162
  394. package/dist/chunk-VI2UW6B6.js.map +0 -1
  395. package/dist/chunk-VLOATJQ2.js +0 -908
  396. package/dist/chunk-VLOATJQ2.js.map +0 -1
  397. package/dist/chunk-VQMK5FMP.js +0 -247
  398. package/dist/chunk-VQMK5FMP.js.map +0 -1
  399. package/dist/chunk-VZSRQ272.js +0 -149
  400. package/dist/chunk-VZSRQ272.js.map +0 -1
  401. package/dist/chunk-WGXIEX7P.js +0 -116
  402. package/dist/chunk-WGXIEX7P.js.map +0 -1
  403. package/dist/chunk-WS3NZZQQ.js +0 -929
  404. package/dist/chunk-WS3NZZQQ.js.map +0 -1
  405. package/dist/chunk-XDWDC2MP.js +0 -695
  406. package/dist/chunk-XDWDC2MP.js.map +0 -1
  407. package/dist/chunk-XPRT64IE.js +0 -766
  408. package/dist/chunk-XPRT64IE.js.map +0 -1
  409. package/dist/chunk-YJBNWCAA.js +0 -1056
  410. package/dist/chunk-YJBNWCAA.js.map +0 -1
  411. package/dist/chunk-ZET2UAYW.js +0 -89
  412. package/dist/chunk-ZET2UAYW.js.map +0 -1
  413. package/dist/chunk-ZUUWPZCV.js +0 -752
  414. package/dist/chunk-ZUUWPZCV.js.map +0 -1
  415. package/dist/control.js.map +0 -1
  416. package/dist/hosted/index.js.map +0 -1
  417. package/dist/matrix/index.js.map +0 -1
  418. package/dist/reporting.js.map +0 -1
  419. package/dist/rollout/index.js.map +0 -1
  420. package/dist/run-campaign-ISHFZ7FJ.js +0 -17
  421. package/dist/run-campaign-ISHFZ7FJ.js.map +0 -1
  422. package/dist/supervisor-run/index.js.map +0 -1
  423. package/dist/traces.js.map +0 -1
  424. package/dist/wire/index.js.map +0 -1
@@ -1,2428 +0,0 @@
1
- import {
2
- executionTrackByLane
3
- } from "./chunk-HHWE3POT.js";
4
- import {
5
- combineAbortSignals
6
- } from "./chunk-WGXIEX7P.js";
7
- import {
8
- LlmClient,
9
- callLlm,
10
- costReceiptFromLlm,
11
- costReceiptFromLlmError,
12
- maximumChargeForLlmRequest
13
- } from "./chunk-PBE2LOSS.js";
14
- import {
15
- CostLedger
16
- } from "./chunk-WS3NZZQQ.js";
17
- import {
18
- buildTraceAnalystTools,
19
- runTraceAnalysisLoop,
20
- spanEpochMillis
21
- } from "./chunk-P6FYH6K4.js";
22
- import {
23
- LLM_CONTEXT_TOKENS,
24
- LLM_INPUT_TOKEN_ATTR_KEYS,
25
- LLM_OUTPUT_TOKEN_ATTR_KEYS,
26
- TOOL_NAME_ATTR_KEYS
27
- } from "./chunk-K4DBDHLK.js";
28
-
29
- // src/analyst/ax-service.ts
30
- import { ai } from "@ax-llm/ax";
31
- var configuredModels = /* @__PURE__ */ new WeakMap();
32
- function createAnalystAi(config) {
33
- const model = config.model.trim();
34
- if (!model) throw new TypeError("createAnalystAi: model must be a non-empty string");
35
- const args = {
36
- name: config.provider ?? "openai",
37
- apiKey: config.apiKey,
38
- ...config.baseUrl ? { apiURL: config.baseUrl } : {},
39
- ...config.headers ? { headers: config.headers } : {},
40
- config: { model }
41
- };
42
- const service = ai(args);
43
- configuredModels.set(service, model);
44
- return service;
45
- }
46
- function getConfiguredAnalystModel(service) {
47
- return configuredModels.get(service);
48
- }
49
- function resolveAnalystModel(service, override) {
50
- if (override !== void 0) {
51
- const model2 = override.trim();
52
- if (!model2) throw new TypeError("createTraceAnalystKind: model must be a non-empty string");
53
- return model2;
54
- }
55
- const model = getConfiguredAnalystModel(service)?.trim();
56
- if (!model) {
57
- throw new TypeError(
58
- "createTraceAnalystKind: model is required for Ax services not created by createAnalystAi()"
59
- );
60
- }
61
- return model;
62
- }
63
-
64
- // src/analyst/chat-client.ts
65
- function createChatClient(opts) {
66
- switch (opts.transport) {
67
- case "router":
68
- return wrapLlmClient(
69
- opts.transport,
70
- opts.defaultModel,
71
- new LlmClient({
72
- baseUrl: opts.baseUrl ?? "https://router.tangle.tools/v1",
73
- apiKey: opts.apiKey,
74
- maxRetries: opts.maximumAttempts
75
- })
76
- );
77
- case "cli-bridge":
78
- return wrapLlmClient(
79
- opts.transport,
80
- opts.defaultModel,
81
- new LlmClient({
82
- baseUrl: opts.baseUrl ?? "http://127.0.0.1:3344/v1",
83
- apiKey: opts.bearer ?? "",
84
- maxRetries: opts.maximumAttempts
85
- })
86
- );
87
- case "direct-provider":
88
- return wrapLlmClient(
89
- opts.transport,
90
- opts.defaultModel,
91
- new LlmClient({
92
- baseUrl: opts.baseUrl,
93
- apiKey: opts.apiKey,
94
- maxRetries: opts.maximumAttempts
95
- })
96
- );
97
- case "sandbox-sdk":
98
- return {
99
- transport: "sandbox-sdk",
100
- defaultModel: opts.defaultModel,
101
- maximumAttempts: opts.maximumAttempts,
102
- chat: async (req, callOpts) => opts.chat(resolveModel(req, opts.defaultModel), callOpts)
103
- };
104
- case "mock":
105
- return {
106
- transport: "mock",
107
- defaultModel: opts.defaultModel,
108
- maximumAttempts: 1,
109
- chat: async (req, callOpts) => opts.handler(resolveModel(req, opts.defaultModel), callOpts)
110
- };
111
- }
112
- }
113
- function wrapLlmClient(transport, defaultModel, inner) {
114
- return {
115
- transport,
116
- defaultModel,
117
- maximumAttempts: inner.maximumAttempts,
118
- chat: (req, callOpts) => {
119
- const resolved = resolveModel(req, defaultModel);
120
- const request = {
121
- model: resolved.model,
122
- messages: req.messages,
123
- jsonMode: req.jsonMode,
124
- jsonSchema: req.jsonSchema,
125
- temperature: req.temperature,
126
- maxTokens: req.maxTokens,
127
- timeoutMs: req.timeoutMs
128
- };
129
- return inner.call(request, {
130
- signal: callOpts?.signal,
131
- idempotencyKey: callOpts?.idempotencyKey
132
- });
133
- }
134
- };
135
- }
136
- function resolveModel(req, defaultModel) {
137
- if (req.model) return req;
138
- if (!defaultModel) {
139
- throw new Error(
140
- "ChatClient.chat: no model on request and no defaultModel on the client. Either pass req.model or bind defaultModel at createChatClient()."
141
- );
142
- }
143
- return { ...req, model: defaultModel };
144
- }
145
-
146
- // src/trace-analyst/behavioral-metrics.ts
147
- var INPUT_GROWTH_FACTOR = 3;
148
- var MIN_TOOL_CALLS = 3;
149
- var VERIFY_RE = /verif|eval|inspect|check|assert|validat|review|confirm|read|grep|glob|search|view|\blist\b|\bls\b|\bcat\b|\bfind\b|diff|status|\btest|lint|typecheck/i;
150
- function num(v) {
151
- return typeof v === "number" && Number.isFinite(v) ? v : null;
152
- }
153
- function numAttr(attrs, keys) {
154
- for (const key of keys) {
155
- const value = num(attrs[key]);
156
- if (value !== null) return value;
157
- }
158
- return null;
159
- }
160
- function inputTokensOf(s) {
161
- const exactContext = num(s.attributes[LLM_CONTEXT_TOKENS]);
162
- if (exactContext !== null) return exactContext;
163
- return numAttr(s.attributes, LLM_INPUT_TOKEN_ATTR_KEYS) ?? num(s.attributes["llm.usage.input_tokens"]);
164
- }
165
- function outputTokensOf(s) {
166
- return numAttr(s.attributes, LLM_OUTPUT_TOKEN_ATTR_KEYS) ?? num(s.attributes["llm.usage.output_tokens"]);
167
- }
168
- function stepOf(s) {
169
- return num(s.attributes.step);
170
- }
171
- function toolNameOf(s) {
172
- if (s.tool_name) return s.tool_name;
173
- for (const key of TOOL_NAME_ATTR_KEYS) {
174
- const t = s.attributes[key];
175
- if (typeof t === "string" && t.length > 0) return t;
176
- }
177
- return null;
178
- }
179
- function computeTraceMetrics(spans) {
180
- const traceIds = new Set(spans.map((span) => span.trace_id));
181
- if (traceIds.size > 1) {
182
- throw new Error(
183
- `computeTraceMetrics: expected spans from one trace, received ${traceIds.size} traces`
184
- );
185
- }
186
- const traceId = traceIds.values().next().value ?? null;
187
- const samples = spans.map((span) => ({
188
- span,
189
- input: inputTokensOf(span),
190
- output: outputTokensOf(span),
191
- step: stepOf(span)
192
- }));
193
- const llmSamples = samples.filter((sample) => sample.span.kind === "LLM");
194
- const tokenSamples = llmSamples.length > 0 ? llmSamples : samples.filter((sample) => sample.input !== null || sample.output !== null);
195
- const tokenSequences = buildTokenSequences(tokenSamples, spans);
196
- const primarySequence = tokenSequences[0];
197
- const inputTokenTrajectory = primarySequence?.inputTokenTrajectory.filter((value) => value !== null) ?? [];
198
- const outputTokenTrajectory = primarySequence?.outputTokenTrajectory.filter((value) => value !== null) ?? [];
199
- const toolHistogram = {};
200
- let hasSelfVerification = false;
201
- for (const s of spans) {
202
- const tool = toolNameOf(s);
203
- if (tool) {
204
- toolHistogram[tool] = (toolHistogram[tool] ?? 0) + 1;
205
- if (VERIFY_RE.test(tool)) hasSelfVerification = true;
206
- }
207
- }
208
- const totalToolCalls = Object.values(toolHistogram).reduce((a, b) => a + b, 0);
209
- const distinctTools = Object.keys(toolHistogram).length;
210
- const toolDiversityRatio = totalToolCalls === 0 ? 1 : distinctTools / totalToolCalls;
211
- const signals = [];
212
- const seenTokenSignals = /* @__PURE__ */ new Set();
213
- for (const sequence of tokenSequences) {
214
- for (const signal of tokenSignals(sequence)) {
215
- if (seenTokenSignals.has(signal.code)) continue;
216
- seenTokenSignals.add(signal.code);
217
- signals.push(signal);
218
- }
219
- }
220
- if (totalToolCalls >= MIN_TOOL_CALLS && distinctTools === 1) {
221
- const only = Object.keys(toolHistogram)[0];
222
- signals.push({
223
- code: "single-tool-dependency",
224
- severity: "medium",
225
- detail: `All ${totalToolCalls} observed tool calls are \`${only}\`; no alternate tool call was observed.`,
226
- evidence: { tool: only, calls: totalToolCalls, distinct_tools: 1 }
227
- });
228
- }
229
- if (totalToolCalls >= MIN_TOOL_CALLS && !hasSelfVerification) {
230
- signals.push({
231
- code: "no-self-verification",
232
- severity: "medium",
233
- detail: `${totalToolCalls} tool calls were observed without a verification-named tool call.`,
234
- evidence: { tool_calls: totalToolCalls, verification_calls: 0 }
235
- });
236
- }
237
- return {
238
- traceId,
239
- llmCallCount: tokenSamples.length,
240
- tokenSequences,
241
- inputTokenTrajectory,
242
- outputTokenTrajectory,
243
- toolHistogram,
244
- totalToolCalls,
245
- distinctTools,
246
- toolDiversityRatio,
247
- hasSelfVerification,
248
- signals
249
- };
250
- }
251
- function buildTokenSequences(samples, spans) {
252
- const spansById = new Map(spans.map((span) => [span.span_id, span]));
253
- const executionScopeFor = createTokenExecutionScopeResolver(spansById);
254
- const scopedSamples = samples.map((sample) => ({
255
- sample,
256
- ...executionScopeFor(sample.span)
257
- }));
258
- const trackByLane = executionTrackByLane(scopedSamples);
259
- const byTrack = /* @__PURE__ */ new Map();
260
- for (const scoped of scopedSamples) {
261
- const trackId = trackByLane.get(scoped.key);
262
- const track = byTrack.get(trackId) ?? { scopeId: scoped.scopeId, samples: [] };
263
- track.samples.push(scoped.sample);
264
- byTrack.set(trackId, track);
265
- }
266
- const sequences = [];
267
- for (const { scopeId, samples: tracked } of byTrack.values()) {
268
- const runs = serialTokenRuns([...tracked].sort(compareTokenSamples));
269
- runs.forEach((run, index) => {
270
- sequences.push({
271
- scopeId: runs.length === 1 ? scopeId : `${scopeId}#${index + 1}`,
272
- spanIds: run.map((sample) => sample.span.span_id),
273
- inputTokenTrajectory: run.map((sample) => sample.input),
274
- outputTokenTrajectory: run.map((sample) => sample.output)
275
- });
276
- });
277
- }
278
- return sequences.sort(
279
- (a, b) => b.spanIds.length - a.spanIds.length || a.scopeId.localeCompare(b.scopeId) || a.spanIds[0].localeCompare(b.spanIds[0])
280
- );
281
- }
282
- function createTokenExecutionScopeResolver(spansById) {
283
- const cache = /* @__PURE__ */ new Map();
284
- return (span) => {
285
- const ancestry = resolveAncestorScope(span.parent_span_id, spansById, cache);
286
- const rootId = ancestry.rootId;
287
- const scopeId = ancestry.agentId ? `span:${ancestry.agentId}` : ancestry.missingParentId ? `parent:${ancestry.missingParentId}` : rootId ? `root:${rootId}` : span.agent_name ? `agent:${span.agent_name}` : `trace:${span.trace_id}`;
288
- const scopeSpanId = ancestry.agentId ?? ancestry.missingParentId ?? rootId;
289
- const laneSpan = ancestry.laneSpanId ? spansById.get(ancestry.laneSpanId) : void 0;
290
- const direct = scopeSpanId === null || ancestry.laneSpanId === scopeSpanId;
291
- const timedSpan = direct ? span : laneSpan;
292
- return {
293
- key: JSON.stringify([scopeId, direct ? span.span_id : ancestry.laneSpanId]),
294
- scopeKey: scopeId,
295
- scopeId,
296
- start: timedSpan ? spanEpochMillis(timedSpan.start_time) : null,
297
- end: timedSpan ? spanEpochMillis(timedSpan.end_time) : null
298
- };
299
- };
300
- }
301
- function resolveAncestorScope(startId, spansById, cache) {
302
- const empty = {
303
- agentId: null,
304
- rootId: null,
305
- missingParentId: null,
306
- laneSpanId: null
307
- };
308
- if (!startId) return empty;
309
- const path = [];
310
- const pathIndex = /* @__PURE__ */ new Map();
311
- let currentId = startId;
312
- let resolved = empty;
313
- while (currentId) {
314
- const cached = cache.get(currentId);
315
- if (cached) {
316
- resolved = cached;
317
- break;
318
- }
319
- const cycleStart = pathIndex.get(currentId);
320
- if (cycleStart !== void 0) {
321
- resolved = {
322
- agentId: null,
323
- rootId: [...path.slice(cycleStart)].sort()[0],
324
- missingParentId: null,
325
- laneSpanId: [...path.slice(cycleStart)].sort()[0]
326
- };
327
- break;
328
- }
329
- const current = spansById.get(currentId);
330
- if (!current) {
331
- resolved = {
332
- agentId: null,
333
- rootId: null,
334
- missingParentId: currentId,
335
- laneSpanId: currentId
336
- };
337
- break;
338
- }
339
- if (current.kind === "AGENT") {
340
- resolved = {
341
- agentId: current.span_id,
342
- rootId: null,
343
- missingParentId: null,
344
- laneSpanId: current.span_id
345
- };
346
- break;
347
- }
348
- pathIndex.set(currentId, path.length);
349
- path.push(currentId);
350
- currentId = current.parent_span_id;
351
- }
352
- for (let index = path.length - 1; index >= 0; index -= 1) {
353
- if (resolved.agentId === null && resolved.rootId === null) {
354
- resolved = { ...resolved, rootId: path[index], laneSpanId: path[index] };
355
- } else if (resolved.laneSpanId === (resolved.agentId ?? resolved.missingParentId ?? resolved.rootId)) {
356
- resolved = { ...resolved, laneSpanId: path[index] };
357
- }
358
- cache.set(path[index], resolved);
359
- }
360
- return resolved;
361
- }
362
- function compareTokenSamples(a, b) {
363
- const aStart = spanEpochMillis(a.span.start_time);
364
- const bStart = spanEpochMillis(b.span.start_time);
365
- if (aStart === null && bStart !== null) return 1;
366
- if (aStart !== null && bStart === null) return -1;
367
- if (aStart !== null && bStart !== null && aStart !== bStart) return aStart - bStart;
368
- if (a.step !== null && b.step !== null && a.step !== b.step) return a.step - b.step;
369
- return a.span.span_id.localeCompare(b.span.span_id);
370
- }
371
- function serialTokenRuns(ordered) {
372
- const runs = [];
373
- let serial = [];
374
- let overlap = [];
375
- let overlapEnd = Number.NEGATIVE_INFINITY;
376
- const flushSerial = () => {
377
- if (serial.length > 0) runs.push(serial);
378
- serial = [];
379
- };
380
- const flushOverlap = () => {
381
- if (overlap.length === 1) {
382
- serial.push(overlap[0]);
383
- } else if (overlap.length > 1) {
384
- flushSerial();
385
- for (const sample of overlap) runs.push([sample]);
386
- }
387
- overlap = [];
388
- overlapEnd = Number.NEGATIVE_INFINITY;
389
- };
390
- for (const sample of ordered) {
391
- const start = spanEpochMillis(sample.span.start_time);
392
- const end = spanEpochMillis(sample.span.end_time);
393
- if (start === null || end === null || sample.span.duration_ms <= 0 || end < start) {
394
- flushOverlap();
395
- flushSerial();
396
- runs.push([sample]);
397
- continue;
398
- }
399
- if (overlap.length > 0 && start >= overlapEnd) flushOverlap();
400
- overlap.push(sample);
401
- overlapEnd = Math.max(overlapEnd, end);
402
- }
403
- flushOverlap();
404
- flushSerial();
405
- return runs;
406
- }
407
- function tokenSignals(sequence) {
408
- const signals = [];
409
- const inputs = sequence.inputTokenTrajectory;
410
- const outputs = sequence.outputTokenTrajectory;
411
- if (inputs.length >= 3 && inputs.every((value) => value !== null)) {
412
- const first = inputs[0];
413
- const last = inputs[inputs.length - 1];
414
- const isMonotonic = everyAdjacent(inputs, (previous, current) => current >= previous);
415
- const growthFromZero = first === 0 && last > 0;
416
- const growth = growthFromZero ? Infinity : first > 0 ? last / first : 0;
417
- if (isMonotonic && last > first && growth >= INPUT_GROWTH_FACTOR) {
418
- const growthLabel = growthFromZero ? "0\u2192nonzero (unbounded)" : `${growth.toFixed(1)}x`;
419
- signals.push({
420
- code: "monotonic-input-growth",
421
- severity: "high",
422
- detail: `LLM input tokens grew ${growthLabel} (${first}\u2192${last}) across ${inputs.length} serial calls without an intervening decrease.`,
423
- evidence: {
424
- first,
425
- last,
426
- growth_x: growthFromZero ? "unbounded" : Number(growth.toFixed(2)),
427
- calls: inputs.length,
428
- scope: sequence.scopeId,
429
- first_span_id: sequence.spanIds[0],
430
- last_span_id: sequence.spanIds[sequence.spanIds.length - 1]
431
- }
432
- });
433
- }
434
- }
435
- if (inputs.length >= 3 && inputs.length === outputs.length && inputs.every((value) => value !== null) && outputs.every((value) => value !== null)) {
436
- const first = outputs[0];
437
- const last = outputs[outputs.length - 1];
438
- const inputIsMonotonic = everyAdjacent(inputs, (previous, current) => current >= previous);
439
- const outputIsMonotonic = everyAdjacent(outputs, (previous, current) => current <= previous);
440
- const inputGrew = inputs[inputs.length - 1] > inputs[0];
441
- if (inputIsMonotonic && inputGrew && outputIsMonotonic && last < first) {
442
- signals.push({
443
- code: "output-length-decay",
444
- severity: "medium",
445
- detail: `LLM output tokens shrank ${first}\u2192${last} over ${outputs.length} serial calls while input tokens increased monotonically.`,
446
- evidence: {
447
- first,
448
- last,
449
- calls: outputs.length,
450
- scope: sequence.scopeId,
451
- first_span_id: sequence.spanIds[0],
452
- last_span_id: sequence.spanIds[sequence.spanIds.length - 1]
453
- }
454
- });
455
- }
456
- }
457
- return signals;
458
- }
459
- function everyAdjacent(values, predicate) {
460
- return values.slice(1).every((current, index) => predicate(values[index], current));
461
- }
462
-
463
- // src/analyst/types.ts
464
- import { createHash } from "crypto";
465
- function computeFindingId(input) {
466
- const basis = JSON.stringify({
467
- a: input.analyst_id,
468
- r: input.area,
469
- s: input.subject ?? "",
470
- c: normalizeClaim(input.id_basis ?? input.claim)
471
- });
472
- return `f_${createHash("sha256").update(basis).digest("hex").slice(0, 20)}`;
473
- }
474
- function normalizeClaim(c) {
475
- return c.toLowerCase().replace(/\s+/g, " ").replace(/[.!?;:,]+$/g, "").trim();
476
- }
477
- function makeFinding(init) {
478
- const { id_basis, produced_at, ...rest } = init;
479
- return {
480
- schema_version: "1.0.0",
481
- finding_id: computeFindingId({
482
- analyst_id: rest.analyst_id,
483
- area: rest.area,
484
- subject: rest.subject,
485
- claim: rest.claim,
486
- id_basis
487
- }),
488
- produced_at: produced_at ?? (/* @__PURE__ */ new Date()).toISOString(),
489
- ...rest
490
- };
491
- }
492
-
493
- // src/analyst/behavioral-analyst.ts
494
- var RECOMMENDED_ACTION = {
495
- "monotonic-input-growth": "Inspect context assembly; if prior history is repeatedly included, summarize completed work before the next model call.",
496
- "output-length-decay": "Check late-step completeness; if shorter responses omit required work, add explicit completion criteria to the agent instructions.",
497
- "single-tool-dependency": "Test whether an inspect or verification tool improves outcomes after the repeated call fails or returns no progress.",
498
- "no-self-verification": "After state-changing actions, require an observable check before the agent proceeds."
499
- };
500
- var ANALYST_ID = "efficiency-behavioral";
501
- var AGGREGATE_CLAIM = {
502
- "monotonic-input-growth": (observed, analyzed) => `${observed}/${analyzed} analyzed traces showed input tokens grow from zero to nonzero or to at least 3x their initial value across at least 3 serial model calls without a decrease.`,
503
- "output-length-decay": (observed, analyzed) => `${observed}/${analyzed} analyzed traces showed output tokens decrease while input tokens increased monotonically across at least 3 serial model calls.`,
504
- "single-tool-dependency": (observed, analyzed) => `${observed}/${analyzed} analyzed traces used only one named tool across at least 3 tool calls.`,
505
- "no-self-verification": (observed, analyzed) => `${observed}/${analyzed} analyzed traces had at least 3 tool calls without a verification-named tool call.`
506
- };
507
- function deriveEfficiencyFindings(metrics, opts = {}) {
508
- const analystId = opts.analystId ?? ANALYST_ID;
509
- const traceId = metrics.traceId;
510
- return metrics.signals.map(
511
- (sig) => makeFinding({
512
- analyst_id: analystId,
513
- area: "efficiency",
514
- subject: sig.code,
515
- claim: sig.detail,
516
- severity: sig.severity,
517
- // Deterministic arithmetic over spans, not a model judgment → certain.
518
- confidence: 1,
519
- evidence_refs: [
520
- {
521
- kind: "metric",
522
- uri: traceId ? `metric://trace/${encodeURIComponent(traceId)}/efficiency/${sig.code}` : `metric://efficiency/${sig.code}`,
523
- excerpt: JSON.stringify(sig.evidence)
524
- }
525
- ],
526
- recommended_action: RECOMMENDED_ACTION[sig.code],
527
- metadata: {
528
- deterministic: true,
529
- evidence: sig.evidence,
530
- ...traceId ? { trace_id: traceId } : {}
531
- },
532
- id_basis: sig.code,
533
- ...opts.producedAt ? { produced_at: opts.producedAt } : {}
534
- })
535
- );
536
- }
537
- function behavioralAnalyst() {
538
- return {
539
- id: ANALYST_ID,
540
- description: "Deterministic behavioral/efficiency findings over OTLP spans \u2014 token-growth, output-decay, tool-monoculture, missing self-verification. Zero LLM; model-agnostic by construction.",
541
- inputKind: "trace-store",
542
- cost: { kind: "deterministic" },
543
- version: "2.0.0",
544
- async analyze(store) {
545
- const overview = await store.getOverview();
546
- const analyzedTraceIds = [...new Set(overview.sample_trace_ids)].sort();
547
- const findingsById = /* @__PURE__ */ new Map();
548
- for (const traceId of analyzedTraceIds) {
549
- const viewed = await store.viewTrace({ trace_id: traceId });
550
- if (viewed.trace_id !== traceId) {
551
- throw new Error(
552
- `behavioralAnalyst: requested trace '${traceId}', received '${viewed.trace_id}'`
553
- );
554
- }
555
- if (!viewed.spans) {
556
- throw new Error(
557
- `behavioralAnalyst: trace '${traceId}' is oversized; complete spans are required`
558
- );
559
- }
560
- const metrics = computeTraceMetrics(viewed.spans);
561
- if (metrics.traceId !== null && metrics.traceId !== traceId) {
562
- throw new Error(
563
- `behavioralAnalyst: requested trace '${traceId}', received '${metrics.traceId}'`
564
- );
565
- }
566
- for (const finding of deriveEfficiencyFindings(metrics)) {
567
- const current = findingsById.get(finding.finding_id);
568
- if (!current) {
569
- findingsById.set(finding.finding_id, {
570
- finding,
571
- traceIds: [traceId],
572
- evidence: [...finding.evidence_refs]
573
- });
574
- continue;
575
- }
576
- current.traceIds.push(traceId);
577
- current.evidence.push(...finding.evidence_refs);
578
- }
579
- }
580
- return [...findingsById.values()].map(({ finding, traceIds, evidence }) => ({
581
- ...finding,
582
- claim: AGGREGATE_CLAIM[finding.subject](
583
- traceIds.length,
584
- analyzedTraceIds.length
585
- ),
586
- rationale: `${traceIds.length}/${analyzedTraceIds.length} analyzed traces exhibited this pattern.`,
587
- evidence_refs: evidence,
588
- metadata: {
589
- deterministic: true,
590
- trace_ids: traceIds,
591
- observed_trace_count: traceIds.length,
592
- analyzed_trace_count: analyzedTraceIds.length
593
- }
594
- }));
595
- }
596
- };
597
- }
598
-
599
- // src/analyst/finding-subject.ts
600
- import { z } from "zod";
601
- var FINDING_SUBJECT_KINDS = [
602
- "knowledge.wiki",
603
- "knowledge.claim",
604
- "knowledge.raw",
605
- "knowledge.stale",
606
- "system-prompt",
607
- "skill",
608
- "tool-doc",
609
- "new-tool",
610
- "mcp",
611
- "hook",
612
- "subagent",
613
- "workflow",
614
- "rollout-policy",
615
- "agent-profile",
616
- "code",
617
- "rag",
618
- "memory",
619
- "scaffolding",
620
- "output-schema",
621
- "websearch.outdated",
622
- "prior-run-summary",
623
- "cluster"
624
- ];
625
- function parseFindingSubject(raw) {
626
- if (raw === null || raw === void 0) return null;
627
- const trimmed = raw.trim();
628
- if (trimmed.length === 0) return null;
629
- const wiki = trimmed.match(
630
- /^agent-knowledge:wiki:([a-z0-9][a-z0-9-]*)(?:#([a-z0-9][a-z0-9-]*))?$/
631
- );
632
- if (wiki)
633
- return { kind: "knowledge.wiki", slug: wiki[1], ...wiki[2] ? { heading: wiki[2] } : {} };
634
- const claim = trimmed.match(/^agent-knowledge:claim:(.+)$/);
635
- if (claim && claim[1].trim().length > 0)
636
- return { kind: "knowledge.claim", topic: claim[1].trim() };
637
- const raw_ = trimmed.match(/^agent-knowledge:raw:(.+)$/);
638
- if (raw_ && raw_[1].trim().length > 0)
639
- return { kind: "knowledge.raw", sourceId: raw_[1].trim() };
640
- const stale = trimmed.match(/^agent-knowledge:stale:([a-z0-9][a-z0-9-]*)$/);
641
- if (stale) return { kind: "knowledge.stale", slug: stale[1] };
642
- const sp = trimmed.match(/^system-prompt:(.+)$/);
643
- if (sp && sp[1].trim().length > 0) return { kind: "system-prompt", section: sp[1].trim() };
644
- const skill = trimmed.match(/^skill:([a-z0-9][a-z0-9_.-]*)$/);
645
- if (skill) return { kind: "skill", name: skill[1] };
646
- const tdAspect = trimmed.match(/^tool-doc:([a-z0-9][a-z0-9_-]*):(.+)$/);
647
- if (tdAspect && tdAspect[2].trim().length > 0) {
648
- return { kind: "tool-doc", tool: tdAspect[1], aspect: tdAspect[2].trim() };
649
- }
650
- const td = trimmed.match(/^tool-doc:([a-z0-9][a-z0-9_-]*)$/);
651
- if (td) return { kind: "tool-doc", tool: td[1] };
652
- const nt = trimmed.match(/^new-tool:([a-z0-9][a-z0-9_-]*)$/);
653
- if (nt) return { kind: "new-tool", name: nt[1] };
654
- const mcp = trimmed.match(/^mcp:([a-z0-9][a-z0-9_.-]*)(?::([a-z0-9][a-z0-9_.-]*))?$/);
655
- if (mcp) {
656
- return { kind: "mcp", server: mcp[1], ...mcp[2] ? { tool: mcp[2] } : {} };
657
- }
658
- const hook = trimmed.match(/^hook:([a-z0-9][a-z0-9_.-]*)$/);
659
- if (hook) return { kind: "hook", name: hook[1] };
660
- const subagent = trimmed.match(/^subagent:([a-z0-9][a-z0-9_.-]*)$/);
661
- if (subagent) return { kind: "subagent", name: subagent[1] };
662
- const workflow = trimmed.match(/^workflow:([a-z0-9][a-z0-9_.-]*)$/);
663
- if (workflow) return { kind: "workflow", name: workflow[1] };
664
- const rolloutPolicy = trimmed.match(/^rollout-policy:(.+)$/);
665
- if (rolloutPolicy && rolloutPolicy[1].trim().length > 0) {
666
- return { kind: "rollout-policy", field: rolloutPolicy[1].trim() };
667
- }
668
- const agentProfile = trimmed.match(/^agent-profile:(.+)$/);
669
- if (agentProfile && agentProfile[1].trim().length > 0) {
670
- return { kind: "agent-profile", field: agentProfile[1].trim() };
671
- }
672
- const code = trimmed.match(/^code:(.+)$/);
673
- if (code && code[1].trim().length > 0) return { kind: "code", path: code[1].trim() };
674
- const rag = trimmed.match(/^rag:([a-z0-9][a-z0-9_-]*):(.+)$/);
675
- if (rag && rag[2].trim().length > 0) {
676
- return { kind: "rag", corpus: rag[1], docId: rag[2].trim() };
677
- }
678
- const mem = trimmed.match(/^memory:(.+)$/);
679
- if (mem && mem[1].trim().length > 0) return { kind: "memory", key: mem[1].trim() };
680
- const sc = trimmed.match(/^scaffolding:(.+)$/);
681
- if (sc && sc[1].trim().length > 0) return { kind: "scaffolding", concern: sc[1].trim() };
682
- const os = trimmed.match(/^output-schema:(.+)$/);
683
- if (os && os[1].trim().length > 0) return { kind: "output-schema", field: os[1].trim() };
684
- const ws = trimmed.match(/^websearch:outdated:(.+)$/);
685
- if (ws && ws[1].trim().length > 0) return { kind: "websearch.outdated", topic: ws[1].trim() };
686
- const prs = trimmed.match(/^prior-run-summary:(.+)$/);
687
- if (prs && prs[1].trim().length > 0) return { kind: "prior-run-summary", topic: prs[1].trim() };
688
- if (/^[a-z0-9][a-z0-9._-]*$/.test(trimmed) && trimmed.length <= 80) {
689
- return { kind: "cluster", label: trimmed };
690
- }
691
- return null;
692
- }
693
- function renderFindingSubject(s) {
694
- switch (s.kind) {
695
- case "knowledge.wiki":
696
- return s.heading ? `agent-knowledge:wiki:${s.slug}#${s.heading}` : `agent-knowledge:wiki:${s.slug}`;
697
- case "knowledge.claim":
698
- return `agent-knowledge:claim:${s.topic}`;
699
- case "knowledge.raw":
700
- return `agent-knowledge:raw:${s.sourceId}`;
701
- case "knowledge.stale":
702
- return `agent-knowledge:stale:${s.slug}`;
703
- case "system-prompt":
704
- return `system-prompt:${s.section}`;
705
- case "skill":
706
- return `skill:${s.name}`;
707
- case "tool-doc":
708
- return s.aspect ? `tool-doc:${s.tool}:${s.aspect}` : `tool-doc:${s.tool}`;
709
- case "new-tool":
710
- return `new-tool:${s.name}`;
711
- case "mcp":
712
- return s.tool ? `mcp:${s.server}:${s.tool}` : `mcp:${s.server}`;
713
- case "hook":
714
- return `hook:${s.name}`;
715
- case "subagent":
716
- return `subagent:${s.name}`;
717
- case "workflow":
718
- return `workflow:${s.name}`;
719
- case "rollout-policy":
720
- return `rollout-policy:${s.field}`;
721
- case "agent-profile":
722
- return `agent-profile:${s.field}`;
723
- case "code":
724
- return `code:${s.path}`;
725
- case "rag":
726
- return `rag:${s.corpus}:${s.docId}`;
727
- case "memory":
728
- return `memory:${s.key}`;
729
- case "scaffolding":
730
- return `scaffolding:${s.concern}`;
731
- case "output-schema":
732
- return `output-schema:${s.field}`;
733
- case "websearch.outdated":
734
- return `websearch:outdated:${s.topic}`;
735
- case "prior-run-summary":
736
- return `prior-run-summary:${s.topic}`;
737
- case "cluster":
738
- return s.label;
739
- }
740
- }
741
- var FINDING_SUBJECT_SYNTAX = {
742
- "knowledge.wiki": "agent-knowledge:wiki:<slug>[#<heading>]",
743
- "knowledge.claim": "agent-knowledge:claim:<topic>",
744
- "knowledge.raw": "agent-knowledge:raw:<source-id>",
745
- "knowledge.stale": "agent-knowledge:stale:<slug>",
746
- "system-prompt": "system-prompt:<section>",
747
- skill: "skill:<name>",
748
- "tool-doc": "tool-doc:<tool>[:<aspect>]",
749
- "new-tool": "new-tool:<name>",
750
- mcp: "mcp:<server>[:<tool>]",
751
- hook: "hook:<name>",
752
- subagent: "subagent:<name>",
753
- workflow: "workflow:<name>",
754
- "rollout-policy": "rollout-policy:<field>",
755
- "agent-profile": "agent-profile:<field>",
756
- code: "code:<path>",
757
- rag: "rag:<corpus>:<doc-id>",
758
- memory: "memory:<key>",
759
- scaffolding: "scaffolding:<concern>",
760
- "output-schema": "output-schema:<field>",
761
- "websearch.outdated": "websearch:outdated:<topic>",
762
- "prior-run-summary": "prior-run-summary:<topic>",
763
- cluster: "<lowercase-cluster-label>"
764
- };
765
- var FINDING_SUBJECT_PURPOSE = {
766
- "knowledge.wiki": "create or update a wiki page",
767
- "knowledge.claim": "draft a claim or relation",
768
- "knowledge.raw": "curate a raw source",
769
- "knowledge.stale": "mark a stale page",
770
- "system-prompt": "revise a system-prompt section",
771
- skill: "create or revise a skill",
772
- "tool-doc": "revise a tool contract",
773
- "new-tool": "propose a new tool",
774
- mcp: "revise an MCP server or tool",
775
- hook: "revise a lifecycle hook",
776
- subagent: "revise a delegated agent",
777
- workflow: "revise an orchestration workflow",
778
- "rollout-policy": "revise budget, sampling, or stop policy",
779
- "agent-profile": "revise another AgentProfile field",
780
- code: "revise an implementation path",
781
- rag: "ingest or correct a RAG document",
782
- memory: "invalidate or set memory",
783
- scaffolding: "revise preconditions, retries, or verification",
784
- "output-schema": "constrain the output shape",
785
- "websearch.outdated": "identify a stale web result",
786
- "prior-run-summary": "identify a stale prior-run summary",
787
- cluster: "name one failure cluster"
788
- };
789
- function renderFindingSubjectGrammar(kinds) {
790
- return [
791
- "Subjects MUST match one of these forms \u2014 anything else is rejected at parse time:",
792
- ...kinds.map((kind) => ` ${FINDING_SUBJECT_SYNTAX[kind]} \u2014 ${FINDING_SUBJECT_PURPOSE[kind]}`),
793
- "Runtime ids are lowercase [a-z0-9_.-]+. Topics, keys, paths, and sections are free-form and trimmed."
794
- ].join("\n");
795
- }
796
- var FINDING_SUBJECT_GRAMMAR_PROMPT = renderFindingSubjectGrammar(FINDING_SUBJECT_KINDS);
797
- var KIND_EXPECTED_SUBJECTS = {
798
- "failure-mode": ["cluster"],
799
- "knowledge-gap": [
800
- "knowledge.wiki",
801
- "knowledge.claim",
802
- "knowledge.raw",
803
- "knowledge.stale",
804
- "tool-doc",
805
- "system-prompt",
806
- "skill",
807
- "mcp",
808
- "subagent",
809
- "workflow",
810
- "memory",
811
- "websearch.outdated",
812
- "prior-run-summary"
813
- ],
814
- "knowledge-poisoning": [
815
- "knowledge.wiki",
816
- "knowledge.claim",
817
- "knowledge.raw",
818
- "tool-doc",
819
- "system-prompt",
820
- "skill",
821
- "mcp",
822
- "hook",
823
- "memory",
824
- "websearch.outdated",
825
- "prior-run-summary"
826
- ],
827
- improvement: [
828
- "system-prompt",
829
- "skill",
830
- "tool-doc",
831
- "new-tool",
832
- "mcp",
833
- "hook",
834
- "subagent",
835
- "workflow",
836
- "rollout-policy",
837
- "agent-profile",
838
- "code",
839
- "rag",
840
- "memory",
841
- "scaffolding",
842
- "output-schema",
843
- "knowledge.wiki",
844
- "knowledge.claim"
845
- ]
846
- };
847
- function findingSubjectGrammarPromptFor(kindId) {
848
- const kinds = KIND_EXPECTED_SUBJECTS[kindId];
849
- if (!kinds) throw new Error(`unknown analyst kind: ${kindId}`);
850
- return renderFindingSubjectGrammar(kinds);
851
- }
852
- var FindingSubjectStringSchema = z.string().refine((s) => parseFindingSubject(s) !== null, {
853
- message: "subject does not match the finding-subject grammar"
854
- });
855
-
856
- // src/analyst/parse-tolerant.ts
857
- function stripCodeFences(text) {
858
- const t = text.trim();
859
- const fence = /^```[a-zA-Z0-9]*\s*\n?([\s\S]*?)\n?```$/;
860
- const m = t.match(fence);
861
- return m ? m[1].trim() : t;
862
- }
863
- function dropTrailingCommas(s) {
864
- return s.replace(/,(\s*[}\]])/g, "$1");
865
- }
866
- function coerceJson(text) {
867
- const candidate = dropTrailingCommas(stripCodeFences(text));
868
- try {
869
- return JSON.parse(candidate);
870
- } catch {
871
- return void 0;
872
- }
873
- }
874
- function coerceToFindingRows(raw) {
875
- let value = raw;
876
- if (typeof value === "string") {
877
- const parsed = coerceJson(value);
878
- if (parsed === void 0) return [];
879
- value = parsed;
880
- }
881
- if (Array.isArray(value)) return value;
882
- if (value && typeof value === "object") {
883
- const inner = value.findings;
884
- if (Array.isArray(inner)) return inner;
885
- return [value];
886
- }
887
- return [];
888
- }
889
-
890
- // src/analyst/finding-signature.ts
891
- import { z as z2 } from "zod";
892
- var ANALYST_SEVERITIES = ["critical", "high", "medium", "low", "info"];
893
- var RawAnalystEvidenceSchema = z2.object({
894
- uri: z2.string().trim().min(1).max(2e3),
895
- excerpt: z2.string().max(2e3).optional()
896
- }).strict();
897
- var RawAnalystFindingBaseShape = {
898
- severity: z2.enum(ANALYST_SEVERITIES),
899
- claim: z2.string().min(1).max(2e3),
900
- subject: z2.string().max(400).refine((subject) => parseFindingSubject(subject) !== null, {
901
- message: "subject does not match the finding-subject grammar"
902
- }).optional(),
903
- confidence: z2.number().min(0).max(1),
904
- rationale: z2.string().max(4e3).optional(),
905
- recommended_action: z2.string().max(2e3).optional()
906
- };
907
- var RawAnalystFindingSchema = z2.object({
908
- ...RawAnalystFindingBaseShape,
909
- evidence_uri: z2.string().min(1).max(2e3).refine((uri) => uri.trim().length > 0, { message: "evidence_uri must not be blank" }),
910
- evidence_excerpt: z2.string().max(2e3).optional()
911
- }).strict();
912
- var CanonicalRawAnalystFindingObjectSchema = z2.object({
913
- ...RawAnalystFindingBaseShape,
914
- evidence: z2.array(RawAnalystEvidenceSchema).min(1)
915
- }).strict();
916
- var CanonicalRawAnalystFindingSchema = z2.preprocess(
917
- normalizeLegacySingleCitation,
918
- CanonicalRawAnalystFindingObjectSchema
919
- );
920
- var RAW_FINDING_SCHEMA_PROMPT = `Each finding MUST be a strict JSON object with:
921
- - severity: "critical" | "high" | "medium" | "low" | "info"
922
- - claim: one-sentence statement (max 2000 chars)
923
- - subject?: one exact subject form listed by this kind; omit rather than guess
924
- - evidence: REQUIRED non-empty array of {"uri": string, "excerpt"?: string}. Use real identifiers with span://, event://, artifact://, metric://, or finding://. Include a short exact quote in excerpt when available. If nothing is citable, do not emit the finding.
925
- - confidence: number 0..1 (0.9+ exact evidence; 0.6-0.8 inferred pattern; <0.5 speculative)
926
- - rationale?: one or two reasoning sentences
927
- - recommended_action?: concrete imperative change; omit for descriptive findings
928
-
929
- Unknown fields are rejected. Do not emit area; the factory assigns it. Emit [] when there are no findings. Never fabricate evidence.`;
930
- function evidenceRefsFromRawFinding(finding) {
931
- return finding.evidence.map(({ uri, excerpt }) => ({
932
- kind: evidenceKindFromUri(uri),
933
- uri,
934
- excerpt
935
- }));
936
- }
937
- function parseRawFinding(row, log) {
938
- return parseFindingWithSchema(RawAnalystFindingSchema, row, log);
939
- }
940
- function parseCanonicalRawFinding(row, log) {
941
- return parseFindingWithSchema(CanonicalRawAnalystFindingSchema, row, log);
942
- }
943
- function parseFindingWithSchema(schema, row, log) {
944
- const result = schema.safeParse(row);
945
- if (result.success) return result.data;
946
- if (typeof row === "string") {
947
- const coerced = coerceJson(row);
948
- if (coerced !== void 0) {
949
- const retry = schema.safeParse(coerced);
950
- if (retry.success) return retry.data;
951
- }
952
- }
953
- log?.("finding rejected: schema failure", {
954
- issues: result.error.issues.map((i) => ({
955
- path: i.path.join("."),
956
- code: i.code,
957
- message: i.message
958
- }))
959
- });
960
- return null;
961
- }
962
- function normalizeLegacySingleCitation(value) {
963
- if (value === null || typeof value !== "object" || Array.isArray(value)) return value;
964
- const row = value;
965
- if ("evidence" in row) {
966
- if (!("evidence_uri" in row) || !Array.isArray(row.evidence)) return value;
967
- const { evidence, evidence_uri: uri2, evidence_excerpt: excerpt2, ...rest2 } = row;
968
- let alreadyPresent = false;
969
- const mergedEvidence = evidence.map((citation) => {
970
- if (citation === null || typeof citation !== "object" || Array.isArray(citation) || citation.uri !== uri2) {
971
- return citation;
972
- }
973
- alreadyPresent = true;
974
- const current = citation;
975
- return excerpt2 === void 0 || current.excerpt !== void 0 ? citation : { ...current, excerpt: excerpt2 };
976
- });
977
- return {
978
- ...rest2,
979
- evidence: alreadyPresent ? mergedEvidence : [...evidence, { uri: uri2, ...excerpt2 === void 0 ? {} : { excerpt: excerpt2 } }]
980
- };
981
- }
982
- if (!("evidence_uri" in row)) return value;
983
- const { evidence_uri: uri, evidence_excerpt: excerpt, ...rest } = row;
984
- return {
985
- ...rest,
986
- evidence: [{ uri, ...excerpt === void 0 ? {} : { excerpt } }]
987
- };
988
- }
989
- function toLegacyRawAnalystFinding(finding) {
990
- const primaryEvidence = finding.evidence[0];
991
- if (!primaryEvidence) {
992
- throw new TypeError("Canonical raw analyst findings require at least one evidence citation");
993
- }
994
- const { evidence: _evidence, ...rest } = finding;
995
- return {
996
- ...rest,
997
- evidence_uri: primaryEvidence.uri,
998
- ...primaryEvidence.excerpt === void 0 ? {} : { evidence_excerpt: primaryEvidence.excerpt }
999
- };
1000
- }
1001
- function applyLegacyRawFindingCallback(finding, callback, log) {
1002
- const callbackResult = callback(toLegacyRawAnalystFinding(finding));
1003
- if (callbackResult === null) return null;
1004
- const parsed = parseCanonicalRawFinding(callbackResult, log);
1005
- if (!parsed) return null;
1006
- const primaryEvidence = parsed.evidence[0];
1007
- if (!primaryEvidence) {
1008
- throw new TypeError("Canonical raw analyst findings require at least one evidence citation");
1009
- }
1010
- const remainingEvidence = finding.evidence.slice(1).filter((citation) => citation.uri !== primaryEvidence.uri);
1011
- return {
1012
- ...parsed,
1013
- evidence: [primaryEvidence, ...remainingEvidence]
1014
- };
1015
- }
1016
- function evidenceKindFromUri(uri) {
1017
- if (uri.startsWith("span://")) return "span";
1018
- if (uri.startsWith("event://")) return "event";
1019
- if (uri.startsWith("finding://")) return "finding";
1020
- if (uri.startsWith("metric://")) return "metric";
1021
- return "artifact";
1022
- }
1023
-
1024
- // src/analyst/structure-findings.ts
1025
- var SYSTEM = [
1026
- "You convert a free-form trace-analysis report into a STRICT JSON array of findings.",
1027
- "Output ONLY the JSON array \u2014 no prose, no code fences.",
1028
- RAW_FINDING_SCHEMA_PROMPT,
1029
- "Omit subject when the report does not contain an exact valid locus.",
1030
- "If the report asserts NO problems, output exactly []."
1031
- ].join(" ");
1032
- function buildRows(raw, opts) {
1033
- const rows = coerceToFindingRows(raw);
1034
- const out = [];
1035
- for (const row of rows) {
1036
- const parsed = parseCanonicalRawFinding(row);
1037
- if (!parsed) continue;
1038
- const callbackProcessed = opts.processRow ? applyLegacyRawFindingCallback(parsed, opts.processRow) : parsed;
1039
- if (!callbackProcessed) continue;
1040
- const processed = opts.processCanonicalRow ? opts.processCanonicalRow(callbackProcessed) : callbackProcessed;
1041
- if (!processed) continue;
1042
- out.push(
1043
- makeFinding({
1044
- analyst_id: opts.analystId,
1045
- area: opts.area,
1046
- subject: processed.subject,
1047
- claim: processed.claim,
1048
- rationale: processed.rationale,
1049
- severity: processed.severity,
1050
- confidence: processed.confidence,
1051
- evidence_refs: evidenceRefsFromRawFinding(processed),
1052
- recommended_action: processed.recommended_action,
1053
- ...opts.findingMetadata ? { metadata: { ...opts.findingMetadata } } : {}
1054
- })
1055
- );
1056
- }
1057
- return out;
1058
- }
1059
- async function structureFindings(opts) {
1060
- const maxReasks = opts.maxReasks ?? 1;
1061
- if (!Number.isSafeInteger(maxReasks) || maxReasks < 0) {
1062
- throw new RangeError("structureFindings: maxReasks must be a non-negative safe integer");
1063
- }
1064
- const llm = { baseUrl: opts.baseUrl, apiKey: opts.apiKey, fetch: opts.fetchImpl };
1065
- const costLedger = opts.costLedger ?? new CostLedger();
1066
- let user = `TRACE-ANALYSIS REPORT:
1067
- ${opts.report}
1068
-
1069
- Return the findings JSON array.`;
1070
- for (let attempt = 0; attempt <= maxReasks; attempt++) {
1071
- const request = {
1072
- model: opts.model,
1073
- messages: [
1074
- { role: "system", content: SYSTEM },
1075
- { role: "user", content: user }
1076
- ],
1077
- maxTokens: opts.maxTokens ?? 2e3
1078
- };
1079
- const paid = await costLedger.runPaidCall({
1080
- channel: "analyst",
1081
- phase: opts.costPhase ?? "analyst.structure-findings",
1082
- actor: "structure-findings",
1083
- model: opts.model,
1084
- signal: opts.signal,
1085
- maximumCharge: maximumChargeForLlmRequest(request, llm),
1086
- tags: { ...opts.costTags, analystId: opts.analystId, attempt: String(attempt) },
1087
- execute: (signal, callId) => callLlm(request, { ...llm, signal, idempotencyKey: callId }),
1088
- receipt: costReceiptFromLlm,
1089
- receiptFromError: costReceiptFromLlmError
1090
- });
1091
- if (!paid.succeeded) throw paid.error;
1092
- const res = paid.value;
1093
- const text = res.content.trim();
1094
- const findings = buildRows(text, opts);
1095
- if (findings.length > 0) return { findings, outcome: "ok" };
1096
- if (opts.report.trim().length < 200) return { findings: [], outcome: "ok" };
1097
- user = `${user}
1098
-
1099
- That produced no valid findings. The report DOES describe issues \u2014 re-extract them as the strict JSON array described in the system prompt. Output ONLY the array.`;
1100
- }
1101
- return { findings: [], outcome: "extraction_failed" };
1102
- }
1103
-
1104
- // src/analyst/ax-cost-service.ts
1105
- function meterAxChatService(ai2, options) {
1106
- assertPositiveInteger(options.maxOutputTokens, "maxOutputTokens");
1107
- const source = ai2;
1108
- if (typeof source.chat !== "function") {
1109
- throw new TypeError("meterAxChatService: Ax service must implement chat()");
1110
- }
1111
- const providerChat = source.chat.bind(ai2);
1112
- const chat = async (request, callOptions = {}) => {
1113
- const boundedRequest = boundOutputTokens(request, options.maxOutputTokens);
1114
- const model = modelName(boundedRequest.model) || options.defaultModel || "";
1115
- const canTurnOffThinking = canDisableThinking(ai2, model);
1116
- const combined = combineSignals(options.signal, callOptions.abortSignal);
1117
- try {
1118
- const paid = await options.ledger.runPaidCall({
1119
- channel: "analyst",
1120
- phase: options.phase ?? "analyst.ax.chat",
1121
- actor: options.actor,
1122
- model,
1123
- tags: options.tags,
1124
- signal: combined.signal,
1125
- maximumCharge: maximumChargeForAxChatRequest(boundedRequest, model),
1126
- execute: async (executionSignal) => {
1127
- const providerOptions = {
1128
- ...callOptions,
1129
- abortSignal: executionSignal,
1130
- retry: { ...callOptions.retry, maxRetries: 0 },
1131
- stream: false,
1132
- showThoughts: false
1133
- };
1134
- if (canTurnOffThinking) providerOptions.thinkingTokenBudget = "none";
1135
- else Reflect.deleteProperty(providerOptions, "thinkingTokenBudget");
1136
- const response = await providerChat(boundedRequest, providerOptions);
1137
- if (response instanceof ReadableStream) {
1138
- throw new Error("meterAxChatService: provider returned a stream after stream:false");
1139
- }
1140
- return response;
1141
- },
1142
- receipt: (response) => costReceiptFromAxResponse(response, model)
1143
- });
1144
- if (!paid.succeeded) throw paid.error;
1145
- return paid.value;
1146
- } finally {
1147
- combined.dispose();
1148
- }
1149
- };
1150
- return new Proxy(ai2, {
1151
- get(target, property) {
1152
- if (property === "chat") return chat;
1153
- const value = Reflect.get(target, property, target);
1154
- return typeof value === "function" ? value.bind(target) : value;
1155
- }
1156
- });
1157
- }
1158
- function maximumChargeForAxChatRequest(request, defaultModel) {
1159
- const model = modelName(request.model) || defaultModel || "";
1160
- const maxTokens = request.modelConfig?.maxTokens;
1161
- const completions = request.modelConfig?.n;
1162
- if (!model || maxTokens === void 0 || completions === void 0) return void 0;
1163
- assertPositiveInteger(maxTokens, "request.modelConfig.maxTokens");
1164
- assertPositiveInteger(completions, "request.modelConfig.n");
1165
- const maximumOutputTokens = maxTokens * completions;
1166
- assertPositiveInteger(maximumOutputTokens, "maximum output tokens");
1167
- if (containsUnboundedOrCacheableContent(request)) return void 0;
1168
- let inputTokens;
1169
- try {
1170
- const pricedRequest = request.model === void 0 ? { ...request, model } : request;
1171
- inputTokens = new TextEncoder().encode(JSON.stringify(pricedRequest)).byteLength * completions;
1172
- } catch {
1173
- return void 0;
1174
- }
1175
- assertPositiveInteger(inputTokens, "maximum input tokens");
1176
- return { model, inputTokens, outputTokens: maximumOutputTokens };
1177
- }
1178
- function boundOutputTokens(request, limit) {
1179
- const requested = request.modelConfig?.maxTokens;
1180
- if (requested !== void 0) {
1181
- assertPositiveInteger(requested, "request.modelConfig.maxTokens");
1182
- }
1183
- const completions = request.modelConfig?.n ?? 1;
1184
- assertPositiveInteger(completions, "request.modelConfig.n");
1185
- const maxTokens = requested === void 0 ? limit : Math.min(requested, limit);
1186
- return {
1187
- ...request,
1188
- modelConfig: { ...request.modelConfig, maxTokens, n: completions }
1189
- };
1190
- }
1191
- function costReceiptFromAxResponse(response, fallbackModel) {
1192
- const usage = response.modelUsage;
1193
- const tokens = usage?.tokens;
1194
- const model = usage?.model || fallbackModel;
1195
- if (!tokens || !validUsage(tokens.promptTokens) || !validUsage(tokens.completionTokens) || !validUsage(tokens.totalTokens) || tokens.totalTokens < tokens.promptTokens + tokens.completionTokens || !validOptionalUsage(tokens.cacheReadTokens) || !validOptionalUsage(tokens.cacheCreationTokens) || !validOptionalUsage(tokens.reasoningTokens) || !validOptionalUsage(tokens.thoughtsTokens)) {
1196
- return {
1197
- model,
1198
- inputTokens: 0,
1199
- outputTokens: 0,
1200
- usageUnknown: true
1201
- };
1202
- }
1203
- const cacheReadTokens = validUsage(tokens.cacheReadTokens) ? tokens.cacheReadTokens : 0;
1204
- const cacheCreationTokens = validUsage(tokens.cacheCreationTokens) ? tokens.cacheCreationTokens : 0;
1205
- const totalCacheTokens = cacheReadTokens + cacheCreationTokens;
1206
- const thoughtsTokens = validUsage(tokens.thoughtsTokens) ? tokens.thoughtsTokens : 0;
1207
- const reasoningTokens = Math.max(
1208
- validUsage(tokens.reasoningTokens) ? tokens.reasoningTokens : 0,
1209
- thoughtsTokens
1210
- );
1211
- const separateReasoningTokens = reasoningTokens > tokens.completionTokens ? reasoningTokens : 0;
1212
- const additionalOutputTokens = Math.max(thoughtsTokens, separateReasoningTokens);
1213
- const outputTokens = tokens.completionTokens + additionalOutputTokens;
1214
- const directTotal = tokens.promptTokens + tokens.completionTokens;
1215
- const validTotals = /* @__PURE__ */ new Set([
1216
- directTotal,
1217
- directTotal + totalCacheTokens,
1218
- directTotal + additionalOutputTokens,
1219
- directTotal + totalCacheTokens + additionalOutputTokens
1220
- ]);
1221
- if (!validTotals.has(tokens.totalTokens)) {
1222
- return {
1223
- model,
1224
- inputTokens: 0,
1225
- outputTokens: 0,
1226
- usageUnknown: true
1227
- };
1228
- }
1229
- return {
1230
- model,
1231
- inputTokens: tokens.promptTokens,
1232
- outputTokens,
1233
- ...reasoningTokens > 0 ? { reasoningTokens } : {},
1234
- ...cacheReadTokens > 0 ? { cachedTokens: cacheReadTokens } : {},
1235
- ...cacheCreationTokens > 0 ? { cacheWriteTokens: cacheCreationTokens } : {}
1236
- };
1237
- }
1238
- function combineSignals(first, second) {
1239
- if (!first) return { signal: second, dispose: () => {
1240
- } };
1241
- if (!second || first === second) return { signal: first, dispose: () => {
1242
- } };
1243
- if (typeof AbortSignal.any === "function") {
1244
- return { signal: AbortSignal.any([first, second]), dispose: () => {
1245
- } };
1246
- }
1247
- const controller = new AbortController();
1248
- const dispose = () => {
1249
- first.removeEventListener("abort", abortFromFirst);
1250
- second.removeEventListener("abort", abortFromSecond);
1251
- };
1252
- const abortFrom = (source) => {
1253
- if (!controller.signal.aborted) controller.abort(source.reason);
1254
- dispose();
1255
- };
1256
- const abortFromFirst = () => abortFrom(first);
1257
- const abortFromSecond = () => abortFrom(second);
1258
- if (first.aborted) abortFrom(first);
1259
- else if (second.aborted) abortFrom(second);
1260
- else {
1261
- first.addEventListener("abort", abortFromFirst, { once: true });
1262
- second.addEventListener("abort", abortFromSecond, { once: true });
1263
- }
1264
- return { signal: controller.signal, dispose };
1265
- }
1266
- function containsUnboundedOrCacheableContent(request) {
1267
- if (request.functions?.some((fn) => fn.cache === true)) return true;
1268
- return request.chatPrompt.some((message) => {
1269
- if (message.cache === true) return true;
1270
- if (!("content" in message) || !Array.isArray(message.content)) return false;
1271
- return message.content.some((part) => part.cache === true || part.type !== "text");
1272
- });
1273
- }
1274
- function modelName(value) {
1275
- return typeof value === "string" ? value : "";
1276
- }
1277
- function canDisableThinking(ai2, model) {
1278
- const namedAi = ai2;
1279
- const serviceName = typeof namedAi.getName === "function" ? namedAi.getName() : "";
1280
- const modelId = model.slice(model.lastIndexOf("/") + 1);
1281
- return serviceName !== "GoogleGeminiAI" || !/^gemini-3(?:[.-]|$)/i.test(modelId);
1282
- }
1283
- function validUsage(value) {
1284
- return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
1285
- }
1286
- function validOptionalUsage(value) {
1287
- return value === void 0 || validUsage(value);
1288
- }
1289
- function assertPositiveInteger(value, field) {
1290
- if (!Number.isSafeInteger(value) || value <= 0) {
1291
- throw new RangeError(`meterAxChatService: ${field} must be a positive integer`);
1292
- }
1293
- }
1294
-
1295
- // src/analyst/usage-receipt.ts
1296
- var DEFAULT_USAGE_SETTLEMENT_TIMEOUT_MS = 5e3;
1297
- function usageReceiptFromCostLedger(ledger, filter = "analyst") {
1298
- const resolvedFilter = typeof filter === "string" ? { channel: filter } : filter;
1299
- const summary = ledger.summary(resolvedFilter);
1300
- const receipts = ledger.list(resolvedFilter);
1301
- const hasReasoningUsage = receipts.some((receipt) => receipt.reasoningTokens !== void 0);
1302
- const hasCacheWriteUsage = receipts.some((receipt) => receipt.cacheWriteTokens !== void 0);
1303
- const costUncaptured = summary.pendingCalls > 0 || receipts.some((receipt) => receipt.costUnknown);
1304
- const cost = costUncaptured ? { kind: "uncaptured", usd: null } : receipts.every((receipt) => receipt.actualCostUsd !== void 0) ? { kind: "observed", usd: summary.totalCostUsd } : { kind: "estimated", usd: summary.totalCostUsd };
1305
- return {
1306
- calls: summary.totalCalls + summary.pendingCalls,
1307
- tokens: summary.usageComplete ? {
1308
- input: summary.inputTokens,
1309
- output: summary.outputTokens,
1310
- ...hasReasoningUsage ? { reasoning: summary.reasoningTokens ?? 0 } : {},
1311
- ...summary.cachedTokens > 0 ? { cached: summary.cachedTokens } : {},
1312
- ...hasCacheWriteUsage ? { cacheWrite: summary.cacheWriteTokens ?? 0 } : {}
1313
- } : null,
1314
- cost,
1315
- ...cost.kind === "uncaptured" ? { knownCostUsd: summary.totalCostUsd } : {}
1316
- };
1317
- }
1318
- async function settleUsageReceiptFromCostLedger(ledger, options = {}) {
1319
- const { timeoutMs: requestedTimeoutMs, ...requestedFilter } = options;
1320
- const filter = {
1321
- channel: requestedFilter.channel ?? "analyst",
1322
- ...requestedFilter.phase === void 0 ? {} : { phase: requestedFilter.phase },
1323
- ...requestedFilter.tags === void 0 ? {} : { tags: requestedFilter.tags }
1324
- };
1325
- const timeoutMs = validateUsageSettlementTimeout(requestedTimeoutMs);
1326
- const initial = ledger.summary(filter);
1327
- const waitResult = initial.pendingCalls === 0 ? true : ledger.waitForIdle ? await ledger.waitForIdle({ timeoutMs }) : false;
1328
- const pendingCalls = ledger.summary(filter).pendingCalls;
1329
- return {
1330
- settled: waitResult && pendingCalls === 0,
1331
- pendingCalls,
1332
- receipt: usageReceiptFromCostLedger(ledger, filter)
1333
- };
1334
- }
1335
- function validateUsageSettlementTimeout(timeoutMs) {
1336
- const resolved = timeoutMs ?? DEFAULT_USAGE_SETTLEMENT_TIMEOUT_MS;
1337
- if (!Number.isSafeInteger(resolved) || resolved < 0 || resolved > 2147483647) {
1338
- throw new TypeError(
1339
- "settlementTimeoutMs must be a non-negative safe integer no greater than 2147483647"
1340
- );
1341
- }
1342
- return resolved;
1343
- }
1344
-
1345
- // src/analyst/kind-factory.ts
1346
- function createTraceAnalystKind(spec, opts) {
1347
- rejectRemovedKindOptions(spec);
1348
- const version = opts.versionSuffix ? `${spec.version}+${opts.versionSuffix}` : spec.version;
1349
- const model = resolveAnalystModel(opts.ai, opts.model);
1350
- const minimumEvidenceCitations = spec.minimumEvidenceCitations ?? 1;
1351
- if (!Number.isInteger(minimumEvidenceCitations) || minimumEvidenceCitations < 1) {
1352
- throw new TypeError("minimumEvidenceCitations must be a positive integer");
1353
- }
1354
- const settlementTimeoutMs = validateUsageSettlementTimeout(opts.settlementTimeoutMs);
1355
- return {
1356
- id: spec.id,
1357
- description: spec.description,
1358
- inputKind: "trace-store",
1359
- cost: { ...spec.cost, settlement_timeout_ms: settlementTimeoutMs },
1360
- version,
1361
- async analyze(store, ctx) {
1362
- const maxOutputTokens = spec.maxOutputTokens ?? 4096;
1363
- const costLedger = ctx.costLedger ?? new CostLedger(ctx.budgetUsd);
1364
- const costTags = {
1365
- ...ctx.tags ?? {},
1366
- analystId: spec.id,
1367
- ...ctx.correlationId ? { analystRunId: ctx.correlationId } : {}
1368
- };
1369
- const meteredAi = meterAxChatService(opts.ai, {
1370
- ledger: costLedger,
1371
- actor: spec.id,
1372
- maxOutputTokens,
1373
- defaultModel: model,
1374
- phase: ctx.costPhase,
1375
- signal: ctx.signal,
1376
- tags: costTags
1377
- });
1378
- try {
1379
- const tools = spec.buildTools(store);
1380
- const maxSubqueries = spec.subqueries?.maxCalls ?? 0;
1381
- const maxParallel = spec.subqueries?.maxParallel ?? 2;
1382
- const priorContext = renderPriorFindings(ctx.priorFindings);
1383
- const upstreamContext = renderUpstreamFindings(ctx.upstreamFindings);
1384
- const actorDescription = spec.actorDescription.trim() + priorContext + upstreamContext + "\n\n" + RAW_FINDING_SCHEMA_PROMPT + (minimumEvidenceCitations > 1 ? `
1385
-
1386
- This kind requires at least ${minimumEvidenceCitations} evidence citations per finding; rows with fewer are rejected.` : "") + "\n\nFirst write `report`: a concise free-form prose diagnosis of what the traces show \u2014 what succeeded, what was suboptimal or failed \u2014 with concrete trace ids and numbers. THEN return the structured `findings` array (it MAY be empty when there is nothing to report).";
1387
- ctx.log?.(`analyst.kind ${spec.id} forward`, {
1388
- max_subqueries: maxSubqueries,
1389
- tool_count: tools.length,
1390
- tags: ctx.tags
1391
- });
1392
- const completed = await runTraceAnalysisLoop({
1393
- id: spec.id,
1394
- description: spec.description,
1395
- prompt: actorDescription,
1396
- question: deriveQuestion(ctx, spec),
1397
- ai: meteredAi,
1398
- model,
1399
- tools,
1400
- findingType: "object",
1401
- maxSubqueries,
1402
- maxParallelSubqueries: maxParallel,
1403
- maxTurns: spec.maxTurns ?? 12,
1404
- maxRuntimeChars: spec.maxRuntimeChars ?? 6e3,
1405
- ...ctx.signal ? { signal: ctx.signal } : {}
1406
- });
1407
- const { report, findings: submittedFindings } = completed;
1408
- const expectedSubjects = KIND_EXPECTED_SUBJECTS[spec.id];
1409
- const out = [];
1410
- const rawRows = submittedFindings;
1411
- let rejectedWrongKind = 0;
1412
- let rejectedInsufficientEvidence = 0;
1413
- const processRow = (parsed) => {
1414
- const postProcessed = spec.postProcess ? applyLegacyRawFindingCallback(
1415
- parsed,
1416
- (row) => spec.postProcess?.(row, ctx) ?? null,
1417
- ctx.log
1418
- ) : parsed;
1419
- if (!postProcessed) return null;
1420
- if (expectedSubjects && postProcessed.subject !== void 0) {
1421
- const parsedSubject = parseFindingSubject(postProcessed.subject);
1422
- if (parsedSubject === null) {
1423
- ctx.log?.("finding rejected: subject failed to parse", {
1424
- kind: spec.id,
1425
- subject: postProcessed.subject
1426
- });
1427
- rejectedWrongKind += 1;
1428
- return null;
1429
- }
1430
- if (!expectedSubjects.includes(parsedSubject.kind)) {
1431
- ctx.log?.("finding rejected: subject variant not allowed for this kind", {
1432
- kind: spec.id,
1433
- subject_kind: parsedSubject.kind,
1434
- subject: postProcessed.subject,
1435
- allowed: expectedSubjects
1436
- });
1437
- rejectedWrongKind += 1;
1438
- return null;
1439
- }
1440
- }
1441
- const distinctEvidenceCitations = new Set(
1442
- postProcessed.evidence.map((citation) => citation.uri.trim())
1443
- ).size;
1444
- if (distinctEvidenceCitations < minimumEvidenceCitations) {
1445
- ctx.log?.("finding rejected: insufficient evidence citations", {
1446
- kind: spec.id,
1447
- required: minimumEvidenceCitations,
1448
- received: postProcessed.evidence.length,
1449
- distinct: distinctEvidenceCitations
1450
- });
1451
- rejectedInsufficientEvidence += 1;
1452
- return null;
1453
- }
1454
- return postProcessed;
1455
- };
1456
- for (const row of rawRows) {
1457
- const parsed = parseCanonicalRawFinding(row, ctx.log);
1458
- if (!parsed) continue;
1459
- const postProcessed = processRow(parsed);
1460
- if (!postProcessed) continue;
1461
- out.push(toAnalystFinding(spec, version, postProcessed));
1462
- }
1463
- ctx.log?.(`analyst.kind ${spec.id} done`, {
1464
- emitted: rawRows.length,
1465
- accepted: out.length,
1466
- rejected_wrong_subject: rejectedWrongKind,
1467
- rejected_insufficient_evidence: rejectedInsufficientEvidence
1468
- });
1469
- if (out.length === 0 && report.trim().length >= 200) {
1470
- if (opts.recovery) {
1471
- const wrongKindBefore = rejectedWrongKind;
1472
- const insufficientEvidenceBefore = rejectedInsufficientEvidence;
1473
- const recovered = await structureFindings({
1474
- report,
1475
- analystId: spec.id,
1476
- area: spec.area,
1477
- model: opts.recovery.model ?? model,
1478
- baseUrl: opts.recovery.baseUrl,
1479
- apiKey: opts.recovery.apiKey,
1480
- fetchImpl: opts.recovery.fetchImpl,
1481
- costLedger,
1482
- costPhase: ctx.costPhase,
1483
- costTags,
1484
- signal: ctx.signal,
1485
- maxTokens: Math.min(maxOutputTokens, 2e3),
1486
- processCanonicalRow: processRow,
1487
- findingMetadata: { kind_version: version }
1488
- });
1489
- out.push(...recovered.findings);
1490
- ctx.log?.(`analyst.kind ${spec.id} recovery`, {
1491
- outcome: recovered.outcome,
1492
- recovered: recovered.findings.length,
1493
- rejected_wrong_subject: rejectedWrongKind - wrongKindBefore,
1494
- rejected_insufficient_evidence: rejectedInsufficientEvidence - insufficientEvidenceBefore
1495
- });
1496
- }
1497
- if (out.length === 0) {
1498
- const fallback = processRow({
1499
- claim: "Analyst produced a diagnosis but no structured findings \u2014 see report.",
1500
- rationale: report.slice(0, 1500),
1501
- severity: "info",
1502
- confidence: 0.3,
1503
- evidence: [{ uri: "report://summary", excerpt: report.slice(0, 2e3) }]
1504
- });
1505
- if (fallback) {
1506
- out.push(toAnalystFinding(spec, version, fallback, { outcome: "extraction_failed" }));
1507
- } else {
1508
- throw new Error(
1509
- `Trace analyst '${spec.id}' produced a substantive report, but no finding satisfied its acceptance rules`
1510
- );
1511
- }
1512
- }
1513
- }
1514
- return out;
1515
- } finally {
1516
- const usage = await settleUsageReceiptFromCostLedger(costLedger, {
1517
- tags: {
1518
- analystId: spec.id,
1519
- ...ctx.correlationId ? { analystRunId: ctx.correlationId } : {}
1520
- },
1521
- timeoutMs: settlementTimeoutMs
1522
- });
1523
- if (!usage.settled) {
1524
- ctx.log?.(`analyst.kind ${spec.id} provider settlement timed out`, {
1525
- pending_calls: usage.pendingCalls,
1526
- timeout_ms: settlementTimeoutMs
1527
- });
1528
- }
1529
- ctx.recordUsage?.(usage.receipt);
1530
- }
1531
- }
1532
- };
1533
- }
1534
- function rejectRemovedKindOptions(spec) {
1535
- const supplied = spec;
1536
- const migrations = [
1537
- ["recursion", "subqueries"],
1538
- ["responderDescription", "actorDescription"],
1539
- ["maxDepth", "subqueries"],
1540
- ["maxParallelSubagents", "subqueries.maxParallel"],
1541
- ["subagentDescription", "actorDescription"]
1542
- ];
1543
- for (const [removed, replacement] of migrations) {
1544
- if (removed in supplied) {
1545
- throw new TypeError(
1546
- `createTraceAnalystKind: '${removed}' is unsupported; use '${replacement}'`
1547
- );
1548
- }
1549
- }
1550
- }
1551
- function deriveQuestion(ctx, spec) {
1552
- const focus = ctx.tags?.focus?.trim();
1553
- const task = `Analyze this trace dataset with the available tools and report ${spec.area} findings. ${spec.description}`;
1554
- return focus ? `${task} Focus: ${focus}.` : task;
1555
- }
1556
- function toAnalystFinding(spec, version, raw, metadata = {}) {
1557
- return makeFinding({
1558
- analyst_id: spec.id,
1559
- area: spec.area,
1560
- subject: raw.subject,
1561
- claim: raw.claim,
1562
- rationale: raw.rationale,
1563
- severity: raw.severity,
1564
- confidence: raw.confidence,
1565
- evidence_refs: evidenceRefsFromRawFinding(raw),
1566
- recommended_action: raw.recommended_action,
1567
- metadata: { kind_version: version, ...metadata }
1568
- });
1569
- }
1570
- function renderPriorFindings(prior) {
1571
- if (!prior || prior.length === 0) return "";
1572
- const MAX_ROWS = 40;
1573
- const rows = prior.slice(0, MAX_ROWS).map((f) => {
1574
- const subject = f.subject ? ` [${f.subject}]` : "";
1575
- return ` - id=${f.finding_id} ${f.severity}${subject} ${truncateForContext(f.claim, 160)}`;
1576
- });
1577
- const overflow = prior.length > MAX_ROWS ? `
1578
- ... +${prior.length - MAX_ROWS} more prior findings (older history truncated)` : "";
1579
- return [
1580
- "",
1581
- "",
1582
- "PRIOR FINDINGS (from a previous run on related data):",
1583
- "When the work you do now matches a row below, REUSE the `finding_id` (pass it as `id_basis`) so the cross-run diff stays stable.",
1584
- "A finding that reappears with no remediation evidence SHOULD raise its `confidence` and may justify a higher `severity`.",
1585
- ...rows,
1586
- overflow
1587
- ].filter(Boolean).join("\n");
1588
- }
1589
- function renderUpstreamFindings(upstream) {
1590
- if (!upstream || upstream.length === 0) return "";
1591
- const MAX_ROWS = 40;
1592
- const rows = upstream.slice(0, MAX_ROWS).map((finding) => {
1593
- const subject = finding.subject ? ` [${finding.subject}]` : "";
1594
- const action = finding.recommended_action ? ` action=${truncateForContext(finding.recommended_action, 120)}` : "";
1595
- const evidence = finding.evidence_refs[0] ? ` evidence=${truncateForContext(finding.evidence_refs[0].uri, 120)}` : "";
1596
- return ` - id=${finding.finding_id} source=${finding.analyst_id} ${finding.severity}${subject} claim=${truncateForContext(finding.claim, 160)}${action}${evidence}`;
1597
- });
1598
- const overflow = upstream.length > MAX_ROWS ? `
1599
- ... +${upstream.length - MAX_ROWS} more upstream findings (truncated)` : "";
1600
- return [
1601
- "",
1602
- "",
1603
- "UPSTREAM FINDINGS (produced earlier in this same registry run):",
1604
- "Use these as intermediate evidence. Build on them instead of repeating the same diagnosis, and cite a dependency with `finding://<id>`.",
1605
- ...rows,
1606
- overflow
1607
- ].filter(Boolean).join("\n");
1608
- }
1609
- function truncateForContext(s, max) {
1610
- if (s.length <= max) return s;
1611
- return `${s.slice(0, max - 1).trimEnd()}\u2026`;
1612
- }
1613
-
1614
- // src/analyst/tool-groups.ts
1615
- var TOOL_NAMES_BY_GROUP = {
1616
- all: /* @__PURE__ */ new Set(),
1617
- discovery: /* @__PURE__ */ new Set(["getDatasetOverview", "queryTraces", "countTraces"]),
1618
- discoveryAndRead: /* @__PURE__ */ new Set([
1619
- "getDatasetOverview",
1620
- "queryTraces",
1621
- "countTraces",
1622
- "viewTrace",
1623
- "viewSpans"
1624
- ]),
1625
- discoveryAndSearch: /* @__PURE__ */ new Set([
1626
- "getDatasetOverview",
1627
- "queryTraces",
1628
- "countTraces",
1629
- "searchTrace",
1630
- "searchSpan"
1631
- ]),
1632
- targeted: /* @__PURE__ */ new Set(["getDatasetOverview", "queryTraces", "viewSpans", "searchSpan"])
1633
- };
1634
- function buildTraceToolsForGroup(group, store) {
1635
- const all = buildTraceAnalystTools({ store });
1636
- if (group === "all") return all;
1637
- const allow = TOOL_NAMES_BY_GROUP[group];
1638
- if (!allow) throw new Error(`unknown trace tool group: ${group}`);
1639
- return all.filter((tool) => allow.has(tool.name));
1640
- }
1641
-
1642
- // src/analyst/kinds/failure-mode.ts
1643
- var subjectGrammar = findingSubjectGrammarPromptFor("failure-mode");
1644
- var ACTOR_PROMPT = `You are a failure-mode classifier for an OTLP trace dataset. Your job is to identify the **distinct ways agents failed** in this dataset, not to grade individual runs.
1645
-
1646
- ${subjectGrammar}
1647
-
1648
- DISCOVERY \u2192 CLUSTER \u2192 CITE protocol:
1649
-
1650
- 1. Call \`traces.getDatasetOverview({})\` first. Use \`has_errors\`, \`models\`, \`agent_names\`, \`tools\`, and \`sample_trace_ids\` to size the failure surface.
1651
- 2. Use \`traces.queryTraces({ filters: { has_errors: true }, limit })\` to pull error-bearing traces. Combine with \`traces.countTraces\` to see what fraction of the dataset failed.
1652
- 3. For each candidate failure cluster, use \`traces.searchTrace\` with regex like \`STATUS_CODE_ERROR\`, \`MaxTurnsExceeded\`, \`assertion\`, \`unauthorized\`, \`timeout\`, \`429\`, \`5\\d\\d\`, the agent's specific error strings, or the names of its tools. Pull one or two representative traces per cluster, **not all** of them.
1653
- 4. **Cluster, do not enumerate.** Two failures with the same root cause should be ONE finding citing both traces, not two findings. The point of this analyst is to compress N runs into K modes.
1654
- 5. For each defensible cluster, emit ONE finding. Use a lowercase cluster label matching the subject grammar ("tool-call-loop", "auth-revoked-mid-run", ...). Rate it critical when it blocks the run, high when the run finishes degraded, and medium when it slows convergence. Cite representative spans and include exact error, payload, or contradictory-output quotes. Use confidence 0.85+ when multiple traces show the same shape, 0.6-0.8 for a single-trace inference, and <0.5 for speculation. Keep the imperative fix idea short; the improvement analyst expands it.
1655
-
1656
- If the dataset has no failures, return an empty findings array \u2014 do NOT pad with low-confidence speculation.
1657
-
1658
- **Use subqueries over loaded evidence.** After the first scan, load representative span excerpts for each candidate cluster. Then send one bounded \`llmQuery\` per cluster in one batch, including the exact excerpts and asking it to classify the root cause. Subqueries cannot call trace tools. Merge or split clusters yourself from their classifications and the cited source evidence.
1659
-
1660
- OBSERVABILITY rules:
1661
- - Each non-final turn must emit at least one \`console.log\` for evidence.
1662
- - Reuse runtime variables across turns; don't recompute.`;
1663
- var FAILURE_MODE_KIND_SPEC = {
1664
- id: "failure-mode",
1665
- description: "Clusters trace-dataset failures into distinct failure modes with cited evidence and a short recommended action.",
1666
- area: "failure-mode",
1667
- version: "1.2.0",
1668
- actorDescription: ACTOR_PROMPT,
1669
- buildTools: (store) => buildTraceToolsForGroup("all", store),
1670
- subqueries: { maxCalls: 8, maxParallel: 4 },
1671
- maxTurns: 24,
1672
- cost: { kind: "llm" }
1673
- };
1674
-
1675
- // src/analyst/kinds/improvement.ts
1676
- var subjectGrammar2 = findingSubjectGrammarPromptFor("improvement");
1677
- var ACTOR_PROMPT2 = `You are a self-improvement analyst. Your job is to propose **concrete, locus-named edits** the agent's runtime should adopt to fix the failure modes, knowledge gaps, and poisonings present in this dataset.
1678
-
1679
- Upstream analysts have already classified the problems. Your job is to convert each problem into a *change to make* and grade its expected leverage. Each finding is one proposed edit.
1680
-
1681
- ${subjectGrammar2}
1682
-
1683
- DISCOVERY \u2192 CANDIDATE-FIXES \u2192 COMPETE \u2192 CITE protocol:
1684
-
1685
- 1. \`traces.getDatasetOverview({})\` first. Note the agents, tools, and any system-prompt fingerprints (look for the prompt text echoed in early spans).
1686
- 2. For each high-severity failure pattern, generate 2-3 candidate fixes. Real candidate axes:
1687
- - **System-prompt edit** \u2014 add an instruction, remove a misleading one, restructure precedence
1688
- - **Tool description edit** \u2014 rewrite a tool's description so the agent picks it correctly / passes valid args
1689
- - **New tool** \u2014 add a tool the agent kept emulating in code
1690
- - **RAG ingestion** \u2014 add a document or correct a stale one
1691
- - **Memory invalidation** \u2014 clear cached prior-run decisions that no longer apply
1692
- - **Scaffolding** \u2014 add a precondition check, a retry policy, a turn budget, a verification step
1693
- - **Output schema** \u2014 narrow the agent's output to forbid the failure shape
1694
- - **Skill / MCP / hook / subagent** \u2014 change the reusable profile component responsible for the behavior
1695
- - **Workflow / rollout policy** \u2014 change orchestration, budget, sampling, or stopping behavior
1696
- - **Code** \u2014 change an implementation path when profile edits cannot repair the behavior
1697
- 3. **Compare candidate fixes with bounded subqueries.** Load the representative failure excerpts, then send one \`llmQuery\` per candidate-fix axis the same evidence. Ask for likely effect, side effects, and implementation scope. Subqueries cannot call trace tools; trace ids alone are insufficient context.
1698
- 4. After the comparisons return, **pick the winning candidate per cluster** based on expected effect and risk, then emit ONE finding. Keep the alternatives and rejection reasons in the rationale so the recommendation is auditable.
1699
- 5. **Cross-reference upstream findings.** Cite prior failure-mode or knowledge-gap findings as \`finding://<prior-finding-id>\`. This builds the dependency graph that lets the dashboard show "fix #X resolves failure modes A, B, C."
1700
-
1701
- For each winning recommendation, emit ONE finding. Use one exact locus from the subject grammar and state the edit in one sentence. Match leverage to the source failure's severity; use medium for quality-of-life changes and info for cleanup with no behavioral effect. Cite the targeted \`finding://<id>\` when available and the most representative span when useful. Quote the problem being fixed. Use confidence 0.85+ for a mechanical fix to a well-evidenced failure, 0.6-0.8 when judgment is required, and <0.5 for speculation. Explain in at most two sentences why this candidate beat its alternatives. The recommended action must be the literal diff, quoted replacement, tool description, or setting change.
1702
-
1703
- If no upstream failure findings exist in this run, derive your own from the trace dataset using the failure-mode protocol inline (\`searchTrace\` for STATUS_CODE_ERROR / MaxTurnsExceeded / etc.). But prefer to consume upstream findings when present \u2014 the kinds are designed to chain.
1704
-
1705
- Do NOT propose a fix you cannot defend with evidence. "Tighten the prompt" is not a finding; "Add 'When the user asks for X, always Y' to the system prompt section "request-classification"" is.
1706
-
1707
- OBSERVABILITY rules:
1708
- - Each non-final turn must emit at least one \`console.log\` for evidence.`;
1709
- var IMPROVEMENT_KIND_SPEC = {
1710
- id: "improvement",
1711
- description: "Converts upstream failure / gap / poisoning findings into concrete locus-named edits (prompt, tool-doc, RAG, scaffolding) with leverage grades.",
1712
- area: "improvement",
1713
- version: "1.2.0",
1714
- actorDescription: ACTOR_PROMPT2,
1715
- buildTools: (store) => buildTraceToolsForGroup("all", store),
1716
- subqueries: { maxCalls: 8, maxParallel: 4 },
1717
- maxTurns: 30,
1718
- maxRuntimeChars: 12e3,
1719
- cost: { kind: "llm" }
1720
- };
1721
-
1722
- // src/analyst/kinds/knowledge-gap.ts
1723
- var subjectGrammar3 = findingSubjectGrammarPromptFor("knowledge-gap");
1724
- var ACTOR_PROMPT3 = `You are a knowledge-gap analyst for an OTLP trace dataset. Your job is to identify the **specific pieces of information the agent lacked, or that were stale**, that caused poor decisions.
1725
-
1726
- The agent under analysis maintains a curated knowledge base via \`@tangle-network/agent-knowledge\` \u2014 a wiki of \`KnowledgePage\`s with raw source anchors, claims, and relations. The primary expected store of agent-knowable facts IS that wiki. A "knowledge gap" is anything the agent had to discover or guess at run-time that the wiki should have held \u2014 or an outdated/contradictory fact the agent picked up from a non-wiki source.
1727
-
1728
- ${subjectGrammar3}
1729
-
1730
- DISCOVERY \u2192 ATTRIBUTE-TO-LAYER \u2192 CITE protocol:
1731
-
1732
- 1. \`traces.getDatasetOverview({})\` first. Note which agents, tools, and models appear.
1733
- 2. Pull traces where the agent shows gap signals. The strongest signals are:
1734
- - Self-correction turns ("I assumed X but\u2026", "let me re-check", "actually,")
1735
- - Clarifying-question turns where the agent asked the user something the runtime should have surfaced
1736
- - Repeated retrieval / lookup calls for the same artifact with slightly varied queries
1737
- - Tool errors that name a missing argument or unknown resource
1738
- - Web-search calls returning pages dated before a known cutoff for content that changes (versioned APIs, schemas, policies)
1739
- - Agent quoting a tool's docs / system prompt incorrectly because the actual text was insufficient
1740
- - Fabricated identifiers that don't appear in dataset \`sample_trace_ids\`
1741
- Use \`traces.searchTrace\` with patterns like \`I (don.?t|do not) know\`, \`assumed\`, \`unclear\`, \`could you (clarify|tell me|provide)\`, \`not found\`, \`undefined\`, \`unknown\`, \`null\`, dates older than the analysis window, or the agent's specific clarification phrases.
1742
- 3. For each gap, identify the **layer of the runtime that should have prevented it** and use its exact locus from the subject grammar above.
1743
- 4. For each defensible gap, emit ONE finding. Use an exact locus from the subject grammar and name the missing or stale knowledge (for example, "wiki has no page on invoice line-item shape; agent re-derived it from raw spans"). Rate it high when it caused failure or a clarifying question, medium for unnecessary turns, and low for minor inefficiency. Cite the span where the question, correction, retrieval miss, or stale result surfaced and quote it exactly. Use confidence 0.85+ when the agent articulated the gap and 0.6-0.8 when inferred. Recommend a concrete wiki edit for an agent-knowledge locus or a prompt/tool-description edit otherwise.
1744
-
1745
- **Compare layers over loaded evidence.** After the first scan, load the exact excerpts behind candidates across \`agent-knowledge:*\`, \`websearch:outdated\`, \`tool-doc:*\`, \`system-prompt:*\`, and \`memory:*\`. Use one bounded \`llmQuery\` per layer to classify those excerpts. Subqueries cannot call trace tools. Merge their classifications into the final finding set only when the source excerpts support them.
1746
-
1747
- Do NOT report a gap that the agent later recovered from cleanly within the same turn \u2014 that's resilience, not a gap. Cite the *non-recovery* version when both exist.
1748
-
1749
- OBSERVABILITY rules:
1750
- - Each non-final turn must emit at least one \`console.log\` for evidence.`;
1751
- var KNOWLEDGE_GAP_KIND_SPEC = {
1752
- id: "knowledge-gap",
1753
- description: "Identifies missing or stale pieces of knowledge \u2014 primarily against the agent-knowledge wiki \u2014 and attributes each to the runtime layer (wiki page, claim, raw source, websearch, tool-doc, system-prompt, memory) that should have held it.",
1754
- area: "knowledge-gap",
1755
- version: "1.2.0",
1756
- actorDescription: ACTOR_PROMPT3,
1757
- buildTools: (store) => buildTraceToolsForGroup("discoveryAndSearch", store),
1758
- subqueries: { maxCalls: 5, maxParallel: 4 },
1759
- maxTurns: 18,
1760
- cost: { kind: "llm" }
1761
- };
1762
-
1763
- // src/analyst/kinds/knowledge-poisoning.ts
1764
- var subjectGrammar4 = findingSubjectGrammarPromptFor("knowledge-poisoning");
1765
- var ACTOR_PROMPT4 = `You are a knowledge-poisoning analyst for an OTLP trace dataset. Your job is to identify cases where the agent **confidently used wrong information** \u2014 not where it lacked information (that's the knowledge-gap analyst).
1766
-
1767
- ${subjectGrammar4}
1768
-
1769
- DISCOVERY \u2192 DUAL-VERIFY \u2192 CITE protocol:
1770
-
1771
- 1. \`traces.getDatasetOverview({})\` first. Identify the agents, models, and tools.
1772
- 2. Pull traces where the agent's confident action was later contradicted. Strongest signals:
1773
- - Agent stated a fact in one span; a later span surfaced contradictory evidence; the agent then proceeded anyway or fabricated reconciliation.
1774
- - Tool call with stale arguments (an id that no longer exists, an API shape that changed).
1775
- - Agent cited an \`agent-knowledge\` wiki page or claim whose content contradicts the trace's own evidence \u2014 the wiki itself drifted.
1776
- - Web-search result the agent cited that returned an outdated page; agent treated it as canonical.
1777
- - System-prompt instruction the agent followed that ground-truth evidence in the trace contradicts (e.g. prompt says "use endpoint A"; tool reply says "endpoint A deprecated, use B").
1778
- - Repeated wrong-shape parsing despite the tool's actual output proving the shape.
1779
- 3. Use \`traces.searchTrace\` with regex on phrases like \`actually\`, \`turns out\`, \`previously assumed\`, \`old version\`, \`deprecated\`, \`updated to\`, \`now uses\`, or specific entity names you suspect have changed.
1780
- 4. For each candidate poisoning, **DUAL-VERIFY**:
1781
- - Confirm the agent actually acted on the false belief (cite the span where it did)
1782
- - Confirm the belief is actually false in this trace's own evidence (cite the span that contradicts it)
1783
- Only emit a finding when both halves are nailed down. If you can only nail one, drop it \u2014 single-evidence poisoning findings are too speculative to be useful.
1784
-
1785
- **Independently assess both halves.** Load the action excerpt and contradicting excerpt yourself, then send bounded \`llmQuery\` calls the exact evidence for "did the agent act?" and "does the trace contradict the belief?" Subqueries cannot call trace tools. Accept a poisoning only when both assessments and the source excerpts support it.
1786
-
1787
- For each confirmed poisoning, emit ONE finding. Use the source of the false belief as the exact subject. State "agent believed X (from source S); trace evidence shows X is false." Rate it critical for a wrong user-visible action, high when caught internally after significant waste, and medium for inefficiency. Cite BOTH the action span and the contradicting span with exact quotes. Use confidence 0.85+ when both halves have exact quotes and 0.6-0.8 when one half is inferred. Recommend the literal source correction: update the wiki claim, invalidate and re-curate the raw source, or replace the stale prompt/tool instruction.
1788
-
1789
- Do NOT report a finding if the agent caught and corrected the false belief in the same turn \u2014 that's the system working. Reserve poisoning for cases where the false belief shaped downstream action.
1790
-
1791
- OBSERVABILITY rules:
1792
- - Each non-final turn must emit at least one \`console.log\` for evidence.`;
1793
- var KNOWLEDGE_POISONING_KIND_SPEC = {
1794
- id: "knowledge-poisoning",
1795
- description: "Identifies confident-but-wrong actions caused by stale memory, contradicting RAG, deprecated tool docs, or outdated system-prompt instructions.",
1796
- area: "knowledge-poisoning",
1797
- version: "1.2.0",
1798
- actorDescription: ACTOR_PROMPT4,
1799
- buildTools: (store) => buildTraceToolsForGroup("all", store),
1800
- subqueries: { maxCalls: 8, maxParallel: 4 },
1801
- maxTurns: 20,
1802
- minimumEvidenceCitations: 2,
1803
- cost: { kind: "llm" }
1804
- };
1805
-
1806
- // src/analyst/kinds/index.ts
1807
- var DEFAULT_TRACE_ANALYST_KINDS = [
1808
- FAILURE_MODE_KIND_SPEC,
1809
- KNOWLEDGE_GAP_KIND_SPEC,
1810
- KNOWLEDGE_POISONING_KIND_SPEC,
1811
- IMPROVEMENT_KIND_SPEC
1812
- ];
1813
-
1814
- // src/analyst/registry.ts
1815
- import { randomUUID } from "crypto";
1816
- var AnalystRegistry = class {
1817
- analysts = /* @__PURE__ */ new Map();
1818
- options;
1819
- constructor(options = {}) {
1820
- this.options = options;
1821
- }
1822
- register(analyst) {
1823
- if (!analyst.id) throw new Error("AnalystRegistry.register: analyst.id is required");
1824
- if (this.analysts.has(analyst.id)) {
1825
- throw new Error(`AnalystRegistry.register: duplicate analyst id "${analyst.id}"`);
1826
- }
1827
- if (!analyst.version) {
1828
- throw new Error(`AnalystRegistry.register: analyst "${analyst.id}" must declare a version`);
1829
- }
1830
- if (analyst.cost.kind === "deterministic" && analyst.cost.settlement_timeout_ms !== void 0) {
1831
- throw new TypeError(
1832
- `AnalystRegistry.register: deterministic analyst "${analyst.id}" cannot declare settlement_timeout_ms`
1833
- );
1834
- }
1835
- if (analyst.cost.settlement_timeout_ms !== void 0) {
1836
- validateUsageSettlementTimeout(analyst.cost.settlement_timeout_ms);
1837
- }
1838
- this.analysts.set(analyst.id, analyst);
1839
- }
1840
- list() {
1841
- return Array.from(this.analysts.values()).map((a) => ({
1842
- id: a.id,
1843
- description: a.description,
1844
- version: a.version,
1845
- cost: a.cost
1846
- }));
1847
- }
1848
- async run(runId, inputs, runOpts = {}) {
1849
- for await (const ev of this.runStream(runId, inputs, runOpts)) {
1850
- if (ev.type === "run-completed") return ev.result;
1851
- }
1852
- throw new Error("AnalystRegistry.run: stream completed without run-completed event");
1853
- }
1854
- /**
1855
- * Streaming counterpart to `run()`. Emits `AnalystRunEvent` values
1856
- * in real time — `run-started`, then per-analyst `skipped` /
1857
- * `started` / `completed`, then a terminal `run-completed` whose
1858
- * payload is the full `AnalystRunResult`. UIs use this to render
1859
- * progress; persistence consumers use `run()` and read the result.
1860
- *
1861
- * Hooks (`onBeforeAnalyze` / `onAfterAnalyze` / `onError` /
1862
- * `onComplete`) fire as before — streaming is additive, not a hook
1863
- * replacement.
1864
- */
1865
- async *runStream(runId, inputs, runOpts = {}) {
1866
- const correlationId = `ar_${randomUUID().slice(0, 12)}`;
1867
- const log = this.options.log ?? (() => {
1868
- });
1869
- const hooks = this.options.hooks ?? {};
1870
- const startedAt = (/* @__PURE__ */ new Date()).toISOString();
1871
- const started = Date.now();
1872
- const timeoutMs = validateTimeout(runOpts.timeoutMs);
1873
- const deadlineMs = timeoutMs === void 0 ? void 0 : started + timeoutMs;
1874
- const timeoutSignal = timeoutMs === void 0 ? void 0 : AbortSignal.timeout(timeoutMs);
1875
- const runSignal = combineAbortSignals(runOpts.signal, timeoutSignal);
1876
- const selected = this.selectAnalysts(runOpts);
1877
- const budget = runOpts.budget ?? this.options.defaultBudget;
1878
- validateBudgetPolicy(budget);
1879
- yield {
1880
- type: "run-started",
1881
- run_id: runId,
1882
- correlation_id: correlationId,
1883
- started_at: startedAt,
1884
- analyst_ids: selected.map((a) => a.id)
1885
- };
1886
- const summaries = [];
1887
- const allFindings = [];
1888
- let totalCost = 0;
1889
- let remainingUsd = budget?.totalUsd;
1890
- const runnableAnalysts = selected.filter((a) => this.routeInput(a, inputs).kind !== "missing");
1891
- const runnableCount = runnableAnalysts.length;
1892
- const weights = budget?.weights;
1893
- const totalWeight = weights && budget?.totalUsd != null && !budget.allocate && runnableCount > 0 ? runnableAnalysts.reduce((sum, analyst) => sum + analystWeight(weights, analyst.id), 0) : void 0;
1894
- if (totalWeight === 0) {
1895
- throw new Error("BudgetPolicy.weights must allocate positive weight to a runnable analyst");
1896
- }
1897
- for (const analyst of selected) {
1898
- const t0 = Date.now();
1899
- if (runSignal?.aborted) {
1900
- const summary2 = abortedBeforeStartSummary(analyst, runSignal);
1901
- summaries.push(summary2);
1902
- log(`[analyst] skip ${analyst.id} \u2014 run aborted`, { runId, reason: summary2.reason });
1903
- yield { type: "analyst-skipped", summary: summary2 };
1904
- continue;
1905
- }
1906
- const input = this.routeInput(analyst, inputs);
1907
- if (input.kind === "missing") {
1908
- const summary2 = {
1909
- analyst_id: analyst.id,
1910
- status: "skipped",
1911
- reason: `missing input of kind '${analyst.inputKind}'`,
1912
- findings_count: 0,
1913
- latency_ms: 0,
1914
- cost_usd: 0,
1915
- usage: zeroUsage()
1916
- };
1917
- summaries.push(summary2);
1918
- log(`[analyst] skip ${analyst.id} \u2014 missing input`, { runId, kind: analyst.inputKind });
1919
- await waitForHook(
1920
- hooks.onAfterAnalyze ? () => hooks.onAfterAnalyze?.({ analyst, summary: summary2, findings: [], runId }) : void 0,
1921
- runSignal
1922
- );
1923
- yield { type: "analyst-skipped", summary: summary2 };
1924
- continue;
1925
- }
1926
- const perBudget = allocateBudget(budget, {
1927
- analyst,
1928
- remainingUsd,
1929
- runningCount: runnableCount,
1930
- totalWeight
1931
- });
1932
- const usageReceipts = [];
1933
- const ctx = {
1934
- runId,
1935
- correlationId,
1936
- deadlineMs,
1937
- budgetUsd: perBudget,
1938
- costLedger: runOpts.costLedger,
1939
- costPhase: runOpts.costPhase,
1940
- chat: this.options.chat,
1941
- tags: runOpts.tags,
1942
- log: (msg, fields) => log(`[${analyst.id}] ${msg}`, { runId, correlationId, ...fields }),
1943
- signal: runSignal,
1944
- priorFindings: selectPriorFindings(runOpts.priorFindings, analyst.id),
1945
- upstreamFindings: runOpts.chainFindings && allFindings.length > 0 ? [...allFindings] : void 0,
1946
- recordUsage: (receipt) => {
1947
- assertValidUsageReceipt(receipt);
1948
- usageReceipts.push(receipt);
1949
- }
1950
- };
1951
- await waitForHook(
1952
- hooks.onBeforeAnalyze ? () => hooks.onBeforeAnalyze?.({ analyst, ctx, runId }) : void 0,
1953
- runSignal
1954
- );
1955
- if (runSignal?.aborted) {
1956
- const summary2 = abortedBeforeStartSummary(analyst, runSignal, Date.now() - t0);
1957
- summaries.push(summary2);
1958
- log(`[analyst] skip ${analyst.id} \u2014 run aborted`, { runId, reason: summary2.reason });
1959
- yield { type: "analyst-skipped", summary: summary2 };
1960
- continue;
1961
- }
1962
- const effectiveBudget = validateEffectiveBudget(ctx.budgetUsd, remainingUsd, analyst.id);
1963
- yield {
1964
- type: "analyst-started",
1965
- analyst_id: analyst.id,
1966
- started_at: new Date(t0).toISOString()
1967
- };
1968
- let findings;
1969
- let summary;
1970
- try {
1971
- if (runSignal?.aborted) throw abortReason(runSignal);
1972
- findings = await waitForOperation(
1973
- analyst.analyze(input.value, ctx),
1974
- runSignal,
1975
- analystAbortGraceMs(analyst)
1976
- );
1977
- const latency = Date.now() - t0;
1978
- const usage = resolveUsage(analyst, findings, usageReceipts);
1979
- const cost = knownCostUsd(usage);
1980
- totalCost += cost;
1981
- if (typeof remainingUsd === "number") {
1982
- remainingUsd = Math.max(0, remainingUsd - budgetDebit(usage, effectiveBudget));
1983
- }
1984
- allFindings.push(...findings);
1985
- summary = {
1986
- analyst_id: analyst.id,
1987
- status: "ok",
1988
- findings_count: findings.length,
1989
- latency_ms: latency,
1990
- cost_usd: cost,
1991
- usage
1992
- };
1993
- summaries.push(summary);
1994
- log(`[analyst] ok ${analyst.id}`, {
1995
- runId,
1996
- findings: findings.length,
1997
- latency_ms: latency,
1998
- cost_usd: cost,
1999
- cost_kind: usage.cost.kind,
2000
- input_tokens: usage.tokens?.input ?? null,
2001
- output_tokens: usage.tokens?.output ?? null
2002
- });
2003
- if (effectiveBudget !== void 0 && usage.cost.kind === "uncaptured") {
2004
- log(`[analyst] WARN ${analyst.id} \u2014 USD cost uncaptured; budget not reconciled`, {
2005
- runId,
2006
- budget_usd: effectiveBudget,
2007
- cost_captured: false
2008
- });
2009
- }
2010
- } catch (err) {
2011
- const latency = Date.now() - t0;
2012
- const e = err instanceof Error ? err : new Error(String(err));
2013
- const hookFindings = runSignal?.aborted ? [] : await hooks.onError?.({ analyst, error: e, runId }) ?? [];
2014
- if (hookFindings.length) allFindings.push(...hookFindings);
2015
- const usage = resolveUsage(analyst, hookFindings, usageReceipts);
2016
- const cost = knownCostUsd(usage);
2017
- totalCost += cost;
2018
- if (typeof remainingUsd === "number") {
2019
- remainingUsd = Math.max(0, remainingUsd - budgetDebit(usage, effectiveBudget));
2020
- }
2021
- const summary2 = {
2022
- analyst_id: analyst.id,
2023
- status: "failed",
2024
- findings_count: hookFindings.length,
2025
- latency_ms: latency,
2026
- cost_usd: cost,
2027
- usage,
2028
- error: { class: e.constructor.name, message: e.message }
2029
- };
2030
- summaries.push(summary2);
2031
- log(`[analyst] FAIL ${analyst.id}`, {
2032
- runId,
2033
- error_class: e.constructor.name,
2034
- error: e.message,
2035
- cost_usd: cost,
2036
- cost_kind: usage.cost.kind
2037
- });
2038
- if (effectiveBudget !== void 0 && usage.cost.kind === "uncaptured") {
2039
- log(`[analyst] WARN ${analyst.id} \u2014 USD cost uncaptured; budget not reconciled`, {
2040
- runId,
2041
- budget_usd: effectiveBudget,
2042
- cost_captured: false
2043
- });
2044
- }
2045
- await waitForHook(
2046
- hooks.onAfterAnalyze ? () => hooks.onAfterAnalyze?.({ analyst, summary: summary2, findings: hookFindings, runId }) : void 0,
2047
- runSignal
2048
- );
2049
- yield { type: "analyst-completed", summary: summary2, findings: hookFindings };
2050
- continue;
2051
- }
2052
- await waitForHook(
2053
- hooks.onAfterAnalyze ? () => hooks.onAfterAnalyze?.({ analyst, summary, findings, runId }) : void 0,
2054
- runSignal
2055
- );
2056
- yield { type: "analyst-completed", summary, findings };
2057
- }
2058
- const result = {
2059
- run_id: runId,
2060
- correlation_id: correlationId,
2061
- started_at: startedAt,
2062
- ended_at: (/* @__PURE__ */ new Date()).toISOString(),
2063
- findings: allFindings,
2064
- per_analyst: summaries,
2065
- total_cost_usd: totalCost,
2066
- total_cost_provenance: aggregateCostProvenance(
2067
- summaries.map((summary) => summary.usage?.cost ?? { kind: "uncaptured", usd: null })
2068
- )
2069
- };
2070
- await waitForHook(
2071
- hooks.onComplete ? () => hooks.onComplete?.({ result }) : void 0,
2072
- runSignal
2073
- );
2074
- yield { type: "run-completed", result };
2075
- }
2076
- selectAnalysts(opts) {
2077
- let candidates = Array.from(this.analysts.values());
2078
- if (opts.only?.length) {
2079
- const only = new Set(opts.only);
2080
- candidates = candidates.filter((a) => only.has(a.id));
2081
- }
2082
- if (opts.skip?.length) {
2083
- const skip = new Set(opts.skip);
2084
- candidates = candidates.filter((a) => !skip.has(a.id));
2085
- }
2086
- return candidates;
2087
- }
2088
- routeInput(analyst, inputs) {
2089
- switch (analyst.inputKind) {
2090
- case "trace-store":
2091
- return inputs.traceStore ? { kind: "present", value: inputs.traceStore } : { kind: "missing" };
2092
- case "artifact-dir":
2093
- return inputs.artifactDir ? { kind: "present", value: inputs.artifactDir } : { kind: "missing" };
2094
- case "run-record":
2095
- return inputs.runRecord ? { kind: "present", value: inputs.runRecord } : { kind: "missing" };
2096
- case "judge-input":
2097
- return inputs.judgeInput ? { kind: "present", value: inputs.judgeInput } : { kind: "missing" };
2098
- case "custom": {
2099
- const v = inputs.custom?.[analyst.id];
2100
- return v !== void 0 ? { kind: "present", value: v } : { kind: "missing" };
2101
- }
2102
- }
2103
- }
2104
- };
2105
- function validateTimeout(timeoutMs) {
2106
- if (timeoutMs === void 0) return void 0;
2107
- if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) {
2108
- throw new TypeError(
2109
- "RegistryRunOpts.timeoutMs must be a positive safe integer no greater than 2147483647"
2110
- );
2111
- }
2112
- return timeoutMs;
2113
- }
2114
- async function waitForOperation(operation, signal, abortGraceMs) {
2115
- if (!signal) return operation;
2116
- if (signal.aborted) {
2117
- void operation.catch(() => {
2118
- });
2119
- throw abortReason(signal);
2120
- }
2121
- return new Promise((resolve, reject) => {
2122
- let settlementTimer;
2123
- const cleanup = () => {
2124
- signal.removeEventListener("abort", onAbort);
2125
- if (settlementTimer) clearTimeout(settlementTimer);
2126
- };
2127
- const onAbort = () => {
2128
- if (abortGraceMs === 0) {
2129
- cleanup();
2130
- reject(abortReason(signal));
2131
- return;
2132
- }
2133
- settlementTimer = setTimeout(() => {
2134
- cleanup();
2135
- reject(abortReason(signal));
2136
- }, abortGraceMs);
2137
- };
2138
- signal.addEventListener("abort", onAbort, { once: true });
2139
- operation.then(
2140
- (value) => {
2141
- cleanup();
2142
- if (signal.aborted) reject(abortReason(signal));
2143
- else resolve(value);
2144
- },
2145
- (error) => {
2146
- cleanup();
2147
- reject(signal.aborted ? abortReason(signal) : error);
2148
- }
2149
- );
2150
- });
2151
- }
2152
- async function waitForHook(operation, signal) {
2153
- if (operation === void 0 || signal?.aborted) return void 0;
2154
- try {
2155
- return await waitForOperation(
2156
- Promise.resolve().then(() => {
2157
- if (signal?.aborted) throw abortReason(signal);
2158
- return operation();
2159
- }),
2160
- signal,
2161
- 0
2162
- );
2163
- } catch (error) {
2164
- if (signal?.aborted) return void 0;
2165
- throw error;
2166
- }
2167
- }
2168
- function analystAbortGraceMs(analyst) {
2169
- if (analyst.cost.kind === "deterministic") return 0;
2170
- const settlementMs = validateUsageSettlementTimeout(analyst.cost.settlement_timeout_ms);
2171
- if (settlementMs === 0) return 0;
2172
- return Math.min(settlementMs + 100, 2147483647);
2173
- }
2174
- function abortedBeforeStartSummary(analyst, signal, latencyMs = 0) {
2175
- const reason = abortReason(signal);
2176
- return {
2177
- analyst_id: analyst.id,
2178
- status: "skipped",
2179
- reason: `${reason.name}: ${reason.message}`,
2180
- findings_count: 0,
2181
- latency_ms: latencyMs,
2182
- cost_usd: 0,
2183
- usage: zeroUsage()
2184
- };
2185
- }
2186
- function abortReason(signal) {
2187
- return signal.reason instanceof Error ? signal.reason : new DOMException("The operation was aborted", "AbortError");
2188
- }
2189
- function allocateBudget(policy, args) {
2190
- if (!policy) return void 0;
2191
- if (policy.allocate) {
2192
- const allocated2 = policy.allocate({
2193
- analyst: args.analyst,
2194
- totalUsd: policy.totalUsd,
2195
- remainingUsd: args.remainingUsd,
2196
- runningCount: args.runningCount
2197
- });
2198
- if (allocated2 === void 0) {
2199
- if (policy.totalUsd !== void 0) {
2200
- throw new Error(
2201
- `BudgetPolicy.allocate('${args.analyst.id}') cannot return undefined when totalUsd is set`
2202
- );
2203
- }
2204
- return void 0;
2205
- }
2206
- assertBudgetAmount(allocated2, `BudgetPolicy.allocate('${args.analyst.id}')`);
2207
- return args.remainingUsd === void 0 ? allocated2 : Math.min(allocated2, args.remainingUsd);
2208
- }
2209
- if (policy.totalUsd == null) return void 0;
2210
- const allocated = policy.weights ? policy.totalUsd * analystWeight(policy.weights, args.analyst.id) / args.totalWeight : policy.totalUsd / Math.max(1, args.runningCount);
2211
- return args.remainingUsd === void 0 ? allocated : Math.min(allocated, args.remainingUsd);
2212
- }
2213
- function validateBudgetPolicy(policy) {
2214
- if (!policy) return;
2215
- if (policy.totalUsd !== void 0) assertBudgetAmount(policy.totalUsd, "BudgetPolicy.totalUsd");
2216
- for (const [analystId, weight] of Object.entries(policy.weights ?? {})) {
2217
- assertBudgetAmount(weight, `BudgetPolicy.weights['${analystId}']`);
2218
- }
2219
- }
2220
- function assertBudgetAmount(value, field) {
2221
- if (!Number.isFinite(value) || value < 0) {
2222
- throw new Error(`${field} must be a non-negative finite number`);
2223
- }
2224
- }
2225
- function validateEffectiveBudget(budgetUsd, remainingUsd, analystId) {
2226
- if (budgetUsd !== void 0) {
2227
- assertBudgetAmount(budgetUsd, `AnalystContext.budgetUsd for '${analystId}'`);
2228
- }
2229
- if (remainingUsd === void 0) return budgetUsd;
2230
- if (budgetUsd === void 0) {
2231
- throw new Error(
2232
- `AnalystContext.budgetUsd for '${analystId}' cannot be removed while an overall budget remains`
2233
- );
2234
- }
2235
- if (budgetUsd > remainingUsd) {
2236
- throw new Error(
2237
- `AnalystContext.budgetUsd for '${analystId}' (${budgetUsd}) exceeds the remaining overall budget (${remainingUsd})`
2238
- );
2239
- }
2240
- return budgetUsd;
2241
- }
2242
- function analystWeight(weights, analystId) {
2243
- const weight = weights[analystId] ?? 1;
2244
- assertBudgetAmount(weight, `BudgetPolicy.weights['${analystId}']`);
2245
- return weight;
2246
- }
2247
- function zeroUsage() {
2248
- return {
2249
- calls: 0,
2250
- tokens: { input: 0, output: 0 },
2251
- cost: { kind: "observed", usd: 0 }
2252
- };
2253
- }
2254
- function resolveUsage(analyst, findings, receipts) {
2255
- const legacyCost = sumFindingCost(findings);
2256
- if (receipts.length > 0) {
2257
- const merged = mergeUsageReceipts(receipts);
2258
- return merged.cost.kind === "uncaptured" && legacyCost.captured ? { ...merged, knownCostUsd: Math.max(merged.knownCostUsd ?? 0, legacyCost.usd) } : merged;
2259
- }
2260
- if (legacyCost.captured) {
2261
- return {
2262
- calls: null,
2263
- tokens: null,
2264
- cost: { kind: "observed", usd: legacyCost.usd }
2265
- };
2266
- }
2267
- if (analyst.cost.kind === "deterministic") return zeroUsage();
2268
- return { calls: null, tokens: null, cost: { kind: "uncaptured", usd: null } };
2269
- }
2270
- function mergeUsageReceipts(receipts) {
2271
- const calls = receipts.every((receipt) => receipt.calls !== null) ? receipts.reduce((sum, receipt) => sum + (receipt.calls ?? 0), 0) : null;
2272
- const tokens = receipts.every((receipt) => receipt.tokens !== null) ? receipts.reduce(
2273
- (sum, receipt) => ({
2274
- input: sum.input + (receipt.tokens?.input ?? 0),
2275
- output: sum.output + (receipt.tokens?.output ?? 0),
2276
- ...sum.reasoning !== void 0 || receipt.tokens?.reasoning !== void 0 ? { reasoning: (sum.reasoning ?? 0) + (receipt.tokens?.reasoning ?? 0) } : {},
2277
- ...sum.cached !== void 0 || receipt.tokens?.cached !== void 0 ? { cached: (sum.cached ?? 0) + (receipt.tokens?.cached ?? 0) } : {},
2278
- ...sum.cacheWrite !== void 0 || receipt.tokens?.cacheWrite !== void 0 ? { cacheWrite: (sum.cacheWrite ?? 0) + (receipt.tokens?.cacheWrite ?? 0) } : {}
2279
- }),
2280
- { input: 0, output: 0 }
2281
- ) : null;
2282
- const cost = aggregateCostProvenance(receipts.map((receipt) => receipt.cost));
2283
- return {
2284
- calls,
2285
- tokens,
2286
- cost,
2287
- ...cost.kind === "uncaptured" ? {
2288
- knownCostUsd: receipts.reduce((sum, receipt) => sum + knownCostUsd(receipt), 0)
2289
- } : {}
2290
- };
2291
- }
2292
- function knownCostUsd(receipt) {
2293
- return receipt.cost.kind === "uncaptured" ? receipt.knownCostUsd ?? 0 : receipt.cost.usd;
2294
- }
2295
- function budgetDebit(receipt, allocatedUsd) {
2296
- const known = knownCostUsd(receipt);
2297
- return receipt.cost.kind === "uncaptured" && allocatedUsd !== void 0 ? Math.max(known, allocatedUsd) : known;
2298
- }
2299
- function aggregateCostProvenance(costs) {
2300
- if (costs.some((cost) => cost.kind === "uncaptured")) {
2301
- return { kind: "uncaptured", usd: null };
2302
- }
2303
- const usd = costs.reduce((sum, cost) => sum + (cost.usd ?? 0), 0);
2304
- return costs.some((cost) => cost.kind === "estimated") ? { kind: "estimated", usd } : { kind: "observed", usd };
2305
- }
2306
- function assertValidUsageReceipt(receipt) {
2307
- if (receipt.calls !== null && (!Number.isInteger(receipt.calls) || receipt.calls < 0)) {
2308
- throw new Error("AnalystContext.recordUsage: calls must be a non-negative integer or null");
2309
- }
2310
- if (receipt.tokens) {
2311
- assertNonNegativeFinite(receipt.tokens.input, "tokens.input");
2312
- assertNonNegativeFinite(receipt.tokens.output, "tokens.output");
2313
- if (receipt.tokens.reasoning !== void 0) {
2314
- assertNonNegativeFinite(receipt.tokens.reasoning, "tokens.reasoning");
2315
- if (receipt.tokens.reasoning > receipt.tokens.output) {
2316
- throw new Error(
2317
- "AnalystContext.recordUsage: tokens.reasoning must not exceed tokens.output"
2318
- );
2319
- }
2320
- }
2321
- if (receipt.tokens.cached !== void 0) {
2322
- assertNonNegativeFinite(receipt.tokens.cached, "tokens.cached");
2323
- }
2324
- if (receipt.tokens.cacheWrite !== void 0) {
2325
- assertNonNegativeFinite(receipt.tokens.cacheWrite, "tokens.cacheWrite");
2326
- }
2327
- }
2328
- if (receipt.cost.kind !== "uncaptured") {
2329
- assertNonNegativeFinite(receipt.cost.usd, "cost.usd");
2330
- } else if (receipt.cost.usd !== null) {
2331
- throw new Error("AnalystContext.recordUsage: uncaptured cost.usd must be null");
2332
- }
2333
- if (receipt.knownCostUsd !== void 0) {
2334
- assertNonNegativeFinite(receipt.knownCostUsd, "knownCostUsd");
2335
- }
2336
- }
2337
- function assertNonNegativeFinite(value, field) {
2338
- if (!Number.isFinite(value) || value < 0) {
2339
- throw new Error(`AnalystContext.recordUsage: ${field} must be a non-negative finite number`);
2340
- }
2341
- }
2342
- function sumFindingCost(findings) {
2343
- let sum = 0;
2344
- let captured = false;
2345
- for (const f of findings) {
2346
- const c = f.metadata?.cost_usd;
2347
- if (c === void 0) continue;
2348
- if (typeof c !== "number" || !Number.isFinite(c) || c < 0) {
2349
- throw new Error(
2350
- `Analyst finding '${f.finding_id}' metadata.cost_usd must be a non-negative finite number`
2351
- );
2352
- }
2353
- sum += c;
2354
- captured = true;
2355
- }
2356
- return { usd: sum, captured };
2357
- }
2358
- function selectPriorFindings(source, analystId) {
2359
- if (!source) return void 0;
2360
- if (Array.isArray(source)) {
2361
- const own2 = source.filter((f) => f.analyst_id === analystId);
2362
- return own2.length > 0 ? own2 : void 0;
2363
- }
2364
- const record = source;
2365
- const own = record[analystId] ?? [];
2366
- const wildcard = record["*"] ?? [];
2367
- const merged = [...own, ...wildcard];
2368
- return merged.length > 0 ? merged : void 0;
2369
- }
2370
-
2371
- // src/analyst/default-registry.ts
2372
- function buildDefaultAnalystRegistry(opts = {}) {
2373
- const registry = new AnalystRegistry(opts.registry);
2374
- if (opts.includeBehavioral !== false) {
2375
- registry.register(behavioralAnalyst());
2376
- }
2377
- if (opts.ai) {
2378
- const kinds = opts.kinds ?? DEFAULT_TRACE_ANALYST_KINDS;
2379
- for (const spec of kinds) {
2380
- registry.register(createTraceAnalystKind(spec, { ai: opts.ai, model: opts.model }));
2381
- }
2382
- }
2383
- return registry;
2384
- }
2385
-
2386
- export {
2387
- createAnalystAi,
2388
- createChatClient,
2389
- computeTraceMetrics,
2390
- computeFindingId,
2391
- makeFinding,
2392
- deriveEfficiencyFindings,
2393
- behavioralAnalyst,
2394
- FINDING_SUBJECT_KINDS,
2395
- parseFindingSubject,
2396
- renderFindingSubject,
2397
- FINDING_SUBJECT_SYNTAX,
2398
- FINDING_SUBJECT_GRAMMAR_PROMPT,
2399
- KIND_EXPECTED_SUBJECTS,
2400
- findingSubjectGrammarPromptFor,
2401
- FindingSubjectStringSchema,
2402
- stripCodeFences,
2403
- coerceJson,
2404
- coerceToFindingRows,
2405
- ANALYST_SEVERITIES,
2406
- RawAnalystEvidenceSchema,
2407
- RawAnalystFindingSchema,
2408
- CanonicalRawAnalystFindingSchema,
2409
- RAW_FINDING_SCHEMA_PROMPT,
2410
- evidenceRefsFromRawFinding,
2411
- parseRawFinding,
2412
- parseCanonicalRawFinding,
2413
- structureFindings,
2414
- settleUsageReceiptFromCostLedger,
2415
- validateUsageSettlementTimeout,
2416
- createTraceAnalystKind,
2417
- renderPriorFindings,
2418
- renderUpstreamFindings,
2419
- buildTraceToolsForGroup,
2420
- FAILURE_MODE_KIND_SPEC,
2421
- IMPROVEMENT_KIND_SPEC,
2422
- KNOWLEDGE_GAP_KIND_SPEC,
2423
- KNOWLEDGE_POISONING_KIND_SPEC,
2424
- DEFAULT_TRACE_ANALYST_KINDS,
2425
- AnalystRegistry,
2426
- buildDefaultAnalystRegistry
2427
- };
2428
- //# sourceMappingURL=chunk-DJKY2TSY.js.map