@adcp/sdk 16.0.0-beta.2 → 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 (248) hide show
  1. package/README.md +54 -0
  2. package/dist/lib/auth/oauth/diagnose.js +3 -11
  3. package/dist/lib/auth/oauth/diagnose.mjs +4 -12
  4. package/dist/lib/auth/oauth/diagnostics.d.mts +2 -1
  5. package/dist/lib/auth/oauth/diagnostics.d.ts +2 -1
  6. package/dist/lib/auth/oauth/diagnostics.js +8 -10
  7. package/dist/lib/auth/oauth/diagnostics.mjs +7 -10
  8. package/dist/lib/core/ConversationTypes.d.mts +8 -3
  9. package/dist/lib/core/ConversationTypes.d.ts +8 -3
  10. package/dist/lib/core/SingleAgentClient.d.mts +5 -3
  11. package/dist/lib/core/SingleAgentClient.d.ts +5 -3
  12. package/dist/lib/core/TaskExecutor.js +5 -1
  13. package/dist/lib/core/TaskExecutor.mjs +5 -1
  14. package/dist/lib/errors/index.d.mts +9 -4
  15. package/dist/lib/errors/index.d.ts +9 -4
  16. package/dist/lib/index.d.mts +3 -3
  17. package/dist/lib/index.d.ts +3 -3
  18. package/dist/lib/index.js +8 -0
  19. package/dist/lib/index.mjs +14 -2
  20. package/dist/lib/negotiation/types.d.mts +20 -39
  21. package/dist/lib/negotiation/types.d.ts +20 -39
  22. package/dist/lib/protocols/index.d.mts +60 -24
  23. package/dist/lib/protocols/index.d.ts +60 -24
  24. package/dist/lib/protocols/index.js +9 -4
  25. package/dist/lib/protocols/index.mjs +3 -0
  26. package/dist/lib/protocols/mcp-modern.js +1 -0
  27. package/dist/lib/protocols/mcp-modern.mjs +2 -1
  28. package/dist/lib/protocols/mcp.js +1 -0
  29. package/dist/lib/protocols/mcp.mjs +2 -1
  30. package/dist/lib/protocols/responseSizeLimit.d.mts +91 -5
  31. package/dist/lib/protocols/responseSizeLimit.d.ts +91 -5
  32. package/dist/lib/protocols/responseSizeLimit.js +195 -31
  33. package/dist/lib/protocols/responseSizeLimit.mjs +192 -31
  34. package/dist/lib/reporting/consumer-runtime.js +2 -1
  35. package/dist/lib/reporting/consumer-runtime.mjs +2 -1
  36. package/dist/lib/reporting/ledger/handler.js +50 -11
  37. package/dist/lib/reporting/ledger/handler.mjs +50 -12
  38. package/dist/lib/reporting/ledger/index.d.mts +17 -14
  39. package/dist/lib/reporting/ledger/index.d.ts +17 -14
  40. package/dist/lib/reporting/ledger/index.js +32 -20
  41. package/dist/lib/reporting/ledger/index.mjs +21 -10
  42. package/dist/lib/reporting/ledger/offering-validation.d.mts +9 -0
  43. package/dist/lib/reporting/ledger/offering-validation.d.ts +9 -0
  44. package/dist/lib/reporting/{gcs/bucket-policy.js → ledger/offering-validation.js} +9 -8
  45. package/dist/lib/reporting/ledger/offering-validation.mjs +8 -0
  46. package/dist/lib/reporting/ledger/receipts.d.mts +48 -0
  47. package/dist/lib/reporting/ledger/receipts.d.ts +48 -0
  48. package/dist/lib/reporting/ledger/receipts.js +186 -0
  49. package/dist/lib/reporting/ledger/receipts.mjs +161 -0
  50. package/dist/lib/reporting/ledger/status-ingest.js +3 -3
  51. package/dist/lib/reporting/ledger/status-ingest.mjs +4 -5
  52. package/dist/lib/reporting/ledger/types.d.mts +43 -0
  53. package/dist/lib/reporting/ledger/types.d.ts +43 -0
  54. package/dist/lib/reporting/ledger/types.js +31 -2
  55. package/dist/lib/reporting/ledger/types.mjs +26 -1
  56. package/dist/lib/reporting/pagination.d.mts +6 -0
  57. package/dist/lib/reporting/pagination.d.ts +6 -0
  58. package/dist/lib/reporting/{gcs/errors.js → pagination.js} +12 -17
  59. package/dist/lib/reporting/pagination.mjs +12 -0
  60. package/dist/lib/reporting/reconciliation.d.mts +1 -0
  61. package/dist/lib/reporting/reconciliation.d.ts +1 -0
  62. package/dist/lib/reporting/reconciliation.js +3 -2
  63. package/dist/lib/reporting/reconciliation.mjs +3 -2
  64. package/dist/lib/reporting/service/index.d.mts +79 -153
  65. package/dist/lib/reporting/service/index.d.ts +79 -153
  66. package/dist/lib/reporting/service/index.js +34 -491
  67. package/dist/lib/reporting/service/index.mjs +36 -510
  68. package/dist/lib/reporting/service/scheduler-support.d.mts +43 -0
  69. package/dist/lib/reporting/service/scheduler-support.d.ts +43 -0
  70. package/dist/lib/reporting/service/scheduler-support.js +125 -0
  71. package/dist/lib/reporting/service/scheduler-support.mjs +96 -0
  72. package/dist/lib/schemas-data/v2.5/_provenance.json +1 -1
  73. package/dist/lib/signing/canonicalize.d.mts +4 -0
  74. package/dist/lib/signing/canonicalize.d.ts +4 -0
  75. package/dist/lib/signing/canonicalize.js +14 -8
  76. package/dist/lib/signing/canonicalize.mjs +14 -8
  77. package/dist/lib/signing/content-digest.d.mts +8 -0
  78. package/dist/lib/signing/content-digest.d.ts +8 -0
  79. package/dist/lib/signing/content-digest.js +11 -5
  80. package/dist/lib/signing/content-digest.mjs +10 -5
  81. package/dist/lib/signing/crypto.d.mts +15 -0
  82. package/dist/lib/signing/crypto.d.ts +15 -0
  83. package/dist/lib/signing/crypto.js +75 -3
  84. package/dist/lib/signing/crypto.mjs +78 -3
  85. package/dist/lib/signing/server.d.mts +1 -1
  86. package/dist/lib/signing/server.d.ts +1 -1
  87. package/dist/lib/signing/server.js +2 -0
  88. package/dist/lib/signing/server.mjs +2 -1
  89. package/dist/lib/signing/signer.js +11 -14
  90. package/dist/lib/signing/signer.mjs +13 -16
  91. package/dist/lib/signing/testing.js +2 -6
  92. package/dist/lib/signing/testing.mjs +3 -7
  93. package/dist/lib/signing/verifier.js +2 -2
  94. package/dist/lib/signing/verifier.mjs +3 -3
  95. package/dist/lib/signing/web-bot-auth.js +122 -43
  96. package/dist/lib/signing/web-bot-auth.mjs +128 -44
  97. package/dist/lib/signing/webhook-verifier.js +2 -2
  98. package/dist/lib/signing/webhook-verifier.mjs +3 -3
  99. package/dist/lib/substitution/compile.js +2 -1
  100. package/dist/lib/substitution/compile.mjs +2 -1
  101. package/dist/lib/substitution/consent-macros.d.mts +4 -0
  102. package/dist/lib/substitution/consent-macros.d.ts +4 -0
  103. package/dist/lib/substitution/consent-macros.js +45 -0
  104. package/dist/lib/substitution/consent-macros.mjs +20 -0
  105. package/dist/lib/substitution/index.d.mts +1 -1
  106. package/dist/lib/substitution/index.d.ts +1 -1
  107. package/dist/lib/substitution/index.js +2 -0
  108. package/dist/lib/substitution/index.mjs +2 -1
  109. package/dist/lib/substitution/translate.d.mts +15 -2
  110. package/dist/lib/substitution/translate.d.ts +15 -2
  111. package/dist/lib/substitution/translate.js +148 -26
  112. package/dist/lib/substitution/translate.mjs +147 -26
  113. package/dist/lib/testing/client.js +2 -1
  114. package/dist/lib/testing/client.mjs +2 -1
  115. package/dist/lib/testing/scenarios/capabilities.js +1 -1
  116. package/dist/lib/testing/scenarios/capabilities.mjs +1 -1
  117. package/dist/lib/testing/storyboard/junit.js +16 -3
  118. package/dist/lib/testing/storyboard/junit.mjs +16 -3
  119. package/dist/lib/testing/storyboard/request-signing/builder.js +12 -14
  120. package/dist/lib/testing/storyboard/request-signing/builder.mjs +13 -15
  121. package/dist/lib/testing/storyboard/runner.js +94 -25
  122. package/dist/lib/testing/storyboard/runner.mjs +94 -25
  123. package/dist/lib/testing/storyboard/signer-grader/grader.js +2 -7
  124. package/dist/lib/testing/storyboard/signer-grader/grader.mjs +2 -7
  125. package/dist/lib/testing/storyboard/types.d.mts +6 -0
  126. package/dist/lib/testing/storyboard/types.d.ts +6 -0
  127. package/dist/lib/testing/storyboard/validations.d.mts +1 -1
  128. package/dist/lib/testing/storyboard/validations.d.ts +1 -1
  129. package/dist/lib/types/enums.generated.d.mts +3 -0
  130. package/dist/lib/types/enums.generated.d.ts +3 -0
  131. package/dist/lib/types/enums.generated.js +9 -0
  132. package/dist/lib/types/enums.generated.mjs +6 -0
  133. package/dist/lib/validation/schema-loader.js +25 -0
  134. package/dist/lib/validation/schema-loader.mjs +24 -0
  135. package/dist/lib/version.d.mts +3 -3
  136. package/dist/lib/version.d.ts +3 -3
  137. package/dist/lib/version.js +3 -3
  138. package/dist/lib/version.mjs +3 -3
  139. package/docs/TYPE-SUMMARY.md +28 -5
  140. package/docs/guides/DISCOVERY-CACHE-AND-SESSION-REUSE.md +3 -2
  141. package/docs/guides/REPORTING-GCS-FENCE.md +3 -3
  142. package/docs/guides/REPORTING-GCS-MANAGED.md +4 -4
  143. package/docs/guides/REPORTING-LEDGER-CUTOVER.md +7 -7
  144. package/docs/guides/REPORTING-LEDGER.md +107 -51
  145. package/docs/guides/REPORTING-OPERATIONS.md +66 -5
  146. package/docs/guides/REPORTING-STABILITY.md +36 -32
  147. package/docs/guides/UNIVERSAL-MACRO-COMPILATION.md +38 -0
  148. package/docs/guides/VALIDATE-YOUR-AGENT.md +12 -0
  149. package/docs/llms.txt +4 -3
  150. package/docs/migration-14.x-rc-worksheet.md +4 -7
  151. package/docs/migration-reporting-storage-package.md +186 -0
  152. package/examples/README.md +1 -1
  153. package/package.json +14 -78
  154. package/dist/lib/reporting/azure/index.d.mts +0 -31
  155. package/dist/lib/reporting/azure/index.d.ts +0 -31
  156. package/dist/lib/reporting/azure/index.js +0 -266
  157. package/dist/lib/reporting/azure/index.mjs +0 -249
  158. package/dist/lib/reporting/bigquery/index.d.mts +0 -246
  159. package/dist/lib/reporting/bigquery/index.d.ts +0 -246
  160. package/dist/lib/reporting/bigquery/index.js +0 -633
  161. package/dist/lib/reporting/bigquery/index.mjs +0 -594
  162. package/dist/lib/reporting/gcs/adapter.d.mts +0 -38
  163. package/dist/lib/reporting/gcs/adapter.d.ts +0 -38
  164. package/dist/lib/reporting/gcs/adapter.js +0 -342
  165. package/dist/lib/reporting/gcs/adapter.mjs +0 -325
  166. package/dist/lib/reporting/gcs/bucket-policy.d.mts +0 -3
  167. package/dist/lib/reporting/gcs/bucket-policy.d.ts +0 -3
  168. package/dist/lib/reporting/gcs/bucket-policy.mjs +0 -7
  169. package/dist/lib/reporting/gcs/errors.d.mts +0 -11
  170. package/dist/lib/reporting/gcs/errors.d.ts +0 -11
  171. package/dist/lib/reporting/gcs/errors.mjs +0 -16
  172. package/dist/lib/reporting/gcs/fence.d.mts +0 -41
  173. package/dist/lib/reporting/gcs/fence.d.ts +0 -41
  174. package/dist/lib/reporting/gcs/fence.js +0 -250
  175. package/dist/lib/reporting/gcs/fence.mjs +0 -230
  176. package/dist/lib/reporting/gcs/index.d.mts +0 -18
  177. package/dist/lib/reporting/gcs/index.d.ts +0 -18
  178. package/dist/lib/reporting/gcs/index.js +0 -44
  179. package/dist/lib/reporting/gcs/index.mjs +0 -14
  180. package/dist/lib/reporting/gcs/io.d.mts +0 -7
  181. package/dist/lib/reporting/gcs/io.d.ts +0 -7
  182. package/dist/lib/reporting/gcs/io.js +0 -95
  183. package/dist/lib/reporting/gcs/io.mjs +0 -69
  184. package/dist/lib/reporting/gcs/reader.d.mts +0 -67
  185. package/dist/lib/reporting/gcs/reader.d.ts +0 -67
  186. package/dist/lib/reporting/gcs/reader.js +0 -193
  187. package/dist/lib/reporting/gcs/reader.mjs +0 -169
  188. package/dist/lib/reporting/gcs/row-provider.d.mts +0 -39
  189. package/dist/lib/reporting/gcs/row-provider.d.ts +0 -39
  190. package/dist/lib/reporting/gcs/row-provider.js +0 -225
  191. package/dist/lib/reporting/gcs/row-provider.mjs +0 -208
  192. package/dist/lib/reporting/gcs/validation.d.mts +0 -5
  193. package/dist/lib/reporting/gcs/validation.d.ts +0 -5
  194. package/dist/lib/reporting/gcs/validation.js +0 -49
  195. package/dist/lib/reporting/gcs/validation.mjs +0 -25
  196. package/dist/lib/reporting/ledger/change-feed.d.mts +0 -69
  197. package/dist/lib/reporting/ledger/change-feed.d.ts +0 -69
  198. package/dist/lib/reporting/ledger/change-feed.js +0 -107
  199. package/dist/lib/reporting/ledger/change-feed.mjs +0 -80
  200. package/dist/lib/reporting/ledger/managed-postgres.d.mts +0 -293
  201. package/dist/lib/reporting/ledger/managed-postgres.d.ts +0 -293
  202. package/dist/lib/reporting/ledger/managed-postgres.js +0 -2346
  203. package/dist/lib/reporting/ledger/managed-postgres.mjs +0 -2332
  204. package/dist/lib/reporting/ledger/managed-router.d.mts +0 -148
  205. package/dist/lib/reporting/ledger/managed-router.d.ts +0 -148
  206. package/dist/lib/reporting/ledger/managed-router.js +0 -280
  207. package/dist/lib/reporting/ledger/managed-router.mjs +0 -258
  208. package/dist/lib/reporting/ledger/managed.d.mts +0 -535
  209. package/dist/lib/reporting/ledger/managed.d.ts +0 -535
  210. package/dist/lib/reporting/ledger/managed.js +0 -761
  211. package/dist/lib/reporting/ledger/managed.mjs +0 -729
  212. package/dist/lib/reporting/ledger/notification-activity.d.mts +0 -332
  213. package/dist/lib/reporting/ledger/notification-activity.d.ts +0 -332
  214. package/dist/lib/reporting/ledger/notification-activity.js +0 -1366
  215. package/dist/lib/reporting/ledger/notification-activity.mjs +0 -1337
  216. package/dist/lib/reporting/ledger/object-writes.d.mts +0 -128
  217. package/dist/lib/reporting/ledger/object-writes.d.ts +0 -128
  218. package/dist/lib/reporting/ledger/object-writes.js +0 -147
  219. package/dist/lib/reporting/ledger/object-writes.mjs +0 -114
  220. package/dist/lib/reporting/ledger/postgres.d.mts +0 -689
  221. package/dist/lib/reporting/ledger/postgres.d.ts +0 -689
  222. package/dist/lib/reporting/ledger/postgres.js +0 -4462
  223. package/dist/lib/reporting/ledger/postgres.mjs +0 -4462
  224. package/dist/lib/reporting/ledger/row-storage-migration.d.mts +0 -59
  225. package/dist/lib/reporting/ledger/row-storage-migration.d.ts +0 -59
  226. package/dist/lib/reporting/ledger/row-storage-migration.js +0 -365
  227. package/dist/lib/reporting/ledger/row-storage-migration.mjs +0 -339
  228. package/dist/lib/reporting/ledger/row-storage-object.d.mts +0 -203
  229. package/dist/lib/reporting/ledger/row-storage-object.d.ts +0 -203
  230. package/dist/lib/reporting/ledger/row-storage-object.js +0 -379
  231. package/dist/lib/reporting/ledger/row-storage-object.mjs +0 -335
  232. package/dist/lib/reporting/ledger/row-storage-provider-io.d.mts +0 -44
  233. package/dist/lib/reporting/ledger/row-storage-provider-io.d.ts +0 -44
  234. package/dist/lib/reporting/ledger/row-storage-provider-io.js +0 -142
  235. package/dist/lib/reporting/ledger/row-storage-provider-io.mjs +0 -111
  236. package/dist/lib/reporting/ledger/row-storage.d.mts +0 -316
  237. package/dist/lib/reporting/ledger/row-storage.d.ts +0 -316
  238. package/dist/lib/reporting/ledger/row-storage.js +0 -812
  239. package/dist/lib/reporting/ledger/row-storage.mjs +0 -802
  240. package/dist/lib/reporting/s3/index.d.mts +0 -34
  241. package/dist/lib/reporting/s3/index.d.ts +0 -34
  242. package/dist/lib/reporting/s3/index.js +0 -347
  243. package/dist/lib/reporting/s3/index.mjs +0 -340
  244. package/dist/lib/reporting/service/maintenance.d.mts +0 -148
  245. package/dist/lib/reporting/service/maintenance.d.ts +0 -148
  246. package/dist/lib/reporting/service/maintenance.js +0 -185
  247. package/dist/lib/reporting/service/maintenance.mjs +0 -159
  248. package/examples/reliable-reporting-service/README.md +0 -169
@@ -1,802 +0,0 @@
1
- import { createHash, randomUUID } from "crypto";
2
- import { gunzipSync, gzipSync } from "zlib";
3
- import { canonicalize } from "../../utils/jcs.mjs";
4
- import {
5
- REPORTING_ROW_CHUNK_MAX_BYTES,
6
- REPORTING_ROW_ALGORITHM_V1,
7
- REPORTING_ROW_FORMAT_V1,
8
- createReportingRevisionEnvelopeHasherV1,
9
- createReportingRowsOnlyHasherV1,
10
- decodeVerifiedReportingRowSegmentsV1,
11
- encodeReportingRowsV1,
12
- isReportingRowEncodingError,
13
- verifyReportingRowChunkV1,
14
- verifyReportingRowManifestsV1
15
- } from "./row-encoding.mjs";
16
- import { probeReportingRowStorageSchemaV1 } from "./row-storage-migration.mjs";
17
- import { ReportingRowStoreError, isReportingRowStoreError } from "./row-storage-errors.mjs";
18
- import {
19
- REPORTING_ROW_DEFAULT_KEY_TEMPLATE,
20
- REPORTING_ROW_NATIVE_VERSION_REF_MAX_LENGTH,
21
- assertReportingRowKeyTemplateV1,
22
- assertReportingRowPrefixV1,
23
- renderReportingRowObjectKeyV1
24
- } from "./row-storage-object.mjs";
25
- import { ReportingRowStoreError as ReportingRowStoreError2, isReportingRowStoreError as isReportingRowStoreError2 } from "./row-storage-errors.mjs";
26
- const DEFAULT_BINDING_ID = "postgres";
27
- const BINDING_ID = /^[A-Za-z0-9_.:-]{1,128}$/;
28
- const CREDENTIAL_REF = /^[A-Za-z0-9_.:-]{1,128}$/;
29
- const MAX_ADOPTED_OBJECT_BYTES = 2 * REPORTING_ROW_CHUNK_MAX_BYTES + 1024 * 1024;
30
- class ReportingRowStorageV1 {
31
- constructor(pool, options = {}) {
32
- this.pool = pool;
33
- this.options = options;
34
- const bindings = options.bindings ?? { [DEFAULT_BINDING_ID]: { kind: "postgres" } };
35
- this.providers = Object.freeze({ ...options.providers ?? {} });
36
- const ids = Object.keys(bindings);
37
- if (ids.length === 0) throw new ReportingRowStoreError("INVALID_INPUT", "at least one row binding is required");
38
- for (const id of ids) {
39
- if (!BINDING_ID.test(id)) throw new ReportingRowStoreError("INVALID_INPUT", `invalid row binding ID ${id}`);
40
- const binding = bindings[id];
41
- if (binding.kind === "postgres") continue;
42
- if (binding.kind !== "object") {
43
- throw new ReportingRowStoreError("INVALID_INPUT", `row binding ${id} has an unsupported kind`);
44
- }
45
- const provider = this.providers[binding.provider];
46
- if (!provider) throw new ReportingRowStoreError("INVALID_INPUT", `row binding ${id} names an unknown provider`);
47
- provider.validateLocation(binding.location);
48
- assertReportingRowPrefixV1(binding.prefix);
49
- if (binding.credentialRef !== void 0 && !CREDENTIAL_REF.test(binding.credentialRef)) {
50
- throw new ReportingRowStoreError("INVALID_INPUT", `row binding ${id} has an invalid credentialRef`);
51
- }
52
- }
53
- this.bindings = Object.freeze(
54
- Object.fromEntries(Object.entries(bindings).map(([id, binding]) => [id, Object.freeze({ ...binding })]))
55
- );
56
- this.assertBindingKeyTemplates();
57
- this.deploymentNamespace = options.deploymentNamespace ?? "default";
58
- if (!this.deploymentNamespace || this.deploymentNamespace.length > 256) {
59
- throw new ReportingRowStoreError("INVALID_INPUT", "deploymentNamespace must be 1-256 characters");
60
- }
61
- this.writeDeadline = positiveMilliseconds(options.writeDeadlineMilliseconds ?? 6e4, "writeDeadlineMilliseconds");
62
- this.readDeadline = positiveMilliseconds(options.readDeadlineMilliseconds ?? 3e4, "readDeadlineMilliseconds");
63
- }
64
- pool;
65
- options;
66
- bindings;
67
- providers;
68
- deploymentNamespace;
69
- writeDeadline;
70
- readDeadline;
71
- readied;
72
- installation;
73
- /** Deadline budget a writer must still hold on its lease before uploading. */
74
- get writeDeadlineMilliseconds() {
75
- return this.writeDeadline;
76
- }
77
- /** Every writable object binding's key template must include `{revision_key}`; throws `INVALID_INPUT`. */
78
- assertBindingKeyTemplates() {
79
- for (const [id, binding] of Object.entries(this.bindings)) {
80
- if (binding.kind !== "object") continue;
81
- try {
82
- assertReportingRowKeyTemplateV1(binding.keyTemplate ?? REPORTING_ROW_DEFAULT_KEY_TEMPLATE, {
83
- requireRevisionKey: binding.readOnly !== true
84
- });
85
- } catch (error) {
86
- if (isReportingRowStoreError(error)) {
87
- throw new ReportingRowStoreError(
88
- "INVALID_INPUT",
89
- `row binding ${id}: ${error.message.replace(/^Reporting row store: INVALID_INPUT: /, "")}`
90
- );
91
- }
92
- throw error;
93
- }
94
- }
95
- }
96
- /**
97
- * Refuse a configuration in which compaction could delete an object a concurrent commit is
98
- * adopting. An object binding whose key template omits `{revision_key}` can map two revisions
99
- * with identical chunk content to one object key, and a create-only write adopts an existing
100
- * object with identical bytes. Compaction deleting that object at the same moment would leave
101
- * the newer revision without its rows. Every writable binding is already required to include
102
- * `{revision_key}`, so this cannot fail for a validated configuration. A `readOnly` binding is
103
- * skipped: it never takes commits, so nothing can adopt through it, and every delete is
104
- * reference-checked.
105
- *
106
- * Since the reference-checked, per-object-locked delete this is no longer the only guard: every
107
- * delete path now skips an object another row set still references. It is kept as defense in
108
- * depth for compaction, the one path that deletes content while the obligation stays live.
109
- */
110
- assertCompactionSafeKeyTemplates() {
111
- const unsafe = Object.entries(this.bindings).filter(
112
- ([, binding]) => binding.kind === "object" && binding.readOnly !== true && !(binding.keyTemplate ?? REPORTING_ROW_DEFAULT_KEY_TEMPLATE).includes("{revision_key}")
113
- ).map(([id]) => id);
114
- if (unsafe.length > 0) {
115
- throw new ReportingRowStoreError(
116
- "INVALID_INPUT",
117
- `snapshot compaction requires every object row binding's keyTemplate to include {revision_key}; binding(s) ${unsafe.join(", ")} omit it, so two revisions with identical content could share an object that compaction deletes while a concurrent commit adopts it`
118
- );
119
- }
120
- }
121
- /** IDs of the object bindings this host configures, the only ones it can delete objects through. */
122
- objectBindingIds() {
123
- return Object.entries(this.bindings).filter(([, binding]) => binding.kind === "object").map(([id]) => id);
124
- }
125
- /** Probe the schema and every provider, and register (or verify) every configured binding. Idempotent. */
126
- ready() {
127
- this.readied ??= this.register().catch((error) => {
128
- this.readied = void 0;
129
- throw error;
130
- });
131
- return this.readied;
132
- }
133
- /** Encode rows, prove the chunks reproduce the committed binding, select a binding and plan objects. */
134
- async prepare(input) {
135
- await this.ready();
136
- let encoded;
137
- try {
138
- encoded = encodeReportingRowsV1(input.rows);
139
- } catch (error) {
140
- if (isReportingRowEncodingError(error)) throw new ReportingRowStoreError("INVALID_INPUT", error.message);
141
- throw error;
142
- }
143
- const digestProfile = input.rowSetKind === "revision" ? "revision_envelope_v1" : "rows_v1";
144
- const hasher = digestProfile === "revision_envelope_v1" ? createReportingRevisionEnvelopeHasherV1({
145
- reporting_revision_id: input.rowSetId,
146
- row_count: input.rows.length,
147
- control_totals: input.controlTotals ?? []
148
- }) : createReportingRowsOnlyHasherV1(input.rows.length);
149
- for (const chunk of encoded.chunks) hasher.update(chunk.bytes);
150
- const digest = hasher.digest();
151
- if (digest.sha256 !== input.binding.sha256 || digest.byteCount !== input.binding.byteCount || input.binding.rowCount !== input.rows.length) {
152
- throw new ReportingRowStoreError("INVALID_INPUT", "rows do not reproduce their committed binding");
153
- }
154
- const writable = Object.keys(this.bindings).filter((id) => {
155
- const candidate = this.bindings[id];
156
- return candidate.kind !== "object" || candidate.readOnly !== true;
157
- });
158
- if (writable.length === 0 && !input.bindingId && !this.options.selectRowBinding) {
159
- throw new ReportingRowStoreError(
160
- "UNSAFE_BINDING",
161
- "no writable row binding is configured: every binding is readOnly"
162
- );
163
- }
164
- const bindingId = input.bindingId ? input.bindingId : this.options.selectRowBinding ? await this.options.selectRowBinding({
165
- account_id: input.accountId,
166
- reporting_obligation_id: input.obligationId,
167
- row_set_kind: input.rowSetKind,
168
- ...input.finality ? { finality: input.finality } : {},
169
- row_count: input.rows.length,
170
- content_size_bytes: digest.byteCount
171
- }) : writable.length === 1 ? writable[0] : DEFAULT_BINDING_ID;
172
- const binding = this.bindings[bindingId];
173
- if (!binding) throw new ReportingRowStoreError("STATE_UNAVAILABLE", `row binding ${bindingId} is not configured`);
174
- if (binding.kind === "object" && binding.readOnly === true) {
175
- throw new ReportingRowStoreError(
176
- "UNSAFE_BINDING",
177
- `row binding ${bindingId} is read-only and cannot take new row sets`
178
- );
179
- }
180
- if (binding.kind === "postgres") return Object.freeze({ input, encoded, bindingId, binding, digestProfile });
181
- const compression = binding.compression ?? "gzip";
182
- const namespaceKey = this.installation.namespaceKey;
183
- const objects = encoded.chunks.map((chunk) => {
184
- const physicalBytes = compression === "gzip" ? gzipSync(chunk.bytes, { level: 6 }) : chunk.bytes;
185
- return Object.freeze({
186
- objectRef: renderReportingRowObjectKeyV1({
187
- template: binding.keyTemplate ?? REPORTING_ROW_DEFAULT_KEY_TEMPLATE,
188
- prefix: binding.prefix,
189
- namespaceKey,
190
- accountId: input.accountId,
191
- rowSetId: input.rowSetId,
192
- ...input.periodStart ? { periodStart: input.periodStart } : {},
193
- ...input.sourceTimezone ? { sourceTimezone: input.sourceTimezone } : {},
194
- ...input.finality ? { finality: input.finality } : {},
195
- contentSha256: input.binding.sha256,
196
- chunkIndex: chunk.manifest.chunk_index,
197
- compression
198
- }),
199
- physicalBytes,
200
- physicalSha256: sha256Hex(physicalBytes),
201
- compression
202
- });
203
- });
204
- return Object.freeze({ input, encoded, bindingId, binding, digestProfile, objects: Object.freeze(objects) });
205
- }
206
- /**
207
- * Upload a prepared object-kind row set, create-only. An object already at a
208
- * planned key is adopted only when its bytes decode to the exact chunk;
209
- * otherwise `CONTENT_CONFLICT`. `onCreated` is awaited for every object this
210
- * call created, before the next upload, so the caller can record it in its
211
- * write intent.
212
- */
213
- async upload(prepared, intentId, onCreated, signal) {
214
- const binding = prepared.binding;
215
- if (binding.kind !== "object" || !prepared.objects) {
216
- throw new ReportingRowStoreError("INVALID_INPUT", "only object-kind row sets are uploaded");
217
- }
218
- const provider = this.providers[binding.provider];
219
- const context = { signal: deadlineSignal(this.writeDeadline, signal) };
220
- const location = providerLocation(binding);
221
- const locators = [];
222
- for (const [index, object] of prepared.objects.entries()) {
223
- const manifest = prepared.encoded.manifests[index];
224
- const put = await this.provider(
225
- () => provider.putIfAbsent(
226
- {
227
- ...location,
228
- objectRef: object.objectRef,
229
- bytes: object.physicalBytes,
230
- contentType: object.compression === "gzip" ? "application/gzip" : "application/x-ndjson",
231
- metadata: {
232
- "adcp-installation": this.installation.installationId,
233
- "adcp-intent": intentId
234
- }
235
- },
236
- context
237
- )
238
- );
239
- if (put.created) {
240
- await onCreated({ objectRef: object.objectRef, nativeVersionRef: put.nativeVersionRef });
241
- assertNativeVersionRef(put.nativeVersionRef);
242
- locators.push({
243
- objectRef: object.objectRef,
244
- nativeVersionRef: put.nativeVersionRef,
245
- compression: object.compression,
246
- physicalSha256: object.physicalSha256,
247
- sizeBytes: object.physicalBytes.byteLength,
248
- created: true
249
- });
250
- continue;
251
- }
252
- assertNativeVersionRef(put.nativeVersionRef);
253
- const existing = await this.provider(
254
- () => provider.get(
255
- {
256
- ...location,
257
- objectRef: object.objectRef,
258
- nativeVersionRef: put.nativeVersionRef,
259
- maxBytes: MAX_ADOPTED_OBJECT_BYTES
260
- },
261
- context
262
- )
263
- );
264
- const logical = existing ? this.inflate(Buffer.from(existing), object.compression, manifest.byte_count) : null;
265
- if (!existing || !logical || sha256Hex(logical) !== manifest.sha256) {
266
- throw new ReportingRowStoreError("CONTENT_CONFLICT", `object for chunk ${index} holds different bytes`);
267
- }
268
- locators.push({
269
- objectRef: object.objectRef,
270
- nativeVersionRef: put.nativeVersionRef,
271
- compression: object.compression,
272
- physicalSha256: sha256Hex(existing),
273
- sizeBytes: existing.byteLength,
274
- created: false
275
- });
276
- }
277
- return locators;
278
- }
279
- /**
280
- * Persist a prepared row set inside the caller's ledger transaction:
281
- * bodies for the `postgres` kind, or the uploaded locators for the object
282
- * kind. Replays of an identical row set are no-ops; a conflicting row set
283
- * under the same ID is refused.
284
- */
285
- async writeInTransaction(transaction, prepared, locators, sharedFromRowSetId) {
286
- const { input, encoded } = prepared;
287
- if (prepared.binding.kind === "object" && locators?.length !== encoded.chunks.length) {
288
- throw new ReportingRowStoreError("INVALID_INPUT", "object-kind row sets need one locator per chunk");
289
- }
290
- const inserted = await transaction.query(
291
- `INSERT INTO adcp_reporting_row_sets
292
- (row_set_id, row_set_kind, account_id, obligation_id, format, algorithm, digest_profile, content_sha256,
293
- content_size_bytes, row_count, row_manifest_sha256, chunk_count, row_binding_id,
294
- rows_shared_from_row_set_id)
295
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
296
- ON CONFLICT (row_set_id) DO NOTHING`,
297
- [
298
- input.rowSetId,
299
- input.rowSetKind,
300
- input.accountId,
301
- input.obligationId,
302
- REPORTING_ROW_FORMAT_V1,
303
- REPORTING_ROW_ALGORITHM_V1,
304
- prepared.digestProfile,
305
- input.binding.sha256,
306
- input.binding.byteCount,
307
- encoded.rowCount,
308
- encoded.rowManifestSha256,
309
- encoded.chunks.length,
310
- prepared.bindingId,
311
- sharedFromRowSetId ?? null
312
- ]
313
- );
314
- if (inserted.rowCount !== 1) {
315
- const existing = await transaction.query(
316
- "SELECT content_sha256, row_manifest_sha256 FROM adcp_reporting_row_sets WHERE row_set_id = $1",
317
- [input.rowSetId]
318
- );
319
- const row = existing.rows[0];
320
- if (!row || row.content_sha256 !== input.binding.sha256 || row.row_manifest_sha256 !== encoded.rowManifestSha256) {
321
- throw new ReportingRowStoreError("CONTENT_CONFLICT", "row set identity names different content");
322
- }
323
- return;
324
- }
325
- for (const [index, chunk] of encoded.chunks.entries()) {
326
- const manifest = chunk.manifest;
327
- const locator = locators?.[index];
328
- if (!locator) {
329
- await transaction.query(
330
- `INSERT INTO adcp_reporting_chunk_bodies (account_id, obligation_id, sha256, body)
331
- VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING`,
332
- [input.accountId, input.obligationId, manifest.sha256, chunk.bytes]
333
- );
334
- }
335
- await transaction.query(
336
- `INSERT INTO adcp_reporting_row_chunks
337
- (row_set_id, chunk_index, account_id, obligation_id, first_ordinal, row_count, canonical_size_bytes, sha256,
338
- segments, object_ref, native_version_ref, compression, physical_sha256, size_bytes)
339
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::jsonb, $10, $11, $12, $13, $14)`,
340
- [
341
- input.rowSetId,
342
- manifest.chunk_index,
343
- input.accountId,
344
- input.obligationId,
345
- manifest.first_ordinal,
346
- manifest.row_count,
347
- manifest.byte_count,
348
- manifest.sha256,
349
- JSON.stringify(manifest.segments),
350
- locator?.objectRef ?? null,
351
- locator?.nativeVersionRef ?? null,
352
- locator?.compression ?? "none",
353
- locator?.physicalSha256 ?? null,
354
- locator?.sizeBytes ?? null
355
- ]
356
- );
357
- }
358
- }
359
- /**
360
- * Load a row set and verify its chunk manifest against the committed digest.
361
- * Returns `null` when no row set exists (legacy inline rows) or it belongs
362
- * to another account. Throws `ROWS_EXPIRED` / `ROWS_UNAVAILABLE` when the
363
- * rows were pruned or lost.
364
- */
365
- async find(rowSetId, accountId, queryable = this.pool) {
366
- const found = await queryable.query(
367
- `SELECT row_set.row_set_id, row_set.row_set_kind, row_set.account_id, row_set.obligation_id,
368
- row_set.digest_profile, row_set.content_sha256, row_set.content_size_bytes, row_set.row_count,
369
- row_set.row_manifest_sha256, row_set.chunk_count, row_set.row_binding_id,
370
- row_set.row_location_version, row_set.rows_state, binding.kind, binding.identity_sha256
371
- FROM adcp_reporting_row_sets row_set
372
- JOIN adcp_reporting_row_bindings binding ON binding.row_binding_id = row_set.row_binding_id
373
- WHERE row_set.row_set_id = $1 AND row_set.account_id = $2`,
374
- [rowSetId, accountId]
375
- );
376
- const record = found.rows[0];
377
- if (!record) return null;
378
- if (record.rows_state === "pruning" || record.rows_state === "pruned") {
379
- throw new ReportingRowStoreError("ROWS_EXPIRED");
380
- }
381
- if (record.rows_state === "unavailable") throw new ReportingRowStoreError("ROWS_UNAVAILABLE");
382
- const chunks = await queryable.query(
383
- `SELECT chunk_index, first_ordinal, row_count, canonical_size_bytes, sha256, segments,
384
- object_ref, native_version_ref, compression, physical_sha256, size_bytes
385
- FROM adcp_reporting_row_chunks WHERE row_set_id = $1 ORDER BY chunk_index`,
386
- [rowSetId]
387
- );
388
- const manifests = chunks.rows.map((chunk) => ({
389
- chunk_index: chunk.chunk_index,
390
- first_ordinal: Number(chunk.first_ordinal),
391
- row_count: chunk.row_count,
392
- byte_count: Number(chunk.canonical_size_bytes),
393
- sha256: chunk.sha256,
394
- segments: chunk.segments.map((segment) => ({
395
- first_ordinal: Number(segment.first_ordinal),
396
- row_count: Number(segment.row_count),
397
- byte_offset: Number(segment.byte_offset),
398
- byte_count: Number(segment.byte_count),
399
- sha256: segment.sha256
400
- }))
401
- }));
402
- const rowCount = Number(record.row_count);
403
- try {
404
- verifyReportingRowManifestsV1(manifests, { rowCount, rowManifestSha256: record.row_manifest_sha256 });
405
- } catch (error) {
406
- if (isReportingRowEncodingError(error)) throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", error.message);
407
- throw error;
408
- }
409
- if (manifests.length !== record.chunk_count) {
410
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "chunk count does not match the row set");
411
- }
412
- const locators = chunks.rows.map((chunk) => {
413
- const hasLocation = chunk.object_ref !== null;
414
- if (hasLocation !== (record.kind === "object")) {
415
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "chunk location does not match its binding kind");
416
- }
417
- return hasLocation ? Object.freeze({
418
- objectRef: chunk.object_ref,
419
- nativeVersionRef: chunk.native_version_ref,
420
- compression: chunk.compression,
421
- physicalSha256: chunk.physical_sha256,
422
- sizeBytes: Number(chunk.size_bytes)
423
- }) : void 0;
424
- });
425
- return Object.freeze({
426
- rowSetId: record.row_set_id,
427
- rowSetKind: record.row_set_kind,
428
- accountId: record.account_id,
429
- obligationId: record.obligation_id,
430
- digestProfile: record.digest_profile,
431
- contentSha256: record.content_sha256,
432
- contentSizeBytes: Number(record.content_size_bytes),
433
- rowCount,
434
- rowManifestSha256: record.row_manifest_sha256,
435
- bindingId: record.row_binding_id,
436
- bindingIdentitySha256: record.identity_sha256,
437
- kind: record.kind,
438
- manifests: Object.freeze(manifests),
439
- locators: Object.freeze(locators)
440
- });
441
- }
442
- /** Verified rows `[offset, offset + limit)` in ordinal order. */
443
- async readPage(handle, offset, limit, signal) {
444
- if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(limit) || limit < 1) {
445
- throw new ReportingRowStoreError("INVALID_INPUT", "row page bounds are invalid");
446
- }
447
- const end = Math.min(handle.rowCount, offset + limit);
448
- const rows = [];
449
- for (const manifest of handle.manifests) {
450
- if (offset >= end) break;
451
- const chunkEnd = manifest.first_ordinal + manifest.row_count;
452
- if (chunkEnd <= offset || manifest.first_ordinal >= end) continue;
453
- signal?.throwIfAborted();
454
- const from = manifest.segments.findIndex(
455
- (segment) => segment.first_ordinal + segment.row_count > Math.max(offset, manifest.first_ordinal)
456
- );
457
- let to = from;
458
- while (to + 1 < manifest.segments.length && manifest.segments[to + 1].first_ordinal < end) to += 1;
459
- const firstSegment = manifest.segments[from];
460
- const lastSegment = manifest.segments[to];
461
- const bytes = await this.readChunkRange(
462
- handle,
463
- manifest,
464
- firstSegment.byte_offset,
465
- lastSegment.byte_offset + lastSegment.byte_count - firstSegment.byte_offset,
466
- signal
467
- );
468
- const decoded = this.decode(() => decodeVerifiedReportingRowSegmentsV1(manifest, [from, to], bytes));
469
- const startIndex = Math.max(offset, firstSegment.first_ordinal) - firstSegment.first_ordinal;
470
- const endIndex = Math.min(end, lastSegment.first_ordinal + lastSegment.row_count) - firstSegment.first_ordinal;
471
- rows.push(...decoded.slice(startIndex, endIndex));
472
- }
473
- return rows;
474
- }
475
- /**
476
- * Every row, verified chunk by chunk and against the committed binding.
477
- * `controlTotals` are required for revision row sets.
478
- */
479
- async readAll(handle, controlTotals, options = {}) {
480
- if (options.maxBytes !== void 0 && handle.contentSizeBytes > options.maxBytes) {
481
- throw new ReportingRowStoreError("INVALID_INPUT", "row set exceeds the hydration byte limit");
482
- }
483
- const hasher = handle.digestProfile === "revision_envelope_v1" ? createReportingRevisionEnvelopeHasherV1({
484
- reporting_revision_id: handle.rowSetId,
485
- row_count: handle.rowCount,
486
- control_totals: controlTotals ?? []
487
- }) : createReportingRowsOnlyHasherV1(handle.rowCount);
488
- const rows = [];
489
- for (const manifest of handle.manifests) {
490
- options.signal?.throwIfAborted();
491
- const bytes = await this.readChunkRange(handle, manifest, 0, manifest.byte_count, options.signal);
492
- this.decode(() => verifyReportingRowChunkV1(manifest, bytes));
493
- hasher.update(bytes);
494
- rows.push(
495
- ...this.decode(() => decodeVerifiedReportingRowSegmentsV1(manifest, [0, manifest.segments.length - 1], bytes))
496
- );
497
- }
498
- const digest = this.decode(() => hasher.digest());
499
- if (digest.sha256 !== handle.contentSha256 || digest.byteCount !== handle.contentSizeBytes) {
500
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", "rows do not reproduce the committed binding");
501
- }
502
- return rows;
503
- }
504
- /**
505
- * Delete exact object versions through the configured binding's provider.
506
- * `absent` counts as success. Only object-kind bindings are accepted.
507
- */
508
- async deleteObjects(bindingId, objects, signal) {
509
- await this.ready();
510
- const binding = this.bindings[bindingId];
511
- if (!binding || binding.kind !== "object") {
512
- throw new ReportingRowStoreError("STATE_UNAVAILABLE", `row binding ${bindingId} is not configured for objects`);
513
- }
514
- const provider = this.providers[binding.provider];
515
- const ownedPrefix = `${binding.prefix}/${this.installation.namespaceKey}/`;
516
- const context = { signal: deadlineSignal(this.writeDeadline, signal) };
517
- for (const object of objects) {
518
- if (!object.objectRef.startsWith(ownedPrefix)) {
519
- throw new ReportingRowStoreError("UNSAFE_BINDING", "refusing to delete an object outside the owned prefix");
520
- }
521
- await this.provider(
522
- () => provider.delete(
523
- { ...providerLocation(binding), objectRef: object.objectRef, nativeVersionRef: object.nativeVersionRef },
524
- context
525
- )
526
- );
527
- }
528
- }
529
- /**
530
- * Prove each object is still present at its recorded version with the recorded size.
531
- * A commit that adopted an object another row set created calls this under the object's
532
- * lock, so a concurrent delete either finished first (this throws, the commit fails and
533
- * the writer re-uploads) or sees the commit's chunk reference and skips the delete.
534
- *
535
- * Native versions are write-unique (S3 VersionId, Azure versionId, GCS generation, filesystem
536
- * `ino:birthtime:mtime:size`), so a version-pinned metadata check, "this exact version exists at the
537
- * recorded size", proves the object unchanged without downloading it while the commit holds its
538
- * locks. Bytes were verified when the object was uploaded or adopted. A custom provider without
539
- * `stat` falls back to reading the whole object and comparing its physical digest.
540
- */
541
- async assertObjectsPresent(bindingId, objects, signal) {
542
- await this.ready();
543
- const binding = this.bindings[bindingId];
544
- if (!binding || binding.kind !== "object") {
545
- throw new ReportingRowStoreError("STATE_UNAVAILABLE", `row binding ${bindingId} is not configured for objects`);
546
- }
547
- const provider = this.providers[binding.provider];
548
- const context = { signal: deadlineSignal(this.writeDeadline, signal) };
549
- const location = providerLocation(binding);
550
- const changed = () => new ReportingRowStoreError(
551
- "STATE_UNAVAILABLE",
552
- "an adopted row object was deleted or replaced before its commit; retry the upload"
553
- );
554
- for (const object of objects) {
555
- const identity = { ...location, objectRef: object.objectRef, nativeVersionRef: object.nativeVersionRef };
556
- if (provider.stat) {
557
- const stat = await this.provider(() => provider.stat(identity, context));
558
- if (!stat || stat.nativeVersionRef !== object.nativeVersionRef || !Number.isSafeInteger(stat.sizeBytes) || stat.sizeBytes !== object.sizeBytes) {
559
- throw changed();
560
- }
561
- continue;
562
- }
563
- const bytes = await this.provider(
564
- () => provider.get({ ...identity, maxBytes: MAX_ADOPTED_OBJECT_BYTES }, context)
565
- );
566
- if (!bytes || sha256Hex(bytes) !== object.physicalSha256) throw changed();
567
- }
568
- }
569
- /**
570
- * A live row set in the same obligation and binding whose rows are
571
- * byte-identical (same manifest digest), with its chunk locations. Sharing
572
- * never crosses obligations, so it never crosses consumers.
573
- */
574
- async findShareable(queryable, prepared) {
575
- if (prepared.encoded.chunks.length === 0) return void 0;
576
- const found = await queryable.query(
577
- `SELECT row_set_id FROM adcp_reporting_row_sets
578
- WHERE obligation_id = $1 AND account_id = $2 AND row_binding_id = $3 AND row_manifest_sha256 = $4
579
- AND rows_state = 'live' AND row_set_id <> $5
580
- ORDER BY recorded_at DESC, row_set_id LIMIT 1
581
- FOR SHARE`,
582
- [
583
- prepared.input.obligationId,
584
- prepared.input.accountId,
585
- prepared.bindingId,
586
- prepared.encoded.rowManifestSha256,
587
- prepared.input.rowSetId
588
- ]
589
- );
590
- const rowSetId = found.rows[0]?.row_set_id;
591
- if (!rowSetId) return void 0;
592
- const chunks = await queryable.query(
593
- `SELECT object_ref, native_version_ref, compression, physical_sha256, size_bytes
594
- FROM adcp_reporting_row_chunks WHERE row_set_id = $1 ORDER BY chunk_index`,
595
- [rowSetId]
596
- );
597
- if (chunks.rows.length !== prepared.encoded.chunks.length) return void 0;
598
- if (prepared.binding.kind === "postgres") return { rowSetId, locators: [] };
599
- if (chunks.rows.some((chunk) => chunk.object_ref === null)) return void 0;
600
- return {
601
- rowSetId,
602
- locators: chunks.rows.map((chunk) => ({
603
- objectRef: chunk.object_ref,
604
- nativeVersionRef: chunk.native_version_ref,
605
- compression: chunk.compression,
606
- physicalSha256: chunk.physical_sha256,
607
- sizeBytes: Number(chunk.size_bytes),
608
- created: false
609
- }))
610
- };
611
- }
612
- /** Installation identity and namespace key; available after `ready()`. */
613
- get installationIdentity() {
614
- if (!this.installation) throw new ReportingRowStoreError("STATE_UNAVAILABLE", "row storage is not ready");
615
- return this.installation;
616
- }
617
- async readChunkRange(handle, manifest, byteOffset, byteCount, signal) {
618
- if (handle.kind === "postgres") {
619
- const result = await this.pool.query(
620
- `SELECT substring(body FROM $4::integer + 1 FOR $5::integer) AS bytes
621
- FROM adcp_reporting_chunk_bodies
622
- WHERE account_id = $1 AND obligation_id = $2 AND sha256 = $3`,
623
- [handle.accountId, handle.obligationId, manifest.sha256, byteOffset, byteCount]
624
- );
625
- const bytes = result.rows[0]?.bytes;
626
- if (!bytes) throw new ReportingRowStoreError("ROWS_UNAVAILABLE", `chunk ${manifest.chunk_index} body is missing`);
627
- return bytes;
628
- }
629
- const binding = this.bindings[handle.bindingId];
630
- if (!binding || binding.kind !== "object" || bindingIdentitySha256(binding) !== handle.bindingIdentitySha256) {
631
- throw new ReportingRowStoreError("STATE_UNAVAILABLE", `row binding ${handle.bindingId} is not configured`);
632
- }
633
- const locator = handle.locators[manifest.chunk_index];
634
- const provider = this.providers[binding.provider];
635
- const context = { signal: deadlineSignal(this.readDeadline, signal) };
636
- const location = providerLocation(binding);
637
- if (locator.compression === "none" && provider.rangedReads) {
638
- const bytes = await this.provider(
639
- () => provider.get(
640
- {
641
- ...location,
642
- objectRef: locator.objectRef,
643
- nativeVersionRef: locator.nativeVersionRef,
644
- range: { offset: byteOffset, length: byteCount },
645
- maxBytes: byteCount
646
- },
647
- context
648
- )
649
- );
650
- if (!bytes)
651
- throw new ReportingRowStoreError("ROWS_UNAVAILABLE", `chunk ${manifest.chunk_index} object is missing`);
652
- if (bytes.byteLength !== byteCount) {
653
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", `chunk ${manifest.chunk_index} range is truncated`);
654
- }
655
- return Buffer.from(bytes);
656
- }
657
- const stored = await this.provider(
658
- () => provider.get(
659
- {
660
- ...location,
661
- objectRef: locator.objectRef,
662
- nativeVersionRef: locator.nativeVersionRef,
663
- maxBytes: locator.sizeBytes
664
- },
665
- context
666
- )
667
- );
668
- if (!stored)
669
- throw new ReportingRowStoreError("ROWS_UNAVAILABLE", `chunk ${manifest.chunk_index} object is missing`);
670
- const physical = Buffer.from(stored);
671
- if (physical.byteLength !== locator.sizeBytes || sha256Hex(physical) !== locator.physicalSha256) {
672
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", `chunk ${manifest.chunk_index} stored bytes differ`);
673
- }
674
- const logical = this.inflate(physical, locator.compression, manifest.byte_count);
675
- if (!logical) {
676
- throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", `chunk ${manifest.chunk_index} does not decompress`);
677
- }
678
- return logical.subarray(byteOffset, byteOffset + byteCount);
679
- }
680
- inflate(physical, compression, byteCount) {
681
- if (compression === "none") return physical.byteLength === byteCount ? physical : null;
682
- try {
683
- const logical = gunzipSync(physical, { maxOutputLength: byteCount });
684
- return logical.byteLength === byteCount ? logical : null;
685
- } catch {
686
- return null;
687
- }
688
- }
689
- decode(operation) {
690
- try {
691
- return operation();
692
- } catch (error) {
693
- if (isReportingRowEncodingError(error)) throw new ReportingRowStoreError("ROWS_INTEGRITY_FAILED", error.message);
694
- throw error;
695
- }
696
- }
697
- /** Collapse provider failures to stable, secret-free codes. */
698
- async provider(operation) {
699
- try {
700
- return await operation();
701
- } catch (error) {
702
- if (isReportingRowStoreError(error)) throw error;
703
- if (error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError")) {
704
- throw new ReportingRowStoreError(error.name === "TimeoutError" ? "DEADLINE_EXCEEDED" : "ABORTED");
705
- }
706
- throw new ReportingRowStoreError("PROVIDER_UNAVAILABLE");
707
- }
708
- }
709
- async register() {
710
- this.assertBindingKeyTemplates();
711
- const { installationId } = await probeReportingRowStorageSchemaV1(this.pool);
712
- const namespaceKey = sha256Hex(canonicalize(["adcp.rows.v1", this.deploymentNamespace, installationId]));
713
- this.installation = { installationId, namespaceKey };
714
- for (const [bindingId, binding] of Object.entries(this.bindings)) {
715
- if (binding.kind === "object") {
716
- const provider = this.providers[binding.provider];
717
- await this.provider(
718
- () => provider.probe(
719
- { ...providerLocation(binding), prefix: binding.prefix },
720
- { signal: AbortSignal.timeout(this.readDeadline) }
721
- )
722
- );
723
- }
724
- const identityConfig = bindingIdentityConfig(binding);
725
- const identitySha256 = bindingIdentitySha256(binding);
726
- const operationalConfig = binding.kind === "object" ? {
727
- compression: binding.compression ?? "gzip",
728
- ...binding.credentialRef ? { credentialRef: binding.credentialRef } : {}
729
- } : {};
730
- await this.pool.query(
731
- `INSERT INTO adcp_reporting_row_bindings
732
- (row_binding_id, kind, provider, identity_config, identity_sha256, operational_config, namespace_key)
733
- VALUES ($1, $2, $3, $4::jsonb, $5, $6::jsonb, $7)
734
- ON CONFLICT (row_binding_id) DO NOTHING`,
735
- [
736
- bindingId,
737
- binding.kind,
738
- binding.kind === "object" ? binding.provider : "postgres",
739
- JSON.stringify(identityConfig),
740
- identitySha256,
741
- JSON.stringify(operationalConfig),
742
- namespaceKey
743
- ]
744
- );
745
- const stored = await this.pool.query(
746
- "SELECT identity_sha256, namespace_key, state FROM adcp_reporting_row_bindings WHERE row_binding_id = $1",
747
- [bindingId]
748
- );
749
- const row = stored.rows[0];
750
- if (!row || row.identity_sha256 !== identitySha256) {
751
- throw new ReportingRowStoreError(
752
- "UNSAFE_BINDING",
753
- `row binding ${bindingId} identity differs from its definition`
754
- );
755
- }
756
- if (row.namespace_key !== namespaceKey) {
757
- throw new ReportingRowStoreError("UNSAFE_BINDING", `row binding ${bindingId} belongs to another installation`);
758
- }
759
- }
760
- }
761
- }
762
- function bindingIdentityConfig(binding) {
763
- return binding.kind === "object" ? {
764
- location: { ...binding.location },
765
- prefix: binding.prefix,
766
- keyTemplate: binding.keyTemplate ?? REPORTING_ROW_DEFAULT_KEY_TEMPLATE
767
- } : {};
768
- }
769
- function bindingIdentitySha256(binding) {
770
- const provider = binding.kind === "object" ? binding.provider : binding.kind;
771
- return sha256Hex(canonicalize([binding.kind, provider, bindingIdentityConfig(binding)]));
772
- }
773
- function providerLocation(binding) {
774
- return { location: binding.location, ...binding.credentialRef ? { credentialRef: binding.credentialRef } : {} };
775
- }
776
- function deadlineSignal(milliseconds, signal) {
777
- const timeout = AbortSignal.timeout(milliseconds);
778
- return signal ? AbortSignal.any([signal, timeout]) : timeout;
779
- }
780
- function assertNativeVersionRef(value) {
781
- if (typeof value !== "string" || value.length < 1 || value.length > REPORTING_ROW_NATIVE_VERSION_REF_MAX_LENGTH) {
782
- throw new ReportingRowStoreError("PROVIDER_UNAVAILABLE", "provider returned an invalid native version reference");
783
- }
784
- }
785
- function positiveMilliseconds(value, name) {
786
- if (!Number.isSafeInteger(value) || value < 1e3 || value > 6e5) {
787
- throw new ReportingRowStoreError("INVALID_INPUT", `${name} must be between 1000 and 600000`);
788
- }
789
- return value;
790
- }
791
- function sha256Hex(value) {
792
- return createHash("sha256").update(value).digest("hex");
793
- }
794
- function reportingRowWriteIntentIdV1() {
795
- return randomUUID();
796
- }
797
- export {
798
- ReportingRowStorageV1,
799
- ReportingRowStoreError2 as ReportingRowStoreError,
800
- isReportingRowStoreError2 as isReportingRowStoreError,
801
- reportingRowWriteIntentIdV1
802
- };