mayura 0.1.0-dev.0 → 1.0.0-rc.2

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 (578) hide show
  1. package/README.md +152 -35
  2. package/docs/README.md +82 -0
  3. package/docs/ai-agents.md +73 -0
  4. package/docs/cli/dev.md +111 -0
  5. package/docs/cli/init.md +192 -0
  6. package/docs/cli/operations.md +235 -0
  7. package/docs/cli/overview.md +129 -0
  8. package/docs/cli/run.md +182 -0
  9. package/docs/concepts/agent.md +197 -0
  10. package/docs/concepts/costs-and-budgets.md +177 -0
  11. package/docs/concepts/outcomes.md +154 -0
  12. package/docs/concepts/permissions.md +155 -0
  13. package/docs/concepts/runtime.md +172 -0
  14. package/docs/concepts/tools.md +202 -0
  15. package/docs/concepts/workflows.md +134 -0
  16. package/docs/guides/approvals-and-human-input.md +278 -0
  17. package/docs/guides/artifacts.md +123 -0
  18. package/docs/guides/child-agents.md +158 -0
  19. package/docs/guides/code-mode.md +233 -0
  20. package/docs/guides/deployment.md +644 -0
  21. package/docs/guides/durable-workflows.md +463 -0
  22. package/docs/guides/guardrails.md +205 -0
  23. package/docs/guides/helpers.md +176 -0
  24. package/docs/guides/lifecycle-hooks.md +187 -0
  25. package/docs/guides/mcp.md +123 -0
  26. package/docs/guides/memory-and-context.md +293 -0
  27. package/docs/guides/model-providers.md +371 -0
  28. package/docs/guides/model-routing.md +138 -0
  29. package/docs/guides/observability.md +238 -0
  30. package/docs/guides/operator-console.md +140 -0
  31. package/docs/guides/react.md +276 -0
  32. package/docs/guides/sagas-and-loops.md +212 -0
  33. package/docs/guides/server-and-client.md +559 -0
  34. package/docs/guides/skills.md +152 -0
  35. package/docs/guides/storage.md +251 -0
  36. package/docs/guides/streaming.md +146 -0
  37. package/docs/guides/terminal.md +166 -0
  38. package/docs/guides/testing.md +220 -0
  39. package/docs/guides/vision.md +155 -0
  40. package/docs/guides/webhooks.md +172 -0
  41. package/docs/guides/workflow-composition.md +141 -0
  42. package/docs/guides/workflow-operations.md +269 -0
  43. package/docs/installation.md +90 -0
  44. package/docs/introduction.md +74 -0
  45. package/docs/project/credits.md +78 -0
  46. package/docs/project/security.md +178 -0
  47. package/docs/project/support.md +91 -0
  48. package/docs/project/versioning.md +122 -0
  49. package/docs/quickstart.md +190 -0
  50. package/docs/reference/entry-points.md +146 -0
  51. package/lib/adapter-code-docker/dist/index.d.ts +8 -1
  52. package/lib/adapter-code-docker/dist/index.d.ts.map +1 -1
  53. package/lib/adapter-code-docker/dist/index.js +64 -67
  54. package/lib/adapter-code-docker/dist/index.js.map +1 -1
  55. package/lib/adapter-code-docker/dist/options.d.ts +15 -0
  56. package/lib/adapter-code-docker/dist/options.d.ts.map +1 -0
  57. package/lib/adapter-code-docker/dist/options.js +56 -0
  58. package/lib/adapter-code-docker/dist/options.js.map +1 -0
  59. package/lib/adapter-code-docker/image/Dockerfile +4 -2
  60. package/lib/adapter-code-docker/package.json +1 -1
  61. package/lib/adapter-code-docker/src/index.ts +63 -65
  62. package/lib/adapter-code-docker/src/options.ts +51 -0
  63. package/lib/adapter-code-quickjs/dist/index.d.ts +16 -4
  64. package/lib/adapter-code-quickjs/dist/index.d.ts.map +1 -1
  65. package/lib/adapter-code-quickjs/dist/index.js +94 -50
  66. package/lib/adapter-code-quickjs/dist/index.js.map +1 -1
  67. package/lib/adapter-code-quickjs/dist/worker-command.d.ts +27 -0
  68. package/lib/adapter-code-quickjs/dist/worker-command.d.ts.map +1 -0
  69. package/lib/adapter-code-quickjs/dist/worker-command.js +66 -0
  70. package/lib/adapter-code-quickjs/dist/worker-command.js.map +1 -0
  71. package/lib/adapter-code-quickjs/dist/worker.js +421 -104
  72. package/lib/adapter-code-quickjs/dist/worker.js.map +1 -1
  73. package/lib/adapter-code-quickjs/package.json +1 -1
  74. package/lib/adapter-code-quickjs/src/index.ts +88 -44
  75. package/lib/adapter-code-quickjs/src/worker-command.ts +68 -0
  76. package/lib/adapter-code-quickjs/src/worker.ts +315 -89
  77. package/lib/adapter-mcp/package.json +1 -1
  78. package/lib/artifacts/dist/contracts.d.ts +6 -2
  79. package/lib/artifacts/dist/contracts.d.ts.map +1 -1
  80. package/lib/artifacts/dist/contracts.js.map +1 -1
  81. package/lib/artifacts/dist/index.d.ts +1 -0
  82. package/lib/artifacts/dist/index.d.ts.map +1 -1
  83. package/lib/artifacts/dist/index.js +1 -0
  84. package/lib/artifacts/dist/index.js.map +1 -1
  85. package/lib/artifacts/dist/local-store.d.ts.map +1 -1
  86. package/lib/artifacts/dist/local-store.js +4 -7
  87. package/lib/artifacts/dist/local-store.js.map +1 -1
  88. package/lib/artifacts/dist/media.d.ts +11 -0
  89. package/lib/artifacts/dist/media.d.ts.map +1 -0
  90. package/lib/artifacts/dist/media.js +18 -0
  91. package/lib/artifacts/dist/media.js.map +1 -0
  92. package/lib/artifacts/package.json +1 -1
  93. package/lib/artifacts/src/contracts.ts +6 -2
  94. package/lib/artifacts/src/index.ts +1 -0
  95. package/lib/artifacts/src/local-store.ts +5 -7
  96. package/lib/artifacts/src/media.ts +20 -0
  97. package/lib/cli/dist/agent-guide.d.ts +10 -0
  98. package/lib/cli/dist/agent-guide.d.ts.map +1 -0
  99. package/lib/cli/dist/agent-guide.js +48 -0
  100. package/lib/cli/dist/agent-guide.js.map +1 -0
  101. package/lib/cli/dist/application.d.ts +24 -0
  102. package/lib/cli/dist/application.d.ts.map +1 -1
  103. package/lib/cli/dist/application.js +21 -0
  104. package/lib/cli/dist/application.js.map +1 -1
  105. package/lib/cli/dist/bin.js +26 -6
  106. package/lib/cli/dist/bin.js.map +1 -1
  107. package/lib/cli/dist/dev.d.ts.map +1 -1
  108. package/lib/cli/dist/dev.js +7 -1
  109. package/lib/cli/dist/dev.js.map +1 -1
  110. package/lib/cli/dist/index.d.ts +3 -3
  111. package/lib/cli/dist/index.d.ts.map +1 -1
  112. package/lib/cli/dist/index.js +19 -9
  113. package/lib/cli/dist/index.js.map +1 -1
  114. package/lib/cli/dist/interactive.d.ts.map +1 -1
  115. package/lib/cli/dist/interactive.js +52 -11
  116. package/lib/cli/dist/interactive.js.map +1 -1
  117. package/lib/cli/dist/operations.d.ts +25 -2
  118. package/lib/cli/dist/operations.d.ts.map +1 -1
  119. package/lib/cli/dist/operations.js +125 -12
  120. package/lib/cli/dist/operations.js.map +1 -1
  121. package/lib/cli/dist/output.d.ts.map +1 -1
  122. package/lib/cli/dist/output.js +3 -0
  123. package/lib/cli/dist/output.js.map +1 -1
  124. package/lib/cli/dist/providers.d.ts +34 -4
  125. package/lib/cli/dist/providers.d.ts.map +1 -1
  126. package/lib/cli/dist/providers.js +26 -5
  127. package/lib/cli/dist/providers.js.map +1 -1
  128. package/lib/cli/package.json +1 -1
  129. package/lib/cli/src/agent-guide.ts +47 -0
  130. package/lib/cli/src/application.ts +24 -0
  131. package/lib/cli/src/bin.ts +22 -5
  132. package/lib/cli/src/dev.ts +5 -1
  133. package/lib/cli/src/index.ts +18 -10
  134. package/lib/cli/src/interactive.ts +44 -12
  135. package/lib/cli/src/operations.ts +82 -12
  136. package/lib/cli/src/output.ts +3 -0
  137. package/lib/cli/src/providers.ts +45 -10
  138. package/lib/cli/starters/approval-workflow/dot-env.example +11 -0
  139. package/lib/cli/starters/approval-workflow/package.json +1 -2
  140. package/lib/cli/starters/approval-workflow/src/config.ts +25 -7
  141. package/lib/cli/starters/approval-workflow/src/intake.ts +1 -2
  142. package/lib/cli/starters/approval-workflow/src/model.ts +11 -4
  143. package/lib/cli/starters/approval-workflow/src/workflow.ts +1 -2
  144. package/lib/cli/starters/cli-agent/README.md +121 -0
  145. package/lib/cli/starters/cli-agent/dot-env.example +37 -0
  146. package/lib/cli/starters/cli-agent/dot-github/workflows/ci.yml +13 -0
  147. package/lib/cli/starters/cli-agent/dot-gitignore +10 -0
  148. package/lib/cli/starters/cli-agent/mayura.project.json +15 -0
  149. package/lib/cli/starters/cli-agent/package.json +28 -0
  150. package/lib/cli/starters/cli-agent/skills/release-notes/SKILL.md +18 -0
  151. package/lib/cli/starters/cli-agent/skills/release-notes/references/example.md +12 -0
  152. package/lib/cli/starters/cli-agent/src/assistant.ts +124 -0
  153. package/lib/cli/starters/cli-agent/src/cli.ts +72 -0
  154. package/lib/cli/starters/cli-agent/src/config.ts +117 -0
  155. package/lib/cli/starters/cli-agent/src/model.ts +34 -0
  156. package/lib/cli/starters/cli-agent/src/workspace.ts +170 -0
  157. package/lib/cli/starters/cli-agent/test/assistant.test.ts +205 -0
  158. package/lib/cli/starters/cli-agent/tsconfig.json +20 -0
  159. package/lib/cli/starters/event-automation/README.md +4 -3
  160. package/lib/cli/starters/event-automation/dot-env.example +11 -0
  161. package/lib/cli/starters/event-automation/package.json +1 -2
  162. package/lib/cli/starters/event-automation/src/config.ts +25 -7
  163. package/lib/cli/starters/event-automation/src/ingress.ts +3 -5
  164. package/lib/cli/starters/event-automation/src/model.ts +11 -4
  165. package/lib/cli/starters/event-automation/src/send-sample.ts +1 -1
  166. package/lib/cli/starters/event-automation/src/tracker/main.ts +1 -1
  167. package/lib/cli/starters/event-automation/src/tracker/mcp-server.ts +1 -1
  168. package/lib/cli/starters/event-automation/src/tracker/tickets.ts +1 -1
  169. package/lib/cli/starters/event-automation/src/tracker-tools.ts +1 -1
  170. package/lib/cli/starters/event-automation/src/triage.ts +15 -22
  171. package/lib/cli/starters/event-automation/src/workflows.ts +1 -1
  172. package/lib/cli/starters/event-automation/test/automation.test.ts +3 -3
  173. package/lib/cli/starters/research-team/README.md +2 -2
  174. package/lib/cli/starters/research-team/dot-env.example +11 -0
  175. package/lib/cli/starters/research-team/package.json +1 -2
  176. package/lib/cli/starters/research-team/src/config.ts +25 -7
  177. package/lib/cli/starters/research-team/src/desk.ts +1 -2
  178. package/lib/cli/starters/research-team/src/library/index.ts +1 -2
  179. package/lib/cli/starters/research-team/src/model.ts +11 -4
  180. package/lib/cli/starters/research-team/src/services.ts +1 -1
  181. package/lib/cli/starters/research-team/src/team.ts +1 -2
  182. package/lib/cli/starters/research-team/src/workflow.ts +45 -59
  183. package/lib/cli/starters/support-agent/dot-env.example +11 -0
  184. package/lib/cli/starters/support-agent/package.json +1 -2
  185. package/lib/cli/starters/support-agent/src/assistant.ts +5 -5
  186. package/lib/cli/starters/support-agent/src/config.ts +25 -7
  187. package/lib/cli/starters/support-agent/src/guardrails.ts +3 -1
  188. package/lib/cli/starters/support-agent/src/model.ts +11 -4
  189. package/lib/cli/starters/support-agent/src/returns.ts +1 -2
  190. package/lib/cli/starters/support-agent/test/support.test.ts +25 -1
  191. package/lib/cli/templates/basic-agent.ts +1 -2
  192. package/lib/cli/templates/capability-policy.ts +1 -2
  193. package/lib/cli/templates/code-mode-workflow.ts +1 -1
  194. package/lib/cli/templates/durable-approval.ts +1 -1
  195. package/lib/cli/templates/parallel-research.ts +1 -2
  196. package/lib/cli/templates/typed-tool-runner.ts +1 -2
  197. package/lib/client/dist/forms.d.ts.map +1 -1
  198. package/lib/client/dist/forms.js +3 -4
  199. package/lib/client/dist/forms.js.map +1 -1
  200. package/lib/client/dist/headless.d.ts +3 -1
  201. package/lib/client/dist/headless.d.ts.map +1 -1
  202. package/lib/client/dist/headless.js +10 -6
  203. package/lib/client/dist/headless.js.map +1 -1
  204. package/lib/client/dist/index.d.ts +95 -6
  205. package/lib/client/dist/index.d.ts.map +1 -1
  206. package/lib/client/dist/index.js +397 -124
  207. package/lib/client/dist/index.js.map +1 -1
  208. package/lib/client/dist/workflows.d.ts +1 -1
  209. package/lib/client/dist/workflows.d.ts.map +1 -1
  210. package/lib/client/dist/workflows.js +7 -6
  211. package/lib/client/dist/workflows.js.map +1 -1
  212. package/lib/client/package.json +1 -1
  213. package/lib/client/src/forms.ts +3 -4
  214. package/lib/client/src/headless.ts +13 -7
  215. package/lib/client/src/index.ts +344 -74
  216. package/lib/client/src/workflows.ts +8 -7
  217. package/lib/client-react/package.json +1 -1
  218. package/lib/code-mode/dist/index.d.ts +32 -1
  219. package/lib/code-mode/dist/index.d.ts.map +1 -1
  220. package/lib/code-mode/dist/index.js +289 -221
  221. package/lib/code-mode/dist/index.js.map +1 -1
  222. package/lib/code-mode/package.json +1 -1
  223. package/lib/code-mode/src/index.ts +288 -184
  224. package/lib/code-mode-workflows/package.json +1 -1
  225. package/lib/context/package.json +1 -1
  226. package/lib/core/dist/budget.d.ts.map +1 -1
  227. package/lib/core/dist/budget.js +17 -2
  228. package/lib/core/dist/budget.js.map +1 -1
  229. package/lib/core/dist/contracts.d.ts +57 -0
  230. package/lib/core/dist/contracts.d.ts.map +1 -1
  231. package/lib/core/dist/contracts.js +36 -0
  232. package/lib/core/dist/contracts.js.map +1 -1
  233. package/lib/core/dist/destination.d.ts +12 -0
  234. package/lib/core/dist/destination.d.ts.map +1 -0
  235. package/lib/core/dist/destination.js +37 -0
  236. package/lib/core/dist/destination.js.map +1 -0
  237. package/lib/core/dist/event-stream.d.ts +1 -1
  238. package/lib/core/dist/event-stream.d.ts.map +1 -1
  239. package/lib/core/dist/event-stream.js +7 -0
  240. package/lib/core/dist/event-stream.js.map +1 -1
  241. package/lib/core/dist/host.d.ts +3 -0
  242. package/lib/core/dist/host.d.ts.map +1 -1
  243. package/lib/core/dist/host.js +3 -0
  244. package/lib/core/dist/host.js.map +1 -1
  245. package/lib/core/dist/index.d.ts +2 -0
  246. package/lib/core/dist/index.d.ts.map +1 -1
  247. package/lib/core/dist/index.js +2 -0
  248. package/lib/core/dist/index.js.map +1 -1
  249. package/lib/core/dist/media.d.ts +98 -0
  250. package/lib/core/dist/media.d.ts.map +1 -0
  251. package/lib/core/dist/media.js +227 -0
  252. package/lib/core/dist/media.js.map +1 -0
  253. package/lib/core/dist/model-schema.d.ts +37 -0
  254. package/lib/core/dist/model-schema.d.ts.map +1 -0
  255. package/lib/core/dist/model-schema.js +154 -0
  256. package/lib/core/dist/model-schema.js.map +1 -0
  257. package/lib/core/package.json +1 -1
  258. package/lib/core/src/budget.ts +17 -2
  259. package/lib/core/src/contracts.ts +71 -3
  260. package/lib/core/src/destination.ts +33 -0
  261. package/lib/core/src/event-stream.ts +5 -1
  262. package/lib/core/src/host.ts +3 -0
  263. package/lib/core/src/index.ts +2 -0
  264. package/lib/core/src/media.ts +228 -0
  265. package/lib/core/src/model-schema.ts +129 -0
  266. package/lib/exporter-otlp/dist/otlp-http-json.js +1 -1
  267. package/lib/exporter-otlp/dist/otlp-http-json.js.map +1 -1
  268. package/lib/exporter-otlp/dist/signals.js +2 -2
  269. package/lib/exporter-otlp/dist/signals.js.map +1 -1
  270. package/lib/exporter-otlp/package.json +1 -1
  271. package/lib/exporter-otlp/src/otlp-http-json.ts +1 -1
  272. package/lib/exporter-otlp/src/signals.ts +2 -2
  273. package/lib/guardrails/package.json +1 -1
  274. package/lib/helpers/package.json +1 -1
  275. package/lib/memory/dist/index.js +1 -1
  276. package/lib/memory/dist/index.js.map +1 -1
  277. package/lib/memory/dist/native.d.ts.map +1 -1
  278. package/lib/memory/dist/native.js +4 -3
  279. package/lib/memory/dist/native.js.map +1 -1
  280. package/lib/memory/package.json +1 -1
  281. package/lib/memory/src/index.ts +1 -1
  282. package/lib/memory/src/native.ts +4 -3
  283. package/lib/memory-remote/package.json +1 -1
  284. package/lib/observability/package.json +1 -1
  285. package/lib/provider-anthropic/dist/index.d.ts +36 -3
  286. package/lib/provider-anthropic/dist/index.d.ts.map +1 -1
  287. package/lib/provider-anthropic/dist/index.js +128 -78
  288. package/lib/provider-anthropic/dist/index.js.map +1 -1
  289. package/lib/provider-anthropic/package.json +1 -1
  290. package/lib/provider-anthropic/src/index.ts +152 -76
  291. package/lib/provider-openai/dist/index.d.ts +71 -4
  292. package/lib/provider-openai/dist/index.d.ts.map +1 -1
  293. package/lib/provider-openai/dist/index.js +259 -96
  294. package/lib/provider-openai/dist/index.js.map +1 -1
  295. package/lib/provider-openai/package.json +1 -1
  296. package/lib/provider-openai/src/index.ts +282 -84
  297. package/lib/runtime/dist/agent.d.ts +18 -1
  298. package/lib/runtime/dist/agent.d.ts.map +1 -1
  299. package/lib/runtime/dist/agent.js +59 -3
  300. package/lib/runtime/dist/agent.js.map +1 -1
  301. package/lib/runtime/dist/composition.d.ts.map +1 -1
  302. package/lib/runtime/dist/composition.js +2 -1
  303. package/lib/runtime/dist/composition.js.map +1 -1
  304. package/lib/runtime/dist/hooks.d.ts +15 -3
  305. package/lib/runtime/dist/hooks.d.ts.map +1 -1
  306. package/lib/runtime/dist/hooks.js.map +1 -1
  307. package/lib/runtime/dist/index.d.ts +1 -1
  308. package/lib/runtime/dist/index.d.ts.map +1 -1
  309. package/lib/runtime/dist/index.js.map +1 -1
  310. package/lib/runtime/dist/response.d.ts +7 -1
  311. package/lib/runtime/dist/response.d.ts.map +1 -1
  312. package/lib/runtime/dist/response.js +23 -2
  313. package/lib/runtime/dist/response.js.map +1 -1
  314. package/lib/runtime/dist/router.d.ts +10 -3
  315. package/lib/runtime/dist/router.d.ts.map +1 -1
  316. package/lib/runtime/dist/router.js +45 -8
  317. package/lib/runtime/dist/router.js.map +1 -1
  318. package/lib/runtime/dist/runtime.d.ts +9 -1
  319. package/lib/runtime/dist/runtime.d.ts.map +1 -1
  320. package/lib/runtime/dist/runtime.js +50 -22
  321. package/lib/runtime/dist/runtime.js.map +1 -1
  322. package/lib/runtime/package.json +1 -1
  323. package/lib/runtime/src/agent.ts +69 -3
  324. package/lib/runtime/src/composition.ts +2 -1
  325. package/lib/runtime/src/hooks.ts +10 -4
  326. package/lib/runtime/src/index.ts +1 -1
  327. package/lib/runtime/src/response.ts +20 -2
  328. package/lib/runtime/src/router.ts +43 -10
  329. package/lib/runtime/src/runtime.ts +58 -27
  330. package/lib/sdk/dist/index.d.ts +9 -1
  331. package/lib/sdk/dist/index.d.ts.map +1 -1
  332. package/lib/sdk/dist/index.js +9 -1
  333. package/lib/sdk/dist/index.js.map +1 -1
  334. package/lib/sdk/package.json +1 -1
  335. package/lib/sdk/src/index.ts +9 -1
  336. package/lib/server/dist/index.d.ts +201 -3
  337. package/lib/server/dist/index.d.ts.map +1 -1
  338. package/lib/server/dist/index.js +701 -149
  339. package/lib/server/dist/index.js.map +1 -1
  340. package/lib/server/dist/inspector-bundle.d.ts +1 -1
  341. package/lib/server/dist/inspector-bundle.d.ts.map +1 -1
  342. package/lib/server/dist/inspector-bundle.js +4 -4
  343. package/lib/server/dist/inspector-bundle.js.map +1 -1
  344. package/lib/server/package.json +1 -1
  345. package/lib/server/src/index.ts +611 -135
  346. package/lib/server/src/inspector-bundle.ts +4 -4
  347. package/lib/server-node/dist/hardening.d.ts +1 -1
  348. package/lib/server-node/dist/hardening.d.ts.map +1 -1
  349. package/lib/server-node/dist/hardening.js +4 -2
  350. package/lib/server-node/dist/hardening.js.map +1 -1
  351. package/lib/server-node/dist/index.d.ts +2 -1
  352. package/lib/server-node/dist/index.d.ts.map +1 -1
  353. package/lib/server-node/dist/index.js.map +1 -1
  354. package/lib/server-node/dist/probe.js +2 -2
  355. package/lib/server-node/dist/probe.js.map +1 -1
  356. package/lib/server-node/dist/production.d.ts +8 -1
  357. package/lib/server-node/dist/production.d.ts.map +1 -1
  358. package/lib/server-node/dist/production.js +15 -2
  359. package/lib/server-node/dist/production.js.map +1 -1
  360. package/lib/server-node/package.json +1 -1
  361. package/lib/server-node/src/hardening.ts +5 -3
  362. package/lib/server-node/src/index.ts +2 -1
  363. package/lib/server-node/src/probe.ts +2 -2
  364. package/lib/server-node/src/production.ts +23 -3
  365. package/lib/skills/dist/index.d.ts +81 -0
  366. package/lib/skills/dist/index.d.ts.map +1 -0
  367. package/lib/skills/dist/index.js +311 -0
  368. package/lib/skills/dist/index.js.map +1 -0
  369. package/lib/skills/package.json +5 -0
  370. package/lib/skills/src/index.ts +260 -0
  371. package/lib/storage/dist/index.d.ts +1 -1
  372. package/lib/storage/dist/index.d.ts.map +1 -1
  373. package/lib/storage/dist/index.js.map +1 -1
  374. package/lib/storage/package.json +1 -1
  375. package/lib/storage/src/index.ts +1 -1
  376. package/lib/storage-contracts/dist/contracts.d.ts +12 -5
  377. package/lib/storage-contracts/dist/contracts.d.ts.map +1 -1
  378. package/lib/storage-contracts/dist/contracts.js +24 -6
  379. package/lib/storage-contracts/dist/contracts.js.map +1 -1
  380. package/lib/storage-contracts/dist/index.d.ts +2 -0
  381. package/lib/storage-contracts/dist/index.d.ts.map +1 -1
  382. package/lib/storage-contracts/dist/index.js +2 -0
  383. package/lib/storage-contracts/dist/index.js.map +1 -1
  384. package/lib/storage-contracts/dist/memory-contracts.js +1 -1
  385. package/lib/storage-contracts/dist/memory-contracts.js.map +1 -1
  386. package/lib/storage-contracts/dist/run-records.d.ts +100 -0
  387. package/lib/storage-contracts/dist/run-records.d.ts.map +1 -0
  388. package/lib/storage-contracts/dist/run-records.js +464 -0
  389. package/lib/storage-contracts/dist/run-records.js.map +1 -0
  390. package/lib/storage-contracts/dist/schema-digest.d.ts +17 -0
  391. package/lib/storage-contracts/dist/schema-digest.d.ts.map +1 -0
  392. package/lib/storage-contracts/dist/schema-digest.js +94 -0
  393. package/lib/storage-contracts/dist/schema-digest.js.map +1 -0
  394. package/lib/storage-contracts/dist/submission-journal.js +1 -1
  395. package/lib/storage-contracts/dist/submission-journal.js.map +1 -1
  396. package/lib/storage-contracts/dist/workflow-format5.d.ts +30 -2
  397. package/lib/storage-contracts/dist/workflow-format5.d.ts.map +1 -1
  398. package/lib/storage-contracts/dist/workflow-format5.js +57 -5
  399. package/lib/storage-contracts/dist/workflow-format5.js.map +1 -1
  400. package/lib/storage-contracts/package.json +1 -1
  401. package/lib/storage-contracts/src/contracts.ts +29 -5
  402. package/lib/storage-contracts/src/index.ts +2 -0
  403. package/lib/storage-contracts/src/memory-contracts.ts +1 -1
  404. package/lib/storage-contracts/src/run-records.ts +365 -0
  405. package/lib/storage-contracts/src/schema-digest.ts +71 -0
  406. package/lib/storage-contracts/src/submission-journal.ts +1 -1
  407. package/lib/storage-contracts/src/workflow-format5.ts +70 -6
  408. package/lib/storage-postgres/dist/index.d.ts +1 -1
  409. package/lib/storage-postgres/dist/index.d.ts.map +1 -1
  410. package/lib/storage-postgres/dist/index.js.map +1 -1
  411. package/lib/storage-postgres/dist/postgres.d.ts +15 -0
  412. package/lib/storage-postgres/dist/postgres.d.ts.map +1 -1
  413. package/lib/storage-postgres/dist/postgres.js +18 -7
  414. package/lib/storage-postgres/dist/postgres.js.map +1 -1
  415. package/lib/storage-postgres/package.json +1 -1
  416. package/lib/storage-postgres/src/index.ts +1 -1
  417. package/lib/storage-postgres/src/postgres.ts +34 -8
  418. package/lib/storage-sql/dist/aggregate-session.js +1 -1
  419. package/lib/storage-sql/dist/aggregate-session.js.map +1 -1
  420. package/lib/storage-sql/dist/scheduled-database.js +1 -1
  421. package/lib/storage-sql/dist/scheduled-database.js.map +1 -1
  422. package/lib/storage-sql/dist/workflow-tree-validation.js +1 -1
  423. package/lib/storage-sql/dist/workflow-tree-validation.js.map +1 -1
  424. package/lib/storage-sql/package.json +1 -1
  425. package/lib/storage-sql/src/aggregate-session.ts +1 -1
  426. package/lib/storage-sql/src/scheduled-database.ts +1 -1
  427. package/lib/storage-sql/src/workflow-tree-validation.ts +1 -1
  428. package/lib/storage-sqlite/dist/sqlite-database.js +4 -4
  429. package/lib/storage-sqlite/dist/sqlite-database.js.map +1 -1
  430. package/lib/storage-sqlite/dist/sqlite-worker.js +2 -2
  431. package/lib/storage-sqlite/dist/sqlite-worker.js.map +1 -1
  432. package/lib/storage-sqlite/dist/sqlite.js +1 -1
  433. package/lib/storage-sqlite/dist/sqlite.js.map +1 -1
  434. package/lib/storage-sqlite/package.json +1 -1
  435. package/lib/storage-sqlite/src/sqlite-database.ts +4 -4
  436. package/lib/storage-sqlite/src/sqlite-worker.ts +2 -2
  437. package/lib/storage-sqlite/src/sqlite.ts +1 -1
  438. package/lib/terminal/dist/index.d.ts +92 -0
  439. package/lib/terminal/dist/index.d.ts.map +1 -0
  440. package/lib/terminal/dist/index.js +397 -0
  441. package/lib/terminal/dist/index.js.map +1 -0
  442. package/lib/terminal/package.json +5 -0
  443. package/lib/terminal/src/index.ts +273 -0
  444. package/lib/testing/dist/index.d.ts +44 -1
  445. package/lib/testing/dist/index.d.ts.map +1 -1
  446. package/lib/testing/dist/index.js +63 -10
  447. package/lib/testing/dist/index.js.map +1 -1
  448. package/lib/testing/package.json +1 -1
  449. package/lib/testing/src/index.ts +83 -10
  450. package/lib/tools/dist/index.d.ts +32 -4
  451. package/lib/tools/dist/index.d.ts.map +1 -1
  452. package/lib/tools/dist/index.js +64 -11
  453. package/lib/tools/dist/index.js.map +1 -1
  454. package/lib/tools/package.json +1 -1
  455. package/lib/tools/src/index.ts +74 -13
  456. package/lib/workflows/dist/abandoned.d.ts +15 -0
  457. package/lib/workflows/dist/abandoned.d.ts.map +1 -0
  458. package/lib/workflows/dist/abandoned.js +33 -0
  459. package/lib/workflows/dist/abandoned.js.map +1 -0
  460. package/lib/workflows/dist/agent-step.d.ts +83 -0
  461. package/lib/workflows/dist/agent-step.d.ts.map +1 -0
  462. package/lib/workflows/dist/agent-step.js +161 -0
  463. package/lib/workflows/dist/agent-step.js.map +1 -0
  464. package/lib/workflows/dist/children-runtime.js +8 -8
  465. package/lib/workflows/dist/children-runtime.js.map +1 -1
  466. package/lib/workflows/dist/composite-fleet.js +2 -2
  467. package/lib/workflows/dist/composite-fleet.js.map +1 -1
  468. package/lib/workflows/dist/composite-host.d.ts.map +1 -1
  469. package/lib/workflows/dist/composite-host.js +26 -2
  470. package/lib/workflows/dist/composite-host.js.map +1 -1
  471. package/lib/workflows/dist/hook-relay.js +2 -2
  472. package/lib/workflows/dist/hook-relay.js.map +1 -1
  473. package/lib/workflows/dist/index.d.ts +1 -1
  474. package/lib/workflows/dist/index.d.ts.map +1 -1
  475. package/lib/workflows/dist/index.js.map +1 -1
  476. package/lib/workflows/dist/leadership.js +3 -3
  477. package/lib/workflows/dist/leadership.js.map +1 -1
  478. package/lib/workflows/dist/lifecycle-definition.d.ts +26 -4
  479. package/lib/workflows/dist/lifecycle-definition.d.ts.map +1 -1
  480. package/lib/workflows/dist/lifecycle-definition.js +31 -6
  481. package/lib/workflows/dist/lifecycle-definition.js.map +1 -1
  482. package/lib/workflows/dist/lifecycle-fleet.d.ts.map +1 -1
  483. package/lib/workflows/dist/lifecycle-fleet.js +9 -11
  484. package/lib/workflows/dist/lifecycle-fleet.js.map +1 -1
  485. package/lib/workflows/dist/lifecycle-host.d.ts.map +1 -1
  486. package/lib/workflows/dist/lifecycle-host.js +26 -2
  487. package/lib/workflows/dist/lifecycle-host.js.map +1 -1
  488. package/lib/workflows/dist/lifecycle-human-transport.d.ts +22 -2
  489. package/lib/workflows/dist/lifecycle-human-transport.d.ts.map +1 -1
  490. package/lib/workflows/dist/lifecycle-human-transport.js +115 -33
  491. package/lib/workflows/dist/lifecycle-human-transport.js.map +1 -1
  492. package/lib/workflows/dist/lifecycle-runtime.d.ts +62 -1
  493. package/lib/workflows/dist/lifecycle-runtime.d.ts.map +1 -1
  494. package/lib/workflows/dist/lifecycle-runtime.js +268 -64
  495. package/lib/workflows/dist/lifecycle-runtime.js.map +1 -1
  496. package/lib/workflows/dist/lifecycle.d.ts +6 -4
  497. package/lib/workflows/dist/lifecycle.d.ts.map +1 -1
  498. package/lib/workflows/dist/lifecycle.js +2 -0
  499. package/lib/workflows/dist/lifecycle.js.map +1 -1
  500. package/lib/workflows/dist/loop-runtime.d.ts.map +1 -1
  501. package/lib/workflows/dist/loop-runtime.js +38 -26
  502. package/lib/workflows/dist/loop-runtime.js.map +1 -1
  503. package/lib/workflows/dist/migration.d.ts +1 -1
  504. package/lib/workflows/dist/migration.d.ts.map +1 -1
  505. package/lib/workflows/dist/migration.js +3 -3
  506. package/lib/workflows/dist/migration.js.map +1 -1
  507. package/lib/workflows/dist/operator-transports.d.ts +24 -2
  508. package/lib/workflows/dist/operator-transports.d.ts.map +1 -1
  509. package/lib/workflows/dist/operator-transports.js +49 -3
  510. package/lib/workflows/dist/operator-transports.js.map +1 -1
  511. package/lib/workflows/dist/runtime.d.ts.map +1 -1
  512. package/lib/workflows/dist/runtime.js +34 -22
  513. package/lib/workflows/dist/runtime.js.map +1 -1
  514. package/lib/workflows/dist/saga-runtime.d.ts.map +1 -1
  515. package/lib/workflows/dist/saga-runtime.js +44 -30
  516. package/lib/workflows/dist/saga-runtime.js.map +1 -1
  517. package/lib/workflows/dist/scheduled-helpers.d.ts.map +1 -1
  518. package/lib/workflows/dist/scheduled-helpers.js +3 -3
  519. package/lib/workflows/dist/scheduled-helpers.js.map +1 -1
  520. package/lib/workflows/dist/storage-failure.d.ts +16 -0
  521. package/lib/workflows/dist/storage-failure.d.ts.map +1 -0
  522. package/lib/workflows/dist/storage-failure.js +38 -0
  523. package/lib/workflows/dist/storage-failure.js.map +1 -0
  524. package/lib/workflows/dist/sweep-position.d.ts +22 -0
  525. package/lib/workflows/dist/sweep-position.d.ts.map +1 -0
  526. package/lib/workflows/dist/sweep-position.js +45 -0
  527. package/lib/workflows/dist/sweep-position.js.map +1 -0
  528. package/lib/workflows/dist/traces.js +2 -2
  529. package/lib/workflows/dist/traces.js.map +1 -1
  530. package/lib/workflows/dist/worker.d.ts +31 -0
  531. package/lib/workflows/dist/worker.d.ts.map +1 -1
  532. package/lib/workflows/dist/worker.js +94 -0
  533. package/lib/workflows/dist/worker.js.map +1 -1
  534. package/lib/workflows/package.json +1 -1
  535. package/lib/workflows/src/abandoned.ts +32 -0
  536. package/lib/workflows/src/agent-step.ts +199 -0
  537. package/lib/workflows/src/children-runtime.ts +8 -8
  538. package/lib/workflows/src/composite-fleet.ts +2 -2
  539. package/lib/workflows/src/composite-host.ts +21 -3
  540. package/lib/workflows/src/hook-relay.ts +2 -2
  541. package/lib/workflows/src/index.ts +2 -1
  542. package/lib/workflows/src/leadership.ts +3 -3
  543. package/lib/workflows/src/lifecycle-definition.ts +50 -9
  544. package/lib/workflows/src/lifecycle-fleet.ts +7 -8
  545. package/lib/workflows/src/lifecycle-host.ts +21 -5
  546. package/lib/workflows/src/lifecycle-human-transport.ts +110 -26
  547. package/lib/workflows/src/lifecycle-runtime.ts +270 -60
  548. package/lib/workflows/src/lifecycle.ts +7 -0
  549. package/lib/workflows/src/loop-runtime.ts +34 -25
  550. package/lib/workflows/src/migration.ts +2 -2
  551. package/lib/workflows/src/operator-transports.ts +48 -4
  552. package/lib/workflows/src/runtime.ts +28 -22
  553. package/lib/workflows/src/saga-runtime.ts +38 -28
  554. package/lib/workflows/src/scheduled-helpers.ts +3 -3
  555. package/lib/workflows/src/storage-failure.ts +37 -0
  556. package/lib/workflows/src/sweep-position.ts +52 -0
  557. package/lib/workflows/src/traces.ts +2 -2
  558. package/lib/workflows/src/worker.ts +79 -1
  559. package/lib/workstream/dist/humans.d.ts +7 -3
  560. package/lib/workstream/dist/humans.d.ts.map +1 -1
  561. package/lib/workstream/dist/humans.js +13 -2
  562. package/lib/workstream/dist/humans.js.map +1 -1
  563. package/lib/workstream/dist/index.js +2 -2
  564. package/lib/workstream/dist/index.js.map +1 -1
  565. package/lib/workstream/dist/timers.js +2 -2
  566. package/lib/workstream/dist/timers.js.map +1 -1
  567. package/lib/workstream/dist/webhooks.d.ts +6 -2
  568. package/lib/workstream/dist/webhooks.d.ts.map +1 -1
  569. package/lib/workstream/dist/webhooks.js +17 -7
  570. package/lib/workstream/dist/webhooks.js.map +1 -1
  571. package/lib/workstream/package.json +1 -1
  572. package/lib/workstream/src/humans.ts +15 -5
  573. package/lib/workstream/src/index.ts +2 -2
  574. package/lib/workstream/src/timers.ts +2 -2
  575. package/lib/workstream/src/webhooks.ts +21 -8
  576. package/llms-full.txt +9766 -0
  577. package/llms.txt +92 -0
  578. package/package.json +18 -6
@@ -9,31 +9,45 @@ const digest = /^[a-f0-9]{64}$/;
9
9
  const MAX_SOURCE_BYTES = 1_048_576;
10
10
  const MAX_TOOLS = 128;
11
11
  const MAX_IMPORTS = 64;
12
- const MAX_EXECUTIONS_PER_RUNTIME = 100_000;
12
+ /** Recently finished execution ids stay reserved, so a retry with a stale id fails instead of reusing call ids. */
13
+ const RECENT_EXECUTION_IDS = 100_000;
14
+ const PROGRAM_ERROR_NAME = 128;
15
+ const PROGRAM_ERROR_MESSAGE = 1_024;
16
+ /** Fixed per-code text for tool outcomes shown to the program; broker messages never reach the sandbox. */
13
17
  const messages = Object.freeze({
14
- INVALID_CONFIG: 'Code Mode configuration is invalid.',
15
- INVALID_INPUT: 'Code Mode input did not pass its admission boundary.',
16
- INVALID_OUTPUT: 'Code Mode output did not pass its disclosure boundary.',
17
- INVALID_JSON: 'Code Mode data must be bounded plain JSON.',
18
- PERMISSION_DENIED: 'The Code Mode operation was not authorized.',
19
- BUDGET_EXCEEDED: 'The execution budget could not admit this Code Mode operation.',
20
- LIMIT_EXCEEDED: 'A Code Mode execution limit was reached.',
21
- CANCELLED: 'Code Mode execution was cancelled.',
22
- TIMEOUT: 'Code Mode execution exceeded its deadline.',
23
- TOOL_FAILED: 'Code Mode execution failed; raw adapter details are withheld.',
24
- MODEL_FAILED: 'A model operation failed.',
25
- GUARD_BLOCKED: 'A required guard withheld this Code Mode operation.',
26
- GUARD_UNAVAILABLE: 'A required guard could not establish a verdict.',
27
- OUTCOME_UNKNOWN: 'An external operation may have occurred and requires reconciliation.',
28
- UNSUPPORTED_PROFILE: 'A qualified Code Mode sandbox is unavailable.',
29
- NOT_FOUND: 'The requested Code Mode resource was not found.',
30
- CONFLICT: 'The Code Mode execution identity is no longer available.',
31
- STORAGE_UNAVAILABLE: 'Required Code Mode storage is unavailable.',
32
- INTEGRITY_VIOLATION: 'Code Mode evidence failed integrity verification.',
18
+ INVALID_CONFIG: 'The tool call was not configured correctly.',
19
+ INVALID_INPUT: 'The tool input was rejected: it is not plain JSON within maxToolInputBytes, or it does not match the tool schema.',
20
+ INVALID_OUTPUT: 'The tool output was rejected.',
21
+ INVALID_JSON: 'The tool data must be bounded plain JSON.',
22
+ PERMISSION_DENIED: 'The tool call was not permitted.',
23
+ BUDGET_EXCEEDED: 'The tool call did not fit the remaining budget.',
24
+ LIMIT_EXCEEDED: 'The tool call exceeded a limit, such as the program\'s maxToolCalls or maxToolConcurrency.',
25
+ CANCELLED: 'The tool call was cancelled.',
26
+ TIMEOUT: 'The tool call ran out of time.',
27
+ TOOL_FAILED: 'The tool call failed.',
28
+ MODEL_FAILED: 'A model call failed.',
29
+ GUARD_BLOCKED: 'A guard blocked the tool call.',
30
+ GUARD_UNAVAILABLE: 'A required guard could not decide on the tool call.',
31
+ OUTCOME_UNKNOWN: 'The tool call may or may not have taken effect.',
32
+ UNSUPPORTED_PROFILE: 'The tool call is not supported here.',
33
+ NOT_FOUND: 'The tool call referred to something that does not exist.',
34
+ CONFLICT: 'The execution has finished; it accepts no more tool calls.',
35
+ STORAGE_UNAVAILABLE: 'Storage needed by the tool call is unavailable.',
36
+ INTEGRITY_VIOLATION: 'The tool call failed an integrity check.',
37
+ });
38
+ const failureReasons = new Set(['program_error', 'cpu_limit', 'memory_limit',
39
+ 'invalid_output', 'unsupported_program', 'sandbox_error']);
40
+ const reasonFailures = Object.freeze({
41
+ program_error: ['TOOL_FAILED', 'The program threw an error, did not compile, or returned a promise that can never settle. programError holds what it threw, when the sandbox could report it.'],
42
+ cpu_limit: ['LIMIT_EXCEEDED', 'The program ran longer than its cpuMillis limit.'],
43
+ memory_limit: ['LIMIT_EXCEEDED', 'The program needed more memory than its memoryBytes limit.'],
44
+ invalid_output: ['INVALID_OUTPUT', 'The program\'s result is not plain JSON within maxOutputBytes.'],
45
+ unsupported_program: ['UNSUPPORTED_PROFILE', 'This sandbox cannot run the program\'s language or approved imports.'],
46
+ sandbox_error: ['TOOL_FAILED', 'The sandbox failed before the program finished; its details are withheld.'],
33
47
  });
34
48
  function text(value, name, maxLength) {
35
49
  if (typeof value !== 'string' || value.trim().length === 0 || value.length > maxLength) {
36
- throw new MayuraError('INVALID_CONFIG', `${name} must be a bounded nonempty string.`);
50
+ throw new MayuraError('INVALID_CONFIG', `${name} must be a nonempty string of at most ${maxLength} characters.`);
37
51
  }
38
52
  }
39
53
  function plainRecord(value) {
@@ -43,15 +57,19 @@ function plainRecord(value) {
43
57
  }
44
58
  function exactData(value, required, optional = []) {
45
59
  if (!plainRecord(value))
46
- throw new MayuraError('INVALID_CONFIG', 'Code Mode configuration must be plain data.');
60
+ throw new MayuraError('INVALID_CONFIG', 'Code Mode options must be a plain object.');
47
61
  const fields = Object.getOwnPropertyDescriptors(value);
48
- if (Reflect.ownKeys(fields).some(key => typeof key !== 'string' || ![...required, ...optional].includes(key))
49
- || required.some(key => !fields[key]))
50
- throw new MayuraError('INVALID_CONFIG', 'Code Mode configuration fields are invalid.');
62
+ const unknown = Reflect.ownKeys(fields).find(key => typeof key !== 'string' || ![...required, ...optional].includes(key));
63
+ if (unknown !== undefined) {
64
+ throw new MayuraError('INVALID_CONFIG', typeof unknown === 'string' && unknown.length <= 64 ? `Unknown Code Mode option "${unknown}".` : 'Code Mode options contain an unknown key.');
65
+ }
66
+ const missing = required.find(key => !fields[key]);
67
+ if (missing !== undefined)
68
+ throw new MayuraError('INVALID_CONFIG', `The Code Mode option "${missing}" is required.`);
51
69
  const snapshot = Object.create(null);
52
70
  for (const [key, descriptor] of Object.entries(fields)) {
53
71
  if (!descriptor.enumerable || !('value' in descriptor))
54
- throw new MayuraError('INVALID_CONFIG', 'Code Mode configuration fields must be data properties.');
72
+ throw new MayuraError('INVALID_CONFIG', `The Code Mode option "${key}" must be a plain data property.`);
55
73
  if (descriptor.value !== undefined || required.includes(key))
56
74
  snapshot[key] = descriptor.value;
57
75
  }
@@ -69,10 +87,8 @@ function limits(value) {
69
87
  'maxOutputBytes', 'maxToolInputBytes', 'maxToolCalls', 'maxToolConcurrency']);
70
88
  for (const [name, item] of Object.entries(data))
71
89
  assertPositiveInteger(item, `limits.${name}`);
72
- if (data['wallTimeMillis'] > 2_147_483_647)
73
- throw new MayuraError('INVALID_CONFIG', 'wallTimeMillis exceeds the host timer range.');
74
90
  if (data['maxToolConcurrency'] > data['maxToolCalls']) {
75
- throw new MayuraError('INVALID_CONFIG', 'maxToolConcurrency cannot exceed maxToolCalls.');
91
+ throw new MayuraError('INVALID_CONFIG', 'limits.maxToolConcurrency cannot be greater than limits.maxToolCalls.');
76
92
  }
77
93
  const ceilings = Object.freeze({
78
94
  cpuMillis: 3_600_000,
@@ -87,7 +103,7 @@ function limits(value) {
87
103
  });
88
104
  for (const [name, ceiling] of Object.entries(ceilings)) {
89
105
  if (data[name] > ceiling)
90
- throw new MayuraError('INVALID_CONFIG', `limits.${name} exceeds the supported maximum.`);
106
+ throw new MayuraError('INVALID_CONFIG', `limits.${name} exceeds the supported maximum of ${ceiling}.`);
91
107
  }
92
108
  return Object.freeze({
93
109
  cpuMillis: data['cpuMillis'],
@@ -128,48 +144,48 @@ export function defineCodeProgram(options) {
128
144
  const data = exactData(options, ['id', 'version', 'intent', 'language', 'source', 'input', 'output', 'inputSchemaId',
129
145
  'outputSchemaId', 'limits'], ['tools', 'approvedImports']);
130
146
  if (!identifier.test(String(data['id'])))
131
- throw new MayuraError('INVALID_CONFIG', 'Program id must be a bounded identifier.');
147
+ throw new MayuraError('INVALID_CONFIG', 'Program id must start with a letter and contain at most 128 letters, digits, ".", "_", "/" or "-".');
132
148
  text(data['version'], 'version', 128);
133
149
  text(data['intent'], 'intent', 4_096);
134
150
  text(data['inputSchemaId'], 'inputSchemaId', 256);
135
151
  text(data['outputSchemaId'], 'outputSchemaId', 256);
136
152
  if (data['language'] !== 'javascript' && data['language'] !== 'typescript')
137
- throw new MayuraError('INVALID_CONFIG', 'Program language is unsupported.');
153
+ throw new MayuraError('INVALID_CONFIG', 'Program language must be "javascript" or "typescript".');
138
154
  if (typeof data['source'] !== 'string' || data['source'].trim().length === 0)
139
- throw new MayuraError('INVALID_CONFIG', 'Program source must be nonempty text.');
155
+ throw new MayuraError('INVALID_CONFIG', 'Program source must be a nonempty string.');
140
156
  const sourceBytes = Buffer.byteLength(data['source'], 'utf8');
141
157
  if (sourceBytes > MAX_SOURCE_BYTES)
142
- throw new MayuraError('LIMIT_EXCEEDED', 'Program source exceeds the definition limit.');
158
+ throw new MayuraError('LIMIT_EXCEEDED', 'Program source is larger than 1 MiB.');
143
159
  const input = snapshotSchema(data['input']);
144
160
  const output = snapshotSchema(data['output']);
145
161
  const codeLimits = limits(data['limits']);
146
162
  const suppliedTools = data['tools'] ?? [];
147
163
  if (!Array.isArray(suppliedTools) || suppliedTools.length > MAX_TOOLS)
148
- throw new MayuraError('INVALID_CONFIG', 'Program tools must be a bounded array.');
164
+ throw new MayuraError('INVALID_CONFIG', 'Program tools must be an array of at most 128 tools.');
149
165
  const toolMap = new Map();
150
166
  const toolManifest = suppliedTools.map(item => {
151
167
  assertTool(item);
152
168
  const tool = item;
153
169
  if (toolMap.has(tool.id))
154
- throw new MayuraError('CONFLICT', 'Program tool ids must be unique.');
170
+ throw new MayuraError('CONFLICT', `Program tools contain "${tool.id}" twice; tool ids must be unique.`);
155
171
  toolMap.set(tool.id, tool);
156
172
  return freezeTool(tool);
157
173
  });
158
174
  const maximumToolCost = toolManifest.reduce((maximum, tool) => Math.max(maximum, tool.costMicros), 0);
159
175
  if (!Number.isSafeInteger(maximumToolCost * codeLimits.maxToolCalls)) {
160
- throw new MayuraError('LIMIT_EXCEEDED', 'Program nested-tool cost bound exceeds safe accounting.');
176
+ throw new MayuraError('LIMIT_EXCEEDED', 'The program\'s maximum tool cost (the highest tool costMicros times limits.maxToolCalls) is larger than Number.MAX_SAFE_INTEGER.');
161
177
  }
162
178
  const suppliedImports = data['approvedImports'] ?? [];
163
179
  if (!Array.isArray(suppliedImports) || suppliedImports.length > MAX_IMPORTS)
164
- throw new MayuraError('INVALID_CONFIG', 'Approved imports must be a bounded array.');
180
+ throw new MayuraError('INVALID_CONFIG', 'approvedImports must be an array of at most 64 specifiers.');
165
181
  const approvedImports = suppliedImports.map(item => {
166
182
  text(item, 'approved import', 256);
167
183
  if (item.includes(':') || item.includes('\\') || item.startsWith('/'))
168
- throw new MayuraError('INVALID_CONFIG', 'Approved import specifier is invalid.');
184
+ throw new MayuraError('INVALID_CONFIG', 'approvedImports entries must be bare package specifiers, without ":", "\\" or a leading "/".');
169
185
  return item;
170
186
  });
171
187
  if (new Set(approvedImports).size !== approvedImports.length)
172
- throw new MayuraError('CONFLICT', 'Approved imports must be unique.');
188
+ throw new MayuraError('CONFLICT', 'approvedImports entries must be unique.');
173
189
  const unsigned = freezeJson(jsonValue({
174
190
  format: 1,
175
191
  id: data['id'],
@@ -192,18 +208,18 @@ export function defineCodeProgram(options) {
192
208
  /** Rejects forged or foreign-instance program metadata before durable composition. */
193
209
  export function assertCodeProgram(program) {
194
210
  if (!programs.has(program))
195
- throw new MayuraError('INVALID_CONFIG', 'Use defineCodeProgram from this package instance.');
211
+ throw new MayuraError('INVALID_CONFIG', 'The program was not created by defineCodeProgram from this package instance.');
196
212
  }
197
213
  /** Registers trusted adapter callbacks behind an immutable metadata-only handle. */
198
214
  export function defineSandboxAdapter(options) {
199
215
  const data = exactData(options, ['id', 'version', 'qualification', 'isAvailable', 'execute']);
200
216
  if (!identifier.test(String(data['id'])))
201
- throw new MayuraError('INVALID_CONFIG', 'Sandbox adapter id must be a bounded identifier.');
217
+ throw new MayuraError('INVALID_CONFIG', 'Sandbox adapter id must start with a letter and contain at most 128 letters, digits, ".", "_", "/" or "-".');
202
218
  text(data['version'], 'version', 128);
203
219
  if (data['qualification'] !== 'test' && data['qualification'] !== 'production')
204
- throw new MayuraError('INVALID_CONFIG', 'Sandbox qualification is invalid.');
220
+ throw new MayuraError('INVALID_CONFIG', 'Sandbox adapter qualification must be "production" or "test".');
205
221
  if (typeof data['isAvailable'] !== 'function' || typeof data['execute'] !== 'function')
206
- throw new MayuraError('INVALID_CONFIG', 'Sandbox adapter callbacks are required.');
222
+ throw new MayuraError('INVALID_CONFIG', 'Sandbox adapter isAvailable and execute must be functions.');
207
223
  const adapter = Object.freeze({ id: data['id'], version: data['version'], qualification: data['qualification'] });
208
224
  adapters.set(adapter, Object.freeze({
209
225
  isAvailable: data['isAvailable'],
@@ -211,20 +227,26 @@ export function defineSandboxAdapter(options) {
211
227
  }));
212
228
  return adapter;
213
229
  }
214
- function failure(code) {
215
- const status = code === 'CANCELLED' ? 'cancelled' : code === 'OUTCOME_UNKNOWN' ? 'outcome_unknown'
230
+ function statusOf(code) {
231
+ return code === 'CANCELLED' ? 'cancelled' : code === 'OUTCOME_UNKNOWN' ? 'outcome_unknown'
216
232
  : ['PERMISSION_DENIED', 'BUDGET_EXCEEDED', 'GUARD_BLOCKED', 'GUARD_UNAVAILABLE'].includes(code) ? 'blocked' : 'failed';
217
- return Object.freeze({ status, error: Object.freeze({ code, message: messages[code] }) });
233
+ }
234
+ /** Host-authored outcome errors: each message names the limit or rule involved and never carries program or tool data. */
235
+ function failure(code, message) {
236
+ return Object.freeze({ status: statusOf(code), error: Object.freeze({ code, message }) });
218
237
  }
219
238
  const zeroUsage = Object.freeze({ toolCalls: 0, unknownCalls: 0, knownCostMicros: 0, unknownCostMicros: 0, maximumCostMicros: 0 });
220
- function codeOutcome(outcome, usage = zeroUsage) {
221
- return Object.freeze({ ...outcome, usage: Object.freeze({ ...usage }) });
239
+ function codeOutcome(outcome, usage = zeroUsage, programError) {
240
+ return Object.freeze({ ...outcome, usage: Object.freeze({ ...usage }), ...(programError ? { programError } : {}) });
222
241
  }
223
242
  function bridgeFailure(code) {
224
- return Object.freeze({ status: code === 'CANCELLED' ? 'cancelled' : code === 'OUTCOME_UNKNOWN' ? 'outcome_unknown'
225
- : ['PERMISSION_DENIED', 'BUDGET_EXCEEDED', 'GUARD_BLOCKED', 'GUARD_UNAVAILABLE'].includes(code) ? 'blocked' : 'failed',
226
- error: Object.freeze({ code, message: messages[code] }) });
243
+ return Object.freeze({ status: statusOf(code), error: Object.freeze({ code, message: messages[code] }) });
227
244
  }
245
+ const abortMessages = Object.freeze({
246
+ CANCELLED: 'The execution was cancelled by its signal.',
247
+ TIMEOUT: 'The execution ran longer than the program\'s wallTimeMillis limit.',
248
+ });
249
+ const UNKNOWN_MESSAGE = 'A tool call may or may not have taken effect, so the program\'s result was discarded. Reconcile the effect (see evidence) before retrying.';
228
250
  function errorCode(value) {
229
251
  return typeof value === 'string' && Object.hasOwn(messages, value) ? value : 'TOOL_FAILED';
230
252
  }
@@ -268,33 +290,58 @@ function snapshotExecutionOptions(value) {
268
290
  text(data['runId'], 'runId', 256);
269
291
  text(data['executionId'], 'executionId', 256);
270
292
  if (!(data['signal'] instanceof AbortSignal))
271
- throw new MayuraError('INVALID_CONFIG', 'An AbortSignal is required.');
293
+ throw new MayuraError('INVALID_CONFIG', 'execute options need signal to be an AbortSignal.');
272
294
  const scope = freezeJson(jsonValue(data['scope'], { maxBytes: 2_048 }));
273
295
  if (!plainRecord(scope) || Object.keys(scope).length !== 2 || !Object.hasOwn(scope, 'principalId') || !Object.hasOwn(scope, 'projectId')) {
274
- throw new MayuraError('INVALID_CONFIG', 'A bounded execution scope is required.');
296
+ throw new MayuraError('INVALID_CONFIG', 'execute options need scope to be exactly { principalId, projectId }.');
275
297
  }
276
298
  text(scope['principalId'], 'scope.principalId', 256);
277
299
  text(scope['projectId'], 'scope.projectId', 256);
278
300
  return Object.freeze({ runId: data['runId'], executionId: data['executionId'], scope: scope, signal: data['signal'] });
279
301
  }
302
+ const sandboxError = Object.freeze({ status: 'failed', reason: 'sandbox_error' });
303
+ function snapshotProgramError(value) {
304
+ if (!plainRecord(value))
305
+ return undefined;
306
+ const fields = Object.getOwnPropertyDescriptors(value);
307
+ const name = fields['name'];
308
+ const message = fields['message'];
309
+ if (Reflect.ownKeys(fields).length !== 2 || !name || !message || !('value' in name) || !('value' in message)
310
+ || typeof name.value !== 'string' || typeof message.value !== 'string')
311
+ return undefined;
312
+ return Object.freeze({ name: name.value.slice(0, PROGRAM_ERROR_NAME), message: message.value.slice(0, PROGRAM_ERROR_MESSAGE) });
313
+ }
314
+ /** Admits only exact plain result data; accessors, extra fields and unknown reasons are sandbox failures. */
280
315
  function snapshotSandboxResult(value, maxOutputBytes) {
281
316
  try {
282
317
  if (!plainRecord(value))
283
- return undefined;
318
+ return sandboxError;
284
319
  const fields = Object.getOwnPropertyDescriptors(value);
285
- if (!fields['status'] || !fields['status'].enumerable || !('value' in fields['status']))
286
- return undefined;
287
- if (fields['status'].value === 'failed' && Reflect.ownKeys(fields).length === 1)
288
- return Object.freeze({ status: 'failed' });
289
- if (fields['status'].value !== 'succeeded' || Reflect.ownKeys(fields).length !== 2)
290
- return undefined;
291
- const output = fields['output'];
292
- if (!output || !output.enumerable || !('value' in output))
293
- return undefined;
294
- return Object.freeze({ status: 'succeeded', output: freezeJson(jsonValue(output.value, { maxBytes: maxOutputBytes })) });
320
+ if (Object.values(fields).some(field => !field.enumerable || !('value' in field)))
321
+ return sandboxError;
322
+ const keys = Reflect.ownKeys(fields);
323
+ if (fields['status']?.value === 'failed') {
324
+ if (keys.some(key => !['status', 'reason', 'programError'].includes(key)))
325
+ return sandboxError;
326
+ const reason = fields['reason']?.value;
327
+ if (reason === undefined)
328
+ return Object.freeze({ status: 'failed' });
329
+ if (typeof reason !== 'string' || !failureReasons.has(reason))
330
+ return sandboxError;
331
+ const programError = reason === 'program_error' ? snapshotProgramError(fields['programError']?.value) : undefined;
332
+ return Object.freeze({ status: 'failed', reason: reason, ...(programError ? { programError } : {}) });
333
+ }
334
+ if (fields['status']?.value !== 'succeeded' || keys.length !== 2 || !fields['output'])
335
+ return sandboxError;
336
+ try {
337
+ return Object.freeze({ status: 'succeeded', output: freezeJson(jsonValue(fields['output'].value, { maxBytes: maxOutputBytes })) });
338
+ }
339
+ catch {
340
+ return Object.freeze({ status: 'failed', reason: 'invalid_output' });
341
+ }
295
342
  }
296
343
  catch {
297
- return undefined;
344
+ return sandboxError;
298
345
  }
299
346
  }
300
347
  /** Creates an ephemeral Code Mode executor. It never evaluates source outside the supplied adapter. */
@@ -303,193 +350,214 @@ export function createCodeMode(options) {
303
350
  const adapter = data['adapter'];
304
351
  const registration = adapters.get(adapter);
305
352
  if (!registration)
306
- throw new MayuraError('INVALID_CONFIG', 'Sandbox adapter was not created by this package instance.');
353
+ throw new MayuraError('INVALID_CONFIG', 'The sandbox adapter was not created by defineSandboxAdapter from this package instance.');
307
354
  if (typeof data['invokeTool'] !== 'function')
308
- throw new MayuraError('INVALID_CONFIG', 'A trusted tool broker callback is required.');
355
+ throw new MayuraError('INVALID_CONFIG', 'invokeTool must be a function that brokers each tool call.');
309
356
  if (data['allowTestAdapter'] !== undefined && typeof data['allowTestAdapter'] !== 'boolean')
310
- throw new MayuraError('INVALID_CONFIG', 'allowTestAdapter must be boolean.');
311
- if (adapter.qualification !== 'production' && data['allowTestAdapter'] !== true)
312
- throw new MayuraError('UNSUPPORTED_PROFILE', messages.UNSUPPORTED_PROFILE);
357
+ throw new MayuraError('INVALID_CONFIG', 'allowTestAdapter must be a boolean.');
358
+ if (adapter.qualification !== 'production' && data['allowTestAdapter'] !== true) {
359
+ throw new MayuraError('UNSUPPORTED_PROFILE', `Sandbox adapter "${adapter.id}" is qualified for tests only. Use a production adapter, or pass allowTestAdapter: true to accept its isolation.`);
360
+ }
313
361
  const invoke = data['invokeTool'];
314
- const executions = new Set();
362
+ const active = new Set();
363
+ const recent = new Set();
364
+ const release = (id) => {
365
+ active.delete(id);
366
+ recent.add(id);
367
+ // Set iteration is insertion order, so this evicts the oldest finished id.
368
+ if (recent.size > RECENT_EXECUTION_IDS)
369
+ recent.delete(recent.values().next().value);
370
+ };
315
371
  const mode = Object.freeze({
316
372
  async execute(program, rawInput, rawOptions) {
317
373
  const registered = programs.get(program);
318
374
  if (!registered)
319
- return codeOutcome(failure('INVALID_CONFIG'));
375
+ return codeOutcome(failure('INVALID_CONFIG', 'The program was not created by defineCodeProgram from this package instance.'));
320
376
  const maximumToolCost = program.manifest.tools.reduce((maximum, tool) => Math.max(maximum, tool.costMicros), 0);
321
377
  const maximumCostMicros = maximumToolCost * program.manifest.limits.maxToolCalls;
378
+ const early = (code, message) => codeOutcome(failure(code, message), { ...zeroUsage, maximumCostMicros });
322
379
  let execution;
323
380
  try {
324
381
  execution = snapshotExecutionOptions(rawOptions);
325
382
  }
326
- catch {
327
- return codeOutcome(failure('INVALID_CONFIG'), { ...zeroUsage, maximumCostMicros });
383
+ catch (error) {
384
+ return early('INVALID_CONFIG', error instanceof MayuraError ? error.message : 'The execute options are invalid.');
385
+ }
386
+ if (active.has(execution.executionId) || recent.has(execution.executionId)) {
387
+ return early('CONFLICT', 'This executionId is running or was used recently by this executor. Pass a new executionId for each execution.');
328
388
  }
329
- if (executions.size >= MAX_EXECUTIONS_PER_RUNTIME)
330
- return codeOutcome(failure('LIMIT_EXCEEDED'), { ...zeroUsage, maximumCostMicros });
331
- if (executions.has(execution.executionId))
332
- return codeOutcome(failure('CONFLICT'), { ...zeroUsage, maximumCostMicros });
333
- executions.add(execution.executionId);
334
- let inputSnapshot;
389
+ active.add(execution.executionId);
335
390
  try {
336
- inputSnapshot = freezeJson(jsonValue(rawInput, { maxBytes: program.manifest.limits.maxInputBytes }));
391
+ return await run(program, registered, rawInput, execution, maximumCostMicros);
337
392
  }
338
- catch {
339
- return codeOutcome(failure('INVALID_INPUT'), { ...zeroUsage, maximumCostMicros });
393
+ finally {
394
+ release(execution.executionId);
340
395
  }
341
- let available = false;
342
- try {
343
- available = await registration.isAvailable() === true;
396
+ },
397
+ });
398
+ async function run(program, registered, rawInput, execution, maximumCostMicros) {
399
+ const limits = program.manifest.limits;
400
+ const early = (code, message) => codeOutcome(failure(code, message), { ...zeroUsage, maximumCostMicros });
401
+ let inputSnapshot;
402
+ try {
403
+ inputSnapshot = freezeJson(jsonValue(rawInput, { maxBytes: limits.maxInputBytes }));
404
+ }
405
+ catch {
406
+ return early('INVALID_INPUT', 'The input is not plain JSON within the program\'s maxInputBytes limit.');
407
+ }
408
+ let available = false;
409
+ try {
410
+ available = await registration.isAvailable() === true;
411
+ }
412
+ catch { /* Availability errors reveal no adapter details. */ }
413
+ if (!available) {
414
+ return early('UNSUPPORTED_PROFILE', `Sandbox adapter "${adapter.id}" is not available on this host, for example because its runtime or image is missing. Code Mode never runs a program outside its sandbox.`);
415
+ }
416
+ const controller = new AbortController();
417
+ let abortCode = 'CANCELLED';
418
+ const relay = () => { if (!controller.signal.aborted) {
419
+ abortCode = 'CANCELLED';
420
+ controller.abort();
421
+ } };
422
+ execution.signal.addEventListener('abort', relay, { once: true });
423
+ if (execution.signal.aborted)
424
+ relay();
425
+ const timer = setTimeout(() => { if (!controller.signal.aborted) {
426
+ abortCode = 'TIMEOUT';
427
+ controller.abort();
428
+ } }, limits.wallTimeMillis);
429
+ const evidence = [];
430
+ const pending = new Set();
431
+ let startedCalls = 0;
432
+ let activeCalls = 0;
433
+ let accountedCalls = 0;
434
+ let unknownCalls = 0;
435
+ let knownCostMicros = 0;
436
+ let unknownCostMicros = 0;
437
+ const usage = () => Object.freeze({ toolCalls: accountedCalls, unknownCalls, knownCostMicros, unknownCostMicros, maximumCostMicros });
438
+ const withEvidence = (outcome) => Object.freeze({ ...outcome, ...(evidence.length ? { evidence: Object.freeze([...evidence]) } : {}) });
439
+ const account = (tool, receipt) => {
440
+ accountedCalls++;
441
+ if (!receipt || receipt.execution === 'unknown') {
442
+ unknownCalls++;
443
+ unknownCostMicros += tool.costMicros;
344
444
  }
345
- catch { /* Availability errors reveal no adapter details. */ }
346
- if (!available)
347
- return codeOutcome(failure('UNSUPPORTED_PROFILE'), { ...zeroUsage, maximumCostMicros });
348
- const controller = new AbortController();
349
- let abortCode = 'CANCELLED';
350
- const relay = () => { if (!controller.signal.aborted) {
351
- abortCode = 'CANCELLED';
352
- controller.abort();
353
- } };
354
- execution.signal.addEventListener('abort', relay, { once: true });
355
- if (execution.signal.aborted)
356
- relay();
357
- const timer = setTimeout(() => { if (!controller.signal.aborted) {
358
- abortCode = 'TIMEOUT';
359
- controller.abort();
360
- } }, program.manifest.limits.wallTimeMillis);
361
- const evidence = [];
362
- const pending = new Set();
363
- let startedCalls = 0;
364
- let activeCalls = 0;
365
- let accountedCalls = 0;
366
- let unknownCalls = 0;
367
- let knownCostMicros = 0;
368
- let unknownCostMicros = 0;
369
- const usage = () => Object.freeze({ toolCalls: accountedCalls, unknownCalls, knownCostMicros, unknownCostMicros, maximumCostMicros });
370
- const account = (tool, receipt) => {
371
- accountedCalls++;
372
- if (!receipt || receipt.execution === 'unknown') {
373
- unknownCalls++;
374
- unknownCostMicros += tool.costMicros;
375
- }
376
- else if (receipt.execution !== 'not_started')
377
- knownCostMicros += tool.costMicros;
378
- if (!Number.isSafeInteger(knownCostMicros) || !Number.isSafeInteger(unknownCostMicros)
379
- || knownCostMicros + unknownCostMicros > maximumCostMicros) {
380
- throw new MayuraError('LIMIT_EXCEEDED', 'Nested-tool usage exceeded the admitted accounting bound.');
381
- }
382
- };
383
- let bridgeOpen = true;
384
- let bridgeCloseCode = 'CONFLICT';
385
- const bridge = Object.freeze({
386
- call(toolId, untrustedInput) {
387
- const operation = (async () => {
388
- if (!bridgeOpen)
389
- return bridgeFailure(bridgeCloseCode);
445
+ else if (receipt.execution !== 'not_started')
446
+ knownCostMicros += tool.costMicros;
447
+ if (!Number.isSafeInteger(knownCostMicros) || !Number.isSafeInteger(unknownCostMicros)
448
+ || knownCostMicros + unknownCostMicros > maximumCostMicros) {
449
+ throw new MayuraError('LIMIT_EXCEEDED', 'Nested-tool usage exceeded the admitted accounting bound.');
450
+ }
451
+ };
452
+ let bridgeOpen = true;
453
+ let bridgeCloseCode = 'CONFLICT';
454
+ const bridge = Object.freeze({
455
+ call(toolId, untrustedInput) {
456
+ const operation = (async () => {
457
+ if (!bridgeOpen)
458
+ return bridgeFailure(bridgeCloseCode);
459
+ if (controller.signal.aborted)
460
+ return bridgeFailure(abortCode);
461
+ if (typeof toolId !== 'string' || !registered.tools.has(toolId))
462
+ return bridgeFailure('PERMISSION_DENIED');
463
+ if (startedCalls >= limits.maxToolCalls || activeCalls >= limits.maxToolConcurrency)
464
+ return bridgeFailure('LIMIT_EXCEEDED');
465
+ const sequence = ++startedCalls;
466
+ activeCalls++;
467
+ try {
468
+ let input;
469
+ try {
470
+ input = freezeJson(jsonValue(untrustedInput, { maxBytes: limits.maxToolInputBytes }));
471
+ }
472
+ catch {
473
+ return bridgeFailure('INVALID_INPUT');
474
+ }
390
475
  if (controller.signal.aborted)
391
476
  return bridgeFailure(abortCode);
392
- if (typeof toolId !== 'string' || !registered.tools.has(toolId))
393
- return bridgeFailure('PERMISSION_DENIED');
394
- if (startedCalls >= program.manifest.limits.maxToolCalls || activeCalls >= program.manifest.limits.maxToolConcurrency)
395
- return bridgeFailure('LIMIT_EXCEEDED');
396
- const sequence = ++startedCalls;
397
- activeCalls++;
477
+ const tool = registered.tools.get(toolId);
478
+ const callId = `${execution.executionId}:code:${sequence}`;
479
+ let outcome;
398
480
  try {
399
- let input;
400
- try {
401
- input = freezeJson(jsonValue(untrustedInput, { maxBytes: program.manifest.limits.maxToolInputBytes }));
402
- }
403
- catch {
404
- return bridgeFailure('INVALID_INPUT');
405
- }
406
- if (controller.signal.aborted)
407
- return bridgeFailure(abortCode);
408
- const tool = registered.tools.get(toolId);
409
- const callId = `${execution.executionId}:code:${sequence}`;
410
- let outcome;
411
- try {
412
- outcome = await invoke(tool, input, Object.freeze({ runId: execution.runId, executionId: execution.executionId,
413
- callId, programDigest: program.manifest.digest, scope: execution.scope, signal: controller.signal }));
414
- }
415
- catch {
416
- account(tool);
417
- return bridgeFailure('TOOL_FAILED');
418
- }
419
- const snapshot = snapshotToolOutcome(outcome, program.manifest.limits.maxOutputBytes, callId, toolId);
420
- account(tool, snapshot.receipt);
421
- if (snapshot.receipt)
422
- evidence.push(Object.freeze({ runId: execution.runId, receipt: snapshot.receipt }));
423
- return snapshot.exposed;
481
+ outcome = await invoke(tool, input, Object.freeze({ runId: execution.runId, executionId: execution.executionId,
482
+ callId, programDigest: program.manifest.digest, scope: execution.scope, signal: controller.signal }));
424
483
  }
425
- finally {
426
- activeCalls--;
484
+ catch {
485
+ account(tool);
486
+ return bridgeFailure('TOOL_FAILED');
427
487
  }
428
- })();
429
- pending.add(operation);
430
- void operation.finally(() => pending.delete(operation));
431
- return operation;
432
- },
433
- });
488
+ const snapshot = snapshotToolOutcome(outcome, limits.maxOutputBytes, callId, toolId);
489
+ account(tool, snapshot.receipt);
490
+ if (snapshot.receipt)
491
+ evidence.push(Object.freeze({ runId: execution.runId, receipt: snapshot.receipt }));
492
+ return snapshot.exposed;
493
+ }
494
+ finally {
495
+ activeCalls--;
496
+ }
497
+ })();
498
+ pending.add(operation);
499
+ void operation.finally(() => pending.delete(operation));
500
+ return operation;
501
+ },
502
+ });
503
+ try {
504
+ let input;
434
505
  try {
435
- let input;
436
- try {
437
- input = freezeJson(jsonValue(await validate(program.input, inputSnapshot, 'input', { maxBytes: program.manifest.limits.maxInputBytes }), { maxBytes: program.manifest.limits.maxInputBytes }));
438
- }
439
- catch {
440
- return codeOutcome(failure('INVALID_INPUT'), usage());
441
- }
442
- if (controller.signal.aborted)
443
- return codeOutcome(failure(abortCode), usage());
444
- const request = Object.freeze({ executionId: execution.executionId, manifest: program.manifest, source: registered.source,
445
- input, signal: controller.signal, tools: bridge });
446
- let sandboxResult;
447
- const adapterWork = Promise.resolve().then(() => registration.execute(request));
448
- void adapterWork.catch(() => undefined);
449
- await Promise.race([
450
- adapterWork.then(result => { sandboxResult = snapshotSandboxResult(result, program.manifest.limits.maxOutputBytes); }, () => undefined),
451
- new Promise(resolve => controller.signal.addEventListener('abort', () => resolve(), { once: true })),
452
- ]);
453
- bridgeCloseCode = controller.signal.aborted ? abortCode : 'CONFLICT';
454
- bridgeOpen = false;
455
- if (controller.signal.aborted) {
456
- await Promise.allSettled([...pending]);
457
- const code = unknownCalls > 0 ? 'OUTCOME_UNKNOWN' : abortCode;
458
- return codeOutcome(Object.freeze({ ...failure(code), ...(evidence.length ? { evidence: Object.freeze([...evidence]) } : {}) }), usage());
459
- }
460
- await Promise.allSettled([...pending]);
461
- if (unknownCalls > 0) {
462
- return codeOutcome(Object.freeze({ ...failure('OUTCOME_UNKNOWN'), ...(evidence.length ? { evidence: Object.freeze([...evidence]) } : {}) }), usage());
463
- }
464
- if (!sandboxResult || !plainRecord(sandboxResult) || sandboxResult.status !== 'succeeded') {
465
- return codeOutcome(Object.freeze({ ...failure('TOOL_FAILED'), ...(evidence.length ? { evidence: Object.freeze([...evidence]) } : {}) }), usage());
466
- }
467
- try {
468
- const output = freezeJson(jsonValue(await validate(program.output, sandboxResult.output, 'output', { maxBytes: program.manifest.limits.maxOutputBytes }), { maxBytes: program.manifest.limits.maxOutputBytes }));
469
- return codeOutcome(Object.freeze({ status: 'succeeded', output, ...(evidence.length ? { evidence: Object.freeze([...evidence]) } : {}) }), usage());
470
- }
471
- catch {
472
- return codeOutcome(Object.freeze({ ...failure('INVALID_OUTPUT'), ...(evidence.length ? { evidence: Object.freeze([...evidence]) } : {}) }), usage());
473
- }
506
+ input = freezeJson(jsonValue(await validate(program.input, inputSnapshot, 'input', { maxBytes: limits.maxInputBytes }), { maxBytes: limits.maxInputBytes }));
474
507
  }
475
- finally {
476
- if (controller.signal.aborted)
477
- bridgeCloseCode = abortCode;
478
- bridgeOpen = false;
479
- clearTimeout(timer);
480
- execution.signal.removeEventListener('abort', relay);
481
- if (!controller.signal.aborted)
482
- controller.abort();
508
+ catch {
509
+ return codeOutcome(failure('INVALID_INPUT', 'The input does not match the program\'s input schema.'), usage());
483
510
  }
484
- },
485
- });
511
+ if (controller.signal.aborted)
512
+ return codeOutcome(failure(abortCode, abortMessages[abortCode]), usage());
513
+ const request = Object.freeze({ executionId: execution.executionId, manifest: program.manifest, source: registered.source,
514
+ input, signal: controller.signal, tools: bridge });
515
+ let sandboxResult = sandboxError;
516
+ const adapterWork = Promise.resolve().then(() => registration.execute(request));
517
+ void adapterWork.catch(() => undefined);
518
+ await Promise.race([
519
+ adapterWork.then(result => { sandboxResult = snapshotSandboxResult(result, limits.maxOutputBytes); }, () => undefined),
520
+ new Promise(resolve => controller.signal.addEventListener('abort', () => resolve(), { once: true })),
521
+ ]);
522
+ bridgeCloseCode = controller.signal.aborted ? abortCode : 'CONFLICT';
523
+ bridgeOpen = false;
524
+ // Admitted broker calls always settle before the outcome, so usage and evidence are complete.
525
+ await Promise.allSettled([...pending]);
526
+ if (unknownCalls > 0)
527
+ return codeOutcome(withEvidence(failure('OUTCOME_UNKNOWN', UNKNOWN_MESSAGE)), usage());
528
+ if (controller.signal.aborted)
529
+ return codeOutcome(withEvidence(failure(abortCode, abortMessages[abortCode])), usage());
530
+ const admitted = sandboxResult;
531
+ if (admitted.status === 'failed') {
532
+ const [code, message] = admitted.reason ? reasonFailures[admitted.reason]
533
+ : ['TOOL_FAILED', 'The program or its sandbox failed; the adapter did not report why.'];
534
+ return codeOutcome(withEvidence(failure(code, message)), usage(), admitted.programError);
535
+ }
536
+ try {
537
+ const output = freezeJson(jsonValue(await validate(program.output, admitted.output, 'output', { maxBytes: limits.maxOutputBytes }), { maxBytes: limits.maxOutputBytes }));
538
+ return codeOutcome(withEvidence({ status: 'succeeded', output }), usage());
539
+ }
540
+ catch {
541
+ return codeOutcome(withEvidence(failure('INVALID_OUTPUT', 'The program\'s result does not match its output schema.')), usage());
542
+ }
543
+ }
544
+ finally {
545
+ if (controller.signal.aborted)
546
+ bridgeCloseCode = abortCode;
547
+ bridgeOpen = false;
548
+ clearTimeout(timer);
549
+ execution.signal.removeEventListener('abort', relay);
550
+ if (!controller.signal.aborted)
551
+ controller.abort();
552
+ }
553
+ }
486
554
  modes.add(mode);
487
555
  return mode;
488
556
  }
489
557
  /** Rejects forged executors before they are captured by a durable phase tool. */
490
558
  export function assertCodeMode(mode) {
491
559
  if (!modes.has(mode))
492
- throw new MayuraError('INVALID_CONFIG', 'Use createCodeMode from this package instance.');
560
+ throw new MayuraError('INVALID_CONFIG', 'The Code Mode executor was not created by createCodeMode from this package instance.');
493
561
  }
494
562
  /** Runtime assertion for callers that persist or compare program digests. */
495
563
  export function isCodeProgramDigest(value) {