@smthrs/harness 0.0.0-stage → 1.0.0-rc.3

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 (476) hide show
  1. package/CHANGELOG.md +387 -0
  2. package/LICENSE +21 -0
  3. package/README.md +138 -2
  4. package/dist/cjs/AgentEvent.d.ts +3092 -0
  5. package/dist/cjs/AgentEvent.d.ts.map +1 -0
  6. package/dist/cjs/AgentEvent.js +1116 -0
  7. package/dist/cjs/AgentEvent.js.map +7 -0
  8. package/dist/cjs/CallLedger.d.ts +378 -0
  9. package/dist/cjs/CallLedger.d.ts.map +1 -0
  10. package/dist/cjs/CallLedger.js +240 -0
  11. package/dist/cjs/CallLedger.js.map +7 -0
  12. package/dist/cjs/Cell.d.ts +774 -0
  13. package/dist/cjs/Cell.d.ts.map +1 -0
  14. package/dist/cjs/Cell.js +431 -0
  15. package/dist/cjs/Cell.js.map +7 -0
  16. package/dist/cjs/CellCalls.d.ts +115 -0
  17. package/dist/cjs/CellCalls.d.ts.map +1 -0
  18. package/dist/cjs/CellCalls.js +98 -0
  19. package/dist/cjs/CellCalls.js.map +7 -0
  20. package/dist/cjs/CellHistory.d.ts +102 -0
  21. package/dist/cjs/CellHistory.d.ts.map +1 -0
  22. package/dist/cjs/CellHistory.js +54 -0
  23. package/dist/cjs/CellHistory.js.map +7 -0
  24. package/dist/cjs/CellTurn.d.ts +1071 -0
  25. package/dist/cjs/CellTurn.d.ts.map +1 -0
  26. package/dist/cjs/CellTurn.js +2618 -0
  27. package/dist/cjs/CellTurn.js.map +7 -0
  28. package/dist/cjs/CellValidation.d.ts +94 -0
  29. package/dist/cjs/CellValidation.d.ts.map +1 -0
  30. package/dist/cjs/CellValidation.js +218 -0
  31. package/dist/cjs/CellValidation.js.map +7 -0
  32. package/dist/cjs/Compaction.d.ts +158 -0
  33. package/dist/cjs/Compaction.d.ts.map +1 -0
  34. package/dist/cjs/Compaction.js +189 -0
  35. package/dist/cjs/Compaction.js.map +7 -0
  36. package/dist/cjs/CompletionClaim.d.ts +801 -0
  37. package/dist/cjs/CompletionClaim.d.ts.map +1 -0
  38. package/dist/cjs/CompletionClaim.js +303 -0
  39. package/dist/cjs/CompletionClaim.js.map +7 -0
  40. package/dist/cjs/ContextWindow.d.ts +435 -0
  41. package/dist/cjs/ContextWindow.d.ts.map +1 -0
  42. package/dist/cjs/ContextWindow.js +319 -0
  43. package/dist/cjs/ContextWindow.js.map +7 -0
  44. package/dist/cjs/EngineLike.d.ts +546 -0
  45. package/dist/cjs/EngineLike.d.ts.map +1 -0
  46. package/dist/cjs/EngineLike.js +114 -0
  47. package/dist/cjs/EngineLike.js.map +7 -0
  48. package/dist/cjs/ExternalTranscript.d.ts +349 -0
  49. package/dist/cjs/ExternalTranscript.d.ts.map +1 -0
  50. package/dist/cjs/ExternalTranscript.js +826 -0
  51. package/dist/cjs/ExternalTranscript.js.map +7 -0
  52. package/dist/cjs/FailedCall.d.ts +132 -0
  53. package/dist/cjs/FailedCall.d.ts.map +1 -0
  54. package/dist/cjs/FailedCall.js +53 -0
  55. package/dist/cjs/FailedCall.js.map +7 -0
  56. package/dist/cjs/FlowBinding.d.ts +289 -0
  57. package/dist/cjs/FlowBinding.d.ts.map +1 -0
  58. package/dist/cjs/FlowBinding.js +251 -0
  59. package/dist/cjs/FlowBinding.js.map +7 -0
  60. package/dist/cjs/HarnessError.d.ts +57 -0
  61. package/dist/cjs/HarnessError.d.ts.map +1 -0
  62. package/dist/cjs/HarnessError.js +75 -0
  63. package/dist/cjs/HarnessError.js.map +7 -0
  64. package/dist/cjs/Judgement.d.ts +289 -0
  65. package/dist/cjs/Judgement.d.ts.map +1 -0
  66. package/dist/cjs/Judgement.js +240 -0
  67. package/dist/cjs/Judgement.js.map +7 -0
  68. package/dist/cjs/Monitor.d.ts +374 -0
  69. package/dist/cjs/Monitor.d.ts.map +1 -0
  70. package/dist/cjs/Monitor.js +233 -0
  71. package/dist/cjs/Monitor.js.map +7 -0
  72. package/dist/cjs/NarrowedCheck.d.ts +523 -0
  73. package/dist/cjs/NarrowedCheck.d.ts.map +1 -0
  74. package/dist/cjs/NarrowedCheck.js +263 -0
  75. package/dist/cjs/NarrowedCheck.js.map +7 -0
  76. package/dist/cjs/Notifications.d.ts +42 -0
  77. package/dist/cjs/Notifications.d.ts.map +1 -0
  78. package/dist/cjs/Notifications.js +177 -0
  79. package/dist/cjs/Notifications.js.map +7 -0
  80. package/dist/cjs/Plan.d.ts +127 -0
  81. package/dist/cjs/Plan.d.ts.map +1 -0
  82. package/dist/cjs/Plan.js +77 -0
  83. package/dist/cjs/Plan.js.map +7 -0
  84. package/dist/cjs/QuickJSSandbox.d.ts +151 -0
  85. package/dist/cjs/QuickJSSandbox.d.ts.map +1 -0
  86. package/dist/cjs/QuickJSSandbox.js +987 -0
  87. package/dist/cjs/QuickJSSandbox.js.map +7 -0
  88. package/dist/cjs/Relevance.d.ts +213 -0
  89. package/dist/cjs/Relevance.d.ts.map +1 -0
  90. package/dist/cjs/Relevance.js +183 -0
  91. package/dist/cjs/Relevance.js.map +7 -0
  92. package/dist/cjs/Sandbox.d.ts +637 -0
  93. package/dist/cjs/Sandbox.d.ts.map +1 -0
  94. package/dist/cjs/Sandbox.js +260 -0
  95. package/dist/cjs/Sandbox.js.map +7 -0
  96. package/dist/cjs/Steering.d.ts +464 -0
  97. package/dist/cjs/Steering.d.ts.map +1 -0
  98. package/dist/cjs/Steering.js +153 -0
  99. package/dist/cjs/Steering.js.map +7 -0
  100. package/dist/cjs/StructuredOutput.d.ts +252 -0
  101. package/dist/cjs/StructuredOutput.d.ts.map +1 -0
  102. package/dist/cjs/StructuredOutput.js +266 -0
  103. package/dist/cjs/StructuredOutput.js.map +7 -0
  104. package/dist/cjs/Sufficiency.d.ts +195 -0
  105. package/dist/cjs/Sufficiency.d.ts.map +1 -0
  106. package/dist/cjs/Sufficiency.js +110 -0
  107. package/dist/cjs/Sufficiency.js.map +7 -0
  108. package/dist/cjs/Supervisor.d.ts +719 -0
  109. package/dist/cjs/Supervisor.d.ts.map +1 -0
  110. package/dist/cjs/Supervisor.js +313 -0
  111. package/dist/cjs/Supervisor.js.map +7 -0
  112. package/dist/cjs/Tokens.d.ts +86 -0
  113. package/dist/cjs/Tokens.d.ts.map +1 -0
  114. package/dist/cjs/Tokens.js +72 -0
  115. package/dist/cjs/Tokens.js.map +7 -0
  116. package/dist/cjs/Transcript.d.ts +171 -0
  117. package/dist/cjs/Transcript.d.ts.map +1 -0
  118. package/dist/cjs/Transcript.js +342 -0
  119. package/dist/cjs/Transcript.js.map +7 -0
  120. package/dist/cjs/TruncatedOutput.d.ts +186 -0
  121. package/dist/cjs/TruncatedOutput.d.ts.map +1 -0
  122. package/dist/cjs/TruncatedOutput.js +143 -0
  123. package/dist/cjs/TruncatedOutput.js.map +7 -0
  124. package/dist/cjs/UnmovedTree.d.ts +113 -0
  125. package/dist/cjs/UnmovedTree.d.ts.map +1 -0
  126. package/dist/cjs/UnmovedTree.js +38 -0
  127. package/dist/cjs/UnmovedTree.js.map +7 -0
  128. package/dist/cjs/UnresolvedFailure.d.ts +196 -0
  129. package/dist/cjs/UnresolvedFailure.d.ts.map +1 -0
  130. package/dist/cjs/UnresolvedFailure.js +77 -0
  131. package/dist/cjs/UnresolvedFailure.js.map +7 -0
  132. package/dist/cjs/VacuousVerification.d.ts +237 -0
  133. package/dist/cjs/VacuousVerification.d.ts.map +1 -0
  134. package/dist/cjs/VacuousVerification.js +91 -0
  135. package/dist/cjs/VacuousVerification.js.map +7 -0
  136. package/dist/cjs/VariablesPanel.d.ts +117 -0
  137. package/dist/cjs/VariablesPanel.d.ts.map +1 -0
  138. package/dist/cjs/VariablesPanel.js +108 -0
  139. package/dist/cjs/VariablesPanel.js.map +7 -0
  140. package/dist/cjs/index.d.ts +172 -0
  141. package/dist/cjs/index.d.ts.map +1 -0
  142. package/dist/cjs/index.js +99 -0
  143. package/dist/cjs/index.js.map +7 -0
  144. package/dist/cjs/internal/bytes.d.ts +36 -0
  145. package/dist/cjs/internal/bytes.d.ts.map +1 -0
  146. package/dist/cjs/internal/bytes.js +57 -0
  147. package/dist/cjs/internal/bytes.js.map +7 -0
  148. package/dist/cjs/internal/cellPrompt.d.ts +122 -0
  149. package/dist/cjs/internal/cellPrompt.d.ts.map +1 -0
  150. package/dist/cjs/internal/cellPrompt.js +146 -0
  151. package/dist/cjs/internal/cellPrompt.js.map +7 -0
  152. package/dist/cjs/internal/compactable.d.ts +44 -0
  153. package/dist/cjs/internal/compactable.d.ts.map +1 -0
  154. package/dist/cjs/internal/compactable.js +56 -0
  155. package/dist/cjs/internal/compactable.js.map +7 -0
  156. package/dist/cjs/internal/compactionMarks.d.ts +278 -0
  157. package/dist/cjs/internal/compactionMarks.d.ts.map +1 -0
  158. package/dist/cjs/internal/compactionMarks.js +212 -0
  159. package/dist/cjs/internal/compactionMarks.js.map +7 -0
  160. package/dist/cjs/internal/demandText.d.ts +95 -0
  161. package/dist/cjs/internal/demandText.d.ts.map +1 -0
  162. package/dist/cjs/internal/demandText.js +70 -0
  163. package/dist/cjs/internal/demandText.js.map +7 -0
  164. package/dist/cjs/internal/elide.d.ts +109 -0
  165. package/dist/cjs/internal/elide.d.ts.map +1 -0
  166. package/dist/cjs/internal/elide.js +59 -0
  167. package/dist/cjs/internal/elide.js.map +7 -0
  168. package/dist/cjs/internal/frame.d.ts +463 -0
  169. package/dist/cjs/internal/frame.d.ts.map +1 -0
  170. package/dist/cjs/internal/frame.js +514 -0
  171. package/dist/cjs/internal/frame.js.map +7 -0
  172. package/dist/cjs/internal/nonNegativeSafeInt.d.ts +20 -0
  173. package/dist/cjs/internal/nonNegativeSafeInt.d.ts.map +1 -0
  174. package/dist/cjs/internal/nonNegativeSafeInt.js +29 -0
  175. package/dist/cjs/internal/nonNegativeSafeInt.js.map +7 -0
  176. package/dist/cjs/internal/paidUsage.d.ts +52 -0
  177. package/dist/cjs/internal/paidUsage.d.ts.map +1 -0
  178. package/dist/cjs/internal/paidUsage.js +70 -0
  179. package/dist/cjs/internal/paidUsage.js.map +7 -0
  180. package/dist/cjs/internal/printChannel.d.ts +233 -0
  181. package/dist/cjs/internal/printChannel.d.ts.map +1 -0
  182. package/dist/cjs/internal/printChannel.js +165 -0
  183. package/dist/cjs/internal/printChannel.js.map +7 -0
  184. package/dist/cjs/internal/printsObservation.d.ts +26 -0
  185. package/dist/cjs/internal/printsObservation.d.ts.map +1 -0
  186. package/dist/cjs/internal/printsObservation.js +27 -0
  187. package/dist/cjs/internal/printsObservation.js.map +7 -0
  188. package/dist/cjs/internal/refusal.d.ts +40 -0
  189. package/dist/cjs/internal/refusal.d.ts.map +1 -0
  190. package/dist/cjs/internal/refusal.js +45 -0
  191. package/dist/cjs/internal/refusal.js.map +7 -0
  192. package/dist/cjs/internal/supervision.d.ts +174 -0
  193. package/dist/cjs/internal/supervision.d.ts.map +1 -0
  194. package/dist/cjs/internal/supervision.js +402 -0
  195. package/dist/cjs/internal/supervision.js.map +7 -0
  196. package/dist/cjs/internal/unfinishedWork.d.ts +99 -0
  197. package/dist/cjs/internal/unfinishedWork.d.ts.map +1 -0
  198. package/dist/cjs/internal/unfinishedWork.js +80 -0
  199. package/dist/cjs/internal/unfinishedWork.js.map +7 -0
  200. package/dist/cjs/internal/unobservedCall.d.ts +112 -0
  201. package/dist/cjs/internal/unobservedCall.d.ts.map +1 -0
  202. package/dist/cjs/internal/unobservedCall.js +345 -0
  203. package/dist/cjs/internal/unobservedCall.js.map +7 -0
  204. package/dist/cjs/internal/untrustedData.d.ts +14 -0
  205. package/dist/cjs/internal/untrustedData.d.ts.map +1 -0
  206. package/dist/cjs/internal/untrustedData.js +30 -0
  207. package/dist/cjs/internal/untrustedData.js.map +7 -0
  208. package/dist/cjs/package.json +1 -0
  209. package/dist/esm/AgentEvent.d.ts +3092 -0
  210. package/dist/esm/AgentEvent.d.ts.map +1 -0
  211. package/dist/esm/AgentEvent.js +1610 -0
  212. package/dist/esm/AgentEvent.js.map +1 -0
  213. package/dist/esm/CallLedger.d.ts +378 -0
  214. package/dist/esm/CallLedger.d.ts.map +1 -0
  215. package/dist/esm/CallLedger.js +506 -0
  216. package/dist/esm/CallLedger.js.map +1 -0
  217. package/dist/esm/Cell.d.ts +774 -0
  218. package/dist/esm/Cell.d.ts.map +1 -0
  219. package/dist/esm/Cell.js +772 -0
  220. package/dist/esm/Cell.js.map +1 -0
  221. package/dist/esm/CellCalls.d.ts +115 -0
  222. package/dist/esm/CellCalls.d.ts.map +1 -0
  223. package/dist/esm/CellCalls.js +97 -0
  224. package/dist/esm/CellCalls.js.map +1 -0
  225. package/dist/esm/CellHistory.d.ts +102 -0
  226. package/dist/esm/CellHistory.d.ts.map +1 -0
  227. package/dist/esm/CellHistory.js +91 -0
  228. package/dist/esm/CellHistory.js.map +1 -0
  229. package/dist/esm/CellTurn.d.ts +1071 -0
  230. package/dist/esm/CellTurn.d.ts.map +1 -0
  231. package/dist/esm/CellTurn.js +3410 -0
  232. package/dist/esm/CellTurn.js.map +1 -0
  233. package/dist/esm/CellValidation.d.ts +94 -0
  234. package/dist/esm/CellValidation.d.ts.map +1 -0
  235. package/dist/esm/CellValidation.js +335 -0
  236. package/dist/esm/CellValidation.js.map +1 -0
  237. package/dist/esm/Compaction.d.ts +158 -0
  238. package/dist/esm/Compaction.d.ts.map +1 -0
  239. package/dist/esm/Compaction.js +216 -0
  240. package/dist/esm/Compaction.js.map +1 -0
  241. package/dist/esm/CompletionClaim.d.ts +801 -0
  242. package/dist/esm/CompletionClaim.d.ts.map +1 -0
  243. package/dist/esm/CompletionClaim.js +833 -0
  244. package/dist/esm/CompletionClaim.js.map +1 -0
  245. package/dist/esm/ContextWindow.d.ts +435 -0
  246. package/dist/esm/ContextWindow.d.ts.map +1 -0
  247. package/dist/esm/ContextWindow.js +427 -0
  248. package/dist/esm/ContextWindow.js.map +1 -0
  249. package/dist/esm/EngineLike.d.ts +546 -0
  250. package/dist/esm/EngineLike.d.ts.map +1 -0
  251. package/dist/esm/EngineLike.js +218 -0
  252. package/dist/esm/EngineLike.js.map +1 -0
  253. package/dist/esm/ExternalTranscript.d.ts +349 -0
  254. package/dist/esm/ExternalTranscript.d.ts.map +1 -0
  255. package/dist/esm/ExternalTranscript.js +987 -0
  256. package/dist/esm/ExternalTranscript.js.map +1 -0
  257. package/dist/esm/FailedCall.d.ts +132 -0
  258. package/dist/esm/FailedCall.d.ts.map +1 -0
  259. package/dist/esm/FailedCall.js +131 -0
  260. package/dist/esm/FailedCall.js.map +1 -0
  261. package/dist/esm/FlowBinding.d.ts +289 -0
  262. package/dist/esm/FlowBinding.d.ts.map +1 -0
  263. package/dist/esm/FlowBinding.js +376 -0
  264. package/dist/esm/FlowBinding.js.map +1 -0
  265. package/dist/esm/HarnessError.d.ts +57 -0
  266. package/dist/esm/HarnessError.d.ts.map +1 -0
  267. package/dist/esm/HarnessError.js +85 -0
  268. package/dist/esm/HarnessError.js.map +1 -0
  269. package/dist/esm/Judgement.d.ts +289 -0
  270. package/dist/esm/Judgement.d.ts.map +1 -0
  271. package/dist/esm/Judgement.js +305 -0
  272. package/dist/esm/Judgement.js.map +1 -0
  273. package/dist/esm/Monitor.d.ts +374 -0
  274. package/dist/esm/Monitor.d.ts.map +1 -0
  275. package/dist/esm/Monitor.js +370 -0
  276. package/dist/esm/Monitor.js.map +1 -0
  277. package/dist/esm/NarrowedCheck.d.ts +523 -0
  278. package/dist/esm/NarrowedCheck.d.ts.map +1 -0
  279. package/dist/esm/NarrowedCheck.js +612 -0
  280. package/dist/esm/NarrowedCheck.js.map +1 -0
  281. package/dist/esm/Notifications.d.ts +42 -0
  282. package/dist/esm/Notifications.d.ts.map +1 -0
  283. package/dist/esm/Notifications.js +215 -0
  284. package/dist/esm/Notifications.js.map +1 -0
  285. package/dist/esm/Plan.d.ts +127 -0
  286. package/dist/esm/Plan.d.ts.map +1 -0
  287. package/dist/esm/Plan.js +97 -0
  288. package/dist/esm/Plan.js.map +1 -0
  289. package/dist/esm/QuickJSSandbox.d.ts +151 -0
  290. package/dist/esm/QuickJSSandbox.d.ts.map +1 -0
  291. package/dist/esm/QuickJSSandbox.js +1364 -0
  292. package/dist/esm/QuickJSSandbox.js.map +1 -0
  293. package/dist/esm/Relevance.d.ts +213 -0
  294. package/dist/esm/Relevance.d.ts.map +1 -0
  295. package/dist/esm/Relevance.js +254 -0
  296. package/dist/esm/Relevance.js.map +1 -0
  297. package/dist/esm/Sandbox.d.ts +637 -0
  298. package/dist/esm/Sandbox.d.ts.map +1 -0
  299. package/dist/esm/Sandbox.js +464 -0
  300. package/dist/esm/Sandbox.js.map +1 -0
  301. package/dist/esm/Steering.d.ts +464 -0
  302. package/dist/esm/Steering.d.ts.map +1 -0
  303. package/dist/esm/Steering.js +193 -0
  304. package/dist/esm/Steering.js.map +1 -0
  305. package/dist/esm/StructuredOutput.d.ts +252 -0
  306. package/dist/esm/StructuredOutput.d.ts.map +1 -0
  307. package/dist/esm/StructuredOutput.js +430 -0
  308. package/dist/esm/StructuredOutput.js.map +1 -0
  309. package/dist/esm/Sufficiency.d.ts +195 -0
  310. package/dist/esm/Sufficiency.d.ts.map +1 -0
  311. package/dist/esm/Sufficiency.js +207 -0
  312. package/dist/esm/Sufficiency.js.map +1 -0
  313. package/dist/esm/Supervisor.d.ts +719 -0
  314. package/dist/esm/Supervisor.d.ts.map +1 -0
  315. package/dist/esm/Supervisor.js +575 -0
  316. package/dist/esm/Supervisor.js.map +1 -0
  317. package/dist/esm/Tokens.d.ts +86 -0
  318. package/dist/esm/Tokens.d.ts.map +1 -0
  319. package/dist/esm/Tokens.js +92 -0
  320. package/dist/esm/Tokens.js.map +1 -0
  321. package/dist/esm/Transcript.d.ts +171 -0
  322. package/dist/esm/Transcript.d.ts.map +1 -0
  323. package/dist/esm/Transcript.js +424 -0
  324. package/dist/esm/Transcript.js.map +1 -0
  325. package/dist/esm/TruncatedOutput.d.ts +186 -0
  326. package/dist/esm/TruncatedOutput.d.ts.map +1 -0
  327. package/dist/esm/TruncatedOutput.js +257 -0
  328. package/dist/esm/TruncatedOutput.js.map +1 -0
  329. package/dist/esm/UnmovedTree.d.ts +113 -0
  330. package/dist/esm/UnmovedTree.d.ts.map +1 -0
  331. package/dist/esm/UnmovedTree.js +90 -0
  332. package/dist/esm/UnmovedTree.js.map +1 -0
  333. package/dist/esm/UnresolvedFailure.d.ts +196 -0
  334. package/dist/esm/UnresolvedFailure.d.ts.map +1 -0
  335. package/dist/esm/UnresolvedFailure.js +218 -0
  336. package/dist/esm/UnresolvedFailure.js.map +1 -0
  337. package/dist/esm/VacuousVerification.d.ts +237 -0
  338. package/dist/esm/VacuousVerification.d.ts.map +1 -0
  339. package/dist/esm/VacuousVerification.js +245 -0
  340. package/dist/esm/VacuousVerification.js.map +1 -0
  341. package/dist/esm/VariablesPanel.d.ts +117 -0
  342. package/dist/esm/VariablesPanel.d.ts.map +1 -0
  343. package/dist/esm/VariablesPanel.js +142 -0
  344. package/dist/esm/VariablesPanel.js.map +1 -0
  345. package/dist/esm/index.d.ts +172 -0
  346. package/dist/esm/index.d.ts.map +1 -0
  347. package/dist/esm/index.js +172 -0
  348. package/dist/esm/index.js.map +1 -0
  349. package/dist/esm/internal/bytes.d.ts +36 -0
  350. package/dist/esm/internal/bytes.d.ts.map +1 -0
  351. package/dist/esm/internal/bytes.js +70 -0
  352. package/dist/esm/internal/bytes.js.map +1 -0
  353. package/dist/esm/internal/cellPrompt.d.ts +122 -0
  354. package/dist/esm/internal/cellPrompt.d.ts.map +1 -0
  355. package/dist/esm/internal/cellPrompt.js +276 -0
  356. package/dist/esm/internal/cellPrompt.js.map +1 -0
  357. package/dist/esm/internal/compactable.d.ts +44 -0
  358. package/dist/esm/internal/compactable.d.ts.map +1 -0
  359. package/dist/esm/internal/compactable.js +71 -0
  360. package/dist/esm/internal/compactable.js.map +1 -0
  361. package/dist/esm/internal/compactionMarks.d.ts +278 -0
  362. package/dist/esm/internal/compactionMarks.d.ts.map +1 -0
  363. package/dist/esm/internal/compactionMarks.js +317 -0
  364. package/dist/esm/internal/compactionMarks.js.map +1 -0
  365. package/dist/esm/internal/demandText.d.ts +95 -0
  366. package/dist/esm/internal/demandText.d.ts.map +1 -0
  367. package/dist/esm/internal/demandText.js +128 -0
  368. package/dist/esm/internal/demandText.js.map +1 -0
  369. package/dist/esm/internal/elide.d.ts +109 -0
  370. package/dist/esm/internal/elide.d.ts.map +1 -0
  371. package/dist/esm/internal/elide.js +123 -0
  372. package/dist/esm/internal/elide.js.map +1 -0
  373. package/dist/esm/internal/frame.d.ts +463 -0
  374. package/dist/esm/internal/frame.d.ts.map +1 -0
  375. package/dist/esm/internal/frame.js +861 -0
  376. package/dist/esm/internal/frame.js.map +1 -0
  377. package/dist/esm/internal/nonNegativeSafeInt.d.ts +20 -0
  378. package/dist/esm/internal/nonNegativeSafeInt.d.ts.map +1 -0
  379. package/dist/esm/internal/nonNegativeSafeInt.js +20 -0
  380. package/dist/esm/internal/nonNegativeSafeInt.js.map +1 -0
  381. package/dist/esm/internal/paidUsage.d.ts +52 -0
  382. package/dist/esm/internal/paidUsage.d.ts.map +1 -0
  383. package/dist/esm/internal/paidUsage.js +72 -0
  384. package/dist/esm/internal/paidUsage.js.map +1 -0
  385. package/dist/esm/internal/printChannel.d.ts +233 -0
  386. package/dist/esm/internal/printChannel.d.ts.map +1 -0
  387. package/dist/esm/internal/printChannel.js +376 -0
  388. package/dist/esm/internal/printChannel.js.map +1 -0
  389. package/dist/esm/internal/printsObservation.d.ts +26 -0
  390. package/dist/esm/internal/printsObservation.d.ts.map +1 -0
  391. package/dist/esm/internal/printsObservation.js +29 -0
  392. package/dist/esm/internal/printsObservation.js.map +1 -0
  393. package/dist/esm/internal/refusal.d.ts +40 -0
  394. package/dist/esm/internal/refusal.d.ts.map +1 -0
  395. package/dist/esm/internal/refusal.js +47 -0
  396. package/dist/esm/internal/refusal.js.map +1 -0
  397. package/dist/esm/internal/supervision.d.ts +174 -0
  398. package/dist/esm/internal/supervision.d.ts.map +1 -0
  399. package/dist/esm/internal/supervision.js +485 -0
  400. package/dist/esm/internal/supervision.js.map +1 -0
  401. package/dist/esm/internal/unfinishedWork.d.ts +99 -0
  402. package/dist/esm/internal/unfinishedWork.d.ts.map +1 -0
  403. package/dist/esm/internal/unfinishedWork.js +85 -0
  404. package/dist/esm/internal/unfinishedWork.js.map +1 -0
  405. package/dist/esm/internal/unobservedCall.d.ts +112 -0
  406. package/dist/esm/internal/unobservedCall.d.ts.map +1 -0
  407. package/dist/esm/internal/unobservedCall.js +501 -0
  408. package/dist/esm/internal/unobservedCall.js.map +1 -0
  409. package/dist/esm/internal/untrustedData.d.ts +14 -0
  410. package/dist/esm/internal/untrustedData.d.ts.map +1 -0
  411. package/dist/esm/internal/untrustedData.js +15 -0
  412. package/dist/esm/internal/untrustedData.js.map +1 -0
  413. package/docs/README.md +129 -0
  414. package/docs/api.md +1869 -0
  415. package/docs/concepts.md +238 -0
  416. package/docs/external-transcripts.md +245 -0
  417. package/docs/guides/bind-flows.md +167 -0
  418. package/docs/guides/drive-the-loop.md +265 -0
  419. package/docs/guides/run-cells.md +262 -0
  420. package/docs/guides/workerd.md +144 -0
  421. package/docs/installation.md +69 -0
  422. package/docs/quickstart.md +121 -0
  423. package/docs/reference.md +954 -0
  424. package/docs/troubleshooting.md +177 -0
  425. package/package.json +463 -3
  426. package/src/AgentEvent.ts +1772 -0
  427. package/src/CallLedger.ts +560 -0
  428. package/src/Cell.ts +926 -0
  429. package/src/CellCalls.ts +198 -0
  430. package/src/CellHistory.ts +128 -0
  431. package/src/CellTurn.ts +4450 -0
  432. package/src/CellValidation.ts +382 -0
  433. package/src/Compaction.ts +330 -0
  434. package/src/CompletionClaim.ts +1013 -0
  435. package/src/ContextWindow.ts +669 -0
  436. package/src/EngineLike.ts +614 -0
  437. package/src/ExternalTranscript.ts +1143 -0
  438. package/src/FailedCall.ts +163 -0
  439. package/src/FlowBinding.ts +603 -0
  440. package/src/HarnessError.ts +98 -0
  441. package/src/Judgement.ts +526 -0
  442. package/src/Monitor.ts +579 -0
  443. package/src/NarrowedCheck.ts +694 -0
  444. package/src/Notifications.ts +262 -0
  445. package/src/Plan.ts +113 -0
  446. package/src/QuickJSSandbox.ts +1557 -0
  447. package/src/Relevance.ts +352 -0
  448. package/src/Sandbox.ts +908 -0
  449. package/src/Steering.ts +405 -0
  450. package/src/StructuredOutput.ts +484 -0
  451. package/src/Sufficiency.ts +247 -0
  452. package/src/Supervisor.ts +798 -0
  453. package/src/Tokens.ts +108 -0
  454. package/src/Transcript.ts +513 -0
  455. package/src/TruncatedOutput.ts +297 -0
  456. package/src/UnmovedTree.ts +121 -0
  457. package/src/UnresolvedFailure.ts +240 -0
  458. package/src/VacuousVerification.ts +280 -0
  459. package/src/VariablesPanel.ts +165 -0
  460. package/src/index.ts +204 -0
  461. package/src/internal/bytes.ts +70 -0
  462. package/src/internal/cellPrompt.ts +337 -0
  463. package/src/internal/compactable.ts +76 -0
  464. package/src/internal/compactionMarks.ts +454 -0
  465. package/src/internal/demandText.ts +145 -0
  466. package/src/internal/elide.ts +130 -0
  467. package/src/internal/frame.ts +1178 -0
  468. package/src/internal/nonNegativeSafeInt.ts +24 -0
  469. package/src/internal/paidUsage.ts +90 -0
  470. package/src/internal/printChannel.ts +434 -0
  471. package/src/internal/printsObservation.ts +31 -0
  472. package/src/internal/refusal.ts +53 -0
  473. package/src/internal/supervision.ts +652 -0
  474. package/src/internal/unfinishedWork.ts +123 -0
  475. package/src/internal/unobservedCall.ts +536 -0
  476. package/src/internal/untrustedData.ts +19 -0
@@ -0,0 +1,430 @@
1
+ /**
2
+ * Turning one agent's final text into a value the declared output schema
3
+ * accepts, or into a typed failure.
4
+ *
5
+ * A model may answer with a bare JSON document, prose wrapped around a JSON
6
+ * document, a fenced block, or JSON of the wrong shape. Downstream nodes must
7
+ * never receive that ambiguity, so this module implements the recovery
8
+ * contract in `../docs/concepts.md#structured-output`:
9
+ *
10
+ * 1. parse the complete, BOM-stripped response and decode it;
11
+ * 2. otherwise scan once for balanced JSON containers, take the container
12
+ * whose matching close ends last, and decode that;
13
+ * 3. otherwise report a typed {@link StructuredOutputFailure} carrying bounded
14
+ * diagnostics, which is what a caller re-prompts with when it still holds a
15
+ * correction slot.
16
+ *
17
+ * Every candidate is decoded by the declared schema. Extraction never relaxes
18
+ * validation: it only decides which bytes are offered to it, and the schema may
19
+ * accept any JSON root rather than silently requiring an object.
20
+ *
21
+ * The prompt half is {@link instructions}. It renders the declared schema as a
22
+ * JSON Schema document placed in the run's system teaching, which is BAML's
23
+ * `ctx.output_format` injection reached through the seam the vault note
24
+ * specifies — the model is told the shape before it answers, and the answer is
25
+ * still validated locally. Provider acceptance is not a cast.
26
+ *
27
+ * Reference consulted: the `generateObject` function from Effect's
28
+ * `effect/unstable/ai/LanguageModel` module, which
29
+ * sends `responseFormat: { type: "json", schema }` and then decodes the result
30
+ * with the same schema, failing `AiError.InvalidOutputError` on mismatch. The
31
+ * decode-and-fail-typed half is copied. The provider `responseFormat` half is
32
+ * not reachable here: `@smthrs/model`'s `ModelRequest.GenerationParams` has no
33
+ * response-format field, and the cell loop's final answer is a `Cell.Complete`
34
+ * `output` string rather than a provider-decoded value, so the schema is
35
+ * carried in the prompt and enforced at this boundary.
36
+ *
37
+ * @since 0.1.0
38
+ */
39
+ import * as Digest from "@smthrs/core/Digest";
40
+ import * as Fault from "@smthrs/flow/Fault";
41
+ import * as Effect from "effect/Effect";
42
+ import * as Schema from "effect/Schema";
43
+ import * as SchemaIssue from "effect/SchemaIssue";
44
+ import * as elide from "./internal/elide.js";
45
+ /**
46
+ * Stable reasons a structured-output boundary failed.
47
+ *
48
+ * `invalid_json` means candidate text was not JSON. `schema_mismatch` means
49
+ * JSON parsed but the declared schema refused it. `no_candidate` means the
50
+ * answer contained no candidate text. `correction_exhausted` means the
51
+ * boundary had already spent its correction budget.
52
+ *
53
+ * @category models
54
+ * @since 1.0.0-rc.0
55
+ * @slop
56
+ */
57
+ export const StructuredOutputFailureCode = Schema.Literals([
58
+ "invalid_json",
59
+ "schema_mismatch",
60
+ "no_candidate",
61
+ "correction_exhausted"
62
+ ]);
63
+ /**
64
+ * Stable reasons an individual structured-output issue was reported.
65
+ *
66
+ * `invalid_json` means candidate text was not JSON. `no_candidate` means the
67
+ * answer contained no candidate text. `invalid_type` means a decoded value had
68
+ * the wrong type. `invalid_value` means a value of the expected type was still
69
+ * invalid. `missing_key` means a required property was absent.
70
+ * `unexpected_key` means a prohibited extra property was present. `forbidden`
71
+ * means decoding attempted an operation the schema forbids. `one_of` means
72
+ * more than one exclusive union member matched. `constraint` means a schema
73
+ * check or empty-union fallback failed.
74
+ *
75
+ * @category models
76
+ * @since 1.0.0-rc.0
77
+ * @slop
78
+ */
79
+ export const OutputIssueCode = Schema.Literals([
80
+ "invalid_json",
81
+ "no_candidate",
82
+ "invalid_type",
83
+ "invalid_value",
84
+ "missing_key",
85
+ "unexpected_key",
86
+ "forbidden",
87
+ "one_of",
88
+ "constraint"
89
+ ]);
90
+ /**
91
+ * One bounded issue raised while decoding structured output.
92
+ *
93
+ * @category models
94
+ * @since 1.0.0-rc.0
95
+ * @slop
96
+ */
97
+ export class OutputIssue extends Schema.Class("flows/harness/StructuredOutput/OutputIssue")({
98
+ /** The stable reason this issue was reported. */
99
+ code: OutputIssueCode,
100
+ /** The JSON path of the offending value, dot/bracket joined; empty for the root. */
101
+ path: Schema.String,
102
+ /** What the decoder wanted there. */
103
+ message: Schema.String
104
+ }) {
105
+ /** Renders the issue for existing prose-oriented consumers. */
106
+ toString() {
107
+ return this.path === "" ? this.message : `${this.path}: ${this.message}`;
108
+ }
109
+ }
110
+ /**
111
+ * The terminal failure of one structured-output boundary.
112
+ *
113
+ * It carries what an operator needs to tell two identical-looking refusals
114
+ * apart: which schema was demanded, how many correction slots the attempt
115
+ * consumed out of its budget, the digest of the last candidate that was
116
+ * offered to the decoder, and the bounded issue list.
117
+ *
118
+ * @category errors
119
+ * @since 0.1.0
120
+ * @slop
121
+ */
122
+ export class StructuredOutputFailure extends Schema.TaggedError()("/harness/StructuredOutputFailure", {
123
+ /** The stable reason this boundary failed. */
124
+ code: StructuredOutputFailureCode,
125
+ /** The declared output schema's canonical digest. */
126
+ schema: Schema.String,
127
+ /** SHA-256 of the text the decoder was last offered. */
128
+ candidate: Schema.String,
129
+ /** Correction attempts already spent on this boundary. */
130
+ corrections: Schema.Number,
131
+ /** The correction budget the boundary was declared with. */
132
+ limit: Schema.Number,
133
+ /** At most {@link maxIssues} structured validation issues. */
134
+ issues: Schema.Array(OutputIssue),
135
+ message: Schema.String
136
+ }) {
137
+ }
138
+ // The model's answer did not fit the declared shape: another model may answer it.
139
+ Fault.register("/harness/StructuredOutputFailure", "dependency");
140
+ /**
141
+ * The most validation issues a failure or a correction prompt carries.
142
+ *
143
+ * Bounded for the same reason smithers bounds its own validation summary: a
144
+ * wide struct that mismatched everywhere would otherwise spend the correction
145
+ * prompt's whole budget restating the schema back to the model.
146
+ *
147
+ * @category constants
148
+ * @since 0.1.0
149
+ * @slop
150
+ */
151
+ export const maxIssues = 5;
152
+ /**
153
+ * The JSON Schema document for a declared output schema.
154
+ *
155
+ * @category conversions
156
+ * @since 0.1.0
157
+ * @slop
158
+ */
159
+ export const jsonSchema = (schema) => {
160
+ const document = Schema.toJsonSchemaDocument(schema, { onExcessProperty: "error" });
161
+ return Object.keys(document.definitions).length > 0
162
+ ? { ...document.schema, $defs: document.definitions }
163
+ : document.schema;
164
+ };
165
+ /**
166
+ * The canonical digest of a declared output schema.
167
+ *
168
+ * Two declarations that render the same JSON Schema name the same boundary, so
169
+ * this is what a step key folds in rather than the schema value itself.
170
+ *
171
+ * @category identity
172
+ * @since 0.1.0
173
+ * @slop
174
+ */
175
+ export const digest = (schema) => Digest.digest(Digest.canonical(jsonSchema(schema)));
176
+ /**
177
+ * The system teaching that tells a run what its final `output` must be.
178
+ *
179
+ * Written against the cell contract: a cell finishes by calling
180
+ * `ctx.done(output)`, and the sandbox rejects a returned transition, so the
181
+ * instruction is about the argument of that call and not about the assistant
182
+ * message around it. The run passes the value itself: a structured value
183
+ * reaches {@link decode} as canonical JSON, and a string as its own text.
184
+ *
185
+ * @category prompts
186
+ * @since 0.1.0
187
+ * @slop
188
+ */
189
+ export const instructions = (schema) => `## Required output shape
190
+
191
+ Finish by calling \`ctx.done(output)\` with the value itself: a JavaScript
192
+ value that validates against this JSON Schema, such as
193
+ \`ctx.done({ ...fields })\`. Do not stringify it, and add no prose around it.
194
+
195
+ \`\`\`json
196
+ ${JSON.stringify(jsonSchema(schema), null, 2)}
197
+ \`\`\``;
198
+ /**
199
+ * The digest of one failure's rendered validation issues.
200
+ *
201
+ * Two rejections that raised the same issues share this digest, which is what a
202
+ * record carries instead of the issues themselves: a journal that repeated the
203
+ * validator's prose for every correction would grow with the model's verbosity,
204
+ * and what an operator actually asks is whether the second answer was wrong the
205
+ * same way as the first.
206
+ *
207
+ * @category identity
208
+ * @since 0.1.0
209
+ */
210
+ export const issuesDigest = (failure) => Digest.digest(Digest.canonical(failure.issues.map((issue) => ({ path: issue.path, message: issue.message }))));
211
+ /**
212
+ * The correction teaching appended when a candidate failed to decode.
213
+ *
214
+ * It restates the schema and the bounded diagnostics and nothing else: a
215
+ * correction does not reinterpret the task, so the original prompt is repeated
216
+ * verbatim by the caller and only this section is new.
217
+ *
218
+ * @category prompts
219
+ * @since 0.1.0
220
+ * @slop
221
+ */
222
+ export const correction = (failure) => `## Your previous answer did not validate
223
+
224
+ The output you passed to \`ctx.done\` could not be decoded against the required
225
+ schema. Call \`ctx.done(output)\` again with the same information as a value
226
+ that validates.
227
+
228
+ Validation issues:
229
+ ${failure.issues.map((issue) => `- ${issue}`).join("\n")}`;
230
+ const stripBom = (text) => text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
231
+ /**
232
+ * The balanced JSON container whose matching close ends last.
233
+ *
234
+ * Scans once, left to right, tracking object and array delimiters, quoted
235
+ * strings, and escapes. A mismatched close abandons the open container rather
236
+ * than pairing it with the wrong opener, so a truncated prefix cannot be
237
+ * reported as a complete document. `undefined` means the text holds no
238
+ * balanced container at all.
239
+ *
240
+ * This is the array-capable form of smithers' `extractLastBalancedJson`, which
241
+ * is object-only.
242
+ *
243
+ * @category extraction
244
+ * @since 0.1.0
245
+ * @slop
246
+ */
247
+ export const lastBalanced = (text) => {
248
+ const stack = [];
249
+ let start = -1;
250
+ let found = undefined;
251
+ let quoted = false;
252
+ let escaped = false;
253
+ for (let index = 0; index < text.length; index++) {
254
+ const char = text[index];
255
+ if (quoted) {
256
+ if (escaped)
257
+ escaped = false;
258
+ else if (char === "\\")
259
+ escaped = true;
260
+ else if (char === "\"")
261
+ quoted = false;
262
+ continue;
263
+ }
264
+ if (char === "\"") {
265
+ if (stack.length > 0)
266
+ quoted = true;
267
+ continue;
268
+ }
269
+ if (char === "{" || char === "[") {
270
+ if (stack.length === 0)
271
+ start = index;
272
+ stack.push(char);
273
+ continue;
274
+ }
275
+ if (char === "}" || char === "]") {
276
+ const opener = stack.pop();
277
+ if (opener === undefined)
278
+ continue;
279
+ if ((char === "}") !== (opener === "{")) {
280
+ // Mismatched close: abandon the whole open container rather than
281
+ // reporting a pairing the text does not contain.
282
+ stack.length = 0;
283
+ start = -1;
284
+ continue;
285
+ }
286
+ if (stack.length === 0 && start >= 0)
287
+ found = text.slice(start, index + 1);
288
+ }
289
+ }
290
+ return found;
291
+ };
292
+ /**
293
+ * The candidates offered to the decoder, in the declared recovery order.
294
+ *
295
+ * @category extraction
296
+ * @since 0.1.0
297
+ * @slop
298
+ */
299
+ export const candidates = (text) => {
300
+ const stripped = stripBom(text).trim();
301
+ const balanced = lastBalanced(stripped);
302
+ return balanced === undefined || balanced === stripped ? [stripped] : [stripped, balanced];
303
+ };
304
+ const issueMessage = (message) => elide.head(message, 240, "reissue the answer to see the whole issue");
305
+ const issuePath = (path) => path.map((segment, index) => typeof segment === "number" ? `[${segment}]` : `${index === 0 ? "" : "."}${String(segment)}`).join("");
306
+ const issueCodeSeparator = "\u0000";
307
+ const leafCodes = {
308
+ InvalidType: "invalid_type",
309
+ InvalidValue: "invalid_value",
310
+ MissingKey: "missing_key",
311
+ UnexpectedKey: "unexpected_key",
312
+ Forbidden: "forbidden",
313
+ OneOf: "one_of"
314
+ };
315
+ const formatSchemaIssues = SchemaIssue.makeFormatterStandardSchemaV1({
316
+ leafHook: (issue) => `${issue._tag}${issueCodeSeparator}${SchemaIssue.defaultLeafHook(issue)}`
317
+ });
318
+ const schemaIssuesOf = (error) => formatSchemaIssues(error.issue).issues.slice(0, maxIssues).map((issue) => {
319
+ const separator = issue.message.indexOf(issueCodeSeparator);
320
+ const tagged = separator >= 0;
321
+ return new OutputIssue({
322
+ code: tagged ? leafCodes[issue.message.slice(0, separator)] : "constraint",
323
+ path: issuePath(issue.path),
324
+ message: issueMessage(tagged ? issue.message.slice(separator + issueCodeSeparator.length) : issue.message)
325
+ });
326
+ });
327
+ const textIssuesOf = (code, message) => message.split("\n").map((line) => line.trim()).filter((line) => line.length > 0).slice(0, maxIssues).map((line) => new OutputIssue({ code, path: "", message: issueMessage(line) }));
328
+ const quoted = (text) => {
329
+ try {
330
+ return typeof JSON.parse(text) === "string";
331
+ }
332
+ catch {
333
+ return false;
334
+ }
335
+ };
336
+ /**
337
+ * Decodes one agent answer with the declared output schema.
338
+ *
339
+ * Decodes an original completion value first when supplied. Only a string
340
+ * refused for its type proceeds to legacy text extraction; refinements and
341
+ * nonstring failures retain the original value's diagnostics.
342
+ * Tries every {@link candidates} entry in order and returns the first the
343
+ * schema accepts, then the answer itself, untrimmed, as a string unless it is
344
+ * a JSON string literal, which is how a `ctx.done("text")` answer reaches a
345
+ * string schema. When none does, the failure reports the issues raised by the
346
+ * LAST candidate, which is the narrowest text the extractor could isolate and
347
+ * therefore the one a correction prompt should be about.
348
+ *
349
+ * @category decoding
350
+ * @since 0.1.0
351
+ * @slop
352
+ */
353
+ export const decode = (schema, text, attempt, value) => Effect.gen(function* () {
354
+ const offered = candidates(text);
355
+ const decoder = Schema.decodeUnknownEffect(schema);
356
+ let code = "no_candidate";
357
+ let issues = [
358
+ new OutputIssue({ code: "no_candidate", path: "", message: "the answer held no JSON document" })
359
+ ];
360
+ if (value !== undefined) {
361
+ const result = yield* Effect.result(decoder(value));
362
+ if (result._tag === "Success")
363
+ return result.success;
364
+ const rawIssues = schemaIssuesOf(result.failure);
365
+ // Extraction is only for text offered to a schema expecting another type.
366
+ // Preserve string refinements and nonstring type failures on the value.
367
+ if (typeof value !== "string" || !rawIssues.every((issue) => issue.code === "invalid_type")) {
368
+ return yield* new StructuredOutputFailure({
369
+ code: attempt.corrections >= attempt.limit ? "correction_exhausted" : "schema_mismatch",
370
+ schema: digest(schema),
371
+ candidate: Digest.digest(JSON.stringify(value)),
372
+ corrections: attempt.corrections,
373
+ limit: attempt.limit,
374
+ issues: rawIssues,
375
+ message: `The agent's answer did not validate against its declared output schema after ${attempt.corrections} of ${attempt.limit} corrections`
376
+ });
377
+ }
378
+ }
379
+ for (const candidate of offered) {
380
+ if (candidate.length === 0)
381
+ continue;
382
+ let parsed;
383
+ try {
384
+ parsed = JSON.parse(candidate);
385
+ }
386
+ catch (error) {
387
+ code = "invalid_json";
388
+ // The ECMAScript JSON parser throws a SyntaxError with a message.
389
+ issues = textIssuesOf("invalid_json", error.message);
390
+ continue;
391
+ }
392
+ const result = yield* Effect.result(decoder(parsed));
393
+ if (result._tag === "Success")
394
+ return result.success;
395
+ code = "schema_mismatch";
396
+ issues = schemaIssuesOf(result.failure);
397
+ }
398
+ // `ctx.done("text")` reaches the boundary as the text itself, not as a
399
+ // JSON string, so a schema that accepts the raw answer takes it as it is.
400
+ // An answer that is a whole JSON string literal already said which string
401
+ // it meant, and its quotes are not part of it.
402
+ // Untrimmed, so an empty or whitespace-bearing string arrives as written.
403
+ const raw = stripBom(text);
404
+ if (!quoted(offered[0])) {
405
+ const result = yield* Effect.result(decoder(raw));
406
+ if (result._tag === "Success")
407
+ return result.success;
408
+ // A string the schema read and refused on its merits (too short, not
409
+ // one of the literals) is the issue to correct, not the JSON parse. A
410
+ // schema that wanted no string at all keeps the JSON diagnosis.
411
+ const rawIssues = schemaIssuesOf(result.failure);
412
+ if (code === "invalid_json" && rawIssues[0]?.code !== "invalid_type") {
413
+ code = "schema_mismatch";
414
+ issues = rawIssues;
415
+ }
416
+ }
417
+ return yield* new StructuredOutputFailure({
418
+ // Once the budget is spent, exhaustion is the actionable classification;
419
+ // the issue records retain whether the last candidate was invalid JSON or
420
+ // failed schema decoding.
421
+ code: attempt.corrections >= attempt.limit ? "correction_exhausted" : code,
422
+ schema: digest(schema),
423
+ candidate: Digest.digest(offered[offered.length - 1]),
424
+ corrections: attempt.corrections,
425
+ limit: attempt.limit,
426
+ issues,
427
+ message: `The agent's answer did not validate against its declared output schema after ${attempt.corrections} of ${attempt.limit} corrections`
428
+ });
429
+ });
430
+ //# sourceMappingURL=StructuredOutput.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StructuredOutput.js","sourceRoot":"","sources":["../../src/StructuredOutput.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,KAAK,MAAM,MAAM,qBAAqB,CAAA;AAC7C,OAAO,KAAK,KAAK,MAAM,oBAAoB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,WAAW,MAAM,oBAAoB,CAAA;AACjD,OAAO,KAAK,KAAK,MAAM,qBAAqB,CAAA;AAE5C;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,MAAM,CAAC,QAAQ,CAAC;IACzD,cAAc;IACd,iBAAiB;IACjB,cAAc;IACd,sBAAsB;CACvB,CAAC,CAAA;AAWF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,QAAQ,CAAC;IAC7C,cAAc;IACd,cAAc;IACd,cAAc;IACd,eAAe;IACf,aAAa;IACb,gBAAgB;IAChB,WAA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AC,CAAC,IAAI,uBAAuB,CAAC;QACxC,yEAAyE;QACzE,0EAA0E;QAC1E,0BAA0B;QAC1B,IAAI,EAAE,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,sBAAsB,CAAC,CAAC,CAAC,IAAI;QAC1E,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC;QACtB,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAE,CAAC;QACtD,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,MAAM;QACN,OAAO,EACL,gFAAgF,OAAO,CAAC,WAAW,OAAO,OAAO,CAAC,KAAK,cAAc;KACxI,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["/**\n * Turning one agent's final text into a value the declared output schema\n * accepts, or into a typed failure.\n *\n * A model may answer with a bare JSON document, prose wrapped around a JSON\n * document, a fenced block, or JSON of the wrong shape. Downstream nodes must\n * never receive that ambiguity, so this module implements the recovery\n * contract in `../docs/concepts.md#structured-output`:\n *\n * 1. parse the complete, BOM-stripped response and decode it;\n * 2. otherwise scan once for balanced JSON containers, take the container\n * whose matching close ends last, and decode that;\n * 3. otherwise report a typed {@link StructuredOutputFailure} carrying bounded\n * diagnostics, which is what a caller re-prompts with when it still holds a\n * correction slot.\n *\n * Every candidate is decoded by the declared schema. Extraction never relaxes\n * validation: it only decides which bytes are offered to it, and the schema may\n * accept any JSON root rather than silently requiring an object.\n *\n * The prompt half is {@link instructions}. It renders the declared schema as a\n * JSON Schema document placed in the run's system teaching, which is BAML's\n * `ctx.output_format` injection reached through the seam the vault note\n * specifies — the model is told the shape before it answers, and the answer is\n * still validated locally. Provider acceptance is not a cast.\n *\n * Reference consulted: the `generateObject` function from Effect's\n * `effect/unstable/ai/LanguageModel` module, which\n * sends `responseFormat: { type: \"json\", schema }` and then decodes the result\n * with the same schema, failing `AiError.InvalidOutputError` on mismatch. The\n * decode-and-fail-typed half is copied. The provider `responseFormat` half is\n * not reachable here: `@smthrs/model`'s `ModelRequest.GenerationParams` has no\n * response-format field, and the cell loop's final answer is a `Cell.Complete`\n * `output` string rather than a provider-decoded value, so the schema is\n * carried in the prompt and enforced at this boundary.\n *\n * @since 0.1.0\n */\n\nimport * as Digest from \"@smthrs/core/Digest\"\nimport * as Fault from \"@smthrs/flow/Fault\"\nimport * as Effect from \"effect/Effect\"\nimport * as Schema from \"effect/Schema\"\nimport * as SchemaIssue from \"effect/SchemaIssue\"\nimport * as elide from \"./internal/elide.ts\"\n\n/**\n * Stable reasons a structured-output boundary failed.\n *\n * `invalid_json` means candidate text was not JSON. `schema_mismatch` means\n * JSON parsed but the declared schema refused it. `no_candidate` means the\n * answer contained no candidate text. `correction_exhausted` means the\n * boundary had already spent its correction budget.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const StructuredOutputFailureCode = Schema.Literals([\n \"invalid_json\",\n \"schema_mismatch\",\n \"no_candidate\",\n \"correction_exhausted\"\n])\n\n/**\n * Stable reasons a structured-output boundary failed.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport type StructuredOutputFailureCode = typeof StructuredOutputFailureCode.Type\n\n/**\n * Stable reasons an individual structured-output issue was reported.\n *\n * `invalid_json` means candidate text was not JSON. `no_candidate` means the\n * answer contained no candidate text. `invalid_type` means a decoded value had\n * the wrong type. `invalid_value` means a value of the expected type was still\n * invalid. `missing_key` means a required property was absent.\n * `unexpected_key` means a prohibited extra property was present. `forbidden`\n * means decoding attempted an operation the schema forbids. `one_of` means\n * more than one exclusive union member matched. `constraint` means a schema\n * check or empty-union fallback failed.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const OutputIssueCode = Schema.Literals([\n \"invalid_json\",\n \"no_candidate\",\n \"invalid_type\",\n \"invalid_value\",\n \"missing_key\",\n \"unexpected_key\",\n \"forbidden\",\n \"one_of\",\n \"constraint\"\n])\n\n/**\n * Stable reasons an individual structured-output issue was reported.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport type OutputIssueCode = typeof OutputIssueCode.Type\n\n/**\n * One bounded issue raised while decoding structured output.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport class OutputIssue extends Schema.Class<OutputIssue>(\"flows/harness/StructuredOutput/OutputIssue\")({\n /** The stable reason this issue was reported. */\n code: OutputIssueCode,\n /** The JSON path of the offending value, dot/bracket joined; empty for the root. */\n path: Schema.String,\n /** What the decoder wanted there. */\n message: Schema.String\n}) {\n /** Renders the issue for existing prose-oriented consumers. */\n override toString(): string {\n return this.path === \"\" ? this.message : `${this.path}: ${this.message}`\n }\n}\n\n/**\n * The terminal failure of one structured-output boundary.\n *\n * It carries what an operator needs to tell two identical-looking refusals\n * apart: which schema was demanded, how many correction slots the attempt\n * consumed out of its budget, the digest of the last candidate that was\n * offered to the decoder, and the bounded issue list.\n *\n * @category errors\n * @since 0.1.0\n * @slop\n */\nexport class StructuredOutputFailure extends Schema.TaggedError<StructuredOutputFailure>()(\n \"/harness/StructuredOutputFailure\",\n {\n /** The stable reason this boundary failed. */\n code: StructuredOutputFailureCode,\n /** The declared output schema's canonical digest. */\n schema: Schema.String,\n /** SHA-256 of the text the decoder was last offered. */\n candidate: Schema.String,\n /** Correction attempts already spent on this boundary. */\n corrections: Schema.Number,\n /** The correction budget the boundary was declared with. */\n limit: Schema.Number,\n /** At most {@link maxIssues} structured validation issues. */\n issues: Schema.Array(OutputIssue),\n message: Schema.String\n }\n) {}\n// The model's answer did not fit the declared shape: another model may answer it.\nFault.register(\"/harness/StructuredOutputFailure\", \"dependency\")\n\n/**\n * The most validation issues a failure or a correction prompt carries.\n *\n * Bounded for the same reason smithers bounds its own validation summary: a\n * wide struct that mismatched everywhere would otherwise spend the correction\n * prompt's whole budget restating the schema back to the model.\n *\n * @category constants\n * @since 0.1.0\n * @slop\n */\nexport const maxIssues = 5\n\n/**\n * The JSON Schema document for a declared output schema.\n *\n * @category conversions\n * @since 0.1.0\n * @slop\n */\nexport const jsonSchema = (schema: Schema.Top): unknown => {\n const document = Schema.toJsonSchemaDocument(schema, { onExcessProperty: \"error\" })\n return Object.keys(document.definitions).length > 0\n ? { ...document.schema, $defs: document.definitions }\n : document.schema\n}\n\n/**\n * The canonical digest of a declared output schema.\n *\n * Two declarations that render the same JSON Schema name the same boundary, so\n * this is what a step key folds in rather than the schema value itself.\n *\n * @category identity\n * @since 0.1.0\n * @slop\n */\nexport const digest = (schema: Schema.Top): string => Digest.digest(Digest.canonical(jsonSchema(schema)))\n\n/**\n * The system teaching that tells a run what its final `output` must be.\n *\n * Written against the cell contract: a cell finishes by calling\n * `ctx.done(output)`, and the sandbox rejects a returned transition, so the\n * instruction is about the argument of that call and not about the assistant\n * message around it. The run passes the value itself: a structured value\n * reaches {@link decode} as canonical JSON, and a string as its own text.\n *\n * @category prompts\n * @since 0.1.0\n * @slop\n */\nexport const instructions = (schema: Schema.Top): string =>\n `## Required output shape\n\nFinish by calling \\`ctx.done(output)\\` with the value itself: a JavaScript\nvalue that validates against this JSON Schema, such as\n\\`ctx.done({ ...fields })\\`. Do not stringify it, and add no prose around it.\n\n\\`\\`\\`json\n${JSON.stringify(jsonSchema(schema), null, 2)}\n\\`\\`\\``\n\n/**\n * The digest of one failure's rendered validation issues.\n *\n * Two rejections that raised the same issues share this digest, which is what a\n * record carries instead of the issues themselves: a journal that repeated the\n * validator's prose for every correction would grow with the model's verbosity,\n * and what an operator actually asks is whether the second answer was wrong the\n * same way as the first.\n *\n * @category identity\n * @since 0.1.0\n */\nexport const issuesDigest = (failure: StructuredOutputFailure): string =>\n Digest.digest(\n Digest.canonical(failure.issues.map((issue) => ({ path: issue.path, message: issue.message })))\n )\n\n/**\n * The correction teaching appended when a candidate failed to decode.\n *\n * It restates the schema and the bounded diagnostics and nothing else: a\n * correction does not reinterpret the task, so the original prompt is repeated\n * verbatim by the caller and only this section is new.\n *\n * @category prompts\n * @since 0.1.0\n * @slop\n */\nexport const correction = (failure: StructuredOutputFailure): string =>\n `## Your previous answer did not validate\n\nThe output you passed to \\`ctx.done\\` could not be decoded against the required\nschema. Call \\`ctx.done(output)\\` again with the same information as a value\nthat validates.\n\nValidation issues:\n${failure.issues.map((issue) => `- ${issue}`).join(\"\\n\")}`\n\nconst stripBom = (text: string): string => text.charCodeAt(0) === 0xfeff ? text.slice(1) : text\n\n/**\n * The balanced JSON container whose matching close ends last.\n *\n * Scans once, left to right, tracking object and array delimiters, quoted\n * strings, and escapes. A mismatched close abandons the open container rather\n * than pairing it with the wrong opener, so a truncated prefix cannot be\n * reported as a complete document. `undefined` means the text holds no\n * balanced container at all.\n *\n * This is the array-capable form of smithers' `extractLastBalancedJson`, which\n * is object-only.\n *\n * @category extraction\n * @since 0.1.0\n * @slop\n */\nexport const lastBalanced = (text: string): string | undefined => {\n const stack: Array<string> = []\n let start = -1\n let found: string | undefined = undefined\n let quoted = false\n let escaped = false\n for (let index = 0; index < text.length; index++) {\n const char = text[index]!\n if (quoted) {\n if (escaped) escaped = false\n else if (char === \"\\\\\") escaped = true\n else if (char === \"\\\"\") quoted = false\n continue\n }\n if (char === \"\\\"\") {\n if (stack.length > 0) quoted = true\n continue\n }\n if (char === \"{\" || char === \"[\") {\n if (stack.length === 0) start = index\n stack.push(char)\n continue\n }\n if (char === \"}\" || char === \"]\") {\n const opener = stack.pop()\n if (opener === undefined) continue\n if ((char === \"}\") !== (opener === \"{\")) {\n // Mismatched close: abandon the whole open container rather than\n // reporting a pairing the text does not contain.\n stack.length = 0\n start = -1\n continue\n }\n if (stack.length === 0 && start >= 0) found = text.slice(start, index + 1)\n }\n }\n return found\n}\n\n/**\n * The candidates offered to the decoder, in the declared recovery order.\n *\n * @category extraction\n * @since 0.1.0\n * @slop\n */\nexport const candidates = (text: string): ReadonlyArray<string> => {\n const stripped = stripBom(text).trim()\n const balanced = lastBalanced(stripped)\n return balanced === undefined || balanced === stripped ? [stripped] : [stripped, balanced]\n}\n\nconst issueMessage = (message: string): string => elide.head(message, 240, \"reissue the answer to see the whole issue\")\n\nconst issuePath = (path: ReadonlyArray<PropertyKey>): string =>\n path.map((segment, index) =>\n typeof segment === \"number\" ? `[${segment}]` : `${index === 0 ? \"\" : \".\"}${String(segment)}`\n ).join(\"\")\n\ntype FormattedIssue = {\n readonly message: string\n readonly path: ReadonlyArray<PropertyKey>\n}\n\nconst issueCodeSeparator = \"\\u0000\"\n\nconst leafCodes: Record<SchemaIssue.Leaf[\"_tag\"], OutputIssueCode> = {\n InvalidType: \"invalid_type\",\n InvalidValue: \"invalid_value\",\n MissingKey: \"missing_key\",\n UnexpectedKey: \"unexpected_key\",\n Forbidden: \"forbidden\",\n OneOf: \"one_of\"\n}\n\nconst formatSchemaIssues = SchemaIssue.makeFormatterStandardSchemaV1({\n leafHook: (issue) => `${issue._tag}${issueCodeSeparator}${SchemaIssue.defaultLeafHook(issue)}`\n})\n\nconst schemaIssuesOf = (error: Schema.SchemaError): ReadonlyArray<OutputIssue> =>\n (formatSchemaIssues(error.issue).issues as ReadonlyArray<FormattedIssue>).slice(0, maxIssues).map((issue) => {\n const separator = issue.message.indexOf(issueCodeSeparator)\n const tagged = separator >= 0\n return new OutputIssue({\n code: tagged ? leafCodes[issue.message.slice(0, separator) as SchemaIssue.Leaf[\"_tag\"]] : \"constraint\",\n path: issuePath(issue.path),\n message: issueMessage(tagged ? issue.message.slice(separator + issueCodeSeparator.length) : issue.message)\n })\n })\n\nconst textIssuesOf = (code: OutputIssueCode, message: string): ReadonlyArray<OutputIssue> =>\n message.split(\"\\n\").map((line) => line.trim()).filter((line) => line.length > 0).slice(0, maxIssues).map((line) =>\n new OutputIssue({ code, path: \"\", message: issueMessage(line) })\n )\n\nconst quoted = (text: string): boolean => {\n try {\n return typeof JSON.parse(text) === \"string\"\n } catch {\n return false\n }\n}\n\n/**\n * Decodes one agent answer with the declared output schema.\n *\n * Decodes an original completion value first when supplied. Only a string\n * refused for its type proceeds to legacy text extraction; refinements and\n * nonstring failures retain the original value's diagnostics.\n * Tries every {@link candidates} entry in order and returns the first the\n * schema accepts, then the answer itself, untrimmed, as a string unless it is\n * a JSON string literal, which is how a `ctx.done(\"text\")` answer reaches a\n * string schema. When none does, the failure reports the issues raised by the\n * LAST candidate, which is the narrowest text the extractor could isolate and\n * therefore the one a correction prompt should be about.\n *\n * @category decoding\n * @since 0.1.0\n * @slop\n */\nexport const decode = <S extends Schema.Top>(\n schema: S,\n text: string,\n attempt: { readonly corrections: number; readonly limit: number },\n value?: Schema.Json | undefined\n): Effect.Effect<S[\"Type\"], StructuredOutputFailure, S[\"DecodingServices\"]> =>\n Effect.gen(function*() {\n const offered = candidates(text)\n const decoder = Schema.decodeUnknownEffect(schema)\n let code: StructuredOutputFailureCode = \"no_candidate\"\n let issues: ReadonlyArray<OutputIssue> = [\n new OutputIssue({ code: \"no_candidate\", path: \"\", message: \"the answer held no JSON document\" })\n ]\n if (value !== undefined) {\n const result = yield* Effect.result(decoder(value))\n if (result._tag === \"Success\") return result.success\n const rawIssues = schemaIssuesOf(result.failure)\n // Extraction is only for text offered to a schema expecting another type.\n // Preserve string refinements and nonstring type failures on the value.\n if (typeof value !== \"string\" || !rawIssues.every((issue) => issue.code === \"invalid_type\")) {\n return yield* new StructuredOutputFailure({\n code: attempt.corrections >= attempt.limit ? \"correction_exhausted\" : \"schema_mismatch\",\n schema: digest(schema),\n candidate: Digest.digest(JSON.stringify(value)),\n corrections: attempt.corrections,\n limit: attempt.limit,\n issues: rawIssues,\n message:\n `The agent's answer did not validate against its declared output schema after ${attempt.corrections} of ${attempt.limit} corrections`\n })\n }\n }\n for (const candidate of offered) {\n if (candidate.length === 0) continue\n let parsed: unknown\n try {\n parsed = JSON.parse(candidate)\n } catch (error) {\n code = \"invalid_json\"\n // The ECMAScript JSON parser throws a SyntaxError with a message.\n issues = textIssuesOf(\"invalid_json\", (error as SyntaxError).message)\n continue\n }\n const result = yield* Effect.result(decoder(parsed))\n if (result._tag === \"Success\") return result.success\n code = \"schema_mismatch\"\n issues = schemaIssuesOf(result.failure)\n }\n // `ctx.done(\"text\")` reaches the boundary as the text itself, not as a\n // JSON string, so a schema that accepts the raw answer takes it as it is.\n // An answer that is a whole JSON string literal already said which string\n // it meant, and its quotes are not part of it.\n // Untrimmed, so an empty or whitespace-bearing string arrives as written.\n const raw = stripBom(text)\n if (!quoted(offered[0]!)) {\n const result = yield* Effect.result(decoder(raw))\n if (result._tag === \"Success\") return result.success\n // A string the schema read and refused on its merits (too short, not\n // one of the literals) is the issue to correct, not the JSON parse. A\n // schema that wanted no string at all keeps the JSON diagnosis.\n const rawIssues = schemaIssuesOf(result.failure)\n if (code === \"invalid_json\" && rawIssues[0]?.code !== \"invalid_type\") {\n code = \"schema_mismatch\"\n issues = rawIssues\n }\n }\n return yield* new StructuredOutputFailure({\n // Once the budget is spent, exhaustion is the actionable classification;\n // the issue records retain whether the last candidate was invalid JSON or\n // failed schema decoding.\n code: attempt.corrections >= attempt.limit ? \"correction_exhausted\" : code,\n schema: digest(schema),\n candidate: Digest.digest(offered[offered.length - 1]!),\n corrections: attempt.corrections,\n limit: attempt.limit,\n issues,\n message:\n `The agent's answer did not validate against its declared output schema after ${attempt.corrections} of ${attempt.limit} corrections`\n })\n })\n"]}