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

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 +985 -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 +1142 -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,252 @@
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 Effect from "effect/Effect";
40
+ import * as Schema from "effect/Schema";
41
+ /**
42
+ * Stable reasons a structured-output boundary failed.
43
+ *
44
+ * `invalid_json` means candidate text was not JSON. `schema_mismatch` means
45
+ * JSON parsed but the declared schema refused it. `no_candidate` means the
46
+ * answer contained no candidate text. `correction_exhausted` means the
47
+ * boundary had already spent its correction budget.
48
+ *
49
+ * @category models
50
+ * @since 1.0.0-rc.0
51
+ * @slop
52
+ */
53
+ export declare const StructuredOutputFailureCode: Schema.Literals<readonly ["invalid_json", "schema_mismatch", "no_candidate", "correction_exhausted"]>;
54
+ /**
55
+ * Stable reasons a structured-output boundary failed.
56
+ *
57
+ * @category models
58
+ * @since 1.0.0-rc.0
59
+ * @slop
60
+ */
61
+ export type StructuredOutputFailureCode = typeof StructuredOutputFailureCode.Type;
62
+ /**
63
+ * Stable reasons an individual structured-output issue was reported.
64
+ *
65
+ * `invalid_json` means candidate text was not JSON. `no_candidate` means the
66
+ * answer contained no candidate text. `invalid_type` means a decoded value had
67
+ * the wrong type. `invalid_value` means a value of the expected type was still
68
+ * invalid. `missing_key` means a required property was absent.
69
+ * `unexpected_key` means a prohibited extra property was present. `forbidden`
70
+ * means decoding attempted an operation the schema forbids. `one_of` means
71
+ * more than one exclusive union member matched. `constraint` means a schema
72
+ * check or empty-union fallback failed.
73
+ *
74
+ * @category models
75
+ * @since 1.0.0-rc.0
76
+ * @slop
77
+ */
78
+ export declare const OutputIssueCode: Schema.Literals<readonly ["invalid_json", "no_candidate", "invalid_type", "invalid_value", "missing_key", "unexpected_key", "forbidden", "one_of", "constraint"]>;
79
+ /**
80
+ * Stable reasons an individual structured-output issue was reported.
81
+ *
82
+ * @category models
83
+ * @since 1.0.0-rc.0
84
+ * @slop
85
+ */
86
+ export type OutputIssueCode = typeof OutputIssueCode.Type;
87
+ declare const OutputIssue_base: Schema.Class<OutputIssue, Schema.Struct<{
88
+ /** The stable reason this issue was reported. */
89
+ readonly code: Schema.Literals<readonly ["invalid_json", "no_candidate", "invalid_type", "invalid_value", "missing_key", "unexpected_key", "forbidden", "one_of", "constraint"]>;
90
+ /** The JSON path of the offending value, dot/bracket joined; empty for the root. */
91
+ readonly path: Schema.String;
92
+ /** What the decoder wanted there. */
93
+ readonly message: Schema.String;
94
+ }>, {}>;
95
+ /**
96
+ * One bounded issue raised while decoding structured output.
97
+ *
98
+ * @category models
99
+ * @since 1.0.0-rc.0
100
+ * @slop
101
+ */
102
+ export declare class OutputIssue extends OutputIssue_base {
103
+ /** Renders the issue for existing prose-oriented consumers. */
104
+ toString(): string;
105
+ }
106
+ declare const StructuredOutputFailure_base: Schema.Class<StructuredOutputFailure, Schema.TaggedStruct<"/harness/StructuredOutputFailure", {
107
+ /** The stable reason this boundary failed. */
108
+ readonly code: Schema.Literals<readonly ["invalid_json", "schema_mismatch", "no_candidate", "correction_exhausted"]>;
109
+ /** The declared output schema's canonical digest. */
110
+ readonly schema: Schema.String;
111
+ /** SHA-256 of the text the decoder was last offered. */
112
+ readonly candidate: Schema.String;
113
+ /** Correction attempts already spent on this boundary. */
114
+ readonly corrections: Schema.Number;
115
+ /** The correction budget the boundary was declared with. */
116
+ readonly limit: Schema.Number;
117
+ /** At most {@link maxIssues} structured validation issues. */
118
+ readonly issues: Schema.$Array<typeof OutputIssue>;
119
+ readonly message: Schema.String;
120
+ }>, import("effect/Cause").YieldableError>;
121
+ /**
122
+ * The terminal failure of one structured-output boundary.
123
+ *
124
+ * It carries what an operator needs to tell two identical-looking refusals
125
+ * apart: which schema was demanded, how many correction slots the attempt
126
+ * consumed out of its budget, the digest of the last candidate that was
127
+ * offered to the decoder, and the bounded issue list.
128
+ *
129
+ * @category errors
130
+ * @since 0.1.0
131
+ * @slop
132
+ */
133
+ export declare class StructuredOutputFailure extends StructuredOutputFailure_base {
134
+ }
135
+ /**
136
+ * The most validation issues a failure or a correction prompt carries.
137
+ *
138
+ * Bounded for the same reason smithers bounds its own validation summary: a
139
+ * wide struct that mismatched everywhere would otherwise spend the correction
140
+ * prompt's whole budget restating the schema back to the model.
141
+ *
142
+ * @category constants
143
+ * @since 0.1.0
144
+ * @slop
145
+ */
146
+ export declare const maxIssues = 5;
147
+ /**
148
+ * The JSON Schema document for a declared output schema.
149
+ *
150
+ * @category conversions
151
+ * @since 0.1.0
152
+ * @slop
153
+ */
154
+ export declare const jsonSchema: (schema: Schema.Top) => unknown;
155
+ /**
156
+ * The canonical digest of a declared output schema.
157
+ *
158
+ * Two declarations that render the same JSON Schema name the same boundary, so
159
+ * this is what a step key folds in rather than the schema value itself.
160
+ *
161
+ * @category identity
162
+ * @since 0.1.0
163
+ * @slop
164
+ */
165
+ export declare const digest: (schema: Schema.Top) => string;
166
+ /**
167
+ * The system teaching that tells a run what its final `output` must be.
168
+ *
169
+ * Written against the cell contract: a cell finishes by calling
170
+ * `ctx.done(output)`, and the sandbox rejects a returned transition, so the
171
+ * instruction is about the argument of that call and not about the assistant
172
+ * message around it. The run passes the value itself: a structured value
173
+ * reaches {@link decode} as canonical JSON, and a string as its own text.
174
+ *
175
+ * @category prompts
176
+ * @since 0.1.0
177
+ * @slop
178
+ */
179
+ export declare const instructions: (schema: Schema.Top) => string;
180
+ /**
181
+ * The digest of one failure's rendered validation issues.
182
+ *
183
+ * Two rejections that raised the same issues share this digest, which is what a
184
+ * record carries instead of the issues themselves: a journal that repeated the
185
+ * validator's prose for every correction would grow with the model's verbosity,
186
+ * and what an operator actually asks is whether the second answer was wrong the
187
+ * same way as the first.
188
+ *
189
+ * @category identity
190
+ * @since 0.1.0
191
+ */
192
+ export declare const issuesDigest: (failure: StructuredOutputFailure) => string;
193
+ /**
194
+ * The correction teaching appended when a candidate failed to decode.
195
+ *
196
+ * It restates the schema and the bounded diagnostics and nothing else: a
197
+ * correction does not reinterpret the task, so the original prompt is repeated
198
+ * verbatim by the caller and only this section is new.
199
+ *
200
+ * @category prompts
201
+ * @since 0.1.0
202
+ * @slop
203
+ */
204
+ export declare const correction: (failure: StructuredOutputFailure) => string;
205
+ /**
206
+ * The balanced JSON container whose matching close ends last.
207
+ *
208
+ * Scans once, left to right, tracking object and array delimiters, quoted
209
+ * strings, and escapes. A mismatched close abandons the open container rather
210
+ * than pairing it with the wrong opener, so a truncated prefix cannot be
211
+ * reported as a complete document. `undefined` means the text holds no
212
+ * balanced container at all.
213
+ *
214
+ * This is the array-capable form of smithers' `extractLastBalancedJson`, which
215
+ * is object-only.
216
+ *
217
+ * @category extraction
218
+ * @since 0.1.0
219
+ * @slop
220
+ */
221
+ export declare const lastBalanced: (text: string) => string | undefined;
222
+ /**
223
+ * The candidates offered to the decoder, in the declared recovery order.
224
+ *
225
+ * @category extraction
226
+ * @since 0.1.0
227
+ * @slop
228
+ */
229
+ export declare const candidates: (text: string) => ReadonlyArray<string>;
230
+ /**
231
+ * Decodes one agent answer with the declared output schema.
232
+ *
233
+ * Decodes an original completion value first when supplied. Only a string
234
+ * refused for its type proceeds to legacy text extraction; refinements and
235
+ * nonstring failures retain the original value's diagnostics.
236
+ * Tries every {@link candidates} entry in order and returns the first the
237
+ * schema accepts, then the answer itself, untrimmed, as a string unless it is
238
+ * a JSON string literal, which is how a `ctx.done("text")` answer reaches a
239
+ * string schema. When none does, the failure reports the issues raised by the
240
+ * LAST candidate, which is the narrowest text the extractor could isolate and
241
+ * therefore the one a correction prompt should be about.
242
+ *
243
+ * @category decoding
244
+ * @since 0.1.0
245
+ * @slop
246
+ */
247
+ export declare const decode: <S extends Schema.Top>(schema: S, text: string, attempt: {
248
+ readonly corrections: number;
249
+ readonly limit: number;
250
+ }, value?: Schema.Json | undefined) => Effect.Effect<S["Type"], StructuredOutputFailure, S["DecodingServices"]>;
251
+ export {};
252
+ //# sourceMappingURL=StructuredOutput.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StructuredOutput.d.ts","sourceRoot":"","sources":["../../src/StructuredOutput.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAIH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAIvC;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,2BAA2B,uGAKtC,CAAA;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,2BAA2B,GAAG,OAAO,2BAA2B,CAAC,IAAI,CAAA;AAEjF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,eAAe,mKAU1B,CAAA;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,GAAG,OAAO,eAAe,CAAC,IAAI,CAAA;;IAUvD,iDAAiD;;IAEjD,oFAAoF;;IAEpF,qCAAqC;;;AAZvC;;;;;;GAMG;AACH,qBAAa,WAAY,SAAQ,gBAO/B;IACA,+DAA+D;IACtD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,MAAM,CAAC,EAAE,uBAAuB,EAAE,CAAC,CAAC,kBAAkB,CAAC,CA0EtE,CAAA"}
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var StructuredOutput_exports = {};
30
+ __export(StructuredOutput_exports, {
31
+ OutputIssue: () => OutputIssue,
32
+ OutputIssueCode: () => OutputIssueCode,
33
+ StructuredOutputFailure: () => StructuredOutputFailure,
34
+ StructuredOutputFailureCode: () => StructuredOutputFailureCode,
35
+ candidates: () => candidates,
36
+ correction: () => correction,
37
+ decode: () => decode,
38
+ digest: () => digest,
39
+ instructions: () => instructions,
40
+ issuesDigest: () => issuesDigest,
41
+ jsonSchema: () => jsonSchema,
42
+ lastBalanced: () => lastBalanced,
43
+ maxIssues: () => maxIssues
44
+ });
45
+ module.exports = __toCommonJS(StructuredOutput_exports);
46
+ var Digest = __toESM(require("@smthrs/core/Digest"), 1);
47
+ var Fault = __toESM(require("@smthrs/flow/Fault"), 1);
48
+ var Effect = __toESM(require("effect/Effect"), 1);
49
+ var Schema = __toESM(require("effect/Schema"), 1);
50
+ var SchemaIssue = __toESM(require("effect/SchemaIssue"), 1);
51
+ var elide = __toESM(require("./internal/elide.js"), 1);
52
+ const StructuredOutputFailureCode = Schema.Literals([
53
+ "invalid_json",
54
+ "schema_mismatch",
55
+ "no_candidate",
56
+ "correction_exhausted"
57
+ ]);
58
+ const OutputIssueCode = Schema.Literals([
59
+ "invalid_json",
60
+ "no_candidate",
61
+ "invalid_type",
62
+ "invalid_value",
63
+ "missing_key",
64
+ "unexpected_key",
65
+ "forbidden",
66
+ "one_of",
67
+ "constraint"
68
+ ]);
69
+ class OutputIssue extends Schema.Class("flows/harness/StructuredOutput/OutputIssue")({
70
+ /** The stable reason this issue was reported. */
71
+ code: OutputIssueCode,
72
+ /** The JSON path of the offending value, dot/bracket joined; empty for the root. */
73
+ path: Schema.String,
74
+ /** What the decoder wanted there. */
75
+ message: Schema.String
76
+ }) {
77
+ /** Renders the issue for existing prose-oriented consumers. */
78
+ toString() {
79
+ return this.path === "" ? this.message : `${this.path}: ${this.message}`;
80
+ }
81
+ }
82
+ class StructuredOutputFailure extends Schema.TaggedError()(
83
+ "/harness/StructuredOutputFailure",
84
+ {
85
+ /** The stable reason this boundary failed. */
86
+ code: StructuredOutputFailureCode,
87
+ /** The declared output schema's canonical digest. */
88
+ schema: Schema.String,
89
+ /** SHA-256 of the text the decoder was last offered. */
90
+ candidate: Schema.String,
91
+ /** Correction attempts already spent on this boundary. */
92
+ corrections: Schema.Number,
93
+ /** The correction budget the boundary was declared with. */
94
+ limit: Schema.Number,
95
+ /** At most {@link maxIssues} structured validation issues. */
96
+ issues: Schema.Array(OutputIssue),
97
+ message: Schema.String
98
+ }
99
+ ) {
100
+ }
101
+ Fault.register("/harness/StructuredOutputFailure", "dependency");
102
+ const maxIssues = 5;
103
+ const jsonSchema = (schema) => {
104
+ const document = Schema.toJsonSchemaDocument(schema, { onExcessProperty: "error" });
105
+ return Object.keys(document.definitions).length > 0 ? { ...document.schema, $defs: document.definitions } : document.schema;
106
+ };
107
+ const digest = (schema) => Digest.digest(Digest.canonical(jsonSchema(schema)));
108
+ const instructions = (schema) => `## Required output shape
109
+
110
+ Finish by calling \`ctx.done(output)\` with the value itself: a JavaScript
111
+ value that validates against this JSON Schema, such as
112
+ \`ctx.done({ ...fields })\`. Do not stringify it, and add no prose around it.
113
+
114
+ \`\`\`json
115
+ ${JSON.stringify(jsonSchema(schema), null, 2)}
116
+ \`\`\``;
117
+ const issuesDigest = (failure) => Digest.digest(
118
+ Digest.canonical(failure.issues.map((issue) => ({ path: issue.path, message: issue.message })))
119
+ );
120
+ const correction = (failure) => `## Your previous answer did not validate
121
+
122
+ The output you passed to \`ctx.done\` could not be decoded against the required
123
+ schema. Call \`ctx.done(output)\` again with the same information as a value
124
+ that validates.
125
+
126
+ Validation issues:
127
+ ${failure.issues.map((issue) => `- ${issue}`).join("\n")}`;
128
+ const stripBom = (text) => text.charCodeAt(0) === 65279 ? text.slice(1) : text;
129
+ const lastBalanced = (text) => {
130
+ const stack = [];
131
+ let start = -1;
132
+ let found = void 0;
133
+ let quoted2 = false;
134
+ let escaped = false;
135
+ for (let index = 0; index < text.length; index++) {
136
+ const char = text[index];
137
+ if (quoted2) {
138
+ if (escaped) escaped = false;
139
+ else if (char === "\\") escaped = true;
140
+ else if (char === '"') quoted2 = false;
141
+ continue;
142
+ }
143
+ if (char === '"') {
144
+ if (stack.length > 0) quoted2 = true;
145
+ continue;
146
+ }
147
+ if (char === "{" || char === "[") {
148
+ if (stack.length === 0) start = index;
149
+ stack.push(char);
150
+ continue;
151
+ }
152
+ if (char === "}" || char === "]") {
153
+ const opener = stack.pop();
154
+ if (opener === void 0) continue;
155
+ if (char === "}" !== (opener === "{")) {
156
+ stack.length = 0;
157
+ start = -1;
158
+ continue;
159
+ }
160
+ if (stack.length === 0 && start >= 0) found = text.slice(start, index + 1);
161
+ }
162
+ }
163
+ return found;
164
+ };
165
+ const candidates = (text) => {
166
+ const stripped = stripBom(text).trim();
167
+ const balanced = lastBalanced(stripped);
168
+ return balanced === void 0 || balanced === stripped ? [stripped] : [stripped, balanced];
169
+ };
170
+ const issueMessage = (message) => elide.head(message, 240, "reissue the answer to see the whole issue");
171
+ const issuePath = (path) => path.map(
172
+ (segment, index) => typeof segment === "number" ? `[${segment}]` : `${index === 0 ? "" : "."}${String(segment)}`
173
+ ).join("");
174
+ const issueCodeSeparator = "\0";
175
+ const leafCodes = {
176
+ InvalidType: "invalid_type",
177
+ InvalidValue: "invalid_value",
178
+ MissingKey: "missing_key",
179
+ UnexpectedKey: "unexpected_key",
180
+ Forbidden: "forbidden",
181
+ OneOf: "one_of"
182
+ };
183
+ const formatSchemaIssues = SchemaIssue.makeFormatterStandardSchemaV1({
184
+ leafHook: (issue) => `${issue._tag}${issueCodeSeparator}${SchemaIssue.defaultLeafHook(issue)}`
185
+ });
186
+ const schemaIssuesOf = (error) => formatSchemaIssues(error.issue).issues.slice(0, maxIssues).map((issue) => {
187
+ const separator = issue.message.indexOf(issueCodeSeparator);
188
+ const tagged = separator >= 0;
189
+ return new OutputIssue({
190
+ code: tagged ? leafCodes[issue.message.slice(0, separator)] : "constraint",
191
+ path: issuePath(issue.path),
192
+ message: issueMessage(tagged ? issue.message.slice(separator + issueCodeSeparator.length) : issue.message)
193
+ });
194
+ });
195
+ const textIssuesOf = (code, message) => message.split("\n").map((line) => line.trim()).filter((line) => line.length > 0).slice(0, maxIssues).map(
196
+ (line) => new OutputIssue({ code, path: "", message: issueMessage(line) })
197
+ );
198
+ const quoted = (text) => {
199
+ try {
200
+ return typeof JSON.parse(text) === "string";
201
+ } catch {
202
+ return false;
203
+ }
204
+ };
205
+ const decode = (schema, text, attempt, value) => Effect.gen(function* () {
206
+ const offered = candidates(text);
207
+ const decoder = Schema.decodeUnknownEffect(schema);
208
+ let code = "no_candidate";
209
+ let issues = [
210
+ new OutputIssue({ code: "no_candidate", path: "", message: "the answer held no JSON document" })
211
+ ];
212
+ if (value !== void 0) {
213
+ const result = yield* Effect.result(decoder(value));
214
+ if (result._tag === "Success") return result.success;
215
+ const rawIssues = schemaIssuesOf(result.failure);
216
+ if (typeof value !== "string" || !rawIssues.every((issue) => issue.code === "invalid_type")) {
217
+ return yield* new StructuredOutputFailure({
218
+ code: attempt.corrections >= attempt.limit ? "correction_exhausted" : "schema_mismatch",
219
+ schema: digest(schema),
220
+ candidate: Digest.digest(JSON.stringify(value)),
221
+ corrections: attempt.corrections,
222
+ limit: attempt.limit,
223
+ issues: rawIssues,
224
+ message: `The agent's answer did not validate against its declared output schema after ${attempt.corrections} of ${attempt.limit} corrections`
225
+ });
226
+ }
227
+ }
228
+ for (const candidate of offered) {
229
+ if (candidate.length === 0) continue;
230
+ let parsed;
231
+ try {
232
+ parsed = JSON.parse(candidate);
233
+ } catch (error) {
234
+ code = "invalid_json";
235
+ issues = textIssuesOf("invalid_json", error.message);
236
+ continue;
237
+ }
238
+ const result = yield* Effect.result(decoder(parsed));
239
+ if (result._tag === "Success") return result.success;
240
+ code = "schema_mismatch";
241
+ issues = schemaIssuesOf(result.failure);
242
+ }
243
+ const raw = stripBom(text);
244
+ if (!quoted(offered[0])) {
245
+ const result = yield* Effect.result(decoder(raw));
246
+ if (result._tag === "Success") return result.success;
247
+ const rawIssues = schemaIssuesOf(result.failure);
248
+ if (code === "invalid_json" && rawIssues[0]?.code !== "invalid_type") {
249
+ code = "schema_mismatch";
250
+ issues = rawIssues;
251
+ }
252
+ }
253
+ return yield* new StructuredOutputFailure({
254
+ // Once the budget is spent, exhaustion is the actionable classification;
255
+ // the issue records retain whether the last candidate was invalid JSON or
256
+ // failed schema decoding.
257
+ code: attempt.corrections >= attempt.limit ? "correction_exhausted" : code,
258
+ schema: digest(schema),
259
+ candidate: Digest.digest(offered[offered.length - 1]),
260
+ corrections: attempt.corrections,
261
+ limit: attempt.limit,
262
+ issues,
263
+ message: `The agent's answer did not validate against its declared output schema after ${attempt.corrections} of ${attempt.limit} corrections`
264
+ });
265
+ });
266
+ //# sourceMappingURL=StructuredOutput.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/StructuredOutput.ts"],
4
+ "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 \u2014 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"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuCA,aAAwB;AACxB,YAAuB;AACvB,aAAwB;AACxB,aAAwB;AACxB,kBAA6B;AAC7B,YAAuB;AAchB,MAAM,8BAA8B,OAAO,SAAS;AAAA,EACzD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AA2BM,MAAM,kBAAkB,OAAO,SAAS;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAkBM,MAAM,oBAAoB,OAAO,MAAmB,4CAA4C,EAAE;AAAA;AAAA,EAEvG,MAAM;AAAA;AAAA,EAEN,MAAM,OAAO;AAAA;AAAA,EAEb,SAAS,OAAO;AAClB,CAAC,EAAE;AAAA;AAAA,EAEQ,WAAmB;AA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