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

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 (182) 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/version.d.mts +3 -3
  73. package/dist/lib/version.d.ts +3 -3
  74. package/dist/lib/version.js +3 -3
  75. package/dist/lib/version.mjs +3 -3
  76. package/docs/TYPE-SUMMARY.md +23 -5
  77. package/docs/guides/REPORTING-GCS-FENCE.md +3 -3
  78. package/docs/guides/REPORTING-GCS-MANAGED.md +4 -4
  79. package/docs/guides/REPORTING-LEDGER-CUTOVER.md +7 -7
  80. package/docs/guides/REPORTING-LEDGER.md +107 -51
  81. package/docs/guides/REPORTING-OPERATIONS.md +66 -5
  82. package/docs/guides/REPORTING-STABILITY.md +36 -32
  83. package/docs/llms.txt +4 -3
  84. package/docs/migration-14.x-rc-worksheet.md +4 -7
  85. package/docs/migration-reporting-storage-package.md +186 -0
  86. package/examples/README.md +1 -1
  87. package/package.json +13 -78
  88. package/dist/lib/reporting/azure/index.d.mts +0 -31
  89. package/dist/lib/reporting/azure/index.d.ts +0 -31
  90. package/dist/lib/reporting/azure/index.js +0 -266
  91. package/dist/lib/reporting/azure/index.mjs +0 -249
  92. package/dist/lib/reporting/bigquery/index.d.mts +0 -246
  93. package/dist/lib/reporting/bigquery/index.d.ts +0 -246
  94. package/dist/lib/reporting/bigquery/index.js +0 -633
  95. package/dist/lib/reporting/bigquery/index.mjs +0 -594
  96. package/dist/lib/reporting/gcs/adapter.d.mts +0 -38
  97. package/dist/lib/reporting/gcs/adapter.d.ts +0 -38
  98. package/dist/lib/reporting/gcs/adapter.js +0 -342
  99. package/dist/lib/reporting/gcs/adapter.mjs +0 -325
  100. package/dist/lib/reporting/gcs/bucket-policy.d.mts +0 -3
  101. package/dist/lib/reporting/gcs/bucket-policy.d.ts +0 -3
  102. package/dist/lib/reporting/gcs/bucket-policy.mjs +0 -7
  103. package/dist/lib/reporting/gcs/errors.d.mts +0 -11
  104. package/dist/lib/reporting/gcs/errors.d.ts +0 -11
  105. package/dist/lib/reporting/gcs/errors.mjs +0 -16
  106. package/dist/lib/reporting/gcs/fence.d.mts +0 -41
  107. package/dist/lib/reporting/gcs/fence.d.ts +0 -41
  108. package/dist/lib/reporting/gcs/fence.js +0 -250
  109. package/dist/lib/reporting/gcs/fence.mjs +0 -230
  110. package/dist/lib/reporting/gcs/index.d.mts +0 -18
  111. package/dist/lib/reporting/gcs/index.d.ts +0 -18
  112. package/dist/lib/reporting/gcs/index.js +0 -44
  113. package/dist/lib/reporting/gcs/index.mjs +0 -14
  114. package/dist/lib/reporting/gcs/io.d.mts +0 -7
  115. package/dist/lib/reporting/gcs/io.d.ts +0 -7
  116. package/dist/lib/reporting/gcs/io.js +0 -95
  117. package/dist/lib/reporting/gcs/io.mjs +0 -69
  118. package/dist/lib/reporting/gcs/reader.d.mts +0 -67
  119. package/dist/lib/reporting/gcs/reader.d.ts +0 -67
  120. package/dist/lib/reporting/gcs/reader.js +0 -193
  121. package/dist/lib/reporting/gcs/reader.mjs +0 -169
  122. package/dist/lib/reporting/gcs/row-provider.d.mts +0 -39
  123. package/dist/lib/reporting/gcs/row-provider.d.ts +0 -39
  124. package/dist/lib/reporting/gcs/row-provider.js +0 -225
  125. package/dist/lib/reporting/gcs/row-provider.mjs +0 -208
  126. package/dist/lib/reporting/gcs/validation.d.mts +0 -5
  127. package/dist/lib/reporting/gcs/validation.d.ts +0 -5
  128. package/dist/lib/reporting/gcs/validation.js +0 -49
  129. package/dist/lib/reporting/gcs/validation.mjs +0 -25
  130. package/dist/lib/reporting/ledger/change-feed.d.mts +0 -69
  131. package/dist/lib/reporting/ledger/change-feed.d.ts +0 -69
  132. package/dist/lib/reporting/ledger/change-feed.js +0 -107
  133. package/dist/lib/reporting/ledger/change-feed.mjs +0 -80
  134. package/dist/lib/reporting/ledger/managed-postgres.d.mts +0 -293
  135. package/dist/lib/reporting/ledger/managed-postgres.d.ts +0 -293
  136. package/dist/lib/reporting/ledger/managed-postgres.js +0 -2346
  137. package/dist/lib/reporting/ledger/managed-postgres.mjs +0 -2332
  138. package/dist/lib/reporting/ledger/managed-router.d.mts +0 -148
  139. package/dist/lib/reporting/ledger/managed-router.d.ts +0 -148
  140. package/dist/lib/reporting/ledger/managed-router.js +0 -280
  141. package/dist/lib/reporting/ledger/managed-router.mjs +0 -258
  142. package/dist/lib/reporting/ledger/managed.d.mts +0 -535
  143. package/dist/lib/reporting/ledger/managed.d.ts +0 -535
  144. package/dist/lib/reporting/ledger/managed.js +0 -761
  145. package/dist/lib/reporting/ledger/managed.mjs +0 -729
  146. package/dist/lib/reporting/ledger/notification-activity.d.mts +0 -332
  147. package/dist/lib/reporting/ledger/notification-activity.d.ts +0 -332
  148. package/dist/lib/reporting/ledger/notification-activity.js +0 -1366
  149. package/dist/lib/reporting/ledger/notification-activity.mjs +0 -1337
  150. package/dist/lib/reporting/ledger/object-writes.d.mts +0 -128
  151. package/dist/lib/reporting/ledger/object-writes.d.ts +0 -128
  152. package/dist/lib/reporting/ledger/object-writes.js +0 -147
  153. package/dist/lib/reporting/ledger/object-writes.mjs +0 -114
  154. package/dist/lib/reporting/ledger/postgres.d.mts +0 -689
  155. package/dist/lib/reporting/ledger/postgres.d.ts +0 -689
  156. package/dist/lib/reporting/ledger/postgres.js +0 -4462
  157. package/dist/lib/reporting/ledger/postgres.mjs +0 -4462
  158. package/dist/lib/reporting/ledger/row-storage-migration.d.mts +0 -59
  159. package/dist/lib/reporting/ledger/row-storage-migration.d.ts +0 -59
  160. package/dist/lib/reporting/ledger/row-storage-migration.js +0 -365
  161. package/dist/lib/reporting/ledger/row-storage-migration.mjs +0 -339
  162. package/dist/lib/reporting/ledger/row-storage-object.d.mts +0 -203
  163. package/dist/lib/reporting/ledger/row-storage-object.d.ts +0 -203
  164. package/dist/lib/reporting/ledger/row-storage-object.js +0 -379
  165. package/dist/lib/reporting/ledger/row-storage-object.mjs +0 -335
  166. package/dist/lib/reporting/ledger/row-storage-provider-io.d.mts +0 -44
  167. package/dist/lib/reporting/ledger/row-storage-provider-io.d.ts +0 -44
  168. package/dist/lib/reporting/ledger/row-storage-provider-io.js +0 -142
  169. package/dist/lib/reporting/ledger/row-storage-provider-io.mjs +0 -111
  170. package/dist/lib/reporting/ledger/row-storage.d.mts +0 -316
  171. package/dist/lib/reporting/ledger/row-storage.d.ts +0 -316
  172. package/dist/lib/reporting/ledger/row-storage.js +0 -812
  173. package/dist/lib/reporting/ledger/row-storage.mjs +0 -802
  174. package/dist/lib/reporting/s3/index.d.mts +0 -34
  175. package/dist/lib/reporting/s3/index.d.ts +0 -34
  176. package/dist/lib/reporting/s3/index.js +0 -347
  177. package/dist/lib/reporting/s3/index.mjs +0 -340
  178. package/dist/lib/reporting/service/maintenance.d.mts +0 -148
  179. package/dist/lib/reporting/service/maintenance.d.ts +0 -148
  180. package/dist/lib/reporting/service/maintenance.js +0 -185
  181. package/dist/lib/reporting/service/maintenance.mjs +0 -159
  182. package/examples/reliable-reporting-service/README.md +0 -169
@@ -1,59 +0,0 @@
1
- /**
2
- * Row-storage tables for reporting revisions (shared SDK persistence spec,
3
- * `reporting-row-storage` v1, adcontextprotocol/adcp#7996 §3.2).
4
- *
5
- * Apply after {@link REPORTING_LEDGER_MIGRATION}. PostgreSQL 13 or later
6
- * (`gen_random_uuid()`, `sha256()`). Idempotent; reapplying preserves the
7
- * installation identity and every recorded location.
8
- *
9
- * Stored values use AdCP's names and bounds (`reporting-file-manifest.json`,
10
- * `reporting-file-entry.json`): `object_ref` and `native_version_ref` are 1 to
11
- * 1024 characters, `size_bytes` is the stored object's size, and the row-set
12
- * `format` / `algorithm` are `jsonl` / `adcp_jcs_rows_v1`. A database created by
13
- * an earlier 15.x release (`object_key`, `native_version`, `byte_count`,
14
- * `physical_byte_count`, `canonical_byte_count`, `encoding`) is renamed in place
15
- * by the first block below; the migration also rewrites any open write
16
- * intent's `created_objects`. Recorded object keys are not rewritten. Adopters
17
- * who have not activated row storage should re-sync instead.
18
- *
19
- * - `adcp_persistence_installation` holds one installation UUID per isolated
20
- * schema. A restore of the same authority keeps it; an independently
21
- * writable clone must mint a new one before it writes or sweeps.
22
- * - `adcp_reporting_row_bindings` holds immutable, non-secret descriptions of
23
- * where row bytes live. Only `state`, `operational_config` and `retired_at`
24
- * may change.
25
- * - `adcp_reporting_row_sets` records where a revision's (or adjustment's)
26
- * rows live and binds the chunk manifest digest. A revision without a row
27
- * set still carries its rows inline in its ledger document.
28
- * - `adcp_reporting_row_chunks` is the per-chunk manifest; only location
29
- * columns may change, through a guarded move.
30
- * - `adcp_reporting_chunk_bodies` holds `postgres`-kind chunk bytes,
31
- * content-addressed within one obligation so revisions that repeat their
32
- * predecessor's rows share bodies.
33
- * - `adcp_reporting_row_write_intents` fences external uploads so a sweep
34
- * never deletes bytes a committing writer still needs.
35
- *
36
- * @experimental Reliable Reporting storage surface: breaking changes may ship in minor releases. See https://github.com/adcontextprotocol/adcp-client/blob/main/docs/guides/REPORTING-STABILITY.md
37
- */
38
- export declare const REPORTING_ROW_STORAGE_MIGRATION: string;
39
- /**
40
- * Tables {@link REPORTING_ROW_STORAGE_MIGRATION} creates, in dependency order.
41
- *
42
- * @experimental Reliable Reporting storage surface: breaking changes may ship in minor releases. See https://github.com/adcontextprotocol/adcp-client/blob/main/docs/guides/REPORTING-STABILITY.md
43
- */
44
- export declare const REPORTING_ROW_STORAGE_TABLES: readonly ["adcp_persistence_installation", "adcp_reporting_row_bindings", "adcp_reporting_row_sets", "adcp_reporting_row_chunks", "adcp_reporting_chunk_bodies", "adcp_reporting_row_write_intents"];
45
- interface QueryablePoolV1 {
46
- query<Row extends Record<string, unknown> = Record<string, unknown>>(text: string, values?: unknown[]): Promise<{
47
- rows: Row[];
48
- }>;
49
- }
50
- /**
51
- * Fail closed unless every row-storage table exists in the current schema.
52
- * Returns the installation identity.
53
- *
54
- * @experimental Reliable Reporting storage surface: breaking changes may ship in minor releases. See https://github.com/adcontextprotocol/adcp-client/blob/main/docs/guides/REPORTING-STABILITY.md
55
- */
56
- export declare function probeReportingRowStorageSchemaV1(pool: QueryablePoolV1): Promise<{
57
- installationId: string;
58
- }>;
59
- export {};
@@ -1,59 +0,0 @@
1
- /**
2
- * Row-storage tables for reporting revisions (shared SDK persistence spec,
3
- * `reporting-row-storage` v1, adcontextprotocol/adcp#7996 §3.2).
4
- *
5
- * Apply after {@link REPORTING_LEDGER_MIGRATION}. PostgreSQL 13 or later
6
- * (`gen_random_uuid()`, `sha256()`). Idempotent; reapplying preserves the
7
- * installation identity and every recorded location.
8
- *
9
- * Stored values use AdCP's names and bounds (`reporting-file-manifest.json`,
10
- * `reporting-file-entry.json`): `object_ref` and `native_version_ref` are 1 to
11
- * 1024 characters, `size_bytes` is the stored object's size, and the row-set
12
- * `format` / `algorithm` are `jsonl` / `adcp_jcs_rows_v1`. A database created by
13
- * an earlier 15.x release (`object_key`, `native_version`, `byte_count`,
14
- * `physical_byte_count`, `canonical_byte_count`, `encoding`) is renamed in place
15
- * by the first block below; the migration also rewrites any open write
16
- * intent's `created_objects`. Recorded object keys are not rewritten. Adopters
17
- * who have not activated row storage should re-sync instead.
18
- *
19
- * - `adcp_persistence_installation` holds one installation UUID per isolated
20
- * schema. A restore of the same authority keeps it; an independently
21
- * writable clone must mint a new one before it writes or sweeps.
22
- * - `adcp_reporting_row_bindings` holds immutable, non-secret descriptions of
23
- * where row bytes live. Only `state`, `operational_config` and `retired_at`
24
- * may change.
25
- * - `adcp_reporting_row_sets` records where a revision's (or adjustment's)
26
- * rows live and binds the chunk manifest digest. A revision without a row
27
- * set still carries its rows inline in its ledger document.
28
- * - `adcp_reporting_row_chunks` is the per-chunk manifest; only location
29
- * columns may change, through a guarded move.
30
- * - `adcp_reporting_chunk_bodies` holds `postgres`-kind chunk bytes,
31
- * content-addressed within one obligation so revisions that repeat their
32
- * predecessor's rows share bodies.
33
- * - `adcp_reporting_row_write_intents` fences external uploads so a sweep
34
- * never deletes bytes a committing writer still needs.
35
- *
36
- * @experimental Reliable Reporting storage surface: breaking changes may ship in minor releases. See https://github.com/adcontextprotocol/adcp-client/blob/main/docs/guides/REPORTING-STABILITY.md
37
- */
38
- export declare const REPORTING_ROW_STORAGE_MIGRATION: string;
39
- /**
40
- * Tables {@link REPORTING_ROW_STORAGE_MIGRATION} creates, in dependency order.
41
- *
42
- * @experimental Reliable Reporting storage surface: breaking changes may ship in minor releases. See https://github.com/adcontextprotocol/adcp-client/blob/main/docs/guides/REPORTING-STABILITY.md
43
- */
44
- export declare const REPORTING_ROW_STORAGE_TABLES: readonly ["adcp_persistence_installation", "adcp_reporting_row_bindings", "adcp_reporting_row_sets", "adcp_reporting_row_chunks", "adcp_reporting_chunk_bodies", "adcp_reporting_row_write_intents"];
45
- interface QueryablePoolV1 {
46
- query<Row extends Record<string, unknown> = Record<string, unknown>>(text: string, values?: unknown[]): Promise<{
47
- rows: Row[];
48
- }>;
49
- }
50
- /**
51
- * Fail closed unless every row-storage table exists in the current schema.
52
- * Returns the installation identity.
53
- *
54
- * @experimental Reliable Reporting storage surface: breaking changes may ship in minor releases. See https://github.com/adcontextprotocol/adcp-client/blob/main/docs/guides/REPORTING-STABILITY.md
55
- */
56
- export declare function probeReportingRowStorageSchemaV1(pool: QueryablePoolV1): Promise<{
57
- installationId: string;
58
- }>;
59
- export {};
@@ -1,365 +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 row_storage_migration_exports = {};
20
- __export(row_storage_migration_exports, {
21
- REPORTING_ROW_STORAGE_MIGRATION: () => REPORTING_ROW_STORAGE_MIGRATION,
22
- REPORTING_ROW_STORAGE_TABLES: () => REPORTING_ROW_STORAGE_TABLES,
23
- probeReportingRowStorageSchemaV1: () => probeReportingRowStorageSchemaV1
24
- });
25
- module.exports = __toCommonJS(row_storage_migration_exports);
26
- const REPORTING_ROW_STORAGE_MIGRATION = `
27
- -- Serialize concurrent appliers (several instances may run migrations at startup) so the guarded
28
- -- renames below cannot race each other. Released at the end of this implicit transaction.
29
- SELECT pg_advisory_xact_lock(hashtextextended('adcp-reporting-row-storage-migration', 0));
30
-
31
- -- In-place upgrade from the pre-AdCP-alignment column names. Every step is guarded, so a fresh
32
- -- install (no tables yet) and a reapplied migration are both no-ops here.
33
- DO $align$
34
- DECLARE
35
- renames TEXT[][] := ARRAY[
36
- ARRAY['adcp_reporting_row_chunks', 'object_key', 'object_ref'],
37
- ARRAY['adcp_reporting_row_chunks', 'native_version', 'native_version_ref'],
38
- ARRAY['adcp_reporting_row_chunks', 'byte_count', 'canonical_size_bytes'],
39
- ARRAY['adcp_reporting_row_chunks', 'physical_byte_count', 'size_bytes'],
40
- ARRAY['adcp_reporting_row_sets', 'canonical_byte_count', 'content_size_bytes']
41
- ];
42
- item TEXT[];
43
- BEGIN
44
- FOREACH item SLICE 1 IN ARRAY renames LOOP
45
- IF EXISTS (
46
- SELECT 1 FROM information_schema.columns
47
- WHERE table_schema = current_schema() AND table_name = item[1] AND column_name = item[2]
48
- ) AND NOT EXISTS (
49
- SELECT 1 FROM information_schema.columns
50
- WHERE table_schema = current_schema() AND table_name = item[1] AND column_name = item[3]
51
- ) THEN
52
- EXECUTE format('ALTER TABLE %I RENAME COLUMN %I TO %I', item[1], item[2], item[3]);
53
- END IF;
54
- END LOOP;
55
- -- encoding = 'adcp_canonical_jsonl_v1' becomes AdCP's format = 'jsonl' plus
56
- -- algorithm = 'adcp_jcs_rows_v1'. The old CHECK admitted one value, so the defaults backfill
57
- -- every row without an UPDATE (the immutability trigger forbids one).
58
- IF EXISTS (
59
- SELECT 1 FROM information_schema.columns
60
- WHERE table_schema = current_schema() AND table_name = 'adcp_reporting_row_sets' AND column_name = 'encoding'
61
- ) AND NOT EXISTS (
62
- SELECT 1 FROM information_schema.columns
63
- WHERE table_schema = current_schema() AND table_name = 'adcp_reporting_row_sets' AND column_name = 'format'
64
- ) THEN
65
- ALTER TABLE adcp_reporting_row_sets
66
- ADD COLUMN format TEXT NOT NULL DEFAULT 'jsonl' CHECK (format = 'jsonl'),
67
- ADD COLUMN algorithm TEXT NOT NULL DEFAULT 'adcp_jcs_rows_v1' CHECK (algorithm = 'adcp_jcs_rows_v1');
68
- ALTER TABLE adcp_reporting_row_sets ALTER COLUMN format DROP DEFAULT, ALTER COLUMN algorithm DROP DEFAULT;
69
- ALTER TABLE adcp_reporting_row_sets DROP COLUMN encoding;
70
- END IF;
71
- END
72
- $align$;
73
-
74
- CREATE TABLE IF NOT EXISTS adcp_persistence_installation (
75
- singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton),
76
- installation_id UUID NOT NULL DEFAULT gen_random_uuid(),
77
- created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
78
- );
79
- INSERT INTO adcp_persistence_installation (singleton) VALUES (TRUE) ON CONFLICT DO NOTHING;
80
-
81
- CREATE TABLE IF NOT EXISTS adcp_reporting_row_bindings (
82
- row_binding_id TEXT COLLATE "C" PRIMARY KEY CHECK (row_binding_id ~ '^[A-Za-z0-9_.:-]{1,128}$'),
83
- kind TEXT NOT NULL CHECK (kind IN ('postgres', 'object')),
84
- provider TEXT NOT NULL CHECK (provider ~ '^[a-z][a-z0-9_]{0,63}$'),
85
- identity_config JSONB NOT NULL CHECK (jsonb_typeof(identity_config) = 'object'),
86
- identity_sha256 TEXT COLLATE "C" NOT NULL CHECK (identity_sha256 ~ '^[0-9a-f]{64}$'),
87
- operational_config JSONB NOT NULL DEFAULT '{}'::jsonb CHECK (jsonb_typeof(operational_config) = 'object'),
88
- namespace_key TEXT COLLATE "C" NOT NULL CHECK (namespace_key ~ '^[0-9a-f]{64}$'),
89
- state TEXT NOT NULL DEFAULT 'active' CHECK (state IN ('active', 'read_only', 'retired')),
90
- created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
91
- retired_at TIMESTAMPTZ,
92
- CHECK ((state = 'retired') = (retired_at IS NOT NULL))
93
- );
94
-
95
- CREATE TABLE IF NOT EXISTS adcp_reporting_row_sets (
96
- row_set_id TEXT COLLATE "C" PRIMARY KEY,
97
- row_set_kind TEXT NOT NULL CHECK (row_set_kind IN ('revision', 'adjustment')),
98
- account_id TEXT COLLATE "C" NOT NULL,
99
- obligation_id TEXT COLLATE "C" NOT NULL REFERENCES adcp_reporting_obligations(obligation_id),
100
- format TEXT NOT NULL CHECK (format = 'jsonl'),
101
- algorithm TEXT NOT NULL CHECK (algorithm = 'adcp_jcs_rows_v1'),
102
- digest_profile TEXT NOT NULL CHECK (digest_profile IN ('revision_envelope_v1', 'rows_v1')),
103
- content_sha256 TEXT COLLATE "C" NOT NULL CHECK (content_sha256 ~ '^[0-9a-f]{64}$'),
104
- content_size_bytes BIGINT NOT NULL CHECK (content_size_bytes > 0),
105
- row_count BIGINT NOT NULL CHECK (row_count >= 0),
106
- row_manifest_sha256 TEXT COLLATE "C" NOT NULL CHECK (row_manifest_sha256 ~ '^[0-9a-f]{64}$'),
107
- chunk_count INTEGER NOT NULL CHECK (chunk_count >= 0),
108
- rows_shared_from_row_set_id TEXT COLLATE "C",
109
- row_binding_id TEXT COLLATE "C" NOT NULL REFERENCES adcp_reporting_row_bindings(row_binding_id),
110
- row_location_version INTEGER NOT NULL DEFAULT 1 CHECK (row_location_version > 0),
111
- rows_state TEXT NOT NULL DEFAULT 'live' CHECK (rows_state IN ('live', 'pruning', 'pruned', 'unavailable')),
112
- rows_state_changed_at TIMESTAMPTZ,
113
- recorded_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
114
- CHECK ((row_count = 0) = (chunk_count = 0)),
115
- CHECK (rows_shared_from_row_set_id IS NULL OR rows_shared_from_row_set_id <> row_set_id),
116
- -- Tenant columns are the composite key children reference, so a chunk or a
117
- -- shared row set can never name another account's or obligation's rows.
118
- UNIQUE (row_set_id, account_id, obligation_id),
119
- FOREIGN KEY (rows_shared_from_row_set_id, account_id, obligation_id)
120
- REFERENCES adcp_reporting_row_sets (row_set_id, account_id, obligation_id)
121
- );
122
- CREATE INDEX IF NOT EXISTS adcp_reporting_row_sets_obligation
123
- ON adcp_reporting_row_sets (obligation_id, recorded_at, row_set_id);
124
- CREATE INDEX IF NOT EXISTS adcp_reporting_row_sets_state
125
- ON adcp_reporting_row_sets (account_id, rows_state, recorded_at)
126
- WHERE rows_state <> 'pruned';
127
- CREATE INDEX IF NOT EXISTS adcp_reporting_row_sets_shared
128
- ON adcp_reporting_row_sets (rows_shared_from_row_set_id)
129
- WHERE rows_shared_from_row_set_id IS NOT NULL;
130
-
131
- CREATE TABLE IF NOT EXISTS adcp_reporting_row_chunks (
132
- row_set_id TEXT COLLATE "C" NOT NULL,
133
- chunk_index INTEGER NOT NULL CHECK (chunk_index >= 0),
134
- account_id TEXT COLLATE "C" NOT NULL,
135
- obligation_id TEXT COLLATE "C" NOT NULL,
136
- first_ordinal BIGINT NOT NULL CHECK (first_ordinal >= 0),
137
- row_count INTEGER NOT NULL CHECK (row_count > 0),
138
- canonical_size_bytes BIGINT NOT NULL CHECK (canonical_size_bytes > 0),
139
- sha256 TEXT COLLATE "C" NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
140
- segments JSONB NOT NULL CHECK (jsonb_typeof(segments) = 'array'),
141
- object_ref TEXT COLLATE "C" CONSTRAINT adcp_reporting_row_chunks_object_ref_bounds
142
- CHECK (object_ref IS NULL OR char_length(object_ref) BETWEEN 1 AND 1024),
143
- native_version_ref TEXT CONSTRAINT adcp_reporting_row_chunks_native_version_ref_bounds
144
- CHECK (native_version_ref IS NULL OR char_length(native_version_ref) BETWEEN 1 AND 1024),
145
- compression TEXT NOT NULL DEFAULT 'none' CHECK (compression IN ('none', 'gzip')),
146
- physical_sha256 TEXT COLLATE "C" CHECK (physical_sha256 IS NULL OR physical_sha256 ~ '^[0-9a-f]{64}$'),
147
- size_bytes BIGINT CHECK (size_bytes IS NULL OR size_bytes > 0),
148
- PRIMARY KEY (row_set_id, chunk_index),
149
- FOREIGN KEY (row_set_id, account_id, obligation_id)
150
- REFERENCES adcp_reporting_row_sets (row_set_id, account_id, obligation_id),
151
- CHECK ((object_ref IS NULL) = (native_version_ref IS NULL)),
152
- CHECK ((object_ref IS NULL) = (physical_sha256 IS NULL)),
153
- CHECK ((object_ref IS NULL) = (size_bytes IS NULL))
154
- );
155
- CREATE INDEX IF NOT EXISTS adcp_reporting_row_chunks_object
156
- ON adcp_reporting_row_chunks (object_ref, native_version_ref)
157
- WHERE object_ref IS NOT NULL;
158
- CREATE INDEX IF NOT EXISTS adcp_reporting_row_chunks_body
159
- ON adcp_reporting_row_chunks (account_id, obligation_id, sha256)
160
- WHERE object_ref IS NULL;
161
-
162
- CREATE TABLE IF NOT EXISTS adcp_reporting_chunk_bodies (
163
- account_id TEXT COLLATE "C" NOT NULL,
164
- obligation_id TEXT COLLATE "C" NOT NULL,
165
- sha256 TEXT COLLATE "C" NOT NULL,
166
- body BYTEA NOT NULL,
167
- recorded_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
168
- PRIMARY KEY (account_id, obligation_id, sha256),
169
- CHECK (sha256 = encode(sha256(body), 'hex'))
170
- );
171
-
172
- CREATE TABLE IF NOT EXISTS adcp_reporting_row_write_intents (
173
- row_binding_id TEXT COLLATE "C" NOT NULL REFERENCES adcp_reporting_row_bindings(row_binding_id),
174
- account_id TEXT COLLATE "C" NOT NULL,
175
- row_set_id TEXT COLLATE "C" NOT NULL,
176
- content_sha256 TEXT COLLATE "C" NOT NULL CHECK (content_sha256 ~ '^[0-9a-f]{64}$'),
177
- state TEXT NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'sweeping')),
178
- owner_token TEXT COLLATE "C" NOT NULL,
179
- created_objects JSONB NOT NULL DEFAULT '[]'::jsonb CHECK (jsonb_typeof(created_objects) = 'array'),
180
- created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
181
- expires_at TIMESTAMPTZ NOT NULL,
182
- PRIMARY KEY (row_binding_id, row_set_id, content_sha256)
183
- );
184
- CREATE INDEX IF NOT EXISTS adcp_reporting_row_write_intents_expiry
185
- ON adcp_reporting_row_write_intents (expires_at, account_id)
186
- WHERE state = 'open';
187
- -- Sweep retry state. An intent whose sweep fails backs off instead of holding
188
- -- the head of every pass. Guarded on the catalog because ADD COLUMN IF NOT EXISTS
189
- -- takes ACCESS EXCLUSIVE even when the column exists, which would block a rerun
190
- -- behind every upload.
191
- DO $intent_retry$
192
- DECLARE
193
- intents regclass := to_regclass('adcp_reporting_row_write_intents');
194
- BEGIN
195
- IF NOT EXISTS (SELECT 1 FROM pg_attribute WHERE attrelid = intents AND attname = 'sweep_attempts' AND NOT attisdropped) THEN
196
- ALTER TABLE adcp_reporting_row_write_intents ADD COLUMN sweep_attempts INTEGER NOT NULL DEFAULT 0;
197
- END IF;
198
- IF NOT EXISTS (SELECT 1 FROM pg_attribute WHERE attrelid = intents AND attname = 'next_sweep_at' AND NOT attisdropped) THEN
199
- ALTER TABLE adcp_reporting_row_write_intents ADD COLUMN next_sweep_at TIMESTAMPTZ;
200
- END IF;
201
- END
202
- $intent_retry$;
203
-
204
- CREATE OR REPLACE FUNCTION adcp_reporting_row_storage_immutable() RETURNS trigger
205
- LANGUAGE plpgsql AS $immutable$
206
- BEGIN
207
- IF TG_TABLE_NAME = 'adcp_reporting_row_bindings' THEN
208
- IF (NEW.row_binding_id, NEW.kind, NEW.provider, NEW.identity_config, NEW.identity_sha256, NEW.namespace_key)
209
- IS DISTINCT FROM
210
- (OLD.row_binding_id, OLD.kind, OLD.provider, OLD.identity_config, OLD.identity_sha256, OLD.namespace_key) THEN
211
- RAISE EXCEPTION 'reporting row binding identity is immutable' USING ERRCODE = '23514';
212
- END IF;
213
- ELSIF TG_TABLE_NAME = 'adcp_reporting_row_sets' THEN
214
- IF (NEW.row_set_id, NEW.row_set_kind, NEW.account_id, NEW.obligation_id, NEW.format, NEW.algorithm,
215
- NEW.digest_profile, NEW.content_sha256, NEW.content_size_bytes, NEW.row_count, NEW.row_manifest_sha256, NEW.chunk_count,
216
- NEW.rows_shared_from_row_set_id, NEW.recorded_at)
217
- IS DISTINCT FROM
218
- (OLD.row_set_id, OLD.row_set_kind, OLD.account_id, OLD.obligation_id, OLD.format, OLD.algorithm,
219
- OLD.digest_profile, OLD.content_sha256, OLD.content_size_bytes, OLD.row_count, OLD.row_manifest_sha256, OLD.chunk_count,
220
- OLD.rows_shared_from_row_set_id, OLD.recorded_at) THEN
221
- RAISE EXCEPTION 'reporting row set content is immutable' USING ERRCODE = '23514';
222
- END IF;
223
- IF NEW.row_binding_id IS DISTINCT FROM OLD.row_binding_id
224
- AND NEW.row_location_version <> OLD.row_location_version + 1 THEN
225
- RAISE EXCEPTION 'reporting row set moves must advance row_location_version' USING ERRCODE = '23514';
226
- END IF;
227
- ELSIF TG_TABLE_NAME = 'adcp_reporting_row_chunks' THEN
228
- IF (NEW.row_set_id, NEW.chunk_index, NEW.account_id, NEW.obligation_id, NEW.first_ordinal, NEW.row_count,
229
- NEW.canonical_size_bytes, NEW.sha256, NEW.segments)
230
- IS DISTINCT FROM
231
- (OLD.row_set_id, OLD.chunk_index, OLD.account_id, OLD.obligation_id, OLD.first_ordinal, OLD.row_count,
232
- OLD.canonical_size_bytes, OLD.sha256, OLD.segments) THEN
233
- RAISE EXCEPTION 'reporting row chunk content is immutable' USING ERRCODE = '23514';
234
- END IF;
235
- ELSIF TG_TABLE_NAME = 'adcp_reporting_chunk_bodies' THEN
236
- RAISE EXCEPTION 'reporting chunk bodies are immutable' USING ERRCODE = '23514';
237
- END IF;
238
- RETURN NEW;
239
- END
240
- $immutable$;
241
-
242
- DO $triggers$
243
- DECLARE
244
- target TEXT;
245
- BEGIN
246
- FOREACH target IN ARRAY ARRAY[
247
- 'adcp_reporting_row_bindings',
248
- 'adcp_reporting_row_sets',
249
- 'adcp_reporting_row_chunks',
250
- 'adcp_reporting_chunk_bodies'
251
- ] LOOP
252
- IF NOT EXISTS (
253
- SELECT 1 FROM pg_trigger
254
- WHERE tgname = target || '_immutable'
255
- AND tgrelid = to_regclass(quote_ident(current_schema()) || '.' || quote_ident(target))
256
- ) THEN
257
- EXECUTE format(
258
- 'CREATE TRIGGER %I BEFORE UPDATE ON %I FOR EACH ROW EXECUTE FUNCTION adcp_reporting_row_storage_immutable()',
259
- target || '_immutable',
260
- target
261
- );
262
- END IF;
263
- END LOOP;
264
- END
265
- $triggers$;
266
-
267
- -- AdCP bounds on installs created before they existed (a fresh install has them inline).
268
- DO $bounds$
269
- DECLARE
270
- offending BIGINT;
271
- BEGIN
272
- SELECT count(*) INTO offending FROM adcp_reporting_row_chunks
273
- WHERE object_ref IS NOT NULL
274
- AND (char_length(object_ref) NOT BETWEEN 1 AND 1024 OR char_length(native_version_ref) NOT BETWEEN 1 AND 1024);
275
- IF offending > 0 THEN
276
- RAISE EXCEPTION 'reporting row storage: % chunk(s) hold an object_ref or native_version_ref outside AdCP 1 to 1024 characters; re-sync them before applying this migration', offending
277
- USING ERRCODE = '23514';
278
- END IF;
279
- IF NOT EXISTS (
280
- SELECT 1 FROM pg_constraint
281
- WHERE conname = 'adcp_reporting_row_chunks_object_ref_bounds'
282
- AND conrelid = to_regclass(quote_ident(current_schema()) || '.adcp_reporting_row_chunks')
283
- ) THEN
284
- ALTER TABLE adcp_reporting_row_chunks ADD CONSTRAINT adcp_reporting_row_chunks_object_ref_bounds
285
- CHECK (object_ref IS NULL OR char_length(object_ref) BETWEEN 1 AND 1024);
286
- END IF;
287
- IF NOT EXISTS (
288
- SELECT 1 FROM pg_constraint
289
- WHERE conname = 'adcp_reporting_row_chunks_native_version_ref_bounds'
290
- AND conrelid = to_regclass(quote_ident(current_schema()) || '.adcp_reporting_row_chunks')
291
- ) THEN
292
- ALTER TABLE adcp_reporting_row_chunks ADD CONSTRAINT adcp_reporting_row_chunks_native_version_ref_bounds
293
- CHECK (native_version_ref IS NULL OR char_length(native_version_ref) BETWEEN 1 AND 1024);
294
- END IF;
295
- END
296
- $bounds$;
297
-
298
- -- Open write intents written before the rename name objects objectKey / nativeVersion.
299
- UPDATE adcp_reporting_row_write_intents
300
- SET created_objects = (
301
- SELECT COALESCE(jsonb_agg(
302
- CASE WHEN jsonb_typeof(created) = 'object' AND created ? 'objectKey'
303
- THEN jsonb_build_object('object_ref', created -> 'objectKey', 'native_version_ref', created -> 'nativeVersion')
304
- ELSE created END
305
- ORDER BY ordinality
306
- ), '[]'::jsonb)
307
- FROM jsonb_array_elements(created_objects) WITH ORDINALITY AS element(created, ordinality)
308
- )
309
- WHERE EXISTS (SELECT 1 FROM jsonb_array_elements(created_objects) AS element(created) WHERE jsonb_typeof(created) = 'object' AND created ? 'objectKey');
310
- `.trim();
311
- const REPORTING_ROW_STORAGE_TABLES = Object.freeze([
312
- "adcp_persistence_installation",
313
- "adcp_reporting_row_bindings",
314
- "adcp_reporting_row_sets",
315
- "adcp_reporting_row_chunks",
316
- "adcp_reporting_chunk_bodies",
317
- "adcp_reporting_row_write_intents"
318
- ]);
319
- const REPORTING_ROW_STORAGE_REQUIRED_COLUMNS = Object.freeze([
320
- "adcp_reporting_row_sets.format",
321
- "adcp_reporting_row_sets.algorithm",
322
- "adcp_reporting_row_sets.content_size_bytes",
323
- "adcp_reporting_row_chunks.canonical_size_bytes",
324
- "adcp_reporting_row_chunks.object_ref",
325
- "adcp_reporting_row_chunks.native_version_ref",
326
- "adcp_reporting_row_chunks.size_bytes"
327
- ]);
328
- async function probeReportingRowStorageSchemaV1(pool) {
329
- const present = await pool.query(
330
- `SELECT table_name AS name FROM information_schema.tables
331
- WHERE table_schema = current_schema() AND table_name = ANY($1::text[])`,
332
- [[...REPORTING_ROW_STORAGE_TABLES]]
333
- );
334
- const names = new Set(present.rows.map((row) => row.name));
335
- const missing = REPORTING_ROW_STORAGE_TABLES.filter((name) => !names.has(name));
336
- if (missing.length > 0) {
337
- throw new Error(
338
- `Reporting row storage requires REPORTING_ROW_STORAGE_MIGRATION; missing tables: ${missing.join(", ")}`
339
- );
340
- }
341
- const columns = await pool.query(
342
- `SELECT table_name || '.' || column_name AS name FROM information_schema.columns
343
- WHERE table_schema = current_schema() AND table_name = ANY($1::text[])`,
344
- [["adcp_reporting_row_chunks", "adcp_reporting_row_sets"]]
345
- );
346
- const columnNames = new Set(columns.rows.map((row) => row.name));
347
- const absent = REPORTING_ROW_STORAGE_REQUIRED_COLUMNS.filter((name) => !columnNames.has(name));
348
- if (absent.length > 0) {
349
- throw new Error(
350
- `Reporting row storage requires the current REPORTING_ROW_STORAGE_MIGRATION; missing columns: ${absent.join(", ")}`
351
- );
352
- }
353
- const installation = await pool.query(
354
- "SELECT installation_id::text AS installation_id FROM adcp_persistence_installation WHERE singleton"
355
- );
356
- const installationId = installation.rows[0]?.installation_id;
357
- if (!installationId) throw new Error("Reporting row storage installation identity is missing");
358
- return { installationId };
359
- }
360
- // Annotate the CommonJS export names for ESM import in node:
361
- 0 && (module.exports = {
362
- REPORTING_ROW_STORAGE_MIGRATION,
363
- REPORTING_ROW_STORAGE_TABLES,
364
- probeReportingRowStorageSchemaV1
365
- });