@getstrata/core 2.2.60 → 3.0.3

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 (330) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/core/audit/exportAuditLogs.d.ts +12 -3
  3. package/dist/core/audit/exportStore.d.ts +6 -2
  4. package/dist/core/audit/siemFormatter.d.ts +2 -2
  5. package/dist/core/auth/abilityCatalog.d.ts +1 -9
  6. package/dist/core/auth/authContext.d.ts +2 -0
  7. package/dist/core/auth/authOutcome.d.ts +32 -0
  8. package/dist/core/auth/authReader.d.ts +22 -0
  9. package/dist/core/auth/basicAuthGuard.d.ts +19 -3
  10. package/dist/core/auth/emailVerification.d.ts +1 -1
  11. package/dist/core/auth/guard.d.ts +27 -6
  12. package/dist/core/auth/intendedUrlCookie.d.ts +13 -7
  13. package/dist/core/auth/jwt.d.ts +5 -4
  14. package/dist/core/auth/jwtGuard.d.ts +7 -6
  15. package/dist/core/auth/membershipContext.d.ts +10 -7
  16. package/dist/core/auth/membershipContextMiddleware.d.ts +3 -3
  17. package/dist/core/auth/membershipMiddleware.d.ts +1 -2
  18. package/dist/core/auth/membershipServiceImplementation.d.ts +1 -1
  19. package/dist/core/auth/oauth/oidcIdToken.d.ts +25 -16
  20. package/dist/core/auth/oauth/oidcProvider.d.ts +6 -4
  21. package/dist/core/auth/oauth/providers.d.ts +3 -1
  22. package/dist/core/auth/oneTimeToken.d.ts +2 -3
  23. package/dist/core/auth/passwordConfirmCookie.d.ts +15 -6
  24. package/dist/core/auth/passwordLogin.d.ts +5 -5
  25. package/dist/core/auth/pendingMfaCookie.d.ts +2 -2
  26. package/dist/core/auth/policy.d.ts +23 -10
  27. package/dist/core/auth/requireAuthCapability.d.ts +3 -0
  28. package/dist/core/auth/resolveMembershipService.d.ts +3 -2
  29. package/dist/core/auth/saml/samlAssertionReplay.d.ts +4 -4
  30. package/dist/core/auth/saml/samlServiceProvider.d.ts +11 -7
  31. package/dist/core/auth/scimAuthMiddleware.d.ts +10 -3
  32. package/dist/core/auth/sessionCookie.d.ts +20 -9
  33. package/dist/core/auth/sessionGuard.d.ts +14 -3
  34. package/dist/core/cache/createCacheStore.d.ts +6 -3
  35. package/dist/core/cache/redisCacheStore.d.ts +5 -1
  36. package/dist/core/cache/repository.d.ts +5 -1
  37. package/dist/core/cache/simpleCache.d.ts +2 -1
  38. package/dist/core/cache/tokens.d.ts +8 -0
  39. package/dist/core/config/applicationConfig.d.ts +21 -0
  40. package/dist/core/config/configSnapshot.d.ts +3 -0
  41. package/dist/core/config/envSchema.d.ts +3 -2
  42. package/dist/core/contracts/application.d.ts +153 -0
  43. package/dist/core/contracts/authUserDirectory.d.ts +24 -7
  44. package/dist/core/contracts/container.d.ts +20 -23
  45. package/dist/core/contracts/di.d.ts +4 -2
  46. package/dist/core/contracts/executionContext.d.ts +5 -0
  47. package/dist/core/contracts/executionOperations.d.ts +10 -0
  48. package/dist/core/contracts/executionOwnership.d.ts +38 -0
  49. package/dist/core/contracts/persistence.d.ts +20 -0
  50. package/dist/core/contracts/serviceTokens.d.ts +32 -18
  51. package/dist/core/contracts/services.d.ts +63 -0
  52. package/dist/core/contracts/token.d.ts +19 -0
  53. package/dist/core/crypto/fieldEncryption.d.ts +19 -7
  54. package/dist/core/crypto/fieldEncryptionRotation.d.ts +21 -5
  55. package/dist/core/crypto/mfaSecret.d.ts +10 -3
  56. package/dist/core/database/baseRepository.d.ts +14 -10
  57. package/dist/core/database/bindings.d.ts +6 -0
  58. package/dist/core/database/bunSql.d.ts +3 -12
  59. package/dist/core/database/definitions.d.ts +209 -0
  60. package/dist/core/database/dialect.d.ts +5 -24
  61. package/dist/core/database/driverPorts.d.ts +116 -0
  62. package/dist/core/database/errors.d.ts +47 -7
  63. package/dist/core/database/factory.d.ts +51 -31
  64. package/dist/core/database/index.d.ts +9 -10
  65. package/dist/core/database/keyset.d.ts +2 -1
  66. package/dist/core/database/legacyNativeTransaction.d.ts +5 -0
  67. package/dist/core/database/manager.d.ts +12 -0
  68. package/dist/core/database/model.d.ts +91 -97
  69. package/dist/core/database/modelQuery.d.ts +9 -5
  70. package/dist/core/database/modelRegistry.d.ts +52 -33
  71. package/dist/core/database/mysqlConnection.d.ts +1 -19
  72. package/dist/core/database/mysqlNative.d.ts +38 -0
  73. package/dist/core/database/nativeTransactions.d.ts +16 -0
  74. package/dist/core/database/ormInternal.d.ts +85 -0
  75. package/dist/core/database/ormRuntime.d.ts +4 -0
  76. package/dist/core/database/ownedReadOnlyObservation.d.ts +16 -0
  77. package/dist/core/database/postgresNative.d.ts +11 -0
  78. package/dist/core/database/query.d.ts +24 -23
  79. package/dist/core/database/queryPlanSnapshot.d.ts +7 -0
  80. package/dist/core/database/rawMysql.d.ts +7 -0
  81. package/dist/core/database/rawPostgres.d.ts +23 -0
  82. package/dist/core/database/rawSqlite.d.ts +6 -0
  83. package/dist/core/database/relationQueries.d.ts +3 -2
  84. package/dist/core/database/relationQuery.d.ts +24 -6
  85. package/dist/core/database/repositoryQuery.d.ts +6 -1
  86. package/dist/core/database/schema/driver.d.ts +1 -1
  87. package/dist/core/database/seeders/types.d.ts +1 -1
  88. package/dist/core/database/session.d.ts +13 -0
  89. package/dist/core/database/sessionFactory.d.ts +9 -0
  90. package/dist/core/database/sqlDialects.d.ts +22 -0
  91. package/dist/core/database/tokens.d.ts +7 -0
  92. package/dist/core/database/transactionSession.d.ts +43 -0
  93. package/dist/core/errors/http.d.ts +4 -1
  94. package/dist/core/events/{eventBus.d.ts → eventBusClass.d.ts} +1 -3
  95. package/dist/core/events/index.d.ts +2 -3
  96. package/dist/core/events/outbox/index.d.ts +44 -13
  97. package/dist/core/events/outbox/metrics.d.ts +4 -4
  98. package/dist/core/events/outbox/workerTransport.d.ts +17 -0
  99. package/dist/core/http/authMiddleware.d.ts +2 -2
  100. package/dist/core/http/authorizeMiddleware.d.ts +2 -2
  101. package/dist/core/http/bodySizeLimitMiddleware.d.ts +1 -1
  102. package/dist/core/http/conditionalResponse.d.ts +7 -2
  103. package/dist/core/http/contentSecurityPolicy.d.ts +4 -3
  104. package/dist/core/http/corsMiddleware.d.ts +2 -1
  105. package/dist/core/http/csrfMiddleware.d.ts +6 -1
  106. package/dist/core/http/csrfToken.d.ts +17 -7
  107. package/dist/core/http/databaseError.d.ts +4 -0
  108. package/dist/core/http/etag.d.ts +5 -2
  109. package/dist/core/http/flashMiddleware.d.ts +1 -1
  110. package/dist/core/http/flashSession.d.ts +13 -7
  111. package/dist/core/http/index.d.ts +1 -1
  112. package/dist/core/http/loginThrottleMiddleware.d.ts +3 -3
  113. package/dist/core/http/memoryThrottleMiddleware.d.ts +2 -2
  114. package/dist/core/http/parseMultipartUpload.d.ts +3 -2
  115. package/dist/core/http/requestMetaContext.d.ts +5 -0
  116. package/dist/core/http/requireAbilityMiddleware.d.ts +1 -1
  117. package/dist/core/http/requireAuthMiddleware.d.ts +2 -2
  118. package/dist/core/http/requirePasswordConfirmMiddleware.d.ts +2 -1
  119. package/dist/core/http/requireVerifiedMiddleware.d.ts +2 -2
  120. package/dist/core/http/requireWebAuthMiddleware.d.ts +2 -2
  121. package/dist/core/http/response.d.ts +4 -3
  122. package/dist/core/http/scimThrottleMiddleware.d.ts +4 -3
  123. package/dist/core/http/securedRouteModelBinding.d.ts +10 -3
  124. package/dist/core/http/securityHeadersMiddleware.d.ts +1 -2
  125. package/dist/core/http/signedUrl.d.ts +8 -6
  126. package/dist/core/http/throttleMiddleware.d.ts +9 -5
  127. package/dist/core/http/uploads.d.ts +9 -3
  128. package/dist/core/http/webErrorResponse.d.ts +8 -2
  129. package/dist/core/jobs/exportAuditLogsJob.d.ts +5 -2
  130. package/dist/core/jobs/invalidateCacheTagsJob.d.ts +5 -4
  131. package/dist/core/lifecycle/gracefulShutdown.d.ts +2 -5
  132. package/dist/core/logging/requestLoggingMiddleware.d.ts +1 -1
  133. package/dist/core/mail/mailer.d.ts +7 -5
  134. package/dist/core/openapi/generator.d.ts +12 -4
  135. package/dist/core/openapi/registeredRoute.d.ts +4 -4
  136. package/dist/core/queue/createAppQueue.d.ts +9 -4
  137. package/dist/core/queue/failedJobMetrics.d.ts +11 -2
  138. package/dist/core/queue/failedJobRepository.d.ts +6 -3
  139. package/dist/core/queue/failedJobService.d.ts +1 -0
  140. package/dist/core/queue/index.d.ts +16 -6
  141. package/dist/core/queue/jobRegistry.d.ts +3 -2
  142. package/dist/core/queue/jobRunner.d.ts +10 -4
  143. package/dist/core/queue/publicQueue.d.ts +18 -9
  144. package/dist/core/queue/queueConfig.d.ts +20 -10
  145. package/dist/core/queue/queueHandoff.d.ts +31 -0
  146. package/dist/core/queue/queueMetrics.d.ts +10 -3
  147. package/dist/core/queue/queueServices.d.ts +28 -0
  148. package/dist/core/queue/queueWorkerTransport.d.ts +28 -0
  149. package/dist/core/queue/redisQueue.d.ts +19 -10
  150. package/dist/core/queue/redisQueueKeys.d.ts +5 -5
  151. package/dist/core/queue/redisStreams.d.ts +17 -10
  152. package/dist/core/queue/resilientQueue.d.ts +10 -4
  153. package/dist/core/queue/types.d.ts +1 -0
  154. package/dist/core/runtime/appKeyPrefix.d.ts +5 -5
  155. package/dist/core/runtime/application.d.ts +7 -0
  156. package/dist/core/runtime/executionContext.d.ts +9 -0
  157. package/dist/core/runtime/executionTasks.d.ts +49 -0
  158. package/dist/core/runtime/resourceJournal.d.ts +67 -0
  159. package/dist/core/runtime/serviceGraph.d.ts +14 -0
  160. package/dist/core/security/oauthState.d.ts +13 -9
  161. package/dist/core/security/publicReads.d.ts +2 -2
  162. package/dist/core/security/safeFetch.d.ts +1 -0
  163. package/dist/core/security/safeUrl.d.ts +3 -0
  164. package/dist/core/security/scimTenantTokens.d.ts +1 -1
  165. package/dist/core/security/tokenExpiry.d.ts +1 -1
  166. package/dist/core/security/webhookSignature.d.ts +1 -1
  167. package/dist/core/storage/storage.d.ts +24 -16
  168. package/dist/core/tenant/databaseTenantContext.d.ts +5 -7
  169. package/dist/core/tenant/resolveTenant.d.ts +2 -2
  170. package/dist/core/tenant/scopedDatabasePolicy.d.ts +8 -0
  171. package/dist/core/tenant/tenantDatabaseScope.d.ts +3 -7
  172. package/dist/core/tenant/tenantMiddleware.d.ts +15 -4
  173. package/dist/core/tracing/otel.d.ts +4 -6
  174. package/dist/core/tracing/tracingMiddleware.d.ts +3 -3
  175. package/dist/core/view/etaViewEngine.d.ts +1 -1
  176. package/dist/core/view/index.d.ts +5 -4
  177. package/dist/core/view/tokens.d.ts +8 -0
  178. package/dist/core/view/viewRenderer.d.ts +2 -2
  179. package/dist/core/view/webErrorView.d.ts +8 -7
  180. package/dist/core/view/webLayoutData.d.ts +6 -4
  181. package/dist/entries/audit/exportAuditLogs.js +87 -1024
  182. package/dist/entries/audit/siemFormatter.js +4 -90
  183. package/dist/entries/auth/abilityCatalog.js +28 -0
  184. package/dist/entries/auth/accessControl.js +5 -16
  185. package/dist/entries/auth/accountStateMigration.js +216 -236
  186. package/dist/entries/auth/authOutcome.js +80 -0
  187. package/dist/entries/auth/authReader.js +68 -0
  188. package/dist/entries/auth/basicAuthGuard.js +144 -1249
  189. package/dist/entries/auth/emailVerification.js +2 -2
  190. package/dist/entries/auth/intendedUrlCookie.js +35 -59
  191. package/dist/entries/auth/jwt.js +22 -47
  192. package/dist/entries/auth/jwtGuard.js +99 -231
  193. package/dist/entries/auth/membershipMiddleware.js +71 -10
  194. package/dist/entries/auth/membershipScope.js +16 -22
  195. package/dist/entries/auth/membershipService.js +11 -28
  196. package/dist/entries/auth/oauth/oidcProvider.js +59 -64
  197. package/dist/entries/auth/oauth/providers.js +12 -95
  198. package/dist/entries/auth/oauth/samlProvider.js +53 -1080
  199. package/dist/entries/auth/oneTimeToken.js +27 -289
  200. package/dist/entries/auth/passwordConfirmCookie.js +40 -63
  201. package/dist/entries/auth/passwordLogin.js +94 -399
  202. package/dist/entries/auth/pendingMfaCookie.js +6 -6
  203. package/dist/entries/auth/policy.js +22 -56
  204. package/dist/entries/auth/saml/samlServiceProvider.js +55 -1084
  205. package/dist/entries/auth/scimAuthMiddleware.js +16 -3358
  206. package/dist/entries/auth/sessionCookie.js +58 -73
  207. package/dist/entries/auth/sessionGuard.js +95 -248
  208. package/dist/entries/auth/tokenHash.js +11 -43
  209. package/dist/entries/cache/createCacheStore.js +126 -117
  210. package/dist/entries/cache/repository.js +25 -0
  211. package/dist/entries/cache/simpleCache.js +12 -7
  212. package/dist/entries/cache/tokens.js +9 -0
  213. package/dist/entries/config/applicationConfig.js +163 -0
  214. package/dist/entries/config/envSchema.js +21 -4
  215. package/dist/entries/contracts/application.js +1 -0
  216. package/dist/entries/contracts/authUserDirectory.js +10 -0
  217. package/dist/entries/contracts/container.js +99 -26
  218. package/dist/entries/contracts/di.js +3 -0
  219. package/dist/entries/contracts/executionContext.js +7 -0
  220. package/dist/entries/contracts/executionOperations.js +7 -0
  221. package/dist/entries/contracts/executionOwnership.js +7 -0
  222. package/dist/entries/contracts/membership.js +1 -0
  223. package/dist/entries/contracts/persistence.js +1 -0
  224. package/dist/entries/contracts/serviceTokens.js +26 -116
  225. package/dist/entries/contracts/services.js +98 -0
  226. package/dist/entries/contracts/token.js +41 -0
  227. package/dist/entries/crypto/fieldEncryption.js +49 -50
  228. package/dist/entries/crypto/mfaSecret.js +86 -82
  229. package/dist/entries/database/acquisitionMetrics.js +107 -0
  230. package/dist/entries/database/baseRepository.d.ts +2 -0
  231. package/dist/entries/database/bindings.js +2082 -0
  232. package/dist/entries/database/definitions.js +175 -0
  233. package/dist/entries/database/errors.js +114 -89
  234. package/dist/entries/database/factory.js +67 -28
  235. package/dist/entries/database/migrations.js +172 -224
  236. package/dist/entries/database/model.d.ts +1 -0
  237. package/dist/entries/database/ownedReadOnlyObservation.js +1282 -0
  238. package/dist/entries/database/query.js +159 -161
  239. package/dist/entries/database/repositoryQuery.d.ts +1 -0
  240. package/dist/entries/database/repositoryQuery.js +1 -1373
  241. package/dist/entries/database/schema.js +216 -236
  242. package/dist/entries/database/session.js +1 -0
  243. package/dist/entries/database/sqliteConnection.js +525 -83
  244. package/dist/entries/database/tokens.js +9 -0
  245. package/dist/entries/errors/http.js +118 -1
  246. package/dist/entries/http/authMiddleware.js +8 -5
  247. package/dist/entries/http/authorizeMiddleware.js +1 -1
  248. package/dist/entries/http/bodySizeLimitMiddleware.js +2 -2
  249. package/dist/entries/http/clientIp.js +2 -2
  250. package/dist/entries/http/conditionalResponse.js +19 -7
  251. package/dist/entries/http/corsMiddleware.js +6 -101
  252. package/dist/entries/http/csrfMiddleware.js +92 -106
  253. package/dist/entries/http/csrfToken.js +58 -94
  254. package/dist/entries/http/etag.js +7 -4
  255. package/dist/entries/http/flashMiddleware.js +57 -99
  256. package/dist/entries/http/flashSession.js +41 -68
  257. package/dist/entries/http/memoryThrottleMiddleware.js +43 -186
  258. package/dist/entries/http/metricsMiddleware.js +2 -16
  259. package/dist/entries/http/parseMultipartUpload.js +15 -29
  260. package/dist/entries/http/requireAbilityMiddleware.js +2 -2
  261. package/dist/entries/http/requirePasswordConfirmMiddleware.js +19 -31
  262. package/dist/entries/http/requireVerifiedMiddleware.js +4 -7
  263. package/dist/entries/http/requireWebAuthMiddleware.js +2 -2
  264. package/dist/entries/http/response.js +67 -233
  265. package/dist/entries/http/routeMiddleware.js +0 -14
  266. package/dist/entries/http/scimThrottleMiddleware.js +43 -237
  267. package/dist/entries/http/securedRouteModelBinding.js +37 -25
  268. package/dist/entries/http/securityHeadersMiddleware.js +8 -25
  269. package/dist/entries/http/signedUrl.js +20 -110
  270. package/dist/entries/http/throttleMiddleware.js +41 -185
  271. package/dist/entries/http/uploads.js +9 -24
  272. package/dist/entries/jobs/exportAuditLogsJob.js +17 -1498
  273. package/dist/entries/jobs/invalidateCacheTagsJob.js +12 -47
  274. package/dist/entries/logging/requestLoggingMiddleware.js +4 -4
  275. package/dist/entries/mail/mailer.js +68 -78
  276. package/dist/entries/openapi/generator.js +33 -100
  277. package/dist/entries/queue/createAppQueue.js +416 -256
  278. package/dist/entries/queue/failedJobRepository.js +17 -8
  279. package/dist/entries/queue/failedJobService.js +1 -0
  280. package/dist/entries/queue/jobRunner.js +141 -66
  281. package/dist/entries/queue/publicQueue.js +411 -257
  282. package/dist/entries/queue/queueConfig.js +61 -0
  283. package/dist/entries/queue/queueHandoff.js +318 -0
  284. package/dist/entries/queue/queueMetrics.js +989 -1386
  285. package/dist/entries/queue/queueServices.js +133 -0
  286. package/dist/entries/queue/queueWorkerTransport.js +1703 -0
  287. package/dist/entries/queue/redisQueue.js +282 -180
  288. package/dist/entries/queue.js +6 -45
  289. package/dist/entries/runtime/application.js +1585 -0
  290. package/dist/entries/runtime/asyncContextStore.js +0 -16
  291. package/dist/entries/security/oauthState.js +35 -67
  292. package/dist/entries/security/publicReads.js +4 -4
  293. package/dist/entries/security/safeFetch.js +1 -0
  294. package/dist/entries/security/scimTenantTokens.js +3 -3
  295. package/dist/entries/security/tokenExpiry.js +2 -2
  296. package/dist/entries/security/totp.js +1 -87
  297. package/dist/entries/security/webhookSignature.js +2 -90
  298. package/dist/entries/storage/storage.js +120 -45
  299. package/dist/entries/tenant/databaseTenantContext.js +6 -907
  300. package/dist/entries/tenant/publicTenantHost.js +78 -0
  301. package/dist/entries/tenant/resolveTenant.js +14 -0
  302. package/dist/entries/tenant/scopedDatabasePolicy.js +75 -0
  303. package/dist/entries/tenant/tenancyConfig.js +1 -19
  304. package/dist/entries/tenant/tenantDatabaseScope.js +3 -892
  305. package/dist/entries/tracing/tracingMiddleware.js +104 -173
  306. package/dist/entries/view/tokens.js +11 -0
  307. package/dist/framework/public-api.d.ts +62 -44
  308. package/dist/index.js +10435 -11351
  309. package/dist/internal/ormInternal.js +117 -0
  310. package/dist/internal/ormRuntime.js +5000 -0
  311. package/package.json +158 -46
  312. package/dist/core/database/bindConnection.d.ts +0 -1
  313. package/dist/core/database/boundConnection.d.ts +0 -5
  314. package/dist/core/database/defaultConnection.d.ts +0 -7
  315. package/dist/core/database/namedConnections.d.ts +0 -15
  316. package/dist/core/database/readOnlyCollector.d.ts +0 -7
  317. package/dist/core/database/repositoryConnection.d.ts +0 -4
  318. package/dist/core/database/transaction.d.ts +0 -23
  319. package/dist/core/database/transactionControl.d.ts +0 -6
  320. package/dist/core/events/deferredModelEvents.d.ts +0 -7
  321. package/dist/core/facades/index.d.ts +0 -10
  322. package/dist/core/runtime/applicationRegistry.d.ts +0 -18
  323. package/dist/entries/database/bindConnection.js +0 -1
  324. package/dist/entries/database/boundConnection.js +0 -1
  325. package/dist/entries/database/defaultConnection.js +0 -1
  326. package/dist/entries/database/namedConnections.js +0 -1
  327. package/dist/entries/database/repositoryConnection.js +0 -1
  328. package/dist/entries/database/transaction.js +0 -1
  329. package/dist/entries/facades.js +0 -711
  330. package/dist/entries/runtime/applicationRegistry.js +0 -1
@@ -1,47 +1,6 @@
1
1
  // @bun
2
- // ../../src/core/queue/failedJobRepository.ts
3
- import { BaseRepository } from "@getstrata/core/database/baseRepository";
4
- import { currentSqlDialect, sqlTimestamp } from "@getstrata/core/database/dialect";
5
-
6
- // ../../src/core/queue/failedJobTable.ts
7
- import { defineTable } from "@getstrata/core/database/table";
8
- var failedJobTable = defineTable({
9
- name: "failed_job",
10
- primaryKey: "id",
11
- columns: ["id", "job_name", "job_id", "payload", "exception", "failed_at"],
12
- defaultOrderBy: { column: "failed_at", direction: "DESC" }
13
- });
14
-
15
- // ../../src/core/queue/failedJobRepository.ts
16
- class FailedJobRepository extends BaseRepository {
17
- constructor() {
18
- super(failedJobTable);
19
- }
20
- async create(values) {
21
- const stored = {
22
- ...values,
23
- ...values.payload === undefined ? {} : {
24
- payload: currentSqlDialect().driver === "pgsql" ? values.payload : JSON.stringify(values.payload)
25
- },
26
- ...values.failed_at === undefined ? {} : { failed_at: sqlTimestamp(values.failed_at) }
27
- };
28
- return hydrateFailure(await super.create(stored));
29
- }
30
- async findAll(options = {}) {
31
- const rows = await super.findAll(options);
32
- return options.select?.length ? rows : rows.map(hydrateFailure);
33
- }
34
- }
35
- function hydrateFailure(row) {
36
- const result = { ...row };
37
- if (typeof row.payload === "string")
38
- result.payload = JSON.parse(row.payload);
39
- if ("failed_at" in row && !(row.failed_at instanceof Date)) {
40
- result.failed_at = new Date(row.failed_at);
41
- }
42
- return result;
43
- }
44
- var failedJobRepository_default = FailedJobRepository;
2
+ // ../../src/core/queue/publicQueue.ts
3
+ import FailedJobRepository from "@getstrata/core/queue/failedJobRepository";
45
4
 
46
5
  // ../../src/core/queue/failedJobService.ts
47
6
  class FailedJobService {
@@ -53,6 +12,7 @@ class FailedJobService {
53
12
  return await this.repository.create({
54
13
  job_name: input.jobName,
55
14
  job_id: input.jobId ?? null,
15
+ ...input.handoff === undefined ? {} : { handoff: input.handoff },
56
16
  payload: input.payload,
57
17
  exception: input.exception,
58
18
  failed_at: new Date
@@ -91,83 +51,133 @@ class FailedJobService {
91
51
  }
92
52
  var failedJobService_default = FailedJobService;
93
53
 
94
- // ../../src/core/queue/jobRegistry.ts
95
- class JobRegistry {
96
- factories = new Map;
97
- instances = new WeakMap;
98
- register(name, factory) {
99
- this.factories.set(name, factory);
100
- }
101
- resolveName(job) {
102
- return this.instances.get(job);
103
- }
104
- track(name, job) {
105
- this.instances.set(job, name);
106
- return job;
107
- }
108
- create(name) {
109
- const factory = this.factories.get(name);
110
- if (!factory) {
111
- return;
54
+ // ../../src/core/queue/jobRunner.ts
55
+ import { setTimeout as delay } from "timers/promises";
56
+
57
+ // ../../src/core/config/configSnapshot.ts
58
+ function copyApplicationConfig(value, ancestors = new Set) {
59
+ if (value === null || typeof value === "string" || typeof value === "boolean")
60
+ return value;
61
+ if (typeof value === "number" && Number.isFinite(value))
62
+ return value;
63
+ if (typeof value !== "object" || ancestors.has(value)) {
64
+ throw new TypeError("Application config must contain acyclic plain data.");
65
+ }
66
+ const array = Array.isArray(value);
67
+ const prototype = Object.getPrototypeOf(value);
68
+ if (prototype !== (array ? Array.prototype : Object.prototype) && !(prototype === null && !array)) {
69
+ throw new TypeError("Application config must contain only plain objects and arrays.");
70
+ }
71
+ if (Object.getOwnPropertySymbols(value).length > 0) {
72
+ throw new TypeError("Application config cannot contain symbol keys.");
73
+ }
74
+ const descriptors = Object.getOwnPropertyDescriptors(value);
75
+ for (const [key, descriptor] of Object.entries(descriptors)) {
76
+ if (!(array && key === "length") && (!descriptor.enumerable || !("value" in descriptor))) {
77
+ throw new TypeError("Application config cannot contain accessors or hidden properties.");
112
78
  }
113
- return factory();
114
79
  }
115
- names() {
116
- return [...this.factories.keys()];
117
- }
118
- }
119
- var JOB_REGISTRY_KEY = Symbol.for("@getstrata/jobRegistry");
120
- function readSharedJobRegistry() {
121
- const globalRegistry = globalThis[JOB_REGISTRY_KEY];
122
- if (globalRegistry) {
123
- return globalRegistry;
80
+ ancestors.add(value);
81
+ try {
82
+ if (array) {
83
+ const length = descriptors.length.value;
84
+ if (Object.keys(descriptors).length !== length + 1) {
85
+ throw new TypeError("Application config arrays must be dense and have no extra properties.");
86
+ }
87
+ const result = [];
88
+ for (let index = 0;index < length; index++) {
89
+ const descriptor = descriptors[String(index)];
90
+ if (!descriptor)
91
+ throw new TypeError("Application config arrays must be dense.");
92
+ result.push(copyApplicationConfig(descriptor.value, ancestors));
93
+ }
94
+ return Object.freeze(result);
95
+ }
96
+ const result = {};
97
+ for (const [key, descriptor] of Object.entries(descriptors)) {
98
+ Object.defineProperty(result, key, {
99
+ value: copyApplicationConfig(descriptor.value, ancestors),
100
+ enumerable: true
101
+ });
102
+ }
103
+ return Object.freeze(result);
104
+ } finally {
105
+ ancestors.delete(value);
124
106
  }
125
- const registry = new JobRegistry;
126
- globalThis[JOB_REGISTRY_KEY] = registry;
127
- return registry;
128
107
  }
129
- var jobRegistry = readSharedJobRegistry();
130
-
131
- // ../../src/core/queue/jobRunner.ts
132
- import { setTimeout as delay } from "timers/promises";
133
108
 
134
109
  // ../../src/core/queue/queueConfig.ts
135
- function resolveQueueConfig() {
136
- const driver = process.env.QUEUE_DRIVER;
137
- const maxAttempts = Number(process.env.QUEUE_MAX_ATTEMPTS ?? "3");
138
- const backoffMs = Number(process.env.QUEUE_BACKOFF_MS ?? "1000");
139
- return {
140
- driver: driver === "redis" || driver === "async" || driver === "sync" ? driver : "sync",
141
- maxAttempts: Number.isFinite(maxAttempts) && maxAttempts > 0 ? maxAttempts : 3,
142
- backoffMs: Number.isFinite(backoffMs) && backoffMs >= 0 ? backoffMs : 1000
143
- };
144
- }
145
- var queueConfig = {
146
- get driver() {
147
- return resolveQueueConfig().driver;
148
- },
149
- get maxAttempts() {
150
- return resolveQueueConfig().maxAttempts;
151
- },
152
- get backoffMs() {
153
- return resolveQueueConfig().backoffMs;
154
- }
155
- };
156
- function resolveRedisQueueTransport() {
157
- const transport = process.env.QUEUE_REDIS_TRANSPORT ?? "lists";
110
+ var DEFAULT_QUEUE_VISIBILITY_MS = 60000;
111
+ function snapshotQueueRetryPolicy(policy) {
112
+ const { maxAttempts, backoffMs } = policy;
113
+ if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1)
114
+ throw new TypeError("QUEUE_MAX_ATTEMPTS must be a positive safe integer.");
115
+ if (!Number.isSafeInteger(backoffMs) || backoffMs < 0 || backoffMs > 2147483647)
116
+ throw new TypeError("QUEUE_BACKOFF_MS must be a non-negative integer within the timer range.");
117
+ return Object.freeze({ maxAttempts, backoffMs });
118
+ }
119
+ function resolveQueueConfig(env = process.env) {
120
+ const driver = env.QUEUE_DRIVER ?? "sync";
121
+ if (!["sync", "async", "redis"].includes(driver))
122
+ throw new TypeError("Unknown queue driver.");
123
+ const policy = snapshotQueueRetryPolicy({
124
+ maxAttempts: Number(env.QUEUE_MAX_ATTEMPTS ?? "3"),
125
+ backoffMs: Number(env.QUEUE_BACKOFF_MS ?? "1000")
126
+ });
127
+ return Object.freeze({ driver, ...policy });
128
+ }
129
+ function resolveRedisQueueTransport(env = process.env) {
130
+ const transport = env.QUEUE_REDIS_TRANSPORT ?? "lists";
158
131
  if (transport !== "lists" && transport !== "streams")
159
132
  throw new Error('QUEUE_REDIS_TRANSPORT must be "lists" or "streams".');
160
133
  return transport;
161
134
  }
162
- function readQueueVisibilityMs() {
163
- const value = Number(process.env.QUEUE_VISIBILITY_MS ?? "60000");
164
- if (!Number.isSafeInteger(value) || value < 30)
165
- throw new Error("QUEUE_VISIBILITY_MS must be an integer of at least 30 milliseconds.");
135
+ function readQueueVisibilityMs(env = process.env) {
136
+ const value = Number(env.QUEUE_VISIBILITY_MS ?? "60000");
137
+ if (!Number.isSafeInteger(value) || value < 30 || value > 2147483647)
138
+ throw new Error("QUEUE_VISIBILITY_MS must be an integer from 30 through 2147483647 milliseconds.");
166
139
  return value;
167
140
  }
141
+ function snapshotQueueRuntimeConfig(config) {
142
+ if (config.transport !== "lists" && config.transport !== "streams" || typeof config.visibilityMs !== "number")
143
+ throw new TypeError("Queue runtime requires explicit transport and numeric visibility.");
144
+ if (!Array.isArray(config.queueKeys) || config.queueKeys.length !== 3 || config.queueKeys.some((key) => typeof key !== "string" || !key.length) || new Set(config.queueKeys).size !== 3)
145
+ throw new TypeError("Queue runtime requires three distinct nonempty priority keys.");
146
+ return Object.freeze({
147
+ queueKeys: Object.freeze([...config.queueKeys]),
148
+ transport: resolveRedisQueueTransport({ QUEUE_REDIS_TRANSPORT: config.transport }),
149
+ visibilityMs: readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(config.visibilityMs) })
150
+ });
151
+ }
152
+ function resolveQueueRuntimeConfig(env = process.env) {
153
+ const prefix = env.APP_KEY_PREFIX?.trim() || "strata";
154
+ return snapshotQueueRuntimeConfig({
155
+ transport: resolveRedisQueueTransport(env),
156
+ visibilityMs: readQueueVisibilityMs(env),
157
+ queueKeys: [`${prefix}:queue:high`, `${prefix}:queue:default`, `${prefix}:queue:low`]
158
+ });
159
+ }
168
160
 
169
161
  // ../../src/core/queue/jobRunner.ts
170
- function parseQueueJobEnvelope(rawPayload) {
162
+ function snapshotQueueJobEnvelope(input) {
163
+ if (!input || typeof input !== "object" || Array.isArray(input) || ![Object.prototype, null].includes(Object.getPrototypeOf(input)))
164
+ throw new TypeError("Queue envelope must be plain data.");
165
+ const data = {};
166
+ for (const key of Reflect.ownKeys(input)) {
167
+ if (typeof key !== "string" || !["name", "payload", "attempts", "jobId", "deadlineAtMs", "cancellable", "handoff"].includes(key))
168
+ throw new TypeError("Unknown queue envelope field.");
169
+ const descriptor = Object.getOwnPropertyDescriptor(input, key);
170
+ if (!descriptor || !("value" in descriptor) || !descriptor.enumerable)
171
+ throw new TypeError("Queue envelope must contain own data fields.");
172
+ if (descriptor.value !== undefined)
173
+ Object.defineProperty(data, key, { value: descriptor.value, enumerable: true });
174
+ }
175
+ const captured = copyApplicationConfig(data);
176
+ if (typeof captured.name !== "string" || !captured.name || !captured.payload || typeof captured.payload !== "object" || Array.isArray(captured.payload))
177
+ throw new TypeError("Queue envelope requires a job name and payload record.");
178
+ return captured;
179
+ }
180
+ function parseQueueJobEnvelope(rawPayload, registry) {
171
181
  let parsed;
172
182
  try {
173
183
  parsed = JSON.parse(rawPayload);
@@ -180,15 +190,27 @@ function parseQueueJobEnvelope(rawPayload) {
180
190
  return null;
181
191
  }
182
192
  const envelope = parsed;
193
+ if (Object.keys(parsed).some((key) => ![
194
+ "name",
195
+ "payload",
196
+ "attempts",
197
+ "jobId",
198
+ "deadlineAtMs",
199
+ "cancellable",
200
+ "handoff"
201
+ ].includes(key)))
202
+ return null;
203
+ if (envelope.handoff !== undefined && (typeof envelope.handoff !== "string" || !envelope.handoff.length || envelope.handoff.length > 32768))
204
+ return null;
183
205
  if (typeof envelope.name !== "string" || envelope.name.length === 0) {
184
206
  console.error("[QueueWorker] Ignoring queue payload without job name");
185
207
  return null;
186
208
  }
187
- if (!jobRegistry.create(envelope.name)) {
209
+ if (!registry.has(envelope.name)) {
188
210
  console.error(`[QueueWorker] Ignoring unknown job name: ${envelope.name}`);
189
211
  return null;
190
212
  }
191
- if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null)) {
213
+ if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null || Array.isArray(envelope.payload))) {
192
214
  console.error("[QueueWorker] Ignoring queue payload with invalid payload object");
193
215
  return null;
194
216
  }
@@ -203,16 +225,18 @@ function parseQueueJobEnvelope(rawPayload) {
203
225
  return {
204
226
  ...envelope.cancellable ? { cancellable: true } : {},
205
227
  ...envelope.deadlineAtMs === undefined ? {} : { deadlineAtMs: envelope.deadlineAtMs },
228
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
206
229
  jobId: envelope.jobId,
207
230
  name: envelope.name,
208
231
  payload: envelope.payload ?? {},
209
232
  attempts: typeof envelope.attempts === "number" ? envelope.attempts : 0
210
233
  };
211
234
  }
212
- async function runQueueJob(envelope, failedJobs, options = {}) {
235
+ async function runQueueJob(envelope, failedJobs, registry, options = {}) {
236
+ envelope = snapshotQueueJobEnvelope(envelope);
237
+ const retryPolicy = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
213
238
  options.context?.signal.throwIfAborted();
214
- const job = jobRegistry.create(envelope.name);
215
- if (!job) {
239
+ if (!registry.has(envelope.name)) {
216
240
  throw new Error(`Unknown job "${envelope.name}".`);
217
241
  }
218
242
  const attempts = envelope.attempts ?? 0;
@@ -220,14 +244,23 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
220
244
  jobId: envelope.jobId ?? crypto.randomUUID(),
221
245
  signal: new AbortController().signal
222
246
  };
247
+ let job;
223
248
  try {
224
- await job.handle(envelope.payload, { ...context, attempt: attempts + 1 });
249
+ await failedJobs.runAttempt(envelope, { ...context, attempt: attempts + 1 }, (scoped) => {
250
+ scoped.signal.throwIfAborted();
251
+ job = registry.create(envelope.name);
252
+ if (!job)
253
+ throw new Error(`Unknown job "${envelope.name}".`);
254
+ return job.handle(envelope.payload, scoped);
255
+ });
225
256
  } catch (error) {
226
257
  options.context?.signal.throwIfAborted();
258
+ if (!job)
259
+ throw error;
227
260
  const nextAttempt = attempts + 1;
228
- const maxAttempts = job.maxAttempts ?? queueConfig.maxAttempts;
261
+ const maxAttempts = job.maxAttempts ?? retryPolicy.maxAttempts;
229
262
  if (nextAttempt < maxAttempts) {
230
- const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
263
+ const backoffMs = job.backoffMs ?? retryPolicy.backoffMs;
231
264
  if (options.deferRetry) {
232
265
  await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
233
266
  return;
@@ -236,11 +269,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
236
269
  await runQueueJob({
237
270
  ...envelope,
238
271
  attempts: nextAttempt
239
- }, failedJobs, { ...options, context });
272
+ }, failedJobs, registry, { ...options, context, retryPolicy });
240
273
  return;
241
274
  }
242
275
  await failedJobs.recordFailure({
243
276
  jobName: envelope.name,
277
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
244
278
  jobId: context.jobId,
245
279
  payload: envelope.payload,
246
280
  exception: error instanceof Error ? error.stack ?? error.message : String(error)
@@ -250,7 +284,85 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
250
284
  }
251
285
  }
252
286
 
287
+ // ../../src/core/queue/queueServices.ts
288
+ import { failedJobRepository } from "@getstrata/core/queue/failedJobRepository";
289
+ import {
290
+ QueueHandoffError
291
+ } from "@getstrata/core/queue/queueHandoff";
292
+ function createFailedJobService(database) {
293
+ return new FailedJobService(database.repository(failedJobRepository));
294
+ }
295
+ function createQueueServices(config) {
296
+ const { invoke, attemptEnvelope, recoveryEnvelope, validateReplay } = config;
297
+ const jobContext = Object.freeze({ ...config.jobContext });
298
+ const operate = (work, failure) => invoke(recoveryEnvelope(failure), ({ database }) => work(createFailedJobService(database)));
299
+ return Object.freeze({
300
+ recordFailure: (input) => operate((service) => service.recordFailure(input), input),
301
+ listRecent: (limit) => operate((service) => service.listRecent(limit)),
302
+ retry: (id, enqueue) => operate((service) => service.retry(id, async (record) => {
303
+ validateReplay?.(record);
304
+ await enqueue(record);
305
+ })),
306
+ delete: (id) => operate((service) => service.delete(id)),
307
+ flush: () => operate((service) => service.flush()),
308
+ runAttempt: (job, context, work) => {
309
+ const envelope = attemptEnvelope(job, context);
310
+ return invoke(envelope, ({ database, policy }) => work({
311
+ ...context,
312
+ ...jobContext,
313
+ database,
314
+ policy,
315
+ actor: envelope.actor,
316
+ tenant: envelope.tenant
317
+ }));
318
+ }
319
+ });
320
+ }
321
+ function createSystemQueueServices(invoke, jobContext = {}, handoff, systemJobs = []) {
322
+ if (!Array.isArray(systemJobs) || systemJobs.some((name) => typeof name !== "string" || !name.length))
323
+ throw new TypeError("System job admission requires explicit job names.");
324
+ const allowedSystemJobs = new Set(systemJobs);
325
+ const establishIdentity = (job) => {
326
+ if (job.handoff === undefined) {
327
+ if (handoff && !allowedSystemJobs.has(job.name))
328
+ throw new QueueHandoffError;
329
+ return { actor: { kind: "system", policy: "queue.job" } };
330
+ }
331
+ if (!handoff)
332
+ throw new QueueHandoffError;
333
+ return handoff.verify(job);
334
+ };
335
+ return createQueueServices({
336
+ invoke,
337
+ jobContext,
338
+ validateReplay: (record) => {
339
+ establishIdentity({
340
+ name: record.job_name,
341
+ payload: record.payload,
342
+ ...record.handoff == null ? {} : { handoff: record.handoff }
343
+ });
344
+ },
345
+ attemptEnvelope: (job, context) => {
346
+ const identity = establishIdentity(job);
347
+ return {
348
+ kind: "job",
349
+ externalId: context.jobId,
350
+ signal: context.signal,
351
+ ...context.deadlineAtMs === undefined ? {} : { deadlineAtMs: context.deadlineAtMs },
352
+ ...identity,
353
+ metadata: { jobName: job.name, attempt: context.attempt ?? 1 }
354
+ };
355
+ },
356
+ recoveryEnvelope: (failure) => ({
357
+ kind: "command",
358
+ actor: { kind: "system", policy: "queue.recovery" },
359
+ metadata: { phase: "queue-recovery", ...failure ? { jobName: failure.jobName } : {} }
360
+ })
361
+ });
362
+ }
363
+
253
364
  // ../../src/core/queue/redisQueue.ts
365
+ import { QueueHandoffError as QueueHandoffError3 } from "@getstrata/core/queue/queueHandoff";
254
366
  var {RedisClient: RedisClient2 } = globalThis.Bun;
255
367
 
256
368
  // ../../src/core/queue/redisListScripts.ts
@@ -411,14 +523,14 @@ function envFlagEnabled(value) {
411
523
  }
412
524
 
413
525
  // ../../src/core/runtime/appKeyPrefix.ts
414
- function appKeyPrefix() {
415
- return process.env.APP_KEY_PREFIX?.trim() || "strata";
526
+ function appKeyPrefix(env = process.env) {
527
+ return env.APP_KEY_PREFIX?.trim() || "strata";
416
528
  }
417
- function appCookieName(kind) {
418
- return `${appKeyPrefix()}_${kind}`;
529
+ function appCookieName(kind, env = process.env) {
530
+ return `${appKeyPrefix(env)}_${kind}`;
419
531
  }
420
- function appDevSecret(kind) {
421
- return `${appKeyPrefix()}-dev-${kind}`;
532
+ function appDevSecret(kind, env = process.env) {
533
+ return `${appKeyPrefix(env)}-dev-${kind}`;
422
534
  }
423
535
  function requireConfiguredSecret(names, devKind, env = process.env) {
424
536
  for (const name of names) {
@@ -430,50 +542,18 @@ function requireConfiguredSecret(names, devKind, env = process.env) {
430
542
  if (isProductionEnv(env)) {
431
543
  throw new Error(`${names[0]} must be set outside development. Do not derive secrets from the app name.`);
432
544
  }
433
- return appDevSecret(devKind);
545
+ return appDevSecret(devKind, env);
434
546
  }
435
547
  function namespacedRedisKey(kind) {
436
548
  return `${appKeyPrefix()}:${kind}`;
437
549
  }
438
- function smtpEhloHost() {
439
- const raw = process.env.MAIL_EHLO?.trim() || `${appKeyPrefix()}.local`;
550
+ function smtpEhloHost(env = process.env) {
551
+ const raw = env.MAIL_EHLO?.trim() || `${appKeyPrefix(env)}.local`;
440
552
  const safe = raw.replace(/[^a-zA-Z0-9.-]/g, "");
441
553
  return safe || "strata.local";
442
554
  }
443
- function siemEventType() {
444
- return process.env.SIEM_EVENT_TYPE?.trim() || `${appKeyPrefix()}.audit`;
445
- }
446
- function appUserAgent() {
447
- return process.env.APP_USER_AGENT?.trim() || appKeyPrefix();
448
- }
449
- function otelServiceName() {
450
- return process.env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix()}-api`;
451
- }
452
- function webhookSignatureHeader() {
453
- return process.env.WEBHOOK_SIGNATURE_HEADER?.trim() || `x-${appKeyPrefix()}-signature`;
454
- }
455
- function appDisplayName() {
456
- return process.env.APP_NAME?.trim() || "Strata";
457
- }
458
- function appEnv() {
459
- return process.env.APP_ENV?.trim() || "local";
460
- }
461
- function appUrl() {
462
- return (process.env.APP_URL?.trim() || "http://localhost:3000").replace(/\/$/, "");
463
- }
464
- function apiPrefix() {
465
- const raw = process.env.API_PREFIX?.trim() || "/api/v1";
466
- const withSlash = raw.startsWith("/") ? raw : `/${raw}`;
467
- const trimmed = withSlash.replace(/\/+$/, "");
468
- return trimmed || "/api/v1";
469
- }
470
- function sdkClientClassName() {
471
- const override = process.env.APP_SDK_CLASS?.trim();
472
- if (override && /^[A-Za-z_][A-Za-z0-9_]*$/.test(override)) {
473
- return override;
474
- }
475
- const fromName = appDisplayName().replace(/[^A-Za-z0-9]/g, "");
476
- return fromName ? `${fromName}Client` : "AppClient";
555
+ function otelServiceName(env = process.env) {
556
+ return env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix(env)}-api`;
477
557
  }
478
558
 
479
559
  // ../../src/core/queue/redisQueueKeys.ts
@@ -486,25 +566,19 @@ function queueHighKey() {
486
566
  function queueLowKey() {
487
567
  return namespacedRedisKey("queue:low");
488
568
  }
489
- var QUEUE_LIST_KEY = queueListKey();
490
- var QUEUE_HIGH_KEY = queueHighKey();
491
- var QUEUE_LOW_KEY = queueLowKey();
569
+ var QUEUE_LIST_KEY = "strata:queue:default";
570
+ var QUEUE_HIGH_KEY = "strata:queue:high";
571
+ var QUEUE_LOW_KEY = "strata:queue:low";
492
572
  function defaultQueueKeys() {
493
573
  return [queueHighKey(), queueListKey(), queueLowKey()];
494
574
  }
495
- var QUEUE_KEYS = defaultQueueKeys();
496
- function queueKeyForPriority(priority = "default") {
497
- switch (priority) {
498
- case "high":
499
- return queueHighKey();
500
- case "low":
501
- return queueLowKey();
502
- default:
503
- return queueListKey();
504
- }
575
+ var QUEUE_KEYS = Object.freeze([QUEUE_HIGH_KEY, QUEUE_LIST_KEY, QUEUE_LOW_KEY]);
576
+ function queueKeyForPriority(priority = "default", keys = defaultQueueKeys()) {
577
+ return priority === "high" ? keys[0] : priority === "low" ? keys[2] : keys[1];
505
578
  }
506
579
 
507
580
  // ../../src/core/queue/redisStreams.ts
581
+ import { QueueHandoffError as QueueHandoffError2 } from "@getstrata/core/queue/queueHandoff";
508
582
  var {RedisClient } = globalThis.Bun;
509
583
 
510
584
  // ../../src/core/queue/redisStreamsScripts.ts
@@ -717,7 +791,7 @@ function watchStreamDeadline(client, deadlineAtMs, remainingMs, expire, fail) {
717
791
  clearTimeout(timer);
718
792
  };
719
793
  }
720
- async function scheduleStreamRetry(client, key, reservation, envelope, delayMs) {
794
+ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
721
795
  if (!Number.isSafeInteger(delayMs) || delayMs < 0 || delayMs > Number.MAX_SAFE_INTEGER / 2)
722
796
  throw new Error("Invalid Streams retry delay");
723
797
  const scheduled = await client.send("EVAL", [
@@ -729,7 +803,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
729
803
  STREAM_GROUP,
730
804
  reservation.id,
731
805
  reservation.owner,
732
- String(readQueueVisibilityMs()),
806
+ String(visibilityMs),
733
807
  String(delayMs),
734
808
  JSON.stringify(envelope),
735
809
  envelope.deadlineAtMs === undefined ? "" : String(envelope.deadlineAtMs),
@@ -738,7 +812,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
738
812
  if (Number(scheduled) !== 1)
739
813
  throw new Error("Queue lease lost while scheduling retry");
740
814
  }
741
- async function pollStreamPriorities(client, queueKeys, owner, cursors) {
815
+ async function pollStreamPriorities(client, queueKeys, owner, cursors, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
742
816
  const keys = [];
743
817
  for (const key of queueKeys)
744
818
  keys.push(streamQueueKey(key), streamRetryKey(key));
@@ -748,7 +822,7 @@ async function pollStreamPriorities(client, queueKeys, owner, cursors) {
748
822
  ...keys,
749
823
  STREAM_GROUP,
750
824
  owner,
751
- String(readQueueVisibilityMs()),
825
+ String(visibilityMs),
752
826
  ...cursors
753
827
  ]);
754
828
  if (!Array.isArray(raw))
@@ -799,7 +873,7 @@ async function ensureStreamGroup(client, key) {
799
873
  throw error;
800
874
  }
801
875
  }
802
- async function updateStreamReservation(client, key, reservation, action, controlKey) {
876
+ async function updateStreamReservation(client, key, reservation, action, controlKey, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
803
877
  return Number(await client.send("EVAL", [
804
878
  UPDATE_STREAM,
805
879
  "3",
@@ -809,41 +883,46 @@ async function updateStreamReservation(client, key, reservation, action, control
809
883
  STREAM_GROUP,
810
884
  reservation.id,
811
885
  reservation.owner,
812
- String(readQueueVisibilityMs()),
886
+ String(visibilityMs),
813
887
  action,
814
888
  reservation.payload,
815
889
  controlKey ? "1" : "0"
816
890
  ])) === 1;
817
891
  }
818
892
  class RedisStreamsQueue {
893
+ registry;
894
+ queueKeys;
819
895
  client;
820
- constructor(redisUrl) {
896
+ constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
897
+ this.registry = registry;
898
+ this.queueKeys = queueKeys;
899
+ this.queueKeys = Object.freeze([...queueKeys]);
821
900
  this.client = new RedisClient(redisUrl);
822
901
  }
823
902
  close() {
824
903
  this.client.close();
825
904
  }
826
- async dispatch(job, payload) {
827
- await this.enqueue(job, payload);
905
+ async dispatch(job, payload, options) {
906
+ await this.admit(job, payload, {}, undefined, options?.handoff);
828
907
  }
829
908
  async enqueue(job, payload, options = {}) {
830
909
  return this.admit(job, payload, options);
831
910
  }
832
- async replay(job, payload, jobId) {
833
- const prefix = defaultQueueKeys().find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
911
+ async replay(job, payload, jobId, options) {
912
+ const prefix = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
834
913
  if (!prefix || !/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9]+-[0-9]+)$/.test(jobId.slice(streamQueueKey(prefix).length + 1)))
835
914
  throw new Error("Replay identity does not belong to this Streams namespace");
836
- await this.admit(job, payload, {}, jobId);
915
+ await this.admit(job, payload, {}, jobId, options?.handoff);
837
916
  }
838
- async admit(job, payload, options, replayId) {
917
+ async admit(job, payload, options, replayId, handoff) {
839
918
  if (options.cancellable !== undefined && typeof options.cancellable !== "boolean")
840
919
  throw new Error("cancellable must be a boolean");
841
920
  if (options.timeoutMs !== undefined && (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs <= 0 || options.timeoutMs > MAX_TIMER_MS))
842
921
  throw new Error("Streams timeoutMs must be an integer between 1 and 2147483647");
843
- const name = jobRegistry.resolveName(job);
922
+ const name = this.registry.resolveName(job);
844
923
  if (!name)
845
924
  throw new Error("Job is not registered with the queue worker registry.");
846
- const key = queueKeyForPriority(job.priority);
925
+ const key = queueKeyForPriority(job.priority, this.queueKeys);
847
926
  await assertLegacyDrained(this.client, key);
848
927
  const jobId = replayId ?? `${streamQueueKey(key)}/${crypto.randomUUID()}`;
849
928
  const deadlineAtMs = options.timeoutMs === undefined ? undefined : await readStreamTime(this.client) + options.timeoutMs;
@@ -851,6 +930,7 @@ class RedisStreamsQueue {
851
930
  name,
852
931
  payload,
853
932
  attempts: 0,
933
+ ...handoff === undefined ? {} : { handoff },
854
934
  jobId,
855
935
  deadlineAtMs,
856
936
  ...options.cancellable ? { cancellable: true } : {}
@@ -868,7 +948,7 @@ class RedisStreamsQueue {
868
948
  return jobId;
869
949
  }
870
950
  async cancel(jobId) {
871
- const key = defaultQueueKeys().find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
951
+ const key = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
872
952
  if (!key)
873
953
  throw new Error("Invalid cancellable Streams job ID");
874
954
  return cancelStreamJob(this.client, key, jobId);
@@ -878,8 +958,11 @@ class RedisStreamsQueue {
878
958
  class RedisStreamsWorker {
879
959
  redisUrl;
880
960
  failedJobs;
961
+ registry;
881
962
  timeoutSeconds;
882
963
  queueKeys;
964
+ visibilityMs;
965
+ retryPolicy;
883
966
  client;
884
967
  owner = crypto.randomUUID();
885
968
  cursors = new Map;
@@ -888,12 +971,16 @@ class RedisStreamsWorker {
888
971
  running = false;
889
972
  stopping = false;
890
973
  active;
891
- constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = defaultQueueKeys()) {
974
+ constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
892
975
  this.redisUrl = redisUrl;
893
976
  this.failedJobs = failedJobs;
977
+ this.registry = registry;
894
978
  this.timeoutSeconds = timeoutSeconds;
895
979
  this.queueKeys = queueKeys;
896
- readQueueVisibilityMs();
980
+ this.visibilityMs = visibilityMs;
981
+ this.queueKeys = Object.freeze([...queueKeys]);
982
+ this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
983
+ readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
897
984
  this.client = new RedisClient(redisUrl);
898
985
  }
899
986
  requestStop() {
@@ -932,7 +1019,7 @@ class RedisStreamsWorker {
932
1019
  do {
933
1020
  if (this.stopping)
934
1021
  return false;
935
- const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"));
1022
+ const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"), this.visibilityMs);
936
1023
  this.queueKeys.forEach((key, index) => {
937
1024
  const cursor = polled.cursors[index];
938
1025
  if (cursor)
@@ -952,9 +1039,15 @@ class RedisStreamsWorker {
952
1039
  return false;
953
1040
  }
954
1041
  async handle(key, reservation) {
955
- const envelope = parseQueueJobEnvelope(reservation.payload);
1042
+ const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
956
1043
  if (!envelope) {
957
- await updateStreamReservation(this.client, key, reservation, "quarantine");
1044
+ await this.failedJobs.recordFailure({
1045
+ jobName: "queue.invalid-envelope",
1046
+ jobId: `${streamQueueKey(key)}/${reservation.id}`,
1047
+ payload: { raw: reservation.payload },
1048
+ exception: "Malformed or unknown queue envelope"
1049
+ });
1050
+ await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
958
1051
  return;
959
1052
  }
960
1053
  envelope.jobId ??= `${streamQueueKey(key)}/${reservation.id}`;
@@ -963,7 +1056,7 @@ class RedisStreamsWorker {
963
1056
  try {
964
1057
  controlKey = streamControlKey(key, envelope.jobId);
965
1058
  } catch {
966
- await updateStreamReservation(this.client, key, reservation, "quarantine");
1059
+ await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
967
1060
  return;
968
1061
  }
969
1062
  }
@@ -972,10 +1065,11 @@ class RedisStreamsWorker {
972
1065
  await this.failedJobs.recordFailure({
973
1066
  jobName: envelope.name,
974
1067
  jobId: envelope.jobId,
1068
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
975
1069
  payload: envelope.payload,
976
1070
  exception: cancellationError.message
977
1071
  });
978
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
1072
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
979
1073
  return;
980
1074
  }
981
1075
  const remainingMs = envelope.deadlineAtMs === undefined ? undefined : envelope.deadlineAtMs - await readStreamTime(this.client);
@@ -984,15 +1078,17 @@ class RedisStreamsWorker {
984
1078
  await this.failedJobs.recordFailure({
985
1079
  jobName: envelope.name,
986
1080
  jobId: envelope.jobId,
1081
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
987
1082
  payload: envelope.payload,
988
1083
  exception: deadlineError.message
989
1084
  });
990
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
1085
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
991
1086
  return;
992
1087
  }
993
1088
  const controller = new AbortController;
994
1089
  this.active = controller;
995
1090
  let acknowledge = false;
1091
+ let invalidHandoff = false;
996
1092
  let released = false;
997
1093
  let ownershipLost = false;
998
1094
  let renewal;
@@ -1012,7 +1108,7 @@ class RedisStreamsWorker {
1012
1108
  return;
1013
1109
  renewal = (async () => {
1014
1110
  try {
1015
- if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey))
1111
+ if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey, this.visibilityMs))
1016
1112
  loseOwnership(new Error("Queue lease lost"));
1017
1113
  else if (controlKey && await readStreamCancellation(this.client, controlKey)) {
1018
1114
  cancellationRequested = true;
@@ -1025,9 +1121,10 @@ class RedisStreamsWorker {
1025
1121
  })().finally(() => {
1026
1122
  renewal = undefined;
1027
1123
  });
1028
- }, Math.max(1, Math.floor(readQueueVisibilityMs() / 3)));
1124
+ }, Math.max(1, Math.floor(this.visibilityMs / 3)));
1029
1125
  try {
1030
- await runQueueJob(envelope, this.failedJobs, {
1126
+ await runQueueJob(envelope, this.failedJobs, this.registry, {
1127
+ retryPolicy: this.retryPolicy,
1031
1128
  context: {
1032
1129
  jobId: envelope.jobId,
1033
1130
  signal: controller.signal,
@@ -1035,7 +1132,7 @@ class RedisStreamsWorker {
1035
1132
  },
1036
1133
  deferRetry: async (next, delayMs) => {
1037
1134
  controller.signal.throwIfAborted();
1038
- await scheduleStreamRetry(this.client, key, reservation, next, delayMs);
1135
+ await scheduleStreamRetry(this.client, key, reservation, next, delayMs, this.visibilityMs);
1039
1136
  released = true;
1040
1137
  },
1041
1138
  onFailureRecorded: () => {
@@ -1045,18 +1142,26 @@ class RedisStreamsWorker {
1045
1142
  });
1046
1143
  acknowledge = true;
1047
1144
  } catch (error) {
1048
- console.error("[StreamsWorker] Job failed:", error);
1145
+ if (error instanceof QueueHandoffError2)
1146
+ invalidHandoff = true;
1147
+ else
1148
+ console.error("[StreamsWorker] Job failed:", error);
1049
1149
  } finally {
1050
1150
  stopDeadlineWatch?.();
1051
1151
  clearInterval(heartbeat);
1052
1152
  await renewal;
1053
1153
  this.active = undefined;
1054
1154
  }
1155
+ if (invalidHandoff && !ownershipLost && !released) {
1156
+ await updateStreamReservation(this.client, key, reservation, "quarantine", controlKey, this.visibilityMs);
1157
+ return;
1158
+ }
1055
1159
  if (!ownershipLost && !released && controlKey && (cancellationRequested || await readStreamCancellation(this.client, controlKey))) {
1056
1160
  if (!failureRecorded)
1057
1161
  await this.failedJobs.recordFailure({
1058
1162
  jobName: envelope.name,
1059
1163
  jobId: envelope.jobId,
1164
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1060
1165
  payload: envelope.payload,
1061
1166
  exception: cancellationError.message
1062
1167
  });
@@ -1068,13 +1173,14 @@ class RedisStreamsWorker {
1068
1173
  await this.failedJobs.recordFailure({
1069
1174
  jobName: envelope.name,
1070
1175
  jobId: envelope.jobId,
1176
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1071
1177
  payload: envelope.payload,
1072
1178
  exception: deadlineError.message
1073
1179
  });
1074
1180
  acknowledge = true;
1075
1181
  }
1076
1182
  if (acknowledge && !ownershipLost && !released)
1077
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
1183
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
1078
1184
  }
1079
1185
  async run() {
1080
1186
  this.running = true;
@@ -1106,7 +1212,7 @@ async function migrateLegacyQueueToStreams(client, options) {
1106
1212
  if (!Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 1000)
1107
1213
  throw new Error("Queue conversion batchSize must be between 1 and 1000.");
1108
1214
  let converted = 0;
1109
- for (const key of options.queueKeys ?? defaultQueueKeys())
1215
+ for (const key of [...options.queueKeys ?? QUEUE_KEYS])
1110
1216
  converted += Number(await client.send("EVAL", [
1111
1217
  CONVERT_LIST,
1112
1218
  "4",
@@ -1139,7 +1245,7 @@ function reservationExpired(lease, now, visibilityMs) {
1139
1245
  function queueInvalidKey(queueKey) {
1140
1246
  return `${queueKey}:invalid`;
1141
1247
  }
1142
- async function reserveQueueJob(client, queueKey, owner) {
1248
+ async function reserveQueueJob(client, queueKey, owner, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1143
1249
  const raw = await client.send("EVAL", [
1144
1250
  RESERVE_JOB,
1145
1251
  "3",
@@ -1148,14 +1254,14 @@ async function reserveQueueJob(client, queueKey, owner) {
1148
1254
  queueProcessingLeaseKey(queueKey),
1149
1255
  crypto.randomUUID(),
1150
1256
  owner,
1151
- String(readQueueVisibilityMs())
1257
+ String(visibilityMs)
1152
1258
  ]);
1153
1259
  if (typeof raw !== "string")
1154
1260
  return null;
1155
1261
  const reservation = JSON.parse(raw);
1156
1262
  return { ...reservation, record: raw };
1157
1263
  }
1158
- async function updateQueueReservation(client, queueKey, reservation, action) {
1264
+ async function updateQueueReservation(client, queueKey, reservation, action, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1159
1265
  return Number(await client.send("EVAL", [
1160
1266
  RENEW_OR_ACK,
1161
1267
  "3",
@@ -1166,11 +1272,11 @@ async function updateQueueReservation(client, queueKey, reservation, action) {
1166
1272
  reservation.owner,
1167
1273
  reservation.record,
1168
1274
  action,
1169
- String(readQueueVisibilityMs()),
1275
+ String(visibilityMs),
1170
1276
  reservation.payload
1171
1277
  ])) === 1;
1172
1278
  }
1173
- async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim) {
1279
+ async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1174
1280
  const keys = [];
1175
1281
  for (const key of queueKeys) {
1176
1282
  keys.push(key, queueProcessingKey(key), queueProcessingLeaseKey(key));
@@ -1180,7 +1286,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
1180
1286
  String(keys.length),
1181
1287
  ...keys,
1182
1288
  owner,
1183
- String(readQueueVisibilityMs()),
1289
+ String(visibilityMs),
1184
1290
  reservationId,
1185
1291
  reclaim ? "1" : "0"
1186
1292
  ]);
@@ -1205,7 +1311,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
1205
1311
  const reservation = JSON.parse(record);
1206
1312
  return { queueKey, reservation: { ...reservation, record } };
1207
1313
  }
1208
- async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
1314
+ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1209
1315
  let reclaimed = 0;
1210
1316
  for (const key of queueKeys) {
1211
1317
  reclaimed += Number(await client.send("EVAL", [
@@ -1214,31 +1320,46 @@ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
1214
1320
  key,
1215
1321
  queueProcessingKey(key),
1216
1322
  queueProcessingLeaseKey(key),
1217
- String(readQueueVisibilityMs())
1323
+ String(visibilityMs)
1218
1324
  ]));
1219
1325
  }
1220
1326
  return reclaimed;
1221
1327
  }
1222
1328
 
1223
1329
  class RedisQueue {
1330
+ registry;
1331
+ queueKeys;
1224
1332
  client;
1225
- constructor(redisUrl) {
1333
+ constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
1334
+ this.registry = registry;
1335
+ this.queueKeys = queueKeys;
1336
+ this.queueKeys = Object.freeze([...queueKeys]);
1226
1337
  this.client = new RedisClient2(redisUrl);
1227
1338
  }
1228
1339
  close() {
1229
1340
  this.client.close();
1230
1341
  }
1231
- async dispatch(job, payload) {
1232
- const name = jobRegistry.resolveName(job);
1342
+ async dispatch(job, payload, options) {
1343
+ await this.admit(job, payload, options);
1344
+ }
1345
+ async replay(job, payload, jobId, options) {
1346
+ if (typeof jobId !== "string" || !jobId.length)
1347
+ throw new TypeError("Replay identity is required.");
1348
+ await this.admit(job, payload, options, jobId);
1349
+ }
1350
+ async admit(job, payload, options, jobId) {
1351
+ const name = this.registry.resolveName(job);
1233
1352
  if (!name) {
1234
1353
  throw new Error("Job is not registered with the queue worker registry.");
1235
1354
  }
1236
1355
  const envelope = {
1237
1356
  name,
1357
+ jobId: jobId ?? crypto.randomUUID(),
1238
1358
  payload,
1239
- attempts: 0
1359
+ attempts: 0,
1360
+ ...options?.handoff === undefined ? {} : { handoff: options.handoff }
1240
1361
  };
1241
- const queueKey = queueKeyForPriority(job.priority);
1362
+ const queueKey = queueKeyForPriority(job.priority, this.queueKeys);
1242
1363
  await this.client.lpush(queueKey, JSON.stringify(envelope));
1243
1364
  }
1244
1365
  }
@@ -1246,17 +1367,25 @@ class RedisQueue {
1246
1367
  class QueueWorker {
1247
1368
  redisUrl;
1248
1369
  failedJobs;
1370
+ registry;
1249
1371
  timeoutSeconds;
1250
1372
  queueKeys;
1373
+ visibilityMs;
1374
+ retryPolicy;
1251
1375
  owner = crypto.randomUUID();
1252
1376
  running = false;
1253
1377
  stopping = false;
1254
1378
  client;
1255
- constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = QUEUE_KEYS) {
1379
+ constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
1256
1380
  this.redisUrl = redisUrl;
1257
1381
  this.failedJobs = failedJobs;
1382
+ this.registry = registry;
1258
1383
  this.timeoutSeconds = timeoutSeconds;
1259
1384
  this.queueKeys = queueKeys;
1385
+ this.visibilityMs = visibilityMs;
1386
+ this.queueKeys = Object.freeze([...queueKeys]);
1387
+ this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
1388
+ readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
1260
1389
  this.client = new RedisClient2(redisUrl);
1261
1390
  }
1262
1391
  requestStop() {
@@ -1271,7 +1400,7 @@ class QueueWorker {
1271
1400
  do {
1272
1401
  if (this.stopping)
1273
1402
  return null;
1274
- const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim);
1403
+ const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim, this.visibilityMs);
1275
1404
  reclaim = false;
1276
1405
  if (reserved)
1277
1406
  return reserved;
@@ -1288,12 +1417,13 @@ class QueueWorker {
1288
1417
  if (!reserved)
1289
1418
  return false;
1290
1419
  const { queueKey, reservation } = reserved;
1291
- const envelope = parseQueueJobEnvelope(reservation.payload);
1420
+ const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
1292
1421
  if (!envelope) {
1293
- await updateQueueReservation(this.client, queueKey, reservation, "quarantine");
1422
+ await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
1294
1423
  return true;
1295
1424
  }
1296
1425
  let acknowledge = false;
1426
+ let invalidHandoff = false;
1297
1427
  let ownershipLost = false;
1298
1428
  let renewal;
1299
1429
  const heartbeat = setInterval(() => {
@@ -1301,7 +1431,7 @@ class QueueWorker {
1301
1431
  return;
1302
1432
  renewal = (async () => {
1303
1433
  try {
1304
- if (!await updateQueueReservation(this.client, queueKey, reservation, "renew")) {
1434
+ if (!await updateQueueReservation(this.client, queueKey, reservation, "renew", this.visibilityMs)) {
1305
1435
  ownershipLost = true;
1306
1436
  }
1307
1437
  } catch (error) {
@@ -1311,22 +1441,30 @@ class QueueWorker {
1311
1441
  })().finally(() => {
1312
1442
  renewal = undefined;
1313
1443
  });
1314
- }, Math.max(1, Math.floor(readQueueVisibilityMs() / 3)));
1444
+ }, Math.max(1, Math.floor(this.visibilityMs / 3)));
1315
1445
  try {
1316
- await runQueueJob(envelope, this.failedJobs, {
1446
+ await runQueueJob(envelope, this.failedJobs, this.registry, {
1447
+ retryPolicy: this.retryPolicy,
1317
1448
  onFailureRecorded: () => {
1318
1449
  acknowledge = true;
1319
1450
  }
1320
1451
  });
1321
1452
  acknowledge = true;
1322
1453
  } catch (error) {
1323
- console.error("[QueueWorker] Job failed:", error);
1454
+ if (error instanceof QueueHandoffError3)
1455
+ invalidHandoff = true;
1456
+ else
1457
+ console.error("[QueueWorker] Job failed:", error);
1324
1458
  } finally {
1325
1459
  clearInterval(heartbeat);
1326
1460
  await renewal;
1327
1461
  }
1462
+ if (invalidHandoff && !ownershipLost) {
1463
+ await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
1464
+ return true;
1465
+ }
1328
1466
  if (acknowledge && !ownershipLost) {
1329
- await updateQueueReservation(this.client, queueKey, reservation, "ack");
1467
+ await updateQueueReservation(this.client, queueKey, reservation, "ack", this.visibilityMs);
1330
1468
  }
1331
1469
  return true;
1332
1470
  }
@@ -1355,12 +1493,13 @@ class QueueWorker {
1355
1493
  this.client.close();
1356
1494
  }
1357
1495
  }
1358
- async function countPendingQueueJobs(redisUrl) {
1496
+ async function countPendingQueueJobs(redisUrl, config = resolveQueueRuntimeConfig({})) {
1497
+ const { transport, queueKeys } = snapshotQueueRuntimeConfig(config);
1359
1498
  const client = new RedisClient2(redisUrl);
1360
1499
  try {
1361
1500
  let total = 0;
1362
- for (const queueKey of resolveRedisQueueTransport() === "streams" ? defaultQueueKeys() : QUEUE_KEYS) {
1363
- total += resolveRedisQueueTransport() === "streams" ? await readStreamQueueDepth(client, queueKey) : await client.llen(queueKey);
1501
+ for (const queueKey of queueKeys) {
1502
+ total += transport === "streams" ? await readStreamQueueDepth(client, queueKey) : await client.llen(queueKey);
1364
1503
  }
1365
1504
  return total;
1366
1505
  } finally {
@@ -1374,16 +1513,12 @@ class Job {
1374
1513
  maxAttempts;
1375
1514
  backoffMs;
1376
1515
  priority;
1377
- constructor() {
1378
- const jobName = this.constructor.jobName;
1379
- if (jobName) {
1380
- jobRegistry.track(jobName, this);
1381
- }
1382
- }
1383
1516
  }
1384
1517
 
1385
1518
  class SyncQueue {
1386
- async dispatch(job, payload) {
1519
+ async dispatch(job, payload, options) {
1520
+ if (options?.handoff !== undefined)
1521
+ throw new Error("Identity handoff requires an application queue.");
1387
1522
  await job.handle(payload);
1388
1523
  }
1389
1524
  }
@@ -1408,7 +1543,9 @@ class AsyncQueue {
1408
1543
  this.active.delete(running);
1409
1544
  });
1410
1545
  }
1411
- async dispatch(job, payload) {
1546
+ async dispatch(job, payload, options) {
1547
+ if (options?.handoff !== undefined)
1548
+ throw new Error("Identity handoff requires an application queue.");
1412
1549
  this.enqueue(() => job.handle(payload), (error) => {
1413
1550
  console.error("[AsyncQueue] Job failed:", error);
1414
1551
  });
@@ -1429,64 +1566,81 @@ function createQueue(driver) {
1429
1566
  // ../../src/core/queue/resilientQueue.ts
1430
1567
  class ResilientQueue extends AsyncQueue {
1431
1568
  failedJobs;
1569
+ registry;
1432
1570
  asyncDispatch;
1433
- constructor(failedJobs, asyncDispatch = false) {
1571
+ retryPolicy;
1572
+ constructor(failedJobs, registry, asyncDispatch = false, retryPolicy = resolveQueueConfig()) {
1434
1573
  super();
1435
1574
  this.failedJobs = failedJobs;
1575
+ this.registry = registry;
1436
1576
  this.asyncDispatch = asyncDispatch;
1577
+ this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
1578
+ }
1579
+ async dispatch(job, payload, options) {
1580
+ await this.admit(job, payload, options);
1437
1581
  }
1438
- async dispatch(job, payload) {
1582
+ async replay(job, payload, jobId, options) {
1583
+ if (typeof jobId !== "string" || !jobId.length)
1584
+ throw new TypeError("Replay identity is required.");
1585
+ await this.admit(job, payload, options, jobId);
1586
+ }
1587
+ async admit(job, payload, options, jobId) {
1439
1588
  this.assertOpen();
1440
- const name = jobRegistry.resolveName(job);
1589
+ const name = this.registry.resolveName(job);
1441
1590
  if (!name) {
1442
1591
  throw new Error("Job is not registered with the queue worker registry.");
1443
1592
  }
1444
- const envelope = {
1593
+ const envelope = snapshotQueueJobEnvelope({
1445
1594
  name,
1595
+ jobId: jobId ?? crypto.randomUUID(),
1446
1596
  payload,
1447
- attempts: 0
1448
- };
1597
+ attempts: 0,
1598
+ ...options?.handoff === undefined ? {} : { handoff: options.handoff }
1599
+ });
1449
1600
  if (this.asyncDispatch) {
1450
- this.enqueue(() => runQueueJob(envelope, this.failedJobs), (error) => {
1601
+ this.enqueue(() => runQueueJob(envelope, this.failedJobs, this.registry, { retryPolicy: this.retryPolicy }), (error) => {
1451
1602
  console.error("[ResilientQueue] Job failed:", error);
1452
1603
  });
1453
1604
  return;
1454
1605
  }
1455
- await runQueueJob(envelope, this.failedJobs);
1606
+ await runQueueJob(envelope, this.failedJobs, this.registry, { retryPolicy: this.retryPolicy });
1456
1607
  }
1457
1608
  }
1458
1609
 
1459
1610
  // ../../src/core/queue/publicQueue.ts
1460
- function createFailedJobService() {
1461
- return new failedJobService_default(new failedJobRepository_default);
1462
- }
1463
- function createTrackedJob(name, job) {
1464
- return jobRegistry.track(name, job);
1611
+ import { createIdentityQueue, createQueueHandoff } from "@getstrata/core/queue/queueHandoff";
1612
+ function createTrackedJob(registry, name, job) {
1613
+ return registry.track(name, job);
1465
1614
  }
1466
- function createProductionQueue(driver, options = {}) {
1467
- options.registerJobs?.();
1468
- const failedJobs = options.failedJobs ?? createFailedJobService();
1615
+ function createProductionQueue(driver, options) {
1616
+ const failedJobs = options.failedJobs;
1469
1617
  if (driver === "redis") {
1470
1618
  if (!options.redisUrl) {
1471
1619
  throw new Error('QUEUE_DRIVER="redis" requires REDIS_URL to be set.');
1472
1620
  }
1473
- return resolveRedisQueueTransport() === "streams" ? new RedisStreamsQueue(options.redisUrl) : new RedisQueue(options.redisUrl);
1621
+ const runtime = snapshotQueueRuntimeConfig(options.runtime ?? resolveQueueRuntimeConfig());
1622
+ return runtime.transport === "streams" ? new RedisStreamsQueue(options.redisUrl, options.registry, runtime.queueKeys) : new RedisQueue(options.redisUrl, options.registry, runtime.queueKeys);
1474
1623
  }
1475
- return new ResilientQueue(failedJobs, driver === "async");
1624
+ return new ResilientQueue(failedJobs, options.registry, driver === "async", snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig()));
1476
1625
  }
1477
- function createQueueWorker(redisUrl, failedJobs = createFailedJobService()) {
1478
- return resolveRedisQueueTransport() === "streams" ? new RedisStreamsWorker(redisUrl, failedJobs) : new QueueWorker(redisUrl, failedJobs);
1626
+ function createQueueWorker(redisUrl, failedJobs, registry, options = {}) {
1627
+ const runtime = snapshotQueueRuntimeConfig(options.runtime ?? resolveQueueRuntimeConfig());
1628
+ const retry = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
1629
+ return runtime.transport === "streams" ? new RedisStreamsWorker(redisUrl, failedJobs, registry, 5, runtime.queueKeys, retry, runtime.visibilityMs) : new QueueWorker(redisUrl, failedJobs, registry, 5, runtime.queueKeys, retry, runtime.visibilityMs);
1479
1630
  }
1480
1631
  export {
1481
- failedJobRepository_default as FailedJobRepository,
1632
+ FailedJobRepository,
1482
1633
  failedJobService_default as FailedJobService,
1483
1634
  QueueWorker,
1484
1635
  RedisQueue,
1485
1636
  ResilientQueue,
1486
1637
  createFailedJobService,
1638
+ createIdentityQueue,
1487
1639
  createProductionQueue,
1640
+ createQueueHandoff,
1641
+ createQueueServices,
1488
1642
  createQueueWorker,
1643
+ createSystemQueueServices,
1489
1644
  createTrackedJob,
1490
- jobRegistry,
1491
1645
  runQueueJob
1492
1646
  };