@getstrata/core 2.2.9 → 2.2.11

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.
@@ -1,4 +1,877 @@
1
1
  // @bun
2
+ var __esm = (fn, res, err) => () => {
3
+ if (fn)
4
+ try {
5
+ res = fn(fn = 0);
6
+ } catch (e) {
7
+ err = [e];
8
+ }
9
+ if (err)
10
+ throw err[0];
11
+ return res;
12
+ };
13
+
14
+ // ../../src/core/runtime/asyncContextStore.ts
15
+ import { AsyncLocalStorage } from "async_hooks";
16
+ function createAsyncContextStore(key) {
17
+ const symbol = Symbol.for(key);
18
+ const globalRecord = globalThis;
19
+ const existing = globalRecord[symbol];
20
+ if (existing) {
21
+ return existing;
22
+ }
23
+ const store = new AsyncLocalStorage;
24
+ globalRecord[symbol] = store;
25
+ return store;
26
+ }
27
+ var init_asyncContextStore = () => {};
28
+
29
+ // ../../src/core/database/schema/driver.ts
30
+ function normalizeConnectionName(connection) {
31
+ const normalized = connection.trim().toLowerCase();
32
+ if (normalized === "pgsql" || normalized === "postgres" || normalized === "postgresql") {
33
+ return "pgsql";
34
+ }
35
+ if (normalized === "mysql" || normalized === "mariadb") {
36
+ return "mysql";
37
+ }
38
+ if (normalized === "sqlite") {
39
+ return "sqlite";
40
+ }
41
+ throw new Error(`Unsupported DB_CONNECTION: ${connection}`);
42
+ }
43
+ function resolveDriverFromUrl(url) {
44
+ const normalized = url.trim().toLowerCase();
45
+ if (normalized.startsWith("postgres://") || normalized.startsWith("postgresql://") || normalized.startsWith("postgres:")) {
46
+ return "pgsql";
47
+ }
48
+ if (normalized.startsWith("mysql://") || normalized.startsWith("mysql:")) {
49
+ return "mysql";
50
+ }
51
+ if (normalized.startsWith("sqlite:")) {
52
+ return "sqlite";
53
+ }
54
+ return null;
55
+ }
56
+ function resolveDatabaseDriver(options = {}) {
57
+ const connection = options.connection ?? process.env.DB_CONNECTION;
58
+ if (connection) {
59
+ return normalizeConnectionName(connection);
60
+ }
61
+ const url = options.url ?? process.env.DATABASE_URL ?? "";
62
+ const fromUrl = resolveDriverFromUrl(url);
63
+ if (fromUrl) {
64
+ return fromUrl;
65
+ }
66
+ return "pgsql";
67
+ }
68
+ var init_driver = () => {};
69
+
70
+ // ../../src/core/database/dialect.ts
71
+ function assertSafeIdentifier(identifier) {
72
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(identifier)) {
73
+ throw new Error(`Invalid SQL identifier: ${identifier}`);
74
+ }
75
+ return identifier;
76
+ }
77
+ function quoteIdentifierFor(dialect, column) {
78
+ return dialect.quoteIdentifier(column);
79
+ }
80
+ function dialectFor(driver) {
81
+ return dialects[driver];
82
+ }
83
+ function currentSqlDialect() {
84
+ return dialectContext.getStore() ?? dialectOverride ?? dialectFor(resolveDatabaseDriver());
85
+ }
86
+ var postgresDialect, mysqlDialect, sqliteDialect, dialects, dialectContext, dialectOverride = null;
87
+ var init_dialect = __esm(() => {
88
+ init_asyncContextStore();
89
+ init_driver();
90
+ postgresDialect = {
91
+ driver: "pgsql",
92
+ placeholder(index) {
93
+ return `$${index}`;
94
+ },
95
+ quoteIdentifier(identifier) {
96
+ return `"${assertSafeIdentifier(identifier)}"`;
97
+ },
98
+ nowExpression() {
99
+ return "NOW()";
100
+ },
101
+ epochMillisecondsExpression() {
102
+ return "CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT)";
103
+ },
104
+ rowLockClause(skipLocked = false) {
105
+ return ` FOR UPDATE${skipLocked ? " SKIP LOCKED" : ""}`;
106
+ },
107
+ timestampValue(value) {
108
+ return value.toISOString();
109
+ },
110
+ returningClause(columns) {
111
+ return ` RETURNING ${columns}`;
112
+ },
113
+ ilikeOperator() {
114
+ return "ILIKE";
115
+ },
116
+ nullsLastSuffix() {
117
+ return " NULLS LAST";
118
+ },
119
+ castToText(expression) {
120
+ return `${expression}::text`;
121
+ },
122
+ upsertSuffix(conflictColumns, updateColumns) {
123
+ const target = conflictColumns.map((column) => quoteIdentifierFor(this, column)).join(", ");
124
+ if (updateColumns.length === 0) {
125
+ return ` ON CONFLICT (${target}) DO NOTHING`;
126
+ }
127
+ const assignments = updateColumns.map((column) => {
128
+ const quoted = quoteIdentifierFor(this, column);
129
+ return `${quoted} = excluded.${quoted}`;
130
+ }).join(", ");
131
+ return ` ON CONFLICT (${target}) DO UPDATE SET ${assignments}`;
132
+ }
133
+ };
134
+ mysqlDialect = {
135
+ driver: "mysql",
136
+ placeholder() {
137
+ return "?";
138
+ },
139
+ quoteIdentifier(identifier) {
140
+ return `\`${assertSafeIdentifier(identifier)}\``;
141
+ },
142
+ nowExpression() {
143
+ return "CURRENT_TIMESTAMP";
144
+ },
145
+ epochMillisecondsExpression() {
146
+ return "CAST(UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000 AS UNSIGNED)";
147
+ },
148
+ rowLockClause(skipLocked = false) {
149
+ return ` FOR UPDATE${skipLocked ? " SKIP LOCKED" : ""}`;
150
+ },
151
+ timestampValue(value) {
152
+ return value.toISOString().slice(0, 19).replace("T", " ");
153
+ },
154
+ returningClause() {
155
+ return "";
156
+ },
157
+ ilikeOperator() {
158
+ return "LIKE";
159
+ },
160
+ nullsLastSuffix() {
161
+ return "";
162
+ },
163
+ castToText(expression) {
164
+ return `CAST(${expression} AS CHAR)`;
165
+ },
166
+ upsertSuffix(conflictColumns, updateColumns) {
167
+ if (updateColumns.length === 0) {
168
+ const anchor = quoteIdentifierFor(this, conflictColumns[0] ?? "");
169
+ return ` ON DUPLICATE KEY UPDATE ${anchor} = ${anchor}`;
170
+ }
171
+ const assignments = updateColumns.map((column) => {
172
+ const quoted = quoteIdentifierFor(this, column);
173
+ return `${quoted} = VALUES(${quoted})`;
174
+ }).join(", ");
175
+ return ` ON DUPLICATE KEY UPDATE ${assignments}`;
176
+ }
177
+ };
178
+ sqliteDialect = {
179
+ driver: "sqlite",
180
+ placeholder() {
181
+ return "?";
182
+ },
183
+ quoteIdentifier(identifier) {
184
+ return `"${assertSafeIdentifier(identifier)}"`;
185
+ },
186
+ nowExpression() {
187
+ return "strftime('%Y-%m-%dT%H:%M:%fZ', 'now')";
188
+ },
189
+ epochMillisecondsExpression() {
190
+ return "CAST((julianday('now') - 2440587.5) * 86400000 AS INTEGER)";
191
+ },
192
+ rowLockClause() {
193
+ return "";
194
+ },
195
+ timestampValue(value) {
196
+ return value.toISOString();
197
+ },
198
+ returningClause(columns) {
199
+ return ` RETURNING ${columns}`;
200
+ },
201
+ ilikeOperator() {
202
+ return "LIKE";
203
+ },
204
+ nullsLastSuffix() {
205
+ return "";
206
+ },
207
+ castToText(expression) {
208
+ return `CAST(${expression} AS TEXT)`;
209
+ },
210
+ upsertSuffix(conflictColumns, updateColumns) {
211
+ const target = conflictColumns.map((column) => quoteIdentifierFor(this, column)).join(", ");
212
+ if (updateColumns.length === 0) {
213
+ return ` ON CONFLICT (${target}) DO NOTHING`;
214
+ }
215
+ const assignments = updateColumns.map((column) => {
216
+ const quoted = quoteIdentifierFor(this, column);
217
+ return `${quoted} = excluded.${quoted}`;
218
+ }).join(", ");
219
+ return ` ON CONFLICT (${target}) DO UPDATE SET ${assignments}`;
220
+ }
221
+ };
222
+ dialects = {
223
+ pgsql: postgresDialect,
224
+ mysql: mysqlDialect,
225
+ sqlite: sqliteDialect
226
+ };
227
+ dialectContext = createAsyncContextStore("@getstrata/sqlDialect");
228
+ });
229
+
230
+ // ../../src/core/database/connectionContext.ts
231
+ function runWithDatabaseConnection(connection, callback) {
232
+ return activeConnection.run(connection, callback);
233
+ }
234
+ function getActiveDatabaseConnection(fallback) {
235
+ return activeConnection.getStore() ?? fallback;
236
+ }
237
+ function hasActiveDatabaseConnection() {
238
+ return activeConnection.getStore() !== undefined;
239
+ }
240
+ var activeConnection;
241
+ var init_connectionContext = __esm(() => {
242
+ init_asyncContextStore();
243
+ activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
244
+ });
245
+
246
+ // ../../src/core/database/boundConnection.ts
247
+ function boundConnectionState() {
248
+ const globalRecord = globalThis;
249
+ const existing = globalRecord[BOUND_CONNECTION_KEY];
250
+ if (existing) {
251
+ return existing;
252
+ }
253
+ const created = { connection: null };
254
+ globalRecord[BOUND_CONNECTION_KEY] = created;
255
+ return created;
256
+ }
257
+ function getBoundDatabaseConnection() {
258
+ return boundConnectionState().connection;
259
+ }
260
+ var BOUND_CONNECTION_KEY;
261
+ var init_boundConnection = __esm(() => {
262
+ BOUND_CONNECTION_KEY = Symbol.for("@getstrata/boundDatabaseConnection");
263
+ });
264
+
265
+ // ../../src/core/database/queryProxy.ts
266
+ var POOL_CONNECTION_METHODS;
267
+ var init_queryProxy = __esm(() => {
268
+ init_connectionContext();
269
+ POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
270
+ });
271
+
272
+ // ../../src/core/database/defaultConnection.ts
273
+ function defaultConnectionState() {
274
+ const globalRecord = globalThis;
275
+ const existing = globalRecord[DEFAULT_CONNECTION_KEY];
276
+ if (existing) {
277
+ return existing;
278
+ }
279
+ const created = { pool: null, query: null };
280
+ globalRecord[DEFAULT_CONNECTION_KEY] = created;
281
+ return created;
282
+ }
283
+ function getDefaultDatabasePool() {
284
+ const pool = defaultConnectionState().pool;
285
+ if (!pool) {
286
+ throw new Error("Default database pool is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
287
+ }
288
+ return pool;
289
+ }
290
+ function getDefaultDatabaseQuery() {
291
+ const query = defaultConnectionState().query;
292
+ if (!query) {
293
+ throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
294
+ }
295
+ return query;
296
+ }
297
+ var DEFAULT_CONNECTION_KEY;
298
+ var init_defaultConnection = __esm(() => {
299
+ init_queryProxy();
300
+ DEFAULT_CONNECTION_KEY = Symbol.for("@getstrata/defaultDatabaseConnection");
301
+ });
302
+
303
+ // ../../src/core/database/repositoryConnection.ts
304
+ function resolveRepositoryConnection() {
305
+ return getActiveDatabaseConnection(getBoundDatabaseConnection() ?? getDefaultDatabaseQuery());
306
+ }
307
+ function compileTaggedSql(strings, values) {
308
+ const dialect = currentSqlDialect();
309
+ let query = strings[0] ?? "";
310
+ const params = [];
311
+ for (let index = 0;index < values.length; index += 1) {
312
+ params.push(values[index]);
313
+ query += `${dialect.placeholder(params.length)}${strings[index + 1] ?? ""}`;
314
+ }
315
+ return { query, params };
316
+ }
317
+ function isTaggedStrings(value) {
318
+ if (!Array.isArray(value) || typeof value[0] !== "string") {
319
+ return false;
320
+ }
321
+ return typeof value.raw !== "undefined";
322
+ }
323
+ function runOnConnection(connection, args) {
324
+ if (typeof connection === "function") {
325
+ return connection(...args);
326
+ }
327
+ if (typeof connection.unsafe !== "function") {
328
+ throw new Error("Database connection cannot run SQL.");
329
+ }
330
+ const strings = args[0];
331
+ if (!isTaggedStrings(strings)) {
332
+ throw new Error("Database connection cannot run tagged SQL.");
333
+ }
334
+ const compiled = compileTaggedSql(strings, args.slice(1));
335
+ return connection.unsafe(compiled.query, compiled.params);
336
+ }
337
+ var repositoryConnection;
338
+ var init_repositoryConnection = __esm(() => {
339
+ init_boundConnection();
340
+ init_connectionContext();
341
+ init_defaultConnection();
342
+ init_dialect();
343
+ repositoryConnection = new Proxy(function repositoryConnection() {}, {
344
+ apply(_target, _thisArg, args) {
345
+ return runOnConnection(resolveRepositoryConnection(), args);
346
+ },
347
+ get(_target, property) {
348
+ const connection = resolveRepositoryConnection();
349
+ const value = connection[property];
350
+ return typeof value === "function" ? value.bind(connection) : value;
351
+ }
352
+ });
353
+ });
354
+
355
+ // ../../src/core/tenant/tenantContext.ts
356
+ function runWithTenant(tenant, callback) {
357
+ return tenantContext.run(tenant, callback);
358
+ }
359
+ function currentTenant() {
360
+ return tenantContext.getStore() ?? null;
361
+ }
362
+ var tenantContext;
363
+ var init_tenantContext = __esm(() => {
364
+ init_asyncContextStore();
365
+ tenantContext = createAsyncContextStore("@getstrata/tenantContext");
366
+ });
367
+
368
+ // ../../src/core/events/eventBus.ts
369
+ class EventBus {
370
+ constructor() {}
371
+ transactionalListeners = new Map;
372
+ listenTransactional(event, listener) {
373
+ const handlers = this.transactionalListeners.get(event) ?? new Set;
374
+ handlers.add(listener);
375
+ this.transactionalListeners.set(event, handlers);
376
+ return () => {
377
+ handlers.delete(listener);
378
+ if (!handlers.size)
379
+ this.transactionalListeners.delete(event);
380
+ };
381
+ }
382
+ hasTransactionalListeners(event) {
383
+ return (this.transactionalListeners.get(event)?.size ?? 0) > 0;
384
+ }
385
+ async dispatchTransactional(event, payload) {
386
+ for (const listener of this.transactionalListeners.get(event) ?? [])
387
+ await listener(payload);
388
+ }
389
+ listeners = new Map;
390
+ on(event, listener) {
391
+ return this.listen(event, listener);
392
+ }
393
+ listen(event, listener) {
394
+ const handlers = this.listeners.get(event) ?? new Set;
395
+ handlers.add(listener);
396
+ this.listeners.set(event, handlers);
397
+ return () => {
398
+ handlers.delete(listener);
399
+ if (handlers.size === 0) {
400
+ this.listeners.delete(event);
401
+ }
402
+ };
403
+ }
404
+ async emit(event, payload) {
405
+ await this.dispatch(event, payload);
406
+ }
407
+ async dispatch(event, payload) {
408
+ const handlers = this.listeners.get(event);
409
+ if (!handlers || handlers.size === 0) {
410
+ return;
411
+ }
412
+ for (const handler of handlers) {
413
+ await handler(payload);
414
+ }
415
+ }
416
+ }
417
+ function readSharedEventBus() {
418
+ const globalBus = globalThis[EVENT_BUS_KEY];
419
+ if (globalBus) {
420
+ return globalBus;
421
+ }
422
+ const bus = new EventBus;
423
+ globalThis[EVENT_BUS_KEY] = bus;
424
+ return bus;
425
+ }
426
+ var EVENT_BUS_KEY, eventBus;
427
+ var init_eventBus = __esm(() => {
428
+ EVENT_BUS_KEY = Symbol.for("@getstrata/eventBus");
429
+ eventBus = readSharedEventBus();
430
+ });
431
+
432
+ // ../../src/core/database/transactionControl.ts
433
+ function unwrapTransactionRollback(error, depth = 0) {
434
+ if (error instanceof TransactionRollback) {
435
+ return error;
436
+ }
437
+ if (depth >= 5 || !(error instanceof Error) || error.cause === undefined) {
438
+ return null;
439
+ }
440
+ return unwrapTransactionRollback(error.cause, depth + 1);
441
+ }
442
+ function hasActiveTransaction() {
443
+ return hasActiveDatabaseConnection() && (transactionScopes.getStore()?.rollback.length ?? 0) > 0;
444
+ }
445
+ function requestTransactionRollback() {
446
+ const state = transactionScopes.getStore();
447
+ const index = state ? state.rollback.length - 1 : -1;
448
+ if (!state || index < 0) {
449
+ return;
450
+ }
451
+ const flags = state.rollback;
452
+ const current = flags[index];
453
+ if (current === undefined) {
454
+ return;
455
+ }
456
+ flags[index] = true;
457
+ }
458
+ function scopeRequestsRollback() {
459
+ const state = transactionScopes.getStore();
460
+ if (!state || state.rollback.length === 0) {
461
+ return false;
462
+ }
463
+ return state.rollback[state.rollback.length - 1] === true;
464
+ }
465
+ async function commitOrRollbackScope(result) {
466
+ if (scopeRequestsRollback()) {
467
+ throw new TransactionRollback(result);
468
+ }
469
+ return result;
470
+ }
471
+ async function settleTransaction(work) {
472
+ try {
473
+ return await work();
474
+ } catch (error) {
475
+ const rollback = unwrapTransactionRollback(error);
476
+ if (rollback) {
477
+ return rollback.result;
478
+ }
479
+ throw error;
480
+ }
481
+ }
482
+ async function runWithTransactionScope(callback) {
483
+ const existing = transactionScopes.getStore();
484
+ if (existing) {
485
+ existing.rollback.push(false);
486
+ try {
487
+ return await callback();
488
+ } finally {
489
+ existing.rollback.pop();
490
+ }
491
+ }
492
+ const state = { rollback: [false] };
493
+ return await transactionScopes.run(state, callback);
494
+ }
495
+ var TransactionRollback, transactionScopes;
496
+ var init_transactionControl = __esm(() => {
497
+ init_asyncContextStore();
498
+ init_connectionContext();
499
+ TransactionRollback = class TransactionRollback extends Error {
500
+ result;
501
+ constructor(result) {
502
+ super("Transaction rolled back");
503
+ this.name = "TransactionRollback";
504
+ this.result = result;
505
+ }
506
+ };
507
+ transactionScopes = createAsyncContextStore("@getstrata/transactionScopes");
508
+ });
509
+
510
+ // ../../src/core/tenant/tenancyConfig.ts
511
+ function readTenancyDriver(env = process.env) {
512
+ const raw = env.TENANCY_DRIVER?.trim();
513
+ if (raw === undefined || raw === "") {
514
+ return "rls";
515
+ }
516
+ if (TENANCY_DRIVERS.includes(raw)) {
517
+ return raw;
518
+ }
519
+ throw new Error(`TENANCY_DRIVER must be one of ${TENANCY_DRIVERS.join(", ")}; received "${raw}".`);
520
+ }
521
+ function isTenancyEnabled(env = process.env) {
522
+ return readTenancyDriver(env) !== "none";
523
+ }
524
+ function isRlsTenancy(env = process.env) {
525
+ return readTenancyDriver(env) === "rls";
526
+ }
527
+ var TENANCY_DRIVERS;
528
+ var init_tenancyConfig = __esm(() => {
529
+ TENANCY_DRIVERS = ["rls", "column", "none"];
530
+ });
531
+
532
+ // ../../src/core/tenant/databaseTenantContext.ts
533
+ import { getDefaultDatabasePool as getDefaultDatabasePool2 } from "@getstrata/core/database/defaultConnection";
534
+ async function applyBypassToTransaction(transaction, bypass) {
535
+ await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, [
536
+ bypass ? "true" : "false"
537
+ ]);
538
+ }
539
+ function stringifyBypassIdentifier(identifier) {
540
+ if (typeof identifier === "number") {
541
+ return String(identifier);
542
+ }
543
+ return identifier.trim();
544
+ }
545
+ async function applyIdentifierToTransaction(transaction, identifier) {
546
+ await transaction.unsafe(`SELECT set_config('app.bypass_identifier', $1, true)`, [
547
+ stringifyBypassIdentifier(identifier)
548
+ ]);
549
+ }
550
+ function assertBypassIdentifier(identifier) {
551
+ if (typeof identifier === "number") {
552
+ if (!Number.isInteger(identifier) || identifier <= 0) {
553
+ throw new Error("RLS bypass requires a caller-supplied identifier that pins the row.");
554
+ }
555
+ return;
556
+ }
557
+ if (typeof identifier === "string" && identifier.trim() !== "") {
558
+ return;
559
+ }
560
+ throw new Error("RLS bypass requires a caller-supplied identifier that pins the row.");
561
+ }
562
+ async function runWithScopedTenantTransaction(apply, callback) {
563
+ if (!isRlsTenancy()) {
564
+ return await callback();
565
+ }
566
+ if (hasActiveDatabaseConnection()) {
567
+ const connection = getActiveDatabaseConnection(getDefaultDatabasePool2());
568
+ return await runInTransaction(async () => {
569
+ const [previous] = await connection.unsafe(`SELECT current_setting('app.tenant_id', true) AS tenant_id,
570
+ current_setting('app.bypass_rls', true) AS bypass_rls,
571
+ current_setting('app.bypass_identifier', true) AS bypass_identifier`);
572
+ await apply(connection);
573
+ const result = await callback();
574
+ await connection.unsafe(`SELECT set_config('app.tenant_id', $1, true),
575
+ set_config('app.bypass_rls', $2, true), set_config('app.bypass_identifier', $3, true)`, [previous?.tenant_id ?? "", previous?.bypass_rls ?? "", previous?.bypass_identifier ?? ""]);
576
+ return result;
577
+ });
578
+ }
579
+ const pool = getDefaultDatabasePool2();
580
+ const begin = pool.begin;
581
+ if (typeof begin !== "function") {
582
+ throw new Error("RLS migration bypass requires a pool that supports begin(). Session-scoped set_config is not used on pooled connections.");
583
+ }
584
+ return await settleTransaction(() => runWithDeferredModelEvents(async () => {
585
+ return await begin.bind(pool)(async (transaction) => {
586
+ await apply(transaction);
587
+ return await runWithDatabaseConnection(transaction, () => runWithTransactionScope(async () => commitOrRollbackScope(await callback())));
588
+ });
589
+ }));
590
+ }
591
+ async function runWithMigrationBypass(callback) {
592
+ return await runWithScopedTenantTransaction((transaction) => applyBypassToTransaction(transaction, true), callback);
593
+ }
594
+ async function runWithMigrationBypassForIdentifier(identifier, callback) {
595
+ assertBypassIdentifier(identifier);
596
+ return await runWithScopedTenantTransaction((transaction) => applyIdentifierToTransaction(transaction, identifier), callback);
597
+ }
598
+ var init_databaseTenantContext = __esm(() => {
599
+ init_connectionContext();
600
+ init_transaction();
601
+ init_transactionControl();
602
+ init_deferredModelEvents();
603
+ init_tenancyConfig();
604
+ });
605
+
606
+ // ../../src/core/tenant/tenantDatabaseScope.ts
607
+ import { getDefaultDatabasePool as getDefaultDatabasePool3 } from "@getstrata/core/database/defaultConnection";
608
+ async function applyTenantContextToTransaction(transaction, tenantId) {
609
+ await transaction.unsafe(`SELECT set_config('app.tenant_id', $1, true)`, [String(tenantId)]);
610
+ await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, ["false"]);
611
+ }
612
+ async function runWithTenantDatabase(tenant, callback) {
613
+ const outerTenant = currentTenant();
614
+ if (hasActiveDatabaseConnection() && outerTenant && outerTenant.id !== tenant.id) {
615
+ throw new Error("Cannot switch tenants inside an active database transaction.");
616
+ }
617
+ if (!isTenancyEnabled() || !isRlsTenancy()) {
618
+ return await runWithTenant(tenant, callback);
619
+ }
620
+ if (hasActiveDatabaseConnection()) {
621
+ const activeConnection = getActiveDatabaseConnection(getDefaultDatabasePool3());
622
+ return await runWithScopedTenantTransaction(() => applyTenantContextToTransaction(activeConnection, tenant.id), () => runWithTenant(tenant, callback));
623
+ }
624
+ const pool = getDefaultDatabasePool3();
625
+ const begin = pool.begin;
626
+ if (typeof begin !== "function") {
627
+ throw new Error("RLS tenant scope requires a pool that supports begin(). Session-scoped set_config is not used on pooled connections.");
628
+ }
629
+ return await settleTransaction(() => runWithDeferredModelEvents(async () => {
630
+ return await begin.bind(pool)(async (transaction) => {
631
+ await applyTenantContextToTransaction(transaction, tenant.id);
632
+ return await runWithDatabaseConnection(transaction, async () => {
633
+ return await runWithTransactionScope(async () => commitOrRollbackScope(await runWithTenant(tenant, callback)));
634
+ });
635
+ });
636
+ }));
637
+ }
638
+ function isInsideTenantDatabaseScope(tenantId = currentTenant()?.id) {
639
+ return hasActiveDatabaseConnection() && currentTenant()?.id === tenantId;
640
+ }
641
+ var init_tenantDatabaseScope = __esm(() => {
642
+ init_connectionContext();
643
+ init_transactionControl();
644
+ init_deferredModelEvents();
645
+ init_databaseTenantContext();
646
+ init_tenancyConfig();
647
+ init_tenantContext();
648
+ });
649
+
650
+ // ../../src/core/events/deferredModelEvents.ts
651
+ function sameTenant(left, right) {
652
+ if (left === null && right === null) {
653
+ return true;
654
+ }
655
+ if (left === null || right === null) {
656
+ return false;
657
+ }
658
+ return left.id === right.id;
659
+ }
660
+ function groupQueuedModelEvents(queued) {
661
+ const groups = [];
662
+ for (const item of queued) {
663
+ const lastGroup = groups[groups.length - 1];
664
+ const last = lastGroup?.[lastGroup.length - 1];
665
+ if (!lastGroup || !last || !sameTenant(last.tenant, item.tenant)) {
666
+ groups.push([item]);
667
+ } else {
668
+ lastGroup.push(item);
669
+ }
670
+ }
671
+ return groups;
672
+ }
673
+ async function dispatchEventBatch(batch) {
674
+ for (const item of batch) {
675
+ await eventBus.dispatch(item.event, item.payload);
676
+ }
677
+ }
678
+ async function dispatchQueuedModelEvents(queued) {
679
+ if (queued.length === 0) {
680
+ return;
681
+ }
682
+ await Promise.resolve().then(() => init_tenantDatabaseScope());
683
+ for (const batch of groupQueuedModelEvents(queued)) {
684
+ const tenant = batch[0]?.tenant ?? null;
685
+ if (tenant) {
686
+ await runWithTenantDatabase(tenant, () => dispatchEventBatch(batch));
687
+ } else {
688
+ await dispatchEventBatch(batch);
689
+ }
690
+ }
691
+ }
692
+ async function runWithDeferredModelEvents(callback) {
693
+ const existing = deferredModelEvents.getStore();
694
+ if (existing) {
695
+ existing.frames.push([]);
696
+ existing.rollbackFrames.push([]);
697
+ try {
698
+ const result = await callback();
699
+ const nested = existing.frames.pop() ?? [];
700
+ const rollback = existing.rollbackFrames.pop() ?? [];
701
+ existing.rollbackFrames.at(-1)?.push(...rollback);
702
+ const parent = existing.frames[existing.frames.length - 1];
703
+ if (parent) {
704
+ parent.push(...nested);
705
+ }
706
+ return result;
707
+ } catch (error) {
708
+ existing.frames.pop();
709
+ for (const rollback of (existing.rollbackFrames.pop() ?? []).reverse())
710
+ rollback();
711
+ throw error;
712
+ }
713
+ }
714
+ const state = { frames: [[]], rollbackFrames: [[]] };
715
+ let result;
716
+ try {
717
+ result = await deferredModelEvents.run(state, callback);
718
+ } catch (error) {
719
+ for (const rollback of (state.rollbackFrames[0] ?? []).reverse())
720
+ rollback();
721
+ throw error;
722
+ }
723
+ await dispatchQueuedModelEvents(state.frames[0] ?? []);
724
+ return result;
725
+ }
726
+ var deferredModelEvents;
727
+ var init_deferredModelEvents = __esm(() => {
728
+ init_asyncContextStore();
729
+ init_tenantContext();
730
+ init_eventBus();
731
+ deferredModelEvents = createAsyncContextStore("@getstrata/deferredModelEvents");
732
+ });
733
+
734
+ // ../../src/core/database/acquisitionMetrics.ts
735
+ async function observeTransactionAcquisition(metrics, acquire, signal) {
736
+ const state = states.get(metrics);
737
+ if (!state)
738
+ throw new TypeError("Transaction acquisition metrics must be created by the framework factory.");
739
+ const started = performance.now();
740
+ state.inflight++;
741
+ let outcome = "acquired";
742
+ try {
743
+ return await acquire();
744
+ } catch (error) {
745
+ outcome = signal?.aborted ? "aborted" : "failed";
746
+ throw error;
747
+ } finally {
748
+ state.inflight--;
749
+ const elapsed = Math.max(0, performance.now() - started) / 1000;
750
+ const histogram = state.outcomes[outcomes.indexOf(outcome)];
751
+ if (histogram) {
752
+ histogram.count++;
753
+ histogram.sumSeconds += elapsed;
754
+ for (let index = 0;index < ACQUISITION_BUCKETS_SECONDS.length; index++) {
755
+ const bound = ACQUISITION_BUCKETS_SECONDS[index];
756
+ if (bound !== undefined && elapsed <= bound)
757
+ histogram.buckets[index] = (histogram.buckets[index] ?? 0) + 1;
758
+ }
759
+ }
760
+ }
761
+ }
762
+ var ACQUISITION_BUCKETS_SECONDS, outcomes, metricsBrand, states;
763
+ var init_acquisitionMetrics = __esm(() => {
764
+ ACQUISITION_BUCKETS_SECONDS = [
765
+ 0.001,
766
+ 0.005,
767
+ 0.01,
768
+ 0.025,
769
+ 0.05,
770
+ 0.1,
771
+ 0.25,
772
+ 0.5,
773
+ 1,
774
+ 2.5,
775
+ 5,
776
+ 10
777
+ ];
778
+ outcomes = ["acquired", "failed", "aborted"];
779
+ metricsBrand = Symbol("transaction acquisition metrics");
780
+ states = new WeakMap;
781
+ });
782
+
783
+ // ../../src/core/database/connection.ts
784
+ function createDatabaseConnection(source) {
785
+ return {
786
+ async unsafe(query, params = []) {
787
+ return await source.unsafe(query, params);
788
+ }
789
+ };
790
+ }
791
+ var init_connection = () => {};
792
+
793
+ // ../../src/core/database/transaction.ts
794
+ async function runInSavepoint(connection, operation) {
795
+ const inherited = transactionFrame.getStore();
796
+ let parent = inherited?.connection === connection ? inherited : outerFrames.get(connection);
797
+ if (!parent) {
798
+ parent = { connection, childActive: false };
799
+ outerFrames.set(connection, parent);
800
+ }
801
+ if (parent.childActive) {
802
+ throw new Error("Concurrent nested transactions on one connection are not supported. Await each transaction before starting the next.");
803
+ }
804
+ parent.childActive = true;
805
+ const name = `strata_${crypto.randomUUID().replaceAll("-", "")}`;
806
+ try {
807
+ return await settleTransaction(() => runWithDeferredModelEvents(async () => {
808
+ await connection.unsafe(`SAVEPOINT ${name}`);
809
+ try {
810
+ const result = await transactionFrame.run({ connection, childActive: false }, () => runWithTransactionScope(async () => commitOrRollbackScope(await operation(createDatabaseConnection(connection)))));
811
+ await connection.unsafe(`RELEASE SAVEPOINT ${name}`);
812
+ return result;
813
+ } catch (error) {
814
+ await connection.unsafe(`ROLLBACK TO SAVEPOINT ${name}`);
815
+ await connection.unsafe(`RELEASE SAVEPOINT ${name}`);
816
+ throw error;
817
+ }
818
+ }));
819
+ } finally {
820
+ parent.childActive = false;
821
+ }
822
+ }
823
+ function supportsTransactions(connection) {
824
+ return typeof connection.begin === "function";
825
+ }
826
+ async function runInTransaction(operation, options = {}) {
827
+ const signal = options.acquisitionSignal;
828
+ signal?.throwIfAborted();
829
+ const admittedOperation = (connection) => {
830
+ signal?.throwIfAborted();
831
+ return operation(connection);
832
+ };
833
+ const pool = resolveRepositoryConnection();
834
+ if (hasActiveDatabaseConnection()) {
835
+ return await runInSavepoint(getActiveDatabaseConnection(pool), admittedOperation);
836
+ }
837
+ if (!supportsTransactions(pool)) {
838
+ throw new Error("Active database connection does not support transactions. Bind a client with begin() via bindDatabaseConnection.");
839
+ }
840
+ return await settleTransaction(() => runWithDeferredModelEvents(async () => {
841
+ let reserved;
842
+ if (signal || options.acquisitionMetrics) {
843
+ if (!pool.reserve)
844
+ throw new Error(signal ? "Active database connection does not support cancellable transaction acquisition." : "Active database connection does not support observable transaction acquisition.");
845
+ const reserve = pool.reserve.bind(pool);
846
+ reserved = options.acquisitionMetrics ? await observeTransactionAcquisition(options.acquisitionMetrics, () => reserve({ signal }), signal) : await reserve({ signal });
847
+ }
848
+ const connection = reserved ?? pool;
849
+ try {
850
+ signal?.throwIfAborted();
851
+ if (!supportsTransactions(connection))
852
+ throw new Error("Reserved database connection does not support transactions.");
853
+ return await connection.begin(async (transaction) => {
854
+ return await runWithDatabaseConnection(transaction, () => transactionFrame.run({ connection: transaction, childActive: false }, () => runWithTransactionScope(async () => commitOrRollbackScope(await admittedOperation(createDatabaseConnection(transaction))))));
855
+ });
856
+ } finally {
857
+ await reserved?.release();
858
+ }
859
+ }));
860
+ }
861
+ var transactionFrame, outerFrames;
862
+ var init_transaction = __esm(() => {
863
+ init_deferredModelEvents();
864
+ init_asyncContextStore();
865
+ init_acquisitionMetrics();
866
+ init_connection();
867
+ init_connectionContext();
868
+ init_repositoryConnection();
869
+ init_transactionControl();
870
+ init_acquisitionMetrics();
871
+ transactionFrame = createAsyncContextStore("@getstrata/transactionFrame");
872
+ outerFrames = new WeakMap;
873
+ });
874
+
2
875
  // ../../src/core/queue/queueMetrics.ts
3
876
  var {RedisClient: RedisClient3 } = globalThis.Bun;
4
877
 
@@ -1319,6 +2192,151 @@ function createAppQueue(driver, redisUrl, failedJobs = createFailedJobService(),
1319
2192
  });
1320
2193
  }
1321
2194
 
2195
+ // ../../src/core/queue/failedJobMetrics.ts
2196
+ init_dialect();
2197
+
2198
+ // ../../src/core/database/readOnlyCollector.ts
2199
+ init_connectionContext();
2200
+ init_dialect();
2201
+ init_repositoryConnection();
2202
+ init_transaction();
2203
+ function createReadOnlyCollector(name, timeoutMs, collect) {
2204
+ if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 5000)
2205
+ throw new TypeError(`${name} timeout must be between 1 and 5000 milliseconds.`);
2206
+ let active;
2207
+ let closed = false;
2208
+ return {
2209
+ async collect() {
2210
+ if (closed)
2211
+ throw new Error(`${name} collector is closed.`);
2212
+ if (hasActiveTransaction() || hasActiveDatabaseConnection())
2213
+ throw new Error(`${name} require an independent transaction.`);
2214
+ const driver = currentSqlDialect().driver;
2215
+ const pool = resolveRepositoryConnection();
2216
+ if (driver !== "pgsql" && driver !== "sqlite" && driver !== "mysql")
2217
+ throw new Error(`${name} require Postgres or an interruptible SQLite/MySQL adapter.`);
2218
+ if (active)
2219
+ throw new Error(`${name} collection is still settling.`);
2220
+ const deadline = performance.now() + timeoutMs;
2221
+ let expired = false;
2222
+ const acquisition = new AbortController;
2223
+ let timer;
2224
+ const remaining = () => {
2225
+ const value = Math.floor(deadline - performance.now());
2226
+ if (expired || value < 1)
2227
+ throw new Error(`${name} collection timed out.`);
2228
+ return value;
2229
+ };
2230
+ const work = driver === "pgsql" ? runInTransaction(async () => {
2231
+ await repositoryConnection.unsafe("SELECT set_config('statement_timeout', $1, true)", [
2232
+ `${remaining()}ms`
2233
+ ]);
2234
+ await repositoryConnection.unsafe("SET TRANSACTION READ ONLY");
2235
+ remaining();
2236
+ const result = await collect(remaining);
2237
+ remaining();
2238
+ return result;
2239
+ }, { acquisitionSignal: acquisition.signal }) : (async () => {
2240
+ if (!pool.observeReadOnly)
2241
+ throw new Error(`${name} require Postgres or an interruptible SQLite/MySQL adapter.`);
2242
+ return pool.observeReadOnly(async (connection) => await runWithDatabaseConnection(connection, async () => {
2243
+ remaining();
2244
+ const result = await collect(remaining);
2245
+ remaining();
2246
+ return result;
2247
+ }), { signal: acquisition.signal, timeoutMs: remaining() });
2248
+ })();
2249
+ active = work;
2250
+ work.finally(() => {
2251
+ if (active === work)
2252
+ active = undefined;
2253
+ }).catch(() => {});
2254
+ try {
2255
+ return await Promise.race([
2256
+ work,
2257
+ new Promise((_, reject) => {
2258
+ timer = setTimeout(() => {
2259
+ expired = true;
2260
+ const error = new Error(`${name} collection timed out.`);
2261
+ acquisition.abort(error);
2262
+ reject(error);
2263
+ }, timeoutMs);
2264
+ })
2265
+ ]);
2266
+ } finally {
2267
+ if (timer !== undefined)
2268
+ clearTimeout(timer);
2269
+ }
2270
+ },
2271
+ async close() {
2272
+ closed = true;
2273
+ await active?.catch(() => {});
2274
+ }
2275
+ };
2276
+ }
2277
+
2278
+ // ../../src/core/queue/failedJobMetrics.ts
2279
+ init_repositoryConnection();
2280
+ var schemaQuery = `SELECT c.relkind = 'r' AND c.relpersistence = 'p' AND NOT c.relrowsecurity AND EXISTS (
2281
+ SELECT 1 FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = i.indkey[0]
2282
+ WHERE i.indrelid = c.oid AND i.indisprimary AND i.indisvalid AND i.indisready
2283
+ AND i.indnkeyatts = 1 AND a.attname = 'id' AND NOT a.attisdropped
2284
+ AND a.atttypid IN ('int2'::regtype, 'int4'::regtype, 'int8'::regtype)
2285
+ ) AS supported FROM pg_class c WHERE c.oid = 'failed_job'::regclass`;
2286
+ var sqliteSchemaQuery = `SELECT 1 AS supported FROM main.sqlite_schema
2287
+ WHERE name='failed_job' AND type='table' AND rootpage > 0
2288
+ AND (SELECT COUNT(*) FROM pragma_table_xinfo('failed_job', 'main') WHERE pk > 0) = 1
2289
+ AND EXISTS (SELECT 1 FROM pragma_table_xinfo('failed_job', 'main')
2290
+ WHERE name='id' AND pk=1 AND upper(type)='INTEGER' AND hidden=0) LIMIT 1`;
2291
+ var mysqlSchemaQuery = `SELECT 1 AS supported FROM information_schema.TABLES t
2292
+ WHERE t.TABLE_SCHEMA=DATABASE() AND t.TABLE_NAME='failed_job' AND t.TABLE_TYPE='BASE TABLE' AND t.ENGINE='InnoDB'
2293
+ AND (SELECT COUNT(*) FROM information_schema.STATISTICS i WHERE i.TABLE_SCHEMA=t.TABLE_SCHEMA
2294
+ AND i.TABLE_NAME=t.TABLE_NAME AND i.INDEX_NAME='PRIMARY')=1
2295
+ AND EXISTS(SELECT 1 FROM information_schema.STATISTICS i JOIN information_schema.COLUMNS c
2296
+ ON c.TABLE_SCHEMA=i.TABLE_SCHEMA AND c.TABLE_NAME=i.TABLE_NAME AND c.COLUMN_NAME=i.COLUMN_NAME
2297
+ WHERE i.TABLE_SCHEMA=t.TABLE_SCHEMA AND i.TABLE_NAME=t.TABLE_NAME AND i.INDEX_NAME='PRIMARY'
2298
+ AND i.SEQ_IN_INDEX=1 AND i.COLUMN_NAME='id' AND c.DATA_TYPE IN ('tinyint','smallint','mediumint','int','bigint')) LIMIT 1`;
2299
+ function createFailedJobMetricsCollector(options = {}) {
2300
+ const sampleLimit = options.sampleLimit ?? 500;
2301
+ if (!Number.isSafeInteger(sampleLimit) || sampleLimit < 1 || sampleLimit > 1000)
2302
+ throw new TypeError("Failed-job metrics sample limit must be between 1 and 1000.");
2303
+ const repository = new failedJobRepository_default;
2304
+ return createReadOnlyCollector("Failed-job metrics", options.timeoutMs ?? 1000, async (remaining) => {
2305
+ const driver = currentSqlDialect().driver;
2306
+ const postgres = driver === "pgsql";
2307
+ if (postgres)
2308
+ await repositoryConnection.unsafe('LOCK TABLE "failed_job" IN ACCESS SHARE MODE');
2309
+ if (driver === "mysql")
2310
+ await repositoryConnection.unsafe("SELECT id FROM failed_job LIMIT 0");
2311
+ const schema = await repositoryConnection.unsafe(postgres ? schemaQuery : driver === "mysql" ? mysqlSchemaQuery : sqliteSchemaQuery);
2312
+ if (schema.length !== 1 || schema[0]?.supported !== (postgres ? true : 1))
2313
+ throw new Error("Failed-job metrics require the standard global table with an ID primary key and no RLS.");
2314
+ remaining();
2315
+ const rows = await repository.query().orderBy({ column: "id", direction: "ASC" }).project(["id"], sampleLimit + 1);
2316
+ if (rows.length > sampleLimit + 1 || rows.some((row) => row.id == null))
2317
+ throw new Error("Invalid failed-job metrics response.");
2318
+ return {
2319
+ sampleLimit,
2320
+ count: Math.min(rows.length, sampleLimit),
2321
+ capped: rows.length > sampleLimit
2322
+ };
2323
+ });
2324
+ }
2325
+ function renderFailedJobMetrics(snapshot) {
2326
+ if (!Number.isSafeInteger(snapshot.sampleLimit) || snapshot.sampleLimit < 1 || snapshot.sampleLimit > 1000 || !Number.isSafeInteger(snapshot.count) || snapshot.count < 0 || snapshot.count > snapshot.sampleLimit || typeof snapshot.capped !== "boolean" || snapshot.capped && snapshot.count !== snapshot.sampleLimit)
2327
+ throw new TypeError("Invalid failed-job metrics snapshot.");
2328
+ return `# HELP strata_queue_failed_job_collector_success Whether failed-job collection succeeded.
2329
+ # TYPE strata_queue_failed_job_collector_success gauge
2330
+ strata_queue_failed_job_collector_success 1
2331
+ # HELP strata_queue_failed_jobs_sample Capped retained failed-job count; a lower bound when capped.
2332
+ # TYPE strata_queue_failed_jobs_sample gauge
2333
+ strata_queue_failed_jobs_sample ${snapshot.count}
2334
+ # HELP strata_queue_failed_jobs_sample_capped Whether the failed-job sample is truncated.
2335
+ # TYPE strata_queue_failed_jobs_sample_capped gauge
2336
+ strata_queue_failed_jobs_sample_capped ${Number(snapshot.capped)}
2337
+ `;
2338
+ }
2339
+
1322
2340
  // ../../src/core/queue/queueMetrics.ts
1323
2341
  async function readRedisQueueDepth(redisUrl) {
1324
2342
  const client = new RedisClient3(redisUrl);
@@ -1393,18 +2411,28 @@ local ready = capped == 1 and -1 or size-inflight
1393
2411
  if ready < 0 and capped == 0 then return redis.error_reply('Inconsistent stream pending state') end
1394
2412
  local retries = redis.call('ZCARD', KEYS[2])
1395
2413
  local due = redis.call('ZCOUNT', KEYS[2], '-inf', ms)
2414
+ local lateness = 0
2415
+ if due > 0 then
2416
+ local oldest = redis.call('ZRANGE', KEYS[2], 0, 0, 'WITHSCORES')
2417
+ local score = tonumber(oldest[2])
2418
+ if not score or score < 0 or score ~= math.floor(score) or score > ms then
2419
+ return redis.error_reply('Invalid retry due score')
2420
+ end
2421
+ -- Cancellation sets score zero to request immediate promotion/cleanup; it is not a timestamp.
2422
+ lateness = score == 0 and -1 or (ms-score)/1000
2423
+ end
1396
2424
  local first = redis.call('XRANGE', KEYS[1], '-', '+', 'COUNT', 1)
1397
2425
  local age = 0
1398
2426
  if #first > 0 then
1399
2427
  local timestamp = tonumber(string.match(first[1][1], '^(%d+)-'))
1400
2428
  age = math.max(0, ms-timestamp)/1000
1401
2429
  end
1402
- return {size+retries, ready, inflight, capped, due, retries-due, redis.call('LLEN', KEYS[3]), tostring(age)}
2430
+ return {size+retries, ready, inflight, capped, due, retries-due, redis.call('LLEN', KEYS[3]), tostring(age), tostring(lateness)}
1403
2431
  `;
1404
2432
  var LIST_SNAPSHOT = `
1405
2433
  local ready = redis.call('LLEN', KEYS[1])
1406
2434
  local inflight = redis.call('LLEN', KEYS[2])
1407
- return {ready+inflight, ready, inflight, 0, 0, 0, redis.call('LLEN', KEYS[3]), '-1'}
2435
+ return {ready+inflight, ready, inflight, 0, 0, 0, redis.call('LLEN', KEYS[3]), '-1', '-1'}
1408
2436
  `;
1409
2437
  async function readRedisQueueSnapshot(redisUrl, options = {}) {
1410
2438
  if (!redisUrl.trim())
@@ -1427,13 +2455,23 @@ async function readRedisQueueSnapshot(redisUrl, options = {}) {
1427
2455
  STREAM_GROUP,
1428
2456
  String(INFLIGHT_SAMPLE_LIMIT)
1429
2457
  ]);
1430
- if (!Array.isArray(raw) || raw.length !== 8)
2458
+ if (!Array.isArray(raw) || raw.length !== 9)
1431
2459
  throw new Error("Invalid queue metrics response.");
1432
2460
  const values = raw.map(Number);
1433
2461
  if (values.some((value) => !Number.isFinite(value)) || values.slice(0, 7).some((value, i) => !Number.isSafeInteger(value) || value < (i === 1 ? -1 : 0)))
1434
2462
  throw new Error("Invalid queue metrics values.");
1435
- const [unfinished, ready, inflight, capped, retryDue, retryWaiting, quarantined, age] = values;
1436
- if (unfinished === undefined || ready === undefined || inflight === undefined || capped === undefined || retryDue === undefined || retryWaiting === undefined || quarantined === undefined || age === undefined || capped !== 0 && capped !== 1 || age < (transport === "lists" ? -1 : 0))
2463
+ const [
2464
+ unfinished,
2465
+ ready,
2466
+ inflight,
2467
+ capped,
2468
+ retryDue,
2469
+ retryWaiting,
2470
+ quarantined,
2471
+ age,
2472
+ lateness
2473
+ ] = values;
2474
+ if (unfinished === undefined || ready === undefined || inflight === undefined || capped === undefined || retryDue === undefined || retryWaiting === undefined || quarantined === undefined || age === undefined || lateness === undefined || lateness < 0 && lateness !== -1 || capped !== 0 && capped !== 1 || age < (transport === "lists" ? -1 : 0))
1437
2475
  throw new Error("Invalid queue metrics values.");
1438
2476
  return {
1439
2477
  priority: index === 0 ? "high" : index === 1 ? "default" : "low",
@@ -1443,6 +2481,7 @@ async function readRedisQueueSnapshot(redisUrl, options = {}) {
1443
2481
  inflightCapped: capped === 1,
1444
2482
  retryDue,
1445
2483
  retryWaiting,
2484
+ retryActionableLatenessSeconds: lateness === -1 ? null : lateness,
1446
2485
  quarantined,
1447
2486
  oldestStreamEntryAgeSeconds: transport === "streams" ? age : null
1448
2487
  };
@@ -1464,7 +2503,7 @@ function renderRedisQueueMetrics(snapshot) {
1464
2503
  if (snapshot.transport !== "streams" && snapshot.transport !== "lists" || snapshot.priorities.length !== 3 || new Set(snapshot.priorities.map((row) => row.priority)).size !== 3)
1465
2504
  throw new TypeError("Invalid queue metrics snapshot.");
1466
2505
  for (const row of snapshot.priorities) {
1467
- if (!["high", "default", "low"].includes(row.priority) || typeof row.inflightCapped !== "boolean" || [row.unfinished, row.inflight, row.retryDue, row.retryWaiting, row.quarantined].some((value) => !Number.isSafeInteger(value) || value < 0) || row.ready !== null && (!Number.isSafeInteger(row.ready) || row.ready < 0) || row.oldestStreamEntryAgeSeconds !== null && (!Number.isFinite(row.oldestStreamEntryAgeSeconds) || row.oldestStreamEntryAgeSeconds < 0))
2506
+ if (!["high", "default", "low"].includes(row.priority) || typeof row.inflightCapped !== "boolean" || [row.unfinished, row.inflight, row.retryDue, row.retryWaiting, row.quarantined].some((value) => !Number.isSafeInteger(value) || value < 0) || row.ready !== null && (!Number.isSafeInteger(row.ready) || row.ready < 0) || row.retryActionableLatenessSeconds != null && (!Number.isFinite(row.retryActionableLatenessSeconds) || row.retryActionableLatenessSeconds < 0) || row.oldestStreamEntryAgeSeconds !== null && (!Number.isFinite(row.oldestStreamEntryAgeSeconds) || row.oldestStreamEntryAgeSeconds < 0))
1468
2507
  throw new TypeError("Invalid queue metrics snapshot.");
1469
2508
  }
1470
2509
  const lines = [
@@ -1478,7 +2517,9 @@ function renderRedisQueueMetrics(snapshot) {
1478
2517
  "# HELP strata_queue_inflight_sample_capped Whether the inflight sample was truncated; ready is omitted when truncated.",
1479
2518
  "# TYPE strata_queue_inflight_sample_capped gauge",
1480
2519
  "# HELP strata_queue_oldest_stream_entry_age_seconds Oldest current stream entry residence age; resets when a retry is promoted.",
1481
- "# TYPE strata_queue_oldest_stream_entry_age_seconds gauge"
2520
+ "# TYPE strata_queue_oldest_stream_entry_age_seconds gauge",
2521
+ "# HELP strata_queue_retry_actionable_lateness_seconds Oldest retry due-score lateness; unavailable for cancellation sentinels or lists.",
2522
+ "# TYPE strata_queue_retry_actionable_lateness_seconds gauge"
1482
2523
  ];
1483
2524
  for (const priority of snapshot.priorities) {
1484
2525
  const label = `priority="${priority.priority}"`;
@@ -1494,6 +2535,8 @@ function renderRedisQueueMetrics(snapshot) {
1494
2535
  if (value !== null)
1495
2536
  lines.push(`strata_queue_jobs{${label},state="${state}"} ${value}`);
1496
2537
  lines.push(`strata_queue_inflight_sample_capped{${label}} ${Number(priority.inflightCapped)}`);
2538
+ if (snapshot.transport === "streams" && priority.retryActionableLatenessSeconds != null)
2539
+ lines.push(`strata_queue_retry_actionable_lateness_seconds{${label}} ${priority.retryActionableLatenessSeconds}`);
1497
2540
  if (priority.oldestStreamEntryAgeSeconds !== null)
1498
2541
  lines.push(`strata_queue_oldest_stream_entry_age_seconds{${label}} ${priority.oldestStreamEntryAgeSeconds}`);
1499
2542
  }
@@ -1504,7 +2547,9 @@ function renderRedisQueueMetrics(snapshot) {
1504
2547
  export {
1505
2548
  INFLIGHT_SAMPLE_LIMIT,
1506
2549
  collectQueueMetrics,
2550
+ createFailedJobMetricsCollector,
1507
2551
  readRedisQueueDepth,
1508
2552
  readRedisQueueSnapshot,
2553
+ renderFailedJobMetrics,
1509
2554
  renderRedisQueueMetrics
1510
2555
  };