@proofoftech/flowsafe 0.10.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 (391) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +77 -47
  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.d.ts +1 -1
  10. package/dist/agent-host/catalog.d.ts.map +1 -1
  11. package/dist/agent-host/catalog.js +26 -2
  12. package/dist/agent-host/catalog.js.map +1 -1
  13. package/dist/agent-host/index.d.ts +4 -3
  14. package/dist/agent-host/index.d.ts.map +1 -1
  15. package/dist/agent-host/index.js +1 -0
  16. package/dist/agent-host/index.js.map +1 -1
  17. package/dist/agent-host/router.d.ts +2 -2
  18. package/dist/agent-host/router.d.ts.map +1 -1
  19. package/dist/agent-host/router.js +22 -54
  20. package/dist/agent-host/router.js.map +1 -1
  21. package/dist/agent-host/thread-host.d.ts +53 -9
  22. package/dist/agent-host/thread-host.d.ts.map +1 -1
  23. package/dist/agent-host/thread-host.js +624 -109
  24. package/dist/agent-host/thread-host.js.map +1 -1
  25. package/dist/agent-host/thread-topology.d.ts +28 -7
  26. package/dist/agent-host/thread-topology.d.ts.map +1 -1
  27. package/dist/agent-host/thread-topology.js +97 -39
  28. package/dist/agent-host/thread-topology.js.map +1 -1
  29. package/dist/agent-host/trusted-context.d.ts.map +1 -1
  30. package/dist/agent-host/trusted-context.js +9 -2
  31. package/dist/agent-host/trusted-context.js.map +1 -1
  32. package/dist/agent-host/types.d.ts +48 -0
  33. package/dist/agent-host/types.d.ts.map +1 -1
  34. package/dist/agent-host/types.js +1 -1
  35. package/dist/agent-host/types.js.map +1 -1
  36. package/dist/agent-runner/agent-run-state.d.ts +9 -8
  37. package/dist/agent-runner/agent-run-state.d.ts.map +1 -1
  38. package/dist/agent-runner/agent-run-state.js +78 -40
  39. package/dist/agent-runner/agent-run-state.js.map +1 -1
  40. package/dist/agent-runner/durable-agent-runner.d.ts +18 -16
  41. package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
  42. package/dist/agent-runner/durable-agent-runner.js +120 -43
  43. package/dist/agent-runner/durable-agent-runner.js.map +1 -1
  44. package/dist/agent-runner/index.d.ts +1 -1
  45. package/dist/agent-runner/index.d.ts.map +1 -1
  46. package/dist/agent-runner/index.js +1 -1
  47. package/dist/agent-runner/index.js.map +1 -1
  48. package/dist/approval-api/actor-context.d.ts +67 -0
  49. package/dist/approval-api/actor-context.d.ts.map +1 -0
  50. package/dist/approval-api/actor-context.js +120 -0
  51. package/dist/approval-api/actor-context.js.map +1 -0
  52. package/dist/approval-api/contract.d.ts +13 -24
  53. package/dist/approval-api/contract.d.ts.map +1 -1
  54. package/dist/approval-api/contract.js +1 -1
  55. package/dist/approval-api/contract.js.map +1 -1
  56. package/dist/approval-api/d1-store.d.ts +7 -15
  57. package/dist/approval-api/d1-store.d.ts.map +1 -1
  58. package/dist/approval-api/d1-store.js +83 -159
  59. package/dist/approval-api/d1-store.js.map +1 -1
  60. package/dist/approval-api/grants.d.ts +3 -17
  61. package/dist/approval-api/grants.d.ts.map +1 -1
  62. package/dist/approval-api/grants.js +19 -39
  63. package/dist/approval-api/grants.js.map +1 -1
  64. package/dist/approval-api/index.d.ts +9 -9
  65. package/dist/approval-api/index.d.ts.map +1 -1
  66. package/dist/approval-api/index.js +6 -6
  67. package/dist/approval-api/index.js.map +1 -1
  68. package/dist/approval-api/principal.d.ts +29 -30
  69. package/dist/approval-api/principal.d.ts.map +1 -1
  70. package/dist/approval-api/principal.js +73 -54
  71. package/dist/approval-api/principal.js.map +1 -1
  72. package/dist/approval-api/resource-ownership.d.ts +64 -0
  73. package/dist/approval-api/resource-ownership.d.ts.map +1 -0
  74. package/dist/approval-api/resource-ownership.js +329 -0
  75. package/dist/approval-api/resource-ownership.js.map +1 -0
  76. package/dist/approval-api/retention.d.ts +6 -11
  77. package/dist/approval-api/retention.d.ts.map +1 -1
  78. package/dist/approval-api/retention.js +7 -12
  79. package/dist/approval-api/retention.js.map +1 -1
  80. package/dist/approval-api/router.d.ts +9 -8
  81. package/dist/approval-api/router.d.ts.map +1 -1
  82. package/dist/approval-api/router.js +50 -18
  83. package/dist/approval-api/router.js.map +1 -1
  84. package/dist/approval-api/service.d.ts +26 -36
  85. package/dist/approval-api/service.d.ts.map +1 -1
  86. package/dist/approval-api/service.js +104 -166
  87. package/dist/approval-api/service.js.map +1 -1
  88. package/dist/approval-api/store-factory.d.ts +19 -0
  89. package/dist/approval-api/store-factory.d.ts.map +1 -0
  90. package/dist/approval-api/store-factory.js +49 -0
  91. package/dist/approval-api/store-factory.js.map +1 -0
  92. package/dist/approval-api/store.d.ts +22 -36
  93. package/dist/approval-api/store.d.ts.map +1 -1
  94. package/dist/approval-api/store.js +48 -66
  95. package/dist/approval-api/store.js.map +1 -1
  96. package/dist/approval-api/types.d.ts +28 -22
  97. package/dist/approval-api/types.d.ts.map +1 -1
  98. package/dist/approval-api/types.js +51 -3
  99. package/dist/approval-api/types.js.map +1 -1
  100. package/dist/approval-ui/client.js +3 -3
  101. package/dist/approval-ui/client.js.map +1 -1
  102. package/dist/approval-ui/stream.d.ts +4 -4
  103. package/dist/approval-ui/stream.d.ts.map +1 -1
  104. package/dist/approval-ui/stream.js +2 -2
  105. package/dist/approval-ui/tips.d.ts +1 -1
  106. package/dist/approval-ui/tips.js +1 -1
  107. package/dist/approval-ui/tips.js.map +1 -1
  108. package/dist/approval-ui/use-approval-dashboard.d.ts +3 -3
  109. package/dist/approval-ui/use-approval-dashboard.js +1 -1
  110. package/dist/artifacts/index.d.ts +2 -3
  111. package/dist/artifacts/index.d.ts.map +1 -1
  112. package/dist/artifacts/index.js +9 -8
  113. package/dist/artifacts/index.js.map +1 -1
  114. package/dist/audit-export/audit-proxy.d.ts +71 -0
  115. package/dist/audit-export/audit-proxy.d.ts.map +1 -0
  116. package/dist/audit-export/audit-proxy.js +142 -0
  117. package/dist/audit-export/audit-proxy.js.map +1 -0
  118. package/dist/audit-export/index.d.ts +4 -7
  119. package/dist/audit-export/index.d.ts.map +1 -1
  120. package/dist/audit-export/index.js +1 -4
  121. package/dist/audit-export/index.js.map +1 -1
  122. package/dist/audit-export/queue-types.d.ts +5 -0
  123. package/dist/audit-export/queue-types.d.ts.map +1 -0
  124. package/dist/audit-export/queue-types.js +3 -0
  125. package/dist/audit-export/queue-types.js.map +1 -0
  126. package/dist/background-tasks/d1-storage.d.ts +9 -19
  127. package/dist/background-tasks/d1-storage.d.ts.map +1 -1
  128. package/dist/background-tasks/d1-storage.js +22 -52
  129. package/dist/background-tasks/d1-storage.js.map +1 -1
  130. package/dist/background-tasks/host.d.ts +4 -6
  131. package/dist/background-tasks/host.d.ts.map +1 -1
  132. package/dist/background-tasks/host.js +14 -17
  133. package/dist/background-tasks/host.js.map +1 -1
  134. package/dist/background-tasks/index.d.ts +1 -1
  135. package/dist/background-tasks/index.d.ts.map +1 -1
  136. package/dist/background-tasks/index.js +3 -3
  137. package/dist/background-tasks/index.js.map +1 -1
  138. package/dist/background-tasks/routes.d.ts +8 -4
  139. package/dist/background-tasks/routes.d.ts.map +1 -1
  140. package/dist/background-tasks/routes.js +33 -31
  141. package/dist/background-tasks/routes.js.map +1 -1
  142. package/dist/deployment-identity-protocol.d.ts +3 -0
  143. package/dist/deployment-identity-protocol.d.ts.map +1 -0
  144. package/dist/deployment-identity-protocol.js +3 -0
  145. package/dist/deployment-identity-protocol.js.map +1 -0
  146. package/dist/do-runner/breakwater-keys.d.ts +16 -5
  147. package/dist/do-runner/breakwater-keys.d.ts.map +1 -1
  148. package/dist/do-runner/breakwater-keys.js +16 -5
  149. package/dist/do-runner/breakwater-keys.js.map +1 -1
  150. package/dist/do-runner/cf-types.d.ts +18 -12
  151. package/dist/do-runner/cf-types.d.ts.map +1 -1
  152. package/dist/do-runner/cf-types.js.map +1 -1
  153. package/dist/do-runner/d1-storage.d.ts +44 -182
  154. package/dist/do-runner/d1-storage.d.ts.map +1 -1
  155. package/dist/do-runner/d1-storage.js +102 -353
  156. package/dist/do-runner/d1-storage.js.map +1 -1
  157. package/dist/do-runner/deployment-identity.d.ts +121 -0
  158. package/dist/do-runner/deployment-identity.d.ts.map +1 -0
  159. package/dist/do-runner/deployment-identity.js +272 -0
  160. package/dist/do-runner/deployment-identity.js.map +1 -0
  161. package/dist/do-runner/do-error-response.d.ts +1 -1
  162. package/dist/do-runner/do-error-response.d.ts.map +1 -1
  163. package/dist/do-runner/do-error-response.js +7 -1
  164. package/dist/do-runner/do-error-response.js.map +1 -1
  165. package/dist/do-runner/durable-object.d.ts +23 -16
  166. package/dist/do-runner/durable-object.d.ts.map +1 -1
  167. package/dist/do-runner/durable-object.js +303 -70
  168. package/dist/do-runner/durable-object.js.map +1 -1
  169. package/dist/do-runner/execution-context.d.ts +1 -0
  170. package/dist/do-runner/execution-context.d.ts.map +1 -1
  171. package/dist/do-runner/execution-context.js +6 -8
  172. package/dist/do-runner/execution-context.js.map +1 -1
  173. package/dist/do-runner/execution-principal-header.d.ts +16 -0
  174. package/dist/do-runner/execution-principal-header.d.ts.map +1 -0
  175. package/dist/do-runner/execution-principal-header.js +20 -0
  176. package/dist/do-runner/execution-principal-header.js.map +1 -0
  177. package/dist/do-runner/hub-do.d.ts +17 -25
  178. package/dist/do-runner/hub-do.d.ts.map +1 -1
  179. package/dist/do-runner/hub-do.js +63 -59
  180. package/dist/do-runner/hub-do.js.map +1 -1
  181. package/dist/do-runner/index.d.ts +12 -10
  182. package/dist/do-runner/index.d.ts.map +1 -1
  183. package/dist/do-runner/index.js +10 -9
  184. package/dist/do-runner/index.js.map +1 -1
  185. package/dist/do-runner/init.d.ts +0 -7
  186. package/dist/do-runner/init.d.ts.map +1 -1
  187. package/dist/do-runner/init.js +1 -2
  188. package/dist/do-runner/init.js.map +1 -1
  189. package/dist/do-runner/memory-id.d.ts +8 -25
  190. package/dist/do-runner/memory-id.d.ts.map +1 -1
  191. package/dist/do-runner/memory-id.js +25 -52
  192. package/dist/do-runner/memory-id.js.map +1 -1
  193. package/dist/do-runner/path-safe-id.d.ts +2 -48
  194. package/dist/do-runner/path-safe-id.d.ts.map +1 -1
  195. package/dist/do-runner/path-safe-id.js +3 -93
  196. package/dist/do-runner/path-safe-id.js.map +1 -1
  197. package/dist/do-runner/pubsub.d.ts +1 -1
  198. package/dist/do-runner/pubsub.js +1 -1
  199. package/dist/do-runner/runtime.d.ts +47 -34
  200. package/dist/do-runner/runtime.d.ts.map +1 -1
  201. package/dist/do-runner/runtime.js +281 -143
  202. package/dist/do-runner/runtime.js.map +1 -1
  203. package/dist/do-runner/schedule-source.d.ts +81 -0
  204. package/dist/do-runner/schedule-source.d.ts.map +1 -0
  205. package/dist/do-runner/schedule-source.js +64 -0
  206. package/dist/do-runner/schedule-source.js.map +1 -0
  207. package/dist/do-runner/thread-do.d.ts +18 -32
  208. package/dist/do-runner/thread-do.d.ts.map +1 -1
  209. package/dist/do-runner/thread-do.js +52 -74
  210. package/dist/do-runner/thread-do.js.map +1 -1
  211. package/dist/goals/objective-routes.d.ts +7 -4
  212. package/dist/goals/objective-routes.d.ts.map +1 -1
  213. package/dist/goals/objective-routes.js +58 -43
  214. package/dist/goals/objective-routes.js.map +1 -1
  215. package/dist/host-kit/approval-bridge.d.ts +18 -21
  216. package/dist/host-kit/approval-bridge.d.ts.map +1 -1
  217. package/dist/host-kit/approval-bridge.js +37 -37
  218. package/dist/host-kit/approval-bridge.js.map +1 -1
  219. package/dist/host-kit/bearer-auth.d.ts +3 -4
  220. package/dist/host-kit/bearer-auth.d.ts.map +1 -1
  221. package/dist/host-kit/bearer-auth.js +7 -11
  222. package/dist/host-kit/bearer-auth.js.map +1 -1
  223. package/dist/host-kit/do-run-topology.d.ts +14 -5
  224. package/dist/host-kit/do-run-topology.d.ts.map +1 -1
  225. package/dist/host-kit/do-run-topology.js +47 -15
  226. package/dist/host-kit/do-run-topology.js.map +1 -1
  227. package/dist/host-kit/env-vars.d.ts +1 -1
  228. package/dist/host-kit/env-vars.js +1 -1
  229. package/dist/host-kit/flowsafe-worker.d.ts +149 -73
  230. package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
  231. package/dist/host-kit/flowsafe-worker.js +619 -141
  232. package/dist/host-kit/flowsafe-worker.js.map +1 -1
  233. package/dist/host-kit/host-approval-service.d.ts +62 -54
  234. package/dist/host-kit/host-approval-service.d.ts.map +1 -1
  235. package/dist/host-kit/host-approval-service.js +90 -61
  236. package/dist/host-kit/host-approval-service.js.map +1 -1
  237. package/dist/host-kit/hub-topology.d.ts +6 -7
  238. package/dist/host-kit/hub-topology.d.ts.map +1 -1
  239. package/dist/host-kit/hub-topology.js +16 -12
  240. package/dist/host-kit/hub-topology.js.map +1 -1
  241. package/dist/host-kit/index.d.ts +13 -11
  242. package/dist/host-kit/index.d.ts.map +1 -1
  243. package/dist/host-kit/index.js +9 -12
  244. package/dist/host-kit/index.js.map +1 -1
  245. package/dist/host-kit/maintenance-capability.d.ts +58 -0
  246. package/dist/host-kit/maintenance-capability.d.ts.map +1 -0
  247. package/dist/host-kit/maintenance-capability.js +268 -0
  248. package/dist/host-kit/maintenance-capability.js.map +1 -0
  249. package/dist/host-kit/memory-boundary.d.ts +4 -19
  250. package/dist/host-kit/memory-boundary.d.ts.map +1 -1
  251. package/dist/host-kit/memory-boundary.js +12 -27
  252. package/dist/host-kit/memory-boundary.js.map +1 -1
  253. package/dist/host-kit/resource-access.d.ts +4 -0
  254. package/dist/host-kit/resource-access.d.ts.map +1 -0
  255. package/dist/host-kit/resource-access.js +12 -0
  256. package/dist/host-kit/resource-access.js.map +1 -0
  257. package/dist/host-kit/run-router.d.ts +25 -15
  258. package/dist/host-kit/run-router.d.ts.map +1 -1
  259. package/dist/host-kit/run-router.js +61 -46
  260. package/dist/host-kit/run-router.js.map +1 -1
  261. package/dist/host-kit/state-egress.d.ts +51 -0
  262. package/dist/host-kit/state-egress.d.ts.map +1 -0
  263. package/dist/host-kit/state-egress.js +120 -0
  264. package/dist/host-kit/state-egress.js.map +1 -0
  265. package/dist/host-kit/stream-router.d.ts +9 -4
  266. package/dist/host-kit/stream-router.d.ts.map +1 -1
  267. package/dist/host-kit/stream-router.js +45 -38
  268. package/dist/host-kit/stream-router.js.map +1 -1
  269. package/dist/host-kit/stream-ticket.d.ts +10 -10
  270. package/dist/host-kit/stream-ticket.d.ts.map +1 -1
  271. package/dist/host-kit/stream-ticket.js +60 -90
  272. package/dist/host-kit/stream-ticket.js.map +1 -1
  273. package/dist/host-kit/thread-topology.d.ts +17 -11
  274. package/dist/host-kit/thread-topology.d.ts.map +1 -1
  275. package/dist/host-kit/thread-topology.js +33 -59
  276. package/dist/host-kit/thread-topology.js.map +1 -1
  277. package/dist/host-kit/verifier.d.ts +6 -6
  278. package/dist/host-kit/verifier.d.ts.map +1 -1
  279. package/dist/host-kit/verifier.js +51 -119
  280. package/dist/host-kit/verifier.js.map +1 -1
  281. package/dist/http-body.d.ts +20 -0
  282. package/dist/http-body.d.ts.map +1 -0
  283. package/dist/http-body.js +67 -0
  284. package/dist/http-body.js.map +1 -0
  285. package/dist/schedules/index.d.ts +3 -2
  286. package/dist/schedules/index.d.ts.map +1 -1
  287. package/dist/schedules/index.js +5 -4
  288. package/dist/schedules/index.js.map +1 -1
  289. package/dist/schedules/router.d.ts +16 -10
  290. package/dist/schedules/router.d.ts.map +1 -1
  291. package/dist/schedules/router.js +153 -91
  292. package/dist/schedules/router.js.map +1 -1
  293. package/dist/schedules/schedules-d1.d.ts +88 -0
  294. package/dist/schedules/schedules-d1.d.ts.map +1 -1
  295. package/dist/schedules/schedules-d1.js +411 -50
  296. package/dist/schedules/schedules-d1.js.map +1 -1
  297. package/dist/schedules/target-policy.d.ts +29 -0
  298. package/dist/schedules/target-policy.d.ts.map +1 -0
  299. package/dist/schedules/target-policy.js +70 -0
  300. package/dist/schedules/target-policy.js.map +1 -0
  301. package/dist/schedules/tick.d.ts +125 -21
  302. package/dist/schedules/tick.d.ts.map +1 -1
  303. package/dist/schedules/tick.js +637 -160
  304. package/dist/schedules/tick.js.map +1 -1
  305. package/dist/signal-providers/delivery.d.ts +0 -19
  306. package/dist/signal-providers/delivery.d.ts.map +1 -1
  307. package/dist/signal-providers/delivery.js +5 -54
  308. package/dist/signal-providers/delivery.js.map +1 -1
  309. package/dist/signal-providers/github-provider.d.ts.map +1 -1
  310. package/dist/signal-providers/github-provider.js +9 -16
  311. package/dist/signal-providers/github-provider.js.map +1 -1
  312. package/dist/signal-providers/host-do.d.ts +3 -45
  313. package/dist/signal-providers/host-do.d.ts.map +1 -1
  314. package/dist/signal-providers/host-do.js +86 -106
  315. package/dist/signal-providers/host-do.js.map +1 -1
  316. package/dist/signal-providers/host-topology.d.ts +5 -14
  317. package/dist/signal-providers/host-topology.d.ts.map +1 -1
  318. package/dist/signal-providers/host-topology.js +11 -25
  319. package/dist/signal-providers/host-topology.js.map +1 -1
  320. package/dist/signal-providers/index.d.ts +3 -3
  321. package/dist/signal-providers/index.d.ts.map +1 -1
  322. package/dist/signal-providers/index.js +3 -3
  323. package/dist/signal-providers/index.js.map +1 -1
  324. package/dist/signal-providers/provider.d.ts +4 -5
  325. package/dist/signal-providers/provider.d.ts.map +1 -1
  326. package/dist/signal-providers/provider.js +5 -6
  327. package/dist/signal-providers/provider.js.map +1 -1
  328. package/dist/signal-providers/subscription-d1.d.ts +10 -57
  329. package/dist/signal-providers/subscription-d1.d.ts.map +1 -1
  330. package/dist/signal-providers/subscription-d1.js +208 -155
  331. package/dist/signal-providers/subscription-d1.js.map +1 -1
  332. package/dist/signal-providers/webhook-route.d.ts +28 -14
  333. package/dist/signal-providers/webhook-route.d.ts.map +1 -1
  334. package/dist/signal-providers/webhook-route.js +117 -88
  335. package/dist/signal-providers/webhook-route.js.map +1 -1
  336. package/dist/signals/client.js +8 -8
  337. package/dist/signals/client.js.map +1 -1
  338. package/dist/signals/d1-shared.js +1 -1
  339. package/dist/signals/d1-shared.js.map +1 -1
  340. package/dist/signals/index.d.ts +1 -1
  341. package/dist/signals/index.d.ts.map +1 -1
  342. package/dist/signals/index.js.map +1 -1
  343. package/dist/signals/notification-dispatch.d.ts +12 -6
  344. package/dist/signals/notification-dispatch.d.ts.map +1 -1
  345. package/dist/signals/notification-dispatch.js +37 -21
  346. package/dist/signals/notification-dispatch.js.map +1 -1
  347. package/dist/signals/notifications-d1.d.ts +5 -8
  348. package/dist/signals/notifications-d1.d.ts.map +1 -1
  349. package/dist/signals/notifications-d1.js +8 -13
  350. package/dist/signals/notifications-d1.js.map +1 -1
  351. package/dist/signals/router.d.ts +10 -10
  352. package/dist/signals/router.d.ts.map +1 -1
  353. package/dist/signals/router.js +49 -51
  354. package/dist/signals/router.js.map +1 -1
  355. package/dist/signals/thread-do-routes.d.ts +74 -15
  356. package/dist/signals/thread-do-routes.d.ts.map +1 -1
  357. package/dist/signals/thread-do-routes.js +484 -41
  358. package/dist/signals/thread-do-routes.js.map +1 -1
  359. package/dist/signals/thread-state-d1.d.ts.map +1 -1
  360. package/dist/signals/thread-state-d1.js +2 -3
  361. package/dist/signals/thread-state-d1.js.map +1 -1
  362. package/package.json +30 -7
  363. package/scripts/seed-deployment-identity.mjs +252 -0
  364. package/dist/approval-api/tenant-brand.d.ts +0 -35
  365. package/dist/approval-api/tenant-brand.d.ts.map +0 -1
  366. package/dist/approval-api/tenant-brand.js +0 -19
  367. package/dist/approval-api/tenant-brand.js.map +0 -1
  368. package/dist/approval-api/tenant-context.d.ts +0 -84
  369. package/dist/approval-api/tenant-context.d.ts.map +0 -1
  370. package/dist/approval-api/tenant-context.js +0 -112
  371. package/dist/approval-api/tenant-context.js.map +0 -1
  372. package/dist/approval-api/tenant-store.d.ts +0 -36
  373. package/dist/approval-api/tenant-store.d.ts.map +0 -1
  374. package/dist/approval-api/tenant-store.js +0 -129
  375. package/dist/approval-api/tenant-store.js.map +0 -1
  376. package/dist/do-runner/resume-ledger.d.ts +0 -48
  377. package/dist/do-runner/resume-ledger.d.ts.map +0 -1
  378. package/dist/do-runner/resume-ledger.js +0 -60
  379. package/dist/do-runner/resume-ledger.js.map +0 -1
  380. package/dist/do-runner/thread-header.d.ts +0 -31
  381. package/dist/do-runner/thread-header.d.ts.map +0 -1
  382. package/dist/do-runner/thread-header.js +0 -39
  383. package/dist/do-runner/thread-header.js.map +0 -1
  384. package/dist/host-kit/subdomain-check.d.ts +0 -23
  385. package/dist/host-kit/subdomain-check.d.ts.map +0 -1
  386. package/dist/host-kit/subdomain-check.js +0 -61
  387. package/dist/host-kit/subdomain-check.js.map +0 -1
  388. package/dist/host-kit/tenant-registry.d.ts +0 -51
  389. package/dist/host-kit/tenant-registry.d.ts.map +0 -1
  390. package/dist/host-kit/tenant-registry.js +0 -88
  391. package/dist/host-kit/tenant-registry.js.map +0 -1
@@ -1,22 +1,22 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  // Track D (M-006), CI-M-006-002 — createScheduleTick. WE OWN THE TICK (DL-012):
3
- // a cron-triggered scheduled() (or DO alarm) drives listDueSchedules -> CAS
3
+ // a Durable Object alarm drives listDueSchedules -> CAS
4
4
  // updateScheduleNextFire claim -> fire, bypassing core's pubsub worker loop
5
5
  // entirely (the P1 "one chokepoint, no second execution path" rule). Core's own
6
6
  // pubsub-driven schedule-worker loop is deliberately not adopted.
7
7
  //
8
8
  // TARGET KINDS:
9
- // - WORKFLOW targets: mint a fresh INV-1 runId `${tenantId}_${uuid}` and fire
9
+ // - WORKFLOW targets: mint a fresh path-safe runId and fire
10
10
  // through the host's run-start seam (topology.start / RunnerRuntime.start),
11
11
  // so the run inherits INV-1, the per-leg requestContext derivation, and the
12
- // resume ledger. This is the fully-owned path.
12
+ // snapshot provenance. This is the fully-owned path.
13
13
  // - AGENT targets: `startAgent` routes the claimed target through the host's
14
14
  // runtime-driven thread DO. Without that seam, the tick retains the audited
15
15
  // `agent-target-unsupported` skip and never adopts core's worker.
16
16
  //
17
17
  // CAP (DL-007, P7): every unattended workflow start consults an INJECTABLE
18
18
  // run-cap seam (host-agnostic; the showcase's demo caps are one implementation).
19
- // A capped tenant yields an audited skip and the schedule stays healthy — the
19
+ // A capped deployment yields an audited skip and the schedule stays healthy — the
20
20
  // CAS claim already advanced nextFireAt, so a capped fire is CONSUMED, not
21
21
  // retried hot (spike D-S4).
22
22
  //
@@ -29,14 +29,14 @@
29
29
  // there is normally nothing to collide. buildScheduledLegContext keeps the
30
30
  // R-004 order (stored FIRST, runtime-derived LAST) as defense-in-depth for a host
31
31
  // that applies the stored context, so a reserved key that ever slipped the strip
32
- // still LOSES to the runtime value. NOTE the flowsafe DO topology's start seam
33
- // carries only inputData (the frozen /runs route), so on the DO path stored
34
- // context is DROPPED entirely (strictly more fail-closed); a local-runtime host
35
- // can apply the tick-provided requestContext.
32
+ // still LOSES to the runtime value. The DO target resolves this sanitized
33
+ // context from the exact prepared trigger snapshot and RunnerRuntime merges it
34
+ // below provider/runtime-derived values; it never trusts a forwarded body copy.
36
35
  import { ScheduleInputSchema } from '@mastra/core/schedules';
37
36
  import { computeNextFireAt } from '@mastra/core/workflows';
38
- import { assertMintableTenantId, isReservedExecutionContextKey, mintSaltedId, RESERVED_EXECUTION_CONTEXT_KEYS, stripReservedExecutionContext, tenantOwnsSaltedId, } from '../do-runner/index.js';
37
+ import { isPathSafeId, isReservedExecutionContextKey, RESERVED_EXECUTION_CONTEXT_KEYS, resolveScheduleStartOwner, stripReservedExecutionContext, } from '../do-runner/index.js';
39
38
  import { nonnegativeSafeInteger } from '../numeric-config.js';
39
+ import { scheduleCreatorRole, } from './target-policy.js';
40
40
  /**
41
41
  * Compatibility alias for the former schedule-specific inventory. New code
42
42
  * should use RESERVED_EXECUTION_CONTEXT_KEYS from do-runner.
@@ -71,16 +71,26 @@ export function stripReservedScheduleContext(stored) {
71
71
  export function buildScheduledLegContext(stored, runtimeDerived) {
72
72
  return { ...stripReservedScheduleContext(stored), ...runtimeDerived };
73
73
  }
74
- function isMintableTenant(tenantId) {
75
- if (typeof tenantId !== 'string')
76
- return false;
77
- try {
78
- assertMintableTenantId(tenantId, 'scheduleTick');
79
- return true;
80
- }
81
- catch {
82
- return false;
74
+ /**
75
+ * Authorize durable persistence requested by a schedule while keeping the
76
+ * executing principal as SYSTEM. The schedule and its fixed thread/resource
77
+ * must share one committed registered owner.
78
+ */
79
+ export async function canPersistScheduledAgentSignal(store, resources, input) {
80
+ return ((await resolveScheduleStartOwner(store, resources, input.scheduleId, input.dispatchId, input.runId, {
81
+ type: 'agent',
82
+ mode: 'threaded-signal',
83
+ agentId: input.agentId,
84
+ threadId: input.threadId,
85
+ resourceId: input.resourceId,
86
+ })) !== undefined);
87
+ }
88
+ function mintPathSafeId(label) {
89
+ const id = crypto.randomUUID();
90
+ if (!isPathSafeId(id)) {
91
+ throw new Error(`${label}: generated id must be URL-path-safe`);
83
92
  }
93
+ return id;
84
94
  }
85
95
  function contextObject(value) {
86
96
  if (value === undefined)
@@ -90,9 +100,26 @@ function contextObject(value) {
90
100
  }
91
101
  return value;
92
102
  }
93
- function normalizeAndSanitizeAgentTarget(scheduleId, stored) {
94
- if (typeof stored.agentId !== 'string' ||
95
- stored.agentId.length === 0 ||
103
+ function normalizeAndSanitizeWorkflowTarget(stored) {
104
+ if (!isPathSafeId(stored.workflowId))
105
+ return undefined;
106
+ const requestContext = contextObject(stored.requestContext);
107
+ if (requestContext === null)
108
+ return undefined;
109
+ return {
110
+ type: 'workflow',
111
+ workflowId: stored.workflowId,
112
+ ...(stored.inputData !== undefined ? { inputData: stored.inputData } : {}),
113
+ ...(stored.initialState !== undefined
114
+ ? { initialState: stored.initialState }
115
+ : {}),
116
+ ...(requestContext !== undefined
117
+ ? { requestContext: stripReservedScheduleContext(requestContext) }
118
+ : {}),
119
+ };
120
+ }
121
+ export function normalizeAndSanitizeAgentTarget(scheduleId, stored) {
122
+ if (!isPathSafeId(stored.agentId) ||
96
123
  typeof stored.prompt !== 'string' ||
97
124
  stored.prompt.length === 0 ||
98
125
  (stored.name !== undefined && typeof stored.name !== 'string')) {
@@ -145,6 +172,19 @@ function normalizeAndSanitizeAgentTarget(scheduleId, stored) {
145
172
  if (!parsed.success)
146
173
  return undefined;
147
174
  const { scheduleId: _scheduleId, agentId, prompt, ...runtimeFields } = parsed.data;
175
+ if ((runtimeFields.threadId !== undefined &&
176
+ !isPathSafeId(runtimeFields.threadId)) ||
177
+ (runtimeFields.resourceId !== undefined &&
178
+ !isPathSafeId(runtimeFields.resourceId)) ||
179
+ (runtimeFields.threadId !== undefined &&
180
+ runtimeFields.resourceId === undefined) ||
181
+ (runtimeFields.threadId === undefined &&
182
+ (runtimeFields.resourceId !== undefined ||
183
+ runtimeFields.signalType !== undefined ||
184
+ runtimeFields.ifActive !== undefined ||
185
+ runtimeFields.ifIdle !== undefined))) {
186
+ return undefined;
187
+ }
148
188
  return {
149
189
  type: 'agent',
150
190
  agentId,
@@ -156,9 +196,122 @@ function normalizeAndSanitizeAgentTarget(scheduleId, stored) {
156
196
  : {}),
157
197
  };
158
198
  }
199
+ function agentScheduleProviderOptions(scheduleId, target) {
200
+ const base = target.providerOptions ?? {};
201
+ const mastra = typeof base.mastra === 'object' &&
202
+ base.mastra !== null &&
203
+ !Array.isArray(base.mastra)
204
+ ? base.mastra
205
+ : {};
206
+ return {
207
+ ...base,
208
+ mastra: {
209
+ ...mastra,
210
+ schedule: {
211
+ scheduleId,
212
+ ...(target.threadId !== undefined ? { threadId: target.threadId } : {}),
213
+ },
214
+ },
215
+ };
216
+ }
217
+ function scheduleTickDispatchRef(value) {
218
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
219
+ return undefined;
220
+ }
221
+ const candidate = value;
222
+ if (!isPathSafeId(candidate.scheduleId) ||
223
+ !isPathSafeId(candidate.dispatchId) ||
224
+ !isPathSafeId(candidate.runId)) {
225
+ return undefined;
226
+ }
227
+ if (candidate.target === 'workflow' && isPathSafeId(candidate.workflowId)) {
228
+ const stored = candidate.workflowTarget;
229
+ if (stored === null ||
230
+ typeof stored !== 'object' ||
231
+ Array.isArray(stored) ||
232
+ stored.type !== 'workflow') {
233
+ return undefined;
234
+ }
235
+ const workflowTarget = normalizeAndSanitizeWorkflowTarget(stored);
236
+ if (!workflowTarget || workflowTarget.workflowId !== candidate.workflowId) {
237
+ return undefined;
238
+ }
239
+ return {
240
+ scheduleId: candidate.scheduleId,
241
+ dispatchId: candidate.dispatchId,
242
+ target: 'workflow',
243
+ workflowId: candidate.workflowId,
244
+ runId: candidate.runId,
245
+ workflowTarget,
246
+ };
247
+ }
248
+ if (candidate.target !== 'agent' ||
249
+ !isPathSafeId(candidate.agentId) ||
250
+ !isPathSafeId(candidate.threadId) ||
251
+ (candidate.mode !== 'start' && candidate.mode !== 'signal')) {
252
+ return undefined;
253
+ }
254
+ const stored = candidate.agentTarget;
255
+ if (stored === null ||
256
+ typeof stored !== 'object' ||
257
+ Array.isArray(stored) ||
258
+ stored.type !== 'agent') {
259
+ return undefined;
260
+ }
261
+ const agentTarget = normalizeAndSanitizeAgentTarget(candidate.scheduleId, stored);
262
+ if (!agentTarget ||
263
+ agentTarget.agentId !== candidate.agentId ||
264
+ (candidate.mode === 'start' && agentTarget.threadId !== undefined) ||
265
+ (candidate.mode === 'signal' && agentTarget.threadId !== candidate.threadId)) {
266
+ return undefined;
267
+ }
268
+ return {
269
+ scheduleId: candidate.scheduleId,
270
+ dispatchId: candidate.dispatchId,
271
+ target: 'agent',
272
+ agentId: candidate.agentId,
273
+ threadId: candidate.threadId,
274
+ runId: candidate.runId,
275
+ mode: candidate.mode,
276
+ agentTarget,
277
+ };
278
+ }
279
+ /**
280
+ * Resolve the canonical target snapshot bound to one atomically claimed fire.
281
+ * A reusable schedule id alone never authorizes a start.
282
+ */
283
+ export function createScheduleStartSource(store) {
284
+ return {
285
+ async resolveScheduleTarget(scheduleId, dispatchId, runId) {
286
+ const trigger = await store.getClaimedScheduleDispatch(scheduleId, dispatchId, runId);
287
+ const metadata = trigger?.metadata;
288
+ if (trigger?.id !== dispatchId ||
289
+ trigger.scheduleId !== scheduleId ||
290
+ trigger.runId !== runId ||
291
+ trigger.outcome !== 'deferred' ||
292
+ !metadata ||
293
+ !['prepared', 'executing', 'settled'].includes(String(metadata.dispatchState))) {
294
+ return undefined;
295
+ }
296
+ const ref = scheduleTickDispatchRef(metadata.dispatchRef);
297
+ if (!ref ||
298
+ ref.scheduleId !== scheduleId ||
299
+ ref.dispatchId !== dispatchId ||
300
+ ref.runId !== runId) {
301
+ return undefined;
302
+ }
303
+ return ref.target === 'workflow'
304
+ ? ref.workflowTarget
305
+ : {
306
+ ...ref.agentTarget,
307
+ providerOptions: agentScheduleProviderOptions(scheduleId, ref.agentTarget),
308
+ };
309
+ },
310
+ };
311
+ }
159
312
  /**
160
313
  * Build the schedule tick: a `() => Promise<ScheduleTickResult>` a host slots
161
- * into its cron dispatch as its OWN failure-isolated duty (own try/catch, own
314
+ * into its alarm dispatch as its OWN failure-isolated duty (own try/catch, own
162
315
  * log line — the purge-availability lesson). Each due schedule is processed
163
316
  * independently: one bad row (a start throw, a corrupt cron) is contained and the
164
317
  * rest still fire.
@@ -173,9 +326,212 @@ export function createScheduleTick(options) {
173
326
  await options.audit({
174
327
  type: 'schedule.fire',
175
328
  timestamp: new Date().toISOString(),
329
+ ...(options.deploymentTag !== undefined
330
+ ? { deploymentTag: options.deploymentTag }
331
+ : {}),
176
332
  ...event,
177
333
  });
178
334
  };
335
+ const triggerMetadata = (extra = {}) => ({
336
+ ...(options.deploymentTag !== undefined
337
+ ? { deploymentTag: options.deploymentTag }
338
+ : {}),
339
+ ...extra,
340
+ });
341
+ const recordResolvedDispatch = async (ref, trigger, summary, result) => {
342
+ const published = summary !== undefined;
343
+ const receipt = summary?.dispatchReceipt;
344
+ const outcome = receipt?.outcome ?? (published ? 'published' : 'failed');
345
+ const resolvedRunId = receipt?.runId ?? summary?.runId ?? ref.runId;
346
+ const { error: priorError, ...resolvedTrigger } = trigger;
347
+ if (!published)
348
+ result.failed += 1;
349
+ else if (receipt &&
350
+ (receipt.outcome === 'persisted' ||
351
+ receipt.outcome === 'discarded' ||
352
+ receipt.outcome === 'skipped')) {
353
+ result.skipped += 1;
354
+ }
355
+ else {
356
+ result.fired += 1;
357
+ }
358
+ try {
359
+ await store.recordTrigger({
360
+ ...resolvedTrigger,
361
+ runId: published ? resolvedRunId : ref.runId,
362
+ outcome,
363
+ ...(published ? {} : { error: priorError ?? 'start failed' }),
364
+ metadata: triggerMetadata({
365
+ ...(trigger.metadata ?? {}),
366
+ dispatchRef: ref,
367
+ reason: published ? 'dispatch-reconciled' : 'start-error-confirmed',
368
+ }),
369
+ });
370
+ await audit({
371
+ scheduleId: ref.scheduleId,
372
+ target: ref.target,
373
+ outcome,
374
+ runId: resolvedRunId,
375
+ ...(published ? {} : { reason: 'start-error-confirmed' }),
376
+ });
377
+ }
378
+ catch (error) {
379
+ console.error(JSON.stringify({
380
+ type: 'schedule-tick-bookkeeping-error',
381
+ scheduleId: ref.scheduleId,
382
+ runId: ref.runId,
383
+ error: error instanceof Error ? error.message : String(error),
384
+ }));
385
+ }
386
+ };
387
+ const reconcileDeferred = async (result) => {
388
+ const pending = await store.listDeferredTriggers(limit);
389
+ for (const trigger of pending) {
390
+ const ref = scheduleTickDispatchRef(trigger.metadata?.dispatchRef);
391
+ if (!ref) {
392
+ result.failed += 1;
393
+ result.reconciled += 1;
394
+ await store.recordTrigger({
395
+ ...trigger,
396
+ outcome: 'failed',
397
+ error: 'stored deferred dispatch is malformed',
398
+ metadata: triggerMetadata({ reason: 'invalid-deferred-dispatch' }),
399
+ });
400
+ continue;
401
+ }
402
+ try {
403
+ const summary = await options.status(ref);
404
+ result.reconciled += 1;
405
+ await recordResolvedDispatch(ref, trigger, summary, result);
406
+ }
407
+ catch (error) {
408
+ let pendingError = error;
409
+ if (ref.target === 'agent' &&
410
+ ref.mode === 'signal' &&
411
+ options.signalAgent) {
412
+ try {
413
+ const receipt = await options.signalAgent({
414
+ scheduleId: ref.scheduleId,
415
+ target: ref.agentTarget,
416
+ ...(options.deploymentTag !== undefined
417
+ ? { deploymentTag: options.deploymentTag }
418
+ : {}),
419
+ dispatchId: ref.dispatchId,
420
+ runId: ref.runId,
421
+ topologyThreadId: ref.threadId,
422
+ threaded: true,
423
+ entryPath: 'schedule.fire',
424
+ requestContext: ref.agentTarget.requestContext ?? {},
425
+ streamRequestContext: ref.agentTarget.ifIdle?.streamOptions?.requestContext ?? {},
426
+ providerOptions: agentScheduleProviderOptions(ref.scheduleId, ref.agentTarget),
427
+ });
428
+ result.reconciled += 1;
429
+ await recordResolvedDispatch(ref, trigger, {
430
+ ...(receipt.runId !== undefined
431
+ ? { runId: receipt.runId }
432
+ : {}),
433
+ dispatchReceipt: receipt,
434
+ }, result);
435
+ continue;
436
+ }
437
+ catch (retryError) {
438
+ pendingError = retryError;
439
+ }
440
+ }
441
+ result.deferred += 1;
442
+ const priorAttempts = trigger.metadata?.reconcileAttempts;
443
+ const attempts = typeof priorAttempts === 'number' &&
444
+ Number.isSafeInteger(priorAttempts) &&
445
+ priorAttempts >= 0
446
+ ? priorAttempts
447
+ : 0;
448
+ const priorAfter = trigger.metadata?.reconcileAfter;
449
+ const reconcileAfter = Math.max(now(), typeof priorAfter === 'number' && Number.isSafeInteger(priorAfter)
450
+ ? priorAfter + 1
451
+ : trigger.actualFireAt + 1);
452
+ try {
453
+ await store.touchDeferredTrigger(trigger.id ?? '', trigger.scheduleId, trigger.error, triggerMetadata({
454
+ reconcileAttempts: attempts + 1,
455
+ reconcileAfter,
456
+ statusError: pendingError instanceof Error
457
+ ? pendingError.message
458
+ : String(pendingError),
459
+ }));
460
+ }
461
+ catch (bookkeepingError) {
462
+ console.error(JSON.stringify({
463
+ type: 'schedule-tick-bookkeeping-error',
464
+ scheduleId: ref.scheduleId,
465
+ runId: ref.runId,
466
+ error: bookkeepingError instanceof Error
467
+ ? bookkeepingError.message
468
+ : String(bookkeepingError),
469
+ }));
470
+ }
471
+ }
472
+ }
473
+ };
474
+ const reconcileStartError = async (ref, trigger, error, result) => {
475
+ try {
476
+ const summary = await options.status(ref);
477
+ await recordResolvedDispatch(ref, trigger, summary, result);
478
+ }
479
+ catch (statusError) {
480
+ result.deferred += 1;
481
+ try {
482
+ await store.touchDeferredTrigger(trigger.id ?? '', trigger.scheduleId, error instanceof Error ? error.message : String(error), triggerMetadata({
483
+ dispatchRef: ref,
484
+ reason: 'dispatch-indeterminate',
485
+ reconcileAttempts: 0,
486
+ reconcileAfter: trigger.actualFireAt,
487
+ statusError: statusError instanceof Error
488
+ ? statusError.message
489
+ : String(statusError),
490
+ }));
491
+ await audit({
492
+ scheduleId: ref.scheduleId,
493
+ target: ref.target,
494
+ outcome: 'deferred',
495
+ runId: ref.runId,
496
+ reason: 'dispatch-indeterminate',
497
+ });
498
+ }
499
+ catch (bookkeepingError) {
500
+ console.error(JSON.stringify({
501
+ type: 'schedule-tick-bookkeeping-error',
502
+ scheduleId: ref.scheduleId,
503
+ runId: ref.runId,
504
+ error: bookkeepingError instanceof Error
505
+ ? bookkeepingError.message
506
+ : String(bookkeepingError),
507
+ }));
508
+ }
509
+ }
510
+ };
511
+ const prepareDispatch = async (ref, trigger, result) => {
512
+ try {
513
+ await store.recordTrigger({
514
+ ...trigger,
515
+ metadata: triggerMetadata({
516
+ ...(trigger.metadata ?? {}),
517
+ dispatchRef: ref,
518
+ dispatchState: 'prepared',
519
+ reason: 'dispatch-indeterminate',
520
+ }),
521
+ });
522
+ return true;
523
+ }
524
+ catch (error) {
525
+ result.deferred += 1;
526
+ console.error(JSON.stringify({
527
+ type: 'schedule-tick-bookkeeping-error',
528
+ scheduleId: ref.scheduleId,
529
+ runId: ref.runId,
530
+ error: error instanceof Error ? error.message : String(error),
531
+ }));
532
+ return false;
533
+ }
534
+ };
179
535
  const fireOne = async (schedule, at, result) => {
180
536
  const targetType = schedule.target.type;
181
537
  // 1. Compute the next fire time. A corrupt cron cannot be advanced — the
@@ -202,24 +558,40 @@ export function createScheduleTick(options) {
202
558
  });
203
559
  return;
204
560
  }
205
- // 2. Resolve the tenant from metadata (re-validated INV-3 at fire time).
206
- const rawTenant = schedule.metadata
207
- ?.tenantId;
208
- const tenantId = isMintableTenant(rawTenant) ? rawTenant : undefined;
209
- // 3. Provisional runId — only a workflow target with a valid tenant mints one
210
- // (the CAS lastRunId). Agent / capped / invalid fires carry no run.
211
- const runId = tenantId !== undefined
212
- ? mintSaltedId(tenantId, () => crypto.randomUUID(), 'scheduleTick')
213
- : undefined;
214
- // 4. CAS claim — the exactly-once gate (spike D-S1). The LOSER of two
215
- // concurrent ticks gets false here and fires nothing / records no trigger.
561
+ // 2. Mint the provisional runId before the CAS so lastRunId identifies this
562
+ // claim even when a cap later consumes it without dispatch.
563
+ const runId = mintPathSafeId('scheduleTick');
564
+ // 3. CAS claim — the single-claim gate (spike D-S1). The LOSER of two
565
+ // concurrent ticks gets false here and dispatches nothing. This serializes
566
+ // the claim only; downstream dispatch remains recoverable/at-least-once.
216
567
  // The cap is consulted AFTER the claim (DL-007), so `runId` is minted before
217
568
  // we know whether the fire will actually dispatch: on a capped fire the row's
218
569
  // `lastRunId` records this CLAIM's id though no run started. The trigger row
219
570
  // is the authoritative record of what happened (outcome + its own runId,
220
571
  // null for a skip); `lastRunId` is a best-effort last-claim marker, not a
221
572
  // guarantee the run was dispatched.
222
- const claimed = await store.updateScheduleNextFire(schedule.id, schedule.nextFireAt, newNextFireAt, at, runId ?? '');
573
+ const claimTrigger = {
574
+ id: mintPathSafeId('scheduleTrigger'),
575
+ scheduleId: schedule.id,
576
+ runId,
577
+ scheduledFireAt: schedule.nextFireAt,
578
+ actualFireAt: at,
579
+ outcome: 'deferred',
580
+ error: 'dispatch has not reached an authoritative target state',
581
+ metadata: triggerMetadata({
582
+ reason: 'dispatch-preparing',
583
+ reconcileAttempts: 0,
584
+ reconcileAfter: at,
585
+ }),
586
+ };
587
+ const claimed = await store.claimScheduleFire({
588
+ scheduleId: schedule.id,
589
+ expectedNextFireAt: schedule.nextFireAt,
590
+ newNextFireAt,
591
+ actualFireAt: at,
592
+ runId,
593
+ trigger: claimTrigger,
594
+ });
223
595
  if (!claimed) {
224
596
  result.lost += 1;
225
597
  const current = await store.getSchedule(schedule.id);
@@ -229,106 +601,76 @@ export function createScheduleTick(options) {
229
601
  ? 'paused'
230
602
  : 'concurrent-claim';
231
603
  const lostReason = `lost: ${reason}`;
232
- await store.recordTrigger({
233
- scheduleId: schedule.id,
234
- runId: null,
235
- scheduledFireAt: schedule.nextFireAt,
236
- actualFireAt: at,
237
- outcome: 'skipped',
238
- metadata: { ...(tenantId ? { tenantId } : {}), reason: lostReason },
239
- });
240
604
  await audit({
241
605
  scheduleId: schedule.id,
242
- ...(tenantId ? { tenantId } : {}),
243
606
  target: targetType,
244
607
  outcome: 'lost',
245
608
  reason: lostReason,
246
609
  });
247
610
  return;
248
611
  }
249
- // 5. We OWN this fire — nextFireAt is already advanced (consumed, never
612
+ // 4. We own this fire — nextFireAt is already advanced (consumed, never
250
613
  // hot-looped), whatever happens below.
251
- const scheduledFireAt = schedule.nextFireAt;
252
- if (tenantId === undefined) {
253
- // Defense-in-depth: the facade always stamps a valid metadata.tenantId, so
254
- // this is a tampered/legacy row. Fail closed — no runId can be minted.
614
+ let targetDecision;
615
+ try {
616
+ targetDecision = options.targetPolicy.authorize(schedule.target, scheduleCreatorRole(schedule));
617
+ }
618
+ catch {
619
+ // A legacy/corrupt row with no creator role is never dispatched.
620
+ }
621
+ if (!targetDecision?.allowed) {
622
+ const reason = targetDecision?.reason ?? 'invalid-creator-role';
255
623
  result.failed += 1;
256
624
  await store.recordTrigger({
257
- scheduleId: schedule.id,
625
+ ...claimTrigger,
258
626
  runId: null,
259
- scheduledFireAt,
260
- actualFireAt: at,
261
627
  outcome: 'failed',
262
- metadata: { reason: 'invalid-tenant' },
628
+ error: reason,
629
+ metadata: triggerMetadata({ reason }),
263
630
  });
264
631
  await audit({
265
632
  scheduleId: schedule.id,
266
633
  target: targetType,
267
634
  outcome: 'failed',
268
- reason: 'invalid-tenant',
635
+ reason,
269
636
  });
270
637
  return;
271
638
  }
272
639
  if (targetType === 'agent') {
273
- if (!options.startAgent) {
274
- result.skipped += 1;
275
- await store.recordTrigger({
276
- scheduleId: schedule.id,
277
- runId: null,
278
- scheduledFireAt,
279
- actualFireAt: at,
280
- outcome: 'skipped',
281
- metadata: { tenantId, reason: 'agent-target-unsupported' },
282
- });
283
- await audit({
284
- scheduleId: schedule.id,
285
- tenantId,
286
- target: 'agent',
287
- outcome: 'skipped',
288
- reason: 'agent-target-unsupported',
289
- });
290
- return;
291
- }
292
640
  const target = normalizeAndSanitizeAgentTarget(schedule.id, schedule.target);
293
641
  if (!target) {
294
642
  result.failed += 1;
295
643
  await store.recordTrigger({
296
- scheduleId: schedule.id,
644
+ ...claimTrigger,
297
645
  runId: null,
298
- scheduledFireAt,
299
- actualFireAt: at,
300
646
  outcome: 'failed',
301
- metadata: { tenantId, reason: 'invalid-agent-target' },
647
+ error: 'invalid agent target',
648
+ metadata: triggerMetadata({ reason: 'invalid-agent-target' }),
302
649
  });
303
650
  await audit({
304
651
  scheduleId: schedule.id,
305
- tenantId,
306
652
  target: 'agent',
307
653
  outcome: 'failed',
308
654
  reason: 'invalid-agent-target',
309
655
  });
310
656
  return;
311
657
  }
312
- if ((target.threadId !== undefined &&
313
- !tenantOwnsSaltedId(tenantId, target.threadId)) ||
314
- (target.resourceId !== undefined &&
315
- !tenantOwnsSaltedId(tenantId, target.resourceId)) ||
658
+ if ((target.threadId !== undefined && !isPathSafeId(target.threadId)) ||
659
+ (target.resourceId !== undefined && !isPathSafeId(target.resourceId)) ||
316
660
  (target.threadId !== undefined && target.resourceId === undefined)) {
317
661
  result.failed += 1;
318
662
  await store.recordTrigger({
319
- scheduleId: schedule.id,
663
+ ...claimTrigger,
320
664
  runId: null,
321
- scheduledFireAt,
322
- actualFireAt: at,
323
665
  outcome: 'failed',
324
- metadata: { tenantId, reason: 'foreign-memory-id' },
666
+ error: 'invalid memory id',
667
+ metadata: triggerMetadata({ reason: 'invalid-memory-id' }),
325
668
  });
326
669
  await audit({
327
670
  scheduleId: schedule.id,
328
- tenantId,
329
671
  target: 'agent',
330
672
  outcome: 'failed',
331
- reason: 'foreign-memory-id',
673
+ reason: 'invalid-memory-id',
332
674
  });
333
675
  return;
334
676
  }
@@ -339,94 +681,203 @@ export function createScheduleTick(options) {
339
681
  target.ifIdle !== undefined)) {
340
682
  result.failed += 1;
341
683
  await store.recordTrigger({
342
- scheduleId: schedule.id,
684
+ ...claimTrigger,
343
685
  runId: null,
344
- scheduledFireAt,
345
- actualFireAt: at,
346
686
  outcome: 'failed',
347
- metadata: { tenantId, reason: 'invalid-agent-target' },
687
+ error: 'invalid agent target',
688
+ metadata: triggerMetadata({ reason: 'invalid-agent-target' }),
348
689
  });
349
690
  await audit({
350
691
  scheduleId: schedule.id,
351
- tenantId,
352
692
  target: 'agent',
353
693
  outcome: 'failed',
354
694
  reason: 'invalid-agent-target',
355
695
  });
356
696
  return;
357
697
  }
358
- const allowed = options.runCap ? await options.runCap(tenantId) : true;
359
- if (!allowed) {
698
+ const firedRunId = runId;
699
+ const threaded = target.threadId !== undefined;
700
+ const topologyThreadId = target.threadId ?? mintPathSafeId('scheduleThread');
701
+ const providerOptions = agentScheduleProviderOptions(schedule.id, target);
702
+ if (threaded) {
703
+ if (!options.signalAgent) {
704
+ result.skipped += 1;
705
+ await store.recordTrigger({
706
+ ...claimTrigger,
707
+ runId: null,
708
+ outcome: 'skipped',
709
+ error: undefined,
710
+ metadata: triggerMetadata({ reason: 'agent-target-unsupported' }),
711
+ });
712
+ await audit({
713
+ scheduleId: schedule.id,
714
+ target: 'agent',
715
+ outcome: 'skipped',
716
+ reason: 'agent-target-unsupported',
717
+ });
718
+ return;
719
+ }
720
+ const dispatch = {
721
+ scheduleId: schedule.id,
722
+ target: 'agent',
723
+ agentId: target.agentId,
724
+ threadId: topologyThreadId,
725
+ runId: firedRunId,
726
+ mode: 'signal',
727
+ dispatchId: claimTrigger.id,
728
+ agentTarget: target,
729
+ };
730
+ if (!(await prepareDispatch(dispatch, claimTrigger, result)))
731
+ return;
732
+ let receipt;
733
+ try {
734
+ receipt = await options.signalAgent({
735
+ scheduleId: schedule.id,
736
+ target,
737
+ ...(options.deploymentTag !== undefined
738
+ ? { deploymentTag: options.deploymentTag }
739
+ : {}),
740
+ dispatchId: claimTrigger.id,
741
+ runId: firedRunId,
742
+ topologyThreadId,
743
+ threaded: true,
744
+ entryPath: 'schedule.fire',
745
+ requestContext: target.requestContext ?? {},
746
+ streamRequestContext: target.ifIdle?.streamOptions?.requestContext ?? {},
747
+ providerOptions,
748
+ });
749
+ }
750
+ catch (error) {
751
+ await reconcileStartError(dispatch, {
752
+ ...claimTrigger,
753
+ error: error instanceof Error ? error.message : String(error),
754
+ metadata: triggerMetadata({ dispatchRef: dispatch }),
755
+ }, error, result);
756
+ return;
757
+ }
758
+ if (receipt.outcome === 'persisted' ||
759
+ receipt.outcome === 'discarded' ||
760
+ receipt.outcome === 'skipped') {
761
+ result.skipped += 1;
762
+ }
763
+ else {
764
+ result.fired += 1;
765
+ }
766
+ try {
767
+ await store.recordTrigger({
768
+ ...claimTrigger,
769
+ runId: receipt.runId ?? null,
770
+ outcome: receipt.outcome,
771
+ error: undefined,
772
+ metadata: triggerMetadata({
773
+ action: receipt.action,
774
+ ...(receipt.signalId !== undefined
775
+ ? { signalId: receipt.signalId }
776
+ : {}),
777
+ }),
778
+ });
779
+ await audit({
780
+ scheduleId: schedule.id,
781
+ target: 'agent',
782
+ outcome: receipt.outcome,
783
+ ...(receipt.runId !== undefined ? { runId: receipt.runId } : {}),
784
+ });
785
+ }
786
+ catch (error) {
787
+ console.error(JSON.stringify({
788
+ type: 'schedule-tick-bookkeeping-error',
789
+ scheduleId: schedule.id,
790
+ runId: receipt.runId ?? firedRunId,
791
+ error: error instanceof Error ? error.message : String(error),
792
+ }));
793
+ }
794
+ return;
795
+ }
796
+ if (!options.startAgent) {
360
797
  result.skipped += 1;
361
798
  await store.recordTrigger({
799
+ ...claimTrigger,
800
+ runId: null,
801
+ outcome: 'skipped',
802
+ error: undefined,
803
+ metadata: triggerMetadata({ reason: 'agent-target-unsupported' }),
804
+ });
805
+ await audit({
362
806
  scheduleId: schedule.id,
807
+ target: 'agent',
808
+ outcome: 'skipped',
809
+ reason: 'agent-target-unsupported',
810
+ });
811
+ return;
812
+ }
813
+ const allowed = options.runCap ? await options.runCap() : true;
814
+ if (!allowed) {
815
+ result.skipped += 1;
816
+ await store.recordTrigger({
817
+ ...claimTrigger,
363
818
  runId: null,
364
- scheduledFireAt,
365
- actualFireAt: at,
366
819
  outcome: 'skipped',
367
- metadata: { tenantId, reason: 'run-capped' },
820
+ error: undefined,
821
+ metadata: triggerMetadata({ reason: 'run-capped' }),
368
822
  });
369
823
  await audit({
370
824
  scheduleId: schedule.id,
371
- tenantId,
372
825
  target: 'agent',
373
826
  outcome: 'skipped',
374
827
  reason: 'run-capped',
375
828
  });
376
829
  return;
377
830
  }
378
- const firedRunId = runId;
379
- const threaded = target.threadId !== undefined;
380
- const topologyThreadId = target.threadId ??
381
- mintSaltedId(tenantId, () => crypto.randomUUID(), 'schedule agent thread');
831
+ const dispatch = {
832
+ scheduleId: schedule.id,
833
+ dispatchId: claimTrigger.id,
834
+ target: 'agent',
835
+ agentId: target.agentId,
836
+ threadId: topologyThreadId,
837
+ runId: firedRunId,
838
+ mode: 'start',
839
+ agentTarget: target,
840
+ };
841
+ if (!(await prepareDispatch(dispatch, claimTrigger, result)))
842
+ return;
382
843
  let summary;
383
844
  try {
384
845
  summary = await options.startAgent({
846
+ scheduleId: schedule.id,
847
+ dispatchId: claimTrigger.id,
385
848
  target,
386
- tenantId,
849
+ ...(options.deploymentTag !== undefined
850
+ ? { deploymentTag: options.deploymentTag }
851
+ : {}),
387
852
  runId: firedRunId,
388
853
  topologyThreadId,
389
854
  threaded,
390
855
  entryPath: 'schedule.fire',
391
856
  requestContext: target.requestContext ?? {},
392
857
  streamRequestContext: target.ifIdle?.streamOptions?.requestContext ?? {},
858
+ providerOptions,
393
859
  });
394
860
  }
395
861
  catch (error) {
396
- result.failed += 1;
397
- await store.recordTrigger({
398
- scheduleId: schedule.id,
399
- runId: firedRunId,
400
- scheduledFireAt,
401
- actualFireAt: at,
402
- outcome: 'failed',
862
+ await reconcileStartError(dispatch, {
863
+ ...claimTrigger,
403
864
  error: error instanceof Error ? error.message : String(error),
404
- metadata: { tenantId },
405
- });
406
- await audit({
407
- scheduleId: schedule.id,
408
- tenantId,
409
- target: 'agent',
410
- outcome: 'failed',
411
- runId: firedRunId,
412
- reason: 'start-error',
413
- });
865
+ metadata: triggerMetadata({ dispatchRef: dispatch }),
866
+ }, error, result);
414
867
  return;
415
868
  }
416
869
  const dispatchedRunId = summary.runId ?? firedRunId;
417
870
  result.fired += 1;
418
871
  try {
419
872
  await store.recordTrigger({
420
- scheduleId: schedule.id,
873
+ ...claimTrigger,
421
874
  runId: dispatchedRunId,
422
- scheduledFireAt,
423
- actualFireAt: at,
424
875
  outcome: 'published',
425
- metadata: { tenantId },
876
+ error: undefined,
877
+ metadata: triggerMetadata(),
426
878
  });
427
879
  await audit({
428
880
  scheduleId: schedule.id,
429
- tenantId,
430
881
  target: 'agent',
431
882
  outcome: 'published',
432
883
  runId: dispatchedRunId,
@@ -442,22 +893,38 @@ export function createScheduleTick(options) {
442
893
  }
443
894
  return;
444
895
  }
445
- // 6. Workflow target, valid tenant: consult the run cap (DL-007).
446
- const allowed = options.runCap ? await options.runCap(tenantId) : true;
896
+ const target = normalizeAndSanitizeWorkflowTarget(schedule.target);
897
+ if (!target) {
898
+ result.failed += 1;
899
+ await store.recordTrigger({
900
+ ...claimTrigger,
901
+ runId: null,
902
+ outcome: 'failed',
903
+ error: 'invalid workflow target',
904
+ metadata: triggerMetadata({ reason: 'invalid-workflow-target' }),
905
+ });
906
+ await audit({
907
+ scheduleId: schedule.id,
908
+ target: 'workflow',
909
+ outcome: 'failed',
910
+ reason: 'invalid-workflow-target',
911
+ });
912
+ return;
913
+ }
914
+ // 5. Workflow target: consult the run cap (DL-007).
915
+ const allowed = options.runCap ? await options.runCap() : true;
447
916
  if (!allowed) {
448
917
  // Capped: the schedule stays healthy (already advanced), audited (D-S4).
449
918
  result.skipped += 1;
450
919
  await store.recordTrigger({
451
- scheduleId: schedule.id,
920
+ ...claimTrigger,
452
921
  runId: null,
453
- scheduledFireAt,
454
- actualFireAt: at,
455
922
  outcome: 'skipped',
456
- metadata: { tenantId, reason: 'run-capped' },
923
+ error: undefined,
924
+ metadata: triggerMetadata({ reason: 'run-capped' }),
457
925
  });
458
926
  await audit({
459
927
  scheduleId: schedule.id,
460
- tenantId,
461
928
  target: 'workflow',
462
929
  outcome: 'skipped',
463
930
  reason: 'run-capped',
@@ -466,37 +933,38 @@ export function createScheduleTick(options) {
466
933
  }
467
934
  // 7. Fire the workflow run (runId is defined by construction here).
468
935
  const firedRunId = runId;
469
- const target = schedule.target;
470
- // Barrier (b): hand the start seam only the NON-reserved stored context.
471
- const safeContext = stripReservedScheduleContext(target.type === 'workflow' ? target.requestContext : undefined);
936
+ const dispatch = {
937
+ scheduleId: schedule.id,
938
+ dispatchId: claimTrigger.id,
939
+ target: 'workflow',
940
+ workflowId: target.workflowId,
941
+ runId: firedRunId,
942
+ workflowTarget: target,
943
+ };
944
+ if (!(await prepareDispatch(dispatch, claimTrigger, result)))
945
+ return;
472
946
  // The try wraps ONLY the start() dispatch. A post-dispatch bookkeeping
473
947
  // failure (a transient recordTrigger/audit throw) must NOT be reclassified as
474
948
  // a start failure — that would double-count and audit a run that actually ran
475
949
  // as `failed`/`start-error`, corrupting the very trail operators rely on.
476
950
  let summary;
477
951
  try {
478
- summary = await start(target.type === 'workflow' ? target.workflowId : '', firedRunId, target.type === 'workflow' ? target.inputData : undefined, safeContext);
479
- }
480
- catch (error) {
481
- // The dispatch itself threw — no run started.
482
- result.failed += 1;
483
- await store.recordTrigger({
952
+ summary = await start({
484
953
  scheduleId: schedule.id,
954
+ dispatchId: claimTrigger.id,
955
+ workflowId: target.workflowId,
485
956
  runId: firedRunId,
486
- scheduledFireAt,
487
- actualFireAt: at,
488
- outcome: 'failed',
489
- error: error instanceof Error ? error.message : String(error),
490
- metadata: { tenantId },
491
- });
492
- await audit({
493
- scheduleId: schedule.id,
494
- tenantId,
495
- target: 'workflow',
496
- outcome: 'failed',
497
- runId: firedRunId,
498
- reason: 'start-error',
957
+ inputData: target.inputData,
958
+ initialState: target.initialState,
959
+ requestContext: target.requestContext ?? {},
499
960
  });
961
+ }
962
+ catch (error) {
963
+ await reconcileStartError(dispatch, {
964
+ ...claimTrigger,
965
+ error: error instanceof Error ? error.message : String(error),
966
+ metadata: triggerMetadata({ dispatchRef: dispatch }),
967
+ }, error, result);
500
968
  return;
501
969
  }
502
970
  // The run WAS dispatched. Record it as `published` (write-once at dispatch,
@@ -506,16 +974,14 @@ export function createScheduleTick(options) {
506
974
  result.fired += 1;
507
975
  try {
508
976
  await store.recordTrigger({
509
- scheduleId: schedule.id,
977
+ ...claimTrigger,
510
978
  runId: dispatchedRunId,
511
- scheduledFireAt,
512
- actualFireAt: at,
513
979
  outcome: 'published',
514
- metadata: { tenantId },
980
+ error: undefined,
981
+ metadata: triggerMetadata(),
515
982
  });
516
983
  await audit({
517
984
  scheduleId: schedule.id,
518
- tenantId,
519
985
  target: 'workflow',
520
986
  outcome: 'published',
521
987
  runId: dispatchedRunId,
@@ -532,7 +998,15 @@ export function createScheduleTick(options) {
532
998
  };
533
999
  return async () => {
534
1000
  if (limit === 0) {
535
- return { due: 0, fired: 0, skipped: 0, failed: 0, lost: 0 };
1001
+ return {
1002
+ due: 0,
1003
+ fired: 0,
1004
+ skipped: 0,
1005
+ failed: 0,
1006
+ deferred: 0,
1007
+ reconciled: 0,
1008
+ lost: 0,
1009
+ };
536
1010
  }
537
1011
  const at = now();
538
1012
  const due = await store.listDueSchedules(at, limit);
@@ -541,8 +1015,11 @@ export function createScheduleTick(options) {
541
1015
  fired: 0,
542
1016
  skipped: 0,
543
1017
  failed: 0,
1018
+ deferred: 0,
1019
+ reconciled: 0,
544
1020
  lost: 0,
545
1021
  };
1022
+ await reconcileDeferred(result);
546
1023
  for (const schedule of due) {
547
1024
  // Per-schedule isolation: one wedged row (an unexpected store/audit throw)
548
1025
  // must not abort the pass and strand every due schedule behind it.