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,176 @@
1
+ ---
2
+ title: "Helpers"
3
+ description: "Small utilities for configuration, secrets, retries, cancellation, pagination, redacted logging and budgeted concurrency."
4
+ ---
5
+
6
+ `mayura/helpers` collects the small utilities most agent applications end up writing themselves: reading
7
+ configuration safely, handling credentials, retrying, deadlines, pagination, logging without leaking secrets, and
8
+ running tasks in parallel under a cost budget. None of them read the environment, the network or the filesystem on
9
+ their own. You pass every source, signal and sink explicitly, which keeps them easy to test.
10
+
11
+ ```ts
12
+ import { retry, validatedEnvironment } from 'mayura/helpers';
13
+ import { z } from 'mayura';
14
+
15
+ const config = await validatedEnvironment({
16
+ schema: z.object({ apiUrl: z.url(), timeoutMs: z.coerce.number().int().positive() }),
17
+ source: process.env,
18
+ fields: { apiUrl: 'ORDERS_API_URL', timeoutMs: 'ORDERS_TIMEOUT_MS' },
19
+ });
20
+
21
+ const orders = await retry(async (_attempt, signal) => {
22
+ const response = await fetch(`${config.apiUrl}/orders`, { signal });
23
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
24
+ return response.json();
25
+ }, { signal: AbortSignal.timeout(30_000), maxAttempts: 3, initialDelayMs: 250, maxDelayMs: 2_000, safety: 'read-only' });
26
+ ```
27
+
28
+ ## Configuration
29
+
30
+ | Helper | Use it to |
31
+ | --- | --- |
32
+ | `validatedEnvironment({ schema, source, fields })` | Pick named variables from `source` (usually `process.env`), map them to your field names and validate them. Only the variables you list are read. An error names the failing variables, never their values. |
33
+ | `validatedConfig(schema, value)` | Validate any configuration object and return a frozen copy. Rejected values are not echoed in errors. |
34
+ | `providerSchema(schema, jsonSchema)` | Pair a runtime validator with the JSON Schema you send to a model provider, so neither is inferred from the other. |
35
+ | `capture(operation)` | Run an operation and get `{ ok: true, value }` or `{ ok: false, error }` with a safe public error instead of a thrown exception. |
36
+
37
+ Empty variables are rejected unless you pass `allowEmpty: true`. Each value is limited to 16 KiB, and a mapping can
38
+ list up to 128 variables.
39
+
40
+ ## Secret references and the credential broker
41
+
42
+ Keep secrets out of configuration objects. A `secretReference({ provider, key, version })` is a frozen handle that
43
+ names a secret without containing it. The credential broker resolves a handle only for the duration of one callback:
44
+
45
+ ```ts
46
+ import { createCredentialBroker, defineCredentialProvider, secretReference } from 'mayura/helpers';
47
+
48
+ const vault = defineCredentialProvider({
49
+ id: 'vault',
50
+ resolve: async ({ key }, signal) => {
51
+ const value = await readSecretFromVault(key, signal);
52
+ return { bytes: new TextEncoder().encode(value), version: '1' };
53
+ },
54
+ });
55
+ const broker = createCredentialBroker({ providers: [vault] });
56
+
57
+ const apiKey = secretReference({ provider: 'vault', key: 'orders-api-key' });
58
+ const status = await broker.use(apiKey, AbortSignal.timeout(10_000), async credential => {
59
+ const response = await fetch('https://orders.example.com/health', {
60
+ headers: { authorization: `Bearer ${new TextDecoder().decode(credential)}` },
61
+ });
62
+ return response.status;
63
+ });
64
+ ```
65
+
66
+ The broker copies the secret into a fresh buffer, passes it to your callback, and zeroes it afterwards. It never
67
+ caches. Resolution times out after 10 seconds by default (`timeoutMs`, up to 60 seconds), at most 16 uses run at once
68
+ (`maxConcurrent`), and secrets are limited to 64 KiB (`maxSecretBytes`). A provider can return `expiresAtMs`; expired
69
+ material is refused. If the callback throws, `use` rejects with `TOOL_FAILED` and no detail, so a secret cannot leak
70
+ through an error message.
71
+
72
+ ## Retries
73
+
74
+ `retry(operation, options)` calls `operation(attempt, signal)` until it succeeds or the attempts run out, waiting
75
+ `initialDelayMs`, then multiplying by `backoffFactor` (default 2) up to `maxDelayMs`.
76
+
77
+ The required `safety` option is your statement about repeating the operation:
78
+
79
+ | `safety` | Meaning |
80
+ | --- | --- |
81
+ | `'read-only'` | The operation changes nothing. Safe to repeat. |
82
+ | `'idempotent'` | Repeating it has the same effect as doing it once, for example because it sends an idempotency key. |
83
+ | `'single-attempt'` | Must not be repeated. `maxAttempts` above 1 is rejected with `PERMISSION_DENIED`. |
84
+
85
+ Only claim `read-only` or `idempotent` when it is true: retrying a payment that is neither can charge twice. Other
86
+ options: `maxAttempts` (up to 10), `retryable(error, attempt)` to stop early on errors that will not go away, and an
87
+ `onRetry` hook that sees the attempt number, the next delay and the error code, and can return
88
+ `{ decision: 'block' }` to stop retrying.
89
+
90
+ ## Cancellation and deadlines
91
+
92
+ | Helper | What it does |
93
+ | --- | --- |
94
+ | `withDeadline(operation, signal, timeoutMs)` | Runs `operation(childSignal)` and rejects with `TIMEOUT` or `CANCELLED` when the deadline passes or the parent signal aborts. A late result is discarded. |
95
+ | `deadlineSignal(signal, timeoutMs)` | A child signal that aborts at the deadline or with its parent. Call `dispose()` when done. |
96
+ | `delay(ms, signal)` | A sleep that rejects with `CANCELLED` when the signal aborts. |
97
+ | `pollUntil(read, accept, { signal, maxAttempts, intervalMs })` | Reads until `accept(value)` is true, then returns the value; rejects with `TIMEOUT` after `maxAttempts`. |
98
+ | `withCleanup(acquire, use, release)` | Runs `release` exactly once. A failing `release` never hides an error from `use`. |
99
+
100
+ JavaScript cannot stop a running function from outside. These helpers stop waiting and hand the abort signal to your
101
+ code; your code has to honour it.
102
+
103
+ ## Pagination
104
+
105
+ `collectPages(fetchPage, { signal, maxPages, maxItems })` follows cursor pagination until a page has no `nextCursor`.
106
+ Each call to `fetchPage(cursor, signal)` returns `{ items, nextCursor }`. It stops with `LIMIT_EXCEEDED` when a limit is
107
+ reached and with `CONFLICT` when the API returns a cursor it has already seen, so a buggy API cannot loop forever.
108
+
109
+ ```ts
110
+ import { collectPages } from 'mayura/helpers';
111
+
112
+ const tickets = await collectPages(async (cursor, signal) => {
113
+ const response = await fetch(`https://tickets.example.com/list${cursor ? `?after=${cursor}` : ''}`, { signal });
114
+ const body = await response.json() as { items: { id: string }[]; next?: string };
115
+ return body.next ? { items: body.items, nextCursor: body.next } : { items: body.items };
116
+ }, { signal: AbortSignal.timeout(60_000), maxPages: 50, maxItems: 5_000 });
117
+ ```
118
+
119
+ ## Redacted logging
120
+
121
+ `createRedactedLogger(sink, { allowedFields, redactedFields })` writes structured JSON entries to your `sink`. Only
122
+ fields in `allowedFields` are kept; fields also listed in `redactedFields` are written as `[REDACTED]`. Anything else
123
+ is dropped, so a new field cannot start leaking by accident.
124
+
125
+ ```ts
126
+ import { createRedactedLogger } from 'mayura/helpers';
127
+
128
+ const logger = createRedactedLogger(entry => { console.log(JSON.stringify(entry)); }, {
129
+ allowedFields: ['orderId', 'status', 'customerEmail'],
130
+ redactedFields: ['customerEmail'],
131
+ });
132
+ await logger.log('info', 'order.refunded', { orderId: 'ord-1001', status: 'ok', customerEmail: 'a@example.com', token: 'secret' });
133
+ // {"timestamp":1790000000000,"level":"info","event":"order.refunded","fields":{"orderId":"ord-1001","status":"ok","customerEmail":"[REDACTED]"}}
134
+ ```
135
+
136
+ ## Budgeted concurrency
137
+
138
+ `runBudgetedTasks(tasks, { budget, signal, concurrency })` runs up to 128 tasks, at most `concurrency` (up to 64) at a
139
+ time, under a `Budget` from `mayura`. Before any task starts, it reserves every task's `maxCostMicros` in one step; if
140
+ the budget cannot cover all of them, nothing runs. Each task returns `{ value, costMicros }`, and unused reservation is
141
+ released.
142
+
143
+ ```ts
144
+ import { Budget } from 'mayura';
145
+ import { runBudgetedTasks } from 'mayura/helpers';
146
+
147
+ const budget = new Budget(50_000, 10);
148
+ const results = await runBudgetedTasks(['a', 'b', 'c'].map(topic => ({
149
+ id: `summarize-${topic}`,
150
+ maxCostMicros: 10_000,
151
+ execute: async (signal: AbortSignal) => ({ value: await summarize(topic, signal), costMicros: 4_000 }),
152
+ })), { budget, signal: AbortSignal.timeout(60_000), concurrency: 2 });
153
+ ```
154
+
155
+ Results keep the order of the tasks. A task that throws, or reports its cost incorrectly, comes back as
156
+ `outcome_unknown`: it may have done its work and spent money, so its whole reservation stays charged.
157
+
158
+ ## Verified downloads
159
+
160
+ `transferArtifact(source, stage, { signal, expectedDigest, maxBytes })` copies an async stream of byte chunks into a
161
+ staging sink (`write`, `commit`, `discard`), checks the size limit (up to 64 MiB) and the expected `sha256:` digest,
162
+ and only then calls `commit`. On any failure it calls `discard`. See [artifacts](artifacts.md) for local file storage.
163
+
164
+ ## Good to know
165
+
166
+ - The helpers coordinate your own trusted code. They are not a sandbox; for untrusted code use
167
+ [Code Mode](code-mode.md).
168
+ - Errors are `MayuraError`s with stable codes such as `TIMEOUT`, `CANCELLED` and `LIMIT_EXCEEDED`, and messages that never
169
+ include the values involved. See [outcomes](../concepts/outcomes.md).
170
+
171
+ ## Related
172
+
173
+ - [Costs and budgets](../concepts/costs-and-budgets.md)
174
+ - [Lifecycle hooks](lifecycle-hooks.md)
175
+ - [Deployment](deployment.md)
176
+ - [Artifacts](artifacts.md)
@@ -0,0 +1,187 @@
1
+ ---
2
+ title: "Lifecycle hooks"
3
+ description: "Run your own checks and observers at fixed points of an agent run: before model and tool calls, before output release, and at the end."
4
+ ---
5
+
6
+ Lifecycle hooks let your code take part in every run of an agent at fixed points: before the run starts, before each
7
+ model call, before each tool call, before a result is released, and when the run finishes. There are two kinds.
8
+ **Control hooks** can stop the run and can ask the runtime to call read-only tools first, for example a policy check.
9
+ **Observer hooks** watch without changing anything, for example to write an audit log or count outcomes. Use hooks for
10
+ cross-cutting policy and telemetry; use [guards](guardrails.md) to check or redact content itself.
11
+
12
+ ```ts
13
+ import { defineAgent, defineHook, z } from 'mayura';
14
+
15
+ const releasePolicy = defineHook({
16
+ id: 'policy.release',
17
+ version: '1',
18
+ stage: 'beforeOutputRelease',
19
+ tools: [],
20
+ handler: event => ({ decision: event.candidate === null ? 'block' : 'continue' }),
21
+ });
22
+
23
+ const audit = defineHook({
24
+ id: 'audit.tools',
25
+ version: '1',
26
+ stage: 'afterToolCall',
27
+ mandatory: true,
28
+ handler: async event => { await auditLog.append({ tool: event.toolId, status: event.status }); },
29
+ });
30
+
31
+ const agent = defineAgent({
32
+ id: 'support.assistant', version: '1', instructions: 'Answer questions about orders.',
33
+ model, tools: [], input: z.string(), output: z.string(),
34
+ hooks: [releasePolicy, audit],
35
+ });
36
+ ```
37
+
38
+ Hooks belong to the agent definition, so they run on every run of that agent. An agent accepts up to 16 hooks, with
39
+ unique ids. Hooks of the same stage run in the order you list them.
40
+
41
+ ## Stages
42
+
43
+ Control stages are awaited before the step they protect. Each handler returns `{ decision: 'continue' }` or
44
+ `{ decision: 'block' }`.
45
+
46
+ | Stage | Runs | The event contains |
47
+ | --- | --- | --- |
48
+ | `beforeExecution` | After the input schema and input guards, before the first model call | `input`, and `media` (a summary of each image or PDF: type, size, name) when there is some |
49
+ | `beforeStep` | Before each reasoning step | `step` |
50
+ | `beforeModelCall` | Before each primary model call | `modelId`, `purpose`, and `request` with `messages`, `tools`, `maxOutputTokens`, and `media` (summaries by message index) when there is some; hooks never see media bytes |
51
+ | `beforeToolCall` | Before each tool call the model proposed | `proposal` with `callId`, `toolId`, `input` |
52
+ | `beforeDelegate` | Before a child agent starts (on the parent's hooks) | `childRunId`, `childAgentId`, `input` |
53
+ | `beforeOutputRelease` | After output guards, before a tool result enters the conversation or the final answer is released | `source` (`agent` or `tool`), `callId`, `toolId`, `candidate` |
54
+
55
+ Observer stages see metadata only, never content, and return nothing.
56
+
57
+ | Stage | Runs | The event contains |
58
+ | --- | --- | --- |
59
+ | `afterStep` | After each step | `step`, `result` (`tool_calls`, `final` or `stopped`) |
60
+ | `afterModelCall` | After each primary model call | `step`, `modelId`, `response`, `toolCalls` |
61
+ | `afterToolCall` | After each tool call | `callId`, `toolId`, `status`, `execution`, `disclosure` |
62
+ | `afterDelegate` | After a child agent finishes (on the parent's hooks) | `childRunId`, `childAgentId`, `status` |
63
+ | `onViolation` | When a guard, hook or permission check refuses something | `source`, `boundary`, `code` |
64
+ | `afterExecution`, `onError`, `onCancel`, `onBlocked` | Once at the end, depending on the outcome | `status`, `error.code` |
65
+ | `onFinally` | Once at the very end, always | `status`, `error.code` |
66
+
67
+ Every handler also receives a `context` with the run's `runId`, `rootId`, `parentId`, `agentId`, verified `scope`,
68
+ the hook's id and version, the current `step` (`null` before the first step) and an abort `signal`. The event and
69
+ context are frozen.
70
+
71
+ ## Decisions
72
+
73
+ A control hook that returns `{ decision: 'block' }` ends the run with status `blocked` and code `GUARD_BLOCKED`. A
74
+ handler that throws, times out or returns anything else ends it `blocked` with `GUARD_UNAVAILABLE`. Hooks cannot
75
+ rewrite what they see: the model request, the tool proposal and the candidate output stay exactly as they are. To
76
+ change content, use a guard with a `rewrite` verdict.
77
+
78
+ The `beforeToolCall` event holds the model's raw proposal, before the tool's input schema transforms it.
79
+
80
+ ## Actions: calling a tool from a hook
81
+
82
+ A control hook can ask the runtime to run tools before it continues. List the tools the hook may call in `tools`, and
83
+ return them as `actions`:
84
+
85
+ ```ts
86
+ import { defineHook, defineTool, z } from 'mayura';
87
+
88
+ const assertProjectReadable = defineTool({
89
+ id: 'policy.assert-readable', version: '1', description: 'Fails unless the path is inside the project.',
90
+ input: z.string(), output: z.literal(true), effects: 'none', capabilities: [],
91
+ execute: path => {
92
+ if (!path.startsWith('project/')) throw new Error('Outside the project.');
93
+ return true as const;
94
+ },
95
+ });
96
+
97
+ const proposalPolicy = defineHook({
98
+ id: 'policy.proposal',
99
+ version: '1',
100
+ stage: 'beforeToolCall',
101
+ tools: [assertProjectReadable],
102
+ handler: event => ({
103
+ decision: 'continue',
104
+ actions: [{ toolId: assertProjectReadable.id, input: event.proposal.input }],
105
+ }),
106
+ });
107
+ ```
108
+
109
+ - Hook tools must have `effects: 'none'` or `'read'`. Agent wrappers and child workflows are not allowed.
110
+ - The tools are private to the hook. The model does not see them, and listing them grants nothing: the runtime still
111
+ needs `tool:<id>`, the tool's capabilities and `effect:read` for a read tool.
112
+ - Actions run one after another through the normal tool path: same scope, budget, tool guards and the agent's output
113
+ guards. If one fails, the run stops with that tool's outcome.
114
+ - The action's result is **not** passed back to the handler. Write the policy as an assertion: a tool that fails (or
115
+ whose guard blocks) when the check fails. A tool that returns `false` successfully does not block anything.
116
+ - Actions are not a transaction. A later failure does not undo an earlier action.
117
+
118
+ ## Observers and `mandatory`
119
+
120
+ An observer that throws or times out is recorded as a failed hook, and the run continues. Set `mandatory: true` when
121
+ the observation must happen, such as an audit log: if a mandatory observer fails, the run ends `blocked` with
122
+ `GUARD_UNAVAILABLE` and a successful answer is withheld. The end-of-run stages `onViolation`, `onError`, `onCancel`,
123
+ `onBlocked` and `onFinally` never change the outcome, even when mandatory. A mandatory `afterExecution` observer can
124
+ withhold a successful answer.
125
+
126
+ ## Limits
127
+
128
+ | Option | Default | Maximum | Applies to |
129
+ | --- | --- | --- | --- |
130
+ | `timeoutMs` | 5,000 | 30,000 | Each hook call, including its actions |
131
+ | `maxActions` | 4 | 8 | Control hooks, per call |
132
+ | `maxResultBytes` | 65,536 | 1,048,576 | Control hooks: the decision and all action inputs |
133
+ | `tools` | none | 32 | Control hooks (required, `[]` if none) |
134
+
135
+ The runtime also counts every hook call against its `maxHookCalls` limit (default 128, maximum 4,096). Hook calls do
136
+ not use model steps or cost anything themselves; the tools they call are charged like any other tool call.
137
+
138
+ ## Hooks outside agent runs
139
+
140
+ Some lifecycle points belong to other parts of Mayura and take plain callbacks rather than `defineHook`:
141
+
142
+ | Where | Option | Stages |
143
+ | --- | --- | --- |
144
+ | [Memory](memory-and-context.md) | `hooks` on `createNativeMemory`, `createMemoryStore` | `beforeMemoryWrite` (control), `afterMemoryWrite` (observer) |
145
+ | [Context assembly](memory-and-context.md) | `hooks` on `assembleContext` | `beforeContextBuild`, `afterContextBuild` (both control) |
146
+ | [Retries](helpers.md) | `onRetry` on `retry` | `onRetry` (control) |
147
+ | [Durable workflows](durable-workflows.md) | `createWorkflowHookRelay` from `mayura/workflows` | `onWait`, `onResume`, `onApprovalRequested`, `onApprovalResolved`, and the end stages |
148
+
149
+ ```ts
150
+ import { createNativeMemory } from 'mayura/memory';
151
+
152
+ const memory = createNativeMemory({
153
+ store, scope, permissions: { allow: ['memory:read', 'memory:write'] },
154
+ hooks: {
155
+ beforeMemoryWrite: event => ({ decision: event.candidate?.sensitivity === 'restricted' ? 'block' : 'continue' }),
156
+ afterMemoryWrite: event => { console.log('memory', event.operation, event.id, event.version); },
157
+ },
158
+ });
159
+ ```
160
+
161
+ A blocked memory write stores nothing. If `afterMemoryWrite` fails, the call rejects with a message saying the write
162
+ did commit.
163
+
164
+ The workflow hook relay reads a run's stored event log and delivers callbacks from a durable cursor. Call
165
+ `relay.deliver(runId)` from a worker loop. Delivery is at least once, so deduplicate on `event.eventId`.
166
+
167
+ ## Events
168
+
169
+ Runs with hooks emit `hook.started` and `hook.completed` events with the hook id, stage, step and completion status.
170
+ They never include content, action inputs or error messages. See [observability](observability.md).
171
+
172
+ ## Good to know
173
+
174
+ - Handlers are your code in your process. They are trusted: hooks are not a sandbox.
175
+ - A timed-out handler cannot dispatch the actions it returns later, but its promise keeps running until it settles.
176
+ - `beforeModelCall` sees the messages and tools sent to the model, not the agent instructions or the provider's
177
+ private continuation state.
178
+ - The final `beforeOutputRelease` runs before the run waits for required child agents; a child that fails later can
179
+ still fail the parent.
180
+
181
+ ## Related
182
+
183
+ - [Guardrails](guardrails.md)
184
+ - [Agents](../concepts/agent.md)
185
+ - [Permissions](../concepts/permissions.md)
186
+ - [Observability](observability.md)
187
+ - [Durable workflows](durable-workflows.md)
@@ -0,0 +1,123 @@
1
+ ---
2
+ title: "MCP tools"
3
+ description: "Wrap one operation of a Model Context Protocol server as a Mayura tool, with the effects, permissions and cost you declare."
4
+ ---
5
+
6
+ The Model Context Protocol (MCP) is a standard way for servers to offer tools to AI applications. `mayura/adapter-mcp`
7
+ turns one tool of an MCP server into an ordinary Mayura tool, so an agent can call it under the same permission,
8
+ validation, guard, budget and timeout checks as your own tools.
9
+
10
+ Mayura does not connect to MCP servers itself, discover their tools or read their descriptions of what a tool does.
11
+ You connect with an MCP client of your choice, pick each remote tool you want, and declare what it is allowed to do.
12
+
13
+ ```ts
14
+ import { createRuntime, defineAgent, z } from 'mayura';
15
+ import { defineMcpTool } from 'mayura/adapter-mcp';
16
+
17
+ const IssueInput = z.object({ title: z.string().max(200), body: z.string().max(10_000) });
18
+ const IssueOutput = z.object({ number: z.number().int(), url: z.string() });
19
+
20
+ const createIssue = defineMcpTool({
21
+ id: 'tracker.create_issue',
22
+ version: '1',
23
+ description: 'Create an issue in the team tracker.',
24
+ remoteName: 'create_issue', // the tool's name on the MCP server
25
+ input: IssueInput,
26
+ output: IssueOutput,
27
+ effects: 'write',
28
+ capabilities: ['tracker:write'],
29
+ timeoutMs: 15_000,
30
+ client: {
31
+ // `mcp` is a connected client from an MCP SDK, for example the official TypeScript SDK's Client.
32
+ callTool: ({ name, arguments: args, signal }) => mcp.callTool({ name, arguments: args }, undefined, { signal }),
33
+ },
34
+ });
35
+
36
+ const agent = defineAgent({
37
+ id: 'triage', version: '1', instructions: 'File an issue for each bug report.',
38
+ model, tools: [createIssue], input, output,
39
+ });
40
+
41
+ const runtime = createRuntime({
42
+ profile: 'ephemeral',
43
+ permissions: { allow: ['model:openai.responses', 'tool:tracker.create_issue', 'tracker:write', 'effect:write'] },
44
+ limits: { maxCostMicros: 100_000 },
45
+ });
46
+ ```
47
+
48
+ ## What you declare
49
+
50
+ `defineMcpTool` takes the same options as `defineTool`, minus `execute`, plus `remoteName` and `client`:
51
+
52
+ | Option | Meaning |
53
+ |---|---|
54
+ | `id`, `version`, `description` | The Mayura tool. The model reads `description`, so write your own; the server's is not used. |
55
+ | `remoteName` | The tool's name on the MCP server (letters, digits and `._/-`, up to 128 characters). |
56
+ | `input`, `output` | Validators for the arguments and the result. The input must be a JSON object. |
57
+ | `inputJsonSchema` | The input the model sees. Generated from `input` when the validator can describe itself (Zod 4.2 and later). |
58
+ | `effects` | `none`, `read`, `write` or `host`: what calling this tool can change. |
59
+ | `capabilities` | Extra permission strings the runtime must grant, such as `tracker:write`. |
60
+ | `client` | An object with a `callTool(request)` method (below). |
61
+ | `timeoutMs`, `costMicros` | Deadline (default 30,000) and the most one call may cost (default 0). |
62
+
63
+ Nothing is taken from the server: not the effects, not the cost, not the schema. Declare them from what you know the
64
+ remote tool does. To call a tool, the runtime must grant `tool:<id>`, every capability, and `effect:<effects>` unless
65
+ the effects are `none`. See [Permissions](../concepts/permissions.md).
66
+
67
+ ## The client
68
+
69
+ `client` is any object with a `callTool` method. Mayura calls it with a frozen request:
70
+
71
+ ```ts
72
+ import type { McpClient } from 'mayura/adapter-mcp';
73
+
74
+ const client: McpClient = {
75
+ async callTool({ name, arguments: args, signal }) {
76
+ // Send an MCP tools/call request for `name` with `args`, and stop when `signal` aborts.
77
+ return await mcp.callTool({ name, arguments: args }, undefined, { signal });
78
+ },
79
+ };
80
+ ```
81
+
82
+ Connecting, authenticating and closing the MCP session are up to you and your MCP client. Mayura only calls
83
+ `callTool`, and only after the runtime's checks allowed the call and the input passed its validator.
84
+
85
+ ## What the server must return
86
+
87
+ `callTool` must resolve to the MCP tool result: an object with `structuredContent`, and optionally `content`,
88
+ `isError` and `_meta`. Mayura validates `structuredContent` with your `output` schema and hands that to the agent.
89
+
90
+ The call fails when:
91
+
92
+ - the result has no `structuredContent` (text-only results are not parsed into data); MCP tools that declare an
93
+ output schema return structured content;
94
+ - `isError` is `true`;
95
+ - the result has any other top-level key, or is larger than 1 MiB;
96
+ - `callTool` throws, or the timeout passes.
97
+
98
+ Error details from the server or the transport never reach the model, events or outcomes. `_meta` is accepted and
99
+ dropped.
100
+
101
+ ## When a call fails
102
+
103
+ For a tool with effects other than `none`, a failure after the request was sent means Mayura cannot know whether the
104
+ server acted. The run ends `outcome_unknown`, and you should check the remote system before retrying. This includes a
105
+ result with `isError: true`, because the server may have done part of the work. For a tool with effects `none`, the run
106
+ ends `failed`. See [Outcomes](../concepts/outcomes.md).
107
+
108
+ ## Good to know
109
+
110
+ - Wrap only the remote tools the agent needs, one `defineMcpTool` each. There is no way to expose a whole server at
111
+ once.
112
+ - An MCP server is someone else's code. Its results pass your `output` schema and the agent's output guards before the
113
+ model sees them; add [guardrails](guardrails.md) if a server may return text you would not show a model.
114
+ - Server-side tool lists and descriptions can change. Because you declare each tool, a change on the server does not
115
+ change what your agent is allowed to do.
116
+
117
+ ## Related
118
+
119
+ - [Tools](../concepts/tools.md)
120
+ - [Permissions](../concepts/permissions.md)
121
+ - [Outcomes](../concepts/outcomes.md)
122
+ - [Guardrails](guardrails.md)
123
+ - [Model providers](model-providers.md)