@proofoftech/flowsafe 0.11.0 → 0.13.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 (388) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +91 -44
  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 +51 -189
  147. package/dist/do-runner/d1-storage.d.ts.map +1 -1
  148. package/dist/do-runner/d1-storage.js +111 -362
  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/table-prefix.d.ts +3 -0
  201. package/dist/do-runner/table-prefix.d.ts.map +1 -0
  202. package/dist/do-runner/table-prefix.js +17 -0
  203. package/dist/do-runner/table-prefix.js.map +1 -0
  204. package/dist/do-runner/thread-do.d.ts +18 -32
  205. package/dist/do-runner/thread-do.d.ts.map +1 -1
  206. package/dist/do-runner/thread-do.js +52 -74
  207. package/dist/do-runner/thread-do.js.map +1 -1
  208. package/dist/goals/objective-routes.d.ts +7 -4
  209. package/dist/goals/objective-routes.d.ts.map +1 -1
  210. package/dist/goals/objective-routes.js +58 -43
  211. package/dist/goals/objective-routes.js.map +1 -1
  212. package/dist/host-kit/approval-bridge.d.ts +18 -21
  213. package/dist/host-kit/approval-bridge.d.ts.map +1 -1
  214. package/dist/host-kit/approval-bridge.js +37 -37
  215. package/dist/host-kit/approval-bridge.js.map +1 -1
  216. package/dist/host-kit/bearer-auth.d.ts +3 -4
  217. package/dist/host-kit/bearer-auth.d.ts.map +1 -1
  218. package/dist/host-kit/bearer-auth.js +7 -11
  219. package/dist/host-kit/bearer-auth.js.map +1 -1
  220. package/dist/host-kit/do-run-topology.d.ts +14 -5
  221. package/dist/host-kit/do-run-topology.d.ts.map +1 -1
  222. package/dist/host-kit/do-run-topology.js +47 -15
  223. package/dist/host-kit/do-run-topology.js.map +1 -1
  224. package/dist/host-kit/env-vars.d.ts +1 -1
  225. package/dist/host-kit/env-vars.js +1 -1
  226. package/dist/host-kit/flowsafe-worker.d.ts +159 -80
  227. package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
  228. package/dist/host-kit/flowsafe-worker.js +629 -142
  229. package/dist/host-kit/flowsafe-worker.js.map +1 -1
  230. package/dist/host-kit/host-approval-service.d.ts +62 -54
  231. package/dist/host-kit/host-approval-service.d.ts.map +1 -1
  232. package/dist/host-kit/host-approval-service.js +90 -61
  233. package/dist/host-kit/host-approval-service.js.map +1 -1
  234. package/dist/host-kit/hub-topology.d.ts +6 -7
  235. package/dist/host-kit/hub-topology.d.ts.map +1 -1
  236. package/dist/host-kit/hub-topology.js +16 -12
  237. package/dist/host-kit/hub-topology.js.map +1 -1
  238. package/dist/host-kit/index.d.ts +13 -11
  239. package/dist/host-kit/index.d.ts.map +1 -1
  240. package/dist/host-kit/index.js +9 -12
  241. package/dist/host-kit/index.js.map +1 -1
  242. package/dist/host-kit/maintenance-capability.d.ts +58 -0
  243. package/dist/host-kit/maintenance-capability.d.ts.map +1 -0
  244. package/dist/host-kit/maintenance-capability.js +268 -0
  245. package/dist/host-kit/maintenance-capability.js.map +1 -0
  246. package/dist/host-kit/memory-boundary.d.ts +4 -19
  247. package/dist/host-kit/memory-boundary.d.ts.map +1 -1
  248. package/dist/host-kit/memory-boundary.js +12 -27
  249. package/dist/host-kit/memory-boundary.js.map +1 -1
  250. package/dist/host-kit/resource-access.d.ts +4 -0
  251. package/dist/host-kit/resource-access.d.ts.map +1 -0
  252. package/dist/host-kit/resource-access.js +12 -0
  253. package/dist/host-kit/resource-access.js.map +1 -0
  254. package/dist/host-kit/run-router.d.ts +25 -15
  255. package/dist/host-kit/run-router.d.ts.map +1 -1
  256. package/dist/host-kit/run-router.js +61 -46
  257. package/dist/host-kit/run-router.js.map +1 -1
  258. package/dist/host-kit/state-egress.d.ts +51 -0
  259. package/dist/host-kit/state-egress.d.ts.map +1 -0
  260. package/dist/host-kit/state-egress.js +120 -0
  261. package/dist/host-kit/state-egress.js.map +1 -0
  262. package/dist/host-kit/stream-router.d.ts +9 -4
  263. package/dist/host-kit/stream-router.d.ts.map +1 -1
  264. package/dist/host-kit/stream-router.js +45 -38
  265. package/dist/host-kit/stream-router.js.map +1 -1
  266. package/dist/host-kit/stream-ticket.d.ts +10 -10
  267. package/dist/host-kit/stream-ticket.d.ts.map +1 -1
  268. package/dist/host-kit/stream-ticket.js +60 -90
  269. package/dist/host-kit/stream-ticket.js.map +1 -1
  270. package/dist/host-kit/thread-topology.d.ts +17 -11
  271. package/dist/host-kit/thread-topology.d.ts.map +1 -1
  272. package/dist/host-kit/thread-topology.js +33 -59
  273. package/dist/host-kit/thread-topology.js.map +1 -1
  274. package/dist/host-kit/verifier.d.ts +6 -6
  275. package/dist/host-kit/verifier.d.ts.map +1 -1
  276. package/dist/host-kit/verifier.js +51 -119
  277. package/dist/host-kit/verifier.js.map +1 -1
  278. package/dist/http-body.d.ts +20 -0
  279. package/dist/http-body.d.ts.map +1 -0
  280. package/dist/http-body.js +67 -0
  281. package/dist/http-body.js.map +1 -0
  282. package/dist/schedules/index.d.ts +3 -2
  283. package/dist/schedules/index.d.ts.map +1 -1
  284. package/dist/schedules/index.js +5 -4
  285. package/dist/schedules/index.js.map +1 -1
  286. package/dist/schedules/router.d.ts +16 -10
  287. package/dist/schedules/router.d.ts.map +1 -1
  288. package/dist/schedules/router.js +153 -91
  289. package/dist/schedules/router.js.map +1 -1
  290. package/dist/schedules/schedules-d1.d.ts +88 -0
  291. package/dist/schedules/schedules-d1.d.ts.map +1 -1
  292. package/dist/schedules/schedules-d1.js +411 -50
  293. package/dist/schedules/schedules-d1.js.map +1 -1
  294. package/dist/schedules/target-policy.d.ts +29 -0
  295. package/dist/schedules/target-policy.d.ts.map +1 -0
  296. package/dist/schedules/target-policy.js +70 -0
  297. package/dist/schedules/target-policy.js.map +1 -0
  298. package/dist/schedules/tick.d.ts +125 -21
  299. package/dist/schedules/tick.d.ts.map +1 -1
  300. package/dist/schedules/tick.js +637 -160
  301. package/dist/schedules/tick.js.map +1 -1
  302. package/dist/signal-providers/delivery.d.ts +0 -19
  303. package/dist/signal-providers/delivery.d.ts.map +1 -1
  304. package/dist/signal-providers/delivery.js +5 -54
  305. package/dist/signal-providers/delivery.js.map +1 -1
  306. package/dist/signal-providers/github-provider.d.ts.map +1 -1
  307. package/dist/signal-providers/github-provider.js +9 -16
  308. package/dist/signal-providers/github-provider.js.map +1 -1
  309. package/dist/signal-providers/host-do.d.ts +3 -45
  310. package/dist/signal-providers/host-do.d.ts.map +1 -1
  311. package/dist/signal-providers/host-do.js +86 -106
  312. package/dist/signal-providers/host-do.js.map +1 -1
  313. package/dist/signal-providers/host-topology.d.ts +5 -14
  314. package/dist/signal-providers/host-topology.d.ts.map +1 -1
  315. package/dist/signal-providers/host-topology.js +11 -25
  316. package/dist/signal-providers/host-topology.js.map +1 -1
  317. package/dist/signal-providers/index.d.ts +3 -3
  318. package/dist/signal-providers/index.d.ts.map +1 -1
  319. package/dist/signal-providers/index.js +3 -3
  320. package/dist/signal-providers/index.js.map +1 -1
  321. package/dist/signal-providers/provider.d.ts +4 -5
  322. package/dist/signal-providers/provider.d.ts.map +1 -1
  323. package/dist/signal-providers/provider.js +5 -6
  324. package/dist/signal-providers/provider.js.map +1 -1
  325. package/dist/signal-providers/subscription-d1.d.ts +10 -57
  326. package/dist/signal-providers/subscription-d1.d.ts.map +1 -1
  327. package/dist/signal-providers/subscription-d1.js +208 -155
  328. package/dist/signal-providers/subscription-d1.js.map +1 -1
  329. package/dist/signal-providers/webhook-route.d.ts +28 -14
  330. package/dist/signal-providers/webhook-route.d.ts.map +1 -1
  331. package/dist/signal-providers/webhook-route.js +117 -88
  332. package/dist/signal-providers/webhook-route.js.map +1 -1
  333. package/dist/signals/client.js +8 -8
  334. package/dist/signals/client.js.map +1 -1
  335. package/dist/signals/d1-shared.js +1 -1
  336. package/dist/signals/d1-shared.js.map +1 -1
  337. package/dist/signals/index.d.ts +1 -1
  338. package/dist/signals/index.d.ts.map +1 -1
  339. package/dist/signals/index.js.map +1 -1
  340. package/dist/signals/notification-dispatch.d.ts +12 -6
  341. package/dist/signals/notification-dispatch.d.ts.map +1 -1
  342. package/dist/signals/notification-dispatch.js +37 -21
  343. package/dist/signals/notification-dispatch.js.map +1 -1
  344. package/dist/signals/notifications-d1.d.ts +5 -8
  345. package/dist/signals/notifications-d1.d.ts.map +1 -1
  346. package/dist/signals/notifications-d1.js +8 -13
  347. package/dist/signals/notifications-d1.js.map +1 -1
  348. package/dist/signals/router.d.ts +10 -10
  349. package/dist/signals/router.d.ts.map +1 -1
  350. package/dist/signals/router.js +49 -51
  351. package/dist/signals/router.js.map +1 -1
  352. package/dist/signals/thread-do-routes.d.ts +74 -15
  353. package/dist/signals/thread-do-routes.d.ts.map +1 -1
  354. package/dist/signals/thread-do-routes.js +484 -41
  355. package/dist/signals/thread-do-routes.js.map +1 -1
  356. package/dist/signals/thread-state-d1.d.ts.map +1 -1
  357. package/dist/signals/thread-state-d1.js +2 -3
  358. package/dist/signals/thread-state-d1.js.map +1 -1
  359. package/package.json +29 -6
  360. package/scripts/seed-deployment-identity.mjs +252 -0
  361. package/dist/approval-api/tenant-brand.d.ts +0 -35
  362. package/dist/approval-api/tenant-brand.d.ts.map +0 -1
  363. package/dist/approval-api/tenant-brand.js +0 -19
  364. package/dist/approval-api/tenant-brand.js.map +0 -1
  365. package/dist/approval-api/tenant-context.d.ts +0 -84
  366. package/dist/approval-api/tenant-context.d.ts.map +0 -1
  367. package/dist/approval-api/tenant-context.js +0 -112
  368. package/dist/approval-api/tenant-context.js.map +0 -1
  369. package/dist/approval-api/tenant-store.d.ts +0 -36
  370. package/dist/approval-api/tenant-store.d.ts.map +0 -1
  371. package/dist/approval-api/tenant-store.js +0 -129
  372. package/dist/approval-api/tenant-store.js.map +0 -1
  373. package/dist/do-runner/resume-ledger.d.ts +0 -48
  374. package/dist/do-runner/resume-ledger.d.ts.map +0 -1
  375. package/dist/do-runner/resume-ledger.js +0 -60
  376. package/dist/do-runner/resume-ledger.js.map +0 -1
  377. package/dist/do-runner/thread-header.d.ts +0 -31
  378. package/dist/do-runner/thread-header.d.ts.map +0 -1
  379. package/dist/do-runner/thread-header.js +0 -39
  380. package/dist/do-runner/thread-header.js.map +0 -1
  381. package/dist/host-kit/subdomain-check.d.ts +0 -23
  382. package/dist/host-kit/subdomain-check.d.ts.map +0 -1
  383. package/dist/host-kit/subdomain-check.js +0 -61
  384. package/dist/host-kit/subdomain-check.js.map +0 -1
  385. package/dist/host-kit/tenant-registry.d.ts +0 -51
  386. package/dist/host-kit/tenant-registry.d.ts.map +0 -1
  387. package/dist/host-kit/tenant-registry.js +0 -88
  388. package/dist/host-kit/tenant-registry.js.map +0 -1
@@ -2,9 +2,10 @@
2
2
  import { AGENT_STREAM_TOPIC } from '@mastra/core/agent/durable';
3
3
  import { Mastra } from '@mastra/core/mastra';
4
4
  import { isPrincipalPermissions } from '@proofoftech/breakwater/rbac';
5
- import { bindAgentThread, createFlowsafeDurableAgent, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, deleteAgentRunRecord, readAgentRunRecord, readAgentThreadBinding, writeAgentRunRecord, } from '../agent-runner/index.js';
6
- import { principalActor, principalAuditFields, RUN_START_ROLES, samePrincipal, } from '../approval-api/index.js';
7
- import { DoStatusError, mintResourceId, tenantOwnsMemoryId, tenantOwnsSaltedId, } from '../do-runner/index.js';
5
+ import { AGENT_ENTRY_PATHS, AGENT_RUN_STORAGE_KEY_PREFIX, bindAgentThread, createFlowsafeDurableAgent, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, deleteAgentRunRecord, deleteAgentThreadBinding, readAgentRunRecord, readAgentThreadBinding, writeAgentRunRecord, } from '../agent-runner/index.js';
6
+ import { canonicalResourceOwner, principalActor, principalAuditFields, principalOwner, RUN_START_ROLES, samePrincipal, } from '../approval-api/index.js';
7
+ import { isExecutionPrincipalId, } from '../approval-api/principal.js';
8
+ import { DoStatusError, isPathSafeId, resolveScheduleStartOwner, resourceIdFromKey, } from '../do-runner/index.js';
8
9
  import { mastraRegistryEntries } from '../do-runner/mastra-registry.js';
9
10
  import { reconcileApprovalsForSummary } from '../host-kit/index.js';
10
11
  import { createAgentModuleCatalog } from './catalog.js';
@@ -59,24 +60,14 @@ const TERMINAL_RUN_STATUSES = [
59
60
  'bailed',
60
61
  'skipped',
61
62
  ];
63
+ const AGENT_OWNER_RECOVERY_PREFIX = 'flowsafe:agent-owner-recovery:v1:';
64
+ const AGENT_OWNER_RECOVERY_DELAY_MS = 60_000;
62
65
  function isTerminalRunStatus(status) {
63
66
  return TERMINAL_RUN_STATUSES.includes(status);
64
67
  }
65
68
  function entryPath(value) {
66
- const allowed = [
67
- 'http.start',
68
- 'approval.resume',
69
- 'signal.message',
70
- 'signal.queue',
71
- 'signal.reactive',
72
- 'signal.state',
73
- 'signal.notification',
74
- 'signal.wake',
75
- 'notification.dispatch',
76
- 'schedule.fire',
77
- ];
78
69
  if (typeof value !== 'string' ||
79
- !allowed.includes(value)) {
70
+ !AGENT_ENTRY_PATHS.includes(value)) {
80
71
  throw new AgentHostRequestError(400, 'entryPath is required');
81
72
  }
82
73
  return value;
@@ -88,14 +79,65 @@ function safeContext(value) {
88
79
  }
89
80
  throw new AgentHostRequestError(400, 'safeContext must be an object');
90
81
  }
82
+ function providerOptions(value) {
83
+ if (value === undefined)
84
+ return undefined;
85
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
86
+ throw new AgentHostRequestError(400, 'providerOptions must be an object');
87
+ }
88
+ return structuredClone(value);
89
+ }
90
+ function record(value) {
91
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
92
+ ? value
93
+ : undefined;
94
+ }
95
+ function scheduleAgentContext(target) {
96
+ const idle = record(target.ifIdle);
97
+ const streamOptions = record(idle?.streamOptions);
98
+ const streamContext = record(streamOptions?.requestContext);
99
+ return {
100
+ ...target.requestContext,
101
+ ...streamContext,
102
+ };
103
+ }
104
+ function resourceOwner(value) {
105
+ try {
106
+ return canonicalResourceOwner(value);
107
+ }
108
+ catch {
109
+ throw new AgentHostRequestError(400, 'resourceOwner is required');
110
+ }
111
+ }
112
+ function requestedBy(value) {
113
+ if (!isExecutionPrincipalId(value)) {
114
+ throw new AgentHostRequestError(400, 'requestedBy is malformed');
115
+ }
116
+ return value;
117
+ }
118
+ function createFifoLock() {
119
+ let tail = Promise.resolve();
120
+ return async (operation) => {
121
+ const previous = tail;
122
+ let release = () => undefined;
123
+ tail = new Promise((resolve) => {
124
+ release = resolve;
125
+ });
126
+ await previous;
127
+ try {
128
+ return await operation();
129
+ }
130
+ finally {
131
+ release();
132
+ }
133
+ };
134
+ }
91
135
  function runRef(scope, input) {
92
136
  const { agentId, threadId, resourceId, runId } = input;
93
- if (typeof agentId !== 'string' ||
137
+ if (!isPathSafeId(agentId) ||
94
138
  threadId !== scope.threadId ||
95
- typeof resourceId !== 'string' ||
96
- !tenantOwnsMemoryId(scope.tenantId, resourceId) ||
97
- typeof runId !== 'string' ||
98
- !tenantOwnsSaltedId(scope.tenantId, runId)) {
139
+ !isPathSafeId(resourceId) ||
140
+ !isPathSafeId(runId)) {
99
141
  throw new AgentHostRequestError(404, 'run not found');
100
142
  }
101
143
  return { agentId, threadId: scope.threadId, resourceId, runId };
@@ -155,11 +197,13 @@ export function createThreadAgentHost(options) {
155
197
  let runtime;
156
198
  const executions = new Map();
157
199
  let stableScope;
158
- let bindingTail = Promise.resolve();
200
+ const withBindingLock = createFifoLock();
201
+ const withDispatchLock = createFifoLock();
202
+ const withRecoveryLock = createFifoLock();
159
203
  const instanceScopeFor = (scope) => {
160
204
  if (stableScope) {
161
205
  if (stableScope.threadId !== scope.threadId ||
162
- stableScope.tenantId !== scope.tenantId ||
206
+ stableScope.deploymentTag !== scope.deploymentTag ||
163
207
  stableScope.init !== scope.init) {
164
208
  throw new Error('thread agent host cannot be shared across DO instances');
165
209
  }
@@ -167,25 +211,11 @@ export function createThreadAgentHost(options) {
167
211
  }
168
212
  stableScope = Object.freeze({
169
213
  threadId: scope.threadId,
170
- tenantId: scope.tenantId,
214
+ deploymentTag: scope.deploymentTag,
171
215
  init: scope.init,
172
216
  });
173
217
  return stableScope;
174
218
  };
175
- const withBindingLock = async (operation) => {
176
- const previous = bindingTail;
177
- let release = () => undefined;
178
- bindingTail = new Promise((resolve) => {
179
- release = resolve;
180
- });
181
- await previous;
182
- try {
183
- return await operation();
184
- }
185
- finally {
186
- release();
187
- }
188
- };
189
219
  const catalogFor = async (scope) => {
190
220
  catalogPromise ??= Promise.resolve(options.buildModules(instanceScopeFor(scope)))
191
221
  .then(createAgentModuleCatalog)
@@ -233,7 +263,8 @@ export function createThreadAgentHost(options) {
233
263
  * roles at all: it must be declared in the agent's `allowedAutomation` for
234
264
  * this precise entry path, AND survive the host's optional authorizer. Absent
235
265
  * declaration denies — which is why a scheduled start of an agent that has
236
- * not opted in fails here rather than executing as a synthetic operator.
266
+ * not opted in fails here rather than executing through a role-bearing
267
+ * human identity.
237
268
  *
238
269
  * After that kind-specific gate, a configured server-owned resolver runs for
239
270
  * EVERY granted entry: an agent that declares required permissions also
@@ -275,7 +306,7 @@ export function createThreadAgentHost(options) {
275
306
  principal,
276
307
  agentId,
277
308
  entryPath: entry,
278
- tenantId: scope.tenantId,
309
+ deploymentTag: scope.deploymentTag,
279
310
  threadId: scope.threadId,
280
311
  })) ?? true;
281
312
  granted = hostAllows;
@@ -307,7 +338,9 @@ export function createThreadAgentHost(options) {
307
338
  reason: 'permission resolution failed',
308
339
  detail: {
309
340
  agentId,
310
- tenantId: scope.tenantId,
341
+ ...(scope.deploymentTag !== undefined
342
+ ? { tenantId: scope.deploymentTag }
343
+ : {}),
311
344
  threadId: scope.threadId,
312
345
  entryPath: entry,
313
346
  ...principalAuditFields(principal),
@@ -340,7 +373,9 @@ export function createThreadAgentHost(options) {
340
373
  ...(reason !== undefined ? { reason } : {}),
341
374
  detail: {
342
375
  agentId,
343
- tenantId: scope.tenantId,
376
+ ...(scope.deploymentTag !== undefined
377
+ ? { tenantId: scope.deploymentTag }
378
+ : {}),
344
379
  threadId: scope.threadId,
345
380
  entryPath: entry,
346
381
  ...principalAuditFields(principal),
@@ -357,6 +392,134 @@ export function createThreadAgentHost(options) {
357
392
  };
358
393
  const readBinding = () => readAgentThreadBinding(options.stateStorage());
359
394
  const readRun = (runId) => readAgentRunRecord(options.stateStorage(), runId);
395
+ const findBlockingRun = async (scopeRuntime) => {
396
+ const storage = options.stateStorage();
397
+ const records = await storage.list({
398
+ prefix: AGENT_RUN_STORAGE_KEY_PREFIX,
399
+ });
400
+ for (const key of records.keys()) {
401
+ const runId = key.slice(AGENT_RUN_STORAGE_KEY_PREFIX.length);
402
+ const record = await readRun(runId);
403
+ if (!record)
404
+ continue;
405
+ const summary = await scopeRuntime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, runId);
406
+ if (executions.has(runId) ||
407
+ (summary !== null && !isTerminalRunStatus(summary.status))) {
408
+ return { runId, principal: record.principal };
409
+ }
410
+ const recovery = await storage.get(AGENT_OWNER_RECOVERY_PREFIX + runId);
411
+ if (recovery)
412
+ return { runId, principal: record.principal };
413
+ await deleteAgentRunRecord(storage, runId);
414
+ }
415
+ return undefined;
416
+ };
417
+ const ownerRecoveryKey = (runId) => AGENT_OWNER_RECOVERY_PREFIX + runId;
418
+ const resolveStartSource = async (scope, ref, entry, threaded, scheduleId, dispatchId) => {
419
+ const ownership = options.resourceAccess();
420
+ if (entry !== 'schedule.fire' &&
421
+ (scheduleId !== undefined || dispatchId !== undefined)) {
422
+ throw new AgentHostRequestError(404, 'run not found');
423
+ }
424
+ if (entry === 'schedule.fire') {
425
+ if (!isPathSafeId(scheduleId) || !isPathSafeId(dispatchId)) {
426
+ throw new AgentHostRequestError(404, 'run not found');
427
+ }
428
+ const schedules = options.scheduleSource?.();
429
+ const source = schedules
430
+ ? await resolveScheduleStartOwner(schedules, ownership, scheduleId, dispatchId, ref.runId, threaded
431
+ ? {
432
+ type: 'agent',
433
+ mode: 'threaded-wake',
434
+ agentId: ref.agentId,
435
+ threadId: ref.threadId,
436
+ resourceId: ref.resourceId,
437
+ }
438
+ : {
439
+ type: 'agent',
440
+ mode: 'threadless-start',
441
+ agentId: ref.agentId,
442
+ })
443
+ : undefined;
444
+ if (!source)
445
+ throw new AgentHostRequestError(404, 'run not found');
446
+ return source;
447
+ }
448
+ if (entry === 'http.start') {
449
+ return { owner: principalOwner(scope.principal) };
450
+ }
451
+ if (!threaded)
452
+ throw new AgentHostRequestError(404, 'run not found');
453
+ const [threadOwner, resourceOwner] = await Promise.all([
454
+ ownership.owner('thread', ref.threadId),
455
+ ownership.owner('resource', ref.resourceId),
456
+ ]);
457
+ if (!threadOwner ||
458
+ !resourceOwner ||
459
+ threadOwner.kind !== resourceOwner.kind ||
460
+ threadOwner.id !== resourceOwner.id) {
461
+ throw new AgentHostRequestError(404, 'run not found');
462
+ }
463
+ return { owner: threadOwner };
464
+ };
465
+ const ensureOwnerRecoveryAlarm = async (storage) => {
466
+ const scheduled = Date.now() + AGENT_OWNER_RECOVERY_DELAY_MS;
467
+ const existing = await storage.getAlarm();
468
+ if (existing === null || existing > scheduled) {
469
+ await storage.setAlarm(scheduled);
470
+ }
471
+ };
472
+ const armOwnerRecovery = async (recovery) => withRecoveryLock(async () => {
473
+ const storage = options.stateStorage();
474
+ await ensureOwnerRecoveryAlarm(storage);
475
+ await storage.put(ownerRecoveryKey(recovery.runId), recovery);
476
+ await ensureOwnerRecoveryAlarm(storage);
477
+ });
478
+ const clearOwnerRecovery = async (recovery) => withRecoveryLock(async () => {
479
+ const storage = options.stateStorage();
480
+ const key = ownerRecoveryKey(recovery.runId);
481
+ const current = await storage.get(key);
482
+ if (current?.token === recovery.token)
483
+ await storage.delete(key);
484
+ });
485
+ const releaseEphemeralOwnerClaims = async (recovery) => {
486
+ await Promise.all([
487
+ options
488
+ .resourceAccess()
489
+ .release('resource', recovery.resourceId, recovery.owner),
490
+ options
491
+ .resourceAccess()
492
+ .release('thread', recovery.threadId, recovery.owner),
493
+ ]);
494
+ };
495
+ const finalizeOwnerRecoveryBestEffort = async (recovery, summary) => {
496
+ try {
497
+ await withRecoveryLock(async () => {
498
+ const storage = options.stateStorage();
499
+ await options.resourceAccess().settleReservation(recovery.token, []);
500
+ if (!recovery.threaded && !isTerminalRunStatus(summary.status)) {
501
+ await ensureOwnerRecoveryAlarm(storage);
502
+ return;
503
+ }
504
+ if (!recovery.threaded) {
505
+ await releaseEphemeralOwnerClaims(recovery);
506
+ }
507
+ const key = ownerRecoveryKey(recovery.runId);
508
+ const current = await storage.get(key);
509
+ if (current?.token === recovery.token)
510
+ await storage.delete(key);
511
+ });
512
+ }
513
+ catch (error) {
514
+ console.error('agent owner recovery cleanup failed', error);
515
+ try {
516
+ await withRecoveryLock(() => ensureOwnerRecoveryAlarm(options.stateStorage()));
517
+ }
518
+ catch (alarmError) {
519
+ console.error('agent owner recovery rearm failed', alarmError);
520
+ }
521
+ }
522
+ };
360
523
  const withExecution = async (execution, operation) => {
361
524
  if (executions.has(execution.runId)) {
362
525
  throw new AgentHostRequestError(409, `run '${execution.runId}' already has an active operation`);
@@ -372,8 +535,8 @@ export function createThreadAgentHost(options) {
372
535
  // Reconciling approvals is trusted platform work with no person behind it.
373
536
  // It used to mint role:'operator', which is why an approval bridge looked
374
537
  // indistinguishable from a human operator in the audit trail. The bridge
375
- // mints its own principal from this id against the service's tenant.
376
- const systemActorId = options.systemActorId ?? 'flowsafe-system';
538
+ // mints its own principal from this id.
539
+ const systemPrincipalId = options.systemPrincipalId ?? 'flowsafe-system';
377
540
  // Deliberately NOT vouched. Its only consumer projects it to an ApprovalActor
378
541
  // for a role-gated READ, which grants nothing an automated principal does not
379
542
  // already have — so calling the trust assertion here would assert trust that
@@ -384,27 +547,29 @@ export function createThreadAgentHost(options) {
384
547
  // list() emits no audit event, so this string reaches nothing. It is not
385
548
  // shared with the bridge's RECONCILE_PURPOSE for that reason — there is no
386
549
  // provenance here to drift.
387
- const systemPrincipal = (scope) => ({
550
+ const systemPrincipal = () => ({
388
551
  kind: 'system',
389
- id: systemActorId,
390
- tenantId: scope.tenantId,
552
+ id: systemPrincipalId,
391
553
  purpose: 'approval-suspension-reconcile',
392
554
  });
393
555
  const currentApprovals = async (scope, summary, principal, agentId, resourceId) => {
394
556
  if (summary.status !== 'suspended')
395
557
  return [];
558
+ if (!summary.requestedBy) {
559
+ throw new Error('suspended agent run has no durable requester provenance');
560
+ }
396
561
  const service = options.approvalService(instanceScopeFor(scope));
397
- await reconcileApprovalsForSummary(service, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, summary, systemActorId, {
562
+ await reconcileApprovalsForSummary(service, DURABLE_AGENTIC_LOOP_WORKFLOW_ID, summary, systemPrincipalId, {
398
563
  kind: 'agent-thread',
399
564
  agentId,
400
565
  threadId: scope.threadId,
401
566
  resourceId,
402
567
  principal,
403
- }, principal.id);
568
+ }, summary.requestedBy);
404
569
  const records = await service.list({
405
570
  workflowId: DURABLE_AGENTIC_LOOP_WORKFLOW_ID,
406
571
  runId: summary.runId,
407
- }, principalActor(systemPrincipal(scope)));
572
+ }, principalActor(systemPrincipal()));
408
573
  const keys = new Set((summary.suspended ?? []).map((path) => path.join('.')));
409
574
  return records.filter((record) => {
410
575
  const key = record.stepPath?.join('.');
@@ -424,7 +589,18 @@ export function createThreadAgentHost(options) {
424
589
  };
425
590
  if (summary.status !== 'suspended')
426
591
  return base;
427
- const approvals = await currentApprovals(scope, summary, principal, ref.agentId, ref.resourceId);
592
+ let approvals = [];
593
+ try {
594
+ approvals = await currentApprovals(scope, summary, principal, ref.agentId, ref.resourceId);
595
+ }
596
+ catch (error) {
597
+ console.error(JSON.stringify({
598
+ type: 'agent-approval-filing-error',
599
+ agentId: ref.agentId,
600
+ runId: ref.runId,
601
+ error: error instanceof Error ? error.message : String(error),
602
+ }));
603
+ }
428
604
  return {
429
605
  ...base,
430
606
  ...(approvals[0] ? { approval: approvals[0] } : {}),
@@ -461,6 +637,7 @@ export function createThreadAgentHost(options) {
461
637
  safeContext: sanitizeStoredAgentContext(requestContext),
462
638
  };
463
639
  };
640
+ let recoverOwner;
464
641
  const statusFor = async (scope, ref, knownThreaded) => {
465
642
  const binding = await readBinding();
466
643
  const current = await runtimeFor(scope);
@@ -482,18 +659,81 @@ export function createThreadAgentHost(options) {
482
659
  if (summary.status === 'suspended' && !stored) {
483
660
  throw new AgentHostRequestError(409, 'suspended agent run has no recoverable execution principal');
484
661
  }
485
- // Inert today — a suspended run without a stored record already threw 409
486
- // above, and only a suspended run consults this. Kept as the scope's
487
- // PRINCIPAL rather than its actor so that if those two conditions are ever
488
- // decoupled, the fallback still cannot relabel an automated run as whoever
489
- // happened to poll its status.
490
662
  const principal = stored?.principal ?? scope.principal;
491
663
  const result = await envelopeFor(scope, ref, principal, summary);
492
664
  if (isTerminalRunStatus(summary.status) && stored) {
493
665
  await deleteAgentRunRecord(options.stateStorage(), ref.runId);
494
666
  }
667
+ if (isTerminalRunStatus(summary.status)) {
668
+ const key = ownerRecoveryKey(ref.runId);
669
+ const recovery = await options
670
+ .stateStorage()
671
+ .get(key);
672
+ if (recovery) {
673
+ await recoverOwner(scope.init.runtime, scope.threadId, key, recovery, true);
674
+ }
675
+ }
495
676
  return result;
496
677
  };
678
+ recoverOwner = async (scopeRuntime, threadId, key, stored, ignoreActive = false) => withBindingLock(() => withRecoveryLock(async () => {
679
+ if (stored?.version !== 1 ||
680
+ stored.threadId !== threadId ||
681
+ !isPathSafeId(stored.agentId) ||
682
+ !isPathSafeId(stored.resourceId) ||
683
+ !isPathSafeId(stored.runId) ||
684
+ !isPathSafeId(stored.token) ||
685
+ ownerRecoveryKey(stored.runId) !== key ||
686
+ typeof stored.threaded !== 'boolean' ||
687
+ typeof stored.bindingPreexisting !== 'boolean') {
688
+ throw new Error('stored agent owner recovery is malformed');
689
+ }
690
+ resourceOwner(stored.owner);
691
+ const storage = options.stateStorage();
692
+ const currentRecovery = await storage.get(key);
693
+ if (currentRecovery?.token !== stored.token)
694
+ return 'pending';
695
+ if (!ignoreActive && executions.has(stored.runId))
696
+ return 'pending';
697
+ const summary = await scopeRuntime.recoverStartAttempt(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, stored.runId, stored.token);
698
+ const [binding, record] = await Promise.all([
699
+ readBinding(),
700
+ readRun(stored.runId),
701
+ ]);
702
+ const bindingMatches = binding?.agentId === stored.agentId &&
703
+ binding.resourceId === stored.resourceId;
704
+ if (record && (!summary || isTerminalRunStatus(summary.status))) {
705
+ await deleteAgentRunRecord(storage, stored.runId);
706
+ }
707
+ if (!summary && !stored.bindingPreexisting && bindingMatches) {
708
+ await deleteAgentThreadBinding(storage, {
709
+ agentId: stored.agentId,
710
+ resourceId: stored.resourceId,
711
+ });
712
+ }
713
+ if (summary) {
714
+ await options.resourceAccess().settleReservation(stored.token, []);
715
+ if (!stored.threaded && !isTerminalRunStatus(summary.status)) {
716
+ await ensureOwnerRecoveryAlarm(storage);
717
+ return 'pending';
718
+ }
719
+ if (!stored.threaded)
720
+ await releaseEphemeralOwnerClaims(stored);
721
+ const current = await storage.get(key);
722
+ if (current?.token === stored.token)
723
+ await storage.delete(key);
724
+ return 'cleared';
725
+ }
726
+ const release = [{ kind: 'run', resourceId: stored.runId }];
727
+ const retainThread = stored.threaded && stored.bindingPreexisting && bindingMatches;
728
+ if (!retainThread) {
729
+ release.push({ kind: 'resource', resourceId: stored.resourceId }, { kind: 'thread', resourceId: stored.threadId });
730
+ }
731
+ await options.resourceAccess().settleReservation(stored.token, release);
732
+ const current = await storage.get(key);
733
+ if (current?.token === stored.token)
734
+ await storage.delete(key);
735
+ return 'cleared';
736
+ }));
497
737
  const host = {
498
738
  requestContextForRun: (base) => async (workflowId, runId, leg) => {
499
739
  const values = base ? await base(workflowId, runId, leg) : undefined;
@@ -506,75 +746,177 @@ export function createThreadAgentHost(options) {
506
746
  }
507
747
  : values;
508
748
  },
749
+ serializeDispatch: withDispatchLock,
750
+ blockingRun: (scope) => findBlockingRun(scope.init.runtime),
751
+ scheduleDispatchStatus: async (scope, input) => {
752
+ const ref = runRef(scope, {
753
+ ...input,
754
+ threadId: scope.threadId,
755
+ });
756
+ const key = ownerRecoveryKey(ref.runId);
757
+ const recovery = await options
758
+ .stateStorage()
759
+ .get(key);
760
+ if (recovery) {
761
+ await recoverOwner(scope.init.runtime, scope.threadId, key, recovery, true);
762
+ }
763
+ const summary = await scope.init.runtime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId);
764
+ if (!summary)
765
+ return undefined;
766
+ return (await statusFor(scope, ref)).summary;
767
+ },
768
+ recoverOwnership: (scopeRuntime, threadId) => withDispatchLock(async () => {
769
+ const storage = options.stateStorage();
770
+ try {
771
+ const pending = await storage.list({
772
+ prefix: AGENT_OWNER_RECOVERY_PREFIX,
773
+ });
774
+ for (const [key, stored] of pending) {
775
+ await recoverOwner(scopeRuntime, threadId, key, stored);
776
+ }
777
+ await withRecoveryLock(async () => {
778
+ const remaining = await storage.list({
779
+ prefix: AGENT_OWNER_RECOVERY_PREFIX,
780
+ });
781
+ if (remaining.size > 0) {
782
+ await ensureOwnerRecoveryAlarm(storage);
783
+ }
784
+ else {
785
+ await storage.deleteAlarm();
786
+ }
787
+ });
788
+ }
789
+ catch (error) {
790
+ await withRecoveryLock(() => ensureOwnerRecoveryAlarm(storage));
791
+ throw error;
792
+ }
793
+ }),
509
794
  start: async (scope, input) => {
510
795
  const ref = runRef(scope, input);
511
- const hasPrompt = input.prompt !== undefined;
512
- const hasMessages = input.messages !== undefined;
513
- if (hasPrompt === hasMessages ||
796
+ const entry = entryPath(input.entryPath);
797
+ const threaded = input.threaded !== false;
798
+ const source = await resolveStartSource(scope, ref, entry, threaded, input.scheduleId, input.dispatchId);
799
+ const hasPrompt = source.target === undefined && input.prompt !== undefined;
800
+ const hasMessages = source.target === undefined && input.messages !== undefined;
801
+ if ((source.target === undefined && hasPrompt === hasMessages) ||
514
802
  (hasPrompt &&
515
803
  (typeof input.prompt !== 'string' ||
516
804
  input.prompt.trim() === '' ||
517
805
  input.prompt.length > 10_000))) {
518
806
  throw new AgentHostRequestError(400, 'exactly one valid prompt or messages input is required');
519
807
  }
520
- const messages = input.messages ?? input.prompt;
808
+ const messages = source.target
809
+ ? threaded && input.messages !== undefined
810
+ ? input.messages
811
+ : source.target.prompt
812
+ : (input.messages ?? input.prompt);
521
813
  if (messages === undefined) {
522
814
  throw new AgentHostRequestError(400, 'agent input is required');
523
815
  }
524
- const entry = entryPath(input.entryPath);
816
+ const resolvedProviderOptions = source.target
817
+ ? source.target.providerOptions
818
+ : input.providerOptions;
819
+ const owner = source.owner;
525
820
  const { current, module, principalPermissions } = await authorize(scope, ref.agentId, entry, scope.principal);
526
- if (ref.resourceId !== mintResourceId(scope.tenantId, scope.threadId)) {
821
+ if (ref.resourceId !== resourceIdFromKey(scope.threadId)) {
527
822
  throw new AgentHostRequestError(404, 'run not found');
528
823
  }
529
824
  const execution = {
530
825
  agentId: ref.agentId,
826
+ deploymentTag: scope.deploymentTag,
531
827
  principal: scope.principal,
532
828
  threadId: scope.threadId,
533
829
  resourceId: ref.resourceId,
534
830
  runId: ref.runId,
535
831
  entryPath: entry,
536
832
  principalPermissions,
537
- safeContext: safeContext(input.safeContext),
833
+ safeContext: safeContext(source.target
834
+ ? scheduleAgentContext(source.target)
835
+ : input.safeContext),
538
836
  };
539
837
  const durable = current.agents.get(module.meta.id);
540
838
  if (!durable)
541
839
  throw new Error('guarded agent was not registered');
542
840
  return withExecution(execution, async () => {
841
+ const recoveryKey = ownerRecoveryKey(ref.runId);
842
+ const pending = await options
843
+ .stateStorage()
844
+ .get(recoveryKey);
845
+ if (pending) {
846
+ await recoverOwner(scope.init.runtime, scope.threadId, recoveryKey, pending, true);
847
+ }
543
848
  const existingRecord = await readRun(ref.runId);
544
849
  const existingSummary = await scope.init.runtime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId);
545
850
  if (existingRecord || existingSummary) {
546
851
  throw new AgentHostRequestError(409, `run '${ref.runId}' already exists`);
547
852
  }
548
- if (input.threaded !== false) {
549
- await withBindingLock(async () => {
550
- const existing = await readBinding();
853
+ const claims = [
854
+ { kind: 'thread', resourceId: scope.threadId },
855
+ { kind: 'resource', resourceId: ref.resourceId },
856
+ { kind: 'run', resourceId: ref.runId },
857
+ ];
858
+ const stored = {
859
+ version: 2,
860
+ agentId: ref.agentId,
861
+ principal: scope.principal,
862
+ originEntryPath: entry,
863
+ };
864
+ const recovery = await withBindingLock(async () => {
865
+ const existing = await readBinding();
866
+ if (!threaded && existing) {
867
+ throw new AgentHostRequestError(409, 'unthreaded starts require an unbound object');
868
+ }
869
+ if (threaded) {
551
870
  const bindingMatches = existing?.agentId === ref.agentId &&
552
871
  existing.resourceId === ref.resourceId;
553
872
  if (entry === 'schedule.fire') {
554
873
  if (!bindingMatches) {
555
874
  throw new AgentHostRequestError(404, 'run not found');
556
875
  }
557
- return;
558
876
  }
559
- if (existing && !bindingMatches) {
877
+ else if (existing && !bindingMatches) {
560
878
  throw new AgentHostRequestError(409, 'thread is bound to another agent');
561
879
  }
562
- if (!existing) {
563
- await bindAgentThread(options.stateStorage(), {
564
- version: 1,
565
- agentId: ref.agentId,
566
- resourceId: ref.resourceId,
567
- });
568
- }
569
- });
570
- }
571
- const stored = {
572
- version: 2,
573
- agentId: ref.agentId,
574
- principal: scope.principal,
575
- originEntryPath: entry,
576
- };
577
- await writeAgentRunRecord(options.stateStorage(), ref.runId, stored);
880
+ }
881
+ const blocking = await findBlockingRun(scope.init.runtime);
882
+ if (blocking && blocking.runId !== ref.runId) {
883
+ throw new AgentHostRequestError(409, `thread is blocked by run '${blocking.runId}'`);
884
+ }
885
+ const recovery = {
886
+ version: 1,
887
+ agentId: ref.agentId,
888
+ threadId: scope.threadId,
889
+ resourceId: ref.resourceId,
890
+ runId: ref.runId,
891
+ owner,
892
+ token: crypto.randomUUID(),
893
+ threaded,
894
+ bindingPreexisting: existing !== undefined,
895
+ };
896
+ if (!threaded &&
897
+ (await Promise.all(claims.map((claim) => options.resourceAccess().owner(claim.kind, claim.resourceId)))).some((registered) => registered !== undefined)) {
898
+ throw new AgentHostRequestError(404, 'run not found');
899
+ }
900
+ await armOwnerRecovery(recovery);
901
+ if (!(await options
902
+ .resourceAccess()
903
+ .reserveAll(claims, owner, recovery.token))) {
904
+ await options
905
+ .resourceAccess()
906
+ .settleReservation(recovery.token, claims);
907
+ await clearOwnerRecovery(recovery);
908
+ throw new AgentHostRequestError(404, 'run not found');
909
+ }
910
+ if (threaded && !existing) {
911
+ await bindAgentThread(options.stateStorage(), {
912
+ version: 1,
913
+ agentId: ref.agentId,
914
+ resourceId: ref.resourceId,
915
+ });
916
+ }
917
+ await writeAgentRunRecord(options.stateStorage(), ref.runId, stored);
918
+ return recovery;
919
+ });
578
920
  try {
579
921
  await durable.streamUntilPersisted(messages, {
580
922
  runId: ref.runId,
@@ -589,7 +931,10 @@ export function createThreadAgentHost(options) {
589
931
  : {}),
590
932
  maxSteps: module.agent.maxSteps,
591
933
  disableBackgroundTasks: true,
592
- });
934
+ ...(resolvedProviderOptions !== undefined
935
+ ? { providerOptions: providerOptions(resolvedProviderOptions) }
936
+ : {}),
937
+ }, scope.principal.id, scope.principal.kind, recovery.token);
593
938
  const summary = await scope.init.runtime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId);
594
939
  if (!summary)
595
940
  throw new Error('agent run did not persist a summary');
@@ -597,19 +942,41 @@ export function createThreadAgentHost(options) {
597
942
  if (isTerminalRunStatus(result.summary.status)) {
598
943
  await deleteAgentRunRecord(options.stateStorage(), ref.runId);
599
944
  }
945
+ await finalizeOwnerRecoveryBestEffort(recovery, summary);
600
946
  return result;
601
947
  }
602
948
  catch (error) {
603
949
  let summary;
604
950
  try {
605
- summary = await scope.init.runtime.status(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId);
951
+ summary = await scope.init.runtime.recoverStartAttempt(DURABLE_AGENTIC_LOOP_WORKFLOW_ID, ref.runId, recovery.token);
606
952
  }
607
953
  catch {
608
954
  // Unknown authoritative state: retain metadata and fail closed.
609
955
  }
610
- if (!summary || isTerminalRunStatus(summary.status)) {
956
+ if (summary === null ||
957
+ (summary && isTerminalRunStatus(summary.status))) {
611
958
  await deleteAgentRunRecord(options.stateStorage(), ref.runId);
612
959
  }
960
+ if (summary) {
961
+ await finalizeOwnerRecoveryBestEffort(recovery, summary);
962
+ return envelopeFor(scope, ref, scope.principal, summary);
963
+ }
964
+ else if (summary === null) {
965
+ try {
966
+ await recoverOwner(scope.init.runtime, scope.threadId, recoveryKey, recovery, true);
967
+ }
968
+ catch (recoveryError) {
969
+ console.error('agent owner recovery failed', recoveryError);
970
+ }
971
+ }
972
+ else {
973
+ try {
974
+ await withRecoveryLock(() => ensureOwnerRecoveryAlarm(options.stateStorage()));
975
+ }
976
+ catch (alarmError) {
977
+ console.error('agent owner recovery rearm failed', alarmError);
978
+ }
979
+ }
613
980
  throw error;
614
981
  }
615
982
  });
@@ -618,7 +985,7 @@ export function createThreadAgentHost(options) {
618
985
  const binding = await readBinding();
619
986
  if (!binding)
620
987
  throw new AgentHostRequestError(404, 'agent not found');
621
- if (binding.resourceId !== mintResourceId(scope.tenantId, scope.threadId)) {
988
+ if (binding.resourceId !== resourceIdFromKey(scope.threadId)) {
622
989
  throw new AgentHostRequestError(404, 'agent not found');
623
990
  }
624
991
  if (input.agentId && input.agentId !== binding.agentId) {
@@ -634,23 +1001,62 @@ export function createThreadAgentHost(options) {
634
1001
  durableAgent,
635
1002
  };
636
1003
  },
637
- route: async (request, scope) => {
1004
+ route: (request, scope) => withDispatchLock(async () => {
638
1005
  const url = new URL(request.url);
639
1006
  if (!url.pathname.startsWith(AGENT_HOST_ROUTE_PREFIX))
640
1007
  return null;
1008
+ if (request.method === 'GET' &&
1009
+ url.pathname === `${AGENT_HOST_ROUTE_PREFIX}/binding`) {
1010
+ const resourceId = url.searchParams.get('resourceId');
1011
+ const agentId = url.searchParams.get('agentId');
1012
+ const binding = await readBinding();
1013
+ if (!resourceId ||
1014
+ resourceId !== resourceIdFromKey(scope.threadId) ||
1015
+ binding?.resourceId !== resourceId ||
1016
+ (agentId !== null && binding.agentId !== agentId)) {
1017
+ throw new AgentHostRequestError(404, 'agent not found');
1018
+ }
1019
+ return json({ bound: true });
1020
+ }
641
1021
  if (request.method === 'POST' &&
642
1022
  url.pathname === `${AGENT_HOST_ROUTE_PREFIX}/start`) {
643
1023
  const body = await objectBody(request);
1024
+ if ('resourceOwner' in body || 'requestedBy' in body) {
1025
+ throw new AgentHostRequestError(400, 'start owner and requester are derived from trusted provenance');
1026
+ }
644
1027
  const ref = runRef(scope, body);
645
- if (typeof body.prompt !== 'string') {
1028
+ const requestedEntry = entryPath(body.entryPath);
1029
+ if (requestedEntry !== 'schedule.fire' &&
1030
+ typeof body.prompt !== 'string') {
646
1031
  throw new AgentHostRequestError(400, 'prompt is required');
647
1032
  }
648
1033
  return json(await host.start(scope, {
649
1034
  ...ref,
650
- prompt: body.prompt,
651
- entryPath: entryPath(body.entryPath),
1035
+ ...(typeof body.prompt === 'string'
1036
+ ? { prompt: body.prompt }
1037
+ : {}),
1038
+ entryPath: requestedEntry,
652
1039
  threaded: body.threaded !== false,
653
1040
  safeContext: safeContext(body.safeContext),
1041
+ providerOptions: providerOptions(body.providerOptions),
1042
+ ...(body.scheduleId !== undefined
1043
+ ? {
1044
+ scheduleId: isPathSafeId(body.scheduleId)
1045
+ ? body.scheduleId
1046
+ : (() => {
1047
+ throw new AgentHostRequestError(404, 'run not found');
1048
+ })(),
1049
+ }
1050
+ : {}),
1051
+ ...(body.dispatchId !== undefined
1052
+ ? {
1053
+ dispatchId: isPathSafeId(body.dispatchId)
1054
+ ? body.dispatchId
1055
+ : (() => {
1056
+ throw new AgentHostRequestError(404, 'run not found');
1057
+ })(),
1058
+ }
1059
+ : {}),
654
1060
  }));
655
1061
  }
656
1062
  if (request.method === 'POST' &&
@@ -669,6 +1075,7 @@ export function createThreadAgentHost(options) {
669
1075
  const durable = current.agents.get(module.meta.id);
670
1076
  if (!durable)
671
1077
  throw new Error('guarded agent was not registered');
1078
+ const requesterId = requestedBy(body.requestedBy);
672
1079
  const step = typeof body.step === 'string' ||
673
1080
  (Array.isArray(body.step) &&
674
1081
  body.step.every((part) => typeof part === 'string'))
@@ -676,6 +1083,7 @@ export function createThreadAgentHost(options) {
676
1083
  : undefined;
677
1084
  const execution = {
678
1085
  agentId: ref.agentId,
1086
+ deploymentTag: scope.deploymentTag,
679
1087
  principal: stored.principal,
680
1088
  threadId: scope.threadId,
681
1089
  resourceId: ref.resourceId,
@@ -687,22 +1095,31 @@ export function createThreadAgentHost(options) {
687
1095
  principalPermissions,
688
1096
  safeContext: snapshotExecution.safeContext,
689
1097
  };
690
- const summary = await withExecution(execution, () => durable.resumeViaRuntime({
691
- runId: ref.runId,
692
- ...(step !== undefined ? { step } : {}),
693
- ...('resumeData' in body ? { resumeData: body.resumeData } : {}),
694
- ...(snapshotExecution.threaded
695
- ? {
696
- memory: {
697
- thread: scope.threadId,
698
- resource: ref.resourceId,
699
- },
700
- }
701
- : {}),
702
- }));
1098
+ const summary = await withExecution(execution, async () => {
1099
+ return durable.resumeViaRuntime({
1100
+ runId: ref.runId,
1101
+ requestedBy: requesterId,
1102
+ ...(step !== undefined ? { step } : {}),
1103
+ ...('resumeData' in body ? { resumeData: body.resumeData } : {}),
1104
+ ...(snapshotExecution.threaded
1105
+ ? {
1106
+ memory: {
1107
+ thread: scope.threadId,
1108
+ resource: ref.resourceId,
1109
+ },
1110
+ }
1111
+ : {}),
1112
+ });
1113
+ });
703
1114
  const result = await envelopeFor(scope, ref, stored.principal, summary);
704
1115
  if (isTerminalRunStatus(summary.status)) {
705
1116
  await deleteAgentRunRecord(options.stateStorage(), ref.runId);
1117
+ const recovery = await options
1118
+ .stateStorage()
1119
+ .get(ownerRecoveryKey(ref.runId));
1120
+ if (recovery) {
1121
+ await finalizeOwnerRecoveryBestEffort(recovery, summary);
1122
+ }
706
1123
  }
707
1124
  return json(result);
708
1125
  }
@@ -723,6 +1140,16 @@ export function createThreadAgentHost(options) {
723
1140
  runId,
724
1141
  });
725
1142
  if (segments.length === 3 && request.method === 'GET') {
1143
+ if (url.searchParams.get('dispatch') === '1') {
1144
+ const key = ownerRecoveryKey(ref.runId);
1145
+ const pending = await options
1146
+ .stateStorage()
1147
+ .get(key);
1148
+ if (pending) {
1149
+ await recoverOwner(scope.init.runtime, scope.threadId, key, pending, true);
1150
+ }
1151
+ return json(await statusFor(scope, ref));
1152
+ }
726
1153
  return json(await statusFor(scope, ref));
727
1154
  }
728
1155
  if (segments.length === 4 &&
@@ -769,7 +1196,7 @@ export function createThreadAgentHost(options) {
769
1196
  return known
770
1197
  ? json({ error: 'method not allowed' }, 405)
771
1198
  : json({ error: 'not found' }, 404);
772
- },
1199
+ }),
773
1200
  };
774
1201
  return host;
775
1202
  }