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
@@ -5,6 +5,7 @@ import { createWorkflowCompositeFleetRuntime, type WorkflowCompositeCursor, type
5
5
  type WorkflowCompositeFleetRuntime, type WorkflowCompositeOutcome } from './composite-fleet.js';
6
6
  import { assertWorkflowLoop, type AnyWorkflowLoop } from './loop-definition.js';
7
7
  import { assertWorkflowSaga, type AnyWorkflowSaga } from './saga-definition.js';
8
+ import { createSweepPosition } from './sweep-position.js';
8
9
 
9
10
  export interface WorkflowCompositeHostOptions extends WorkflowCompositeFleetOptions {
10
11
  readonly sagaDefinitions?: readonly AnyWorkflowSaga[]; readonly loopDefinitions?: readonly AnyWorkflowLoop[];
@@ -41,14 +42,31 @@ export function createWorkflowCompositeHost(options: WorkflowCompositeHostOption
41
42
  if (backoffMaximum < interval) throw new MayuraError('INVALID_CONFIG', 'maxBackoffMs must be at least intervalMs.');
42
43
  const hold = options.hold; if (hold !== undefined && typeof hold?.isHeld !== 'function') throw new MayuraError('INVALID_CONFIG', 'A fleet hold reader requires isHeld().');
43
44
  const runtime = createWorkflowCompositeFleetRuntime(options); let cursor: WorkflowCompositeCursor | null = null; let closed = false;
45
+ // The sweep continues where the last process left it, rather than starting over (see sweep-position.ts).
46
+ const position = createSweepPosition<WorkflowCompositeCursor>(options.store, { kind: 'composite', scope: options.scope,
47
+ catalog: [...sagas, ...loops].map(definition => definition.digest) });
44
48
  let cycles = 0; let failures = 0; let lastError: ErrorCode | null = null; let lastCycle: WorkflowCompositeHostCycle | null = null;
45
49
  let controller: AbortController | undefined; let loop: Promise<void> | undefined; let active: Promise<WorkflowCompositeHostCycle> | undefined;
46
50
  const runOnce = (): Promise<WorkflowCompositeHostCycle> => { if (closed) return Promise.reject(new MayuraError('CANCELLED', 'Composite host is closed.')); if (active) return active;
47
51
  const operation = (async () => { let pages = 0; let examined = 0; let shardReads = 0; const outcomes: WorkflowCompositeOutcome[] = [];
48
52
  const held = hold ? await hold.isHeld() === true : false;
49
- if (!held) do { const report = await runtime.runPage({ sagas, loops }, { cursor, limit, maxShardReads: reads }); pages += 1;
50
- examined += report.page.examined; shardReads += report.page.shardReads; outcomes.push(...report.outcomes); cursor = report.page.nextCursor; }
51
- while (cursor && pages < pageMaximum); const completedSweep = !held && cursor === null;
53
+ if (!held) {
54
+ const stored = await position.load(); let restored = false;
55
+ if (stored !== null && cursor === null) { cursor = stored; restored = true; }
56
+ do {
57
+ const page = (from: WorkflowCompositeCursor | null) => runtime.runPage({ sagas, loops }, { cursor: from, limit, maxShardReads: reads });
58
+ let report: Awaited<ReturnType<typeof page>>;
59
+ try { report = await page(cursor); } catch (error) {
60
+ // A stored position this host cannot use restarts the sweep instead of failing every cycle.
61
+ if (!restored || !(error instanceof MayuraError) || error.code !== 'INVALID_INPUT') throw error;
62
+ cursor = null; report = await page(cursor);
63
+ }
64
+ restored = false; pages += 1;
65
+ examined += report.page.examined; shardReads += report.page.shardReads; outcomes.push(...report.outcomes); cursor = report.page.nextCursor;
66
+ } while (cursor && pages < pageMaximum);
67
+ await position.save(cursor);
68
+ }
69
+ const completedSweep = !held && cursor === null;
52
70
  const result = freezeJson(jsonValue({ pages, examined, shardReads, outcomes, completedSweep, held })) as unknown as WorkflowCompositeHostCycle;
53
71
  cycles += 1; failures = 0; lastError = null; lastCycle = result; return result; })();
54
72
  active = operation; void operation.finally(() => { if (active === operation) active = undefined; }).catch(() => {}); return operation; };
@@ -99,7 +99,7 @@ export function createWorkflowHookRelay(options: WorkflowHookRelayOptions): Work
99
99
  const cursorId = (runId: string): string => digest('mayura:workflow-hook-relay:v1', { relayId, runId });
100
100
  const guarded = async <T>(operation: () => Promise<T>): Promise<T> => {
101
101
  try { return await operation(); }
102
- catch (error) { if (error instanceof MayuraError || (error instanceof StorageError && error.code === 'CONFLICT')) throw error;
102
+ catch (error) { if (error instanceof MayuraError || (error instanceof StorageError && error.storageCode === 'CONFLICT')) throw error;
103
103
  throw new MayuraError('STORAGE_UNAVAILABLE', 'Workflow hook relay storage is unavailable.'); }
104
104
  };
105
105
  const read = async (runId: string): Promise<{ record?: StoredRecord; sequence: number }> => {
@@ -122,7 +122,7 @@ export function createWorkflowHookRelay(options: WorkflowHookRelayOptions): Work
122
122
  definitionHash: digest('mayura:workflow-hook-relay-format:v1', {}), state, events: [event] }))).record;
123
123
  return { record, sequence };
124
124
  } catch (error) {
125
- if (!(error instanceof StorageError && error.code === 'CONFLICT')) throw error;
125
+ if (!(error instanceof StorageError && error.storageCode === 'CONFLICT')) throw error;
126
126
  const latest = await read(runId);
127
127
  return latest.sequence >= sequence ? latest : advance(runId, latest, sequence);
128
128
  }
@@ -10,7 +10,8 @@ export { createWorkflowFleetControl, compositeFleetTarget, graphFleetTarget, lif
10
10
  type WorkflowFleetControlOptions, type WorkflowFleetHoldReader, type WorkflowFleetHoldState, type WorkflowFleetSweepCursor,
11
11
  type WorkflowFleetSweepOutcome, type WorkflowFleetSweepReport, type WorkflowFleetTarget } from './fleet-control.js';
12
12
  export { createWorkflowLeadership, type WorkflowLeadership, type WorkflowLeadershipOptions, type WorkflowLeadershipState } from './leadership.js';
13
- export { coordinatorUnit, createWorkflowWorker, type WorkflowWorker, type WorkflowWorkerOptions, type WorkflowWorkerStatus,
13
+ export { coordinatorUnit, createWorkflowWorker, type WorkflowWorker, type WorkflowWorkerOnceOptions, type WorkflowWorkerOnceReport,
14
+ type WorkflowWorkerOptions, type WorkflowWorkerStatus,
14
15
  type WorkflowWorkerUnit } from './worker.js';
15
16
  export { createWorkflowHookRelay, workflowHookStages, type WorkflowHookRelay, type WorkflowHookRelayOptions, type WorkflowHookDelivery, type WorkflowHookEvent, type WorkflowHookStage, type WorkflowHooks, type WorkflowEventSource } from './hook-relay.js';
16
17
  export { inventoryWorkflowVersions, assertWorkflowVersionsRetained, compositeVersionTarget, type WorkflowVersionInventory, type WorkflowVersionInventoryOptions, type WorkflowVersionEntry, type WorkflowVersionTarget } from './versions.js';
@@ -61,7 +61,7 @@ export function createWorkflowLeadership(options: WorkflowLeadershipOptions): Wo
61
61
  })) as unknown as WorkflowLeadershipState;
62
62
  const guarded = async <T>(operation: () => Promise<T>): Promise<T> => {
63
63
  try { return await operation(); }
64
- catch (error) { if (error instanceof MayuraError || (error instanceof StorageError && error.code === 'CONFLICT')) throw error;
64
+ catch (error) { if (error instanceof MayuraError || (error instanceof StorageError && error.storageCode === 'CONFLICT')) throw error;
65
65
  throw new MayuraError('STORAGE_UNAVAILABLE', 'Leadership storage is unavailable.'); }
66
66
  };
67
67
  return Object.freeze<WorkflowLeadership>({
@@ -81,7 +81,7 @@ export function createWorkflowLeadership(options: WorkflowLeadershipOptions): Wo
81
81
  } else await store.update({ scope, id, expectedVersion: record.version, state: jsonValue(next) as JsonObject, events: [event] });
82
82
  confirmed = { fence: next.fence, expiresAtMs: next.expiresAtMs }; return publicState(next, true);
83
83
  } catch (error) {
84
- if (error instanceof StorageError && error.code === 'CONFLICT') continue;
84
+ if (error instanceof StorageError && error.storageCode === 'CONFLICT') continue;
85
85
  confirmed = null; throw new MayuraError('STORAGE_UNAVAILABLE', 'Leadership lease update could not be confirmed.');
86
86
  }
87
87
  }
@@ -94,7 +94,7 @@ export function createWorkflowLeadership(options: WorkflowLeadershipOptions): Wo
94
94
  if (!record || !lease || lease.holderId !== holderId || lease.fence !== held.fence) return;
95
95
  try { await store.update({ scope, id, expectedVersion: record.version, state: jsonValue({ ...lease, expiresAtMs: 0 }) as JsonObject,
96
96
  events: [{ type: 'leadership.released', data: { fence: lease.fence } }] }); return; }
97
- catch (error) { if (!(error instanceof StorageError && error.code === 'CONFLICT')) throw new MayuraError('STORAGE_UNAVAILABLE', 'Leadership release could not be confirmed.'); }
97
+ catch (error) { if (!(error instanceof StorageError && error.storageCode === 'CONFLICT')) throw new MayuraError('STORAGE_UNAVAILABLE', 'Leadership release could not be confirmed.'); }
98
98
  }
99
99
  },
100
100
  isLeader: () => confirmed !== null && now() < confirmed.expiresAtMs - Math.floor(leaseMs / 3),
@@ -1,5 +1,5 @@
1
1
  import { assertSchema, MayuraError, type InferInput, type InferOutput, type Schema } from '@mayura/core';
2
- import { workflowLifecycleManifest, type WorkflowLifecycleHumanKind, type WorkflowLifecycleManifest } from '@mayura/storage-contracts';
2
+ import { schemaDigest, workflowLifecycleManifest, type WorkflowLifecycleHumanKind, type WorkflowLifecycleManifest } from '@mayura/storage-contracts';
3
3
  import { assertTool, type AnyTool } from '@mayura/tools';
4
4
  import { digest, type Binding, type WorkflowNode } from './definition.js';
5
5
 
@@ -10,7 +10,11 @@ export interface WorkflowLifecycleHumanNode<S extends Schema = Schema> {
10
10
  readonly request: {
11
11
  readonly kind: WorkflowLifecycleHumanKind;
12
12
  readonly schemaId: string;
13
- readonly schemaDigest: string;
13
+ /**
14
+ * 64-hex SHA-256 of the response contract. Omit it to derive it from `response` when that validator can describe
15
+ * itself as JSON Schema (Zod 4.2 and later can); see `schemaDigest`.
16
+ */
17
+ readonly schemaDigest?: string;
14
18
  readonly prompt: string;
15
19
  readonly response: S;
16
20
  readonly context?: Binding;
@@ -28,13 +32,33 @@ export interface WorkflowLifecycleTimerNode {
28
32
  readonly when?: Binding;
29
33
  }
30
34
 
35
+ /**
36
+ * Waits, without holding a worker, for one signal delivered from outside the run: from code with the runtime's
37
+ * `signal`, or over the operator API (`client.signalWorkflow`, `mayura workflow-signal`). The payload is validated
38
+ * with `payload` and becomes the step's output, which later steps bind to like any step output. Delivery is
39
+ * idempotent on the signal id. A signal that arrives before the step starts is kept and completes the step when it
40
+ * starts. With `deadlineAtMs`, a step that has no signal by then is `timed_out`, and the run fails.
41
+ */
42
+ export interface WorkflowLifecycleSignalNode<S extends Schema = Schema> {
43
+ readonly kind: 'signal';
44
+ readonly id: string;
45
+ readonly dependsOn?: readonly string[];
46
+ /** The name senders address, unique among the definition's signal nodes. Default: the node id. */
47
+ readonly name?: string;
48
+ readonly payload: S;
49
+ /** Binding to an absolute Unix epoch millisecond deadline. */
50
+ readonly deadlineAtMs?: Binding;
51
+ readonly when?: Binding;
52
+ }
53
+
31
54
  /**
32
55
  * A lifecycle node. Any node may declare `when`, a binding over the input or a dependency's output: the node runs
33
56
  * only when it resolves to a value other than `null` or `false` (a path that does not resolve counts as `null`).
34
57
  * Otherwise the step is `bypassed`: it is never admitted, reserves and costs nothing, and dependents see its output
35
58
  * as `null`. A `step` binding must name one of the node's dependencies.
36
59
  */
37
- export type WorkflowLifecycleNode = (WorkflowNode & { readonly when?: Binding }) | WorkflowLifecycleHumanNode | WorkflowLifecycleTimerNode;
60
+ export type WorkflowLifecycleNode = (WorkflowNode & { readonly when?: Binding }) | WorkflowLifecycleHumanNode | WorkflowLifecycleTimerNode
61
+ | WorkflowLifecycleSignalNode;
38
62
 
39
63
  export interface WorkflowLifecycleOptions<I extends Schema, O extends Schema> {
40
64
  readonly id: string;
@@ -76,7 +100,16 @@ export function assertWorkflowLifecycle(definition: AnyWorkflowLifecycle): void
76
100
  if (!definitions.has(definition)) throw new MayuraError('INVALID_CONFIG', 'Use defineWorkflowLifecycle from this package instance.');
77
101
  }
78
102
 
79
- /** Build the strict data-only format-5 material; response validators are never persisted. */
103
+ /** The schema digest of a human node that does not give one, derived from its response validator. */
104
+ function derivedDigest(node: WorkflowLifecycleHumanNode): string {
105
+ try { return schemaDigest(node.request.response); }
106
+ catch {
107
+ throw new MayuraError('INVALID_CONFIG', `Human step "${node.id}" has no schemaDigest, and its response validator cannot describe itself as JSON Schema. `
108
+ + 'Give request.schemaDigest (64 hex characters), for example schemaDigest(jsonSchemaOfYourResponse).');
109
+ }
110
+ }
111
+
112
+ /** Build the strict data-only format-5 material; response and payload validators are never persisted. */
80
113
  export function lifecycleManifest(
81
114
  definition: Pick<AnyWorkflowLifecycle, 'id' | 'version' | 'nodes' | 'result'>,
82
115
  ): WorkflowLifecycleManifest {
@@ -95,9 +128,11 @@ export function lifecycleManifest(
95
128
  };
96
129
  if (node.kind === 'join') return { kind: node.kind, id: node.id, dependsOn: node.dependsOn, ...when };
97
130
  if (node.kind === 'timer') return { kind: node.kind, id: node.id, dependsOn: node.dependsOn ?? [], fireAtMs: node.fireAtMs, ...when };
131
+ if (node.kind === 'signal') return { kind: node.kind, id: node.id, dependsOn: node.dependsOn ?? [], name: node.name ?? node.id,
132
+ deadlineAtMs: node.deadlineAtMs ?? null, ...when };
98
133
  if (node.kind === 'human') return {
99
134
  kind: node.kind, id: node.id, dependsOn: node.dependsOn ?? [], requestKind: node.request.kind,
100
- schemaId: node.request.schemaId, schemaDigest: node.request.schemaDigest, prompt: node.request.prompt,
135
+ schemaId: node.request.schemaId, schemaDigest: node.request.schemaDigest ?? derivedDigest(node), prompt: node.request.prompt,
101
136
  context: node.request.context ?? null, subjectDigest: node.request.subjectDigest ?? null,
102
137
  deadlineAtMs: node.request.deadlineAtMs ?? null, ...when,
103
138
  };
@@ -105,12 +140,13 @@ export function lifecycleManifest(
105
140
  }),
106
141
  result: definition.result,
107
142
  });
108
- } catch {
143
+ } catch (error) {
144
+ if (error instanceof MayuraError && error.code === 'INVALID_CONFIG') throw error;
109
145
  throw new MayuraError('INVALID_CONFIG', 'Workflow lifecycle metadata is invalid or exceeds its finite bounds.');
110
146
  }
111
147
  }
112
148
 
113
- /** Define an acyclic format-5 graph with explicit human and timer suspension nodes. */
149
+ /** Define an acyclic format-5 graph with explicit human, timer and signal suspension nodes. */
114
150
  export function defineWorkflowLifecycle<I extends Schema, O extends Schema>(
115
151
  options: WorkflowLifecycleOptions<I, O>,
116
152
  ): WorkflowLifecycleDefinition<I, O> {
@@ -119,13 +155,14 @@ export function defineWorkflowLifecycle<I extends Schema, O extends Schema>(
119
155
  }
120
156
  assertSchema(options.input); assertSchema(options.output);
121
157
  const tools = new Map<string, Extract<WorkflowLifecycleNode, { readonly kind: 'tool' }>['tool']>();
122
- const responses = new Map<string, Schema>();
158
+ const responses = new Map<string, Schema>(); const payloads = new Map<string, Schema>();
123
159
  for (const node of options.nodes) {
124
- if (!node || !['tool', 'join', 'human', 'timer'].includes(node.kind)) {
160
+ if (!node || !['tool', 'join', 'human', 'timer', 'signal'].includes(node.kind)) {
125
161
  throw new MayuraError('INVALID_CONFIG', 'Unknown workflow lifecycle node kind.');
126
162
  }
127
163
  if (node.kind === 'tool') { assertTool(node.tool); tools.set(node.id, node.tool); }
128
164
  if (node.kind === 'human') { assertSchema(node.request.response); responses.set(node.id, snapshot(node.request.response)); }
165
+ if (node.kind === 'signal') { assertSchema(node.payload); payloads.set(node.id, snapshot(node.payload)); }
129
166
  }
130
167
  const manifest = lifecycleManifest(options);
131
168
  const nodes = Object.freeze(manifest.graph.map(node => {
@@ -138,6 +175,10 @@ export function defineWorkflowLifecycle<I extends Schema, O extends Schema>(
138
175
  if (node.kind === 'timer') return Object.freeze({
139
176
  kind: node.kind, id: node.id, dependsOn: node.dependsOn, fireAtMs: node.fireAtMs, ...when,
140
177
  });
178
+ if (node.kind === 'signal') return Object.freeze({
179
+ kind: node.kind, id: node.id, dependsOn: node.dependsOn, name: node.name, payload: payloads.get(node.id)!,
180
+ ...(node.deadlineAtMs === null ? {} : { deadlineAtMs: node.deadlineAtMs }), ...when,
181
+ });
141
182
  return Object.freeze({
142
183
  kind: node.kind, id: node.id, dependsOn: node.dependsOn, ...when,
143
184
  request: Object.freeze({
@@ -129,13 +129,9 @@ function stateFrom(record: StoredRecord, scope: string, shard: string, maximum:
129
129
  return { format: 1, shard, entries };
130
130
  } catch { throw new MayuraError('CONFLICT', 'Lifecycle fleet index failed integrity validation.'); }
131
131
  }
132
+ /** Storage failures are MayuraErrors too (a full queue arrives as LIMIT_EXCEEDED); anything else is unavailable storage. */
132
133
  function failure(error: unknown): ErrorCode {
133
- if (error instanceof MayuraError) return error.code;
134
- if (error instanceof StorageError) {
135
- if (error.code === 'QUEUE_FULL' || error.code === 'LIMIT_EXCEEDED') return 'LIMIT_EXCEEDED';
136
- if (['INVALID_INPUT', 'CONFLICT', 'NOT_FOUND'].includes(error.code)) return error.code as 'INVALID_INPUT' | 'CONFLICT' | 'NOT_FOUND';
137
- }
138
- return 'STORAGE_UNAVAILABLE';
134
+ return error instanceof MayuraError ? error.code : 'STORAGE_UNAVAILABLE';
139
135
  }
140
136
 
141
137
  /** Durable sharded discovery over the base aggregate contract; the caller still owns storage lifecycle and scheduling. */
@@ -207,7 +203,7 @@ export function createWorkflowLifecycleFleetRuntime(options: WorkflowLifecycleFl
207
203
  state: jsonValue(current.state) as JsonObject, events: [{ type: terminal.has(snapshot.status) ? 'lifecycle-index.removed' : 'lifecycle-index.updated',
208
204
  data: { runId: snapshot.id, version: snapshot.version, status: snapshot.status } }] }); return;
209
205
  } catch (error) {
210
- if (error instanceof StorageError && error.code === 'CONFLICT') continue;
206
+ if (error instanceof StorageError && error.storageCode === 'CONFLICT') continue;
211
207
  throw new MayuraError('STORAGE_UNAVAILABLE', 'Lifecycle fleet index update could not be confirmed.');
212
208
  }
213
209
  }
@@ -240,7 +236,7 @@ export function createWorkflowLifecycleFleetRuntime(options: WorkflowLifecycleFl
240
236
  await store.update({ scope, id: current.record.id, expectedVersion: current.record.version, state: jsonValue({ format: 1, shard, entries: retained }) as JsonObject,
241
237
  events: [{ type: 'lifecycle-settled.recorded', data: { runId: snapshot.id, status: snapshot.status } }] }); return;
242
238
  } catch (error) {
243
- if (error instanceof StorageError && error.code === 'CONFLICT') continue;
239
+ if (error instanceof StorageError && error.storageCode === 'CONFLICT') continue;
244
240
  throw new MayuraError('STORAGE_UNAVAILABLE', 'Lifecycle settled index update could not be confirmed.');
245
241
  }
246
242
  }
@@ -317,6 +313,9 @@ export function createWorkflowLifecycleFleetRuntime(options: WorkflowLifecycleFl
317
313
  respondVerified: async (definition: AnyWorkflowLifecycle, command: Parameters<WorkflowLifecycleRuntime['respondVerified']>[1]) => {
318
314
  const snapshot = await runtime.respondVerified(definition, command); await write(snapshot, definition.digest); return snapshot;
319
315
  },
316
+ signal: async (definition: AnyWorkflowLifecycle, command: Parameters<WorkflowLifecycleRuntime['signal']>[1]) => {
317
+ const snapshot = await runtime.signal(definition, command); await write(snapshot, definition.digest); return snapshot;
318
+ },
320
319
  pause: async (id: string) => { const snapshot = await runtime.pause(id); await write(snapshot); return snapshot; },
321
320
  resume: async (id: string) => { const snapshot = await runtime.resume(id); await write(snapshot); return snapshot; },
322
321
  cancel: async (id: string) => { const snapshot = await runtime.cancel(id); await write(snapshot); return snapshot; },
@@ -2,6 +2,7 @@ import { freezeJson, jsonValue, MayuraError, type ErrorCode } from '@mayura/core
2
2
  import type { WorkflowFleetHoldReader } from './fleet-control.js';
3
3
  import type { WorkflowDrainOptions, WorkflowDrainReport } from './drain.js';
4
4
  import { assertWorkflowLifecycle, type AnyWorkflowLifecycle } from './lifecycle-definition.js';
5
+ import { createSweepPosition } from './sweep-position.js';
5
6
  import { createWorkflowLifecycleFleetRuntime, type WorkflowLifecycleFleetCursor,
6
7
  type WorkflowLifecycleFleetOutcome, type WorkflowLifecycleFleetRuntime,
7
8
  type WorkflowLifecycleFleetRuntimeOptions } from './lifecycle-fleet.js';
@@ -65,6 +66,9 @@ export function createWorkflowLifecycleHost(options: WorkflowLifecycleHostOption
65
66
  if (maxBackoffMs < intervalMs) throw new MayuraError('INVALID_CONFIG', 'maxBackoffMs must be at least intervalMs.');
66
67
  const hold = options.hold; if (hold !== undefined && typeof hold?.isHeld !== 'function') throw new MayuraError('INVALID_CONFIG', 'A fleet hold reader requires isHeld().');
67
68
  const runtime = createWorkflowLifecycleFleetRuntime(options); let cursor: WorkflowLifecycleFleetCursor | null = null;
69
+ // The sweep continues where the last process left it, rather than starting over (see sweep-position.ts).
70
+ const position = createSweepPosition<WorkflowLifecycleFleetCursor>(options.store, { kind: 'lifecycle', scope: options.scope,
71
+ catalog: definitions.map(definition => definition.digest) });
68
72
  let cycles = 0; let consecutiveFailures = 0; let lastError: ErrorCode | null = null;
69
73
  let lastCycle: WorkflowLifecycleHostCycle | null = null; let controller: AbortController | undefined;
70
74
  let loop: Promise<void> | undefined; let inFlight: Promise<WorkflowLifecycleHostCycle> | undefined; let closed = false;
@@ -75,11 +79,23 @@ export function createWorkflowLifecycleHost(options: WorkflowLifecycleHostOption
75
79
  let pages = 0; let examined = 0; let shardReads = 0; const outcomes: WorkflowLifecycleFleetOutcome[] = [];
76
80
  // Fail closed: a hold that cannot be confirmed fails the cycle instead of driving runs.
77
81
  const held = hold ? await hold.isHeld() === true : false;
78
- if (!held) do {
79
- const report = await runtime.runPage(definitions, { cursor, limit: pageLimit, maxShardReads });
80
- pages += 1; examined += report.page.examined; shardReads += report.page.shardReads; outcomes.push(...report.outcomes);
81
- cursor = report.page.nextCursor;
82
- } while (cursor && pages < maxPages);
82
+ if (!held) {
83
+ const stored = await position.load(); let restored = false;
84
+ if (stored !== null && cursor === null) { cursor = stored; restored = true; }
85
+ do {
86
+ const page = (from: WorkflowLifecycleFleetCursor | null) => runtime.runPage(definitions, { cursor: from, limit: pageLimit, maxShardReads });
87
+ let report: Awaited<ReturnType<typeof page>>;
88
+ try { report = await page(cursor); } catch (error) {
89
+ // A stored position this host cannot use restarts the sweep instead of failing every cycle.
90
+ if (!restored || !(error instanceof MayuraError) || error.code !== 'INVALID_INPUT') throw error;
91
+ cursor = null; report = await page(cursor);
92
+ }
93
+ restored = false;
94
+ pages += 1; examined += report.page.examined; shardReads += report.page.shardReads; outcomes.push(...report.outcomes);
95
+ cursor = report.page.nextCursor;
96
+ } while (cursor && pages < maxPages);
97
+ await position.save(cursor);
98
+ }
83
99
  const completedSweep = !held && cursor === null; if (completedSweep) cursor = null;
84
100
  const result = freezeJson(jsonValue({ pages, examined, shardReads, outcomes, completedSweep, held })) as unknown as WorkflowLifecycleHostCycle;
85
101
  cycles += 1; consecutiveFailures = 0; lastError = null; lastCycle = result; return result;
@@ -1,6 +1,7 @@
1
1
  import { freezeJson, jsonValue, MayuraError, type JsonValue, type Scope } from '@mayura/core';
2
2
  import { digest } from './definition.js';
3
3
  import { assertWorkflowLifecycle, type AnyWorkflowLifecycle } from './lifecycle-definition.js';
4
+ import type { WorkflowLifecycleFleetCandidate, WorkflowLifecycleFleetRuntime } from './lifecycle-fleet.js';
4
5
  import type { WorkflowLifecycleHumanRequest, WorkflowLifecycleRuntime } from './lifecycle-runtime.js';
5
6
 
6
7
  export interface WorkflowLifecycleHumanTransportRecord {
@@ -34,20 +35,37 @@ export interface WorkflowLifecycleHumanRegistration {
34
35
  readonly runId: string;
35
36
  }
36
37
 
38
+ /** A definition whose human requests a durable transport serves, and the agent whose callers may see and answer them. */
39
+ export interface WorkflowLifecycleHumanRoute {
40
+ readonly agentId: string;
41
+ readonly definition: AnyWorkflowLifecycle;
42
+ }
43
+
37
44
  export interface WorkflowLifecycleHumanTransportController {
38
45
  /** Exact three-callback object accepted by @mayura/server. */
39
46
  readonly transport: WorkflowLifecycleHumanTransport;
47
+ /** Registration mode only: serve one run's human requests. A durable transport finds them itself and refuses this. */
40
48
  register(input: WorkflowLifecycleHumanRegistration): readonly string[];
41
49
  unregister(runId: string): void;
42
50
  }
43
51
 
44
52
  export interface WorkflowLifecycleHumanTransportOptions {
45
53
  readonly scope: Scope;
54
+ /**
55
+ * Durable discovery: the fleet runtime (or host runtime) whose index lists the active runs of `scope`. Pending
56
+ * requests are then found in storage: nothing is registered, and a restart loses nothing. Requires `definitions`.
57
+ */
58
+ readonly runtime?: WorkflowLifecycleFleetRuntime;
59
+ /** With `runtime`: every definition version whose requests this transport serves (at most 128), each with its agent. */
60
+ readonly definitions?: readonly WorkflowLifecycleHumanRoute[];
61
+ /** Registration mode: the most registered requests (default 512). */
46
62
  readonly maxEntries?: number;
63
+ /** The most requests one page reads (default 256). */
47
64
  readonly maxExaminedPerPage?: number;
48
65
  }
49
66
 
50
- interface Entry extends WorkflowLifecycleHumanRegistration { readonly id: string; readonly nodeId: string }
67
+ interface Entry { readonly id: string; readonly agentId: string; readonly definition: AnyWorkflowLifecycle; readonly runtime: WorkflowLifecycleRuntime;
68
+ readonly runId: string; readonly nodeId: string }
51
69
  const identifier = /^[A-Za-z0-9][A-Za-z0-9._/-]{0,127}$/;
52
70
  const hashPattern = /^[a-f0-9]{64}$/;
53
71
 
@@ -56,8 +74,20 @@ function active(signal: AbortSignal): void {
56
74
  }
57
75
 
58
76
  /**
59
- * Bridges registered format-5 runs to the authenticated server transport shape.
60
- * Registration is intentionally explicit until durable lifecycle fleet discovery exists.
77
+ * A request's opaque route id. It starts with its run's two-hex index shard, so a durable transport finds a request
78
+ * by reading one shard, and pages in id order shard by shard.
79
+ */
80
+ function routeId(agentId: string, definitionHash: string, runId: string, nodeId: string): string {
81
+ return runId.slice(0, 2) + digest('mayura:workflow-lifecycle-human-route:v2', { agentId, definitionHash, runId, nodeId }).slice(2);
82
+ }
83
+
84
+ /**
85
+ * Serves format-5 human requests to the agent server's `humanRequests` transport (list, inspect, respond).
86
+ *
87
+ * With `runtime` and `definitions` it is durable: requests are discovered from the lifecycle fleet index, so they are
88
+ * listed, read and answered after a restart without registering anything. Without them, each run is registered with
89
+ * `register` and forgotten on restart. Either way, a caller sees a request only in the transport's exact scope and
90
+ * only when its identity lists the request's agent, and an answer is recorded as the verified caller's.
61
91
  */
62
92
  export function createWorkflowLifecycleHumanTransport(
63
93
  options: WorkflowLifecycleHumanTransportOptions,
@@ -69,7 +99,23 @@ export function createWorkflowLifecycleHumanTransport(
69
99
  || !Number.isSafeInteger(maxExaminedPerPage) || maxExaminedPerPage < 1 || maxExaminedPerPage > 1_024) {
70
100
  throw new MayuraError('INVALID_CONFIG', 'Lifecycle human transport limits are invalid.');
71
101
  }
72
- const entries = new Map<string, Entry>();
102
+ const durable = options.runtime !== undefined || options.definitions !== undefined;
103
+ const routes = new Map<string, WorkflowLifecycleHumanRoute>();
104
+ if (durable) {
105
+ const runtime = options.runtime;
106
+ if (!runtime || runtime.profile !== 'lifecycle-v1' || typeof runtime.scan !== 'function' || !Array.isArray(options.definitions)
107
+ || options.definitions.length < 1 || options.definitions.length > 128) {
108
+ throw new MayuraError('INVALID_CONFIG', 'A durable human transport needs the lifecycle fleet runtime and 1–128 definitions, each with its agentId.');
109
+ }
110
+ for (const route of options.definitions) {
111
+ assertWorkflowLifecycle(route?.definition);
112
+ if (typeof route.agentId !== 'string' || !identifier.test(route.agentId)) throw new MayuraError('INVALID_CONFIG', 'Each human transport definition needs a valid agentId.');
113
+ if (routes.has(route.definition.digest)) throw new MayuraError('INVALID_CONFIG', 'Human transport definitions must be unique.');
114
+ routes.set(route.definition.digest, Object.freeze({ agentId: route.agentId, definition: route.definition }));
115
+ }
116
+ }
117
+ const scopeKey = digest('mayura:scope:v1', scope);
118
+ const registered = new Map<string, Entry>();
73
119
  const exactScope = (candidate: Scope): void => {
74
120
  if (candidate?.principalId !== scope.principalId || candidate?.projectId !== scope.projectId) {
75
121
  throw new MayuraError('PERMISSION_DENIED', 'Lifecycle human transport scope does not match.');
@@ -79,7 +125,6 @@ export function createWorkflowLifecycleHumanTransport(
79
125
  if (!Array.isArray(agentIds) || agentIds.length > 256 || agentIds.some(id => !identifier.test(id))
80
126
  || new Set(agentIds).size !== agentIds.length) throw new MayuraError('INVALID_INPUT', 'Lifecycle human transport agent visibility is invalid.');
81
127
  };
82
- const visible = (entry: Entry, agentIds: readonly string[]): boolean => agentIds.includes(entry.agentId);
83
128
  const record = (entry: Entry, request: WorkflowLifecycleHumanRequest): WorkflowLifecycleHumanTransportRecord =>
84
129
  freezeJson(jsonValue({ id: entry.id, agentId: entry.agentId, kind: request.kind, schemaId: request.schemaId,
85
130
  schemaDigest: request.schemaDigest, prompt: request.prompt, digest: request.digest,
@@ -97,6 +142,54 @@ export function createWorkflowLifecycleHumanTransport(
97
142
  throw error;
98
143
  }
99
144
  };
145
+ /** The human requests of the active runs in one index shard, in route id order. */
146
+ const shardEntries = async (shard: number): Promise<Entry[]> => {
147
+ const runtime = options.runtime!; const candidates: WorkflowLifecycleFleetCandidate[] = []; let afterId = '';
148
+ for (let reads = 0; reads < 8; reads++) {
149
+ let page;
150
+ try { page = await runtime.scan({ cursor: { format: 1, scope: scopeKey, shard, afterId }, limit: 128, maxShardReads: 1 }); }
151
+ catch (error) {
152
+ // The fleet runtime refuses a cursor for another scope: the transport and runtime serve different scopes.
153
+ if (error instanceof MayuraError && error.code === 'INVALID_INPUT') throw new MayuraError('INVALID_CONFIG', 'The human transport and its lifecycle runtime serve different scopes.');
154
+ throw error;
155
+ }
156
+ candidates.push(...page.candidates);
157
+ if (!page.nextCursor || page.nextCursor.shard !== shard) break;
158
+ afterId = page.nextCursor.afterId;
159
+ }
160
+ return candidates.flatMap(candidate => {
161
+ const route = routes.get(candidate.definitionHash); if (!route) return [];
162
+ return route.definition.nodes.filter(node => node.kind === 'human').map(node => Object.freeze({
163
+ id: routeId(route.agentId, candidate.definitionHash, candidate.runId, node.id), agentId: route.agentId, definition: route.definition,
164
+ runtime, runId: candidate.runId, nodeId: node.id }));
165
+ }).sort((left, right) => left.id.localeCompare(right.id));
166
+ };
167
+ const find = async (id: string): Promise<Entry | undefined> => durable
168
+ ? (await shardEntries(Number.parseInt(id.slice(0, 2), 16))).find(entry => entry.id === id)
169
+ : registered.get(id);
170
+ const page = async (agentIds: readonly string[], after: string | null, limit: number, signal: AbortSignal): Promise<{ items: WorkflowLifecycleHumanTransportRecord[]; next: string | null }> => {
171
+ const items: WorkflowLifecycleHumanTransportRecord[] = []; let examined = 0; let cursor: string | null = null;
172
+ const visible = (entry: Entry): boolean => agentIds.includes(entry.agentId) && (after === null || entry.id > after);
173
+ if (!durable) {
174
+ const ordered = [...registered.values()].filter(visible).sort((left, right) => left.id.localeCompare(right.id));
175
+ for (const entry of ordered) {
176
+ if (items.length >= limit || examined >= maxExaminedPerPage) break;
177
+ active(signal); examined += 1; cursor = entry.id;
178
+ const item = await requestFor(entry); if (item) items.push(item);
179
+ }
180
+ return { items, next: cursor !== null && ordered.some(entry => entry.id > cursor!) ? cursor : null };
181
+ }
182
+ // Route ids start with their shard, so walking shards in order walks ids in order.
183
+ for (let shard = after === null ? 0 : Number.parseInt(after.slice(0, 2), 16); shard < 256; shard++) {
184
+ active(signal);
185
+ for (const entry of (await shardEntries(shard)).filter(visible)) {
186
+ if (items.length >= limit || examined >= maxExaminedPerPage) return { items, next: cursor };
187
+ active(signal); examined += 1; cursor = entry.id;
188
+ const item = await requestFor(entry); if (item) items.push(item);
189
+ }
190
+ }
191
+ return { items, next: null };
192
+ };
100
193
  const transport = Object.freeze<WorkflowLifecycleHumanTransport>({
101
194
  list: async input => {
102
195
  exactScope(input.scope); active(input.signal);
@@ -104,22 +197,14 @@ export function createWorkflowLifecycleHumanTransport(
104
197
  if ((input.after !== null && !hashPattern.test(input.after)) || !Number.isSafeInteger(input.limit) || input.limit < 1 || input.limit > 100) {
105
198
  throw new MayuraError('INVALID_INPUT', 'Lifecycle human request page is invalid.');
106
199
  }
107
- const ordered = [...entries.values()].filter(entry => visible(entry, input.agentIds) && (input.after === null || entry.id > input.after))
108
- .sort((left, right) => left.id.localeCompare(right.id));
109
- const items: WorkflowLifecycleHumanTransportRecord[] = []; let examined = 0; let cursor: string | null = null;
110
- for (const entry of ordered) {
111
- if (items.length >= input.limit || examined >= maxExaminedPerPage) break;
112
- active(input.signal); examined += 1; cursor = entry.id;
113
- const item = await requestFor(entry); if (item) items.push(item);
114
- }
115
- const hasMore = cursor !== null && ordered.some(entry => entry.id > cursor!);
116
- active(input.signal); return freezeJson(jsonValue({ items, next: hasMore ? cursor : null })) as unknown as { readonly items: readonly WorkflowLifecycleHumanTransportRecord[]; readonly next: string | null };
200
+ const result = await page(input.agentIds, input.after, input.limit, input.signal);
201
+ active(input.signal); return freezeJson(jsonValue(result)) as unknown as { readonly items: readonly WorkflowLifecycleHumanTransportRecord[]; readonly next: string | null };
117
202
  },
118
203
  inspect: async input => {
119
204
  exactScope(input.scope); active(input.signal);
120
205
  checkedAgents(input.agentIds);
121
206
  if (!hashPattern.test(input.id)) throw new MayuraError('INVALID_INPUT', 'Lifecycle human request identity is invalid.');
122
- const entry = entries.get(input.id); if (!entry || !visible(entry, input.agentIds)) return null;
207
+ const entry = await find(input.id); if (!entry || !input.agentIds.includes(entry.agentId)) return null;
123
208
  const item = await requestFor(entry); active(input.signal); return item;
124
209
  },
125
210
  respond: async input => {
@@ -129,8 +214,8 @@ export function createWorkflowLifecycleHumanTransport(
129
214
  || !identifier.test(input.actorId)) {
130
215
  throw new MayuraError('INVALID_INPUT', 'Lifecycle human response identity is invalid.');
131
216
  }
132
- const entry = entries.get(input.id);
133
- if (!entry || !visible(entry, input.agentIds)) throw new MayuraError('NOT_FOUND', 'Lifecycle human request was not found.');
217
+ const entry = await find(input.id);
218
+ if (!entry || !input.agentIds.includes(entry.agentId)) throw new MayuraError('NOT_FOUND', 'Lifecycle human request was not found.');
134
219
  await entry.runtime.respondVerified(entry.definition, { id: entry.runId, nodeId: entry.nodeId,
135
220
  requestDigest: input.requestDigest, commandId: input.commandId,
136
221
  actor: { id: input.actorId, projectId: scope.projectId }, value: input.value });
@@ -142,29 +227,28 @@ export function createWorkflowLifecycleHumanTransport(
142
227
  return Object.freeze<WorkflowLifecycleHumanTransportController>({
143
228
  transport,
144
229
  register: input => {
230
+ if (durable) throw new MayuraError('INVALID_CONFIG', 'This human transport finds requests in storage; runs need no registration.');
145
231
  assertWorkflowLifecycle(input.definition);
146
232
  if (!identifier.test(input.agentId) || !hashPattern.test(input.runId) || input.runtime?.profile !== 'lifecycle-v1') {
147
233
  throw new MayuraError('INVALID_INPUT', 'Lifecycle human registration is invalid.');
148
234
  }
149
235
  const nodes = input.definition.nodes.filter(node => node.kind === 'human');
150
- const ids = nodes.map(node => digest('mayura:workflow-lifecycle-human-route:v1', {
151
- agentId: input.agentId, definitionHash: input.definition.digest, runId: input.runId, nodeId: node.id,
152
- }));
153
- const additions = ids.filter(id => !entries.has(id)).length;
154
- if (entries.size + additions > maxEntries) throw new MayuraError('LIMIT_EXCEEDED', 'Lifecycle human transport registry is full.');
236
+ const ids = nodes.map(node => routeId(input.agentId, input.definition.digest, input.runId, node.id));
237
+ const additions = ids.filter(id => !registered.has(id)).length;
238
+ if (registered.size + additions > maxEntries) throw new MayuraError('LIMIT_EXCEEDED', 'Lifecycle human transport registry is full.');
155
239
  for (const [index, node] of nodes.entries()) {
156
- const id = ids[index]!; const previous = entries.get(id);
240
+ const id = ids[index]!; const previous = registered.get(id);
157
241
  if (previous && (previous.agentId !== input.agentId || previous.definition !== input.definition
158
242
  || previous.runtime !== input.runtime || previous.runId !== input.runId || previous.nodeId !== node.id)) {
159
243
  throw new MayuraError('CONFLICT', 'Lifecycle human route ID collision.');
160
244
  }
161
- entries.set(id, Object.freeze({ ...input, id, nodeId: node.id }));
245
+ registered.set(id, Object.freeze({ ...input, id, nodeId: node.id }));
162
246
  }
163
247
  return Object.freeze(ids);
164
248
  },
165
249
  unregister: runId => {
166
250
  if (!hashPattern.test(runId)) throw new MayuraError('INVALID_INPUT', 'Lifecycle run ID is invalid.');
167
- for (const [id, entry] of entries) if (entry.runId === runId) entries.delete(id);
251
+ for (const [id, entry] of registered) if (entry.runId === runId) registered.delete(id);
168
252
  },
169
253
  });
170
254
  }