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
@@ -0,0 +1,123 @@
1
+ ---
2
+ title: "Artifacts"
3
+ description: "Store files your agents produce on local disk with verified content, per-tenant isolation, safe downloads, audits and backups."
4
+ ---
5
+
6
+ Agents and workflows often produce files: a generated report, an exported CSV, an image. `mayura/artifacts` stores
7
+ such files on the local filesystem. Each file is addressed by its SHA-256 digest, kept apart per tenant, checked again
8
+ every time it is read, and handed out only as a download under a policy you choose. You get back a small JSON
9
+ reference, which you save next to your own records.
10
+
11
+ ```ts
12
+ import { createLocalArtifactStore } from 'mayura/artifacts';
13
+
14
+ const artifacts = createLocalArtifactStore({
15
+ rootDirectory: '/var/lib/my-app/artifacts',
16
+ maxArtifactBytes: 10 * 1024 * 1024,
17
+ });
18
+ const scope = { principalId: 'acme', projectId: 'reports' };
19
+
20
+ const staged = await artifacts.stage({
21
+ scope,
22
+ content: new TextEncoder().encode('order,total\nord-1001,42.00\n'),
23
+ mediaType: 'text/csv',
24
+ classification: 'internal',
25
+ filename: 'orders.csv',
26
+ });
27
+ const reference = await artifacts.commit(staged);
28
+ // Save `reference` (plain JSON) with your own record, for example the workflow run that produced it.
29
+
30
+ const bytes = await artifacts.read(reference, scope);
31
+ ```
32
+
33
+ ## Store options
34
+
35
+ | Option | Default | Notes |
36
+ | --- | --- | --- |
37
+ | `rootDirectory` | required | An absolute path. Mayura creates its staging and object folders inside it. |
38
+ | `maxArtifactBytes` | required | Largest file accepted, up to 64 MiB. Files are held in memory while stored. |
39
+ | `maxStagedArtifacts` | 128 | Staged files not yet committed or discarded, up to 4,096. |
40
+ | `maxCommittedArtifactsPerScope` | 4,096 | Committed files per scope, up to 65,536. |
41
+
42
+ ## Stage, commit, discard
43
+
44
+ Storing is two steps. `stage` writes the bytes to a private staging area and computes their digest. `commit` checks the
45
+ staged file again and moves it into place atomically, returning the reference. If you decide not to keep a staged file,
46
+ call `discard(staged)`.
47
+
48
+ A stage takes:
49
+
50
+ | Field | Notes |
51
+ | --- | --- |
52
+ | `scope` | `{ principalId, projectId? }`. Files are partitioned by scope, even when two tenants store identical bytes. |
53
+ | `content` | A `Uint8Array`. |
54
+ | `mediaType` | A registered media type without parameters, such as `text/csv` or `application/pdf`. |
55
+ | `classification` | `public`, `internal`, `confidential` or `restricted`. Downloads are allowed per classification. |
56
+ | `filename` | Optional name used for downloads. |
57
+ | `expiresAt` | Optional future Unix time in milliseconds. After it, reads fail with `NOT_FOUND`. |
58
+
59
+ Committing the same bytes with the same metadata in the same scope returns the same reference. References are frozen
60
+ JSON objects that bind the scope, digest, size, media type, classification, filename and expiry together; changing any
61
+ field makes the reference invalid.
62
+
63
+ ## Read and download
64
+
65
+ `read(reference, scope)` returns the bytes after checking that the reference is intact, the scope matches, the file
66
+ exists, and its size and digest still match. A tampered or missing file fails with an integrity error and returns
67
+ nothing. A reference for another scope fails with `PERMISSION_DENIED`.
68
+
69
+ `disclose` prepares a download for an HTTP response:
70
+
71
+ ```ts
72
+ const download = await artifacts.disclose(reference, scope, {
73
+ classifications: ['public', 'internal'],
74
+ maxBytes: 5 * 1024 * 1024,
75
+ mediaTypes: ['text/csv', 'application/pdf'],
76
+ });
77
+ // download.body is the bytes; download.headers has Content-Type, Content-Length,
78
+ // Content-Disposition: attachment, and X-Content-Type-Options: nosniff.
79
+ ```
80
+
81
+ A disclosure is always an attachment, never rendered inline. HTML, SVG and XML types are refused even when their
82
+ classification is allowed, so a stored file cannot run script in your site. `mediaTypes` narrows the allowed types
83
+ further.
84
+
85
+ ## Delete, clean up and audit
86
+
87
+ - `delete(reference, scope)` removes one committed file.
88
+ - `reconcileStaging({ olderThan, maxDeletes })` removes staged files older than a cutoff, left behind when a process
89
+ stopped between `stage` and `commit`. Run it at startup or on a timer.
90
+ - `audit(references, scope, { maxTotalBytes })` checks a list of references and reports each as `ok`, `missing`,
91
+ `expired` or `integrity_failed`, without returning content.
92
+ - `planReconciliation` and `applyReconciliation` delete committed files that your records no longer reference. You pass
93
+ the complete set of references you keep for the scope, an age cutoff and limits; the plan lists candidates, and
94
+ applying it rechecks each file and skips any that changed. A plan can be applied once, by the store that made it.
95
+
96
+ ## Back up and restore
97
+
98
+ `backup({ scope, references, authoritativeSetComplete: true, maxTotalBytes })` returns one archive (bytes) for a whole
99
+ scope, with an integrity digest over its contents.
100
+ `restore(archive, scope, { maxArchiveBytes, maxTotalBytes, maxArtifacts })` installs the files into a store that is
101
+ empty or already holds an exact subset of the archive, so an interrupted restore can simply be retried. Run `audit` after a restore, before reopening access.
102
+
103
+ An archive holds at most 256 files and 64 MiB of content. It is not encrypted: store it somewhere protected, and keep
104
+ it together with the database backup that holds your references, because restoring files alone does not restore the
105
+ records that point at them.
106
+
107
+ ## Good to know
108
+
109
+ - Holding a reference is not permission to read the file. Authenticate the caller, derive the scope from their
110
+ verified identity, and check they may see the record the artifact belongs to.
111
+ - The store is local to one host. It is not a shared network filesystem or an object store, and it does not scan for
112
+ malware, encrypt files or schedule backups.
113
+ - Your database and the artifact folder are not updated in one transaction. Keep references in your records, and use
114
+ `reconcileStaging` and the reconciliation plan to clean up after crashes.
115
+ - To download from a remote source into any staging sink with a size limit and digest check, see `transferArtifact` in
116
+ [helpers](helpers.md).
117
+
118
+ ## Related
119
+
120
+ - [Storage](storage.md)
121
+ - [Durable workflows](durable-workflows.md)
122
+ - [Server and client](server-and-client.md)
123
+ - [Helpers](helpers.md)
@@ -0,0 +1,158 @@
1
+ ---
2
+ title: "Child agents"
3
+ description: "Let one agent call another as a tool, spawn child runs from your code, or race speculative branches, under one shared budget."
4
+ ---
5
+
6
+ A child agent is an agent that runs on behalf of another run: a summarizer the assistant calls, a researcher a
7
+ coordinator hands a topic to. Use child agents to split work between specialists with their own instructions, tools
8
+ and output schemas, while one budget, one permission ceiling and one cancellation cover the whole tree.
9
+
10
+ There are three ways to start one:
11
+
12
+ - **`agentAsTool`**: the parent's model decides when to call the child, like any other tool.
13
+ - **`runtime.spawn`**: your code starts a child of a running parent.
14
+ - **`runtime.speculate`**: your code runs a few alternative children and keeps at most one verified answer.
15
+
16
+ ```ts
17
+ import { agentAsTool, createRuntime, defineAgent, z } from 'mayura';
18
+ import { openAIResponses } from 'mayura/provider-openai';
19
+
20
+ const openai = openAIResponses({
21
+ apiKey: process.env.OPENAI_API_KEY!, model: process.env.OPENAI_MODEL!,
22
+ maxCostMicros: 20_000, pricing: { inputMicrosPerMillionTokens: 400_000, outputMicrosPerMillionTokens: 1_600_000 },
23
+ });
24
+
25
+ const summarizer = defineAgent({
26
+ id: 'summarizer', version: '1', instructions: 'Summarize the text in two sentences.',
27
+ model: openai, tools: [], input: z.object({ text: z.string() }), output: z.object({ summary: z.string() }),
28
+ });
29
+
30
+ const summarize = agentAsTool(summarizer, {
31
+ id: 'text.summarize',
32
+ description: 'Summarize a long text in two sentences.',
33
+ permissions: { allow: ['model:openai.responses'] },
34
+ limits: { maxCostMicros: 40_000 },
35
+ });
36
+
37
+ const assistant = defineAgent({
38
+ id: 'assistant', version: '1', instructions: 'Help the user. Summarize long documents before answering.',
39
+ model: openai, tools: [summarize], input: z.object({ message: z.string() }), output: z.object({ reply: z.string() }),
40
+ });
41
+
42
+ const runtime = createRuntime({
43
+ profile: 'ephemeral',
44
+ permissions: { allow: ['model:openai.responses', 'agent:delegate', 'tool:text.summarize'] },
45
+ limits: { maxCostMicros: 200_000 },
46
+ });
47
+ ```
48
+
49
+ One adapter serves both agents: Mayura sends each agent's own output schema with its calls, and the parent's model
50
+ sees the child's input schema as the tool's input.
51
+
52
+ ## Agents as tools
53
+
54
+ `agentAsTool(agent, options)` returns a tool that runs `agent` as a child and returns its validated output. The parent's
55
+ model sees an ordinary tool; the child's output is the tool result.
56
+
57
+ | Option | Meaning |
58
+ |---|---|
59
+ | `id`, `description` | The tool's id and the description the parent's model reads. |
60
+ | `permissions` | Required. What the child may do: `{ allow: [...] }`. |
61
+ | `limits` | Optional ceilings for the child, such as `maxCostMicros`. |
62
+ | `inputJsonSchema` | The input the parent's model sees. The default is the child's own input schema. |
63
+
64
+ The runtime must grant `agent:delegate` (to start children at all), `tool:<id>` (to call this tool) and everything the
65
+ child itself needs. If the child fails, the tool call fails and the parent run ends with the child's failure.
66
+
67
+ ## Spawning from your code
68
+
69
+ `runtime.spawn(parentRun, agent, options)` starts a child of a run that is still in progress:
70
+
71
+ ```ts
72
+ const parentRun = runtime.submit(coordinator, { input: { goal: 'Plan the launch' } });
73
+ const childRun = runtime.spawn(parentRun, researcher, {
74
+ input: { topic: 'competitor pricing' },
75
+ permissions: { allow: ['model:openai.responses', 'tool:web.search'] },
76
+ limits: { maxCostMicros: 50_000 },
77
+ });
78
+ const research = await childRun.result();
79
+ ```
80
+
81
+ The parent run must come from the same runtime, be still running, and hold `agent:delegate`. Spawning after the parent
82
+ finished throws `CONFLICT`. Every accepted child is required: the parent's result waits for its children, and a failed
83
+ child fails the parent.
84
+
85
+ Do not call `spawn` and wait for the child inside a tool's `execute`; that tool holds an execution slot while it waits.
86
+ Use `agentAsTool` for model-driven nesting.
87
+
88
+ ## Shared authority, budgets and cancellation
89
+
90
+ Children never get more than their parent:
91
+
92
+ - **Permissions** are intersected: a child gets only the grants that are both in its `permissions` and in its
93
+ parent's. Listing a grant the parent lacks does not add it.
94
+ - **Limits** are ceilings, not extra money. A child's `limits` default to the parent's and may only be lower; asking
95
+ for more throws `INVALID_CONFIG`. A child's spending comes out of the parent's budget, so a child with
96
+ `maxCostMicros: 40_000` can spend at most 40,000 of the parent's remaining funds.
97
+ - **Call counts are shared.** Model and tool calls count against every ancestor's `maxModelCalls` and `maxToolCalls`,
98
+ so children cannot multiply the number of calls.
99
+ - **Cancellation and deadlines flow down.** Cancelling a parent cancels all its descendants; a child's deadline is
100
+ never later than its parent's. Cancelling one child does not cancel its siblings.
101
+ - **The tree has size limits.** `limits.maxDepth` (default 8, at most 32) and `limits.maxDescendantRuns` (default 64, at most
102
+ 1,023) cap nesting and the number of children. An agent cannot appear twice in its own ancestry: a child with the
103
+ same agent id as one of its ancestors is refused.
104
+
105
+ A child sees only its own input, instructions and tools. It does not inherit the parent's conversation, memory or
106
+ provider state, and the parent's model sees only the child's validated output.
107
+
108
+ `runtime.inspect(run)` returns the run tree with statuses and the shared budget. The budget of a parent already
109
+ includes its children; do not add them up again.
110
+
111
+ ```ts
112
+ const view = runtime.inspect(parentRun);
113
+ console.log(view.runs.map(entry => `${entry.agentId}: ${entry.status}`), view.budget.spentMicros);
114
+ ```
115
+
116
+ ## Speculative branches
117
+
118
+ `runtime.speculate(parentRun, options)` starts 1 to 8 alternative children at once, for example a fast and a thorough
119
+ planner, and promotes at most one answer. Each successful branch is passed to your `verify` function as it finishes;
120
+ the first for which `verify` returns exactly `true` wins, and the others are cancelled.
121
+
122
+ ```ts
123
+ const result = await runtime.speculate(parentRun, {
124
+ branches: [
125
+ { id: 'quick', agent: quickPlanner, input: request, permissions: { allow: ['model:openai.responses'] },
126
+ assumptions: { inventoryVersion: 41 } },
127
+ { id: 'thorough', agent: carefulPlanner, input: request, permissions: { allow: ['model:openai.responses'] },
128
+ assumptions: { inventoryVersion: 41 } },
129
+ ],
130
+ verify: async ({ branchId, output }) => await stillValid(branchId, output),
131
+ verifyTimeoutMs: 5_000,
132
+ });
133
+ if (result.status === 'promoted') console.log(result.branchId, result.output);
134
+ ```
135
+
136
+ - Branches run under the parent's shared budget, and a failed or losing branch never fails the parent.
137
+ - Branches may not hold write or host effects, `agent:delegate`, or memory grants other than `memory:read`. A branch
138
+ asking for one is refused before any branch starts. Speculation is for work that is safe to throw away.
139
+ - `verify` should re-check that the branch's answer still holds (inputs, policy, the state it assumed). A thrown error,
140
+ a timeout (`verifyTimeoutMs`, default 5,000, at most 30,000) or any value other than `true` counts as a rejection.
141
+ - `assumptions` is JSON describing what the branch depends on. Its SHA-256 digest is passed to `verify` and reported
142
+ in `result.branches`, along with each branch's run id, status and whether it was verified.
143
+ - `result.status` is `'none'` when no branch was verified.
144
+
145
+ ## Good to know
146
+
147
+ - Child runs live in the runtime's process. For children that survive restarts, use workflows; see
148
+ [Workflow composition](workflow-composition.md).
149
+ - Cancellation stops new work but cannot undo an external write. A child with an uncertain write makes the parent's
150
+ outcome `outcome_unknown`; see [Outcomes](../concepts/outcomes.md).
151
+
152
+ ## Related
153
+
154
+ - [Agents](../concepts/agent.md)
155
+ - [Permissions](../concepts/permissions.md)
156
+ - [Costs and budgets](../concepts/costs-and-budgets.md)
157
+ - [Workflow composition](workflow-composition.md)
158
+ - [Testing](testing.md)
@@ -0,0 +1,233 @@
1
+ ---
2
+ title: "Code Mode"
3
+ description: "Run small JavaScript programs, including model-written ones, in a sandbox that can call only the tools you allow."
4
+ ---
5
+
6
+ Code Mode runs a small JavaScript program in a sandbox instead of in your process. The program gets a JSON input,
7
+ may call the tools you list for it, and returns a JSON output. Use it when a model should write a short program that
8
+ calls tools in a loop or combines their results, instead of making one tool call per model step, or when you need to
9
+ run code you do not fully trust. The program never touches your process: every tool call goes back through your own
10
+ tool broker, with its permissions, budget and guards.
11
+
12
+ ```ts
13
+ import { createQuickJsSandboxAdapter } from 'mayura/adapter-code-quickjs';
14
+ import { createCodeMode, defineCodeProgram } from 'mayura/code-mode';
15
+ import { Budget, defineTool, invokeTool, type JsonValue, type Outcome, z } from 'mayura';
16
+
17
+ const price = defineTool({
18
+ id: 'catalog.price', version: '1', description: 'Price of one product in cents.',
19
+ input: z.object({ sku: z.string() }), output: z.object({ cents: z.number().int() }),
20
+ effects: 'read', capabilities: [], costMicros: 0,
21
+ execute: async ({ sku }) => ({ cents: sku.length * 100 }),
22
+ });
23
+
24
+ const program = defineCodeProgram({
25
+ id: 'catalog.total', version: '1', intent: 'Add up the prices of several products.', language: 'javascript',
26
+ source: `async (input, tools) => {
27
+ let total = 0;
28
+ for (const sku of input.skus) {
29
+ const result = await tools.call('catalog.price', { sku });
30
+ if (result.status !== 'succeeded') throw new Error('price lookup failed');
31
+ total += result.output.cents;
32
+ }
33
+ return { totalCents: total };
34
+ }`,
35
+ input: z.object({ skus: z.array(z.string()).max(20) }),
36
+ output: z.object({ totalCents: z.number().int() }),
37
+ inputSchemaId: 'catalog.skus.v1',
38
+ outputSchemaId: 'catalog.total.v1',
39
+ tools: [price],
40
+ limits: { cpuMillis: 200, wallTimeMillis: 5_000, memoryBytes: 32 * 1024 * 1024, scratchBytes: 1_024,
41
+ maxInputBytes: 4_096, maxOutputBytes: 4_096, maxToolInputBytes: 1_024, maxToolCalls: 20, maxToolConcurrency: 1 },
42
+ });
43
+
44
+ const budget = new Budget(0, 20);
45
+ const mode = createCodeMode({
46
+ adapter: createQuickJsSandboxAdapter(),
47
+ invokeTool: (tool, input, context) => invokeTool(tool, input, {
48
+ runId: context.runId, callId: context.callId, scope: context.scope, signal: context.signal,
49
+ permissions: { allow: [`tool:${tool.id}`, 'effect:read'] }, budget,
50
+ }) as Promise<Outcome<JsonValue>>,
51
+ });
52
+
53
+ const outcome = await mode.execute(program, { skus: ['ab', 'cde'] }, {
54
+ runId: 'run-1', executionId: 'run-1:total', scope: { principalId: 'local', projectId: 'demo' },
55
+ signal: AbortSignal.timeout(10_000),
56
+ });
57
+ console.log(outcome.status, outcome.status === 'succeeded' ? outcome.output : outcome.error.code, outcome.usage);
58
+ ```
59
+
60
+ The QuickJS adapter needs two optional packages: `npm install quickjs-emscripten-core @jitl/quickjs-wasmfile-release-sync`.
61
+
62
+ ## Programs
63
+
64
+ `defineCodeProgram` captures a program without running it. The definition gets a SHA-256 `digest` over its source,
65
+ tools, limits and metadata; approvals and audit records refer to that exact digest.
66
+
67
+ | Option | Notes |
68
+ | --- | --- |
69
+ | `id`, `version`, `intent` | Identity, and a short description of what the program is for (shown to approvers). |
70
+ | `language` | `'javascript'`. The built-in sandboxes run JavaScript only and report `UNSUPPORTED_PROFILE` for `'typescript'`, which exists for custom sandboxes. |
71
+ | `source` | One function expression, `(input, tools) => output`, sync or async. Up to 1 MiB. It runs in strict mode. |
72
+ | `input`, `output` | Schemas for the program's input and result. The result is validated before you receive it. |
73
+ | `inputSchemaId`, `outputSchemaId` | Stable names for those schemas, recorded in the manifest. |
74
+ | `tools` | The only tools the program may call, up to 128. |
75
+ | `approvedImports` | Module specifiers for custom sandboxes that support imports. The built-in sandboxes support none and report `UNSUPPORTED_PROFILE` if you list any. |
76
+ | `limits` | Required; see below. |
77
+
78
+ Inside the sandbox, `tools.call(toolId, input)` returns `{ status, output }` on success or `{ status, error }` with a
79
+ public error code otherwise. It does not throw for a tool failure, so the program decides what to do; it throws only if
80
+ `input` cannot be serialized as JSON. Calling a tool that is not in `tools` returns `PERMISSION_DENIED`, and calls past
81
+ `maxToolCalls` return `LIMIT_EXCEEDED`. The program sees standard JavaScript (including `Date` and `Math.random`) and
82
+ `tools`, nothing else: no imports, network, file system, timers, `console` or `process`.
83
+
84
+ ## Limits
85
+
86
+ Every limit is required, so each program states its own ceiling.
87
+
88
+ | Limit | Meaning | Maximum |
89
+ | --- | --- | --- |
90
+ | `cpuMillis` | Time the interpreter spends running the program. Time spent waiting for tool calls does not count. Then `LIMIT_EXCEEDED`. | 1 hour |
91
+ | `wallTimeMillis` | Total time, including tool calls. Then `TIMEOUT`. | 1 hour |
92
+ | `memoryBytes` | The program's heap. The interpreter adds a fixed 16 MiB for itself. Then `LIMIT_EXCEEDED`. | 2 GiB |
93
+ | `scratchBytes` | Size of the Docker sandbox's `/tmp`. The QuickJS sandbox has no file system and ignores it. | 2 GiB |
94
+ | `maxInputBytes`, `maxOutputBytes` | Program input and result as JSON. | 16 MiB |
95
+ | `maxToolInputBytes` | Each tool call's input. | 16 MiB |
96
+ | `maxToolCalls` | Tool calls per execution. | 10,000 |
97
+ | `maxToolConcurrency` | Tool calls in flight at once; at most `maxToolCalls`. | 128 |
98
+
99
+ JSON values crossing the sandbox boundary may nest at most 32 levels deep and hold at most 100,000 values. Recursion is
100
+ limited to roughly 1,500 plain JavaScript frames; deeper recursion throws a catchable `InternalError: stack overflow`.
101
+
102
+ ## Running a program
103
+
104
+ `createCodeMode({ adapter, invokeTool })` returns an executor. `invokeTool` is your broker: it receives the tool, the
105
+ input the program sent and a context with `runId`, `callId`, `scope`, `signal` and the program digest, and returns the
106
+ tool's outcome. Calling `invokeTool` from `mayura` (as above) applies the tool's schemas, guards, permissions and
107
+ budget. Honour `context.signal`: an execution returns only after every tool call it started has settled.
108
+
109
+ `mode.execute(program, input, { runId, executionId, scope, signal })` never throws. It returns an outcome plus
110
+ `usage`: tool calls made, known cost, unresolved cost and the program's maximum possible cost. Use a new `executionId`
111
+ for each execution; the executor rejects one that is running or among the last 100,000 it finished.
112
+
113
+ | Result | When |
114
+ | --- | --- |
115
+ | `succeeded` | The program returned a value that passed the output schema. |
116
+ | `failed` with `TOOL_FAILED` | The program threw, did not compile, or returned a promise that can never settle; or the sandbox itself failed. |
117
+ | `failed` with `LIMIT_EXCEEDED` | The program used more than `cpuMillis` or `memoryBytes`. |
118
+ | `failed` with `INVALID_INPUT` or `INVALID_OUTPUT` | Input or result is not plain JSON within its size limit, or does not match its schema. |
119
+ | `failed` with `TIMEOUT` | `wallTimeMillis` passed. |
120
+ | `cancelled` with `CANCELLED` | Your `signal` aborted. |
121
+ | `outcome_unknown` | A tool call's result is unknown (for example it timed out mid-effect). The program's return value is discarded; reconcile the effect. |
122
+ | `failed` with `UNSUPPORTED_PROFILE` | The sandbox is not available on this machine, or cannot run the program's language or imports. Mayura never falls back to running code in-process. |
123
+ | `failed` with `CONFLICT` | The `executionId` was already used. |
124
+ | `failed` with `INVALID_CONFIG` | The program or execute options are invalid; the message names the field. |
125
+
126
+ Every `error.message` is written by Mayura and names the limit or rule involved; it never contains program text or
127
+ tool data. When the program itself threw, the outcome also has `programError: { name, message }`, the error it threw
128
+ (at most 128 and 1,024 characters). Show it to the model that wrote the program so it can fix it, but treat it like the
129
+ program's output: the program chose that text, and it may contain tool data.
130
+
131
+ ```ts
132
+ import type { CodeExecutionOutcome } from 'mayura/code-mode';
133
+
134
+ function feedback(outcome: CodeExecutionOutcome<unknown>): string {
135
+ if (outcome.status === 'succeeded') return 'ok';
136
+ if (outcome.programError) return `Your program threw ${outcome.programError.name}: ${outcome.programError.message}`;
137
+ return `${outcome.error.code}: ${outcome.error.message}`;
138
+ }
139
+ ```
140
+
141
+ ## Sandboxes
142
+
143
+ Both built-in adapters are qualified `production`: they meet the guarantees below, and `createCodeMode` accepts them
144
+ directly. [Security](../project/security.md#code-mode-sandboxing) explains what each one does and does not protect
145
+ against.
146
+
147
+ | Adapter | Import | Isolation |
148
+ | --- | --- | --- |
149
+ | QuickJS | `createQuickJsSandboxAdapter()` from `mayura/adapter-code-quickjs` | A QuickJS WebAssembly interpreter in a new Node.js child process per execution, under the Node.js permission model, with hard CPU, memory and stack limits. |
150
+ | Docker | `createDockerQuickJsSandboxAdapter({ dockerPath, image, provenance })` from `mayura/adapter-code-docker` | The same worker inside a new container per execution: no network, read-only root, non-root user, no capabilities, limits on processes, memory, CPU and files, a small `tmpfs`, no logs. |
151
+
152
+ Use QuickJS for programs from a model working for your own users. For programs from many tenants, or when a
153
+ sandbox escape must not reach the host's files or network, use Docker, ideally with gVisor (`runtime: 'runsc'`).
154
+
155
+ ### Your own sandbox
156
+
157
+ `defineSandboxAdapter({ id, version, qualification, isAvailable, execute })` wraps any sandbox. `execute` receives the
158
+ program, its input and a `tools` bridge, and returns `{ status: 'succeeded', output }` or
159
+ `{ status: 'failed', reason?, programError? }`, where `reason` is one of `program_error`, `cpu_limit`, `memory_limit`,
160
+ `invalid_output`, `unsupported_program` or `sandbox_error`. To run Mayura's QuickJS worker inside your own outer sandbox,
161
+ use `createQuickJsProtocolAdapter({ id, version, qualification, launch })`.
162
+
163
+ Declare `qualification: 'production'` only for a sandbox that meets guarantees you have documented and tested.
164
+ An adapter declared `'test'` (the default for `createQuickJsProtocolAdapter`) is for fixtures and experiments:
165
+ `createCodeMode` throws `UNSUPPORTED_PROFILE` for it unless you pass `allowTestAdapter: true`.
166
+
167
+ ### Docker
168
+
169
+ The Docker adapter takes an absolute path to the Docker CLI and exact `sha256:` digests for a locally built image and
170
+ its SPDX provenance document; it never pulls images or looks up `docker` on the `PATH`. Two options are optional:
171
+ `host`, a local daemon socket such as a rootless daemon's (`unix:///run/user/1000/docker.sock` or
172
+ `npipe:////./pipe/<name>`), and `runtime`, an OCI runtime such as `runsc`.
173
+ `createPromotedDockerQuickJsSandboxAdapter` additionally requires a signed statement that the image passed a clean
174
+ vulnerability scan, and checks it before every run.
175
+
176
+ Build the image from the files in the installed package, and rebuild it whenever you upgrade `mayura`, because the
177
+ worker inside must match the adapter:
178
+
179
+ 1. Make a build directory with a `root/` folder. Copy `node_modules/mayura/lib/adapter-code-quickjs/dist/worker.js` to
180
+ `root/worker.mjs`, write `{"type":"module"}` to `root/package.json`, and copy the installed
181
+ `quickjs-emscripten-core`, `@jitl/quickjs-wasmfile-release-sync` and `@jitl/quickjs-ffi-types` packages into
182
+ `root/node_modules/`.
183
+ 2. Generate an SPDX document for `root/` with your SBOM tool, keep it, and save it as `root/sbom.spdx.json`. Its
184
+ `sha256:` digest is your `provenance`.
185
+ 3. Copy `node_modules/mayura/lib/adapter-code-docker/image/Dockerfile` into the build directory and run
186
+ `docker build --network=none --build-arg MAYURA_PROVENANCE=sha256:<digest> --tag mayura-code-sandbox .`
187
+ 4. `docker image inspect --format '{{.Id}}' mayura-code-sandbox` prints the `image` value to configure.
188
+
189
+ ## Approvals and durable phases
190
+
191
+ `mayura/code-mode-workflows` turns programs into steps of a [durable workflow](durable-workflows.md). Every phase
192
+ requires a human to approve the exact program digest before it runs, and every execution writes an audit record of
193
+ the nested tool calls.
194
+
195
+ ```ts
196
+ import { createDurableCodeAudit, defineDurableCodeWorkflow } from 'mayura/code-mode-workflows';
197
+
198
+ const audit = createDurableCodeAudit({ store, scope: { principalId: 'local', projectId: 'demo' } });
199
+ const workflow = defineDurableCodeWorkflow({
200
+ id: 'catalog.quote', version: '1', input: program.input, output: program.output,
201
+ codeMode: mode, audit,
202
+ phases: [{ id: 'total', program, input: { kind: 'input', path: [] } }],
203
+ result: { kind: 'step', stepId: 'total', path: [] },
204
+ });
205
+ ```
206
+
207
+ Run it with `createScheduledWorkflowRuntime` from `mayura/workflows`. The run waits at each phase until someone calls
208
+ `runtime.approve` with that phase's approval digest; see [approvals and human input](approvals-and-human-input.md).
209
+ Each phase becomes a tool whose `capabilities` (`code:execute`, `code:audit:v2`, the audit scope and the program
210
+ digest) you grant to the runtime, together with `tool:<phase tool id>`. The phase takes the strongest effect of its
211
+ nested tools and reserves the program's maximum tool cost up front, releasing what it did not use. A phase allows at
212
+ most 64 tool calls. `audit.inspect(runId, phaseId)` returns the stored record, which never includes `programError`.
213
+
214
+ The [`code-mode-workflow` template](https://github.com/KartikeyAI/mayura/blob/main/packages/cli/templates/code-mode-workflow.ts)
215
+ is a complete, runnable version.
216
+
217
+ ## Good to know
218
+
219
+ - The program cannot hide an uncertain tool call: if any call's result is unknown, the execution is `outcome_unknown`
220
+ whatever the program returns.
221
+ - Durable phases persist only JSON input, output and evidence. A crash mid-phase does not resume the program; a phase
222
+ with write effects that was interrupted is reported as `outcome_unknown` and is not replayed automatically.
223
+ - Nothing in Code Mode decides whether generated code is correct or safe to approve. The approver sees the program's
224
+ `intent`, tools, limits and source; review them.
225
+
226
+ ## Related
227
+
228
+ - [Tools](../concepts/tools.md)
229
+ - [Durable workflows](durable-workflows.md)
230
+ - [Approvals and human input](approvals-and-human-input.md)
231
+ - [Permissions](../concepts/permissions.md)
232
+ - [Costs and budgets](../concepts/costs-and-budgets.md)
233
+ - [Security](../project/security.md)