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
@@ -1,9 +1,11 @@
1
1
  import { Budget, MayuraError, assertPositiveInteger, freezeJson, jsonValue, validate } from '../../core/dist/index.js';
2
2
  import { invokeTool } from '../../tools/dist/index.js';
3
+ import { abandoned, dispatchedAt } from './abandoned.js';
3
4
  import { createWorkflowDrainGate } from './drain.js';
4
5
  import { StorageError, assertWorkflowLifecycleStateMatchesManifest, initialWorkflowLifecycleState, mergeWorkflowReceipt, workflowLifecycleOutputs, workflowLifecycleState } from '../../storage-contracts/dist/index.js';
5
6
  import { charged, digest, resolveBinding } from './definition.js';
6
7
  import { assertWorkflowLifecycle, lifecycleManifest } from './lifecycle-definition.js';
8
+ import { createSubmission, workflowStorageFailure } from './storage-failure.js';
7
9
  import { assertMigrationAllowed, assertWorkflowMigration, migrationCommand, migrationEvent, nodeEvidence, nodeFingerprint, planWorkflowMigration } from './migration.js';
8
10
  const finalRunStatuses = new Set(['succeeded', 'failed', 'blocked', 'cancelled', 'outcome_unknown']);
9
11
  const terminalStepStatuses = new Set(['succeeded', 'failed', 'blocked', 'unknown', 'timed_out', 'skipped', 'bypassed']);
@@ -11,6 +13,7 @@ const terminalStepStatuses = new Set(['succeeded', 'failed', 'blocked', 'unknown
11
13
  const satisfiedStepStatuses = new Set(['succeeded', 'bypassed']);
12
14
  const hashPattern = /^[a-f0-9]{64}$/;
13
15
  const nodePattern = /^[A-Za-z][A-Za-z0-9._-]{0,127}$/;
16
+ const signalPattern = /^[A-Za-z0-9][A-Za-z0-9._/-]{0,127}$/;
14
17
  async function bounded(operation, timeoutMs) {
15
18
  let timer;
16
19
  try {
@@ -28,9 +31,7 @@ async function storageCall(operation) {
28
31
  return await operation();
29
32
  }
30
33
  catch (error) {
31
- if (error instanceof StorageError && error.code === 'CONFLICT')
32
- throw new StorageError('CONFLICT', 'Workflow lifecycle storage version changed.');
33
- throw new MayuraError('STORAGE_UNAVAILABLE', 'Workflow lifecycle storage is unavailable; reconcile uncertain actions before retrying.');
34
+ throw workflowStorageFailure(error, 'workflow lifecycle run');
34
35
  }
35
36
  }
36
37
  function stateFrom(record) {
@@ -59,7 +60,7 @@ function receipt(previous, incoming) {
59
60
  }
60
61
  function nextWake(state) {
61
62
  const deadlines = Object.values(state.steps).flatMap(step => step.kind === 'timer' && step.status === 'waiting' && step.fireAtMs !== null
62
- ? [step.fireAtMs] : step.kind === 'human' && step.status === 'waiting' && step.deadlineAtMs !== null
63
+ ? [step.fireAtMs] : (step.kind === 'human' || step.kind === 'signal') && step.status === 'waiting' && step.deadlineAtMs !== null
63
64
  ? [step.deadlineAtMs] : step.kind === 'tool' && step.status === 'waiting' && step.approval !== null ? [step.approval.expiresAt] : []);
64
65
  return deadlines.length === 0 ? null : Math.min(...deadlines);
65
66
  }
@@ -79,6 +80,56 @@ function exactDigest(value) {
79
80
  throw new MayuraError('INVALID_INPUT', 'Correction subject binding must resolve to a SHA-256 digest.');
80
81
  return value;
81
82
  }
83
+ /**
84
+ * @internal The runtime's own settings first, then each previous policy, validated and defaulted alike, so index `n`
85
+ * means the same settings to every runtime built from one options object.
86
+ */
87
+ export function workflowPolicySettings(options) {
88
+ const previous = options.previousPolicies ?? [];
89
+ if (!Array.isArray(previous) || previous.length > 16)
90
+ throw new MayuraError('INVALID_CONFIG', 'previousPolicies must list at most 16 policies.');
91
+ return Object.freeze([options, ...previous].map(value => {
92
+ if (value === null || typeof value !== 'object')
93
+ throw new MayuraError('INVALID_CONFIG', 'Each previous policy must be an explicit settings object.');
94
+ const policy = value;
95
+ if (typeof policy.policyVersion !== 'string' || policy.policyVersion.length < 1 || policy.policyVersion.length > 128) {
96
+ throw new MayuraError('INVALID_CONFIG', 'Workflow lifecycle policy version is required.');
97
+ }
98
+ if (!Array.isArray(policy.permissions?.allow) || policy.permissions.allow.length > 4_096
99
+ || policy.permissions.allow.some(grant => typeof grant !== 'string' || grant.length < 1 || grant.length > 256)) {
100
+ throw new MayuraError('INVALID_CONFIG', 'Workflow lifecycle permissions must be bounded explicit grants.');
101
+ }
102
+ if (!Number.isSafeInteger(policy.maxCostMicros) || policy.maxCostMicros < 0)
103
+ throw new MayuraError('INVALID_CONFIG', 'A bounded lifecycle cost is required.');
104
+ const maxOutputBytes = policy.maxOutputBytes ?? 1_048_576;
105
+ const approvalTtlMs = policy.approvalTtlMs ?? 3_600_000;
106
+ assertPositiveInteger(approvalTtlMs, 'approvalTtlMs');
107
+ assertPositiveInteger(maxOutputBytes, 'maxOutputBytes');
108
+ return Object.freeze({ permissions: Object.freeze({ allow: Object.freeze([...policy.permissions.allow]) }),
109
+ policyVersion: policy.policyVersion, maxCostMicros: policy.maxCostMicros, maxOutputBytes, approvalTtlMs });
110
+ }));
111
+ }
112
+ /** @internal The digest a run records for its settings; each runtime kind has its own domain tag. */
113
+ export function workflowPolicyDigest(tag, scope, settings) {
114
+ return digest(tag, { scope, permissions: [...settings.permissions.allow].sort(), policyVersion: settings.policyVersion,
115
+ maxCostMicros: settings.maxCostMicros, maxOutputBytes: settings.maxOutputBytes, approvalTtlMs: settings.approvalTtlMs });
116
+ }
117
+ /** @internal */
118
+ export function unknownWorkflowPolicy() {
119
+ return new MayuraError('CONFLICT', 'This run was started under different runtime settings (permissions, policyVersion or limits). '
120
+ + 'List those settings in previousPolicies to let it continue, or migrate it.');
121
+ }
122
+ const pinnedSubmissions = new WeakMap();
123
+ /**
124
+ * @internal Submit under the runtime's policy at `policy` (0: current, n: `previousPolicies[n - 1]`), so the later
125
+ * children of a pinned saga or loop keep the parent's settings instead of gaining the current ones.
126
+ */
127
+ export function submitWorkflowLifecycleUnder(runtime, policy, ...submission) {
128
+ const submit = pinnedSubmissions.get(runtime);
129
+ if (!submit)
130
+ throw new MayuraError('INVALID_CONFIG', 'A genuine workflow lifecycle runtime is required.');
131
+ return submit(policy, ...submission);
132
+ }
82
133
  /** Conservative durable lifecycle driver. Waiting nodes never retain a callback, worker, or timer handle. */
83
134
  export function createWorkflowLifecycleRuntime(options) {
84
135
  const store = options.store;
@@ -86,20 +137,9 @@ export function createWorkflowLifecycleRuntime(options) {
86
137
  if ([scope.principalId, scope.projectId, options.policyVersion].some(value => typeof value !== 'string' || value.length < 1 || value.length > 128)) {
87
138
  throw new MayuraError('INVALID_CONFIG', 'Workflow lifecycle scope and policy version are required.');
88
139
  }
89
- if (!Array.isArray(options.permissions?.allow) || options.permissions.allow.length > 4_096
90
- || options.permissions.allow.some(grant => typeof grant !== 'string' || grant.length < 1 || grant.length > 256)) {
91
- throw new MayuraError('INVALID_CONFIG', 'Workflow lifecycle permissions must be bounded explicit grants.');
92
- }
93
- if (!Number.isSafeInteger(options.maxCostMicros) || options.maxCostMicros < 0)
94
- throw new MayuraError('INVALID_CONFIG', 'A bounded lifecycle cost is required.');
95
- const permissions = Object.freeze({ allow: Object.freeze([...options.permissions.allow]) });
96
- const maxCostMicros = options.maxCostMicros;
97
- const approvalTtlMs = options.approvalTtlMs ?? 3_600_000;
98
- const maxOutputBytes = options.maxOutputBytes ?? 1_048_576;
140
+ const settings = workflowPolicySettings(options);
99
141
  const callbackTimeoutMs = options.callbackTimeoutMs ?? 30_000;
100
142
  const maxPendingCallbacks = options.maxPendingCallbacks ?? 32;
101
- assertPositiveInteger(approvalTtlMs, 'approvalTtlMs');
102
- assertPositiveInteger(maxOutputBytes, 'maxOutputBytes');
103
143
  assertPositiveInteger(callbackTimeoutMs, 'callbackTimeoutMs');
104
144
  assertPositiveInteger(maxPendingCallbacks, 'maxPendingCallbacks');
105
145
  if (maxPendingCallbacks > 128)
@@ -131,8 +171,12 @@ export function createWorkflowLifecycleRuntime(options) {
131
171
  return value;
132
172
  };
133
173
  const scopeKey = digest('mayura:scope:v1', scope);
134
- const policy = digest('mayura:workflow-lifecycle-policy:v1', { scope, permissions: [...permissions.allow].sort(),
135
- policyVersion: options.policyVersion, maxCostMicros, maxOutputBytes, approvalTtlMs });
174
+ const policies = settings.map(entry => Object.freeze({ ...entry, digest: workflowPolicyDigest('mayura:workflow-lifecycle-policy:v1', scope, entry) }));
175
+ const currentPolicy = policies[0];
176
+ const policy = currentPolicy.digest;
177
+ const maxCostMicros = currentPolicy.maxCostMicros;
178
+ /** The settings a stored run was started with; undefined when they are neither current nor listed. */
179
+ const pinnedTo = (state) => policies.find(entry => entry.digest === state.policy);
136
180
  const active = new Map();
137
181
  let closed = false;
138
182
  const gate = createWorkflowDrainGate();
@@ -163,7 +207,7 @@ export function createWorkflowLifecycleRuntime(options) {
163
207
  return await save(record, state, type, data);
164
208
  }
165
209
  catch (error) {
166
- if (!(error instanceof StorageError) || error.code !== 'CONFLICT')
210
+ if (!(error instanceof StorageError) || error.storageCode !== 'CONFLICT')
167
211
  throw error;
168
212
  }
169
213
  }
@@ -184,7 +228,8 @@ export function createWorkflowLifecycleRuntime(options) {
184
228
  throw new MayuraError('PERMISSION_DENIED', 'Human identity verification failed.');
185
229
  }
186
230
  };
187
- const approvalCandidate = (node, input, runId, expiresAt) => digest('mayura:approval:v1', { runId, nodeId: node.id,
231
+ // Bound to the run's own policy, so an approval issued before an upgrade stays valid for the run it was issued to.
232
+ const approvalCandidate = (node, input, runId, expiresAt, policy) => digest('mayura:approval:v1', { runId, nodeId: node.id,
188
233
  tool: node.tool.id, toolVersion: node.tool.version, input, policy, expiresAt });
189
234
  const schedulable = (state) => state.status !== 'paused' && state.status !== 'cancelled';
190
235
  const skip = (step) => {
@@ -208,13 +253,27 @@ export function createWorkflowLifecycleRuntime(options) {
208
253
  step.fireAtMs = null;
209
254
  step.firedAtMs = null;
210
255
  }
256
+ else if (step.kind === 'signal') {
257
+ step.status = 'skipped';
258
+ clearSignal(step);
259
+ step.deadlineAtMs = null;
260
+ }
211
261
  else
212
262
  step.status = 'skipped';
213
263
  return true;
214
264
  };
265
+ // A signal kept for a step that never runs is dropped with it.
266
+ const clearSignal = (step) => {
267
+ step.output = null;
268
+ step.signalId = null;
269
+ step.payloadDigest = null;
270
+ step.receivedAtMs = null;
271
+ };
215
272
  const bypass = (step) => {
216
273
  if (step.status !== 'pending')
217
274
  return false;
275
+ if (step.kind === 'signal')
276
+ clearSignal(step);
218
277
  step.status = 'bypassed';
219
278
  return true;
220
279
  };
@@ -235,10 +294,33 @@ export function createWorkflowLifecycleRuntime(options) {
235
294
  const record = await load(id);
236
295
  const state = stateFrom(record);
237
296
  const step = state.steps[node.id];
238
- if (!step || step.kind !== node.kind || state.policy !== policy || !schedulable(state))
297
+ // Every limit and grant below comes from the run's own policy, never from the current one.
298
+ const run = pinnedTo(state);
299
+ if (!step || step.kind !== node.kind || !run || !schedulable(state))
239
300
  return;
301
+ const { permissions, maxOutputBytes, approvalTtlMs } = run;
240
302
  // A pause or cancellation may commit after this read; every scheduling transition rechecks the latest state.
241
303
  const advance = (transition, type, data) => mutate(id, current => schedulable(current) && transition(current), type, data);
304
+ // A dispatching step that no call in this process owns may belong to a process that stopped (killed, or frozen
305
+ // past its limit). Once the tool's timeout and a margin have passed since the dispatch, no live process can still
306
+ // settle it, so it is settled as recoverAbandoned would: unknown, or blocked when a receipt shows it succeeded. The
307
+ // effect is never run again.
308
+ if (node.kind === 'tool' && step.kind === 'tool' && step.status === 'dispatching') {
309
+ if (active.has(`${id}/${node.id}`))
310
+ return;
311
+ const dispatchedAtMs = await storageCall(() => dispatchedAt(store, scopeKey, id, 'lifecycle.step.dispatching', node.id));
312
+ if (!abandoned(dispatchedAtMs, node.tool.timeoutMs, now()))
313
+ return;
314
+ await advance(current => {
315
+ const target = current.steps[node.id];
316
+ if (!target || target.kind !== 'tool' || target.status !== 'dispatching' || active.has(`${id}/${node.id}`))
317
+ return false;
318
+ target.status = target.receipt?.execution === 'succeeded' ? 'blocked' : 'unknown';
319
+ current.status = 'running';
320
+ return true;
321
+ }, 'lifecycle.step.abandoned', { nodeId: node.id });
322
+ return;
323
+ }
242
324
  const dependencies = (node.dependsOn ?? []).map(key => state.steps[key]);
243
325
  if (dependencies.some(item => terminalStepStatuses.has(item.status) && !satisfiedStepStatuses.has(item.status))) {
244
326
  await advance(current => skip(current.steps[node.id]), 'lifecycle.step.skipped', { nodeId: node.id });
@@ -299,6 +381,51 @@ export function createWorkflowLifecycleRuntime(options) {
299
381
  }, deadlineAtMs !== null && observedAtMs >= deadlineAtMs ? 'lifecycle.human.timed_out' : 'lifecycle.human.requested', { nodeId: node.id, requestDigest });
300
382
  return;
301
383
  }
384
+ if (node.kind === 'signal') {
385
+ if (step.kind !== 'signal' || !['pending', 'waiting'].includes(step.status))
386
+ return;
387
+ if (step.status === 'waiting') {
388
+ const observedAtMs = now();
389
+ if (step.deadlineAtMs === null || observedAtMs < step.deadlineAtMs)
390
+ return;
391
+ await advance(current => {
392
+ const target = current.steps[node.id];
393
+ if (!target || target.kind !== 'signal' || target.status !== 'waiting' || target.deadlineAtMs === null || observedAtMs < target.deadlineAtMs)
394
+ return false;
395
+ target.status = 'timed_out';
396
+ current.status = 'running';
397
+ return true;
398
+ }, 'lifecycle.signal.timed_out', { nodeId: node.id, observedAtMs });
399
+ return;
400
+ }
401
+ let deadlineAtMs = null;
402
+ try {
403
+ if (node.deadlineAtMs)
404
+ deadlineAtMs = exactTimestamp(resolveBinding(node.deadlineAtMs, state.input, outputs(state)));
405
+ }
406
+ catch {
407
+ await advance(current => skip(current.steps[node.id]), 'lifecycle.signal.invalid', { nodeId: node.id });
408
+ return;
409
+ }
410
+ const observedAtMs = now();
411
+ // A signal kept from before the step started counts if it arrived before the deadline.
412
+ const decide = (target) => target.signalId !== null ? (deadlineAtMs === null || target.receivedAtMs < deadlineAtMs ? 'succeeded' : 'timed_out')
413
+ : deadlineAtMs !== null && observedAtMs >= deadlineAtMs ? 'timed_out' : 'waiting';
414
+ const next = decide(step);
415
+ await advance(current => {
416
+ const target = current.steps[node.id];
417
+ // A signal delivered since the read changes the decision: leave it to the next wave.
418
+ if (!target || target.kind !== 'signal' || target.status !== 'pending' || decide(target) !== next)
419
+ return false;
420
+ target.deadlineAtMs = deadlineAtMs;
421
+ target.status = next;
422
+ if (next === 'timed_out')
423
+ clearSignal(target);
424
+ current.status = next === 'waiting' ? 'waiting' : 'running';
425
+ return true;
426
+ }, next === 'succeeded' ? 'lifecycle.signal.received' : `lifecycle.signal.${next}`, { nodeId: node.id });
427
+ return;
428
+ }
302
429
  if (node.kind === 'timer') {
303
430
  if (step.kind !== 'timer' || !['pending', 'waiting'].includes(step.status))
304
431
  return;
@@ -355,7 +482,7 @@ export function createWorkflowLifecycleRuntime(options) {
355
482
  await save(record, state, 'lifecycle.step.completed', { nodeId: node.id });
356
483
  }
357
484
  catch (error) {
358
- if (!(error instanceof StorageError) || error.code !== 'CONFLICT')
485
+ if (!(error instanceof StorageError) || error.storageCode !== 'CONFLICT')
359
486
  throw error;
360
487
  }
361
488
  return;
@@ -370,7 +497,7 @@ export function createWorkflowLifecycleRuntime(options) {
370
497
  await save(record, state, 'lifecycle.step.blocked', { nodeId: node.id, code: 'PERMISSION_DENIED' });
371
498
  }
372
499
  catch (error) {
373
- if (!(error instanceof StorageError) || error.code !== 'CONFLICT')
500
+ if (!(error instanceof StorageError) || error.storageCode !== 'CONFLICT')
374
501
  throw error;
375
502
  }
376
503
  return;
@@ -385,7 +512,7 @@ export function createWorkflowLifecycleRuntime(options) {
385
512
  await save(record, state, 'lifecycle.step.failed', { nodeId: node.id, code: 'INVALID_INPUT' });
386
513
  }
387
514
  catch (error) {
388
- if (!(error instanceof StorageError) || error.code !== 'CONFLICT')
515
+ if (!(error instanceof StorageError) || error.storageCode !== 'CONFLICT')
389
516
  throw error;
390
517
  }
391
518
  return;
@@ -393,7 +520,7 @@ export function createWorkflowLifecycleRuntime(options) {
393
520
  const observedAtMs = now();
394
521
  const previousExpiry = step.approval?.expiresAt ?? 0;
395
522
  const reviewExpiry = node.approval ? (previousExpiry > observedAtMs ? previousExpiry : observedAtMs + approvalTtlMs) : null;
396
- const candidateHash = approvalCandidate(node, input, id, reviewExpiry);
523
+ const candidateHash = approvalCandidate(node, input, id, reviewExpiry, run.digest);
397
524
  if (node.approval && (step.status !== 'approved' || step.approval?.digest !== candidateHash || step.approval.expiresAt <= observedAtMs)) {
398
525
  step.status = 'waiting';
399
526
  step.approval = { digest: candidateHash, expiresAt: reviewExpiry, humanId: null };
@@ -402,7 +529,7 @@ export function createWorkflowLifecycleRuntime(options) {
402
529
  await save(record, state, 'lifecycle.approval.requested', { nodeId: node.id, digest: candidateHash });
403
530
  }
404
531
  catch (error) {
405
- if (!(error instanceof StorageError) || error.code !== 'CONFLICT')
532
+ if (!(error instanceof StorageError) || error.storageCode !== 'CONFLICT')
406
533
  throw error;
407
534
  }
408
535
  return;
@@ -429,7 +556,7 @@ export function createWorkflowLifecycleRuntime(options) {
429
556
  await save(record, state, 'lifecycle.step.dispatching', { nodeId: node.id, callId: step.callId });
430
557
  }
431
558
  catch (error) {
432
- if (error instanceof StorageError && error.code === 'CONFLICT' && claimRetries < 32)
559
+ if (error instanceof StorageError && error.storageCode === 'CONFLICT' && claimRetries < 32)
433
560
  return executeNode(id, definition, node, claimRetries + 1);
434
561
  throw error;
435
562
  }
@@ -443,8 +570,8 @@ export function createWorkflowLifecycleRuntime(options) {
443
570
  beforeDispatch: async (processed) => {
444
571
  const current = stateFrom(await load(id));
445
572
  const target = current.steps[node.id];
446
- if (!target || target.kind !== 'tool' || current.status !== 'running' || current.policy !== policy
447
- || target.status !== 'dispatching' || approvalCandidate(node, processed, id, node.approval ? target.approval.expiresAt : null) !== candidateHash)
573
+ if (!target || target.kind !== 'tool' || current.status !== 'running' || current.policy !== run.digest
574
+ || target.status !== 'dispatching' || approvalCandidate(node, processed, id, node.approval ? target.approval.expiresAt : null, run.digest) !== candidateHash)
448
575
  throw new MayuraError('CONFLICT', 'Lifecycle dispatch candidate is no longer authorized.');
449
576
  if (node.approval && target.approval.expiresAt <= now())
450
577
  throw new MayuraError('PERMISSION_DENIED', 'Approval expired before dispatch.');
@@ -511,18 +638,22 @@ export function createWorkflowLifecycleRuntime(options) {
511
638
  schemaDigest: node.request.schemaDigest, prompt: node.request.prompt, context, subjectDigest, deadlineAtMs });
512
639
  return { requestDigest, deadlineAtMs, context, subjectDigest };
513
640
  };
514
- const verifyDefinition = (definition, record, state) => {
641
+ function verifyDefinition(definition, record, state, anyPolicy = false) {
515
642
  assertWorkflowLifecycle(definition);
516
- if (record.definitionHash !== definition.digest || state.definition !== definition.digest || state.policy !== policy
517
- || state.maxCostMicros !== maxCostMicros)
518
- throw new MayuraError('CONFLICT', 'Lifecycle definition or policy changed; explicit migration/review is required.');
643
+ if (record.definitionHash !== definition.digest || state.definition !== definition.digest) {
644
+ throw new MayuraError('CONFLICT', 'Lifecycle definition changed; explicit migration/review is required.');
645
+ }
646
+ const run = pinnedTo(state);
647
+ if (run ? state.maxCostMicros !== run.maxCostMicros : !anyPolicy)
648
+ throw unknownWorkflowPolicy();
519
649
  try {
520
650
  assertWorkflowLifecycleStateMatchesManifest(state, lifecycleManifest(definition));
521
651
  }
522
652
  catch {
523
653
  throw new MayuraError('CONFLICT', 'Stored lifecycle steps do not match the pinned definition.');
524
654
  }
525
- };
655
+ return run;
656
+ }
526
657
  const respondAs = async (definition, command, actor) => {
527
658
  ensureOpen();
528
659
  assertWorkflowLifecycle(definition);
@@ -537,14 +668,14 @@ export function createWorkflowLifecycleRuntime(options) {
537
668
  if (!node || node.kind !== 'human')
538
669
  throw new MayuraError('INVALID_INPUT', 'Human response node is not part of the lifecycle definition.');
539
670
  const before = await load(command.id);
540
- verifyDefinition(definition, before, stateFrom(before));
541
- const value = jsonValue(await controlled(() => validate(node.request.response, command.value, 'input')), { maxBytes: maxOutputBytes });
671
+ const run = verifyDefinition(definition, before, stateFrom(before));
672
+ const value = jsonValue(await controlled(() => validate(node.request.response, command.value, 'input')), { maxBytes: run.maxOutputBytes });
542
673
  const responseDigest = digest('mayura:human-response:v1', { requestDigest: command.requestDigest,
543
674
  commandId: command.commandId, actorId: actor.id, value });
544
675
  const observedAtMs = now();
545
676
  return publicSnapshot(await mutate(command.id, state => {
546
677
  const step = state.steps[command.nodeId];
547
- if (!step || step.kind !== 'human' || state.policy !== policy || step.requestDigest !== command.requestDigest) {
678
+ if (!step || step.kind !== 'human' || state.policy !== run.digest || step.requestDigest !== command.requestDigest) {
548
679
  throw new MayuraError('CONFLICT', 'Human request is stale or mismatched.');
549
680
  }
550
681
  if (step.status === 'succeeded') {
@@ -565,23 +696,25 @@ export function createWorkflowLifecycleRuntime(options) {
565
696
  }, 'lifecycle.human.responded', { nodeId: command.nodeId, requestDigest: command.requestDigest,
566
697
  responseDigest, actorId: actor.id }));
567
698
  };
568
- return Object.freeze({
699
+ const submitUnder = async (under, definition, command) => {
700
+ ensureOpen();
701
+ assertWorkflowLifecycle(definition);
702
+ if (typeof command.idempotencyKey !== 'string' || command.idempotencyKey.length < 1 || command.idempotencyKey.length > 128) {
703
+ throw new MayuraError('INVALID_INPUT', 'A bounded lifecycle submission key is required.');
704
+ }
705
+ const inputSnapshot = freezeJson(jsonValue(command.input, { maxBytes: under.maxOutputBytes }));
706
+ const input = jsonValue(await controlled(() => validate(definition.input, inputSnapshot, 'input')), { maxBytes: under.maxOutputBytes });
707
+ const state = initialWorkflowLifecycleState(lifecycleManifest(definition), input, definition.digest, under.digest, under.maxCostMicros);
708
+ const id = digest('mayura:workflow-lifecycle-run-id:v1', { scope: scopeKey, submissionKey: command.idempotencyKey });
709
+ const created = await createSubmission(() => store.create({ scope: scopeKey, id, idempotencyKey: `lifecycle:${command.idempotencyKey}`,
710
+ definitionHash: definition.digest, state: jsonValue(state), events: [{ type: 'lifecycle.run.created', data: {} }] }), 'workflow lifecycle run');
711
+ verifyDefinition(definition, created.record, stateFrom(created.record));
712
+ return publicSnapshot(created.record);
713
+ };
714
+ const runtime = Object.freeze({
569
715
  profile: 'lifecycle-v1',
570
- submit: async (definition, command) => {
571
- ensureOpen();
572
- assertWorkflowLifecycle(definition);
573
- if (typeof command.idempotencyKey !== 'string' || command.idempotencyKey.length < 1 || command.idempotencyKey.length > 128) {
574
- throw new MayuraError('INVALID_INPUT', 'A bounded lifecycle submission key is required.');
575
- }
576
- const inputSnapshot = freezeJson(jsonValue(command.input, { maxBytes: maxOutputBytes }));
577
- const input = jsonValue(await controlled(() => validate(definition.input, inputSnapshot, 'input')), { maxBytes: maxOutputBytes });
578
- const state = initialWorkflowLifecycleState(lifecycleManifest(definition), input, definition.digest, policy, maxCostMicros);
579
- const id = digest('mayura:workflow-lifecycle-run-id:v1', { scope: scopeKey, submissionKey: command.idempotencyKey });
580
- const created = await storageCall(() => store.create({ scope: scopeKey, id, idempotencyKey: `lifecycle:${command.idempotencyKey}`,
581
- definitionHash: definition.digest, state: jsonValue(state), events: [{ type: 'lifecycle.run.created', data: {} }] }));
582
- verifyDefinition(definition, created.record, stateFrom(created.record));
583
- return publicSnapshot(created.record);
584
- },
716
+ // New runs always use the current settings.
717
+ submit: (definition, command) => submitUnder(currentPolicy, definition, command),
585
718
  inspect: async (id) => publicSnapshot(await load(id)),
586
719
  humanRequest: async (definition, id, nodeId) => {
587
720
  ensureOpen();
@@ -611,7 +744,7 @@ export function createWorkflowLifecycleRuntime(options) {
611
744
  throw new MayuraError('INVALID_INPUT', 'A lifecycle tool node ID is required.');
612
745
  const record = await load(id);
613
746
  const state = stateFrom(record);
614
- verifyDefinition(definition, record, state);
747
+ const run = verifyDefinition(definition, record, state);
615
748
  const node = definition.nodes.find(candidate => candidate.id === nodeId);
616
749
  const step = state.steps[nodeId];
617
750
  if (!node || node.kind !== 'tool' || !step || step.kind !== 'tool')
@@ -619,8 +752,8 @@ export function createWorkflowLifecycleRuntime(options) {
619
752
  if (!node.approval || !step.approval || (step.status !== 'waiting' && step.status !== 'approved'))
620
753
  return undefined;
621
754
  // Rebuild the input exactly as preparation did; it must reproduce the persisted digest, or the evidence is refused.
622
- const input = jsonValue(await controlled(() => validate(node.tool.input, resolveBinding(node.input, state.input, outputs(state)), 'input'), node.tool.timeoutMs), { maxBytes: maxOutputBytes });
623
- if (approvalCandidate(node, input, id, step.approval.expiresAt) !== step.approval.digest) {
755
+ const input = jsonValue(await controlled(() => validate(node.tool.input, resolveBinding(node.input, state.input, outputs(state)), 'input'), node.tool.timeoutMs), { maxBytes: run.maxOutputBytes });
756
+ if (approvalCandidate(node, input, id, step.approval.expiresAt, run.digest) !== step.approval.digest) {
624
757
  throw new MayuraError('CONFLICT', 'Persisted approval evidence does not match its definition.');
625
758
  }
626
759
  return freezeJson(jsonValue({ runId: id, nodeId, status: step.status, toolId: node.tool.id, toolVersion: node.tool.version, input,
@@ -650,14 +783,14 @@ export function createWorkflowLifecycleRuntime(options) {
650
783
  }
651
784
  let after = await load(id);
652
785
  const next = stateFrom(after);
653
- verifyDefinition(definition, after, next);
786
+ const run = verifyDefinition(definition, after, next);
654
787
  const steps = Object.values(next.steps);
655
788
  if (!schedulable(next))
656
789
  return publicSnapshot(after);
657
790
  if (steps.every(step => terminalStepStatuses.has(step.status))) {
658
791
  if (steps.every(step => satisfiedStepStatuses.has(step.status))) {
659
792
  try {
660
- next.output = jsonValue(await controlled(() => validate(definition.output, resolveBinding(definition.result, next.input, outputs(next)), 'output')), { maxBytes: maxOutputBytes });
793
+ next.output = jsonValue(await controlled(() => validate(definition.output, resolveBinding(definition.result, next.input, outputs(next)), 'output')), { maxBytes: run.maxOutputBytes });
661
794
  next.status = 'succeeded';
662
795
  }
663
796
  catch {
@@ -671,7 +804,7 @@ export function createWorkflowLifecycleRuntime(options) {
671
804
  after = await save(after, next, 'lifecycle.run.completed', { status: next.status });
672
805
  }
673
806
  catch (error) {
674
- if (!(error instanceof StorageError) || error.code !== 'CONFLICT')
807
+ if (!(error instanceof StorageError) || error.storageCode !== 'CONFLICT')
675
808
  throw error;
676
809
  after = await load(id);
677
810
  }
@@ -685,7 +818,7 @@ export function createWorkflowLifecycleRuntime(options) {
685
818
  after = await save(after, next, 'lifecycle.run.waiting');
686
819
  }
687
820
  catch (error) {
688
- if (!(error instanceof StorageError) || error.code !== 'CONFLICT')
821
+ if (!(error instanceof StorageError) || error.storageCode !== 'CONFLICT')
689
822
  throw error;
690
823
  after = await load(id);
691
824
  }
@@ -704,7 +837,8 @@ export function createWorkflowLifecycleRuntime(options) {
704
837
  const observedAtMs = now();
705
838
  return publicSnapshot(await mutate(command.id, state => {
706
839
  const step = state.steps[command.nodeId];
707
- if (!step || step.kind !== 'tool' || state.policy !== policy || !step.approval || step.approval.digest !== command.digest) {
840
+ // The digest binds the run's own policy; a run under an unlisted policy has nothing approvable.
841
+ if (!step || step.kind !== 'tool' || !pinnedTo(state) || !step.approval || step.approval.digest !== command.digest) {
708
842
  throw new MayuraError('CONFLICT', 'Approval request is stale, expired or mismatched.');
709
843
  }
710
844
  if (step.status !== 'waiting' && step.approval.humanId === human.id)
@@ -728,6 +862,64 @@ export function createWorkflowLifecycleRuntime(options) {
728
862
  return respondAs(definition, command, { id: human.id, projectId: human.projectId });
729
863
  },
730
864
  respondVerified: (definition, command) => respondAs(definition, command, command.actor),
865
+ signal: async (definition, command) => {
866
+ ensureOpen();
867
+ assertWorkflowLifecycle(definition);
868
+ if (!command || typeof command.id !== 'string' || !hashPattern.test(command.id) || typeof command.name !== 'string' || !signalPattern.test(command.name)
869
+ || typeof command.signalId !== 'string' || !signalPattern.test(command.signalId)
870
+ || (command.actorId !== undefined && (typeof command.actorId !== 'string' || command.actorId.length < 1 || command.actorId.length > 256))) {
871
+ throw new MayuraError('INVALID_INPUT', 'A signal needs a run id, a signal name and a signal id (1–128 letters, digits, ".", "_", "/" or "-").');
872
+ }
873
+ const node = definition.nodes.find(candidate => candidate.kind === 'signal' && candidate.name === command.name);
874
+ if (!node || node.kind !== 'signal')
875
+ throw new MayuraError('NOT_FOUND', `This workflow has no signal step named "${command.name}".`);
876
+ const before = await load(command.id);
877
+ const run = verifyDefinition(definition, before, stateFrom(before));
878
+ let payload;
879
+ try {
880
+ payload = jsonValue(await controlled(() => validate(node.payload, command.payload, 'input')), { maxBytes: run.maxOutputBytes });
881
+ }
882
+ catch (error) {
883
+ if (error instanceof MayuraError && ['TIMEOUT', 'LIMIT_EXCEEDED'].includes(error.code))
884
+ throw error;
885
+ throw new MayuraError('INVALID_INPUT', `The payload of signal "${command.name}" does not match the step's payload schema.`);
886
+ }
887
+ const payloadDigest = digest('mayura:workflow-signal:v1', { runId: command.id, nodeId: node.id, signalId: command.signalId, payload });
888
+ const receivedAtMs = now();
889
+ return publicSnapshot(await mutate(command.id, state => {
890
+ const step = state.steps[node.id];
891
+ if (!step || step.kind !== 'signal')
892
+ throw new MayuraError('CONFLICT', 'Stored lifecycle steps do not match the pinned definition.');
893
+ if (step.signalId !== null) {
894
+ // Idempotent on the signal id: the same signal again changes nothing.
895
+ if (step.signalId === command.signalId && step.payloadDigest === payloadDigest)
896
+ return false;
897
+ throw new MayuraError('CONFLICT', step.signalId === command.signalId
898
+ ? `Signal "${command.signalId}" was already delivered with a different payload.`
899
+ : `Signal step "${node.id}" already received signal "${step.signalId}"; a signal step accepts one signal.`);
900
+ }
901
+ if (finalRunStatuses.has(state.status))
902
+ throw new MayuraError('CONFLICT', `The run is ${state.status}; it no longer accepts signals.`);
903
+ if (step.status !== 'pending' && step.status !== 'waiting') {
904
+ throw new MayuraError('CONFLICT', `Signal step "${node.id}" is ${step.status}; it no longer accepts signals.`);
905
+ }
906
+ if (step.status === 'waiting' && step.deadlineAtMs !== null && receivedAtMs >= step.deadlineAtMs) {
907
+ throw new MayuraError('CONFLICT', `Signal step "${node.id}" passed its deadline; it no longer accepts signals.`);
908
+ }
909
+ step.signalId = command.signalId;
910
+ step.payloadDigest = payloadDigest;
911
+ step.receivedAtMs = receivedAtMs;
912
+ step.output = payload;
913
+ // A step that has not started keeps the signal until it starts.
914
+ if (step.status === 'waiting') {
915
+ step.status = 'succeeded';
916
+ if (state.status !== 'paused')
917
+ state.status = 'running';
918
+ }
919
+ return true;
920
+ }, 'lifecycle.signal.delivered', { nodeId: node.id, signalId: command.signalId, payloadDigest,
921
+ ...(command.actorId === undefined ? {} : { actorId: command.actorId }) }));
922
+ },
731
923
  pause: async (id) => publicSnapshot(await mutate(id, state => {
732
924
  if (state.status === 'paused')
733
925
  return false;
@@ -745,14 +937,17 @@ export function createWorkflowLifecycleRuntime(options) {
745
937
  assertWorkflowLifecycle(migration.from);
746
938
  assertWorkflowLifecycle(migration.to);
747
939
  const { id, actorId, commandId, dryRun = false } = migrationCommand(command);
940
+ // A reviewed migration is the explicit way to move a run onto the current settings, whichever it started under.
748
941
  const record = await load(id);
749
942
  const state = stateFrom(record);
750
- verifyDefinition(migration.from, record, state);
943
+ verifyDefinition(migration.from, record, state, true);
751
944
  const nodes = (definition) => lifecycleManifest(definition).graph.map(node => ({ id: node.id, kind: node.kind, dependsOn: node.dependsOn, fingerprint: nodeFingerprint(node),
752
945
  ...(nodeEvidence(node) ? { evidence: nodeEvidence(node) } : {}) }));
753
946
  const preconditions = [];
754
947
  if (state.status !== 'paused')
755
948
  preconditions.push({ node: '*', reason: `The run is ${state.status}; pause it before migrating.` });
949
+ if (state.spentMicros > maxCostMicros)
950
+ preconditions.push({ node: '*', reason: 'The run already spent more than the current maxCostMicros.' });
756
951
  const plan = planWorkflowMigration({ migration, format: 'lifecycle-v1', runId: id, fromDigest: migration.from.digest, toDigest: migration.to.digest,
757
952
  from: nodes(migration.from), to: nodes(migration.to), steps: Object.entries(state.steps).map(([step, value]) => ({ id: step, status: value.status })), preconditions });
758
953
  if (dryRun)
@@ -763,9 +958,11 @@ export function createWorkflowLifecycleRuntime(options) {
763
958
  for (const entry of plan.entries) {
764
959
  if (!entry.target)
765
960
  continue;
766
- steps[entry.target] = entry.action === 'keep' || entry.action === 'accept' ? state.steps[entry.source] : fresh.steps[entry.target];
961
+ const source = entry.source === undefined ? undefined : state.steps[entry.source];
962
+ steps[entry.target] = entry.action === 'keep' || entry.action === 'accept'
963
+ || (entry.action === 'update' && source?.kind === 'signal' && fresh.steps[entry.target].kind === 'signal') ? source : fresh.steps[entry.target];
767
964
  }
768
- const next = { ...state, definition: migration.to.digest,
965
+ const next = { ...state, definition: migration.to.digest, policy, maxCostMicros,
769
966
  steps: Object.fromEntries(migration.to.nodes.map(node => [node.id, steps[node.id]])) };
770
967
  // Released holds: only carried tool steps keep reserved cost.
771
968
  next.reservedMicros = Object.values(next.steps).reduce((sum, step) => sum + (step.kind === 'tool' ? step.costReserved : 0), 0);
@@ -827,5 +1024,12 @@ export function createWorkflowLifecycleRuntime(options) {
827
1024
  drain: options => gate.drain(options, () => { closed = true; for (const controller of active.values())
828
1025
  controller.abort(); }),
829
1026
  });
1027
+ pinnedSubmissions.set(runtime, (index, definition, command) => {
1028
+ const under = policies[index];
1029
+ if (!Number.isSafeInteger(index) || !under)
1030
+ throw new MayuraError('INVALID_INPUT', 'The requested lifecycle policy is not configured.');
1031
+ return submitUnder(under, definition, command);
1032
+ });
1033
+ return runtime;
830
1034
  }
831
1035
  //# sourceMappingURL=lifecycle-runtime.js.map