@adcp/sdk 16.0.0-beta.3 → 16.0.0-beta.5

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 (185) hide show
  1. package/dist/lib/auth/oauth/diagnose.js +3 -11
  2. package/dist/lib/auth/oauth/diagnose.mjs +4 -12
  3. package/dist/lib/auth/oauth/diagnostics.d.mts +2 -1
  4. package/dist/lib/auth/oauth/diagnostics.d.ts +2 -1
  5. package/dist/lib/auth/oauth/diagnostics.js +8 -10
  6. package/dist/lib/auth/oauth/diagnostics.mjs +7 -10
  7. package/dist/lib/index.d.mts +1 -1
  8. package/dist/lib/index.d.ts +1 -1
  9. package/dist/lib/index.js +2 -0
  10. package/dist/lib/index.mjs +7 -1
  11. package/dist/lib/reporting/consumer-runtime.js +2 -1
  12. package/dist/lib/reporting/consumer-runtime.mjs +2 -1
  13. package/dist/lib/reporting/ledger/handler.js +50 -11
  14. package/dist/lib/reporting/ledger/handler.mjs +50 -12
  15. package/dist/lib/reporting/ledger/index.d.mts +17 -14
  16. package/dist/lib/reporting/ledger/index.d.ts +17 -14
  17. package/dist/lib/reporting/ledger/index.js +32 -20
  18. package/dist/lib/reporting/ledger/index.mjs +21 -10
  19. package/dist/lib/reporting/ledger/offering-validation.d.mts +9 -0
  20. package/dist/lib/reporting/ledger/offering-validation.d.ts +9 -0
  21. package/dist/lib/reporting/{gcs/bucket-policy.js → ledger/offering-validation.js} +9 -8
  22. package/dist/lib/reporting/ledger/offering-validation.mjs +8 -0
  23. package/dist/lib/reporting/ledger/receipts.d.mts +48 -0
  24. package/dist/lib/reporting/ledger/receipts.d.ts +48 -0
  25. package/dist/lib/reporting/ledger/receipts.js +186 -0
  26. package/dist/lib/reporting/ledger/receipts.mjs +161 -0
  27. package/dist/lib/reporting/ledger/status-ingest.js +3 -3
  28. package/dist/lib/reporting/ledger/status-ingest.mjs +4 -5
  29. package/dist/lib/reporting/ledger/types.d.mts +43 -0
  30. package/dist/lib/reporting/ledger/types.d.ts +43 -0
  31. package/dist/lib/reporting/ledger/types.js +31 -2
  32. package/dist/lib/reporting/ledger/types.mjs +26 -1
  33. package/dist/lib/reporting/pagination.d.mts +6 -0
  34. package/dist/lib/reporting/pagination.d.ts +6 -0
  35. package/dist/lib/reporting/{gcs/errors.js → pagination.js} +12 -17
  36. package/dist/lib/reporting/pagination.mjs +12 -0
  37. package/dist/lib/reporting/reconciliation.d.mts +1 -0
  38. package/dist/lib/reporting/reconciliation.d.ts +1 -0
  39. package/dist/lib/reporting/reconciliation.js +3 -2
  40. package/dist/lib/reporting/reconciliation.mjs +3 -2
  41. package/dist/lib/reporting/service/index.d.mts +79 -153
  42. package/dist/lib/reporting/service/index.d.ts +79 -153
  43. package/dist/lib/reporting/service/index.js +34 -491
  44. package/dist/lib/reporting/service/index.mjs +36 -510
  45. package/dist/lib/reporting/service/scheduler-support.d.mts +43 -0
  46. package/dist/lib/reporting/service/scheduler-support.d.ts +43 -0
  47. package/dist/lib/reporting/service/scheduler-support.js +125 -0
  48. package/dist/lib/reporting/service/scheduler-support.mjs +96 -0
  49. package/dist/lib/schemas-data/v2.5/_provenance.json +1 -1
  50. package/dist/lib/signing/canonicalize.d.mts +4 -0
  51. package/dist/lib/signing/canonicalize.d.ts +4 -0
  52. package/dist/lib/signing/canonicalize.js +14 -8
  53. package/dist/lib/signing/canonicalize.mjs +14 -8
  54. package/dist/lib/signing/content-digest.d.mts +8 -0
  55. package/dist/lib/signing/content-digest.d.ts +8 -0
  56. package/dist/lib/signing/content-digest.js +11 -5
  57. package/dist/lib/signing/content-digest.mjs +10 -5
  58. package/dist/lib/signing/signer.js +9 -6
  59. package/dist/lib/signing/signer.mjs +10 -7
  60. package/dist/lib/signing/web-bot-auth.js +103 -37
  61. package/dist/lib/signing/web-bot-auth.mjs +108 -37
  62. package/dist/lib/testing/storyboard/junit.js +16 -3
  63. package/dist/lib/testing/storyboard/junit.mjs +16 -3
  64. package/dist/lib/testing/storyboard/request-signing/builder.js +10 -9
  65. package/dist/lib/testing/storyboard/request-signing/builder.mjs +10 -9
  66. package/dist/lib/testing/storyboard/runner.js +94 -25
  67. package/dist/lib/testing/storyboard/runner.mjs +94 -25
  68. package/dist/lib/testing/storyboard/types.d.mts +6 -0
  69. package/dist/lib/testing/storyboard/types.d.ts +6 -0
  70. package/dist/lib/testing/storyboard/validations.d.mts +1 -1
  71. package/dist/lib/testing/storyboard/validations.d.ts +1 -1
  72. package/dist/lib/types/schemas.generated.d.ts +907 -60
  73. package/dist/lib/types/schemas.generated.js +7 -7
  74. package/dist/lib/types/schemas.generated.mjs +7 -7
  75. package/dist/lib/version.d.mts +3 -3
  76. package/dist/lib/version.d.ts +3 -3
  77. package/dist/lib/version.js +3 -3
  78. package/dist/lib/version.mjs +3 -3
  79. package/docs/TYPE-SUMMARY.md +23 -5
  80. package/docs/guides/REPORTING-GCS-FENCE.md +3 -3
  81. package/docs/guides/REPORTING-GCS-MANAGED.md +4 -4
  82. package/docs/guides/REPORTING-LEDGER-CUTOVER.md +7 -7
  83. package/docs/guides/REPORTING-LEDGER.md +107 -51
  84. package/docs/guides/REPORTING-OPERATIONS.md +66 -5
  85. package/docs/guides/REPORTING-STABILITY.md +36 -32
  86. package/docs/llms.txt +4 -3
  87. package/docs/migration-14.x-rc-worksheet.md +4 -7
  88. package/docs/migration-reporting-storage-package.md +186 -0
  89. package/examples/README.md +1 -1
  90. package/package.json +13 -78
  91. package/dist/lib/reporting/azure/index.d.mts +0 -31
  92. package/dist/lib/reporting/azure/index.d.ts +0 -31
  93. package/dist/lib/reporting/azure/index.js +0 -266
  94. package/dist/lib/reporting/azure/index.mjs +0 -249
  95. package/dist/lib/reporting/bigquery/index.d.mts +0 -246
  96. package/dist/lib/reporting/bigquery/index.d.ts +0 -246
  97. package/dist/lib/reporting/bigquery/index.js +0 -633
  98. package/dist/lib/reporting/bigquery/index.mjs +0 -594
  99. package/dist/lib/reporting/gcs/adapter.d.mts +0 -38
  100. package/dist/lib/reporting/gcs/adapter.d.ts +0 -38
  101. package/dist/lib/reporting/gcs/adapter.js +0 -342
  102. package/dist/lib/reporting/gcs/adapter.mjs +0 -325
  103. package/dist/lib/reporting/gcs/bucket-policy.d.mts +0 -3
  104. package/dist/lib/reporting/gcs/bucket-policy.d.ts +0 -3
  105. package/dist/lib/reporting/gcs/bucket-policy.mjs +0 -7
  106. package/dist/lib/reporting/gcs/errors.d.mts +0 -11
  107. package/dist/lib/reporting/gcs/errors.d.ts +0 -11
  108. package/dist/lib/reporting/gcs/errors.mjs +0 -16
  109. package/dist/lib/reporting/gcs/fence.d.mts +0 -41
  110. package/dist/lib/reporting/gcs/fence.d.ts +0 -41
  111. package/dist/lib/reporting/gcs/fence.js +0 -250
  112. package/dist/lib/reporting/gcs/fence.mjs +0 -230
  113. package/dist/lib/reporting/gcs/index.d.mts +0 -18
  114. package/dist/lib/reporting/gcs/index.d.ts +0 -18
  115. package/dist/lib/reporting/gcs/index.js +0 -44
  116. package/dist/lib/reporting/gcs/index.mjs +0 -14
  117. package/dist/lib/reporting/gcs/io.d.mts +0 -7
  118. package/dist/lib/reporting/gcs/io.d.ts +0 -7
  119. package/dist/lib/reporting/gcs/io.js +0 -95
  120. package/dist/lib/reporting/gcs/io.mjs +0 -69
  121. package/dist/lib/reporting/gcs/reader.d.mts +0 -67
  122. package/dist/lib/reporting/gcs/reader.d.ts +0 -67
  123. package/dist/lib/reporting/gcs/reader.js +0 -193
  124. package/dist/lib/reporting/gcs/reader.mjs +0 -169
  125. package/dist/lib/reporting/gcs/row-provider.d.mts +0 -39
  126. package/dist/lib/reporting/gcs/row-provider.d.ts +0 -39
  127. package/dist/lib/reporting/gcs/row-provider.js +0 -225
  128. package/dist/lib/reporting/gcs/row-provider.mjs +0 -208
  129. package/dist/lib/reporting/gcs/validation.d.mts +0 -5
  130. package/dist/lib/reporting/gcs/validation.d.ts +0 -5
  131. package/dist/lib/reporting/gcs/validation.js +0 -49
  132. package/dist/lib/reporting/gcs/validation.mjs +0 -25
  133. package/dist/lib/reporting/ledger/change-feed.d.mts +0 -69
  134. package/dist/lib/reporting/ledger/change-feed.d.ts +0 -69
  135. package/dist/lib/reporting/ledger/change-feed.js +0 -107
  136. package/dist/lib/reporting/ledger/change-feed.mjs +0 -80
  137. package/dist/lib/reporting/ledger/managed-postgres.d.mts +0 -293
  138. package/dist/lib/reporting/ledger/managed-postgres.d.ts +0 -293
  139. package/dist/lib/reporting/ledger/managed-postgres.js +0 -2346
  140. package/dist/lib/reporting/ledger/managed-postgres.mjs +0 -2332
  141. package/dist/lib/reporting/ledger/managed-router.d.mts +0 -148
  142. package/dist/lib/reporting/ledger/managed-router.d.ts +0 -148
  143. package/dist/lib/reporting/ledger/managed-router.js +0 -280
  144. package/dist/lib/reporting/ledger/managed-router.mjs +0 -258
  145. package/dist/lib/reporting/ledger/managed.d.mts +0 -535
  146. package/dist/lib/reporting/ledger/managed.d.ts +0 -535
  147. package/dist/lib/reporting/ledger/managed.js +0 -761
  148. package/dist/lib/reporting/ledger/managed.mjs +0 -729
  149. package/dist/lib/reporting/ledger/notification-activity.d.mts +0 -332
  150. package/dist/lib/reporting/ledger/notification-activity.d.ts +0 -332
  151. package/dist/lib/reporting/ledger/notification-activity.js +0 -1366
  152. package/dist/lib/reporting/ledger/notification-activity.mjs +0 -1337
  153. package/dist/lib/reporting/ledger/object-writes.d.mts +0 -128
  154. package/dist/lib/reporting/ledger/object-writes.d.ts +0 -128
  155. package/dist/lib/reporting/ledger/object-writes.js +0 -147
  156. package/dist/lib/reporting/ledger/object-writes.mjs +0 -114
  157. package/dist/lib/reporting/ledger/postgres.d.mts +0 -689
  158. package/dist/lib/reporting/ledger/postgres.d.ts +0 -689
  159. package/dist/lib/reporting/ledger/postgres.js +0 -4462
  160. package/dist/lib/reporting/ledger/postgres.mjs +0 -4462
  161. package/dist/lib/reporting/ledger/row-storage-migration.d.mts +0 -59
  162. package/dist/lib/reporting/ledger/row-storage-migration.d.ts +0 -59
  163. package/dist/lib/reporting/ledger/row-storage-migration.js +0 -365
  164. package/dist/lib/reporting/ledger/row-storage-migration.mjs +0 -339
  165. package/dist/lib/reporting/ledger/row-storage-object.d.mts +0 -203
  166. package/dist/lib/reporting/ledger/row-storage-object.d.ts +0 -203
  167. package/dist/lib/reporting/ledger/row-storage-object.js +0 -379
  168. package/dist/lib/reporting/ledger/row-storage-object.mjs +0 -335
  169. package/dist/lib/reporting/ledger/row-storage-provider-io.d.mts +0 -44
  170. package/dist/lib/reporting/ledger/row-storage-provider-io.d.ts +0 -44
  171. package/dist/lib/reporting/ledger/row-storage-provider-io.js +0 -142
  172. package/dist/lib/reporting/ledger/row-storage-provider-io.mjs +0 -111
  173. package/dist/lib/reporting/ledger/row-storage.d.mts +0 -316
  174. package/dist/lib/reporting/ledger/row-storage.d.ts +0 -316
  175. package/dist/lib/reporting/ledger/row-storage.js +0 -812
  176. package/dist/lib/reporting/ledger/row-storage.mjs +0 -802
  177. package/dist/lib/reporting/s3/index.d.mts +0 -34
  178. package/dist/lib/reporting/s3/index.d.ts +0 -34
  179. package/dist/lib/reporting/s3/index.js +0 -347
  180. package/dist/lib/reporting/s3/index.mjs +0 -340
  181. package/dist/lib/reporting/service/maintenance.d.mts +0 -148
  182. package/dist/lib/reporting/service/maintenance.d.ts +0 -148
  183. package/dist/lib/reporting/service/maintenance.js +0 -185
  184. package/dist/lib/reporting/service/maintenance.mjs +0 -159
  185. package/examples/reliable-reporting-service/README.md +0 -169
@@ -1,1366 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var notification_activity_exports = {};
20
- __export(notification_activity_exports, {
21
- REPORTING_NOTIFICATION_ACTIVITY_MIGRATION: () => REPORTING_NOTIFICATION_ACTIVITY_MIGRATION,
22
- ReportingNotificationRetryableSuppressionError: () => ReportingNotificationRetryableSuppressionError,
23
- createPostgresReportingNotificationActivityRuntime: () => createPostgresReportingNotificationActivityRuntime,
24
- createPostgresReportingNotificationAttemptCheckpoint: () => createPostgresReportingNotificationAttemptCheckpoint,
25
- getReportingNotificationActivityMigration: () => getReportingNotificationActivityMigration,
26
- recipientTableName: () => recipientTableName
27
- });
28
- module.exports = __toCommonJS(notification_activity_exports);
29
- var import_node_crypto = require("node:crypto");
30
- var import_notification_subscriptions = require('../../server/notification-subscriptions/index.js');
31
- var import_jcs = require('../../utils/jcs.js');
32
- const DEFAULT_TABLE = "adcp_reporting_notification_activity";
33
- const REPORTING_STATUS_EVENT_TYPE = "reporting.status_changed";
34
- const REPORTING_LEDGER_EVENT_TYPE = "reporting.ledger_changed";
35
- const REPORTING_DELIVERY_EVENT_TYPE = "reporting.delivery_ready";
36
- const REPORTING_EVENT_STORAGE_PREFIX = "reporting-event:";
37
- const DEFAULT_NAMESPACE = "adcp-reporting";
38
- const DEFAULT_RETENTION_MS = 90 * 24 * 60 * 60 * 1e3;
39
- const DEFAULT_MAX_PENDING_PER_TENANT = 1e5;
40
- const DEFAULT_MAX_ATTEMPTS = 100;
41
- const MAX_ACTIVITY_BYTES = 64 * 1024;
42
- const MAX_CURSOR_BYTES = 16 * 1024;
43
- class ReportingNotificationRetryableSuppressionError extends Error {
44
- constructor(reason) {
45
- super(`Reporting notification delivery authority failed closed (${reason}); no external attempt was made`);
46
- this.reason = reason;
47
- }
48
- reason;
49
- name = "ReportingNotificationRetryableSuppressionError";
50
- }
51
- const MAX_SUPPORTED_RECIPIENTS = 1e4;
52
- const MAX_SUPPORTED_RETAINED_RECIPIENTS = 1e5;
53
- const MAX_BTREE_ENTRY_BYTES = 2704;
54
- const RECIPIENT_FINGERPRINT_BYTES = 64;
55
- const RECIPIENT_ROUND_BYTES = 4;
56
- const MAX_TRANSITION_ID_BYTES = 512;
57
- const MAX_RECIPIENT_TABLE_BYTES = 53;
58
- function getReportingNotificationActivityMigration(options = {}) {
59
- const raw = options.tableName ?? DEFAULT_TABLE;
60
- const table = quoteIdentifier(raw);
61
- const rawRecipients = recipientTableName(raw);
62
- const recipientTable = quoteIdentifier(rawRecipients, MAX_RECIPIENT_TABLE_BYTES);
63
- const cursorTable = quoteIdentifier(`${raw}_cursor`, MAX_RECIPIENT_TABLE_BYTES);
64
- return `
65
- CREATE TABLE IF NOT EXISTS ${table} (
66
- namespace TEXT NOT NULL,
67
- transition_id TEXT NOT NULL,
68
- activity_sequence BIGSERIAL NOT NULL UNIQUE,
69
- tenant_scope TEXT NOT NULL,
70
- account_id TEXT NOT NULL,
71
- obligation_id TEXT NOT NULL,
72
- activity JSONB NOT NULL,
73
- intent_fingerprint TEXT NOT NULL,
74
- state TEXT NOT NULL DEFAULT 'pending',
75
- notification_required BOOLEAN NOT NULL,
76
- attempt_count INTEGER NOT NULL DEFAULT 0,
77
- next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
78
- lease_owner TEXT,
79
- lease_version BIGINT NOT NULL DEFAULT 0,
80
- lease_expires_at TIMESTAMPTZ,
81
- created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
82
- projected_at TIMESTAMPTZ,
83
- retain_until TIMESTAMPTZ,
84
- delivery_intent_at TIMESTAMPTZ,
85
- abandoned_at TIMESTAMPTZ,
86
- PRIMARY KEY (namespace, transition_id),
87
- CONSTRAINT ${raw}_valid_state CHECK (state IN ('pending', 'projected', 'abandoned')),
88
- CONSTRAINT ${raw}_valid_fingerprint CHECK (intent_fingerprint ~ '^[a-f0-9]{64}$'),
89
- CONSTRAINT ${raw}_valid_activity CHECK (jsonb_typeof(activity) = 'object'),
90
- CONSTRAINT ${raw}_valid_projection CHECK (
91
- (state = 'pending' AND notification_required AND projected_at IS NULL AND retain_until IS NULL) OR
92
- (state = 'projected' AND projected_at IS NOT NULL AND retain_until IS NOT NULL) OR
93
- -- Abandoned: bounded out of the pending set without ever being recorded as
94
- -- delivered, so it stops consuming tenant capacity but stays auditable.
95
- (state = 'abandoned' AND projected_at IS NULL AND retain_until IS NOT NULL AND abandoned_at IS NOT NULL)
96
- )
97
- );
98
-
99
- CREATE TABLE IF NOT EXISTS ${cursorTable} (
100
- namespace TEXT PRIMARY KEY,
101
- tenant_scope TEXT,
102
- changed_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
103
- );
104
-
105
- -- Upgrade in place, once, and touch nothing on a rerun.
106
- --
107
- -- Every statement here is guarded on the catalog, including the column adds:
108
- -- \`ADD COLUMN IF NOT EXISTS\` still takes ACCESS EXCLUSIVE to discover the
109
- -- column already exists, so a rerun during normal traffic blocks behind ordinary
110
- -- readers and fails outright under a lock_timeout. An already-upgraded rerun now
111
- -- issues no table-locking statement at all.
112
- --
113
- -- Every guard is scoped to this schema's table by resolving it once to a
114
- -- regclass. A database-wide lookup by index name would see an upgraded index in
115
- -- another schema and skip the upgrade here, leaving that deployment with a
116
- -- projected-only retention index that cannot serve abandonment pruning.
117
- DO $$
118
- DECLARE
119
- activity_table regclass := to_regclass('${raw}');
120
- stale_index oid;
121
- BEGIN
122
- IF activity_table IS NULL THEN
123
- RETURN;
124
- END IF;
125
- IF NOT EXISTS (
126
- SELECT 1 FROM pg_attribute
127
- WHERE attrelid = activity_table AND attname = 'delivery_intent_at' AND NOT attisdropped
128
- ) THEN
129
- ALTER TABLE ${table} ADD COLUMN delivery_intent_at TIMESTAMPTZ;
130
- END IF;
131
- IF NOT EXISTS (
132
- SELECT 1 FROM pg_attribute
133
- WHERE attrelid = activity_table AND attname = 'abandoned_at' AND NOT attisdropped
134
- ) THEN
135
- ALTER TABLE ${table} ADD COLUMN abandoned_at TIMESTAMPTZ;
136
- END IF;
137
- IF NOT EXISTS (
138
- SELECT 1 FROM pg_constraint
139
- WHERE conrelid = activity_table AND conname = '${raw}_valid_state'
140
- AND pg_get_constraintdef(oid) LIKE '%abandoned%'
141
- ) THEN
142
- ALTER TABLE ${table} DROP CONSTRAINT IF EXISTS ${raw}_valid_state;
143
- ALTER TABLE ${table} ADD CONSTRAINT ${raw}_valid_state
144
- CHECK (state IN ('pending', 'projected', 'abandoned')) NOT VALID;
145
- ALTER TABLE ${table} VALIDATE CONSTRAINT ${raw}_valid_state;
146
- END IF;
147
- IF NOT EXISTS (
148
- SELECT 1 FROM pg_constraint
149
- WHERE conrelid = activity_table AND conname = '${raw}_valid_projection'
150
- AND pg_get_constraintdef(oid) LIKE '%abandoned_at%'
151
- ) THEN
152
- ALTER TABLE ${table} DROP CONSTRAINT IF EXISTS ${raw}_valid_projection;
153
- ALTER TABLE ${table} ADD CONSTRAINT ${raw}_valid_projection CHECK (
154
- (state = 'pending' AND notification_required AND projected_at IS NULL AND retain_until IS NULL) OR
155
- (state = 'projected' AND projected_at IS NOT NULL AND retain_until IS NOT NULL) OR
156
- (state = 'abandoned' AND projected_at IS NULL AND retain_until IS NOT NULL AND abandoned_at IS NOT NULL)
157
- ) NOT VALID;
158
- ALTER TABLE ${table} VALIDATE CONSTRAINT ${raw}_valid_projection;
159
- END IF;
160
- SELECT index_class.oid INTO stale_index
161
- FROM pg_index
162
- JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid
163
- WHERE pg_index.indrelid = activity_table
164
- AND index_class.relname = 'idx_${raw}_retention'
165
- AND pg_get_indexdef(pg_index.indexrelid) NOT LIKE '%abandoned%';
166
- IF stale_index IS NOT NULL THEN
167
- EXECUTE format('DROP INDEX %s', stale_index::regclass);
168
- END IF;
169
- IF NOT EXISTS (
170
- SELECT 1 FROM pg_index
171
- JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid
172
- WHERE pg_index.indrelid = activity_table AND index_class.relname = 'idx_${raw}_retention'
173
- ) THEN
174
- CREATE INDEX idx_${raw}_retention
175
- ON ${table}(namespace, retain_until, activity_sequence)
176
- WHERE state IN ('projected', 'abandoned');
177
- END IF;
178
- END $$;
179
-
180
- -- One row per frozen recipient, carrying that recipient's own delivery state.
181
- --
182
- -- Relational rather than one serialized document: a document needs a size cap,
183
- -- and a fanout that legitimately exceeded it could never be committed and would
184
- -- retry until it aged out. Only the bounded 64-hex fingerprint enters the index,
185
- -- so an individual recipient reference has no length limit of its own.
186
- --
187
- -- Per-recipient rather than per-emission: attempt_at is the durable pre-POST
188
- -- checkpoint for exactly one recipient, so one recipient's attempt cannot pin a
189
- -- sibling that was suppressed before its own first POST. Rows with no
190
- -- attempt_at are revisable and replaced in place, so a pre-attempt retry can
191
- -- never accumulate superseded state.
192
- --
193
- -- subscriber_key identifies the subscriber independently of its destination
194
- -- generation. Pinning is per subscriber: once a subscriber has been addressed,
195
- -- a later generation of the same subscriber is never addressed for this
196
- -- notification, because that would be the same logical delivery under a new
197
- -- idempotency key. A different subscriber is unaffected.
198
- CREATE TABLE IF NOT EXISTS ${recipientTable} (
199
- namespace TEXT NOT NULL,
200
- transition_id TEXT NOT NULL,
201
- recipient_fingerprint TEXT NOT NULL,
202
- subscriber_key TEXT NOT NULL,
203
- recipient JSONB NOT NULL,
204
- frozen_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
205
- attempt_at TIMESTAMPTZ,
206
- settled_at TIMESTAMPTZ,
207
- disposition TEXT,
208
- PRIMARY KEY (namespace, transition_id, recipient_fingerprint),
209
- CONSTRAINT ${rawRecipients}_fk FOREIGN KEY (namespace, transition_id)
210
- REFERENCES ${table}(namespace, transition_id) ON DELETE CASCADE,
211
- CONSTRAINT ${rawRecipients}_fp CHECK (recipient_fingerprint ~ '^[a-f0-9]{64}$'),
212
- CONSTRAINT ${rawRecipients}_sk CHECK (subscriber_key ~ '^[a-f0-9]{64}$'),
213
- CONSTRAINT ${rawRecipients}_obj CHECK (jsonb_typeof(recipient) = 'object'),
214
- CONSTRAINT ${rawRecipients}_set CHECK (
215
- (settled_at IS NULL AND disposition IS NULL) OR
216
- (settled_at IS NOT NULL AND disposition IN ('delivered', 'terminal'))
217
- )
218
- );
219
- -- Indexes are catalog-guarded too. CREATE INDEX IF NOT EXISTS still takes a
220
- -- ShareLock to discover the index already exists, which conflicts with the
221
- -- RowExclusiveLock every ordinary writer holds \u2014 so an already-current rerun
222
- -- would stall live traffic, or fail under a lock_timeout, despite creating
223
- -- nothing. Scoped to this schema's tables by regclass, like every other guard.
224
- DO $$
225
- DECLARE
226
- activity_table regclass := to_regclass('${raw}');
227
- recipient_table regclass := to_regclass('${rawRecipients}');
228
- BEGIN
229
- IF activity_table IS NULL OR recipient_table IS NULL THEN
230
- RETURN;
231
- END IF;
232
- IF NOT EXISTS (
233
- SELECT 1 FROM pg_index
234
- JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid
235
- WHERE pg_index.indrelid = recipient_table AND index_class.relname = 'idx_${rawRecipients}_unsettled'
236
- ) THEN
237
- CREATE INDEX idx_${rawRecipients}_unsettled
238
- ON ${recipientTable}(namespace, transition_id)
239
- WHERE settled_at IS NULL;
240
- END IF;
241
- -- At most one addressed generation per subscriber, enforced by the database.
242
- --
243
- -- The checkpoint and a concurrent recipient replacement run as separate
244
- -- statements against a connection pool, so neither sees the other's
245
- -- uncommitted work: a freeze can propose a replacement generation while the
246
- -- original is being checkpointed, and PostgreSQL will keep both rows. This
247
- -- index is the serialization point. The second generation's checkpoint fails,
248
- -- so it is never POSTed, and the next freeze drops it because its subscriber
249
- -- is already claimed. One logical delivery, one idempotency key.
250
- IF NOT EXISTS (
251
- SELECT 1 FROM pg_index
252
- JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid
253
- WHERE pg_index.indrelid = recipient_table
254
- AND index_class.relname = 'idx_${rawRecipients}_attempted_subscriber'
255
- ) THEN
256
- CREATE UNIQUE INDEX idx_${rawRecipients}_attempted_subscriber
257
- ON ${recipientTable}(namespace, transition_id, subscriber_key)
258
- WHERE attempt_at IS NOT NULL;
259
- END IF;
260
- IF NOT EXISTS (
261
- SELECT 1 FROM pg_index
262
- JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid
263
- WHERE pg_index.indrelid = activity_table AND index_class.relname = 'idx_${raw}_pending'
264
- ) THEN
265
- CREATE INDEX idx_${raw}_pending
266
- ON ${table}(namespace, next_attempt_at, lease_expires_at, activity_sequence)
267
- WHERE state = 'pending';
268
- END IF;
269
- IF NOT EXISTS (
270
- SELECT 1 FROM pg_index
271
- JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid
272
- WHERE pg_index.indrelid = activity_table AND index_class.relname = 'idx_${raw}_pending_tenant'
273
- ) THEN
274
- CREATE INDEX idx_${raw}_pending_tenant
275
- ON ${table}(namespace, tenant_scope)
276
- WHERE state = 'pending';
277
- END IF;
278
- IF NOT EXISTS (
279
- SELECT 1 FROM pg_index
280
- JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid
281
- WHERE pg_index.indrelid = activity_table AND index_class.relname = 'idx_${raw}_pending_tenant_due'
282
- ) THEN
283
- CREATE INDEX idx_${raw}_pending_tenant_due
284
- ON ${table}(namespace, tenant_scope, next_attempt_at, lease_expires_at)
285
- WHERE state = 'pending';
286
- END IF;
287
- IF NOT EXISTS (
288
- SELECT 1 FROM pg_index
289
- JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid
290
- WHERE pg_index.indrelid = activity_table AND index_class.relname = 'idx_${raw}_account_activity'
291
- ) THEN
292
- CREATE INDEX idx_${raw}_account_activity
293
- ON ${table}(namespace, tenant_scope, account_id, activity_sequence DESC);
294
- END IF;
295
- END $$;
296
- `.trim();
297
- }
298
- function recipientTableName(tableName = DEFAULT_TABLE) {
299
- return `${tableName}_recipients`;
300
- }
301
- const REPORTING_NOTIFICATION_ACTIVITY_MIGRATION = getReportingNotificationActivityMigration();
302
- function createPostgresReportingNotificationAttemptCheckpoint(options) {
303
- if (!options?.db || typeof options.db.query !== "function") {
304
- throw new TypeError("createPostgresReportingNotificationAttemptCheckpoint requires a PostgreSQL queryable");
305
- }
306
- const namespace = options.namespace ?? DEFAULT_NAMESPACE;
307
- assertIdentifier(namespace, "namespace", 255);
308
- const rawTable = options.tableName ?? DEFAULT_TABLE;
309
- const recipientTable = quoteIdentifier(recipientTableName(rawTable), MAX_RECIPIENT_TABLE_BYTES);
310
- const owned = /* @__PURE__ */ new Set([
311
- REPORTING_STATUS_EVENT_TYPE,
312
- REPORTING_LEDGER_EVENT_TYPE,
313
- REPORTING_DELIVERY_EVENT_TYPE,
314
- ...options.eventTypes ?? []
315
- ]);
316
- const checkpoint = async (input) => {
317
- if (!owned.has(input.eventType)) return;
318
- const fingerprint = recipientFingerprint({
319
- scope: input.scope,
320
- subscriberId: input.subscriberId,
321
- destinationGeneration: input.destinationGeneration
322
- });
323
- const marked = await reportingActivityDatabaseOperation(
324
- "Reporting notification delivery attempt could not be checkpointed",
325
- () => options.db.query(
326
- `UPDATE ${recipientTable} SET attempt_at = COALESCE(attempt_at, clock_timestamp())
327
- WHERE namespace = $1 AND transition_id = $2 AND recipient_fingerprint = $3`,
328
- [namespace, input.notificationId, fingerprint]
329
- )
330
- );
331
- if (marked.rowCount !== 1) {
332
- throw new Error(
333
- "Reporting notification delivery attempt has no frozen recipient to checkpoint; the emission owner must freeze its recipient set before any external attempt"
334
- );
335
- }
336
- };
337
- return Object.assign(checkpoint, {
338
- activityStore: Object.freeze({ db: options.db, namespace, tableName: rawTable })
339
- });
340
- }
341
- function createPostgresReportingNotificationActivityRuntime(options) {
342
- if (!options?.db || typeof options.db.query !== "function") {
343
- throw new TypeError("createPostgresReportingNotificationActivityRuntime requires a PostgreSQL queryable");
344
- }
345
- if (!options.notifications || typeof options.notifications.emit !== "function") {
346
- throw new TypeError(
347
- "createPostgresReportingNotificationActivityRuntime requires the persistent notification runtime"
348
- );
349
- }
350
- if (typeof options.tenantScopeForAccount !== "function") {
351
- throw new TypeError("tenantScopeForAccount must be a function");
352
- }
353
- if (options.notifications.hasDeliveryAttemptCheckpoint !== true && !options.acknowledgeMissingAttemptCheckpoint) {
354
- throw new TypeError(
355
- "createPostgresReportingNotificationActivityRuntime requires a notification port that runs the durable pre-POST attempt checkpoint. Build createPostgresReportingNotificationAttemptCheckpoint() and pass it as checkpointDeliveryAttempt; a custom port must set hasDeliveryAttemptCheckpoint: true to prove it forwards the event it is given."
356
- );
357
- }
358
- const namespace = options.namespace ?? DEFAULT_NAMESPACE;
359
- if (!options.attemptCheckpoint && !options.acknowledgeMissingAttemptCheckpoint) {
360
- throw new TypeError(
361
- "createPostgresReportingNotificationActivityRuntime requires attemptCheckpoint: pass the very checkpoint wired into the notification runtime so its durable store can be verified against this one."
362
- );
363
- }
364
- const wired = options.notifications.deliveryAttemptCheckpoint;
365
- if (wired !== void 0 && options.attemptCheckpoint && wired !== options.attemptCheckpoint) {
366
- throw new TypeError(
367
- "createPostgresReportingNotificationActivityRuntime: the notification runtime invokes a different checkpoint than the one passed as attemptCheckpoint. Each can be correctly built and still target a different store, in which case every delivery checkpoints nothing and is abandoned undelivered. Pass the very object wired into checkpointDeliveryAttempt."
368
- );
369
- }
370
- if (options.attemptCheckpoint && wired === void 0 && !options.attemptCheckpoint.activityStore) {
371
- throw new TypeError(
372
- "createPostgresReportingNotificationActivityRuntime: attemptCheckpoint declares no durable store and the notification port does not expose the checkpoint it invokes, so neither its identity nor its target can be verified. Build it with createPostgresReportingNotificationAttemptCheckpoint()."
373
- );
374
- }
375
- if (options.attemptCheckpoint?.activityStore) {
376
- const bound = options.attemptCheckpoint.activityStore;
377
- const mismatch = bound.db !== options.db || bound.namespace !== namespace || bound.tableName !== (options.tableName ?? DEFAULT_TABLE);
378
- if (mismatch) {
379
- throw new TypeError(
380
- `createPostgresReportingNotificationActivityRuntime: attemptCheckpoint is bound to a different durable store (namespace ${JSON.stringify(bound.namespace)}, table ${JSON.stringify(bound.tableName)}${bound.db === options.db ? "" : ", different queryable"}) than this runtime (namespace ${JSON.stringify(namespace)}, table ${JSON.stringify(options.tableName ?? DEFAULT_TABLE)}). A mismatched pair checkpoints nothing, so every delivery is suppressed until its binding retires and the notification is lost silently.`
381
- );
382
- }
383
- }
384
- const development = process.env.NODE_ENV === "test" || process.env.NODE_ENV === "development";
385
- if (!development && options.namespace === void 0 && !options.acknowledgeIsolatedDatabase) {
386
- throw new TypeError(
387
- "Production reporting notification activity requires an explicit deployment namespace or acknowledgeIsolatedDatabase: true"
388
- );
389
- }
390
- assertIdentifier(namespace, "namespace", 255);
391
- const rawTable = options.tableName ?? DEFAULT_TABLE;
392
- const table = quoteIdentifier(rawTable);
393
- const retentionMs = options.retentionMs ?? DEFAULT_RETENTION_MS;
394
- positiveInteger(retentionMs, "retentionMs");
395
- const maxPendingPerTenant = options.maxPendingPerTenant ?? DEFAULT_MAX_PENDING_PER_TENANT;
396
- boundedInteger(maxPendingPerTenant, "maxPendingPerTenant", 1, 1e6);
397
- const maxRecipients = options.maxRecipients ?? MAX_SUPPORTED_RECIPIENTS;
398
- boundedInteger(maxRecipients, "maxRecipients", 1, MAX_SUPPORTED_RECIPIENTS);
399
- const maxRetainedRecipients = options.maxRetainedRecipients ?? maxRecipients * 2;
400
- boundedInteger(maxRetainedRecipients, "maxRetainedRecipients", 1, MAX_SUPPORTED_RETAINED_RECIPIENTS);
401
- if (maxRetainedRecipients < maxRecipients) {
402
- throw new TypeError(
403
- `maxRetainedRecipients ${maxRetainedRecipients} is below maxRecipients ${maxRecipients}; a full fanout could then never be retained and every claim at that size would abandon undelivered`
404
- );
405
- }
406
- const maxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
407
- boundedInteger(maxAttempts, "maxAttempts", 1, 1e5);
408
- const recipientRawTable = recipientTableName(rawTable);
409
- const recipientTable = quoteIdentifier(recipientRawTable, MAX_RECIPIENT_TABLE_BYTES);
410
- const cursorTable = quoteIdentifier(`${rawTable}_cursor`, MAX_RECIPIENT_TABLE_BYTES);
411
- const worstCaseIndexBytes = Buffer.byteLength(namespace, "utf8") + MAX_TRANSITION_ID_BYTES + RECIPIENT_ROUND_BYTES + RECIPIENT_FINGERPRINT_BYTES;
412
- if (worstCaseIndexBytes > MAX_BTREE_ENTRY_BYTES) {
413
- throw new TypeError(
414
- `Reporting notification activity namespace is too long to index recipient intent (${worstCaseIndexBytes} of ${MAX_BTREE_ENTRY_BYTES} bytes); shorten namespace`
415
- );
416
- }
417
- const persistActivity = async (transaction, input) => {
418
- const { activity, eventId, notificationRequired, obligation } = input;
419
- const persistenceEventId = activity.activityType === "reporting.lifecycle_changed" ? eventId.startsWith(REPORTING_EVENT_STORAGE_PREFIX) || Buffer.byteLength(eventId, "utf8") > 255 || !/^[A-Za-z0-9_.:-]+$/.test(eventId) ? `${REPORTING_EVENT_STORAGE_PREFIX}${(0, import_jcs.canonicalJsonSha256)({ kind: "lifecycle", eventId })}` : eventId : `${REPORTING_EVENT_STORAGE_PREFIX}${(0, import_jcs.canonicalJsonSha256)({
420
- kind: activity.activityType,
421
- changeKind: activity.activityType === "reporting.ledger_changed" ? activity.changeKind : "ready",
422
- eventId
423
- })}`;
424
- const accountId = obligation.account.account_id;
425
- const tenantId = activity.tenantId;
426
- if (Buffer.byteLength(JSON.stringify(activity), "utf8") > MAX_ACTIVITY_BYTES) {
427
- throw new RangeError("Reporting notification/activity intent exceeds 64 KiB");
428
- }
429
- const fingerprint = (0, import_jcs.canonicalJsonSha256)(activity);
430
- if (notificationRequired) {
431
- await reportingActivityDatabaseOperation("Reporting notification/activity capacity check failed", async () => {
432
- await transaction.query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", [
433
- `adcp-reporting-activity-cap:${namespace}:${tenantId}`
434
- ]);
435
- const existing = await transaction.query(
436
- `SELECT intent_fingerprint, tenant_scope, account_id, obligation_id
437
- FROM ${table}
438
- WHERE namespace = $1 AND transition_id = $2`,
439
- [namespace, persistenceEventId]
440
- );
441
- const existingIntent = existing.rows[0];
442
- if (existingIntent) {
443
- if (existingIntent.intent_fingerprint !== fingerprint || existingIntent.tenant_scope !== tenantId || existingIntent.account_id !== accountId || existingIntent.obligation_id !== obligation.reporting_obligation_id) {
444
- throw new Error("Reporting event identity conflicts with existing notification/activity intent");
445
- }
446
- return;
447
- }
448
- const pending = await transaction.query(
449
- `SELECT COUNT(*)::integer AS count
450
- FROM (
451
- SELECT 1 FROM ${table}
452
- WHERE namespace = $1 AND tenant_scope = $2 AND state = 'pending'
453
- LIMIT $3
454
- ) bounded_pending`,
455
- [namespace, tenantId, maxPendingPerTenant]
456
- );
457
- if ((pending.rows[0]?.count ?? 0) >= maxPendingPerTenant) {
458
- throw new Error("Reporting notification activity pending capacity reached; recovery must catch up");
459
- }
460
- });
461
- }
462
- let write;
463
- try {
464
- write = await transaction.query(
465
- `INSERT INTO ${table} (
466
- namespace, transition_id, tenant_scope, account_id, obligation_id,
467
- activity, intent_fingerprint, notification_required, state,
468
- projected_at, retain_until
469
- ) VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8,
470
- CASE WHEN $8 THEN 'pending' ELSE 'projected' END,
471
- CASE WHEN $8 THEN NULL ELSE clock_timestamp() END,
472
- CASE WHEN $8 THEN NULL ELSE clock_timestamp() + ($9::bigint * INTERVAL '1 millisecond') END)
473
- ON CONFLICT (namespace, transition_id) DO UPDATE SET
474
- transition_id = EXCLUDED.transition_id
475
- WHERE ${table}.intent_fingerprint = EXCLUDED.intent_fingerprint
476
- AND ${table}.tenant_scope = EXCLUDED.tenant_scope
477
- AND ${table}.account_id = EXCLUDED.account_id
478
- AND ${table}.obligation_id = EXCLUDED.obligation_id
479
- RETURNING transition_id`,
480
- [
481
- namespace,
482
- persistenceEventId,
483
- tenantId,
484
- accountId,
485
- obligation.reporting_obligation_id,
486
- JSON.stringify(activity),
487
- fingerprint,
488
- notificationRequired,
489
- retentionMs
490
- ]
491
- );
492
- } catch (cause) {
493
- if (isPostgresUndefinedTable(cause)) {
494
- throw new Error(
495
- "Reporting notification/activity persistence failed: run getReportingNotificationActivityMigration() before serving",
496
- { cause }
497
- );
498
- }
499
- throw new Error("Reporting notification/activity persistence failed", { cause });
500
- }
501
- if (write.rowCount !== 1) {
502
- throw new Error("Reporting event identity conflicts with existing notification/activity intent");
503
- }
504
- };
505
- const activityScope = (transaction, obligation, eventId) => {
506
- if (!transaction || typeof transaction.query !== "function") {
507
- throw new TypeError("Reporting notification/activity persistence requires the active ledger transaction");
508
- }
509
- const accountId = obligation.account.account_id;
510
- assertIdentifier(accountId, "accountId", 512);
511
- assertIdentifier(eventId, "eventId", MAX_TRANSITION_ID_BYTES);
512
- const tenantId = options.tenantScopeForAccount(accountId);
513
- if (isPromiseLike(tenantId)) {
514
- throw new TypeError("tenantScopeForAccount must be synchronous and side-effect free");
515
- }
516
- assertIdentifier(tenantId, "tenantId", 512);
517
- return { accountId, tenantId };
518
- };
519
- const databaseNow = async (transaction) => {
520
- const result = await transaction.query("SELECT clock_timestamp() AS now");
521
- if (!result.rows[0]) throw new Error("Reporting notification/activity database clock is unavailable");
522
- return asIso(result.rows[0].now);
523
- };
524
- const port = {
525
- async recordTransition(input, transaction) {
526
- const { tenantId } = activityScope(transaction, input.obligation, input.transition.transitionId);
527
- if (input.transition.reporting_obligation_id !== input.obligation.reporting_obligation_id) {
528
- throw new Error("Reporting transition and authoritative obligation identities disagree");
529
- }
530
- const activity = buildActivity(namespace, tenantId, input.transition, input.obligation);
531
- const notificationRequired = input.transition.previousHealth !== input.transition.health;
532
- await persistActivity(transaction, {
533
- eventId: input.transition.transitionId,
534
- obligation: input.obligation,
535
- activity,
536
- notificationRequired
537
- });
538
- },
539
- async recordLedgerChanged(input, transaction) {
540
- const record = "revision" in input ? input.revision : input.adjustment;
541
- const eventId = "revision" in input ? input.revision.reporting_revision_id : input.adjustment.reporting_adjustment_id;
542
- const { tenantId } = activityScope(transaction, input.obligation, eventId);
543
- if (record.reporting_obligation_id !== input.obligation.reporting_obligation_id) {
544
- throw new Error("Reporting ledger record and authoritative obligation identities disagree");
545
- }
546
- const occurredAt = await databaseNow(transaction);
547
- const activity = buildLedgerChangedActivity(namespace, tenantId, occurredAt, input);
548
- await persistActivity(transaction, {
549
- eventId,
550
- obligation: input.obligation,
551
- activity,
552
- notificationRequired: true
553
- });
554
- },
555
- async recordDeliveryReady(input, transaction) {
556
- const eventId = input.materialization.reporting_materialization_id;
557
- const { tenantId } = activityScope(transaction, input.obligation, eventId);
558
- if (input.revision.reporting_obligation_id !== input.obligation.reporting_obligation_id || input.materialization.reporting_revision_id !== input.revision.reporting_revision_id) {
559
- throw new Error("Reporting delivery-ready records do not share one authoritative ledger identity");
560
- }
561
- if (input.materialization.status !== "available" && input.materialization.status !== "delivered") {
562
- throw new Error("Reporting delivery-ready activity requires a successful materialization");
563
- }
564
- const occurredAt = await databaseNow(transaction);
565
- const activity = buildDeliveryReadyActivity(namespace, tenantId, occurredAt, input);
566
- await persistActivity(transaction, {
567
- eventId,
568
- obligation: input.obligation,
569
- activity,
570
- notificationRequired: true
571
- });
572
- }
573
- };
574
- const readActivity = async (input, lifecycleOnly) => {
575
- assertIdentifier(input.tenantId, "tenantId", 512);
576
- assertIdentifier(input.accountId, "accountId", 512);
577
- const expectedTenantId = options.tenantScopeForAccount(input.accountId);
578
- if (isPromiseLike(expectedTenantId)) {
579
- throw new TypeError("tenantScopeForAccount must be synchronous and side-effect free");
580
- }
581
- assertIdentifier(expectedTenantId, "tenantId", 512);
582
- if (expectedTenantId !== input.tenantId) {
583
- throw new TypeError("Reporting account activity scope does not match the trusted account directory");
584
- }
585
- const limit = input.limit ?? 100;
586
- boundedInteger(limit, "limit", 1, 200);
587
- const before = decodeCursor(input.cursor, namespace, input.tenantId, input.accountId);
588
- const result = await reportingActivityDatabaseOperation(
589
- "Reporting account activity read failed",
590
- () => options.db.query(
591
- `SELECT activity, state, created_at, projected_at, abandoned_at, activity_sequence
592
- FROM ${table}
593
- WHERE namespace = $1 AND tenant_scope = $2 AND account_id = $3
594
- AND ($4::bigint IS NULL OR activity_sequence < $4)
595
- AND ($6::boolean = false OR activity->>'activityType' = 'reporting.lifecycle_changed')
596
- ORDER BY activity_sequence DESC
597
- LIMIT $5`,
598
- [namespace, input.tenantId, input.accountId, before ?? null, limit + 1, lifecycleOnly]
599
- )
600
- );
601
- const selected = result.rows.slice(0, limit);
602
- const activities = selected.map((row) => ({
603
- ...structuredClone(row.activity),
604
- recordedAt: asIso(row.created_at),
605
- ...row.projected_at ? { notificationProjectedAt: asIso(row.projected_at) } : {},
606
- ...row.abandoned_at ? { notificationAbandonedAt: asIso(row.abandoned_at) } : {}
607
- }));
608
- const hasMore = result.rows.length > limit;
609
- return {
610
- activities,
611
- hasMore,
612
- ...hasMore && selected.length > 0 ? {
613
- nextCursor: encodeCursor(
614
- namespace,
615
- input.tenantId,
616
- input.accountId,
617
- String(selected[selected.length - 1].activity_sequence)
618
- )
619
- } : {}
620
- };
621
- };
622
- return {
623
- port,
624
- migrations: {
625
- activity: getReportingNotificationActivityMigration({ tableName: rawTable }),
626
- all: [getReportingNotificationActivityMigration({ tableName: rawTable })]
627
- },
628
- async probe() {
629
- if (options.notifications.hasDeliveryAttemptCheckpoint !== true) {
630
- throw new Error(
631
- "Reporting notification/activity probe failed: the notification port does not prove it runs the durable pre-POST attempt checkpoint. Wire createPostgresReportingNotificationAttemptCheckpoint() into checkpointDeliveryAttempt, or set hasDeliveryAttemptCheckpoint: true on a custom port that forwards the event it is given."
632
- );
633
- }
634
- try {
635
- await options.db.query(
636
- `SELECT namespace, transition_id, tenant_scope, account_id, obligation_id,
637
- activity, intent_fingerprint, state, notification_required, lease_owner, lease_version,
638
- lease_expires_at, projected_at, retain_until, delivery_intent_at, abandoned_at
639
- FROM ${table} LIMIT 0`
640
- );
641
- await options.db.query(
642
- `SELECT namespace, transition_id, recipient_fingerprint, recipient, frozen_at,
643
- attempt_at, settled_at, disposition
644
- FROM ${recipientTable} LIMIT 0`
645
- );
646
- await options.db.query(`SELECT namespace, tenant_scope, changed_at FROM ${cursorTable} LIMIT 0`);
647
- } catch (cause) {
648
- throw new Error(
649
- "Reporting notification/activity probe failed: run getReportingNotificationActivityMigration() before serving",
650
- { cause }
651
- );
652
- }
653
- },
654
- async recoverOnce(recoveryOptions = {}) {
655
- const ownerToken = recoveryOptions.ownerToken ?? (0, import_node_crypto.randomUUID)();
656
- assertIdentifier(ownerToken, "ownerToken", 255);
657
- if (ownerToken.length < 8) throw new TypeError("ownerToken must contain at least 8 characters");
658
- const leaseMs = recoveryOptions.leaseMs ?? 6e4;
659
- const limit = recoveryOptions.limit ?? 50;
660
- const retryAfterMs = recoveryOptions.retryAfterMs ?? 3e4;
661
- boundedInteger(leaseMs, "leaseMs", 1e3, 3e5);
662
- boundedInteger(limit, "limit", 1, 1e3);
663
- boundedInteger(retryAfterMs, "retryAfterMs", 1, 6048e5);
664
- const metrics = {
665
- claimed: 0,
666
- matched: 0,
667
- projected: 0,
668
- retried: 0,
669
- leaseLost: 0,
670
- abandoned: 0
671
- };
672
- let emptyTenantClaims = 0;
673
- while (metrics.claimed < limit && emptyTenantClaims < 8) {
674
- recoveryOptions.signal?.throwIfAborted();
675
- const [claim] = await claimPending(options.db, table, cursorTable, namespace, ownerToken, leaseMs, 1);
676
- if (!claim) {
677
- emptyTenantClaims += 1;
678
- continue;
679
- }
680
- emptyTenantClaims = 0;
681
- metrics.claimed += 1;
682
- let leaseLost = false;
683
- let renewing = false;
684
- const heartbeat = setInterval(
685
- () => {
686
- if (renewing || leaseLost) return;
687
- renewing = true;
688
- void renewClaim(options.db, table, namespace, claim, leaseMs).then((renewed) => {
689
- if (!renewed) leaseLost = true;
690
- }).catch(() => {
691
- leaseLost = true;
692
- }).finally(() => {
693
- renewing = false;
694
- });
695
- },
696
- Math.max(250, Math.floor(leaseMs / 3))
697
- );
698
- heartbeat.unref?.();
699
- try {
700
- const notificationType = claim.activity.notificationType;
701
- if (!notificationType) throw new Error("Pending reporting activity has no notification type");
702
- let frozen = [];
703
- let froze = false;
704
- const result = await options.notifications.emit({
705
- emissionId: claim.activity.activityId,
706
- notificationId: claim.transitionId,
707
- notificationType,
708
- anchor: "account",
709
- tenantId: claim.tenantId,
710
- accountId: claim.accountId,
711
- payload: notificationPayload(claim.activity),
712
- // Commit the recipient set under this lease before anything leaves
713
- // the process. Each recipient is revisable until its own durable
714
- // pre-POST checkpoint and pinned afterwards.
715
- freezeRecipients: async (candidates) => {
716
- froze = true;
717
- frozen = await freezeClaimRecipients(
718
- options.db,
719
- table,
720
- recipientTable,
721
- namespace,
722
- claim,
723
- maxRecipients,
724
- maxRetainedRecipients,
725
- candidates
726
- );
727
- return frozen;
728
- }
729
- });
730
- if (!froze) {
731
- throw new Error(
732
- "Reporting notification port did not freeze its recipient set; it must forward the emitted event, including freezeRecipients, to a runtime that honours it. Nothing was projected."
733
- );
734
- }
735
- metrics.matched += result.matched;
736
- const outcomes = result.deliveries.map((delivery) => classifyDelivery(delivery));
737
- const settlement = await settleClaimRecipients(
738
- options.db,
739
- table,
740
- recipientTable,
741
- namespace,
742
- claim,
743
- frozen,
744
- outcomes
745
- );
746
- if (!settlement.settled) {
747
- const retryable = outcomes.find((outcome) => outcome.disposition === "retryable");
748
- throw retryable?.suppression ? new ReportingNotificationRetryableSuppressionError(retryable.suppression) : new Error(
749
- `Reporting notification delivery is not settled for ${settlement.unsettled} recipient(s); retrying`
750
- );
751
- }
752
- const projected = !leaseLost && await projectClaim(options.db, table, namespace, claim, retentionMs);
753
- if (projected) metrics.projected += 1;
754
- else metrics.leaseLost += 1;
755
- } catch (error) {
756
- reportProjectionError(recoveryOptions.onError, error, claim);
757
- if (!leaseLost && claim.attemptCount >= maxAttempts) {
758
- const abandoned = await abandonClaim(options.db, table, namespace, claim, retentionMs);
759
- if (abandoned) metrics.abandoned += 1;
760
- else metrics.leaseLost += 1;
761
- } else {
762
- const released = !leaseLost && await releaseClaim(options.db, table, namespace, claim, retryAfterMs);
763
- if (released) metrics.retried += 1;
764
- else metrics.leaseLost += 1;
765
- }
766
- } finally {
767
- clearInterval(heartbeat);
768
- }
769
- }
770
- return metrics;
771
- },
772
- async listActivity(input) {
773
- return await readActivity(input, true);
774
- },
775
- async listNotificationActivity(input) {
776
- return readActivity(input, false);
777
- },
778
- async pruneProjected(pruneOptions = {}) {
779
- const limit = pruneOptions.limit ?? 1e3;
780
- boundedInteger(limit, "limit", 1, 1e4);
781
- const result = await reportingActivityDatabaseOperation(
782
- "Reporting account activity pruning failed",
783
- () => options.db.query(
784
- `WITH expired AS (
785
- SELECT namespace, transition_id FROM ${table}
786
- WHERE namespace = $1 AND state IN ('projected', 'abandoned')
787
- AND retain_until <= clock_timestamp()
788
- ORDER BY retain_until, activity_sequence
789
- FOR UPDATE SKIP LOCKED LIMIT $2
790
- )
791
- DELETE FROM ${table} target USING expired
792
- WHERE target.namespace = expired.namespace AND target.transition_id = expired.transition_id`,
793
- [namespace, limit]
794
- )
795
- );
796
- return result.rowCount ?? 0;
797
- }
798
- };
799
- }
800
- function buildActivity(namespace, tenantId, transition, obligation) {
801
- const identity = (0, import_jcs.canonicalJsonSha256)({
802
- namespace,
803
- tenantId,
804
- accountId: obligation.account.account_id,
805
- transitionId: transition.transitionId
806
- });
807
- return {
808
- activityId: `ract_${identity.slice(0, 32)}`,
809
- transitionId: transition.transitionId,
810
- activityType: "reporting.lifecycle_changed",
811
- ...transition.previousHealth === transition.health ? {} : { notificationType: "reporting.status_changed" },
812
- tenantId,
813
- accountId: obligation.account.account_id,
814
- reporting_obligation_id: obligation.reporting_obligation_id,
815
- previousHealth: transition.previousHealth,
816
- health: transition.health,
817
- previousFinality: transition.previousFinality ?? "none",
818
- finality: transition.finality ?? "none",
819
- issueIds: [...transition.issueIds],
820
- occurredAt: transition.occurredAt,
821
- correlation: {
822
- delivery_config_id: obligation.delivery_config_id,
823
- delivery_config_version: obligation.delivery_config_version,
824
- report_definition_id: obligation.report_definition_id,
825
- feed_purpose: obligation.feedPurpose,
826
- period: { start: obligation.period.start, end: obligation.period.end }
827
- }
828
- };
829
- }
830
- function activityIdentity(namespace, tenantId, accountId, eventId, eventType) {
831
- return `ract_${(0, import_jcs.canonicalJsonSha256)({ namespace, tenantId, accountId, eventId, eventType }).slice(0, 32)}`;
832
- }
833
- function buildLedgerChangedActivity(namespace, tenantId, occurredAt, input) {
834
- const accountId = input.obligation.account.account_id;
835
- if ("revision" in input) {
836
- const eventId2 = input.revision.reporting_revision_id;
837
- return {
838
- activityId: activityIdentity(
839
- namespace,
840
- tenantId,
841
- accountId,
842
- eventId2,
843
- `${REPORTING_LEDGER_EVENT_TYPE}:revision_published`
844
- ),
845
- transitionId: eventId2,
846
- activityType: REPORTING_LEDGER_EVENT_TYPE,
847
- notificationType: REPORTING_LEDGER_EVENT_TYPE,
848
- tenantId,
849
- accountId,
850
- reporting_obligation_id: input.obligation.reporting_obligation_id,
851
- occurredAt,
852
- changeKind: "revision_published",
853
- reporting_revision_id: eventId2,
854
- ...input.revision.supersedes_reporting_revision_id ? { supersedes_reporting_revision_id: input.revision.supersedes_reporting_revision_id } : {},
855
- finality: input.revision.finality
856
- };
857
- }
858
- const eventId = input.adjustment.reporting_adjustment_id;
859
- return {
860
- activityId: activityIdentity(
861
- namespace,
862
- tenantId,
863
- accountId,
864
- eventId,
865
- `${REPORTING_LEDGER_EVENT_TYPE}:adjustment_published`
866
- ),
867
- transitionId: eventId,
868
- activityType: REPORTING_LEDGER_EVENT_TYPE,
869
- notificationType: REPORTING_LEDGER_EVENT_TYPE,
870
- tenantId,
871
- accountId,
872
- reporting_obligation_id: input.obligation.reporting_obligation_id,
873
- occurredAt,
874
- changeKind: "adjustment_published",
875
- reporting_adjustment_id: eventId,
876
- adjusts_reporting_revision_id: input.adjustment.adjusts_reporting_revision_id
877
- };
878
- }
879
- function buildDeliveryReadyActivity(namespace, tenantId, occurredAt, input) {
880
- const accountId = input.obligation.account.account_id;
881
- const eventId = input.materialization.reporting_materialization_id;
882
- const readiness = input.materialization.status;
883
- if (readiness !== "available" && readiness !== "delivered") {
884
- throw new Error("Reporting delivery-ready activity requires a successful materialization");
885
- }
886
- return {
887
- activityId: activityIdentity(namespace, tenantId, accountId, eventId, REPORTING_DELIVERY_EVENT_TYPE),
888
- transitionId: eventId,
889
- activityType: REPORTING_DELIVERY_EVENT_TYPE,
890
- notificationType: REPORTING_DELIVERY_EVENT_TYPE,
891
- tenantId,
892
- accountId,
893
- reporting_obligation_id: input.obligation.reporting_obligation_id,
894
- occurredAt,
895
- delivery_config_id: input.obligation.delivery_config_id,
896
- delivery_config_version: input.obligation.delivery_config_version,
897
- feed_purpose: input.obligation.feedPurpose,
898
- reporting_revision_id: input.revision.reporting_revision_id,
899
- reporting_materialization_id: eventId,
900
- readiness,
901
- finality: input.revision.finality,
902
- data_through: input.revision.dataThrough
903
- };
904
- }
905
- function notificationPayload(activity) {
906
- if (activity.activityType === "reporting.lifecycle_changed") {
907
- return {
908
- reporting_obligation_id: activity.reporting_obligation_id,
909
- previous_health: activity.previousHealth,
910
- health: activity.health,
911
- issue_ids: [...activity.issueIds],
912
- fired_at: activity.occurredAt,
913
- delivery_config_id: activity.correlation.delivery_config_id,
914
- delivery_config_version: activity.correlation.delivery_config_version,
915
- feed_purpose: activity.correlation.feed_purpose
916
- };
917
- }
918
- if (activity.activityType === REPORTING_LEDGER_EVENT_TYPE) {
919
- return {
920
- fired_at: activity.occurredAt,
921
- change_kind: activity.changeKind,
922
- ...activity.reporting_revision_id ? { reporting_revision_id: activity.reporting_revision_id } : {},
923
- ...activity.supersedes_reporting_revision_id ? { supersedes_reporting_revision_id: activity.supersedes_reporting_revision_id } : {},
924
- ...activity.finality ? { finality: activity.finality } : {},
925
- ...activity.reporting_adjustment_id ? { reporting_adjustment_id: activity.reporting_adjustment_id } : {},
926
- ...activity.adjusts_reporting_revision_id ? { adjusts_reporting_revision_id: activity.adjusts_reporting_revision_id } : {}
927
- };
928
- }
929
- return {
930
- fired_at: activity.occurredAt,
931
- delivery_config_id: activity.delivery_config_id,
932
- delivery_config_version: activity.delivery_config_version,
933
- feed_purpose: activity.feed_purpose,
934
- reporting_revision_id: activity.reporting_revision_id,
935
- reporting_materialization_id: activity.reporting_materialization_id,
936
- readiness: activity.readiness,
937
- finality: activity.finality,
938
- data_through: activity.data_through
939
- };
940
- }
941
- async function claimPending(db, table, cursorTable, namespace, ownerToken, leaseMs, limit) {
942
- const result = await reportingActivityDatabaseOperation(
943
- "Reporting notification recovery claim failed",
944
- () => db.query(
945
- `WITH selected_tenant AS (
946
- INSERT INTO ${cursorTable} (namespace, tenant_scope)
947
- VALUES (
948
- $1,
949
- (SELECT MIN(tenant_scope) FROM ${table}
950
- WHERE namespace = $1 AND state = 'pending' AND next_attempt_at <= clock_timestamp()
951
- AND (lease_expires_at IS NULL OR lease_expires_at < clock_timestamp()))
952
- )
953
- ON CONFLICT (namespace) DO UPDATE SET
954
- tenant_scope = COALESCE(
955
- (SELECT MIN(candidate.tenant_scope) FROM ${table} candidate
956
- WHERE candidate.namespace = $1 AND candidate.state = 'pending'
957
- AND candidate.next_attempt_at <= clock_timestamp()
958
- AND (candidate.lease_expires_at IS NULL OR candidate.lease_expires_at < clock_timestamp())
959
- AND candidate.tenant_scope > ${cursorTable}.tenant_scope),
960
- (SELECT MIN(candidate.tenant_scope) FROM ${table} candidate
961
- WHERE candidate.namespace = $1 AND candidate.state = 'pending'
962
- AND candidate.next_attempt_at <= clock_timestamp()
963
- AND (candidate.lease_expires_at IS NULL OR candidate.lease_expires_at < clock_timestamp()))
964
- ),
965
- changed_at = clock_timestamp()
966
- RETURNING tenant_scope
967
- ), candidates AS (
968
- SELECT activity.namespace, activity.transition_id FROM ${table} activity
969
- CROSS JOIN selected_tenant selected
970
- WHERE activity.namespace = $1 AND activity.state = 'pending'
971
- AND activity.tenant_scope = selected.tenant_scope
972
- AND activity.next_attempt_at <= clock_timestamp()
973
- AND (activity.lease_expires_at IS NULL OR activity.lease_expires_at < clock_timestamp())
974
- ORDER BY activity.next_attempt_at, activity.activity_sequence
975
- FOR UPDATE OF activity SKIP LOCKED LIMIT $4
976
- )
977
- UPDATE ${table} target SET
978
- lease_owner = $2,
979
- lease_version = target.lease_version + 1,
980
- lease_expires_at = clock_timestamp() + ($3::bigint * INTERVAL '1 millisecond'),
981
- attempt_count = target.attempt_count + 1
982
- FROM candidates
983
- WHERE target.namespace = candidates.namespace AND target.transition_id = candidates.transition_id
984
- RETURNING target.transition_id, target.tenant_scope, target.account_id,
985
- target.activity, target.lease_version::text, target.attempt_count`,
986
- [namespace, ownerToken, leaseMs, limit]
987
- )
988
- );
989
- return result.rows.map((row) => ({
990
- transitionId: row.transition_id,
991
- tenantId: row.tenant_scope,
992
- accountId: row.account_id,
993
- activity: structuredClone(row.activity),
994
- leaseOwner: ownerToken,
995
- leaseVersion: row.lease_version,
996
- attemptCount: row.attempt_count
997
- }));
998
- }
999
- async function freezeClaimRecipients(db, table, recipientTable, namespace, claim, maxRecipients, maxRetainedRecipients, candidates) {
1000
- if (candidates.length > maxRecipients) {
1001
- throw new Error(
1002
- `Reporting notification fanout of ${candidates.length} recipients exceeds maxRecipients ${maxRecipients}; raise maxRecipients to at least the notification runtime maxFanoutCandidates`
1003
- );
1004
- }
1005
- const fenced = await reportingActivityDatabaseOperation(
1006
- "Reporting notification recipient intent could not be committed",
1007
- () => db.query(
1008
- `UPDATE ${table} SET delivery_intent_at = COALESCE(delivery_intent_at, clock_timestamp())
1009
- WHERE namespace = $1 AND transition_id = $2 AND state = 'pending'
1010
- AND lease_owner = $3 AND lease_version = $4::bigint
1011
- AND lease_expires_at >= clock_timestamp()`,
1012
- [namespace, claim.transitionId, claim.leaseOwner, claim.leaseVersion]
1013
- )
1014
- );
1015
- if (fenced.rowCount !== 1) {
1016
- throw new Error("Reporting notification recipient intent was not committed; the recovery lease was lost");
1017
- }
1018
- const rows = canonicalRecipients(candidates);
1019
- await compactSettledRecipients(db, table, recipientTable, namespace, claim);
1020
- const committed = await reportingActivityDatabaseOperation(
1021
- "Reporting notification recipient intent could not be committed",
1022
- () => db.query(
1023
- `WITH leased AS MATERIALIZED (
1024
- -- The parent row is the serialization point, and MATERIALIZED makes
1025
- -- its lock the statement's first act. Reading the lease without
1026
- -- locking it only proved the lease was live when the snapshot was
1027
- -- taken: a statement that then blocked on a recipient lock could
1028
- -- resume long after a successor had claimed, still see its own lease
1029
- -- in the cached snapshot, and mutate the successor's rows. Holding
1030
- -- this lock means a takeover cannot complete while this statement
1031
- -- runs, and a statement that starts after one sees the new lease and
1032
- -- matches nothing.
1033
- SELECT transition_id FROM ${table}
1034
- WHERE namespace = $1 AND transition_id = $2 AND state = 'pending'
1035
- AND lease_owner = $3 AND lease_version = $4::bigint
1036
- AND lease_expires_at >= clock_timestamp()
1037
- FOR UPDATE
1038
- ), existing AS (
1039
- -- FOR UPDATE is what serializes this statement against a concurrent
1040
- -- checkpoint. Without it the budget is computed from a snapshot in
1041
- -- which a row is still revisable, the DELETE then re-checks the
1042
- -- locked row, finds it attempted and skips it, and the retained set
1043
- -- lands one row above the budget that approved the write.
1044
- SELECT current.recipient_fingerprint, current.subscriber_key, current.recipient,
1045
- current.attempt_at, current.settled_at
1046
- FROM ${recipientTable} current, leased
1047
- WHERE current.namespace = $1 AND current.transition_id = $2
1048
- FOR UPDATE OF current
1049
- ), claimed AS (
1050
- SELECT subscriber_key FROM existing
1051
- WHERE attempt_at IS NOT NULL OR settled_at IS NOT NULL
1052
- ), proposed AS (
1053
- SELECT entry.fingerprint, entry.subscriber_key, entry.recipient::jsonb AS recipient
1054
- FROM leased, unnest($5::text[], $6::text[], $7::text[])
1055
- AS entry(fingerprint, subscriber_key, recipient)
1056
- WHERE NOT EXISTS (SELECT 1 FROM claimed WHERE claimed.subscriber_key = entry.subscriber_key)
1057
- ), budget AS (
1058
- SELECT
1059
- (SELECT count(*) FROM existing
1060
- WHERE attempt_at IS NOT NULL OR settled_at IS NOT NULL
1061
- OR recipient_fingerprint IN (SELECT fingerprint FROM proposed))
1062
- + (SELECT count(*) FROM proposed
1063
- WHERE fingerprint NOT IN (SELECT recipient_fingerprint FROM existing)) AS retained
1064
- ), gate AS (
1065
- -- The lease is part of the gate, not just of the row sources. A
1066
- -- stale worker sees an empty \`leased\`, which makes every source
1067
- -- empty and the budget zero; without this predicate the budget would
1068
- -- be satisfied and the delete below would reap the rows the
1069
- -- successor had already frozen under its own lease.
1070
- SELECT retained, retained <= $8::integer AND EXISTS (SELECT 1 FROM leased) AS ok FROM budget
1071
- ), dropped AS (
1072
- DELETE FROM ${recipientTable} stale USING gate, leased
1073
- WHERE gate.ok AND stale.namespace = $1 AND stale.transition_id = $2
1074
- AND stale.attempt_at IS NULL AND stale.settled_at IS NULL
1075
- AND NOT (stale.recipient_fingerprint IN (SELECT fingerprint FROM proposed))
1076
- ), added AS (
1077
- INSERT INTO ${recipientTable}
1078
- (namespace, transition_id, recipient_fingerprint, subscriber_key, recipient)
1079
- SELECT $1, $2, proposed.fingerprint, proposed.subscriber_key, proposed.recipient
1080
- FROM proposed, gate, leased WHERE gate.ok
1081
- ON CONFLICT (namespace, transition_id, recipient_fingerprint) DO NOTHING
1082
- ), pinned AS (
1083
- SELECT recipient FROM existing WHERE attempt_at IS NOT NULL AND settled_at IS NULL
1084
- )
1085
- SELECT (SELECT count(*) FROM leased) = 1 AS leased, gate.ok, gate.retained, entry.recipient
1086
- FROM gate
1087
- LEFT JOIN (SELECT recipient FROM proposed UNION ALL SELECT recipient FROM pinned) entry ON TRUE`,
1088
- [
1089
- namespace,
1090
- claim.transitionId,
1091
- claim.leaseOwner,
1092
- claim.leaseVersion,
1093
- rows.map((entry) => entry.fingerprint),
1094
- rows.map((entry) => entry.subscriberKey),
1095
- rows.map((entry) => JSON.stringify(entry.recipient)),
1096
- maxRetainedRecipients
1097
- ]
1098
- )
1099
- );
1100
- const verdict = committed.rows[0];
1101
- if (!verdict?.leased) {
1102
- throw new Error("Reporting notification recipient intent was not committed; the recovery lease was lost");
1103
- }
1104
- if (!verdict.ok) {
1105
- throw new Error(
1106
- `Reporting notification recipient intent would hold ${verdict.retained} recipients, above maxRetainedRecipients ${maxRetainedRecipients}; nothing was changed. Raise maxRetainedRecipients to at least the fanout plus the distinct subscribers this notification can replace, or reduce its destination churn`
1107
- );
1108
- }
1109
- return committed.rows.flatMap((row) => row.recipient === null ? [] : [row.recipient]);
1110
- }
1111
- async function settleClaimRecipients(db, table, recipientTable, namespace, claim, frozen, deliveries) {
1112
- const outcomes = new Map(deliveries.map((delivery) => [recipientFingerprint(delivery.recipient), delivery]));
1113
- const terminal = [];
1114
- const delivered = [];
1115
- for (const recipient of frozen) {
1116
- const fingerprint = recipientFingerprint(recipient);
1117
- const outcome = outcomes.get(fingerprint);
1118
- if (outcome === void 0) {
1119
- terminal.push(fingerprint);
1120
- continue;
1121
- }
1122
- if (outcome.disposition === "delivered") delivered.push(fingerprint);
1123
- else if (outcome.disposition === "terminal") terminal.push(fingerprint);
1124
- }
1125
- if (delivered.length > 0 || terminal.length > 0) {
1126
- await reportingActivityDatabaseOperation(
1127
- "Reporting notification recipient settlement failed",
1128
- () => db.query(
1129
- `WITH leased AS MATERIALIZED (
1130
- SELECT transition_id FROM ${table}
1131
- WHERE namespace = $1 AND transition_id = $2 AND state = 'pending'
1132
- AND lease_owner = $5 AND lease_version = $6::bigint
1133
- AND lease_expires_at >= clock_timestamp()
1134
- FOR UPDATE
1135
- )
1136
- UPDATE ${recipientTable} AS target SET settled_at = clock_timestamp(),
1137
- disposition = CASE WHEN target.recipient_fingerprint = ANY($3::text[]) THEN 'delivered' ELSE 'terminal' END
1138
- FROM leased
1139
- WHERE target.namespace = $1 AND target.transition_id = $2 AND target.settled_at IS NULL
1140
- AND (target.recipient_fingerprint = ANY($3::text[]) OR target.recipient_fingerprint = ANY($4::text[]))`,
1141
- [namespace, claim.transitionId, delivered, terminal, claim.leaseOwner, claim.leaseVersion]
1142
- )
1143
- );
1144
- await compactSettledRecipients(db, table, recipientTable, namespace, claim);
1145
- }
1146
- const remaining = await reportingActivityDatabaseOperation(
1147
- "Reporting notification recipient settlement failed",
1148
- () => db.query(
1149
- `SELECT count(*)::integer AS unsettled FROM ${recipientTable}
1150
- WHERE namespace = $1 AND transition_id = $2 AND settled_at IS NULL`,
1151
- [namespace, claim.transitionId]
1152
- )
1153
- );
1154
- const unsettled = remaining.rows[0]?.unsettled ?? 0;
1155
- return { settled: unsettled === 0, unsettled };
1156
- }
1157
- async function compactSettledRecipients(db, table, recipientTable, namespace, claim) {
1158
- await reportingActivityDatabaseOperation(
1159
- "Reporting notification recipient compaction failed",
1160
- () => db.query(
1161
- `WITH leased AS MATERIALIZED (
1162
- SELECT transition_id FROM ${table}
1163
- WHERE namespace = $1 AND transition_id = $2 AND state = 'pending'
1164
- AND lease_owner = $3 AND lease_version = $4::bigint
1165
- AND lease_expires_at >= clock_timestamp()
1166
- FOR UPDATE
1167
- )
1168
- DELETE FROM ${recipientTable} superseded USING leased
1169
- WHERE superseded.namespace = $1 AND superseded.transition_id = $2
1170
- AND superseded.settled_at IS NOT NULL
1171
- AND EXISTS (
1172
- SELECT 1 FROM ${recipientTable} survivor
1173
- WHERE survivor.namespace = superseded.namespace
1174
- AND survivor.transition_id = superseded.transition_id
1175
- AND survivor.subscriber_key = superseded.subscriber_key
1176
- AND survivor.settled_at IS NOT NULL
1177
- AND (survivor.disposition, survivor.recipient_fingerprint)
1178
- > (superseded.disposition, superseded.recipient_fingerprint)
1179
- )`,
1180
- [namespace, claim.transitionId, claim.leaseOwner, claim.leaseVersion]
1181
- )
1182
- );
1183
- }
1184
- function classifyDelivery(delivery) {
1185
- const recipient = {
1186
- scope: delivery.scope,
1187
- subscriberId: delivery.subscriberId,
1188
- destinationGeneration: delivery.destinationGeneration
1189
- };
1190
- if (delivery.failure !== void 0 || !delivery.result) {
1191
- return { recipient, disposition: delivery.failure?.terminal === true ? "terminal" : "retryable" };
1192
- }
1193
- const suppression = delivery.result.suppression;
1194
- if (suppression) {
1195
- return {
1196
- recipient,
1197
- disposition: (0, import_notification_subscriptions.notificationSuppressionDisposition)(suppression.reason) === "retryable" ? "retryable" : "terminal",
1198
- suppression: suppression.reason
1199
- };
1200
- }
1201
- if (delivery.result.delivered) return { recipient, disposition: "delivered" };
1202
- return { recipient, disposition: delivery.result.terminal === true ? "terminal" : "retryable" };
1203
- }
1204
- function recipientFingerprint(recipient) {
1205
- return (0, import_jcs.canonicalJsonSha256)({
1206
- scope: recipient.scope,
1207
- subscriberId: recipient.subscriberId,
1208
- destinationGeneration: recipient.destinationGeneration
1209
- });
1210
- }
1211
- function canonicalRecipients(candidates) {
1212
- return candidates.map((candidate) => {
1213
- const recipient = {
1214
- scope: candidate.scope,
1215
- subscriberId: candidate.subscriberId,
1216
- destinationGeneration: candidate.destinationGeneration
1217
- };
1218
- return {
1219
- fingerprint: recipientFingerprint(recipient),
1220
- subscriberKey: subscriberKey(recipient),
1221
- recipient
1222
- };
1223
- }).sort((left, right) => left.fingerprint < right.fingerprint ? -1 : 1);
1224
- }
1225
- function subscriberKey(recipient) {
1226
- return (0, import_jcs.canonicalJsonSha256)({ scope: recipient.scope, subscriberId: recipient.subscriberId });
1227
- }
1228
- async function renewClaim(db, table, namespace, claim, leaseMs) {
1229
- const result = await reportingActivityDatabaseOperation(
1230
- "Reporting notification lease renewal failed",
1231
- () => db.query(
1232
- `UPDATE ${table} SET lease_expires_at = clock_timestamp() + ($5::bigint * INTERVAL '1 millisecond')
1233
- WHERE namespace = $1 AND transition_id = $2 AND state = 'pending'
1234
- AND lease_owner = $3 AND lease_version = $4::bigint
1235
- AND lease_expires_at >= clock_timestamp()`,
1236
- [namespace, claim.transitionId, claim.leaseOwner, claim.leaseVersion, leaseMs]
1237
- )
1238
- );
1239
- return result.rowCount === 1;
1240
- }
1241
- async function projectClaim(db, table, namespace, claim, retentionMs) {
1242
- const result = await reportingActivityDatabaseOperation(
1243
- "Reporting notification projection settlement failed",
1244
- () => db.query(
1245
- `UPDATE ${table} SET state = 'projected', projected_at = clock_timestamp(),
1246
- retain_until = clock_timestamp() + ($5::bigint * INTERVAL '1 millisecond'),
1247
- lease_owner = NULL, lease_expires_at = NULL
1248
- WHERE namespace = $1 AND transition_id = $2 AND state = 'pending'
1249
- AND lease_owner = $3 AND lease_version = $4::bigint
1250
- AND lease_expires_at >= clock_timestamp()`,
1251
- [namespace, claim.transitionId, claim.leaseOwner, claim.leaseVersion, retentionMs]
1252
- )
1253
- );
1254
- return result.rowCount === 1;
1255
- }
1256
- async function releaseClaim(db, table, namespace, claim, retryAfterMs) {
1257
- const result = await reportingActivityDatabaseOperation(
1258
- "Reporting notification lease release failed",
1259
- () => db.query(
1260
- `UPDATE ${table} SET lease_owner = NULL, lease_expires_at = NULL,
1261
- next_attempt_at = clock_timestamp() + ($5::bigint * INTERVAL '1 millisecond')
1262
- WHERE namespace = $1 AND transition_id = $2 AND state = 'pending'
1263
- AND lease_owner = $3 AND lease_version = $4::bigint
1264
- AND lease_expires_at >= clock_timestamp()`,
1265
- [namespace, claim.transitionId, claim.leaseOwner, claim.leaseVersion, retryAfterMs]
1266
- )
1267
- );
1268
- return result.rowCount === 1;
1269
- }
1270
- async function abandonClaim(db, table, namespace, claim, retentionMs) {
1271
- const result = await reportingActivityDatabaseOperation(
1272
- "Reporting notification abandonment failed",
1273
- () => db.query(
1274
- `UPDATE ${table} SET state = 'abandoned',
1275
- abandoned_at = clock_timestamp(),
1276
- retain_until = clock_timestamp() + ($5::bigint * INTERVAL '1 millisecond'),
1277
- lease_owner = NULL, lease_expires_at = NULL
1278
- WHERE namespace = $1 AND transition_id = $2 AND state = 'pending'
1279
- AND lease_owner = $3 AND lease_version = $4::bigint
1280
- AND lease_expires_at >= clock_timestamp()`,
1281
- [namespace, claim.transitionId, claim.leaseOwner, claim.leaseVersion, retentionMs]
1282
- )
1283
- );
1284
- return result.rowCount === 1;
1285
- }
1286
- function encodeCursor(namespace, tenantId, accountId, before) {
1287
- const cursor = `ract1.${Buffer.from(JSON.stringify({ namespace, tenantId, accountId, before })).toString("base64url")}`;
1288
- if (Buffer.byteLength(cursor, "utf8") > MAX_CURSOR_BYTES) {
1289
- throw new Error("Reporting account activity cursor exceeds its internal bound");
1290
- }
1291
- return cursor;
1292
- }
1293
- function decodeCursor(cursor, namespace, tenantId, accountId) {
1294
- if (cursor === void 0) return void 0;
1295
- try {
1296
- if (Buffer.byteLength(cursor, "utf8") > MAX_CURSOR_BYTES) throw new Error();
1297
- if (!cursor.startsWith("ract1.")) throw new Error();
1298
- const value = JSON.parse(Buffer.from(cursor.slice(6), "base64url").toString("utf8"));
1299
- if (value.namespace !== namespace || value.tenantId !== tenantId || value.accountId !== accountId || typeof value.before !== "string" || !/^[1-9][0-9]*$/.test(value.before)) {
1300
- throw new Error();
1301
- }
1302
- if (BigInt(value.before) > 9223372036854775807n) throw new Error();
1303
- return value.before;
1304
- } catch {
1305
- throw new TypeError("Reporting account activity cursor is invalid for the authenticated scope");
1306
- }
1307
- }
1308
- function quoteIdentifier(value, maxBytes = 42) {
1309
- if (!/^[a-z_][a-z0-9_]*$/.test(value) || Buffer.byteLength(value, "utf8") > maxBytes) {
1310
- throw new TypeError(
1311
- `Invalid reporting activity table name ${JSON.stringify(value)}: use at most ${maxBytes} lowercase characters`
1312
- );
1313
- }
1314
- return `"${value}"`;
1315
- }
1316
- function assertIdentifier(value, name, maxBytes) {
1317
- if (typeof value !== "string" || value.length === 0 || value.includes("\0") || Buffer.byteLength(value) > maxBytes) {
1318
- throw new TypeError(`${name} must be a non-empty UTF-8 string of at most ${maxBytes} bytes without NUL`);
1319
- }
1320
- }
1321
- function positiveInteger(value, name) {
1322
- if (!Number.isSafeInteger(value) || value < 1) throw new TypeError(`${name} must be a positive safe integer`);
1323
- }
1324
- function boundedInteger(value, name, minimum, maximum) {
1325
- if (!Number.isSafeInteger(value) || value < minimum || value > maximum) {
1326
- throw new TypeError(`${name} must be an integer from ${minimum} through ${maximum}`);
1327
- }
1328
- }
1329
- function isPromiseLike(value) {
1330
- return value != null && typeof value === "object" && typeof value.then === "function";
1331
- }
1332
- function isPostgresUndefinedTable(value) {
1333
- return typeof value === "object" && value !== null && value.code === "42P01";
1334
- }
1335
- function reportProjectionError(observer, error, claim) {
1336
- try {
1337
- const result = observer?.(error, {
1338
- transitionId: claim.transitionId,
1339
- tenantId: claim.tenantId,
1340
- accountId: claim.accountId,
1341
- attemptCount: claim.attemptCount
1342
- });
1343
- if (isPromiseLike(result)) void Promise.resolve(result).catch(() => {
1344
- });
1345
- } catch {
1346
- }
1347
- }
1348
- async function reportingActivityDatabaseOperation(message, operation) {
1349
- try {
1350
- return await operation();
1351
- } catch (cause) {
1352
- throw new Error(message, { cause });
1353
- }
1354
- }
1355
- function asIso(value) {
1356
- return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
1357
- }
1358
- // Annotate the CommonJS export names for ESM import in node:
1359
- 0 && (module.exports = {
1360
- REPORTING_NOTIFICATION_ACTIVITY_MIGRATION,
1361
- ReportingNotificationRetryableSuppressionError,
1362
- createPostgresReportingNotificationActivityRuntime,
1363
- createPostgresReportingNotificationAttemptCheckpoint,
1364
- getReportingNotificationActivityMigration,
1365
- recipientTableName
1366
- });