@proofoftech/flowsafe 0.11.0 → 0.12.0

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 (384) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +65 -43
  3. package/deployment-identity-protocol.d.mts +52 -0
  4. package/deployment-identity-protocol.mjs +231 -0
  5. package/dist/agent-host/approval-resumer.d.ts +3 -3
  6. package/dist/agent-host/approval-resumer.d.ts.map +1 -1
  7. package/dist/agent-host/approval-resumer.js +6 -7
  8. package/dist/agent-host/approval-resumer.js.map +1 -1
  9. package/dist/agent-host/catalog.js +2 -2
  10. package/dist/agent-host/catalog.js.map +1 -1
  11. package/dist/agent-host/index.d.ts +2 -2
  12. package/dist/agent-host/index.d.ts.map +1 -1
  13. package/dist/agent-host/index.js.map +1 -1
  14. package/dist/agent-host/router.d.ts +2 -2
  15. package/dist/agent-host/router.d.ts.map +1 -1
  16. package/dist/agent-host/router.js +22 -54
  17. package/dist/agent-host/router.js.map +1 -1
  18. package/dist/agent-host/thread-host.d.ts +42 -8
  19. package/dist/agent-host/thread-host.d.ts.map +1 -1
  20. package/dist/agent-host/thread-host.js +533 -106
  21. package/dist/agent-host/thread-host.js.map +1 -1
  22. package/dist/agent-host/thread-topology.d.ts +28 -7
  23. package/dist/agent-host/thread-topology.d.ts.map +1 -1
  24. package/dist/agent-host/thread-topology.js +97 -39
  25. package/dist/agent-host/thread-topology.js.map +1 -1
  26. package/dist/agent-host/trusted-context.d.ts.map +1 -1
  27. package/dist/agent-host/trusted-context.js +3 -1
  28. package/dist/agent-host/trusted-context.js.map +1 -1
  29. package/dist/agent-host/types.d.ts +2 -0
  30. package/dist/agent-host/types.d.ts.map +1 -1
  31. package/dist/agent-runner/agent-run-state.d.ts +9 -8
  32. package/dist/agent-runner/agent-run-state.d.ts.map +1 -1
  33. package/dist/agent-runner/agent-run-state.js +78 -40
  34. package/dist/agent-runner/agent-run-state.js.map +1 -1
  35. package/dist/agent-runner/durable-agent-runner.d.ts +18 -16
  36. package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
  37. package/dist/agent-runner/durable-agent-runner.js +120 -43
  38. package/dist/agent-runner/durable-agent-runner.js.map +1 -1
  39. package/dist/agent-runner/index.d.ts +1 -1
  40. package/dist/agent-runner/index.d.ts.map +1 -1
  41. package/dist/agent-runner/index.js +1 -1
  42. package/dist/agent-runner/index.js.map +1 -1
  43. package/dist/approval-api/actor-context.d.ts +67 -0
  44. package/dist/approval-api/actor-context.d.ts.map +1 -0
  45. package/dist/approval-api/actor-context.js +120 -0
  46. package/dist/approval-api/actor-context.js.map +1 -0
  47. package/dist/approval-api/contract.d.ts +12 -23
  48. package/dist/approval-api/contract.d.ts.map +1 -1
  49. package/dist/approval-api/d1-store.d.ts +7 -15
  50. package/dist/approval-api/d1-store.d.ts.map +1 -1
  51. package/dist/approval-api/d1-store.js +83 -159
  52. package/dist/approval-api/d1-store.js.map +1 -1
  53. package/dist/approval-api/grants.d.ts +3 -17
  54. package/dist/approval-api/grants.d.ts.map +1 -1
  55. package/dist/approval-api/grants.js +19 -39
  56. package/dist/approval-api/grants.js.map +1 -1
  57. package/dist/approval-api/index.d.ts +8 -8
  58. package/dist/approval-api/index.d.ts.map +1 -1
  59. package/dist/approval-api/index.js +5 -5
  60. package/dist/approval-api/index.js.map +1 -1
  61. package/dist/approval-api/principal.d.ts +29 -30
  62. package/dist/approval-api/principal.d.ts.map +1 -1
  63. package/dist/approval-api/principal.js +73 -54
  64. package/dist/approval-api/principal.js.map +1 -1
  65. package/dist/approval-api/resource-ownership.d.ts +64 -0
  66. package/dist/approval-api/resource-ownership.d.ts.map +1 -0
  67. package/dist/approval-api/resource-ownership.js +329 -0
  68. package/dist/approval-api/resource-ownership.js.map +1 -0
  69. package/dist/approval-api/retention.d.ts +6 -11
  70. package/dist/approval-api/retention.d.ts.map +1 -1
  71. package/dist/approval-api/retention.js +7 -12
  72. package/dist/approval-api/retention.js.map +1 -1
  73. package/dist/approval-api/router.d.ts +9 -8
  74. package/dist/approval-api/router.d.ts.map +1 -1
  75. package/dist/approval-api/router.js +50 -18
  76. package/dist/approval-api/router.js.map +1 -1
  77. package/dist/approval-api/service.d.ts +26 -36
  78. package/dist/approval-api/service.d.ts.map +1 -1
  79. package/dist/approval-api/service.js +104 -166
  80. package/dist/approval-api/service.js.map +1 -1
  81. package/dist/approval-api/store-factory.d.ts +19 -0
  82. package/dist/approval-api/store-factory.d.ts.map +1 -0
  83. package/dist/approval-api/store-factory.js +49 -0
  84. package/dist/approval-api/store-factory.js.map +1 -0
  85. package/dist/approval-api/store.d.ts +22 -36
  86. package/dist/approval-api/store.d.ts.map +1 -1
  87. package/dist/approval-api/store.js +48 -66
  88. package/dist/approval-api/store.js.map +1 -1
  89. package/dist/approval-api/types.d.ts +28 -22
  90. package/dist/approval-api/types.d.ts.map +1 -1
  91. package/dist/approval-api/types.js +51 -3
  92. package/dist/approval-api/types.js.map +1 -1
  93. package/dist/approval-ui/client.js +3 -3
  94. package/dist/approval-ui/client.js.map +1 -1
  95. package/dist/approval-ui/stream.d.ts +4 -4
  96. package/dist/approval-ui/stream.d.ts.map +1 -1
  97. package/dist/approval-ui/stream.js +2 -2
  98. package/dist/approval-ui/tips.d.ts +1 -1
  99. package/dist/approval-ui/tips.js +1 -1
  100. package/dist/approval-ui/tips.js.map +1 -1
  101. package/dist/approval-ui/use-approval-dashboard.d.ts +3 -3
  102. package/dist/approval-ui/use-approval-dashboard.js +1 -1
  103. package/dist/artifacts/index.d.ts +2 -3
  104. package/dist/artifacts/index.d.ts.map +1 -1
  105. package/dist/artifacts/index.js +9 -8
  106. package/dist/artifacts/index.js.map +1 -1
  107. package/dist/audit-export/audit-proxy.d.ts +71 -0
  108. package/dist/audit-export/audit-proxy.d.ts.map +1 -0
  109. package/dist/audit-export/audit-proxy.js +142 -0
  110. package/dist/audit-export/audit-proxy.js.map +1 -0
  111. package/dist/audit-export/index.d.ts +4 -7
  112. package/dist/audit-export/index.d.ts.map +1 -1
  113. package/dist/audit-export/index.js +1 -4
  114. package/dist/audit-export/index.js.map +1 -1
  115. package/dist/audit-export/queue-types.d.ts +5 -0
  116. package/dist/audit-export/queue-types.d.ts.map +1 -0
  117. package/dist/audit-export/queue-types.js +3 -0
  118. package/dist/audit-export/queue-types.js.map +1 -0
  119. package/dist/background-tasks/d1-storage.d.ts +9 -19
  120. package/dist/background-tasks/d1-storage.d.ts.map +1 -1
  121. package/dist/background-tasks/d1-storage.js +22 -52
  122. package/dist/background-tasks/d1-storage.js.map +1 -1
  123. package/dist/background-tasks/host.d.ts +4 -6
  124. package/dist/background-tasks/host.d.ts.map +1 -1
  125. package/dist/background-tasks/host.js +14 -17
  126. package/dist/background-tasks/host.js.map +1 -1
  127. package/dist/background-tasks/index.d.ts +1 -1
  128. package/dist/background-tasks/index.d.ts.map +1 -1
  129. package/dist/background-tasks/index.js +3 -3
  130. package/dist/background-tasks/index.js.map +1 -1
  131. package/dist/background-tasks/routes.d.ts +8 -4
  132. package/dist/background-tasks/routes.d.ts.map +1 -1
  133. package/dist/background-tasks/routes.js +33 -31
  134. package/dist/background-tasks/routes.js.map +1 -1
  135. package/dist/deployment-identity-protocol.d.ts +3 -0
  136. package/dist/deployment-identity-protocol.d.ts.map +1 -0
  137. package/dist/deployment-identity-protocol.js +3 -0
  138. package/dist/deployment-identity-protocol.js.map +1 -0
  139. package/dist/do-runner/breakwater-keys.d.ts +7 -5
  140. package/dist/do-runner/breakwater-keys.d.ts.map +1 -1
  141. package/dist/do-runner/breakwater-keys.js +7 -5
  142. package/dist/do-runner/breakwater-keys.js.map +1 -1
  143. package/dist/do-runner/cf-types.d.ts +18 -12
  144. package/dist/do-runner/cf-types.d.ts.map +1 -1
  145. package/dist/do-runner/cf-types.js.map +1 -1
  146. package/dist/do-runner/d1-storage.d.ts +44 -182
  147. package/dist/do-runner/d1-storage.d.ts.map +1 -1
  148. package/dist/do-runner/d1-storage.js +102 -353
  149. package/dist/do-runner/d1-storage.js.map +1 -1
  150. package/dist/do-runner/deployment-identity.d.ts +121 -0
  151. package/dist/do-runner/deployment-identity.d.ts.map +1 -0
  152. package/dist/do-runner/deployment-identity.js +272 -0
  153. package/dist/do-runner/deployment-identity.js.map +1 -0
  154. package/dist/do-runner/do-error-response.d.ts +1 -1
  155. package/dist/do-runner/do-error-response.d.ts.map +1 -1
  156. package/dist/do-runner/do-error-response.js +7 -1
  157. package/dist/do-runner/do-error-response.js.map +1 -1
  158. package/dist/do-runner/durable-object.d.ts +23 -16
  159. package/dist/do-runner/durable-object.d.ts.map +1 -1
  160. package/dist/do-runner/durable-object.js +303 -70
  161. package/dist/do-runner/durable-object.js.map +1 -1
  162. package/dist/do-runner/execution-context.d.ts +1 -0
  163. package/dist/do-runner/execution-context.d.ts.map +1 -1
  164. package/dist/do-runner/execution-context.js +4 -7
  165. package/dist/do-runner/execution-context.js.map +1 -1
  166. package/dist/do-runner/execution-principal-header.d.ts +16 -0
  167. package/dist/do-runner/execution-principal-header.d.ts.map +1 -0
  168. package/dist/do-runner/execution-principal-header.js +20 -0
  169. package/dist/do-runner/execution-principal-header.js.map +1 -0
  170. package/dist/do-runner/hub-do.d.ts +17 -25
  171. package/dist/do-runner/hub-do.d.ts.map +1 -1
  172. package/dist/do-runner/hub-do.js +63 -59
  173. package/dist/do-runner/hub-do.js.map +1 -1
  174. package/dist/do-runner/index.d.ts +12 -10
  175. package/dist/do-runner/index.d.ts.map +1 -1
  176. package/dist/do-runner/index.js +10 -9
  177. package/dist/do-runner/index.js.map +1 -1
  178. package/dist/do-runner/init.d.ts +0 -7
  179. package/dist/do-runner/init.d.ts.map +1 -1
  180. package/dist/do-runner/init.js +1 -2
  181. package/dist/do-runner/init.js.map +1 -1
  182. package/dist/do-runner/memory-id.d.ts +8 -25
  183. package/dist/do-runner/memory-id.d.ts.map +1 -1
  184. package/dist/do-runner/memory-id.js +25 -52
  185. package/dist/do-runner/memory-id.js.map +1 -1
  186. package/dist/do-runner/path-safe-id.d.ts +2 -48
  187. package/dist/do-runner/path-safe-id.d.ts.map +1 -1
  188. package/dist/do-runner/path-safe-id.js +3 -93
  189. package/dist/do-runner/path-safe-id.js.map +1 -1
  190. package/dist/do-runner/pubsub.d.ts +1 -1
  191. package/dist/do-runner/pubsub.js +1 -1
  192. package/dist/do-runner/runtime.d.ts +47 -34
  193. package/dist/do-runner/runtime.d.ts.map +1 -1
  194. package/dist/do-runner/runtime.js +281 -143
  195. package/dist/do-runner/runtime.js.map +1 -1
  196. package/dist/do-runner/schedule-source.d.ts +81 -0
  197. package/dist/do-runner/schedule-source.d.ts.map +1 -0
  198. package/dist/do-runner/schedule-source.js +64 -0
  199. package/dist/do-runner/schedule-source.js.map +1 -0
  200. package/dist/do-runner/thread-do.d.ts +18 -32
  201. package/dist/do-runner/thread-do.d.ts.map +1 -1
  202. package/dist/do-runner/thread-do.js +52 -74
  203. package/dist/do-runner/thread-do.js.map +1 -1
  204. package/dist/goals/objective-routes.d.ts +7 -4
  205. package/dist/goals/objective-routes.d.ts.map +1 -1
  206. package/dist/goals/objective-routes.js +58 -43
  207. package/dist/goals/objective-routes.js.map +1 -1
  208. package/dist/host-kit/approval-bridge.d.ts +18 -21
  209. package/dist/host-kit/approval-bridge.d.ts.map +1 -1
  210. package/dist/host-kit/approval-bridge.js +37 -37
  211. package/dist/host-kit/approval-bridge.js.map +1 -1
  212. package/dist/host-kit/bearer-auth.d.ts +3 -4
  213. package/dist/host-kit/bearer-auth.d.ts.map +1 -1
  214. package/dist/host-kit/bearer-auth.js +7 -11
  215. package/dist/host-kit/bearer-auth.js.map +1 -1
  216. package/dist/host-kit/do-run-topology.d.ts +14 -5
  217. package/dist/host-kit/do-run-topology.d.ts.map +1 -1
  218. package/dist/host-kit/do-run-topology.js +47 -15
  219. package/dist/host-kit/do-run-topology.js.map +1 -1
  220. package/dist/host-kit/env-vars.d.ts +1 -1
  221. package/dist/host-kit/env-vars.js +1 -1
  222. package/dist/host-kit/flowsafe-worker.d.ts +149 -73
  223. package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
  224. package/dist/host-kit/flowsafe-worker.js +619 -141
  225. package/dist/host-kit/flowsafe-worker.js.map +1 -1
  226. package/dist/host-kit/host-approval-service.d.ts +62 -54
  227. package/dist/host-kit/host-approval-service.d.ts.map +1 -1
  228. package/dist/host-kit/host-approval-service.js +90 -61
  229. package/dist/host-kit/host-approval-service.js.map +1 -1
  230. package/dist/host-kit/hub-topology.d.ts +6 -7
  231. package/dist/host-kit/hub-topology.d.ts.map +1 -1
  232. package/dist/host-kit/hub-topology.js +16 -12
  233. package/dist/host-kit/hub-topology.js.map +1 -1
  234. package/dist/host-kit/index.d.ts +13 -11
  235. package/dist/host-kit/index.d.ts.map +1 -1
  236. package/dist/host-kit/index.js +9 -12
  237. package/dist/host-kit/index.js.map +1 -1
  238. package/dist/host-kit/maintenance-capability.d.ts +58 -0
  239. package/dist/host-kit/maintenance-capability.d.ts.map +1 -0
  240. package/dist/host-kit/maintenance-capability.js +268 -0
  241. package/dist/host-kit/maintenance-capability.js.map +1 -0
  242. package/dist/host-kit/memory-boundary.d.ts +4 -19
  243. package/dist/host-kit/memory-boundary.d.ts.map +1 -1
  244. package/dist/host-kit/memory-boundary.js +12 -27
  245. package/dist/host-kit/memory-boundary.js.map +1 -1
  246. package/dist/host-kit/resource-access.d.ts +4 -0
  247. package/dist/host-kit/resource-access.d.ts.map +1 -0
  248. package/dist/host-kit/resource-access.js +12 -0
  249. package/dist/host-kit/resource-access.js.map +1 -0
  250. package/dist/host-kit/run-router.d.ts +25 -15
  251. package/dist/host-kit/run-router.d.ts.map +1 -1
  252. package/dist/host-kit/run-router.js +61 -46
  253. package/dist/host-kit/run-router.js.map +1 -1
  254. package/dist/host-kit/state-egress.d.ts +51 -0
  255. package/dist/host-kit/state-egress.d.ts.map +1 -0
  256. package/dist/host-kit/state-egress.js +120 -0
  257. package/dist/host-kit/state-egress.js.map +1 -0
  258. package/dist/host-kit/stream-router.d.ts +9 -4
  259. package/dist/host-kit/stream-router.d.ts.map +1 -1
  260. package/dist/host-kit/stream-router.js +45 -38
  261. package/dist/host-kit/stream-router.js.map +1 -1
  262. package/dist/host-kit/stream-ticket.d.ts +10 -10
  263. package/dist/host-kit/stream-ticket.d.ts.map +1 -1
  264. package/dist/host-kit/stream-ticket.js +60 -90
  265. package/dist/host-kit/stream-ticket.js.map +1 -1
  266. package/dist/host-kit/thread-topology.d.ts +17 -11
  267. package/dist/host-kit/thread-topology.d.ts.map +1 -1
  268. package/dist/host-kit/thread-topology.js +33 -59
  269. package/dist/host-kit/thread-topology.js.map +1 -1
  270. package/dist/host-kit/verifier.d.ts +6 -6
  271. package/dist/host-kit/verifier.d.ts.map +1 -1
  272. package/dist/host-kit/verifier.js +51 -119
  273. package/dist/host-kit/verifier.js.map +1 -1
  274. package/dist/http-body.d.ts +20 -0
  275. package/dist/http-body.d.ts.map +1 -0
  276. package/dist/http-body.js +67 -0
  277. package/dist/http-body.js.map +1 -0
  278. package/dist/schedules/index.d.ts +3 -2
  279. package/dist/schedules/index.d.ts.map +1 -1
  280. package/dist/schedules/index.js +5 -4
  281. package/dist/schedules/index.js.map +1 -1
  282. package/dist/schedules/router.d.ts +16 -10
  283. package/dist/schedules/router.d.ts.map +1 -1
  284. package/dist/schedules/router.js +153 -91
  285. package/dist/schedules/router.js.map +1 -1
  286. package/dist/schedules/schedules-d1.d.ts +88 -0
  287. package/dist/schedules/schedules-d1.d.ts.map +1 -1
  288. package/dist/schedules/schedules-d1.js +411 -50
  289. package/dist/schedules/schedules-d1.js.map +1 -1
  290. package/dist/schedules/target-policy.d.ts +29 -0
  291. package/dist/schedules/target-policy.d.ts.map +1 -0
  292. package/dist/schedules/target-policy.js +70 -0
  293. package/dist/schedules/target-policy.js.map +1 -0
  294. package/dist/schedules/tick.d.ts +125 -21
  295. package/dist/schedules/tick.d.ts.map +1 -1
  296. package/dist/schedules/tick.js +637 -160
  297. package/dist/schedules/tick.js.map +1 -1
  298. package/dist/signal-providers/delivery.d.ts +0 -19
  299. package/dist/signal-providers/delivery.d.ts.map +1 -1
  300. package/dist/signal-providers/delivery.js +5 -54
  301. package/dist/signal-providers/delivery.js.map +1 -1
  302. package/dist/signal-providers/github-provider.d.ts.map +1 -1
  303. package/dist/signal-providers/github-provider.js +9 -16
  304. package/dist/signal-providers/github-provider.js.map +1 -1
  305. package/dist/signal-providers/host-do.d.ts +3 -45
  306. package/dist/signal-providers/host-do.d.ts.map +1 -1
  307. package/dist/signal-providers/host-do.js +86 -106
  308. package/dist/signal-providers/host-do.js.map +1 -1
  309. package/dist/signal-providers/host-topology.d.ts +5 -14
  310. package/dist/signal-providers/host-topology.d.ts.map +1 -1
  311. package/dist/signal-providers/host-topology.js +11 -25
  312. package/dist/signal-providers/host-topology.js.map +1 -1
  313. package/dist/signal-providers/index.d.ts +3 -3
  314. package/dist/signal-providers/index.d.ts.map +1 -1
  315. package/dist/signal-providers/index.js +3 -3
  316. package/dist/signal-providers/index.js.map +1 -1
  317. package/dist/signal-providers/provider.d.ts +4 -5
  318. package/dist/signal-providers/provider.d.ts.map +1 -1
  319. package/dist/signal-providers/provider.js +5 -6
  320. package/dist/signal-providers/provider.js.map +1 -1
  321. package/dist/signal-providers/subscription-d1.d.ts +10 -57
  322. package/dist/signal-providers/subscription-d1.d.ts.map +1 -1
  323. package/dist/signal-providers/subscription-d1.js +208 -155
  324. package/dist/signal-providers/subscription-d1.js.map +1 -1
  325. package/dist/signal-providers/webhook-route.d.ts +28 -14
  326. package/dist/signal-providers/webhook-route.d.ts.map +1 -1
  327. package/dist/signal-providers/webhook-route.js +117 -88
  328. package/dist/signal-providers/webhook-route.js.map +1 -1
  329. package/dist/signals/client.js +8 -8
  330. package/dist/signals/client.js.map +1 -1
  331. package/dist/signals/d1-shared.js +1 -1
  332. package/dist/signals/d1-shared.js.map +1 -1
  333. package/dist/signals/index.d.ts +1 -1
  334. package/dist/signals/index.d.ts.map +1 -1
  335. package/dist/signals/index.js.map +1 -1
  336. package/dist/signals/notification-dispatch.d.ts +12 -6
  337. package/dist/signals/notification-dispatch.d.ts.map +1 -1
  338. package/dist/signals/notification-dispatch.js +37 -21
  339. package/dist/signals/notification-dispatch.js.map +1 -1
  340. package/dist/signals/notifications-d1.d.ts +5 -8
  341. package/dist/signals/notifications-d1.d.ts.map +1 -1
  342. package/dist/signals/notifications-d1.js +8 -13
  343. package/dist/signals/notifications-d1.js.map +1 -1
  344. package/dist/signals/router.d.ts +10 -10
  345. package/dist/signals/router.d.ts.map +1 -1
  346. package/dist/signals/router.js +49 -51
  347. package/dist/signals/router.js.map +1 -1
  348. package/dist/signals/thread-do-routes.d.ts +74 -15
  349. package/dist/signals/thread-do-routes.d.ts.map +1 -1
  350. package/dist/signals/thread-do-routes.js +484 -41
  351. package/dist/signals/thread-do-routes.js.map +1 -1
  352. package/dist/signals/thread-state-d1.d.ts.map +1 -1
  353. package/dist/signals/thread-state-d1.js +2 -3
  354. package/dist/signals/thread-state-d1.js.map +1 -1
  355. package/package.json +29 -6
  356. package/scripts/seed-deployment-identity.mjs +252 -0
  357. package/dist/approval-api/tenant-brand.d.ts +0 -35
  358. package/dist/approval-api/tenant-brand.d.ts.map +0 -1
  359. package/dist/approval-api/tenant-brand.js +0 -19
  360. package/dist/approval-api/tenant-brand.js.map +0 -1
  361. package/dist/approval-api/tenant-context.d.ts +0 -84
  362. package/dist/approval-api/tenant-context.d.ts.map +0 -1
  363. package/dist/approval-api/tenant-context.js +0 -112
  364. package/dist/approval-api/tenant-context.js.map +0 -1
  365. package/dist/approval-api/tenant-store.d.ts +0 -36
  366. package/dist/approval-api/tenant-store.d.ts.map +0 -1
  367. package/dist/approval-api/tenant-store.js +0 -129
  368. package/dist/approval-api/tenant-store.js.map +0 -1
  369. package/dist/do-runner/resume-ledger.d.ts +0 -48
  370. package/dist/do-runner/resume-ledger.d.ts.map +0 -1
  371. package/dist/do-runner/resume-ledger.js +0 -60
  372. package/dist/do-runner/resume-ledger.js.map +0 -1
  373. package/dist/do-runner/thread-header.d.ts +0 -31
  374. package/dist/do-runner/thread-header.d.ts.map +0 -1
  375. package/dist/do-runner/thread-header.js +0 -39
  376. package/dist/do-runner/thread-header.js.map +0 -1
  377. package/dist/host-kit/subdomain-check.d.ts +0 -23
  378. package/dist/host-kit/subdomain-check.d.ts.map +0 -1
  379. package/dist/host-kit/subdomain-check.js +0 -61
  380. package/dist/host-kit/subdomain-check.js.map +0 -1
  381. package/dist/host-kit/tenant-registry.d.ts +0 -51
  382. package/dist/host-kit/tenant-registry.d.ts.map +0 -1
  383. package/dist/host-kit/tenant-registry.js +0 -88
  384. package/dist/host-kit/tenant-registry.js.map +0 -1
@@ -19,10 +19,11 @@
19
19
  // (see durable-object.ts).
20
20
  import { Mastra } from '@mastra/core/mastra';
21
21
  import { RequestContext } from '@mastra/core/request-context';
22
- import { BREAKWATER_ACTOR_KEY, BREAKWATER_CONNECTOR_EXECUTION_KEY, BREAKWATER_CONNECTOR_GRANTS_KEY, BREAKWATER_ISOLATION_SCOPE_KEY, BREAKWATER_WORKFLOW_SCOPE_KEY, } from './breakwater-keys.js';
22
+ import { isExecutionPrincipalId, isExecutionPrincipalKind, } from '../approval-api/principal.js';
23
+ import { BREAKWATER_ACTOR_KEY, BREAKWATER_CONNECTOR_EXECUTION_KEY, BREAKWATER_ISOLATION_SCOPE_KEY, BREAKWATER_WORKFLOW_SCOPE_KEY, } from './breakwater-keys.js';
24
+ import { isReservedExecutionContextKey, RUN_PROVENANCE_CONTEXT_KEY, stripReservedExecutionContext, } from './execution-context.js';
23
25
  import { mastraRegistryEntries } from './mastra-registry.js';
24
- import { PATH_SAFE_ID_PATTERN, tenantOfRunId } from './path-safe-id.js';
25
- import { InMemoryResumeLedger } from './resume-ledger.js';
26
+ import { isPathSafeId } from './path-safe-id.js';
26
27
  export class UnknownWorkflowError extends Error {
27
28
  constructor(workflowId) {
28
29
  super(`unknown workflow '${workflowId}'`);
@@ -75,6 +76,58 @@ function asClientError(error) {
75
76
  ? new InvalidRunRequestError(error.message)
76
77
  : undefined;
77
78
  }
79
+ const RUN_STATE_FIELDS = [
80
+ 'result',
81
+ 'error',
82
+ 'steps',
83
+ 'suspendedPaths',
84
+ 'requestContext',
85
+ ];
86
+ function runProvenance(state) {
87
+ const value = state.requestContext?.[RUN_PROVENANCE_CONTEXT_KEY];
88
+ if (value === undefined)
89
+ return undefined;
90
+ if (value === null || typeof value !== 'object') {
91
+ throw new Error('stored run provenance is malformed');
92
+ }
93
+ const candidate = value;
94
+ if (candidate.version !== 1 ||
95
+ (candidate.requestedBy !== undefined &&
96
+ !isExecutionPrincipalId(candidate.requestedBy)) ||
97
+ (candidate.requestedByKind !== undefined &&
98
+ !isExecutionPrincipalKind(candidate.requestedByKind)) ||
99
+ (candidate.requestedBy === undefined &&
100
+ candidate.requestedByKind !== undefined) ||
101
+ !isPathSafeId(candidate.attemptToken) ||
102
+ (candidate.startToken !== undefined &&
103
+ !isPathSafeId(candidate.startToken)) ||
104
+ !Array.isArray(candidate.resumeCounts)) {
105
+ throw new Error('stored run provenance is malformed');
106
+ }
107
+ const counts = [];
108
+ for (const entry of candidate.resumeCounts) {
109
+ if (!Array.isArray(entry) ||
110
+ entry.length !== 2 ||
111
+ typeof entry[0] !== 'string' ||
112
+ !Number.isSafeInteger(entry[1]) ||
113
+ entry[1] < 1) {
114
+ throw new Error('stored run provenance is malformed');
115
+ }
116
+ counts.push([entry[0], entry[1]]);
117
+ }
118
+ return {
119
+ version: 1,
120
+ ...(candidate.requestedBy === undefined
121
+ ? {}
122
+ : { requestedBy: candidate.requestedBy }),
123
+ ...(candidate.requestedByKind === undefined
124
+ ? {}
125
+ : { requestedByKind: candidate.requestedByKind }),
126
+ attemptToken: candidate.attemptToken,
127
+ startToken: candidate.startToken ?? candidate.attemptToken,
128
+ resumeCounts: counts,
129
+ };
130
+ }
78
131
  const NONTERMINAL_RUN_STATUSES = new Set([
79
132
  'running',
80
133
  'suspended',
@@ -147,18 +200,21 @@ function errorText(error) {
147
200
  }
148
201
  return String(error);
149
202
  }
150
- function summarize(runId, result, counts) {
203
+ function summarize(runId, result, counts, requestedBy, requestedByKind) {
204
+ let summary;
151
205
  switch (result.status) {
152
206
  case 'success':
153
- return { runId, status: result.status, result: result.result };
207
+ summary = { runId, status: result.status, result: result.result };
208
+ break;
154
209
  case 'failed':
155
- return {
210
+ summary = {
156
211
  runId,
157
212
  status: result.status,
158
213
  error: errorText(result.error),
159
214
  };
215
+ break;
160
216
  case 'suspended': {
161
- const summary = {
217
+ summary = {
162
218
  runId,
163
219
  status: result.status,
164
220
  suspended: result.suspended,
@@ -171,17 +227,28 @@ function summarize(runId, result, counts) {
171
227
  const resumedAt = byStep(suspendedKeys, (key) => resumedAtOf(result.steps, key));
172
228
  if (resumedAt !== undefined)
173
229
  summary.resumedAt = resumedAt;
174
- // resumeCount is runtime-owned (the ledger), NOT read from the snapshot.
230
+ // resumeCount is runtime-owned snapshot provenance, not core step state.
175
231
  const resumeCount = byStep(suspendedKeys, (key) => counts?.get(key));
176
232
  if (resumeCount !== undefined)
177
233
  summary.resumeCount = resumeCount;
178
- return summary;
234
+ break;
179
235
  }
180
236
  case 'tripwire':
181
- return { runId, status: result.status, error: result.tripwire.reason };
237
+ summary = {
238
+ runId,
239
+ status: result.status,
240
+ error: result.tripwire.reason,
241
+ };
242
+ break;
182
243
  default:
183
- return { runId, status: result.status };
244
+ summary = { runId, status: result.status };
184
245
  }
246
+ if (requestedBy !== undefined)
247
+ summary.requestedBy = requestedBy;
248
+ if (requestedByKind !== undefined) {
249
+ summary.requestedByKind = requestedByKind;
250
+ }
251
+ return summary;
185
252
  }
186
253
  // Storage may hand timestamps back as Date or as a serialized string
187
254
  // depending on the adapter's snapshot round-trip; normalize defensively.
@@ -218,7 +285,7 @@ function suspendedAtOf(steps, stepKey) {
218
285
  * Epoch-ms resume time of the step's latest attempt, if recorded. Feeds the
219
286
  * INFORMATIONAL RunSummary.resumedAt only — Mastra stamps it solely on a
220
287
  * payload-bearing resume, so it is unreliable as a first-vs-re-suspension
221
- * signal. The grant binding uses `resumeCount` (the runtime ledger) instead.
288
+ * signal. The grant binding uses snapshot provenance `resumeCount` instead.
222
289
  */
223
290
  function resumedAtOf(steps, stepKey) {
224
291
  return latestAttempt(steps, stepKey)?.resumedAt;
@@ -232,7 +299,7 @@ function resumedAtOf(steps, stepKey) {
232
299
  * object literal a step named '__proto__' would route into the
233
300
  * Object.prototype setter and silently vanish (or, for an object-valued
234
301
  * payload, rewire the accumulator's prototype) — the same hazard
235
- * resume-ledger.ts stores [stepKey, count] pairs to avoid.
302
+ * snapshot provenance stores [stepKey, count] pairs to avoid.
236
303
  */
237
304
  function byStep(suspendedKeys, lookup) {
238
305
  const map = Object.create(null);
@@ -255,13 +322,18 @@ function byStep(suspendedKeys, lookup) {
255
322
  // (isFromInMemory), steps are empty and timestamps are current-time; the
256
323
  // projection truthfully degrades to status-only rather than fabricating
257
324
  // detail.
258
- function summarizeState(runId, state, counts) {
325
+ function summarizeState(runId, state, counts, requestedBy, requestedByKind) {
259
326
  const summary = {
260
327
  runId,
261
328
  status: state.status,
262
329
  createdAt: toIso(state.createdAt),
263
330
  updatedAt: toIso(state.updatedAt),
264
331
  };
332
+ if (requestedBy !== undefined)
333
+ summary.requestedBy = requestedBy;
334
+ if (requestedByKind !== undefined) {
335
+ summary.requestedByKind = requestedByKind;
336
+ }
265
337
  if (state.status === 'success') {
266
338
  summary.result = state.result;
267
339
  }
@@ -293,8 +365,8 @@ const TRUSTED_IDENTITY_CONTEXT_KEYS = new Set([
293
365
  'threadId',
294
366
  'resourceId',
295
367
  ]);
296
- function orderedRequestContext(runtimeContext, provided) {
297
- const stored = [];
368
+ function orderedRequestContext(runtimeContext, provided, scheduled) {
369
+ const application = [];
298
370
  const capabilities = [];
299
371
  const identity = [];
300
372
  for (const entry of Object.entries(provided ?? {})) {
@@ -302,23 +374,23 @@ function orderedRequestContext(runtimeContext, provided) {
302
374
  if (key === BREAKWATER_WORKFLOW_SCOPE_KEY ||
303
375
  key === BREAKWATER_ISOLATION_SCOPE_KEY ||
304
376
  key === BREAKWATER_CONNECTOR_EXECUTION_KEY ||
377
+ key === RUN_PROVENANCE_CONTEXT_KEY ||
305
378
  key === 'runId') {
306
379
  continue;
307
380
  }
308
381
  if (TRUSTED_IDENTITY_CONTEXT_KEYS.has(key)) {
309
382
  identity.push(entry);
310
383
  }
311
- else if (key === BREAKWATER_CONNECTOR_GRANTS_KEY ||
312
- key.startsWith('breakwater.') ||
313
- key === 'mastra:goal') {
384
+ else if (isReservedExecutionContextKey(key)) {
314
385
  capabilities.push(entry);
315
386
  }
316
387
  else {
317
- stored.push(entry);
388
+ application.push(entry);
318
389
  }
319
390
  }
320
391
  return new RequestContext([
321
- ...stored,
392
+ ...Object.entries(stripReservedExecutionContext(scheduled)),
393
+ ...application,
322
394
  ...Object.entries(runtimeContext),
323
395
  ...capabilities,
324
396
  ...identity,
@@ -331,25 +403,6 @@ export class RunnerRuntime {
331
403
  #agents = new Map();
332
404
  #workflows = new Map();
333
405
  #runLocks = new Map();
334
- // Per-run resume ledger: #runKey(workflowId, runId) -> (stepKey -> times that
335
- // step has been resumed). Keyed by the run's FULL identity, never runId alone
336
- // (see #runKey): a shared caller runId under two workflows are distinct runs.
337
- // The runtime increments it on every resume (regardless of
338
- // payload) and projects it as RunSummary.resumeCount / RunLeg.resumeCount —
339
- // the collision-free grant-binding tie-breaker that Mastra's
340
- // payload-conditional resumedAt cannot provide.
341
- //
342
- // Durability: a lost ledger is fail-closed for grants (leg.resumeCount
343
- // undefined vs an approval's captured ordinal -> mismatch -> deny, see
344
- // grants.ts) — never a leak, but an AVAILABILITY bug: the approved action
345
- // silently no-ops. DO eviction (~70-140s idle), hibernation, and code
346
- // deploys all wipe in-memory state while ctx.storage survives, so the DO
347
- // shell adopts a DurableStorageResumeLedger (durable-object.ts); the
348
- // in-memory default covers node tests and non-DO hosts. Entries are
349
- // deleted on terminal status below; a resumed-then-abandoned suspended run
350
- // keeps one small entry until the run is purged.
351
- #resumeLedger;
352
- #resumeLedgerExplicit;
353
406
  // The host DO's pubsub identity (RunnerRuntimeOptions.pubsub), threaded into
354
407
  // both createRun sites below (CI-M-002-002) so a configured host publishes and
355
408
  // replays on ONE shared feed. Undefined ⇒ core defaults a fresh emitter per
@@ -360,8 +413,6 @@ export class RunnerRuntime {
360
413
  this.#storage = options.storage;
361
414
  this.#logger = options.logger ?? false;
362
415
  this.#requestContextForRun = options.requestContextForRun;
363
- this.#resumeLedgerExplicit = options.resumeLedger !== undefined;
364
- this.#resumeLedger = options.resumeLedger ?? new InMemoryResumeLedger();
365
416
  this.#pubsub = options.pubsub;
366
417
  }
367
418
  /**
@@ -373,22 +424,6 @@ export class RunnerRuntime {
373
424
  get pubsub() {
374
425
  return this.#pubsub;
375
426
  }
376
- /**
377
- * Host-shell seam: replace the DEFAULT in-memory ledger with a durable one.
378
- * The DO shell calls this with a ctx.storage-backed ledger right after
379
- * build(), so every DO host gets eviction-proof ordinals without threading
380
- * a parameter (a forgettable thread is how the durability guarantee rots).
381
- * A ledger explicitly injected via options wins; adoption is rejected once
382
- * runs may have consulted the ledger.
383
- */
384
- adoptDefaultResumeLedger(ledger) {
385
- if (this.#mastra) {
386
- throw new Error('RunnerRuntime: adopt the resume ledger before the first run — ordinals already read from the default ledger would be lost');
387
- }
388
- if (this.#resumeLedgerExplicit)
389
- return;
390
- this.#resumeLedger = ledger;
391
- }
392
427
  /**
393
428
  * Register an agent that a runtime-owned workflow resolves by id.
394
429
  *
@@ -409,7 +444,7 @@ export class RunnerRuntime {
409
444
  if (this.#mastra) {
410
445
  throw new Error('RunnerRuntime: register all workflows before the first run — the Mastra instance is frozen once runs start');
411
446
  }
412
- if (!PATH_SAFE_ID_PATTERN.test(workflow.id)) {
447
+ if (!isPathSafeId(workflow.id)) {
413
448
  throw new Error(`RunnerRuntime: workflow id '${workflow.id}' must be URL-path-safe (letters, digits, '.', '_', '~', '-'; 1-200 chars) — it feeds the DO name join and the /runs/:workflowId/:runId path`);
414
449
  }
415
450
  if (this.#workflows.has(workflow.id)) {
@@ -432,11 +467,26 @@ export class RunnerRuntime {
432
467
  // mint a run keyed by the number 123, unreachable by the string "123" the
433
468
  // URL path later carries. There is NO generation fallback (INV-1): a
434
469
  // missing/null runId is a client error, not a request for one.
435
- if (typeof options.runId !== 'string' ||
436
- !PATH_SAFE_ID_PATTERN.test(options.runId)) {
470
+ if (!isPathSafeId(options.runId)) {
437
471
  throw new InvalidRunRequestError("runId is required and must be URL-path-safe (letters, digits, '.', '_', '~', '-'; 1–200 chars)");
438
472
  }
439
473
  const runId = options.runId;
474
+ if (options.requestedBy !== undefined &&
475
+ !isExecutionPrincipalId(options.requestedBy)) {
476
+ throw new InvalidRunRequestError('requestedBy is malformed');
477
+ }
478
+ if (options.requestedByKind !== undefined &&
479
+ !isExecutionPrincipalKind(options.requestedByKind)) {
480
+ throw new InvalidRunRequestError('requestedByKind is malformed');
481
+ }
482
+ if ((options.requestedBy === undefined) !==
483
+ (options.requestedByKind === undefined)) {
484
+ throw new InvalidRunRequestError('requestedBy and requestedByKind must be provided together');
485
+ }
486
+ if (options.attemptToken !== undefined &&
487
+ !isPathSafeId(options.attemptToken)) {
488
+ throw new InvalidRunRequestError('attemptToken is malformed');
489
+ }
440
490
  return this.#withRunLock(workflowId, runId, async () => {
441
491
  // Supplied ids can collide with an existing run; starting it
442
492
  // again would re-execute already-executed steps.
@@ -448,9 +498,20 @@ export class RunnerRuntime {
448
498
  // initial snapshot, so a provider failure after it would strand a
449
499
  // pending-but-never-started run (a supplied runId would then be locked
450
500
  // out by RunAlreadyExistsError on retry). Failing here leaves no state.
451
- const requestContext = await this.#requestContextFor(workflowId, runId, {
452
- kind: 'start',
453
- });
501
+ const startAttemptToken = options.attemptToken ?? crypto.randomUUID();
502
+ const provenance = {
503
+ version: 1,
504
+ ...(options.requestedBy === undefined
505
+ ? {}
506
+ : {
507
+ requestedBy: options.requestedBy,
508
+ requestedByKind: options.requestedByKind,
509
+ }),
510
+ startToken: startAttemptToken,
511
+ attemptToken: startAttemptToken,
512
+ resumeCounts: [],
513
+ };
514
+ const requestContext = await this.#requestContextFor(workflowId, runId, { kind: 'start' }, provenance, options.storedRequestContext);
454
515
  // Thread the host DO's pubsub identity into the run (CI-M-002-002). Core
455
516
  // accepts `createRun({ runId, pubsub })` at every one of its OWN call
456
517
  // sites (agent/durable index.js:5224/5541) and stamps it straight onto
@@ -465,14 +526,26 @@ export class RunnerRuntime {
465
526
  try {
466
527
  result = await run.start({
467
528
  inputData: options.inputData,
529
+ initialState: options.initialState,
468
530
  requestContext,
469
531
  });
470
532
  }
471
533
  catch (error) {
534
+ const recovered = await this.#summaryForAttempt(workflow, runId, provenance.attemptToken);
535
+ if (recovered)
536
+ return recovered;
472
537
  throw asClientError(error) ?? error;
473
538
  }
474
- await this.#reconcileTerminalState(workflowId, run.runId, result);
475
- return summarize(run.runId, result);
539
+ try {
540
+ await this.#reconcileTerminalState(workflowId, run.runId, result, requestContext);
541
+ }
542
+ catch (error) {
543
+ const recovered = await this.#summaryForAttempt(workflow, runId, provenance.attemptToken);
544
+ if (recovered)
545
+ return recovered;
546
+ throw error;
547
+ }
548
+ return summarize(run.runId, result, undefined, provenance.requestedBy, provenance.requestedByKind);
476
549
  });
477
550
  }
478
551
  /**
@@ -483,8 +556,7 @@ export class RunnerRuntime {
483
556
  async resume(workflowId, runId, options = {}) {
484
557
  const workflow = this.#getWorkflow(workflowId);
485
558
  return this.#withRunLock(workflowId, runId, async () => {
486
- const runKey = this.#runKey(workflowId, runId);
487
- const state = await workflow.getWorkflowRunById(runId);
559
+ const state = await this.#workflowState(workflow, runId);
488
560
  if (!state)
489
561
  throw new UnknownRunError(workflowId, runId);
490
562
  if (state.status !== 'suspended') {
@@ -493,7 +565,11 @@ export class RunnerRuntime {
493
565
  // Provider before createRun for symmetry with start(): a resume-time
494
566
  // createRun only reattaches (no snapshot write), but failing before it
495
567
  // still does the least work and keeps the ordering invariant uniform.
496
- const { stepKey, priorCounts, requestContext } = await this.#trustedResumePreparation(workflowId, runId, state, options.step);
568
+ const { nextCounts, provenance, requestContext } = await this.#trustedResumePreparation(workflowId, runId, state, options.step, options.requestedBy, options.requestedByKind);
569
+ if (options.prepareExecution) {
570
+ const preparationValues = structuredClone(Object.fromEntries(requestContext.entries()));
571
+ await options.prepareExecution(new RequestContext(Object.entries(preparationValues)));
572
+ }
497
573
  // Same host pubsub identity as start() (CI-M-002-002) — see the note
498
574
  // there; undefined stays byte-identical to `createRun({ runId })`.
499
575
  const run = await workflow.createRun({ runId, pubsub: this.#pubsub });
@@ -506,58 +582,64 @@ export class RunnerRuntime {
506
582
  });
507
583
  }
508
584
  catch (error) {
509
- // The run stayed suspended (resume threw) — do NOT increment; the
510
- // prior count must persist for a later retry of this same suspension.
585
+ const recovered = await this.#summaryForAttempt(workflow, runId, provenance.attemptToken);
586
+ if (recovered)
587
+ return recovered;
588
+ // No authoritative snapshot carries this attempt token: the run stayed
589
+ // on its prior suspension, so neither requester nor ordinal advances.
511
590
  throw asClientError(error) ?? error;
512
591
  }
513
- // Record THIS resume AFTER the leg fingerprint read above and BEFORE
514
- // summarize: a re-suspension produced by this resume must capture the
515
- // incremented ordinal so its approval binds to the new suspension.
516
- // priorCounts rides along so a durable ledger need not re-read what the
517
- // per-run FIFO lock guarantees unchanged since the read above.
518
- let counts = priorCounts;
519
- if (stepKey !== undefined) {
520
- counts = await this.#resumeLedger.increment(runKey, stepKey, priorCounts ?? new Map());
592
+ // A re-suspension produced by this resume carries the incremented ordinal
593
+ // in the same authoritative snapshot as the new workflow state.
594
+ const summary = summarize(run.runId, result, nextCounts, provenance.requestedBy, provenance.requestedByKind);
595
+ try {
596
+ await this.#reconcileTerminalState(workflowId, run.runId, result, requestContext);
521
597
  }
522
- const summary = summarize(run.runId, result, counts);
523
- await this.#reconcileTerminalState(workflowId, run.runId, result);
524
- // Terminal run: drop the ledger (no further suspension can occur).
525
- if (summary.status !== 'suspended') {
526
- await this.#resumeLedger.delete(runKey);
598
+ catch (error) {
599
+ const recovered = await this.#summaryForAttempt(workflow, runId, provenance.attemptToken);
600
+ if (recovered)
601
+ return recovered;
602
+ throw error;
527
603
  }
528
604
  return summary;
529
605
  });
530
606
  }
531
607
  async status(workflowId, runId) {
532
608
  const workflow = this.#getWorkflow(workflowId);
533
- const state = await workflow.getWorkflowRunById(runId);
609
+ const state = await this.#workflowState(workflow, runId);
534
610
  if (!state)
535
611
  return null;
536
- // Ledger read only for SUSPENDED runs — the one branch that projects
537
- // resumeCount (defense in depth: a bridge that ever mints off status()
538
- // must not see a stale/absent ordinal). Every other status would discard
539
- // the result, and under the DO shell each read is a billed
540
- // ctx.storage.get the SPA's polling would pay on every terminal run.
541
- const counts = state.status === 'suspended'
542
- ? await this.#resumeLedger.counts(this.#runKey(workflowId, runId))
543
- : undefined;
544
- return summarizeState(runId, state, counts);
612
+ const provenance = runProvenance(state);
613
+ // Snapshot provenance projects requester and resume ordinal alongside the
614
+ // authoritative workflow state, including after isolate eviction.
615
+ const counts = provenance ? new Map(provenance.resumeCounts) : undefined;
616
+ return summarizeState(runId, state, counts, provenance?.requestedBy, provenance?.requestedByKind);
545
617
  }
546
618
  /**
547
- * Derive the exact trusted context the next resume leg will receive without
548
- * advancing the run. Durable-agent hosts use it while rehydrating the
549
- * in-process registry, then call resume(); resume independently re-derives
550
- * the same suspension fingerprint before execution.
619
+ * Reconcile an interrupted start against the token stored in the
620
+ * authoritative workflow snapshot. Mastra's `createRun()` first persists a
621
+ * tokenless pending shell; if no executed snapshot replaced it, the shell is
622
+ * abandoned and must not become a successful FlowSafe run.
551
623
  */
552
- async trustedRequestContextForResume(workflowId, runId, options = {}) {
553
- const workflow = this.#getWorkflow(workflowId);
554
- const state = await workflow.getWorkflowRunById(runId);
555
- if (!state)
556
- throw new UnknownRunError(workflowId, runId);
557
- if (state.status !== 'suspended') {
558
- throw new RunNotSuspendedError(workflowId, runId, state.status);
624
+ async recoverStartAttempt(workflowId, runId, attemptToken) {
625
+ if (!isPathSafeId(attemptToken)) {
626
+ throw new InvalidRunRequestError('attemptToken is malformed');
559
627
  }
560
- return (await this.#trustedResumePreparation(workflowId, runId, state, options.step)).requestContext;
628
+ const workflow = this.#getWorkflow(workflowId);
629
+ return this.#withRunLock(workflowId, runId, async () => {
630
+ const state = await this.#workflowState(workflow, runId);
631
+ if (!state)
632
+ return null;
633
+ const provenance = runProvenance(state);
634
+ if (provenance?.startToken === attemptToken) {
635
+ return summarizeState(runId, state, new Map(provenance.resumeCounts), provenance.requestedBy, provenance.requestedByKind);
636
+ }
637
+ if (state.status === 'pending' && provenance === undefined) {
638
+ await workflow.deleteWorkflowRunById(runId);
639
+ return null;
640
+ }
641
+ throw new Error(`run '${runId}' snapshot belongs to another start attempt`);
642
+ });
561
643
  }
562
644
  // A provider crash propagates (fail loud): silently starting the leg with
563
645
  // fewer capabilities than intended would mask the fault. Missing grants can
@@ -569,41 +651,27 @@ export class RunnerRuntime {
569
651
  // the scope is never client-suppliable. Provider values are partitioned into
570
652
  // stored application context, capabilities, and trusted identity so every
571
653
  // layer has an explicit order and a provider cannot replace runtime scope.
572
- async #requestContextFor(workflowId, runId, leg) {
654
+ async #requestContextFor(workflowId, runId, leg, provenance, storedRequestContext) {
573
655
  const base = {
574
656
  [BREAKWATER_WORKFLOW_SCOPE_KEY]: workflowId,
575
657
  runId,
576
658
  };
577
- // Tenant-salted runs (INV-1: `${tenantId}_${uuid}`) also mint the OPAQUE
578
- // isolation scope, segmenting breakwater's connector idempotency and
579
- // rate-limit keys per tenant. Server-authoritative like the workflow
580
- // scope above — the runId was minted from the AUTHENTICATED tenant, so
581
- // its prefix is trustworthy here.
582
- //
583
- // Structural, not flagged: a runId shaped `<inv3-slug>_<rest>` mints a
584
- // scope. For this repo's hosts that is exactly the tenant (they all mint
585
- // via TenantContext.newRunId). CONSTRAINT for standalone OSS consumers
586
- // minting their own runIds: the scope segments breakwater's CROSS-RUN
587
- // idempotency and rate-limit keys, so one logical account must keep ONE
588
- // stable pre-`_` prefix (or avoid `_`-prefixed runIds entirely) — ids
589
- // like `daily_<uuid>` vs `weekly_<uuid>` would split that account's key
590
- // namespace per prefix, and a cross-run business key ("invoice-2026-07")
591
- // would fire once per prefix instead of once. Never a cross-tenant leak
592
- // (splitting only narrows sharing), but a real duplication hazard.
593
- // Non-matching runIds mint nothing: the single-tenant keys stay
594
- // byte-identical, and deployments that must never run scope-less enforce
595
- // that with breakwater's tenantIsolation evaluator (which binds dry-run
596
- // too, unlike the idempotency path).
597
- const tenantId = tenantOfRunId(runId);
598
- if (tenantId !== undefined) {
599
- base[BREAKWATER_ISOLATION_SCOPE_KEY] = tenantId;
659
+ if (provenance !== undefined) {
660
+ base[RUN_PROVENANCE_CONTEXT_KEY] = provenance;
600
661
  }
662
+ // No isolation scope is minted here: a deployment serves exactly one
663
+ // organization, so breakwater's connector idempotency and rate-limit keys
664
+ // are deployment-wide by construction. Budget partitioning within a
665
+ // deployment stays available
666
+ // through breakwater's crossWorkflowIsolation, which reads the workflow
667
+ // scope minted above. The isolation-scope context key remains RESERVED —
668
+ // orderedRequestContext drops it from provider values — so a provider can
669
+ // never mint a scope that desyncs from the execution identity below.
601
670
  if (leg.kind === 'start') {
602
671
  base[BREAKWATER_CONNECTOR_EXECUTION_KEY] = {
603
672
  kind: 'start',
604
673
  workflowId,
605
674
  runId,
606
- ...(tenantId === undefined ? {} : { isolationScope: tenantId }),
607
675
  };
608
676
  }
609
677
  else if (leg.step !== undefined && leg.suspendedAt !== undefined) {
@@ -611,7 +679,6 @@ export class RunnerRuntime {
611
679
  kind: 'resume',
612
680
  workflowId,
613
681
  runId,
614
- ...(tenantId === undefined ? {} : { isolationScope: tenantId }),
615
682
  suspension: {
616
683
  stepPath: [...leg.step],
617
684
  suspendedAt: leg.suspendedAt,
@@ -627,24 +694,75 @@ export class RunnerRuntime {
627
694
  const values = this.#requestContextForRun
628
695
  ? await this.#requestContextForRun(workflowId, runId, leg)
629
696
  : undefined;
630
- return orderedRequestContext(base, values);
697
+ return orderedRequestContext(base, values, storedRequestContext);
631
698
  }
632
- async #trustedResumePreparation(workflowId, runId, state, selectedStep) {
699
+ async #trustedResumePreparation(workflowId, runId, state, selectedStep, requestedBy, requestedByKind) {
700
+ if (requestedBy !== undefined && !isExecutionPrincipalId(requestedBy)) {
701
+ throw new InvalidRunRequestError('requestedBy is malformed');
702
+ }
703
+ if (requestedByKind !== undefined &&
704
+ !isExecutionPrincipalKind(requestedByKind)) {
705
+ throw new InvalidRunRequestError('requestedByKind is malformed');
706
+ }
707
+ if ((requestedBy === undefined) !== (requestedByKind === undefined)) {
708
+ throw new InvalidRunRequestError('requestedBy and requestedByKind must be provided together');
709
+ }
633
710
  const step = resolveResumeStep(selectedStep, state);
634
711
  const stepKey = step?.join('.');
635
712
  // resumeCount is read BEFORE this resume increments it: it is the count
636
713
  // of prior resumes = the ordinal of the CURRENT suspension being resumed
637
714
  // (undefined for a first suspension), which the minting approval captured.
638
- const priorCounts = await this.#resumeLedger.counts(this.#runKey(workflowId, runId));
715
+ const storedProvenance = runProvenance(state);
716
+ if (requestedBy === undefined &&
717
+ storedProvenance?.requestedBy !== undefined &&
718
+ storedProvenance.requestedByKind === undefined) {
719
+ throw new InvalidRunRequestError('legacy requestedBy provenance requires an explicit requestedBy and requestedByKind to resume');
720
+ }
721
+ const requester = requestedBy ?? storedProvenance?.requestedBy;
722
+ const requesterKind = requestedBy === undefined
723
+ ? storedProvenance?.requestedByKind
724
+ : requestedByKind;
725
+ const priorCounts = new Map(storedProvenance?.resumeCounts ?? []);
726
+ const nextCounts = new Map(priorCounts);
727
+ if (stepKey !== undefined) {
728
+ nextCounts.set(stepKey, (nextCounts.get(stepKey) ?? 0) + 1);
729
+ }
730
+ const provenance = {
731
+ version: 1,
732
+ ...(requester === undefined
733
+ ? {}
734
+ : { requestedBy: requester, requestedByKind: requesterKind }),
735
+ startToken: storedProvenance?.startToken ?? crypto.randomUUID(),
736
+ attemptToken: crypto.randomUUID(),
737
+ resumeCounts: [...nextCounts],
738
+ };
639
739
  const requestContext = await this.#requestContextFor(workflowId, runId, {
640
740
  kind: 'resume',
641
741
  step,
642
- suspendedAt: stepKey !== undefined ? suspendedAtOf(state.steps, stepKey) : undefined,
742
+ suspendedAt: stepKey !== undefined
743
+ ? suspendedAtOf(state.steps, stepKey)
744
+ : undefined,
643
745
  resumeCount: stepKey !== undefined ? priorCounts?.get(stepKey) : undefined,
644
- });
645
- return { stepKey, priorCounts, requestContext };
746
+ }, provenance);
747
+ return {
748
+ nextCounts,
749
+ provenance,
750
+ requestContext,
751
+ };
752
+ }
753
+ #workflowState(workflow, runId) {
754
+ return workflow.getWorkflowRunById(runId, { fields: RUN_STATE_FIELDS });
646
755
  }
647
- async #reconcileTerminalState(workflowId, runId, result) {
756
+ async #summaryForAttempt(workflow, runId, attemptToken) {
757
+ const persisted = await this.#workflowState(workflow, runId);
758
+ if (!persisted)
759
+ return undefined;
760
+ const provenance = runProvenance(persisted);
761
+ if (provenance?.attemptToken !== attemptToken)
762
+ return undefined;
763
+ return summarizeState(runId, persisted, new Map(provenance.resumeCounts), provenance.requestedBy, provenance.requestedByKind);
764
+ }
765
+ async #reconcileTerminalState(workflowId, runId, result, requestContext) {
648
766
  const opts = terminalStateUpdate(result);
649
767
  if (!opts)
650
768
  return;
@@ -659,14 +777,34 @@ export class RunnerRuntime {
659
777
  if (!snapshot) {
660
778
  throw new Error(`RunnerRuntime: run '${runId}' of workflow '${workflowId}' completed without a durable snapshot`);
661
779
  }
662
- if (snapshot.status === result.status)
780
+ const persistedContext = snapshot.requestContext !== null &&
781
+ typeof snapshot.requestContext === 'object' &&
782
+ !Array.isArray(snapshot.requestContext)
783
+ ? snapshot.requestContext
784
+ : {};
785
+ // Core merges resume context over the prior snapshot. Terminal-only repair
786
+ // must persist that same effective context, including application keys the
787
+ // current provider intentionally omitted.
788
+ const authoritativeContext = {
789
+ ...persistedContext,
790
+ ...Object.fromEntries(requestContext.entries()),
791
+ };
792
+ const persistedToken = runProvenance(snapshot)?.attemptToken;
793
+ const authoritativeToken = runProvenance({
794
+ ...snapshot,
795
+ requestContext: authoritativeContext,
796
+ })?.attemptToken;
797
+ if (snapshot.status === result.status &&
798
+ persistedToken === authoritativeToken) {
663
799
  return;
800
+ }
664
801
  await workflows.persistWorkflowSnapshot({
665
802
  workflowName: workflowId,
666
803
  runId,
667
804
  snapshot: {
668
805
  ...snapshot,
669
806
  ...opts,
807
+ requestContext: authoritativeContext,
670
808
  timestamp: Date.now(),
671
809
  },
672
810
  });
@@ -696,9 +834,9 @@ export class RunnerRuntime {
696
834
  // The run's full identity as a single map key: workflowId + runId, never runId
697
835
  // alone. The same caller-supplied runId under two workflows are DISTINCT
698
836
  // persisted runs (Mastra snapshots key on workflowName+runId) and must never
699
- // share a per-run entry — the FIFO lock OR the resume ledger. Composing the key
700
- // in ONE place keeps every per-run map keyed identically, so a future per-run
701
- // map cannot reintroduce a runId-only key (the grant leak the ledger guards).
837
+ // share a per-run FIFO entry. Composing the key in ONE place keeps every
838
+ // per-run map keyed identically, so a future map cannot reintroduce a
839
+ // runId-only key and cross workflow boundaries.
702
840
  // This is the exact string the DO name join produces
703
841
  // (idFromName(`${workflowId}:${runId}`)); PATH_SAFE_ID_PATTERN excludes ':'
704
842
  // from both ids, so the join is unambiguous.